blob: 18a70e4a0b0f035484cc6a775a089fa662218ffd [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
Sylwester Nawrocki6612a082012-09-17 06:00:48 -030053 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);
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -030056 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 Nawrocki9448ab72012-04-02 06:41:22 -030065 fimc_hw_set_effect(ctx);
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 Nawrockib9ee31e62012-08-14 10:46:58 -0300121 return fimc_pipeline_call(fimc, set_stream,
122 &fimc->pipeline, 0);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300123 else
124 return 0;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300125}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300126
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300127static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend)
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300128{
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300129 unsigned long flags;
130
131 if (!fimc_capture_active(fimc))
132 return 0;
133
134 spin_lock_irqsave(&fimc->slock, flags);
135 set_bit(ST_CAPT_SHUT, &fimc->state);
136 fimc_deactivate_capture(fimc);
137 spin_unlock_irqrestore(&fimc->slock, flags);
138
139 wait_event_timeout(fimc->irq_queue,
140 !test_bit(ST_CAPT_SHUT, &fimc->state),
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300141 (2*HZ/10)); /* 200 ms */
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300142
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300143 return fimc_capture_state_cleanup(fimc, suspend);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300144}
145
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300146/**
147 * fimc_capture_config_update - apply the camera interface configuration
148 *
149 * To be called from within the interrupt handler with fimc.slock
150 * spinlock held. It updates the camera pixel crop, rotation and
151 * image flip in H/W.
152 */
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300153static int fimc_capture_config_update(struct fimc_ctx *ctx)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300154{
155 struct fimc_dev *fimc = ctx->fimc_dev;
156 int ret;
157
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300158 fimc_hw_set_camera_offset(fimc, &ctx->s_frame);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300159
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300160 ret = fimc_set_scaler_info(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300161 if (ret)
162 return ret;
163
164 fimc_hw_set_prescaler(ctx);
165 fimc_hw_set_mainscaler(ctx);
166 fimc_hw_set_target_format(ctx);
167 fimc_hw_set_rotation(ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300168 fimc_hw_set_effect(ctx);
Sylwester Nawrockiefb13c3d2012-03-19 13:11:40 -0300169 fimc_prepare_dma_offset(ctx, &ctx->d_frame);
170 fimc_hw_set_out_dma(ctx);
171 if (fimc->variant->has_alpha)
172 fimc_hw_set_rgb_alpha(ctx);
173
174 clear_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300175 return ret;
176}
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300177
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300178void fimc_capture_irq_handler(struct fimc_dev *fimc, int deq_buf)
179{
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300180 struct v4l2_subdev *csis = fimc->pipeline.subdevs[IDX_CSIS];
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300181 struct fimc_vid_cap *cap = &fimc->vid_cap;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300182 struct fimc_frame *f = &cap->ctx->d_frame;
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300183 struct fimc_vid_buffer *v_buf;
184 struct timeval *tv;
185 struct timespec ts;
186
187 if (test_and_clear_bit(ST_CAPT_SHUT, &fimc->state)) {
188 wake_up(&fimc->irq_queue);
189 goto done;
190 }
191
192 if (!list_empty(&cap->active_buf_q) &&
193 test_bit(ST_CAPT_RUN, &fimc->state) && deq_buf) {
194 ktime_get_real_ts(&ts);
195
196 v_buf = fimc_active_queue_pop(cap);
197
198 tv = &v_buf->vb.v4l2_buf.timestamp;
199 tv->tv_sec = ts.tv_sec;
200 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
201 v_buf->vb.v4l2_buf.sequence = cap->frame_count++;
202
203 vb2_buffer_done(&v_buf->vb, VB2_BUF_STATE_DONE);
204 }
205
206 if (!list_empty(&cap->pending_buf_q)) {
207
208 v_buf = fimc_pending_queue_pop(cap);
209 fimc_hw_set_output_addr(fimc, &v_buf->paddr, cap->buf_index);
210 v_buf->index = cap->buf_index;
211
212 /* Move the buffer to the capture active queue */
213 fimc_active_queue_add(cap, v_buf);
214
215 dbg("next frame: %d, done frame: %d",
216 fimc_hw_get_frame_index(fimc), v_buf->index);
217
218 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
219 cap->buf_index = 0;
220 }
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300221 /*
222 * Set up a buffer at MIPI-CSIS if current image format
223 * requires the frame embedded data capture.
224 */
225 if (f->fmt->mdataplanes && !list_empty(&cap->active_buf_q)) {
226 unsigned int plane = ffs(f->fmt->mdataplanes) - 1;
227 unsigned int size = f->payload[plane];
228 s32 index = fimc_hw_get_frame_index(fimc);
229 void *vaddr;
230
231 list_for_each_entry(v_buf, &cap->active_buf_q, list) {
232 if (v_buf->index != index)
233 continue;
234 vaddr = vb2_plane_vaddr(&v_buf->vb, plane);
235 v4l2_subdev_call(csis, video, s_rx_buffer,
236 vaddr, &size);
237 break;
238 }
239 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300240
241 if (cap->active_buf_cnt == 0) {
242 if (deq_buf)
243 clear_bit(ST_CAPT_RUN, &fimc->state);
244
245 if (++cap->buf_index >= FIMC_MAX_OUT_BUFS)
246 cap->buf_index = 0;
247 } else {
248 set_bit(ST_CAPT_RUN, &fimc->state);
249 }
250
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300251 if (test_bit(ST_CAPT_APPLY_CFG, &fimc->state))
252 fimc_capture_config_update(cap->ctx);
Sylwester Nawrocki97d97422012-05-08 15:51:24 -0300253done:
254 if (cap->active_buf_cnt == 1) {
255 fimc_deactivate_capture(fimc);
256 clear_bit(ST_CAPT_STREAM, &fimc->state);
257 }
258
259 dbg("frame: %d, active_buf_cnt: %d",
260 fimc_hw_get_frame_index(fimc), cap->active_buf_cnt);
261}
262
263
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300264static int start_streaming(struct vb2_queue *q, unsigned int count)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300265{
266 struct fimc_ctx *ctx = q->drv_priv;
267 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300268 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300269 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300270 int ret;
271
Sylwester Nawrocki9e803a02011-06-10 15:36:53 -0300272 vid_cap->frame_count = 0;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300273
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300274 ret = fimc_capture_hw_init(fimc);
275 if (ret) {
276 fimc_capture_state_cleanup(fimc, false);
277 return ret;
278 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300279
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300280 set_bit(ST_CAPT_PEND, &fimc->state);
281
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300282 min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1;
283
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300284 if (vid_cap->active_buf_cnt >= min_bufs &&
285 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300286 fimc_activate_capture(ctx);
287
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300288 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300289 fimc_pipeline_call(fimc, set_stream,
290 &fimc->pipeline, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300291 }
292
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300293 return 0;
294}
295
296static int stop_streaming(struct vb2_queue *q)
297{
298 struct fimc_ctx *ctx = q->drv_priv;
299 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300300
Sylwester Nawrocki4ecbf5d2011-02-23 08:24:33 -0300301 if (!fimc_capture_active(fimc))
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300302 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300303
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300304 return fimc_stop_capture(fimc, false);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300305}
306
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300307int fimc_capture_suspend(struct fimc_dev *fimc)
308{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300309 bool suspend = fimc_capture_busy(fimc);
310
311 int ret = fimc_stop_capture(fimc, suspend);
312 if (ret)
313 return ret;
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300314 return fimc_pipeline_call(fimc, close, &fimc->pipeline);
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300315}
316
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300317static void buffer_queue(struct vb2_buffer *vb);
318
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300319int fimc_capture_resume(struct fimc_dev *fimc)
320{
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300321 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
322 struct fimc_vid_buffer *buf;
323 int i;
324
325 if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state))
326 return 0;
327
328 INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q);
329 vid_cap->buf_index = 0;
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300330 fimc_pipeline_call(fimc, open, &fimc->pipeline,
331 &vid_cap->vfd.entity, false);
Sylwester Nawrockibb7c2762012-04-27 09:33:23 -0300332 fimc_capture_hw_init(fimc);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300333
334 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
335
336 for (i = 0; i < vid_cap->reqbufs_count; i++) {
337 if (list_empty(&vid_cap->pending_buf_q))
338 break;
339 buf = fimc_pending_queue_pop(vid_cap);
340 buffer_queue(&buf->vb);
341 }
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300342 return 0;
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300343
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300344}
345
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300346static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300347 unsigned int *num_buffers, unsigned int *num_planes,
348 unsigned int sizes[], void *allocators[])
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300349{
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300350 const struct v4l2_pix_format_mplane *pixm = NULL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300351 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300352 struct fimc_frame *frame = &ctx->d_frame;
353 struct fimc_fmt *fmt = frame->fmt;
354 unsigned long wh;
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300355 int i;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300356
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300357 if (pfmt) {
358 pixm = &pfmt->fmt.pix_mp;
359 fmt = fimc_find_format(&pixm->pixelformat, NULL,
360 FMT_FLAGS_CAM | FMT_FLAGS_M2M, -1);
361 wh = pixm->width * pixm->height;
362 } else {
363 wh = frame->f_width * frame->f_height;
364 }
365
366 if (fmt == NULL)
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300367 return -EINVAL;
368
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300369 *num_planes = fmt->memplanes;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300370
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300371 for (i = 0; i < fmt->memplanes; i++) {
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300372 unsigned int size = (wh * fmt->depth[i]) / 8;
373 if (pixm)
374 sizes[i] = max(size, pixm->plane_fmt[i].sizeimage);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300375 else if (fimc_fmt_is_user_defined(fmt->color))
376 sizes[i] = frame->payload[i];
Sylwester Nawrocki63746be2012-04-22 17:07:09 -0300377 else
Sylwester Nawrockid547ab62012-05-16 15:00:26 -0300378 sizes[i] = max_t(u32, size, frame->payload[i]);
379
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300380 allocators[i] = ctx->fimc_dev->alloc_ctx;
381 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300382
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300383 return 0;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300384}
385
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300386static int buffer_prepare(struct vb2_buffer *vb)
387{
388 struct vb2_queue *vq = vb->vb2_queue;
389 struct fimc_ctx *ctx = vq->drv_priv;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300390 int i;
391
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300392 if (ctx->d_frame.fmt == NULL)
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300393 return -EINVAL;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300394
Sylwester Nawrockief7af592010-12-08 14:05:08 -0300395 for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) {
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300396 unsigned long size = ctx->d_frame.payload[i];
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300397
398 if (vb2_plane_size(vb, i) < size) {
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -0300399 v4l2_err(&ctx->fimc_dev->vid_cap.vfd,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -0300400 "User buffer too small (%ld < %ld)\n",
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300401 vb2_plane_size(vb, i), size);
402 return -EINVAL;
403 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300404 vb2_set_plane_payload(vb, i, size);
405 }
406
407 return 0;
408}
409
410static void buffer_queue(struct vb2_buffer *vb)
411{
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300412 struct fimc_vid_buffer *buf
413 = container_of(vb, struct fimc_vid_buffer, vb);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300414 struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
415 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300416 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
417 unsigned long flags;
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300418 int min_bufs;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300419
420 spin_lock_irqsave(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300421 fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300422
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300423 if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) &&
424 !test_bit(ST_CAPT_STREAM, &fimc->state) &&
425 vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300426 /* Setup the buffer directly for processing. */
427 int buf_id = (vid_cap->reqbufs_count == 1) ? -1 :
428 vid_cap->buf_index;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300429
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300430 fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id);
431 buf->index = vid_cap->buf_index;
Sylwester Nawrocki02952022011-06-10 15:36:59 -0300432 fimc_active_queue_add(vid_cap, buf);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300433
434 if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS)
435 vid_cap->buf_index = 0;
436 } else {
437 fimc_pending_queue_add(vid_cap, buf);
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300438 }
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300439
440 min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1;
441
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300442
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300443 if (vb2_is_streaming(&vid_cap->vbq) &&
444 vid_cap->active_buf_cnt >= min_bufs &&
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300445 !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) {
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300446 fimc_activate_capture(ctx);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300447 spin_unlock_irqrestore(&fimc->slock, flags);
Sungchun Kang8ec737f2011-02-07 15:59:46 -0300448
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300449 if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state))
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300450 fimc_pipeline_call(fimc, set_stream,
451 &fimc->pipeline, 1);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300452 return;
453 }
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300454 spin_unlock_irqrestore(&fimc->slock, flags);
455}
456
457static void fimc_lock(struct vb2_queue *vq)
458{
459 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
460 mutex_lock(&ctx->fimc_dev->lock);
461}
462
463static void fimc_unlock(struct vb2_queue *vq)
464{
465 struct fimc_ctx *ctx = vb2_get_drv_priv(vq);
466 mutex_unlock(&ctx->fimc_dev->lock);
467}
468
469static struct vb2_ops fimc_capture_qops = {
470 .queue_setup = queue_setup,
471 .buf_prepare = buffer_prepare,
472 .buf_queue = buffer_queue,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -0300473 .wait_prepare = fimc_unlock,
474 .wait_finish = fimc_lock,
475 .start_streaming = start_streaming,
476 .stop_streaming = stop_streaming,
477};
478
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300479/**
480 * fimc_capture_ctrls_create - initialize the control handler
481 * Initialize the capture video node control handler and fill it
482 * with the FIMC controls. Inherit any sensor's controls if the
483 * 'user_subdev_api' flag is false (default behaviour).
484 * This function need to be called with the graph mutex held.
485 */
486int fimc_capture_ctrls_create(struct fimc_dev *fimc)
487{
488 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
489 int ret;
490
491 if (WARN_ON(vid_cap->ctx == NULL))
492 return -ENXIO;
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300493 if (vid_cap->ctx->ctrls.ready)
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300494 return 0;
495
496 ret = fimc_ctrls_create(vid_cap->ctx);
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300497 if (ret || vid_cap->user_subdev_api || !vid_cap->ctx->ctrls.ready)
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300498 return ret;
499
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -0300500 return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrls.handler,
Hans Verkuil34a6b7d2012-09-14 07:15:03 -0300501 fimc->pipeline.subdevs[IDX_SENSOR]->ctrl_handler, NULL);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300502}
503
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300504static int fimc_capture_set_default_format(struct fimc_dev *fimc);
505
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300506static int fimc_capture_open(struct file *file)
507{
508 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300509 int ret = -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300510
511 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
512
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300513 fimc_md_graph_lock(fimc);
514 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300515
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300516 if (fimc_m2m_active(fimc))
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300517 goto unlock;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300518
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300519 set_bit(ST_CAPT_BUSY, &fimc->state);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300520 ret = pm_runtime_get_sync(&fimc->pdev->dev);
521 if (ret < 0)
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300522 goto unlock;
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300523
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300524 ret = v4l2_fh_open(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300525 if (ret) {
526 pm_runtime_put(&fimc->pdev->dev);
527 goto unlock;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -0300528 }
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300529
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300530 if (++fimc->vid_cap.refcnt == 1) {
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300531 ret = fimc_pipeline_call(fimc, open, &fimc->pipeline,
532 &fimc->vid_cap.vfd.entity, true);
Sylwester Nawrockie3fc82e2012-05-17 14:22:10 -0300533
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300534 if (!ret && !fimc->vid_cap.user_subdev_api)
535 ret = fimc_capture_set_default_format(fimc);
536
537 if (!ret)
538 ret = fimc_capture_ctrls_create(fimc);
539
540 if (ret < 0) {
541 clear_bit(ST_CAPT_BUSY, &fimc->state);
542 pm_runtime_put_sync(&fimc->pdev->dev);
543 fimc->vid_cap.refcnt--;
544 v4l2_fh_release(file);
545 }
546 }
547unlock:
548 mutex_unlock(&fimc->lock);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300549 fimc_md_graph_unlock(fimc);
Sylwester Nawrocki131b6c62011-08-24 19:25:10 -0300550 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300551}
552
553static int fimc_capture_close(struct file *file)
554{
555 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300556 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300557
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300558 dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state);
559
Sylwester Nawrockiba6b3722012-11-22 11:12:16 -0300560 mutex_lock(&fimc->lock);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300561
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300562 if (--fimc->vid_cap.refcnt == 0) {
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300563 clear_bit(ST_CAPT_BUSY, &fimc->state);
564 fimc_stop_capture(fimc, false);
Sylwester Nawrockib9ee31e62012-08-14 10:46:58 -0300565 fimc_pipeline_call(fimc, close, &fimc->pipeline);
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300566 clear_bit(ST_CAPT_SUSPENDED, &fimc->state);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300567 }
568
Sylwester Nawrockie9e21082011-09-02 06:25:32 -0300569 pm_runtime_put(&fimc->pdev->dev);
570
Sylwester Nawrocki3e4748d2011-08-24 20:45:34 -0300571 if (fimc->vid_cap.refcnt == 0) {
572 vb2_queue_release(&fimc->vid_cap.vbq);
573 fimc_ctrls_delete(fimc->vid_cap.ctx);
574 }
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300575
576 ret = v4l2_fh_release(file);
577
578 mutex_unlock(&fimc->lock);
579 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300580}
581
582static unsigned int fimc_capture_poll(struct file *file,
583 struct poll_table_struct *wait)
584{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300585 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300586 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300587
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300588 if (mutex_lock_interruptible(&fimc->lock))
589 return POLL_ERR;
590
591 ret = vb2_poll(&fimc->vid_cap.vbq, file, wait);
592 mutex_unlock(&fimc->lock);
593
594 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300595}
596
597static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma)
598{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300599 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300600 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300601
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -0300602 if (mutex_lock_interruptible(&fimc->lock))
603 return -ERESTARTSYS;
604
605 ret = vb2_mmap(&fimc->vid_cap.vbq, vma);
606 mutex_unlock(&fimc->lock);
607
608 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300609}
610
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300611static const struct v4l2_file_operations fimc_capture_fops = {
612 .owner = THIS_MODULE,
613 .open = fimc_capture_open,
614 .release = fimc_capture_close,
615 .poll = fimc_capture_poll,
616 .unlocked_ioctl = video_ioctl2,
617 .mmap = fimc_capture_mmap,
618};
619
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300620/*
621 * Format and crop negotiation helpers
622 */
623
624static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx,
625 u32 *width, u32 *height,
626 u32 *code, u32 *fourcc, int pad)
627{
628 bool rotation = ctx->rotation == 90 || ctx->rotation == 270;
629 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300630 const struct fimc_variant *var = fimc->variant;
631 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300632 struct fimc_frame *dst = &ctx->d_frame;
633 u32 depth, min_w, max_w, min_h, align_h = 3;
634 u32 mask = FMT_FLAGS_CAM;
635 struct fimc_fmt *ffmt;
636
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300637 /* Conversion from/to JPEG or User Defined format is not supported */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300638 if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE &&
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300639 fimc_fmt_is_user_defined(ctx->s_frame.fmt->color))
640 *code = ctx->s_frame.fmt->mbus_code;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300641
642 if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK)
643 mask |= FMT_FLAGS_M2M;
644
645 ffmt = fimc_find_format(fourcc, code, mask, 0);
646 if (WARN_ON(!ffmt))
647 return NULL;
648 if (code)
649 *code = ffmt->mbus_code;
650 if (fourcc)
651 *fourcc = ffmt->fourcc;
652
653 if (pad == FIMC_SD_PAD_SINK) {
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300654 max_w = fimc_fmt_is_user_defined(ffmt->color) ?
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300655 pl->scaler_dis_w : pl->scaler_en_w;
656 /* Apply the camera input interface pixel constraints */
657 v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4,
658 height, max_t(u32, *height, 32),
659 FIMC_CAMIF_MAX_HEIGHT,
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300660 fimc_fmt_is_user_defined(ffmt->color) ?
661 3 : 1,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300662 0);
663 return ffmt;
664 }
665 /* Can't scale or crop in transparent (JPEG) transfer mode */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300666 if (fimc_fmt_is_user_defined(ffmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300667 *width = ctx->s_frame.f_width;
668 *height = ctx->s_frame.f_height;
669 return ffmt;
670 }
671 /* Apply the scaler and the output DMA constraints */
672 max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300673 if (ctx->state & FIMC_COMPOSE) {
674 min_w = dst->offs_h + dst->width;
675 min_h = dst->offs_v + dst->height;
676 } else {
677 min_w = var->min_out_pixsize;
678 min_h = var->min_out_pixsize;
679 }
Sylwester Nawrocki9c63afc2011-05-27 13:12:23 -0300680 if (var->min_vsize_align == 1 && !rotation)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300681 align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1;
682
683 depth = fimc_get_format_depth(ffmt);
684 v4l_bound_align_image(width, min_w, max_w,
685 ffs(var->min_out_pixsize) - 1,
686 height, min_h, FIMC_CAMIF_MAX_HEIGHT,
687 align_h,
688 64/(ALIGN(depth, 8)));
689
690 dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d",
691 pad, code ? *code : 0, *width, *height,
692 dst->f_width, dst->f_height);
693
694 return ffmt;
695}
696
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300697static void fimc_capture_try_selection(struct fimc_ctx *ctx,
698 struct v4l2_rect *r,
699 int target)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300700{
701 bool rotate = ctx->rotation == 90 || ctx->rotation == 270;
702 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki405f2302012-08-02 10:27:46 -0300703 const struct fimc_variant *var = fimc->variant;
704 const struct fimc_pix_limit *pl = var->pix_limit;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300705 struct fimc_frame *sink = &ctx->s_frame;
706 u32 max_w, max_h, min_w = 0, min_h = 0, min_sz;
707 u32 align_sz = 0, align_h = 4;
708 u32 max_sc_h, max_sc_v;
709
710 /* In JPEG transparent transfer mode cropping is not supported */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300711 if (fimc_fmt_is_user_defined(ctx->d_frame.fmt->color)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300712 r->width = sink->f_width;
713 r->height = sink->f_height;
714 r->left = r->top = 0;
715 return;
716 }
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300717 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300718 if (ctx->rotation != 90 && ctx->rotation != 270)
719 align_h = 1;
720 max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3));
721 max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1));
722 min_sz = var->min_out_pixsize;
723 } else {
724 u32 depth = fimc_get_format_depth(sink->fmt);
725 align_sz = 64/ALIGN(depth, 8);
726 min_sz = var->min_inp_pixsize;
727 min_w = min_h = min_sz;
728 max_sc_h = max_sc_v = 1;
729 }
730 /*
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300731 * For the compose rectangle the following constraints must be met:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300732 * - it must fit in the sink pad format rectangle (f_width/f_height);
733 * - maximum downscaling ratio is 64;
734 * - maximum crop size depends if the rotator is used or not;
735 * - the sink pad format width/height must be 4 multiple of the
736 * prescaler ratios determined by sink pad size and source pad crop,
737 * the prescaler ratio is returned by fimc_get_scaler_factor().
738 */
739 max_w = min_t(u32,
740 rotate ? pl->out_rot_en_w : pl->out_rot_dis_w,
741 rotate ? sink->f_height : sink->f_width);
742 max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300743
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300744 if (target == V4L2_SEL_TGT_COMPOSE) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300745 min_w = min_t(u32, max_w, sink->f_width / max_sc_h);
746 min_h = min_t(u32, max_h, sink->f_height / max_sc_v);
747 if (rotate) {
748 swap(max_sc_h, max_sc_v);
749 swap(min_w, min_h);
750 }
751 }
752 v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1,
753 &r->height, min_h, max_h, align_h,
754 align_sz);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300755 /* Adjust left/top if crop/compose rectangle is out of bounds */
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300756 r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width);
757 r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height);
758 r->left = round_down(r->left, var->hor_offs_align);
759
Sylwester Nawrockifed07f82012-04-29 17:46:03 -0300760 dbg("target %#x: (%d,%d)/%dx%d, sink fmt: %dx%d",
761 target, r->left, r->top, r->width, r->height,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300762 sink->f_width, sink->f_height);
763}
764
765/*
766 * The video node ioctl operations
767 */
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300768static int fimc_vidioc_querycap_capture(struct file *file, void *priv,
769 struct v4l2_capability *cap)
770{
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300771 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300772
773 strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1);
774 strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1);
775 cap->bus_info[0] = 0;
Sylwester Nawrocki8f401542011-07-07 06:42:05 -0300776 cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300777
778 return 0;
779}
780
Sylwester Nawrockicf52df82011-06-13 11:09:40 -0300781static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv,
782 struct v4l2_fmtdesc *f)
783{
784 struct fimc_fmt *fmt;
785
786 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M,
787 f->index);
788 if (!fmt)
789 return -EINVAL;
790 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
791 f->pixelformat = fmt->fourcc;
792 if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8)
793 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
794 return 0;
795}
796
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300797static struct media_entity *fimc_pipeline_get_head(struct media_entity *me)
798{
799 struct media_pad *pad = &me->pads[0];
800
801 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) {
802 pad = media_entity_remote_source(pad);
803 if (!pad)
804 break;
805 me = pad->entity;
806 pad = &me->pads[0];
807 }
808
809 return me;
810}
811
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300812/**
813 * fimc_pipeline_try_format - negotiate and/or set formats at pipeline
814 * elements
815 * @ctx: FIMC capture context
816 * @tfmt: media bus format to try/set on subdevs
817 * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output)
818 * @set: true to set format on subdevs, false to try only
819 */
820static int fimc_pipeline_try_format(struct fimc_ctx *ctx,
821 struct v4l2_mbus_framefmt *tfmt,
822 struct fimc_fmt **fmt_id,
823 bool set)
824{
825 struct fimc_dev *fimc = ctx->fimc_dev;
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -0300826 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300827 struct v4l2_subdev_format sfmt;
828 struct v4l2_mbus_framefmt *mf = &sfmt.format;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300829 struct media_entity *me;
830 struct fimc_fmt *ffmt;
831 struct media_pad *pad;
832 int ret, i = 1;
833 u32 fcc;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300834
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300835 if (WARN_ON(!sd || !tfmt))
836 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300837
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300838 memset(&sfmt, 0, sizeof(sfmt));
839 sfmt.format = *tfmt;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300840 sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY;
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300841
842 me = fimc_pipeline_get_head(&sd->entity);
843
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300844 while (1) {
845 ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL,
846 FMT_FLAGS_CAM, i++);
847 if (ffmt == NULL) {
848 /*
849 * Notify user-space if common pixel code for
850 * host and sensor does not exist.
851 */
852 return -EINVAL;
853 }
854 mf->code = tfmt->code = ffmt->mbus_code;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300855
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300856 /* set format on all pipeline subdevs */
857 while (me != &fimc->vid_cap.subdev.entity) {
858 sd = media_entity_to_v4l2_subdev(me);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300859
Sylwester Nawrocki47800bc2012-12-03 06:24:32 -0300860 sfmt.pad = 0;
861 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt);
862 if (ret)
863 return ret;
864
865 if (me->pads[0].flags & MEDIA_PAD_FL_SINK) {
866 sfmt.pad = me->num_pads - 1;
867 mf->code = tfmt->code;
868 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL,
869 &sfmt);
870 if (ret)
871 return ret;
872 }
873
874 pad = media_entity_remote_source(&me->pads[sfmt.pad]);
875 if (!pad)
876 return -EINVAL;
877 me = pad->entity;
878 }
879
880 if (mf->code != tfmt->code)
881 continue;
882
883 fcc = ffmt->fourcc;
884 tfmt->width = mf->width;
885 tfmt->height = mf->height;
886 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
887 NULL, &fcc, FIMC_SD_PAD_SINK);
888 ffmt = fimc_capture_try_format(ctx, &tfmt->width, &tfmt->height,
889 NULL, &fcc, FIMC_SD_PAD_SOURCE);
890 if (ffmt && ffmt->mbus_code)
891 mf->code = ffmt->mbus_code;
892 if (mf->width != tfmt->width || mf->height != tfmt->height)
893 continue;
894 tfmt->code = mf->code;
895 break;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300896 }
897
898 if (fmt_id && ffmt)
899 *fmt_id = ffmt;
900 *tfmt = *mf;
901
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300902 return 0;
903}
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -0300904
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300905/**
906 * fimc_get_sensor_frame_desc - query the sensor for media bus frame parameters
907 * @sensor: pointer to the sensor subdev
908 * @plane_fmt: provides plane sizes corresponding to the frame layout entries
909 * @try: true to set the frame parameters, false to query only
910 *
911 * This function is used by this driver only for compressed/blob data formats.
912 */
913static int fimc_get_sensor_frame_desc(struct v4l2_subdev *sensor,
914 struct v4l2_plane_pix_format *plane_fmt,
915 unsigned int num_planes, bool try)
916{
917 struct v4l2_mbus_frame_desc fd;
918 int i, ret;
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300919 int pad;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300920
921 for (i = 0; i < num_planes; i++)
922 fd.entry[i].length = plane_fmt[i].sizeimage;
923
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300924 pad = sensor->entity.num_pads - 1;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300925 if (try)
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300926 ret = v4l2_subdev_call(sensor, pad, set_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300927 else
Andrzej Hajda1c9f5bd2012-11-22 12:13:27 -0300928 ret = v4l2_subdev_call(sensor, pad, get_frame_desc, pad, &fd);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300929
930 if (ret < 0)
931 return ret;
932
933 if (num_planes != fd.num_entries)
934 return -EINVAL;
935
936 for (i = 0; i < num_planes; i++)
937 plane_fmt[i].sizeimage = fd.entry[i].length;
938
939 if (fd.entry[0].length > FIMC_MAX_JPEG_BUF_SIZE) {
940 v4l2_err(sensor->v4l2_dev, "Unsupported buffer size: %u\n",
941 fd.entry[0].length);
942
943 return -EINVAL;
944 }
945
946 return 0;
947}
948
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300949static int fimc_cap_g_fmt_mplane(struct file *file, void *fh,
950 struct v4l2_format *f)
951{
952 struct fimc_dev *fimc = video_drvdata(file);
953 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
954
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300955 return fimc_fill_format(&ctx->d_frame, f);
956}
957
958static int fimc_cap_try_fmt_mplane(struct file *file, void *fh,
959 struct v4l2_format *f)
960{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300961 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300962 struct fimc_dev *fimc = video_drvdata(file);
963 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300964 struct v4l2_mbus_framefmt mf;
965 struct fimc_fmt *ffmt = NULL;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300966 int ret = 0;
967
968 fimc_md_graph_lock(fimc);
969 mutex_lock(&fimc->lock);
Sylwester Nawrockie5785882011-06-10 15:36:50 -0300970
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300971 if (fimc_jpeg_fourcc(pix->pixelformat)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300972 fimc_capture_try_format(ctx, &pix->width, &pix->height,
973 NULL, &pix->pixelformat,
974 FIMC_SD_PAD_SINK);
975 ctx->s_frame.f_width = pix->width;
976 ctx->s_frame.f_height = pix->height;
977 }
978 ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
979 NULL, &pix->pixelformat,
980 FIMC_SD_PAD_SOURCE);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -0300981 if (!ffmt) {
982 ret = -EINVAL;
983 goto unlock;
984 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300985
986 if (!fimc->vid_cap.user_subdev_api) {
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300987 mf.width = pix->width;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300988 mf.height = pix->height;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300989 mf.code = ffmt->mbus_code;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300990 fimc_pipeline_try_format(ctx, &mf, &ffmt, false);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300991 pix->width = mf.width;
992 pix->height = mf.height;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -0300993 if (ffmt)
994 pix->pixelformat = ffmt->fourcc;
995 }
996
997 fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -0300998
999 if (ffmt->flags & FMT_FLAGS_COMPRESSED)
1000 fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
1001 pix->plane_fmt, ffmt->memplanes, true);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001002unlock:
1003 mutex_unlock(&fimc->lock);
1004 fimc_md_graph_unlock(fimc);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001005
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001006 return ret;
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001007}
1008
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001009static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx,
1010 enum fimc_color_fmt color)
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001011{
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001012 bool jpeg = fimc_fmt_is_user_defined(color);
1013
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001014 ctx->scaler.enabled = !jpeg;
1015 fimc_ctrls_activate(ctx, !jpeg);
1016
1017 if (jpeg)
1018 set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1019 else
1020 clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state);
1021}
1022
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001023static int __fimc_capture_set_format(struct fimc_dev *fimc,
1024 struct v4l2_format *f)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001025{
1026 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1027 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
1028 struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf;
1029 struct fimc_frame *ff = &ctx->d_frame;
1030 struct fimc_fmt *s_fmt = NULL;
1031 int ret, i;
1032
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001033 if (vb2_is_busy(&fimc->vid_cap.vbq))
1034 return -EBUSY;
1035
1036 /* Pre-configure format at camera interface input, for JPEG only */
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001037 if (fimc_jpeg_fourcc(pix->pixelformat)) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001038 fimc_capture_try_format(ctx, &pix->width, &pix->height,
1039 NULL, &pix->pixelformat,
1040 FIMC_SD_PAD_SINK);
1041 ctx->s_frame.f_width = pix->width;
1042 ctx->s_frame.f_height = pix->height;
1043 }
1044 /* Try the format at the scaler and the DMA output */
1045 ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height,
1046 NULL, &pix->pixelformat,
1047 FIMC_SD_PAD_SOURCE);
1048 if (!ff->fmt)
1049 return -EINVAL;
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001050
1051 /* Update RGB Alpha control state and value range */
1052 fimc_alpha_ctrl_update(ctx);
1053
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001054 /* Try to match format at the host and the sensor */
1055 if (!fimc->vid_cap.user_subdev_api) {
1056 mf->code = ff->fmt->mbus_code;
1057 mf->width = pix->width;
1058 mf->height = pix->height;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001059 ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001060 if (ret)
1061 return ret;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001062
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001063 pix->width = mf->width;
1064 pix->height = mf->height;
1065 }
Sylwester Nawrockid547ab62012-05-16 15:00:26 -03001066
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001067 fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix);
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001068
1069 if (ff->fmt->flags & FMT_FLAGS_COMPRESSED) {
1070 ret = fimc_get_sensor_frame_desc(fimc->pipeline.subdevs[IDX_SENSOR],
1071 pix->plane_fmt, ff->fmt->memplanes,
1072 true);
1073 if (ret < 0)
1074 return ret;
1075 }
1076
1077 for (i = 0; i < ff->fmt->memplanes; i++)
Sylwester Nawrockid547ab62012-05-16 15:00:26 -03001078 ff->payload[i] = pix->plane_fmt[i].sizeimage;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001079
1080 set_frame_bounds(ff, pix->width, pix->height);
1081 /* Reset the composition rectangle if not yet configured */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001082 if (!(ctx->state & FIMC_COMPOSE))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001083 set_frame_crop(ff, 0, 0, pix->width, pix->height);
1084
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001085 fimc_capture_mark_jpeg_xfer(ctx, ff->fmt->color);
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001086
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001087 /* Reset cropping and set format at the camera interface input */
1088 if (!fimc->vid_cap.user_subdev_api) {
1089 ctx->s_frame.fmt = s_fmt;
1090 set_frame_bounds(&ctx->s_frame, pix->width, pix->height);
1091 set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height);
1092 }
1093
1094 return ret;
1095}
1096
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001097static int fimc_cap_s_fmt_mplane(struct file *file, void *priv,
1098 struct v4l2_format *f)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001099{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001100 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001101 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001102
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001103 fimc_md_graph_lock(fimc);
1104 mutex_lock(&fimc->lock);
1105 /*
1106 * The graph is walked within __fimc_capture_set_format() to set
1107 * the format at subdevs thus the graph mutex needs to be held at
1108 * this point and acquired before the video mutex, to avoid AB-BA
1109 * deadlock when fimc_md_link_notify() is called by other thread.
1110 * Ideally the graph walking and setting format at the whole pipeline
1111 * should be removed from this driver and handled in userspace only.
1112 */
1113 ret = __fimc_capture_set_format(fimc, f);
1114
1115 mutex_unlock(&fimc->lock);
1116 fimc_md_graph_unlock(fimc);
1117 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001118}
1119
1120static int fimc_cap_enum_input(struct file *file, void *priv,
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001121 struct v4l2_input *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001122{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001123 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001124 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001125
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001126 if (i->index != 0)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001127 return -EINVAL;
1128
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001129 i->type = V4L2_INPUT_TYPE_CAMERA;
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001130 if (sd)
1131 strlcpy(i->name, sd->name, sizeof(i->name));
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001132 return 0;
1133}
1134
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001135static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001136{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001137 return i == 0 ? i : -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001138}
1139
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001140static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001141{
Sylwester Nawrocki3e002182011-07-26 18:27:59 -03001142 *i = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001143 return 0;
1144}
1145
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001146/**
1147 * fimc_pipeline_validate - check for formats inconsistencies
1148 * between source and sink pad of each link
1149 *
1150 * Return 0 if all formats match or -EPIPE otherwise.
1151 */
1152static int fimc_pipeline_validate(struct fimc_dev *fimc)
1153{
1154 struct v4l2_subdev_format sink_fmt, src_fmt;
1155 struct fimc_vid_cap *vid_cap = &fimc->vid_cap;
1156 struct v4l2_subdev *sd;
1157 struct media_pad *pad;
1158 int ret;
1159
1160 /* Start with the video capture node pad */
1161 pad = media_entity_remote_source(&vid_cap->vd_pad);
1162 if (pad == NULL)
1163 return -EPIPE;
1164 /* FIMC.{N} subdevice */
1165 sd = media_entity_to_v4l2_subdev(pad->entity);
1166
1167 while (1) {
1168 /* Retrieve format at the sink pad */
1169 pad = &sd->entity.pads[0];
1170 if (!(pad->flags & MEDIA_PAD_FL_SINK))
1171 break;
1172 /* Don't call FIMC subdev operation to avoid nested locking */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001173 if (sd == &fimc->vid_cap.subdev) {
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001174 struct fimc_frame *ff = &vid_cap->ctx->s_frame;
1175 sink_fmt.format.width = ff->f_width;
1176 sink_fmt.format.height = ff->f_height;
1177 sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0;
1178 } else {
1179 sink_fmt.pad = pad->index;
1180 sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1181 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt);
1182 if (ret < 0 && ret != -ENOIOCTLCMD)
1183 return -EPIPE;
1184 }
1185 /* Retrieve format at the source pad */
1186 pad = media_entity_remote_source(pad);
1187 if (pad == NULL ||
1188 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1189 break;
1190
1191 sd = media_entity_to_v4l2_subdev(pad->entity);
1192 src_fmt.pad = pad->index;
1193 src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1194 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt);
1195 if (ret < 0 && ret != -ENOIOCTLCMD)
1196 return -EPIPE;
1197
1198 if (src_fmt.format.width != sink_fmt.format.width ||
1199 src_fmt.format.height != sink_fmt.format.height ||
1200 src_fmt.format.code != sink_fmt.format.code)
1201 return -EPIPE;
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001202
1203 if (sd == fimc->pipeline.subdevs[IDX_SENSOR] &&
1204 fimc_user_defined_mbus_fmt(src_fmt.format.code)) {
1205 struct v4l2_plane_pix_format plane_fmt[FIMC_MAX_PLANES];
1206 struct fimc_frame *frame = &vid_cap->ctx->d_frame;
1207 unsigned int i;
1208
1209 ret = fimc_get_sensor_frame_desc(sd, plane_fmt,
1210 frame->fmt->memplanes,
1211 false);
1212 if (ret < 0)
1213 return -EPIPE;
1214
1215 for (i = 0; i < frame->fmt->memplanes; i++)
1216 if (frame->payload[i] < plane_fmt[i].sizeimage)
1217 return -EPIPE;
1218 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001219 }
1220 return 0;
1221}
1222
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001223static int fimc_cap_streamon(struct file *file, void *priv,
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001224 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001225{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001226 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001227 struct fimc_pipeline *p = &fimc->pipeline;
Sachin Kamatf676fa02012-05-25 03:29:40 -03001228 struct v4l2_subdev *sd = p->subdevs[IDX_SENSOR];
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001229 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001230
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001231 if (fimc_capture_active(fimc))
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001232 return -EBUSY;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001233
Sachin Kamatf676fa02012-05-25 03:29:40 -03001234 ret = media_entity_pipeline_start(&sd->entity, p->m_pipeline);
Sakari Ailusa60a2952012-05-25 07:08:05 -03001235 if (ret < 0)
1236 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001237
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001238 if (fimc->vid_cap.user_subdev_api) {
1239 ret = fimc_pipeline_validate(fimc);
Sachin Kamatf676fa02012-05-25 03:29:40 -03001240 if (ret < 0) {
1241 media_entity_pipeline_stop(&sd->entity);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001242 return ret;
Sachin Kamatf676fa02012-05-25 03:29:40 -03001243 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001244 }
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001245 return vb2_streamon(&fimc->vid_cap.vbq, type);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001246}
1247
1248static int fimc_cap_streamoff(struct file *file, void *priv,
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001249 enum v4l2_buf_type type)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001250{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001251 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki0f735f52012-04-27 09:33:10 -03001252 struct v4l2_subdev *sd = fimc->pipeline.subdevs[IDX_SENSOR];
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001253 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001254
Sylwester Nawrocki4db5e272011-08-26 14:51:00 -03001255 ret = vb2_streamoff(&fimc->vid_cap.vbq, type);
1256 if (ret == 0)
1257 media_entity_pipeline_stop(&sd->entity);
1258 return ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001259}
1260
1261static int fimc_cap_reqbufs(struct file *file, void *priv,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001262 struct v4l2_requestbuffers *reqbufs)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001263{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001264 struct fimc_dev *fimc = video_drvdata(file);
1265 int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001266
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001267 if (!ret)
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001268 fimc->vid_cap.reqbufs_count = reqbufs->count;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001269 return ret;
1270}
1271
1272static int fimc_cap_querybuf(struct file *file, void *priv,
1273 struct v4l2_buffer *buf)
1274{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001275 struct fimc_dev *fimc = video_drvdata(file);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001276
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001277 return vb2_querybuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001278}
1279
1280static int fimc_cap_qbuf(struct file *file, void *priv,
1281 struct v4l2_buffer *buf)
1282{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001283 struct fimc_dev *fimc = video_drvdata(file);
1284
1285 return vb2_qbuf(&fimc->vid_cap.vbq, buf);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001286}
1287
Tomasz Stanislawskib28d61b2012-06-14 11:32:26 -03001288static int fimc_cap_expbuf(struct file *file, void *priv,
1289 struct v4l2_exportbuffer *eb)
1290{
1291 struct fimc_dev *fimc = video_drvdata(file);
1292
1293 return vb2_expbuf(&fimc->vid_cap.vbq, eb);
1294}
1295
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001296static int fimc_cap_dqbuf(struct file *file, void *priv,
1297 struct v4l2_buffer *buf)
1298{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001299 struct fimc_dev *fimc = video_drvdata(file);
1300
1301 return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001302}
1303
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001304static int fimc_cap_create_bufs(struct file *file, void *priv,
1305 struct v4l2_create_buffers *create)
1306{
1307 struct fimc_dev *fimc = video_drvdata(file);
1308
1309 return vb2_create_bufs(&fimc->vid_cap.vbq, create);
1310}
1311
1312static int fimc_cap_prepare_buf(struct file *file, void *priv,
1313 struct v4l2_buffer *b)
1314{
1315 struct fimc_dev *fimc = video_drvdata(file);
1316
1317 return vb2_prepare_buf(&fimc->vid_cap.vbq, b);
1318}
1319
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001320static int fimc_cap_g_selection(struct file *file, void *fh,
1321 struct v4l2_selection *s)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001322{
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001323 struct fimc_dev *fimc = video_drvdata(file);
1324 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001325 struct fimc_frame *f = &ctx->s_frame;
1326
1327 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1328 return -EINVAL;
1329
1330 switch (s->target) {
1331 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1332 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1333 f = &ctx->d_frame;
1334 case V4L2_SEL_TGT_CROP_BOUNDS:
1335 case V4L2_SEL_TGT_CROP_DEFAULT:
1336 s->r.left = 0;
1337 s->r.top = 0;
1338 s->r.width = f->o_width;
1339 s->r.height = f->o_height;
1340 return 0;
1341
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001342 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001343 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001344 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001345 s->r.left = f->offs_h;
1346 s->r.top = f->offs_v;
1347 s->r.width = f->width;
1348 s->r.height = f->height;
1349 return 0;
1350 }
1351
1352 return -EINVAL;
1353}
1354
1355/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
Sachin Kamat7e566be2012-05-26 11:11:54 -03001356static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001357{
1358 if (a->left < b->left || a->top < b->top)
1359 return 0;
1360 if (a->left + a->width > b->left + b->width)
1361 return 0;
1362 if (a->top + a->height > b->top + b->height)
1363 return 0;
1364
1365 return 1;
1366}
1367
1368static int fimc_cap_s_selection(struct file *file, void *fh,
1369 struct v4l2_selection *s)
1370{
1371 struct fimc_dev *fimc = video_drvdata(file);
1372 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1373 struct v4l2_rect rect = s->r;
1374 struct fimc_frame *f;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001375 unsigned long flags;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001376
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001377 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1378 return -EINVAL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001379
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001380 if (s->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001381 f = &ctx->d_frame;
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001382 else if (s->target == V4L2_SEL_TGT_CROP)
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001383 f = &ctx->s_frame;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001384 else
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001385 return -EINVAL;
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001386
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001387 fimc_capture_try_selection(ctx, &rect, s->target);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001388
1389 if (s->flags & V4L2_SEL_FLAG_LE &&
1390 !enclosed_rectangle(&rect, &s->r))
1391 return -ERANGE;
1392
1393 if (s->flags & V4L2_SEL_FLAG_GE &&
1394 !enclosed_rectangle(&s->r, &rect))
1395 return -ERANGE;
1396
1397 s->r = rect;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001398 spin_lock_irqsave(&fimc->slock, flags);
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001399 set_frame_crop(f, s->r.left, s->r.top, s->r.width,
1400 s->r.height);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001401 spin_unlock_irqrestore(&fimc->slock, flags);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001402
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001403 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001404 return 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001405}
1406
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001407static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
1408 .vidioc_querycap = fimc_vidioc_querycap_capture,
1409
Sylwester Nawrockicf52df82011-06-13 11:09:40 -03001410 .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001411 .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane,
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001412 .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane,
Sylwester Nawrockie5785882011-06-10 15:36:50 -03001413 .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001414
1415 .vidioc_reqbufs = fimc_cap_reqbufs,
1416 .vidioc_querybuf = fimc_cap_querybuf,
1417
1418 .vidioc_qbuf = fimc_cap_qbuf,
1419 .vidioc_dqbuf = fimc_cap_dqbuf,
Tomasz Stanislawskib28d61b2012-06-14 11:32:26 -03001420 .vidioc_expbuf = fimc_cap_expbuf,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001421
Sylwester Nawrocki3b4c34a2012-01-30 14:56:59 -03001422 .vidioc_prepare_buf = fimc_cap_prepare_buf,
1423 .vidioc_create_bufs = fimc_cap_create_bufs,
1424
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001425 .vidioc_streamon = fimc_cap_streamon,
1426 .vidioc_streamoff = fimc_cap_streamoff,
1427
Sylwester Nawrockif9331d12012-02-04 17:03:54 -03001428 .vidioc_g_selection = fimc_cap_g_selection,
1429 .vidioc_s_selection = fimc_cap_s_selection,
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001430
1431 .vidioc_enum_input = fimc_cap_enum_input,
1432 .vidioc_s_input = fimc_cap_s_input,
1433 .vidioc_g_input = fimc_cap_g_input,
1434};
1435
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001436/* Capture subdev media entity operations */
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001437static int fimc_link_setup(struct media_entity *entity,
1438 const struct media_pad *local,
1439 const struct media_pad *remote, u32 flags)
1440{
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001441 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1442 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1443
1444 if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
1445 return -EINVAL;
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001446
1447 if (WARN_ON(fimc == NULL))
1448 return 0;
1449
1450 dbg("%s --> %s, flags: 0x%x. input: 0x%x",
1451 local->entity->name, remote->entity->name, flags,
1452 fimc->vid_cap.input);
1453
1454 if (flags & MEDIA_LNK_FL_ENABLED) {
1455 if (fimc->vid_cap.input != 0)
1456 return -EBUSY;
1457 fimc->vid_cap.input = sd->grp_id;
1458 return 0;
1459 }
1460
1461 fimc->vid_cap.input = 0;
1462 return 0;
1463}
1464
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001465static const struct media_entity_operations fimc_sd_media_ops = {
Sylwester Nawrockid09a7dc2011-08-24 19:28:18 -03001466 .link_setup = fimc_link_setup,
1467};
1468
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001469/**
1470 * fimc_sensor_notify - v4l2_device notification from a sensor subdev
1471 * @sd: pointer to a subdev generating the notification
1472 * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY
1473 * @arg: pointer to an u32 type integer that stores the frame payload value
1474 *
1475 * The End Of Frame notification sent by sensor subdev in its still capture
1476 * mode. If there is only a single VSYNC generated by the sensor at the
1477 * beginning of a frame transmission, FIMC does not issue the LastIrq
1478 * (end of frame) interrupt. And this notification is used to complete the
1479 * frame capture and returning a buffer to user-space. Subdev drivers should
1480 * call this notification from their last 'End of frame capture' interrupt.
1481 */
1482void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
1483 void *arg)
1484{
1485 struct fimc_sensor_info *sensor;
1486 struct fimc_vid_buffer *buf;
1487 struct fimc_md *fmd;
1488 struct fimc_dev *fimc;
1489 unsigned long flags;
1490
1491 if (sd == NULL)
1492 return;
1493
1494 sensor = v4l2_get_subdev_hostdata(sd);
1495 fmd = entity_to_fimc_mdev(&sd->entity);
1496
1497 spin_lock_irqsave(&fmd->slock, flags);
1498 fimc = sensor ? sensor->host : NULL;
1499
1500 if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY &&
1501 test_bit(ST_CAPT_PEND, &fimc->state)) {
1502 unsigned long irq_flags;
1503 spin_lock_irqsave(&fimc->slock, irq_flags);
1504 if (!list_empty(&fimc->vid_cap.active_buf_q)) {
1505 buf = list_entry(fimc->vid_cap.active_buf_q.next,
1506 struct fimc_vid_buffer, list);
1507 vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg));
1508 }
Sylwester Nawrocki97d97422012-05-08 15:51:24 -03001509 fimc_capture_irq_handler(fimc, 1);
Sylwester Nawrockie1d72f42011-06-10 15:36:58 -03001510 fimc_deactivate_capture(fimc);
1511 spin_unlock_irqrestore(&fimc->slock, irq_flags);
1512 }
1513 spin_unlock_irqrestore(&fmd->slock, flags);
1514}
1515
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001516static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd,
1517 struct v4l2_subdev_fh *fh,
1518 struct v4l2_subdev_mbus_code_enum *code)
1519{
1520 struct fimc_fmt *fmt;
1521
1522 fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index);
1523 if (!fmt)
1524 return -EINVAL;
1525 code->code = fmt->mbus_code;
1526 return 0;
1527}
1528
1529static int fimc_subdev_get_fmt(struct v4l2_subdev *sd,
1530 struct v4l2_subdev_fh *fh,
1531 struct v4l2_subdev_format *fmt)
1532{
1533 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1534 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1535 struct v4l2_mbus_framefmt *mf;
1536 struct fimc_frame *ff;
1537
1538 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1539 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1540 fmt->format = *mf;
1541 return 0;
1542 }
1543 mf = &fmt->format;
1544 mf->colorspace = V4L2_COLORSPACE_JPEG;
1545 ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame;
1546
1547 mutex_lock(&fimc->lock);
1548 /* The pixel code is same on both input and output pad */
1549 if (!WARN_ON(ctx->s_frame.fmt == NULL))
1550 mf->code = ctx->s_frame.fmt->mbus_code;
1551 mf->width = ff->f_width;
1552 mf->height = ff->f_height;
1553 mutex_unlock(&fimc->lock);
1554
1555 return 0;
1556}
1557
1558static int fimc_subdev_set_fmt(struct v4l2_subdev *sd,
1559 struct v4l2_subdev_fh *fh,
1560 struct v4l2_subdev_format *fmt)
1561{
1562 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1563 struct v4l2_mbus_framefmt *mf = &fmt->format;
1564 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
1565 struct fimc_frame *ff;
1566 struct fimc_fmt *ffmt;
1567
1568 dbg("pad%d: code: 0x%x, %dx%d",
1569 fmt->pad, mf->code, mf->width, mf->height);
1570
1571 if (fmt->pad == FIMC_SD_PAD_SOURCE &&
1572 vb2_is_busy(&fimc->vid_cap.vbq))
1573 return -EBUSY;
1574
1575 mutex_lock(&fimc->lock);
1576 ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height,
1577 &mf->code, NULL, fmt->pad);
1578 mutex_unlock(&fimc->lock);
1579 mf->colorspace = V4L2_COLORSPACE_JPEG;
1580
1581 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1582 mf = v4l2_subdev_get_try_format(fh, fmt->pad);
1583 *mf = fmt->format;
1584 return 0;
1585 }
Sylwester Nawrocki66125452012-12-05 13:52:00 -03001586 /* There must be a bug in the driver if this happens */
1587 if (WARN_ON(ffmt == NULL))
1588 return -EINVAL;
1589
Sylwester Nawrockidafb9c72011-12-01 14:02:24 -03001590 /* Update RGB Alpha control state and value range */
1591 fimc_alpha_ctrl_update(ctx);
1592
Sylwester Nawrocki14783d22012-09-24 11:08:45 -03001593 fimc_capture_mark_jpeg_xfer(ctx, ffmt->color);
Sylwester Nawrockiee7160e52011-08-26 14:57:06 -03001594
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001595 ff = fmt->pad == FIMC_SD_PAD_SINK ?
1596 &ctx->s_frame : &ctx->d_frame;
1597
1598 mutex_lock(&fimc->lock);
1599 set_frame_bounds(ff, mf->width, mf->height);
Sylwester Nawrocki393a23f2011-12-08 06:56:22 -03001600 fimc->vid_cap.mf = *mf;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001601 ff->fmt = ffmt;
1602
1603 /* Reset the crop rectangle if required. */
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001604 if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_COMPOSE)))
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001605 set_frame_crop(ff, 0, 0, mf->width, mf->height);
1606
1607 if (fmt->pad == FIMC_SD_PAD_SINK)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001608 ctx->state &= ~FIMC_COMPOSE;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001609 mutex_unlock(&fimc->lock);
1610 return 0;
1611}
1612
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001613static int fimc_subdev_get_selection(struct v4l2_subdev *sd,
1614 struct v4l2_subdev_fh *fh,
1615 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001616{
1617 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1618 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001619 struct fimc_frame *f = &ctx->s_frame;
1620 struct v4l2_rect *r = &sel->r;
1621 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001622
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001623 if (sel->pad != FIMC_SD_PAD_SINK)
1624 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001625
1626 mutex_lock(&fimc->lock);
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001627
1628 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001629 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001630 f = &ctx->d_frame;
Sakari Ailus5689b282012-05-18 09:31:18 -03001631 case V4L2_SEL_TGT_CROP_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001632 r->width = f->o_width;
1633 r->height = f->o_height;
1634 r->left = 0;
1635 r->top = 0;
1636 mutex_unlock(&fimc->lock);
1637 return 0;
1638
Sakari Ailus5689b282012-05-18 09:31:18 -03001639 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001640 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1641 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001642 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001643 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1644 f = &ctx->d_frame;
1645 break;
1646 default:
1647 mutex_unlock(&fimc->lock);
1648 return -EINVAL;
1649 }
1650
1651 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1652 sel->r = *try_sel;
1653 } else {
1654 r->left = f->offs_h;
1655 r->top = f->offs_v;
1656 r->width = f->width;
1657 r->height = f->height;
1658 }
1659
1660 dbg("target %#x: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d",
1661 sel->pad, r->left, r->top, r->width, r->height,
1662 f->f_width, f->f_height);
1663
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001664 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001665 return 0;
1666}
1667
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001668static int fimc_subdev_set_selection(struct v4l2_subdev *sd,
1669 struct v4l2_subdev_fh *fh,
1670 struct v4l2_subdev_selection *sel)
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001671{
1672 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1673 struct fimc_ctx *ctx = fimc->vid_cap.ctx;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001674 struct fimc_frame *f = &ctx->s_frame;
1675 struct v4l2_rect *r = &sel->r;
1676 struct v4l2_rect *try_sel;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001677 unsigned long flags;
1678
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001679 if (sel->pad != FIMC_SD_PAD_SINK)
1680 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001681
1682 mutex_lock(&fimc->lock);
Sylwester Nawrockic1334822012-05-20 11:17:12 -03001683 fimc_capture_try_selection(ctx, r, V4L2_SEL_TGT_CROP);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001684
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001685 switch (sel->target) {
Sakari Ailus5689b282012-05-18 09:31:18 -03001686 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001687 f = &ctx->d_frame;
Sakari Ailus5689b282012-05-18 09:31:18 -03001688 case V4L2_SEL_TGT_CROP_BOUNDS:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001689 r->width = f->o_width;
1690 r->height = f->o_height;
1691 r->left = 0;
1692 r->top = 0;
Sylwester Nawrocki5be4fe62012-04-21 18:46:30 -03001693 mutex_unlock(&fimc->lock);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001694 return 0;
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001695
Sakari Ailus5689b282012-05-18 09:31:18 -03001696 case V4L2_SEL_TGT_CROP:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001697 try_sel = v4l2_subdev_get_try_crop(fh, sel->pad);
1698 break;
Sakari Ailus5689b282012-05-18 09:31:18 -03001699 case V4L2_SEL_TGT_COMPOSE:
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001700 try_sel = v4l2_subdev_get_try_compose(fh, sel->pad);
1701 f = &ctx->d_frame;
1702 break;
1703 default:
1704 mutex_unlock(&fimc->lock);
1705 return -EINVAL;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001706 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001707
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001708 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1709 *try_sel = sel->r;
1710 } else {
1711 spin_lock_irqsave(&fimc->slock, flags);
1712 set_frame_crop(f, r->left, r->top, r->width, r->height);
1713 set_bit(ST_CAPT_APPLY_CFG, &fimc->state);
1714 spin_unlock_irqrestore(&fimc->slock, flags);
Sakari Ailus5689b282012-05-18 09:31:18 -03001715 if (sel->target == V4L2_SEL_TGT_COMPOSE)
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001716 ctx->state |= FIMC_COMPOSE;
1717 }
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001718
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001719 dbg("target %#x: (%d,%d)/%dx%d", sel->target, r->left, r->top,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001720 r->width, r->height);
1721
1722 mutex_unlock(&fimc->lock);
1723 return 0;
1724}
1725
1726static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = {
1727 .enum_mbus_code = fimc_subdev_enum_mbus_code,
Sylwester Nawrockifed07f82012-04-29 17:46:03 -03001728 .get_selection = fimc_subdev_get_selection,
1729 .set_selection = fimc_subdev_set_selection,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001730 .get_fmt = fimc_subdev_get_fmt,
1731 .set_fmt = fimc_subdev_set_fmt,
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001732};
1733
1734static struct v4l2_subdev_ops fimc_subdev_ops = {
1735 .pad = &fimc_subdev_pad_ops,
1736};
1737
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001738/* Set default format at the sensor and host interface */
1739static int fimc_capture_set_default_format(struct fimc_dev *fimc)
1740{
1741 struct v4l2_format fmt = {
1742 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
1743 .fmt.pix_mp = {
1744 .width = 640,
1745 .height = 480,
1746 .pixelformat = V4L2_PIX_FMT_YUYV,
1747 .field = V4L2_FIELD_NONE,
1748 .colorspace = V4L2_COLORSPACE_JPEG,
1749 },
1750 };
1751
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001752 return __fimc_capture_set_format(fimc, &fmt);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001753}
1754
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001755/* fimc->lock must be already initialized */
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001756static int fimc_register_capture_device(struct fimc_dev *fimc,
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001757 struct v4l2_device *v4l2_dev)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001758{
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001759 struct video_device *vfd = &fimc->vid_cap.vfd;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001760 struct fimc_vid_cap *vid_cap;
1761 struct fimc_ctx *ctx;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001762 struct vb2_queue *q;
Sylwester Nawrocki30c99392011-06-10 15:36:48 -03001763 int ret = -ENOMEM;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001764
Sachin Kamat26ee7f42012-08-17 03:28:27 -03001765 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001766 if (!ctx)
1767 return -ENOMEM;
1768
1769 ctx->fimc_dev = fimc;
Sylwester Nawrocki3d112d92012-04-26 06:26:29 -03001770 ctx->in_path = FIMC_IO_CAMERA;
1771 ctx->out_path = FIMC_IO_DMA;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001772 ctx->state = FIMC_CTX_CAP;
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001773 ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001774 ctx->d_frame.fmt = ctx->s_frame.fmt;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001775
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001776 memset(vfd, 0, sizeof(*vfd));
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001777 snprintf(vfd->name, sizeof(vfd->name), "fimc.%d.capture", fimc->id);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001778
1779 vfd->fops = &fimc_capture_fops;
1780 vfd->ioctl_ops = &fimc_capture_ioctl_ops;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001781 vfd->v4l2_dev = v4l2_dev;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001782 vfd->minor = -1;
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001783 vfd->release = video_device_release_empty;
Sylwester Nawrocki8293ebf2010-12-01 10:25:18 -03001784 vfd->lock = &fimc->lock;
Sylwester Nawrockic2d430a2012-06-27 09:56:17 -03001785
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001786 video_set_drvdata(vfd, fimc);
1787
1788 vid_cap = &fimc->vid_cap;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001789 vid_cap->active_buf_cnt = 0;
1790 vid_cap->reqbufs_count = 0;
1791 vid_cap->refcnt = 0;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001792
1793 INIT_LIST_HEAD(&vid_cap->pending_buf_q);
1794 INIT_LIST_HEAD(&vid_cap->active_buf_q);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001795 vid_cap->ctx = ctx;
1796
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001797 q = &fimc->vid_cap.vbq;
1798 memset(q, 0, sizeof(*q));
Sylwester Nawrockief7af592010-12-08 14:05:08 -03001799 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
Tomasz Stanislawski9bd09fd2012-06-14 10:37:49 -03001800 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Sylwester Nawrocki2dab38e2010-12-01 10:14:59 -03001801 q->drv_priv = fimc->vid_cap.ctx;
1802 q->ops = &fimc_capture_qops;
1803 q->mem_ops = &vb2_dma_contig_memops;
1804 q->buf_struct_size = sizeof(struct fimc_vid_buffer);
1805
Sylwester Nawrocki41fd0872012-10-19 08:24:18 -03001806 ret = vb2_queue_init(q);
1807 if (ret)
1808 goto err_ent;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001809
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001810 vid_cap->vd_pad.flags = MEDIA_PAD_FL_SINK;
1811 ret = media_entity_init(&vfd->entity, 1, &vid_cap->vd_pad, 0);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001812 if (ret)
1813 goto err_ent;
Sylwester Nawrocki740ad922012-12-06 10:26:19 -03001814 /*
1815 * For proper order of acquiring/releasing the video
1816 * and the graph mutex.
1817 */
1818 v4l2_disable_ioctl_locking(vfd, VIDIOC_TRY_FMT);
1819 v4l2_disable_ioctl_locking(vfd, VIDIOC_S_FMT);
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001820
1821 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001822 if (ret)
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001823 goto err_vd;
1824
1825 v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n",
1826 vfd->name, video_device_node_name(vfd));
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001827
Sylwester Nawrocki9448ab72012-04-02 06:41:22 -03001828 vfd->ctrl_handler = &ctx->ctrls.handler;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001829 return 0;
1830
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001831err_vd:
Sylwester Nawrocki237e0262011-08-24 20:35:30 -03001832 media_entity_cleanup(&vfd->entity);
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001833err_ent:
Sylwester Nawrockicfd77312011-06-02 06:18:34 -03001834 kfree(ctx);
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001835 return ret;
1836}
1837
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001838static int fimc_capture_subdev_registered(struct v4l2_subdev *sd)
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001839{
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001840 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1841 int ret;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001842
Sylwester Nawrockibbc52962012-07-26 07:59:11 -03001843 if (fimc == NULL)
1844 return -ENXIO;
1845
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001846 ret = fimc_register_m2m_device(fimc, sd->v4l2_dev);
1847 if (ret)
1848 return ret;
1849
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001850 fimc->pipeline_ops = v4l2_get_subdev_hostdata(sd);
1851
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001852 ret = fimc_register_capture_device(fimc, sd->v4l2_dev);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001853 if (ret) {
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001854 fimc_unregister_m2m_device(fimc);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001855 fimc->pipeline_ops = NULL;
1856 }
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001857
1858 return ret;
1859}
1860
1861static void fimc_capture_subdev_unregistered(struct v4l2_subdev *sd)
1862{
1863 struct fimc_dev *fimc = v4l2_get_subdevdata(sd);
1864
1865 if (fimc == NULL)
1866 return;
1867
1868 fimc_unregister_m2m_device(fimc);
1869
Sylwester Nawrocki31d34d92012-07-26 07:15:42 -03001870 if (video_is_registered(&fimc->vid_cap.vfd)) {
1871 video_unregister_device(&fimc->vid_cap.vfd);
1872 media_entity_cleanup(&fimc->vid_cap.vfd.entity);
Sylwester Nawrocki97d66c42012-11-22 06:17:19 -03001873 fimc->pipeline_ops = NULL;
Sylwester Nawrocki574e1712011-07-26 18:08:21 -03001874 }
1875 kfree(fimc->vid_cap.ctx);
Sylwester Nawrocki96a85742011-08-26 15:40:36 -03001876 fimc->vid_cap.ctx = NULL;
Sylwester Nawrocki5f3cc442010-10-07 10:06:16 -03001877}
Sylwester Nawrocki693f5c42012-04-20 18:57:25 -03001878
1879static const struct v4l2_subdev_internal_ops fimc_capture_sd_internal_ops = {
1880 .registered = fimc_capture_subdev_registered,
1881 .unregistered = fimc_capture_subdev_unregistered,
1882};
1883
1884int fimc_initialize_capture_subdev(struct fimc_dev *fimc)
1885{
1886 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1887 int ret;
1888
1889 v4l2_subdev_init(sd, &fimc_subdev_ops);
1890 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
1891 snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id);
1892
1893 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
1894 fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
1895 ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM,
1896 fimc->vid_cap.sd_pads, 0);
1897 if (ret)
1898 return ret;
1899
1900 sd->entity.ops = &fimc_sd_media_ops;
1901 sd->internal_ops = &fimc_capture_sd_internal_ops;
1902 v4l2_set_subdevdata(sd, fimc);
1903 return 0;
1904}
1905
1906void fimc_unregister_capture_subdev(struct fimc_dev *fimc)
1907{
1908 struct v4l2_subdev *sd = &fimc->vid_cap.subdev;
1909
1910 v4l2_device_unregister_subdev(sd);
1911 media_entity_cleanup(&sd->entity);
1912 v4l2_set_subdevdata(sd, NULL);
1913}