blob: fb27ff7e1e0750e33215b96ee100c9ad569fd9dd [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>
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030027#include <media/videobuf2-core.h>
28#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);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300106 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
107 }
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
114 vb2_buffer_done(&buf->vb, 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 Nawrockiefb13c3d2012-03-19 13:11:40 -0300160
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300161 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-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 Nawrockiefb13c3d2012-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 Nawrockiefb13c3d2012-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;
186 struct timeval *tv;
187 struct timespec ts;
188
189 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
190 wake_up(&fimc->irq_queue);
191 goto done;
192 }
193
194 if (!list_empty(&cap->active_buf_q) &&
195 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
196 ktime_get_real_ts(&ts);
197
198 v_buf = fimc_active_queue_pop(cap);
199
200 tv = &v_buf->vb.v4l2_buf.timestamp;
201 tv->tv_sec = ts.tv_sec;
202 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
203 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
204
205 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
206 }
207
208 if (!list_empty(&cap->pending_buf_q)) {
209
210 v_buf = fimc_pending_queue_pop(cap);
211 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
212 v_buf->index = cap->buf_index;
213
214 /* Move the buffer to the capture active queue */
215 fimc_active_queue_add(cap, v_buf);
216
217 dbg("next frame: %d, done frame: %d",
218 fimc_hw_get_frame_index(fimc), v_buf->index);
219
220 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
221 cap->buf_index = 0;
222 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300223 /*
224 * Set up a buffer at MIPI-CSIS if current image format
225 * requires the frame embedded data capture.
226 */
227 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
228 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
229 unsigned int size = f->payload[plane];
230 s32 index = fimc_hw_get_frame_index(fimc);
231 void *vaddr;
232
233 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
234 if (v_buf->index != index)
235 continue;
236 vaddr = vb2_plane_vaddr(&v_buf->vb, plane);
237 v4l2_subdev_call(csis, video, s_rx_buffer,
238 vaddr, &size);
239 break;
240 }
241 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300242
243 if (cap->active_buf_cnt == 0) {
244 if (deq_buf)
245 clear_bit(ST_CAPT_RUN, &fimc->state);
246
247 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
248 cap->buf_index = 0;
249 } else {
250 set_bit(ST_CAPT_RUN, &fimc->state);
251 }
252
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300253 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
254 fimc_capture_config_update(cap->ctx);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300255done:
256 if (cap->active_buf_cnt == 1) {
257 fimc_deactivate_capture(fimc);
258 clear_bit(ST_CAPT_STREAM, &fimc->state);
259 }
260
261 dbg("frame: %d, active_buf_cnt: %d",
262 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
263}
264
265
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300266static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300267{
268 struct fimc_ctx *ctx = q->drv_priv;
269 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300270 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300271 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300272 int ret;
273
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300274 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300275
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300276 ret = fimc_capture_hw_init(fimc);
277 if (ret) {
278 fimc_capture_state_cleanup(fimc, false);
279 return ret;
280 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300281
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300282 set_bit(ST_CAPT_PEND, &fimc->state);
283
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300284 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
285
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300286 if (vid_cap->active_buf_cnt >= min_bufs &&
287 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300288 fimc_activate_capture(ctx);
289
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300290 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300291 return fimc_pipeline_call(&vid_cap->ve, set_stream, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300292 }
293
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300294 return 0;
295}
296
297static int stop_streaming(struct vb2_queue *q)
298{
299 struct fimc_ctx *ctx = q->drv_priv;
300 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300301
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300302 if (!fimc_capture_active(fimc))
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300303 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300304
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300305 return fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300306}
307
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300308int fimc_capture_suspend(struct fimc_dev *fimc)
309{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300310 bool suspend = fimc_capture_busy(fimc);
311
312 int ret = fimc_stop_capture(fimc, suspend);
313 if (ret)
314 return ret;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300315 return fimc_pipeline_call(&fimc->vid_cap.ve, close);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300316}
317
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300318static void buffer_queue(struct vb2_buffer *vb);
319
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300320int fimc_capture_resume(struct fimc_dev *fimc)
321{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300322 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300323 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300324 struct fimc_vid_buffer *buf;
325 int i;
326
327 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
328 return 0;
329
330 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
331 vid_cap->buf_index = 0;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300332 fimc_pipeline_call(ve, open, &ve->vdev.entity, false);
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300333 fimc_capture_hw_init(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300334
335 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
336
337 for (i = 0; i < vid_cap->reqbufs_count; i++) {
338 if (list_empty(&vid_cap->pending_buf_q))
339 break;
340 buf = fimc_pending_queue_pop(vid_cap);
341 buffer_queue(&buf->vb);
342 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300343 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300344
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300345}
346
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300347static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300348 unsigned int *num_buffers, unsigned int *num_planes,
349 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300350{
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300351 const struct v4l2_pix_format_mplane *pixm = NULL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300352 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300353 struct fimc_frame *frame = &ctx->d_frame;
354 struct fimc_fmt *fmt = frame->fmt;
355 unsigned long wh;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300356 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300357
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300358 if (pfmt) {
359 pixm = &pfmt->fmt.pix_mp;
360 fmt = fimc_find_format(&pixm->pixelformat, NULL,
361 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
362 wh = pixm->width * pixm->height;
363 } else {
364 wh = frame->f_width * frame->f_height;
365 }
366
367 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300368 return -EINVAL;
369
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300370 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300371
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300372 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300373 unsigned int size = (wh * fmt->depth[i]) / 8;
374 if (pixm)
375 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300376 else if (fimc_fmt_is_user_defined(fmt->color))
377 sizes[i] = frame->payload[i];
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300378 else
Sylwester Nawrockid547ab62012-05-16 15:00:26 -0300379 sizes[i] = max_t(u32, size, frame->payload[i]);
380
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300381 allocators[i] = ctx->fimc_dev->alloc_ctx;
382 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300383
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300384 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300385}
386
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300387static int buffer_prepare(struct vb2_buffer *vb)
388{
389 struct vb2_queue *vq = vb->vb2_queue;
390 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300391 int i;
392
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300393 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300394 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300395
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300396 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300397 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300398
399 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300400 v4l2_err(&ctx->fimc_dev->vid_cap.ve.vdev,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300401 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300402 vb2_plane_size(vb, i), size);
403 return -EINVAL;
404 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300405 vb2_set_plane_payload(vb, i, size);
406 }
407
408 return 0;
409}
410
411static void buffer_queue(struct vb2_buffer *vb)
412{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300413 struct fimc_vid_buffer *buf
414 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300415 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
416 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300417 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300418 struct exynos_video_entity *ve = &vid_cap->ve;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300419 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300420 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300421
422 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300423 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300424
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300425 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
426 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
427 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300428 /* Setup the buffer directly for processing. */
429 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
430 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300431
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300432 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
433 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300434 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300435
436 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
437 vid_cap->buf_index = 0;
438 } else {
439 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300440 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300441
442 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
443
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300444
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300445 if (vb2_is_streaming(&vid_cap->vbq) &&
446 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300447 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Andrzej Hajda76323e52013-02-27 08:13:59 -0300448 int ret;
449
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300450 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300451 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300452
Andrzej Hajda76323e52013-02-27 08:13:59 -0300453 if (test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
454 return;
455
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300456 ret = fimc_pipeline_call(ve, set_stream, 1);
Andrzej Hajda76323e52013-02-27 08:13:59 -0300457 if (ret < 0)
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -0300458 v4l2_err(&ve->vdev, "stream on failed: %d\n", ret);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300459 return;
460 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300461 spin_unlock_irqrestore(&fimc->slock, flags);
462}
463
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300464static struct vb2_ops fimc_capture_qops = {
465 .queue_setup = queue_setup,
466 .buf_prepare = buffer_prepare,
467 .buf_queue = buffer_queue,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300468 .wait_prepare = vb2_ops_wait_prepare,
469 .wait_finish = vb2_ops_wait_finish,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300470 .start_streaming = start_streaming,
471 .stop_streaming = stop_streaming,
472};
473
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300474static int fimc_capture_set_default_format(struct fimc_dev *fimc);
475
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300476static int fimc_capture_open(struct file *file)
477{
478 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300479 struct fimc_vid_cap *vc = &fimc->vid_cap;
480 struct exynos_video_entity *ve = &vc->ve;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300481 int ret = -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300482
483 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
484
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300485 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300486
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300487 if (fimc_m2m_active(fimc))
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300488 goto unlock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300489
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300490 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300491 ret = pm_runtime_get_sync(&fimc->pdev->dev);
492 if (ret < 0)
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300493 goto unlock;
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300494
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300495 ret = v4l2_fh_open(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300496 if (ret) {
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300497 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300498 goto unlock;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300499 }
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300500
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300501 if (v4l2_fh_is_singular_file(file)) {
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300502 fimc_md_graph_lock(ve);
503
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300504 ret = fimc_pipeline_call(ve, open, &ve->vdev.entity, true);
505
Sylwester Nawrocki44031062013-05-31 11:37:19 -0300506 if (ret == 0 && vc->user_subdev_api && vc->inh_sensor_ctrls) {
507 /*
508 * Recreate controls of the the video node to drop
509 * any controls inherited from the sensor subdev.
510 */
511 fimc_ctrls_delete(vc->ctx);
512
513 ret = fimc_ctrls_create(vc->ctx);
514 if (ret == 0)
515 vc->inh_sensor_ctrls = false;
516 }
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300517 if (ret == 0)
518 ve->vdev.entity.use_count++;
519
520 fimc_md_graph_unlock(ve);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300521
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300522 if (ret == 0)
523 ret = fimc_capture_set_default_format(fimc);
524
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300525 if (ret < 0) {
526 clear_bit(ST_CAPT_BUSY, &fimc->state);
527 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300528 v4l2_fh_release(file);
529 }
530 }
531unlock:
532 mutex_unlock(&fimc->lock);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300533 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300534}
535
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300536static int fimc_capture_release(struct file *file)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300537{
538 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -0300539 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300540 bool close = v4l2_fh_is_singular_file(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300541 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300542
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300543 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
544
Sylwester Nawrockiba6b3722012-11-22 11:12:16 -0300545 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300546
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300547 if (close && vc->streaming) {
548 media_entity_pipeline_stop(&vc->ve.vdev.entity);
549 vc->streaming = false;
550 }
551
552 ret = vb2_fop_release(file);
553
554 if (close) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300555 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300556 fimc_pipeline_call(&vc->ve, close);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300557 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300558
559 fimc_md_graph_lock(&vc->ve);
560 vc->ve.vdev.entity.use_count--;
561 fimc_md_graph_unlock(&vc->ve);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300562 }
563
Sylwester Nawrocki4bd728a2013-05-31 11:37:21 -0300564 pm_runtime_put_sync(&fimc->pdev->dev);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300565 mutex_unlock(&fimc->lock);
566
567 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300568}
569
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300570static const struct v4l2_file_operations fimc_capture_fops = {
571 .owner = THIS_MODULE,
572 .open = fimc_capture_open,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300573 .release = fimc_capture_release,
574 .poll = vb2_fop_poll,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300575 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrockic4449142013-03-25 16:36:35 -0300576 .mmap = vb2_fop_mmap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300577};
578
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300579/*
580 * Format and crop negotiation helpers
581 */
582
583static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
584 u32 *width, u32 *height,
585 u32 *code, u32 *fourcc, int pad)
586{
587 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
588 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300589 const struct fimc_variant *var = fimc->variant;
590 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300591 struct fimc_frame *dst = &ctx->d_frame;
592 u32 depth, min_w, max_w, min_h, align_h = 3;
593 u32 mask = FMT_FLAGS_CAM;
594 struct fimc_fmt *ffmt;
595
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300596 /* Conversion from/to JPEG or User Defined format is not supported */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300597 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300598 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
599 *code = ctx->s_frame.fmt->mbus_code;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300600
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300601 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300602 mask |= FMT_FLAGS_M2M;
603
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300604 if (pad == FIMC_SD_PAD_SINK_FIFO)
605 mask = FMT_FLAGS_WRITEBACK;
606
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300607 ffmt = fimc_find_format(fourcc, code, mask, 0);
608 if (WARN_ON(!ffmt))
609 return NULL;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300610
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300611 if (code)
612 *code = ffmt->mbus_code;
613 if (fourcc)
614 *fourcc = ffmt->fourcc;
615
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300616 if (pad != FIMC_SD_PAD_SOURCE) {
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300617 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300618 pl->scaler_dis_w : pl->scaler_en_w;
619 /* Apply the camera input interface pixel constraints */
620 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
621 height, max_t(u32, *height, 32),
622 FIMC_CAMIF_MAX_HEIGHT,
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300623 fimc_fmt_is_user_defined(ffmt->color) ?
624 3 : 1,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300625 0);
626 return ffmt;
627 }
628 /* Can't scale or crop in transparent (JPEG) transfer mode */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300629 if (fimc_fmt_is_user_defined(ffmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300630 *width = ctx->s_frame.f_width;
631 *height = ctx->s_frame.f_height;
632 return ffmt;
633 }
634 /* Apply the scaler and the output DMA constraints */
635 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300636 if (ctx->state & FIMC_COMPOSE) {
637 min_w = dst->offs_h + dst->width;
638 min_h = dst->offs_v + dst->height;
639 } else {
640 min_w = var->min_out_pixsize;
641 min_h = var->min_out_pixsize;
642 }
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300643 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300644 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
645
646 depth = fimc_get_format_depth(ffmt);
647 v4l_bound_align_image(width, min_w, max_w,
648 ffs(var->min_out_pixsize) - 1,
649 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
650 align_h,
651 64/(ALIGN(depth, 8)));
652
653 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
654 pad, code ? *code : 0, *width, *height,
655 dst->f_width, dst->f_height);
656
657 return ffmt;
658}
659
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300660static void fimc_capture_try_selection(struct fimc_ctx *ctx,
661 struct v4l2_rect *r,
662 int target)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300663{
664 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
665 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300666 const struct fimc_variant *var = fimc->variant;
667 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300668 struct fimc_frame *sink = &ctx->s_frame;
669 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
670 u32 align_sz = 0, align_h = 4;
671 u32 max_sc_h, max_sc_v;
672
673 /* In JPEG transparent transfer mode cropping is not supported */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300674 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300675 r->width = sink->f_width;
676 r->height = sink->f_height;
677 r->left = r->top = 0;
678 return;
679 }
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300680 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300681 if (ctx->rotation != 90 && ctx->rotation != 270)
682 align_h = 1;
683 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
684 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
685 min_sz = var->min_out_pixsize;
686 } else {
687 u32 depth = fimc_get_format_depth(sink->fmt);
688 align_sz = 64/ALIGN(depth, 8);
689 min_sz = var->min_inp_pixsize;
690 min_w = min_h = min_sz;
691 max_sc_h = max_sc_v = 1;
692 }
693 /*
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300694 * For the compose rectangle the following constraints must be met:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300695 * - it must fit in the sink pad format rectangle (f_width/f_height);
696 * - maximum downscaling ratio is 64;
697 * - maximum crop size depends if the rotator is used or not;
698 * - the sink pad format width/height must be 4 multiple of the
699 * prescaler ratios determined by sink pad size and source pad crop,
700 * the prescaler ratio is returned by fimc_get_scaler_factor().
701 */
702 max_w = min_t(u32,
703 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
704 rotate ? sink->f_height : sink->f_width);
705 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300706
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300707 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300708 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
709 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
710 if (rotate) {
711 swap(max_sc_h, max_sc_v);
712 swap(min_w, min_h);
713 }
714 }
715 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
716 &r->height, min_h, max_h, align_h,
717 align_sz);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300718 /* Adjust left/top if crop/compose rectangle is out of bounds */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300719 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
720 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
721 r->left = round_down(r->left, var->hor_offs_align);
722
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300723 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
724 target, r->left, r->top, r->width, r->height,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300725 sink->f_width, sink->f_height);
726}
727
728/*
729 * The video node ioctl operations
730 */
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -0300731static int fimc_cap_querycap(struct file *file, void *priv,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300732 struct v4l2_capability *cap)
733{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300734 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300735
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -0300736 __fimc_vidioc_querycap(&fimc->pdev->dev, cap, V4L2_CAP_STREAMING |
737 V4L2_CAP_VIDEO_CAPTURE_MPLANE);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300738 return 0;
739}
740
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300741static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
742 struct v4l2_fmtdesc *f)
743{
744 struct fimc_fmt *fmt;
745
746 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
747 f->index);
748 if (!fmt)
749 return -EINVAL;
750 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
751 f->pixelformat = fmt->fourcc;
752 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
753 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
754 return 0;
755}
756
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300757static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
758{
759 struct media_pad *pad = &me->pads[0];
760
761 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300762 pad = media_entity_remote_pad(pad);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300763 if (!pad)
764 break;
765 me = pad->entity;
766 pad = &me->pads[0];
767 }
768
769 return me;
770}
771
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300772/**
773 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
774 * elements
775 * @ctx: FIMC capture context
776 * @tfmt: media bus format to try/set on subdevs
777 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
778 * @set: true to set format on subdevs, false to try only
779 */
780static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
781 struct v4l2_mbus_framefmt *tfmt,
782 struct fimc_fmt **fmt_id,
783 bool set)
784{
785 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300786 struct fimc_pipeline *p = to_fimc_pipeline(fimc->vid_cap.ve.pipe);
787 struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300788 struct v4l2_subdev_format sfmt;
789 struct v4l2_mbus_framefmt *mf = &sfmt.format;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300790 struct media_entity *me;
791 struct fimc_fmt *ffmt;
792 struct media_pad *pad;
793 int ret, i = 1;
794 u32 fcc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300795
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300796 if (WARN_ON(!sd || !tfmt))
797 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300798
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300799 memset(&sfmt, 0, sizeof(sfmt));
800 sfmt.format = *tfmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300801 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300802
803 me = fimc_pipeline_get_head(&sd->entity);
804
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300805 while (1) {
806 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
807 FMT_FLAGS_CAM, i++);
808 if (ffmt == NULL) {
809 /*
810 * Notify user-space if common pixel code for
811 * host and sensor does not exist.
812 */
813 return -EINVAL;
814 }
815 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300816
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300817 /* set format on all pipeline subdevs */
818 while (me != &fimc->vid_cap.subdev.entity) {
819 sd = media_entity_to_v4l2_subdev(me);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300820
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300821 sfmt.pad = 0;
822 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
823 if (ret)
824 return ret;
825
826 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
827 sfmt.pad = me->num_pads - 1;
828 mf->code = tfmt->code;
829 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
830 &sfmt);
831 if (ret)
832 return ret;
833 }
834
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300835 pad = media_entity_remote_pad(&me->pads[sfmt.pad]);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300836 if (!pad)
837 return -EINVAL;
838 me = pad->entity;
839 }
840
841 if (mf->code != tfmt->code)
842 continue;
843
844 fcc = ffmt->fourcc;
845 tfmt->width = mf->width;
846 tfmt->height = mf->height;
847 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300848 NULL, &fcc, FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300849 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
850 NULL, &fcc, FIMC_SD_PAD_SOURCE);
851 if (ffmt && ffmt->mbus_code)
852 mf->code = ffmt->mbus_code;
853 if (mf->width != tfmt->width || mf->height != tfmt->height)
854 continue;
855 tfmt->code = mf->code;
856 break;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300857 }
858
859 if (fmt_id && ffmt)
860 *fmt_id = ffmt;
861 *tfmt = *mf;
862
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300863 return 0;
864}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300865
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300866/**
867 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
868 * @sensor: pointer to the sensor subdev
869 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
870 * @try: true to set the frame parameters, false to query only
871 *
872 * This function is used by this driver only for compressed/blob data formats.
873 */
874static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
875 struct v4l2_plane_pix_format *plane_fmt,
876 unsigned int num_planes, bool try)
877{
878 struct v4l2_mbus_frame_desc fd;
879 int i, ret;
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300880 int pad;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300881
882 for (i = 0; i < num_planes; i++)
883 fd.entry[i].length = plane_fmt[i].sizeimage;
884
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300885 pad = sensor->entity.num_pads - 1;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300886 if (try)
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300887 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300888 else
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300889 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300890
891 if (ret < 0)
892 return ret;
893
894 if (num_planes != fd.num_entries)
895 return -EINVAL;
896
897 for (i = 0; i < num_planes; i++)
898 plane_fmt[i].sizeimage = fd.entry[i].length;
899
900 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
901 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
902 fd.entry[0].length);
903
904 return -EINVAL;
905 }
906
907 return 0;
908}
909
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300910static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
911 struct v4l2_format *f)
912{
913 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300914
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -0300915 __fimc_get_format(&fimc->vid_cap.ctx->d_frame, f);
916 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300917}
918
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300919/*
920 * Try or set format on the fimc.X.capture video node and additionally
921 * on the whole pipeline if @try is false.
922 * Locking: the caller must _not_ hold the graph mutex.
923 */
924static int __video_try_or_set_format(struct fimc_dev *fimc,
925 struct v4l2_format *f, bool try,
926 struct fimc_fmt **inp_fmt,
927 struct fimc_fmt **out_fmt)
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300928{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300929 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300930 struct fimc_vid_cap *vc = &fimc->vid_cap;
931 struct exynos_video_entity *ve = &vc->ve;
932 struct fimc_ctx *ctx = vc->ctx;
933 unsigned int width = 0, height = 0;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300934 int ret = 0;
935
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300936 /* Pre-configure format at the camera input interface, for JPEG only */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300937 if (fimc_jpeg_fourcc(pix->pixelformat)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300938 fimc_capture_try_format(ctx, &pix->width, &pix->height,
939 NULL, &pix->pixelformat,
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -0300940 FIMC_SD_PAD_SINK_CAM);
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300941 if (try) {
942 width = pix->width;
943 height = pix->height;
944 } else {
945 ctx->s_frame.f_width = pix->width;
946 ctx->s_frame.f_height = pix->height;
947 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300948 }
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300949
950 /* Try the format at the scaler and the DMA output */
951 *out_fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
952 NULL, &pix->pixelformat,
953 FIMC_SD_PAD_SOURCE);
954 if (*out_fmt == NULL)
Sylwester Nawrocki42625fd2013-05-31 11:37:20 -0300955 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300956
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300957 /* Restore image width/height for JPEG (no resizing supported). */
958 if (try && fimc_jpeg_fourcc(pix->pixelformat)) {
959 pix->width = width;
960 pix->height = height;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300961 }
962
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300963 /* Try to match format at the host and the sensor */
964 if (!vc->user_subdev_api) {
965 struct v4l2_mbus_framefmt mbus_fmt;
966 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300967
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300968 mf = try ? &mbus_fmt : &fimc->vid_cap.ci_fmt;
969
970 mf->code = (*out_fmt)->mbus_code;
971 mf->width = pix->width;
972 mf->height = pix->height;
973
974 fimc_md_graph_lock(ve);
975 ret = fimc_pipeline_try_format(ctx, mf, inp_fmt, try);
976 fimc_md_graph_unlock(ve);
977
978 if (ret < 0)
979 return ret;
980
981 pix->width = mf->width;
982 pix->height = mf->height;
983 }
984
985 fimc_adjust_mplane_format(*out_fmt, pix->width, pix->height, pix);
986
987 if ((*out_fmt)->flags & FMT_FLAGS_COMPRESSED) {
988 struct v4l2_subdev *sensor;
989
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300990 fimc_md_graph_lock(ve);
991
992 sensor = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
993 if (sensor)
994 fimc_get_sensor_frame_desc(sensor, pix->plane_fmt,
Sylwester Nawrocki7536b422013-05-31 13:47:02 -0300995 (*out_fmt)->memplanes, try);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -0300996 else
997 ret = -EPIPE;
998
999 fimc_md_graph_unlock(ve);
1000 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001001
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001002 return ret;
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001003}
1004
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001005static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
1006 struct v4l2_format *f)
1007{
1008 struct fimc_dev *fimc = video_drvdata(file);
1009 struct fimc_fmt *out_fmt = NULL, *inp_fmt = NULL;
1010
1011 return __video_try_or_set_format(fimc, f, true, &inp_fmt, &out_fmt);
1012}
1013
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001014static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
1015 enum fimc_color_fmt color)
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001016{
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001017 bool jpeg = fimc_fmt_is_user_defined(color);
1018
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001019 ctx->scaler.enabled = !jpeg;
1020 fimc_ctrls_activate(ctx, !jpeg);
1021
1022 if (jpeg)
1023 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1024 else
1025 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1026}
1027
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001028static int __fimc_capture_set_format(struct fimc_dev *fimc,
1029 struct v4l2_format *f)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001030{
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001031 struct fimc_vid_cap *vc = &fimc->vid_cap;
1032 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001033 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001034 struct fimc_frame *ff = &ctx->d_frame;
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001035 struct fimc_fmt *inp_fmt = NULL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001036 int ret, i;
1037
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001038 if (vb2_is_busy(&fimc->vid_cap.vbq))
1039 return -EBUSY;
1040
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001041 ret = __video_try_or_set_format(fimc, f, false, &inp_fmt, &ff->fmt);
1042 if (ret < 0)
1043 return ret;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001044
1045 /* Update RGB Alpha control state and value range */
1046 fimc_alpha_ctrl_update(ctx);
1047
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001048 for (i = 0; i < ff->fmt->memplanes; i++) {
1049 ff->bytesperline[i] = pix->plane_fmt[i].bytesperline;
Sylwester Nawrockid547ab62012-05-16 15:00:26 -03001050 ff->payload[i] = pix->plane_fmt[i].sizeimage;
Sylwester Nawrockifa8880b2013-01-11 06:36:19 -03001051 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001052
1053 set_frame_bounds(ff, pix->width, pix->height);
1054 /* Reset the composition rectangle if not yet configured */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001055 if (!(ctx->state & FIMC_COMPOSE))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001056 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1057
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001058 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001059
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001060 /* Reset cropping and set format at the camera interface input */
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001061 if (!vc->user_subdev_api) {
1062 ctx->s_frame.fmt = inp_fmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001063 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1064 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1065 }
1066
1067 return ret;
1068}
1069
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001070static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1071 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001072{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001073 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001074
Sylwester Nawrocki7536b422013-05-31 13:47:02 -03001075 return __fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001076}
1077
1078static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001079 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001080{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001081 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001082 struct exynos_video_entity *ve = &fimc->vid_cap.ve;
1083 struct v4l2_subdev *sd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001084
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001085 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001086 return -EINVAL;
1087
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001088 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001089 fimc_md_graph_lock(ve);
1090 sd = __fimc_md_get_subdev(ve->pipe, IDX_SENSOR);
1091 fimc_md_graph_unlock(ve);
1092
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001093 if (sd)
1094 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001095
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001096 return 0;
1097}
1098
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001099static int fimc_cap_s_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 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001102}
1103
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001104static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001105{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001106 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001107 return 0;
1108}
1109
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001110/**
1111 * fimc_pipeline_validate - check for formats inconsistencies
1112 * between source and sink pad of each link
1113 *
1114 * Return 0 if all formats match or -EPIPE otherwise.
1115 */
1116static int fimc_pipeline_validate(struct fimc_dev *fimc)
1117{
1118 struct v4l2_subdev_format sink_fmt, src_fmt;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001119 struct fimc_vid_cap *vc = &fimc->vid_cap;
1120 struct v4l2_subdev *sd = &vc->subdev;
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001121 struct fimc_pipeline *p = to_fimc_pipeline(vc->ve.pipe);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001122 struct media_pad *sink_pad, *src_pad;
1123 int i, ret;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001124
1125 while (1) {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001126 /*
1127 * Find current entity sink pad and any remote sink pad linked
1128 * to it. We stop if there is no sink pad in current entity or
1129 * it is not linked to any other remote entity.
1130 */
1131 src_pad = NULL;
1132
1133 for (i = 0; i < sd->entity.num_pads; i++) {
1134 struct media_pad *p = &sd->entity.pads[i];
1135
1136 if (p->flags & MEDIA_PAD_FL_SINK) {
1137 sink_pad = p;
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -03001138 src_pad = media_entity_remote_pad(sink_pad);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001139 if (src_pad)
1140 break;
1141 }
1142 }
1143
1144 if (src_pad == NULL ||
1145 media_entity_type(src_pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001146 break;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001147
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001148 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001149 if (sd == &vc->subdev) {
1150 struct fimc_frame *ff = &vc->ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001151 sink_fmt.format.width = ff->f_width;
1152 sink_fmt.format.height = ff->f_height;
1153 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1154 } else {
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001155 sink_fmt.pad = sink_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001156 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1157 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1158 if (ret < 0 && ret != -ENOIOCTLCMD)
1159 return -EPIPE;
1160 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001161
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001162 /* Retrieve format at the source pad */
1163 sd = media_entity_to_v4l2_subdev(src_pad->entity);
1164 src_fmt.pad = src_pad->index;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001165 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1166 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1167 if (ret < 0 && ret != -ENOIOCTLCMD)
1168 return -EPIPE;
1169
1170 if (src_fmt.format.width != sink_fmt.format.width ||
1171 src_fmt.format.height != sink_fmt.format.height ||
1172 src_fmt.format.code != sink_fmt.format.code)
1173 return -EPIPE;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001174
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001175 if (sd == p->subdevs[IDX_SENSOR] &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001176 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1177 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001178 struct fimc_frame *frame = &vc->ctx->d_frame;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001179 unsigned int i;
1180
1181 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1182 frame->fmt->memplanes,
1183 false);
1184 if (ret < 0)
1185 return -EPIPE;
1186
1187 for (i = 0; i < frame->fmt->memplanes; i++)
1188 if (frame->payload[i] < plane_fmt[i].sizeimage)
1189 return -EPIPE;
1190 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001191 }
1192 return 0;
1193}
1194
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001195static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001196 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001197{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001198 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001199 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001200 struct media_entity *entity = &vc->ve.vdev.entity;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001201 struct fimc_source_info *si = NULL;
1202 struct v4l2_subdev *sd;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001203 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001204
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001205 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001206 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001207
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001208 ret = media_entity_pipeline_start(entity, &vc->ve.pipe->mp);
Sakari Ailusa60a2952012-05-25 07:08:05 -03001209 if (ret < 0)
1210 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001211
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001212 sd = __fimc_md_get_subdev(vc->ve.pipe, IDX_SENSOR);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001213 if (sd)
1214 si = v4l2_get_subdev_hostdata(sd);
1215
1216 if (si == NULL) {
1217 ret = -EPIPE;
1218 goto err_p_stop;
1219 }
1220 /*
1221 * Save configuration data related to currently attached image
1222 * sensor or other data source, e.g. FIMC-IS.
1223 */
1224 vc->source_config = *si;
1225
1226 if (vc->input == GRP_ID_FIMC_IS)
1227 vc->source_config.fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
1228
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001229 if (vc->user_subdev_api) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001230 ret = fimc_pipeline_validate(fimc);
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001231 if (ret < 0)
1232 goto err_p_stop;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001233 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001234
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001235 ret = vb2_ioctl_streamon(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001236 if (!ret) {
1237 vc->streaming = true;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001238 return ret;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001239 }
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001240
1241err_p_stop:
1242 media_entity_pipeline_stop(entity);
1243 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001244}
1245
1246static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001247 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001248{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001249 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001250 struct fimc_vid_cap *vc = &fimc->vid_cap;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001251 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001252
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001253 ret = vb2_ioctl_streamoff(file, priv, type);
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001254 if (ret < 0)
1255 return ret;
Sylwester Nawrocki95c4a172013-03-20 10:35:43 -03001256
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001257 media_entity_pipeline_stop(&vc->ve.vdev.entity);
1258 vc->streaming = false;
Sylwester Nawrocki9ea89e22013-03-25 16:50:50 -03001259 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001260}
1261
1262static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001263 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001264{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001265 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001266 int ret;
1267
1268 ret = vb2_ioctl_reqbufs(file, priv, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001269
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001270 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001271 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001272
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001273 return ret;
1274}
1275
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001276static int fimc_cap_g_selection(struct file *file, void *fh,
1277 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001278{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001279 struct fimc_dev *fimc = video_drvdata(file);
1280 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001281 struct fimc_frame *f = &ctx->s_frame;
1282
1283 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1284 return -EINVAL;
1285
1286 switch (s->target) {
1287 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1288 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1289 f = &ctx->d_frame;
1290 case V4L2_SEL_TGT_CROP_BOUNDS:
1291 case V4L2_SEL_TGT_CROP_DEFAULT:
1292 s->r.left = 0;
1293 s->r.top = 0;
1294 s->r.width = f->o_width;
1295 s->r.height = f->o_height;
1296 return 0;
1297
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001298 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001299 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001300 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001301 s->r.left = f->offs_h;
1302 s->r.top = f->offs_v;
1303 s->r.width = f->width;
1304 s->r.height = f->height;
1305 return 0;
1306 }
1307
1308 return -EINVAL;
1309}
1310
1311/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
Sachin Kamat7e566be2012-05-26 11:11:54 -03001312static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001313{
1314 if (a->left < b->left || a->top < b->top)
1315 return 0;
1316 if (a->left + a->width > b->left + b->width)
1317 return 0;
1318 if (a->top + a->height > b->top + b->height)
1319 return 0;
1320
1321 return 1;
1322}
1323
1324static int fimc_cap_s_selection(struct file *file, void *fh,
1325 struct v4l2_selection *s)
1326{
1327 struct fimc_dev *fimc = video_drvdata(file);
1328 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1329 struct v4l2_rect rect = s->r;
1330 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001331 unsigned long flags;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001332
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001333 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1334 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001335
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001336 if (s->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001337 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001338 else if (s->target == V4L2_SEL_TGT_CROP)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001339 f = &ctx->s_frame;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001340 else
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001341 return -EINVAL;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001342
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001343 fimc_capture_try_selection(ctx, &rect, s->target);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001344
1345 if (s->flags & V4L2_SEL_FLAG_LE &&
1346 !enclosed_rectangle(&rect, &s->r))
1347 return -ERANGE;
1348
1349 if (s->flags & V4L2_SEL_FLAG_GE &&
1350 !enclosed_rectangle(&s->r, &rect))
1351 return -ERANGE;
1352
1353 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001354 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001355 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1356 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001357 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001358
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001359 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001360 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001361}
1362
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001363static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
Sylwester Nawrockiaceb59e2013-04-23 11:36:04 -03001364 .vidioc_querycap = fimc_cap_querycap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001365
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001366 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001367 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001368 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001369 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001370
1371 .vidioc_reqbufs = fimc_cap_reqbufs,
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001372 .vidioc_querybuf = vb2_ioctl_querybuf,
1373 .vidioc_qbuf = vb2_ioctl_qbuf,
1374 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1375 .vidioc_expbuf = vb2_ioctl_expbuf,
1376 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1377 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001378
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001379 .vidioc_streamon = fimc_cap_streamon,
1380 .vidioc_streamoff = fimc_cap_streamoff,
1381
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001382 .vidioc_g_selection = fimc_cap_g_selection,
1383 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001384
1385 .vidioc_enum_input = fimc_cap_enum_input,
1386 .vidioc_s_input = fimc_cap_s_input,
1387 .vidioc_g_input = fimc_cap_g_input,
1388};
1389
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001390/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001391static int fimc_link_setup(struct media_entity *entity,
1392 const struct media_pad *local,
1393 const struct media_pad *remote, u32 flags)
1394{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001395 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1396 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001397 struct fimc_vid_cap *vc = &fimc->vid_cap;
1398 struct v4l2_subdev *sensor;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001399
1400 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1401 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001402
1403 if (WARN_ON(fimc == NULL))
1404 return 0;
1405
1406 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1407 local->entity->name, remote->entity->name, flags,
1408 fimc->vid_cap.input);
1409
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001410 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
1411 fimc->vid_cap.input = 0;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001412 return 0;
1413 }
1414
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001415 if (vc->input != 0)
1416 return -EBUSY;
1417
1418 vc->input = sd->grp_id;
1419
1420 if (vc->user_subdev_api || vc->inh_sensor_ctrls)
1421 return 0;
1422
1423 /* Inherit V4L2 controls from the image sensor subdev. */
1424 sensor = fimc_find_remote_sensor(&vc->subdev.entity);
1425 if (sensor == NULL)
1426 return 0;
1427
1428 return v4l2_ctrl_add_handler(&vc->ctx->ctrls.handler,
1429 sensor->ctrl_handler, NULL);
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001430}
1431
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001432static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001433 .link_setup = fimc_link_setup,
1434};
1435
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001436/**
1437 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1438 * @sd: pointer to a subdev generating the notification
1439 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1440 * @arg: pointer to an u32 type integer that stores the frame payload value
1441 *
1442 * The End Of Frame notification sent by sensor subdev in its still capture
1443 * mode. If there is only a single VSYNC generated by the sensor at the
1444 * beginning of a frame transmission, FIMC does not issue the LastIrq
1445 * (end of frame) interrupt. And this notification is used to complete the
1446 * frame capture and returning a buffer to user-space. Subdev drivers should
1447 * call this notification from their last 'End of frame capture' interrupt.
1448 */
1449void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1450 void *arg)
1451{
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001452 struct fimc_source_info *si;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001453 struct fimc_vid_buffer *buf;
1454 struct fimc_md *fmd;
1455 struct fimc_dev *fimc;
1456 unsigned long flags;
1457
1458 if (sd == NULL)
1459 return;
1460
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001461 si = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001462 fmd = entity_to_fimc_mdev(&sd->entity);
1463
1464 spin_lock_irqsave(&fmd->slock, flags);
Sylwester Nawrocki4c8f0622013-04-09 11:11:58 -03001465
1466 fimc = si ? source_to_sensor_info(si)->host : NULL;
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001467
1468 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1469 test_bit(ST_CAPT_PEND, &fimc->state)) {
1470 unsigned long irq_flags;
1471 spin_lock_irqsave(&fimc->slock, irq_flags);
1472 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1473 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1474 struct fimc_vid_buffer, list);
1475 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1476 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001477 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001478 fimc_deactivate_capture(fimc);
1479 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1480 }
1481 spin_unlock_irqrestore(&fmd->slock, flags);
1482}
1483
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001484static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1485 struct v4l2_subdev_fh *fh,
1486 struct v4l2_subdev_mbus_code_enum *code)
1487{
1488 struct fimc_fmt *fmt;
1489
1490 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1491 if (!fmt)
1492 return -EINVAL;
1493 code->code = fmt->mbus_code;
1494 return 0;
1495}
1496
1497static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1498 struct v4l2_subdev_fh *fh,
1499 struct v4l2_subdev_format *fmt)
1500{
1501 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1502 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001503 struct fimc_frame *ff = &ctx->s_frame;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001504 struct v4l2_mbus_framefmt *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001505
1506 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1507 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1508 fmt->format = *mf;
1509 return 0;
1510 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001511
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001512 mf = &fmt->format;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001513 mutex_lock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001514
1515 switch (fmt->pad) {
1516 case FIMC_SD_PAD_SOURCE:
1517 if (!WARN_ON(ff->fmt == NULL))
1518 mf->code = ff->fmt->mbus_code;
1519 /* Sink pads crop rectangle size */
1520 mf->width = ff->width;
1521 mf->height = ff->height;
1522 break;
1523 case FIMC_SD_PAD_SINK_FIFO:
1524 *mf = fimc->vid_cap.wb_fmt;
1525 break;
1526 case FIMC_SD_PAD_SINK_CAM:
1527 default:
1528 *mf = fimc->vid_cap.ci_fmt;
1529 break;
1530 }
1531
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001532 mutex_unlock(&fimc->lock);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001533 mf->colorspace = V4L2_COLORSPACE_JPEG;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001534
1535 return 0;
1536}
1537
1538static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1539 struct v4l2_subdev_fh *fh,
1540 struct v4l2_subdev_format *fmt)
1541{
1542 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1543 struct v4l2_mbus_framefmt *mf = &fmt->format;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001544 struct fimc_vid_cap *vc = &fimc->vid_cap;
1545 struct fimc_ctx *ctx = vc->ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001546 struct fimc_frame *ff;
1547 struct fimc_fmt *ffmt;
1548
1549 dbg("pad%d: code: 0x%x, %dx%d",
1550 fmt->pad, mf->code, mf->width, mf->height);
1551
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001552 if (fmt->pad == FIMC_SD_PAD_SOURCE && vb2_is_busy(&vc->vbq))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001553 return -EBUSY;
1554
1555 mutex_lock(&fimc->lock);
1556 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1557 &mf->code, NULL, fmt->pad);
1558 mutex_unlock(&fimc->lock);
1559 mf->colorspace = V4L2_COLORSPACE_JPEG;
1560
1561 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1562 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1563 *mf = fmt->format;
1564 return 0;
1565 }
Sylwester Nawrocki66125452012-12-05 13:52:00 -03001566 /* There must be a bug in the driver if this happens */
1567 if (WARN_ON(ffmt == NULL))
1568 return -EINVAL;
1569
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001570 /* Update RGB Alpha control state and value range */
1571 fimc_alpha_ctrl_update(ctx);
1572
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001573 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001574 if (fmt->pad == FIMC_SD_PAD_SOURCE) {
1575 ff = &ctx->d_frame;
1576 /* Sink pads crop rectangle size */
1577 mf->width = ctx->s_frame.width;
1578 mf->height = ctx->s_frame.height;
1579 } else {
1580 ff = &ctx->s_frame;
1581 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001582
1583 mutex_lock(&fimc->lock);
1584 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001585
1586 if (fmt->pad == FIMC_SD_PAD_SINK_FIFO)
1587 vc->wb_fmt = *mf;
1588 else if (fmt->pad == FIMC_SD_PAD_SINK_CAM)
1589 vc->ci_fmt = *mf;
1590
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001591 ff->fmt = ffmt;
1592
1593 /* Reset the crop rectangle if required. */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001594 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001595 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1596
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001597 if (fmt->pad != FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001598 ctx->state &= ~FIMC_COMPOSE;
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001599
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001600 mutex_unlock(&fimc->lock);
1601 return 0;
1602}
1603
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001604static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1605 struct v4l2_subdev_fh *fh,
1606 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001607{
1608 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1609 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001610 struct fimc_frame *f = &ctx->s_frame;
1611 struct v4l2_rect *r = &sel->r;
1612 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001613
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001614 if (sel->pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001615 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001616
1617 mutex_lock(&fimc->lock);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001618
1619 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001620 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001621 f = &ctx->d_frame;
Sakari Ailus5689b282012-05-18 09:31:18 -03001622 case V4L2_SEL_TGT_CROP_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001623 r->width = f->o_width;
1624 r->height = f->o_height;
1625 r->left = 0;
1626 r->top = 0;
1627 mutex_unlock(&fimc->lock);
1628 return 0;
1629
Sakari Ailus5689b282012-05-18 09:31:18 -03001630 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001631 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1632 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001633 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001634 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1635 f = &ctx->d_frame;
1636 break;
1637 default:
1638 mutex_unlock(&fimc->lock);
1639 return -EINVAL;
1640 }
1641
1642 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1643 sel->r = *try_sel;
1644 } else {
1645 r->left = f->offs_h;
1646 r->top = f->offs_v;
1647 r->width = f->width;
1648 r->height = f->height;
1649 }
1650
1651 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1652 sel->pad, r->left, r->top, r->width, r->height,
1653 f->f_width, f->f_height);
1654
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001655 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001656 return 0;
1657}
1658
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001659static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1660 struct v4l2_subdev_fh *fh,
1661 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001662{
1663 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1664 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001665 struct fimc_frame *f = &ctx->s_frame;
1666 struct v4l2_rect *r = &sel->r;
1667 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001668 unsigned long flags;
1669
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001670 if (sel->pad == FIMC_SD_PAD_SOURCE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001671 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001672
1673 mutex_lock(&fimc->lock);
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001674 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001675
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001676 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001677 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001678 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1679 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001680 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001681 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1682 f = &ctx->d_frame;
1683 break;
1684 default:
1685 mutex_unlock(&fimc->lock);
1686 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001687 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001688
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001689 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1690 *try_sel = sel->r;
1691 } else {
1692 spin_lock_irqsave(&fimc->slock, flags);
1693 set_frame_crop(f, r->left, r->top, r->width, r->height);
1694 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sakari Ailus5689b282012-05-18 09:31:18 -03001695 if (sel->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001696 ctx->state |= FIMC_COMPOSE;
Sylwester Nawrocki8b164102013-01-30 09:55:46 -03001697 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001698 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001699
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001700 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001701 r->width, r->height);
1702
1703 mutex_unlock(&fimc->lock);
1704 return 0;
1705}
1706
1707static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1708 .enum_mbus_code = fimc_subdev_enum_mbus_code,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001709 .get_selection = fimc_subdev_get_selection,
1710 .set_selection = fimc_subdev_set_selection,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001711 .get_fmt = fimc_subdev_get_fmt,
1712 .set_fmt = fimc_subdev_set_fmt,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001713};
1714
1715static struct v4l2_subdev_ops fimc_subdev_ops = {
1716 .pad = &fimc_subdev_pad_ops,
1717};
1718
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001719/* Set default format at the sensor and host interface */
1720static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1721{
1722 struct v4l2_format fmt = {
1723 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1724 .fmt.pix_mp = {
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001725 .width = FIMC_DEFAULT_WIDTH,
1726 .height = FIMC_DEFAULT_HEIGHT,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001727 .pixelformat = V4L2_PIX_FMT_YUYV,
1728 .field = V4L2_FIELD_NONE,
1729 .colorspace = V4L2_COLORSPACE_JPEG,
1730 },
1731 };
1732
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001733 return __fimc_capture_set_format(fimc, &fmt);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001734}
1735
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001736/* fimc->lock must be already initialized */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001737static int fimc_register_capture_device(struct fimc_dev *fimc,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001738 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001739{
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001740 struct video_device *vfd = &fimc->vid_cap.ve.vdev;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001741 struct vb2_queue *q = &fimc->vid_cap.vbq;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001742 struct fimc_ctx *ctx;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001743 struct fimc_vid_cap *vid_cap;
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001744 struct fimc_fmt *fmt;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001745 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001746
Sachin Kamat26ee7f42012-08-17 03:28:27 -03001747 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001748 if (!ctx)
1749 return -ENOMEM;
1750
1751 ctx->fimc_dev = fimc;
Sylwester Nawrocki3d112d92012-04-26 06:26:29 -03001752 ctx->in_path = FIMC_IO_CAMERA;
1753 ctx->out_path = FIMC_IO_DMA;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001754 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001755 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001756 ctx->d_frame.fmt = ctx->s_frame.fmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001757
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001758 memset(vfd, 0, sizeof(*vfd));
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001759 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001760
1761 vfd->fops = &fimc_capture_fops;
1762 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001763 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001764 vfd->minor = -1;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001765 vfd->release = video_device_release_empty;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001766 vfd->queue = q;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001767 vfd->lock = &fimc->lock;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -03001768
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001769 video_set_drvdata(vfd, fimc);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001770 vid_cap = &fimc->vid_cap;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001771 vid_cap->active_buf_cnt = 0;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001772 vid_cap->reqbufs_count = 0;
1773 vid_cap->ctx = ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001774
1775 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1776 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001777
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001778 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001779 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Tomasz Stanislawski9bd09fd2012-06-14 10:37:49 -03001780 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001781 q->drv_priv = ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001782 q->ops = &fimc_capture_qops;
1783 q->mem_ops = &vb2_dma_contig_memops;
1784 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
Kamil Debski6aa69f92013-01-25 06:29:57 -03001785 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Sylwester Nawrockic4449142013-03-25 16:36:35 -03001786 q->lock = &fimc->lock;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001787
Sylwester Nawrocki41fd0872012-10-19 08:24:18 -03001788 ret = vb2_queue_init(q);
1789 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001790 goto err_free_ctx;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001791
Sylwester Nawrocki3ad86242013-06-18 14:56:11 -03001792 /* Default format configuration */
1793 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1794 vid_cap->ci_fmt.width = FIMC_DEFAULT_WIDTH;
1795 vid_cap->ci_fmt.height = FIMC_DEFAULT_HEIGHT;
1796 vid_cap->ci_fmt.code = fmt->mbus_code;
1797
1798 ctx->s_frame.width = FIMC_DEFAULT_WIDTH;
1799 ctx->s_frame.height = FIMC_DEFAULT_HEIGHT;
1800 ctx->s_frame.fmt = fmt;
1801
1802 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_WRITEBACK, 0);
1803 vid_cap->wb_fmt = vid_cap->ci_fmt;
1804 vid_cap->wb_fmt.code = fmt->mbus_code;
1805
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001806 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1807 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001808 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001809 goto err_free_ctx;
1810
1811 ret = fimc_ctrls_create(ctx);
1812 if (ret)
1813 goto err_me_cleanup;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001814
1815 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001816 if (ret)
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001817 goto err_ctrl_free;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001818
1819 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1820 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001821
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -03001822 vfd->ctrl_handler = &ctx->ctrls.handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001823 return 0;
1824
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001825err_ctrl_free:
1826 fimc_ctrls_delete(ctx);
1827err_me_cleanup:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001828 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001829err_free_ctx:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001830 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001831 return ret;
1832}
1833
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001834static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001835{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001836 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1837 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001838
Sylwester Nawrockibbc52962012-07-26 07:59:11 -03001839 if (fimc == NULL)
1840 return -ENXIO;
1841
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001842 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1843 if (ret)
1844 return ret;
1845
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001846 fimc->vid_cap.ve.pipe = v4l2_get_subdev_hostdata(sd);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001847
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001848 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001849 if (ret) {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001850 fimc_unregister_m2m_device(fimc);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001851 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001852 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001853
1854 return ret;
1855}
1856
1857static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1858{
1859 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001860 struct video_device *vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001861
1862 if (fimc == NULL)
1863 return;
1864
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001865 mutex_lock(&fimc->lock);
1866
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001867 fimc_unregister_m2m_device(fimc);
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001868 vdev = &fimc->vid_cap.ve.vdev;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001869
Sylwester Nawrockibc7584b2013-05-31 11:37:18 -03001870 if (video_is_registered(vdev)) {
1871 video_unregister_device(vdev);
1872 media_entity_cleanup(&vdev->entity);
Sylwester Nawrocki44031062013-05-31 11:37:19 -03001873 fimc_ctrls_delete(fimc->vid_cap.ctx);
Sylwester Nawrocki403dfbe2013-05-31 11:37:22 -03001874 fimc->vid_cap.ve.pipe = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001875 }
1876 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001877 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki26d63d12013-05-31 11:37:25 -03001878
1879 mutex_unlock(&fimc->lock);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001880}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001881
1882static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1883 .registered = fimc_capture_subdev_registered,
1884 .unregistered = fimc_capture_subdev_unregistered,
1885};
1886
1887int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1888{
1889 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1890 int ret;
1891
1892 v4l2_subdev_init(sd, &fimc_subdev_ops);
Sylwester Nawrocki5a665612013-04-18 08:49:54 -03001893 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
Sylwester Nawrockie80cb1f2013-03-26 08:22:21 -03001894 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->id);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001895
Sylwester Nawrocki88fa8312013-03-20 10:44:39 -03001896 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_CAM].flags = MEDIA_PAD_FL_SINK;
1897 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK_FIFO].flags = MEDIA_PAD_FL_SINK;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001898 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1899 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1900 fimc->vid_cap.sd_pads, 0);
1901 if (ret)
1902 return ret;
1903
1904 sd->entity.ops = &fimc_sd_media_ops;
1905 sd->internal_ops = &fimc_capture_sd_internal_ops;
1906 v4l2_set_subdevdata(sd, fimc);
1907 return 0;
1908}
1909
1910void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1911{
1912 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1913
1914 v4l2_device_unregister_subdev(sd);
1915 media_entity_cleanup(&sd->entity);
1916 v4l2_set_subdevdata(sd, NULL);
1917}