blob: dd33b888971fb974c175e70d39e7c4070eb3a93a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03002/*
Sylwester Nawrocki3a3f9442011-04-28 09:06:19 -03003 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03004 *
Sylwester Nawrocki0c9204d2012-04-25 06:55:42 -03005 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
6 * Sylwester Nawrocki <s.nawrocki@samsung.com>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03007 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030011#include <linux/types.h>
12#include <linux/errno.h>
13#include <linux/bug.h>
14#include <linux/interrupt.h>
15#include <linux/device.h>
Sylwester Nawrockie9e21082011-09-02 06:25:32 -030016#include <linux/pm_runtime.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030017#include <linux/list.h>
18#include <linux/slab.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030019
20#include <linux/videodev2.h>
21#include <media/v4l2-device.h>
22#include <media/v4l2-ioctl.h>
23#include <media/v4l2-mem2mem.h>
Junghak Sungc1399902015-09-22 10:30:29 -030024#include <media/videobuf2-v4l2.h>
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030025#include <media/videobuf2-dma-contig.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030026
Sylwester Nawrocki44031062013-05-31 11:37:19 -030027#include "common.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030028#include "fimc-core.h"
Sylwester Nawrockic83a1ff2012-05-02 06:14:49 -030029#include "fimc-reg.h"
Sylwester Nawrocki44031062013-05-31 11:37:19 -030030#include "media-dev.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030031
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030032static int fimc_capture_hw_init(struct fimc_dev *fimc)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030033{
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030034 struct fimc_source_info *si = &fimc->vid_cap.source_config;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030035 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030036 int ret;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030037 unsigned long flags;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030038
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030039 if (ctx == NULL || ctx->s_frame.fmt == NULL)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030040 return -EINVAL;
41
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030042 if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
43 ret = fimc_hw_camblk_cfg_writeback(fimc);
44 if (ret < 0)
45 return ret;
46 }
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030047
48 spin_lock_irqsave(&fimc->slock, flags);
49 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
50 fimc_set_yuv_order(ctx);
51
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030052 fimc_hw_set_camera_polarity(fimc, si);
53 fimc_hw_set_camera_type(fimc, si);
54 fimc_hw_set_camera_source(fimc, si);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030055 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
56
57 ret = fimc_set_scaler_info(ctx);
58 if (!ret) {
59 fimc_hw_set_input_path(ctx);
60 fimc_hw_set_prescaler(ctx);
61 fimc_hw_set_mainscaler(ctx);
62 fimc_hw_set_target_format(ctx);
63 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -030064 fimc_hw_set_effect(ctx);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030065 fimc_hw_set_output_path(ctx);
66 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -030067 if (fimc->drv_data->alpha_color)
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -030068 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -030069 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030070 }
71 spin_unlock_irqrestore(&fimc->slock, flags);
72 return ret;
73}
74
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030075/*
76 * Reinitialize the driver so it is ready to start the streaming again.
77 * Set fimc->state to indicate stream off and the hardware shut down state.
78 * If not suspending (@suspend is false), return any buffers to videobuf2.
79 * Otherwise put any owned buffers onto the pending buffers queue, so they
80 * can be re-spun when the device is being resumed. Also perform FIMC
81 * software reset and disable streaming on the whole pipeline if required.
82 */
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030083static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030084{
Marek Szyprowskibd323e22011-08-29 08:51:49 -030085 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030086 struct fimc_vid_buffer *buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -030087 unsigned long flags;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030088 bool streaming;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030089
90 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030091 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030092
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030093 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
94 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrockiaa3331222012-03-16 11:47:51 -030095 if (suspend)
96 fimc->state |= (1 << ST_CAPT_SUSPENDED);
97 else
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030098 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030099
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300100 /* Release unused buffers */
101 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300102 buf = fimc_pending_queue_pop(cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300103 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300104 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300105 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300106 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300107 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300108 if (suspend)
109 fimc_pending_queue_add(cap, buf);
110 else
Junghak Sung2d700712015-09-22 10:30:30 -0300111 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300112 }
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300113
114 fimc_hw_reset(fimc);
115 cap->buf_index = 0;
116
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300117 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300118
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300119 if (streaming)
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300120 return fimc_pipeline_call(&cap->ve, set_stream, 0);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300121 else
122 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300123}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300124
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300125static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300126{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300127 unsigned long flags;
128
129 if (!fimc_capture_active(fimc))
130 return 0;
131
132 spin_lock_irqsave(&fimc->slock, flags);
133 set_bit(ST_CAPT_SHUT, &fimc->state);
134 fimc_deactivate_capture(fimc);
135 spin_unlock_irqrestore(&fimc->slock, flags);
136
137 wait_event_timeout(fimc->irq_queue,
138 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300139 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300140
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300141 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300142}
143
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300144/**
145 * fimc_capture_config_update - apply the camera interface configuration
Mauro Carvalho Chehab3b060ba02017-11-29 04:32:51 -0500146 * @ctx: FIMC capture context
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300147 *
148 * To be called from within the interrupt handler with fimc.slock
149 * spinlock held. It updates the camera pixel crop, rotation and
150 * image flip in H/W.
151 */
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300152static int fimc_capture_config_update(struct fimc_ctx *ctx)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300153{
154 struct fimc_dev *fimc = ctx->fimc_dev;
155 int ret;
156
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300157 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300158
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300159 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300160 if (ret)
161 return ret;
162
163 fimc_hw_set_prescaler(ctx);
164 fimc_hw_set_mainscaler(ctx);
165 fimc_hw_set_target_format(ctx);
166 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300167 fimc_hw_set_effect(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300168 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
169 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -0300170 if (fimc->drv_data->alpha_color)
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300171 fimc_hw_set_rgb_alpha(ctx);
172
173 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300174 return ret;
175}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300176
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300177void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
178{
179 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300180 struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
181 struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300182 struct fimc_frame *f = &cap->ctx->d_frame;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300183 struct fimc_vid_buffer *v_buf;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300184
185 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
186 wake_up(&fimc->irq_queue);
187 goto done;
188 }
189
190 if (!list_empty(&cap->active_buf_q) &&
191 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300192 v_buf = fimc_active_queue_pop(cap);
193
Junghak Sungd6dd6452015-11-03 08:16:37 -0200194 v_buf->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300195 v_buf->vb.sequence = cap->frame_count++;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300196
Junghak Sung2d700712015-09-22 10:30:30 -0300197 vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300198 }
199
200 if (!list_empty(&cap->pending_buf_q)) {
201
202 v_buf = fimc_pending_queue_pop(cap);
203 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
204 v_buf->index = cap->buf_index;
205
206 /* Move the buffer to the capture active queue */
207 fimc_active_queue_add(cap, v_buf);
208
209 dbg("next frame: %d, done frame: %d",
210 fimc_hw_get_frame_index(fimc), v_buf->index);
211
212 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
213 cap->buf_index = 0;
214 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300215 /*
216 * Set up a buffer at MIPI-CSIS if current image format
217 * requires the frame embedded data capture.
218 */
219 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
220 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
221 unsigned int size = f->payload[plane];
222 s32 index = fimc_hw_get_frame_index(fimc);
223 void *vaddr;
224
225 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
226 if (v_buf->index != index)
227 continue;
Junghak Sung2d700712015-09-22 10:30:30 -0300228 vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300229 v4l2_subdev_call(csis, video, s_rx_buffer,
230 vaddr, &size);
231 break;
232 }
233 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300234
235 if (cap->active_buf_cnt == 0) {
236 if (deq_buf)
237 clear_bit(ST_CAPT_RUN, &fimc->state);
238
239 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
240 cap->buf_index = 0;
241 } else {
242 set_bit(ST_CAPT_RUN, &fimc->state);
243 }
244
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300245 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
246 fimc_capture_config_update(cap->ctx);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300247done:
248 if (cap->active_buf_cnt == 1) {
249 fimc_deactivate_capture(fimc);
250 clear_bit(ST_CAPT_STREAM, &fimc->state);
251 }
252
253 dbg("frame: %d, active_buf_cnt: %d",
254 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
255}
256
257
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300258static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300259{
260 struct fimc_ctx *ctx = q->drv_priv;
261 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300262 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300263 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300264 int ret;
265
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300266 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300267
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300268 ret = fimc_capture_hw_init(fimc);
269 if (ret) {
270 fimc_capture_state_cleanup(fimc, false);
271 return ret;
272 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300273
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300274 set_bit(ST_CAPT_PEND, &fimc->state);
275
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300276 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
277
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300278 if (vid_cap->active_buf_cnt >= min_bufs &&
279 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300280 fimc_activate_capture(ctx);
281
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300282 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300283 return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300284 }
285
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300286 return 0;
287}
288
Hans Verkuile37559b2014-04-17 02:47:21 -0300289static void stop_streaming(struct vb2_queue *q)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300290{
291 struct fimc_ctx *ctx = q->drv_priv;
292 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300293
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300294 if (!fimc_capture_active(fimc))
Hans Verkuile37559b2014-04-17 02:47:21 -0300295 return;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300296
Hans Verkuile37559b2014-04-17 02:47:21 -0300297 fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300298}
299
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300300int fimc_capture_suspend(struct fimc_dev *fimc)
301{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300302 bool suspend = fimc_capture_busy(fimc);
303
304 int ret = fimc_stop_capture(fimc, suspend);
305 if (ret)
306 return ret;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300307 return fimc_pipeline_call(&fimc->vid_cap.ve, close);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300308}
309
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300310static void buffer_queue(struct vb2_buffer *vb);
311
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300312int fimc_capture_resume(struct fimc_dev *fimc)
313{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300314 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300315 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300316 struct fimc_vid_buffer *buf;
317 int i;
318
319 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
320 return 0;
321
322 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
323 vid_cap->buf_index = 0;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300324 fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300325 fimc_capture_hw_init(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300326
327 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
328
329 for (i = 0; i < vid_cap->reqbufs_count; i++) {
330 if (list_empty(&vid_cap->pending_buf_q))
331 break;
332 buf = fimc_pending_queue_pop(vid_cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300333 buffer_queue(&buf->vb.vb2_buf);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300334 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300335 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300336
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300337}
338
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200339static int queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300340 unsigned int *num_buffers, unsigned int *num_planes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300341 unsigned int sizes[], struct device *alloc_devs[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300342{
343 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300344 struct fimc_frame *frame = &ctx->d_frame;
345 struct fimc_fmt *fmt = frame->fmt;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200346 unsigned long wh = frame->f_width * frame->f_height;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300347 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300348
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300349 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300350 return -EINVAL;
351
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200352 if (*num_planes) {
353 if (*num_planes != fmt->memplanes)
354 return -EINVAL;
Hans Verkuil2548fee2016-02-16 07:30:19 -0200355 for (i = 0; i < *num_planes; i++)
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200356 if (sizes[i] < (wh * fmt->depth[i]) / 8)
357 return -EINVAL;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200358 return 0;
359 }
360
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300361 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300362
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300363 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300364 unsigned int size = (wh * fmt->depth[i]) / 8;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200365
366 if (fimc_fmt_is_user_defined(fmt->color))
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300367 sizes[i] = frame->payload[i];
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300368 else
Sylwester Nawrockid547ab62012-05-16 15:00:26 -0300369 sizes[i] = max_t(u32, size, frame->payload[i]);
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300370 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300371
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300372 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300373}
374
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300375static int buffer_prepare(struct vb2_buffer *vb)
376{
377 struct vb2_queue *vq = vb->vb2_queue;
378 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300379 int i;
380
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300381 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300382 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300383
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300384 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300385 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300386
387 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300388 v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300389 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300390 vb2_plane_size(vb, i), size);
391 return -EINVAL;
392 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300393 vb2_set_plane_payload(vb, i, size);
394 }
395
396 return 0;
397}
398
399static void buffer_queue(struct vb2_buffer *vb)
400{
Junghak Sung2d700712015-09-22 10:30:30 -0300401 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300402 struct fimc_vid_buffer *buf
Junghak Sung2d700712015-09-22 10:30:30 -0300403 = container_of(vbuf, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300404 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
405 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300406 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300407 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300408 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300409 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300410
411 spin_lock_irqsave(&fimc->slock, flags);
Junghak Sung2d700712015-09-22 10:30:30 -0300412 fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300413
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300414 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
415 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
416 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300417 /* Setup the buffer directly for processing. */
418 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
419 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300420
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300421 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
422 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300423 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300424
425 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
426 vid_cap->buf_index = 0;
427 } else {
428 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300429 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300430
431 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
432
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300433
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300434 if (vb2_is_streaming(&vid_cap->vbq) &&
435 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300436 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Andrzej Hajda76323e52013-02-27 08:13:59 -0300437 int ret;
438
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300439 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300440 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300441
Andrzej Hajda76323e52013-02-27 08:13:59 -0300442 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
443 return;
444
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300445 ret = fimc_pipeline_call(ve, set_stream, 1);
Andrzej Hajda76323e52013-02-27 08:13:59 -0300446 if (ret < 0)
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300447 v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300448 return;
449 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300450 spin_unlock_irqrestore(&fimc->slock, flags);
451}
452
Julia Lawallb7b361f2016-09-08 20:59:10 -0300453static const struct vb2_ops fimc_capture_qops = {
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300454 .queue_setup = queue_setup,
455 .buf_prepare = buffer_prepare,
456 .buf_queue = buffer_queue,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300457 .wait_prepare = vb2_ops_wait_prepare,
458 .wait_finish = vb2_ops_wait_finish,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300459 .start_streaming = start_streaming,
460 .stop_streaming = stop_streaming,
461};
462
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300463static int fimc_capture_set_default_format(struct fimc_dev *fimc);
464
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300465static int fimc_capture_open(struct file *file)
466{
467 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300468 struct fimc_vid_cap *vc = &fimc->vid_cap;
469 struct exynos_video_entity *ve = &vc->ve;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300470 int ret = -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300471
472 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
473
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300474 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300475
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300476 if (fimc_m2m_active(fimc))
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300477 goto unlock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300478
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300479 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300480 ret = pm_runtime_get_sync(&fimc->pdev->dev);
Dinghao Liue40a8772020-05-21 12:22:04 +0200481 if (ret < 0) {
482 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300483 goto unlock;
Dinghao Liue40a8772020-05-21 12:22:04 +0200484 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300485
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300486 ret = v4l2_fh_open(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300487 if (ret) {
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300488 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300489 goto unlock;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300490 }
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300491
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300492 if (v4l2_fh_is_singular_file(file)) {
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300493 fimc_md_graph_lock(ve);
494
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300495 ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
496
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300497 if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
498 /*
499 * Recreate controls of the the video node to drop
500 * any controls inherited from the sensor subdev.
501 */
502 fimc_ctrls_delete(vc->ctx);
503
504 ret = fimc_ctrls_create(vc->ctx);
505 if (ret == 0)
506 vc->inh_sensor_ctrls = false;
507 }
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300508 if (ret == 0)
509 ve->vdev.entity.use_count++;
510
511 fimc_md_graph_unlock(ve);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300512
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300513 if (ret == 0)
514 ret = fimc_capture_set_default_format(fimc);
515
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300516 if (ret < 0) {
517 clear_bit(ST_CAPT_BUSY, &fimc->state);
518 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300519 v4l2_fh_release(file);
520 }
521 }
522unlock:
523 mutex_unlock(&fimc->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300524 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300525}
526
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300527static int fimc_capture_release(struct file *file)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300528{
529 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -0300530 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300531 bool close = v4l2_fh_is_singular_file(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300532 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300533
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300534 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
535
Sylwester Nawrockiba6b3722012-11-22 11:12:16 -0300536 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300537
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300538 if (close && vc->streaming) {
Sakari Ailus20b85222016-11-21 14:48:30 -0200539 media_pipeline_stop(&vc->ve.vdev.entity);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300540 vc->streaming = false;
541 }
542
Ricardo Ribalda1380f572013-11-25 05:49:02 -0300543 ret = _vb2_fop_release(file, NULL);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300544
545 if (close) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300546 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300547 fimc_pipeline_call(&vc->ve, close);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300548 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300549
550 fimc_md_graph_lock(&vc->ve);
551 vc->ve.vdev.entity.use_count--;
552 fimc_md_graph_unlock(&vc->ve);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300553 }
554
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300555 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300556 mutex_unlock(&fimc->lock);
557
558 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300559}
560
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300561static const struct v4l2_file_operations fimc_capture_fops = {
562 .owner = THIS_MODULE,
563 .open = fimc_capture_open,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300564 .release = fimc_capture_release,
565 .poll = vb2_fop_poll,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300566 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300567 .mmap = vb2_fop_mmap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300568};
569
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300570/*
571 * Format and crop negotiation helpers
572 */
573
574static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
575 u32 *width, u32 *height,
576 u32 *code, u32 *fourcc, int pad)
577{
578 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
579 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300580 const struct fimc_variant *var = fimc->variant;
581 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300582 struct fimc_frame *dst = &ctx->d_frame;
583 u32 depth, min_w, max_w, min_h, align_h = 3;
584 u32 mask = FMT_FLAGS_CAM;
585 struct fimc_fmt *ffmt;
586
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300587 /* Conversion from/to JPEG or User Defined format is not supported */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300588 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300589 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
590 *code = ctx->s_frame.fmt->mbus_code;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300591
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300592 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300593 mask |= FMT_FLAGS_M2M;
594
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300595 if (pad == FIMC_SD_PAD_SINK_FIFO)
596 mask = FMT_FLAGS_WRITEBACK;
597
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300598 ffmt = fimc_find_format(fourcc, code, mask, 0);
599 if (WARN_ON(!ffmt))
600 return NULL;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300601
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300602 if (code)
603 *code = ffmt->mbus_code;
604 if (fourcc)
605 *fourcc = ffmt->fourcc;
606
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300607 if (pad != FIMC_SD_PAD_SOURCE) {
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300608 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300609 pl->scaler_dis_w : pl->scaler_en_w;
610 /* Apply the camera input interface pixel constraints */
611 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
612 height, max_t(u32, *height, 32),
613 FIMC_CAMIF_MAX_HEIGHT,
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300614 fimc_fmt_is_user_defined(ffmt->color) ?
615 3 : 1,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300616 0);
617 return ffmt;
618 }
619 /* Can't scale or crop in transparent (JPEG) transfer mode */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300620 if (fimc_fmt_is_user_defined(ffmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300621 *width = ctx->s_frame.f_width;
622 *height = ctx->s_frame.f_height;
623 return ffmt;
624 }
625 /* Apply the scaler and the output DMA constraints */
626 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300627 if (ctx->state & FIMC_COMPOSE) {
628 min_w = dst->offs_h + dst->width;
629 min_h = dst->offs_v + dst->height;
630 } else {
631 min_w = var->min_out_pixsize;
632 min_h = var->min_out_pixsize;
633 }
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300634 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300635 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
636
637 depth = fimc_get_format_depth(ffmt);
638 v4l_bound_align_image(width, min_w, max_w,
639 ffs(var->min_out_pixsize) - 1,
640 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
641 align_h,
642 64/(ALIGN(depth, 8)));
643
644 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
645 pad, code ? *code : 0, *width, *height,
646 dst->f_width, dst->f_height);
647
648 return ffmt;
649}
650
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300651static void fimc_capture_try_selection(struct fimc_ctx *ctx,
652 struct v4l2_rect *r,
653 int target)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300654{
655 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
656 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300657 const struct fimc_variant *var = fimc->variant;
658 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300659 struct fimc_frame *sink = &ctx->s_frame;
660 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
661 u32 align_sz = 0, align_h = 4;
662 u32 max_sc_h, max_sc_v;
663
664 /* In JPEG transparent transfer mode cropping is not supported */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300665 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300666 r->width = sink->f_width;
667 r->height = sink->f_height;
668 r->left = r->top = 0;
669 return;
670 }
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300671 if (target == V4L2_SEL_TGT_COMPOSE) {
Mauro Carvalho Chehab6ccd2282018-03-23 08:54:22 -0400672 u32 tmp_min_h = ffs(sink->width) - 3;
673 u32 tmp_min_v = ffs(sink->height) - 1;
674
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300675 if (ctx->rotation != 90 && ctx->rotation != 270)
676 align_h = 1;
Mauro Carvalho Chehab6ccd2282018-03-23 08:54:22 -0400677 max_sc_h = min(SCALER_MAX_HRATIO, 1 << tmp_min_h);
678 max_sc_v = min(SCALER_MAX_VRATIO, 1 << tmp_min_v);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300679 min_sz = var->min_out_pixsize;
680 } else {
681 u32 depth = fimc_get_format_depth(sink->fmt);
682 align_sz = 64/ALIGN(depth, 8);
683 min_sz = var->min_inp_pixsize;
684 min_w = min_h = min_sz;
685 max_sc_h = max_sc_v = 1;
686 }
687 /*
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300688 * For the compose rectangle the following constraints must be met:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300689 * - it must fit in the sink pad format rectangle (f_width/f_height);
690 * - maximum downscaling ratio is 64;
691 * - maximum crop size depends if the rotator is used or not;
692 * - the sink pad format width/height must be 4 multiple of the
693 * prescaler ratios determined by sink pad size and source pad crop,
694 * the prescaler ratio is returned by fimc_get_scaler_factor().
695 */
696 max_w = min_t(u32,
697 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
698 rotate ? sink->f_height : sink->f_width);
699 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300700
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300701 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300702 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
703 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
704 if (rotate) {
705 swap(max_sc_h, max_sc_v);
706 swap(min_w, min_h);
707 }
708 }
709 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
710 &r->height, min_h, max_h, align_h,
711 align_sz);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300712 /* Adjust left/top if crop/compose rectangle is out of bounds */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300713 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
714 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
715 r->left = round_down(r->left, var->hor_offs_align);
716
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300717 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
718 target, r->left, r->top, r->width, r->height,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300719 sink->f_width, sink->f_height);
720}
721
722/*
723 * The video node ioctl operations
724 */
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -0300725static int fimc_cap_querycap(struct file *file, void *priv,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300726 struct v4l2_capability *cap)
727{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300728 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300729
Boris Brezillon1ddc8a92019-06-04 03:06:24 -0400730 __fimc_vidioc_querycap(&fimc->pdev->dev, cap);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300731 return 0;
732}
733
Boris Brezillon7e98b7b2019-06-04 03:06:25 -0400734static int fimc_cap_enum_fmt(struct file *file, void *priv,
735 struct v4l2_fmtdesc *f)
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300736{
737 struct fimc_fmt *fmt;
738
739 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
740 f->index);
741 if (!fmt)
742 return -EINVAL;
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300743 f->pixelformat = fmt->fourcc;
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300744 return 0;
745}
746
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300747static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
748{
749 struct media_pad *pad = &me->pads[0];
750
751 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300752 pad = media_entity_remote_pad(pad);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300753 if (!pad)
754 break;
755 me = pad->entity;
756 pad = &me->pads[0];
757 }
758
759 return me;
760}
761
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300762/**
763 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
764 * elements
765 * @ctx: FIMC capture context
766 * @tfmt: media bus format to try/set on subdevs
767 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
768 * @set: true to set format on subdevs, false to try only
769 */
770static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
771 struct v4l2_mbus_framefmt *tfmt,
772 struct fimc_fmt **fmt_id,
773 bool set)
774{
775 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300776 struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
777 struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300778 struct v4l2_subdev_format sfmt;
779 struct v4l2_mbus_framefmt *mf = &sfmt.format;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300780 struct media_entity *me;
781 struct fimc_fmt *ffmt;
782 struct media_pad *pad;
783 int ret, i = 1;
784 u32 fcc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300785
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300786 if (WARN_ON(!sd || !tfmt))
787 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300788
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300789 memset(&sfmt, 0, sizeof(sfmt));
790 sfmt.format = *tfmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300791 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300792
793 me = fimc_pipeline_get_head(&sd->entity);
794
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300795 while (1) {
796 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
797 FMT_FLAGS_CAM, i++);
798 if (ffmt == NULL) {
799 /*
800 * Notify user-space if common pixel code for
801 * host and sensor does not exist.
802 */
803 return -EINVAL;
804 }
805 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300806
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300807 /* set format on all pipeline subdevs */
808 while (me != &fimc->vid_cap.subdev.entity) {
809 sd = media_entity_to_v4l2_subdev(me);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300810
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300811 sfmt.pad = 0;
812 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
813 if (ret)
814 return ret;
815
816 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
817 sfmt.pad = me->num_pads - 1;
818 mf->code = tfmt->code;
819 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
820 &sfmt);
821 if (ret)
822 return ret;
823 }
824
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300825 pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300826 if (!pad)
827 return -EINVAL;
828 me = pad->entity;
829 }
830
831 if (mf->code != tfmt->code)
832 continue;
833
834 fcc = ffmt->fourcc;
835 tfmt->width = mf->width;
836 tfmt->height = mf->height;
837 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300838 NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300839 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
840 NULL, &fcc, FIMC_SD_PAD_SOURCE);
841 if (ffmt && ffmt->mbus_code)
842 mf->code = ffmt->mbus_code;
843 if (mf->width != tfmt->width || mf->height != tfmt->height)
844 continue;
845 tfmt->code = mf->code;
846 break;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300847 }
848
849 if (fmt_id && ffmt)
850 *fmt_id = ffmt;
851 *tfmt = *mf;
852
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300853 return 0;
854}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300855
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300856/**
857 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
858 * @sensor: pointer to the sensor subdev
859 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
Mauro Carvalho Chehab3b060ba02017-11-29 04:32:51 -0500860 * @num_planes: number of planes
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300861 * @try: true to set the frame parameters, false to query only
862 *
863 * This function is used by this driver only for compressed/blob data formats.
864 */
865static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
866 struct v4l2_plane_pix_format *plane_fmt,
867 unsigned int num_planes, bool try)
868{
869 struct v4l2_mbus_frame_desc fd;
870 int i, ret;
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300871 int pad;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300872
873 for (i = 0; i < num_planes; i++)
874 fd.entry[i].length = plane_fmt[i].sizeimage;
875
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300876 pad = sensor->entity.num_pads - 1;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300877 if (try)
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300878 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300879 else
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300880 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300881
882 if (ret < 0)
883 return ret;
884
885 if (num_planes != fd.num_entries)
886 return -EINVAL;
887
888 for (i = 0; i < num_planes; i++)
889 plane_fmt[i].sizeimage = fd.entry[i].length;
890
891 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
892 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
893 fd.entry[0].length);
894
895 return -EINVAL;
896 }
897
898 return 0;
899}
900
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300901static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
902 struct v4l2_format *f)
903{
904 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300905
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -0300906 __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
907 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300908}
909
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300910/*
911 * Try or set format on the fimc.X.capture video node and additionally
912 * on the whole pipeline if @try is false.
913 * Locking: the caller must _not_ hold the graph mutex.
914 */
915static int __video_try_or_set_format(struct fimc_dev *fimc,
916 struct v4l2_format *f, bool try,
917 struct fimc_fmt **inp_fmt,
918 struct fimc_fmt **out_fmt)
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300919{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300920 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300921 struct fimc_vid_cap *vc = &fimc->vid_cap;
922 struct exynos_video_entity *ve = &vc->ve;
923 struct fimc_ctx *ctx = vc->ctx;
924 unsigned int width = 0, height = 0;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300925 int ret = 0;
926
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300927 /* Pre-configure format at the camera input interface, for JPEG only */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300928 if (fimc_jpeg_fourcc(pix->pixelformat)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300929 fimc_capture_try_format(ctx, &pix->width, &pix->height,
930 NULL, &pix->pixelformat,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300931 FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300932 if (try) {
933 width = pix->width;
934 height = pix->height;
935 } else {
936 ctx->s_frame.f_width = pix->width;
937 ctx->s_frame.f_height = pix->height;
938 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300939 }
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300940
941 /* Try the format at the scaler and the DMA output */
942 *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
943 NULL, &pix->pixelformat,
944 FIMC_SD_PAD_SOURCE);
945 if (*out_fmt == NULL)
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300946 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300947
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300948 /* Restore image width/height for JPEG (no resizing supported). */
949 if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
950 pix->width = width;
951 pix->height = height;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300952 }
953
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300954 /* Try to match format at the host and the sensor */
955 if (!vc->user_subdev_api) {
956 struct v4l2_mbus_framefmt mbus_fmt;
957 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300958
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300959 mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
960
961 mf->code = (*out_fmt)->mbus_code;
962 mf->width = pix->width;
963 mf->height = pix->height;
964
965 fimc_md_graph_lock(ve);
966 ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
967 fimc_md_graph_unlock(ve);
968
969 if (ret < 0)
970 return ret;
971
972 pix->width = mf->width;
973 pix->height = mf->height;
974 }
975
976 fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
977
978 if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
979 struct v4l2_subdev *sensor;
980
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300981 fimc_md_graph_lock(ve);
982
983 sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
984 if (sensor)
985 fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300986 (*out_fmt)->memplanes, try);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300987 else
988 ret = -EPIPE;
989
990 fimc_md_graph_unlock(ve);
991 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300992
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300993 return ret;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300994}
995
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300996static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
997 struct v4l2_format *f)
998{
999 struct fimc_dev *fimc = video_drvdata(file);
1000 struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
1001
1002 return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
1003}
1004
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001005static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
1006 enum fimc_color_fmt color)
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001007{
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001008 bool jpeg = fimc_fmt_is_user_defined(color);
1009
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001010 ctx->scaler.enabled = !jpeg;
1011 fimc_ctrls_activate(ctx, !jpeg);
1012
1013 if (jpeg)
1014 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1015 else
1016 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1017}
1018
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001019static int __fimc_capture_set_format(struct fimc_dev *fimc,
1020 struct v4l2_format *f)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001021{
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001022 struct fimc_vid_cap *vc = &fimc->vid_cap;
1023 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001024 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001025 struct fimc_frame *ff = &ctx->d_frame;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001026 struct fimc_fmt *inp_fmt = NULL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001027 int ret, i;
1028
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001029 if (vb2_is_busy(&fimc->vid_cap.vbq))
1030 return -EBUSY;
1031
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001032 ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1033 if (ret < 0)
1034 return ret;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001035
1036 /* Update RGB Alpha control state and value range */
1037 fimc_alpha_ctrl_update(ctx);
1038
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001039 for (i = 0; i < ff->fmt->memplanes; i++) {
1040 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
Sylwester Nawrockid547ab62012-05-16 15:00:26 -03001041 ff->payload[i] = pix->plane_fmt[i].sizeimage;
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001042 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001043
1044 set_frame_bounds(ff, pix->width, pix->height);
1045 /* Reset the composition rectangle if not yet configured */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001046 if (!(ctx->state & FIMC_COMPOSE))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001047 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1048
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001049 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001050
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001051 /* Reset cropping and set format at the camera interface input */
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001052 if (!vc->user_subdev_api) {
1053 ctx->s_frame.fmt = inp_fmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001054 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1055 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1056 }
1057
1058 return ret;
1059}
1060
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001061static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1062 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001063{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001064 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001065
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001066 return __fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001067}
1068
1069static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001070 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001071{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001072 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001073 struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1074 struct v4l2_subdev *sd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001075
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001076 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001077 return -EINVAL;
1078
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001079 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001080 fimc_md_graph_lock(ve);
1081 sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1082 fimc_md_graph_unlock(ve);
1083
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001084 if (sd)
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001085 strscpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001086
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001087 return 0;
1088}
1089
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001090static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001091{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001092 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001093}
1094
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001095static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001096{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001097 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001098 return 0;
1099}
1100
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001101/**
1102 * fimc_pipeline_validate - check for formats inconsistencies
1103 * between source and sink pad of each link
Mauro Carvalho Chehab3b060ba02017-11-29 04:32:51 -05001104 * @fimc: the FIMC device this context applies to
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001105 *
1106 * Return 0 if all formats match or -EPIPE otherwise.
1107 */
1108static int fimc_pipeline_validate(struct fimc_dev *fimc)
1109{
1110 struct v4l2_subdev_format sink_fmt, src_fmt;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001111 struct fimc_vid_cap *vc = &fimc->vid_cap;
1112 struct v4l2_subdev *sd = &vc->subdev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001113 struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001114 struct media_pad *sink_pad, *src_pad;
1115 int i, ret;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001116
1117 while (1) {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001118 /*
1119 * Find current entity sink pad and any remote sink pad linked
1120 * to it. We stop if there is no sink pad in current entity or
1121 * it is not linked to any other remote entity.
1122 */
1123 src_pad = NULL;
1124
1125 for (i = 0; i < sd->entity.num_pads; i++) {
1126 struct media_pad *p = &sd->entity.pads[i];
1127
1128 if (p->flags & MEDIA_PAD_FL_SINK) {
1129 sink_pad = p;
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -03001130 src_pad = media_entity_remote_pad(sink_pad);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001131 if (src_pad)
1132 break;
1133 }
1134 }
1135
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001136 if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001137 break;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001138
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001139 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001140 if (sd == &vc->subdev) {
1141 struct fimc_frame *ff = &vc->ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001142 sink_fmt.format.width = ff->f_width;
1143 sink_fmt.format.height = ff->f_height;
1144 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1145 } else {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001146 sink_fmt.pad = sink_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001147 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1148 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1149 if (ret < 0 && ret != -ENOIOCTLCMD)
1150 return -EPIPE;
1151 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001152
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001153 /* Retrieve format at the source pad */
1154 sd = media_entity_to_v4l2_subdev(src_pad->entity);
1155 src_fmt.pad = src_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001156 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1157 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1158 if (ret < 0 && ret != -ENOIOCTLCMD)
1159 return -EPIPE;
1160
1161 if (src_fmt.format.width != sink_fmt.format.width ||
1162 src_fmt.format.height != sink_fmt.format.height ||
1163 src_fmt.format.code != sink_fmt.format.code)
1164 return -EPIPE;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001165
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001166 if (sd == p->subdevs[IDX_SENSOR] &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001167 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1168 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001169 struct fimc_frame *frame = &vc->ctx->d_frame;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001170 unsigned int i;
1171
1172 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1173 frame->fmt->memplanes,
1174 false);
1175 if (ret < 0)
1176 return -EPIPE;
1177
1178 for (i = 0; i < frame->fmt->memplanes; i++)
1179 if (frame->payload[i] < plane_fmt[i].sizeimage)
1180 return -EPIPE;
1181 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001182 }
1183 return 0;
1184}
1185
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001186static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001187 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001188{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001189 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001190 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001191 struct media_entity *entity = &vc->ve.vdev.entity;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001192 struct fimc_source_info *si = NULL;
1193 struct v4l2_subdev *sd;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001194 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001195
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001196 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001197 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001198
Sakari Ailus20b85222016-11-21 14:48:30 -02001199 ret = media_pipeline_start(entity, &vc->ve.pipe->mp);
Sakari Ailusa60a2952012-05-25 07:08:05 -03001200 if (ret < 0)
1201 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001202
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001203 sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001204 if (sd)
1205 si = v4l2_get_subdev_hostdata(sd);
1206
1207 if (si == NULL) {
1208 ret = -EPIPE;
1209 goto err_p_stop;
1210 }
1211 /*
1212 * Save configuration data related to currently attached image
1213 * sensor or other data source, e.g. FIMC-IS.
1214 */
1215 vc->source_config = *si;
1216
1217 if (vc->input == GRP_ID_FIMC_IS)
1218 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1219
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001220 if (vc->user_subdev_api) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001221 ret = fimc_pipeline_validate(fimc);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001222 if (ret < 0)
1223 goto err_p_stop;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001224 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001225
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001226 ret = vb2_ioctl_streamon(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001227 if (!ret) {
1228 vc->streaming = true;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001229 return ret;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001230 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001231
1232err_p_stop:
Sakari Ailus20b85222016-11-21 14:48:30 -02001233 media_pipeline_stop(entity);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001234 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001235}
1236
1237static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001238 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001239{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001240 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001241 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001242 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001243
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001244 ret = vb2_ioctl_streamoff(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001245 if (ret < 0)
1246 return ret;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001247
Sakari Ailus20b85222016-11-21 14:48:30 -02001248 media_pipeline_stop(&vc->ve.vdev.entity);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001249 vc->streaming = false;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001250 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001251}
1252
1253static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001254 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001255{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001256 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001257 int ret;
1258
1259 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001260
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001261 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001262 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001263
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001264 return ret;
1265}
1266
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001267static int fimc_cap_g_selection(struct file *file, void *fh,
1268 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001269{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001270 struct fimc_dev *fimc = video_drvdata(file);
1271 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001272 struct fimc_frame *f = &ctx->s_frame;
1273
Hans Verkuileaec4202017-05-08 11:35:05 -03001274 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001275 return -EINVAL;
1276
1277 switch (s->target) {
1278 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1279 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1280 f = &ctx->d_frame;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001281 /* fall through */
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001282 case V4L2_SEL_TGT_CROP_BOUNDS:
1283 case V4L2_SEL_TGT_CROP_DEFAULT:
1284 s->r.left = 0;
1285 s->r.top = 0;
1286 s->r.width = f->o_width;
1287 s->r.height = f->o_height;
1288 return 0;
1289
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001290 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001291 f = &ctx->d_frame;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001292 /* fall through */
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001293 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001294 s->r.left = f->offs_h;
1295 s->r.top = f->offs_v;
1296 s->r.width = f->width;
1297 s->r.height = f->height;
1298 return 0;
1299 }
1300
1301 return -EINVAL;
1302}
1303
1304/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
Sachin Kamat7e566be2012-05-26 11:11:54 -03001305static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001306{
1307 if (a->left < b->left || a->top < b->top)
1308 return 0;
1309 if (a->left + a->width > b->left + b->width)
1310 return 0;
1311 if (a->top + a->height > b->top + b->height)
1312 return 0;
1313
1314 return 1;
1315}
1316
1317static int fimc_cap_s_selection(struct file *file, void *fh,
1318 struct v4l2_selection *s)
1319{
1320 struct fimc_dev *fimc = video_drvdata(file);
1321 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1322 struct v4l2_rect rect = s->r;
1323 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001324 unsigned long flags;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001325
Hans Verkuileaec4202017-05-08 11:35:05 -03001326 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001327 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001328
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001329 if (s->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001330 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001331 else if (s->target == V4L2_SEL_TGT_CROP)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001332 f = &ctx->s_frame;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001333 else
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001334 return -EINVAL;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001335
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001336 fimc_capture_try_selection(ctx, &rect, s->target);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001337
1338 if (s->flags & V4L2_SEL_FLAG_LE &&
1339 !enclosed_rectangle(&rect, &s->r))
1340 return -ERANGE;
1341
1342 if (s->flags & V4L2_SEL_FLAG_GE &&
1343 !enclosed_rectangle(&s->r, &rect))
1344 return -ERANGE;
1345
1346 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001347 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001348 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1349 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001350 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001351
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001352 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001353 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001354}
1355
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001356static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -03001357 .vidioc_querycap = fimc_cap_querycap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001358
Boris Brezillon7e98b7b2019-06-04 03:06:25 -04001359 .vidioc_enum_fmt_vid_cap = fimc_cap_enum_fmt,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001360 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001361 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001362 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001363
1364 .vidioc_reqbufs = fimc_cap_reqbufs,
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001365 .vidioc_querybuf = vb2_ioctl_querybuf,
1366 .vidioc_qbuf = vb2_ioctl_qbuf,
1367 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1368 .vidioc_expbuf = vb2_ioctl_expbuf,
1369 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1370 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001371
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001372 .vidioc_streamon = fimc_cap_streamon,
1373 .vidioc_streamoff = fimc_cap_streamoff,
1374
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001375 .vidioc_g_selection = fimc_cap_g_selection,
1376 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001377
1378 .vidioc_enum_input = fimc_cap_enum_input,
1379 .vidioc_s_input = fimc_cap_s_input,
1380 .vidioc_g_input = fimc_cap_g_input,
1381};
1382
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001383/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001384static int fimc_link_setup(struct media_entity *entity,
1385 const struct media_pad *local,
1386 const struct media_pad *remote, u32 flags)
1387{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001388 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1389 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001390 struct fimc_vid_cap *vc = &fimc->vid_cap;
1391 struct v4l2_subdev *sensor;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001392
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001393 if (!is_media_entity_v4l2_subdev(remote->entity))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001394 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001395
1396 if (WARN_ON(fimc == NULL))
1397 return 0;
1398
1399 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1400 local->entity->name, remote->entity->name, flags,
1401 fimc->vid_cap.input);
1402
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001403 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1404 fimc->vid_cap.input = 0;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001405 return 0;
1406 }
1407
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001408 if (vc->input != 0)
1409 return -EBUSY;
1410
1411 vc->input = sd->grp_id;
1412
1413 if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1414 return 0;
1415
1416 /* Inherit V4L2 controls from the image sensor subdev. */
1417 sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1418 if (sensor == NULL)
1419 return 0;
1420
1421 return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
Hans Verkuilda1b1ae2018-05-21 04:54:36 -04001422 sensor->ctrl_handler, NULL, true);
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001423}
1424
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001425static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001426 .link_setup = fimc_link_setup,
1427};
1428
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001429/**
1430 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1431 * @sd: pointer to a subdev generating the notification
1432 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1433 * @arg: pointer to an u32 type integer that stores the frame payload value
1434 *
1435 * The End Of Frame notification sent by sensor subdev in its still capture
1436 * mode. If there is only a single VSYNC generated by the sensor at the
1437 * beginning of a frame transmission, FIMC does not issue the LastIrq
1438 * (end of frame) interrupt. And this notification is used to complete the
1439 * frame capture and returning a buffer to user-space. Subdev drivers should
1440 * call this notification from their last 'End of frame capture' interrupt.
1441 */
1442void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1443 void *arg)
1444{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001445 struct fimc_source_info *si;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001446 struct fimc_vid_buffer *buf;
1447 struct fimc_md *fmd;
1448 struct fimc_dev *fimc;
1449 unsigned long flags;
1450
1451 if (sd == NULL)
1452 return;
1453
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001454 si = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001455 fmd = entity_to_fimc_mdev(&sd->entity);
1456
1457 spin_lock_irqsave(&fmd->slock, flags);
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001458
1459 fimc = si ? source_to_sensor_info(si)->host : NULL;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001460
1461 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1462 test_bit(ST_CAPT_PEND, &fimc->state)) {
1463 unsigned long irq_flags;
1464 spin_lock_irqsave(&fimc->slock, irq_flags);
1465 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1466 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1467 struct fimc_vid_buffer, list);
Junghak Sung2d700712015-09-22 10:30:30 -03001468 vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1469 *((u32 *)arg));
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001470 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001471 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001472 fimc_deactivate_capture(fimc);
1473 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1474 }
1475 spin_unlock_irqrestore(&fmd->slock, flags);
1476}
1477
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001478static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001479 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001480 struct v4l2_subdev_mbus_code_enum *code)
1481{
1482 struct fimc_fmt *fmt;
1483
1484 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1485 if (!fmt)
1486 return -EINVAL;
1487 code->code = fmt->mbus_code;
1488 return 0;
1489}
1490
1491static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001492 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001493 struct v4l2_subdev_format *fmt)
1494{
1495 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1496 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001497 struct fimc_frame *ff = &ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001498 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001499
1500 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
Hans Verkuilf7234132015-03-04 01:47:54 -08001501 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001502 fmt->format = *mf;
1503 return 0;
1504 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001505
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001506 mf = &fmt->format;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001507 mutex_lock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001508
1509 switch (fmt->pad) {
1510 case FIMC_SD_PAD_SOURCE:
1511 if (!WARN_ON(ff->fmt == NULL))
1512 mf->code = ff->fmt->mbus_code;
1513 /* Sink pads crop rectangle size */
1514 mf->width = ff->width;
1515 mf->height = ff->height;
1516 break;
1517 case FIMC_SD_PAD_SINK_FIFO:
1518 *mf = fimc->vid_cap.wb_fmt;
1519 break;
1520 case FIMC_SD_PAD_SINK_CAM:
1521 default:
1522 *mf = fimc->vid_cap.ci_fmt;
1523 break;
1524 }
1525
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001526 mutex_unlock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001527 mf->colorspace = V4L2_COLORSPACE_JPEG;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001528
1529 return 0;
1530}
1531
1532static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001533 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001534 struct v4l2_subdev_format *fmt)
1535{
1536 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1537 struct v4l2_mbus_framefmt *mf = &fmt->format;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001538 struct fimc_vid_cap *vc = &fimc->vid_cap;
1539 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001540 struct fimc_frame *ff;
1541 struct fimc_fmt *ffmt;
1542
1543 dbg("pad%d: code: 0x%x, %dx%d",
1544 fmt->pad, mf->code, mf->width, mf->height);
1545
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001546 if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001547 return -EBUSY;
1548
1549 mutex_lock(&fimc->lock);
1550 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1551 &mf->code, NULL, fmt->pad);
1552 mutex_unlock(&fimc->lock);
1553 mf->colorspace = V4L2_COLORSPACE_JPEG;
1554
1555 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
Hans Verkuilf7234132015-03-04 01:47:54 -08001556 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001557 *mf = fmt->format;
1558 return 0;
1559 }
Sylwester Nawrocki66125452012-12-05 13:52:00 -03001560 /* There must be a bug in the driver if this happens */
1561 if (WARN_ON(ffmt == NULL))
1562 return -EINVAL;
1563
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001564 /* Update RGB Alpha control state and value range */
1565 fimc_alpha_ctrl_update(ctx);
1566
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001567 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001568 if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1569 ff = &ctx->d_frame;
1570 /* Sink pads crop rectangle size */
1571 mf->width = ctx->s_frame.width;
1572 mf->height = ctx->s_frame.height;
1573 } else {
1574 ff = &ctx->s_frame;
1575 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001576
1577 mutex_lock(&fimc->lock);
1578 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001579
1580 if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1581 vc->wb_fmt = *mf;
1582 else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1583 vc->ci_fmt = *mf;
1584
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001585 ff->fmt = ffmt;
1586
1587 /* Reset the crop rectangle if required. */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001588 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001589 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1590
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001591 if (fmt->pad != FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001592 ctx->state &= ~FIMC_COMPOSE;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001593
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001594 mutex_unlock(&fimc->lock);
1595 return 0;
1596}
1597
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001598static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001599 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001600 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001601{
1602 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1603 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001604 struct fimc_frame *f = &ctx->s_frame;
1605 struct v4l2_rect *r = &sel->r;
1606 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001607
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001608 if (sel->pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001609 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001610
1611 mutex_lock(&fimc->lock);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001612
1613 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001614 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001615 f = &ctx->d_frame;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03001616 /* fall through */
Sakari Ailus5689b282012-05-18 09:31:18 -03001617 case V4L2_SEL_TGT_CROP_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001618 r->width = f->o_width;
1619 r->height = f->o_height;
1620 r->left = 0;
1621 r->top = 0;
1622 mutex_unlock(&fimc->lock);
1623 return 0;
1624
Sakari Ailus5689b282012-05-18 09:31:18 -03001625 case V4L2_SEL_TGT_CROP:
Hans Verkuilf7234132015-03-04 01:47:54 -08001626 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001627 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001628 case V4L2_SEL_TGT_COMPOSE:
Hans Verkuilf7234132015-03-04 01:47:54 -08001629 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001630 f = &ctx->d_frame;
1631 break;
1632 default:
1633 mutex_unlock(&fimc->lock);
1634 return -EINVAL;
1635 }
1636
1637 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1638 sel->r = *try_sel;
1639 } else {
1640 r->left = f->offs_h;
1641 r->top = f->offs_v;
1642 r->width = f->width;
1643 r->height = f->height;
1644 }
1645
1646 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1647 sel->pad, r->left, r->top, r->width, r->height,
1648 f->f_width, f->f_height);
1649
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001650 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001651 return 0;
1652}
1653
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001654static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001655 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001656 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001657{
1658 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1659 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001660 struct fimc_frame *f = &ctx->s_frame;
1661 struct v4l2_rect *r = &sel->r;
1662 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001663 unsigned long flags;
1664
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001665 if (sel->pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001666 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001667
1668 mutex_lock(&fimc->lock);
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001669 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001670
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001671 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001672 case V4L2_SEL_TGT_CROP:
Hans Verkuilf7234132015-03-04 01:47:54 -08001673 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001674 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001675 case V4L2_SEL_TGT_COMPOSE:
Hans Verkuilf7234132015-03-04 01:47:54 -08001676 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001677 f = &ctx->d_frame;
1678 break;
1679 default:
1680 mutex_unlock(&fimc->lock);
1681 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001682 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001683
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001684 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1685 *try_sel = sel->r;
1686 } else {
1687 spin_lock_irqsave(&fimc->slock, flags);
1688 set_frame_crop(f, r->left, r->top, r->width, r->height);
1689 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sakari Ailus5689b282012-05-18 09:31:18 -03001690 if (sel->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001691 ctx->state |= FIMC_COMPOSE;
Sylwester Nawrocki8b164102013-01-30 09:55:46 -03001692 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001693 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001694
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001695 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001696 r->width, r->height);
1697
1698 mutex_unlock(&fimc->lock);
1699 return 0;
1700}
1701
Bhumika Goyala2fafda2016-12-12 16:03:57 -02001702static const struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001703 .enum_mbus_code = fimc_subdev_enum_mbus_code,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001704 .get_selection = fimc_subdev_get_selection,
1705 .set_selection = fimc_subdev_set_selection,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001706 .get_fmt = fimc_subdev_get_fmt,
1707 .set_fmt = fimc_subdev_set_fmt,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001708};
1709
Bhumika Goyala2fafda2016-12-12 16:03:57 -02001710static const struct v4l2_subdev_ops fimc_subdev_ops = {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001711 .pad = &fimc_subdev_pad_ops,
1712};
1713
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001714/* Set default format at the sensor and host interface */
1715static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1716{
1717 struct v4l2_format fmt = {
1718 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1719 .fmt.pix_mp = {
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001720 .width = FIMC_DEFAULT_WIDTH,
1721 .height = FIMC_DEFAULT_HEIGHT,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001722 .pixelformat = V4L2_PIX_FMT_YUYV,
1723 .field = V4L2_FIELD_NONE,
1724 .colorspace = V4L2_COLORSPACE_JPEG,
1725 },
1726 };
1727
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001728 return __fimc_capture_set_format(fimc, &fmt);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001729}
1730
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001731/* fimc->lock must be already initialized */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001732static int fimc_register_capture_device(struct fimc_dev *fimc,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001733 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001734{
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001735 struct video_device *vfd = &fimc->vid_cap.ve.vdev;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001736 struct vb2_queue *q = &fimc->vid_cap.vbq;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001737 struct fimc_ctx *ctx;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001738 struct fimc_vid_cap *vid_cap;
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001739 struct fimc_fmt *fmt;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001740 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001741
Sachin Kamat26ee7f42012-08-17 03:28:27 -03001742 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001743 if (!ctx)
1744 return -ENOMEM;
1745
1746 ctx->fimc_dev = fimc;
Sylwester Nawrocki3d112d92012-04-26 06:26:29 -03001747 ctx->in_path = FIMC_IO_CAMERA;
1748 ctx->out_path = FIMC_IO_DMA;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001749 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001750 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001751 ctx->d_frame.fmt = ctx->s_frame.fmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001752
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001753 memset(vfd, 0, sizeof(*vfd));
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001754 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001755
1756 vfd->fops = &fimc_capture_fops;
1757 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001758 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001759 vfd->minor = -1;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001760 vfd->release = video_device_release_empty;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001761 vfd->queue = q;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001762 vfd->lock = &fimc->lock;
Boris Brezillon1ddc8a92019-06-04 03:06:24 -04001763 vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -03001764
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001765 video_set_drvdata(vfd, fimc);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001766 vid_cap = &fimc->vid_cap;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001767 vid_cap->active_buf_cnt = 0;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001768 vid_cap->reqbufs_count = 0;
1769 vid_cap->ctx = ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001770
1771 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1772 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001773
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001774 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001775 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Tomasz Stanislawski9bd09fd2012-06-14 10:37:49 -03001776 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001777 q->drv_priv = ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001778 q->ops = &fimc_capture_qops;
1779 q->mem_ops = &vb2_dma_contig_memops;
1780 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -03001781 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001782 q->lock = &fimc->lock;
Hans Verkuil2548fee2016-02-16 07:30:19 -02001783 q->dev = &fimc->pdev->dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001784
Sylwester Nawrocki41fd0872012-10-19 08:24:18 -03001785 ret = vb2_queue_init(q);
1786 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001787 goto err_free_ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001788
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001789 /* Default format configuration */
1790 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1791 vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1792 vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1793 vid_cap->ci_fmt.code = fmt->mbus_code;
1794
1795 ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1796 ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1797 ctx->s_frame.fmt = fmt;
1798
1799 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1800 vid_cap->wb_fmt = vid_cap->ci_fmt;
1801 vid_cap->wb_fmt.code = fmt->mbus_code;
1802
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001803 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
Sylwester Nawrockibae45002016-09-01 08:47:05 -03001804 vfd->entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001805 ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001806 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001807 goto err_free_ctx;
1808
1809 ret = fimc_ctrls_create(ctx);
1810 if (ret)
1811 goto err_me_cleanup;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001812
Hans Verkuil70cad442020-02-03 12:41:18 +01001813 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001814 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001815 goto err_ctrl_free;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001816
1817 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1818 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001819
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -03001820 vfd->ctrl_handler = &ctx->ctrls.handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001821 return 0;
1822
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001823err_ctrl_free:
1824 fimc_ctrls_delete(ctx);
1825err_me_cleanup:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001826 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001827err_free_ctx:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001828 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001829 return ret;
1830}
1831
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001832static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001833{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001834 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1835 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001836
Sylwester Nawrockibbc52962012-07-26 07:59:11 -03001837 if (fimc == NULL)
1838 return -ENXIO;
1839
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001840 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1841 if (ret)
1842 return ret;
1843
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001844 fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001845
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001846 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001847 if (ret) {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001848 fimc_unregister_m2m_device(fimc);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001849 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001850 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001851
1852 return ret;
1853}
1854
1855static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1856{
1857 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001858 struct video_device *vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001859
1860 if (fimc == NULL)
1861 return;
1862
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001863 mutex_lock(&fimc->lock);
1864
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001865 fimc_unregister_m2m_device(fimc);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001866 vdev = &fimc->vid_cap.ve.vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001867
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001868 if (video_is_registered(vdev)) {
1869 video_unregister_device(vdev);
1870 media_entity_cleanup(&vdev->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001871 fimc_ctrls_delete(fimc->vid_cap.ctx);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001872 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001873 }
1874 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001875 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001876
1877 mutex_unlock(&fimc->lock);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001878}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001879
1880static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1881 .registered = fimc_capture_subdev_registered,
1882 .unregistered = fimc_capture_subdev_unregistered,
1883};
1884
1885int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1886{
1887 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1888 int ret;
1889
1890 v4l2_subdev_init(sd, &fimc_subdev_ops);
Sylwester Nawrocki5a665612013-04-18 08:49:54 -03001891 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -03001892 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001893
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001894 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1895 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001896 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001897 ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -03001898 fimc->vid_cap.sd_pads);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001899 if (ret)
1900 return ret;
1901
1902 sd->entity.ops = &fimc_sd_media_ops;
1903 sd->internal_ops = &fimc_capture_sd_internal_ops;
1904 v4l2_set_subdevdata(sd, fimc);
1905 return 0;
1906}
1907
1908void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1909{
1910 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1911
1912 v4l2_device_unregister_subdev(sd);
1913 media_entity_cleanup(&sd->entity);
1914 v4l2_set_subdevdata(sd, NULL);
1915}