1 // SPDX-License-Identifier: GPL-2.0-only
3 * Samsung EXYNOS FIMC-LITE (camera host interface) driver
5 * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
8 #define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
10 #include <linux/bug.h>
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/slab.h>
23 #include <linux/videodev2.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-mem2mem.h>
28 #include <media/v4l2-rect.h>
29 #include <media/videobuf2-v4l2.h>
30 #include <media/videobuf2-dma-contig.h>
31 #include <media/drv-intf/exynos-fimc.h>
34 #include "fimc-core.h"
35 #include "fimc-lite.h"
36 #include "fimc-lite-reg.h"
39 module_param(debug, int, 0644);
41 static const struct fimc_fmt fimc_lite_formats[] = {
43 .fourcc = V4L2_PIX_FMT_YUYV,
44 .colorspace = V4L2_COLORSPACE_JPEG,
46 .color = FIMC_FMT_YCBYCR422,
48 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
49 .flags = FMT_FLAGS_YUV,
51 .fourcc = V4L2_PIX_FMT_UYVY,
52 .colorspace = V4L2_COLORSPACE_JPEG,
54 .color = FIMC_FMT_CBYCRY422,
56 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
57 .flags = FMT_FLAGS_YUV,
59 .fourcc = V4L2_PIX_FMT_VYUY,
60 .colorspace = V4L2_COLORSPACE_JPEG,
62 .color = FIMC_FMT_CRYCBY422,
64 .mbus_code = MEDIA_BUS_FMT_VYUY8_2X8,
65 .flags = FMT_FLAGS_YUV,
67 .fourcc = V4L2_PIX_FMT_YVYU,
68 .colorspace = V4L2_COLORSPACE_JPEG,
70 .color = FIMC_FMT_YCRYCB422,
72 .mbus_code = MEDIA_BUS_FMT_YVYU8_2X8,
73 .flags = FMT_FLAGS_YUV,
75 .fourcc = V4L2_PIX_FMT_SGRBG8,
76 .colorspace = V4L2_COLORSPACE_SRGB,
78 .color = FIMC_FMT_RAW8,
80 .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8,
81 .flags = FMT_FLAGS_RAW_BAYER,
83 .fourcc = V4L2_PIX_FMT_SGRBG10,
84 .colorspace = V4L2_COLORSPACE_SRGB,
86 .color = FIMC_FMT_RAW10,
88 .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
89 .flags = FMT_FLAGS_RAW_BAYER,
91 .fourcc = V4L2_PIX_FMT_SGRBG12,
92 .colorspace = V4L2_COLORSPACE_SRGB,
94 .color = FIMC_FMT_RAW12,
96 .mbus_code = MEDIA_BUS_FMT_SGRBG12_1X12,
97 .flags = FMT_FLAGS_RAW_BAYER,
102 * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
103 * @pixelformat: fourcc to match, ignored if null
104 * @mbus_code: media bus code to match, ignored if null
105 * @mask: the color format flags to match
106 * @index: index to the fimc_lite_formats array, ignored if negative
108 static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat,
109 const u32 *mbus_code, unsigned int mask, int index)
111 const struct fimc_fmt *fmt, *def_fmt = NULL;
115 if (index >= (int)ARRAY_SIZE(fimc_lite_formats))
118 for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) {
119 fmt = &fimc_lite_formats[i];
120 if (mask && !(fmt->flags & mask))
122 if (pixelformat && fmt->fourcc == *pixelformat)
124 if (mbus_code && fmt->mbus_code == *mbus_code)
133 static int fimc_lite_hw_init(struct fimc_lite *fimc, bool isp_output)
135 struct fimc_source_info *si;
138 if (fimc->sensor == NULL)
141 if (fimc->inp_frame.fmt == NULL || fimc->out_frame.fmt == NULL)
144 /* Get sensor configuration data from the sensor subdev */
145 si = v4l2_get_subdev_hostdata(fimc->sensor);
149 spin_lock_irqsave(&fimc->slock, flags);
151 flite_hw_set_camera_bus(fimc, si);
152 flite_hw_set_source_format(fimc, &fimc->inp_frame);
153 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
154 flite_hw_set_dma_buf_mask(fimc, 0);
155 flite_hw_set_output_dma(fimc, &fimc->out_frame, !isp_output);
156 flite_hw_set_interrupt_mask(fimc);
157 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
160 flite_hw_dump_regs(fimc, __func__);
162 spin_unlock_irqrestore(&fimc->slock, flags);
167 * Reinitialize the driver so it is ready to start the streaming again.
168 * Set fimc->state to indicate stream off and the hardware shut down state.
169 * If not suspending (@suspend is false), return any buffers to videobuf2.
170 * Otherwise put any owned buffers onto the pending buffers queue, so they
171 * can be re-spun when the device is being resumed. Also perform FIMC
172 * software reset and disable streaming on the whole pipeline if required.
174 static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend)
176 struct flite_buffer *buf;
180 spin_lock_irqsave(&fimc->slock, flags);
181 streaming = fimc->state & (1 << ST_SENSOR_STREAM);
183 fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF |
184 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM);
186 fimc->state |= (1 << ST_FLITE_SUSPENDED);
188 fimc->state &= ~(1 << ST_FLITE_PENDING |
189 1 << ST_FLITE_SUSPENDED);
191 /* Release unused buffers */
192 while (!suspend && !list_empty(&fimc->pending_buf_q)) {
193 buf = fimc_lite_pending_queue_pop(fimc);
194 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
196 /* If suspending put unused buffers onto pending queue */
197 while (!list_empty(&fimc->active_buf_q)) {
198 buf = fimc_lite_active_queue_pop(fimc);
200 fimc_lite_pending_queue_add(fimc, buf);
202 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
205 spin_unlock_irqrestore(&fimc->slock, flags);
207 flite_hw_reset(fimc);
212 return fimc_pipeline_call(&fimc->ve, set_stream, 0);
215 static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend)
219 if (!fimc_lite_active(fimc))
222 spin_lock_irqsave(&fimc->slock, flags);
223 set_bit(ST_FLITE_OFF, &fimc->state);
224 flite_hw_capture_stop(fimc);
225 spin_unlock_irqrestore(&fimc->slock, flags);
227 wait_event_timeout(fimc->irq_queue,
228 !test_bit(ST_FLITE_OFF, &fimc->state),
229 (2*HZ/10)); /* 200 ms */
231 return fimc_lite_reinit(fimc, suspend);
234 /* Must be called with fimc.slock spinlock held. */
235 static void fimc_lite_config_update(struct fimc_lite *fimc)
237 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
238 flite_hw_set_dma_window(fimc, &fimc->out_frame);
239 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
240 clear_bit(ST_FLITE_CONFIG, &fimc->state);
243 static irqreturn_t flite_irq_handler(int irq, void *priv)
245 struct fimc_lite *fimc = priv;
246 struct flite_buffer *vbuf;
250 spin_lock_irqsave(&fimc->slock, flags);
252 intsrc = flite_hw_get_interrupt_source(fimc);
253 flite_hw_clear_pending_irq(fimc);
255 if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) {
256 wake_up(&fimc->irq_queue);
260 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) {
261 clear_bit(ST_FLITE_RUN, &fimc->state);
262 fimc->events.data_overflow++;
265 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) {
266 flite_hw_clear_last_capture_end(fimc);
267 clear_bit(ST_FLITE_STREAM, &fimc->state);
268 wake_up(&fimc->irq_queue);
271 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA)
274 if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) &&
275 test_bit(ST_FLITE_RUN, &fimc->state) &&
276 !list_empty(&fimc->pending_buf_q)) {
277 vbuf = fimc_lite_pending_queue_pop(fimc);
278 flite_hw_set_dma_buffer(fimc, vbuf);
279 fimc_lite_active_queue_add(fimc, vbuf);
282 if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMEND) &&
283 test_bit(ST_FLITE_RUN, &fimc->state) &&
284 !list_empty(&fimc->active_buf_q)) {
285 vbuf = fimc_lite_active_queue_pop(fimc);
286 vbuf->vb.vb2_buf.timestamp = ktime_get_ns();
287 vbuf->vb.sequence = fimc->frame_count++;
288 flite_hw_mask_dma_buffer(fimc, vbuf->index);
289 vb2_buffer_done(&vbuf->vb.vb2_buf, VB2_BUF_STATE_DONE);
292 if (test_bit(ST_FLITE_CONFIG, &fimc->state))
293 fimc_lite_config_update(fimc);
295 if (list_empty(&fimc->pending_buf_q)) {
296 flite_hw_capture_stop(fimc);
297 clear_bit(ST_FLITE_STREAM, &fimc->state);
300 set_bit(ST_FLITE_RUN, &fimc->state);
301 spin_unlock_irqrestore(&fimc->slock, flags);
305 static int start_streaming(struct vb2_queue *q, unsigned int count)
307 struct fimc_lite *fimc = q->drv_priv;
311 spin_lock_irqsave(&fimc->slock, flags);
314 fimc->frame_count = 0;
316 spin_unlock_irqrestore(&fimc->slock, flags);
318 ret = fimc_lite_hw_init(fimc, false);
320 fimc_lite_reinit(fimc, false);
324 set_bit(ST_FLITE_PENDING, &fimc->state);
326 if (!list_empty(&fimc->active_buf_q) &&
327 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
328 flite_hw_capture_start(fimc);
330 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
331 fimc_pipeline_call(&fimc->ve, set_stream, 1);
334 flite_hw_dump_regs(fimc, __func__);
339 static void stop_streaming(struct vb2_queue *q)
341 struct fimc_lite *fimc = q->drv_priv;
343 if (!fimc_lite_active(fimc))
346 fimc_lite_stop_capture(fimc, false);
349 static int queue_setup(struct vb2_queue *vq,
350 unsigned int *num_buffers, unsigned int *num_planes,
351 unsigned int sizes[], struct device *alloc_devs[])
353 struct fimc_lite *fimc = vq->drv_priv;
354 struct flite_frame *frame = &fimc->out_frame;
355 const struct fimc_fmt *fmt = frame->fmt;
356 unsigned long wh = frame->f_width * frame->f_height;
363 if (*num_planes != fmt->memplanes)
365 for (i = 0; i < *num_planes; i++)
366 if (sizes[i] < (wh * fmt->depth[i]) / 8)
371 *num_planes = fmt->memplanes;
373 for (i = 0; i < fmt->memplanes; i++)
374 sizes[i] = (wh * fmt->depth[i]) / 8;
379 static int buffer_prepare(struct vb2_buffer *vb)
381 struct vb2_queue *vq = vb->vb2_queue;
382 struct fimc_lite *fimc = vq->drv_priv;
385 if (fimc->out_frame.fmt == NULL)
388 for (i = 0; i < fimc->out_frame.fmt->memplanes; i++) {
389 unsigned long size = fimc->payload[i];
391 if (vb2_plane_size(vb, i) < size) {
392 v4l2_err(&fimc->ve.vdev,
393 "User buffer too small (%ld < %ld)\n",
394 vb2_plane_size(vb, i), size);
397 vb2_set_plane_payload(vb, i, size);
403 static void buffer_queue(struct vb2_buffer *vb)
405 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
406 struct flite_buffer *buf
407 = container_of(vbuf, struct flite_buffer, vb);
408 struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue);
411 spin_lock_irqsave(&fimc->slock, flags);
412 buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0);
414 buf->index = fimc->buf_index++;
415 if (fimc->buf_index >= fimc->reqbufs_count)
418 if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) &&
419 !test_bit(ST_FLITE_STREAM, &fimc->state) &&
420 list_empty(&fimc->active_buf_q)) {
421 flite_hw_set_dma_buffer(fimc, buf);
422 fimc_lite_active_queue_add(fimc, buf);
424 fimc_lite_pending_queue_add(fimc, buf);
427 if (vb2_is_streaming(&fimc->vb_queue) &&
428 !list_empty(&fimc->pending_buf_q) &&
429 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
430 flite_hw_capture_start(fimc);
431 spin_unlock_irqrestore(&fimc->slock, flags);
433 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
434 fimc_pipeline_call(&fimc->ve, set_stream, 1);
437 spin_unlock_irqrestore(&fimc->slock, flags);
440 static const struct vb2_ops fimc_lite_qops = {
441 .queue_setup = queue_setup,
442 .buf_prepare = buffer_prepare,
443 .buf_queue = buffer_queue,
444 .wait_prepare = vb2_ops_wait_prepare,
445 .wait_finish = vb2_ops_wait_finish,
446 .start_streaming = start_streaming,
447 .stop_streaming = stop_streaming,
450 static void fimc_lite_clear_event_counters(struct fimc_lite *fimc)
454 spin_lock_irqsave(&fimc->slock, flags);
455 memset(&fimc->events, 0, sizeof(fimc->events));
456 spin_unlock_irqrestore(&fimc->slock, flags);
459 static int fimc_lite_open(struct file *file)
461 struct fimc_lite *fimc = video_drvdata(file);
462 struct media_entity *me = &fimc->ve.vdev.entity;
465 mutex_lock(&fimc->lock);
466 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA) {
471 set_bit(ST_FLITE_IN_USE, &fimc->state);
472 ret = pm_runtime_resume_and_get(&fimc->pdev->dev);
476 ret = v4l2_fh_open(file);
480 if (!v4l2_fh_is_singular_file(file) ||
481 atomic_read(&fimc->out_path) != FIMC_IO_DMA)
484 mutex_lock(&me->graph_obj.mdev->graph_mutex);
486 ret = fimc_pipeline_call(&fimc->ve, open, me, true);
488 /* Mark video pipeline ending at this video node as in use. */
492 mutex_unlock(&me->graph_obj.mdev->graph_mutex);
495 fimc_lite_clear_event_counters(fimc);
499 v4l2_fh_release(file);
501 pm_runtime_put_sync(&fimc->pdev->dev);
503 clear_bit(ST_FLITE_IN_USE, &fimc->state);
505 mutex_unlock(&fimc->lock);
509 static int fimc_lite_release(struct file *file)
511 struct fimc_lite *fimc = video_drvdata(file);
512 struct media_entity *entity = &fimc->ve.vdev.entity;
514 mutex_lock(&fimc->lock);
516 if (v4l2_fh_is_singular_file(file) &&
517 atomic_read(&fimc->out_path) == FIMC_IO_DMA) {
518 if (fimc->streaming) {
519 media_pipeline_stop(entity);
520 fimc->streaming = false;
522 fimc_lite_stop_capture(fimc, false);
523 fimc_pipeline_call(&fimc->ve, close);
524 clear_bit(ST_FLITE_IN_USE, &fimc->state);
526 mutex_lock(&entity->graph_obj.mdev->graph_mutex);
528 mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
531 _vb2_fop_release(file, NULL);
532 pm_runtime_put(&fimc->pdev->dev);
533 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
535 mutex_unlock(&fimc->lock);
539 static const struct v4l2_file_operations fimc_lite_fops = {
540 .owner = THIS_MODULE,
541 .open = fimc_lite_open,
542 .release = fimc_lite_release,
543 .poll = vb2_fop_poll,
544 .unlocked_ioctl = video_ioctl2,
545 .mmap = vb2_fop_mmap,
549 * Format and crop negotiation helpers
552 static const struct fimc_fmt *fimc_lite_subdev_try_fmt(struct fimc_lite *fimc,
553 struct v4l2_subdev_pad_config *cfg,
554 struct v4l2_subdev_format *format)
556 struct flite_drvdata *dd = fimc->dd;
557 struct v4l2_mbus_framefmt *mf = &format->format;
558 const struct fimc_fmt *fmt = NULL;
560 if (format->pad == FLITE_SD_PAD_SINK) {
561 v4l_bound_align_image(&mf->width, 8, dd->max_width,
562 ffs(dd->out_width_align) - 1,
563 &mf->height, 0, dd->max_height, 0, 0);
565 fmt = fimc_lite_find_format(NULL, &mf->code, 0, 0);
569 mf->colorspace = fmt->colorspace;
570 mf->code = fmt->mbus_code;
572 struct flite_frame *sink = &fimc->inp_frame;
573 struct v4l2_mbus_framefmt *sink_fmt;
574 struct v4l2_rect *rect;
576 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
577 sink_fmt = v4l2_subdev_get_try_format(&fimc->subdev, cfg,
580 mf->code = sink_fmt->code;
581 mf->colorspace = sink_fmt->colorspace;
583 rect = v4l2_subdev_get_try_crop(&fimc->subdev, cfg,
586 mf->code = sink->fmt->mbus_code;
587 mf->colorspace = sink->fmt->colorspace;
591 /* Allow changing format only on sink pad */
592 mf->width = rect->width;
593 mf->height = rect->height;
596 mf->field = V4L2_FIELD_NONE;
598 v4l2_dbg(1, debug, &fimc->subdev, "code: %#x (%d), %dx%d\n",
599 mf->code, mf->colorspace, mf->width, mf->height);
604 static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r)
606 struct flite_frame *frame = &fimc->inp_frame;
608 v4l_bound_align_image(&r->width, 0, frame->f_width, 0,
609 &r->height, 0, frame->f_height, 0, 0);
611 /* Adjust left/top if cropping rectangle got out of bounds */
612 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
613 r->left = round_down(r->left, fimc->dd->win_hor_offs_align);
614 r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height);
616 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d\n",
617 r->left, r->top, r->width, r->height,
618 frame->f_width, frame->f_height);
621 static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r)
623 struct flite_frame *frame = &fimc->out_frame;
624 struct v4l2_rect *crop_rect = &fimc->inp_frame.rect;
626 /* Scaling is not supported so we enforce compose rectangle size
627 same as size of the sink crop rectangle. */
628 r->width = crop_rect->width;
629 r->height = crop_rect->height;
631 /* Adjust left/top if the composing rectangle got out of bounds */
632 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
633 r->left = round_down(r->left, fimc->dd->out_hor_offs_align);
634 r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height);
636 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d\n",
637 r->left, r->top, r->width, r->height,
638 frame->f_width, frame->f_height);
642 * Video node ioctl operations
644 static int fimc_lite_querycap(struct file *file, void *priv,
645 struct v4l2_capability *cap)
647 struct fimc_lite *fimc = video_drvdata(file);
649 strscpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver));
650 strscpy(cap->card, FIMC_LITE_DRV_NAME, sizeof(cap->card));
651 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
652 dev_name(&fimc->pdev->dev));
656 static int fimc_lite_enum_fmt(struct file *file, void *priv,
657 struct v4l2_fmtdesc *f)
659 const struct fimc_fmt *fmt;
661 if (f->index >= ARRAY_SIZE(fimc_lite_formats))
664 fmt = &fimc_lite_formats[f->index];
665 f->pixelformat = fmt->fourcc;
670 static int fimc_lite_g_fmt_mplane(struct file *file, void *fh,
671 struct v4l2_format *f)
673 struct fimc_lite *fimc = video_drvdata(file);
674 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
675 struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0];
676 struct flite_frame *frame = &fimc->out_frame;
677 const struct fimc_fmt *fmt = frame->fmt;
679 plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8;
680 plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height;
682 pixm->num_planes = fmt->memplanes;
683 pixm->pixelformat = fmt->fourcc;
684 pixm->width = frame->f_width;
685 pixm->height = frame->f_height;
686 pixm->field = V4L2_FIELD_NONE;
687 pixm->colorspace = fmt->colorspace;
691 static int fimc_lite_try_fmt(struct fimc_lite *fimc,
692 struct v4l2_pix_format_mplane *pixm,
693 const struct fimc_fmt **ffmt)
695 u32 bpl = pixm->plane_fmt[0].bytesperline;
696 struct flite_drvdata *dd = fimc->dd;
697 const struct fimc_fmt *inp_fmt = fimc->inp_frame.fmt;
698 const struct fimc_fmt *fmt;
700 if (WARN_ON(inp_fmt == NULL))
703 * We allow some flexibility only for YUV formats. In case of raw
704 * raw Bayer the FIMC-LITE's output format must match its camera
705 * interface input format.
707 if (inp_fmt->flags & FMT_FLAGS_YUV)
708 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL,
713 if (WARN_ON(fmt == NULL))
717 v4l_bound_align_image(&pixm->width, 8, dd->max_width,
718 ffs(dd->out_width_align) - 1,
719 &pixm->height, 0, dd->max_height, 0, 0);
721 if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width))
722 pixm->plane_fmt[0].bytesperline = (pixm->width *
725 if (pixm->plane_fmt[0].sizeimage == 0)
726 pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height *
728 pixm->num_planes = fmt->memplanes;
729 pixm->pixelformat = fmt->fourcc;
730 pixm->colorspace = fmt->colorspace;
731 pixm->field = V4L2_FIELD_NONE;
735 static int fimc_lite_try_fmt_mplane(struct file *file, void *fh,
736 struct v4l2_format *f)
738 struct fimc_lite *fimc = video_drvdata(file);
739 return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL);
742 static int fimc_lite_s_fmt_mplane(struct file *file, void *priv,
743 struct v4l2_format *f)
745 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
746 struct fimc_lite *fimc = video_drvdata(file);
747 struct flite_frame *frame = &fimc->out_frame;
748 const struct fimc_fmt *fmt = NULL;
751 if (vb2_is_busy(&fimc->vb_queue))
754 ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt);
759 fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8,
760 pixm->plane_fmt[0].sizeimage);
761 frame->f_width = pixm->width;
762 frame->f_height = pixm->height;
767 static int fimc_pipeline_validate(struct fimc_lite *fimc)
769 struct v4l2_subdev *sd = &fimc->subdev;
770 struct v4l2_subdev_format sink_fmt, src_fmt;
771 struct media_pad *pad;
775 /* Retrieve format at the sink pad */
776 pad = &sd->entity.pads[0];
777 if (!(pad->flags & MEDIA_PAD_FL_SINK))
779 /* Don't call FIMC subdev operation to avoid nested locking */
780 if (sd == &fimc->subdev) {
781 struct flite_frame *ff = &fimc->out_frame;
782 sink_fmt.format.width = ff->f_width;
783 sink_fmt.format.height = ff->f_height;
784 sink_fmt.format.code = fimc->inp_frame.fmt->mbus_code;
786 sink_fmt.pad = pad->index;
787 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
788 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
790 if (ret < 0 && ret != -ENOIOCTLCMD)
793 /* Retrieve format at the source pad */
794 pad = media_entity_remote_pad(pad);
795 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
798 sd = media_entity_to_v4l2_subdev(pad->entity);
799 src_fmt.pad = pad->index;
800 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
801 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
802 if (ret < 0 && ret != -ENOIOCTLCMD)
805 if (src_fmt.format.width != sink_fmt.format.width ||
806 src_fmt.format.height != sink_fmt.format.height ||
807 src_fmt.format.code != sink_fmt.format.code)
813 static int fimc_lite_streamon(struct file *file, void *priv,
814 enum v4l2_buf_type type)
816 struct fimc_lite *fimc = video_drvdata(file);
817 struct media_entity *entity = &fimc->ve.vdev.entity;
820 if (fimc_lite_active(fimc))
823 ret = media_pipeline_start(entity, &fimc->ve.pipe->mp);
827 ret = fimc_pipeline_validate(fimc);
831 fimc->sensor = fimc_find_remote_sensor(&fimc->subdev.entity);
833 ret = vb2_ioctl_streamon(file, priv, type);
835 fimc->streaming = true;
840 media_pipeline_stop(entity);
844 static int fimc_lite_streamoff(struct file *file, void *priv,
845 enum v4l2_buf_type type)
847 struct fimc_lite *fimc = video_drvdata(file);
850 ret = vb2_ioctl_streamoff(file, priv, type);
854 media_pipeline_stop(&fimc->ve.vdev.entity);
855 fimc->streaming = false;
859 static int fimc_lite_reqbufs(struct file *file, void *priv,
860 struct v4l2_requestbuffers *reqbufs)
862 struct fimc_lite *fimc = video_drvdata(file);
865 reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count);
866 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
868 fimc->reqbufs_count = reqbufs->count;
873 static int fimc_lite_g_selection(struct file *file, void *fh,
874 struct v4l2_selection *sel)
876 struct fimc_lite *fimc = video_drvdata(file);
877 struct flite_frame *f = &fimc->out_frame;
879 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
882 switch (sel->target) {
883 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
884 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
887 sel->r.width = f->f_width;
888 sel->r.height = f->f_height;
891 case V4L2_SEL_TGT_COMPOSE:
899 static int fimc_lite_s_selection(struct file *file, void *fh,
900 struct v4l2_selection *sel)
902 struct fimc_lite *fimc = video_drvdata(file);
903 struct flite_frame *f = &fimc->out_frame;
904 struct v4l2_rect rect = sel->r;
907 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
908 sel->target != V4L2_SEL_TGT_COMPOSE)
911 fimc_lite_try_compose(fimc, &rect);
913 if ((sel->flags & V4L2_SEL_FLAG_LE) &&
914 !v4l2_rect_enclosed(&rect, &sel->r))
917 if ((sel->flags & V4L2_SEL_FLAG_GE) &&
918 !v4l2_rect_enclosed(&sel->r, &rect))
922 spin_lock_irqsave(&fimc->slock, flags);
924 set_bit(ST_FLITE_CONFIG, &fimc->state);
925 spin_unlock_irqrestore(&fimc->slock, flags);
930 static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = {
931 .vidioc_querycap = fimc_lite_querycap,
932 .vidioc_enum_fmt_vid_cap = fimc_lite_enum_fmt,
933 .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane,
934 .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane,
935 .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane,
936 .vidioc_g_selection = fimc_lite_g_selection,
937 .vidioc_s_selection = fimc_lite_s_selection,
938 .vidioc_reqbufs = fimc_lite_reqbufs,
939 .vidioc_querybuf = vb2_ioctl_querybuf,
940 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
941 .vidioc_create_bufs = vb2_ioctl_create_bufs,
942 .vidioc_qbuf = vb2_ioctl_qbuf,
943 .vidioc_dqbuf = vb2_ioctl_dqbuf,
944 .vidioc_streamon = fimc_lite_streamon,
945 .vidioc_streamoff = fimc_lite_streamoff,
948 /* Capture subdev media entity operations */
949 static int fimc_lite_link_setup(struct media_entity *entity,
950 const struct media_pad *local,
951 const struct media_pad *remote, u32 flags)
953 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
954 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
957 if (WARN_ON(fimc == NULL))
960 v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x\n",
961 __func__, remote->entity->name, local->entity->name,
962 flags, fimc->source_subdev_grp_id);
964 switch (local->index) {
965 case FLITE_SD_PAD_SINK:
966 if (flags & MEDIA_LNK_FL_ENABLED) {
967 if (fimc->source_subdev_grp_id == 0)
968 fimc->source_subdev_grp_id = sd->grp_id;
972 fimc->source_subdev_grp_id = 0;
977 case FLITE_SD_PAD_SOURCE_DMA:
978 if (!(flags & MEDIA_LNK_FL_ENABLED))
979 atomic_set(&fimc->out_path, FIMC_IO_NONE);
981 atomic_set(&fimc->out_path, FIMC_IO_DMA);
984 case FLITE_SD_PAD_SOURCE_ISP:
985 if (!(flags & MEDIA_LNK_FL_ENABLED))
986 atomic_set(&fimc->out_path, FIMC_IO_NONE);
988 atomic_set(&fimc->out_path, FIMC_IO_ISP);
992 v4l2_err(sd, "Invalid pad index\n");
1000 static const struct media_entity_operations fimc_lite_subdev_media_ops = {
1001 .link_setup = fimc_lite_link_setup,
1004 static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1005 struct v4l2_subdev_pad_config *cfg,
1006 struct v4l2_subdev_mbus_code_enum *code)
1008 const struct fimc_fmt *fmt;
1010 fmt = fimc_lite_find_format(NULL, NULL, 0, code->index);
1013 code->code = fmt->mbus_code;
1017 static struct v4l2_mbus_framefmt *__fimc_lite_subdev_get_try_fmt(
1018 struct v4l2_subdev *sd,
1019 struct v4l2_subdev_pad_config *cfg, unsigned int pad)
1021 if (pad != FLITE_SD_PAD_SINK)
1022 pad = FLITE_SD_PAD_SOURCE_DMA;
1024 return v4l2_subdev_get_try_format(sd, cfg, pad);
1027 static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd,
1028 struct v4l2_subdev_pad_config *cfg,
1029 struct v4l2_subdev_format *fmt)
1031 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1032 struct v4l2_mbus_framefmt *mf = &fmt->format;
1033 struct flite_frame *f = &fimc->inp_frame;
1035 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1036 mf = __fimc_lite_subdev_get_try_fmt(sd, cfg, fmt->pad);
1041 mutex_lock(&fimc->lock);
1042 mf->colorspace = f->fmt->colorspace;
1043 mf->code = f->fmt->mbus_code;
1045 if (fmt->pad == FLITE_SD_PAD_SINK) {
1046 /* full camera input frame size */
1047 mf->width = f->f_width;
1048 mf->height = f->f_height;
1051 mf->width = f->rect.width;
1052 mf->height = f->rect.height;
1054 mutex_unlock(&fimc->lock);
1058 static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd,
1059 struct v4l2_subdev_pad_config *cfg,
1060 struct v4l2_subdev_format *fmt)
1062 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1063 struct v4l2_mbus_framefmt *mf = &fmt->format;
1064 struct flite_frame *sink = &fimc->inp_frame;
1065 struct flite_frame *source = &fimc->out_frame;
1066 const struct fimc_fmt *ffmt;
1068 v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d\n",
1069 fmt->pad, mf->code, mf->width, mf->height);
1071 mutex_lock(&fimc->lock);
1073 if ((atomic_read(&fimc->out_path) == FIMC_IO_ISP &&
1074 sd->entity.stream_count > 0) ||
1075 (atomic_read(&fimc->out_path) == FIMC_IO_DMA &&
1076 vb2_is_busy(&fimc->vb_queue))) {
1077 mutex_unlock(&fimc->lock);
1081 ffmt = fimc_lite_subdev_try_fmt(fimc, cfg, fmt);
1083 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1084 struct v4l2_mbus_framefmt *src_fmt;
1086 mf = __fimc_lite_subdev_get_try_fmt(sd, cfg, fmt->pad);
1089 if (fmt->pad == FLITE_SD_PAD_SINK) {
1090 unsigned int pad = FLITE_SD_PAD_SOURCE_DMA;
1091 src_fmt = __fimc_lite_subdev_get_try_fmt(sd, cfg, pad);
1095 mutex_unlock(&fimc->lock);
1099 if (fmt->pad == FLITE_SD_PAD_SINK) {
1100 sink->f_width = mf->width;
1101 sink->f_height = mf->height;
1103 /* Set sink crop rectangle */
1104 sink->rect.width = mf->width;
1105 sink->rect.height = mf->height;
1106 sink->rect.left = 0;
1108 /* Reset source format and crop rectangle */
1109 source->rect = sink->rect;
1110 source->f_width = mf->width;
1111 source->f_height = mf->height;
1114 mutex_unlock(&fimc->lock);
1118 static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd,
1119 struct v4l2_subdev_pad_config *cfg,
1120 struct v4l2_subdev_selection *sel)
1122 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1123 struct flite_frame *f = &fimc->inp_frame;
1125 if ((sel->target != V4L2_SEL_TGT_CROP &&
1126 sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
1127 sel->pad != FLITE_SD_PAD_SINK)
1130 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1131 sel->r = *v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
1135 mutex_lock(&fimc->lock);
1136 if (sel->target == V4L2_SEL_TGT_CROP) {
1141 sel->r.width = f->f_width;
1142 sel->r.height = f->f_height;
1144 mutex_unlock(&fimc->lock);
1146 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1147 __func__, f->rect.left, f->rect.top, f->rect.width,
1148 f->rect.height, f->f_width, f->f_height);
1153 static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd,
1154 struct v4l2_subdev_pad_config *cfg,
1155 struct v4l2_subdev_selection *sel)
1157 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1158 struct flite_frame *f = &fimc->inp_frame;
1161 if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != FLITE_SD_PAD_SINK)
1164 mutex_lock(&fimc->lock);
1165 fimc_lite_try_crop(fimc, &sel->r);
1167 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1168 *v4l2_subdev_get_try_crop(sd, cfg, sel->pad) = sel->r;
1170 unsigned long flags;
1171 spin_lock_irqsave(&fimc->slock, flags);
1173 /* Same crop rectangle on the source pad */
1174 fimc->out_frame.rect = sel->r;
1175 set_bit(ST_FLITE_CONFIG, &fimc->state);
1176 spin_unlock_irqrestore(&fimc->slock, flags);
1178 mutex_unlock(&fimc->lock);
1180 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1181 __func__, f->rect.left, f->rect.top, f->rect.width,
1182 f->rect.height, f->f_width, f->f_height);
1187 static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on)
1189 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1190 unsigned long flags;
1194 * Find sensor subdev linked to FIMC-LITE directly or through
1195 * MIPI-CSIS. This is required for configuration where FIMC-LITE
1196 * is used as a subdev only and feeds data internally to FIMC-IS.
1197 * The pipeline links are protected through entity.stream_count
1198 * so there is no need to take the media graph mutex here.
1200 fimc->sensor = fimc_find_remote_sensor(&sd->entity);
1202 if (atomic_read(&fimc->out_path) != FIMC_IO_ISP)
1203 return -ENOIOCTLCMD;
1205 mutex_lock(&fimc->lock);
1207 flite_hw_reset(fimc);
1208 ret = fimc_lite_hw_init(fimc, true);
1210 spin_lock_irqsave(&fimc->slock, flags);
1211 flite_hw_capture_start(fimc);
1212 spin_unlock_irqrestore(&fimc->slock, flags);
1215 set_bit(ST_FLITE_OFF, &fimc->state);
1217 spin_lock_irqsave(&fimc->slock, flags);
1218 flite_hw_capture_stop(fimc);
1219 spin_unlock_irqrestore(&fimc->slock, flags);
1221 ret = wait_event_timeout(fimc->irq_queue,
1222 !test_bit(ST_FLITE_OFF, &fimc->state),
1223 msecs_to_jiffies(200));
1225 v4l2_err(sd, "s_stream(0) timeout\n");
1226 clear_bit(ST_FLITE_RUN, &fimc->state);
1229 mutex_unlock(&fimc->lock);
1233 static int fimc_lite_log_status(struct v4l2_subdev *sd)
1235 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1237 flite_hw_dump_regs(fimc, __func__);
1241 static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
1243 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1244 struct vb2_queue *q = &fimc->vb_queue;
1245 struct video_device *vfd = &fimc->ve.vdev;
1248 memset(vfd, 0, sizeof(*vfd));
1249 atomic_set(&fimc->out_path, FIMC_IO_DMA);
1251 snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture",
1254 vfd->fops = &fimc_lite_fops;
1255 vfd->ioctl_ops = &fimc_lite_ioctl_ops;
1256 vfd->v4l2_dev = sd->v4l2_dev;
1258 vfd->release = video_device_release_empty;
1260 vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING;
1261 fimc->reqbufs_count = 0;
1263 INIT_LIST_HEAD(&fimc->pending_buf_q);
1264 INIT_LIST_HEAD(&fimc->active_buf_q);
1266 memset(q, 0, sizeof(*q));
1267 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1268 q->io_modes = VB2_MMAP | VB2_USERPTR;
1269 q->ops = &fimc_lite_qops;
1270 q->mem_ops = &vb2_dma_contig_memops;
1271 q->buf_struct_size = sizeof(struct flite_buffer);
1273 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1274 q->lock = &fimc->lock;
1275 q->dev = &fimc->pdev->dev;
1277 ret = vb2_queue_init(q);
1281 fimc->vd_pad.flags = MEDIA_PAD_FL_SINK;
1282 ret = media_entity_pads_init(&vfd->entity, 1, &fimc->vd_pad);
1286 video_set_drvdata(vfd, fimc);
1287 fimc->ve.pipe = v4l2_get_subdev_hostdata(sd);
1289 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
1291 media_entity_cleanup(&vfd->entity);
1292 fimc->ve.pipe = NULL;
1296 v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n",
1297 vfd->name, video_device_node_name(vfd));
1301 static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd)
1303 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1308 mutex_lock(&fimc->lock);
1310 if (video_is_registered(&fimc->ve.vdev)) {
1311 video_unregister_device(&fimc->ve.vdev);
1312 media_entity_cleanup(&fimc->ve.vdev.entity);
1313 fimc->ve.pipe = NULL;
1316 mutex_unlock(&fimc->lock);
1319 static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = {
1320 .registered = fimc_lite_subdev_registered,
1321 .unregistered = fimc_lite_subdev_unregistered,
1324 static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = {
1325 .enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
1326 .get_selection = fimc_lite_subdev_get_selection,
1327 .set_selection = fimc_lite_subdev_set_selection,
1328 .get_fmt = fimc_lite_subdev_get_fmt,
1329 .set_fmt = fimc_lite_subdev_set_fmt,
1332 static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = {
1333 .s_stream = fimc_lite_subdev_s_stream,
1336 static const struct v4l2_subdev_core_ops fimc_lite_core_ops = {
1337 .log_status = fimc_lite_log_status,
1340 static const struct v4l2_subdev_ops fimc_lite_subdev_ops = {
1341 .core = &fimc_lite_core_ops,
1342 .video = &fimc_lite_subdev_video_ops,
1343 .pad = &fimc_lite_subdev_pad_ops,
1346 static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl)
1348 struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite,
1350 set_bit(ST_FLITE_CONFIG, &fimc->state);
1354 static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = {
1355 .s_ctrl = fimc_lite_s_ctrl,
1358 static const struct v4l2_ctrl_config fimc_lite_ctrl = {
1359 .ops = &fimc_lite_ctrl_ops,
1360 .id = V4L2_CTRL_CLASS_USER | 0x1001,
1361 .type = V4L2_CTRL_TYPE_BOOLEAN,
1362 .name = "Test Pattern 640x480",
1366 static void fimc_lite_set_default_config(struct fimc_lite *fimc)
1368 struct flite_frame *sink = &fimc->inp_frame;
1369 struct flite_frame *source = &fimc->out_frame;
1371 sink->fmt = &fimc_lite_formats[0];
1372 sink->f_width = FLITE_DEFAULT_WIDTH;
1373 sink->f_height = FLITE_DEFAULT_HEIGHT;
1375 sink->rect.width = FLITE_DEFAULT_WIDTH;
1376 sink->rect.height = FLITE_DEFAULT_HEIGHT;
1377 sink->rect.left = 0;
1383 static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc)
1385 struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler;
1386 struct v4l2_subdev *sd = &fimc->subdev;
1389 v4l2_subdev_init(sd, &fimc_lite_subdev_ops);
1390 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1391 snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index);
1393 fimc->subdev_pads[FLITE_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1394 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_DMA].flags = MEDIA_PAD_FL_SOURCE;
1395 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_ISP].flags = MEDIA_PAD_FL_SOURCE;
1396 ret = media_entity_pads_init(&sd->entity, FLITE_SD_PADS_NUM,
1401 v4l2_ctrl_handler_init(handler, 1);
1402 fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl,
1404 if (handler->error) {
1405 media_entity_cleanup(&sd->entity);
1406 return handler->error;
1409 sd->ctrl_handler = handler;
1410 sd->internal_ops = &fimc_lite_subdev_internal_ops;
1411 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
1412 sd->entity.ops = &fimc_lite_subdev_media_ops;
1413 sd->owner = THIS_MODULE;
1414 v4l2_set_subdevdata(sd, fimc);
1419 static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc)
1421 struct v4l2_subdev *sd = &fimc->subdev;
1423 v4l2_device_unregister_subdev(sd);
1424 media_entity_cleanup(&sd->entity);
1425 v4l2_ctrl_handler_free(&fimc->ctrl_handler);
1426 v4l2_set_subdevdata(sd, NULL);
1429 static void fimc_lite_clk_put(struct fimc_lite *fimc)
1431 if (IS_ERR(fimc->clock))
1434 clk_put(fimc->clock);
1435 fimc->clock = ERR_PTR(-EINVAL);
1438 static int fimc_lite_clk_get(struct fimc_lite *fimc)
1440 fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME);
1441 return PTR_ERR_OR_ZERO(fimc->clock);
1444 static const struct of_device_id flite_of_match[];
1446 static int fimc_lite_probe(struct platform_device *pdev)
1448 struct flite_drvdata *drv_data = NULL;
1449 struct device *dev = &pdev->dev;
1450 const struct of_device_id *of_id;
1451 struct fimc_lite *fimc;
1452 struct resource *res;
1458 fimc = devm_kzalloc(dev, sizeof(*fimc), GFP_KERNEL);
1462 of_id = of_match_node(flite_of_match, dev->of_node);
1464 drv_data = (struct flite_drvdata *)of_id->data;
1465 fimc->index = of_alias_get_id(dev->of_node, "fimc-lite");
1467 if (!drv_data || fimc->index >= drv_data->num_instances ||
1469 dev_err(dev, "Wrong %pOF node alias\n", dev->of_node);
1473 fimc->dd = drv_data;
1476 init_waitqueue_head(&fimc->irq_queue);
1477 spin_lock_init(&fimc->slock);
1478 mutex_init(&fimc->lock);
1480 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1481 fimc->regs = devm_ioremap_resource(dev, res);
1482 if (IS_ERR(fimc->regs))
1483 return PTR_ERR(fimc->regs);
1485 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1487 dev_err(dev, "Failed to get IRQ resource\n");
1491 ret = fimc_lite_clk_get(fimc);
1495 ret = devm_request_irq(dev, res->start, flite_irq_handler,
1496 0, dev_name(dev), fimc);
1498 dev_err(dev, "Failed to install irq (%d)\n", ret);
1502 /* The video node will be created within the subdev's registered() op */
1503 ret = fimc_lite_create_capture_subdev(fimc);
1507 platform_set_drvdata(pdev, fimc);
1508 pm_runtime_enable(dev);
1510 if (!pm_runtime_enabled(dev)) {
1511 ret = clk_prepare_enable(fimc->clock);
1516 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
1518 fimc_lite_set_default_config(fimc);
1520 dev_dbg(dev, "FIMC-LITE.%d registered successfully\n",
1525 fimc_lite_unregister_capture_subdev(fimc);
1527 fimc_lite_clk_put(fimc);
1532 static int fimc_lite_runtime_resume(struct device *dev)
1534 struct fimc_lite *fimc = dev_get_drvdata(dev);
1536 clk_prepare_enable(fimc->clock);
1540 static int fimc_lite_runtime_suspend(struct device *dev)
1542 struct fimc_lite *fimc = dev_get_drvdata(dev);
1544 clk_disable_unprepare(fimc->clock);
1549 #ifdef CONFIG_PM_SLEEP
1550 static int fimc_lite_resume(struct device *dev)
1552 struct fimc_lite *fimc = dev_get_drvdata(dev);
1553 struct flite_buffer *buf;
1554 unsigned long flags;
1557 spin_lock_irqsave(&fimc->slock, flags);
1558 if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
1559 !test_bit(ST_FLITE_IN_USE, &fimc->state)) {
1560 spin_unlock_irqrestore(&fimc->slock, flags);
1563 flite_hw_reset(fimc);
1564 spin_unlock_irqrestore(&fimc->slock, flags);
1566 if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state))
1569 INIT_LIST_HEAD(&fimc->active_buf_q);
1570 fimc_pipeline_call(&fimc->ve, open,
1571 &fimc->ve.vdev.entity, false);
1572 fimc_lite_hw_init(fimc, atomic_read(&fimc->out_path) == FIMC_IO_ISP);
1573 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
1575 for (i = 0; i < fimc->reqbufs_count; i++) {
1576 if (list_empty(&fimc->pending_buf_q))
1578 buf = fimc_lite_pending_queue_pop(fimc);
1579 buffer_queue(&buf->vb.vb2_buf);
1584 static int fimc_lite_suspend(struct device *dev)
1586 struct fimc_lite *fimc = dev_get_drvdata(dev);
1587 bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state);
1590 if (test_and_set_bit(ST_LPM, &fimc->state))
1593 ret = fimc_lite_stop_capture(fimc, suspend);
1594 if (ret < 0 || !fimc_lite_active(fimc))
1597 return fimc_pipeline_call(&fimc->ve, close);
1599 #endif /* CONFIG_PM_SLEEP */
1601 static int fimc_lite_remove(struct platform_device *pdev)
1603 struct fimc_lite *fimc = platform_get_drvdata(pdev);
1604 struct device *dev = &pdev->dev;
1606 if (!pm_runtime_enabled(dev))
1607 clk_disable_unprepare(fimc->clock);
1609 pm_runtime_disable(dev);
1610 pm_runtime_set_suspended(dev);
1611 fimc_lite_unregister_capture_subdev(fimc);
1612 vb2_dma_contig_clear_max_seg_size(dev);
1613 fimc_lite_clk_put(fimc);
1615 dev_info(dev, "Driver unloaded\n");
1619 static const struct dev_pm_ops fimc_lite_pm_ops = {
1620 SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume)
1621 SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume,
1626 static struct flite_drvdata fimc_lite_drvdata_exynos4 = {
1629 .out_width_align = 8,
1630 .win_hor_offs_align = 2,
1631 .out_hor_offs_align = 8,
1637 static struct flite_drvdata fimc_lite_drvdata_exynos5 = {
1640 .out_width_align = 8,
1641 .win_hor_offs_align = 2,
1642 .out_hor_offs_align = 8,
1647 static const struct of_device_id flite_of_match[] = {
1649 .compatible = "samsung,exynos4212-fimc-lite",
1650 .data = &fimc_lite_drvdata_exynos4,
1653 .compatible = "samsung,exynos5250-fimc-lite",
1654 .data = &fimc_lite_drvdata_exynos5,
1658 MODULE_DEVICE_TABLE(of, flite_of_match);
1660 static struct platform_driver fimc_lite_driver = {
1661 .probe = fimc_lite_probe,
1662 .remove = fimc_lite_remove,
1664 .of_match_table = flite_of_match,
1665 .name = FIMC_LITE_DRV_NAME,
1666 .pm = &fimc_lite_pm_ops,
1669 module_platform_driver(fimc_lite_driver);
1670 MODULE_LICENSE("GPL");
1671 MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME);