blob: 52a5fb469b4509a166cfef38fdb2dc967aff8c71 [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 Nawrocki3a3f9442011-04-28 09:06:19 -03004 * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03005 * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com>
6 *
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 Nawrocki9e803a02011-06-10 15:36:53 -030034static int fimc_init_capture(struct fimc_dev *fimc)
35{
36 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
37 struct fimc_sensor_info *sensor;
38 unsigned long flags;
39 int ret = 0;
40
41 if (fimc->pipeline.sensor == NULL || ctx == NULL)
42 return -ENXIO;
43 if (ctx->s_frame.fmt == NULL)
44 return -EINVAL;
45
46 sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor);
47
48 spin_lock_irqsave(&fimc->slock, flags);
49 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
50 fimc_set_yuv_order(ctx);
51
52 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
53 fimc_hw_set_camera_type(fimc, sensor->pdata);
54 fimc_hw_set_camera_source(fimc, sensor->pdata);
55 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
56
57 ret = fimc_set_scaler_info(ctx);
58 if (!ret) {
59 fimc_hw_set_input_path(ctx);
60 fimc_hw_set_prescaler(ctx);
61 fimc_hw_set_mainscaler(ctx);
62 fimc_hw_set_target_format(ctx);
63 fimc_hw_set_rotation(ctx);
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -030064 fimc_hw_set_effect(ctx, false);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030065 fimc_hw_set_output_path(ctx);
66 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -030067 if (fimc->variant->has_alpha)
68 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -030069 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030070 }
71 spin_unlock_irqrestore(&fimc->slock, flags);
72 return ret;
73}
74
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030075static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030076{
Marek Szyprowskibd323e22011-08-29 08:51:49 -030077 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030078 struct fimc_vid_buffer *buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -030079 unsigned long flags;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030080 bool streaming;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030081
82 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030083 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030084
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030085 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
86 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrockiaa3331222012-03-16 11:47:51 -030087 if (suspend)
88 fimc->state |= (1 << ST_CAPT_SUSPENDED);
89 else
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030090 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030091
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030092 /* Release unused buffers */
93 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -030094 buf = fimc_pending_queue_pop(cap);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030095 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
96 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030097 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030098 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -030099 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300100 if (suspend)
101 fimc_pending_queue_add(cap, buf);
102 else
103 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300104 }
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300105
106 fimc_hw_reset(fimc);
107 cap->buf_index = 0;
108
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300109 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300110
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300111 if (streaming)
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300112 return fimc_pipeline_s_stream(fimc, 0);
113 else
114 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300115}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300116
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300117static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300118{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300119 unsigned long flags;
120
121 if (!fimc_capture_active(fimc))
122 return 0;
123
124 spin_lock_irqsave(&fimc->slock, flags);
125 set_bit(ST_CAPT_SHUT, &fimc->state);
126 fimc_deactivate_capture(fimc);
127 spin_unlock_irqrestore(&fimc->slock, flags);
128
129 wait_event_timeout(fimc->irq_queue,
130 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300131 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300132
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300133 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300134}
135
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300136/**
137 * fimc_capture_config_update - apply the camera interface configuration
138 *
139 * To be called from within the interrupt handler with fimc.slock
140 * spinlock held. It updates the camera pixel crop, rotation and
141 * image flip in H/W.
142 */
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300143static int fimc_capture_config_update(struct fimc_ctx *ctx)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300144{
145 struct fimc_dev *fimc = ctx->fimc_dev;
146 int ret;
147
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300148 if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300149 return 0;
150
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300151 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300152
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300153 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300154 if (ret)
155 return ret;
156
157 fimc_hw_set_prescaler(ctx);
158 fimc_hw_set_mainscaler(ctx);
159 fimc_hw_set_target_format(ctx);
160 fimc_hw_set_rotation(ctx);
161 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
162 fimc_hw_set_out_dma(ctx);
163 if (fimc->variant->has_alpha)
164 fimc_hw_set_rgb_alpha(ctx);
165
166 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300167 return ret;
168}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300169
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300170void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
171{
172 struct fimc_vid_cap *cap = &fimc->vid_cap;
173 struct fimc_vid_buffer *v_buf;
174 struct timeval *tv;
175 struct timespec ts;
176
177 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
178 wake_up(&fimc->irq_queue);
179 goto done;
180 }
181
182 if (!list_empty(&cap->active_buf_q) &&
183 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
184 ktime_get_real_ts(&ts);
185
186 v_buf = fimc_active_queue_pop(cap);
187
188 tv = &v_buf->vb.v4l2_buf.timestamp;
189 tv->tv_sec = ts.tv_sec;
190 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
191 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
192
193 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
194 }
195
196 if (!list_empty(&cap->pending_buf_q)) {
197
198 v_buf = fimc_pending_queue_pop(cap);
199 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
200 v_buf->index = cap->buf_index;
201
202 /* Move the buffer to the capture active queue */
203 fimc_active_queue_add(cap, v_buf);
204
205 dbg("next frame: %d, done frame: %d",
206 fimc_hw_get_frame_index(fimc), v_buf->index);
207
208 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
209 cap->buf_index = 0;
210 }
211
212 if (cap->active_buf_cnt == 0) {
213 if (deq_buf)
214 clear_bit(ST_CAPT_RUN, &fimc->state);
215
216 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
217 cap->buf_index = 0;
218 } else {
219 set_bit(ST_CAPT_RUN, &fimc->state);
220 }
221
222 fimc_capture_config_update(cap->ctx);
223done:
224 if (cap->active_buf_cnt == 1) {
225 fimc_deactivate_capture(fimc);
226 clear_bit(ST_CAPT_STREAM, &fimc->state);
227 }
228
229 dbg("frame: %d, active_buf_cnt: %d",
230 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
231}
232
233
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300234static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300235{
236 struct fimc_ctx *ctx = q->drv_priv;
237 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300238 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300239 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300240 int ret;
241
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300242 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300243
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300244 ret = fimc_init_capture(fimc);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300245 if (ret)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300246 goto error;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300247
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300248 set_bit(ST_CAPT_PEND, &fimc->state);
249
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300250 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
251
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300252 if (vid_cap->active_buf_cnt >= min_bufs &&
253 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300254 fimc_activate_capture(ctx);
255
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300256 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
257 fimc_pipeline_s_stream(fimc, 1);
258 }
259
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300260 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300261error:
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300262 fimc_capture_state_cleanup(fimc, false);
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300263 return ret;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300264}
265
266static int stop_streaming(struct vb2_queue *q)
267{
268 struct fimc_ctx *ctx = q->drv_priv;
269 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300270
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300271 if (!fimc_capture_active(fimc))
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300272 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300273
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300274 return fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300275}
276
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300277int fimc_capture_suspend(struct fimc_dev *fimc)
278{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300279 bool suspend = fimc_capture_busy(fimc);
280
281 int ret = fimc_stop_capture(fimc, suspend);
282 if (ret)
283 return ret;
284 return fimc_pipeline_shutdown(fimc);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300285}
286
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300287static void buffer_queue(struct vb2_buffer *vb);
288
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300289int fimc_capture_resume(struct fimc_dev *fimc)
290{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300291 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
292 struct fimc_vid_buffer *buf;
293 int i;
294
295 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
296 return 0;
297
298 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
299 vid_cap->buf_index = 0;
300 fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
301 false);
302 fimc_init_capture(fimc);
303
304 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
305
306 for (i = 0; i < vid_cap->reqbufs_count; i++) {
307 if (list_empty(&vid_cap->pending_buf_q))
308 break;
309 buf = fimc_pending_queue_pop(vid_cap);
310 buffer_queue(&buf->vb);
311 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300312 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300313
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300314}
315
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300316static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300317 unsigned int *num_buffers, unsigned int *num_planes,
318 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300319{
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300320 const struct v4l2_pix_format_mplane *pixm = NULL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300321 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300322 struct fimc_frame *frame = &ctx->d_frame;
323 struct fimc_fmt *fmt = frame->fmt;
324 unsigned long wh;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300325 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300326
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300327 if (pfmt) {
328 pixm = &pfmt->fmt.pix_mp;
329 fmt = fimc_find_format(&pixm->pixelformat, NULL,
330 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
331 wh = pixm->width * pixm->height;
332 } else {
333 wh = frame->f_width * frame->f_height;
334 }
335
336 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300337 return -EINVAL;
338
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300339 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300340
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300341 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300342 unsigned int size = (wh * fmt->depth[i]) / 8;
343 if (pixm)
344 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
345 else
346 sizes[i] = size;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300347 allocators[i] = ctx->fimc_dev->alloc_ctx;
348 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300349
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300350 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300351}
352
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300353static int buffer_prepare(struct vb2_buffer *vb)
354{
355 struct vb2_queue *vq = vb->vb2_queue;
356 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300357 int i;
358
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300359 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300360 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300361
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300362 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300363 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300364
365 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300366 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
367 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300368 vb2_plane_size(vb, i), size);
369 return -EINVAL;
370 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300371 vb2_set_plane_payload(vb, i, size);
372 }
373
374 return 0;
375}
376
377static void buffer_queue(struct vb2_buffer *vb)
378{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300379 struct fimc_vid_buffer *buf
380 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300381 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
382 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300383 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
384 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300385 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300386
387 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300388 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300389
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300390 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
391 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
392 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300393 /* Setup the buffer directly for processing. */
394 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
395 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300396
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300397 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
398 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300399 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300400
401 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
402 vid_cap->buf_index = 0;
403 } else {
404 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300405 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300406
407 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
408
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300409
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300410 if (vb2_is_streaming(&vid_cap->vbq) &&
411 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300412 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300413 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300414 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300415
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300416 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
417 fimc_pipeline_s_stream(fimc, 1);
418 return;
419 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300420 spin_unlock_irqrestore(&fimc->slock, flags);
421}
422
423static void fimc_lock(struct vb2_queue *vq)
424{
425 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
426 mutex_lock(&ctx->fimc_dev->lock);
427}
428
429static void fimc_unlock(struct vb2_queue *vq)
430{
431 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
432 mutex_unlock(&ctx->fimc_dev->lock);
433}
434
435static struct vb2_ops fimc_capture_qops = {
436 .queue_setup = queue_setup,
437 .buf_prepare = buffer_prepare,
438 .buf_queue = buffer_queue,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300439 .wait_prepare = fimc_unlock,
440 .wait_finish = fimc_lock,
441 .start_streaming = start_streaming,
442 .stop_streaming = stop_streaming,
443};
444
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300445/**
446 * fimc_capture_ctrls_create - initialize the control handler
447 * Initialize the capture video node control handler and fill it
448 * with the FIMC controls. Inherit any sensor's controls if the
449 * 'user_subdev_api' flag is false (default behaviour).
450 * This function need to be called with the graph mutex held.
451 */
452int fimc_capture_ctrls_create(struct fimc_dev *fimc)
453{
454 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
455 int ret;
456
457 if (WARN_ON(vid_cap->ctx == NULL))
458 return -ENXIO;
459 if (vid_cap->ctx->ctrls_rdy)
460 return 0;
461
462 ret = fimc_ctrls_create(vid_cap->ctx);
463 if (ret || vid_cap->user_subdev_api)
464 return ret;
465
466 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
467 fimc->pipeline.sensor->ctrl_handler);
468}
469
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300470static int fimc_capture_set_default_format(struct fimc_dev *fimc);
471
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300472static int fimc_capture_open(struct file *file)
473{
474 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300475 int ret = v4l2_fh_open(file);
476
477 if (ret)
478 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300479
480 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
481
482 /* Return if the corresponding video mem2mem node is already opened. */
483 if (fimc_m2m_active(fimc))
484 return -EBUSY;
485
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300486 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300487 pm_runtime_get_sync(&fimc->pdev->dev);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300488
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300489 if (++fimc->vid_cap.refcnt == 1) {
490 ret = fimc_pipeline_initialize(fimc,
491 &fimc->vid_cap.vfd->entity, true);
492 if (ret < 0) {
493 dev_err(&fimc->pdev->dev,
494 "Video pipeline initialization failed\n");
495 pm_runtime_put_sync(&fimc->pdev->dev);
496 fimc->vid_cap.refcnt--;
497 v4l2_fh_release(file);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300498 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300499 return ret;
500 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300501 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300502
503 if (!ret && !fimc->vid_cap.user_subdev_api)
504 ret = fimc_capture_set_default_format(fimc);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300505 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300506 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300507}
508
509static int fimc_capture_close(struct file *file)
510{
511 struct fimc_dev *fimc = video_drvdata(file);
512
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300513 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
514
515 if (--fimc->vid_cap.refcnt == 0) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300516 clear_bit(ST_CAPT_BUSY, &fimc->state);
517 fimc_stop_capture(fimc, false);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300518 fimc_pipeline_shutdown(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300519 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300520 }
521
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300522 pm_runtime_put(&fimc->pdev->dev);
523
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300524 if (fimc->vid_cap.refcnt == 0) {
525 vb2_queue_release(&fimc->vid_cap.vbq);
526 fimc_ctrls_delete(fimc->vid_cap.ctx);
527 }
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300528 return v4l2_fh_release(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300529}
530
531static unsigned int fimc_capture_poll(struct file *file,
532 struct poll_table_struct *wait)
533{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300534 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300535
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300536 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300537}
538
539static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
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_mmap(&fimc->vid_cap.vbq, vma);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300544}
545
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300546static const struct v4l2_file_operations fimc_capture_fops = {
547 .owner = THIS_MODULE,
548 .open = fimc_capture_open,
549 .release = fimc_capture_close,
550 .poll = fimc_capture_poll,
551 .unlocked_ioctl = video_ioctl2,
552 .mmap = fimc_capture_mmap,
553};
554
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300555/*
556 * Format and crop negotiation helpers
557 */
558
559static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
560 u32 *width, u32 *height,
561 u32 *code, u32 *fourcc, int pad)
562{
563 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
564 struct fimc_dev *fimc = ctx->fimc_dev;
565 struct samsung_fimc_variant *var = fimc->variant;
566 struct fimc_pix_limit *pl = var->pix_limit;
567 struct fimc_frame *dst = &ctx->d_frame;
568 u32 depth, min_w, max_w, min_h, align_h = 3;
569 u32 mask = FMT_FLAGS_CAM;
570 struct fimc_fmt *ffmt;
571
572 /* Color conversion from/to JPEG is not supported */
573 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
574 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
575 *code = V4L2_MBUS_FMT_JPEG_1X8;
576
577 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
578 mask |= FMT_FLAGS_M2M;
579
580 ffmt = fimc_find_format(fourcc, code, mask, 0);
581 if (WARN_ON(!ffmt))
582 return NULL;
583 if (code)
584 *code = ffmt->mbus_code;
585 if (fourcc)
586 *fourcc = ffmt->fourcc;
587
588 if (pad == FIMC_SD_PAD_SINK) {
589 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
590 pl->scaler_dis_w : pl->scaler_en_w;
591 /* Apply the camera input interface pixel constraints */
592 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
593 height, max_t(u32, *height, 32),
594 FIMC_CAMIF_MAX_HEIGHT,
595 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
596 0);
597 return ffmt;
598 }
599 /* Can't scale or crop in transparent (JPEG) transfer mode */
600 if (fimc_fmt_is_jpeg(ffmt->color)) {
601 *width = ctx->s_frame.f_width;
602 *height = ctx->s_frame.f_height;
603 return ffmt;
604 }
605 /* Apply the scaler and the output DMA constraints */
606 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
607 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
608 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300609 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300610 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
611
612 depth = fimc_get_format_depth(ffmt);
613 v4l_bound_align_image(width, min_w, max_w,
614 ffs(var->min_out_pixsize) - 1,
615 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
616 align_h,
617 64/(ALIGN(depth, 8)));
618
619 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
620 pad, code ? *code : 0, *width, *height,
621 dst->f_width, dst->f_height);
622
623 return ffmt;
624}
625
626static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
627 int pad)
628{
629 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
630 struct fimc_dev *fimc = ctx->fimc_dev;
631 struct samsung_fimc_variant *var = fimc->variant;
632 struct fimc_pix_limit *pl = var->pix_limit;
633 struct fimc_frame *sink = &ctx->s_frame;
634 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
635 u32 align_sz = 0, align_h = 4;
636 u32 max_sc_h, max_sc_v;
637
638 /* In JPEG transparent transfer mode cropping is not supported */
639 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
640 r->width = sink->f_width;
641 r->height = sink->f_height;
642 r->left = r->top = 0;
643 return;
644 }
645 if (pad == FIMC_SD_PAD_SOURCE) {
646 if (ctx->rotation != 90 && ctx->rotation != 270)
647 align_h = 1;
648 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
649 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
650 min_sz = var->min_out_pixsize;
651 } else {
652 u32 depth = fimc_get_format_depth(sink->fmt);
653 align_sz = 64/ALIGN(depth, 8);
654 min_sz = var->min_inp_pixsize;
655 min_w = min_h = min_sz;
656 max_sc_h = max_sc_v = 1;
657 }
658 /*
659 * For the crop rectangle at source pad the following constraints
660 * must be met:
661 * - it must fit in the sink pad format rectangle (f_width/f_height);
662 * - maximum downscaling ratio is 64;
663 * - maximum crop size depends if the rotator is used or not;
664 * - the sink pad format width/height must be 4 multiple of the
665 * prescaler ratios determined by sink pad size and source pad crop,
666 * the prescaler ratio is returned by fimc_get_scaler_factor().
667 */
668 max_w = min_t(u32,
669 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
670 rotate ? sink->f_height : sink->f_width);
671 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
672 if (pad == FIMC_SD_PAD_SOURCE) {
673 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
674 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
675 if (rotate) {
676 swap(max_sc_h, max_sc_v);
677 swap(min_w, min_h);
678 }
679 }
680 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
681 &r->height, min_h, max_h, align_h,
682 align_sz);
683 /* Adjust left/top if cropping rectangle is out of bounds */
684 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
685 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
686 r->left = round_down(r->left, var->hor_offs_align);
687
688 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
689 pad, r->left, r->top, r->width, r->height,
690 sink->f_width, sink->f_height);
691}
692
693/*
694 * The video node ioctl operations
695 */
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300696static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
697 struct v4l2_capability *cap)
698{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300699 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300700
701 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
702 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
703 cap->bus_info[0] = 0;
Sylwester Nawrocki8f401542011-07-07 06:42:05 -0300704 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300705
706 return 0;
707}
708
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300709static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
710 struct v4l2_fmtdesc *f)
711{
712 struct fimc_fmt *fmt;
713
714 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
715 f->index);
716 if (!fmt)
717 return -EINVAL;
718 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
719 f->pixelformat = fmt->fourcc;
720 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
721 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
722 return 0;
723}
724
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300725/**
726 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
727 * elements
728 * @ctx: FIMC capture context
729 * @tfmt: media bus format to try/set on subdevs
730 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
731 * @set: true to set format on subdevs, false to try only
732 */
733static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
734 struct v4l2_mbus_framefmt *tfmt,
735 struct fimc_fmt **fmt_id,
736 bool set)
737{
738 struct fimc_dev *fimc = ctx->fimc_dev;
739 struct v4l2_subdev *sd = fimc->pipeline.sensor;
740 struct v4l2_subdev *csis = fimc->pipeline.csis;
741 struct v4l2_subdev_format sfmt;
742 struct v4l2_mbus_framefmt *mf = &sfmt.format;
743 struct fimc_fmt *ffmt = NULL;
744 int ret, i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300745
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300746 if (WARN_ON(!sd || !tfmt))
747 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300748
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300749 memset(&sfmt, 0, sizeof(sfmt));
750 sfmt.format = *tfmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300751
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300752 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
753 while (1) {
754 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
755 FMT_FLAGS_CAM, i++);
756 if (ffmt == NULL) {
757 /*
758 * Notify user-space if common pixel code for
759 * host and sensor does not exist.
760 */
761 return -EINVAL;
762 }
763 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300764
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300765 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
766 if (ret)
767 return ret;
768 if (mf->code != tfmt->code) {
769 mf->code = 0;
770 continue;
771 }
Julia Lawallb1aa6082012-01-12 17:49:28 -0300772 if (mf->width != tfmt->width || mf->height != tfmt->height) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300773 u32 fcc = ffmt->fourcc;
774 tfmt->width = mf->width;
775 tfmt->height = mf->height;
776 ffmt = fimc_capture_try_format(ctx,
777 &tfmt->width, &tfmt->height,
778 NULL, &fcc, FIMC_SD_PAD_SOURCE);
779 if (ffmt && ffmt->mbus_code)
780 mf->code = ffmt->mbus_code;
Julia Lawallb1aa6082012-01-12 17:49:28 -0300781 if (mf->width != tfmt->width ||
782 mf->height != tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300783 continue;
784 tfmt->code = mf->code;
785 }
786 if (csis)
787 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300788
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300789 if (mf->code == tfmt->code &&
Julia Lawallb1aa6082012-01-12 17:49:28 -0300790 mf->width == tfmt->width && mf->height == tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300791 break;
792 }
793
794 if (fmt_id && ffmt)
795 *fmt_id = ffmt;
796 *tfmt = *mf;
797
798 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
799 return 0;
800}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300801
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300802static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
803 struct v4l2_format *f)
804{
805 struct fimc_dev *fimc = video_drvdata(file);
806 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
807
808 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
809 return -EINVAL;
810
811 return fimc_fill_format(&ctx->d_frame, f);
812}
813
814static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
815 struct v4l2_format *f)
816{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300817 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300818 struct fimc_dev *fimc = video_drvdata(file);
819 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300820 struct v4l2_mbus_framefmt mf;
821 struct fimc_fmt *ffmt = NULL;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300822
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300823 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
824 return -EINVAL;
825
826 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
827 fimc_capture_try_format(ctx, &pix->width, &pix->height,
828 NULL, &pix->pixelformat,
829 FIMC_SD_PAD_SINK);
830 ctx->s_frame.f_width = pix->width;
831 ctx->s_frame.f_height = pix->height;
832 }
833 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
834 NULL, &pix->pixelformat,
835 FIMC_SD_PAD_SOURCE);
836 if (!ffmt)
837 return -EINVAL;
838
839 if (!fimc->vid_cap.user_subdev_api) {
840 mf.width = pix->width;
841 mf.height = pix->height;
842 mf.code = ffmt->mbus_code;
843 fimc_md_graph_lock(fimc);
844 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
845 fimc_md_graph_unlock(fimc);
846
847 pix->width = mf.width;
848 pix->height = mf.height;
849 if (ffmt)
850 pix->pixelformat = ffmt->fourcc;
851 }
852
853 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300854 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300855}
856
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300857static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
858{
859 ctx->scaler.enabled = !jpeg;
860 fimc_ctrls_activate(ctx, !jpeg);
861
862 if (jpeg)
863 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
864 else
865 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
866}
867
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300868static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
869{
870 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
871 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
872 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
873 struct fimc_frame *ff = &ctx->d_frame;
874 struct fimc_fmt *s_fmt = NULL;
875 int ret, i;
876
877 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
878 return -EINVAL;
879 if (vb2_is_busy(&fimc->vid_cap.vbq))
880 return -EBUSY;
881
882 /* Pre-configure format at camera interface input, for JPEG only */
883 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
884 fimc_capture_try_format(ctx, &pix->width, &pix->height,
885 NULL, &pix->pixelformat,
886 FIMC_SD_PAD_SINK);
887 ctx->s_frame.f_width = pix->width;
888 ctx->s_frame.f_height = pix->height;
889 }
890 /* Try the format at the scaler and the DMA output */
891 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
892 NULL, &pix->pixelformat,
893 FIMC_SD_PAD_SOURCE);
894 if (!ff->fmt)
895 return -EINVAL;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -0300896
897 /* Update RGB Alpha control state and value range */
898 fimc_alpha_ctrl_update(ctx);
899
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300900 /* Try to match format at the host and the sensor */
901 if (!fimc->vid_cap.user_subdev_api) {
902 mf->code = ff->fmt->mbus_code;
903 mf->width = pix->width;
904 mf->height = pix->height;
905
906 fimc_md_graph_lock(fimc);
907 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
908 fimc_md_graph_unlock(fimc);
909 if (ret)
910 return ret;
911 pix->width = mf->width;
912 pix->height = mf->height;
913 }
914 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
915 for (i = 0; i < ff->fmt->colplanes; i++)
916 ff->payload[i] =
917 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
918
919 set_frame_bounds(ff, pix->width, pix->height);
920 /* Reset the composition rectangle if not yet configured */
921 if (!(ctx->state & FIMC_DST_CROP))
922 set_frame_crop(ff, 0, 0, pix->width, pix->height);
923
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300924 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
925
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300926 /* Reset cropping and set format at the camera interface input */
927 if (!fimc->vid_cap.user_subdev_api) {
928 ctx->s_frame.fmt = s_fmt;
929 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
930 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
931 }
932
933 return ret;
934}
935
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300936static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
937 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300938{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300939 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300940
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300941 return fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300942}
943
944static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300945 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300946{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300947 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300948 struct v4l2_subdev *sd = fimc->pipeline.sensor;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300949
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300950 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300951 return -EINVAL;
952
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300953 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300954 if (sd)
955 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300956 return 0;
957}
958
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300959static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300960{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300961 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300962}
963
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300964static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300965{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300966 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300967 return 0;
968}
969
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300970/**
971 * fimc_pipeline_validate - check for formats inconsistencies
972 * between source and sink pad of each link
973 *
974 * Return 0 if all formats match or -EPIPE otherwise.
975 */
976static int fimc_pipeline_validate(struct fimc_dev *fimc)
977{
978 struct v4l2_subdev_format sink_fmt, src_fmt;
979 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
980 struct v4l2_subdev *sd;
981 struct media_pad *pad;
982 int ret;
983
984 /* Start with the video capture node pad */
985 pad = media_entity_remote_source(&vid_cap->vd_pad);
986 if (pad == NULL)
987 return -EPIPE;
988 /* FIMC.{N} subdevice */
989 sd = media_entity_to_v4l2_subdev(pad->entity);
990
991 while (1) {
992 /* Retrieve format at the sink pad */
993 pad = &sd->entity.pads[0];
994 if (!(pad->flags & MEDIA_PAD_FL_SINK))
995 break;
996 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -0300997 if (sd == &fimc->vid_cap.subdev) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300998 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
999 sink_fmt.format.width = ff->f_width;
1000 sink_fmt.format.height = ff->f_height;
1001 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1002 } else {
1003 sink_fmt.pad = pad->index;
1004 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1005 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1006 if (ret < 0 && ret != -ENOIOCTLCMD)
1007 return -EPIPE;
1008 }
1009 /* Retrieve format at the source pad */
1010 pad = media_entity_remote_source(pad);
1011 if (pad == NULL ||
1012 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1013 break;
1014
1015 sd = media_entity_to_v4l2_subdev(pad->entity);
1016 src_fmt.pad = pad->index;
1017 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1018 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1019 if (ret < 0 && ret != -ENOIOCTLCMD)
1020 return -EPIPE;
1021
1022 if (src_fmt.format.width != sink_fmt.format.width ||
1023 src_fmt.format.height != sink_fmt.format.height ||
1024 src_fmt.format.code != sink_fmt.format.code)
1025 return -EPIPE;
1026 }
1027 return 0;
1028}
1029
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001030static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001031 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001032{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001033 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001034 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001035 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001036
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001037 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001038 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001039
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001040 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001041
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001042 if (fimc->vid_cap.user_subdev_api) {
1043 ret = fimc_pipeline_validate(fimc);
1044 if (ret)
1045 return ret;
1046 }
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001047 return vb2_streamon(&fimc->vid_cap.vbq, type);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001048}
1049
1050static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001051 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001052{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001053 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001054 struct v4l2_subdev *sd = fimc->pipeline.sensor;
1055 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001056
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001057 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
1058 if (ret == 0)
1059 media_entity_pipeline_stop(&sd->entity);
1060 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001061}
1062
1063static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001064 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001065{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001066 struct fimc_dev *fimc = video_drvdata(file);
1067 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001068
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001069 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001070 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001071 return ret;
1072}
1073
1074static int fimc_cap_querybuf(struct file *file, void *priv,
1075 struct v4l2_buffer *buf)
1076{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001077 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001078
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001079 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001080}
1081
1082static int fimc_cap_qbuf(struct file *file, void *priv,
1083 struct v4l2_buffer *buf)
1084{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001085 struct fimc_dev *fimc = video_drvdata(file);
1086
1087 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001088}
1089
1090static int fimc_cap_dqbuf(struct file *file, void *priv,
1091 struct v4l2_buffer *buf)
1092{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001093 struct fimc_dev *fimc = video_drvdata(file);
1094
1095 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001096}
1097
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001098static int fimc_cap_create_bufs(struct file *file, void *priv,
1099 struct v4l2_create_buffers *create)
1100{
1101 struct fimc_dev *fimc = video_drvdata(file);
1102
1103 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1104}
1105
1106static int fimc_cap_prepare_buf(struct file *file, void *priv,
1107 struct v4l2_buffer *b)
1108{
1109 struct fimc_dev *fimc = video_drvdata(file);
1110
1111 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1112}
1113
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001114static int fimc_cap_g_selection(struct file *file, void *fh,
1115 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001116{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001117 struct fimc_dev *fimc = video_drvdata(file);
1118 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001119 struct fimc_frame *f = &ctx->s_frame;
1120
1121 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1122 return -EINVAL;
1123
1124 switch (s->target) {
1125 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1126 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1127 f = &ctx->d_frame;
1128 case V4L2_SEL_TGT_CROP_BOUNDS:
1129 case V4L2_SEL_TGT_CROP_DEFAULT:
1130 s->r.left = 0;
1131 s->r.top = 0;
1132 s->r.width = f->o_width;
1133 s->r.height = f->o_height;
1134 return 0;
1135
1136 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1137 f = &ctx->d_frame;
1138 case V4L2_SEL_TGT_CROP_ACTIVE:
1139 s->r.left = f->offs_h;
1140 s->r.top = f->offs_v;
1141 s->r.width = f->width;
1142 s->r.height = f->height;
1143 return 0;
1144 }
1145
1146 return -EINVAL;
1147}
1148
1149/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1150int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1151{
1152 if (a->left < b->left || a->top < b->top)
1153 return 0;
1154 if (a->left + a->width > b->left + b->width)
1155 return 0;
1156 if (a->top + a->height > b->top + b->height)
1157 return 0;
1158
1159 return 1;
1160}
1161
1162static int fimc_cap_s_selection(struct file *file, void *fh,
1163 struct v4l2_selection *s)
1164{
1165 struct fimc_dev *fimc = video_drvdata(file);
1166 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1167 struct v4l2_rect rect = s->r;
1168 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001169 unsigned long flags;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001170 unsigned int pad;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001171
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001172 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1173 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001174
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001175 switch (s->target) {
1176 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1177 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1178 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1179 f = &ctx->d_frame;
1180 pad = FIMC_SD_PAD_SOURCE;
1181 break;
1182 case V4L2_SEL_TGT_CROP_BOUNDS:
1183 case V4L2_SEL_TGT_CROP_DEFAULT:
1184 case V4L2_SEL_TGT_CROP_ACTIVE:
1185 f = &ctx->s_frame;
1186 pad = FIMC_SD_PAD_SINK;
1187 break;
1188 default:
1189 return -EINVAL;
1190 }
1191
1192 fimc_capture_try_crop(ctx, &rect, pad);
1193
1194 if (s->flags & V4L2_SEL_FLAG_LE &&
1195 !enclosed_rectangle(&rect, &s->r))
1196 return -ERANGE;
1197
1198 if (s->flags & V4L2_SEL_FLAG_GE &&
1199 !enclosed_rectangle(&s->r, &rect))
1200 return -ERANGE;
1201
1202 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001203 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001204 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1205 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001206 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001207
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001208 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001209 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001210}
1211
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001212static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1213 .vidioc_querycap = fimc_vidioc_querycap_capture,
1214
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001215 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001216 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001217 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001218 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001219
1220 .vidioc_reqbufs = fimc_cap_reqbufs,
1221 .vidioc_querybuf = fimc_cap_querybuf,
1222
1223 .vidioc_qbuf = fimc_cap_qbuf,
1224 .vidioc_dqbuf = fimc_cap_dqbuf,
1225
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001226 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1227 .vidioc_create_bufs = fimc_cap_create_bufs,
1228
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001229 .vidioc_streamon = fimc_cap_streamon,
1230 .vidioc_streamoff = fimc_cap_streamoff,
1231
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001232 .vidioc_g_selection = fimc_cap_g_selection,
1233 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001234
1235 .vidioc_enum_input = fimc_cap_enum_input,
1236 .vidioc_s_input = fimc_cap_s_input,
1237 .vidioc_g_input = fimc_cap_g_input,
1238};
1239
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001240/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001241static int fimc_link_setup(struct media_entity *entity,
1242 const struct media_pad *local,
1243 const struct media_pad *remote, u32 flags)
1244{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001245 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1246 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1247
1248 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1249 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001250
1251 if (WARN_ON(fimc == NULL))
1252 return 0;
1253
1254 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1255 local->entity->name, remote->entity->name, flags,
1256 fimc->vid_cap.input);
1257
1258 if (flags & MEDIA_LNK_FL_ENABLED) {
1259 if (fimc->vid_cap.input != 0)
1260 return -EBUSY;
1261 fimc->vid_cap.input = sd->grp_id;
1262 return 0;
1263 }
1264
1265 fimc->vid_cap.input = 0;
1266 return 0;
1267}
1268
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001269static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001270 .link_setup = fimc_link_setup,
1271};
1272
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001273/**
1274 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1275 * @sd: pointer to a subdev generating the notification
1276 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1277 * @arg: pointer to an u32 type integer that stores the frame payload value
1278 *
1279 * The End Of Frame notification sent by sensor subdev in its still capture
1280 * mode. If there is only a single VSYNC generated by the sensor at the
1281 * beginning of a frame transmission, FIMC does not issue the LastIrq
1282 * (end of frame) interrupt. And this notification is used to complete the
1283 * frame capture and returning a buffer to user-space. Subdev drivers should
1284 * call this notification from their last 'End of frame capture' interrupt.
1285 */
1286void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1287 void *arg)
1288{
1289 struct fimc_sensor_info *sensor;
1290 struct fimc_vid_buffer *buf;
1291 struct fimc_md *fmd;
1292 struct fimc_dev *fimc;
1293 unsigned long flags;
1294
1295 if (sd == NULL)
1296 return;
1297
1298 sensor = v4l2_get_subdev_hostdata(sd);
1299 fmd = entity_to_fimc_mdev(&sd->entity);
1300
1301 spin_lock_irqsave(&fmd->slock, flags);
1302 fimc = sensor ? sensor->host : NULL;
1303
1304 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1305 test_bit(ST_CAPT_PEND, &fimc->state)) {
1306 unsigned long irq_flags;
1307 spin_lock_irqsave(&fimc->slock, irq_flags);
1308 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1309 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1310 struct fimc_vid_buffer, list);
1311 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1312 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001313 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001314 fimc_deactivate_capture(fimc);
1315 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1316 }
1317 spin_unlock_irqrestore(&fmd->slock, flags);
1318}
1319
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001320static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1321 struct v4l2_subdev_fh *fh,
1322 struct v4l2_subdev_mbus_code_enum *code)
1323{
1324 struct fimc_fmt *fmt;
1325
1326 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1327 if (!fmt)
1328 return -EINVAL;
1329 code->code = fmt->mbus_code;
1330 return 0;
1331}
1332
1333static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1334 struct v4l2_subdev_fh *fh,
1335 struct v4l2_subdev_format *fmt)
1336{
1337 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1338 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1339 struct v4l2_mbus_framefmt *mf;
1340 struct fimc_frame *ff;
1341
1342 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1343 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1344 fmt->format = *mf;
1345 return 0;
1346 }
1347 mf = &fmt->format;
1348 mf->colorspace = V4L2_COLORSPACE_JPEG;
1349 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1350
1351 mutex_lock(&fimc->lock);
1352 /* The pixel code is same on both input and output pad */
1353 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1354 mf->code = ctx->s_frame.fmt->mbus_code;
1355 mf->width = ff->f_width;
1356 mf->height = ff->f_height;
1357 mutex_unlock(&fimc->lock);
1358
1359 return 0;
1360}
1361
1362static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1363 struct v4l2_subdev_fh *fh,
1364 struct v4l2_subdev_format *fmt)
1365{
1366 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1367 struct v4l2_mbus_framefmt *mf = &fmt->format;
1368 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1369 struct fimc_frame *ff;
1370 struct fimc_fmt *ffmt;
1371
1372 dbg("pad%d: code: 0x%x, %dx%d",
1373 fmt->pad, mf->code, mf->width, mf->height);
1374
1375 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1376 vb2_is_busy(&fimc->vid_cap.vbq))
1377 return -EBUSY;
1378
1379 mutex_lock(&fimc->lock);
1380 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1381 &mf->code, NULL, fmt->pad);
1382 mutex_unlock(&fimc->lock);
1383 mf->colorspace = V4L2_COLORSPACE_JPEG;
1384
1385 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1386 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1387 *mf = fmt->format;
1388 return 0;
1389 }
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001390 /* Update RGB Alpha control state and value range */
1391 fimc_alpha_ctrl_update(ctx);
1392
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001393 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1394
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001395 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1396 &ctx->s_frame : &ctx->d_frame;
1397
1398 mutex_lock(&fimc->lock);
1399 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki393a23f2011-12-08 06:56:22 -03001400 fimc->vid_cap.mf = *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001401 ff->fmt = ffmt;
1402
1403 /* Reset the crop rectangle if required. */
1404 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1405 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1406
1407 if (fmt->pad == FIMC_SD_PAD_SINK)
1408 ctx->state &= ~FIMC_DST_CROP;
1409 mutex_unlock(&fimc->lock);
1410 return 0;
1411}
1412
1413static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1414 struct v4l2_subdev_fh *fh,
1415 struct v4l2_subdev_crop *crop)
1416{
1417 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1418 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1419 struct v4l2_rect *r = &crop->rect;
1420 struct fimc_frame *ff;
1421
1422 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1423 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1424 return 0;
1425 }
1426 ff = crop->pad == FIMC_SD_PAD_SINK ?
1427 &ctx->s_frame : &ctx->d_frame;
1428
1429 mutex_lock(&fimc->lock);
1430 r->left = ff->offs_h;
1431 r->top = ff->offs_v;
1432 r->width = ff->width;
1433 r->height = ff->height;
1434 mutex_unlock(&fimc->lock);
1435
1436 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1437 ff, crop->pad, r->left, r->top, r->width, r->height,
1438 ff->f_width, ff->f_height);
1439
1440 return 0;
1441}
1442
1443static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1444 struct v4l2_subdev_fh *fh,
1445 struct v4l2_subdev_crop *crop)
1446{
1447 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1448 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1449 struct v4l2_rect *r = &crop->rect;
1450 struct fimc_frame *ff;
1451 unsigned long flags;
1452
1453 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1454
1455 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1456 &ctx->d_frame : &ctx->s_frame;
1457
1458 mutex_lock(&fimc->lock);
1459 fimc_capture_try_crop(ctx, r, crop->pad);
1460
1461 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
Sylwester Nawrocki5be4fe62012-04-21 18:46:30 -03001462 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001463 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1464 return 0;
1465 }
1466 spin_lock_irqsave(&fimc->slock, flags);
1467 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1468 if (crop->pad == FIMC_SD_PAD_SOURCE)
1469 ctx->state |= FIMC_DST_CROP;
1470
1471 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1472 spin_unlock_irqrestore(&fimc->slock, flags);
1473
1474 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1475 r->width, r->height);
1476
1477 mutex_unlock(&fimc->lock);
1478 return 0;
1479}
1480
1481static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1482 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1483 .get_fmt = fimc_subdev_get_fmt,
1484 .set_fmt = fimc_subdev_set_fmt,
1485 .get_crop = fimc_subdev_get_crop,
1486 .set_crop = fimc_subdev_set_crop,
1487};
1488
1489static struct v4l2_subdev_ops fimc_subdev_ops = {
1490 .pad = &fimc_subdev_pad_ops,
1491};
1492
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001493/* Set default format at the sensor and host interface */
1494static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1495{
1496 struct v4l2_format fmt = {
1497 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1498 .fmt.pix_mp = {
1499 .width = 640,
1500 .height = 480,
1501 .pixelformat = V4L2_PIX_FMT_YUYV,
1502 .field = V4L2_FIELD_NONE,
1503 .colorspace = V4L2_COLORSPACE_JPEG,
1504 },
1505 };
1506
1507 return fimc_capture_set_format(fimc, &fmt);
1508}
1509
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001510/* fimc->lock must be already initialized */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001511static int fimc_register_capture_device(struct fimc_dev *fimc,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001512 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001513{
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001514 struct video_device *vfd;
1515 struct fimc_vid_cap *vid_cap;
1516 struct fimc_ctx *ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001517 struct vb2_queue *q;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001518 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001519
1520 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1521 if (!ctx)
1522 return -ENOMEM;
1523
1524 ctx->fimc_dev = fimc;
1525 ctx->in_path = FIMC_CAMERA;
1526 ctx->out_path = FIMC_DMA;
1527 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001528 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001529 ctx->d_frame.fmt = ctx->s_frame.fmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001530
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001531 vfd = video_device_alloc();
1532 if (!vfd) {
1533 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001534 goto err_vd_alloc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001535 }
1536
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001537 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001538
1539 vfd->fops = &fimc_capture_fops;
1540 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001541 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001542 vfd->minor = -1;
1543 vfd->release = video_device_release;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001544 vfd->lock = &fimc->lock;
Hans Verkuil5126f252012-05-10 04:57:22 -03001545 /* Locking in file operations other than ioctl should be done
1546 by the driver, not the V4L2 core.
1547 This driver needs auditing so that this flag can be removed. */
1548 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001549 video_set_drvdata(vfd, fimc);
1550
1551 vid_cap = &fimc->vid_cap;
1552 vid_cap->vfd = vfd;
1553 vid_cap->active_buf_cnt = 0;
1554 vid_cap->reqbufs_count = 0;
1555 vid_cap->refcnt = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001556
1557 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1558 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001559 vid_cap->ctx = ctx;
1560
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001561 q = &fimc->vid_cap.vbq;
1562 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001563 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001564 q->io_modes = VB2_MMAP | VB2_USERPTR;
1565 q->drv_priv = fimc->vid_cap.ctx;
1566 q->ops = &fimc_capture_qops;
1567 q->mem_ops = &vb2_dma_contig_memops;
1568 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1569
1570 vb2_queue_init(q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001571
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001572 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1573 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001574 if (ret)
1575 goto err_ent;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001576
1577 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001578 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001579 goto err_vd;
1580
1581 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1582 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001583
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001584 vfd->ctrl_handler = &ctx->ctrl_handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001585 return 0;
1586
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001587err_vd:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001588 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001589err_ent:
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001590 video_device_release(vfd);
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001591err_vd_alloc:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001592 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001593 return ret;
1594}
1595
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001596static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001597{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001598 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1599 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001600
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001601 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1602 if (ret)
1603 return ret;
1604
1605 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1606 if (ret)
1607 fimc_unregister_m2m_device(fimc);
1608
1609 return ret;
1610}
1611
1612static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1613{
1614 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1615
1616 if (fimc == NULL)
1617 return;
1618
1619 fimc_unregister_m2m_device(fimc);
1620
1621 if (fimc->vid_cap.vfd) {
1622 media_entity_cleanup(&fimc->vid_cap.vfd->entity);
1623 video_unregister_device(fimc->vid_cap.vfd);
1624 fimc->vid_cap.vfd = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001625 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001626
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001627 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001628 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001629}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001630
1631static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1632 .registered = fimc_capture_subdev_registered,
1633 .unregistered = fimc_capture_subdev_unregistered,
1634};
1635
1636int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1637{
1638 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1639 int ret;
1640
1641 v4l2_subdev_init(sd, &fimc_subdev_ops);
1642 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1643 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1644
1645 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1646 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1647 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1648 fimc->vid_cap.sd_pads, 0);
1649 if (ret)
1650 return ret;
1651
1652 sd->entity.ops = &fimc_sd_media_ops;
1653 sd->internal_ops = &fimc_capture_sd_internal_ops;
1654 v4l2_set_subdevdata(sd, fimc);
1655 return 0;
1656}
1657
1658void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1659{
1660 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1661
1662 v4l2_device_unregister_subdev(sd);
1663 media_entity_cleanup(&sd->entity);
1664 v4l2_set_subdevdata(sd, NULL);
1665}