blob: 510cfab477fff48c57ddd2d9151b53ec144f91ea [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 Nawrockief7af592010-12-08 14:05:08 -0300249static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300250{
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300251 if (!fr || plane >= fr->fmt->memplanes)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300252 return 0;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300253 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300254}
255
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -0300256static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
257 unsigned int *num_buffers, unsigned int *num_planes,
258 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300259{
260 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300261 struct fimc_fmt *fmt = ctx->d_frame.fmt;
262 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300263
264 if (!fmt)
265 return -EINVAL;
266
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300267 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300268
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300269 for (i = 0; i < fmt->memplanes; i++) {
270 sizes[i] = get_plane_size(&ctx->d_frame, i);
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300271 allocators[i] = ctx->fimc_dev->alloc_ctx;
272 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300273
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300274 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300275}
276
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300277static int buffer_prepare(struct vb2_buffer *vb)
278{
279 struct vb2_queue *vq = vb->vb2_queue;
280 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300281 int i;
282
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300283 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300284 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300285
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300286 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300287 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300288
289 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300290 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
291 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300292 vb2_plane_size(vb, i), size);
293 return -EINVAL;
294 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300295 vb2_set_plane_payload(vb, i, size);
296 }
297
298 return 0;
299}
300
301static void buffer_queue(struct vb2_buffer *vb)
302{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300303 struct fimc_vid_buffer *buf
304 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300305 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
306 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300307 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
308 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300309 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300310
311 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300312 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300313
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300314 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
315 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
316 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300317 /* Setup the buffer directly for processing. */
318 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
319 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300320
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300321 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
322 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300323 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300324
325 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
326 vid_cap->buf_index = 0;
327 } else {
328 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300329 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300330
331 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
332
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300333
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300334 if (vb2_is_streaming(&vid_cap->vbq) &&
335 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300336 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300337 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300338 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300339
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300340 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
341 fimc_pipeline_s_stream(fimc, 1);
342 return;
343 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300344 spin_unlock_irqrestore(&fimc->slock, flags);
345}
346
347static void fimc_lock(struct vb2_queue *vq)
348{
349 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
350 mutex_lock(&ctx->fimc_dev->lock);
351}
352
353static void fimc_unlock(struct vb2_queue *vq)
354{
355 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
356 mutex_unlock(&ctx->fimc_dev->lock);
357}
358
359static struct vb2_ops fimc_capture_qops = {
360 .queue_setup = queue_setup,
361 .buf_prepare = buffer_prepare,
362 .buf_queue = buffer_queue,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300363 .wait_prepare = fimc_unlock,
364 .wait_finish = fimc_lock,
365 .start_streaming = start_streaming,
366 .stop_streaming = stop_streaming,
367};
368
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300369/**
370 * fimc_capture_ctrls_create - initialize the control handler
371 * Initialize the capture video node control handler and fill it
372 * with the FIMC controls. Inherit any sensor's controls if the
373 * 'user_subdev_api' flag is false (default behaviour).
374 * This function need to be called with the graph mutex held.
375 */
376int fimc_capture_ctrls_create(struct fimc_dev *fimc)
377{
378 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
379 int ret;
380
381 if (WARN_ON(vid_cap->ctx == NULL))
382 return -ENXIO;
383 if (vid_cap->ctx->ctrls_rdy)
384 return 0;
385
386 ret = fimc_ctrls_create(vid_cap->ctx);
387 if (ret || vid_cap->user_subdev_api)
388 return ret;
389
390 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
391 fimc->pipeline.sensor->ctrl_handler);
392}
393
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300394static int fimc_capture_set_default_format(struct fimc_dev *fimc);
395
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300396static int fimc_capture_open(struct file *file)
397{
398 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300399 int ret = v4l2_fh_open(file);
400
401 if (ret)
402 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300403
404 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
405
406 /* Return if the corresponding video mem2mem node is already opened. */
407 if (fimc_m2m_active(fimc))
408 return -EBUSY;
409
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300410 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300411 pm_runtime_get_sync(&fimc->pdev->dev);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300412
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300413 if (++fimc->vid_cap.refcnt == 1) {
414 ret = fimc_pipeline_initialize(fimc,
415 &fimc->vid_cap.vfd->entity, true);
416 if (ret < 0) {
417 dev_err(&fimc->pdev->dev,
418 "Video pipeline initialization failed\n");
419 pm_runtime_put_sync(&fimc->pdev->dev);
420 fimc->vid_cap.refcnt--;
421 v4l2_fh_release(file);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300422 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300423 return ret;
424 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300425 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300426
427 if (!ret && !fimc->vid_cap.user_subdev_api)
428 ret = fimc_capture_set_default_format(fimc);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300429 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300430 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300431}
432
433static int fimc_capture_close(struct file *file)
434{
435 struct fimc_dev *fimc = video_drvdata(file);
436
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300437 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
438
439 if (--fimc->vid_cap.refcnt == 0) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300440 clear_bit(ST_CAPT_BUSY, &fimc->state);
441 fimc_stop_capture(fimc, false);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300442 fimc_pipeline_shutdown(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300443 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300444 }
445
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300446 pm_runtime_put(&fimc->pdev->dev);
447
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300448 if (fimc->vid_cap.refcnt == 0) {
449 vb2_queue_release(&fimc->vid_cap.vbq);
450 fimc_ctrls_delete(fimc->vid_cap.ctx);
451 }
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300452 return v4l2_fh_release(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300453}
454
455static unsigned int fimc_capture_poll(struct file *file,
456 struct poll_table_struct *wait)
457{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300458 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300459
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300460 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300461}
462
463static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
464{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300465 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300466
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300467 return vb2_mmap(&fimc->vid_cap.vbq, vma);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300468}
469
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300470static const struct v4l2_file_operations fimc_capture_fops = {
471 .owner = THIS_MODULE,
472 .open = fimc_capture_open,
473 .release = fimc_capture_close,
474 .poll = fimc_capture_poll,
475 .unlocked_ioctl = video_ioctl2,
476 .mmap = fimc_capture_mmap,
477};
478
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300479/*
480 * Format and crop negotiation helpers
481 */
482
483static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
484 u32 *width, u32 *height,
485 u32 *code, u32 *fourcc, int pad)
486{
487 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
488 struct fimc_dev *fimc = ctx->fimc_dev;
489 struct samsung_fimc_variant *var = fimc->variant;
490 struct fimc_pix_limit *pl = var->pix_limit;
491 struct fimc_frame *dst = &ctx->d_frame;
492 u32 depth, min_w, max_w, min_h, align_h = 3;
493 u32 mask = FMT_FLAGS_CAM;
494 struct fimc_fmt *ffmt;
495
496 /* Color conversion from/to JPEG is not supported */
497 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
498 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
499 *code = V4L2_MBUS_FMT_JPEG_1X8;
500
501 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
502 mask |= FMT_FLAGS_M2M;
503
504 ffmt = fimc_find_format(fourcc, code, mask, 0);
505 if (WARN_ON(!ffmt))
506 return NULL;
507 if (code)
508 *code = ffmt->mbus_code;
509 if (fourcc)
510 *fourcc = ffmt->fourcc;
511
512 if (pad == FIMC_SD_PAD_SINK) {
513 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
514 pl->scaler_dis_w : pl->scaler_en_w;
515 /* Apply the camera input interface pixel constraints */
516 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
517 height, max_t(u32, *height, 32),
518 FIMC_CAMIF_MAX_HEIGHT,
519 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
520 0);
521 return ffmt;
522 }
523 /* Can't scale or crop in transparent (JPEG) transfer mode */
524 if (fimc_fmt_is_jpeg(ffmt->color)) {
525 *width = ctx->s_frame.f_width;
526 *height = ctx->s_frame.f_height;
527 return ffmt;
528 }
529 /* Apply the scaler and the output DMA constraints */
530 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
531 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
532 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300533 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300534 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
535
536 depth = fimc_get_format_depth(ffmt);
537 v4l_bound_align_image(width, min_w, max_w,
538 ffs(var->min_out_pixsize) - 1,
539 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
540 align_h,
541 64/(ALIGN(depth, 8)));
542
543 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
544 pad, code ? *code : 0, *width, *height,
545 dst->f_width, dst->f_height);
546
547 return ffmt;
548}
549
550static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
551 int pad)
552{
553 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
554 struct fimc_dev *fimc = ctx->fimc_dev;
555 struct samsung_fimc_variant *var = fimc->variant;
556 struct fimc_pix_limit *pl = var->pix_limit;
557 struct fimc_frame *sink = &ctx->s_frame;
558 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
559 u32 align_sz = 0, align_h = 4;
560 u32 max_sc_h, max_sc_v;
561
562 /* In JPEG transparent transfer mode cropping is not supported */
563 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
564 r->width = sink->f_width;
565 r->height = sink->f_height;
566 r->left = r->top = 0;
567 return;
568 }
569 if (pad == FIMC_SD_PAD_SOURCE) {
570 if (ctx->rotation != 90 && ctx->rotation != 270)
571 align_h = 1;
572 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
573 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
574 min_sz = var->min_out_pixsize;
575 } else {
576 u32 depth = fimc_get_format_depth(sink->fmt);
577 align_sz = 64/ALIGN(depth, 8);
578 min_sz = var->min_inp_pixsize;
579 min_w = min_h = min_sz;
580 max_sc_h = max_sc_v = 1;
581 }
582 /*
583 * For the crop rectangle at source pad the following constraints
584 * must be met:
585 * - it must fit in the sink pad format rectangle (f_width/f_height);
586 * - maximum downscaling ratio is 64;
587 * - maximum crop size depends if the rotator is used or not;
588 * - the sink pad format width/height must be 4 multiple of the
589 * prescaler ratios determined by sink pad size and source pad crop,
590 * the prescaler ratio is returned by fimc_get_scaler_factor().
591 */
592 max_w = min_t(u32,
593 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
594 rotate ? sink->f_height : sink->f_width);
595 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
596 if (pad == FIMC_SD_PAD_SOURCE) {
597 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
598 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
599 if (rotate) {
600 swap(max_sc_h, max_sc_v);
601 swap(min_w, min_h);
602 }
603 }
604 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
605 &r->height, min_h, max_h, align_h,
606 align_sz);
607 /* Adjust left/top if cropping rectangle is out of bounds */
608 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
609 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
610 r->left = round_down(r->left, var->hor_offs_align);
611
612 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
613 pad, r->left, r->top, r->width, r->height,
614 sink->f_width, sink->f_height);
615}
616
617/*
618 * The video node ioctl operations
619 */
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300620static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
621 struct v4l2_capability *cap)
622{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300623 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300624
625 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
626 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
627 cap->bus_info[0] = 0;
Sylwester Nawrocki8f401542011-07-07 06:42:05 -0300628 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300629
630 return 0;
631}
632
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300633static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
634 struct v4l2_fmtdesc *f)
635{
636 struct fimc_fmt *fmt;
637
638 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
639 f->index);
640 if (!fmt)
641 return -EINVAL;
642 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
643 f->pixelformat = fmt->fourcc;
644 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
645 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
646 return 0;
647}
648
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300649/**
650 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
651 * elements
652 * @ctx: FIMC capture context
653 * @tfmt: media bus format to try/set on subdevs
654 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
655 * @set: true to set format on subdevs, false to try only
656 */
657static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
658 struct v4l2_mbus_framefmt *tfmt,
659 struct fimc_fmt **fmt_id,
660 bool set)
661{
662 struct fimc_dev *fimc = ctx->fimc_dev;
663 struct v4l2_subdev *sd = fimc->pipeline.sensor;
664 struct v4l2_subdev *csis = fimc->pipeline.csis;
665 struct v4l2_subdev_format sfmt;
666 struct v4l2_mbus_framefmt *mf = &sfmt.format;
667 struct fimc_fmt *ffmt = NULL;
668 int ret, i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300669
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300670 if (WARN_ON(!sd || !tfmt))
671 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300672
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300673 memset(&sfmt, 0, sizeof(sfmt));
674 sfmt.format = *tfmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300675
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300676 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
677 while (1) {
678 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
679 FMT_FLAGS_CAM, i++);
680 if (ffmt == NULL) {
681 /*
682 * Notify user-space if common pixel code for
683 * host and sensor does not exist.
684 */
685 return -EINVAL;
686 }
687 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300688
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300689 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
690 if (ret)
691 return ret;
692 if (mf->code != tfmt->code) {
693 mf->code = 0;
694 continue;
695 }
696 if (mf->width != tfmt->width || mf->width != tfmt->width) {
697 u32 fcc = ffmt->fourcc;
698 tfmt->width = mf->width;
699 tfmt->height = mf->height;
700 ffmt = fimc_capture_try_format(ctx,
701 &tfmt->width, &tfmt->height,
702 NULL, &fcc, FIMC_SD_PAD_SOURCE);
703 if (ffmt && ffmt->mbus_code)
704 mf->code = ffmt->mbus_code;
705 if (mf->width != tfmt->width || mf->width != tfmt->width)
706 continue;
707 tfmt->code = mf->code;
708 }
709 if (csis)
710 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300711
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300712 if (mf->code == tfmt->code &&
713 mf->width == tfmt->width && mf->width == tfmt->width)
714 break;
715 }
716
717 if (fmt_id && ffmt)
718 *fmt_id = ffmt;
719 *tfmt = *mf;
720
721 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
722 return 0;
723}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300724
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300725static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
726 struct v4l2_format *f)
727{
728 struct fimc_dev *fimc = video_drvdata(file);
729 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
730
731 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
732 return -EINVAL;
733
734 return fimc_fill_format(&ctx->d_frame, f);
735}
736
737static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
738 struct v4l2_format *f)
739{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300740 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300741 struct fimc_dev *fimc = video_drvdata(file);
742 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300743 struct v4l2_mbus_framefmt mf;
744 struct fimc_fmt *ffmt = NULL;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300745
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300746 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
747 return -EINVAL;
748
749 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
750 fimc_capture_try_format(ctx, &pix->width, &pix->height,
751 NULL, &pix->pixelformat,
752 FIMC_SD_PAD_SINK);
753 ctx->s_frame.f_width = pix->width;
754 ctx->s_frame.f_height = pix->height;
755 }
756 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
757 NULL, &pix->pixelformat,
758 FIMC_SD_PAD_SOURCE);
759 if (!ffmt)
760 return -EINVAL;
761
762 if (!fimc->vid_cap.user_subdev_api) {
763 mf.width = pix->width;
764 mf.height = pix->height;
765 mf.code = ffmt->mbus_code;
766 fimc_md_graph_lock(fimc);
767 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
768 fimc_md_graph_unlock(fimc);
769
770 pix->width = mf.width;
771 pix->height = mf.height;
772 if (ffmt)
773 pix->pixelformat = ffmt->fourcc;
774 }
775
776 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300777 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300778}
779
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -0300780static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
781{
782 ctx->scaler.enabled = !jpeg;
783 fimc_ctrls_activate(ctx, !jpeg);
784
785 if (jpeg)
786 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
787 else
788 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
789}
790
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300791static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
792{
793 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
794 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
795 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
796 struct fimc_frame *ff = &ctx->d_frame;
797 struct fimc_fmt *s_fmt = NULL;
798 int ret, i;
799
800 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
801 return -EINVAL;
802 if (vb2_is_busy(&fimc->vid_cap.vbq))
803 return -EBUSY;
804
805 /* Pre-configure format at camera interface input, for JPEG only */
806 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
807 fimc_capture_try_format(ctx, &pix->width, &pix->height,
808 NULL, &pix->pixelformat,
809 FIMC_SD_PAD_SINK);
810 ctx->s_frame.f_width = pix->width;
811 ctx->s_frame.f_height = pix->height;
812 }
813 /* Try the format at the scaler and the DMA output */
814 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
815 NULL, &pix->pixelformat,
816 FIMC_SD_PAD_SOURCE);
817 if (!ff->fmt)
818 return -EINVAL;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -0300819
820 /* Update RGB Alpha control state and value range */
821 fimc_alpha_ctrl_update(ctx);
822
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300823 /* Try to match format at the host and the sensor */
824 if (!fimc->vid_cap.user_subdev_api) {
825 mf->code = ff->fmt->mbus_code;
826 mf->width = pix->width;
827 mf->height = pix->height;
828
829 fimc_md_graph_lock(fimc);
830 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
831 fimc_md_graph_unlock(fimc);
832 if (ret)
833 return ret;
834 pix->width = mf->width;
835 pix->height = mf->height;
836 }
837 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
838 for (i = 0; i < ff->fmt->colplanes; i++)
839 ff->payload[i] =
840 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
841
842 set_frame_bounds(ff, pix->width, pix->height);
843 /* Reset the composition rectangle if not yet configured */
844 if (!(ctx->state & FIMC_DST_CROP))
845 set_frame_crop(ff, 0, 0, pix->width, pix->height);
846
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -0300847 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
848
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300849 /* Reset cropping and set format at the camera interface input */
850 if (!fimc->vid_cap.user_subdev_api) {
851 ctx->s_frame.fmt = s_fmt;
852 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
853 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
854 }
855
856 return ret;
857}
858
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300859static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
860 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300861{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300862 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300863
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300864 return fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300865}
866
867static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300868 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300869{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300870 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300871 struct v4l2_subdev *sd = fimc->pipeline.sensor;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300872
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300873 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300874 return -EINVAL;
875
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300876 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300877 if (sd)
878 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300879 return 0;
880}
881
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300882static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300883{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300884 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300885}
886
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300887static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300888{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300889 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300890 return 0;
891}
892
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300893/**
894 * fimc_pipeline_validate - check for formats inconsistencies
895 * between source and sink pad of each link
896 *
897 * Return 0 if all formats match or -EPIPE otherwise.
898 */
899static int fimc_pipeline_validate(struct fimc_dev *fimc)
900{
901 struct v4l2_subdev_format sink_fmt, src_fmt;
902 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
903 struct v4l2_subdev *sd;
904 struct media_pad *pad;
905 int ret;
906
907 /* Start with the video capture node pad */
908 pad = media_entity_remote_source(&vid_cap->vd_pad);
909 if (pad == NULL)
910 return -EPIPE;
911 /* FIMC.{N} subdevice */
912 sd = media_entity_to_v4l2_subdev(pad->entity);
913
914 while (1) {
915 /* Retrieve format at the sink pad */
916 pad = &sd->entity.pads[0];
917 if (!(pad->flags & MEDIA_PAD_FL_SINK))
918 break;
919 /* Don't call FIMC subdev operation to avoid nested locking */
920 if (sd == fimc->vid_cap.subdev) {
921 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
922 sink_fmt.format.width = ff->f_width;
923 sink_fmt.format.height = ff->f_height;
924 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
925 } else {
926 sink_fmt.pad = pad->index;
927 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
928 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
929 if (ret < 0 && ret != -ENOIOCTLCMD)
930 return -EPIPE;
931 }
932 /* Retrieve format at the source pad */
933 pad = media_entity_remote_source(pad);
934 if (pad == NULL ||
935 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
936 break;
937
938 sd = media_entity_to_v4l2_subdev(pad->entity);
939 src_fmt.pad = pad->index;
940 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
941 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
942 if (ret < 0 && ret != -ENOIOCTLCMD)
943 return -EPIPE;
944
945 if (src_fmt.format.width != sink_fmt.format.width ||
946 src_fmt.format.height != sink_fmt.format.height ||
947 src_fmt.format.code != sink_fmt.format.code)
948 return -EPIPE;
949 }
950 return 0;
951}
952
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300953static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300954 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300955{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300956 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300957 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300958 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300959
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300960 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300961 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300962
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300963 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300964
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300965 if (fimc->vid_cap.user_subdev_api) {
966 ret = fimc_pipeline_validate(fimc);
967 if (ret)
968 return ret;
969 }
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300970 return vb2_streamon(&fimc->vid_cap.vbq, type);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300971}
972
973static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300974 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300975{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300976 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300977 struct v4l2_subdev *sd = fimc->pipeline.sensor;
978 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300979
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300980 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
981 if (ret == 0)
982 media_entity_pipeline_stop(&sd->entity);
983 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300984}
985
986static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300987 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300988{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300989 struct fimc_dev *fimc = video_drvdata(file);
990 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300991
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300992 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300993 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300994 return ret;
995}
996
997static int fimc_cap_querybuf(struct file *file, void *priv,
998 struct v4l2_buffer *buf)
999{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001000 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001001
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001002 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001003}
1004
1005static int fimc_cap_qbuf(struct file *file, void *priv,
1006 struct v4l2_buffer *buf)
1007{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001008 struct fimc_dev *fimc = video_drvdata(file);
1009
1010 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001011}
1012
1013static int fimc_cap_dqbuf(struct file *file, void *priv,
1014 struct v4l2_buffer *buf)
1015{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001016 struct fimc_dev *fimc = video_drvdata(file);
1017
1018 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001019}
1020
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001021static int fimc_cap_cropcap(struct file *file, void *fh,
1022 struct v4l2_cropcap *cr)
1023{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001024 struct fimc_dev *fimc = video_drvdata(file);
1025 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001026
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001027 if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001028 return -EINVAL;
1029
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001030 cr->bounds.left = 0;
1031 cr->bounds.top = 0;
1032 cr->bounds.width = f->o_width;
1033 cr->bounds.height = f->o_height;
1034 cr->defrect = cr->bounds;
1035
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001036 return 0;
1037}
1038
1039static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr)
1040{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001041 struct fimc_dev *fimc = video_drvdata(file);
1042 struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001043
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001044 cr->c.left = f->offs_h;
1045 cr->c.top = f->offs_v;
1046 cr->c.width = f->width;
1047 cr->c.height = f->height;
1048
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001049 return 0;
1050}
1051
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001052static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001053{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001054 struct fimc_dev *fimc = video_drvdata(file);
1055 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001056 struct fimc_frame *ff;
1057 unsigned long flags;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001058
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001059 fimc_capture_try_crop(ctx, &cr->c, FIMC_SD_PAD_SINK);
1060 ff = &ctx->s_frame;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001061
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001062 spin_lock_irqsave(&fimc->slock, flags);
1063 set_frame_crop(ff, cr->c.left, cr->c.top, cr->c.width, cr->c.height);
1064 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1065 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001066
1067 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001068}
1069
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001070static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1071 .vidioc_querycap = fimc_vidioc_querycap_capture,
1072
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001073 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001074 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001075 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001076 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001077
1078 .vidioc_reqbufs = fimc_cap_reqbufs,
1079 .vidioc_querybuf = fimc_cap_querybuf,
1080
1081 .vidioc_qbuf = fimc_cap_qbuf,
1082 .vidioc_dqbuf = fimc_cap_dqbuf,
1083
1084 .vidioc_streamon = fimc_cap_streamon,
1085 .vidioc_streamoff = fimc_cap_streamoff,
1086
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001087 .vidioc_g_crop = fimc_cap_g_crop,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001088 .vidioc_s_crop = fimc_cap_s_crop,
Sylwester Nawrockie004e022010-11-25 11:01:51 -03001089 .vidioc_cropcap = fimc_cap_cropcap,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001090
1091 .vidioc_enum_input = fimc_cap_enum_input,
1092 .vidioc_s_input = fimc_cap_s_input,
1093 .vidioc_g_input = fimc_cap_g_input,
1094};
1095
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001096/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001097static int fimc_link_setup(struct media_entity *entity,
1098 const struct media_pad *local,
1099 const struct media_pad *remote, u32 flags)
1100{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001101 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1102 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1103
1104 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1105 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001106
1107 if (WARN_ON(fimc == NULL))
1108 return 0;
1109
1110 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1111 local->entity->name, remote->entity->name, flags,
1112 fimc->vid_cap.input);
1113
1114 if (flags & MEDIA_LNK_FL_ENABLED) {
1115 if (fimc->vid_cap.input != 0)
1116 return -EBUSY;
1117 fimc->vid_cap.input = sd->grp_id;
1118 return 0;
1119 }
1120
1121 fimc->vid_cap.input = 0;
1122 return 0;
1123}
1124
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001125static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001126 .link_setup = fimc_link_setup,
1127};
1128
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001129/**
1130 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1131 * @sd: pointer to a subdev generating the notification
1132 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1133 * @arg: pointer to an u32 type integer that stores the frame payload value
1134 *
1135 * The End Of Frame notification sent by sensor subdev in its still capture
1136 * mode. If there is only a single VSYNC generated by the sensor at the
1137 * beginning of a frame transmission, FIMC does not issue the LastIrq
1138 * (end of frame) interrupt. And this notification is used to complete the
1139 * frame capture and returning a buffer to user-space. Subdev drivers should
1140 * call this notification from their last 'End of frame capture' interrupt.
1141 */
1142void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1143 void *arg)
1144{
1145 struct fimc_sensor_info *sensor;
1146 struct fimc_vid_buffer *buf;
1147 struct fimc_md *fmd;
1148 struct fimc_dev *fimc;
1149 unsigned long flags;
1150
1151 if (sd == NULL)
1152 return;
1153
1154 sensor = v4l2_get_subdev_hostdata(sd);
1155 fmd = entity_to_fimc_mdev(&sd->entity);
1156
1157 spin_lock_irqsave(&fmd->slock, flags);
1158 fimc = sensor ? sensor->host : NULL;
1159
1160 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1161 test_bit(ST_CAPT_PEND, &fimc->state)) {
1162 unsigned long irq_flags;
1163 spin_lock_irqsave(&fimc->slock, irq_flags);
1164 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1165 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1166 struct fimc_vid_buffer, list);
1167 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1168 }
1169 fimc_capture_irq_handler(fimc, true);
1170 fimc_deactivate_capture(fimc);
1171 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1172 }
1173 spin_unlock_irqrestore(&fmd->slock, flags);
1174}
1175
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001176static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1177 struct v4l2_subdev_fh *fh,
1178 struct v4l2_subdev_mbus_code_enum *code)
1179{
1180 struct fimc_fmt *fmt;
1181
1182 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1183 if (!fmt)
1184 return -EINVAL;
1185 code->code = fmt->mbus_code;
1186 return 0;
1187}
1188
1189static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1190 struct v4l2_subdev_fh *fh,
1191 struct v4l2_subdev_format *fmt)
1192{
1193 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1194 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1195 struct v4l2_mbus_framefmt *mf;
1196 struct fimc_frame *ff;
1197
1198 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1199 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1200 fmt->format = *mf;
1201 return 0;
1202 }
1203 mf = &fmt->format;
1204 mf->colorspace = V4L2_COLORSPACE_JPEG;
1205 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1206
1207 mutex_lock(&fimc->lock);
1208 /* The pixel code is same on both input and output pad */
1209 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1210 mf->code = ctx->s_frame.fmt->mbus_code;
1211 mf->width = ff->f_width;
1212 mf->height = ff->f_height;
1213 mutex_unlock(&fimc->lock);
1214
1215 return 0;
1216}
1217
1218static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1219 struct v4l2_subdev_fh *fh,
1220 struct v4l2_subdev_format *fmt)
1221{
1222 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1223 struct v4l2_mbus_framefmt *mf = &fmt->format;
1224 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1225 struct fimc_frame *ff;
1226 struct fimc_fmt *ffmt;
1227
1228 dbg("pad%d: code: 0x%x, %dx%d",
1229 fmt->pad, mf->code, mf->width, mf->height);
1230
1231 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1232 vb2_is_busy(&fimc->vid_cap.vbq))
1233 return -EBUSY;
1234
1235 mutex_lock(&fimc->lock);
1236 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1237 &mf->code, NULL, fmt->pad);
1238 mutex_unlock(&fimc->lock);
1239 mf->colorspace = V4L2_COLORSPACE_JPEG;
1240
1241 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1242 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1243 *mf = fmt->format;
1244 return 0;
1245 }
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001246 /* Update RGB Alpha control state and value range */
1247 fimc_alpha_ctrl_update(ctx);
1248
Sylwester Nawrockiee7160e2011-08-26 14:57:06 -03001249 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1250
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001251 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1252 &ctx->s_frame : &ctx->d_frame;
1253
1254 mutex_lock(&fimc->lock);
1255 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki393a23f2011-12-08 06:56:22 -03001256 fimc->vid_cap.mf = *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001257 ff->fmt = ffmt;
1258
1259 /* Reset the crop rectangle if required. */
1260 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1261 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1262
1263 if (fmt->pad == FIMC_SD_PAD_SINK)
1264 ctx->state &= ~FIMC_DST_CROP;
1265 mutex_unlock(&fimc->lock);
1266 return 0;
1267}
1268
1269static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1270 struct v4l2_subdev_fh *fh,
1271 struct v4l2_subdev_crop *crop)
1272{
1273 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1274 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1275 struct v4l2_rect *r = &crop->rect;
1276 struct fimc_frame *ff;
1277
1278 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1279 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1280 return 0;
1281 }
1282 ff = crop->pad == FIMC_SD_PAD_SINK ?
1283 &ctx->s_frame : &ctx->d_frame;
1284
1285 mutex_lock(&fimc->lock);
1286 r->left = ff->offs_h;
1287 r->top = ff->offs_v;
1288 r->width = ff->width;
1289 r->height = ff->height;
1290 mutex_unlock(&fimc->lock);
1291
1292 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1293 ff, crop->pad, r->left, r->top, r->width, r->height,
1294 ff->f_width, ff->f_height);
1295
1296 return 0;
1297}
1298
1299static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1300 struct v4l2_subdev_fh *fh,
1301 struct v4l2_subdev_crop *crop)
1302{
1303 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1304 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1305 struct v4l2_rect *r = &crop->rect;
1306 struct fimc_frame *ff;
1307 unsigned long flags;
1308
1309 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1310
1311 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1312 &ctx->d_frame : &ctx->s_frame;
1313
1314 mutex_lock(&fimc->lock);
1315 fimc_capture_try_crop(ctx, r, crop->pad);
1316
1317 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1318 mutex_lock(&fimc->lock);
1319 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1320 return 0;
1321 }
1322 spin_lock_irqsave(&fimc->slock, flags);
1323 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1324 if (crop->pad == FIMC_SD_PAD_SOURCE)
1325 ctx->state |= FIMC_DST_CROP;
1326
1327 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1328 spin_unlock_irqrestore(&fimc->slock, flags);
1329
1330 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1331 r->width, r->height);
1332
1333 mutex_unlock(&fimc->lock);
1334 return 0;
1335}
1336
1337static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1338 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1339 .get_fmt = fimc_subdev_get_fmt,
1340 .set_fmt = fimc_subdev_set_fmt,
1341 .get_crop = fimc_subdev_get_crop,
1342 .set_crop = fimc_subdev_set_crop,
1343};
1344
1345static struct v4l2_subdev_ops fimc_subdev_ops = {
1346 .pad = &fimc_subdev_pad_ops,
1347};
1348
1349static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1350 struct v4l2_device *v4l2_dev)
1351{
1352 struct v4l2_subdev *sd;
1353 int ret;
1354
1355 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1356 if (!sd)
1357 return -ENOMEM;
1358
1359 v4l2_subdev_init(sd, &fimc_subdev_ops);
1360 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1361 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1362
1363 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1364 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1365 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1366 fimc->vid_cap.sd_pads, 0);
1367 if (ret)
1368 goto me_err;
1369 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1370 if (ret)
1371 goto sd_err;
1372
1373 fimc->vid_cap.subdev = sd;
1374 v4l2_set_subdevdata(sd, fimc);
1375 sd->entity.ops = &fimc_sd_media_ops;
1376 return 0;
1377sd_err:
1378 media_entity_cleanup(&sd->entity);
1379me_err:
1380 kfree(sd);
1381 return ret;
1382}
1383
1384static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1385{
1386 struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1387
1388 if (!sd)
1389 return;
1390 media_entity_cleanup(&sd->entity);
1391 v4l2_device_unregister_subdev(sd);
1392 kfree(sd);
Sylwester Nawrocki64c570f2011-09-11 10:36:13 -03001393 fimc->vid_cap.subdev = NULL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001394}
1395
1396/* Set default format at the sensor and host interface */
1397static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1398{
1399 struct v4l2_format fmt = {
1400 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1401 .fmt.pix_mp = {
1402 .width = 640,
1403 .height = 480,
1404 .pixelformat = V4L2_PIX_FMT_YUYV,
1405 .field = V4L2_FIELD_NONE,
1406 .colorspace = V4L2_COLORSPACE_JPEG,
1407 },
1408 };
1409
1410 return fimc_capture_set_format(fimc, &fmt);
1411}
1412
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001413/* fimc->lock must be already initialized */
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001414int fimc_register_capture_device(struct fimc_dev *fimc,
1415 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001416{
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001417 struct video_device *vfd;
1418 struct fimc_vid_cap *vid_cap;
1419 struct fimc_ctx *ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001420 struct vb2_queue *q;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001421 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001422
1423 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1424 if (!ctx)
1425 return -ENOMEM;
1426
1427 ctx->fimc_dev = fimc;
1428 ctx->in_path = FIMC_CAMERA;
1429 ctx->out_path = FIMC_DMA;
1430 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001431 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1432 ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001433
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001434 vfd = video_device_alloc();
1435 if (!vfd) {
1436 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001437 goto err_vd_alloc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001438 }
1439
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001440 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1441 dev_name(&fimc->pdev->dev));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001442
1443 vfd->fops = &fimc_capture_fops;
1444 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001445 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001446 vfd->minor = -1;
1447 vfd->release = video_device_release;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001448 vfd->lock = &fimc->lock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001449 video_set_drvdata(vfd, fimc);
1450
1451 vid_cap = &fimc->vid_cap;
1452 vid_cap->vfd = vfd;
1453 vid_cap->active_buf_cnt = 0;
1454 vid_cap->reqbufs_count = 0;
1455 vid_cap->refcnt = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001456
1457 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1458 INIT_LIST_HEAD(&vid_cap->active_buf_q);
1459 spin_lock_init(&ctx->slock);
1460 vid_cap->ctx = ctx;
1461
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001462 q = &fimc->vid_cap.vbq;
1463 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001464 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001465 q->io_modes = VB2_MMAP | VB2_USERPTR;
1466 q->drv_priv = fimc->vid_cap.ctx;
1467 q->ops = &fimc_capture_qops;
1468 q->mem_ops = &vb2_dma_contig_memops;
1469 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1470
1471 vb2_queue_init(q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001472
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001473 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1474 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1475 if (ret)
1476 goto err_ent;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001477 ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1478 if (ret)
1479 goto err_sd_reg;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001480
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001481 vfd->ctrl_handler = &ctx->ctrl_handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001482 return 0;
1483
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001484err_sd_reg:
1485 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001486err_ent:
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001487 video_device_release(vfd);
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001488err_vd_alloc:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001489 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001490 return ret;
1491}
1492
1493void fimc_unregister_capture_device(struct fimc_dev *fimc)
1494{
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001495 struct video_device *vfd = fimc->vid_cap.vfd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001496
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001497 if (vfd) {
1498 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001499 /* Can also be called if video device was
1500 not registered */
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001501 video_unregister_device(vfd);
1502 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001503 fimc_destroy_capture_subdev(fimc);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001504 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001505 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001506}