blob: 6d079ac7089ee30dd0b06fd0ef1b06a28d1de480 [file] [log] [blame]
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001/*
Sylwester Nawrocki3a3f9442011-04-28 09:06:19 -03002 * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03003 *
Sylwester Nawrocki0c9204d2012-04-25 06:55:42 -03004 * Copyright (C) 2010 - 2012 Samsung Electronics Co., Ltd.
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030014#include <linux/types.h>
15#include <linux/errno.h>
16#include <linux/bug.h>
17#include <linux/interrupt.h>
18#include <linux/device.h>
Sylwester Nawrockie9e21082011-09-02 06:25:32 -030019#include <linux/pm_runtime.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030020#include <linux/list.h>
21#include <linux/slab.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030022
23#include <linux/videodev2.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
26#include <media/v4l2-mem2mem.h>
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030027#include <media/videobuf2-core.h>
28#include <media/videobuf2-dma-contig.h>
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030029
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -030030#include "fimc-mdevice.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030031#include "fimc-core.h"
Sylwester Nawrockic83a1ff2012-05-02 06:14:49 -030032#include "fimc-reg.h"
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030033
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030034static int fimc_capture_hw_init(struct fimc_dev *fimc)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030035{
36 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030037 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030038 struct fimc_sensor_info *sensor;
39 unsigned long flags;
40 int ret = 0;
41
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030042 if (p->subdevs[IDX_SENSOR] == NULL || ctx == NULL)
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030043 return -ENXIO;
44 if (ctx->s_frame.fmt == NULL)
45 return -EINVAL;
46
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -030047 sensor = v4l2_get_subdev_hostdata(p->subdevs[IDX_SENSOR]);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030048
49 spin_lock_irqsave(&fimc->slock, flags);
50 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
51 fimc_set_yuv_order(ctx);
52
53 fimc_hw_set_camera_polarity(fimc, sensor->pdata);
54 fimc_hw_set_camera_type(fimc, sensor->pdata);
55 fimc_hw_set_camera_source(fimc, sensor->pdata);
56 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
57
58 ret = fimc_set_scaler_info(ctx);
59 if (!ret) {
60 fimc_hw_set_input_path(ctx);
61 fimc_hw_set_prescaler(ctx);
62 fimc_hw_set_mainscaler(ctx);
63 fimc_hw_set_target_format(ctx);
64 fimc_hw_set_rotation(ctx);
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -030065 fimc_hw_set_effect(ctx, false);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030066 fimc_hw_set_output_path(ctx);
67 fimc_hw_set_out_dma(ctx);
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -030068 if (fimc->variant->has_alpha)
69 fimc_hw_set_rgb_alpha(ctx);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -030070 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030071 }
72 spin_unlock_irqrestore(&fimc->slock, flags);
73 return ret;
74}
75
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -030076/*
77 * Reinitialize the driver so it is ready to start the streaming again.
78 * Set fimc->state to indicate stream off and the hardware shut down state.
79 * If not suspending (@suspend is false), return any buffers to videobuf2.
80 * Otherwise put any owned buffers onto the pending buffers queue, so they
81 * can be re-spun when the device is being resumed. Also perform FIMC
82 * software reset and disable streaming on the whole pipeline if required.
83 */
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030084static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030085{
Marek Szyprowskibd323e22011-08-29 08:51:49 -030086 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -030087 struct fimc_vid_buffer *buf;
Marek Szyprowskibd323e22011-08-29 08:51:49 -030088 unsigned long flags;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030089 bool streaming;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030090
91 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030092 streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -030093
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030094 fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT |
95 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM);
Sylwester Nawrockiaa3331222012-03-16 11:47:51 -030096 if (suspend)
97 fimc->state |= (1 << ST_CAPT_SUSPENDED);
98 else
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -030099 fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300100
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300101 /* Release unused buffers */
102 while (!suspend && !list_empty(&cap->pending_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300103 buf = fimc_pending_queue_pop(cap);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300104 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
105 }
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300106 /* If suspending put unused buffers onto pending queue */
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300107 while (!list_empty(&cap->active_buf_q)) {
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300108 buf = fimc_active_queue_pop(cap);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300109 if (suspend)
110 fimc_pending_queue_add(cap, buf);
111 else
112 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300113 }
Sylwester Nawrocki2c1bb622011-10-05 14:20:45 -0300114
115 fimc_hw_reset(fimc);
116 cap->buf_index = 0;
117
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300118 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300119
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300120 if (streaming)
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300121 return fimc_pipeline_s_stream(&fimc->pipeline, 0);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300122 else
123 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300124}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300125
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300126static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300127{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300128 unsigned long flags;
129
130 if (!fimc_capture_active(fimc))
131 return 0;
132
133 spin_lock_irqsave(&fimc->slock, flags);
134 set_bit(ST_CAPT_SHUT, &fimc->state);
135 fimc_deactivate_capture(fimc);
136 spin_unlock_irqrestore(&fimc->slock, flags);
137
138 wait_event_timeout(fimc->irq_queue,
139 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300140 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300141
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300142 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300143}
144
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300145/**
146 * fimc_capture_config_update - apply the camera interface configuration
147 *
148 * To be called from within the interrupt handler with fimc.slock
149 * spinlock held. It updates the camera pixel crop, rotation and
150 * image flip in H/W.
151 */
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300152static int fimc_capture_config_update(struct fimc_ctx *ctx)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300153{
154 struct fimc_dev *fimc = ctx->fimc_dev;
155 int ret;
156
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300157 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300158
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300159 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300160 if (ret)
161 return ret;
162
163 fimc_hw_set_prescaler(ctx);
164 fimc_hw_set_mainscaler(ctx);
165 fimc_hw_set_target_format(ctx);
166 fimc_hw_set_rotation(ctx);
167 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
168 fimc_hw_set_out_dma(ctx);
169 if (fimc->variant->has_alpha)
170 fimc_hw_set_rgb_alpha(ctx);
171
172 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300173 return ret;
174}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300175
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300176void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
177{
178 struct fimc_vid_cap *cap = &fimc->vid_cap;
179 struct fimc_vid_buffer *v_buf;
180 struct timeval *tv;
181 struct timespec ts;
182
183 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
184 wake_up(&fimc->irq_queue);
185 goto done;
186 }
187
188 if (!list_empty(&cap->active_buf_q) &&
189 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
190 ktime_get_real_ts(&ts);
191
192 v_buf = fimc_active_queue_pop(cap);
193
194 tv = &v_buf->vb.v4l2_buf.timestamp;
195 tv->tv_sec = ts.tv_sec;
196 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
197 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
198
199 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
200 }
201
202 if (!list_empty(&cap->pending_buf_q)) {
203
204 v_buf = fimc_pending_queue_pop(cap);
205 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
206 v_buf->index = cap->buf_index;
207
208 /* Move the buffer to the capture active queue */
209 fimc_active_queue_add(cap, v_buf);
210
211 dbg("next frame: %d, done frame: %d",
212 fimc_hw_get_frame_index(fimc), v_buf->index);
213
214 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
215 cap->buf_index = 0;
216 }
217
218 if (cap->active_buf_cnt == 0) {
219 if (deq_buf)
220 clear_bit(ST_CAPT_RUN, &fimc->state);
221
222 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
223 cap->buf_index = 0;
224 } else {
225 set_bit(ST_CAPT_RUN, &fimc->state);
226 }
227
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300228 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
229 fimc_capture_config_update(cap->ctx);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300230done:
231 if (cap->active_buf_cnt == 1) {
232 fimc_deactivate_capture(fimc);
233 clear_bit(ST_CAPT_STREAM, &fimc->state);
234 }
235
236 dbg("frame: %d, active_buf_cnt: %d",
237 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
238}
239
240
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300241static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300242{
243 struct fimc_ctx *ctx = q->drv_priv;
244 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300245 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300246 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300247 int ret;
248
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300249 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300250
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300251 ret = fimc_capture_hw_init(fimc);
252 if (ret) {
253 fimc_capture_state_cleanup(fimc, false);
254 return ret;
255 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300256
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300257 set_bit(ST_CAPT_PEND, &fimc->state);
258
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300259 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
260
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300261 if (vid_cap->active_buf_cnt >= min_bufs &&
262 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300263 fimc_activate_capture(ctx);
264
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300265 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300266 fimc_pipeline_s_stream(&fimc->pipeline, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300267 }
268
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300269 return 0;
270}
271
272static int stop_streaming(struct vb2_queue *q)
273{
274 struct fimc_ctx *ctx = q->drv_priv;
275 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300276
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300277 if (!fimc_capture_active(fimc))
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300278 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300279
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300280 return fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300281}
282
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300283int fimc_capture_suspend(struct fimc_dev *fimc)
284{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300285 bool suspend = fimc_capture_busy(fimc);
286
287 int ret = fimc_stop_capture(fimc, suspend);
288 if (ret)
289 return ret;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300290 return fimc_pipeline_shutdown(&fimc->pipeline);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300291}
292
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300293static void buffer_queue(struct vb2_buffer *vb);
294
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300295int fimc_capture_resume(struct fimc_dev *fimc)
296{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300297 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
298 struct fimc_vid_buffer *buf;
299 int i;
300
301 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
302 return 0;
303
304 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
305 vid_cap->buf_index = 0;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300306 fimc_pipeline_initialize(&fimc->pipeline, &vid_cap->vfd->entity,
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300307 false);
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300308 fimc_capture_hw_init(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300309
310 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
311
312 for (i = 0; i < vid_cap->reqbufs_count; i++) {
313 if (list_empty(&vid_cap->pending_buf_q))
314 break;
315 buf = fimc_pending_queue_pop(vid_cap);
316 buffer_queue(&buf->vb);
317 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300318 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300319
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300320}
321
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300322static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300323 unsigned int *num_buffers, unsigned int *num_planes,
324 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300325{
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300326 const struct v4l2_pix_format_mplane *pixm = NULL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300327 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300328 struct fimc_frame *frame = &ctx->d_frame;
329 struct fimc_fmt *fmt = frame->fmt;
330 unsigned long wh;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300331 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300332
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300333 if (pfmt) {
334 pixm = &pfmt->fmt.pix_mp;
335 fmt = fimc_find_format(&pixm->pixelformat, NULL,
336 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
337 wh = pixm->width * pixm->height;
338 } else {
339 wh = frame->f_width * frame->f_height;
340 }
341
342 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300343 return -EINVAL;
344
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300345 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300346
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300347 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300348 unsigned int size = (wh * fmt->depth[i]) / 8;
349 if (pixm)
350 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
351 else
352 sizes[i] = size;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300353 allocators[i] = ctx->fimc_dev->alloc_ctx;
354 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300355
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300356 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300357}
358
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300359static int buffer_prepare(struct vb2_buffer *vb)
360{
361 struct vb2_queue *vq = vb->vb2_queue;
362 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300363 int i;
364
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300365 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300366 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300367
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300368 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300369 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300370
371 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300372 v4l2_err(ctx->fimc_dev->vid_cap.vfd,
373 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300374 vb2_plane_size(vb, i), size);
375 return -EINVAL;
376 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300377 vb2_set_plane_payload(vb, i, size);
378 }
379
380 return 0;
381}
382
383static void buffer_queue(struct vb2_buffer *vb)
384{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300385 struct fimc_vid_buffer *buf
386 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300387 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
388 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300389 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
390 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300391 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300392
393 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300394 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300395
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300396 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
397 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
398 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300399 /* Setup the buffer directly for processing. */
400 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
401 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300402
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300403 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
404 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300405 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300406
407 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
408 vid_cap->buf_index = 0;
409 } else {
410 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300411 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300412
413 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
414
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300415
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300416 if (vb2_is_streaming(&vid_cap->vbq) &&
417 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300418 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300419 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300420 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300421
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300422 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300423 fimc_pipeline_s_stream(&fimc->pipeline, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300424 return;
425 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300426 spin_unlock_irqrestore(&fimc->slock, flags);
427}
428
429static void fimc_lock(struct vb2_queue *vq)
430{
431 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
432 mutex_lock(&ctx->fimc_dev->lock);
433}
434
435static void fimc_unlock(struct vb2_queue *vq)
436{
437 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
438 mutex_unlock(&ctx->fimc_dev->lock);
439}
440
441static struct vb2_ops fimc_capture_qops = {
442 .queue_setup = queue_setup,
443 .buf_prepare = buffer_prepare,
444 .buf_queue = buffer_queue,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300445 .wait_prepare = fimc_unlock,
446 .wait_finish = fimc_lock,
447 .start_streaming = start_streaming,
448 .stop_streaming = stop_streaming,
449};
450
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300451/**
452 * fimc_capture_ctrls_create - initialize the control handler
453 * Initialize the capture video node control handler and fill it
454 * with the FIMC controls. Inherit any sensor's controls if the
455 * 'user_subdev_api' flag is false (default behaviour).
456 * This function need to be called with the graph mutex held.
457 */
458int fimc_capture_ctrls_create(struct fimc_dev *fimc)
459{
460 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
461 int ret;
462
463 if (WARN_ON(vid_cap->ctx == NULL))
464 return -ENXIO;
465 if (vid_cap->ctx->ctrls_rdy)
466 return 0;
467
468 ret = fimc_ctrls_create(vid_cap->ctx);
469 if (ret || vid_cap->user_subdev_api)
470 return ret;
471
472 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler,
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300473 fimc->pipeline.subdevs[IDX_SENSOR]->ctrl_handler);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300474}
475
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300476static int fimc_capture_set_default_format(struct fimc_dev *fimc);
477
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300478static int fimc_capture_open(struct file *file)
479{
480 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300481 int ret = v4l2_fh_open(file);
482
483 if (ret)
484 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300485
486 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
487
488 /* Return if the corresponding video mem2mem node is already opened. */
489 if (fimc_m2m_active(fimc))
490 return -EBUSY;
491
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300492 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300493 pm_runtime_get_sync(&fimc->pdev->dev);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300494
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300495 if (++fimc->vid_cap.refcnt == 1) {
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300496 ret = fimc_pipeline_initialize(&fimc->pipeline,
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300497 &fimc->vid_cap.vfd->entity, true);
498 if (ret < 0) {
499 dev_err(&fimc->pdev->dev,
500 "Video pipeline initialization failed\n");
501 pm_runtime_put_sync(&fimc->pdev->dev);
502 fimc->vid_cap.refcnt--;
503 v4l2_fh_release(file);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300504 clear_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300505 return ret;
506 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300507 ret = fimc_capture_ctrls_create(fimc);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300508
509 if (!ret && !fimc->vid_cap.user_subdev_api)
510 ret = fimc_capture_set_default_format(fimc);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300511 }
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300512 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300513}
514
515static int fimc_capture_close(struct file *file)
516{
517 struct fimc_dev *fimc = video_drvdata(file);
518
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300519 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
520
521 if (--fimc->vid_cap.refcnt == 0) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300522 clear_bit(ST_CAPT_BUSY, &fimc->state);
523 fimc_stop_capture(fimc, false);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300524 fimc_pipeline_shutdown(&fimc->pipeline);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300525 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300526 }
527
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300528 pm_runtime_put(&fimc->pdev->dev);
529
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300530 if (fimc->vid_cap.refcnt == 0) {
531 vb2_queue_release(&fimc->vid_cap.vbq);
532 fimc_ctrls_delete(fimc->vid_cap.ctx);
533 }
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300534 return v4l2_fh_release(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300535}
536
537static unsigned int fimc_capture_poll(struct file *file,
538 struct poll_table_struct *wait)
539{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300540 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300541
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300542 return vb2_poll(&fimc->vid_cap.vbq, file, wait);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300543}
544
545static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
546{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300547 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300548
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -0300549 return vb2_mmap(&fimc->vid_cap.vbq, vma);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300550}
551
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300552static const struct v4l2_file_operations fimc_capture_fops = {
553 .owner = THIS_MODULE,
554 .open = fimc_capture_open,
555 .release = fimc_capture_close,
556 .poll = fimc_capture_poll,
557 .unlocked_ioctl = video_ioctl2,
558 .mmap = fimc_capture_mmap,
559};
560
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300561/*
562 * Format and crop negotiation helpers
563 */
564
565static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
566 u32 *width, u32 *height,
567 u32 *code, u32 *fourcc, int pad)
568{
569 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
570 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300571 struct fimc_variant *var = fimc->variant;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300572 struct fimc_pix_limit *pl = var->pix_limit;
573 struct fimc_frame *dst = &ctx->d_frame;
574 u32 depth, min_w, max_w, min_h, align_h = 3;
575 u32 mask = FMT_FLAGS_CAM;
576 struct fimc_fmt *ffmt;
577
578 /* Color conversion from/to JPEG is not supported */
579 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
580 fimc_fmt_is_jpeg(ctx->s_frame.fmt->color))
581 *code = V4L2_MBUS_FMT_JPEG_1X8;
582
583 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
584 mask |= FMT_FLAGS_M2M;
585
586 ffmt = fimc_find_format(fourcc, code, mask, 0);
587 if (WARN_ON(!ffmt))
588 return NULL;
589 if (code)
590 *code = ffmt->mbus_code;
591 if (fourcc)
592 *fourcc = ffmt->fourcc;
593
594 if (pad == FIMC_SD_PAD_SINK) {
595 max_w = fimc_fmt_is_jpeg(ffmt->color) ?
596 pl->scaler_dis_w : pl->scaler_en_w;
597 /* Apply the camera input interface pixel constraints */
598 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
599 height, max_t(u32, *height, 32),
600 FIMC_CAMIF_MAX_HEIGHT,
601 fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1,
602 0);
603 return ffmt;
604 }
605 /* Can't scale or crop in transparent (JPEG) transfer mode */
606 if (fimc_fmt_is_jpeg(ffmt->color)) {
607 *width = ctx->s_frame.f_width;
608 *height = ctx->s_frame.f_height;
609 return ffmt;
610 }
611 /* Apply the scaler and the output DMA constraints */
612 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
613 min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize;
614 min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize;
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300615 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300616 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
617
618 depth = fimc_get_format_depth(ffmt);
619 v4l_bound_align_image(width, min_w, max_w,
620 ffs(var->min_out_pixsize) - 1,
621 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
622 align_h,
623 64/(ALIGN(depth, 8)));
624
625 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
626 pad, code ? *code : 0, *width, *height,
627 dst->f_width, dst->f_height);
628
629 return ffmt;
630}
631
632static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r,
633 int pad)
634{
635 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
636 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300637 struct fimc_variant *var = fimc->variant;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300638 struct fimc_pix_limit *pl = var->pix_limit;
639 struct fimc_frame *sink = &ctx->s_frame;
640 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
641 u32 align_sz = 0, align_h = 4;
642 u32 max_sc_h, max_sc_v;
643
644 /* In JPEG transparent transfer mode cropping is not supported */
645 if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) {
646 r->width = sink->f_width;
647 r->height = sink->f_height;
648 r->left = r->top = 0;
649 return;
650 }
651 if (pad == FIMC_SD_PAD_SOURCE) {
652 if (ctx->rotation != 90 && ctx->rotation != 270)
653 align_h = 1;
654 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
655 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
656 min_sz = var->min_out_pixsize;
657 } else {
658 u32 depth = fimc_get_format_depth(sink->fmt);
659 align_sz = 64/ALIGN(depth, 8);
660 min_sz = var->min_inp_pixsize;
661 min_w = min_h = min_sz;
662 max_sc_h = max_sc_v = 1;
663 }
664 /*
665 * For the crop rectangle at source pad the following constraints
666 * must be met:
667 * - it must fit in the sink pad format rectangle (f_width/f_height);
668 * - maximum downscaling ratio is 64;
669 * - maximum crop size depends if the rotator is used or not;
670 * - the sink pad format width/height must be 4 multiple of the
671 * prescaler ratios determined by sink pad size and source pad crop,
672 * the prescaler ratio is returned by fimc_get_scaler_factor().
673 */
674 max_w = min_t(u32,
675 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
676 rotate ? sink->f_height : sink->f_width);
677 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
678 if (pad == FIMC_SD_PAD_SOURCE) {
679 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
680 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
681 if (rotate) {
682 swap(max_sc_h, max_sc_v);
683 swap(min_w, min_h);
684 }
685 }
686 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
687 &r->height, min_h, max_h, align_h,
688 align_sz);
689 /* Adjust left/top if cropping rectangle is out of bounds */
690 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
691 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
692 r->left = round_down(r->left, var->hor_offs_align);
693
694 dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d",
695 pad, r->left, r->top, r->width, r->height,
696 sink->f_width, sink->f_height);
697}
698
699/*
700 * The video node ioctl operations
701 */
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300702static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
703 struct v4l2_capability *cap)
704{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300705 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300706
707 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
708 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
709 cap->bus_info[0] = 0;
Sylwester Nawrocki8f401542011-07-07 06:42:05 -0300710 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300711
712 return 0;
713}
714
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300715static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
716 struct v4l2_fmtdesc *f)
717{
718 struct fimc_fmt *fmt;
719
720 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
721 f->index);
722 if (!fmt)
723 return -EINVAL;
724 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
725 f->pixelformat = fmt->fourcc;
726 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
727 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
728 return 0;
729}
730
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300731/**
732 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
733 * elements
734 * @ctx: FIMC capture context
735 * @tfmt: media bus format to try/set on subdevs
736 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
737 * @set: true to set format on subdevs, false to try only
738 */
739static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
740 struct v4l2_mbus_framefmt *tfmt,
741 struct fimc_fmt **fmt_id,
742 bool set)
743{
744 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300745 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
746 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300747 struct v4l2_subdev_format sfmt;
748 struct v4l2_mbus_framefmt *mf = &sfmt.format;
749 struct fimc_fmt *ffmt = NULL;
750 int ret, i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300751
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300752 if (WARN_ON(!sd || !tfmt))
753 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300754
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300755 memset(&sfmt, 0, sizeof(sfmt));
756 sfmt.format = *tfmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300757
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300758 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
759 while (1) {
760 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
761 FMT_FLAGS_CAM, i++);
762 if (ffmt == NULL) {
763 /*
764 * Notify user-space if common pixel code for
765 * host and sensor does not exist.
766 */
767 return -EINVAL;
768 }
769 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300770
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300771 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
772 if (ret)
773 return ret;
774 if (mf->code != tfmt->code) {
775 mf->code = 0;
776 continue;
777 }
Julia Lawallb1aa6082012-01-12 17:49:28 -0300778 if (mf->width != tfmt->width || mf->height != tfmt->height) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300779 u32 fcc = ffmt->fourcc;
780 tfmt->width = mf->width;
781 tfmt->height = mf->height;
782 ffmt = fimc_capture_try_format(ctx,
783 &tfmt->width, &tfmt->height,
784 NULL, &fcc, FIMC_SD_PAD_SOURCE);
785 if (ffmt && ffmt->mbus_code)
786 mf->code = ffmt->mbus_code;
Julia Lawallb1aa6082012-01-12 17:49:28 -0300787 if (mf->width != tfmt->width ||
788 mf->height != tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300789 continue;
790 tfmt->code = mf->code;
791 }
792 if (csis)
793 ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300794
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300795 if (mf->code == tfmt->code &&
Julia Lawallb1aa6082012-01-12 17:49:28 -0300796 mf->width == tfmt->width && mf->height == tfmt->height)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300797 break;
798 }
799
800 if (fmt_id && ffmt)
801 *fmt_id = ffmt;
802 *tfmt = *mf;
803
804 dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt);
805 return 0;
806}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300807
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300808static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
809 struct v4l2_format *f)
810{
811 struct fimc_dev *fimc = video_drvdata(file);
812 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
813
814 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
815 return -EINVAL;
816
817 return fimc_fill_format(&ctx->d_frame, f);
818}
819
820static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
821 struct v4l2_format *f)
822{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300823 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300824 struct fimc_dev *fimc = video_drvdata(file);
825 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300826 struct v4l2_mbus_framefmt mf;
827 struct fimc_fmt *ffmt = NULL;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300828
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300829 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
830 return -EINVAL;
831
832 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
833 fimc_capture_try_format(ctx, &pix->width, &pix->height,
834 NULL, &pix->pixelformat,
835 FIMC_SD_PAD_SINK);
836 ctx->s_frame.f_width = pix->width;
837 ctx->s_frame.f_height = pix->height;
838 }
839 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
840 NULL, &pix->pixelformat,
841 FIMC_SD_PAD_SOURCE);
842 if (!ffmt)
843 return -EINVAL;
844
845 if (!fimc->vid_cap.user_subdev_api) {
846 mf.width = pix->width;
847 mf.height = pix->height;
848 mf.code = ffmt->mbus_code;
849 fimc_md_graph_lock(fimc);
850 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
851 fimc_md_graph_unlock(fimc);
852
853 pix->width = mf.width;
854 pix->height = mf.height;
855 if (ffmt)
856 pix->pixelformat = ffmt->fourcc;
857 }
858
859 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300860 return 0;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300861}
862
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300863static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg)
864{
865 ctx->scaler.enabled = !jpeg;
866 fimc_ctrls_activate(ctx, !jpeg);
867
868 if (jpeg)
869 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
870 else
871 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
872}
873
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300874static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f)
875{
876 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
877 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
878 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
879 struct fimc_frame *ff = &ctx->d_frame;
880 struct fimc_fmt *s_fmt = NULL;
881 int ret, i;
882
883 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
884 return -EINVAL;
885 if (vb2_is_busy(&fimc->vid_cap.vbq))
886 return -EBUSY;
887
888 /* Pre-configure format at camera interface input, for JPEG only */
889 if (pix->pixelformat == V4L2_PIX_FMT_JPEG) {
890 fimc_capture_try_format(ctx, &pix->width, &pix->height,
891 NULL, &pix->pixelformat,
892 FIMC_SD_PAD_SINK);
893 ctx->s_frame.f_width = pix->width;
894 ctx->s_frame.f_height = pix->height;
895 }
896 /* Try the format at the scaler and the DMA output */
897 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
898 NULL, &pix->pixelformat,
899 FIMC_SD_PAD_SOURCE);
900 if (!ff->fmt)
901 return -EINVAL;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -0300902
903 /* Update RGB Alpha control state and value range */
904 fimc_alpha_ctrl_update(ctx);
905
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300906 /* Try to match format at the host and the sensor */
907 if (!fimc->vid_cap.user_subdev_api) {
908 mf->code = ff->fmt->mbus_code;
909 mf->width = pix->width;
910 mf->height = pix->height;
911
912 fimc_md_graph_lock(fimc);
913 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
914 fimc_md_graph_unlock(fimc);
915 if (ret)
916 return ret;
917 pix->width = mf->width;
918 pix->height = mf->height;
919 }
920 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
921 for (i = 0; i < ff->fmt->colplanes; i++)
922 ff->payload[i] =
923 (pix->width * pix->height * ff->fmt->depth[i]) / 8;
924
925 set_frame_bounds(ff, pix->width, pix->height);
926 /* Reset the composition rectangle if not yet configured */
927 if (!(ctx->state & FIMC_DST_CROP))
928 set_frame_crop(ff, 0, 0, pix->width, pix->height);
929
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -0300930 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color));
931
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300932 /* Reset cropping and set format at the camera interface input */
933 if (!fimc->vid_cap.user_subdev_api) {
934 ctx->s_frame.fmt = s_fmt;
935 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
936 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
937 }
938
939 return ret;
940}
941
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300942static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
943 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300944{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300945 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300946
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300947 return fimc_capture_set_format(fimc, f);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300948}
949
950static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300951 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300952{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300953 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300954 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300955
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300956 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300957 return -EINVAL;
958
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300959 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300960 if (sd)
961 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300962 return 0;
963}
964
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300965static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300966{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300967 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300968}
969
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300970static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300971{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -0300972 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300973 return 0;
974}
975
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300976/**
977 * fimc_pipeline_validate - check for formats inconsistencies
978 * between source and sink pad of each link
979 *
980 * Return 0 if all formats match or -EPIPE otherwise.
981 */
982static int fimc_pipeline_validate(struct fimc_dev *fimc)
983{
984 struct v4l2_subdev_format sink_fmt, src_fmt;
985 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
986 struct v4l2_subdev *sd;
987 struct media_pad *pad;
988 int ret;
989
990 /* Start with the video capture node pad */
991 pad = media_entity_remote_source(&vid_cap->vd_pad);
992 if (pad == NULL)
993 return -EPIPE;
994 /* FIMC.{N} subdevice */
995 sd = media_entity_to_v4l2_subdev(pad->entity);
996
997 while (1) {
998 /* Retrieve format at the sink pad */
999 pad = &sd->entity.pads[0];
1000 if (!(pad->flags & MEDIA_PAD_FL_SINK))
1001 break;
1002 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001003 if (sd == &fimc->vid_cap.subdev) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001004 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
1005 sink_fmt.format.width = ff->f_width;
1006 sink_fmt.format.height = ff->f_height;
1007 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1008 } else {
1009 sink_fmt.pad = pad->index;
1010 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1011 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1012 if (ret < 0 && ret != -ENOIOCTLCMD)
1013 return -EPIPE;
1014 }
1015 /* Retrieve format at the source pad */
1016 pad = media_entity_remote_source(pad);
1017 if (pad == NULL ||
1018 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1019 break;
1020
1021 sd = media_entity_to_v4l2_subdev(pad->entity);
1022 src_fmt.pad = pad->index;
1023 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1024 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1025 if (ret < 0 && ret != -ENOIOCTLCMD)
1026 return -EPIPE;
1027
1028 if (src_fmt.format.width != sink_fmt.format.width ||
1029 src_fmt.format.height != sink_fmt.format.height ||
1030 src_fmt.format.code != sink_fmt.format.code)
1031 return -EPIPE;
1032 }
1033 return 0;
1034}
1035
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001036static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001037 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001038{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001039 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001040 struct fimc_pipeline *p = &fimc->pipeline;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001041 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001042
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001043 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001044 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001045
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001046 media_entity_pipeline_start(&p->subdevs[IDX_SENSOR]->entity,
1047 p->m_pipeline);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001048
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001049 if (fimc->vid_cap.user_subdev_api) {
1050 ret = fimc_pipeline_validate(fimc);
1051 if (ret)
1052 return ret;
1053 }
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001054 return vb2_streamon(&fimc->vid_cap.vbq, type);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001055}
1056
1057static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001058 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001059{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001060 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001061 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001062 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001063
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001064 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
1065 if (ret == 0)
1066 media_entity_pipeline_stop(&sd->entity);
1067 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001068}
1069
1070static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001071 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001072{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001073 struct fimc_dev *fimc = video_drvdata(file);
1074 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001075
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001076 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001077 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001078 return ret;
1079}
1080
1081static int fimc_cap_querybuf(struct file *file, void *priv,
1082 struct v4l2_buffer *buf)
1083{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001084 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001085
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001086 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001087}
1088
1089static int fimc_cap_qbuf(struct file *file, void *priv,
1090 struct v4l2_buffer *buf)
1091{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001092 struct fimc_dev *fimc = video_drvdata(file);
1093
1094 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001095}
1096
1097static int fimc_cap_dqbuf(struct file *file, void *priv,
1098 struct v4l2_buffer *buf)
1099{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001100 struct fimc_dev *fimc = video_drvdata(file);
1101
1102 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001103}
1104
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001105static int fimc_cap_create_bufs(struct file *file, void *priv,
1106 struct v4l2_create_buffers *create)
1107{
1108 struct fimc_dev *fimc = video_drvdata(file);
1109
1110 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1111}
1112
1113static int fimc_cap_prepare_buf(struct file *file, void *priv,
1114 struct v4l2_buffer *b)
1115{
1116 struct fimc_dev *fimc = video_drvdata(file);
1117
1118 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1119}
1120
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001121static int fimc_cap_g_selection(struct file *file, void *fh,
1122 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001123{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001124 struct fimc_dev *fimc = video_drvdata(file);
1125 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001126 struct fimc_frame *f = &ctx->s_frame;
1127
1128 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1129 return -EINVAL;
1130
1131 switch (s->target) {
1132 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1133 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1134 f = &ctx->d_frame;
1135 case V4L2_SEL_TGT_CROP_BOUNDS:
1136 case V4L2_SEL_TGT_CROP_DEFAULT:
1137 s->r.left = 0;
1138 s->r.top = 0;
1139 s->r.width = f->o_width;
1140 s->r.height = f->o_height;
1141 return 0;
1142
1143 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1144 f = &ctx->d_frame;
1145 case V4L2_SEL_TGT_CROP_ACTIVE:
1146 s->r.left = f->offs_h;
1147 s->r.top = f->offs_v;
1148 s->r.width = f->width;
1149 s->r.height = f->height;
1150 return 0;
1151 }
1152
1153 return -EINVAL;
1154}
1155
1156/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
1157int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1158{
1159 if (a->left < b->left || a->top < b->top)
1160 return 0;
1161 if (a->left + a->width > b->left + b->width)
1162 return 0;
1163 if (a->top + a->height > b->top + b->height)
1164 return 0;
1165
1166 return 1;
1167}
1168
1169static int fimc_cap_s_selection(struct file *file, void *fh,
1170 struct v4l2_selection *s)
1171{
1172 struct fimc_dev *fimc = video_drvdata(file);
1173 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1174 struct v4l2_rect rect = s->r;
1175 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001176 unsigned long flags;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001177 unsigned int pad;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001178
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001179 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1180 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001181
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001182 switch (s->target) {
1183 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1184 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1185 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
1186 f = &ctx->d_frame;
1187 pad = FIMC_SD_PAD_SOURCE;
1188 break;
1189 case V4L2_SEL_TGT_CROP_BOUNDS:
1190 case V4L2_SEL_TGT_CROP_DEFAULT:
1191 case V4L2_SEL_TGT_CROP_ACTIVE:
1192 f = &ctx->s_frame;
1193 pad = FIMC_SD_PAD_SINK;
1194 break;
1195 default:
1196 return -EINVAL;
1197 }
1198
1199 fimc_capture_try_crop(ctx, &rect, pad);
1200
1201 if (s->flags & V4L2_SEL_FLAG_LE &&
1202 !enclosed_rectangle(&rect, &s->r))
1203 return -ERANGE;
1204
1205 if (s->flags & V4L2_SEL_FLAG_GE &&
1206 !enclosed_rectangle(&s->r, &rect))
1207 return -ERANGE;
1208
1209 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001210 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001211 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1212 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001213 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001214
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001215 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001216 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001217}
1218
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001219static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1220 .vidioc_querycap = fimc_vidioc_querycap_capture,
1221
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001222 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001223 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001224 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001225 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001226
1227 .vidioc_reqbufs = fimc_cap_reqbufs,
1228 .vidioc_querybuf = fimc_cap_querybuf,
1229
1230 .vidioc_qbuf = fimc_cap_qbuf,
1231 .vidioc_dqbuf = fimc_cap_dqbuf,
1232
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001233 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1234 .vidioc_create_bufs = fimc_cap_create_bufs,
1235
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001236 .vidioc_streamon = fimc_cap_streamon,
1237 .vidioc_streamoff = fimc_cap_streamoff,
1238
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001239 .vidioc_g_selection = fimc_cap_g_selection,
1240 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001241
1242 .vidioc_enum_input = fimc_cap_enum_input,
1243 .vidioc_s_input = fimc_cap_s_input,
1244 .vidioc_g_input = fimc_cap_g_input,
1245};
1246
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001247/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001248static int fimc_link_setup(struct media_entity *entity,
1249 const struct media_pad *local,
1250 const struct media_pad *remote, u32 flags)
1251{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001252 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1253 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1254
1255 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1256 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001257
1258 if (WARN_ON(fimc == NULL))
1259 return 0;
1260
1261 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1262 local->entity->name, remote->entity->name, flags,
1263 fimc->vid_cap.input);
1264
1265 if (flags & MEDIA_LNK_FL_ENABLED) {
1266 if (fimc->vid_cap.input != 0)
1267 return -EBUSY;
1268 fimc->vid_cap.input = sd->grp_id;
1269 return 0;
1270 }
1271
1272 fimc->vid_cap.input = 0;
1273 return 0;
1274}
1275
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001276static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001277 .link_setup = fimc_link_setup,
1278};
1279
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001280/**
1281 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1282 * @sd: pointer to a subdev generating the notification
1283 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1284 * @arg: pointer to an u32 type integer that stores the frame payload value
1285 *
1286 * The End Of Frame notification sent by sensor subdev in its still capture
1287 * mode. If there is only a single VSYNC generated by the sensor at the
1288 * beginning of a frame transmission, FIMC does not issue the LastIrq
1289 * (end of frame) interrupt. And this notification is used to complete the
1290 * frame capture and returning a buffer to user-space. Subdev drivers should
1291 * call this notification from their last 'End of frame capture' interrupt.
1292 */
1293void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1294 void *arg)
1295{
1296 struct fimc_sensor_info *sensor;
1297 struct fimc_vid_buffer *buf;
1298 struct fimc_md *fmd;
1299 struct fimc_dev *fimc;
1300 unsigned long flags;
1301
1302 if (sd == NULL)
1303 return;
1304
1305 sensor = v4l2_get_subdev_hostdata(sd);
1306 fmd = entity_to_fimc_mdev(&sd->entity);
1307
1308 spin_lock_irqsave(&fmd->slock, flags);
1309 fimc = sensor ? sensor->host : NULL;
1310
1311 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1312 test_bit(ST_CAPT_PEND, &fimc->state)) {
1313 unsigned long irq_flags;
1314 spin_lock_irqsave(&fimc->slock, irq_flags);
1315 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1316 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1317 struct fimc_vid_buffer, list);
1318 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1319 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001320 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001321 fimc_deactivate_capture(fimc);
1322 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1323 }
1324 spin_unlock_irqrestore(&fmd->slock, flags);
1325}
1326
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001327static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1328 struct v4l2_subdev_fh *fh,
1329 struct v4l2_subdev_mbus_code_enum *code)
1330{
1331 struct fimc_fmt *fmt;
1332
1333 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1334 if (!fmt)
1335 return -EINVAL;
1336 code->code = fmt->mbus_code;
1337 return 0;
1338}
1339
1340static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1341 struct v4l2_subdev_fh *fh,
1342 struct v4l2_subdev_format *fmt)
1343{
1344 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1345 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1346 struct v4l2_mbus_framefmt *mf;
1347 struct fimc_frame *ff;
1348
1349 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1350 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1351 fmt->format = *mf;
1352 return 0;
1353 }
1354 mf = &fmt->format;
1355 mf->colorspace = V4L2_COLORSPACE_JPEG;
1356 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1357
1358 mutex_lock(&fimc->lock);
1359 /* The pixel code is same on both input and output pad */
1360 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1361 mf->code = ctx->s_frame.fmt->mbus_code;
1362 mf->width = ff->f_width;
1363 mf->height = ff->f_height;
1364 mutex_unlock(&fimc->lock);
1365
1366 return 0;
1367}
1368
1369static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1370 struct v4l2_subdev_fh *fh,
1371 struct v4l2_subdev_format *fmt)
1372{
1373 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1374 struct v4l2_mbus_framefmt *mf = &fmt->format;
1375 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1376 struct fimc_frame *ff;
1377 struct fimc_fmt *ffmt;
1378
1379 dbg("pad%d: code: 0x%x, %dx%d",
1380 fmt->pad, mf->code, mf->width, mf->height);
1381
1382 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1383 vb2_is_busy(&fimc->vid_cap.vbq))
1384 return -EBUSY;
1385
1386 mutex_lock(&fimc->lock);
1387 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1388 &mf->code, NULL, fmt->pad);
1389 mutex_unlock(&fimc->lock);
1390 mf->colorspace = V4L2_COLORSPACE_JPEG;
1391
1392 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1393 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1394 *mf = fmt->format;
1395 return 0;
1396 }
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001397 /* Update RGB Alpha control state and value range */
1398 fimc_alpha_ctrl_update(ctx);
1399
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001400 fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color));
1401
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001402 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1403 &ctx->s_frame : &ctx->d_frame;
1404
1405 mutex_lock(&fimc->lock);
1406 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki393a23f2011-12-08 06:56:22 -03001407 fimc->vid_cap.mf = *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001408 ff->fmt = ffmt;
1409
1410 /* Reset the crop rectangle if required. */
1411 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP)))
1412 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1413
1414 if (fmt->pad == FIMC_SD_PAD_SINK)
1415 ctx->state &= ~FIMC_DST_CROP;
1416 mutex_unlock(&fimc->lock);
1417 return 0;
1418}
1419
1420static int fimc_subdev_get_crop(struct v4l2_subdev *sd,
1421 struct v4l2_subdev_fh *fh,
1422 struct v4l2_subdev_crop *crop)
1423{
1424 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1425 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1426 struct v4l2_rect *r = &crop->rect;
1427 struct fimc_frame *ff;
1428
1429 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
1430 crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad);
1431 return 0;
1432 }
1433 ff = crop->pad == FIMC_SD_PAD_SINK ?
1434 &ctx->s_frame : &ctx->d_frame;
1435
1436 mutex_lock(&fimc->lock);
1437 r->left = ff->offs_h;
1438 r->top = ff->offs_v;
1439 r->width = ff->width;
1440 r->height = ff->height;
1441 mutex_unlock(&fimc->lock);
1442
1443 dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1444 ff, crop->pad, r->left, r->top, r->width, r->height,
1445 ff->f_width, ff->f_height);
1446
1447 return 0;
1448}
1449
1450static int fimc_subdev_set_crop(struct v4l2_subdev *sd,
1451 struct v4l2_subdev_fh *fh,
1452 struct v4l2_subdev_crop *crop)
1453{
1454 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1455 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1456 struct v4l2_rect *r = &crop->rect;
1457 struct fimc_frame *ff;
1458 unsigned long flags;
1459
1460 dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height);
1461
1462 ff = crop->pad == FIMC_SD_PAD_SOURCE ?
1463 &ctx->d_frame : &ctx->s_frame;
1464
1465 mutex_lock(&fimc->lock);
1466 fimc_capture_try_crop(ctx, r, crop->pad);
1467
1468 if (crop->which == V4L2_SUBDEV_FORMAT_TRY) {
Sylwester Nawrocki5be4fe62012-04-21 18:46:30 -03001469 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001470 *v4l2_subdev_get_try_crop(fh, crop->pad) = *r;
1471 return 0;
1472 }
1473 spin_lock_irqsave(&fimc->slock, flags);
1474 set_frame_crop(ff, r->left, r->top, r->width, r->height);
1475 if (crop->pad == FIMC_SD_PAD_SOURCE)
1476 ctx->state |= FIMC_DST_CROP;
1477
1478 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1479 spin_unlock_irqrestore(&fimc->slock, flags);
1480
1481 dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top,
1482 r->width, r->height);
1483
1484 mutex_unlock(&fimc->lock);
1485 return 0;
1486}
1487
1488static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1489 .enum_mbus_code = fimc_subdev_enum_mbus_code,
1490 .get_fmt = fimc_subdev_get_fmt,
1491 .set_fmt = fimc_subdev_set_fmt,
1492 .get_crop = fimc_subdev_get_crop,
1493 .set_crop = fimc_subdev_set_crop,
1494};
1495
1496static struct v4l2_subdev_ops fimc_subdev_ops = {
1497 .pad = &fimc_subdev_pad_ops,
1498};
1499
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001500/* Set default format at the sensor and host interface */
1501static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1502{
1503 struct v4l2_format fmt = {
1504 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1505 .fmt.pix_mp = {
1506 .width = 640,
1507 .height = 480,
1508 .pixelformat = V4L2_PIX_FMT_YUYV,
1509 .field = V4L2_FIELD_NONE,
1510 .colorspace = V4L2_COLORSPACE_JPEG,
1511 },
1512 };
1513
1514 return fimc_capture_set_format(fimc, &fmt);
1515}
1516
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001517/* fimc->lock must be already initialized */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001518static int fimc_register_capture_device(struct fimc_dev *fimc,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001519 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001520{
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001521 struct video_device *vfd;
1522 struct fimc_vid_cap *vid_cap;
1523 struct fimc_ctx *ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001524 struct vb2_queue *q;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001525 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001526
1527 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1528 if (!ctx)
1529 return -ENOMEM;
1530
1531 ctx->fimc_dev = fimc;
Sylwester Nawrocki3d112d92012-04-26 06:26:29 -03001532 ctx->in_path = FIMC_IO_CAMERA;
1533 ctx->out_path = FIMC_IO_DMA;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001534 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001535 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001536 ctx->d_frame.fmt = ctx->s_frame.fmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001537
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001538 vfd = video_device_alloc();
1539 if (!vfd) {
1540 v4l2_err(v4l2_dev, "Failed to allocate video device\n");
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001541 goto err_vd_alloc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001542 }
1543
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001544 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001545
1546 vfd->fops = &fimc_capture_fops;
1547 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001548 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001549 vfd->minor = -1;
1550 vfd->release = video_device_release;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001551 vfd->lock = &fimc->lock;
Hans Verkuil5126f252012-05-10 04:57:22 -03001552 /* Locking in file operations other than ioctl should be done
1553 by the driver, not the V4L2 core.
1554 This driver needs auditing so that this flag can be removed. */
1555 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001556 video_set_drvdata(vfd, fimc);
1557
1558 vid_cap = &fimc->vid_cap;
1559 vid_cap->vfd = vfd;
1560 vid_cap->active_buf_cnt = 0;
1561 vid_cap->reqbufs_count = 0;
1562 vid_cap->refcnt = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001563
1564 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1565 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001566 vid_cap->ctx = ctx;
1567
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001568 q = &fimc->vid_cap.vbq;
1569 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001570 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001571 q->io_modes = VB2_MMAP | VB2_USERPTR;
1572 q->drv_priv = fimc->vid_cap.ctx;
1573 q->ops = &fimc_capture_qops;
1574 q->mem_ops = &vb2_dma_contig_memops;
1575 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1576
1577 vb2_queue_init(q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001578
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001579 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1580 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001581 if (ret)
1582 goto err_ent;
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001583
1584 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001585 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001586 goto err_vd;
1587
1588 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1589 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001590
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -03001591 vfd->ctrl_handler = &ctx->ctrl_handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001592 return 0;
1593
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001594err_vd:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001595 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001596err_ent:
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001597 video_device_release(vfd);
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001598err_vd_alloc:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001599 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001600 return ret;
1601}
1602
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001603static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001604{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001605 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1606 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001607
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001608 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1609 if (ret)
1610 return ret;
1611
1612 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
1613 if (ret)
1614 fimc_unregister_m2m_device(fimc);
1615
1616 return ret;
1617}
1618
1619static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1620{
1621 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1622
1623 if (fimc == NULL)
1624 return;
1625
1626 fimc_unregister_m2m_device(fimc);
1627
1628 if (fimc->vid_cap.vfd) {
1629 media_entity_cleanup(&fimc->vid_cap.vfd->entity);
1630 video_unregister_device(fimc->vid_cap.vfd);
1631 fimc->vid_cap.vfd = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001632 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001633
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001634 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001635 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001636}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001637
1638static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1639 .registered = fimc_capture_subdev_registered,
1640 .unregistered = fimc_capture_subdev_unregistered,
1641};
1642
1643int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1644{
1645 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1646 int ret;
1647
1648 v4l2_subdev_init(sd, &fimc_subdev_ops);
1649 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1650 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1651
1652 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1653 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1654 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1655 fimc->vid_cap.sd_pads, 0);
1656 if (ret)
1657 return ret;
1658
1659 sd->entity.ops = &fimc_sd_media_ops;
1660 sd->internal_ops = &fimc_capture_sd_internal_ops;
1661 v4l2_set_subdevdata(sd, fimc);
1662 return 0;
1663}
1664
1665void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1666{
1667 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1668
1669 v4l2_device_unregister_subdev(sd);
1670 media_entity_cleanup(&sd->entity);
1671 v4l2_set_subdevdata(sd, NULL);
1672}