blob: 7e9b2c612b03e9ccccb5a03883169f376876e272 [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"
32
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030033static int fimc_init_capture(struct fimc_dev *fimc)
34{
35 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
36 struct fimc_sensor_info *sensor;
37 unsigned long flags;
38 int ret = 0;
39
40 if (fimc->pipeline.sensor == NULL || ctx == NULL)
41 return -ENXIO;
42 if (ctx->s_frame.fmt == NULL)
43 return -EINVAL;
44
45 sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor);
46
47 spin_lock_irqsave(&fimc->slock, flags);
48 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
49 fimc_set_yuv_order(ctx);
50
51 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
52 fimc_hw_set_camera_type(fimc, sensor->pdata);
53 fimc_hw_set_camera_source(fimc, sensor->pdata);
54 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
55
56 ret = fimc_set_scaler_info(ctx);
57 if (!ret) {
58 fimc_hw_set_input_path(ctx);
59 fimc_hw_set_prescaler(ctx);
60 fimc_hw_set_mainscaler(ctx);
61 fimc_hw_set_target_format(ctx);
62 fimc_hw_set_rotation(ctx);
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -030063 fimc_hw_set_effect(ctx, false);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030064 fimc_hw_set_output_path(ctx);
65 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -030066 if (fimc->variant->has_alpha)
67 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -030068 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030069 }
70 spin_unlock_irqrestore(&fimc->slock, flags);
71 return ret;
72}
73
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030074static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030075{
Marek Szyprowskibd323e22011-08-29 08:51:49 -030076 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030077 struct fimc_vid_buffer *buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -030078 unsigned long flags;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030079 bool streaming;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030080
81 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030082 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030083
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030084 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
85 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
86 if (!suspend)
87 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030088
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030089 /* Release unused buffers */
90 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -030091 buf = fimc_pending_queue_pop(cap);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030092 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
93 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030094 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030095 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -030096 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030097 if (suspend)
98 fimc_pending_queue_add(cap, buf);
99 else
100 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300101 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300102 set_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300103
104 fimc_hw_reset(fimc);
105 cap->buf_index = 0;
106
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300107 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300108
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300109 if (streaming)
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300110 return fimc_pipeline_s_stream(fimc, 0);
111 else
112 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300113}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300114
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300115static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300116{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300117 unsigned long flags;
118
119 if (!fimc_capture_active(fimc))
120 return 0;
121
122 spin_lock_irqsave(&fimc->slock, flags);
123 set_bit(ST_CAPT_SHUT, &fimc->state);
124 fimc_deactivate_capture(fimc);
125 spin_unlock_irqrestore(&fimc->slock, flags);
126
127 wait_event_timeout(fimc->irq_queue,
128 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300129 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300130
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300131 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300132}
133
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300134/**
135 * fimc_capture_config_update - apply the camera interface configuration
136 *
137 * To be called from within the interrupt handler with fimc.slock
138 * spinlock held. It updates the camera pixel crop, rotation and
139 * image flip in H/W.
140 */
141int fimc_capture_config_update(struct fimc_ctx *ctx)
142{
143 struct fimc_dev *fimc = ctx->fimc_dev;
144 int ret;
145
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300146 if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300147 return 0;
148
149 spin_lock(&ctx->slock);
150 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
151 ret = fimc_set_scaler_info(ctx);
152 if (ret == 0) {
153 fimc_hw_set_prescaler(ctx);
154 fimc_hw_set_mainscaler(ctx);
155 fimc_hw_set_target_format(ctx);
156 fimc_hw_set_rotation(ctx);
157 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
158 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -0300159 if (fimc->variant->has_alpha)
160 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300161 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300162 }
163 spin_unlock(&ctx->slock);
164 return ret;
165}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300166
167static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300168{
169 struct fimc_ctx *ctx = q->drv_priv;
170 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300171 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300172 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300173 int ret;
174
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300175 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300176
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300177 ret = fimc_init_capture(fimc);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300178 if (ret)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300179 goto error;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300180
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300181 set_bit(ST_CAPT_PEND, &fimc->state);
182
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300183 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
184
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300185 if (vid_cap->active_buf_cnt >= min_bufs &&
186 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300187 fimc_activate_capture(ctx);
188
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300189 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
190 fimc_pipeline_s_stream(fimc, 1);
191 }
192
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300193 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300194error:
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300195 fimc_capture_state_cleanup(fimc, false);
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300196 return ret;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300197}
198
199static int stop_streaming(struct vb2_queue *q)
200{
201 struct fimc_ctx *ctx = q->drv_priv;
202 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300203
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300204 if (!fimc_capture_active(fimc))
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300205 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300206
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300207 return fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300208}
209
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300210int fimc_capture_suspend(struct fimc_dev *fimc)
211{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300212 bool suspend = fimc_capture_busy(fimc);
213
214 int ret = fimc_stop_capture(fimc, suspend);
215 if (ret)
216 return ret;
217 return fimc_pipeline_shutdown(fimc);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300218}
219
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300220static void buffer_queue(struct vb2_buffer *vb);
221
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300222int fimc_capture_resume(struct fimc_dev *fimc)
223{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300224 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
225 struct fimc_vid_buffer *buf;
226 int i;
227
228 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
229 return 0;
230
231 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
232 vid_cap->buf_index = 0;
233 fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
234 false);
235 fimc_init_capture(fimc);
236
237 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
238
239 for (i = 0; i < vid_cap->reqbufs_count; i++) {
240 if (list_empty(&vid_cap->pending_buf_q))
241 break;
242 buf = fimc_pending_queue_pop(vid_cap);
243 buffer_queue(&buf->vb);
244 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300245 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300246
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300247}
248
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300249static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300250 unsigned int *num_buffers, unsigned int *num_planes,
251 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300252{
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300253 const struct v4l2_pix_format_mplane *pixm = NULL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300254 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300255 struct fimc_frame *frame = &ctx->d_frame;
256 struct fimc_fmt *fmt = frame->fmt;
257 unsigned long wh;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300258 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300259
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300260 if (pfmt) {
261 pixm = &pfmt->fmt.pix_mp;
262 fmt = fimc_find_format(&pixm->pixelformat, NULL,
263 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
264 wh = pixm->width * pixm->height;
265 } else {
266 wh = frame->f_width * frame->f_height;
267 }
268
269 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300270 return -EINVAL;
271
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300272 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300273
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300274 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300275 unsigned int size = (wh * fmt->depth[i]) / 8;
276 if (pixm)
277 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
278 else
279 sizes[i] = size;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300280 allocators[i] = ctx->fimc_dev->alloc_ctx;
281 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300282
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300283 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300284}
285
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300286static int buffer_prepare(struct vb2_buffer *vb)
287{
288 struct vb2_queue *vq = vb->vb2_queue;
289 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300290 int i;
291
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300292 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300293 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300294
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300295 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300296 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300297
298 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300299 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
300 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300301 vb2_plane_size(vb, i), size);
302 return -EINVAL;
303 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300304 vb2_set_plane_payload(vb, i, size);
305 }
306
307 return 0;
308}
309
310static void buffer_queue(struct vb2_buffer *vb)
311{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300312 struct fimc_vid_buffer *buf
313 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300314 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
315 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300316 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
317 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300318 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300319
320 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300321 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300322
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300323 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
324 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
325 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300326 /* Setup the buffer directly for processing. */
327 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
328 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300329
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300330 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
331 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300332 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300333
334 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
335 vid_cap->buf_index = 0;
336 } else {
337 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300338 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300339
340 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
341
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300342
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300343 if (vb2_is_streaming(&vid_cap->vbq) &&
344 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300345 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300346 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300347 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300348
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300349 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
350 fimc_pipeline_s_stream(fimc, 1);
351 return;
352 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300353 spin_unlock_irqrestore(&fimc->slock, flags);
354}
355
356static void fimc_lock(struct vb2_queue *vq)
357{
358 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
359 mutex_lock(&ctx->fimc_dev->lock);
360}
361
362static void fimc_unlock(struct vb2_queue *vq)
363{
364 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
365 mutex_unlock(&ctx->fimc_dev->lock);
366}
367
368static struct vb2_ops fimc_capture_qops = {
369 .queue_setup = queue_setup,
370 .buf_prepare = buffer_prepare,
371 .buf_queue = buffer_queue,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300372 .wait_prepare = fimc_unlock,
373 .wait_finish = fimc_lock,
374 .start_streaming = start_streaming,
375 .stop_streaming = stop_streaming,
376};
377
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300378/**
379 * fimc_capture_ctrls_create - initialize the control handler
380 * Initialize the capture video node control handler and fill it
381 * with the FIMC controls. Inherit any sensor's controls if the
382 * 'user_subdev_api' flag is false (default behaviour).
383 * This function need to be called with the graph mutex held.
384 */
385int fimc_capture_ctrls_create(struct fimc_dev *fimc)
386{
387 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
388 int ret;
389
390 if (WARN_ON(vid_cap->ctx == NULL))
391 return -ENXIO;
392 if (vid_cap->ctx->ctrls_rdy)
393 return 0;
394
395 ret = fimc_ctrls_create(vid_cap->ctx);
396 if (ret || vid_cap->user_subdev_api)
397 return ret;
398
399 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
400 fimc->pipeline.sensor->ctrl_handler);
401}
402
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300403static int fimc_capture_set_default_format(struct fimc_dev *fimc);
404
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300405static int fimc_capture_open(struct file *file)
406{
407 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300408 int ret = v4l2_fh_open(file);
409
410 if (ret)
411 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300412
413 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
414
415 /* Return if the corresponding video mem2mem node is already opened. */
416 if (fimc_m2m_active(fimc))
417 return -EBUSY;
418
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300419 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300420 pm_runtime_get_sync(&fimc->pdev->dev);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300421
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300422 if (++fimc->vid_cap.refcnt == 1) {
423 ret = fimc_pipeline_initialize(fimc,
424 &fimc->vid_cap.vfd->entity, true);
425 if (ret < 0) {
426 dev_err(&fimc->pdev->dev,
427 "Video pipeline initialization failed\n");
428 pm_runtime_put_sync(&fimc->pdev->dev);
429 fimc->vid_cap.refcnt--;
430 v4l2_fh_release(file);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300431 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300432 return ret;
433 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300434 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300435
436 if (!ret && !fimc->vid_cap.user_subdev_api)
437 ret = fimc_capture_set_default_format(fimc);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300438 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300439 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300440}
441
442static int fimc_capture_close(struct file *file)
443{
444 struct fimc_dev *fimc = video_drvdata(file);
445
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300446 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
447
448 if (--fimc->vid_cap.refcnt == 0) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300449 clear_bit(ST_CAPT_BUSY, &fimc->state);
450 fimc_stop_capture(fimc, false);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300451 fimc_pipeline_shutdown(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300452 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300453 }
454
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300455 pm_runtime_put(&fimc->pdev->dev);
456
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300457 if (fimc->vid_cap.refcnt == 0) {
458 vb2_queue_release(&fimc->vid_cap.vbq);
459 fimc_ctrls_delete(fimc->vid_cap.ctx);
460 }
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300461 return v4l2_fh_release(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300462}
463
464static unsigned int fimc_capture_poll(struct file *file,
465 struct poll_table_struct *wait)
466{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300467 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300468
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300469 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300470}
471
472static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
473{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300474 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300475
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300476 return vb2_mmap(&fimc->vid_cap.vbq, vma);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300477}
478
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300479static const struct v4l2_file_operations fimc_capture_fops = {
480 .owner = THIS_MODULE,
481 .open = fimc_capture_open,
482 .release = fimc_capture_close,
483 .poll = fimc_capture_poll,
484 .unlocked_ioctl = video_ioctl2,
485 .mmap = fimc_capture_mmap,
486};
487
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300488/*
489 * Format and crop negotiation helpers
490 */
491
492static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
493 u32 *width, u32 *height,
494 u32 *code, u32 *fourcc, int pad)
495{
496 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
497 struct fimc_dev *fimc = ctx->fimc_dev;
498 struct samsung_fimc_variant *var = fimc->variant;
499 struct fimc_pix_limit *pl = var->pix_limit;
500 struct fimc_frame *dst = &ctx->d_frame;
501 u32 depth, min_w, max_w, min_h, align_h = 3;
502 u32 mask = FMT_FLAGS_CAM;
503 struct fimc_fmt *ffmt;
504
505 /* Color conversion from/to JPEG is not supported */
506 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
507 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
508 *code = V4L2_MBUS_FMT_JPEG_1X8;
509
510 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
511 mask |= FMT_FLAGS_M2M;
512
513 ffmt = fimc_find_format(fourcc, code, mask, 0);
514 if (WARN_ON(!ffmt))
515 return NULL;
516 if (code)
517 *code = ffmt->mbus_code;
518 if (fourcc)
519 *fourcc = ffmt->fourcc;
520
521 if (pad == FIMC_SD_PAD_SINK) {
522 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
523 pl->scaler_dis_w : pl->scaler_en_w;
524 /* Apply the camera input interface pixel constraints */
525 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
526 height, max_t(u32, *height, 32),
527 FIMC_CAMIF_MAX_HEIGHT,
528 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
529 0);
530 return ffmt;
531 }
532 /* Can't scale or crop in transparent (JPEG) transfer mode */
533 if (fimc_fmt_is_jpeg(ffmt->color)) {
534 *width = ctx->s_frame.f_width;
535 *height = ctx->s_frame.f_height;
536 return ffmt;
537 }
538 /* Apply the scaler and the output DMA constraints */
539 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
540 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
541 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300542 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300543 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
544
545 depth = fimc_get_format_depth(ffmt);
546 v4l_bound_align_image(width, min_w, max_w,
547 ffs(var->min_out_pixsize) - 1,
548 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
549 align_h,
550 64/(ALIGN(depth, 8)));
551
552 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
553 pad, code ? *code : 0, *width, *height,
554 dst->f_width, dst->f_height);
555
556 return ffmt;
557}
558
559static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
560 int pad)
561{
562 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
563 struct fimc_dev *fimc = ctx->fimc_dev;
564 struct samsung_fimc_variant *var = fimc->variant;
565 struct fimc_pix_limit *pl = var->pix_limit;
566 struct fimc_frame *sink = &ctx->s_frame;
567 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
568 u32 align_sz = 0, align_h = 4;
569 u32 max_sc_h, max_sc_v;
570
571 /* In JPEG transparent transfer mode cropping is not supported */
572 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
573 r->width = sink->f_width;
574 r->height = sink->f_height;
575 r->left = r->top = 0;
576 return;
577 }
578 if (pad == FIMC_SD_PAD_SOURCE) {
579 if (ctx->rotation != 90 && ctx->rotation != 270)
580 align_h = 1;
581 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
582 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
583 min_sz = var->min_out_pixsize;
584 } else {
585 u32 depth = fimc_get_format_depth(sink->fmt);
586 align_sz = 64/ALIGN(depth, 8);
587 min_sz = var->min_inp_pixsize;
588 min_w = min_h = min_sz;
589 max_sc_h = max_sc_v = 1;
590 }
591 /*
592 * For the crop rectangle at source pad the following constraints
593 * must be met:
594 * - it must fit in the sink pad format rectangle (f_width/f_height);
595 * - maximum downscaling ratio is 64;
596 * - maximum crop size depends if the rotator is used or not;
597 * - the sink pad format width/height must be 4 multiple of the
598 * prescaler ratios determined by sink pad size and source pad crop,
599 * the prescaler ratio is returned by fimc_get_scaler_factor().
600 */
601 max_w = min_t(u32,
602 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
603 rotate ? sink->f_height : sink->f_width);
604 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
605 if (pad == FIMC_SD_PAD_SOURCE) {
606 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
607 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
608 if (rotate) {
609 swap(max_sc_h, max_sc_v);
610 swap(min_w, min_h);
611 }
612 }
613 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
614 &r->height, min_h, max_h, align_h,
615 align_sz);
616 /* Adjust left/top if cropping rectangle is out of bounds */
617 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
618 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
619 r->left = round_down(r->left, var->hor_offs_align);
620
621 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
622 pad, r->left, r->top, r->width, r->height,
623 sink->f_width, sink->f_height);
624}
625
626/*
627 * The video node ioctl operations
628 */
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300629static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
630 struct v4l2_capability *cap)
631{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300632 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300633
634 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
635 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
636 cap->bus_info[0] = 0;
Sylwester Nawrocki8f401542011-07-07 06:42:05 -0300637 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300638
639 return 0;
640}
641
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300642static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
643 struct v4l2_fmtdesc *f)
644{
645 struct fimc_fmt *fmt;
646
647 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
648 f->index);
649 if (!fmt)
650 return -EINVAL;
651 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
652 f->pixelformat = fmt->fourcc;
653 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
654 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
655 return 0;
656}
657
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300658/**
659 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
660 * elements
661 * @ctx: FIMC capture context
662 * @tfmt: media bus format to try/set on subdevs
663 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
664 * @set: true to set format on subdevs, false to try only
665 */
666static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
667 struct v4l2_mbus_framefmt *tfmt,
668 struct fimc_fmt **fmt_id,
669 bool set)
670{
671 struct fimc_dev *fimc = ctx->fimc_dev;
672 struct v4l2_subdev *sd = fimc->pipeline.sensor;
673 struct v4l2_subdev *csis = fimc->pipeline.csis;
674 struct v4l2_subdev_format sfmt;
675 struct v4l2_mbus_framefmt *mf = &sfmt.format;
676 struct fimc_fmt *ffmt = NULL;
677 int ret, i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300678
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300679 if (WARN_ON(!sd || !tfmt))
680 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300681
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300682 memset(&sfmt, 0, sizeof(sfmt));
683 sfmt.format = *tfmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300684
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300685 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
686 while (1) {
687 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
688 FMT_FLAGS_CAM, i++);
689 if (ffmt == NULL) {
690 /*
691 * Notify user-space if common pixel code for
692 * host and sensor does not exist.
693 */
694 return -EINVAL;
695 }
696 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300697
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300698 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
699 if (ret)
700 return ret;
701 if (mf->code != tfmt->code) {
702 mf->code = 0;
703 continue;
704 }
Julia Lawallb1aa6082012-01-12 17:49:28 -0300705 if (mf->width != tfmt->width || mf->height != tfmt->height) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300706 u32 fcc = ffmt->fourcc;
707 tfmt->width = mf->width;
708 tfmt->height = mf->height;
709 ffmt = fimc_capture_try_format(ctx,
710 &tfmt->width, &tfmt->height,
711 NULL, &fcc, FIMC_SD_PAD_SOURCE);
712 if (ffmt && ffmt->mbus_code)
713 mf->code = ffmt->mbus_code;
Julia Lawallb1aa6082012-01-12 17:49:28 -0300714 if (mf->width != tfmt->width ||
715 mf->height != tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300716 continue;
717 tfmt->code = mf->code;
718 }
719 if (csis)
720 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300721
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300722 if (mf->code == tfmt->code &&
Julia Lawallb1aa6082012-01-12 17:49:28 -0300723 mf->width == tfmt->width && mf->height == tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300724 break;
725 }
726
727 if (fmt_id && ffmt)
728 *fmt_id = ffmt;
729 *tfmt = *mf;
730
731 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
732 return 0;
733}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300734
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300735static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
736 struct v4l2_format *f)
737{
738 struct fimc_dev *fimc = video_drvdata(file);
739 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
740
741 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
742 return -EINVAL;
743
744 return fimc_fill_format(&ctx->d_frame, f);
745}
746
747static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
748 struct v4l2_format *f)
749{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300750 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300751 struct fimc_dev *fimc = video_drvdata(file);
752 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300753 struct v4l2_mbus_framefmt mf;
754 struct fimc_fmt *ffmt = NULL;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300755
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300756 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
757 return -EINVAL;
758
759 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
760 fimc_capture_try_format(ctx, &pix->width, &pix->height,
761 NULL, &pix->pixelformat,
762 FIMC_SD_PAD_SINK);
763 ctx->s_frame.f_width = pix->width;
764 ctx->s_frame.f_height = pix->height;
765 }
766 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
767 NULL, &pix->pixelformat,
768 FIMC_SD_PAD_SOURCE);
769 if (!ffmt)
770 return -EINVAL;
771
772 if (!fimc->vid_cap.user_subdev_api) {
773 mf.width = pix->width;
774 mf.height = pix->height;
775 mf.code = ffmt->mbus_code;
776 fimc_md_graph_lock(fimc);
777 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
778 fimc_md_graph_unlock(fimc);
779
780 pix->width = mf.width;
781 pix->height = mf.height;
782 if (ffmt)
783 pix->pixelformat = ffmt->fourcc;
784 }
785
786 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300787 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300788}
789
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -0300790static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
791{
792 ctx->scaler.enabled = !jpeg;
793 fimc_ctrls_activate(ctx, !jpeg);
794
795 if (jpeg)
796 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
797 else
798 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
799}
800
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300801static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
802{
803 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
804 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
805 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
806 struct fimc_frame *ff = &ctx->d_frame;
807 struct fimc_fmt *s_fmt = NULL;
808 int ret, i;
809
810 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
811 return -EINVAL;
812 if (vb2_is_busy(&fimc->vid_cap.vbq))
813 return -EBUSY;
814
815 /* Pre-configure format at camera interface input, for JPEG only */
816 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
817 fimc_capture_try_format(ctx, &pix->width, &pix->height,
818 NULL, &pix->pixelformat,
819 FIMC_SD_PAD_SINK);
820 ctx->s_frame.f_width = pix->width;
821 ctx->s_frame.f_height = pix->height;
822 }
823 /* Try the format at the scaler and the DMA output */
824 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
825 NULL, &pix->pixelformat,
826 FIMC_SD_PAD_SOURCE);
827 if (!ff->fmt)
828 return -EINVAL;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -0300829
830 /* Update RGB Alpha control state and value range */
831 fimc_alpha_ctrl_update(ctx);
832
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300833 /* Try to match format at the host and the sensor */
834 if (!fimc->vid_cap.user_subdev_api) {
835 mf->code = ff->fmt->mbus_code;
836 mf->width = pix->width;
837 mf->height = pix->height;
838
839 fimc_md_graph_lock(fimc);
840 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
841 fimc_md_graph_unlock(fimc);
842 if (ret)
843 return ret;
844 pix->width = mf->width;
845 pix->height = mf->height;
846 }
847 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
848 for (i = 0; i < ff->fmt->colplanes; i++)
849 ff->payload[i] =
850 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
851
852 set_frame_bounds(ff, pix->width, pix->height);
853 /* Reset the composition rectangle if not yet configured */
854 if (!(ctx->state & FIMC_DST_CROP))
855 set_frame_crop(ff, 0, 0, pix->width, pix->height);
856
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -0300857 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
858
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300859 /* Reset cropping and set format at the camera interface input */
860 if (!fimc->vid_cap.user_subdev_api) {
861 ctx->s_frame.fmt = s_fmt;
862 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
863 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
864 }
865
866 return ret;
867}
868
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300869static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
870 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300871{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300872 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300873
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300874 return fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300875}
876
877static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300878 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300879{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300880 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300881 struct v4l2_subdev *sd = fimc->pipeline.sensor;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300882
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300883 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300884 return -EINVAL;
885
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300886 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300887 if (sd)
888 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300889 return 0;
890}
891
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300892static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300893{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300894 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300895}
896
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300897static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300898{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300899 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300900 return 0;
901}
902
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300903/**
904 * fimc_pipeline_validate - check for formats inconsistencies
905 * between source and sink pad of each link
906 *
907 * Return 0 if all formats match or -EPIPE otherwise.
908 */
909static int fimc_pipeline_validate(struct fimc_dev *fimc)
910{
911 struct v4l2_subdev_format sink_fmt, src_fmt;
912 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
913 struct v4l2_subdev *sd;
914 struct media_pad *pad;
915 int ret;
916
917 /* Start with the video capture node pad */
918 pad = media_entity_remote_source(&vid_cap->vd_pad);
919 if (pad == NULL)
920 return -EPIPE;
921 /* FIMC.{N} subdevice */
922 sd = media_entity_to_v4l2_subdev(pad->entity);
923
924 while (1) {
925 /* Retrieve format at the sink pad */
926 pad = &sd->entity.pads[0];
927 if (!(pad->flags & MEDIA_PAD_FL_SINK))
928 break;
929 /* Don't call FIMC subdev operation to avoid nested locking */
930 if (sd == fimc->vid_cap.subdev) {
931 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
932 sink_fmt.format.width = ff->f_width;
933 sink_fmt.format.height = ff->f_height;
934 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
935 } else {
936 sink_fmt.pad = pad->index;
937 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
938 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
939 if (ret < 0 && ret != -ENOIOCTLCMD)
940 return -EPIPE;
941 }
942 /* Retrieve format at the source pad */
943 pad = media_entity_remote_source(pad);
944 if (pad == NULL ||
945 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
946 break;
947
948 sd = media_entity_to_v4l2_subdev(pad->entity);
949 src_fmt.pad = pad->index;
950 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
951 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
952 if (ret < 0 && ret != -ENOIOCTLCMD)
953 return -EPIPE;
954
955 if (src_fmt.format.width != sink_fmt.format.width ||
956 src_fmt.format.height != sink_fmt.format.height ||
957 src_fmt.format.code != sink_fmt.format.code)
958 return -EPIPE;
959 }
960 return 0;
961}
962
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300963static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300964 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300965{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300966 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300967 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300968 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300969
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300970 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300971 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300972
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300973 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300974
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300975 if (fimc->vid_cap.user_subdev_api) {
976 ret = fimc_pipeline_validate(fimc);
977 if (ret)
978 return ret;
979 }
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300980 return vb2_streamon(&fimc->vid_cap.vbq, type);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300981}
982
983static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300984 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300985{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300986 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300987 struct v4l2_subdev *sd = fimc->pipeline.sensor;
988 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300989
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300990 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
991 if (ret == 0)
992 media_entity_pipeline_stop(&sd->entity);
993 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300994}
995
996static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300997 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300998{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300999 struct fimc_dev *fimc = video_drvdata(file);
1000 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001001
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001002 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001003 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001004 return ret;
1005}
1006
1007static int fimc_cap_querybuf(struct file *file, void *priv,
1008 struct v4l2_buffer *buf)
1009{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001010 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001011
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001012 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001013}
1014
1015static int fimc_cap_qbuf(struct file *file, void *priv,
1016 struct v4l2_buffer *buf)
1017{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001018 struct fimc_dev *fimc = video_drvdata(file);
1019
1020 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001021}
1022
1023static int fimc_cap_dqbuf(struct file *file, void *priv,
1024 struct v4l2_buffer *buf)
1025{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001026 struct fimc_dev *fimc = video_drvdata(file);
1027
1028 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001029}
1030
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001031static int fimc_cap_create_bufs(struct file *file, void *priv,
1032 struct v4l2_create_buffers *create)
1033{
1034 struct fimc_dev *fimc = video_drvdata(file);
1035
1036 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1037}
1038
1039static int fimc_cap_prepare_buf(struct file *file, void *priv,
1040 struct v4l2_buffer *b)
1041{
1042 struct fimc_dev *fimc = video_drvdata(file);
1043
1044 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1045}
1046
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001047static int fimc_cap_g_selection(struct file *file, void *fh,
1048 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001049{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001050 struct fimc_dev *fimc = video_drvdata(file);
1051 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001052 struct fimc_frame *f = &ctx->s_frame;
1053
1054 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1055 return -EINVAL;
1056
1057 switch (s->target) {
1058 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1059 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1060 f = &ctx->d_frame;
1061 case V4L2_SEL_TGT_CROP_BOUNDS:
1062 case V4L2_SEL_TGT_CROP_DEFAULT:
1063 s->r.left = 0;
1064 s->r.top = 0;
1065 s->r.width = f->o_width;
1066 s->r.height = f->o_height;
1067 return 0;
1068
1069 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1070 f = &ctx->d_frame;
1071 case V4L2_SEL_TGT_CROP_ACTIVE:
1072 s->r.left = f->offs_h;
1073 s->r.top = f->offs_v;
1074 s->r.width = f->width;
1075 s->r.height = f->height;
1076 return 0;
1077 }
1078
1079 return -EINVAL;
1080}
1081
1082/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1083int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1084{
1085 if (a->left < b->left || a->top < b->top)
1086 return 0;
1087 if (a->left + a->width > b->left + b->width)
1088 return 0;
1089 if (a->top + a->height > b->top + b->height)
1090 return 0;
1091
1092 return 1;
1093}
1094
1095static int fimc_cap_s_selection(struct file *file, void *fh,
1096 struct v4l2_selection *s)
1097{
1098 struct fimc_dev *fimc = video_drvdata(file);
1099 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1100 struct v4l2_rect rect = s->r;
1101 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001102 unsigned long flags;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001103 unsigned int pad;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001104
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001105 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1106 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001107
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001108 switch (s->target) {
1109 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1110 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1111 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1112 f = &ctx->d_frame;
1113 pad = FIMC_SD_PAD_SOURCE;
1114 break;
1115 case V4L2_SEL_TGT_CROP_BOUNDS:
1116 case V4L2_SEL_TGT_CROP_DEFAULT:
1117 case V4L2_SEL_TGT_CROP_ACTIVE:
1118 f = &ctx->s_frame;
1119 pad = FIMC_SD_PAD_SINK;
1120 break;
1121 default:
1122 return -EINVAL;
1123 }
1124
1125 fimc_capture_try_crop(ctx, &rect, pad);
1126
1127 if (s->flags & V4L2_SEL_FLAG_LE &&
1128 !enclosed_rectangle(&rect, &s->r))
1129 return -ERANGE;
1130
1131 if (s->flags & V4L2_SEL_FLAG_GE &&
1132 !enclosed_rectangle(&s->r, &rect))
1133 return -ERANGE;
1134
1135 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001136 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001137 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1138 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001139 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001140
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001141 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001142 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001143}
1144
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001145static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1146 .vidioc_querycap = fimc_vidioc_querycap_capture,
1147
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001148 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001149 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001150 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001151 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001152
1153 .vidioc_reqbufs = fimc_cap_reqbufs,
1154 .vidioc_querybuf = fimc_cap_querybuf,
1155
1156 .vidioc_qbuf = fimc_cap_qbuf,
1157 .vidioc_dqbuf = fimc_cap_dqbuf,
1158
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001159 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1160 .vidioc_create_bufs = fimc_cap_create_bufs,
1161
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001162 .vidioc_streamon = fimc_cap_streamon,
1163 .vidioc_streamoff = fimc_cap_streamoff,
1164
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001165 .vidioc_g_selection = fimc_cap_g_selection,
1166 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001167
1168 .vidioc_enum_input = fimc_cap_enum_input,
1169 .vidioc_s_input = fimc_cap_s_input,
1170 .vidioc_g_input = fimc_cap_g_input,
1171};
1172
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001173/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001174static int fimc_link_setup(struct media_entity *entity,
1175 const struct media_pad *local,
1176 const struct media_pad *remote, u32 flags)
1177{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001178 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1179 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1180
1181 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1182 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001183
1184 if (WARN_ON(fimc == NULL))
1185 return 0;
1186
1187 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1188 local->entity->name, remote->entity->name, flags,
1189 fimc->vid_cap.input);
1190
1191 if (flags & MEDIA_LNK_FL_ENABLED) {
1192 if (fimc->vid_cap.input != 0)
1193 return -EBUSY;
1194 fimc->vid_cap.input = sd->grp_id;
1195 return 0;
1196 }
1197
1198 fimc->vid_cap.input = 0;
1199 return 0;
1200}
1201
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001202static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001203 .link_setup = fimc_link_setup,
1204};
1205
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001206/**
1207 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1208 * @sd: pointer to a subdev generating the notification
1209 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1210 * @arg: pointer to an u32 type integer that stores the frame payload value
1211 *
1212 * The End Of Frame notification sent by sensor subdev in its still capture
1213 * mode. If there is only a single VSYNC generated by the sensor at the
1214 * beginning of a frame transmission, FIMC does not issue the LastIrq
1215 * (end of frame) interrupt. And this notification is used to complete the
1216 * frame capture and returning a buffer to user-space. Subdev drivers should
1217 * call this notification from their last 'End of frame capture' interrupt.
1218 */
1219void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1220 void *arg)
1221{
1222 struct fimc_sensor_info *sensor;
1223 struct fimc_vid_buffer *buf;
1224 struct fimc_md *fmd;
1225 struct fimc_dev *fimc;
1226 unsigned long flags;
1227
1228 if (sd == NULL)
1229 return;
1230
1231 sensor = v4l2_get_subdev_hostdata(sd);
1232 fmd = entity_to_fimc_mdev(&sd->entity);
1233
1234 spin_lock_irqsave(&fmd->slock, flags);
1235 fimc = sensor ? sensor->host : NULL;
1236
1237 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1238 test_bit(ST_CAPT_PEND, &fimc->state)) {
1239 unsigned long irq_flags;
1240 spin_lock_irqsave(&fimc->slock, irq_flags);
1241 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1242 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1243 struct fimc_vid_buffer, list);
1244 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1245 }
1246 fimc_capture_irq_handler(fimc, true);
1247 fimc_deactivate_capture(fimc);
1248 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1249 }
1250 spin_unlock_irqrestore(&fmd->slock, flags);
1251}
1252
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001253static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1254 struct v4l2_subdev_fh *fh,
1255 struct v4l2_subdev_mbus_code_enum *code)
1256{
1257 struct fimc_fmt *fmt;
1258
1259 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1260 if (!fmt)
1261 return -EINVAL;
1262 code->code = fmt->mbus_code;
1263 return 0;
1264}
1265
1266static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1267 struct v4l2_subdev_fh *fh,
1268 struct v4l2_subdev_format *fmt)
1269{
1270 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1271 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1272 struct v4l2_mbus_framefmt *mf;
1273 struct fimc_frame *ff;
1274
1275 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1276 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1277 fmt->format = *mf;
1278 return 0;
1279 }
1280 mf = &fmt->format;
1281 mf->colorspace = V4L2_COLORSPACE_JPEG;
1282 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1283
1284 mutex_lock(&fimc->lock);
1285 /* The pixel code is same on both input and output pad */
1286 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1287 mf->code = ctx->s_frame.fmt->mbus_code;
1288 mf->width = ff->f_width;
1289 mf->height = ff->f_height;
1290 mutex_unlock(&fimc->lock);
1291
1292 return 0;
1293}
1294
1295static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1296 struct v4l2_subdev_fh *fh,
1297 struct v4l2_subdev_format *fmt)
1298{
1299 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1300 struct v4l2_mbus_framefmt *mf = &fmt->format;
1301 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1302 struct fimc_frame *ff;
1303 struct fimc_fmt *ffmt;
1304
1305 dbg("pad%d: code: 0x%x, %dx%d",
1306 fmt->pad, mf->code, mf->width, mf->height);
1307
1308 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1309 vb2_is_busy(&fimc->vid_cap.vbq))
1310 return -EBUSY;
1311
1312 mutex_lock(&fimc->lock);
1313 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1314 &mf->code, NULL, fmt->pad);
1315 mutex_unlock(&fimc->lock);
1316 mf->colorspace = V4L2_COLORSPACE_JPEG;
1317
1318 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1319 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1320 *mf = fmt->format;
1321 return 0;
1322 }
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001323 /* Update RGB Alpha control state and value range */
1324 fimc_alpha_ctrl_update(ctx);
1325
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -03001326 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1327
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001328 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1329 &ctx->s_frame : &ctx->d_frame;
1330
1331 mutex_lock(&fimc->lock);
1332 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki393a23f2011-12-08 06:56:22 -03001333 fimc->vid_cap.mf = *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001334 ff->fmt = ffmt;
1335
1336 /* Reset the crop rectangle if required. */
1337 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1338 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1339
1340 if (fmt->pad == FIMC_SD_PAD_SINK)
1341 ctx->state &= ~FIMC_DST_CROP;
1342 mutex_unlock(&fimc->lock);
1343 return 0;
1344}
1345
1346static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1347 struct v4l2_subdev_fh *fh,
1348 struct v4l2_subdev_crop *crop)
1349{
1350 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1351 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1352 struct v4l2_rect *r = &crop->rect;
1353 struct fimc_frame *ff;
1354
1355 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1356 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1357 return 0;
1358 }
1359 ff = crop->pad == FIMC_SD_PAD_SINK ?
1360 &ctx->s_frame : &ctx->d_frame;
1361
1362 mutex_lock(&fimc->lock);
1363 r->left = ff->offs_h;
1364 r->top = ff->offs_v;
1365 r->width = ff->width;
1366 r->height = ff->height;
1367 mutex_unlock(&fimc->lock);
1368
1369 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1370 ff, crop->pad, r->left, r->top, r->width, r->height,
1371 ff->f_width, ff->f_height);
1372
1373 return 0;
1374}
1375
1376static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1377 struct v4l2_subdev_fh *fh,
1378 struct v4l2_subdev_crop *crop)
1379{
1380 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1381 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1382 struct v4l2_rect *r = &crop->rect;
1383 struct fimc_frame *ff;
1384 unsigned long flags;
1385
1386 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1387
1388 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1389 &ctx->d_frame : &ctx->s_frame;
1390
1391 mutex_lock(&fimc->lock);
1392 fimc_capture_try_crop(ctx, r, crop->pad);
1393
1394 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
Sylwester Nawrockie985dbf2012-04-21 18:46:30 -03001395 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001396 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1397 return 0;
1398 }
1399 spin_lock_irqsave(&fimc->slock, flags);
1400 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1401 if (crop->pad == FIMC_SD_PAD_SOURCE)
1402 ctx->state |= FIMC_DST_CROP;
1403
1404 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1405 spin_unlock_irqrestore(&fimc->slock, flags);
1406
1407 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1408 r->width, r->height);
1409
1410 mutex_unlock(&fimc->lock);
1411 return 0;
1412}
1413
1414static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1415 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1416 .get_fmt = fimc_subdev_get_fmt,
1417 .set_fmt = fimc_subdev_set_fmt,
1418 .get_crop = fimc_subdev_get_crop,
1419 .set_crop = fimc_subdev_set_crop,
1420};
1421
1422static struct v4l2_subdev_ops fimc_subdev_ops = {
1423 .pad = &fimc_subdev_pad_ops,
1424};
1425
1426static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1427 struct v4l2_device *v4l2_dev)
1428{
1429 struct v4l2_subdev *sd;
1430 int ret;
1431
1432 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1433 if (!sd)
1434 return -ENOMEM;
1435
1436 v4l2_subdev_init(sd, &fimc_subdev_ops);
1437 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1438 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1439
1440 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1441 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1442 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1443 fimc->vid_cap.sd_pads, 0);
1444 if (ret)
1445 goto me_err;
1446 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1447 if (ret)
1448 goto sd_err;
1449
1450 fimc->vid_cap.subdev = sd;
1451 v4l2_set_subdevdata(sd, fimc);
1452 sd->entity.ops = &fimc_sd_media_ops;
1453 return 0;
1454sd_err:
1455 media_entity_cleanup(&sd->entity);
1456me_err:
1457 kfree(sd);
1458 return ret;
1459}
1460
1461static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1462{
1463 struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1464
1465 if (!sd)
1466 return;
1467 media_entity_cleanup(&sd->entity);
1468 v4l2_device_unregister_subdev(sd);
1469 kfree(sd);
Sylwester Nawrocki64c570f2011-09-11 10:36:13 -03001470 fimc->vid_cap.subdev = NULL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001471}
1472
1473/* Set default format at the sensor and host interface */
1474static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1475{
1476 struct v4l2_format fmt = {
1477 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1478 .fmt.pix_mp = {
1479 .width = 640,
1480 .height = 480,
1481 .pixelformat = V4L2_PIX_FMT_YUYV,
1482 .field = V4L2_FIELD_NONE,
1483 .colorspace = V4L2_COLORSPACE_JPEG,
1484 },
1485 };
1486
1487 return fimc_capture_set_format(fimc, &fmt);
1488}
1489
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001490/* fimc->lock must be already initialized */
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001491int fimc_register_capture_device(struct fimc_dev *fimc,
1492 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001493{
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001494 struct video_device *vfd;
1495 struct fimc_vid_cap *vid_cap;
1496 struct fimc_ctx *ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001497 struct vb2_queue *q;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001498 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001499
1500 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1501 if (!ctx)
1502 return -ENOMEM;
1503
1504 ctx->fimc_dev = fimc;
1505 ctx->in_path = FIMC_CAMERA;
1506 ctx->out_path = FIMC_DMA;
1507 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001508 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1509 ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001510
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001511 vfd = video_device_alloc();
1512 if (!vfd) {
1513 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001514 goto err_vd_alloc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001515 }
1516
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001517 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1518 dev_name(&fimc->pdev->dev));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001519
1520 vfd->fops = &fimc_capture_fops;
1521 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001522 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001523 vfd->minor = -1;
1524 vfd->release = video_device_release;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001525 vfd->lock = &fimc->lock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001526 video_set_drvdata(vfd, fimc);
1527
1528 vid_cap = &fimc->vid_cap;
1529 vid_cap->vfd = vfd;
1530 vid_cap->active_buf_cnt = 0;
1531 vid_cap->reqbufs_count = 0;
1532 vid_cap->refcnt = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001533
1534 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1535 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1536 spin_lock_init(&ctx->slock);
1537 vid_cap->ctx = ctx;
1538
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001539 q = &fimc->vid_cap.vbq;
1540 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001541 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001542 q->io_modes = VB2_MMAP | VB2_USERPTR;
1543 q->drv_priv = fimc->vid_cap.ctx;
1544 q->ops = &fimc_capture_qops;
1545 q->mem_ops = &vb2_dma_contig_memops;
1546 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1547
1548 vb2_queue_init(q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001549
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001550 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1551 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1552 if (ret)
1553 goto err_ent;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001554 ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1555 if (ret)
1556 goto err_sd_reg;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001557
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001558 vfd->ctrl_handler = &ctx->ctrl_handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001559 return 0;
1560
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001561err_sd_reg:
1562 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001563err_ent:
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001564 video_device_release(vfd);
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001565err_vd_alloc:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001566 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001567 return ret;
1568}
1569
1570void fimc_unregister_capture_device(struct fimc_dev *fimc)
1571{
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001572 struct video_device *vfd = fimc->vid_cap.vfd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001573
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001574 if (vfd) {
1575 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001576 /* Can also be called if video device was
1577 not registered */
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001578 video_unregister_device(vfd);
1579 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001580 fimc_destroy_capture_subdev(fimc);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001581 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001582 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001583}