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,
48 .color = FIMC_FMT_YCBYCR422,
50 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
51 .flags = FMT_FLAGS_YUV,
53 .name = "YUV 4:2:2 packed, CbYCrY",
54 .fourcc = V4L2_PIX_FMT_UYVY,
56 .color = FIMC_FMT_CBYCRY422,
58 .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
59 .flags = FMT_FLAGS_YUV,
61 .name = "YUV 4:2:2 packed, CrYCbY",
62 .fourcc = V4L2_PIX_FMT_VYUY,
64 .color = FIMC_FMT_CRYCBY422,
66 .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
67 .flags = FMT_FLAGS_YUV,
69 .name = "YUV 4:2:2 packed, YCrYCb",
70 .fourcc = V4L2_PIX_FMT_YVYU,
72 .color = FIMC_FMT_YCRYCB422,
74 .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
75 .flags = FMT_FLAGS_YUV,
77 .name = "RAW8 (GRBG)",
78 .fourcc = V4L2_PIX_FMT_SGRBG8,
80 .color = FIMC_FMT_RAW8,
82 .mbus_code = V4L2_MBUS_FMT_SGRBG8_1X8,
83 .flags = FMT_FLAGS_RAW_BAYER,
85 .name = "RAW10 (GRBG)",
86 .fourcc = V4L2_PIX_FMT_SGRBG10,
88 .color = FIMC_FMT_RAW10,
90 .mbus_code = V4L2_MBUS_FMT_SGRBG10_1X10,
91 .flags = FMT_FLAGS_RAW_BAYER,
93 .name = "RAW12 (GRBG)",
94 .fourcc = V4L2_PIX_FMT_SGRBG12,
96 .color = FIMC_FMT_RAW12,
98 .mbus_code = V4L2_MBUS_FMT_SGRBG12_1X12,
99 .flags = FMT_FLAGS_RAW_BAYER,
104 * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
105 * @pixelformat: fourcc to match, ignored if null
106 * @mbus_code: media bus code to match, ignored if null
107 * @mask: the color format flags to match
108 * @index: index to the fimc_lite_formats array, ignored if negative
110 static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat,
111 const u32 *mbus_code, unsigned int mask, int index)
113 const struct fimc_fmt *fmt, *def_fmt = NULL;
117 if (index >= (int)ARRAY_SIZE(fimc_lite_formats))
120 for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) {
121 fmt = &fimc_lite_formats[i];
122 if (mask && !(fmt->flags & mask))
124 if (pixelformat && fmt->fourcc == *pixelformat)
126 if (mbus_code && fmt->mbus_code == *mbus_code)
135 static int fimc_lite_hw_init(struct fimc_lite *fimc, bool isp_output)
137 struct fimc_source_info *si;
140 if (fimc->sensor == NULL)
143 if (fimc->inp_frame.fmt == NULL || fimc->out_frame.fmt == NULL)
146 /* Get sensor configuration data from the sensor subdev */
147 si = v4l2_get_subdev_hostdata(fimc->sensor);
151 spin_lock_irqsave(&fimc->slock, flags);
153 flite_hw_set_camera_bus(fimc, si);
154 flite_hw_set_source_format(fimc, &fimc->inp_frame);
155 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
156 flite_hw_set_output_dma(fimc, &fimc->out_frame, !isp_output);
157 flite_hw_set_interrupt_mask(fimc);
158 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
161 flite_hw_dump_regs(fimc, __func__);
163 spin_unlock_irqrestore(&fimc->slock, flags);
168 * Reinitialize the driver so it is ready to start the streaming again.
169 * Set fimc->state to indicate stream off and the hardware shut down state.
170 * If not suspending (@suspend is false), return any buffers to videobuf2.
171 * Otherwise put any owned buffers onto the pending buffers queue, so they
172 * can be re-spun when the device is being resumed. Also perform FIMC
173 * software reset and disable streaming on the whole pipeline if required.
175 static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend)
177 struct flite_buffer *buf;
181 spin_lock_irqsave(&fimc->slock, flags);
182 streaming = fimc->state & (1 << ST_SENSOR_STREAM);
184 fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF |
185 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM);
187 fimc->state |= (1 << ST_FLITE_SUSPENDED);
189 fimc->state &= ~(1 << ST_FLITE_PENDING |
190 1 << ST_FLITE_SUSPENDED);
192 /* Release unused buffers */
193 while (!suspend && !list_empty(&fimc->pending_buf_q)) {
194 buf = fimc_lite_pending_queue_pop(fimc);
195 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
197 /* If suspending put unused buffers onto pending queue */
198 while (!list_empty(&fimc->active_buf_q)) {
199 buf = fimc_lite_active_queue_pop(fimc);
201 fimc_lite_pending_queue_add(fimc, buf);
203 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
206 spin_unlock_irqrestore(&fimc->slock, flags);
208 flite_hw_reset(fimc);
213 return fimc_pipeline_call(fimc, set_stream, &fimc->pipeline, 0);
216 static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend)
220 if (!fimc_lite_active(fimc))
223 spin_lock_irqsave(&fimc->slock, flags);
224 set_bit(ST_FLITE_OFF, &fimc->state);
225 flite_hw_capture_stop(fimc);
226 spin_unlock_irqrestore(&fimc->slock, flags);
228 wait_event_timeout(fimc->irq_queue,
229 !test_bit(ST_FLITE_OFF, &fimc->state),
230 (2*HZ/10)); /* 200 ms */
232 return fimc_lite_reinit(fimc, suspend);
235 /* Must be called with fimc.slock spinlock held. */
236 static void fimc_lite_config_update(struct fimc_lite *fimc)
238 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
239 flite_hw_set_dma_window(fimc, &fimc->out_frame);
240 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
241 clear_bit(ST_FLITE_CONFIG, &fimc->state);
244 static irqreturn_t flite_irq_handler(int irq, void *priv)
246 struct fimc_lite *fimc = priv;
247 struct flite_buffer *vbuf;
253 spin_lock_irqsave(&fimc->slock, flags);
255 intsrc = flite_hw_get_interrupt_source(fimc);
256 flite_hw_clear_pending_irq(fimc);
258 if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) {
259 wake_up(&fimc->irq_queue);
263 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) {
264 clear_bit(ST_FLITE_RUN, &fimc->state);
265 fimc->events.data_overflow++;
268 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) {
269 flite_hw_clear_last_capture_end(fimc);
270 clear_bit(ST_FLITE_STREAM, &fimc->state);
271 wake_up(&fimc->irq_queue);
274 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA)
277 if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) &&
278 test_bit(ST_FLITE_RUN, &fimc->state) &&
279 !list_empty(&fimc->active_buf_q) &&
280 !list_empty(&fimc->pending_buf_q)) {
281 vbuf = fimc_lite_active_queue_pop(fimc);
283 tv = &vbuf->vb.v4l2_buf.timestamp;
284 tv->tv_sec = ts.tv_sec;
285 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
286 vbuf->vb.v4l2_buf.sequence = fimc->frame_count++;
287 vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE);
289 vbuf = fimc_lite_pending_queue_pop(fimc);
290 flite_hw_set_output_addr(fimc, vbuf->paddr);
291 fimc_lite_active_queue_add(fimc, vbuf);
294 if (test_bit(ST_FLITE_CONFIG, &fimc->state))
295 fimc_lite_config_update(fimc);
297 if (list_empty(&fimc->pending_buf_q)) {
298 flite_hw_capture_stop(fimc);
299 clear_bit(ST_FLITE_STREAM, &fimc->state);
302 set_bit(ST_FLITE_RUN, &fimc->state);
303 spin_unlock_irqrestore(&fimc->slock, flags);
307 static int start_streaming(struct vb2_queue *q, unsigned int count)
309 struct fimc_lite *fimc = q->drv_priv;
312 fimc->frame_count = 0;
314 ret = fimc_lite_hw_init(fimc, false);
316 fimc_lite_reinit(fimc, false);
320 set_bit(ST_FLITE_PENDING, &fimc->state);
322 if (!list_empty(&fimc->active_buf_q) &&
323 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
324 flite_hw_capture_start(fimc);
326 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
327 fimc_pipeline_call(fimc, set_stream,
331 flite_hw_dump_regs(fimc, __func__);
336 static int stop_streaming(struct vb2_queue *q)
338 struct fimc_lite *fimc = q->drv_priv;
340 if (!fimc_lite_active(fimc))
343 return fimc_lite_stop_capture(fimc, false);
346 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
347 unsigned int *num_buffers, unsigned int *num_planes,
348 unsigned int sizes[], void *allocators[])
350 const struct v4l2_pix_format_mplane *pixm = NULL;
351 struct fimc_lite *fimc = vq->drv_priv;
352 struct flite_frame *frame = &fimc->out_frame;
353 const struct fimc_fmt *fmt = frame->fmt;
358 pixm = &pfmt->fmt.pix_mp;
359 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, 0, -1);
360 wh = pixm->width * pixm->height;
362 wh = frame->f_width * frame->f_height;
368 *num_planes = fmt->memplanes;
370 for (i = 0; i < fmt->memplanes; i++) {
371 unsigned int size = (wh * fmt->depth[i]) / 8;
373 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
376 allocators[i] = fimc->alloc_ctx;
382 static int buffer_prepare(struct vb2_buffer *vb)
384 struct vb2_queue *vq = vb->vb2_queue;
385 struct fimc_lite *fimc = vq->drv_priv;
388 if (fimc->out_frame.fmt == NULL)
391 for (i = 0; i < fimc->out_frame.fmt->memplanes; i++) {
392 unsigned long size = fimc->payload[i];
394 if (vb2_plane_size(vb, i) < size) {
395 v4l2_err(&fimc->ve.vdev,
396 "User buffer too small (%ld < %ld)\n",
397 vb2_plane_size(vb, i), size);
400 vb2_set_plane_payload(vb, i, size);
406 static void buffer_queue(struct vb2_buffer *vb)
408 struct flite_buffer *buf
409 = container_of(vb, struct flite_buffer, vb);
410 struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue);
413 spin_lock_irqsave(&fimc->slock, flags);
414 buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0);
416 if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) &&
417 !test_bit(ST_FLITE_STREAM, &fimc->state) &&
418 list_empty(&fimc->active_buf_q)) {
419 flite_hw_set_output_addr(fimc, buf->paddr);
420 fimc_lite_active_queue_add(fimc, buf);
422 fimc_lite_pending_queue_add(fimc, buf);
425 if (vb2_is_streaming(&fimc->vb_queue) &&
426 !list_empty(&fimc->pending_buf_q) &&
427 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
428 flite_hw_capture_start(fimc);
429 spin_unlock_irqrestore(&fimc->slock, flags);
431 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
432 fimc_pipeline_call(fimc, set_stream,
436 spin_unlock_irqrestore(&fimc->slock, flags);
439 static const struct vb2_ops fimc_lite_qops = {
440 .queue_setup = queue_setup,
441 .buf_prepare = buffer_prepare,
442 .buf_queue = buffer_queue,
443 .wait_prepare = vb2_ops_wait_prepare,
444 .wait_finish = vb2_ops_wait_finish,
445 .start_streaming = start_streaming,
446 .stop_streaming = stop_streaming,
449 static void fimc_lite_clear_event_counters(struct fimc_lite *fimc)
453 spin_lock_irqsave(&fimc->slock, flags);
454 memset(&fimc->events, 0, sizeof(fimc->events));
455 spin_unlock_irqrestore(&fimc->slock, flags);
458 static int fimc_lite_open(struct file *file)
460 struct fimc_lite *fimc = video_drvdata(file);
461 struct media_entity *me = &fimc->ve.vdev.entity;
464 mutex_lock(&me->parent->graph_mutex);
466 mutex_lock(&fimc->lock);
467 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA) {
472 set_bit(ST_FLITE_IN_USE, &fimc->state);
473 ret = pm_runtime_get_sync(&fimc->pdev->dev);
477 ret = v4l2_fh_open(file);
481 if (!v4l2_fh_is_singular_file(file) ||
482 atomic_read(&fimc->out_path) != FIMC_IO_DMA)
485 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
488 fimc_lite_clear_event_counters(fimc);
493 v4l2_fh_release(file);
495 pm_runtime_put_sync(&fimc->pdev->dev);
496 clear_bit(ST_FLITE_IN_USE, &fimc->state);
498 mutex_unlock(&fimc->lock);
499 mutex_unlock(&me->parent->graph_mutex);
503 static int fimc_lite_release(struct file *file)
505 struct fimc_lite *fimc = video_drvdata(file);
507 mutex_lock(&fimc->lock);
509 if (v4l2_fh_is_singular_file(file) &&
510 atomic_read(&fimc->out_path) == FIMC_IO_DMA) {
511 if (fimc->streaming) {
512 media_entity_pipeline_stop(&fimc->ve.vdev.entity);
513 fimc->streaming = false;
515 clear_bit(ST_FLITE_IN_USE, &fimc->state);
516 fimc_lite_stop_capture(fimc, false);
517 fimc_pipeline_call(fimc, close, &fimc->pipeline);
521 vb2_fop_release(file);
522 pm_runtime_put(&fimc->pdev->dev);
523 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
525 mutex_unlock(&fimc->lock);
529 static const struct v4l2_file_operations fimc_lite_fops = {
530 .owner = THIS_MODULE,
531 .open = fimc_lite_open,
532 .release = fimc_lite_release,
533 .poll = vb2_fop_poll,
534 .unlocked_ioctl = video_ioctl2,
535 .mmap = vb2_fop_mmap,
539 * Format and crop negotiation helpers
542 static const struct fimc_fmt *fimc_lite_try_format(struct fimc_lite *fimc,
543 u32 *width, u32 *height,
544 u32 *code, u32 *fourcc, int pad)
546 struct flite_drvdata *dd = fimc->dd;
547 const struct fimc_fmt *fmt;
548 unsigned int flags = 0;
550 if (pad == FLITE_SD_PAD_SINK) {
551 v4l_bound_align_image(width, 8, dd->max_width,
552 ffs(dd->out_width_align) - 1,
553 height, 0, dd->max_height, 0, 0);
555 v4l_bound_align_image(width, 8, fimc->inp_frame.rect.width,
556 ffs(dd->out_width_align) - 1,
557 height, 0, fimc->inp_frame.rect.height,
559 flags = fimc->inp_frame.fmt->flags;
562 fmt = fimc_lite_find_format(fourcc, code, flags, 0);
567 *code = fmt->mbus_code;
569 *fourcc = fmt->fourcc;
571 v4l2_dbg(1, debug, &fimc->subdev, "code: 0x%x, %dx%d\n",
572 code ? *code : 0, *width, *height);
577 static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r)
579 struct flite_frame *frame = &fimc->inp_frame;
581 v4l_bound_align_image(&r->width, 0, frame->f_width, 0,
582 &r->height, 0, frame->f_height, 0, 0);
584 /* Adjust left/top if cropping rectangle got out of bounds */
585 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
586 r->left = round_down(r->left, fimc->dd->win_hor_offs_align);
587 r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height);
589 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d\n",
590 r->left, r->top, r->width, r->height,
591 frame->f_width, frame->f_height);
594 static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r)
596 struct flite_frame *frame = &fimc->out_frame;
597 struct v4l2_rect *crop_rect = &fimc->inp_frame.rect;
599 /* Scaling is not supported so we enforce compose rectangle size
600 same as size of the sink crop rectangle. */
601 r->width = crop_rect->width;
602 r->height = crop_rect->height;
604 /* Adjust left/top if the composing rectangle got out of bounds */
605 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
606 r->left = round_down(r->left, fimc->dd->out_hor_offs_align);
607 r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height);
609 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d\n",
610 r->left, r->top, r->width, r->height,
611 frame->f_width, frame->f_height);
615 * Video node ioctl operations
617 static int fimc_lite_querycap(struct file *file, void *priv,
618 struct v4l2_capability *cap)
620 struct fimc_lite *fimc = video_drvdata(file);
622 strlcpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver));
623 strlcpy(cap->card, FIMC_LITE_DRV_NAME, sizeof(cap->card));
624 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
625 dev_name(&fimc->pdev->dev));
627 cap->device_caps = V4L2_CAP_STREAMING;
628 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
632 static int fimc_lite_enum_fmt_mplane(struct file *file, void *priv,
633 struct v4l2_fmtdesc *f)
635 const struct fimc_fmt *fmt;
637 if (f->index >= ARRAY_SIZE(fimc_lite_formats))
640 fmt = &fimc_lite_formats[f->index];
641 strlcpy(f->description, fmt->name, sizeof(f->description));
642 f->pixelformat = fmt->fourcc;
647 static int fimc_lite_g_fmt_mplane(struct file *file, void *fh,
648 struct v4l2_format *f)
650 struct fimc_lite *fimc = video_drvdata(file);
651 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
652 struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0];
653 struct flite_frame *frame = &fimc->out_frame;
654 const struct fimc_fmt *fmt = frame->fmt;
656 plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8;
657 plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height;
659 pixm->num_planes = fmt->memplanes;
660 pixm->pixelformat = fmt->fourcc;
661 pixm->width = frame->f_width;
662 pixm->height = frame->f_height;
663 pixm->field = V4L2_FIELD_NONE;
664 pixm->colorspace = V4L2_COLORSPACE_JPEG;
668 static int fimc_lite_try_fmt(struct fimc_lite *fimc,
669 struct v4l2_pix_format_mplane *pixm,
670 const struct fimc_fmt **ffmt)
672 u32 bpl = pixm->plane_fmt[0].bytesperline;
673 struct flite_drvdata *dd = fimc->dd;
674 const struct fimc_fmt *inp_fmt = fimc->inp_frame.fmt;
675 const struct fimc_fmt *fmt;
677 if (WARN_ON(inp_fmt == NULL))
680 * We allow some flexibility only for YUV formats. In case of raw
681 * raw Bayer the FIMC-LITE's output format must match its camera
682 * interface input format.
684 if (inp_fmt->flags & FMT_FLAGS_YUV)
685 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL,
690 if (WARN_ON(fmt == NULL))
694 v4l_bound_align_image(&pixm->width, 8, dd->max_width,
695 ffs(dd->out_width_align) - 1,
696 &pixm->height, 0, dd->max_height, 0, 0);
698 if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width))
699 pixm->plane_fmt[0].bytesperline = (pixm->width *
702 if (pixm->plane_fmt[0].sizeimage == 0)
703 pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height *
705 pixm->num_planes = fmt->memplanes;
706 pixm->pixelformat = fmt->fourcc;
707 pixm->colorspace = V4L2_COLORSPACE_JPEG;
708 pixm->field = V4L2_FIELD_NONE;
712 static int fimc_lite_try_fmt_mplane(struct file *file, void *fh,
713 struct v4l2_format *f)
715 struct fimc_lite *fimc = video_drvdata(file);
716 return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL);
719 static int fimc_lite_s_fmt_mplane(struct file *file, void *priv,
720 struct v4l2_format *f)
722 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
723 struct fimc_lite *fimc = video_drvdata(file);
724 struct flite_frame *frame = &fimc->out_frame;
725 const struct fimc_fmt *fmt = NULL;
728 if (vb2_is_busy(&fimc->vb_queue))
731 ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt);
736 fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8,
737 pixm->plane_fmt[0].sizeimage);
738 frame->f_width = pixm->width;
739 frame->f_height = pixm->height;
744 static int fimc_pipeline_validate(struct fimc_lite *fimc)
746 struct v4l2_subdev *sd = &fimc->subdev;
747 struct v4l2_subdev_format sink_fmt, src_fmt;
748 struct media_pad *pad;
752 /* Retrieve format at the sink pad */
753 pad = &sd->entity.pads[0];
754 if (!(pad->flags & MEDIA_PAD_FL_SINK))
756 /* Don't call FIMC subdev operation to avoid nested locking */
757 if (sd == &fimc->subdev) {
758 struct flite_frame *ff = &fimc->out_frame;
759 sink_fmt.format.width = ff->f_width;
760 sink_fmt.format.height = ff->f_height;
761 sink_fmt.format.code = fimc->inp_frame.fmt->mbus_code;
763 sink_fmt.pad = pad->index;
764 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
765 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
767 if (ret < 0 && ret != -ENOIOCTLCMD)
770 /* Retrieve format at the source pad */
771 pad = media_entity_remote_pad(pad);
773 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
776 sd = media_entity_to_v4l2_subdev(pad->entity);
777 src_fmt.pad = pad->index;
778 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
779 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
780 if (ret < 0 && ret != -ENOIOCTLCMD)
783 if (src_fmt.format.width != sink_fmt.format.width ||
784 src_fmt.format.height != sink_fmt.format.height ||
785 src_fmt.format.code != sink_fmt.format.code)
791 static int fimc_lite_streamon(struct file *file, void *priv,
792 enum v4l2_buf_type type)
794 struct fimc_lite *fimc = video_drvdata(file);
795 struct media_entity *entity = &fimc->ve.vdev.entity;
796 struct fimc_pipeline *p = &fimc->pipeline;
799 if (fimc_lite_active(fimc))
802 ret = media_entity_pipeline_start(entity, p->m_pipeline);
806 ret = fimc_pipeline_validate(fimc);
810 fimc->sensor = fimc_find_remote_sensor(&fimc->subdev.entity);
812 ret = vb2_ioctl_streamon(file, priv, type);
814 fimc->streaming = true;
819 media_entity_pipeline_stop(entity);
823 static int fimc_lite_streamoff(struct file *file, void *priv,
824 enum v4l2_buf_type type)
826 struct fimc_lite *fimc = video_drvdata(file);
829 ret = vb2_ioctl_streamoff(file, priv, type);
833 media_entity_pipeline_stop(&fimc->ve.vdev.entity);
834 fimc->streaming = false;
838 static int fimc_lite_reqbufs(struct file *file, void *priv,
839 struct v4l2_requestbuffers *reqbufs)
841 struct fimc_lite *fimc = video_drvdata(file);
844 reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count);
845 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
847 fimc->reqbufs_count = reqbufs->count;
852 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
853 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
855 if (a->left < b->left || a->top < b->top)
857 if (a->left + a->width > b->left + b->width)
859 if (a->top + a->height > b->top + b->height)
865 static int fimc_lite_g_selection(struct file *file, void *fh,
866 struct v4l2_selection *sel)
868 struct fimc_lite *fimc = video_drvdata(file);
869 struct flite_frame *f = &fimc->out_frame;
871 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
874 switch (sel->target) {
875 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
876 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
879 sel->r.width = f->f_width;
880 sel->r.height = f->f_height;
883 case V4L2_SEL_TGT_COMPOSE:
891 static int fimc_lite_s_selection(struct file *file, void *fh,
892 struct v4l2_selection *sel)
894 struct fimc_lite *fimc = video_drvdata(file);
895 struct flite_frame *f = &fimc->out_frame;
896 struct v4l2_rect rect = sel->r;
899 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
900 sel->target != V4L2_SEL_TGT_COMPOSE)
903 fimc_lite_try_compose(fimc, &rect);
905 if ((sel->flags & V4L2_SEL_FLAG_LE) &&
906 !enclosed_rectangle(&rect, &sel->r))
909 if ((sel->flags & V4L2_SEL_FLAG_GE) &&
910 !enclosed_rectangle(&sel->r, &rect))
914 spin_lock_irqsave(&fimc->slock, flags);
916 set_bit(ST_FLITE_CONFIG, &fimc->state);
917 spin_unlock_irqrestore(&fimc->slock, flags);
922 static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = {
923 .vidioc_querycap = fimc_lite_querycap,
924 .vidioc_enum_fmt_vid_cap_mplane = fimc_lite_enum_fmt_mplane,
925 .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane,
926 .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane,
927 .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane,
928 .vidioc_g_selection = fimc_lite_g_selection,
929 .vidioc_s_selection = fimc_lite_s_selection,
930 .vidioc_reqbufs = fimc_lite_reqbufs,
931 .vidioc_querybuf = vb2_ioctl_querybuf,
932 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
933 .vidioc_create_bufs = vb2_ioctl_create_bufs,
934 .vidioc_qbuf = vb2_ioctl_qbuf,
935 .vidioc_dqbuf = vb2_ioctl_dqbuf,
936 .vidioc_streamon = fimc_lite_streamon,
937 .vidioc_streamoff = fimc_lite_streamoff,
940 /* Capture subdev media entity operations */
941 static int fimc_lite_link_setup(struct media_entity *entity,
942 const struct media_pad *local,
943 const struct media_pad *remote, u32 flags)
945 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
946 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
947 unsigned int remote_ent_type = media_entity_type(remote->entity);
950 if (WARN_ON(fimc == NULL))
953 v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x\n",
954 __func__, remote->entity->name, local->entity->name,
955 flags, fimc->source_subdev_grp_id);
957 mutex_lock(&fimc->lock);
959 switch (local->index) {
960 case FLITE_SD_PAD_SINK:
961 if (remote_ent_type != MEDIA_ENT_T_V4L2_SUBDEV) {
965 if (flags & MEDIA_LNK_FL_ENABLED) {
966 if (fimc->source_subdev_grp_id == 0)
967 fimc->source_subdev_grp_id = sd->grp_id;
971 fimc->source_subdev_grp_id = 0;
976 case FLITE_SD_PAD_SOURCE_DMA:
977 if (!(flags & MEDIA_LNK_FL_ENABLED))
978 atomic_set(&fimc->out_path, FIMC_IO_NONE);
979 else if (remote_ent_type == MEDIA_ENT_T_DEVNODE)
980 atomic_set(&fimc->out_path, FIMC_IO_DMA);
985 case FLITE_SD_PAD_SOURCE_ISP:
986 if (!(flags & MEDIA_LNK_FL_ENABLED))
987 atomic_set(&fimc->out_path, FIMC_IO_NONE);
988 else if (remote_ent_type == MEDIA_ENT_T_V4L2_SUBDEV)
989 atomic_set(&fimc->out_path, FIMC_IO_ISP);
995 v4l2_err(sd, "Invalid pad index\n");
1000 mutex_unlock(&fimc->lock);
1004 static const struct media_entity_operations fimc_lite_subdev_media_ops = {
1005 .link_setup = fimc_lite_link_setup,
1008 static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1009 struct v4l2_subdev_fh *fh,
1010 struct v4l2_subdev_mbus_code_enum *code)
1012 const struct fimc_fmt *fmt;
1014 fmt = fimc_lite_find_format(NULL, NULL, 0, code->index);
1017 code->code = fmt->mbus_code;
1021 static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd,
1022 struct v4l2_subdev_fh *fh,
1023 struct v4l2_subdev_format *fmt)
1025 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1026 struct v4l2_mbus_framefmt *mf = &fmt->format;
1027 struct flite_frame *f = &fimc->inp_frame;
1029 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1030 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1034 mf->colorspace = V4L2_COLORSPACE_JPEG;
1036 mutex_lock(&fimc->lock);
1037 mf->code = f->fmt->mbus_code;
1039 if (fmt->pad == FLITE_SD_PAD_SINK) {
1040 /* full camera input frame size */
1041 mf->width = f->f_width;
1042 mf->height = f->f_height;
1045 mf->width = f->rect.width;
1046 mf->height = f->rect.height;
1048 mutex_unlock(&fimc->lock);
1052 static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd,
1053 struct v4l2_subdev_fh *fh,
1054 struct v4l2_subdev_format *fmt)
1056 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1057 struct v4l2_mbus_framefmt *mf = &fmt->format;
1058 struct flite_frame *sink = &fimc->inp_frame;
1059 struct flite_frame *source = &fimc->out_frame;
1060 const struct fimc_fmt *ffmt;
1062 v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d\n",
1063 fmt->pad, mf->code, mf->width, mf->height);
1065 mf->colorspace = V4L2_COLORSPACE_JPEG;
1066 mutex_lock(&fimc->lock);
1068 if ((atomic_read(&fimc->out_path) == FIMC_IO_ISP &&
1069 sd->entity.stream_count > 0) ||
1070 (atomic_read(&fimc->out_path) == FIMC_IO_DMA &&
1071 vb2_is_busy(&fimc->vb_queue))) {
1072 mutex_unlock(&fimc->lock);
1076 ffmt = fimc_lite_try_format(fimc, &mf->width, &mf->height,
1077 &mf->code, NULL, fmt->pad);
1079 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1080 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1082 mutex_unlock(&fimc->lock);
1086 if (fmt->pad == FLITE_SD_PAD_SINK) {
1087 sink->f_width = mf->width;
1088 sink->f_height = mf->height;
1090 /* Set sink crop rectangle */
1091 sink->rect.width = mf->width;
1092 sink->rect.height = mf->height;
1093 sink->rect.left = 0;
1095 /* Reset source format and crop rectangle */
1096 source->rect = sink->rect;
1097 source->f_width = mf->width;
1098 source->f_height = mf->height;
1100 /* Allow changing format only on sink pad */
1101 mf->code = sink->fmt->mbus_code;
1102 mf->width = sink->rect.width;
1103 mf->height = sink->rect.height;
1106 mutex_unlock(&fimc->lock);
1110 static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd,
1111 struct v4l2_subdev_fh *fh,
1112 struct v4l2_subdev_selection *sel)
1114 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1115 struct flite_frame *f = &fimc->inp_frame;
1117 if ((sel->target != V4L2_SEL_TGT_CROP &&
1118 sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
1119 sel->pad != FLITE_SD_PAD_SINK)
1122 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1123 sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
1127 mutex_lock(&fimc->lock);
1128 if (sel->target == V4L2_SEL_TGT_CROP) {
1133 sel->r.width = f->f_width;
1134 sel->r.height = f->f_height;
1136 mutex_unlock(&fimc->lock);
1138 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1139 __func__, f->rect.left, f->rect.top, f->rect.width,
1140 f->rect.height, f->f_width, f->f_height);
1145 static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd,
1146 struct v4l2_subdev_fh *fh,
1147 struct v4l2_subdev_selection *sel)
1149 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1150 struct flite_frame *f = &fimc->inp_frame;
1153 if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != FLITE_SD_PAD_SINK)
1156 mutex_lock(&fimc->lock);
1157 fimc_lite_try_crop(fimc, &sel->r);
1159 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1160 *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r;
1162 unsigned long flags;
1163 spin_lock_irqsave(&fimc->slock, flags);
1165 /* Same crop rectangle on the source pad */
1166 fimc->out_frame.rect = sel->r;
1167 set_bit(ST_FLITE_CONFIG, &fimc->state);
1168 spin_unlock_irqrestore(&fimc->slock, flags);
1170 mutex_unlock(&fimc->lock);
1172 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1173 __func__, f->rect.left, f->rect.top, f->rect.width,
1174 f->rect.height, f->f_width, f->f_height);
1179 static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on)
1181 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1182 unsigned long flags;
1186 * Find sensor subdev linked to FIMC-LITE directly or through
1187 * MIPI-CSIS. This is required for configuration where FIMC-LITE
1188 * is used as a subdev only and feeds data internally to FIMC-IS.
1189 * The pipeline links are protected through entity.stream_count
1190 * so there is no need to take the media graph mutex here.
1192 fimc->sensor = fimc_find_remote_sensor(&sd->entity);
1194 if (atomic_read(&fimc->out_path) != FIMC_IO_ISP)
1195 return -ENOIOCTLCMD;
1197 mutex_lock(&fimc->lock);
1199 flite_hw_reset(fimc);
1200 ret = fimc_lite_hw_init(fimc, true);
1202 spin_lock_irqsave(&fimc->slock, flags);
1203 flite_hw_capture_start(fimc);
1204 spin_unlock_irqrestore(&fimc->slock, flags);
1207 set_bit(ST_FLITE_OFF, &fimc->state);
1209 spin_lock_irqsave(&fimc->slock, flags);
1210 flite_hw_capture_stop(fimc);
1211 spin_unlock_irqrestore(&fimc->slock, flags);
1213 ret = wait_event_timeout(fimc->irq_queue,
1214 !test_bit(ST_FLITE_OFF, &fimc->state),
1215 msecs_to_jiffies(200));
1217 v4l2_err(sd, "s_stream(0) timeout\n");
1218 clear_bit(ST_FLITE_RUN, &fimc->state);
1221 mutex_unlock(&fimc->lock);
1225 static int fimc_lite_log_status(struct v4l2_subdev *sd)
1227 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1229 flite_hw_dump_regs(fimc, __func__);
1233 static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
1235 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1236 struct vb2_queue *q = &fimc->vb_queue;
1237 struct video_device *vfd = &fimc->ve.vdev;
1240 memset(vfd, 0, sizeof(*vfd));
1242 fimc->inp_frame.fmt = &fimc_lite_formats[0];
1243 fimc->out_frame.fmt = &fimc_lite_formats[0];
1244 atomic_set(&fimc->out_path, FIMC_IO_DMA);
1246 snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture",
1249 vfd->fops = &fimc_lite_fops;
1250 vfd->ioctl_ops = &fimc_lite_ioctl_ops;
1251 vfd->v4l2_dev = sd->v4l2_dev;
1253 vfd->release = video_device_release_empty;
1255 fimc->reqbufs_count = 0;
1257 INIT_LIST_HEAD(&fimc->pending_buf_q);
1258 INIT_LIST_HEAD(&fimc->active_buf_q);
1260 memset(q, 0, sizeof(*q));
1261 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1262 q->io_modes = VB2_MMAP | VB2_USERPTR;
1263 q->ops = &fimc_lite_qops;
1264 q->mem_ops = &vb2_dma_contig_memops;
1265 q->buf_struct_size = sizeof(struct flite_buffer);
1267 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1268 q->lock = &fimc->lock;
1270 ret = vb2_queue_init(q);
1274 fimc->vd_pad.flags = MEDIA_PAD_FL_SINK;
1275 ret = media_entity_init(&vfd->entity, 1, &fimc->vd_pad, 0);
1279 video_set_drvdata(vfd, fimc);
1280 fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
1282 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1284 media_entity_cleanup(&vfd->entity);
1285 fimc->pipeline_ops = NULL;
1289 v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n",
1290 vfd->name, video_device_node_name(vfd));
1294 static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd)
1296 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1301 if (video_is_registered(&fimc->ve.vdev)) {
1302 video_unregister_device(&fimc->ve.vdev);
1303 media_entity_cleanup(&fimc->ve.vdev.entity);
1304 fimc->pipeline_ops = NULL;
1308 static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = {
1309 .registered = fimc_lite_subdev_registered,
1310 .unregistered = fimc_lite_subdev_unregistered,
1313 static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = {
1314 .enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
1315 .get_selection = fimc_lite_subdev_get_selection,
1316 .set_selection = fimc_lite_subdev_set_selection,
1317 .get_fmt = fimc_lite_subdev_get_fmt,
1318 .set_fmt = fimc_lite_subdev_set_fmt,
1321 static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = {
1322 .s_stream = fimc_lite_subdev_s_stream,
1325 static const struct v4l2_subdev_core_ops fimc_lite_core_ops = {
1326 .log_status = fimc_lite_log_status,
1329 static struct v4l2_subdev_ops fimc_lite_subdev_ops = {
1330 .core = &fimc_lite_core_ops,
1331 .video = &fimc_lite_subdev_video_ops,
1332 .pad = &fimc_lite_subdev_pad_ops,
1335 static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl)
1337 struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite,
1339 set_bit(ST_FLITE_CONFIG, &fimc->state);
1343 static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = {
1344 .s_ctrl = fimc_lite_s_ctrl,
1347 static const struct v4l2_ctrl_config fimc_lite_ctrl = {
1348 .ops = &fimc_lite_ctrl_ops,
1349 .id = V4L2_CTRL_CLASS_USER | 0x1001,
1350 .type = V4L2_CTRL_TYPE_BOOLEAN,
1351 .name = "Test Pattern 640x480",
1355 static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc)
1357 struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler;
1358 struct v4l2_subdev *sd = &fimc->subdev;
1361 v4l2_subdev_init(sd, &fimc_lite_subdev_ops);
1362 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1363 snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index);
1365 fimc->subdev_pads[FLITE_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1366 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_DMA].flags = MEDIA_PAD_FL_SOURCE;
1367 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_ISP].flags = MEDIA_PAD_FL_SOURCE;
1368 ret = media_entity_init(&sd->entity, FLITE_SD_PADS_NUM,
1369 fimc->subdev_pads, 0);
1373 v4l2_ctrl_handler_init(handler, 1);
1374 fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl,
1376 if (handler->error) {
1377 media_entity_cleanup(&sd->entity);
1378 return handler->error;
1381 sd->ctrl_handler = handler;
1382 sd->internal_ops = &fimc_lite_subdev_internal_ops;
1383 sd->entity.ops = &fimc_lite_subdev_media_ops;
1384 sd->owner = THIS_MODULE;
1385 v4l2_set_subdevdata(sd, fimc);
1390 static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc)
1392 struct v4l2_subdev *sd = &fimc->subdev;
1394 v4l2_device_unregister_subdev(sd);
1395 media_entity_cleanup(&sd->entity);
1396 v4l2_ctrl_handler_free(&fimc->ctrl_handler);
1397 v4l2_set_subdevdata(sd, NULL);
1400 static void fimc_lite_clk_put(struct fimc_lite *fimc)
1402 if (IS_ERR(fimc->clock))
1405 clk_unprepare(fimc->clock);
1406 clk_put(fimc->clock);
1407 fimc->clock = ERR_PTR(-EINVAL);
1410 static int fimc_lite_clk_get(struct fimc_lite *fimc)
1414 fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME);
1415 if (IS_ERR(fimc->clock))
1416 return PTR_ERR(fimc->clock);
1418 ret = clk_prepare(fimc->clock);
1420 clk_put(fimc->clock);
1421 fimc->clock = ERR_PTR(-EINVAL);
1426 static const struct of_device_id flite_of_match[];
1428 static int fimc_lite_probe(struct platform_device *pdev)
1430 struct flite_drvdata *drv_data = NULL;
1431 struct device *dev = &pdev->dev;
1432 const struct of_device_id *of_id;
1433 struct fimc_lite *fimc;
1434 struct resource *res;
1437 fimc = devm_kzalloc(dev, sizeof(*fimc), GFP_KERNEL);
1442 of_id = of_match_node(flite_of_match, dev->of_node);
1444 drv_data = (struct flite_drvdata *)of_id->data;
1445 fimc->index = of_alias_get_id(dev->of_node, "fimc-lite");
1447 drv_data = fimc_lite_get_drvdata(pdev);
1448 fimc->index = pdev->id;
1451 if (!drv_data || fimc->index < 0 || fimc->index >= FIMC_LITE_MAX_DEVS)
1454 fimc->dd = drv_data;
1457 init_waitqueue_head(&fimc->irq_queue);
1458 spin_lock_init(&fimc->slock);
1459 mutex_init(&fimc->lock);
1461 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1462 fimc->regs = devm_ioremap_resource(dev, res);
1463 if (IS_ERR(fimc->regs))
1464 return PTR_ERR(fimc->regs);
1466 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1468 dev_err(dev, "Failed to get IRQ resource\n");
1472 ret = fimc_lite_clk_get(fimc);
1476 ret = devm_request_irq(dev, res->start, flite_irq_handler,
1477 0, dev_name(dev), fimc);
1479 dev_err(dev, "Failed to install irq (%d)\n", ret);
1483 /* The video node will be created within the subdev's registered() op */
1484 ret = fimc_lite_create_capture_subdev(fimc);
1488 platform_set_drvdata(pdev, fimc);
1489 pm_runtime_enable(dev);
1490 ret = pm_runtime_get_sync(dev);
1494 fimc->alloc_ctx = vb2_dma_contig_init_ctx(dev);
1495 if (IS_ERR(fimc->alloc_ctx)) {
1496 ret = PTR_ERR(fimc->alloc_ctx);
1499 pm_runtime_put(dev);
1501 dev_dbg(dev, "FIMC-LITE.%d registered successfully\n",
1505 pm_runtime_put(dev);
1507 fimc_lite_unregister_capture_subdev(fimc);
1509 fimc_lite_clk_put(fimc);
1513 static int fimc_lite_runtime_resume(struct device *dev)
1515 struct fimc_lite *fimc = dev_get_drvdata(dev);
1517 clk_enable(fimc->clock);
1521 static int fimc_lite_runtime_suspend(struct device *dev)
1523 struct fimc_lite *fimc = dev_get_drvdata(dev);
1525 clk_disable(fimc->clock);
1529 #ifdef CONFIG_PM_SLEEP
1530 static int fimc_lite_resume(struct device *dev)
1532 struct fimc_lite *fimc = dev_get_drvdata(dev);
1533 struct flite_buffer *buf;
1534 unsigned long flags;
1537 spin_lock_irqsave(&fimc->slock, flags);
1538 if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
1539 !test_bit(ST_FLITE_IN_USE, &fimc->state)) {
1540 spin_unlock_irqrestore(&fimc->slock, flags);
1543 flite_hw_reset(fimc);
1544 spin_unlock_irqrestore(&fimc->slock, flags);
1546 if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state))
1549 INIT_LIST_HEAD(&fimc->active_buf_q);
1550 fimc_pipeline_call(fimc, open, &fimc->pipeline,
1551 &fimc->ve.vdev.entity, false);
1552 fimc_lite_hw_init(fimc, atomic_read(&fimc->out_path) == FIMC_IO_ISP);
1553 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
1555 for (i = 0; i < fimc->reqbufs_count; i++) {
1556 if (list_empty(&fimc->pending_buf_q))
1558 buf = fimc_lite_pending_queue_pop(fimc);
1559 buffer_queue(&buf->vb);
1564 static int fimc_lite_suspend(struct device *dev)
1566 struct fimc_lite *fimc = dev_get_drvdata(dev);
1567 bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state);
1570 if (test_and_set_bit(ST_LPM, &fimc->state))
1573 ret = fimc_lite_stop_capture(fimc, suspend);
1574 if (ret < 0 || !fimc_lite_active(fimc))
1577 return fimc_pipeline_call(fimc, close, &fimc->pipeline);
1579 #endif /* CONFIG_PM_SLEEP */
1581 static int fimc_lite_remove(struct platform_device *pdev)
1583 struct fimc_lite *fimc = platform_get_drvdata(pdev);
1584 struct device *dev = &pdev->dev;
1586 pm_runtime_disable(dev);
1587 pm_runtime_set_suspended(dev);
1588 fimc_lite_unregister_capture_subdev(fimc);
1589 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
1590 fimc_lite_clk_put(fimc);
1592 dev_info(dev, "Driver unloaded\n");
1596 static const struct dev_pm_ops fimc_lite_pm_ops = {
1597 SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume)
1598 SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume,
1602 /* EXYNOS4212, EXYNOS4412 */
1603 static struct flite_drvdata fimc_lite_drvdata_exynos4 = {
1606 .out_width_align = 8,
1607 .win_hor_offs_align = 2,
1608 .out_hor_offs_align = 8,
1611 static const struct of_device_id flite_of_match[] = {
1613 .compatible = "samsung,exynos4212-fimc-lite",
1614 .data = &fimc_lite_drvdata_exynos4,
1618 MODULE_DEVICE_TABLE(of, flite_of_match);
1620 static struct platform_driver fimc_lite_driver = {
1621 .probe = fimc_lite_probe,
1622 .remove = fimc_lite_remove,
1624 .of_match_table = flite_of_match,
1625 .name = FIMC_LITE_DRV_NAME,
1626 .owner = THIS_MODULE,
1627 .pm = &fimc_lite_pm_ops,
1630 module_platform_driver(fimc_lite_driver);
1631 MODULE_LICENSE("GPL");
1632 MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME);