1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/errno.h>
16#include <linux/bug.h>
17#include <linux/interrupt.h>
18#include <linux/device.h>
19#include <linux/pm_runtime.h>
20#include <linux/list.h>
21#include <linux/slab.h>
22
23#include <linux/videodev2.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
26#include <media/v4l2-mem2mem.h>
27#include <media/videobuf2-core.h>
28#include <media/videobuf2-dma-contig.h>
29
30#include "media-dev.h"
31#include "fimc-core.h"
32#include "fimc-reg.h"
33
34static int fimc_capture_hw_init(struct fimc_dev *fimc)
35{
36 struct fimc_source_info *si = &fimc->vid_cap.source_config;
37 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
38 int ret;
39 unsigned long flags;
40
41 if (ctx == NULL || ctx->s_frame.fmt == NULL)
42 return -EINVAL;
43
44 if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
45 ret = fimc_hw_camblk_cfg_writeback(fimc);
46 if (ret < 0)
47 return ret;
48 }
49
50 spin_lock_irqsave(&fimc->slock, flags);
51 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
52 fimc_set_yuv_order(ctx);
53
54 fimc_hw_set_camera_polarity(fimc, si);
55 fimc_hw_set_camera_type(fimc, si);
56 fimc_hw_set_camera_source(fimc, si);
57 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
58
59 ret = fimc_set_scaler_info(ctx);
60 if (!ret) {
61 fimc_hw_set_input_path(ctx);
62 fimc_hw_set_prescaler(ctx);
63 fimc_hw_set_mainscaler(ctx);
64 fimc_hw_set_target_format(ctx);
65 fimc_hw_set_rotation(ctx);
66 fimc_hw_set_effect(ctx);
67 fimc_hw_set_output_path(ctx);
68 fimc_hw_set_out_dma(ctx);
69 if (fimc->drv_data->alpha_color)
70 fimc_hw_set_rgb_alpha(ctx);
71 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
72 }
73 spin_unlock_irqrestore(&fimc->slock, flags);
74 return ret;
75}
76
77
78
79
80
81
82
83
84
85static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
86{
87 struct fimc_vid_cap *cap = &fimc->vid_cap;
88 struct fimc_vid_buffer *buf;
89 unsigned long flags;
90 bool streaming;
91
92 spin_lock_irqsave(&fimc->slock, flags);
93 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
94
95 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
96 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
97 if (suspend)
98 fimc->state |= (1 << ST_CAPT_SUSPENDED);
99 else
100 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
101
102
103 while (!suspend && !list_empty(&cap->pending_buf_q)) {
104 buf = fimc_pending_queue_pop(cap);
105 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
106 }
107
108 while (!list_empty(&cap->active_buf_q)) {
109 buf = fimc_active_queue_pop(cap);
110 if (suspend)
111 fimc_pending_queue_add(cap, buf);
112 else
113 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
114 }
115
116 fimc_hw_reset(fimc);
117 cap->buf_index = 0;
118
119 spin_unlock_irqrestore(&fimc->slock, flags);
120
121 if (streaming)
122 return fimc_pipeline_call(fimc, set_stream,
123 &fimc->pipeline, 0);
124 else
125 return 0;
126}
127
128static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
129{
130 unsigned long flags;
131
132 if (!fimc_capture_active(fimc))
133 return 0;
134
135 spin_lock_irqsave(&fimc->slock, flags);
136 set_bit(ST_CAPT_SHUT, &fimc->state);
137 fimc_deactivate_capture(fimc);
138 spin_unlock_irqrestore(&fimc->slock, flags);
139
140 wait_event_timeout(fimc->irq_queue,
141 !test_bit(ST_CAPT_SHUT, &fimc->state),
142 (2*HZ/10));
143
144 return fimc_capture_state_cleanup(fimc, suspend);
145}
146
147
148
149
150
151
152
153
154static int fimc_capture_config_update(struct fimc_ctx *ctx)
155{
156 struct fimc_dev *fimc = ctx->fimc_dev;
157 int ret;
158
159 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
160
161 ret = fimc_set_scaler_info(ctx);
162 if (ret)
163 return ret;
164
165 fimc_hw_set_prescaler(ctx);
166 fimc_hw_set_mainscaler(ctx);
167 fimc_hw_set_target_format(ctx);
168 fimc_hw_set_rotation(ctx);
169 fimc_hw_set_effect(ctx);
170 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
171 fimc_hw_set_out_dma(ctx);
172 if (fimc->drv_data->alpha_color)
173 fimc_hw_set_rgb_alpha(ctx);
174
175 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
176 return ret;
177}
178
179void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
180{
181 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
182 struct fimc_vid_cap *cap = &fimc->vid_cap;
183 struct fimc_frame *f = &cap->ctx->d_frame;
184 struct fimc_vid_buffer *v_buf;
185 struct timeval *tv;
186 struct timespec ts;
187
188 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
189 wake_up(&fimc->irq_queue);
190 goto done;
191 }
192
193 if (!list_empty(&cap->active_buf_q) &&
194 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
195 ktime_get_real_ts(&ts);
196
197 v_buf = fimc_active_queue_pop(cap);
198
199 tv = &v_buf->vb.v4l2_buf.timestamp;
200 tv->tv_sec = ts.tv_sec;
201 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
202 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
203
204 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
205 }
206
207 if (!list_empty(&cap->pending_buf_q)) {
208
209 v_buf = fimc_pending_queue_pop(cap);
210 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
211 v_buf->index = cap->buf_index;
212
213
214 fimc_active_queue_add(cap, v_buf);
215
216 dbg("next frame: %d, done frame: %d",
217 fimc_hw_get_frame_index(fimc), v_buf->index);
218
219 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
220 cap->buf_index = 0;
221 }
222
223
224
225
226 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
227 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
228 unsigned int size = f->payload[plane];
229 s32 index = fimc_hw_get_frame_index(fimc);
230 void *vaddr;
231
232 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
233 if (v_buf->index != index)
234 continue;
235 vaddr = vb2_plane_vaddr(&v_buf->vb, plane);
236 v4l2_subdev_call(csis, video, s_rx_buffer,
237 vaddr, &size);
238 break;
239 }
240 }
241
242 if (cap->active_buf_cnt == 0) {
243 if (deq_buf)
244 clear_bit(ST_CAPT_RUN, &fimc->state);
245
246 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
247 cap->buf_index = 0;
248 } else {
249 set_bit(ST_CAPT_RUN, &fimc->state);
250 }
251
252 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
253 fimc_capture_config_update(cap->ctx);
254done:
255 if (cap->active_buf_cnt == 1) {
256 fimc_deactivate_capture(fimc);
257 clear_bit(ST_CAPT_STREAM, &fimc->state);
258 }
259
260 dbg("frame: %d, active_buf_cnt: %d",
261 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
262}
263
264
265static int start_streaming(struct vb2_queue *q, unsigned int count)
266{
267 struct fimc_ctx *ctx = q->drv_priv;
268 struct fimc_dev *fimc = ctx->fimc_dev;
269 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
270 int min_bufs;
271 int ret;
272
273 vid_cap->frame_count = 0;
274
275 ret = fimc_capture_hw_init(fimc);
276 if (ret) {
277 fimc_capture_state_cleanup(fimc, false);
278 return ret;
279 }
280
281 set_bit(ST_CAPT_PEND, &fimc->state);
282
283 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
284
285 if (vid_cap->active_buf_cnt >= min_bufs &&
286 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
287 fimc_activate_capture(ctx);
288
289 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
290 return fimc_pipeline_call(fimc, set_stream,
291 &fimc->pipeline, 1);
292 }
293
294 return 0;
295}
296
297static int stop_streaming(struct vb2_queue *q)
298{
299 struct fimc_ctx *ctx = q->drv_priv;
300 struct fimc_dev *fimc = ctx->fimc_dev;
301
302 if (!fimc_capture_active(fimc))
303 return -EINVAL;
304
305 return fimc_stop_capture(fimc, false);
306}
307
308int fimc_capture_suspend(struct fimc_dev *fimc)
309{
310 bool suspend = fimc_capture_busy(fimc);
311
312 int ret = fimc_stop_capture(fimc, suspend);
313 if (ret)
314 return ret;
315 return fimc_pipeline_call(fimc, close, &fimc->pipeline);
316}
317
318static void buffer_queue(struct vb2_buffer *vb);
319
320int fimc_capture_resume(struct fimc_dev *fimc)
321{
322 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
323 struct fimc_vid_buffer *buf;
324 int i;
325
326 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
327 return 0;
328
329 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
330 vid_cap->buf_index = 0;
331 fimc_pipeline_call(fimc, open, &fimc->pipeline,
332 &vid_cap->vfd.entity, false);
333 fimc_capture_hw_init(fimc);
334
335 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
336
337 for (i = 0; i < vid_cap->reqbufs_count; i++) {
338 if (list_empty(&vid_cap->pending_buf_q))
339 break;
340 buf = fimc_pending_queue_pop(vid_cap);
341 buffer_queue(&buf->vb);
342 }
343 return 0;
344
345}
346
347static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
348 unsigned int *num_buffers, unsigned int *num_planes,
349 unsigned int sizes[], void *allocators[])
350{
351 const struct v4l2_pix_format_mplane *pixm = NULL;
352 struct fimc_ctx *ctx = vq->drv_priv;
353 struct fimc_frame *frame = &ctx->d_frame;
354 struct fimc_fmt *fmt = frame->fmt;
355 unsigned long wh;
356 int i;
357
358 if (pfmt) {
359 pixm = &pfmt->fmt.pix_mp;
360 fmt = fimc_find_format(&pixm->pixelformat, NULL,
361 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
362 wh = pixm->width * pixm->height;
363 } else {
364 wh = frame->f_width * frame->f_height;
365 }
366
367 if (fmt == NULL)
368 return -EINVAL;
369
370 *num_planes = fmt->memplanes;
371
372 for (i = 0; i < fmt->memplanes; i++) {
373 unsigned int size = (wh * fmt->depth[i]) / 8;
374 if (pixm)
375 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
376 else if (fimc_fmt_is_user_defined(fmt->color))
377 sizes[i] = frame->payload[i];
378 else
379 sizes[i] = max_t(u32, size, frame->payload[i]);
380
381 allocators[i] = ctx->fimc_dev->alloc_ctx;
382 }
383
384 return 0;
385}
386
387static int buffer_prepare(struct vb2_buffer *vb)
388{
389 struct vb2_queue *vq = vb->vb2_queue;
390 struct fimc_ctx *ctx = vq->drv_priv;
391 int i;
392
393 if (ctx->d_frame.fmt == NULL)
394 return -EINVAL;
395
396 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
397 unsigned long size = ctx->d_frame.payload[i];
398
399 if (vb2_plane_size(vb, i) < size) {
400 v4l2_err(&ctx->fimc_dev->vid_cap.vfd,
401 "User buffer too small (%ld < %ld)\n",
402 vb2_plane_size(vb, i), size);
403 return -EINVAL;
404 }
405 vb2_set_plane_payload(vb, i, size);
406 }
407
408 return 0;
409}
410
411static void buffer_queue(struct vb2_buffer *vb)
412{
413 struct fimc_vid_buffer *buf
414 = container_of(vb, struct fimc_vid_buffer, vb);
415 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
416 struct fimc_dev *fimc = ctx->fimc_dev;
417 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
418 unsigned long flags;
419 int min_bufs;
420
421 spin_lock_irqsave(&fimc->slock, flags);
422 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
423
424 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
425 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
426 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
427
428 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
429 vid_cap->buf_index;
430
431 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
432 buf->index = vid_cap->buf_index;
433 fimc_active_queue_add(vid_cap, buf);
434
435 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
436 vid_cap->buf_index = 0;
437 } else {
438 fimc_pending_queue_add(vid_cap, buf);
439 }
440
441 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
442
443
444 if (vb2_is_streaming(&vid_cap->vbq) &&
445 vid_cap->active_buf_cnt >= min_bufs &&
446 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
447 int ret;
448
449 fimc_activate_capture(ctx);
450 spin_unlock_irqrestore(&fimc->slock, flags);
451
452 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
453 return;
454
455 ret = fimc_pipeline_call(fimc, set_stream, &fimc->pipeline, 1);
456 if (ret < 0)
457 v4l2_err(&vid_cap->vfd, "stream on failed: %d\n", ret);
458 return;
459 }
460 spin_unlock_irqrestore(&fimc->slock, flags);
461}
462
463static struct vb2_ops fimc_capture_qops = {
464 .queue_setup = queue_setup,
465 .buf_prepare = buffer_prepare,
466 .buf_queue = buffer_queue,
467 .wait_prepare = vb2_ops_wait_prepare,
468 .wait_finish = vb2_ops_wait_finish,
469 .start_streaming = start_streaming,
470 .stop_streaming = stop_streaming,
471};
472
473
474
475
476
477
478
479
480int fimc_capture_ctrls_create(struct fimc_dev *fimc)
481{
482 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
483 struct v4l2_subdev *sensor = fimc->pipeline.subdevs[IDX_SENSOR];
484 int ret;
485
486 if (WARN_ON(vid_cap->ctx == NULL))
487 return -ENXIO;
488 if (vid_cap->ctx->ctrls.ready)
489 return 0;
490
491 ret = fimc_ctrls_create(vid_cap->ctx);
492
493 if (ret || vid_cap->user_subdev_api || !sensor ||
494 !vid_cap->ctx->ctrls.ready)
495 return ret;
496
497 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrls.handler,
498 sensor->ctrl_handler, NULL);
499}
500
501static int fimc_capture_set_default_format(struct fimc_dev *fimc);
502
503static int fimc_capture_open(struct file *file)
504{
505 struct fimc_dev *fimc = video_drvdata(file);
506 int ret = -EBUSY;
507
508 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
509
510 fimc_md_graph_lock(fimc);
511 mutex_lock(&fimc->lock);
512
513 if (fimc_m2m_active(fimc))
514 goto unlock;
515
516 set_bit(ST_CAPT_BUSY, &fimc->state);
517 ret = pm_runtime_get_sync(&fimc->pdev->dev);
518 if (ret < 0)
519 goto unlock;
520
521 ret = v4l2_fh_open(file);
522 if (ret) {
523 pm_runtime_put(&fimc->pdev->dev);
524 goto unlock;
525 }
526
527 if (v4l2_fh_is_singular_file(file)) {
528 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
529 &fimc->vid_cap.vfd.entity, true);
530
531 if (!ret && !fimc->vid_cap.user_subdev_api)
532 ret = fimc_capture_set_default_format(fimc);
533
534 if (!ret)
535 ret = fimc_capture_ctrls_create(fimc);
536
537 if (ret < 0) {
538 clear_bit(ST_CAPT_BUSY, &fimc->state);
539 pm_runtime_put_sync(&fimc->pdev->dev);
540 v4l2_fh_release(file);
541 } else {
542 fimc->vid_cap.refcnt++;
543 }
544 }
545unlock:
546 mutex_unlock(&fimc->lock);
547 fimc_md_graph_unlock(fimc);
548 return ret;
549}
550
551static int fimc_capture_release(struct file *file)
552{
553 struct fimc_dev *fimc = video_drvdata(file);
554 struct fimc_vid_cap *vc = &fimc->vid_cap;
555 int ret;
556
557 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
558
559 mutex_lock(&fimc->lock);
560
561 if (v4l2_fh_is_singular_file(file)) {
562 if (vc->streaming) {
563 media_entity_pipeline_stop(&vc->vfd.entity);
564 vc->streaming = false;
565 }
566 clear_bit(ST_CAPT_BUSY, &fimc->state);
567 fimc_stop_capture(fimc, false);
568 fimc_pipeline_call(fimc, close, &fimc->pipeline);
569 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
570 fimc->vid_cap.refcnt--;
571 }
572
573 pm_runtime_put(&fimc->pdev->dev);
574
575 if (v4l2_fh_is_singular_file(file))
576 fimc_ctrls_delete(fimc->vid_cap.ctx);
577
578 ret = vb2_fop_release(file);
579 mutex_unlock(&fimc->lock);
580
581 return ret;
582}
583
584static const struct v4l2_file_operations fimc_capture_fops = {
585 .owner = THIS_MODULE,
586 .open = fimc_capture_open,
587 .release = fimc_capture_release,
588 .poll = vb2_fop_poll,
589 .unlocked_ioctl = video_ioctl2,
590 .mmap = vb2_fop_mmap,
591};
592
593
594
595
596
597static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
598 u32 *width, u32 *height,
599 u32 *code, u32 *fourcc, int pad)
600{
601 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
602 struct fimc_dev *fimc = ctx->fimc_dev;
603 const struct fimc_variant *var = fimc->variant;
604 const struct fimc_pix_limit *pl = var->pix_limit;
605 struct fimc_frame *dst = &ctx->d_frame;
606 u32 depth, min_w, max_w, min_h, align_h = 3;
607 u32 mask = FMT_FLAGS_CAM;
608 struct fimc_fmt *ffmt;
609
610
611 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
612 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
613 *code = ctx->s_frame.fmt->mbus_code;
614
615 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
616 mask |= FMT_FLAGS_M2M;
617
618 if (pad == FIMC_SD_PAD_SINK_FIFO)
619 mask = FMT_FLAGS_WRITEBACK;
620
621 ffmt = fimc_find_format(fourcc, code, mask, 0);
622 if (WARN_ON(!ffmt))
623 return NULL;
624
625 if (code)
626 *code = ffmt->mbus_code;
627 if (fourcc)
628 *fourcc = ffmt->fourcc;
629
630 if (pad != FIMC_SD_PAD_SOURCE) {
631 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
632 pl->scaler_dis_w : pl->scaler_en_w;
633
634 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
635 height, max_t(u32, *height, 32),
636 FIMC_CAMIF_MAX_HEIGHT,
637 fimc_fmt_is_user_defined(ffmt->color) ?
638 3 : 1,
639 0);
640 return ffmt;
641 }
642
643 if (fimc_fmt_is_user_defined(ffmt->color)) {
644 *width = ctx->s_frame.f_width;
645 *height = ctx->s_frame.f_height;
646 return ffmt;
647 }
648
649 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
650 if (ctx->state & FIMC_COMPOSE) {
651 min_w = dst->offs_h + dst->width;
652 min_h = dst->offs_v + dst->height;
653 } else {
654 min_w = var->min_out_pixsize;
655 min_h = var->min_out_pixsize;
656 }
657 if (var->min_vsize_align == 1 && !rotation)
658 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
659
660 depth = fimc_get_format_depth(ffmt);
661 v4l_bound_align_image(width, min_w, max_w,
662 ffs(var->min_out_pixsize) - 1,
663 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
664 align_h,
665 64/(ALIGN(depth, 8)));
666
667 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
668 pad, code ? *code : 0, *width, *height,
669 dst->f_width, dst->f_height);
670
671 return ffmt;
672}
673
674static void fimc_capture_try_selection(struct fimc_ctx *ctx,
675 struct v4l2_rect *r,
676 int target)
677{
678 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
679 struct fimc_dev *fimc = ctx->fimc_dev;
680 const struct fimc_variant *var = fimc->variant;
681 const struct fimc_pix_limit *pl = var->pix_limit;
682 struct fimc_frame *sink = &ctx->s_frame;
683 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
684 u32 align_sz = 0, align_h = 4;
685 u32 max_sc_h, max_sc_v;
686
687
688 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
689 r->width = sink->f_width;
690 r->height = sink->f_height;
691 r->left = r->top = 0;
692 return;
693 }
694 if (target == V4L2_SEL_TGT_COMPOSE) {
695 if (ctx->rotation != 90 && ctx->rotation != 270)
696 align_h = 1;
697 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
698 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
699 min_sz = var->min_out_pixsize;
700 } else {
701 u32 depth = fimc_get_format_depth(sink->fmt);
702 align_sz = 64/ALIGN(depth, 8);
703 min_sz = var->min_inp_pixsize;
704 min_w = min_h = min_sz;
705 max_sc_h = max_sc_v = 1;
706 }
707
708
709
710
711
712
713
714
715
716 max_w = min_t(u32,
717 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
718 rotate ? sink->f_height : sink->f_width);
719 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
720
721 if (target == V4L2_SEL_TGT_COMPOSE) {
722 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
723 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
724 if (rotate) {
725 swap(max_sc_h, max_sc_v);
726 swap(min_w, min_h);
727 }
728 }
729 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
730 &r->height, min_h, max_h, align_h,
731 align_sz);
732
733 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
734 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
735 r->left = round_down(r->left, var->hor_offs_align);
736
737 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
738 target, r->left, r->top, r->width, r->height,
739 sink->f_width, sink->f_height);
740}
741
742
743
744
745static int fimc_cap_querycap(struct file *file, void *priv,
746 struct v4l2_capability *cap)
747{
748 struct fimc_dev *fimc = video_drvdata(file);
749
750 __fimc_vidioc_querycap(&fimc->pdev->dev, cap, V4L2_CAP_STREAMING |
751 V4L2_CAP_VIDEO_CAPTURE_MPLANE);
752 return 0;
753}
754
755static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
756 struct v4l2_fmtdesc *f)
757{
758 struct fimc_fmt *fmt;
759
760 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
761 f->index);
762 if (!fmt)
763 return -EINVAL;
764 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
765 f->pixelformat = fmt->fourcc;
766 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
767 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
768 return 0;
769}
770
771static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
772{
773 struct media_pad *pad = &me->pads[0];
774
775 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
776 pad = media_entity_remote_source(pad);
777 if (!pad)
778 break;
779 me = pad->entity;
780 pad = &me->pads[0];
781 }
782
783 return me;
784}
785
786
787
788
789
790
791
792
793
794static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
795 struct v4l2_mbus_framefmt *tfmt,
796 struct fimc_fmt **fmt_id,
797 bool set)
798{
799 struct fimc_dev *fimc = ctx->fimc_dev;
800 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
801 struct v4l2_subdev_format sfmt;
802 struct v4l2_mbus_framefmt *mf = &sfmt.format;
803 struct media_entity *me;
804 struct fimc_fmt *ffmt;
805 struct media_pad *pad;
806 int ret, i = 1;
807 u32 fcc;
808
809 if (WARN_ON(!sd || !tfmt))
810 return -EINVAL;
811
812 memset(&sfmt, 0, sizeof(sfmt));
813 sfmt.format = *tfmt;
814 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
815
816 me = fimc_pipeline_get_head(&sd->entity);
817
818 while (1) {
819 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
820 FMT_FLAGS_CAM, i++);
821 if (ffmt == NULL) {
822
823
824
825
826 return -EINVAL;
827 }
828 mf->code = tfmt->code = ffmt->mbus_code;
829
830
831 while (me != &fimc->vid_cap.subdev.entity) {
832 sd = media_entity_to_v4l2_subdev(me);
833
834 sfmt.pad = 0;
835 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
836 if (ret)
837 return ret;
838
839 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
840 sfmt.pad = me->num_pads - 1;
841 mf->code = tfmt->code;
842 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
843 &sfmt);
844 if (ret)
845 return ret;
846 }
847
848 pad = media_entity_remote_source(&me->pads[sfmt.pad]);
849 if (!pad)
850 return -EINVAL;
851 me = pad->entity;
852 }
853
854 if (mf->code != tfmt->code)
855 continue;
856
857 fcc = ffmt->fourcc;
858 tfmt->width = mf->width;
859 tfmt->height = mf->height;
860 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
861 NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
862 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
863 NULL, &fcc, FIMC_SD_PAD_SOURCE);
864 if (ffmt && ffmt->mbus_code)
865 mf->code = ffmt->mbus_code;
866 if (mf->width != tfmt->width || mf->height != tfmt->height)
867 continue;
868 tfmt->code = mf->code;
869 break;
870 }
871
872 if (fmt_id && ffmt)
873 *fmt_id = ffmt;
874 *tfmt = *mf;
875
876 return 0;
877}
878
879
880
881
882
883
884
885
886
887static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
888 struct v4l2_plane_pix_format *plane_fmt,
889 unsigned int num_planes, bool try)
890{
891 struct v4l2_mbus_frame_desc fd;
892 int i, ret;
893 int pad;
894
895 for (i = 0; i < num_planes; i++)
896 fd.entry[i].length = plane_fmt[i].sizeimage;
897
898 pad = sensor->entity.num_pads - 1;
899 if (try)
900 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
901 else
902 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
903
904 if (ret < 0)
905 return ret;
906
907 if (num_planes != fd.num_entries)
908 return -EINVAL;
909
910 for (i = 0; i < num_planes; i++)
911 plane_fmt[i].sizeimage = fd.entry[i].length;
912
913 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
914 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
915 fd.entry[0].length);
916
917 return -EINVAL;
918 }
919
920 return 0;
921}
922
923static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
924 struct v4l2_format *f)
925{
926 struct fimc_dev *fimc = video_drvdata(file);
927
928 __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
929 return 0;
930}
931
932static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
933 struct v4l2_format *f)
934{
935 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
936 struct fimc_dev *fimc = video_drvdata(file);
937 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
938 struct v4l2_mbus_framefmt mf;
939 struct fimc_fmt *ffmt = NULL;
940 int ret = 0;
941
942 fimc_md_graph_lock(fimc);
943 mutex_lock(&fimc->lock);
944
945 if (fimc_jpeg_fourcc(pix->pixelformat)) {
946 fimc_capture_try_format(ctx, &pix->width, &pix->height,
947 NULL, &pix->pixelformat,
948 FIMC_SD_PAD_SINK_CAM);
949 ctx->s_frame.f_width = pix->width;
950 ctx->s_frame.f_height = pix->height;
951 }
952 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
953 NULL, &pix->pixelformat,
954 FIMC_SD_PAD_SOURCE);
955 if (!ffmt) {
956 ret = -EINVAL;
957 goto unlock;
958 }
959
960 if (!fimc->vid_cap.user_subdev_api) {
961 mf.width = pix->width;
962 mf.height = pix->height;
963 mf.code = ffmt->mbus_code;
964 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
965 pix->width = mf.width;
966 pix->height = mf.height;
967 if (ffmt)
968 pix->pixelformat = ffmt->fourcc;
969 }
970
971 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
972
973 if (ffmt->flags & FMT_FLAGS_COMPRESSED)
974 fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
975 pix->plane_fmt, ffmt->memplanes, true);
976unlock:
977 mutex_unlock(&fimc->lock);
978 fimc_md_graph_unlock(fimc);
979
980 return ret;
981}
982
983static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
984 enum fimc_color_fmt color)
985{
986 bool jpeg = fimc_fmt_is_user_defined(color);
987
988 ctx->scaler.enabled = !jpeg;
989 fimc_ctrls_activate(ctx, !jpeg);
990
991 if (jpeg)
992 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
993 else
994 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
995}
996
997static int __fimc_capture_set_format(struct fimc_dev *fimc,
998 struct v4l2_format *f)
999{
1000 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1001 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1002 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.ci_fmt;
1003 struct fimc_frame *ff = &ctx->d_frame;
1004 struct fimc_fmt *s_fmt = NULL;
1005 int ret, i;
1006
1007 if (vb2_is_busy(&fimc->vid_cap.vbq))
1008 return -EBUSY;
1009
1010
1011 if (fimc_jpeg_fourcc(pix->pixelformat)) {
1012 fimc_capture_try_format(ctx, &pix->width, &pix->height,
1013 NULL, &pix->pixelformat,
1014 FIMC_SD_PAD_SINK_CAM);
1015 ctx->s_frame.f_width = pix->width;
1016 ctx->s_frame.f_height = pix->height;
1017 }
1018
1019 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
1020 NULL, &pix->pixelformat,
1021 FIMC_SD_PAD_SOURCE);
1022 if (!ff->fmt)
1023 return -EINVAL;
1024
1025
1026 fimc_alpha_ctrl_update(ctx);
1027
1028
1029 if (!fimc->vid_cap.user_subdev_api) {
1030 mf->code = ff->fmt->mbus_code;
1031 mf->width = pix->width;
1032 mf->height = pix->height;
1033 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
1034 if (ret)
1035 return ret;
1036
1037 pix->width = mf->width;
1038 pix->height = mf->height;
1039 }
1040
1041 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
1042
1043 if (ff->fmt->flags & FMT_FLAGS_COMPRESSED) {
1044 ret = fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
1045 pix->plane_fmt, ff->fmt->memplanes,
1046 true);
1047 if (ret < 0)
1048 return ret;
1049 }
1050
1051 for (i = 0; i < ff->fmt->memplanes; i++) {
1052 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
1053 ff->payload[i] = pix->plane_fmt[i].sizeimage;
1054 }
1055
1056 set_frame_bounds(ff, pix->width, pix->height);
1057
1058 if (!(ctx->state & FIMC_COMPOSE))
1059 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1060
1061 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
1062
1063
1064 if (!fimc->vid_cap.user_subdev_api) {
1065 ctx->s_frame.fmt = s_fmt;
1066 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1067 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1068 }
1069
1070 return ret;
1071}
1072
1073static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1074 struct v4l2_format *f)
1075{
1076 struct fimc_dev *fimc = video_drvdata(file);
1077 int ret;
1078
1079 fimc_md_graph_lock(fimc);
1080 mutex_lock(&fimc->lock);
1081
1082
1083
1084
1085
1086
1087
1088
1089 ret = __fimc_capture_set_format(fimc, f);
1090
1091 mutex_unlock(&fimc->lock);
1092 fimc_md_graph_unlock(fimc);
1093 return ret;
1094}
1095
1096static int fimc_cap_enum_input(struct file *file, void *priv,
1097 struct v4l2_input *i)
1098{
1099 struct fimc_dev *fimc = video_drvdata(file);
1100 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
1101
1102 if (i->index != 0)
1103 return -EINVAL;
1104
1105 i->type = V4L2_INPUT_TYPE_CAMERA;
1106 if (sd)
1107 strlcpy(i->name, sd->name, sizeof(i->name));
1108 return 0;
1109}
1110
1111static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
1112{
1113 return i == 0 ? i : -EINVAL;
1114}
1115
1116static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
1117{
1118 *i = 0;
1119 return 0;
1120}
1121
1122
1123
1124
1125
1126
1127
1128static int fimc_pipeline_validate(struct fimc_dev *fimc)
1129{
1130 struct v4l2_subdev_format sink_fmt, src_fmt;
1131 struct fimc_vid_cap *vc = &fimc->vid_cap;
1132 struct v4l2_subdev *sd = &vc->subdev;
1133 struct media_pad *sink_pad, *src_pad;
1134 int i, ret;
1135
1136 while (1) {
1137
1138
1139
1140
1141
1142 src_pad = NULL;
1143
1144 for (i = 0; i < sd->entity.num_pads; i++) {
1145 struct media_pad *p = &sd->entity.pads[i];
1146
1147 if (p->flags & MEDIA_PAD_FL_SINK) {
1148 sink_pad = p;
1149 src_pad = media_entity_remote_source(sink_pad);
1150 if (src_pad)
1151 break;
1152 }
1153 }
1154
1155 if (src_pad == NULL ||
1156 media_entity_type(src_pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1157 break;
1158
1159
1160 if (sd == &vc->subdev) {
1161 struct fimc_frame *ff = &vc->ctx->s_frame;
1162 sink_fmt.format.width = ff->f_width;
1163 sink_fmt.format.height = ff->f_height;
1164 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1165 } else {
1166 sink_fmt.pad = sink_pad->index;
1167 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1168 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1169 if (ret < 0 && ret != -ENOIOCTLCMD)
1170 return -EPIPE;
1171 }
1172
1173
1174 sd = media_entity_to_v4l2_subdev(src_pad->entity);
1175 src_fmt.pad = src_pad->index;
1176 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1177 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1178 if (ret < 0 && ret != -ENOIOCTLCMD)
1179 return -EPIPE;
1180
1181 if (src_fmt.format.width != sink_fmt.format.width ||
1182 src_fmt.format.height != sink_fmt.format.height ||
1183 src_fmt.format.code != sink_fmt.format.code)
1184 return -EPIPE;
1185
1186 if (sd == fimc->pipeline.subdevs[IDX_SENSOR] &&
1187 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1188 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1189 struct fimc_frame *frame = &vc->ctx->d_frame;
1190 unsigned int i;
1191
1192 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1193 frame->fmt->memplanes,
1194 false);
1195 if (ret < 0)
1196 return -EPIPE;
1197
1198 for (i = 0; i < frame->fmt->memplanes; i++)
1199 if (frame->payload[i] < plane_fmt[i].sizeimage)
1200 return -EPIPE;
1201 }
1202 }
1203 return 0;
1204}
1205
1206static int fimc_cap_streamon(struct file *file, void *priv,
1207 enum v4l2_buf_type type)
1208{
1209 struct fimc_dev *fimc = video_drvdata(file);
1210 struct fimc_pipeline *p = &fimc->pipeline;
1211 struct fimc_vid_cap *vc = &fimc->vid_cap;
1212 struct media_entity *entity = &vc->vfd.entity;
1213 struct fimc_source_info *si = NULL;
1214 struct v4l2_subdev *sd;
1215 int ret;
1216
1217 if (fimc_capture_active(fimc))
1218 return -EBUSY;
1219
1220 ret = media_entity_pipeline_start(entity, p->m_pipeline);
1221 if (ret < 0)
1222 return ret;
1223
1224 sd = p->subdevs[IDX_SENSOR];
1225 if (sd)
1226 si = v4l2_get_subdev_hostdata(sd);
1227
1228 if (si == NULL) {
1229 ret = -EPIPE;
1230 goto err_p_stop;
1231 }
1232
1233
1234
1235
1236 vc->source_config = *si;
1237
1238 if (vc->input == GRP_ID_FIMC_IS)
1239 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1240
1241 if (vc->user_subdev_api) {
1242 ret = fimc_pipeline_validate(fimc);
1243 if (ret < 0)
1244 goto err_p_stop;
1245 }
1246
1247 ret = vb2_ioctl_streamon(file, priv, type);
1248 if (!ret) {
1249 vc->streaming = true;
1250 return ret;
1251 }
1252
1253err_p_stop:
1254 media_entity_pipeline_stop(entity);
1255 return ret;
1256}
1257
1258static int fimc_cap_streamoff(struct file *file, void *priv,
1259 enum v4l2_buf_type type)
1260{
1261 struct fimc_dev *fimc = video_drvdata(file);
1262 int ret;
1263
1264 ret = vb2_ioctl_streamoff(file, priv, type);
1265 if (ret < 0)
1266 return ret;
1267
1268 media_entity_pipeline_stop(&fimc->vid_cap.vfd.entity);
1269 fimc->vid_cap.streaming = false;
1270 return 0;
1271}
1272
1273static int fimc_cap_reqbufs(struct file *file, void *priv,
1274 struct v4l2_requestbuffers *reqbufs)
1275{
1276 struct fimc_dev *fimc = video_drvdata(file);
1277 int ret;
1278
1279 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
1280
1281 if (!ret)
1282 fimc->vid_cap.reqbufs_count = reqbufs->count;
1283
1284 return ret;
1285}
1286
1287static int fimc_cap_g_selection(struct file *file, void *fh,
1288 struct v4l2_selection *s)
1289{
1290 struct fimc_dev *fimc = video_drvdata(file);
1291 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1292 struct fimc_frame *f = &ctx->s_frame;
1293
1294 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1295 return -EINVAL;
1296
1297 switch (s->target) {
1298 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1299 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1300 f = &ctx->d_frame;
1301
1302 case V4L2_SEL_TGT_CROP_BOUNDS:
1303 case V4L2_SEL_TGT_CROP_DEFAULT:
1304 s->r.left = 0;
1305 s->r.top = 0;
1306 s->r.width = f->o_width;
1307 s->r.height = f->o_height;
1308 return 0;
1309
1310 case V4L2_SEL_TGT_COMPOSE:
1311 f = &ctx->d_frame;
1312
1313 case V4L2_SEL_TGT_CROP:
1314 s->r.left = f->offs_h;
1315 s->r.top = f->offs_v;
1316 s->r.width = f->width;
1317 s->r.height = f->height;
1318 return 0;
1319 }
1320
1321 return -EINVAL;
1322}
1323
1324
1325static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1326{
1327 if (a->left < b->left || a->top < b->top)
1328 return 0;
1329 if (a->left + a->width > b->left + b->width)
1330 return 0;
1331 if (a->top + a->height > b->top + b->height)
1332 return 0;
1333
1334 return 1;
1335}
1336
1337static int fimc_cap_s_selection(struct file *file, void *fh,
1338 struct v4l2_selection *s)
1339{
1340 struct fimc_dev *fimc = video_drvdata(file);
1341 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1342 struct v4l2_rect rect = s->r;
1343 struct fimc_frame *f;
1344 unsigned long flags;
1345
1346 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1347 return -EINVAL;
1348
1349 if (s->target == V4L2_SEL_TGT_COMPOSE)
1350 f = &ctx->d_frame;
1351 else if (s->target == V4L2_SEL_TGT_CROP)
1352 f = &ctx->s_frame;
1353 else
1354 return -EINVAL;
1355
1356 fimc_capture_try_selection(ctx, &rect, s->target);
1357
1358 if (s->flags & V4L2_SEL_FLAG_LE &&
1359 !enclosed_rectangle(&rect, &s->r))
1360 return -ERANGE;
1361
1362 if (s->flags & V4L2_SEL_FLAG_GE &&
1363 !enclosed_rectangle(&s->r, &rect))
1364 return -ERANGE;
1365
1366 s->r = rect;
1367 spin_lock_irqsave(&fimc->slock, flags);
1368 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1369 s->r.height);
1370 spin_unlock_irqrestore(&fimc->slock, flags);
1371
1372 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1373 return 0;
1374}
1375
1376static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1377 .vidioc_querycap = fimc_cap_querycap,
1378
1379 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1380 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1381 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1382 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1383
1384 .vidioc_reqbufs = fimc_cap_reqbufs,
1385 .vidioc_querybuf = vb2_ioctl_querybuf,
1386 .vidioc_qbuf = vb2_ioctl_qbuf,
1387 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1388 .vidioc_expbuf = vb2_ioctl_expbuf,
1389 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1390 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1391
1392 .vidioc_streamon = fimc_cap_streamon,
1393 .vidioc_streamoff = fimc_cap_streamoff,
1394
1395 .vidioc_g_selection = fimc_cap_g_selection,
1396 .vidioc_s_selection = fimc_cap_s_selection,
1397
1398 .vidioc_enum_input = fimc_cap_enum_input,
1399 .vidioc_s_input = fimc_cap_s_input,
1400 .vidioc_g_input = fimc_cap_g_input,
1401};
1402
1403
1404static int fimc_link_setup(struct media_entity *entity,
1405 const struct media_pad *local,
1406 const struct media_pad *remote, u32 flags)
1407{
1408 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1409 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1410
1411 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1412 return -EINVAL;
1413
1414 if (WARN_ON(fimc == NULL))
1415 return 0;
1416
1417 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1418 local->entity->name, remote->entity->name, flags,
1419 fimc->vid_cap.input);
1420
1421 if (flags & MEDIA_LNK_FL_ENABLED) {
1422 if (fimc->vid_cap.input != 0)
1423 return -EBUSY;
1424 fimc->vid_cap.input = sd->grp_id;
1425 return 0;
1426 }
1427
1428 fimc->vid_cap.input = 0;
1429 return 0;
1430}
1431
1432static const struct media_entity_operations fimc_sd_media_ops = {
1433 .link_setup = fimc_link_setup,
1434};
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1450 void *arg)
1451{
1452 struct fimc_source_info *si;
1453 struct fimc_vid_buffer *buf;
1454 struct fimc_md *fmd;
1455 struct fimc_dev *fimc;
1456 unsigned long flags;
1457
1458 if (sd == NULL)
1459 return;
1460
1461 si = v4l2_get_subdev_hostdata(sd);
1462 fmd = entity_to_fimc_mdev(&sd->entity);
1463
1464 spin_lock_irqsave(&fmd->slock, flags);
1465
1466 fimc = si ? source_to_sensor_info(si)->host : NULL;
1467
1468 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1469 test_bit(ST_CAPT_PEND, &fimc->state)) {
1470 unsigned long irq_flags;
1471 spin_lock_irqsave(&fimc->slock, irq_flags);
1472 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1473 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1474 struct fimc_vid_buffer, list);
1475 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1476 }
1477 fimc_capture_irq_handler(fimc, 1);
1478 fimc_deactivate_capture(fimc);
1479 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1480 }
1481 spin_unlock_irqrestore(&fmd->slock, flags);
1482}
1483
1484static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1485 struct v4l2_subdev_fh *fh,
1486 struct v4l2_subdev_mbus_code_enum *code)
1487{
1488 struct fimc_fmt *fmt;
1489
1490 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1491 if (!fmt)
1492 return -EINVAL;
1493 code->code = fmt->mbus_code;
1494 return 0;
1495}
1496
1497static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1498 struct v4l2_subdev_fh *fh,
1499 struct v4l2_subdev_format *fmt)
1500{
1501 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1502 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1503 struct fimc_frame *ff = &ctx->s_frame;
1504 struct v4l2_mbus_framefmt *mf;
1505
1506 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1507 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1508 fmt->format = *mf;
1509 return 0;
1510 }
1511
1512 mf = &fmt->format;
1513 mutex_lock(&fimc->lock);
1514
1515 switch (fmt->pad) {
1516 case FIMC_SD_PAD_SOURCE:
1517 if (!WARN_ON(ff->fmt == NULL))
1518 mf->code = ff->fmt->mbus_code;
1519
1520 mf->width = ff->width;
1521 mf->height = ff->height;
1522 break;
1523 case FIMC_SD_PAD_SINK_FIFO:
1524 *mf = fimc->vid_cap.wb_fmt;
1525 break;
1526 case FIMC_SD_PAD_SINK_CAM:
1527 default:
1528 *mf = fimc->vid_cap.ci_fmt;
1529 break;
1530 }
1531
1532 mutex_unlock(&fimc->lock);
1533 mf->colorspace = V4L2_COLORSPACE_JPEG;
1534
1535 return 0;
1536}
1537
1538static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1539 struct v4l2_subdev_fh *fh,
1540 struct v4l2_subdev_format *fmt)
1541{
1542 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1543 struct v4l2_mbus_framefmt *mf = &fmt->format;
1544 struct fimc_vid_cap *vc = &fimc->vid_cap;
1545 struct fimc_ctx *ctx = vc->ctx;
1546 struct fimc_frame *ff;
1547 struct fimc_fmt *ffmt;
1548
1549 dbg("pad%d: code: 0x%x, %dx%d",
1550 fmt->pad, mf->code, mf->width, mf->height);
1551
1552 if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
1553 return -EBUSY;
1554
1555 mutex_lock(&fimc->lock);
1556 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1557 &mf->code, NULL, fmt->pad);
1558 mutex_unlock(&fimc->lock);
1559 mf->colorspace = V4L2_COLORSPACE_JPEG;
1560
1561 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1562 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1563 *mf = fmt->format;
1564 return 0;
1565 }
1566
1567 if (WARN_ON(ffmt == NULL))
1568 return -EINVAL;
1569
1570
1571 fimc_alpha_ctrl_update(ctx);
1572
1573 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
1574 if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1575 ff = &ctx->d_frame;
1576
1577 mf->width = ctx->s_frame.width;
1578 mf->height = ctx->s_frame.height;
1579 } else {
1580 ff = &ctx->s_frame;
1581 }
1582
1583 mutex_lock(&fimc->lock);
1584 set_frame_bounds(ff, mf->width, mf->height);
1585
1586 if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1587 vc->wb_fmt = *mf;
1588 else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1589 vc->ci_fmt = *mf;
1590
1591 ff->fmt = ffmt;
1592
1593
1594 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
1595 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1596
1597 if (fmt->pad != FIMC_SD_PAD_SOURCE)
1598 ctx->state &= ~FIMC_COMPOSE;
1599
1600 mutex_unlock(&fimc->lock);
1601 return 0;
1602}
1603
1604static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1605 struct v4l2_subdev_fh *fh,
1606 struct v4l2_subdev_selection *sel)
1607{
1608 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1609 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1610 struct fimc_frame *f = &ctx->s_frame;
1611 struct v4l2_rect *r = &sel->r;
1612 struct v4l2_rect *try_sel;
1613
1614 if (sel->pad == FIMC_SD_PAD_SOURCE)
1615 return -EINVAL;
1616
1617 mutex_lock(&fimc->lock);
1618
1619 switch (sel->target) {
1620 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1621 f = &ctx->d_frame;
1622
1623 case V4L2_SEL_TGT_CROP_BOUNDS:
1624 r->width = f->o_width;
1625 r->height = f->o_height;
1626 r->left = 0;
1627 r->top = 0;
1628 mutex_unlock(&fimc->lock);
1629 return 0;
1630
1631 case V4L2_SEL_TGT_CROP:
1632 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1633 break;
1634 case V4L2_SEL_TGT_COMPOSE:
1635 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1636 f = &ctx->d_frame;
1637 break;
1638 default:
1639 mutex_unlock(&fimc->lock);
1640 return -EINVAL;
1641 }
1642
1643 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1644 sel->r = *try_sel;
1645 } else {
1646 r->left = f->offs_h;
1647 r->top = f->offs_v;
1648 r->width = f->width;
1649 r->height = f->height;
1650 }
1651
1652 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1653 sel->pad, r->left, r->top, r->width, r->height,
1654 f->f_width, f->f_height);
1655
1656 mutex_unlock(&fimc->lock);
1657 return 0;
1658}
1659
1660static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1661 struct v4l2_subdev_fh *fh,
1662 struct v4l2_subdev_selection *sel)
1663{
1664 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1665 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1666 struct fimc_frame *f = &ctx->s_frame;
1667 struct v4l2_rect *r = &sel->r;
1668 struct v4l2_rect *try_sel;
1669 unsigned long flags;
1670
1671 if (sel->pad == FIMC_SD_PAD_SOURCE)
1672 return -EINVAL;
1673
1674 mutex_lock(&fimc->lock);
1675 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
1676
1677 switch (sel->target) {
1678 case V4L2_SEL_TGT_CROP:
1679 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1680 break;
1681 case V4L2_SEL_TGT_COMPOSE:
1682 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1683 f = &ctx->d_frame;
1684 break;
1685 default:
1686 mutex_unlock(&fimc->lock);
1687 return -EINVAL;
1688 }
1689
1690 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1691 *try_sel = sel->r;
1692 } else {
1693 spin_lock_irqsave(&fimc->slock, flags);
1694 set_frame_crop(f, r->left, r->top, r->width, r->height);
1695 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1696 if (sel->target == V4L2_SEL_TGT_COMPOSE)
1697 ctx->state |= FIMC_COMPOSE;
1698 spin_unlock_irqrestore(&fimc->slock, flags);
1699 }
1700
1701 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
1702 r->width, r->height);
1703
1704 mutex_unlock(&fimc->lock);
1705 return 0;
1706}
1707
1708static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1709 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1710 .get_selection = fimc_subdev_get_selection,
1711 .set_selection = fimc_subdev_set_selection,
1712 .get_fmt = fimc_subdev_get_fmt,
1713 .set_fmt = fimc_subdev_set_fmt,
1714};
1715
1716static struct v4l2_subdev_ops fimc_subdev_ops = {
1717 .pad = &fimc_subdev_pad_ops,
1718};
1719
1720
1721static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1722{
1723 struct v4l2_format fmt = {
1724 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1725 .fmt.pix_mp = {
1726 .width = 640,
1727 .height = 480,
1728 .pixelformat = V4L2_PIX_FMT_YUYV,
1729 .field = V4L2_FIELD_NONE,
1730 .colorspace = V4L2_COLORSPACE_JPEG,
1731 },
1732 };
1733
1734 return __fimc_capture_set_format(fimc, &fmt);
1735}
1736
1737
1738static int fimc_register_capture_device(struct fimc_dev *fimc,
1739 struct v4l2_device *v4l2_dev)
1740{
1741 struct video_device *vfd = &fimc->vid_cap.vfd;
1742 struct vb2_queue *q = &fimc->vid_cap.vbq;
1743 struct fimc_ctx *ctx;
1744 struct fimc_vid_cap *vid_cap;
1745 int ret = -ENOMEM;
1746
1747 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1748 if (!ctx)
1749 return -ENOMEM;
1750
1751 ctx->fimc_dev = fimc;
1752 ctx->in_path = FIMC_IO_CAMERA;
1753 ctx->out_path = FIMC_IO_DMA;
1754 ctx->state = FIMC_CTX_CAP;
1755 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1756 ctx->d_frame.fmt = ctx->s_frame.fmt;
1757
1758 memset(vfd, 0, sizeof(*vfd));
1759 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1760
1761 vfd->fops = &fimc_capture_fops;
1762 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1763 vfd->v4l2_dev = v4l2_dev;
1764 vfd->minor = -1;
1765 vfd->release = video_device_release_empty;
1766 vfd->queue = q;
1767 vfd->lock = &fimc->lock;
1768
1769 video_set_drvdata(vfd, fimc);
1770 vid_cap = &fimc->vid_cap;
1771 vid_cap->active_buf_cnt = 0;
1772 vid_cap->reqbufs_count = 0;
1773 vid_cap->ctx = ctx;
1774
1775 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1776 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1777
1778 memset(q, 0, sizeof(*q));
1779 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1780 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1781 q->drv_priv = ctx;
1782 q->ops = &fimc_capture_qops;
1783 q->mem_ops = &vb2_dma_contig_memops;
1784 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1785 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1786 q->lock = &fimc->lock;
1787
1788 ret = vb2_queue_init(q);
1789 if (ret)
1790 goto err_ent;
1791
1792 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1793 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
1794 if (ret)
1795 goto err_ent;
1796
1797
1798
1799
1800 v4l2_disable_ioctl_locking(vfd, VIDIOC_TRY_FMT);
1801 v4l2_disable_ioctl_locking(vfd, VIDIOC_S_FMT);
1802
1803 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1804 if (ret)
1805 goto err_vd;
1806
1807 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1808 vfd->name, video_device_node_name(vfd));
1809
1810 vfd->ctrl_handler = &ctx->ctrls.handler;
1811 return 0;
1812
1813err_vd:
1814 media_entity_cleanup(&vfd->entity);
1815err_ent:
1816 kfree(ctx);
1817 return ret;
1818}
1819
1820static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1821{
1822 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1823 int ret;
1824
1825 if (fimc == NULL)
1826 return -ENXIO;
1827
1828 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1829 if (ret)
1830 return ret;
1831
1832 fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
1833
1834 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1835 if (ret) {
1836 fimc_unregister_m2m_device(fimc);
1837 fimc->pipeline_ops = NULL;
1838 }
1839
1840 return ret;
1841}
1842
1843static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1844{
1845 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1846
1847 if (fimc == NULL)
1848 return;
1849
1850 fimc_unregister_m2m_device(fimc);
1851
1852 if (video_is_registered(&fimc->vid_cap.vfd)) {
1853 video_unregister_device(&fimc->vid_cap.vfd);
1854 media_entity_cleanup(&fimc->vid_cap.vfd.entity);
1855 fimc->pipeline_ops = NULL;
1856 }
1857 kfree(fimc->vid_cap.ctx);
1858 fimc->vid_cap.ctx = NULL;
1859}
1860
1861static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1862 .registered = fimc_capture_subdev_registered,
1863 .unregistered = fimc_capture_subdev_unregistered,
1864};
1865
1866int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1867{
1868 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1869 int ret;
1870
1871 v4l2_subdev_init(sd, &fimc_subdev_ops);
1872 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1873 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
1874
1875 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1876 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
1877 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1878 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1879 fimc->vid_cap.sd_pads, 0);
1880 if (ret)
1881 return ret;
1882
1883 sd->entity.ops = &fimc_sd_media_ops;
1884 sd->internal_ops = &fimc_capture_sd_internal_ops;
1885 v4l2_set_subdevdata(sd, fimc);
1886 return 0;
1887}
1888
1889void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1890{
1891 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1892
1893 v4l2_device_unregister_subdev(sd);
1894 media_entity_cleanup(&sd->entity);
1895 v4l2_set_subdevdata(sd, NULL);
1896}
1897