b36809d2f9fc54d4fd5922a6bf7ec0af294191ef
[deliverable/linux.git] / drivers / media / video / s5p-fimc / fimc-capture.c
1 /*
2 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
3 *
4 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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 "fimc-mdevice.h"
31 #include "fimc-core.h"
32 #include "fimc-reg.h"
33
34 static int fimc_init_capture(struct fimc_dev *fimc)
35 {
36 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
37 struct fimc_pipeline *p = &fimc->pipeline;
38 struct fimc_sensor_info *sensor;
39 unsigned long flags;
40 int ret = 0;
41
42 if (p->subdevs[IDX_SENSOR] == NULL || ctx == NULL)
43 return -ENXIO;
44 if (ctx->s_frame.fmt == NULL)
45 return -EINVAL;
46
47 sensor = v4l2_get_subdev_hostdata(p->subdevs[IDX_SENSOR]);
48
49 spin_lock_irqsave(&fimc->slock, flags);
50 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
51 fimc_set_yuv_order(ctx);
52
53 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
54 fimc_hw_set_camera_type(fimc, sensor->pdata);
55 fimc_hw_set_camera_source(fimc, sensor->pdata);
56 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
57
58 ret = fimc_set_scaler_info(ctx);
59 if (!ret) {
60 fimc_hw_set_input_path(ctx);
61 fimc_hw_set_prescaler(ctx);
62 fimc_hw_set_mainscaler(ctx);
63 fimc_hw_set_target_format(ctx);
64 fimc_hw_set_rotation(ctx);
65 fimc_hw_set_effect(ctx, false);
66 fimc_hw_set_output_path(ctx);
67 fimc_hw_set_out_dma(ctx);
68 if (fimc->variant->has_alpha)
69 fimc_hw_set_rgb_alpha(ctx);
70 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
71 }
72 spin_unlock_irqrestore(&fimc->slock, flags);
73 return ret;
74 }
75
76 static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
77 {
78 struct fimc_vid_cap *cap = &fimc->vid_cap;
79 struct fimc_vid_buffer *buf;
80 unsigned long flags;
81 bool streaming;
82
83 spin_lock_irqsave(&fimc->slock, flags);
84 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
85
86 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
87 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
88 if (suspend)
89 fimc->state |= (1 << ST_CAPT_SUSPENDED);
90 else
91 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
92
93 /* Release unused buffers */
94 while (!suspend && !list_empty(&cap->pending_buf_q)) {
95 buf = fimc_pending_queue_pop(cap);
96 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
97 }
98 /* If suspending put unused buffers onto pending queue */
99 while (!list_empty(&cap->active_buf_q)) {
100 buf = fimc_active_queue_pop(cap);
101 if (suspend)
102 fimc_pending_queue_add(cap, buf);
103 else
104 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
105 }
106
107 fimc_hw_reset(fimc);
108 cap->buf_index = 0;
109
110 spin_unlock_irqrestore(&fimc->slock, flags);
111
112 if (streaming)
113 return fimc_pipeline_s_stream(&fimc->pipeline, 0);
114 else
115 return 0;
116 }
117
118 static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
119 {
120 unsigned long flags;
121
122 if (!fimc_capture_active(fimc))
123 return 0;
124
125 spin_lock_irqsave(&fimc->slock, flags);
126 set_bit(ST_CAPT_SHUT, &fimc->state);
127 fimc_deactivate_capture(fimc);
128 spin_unlock_irqrestore(&fimc->slock, flags);
129
130 wait_event_timeout(fimc->irq_queue,
131 !test_bit(ST_CAPT_SHUT, &fimc->state),
132 (2*HZ/10)); /* 200 ms */
133
134 return fimc_capture_state_cleanup(fimc, suspend);
135 }
136
137 /**
138 * fimc_capture_config_update - apply the camera interface configuration
139 *
140 * To be called from within the interrupt handler with fimc.slock
141 * spinlock held. It updates the camera pixel crop, rotation and
142 * image flip in H/W.
143 */
144 static int fimc_capture_config_update(struct fimc_ctx *ctx)
145 {
146 struct fimc_dev *fimc = ctx->fimc_dev;
147 int ret;
148
149 if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
150 return 0;
151
152 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
153
154 ret = fimc_set_scaler_info(ctx);
155 if (ret)
156 return ret;
157
158 fimc_hw_set_prescaler(ctx);
159 fimc_hw_set_mainscaler(ctx);
160 fimc_hw_set_target_format(ctx);
161 fimc_hw_set_rotation(ctx);
162 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
163 fimc_hw_set_out_dma(ctx);
164 if (fimc->variant->has_alpha)
165 fimc_hw_set_rgb_alpha(ctx);
166
167 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
168 return ret;
169 }
170
171 void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
172 {
173 struct fimc_vid_cap *cap = &fimc->vid_cap;
174 struct fimc_vid_buffer *v_buf;
175 struct timeval *tv;
176 struct timespec ts;
177
178 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
179 wake_up(&fimc->irq_queue);
180 goto done;
181 }
182
183 if (!list_empty(&cap->active_buf_q) &&
184 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
185 ktime_get_real_ts(&ts);
186
187 v_buf = fimc_active_queue_pop(cap);
188
189 tv = &v_buf->vb.v4l2_buf.timestamp;
190 tv->tv_sec = ts.tv_sec;
191 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
192 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
193
194 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
195 }
196
197 if (!list_empty(&cap->pending_buf_q)) {
198
199 v_buf = fimc_pending_queue_pop(cap);
200 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
201 v_buf->index = cap->buf_index;
202
203 /* Move the buffer to the capture active queue */
204 fimc_active_queue_add(cap, v_buf);
205
206 dbg("next frame: %d, done frame: %d",
207 fimc_hw_get_frame_index(fimc), v_buf->index);
208
209 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
210 cap->buf_index = 0;
211 }
212
213 if (cap->active_buf_cnt == 0) {
214 if (deq_buf)
215 clear_bit(ST_CAPT_RUN, &fimc->state);
216
217 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
218 cap->buf_index = 0;
219 } else {
220 set_bit(ST_CAPT_RUN, &fimc->state);
221 }
222
223 fimc_capture_config_update(cap->ctx);
224 done:
225 if (cap->active_buf_cnt == 1) {
226 fimc_deactivate_capture(fimc);
227 clear_bit(ST_CAPT_STREAM, &fimc->state);
228 }
229
230 dbg("frame: %d, active_buf_cnt: %d",
231 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
232 }
233
234
235 static int start_streaming(struct vb2_queue *q, unsigned int count)
236 {
237 struct fimc_ctx *ctx = q->drv_priv;
238 struct fimc_dev *fimc = ctx->fimc_dev;
239 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
240 int min_bufs;
241 int ret;
242
243 vid_cap->frame_count = 0;
244
245 ret = fimc_init_capture(fimc);
246 if (ret)
247 goto error;
248
249 set_bit(ST_CAPT_PEND, &fimc->state);
250
251 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
252
253 if (vid_cap->active_buf_cnt >= min_bufs &&
254 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
255 fimc_activate_capture(ctx);
256
257 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
258 fimc_pipeline_s_stream(&fimc->pipeline, 1);
259 }
260
261 return 0;
262 error:
263 fimc_capture_state_cleanup(fimc, false);
264 return ret;
265 }
266
267 static int stop_streaming(struct vb2_queue *q)
268 {
269 struct fimc_ctx *ctx = q->drv_priv;
270 struct fimc_dev *fimc = ctx->fimc_dev;
271
272 if (!fimc_capture_active(fimc))
273 return -EINVAL;
274
275 return fimc_stop_capture(fimc, false);
276 }
277
278 int fimc_capture_suspend(struct fimc_dev *fimc)
279 {
280 bool suspend = fimc_capture_busy(fimc);
281
282 int ret = fimc_stop_capture(fimc, suspend);
283 if (ret)
284 return ret;
285 return fimc_pipeline_shutdown(&fimc->pipeline);
286 }
287
288 static void buffer_queue(struct vb2_buffer *vb);
289
290 int fimc_capture_resume(struct fimc_dev *fimc)
291 {
292 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
293 struct fimc_vid_buffer *buf;
294 int i;
295
296 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
297 return 0;
298
299 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
300 vid_cap->buf_index = 0;
301 fimc_pipeline_initialize(&fimc->pipeline, &vid_cap->vfd->entity,
302 false);
303 fimc_init_capture(fimc);
304
305 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
306
307 for (i = 0; i < vid_cap->reqbufs_count; i++) {
308 if (list_empty(&vid_cap->pending_buf_q))
309 break;
310 buf = fimc_pending_queue_pop(vid_cap);
311 buffer_queue(&buf->vb);
312 }
313 return 0;
314
315 }
316
317 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
318 unsigned int *num_buffers, unsigned int *num_planes,
319 unsigned int sizes[], void *allocators[])
320 {
321 const struct v4l2_pix_format_mplane *pixm = NULL;
322 struct fimc_ctx *ctx = vq->drv_priv;
323 struct fimc_frame *frame = &ctx->d_frame;
324 struct fimc_fmt *fmt = frame->fmt;
325 unsigned long wh;
326 int i;
327
328 if (pfmt) {
329 pixm = &pfmt->fmt.pix_mp;
330 fmt = fimc_find_format(&pixm->pixelformat, NULL,
331 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
332 wh = pixm->width * pixm->height;
333 } else {
334 wh = frame->f_width * frame->f_height;
335 }
336
337 if (fmt == NULL)
338 return -EINVAL;
339
340 *num_planes = fmt->memplanes;
341
342 for (i = 0; i < fmt->memplanes; i++) {
343 unsigned int size = (wh * fmt->depth[i]) / 8;
344 if (pixm)
345 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
346 else
347 sizes[i] = size;
348 allocators[i] = ctx->fimc_dev->alloc_ctx;
349 }
350
351 return 0;
352 }
353
354 static int buffer_prepare(struct vb2_buffer *vb)
355 {
356 struct vb2_queue *vq = vb->vb2_queue;
357 struct fimc_ctx *ctx = vq->drv_priv;
358 int i;
359
360 if (ctx->d_frame.fmt == NULL)
361 return -EINVAL;
362
363 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
364 unsigned long size = ctx->d_frame.payload[i];
365
366 if (vb2_plane_size(vb, i) < size) {
367 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
368 "User buffer too small (%ld < %ld)\n",
369 vb2_plane_size(vb, i), size);
370 return -EINVAL;
371 }
372 vb2_set_plane_payload(vb, i, size);
373 }
374
375 return 0;
376 }
377
378 static void buffer_queue(struct vb2_buffer *vb)
379 {
380 struct fimc_vid_buffer *buf
381 = container_of(vb, struct fimc_vid_buffer, vb);
382 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
383 struct fimc_dev *fimc = ctx->fimc_dev;
384 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
385 unsigned long flags;
386 int min_bufs;
387
388 spin_lock_irqsave(&fimc->slock, flags);
389 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
390
391 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
392 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
393 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
394 /* Setup the buffer directly for processing. */
395 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
396 vid_cap->buf_index;
397
398 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
399 buf->index = vid_cap->buf_index;
400 fimc_active_queue_add(vid_cap, buf);
401
402 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
403 vid_cap->buf_index = 0;
404 } else {
405 fimc_pending_queue_add(vid_cap, buf);
406 }
407
408 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
409
410
411 if (vb2_is_streaming(&vid_cap->vbq) &&
412 vid_cap->active_buf_cnt >= min_bufs &&
413 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
414 fimc_activate_capture(ctx);
415 spin_unlock_irqrestore(&fimc->slock, flags);
416
417 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
418 fimc_pipeline_s_stream(&fimc->pipeline, 1);
419 return;
420 }
421 spin_unlock_irqrestore(&fimc->slock, flags);
422 }
423
424 static void fimc_lock(struct vb2_queue *vq)
425 {
426 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
427 mutex_lock(&ctx->fimc_dev->lock);
428 }
429
430 static void fimc_unlock(struct vb2_queue *vq)
431 {
432 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
433 mutex_unlock(&ctx->fimc_dev->lock);
434 }
435
436 static struct vb2_ops fimc_capture_qops = {
437 .queue_setup = queue_setup,
438 .buf_prepare = buffer_prepare,
439 .buf_queue = buffer_queue,
440 .wait_prepare = fimc_unlock,
441 .wait_finish = fimc_lock,
442 .start_streaming = start_streaming,
443 .stop_streaming = stop_streaming,
444 };
445
446 /**
447 * fimc_capture_ctrls_create - initialize the control handler
448 * Initialize the capture video node control handler and fill it
449 * with the FIMC controls. Inherit any sensor's controls if the
450 * 'user_subdev_api' flag is false (default behaviour).
451 * This function need to be called with the graph mutex held.
452 */
453 int fimc_capture_ctrls_create(struct fimc_dev *fimc)
454 {
455 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
456 int ret;
457
458 if (WARN_ON(vid_cap->ctx == NULL))
459 return -ENXIO;
460 if (vid_cap->ctx->ctrls_rdy)
461 return 0;
462
463 ret = fimc_ctrls_create(vid_cap->ctx);
464 if (ret || vid_cap->user_subdev_api)
465 return ret;
466
467 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
468 fimc->pipeline.subdevs[IDX_SENSOR]->ctrl_handler);
469 }
470
471 static int fimc_capture_set_default_format(struct fimc_dev *fimc);
472
473 static int fimc_capture_open(struct file *file)
474 {
475 struct fimc_dev *fimc = video_drvdata(file);
476 int ret = v4l2_fh_open(file);
477
478 if (ret)
479 return ret;
480
481 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
482
483 /* Return if the corresponding video mem2mem node is already opened. */
484 if (fimc_m2m_active(fimc))
485 return -EBUSY;
486
487 set_bit(ST_CAPT_BUSY, &fimc->state);
488 pm_runtime_get_sync(&fimc->pdev->dev);
489
490 if (++fimc->vid_cap.refcnt == 1) {
491 ret = fimc_pipeline_initialize(&fimc->pipeline,
492 &fimc->vid_cap.vfd->entity, true);
493 if (ret < 0) {
494 dev_err(&fimc->pdev->dev,
495 "Video pipeline initialization failed\n");
496 pm_runtime_put_sync(&fimc->pdev->dev);
497 fimc->vid_cap.refcnt--;
498 v4l2_fh_release(file);
499 clear_bit(ST_CAPT_BUSY, &fimc->state);
500 return ret;
501 }
502 ret = fimc_capture_ctrls_create(fimc);
503
504 if (!ret && !fimc->vid_cap.user_subdev_api)
505 ret = fimc_capture_set_default_format(fimc);
506 }
507 return ret;
508 }
509
510 static int fimc_capture_close(struct file *file)
511 {
512 struct fimc_dev *fimc = video_drvdata(file);
513
514 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
515
516 if (--fimc->vid_cap.refcnt == 0) {
517 clear_bit(ST_CAPT_BUSY, &fimc->state);
518 fimc_stop_capture(fimc, false);
519 fimc_pipeline_shutdown(&fimc->pipeline);
520 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
521 }
522
523 pm_runtime_put(&fimc->pdev->dev);
524
525 if (fimc->vid_cap.refcnt == 0) {
526 vb2_queue_release(&fimc->vid_cap.vbq);
527 fimc_ctrls_delete(fimc->vid_cap.ctx);
528 }
529 return v4l2_fh_release(file);
530 }
531
532 static unsigned int fimc_capture_poll(struct file *file,
533 struct poll_table_struct *wait)
534 {
535 struct fimc_dev *fimc = video_drvdata(file);
536
537 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
538 }
539
540 static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
541 {
542 struct fimc_dev *fimc = video_drvdata(file);
543
544 return vb2_mmap(&fimc->vid_cap.vbq, vma);
545 }
546
547 static const struct v4l2_file_operations fimc_capture_fops = {
548 .owner = THIS_MODULE,
549 .open = fimc_capture_open,
550 .release = fimc_capture_close,
551 .poll = fimc_capture_poll,
552 .unlocked_ioctl = video_ioctl2,
553 .mmap = fimc_capture_mmap,
554 };
555
556 /*
557 * Format and crop negotiation helpers
558 */
559
560 static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
561 u32 *width, u32 *height,
562 u32 *code, u32 *fourcc, int pad)
563 {
564 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
565 struct fimc_dev *fimc = ctx->fimc_dev;
566 struct samsung_fimc_variant *var = fimc->variant;
567 struct fimc_pix_limit *pl = var->pix_limit;
568 struct fimc_frame *dst = &ctx->d_frame;
569 u32 depth, min_w, max_w, min_h, align_h = 3;
570 u32 mask = FMT_FLAGS_CAM;
571 struct fimc_fmt *ffmt;
572
573 /* Color conversion from/to JPEG is not supported */
574 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
575 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
576 *code = V4L2_MBUS_FMT_JPEG_1X8;
577
578 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
579 mask |= FMT_FLAGS_M2M;
580
581 ffmt = fimc_find_format(fourcc, code, mask, 0);
582 if (WARN_ON(!ffmt))
583 return NULL;
584 if (code)
585 *code = ffmt->mbus_code;
586 if (fourcc)
587 *fourcc = ffmt->fourcc;
588
589 if (pad == FIMC_SD_PAD_SINK) {
590 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
591 pl->scaler_dis_w : pl->scaler_en_w;
592 /* Apply the camera input interface pixel constraints */
593 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
594 height, max_t(u32, *height, 32),
595 FIMC_CAMIF_MAX_HEIGHT,
596 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
597 0);
598 return ffmt;
599 }
600 /* Can't scale or crop in transparent (JPEG) transfer mode */
601 if (fimc_fmt_is_jpeg(ffmt->color)) {
602 *width = ctx->s_frame.f_width;
603 *height = ctx->s_frame.f_height;
604 return ffmt;
605 }
606 /* Apply the scaler and the output DMA constraints */
607 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
608 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
609 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
610 if (var->min_vsize_align == 1 && !rotation)
611 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
612
613 depth = fimc_get_format_depth(ffmt);
614 v4l_bound_align_image(width, min_w, max_w,
615 ffs(var->min_out_pixsize) - 1,
616 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
617 align_h,
618 64/(ALIGN(depth, 8)));
619
620 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
621 pad, code ? *code : 0, *width, *height,
622 dst->f_width, dst->f_height);
623
624 return ffmt;
625 }
626
627 static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
628 int pad)
629 {
630 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
631 struct fimc_dev *fimc = ctx->fimc_dev;
632 struct samsung_fimc_variant *var = fimc->variant;
633 struct fimc_pix_limit *pl = var->pix_limit;
634 struct fimc_frame *sink = &ctx->s_frame;
635 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
636 u32 align_sz = 0, align_h = 4;
637 u32 max_sc_h, max_sc_v;
638
639 /* In JPEG transparent transfer mode cropping is not supported */
640 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
641 r->width = sink->f_width;
642 r->height = sink->f_height;
643 r->left = r->top = 0;
644 return;
645 }
646 if (pad == FIMC_SD_PAD_SOURCE) {
647 if (ctx->rotation != 90 && ctx->rotation != 270)
648 align_h = 1;
649 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
650 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
651 min_sz = var->min_out_pixsize;
652 } else {
653 u32 depth = fimc_get_format_depth(sink->fmt);
654 align_sz = 64/ALIGN(depth, 8);
655 min_sz = var->min_inp_pixsize;
656 min_w = min_h = min_sz;
657 max_sc_h = max_sc_v = 1;
658 }
659 /*
660 * For the crop rectangle at source pad the following constraints
661 * must be met:
662 * - it must fit in the sink pad format rectangle (f_width/f_height);
663 * - maximum downscaling ratio is 64;
664 * - maximum crop size depends if the rotator is used or not;
665 * - the sink pad format width/height must be 4 multiple of the
666 * prescaler ratios determined by sink pad size and source pad crop,
667 * the prescaler ratio is returned by fimc_get_scaler_factor().
668 */
669 max_w = min_t(u32,
670 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
671 rotate ? sink->f_height : sink->f_width);
672 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
673 if (pad == FIMC_SD_PAD_SOURCE) {
674 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
675 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
676 if (rotate) {
677 swap(max_sc_h, max_sc_v);
678 swap(min_w, min_h);
679 }
680 }
681 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
682 &r->height, min_h, max_h, align_h,
683 align_sz);
684 /* Adjust left/top if cropping rectangle is out of bounds */
685 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
686 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
687 r->left = round_down(r->left, var->hor_offs_align);
688
689 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
690 pad, r->left, r->top, r->width, r->height,
691 sink->f_width, sink->f_height);
692 }
693
694 /*
695 * The video node ioctl operations
696 */
697 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
698 struct v4l2_capability *cap)
699 {
700 struct fimc_dev *fimc = video_drvdata(file);
701
702 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
703 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
704 cap->bus_info[0] = 0;
705 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
706
707 return 0;
708 }
709
710 static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
711 struct v4l2_fmtdesc *f)
712 {
713 struct fimc_fmt *fmt;
714
715 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
716 f->index);
717 if (!fmt)
718 return -EINVAL;
719 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
720 f->pixelformat = fmt->fourcc;
721 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
722 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
723 return 0;
724 }
725
726 /**
727 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
728 * elements
729 * @ctx: FIMC capture context
730 * @tfmt: media bus format to try/set on subdevs
731 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
732 * @set: true to set format on subdevs, false to try only
733 */
734 static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
735 struct v4l2_mbus_framefmt *tfmt,
736 struct fimc_fmt **fmt_id,
737 bool set)
738 {
739 struct fimc_dev *fimc = ctx->fimc_dev;
740 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
741 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
742 struct v4l2_subdev_format sfmt;
743 struct v4l2_mbus_framefmt *mf = &sfmt.format;
744 struct fimc_fmt *ffmt = NULL;
745 int ret, i = 0;
746
747 if (WARN_ON(!sd || !tfmt))
748 return -EINVAL;
749
750 memset(&sfmt, 0, sizeof(sfmt));
751 sfmt.format = *tfmt;
752
753 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
754 while (1) {
755 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
756 FMT_FLAGS_CAM, i++);
757 if (ffmt == NULL) {
758 /*
759 * Notify user-space if common pixel code for
760 * host and sensor does not exist.
761 */
762 return -EINVAL;
763 }
764 mf->code = tfmt->code = ffmt->mbus_code;
765
766 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
767 if (ret)
768 return ret;
769 if (mf->code != tfmt->code) {
770 mf->code = 0;
771 continue;
772 }
773 if (mf->width != tfmt->width || mf->height != tfmt->height) {
774 u32 fcc = ffmt->fourcc;
775 tfmt->width = mf->width;
776 tfmt->height = mf->height;
777 ffmt = fimc_capture_try_format(ctx,
778 &tfmt->width, &tfmt->height,
779 NULL, &fcc, FIMC_SD_PAD_SOURCE);
780 if (ffmt && ffmt->mbus_code)
781 mf->code = ffmt->mbus_code;
782 if (mf->width != tfmt->width ||
783 mf->height != tfmt->height)
784 continue;
785 tfmt->code = mf->code;
786 }
787 if (csis)
788 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
789
790 if (mf->code == tfmt->code &&
791 mf->width == tfmt->width && mf->height == tfmt->height)
792 break;
793 }
794
795 if (fmt_id && ffmt)
796 *fmt_id = ffmt;
797 *tfmt = *mf;
798
799 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
800 return 0;
801 }
802
803 static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
804 struct v4l2_format *f)
805 {
806 struct fimc_dev *fimc = video_drvdata(file);
807 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
808
809 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
810 return -EINVAL;
811
812 return fimc_fill_format(&ctx->d_frame, f);
813 }
814
815 static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
816 struct v4l2_format *f)
817 {
818 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
819 struct fimc_dev *fimc = video_drvdata(file);
820 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
821 struct v4l2_mbus_framefmt mf;
822 struct fimc_fmt *ffmt = NULL;
823
824 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
825 return -EINVAL;
826
827 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
828 fimc_capture_try_format(ctx, &pix->width, &pix->height,
829 NULL, &pix->pixelformat,
830 FIMC_SD_PAD_SINK);
831 ctx->s_frame.f_width = pix->width;
832 ctx->s_frame.f_height = pix->height;
833 }
834 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
835 NULL, &pix->pixelformat,
836 FIMC_SD_PAD_SOURCE);
837 if (!ffmt)
838 return -EINVAL;
839
840 if (!fimc->vid_cap.user_subdev_api) {
841 mf.width = pix->width;
842 mf.height = pix->height;
843 mf.code = ffmt->mbus_code;
844 fimc_md_graph_lock(fimc);
845 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
846 fimc_md_graph_unlock(fimc);
847
848 pix->width = mf.width;
849 pix->height = mf.height;
850 if (ffmt)
851 pix->pixelformat = ffmt->fourcc;
852 }
853
854 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
855 return 0;
856 }
857
858 static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
859 {
860 ctx->scaler.enabled = !jpeg;
861 fimc_ctrls_activate(ctx, !jpeg);
862
863 if (jpeg)
864 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
865 else
866 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
867 }
868
869 static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
870 {
871 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
872 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
873 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
874 struct fimc_frame *ff = &ctx->d_frame;
875 struct fimc_fmt *s_fmt = NULL;
876 int ret, i;
877
878 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
879 return -EINVAL;
880 if (vb2_is_busy(&fimc->vid_cap.vbq))
881 return -EBUSY;
882
883 /* Pre-configure format at camera interface input, for JPEG only */
884 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
885 fimc_capture_try_format(ctx, &pix->width, &pix->height,
886 NULL, &pix->pixelformat,
887 FIMC_SD_PAD_SINK);
888 ctx->s_frame.f_width = pix->width;
889 ctx->s_frame.f_height = pix->height;
890 }
891 /* Try the format at the scaler and the DMA output */
892 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
893 NULL, &pix->pixelformat,
894 FIMC_SD_PAD_SOURCE);
895 if (!ff->fmt)
896 return -EINVAL;
897
898 /* Update RGB Alpha control state and value range */
899 fimc_alpha_ctrl_update(ctx);
900
901 /* Try to match format at the host and the sensor */
902 if (!fimc->vid_cap.user_subdev_api) {
903 mf->code = ff->fmt->mbus_code;
904 mf->width = pix->width;
905 mf->height = pix->height;
906
907 fimc_md_graph_lock(fimc);
908 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
909 fimc_md_graph_unlock(fimc);
910 if (ret)
911 return ret;
912 pix->width = mf->width;
913 pix->height = mf->height;
914 }
915 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
916 for (i = 0; i < ff->fmt->colplanes; i++)
917 ff->payload[i] =
918 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
919
920 set_frame_bounds(ff, pix->width, pix->height);
921 /* Reset the composition rectangle if not yet configured */
922 if (!(ctx->state & FIMC_DST_CROP))
923 set_frame_crop(ff, 0, 0, pix->width, pix->height);
924
925 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
926
927 /* Reset cropping and set format at the camera interface input */
928 if (!fimc->vid_cap.user_subdev_api) {
929 ctx->s_frame.fmt = s_fmt;
930 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
931 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
932 }
933
934 return ret;
935 }
936
937 static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
938 struct v4l2_format *f)
939 {
940 struct fimc_dev *fimc = video_drvdata(file);
941
942 return fimc_capture_set_format(fimc, f);
943 }
944
945 static int fimc_cap_enum_input(struct file *file, void *priv,
946 struct v4l2_input *i)
947 {
948 struct fimc_dev *fimc = video_drvdata(file);
949 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
950
951 if (i->index != 0)
952 return -EINVAL;
953
954 i->type = V4L2_INPUT_TYPE_CAMERA;
955 if (sd)
956 strlcpy(i->name, sd->name, sizeof(i->name));
957 return 0;
958 }
959
960 static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
961 {
962 return i == 0 ? i : -EINVAL;
963 }
964
965 static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
966 {
967 *i = 0;
968 return 0;
969 }
970
971 /**
972 * fimc_pipeline_validate - check for formats inconsistencies
973 * between source and sink pad of each link
974 *
975 * Return 0 if all formats match or -EPIPE otherwise.
976 */
977 static int fimc_pipeline_validate(struct fimc_dev *fimc)
978 {
979 struct v4l2_subdev_format sink_fmt, src_fmt;
980 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
981 struct v4l2_subdev *sd;
982 struct media_pad *pad;
983 int ret;
984
985 /* Start with the video capture node pad */
986 pad = media_entity_remote_source(&vid_cap->vd_pad);
987 if (pad == NULL)
988 return -EPIPE;
989 /* FIMC.{N} subdevice */
990 sd = media_entity_to_v4l2_subdev(pad->entity);
991
992 while (1) {
993 /* Retrieve format at the sink pad */
994 pad = &sd->entity.pads[0];
995 if (!(pad->flags & MEDIA_PAD_FL_SINK))
996 break;
997 /* Don't call FIMC subdev operation to avoid nested locking */
998 if (sd == &fimc->vid_cap.subdev) {
999 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
1000 sink_fmt.format.width = ff->f_width;
1001 sink_fmt.format.height = ff->f_height;
1002 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1003 } else {
1004 sink_fmt.pad = pad->index;
1005 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1006 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1007 if (ret < 0 && ret != -ENOIOCTLCMD)
1008 return -EPIPE;
1009 }
1010 /* Retrieve format at the source pad */
1011 pad = media_entity_remote_source(pad);
1012 if (pad == NULL ||
1013 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1014 break;
1015
1016 sd = media_entity_to_v4l2_subdev(pad->entity);
1017 src_fmt.pad = pad->index;
1018 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1019 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1020 if (ret < 0 && ret != -ENOIOCTLCMD)
1021 return -EPIPE;
1022
1023 if (src_fmt.format.width != sink_fmt.format.width ||
1024 src_fmt.format.height != sink_fmt.format.height ||
1025 src_fmt.format.code != sink_fmt.format.code)
1026 return -EPIPE;
1027 }
1028 return 0;
1029 }
1030
1031 static int fimc_cap_streamon(struct file *file, void *priv,
1032 enum v4l2_buf_type type)
1033 {
1034 struct fimc_dev *fimc = video_drvdata(file);
1035 struct fimc_pipeline *p = &fimc->pipeline;
1036 int ret;
1037
1038 if (fimc_capture_active(fimc))
1039 return -EBUSY;
1040
1041 media_entity_pipeline_start(&p->subdevs[IDX_SENSOR]->entity,
1042 p->m_pipeline);
1043
1044 if (fimc->vid_cap.user_subdev_api) {
1045 ret = fimc_pipeline_validate(fimc);
1046 if (ret)
1047 return ret;
1048 }
1049 return vb2_streamon(&fimc->vid_cap.vbq, type);
1050 }
1051
1052 static int fimc_cap_streamoff(struct file *file, void *priv,
1053 enum v4l2_buf_type type)
1054 {
1055 struct fimc_dev *fimc = video_drvdata(file);
1056 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
1057 int ret;
1058
1059 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
1060 if (ret == 0)
1061 media_entity_pipeline_stop(&sd->entity);
1062 return ret;
1063 }
1064
1065 static int fimc_cap_reqbufs(struct file *file, void *priv,
1066 struct v4l2_requestbuffers *reqbufs)
1067 {
1068 struct fimc_dev *fimc = video_drvdata(file);
1069 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
1070
1071 if (!ret)
1072 fimc->vid_cap.reqbufs_count = reqbufs->count;
1073 return ret;
1074 }
1075
1076 static int fimc_cap_querybuf(struct file *file, void *priv,
1077 struct v4l2_buffer *buf)
1078 {
1079 struct fimc_dev *fimc = video_drvdata(file);
1080
1081 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
1082 }
1083
1084 static int fimc_cap_qbuf(struct file *file, void *priv,
1085 struct v4l2_buffer *buf)
1086 {
1087 struct fimc_dev *fimc = video_drvdata(file);
1088
1089 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
1090 }
1091
1092 static int fimc_cap_dqbuf(struct file *file, void *priv,
1093 struct v4l2_buffer *buf)
1094 {
1095 struct fimc_dev *fimc = video_drvdata(file);
1096
1097 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
1098 }
1099
1100 static int fimc_cap_create_bufs(struct file *file, void *priv,
1101 struct v4l2_create_buffers *create)
1102 {
1103 struct fimc_dev *fimc = video_drvdata(file);
1104
1105 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1106 }
1107
1108 static int fimc_cap_prepare_buf(struct file *file, void *priv,
1109 struct v4l2_buffer *b)
1110 {
1111 struct fimc_dev *fimc = video_drvdata(file);
1112
1113 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1114 }
1115
1116 static int fimc_cap_g_selection(struct file *file, void *fh,
1117 struct v4l2_selection *s)
1118 {
1119 struct fimc_dev *fimc = video_drvdata(file);
1120 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1121 struct fimc_frame *f = &ctx->s_frame;
1122
1123 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1124 return -EINVAL;
1125
1126 switch (s->target) {
1127 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1128 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1129 f = &ctx->d_frame;
1130 case V4L2_SEL_TGT_CROP_BOUNDS:
1131 case V4L2_SEL_TGT_CROP_DEFAULT:
1132 s->r.left = 0;
1133 s->r.top = 0;
1134 s->r.width = f->o_width;
1135 s->r.height = f->o_height;
1136 return 0;
1137
1138 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1139 f = &ctx->d_frame;
1140 case V4L2_SEL_TGT_CROP_ACTIVE:
1141 s->r.left = f->offs_h;
1142 s->r.top = f->offs_v;
1143 s->r.width = f->width;
1144 s->r.height = f->height;
1145 return 0;
1146 }
1147
1148 return -EINVAL;
1149 }
1150
1151 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1152 int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1153 {
1154 if (a->left < b->left || a->top < b->top)
1155 return 0;
1156 if (a->left + a->width > b->left + b->width)
1157 return 0;
1158 if (a->top + a->height > b->top + b->height)
1159 return 0;
1160
1161 return 1;
1162 }
1163
1164 static int fimc_cap_s_selection(struct file *file, void *fh,
1165 struct v4l2_selection *s)
1166 {
1167 struct fimc_dev *fimc = video_drvdata(file);
1168 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1169 struct v4l2_rect rect = s->r;
1170 struct fimc_frame *f;
1171 unsigned long flags;
1172 unsigned int pad;
1173
1174 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1175 return -EINVAL;
1176
1177 switch (s->target) {
1178 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1179 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1180 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1181 f = &ctx->d_frame;
1182 pad = FIMC_SD_PAD_SOURCE;
1183 break;
1184 case V4L2_SEL_TGT_CROP_BOUNDS:
1185 case V4L2_SEL_TGT_CROP_DEFAULT:
1186 case V4L2_SEL_TGT_CROP_ACTIVE:
1187 f = &ctx->s_frame;
1188 pad = FIMC_SD_PAD_SINK;
1189 break;
1190 default:
1191 return -EINVAL;
1192 }
1193
1194 fimc_capture_try_crop(ctx, &rect, pad);
1195
1196 if (s->flags & V4L2_SEL_FLAG_LE &&
1197 !enclosed_rectangle(&rect, &s->r))
1198 return -ERANGE;
1199
1200 if (s->flags & V4L2_SEL_FLAG_GE &&
1201 !enclosed_rectangle(&s->r, &rect))
1202 return -ERANGE;
1203
1204 s->r = rect;
1205 spin_lock_irqsave(&fimc->slock, flags);
1206 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1207 s->r.height);
1208 spin_unlock_irqrestore(&fimc->slock, flags);
1209
1210 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1211 return 0;
1212 }
1213
1214 static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1215 .vidioc_querycap = fimc_vidioc_querycap_capture,
1216
1217 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
1218 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
1219 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
1220 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
1221
1222 .vidioc_reqbufs = fimc_cap_reqbufs,
1223 .vidioc_querybuf = fimc_cap_querybuf,
1224
1225 .vidioc_qbuf = fimc_cap_qbuf,
1226 .vidioc_dqbuf = fimc_cap_dqbuf,
1227
1228 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1229 .vidioc_create_bufs = fimc_cap_create_bufs,
1230
1231 .vidioc_streamon = fimc_cap_streamon,
1232 .vidioc_streamoff = fimc_cap_streamoff,
1233
1234 .vidioc_g_selection = fimc_cap_g_selection,
1235 .vidioc_s_selection = fimc_cap_s_selection,
1236
1237 .vidioc_enum_input = fimc_cap_enum_input,
1238 .vidioc_s_input = fimc_cap_s_input,
1239 .vidioc_g_input = fimc_cap_g_input,
1240 };
1241
1242 /* Capture subdev media entity operations */
1243 static int fimc_link_setup(struct media_entity *entity,
1244 const struct media_pad *local,
1245 const struct media_pad *remote, u32 flags)
1246 {
1247 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1248 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1249
1250 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1251 return -EINVAL;
1252
1253 if (WARN_ON(fimc == NULL))
1254 return 0;
1255
1256 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1257 local->entity->name, remote->entity->name, flags,
1258 fimc->vid_cap.input);
1259
1260 if (flags & MEDIA_LNK_FL_ENABLED) {
1261 if (fimc->vid_cap.input != 0)
1262 return -EBUSY;
1263 fimc->vid_cap.input = sd->grp_id;
1264 return 0;
1265 }
1266
1267 fimc->vid_cap.input = 0;
1268 return 0;
1269 }
1270
1271 static const struct media_entity_operations fimc_sd_media_ops = {
1272 .link_setup = fimc_link_setup,
1273 };
1274
1275 /**
1276 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1277 * @sd: pointer to a subdev generating the notification
1278 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1279 * @arg: pointer to an u32 type integer that stores the frame payload value
1280 *
1281 * The End Of Frame notification sent by sensor subdev in its still capture
1282 * mode. If there is only a single VSYNC generated by the sensor at the
1283 * beginning of a frame transmission, FIMC does not issue the LastIrq
1284 * (end of frame) interrupt. And this notification is used to complete the
1285 * frame capture and returning a buffer to user-space. Subdev drivers should
1286 * call this notification from their last 'End of frame capture' interrupt.
1287 */
1288 void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1289 void *arg)
1290 {
1291 struct fimc_sensor_info *sensor;
1292 struct fimc_vid_buffer *buf;
1293 struct fimc_md *fmd;
1294 struct fimc_dev *fimc;
1295 unsigned long flags;
1296
1297 if (sd == NULL)
1298 return;
1299
1300 sensor = v4l2_get_subdev_hostdata(sd);
1301 fmd = entity_to_fimc_mdev(&sd->entity);
1302
1303 spin_lock_irqsave(&fmd->slock, flags);
1304 fimc = sensor ? sensor->host : NULL;
1305
1306 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1307 test_bit(ST_CAPT_PEND, &fimc->state)) {
1308 unsigned long irq_flags;
1309 spin_lock_irqsave(&fimc->slock, irq_flags);
1310 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1311 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1312 struct fimc_vid_buffer, list);
1313 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1314 }
1315 fimc_capture_irq_handler(fimc, 1);
1316 fimc_deactivate_capture(fimc);
1317 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1318 }
1319 spin_unlock_irqrestore(&fmd->slock, flags);
1320 }
1321
1322 static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1323 struct v4l2_subdev_fh *fh,
1324 struct v4l2_subdev_mbus_code_enum *code)
1325 {
1326 struct fimc_fmt *fmt;
1327
1328 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1329 if (!fmt)
1330 return -EINVAL;
1331 code->code = fmt->mbus_code;
1332 return 0;
1333 }
1334
1335 static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1336 struct v4l2_subdev_fh *fh,
1337 struct v4l2_subdev_format *fmt)
1338 {
1339 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1340 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1341 struct v4l2_mbus_framefmt *mf;
1342 struct fimc_frame *ff;
1343
1344 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1345 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1346 fmt->format = *mf;
1347 return 0;
1348 }
1349 mf = &fmt->format;
1350 mf->colorspace = V4L2_COLORSPACE_JPEG;
1351 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1352
1353 mutex_lock(&fimc->lock);
1354 /* The pixel code is same on both input and output pad */
1355 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1356 mf->code = ctx->s_frame.fmt->mbus_code;
1357 mf->width = ff->f_width;
1358 mf->height = ff->f_height;
1359 mutex_unlock(&fimc->lock);
1360
1361 return 0;
1362 }
1363
1364 static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1365 struct v4l2_subdev_fh *fh,
1366 struct v4l2_subdev_format *fmt)
1367 {
1368 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1369 struct v4l2_mbus_framefmt *mf = &fmt->format;
1370 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1371 struct fimc_frame *ff;
1372 struct fimc_fmt *ffmt;
1373
1374 dbg("pad%d: code: 0x%x, %dx%d",
1375 fmt->pad, mf->code, mf->width, mf->height);
1376
1377 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1378 vb2_is_busy(&fimc->vid_cap.vbq))
1379 return -EBUSY;
1380
1381 mutex_lock(&fimc->lock);
1382 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1383 &mf->code, NULL, fmt->pad);
1384 mutex_unlock(&fimc->lock);
1385 mf->colorspace = V4L2_COLORSPACE_JPEG;
1386
1387 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1388 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1389 *mf = fmt->format;
1390 return 0;
1391 }
1392 /* Update RGB Alpha control state and value range */
1393 fimc_alpha_ctrl_update(ctx);
1394
1395 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1396
1397 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1398 &ctx->s_frame : &ctx->d_frame;
1399
1400 mutex_lock(&fimc->lock);
1401 set_frame_bounds(ff, mf->width, mf->height);
1402 fimc->vid_cap.mf = *mf;
1403 ff->fmt = ffmt;
1404
1405 /* Reset the crop rectangle if required. */
1406 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1407 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1408
1409 if (fmt->pad == FIMC_SD_PAD_SINK)
1410 ctx->state &= ~FIMC_DST_CROP;
1411 mutex_unlock(&fimc->lock);
1412 return 0;
1413 }
1414
1415 static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1416 struct v4l2_subdev_fh *fh,
1417 struct v4l2_subdev_crop *crop)
1418 {
1419 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1420 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1421 struct v4l2_rect *r = &crop->rect;
1422 struct fimc_frame *ff;
1423
1424 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1425 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1426 return 0;
1427 }
1428 ff = crop->pad == FIMC_SD_PAD_SINK ?
1429 &ctx->s_frame : &ctx->d_frame;
1430
1431 mutex_lock(&fimc->lock);
1432 r->left = ff->offs_h;
1433 r->top = ff->offs_v;
1434 r->width = ff->width;
1435 r->height = ff->height;
1436 mutex_unlock(&fimc->lock);
1437
1438 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1439 ff, crop->pad, r->left, r->top, r->width, r->height,
1440 ff->f_width, ff->f_height);
1441
1442 return 0;
1443 }
1444
1445 static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1446 struct v4l2_subdev_fh *fh,
1447 struct v4l2_subdev_crop *crop)
1448 {
1449 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1450 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1451 struct v4l2_rect *r = &crop->rect;
1452 struct fimc_frame *ff;
1453 unsigned long flags;
1454
1455 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1456
1457 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1458 &ctx->d_frame : &ctx->s_frame;
1459
1460 mutex_lock(&fimc->lock);
1461 fimc_capture_try_crop(ctx, r, crop->pad);
1462
1463 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1464 mutex_unlock(&fimc->lock);
1465 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1466 return 0;
1467 }
1468 spin_lock_irqsave(&fimc->slock, flags);
1469 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1470 if (crop->pad == FIMC_SD_PAD_SOURCE)
1471 ctx->state |= FIMC_DST_CROP;
1472
1473 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1474 spin_unlock_irqrestore(&fimc->slock, flags);
1475
1476 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1477 r->width, r->height);
1478
1479 mutex_unlock(&fimc->lock);
1480 return 0;
1481 }
1482
1483 static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1484 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1485 .get_fmt = fimc_subdev_get_fmt,
1486 .set_fmt = fimc_subdev_set_fmt,
1487 .get_crop = fimc_subdev_get_crop,
1488 .set_crop = fimc_subdev_set_crop,
1489 };
1490
1491 static struct v4l2_subdev_ops fimc_subdev_ops = {
1492 .pad = &fimc_subdev_pad_ops,
1493 };
1494
1495 /* Set default format at the sensor and host interface */
1496 static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1497 {
1498 struct v4l2_format fmt = {
1499 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1500 .fmt.pix_mp = {
1501 .width = 640,
1502 .height = 480,
1503 .pixelformat = V4L2_PIX_FMT_YUYV,
1504 .field = V4L2_FIELD_NONE,
1505 .colorspace = V4L2_COLORSPACE_JPEG,
1506 },
1507 };
1508
1509 return fimc_capture_set_format(fimc, &fmt);
1510 }
1511
1512 /* fimc->lock must be already initialized */
1513 static int fimc_register_capture_device(struct fimc_dev *fimc,
1514 struct v4l2_device *v4l2_dev)
1515 {
1516 struct video_device *vfd;
1517 struct fimc_vid_cap *vid_cap;
1518 struct fimc_ctx *ctx;
1519 struct vb2_queue *q;
1520 int ret = -ENOMEM;
1521
1522 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1523 if (!ctx)
1524 return -ENOMEM;
1525
1526 ctx->fimc_dev = fimc;
1527 ctx->in_path = FIMC_IO_CAMERA;
1528 ctx->out_path = FIMC_IO_DMA;
1529 ctx->state = FIMC_CTX_CAP;
1530 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1531 ctx->d_frame.fmt = ctx->s_frame.fmt;
1532
1533 vfd = video_device_alloc();
1534 if (!vfd) {
1535 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
1536 goto err_vd_alloc;
1537 }
1538
1539 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
1540
1541 vfd->fops = &fimc_capture_fops;
1542 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
1543 vfd->v4l2_dev = v4l2_dev;
1544 vfd->minor = -1;
1545 vfd->release = video_device_release;
1546 vfd->lock = &fimc->lock;
1547 /* Locking in file operations other than ioctl should be done
1548 by the driver, not the V4L2 core.
1549 This driver needs auditing so that this flag can be removed. */
1550 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
1551 video_set_drvdata(vfd, fimc);
1552
1553 vid_cap = &fimc->vid_cap;
1554 vid_cap->vfd = vfd;
1555 vid_cap->active_buf_cnt = 0;
1556 vid_cap->reqbufs_count = 0;
1557 vid_cap->refcnt = 0;
1558
1559 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1560 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1561 vid_cap->ctx = ctx;
1562
1563 q = &fimc->vid_cap.vbq;
1564 memset(q, 0, sizeof(*q));
1565 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1566 q->io_modes = VB2_MMAP | VB2_USERPTR;
1567 q->drv_priv = fimc->vid_cap.ctx;
1568 q->ops = &fimc_capture_qops;
1569 q->mem_ops = &vb2_dma_contig_memops;
1570 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1571
1572 vb2_queue_init(q);
1573
1574 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1575 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
1576 if (ret)
1577 goto err_ent;
1578
1579 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1580 if (ret)
1581 goto err_vd;
1582
1583 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1584 vfd->name, video_device_node_name(vfd));
1585
1586 vfd->ctrl_handler = &ctx->ctrl_handler;
1587 return 0;
1588
1589 err_vd:
1590 media_entity_cleanup(&vfd->entity);
1591 err_ent:
1592 video_device_release(vfd);
1593 err_vd_alloc:
1594 kfree(ctx);
1595 return ret;
1596 }
1597
1598 static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
1599 {
1600 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1601 int ret;
1602
1603 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1604 if (ret)
1605 return ret;
1606
1607 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1608 if (ret)
1609 fimc_unregister_m2m_device(fimc);
1610
1611 return ret;
1612 }
1613
1614 static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1615 {
1616 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1617
1618 if (fimc == NULL)
1619 return;
1620
1621 fimc_unregister_m2m_device(fimc);
1622
1623 if (fimc->vid_cap.vfd) {
1624 media_entity_cleanup(&fimc->vid_cap.vfd->entity);
1625 video_unregister_device(fimc->vid_cap.vfd);
1626 fimc->vid_cap.vfd = NULL;
1627 }
1628
1629 kfree(fimc->vid_cap.ctx);
1630 fimc->vid_cap.ctx = NULL;
1631 }
1632
1633 static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1634 .registered = fimc_capture_subdev_registered,
1635 .unregistered = fimc_capture_subdev_unregistered,
1636 };
1637
1638 int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1639 {
1640 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1641 int ret;
1642
1643 v4l2_subdev_init(sd, &fimc_subdev_ops);
1644 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1645 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1646
1647 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1648 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1649 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1650 fimc->vid_cap.sd_pads, 0);
1651 if (ret)
1652 return ret;
1653
1654 sd->entity.ops = &fimc_sd_media_ops;
1655 sd->internal_ops = &fimc_capture_sd_internal_ops;
1656 v4l2_set_subdevdata(sd, fimc);
1657 return 0;
1658 }
1659
1660 void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1661 {
1662 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1663
1664 v4l2_device_unregister_subdev(sd);
1665 media_entity_cleanup(&sd->entity);
1666 v4l2_set_subdevdata(sd, NULL);
1667 }
This page took 0.062338 seconds and 4 git commands to generate.