blob: dc18ba510986d7819f49ad1831e6d61eefca0ce3 [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 Nawrockiee7160e52011-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);
Sylwester Nawrockiaa3331222012-03-16 11:47:51 -030086 if (suspend)
87 fimc->state |= (1 << ST_CAPT_SUSPENDED);
88 else
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030089 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030090
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030091 /* Release unused buffers */
92 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -030093 buf = fimc_pending_queue_pop(cap);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030094 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
95 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030096 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030097 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -030098 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030099 if (suspend)
100 fimc_pending_queue_add(cap, buf);
101 else
102 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300103 }
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300104
105 fimc_hw_reset(fimc);
106 cap->buf_index = 0;
107
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300108 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300109
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300110 if (streaming)
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300111 return fimc_pipeline_s_stream(fimc, 0);
112 else
113 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300114}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300115
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300116static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300117{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300118 unsigned long flags;
119
120 if (!fimc_capture_active(fimc))
121 return 0;
122
123 spin_lock_irqsave(&fimc->slock, flags);
124 set_bit(ST_CAPT_SHUT, &fimc->state);
125 fimc_deactivate_capture(fimc);
126 spin_unlock_irqrestore(&fimc->slock, flags);
127
128 wait_event_timeout(fimc->irq_queue,
129 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300130 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300131
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300132 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300133}
134
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300135/**
136 * fimc_capture_config_update - apply the camera interface configuration
137 *
138 * To be called from within the interrupt handler with fimc.slock
139 * spinlock held. It updates the camera pixel crop, rotation and
140 * image flip in H/W.
141 */
142int fimc_capture_config_update(struct fimc_ctx *ctx)
143{
144 struct fimc_dev *fimc = ctx->fimc_dev;
145 int ret;
146
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300147 if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300148 return 0;
149
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300150 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300151
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300152 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300153 if (ret)
154 return ret;
155
156 fimc_hw_set_prescaler(ctx);
157 fimc_hw_set_mainscaler(ctx);
158 fimc_hw_set_target_format(ctx);
159 fimc_hw_set_rotation(ctx);
160 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
161 fimc_hw_set_out_dma(ctx);
162 if (fimc->variant->has_alpha)
163 fimc_hw_set_rgb_alpha(ctx);
164
165 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300166 return ret;
167}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300168
169static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300170{
171 struct fimc_ctx *ctx = q->drv_priv;
172 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300173 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300174 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300175 int ret;
176
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300177 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300178
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300179 ret = fimc_init_capture(fimc);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300180 if (ret)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300181 goto error;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300182
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300183 set_bit(ST_CAPT_PEND, &fimc->state);
184
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300185 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
186
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300187 if (vid_cap->active_buf_cnt >= min_bufs &&
188 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300189 fimc_activate_capture(ctx);
190
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300191 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
192 fimc_pipeline_s_stream(fimc, 1);
193 }
194
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300195 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300196error:
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300197 fimc_capture_state_cleanup(fimc, false);
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300198 return ret;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300199}
200
201static int stop_streaming(struct vb2_queue *q)
202{
203 struct fimc_ctx *ctx = q->drv_priv;
204 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300205
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300206 if (!fimc_capture_active(fimc))
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300207 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300208
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300209 return fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300210}
211
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300212int fimc_capture_suspend(struct fimc_dev *fimc)
213{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300214 bool suspend = fimc_capture_busy(fimc);
215
216 int ret = fimc_stop_capture(fimc, suspend);
217 if (ret)
218 return ret;
219 return fimc_pipeline_shutdown(fimc);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300220}
221
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300222static void buffer_queue(struct vb2_buffer *vb);
223
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300224int fimc_capture_resume(struct fimc_dev *fimc)
225{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300226 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
227 struct fimc_vid_buffer *buf;
228 int i;
229
230 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
231 return 0;
232
233 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
234 vid_cap->buf_index = 0;
235 fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity,
236 false);
237 fimc_init_capture(fimc);
238
239 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
240
241 for (i = 0; i < vid_cap->reqbufs_count; i++) {
242 if (list_empty(&vid_cap->pending_buf_q))
243 break;
244 buf = fimc_pending_queue_pop(vid_cap);
245 buffer_queue(&buf->vb);
246 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300247 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300248
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300249}
250
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300251static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300252{
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300253 if (!fr || plane >= fr->fmt->memplanes)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300254 return 0;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300255 return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300256}
257
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300258static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
259 unsigned int *num_buffers, unsigned int *num_planes,
260 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300261{
262 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300263 struct fimc_fmt *fmt = ctx->d_frame.fmt;
264 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300265
266 if (!fmt)
267 return -EINVAL;
268
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300269 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300270
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300271 for (i = 0; i < fmt->memplanes; i++) {
272 sizes[i] = get_plane_size(&ctx->d_frame, i);
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300273 allocators[i] = ctx->fimc_dev->alloc_ctx;
274 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300275
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300276 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300277}
278
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300279static int buffer_prepare(struct vb2_buffer *vb)
280{
281 struct vb2_queue *vq = vb->vb2_queue;
282 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300283 int i;
284
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300285 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300286 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300287
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300288 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300289 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300290
291 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300292 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
293 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300294 vb2_plane_size(vb, i), size);
295 return -EINVAL;
296 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300297 vb2_set_plane_payload(vb, i, size);
298 }
299
300 return 0;
301}
302
303static void buffer_queue(struct vb2_buffer *vb)
304{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300305 struct fimc_vid_buffer *buf
306 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300307 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
308 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300309 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
310 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300311 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300312
313 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300314 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300315
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300316 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
317 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
318 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300319 /* Setup the buffer directly for processing. */
320 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
321 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300322
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300323 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
324 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300325 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300326
327 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
328 vid_cap->buf_index = 0;
329 } else {
330 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300331 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300332
333 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
334
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300335
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300336 if (vb2_is_streaming(&vid_cap->vbq) &&
337 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300338 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300339 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300340 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300341
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300342 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
343 fimc_pipeline_s_stream(fimc, 1);
344 return;
345 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300346 spin_unlock_irqrestore(&fimc->slock, flags);
347}
348
349static void fimc_lock(struct vb2_queue *vq)
350{
351 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
352 mutex_lock(&ctx->fimc_dev->lock);
353}
354
355static void fimc_unlock(struct vb2_queue *vq)
356{
357 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
358 mutex_unlock(&ctx->fimc_dev->lock);
359}
360
361static struct vb2_ops fimc_capture_qops = {
362 .queue_setup = queue_setup,
363 .buf_prepare = buffer_prepare,
364 .buf_queue = buffer_queue,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300365 .wait_prepare = fimc_unlock,
366 .wait_finish = fimc_lock,
367 .start_streaming = start_streaming,
368 .stop_streaming = stop_streaming,
369};
370
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300371/**
372 * fimc_capture_ctrls_create - initialize the control handler
373 * Initialize the capture video node control handler and fill it
374 * with the FIMC controls. Inherit any sensor's controls if the
375 * 'user_subdev_api' flag is false (default behaviour).
376 * This function need to be called with the graph mutex held.
377 */
378int fimc_capture_ctrls_create(struct fimc_dev *fimc)
379{
380 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
381 int ret;
382
383 if (WARN_ON(vid_cap->ctx == NULL))
384 return -ENXIO;
385 if (vid_cap->ctx->ctrls_rdy)
386 return 0;
387
388 ret = fimc_ctrls_create(vid_cap->ctx);
389 if (ret || vid_cap->user_subdev_api)
390 return ret;
391
392 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
393 fimc->pipeline.sensor->ctrl_handler);
394}
395
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300396static int fimc_capture_set_default_format(struct fimc_dev *fimc);
397
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300398static int fimc_capture_open(struct file *file)
399{
400 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300401 int ret = v4l2_fh_open(file);
402
403 if (ret)
404 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300405
406 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
407
408 /* Return if the corresponding video mem2mem node is already opened. */
409 if (fimc_m2m_active(fimc))
410 return -EBUSY;
411
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300412 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300413 pm_runtime_get_sync(&fimc->pdev->dev);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300414
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300415 if (++fimc->vid_cap.refcnt == 1) {
416 ret = fimc_pipeline_initialize(fimc,
417 &fimc->vid_cap.vfd->entity, true);
418 if (ret < 0) {
419 dev_err(&fimc->pdev->dev,
420 "Video pipeline initialization failed\n");
421 pm_runtime_put_sync(&fimc->pdev->dev);
422 fimc->vid_cap.refcnt--;
423 v4l2_fh_release(file);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300424 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300425 return ret;
426 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300427 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300428
429 if (!ret && !fimc->vid_cap.user_subdev_api)
430 ret = fimc_capture_set_default_format(fimc);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300431 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300432 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300433}
434
435static int fimc_capture_close(struct file *file)
436{
437 struct fimc_dev *fimc = video_drvdata(file);
438
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300439 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
440
441 if (--fimc->vid_cap.refcnt == 0) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300442 clear_bit(ST_CAPT_BUSY, &fimc->state);
443 fimc_stop_capture(fimc, false);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300444 fimc_pipeline_shutdown(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300445 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300446 }
447
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300448 pm_runtime_put(&fimc->pdev->dev);
449
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300450 if (fimc->vid_cap.refcnt == 0) {
451 vb2_queue_release(&fimc->vid_cap.vbq);
452 fimc_ctrls_delete(fimc->vid_cap.ctx);
453 }
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300454 return v4l2_fh_release(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300455}
456
457static unsigned int fimc_capture_poll(struct file *file,
458 struct poll_table_struct *wait)
459{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300460 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300461
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300462 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300463}
464
465static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
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_mmap(&fimc->vid_cap.vbq, vma);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300470}
471
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300472static const struct v4l2_file_operations fimc_capture_fops = {
473 .owner = THIS_MODULE,
474 .open = fimc_capture_open,
475 .release = fimc_capture_close,
476 .poll = fimc_capture_poll,
477 .unlocked_ioctl = video_ioctl2,
478 .mmap = fimc_capture_mmap,
479};
480
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300481/*
482 * Format and crop negotiation helpers
483 */
484
485static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
486 u32 *width, u32 *height,
487 u32 *code, u32 *fourcc, int pad)
488{
489 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
490 struct fimc_dev *fimc = ctx->fimc_dev;
491 struct samsung_fimc_variant *var = fimc->variant;
492 struct fimc_pix_limit *pl = var->pix_limit;
493 struct fimc_frame *dst = &ctx->d_frame;
494 u32 depth, min_w, max_w, min_h, align_h = 3;
495 u32 mask = FMT_FLAGS_CAM;
496 struct fimc_fmt *ffmt;
497
498 /* Color conversion from/to JPEG is not supported */
499 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
500 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
501 *code = V4L2_MBUS_FMT_JPEG_1X8;
502
503 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
504 mask |= FMT_FLAGS_M2M;
505
506 ffmt = fimc_find_format(fourcc, code, mask, 0);
507 if (WARN_ON(!ffmt))
508 return NULL;
509 if (code)
510 *code = ffmt->mbus_code;
511 if (fourcc)
512 *fourcc = ffmt->fourcc;
513
514 if (pad == FIMC_SD_PAD_SINK) {
515 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
516 pl->scaler_dis_w : pl->scaler_en_w;
517 /* Apply the camera input interface pixel constraints */
518 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
519 height, max_t(u32, *height, 32),
520 FIMC_CAMIF_MAX_HEIGHT,
521 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
522 0);
523 return ffmt;
524 }
525 /* Can't scale or crop in transparent (JPEG) transfer mode */
526 if (fimc_fmt_is_jpeg(ffmt->color)) {
527 *width = ctx->s_frame.f_width;
528 *height = ctx->s_frame.f_height;
529 return ffmt;
530 }
531 /* Apply the scaler and the output DMA constraints */
532 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
533 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
534 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300535 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300536 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
537
538 depth = fimc_get_format_depth(ffmt);
539 v4l_bound_align_image(width, min_w, max_w,
540 ffs(var->min_out_pixsize) - 1,
541 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
542 align_h,
543 64/(ALIGN(depth, 8)));
544
545 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
546 pad, code ? *code : 0, *width, *height,
547 dst->f_width, dst->f_height);
548
549 return ffmt;
550}
551
552static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
553 int pad)
554{
555 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
556 struct fimc_dev *fimc = ctx->fimc_dev;
557 struct samsung_fimc_variant *var = fimc->variant;
558 struct fimc_pix_limit *pl = var->pix_limit;
559 struct fimc_frame *sink = &ctx->s_frame;
560 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
561 u32 align_sz = 0, align_h = 4;
562 u32 max_sc_h, max_sc_v;
563
564 /* In JPEG transparent transfer mode cropping is not supported */
565 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
566 r->width = sink->f_width;
567 r->height = sink->f_height;
568 r->left = r->top = 0;
569 return;
570 }
571 if (pad == FIMC_SD_PAD_SOURCE) {
572 if (ctx->rotation != 90 && ctx->rotation != 270)
573 align_h = 1;
574 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
575 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
576 min_sz = var->min_out_pixsize;
577 } else {
578 u32 depth = fimc_get_format_depth(sink->fmt);
579 align_sz = 64/ALIGN(depth, 8);
580 min_sz = var->min_inp_pixsize;
581 min_w = min_h = min_sz;
582 max_sc_h = max_sc_v = 1;
583 }
584 /*
585 * For the crop rectangle at source pad the following constraints
586 * must be met:
587 * - it must fit in the sink pad format rectangle (f_width/f_height);
588 * - maximum downscaling ratio is 64;
589 * - maximum crop size depends if the rotator is used or not;
590 * - the sink pad format width/height must be 4 multiple of the
591 * prescaler ratios determined by sink pad size and source pad crop,
592 * the prescaler ratio is returned by fimc_get_scaler_factor().
593 */
594 max_w = min_t(u32,
595 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
596 rotate ? sink->f_height : sink->f_width);
597 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
598 if (pad == FIMC_SD_PAD_SOURCE) {
599 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
600 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
601 if (rotate) {
602 swap(max_sc_h, max_sc_v);
603 swap(min_w, min_h);
604 }
605 }
606 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
607 &r->height, min_h, max_h, align_h,
608 align_sz);
609 /* Adjust left/top if cropping rectangle is out of bounds */
610 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
611 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
612 r->left = round_down(r->left, var->hor_offs_align);
613
614 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
615 pad, r->left, r->top, r->width, r->height,
616 sink->f_width, sink->f_height);
617}
618
619/*
620 * The video node ioctl operations
621 */
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300622static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
623 struct v4l2_capability *cap)
624{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300625 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300626
627 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
628 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
629 cap->bus_info[0] = 0;
Sylwester Nawrocki8f401542011-07-07 06:42:05 -0300630 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300631
632 return 0;
633}
634
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300635static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
636 struct v4l2_fmtdesc *f)
637{
638 struct fimc_fmt *fmt;
639
640 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
641 f->index);
642 if (!fmt)
643 return -EINVAL;
644 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
645 f->pixelformat = fmt->fourcc;
646 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
647 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
648 return 0;
649}
650
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300651/**
652 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
653 * elements
654 * @ctx: FIMC capture context
655 * @tfmt: media bus format to try/set on subdevs
656 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
657 * @set: true to set format on subdevs, false to try only
658 */
659static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
660 struct v4l2_mbus_framefmt *tfmt,
661 struct fimc_fmt **fmt_id,
662 bool set)
663{
664 struct fimc_dev *fimc = ctx->fimc_dev;
665 struct v4l2_subdev *sd = fimc->pipeline.sensor;
666 struct v4l2_subdev *csis = fimc->pipeline.csis;
667 struct v4l2_subdev_format sfmt;
668 struct v4l2_mbus_framefmt *mf = &sfmt.format;
669 struct fimc_fmt *ffmt = NULL;
670 int ret, i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300671
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300672 if (WARN_ON(!sd || !tfmt))
673 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300674
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300675 memset(&sfmt, 0, sizeof(sfmt));
676 sfmt.format = *tfmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300677
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300678 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
679 while (1) {
680 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
681 FMT_FLAGS_CAM, i++);
682 if (ffmt == NULL) {
683 /*
684 * Notify user-space if common pixel code for
685 * host and sensor does not exist.
686 */
687 return -EINVAL;
688 }
689 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300690
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300691 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
692 if (ret)
693 return ret;
694 if (mf->code != tfmt->code) {
695 mf->code = 0;
696 continue;
697 }
Julia Lawallb1aa6082012-01-12 17:49:28 -0300698 if (mf->width != tfmt->width || mf->height != tfmt->height) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300699 u32 fcc = ffmt->fourcc;
700 tfmt->width = mf->width;
701 tfmt->height = mf->height;
702 ffmt = fimc_capture_try_format(ctx,
703 &tfmt->width, &tfmt->height,
704 NULL, &fcc, FIMC_SD_PAD_SOURCE);
705 if (ffmt && ffmt->mbus_code)
706 mf->code = ffmt->mbus_code;
Julia Lawallb1aa6082012-01-12 17:49:28 -0300707 if (mf->width != tfmt->width ||
708 mf->height != tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300709 continue;
710 tfmt->code = mf->code;
711 }
712 if (csis)
713 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300714
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300715 if (mf->code == tfmt->code &&
Julia Lawallb1aa6082012-01-12 17:49:28 -0300716 mf->width == tfmt->width && mf->height == tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300717 break;
718 }
719
720 if (fmt_id && ffmt)
721 *fmt_id = ffmt;
722 *tfmt = *mf;
723
724 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
725 return 0;
726}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300727
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300728static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
729 struct v4l2_format *f)
730{
731 struct fimc_dev *fimc = video_drvdata(file);
732 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
733
734 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
735 return -EINVAL;
736
737 return fimc_fill_format(&ctx->d_frame, f);
738}
739
740static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
741 struct v4l2_format *f)
742{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300743 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300744 struct fimc_dev *fimc = video_drvdata(file);
745 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300746 struct v4l2_mbus_framefmt mf;
747 struct fimc_fmt *ffmt = NULL;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300748
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300749 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
750 return -EINVAL;
751
752 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
753 fimc_capture_try_format(ctx, &pix->width, &pix->height,
754 NULL, &pix->pixelformat,
755 FIMC_SD_PAD_SINK);
756 ctx->s_frame.f_width = pix->width;
757 ctx->s_frame.f_height = pix->height;
758 }
759 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
760 NULL, &pix->pixelformat,
761 FIMC_SD_PAD_SOURCE);
762 if (!ffmt)
763 return -EINVAL;
764
765 if (!fimc->vid_cap.user_subdev_api) {
766 mf.width = pix->width;
767 mf.height = pix->height;
768 mf.code = ffmt->mbus_code;
769 fimc_md_graph_lock(fimc);
770 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
771 fimc_md_graph_unlock(fimc);
772
773 pix->width = mf.width;
774 pix->height = mf.height;
775 if (ffmt)
776 pix->pixelformat = ffmt->fourcc;
777 }
778
779 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300780 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300781}
782
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300783static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
784{
785 ctx->scaler.enabled = !jpeg;
786 fimc_ctrls_activate(ctx, !jpeg);
787
788 if (jpeg)
789 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
790 else
791 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
792}
793
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300794static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
795{
796 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
797 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
798 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
799 struct fimc_frame *ff = &ctx->d_frame;
800 struct fimc_fmt *s_fmt = NULL;
801 int ret, i;
802
803 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
804 return -EINVAL;
805 if (vb2_is_busy(&fimc->vid_cap.vbq))
806 return -EBUSY;
807
808 /* Pre-configure format at camera interface input, for JPEG only */
809 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
810 fimc_capture_try_format(ctx, &pix->width, &pix->height,
811 NULL, &pix->pixelformat,
812 FIMC_SD_PAD_SINK);
813 ctx->s_frame.f_width = pix->width;
814 ctx->s_frame.f_height = pix->height;
815 }
816 /* Try the format at the scaler and the DMA output */
817 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
818 NULL, &pix->pixelformat,
819 FIMC_SD_PAD_SOURCE);
820 if (!ff->fmt)
821 return -EINVAL;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -0300822
823 /* Update RGB Alpha control state and value range */
824 fimc_alpha_ctrl_update(ctx);
825
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300826 /* Try to match format at the host and the sensor */
827 if (!fimc->vid_cap.user_subdev_api) {
828 mf->code = ff->fmt->mbus_code;
829 mf->width = pix->width;
830 mf->height = pix->height;
831
832 fimc_md_graph_lock(fimc);
833 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
834 fimc_md_graph_unlock(fimc);
835 if (ret)
836 return ret;
837 pix->width = mf->width;
838 pix->height = mf->height;
839 }
840 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
841 for (i = 0; i < ff->fmt->colplanes; i++)
842 ff->payload[i] =
843 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
844
845 set_frame_bounds(ff, pix->width, pix->height);
846 /* Reset the composition rectangle if not yet configured */
847 if (!(ctx->state & FIMC_DST_CROP))
848 set_frame_crop(ff, 0, 0, pix->width, pix->height);
849
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300850 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
851
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300852 /* Reset cropping and set format at the camera interface input */
853 if (!fimc->vid_cap.user_subdev_api) {
854 ctx->s_frame.fmt = s_fmt;
855 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
856 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
857 }
858
859 return ret;
860}
861
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300862static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
863 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300864{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300865 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300866
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300867 return fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300868}
869
870static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300871 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300872{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300873 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300874 struct v4l2_subdev *sd = fimc->pipeline.sensor;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300875
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300876 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300877 return -EINVAL;
878
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300879 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300880 if (sd)
881 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300882 return 0;
883}
884
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300885static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300886{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300887 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300888}
889
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300890static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300891{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300892 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300893 return 0;
894}
895
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300896/**
897 * fimc_pipeline_validate - check for formats inconsistencies
898 * between source and sink pad of each link
899 *
900 * Return 0 if all formats match or -EPIPE otherwise.
901 */
902static int fimc_pipeline_validate(struct fimc_dev *fimc)
903{
904 struct v4l2_subdev_format sink_fmt, src_fmt;
905 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
906 struct v4l2_subdev *sd;
907 struct media_pad *pad;
908 int ret;
909
910 /* Start with the video capture node pad */
911 pad = media_entity_remote_source(&vid_cap->vd_pad);
912 if (pad == NULL)
913 return -EPIPE;
914 /* FIMC.{N} subdevice */
915 sd = media_entity_to_v4l2_subdev(pad->entity);
916
917 while (1) {
918 /* Retrieve format at the sink pad */
919 pad = &sd->entity.pads[0];
920 if (!(pad->flags & MEDIA_PAD_FL_SINK))
921 break;
922 /* Don't call FIMC subdev operation to avoid nested locking */
923 if (sd == fimc->vid_cap.subdev) {
924 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
925 sink_fmt.format.width = ff->f_width;
926 sink_fmt.format.height = ff->f_height;
927 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
928 } else {
929 sink_fmt.pad = pad->index;
930 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
931 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
932 if (ret < 0 && ret != -ENOIOCTLCMD)
933 return -EPIPE;
934 }
935 /* Retrieve format at the source pad */
936 pad = media_entity_remote_source(pad);
937 if (pad == NULL ||
938 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
939 break;
940
941 sd = media_entity_to_v4l2_subdev(pad->entity);
942 src_fmt.pad = pad->index;
943 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
944 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
945 if (ret < 0 && ret != -ENOIOCTLCMD)
946 return -EPIPE;
947
948 if (src_fmt.format.width != sink_fmt.format.width ||
949 src_fmt.format.height != sink_fmt.format.height ||
950 src_fmt.format.code != sink_fmt.format.code)
951 return -EPIPE;
952 }
953 return 0;
954}
955
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300956static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300957 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300958{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300959 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300960 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300961 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300962
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300963 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300964 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300965
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300966 media_entity_pipeline_start(&p->sensor->entity, p->pipe);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300967
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300968 if (fimc->vid_cap.user_subdev_api) {
969 ret = fimc_pipeline_validate(fimc);
970 if (ret)
971 return ret;
972 }
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300973 return vb2_streamon(&fimc->vid_cap.vbq, type);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300974}
975
976static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300977 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300978{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300979 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300980 struct v4l2_subdev *sd = fimc->pipeline.sensor;
981 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300982
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300983 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
984 if (ret == 0)
985 media_entity_pipeline_stop(&sd->entity);
986 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300987}
988
989static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300990 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300991{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300992 struct fimc_dev *fimc = video_drvdata(file);
993 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300994
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300995 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300996 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300997 return ret;
998}
999
1000static int fimc_cap_querybuf(struct file *file, void *priv,
1001 struct v4l2_buffer *buf)
1002{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001003 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001004
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001005 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001006}
1007
1008static int fimc_cap_qbuf(struct file *file, void *priv,
1009 struct v4l2_buffer *buf)
1010{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001011 struct fimc_dev *fimc = video_drvdata(file);
1012
1013 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001014}
1015
1016static int fimc_cap_dqbuf(struct file *file, void *priv,
1017 struct v4l2_buffer *buf)
1018{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001019 struct fimc_dev *fimc = video_drvdata(file);
1020
1021 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001022}
1023
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001024static int fimc_cap_create_bufs(struct file *file, void *priv,
1025 struct v4l2_create_buffers *create)
1026{
1027 struct fimc_dev *fimc = video_drvdata(file);
1028
1029 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1030}
1031
1032static int fimc_cap_prepare_buf(struct file *file, void *priv,
1033 struct v4l2_buffer *b)
1034{
1035 struct fimc_dev *fimc = video_drvdata(file);
1036
1037 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1038}
1039
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001040static int fimc_cap_g_selection(struct file *file, void *fh,
1041 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001042{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001043 struct fimc_dev *fimc = video_drvdata(file);
1044 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001045 struct fimc_frame *f = &ctx->s_frame;
1046
1047 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1048 return -EINVAL;
1049
1050 switch (s->target) {
1051 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1052 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1053 f = &ctx->d_frame;
1054 case V4L2_SEL_TGT_CROP_BOUNDS:
1055 case V4L2_SEL_TGT_CROP_DEFAULT:
1056 s->r.left = 0;
1057 s->r.top = 0;
1058 s->r.width = f->o_width;
1059 s->r.height = f->o_height;
1060 return 0;
1061
1062 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1063 f = &ctx->d_frame;
1064 case V4L2_SEL_TGT_CROP_ACTIVE:
1065 s->r.left = f->offs_h;
1066 s->r.top = f->offs_v;
1067 s->r.width = f->width;
1068 s->r.height = f->height;
1069 return 0;
1070 }
1071
1072 return -EINVAL;
1073}
1074
1075/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1076int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1077{
1078 if (a->left < b->left || a->top < b->top)
1079 return 0;
1080 if (a->left + a->width > b->left + b->width)
1081 return 0;
1082 if (a->top + a->height > b->top + b->height)
1083 return 0;
1084
1085 return 1;
1086}
1087
1088static int fimc_cap_s_selection(struct file *file, void *fh,
1089 struct v4l2_selection *s)
1090{
1091 struct fimc_dev *fimc = video_drvdata(file);
1092 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1093 struct v4l2_rect rect = s->r;
1094 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001095 unsigned long flags;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001096 unsigned int pad;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001097
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001098 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1099 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001100
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001101 switch (s->target) {
1102 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1103 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1104 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1105 f = &ctx->d_frame;
1106 pad = FIMC_SD_PAD_SOURCE;
1107 break;
1108 case V4L2_SEL_TGT_CROP_BOUNDS:
1109 case V4L2_SEL_TGT_CROP_DEFAULT:
1110 case V4L2_SEL_TGT_CROP_ACTIVE:
1111 f = &ctx->s_frame;
1112 pad = FIMC_SD_PAD_SINK;
1113 break;
1114 default:
1115 return -EINVAL;
1116 }
1117
1118 fimc_capture_try_crop(ctx, &rect, pad);
1119
1120 if (s->flags & V4L2_SEL_FLAG_LE &&
1121 !enclosed_rectangle(&rect, &s->r))
1122 return -ERANGE;
1123
1124 if (s->flags & V4L2_SEL_FLAG_GE &&
1125 !enclosed_rectangle(&s->r, &rect))
1126 return -ERANGE;
1127
1128 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001129 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001130 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1131 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001132 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001133
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001134 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001135 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001136}
1137
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001138static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1139 .vidioc_querycap = fimc_vidioc_querycap_capture,
1140
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001141 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001142 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001143 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001144 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001145
1146 .vidioc_reqbufs = fimc_cap_reqbufs,
1147 .vidioc_querybuf = fimc_cap_querybuf,
1148
1149 .vidioc_qbuf = fimc_cap_qbuf,
1150 .vidioc_dqbuf = fimc_cap_dqbuf,
1151
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001152 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1153 .vidioc_create_bufs = fimc_cap_create_bufs,
1154
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001155 .vidioc_streamon = fimc_cap_streamon,
1156 .vidioc_streamoff = fimc_cap_streamoff,
1157
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001158 .vidioc_g_selection = fimc_cap_g_selection,
1159 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001160
1161 .vidioc_enum_input = fimc_cap_enum_input,
1162 .vidioc_s_input = fimc_cap_s_input,
1163 .vidioc_g_input = fimc_cap_g_input,
1164};
1165
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001166/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001167static int fimc_link_setup(struct media_entity *entity,
1168 const struct media_pad *local,
1169 const struct media_pad *remote, u32 flags)
1170{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001171 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1172 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1173
1174 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1175 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001176
1177 if (WARN_ON(fimc == NULL))
1178 return 0;
1179
1180 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1181 local->entity->name, remote->entity->name, flags,
1182 fimc->vid_cap.input);
1183
1184 if (flags & MEDIA_LNK_FL_ENABLED) {
1185 if (fimc->vid_cap.input != 0)
1186 return -EBUSY;
1187 fimc->vid_cap.input = sd->grp_id;
1188 return 0;
1189 }
1190
1191 fimc->vid_cap.input = 0;
1192 return 0;
1193}
1194
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001195static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001196 .link_setup = fimc_link_setup,
1197};
1198
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001199/**
1200 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1201 * @sd: pointer to a subdev generating the notification
1202 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1203 * @arg: pointer to an u32 type integer that stores the frame payload value
1204 *
1205 * The End Of Frame notification sent by sensor subdev in its still capture
1206 * mode. If there is only a single VSYNC generated by the sensor at the
1207 * beginning of a frame transmission, FIMC does not issue the LastIrq
1208 * (end of frame) interrupt. And this notification is used to complete the
1209 * frame capture and returning a buffer to user-space. Subdev drivers should
1210 * call this notification from their last 'End of frame capture' interrupt.
1211 */
1212void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1213 void *arg)
1214{
1215 struct fimc_sensor_info *sensor;
1216 struct fimc_vid_buffer *buf;
1217 struct fimc_md *fmd;
1218 struct fimc_dev *fimc;
1219 unsigned long flags;
1220
1221 if (sd == NULL)
1222 return;
1223
1224 sensor = v4l2_get_subdev_hostdata(sd);
1225 fmd = entity_to_fimc_mdev(&sd->entity);
1226
1227 spin_lock_irqsave(&fmd->slock, flags);
1228 fimc = sensor ? sensor->host : NULL;
1229
1230 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1231 test_bit(ST_CAPT_PEND, &fimc->state)) {
1232 unsigned long irq_flags;
1233 spin_lock_irqsave(&fimc->slock, irq_flags);
1234 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1235 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1236 struct fimc_vid_buffer, list);
1237 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1238 }
1239 fimc_capture_irq_handler(fimc, true);
1240 fimc_deactivate_capture(fimc);
1241 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1242 }
1243 spin_unlock_irqrestore(&fmd->slock, flags);
1244}
1245
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001246static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1247 struct v4l2_subdev_fh *fh,
1248 struct v4l2_subdev_mbus_code_enum *code)
1249{
1250 struct fimc_fmt *fmt;
1251
1252 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1253 if (!fmt)
1254 return -EINVAL;
1255 code->code = fmt->mbus_code;
1256 return 0;
1257}
1258
1259static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1260 struct v4l2_subdev_fh *fh,
1261 struct v4l2_subdev_format *fmt)
1262{
1263 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1264 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1265 struct v4l2_mbus_framefmt *mf;
1266 struct fimc_frame *ff;
1267
1268 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1269 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1270 fmt->format = *mf;
1271 return 0;
1272 }
1273 mf = &fmt->format;
1274 mf->colorspace = V4L2_COLORSPACE_JPEG;
1275 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1276
1277 mutex_lock(&fimc->lock);
1278 /* The pixel code is same on both input and output pad */
1279 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1280 mf->code = ctx->s_frame.fmt->mbus_code;
1281 mf->width = ff->f_width;
1282 mf->height = ff->f_height;
1283 mutex_unlock(&fimc->lock);
1284
1285 return 0;
1286}
1287
1288static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1289 struct v4l2_subdev_fh *fh,
1290 struct v4l2_subdev_format *fmt)
1291{
1292 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1293 struct v4l2_mbus_framefmt *mf = &fmt->format;
1294 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1295 struct fimc_frame *ff;
1296 struct fimc_fmt *ffmt;
1297
1298 dbg("pad%d: code: 0x%x, %dx%d",
1299 fmt->pad, mf->code, mf->width, mf->height);
1300
1301 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1302 vb2_is_busy(&fimc->vid_cap.vbq))
1303 return -EBUSY;
1304
1305 mutex_lock(&fimc->lock);
1306 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1307 &mf->code, NULL, fmt->pad);
1308 mutex_unlock(&fimc->lock);
1309 mf->colorspace = V4L2_COLORSPACE_JPEG;
1310
1311 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1312 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1313 *mf = fmt->format;
1314 return 0;
1315 }
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001316 /* Update RGB Alpha control state and value range */
1317 fimc_alpha_ctrl_update(ctx);
1318
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001319 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1320
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001321 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1322 &ctx->s_frame : &ctx->d_frame;
1323
1324 mutex_lock(&fimc->lock);
1325 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki393a23f2011-12-08 06:56:22 -03001326 fimc->vid_cap.mf = *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001327 ff->fmt = ffmt;
1328
1329 /* Reset the crop rectangle if required. */
1330 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1331 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1332
1333 if (fmt->pad == FIMC_SD_PAD_SINK)
1334 ctx->state &= ~FIMC_DST_CROP;
1335 mutex_unlock(&fimc->lock);
1336 return 0;
1337}
1338
1339static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1340 struct v4l2_subdev_fh *fh,
1341 struct v4l2_subdev_crop *crop)
1342{
1343 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1344 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1345 struct v4l2_rect *r = &crop->rect;
1346 struct fimc_frame *ff;
1347
1348 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1349 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1350 return 0;
1351 }
1352 ff = crop->pad == FIMC_SD_PAD_SINK ?
1353 &ctx->s_frame : &ctx->d_frame;
1354
1355 mutex_lock(&fimc->lock);
1356 r->left = ff->offs_h;
1357 r->top = ff->offs_v;
1358 r->width = ff->width;
1359 r->height = ff->height;
1360 mutex_unlock(&fimc->lock);
1361
1362 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1363 ff, crop->pad, r->left, r->top, r->width, r->height,
1364 ff->f_width, ff->f_height);
1365
1366 return 0;
1367}
1368
1369static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1370 struct v4l2_subdev_fh *fh,
1371 struct v4l2_subdev_crop *crop)
1372{
1373 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1374 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1375 struct v4l2_rect *r = &crop->rect;
1376 struct fimc_frame *ff;
1377 unsigned long flags;
1378
1379 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1380
1381 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1382 &ctx->d_frame : &ctx->s_frame;
1383
1384 mutex_lock(&fimc->lock);
1385 fimc_capture_try_crop(ctx, r, crop->pad);
1386
1387 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1388 mutex_lock(&fimc->lock);
1389 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1390 return 0;
1391 }
1392 spin_lock_irqsave(&fimc->slock, flags);
1393 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1394 if (crop->pad == FIMC_SD_PAD_SOURCE)
1395 ctx->state |= FIMC_DST_CROP;
1396
1397 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1398 spin_unlock_irqrestore(&fimc->slock, flags);
1399
1400 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1401 r->width, r->height);
1402
1403 mutex_unlock(&fimc->lock);
1404 return 0;
1405}
1406
1407static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1408 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1409 .get_fmt = fimc_subdev_get_fmt,
1410 .set_fmt = fimc_subdev_set_fmt,
1411 .get_crop = fimc_subdev_get_crop,
1412 .set_crop = fimc_subdev_set_crop,
1413};
1414
1415static struct v4l2_subdev_ops fimc_subdev_ops = {
1416 .pad = &fimc_subdev_pad_ops,
1417};
1418
1419static int fimc_create_capture_subdev(struct fimc_dev *fimc,
1420 struct v4l2_device *v4l2_dev)
1421{
1422 struct v4l2_subdev *sd;
1423 int ret;
1424
1425 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
1426 if (!sd)
1427 return -ENOMEM;
1428
1429 v4l2_subdev_init(sd, &fimc_subdev_ops);
1430 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1431 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1432
1433 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1434 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1435 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1436 fimc->vid_cap.sd_pads, 0);
1437 if (ret)
1438 goto me_err;
1439 ret = v4l2_device_register_subdev(v4l2_dev, sd);
1440 if (ret)
1441 goto sd_err;
1442
1443 fimc->vid_cap.subdev = sd;
1444 v4l2_set_subdevdata(sd, fimc);
1445 sd->entity.ops = &fimc_sd_media_ops;
1446 return 0;
1447sd_err:
1448 media_entity_cleanup(&sd->entity);
1449me_err:
1450 kfree(sd);
1451 return ret;
1452}
1453
1454static void fimc_destroy_capture_subdev(struct fimc_dev *fimc)
1455{
1456 struct v4l2_subdev *sd = fimc->vid_cap.subdev;
1457
1458 if (!sd)
1459 return;
1460 media_entity_cleanup(&sd->entity);
1461 v4l2_device_unregister_subdev(sd);
1462 kfree(sd);
Sylwester Nawrocki64c570f2011-09-11 10:36:13 -03001463 fimc->vid_cap.subdev = NULL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001464}
1465
1466/* Set default format at the sensor and host interface */
1467static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1468{
1469 struct v4l2_format fmt = {
1470 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1471 .fmt.pix_mp = {
1472 .width = 640,
1473 .height = 480,
1474 .pixelformat = V4L2_PIX_FMT_YUYV,
1475 .field = V4L2_FIELD_NONE,
1476 .colorspace = V4L2_COLORSPACE_JPEG,
1477 },
1478 };
1479
1480 return fimc_capture_set_format(fimc, &fmt);
1481}
1482
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001483/* fimc->lock must be already initialized */
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001484int fimc_register_capture_device(struct fimc_dev *fimc,
1485 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001486{
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001487 struct video_device *vfd;
1488 struct fimc_vid_cap *vid_cap;
1489 struct fimc_ctx *ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001490 struct vb2_queue *q;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001491 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001492
1493 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1494 if (!ctx)
1495 return -ENOMEM;
1496
1497 ctx->fimc_dev = fimc;
1498 ctx->in_path = FIMC_CAMERA;
1499 ctx->out_path = FIMC_DMA;
1500 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001501 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
1502 ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001503
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001504 vfd = video_device_alloc();
1505 if (!vfd) {
1506 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001507 goto err_vd_alloc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001508 }
1509
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001510 snprintf(vfd->name, sizeof(vfd->name), "%s.capture",
1511 dev_name(&fimc->pdev->dev));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001512
1513 vfd->fops = &fimc_capture_fops;
1514 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001515 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001516 vfd->minor = -1;
1517 vfd->release = video_device_release;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001518 vfd->lock = &fimc->lock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001519 video_set_drvdata(vfd, fimc);
1520
1521 vid_cap = &fimc->vid_cap;
1522 vid_cap->vfd = vfd;
1523 vid_cap->active_buf_cnt = 0;
1524 vid_cap->reqbufs_count = 0;
1525 vid_cap->refcnt = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001526
1527 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1528 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001529 vid_cap->ctx = ctx;
1530
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001531 q = &fimc->vid_cap.vbq;
1532 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001533 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001534 q->io_modes = VB2_MMAP | VB2_USERPTR;
1535 q->drv_priv = fimc->vid_cap.ctx;
1536 q->ops = &fimc_capture_qops;
1537 q->mem_ops = &vb2_dma_contig_memops;
1538 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1539
1540 vb2_queue_init(q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001541
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001542 fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK;
1543 ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0);
1544 if (ret)
1545 goto err_ent;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001546 ret = fimc_create_capture_subdev(fimc, v4l2_dev);
1547 if (ret)
1548 goto err_sd_reg;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001549
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001550 vfd->ctrl_handler = &ctx->ctrl_handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001551 return 0;
1552
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001553err_sd_reg:
1554 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001555err_ent:
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001556 video_device_release(vfd);
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001557err_vd_alloc:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001558 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001559 return ret;
1560}
1561
1562void fimc_unregister_capture_device(struct fimc_dev *fimc)
1563{
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001564 struct video_device *vfd = fimc->vid_cap.vfd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001565
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001566 if (vfd) {
1567 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001568 /* Can also be called if video device was
1569 not registered */
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001570 video_unregister_device(vfd);
1571 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001572 fimc_destroy_capture_subdev(fimc);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001573 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001574 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001575}