blob: bf47d3b9cbe77e5dce9b1955c8280070afb5e3d0 [file] [log] [blame]
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001/*
Sylwester Nawrocki3a3f9442011-04-28 09:06:19 -03002 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03003 *
Sylwester Nawrocki0c9204d2012-04-25 06:55:42 -03004 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030014#include <linux/types.h>
15#include <linux/errno.h>
16#include <linux/bug.h>
17#include <linux/interrupt.h>
18#include <linux/device.h>
Sylwester Nawrockie9e21082011-09-02 06:25:32 -030019#include <linux/pm_runtime.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030020#include <linux/list.h>
21#include <linux/slab.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030022
23#include <linux/videodev2.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
26#include <media/v4l2-mem2mem.h>
Junghak Sungc1399902015-09-22 10:30:29 -030027#include <media/videobuf2-v4l2.h>
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030028#include <media/videobuf2-dma-contig.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030029
Sylwester Nawrocki44031062013-05-31 11:37:19 -030030#include "common.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030031#include "fimc-core.h"
Sylwester Nawrockic83a1ff2012-05-02 06:14:49 -030032#include "fimc-reg.h"
Sylwester Nawrocki44031062013-05-31 11:37:19 -030033#include "media-dev.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030034
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030035static int fimc_capture_hw_init(struct fimc_dev *fimc)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030036{
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030037 struct fimc_source_info *si = &fimc->vid_cap.source_config;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030038 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030039 int ret;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030040 unsigned long flags;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030041
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030042 if (ctx == NULL || ctx->s_frame.fmt == NULL)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030043 return -EINVAL;
44
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030045 if (si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK) {
46 ret = fimc_hw_camblk_cfg_writeback(fimc);
47 if (ret < 0)
48 return ret;
49 }
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030050
51 spin_lock_irqsave(&fimc->slock, flags);
52 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
53 fimc_set_yuv_order(ctx);
54
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -030055 fimc_hw_set_camera_polarity(fimc, si);
56 fimc_hw_set_camera_type(fimc, si);
57 fimc_hw_set_camera_source(fimc, si);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030058 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
59
60 ret = fimc_set_scaler_info(ctx);
61 if (!ret) {
62 fimc_hw_set_input_path(ctx);
63 fimc_hw_set_prescaler(ctx);
64 fimc_hw_set_mainscaler(ctx);
65 fimc_hw_set_target_format(ctx);
66 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -030067 fimc_hw_set_effect(ctx);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030068 fimc_hw_set_output_path(ctx);
69 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -030070 if (fimc->drv_data->alpha_color)
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -030071 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -030072 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030073 }
74 spin_unlock_irqrestore(&fimc->slock, flags);
75 return ret;
76}
77
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030078/*
79 * Reinitialize the driver so it is ready to start the streaming again.
80 * Set fimc->state to indicate stream off and the hardware shut down state.
81 * If not suspending (@suspend is false), return any buffers to videobuf2.
82 * Otherwise put any owned buffers onto the pending buffers queue, so they
83 * can be re-spun when the device is being resumed. Also perform FIMC
84 * software reset and disable streaming on the whole pipeline if required.
85 */
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030086static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030087{
Marek Szyprowskibd323e22011-08-29 08:51:49 -030088 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030089 struct fimc_vid_buffer *buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -030090 unsigned long flags;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030091 bool streaming;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030092
93 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030094 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030095
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030096 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
97 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrockiaa3331222012-03-16 11:47:51 -030098 if (suspend)
99 fimc->state |= (1 << ST_CAPT_SUSPENDED);
100 else
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300101 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300102
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300103 /* Release unused buffers */
104 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300105 buf = fimc_pending_queue_pop(cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300106 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300107 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300108 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300109 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300110 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300111 if (suspend)
112 fimc_pending_queue_add(cap, buf);
113 else
Junghak Sung2d700712015-09-22 10:30:30 -0300114 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300115 }
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300116
117 fimc_hw_reset(fimc);
118 cap->buf_index = 0;
119
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300120 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300121
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300122 if (streaming)
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300123 return fimc_pipeline_call(&cap->ve, set_stream, 0);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300124 else
125 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300126}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300127
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300128static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300129{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300130 unsigned long flags;
131
132 if (!fimc_capture_active(fimc))
133 return 0;
134
135 spin_lock_irqsave(&fimc->slock, flags);
136 set_bit(ST_CAPT_SHUT, &fimc->state);
137 fimc_deactivate_capture(fimc);
138 spin_unlock_irqrestore(&fimc->slock, flags);
139
140 wait_event_timeout(fimc->irq_queue,
141 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300142 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300143
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300144 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300145}
146
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300147/**
148 * fimc_capture_config_update - apply the camera interface configuration
149 *
150 * To be called from within the interrupt handler with fimc.slock
151 * spinlock held. It updates the camera pixel crop, rotation and
152 * image flip in H/W.
153 */
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300154static int fimc_capture_config_update(struct fimc_ctx *ctx)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300155{
156 struct fimc_dev *fimc = ctx->fimc_dev;
157 int ret;
158
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300159 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c32012-03-19 13:11:40 -0300160
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300161 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c32012-03-19 13:11:40 -0300162 if (ret)
163 return ret;
164
165 fimc_hw_set_prescaler(ctx);
166 fimc_hw_set_mainscaler(ctx);
167 fimc_hw_set_target_format(ctx);
168 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300169 fimc_hw_set_effect(ctx);
Sylwester Nawrockiefb13c32012-03-19 13:11:40 -0300170 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
171 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -0300172 if (fimc->drv_data->alpha_color)
Sylwester Nawrockiefb13c32012-03-19 13:11:40 -0300173 fimc_hw_set_rgb_alpha(ctx);
174
175 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300176 return ret;
177}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300178
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300179void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
180{
181 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300182 struct fimc_pipeline *p = to_fimc_pipeline(cap->ve.pipe);
183 struct v4l2_subdev *csis = p->subdevs[IDX_CSIS];
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300184 struct fimc_frame *f = &cap->ctx->d_frame;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300185 struct fimc_vid_buffer *v_buf;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300186
187 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
188 wake_up(&fimc->irq_queue);
189 goto done;
190 }
191
192 if (!list_empty(&cap->active_buf_q) &&
193 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300194 v_buf = fimc_active_queue_pop(cap);
195
Junghak Sungd6dd6452015-11-03 08:16:37 -0200196 v_buf->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300197 v_buf->vb.sequence = cap->frame_count++;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300198
Junghak Sung2d700712015-09-22 10:30:30 -0300199 vb2_buffer_done(&v_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300200 }
201
202 if (!list_empty(&cap->pending_buf_q)) {
203
204 v_buf = fimc_pending_queue_pop(cap);
205 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
206 v_buf->index = cap->buf_index;
207
208 /* Move the buffer to the capture active queue */
209 fimc_active_queue_add(cap, v_buf);
210
211 dbg("next frame: %d, done frame: %d",
212 fimc_hw_get_frame_index(fimc), v_buf->index);
213
214 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
215 cap->buf_index = 0;
216 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300217 /*
218 * Set up a buffer at MIPI-CSIS if current image format
219 * requires the frame embedded data capture.
220 */
221 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
222 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
223 unsigned int size = f->payload[plane];
224 s32 index = fimc_hw_get_frame_index(fimc);
225 void *vaddr;
226
227 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
228 if (v_buf->index != index)
229 continue;
Junghak Sung2d700712015-09-22 10:30:30 -0300230 vaddr = vb2_plane_vaddr(&v_buf->vb.vb2_buf, plane);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300231 v4l2_subdev_call(csis, video, s_rx_buffer,
232 vaddr, &size);
233 break;
234 }
235 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300236
237 if (cap->active_buf_cnt == 0) {
238 if (deq_buf)
239 clear_bit(ST_CAPT_RUN, &fimc->state);
240
241 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
242 cap->buf_index = 0;
243 } else {
244 set_bit(ST_CAPT_RUN, &fimc->state);
245 }
246
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300247 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
248 fimc_capture_config_update(cap->ctx);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300249done:
250 if (cap->active_buf_cnt == 1) {
251 fimc_deactivate_capture(fimc);
252 clear_bit(ST_CAPT_STREAM, &fimc->state);
253 }
254
255 dbg("frame: %d, active_buf_cnt: %d",
256 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
257}
258
259
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300260static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300261{
262 struct fimc_ctx *ctx = q->drv_priv;
263 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300264 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300265 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300266 int ret;
267
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300268 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300269
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300270 ret = fimc_capture_hw_init(fimc);
271 if (ret) {
272 fimc_capture_state_cleanup(fimc, false);
273 return ret;
274 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300275
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300276 set_bit(ST_CAPT_PEND, &fimc->state);
277
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300278 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
279
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300280 if (vid_cap->active_buf_cnt >= min_bufs &&
281 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300282 fimc_activate_capture(ctx);
283
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300284 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300285 return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300286 }
287
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300288 return 0;
289}
290
Hans Verkuile37559b2014-04-17 02:47:21 -0300291static void stop_streaming(struct vb2_queue *q)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300292{
293 struct fimc_ctx *ctx = q->drv_priv;
294 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300295
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300296 if (!fimc_capture_active(fimc))
Hans Verkuile37559b2014-04-17 02:47:21 -0300297 return;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300298
Hans Verkuile37559b2014-04-17 02:47:21 -0300299 fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300300}
301
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300302int fimc_capture_suspend(struct fimc_dev *fimc)
303{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300304 bool suspend = fimc_capture_busy(fimc);
305
306 int ret = fimc_stop_capture(fimc, suspend);
307 if (ret)
308 return ret;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300309 return fimc_pipeline_call(&fimc->vid_cap.ve, close);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300310}
311
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300312static void buffer_queue(struct vb2_buffer *vb);
313
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300314int fimc_capture_resume(struct fimc_dev *fimc)
315{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300316 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300317 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300318 struct fimc_vid_buffer *buf;
319 int i;
320
321 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
322 return 0;
323
324 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
325 vid_cap->buf_index = 0;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300326 fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300327 fimc_capture_hw_init(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300328
329 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
330
331 for (i = 0; i < vid_cap->reqbufs_count; i++) {
332 if (list_empty(&vid_cap->pending_buf_q))
333 break;
334 buf = fimc_pending_queue_pop(vid_cap);
Junghak Sung2d700712015-09-22 10:30:30 -0300335 buffer_queue(&buf->vb.vb2_buf);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300336 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300337 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300338
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300339}
340
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200341static int queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300342 unsigned int *num_buffers, unsigned int *num_planes,
343 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300344{
345 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300346 struct fimc_frame *frame = &ctx->d_frame;
347 struct fimc_fmt *fmt = frame->fmt;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200348 unsigned long wh = frame->f_width * frame->f_height;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300349 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300350
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300351 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300352 return -EINVAL;
353
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200354 if (*num_planes) {
355 if (*num_planes != fmt->memplanes)
356 return -EINVAL;
357 for (i = 0; i < *num_planes; i++) {
358 if (sizes[i] < (wh * fmt->depth[i]) / 8)
359 return -EINVAL;
360 allocators[i] = ctx->fimc_dev->alloc_ctx;
361 }
362 return 0;
363 }
364
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300365 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300366
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300367 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300368 unsigned int size = (wh * fmt->depth[i]) / 8;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200369
370 if (fimc_fmt_is_user_defined(fmt->color))
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300371 sizes[i] = frame->payload[i];
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300372 else
Sylwester Nawrockid547ab62012-05-16 15:00:26 -0300373 sizes[i] = max_t(u32, size, frame->payload[i]);
374
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300375 allocators[i] = ctx->fimc_dev->alloc_ctx;
376 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300377
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300378 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300379}
380
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300381static int buffer_prepare(struct vb2_buffer *vb)
382{
383 struct vb2_queue *vq = vb->vb2_queue;
384 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300385 int i;
386
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300387 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300388 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300389
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300390 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300391 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300392
393 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300394 v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300395 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300396 vb2_plane_size(vb, i), size);
397 return -EINVAL;
398 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300399 vb2_set_plane_payload(vb, i, size);
400 }
401
402 return 0;
403}
404
405static void buffer_queue(struct vb2_buffer *vb)
406{
Junghak Sung2d700712015-09-22 10:30:30 -0300407 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300408 struct fimc_vid_buffer *buf
Junghak Sung2d700712015-09-22 10:30:30 -0300409 = container_of(vbuf, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300410 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
411 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300412 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300413 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300414 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300415 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300416
417 spin_lock_irqsave(&fimc->slock, flags);
Junghak Sung2d700712015-09-22 10:30:30 -0300418 fimc_prepare_addr(ctx, &buf->vb.vb2_buf, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300419
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300420 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
421 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
422 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300423 /* Setup the buffer directly for processing. */
424 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
425 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300426
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300427 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
428 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300429 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300430
431 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
432 vid_cap->buf_index = 0;
433 } else {
434 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300435 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300436
437 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
438
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300439
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300440 if (vb2_is_streaming(&vid_cap->vbq) &&
441 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300442 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Andrzej Hajda76323e52013-02-27 08:13:59 -0300443 int ret;
444
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300445 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300446 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300447
Andrzej Hajda76323e52013-02-27 08:13:59 -0300448 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
449 return;
450
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300451 ret = fimc_pipeline_call(ve, set_stream, 1);
Andrzej Hajda76323e52013-02-27 08:13:59 -0300452 if (ret < 0)
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300453 v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300454 return;
455 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300456 spin_unlock_irqrestore(&fimc->slock, flags);
457}
458
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300459static struct vb2_ops fimc_capture_qops = {
460 .queue_setup = queue_setup,
461 .buf_prepare = buffer_prepare,
462 .buf_queue = buffer_queue,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300463 .wait_prepare = vb2_ops_wait_prepare,
464 .wait_finish = vb2_ops_wait_finish,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300465 .start_streaming = start_streaming,
466 .stop_streaming = stop_streaming,
467};
468
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300469static int fimc_capture_set_default_format(struct fimc_dev *fimc);
470
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300471static int fimc_capture_open(struct file *file)
472{
473 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300474 struct fimc_vid_cap *vc = &fimc->vid_cap;
475 struct exynos_video_entity *ve = &vc->ve;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300476 int ret = -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300477
478 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
479
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300480 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300481
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300482 if (fimc_m2m_active(fimc))
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300483 goto unlock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300484
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300485 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300486 ret = pm_runtime_get_sync(&fimc->pdev->dev);
487 if (ret < 0)
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300488 goto unlock;
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300489
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300490 ret = v4l2_fh_open(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300491 if (ret) {
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300492 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300493 goto unlock;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300494 }
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300495
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300496 if (v4l2_fh_is_singular_file(file)) {
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300497 fimc_md_graph_lock(ve);
498
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300499 ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
500
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300501 if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
502 /*
503 * Recreate controls of the the video node to drop
504 * any controls inherited from the sensor subdev.
505 */
506 fimc_ctrls_delete(vc->ctx);
507
508 ret = fimc_ctrls_create(vc->ctx);
509 if (ret == 0)
510 vc->inh_sensor_ctrls = false;
511 }
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300512 if (ret == 0)
513 ve->vdev.entity.use_count++;
514
515 fimc_md_graph_unlock(ve);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300516
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300517 if (ret == 0)
518 ret = fimc_capture_set_default_format(fimc);
519
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300520 if (ret < 0) {
521 clear_bit(ST_CAPT_BUSY, &fimc->state);
522 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300523 v4l2_fh_release(file);
524 }
525 }
526unlock:
527 mutex_unlock(&fimc->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300528 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300529}
530
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300531static int fimc_capture_release(struct file *file)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300532{
533 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -0300534 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300535 bool close = v4l2_fh_is_singular_file(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300536 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300537
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300538 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
539
Sylwester Nawrockiba6b3722012-11-22 11:12:16 -0300540 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300541
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300542 if (close && vc->streaming) {
543 media_entity_pipeline_stop(&vc->ve.vdev.entity);
544 vc->streaming = false;
545 }
546
Ricardo Ribalda1380f572013-11-25 05:49:02 -0300547 ret = _vb2_fop_release(file, NULL);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300548
549 if (close) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300550 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300551 fimc_pipeline_call(&vc->ve, close);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300552 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300553
554 fimc_md_graph_lock(&vc->ve);
555 vc->ve.vdev.entity.use_count--;
556 fimc_md_graph_unlock(&vc->ve);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300557 }
558
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300559 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300560 mutex_unlock(&fimc->lock);
561
562 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300563}
564
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300565static const struct v4l2_file_operations fimc_capture_fops = {
566 .owner = THIS_MODULE,
567 .open = fimc_capture_open,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300568 .release = fimc_capture_release,
569 .poll = vb2_fop_poll,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300570 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300571 .mmap = vb2_fop_mmap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300572};
573
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300574/*
575 * Format and crop negotiation helpers
576 */
577
578static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
579 u32 *width, u32 *height,
580 u32 *code, u32 *fourcc, int pad)
581{
582 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
583 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300584 const struct fimc_variant *var = fimc->variant;
585 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300586 struct fimc_frame *dst = &ctx->d_frame;
587 u32 depth, min_w, max_w, min_h, align_h = 3;
588 u32 mask = FMT_FLAGS_CAM;
589 struct fimc_fmt *ffmt;
590
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300591 /* Conversion from/to JPEG or User Defined format is not supported */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300592 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300593 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
594 *code = ctx->s_frame.fmt->mbus_code;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300595
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300596 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300597 mask |= FMT_FLAGS_M2M;
598
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300599 if (pad == FIMC_SD_PAD_SINK_FIFO)
600 mask = FMT_FLAGS_WRITEBACK;
601
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300602 ffmt = fimc_find_format(fourcc, code, mask, 0);
603 if (WARN_ON(!ffmt))
604 return NULL;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300605
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300606 if (code)
607 *code = ffmt->mbus_code;
608 if (fourcc)
609 *fourcc = ffmt->fourcc;
610
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300611 if (pad != FIMC_SD_PAD_SOURCE) {
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300612 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300613 pl->scaler_dis_w : pl->scaler_en_w;
614 /* Apply the camera input interface pixel constraints */
615 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
616 height, max_t(u32, *height, 32),
617 FIMC_CAMIF_MAX_HEIGHT,
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300618 fimc_fmt_is_user_defined(ffmt->color) ?
619 3 : 1,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300620 0);
621 return ffmt;
622 }
623 /* Can't scale or crop in transparent (JPEG) transfer mode */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300624 if (fimc_fmt_is_user_defined(ffmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300625 *width = ctx->s_frame.f_width;
626 *height = ctx->s_frame.f_height;
627 return ffmt;
628 }
629 /* Apply the scaler and the output DMA constraints */
630 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300631 if (ctx->state & FIMC_COMPOSE) {
632 min_w = dst->offs_h + dst->width;
633 min_h = dst->offs_v + dst->height;
634 } else {
635 min_w = var->min_out_pixsize;
636 min_h = var->min_out_pixsize;
637 }
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300638 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300639 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
640
641 depth = fimc_get_format_depth(ffmt);
642 v4l_bound_align_image(width, min_w, max_w,
643 ffs(var->min_out_pixsize) - 1,
644 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
645 align_h,
646 64/(ALIGN(depth, 8)));
647
648 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
649 pad, code ? *code : 0, *width, *height,
650 dst->f_width, dst->f_height);
651
652 return ffmt;
653}
654
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300655static void fimc_capture_try_selection(struct fimc_ctx *ctx,
656 struct v4l2_rect *r,
657 int target)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300658{
659 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
660 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300661 const struct fimc_variant *var = fimc->variant;
662 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300663 struct fimc_frame *sink = &ctx->s_frame;
664 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
665 u32 align_sz = 0, align_h = 4;
666 u32 max_sc_h, max_sc_v;
667
668 /* In JPEG transparent transfer mode cropping is not supported */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300669 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300670 r->width = sink->f_width;
671 r->height = sink->f_height;
672 r->left = r->top = 0;
673 return;
674 }
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300675 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300676 if (ctx->rotation != 90 && ctx->rotation != 270)
677 align_h = 1;
678 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
679 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
680 min_sz = var->min_out_pixsize;
681 } else {
682 u32 depth = fimc_get_format_depth(sink->fmt);
683 align_sz = 64/ALIGN(depth, 8);
684 min_sz = var->min_inp_pixsize;
685 min_w = min_h = min_sz;
686 max_sc_h = max_sc_v = 1;
687 }
688 /*
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300689 * For the compose rectangle the following constraints must be met:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300690 * - it must fit in the sink pad format rectangle (f_width/f_height);
691 * - maximum downscaling ratio is 64;
692 * - maximum crop size depends if the rotator is used or not;
693 * - the sink pad format width/height must be 4 multiple of the
694 * prescaler ratios determined by sink pad size and source pad crop,
695 * the prescaler ratio is returned by fimc_get_scaler_factor().
696 */
697 max_w = min_t(u32,
698 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
699 rotate ? sink->f_height : sink->f_width);
700 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300701
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300702 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300703 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
704 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
705 if (rotate) {
706 swap(max_sc_h, max_sc_v);
707 swap(min_w, min_h);
708 }
709 }
710 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
711 &r->height, min_h, max_h, align_h,
712 align_sz);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300713 /* Adjust left/top if crop/compose rectangle is out of bounds */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300714 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
715 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
716 r->left = round_down(r->left, var->hor_offs_align);
717
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300718 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
719 target, r->left, r->top, r->width, r->height,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300720 sink->f_width, sink->f_height);
721}
722
723/*
724 * The video node ioctl operations
725 */
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -0300726static int fimc_cap_querycap(struct file *file, void *priv,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300727 struct v4l2_capability *cap)
728{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300729 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300730
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -0300731 __fimc_vidioc_querycap(&fimc->pdev->dev, cap, V4L2_CAP_STREAMING |
732 V4L2_CAP_VIDEO_CAPTURE_MPLANE);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300733 return 0;
734}
735
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300736static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
737 struct v4l2_fmtdesc *f)
738{
739 struct fimc_fmt *fmt;
740
741 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
742 f->index);
743 if (!fmt)
744 return -EINVAL;
745 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
746 f->pixelformat = fmt->fourcc;
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300747 if (fmt->fourcc == MEDIA_BUS_FMT_JPEG_1X8)
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300748 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
749 return 0;
750}
751
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300752static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
753{
754 struct media_pad *pad = &me->pads[0];
755
756 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300757 pad = media_entity_remote_pad(pad);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300758 if (!pad)
759 break;
760 me = pad->entity;
761 pad = &me->pads[0];
762 }
763
764 return me;
765}
766
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300767/**
768 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
769 * elements
770 * @ctx: FIMC capture context
771 * @tfmt: media bus format to try/set on subdevs
772 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
773 * @set: true to set format on subdevs, false to try only
774 */
775static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
776 struct v4l2_mbus_framefmt *tfmt,
777 struct fimc_fmt **fmt_id,
778 bool set)
779{
780 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300781 struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
782 struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300783 struct v4l2_subdev_format sfmt;
784 struct v4l2_mbus_framefmt *mf = &sfmt.format;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300785 struct media_entity *me;
786 struct fimc_fmt *ffmt;
787 struct media_pad *pad;
788 int ret, i = 1;
789 u32 fcc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300790
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300791 if (WARN_ON(!sd || !tfmt))
792 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300793
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300794 memset(&sfmt, 0, sizeof(sfmt));
795 sfmt.format = *tfmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300796 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300797
798 me = fimc_pipeline_get_head(&sd->entity);
799
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300800 while (1) {
801 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
802 FMT_FLAGS_CAM, i++);
803 if (ffmt == NULL) {
804 /*
805 * Notify user-space if common pixel code for
806 * host and sensor does not exist.
807 */
808 return -EINVAL;
809 }
810 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300811
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300812 /* set format on all pipeline subdevs */
813 while (me != &fimc->vid_cap.subdev.entity) {
814 sd = media_entity_to_v4l2_subdev(me);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300815
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300816 sfmt.pad = 0;
817 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
818 if (ret)
819 return ret;
820
821 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
822 sfmt.pad = me->num_pads - 1;
823 mf->code = tfmt->code;
824 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
825 &sfmt);
826 if (ret)
827 return ret;
828 }
829
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300830 pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300831 if (!pad)
832 return -EINVAL;
833 me = pad->entity;
834 }
835
836 if (mf->code != tfmt->code)
837 continue;
838
839 fcc = ffmt->fourcc;
840 tfmt->width = mf->width;
841 tfmt->height = mf->height;
842 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300843 NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300844 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
845 NULL, &fcc, FIMC_SD_PAD_SOURCE);
846 if (ffmt && ffmt->mbus_code)
847 mf->code = ffmt->mbus_code;
848 if (mf->width != tfmt->width || mf->height != tfmt->height)
849 continue;
850 tfmt->code = mf->code;
851 break;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300852 }
853
854 if (fmt_id && ffmt)
855 *fmt_id = ffmt;
856 *tfmt = *mf;
857
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300858 return 0;
859}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300860
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300861/**
862 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
863 * @sensor: pointer to the sensor subdev
864 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
865 * @try: true to set the frame parameters, false to query only
866 *
867 * This function is used by this driver only for compressed/blob data formats.
868 */
869static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
870 struct v4l2_plane_pix_format *plane_fmt,
871 unsigned int num_planes, bool try)
872{
873 struct v4l2_mbus_frame_desc fd;
874 int i, ret;
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300875 int pad;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300876
877 for (i = 0; i < num_planes; i++)
878 fd.entry[i].length = plane_fmt[i].sizeimage;
879
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300880 pad = sensor->entity.num_pads - 1;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300881 if (try)
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300882 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300883 else
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300884 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300885
886 if (ret < 0)
887 return ret;
888
889 if (num_planes != fd.num_entries)
890 return -EINVAL;
891
892 for (i = 0; i < num_planes; i++)
893 plane_fmt[i].sizeimage = fd.entry[i].length;
894
895 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
896 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
897 fd.entry[0].length);
898
899 return -EINVAL;
900 }
901
902 return 0;
903}
904
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300905static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
906 struct v4l2_format *f)
907{
908 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300909
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -0300910 __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
911 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300912}
913
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300914/*
915 * Try or set format on the fimc.X.capture video node and additionally
916 * on the whole pipeline if @try is false.
917 * Locking: the caller must _not_ hold the graph mutex.
918 */
919static int __video_try_or_set_format(struct fimc_dev *fimc,
920 struct v4l2_format *f, bool try,
921 struct fimc_fmt **inp_fmt,
922 struct fimc_fmt **out_fmt)
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300923{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300924 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300925 struct fimc_vid_cap *vc = &fimc->vid_cap;
926 struct exynos_video_entity *ve = &vc->ve;
927 struct fimc_ctx *ctx = vc->ctx;
928 unsigned int width = 0, height = 0;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300929 int ret = 0;
930
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300931 /* Pre-configure format at the camera input interface, for JPEG only */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300932 if (fimc_jpeg_fourcc(pix->pixelformat)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300933 fimc_capture_try_format(ctx, &pix->width, &pix->height,
934 NULL, &pix->pixelformat,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300935 FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300936 if (try) {
937 width = pix->width;
938 height = pix->height;
939 } else {
940 ctx->s_frame.f_width = pix->width;
941 ctx->s_frame.f_height = pix->height;
942 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300943 }
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300944
945 /* Try the format at the scaler and the DMA output */
946 *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
947 NULL, &pix->pixelformat,
948 FIMC_SD_PAD_SOURCE);
949 if (*out_fmt == NULL)
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300950 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300951
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300952 /* Restore image width/height for JPEG (no resizing supported). */
953 if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
954 pix->width = width;
955 pix->height = height;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300956 }
957
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300958 /* Try to match format at the host and the sensor */
959 if (!vc->user_subdev_api) {
960 struct v4l2_mbus_framefmt mbus_fmt;
961 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300962
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300963 mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
964
965 mf->code = (*out_fmt)->mbus_code;
966 mf->width = pix->width;
967 mf->height = pix->height;
968
969 fimc_md_graph_lock(ve);
970 ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
971 fimc_md_graph_unlock(ve);
972
973 if (ret < 0)
974 return ret;
975
976 pix->width = mf->width;
977 pix->height = mf->height;
978 }
979
980 fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
981
982 if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
983 struct v4l2_subdev *sensor;
984
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300985 fimc_md_graph_lock(ve);
986
987 sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
988 if (sensor)
989 fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300990 (*out_fmt)->memplanes, try);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300991 else
992 ret = -EPIPE;
993
994 fimc_md_graph_unlock(ve);
995 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300996
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300997 return ret;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300998}
999
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001000static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
1001 struct v4l2_format *f)
1002{
1003 struct fimc_dev *fimc = video_drvdata(file);
1004 struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
1005
1006 return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
1007}
1008
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001009static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
1010 enum fimc_color_fmt color)
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -03001011{
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001012 bool jpeg = fimc_fmt_is_user_defined(color);
1013
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -03001014 ctx->scaler.enabled = !jpeg;
1015 fimc_ctrls_activate(ctx, !jpeg);
1016
1017 if (jpeg)
1018 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1019 else
1020 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1021}
1022
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001023static int __fimc_capture_set_format(struct fimc_dev *fimc,
1024 struct v4l2_format *f)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001025{
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001026 struct fimc_vid_cap *vc = &fimc->vid_cap;
1027 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001028 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001029 struct fimc_frame *ff = &ctx->d_frame;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001030 struct fimc_fmt *inp_fmt = NULL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001031 int ret, i;
1032
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001033 if (vb2_is_busy(&fimc->vid_cap.vbq))
1034 return -EBUSY;
1035
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001036 ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1037 if (ret < 0)
1038 return ret;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001039
1040 /* Update RGB Alpha control state and value range */
1041 fimc_alpha_ctrl_update(ctx);
1042
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001043 for (i = 0; i < ff->fmt->memplanes; i++) {
1044 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
Sylwester Nawrockid547ab62012-05-16 15:00:26 -03001045 ff->payload[i] = pix->plane_fmt[i].sizeimage;
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001046 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001047
1048 set_frame_bounds(ff, pix->width, pix->height);
1049 /* Reset the composition rectangle if not yet configured */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001050 if (!(ctx->state & FIMC_COMPOSE))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001051 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1052
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001053 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -03001054
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001055 /* Reset cropping and set format at the camera interface input */
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001056 if (!vc->user_subdev_api) {
1057 ctx->s_frame.fmt = inp_fmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001058 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1059 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1060 }
1061
1062 return ret;
1063}
1064
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001065static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1066 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001067{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001068 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001069
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001070 return __fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001071}
1072
1073static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001074 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001075{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001076 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001077 struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1078 struct v4l2_subdev *sd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001079
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001080 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001081 return -EINVAL;
1082
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001083 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001084 fimc_md_graph_lock(ve);
1085 sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1086 fimc_md_graph_unlock(ve);
1087
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001088 if (sd)
1089 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001090
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001091 return 0;
1092}
1093
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001094static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001095{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001096 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001097}
1098
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001099static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001100{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001101 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001102 return 0;
1103}
1104
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001105/**
1106 * fimc_pipeline_validate - check for formats inconsistencies
1107 * between source and sink pad of each link
1108 *
1109 * Return 0 if all formats match or -EPIPE otherwise.
1110 */
1111static int fimc_pipeline_validate(struct fimc_dev *fimc)
1112{
1113 struct v4l2_subdev_format sink_fmt, src_fmt;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001114 struct fimc_vid_cap *vc = &fimc->vid_cap;
1115 struct v4l2_subdev *sd = &vc->subdev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001116 struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001117 struct media_pad *sink_pad, *src_pad;
1118 int i, ret;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001119
1120 while (1) {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001121 /*
1122 * Find current entity sink pad and any remote sink pad linked
1123 * to it. We stop if there is no sink pad in current entity or
1124 * it is not linked to any other remote entity.
1125 */
1126 src_pad = NULL;
1127
1128 for (i = 0; i < sd->entity.num_pads; i++) {
1129 struct media_pad *p = &sd->entity.pads[i];
1130
1131 if (p->flags & MEDIA_PAD_FL_SINK) {
1132 sink_pad = p;
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -03001133 src_pad = media_entity_remote_pad(sink_pad);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001134 if (src_pad)
1135 break;
1136 }
1137 }
1138
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001139 if (!src_pad || !is_media_entity_v4l2_subdev(src_pad->entity))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001140 break;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001141
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001142 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001143 if (sd == &vc->subdev) {
1144 struct fimc_frame *ff = &vc->ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001145 sink_fmt.format.width = ff->f_width;
1146 sink_fmt.format.height = ff->f_height;
1147 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1148 } else {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001149 sink_fmt.pad = sink_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001150 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1151 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1152 if (ret < 0 && ret != -ENOIOCTLCMD)
1153 return -EPIPE;
1154 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001155
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001156 /* Retrieve format at the source pad */
1157 sd = media_entity_to_v4l2_subdev(src_pad->entity);
1158 src_fmt.pad = src_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001159 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1160 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1161 if (ret < 0 && ret != -ENOIOCTLCMD)
1162 return -EPIPE;
1163
1164 if (src_fmt.format.width != sink_fmt.format.width ||
1165 src_fmt.format.height != sink_fmt.format.height ||
1166 src_fmt.format.code != sink_fmt.format.code)
1167 return -EPIPE;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001168
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001169 if (sd == p->subdevs[IDX_SENSOR] &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001170 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1171 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001172 struct fimc_frame *frame = &vc->ctx->d_frame;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001173 unsigned int i;
1174
1175 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1176 frame->fmt->memplanes,
1177 false);
1178 if (ret < 0)
1179 return -EPIPE;
1180
1181 for (i = 0; i < frame->fmt->memplanes; i++)
1182 if (frame->payload[i] < plane_fmt[i].sizeimage)
1183 return -EPIPE;
1184 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001185 }
1186 return 0;
1187}
1188
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001189static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001190 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001191{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001192 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001193 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001194 struct media_entity *entity = &vc->ve.vdev.entity;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001195 struct fimc_source_info *si = NULL;
1196 struct v4l2_subdev *sd;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001197 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001198
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001199 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001200 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001201
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001202 ret = media_entity_pipeline_start(entity, &vc->ve.pipe->mp);
Sakari Ailusa60a2952012-05-25 07:08:05 -03001203 if (ret < 0)
1204 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001205
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001206 sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001207 if (sd)
1208 si = v4l2_get_subdev_hostdata(sd);
1209
1210 if (si == NULL) {
1211 ret = -EPIPE;
1212 goto err_p_stop;
1213 }
1214 /*
1215 * Save configuration data related to currently attached image
1216 * sensor or other data source, e.g. FIMC-IS.
1217 */
1218 vc->source_config = *si;
1219
1220 if (vc->input == GRP_ID_FIMC_IS)
1221 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1222
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001223 if (vc->user_subdev_api) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001224 ret = fimc_pipeline_validate(fimc);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001225 if (ret < 0)
1226 goto err_p_stop;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001227 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001228
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001229 ret = vb2_ioctl_streamon(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001230 if (!ret) {
1231 vc->streaming = true;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001232 return ret;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001233 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001234
1235err_p_stop:
1236 media_entity_pipeline_stop(entity);
1237 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001238}
1239
1240static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001241 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001242{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001243 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001244 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001245 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001246
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001247 ret = vb2_ioctl_streamoff(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001248 if (ret < 0)
1249 return ret;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001250
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001251 media_entity_pipeline_stop(&vc->ve.vdev.entity);
1252 vc->streaming = false;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001253 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001254}
1255
1256static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001257 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001258{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001259 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001260 int ret;
1261
1262 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001263
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001264 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001265 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001266
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001267 return ret;
1268}
1269
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001270static int fimc_cap_g_selection(struct file *file, void *fh,
1271 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001272{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001273 struct fimc_dev *fimc = video_drvdata(file);
1274 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001275 struct fimc_frame *f = &ctx->s_frame;
1276
1277 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1278 return -EINVAL;
1279
1280 switch (s->target) {
1281 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1282 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1283 f = &ctx->d_frame;
1284 case V4L2_SEL_TGT_CROP_BOUNDS:
1285 case V4L2_SEL_TGT_CROP_DEFAULT:
1286 s->r.left = 0;
1287 s->r.top = 0;
1288 s->r.width = f->o_width;
1289 s->r.height = f->o_height;
1290 return 0;
1291
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001292 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001293 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001294 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001295 s->r.left = f->offs_h;
1296 s->r.top = f->offs_v;
1297 s->r.width = f->width;
1298 s->r.height = f->height;
1299 return 0;
1300 }
1301
1302 return -EINVAL;
1303}
1304
1305/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
Sachin Kamat7e566be2012-05-26 11:11:54 -03001306static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001307{
1308 if (a->left < b->left || a->top < b->top)
1309 return 0;
1310 if (a->left + a->width > b->left + b->width)
1311 return 0;
1312 if (a->top + a->height > b->top + b->height)
1313 return 0;
1314
1315 return 1;
1316}
1317
1318static int fimc_cap_s_selection(struct file *file, void *fh,
1319 struct v4l2_selection *s)
1320{
1321 struct fimc_dev *fimc = video_drvdata(file);
1322 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1323 struct v4l2_rect rect = s->r;
1324 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001325 unsigned long flags;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001326
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001327 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1328 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001329
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001330 if (s->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001331 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001332 else if (s->target == V4L2_SEL_TGT_CROP)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001333 f = &ctx->s_frame;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001334 else
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001335 return -EINVAL;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001336
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001337 fimc_capture_try_selection(ctx, &rect, s->target);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001338
1339 if (s->flags & V4L2_SEL_FLAG_LE &&
1340 !enclosed_rectangle(&rect, &s->r))
1341 return -ERANGE;
1342
1343 if (s->flags & V4L2_SEL_FLAG_GE &&
1344 !enclosed_rectangle(&s->r, &rect))
1345 return -ERANGE;
1346
1347 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001348 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001349 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1350 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001351 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001352
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001353 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001354 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001355}
1356
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001357static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -03001358 .vidioc_querycap = fimc_cap_querycap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001359
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001360 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001361 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001362 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001363 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001364
1365 .vidioc_reqbufs = fimc_cap_reqbufs,
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001366 .vidioc_querybuf = vb2_ioctl_querybuf,
1367 .vidioc_qbuf = vb2_ioctl_qbuf,
1368 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1369 .vidioc_expbuf = vb2_ioctl_expbuf,
1370 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1371 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001372
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001373 .vidioc_streamon = fimc_cap_streamon,
1374 .vidioc_streamoff = fimc_cap_streamoff,
1375
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001376 .vidioc_g_selection = fimc_cap_g_selection,
1377 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001378
1379 .vidioc_enum_input = fimc_cap_enum_input,
1380 .vidioc_s_input = fimc_cap_s_input,
1381 .vidioc_g_input = fimc_cap_g_input,
1382};
1383
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001384/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001385static int fimc_link_setup(struct media_entity *entity,
1386 const struct media_pad *local,
1387 const struct media_pad *remote, u32 flags)
1388{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001389 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1390 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001391 struct fimc_vid_cap *vc = &fimc->vid_cap;
1392 struct v4l2_subdev *sensor;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001393
Mauro Carvalho Chehab3efdf622015-05-07 22:12:32 -03001394 if (!is_media_entity_v4l2_subdev(remote->entity))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001395 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001396
1397 if (WARN_ON(fimc == NULL))
1398 return 0;
1399
1400 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1401 local->entity->name, remote->entity->name, flags,
1402 fimc->vid_cap.input);
1403
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001404 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1405 fimc->vid_cap.input = 0;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001406 return 0;
1407 }
1408
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001409 if (vc->input != 0)
1410 return -EBUSY;
1411
1412 vc->input = sd->grp_id;
1413
1414 if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1415 return 0;
1416
1417 /* Inherit V4L2 controls from the image sensor subdev. */
1418 sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1419 if (sensor == NULL)
1420 return 0;
1421
1422 return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1423 sensor->ctrl_handler, NULL);
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001424}
1425
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001426static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001427 .link_setup = fimc_link_setup,
1428};
1429
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001430/**
1431 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1432 * @sd: pointer to a subdev generating the notification
1433 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1434 * @arg: pointer to an u32 type integer that stores the frame payload value
1435 *
1436 * The End Of Frame notification sent by sensor subdev in its still capture
1437 * mode. If there is only a single VSYNC generated by the sensor at the
1438 * beginning of a frame transmission, FIMC does not issue the LastIrq
1439 * (end of frame) interrupt. And this notification is used to complete the
1440 * frame capture and returning a buffer to user-space. Subdev drivers should
1441 * call this notification from their last 'End of frame capture' interrupt.
1442 */
1443void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1444 void *arg)
1445{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001446 struct fimc_source_info *si;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001447 struct fimc_vid_buffer *buf;
1448 struct fimc_md *fmd;
1449 struct fimc_dev *fimc;
1450 unsigned long flags;
1451
1452 if (sd == NULL)
1453 return;
1454
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001455 si = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001456 fmd = entity_to_fimc_mdev(&sd->entity);
1457
1458 spin_lock_irqsave(&fmd->slock, flags);
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001459
1460 fimc = si ? source_to_sensor_info(si)->host : NULL;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001461
1462 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1463 test_bit(ST_CAPT_PEND, &fimc->state)) {
1464 unsigned long irq_flags;
1465 spin_lock_irqsave(&fimc->slock, irq_flags);
1466 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1467 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1468 struct fimc_vid_buffer, list);
Junghak Sung2d700712015-09-22 10:30:30 -03001469 vb2_set_plane_payload(&buf->vb.vb2_buf, 0,
1470 *((u32 *)arg));
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001471 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001472 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001473 fimc_deactivate_capture(fimc);
1474 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1475 }
1476 spin_unlock_irqrestore(&fmd->slock, flags);
1477}
1478
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001479static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001480 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001481 struct v4l2_subdev_mbus_code_enum *code)
1482{
1483 struct fimc_fmt *fmt;
1484
1485 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1486 if (!fmt)
1487 return -EINVAL;
1488 code->code = fmt->mbus_code;
1489 return 0;
1490}
1491
1492static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001493 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001494 struct v4l2_subdev_format *fmt)
1495{
1496 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1497 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001498 struct fimc_frame *ff = &ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001499 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001500
1501 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
Hans Verkuilf7234132015-03-04 01:47:54 -08001502 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001503 fmt->format = *mf;
1504 return 0;
1505 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001506
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001507 mf = &fmt->format;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001508 mutex_lock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001509
1510 switch (fmt->pad) {
1511 case FIMC_SD_PAD_SOURCE:
1512 if (!WARN_ON(ff->fmt == NULL))
1513 mf->code = ff->fmt->mbus_code;
1514 /* Sink pads crop rectangle size */
1515 mf->width = ff->width;
1516 mf->height = ff->height;
1517 break;
1518 case FIMC_SD_PAD_SINK_FIFO:
1519 *mf = fimc->vid_cap.wb_fmt;
1520 break;
1521 case FIMC_SD_PAD_SINK_CAM:
1522 default:
1523 *mf = fimc->vid_cap.ci_fmt;
1524 break;
1525 }
1526
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001527 mutex_unlock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001528 mf->colorspace = V4L2_COLORSPACE_JPEG;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001529
1530 return 0;
1531}
1532
1533static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001534 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001535 struct v4l2_subdev_format *fmt)
1536{
1537 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1538 struct v4l2_mbus_framefmt *mf = &fmt->format;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001539 struct fimc_vid_cap *vc = &fimc->vid_cap;
1540 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001541 struct fimc_frame *ff;
1542 struct fimc_fmt *ffmt;
1543
1544 dbg("pad%d: code: 0x%x, %dx%d",
1545 fmt->pad, mf->code, mf->width, mf->height);
1546
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001547 if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001548 return -EBUSY;
1549
1550 mutex_lock(&fimc->lock);
1551 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1552 &mf->code, NULL, fmt->pad);
1553 mutex_unlock(&fimc->lock);
1554 mf->colorspace = V4L2_COLORSPACE_JPEG;
1555
1556 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
Hans Verkuilf7234132015-03-04 01:47:54 -08001557 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001558 *mf = fmt->format;
1559 return 0;
1560 }
Sylwester Nawrocki66125452012-12-05 13:52:00 -03001561 /* There must be a bug in the driver if this happens */
1562 if (WARN_ON(ffmt == NULL))
1563 return -EINVAL;
1564
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001565 /* Update RGB Alpha control state and value range */
1566 fimc_alpha_ctrl_update(ctx);
1567
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001568 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001569 if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1570 ff = &ctx->d_frame;
1571 /* Sink pads crop rectangle size */
1572 mf->width = ctx->s_frame.width;
1573 mf->height = ctx->s_frame.height;
1574 } else {
1575 ff = &ctx->s_frame;
1576 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001577
1578 mutex_lock(&fimc->lock);
1579 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001580
1581 if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1582 vc->wb_fmt = *mf;
1583 else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1584 vc->ci_fmt = *mf;
1585
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001586 ff->fmt = ffmt;
1587
1588 /* Reset the crop rectangle if required. */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001589 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001590 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1591
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001592 if (fmt->pad != FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001593 ctx->state &= ~FIMC_COMPOSE;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001594
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001595 mutex_unlock(&fimc->lock);
1596 return 0;
1597}
1598
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001599static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
Hans Verkuilf7234132015-03-04 01:47:54 -08001600 struct v4l2_subdev_pad_config *cfg,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001601 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001602{
1603 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1604 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001605 struct fimc_frame *f = &ctx->s_frame;
1606 struct v4l2_rect *r = &sel->r;
1607 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001608
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001609 if (sel->pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001610 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001611
1612 mutex_lock(&fimc->lock);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001613
1614 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001615 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001616 f = &ctx->d_frame;
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
1702static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1703 .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
1710static struct v4l2_subdev_ops fimc_subdev_ops = {
1711 .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;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -03001763
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001764 video_set_drvdata(vfd, fimc);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001765 vid_cap = &fimc->vid_cap;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001766 vid_cap->active_buf_cnt = 0;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001767 vid_cap->reqbufs_count = 0;
1768 vid_cap->ctx = ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001769
1770 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1771 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001772
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001773 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001774 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Tomasz Stanislawski9bd09fd2012-06-14 10:37:49 -03001775 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001776 q->drv_priv = ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001777 q->ops = &fimc_capture_qops;
1778 q->mem_ops = &vb2_dma_contig_memops;
1779 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -03001780 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001781 q->lock = &fimc->lock;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001782
Sylwester Nawrocki41fd0872012-10-19 08:24:18 -03001783 ret = vb2_queue_init(q);
1784 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001785 goto err_free_ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001786
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001787 /* Default format configuration */
1788 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1789 vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1790 vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1791 vid_cap->ci_fmt.code = fmt->mbus_code;
1792
1793 ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1794 ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1795 ctx->s_frame.fmt = fmt;
1796
1797 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1798 vid_cap->wb_fmt = vid_cap->ci_fmt;
1799 vid_cap->wb_fmt.code = fmt->mbus_code;
1800
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001801 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001802 ret = media_entity_pads_init(&vfd->entity, 1, &vid_cap->vd_pad);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001803 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001804 goto err_free_ctx;
1805
1806 ret = fimc_ctrls_create(ctx);
1807 if (ret)
1808 goto err_me_cleanup;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001809
1810 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001811 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001812 goto err_ctrl_free;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001813
1814 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1815 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001816
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -03001817 vfd->ctrl_handler = &ctx->ctrls.handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001818 return 0;
1819
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001820err_ctrl_free:
1821 fimc_ctrls_delete(ctx);
1822err_me_cleanup:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001823 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001824err_free_ctx:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001825 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001826 return ret;
1827}
1828
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001829static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001830{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001831 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1832 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001833
Sylwester Nawrockibbc52962012-07-26 07:59:11 -03001834 if (fimc == NULL)
1835 return -ENXIO;
1836
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001837 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1838 if (ret)
1839 return ret;
1840
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001841 fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001842
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001843 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001844 if (ret) {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001845 fimc_unregister_m2m_device(fimc);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001846 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001847 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001848
1849 return ret;
1850}
1851
1852static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1853{
1854 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001855 struct video_device *vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001856
1857 if (fimc == NULL)
1858 return;
1859
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001860 mutex_lock(&fimc->lock);
1861
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001862 fimc_unregister_m2m_device(fimc);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001863 vdev = &fimc->vid_cap.ve.vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001864
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001865 if (video_is_registered(vdev)) {
1866 video_unregister_device(vdev);
1867 media_entity_cleanup(&vdev->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001868 fimc_ctrls_delete(fimc->vid_cap.ctx);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001869 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001870 }
1871 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001872 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001873
1874 mutex_unlock(&fimc->lock);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001875}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001876
1877static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1878 .registered = fimc_capture_subdev_registered,
1879 .unregistered = fimc_capture_subdev_unregistered,
1880};
1881
1882int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1883{
1884 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1885 int ret;
1886
1887 v4l2_subdev_init(sd, &fimc_subdev_ops);
Sylwester Nawrocki5a665612013-04-18 08:49:54 -03001888 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -03001889 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001890
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001891 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1892 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001893 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02001894 ret = media_entity_pads_init(&sd->entity, FIMC_SD_PADS_NUM,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -03001895 fimc->vid_cap.sd_pads);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001896 if (ret)
1897 return ret;
1898
1899 sd->entity.ops = &fimc_sd_media_ops;
1900 sd->internal_ops = &fimc_capture_sd_internal_ops;
1901 v4l2_set_subdevdata(sd, fimc);
1902 return 0;
1903}
1904
1905void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1906{
1907 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1908
1909 v4l2_device_unregister_subdev(sd);
1910 media_entity_cleanup(&sd->entity);
1911 v4l2_set_subdevdata(sd, NULL);
1912}