2 * Samsung EXYNOS FIMC-LITE (camera host interface) driver
4 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
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.
11 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
13 #include <linux/bug.h>
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/errno.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-mem2mem.h>
31 #include <media/videobuf2-core.h>
32 #include <media/videobuf2-dma-contig.h>
33 #include <media/s5p_fimc.h>
36 #include "fimc-core.h"
37 #include "fimc-lite.h"
38 #include "fimc-lite-reg.h"
41 module_param(debug
, int, 0644);
43 static const struct fimc_fmt fimc_lite_formats
[] = {
45 .name
= "YUV 4:2:2 packed, YCbYCr",
46 .fourcc
= V4L2_PIX_FMT_YUYV
,
47 .colorspace
= V4L2_COLORSPACE_JPEG
,
49 .color
= FIMC_FMT_YCBYCR422
,
51 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
52 .flags
= FMT_FLAGS_YUV
,
54 .name
= "YUV 4:2:2 packed, CbYCrY",
55 .fourcc
= V4L2_PIX_FMT_UYVY
,
56 .colorspace
= V4L2_COLORSPACE_JPEG
,
58 .color
= FIMC_FMT_CBYCRY422
,
60 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
61 .flags
= FMT_FLAGS_YUV
,
63 .name
= "YUV 4:2:2 packed, CrYCbY",
64 .fourcc
= V4L2_PIX_FMT_VYUY
,
65 .colorspace
= V4L2_COLORSPACE_JPEG
,
67 .color
= FIMC_FMT_CRYCBY422
,
69 .mbus_code
= V4L2_MBUS_FMT_VYUY8_2X8
,
70 .flags
= FMT_FLAGS_YUV
,
72 .name
= "YUV 4:2:2 packed, YCrYCb",
73 .fourcc
= V4L2_PIX_FMT_YVYU
,
74 .colorspace
= V4L2_COLORSPACE_JPEG
,
76 .color
= FIMC_FMT_YCRYCB422
,
78 .mbus_code
= V4L2_MBUS_FMT_YVYU8_2X8
,
79 .flags
= FMT_FLAGS_YUV
,
81 .name
= "RAW8 (GRBG)",
82 .fourcc
= V4L2_PIX_FMT_SGRBG8
,
83 .colorspace
= V4L2_COLORSPACE_SRGB
,
85 .color
= FIMC_FMT_RAW8
,
87 .mbus_code
= V4L2_MBUS_FMT_SGRBG8_1X8
,
88 .flags
= FMT_FLAGS_RAW_BAYER
,
90 .name
= "RAW10 (GRBG)",
91 .fourcc
= V4L2_PIX_FMT_SGRBG10
,
92 .colorspace
= V4L2_COLORSPACE_SRGB
,
94 .color
= FIMC_FMT_RAW10
,
96 .mbus_code
= V4L2_MBUS_FMT_SGRBG10_1X10
,
97 .flags
= FMT_FLAGS_RAW_BAYER
,
99 .name
= "RAW12 (GRBG)",
100 .fourcc
= V4L2_PIX_FMT_SGRBG12
,
101 .colorspace
= V4L2_COLORSPACE_SRGB
,
103 .color
= FIMC_FMT_RAW12
,
105 .mbus_code
= V4L2_MBUS_FMT_SGRBG12_1X12
,
106 .flags
= FMT_FLAGS_RAW_BAYER
,
111 * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
112 * @pixelformat: fourcc to match, ignored if null
113 * @mbus_code: media bus code to match, ignored if null
114 * @mask: the color format flags to match
115 * @index: index to the fimc_lite_formats array, ignored if negative
117 static const struct fimc_fmt
*fimc_lite_find_format(const u32
*pixelformat
,
118 const u32
*mbus_code
, unsigned int mask
, int index
)
120 const struct fimc_fmt
*fmt
, *def_fmt
= NULL
;
124 if (index
>= (int)ARRAY_SIZE(fimc_lite_formats
))
127 for (i
= 0; i
< ARRAY_SIZE(fimc_lite_formats
); ++i
) {
128 fmt
= &fimc_lite_formats
[i
];
129 if (mask
&& !(fmt
->flags
& mask
))
131 if (pixelformat
&& fmt
->fourcc
== *pixelformat
)
133 if (mbus_code
&& fmt
->mbus_code
== *mbus_code
)
142 static int fimc_lite_hw_init(struct fimc_lite
*fimc
, bool isp_output
)
144 struct fimc_source_info
*si
;
147 if (fimc
->sensor
== NULL
)
150 if (fimc
->inp_frame
.fmt
== NULL
|| fimc
->out_frame
.fmt
== NULL
)
153 /* Get sensor configuration data from the sensor subdev */
154 si
= v4l2_get_subdev_hostdata(fimc
->sensor
);
158 spin_lock_irqsave(&fimc
->slock
, flags
);
160 flite_hw_set_camera_bus(fimc
, si
);
161 flite_hw_set_source_format(fimc
, &fimc
->inp_frame
);
162 flite_hw_set_window_offset(fimc
, &fimc
->inp_frame
);
163 flite_hw_set_dma_buf_mask(fimc
, 0);
164 flite_hw_set_output_dma(fimc
, &fimc
->out_frame
, !isp_output
);
165 flite_hw_set_interrupt_mask(fimc
);
166 flite_hw_set_test_pattern(fimc
, fimc
->test_pattern
->val
);
169 flite_hw_dump_regs(fimc
, __func__
);
171 spin_unlock_irqrestore(&fimc
->slock
, flags
);
176 * Reinitialize the driver so it is ready to start the streaming again.
177 * Set fimc->state to indicate stream off and the hardware shut down state.
178 * If not suspending (@suspend is false), return any buffers to videobuf2.
179 * Otherwise put any owned buffers onto the pending buffers queue, so they
180 * can be re-spun when the device is being resumed. Also perform FIMC
181 * software reset and disable streaming on the whole pipeline if required.
183 static int fimc_lite_reinit(struct fimc_lite
*fimc
, bool suspend
)
185 struct flite_buffer
*buf
;
189 spin_lock_irqsave(&fimc
->slock
, flags
);
190 streaming
= fimc
->state
& (1 << ST_SENSOR_STREAM
);
192 fimc
->state
&= ~(1 << ST_FLITE_RUN
| 1 << ST_FLITE_OFF
|
193 1 << ST_FLITE_STREAM
| 1 << ST_SENSOR_STREAM
);
195 fimc
->state
|= (1 << ST_FLITE_SUSPENDED
);
197 fimc
->state
&= ~(1 << ST_FLITE_PENDING
|
198 1 << ST_FLITE_SUSPENDED
);
200 /* Release unused buffers */
201 while (!suspend
&& !list_empty(&fimc
->pending_buf_q
)) {
202 buf
= fimc_lite_pending_queue_pop(fimc
);
203 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
205 /* If suspending put unused buffers onto pending queue */
206 while (!list_empty(&fimc
->active_buf_q
)) {
207 buf
= fimc_lite_active_queue_pop(fimc
);
209 fimc_lite_pending_queue_add(fimc
, buf
);
211 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
214 spin_unlock_irqrestore(&fimc
->slock
, flags
);
216 flite_hw_reset(fimc
);
221 return fimc_pipeline_call(&fimc
->ve
, set_stream
, 0);
224 static int fimc_lite_stop_capture(struct fimc_lite
*fimc
, bool suspend
)
228 if (!fimc_lite_active(fimc
))
231 spin_lock_irqsave(&fimc
->slock
, flags
);
232 set_bit(ST_FLITE_OFF
, &fimc
->state
);
233 flite_hw_capture_stop(fimc
);
234 spin_unlock_irqrestore(&fimc
->slock
, flags
);
236 wait_event_timeout(fimc
->irq_queue
,
237 !test_bit(ST_FLITE_OFF
, &fimc
->state
),
238 (2*HZ
/10)); /* 200 ms */
240 return fimc_lite_reinit(fimc
, suspend
);
243 /* Must be called with fimc.slock spinlock held. */
244 static void fimc_lite_config_update(struct fimc_lite
*fimc
)
246 flite_hw_set_window_offset(fimc
, &fimc
->inp_frame
);
247 flite_hw_set_dma_window(fimc
, &fimc
->out_frame
);
248 flite_hw_set_test_pattern(fimc
, fimc
->test_pattern
->val
);
249 clear_bit(ST_FLITE_CONFIG
, &fimc
->state
);
252 static irqreturn_t
flite_irq_handler(int irq
, void *priv
)
254 struct fimc_lite
*fimc
= priv
;
255 struct flite_buffer
*vbuf
;
261 spin_lock_irqsave(&fimc
->slock
, flags
);
263 intsrc
= flite_hw_get_interrupt_source(fimc
);
264 flite_hw_clear_pending_irq(fimc
);
266 if (test_and_clear_bit(ST_FLITE_OFF
, &fimc
->state
)) {
267 wake_up(&fimc
->irq_queue
);
271 if (intsrc
& FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW
) {
272 clear_bit(ST_FLITE_RUN
, &fimc
->state
);
273 fimc
->events
.data_overflow
++;
276 if (intsrc
& FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND
) {
277 flite_hw_clear_last_capture_end(fimc
);
278 clear_bit(ST_FLITE_STREAM
, &fimc
->state
);
279 wake_up(&fimc
->irq_queue
);
282 if (atomic_read(&fimc
->out_path
) != FIMC_IO_DMA
)
285 if ((intsrc
& FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART
) &&
286 test_bit(ST_FLITE_RUN
, &fimc
->state
) &&
287 !list_empty(&fimc
->pending_buf_q
)) {
288 vbuf
= fimc_lite_pending_queue_pop(fimc
);
289 flite_hw_set_dma_buffer(fimc
, vbuf
);
290 fimc_lite_active_queue_add(fimc
, vbuf
);
293 if ((intsrc
& FLITE_REG_CISTATUS_IRQ_SRC_FRMEND
) &&
294 test_bit(ST_FLITE_RUN
, &fimc
->state
) &&
295 !list_empty(&fimc
->active_buf_q
)) {
296 vbuf
= fimc_lite_active_queue_pop(fimc
);
298 tv
= &vbuf
->vb
.v4l2_buf
.timestamp
;
299 tv
->tv_sec
= ts
.tv_sec
;
300 tv
->tv_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
301 vbuf
->vb
.v4l2_buf
.sequence
= fimc
->frame_count
++;
302 flite_hw_mask_dma_buffer(fimc
, vbuf
->index
);
303 vb2_buffer_done(&vbuf
->vb
, VB2_BUF_STATE_DONE
);
306 if (test_bit(ST_FLITE_CONFIG
, &fimc
->state
))
307 fimc_lite_config_update(fimc
);
309 if (list_empty(&fimc
->pending_buf_q
)) {
310 flite_hw_capture_stop(fimc
);
311 clear_bit(ST_FLITE_STREAM
, &fimc
->state
);
314 set_bit(ST_FLITE_RUN
, &fimc
->state
);
315 spin_unlock_irqrestore(&fimc
->slock
, flags
);
319 static int start_streaming(struct vb2_queue
*q
, unsigned int count
)
321 struct fimc_lite
*fimc
= q
->drv_priv
;
325 spin_lock_irqsave(&fimc
->slock
, flags
);
328 fimc
->frame_count
= 0;
330 spin_unlock_irqrestore(&fimc
->slock
, flags
);
332 ret
= fimc_lite_hw_init(fimc
, false);
334 fimc_lite_reinit(fimc
, false);
338 set_bit(ST_FLITE_PENDING
, &fimc
->state
);
340 if (!list_empty(&fimc
->active_buf_q
) &&
341 !test_and_set_bit(ST_FLITE_STREAM
, &fimc
->state
)) {
342 flite_hw_capture_start(fimc
);
344 if (!test_and_set_bit(ST_SENSOR_STREAM
, &fimc
->state
))
345 fimc_pipeline_call(&fimc
->ve
, set_stream
, 1);
348 flite_hw_dump_regs(fimc
, __func__
);
353 static int stop_streaming(struct vb2_queue
*q
)
355 struct fimc_lite
*fimc
= q
->drv_priv
;
357 if (!fimc_lite_active(fimc
))
360 return fimc_lite_stop_capture(fimc
, false);
363 static int queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*pfmt
,
364 unsigned int *num_buffers
, unsigned int *num_planes
,
365 unsigned int sizes
[], void *allocators
[])
367 const struct v4l2_pix_format_mplane
*pixm
= NULL
;
368 struct fimc_lite
*fimc
= vq
->drv_priv
;
369 struct flite_frame
*frame
= &fimc
->out_frame
;
370 const struct fimc_fmt
*fmt
= frame
->fmt
;
375 pixm
= &pfmt
->fmt
.pix_mp
;
376 fmt
= fimc_lite_find_format(&pixm
->pixelformat
, NULL
, 0, -1);
377 wh
= pixm
->width
* pixm
->height
;
379 wh
= frame
->f_width
* frame
->f_height
;
385 *num_planes
= fmt
->memplanes
;
387 for (i
= 0; i
< fmt
->memplanes
; i
++) {
388 unsigned int size
= (wh
* fmt
->depth
[i
]) / 8;
390 sizes
[i
] = max(size
, pixm
->plane_fmt
[i
].sizeimage
);
393 allocators
[i
] = fimc
->alloc_ctx
;
399 static int buffer_prepare(struct vb2_buffer
*vb
)
401 struct vb2_queue
*vq
= vb
->vb2_queue
;
402 struct fimc_lite
*fimc
= vq
->drv_priv
;
405 if (fimc
->out_frame
.fmt
== NULL
)
408 for (i
= 0; i
< fimc
->out_frame
.fmt
->memplanes
; i
++) {
409 unsigned long size
= fimc
->payload
[i
];
411 if (vb2_plane_size(vb
, i
) < size
) {
412 v4l2_err(&fimc
->ve
.vdev
,
413 "User buffer too small (%ld < %ld)\n",
414 vb2_plane_size(vb
, i
), size
);
417 vb2_set_plane_payload(vb
, i
, size
);
423 static void buffer_queue(struct vb2_buffer
*vb
)
425 struct flite_buffer
*buf
426 = container_of(vb
, struct flite_buffer
, vb
);
427 struct fimc_lite
*fimc
= vb2_get_drv_priv(vb
->vb2_queue
);
430 spin_lock_irqsave(&fimc
->slock
, flags
);
431 buf
->paddr
= vb2_dma_contig_plane_dma_addr(vb
, 0);
433 buf
->index
= fimc
->buf_index
++;
434 if (fimc
->buf_index
>= fimc
->reqbufs_count
)
437 if (!test_bit(ST_FLITE_SUSPENDED
, &fimc
->state
) &&
438 !test_bit(ST_FLITE_STREAM
, &fimc
->state
) &&
439 list_empty(&fimc
->active_buf_q
)) {
440 flite_hw_set_dma_buffer(fimc
, buf
);
441 fimc_lite_active_queue_add(fimc
, buf
);
443 fimc_lite_pending_queue_add(fimc
, buf
);
446 if (vb2_is_streaming(&fimc
->vb_queue
) &&
447 !list_empty(&fimc
->pending_buf_q
) &&
448 !test_and_set_bit(ST_FLITE_STREAM
, &fimc
->state
)) {
449 flite_hw_capture_start(fimc
);
450 spin_unlock_irqrestore(&fimc
->slock
, flags
);
452 if (!test_and_set_bit(ST_SENSOR_STREAM
, &fimc
->state
))
453 fimc_pipeline_call(&fimc
->ve
, set_stream
, 1);
456 spin_unlock_irqrestore(&fimc
->slock
, flags
);
459 static const struct vb2_ops fimc_lite_qops
= {
460 .queue_setup
= queue_setup
,
461 .buf_prepare
= buffer_prepare
,
462 .buf_queue
= buffer_queue
,
463 .wait_prepare
= vb2_ops_wait_prepare
,
464 .wait_finish
= vb2_ops_wait_finish
,
465 .start_streaming
= start_streaming
,
466 .stop_streaming
= stop_streaming
,
469 static void fimc_lite_clear_event_counters(struct fimc_lite
*fimc
)
473 spin_lock_irqsave(&fimc
->slock
, flags
);
474 memset(&fimc
->events
, 0, sizeof(fimc
->events
));
475 spin_unlock_irqrestore(&fimc
->slock
, flags
);
478 static int fimc_lite_open(struct file
*file
)
480 struct fimc_lite
*fimc
= video_drvdata(file
);
481 struct media_entity
*me
= &fimc
->ve
.vdev
.entity
;
484 mutex_lock(&fimc
->lock
);
485 if (atomic_read(&fimc
->out_path
) != FIMC_IO_DMA
) {
490 set_bit(ST_FLITE_IN_USE
, &fimc
->state
);
491 ret
= pm_runtime_get_sync(&fimc
->pdev
->dev
);
495 ret
= v4l2_fh_open(file
);
499 if (!v4l2_fh_is_singular_file(file
) ||
500 atomic_read(&fimc
->out_path
) != FIMC_IO_DMA
)
503 mutex_lock(&me
->parent
->graph_mutex
);
505 ret
= fimc_pipeline_call(&fimc
->ve
, open
, me
, true);
507 /* Mark video pipeline ending at this video node as in use. */
511 mutex_unlock(&me
->parent
->graph_mutex
);
514 fimc_lite_clear_event_counters(fimc
);
518 v4l2_fh_release(file
);
520 pm_runtime_put_sync(&fimc
->pdev
->dev
);
521 clear_bit(ST_FLITE_IN_USE
, &fimc
->state
);
523 mutex_unlock(&fimc
->lock
);
527 static int fimc_lite_release(struct file
*file
)
529 struct fimc_lite
*fimc
= video_drvdata(file
);
530 struct media_entity
*entity
= &fimc
->ve
.vdev
.entity
;
532 mutex_lock(&fimc
->lock
);
534 if (v4l2_fh_is_singular_file(file
) &&
535 atomic_read(&fimc
->out_path
) == FIMC_IO_DMA
) {
536 if (fimc
->streaming
) {
537 media_entity_pipeline_stop(entity
);
538 fimc
->streaming
= false;
540 fimc_lite_stop_capture(fimc
, false);
541 fimc_pipeline_call(&fimc
->ve
, close
);
542 clear_bit(ST_FLITE_IN_USE
, &fimc
->state
);
544 mutex_lock(&entity
->parent
->graph_mutex
);
546 mutex_unlock(&entity
->parent
->graph_mutex
);
549 _vb2_fop_release(file
, NULL
);
550 pm_runtime_put(&fimc
->pdev
->dev
);
551 clear_bit(ST_FLITE_SUSPENDED
, &fimc
->state
);
553 mutex_unlock(&fimc
->lock
);
557 static const struct v4l2_file_operations fimc_lite_fops
= {
558 .owner
= THIS_MODULE
,
559 .open
= fimc_lite_open
,
560 .release
= fimc_lite_release
,
561 .poll
= vb2_fop_poll
,
562 .unlocked_ioctl
= video_ioctl2
,
563 .mmap
= vb2_fop_mmap
,
567 * Format and crop negotiation helpers
570 static const struct fimc_fmt
*fimc_lite_subdev_try_fmt(struct fimc_lite
*fimc
,
571 struct v4l2_subdev_fh
*fh
,
572 struct v4l2_subdev_format
*format
)
574 struct flite_drvdata
*dd
= fimc
->dd
;
575 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
576 const struct fimc_fmt
*fmt
= NULL
;
578 if (format
->pad
== FLITE_SD_PAD_SINK
) {
579 v4l_bound_align_image(&mf
->width
, 8, dd
->max_width
,
580 ffs(dd
->out_width_align
) - 1,
581 &mf
->height
, 0, dd
->max_height
, 0, 0);
583 fmt
= fimc_lite_find_format(NULL
, &mf
->code
, 0, 0);
587 mf
->colorspace
= fmt
->colorspace
;
588 mf
->code
= fmt
->mbus_code
;
590 struct flite_frame
*sink
= &fimc
->inp_frame
;
591 struct v4l2_mbus_framefmt
*sink_fmt
;
592 struct v4l2_rect
*rect
;
594 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
595 sink_fmt
= v4l2_subdev_get_try_format(fh
,
598 mf
->code
= sink_fmt
->code
;
599 mf
->colorspace
= sink_fmt
->colorspace
;
601 rect
= v4l2_subdev_get_try_crop(fh
,
604 mf
->code
= sink
->fmt
->mbus_code
;
605 mf
->colorspace
= sink
->fmt
->colorspace
;
609 /* Allow changing format only on sink pad */
610 mf
->width
= rect
->width
;
611 mf
->height
= rect
->height
;
614 mf
->field
= V4L2_FIELD_NONE
;
616 v4l2_dbg(1, debug
, &fimc
->subdev
, "code: %#x (%d), %dx%d\n",
617 mf
->code
, mf
->colorspace
, mf
->width
, mf
->height
);
622 static void fimc_lite_try_crop(struct fimc_lite
*fimc
, struct v4l2_rect
*r
)
624 struct flite_frame
*frame
= &fimc
->inp_frame
;
626 v4l_bound_align_image(&r
->width
, 0, frame
->f_width
, 0,
627 &r
->height
, 0, frame
->f_height
, 0, 0);
629 /* Adjust left/top if cropping rectangle got out of bounds */
630 r
->left
= clamp_t(u32
, r
->left
, 0, frame
->f_width
- r
->width
);
631 r
->left
= round_down(r
->left
, fimc
->dd
->win_hor_offs_align
);
632 r
->top
= clamp_t(u32
, r
->top
, 0, frame
->f_height
- r
->height
);
634 v4l2_dbg(1, debug
, &fimc
->subdev
, "(%d,%d)/%dx%d, sink fmt: %dx%d\n",
635 r
->left
, r
->top
, r
->width
, r
->height
,
636 frame
->f_width
, frame
->f_height
);
639 static void fimc_lite_try_compose(struct fimc_lite
*fimc
, struct v4l2_rect
*r
)
641 struct flite_frame
*frame
= &fimc
->out_frame
;
642 struct v4l2_rect
*crop_rect
= &fimc
->inp_frame
.rect
;
644 /* Scaling is not supported so we enforce compose rectangle size
645 same as size of the sink crop rectangle. */
646 r
->width
= crop_rect
->width
;
647 r
->height
= crop_rect
->height
;
649 /* Adjust left/top if the composing rectangle got out of bounds */
650 r
->left
= clamp_t(u32
, r
->left
, 0, frame
->f_width
- r
->width
);
651 r
->left
= round_down(r
->left
, fimc
->dd
->out_hor_offs_align
);
652 r
->top
= clamp_t(u32
, r
->top
, 0, fimc
->out_frame
.f_height
- r
->height
);
654 v4l2_dbg(1, debug
, &fimc
->subdev
, "(%d,%d)/%dx%d, source fmt: %dx%d\n",
655 r
->left
, r
->top
, r
->width
, r
->height
,
656 frame
->f_width
, frame
->f_height
);
660 * Video node ioctl operations
662 static int fimc_lite_querycap(struct file
*file
, void *priv
,
663 struct v4l2_capability
*cap
)
665 struct fimc_lite
*fimc
= video_drvdata(file
);
667 strlcpy(cap
->driver
, FIMC_LITE_DRV_NAME
, sizeof(cap
->driver
));
668 strlcpy(cap
->card
, FIMC_LITE_DRV_NAME
, sizeof(cap
->card
));
669 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
670 dev_name(&fimc
->pdev
->dev
));
672 cap
->device_caps
= V4L2_CAP_STREAMING
;
673 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
677 static int fimc_lite_enum_fmt_mplane(struct file
*file
, void *priv
,
678 struct v4l2_fmtdesc
*f
)
680 const struct fimc_fmt
*fmt
;
682 if (f
->index
>= ARRAY_SIZE(fimc_lite_formats
))
685 fmt
= &fimc_lite_formats
[f
->index
];
686 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
687 f
->pixelformat
= fmt
->fourcc
;
692 static int fimc_lite_g_fmt_mplane(struct file
*file
, void *fh
,
693 struct v4l2_format
*f
)
695 struct fimc_lite
*fimc
= video_drvdata(file
);
696 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
697 struct v4l2_plane_pix_format
*plane_fmt
= &pixm
->plane_fmt
[0];
698 struct flite_frame
*frame
= &fimc
->out_frame
;
699 const struct fimc_fmt
*fmt
= frame
->fmt
;
701 plane_fmt
->bytesperline
= (frame
->f_width
* fmt
->depth
[0]) / 8;
702 plane_fmt
->sizeimage
= plane_fmt
->bytesperline
* frame
->f_height
;
704 pixm
->num_planes
= fmt
->memplanes
;
705 pixm
->pixelformat
= fmt
->fourcc
;
706 pixm
->width
= frame
->f_width
;
707 pixm
->height
= frame
->f_height
;
708 pixm
->field
= V4L2_FIELD_NONE
;
709 pixm
->colorspace
= fmt
->colorspace
;
713 static int fimc_lite_try_fmt(struct fimc_lite
*fimc
,
714 struct v4l2_pix_format_mplane
*pixm
,
715 const struct fimc_fmt
**ffmt
)
717 u32 bpl
= pixm
->plane_fmt
[0].bytesperline
;
718 struct flite_drvdata
*dd
= fimc
->dd
;
719 const struct fimc_fmt
*inp_fmt
= fimc
->inp_frame
.fmt
;
720 const struct fimc_fmt
*fmt
;
722 if (WARN_ON(inp_fmt
== NULL
))
725 * We allow some flexibility only for YUV formats. In case of raw
726 * raw Bayer the FIMC-LITE's output format must match its camera
727 * interface input format.
729 if (inp_fmt
->flags
& FMT_FLAGS_YUV
)
730 fmt
= fimc_lite_find_format(&pixm
->pixelformat
, NULL
,
735 if (WARN_ON(fmt
== NULL
))
739 v4l_bound_align_image(&pixm
->width
, 8, dd
->max_width
,
740 ffs(dd
->out_width_align
) - 1,
741 &pixm
->height
, 0, dd
->max_height
, 0, 0);
743 if ((bpl
== 0 || ((bpl
* 8) / fmt
->depth
[0]) < pixm
->width
))
744 pixm
->plane_fmt
[0].bytesperline
= (pixm
->width
*
747 if (pixm
->plane_fmt
[0].sizeimage
== 0)
748 pixm
->plane_fmt
[0].sizeimage
= (pixm
->width
* pixm
->height
*
750 pixm
->num_planes
= fmt
->memplanes
;
751 pixm
->pixelformat
= fmt
->fourcc
;
752 pixm
->colorspace
= fmt
->colorspace
;
753 pixm
->field
= V4L2_FIELD_NONE
;
757 static int fimc_lite_try_fmt_mplane(struct file
*file
, void *fh
,
758 struct v4l2_format
*f
)
760 struct fimc_lite
*fimc
= video_drvdata(file
);
761 return fimc_lite_try_fmt(fimc
, &f
->fmt
.pix_mp
, NULL
);
764 static int fimc_lite_s_fmt_mplane(struct file
*file
, void *priv
,
765 struct v4l2_format
*f
)
767 struct v4l2_pix_format_mplane
*pixm
= &f
->fmt
.pix_mp
;
768 struct fimc_lite
*fimc
= video_drvdata(file
);
769 struct flite_frame
*frame
= &fimc
->out_frame
;
770 const struct fimc_fmt
*fmt
= NULL
;
773 if (vb2_is_busy(&fimc
->vb_queue
))
776 ret
= fimc_lite_try_fmt(fimc
, &f
->fmt
.pix_mp
, &fmt
);
781 fimc
->payload
[0] = max((pixm
->width
* pixm
->height
* fmt
->depth
[0]) / 8,
782 pixm
->plane_fmt
[0].sizeimage
);
783 frame
->f_width
= pixm
->width
;
784 frame
->f_height
= pixm
->height
;
789 static int fimc_pipeline_validate(struct fimc_lite
*fimc
)
791 struct v4l2_subdev
*sd
= &fimc
->subdev
;
792 struct v4l2_subdev_format sink_fmt
, src_fmt
;
793 struct media_pad
*pad
;
797 /* Retrieve format at the sink pad */
798 pad
= &sd
->entity
.pads
[0];
799 if (!(pad
->flags
& MEDIA_PAD_FL_SINK
))
801 /* Don't call FIMC subdev operation to avoid nested locking */
802 if (sd
== &fimc
->subdev
) {
803 struct flite_frame
*ff
= &fimc
->out_frame
;
804 sink_fmt
.format
.width
= ff
->f_width
;
805 sink_fmt
.format
.height
= ff
->f_height
;
806 sink_fmt
.format
.code
= fimc
->inp_frame
.fmt
->mbus_code
;
808 sink_fmt
.pad
= pad
->index
;
809 sink_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
810 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
,
812 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
815 /* Retrieve format at the source pad */
816 pad
= media_entity_remote_pad(pad
);
818 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
821 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
822 src_fmt
.pad
= pad
->index
;
823 src_fmt
.which
= V4L2_SUBDEV_FORMAT_ACTIVE
;
824 ret
= v4l2_subdev_call(sd
, pad
, get_fmt
, NULL
, &src_fmt
);
825 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
828 if (src_fmt
.format
.width
!= sink_fmt
.format
.width
||
829 src_fmt
.format
.height
!= sink_fmt
.format
.height
||
830 src_fmt
.format
.code
!= sink_fmt
.format
.code
)
836 static int fimc_lite_streamon(struct file
*file
, void *priv
,
837 enum v4l2_buf_type type
)
839 struct fimc_lite
*fimc
= video_drvdata(file
);
840 struct media_entity
*entity
= &fimc
->ve
.vdev
.entity
;
843 if (fimc_lite_active(fimc
))
846 ret
= media_entity_pipeline_start(entity
, &fimc
->ve
.pipe
->mp
);
850 ret
= fimc_pipeline_validate(fimc
);
854 fimc
->sensor
= fimc_find_remote_sensor(&fimc
->subdev
.entity
);
856 ret
= vb2_ioctl_streamon(file
, priv
, type
);
858 fimc
->streaming
= true;
863 media_entity_pipeline_stop(entity
);
867 static int fimc_lite_streamoff(struct file
*file
, void *priv
,
868 enum v4l2_buf_type type
)
870 struct fimc_lite
*fimc
= video_drvdata(file
);
873 ret
= vb2_ioctl_streamoff(file
, priv
, type
);
877 media_entity_pipeline_stop(&fimc
->ve
.vdev
.entity
);
878 fimc
->streaming
= false;
882 static int fimc_lite_reqbufs(struct file
*file
, void *priv
,
883 struct v4l2_requestbuffers
*reqbufs
)
885 struct fimc_lite
*fimc
= video_drvdata(file
);
888 reqbufs
->count
= max_t(u32
, FLITE_REQ_BUFS_MIN
, reqbufs
->count
);
889 ret
= vb2_ioctl_reqbufs(file
, priv
, reqbufs
);
891 fimc
->reqbufs_count
= reqbufs
->count
;
896 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
897 static int enclosed_rectangle(struct v4l2_rect
*a
, struct v4l2_rect
*b
)
899 if (a
->left
< b
->left
|| a
->top
< b
->top
)
901 if (a
->left
+ a
->width
> b
->left
+ b
->width
)
903 if (a
->top
+ a
->height
> b
->top
+ b
->height
)
909 static int fimc_lite_g_selection(struct file
*file
, void *fh
,
910 struct v4l2_selection
*sel
)
912 struct fimc_lite
*fimc
= video_drvdata(file
);
913 struct flite_frame
*f
= &fimc
->out_frame
;
915 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
918 switch (sel
->target
) {
919 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
920 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
923 sel
->r
.width
= f
->f_width
;
924 sel
->r
.height
= f
->f_height
;
927 case V4L2_SEL_TGT_COMPOSE
:
935 static int fimc_lite_s_selection(struct file
*file
, void *fh
,
936 struct v4l2_selection
*sel
)
938 struct fimc_lite
*fimc
= video_drvdata(file
);
939 struct flite_frame
*f
= &fimc
->out_frame
;
940 struct v4l2_rect rect
= sel
->r
;
943 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
||
944 sel
->target
!= V4L2_SEL_TGT_COMPOSE
)
947 fimc_lite_try_compose(fimc
, &rect
);
949 if ((sel
->flags
& V4L2_SEL_FLAG_LE
) &&
950 !enclosed_rectangle(&rect
, &sel
->r
))
953 if ((sel
->flags
& V4L2_SEL_FLAG_GE
) &&
954 !enclosed_rectangle(&sel
->r
, &rect
))
958 spin_lock_irqsave(&fimc
->slock
, flags
);
960 set_bit(ST_FLITE_CONFIG
, &fimc
->state
);
961 spin_unlock_irqrestore(&fimc
->slock
, flags
);
966 static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops
= {
967 .vidioc_querycap
= fimc_lite_querycap
,
968 .vidioc_enum_fmt_vid_cap_mplane
= fimc_lite_enum_fmt_mplane
,
969 .vidioc_try_fmt_vid_cap_mplane
= fimc_lite_try_fmt_mplane
,
970 .vidioc_s_fmt_vid_cap_mplane
= fimc_lite_s_fmt_mplane
,
971 .vidioc_g_fmt_vid_cap_mplane
= fimc_lite_g_fmt_mplane
,
972 .vidioc_g_selection
= fimc_lite_g_selection
,
973 .vidioc_s_selection
= fimc_lite_s_selection
,
974 .vidioc_reqbufs
= fimc_lite_reqbufs
,
975 .vidioc_querybuf
= vb2_ioctl_querybuf
,
976 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
977 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
978 .vidioc_qbuf
= vb2_ioctl_qbuf
,
979 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
980 .vidioc_streamon
= fimc_lite_streamon
,
981 .vidioc_streamoff
= fimc_lite_streamoff
,
984 /* Capture subdev media entity operations */
985 static int fimc_lite_link_setup(struct media_entity
*entity
,
986 const struct media_pad
*local
,
987 const struct media_pad
*remote
, u32 flags
)
989 struct v4l2_subdev
*sd
= media_entity_to_v4l2_subdev(entity
);
990 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
991 unsigned int remote_ent_type
= media_entity_type(remote
->entity
);
994 if (WARN_ON(fimc
== NULL
))
997 v4l2_dbg(1, debug
, sd
, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x\n",
998 __func__
, remote
->entity
->name
, local
->entity
->name
,
999 flags
, fimc
->source_subdev_grp_id
);
1001 switch (local
->index
) {
1002 case FLITE_SD_PAD_SINK
:
1003 if (remote_ent_type
!= MEDIA_ENT_T_V4L2_SUBDEV
) {
1007 if (flags
& MEDIA_LNK_FL_ENABLED
) {
1008 if (fimc
->source_subdev_grp_id
== 0)
1009 fimc
->source_subdev_grp_id
= sd
->grp_id
;
1013 fimc
->source_subdev_grp_id
= 0;
1014 fimc
->sensor
= NULL
;
1018 case FLITE_SD_PAD_SOURCE_DMA
:
1019 if (!(flags
& MEDIA_LNK_FL_ENABLED
))
1020 atomic_set(&fimc
->out_path
, FIMC_IO_NONE
);
1021 else if (remote_ent_type
== MEDIA_ENT_T_DEVNODE
)
1022 atomic_set(&fimc
->out_path
, FIMC_IO_DMA
);
1027 case FLITE_SD_PAD_SOURCE_ISP
:
1028 if (!(flags
& MEDIA_LNK_FL_ENABLED
))
1029 atomic_set(&fimc
->out_path
, FIMC_IO_NONE
);
1030 else if (remote_ent_type
== MEDIA_ENT_T_V4L2_SUBDEV
)
1031 atomic_set(&fimc
->out_path
, FIMC_IO_ISP
);
1037 v4l2_err(sd
, "Invalid pad index\n");
1045 static const struct media_entity_operations fimc_lite_subdev_media_ops
= {
1046 .link_setup
= fimc_lite_link_setup
,
1049 static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev
*sd
,
1050 struct v4l2_subdev_fh
*fh
,
1051 struct v4l2_subdev_mbus_code_enum
*code
)
1053 const struct fimc_fmt
*fmt
;
1055 fmt
= fimc_lite_find_format(NULL
, NULL
, 0, code
->index
);
1058 code
->code
= fmt
->mbus_code
;
1062 static struct v4l2_mbus_framefmt
*__fimc_lite_subdev_get_try_fmt(
1063 struct v4l2_subdev_fh
*fh
, unsigned int pad
)
1065 if (pad
!= FLITE_SD_PAD_SINK
)
1066 pad
= FLITE_SD_PAD_SOURCE_DMA
;
1068 return v4l2_subdev_get_try_format(fh
, pad
);
1071 static int fimc_lite_subdev_get_fmt(struct v4l2_subdev
*sd
,
1072 struct v4l2_subdev_fh
*fh
,
1073 struct v4l2_subdev_format
*fmt
)
1075 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1076 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1077 struct flite_frame
*f
= &fimc
->inp_frame
;
1079 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1080 mf
= __fimc_lite_subdev_get_try_fmt(fh
, fmt
->pad
);
1085 mutex_lock(&fimc
->lock
);
1086 mf
->colorspace
= f
->fmt
->colorspace
;
1087 mf
->code
= f
->fmt
->mbus_code
;
1089 if (fmt
->pad
== FLITE_SD_PAD_SINK
) {
1090 /* full camera input frame size */
1091 mf
->width
= f
->f_width
;
1092 mf
->height
= f
->f_height
;
1095 mf
->width
= f
->rect
.width
;
1096 mf
->height
= f
->rect
.height
;
1098 mutex_unlock(&fimc
->lock
);
1102 static int fimc_lite_subdev_set_fmt(struct v4l2_subdev
*sd
,
1103 struct v4l2_subdev_fh
*fh
,
1104 struct v4l2_subdev_format
*fmt
)
1106 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1107 struct v4l2_mbus_framefmt
*mf
= &fmt
->format
;
1108 struct flite_frame
*sink
= &fimc
->inp_frame
;
1109 struct flite_frame
*source
= &fimc
->out_frame
;
1110 const struct fimc_fmt
*ffmt
;
1112 v4l2_dbg(1, debug
, sd
, "pad%d: code: 0x%x, %dx%d\n",
1113 fmt
->pad
, mf
->code
, mf
->width
, mf
->height
);
1115 mutex_lock(&fimc
->lock
);
1117 if ((atomic_read(&fimc
->out_path
) == FIMC_IO_ISP
&&
1118 sd
->entity
.stream_count
> 0) ||
1119 (atomic_read(&fimc
->out_path
) == FIMC_IO_DMA
&&
1120 vb2_is_busy(&fimc
->vb_queue
))) {
1121 mutex_unlock(&fimc
->lock
);
1125 ffmt
= fimc_lite_subdev_try_fmt(fimc
, fh
, fmt
);
1127 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1128 struct v4l2_mbus_framefmt
*src_fmt
;
1130 mf
= __fimc_lite_subdev_get_try_fmt(fh
, fmt
->pad
);
1133 if (fmt
->pad
== FLITE_SD_PAD_SINK
) {
1134 unsigned int pad
= FLITE_SD_PAD_SOURCE_DMA
;
1135 src_fmt
= __fimc_lite_subdev_get_try_fmt(fh
, pad
);
1139 mutex_unlock(&fimc
->lock
);
1143 if (fmt
->pad
== FLITE_SD_PAD_SINK
) {
1144 sink
->f_width
= mf
->width
;
1145 sink
->f_height
= mf
->height
;
1147 /* Set sink crop rectangle */
1148 sink
->rect
.width
= mf
->width
;
1149 sink
->rect
.height
= mf
->height
;
1150 sink
->rect
.left
= 0;
1152 /* Reset source format and crop rectangle */
1153 source
->rect
= sink
->rect
;
1154 source
->f_width
= mf
->width
;
1155 source
->f_height
= mf
->height
;
1158 mutex_unlock(&fimc
->lock
);
1162 static int fimc_lite_subdev_get_selection(struct v4l2_subdev
*sd
,
1163 struct v4l2_subdev_fh
*fh
,
1164 struct v4l2_subdev_selection
*sel
)
1166 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1167 struct flite_frame
*f
= &fimc
->inp_frame
;
1169 if ((sel
->target
!= V4L2_SEL_TGT_CROP
&&
1170 sel
->target
!= V4L2_SEL_TGT_CROP_BOUNDS
) ||
1171 sel
->pad
!= FLITE_SD_PAD_SINK
)
1174 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1175 sel
->r
= *v4l2_subdev_get_try_crop(fh
, sel
->pad
);
1179 mutex_lock(&fimc
->lock
);
1180 if (sel
->target
== V4L2_SEL_TGT_CROP
) {
1185 sel
->r
.width
= f
->f_width
;
1186 sel
->r
.height
= f
->f_height
;
1188 mutex_unlock(&fimc
->lock
);
1190 v4l2_dbg(1, debug
, sd
, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1191 __func__
, f
->rect
.left
, f
->rect
.top
, f
->rect
.width
,
1192 f
->rect
.height
, f
->f_width
, f
->f_height
);
1197 static int fimc_lite_subdev_set_selection(struct v4l2_subdev
*sd
,
1198 struct v4l2_subdev_fh
*fh
,
1199 struct v4l2_subdev_selection
*sel
)
1201 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1202 struct flite_frame
*f
= &fimc
->inp_frame
;
1205 if (sel
->target
!= V4L2_SEL_TGT_CROP
|| sel
->pad
!= FLITE_SD_PAD_SINK
)
1208 mutex_lock(&fimc
->lock
);
1209 fimc_lite_try_crop(fimc
, &sel
->r
);
1211 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1212 *v4l2_subdev_get_try_crop(fh
, sel
->pad
) = sel
->r
;
1214 unsigned long flags
;
1215 spin_lock_irqsave(&fimc
->slock
, flags
);
1217 /* Same crop rectangle on the source pad */
1218 fimc
->out_frame
.rect
= sel
->r
;
1219 set_bit(ST_FLITE_CONFIG
, &fimc
->state
);
1220 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1222 mutex_unlock(&fimc
->lock
);
1224 v4l2_dbg(1, debug
, sd
, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1225 __func__
, f
->rect
.left
, f
->rect
.top
, f
->rect
.width
,
1226 f
->rect
.height
, f
->f_width
, f
->f_height
);
1231 static int fimc_lite_subdev_s_stream(struct v4l2_subdev
*sd
, int on
)
1233 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1234 unsigned long flags
;
1238 * Find sensor subdev linked to FIMC-LITE directly or through
1239 * MIPI-CSIS. This is required for configuration where FIMC-LITE
1240 * is used as a subdev only and feeds data internally to FIMC-IS.
1241 * The pipeline links are protected through entity.stream_count
1242 * so there is no need to take the media graph mutex here.
1244 fimc
->sensor
= fimc_find_remote_sensor(&sd
->entity
);
1246 if (atomic_read(&fimc
->out_path
) != FIMC_IO_ISP
)
1247 return -ENOIOCTLCMD
;
1249 mutex_lock(&fimc
->lock
);
1251 flite_hw_reset(fimc
);
1252 ret
= fimc_lite_hw_init(fimc
, true);
1254 spin_lock_irqsave(&fimc
->slock
, flags
);
1255 flite_hw_capture_start(fimc
);
1256 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1259 set_bit(ST_FLITE_OFF
, &fimc
->state
);
1261 spin_lock_irqsave(&fimc
->slock
, flags
);
1262 flite_hw_capture_stop(fimc
);
1263 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1265 ret
= wait_event_timeout(fimc
->irq_queue
,
1266 !test_bit(ST_FLITE_OFF
, &fimc
->state
),
1267 msecs_to_jiffies(200));
1269 v4l2_err(sd
, "s_stream(0) timeout\n");
1270 clear_bit(ST_FLITE_RUN
, &fimc
->state
);
1273 mutex_unlock(&fimc
->lock
);
1277 static int fimc_lite_log_status(struct v4l2_subdev
*sd
)
1279 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1281 flite_hw_dump_regs(fimc
, __func__
);
1285 static int fimc_lite_subdev_registered(struct v4l2_subdev
*sd
)
1287 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1288 struct vb2_queue
*q
= &fimc
->vb_queue
;
1289 struct video_device
*vfd
= &fimc
->ve
.vdev
;
1292 memset(vfd
, 0, sizeof(*vfd
));
1293 atomic_set(&fimc
->out_path
, FIMC_IO_DMA
);
1295 snprintf(vfd
->name
, sizeof(vfd
->name
), "fimc-lite.%d.capture",
1298 vfd
->fops
= &fimc_lite_fops
;
1299 vfd
->ioctl_ops
= &fimc_lite_ioctl_ops
;
1300 vfd
->v4l2_dev
= sd
->v4l2_dev
;
1302 vfd
->release
= video_device_release_empty
;
1304 fimc
->reqbufs_count
= 0;
1306 INIT_LIST_HEAD(&fimc
->pending_buf_q
);
1307 INIT_LIST_HEAD(&fimc
->active_buf_q
);
1309 memset(q
, 0, sizeof(*q
));
1310 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
1311 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
1312 q
->ops
= &fimc_lite_qops
;
1313 q
->mem_ops
= &vb2_dma_contig_memops
;
1314 q
->buf_struct_size
= sizeof(struct flite_buffer
);
1316 q
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1317 q
->lock
= &fimc
->lock
;
1319 ret
= vb2_queue_init(q
);
1323 fimc
->vd_pad
.flags
= MEDIA_PAD_FL_SINK
;
1324 ret
= media_entity_init(&vfd
->entity
, 1, &fimc
->vd_pad
, 0);
1328 video_set_drvdata(vfd
, fimc
);
1329 fimc
->ve
.pipe
= v4l2_get_subdev_hostdata(sd
);
1331 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, -1);
1333 media_entity_cleanup(&vfd
->entity
);
1334 fimc
->ve
.pipe
= NULL
;
1338 v4l2_info(sd
->v4l2_dev
, "Registered %s as /dev/%s\n",
1339 vfd
->name
, video_device_node_name(vfd
));
1343 static void fimc_lite_subdev_unregistered(struct v4l2_subdev
*sd
)
1345 struct fimc_lite
*fimc
= v4l2_get_subdevdata(sd
);
1350 mutex_lock(&fimc
->lock
);
1352 if (video_is_registered(&fimc
->ve
.vdev
)) {
1353 video_unregister_device(&fimc
->ve
.vdev
);
1354 media_entity_cleanup(&fimc
->ve
.vdev
.entity
);
1355 fimc
->ve
.pipe
= NULL
;
1358 mutex_unlock(&fimc
->lock
);
1361 static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops
= {
1362 .registered
= fimc_lite_subdev_registered
,
1363 .unregistered
= fimc_lite_subdev_unregistered
,
1366 static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops
= {
1367 .enum_mbus_code
= fimc_lite_subdev_enum_mbus_code
,
1368 .get_selection
= fimc_lite_subdev_get_selection
,
1369 .set_selection
= fimc_lite_subdev_set_selection
,
1370 .get_fmt
= fimc_lite_subdev_get_fmt
,
1371 .set_fmt
= fimc_lite_subdev_set_fmt
,
1374 static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops
= {
1375 .s_stream
= fimc_lite_subdev_s_stream
,
1378 static const struct v4l2_subdev_core_ops fimc_lite_core_ops
= {
1379 .log_status
= fimc_lite_log_status
,
1382 static struct v4l2_subdev_ops fimc_lite_subdev_ops
= {
1383 .core
= &fimc_lite_core_ops
,
1384 .video
= &fimc_lite_subdev_video_ops
,
1385 .pad
= &fimc_lite_subdev_pad_ops
,
1388 static int fimc_lite_s_ctrl(struct v4l2_ctrl
*ctrl
)
1390 struct fimc_lite
*fimc
= container_of(ctrl
->handler
, struct fimc_lite
,
1392 set_bit(ST_FLITE_CONFIG
, &fimc
->state
);
1396 static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops
= {
1397 .s_ctrl
= fimc_lite_s_ctrl
,
1400 static const struct v4l2_ctrl_config fimc_lite_ctrl
= {
1401 .ops
= &fimc_lite_ctrl_ops
,
1402 .id
= V4L2_CTRL_CLASS_USER
| 0x1001,
1403 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1404 .name
= "Test Pattern 640x480",
1408 static void fimc_lite_set_default_config(struct fimc_lite
*fimc
)
1410 struct flite_frame
*sink
= &fimc
->inp_frame
;
1411 struct flite_frame
*source
= &fimc
->out_frame
;
1413 sink
->fmt
= &fimc_lite_formats
[0];
1414 sink
->f_width
= FLITE_DEFAULT_WIDTH
;
1415 sink
->f_height
= FLITE_DEFAULT_HEIGHT
;
1417 sink
->rect
.width
= FLITE_DEFAULT_WIDTH
;
1418 sink
->rect
.height
= FLITE_DEFAULT_HEIGHT
;
1419 sink
->rect
.left
= 0;
1425 static int fimc_lite_create_capture_subdev(struct fimc_lite
*fimc
)
1427 struct v4l2_ctrl_handler
*handler
= &fimc
->ctrl_handler
;
1428 struct v4l2_subdev
*sd
= &fimc
->subdev
;
1431 v4l2_subdev_init(sd
, &fimc_lite_subdev_ops
);
1432 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1433 snprintf(sd
->name
, sizeof(sd
->name
), "FIMC-LITE.%d", fimc
->index
);
1435 fimc
->subdev_pads
[FLITE_SD_PAD_SINK
].flags
= MEDIA_PAD_FL_SINK
;
1436 fimc
->subdev_pads
[FLITE_SD_PAD_SOURCE_DMA
].flags
= MEDIA_PAD_FL_SOURCE
;
1437 fimc
->subdev_pads
[FLITE_SD_PAD_SOURCE_ISP
].flags
= MEDIA_PAD_FL_SOURCE
;
1438 ret
= media_entity_init(&sd
->entity
, FLITE_SD_PADS_NUM
,
1439 fimc
->subdev_pads
, 0);
1443 v4l2_ctrl_handler_init(handler
, 1);
1444 fimc
->test_pattern
= v4l2_ctrl_new_custom(handler
, &fimc_lite_ctrl
,
1446 if (handler
->error
) {
1447 media_entity_cleanup(&sd
->entity
);
1448 return handler
->error
;
1451 sd
->ctrl_handler
= handler
;
1452 sd
->internal_ops
= &fimc_lite_subdev_internal_ops
;
1453 sd
->entity
.ops
= &fimc_lite_subdev_media_ops
;
1454 sd
->owner
= THIS_MODULE
;
1455 v4l2_set_subdevdata(sd
, fimc
);
1460 static void fimc_lite_unregister_capture_subdev(struct fimc_lite
*fimc
)
1462 struct v4l2_subdev
*sd
= &fimc
->subdev
;
1464 v4l2_device_unregister_subdev(sd
);
1465 media_entity_cleanup(&sd
->entity
);
1466 v4l2_ctrl_handler_free(&fimc
->ctrl_handler
);
1467 v4l2_set_subdevdata(sd
, NULL
);
1470 static void fimc_lite_clk_put(struct fimc_lite
*fimc
)
1472 if (IS_ERR(fimc
->clock
))
1475 clk_unprepare(fimc
->clock
);
1476 clk_put(fimc
->clock
);
1477 fimc
->clock
= ERR_PTR(-EINVAL
);
1480 static int fimc_lite_clk_get(struct fimc_lite
*fimc
)
1484 fimc
->clock
= clk_get(&fimc
->pdev
->dev
, FLITE_CLK_NAME
);
1485 if (IS_ERR(fimc
->clock
))
1486 return PTR_ERR(fimc
->clock
);
1488 ret
= clk_prepare(fimc
->clock
);
1490 clk_put(fimc
->clock
);
1491 fimc
->clock
= ERR_PTR(-EINVAL
);
1496 static const struct of_device_id flite_of_match
[];
1498 static int fimc_lite_probe(struct platform_device
*pdev
)
1500 struct flite_drvdata
*drv_data
= NULL
;
1501 struct device
*dev
= &pdev
->dev
;
1502 const struct of_device_id
*of_id
;
1503 struct fimc_lite
*fimc
;
1504 struct resource
*res
;
1510 fimc
= devm_kzalloc(dev
, sizeof(*fimc
), GFP_KERNEL
);
1514 of_id
= of_match_node(flite_of_match
, dev
->of_node
);
1516 drv_data
= (struct flite_drvdata
*)of_id
->data
;
1517 fimc
->index
= of_alias_get_id(dev
->of_node
, "fimc-lite");
1519 if (!drv_data
|| fimc
->index
>= drv_data
->num_instances
||
1521 dev_err(dev
, "Wrong %s node alias\n",
1522 dev
->of_node
->full_name
);
1526 fimc
->dd
= drv_data
;
1529 init_waitqueue_head(&fimc
->irq_queue
);
1530 spin_lock_init(&fimc
->slock
);
1531 mutex_init(&fimc
->lock
);
1533 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1534 fimc
->regs
= devm_ioremap_resource(dev
, res
);
1535 if (IS_ERR(fimc
->regs
))
1536 return PTR_ERR(fimc
->regs
);
1538 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1540 dev_err(dev
, "Failed to get IRQ resource\n");
1544 ret
= fimc_lite_clk_get(fimc
);
1548 ret
= devm_request_irq(dev
, res
->start
, flite_irq_handler
,
1549 0, dev_name(dev
), fimc
);
1551 dev_err(dev
, "Failed to install irq (%d)\n", ret
);
1555 /* The video node will be created within the subdev's registered() op */
1556 ret
= fimc_lite_create_capture_subdev(fimc
);
1560 platform_set_drvdata(pdev
, fimc
);
1561 pm_runtime_enable(dev
);
1563 if (!pm_runtime_enabled(dev
)) {
1564 ret
= clk_enable(fimc
->clock
);
1569 fimc
->alloc_ctx
= vb2_dma_contig_init_ctx(dev
);
1570 if (IS_ERR(fimc
->alloc_ctx
)) {
1571 ret
= PTR_ERR(fimc
->alloc_ctx
);
1575 fimc_lite_set_default_config(fimc
);
1577 dev_dbg(dev
, "FIMC-LITE.%d registered successfully\n",
1582 if (!pm_runtime_enabled(dev
))
1583 clk_disable(fimc
->clock
);
1585 fimc_lite_unregister_capture_subdev(fimc
);
1587 fimc_lite_clk_put(fimc
);
1591 #ifdef CONFIG_PM_RUNTIME
1592 static int fimc_lite_runtime_resume(struct device
*dev
)
1594 struct fimc_lite
*fimc
= dev_get_drvdata(dev
);
1596 clk_enable(fimc
->clock
);
1600 static int fimc_lite_runtime_suspend(struct device
*dev
)
1602 struct fimc_lite
*fimc
= dev_get_drvdata(dev
);
1604 clk_disable(fimc
->clock
);
1609 #ifdef CONFIG_PM_SLEEP
1610 static int fimc_lite_resume(struct device
*dev
)
1612 struct fimc_lite
*fimc
= dev_get_drvdata(dev
);
1613 struct flite_buffer
*buf
;
1614 unsigned long flags
;
1617 spin_lock_irqsave(&fimc
->slock
, flags
);
1618 if (!test_and_clear_bit(ST_LPM
, &fimc
->state
) ||
1619 !test_bit(ST_FLITE_IN_USE
, &fimc
->state
)) {
1620 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1623 flite_hw_reset(fimc
);
1624 spin_unlock_irqrestore(&fimc
->slock
, flags
);
1626 if (!test_and_clear_bit(ST_FLITE_SUSPENDED
, &fimc
->state
))
1629 INIT_LIST_HEAD(&fimc
->active_buf_q
);
1630 fimc_pipeline_call(&fimc
->ve
, open
,
1631 &fimc
->ve
.vdev
.entity
, false);
1632 fimc_lite_hw_init(fimc
, atomic_read(&fimc
->out_path
) == FIMC_IO_ISP
);
1633 clear_bit(ST_FLITE_SUSPENDED
, &fimc
->state
);
1635 for (i
= 0; i
< fimc
->reqbufs_count
; i
++) {
1636 if (list_empty(&fimc
->pending_buf_q
))
1638 buf
= fimc_lite_pending_queue_pop(fimc
);
1639 buffer_queue(&buf
->vb
);
1644 static int fimc_lite_suspend(struct device
*dev
)
1646 struct fimc_lite
*fimc
= dev_get_drvdata(dev
);
1647 bool suspend
= test_bit(ST_FLITE_IN_USE
, &fimc
->state
);
1650 if (test_and_set_bit(ST_LPM
, &fimc
->state
))
1653 ret
= fimc_lite_stop_capture(fimc
, suspend
);
1654 if (ret
< 0 || !fimc_lite_active(fimc
))
1657 return fimc_pipeline_call(&fimc
->ve
, close
);
1659 #endif /* CONFIG_PM_SLEEP */
1661 static int fimc_lite_remove(struct platform_device
*pdev
)
1663 struct fimc_lite
*fimc
= platform_get_drvdata(pdev
);
1664 struct device
*dev
= &pdev
->dev
;
1666 pm_runtime_disable(dev
);
1667 pm_runtime_set_suspended(dev
);
1668 fimc_lite_unregister_capture_subdev(fimc
);
1669 vb2_dma_contig_cleanup_ctx(fimc
->alloc_ctx
);
1670 fimc_lite_clk_put(fimc
);
1672 dev_info(dev
, "Driver unloaded\n");
1676 static const struct dev_pm_ops fimc_lite_pm_ops
= {
1677 SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend
, fimc_lite_resume
)
1678 SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend
, fimc_lite_runtime_resume
,
1682 /* EXYNOS4212, EXYNOS4412 */
1683 static struct flite_drvdata fimc_lite_drvdata_exynos4
= {
1686 .out_width_align
= 8,
1687 .win_hor_offs_align
= 2,
1688 .out_hor_offs_align
= 8,
1694 static struct flite_drvdata fimc_lite_drvdata_exynos5
= {
1697 .out_width_align
= 8,
1698 .win_hor_offs_align
= 2,
1699 .out_hor_offs_align
= 8,
1704 static const struct of_device_id flite_of_match
[] = {
1706 .compatible
= "samsung,exynos4212-fimc-lite",
1707 .data
= &fimc_lite_drvdata_exynos4
,
1710 .compatible
= "samsung,exynos5250-fimc-lite",
1711 .data
= &fimc_lite_drvdata_exynos5
,
1715 MODULE_DEVICE_TABLE(of
, flite_of_match
);
1717 static struct platform_driver fimc_lite_driver
= {
1718 .probe
= fimc_lite_probe
,
1719 .remove
= fimc_lite_remove
,
1721 .of_match_table
= flite_of_match
,
1722 .name
= FIMC_LITE_DRV_NAME
,
1723 .owner
= THIS_MODULE
,
1724 .pm
= &fimc_lite_pm_ops
,
1727 module_platform_driver(fimc_lite_driver
);
1728 MODULE_LICENSE("GPL");
1729 MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME
);