blob: ecfa6ab4a19d9caea64f70c1ee9231362b790780 [file] [log] [blame]
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001/*
Sylwester Nawrocki3a3f9442011-04-28 09:06:19 -03002 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03003 *
Sylwester Nawrocki0c9204d2012-04-25 06:55:42 -03004 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03006 *
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
12#include <linux/module.h>
13#include <linux/kernel.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030014#include <linux/types.h>
15#include <linux/errno.h>
16#include <linux/bug.h>
17#include <linux/interrupt.h>
18#include <linux/device.h>
Sylwester Nawrockie9e21082011-09-02 06:25:32 -030019#include <linux/pm_runtime.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030020#include <linux/list.h>
21#include <linux/slab.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030022
23#include <linux/videodev2.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
26#include <media/v4l2-mem2mem.h>
Junghak Sungc1399902015-09-22 10:30:29 -030027#include <media/videobuf2-v4l2.h>
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030028#include <media/videobuf2-dma-contig.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030029
Sylwester Nawrocki44031062013-05-31 11:37:19 -030030#include "common.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030031#include "fimc-core.h"
Sylwester Nawrockic83a1ff2012-05-02 06:14:49 -030032#include "fimc-reg.h"
Sylwester Nawrocki44031062013-05-31 11:37:19 -030033#include "media-dev.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030034
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030035static int fimc_capture_hw_init(struct fimc_dev *fimc)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030036{
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030037 struct fimc_source_info *si = &fimc->vid_cap.source_config;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030038 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030039 int ret;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030040 unsigned long flags;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030041
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030042 if (ctx == NULL || ctx->s_frame.fmt == NULL)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030043 return -EINVAL;
44
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030045 if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
46 ret = fimc_hw_camblk_cfg_writeback(fimc);
47 if (ret < 0)
48 return ret;
49 }
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030050
51 spin_lock_irqsave(&fimc->slock, flags);
52 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
53 fimc_set_yuv_order(ctx);
54
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030055 fimc_hw_set_camera_polarity(fimc, si);
56 fimc_hw_set_camera_type(fimc, si);
57 fimc_hw_set_camera_source(fimc, si);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030058 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
59
60 ret = fimc_set_scaler_info(ctx);
61 if (!ret) {
62 fimc_hw_set_input_path(ctx);
63 fimc_hw_set_prescaler(ctx);
64 fimc_hw_set_mainscaler(ctx);
65 fimc_hw_set_target_format(ctx);
66 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -030067 fimc_hw_set_effect(ctx);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030068 fimc_hw_set_output_path(ctx);
69 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -030070 if (fimc->drv_data->alpha_color)
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -030071 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -030072 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030073 }
74 spin_unlock_irqrestore(&fimc->slock, flags);
75 return ret;
76}
77
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030078/*
79 * Reinitialize the driver so it is ready to start the streaming again.
80 * Set fimc->state to indicate stream off and the hardware shut down state.
81 * If not suspending (@suspend is false), return any buffers to videobuf2.
82 * Otherwise put any owned buffers onto the pending buffers queue, so they
83 * can be re-spun when the device is being resumed. Also perform FIMC
84 * software reset and disable streaming on the whole pipeline if required.
85 */
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030086static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030087{
Marek Szyprowskibd323e22011-08-29 08:51:49 -030088 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030089 struct fimc_vid_buffer *buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -030090 unsigned long flags;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030091 bool streaming;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030092
93 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030094 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030095
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030096 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
97 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrockiaa3331222012-03-16 11:47:51 -030098 if (suspend)
99 fimc->state |= (1 << ST_CAPT_SUSPENDED);
100 else
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300101 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300102
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300103 /* Release unused buffers */
104 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300105 buf = fimc_pending_queue_pop(cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300106 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300107 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300108 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300109 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300110 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300111 if (suspend)
112 fimc_pending_queue_add(cap, buf);
113 else
Junghak Sung2d700712015-09-22 10:30:30 -0300114 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300115 }
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300116
117 fimc_hw_reset(fimc);
118 cap->buf_index = 0;
119
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300120 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300121
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300122 if (streaming)
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300123 return fimc_pipeline_call(&cap->ve, set_stream, 0);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300124 else
125 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300126}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300127
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300128static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300129{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300130 unsigned long flags;
131
132 if (!fimc_capture_active(fimc))
133 return 0;
134
135 spin_lock_irqsave(&fimc->slock, flags);
136 set_bit(ST_CAPT_SHUT, &fimc->state);
137 fimc_deactivate_capture(fimc);
138 spin_unlock_irqrestore(&fimc->slock, flags);
139
140 wait_event_timeout(fimc->irq_queue,
141 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300142 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300143
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300144 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300145}
146
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300147/**
148 * fimc_capture_config_update - apply the camera interface configuration
Mauro Carvalho Chehab3b060ba02017-11-29 04:32:51 -0500149 * @ctx: FIMC capture context
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300150 *
151 * To be called from within the interrupt handler with fimc.slock
152 * spinlock held. It updates the camera pixel crop, rotation and
153 * image flip in H/W.
154 */
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300155static int fimc_capture_config_update(struct fimc_ctx *ctx)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300156{
157 struct fimc_dev *fimc = ctx->fimc_dev;
158 int ret;
159
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300160 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300161
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300162 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300163 if (ret)
164 return ret;
165
166 fimc_hw_set_prescaler(ctx);
167 fimc_hw_set_mainscaler(ctx);
168 fimc_hw_set_target_format(ctx);
169 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300170 fimc_hw_set_effect(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300171 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
172 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -0300173 if (fimc->drv_data->alpha_color)
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300174 fimc_hw_set_rgb_alpha(ctx);
175
176 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300177 return ret;
178}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300179
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300180void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
181{
182 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300183 struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
184 struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300185 struct fimc_frame *f = &cap->ctx->d_frame;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300186 struct fimc_vid_buffer *v_buf;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300187
188 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
189 wake_up(&fimc->irq_queue);
190 goto done;
191 }
192
193 if (!list_empty(&cap->active_buf_q) &&
194 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300195 v_buf = fimc_active_queue_pop(cap);
196
Junghak Sungd6dd6452015-11-03 08:16:37 -0200197 v_buf->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300198 v_buf->vb.sequence = cap->frame_count++;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300199
Junghak Sung2d700712015-09-22 10:30:30 -0300200 vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300201 }
202
203 if (!list_empty(&cap->pending_buf_q)) {
204
205 v_buf = fimc_pending_queue_pop(cap);
206 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
207 v_buf->index = cap->buf_index;
208
209 /* Move the buffer to the capture active queue */
210 fimc_active_queue_add(cap, v_buf);
211
212 dbg("next frame: %d, done frame: %d",
213 fimc_hw_get_frame_index(fimc), v_buf->index);
214
215 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
216 cap->buf_index = 0;
217 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300218 /*
219 * Set up a buffer at MIPI-CSIS if current image format
220 * requires the frame embedded data capture.
221 */
222 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
223 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
224 unsigned int size = f->payload[plane];
225 s32 index = fimc_hw_get_frame_index(fimc);
226 void *vaddr;
227
228 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
229 if (v_buf->index != index)
230 continue;
Junghak Sung2d700712015-09-22 10:30:30 -0300231 vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300232 v4l2_subdev_call(csis, video, s_rx_buffer,
233 vaddr, &size);
234 break;
235 }
236 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300237
238 if (cap->active_buf_cnt == 0) {
239 if (deq_buf)
240 clear_bit(ST_CAPT_RUN, &fimc->state);
241
242 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
243 cap->buf_index = 0;
244 } else {
245 set_bit(ST_CAPT_RUN, &fimc->state);
246 }
247
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300248 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
249 fimc_capture_config_update(cap->ctx);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300250done:
251 if (cap->active_buf_cnt == 1) {
252 fimc_deactivate_capture(fimc);
253 clear_bit(ST_CAPT_STREAM, &fimc->state);
254 }
255
256 dbg("frame: %d, active_buf_cnt: %d",
257 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
258}
259
260
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300261static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300262{
263 struct fimc_ctx *ctx = q->drv_priv;
264 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300265 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300266 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300267 int ret;
268
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300269 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300270
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300271 ret = fimc_capture_hw_init(fimc);
272 if (ret) {
273 fimc_capture_state_cleanup(fimc, false);
274 return ret;
275 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300276
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300277 set_bit(ST_CAPT_PEND, &fimc->state);
278
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300279 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
280
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300281 if (vid_cap->active_buf_cnt >= min_bufs &&
282 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300283 fimc_activate_capture(ctx);
284
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300285 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300286 return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300287 }
288
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300289 return 0;
290}
291
Hans Verkuile37559b2014-04-17 02:47:21 -0300292static void stop_streaming(struct vb2_queue *q)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300293{
294 struct fimc_ctx *ctx = q->drv_priv;
295 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300296
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300297 if (!fimc_capture_active(fimc))
Hans Verkuile37559b2014-04-17 02:47:21 -0300298 return;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300299
Hans Verkuile37559b2014-04-17 02:47:21 -0300300 fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300301}
302
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300303int fimc_capture_suspend(struct fimc_dev *fimc)
304{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300305 bool suspend = fimc_capture_busy(fimc);
306
307 int ret = fimc_stop_capture(fimc, suspend);
308 if (ret)
309 return ret;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300310 return fimc_pipeline_call(&fimc->vid_cap.ve, close);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300311}
312
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300313static void buffer_queue(struct vb2_buffer *vb);
314
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300315int fimc_capture_resume(struct fimc_dev *fimc)
316{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300317 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300318 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300319 struct fimc_vid_buffer *buf;
320 int i;
321
322 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
323 return 0;
324
325 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
326 vid_cap->buf_index = 0;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300327 fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300328 fimc_capture_hw_init(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300329
330 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
331
332 for (i = 0; i < vid_cap->reqbufs_count; i++) {
333 if (list_empty(&vid_cap->pending_buf_q))
334 break;
335 buf = fimc_pending_queue_pop(vid_cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300336 buffer_queue(&buf->vb.vb2_buf);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300337 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300338 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300339
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300340}
341
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200342static int queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300343 unsigned int *num_buffers, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300344 unsigned int sizes[], struct device *alloc_devs[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300345{
346 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300347 struct fimc_frame *frame = &ctx->d_frame;
348 struct fimc_fmt *fmt = frame->fmt;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200349 unsigned long wh = frame->f_width * frame->f_height;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300350 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300351
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300352 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300353 return -EINVAL;
354
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200355 if (*num_planes) {
356 if (*num_planes != fmt->memplanes)
357 return -EINVAL;
Hans Verkuil2548fee2016-02-16 07:30:19 -0200358 for (i = 0; i < *num_planes; i++)
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200359 if (sizes[i] < (wh * fmt->depth[i]) / 8)
360 return -EINVAL;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200361 return 0;
362 }
363
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300364 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300365
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300366 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300367 unsigned int size = (wh * fmt->depth[i]) / 8;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200368
369 if (fimc_fmt_is_user_defined(fmt->color))
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300370 sizes[i] = frame->payload[i];
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300371 else
Sylwester Nawrockid547ab62012-05-16 15:00:26 -0300372 sizes[i] = max_t(u32, size, frame->payload[i]);
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300373 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300374
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300375 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300376}
377
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300378static int buffer_prepare(struct vb2_buffer *vb)
379{
380 struct vb2_queue *vq = vb->vb2_queue;
381 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300382 int i;
383
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300384 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300385 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300386
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300387 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300388 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300389
390 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300391 v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300392 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300393 vb2_plane_size(vb, i), size);
394 return -EINVAL;
395 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300396 vb2_set_plane_payload(vb, i, size);
397 }
398
399 return 0;
400}
401
402static void buffer_queue(struct vb2_buffer *vb)
403{
Junghak Sung2d700712015-09-22 10:30:30 -0300404 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300405 struct fimc_vid_buffer *buf
Junghak Sung2d700712015-09-22 10:30:30 -0300406 = container_of(vbuf, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300407 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
408 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300409 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300410 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300411 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300412 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300413
414 spin_lock_irqsave(&fimc->slock, flags);
Junghak Sung2d700712015-09-22 10:30:30 -0300415 fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300416
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300417 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
418 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
419 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300420 /* Setup the buffer directly for processing. */
421 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
422 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300423
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300424 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
425 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300426 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300427
428 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
429 vid_cap->buf_index = 0;
430 } else {
431 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300432 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300433
434 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
435
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300436
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300437 if (vb2_is_streaming(&vid_cap->vbq) &&
438 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300439 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Andrzej Hajda76323e52013-02-27 08:13:59 -0300440 int ret;
441
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300442 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300443 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300444
Andrzej Hajda76323e52013-02-27 08:13:59 -0300445 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
446 return;
447
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300448 ret = fimc_pipeline_call(ve, set_stream, 1);
Andrzej Hajda76323e52013-02-27 08:13:59 -0300449 if (ret < 0)
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300450 v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300451 return;
452 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300453 spin_unlock_irqrestore(&fimc->slock, flags);
454}
455
Julia Lawallb7b361f2016-09-08 20:59:10 -0300456static const struct vb2_ops fimc_capture_qops = {
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300457 .queue_setup = queue_setup,
458 .buf_prepare = buffer_prepare,
459 .buf_queue = buffer_queue,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300460 .wait_prepare = vb2_ops_wait_prepare,
461 .wait_finish = vb2_ops_wait_finish,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300462 .start_streaming = start_streaming,
463 .stop_streaming = stop_streaming,
464};
465
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300466static int fimc_capture_set_default_format(struct fimc_dev *fimc);
467
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300468static int fimc_capture_open(struct file *file)
469{
470 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300471 struct fimc_vid_cap *vc = &fimc->vid_cap;
472 struct exynos_video_entity *ve = &vc->ve;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300473 int ret = -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300474
475 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
476
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300477 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300478
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300479 if (fimc_m2m_active(fimc))
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300480 goto unlock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300481
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300482 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300483 ret = pm_runtime_get_sync(&fimc->pdev->dev);
484 if (ret < 0)
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300485 goto unlock;
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300486
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300487 ret = v4l2_fh_open(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300488 if (ret) {
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300489 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300490 goto unlock;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300491 }
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300492
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300493 if (v4l2_fh_is_singular_file(file)) {
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300494 fimc_md_graph_lock(ve);
495
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300496 ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
497
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300498 if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
499 /*
500 * Recreate controls of the the video node to drop
501 * any controls inherited from the sensor subdev.
502 */
503 fimc_ctrls_delete(vc->ctx);
504
505 ret = fimc_ctrls_create(vc->ctx);
506 if (ret == 0)
507 vc->inh_sensor_ctrls = false;
508 }
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300509 if (ret == 0)
510 ve->vdev.entity.use_count++;
511
512 fimc_md_graph_unlock(ve);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300513
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300514 if (ret == 0)
515 ret = fimc_capture_set_default_format(fimc);
516
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300517 if (ret < 0) {
518 clear_bit(ST_CAPT_BUSY, &fimc->state);
519 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300520 v4l2_fh_release(file);
521 }
522 }
523unlock:
524 mutex_unlock(&fimc->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300525 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300526}
527
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300528static int fimc_capture_release(struct file *file)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300529{
530 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -0300531 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300532 bool close = v4l2_fh_is_singular_file(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300533 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300534
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300535 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
536
Sylwester Nawrockiba6b3722012-11-22 11:12:16 -0300537 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300538
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300539 if (close && vc->streaming) {
Sakari Ailus20b85222016-11-21 14:48:30 -0200540 media_pipeline_stop(&vc->ve.vdev.entity);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300541 vc->streaming = false;
542 }
543
Ricardo Ribalda1380f572013-11-25 05:49:02 -0300544 ret = _vb2_fop_release(file, NULL);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300545
546 if (close) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300547 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300548 fimc_pipeline_call(&vc->ve, close);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300549 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300550
551 fimc_md_graph_lock(&vc->ve);
552 vc->ve.vdev.entity.use_count--;
553 fimc_md_graph_unlock(&vc->ve);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300554 }
555
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300556 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300557 mutex_unlock(&fimc->lock);
558
559 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300560}
561
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300562static const struct v4l2_file_operations fimc_capture_fops = {
563 .owner = THIS_MODULE,
564 .open = fimc_capture_open,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300565 .release = fimc_capture_release,
566 .poll = vb2_fop_poll,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300567 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300568 .mmap = vb2_fop_mmap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300569};
570
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300571/*
572 * Format and crop negotiation helpers
573 */
574
575static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
576 u32 *width, u32 *height,
577 u32 *code, u32 *fourcc, int pad)
578{
579 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
580 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300581 const struct fimc_variant *var = fimc->variant;
582 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300583 struct fimc_frame *dst = &ctx->d_frame;
584 u32 depth, min_w, max_w, min_h, align_h = 3;
585 u32 mask = FMT_FLAGS_CAM;
586 struct fimc_fmt *ffmt;
587
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300588 /* Conversion from/to JPEG or User Defined format is not supported */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300589 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300590 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
591 *code = ctx->s_frame.fmt->mbus_code;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300592
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300593 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300594 mask |= FMT_FLAGS_M2M;
595
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300596 if (pad == FIMC_SD_PAD_SINK_FIFO)
597 mask = FMT_FLAGS_WRITEBACK;
598
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300599 ffmt = fimc_find_format(fourcc, code, mask, 0);
600 if (WARN_ON(!ffmt))
601 return NULL;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300602
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300603 if (code)
604 *code = ffmt->mbus_code;
605 if (fourcc)
606 *fourcc = ffmt->fourcc;
607
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300608 if (pad != FIMC_SD_PAD_SOURCE) {
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300609 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300610 pl->scaler_dis_w : pl->scaler_en_w;
611 /* Apply the camera input interface pixel constraints */
612 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
613 height, max_t(u32, *height, 32),
614 FIMC_CAMIF_MAX_HEIGHT,
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300615 fimc_fmt_is_user_defined(ffmt->color) ?
616 3 : 1,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300617 0);
618 return ffmt;
619 }
620 /* Can't scale or crop in transparent (JPEG) transfer mode */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300621 if (fimc_fmt_is_user_defined(ffmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300622 *width = ctx->s_frame.f_width;
623 *height = ctx->s_frame.f_height;
624 return ffmt;
625 }
626 /* Apply the scaler and the output DMA constraints */
627 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300628 if (ctx->state & FIMC_COMPOSE) {
629 min_w = dst->offs_h + dst->width;
630 min_h = dst->offs_v + dst->height;
631 } else {
632 min_w = var->min_out_pixsize;
633 min_h = var->min_out_pixsize;
634 }
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300635 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300636 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
637
638 depth = fimc_get_format_depth(ffmt);
639 v4l_bound_align_image(width, min_w, max_w,
640 ffs(var->min_out_pixsize) - 1,
641 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
642 align_h,
643 64/(ALIGN(depth, 8)));
644
645 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
646 pad, code ? *code : 0, *width, *height,
647 dst->f_width, dst->f_height);
648
649 return ffmt;
650}
651
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300652static void fimc_capture_try_selection(struct fimc_ctx *ctx,
653 struct v4l2_rect *r,
654 int target)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300655{
656 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
657 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300658 const struct fimc_variant *var = fimc->variant;
659 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300660 struct fimc_frame *sink = &ctx->s_frame;
661 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
662 u32 align_sz = 0, align_h = 4;
663 u32 max_sc_h, max_sc_v;
664
665 /* In JPEG transparent transfer mode cropping is not supported */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300666 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300667 r->width = sink->f_width;
668 r->height = sink->f_height;
669 r->left = r->top = 0;
670 return;
671 }
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300672 if (target == V4L2_SEL_TGT_COMPOSE) {
Mauro Carvalho Chehab6ccd2282018-03-23 08:54:22 -0400673 u32 tmp_min_h = ffs(sink->width) - 3;
674 u32 tmp_min_v = ffs(sink->height) - 1;
675
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300676 if (ctx->rotation != 90 && ctx->rotation != 270)
677 align_h = 1;
Mauro Carvalho Chehab6ccd2282018-03-23 08:54:22 -0400678 max_sc_h = min(SCALER_MAX_HRATIO, 1 << tmp_min_h);
679 max_sc_v = min(SCALER_MAX_VRATIO, 1 << tmp_min_v);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300680 min_sz = var->min_out_pixsize;
681 } else {
682 u32 depth = fimc_get_format_depth(sink->fmt);
683 align_sz = 64/ALIGN(depth, 8);
684 min_sz = var->min_inp_pixsize;
685 min_w = min_h = min_sz;
686 max_sc_h = max_sc_v = 1;
687 }
688 /*
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300689 * For the compose rectangle the following constraints must be met:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300690 * - it must fit in the sink pad format rectangle (f_width/f_height);
691 * - maximum downscaling ratio is 64;
692 * - maximum crop size depends if the rotator is used or not;
693 * - the sink pad format width/height must be 4 multiple of the
694 * prescaler ratios determined by sink pad size and source pad crop,
695 * the prescaler ratio is returned by fimc_get_scaler_factor().
696 */
697 max_w = min_t(u32,
698 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
699 rotate ? sink->f_height : sink->f_width);
700 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300701
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300702 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300703 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
704 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
705 if (rotate) {
706 swap(max_sc_h, max_sc_v);
707 swap(min_w, min_h);
708 }
709 }
710 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
711 &r->height, min_h, max_h, align_h,
712 align_sz);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300713 /* Adjust left/top if crop/compose rectangle is out of bounds */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300714 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
715 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
716 r->left = round_down(r->left, var->hor_offs_align);
717
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300718 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
719 target, r->left, r->top, r->width, r->height,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300720 sink->f_width, sink->f_height);
721}
722
723/*
724 * The video node ioctl operations
725 */
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -0300726static int fimc_cap_querycap(struct file *file, void *priv,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300727 struct v4l2_capability *cap)
728{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300729 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300730
Boris Brezillon1ddc8a92019-06-04 03:06:24 -0400731 __fimc_vidioc_querycap(&fimc->pdev->dev, cap);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300732 return 0;
733}
734
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300735static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
736 struct v4l2_fmtdesc *f)
737{
738 struct fimc_fmt *fmt;
739
740 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
741 f->index);
742 if (!fmt)
743 return -EINVAL;
Mauro Carvalho Chehab85709cb2018-09-10 08:19:16 -0400744 strscpy(f->description, fmt->name, sizeof(f->description));
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300745 f->pixelformat = fmt->fourcc;
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300746 if (fmt->fourcc == MEDIA_BUS_FMT_JPEG_1X8)
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300747 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
748 return 0;
749}
750
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300751static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
752{
753 struct media_pad *pad = &me->pads[0];
754
755 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300756 pad = media_entity_remote_pad(pad);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300757 if (!pad)
758 break;
759 me = pad->entity;
760 pad = &me->pads[0];
761 }
762
763 return me;
764}
765
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300766/**
767 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
768 * elements
769 * @ctx: FIMC capture context
770 * @tfmt: media bus format to try/set on subdevs
771 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
772 * @set: true to set format on subdevs, false to try only
773 */
774static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
775 struct v4l2_mbus_framefmt *tfmt,
776 struct fimc_fmt **fmt_id,
777 bool set)
778{
779 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300780 struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
781 struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300782 struct v4l2_subdev_format sfmt;
783 struct v4l2_mbus_framefmt *mf = &sfmt.format;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300784 struct media_entity *me;
785 struct fimc_fmt *ffmt;
786 struct media_pad *pad;
787 int ret, i = 1;
788 u32 fcc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300789
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300790 if (WARN_ON(!sd || !tfmt))
791 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300792
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300793 memset(&sfmt, 0, sizeof(sfmt));
794 sfmt.format = *tfmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300795 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300796
797 me = fimc_pipeline_get_head(&sd->entity);
798
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300799 while (1) {
800 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
801 FMT_FLAGS_CAM, i++);
802 if (ffmt == NULL) {
803 /*
804 * Notify user-space if common pixel code for
805 * host and sensor does not exist.
806 */
807 return -EINVAL;
808 }
809 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300810
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300811 /* set format on all pipeline subdevs */
812 while (me != &fimc->vid_cap.subdev.entity) {
813 sd = media_entity_to_v4l2_subdev(me);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300814
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300815 sfmt.pad = 0;
816 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
817 if (ret)
818 return ret;
819
820 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
821 sfmt.pad = me->num_pads - 1;
822 mf->code = tfmt->code;
823 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
824 &sfmt);
825 if (ret)
826 return ret;
827 }
828
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300829 pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300830 if (!pad)
831 return -EINVAL;
832 me = pad->entity;
833 }
834
835 if (mf->code != tfmt->code)
836 continue;
837
838 fcc = ffmt->fourcc;
839 tfmt->width = mf->width;
840 tfmt->height = mf->height;
841 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300842 NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300843 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
844 NULL, &fcc, FIMC_SD_PAD_SOURCE);
845 if (ffmt && ffmt->mbus_code)
846 mf->code = ffmt->mbus_code;
847 if (mf->width != tfmt->width || mf->height != tfmt->height)
848 continue;
849 tfmt->code = mf->code;
850 break;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300851 }
852
853 if (fmt_id && ffmt)
854 *fmt_id = ffmt;
855 *tfmt = *mf;
856
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300857 return 0;
858}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300859
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300860/**
861 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
862 * @sensor: pointer to the sensor subdev
863 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
Mauro Carvalho Chehab3b060ba02017-11-29 04:32:51 -0500864 * @num_planes: number of planes
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300865 * @try: true to set the frame parameters, false to query only
866 *
867 * This function is used by this driver only for compressed/blob data formats.
868 */
869static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
870 struct v4l2_plane_pix_format *plane_fmt,
871 unsigned int num_planes, bool try)
872{
873 struct v4l2_mbus_frame_desc fd;
874 int i, ret;
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300875 int pad;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300876
877 for (i = 0; i < num_planes; i++)
878 fd.entry[i].length = plane_fmt[i].sizeimage;
879
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300880 pad = sensor->entity.num_pads - 1;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300881 if (try)
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300882 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300883 else
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300884 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300885
886 if (ret < 0)
887 return ret;
888
889 if (num_planes != fd.num_entries)
890 return -EINVAL;
891
892 for (i = 0; i < num_planes; i++)
893 plane_fmt[i].sizeimage = fd.entry[i].length;
894
895 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
896 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
897 fd.entry[0].length);
898
899 return -EINVAL;
900 }
901
902 return 0;
903}
904
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300905static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
906 struct v4l2_format *f)
907{
908 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300909
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -0300910 __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
911 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300912}
913
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300914/*
915 * Try or set format on the fimc.X.capture video node and additionally
916 * on the whole pipeline if @try is false.
917 * Locking: the caller must _not_ hold the graph mutex.
918 */
919static int __video_try_or_set_format(struct fimc_dev *fimc,
920 struct v4l2_format *f, bool try,
921 struct fimc_fmt **inp_fmt,
922 struct fimc_fmt **out_fmt)
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300923{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300924 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300925 struct fimc_vid_cap *vc = &fimc->vid_cap;
926 struct exynos_video_entity *ve = &vc->ve;
927 struct fimc_ctx *ctx = vc->ctx;
928 unsigned int width = 0, height = 0;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300929 int ret = 0;
930
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300931 /* Pre-configure format at the camera input interface, for JPEG only */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300932 if (fimc_jpeg_fourcc(pix->pixelformat)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300933 fimc_capture_try_format(ctx, &pix->width, &pix->height,
934 NULL, &pix->pixelformat,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300935 FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300936 if (try) {
937 width = pix->width;
938 height = pix->height;
939 } else {
940 ctx->s_frame.f_width = pix->width;
941 ctx->s_frame.f_height = pix->height;
942 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300943 }
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300944
945 /* Try the format at the scaler and the DMA output */
946 *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
947 NULL, &pix->pixelformat,
948 FIMC_SD_PAD_SOURCE);
949 if (*out_fmt == NULL)
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300950 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300951
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300952 /* Restore image width/height for JPEG (no resizing supported). */
953 if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
954 pix->width = width;
955 pix->height = height;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300956 }
957
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300958 /* Try to match format at the host and the sensor */
959 if (!vc->user_subdev_api) {
960 struct v4l2_mbus_framefmt mbus_fmt;
961 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300962
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300963 mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
964
965 mf->code = (*out_fmt)->mbus_code;
966 mf->width = pix->width;
967 mf->height = pix->height;
968
969 fimc_md_graph_lock(ve);
970 ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
971 fimc_md_graph_unlock(ve);
972
973 if (ret < 0)
974 return ret;
975
976 pix->width = mf->width;
977 pix->height = mf->height;
978 }
979
980 fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
981
982 if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
983 struct v4l2_subdev *sensor;
984
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300985 fimc_md_graph_lock(ve);
986
987 sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
988 if (sensor)
989 fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300990 (*out_fmt)->memplanes, try);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300991 else
992 ret = -EPIPE;
993
994 fimc_md_graph_unlock(ve);
995 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300996
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300997 return ret;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300998}
999
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001000static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
1001 struct v4l2_format *f)
1002{
1003 struct fimc_dev *fimc = video_drvdata(file);
1004 struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
1005
1006 return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
1007}
1008
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001009static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
1010 enum fimc_color_fmt color)
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001011{
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001012 bool jpeg = fimc_fmt_is_user_defined(color);
1013
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001014 ctx->scaler.enabled = !jpeg;
1015 fimc_ctrls_activate(ctx, !jpeg);
1016
1017 if (jpeg)
1018 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1019 else
1020 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1021}
1022
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001023static int __fimc_capture_set_format(struct fimc_dev *fimc,
1024 struct v4l2_format *f)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001025{
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001026 struct fimc_vid_cap *vc = &fimc->vid_cap;
1027 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001028 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001029 struct fimc_frame *ff = &ctx->d_frame;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001030 struct fimc_fmt *inp_fmt = NULL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001031 int ret, i;
1032
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001033 if (vb2_is_busy(&fimc->vid_cap.vbq))
1034 return -EBUSY;
1035
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001036 ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1037 if (ret < 0)
1038 return ret;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001039
1040 /* Update RGB Alpha control state and value range */
1041 fimc_alpha_ctrl_update(ctx);
1042
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001043 for (i = 0; i < ff->fmt->memplanes; i++) {
1044 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
Sylwester Nawrockid547ab62012-05-16 15:00:26 -03001045 ff->payload[i] = pix->plane_fmt[i].sizeimage;
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001046 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001047
1048 set_frame_bounds(ff, pix->width, pix->height);
1049 /* Reset the composition rectangle if not yet configured */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001050 if (!(ctx->state & FIMC_COMPOSE))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001051 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1052
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001053 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001054
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001055 /* Reset cropping and set format at the camera interface input */
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001056 if (!vc->user_subdev_api) {
1057 ctx->s_frame.fmt = inp_fmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001058 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1059 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1060 }
1061
1062 return ret;
1063}
1064
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001065static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1066 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001067{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001068 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001069
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001070 return __fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001071}
1072
1073static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001074 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001075{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001076 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001077 struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1078 struct v4l2_subdev *sd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001079
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001080 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001081 return -EINVAL;
1082
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001083 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001084 fimc_md_graph_lock(ve);
1085 sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1086 fimc_md_graph_unlock(ve);
1087
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001088 if (sd)
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001089 strscpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001090
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001091 return 0;
1092}
1093
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001094static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001095{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001096 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001097}
1098
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001099static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001100{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001101 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001102 return 0;
1103}
1104
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001105/**
1106 * fimc_pipeline_validate - check for formats inconsistencies
1107 * between source and sink pad of each link
Mauro Carvalho Chehab3b060ba02017-11-29 04:32:51 -05001108 * @fimc: the FIMC device this context applies to
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001109 *
1110 * Return 0 if all formats match or -EPIPE otherwise.
1111 */
1112static int fimc_pipeline_validate(struct fimc_dev *fimc)
1113{
1114 struct v4l2_subdev_format sink_fmt, src_fmt;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001115 struct fimc_vid_cap *vc = &fimc->vid_cap;
1116 struct v4l2_subdev *sd = &vc->subdev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001117 struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001118 struct media_pad *sink_pad, *src_pad;
1119 int i, ret;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001120
1121 while (1) {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001122 /*
1123 * Find current entity sink pad and any remote sink pad linked
1124 * to it. We stop if there is no sink pad in current entity or
1125 * it is not linked to any other remote entity.
1126 */
1127 src_pad = NULL;
1128
1129 for (i = 0; i < sd->entity.num_pads; i++) {
1130 struct media_pad *p = &sd->entity.pads[i];
1131
1132 if (p->flags & MEDIA_PAD_FL_SINK) {
1133 sink_pad = p;
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -03001134 src_pad = media_entity_remote_pad(sink_pad);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001135 if (src_pad)
1136 break;
1137 }
1138 }
1139
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001140 if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001141 break;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001142
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001143 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001144 if (sd == &vc->subdev) {
1145 struct fimc_frame *ff = &vc->ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001146 sink_fmt.format.width = ff->f_width;
1147 sink_fmt.format.height = ff->f_height;
1148 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1149 } else {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001150 sink_fmt.pad = sink_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001151 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1152 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1153 if (ret < 0 && ret != -ENOIOCTLCMD)
1154 return -EPIPE;
1155 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001156
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001157 /* Retrieve format at the source pad */
1158 sd = media_entity_to_v4l2_subdev(src_pad->entity);
1159 src_fmt.pad = src_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001160 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1161 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1162 if (ret < 0 && ret != -ENOIOCTLCMD)
1163 return -EPIPE;
1164
1165 if (src_fmt.format.width != sink_fmt.format.width ||
1166 src_fmt.format.height != sink_fmt.format.height ||
1167 src_fmt.format.code != sink_fmt.format.code)
1168 return -EPIPE;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001169
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001170 if (sd == p->subdevs[IDX_SENSOR] &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001171 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1172 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001173 struct fimc_frame *frame = &vc->ctx->d_frame;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001174 unsigned int i;
1175
1176 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1177 frame->fmt->memplanes,
1178 false);
1179 if (ret < 0)
1180 return -EPIPE;
1181
1182 for (i = 0; i < frame->fmt->memplanes; i++)
1183 if (frame->payload[i] < plane_fmt[i].sizeimage)
1184 return -EPIPE;
1185 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001186 }
1187 return 0;
1188}
1189
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001190static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001191 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001192{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001193 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001194 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001195 struct media_entity *entity = &vc->ve.vdev.entity;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001196 struct fimc_source_info *si = NULL;
1197 struct v4l2_subdev *sd;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001198 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001199
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001200 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001201 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001202
Sakari Ailus20b85222016-11-21 14:48:30 -02001203 ret = media_pipeline_start(entity, &vc->ve.pipe->mp);
Sakari Ailusa60a2952012-05-25 07:08:05 -03001204 if (ret < 0)
1205 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001206
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001207 sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001208 if (sd)
1209 si = v4l2_get_subdev_hostdata(sd);
1210
1211 if (si == NULL) {
1212 ret = -EPIPE;
1213 goto err_p_stop;
1214 }
1215 /*
1216 * Save configuration data related to currently attached image
1217 * sensor or other data source, e.g. FIMC-IS.
1218 */
1219 vc->source_config = *si;
1220
1221 if (vc->input == GRP_ID_FIMC_IS)
1222 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1223
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001224 if (vc->user_subdev_api) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001225 ret = fimc_pipeline_validate(fimc);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001226 if (ret < 0)
1227 goto err_p_stop;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001228 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001229
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001230 ret = vb2_ioctl_streamon(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001231 if (!ret) {
1232 vc->streaming = true;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001233 return ret;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001234 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001235
1236err_p_stop:
Sakari Ailus20b85222016-11-21 14:48:30 -02001237 media_pipeline_stop(entity);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001238 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001239}
1240
1241static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001242 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001243{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001244 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001245 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001246 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001247
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001248 ret = vb2_ioctl_streamoff(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001249 if (ret < 0)
1250 return ret;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001251
Sakari Ailus20b85222016-11-21 14:48:30 -02001252 media_pipeline_stop(&vc->ve.vdev.entity);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001253 vc->streaming = false;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001254 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001255}
1256
1257static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001258 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001259{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001260 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001261 int ret;
1262
1263 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001264
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001265 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001266 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001267
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001268 return ret;
1269}
1270
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001271static int fimc_cap_g_selection(struct file *file, void *fh,
1272 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001273{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001274 struct fimc_dev *fimc = video_drvdata(file);
1275 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001276 struct fimc_frame *f = &ctx->s_frame;
1277
Hans Verkuileaec4202017-05-08 11:35:05 -03001278 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001279 return -EINVAL;
1280
1281 switch (s->target) {
1282 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1283 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1284 f = &ctx->d_frame;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001285 /* fall through */
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001286 case V4L2_SEL_TGT_CROP_BOUNDS:
1287 case V4L2_SEL_TGT_CROP_DEFAULT:
1288 s->r.left = 0;
1289 s->r.top = 0;
1290 s->r.width = f->o_width;
1291 s->r.height = f->o_height;
1292 return 0;
1293
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001294 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001295 f = &ctx->d_frame;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001296 /* fall through */
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001297 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001298 s->r.left = f->offs_h;
1299 s->r.top = f->offs_v;
1300 s->r.width = f->width;
1301 s->r.height = f->height;
1302 return 0;
1303 }
1304
1305 return -EINVAL;
1306}
1307
1308/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
Sachin Kamat7e566be2012-05-26 11:11:54 -03001309static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001310{
1311 if (a->left < b->left || a->top < b->top)
1312 return 0;
1313 if (a->left + a->width > b->left + b->width)
1314 return 0;
1315 if (a->top + a->height > b->top + b->height)
1316 return 0;
1317
1318 return 1;
1319}
1320
1321static int fimc_cap_s_selection(struct file *file, void *fh,
1322 struct v4l2_selection *s)
1323{
1324 struct fimc_dev *fimc = video_drvdata(file);
1325 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1326 struct v4l2_rect rect = s->r;
1327 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001328 unsigned long flags;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001329
Hans Verkuileaec4202017-05-08 11:35:05 -03001330 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001331 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001332
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001333 if (s->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001334 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001335 else if (s->target == V4L2_SEL_TGT_CROP)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001336 f = &ctx->s_frame;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001337 else
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001338 return -EINVAL;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001339
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001340 fimc_capture_try_selection(ctx, &rect, s->target);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001341
1342 if (s->flags & V4L2_SEL_FLAG_LE &&
1343 !enclosed_rectangle(&rect, &s->r))
1344 return -ERANGE;
1345
1346 if (s->flags & V4L2_SEL_FLAG_GE &&
1347 !enclosed_rectangle(&s->r, &rect))
1348 return -ERANGE;
1349
1350 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001351 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001352 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1353 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001354 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001355
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001356 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001357 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001358}
1359
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001360static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -03001361 .vidioc_querycap = fimc_cap_querycap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001362
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001363 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001364 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001365 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001366 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001367
1368 .vidioc_reqbufs = fimc_cap_reqbufs,
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001369 .vidioc_querybuf = vb2_ioctl_querybuf,
1370 .vidioc_qbuf = vb2_ioctl_qbuf,
1371 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1372 .vidioc_expbuf = vb2_ioctl_expbuf,
1373 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1374 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001375
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001376 .vidioc_streamon = fimc_cap_streamon,
1377 .vidioc_streamoff = fimc_cap_streamoff,
1378
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001379 .vidioc_g_selection = fimc_cap_g_selection,
1380 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001381
1382 .vidioc_enum_input = fimc_cap_enum_input,
1383 .vidioc_s_input = fimc_cap_s_input,
1384 .vidioc_g_input = fimc_cap_g_input,
1385};
1386
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001387/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001388static int fimc_link_setup(struct media_entity *entity,
1389 const struct media_pad *local,
1390 const struct media_pad *remote, u32 flags)
1391{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001392 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1393 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001394 struct fimc_vid_cap *vc = &fimc->vid_cap;
1395 struct v4l2_subdev *sensor;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001396
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001397 if (!is_media_entity_v4l2_subdev(remote->entity))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001398 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001399
1400 if (WARN_ON(fimc == NULL))
1401 return 0;
1402
1403 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1404 local->entity->name, remote->entity->name, flags,
1405 fimc->vid_cap.input);
1406
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001407 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1408 fimc->vid_cap.input = 0;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001409 return 0;
1410 }
1411
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001412 if (vc->input != 0)
1413 return -EBUSY;
1414
1415 vc->input = sd->grp_id;
1416
1417 if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1418 return 0;
1419
1420 /* Inherit V4L2 controls from the image sensor subdev. */
1421 sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1422 if (sensor == NULL)
1423 return 0;
1424
1425 return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
Hans Verkuilda1b1ae2018-05-21 04:54:36 -04001426 sensor->ctrl_handler, NULL, true);
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001427}
1428
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001429static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001430 .link_setup = fimc_link_setup,
1431};
1432
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001433/**
1434 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1435 * @sd: pointer to a subdev generating the notification
1436 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1437 * @arg: pointer to an u32 type integer that stores the frame payload value
1438 *
1439 * The End Of Frame notification sent by sensor subdev in its still capture
1440 * mode. If there is only a single VSYNC generated by the sensor at the
1441 * beginning of a frame transmission, FIMC does not issue the LastIrq
1442 * (end of frame) interrupt. And this notification is used to complete the
1443 * frame capture and returning a buffer to user-space. Subdev drivers should
1444 * call this notification from their last 'End of frame capture' interrupt.
1445 */
1446void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1447 void *arg)
1448{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001449 struct fimc_source_info *si;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001450 struct fimc_vid_buffer *buf;
1451 struct fimc_md *fmd;
1452 struct fimc_dev *fimc;
1453 unsigned long flags;
1454
1455 if (sd == NULL)
1456 return;
1457
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001458 si = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001459 fmd = entity_to_fimc_mdev(&sd->entity);
1460
1461 spin_lock_irqsave(&fmd->slock, flags);
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001462
1463 fimc = si ? source_to_sensor_info(si)->host : NULL;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001464
1465 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1466 test_bit(ST_CAPT_PEND, &fimc->state)) {
1467 unsigned long irq_flags;
1468 spin_lock_irqsave(&fimc->slock, irq_flags);
1469 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1470 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1471 struct fimc_vid_buffer, list);
Junghak Sung2d700712015-09-22 10:30:30 -03001472 vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1473 *((u32 *)arg));
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001474 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001475 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001476 fimc_deactivate_capture(fimc);
1477 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1478 }
1479 spin_unlock_irqrestore(&fmd->slock, flags);
1480}
1481
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001482static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001483 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001484 struct v4l2_subdev_mbus_code_enum *code)
1485{
1486 struct fimc_fmt *fmt;
1487
1488 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1489 if (!fmt)
1490 return -EINVAL;
1491 code->code = fmt->mbus_code;
1492 return 0;
1493}
1494
1495static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001496 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001497 struct v4l2_subdev_format *fmt)
1498{
1499 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1500 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001501 struct fimc_frame *ff = &ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001502 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001503
1504 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
Hans Verkuilf7234132015-03-04 01:47:54 -08001505 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001506 fmt->format = *mf;
1507 return 0;
1508 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001509
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001510 mf = &fmt->format;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001511 mutex_lock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001512
1513 switch (fmt->pad) {
1514 case FIMC_SD_PAD_SOURCE:
1515 if (!WARN_ON(ff->fmt == NULL))
1516 mf->code = ff->fmt->mbus_code;
1517 /* Sink pads crop rectangle size */
1518 mf->width = ff->width;
1519 mf->height = ff->height;
1520 break;
1521 case FIMC_SD_PAD_SINK_FIFO:
1522 *mf = fimc->vid_cap.wb_fmt;
1523 break;
1524 case FIMC_SD_PAD_SINK_CAM:
1525 default:
1526 *mf = fimc->vid_cap.ci_fmt;
1527 break;
1528 }
1529
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001530 mutex_unlock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001531 mf->colorspace = V4L2_COLORSPACE_JPEG;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001532
1533 return 0;
1534}
1535
1536static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001537 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001538 struct v4l2_subdev_format *fmt)
1539{
1540 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1541 struct v4l2_mbus_framefmt *mf = &fmt->format;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001542 struct fimc_vid_cap *vc = &fimc->vid_cap;
1543 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001544 struct fimc_frame *ff;
1545 struct fimc_fmt *ffmt;
1546
1547 dbg("pad%d: code: 0x%x, %dx%d",
1548 fmt->pad, mf->code, mf->width, mf->height);
1549
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001550 if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001551 return -EBUSY;
1552
1553 mutex_lock(&fimc->lock);
1554 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1555 &mf->code, NULL, fmt->pad);
1556 mutex_unlock(&fimc->lock);
1557 mf->colorspace = V4L2_COLORSPACE_JPEG;
1558
1559 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
Hans Verkuilf7234132015-03-04 01:47:54 -08001560 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001561 *mf = fmt->format;
1562 return 0;
1563 }
Sylwester Nawrocki66125452012-12-05 13:52:00 -03001564 /* There must be a bug in the driver if this happens */
1565 if (WARN_ON(ffmt == NULL))
1566 return -EINVAL;
1567
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001568 /* Update RGB Alpha control state and value range */
1569 fimc_alpha_ctrl_update(ctx);
1570
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001571 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001572 if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1573 ff = &ctx->d_frame;
1574 /* Sink pads crop rectangle size */
1575 mf->width = ctx->s_frame.width;
1576 mf->height = ctx->s_frame.height;
1577 } else {
1578 ff = &ctx->s_frame;
1579 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001580
1581 mutex_lock(&fimc->lock);
1582 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001583
1584 if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1585 vc->wb_fmt = *mf;
1586 else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1587 vc->ci_fmt = *mf;
1588
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001589 ff->fmt = ffmt;
1590
1591 /* Reset the crop rectangle if required. */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001592 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001593 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1594
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001595 if (fmt->pad != FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001596 ctx->state &= ~FIMC_COMPOSE;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001597
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001598 mutex_unlock(&fimc->lock);
1599 return 0;
1600}
1601
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001602static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001603 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001604 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001605{
1606 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1607 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001608 struct fimc_frame *f = &ctx->s_frame;
1609 struct v4l2_rect *r = &sel->r;
1610 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001611
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001612 if (sel->pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001613 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001614
1615 mutex_lock(&fimc->lock);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001616
1617 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001618 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001619 f = &ctx->d_frame;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001620 /* fall through */
Sakari Ailus5689b282012-05-18 09:31:18 -03001621 case V4L2_SEL_TGT_CROP_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001622 r->width = f->o_width;
1623 r->height = f->o_height;
1624 r->left = 0;
1625 r->top = 0;
1626 mutex_unlock(&fimc->lock);
1627 return 0;
1628
Sakari Ailus5689b282012-05-18 09:31:18 -03001629 case V4L2_SEL_TGT_CROP:
Hans Verkuilf7234132015-03-04 01:47:54 -08001630 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001631 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001632 case V4L2_SEL_TGT_COMPOSE:
Hans Verkuilf7234132015-03-04 01:47:54 -08001633 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001634 f = &ctx->d_frame;
1635 break;
1636 default:
1637 mutex_unlock(&fimc->lock);
1638 return -EINVAL;
1639 }
1640
1641 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1642 sel->r = *try_sel;
1643 } else {
1644 r->left = f->offs_h;
1645 r->top = f->offs_v;
1646 r->width = f->width;
1647 r->height = f->height;
1648 }
1649
1650 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1651 sel->pad, r->left, r->top, r->width, r->height,
1652 f->f_width, f->f_height);
1653
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001654 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001655 return 0;
1656}
1657
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001658static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001659 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001660 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001661{
1662 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1663 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001664 struct fimc_frame *f = &ctx->s_frame;
1665 struct v4l2_rect *r = &sel->r;
1666 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001667 unsigned long flags;
1668
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001669 if (sel->pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001670 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001671
1672 mutex_lock(&fimc->lock);
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001673 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001674
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001675 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001676 case V4L2_SEL_TGT_CROP:
Hans Verkuilf7234132015-03-04 01:47:54 -08001677 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001678 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001679 case V4L2_SEL_TGT_COMPOSE:
Hans Verkuilf7234132015-03-04 01:47:54 -08001680 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001681 f = &ctx->d_frame;
1682 break;
1683 default:
1684 mutex_unlock(&fimc->lock);
1685 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001686 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001687
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001688 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1689 *try_sel = sel->r;
1690 } else {
1691 spin_lock_irqsave(&fimc->slock, flags);
1692 set_frame_crop(f, r->left, r->top, r->width, r->height);
1693 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sakari Ailus5689b282012-05-18 09:31:18 -03001694 if (sel->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001695 ctx->state |= FIMC_COMPOSE;
Sylwester Nawrocki8b164102013-01-30 09:55:46 -03001696 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001697 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001698
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001699 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001700 r->width, r->height);
1701
1702 mutex_unlock(&fimc->lock);
1703 return 0;
1704}
1705
Bhumika Goyala2fafda2016-12-12 16:03:57 -02001706static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001707 .enum_mbus_code = fimc_subdev_enum_mbus_code,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001708 .get_selection = fimc_subdev_get_selection,
1709 .set_selection = fimc_subdev_set_selection,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001710 .get_fmt = fimc_subdev_get_fmt,
1711 .set_fmt = fimc_subdev_set_fmt,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001712};
1713
Bhumika Goyala2fafda2016-12-12 16:03:57 -02001714static const struct v4l2_subdev_ops fimc_subdev_ops = {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001715 .pad = &fimc_subdev_pad_ops,
1716};
1717
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001718/* Set default format at the sensor and host interface */
1719static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1720{
1721 struct v4l2_format fmt = {
1722 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1723 .fmt.pix_mp = {
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001724 .width = FIMC_DEFAULT_WIDTH,
1725 .height = FIMC_DEFAULT_HEIGHT,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001726 .pixelformat = V4L2_PIX_FMT_YUYV,
1727 .field = V4L2_FIELD_NONE,
1728 .colorspace = V4L2_COLORSPACE_JPEG,
1729 },
1730 };
1731
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001732 return __fimc_capture_set_format(fimc, &fmt);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001733}
1734
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001735/* fimc->lock must be already initialized */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001736static int fimc_register_capture_device(struct fimc_dev *fimc,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001737 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001738{
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001739 struct video_device *vfd = &fimc->vid_cap.ve.vdev;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001740 struct vb2_queue *q = &fimc->vid_cap.vbq;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001741 struct fimc_ctx *ctx;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001742 struct fimc_vid_cap *vid_cap;
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001743 struct fimc_fmt *fmt;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001744 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001745
Sachin Kamat26ee7f42012-08-17 03:28:27 -03001746 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001747 if (!ctx)
1748 return -ENOMEM;
1749
1750 ctx->fimc_dev = fimc;
Sylwester Nawrocki3d112d92012-04-26 06:26:29 -03001751 ctx->in_path = FIMC_IO_CAMERA;
1752 ctx->out_path = FIMC_IO_DMA;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001753 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001754 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001755 ctx->d_frame.fmt = ctx->s_frame.fmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001756
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001757 memset(vfd, 0, sizeof(*vfd));
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001758 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001759
1760 vfd->fops = &fimc_capture_fops;
1761 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001762 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001763 vfd->minor = -1;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001764 vfd->release = video_device_release_empty;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001765 vfd->queue = q;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001766 vfd->lock = &fimc->lock;
Boris Brezillon1ddc8a92019-06-04 03:06:24 -04001767 vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -03001768
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001769 video_set_drvdata(vfd, fimc);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001770 vid_cap = &fimc->vid_cap;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001771 vid_cap->active_buf_cnt = 0;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001772 vid_cap->reqbufs_count = 0;
1773 vid_cap->ctx = ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001774
1775 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1776 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001777
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001778 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001779 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Tomasz Stanislawski9bd09fd2012-06-14 10:37:49 -03001780 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001781 q->drv_priv = ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001782 q->ops = &fimc_capture_qops;
1783 q->mem_ops = &vb2_dma_contig_memops;
1784 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -03001785 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001786 q->lock = &fimc->lock;
Hans Verkuil2548fee2016-02-16 07:30:19 -02001787 q->dev = &fimc->pdev->dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001788
Sylwester Nawrocki41fd0872012-10-19 08:24:18 -03001789 ret = vb2_queue_init(q);
1790 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001791 goto err_free_ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001792
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001793 /* Default format configuration */
1794 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1795 vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1796 vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1797 vid_cap->ci_fmt.code = fmt->mbus_code;
1798
1799 ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1800 ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1801 ctx->s_frame.fmt = fmt;
1802
1803 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1804 vid_cap->wb_fmt = vid_cap->ci_fmt;
1805 vid_cap->wb_fmt.code = fmt->mbus_code;
1806
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001807 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
Sylwester Nawrockibae45002016-09-01 08:47:05 -03001808 vfd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001809 ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001810 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001811 goto err_free_ctx;
1812
1813 ret = fimc_ctrls_create(ctx);
1814 if (ret)
1815 goto err_me_cleanup;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001816
1817 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001818 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001819 goto err_ctrl_free;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001820
1821 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1822 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001823
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -03001824 vfd->ctrl_handler = &ctx->ctrls.handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001825 return 0;
1826
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001827err_ctrl_free:
1828 fimc_ctrls_delete(ctx);
1829err_me_cleanup:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001830 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001831err_free_ctx:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001832 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001833 return ret;
1834}
1835
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001836static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001837{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001838 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1839 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001840
Sylwester Nawrockibbc52962012-07-26 07:59:11 -03001841 if (fimc == NULL)
1842 return -ENXIO;
1843
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001844 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1845 if (ret)
1846 return ret;
1847
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001848 fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001849
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001850 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001851 if (ret) {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001852 fimc_unregister_m2m_device(fimc);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001853 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001854 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001855
1856 return ret;
1857}
1858
1859static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1860{
1861 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001862 struct video_device *vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001863
1864 if (fimc == NULL)
1865 return;
1866
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001867 mutex_lock(&fimc->lock);
1868
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001869 fimc_unregister_m2m_device(fimc);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001870 vdev = &fimc->vid_cap.ve.vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001871
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001872 if (video_is_registered(vdev)) {
1873 video_unregister_device(vdev);
1874 media_entity_cleanup(&vdev->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001875 fimc_ctrls_delete(fimc->vid_cap.ctx);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001876 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001877 }
1878 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001879 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001880
1881 mutex_unlock(&fimc->lock);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001882}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001883
1884static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1885 .registered = fimc_capture_subdev_registered,
1886 .unregistered = fimc_capture_subdev_unregistered,
1887};
1888
1889int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1890{
1891 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1892 int ret;
1893
1894 v4l2_subdev_init(sd, &fimc_subdev_ops);
Sylwester Nawrocki5a665612013-04-18 08:49:54 -03001895 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -03001896 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001897
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001898 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1899 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001900 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001901 ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -03001902 fimc->vid_cap.sd_pads);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001903 if (ret)
1904 return ret;
1905
1906 sd->entity.ops = &fimc_sd_media_ops;
1907 sd->internal_ops = &fimc_capture_sd_internal_ops;
1908 v4l2_set_subdevdata(sd, fimc);
1909 return 0;
1910}
1911
1912void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1913{
1914 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1915
1916 v4l2_device_unregister_subdev(sd);
1917 media_entity_cleanup(&sd->entity);
1918 v4l2_set_subdevdata(sd, NULL);
1919}