2 * Samsung EXYNOS FIMC-LITE (camera host interface) driver
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
5 * 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/device.h>
15 #include <linux/errno.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-mem2mem.h>
29 #include <media/videobuf2-core.h>
30 #include <media/videobuf2-dma-contig.h>
31 #include <media/s5p_fimc.h>
33 #include "fimc-mdevice.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 .name = "YUV 4:2:2 packed, YCbYCr",
44 .fourcc = V4L2_PIX_FMT_YUYV,
46 .color = FIMC_FMT_YCBYCR422,
48 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
50 .name = "YUV 4:2:2 packed, CbYCrY",
51 .fourcc = V4L2_PIX_FMT_UYVY,
53 .color = FIMC_FMT_CBYCRY422,
55 .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
57 .name = "YUV 4:2:2 packed, CrYCbY",
58 .fourcc = V4L2_PIX_FMT_VYUY,
60 .color = FIMC_FMT_CRYCBY422,
62 .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
64 .name = "YUV 4:2:2 packed, YCrYCb",
65 .fourcc = V4L2_PIX_FMT_YVYU,
67 .color = FIMC_FMT_YCRYCB422,
69 .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
71 .name = "RAW8 (GRBG)",
72 .fourcc = V4L2_PIX_FMT_SGRBG8,
74 .color = FIMC_FMT_RAW8,
76 .mbus_code = V4L2_MBUS_FMT_SGRBG8_1X8,
78 .name = "RAW10 (GRBG)",
79 .fourcc = V4L2_PIX_FMT_SGRBG10,
81 .color = FIMC_FMT_RAW10,
83 .mbus_code = V4L2_MBUS_FMT_SGRBG10_1X10,
85 .name = "RAW12 (GRBG)",
86 .fourcc = V4L2_PIX_FMT_SGRBG12,
88 .color = FIMC_FMT_RAW12,
90 .mbus_code = V4L2_MBUS_FMT_SGRBG12_1X12,
95 * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
96 * @pixelformat: fourcc to match, ignored if null
97 * @mbus_code: media bus code to match, ignored if null
98 * @index: index to the fimc_lite_formats array, ignored if negative
100 static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat,
101 const u32 *mbus_code, int index)
103 const struct fimc_fmt *fmt, *def_fmt = NULL;
107 if (index >= (int)ARRAY_SIZE(fimc_lite_formats))
110 for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) {
111 fmt = &fimc_lite_formats[i];
112 if (pixelformat && fmt->fourcc == *pixelformat)
114 if (mbus_code && fmt->mbus_code == *mbus_code)
123 static int fimc_lite_hw_init(struct fimc_lite *fimc, bool isp_output)
125 struct fimc_pipeline *pipeline = &fimc->pipeline;
126 struct v4l2_subdev *sensor;
127 struct fimc_sensor_info *si;
130 sensor = isp_output ? fimc->sensor : pipeline->subdevs[IDX_SENSOR];
135 if (fimc->fmt == NULL)
138 /* Get sensor configuration data from the sensor subdev */
139 si = v4l2_get_subdev_hostdata(sensor);
140 spin_lock_irqsave(&fimc->slock, flags);
142 flite_hw_set_camera_bus(fimc, &si->pdata);
143 flite_hw_set_source_format(fimc, &fimc->inp_frame);
144 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
145 flite_hw_set_output_dma(fimc, &fimc->out_frame, !isp_output);
146 flite_hw_set_interrupt_mask(fimc);
147 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
150 flite_hw_dump_regs(fimc, __func__);
152 spin_unlock_irqrestore(&fimc->slock, flags);
157 * Reinitialize the driver so it is ready to start the streaming again.
158 * Set fimc->state to indicate stream off and the hardware shut down state.
159 * If not suspending (@suspend is false), return any buffers to videobuf2.
160 * Otherwise put any owned buffers onto the pending buffers queue, so they
161 * can be re-spun when the device is being resumed. Also perform FIMC
162 * software reset and disable streaming on the whole pipeline if required.
164 static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend)
166 struct flite_buffer *buf;
170 spin_lock_irqsave(&fimc->slock, flags);
171 streaming = fimc->state & (1 << ST_SENSOR_STREAM);
173 fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF |
174 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM);
176 fimc->state |= (1 << ST_FLITE_SUSPENDED);
178 fimc->state &= ~(1 << ST_FLITE_PENDING |
179 1 << ST_FLITE_SUSPENDED);
181 /* Release unused buffers */
182 while (!suspend && !list_empty(&fimc->pending_buf_q)) {
183 buf = fimc_lite_pending_queue_pop(fimc);
184 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
186 /* If suspending put unused buffers onto pending queue */
187 while (!list_empty(&fimc->active_buf_q)) {
188 buf = fimc_lite_active_queue_pop(fimc);
190 fimc_lite_pending_queue_add(fimc, buf);
192 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
195 spin_unlock_irqrestore(&fimc->slock, flags);
197 flite_hw_reset(fimc);
202 return fimc_pipeline_call(fimc, set_stream, &fimc->pipeline, 0);
205 static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend)
209 if (!fimc_lite_active(fimc))
212 spin_lock_irqsave(&fimc->slock, flags);
213 set_bit(ST_FLITE_OFF, &fimc->state);
214 flite_hw_capture_stop(fimc);
215 spin_unlock_irqrestore(&fimc->slock, flags);
217 wait_event_timeout(fimc->irq_queue,
218 !test_bit(ST_FLITE_OFF, &fimc->state),
219 (2*HZ/10)); /* 200 ms */
221 return fimc_lite_reinit(fimc, suspend);
224 /* Must be called with fimc.slock spinlock held. */
225 static void fimc_lite_config_update(struct fimc_lite *fimc)
227 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
228 flite_hw_set_dma_window(fimc, &fimc->out_frame);
229 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
230 clear_bit(ST_FLITE_CONFIG, &fimc->state);
233 static irqreturn_t flite_irq_handler(int irq, void *priv)
235 struct fimc_lite *fimc = priv;
236 struct flite_buffer *vbuf;
242 spin_lock_irqsave(&fimc->slock, flags);
244 intsrc = flite_hw_get_interrupt_source(fimc);
245 flite_hw_clear_pending_irq(fimc);
247 if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) {
248 wake_up(&fimc->irq_queue);
252 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) {
253 clear_bit(ST_FLITE_RUN, &fimc->state);
254 fimc->events.data_overflow++;
257 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) {
258 flite_hw_clear_last_capture_end(fimc);
259 clear_bit(ST_FLITE_STREAM, &fimc->state);
260 wake_up(&fimc->irq_queue);
263 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA)
266 if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) &&
267 test_bit(ST_FLITE_RUN, &fimc->state) &&
268 !list_empty(&fimc->active_buf_q) &&
269 !list_empty(&fimc->pending_buf_q)) {
270 vbuf = fimc_lite_active_queue_pop(fimc);
272 tv = &vbuf->vb.v4l2_buf.timestamp;
273 tv->tv_sec = ts.tv_sec;
274 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
275 vbuf->vb.v4l2_buf.sequence = fimc->frame_count++;
276 vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE);
278 vbuf = fimc_lite_pending_queue_pop(fimc);
279 flite_hw_set_output_addr(fimc, vbuf->paddr);
280 fimc_lite_active_queue_add(fimc, vbuf);
283 if (test_bit(ST_FLITE_CONFIG, &fimc->state))
284 fimc_lite_config_update(fimc);
286 if (list_empty(&fimc->pending_buf_q)) {
287 flite_hw_capture_stop(fimc);
288 clear_bit(ST_FLITE_STREAM, &fimc->state);
291 set_bit(ST_FLITE_RUN, &fimc->state);
292 spin_unlock_irqrestore(&fimc->slock, flags);
296 static int start_streaming(struct vb2_queue *q, unsigned int count)
298 struct fimc_lite *fimc = q->drv_priv;
301 fimc->frame_count = 0;
303 ret = fimc_lite_hw_init(fimc, false);
305 fimc_lite_reinit(fimc, false);
309 set_bit(ST_FLITE_PENDING, &fimc->state);
311 if (!list_empty(&fimc->active_buf_q) &&
312 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
313 flite_hw_capture_start(fimc);
315 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
316 fimc_pipeline_call(fimc, set_stream,
320 flite_hw_dump_regs(fimc, __func__);
325 static int stop_streaming(struct vb2_queue *q)
327 struct fimc_lite *fimc = q->drv_priv;
329 if (!fimc_lite_active(fimc))
332 return fimc_lite_stop_capture(fimc, false);
335 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
336 unsigned int *num_buffers, unsigned int *num_planes,
337 unsigned int sizes[], void *allocators[])
339 const struct v4l2_pix_format_mplane *pixm = NULL;
340 struct fimc_lite *fimc = vq->drv_priv;
341 struct flite_frame *frame = &fimc->out_frame;
342 const struct fimc_fmt *fmt = fimc->fmt;
347 pixm = &pfmt->fmt.pix_mp;
348 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, -1);
349 wh = pixm->width * pixm->height;
351 wh = frame->f_width * frame->f_height;
357 *num_planes = fmt->memplanes;
359 for (i = 0; i < fmt->memplanes; i++) {
360 unsigned int size = (wh * fmt->depth[i]) / 8;
362 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
365 allocators[i] = fimc->alloc_ctx;
371 static int buffer_prepare(struct vb2_buffer *vb)
373 struct vb2_queue *vq = vb->vb2_queue;
374 struct fimc_lite *fimc = vq->drv_priv;
377 if (fimc->fmt == NULL)
380 for (i = 0; i < fimc->fmt->memplanes; i++) {
381 unsigned long size = fimc->payload[i];
383 if (vb2_plane_size(vb, i) < size) {
385 "User buffer too small (%ld < %ld)\n",
386 vb2_plane_size(vb, i), size);
389 vb2_set_plane_payload(vb, i, size);
395 static void buffer_queue(struct vb2_buffer *vb)
397 struct flite_buffer *buf
398 = container_of(vb, struct flite_buffer, vb);
399 struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue);
402 spin_lock_irqsave(&fimc->slock, flags);
403 buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0);
405 if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) &&
406 !test_bit(ST_FLITE_STREAM, &fimc->state) &&
407 list_empty(&fimc->active_buf_q)) {
408 flite_hw_set_output_addr(fimc, buf->paddr);
409 fimc_lite_active_queue_add(fimc, buf);
411 fimc_lite_pending_queue_add(fimc, buf);
414 if (vb2_is_streaming(&fimc->vb_queue) &&
415 !list_empty(&fimc->pending_buf_q) &&
416 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
417 flite_hw_capture_start(fimc);
418 spin_unlock_irqrestore(&fimc->slock, flags);
420 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
421 fimc_pipeline_call(fimc, set_stream,
425 spin_unlock_irqrestore(&fimc->slock, flags);
428 static void fimc_lock(struct vb2_queue *vq)
430 struct fimc_lite *fimc = vb2_get_drv_priv(vq);
431 mutex_lock(&fimc->lock);
434 static void fimc_unlock(struct vb2_queue *vq)
436 struct fimc_lite *fimc = vb2_get_drv_priv(vq);
437 mutex_unlock(&fimc->lock);
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 = fimc_unlock,
445 .wait_finish = fimc_lock,
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->vfd.entity;
465 mutex_lock(&me->parent->graph_mutex);
467 mutex_lock(&fimc->lock);
468 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA) {
473 set_bit(ST_FLITE_IN_USE, &fimc->state);
474 ret = pm_runtime_get_sync(&fimc->pdev->dev);
478 ret = v4l2_fh_open(file);
482 if (++fimc->ref_count == 1 &&
483 atomic_read(&fimc->out_path) == FIMC_IO_DMA) {
484 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
485 &fimc->vfd.entity, true);
487 pm_runtime_put_sync(&fimc->pdev->dev);
489 v4l2_fh_release(file);
490 clear_bit(ST_FLITE_IN_USE, &fimc->state);
493 fimc_lite_clear_event_counters(fimc);
496 mutex_unlock(&fimc->lock);
497 mutex_unlock(&me->parent->graph_mutex);
501 static int fimc_lite_close(struct file *file)
503 struct fimc_lite *fimc = video_drvdata(file);
506 mutex_lock(&fimc->lock);
508 if (--fimc->ref_count == 0 &&
509 atomic_read(&fimc->out_path) == FIMC_IO_DMA) {
510 clear_bit(ST_FLITE_IN_USE, &fimc->state);
511 fimc_lite_stop_capture(fimc, false);
512 fimc_pipeline_call(fimc, close, &fimc->pipeline);
513 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
516 pm_runtime_put(&fimc->pdev->dev);
518 if (fimc->ref_count == 0)
519 vb2_queue_release(&fimc->vb_queue);
521 ret = v4l2_fh_release(file);
523 mutex_unlock(&fimc->lock);
527 static unsigned int fimc_lite_poll(struct file *file,
528 struct poll_table_struct *wait)
530 struct fimc_lite *fimc = video_drvdata(file);
533 if (mutex_lock_interruptible(&fimc->lock))
536 ret = vb2_poll(&fimc->vb_queue, file, wait);
537 mutex_unlock(&fimc->lock);
542 static int fimc_lite_mmap(struct file *file, struct vm_area_struct *vma)
544 struct fimc_lite *fimc = video_drvdata(file);
547 if (mutex_lock_interruptible(&fimc->lock))
550 ret = vb2_mmap(&fimc->vb_queue, vma);
551 mutex_unlock(&fimc->lock);
556 static const struct v4l2_file_operations fimc_lite_fops = {
557 .owner = THIS_MODULE,
558 .open = fimc_lite_open,
559 .release = fimc_lite_close,
560 .poll = fimc_lite_poll,
561 .unlocked_ioctl = video_ioctl2,
562 .mmap = fimc_lite_mmap,
566 * Format and crop negotiation helpers
569 static const struct fimc_fmt *fimc_lite_try_format(struct fimc_lite *fimc,
570 u32 *width, u32 *height,
571 u32 *code, u32 *fourcc, int pad)
573 struct flite_variant *variant = fimc->variant;
574 const struct fimc_fmt *fmt;
576 fmt = fimc_lite_find_format(fourcc, code, 0);
581 *code = fmt->mbus_code;
583 *fourcc = fmt->fourcc;
585 if (pad == FLITE_SD_PAD_SINK) {
586 v4l_bound_align_image(width, 8, variant->max_width,
587 ffs(variant->out_width_align) - 1,
588 height, 0, variant->max_height, 0, 0);
590 v4l_bound_align_image(width, 8, fimc->inp_frame.rect.width,
591 ffs(variant->out_width_align) - 1,
592 height, 0, fimc->inp_frame.rect.height,
596 v4l2_dbg(1, debug, &fimc->subdev, "code: 0x%x, %dx%d\n",
597 code ? *code : 0, *width, *height);
602 static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r)
604 struct flite_frame *frame = &fimc->inp_frame;
606 v4l_bound_align_image(&r->width, 0, frame->f_width, 0,
607 &r->height, 0, frame->f_height, 0, 0);
609 /* Adjust left/top if cropping rectangle got out of bounds */
610 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
611 r->left = round_down(r->left, fimc->variant->win_hor_offs_align);
612 r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height);
614 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d\n",
615 r->left, r->top, r->width, r->height,
616 frame->f_width, frame->f_height);
619 static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r)
621 struct flite_frame *frame = &fimc->out_frame;
622 struct v4l2_rect *crop_rect = &fimc->inp_frame.rect;
624 /* Scaling is not supported so we enforce compose rectangle size
625 same as size of the sink crop rectangle. */
626 r->width = crop_rect->width;
627 r->height = crop_rect->height;
629 /* Adjust left/top if the composing 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->variant->out_hor_offs_align);
632 r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height);
634 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d\n",
635 r->left, r->top, r->width, r->height,
636 frame->f_width, frame->f_height);
640 * Video node ioctl operations
642 static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
643 struct v4l2_capability *cap)
645 strlcpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver));
646 cap->bus_info[0] = 0;
648 cap->capabilities = V4L2_CAP_STREAMING;
652 static int fimc_lite_enum_fmt_mplane(struct file *file, void *priv,
653 struct v4l2_fmtdesc *f)
655 const struct fimc_fmt *fmt;
657 if (f->index >= ARRAY_SIZE(fimc_lite_formats))
660 fmt = &fimc_lite_formats[f->index];
661 strlcpy(f->description, fmt->name, sizeof(f->description));
662 f->pixelformat = fmt->fourcc;
667 static int fimc_lite_g_fmt_mplane(struct file *file, void *fh,
668 struct v4l2_format *f)
670 struct fimc_lite *fimc = video_drvdata(file);
671 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
672 struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0];
673 struct flite_frame *frame = &fimc->out_frame;
674 const struct fimc_fmt *fmt = fimc->fmt;
676 plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8;
677 plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height;
679 pixm->num_planes = fmt->memplanes;
680 pixm->pixelformat = fmt->fourcc;
681 pixm->width = frame->f_width;
682 pixm->height = frame->f_height;
683 pixm->field = V4L2_FIELD_NONE;
684 pixm->colorspace = V4L2_COLORSPACE_JPEG;
688 static int fimc_lite_try_fmt(struct fimc_lite *fimc,
689 struct v4l2_pix_format_mplane *pixm,
690 const struct fimc_fmt **ffmt)
692 struct flite_variant *variant = fimc->variant;
693 u32 bpl = pixm->plane_fmt[0].bytesperline;
694 const struct fimc_fmt *fmt;
696 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, 0);
697 if (WARN_ON(fmt == NULL))
701 v4l_bound_align_image(&pixm->width, 8, variant->max_width,
702 ffs(variant->out_width_align) - 1,
703 &pixm->height, 0, variant->max_height, 0, 0);
705 if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width))
706 pixm->plane_fmt[0].bytesperline = (pixm->width *
709 if (pixm->plane_fmt[0].sizeimage == 0)
710 pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height *
712 pixm->num_planes = fmt->memplanes;
713 pixm->pixelformat = fmt->fourcc;
714 pixm->colorspace = V4L2_COLORSPACE_JPEG;
715 pixm->field = V4L2_FIELD_NONE;
719 static int fimc_lite_try_fmt_mplane(struct file *file, void *fh,
720 struct v4l2_format *f)
722 struct fimc_lite *fimc = video_drvdata(file);
724 return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL);
727 static int fimc_lite_s_fmt_mplane(struct file *file, void *priv,
728 struct v4l2_format *f)
730 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
731 struct fimc_lite *fimc = video_drvdata(file);
732 struct flite_frame *frame = &fimc->out_frame;
733 const struct fimc_fmt *fmt = NULL;
736 if (vb2_is_busy(&fimc->vb_queue))
739 ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt);
744 fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8,
745 pixm->plane_fmt[0].sizeimage);
746 frame->f_width = pixm->width;
747 frame->f_height = pixm->height;
752 static int fimc_pipeline_validate(struct fimc_lite *fimc)
754 struct v4l2_subdev *sd = &fimc->subdev;
755 struct v4l2_subdev_format sink_fmt, src_fmt;
756 struct media_pad *pad;
760 /* Retrieve format at the sink pad */
761 pad = &sd->entity.pads[0];
762 if (!(pad->flags & MEDIA_PAD_FL_SINK))
764 /* Don't call FIMC subdev operation to avoid nested locking */
765 if (sd == &fimc->subdev) {
766 struct flite_frame *ff = &fimc->out_frame;
767 sink_fmt.format.width = ff->f_width;
768 sink_fmt.format.height = ff->f_height;
769 sink_fmt.format.code = fimc->fmt->mbus_code;
771 sink_fmt.pad = pad->index;
772 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
773 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
775 if (ret < 0 && ret != -ENOIOCTLCMD)
778 /* Retrieve format at the source pad */
779 pad = media_entity_remote_source(pad);
781 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
784 sd = media_entity_to_v4l2_subdev(pad->entity);
785 src_fmt.pad = pad->index;
786 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
787 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
788 if (ret < 0 && ret != -ENOIOCTLCMD)
791 if (src_fmt.format.width != sink_fmt.format.width ||
792 src_fmt.format.height != sink_fmt.format.height ||
793 src_fmt.format.code != sink_fmt.format.code)
799 static int fimc_lite_streamon(struct file *file, void *priv,
800 enum v4l2_buf_type type)
802 struct fimc_lite *fimc = video_drvdata(file);
803 struct v4l2_subdev *sensor = fimc->pipeline.subdevs[IDX_SENSOR];
804 struct fimc_pipeline *p = &fimc->pipeline;
807 if (fimc_lite_active(fimc))
810 ret = media_entity_pipeline_start(&sensor->entity, p->m_pipeline);
814 ret = fimc_pipeline_validate(fimc);
816 media_entity_pipeline_stop(&sensor->entity);
820 return vb2_streamon(&fimc->vb_queue, type);
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);
827 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
830 ret = vb2_streamoff(&fimc->vb_queue, type);
832 media_entity_pipeline_stop(&sd->entity);
836 static int fimc_lite_reqbufs(struct file *file, void *priv,
837 struct v4l2_requestbuffers *reqbufs)
839 struct fimc_lite *fimc = video_drvdata(file);
842 reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count);
843 ret = vb2_reqbufs(&fimc->vb_queue, reqbufs);
845 fimc->reqbufs_count = reqbufs->count;
850 static int fimc_lite_querybuf(struct file *file, void *priv,
851 struct v4l2_buffer *buf)
853 struct fimc_lite *fimc = video_drvdata(file);
855 return vb2_querybuf(&fimc->vb_queue, buf);
858 static int fimc_lite_qbuf(struct file *file, void *priv,
859 struct v4l2_buffer *buf)
861 struct fimc_lite *fimc = video_drvdata(file);
863 return vb2_qbuf(&fimc->vb_queue, buf);
866 static int fimc_lite_dqbuf(struct file *file, void *priv,
867 struct v4l2_buffer *buf)
869 struct fimc_lite *fimc = video_drvdata(file);
871 return vb2_dqbuf(&fimc->vb_queue, buf, file->f_flags & O_NONBLOCK);
874 static int fimc_lite_create_bufs(struct file *file, void *priv,
875 struct v4l2_create_buffers *create)
877 struct fimc_lite *fimc = video_drvdata(file);
879 return vb2_create_bufs(&fimc->vb_queue, create);
882 static int fimc_lite_prepare_buf(struct file *file, void *priv,
883 struct v4l2_buffer *b)
885 struct fimc_lite *fimc = video_drvdata(file);
887 return vb2_prepare_buf(&fimc->vb_queue, b);
890 /* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
891 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
893 if (a->left < b->left || a->top < b->top)
895 if (a->left + a->width > b->left + b->width)
897 if (a->top + a->height > b->top + b->height)
903 static int fimc_lite_g_selection(struct file *file, void *fh,
904 struct v4l2_selection *sel)
906 struct fimc_lite *fimc = video_drvdata(file);
907 struct flite_frame *f = &fimc->out_frame;
909 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
912 switch (sel->target) {
913 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
914 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
917 sel->r.width = f->f_width;
918 sel->r.height = f->f_height;
921 case V4L2_SEL_TGT_COMPOSE:
929 static int fimc_lite_s_selection(struct file *file, void *fh,
930 struct v4l2_selection *sel)
932 struct fimc_lite *fimc = video_drvdata(file);
933 struct flite_frame *f = &fimc->out_frame;
934 struct v4l2_rect rect = sel->r;
937 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
938 sel->target != V4L2_SEL_TGT_COMPOSE)
941 fimc_lite_try_compose(fimc, &rect);
943 if ((sel->flags & V4L2_SEL_FLAG_LE) &&
944 !enclosed_rectangle(&rect, &sel->r))
947 if ((sel->flags & V4L2_SEL_FLAG_GE) &&
948 !enclosed_rectangle(&sel->r, &rect))
952 spin_lock_irqsave(&fimc->slock, flags);
954 set_bit(ST_FLITE_CONFIG, &fimc->state);
955 spin_unlock_irqrestore(&fimc->slock, flags);
960 static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = {
961 .vidioc_querycap = fimc_vidioc_querycap_capture,
962 .vidioc_enum_fmt_vid_cap_mplane = fimc_lite_enum_fmt_mplane,
963 .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane,
964 .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane,
965 .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane,
966 .vidioc_g_selection = fimc_lite_g_selection,
967 .vidioc_s_selection = fimc_lite_s_selection,
968 .vidioc_reqbufs = fimc_lite_reqbufs,
969 .vidioc_querybuf = fimc_lite_querybuf,
970 .vidioc_prepare_buf = fimc_lite_prepare_buf,
971 .vidioc_create_bufs = fimc_lite_create_bufs,
972 .vidioc_qbuf = fimc_lite_qbuf,
973 .vidioc_dqbuf = fimc_lite_dqbuf,
974 .vidioc_streamon = fimc_lite_streamon,
975 .vidioc_streamoff = fimc_lite_streamoff,
978 /* Called with the media graph mutex held */
979 static struct v4l2_subdev *__find_remote_sensor(struct media_entity *me)
981 struct media_pad *pad = &me->pads[0];
982 struct v4l2_subdev *sd;
984 while (pad->flags & MEDIA_PAD_FL_SINK) {
986 pad = media_entity_remote_source(pad);
988 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
991 sd = media_entity_to_v4l2_subdev(pad->entity);
993 if (sd->grp_id == GRP_ID_FIMC_IS_SENSOR)
996 pad = &sd->entity.pads[0];
1001 /* Capture subdev media entity operations */
1002 static int fimc_lite_link_setup(struct media_entity *entity,
1003 const struct media_pad *local,
1004 const struct media_pad *remote, u32 flags)
1006 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1007 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1008 unsigned int remote_ent_type = media_entity_type(remote->entity);
1011 if (WARN_ON(fimc == NULL))
1014 v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x\n",
1015 __func__, remote->entity->name, local->entity->name,
1016 flags, fimc->source_subdev_grp_id);
1018 mutex_lock(&fimc->lock);
1020 switch (local->index) {
1021 case FLITE_SD_PAD_SINK:
1022 if (remote_ent_type != MEDIA_ENT_T_V4L2_SUBDEV) {
1026 if (flags & MEDIA_LNK_FL_ENABLED) {
1027 if (fimc->source_subdev_grp_id == 0)
1028 fimc->source_subdev_grp_id = sd->grp_id;
1032 fimc->source_subdev_grp_id = 0;
1033 fimc->sensor = NULL;
1037 case FLITE_SD_PAD_SOURCE_DMA:
1038 if (!(flags & MEDIA_LNK_FL_ENABLED))
1039 atomic_set(&fimc->out_path, FIMC_IO_NONE);
1040 else if (remote_ent_type == MEDIA_ENT_T_DEVNODE)
1041 atomic_set(&fimc->out_path, FIMC_IO_DMA);
1046 case FLITE_SD_PAD_SOURCE_ISP:
1047 if (!(flags & MEDIA_LNK_FL_ENABLED))
1048 atomic_set(&fimc->out_path, FIMC_IO_NONE);
1049 else if (remote_ent_type == MEDIA_ENT_T_V4L2_SUBDEV)
1050 atomic_set(&fimc->out_path, FIMC_IO_ISP);
1056 v4l2_err(sd, "Invalid pad index\n");
1061 mutex_unlock(&fimc->lock);
1065 static const struct media_entity_operations fimc_lite_subdev_media_ops = {
1066 .link_setup = fimc_lite_link_setup,
1069 static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1070 struct v4l2_subdev_fh *fh,
1071 struct v4l2_subdev_mbus_code_enum *code)
1073 const struct fimc_fmt *fmt;
1075 fmt = fimc_lite_find_format(NULL, NULL, code->index);
1078 code->code = fmt->mbus_code;
1082 static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd,
1083 struct v4l2_subdev_fh *fh,
1084 struct v4l2_subdev_format *fmt)
1086 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1087 struct v4l2_mbus_framefmt *mf = &fmt->format;
1088 struct flite_frame *f = &fimc->out_frame;
1090 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1091 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1095 mf->colorspace = V4L2_COLORSPACE_JPEG;
1097 mutex_lock(&fimc->lock);
1098 mf->code = fimc->fmt->mbus_code;
1100 if (fmt->pad == FLITE_SD_PAD_SINK) {
1101 /* full camera input frame size */
1102 mf->width = f->f_width;
1103 mf->height = f->f_height;
1106 mf->width = f->rect.width;
1107 mf->height = f->rect.height;
1109 mutex_unlock(&fimc->lock);
1113 static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd,
1114 struct v4l2_subdev_fh *fh,
1115 struct v4l2_subdev_format *fmt)
1117 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1118 struct v4l2_mbus_framefmt *mf = &fmt->format;
1119 struct flite_frame *sink = &fimc->inp_frame;
1120 struct flite_frame *source = &fimc->out_frame;
1121 const struct fimc_fmt *ffmt;
1123 v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d\n",
1124 fmt->pad, mf->code, mf->width, mf->height);
1126 mf->colorspace = V4L2_COLORSPACE_JPEG;
1127 mutex_lock(&fimc->lock);
1129 if ((atomic_read(&fimc->out_path) == FIMC_IO_ISP &&
1130 sd->entity.stream_count > 0) ||
1131 (atomic_read(&fimc->out_path) == FIMC_IO_DMA &&
1132 vb2_is_busy(&fimc->vb_queue))) {
1133 mutex_unlock(&fimc->lock);
1137 ffmt = fimc_lite_try_format(fimc, &mf->width, &mf->height,
1138 &mf->code, NULL, fmt->pad);
1140 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1141 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1143 mutex_unlock(&fimc->lock);
1147 if (fmt->pad == FLITE_SD_PAD_SINK) {
1148 sink->f_width = mf->width;
1149 sink->f_height = mf->height;
1151 /* Set sink crop rectangle */
1152 sink->rect.width = mf->width;
1153 sink->rect.height = mf->height;
1154 sink->rect.left = 0;
1156 /* Reset source format and crop rectangle */
1157 source->rect = sink->rect;
1158 source->f_width = mf->width;
1159 source->f_height = mf->height;
1161 /* Allow changing format only on sink pad */
1162 mf->code = fimc->fmt->mbus_code;
1163 mf->width = sink->rect.width;
1164 mf->height = sink->rect.height;
1167 mutex_unlock(&fimc->lock);
1171 static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd,
1172 struct v4l2_subdev_fh *fh,
1173 struct v4l2_subdev_selection *sel)
1175 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1176 struct flite_frame *f = &fimc->inp_frame;
1178 if ((sel->target != V4L2_SEL_TGT_CROP &&
1179 sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
1180 sel->pad != FLITE_SD_PAD_SINK)
1183 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1184 sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
1188 mutex_lock(&fimc->lock);
1189 if (sel->target == V4L2_SEL_TGT_CROP) {
1194 sel->r.width = f->f_width;
1195 sel->r.height = f->f_height;
1197 mutex_unlock(&fimc->lock);
1199 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1200 __func__, f->rect.left, f->rect.top, f->rect.width,
1201 f->rect.height, f->f_width, f->f_height);
1206 static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd,
1207 struct v4l2_subdev_fh *fh,
1208 struct v4l2_subdev_selection *sel)
1210 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1211 struct flite_frame *f = &fimc->inp_frame;
1214 if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != FLITE_SD_PAD_SINK)
1217 mutex_lock(&fimc->lock);
1218 fimc_lite_try_crop(fimc, &sel->r);
1220 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1221 *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r;
1223 unsigned long flags;
1224 spin_lock_irqsave(&fimc->slock, flags);
1226 /* Same crop rectangle on the source pad */
1227 fimc->out_frame.rect = sel->r;
1228 set_bit(ST_FLITE_CONFIG, &fimc->state);
1229 spin_unlock_irqrestore(&fimc->slock, flags);
1231 mutex_unlock(&fimc->lock);
1233 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
1234 __func__, f->rect.left, f->rect.top, f->rect.width,
1235 f->rect.height, f->f_width, f->f_height);
1240 static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on)
1242 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1243 unsigned long flags;
1247 * Find sensor subdev linked to FIMC-LITE directly or through
1248 * MIPI-CSIS. This is required for configuration where FIMC-LITE
1249 * is used as a subdev only and feeds data internally to FIMC-IS.
1250 * The pipeline links are protected through entity.stream_count
1251 * so there is no need to take the media graph mutex here.
1253 fimc->sensor = __find_remote_sensor(&sd->entity);
1255 if (atomic_read(&fimc->out_path) != FIMC_IO_ISP)
1256 return -ENOIOCTLCMD;
1258 mutex_lock(&fimc->lock);
1260 flite_hw_reset(fimc);
1261 ret = fimc_lite_hw_init(fimc, true);
1263 spin_lock_irqsave(&fimc->slock, flags);
1264 flite_hw_capture_start(fimc);
1265 spin_unlock_irqrestore(&fimc->slock, flags);
1268 set_bit(ST_FLITE_OFF, &fimc->state);
1270 spin_lock_irqsave(&fimc->slock, flags);
1271 flite_hw_capture_stop(fimc);
1272 spin_unlock_irqrestore(&fimc->slock, flags);
1274 ret = wait_event_timeout(fimc->irq_queue,
1275 !test_bit(ST_FLITE_OFF, &fimc->state),
1276 msecs_to_jiffies(200));
1278 v4l2_err(sd, "s_stream(0) timeout\n");
1279 clear_bit(ST_FLITE_RUN, &fimc->state);
1282 mutex_unlock(&fimc->lock);
1286 static int fimc_lite_log_status(struct v4l2_subdev *sd)
1288 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1290 flite_hw_dump_regs(fimc, __func__);
1294 static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
1296 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1297 struct vb2_queue *q = &fimc->vb_queue;
1298 struct video_device *vfd = &fimc->vfd;
1301 memset(vfd, 0, sizeof(*vfd));
1303 fimc->fmt = &fimc_lite_formats[0];
1304 atomic_set(&fimc->out_path, FIMC_IO_DMA);
1306 snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture",
1309 vfd->fops = &fimc_lite_fops;
1310 vfd->ioctl_ops = &fimc_lite_ioctl_ops;
1311 vfd->v4l2_dev = sd->v4l2_dev;
1313 vfd->release = video_device_release_empty;
1314 vfd->lock = &fimc->lock;
1315 fimc->ref_count = 0;
1316 fimc->reqbufs_count = 0;
1318 INIT_LIST_HEAD(&fimc->pending_buf_q);
1319 INIT_LIST_HEAD(&fimc->active_buf_q);
1321 memset(q, 0, sizeof(*q));
1322 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1323 q->io_modes = VB2_MMAP | VB2_USERPTR;
1324 q->ops = &fimc_lite_qops;
1325 q->mem_ops = &vb2_dma_contig_memops;
1326 q->buf_struct_size = sizeof(struct flite_buffer);
1329 ret = vb2_queue_init(q);
1333 fimc->vd_pad.flags = MEDIA_PAD_FL_SINK;
1334 ret = media_entity_init(&vfd->entity, 1, &fimc->vd_pad, 0);
1338 video_set_drvdata(vfd, fimc);
1339 fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
1341 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1343 media_entity_cleanup(&vfd->entity);
1344 fimc->pipeline_ops = NULL;
1348 v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n",
1349 vfd->name, video_device_node_name(vfd));
1353 static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd)
1355 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1360 if (video_is_registered(&fimc->vfd)) {
1361 video_unregister_device(&fimc->vfd);
1362 media_entity_cleanup(&fimc->vfd.entity);
1363 fimc->pipeline_ops = NULL;
1367 static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = {
1368 .registered = fimc_lite_subdev_registered,
1369 .unregistered = fimc_lite_subdev_unregistered,
1372 static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = {
1373 .enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
1374 .get_selection = fimc_lite_subdev_get_selection,
1375 .set_selection = fimc_lite_subdev_set_selection,
1376 .get_fmt = fimc_lite_subdev_get_fmt,
1377 .set_fmt = fimc_lite_subdev_set_fmt,
1380 static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = {
1381 .s_stream = fimc_lite_subdev_s_stream,
1384 static const struct v4l2_subdev_core_ops fimc_lite_core_ops = {
1385 .log_status = fimc_lite_log_status,
1388 static struct v4l2_subdev_ops fimc_lite_subdev_ops = {
1389 .core = &fimc_lite_core_ops,
1390 .video = &fimc_lite_subdev_video_ops,
1391 .pad = &fimc_lite_subdev_pad_ops,
1394 static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl)
1396 struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite,
1398 set_bit(ST_FLITE_CONFIG, &fimc->state);
1402 static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = {
1403 .s_ctrl = fimc_lite_s_ctrl,
1406 static const struct v4l2_ctrl_config fimc_lite_ctrl = {
1407 .ops = &fimc_lite_ctrl_ops,
1408 .id = V4L2_CTRL_CLASS_USER | 0x1001,
1409 .type = V4L2_CTRL_TYPE_BOOLEAN,
1410 .name = "Test Pattern 640x480",
1414 static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc)
1416 struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler;
1417 struct v4l2_subdev *sd = &fimc->subdev;
1420 v4l2_subdev_init(sd, &fimc_lite_subdev_ops);
1421 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1422 snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index);
1424 fimc->subdev_pads[FLITE_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1425 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_DMA].flags = MEDIA_PAD_FL_SOURCE;
1426 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_ISP].flags = MEDIA_PAD_FL_SOURCE;
1427 ret = media_entity_init(&sd->entity, FLITE_SD_PADS_NUM,
1428 fimc->subdev_pads, 0);
1432 v4l2_ctrl_handler_init(handler, 1);
1433 fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl,
1435 if (handler->error) {
1436 media_entity_cleanup(&sd->entity);
1437 return handler->error;
1440 sd->ctrl_handler = handler;
1441 sd->internal_ops = &fimc_lite_subdev_internal_ops;
1442 sd->entity.ops = &fimc_lite_subdev_media_ops;
1443 v4l2_set_subdevdata(sd, fimc);
1448 static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc)
1450 struct v4l2_subdev *sd = &fimc->subdev;
1452 v4l2_device_unregister_subdev(sd);
1453 media_entity_cleanup(&sd->entity);
1454 v4l2_ctrl_handler_free(&fimc->ctrl_handler);
1455 v4l2_set_subdevdata(sd, NULL);
1458 static void fimc_lite_clk_put(struct fimc_lite *fimc)
1460 if (IS_ERR_OR_NULL(fimc->clock))
1463 clk_unprepare(fimc->clock);
1464 clk_put(fimc->clock);
1468 static int fimc_lite_clk_get(struct fimc_lite *fimc)
1472 fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME);
1473 if (IS_ERR(fimc->clock))
1474 return PTR_ERR(fimc->clock);
1476 ret = clk_prepare(fimc->clock);
1478 clk_put(fimc->clock);
1484 static int fimc_lite_probe(struct platform_device *pdev)
1486 struct flite_drvdata *drv_data = fimc_lite_get_drvdata(pdev);
1487 struct fimc_lite *fimc;
1488 struct resource *res;
1491 fimc = devm_kzalloc(&pdev->dev, sizeof(*fimc), GFP_KERNEL);
1495 fimc->index = pdev->id;
1496 fimc->variant = drv_data->variant[fimc->index];
1499 init_waitqueue_head(&fimc->irq_queue);
1500 spin_lock_init(&fimc->slock);
1501 mutex_init(&fimc->lock);
1503 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1504 fimc->regs = devm_ioremap_resource(&pdev->dev, res);
1505 if (IS_ERR(fimc->regs))
1506 return PTR_ERR(fimc->regs);
1508 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1510 dev_err(&pdev->dev, "Failed to get IRQ resource\n");
1514 ret = fimc_lite_clk_get(fimc);
1518 ret = devm_request_irq(&pdev->dev, res->start, flite_irq_handler,
1519 0, dev_name(&pdev->dev), fimc);
1521 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
1525 /* The video node will be created within the subdev's registered() op */
1526 ret = fimc_lite_create_capture_subdev(fimc);
1530 platform_set_drvdata(pdev, fimc);
1531 pm_runtime_enable(&pdev->dev);
1532 ret = pm_runtime_get_sync(&pdev->dev);
1536 fimc->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1537 if (IS_ERR(fimc->alloc_ctx)) {
1538 ret = PTR_ERR(fimc->alloc_ctx);
1541 pm_runtime_put(&pdev->dev);
1543 dev_dbg(&pdev->dev, "FIMC-LITE.%d registered successfully\n",
1547 pm_runtime_put(&pdev->dev);
1549 fimc_lite_unregister_capture_subdev(fimc);
1551 fimc_lite_clk_put(fimc);
1555 static int fimc_lite_runtime_resume(struct device *dev)
1557 struct fimc_lite *fimc = dev_get_drvdata(dev);
1559 clk_enable(fimc->clock);
1563 static int fimc_lite_runtime_suspend(struct device *dev)
1565 struct fimc_lite *fimc = dev_get_drvdata(dev);
1567 clk_disable(fimc->clock);
1571 #ifdef CONFIG_PM_SLEEP
1572 static int fimc_lite_resume(struct device *dev)
1574 struct fimc_lite *fimc = dev_get_drvdata(dev);
1575 struct flite_buffer *buf;
1576 unsigned long flags;
1579 spin_lock_irqsave(&fimc->slock, flags);
1580 if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
1581 !test_bit(ST_FLITE_IN_USE, &fimc->state)) {
1582 spin_unlock_irqrestore(&fimc->slock, flags);
1585 flite_hw_reset(fimc);
1586 spin_unlock_irqrestore(&fimc->slock, flags);
1588 if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state))
1591 INIT_LIST_HEAD(&fimc->active_buf_q);
1592 fimc_pipeline_call(fimc, open, &fimc->pipeline,
1593 &fimc->vfd.entity, false);
1594 fimc_lite_hw_init(fimc, atomic_read(&fimc->out_path) == FIMC_IO_ISP);
1595 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
1597 for (i = 0; i < fimc->reqbufs_count; i++) {
1598 if (list_empty(&fimc->pending_buf_q))
1600 buf = fimc_lite_pending_queue_pop(fimc);
1601 buffer_queue(&buf->vb);
1606 static int fimc_lite_suspend(struct device *dev)
1608 struct fimc_lite *fimc = dev_get_drvdata(dev);
1609 bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state);
1612 if (test_and_set_bit(ST_LPM, &fimc->state))
1615 ret = fimc_lite_stop_capture(fimc, suspend);
1616 if (ret < 0 || !fimc_lite_active(fimc))
1619 return fimc_pipeline_call(fimc, close, &fimc->pipeline);
1621 #endif /* CONFIG_PM_SLEEP */
1623 static int fimc_lite_remove(struct platform_device *pdev)
1625 struct fimc_lite *fimc = platform_get_drvdata(pdev);
1626 struct device *dev = &pdev->dev;
1628 pm_runtime_disable(dev);
1629 pm_runtime_set_suspended(dev);
1630 fimc_lite_unregister_capture_subdev(fimc);
1631 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
1632 fimc_lite_clk_put(fimc);
1634 dev_info(dev, "Driver unloaded\n");
1638 static struct flite_variant fimc_lite0_variant_exynos4 = {
1641 .out_width_align = 8,
1642 .win_hor_offs_align = 2,
1643 .out_hor_offs_align = 8,
1646 /* EXYNOS4212, EXYNOS4412 */
1647 static struct flite_drvdata fimc_lite_drvdata_exynos4 = {
1649 [0] = &fimc_lite0_variant_exynos4,
1650 [1] = &fimc_lite0_variant_exynos4,
1654 static struct platform_device_id fimc_lite_driver_ids[] = {
1656 .name = "exynos-fimc-lite",
1657 .driver_data = (unsigned long)&fimc_lite_drvdata_exynos4,
1661 MODULE_DEVICE_TABLE(platform, fimc_lite_driver_ids);
1663 static const struct dev_pm_ops fimc_lite_pm_ops = {
1664 SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume)
1665 SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume,
1669 static struct platform_driver fimc_lite_driver = {
1670 .probe = fimc_lite_probe,
1671 .remove = fimc_lite_remove,
1672 .id_table = fimc_lite_driver_ids,
1674 .name = FIMC_LITE_DRV_NAME,
1675 .owner = THIS_MODULE,
1676 .pm = &fimc_lite_pm_ops,
1679 module_platform_driver(fimc_lite_driver);
1680 MODULE_LICENSE("GPL");
1681 MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME);