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