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