blob: 62ce5399c4cf27398df74af19b2aff1cb2fcf5f8 [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 Nawrocki131b6c62011-08-24 19:25:10 -030030#include "fimc-mdevice.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 Nawrocki5f3cc442010-10-07 10:06:16 -030033
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030034static int fimc_capture_hw_init(struct fimc_dev *fimc)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030035{
36 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030037 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030038 struct fimc_sensor_info *sensor;
39 unsigned long flags;
40 int ret = 0;
41
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030042 if (p->subdevs[IDX_SENSOR] == NULL || ctx == NULL)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030043 return -ENXIO;
44 if (ctx->s_frame.fmt == NULL)
45 return -EINVAL;
46
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030047 sensor = v4l2_get_subdev_hostdata(p->subdevs[IDX_SENSOR]);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030048
49 spin_lock_irqsave(&fimc->slock, flags);
50 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
51 fimc_set_yuv_order(ctx);
52
53 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
54 fimc_hw_set_camera_type(fimc, sensor->pdata);
55 fimc_hw_set_camera_source(fimc, sensor->pdata);
56 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
57
58 ret = fimc_set_scaler_info(ctx);
59 if (!ret) {
60 fimc_hw_set_input_path(ctx);
61 fimc_hw_set_prescaler(ctx);
62 fimc_hw_set_mainscaler(ctx);
63 fimc_hw_set_target_format(ctx);
64 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -030065 fimc_hw_set_effect(ctx);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030066 fimc_hw_set_output_path(ctx);
67 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -030068 if (fimc->variant->has_alpha)
69 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -030070 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030071 }
72 spin_unlock_irqrestore(&fimc->slock, flags);
73 return ret;
74}
75
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030076/*
77 * Reinitialize the driver so it is ready to start the streaming again.
78 * Set fimc->state to indicate stream off and the hardware shut down state.
79 * If not suspending (@suspend is false), return any buffers to videobuf2.
80 * Otherwise put any owned buffers onto the pending buffers queue, so they
81 * can be re-spun when the device is being resumed. Also perform FIMC
82 * software reset and disable streaming on the whole pipeline if required.
83 */
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030084static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030085{
Marek Szyprowskibd323e22011-08-29 08:51:49 -030086 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030087 struct fimc_vid_buffer *buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -030088 unsigned long flags;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030089 bool streaming;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030090
91 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030092 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030093
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030094 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
95 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrockiaa3331222012-03-16 11:47:51 -030096 if (suspend)
97 fimc->state |= (1 << ST_CAPT_SUSPENDED);
98 else
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030099 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300100
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300101 /* Release unused buffers */
102 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300103 buf = fimc_pending_queue_pop(cap);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300104 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
105 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300106 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300107 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300108 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300109 if (suspend)
110 fimc_pending_queue_add(cap, buf);
111 else
112 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300113 }
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300114
115 fimc_hw_reset(fimc);
116 cap->buf_index = 0;
117
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300118 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300119
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300120 if (streaming)
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300121 return fimc_pipeline_s_stream(&fimc->pipeline, 0);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300122 else
123 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300124}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300125
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300126static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300127{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300128 unsigned long flags;
129
130 if (!fimc_capture_active(fimc))
131 return 0;
132
133 spin_lock_irqsave(&fimc->slock, flags);
134 set_bit(ST_CAPT_SHUT, &fimc->state);
135 fimc_deactivate_capture(fimc);
136 spin_unlock_irqrestore(&fimc->slock, flags);
137
138 wait_event_timeout(fimc->irq_queue,
139 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300140 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300141
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300142 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300143}
144
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300145/**
146 * fimc_capture_config_update - apply the camera interface configuration
147 *
148 * To be called from within the interrupt handler with fimc.slock
149 * spinlock held. It updates the camera pixel crop, rotation and
150 * image flip in H/W.
151 */
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300152static int fimc_capture_config_update(struct fimc_ctx *ctx)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300153{
154 struct fimc_dev *fimc = ctx->fimc_dev;
155 int ret;
156
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300157 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300158
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300159 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300160 if (ret)
161 return ret;
162
163 fimc_hw_set_prescaler(ctx);
164 fimc_hw_set_mainscaler(ctx);
165 fimc_hw_set_target_format(ctx);
166 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300167 fimc_hw_set_effect(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300168 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
169 fimc_hw_set_out_dma(ctx);
170 if (fimc->variant->has_alpha)
171 fimc_hw_set_rgb_alpha(ctx);
172
173 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300174 return ret;
175}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300176
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300177void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
178{
179 struct fimc_vid_cap *cap = &fimc->vid_cap;
180 struct fimc_vid_buffer *v_buf;
181 struct timeval *tv;
182 struct timespec ts;
183
184 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
185 wake_up(&fimc->irq_queue);
186 goto done;
187 }
188
189 if (!list_empty(&cap->active_buf_q) &&
190 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
191 ktime_get_real_ts(&ts);
192
193 v_buf = fimc_active_queue_pop(cap);
194
195 tv = &v_buf->vb.v4l2_buf.timestamp;
196 tv->tv_sec = ts.tv_sec;
197 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
198 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
199
200 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
201 }
202
203 if (!list_empty(&cap->pending_buf_q)) {
204
205 v_buf = fimc_pending_queue_pop(cap);
206 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
207 v_buf->index = cap->buf_index;
208
209 /* Move the buffer to the capture active queue */
210 fimc_active_queue_add(cap, v_buf);
211
212 dbg("next frame: %d, done frame: %d",
213 fimc_hw_get_frame_index(fimc), v_buf->index);
214
215 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
216 cap->buf_index = 0;
217 }
218
219 if (cap->active_buf_cnt == 0) {
220 if (deq_buf)
221 clear_bit(ST_CAPT_RUN, &fimc->state);
222
223 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
224 cap->buf_index = 0;
225 } else {
226 set_bit(ST_CAPT_RUN, &fimc->state);
227 }
228
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300229 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
230 fimc_capture_config_update(cap->ctx);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300231done:
232 if (cap->active_buf_cnt == 1) {
233 fimc_deactivate_capture(fimc);
234 clear_bit(ST_CAPT_STREAM, &fimc->state);
235 }
236
237 dbg("frame: %d, active_buf_cnt: %d",
238 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
239}
240
241
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300242static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300243{
244 struct fimc_ctx *ctx = q->drv_priv;
245 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300246 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300247 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300248 int ret;
249
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300250 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300251
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300252 ret = fimc_capture_hw_init(fimc);
253 if (ret) {
254 fimc_capture_state_cleanup(fimc, false);
255 return ret;
256 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300257
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300258 set_bit(ST_CAPT_PEND, &fimc->state);
259
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300260 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
261
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300262 if (vid_cap->active_buf_cnt >= min_bufs &&
263 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300264 fimc_activate_capture(ctx);
265
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300266 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300267 fimc_pipeline_s_stream(&fimc->pipeline, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300268 }
269
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300270 return 0;
271}
272
273static int stop_streaming(struct vb2_queue *q)
274{
275 struct fimc_ctx *ctx = q->drv_priv;
276 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300277
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300278 if (!fimc_capture_active(fimc))
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300279 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300280
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300281 return fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300282}
283
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300284int fimc_capture_suspend(struct fimc_dev *fimc)
285{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300286 bool suspend = fimc_capture_busy(fimc);
287
288 int ret = fimc_stop_capture(fimc, suspend);
289 if (ret)
290 return ret;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300291 return fimc_pipeline_shutdown(&fimc->pipeline);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300292}
293
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300294static void buffer_queue(struct vb2_buffer *vb);
295
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300296int fimc_capture_resume(struct fimc_dev *fimc)
297{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300298 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
299 struct fimc_vid_buffer *buf;
300 int i;
301
302 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
303 return 0;
304
305 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
306 vid_cap->buf_index = 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300307 fimc_pipeline_initialize(&fimc->pipeline, &vid_cap->vfd->entity,
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300308 false);
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300309 fimc_capture_hw_init(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300310
311 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
312
313 for (i = 0; i < vid_cap->reqbufs_count; i++) {
314 if (list_empty(&vid_cap->pending_buf_q))
315 break;
316 buf = fimc_pending_queue_pop(vid_cap);
317 buffer_queue(&buf->vb);
318 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300319 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300320
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300321}
322
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300323static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300324 unsigned int *num_buffers, unsigned int *num_planes,
325 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300326{
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300327 const struct v4l2_pix_format_mplane *pixm = NULL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300328 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300329 struct fimc_frame *frame = &ctx->d_frame;
330 struct fimc_fmt *fmt = frame->fmt;
331 unsigned long wh;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300332 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300333
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300334 if (pfmt) {
335 pixm = &pfmt->fmt.pix_mp;
336 fmt = fimc_find_format(&pixm->pixelformat, NULL,
337 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
338 wh = pixm->width * pixm->height;
339 } else {
340 wh = frame->f_width * frame->f_height;
341 }
342
343 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300344 return -EINVAL;
345
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300346 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300347
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300348 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300349 unsigned int size = (wh * fmt->depth[i]) / 8;
350 if (pixm)
351 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
352 else
353 sizes[i] = size;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300354 allocators[i] = ctx->fimc_dev->alloc_ctx;
355 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300356
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300357 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300358}
359
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300360static int buffer_prepare(struct vb2_buffer *vb)
361{
362 struct vb2_queue *vq = vb->vb2_queue;
363 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300364 int i;
365
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300366 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300367 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300368
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300369 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300370 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300371
372 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300373 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
374 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300375 vb2_plane_size(vb, i), size);
376 return -EINVAL;
377 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300378 vb2_set_plane_payload(vb, i, size);
379 }
380
381 return 0;
382}
383
384static void buffer_queue(struct vb2_buffer *vb)
385{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300386 struct fimc_vid_buffer *buf
387 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300388 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
389 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300390 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
391 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300392 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300393
394 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300395 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300396
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300397 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
398 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
399 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300400 /* Setup the buffer directly for processing. */
401 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
402 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300403
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300404 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
405 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300406 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300407
408 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
409 vid_cap->buf_index = 0;
410 } else {
411 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300412 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300413
414 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
415
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300416
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300417 if (vb2_is_streaming(&vid_cap->vbq) &&
418 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300419 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300420 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300421 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300422
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300423 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300424 fimc_pipeline_s_stream(&fimc->pipeline, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300425 return;
426 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300427 spin_unlock_irqrestore(&fimc->slock, flags);
428}
429
430static void fimc_lock(struct vb2_queue *vq)
431{
432 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
433 mutex_lock(&ctx->fimc_dev->lock);
434}
435
436static void fimc_unlock(struct vb2_queue *vq)
437{
438 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
439 mutex_unlock(&ctx->fimc_dev->lock);
440}
441
442static struct vb2_ops fimc_capture_qops = {
443 .queue_setup = queue_setup,
444 .buf_prepare = buffer_prepare,
445 .buf_queue = buffer_queue,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300446 .wait_prepare = fimc_unlock,
447 .wait_finish = fimc_lock,
448 .start_streaming = start_streaming,
449 .stop_streaming = stop_streaming,
450};
451
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300452/**
453 * fimc_capture_ctrls_create - initialize the control handler
454 * Initialize the capture video node control handler and fill it
455 * with the FIMC controls. Inherit any sensor's controls if the
456 * 'user_subdev_api' flag is false (default behaviour).
457 * This function need to be called with the graph mutex held.
458 */
459int fimc_capture_ctrls_create(struct fimc_dev *fimc)
460{
461 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
462 int ret;
463
464 if (WARN_ON(vid_cap->ctx == NULL))
465 return -ENXIO;
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300466 if (vid_cap->ctx->ctrls.ready)
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300467 return 0;
468
469 ret = fimc_ctrls_create(vid_cap->ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300470 if (ret || vid_cap->user_subdev_api || !vid_cap->ctx->ctrls.ready)
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300471 return ret;
472
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300473 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrls.handler,
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300474 fimc->pipeline.subdevs[IDX_SENSOR]->ctrl_handler);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300475}
476
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300477static int fimc_capture_set_default_format(struct fimc_dev *fimc);
478
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300479static int fimc_capture_open(struct file *file)
480{
481 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300482 int ret = v4l2_fh_open(file);
483
484 if (ret)
485 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300486
487 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
488
489 /* Return if the corresponding video mem2mem node is already opened. */
490 if (fimc_m2m_active(fimc))
491 return -EBUSY;
492
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300493 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300494 pm_runtime_get_sync(&fimc->pdev->dev);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300495
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300496 if (++fimc->vid_cap.refcnt == 1) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300497 ret = fimc_pipeline_initialize(&fimc->pipeline,
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300498 &fimc->vid_cap.vfd->entity, true);
499 if (ret < 0) {
500 dev_err(&fimc->pdev->dev,
501 "Video pipeline initialization failed\n");
502 pm_runtime_put_sync(&fimc->pdev->dev);
503 fimc->vid_cap.refcnt--;
504 v4l2_fh_release(file);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300505 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300506 return ret;
507 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300508 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300509
510 if (!ret && !fimc->vid_cap.user_subdev_api)
511 ret = fimc_capture_set_default_format(fimc);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300512 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300513 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300514}
515
516static int fimc_capture_close(struct file *file)
517{
518 struct fimc_dev *fimc = video_drvdata(file);
519
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300520 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
521
522 if (--fimc->vid_cap.refcnt == 0) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300523 clear_bit(ST_CAPT_BUSY, &fimc->state);
524 fimc_stop_capture(fimc, false);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300525 fimc_pipeline_shutdown(&fimc->pipeline);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300526 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300527 }
528
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300529 pm_runtime_put(&fimc->pdev->dev);
530
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300531 if (fimc->vid_cap.refcnt == 0) {
532 vb2_queue_release(&fimc->vid_cap.vbq);
533 fimc_ctrls_delete(fimc->vid_cap.ctx);
534 }
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300535 return v4l2_fh_release(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300536}
537
538static unsigned int fimc_capture_poll(struct file *file,
539 struct poll_table_struct *wait)
540{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300541 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300542
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300543 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300544}
545
546static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
547{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300548 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300549
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300550 return vb2_mmap(&fimc->vid_cap.vbq, vma);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300551}
552
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300553static const struct v4l2_file_operations fimc_capture_fops = {
554 .owner = THIS_MODULE,
555 .open = fimc_capture_open,
556 .release = fimc_capture_close,
557 .poll = fimc_capture_poll,
558 .unlocked_ioctl = video_ioctl2,
559 .mmap = fimc_capture_mmap,
560};
561
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300562/*
563 * Format and crop negotiation helpers
564 */
565
566static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
567 u32 *width, u32 *height,
568 u32 *code, u32 *fourcc, int pad)
569{
570 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
571 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300572 struct fimc_variant *var = fimc->variant;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300573 struct fimc_pix_limit *pl = var->pix_limit;
574 struct fimc_frame *dst = &ctx->d_frame;
575 u32 depth, min_w, max_w, min_h, align_h = 3;
576 u32 mask = FMT_FLAGS_CAM;
577 struct fimc_fmt *ffmt;
578
579 /* Color conversion from/to JPEG is not supported */
580 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
581 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
582 *code = V4L2_MBUS_FMT_JPEG_1X8;
583
584 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
585 mask |= FMT_FLAGS_M2M;
586
587 ffmt = fimc_find_format(fourcc, code, mask, 0);
588 if (WARN_ON(!ffmt))
589 return NULL;
590 if (code)
591 *code = ffmt->mbus_code;
592 if (fourcc)
593 *fourcc = ffmt->fourcc;
594
595 if (pad == FIMC_SD_PAD_SINK) {
596 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
597 pl->scaler_dis_w : pl->scaler_en_w;
598 /* Apply the camera input interface pixel constraints */
599 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
600 height, max_t(u32, *height, 32),
601 FIMC_CAMIF_MAX_HEIGHT,
602 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
603 0);
604 return ffmt;
605 }
606 /* Can't scale or crop in transparent (JPEG) transfer mode */
607 if (fimc_fmt_is_jpeg(ffmt->color)) {
608 *width = ctx->s_frame.f_width;
609 *height = ctx->s_frame.f_height;
610 return ffmt;
611 }
612 /* Apply the scaler and the output DMA constraints */
613 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300614 if (ctx->state & FIMC_COMPOSE) {
615 min_w = dst->offs_h + dst->width;
616 min_h = dst->offs_v + dst->height;
617 } else {
618 min_w = var->min_out_pixsize;
619 min_h = var->min_out_pixsize;
620 }
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300621 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300622 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
623
624 depth = fimc_get_format_depth(ffmt);
625 v4l_bound_align_image(width, min_w, max_w,
626 ffs(var->min_out_pixsize) - 1,
627 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
628 align_h,
629 64/(ALIGN(depth, 8)));
630
631 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
632 pad, code ? *code : 0, *width, *height,
633 dst->f_width, dst->f_height);
634
635 return ffmt;
636}
637
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300638static void fimc_capture_try_selection(struct fimc_ctx *ctx,
639 struct v4l2_rect *r,
640 int target)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300641{
642 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
643 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300644 struct fimc_variant *var = fimc->variant;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300645 struct fimc_pix_limit *pl = var->pix_limit;
646 struct fimc_frame *sink = &ctx->s_frame;
647 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
648 u32 align_sz = 0, align_h = 4;
649 u32 max_sc_h, max_sc_v;
650
651 /* In JPEG transparent transfer mode cropping is not supported */
652 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
653 r->width = sink->f_width;
654 r->height = sink->f_height;
655 r->left = r->top = 0;
656 return;
657 }
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300658 if (target == V4L2_SEL_TGT_COMPOSE_ACTIVE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300659 if (ctx->rotation != 90 && ctx->rotation != 270)
660 align_h = 1;
661 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
662 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
663 min_sz = var->min_out_pixsize;
664 } else {
665 u32 depth = fimc_get_format_depth(sink->fmt);
666 align_sz = 64/ALIGN(depth, 8);
667 min_sz = var->min_inp_pixsize;
668 min_w = min_h = min_sz;
669 max_sc_h = max_sc_v = 1;
670 }
671 /*
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300672 * For the compose rectangle the following constraints must be met:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300673 * - it must fit in the sink pad format rectangle (f_width/f_height);
674 * - maximum downscaling ratio is 64;
675 * - maximum crop size depends if the rotator is used or not;
676 * - the sink pad format width/height must be 4 multiple of the
677 * prescaler ratios determined by sink pad size and source pad crop,
678 * the prescaler ratio is returned by fimc_get_scaler_factor().
679 */
680 max_w = min_t(u32,
681 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
682 rotate ? sink->f_height : sink->f_width);
683 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300684
685 if (target == V4L2_SEL_TGT_COMPOSE_ACTIVE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300686 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
687 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
688 if (rotate) {
689 swap(max_sc_h, max_sc_v);
690 swap(min_w, min_h);
691 }
692 }
693 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
694 &r->height, min_h, max_h, align_h,
695 align_sz);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300696 /* Adjust left/top if crop/compose rectangle is out of bounds */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300697 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
698 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
699 r->left = round_down(r->left, var->hor_offs_align);
700
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300701 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
702 target, r->left, r->top, r->width, r->height,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300703 sink->f_width, sink->f_height);
704}
705
706/*
707 * The video node ioctl operations
708 */
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300709static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
710 struct v4l2_capability *cap)
711{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300712 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300713
714 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
715 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
716 cap->bus_info[0] = 0;
Sylwester Nawrocki8f401542011-07-07 06:42:05 -0300717 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300718
719 return 0;
720}
721
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300722static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
723 struct v4l2_fmtdesc *f)
724{
725 struct fimc_fmt *fmt;
726
727 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
728 f->index);
729 if (!fmt)
730 return -EINVAL;
731 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
732 f->pixelformat = fmt->fourcc;
733 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
734 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
735 return 0;
736}
737
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300738/**
739 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
740 * elements
741 * @ctx: FIMC capture context
742 * @tfmt: media bus format to try/set on subdevs
743 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
744 * @set: true to set format on subdevs, false to try only
745 */
746static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
747 struct v4l2_mbus_framefmt *tfmt,
748 struct fimc_fmt **fmt_id,
749 bool set)
750{
751 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300752 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
753 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300754 struct v4l2_subdev_format sfmt;
755 struct v4l2_mbus_framefmt *mf = &sfmt.format;
756 struct fimc_fmt *ffmt = NULL;
757 int ret, i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300758
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300759 if (WARN_ON(!sd || !tfmt))
760 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300761
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300762 memset(&sfmt, 0, sizeof(sfmt));
763 sfmt.format = *tfmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300764
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300765 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
766 while (1) {
767 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
768 FMT_FLAGS_CAM, i++);
769 if (ffmt == NULL) {
770 /*
771 * Notify user-space if common pixel code for
772 * host and sensor does not exist.
773 */
774 return -EINVAL;
775 }
776 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300777
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300778 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
779 if (ret)
780 return ret;
781 if (mf->code != tfmt->code) {
782 mf->code = 0;
783 continue;
784 }
Julia Lawallb1aa6082012-01-12 17:49:28 -0300785 if (mf->width != tfmt->width || mf->height != tfmt->height) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300786 u32 fcc = ffmt->fourcc;
787 tfmt->width = mf->width;
788 tfmt->height = mf->height;
789 ffmt = fimc_capture_try_format(ctx,
790 &tfmt->width, &tfmt->height,
791 NULL, &fcc, FIMC_SD_PAD_SOURCE);
792 if (ffmt && ffmt->mbus_code)
793 mf->code = ffmt->mbus_code;
Julia Lawallb1aa6082012-01-12 17:49:28 -0300794 if (mf->width != tfmt->width ||
795 mf->height != tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300796 continue;
797 tfmt->code = mf->code;
798 }
799 if (csis)
800 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300801
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300802 if (mf->code == tfmt->code &&
Julia Lawallb1aa6082012-01-12 17:49:28 -0300803 mf->width == tfmt->width && mf->height == tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300804 break;
805 }
806
807 if (fmt_id && ffmt)
808 *fmt_id = ffmt;
809 *tfmt = *mf;
810
811 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
812 return 0;
813}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300814
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300815static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
816 struct v4l2_format *f)
817{
818 struct fimc_dev *fimc = video_drvdata(file);
819 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
820
821 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
822 return -EINVAL;
823
824 return fimc_fill_format(&ctx->d_frame, f);
825}
826
827static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
828 struct v4l2_format *f)
829{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300830 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300831 struct fimc_dev *fimc = video_drvdata(file);
832 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300833 struct v4l2_mbus_framefmt mf;
834 struct fimc_fmt *ffmt = NULL;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300835
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300836 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
837 return -EINVAL;
838
839 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
840 fimc_capture_try_format(ctx, &pix->width, &pix->height,
841 NULL, &pix->pixelformat,
842 FIMC_SD_PAD_SINK);
843 ctx->s_frame.f_width = pix->width;
844 ctx->s_frame.f_height = pix->height;
845 }
846 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
847 NULL, &pix->pixelformat,
848 FIMC_SD_PAD_SOURCE);
849 if (!ffmt)
850 return -EINVAL;
851
852 if (!fimc->vid_cap.user_subdev_api) {
853 mf.width = pix->width;
854 mf.height = pix->height;
855 mf.code = ffmt->mbus_code;
856 fimc_md_graph_lock(fimc);
857 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
858 fimc_md_graph_unlock(fimc);
859
860 pix->width = mf.width;
861 pix->height = mf.height;
862 if (ffmt)
863 pix->pixelformat = ffmt->fourcc;
864 }
865
866 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300867 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300868}
869
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300870static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
871{
872 ctx->scaler.enabled = !jpeg;
873 fimc_ctrls_activate(ctx, !jpeg);
874
875 if (jpeg)
876 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
877 else
878 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
879}
880
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300881static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
882{
883 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
884 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
885 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
886 struct fimc_frame *ff = &ctx->d_frame;
887 struct fimc_fmt *s_fmt = NULL;
888 int ret, i;
889
890 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
891 return -EINVAL;
892 if (vb2_is_busy(&fimc->vid_cap.vbq))
893 return -EBUSY;
894
895 /* Pre-configure format at camera interface input, for JPEG only */
896 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
897 fimc_capture_try_format(ctx, &pix->width, &pix->height,
898 NULL, &pix->pixelformat,
899 FIMC_SD_PAD_SINK);
900 ctx->s_frame.f_width = pix->width;
901 ctx->s_frame.f_height = pix->height;
902 }
903 /* Try the format at the scaler and the DMA output */
904 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
905 NULL, &pix->pixelformat,
906 FIMC_SD_PAD_SOURCE);
907 if (!ff->fmt)
908 return -EINVAL;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -0300909
910 /* Update RGB Alpha control state and value range */
911 fimc_alpha_ctrl_update(ctx);
912
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300913 /* Try to match format at the host and the sensor */
914 if (!fimc->vid_cap.user_subdev_api) {
915 mf->code = ff->fmt->mbus_code;
916 mf->width = pix->width;
917 mf->height = pix->height;
918
919 fimc_md_graph_lock(fimc);
920 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
921 fimc_md_graph_unlock(fimc);
922 if (ret)
923 return ret;
924 pix->width = mf->width;
925 pix->height = mf->height;
926 }
927 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
928 for (i = 0; i < ff->fmt->colplanes; i++)
929 ff->payload[i] =
930 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
931
932 set_frame_bounds(ff, pix->width, pix->height);
933 /* Reset the composition rectangle if not yet configured */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300934 if (!(ctx->state & FIMC_COMPOSE))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300935 set_frame_crop(ff, 0, 0, pix->width, pix->height);
936
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300937 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
938
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300939 /* Reset cropping and set format at the camera interface input */
940 if (!fimc->vid_cap.user_subdev_api) {
941 ctx->s_frame.fmt = s_fmt;
942 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
943 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
944 }
945
946 return ret;
947}
948
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300949static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
950 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300951{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300952 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300953
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300954 return fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300955}
956
957static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300958 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300959{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300960 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300961 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300962
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300963 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300964 return -EINVAL;
965
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300966 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300967 if (sd)
968 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300969 return 0;
970}
971
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300972static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300973{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300974 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300975}
976
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300977static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300978{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300979 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300980 return 0;
981}
982
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300983/**
984 * fimc_pipeline_validate - check for formats inconsistencies
985 * between source and sink pad of each link
986 *
987 * Return 0 if all formats match or -EPIPE otherwise.
988 */
989static int fimc_pipeline_validate(struct fimc_dev *fimc)
990{
991 struct v4l2_subdev_format sink_fmt, src_fmt;
992 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
993 struct v4l2_subdev *sd;
994 struct media_pad *pad;
995 int ret;
996
997 /* Start with the video capture node pad */
998 pad = media_entity_remote_source(&vid_cap->vd_pad);
999 if (pad == NULL)
1000 return -EPIPE;
1001 /* FIMC.{N} subdevice */
1002 sd = media_entity_to_v4l2_subdev(pad->entity);
1003
1004 while (1) {
1005 /* Retrieve format at the sink pad */
1006 pad = &sd->entity.pads[0];
1007 if (!(pad->flags & MEDIA_PAD_FL_SINK))
1008 break;
1009 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001010 if (sd == &fimc->vid_cap.subdev) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001011 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
1012 sink_fmt.format.width = ff->f_width;
1013 sink_fmt.format.height = ff->f_height;
1014 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1015 } else {
1016 sink_fmt.pad = pad->index;
1017 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1018 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1019 if (ret < 0 && ret != -ENOIOCTLCMD)
1020 return -EPIPE;
1021 }
1022 /* Retrieve format at the source pad */
1023 pad = media_entity_remote_source(pad);
1024 if (pad == NULL ||
1025 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1026 break;
1027
1028 sd = media_entity_to_v4l2_subdev(pad->entity);
1029 src_fmt.pad = pad->index;
1030 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1031 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1032 if (ret < 0 && ret != -ENOIOCTLCMD)
1033 return -EPIPE;
1034
1035 if (src_fmt.format.width != sink_fmt.format.width ||
1036 src_fmt.format.height != sink_fmt.format.height ||
1037 src_fmt.format.code != sink_fmt.format.code)
1038 return -EPIPE;
1039 }
1040 return 0;
1041}
1042
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001043static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001044 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001045{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001046 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001047 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001048 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001049
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001050 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001051 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001052
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001053 media_entity_pipeline_start(&p->subdevs[IDX_SENSOR]->entity,
1054 p->m_pipeline);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001055
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001056 if (fimc->vid_cap.user_subdev_api) {
1057 ret = fimc_pipeline_validate(fimc);
1058 if (ret)
1059 return ret;
1060 }
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001061 return vb2_streamon(&fimc->vid_cap.vbq, type);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001062}
1063
1064static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001065 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001066{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001067 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001068 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001069 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001070
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001071 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
1072 if (ret == 0)
1073 media_entity_pipeline_stop(&sd->entity);
1074 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001075}
1076
1077static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001078 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001079{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001080 struct fimc_dev *fimc = video_drvdata(file);
1081 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001082
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001083 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001084 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001085 return ret;
1086}
1087
1088static int fimc_cap_querybuf(struct file *file, void *priv,
1089 struct v4l2_buffer *buf)
1090{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001091 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001092
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001093 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001094}
1095
1096static int fimc_cap_qbuf(struct file *file, void *priv,
1097 struct v4l2_buffer *buf)
1098{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001099 struct fimc_dev *fimc = video_drvdata(file);
1100
1101 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001102}
1103
1104static int fimc_cap_dqbuf(struct file *file, void *priv,
1105 struct v4l2_buffer *buf)
1106{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001107 struct fimc_dev *fimc = video_drvdata(file);
1108
1109 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001110}
1111
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001112static int fimc_cap_create_bufs(struct file *file, void *priv,
1113 struct v4l2_create_buffers *create)
1114{
1115 struct fimc_dev *fimc = video_drvdata(file);
1116
1117 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1118}
1119
1120static int fimc_cap_prepare_buf(struct file *file, void *priv,
1121 struct v4l2_buffer *b)
1122{
1123 struct fimc_dev *fimc = video_drvdata(file);
1124
1125 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1126}
1127
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001128static int fimc_cap_g_selection(struct file *file, void *fh,
1129 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001130{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001131 struct fimc_dev *fimc = video_drvdata(file);
1132 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001133 struct fimc_frame *f = &ctx->s_frame;
1134
1135 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1136 return -EINVAL;
1137
1138 switch (s->target) {
1139 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1140 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1141 f = &ctx->d_frame;
1142 case V4L2_SEL_TGT_CROP_BOUNDS:
1143 case V4L2_SEL_TGT_CROP_DEFAULT:
1144 s->r.left = 0;
1145 s->r.top = 0;
1146 s->r.width = f->o_width;
1147 s->r.height = f->o_height;
1148 return 0;
1149
1150 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1151 f = &ctx->d_frame;
1152 case V4L2_SEL_TGT_CROP_ACTIVE:
1153 s->r.left = f->offs_h;
1154 s->r.top = f->offs_v;
1155 s->r.width = f->width;
1156 s->r.height = f->height;
1157 return 0;
1158 }
1159
1160 return -EINVAL;
1161}
1162
1163/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
Sachin Kamat7e566be2012-05-26 11:11:54 -03001164static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001165{
1166 if (a->left < b->left || a->top < b->top)
1167 return 0;
1168 if (a->left + a->width > b->left + b->width)
1169 return 0;
1170 if (a->top + a->height > b->top + b->height)
1171 return 0;
1172
1173 return 1;
1174}
1175
1176static int fimc_cap_s_selection(struct file *file, void *fh,
1177 struct v4l2_selection *s)
1178{
1179 struct fimc_dev *fimc = video_drvdata(file);
1180 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1181 struct v4l2_rect rect = s->r;
1182 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001183 unsigned long flags;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001184
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001185 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1186 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001187
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001188 if (s->target == V4L2_SEL_TGT_COMPOSE_ACTIVE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001189 f = &ctx->d_frame;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001190 else if (s->target == V4L2_SEL_TGT_CROP_ACTIVE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001191 f = &ctx->s_frame;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001192 else
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001193 return -EINVAL;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001194
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001195 fimc_capture_try_selection(ctx, &rect, s->target);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001196
1197 if (s->flags & V4L2_SEL_FLAG_LE &&
1198 !enclosed_rectangle(&rect, &s->r))
1199 return -ERANGE;
1200
1201 if (s->flags & V4L2_SEL_FLAG_GE &&
1202 !enclosed_rectangle(&s->r, &rect))
1203 return -ERANGE;
1204
1205 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001206 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001207 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1208 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001209 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001210
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001211 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001212 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001213}
1214
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001215static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1216 .vidioc_querycap = fimc_vidioc_querycap_capture,
1217
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001218 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001219 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001220 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001221 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001222
1223 .vidioc_reqbufs = fimc_cap_reqbufs,
1224 .vidioc_querybuf = fimc_cap_querybuf,
1225
1226 .vidioc_qbuf = fimc_cap_qbuf,
1227 .vidioc_dqbuf = fimc_cap_dqbuf,
1228
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001229 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1230 .vidioc_create_bufs = fimc_cap_create_bufs,
1231
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001232 .vidioc_streamon = fimc_cap_streamon,
1233 .vidioc_streamoff = fimc_cap_streamoff,
1234
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001235 .vidioc_g_selection = fimc_cap_g_selection,
1236 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001237
1238 .vidioc_enum_input = fimc_cap_enum_input,
1239 .vidioc_s_input = fimc_cap_s_input,
1240 .vidioc_g_input = fimc_cap_g_input,
1241};
1242
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001243/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001244static int fimc_link_setup(struct media_entity *entity,
1245 const struct media_pad *local,
1246 const struct media_pad *remote, u32 flags)
1247{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001248 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1249 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1250
1251 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1252 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001253
1254 if (WARN_ON(fimc == NULL))
1255 return 0;
1256
1257 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1258 local->entity->name, remote->entity->name, flags,
1259 fimc->vid_cap.input);
1260
1261 if (flags & MEDIA_LNK_FL_ENABLED) {
1262 if (fimc->vid_cap.input != 0)
1263 return -EBUSY;
1264 fimc->vid_cap.input = sd->grp_id;
1265 return 0;
1266 }
1267
1268 fimc->vid_cap.input = 0;
1269 return 0;
1270}
1271
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001272static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001273 .link_setup = fimc_link_setup,
1274};
1275
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001276/**
1277 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1278 * @sd: pointer to a subdev generating the notification
1279 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1280 * @arg: pointer to an u32 type integer that stores the frame payload value
1281 *
1282 * The End Of Frame notification sent by sensor subdev in its still capture
1283 * mode. If there is only a single VSYNC generated by the sensor at the
1284 * beginning of a frame transmission, FIMC does not issue the LastIrq
1285 * (end of frame) interrupt. And this notification is used to complete the
1286 * frame capture and returning a buffer to user-space. Subdev drivers should
1287 * call this notification from their last 'End of frame capture' interrupt.
1288 */
1289void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1290 void *arg)
1291{
1292 struct fimc_sensor_info *sensor;
1293 struct fimc_vid_buffer *buf;
1294 struct fimc_md *fmd;
1295 struct fimc_dev *fimc;
1296 unsigned long flags;
1297
1298 if (sd == NULL)
1299 return;
1300
1301 sensor = v4l2_get_subdev_hostdata(sd);
1302 fmd = entity_to_fimc_mdev(&sd->entity);
1303
1304 spin_lock_irqsave(&fmd->slock, flags);
1305 fimc = sensor ? sensor->host : NULL;
1306
1307 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1308 test_bit(ST_CAPT_PEND, &fimc->state)) {
1309 unsigned long irq_flags;
1310 spin_lock_irqsave(&fimc->slock, irq_flags);
1311 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1312 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1313 struct fimc_vid_buffer, list);
1314 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1315 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001316 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001317 fimc_deactivate_capture(fimc);
1318 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1319 }
1320 spin_unlock_irqrestore(&fmd->slock, flags);
1321}
1322
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001323static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1324 struct v4l2_subdev_fh *fh,
1325 struct v4l2_subdev_mbus_code_enum *code)
1326{
1327 struct fimc_fmt *fmt;
1328
1329 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1330 if (!fmt)
1331 return -EINVAL;
1332 code->code = fmt->mbus_code;
1333 return 0;
1334}
1335
1336static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1337 struct v4l2_subdev_fh *fh,
1338 struct v4l2_subdev_format *fmt)
1339{
1340 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1341 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1342 struct v4l2_mbus_framefmt *mf;
1343 struct fimc_frame *ff;
1344
1345 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1346 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1347 fmt->format = *mf;
1348 return 0;
1349 }
1350 mf = &fmt->format;
1351 mf->colorspace = V4L2_COLORSPACE_JPEG;
1352 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1353
1354 mutex_lock(&fimc->lock);
1355 /* The pixel code is same on both input and output pad */
1356 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1357 mf->code = ctx->s_frame.fmt->mbus_code;
1358 mf->width = ff->f_width;
1359 mf->height = ff->f_height;
1360 mutex_unlock(&fimc->lock);
1361
1362 return 0;
1363}
1364
1365static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1366 struct v4l2_subdev_fh *fh,
1367 struct v4l2_subdev_format *fmt)
1368{
1369 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1370 struct v4l2_mbus_framefmt *mf = &fmt->format;
1371 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1372 struct fimc_frame *ff;
1373 struct fimc_fmt *ffmt;
1374
1375 dbg("pad%d: code: 0x%x, %dx%d",
1376 fmt->pad, mf->code, mf->width, mf->height);
1377
1378 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1379 vb2_is_busy(&fimc->vid_cap.vbq))
1380 return -EBUSY;
1381
1382 mutex_lock(&fimc->lock);
1383 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1384 &mf->code, NULL, fmt->pad);
1385 mutex_unlock(&fimc->lock);
1386 mf->colorspace = V4L2_COLORSPACE_JPEG;
1387
1388 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1389 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1390 *mf = fmt->format;
1391 return 0;
1392 }
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001393 /* Update RGB Alpha control state and value range */
1394 fimc_alpha_ctrl_update(ctx);
1395
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001396 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1397
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001398 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1399 &ctx->s_frame : &ctx->d_frame;
1400
1401 mutex_lock(&fimc->lock);
1402 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki393a23f2011-12-08 06:56:22 -03001403 fimc->vid_cap.mf = *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001404 ff->fmt = ffmt;
1405
1406 /* Reset the crop rectangle if required. */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001407 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001408 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1409
1410 if (fmt->pad == FIMC_SD_PAD_SINK)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001411 ctx->state &= ~FIMC_COMPOSE;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001412 mutex_unlock(&fimc->lock);
1413 return 0;
1414}
1415
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001416static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1417 struct v4l2_subdev_fh *fh,
1418 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001419{
1420 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1421 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001422 struct fimc_frame *f = &ctx->s_frame;
1423 struct v4l2_rect *r = &sel->r;
1424 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001425
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001426 if (sel->pad != FIMC_SD_PAD_SINK)
1427 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001428
1429 mutex_lock(&fimc->lock);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001430
1431 switch (sel->target) {
1432 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
1433 f = &ctx->d_frame;
1434 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
1435 r->width = f->o_width;
1436 r->height = f->o_height;
1437 r->left = 0;
1438 r->top = 0;
1439 mutex_unlock(&fimc->lock);
1440 return 0;
1441
1442 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1443 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1444 break;
1445 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1446 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1447 f = &ctx->d_frame;
1448 break;
1449 default:
1450 mutex_unlock(&fimc->lock);
1451 return -EINVAL;
1452 }
1453
1454 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1455 sel->r = *try_sel;
1456 } else {
1457 r->left = f->offs_h;
1458 r->top = f->offs_v;
1459 r->width = f->width;
1460 r->height = f->height;
1461 }
1462
1463 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1464 sel->pad, r->left, r->top, r->width, r->height,
1465 f->f_width, f->f_height);
1466
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001467 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001468 return 0;
1469}
1470
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001471static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1472 struct v4l2_subdev_fh *fh,
1473 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001474{
1475 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1476 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001477 struct fimc_frame *f = &ctx->s_frame;
1478 struct v4l2_rect *r = &sel->r;
1479 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001480 unsigned long flags;
1481
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001482 if (sel->pad != FIMC_SD_PAD_SINK)
1483 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001484
1485 mutex_lock(&fimc->lock);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001486 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP_ACTIVE);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001487
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001488 switch (sel->target) {
1489 case V4L2_SUBDEV_SEL_TGT_COMPOSE_BOUNDS:
1490 f = &ctx->d_frame;
1491 case V4L2_SUBDEV_SEL_TGT_CROP_BOUNDS:
1492 r->width = f->o_width;
1493 r->height = f->o_height;
1494 r->left = 0;
1495 r->top = 0;
Sylwester Nawrocki5be4fe62012-04-21 18:46:30 -03001496 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001497 return 0;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001498
1499 case V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL:
1500 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1501 break;
1502 case V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL:
1503 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1504 f = &ctx->d_frame;
1505 break;
1506 default:
1507 mutex_unlock(&fimc->lock);
1508 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001509 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001510
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001511 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1512 *try_sel = sel->r;
1513 } else {
1514 spin_lock_irqsave(&fimc->slock, flags);
1515 set_frame_crop(f, r->left, r->top, r->width, r->height);
1516 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1517 spin_unlock_irqrestore(&fimc->slock, flags);
1518 if (sel->target == V4L2_SUBDEV_SEL_TGT_COMPOSE_ACTUAL)
1519 ctx->state |= FIMC_COMPOSE;
1520 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001521
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001522 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001523 r->width, r->height);
1524
1525 mutex_unlock(&fimc->lock);
1526 return 0;
1527}
1528
1529static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1530 .enum_mbus_code = fimc_subdev_enum_mbus_code,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001531 .get_selection = fimc_subdev_get_selection,
1532 .set_selection = fimc_subdev_set_selection,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001533 .get_fmt = fimc_subdev_get_fmt,
1534 .set_fmt = fimc_subdev_set_fmt,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001535};
1536
1537static struct v4l2_subdev_ops fimc_subdev_ops = {
1538 .pad = &fimc_subdev_pad_ops,
1539};
1540
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001541/* Set default format at the sensor and host interface */
1542static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1543{
1544 struct v4l2_format fmt = {
1545 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1546 .fmt.pix_mp = {
1547 .width = 640,
1548 .height = 480,
1549 .pixelformat = V4L2_PIX_FMT_YUYV,
1550 .field = V4L2_FIELD_NONE,
1551 .colorspace = V4L2_COLORSPACE_JPEG,
1552 },
1553 };
1554
1555 return fimc_capture_set_format(fimc, &fmt);
1556}
1557
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001558/* fimc->lock must be already initialized */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001559static int fimc_register_capture_device(struct fimc_dev *fimc,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001560 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001561{
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001562 struct video_device *vfd;
1563 struct fimc_vid_cap *vid_cap;
1564 struct fimc_ctx *ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001565 struct vb2_queue *q;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001566 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001567
1568 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1569 if (!ctx)
1570 return -ENOMEM;
1571
1572 ctx->fimc_dev = fimc;
Sylwester Nawrocki3d112d92012-04-26 06:26:29 -03001573 ctx->in_path = FIMC_IO_CAMERA;
1574 ctx->out_path = FIMC_IO_DMA;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001575 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001576 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001577 ctx->d_frame.fmt = ctx->s_frame.fmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001578
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001579 vfd = video_device_alloc();
1580 if (!vfd) {
1581 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001582 goto err_vd_alloc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001583 }
1584
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001585 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001586
1587 vfd->fops = &fimc_capture_fops;
1588 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001589 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001590 vfd->minor = -1;
1591 vfd->release = video_device_release;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001592 vfd->lock = &fimc->lock;
Hans Verkuil5126f252012-05-10 04:57:22 -03001593 /* Locking in file operations other than ioctl should be done
1594 by the driver, not the V4L2 core.
1595 This driver needs auditing so that this flag can be removed. */
1596 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001597 video_set_drvdata(vfd, fimc);
1598
1599 vid_cap = &fimc->vid_cap;
1600 vid_cap->vfd = vfd;
1601 vid_cap->active_buf_cnt = 0;
1602 vid_cap->reqbufs_count = 0;
1603 vid_cap->refcnt = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001604
1605 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1606 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001607 vid_cap->ctx = ctx;
1608
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001609 q = &fimc->vid_cap.vbq;
1610 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001611 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001612 q->io_modes = VB2_MMAP | VB2_USERPTR;
1613 q->drv_priv = fimc->vid_cap.ctx;
1614 q->ops = &fimc_capture_qops;
1615 q->mem_ops = &vb2_dma_contig_memops;
1616 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1617
1618 vb2_queue_init(q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001619
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001620 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1621 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001622 if (ret)
1623 goto err_ent;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001624
1625 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001626 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001627 goto err_vd;
1628
1629 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1630 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001631
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -03001632 vfd->ctrl_handler = &ctx->ctrls.handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001633 return 0;
1634
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001635err_vd:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001636 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001637err_ent:
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001638 video_device_release(vfd);
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001639err_vd_alloc:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001640 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001641 return ret;
1642}
1643
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001644static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001645{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001646 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1647 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001648
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001649 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1650 if (ret)
1651 return ret;
1652
1653 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1654 if (ret)
1655 fimc_unregister_m2m_device(fimc);
1656
1657 return ret;
1658}
1659
1660static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1661{
1662 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1663
1664 if (fimc == NULL)
1665 return;
1666
1667 fimc_unregister_m2m_device(fimc);
1668
1669 if (fimc->vid_cap.vfd) {
1670 media_entity_cleanup(&fimc->vid_cap.vfd->entity);
1671 video_unregister_device(fimc->vid_cap.vfd);
1672 fimc->vid_cap.vfd = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001673 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001674
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001675 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001676 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001677}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001678
1679static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1680 .registered = fimc_capture_subdev_registered,
1681 .unregistered = fimc_capture_subdev_unregistered,
1682};
1683
1684int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1685{
1686 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1687 int ret;
1688
1689 v4l2_subdev_init(sd, &fimc_subdev_ops);
1690 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1691 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1692
1693 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1694 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1695 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1696 fimc->vid_cap.sd_pads, 0);
1697 if (ret)
1698 return ret;
1699
1700 sd->entity.ops = &fimc_sd_media_ops;
1701 sd->internal_ops = &fimc_capture_sd_internal_ops;
1702 v4l2_set_subdevdata(sd, fimc);
1703 return 0;
1704}
1705
1706void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1707{
1708 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1709
1710 v4l2_device_unregister_subdev(sd);
1711 media_entity_cleanup(&sd->entity);
1712 v4l2_set_subdevdata(sd, NULL);
1713}