[media] exynos4-is: Remove redundant MODULE_DEVICE_TABLE entries
[deliverable/linux.git] / drivers / media / platform / exynos4-is / fimc-lite.c
CommitLineData
4af81310
SN
1/*
2 * Samsung EXYNOS FIMC-LITE (camera host interface) driver
3*
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#define pr_fmt(fmt) "%s:%d " fmt, __func__, __LINE__
12
13#include <linux/bug.h>
4c8f0629 14#include <linux/clk.h>
4af81310
SN
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>
eb62d9e9 21#include <linux/of.h>
4af81310
SN
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>
27
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>
b9ee31e6 33#include <media/s5p_fimc.h>
4af81310 34
4c8f0629 35#include "fimc-core.h"
b9ee31e6 36#include "fimc-lite.h"
4af81310
SN
37#include "fimc-lite-reg.h"
38
39static int debug;
40module_param(debug, int, 0644);
41
42static const struct fimc_fmt fimc_lite_formats[] = {
43 {
44 .name = "YUV 4:2:2 packed, YCbYCr",
45 .fourcc = V4L2_PIX_FMT_YUYV,
46 .depth = { 16 },
47 .color = FIMC_FMT_YCBYCR422,
48 .memplanes = 1,
49 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
e90ad659 50 .flags = FMT_FLAGS_YUV,
4af81310
SN
51 }, {
52 .name = "YUV 4:2:2 packed, CbYCrY",
53 .fourcc = V4L2_PIX_FMT_UYVY,
54 .depth = { 16 },
55 .color = FIMC_FMT_CBYCRY422,
56 .memplanes = 1,
57 .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
e90ad659 58 .flags = FMT_FLAGS_YUV,
4af81310
SN
59 }, {
60 .name = "YUV 4:2:2 packed, CrYCbY",
61 .fourcc = V4L2_PIX_FMT_VYUY,
62 .depth = { 16 },
63 .color = FIMC_FMT_CRYCBY422,
64 .memplanes = 1,
65 .mbus_code = V4L2_MBUS_FMT_VYUY8_2X8,
e90ad659 66 .flags = FMT_FLAGS_YUV,
4af81310
SN
67 }, {
68 .name = "YUV 4:2:2 packed, YCrYCb",
69 .fourcc = V4L2_PIX_FMT_YVYU,
70 .depth = { 16 },
71 .color = FIMC_FMT_YCRYCB422,
72 .memplanes = 1,
73 .mbus_code = V4L2_MBUS_FMT_YVYU8_2X8,
e90ad659 74 .flags = FMT_FLAGS_YUV,
4af81310
SN
75 }, {
76 .name = "RAW8 (GRBG)",
77 .fourcc = V4L2_PIX_FMT_SGRBG8,
78 .depth = { 8 },
79 .color = FIMC_FMT_RAW8,
80 .memplanes = 1,
81 .mbus_code = V4L2_MBUS_FMT_SGRBG8_1X8,
e90ad659 82 .flags = FMT_FLAGS_RAW_BAYER,
4af81310
SN
83 }, {
84 .name = "RAW10 (GRBG)",
85 .fourcc = V4L2_PIX_FMT_SGRBG10,
86 .depth = { 10 },
87 .color = FIMC_FMT_RAW10,
88 .memplanes = 1,
89 .mbus_code = V4L2_MBUS_FMT_SGRBG10_1X10,
e90ad659 90 .flags = FMT_FLAGS_RAW_BAYER,
4af81310
SN
91 }, {
92 .name = "RAW12 (GRBG)",
93 .fourcc = V4L2_PIX_FMT_SGRBG12,
94 .depth = { 12 },
95 .color = FIMC_FMT_RAW12,
96 .memplanes = 1,
97 .mbus_code = V4L2_MBUS_FMT_SGRBG12_1X12,
e90ad659 98 .flags = FMT_FLAGS_RAW_BAYER,
4af81310
SN
99 },
100};
101
102/**
103 * fimc_lite_find_format - lookup fimc color format by fourcc or media bus code
104 * @pixelformat: fourcc to match, ignored if null
105 * @mbus_code: media bus code to match, ignored if null
e90ad659 106 * @mask: the color format flags to match
4af81310
SN
107 * @index: index to the fimc_lite_formats array, ignored if negative
108 */
109static const struct fimc_fmt *fimc_lite_find_format(const u32 *pixelformat,
e90ad659 110 const u32 *mbus_code, unsigned int mask, int index)
4af81310
SN
111{
112 const struct fimc_fmt *fmt, *def_fmt = NULL;
113 unsigned int i;
114 int id = 0;
115
116 if (index >= (int)ARRAY_SIZE(fimc_lite_formats))
117 return NULL;
118
119 for (i = 0; i < ARRAY_SIZE(fimc_lite_formats); ++i) {
120 fmt = &fimc_lite_formats[i];
e90ad659
SN
121 if (mask && !(fmt->flags & mask))
122 continue;
4af81310
SN
123 if (pixelformat && fmt->fourcc == *pixelformat)
124 return fmt;
125 if (mbus_code && fmt->mbus_code == *mbus_code)
126 return fmt;
127 if (index == id)
128 def_fmt = fmt;
129 id++;
130 }
131 return def_fmt;
132}
133
756e6e14
SN
134/* Called with the media graph mutex held or @me stream_count > 0. */
135static struct v4l2_subdev *__find_remote_sensor(struct media_entity *me)
136{
137 struct media_pad *pad = &me->pads[0];
138 struct v4l2_subdev *sd;
139
140 while (pad->flags & MEDIA_PAD_FL_SINK) {
141 /* source pad */
142 pad = media_entity_remote_source(pad);
143 if (pad == NULL ||
144 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
145 break;
146
147 sd = media_entity_to_v4l2_subdev(pad->entity);
148
149 if (sd->grp_id == GRP_ID_FIMC_IS_SENSOR ||
150 sd->grp_id == GRP_ID_SENSOR)
151 return sd;
152 /* sink pad */
153 pad = &sd->entity.pads[0];
154 }
155 return NULL;
156}
157
6319d6a0 158static int fimc_lite_hw_init(struct fimc_lite *fimc, bool isp_output)
4af81310 159{
4c8f0629 160 struct fimc_source_info *si;
4af81310
SN
161 unsigned long flags;
162
756e6e14 163 if (fimc->sensor == NULL)
4af81310
SN
164 return -ENXIO;
165
e90ad659 166 if (fimc->inp_frame.fmt == NULL || fimc->out_frame.fmt == NULL)
4af81310
SN
167 return -EINVAL;
168
6319d6a0 169 /* Get sensor configuration data from the sensor subdev */
756e6e14 170 si = v4l2_get_subdev_hostdata(fimc->sensor);
4c8f0629
SN
171 if (!si)
172 return -EINVAL;
173
4af81310
SN
174 spin_lock_irqsave(&fimc->slock, flags);
175
4c8f0629 176 flite_hw_set_camera_bus(fimc, si);
4af81310
SN
177 flite_hw_set_source_format(fimc, &fimc->inp_frame);
178 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
6319d6a0 179 flite_hw_set_output_dma(fimc, &fimc->out_frame, !isp_output);
4af81310
SN
180 flite_hw_set_interrupt_mask(fimc);
181 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
182
183 if (debug > 0)
184 flite_hw_dump_regs(fimc, __func__);
185
186 spin_unlock_irqrestore(&fimc->slock, flags);
187 return 0;
188}
189
190/*
191 * Reinitialize the driver so it is ready to start the streaming again.
192 * Set fimc->state to indicate stream off and the hardware shut down state.
193 * If not suspending (@suspend is false), return any buffers to videobuf2.
194 * Otherwise put any owned buffers onto the pending buffers queue, so they
195 * can be re-spun when the device is being resumed. Also perform FIMC
196 * software reset and disable streaming on the whole pipeline if required.
197 */
198static int fimc_lite_reinit(struct fimc_lite *fimc, bool suspend)
199{
200 struct flite_buffer *buf;
201 unsigned long flags;
202 bool streaming;
203
204 spin_lock_irqsave(&fimc->slock, flags);
205 streaming = fimc->state & (1 << ST_SENSOR_STREAM);
206
207 fimc->state &= ~(1 << ST_FLITE_RUN | 1 << ST_FLITE_OFF |
208 1 << ST_FLITE_STREAM | 1 << ST_SENSOR_STREAM);
209 if (suspend)
210 fimc->state |= (1 << ST_FLITE_SUSPENDED);
211 else
212 fimc->state &= ~(1 << ST_FLITE_PENDING |
213 1 << ST_FLITE_SUSPENDED);
214
215 /* Release unused buffers */
216 while (!suspend && !list_empty(&fimc->pending_buf_q)) {
217 buf = fimc_lite_pending_queue_pop(fimc);
218 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
219 }
220 /* If suspending put unused buffers onto pending queue */
221 while (!list_empty(&fimc->active_buf_q)) {
222 buf = fimc_lite_active_queue_pop(fimc);
223 if (suspend)
224 fimc_lite_pending_queue_add(fimc, buf);
225 else
226 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
227 }
228
229 spin_unlock_irqrestore(&fimc->slock, flags);
230
231 flite_hw_reset(fimc);
232
233 if (!streaming)
234 return 0;
235
b9ee31e6 236 return fimc_pipeline_call(fimc, set_stream, &fimc->pipeline, 0);
4af81310
SN
237}
238
239static int fimc_lite_stop_capture(struct fimc_lite *fimc, bool suspend)
240{
241 unsigned long flags;
242
243 if (!fimc_lite_active(fimc))
244 return 0;
245
246 spin_lock_irqsave(&fimc->slock, flags);
247 set_bit(ST_FLITE_OFF, &fimc->state);
248 flite_hw_capture_stop(fimc);
249 spin_unlock_irqrestore(&fimc->slock, flags);
250
251 wait_event_timeout(fimc->irq_queue,
252 !test_bit(ST_FLITE_OFF, &fimc->state),
253 (2*HZ/10)); /* 200 ms */
254
255 return fimc_lite_reinit(fimc, suspend);
256}
257
258/* Must be called with fimc.slock spinlock held. */
259static void fimc_lite_config_update(struct fimc_lite *fimc)
260{
261 flite_hw_set_window_offset(fimc, &fimc->inp_frame);
262 flite_hw_set_dma_window(fimc, &fimc->out_frame);
263 flite_hw_set_test_pattern(fimc, fimc->test_pattern->val);
264 clear_bit(ST_FLITE_CONFIG, &fimc->state);
265}
266
267static irqreturn_t flite_irq_handler(int irq, void *priv)
268{
269 struct fimc_lite *fimc = priv;
270 struct flite_buffer *vbuf;
271 unsigned long flags;
272 struct timeval *tv;
273 struct timespec ts;
274 u32 intsrc;
275
276 spin_lock_irqsave(&fimc->slock, flags);
277
278 intsrc = flite_hw_get_interrupt_source(fimc);
279 flite_hw_clear_pending_irq(fimc);
280
281 if (test_and_clear_bit(ST_FLITE_OFF, &fimc->state)) {
282 wake_up(&fimc->irq_queue);
283 goto done;
284 }
285
286 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_OVERFLOW) {
287 clear_bit(ST_FLITE_RUN, &fimc->state);
288 fimc->events.data_overflow++;
289 }
290
291 if (intsrc & FLITE_REG_CISTATUS_IRQ_SRC_LASTCAPEND) {
292 flite_hw_clear_last_capture_end(fimc);
293 clear_bit(ST_FLITE_STREAM, &fimc->state);
294 wake_up(&fimc->irq_queue);
295 }
296
03878bb4 297 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA)
4af81310
SN
298 goto done;
299
300 if ((intsrc & FLITE_REG_CISTATUS_IRQ_SRC_FRMSTART) &&
301 test_bit(ST_FLITE_RUN, &fimc->state) &&
302 !list_empty(&fimc->active_buf_q) &&
303 !list_empty(&fimc->pending_buf_q)) {
304 vbuf = fimc_lite_active_queue_pop(fimc);
305 ktime_get_ts(&ts);
306 tv = &vbuf->vb.v4l2_buf.timestamp;
307 tv->tv_sec = ts.tv_sec;
308 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
309 vbuf->vb.v4l2_buf.sequence = fimc->frame_count++;
310 vb2_buffer_done(&vbuf->vb, VB2_BUF_STATE_DONE);
311
312 vbuf = fimc_lite_pending_queue_pop(fimc);
313 flite_hw_set_output_addr(fimc, vbuf->paddr);
314 fimc_lite_active_queue_add(fimc, vbuf);
315 }
316
317 if (test_bit(ST_FLITE_CONFIG, &fimc->state))
318 fimc_lite_config_update(fimc);
319
320 if (list_empty(&fimc->pending_buf_q)) {
321 flite_hw_capture_stop(fimc);
322 clear_bit(ST_FLITE_STREAM, &fimc->state);
323 }
324done:
325 set_bit(ST_FLITE_RUN, &fimc->state);
326 spin_unlock_irqrestore(&fimc->slock, flags);
327 return IRQ_HANDLED;
328}
329
330static int start_streaming(struct vb2_queue *q, unsigned int count)
331{
332 struct fimc_lite *fimc = q->drv_priv;
333 int ret;
334
335 fimc->frame_count = 0;
336
6319d6a0 337 ret = fimc_lite_hw_init(fimc, false);
4af81310
SN
338 if (ret) {
339 fimc_lite_reinit(fimc, false);
340 return ret;
341 }
342
343 set_bit(ST_FLITE_PENDING, &fimc->state);
344
345 if (!list_empty(&fimc->active_buf_q) &&
346 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
347 flite_hw_capture_start(fimc);
348
349 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
b9ee31e6
SN
350 fimc_pipeline_call(fimc, set_stream,
351 &fimc->pipeline, 1);
4af81310
SN
352 }
353 if (debug > 0)
354 flite_hw_dump_regs(fimc, __func__);
355
356 return 0;
357}
358
359static int stop_streaming(struct vb2_queue *q)
360{
361 struct fimc_lite *fimc = q->drv_priv;
362
363 if (!fimc_lite_active(fimc))
364 return -EINVAL;
365
366 return fimc_lite_stop_capture(fimc, false);
367}
368
369static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
370 unsigned int *num_buffers, unsigned int *num_planes,
371 unsigned int sizes[], void *allocators[])
372{
373 const struct v4l2_pix_format_mplane *pixm = NULL;
374 struct fimc_lite *fimc = vq->drv_priv;
375 struct flite_frame *frame = &fimc->out_frame;
e90ad659 376 const struct fimc_fmt *fmt = frame->fmt;
4af81310
SN
377 unsigned long wh;
378 int i;
379
380 if (pfmt) {
381 pixm = &pfmt->fmt.pix_mp;
e90ad659 382 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL, 0, -1);
4af81310
SN
383 wh = pixm->width * pixm->height;
384 } else {
385 wh = frame->f_width * frame->f_height;
386 }
387
388 if (fmt == NULL)
389 return -EINVAL;
390
391 *num_planes = fmt->memplanes;
392
393 for (i = 0; i < fmt->memplanes; i++) {
394 unsigned int size = (wh * fmt->depth[i]) / 8;
395 if (pixm)
396 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
397 else
398 sizes[i] = size;
399 allocators[i] = fimc->alloc_ctx;
400 }
401
402 return 0;
403}
404
405static int buffer_prepare(struct vb2_buffer *vb)
406{
407 struct vb2_queue *vq = vb->vb2_queue;
408 struct fimc_lite *fimc = vq->drv_priv;
409 int i;
410
e90ad659 411 if (fimc->out_frame.fmt == NULL)
4af81310
SN
412 return -EINVAL;
413
e90ad659 414 for (i = 0; i < fimc->out_frame.fmt->memplanes; i++) {
4af81310
SN
415 unsigned long size = fimc->payload[i];
416
417 if (vb2_plane_size(vb, i) < size) {
1bcd7041 418 v4l2_err(&fimc->vfd,
4af81310
SN
419 "User buffer too small (%ld < %ld)\n",
420 vb2_plane_size(vb, i), size);
421 return -EINVAL;
422 }
423 vb2_set_plane_payload(vb, i, size);
424 }
425
426 return 0;
427}
428
429static void buffer_queue(struct vb2_buffer *vb)
430{
431 struct flite_buffer *buf
432 = container_of(vb, struct flite_buffer, vb);
433 struct fimc_lite *fimc = vb2_get_drv_priv(vb->vb2_queue);
434 unsigned long flags;
435
436 spin_lock_irqsave(&fimc->slock, flags);
437 buf->paddr = vb2_dma_contig_plane_dma_addr(vb, 0);
438
439 if (!test_bit(ST_FLITE_SUSPENDED, &fimc->state) &&
440 !test_bit(ST_FLITE_STREAM, &fimc->state) &&
441 list_empty(&fimc->active_buf_q)) {
442 flite_hw_set_output_addr(fimc, buf->paddr);
443 fimc_lite_active_queue_add(fimc, buf);
444 } else {
445 fimc_lite_pending_queue_add(fimc, buf);
446 }
447
448 if (vb2_is_streaming(&fimc->vb_queue) &&
449 !list_empty(&fimc->pending_buf_q) &&
450 !test_and_set_bit(ST_FLITE_STREAM, &fimc->state)) {
451 flite_hw_capture_start(fimc);
452 spin_unlock_irqrestore(&fimc->slock, flags);
453
454 if (!test_and_set_bit(ST_SENSOR_STREAM, &fimc->state))
b9ee31e6
SN
455 fimc_pipeline_call(fimc, set_stream,
456 &fimc->pipeline, 1);
4af81310
SN
457 return;
458 }
459 spin_unlock_irqrestore(&fimc->slock, flags);
460}
461
4af81310
SN
462static const struct vb2_ops fimc_lite_qops = {
463 .queue_setup = queue_setup,
464 .buf_prepare = buffer_prepare,
465 .buf_queue = buffer_queue,
ee12b049
SN
466 .wait_prepare = vb2_ops_wait_prepare,
467 .wait_finish = vb2_ops_wait_finish,
4af81310
SN
468 .start_streaming = start_streaming,
469 .stop_streaming = stop_streaming,
470};
471
472static void fimc_lite_clear_event_counters(struct fimc_lite *fimc)
473{
474 unsigned long flags;
475
476 spin_lock_irqsave(&fimc->slock, flags);
477 memset(&fimc->events, 0, sizeof(fimc->events));
478 spin_unlock_irqrestore(&fimc->slock, flags);
479}
480
481static int fimc_lite_open(struct file *file)
482{
483 struct fimc_lite *fimc = video_drvdata(file);
740ad921 484 struct media_entity *me = &fimc->vfd.entity;
e3fc82e8 485 int ret;
4af81310 486
740ad921 487 mutex_lock(&me->parent->graph_mutex);
4af81310 488
740ad921 489 mutex_lock(&fimc->lock);
03878bb4 490 if (atomic_read(&fimc->out_path) != FIMC_IO_DMA) {
6319d6a0 491 ret = -EBUSY;
ee12b049 492 goto unlock;
6319d6a0
SN
493 }
494
4af81310 495 set_bit(ST_FLITE_IN_USE, &fimc->state);
e3fc82e8
SN
496 ret = pm_runtime_get_sync(&fimc->pdev->dev);
497 if (ret < 0)
ee12b049 498 goto unlock;
4af81310 499
e3fc82e8
SN
500 ret = v4l2_fh_open(file);
501 if (ret < 0)
ee12b049 502 goto err_pm;
4af81310 503
ee12b049
SN
504 if (!v4l2_fh_is_singular_file(file) ||
505 atomic_read(&fimc->out_path) != FIMC_IO_DMA)
506 goto unlock;
4af81310 507
ee12b049
SN
508 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
509 me, true);
510 if (!ret) {
4e39da01 511 fimc_lite_clear_event_counters(fimc);
ee12b049
SN
512 fimc->ref_count++;
513 goto unlock;
4e39da01 514 }
ee12b049
SN
515
516 v4l2_fh_release(file);
517err_pm:
518 pm_runtime_put_sync(&fimc->pdev->dev);
519 clear_bit(ST_FLITE_IN_USE, &fimc->state);
520unlock:
4e39da01 521 mutex_unlock(&fimc->lock);
740ad921 522 mutex_unlock(&me->parent->graph_mutex);
4af81310
SN
523 return ret;
524}
525
ee12b049 526static int fimc_lite_release(struct file *file)
4af81310
SN
527{
528 struct fimc_lite *fimc = video_drvdata(file);
4e39da01 529
ddc43d6d 530 mutex_lock(&fimc->lock);
4af81310 531
ee12b049 532 if (v4l2_fh_is_singular_file(file) &&
03878bb4 533 atomic_read(&fimc->out_path) == FIMC_IO_DMA) {
9ea89e2b
SN
534 if (fimc->streaming) {
535 media_entity_pipeline_stop(&fimc->vfd.entity);
536 fimc->streaming = false;
537 }
4af81310
SN
538 clear_bit(ST_FLITE_IN_USE, &fimc->state);
539 fimc_lite_stop_capture(fimc, false);
b9ee31e6 540 fimc_pipeline_call(fimc, close, &fimc->pipeline);
ee12b049 541 fimc->ref_count--;
4af81310
SN
542 }
543
ee12b049 544 vb2_fop_release(file);
4af81310 545 pm_runtime_put(&fimc->pdev->dev);
ee12b049 546 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
4af81310 547
4e39da01 548 mutex_unlock(&fimc->lock);
ee12b049 549 return 0;
4af81310
SN
550}
551
552static const struct v4l2_file_operations fimc_lite_fops = {
553 .owner = THIS_MODULE,
554 .open = fimc_lite_open,
ee12b049
SN
555 .release = fimc_lite_release,
556 .poll = vb2_fop_poll,
4af81310 557 .unlocked_ioctl = video_ioctl2,
ee12b049 558 .mmap = vb2_fop_mmap,
4af81310
SN
559};
560
561/*
562 * Format and crop negotiation helpers
563 */
564
565static const struct fimc_fmt *fimc_lite_try_format(struct fimc_lite *fimc,
566 u32 *width, u32 *height,
567 u32 *code, u32 *fourcc, int pad)
568{
9c8399c8 569 struct flite_drvdata *dd = fimc->dd;
4af81310 570 const struct fimc_fmt *fmt;
e90ad659 571 unsigned int flags = 0;
4af81310
SN
572
573 if (pad == FLITE_SD_PAD_SINK) {
9c8399c8
SN
574 v4l_bound_align_image(width, 8, dd->max_width,
575 ffs(dd->out_width_align) - 1,
576 height, 0, dd->max_height, 0, 0);
4af81310
SN
577 } else {
578 v4l_bound_align_image(width, 8, fimc->inp_frame.rect.width,
9c8399c8 579 ffs(dd->out_width_align) - 1,
4af81310
SN
580 height, 0, fimc->inp_frame.rect.height,
581 0, 0);
e90ad659 582 flags = fimc->inp_frame.fmt->flags;
4af81310
SN
583 }
584
e90ad659
SN
585 fmt = fimc_lite_find_format(fourcc, code, flags, 0);
586 if (WARN_ON(!fmt))
587 return NULL;
588
589 if (code)
590 *code = fmt->mbus_code;
591 if (fourcc)
592 *fourcc = fmt->fourcc;
593
4af81310
SN
594 v4l2_dbg(1, debug, &fimc->subdev, "code: 0x%x, %dx%d\n",
595 code ? *code : 0, *width, *height);
596
597 return fmt;
598}
599
600static void fimc_lite_try_crop(struct fimc_lite *fimc, struct v4l2_rect *r)
601{
602 struct flite_frame *frame = &fimc->inp_frame;
603
604 v4l_bound_align_image(&r->width, 0, frame->f_width, 0,
605 &r->height, 0, frame->f_height, 0, 0);
606
607 /* Adjust left/top if cropping rectangle got out of bounds */
608 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
9c8399c8 609 r->left = round_down(r->left, fimc->dd->win_hor_offs_align);
4af81310
SN
610 r->top = clamp_t(u32, r->top, 0, frame->f_height - r->height);
611
969e877c 612 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, sink fmt: %dx%d\n",
4af81310
SN
613 r->left, r->top, r->width, r->height,
614 frame->f_width, frame->f_height);
615}
616
617static void fimc_lite_try_compose(struct fimc_lite *fimc, struct v4l2_rect *r)
618{
619 struct flite_frame *frame = &fimc->out_frame;
620 struct v4l2_rect *crop_rect = &fimc->inp_frame.rect;
621
622 /* Scaling is not supported so we enforce compose rectangle size
623 same as size of the sink crop rectangle. */
624 r->width = crop_rect->width;
625 r->height = crop_rect->height;
626
627 /* Adjust left/top if the composing rectangle got out of bounds */
628 r->left = clamp_t(u32, r->left, 0, frame->f_width - r->width);
9c8399c8 629 r->left = round_down(r->left, fimc->dd->out_hor_offs_align);
4af81310
SN
630 r->top = clamp_t(u32, r->top, 0, fimc->out_frame.f_height - r->height);
631
969e877c 632 v4l2_dbg(1, debug, &fimc->subdev, "(%d,%d)/%dx%d, source fmt: %dx%d\n",
4af81310
SN
633 r->left, r->top, r->width, r->height,
634 frame->f_width, frame->f_height);
635}
636
637/*
638 * Video node ioctl operations
639 */
640static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
641 struct v4l2_capability *cap)
642{
643 strlcpy(cap->driver, FIMC_LITE_DRV_NAME, sizeof(cap->driver));
644 cap->bus_info[0] = 0;
645 cap->card[0] = 0;
646 cap->capabilities = V4L2_CAP_STREAMING;
647 return 0;
648}
649
650static int fimc_lite_enum_fmt_mplane(struct file *file, void *priv,
651 struct v4l2_fmtdesc *f)
652{
653 const struct fimc_fmt *fmt;
654
655 if (f->index >= ARRAY_SIZE(fimc_lite_formats))
656 return -EINVAL;
657
658 fmt = &fimc_lite_formats[f->index];
659 strlcpy(f->description, fmt->name, sizeof(f->description));
660 f->pixelformat = fmt->fourcc;
661
662 return 0;
663}
664
665static int fimc_lite_g_fmt_mplane(struct file *file, void *fh,
666 struct v4l2_format *f)
667{
668 struct fimc_lite *fimc = video_drvdata(file);
669 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
670 struct v4l2_plane_pix_format *plane_fmt = &pixm->plane_fmt[0];
671 struct flite_frame *frame = &fimc->out_frame;
e90ad659 672 const struct fimc_fmt *fmt = frame->fmt;
4af81310
SN
673
674 plane_fmt->bytesperline = (frame->f_width * fmt->depth[0]) / 8;
675 plane_fmt->sizeimage = plane_fmt->bytesperline * frame->f_height;
676
677 pixm->num_planes = fmt->memplanes;
678 pixm->pixelformat = fmt->fourcc;
679 pixm->width = frame->f_width;
680 pixm->height = frame->f_height;
681 pixm->field = V4L2_FIELD_NONE;
682 pixm->colorspace = V4L2_COLORSPACE_JPEG;
683 return 0;
684}
685
686static int fimc_lite_try_fmt(struct fimc_lite *fimc,
687 struct v4l2_pix_format_mplane *pixm,
688 const struct fimc_fmt **ffmt)
689{
4af81310 690 u32 bpl = pixm->plane_fmt[0].bytesperline;
9c8399c8 691 struct flite_drvdata *dd = fimc->dd;
e90ad659 692 const struct fimc_fmt *inp_fmt = fimc->inp_frame.fmt;
4af81310
SN
693 const struct fimc_fmt *fmt;
694
e90ad659
SN
695 if (WARN_ON(inp_fmt == NULL))
696 return -EINVAL;
697 /*
698 * We allow some flexibility only for YUV formats. In case of raw
699 * raw Bayer the FIMC-LITE's output format must match its camera
700 * interface input format.
701 */
702 if (inp_fmt->flags & FMT_FLAGS_YUV)
703 fmt = fimc_lite_find_format(&pixm->pixelformat, NULL,
704 inp_fmt->flags, 0);
705 else
706 fmt = inp_fmt;
707
4af81310
SN
708 if (WARN_ON(fmt == NULL))
709 return -EINVAL;
710 if (ffmt)
711 *ffmt = fmt;
9c8399c8
SN
712 v4l_bound_align_image(&pixm->width, 8, dd->max_width,
713 ffs(dd->out_width_align) - 1,
714 &pixm->height, 0, dd->max_height, 0, 0);
4af81310
SN
715
716 if ((bpl == 0 || ((bpl * 8) / fmt->depth[0]) < pixm->width))
717 pixm->plane_fmt[0].bytesperline = (pixm->width *
718 fmt->depth[0]) / 8;
719
720 if (pixm->plane_fmt[0].sizeimage == 0)
721 pixm->plane_fmt[0].sizeimage = (pixm->width * pixm->height *
722 fmt->depth[0]) / 8;
723 pixm->num_planes = fmt->memplanes;
724 pixm->pixelformat = fmt->fourcc;
725 pixm->colorspace = V4L2_COLORSPACE_JPEG;
726 pixm->field = V4L2_FIELD_NONE;
727 return 0;
728}
729
730static int fimc_lite_try_fmt_mplane(struct file *file, void *fh,
731 struct v4l2_format *f)
732{
733 struct fimc_lite *fimc = video_drvdata(file);
4af81310
SN
734 return fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, NULL);
735}
736
737static int fimc_lite_s_fmt_mplane(struct file *file, void *priv,
738 struct v4l2_format *f)
739{
740 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
741 struct fimc_lite *fimc = video_drvdata(file);
742 struct flite_frame *frame = &fimc->out_frame;
743 const struct fimc_fmt *fmt = NULL;
744 int ret;
745
746 if (vb2_is_busy(&fimc->vb_queue))
747 return -EBUSY;
748
749 ret = fimc_lite_try_fmt(fimc, &f->fmt.pix_mp, &fmt);
750 if (ret < 0)
751 return ret;
752
e90ad659 753 frame->fmt = fmt;
4af81310
SN
754 fimc->payload[0] = max((pixm->width * pixm->height * fmt->depth[0]) / 8,
755 pixm->plane_fmt[0].sizeimage);
756 frame->f_width = pixm->width;
757 frame->f_height = pixm->height;
758
759 return 0;
760}
761
762static int fimc_pipeline_validate(struct fimc_lite *fimc)
763{
764 struct v4l2_subdev *sd = &fimc->subdev;
765 struct v4l2_subdev_format sink_fmt, src_fmt;
766 struct media_pad *pad;
767 int ret;
768
769 while (1) {
770 /* Retrieve format at the sink pad */
771 pad = &sd->entity.pads[0];
772 if (!(pad->flags & MEDIA_PAD_FL_SINK))
773 break;
774 /* Don't call FIMC subdev operation to avoid nested locking */
775 if (sd == &fimc->subdev) {
776 struct flite_frame *ff = &fimc->out_frame;
777 sink_fmt.format.width = ff->f_width;
778 sink_fmt.format.height = ff->f_height;
e90ad659 779 sink_fmt.format.code = fimc->inp_frame.fmt->mbus_code;
4af81310
SN
780 } else {
781 sink_fmt.pad = pad->index;
782 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
783 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL,
784 &sink_fmt);
785 if (ret < 0 && ret != -ENOIOCTLCMD)
786 return -EPIPE;
787 }
788 /* Retrieve format at the source pad */
789 pad = media_entity_remote_source(pad);
790 if (pad == NULL ||
791 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
792 break;
793
794 sd = media_entity_to_v4l2_subdev(pad->entity);
795 src_fmt.pad = pad->index;
796 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
797 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
798 if (ret < 0 && ret != -ENOIOCTLCMD)
799 return -EPIPE;
800
801 if (src_fmt.format.width != sink_fmt.format.width ||
802 src_fmt.format.height != sink_fmt.format.height ||
803 src_fmt.format.code != sink_fmt.format.code)
804 return -EPIPE;
805 }
806 return 0;
807}
808
809static int fimc_lite_streamon(struct file *file, void *priv,
810 enum v4l2_buf_type type)
811{
812 struct fimc_lite *fimc = video_drvdata(file);
95c4a17f 813 struct media_entity *entity = &fimc->vfd.entity;
4af81310
SN
814 struct fimc_pipeline *p = &fimc->pipeline;
815 int ret;
816
817 if (fimc_lite_active(fimc))
818 return -EBUSY;
819
95c4a17f 820 ret = media_entity_pipeline_start(entity, p->m_pipeline);
a1a5861b
SK
821 if (ret < 0)
822 return ret;
4af81310
SN
823
824 ret = fimc_pipeline_validate(fimc);
ee12b049
SN
825 if (ret < 0)
826 goto err_p_stop;
4af81310 827
756e6e14
SN
828 fimc->sensor = __find_remote_sensor(&fimc->subdev.entity);
829
ee12b049 830 ret = vb2_ioctl_streamon(file, priv, type);
9ea89e2b
SN
831 if (!ret) {
832 fimc->streaming = true;
ee12b049 833 return ret;
9ea89e2b
SN
834 }
835
ee12b049
SN
836err_p_stop:
837 media_entity_pipeline_stop(entity);
838 return 0;
4af81310
SN
839}
840
841static int fimc_lite_streamoff(struct file *file, void *priv,
842 enum v4l2_buf_type type)
843{
844 struct fimc_lite *fimc = video_drvdata(file);
4af81310
SN
845 int ret;
846
ee12b049 847 ret = vb2_ioctl_streamoff(file, priv, type);
9ea89e2b
SN
848 if (ret < 0)
849 return ret;
850
851 media_entity_pipeline_stop(&fimc->vfd.entity);
852 fimc->streaming = false;
853 return 0;
4af81310
SN
854}
855
856static int fimc_lite_reqbufs(struct file *file, void *priv,
857 struct v4l2_requestbuffers *reqbufs)
858{
859 struct fimc_lite *fimc = video_drvdata(file);
860 int ret;
861
862 reqbufs->count = max_t(u32, FLITE_REQ_BUFS_MIN, reqbufs->count);
ee12b049 863 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
f68247fc 864 if (!ret)
4af81310
SN
865 fimc->reqbufs_count = reqbufs->count;
866
867 return ret;
868}
869
4af81310
SN
870/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
871static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
872{
873 if (a->left < b->left || a->top < b->top)
874 return 0;
875 if (a->left + a->width > b->left + b->width)
876 return 0;
877 if (a->top + a->height > b->top + b->height)
878 return 0;
879
880 return 1;
881}
882
883static int fimc_lite_g_selection(struct file *file, void *fh,
884 struct v4l2_selection *sel)
885{
886 struct fimc_lite *fimc = video_drvdata(file);
887 struct flite_frame *f = &fimc->out_frame;
888
889 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
890 return -EINVAL;
891
892 switch (sel->target) {
893 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
894 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
895 sel->r.left = 0;
896 sel->r.top = 0;
897 sel->r.width = f->f_width;
898 sel->r.height = f->f_height;
899 return 0;
900
c1334823 901 case V4L2_SEL_TGT_COMPOSE:
4af81310
SN
902 sel->r = f->rect;
903 return 0;
904 }
905
906 return -EINVAL;
907}
908
909static int fimc_lite_s_selection(struct file *file, void *fh,
910 struct v4l2_selection *sel)
911{
912 struct fimc_lite *fimc = video_drvdata(file);
913 struct flite_frame *f = &fimc->out_frame;
914 struct v4l2_rect rect = sel->r;
915 unsigned long flags;
916
917 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
c1334823 918 sel->target != V4L2_SEL_TGT_COMPOSE)
4af81310
SN
919 return -EINVAL;
920
921 fimc_lite_try_compose(fimc, &rect);
922
923 if ((sel->flags & V4L2_SEL_FLAG_LE) &&
924 !enclosed_rectangle(&rect, &sel->r))
925 return -ERANGE;
926
927 if ((sel->flags & V4L2_SEL_FLAG_GE) &&
928 !enclosed_rectangle(&sel->r, &rect))
929 return -ERANGE;
930
931 sel->r = rect;
932 spin_lock_irqsave(&fimc->slock, flags);
933 f->rect = rect;
934 set_bit(ST_FLITE_CONFIG, &fimc->state);
935 spin_unlock_irqrestore(&fimc->slock, flags);
936
937 return 0;
938}
939
940static const struct v4l2_ioctl_ops fimc_lite_ioctl_ops = {
941 .vidioc_querycap = fimc_vidioc_querycap_capture,
942 .vidioc_enum_fmt_vid_cap_mplane = fimc_lite_enum_fmt_mplane,
943 .vidioc_try_fmt_vid_cap_mplane = fimc_lite_try_fmt_mplane,
944 .vidioc_s_fmt_vid_cap_mplane = fimc_lite_s_fmt_mplane,
945 .vidioc_g_fmt_vid_cap_mplane = fimc_lite_g_fmt_mplane,
946 .vidioc_g_selection = fimc_lite_g_selection,
947 .vidioc_s_selection = fimc_lite_s_selection,
948 .vidioc_reqbufs = fimc_lite_reqbufs,
ee12b049
SN
949 .vidioc_querybuf = vb2_ioctl_querybuf,
950 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
951 .vidioc_create_bufs = vb2_ioctl_create_bufs,
952 .vidioc_qbuf = vb2_ioctl_qbuf,
953 .vidioc_dqbuf = vb2_ioctl_dqbuf,
4af81310
SN
954 .vidioc_streamon = fimc_lite_streamon,
955 .vidioc_streamoff = fimc_lite_streamoff,
956};
957
958/* Capture subdev media entity operations */
959static int fimc_lite_link_setup(struct media_entity *entity,
960 const struct media_pad *local,
961 const struct media_pad *remote, u32 flags)
962{
963 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
964 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
965 unsigned int remote_ent_type = media_entity_type(remote->entity);
6319d6a0 966 int ret = 0;
4af81310
SN
967
968 if (WARN_ON(fimc == NULL))
969 return 0;
970
969e877c 971 v4l2_dbg(1, debug, sd, "%s: %s --> %s, flags: 0x%x. source_id: 0x%x\n",
6319d6a0 972 __func__, remote->entity->name, local->entity->name,
4af81310
SN
973 flags, fimc->source_subdev_grp_id);
974
6319d6a0 975 mutex_lock(&fimc->lock);
4af81310 976
6319d6a0
SN
977 switch (local->index) {
978 case FLITE_SD_PAD_SINK:
979 if (remote_ent_type != MEDIA_ENT_T_V4L2_SUBDEV) {
980 ret = -EINVAL;
981 break;
982 }
4af81310 983 if (flags & MEDIA_LNK_FL_ENABLED) {
6319d6a0
SN
984 if (fimc->source_subdev_grp_id == 0)
985 fimc->source_subdev_grp_id = sd->grp_id;
986 else
987 ret = -EBUSY;
988 } else {
989 fimc->source_subdev_grp_id = 0;
990 fimc->sensor = NULL;
4af81310 991 }
6319d6a0 992 break;
4af81310 993
6319d6a0
SN
994 case FLITE_SD_PAD_SOURCE_DMA:
995 if (!(flags & MEDIA_LNK_FL_ENABLED))
03878bb4 996 atomic_set(&fimc->out_path, FIMC_IO_NONE);
6319d6a0 997 else if (remote_ent_type == MEDIA_ENT_T_DEVNODE)
03878bb4 998 atomic_set(&fimc->out_path, FIMC_IO_DMA);
6319d6a0
SN
999 else
1000 ret = -EINVAL;
4af81310
SN
1001 break;
1002
6319d6a0
SN
1003 case FLITE_SD_PAD_SOURCE_ISP:
1004 if (!(flags & MEDIA_LNK_FL_ENABLED))
03878bb4 1005 atomic_set(&fimc->out_path, FIMC_IO_NONE);
6319d6a0 1006 else if (remote_ent_type == MEDIA_ENT_T_V4L2_SUBDEV)
03878bb4 1007 atomic_set(&fimc->out_path, FIMC_IO_ISP);
4af81310 1008 else
6319d6a0 1009 ret = -EINVAL;
4af81310
SN
1010 break;
1011
1012 default:
1013 v4l2_err(sd, "Invalid pad index\n");
6319d6a0 1014 ret = -EINVAL;
4af81310 1015 }
03878bb4 1016 mb();
4af81310 1017
6319d6a0
SN
1018 mutex_unlock(&fimc->lock);
1019 return ret;
4af81310
SN
1020}
1021
1022static const struct media_entity_operations fimc_lite_subdev_media_ops = {
1023 .link_setup = fimc_lite_link_setup,
1024};
1025
1026static int fimc_lite_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1027 struct v4l2_subdev_fh *fh,
1028 struct v4l2_subdev_mbus_code_enum *code)
1029{
1030 const struct fimc_fmt *fmt;
1031
e90ad659 1032 fmt = fimc_lite_find_format(NULL, NULL, 0, code->index);
4af81310
SN
1033 if (!fmt)
1034 return -EINVAL;
1035 code->code = fmt->mbus_code;
1036 return 0;
1037}
1038
1039static int fimc_lite_subdev_get_fmt(struct v4l2_subdev *sd,
1040 struct v4l2_subdev_fh *fh,
1041 struct v4l2_subdev_format *fmt)
1042{
1043 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1044 struct v4l2_mbus_framefmt *mf = &fmt->format;
e90ad659 1045 struct flite_frame *f = &fimc->inp_frame;
4af81310
SN
1046
1047 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1048 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1049 fmt->format = *mf;
1050 return 0;
1051 }
1052 mf->colorspace = V4L2_COLORSPACE_JPEG;
1053
1054 mutex_lock(&fimc->lock);
e90ad659 1055 mf->code = f->fmt->mbus_code;
4af81310
SN
1056
1057 if (fmt->pad == FLITE_SD_PAD_SINK) {
1058 /* full camera input frame size */
1059 mf->width = f->f_width;
1060 mf->height = f->f_height;
1061 } else {
1062 /* crop size */
1063 mf->width = f->rect.width;
1064 mf->height = f->rect.height;
1065 }
1066 mutex_unlock(&fimc->lock);
1067 return 0;
1068}
1069
1070static int fimc_lite_subdev_set_fmt(struct v4l2_subdev *sd,
1071 struct v4l2_subdev_fh *fh,
1072 struct v4l2_subdev_format *fmt)
1073{
1074 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1075 struct v4l2_mbus_framefmt *mf = &fmt->format;
1076 struct flite_frame *sink = &fimc->inp_frame;
9356ac76 1077 struct flite_frame *source = &fimc->out_frame;
4af81310
SN
1078 const struct fimc_fmt *ffmt;
1079
969e877c 1080 v4l2_dbg(1, debug, sd, "pad%d: code: 0x%x, %dx%d\n",
4af81310
SN
1081 fmt->pad, mf->code, mf->width, mf->height);
1082
1083 mf->colorspace = V4L2_COLORSPACE_JPEG;
1084 mutex_lock(&fimc->lock);
1085
03878bb4
SN
1086 if ((atomic_read(&fimc->out_path) == FIMC_IO_ISP &&
1087 sd->entity.stream_count > 0) ||
1088 (atomic_read(&fimc->out_path) == FIMC_IO_DMA &&
1089 vb2_is_busy(&fimc->vb_queue))) {
4af81310
SN
1090 mutex_unlock(&fimc->lock);
1091 return -EBUSY;
1092 }
1093
1094 ffmt = fimc_lite_try_format(fimc, &mf->width, &mf->height,
1095 &mf->code, NULL, fmt->pad);
1096
1097 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1098 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1099 *mf = fmt->format;
1100 mutex_unlock(&fimc->lock);
1101 return 0;
1102 }
1103
1104 if (fmt->pad == FLITE_SD_PAD_SINK) {
1105 sink->f_width = mf->width;
1106 sink->f_height = mf->height;
e90ad659 1107 sink->fmt = ffmt;
4af81310
SN
1108 /* Set sink crop rectangle */
1109 sink->rect.width = mf->width;
1110 sink->rect.height = mf->height;
1111 sink->rect.left = 0;
1112 sink->rect.top = 0;
9356ac76
SN
1113 /* Reset source format and crop rectangle */
1114 source->rect = sink->rect;
1115 source->f_width = mf->width;
1116 source->f_height = mf->height;
4af81310
SN
1117 } else {
1118 /* Allow changing format only on sink pad */
e90ad659 1119 mf->code = sink->fmt->mbus_code;
4af81310
SN
1120 mf->width = sink->rect.width;
1121 mf->height = sink->rect.height;
1122 }
1123
1124 mutex_unlock(&fimc->lock);
1125 return 0;
1126}
1127
1128static int fimc_lite_subdev_get_selection(struct v4l2_subdev *sd,
1129 struct v4l2_subdev_fh *fh,
1130 struct v4l2_subdev_selection *sel)
1131{
1132 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1133 struct flite_frame *f = &fimc->inp_frame;
1134
5689b288
SA
1135 if ((sel->target != V4L2_SEL_TGT_CROP &&
1136 sel->target != V4L2_SEL_TGT_CROP_BOUNDS) ||
1137 sel->pad != FLITE_SD_PAD_SINK)
4af81310
SN
1138 return -EINVAL;
1139
1140 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1141 sel->r = *v4l2_subdev_get_try_crop(fh, sel->pad);
1142 return 0;
1143 }
1144
1145 mutex_lock(&fimc->lock);
5689b288 1146 if (sel->target == V4L2_SEL_TGT_CROP) {
4af81310
SN
1147 sel->r = f->rect;
1148 } else {
1149 sel->r.left = 0;
1150 sel->r.top = 0;
1151 sel->r.width = f->f_width;
1152 sel->r.height = f->f_height;
1153 }
1154 mutex_unlock(&fimc->lock);
1155
969e877c 1156 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
4af81310
SN
1157 __func__, f->rect.left, f->rect.top, f->rect.width,
1158 f->rect.height, f->f_width, f->f_height);
1159
1160 return 0;
1161}
1162
1163static int fimc_lite_subdev_set_selection(struct v4l2_subdev *sd,
1164 struct v4l2_subdev_fh *fh,
1165 struct v4l2_subdev_selection *sel)
1166{
1167 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1168 struct flite_frame *f = &fimc->inp_frame;
1169 int ret = 0;
1170
5689b288 1171 if (sel->target != V4L2_SEL_TGT_CROP || sel->pad != FLITE_SD_PAD_SINK)
4af81310
SN
1172 return -EINVAL;
1173
1174 mutex_lock(&fimc->lock);
1175 fimc_lite_try_crop(fimc, &sel->r);
1176
1177 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1178 *v4l2_subdev_get_try_crop(fh, sel->pad) = sel->r;
1179 } else {
1180 unsigned long flags;
1181 spin_lock_irqsave(&fimc->slock, flags);
1182 f->rect = sel->r;
1183 /* Same crop rectangle on the source pad */
1184 fimc->out_frame.rect = sel->r;
1185 set_bit(ST_FLITE_CONFIG, &fimc->state);
1186 spin_unlock_irqrestore(&fimc->slock, flags);
1187 }
1188 mutex_unlock(&fimc->lock);
1189
969e877c 1190 v4l2_dbg(1, debug, sd, "%s: (%d,%d) %dx%d, f_w: %d, f_h: %d\n",
4af81310
SN
1191 __func__, f->rect.left, f->rect.top, f->rect.width,
1192 f->rect.height, f->f_width, f->f_height);
1193
1194 return ret;
1195}
1196
1197static int fimc_lite_subdev_s_stream(struct v4l2_subdev *sd, int on)
1198{
1199 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
6319d6a0
SN
1200 unsigned long flags;
1201 int ret;
4af81310 1202
6319d6a0
SN
1203 /*
1204 * Find sensor subdev linked to FIMC-LITE directly or through
1205 * MIPI-CSIS. This is required for configuration where FIMC-LITE
1206 * is used as a subdev only and feeds data internally to FIMC-IS.
1207 * The pipeline links are protected through entity.stream_count
1208 * so there is no need to take the media graph mutex here.
1209 */
1210 fimc->sensor = __find_remote_sensor(&sd->entity);
1211
03878bb4 1212 if (atomic_read(&fimc->out_path) != FIMC_IO_ISP)
4af81310
SN
1213 return -ENOIOCTLCMD;
1214
03878bb4 1215 mutex_lock(&fimc->lock);
6319d6a0
SN
1216 if (on) {
1217 flite_hw_reset(fimc);
1218 ret = fimc_lite_hw_init(fimc, true);
1219 if (!ret) {
1220 spin_lock_irqsave(&fimc->slock, flags);
1221 flite_hw_capture_start(fimc);
1222 spin_unlock_irqrestore(&fimc->slock, flags);
1223 }
1224 } else {
1225 set_bit(ST_FLITE_OFF, &fimc->state);
4af81310 1226
6319d6a0
SN
1227 spin_lock_irqsave(&fimc->slock, flags);
1228 flite_hw_capture_stop(fimc);
1229 spin_unlock_irqrestore(&fimc->slock, flags);
1230
1231 ret = wait_event_timeout(fimc->irq_queue,
1232 !test_bit(ST_FLITE_OFF, &fimc->state),
1233 msecs_to_jiffies(200));
1234 if (ret == 0)
1235 v4l2_err(sd, "s_stream(0) timeout\n");
1236 clear_bit(ST_FLITE_RUN, &fimc->state);
1237 }
1238
1239 mutex_unlock(&fimc->lock);
1240 return ret;
4af81310
SN
1241}
1242
4af81310
SN
1243static int fimc_lite_log_status(struct v4l2_subdev *sd)
1244{
1245 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1246
1247 flite_hw_dump_regs(fimc, __func__);
1248 return 0;
1249}
1250
1251static int fimc_lite_subdev_registered(struct v4l2_subdev *sd)
1252{
1253 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1254 struct vb2_queue *q = &fimc->vb_queue;
1bcd7041 1255 struct video_device *vfd = &fimc->vfd;
4af81310
SN
1256 int ret;
1257
1bcd7041
SN
1258 memset(vfd, 0, sizeof(*vfd));
1259
e90ad659
SN
1260 fimc->inp_frame.fmt = &fimc_lite_formats[0];
1261 fimc->out_frame.fmt = &fimc_lite_formats[0];
03878bb4 1262 atomic_set(&fimc->out_path, FIMC_IO_DMA);
4af81310 1263
4af81310
SN
1264 snprintf(vfd->name, sizeof(vfd->name), "fimc-lite.%d.capture",
1265 fimc->index);
1266
1267 vfd->fops = &fimc_lite_fops;
1268 vfd->ioctl_ops = &fimc_lite_ioctl_ops;
1269 vfd->v4l2_dev = sd->v4l2_dev;
1270 vfd->minor = -1;
1bcd7041 1271 vfd->release = video_device_release_empty;
ee12b049 1272 vfd->queue = q;
4af81310
SN
1273 fimc->reqbufs_count = 0;
1274
1275 INIT_LIST_HEAD(&fimc->pending_buf_q);
1276 INIT_LIST_HEAD(&fimc->active_buf_q);
1277
1278 memset(q, 0, sizeof(*q));
1279 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1280 q->io_modes = VB2_MMAP | VB2_USERPTR;
1281 q->ops = &fimc_lite_qops;
1282 q->mem_ops = &vb2_dma_contig_memops;
1283 q->buf_struct_size = sizeof(struct flite_buffer);
1284 q->drv_priv = fimc;
6aa69f99 1285 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
ee12b049 1286 q->lock = &fimc->lock;
4af81310 1287
41fd087f
SN
1288 ret = vb2_queue_init(q);
1289 if (ret < 0)
1290 return ret;
4af81310
SN
1291
1292 fimc->vd_pad.flags = MEDIA_PAD_FL_SINK;
1293 ret = media_entity_init(&vfd->entity, 1, &fimc->vd_pad, 0);
1bcd7041
SN
1294 if (ret < 0)
1295 return ret;
4af81310
SN
1296
1297 video_set_drvdata(vfd, fimc);
97d66c47 1298 fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
4af81310
SN
1299
1300 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
1bcd7041
SN
1301 if (ret < 0) {
1302 media_entity_cleanup(&vfd->entity);
97d66c47 1303 fimc->pipeline_ops = NULL;
1bcd7041
SN
1304 return ret;
1305 }
4af81310
SN
1306
1307 v4l2_info(sd->v4l2_dev, "Registered %s as /dev/%s\n",
1308 vfd->name, video_device_node_name(vfd));
1309 return 0;
4af81310
SN
1310}
1311
1312static void fimc_lite_subdev_unregistered(struct v4l2_subdev *sd)
1313{
1314 struct fimc_lite *fimc = v4l2_get_subdevdata(sd);
1315
1316 if (fimc == NULL)
1317 return;
1318
1bcd7041
SN
1319 if (video_is_registered(&fimc->vfd)) {
1320 video_unregister_device(&fimc->vfd);
1321 media_entity_cleanup(&fimc->vfd.entity);
97d66c47 1322 fimc->pipeline_ops = NULL;
4af81310
SN
1323 }
1324}
1325
1326static const struct v4l2_subdev_internal_ops fimc_lite_subdev_internal_ops = {
1327 .registered = fimc_lite_subdev_registered,
1328 .unregistered = fimc_lite_subdev_unregistered,
1329};
1330
1331static const struct v4l2_subdev_pad_ops fimc_lite_subdev_pad_ops = {
1332 .enum_mbus_code = fimc_lite_subdev_enum_mbus_code,
1333 .get_selection = fimc_lite_subdev_get_selection,
1334 .set_selection = fimc_lite_subdev_set_selection,
1335 .get_fmt = fimc_lite_subdev_get_fmt,
1336 .set_fmt = fimc_lite_subdev_set_fmt,
1337};
1338
1339static const struct v4l2_subdev_video_ops fimc_lite_subdev_video_ops = {
1340 .s_stream = fimc_lite_subdev_s_stream,
1341};
1342
1343static const struct v4l2_subdev_core_ops fimc_lite_core_ops = {
4af81310
SN
1344 .log_status = fimc_lite_log_status,
1345};
1346
1347static struct v4l2_subdev_ops fimc_lite_subdev_ops = {
1348 .core = &fimc_lite_core_ops,
1349 .video = &fimc_lite_subdev_video_ops,
1350 .pad = &fimc_lite_subdev_pad_ops,
1351};
1352
1353static int fimc_lite_s_ctrl(struct v4l2_ctrl *ctrl)
1354{
1355 struct fimc_lite *fimc = container_of(ctrl->handler, struct fimc_lite,
1356 ctrl_handler);
1357 set_bit(ST_FLITE_CONFIG, &fimc->state);
1358 return 0;
1359}
1360
1361static const struct v4l2_ctrl_ops fimc_lite_ctrl_ops = {
1362 .s_ctrl = fimc_lite_s_ctrl,
1363};
1364
1365static const struct v4l2_ctrl_config fimc_lite_ctrl = {
1366 .ops = &fimc_lite_ctrl_ops,
1367 .id = V4L2_CTRL_CLASS_USER | 0x1001,
1368 .type = V4L2_CTRL_TYPE_BOOLEAN,
1369 .name = "Test Pattern 640x480",
4cec1893 1370 .step = 1,
4af81310
SN
1371};
1372
1373static int fimc_lite_create_capture_subdev(struct fimc_lite *fimc)
1374{
1375 struct v4l2_ctrl_handler *handler = &fimc->ctrl_handler;
1376 struct v4l2_subdev *sd = &fimc->subdev;
1377 int ret;
1378
1379 v4l2_subdev_init(sd, &fimc_lite_subdev_ops);
1380 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1381 snprintf(sd->name, sizeof(sd->name), "FIMC-LITE.%d", fimc->index);
1382
6319d6a0
SN
1383 fimc->subdev_pads[FLITE_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1384 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_DMA].flags = MEDIA_PAD_FL_SOURCE;
1385 fimc->subdev_pads[FLITE_SD_PAD_SOURCE_ISP].flags = MEDIA_PAD_FL_SOURCE;
1386 ret = media_entity_init(&sd->entity, FLITE_SD_PADS_NUM,
4af81310
SN
1387 fimc->subdev_pads, 0);
1388 if (ret)
1389 return ret;
1390
1391 v4l2_ctrl_handler_init(handler, 1);
1392 fimc->test_pattern = v4l2_ctrl_new_custom(handler, &fimc_lite_ctrl,
1393 NULL);
1394 if (handler->error) {
1395 media_entity_cleanup(&sd->entity);
1396 return handler->error;
1397 }
1398
1399 sd->ctrl_handler = handler;
1400 sd->internal_ops = &fimc_lite_subdev_internal_ops;
1401 sd->entity.ops = &fimc_lite_subdev_media_ops;
1402 v4l2_set_subdevdata(sd, fimc);
1403
1404 return 0;
1405}
1406
1407static void fimc_lite_unregister_capture_subdev(struct fimc_lite *fimc)
1408{
1409 struct v4l2_subdev *sd = &fimc->subdev;
1410
1411 v4l2_device_unregister_subdev(sd);
1412 media_entity_cleanup(&sd->entity);
1413 v4l2_ctrl_handler_free(&fimc->ctrl_handler);
1414 v4l2_set_subdevdata(sd, NULL);
1415}
1416
1417static void fimc_lite_clk_put(struct fimc_lite *fimc)
1418{
1419 if (IS_ERR_OR_NULL(fimc->clock))
1420 return;
1421
1422 clk_unprepare(fimc->clock);
1423 clk_put(fimc->clock);
1424 fimc->clock = NULL;
1425}
1426
1427static int fimc_lite_clk_get(struct fimc_lite *fimc)
1428{
1429 int ret;
1430
1431 fimc->clock = clk_get(&fimc->pdev->dev, FLITE_CLK_NAME);
1432 if (IS_ERR(fimc->clock))
1433 return PTR_ERR(fimc->clock);
1434
1435 ret = clk_prepare(fimc->clock);
1436 if (ret < 0) {
1437 clk_put(fimc->clock);
1438 fimc->clock = NULL;
1439 }
1440 return ret;
1441}
1442
eb62d9e9
SN
1443static const struct of_device_id flite_of_match[];
1444
4c62e976 1445static int fimc_lite_probe(struct platform_device *pdev)
4af81310 1446{
eb62d9e9
SN
1447 struct flite_drvdata *drv_data = NULL;
1448 struct device *dev = &pdev->dev;
1449 const struct of_device_id *of_id;
4af81310
SN
1450 struct fimc_lite *fimc;
1451 struct resource *res;
1452 int ret;
1453
eb62d9e9 1454 fimc = devm_kzalloc(dev, sizeof(*fimc), GFP_KERNEL);
4af81310
SN
1455 if (!fimc)
1456 return -ENOMEM;
1457
eb62d9e9
SN
1458 if (dev->of_node) {
1459 of_id = of_match_node(flite_of_match, dev->of_node);
1460 if (of_id)
1461 drv_data = (struct flite_drvdata *)of_id->data;
1462 fimc->index = of_alias_get_id(dev->of_node, "fimc-lite");
1463 } else {
1464 drv_data = fimc_lite_get_drvdata(pdev);
1465 fimc->index = pdev->id;
1466 }
1467
1468 if (!drv_data || fimc->index < 0 || fimc->index >= FIMC_LITE_MAX_DEVS)
1469 return -EINVAL;
1470
9c8399c8 1471 fimc->dd = drv_data;
4af81310
SN
1472 fimc->pdev = pdev;
1473
1474 init_waitqueue_head(&fimc->irq_queue);
1475 spin_lock_init(&fimc->slock);
1476 mutex_init(&fimc->lock);
1477
1478 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
eb62d9e9 1479 fimc->regs = devm_ioremap_resource(dev, res);
f23999ec
TR
1480 if (IS_ERR(fimc->regs))
1481 return PTR_ERR(fimc->regs);
4af81310
SN
1482
1483 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1484 if (res == NULL) {
eb62d9e9 1485 dev_err(dev, "Failed to get IRQ resource\n");
4af81310
SN
1486 return -ENXIO;
1487 }
1488
1489 ret = fimc_lite_clk_get(fimc);
1490 if (ret)
1491 return ret;
1492
eb62d9e9
SN
1493 ret = devm_request_irq(dev, res->start, flite_irq_handler,
1494 0, dev_name(dev), fimc);
4af81310 1495 if (ret) {
eb62d9e9 1496 dev_err(dev, "Failed to install irq (%d)\n", ret);
4af81310
SN
1497 goto err_clk;
1498 }
1499
1500 /* The video node will be created within the subdev's registered() op */
1501 ret = fimc_lite_create_capture_subdev(fimc);
1502 if (ret)
1503 goto err_clk;
1504
1505 platform_set_drvdata(pdev, fimc);
eb62d9e9
SN
1506 pm_runtime_enable(dev);
1507 ret = pm_runtime_get_sync(dev);
4af81310
SN
1508 if (ret < 0)
1509 goto err_sd;
1510
eb62d9e9 1511 fimc->alloc_ctx = vb2_dma_contig_init_ctx(dev);
4af81310
SN
1512 if (IS_ERR(fimc->alloc_ctx)) {
1513 ret = PTR_ERR(fimc->alloc_ctx);
1514 goto err_pm;
1515 }
eb62d9e9 1516 pm_runtime_put(dev);
4af81310 1517
eb62d9e9 1518 dev_dbg(dev, "FIMC-LITE.%d registered successfully\n",
4af81310
SN
1519 fimc->index);
1520 return 0;
1521err_pm:
eb62d9e9 1522 pm_runtime_put(dev);
4af81310
SN
1523err_sd:
1524 fimc_lite_unregister_capture_subdev(fimc);
1525err_clk:
1526 fimc_lite_clk_put(fimc);
1527 return ret;
1528}
1529
1530static int fimc_lite_runtime_resume(struct device *dev)
1531{
1532 struct fimc_lite *fimc = dev_get_drvdata(dev);
1533
1534 clk_enable(fimc->clock);
1535 return 0;
1536}
1537
1538static int fimc_lite_runtime_suspend(struct device *dev)
1539{
1540 struct fimc_lite *fimc = dev_get_drvdata(dev);
1541
1542 clk_disable(fimc->clock);
1543 return 0;
1544}
1545
1546#ifdef CONFIG_PM_SLEEP
1547static int fimc_lite_resume(struct device *dev)
1548{
1549 struct fimc_lite *fimc = dev_get_drvdata(dev);
1550 struct flite_buffer *buf;
1551 unsigned long flags;
1552 int i;
1553
1554 spin_lock_irqsave(&fimc->slock, flags);
1555 if (!test_and_clear_bit(ST_LPM, &fimc->state) ||
1556 !test_bit(ST_FLITE_IN_USE, &fimc->state)) {
1557 spin_unlock_irqrestore(&fimc->slock, flags);
1558 return 0;
1559 }
1560 flite_hw_reset(fimc);
1561 spin_unlock_irqrestore(&fimc->slock, flags);
1562
1563 if (!test_and_clear_bit(ST_FLITE_SUSPENDED, &fimc->state))
1564 return 0;
1565
1566 INIT_LIST_HEAD(&fimc->active_buf_q);
b9ee31e6
SN
1567 fimc_pipeline_call(fimc, open, &fimc->pipeline,
1568 &fimc->vfd.entity, false);
03878bb4 1569 fimc_lite_hw_init(fimc, atomic_read(&fimc->out_path) == FIMC_IO_ISP);
4af81310
SN
1570 clear_bit(ST_FLITE_SUSPENDED, &fimc->state);
1571
1572 for (i = 0; i < fimc->reqbufs_count; i++) {
1573 if (list_empty(&fimc->pending_buf_q))
1574 break;
1575 buf = fimc_lite_pending_queue_pop(fimc);
1576 buffer_queue(&buf->vb);
1577 }
1578 return 0;
1579}
1580
1581static int fimc_lite_suspend(struct device *dev)
1582{
1583 struct fimc_lite *fimc = dev_get_drvdata(dev);
1584 bool suspend = test_bit(ST_FLITE_IN_USE, &fimc->state);
1585 int ret;
1586
1587 if (test_and_set_bit(ST_LPM, &fimc->state))
1588 return 0;
1589
1590 ret = fimc_lite_stop_capture(fimc, suspend);
316efab3 1591 if (ret < 0 || !fimc_lite_active(fimc))
4af81310
SN
1592 return ret;
1593
b9ee31e6 1594 return fimc_pipeline_call(fimc, close, &fimc->pipeline);
4af81310
SN
1595}
1596#endif /* CONFIG_PM_SLEEP */
1597
4c62e976 1598static int fimc_lite_remove(struct platform_device *pdev)
4af81310
SN
1599{
1600 struct fimc_lite *fimc = platform_get_drvdata(pdev);
1601 struct device *dev = &pdev->dev;
1602
1603 pm_runtime_disable(dev);
1604 pm_runtime_set_suspended(dev);
1605 fimc_lite_unregister_capture_subdev(fimc);
1606 vb2_dma_contig_cleanup_ctx(fimc->alloc_ctx);
1607 fimc_lite_clk_put(fimc);
1608
1609 dev_info(dev, "Driver unloaded\n");
1610 return 0;
1611}
1612
eb62d9e9
SN
1613static const struct dev_pm_ops fimc_lite_pm_ops = {
1614 SET_SYSTEM_SLEEP_PM_OPS(fimc_lite_suspend, fimc_lite_resume)
1615 SET_RUNTIME_PM_OPS(fimc_lite_runtime_suspend, fimc_lite_runtime_resume,
1616 NULL)
1617};
1618
9c8399c8
SN
1619/* EXYNOS4212, EXYNOS4412 */
1620static struct flite_drvdata fimc_lite_drvdata_exynos4 = {
4af81310
SN
1621 .max_width = 8192,
1622 .max_height = 8192,
1623 .out_width_align = 8,
1624 .win_hor_offs_align = 2,
1625 .out_hor_offs_align = 8,
1626};
1627
4af81310
SN
1628static struct platform_device_id fimc_lite_driver_ids[] = {
1629 {
1630 .name = "exynos-fimc-lite",
1631 .driver_data = (unsigned long)&fimc_lite_drvdata_exynos4,
1632 },
1633 { /* sentinel */ },
1634};
1635MODULE_DEVICE_TABLE(platform, fimc_lite_driver_ids);
1636
eb62d9e9
SN
1637static const struct of_device_id flite_of_match[] = {
1638 {
1639 .compatible = "samsung,exynos4212-fimc-lite",
1640 .data = &fimc_lite_drvdata_exynos4,
1641 },
1642 { /* sentinel */ },
4af81310 1643};
eb62d9e9 1644MODULE_DEVICE_TABLE(of, flite_of_match);
4af81310
SN
1645
1646static struct platform_driver fimc_lite_driver = {
1647 .probe = fimc_lite_probe,
4c62e976 1648 .remove = fimc_lite_remove,
4af81310
SN
1649 .id_table = fimc_lite_driver_ids,
1650 .driver = {
eb62d9e9 1651 .of_match_table = flite_of_match,
4af81310
SN
1652 .name = FIMC_LITE_DRV_NAME,
1653 .owner = THIS_MODULE,
1654 .pm = &fimc_lite_pm_ops,
1655 }
1656};
1657module_platform_driver(fimc_lite_driver);
1658MODULE_LICENSE("GPL");
1659MODULE_ALIAS("platform:" FIMC_LITE_DRV_NAME);
This page took 0.171834 seconds and 5 git commands to generate.