Commit | Line | Data |
---|---|---|
5f3cc447 | 1 | /* |
3a3f9449 | 2 | * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver |
5f3cc447 | 3 | * |
3a3f9449 | 4 | * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd. |
5f3cc447 SN |
5 | * Author: Sylwester Nawrocki, <s.nawrocki@samsung.com> |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/kernel.h> | |
5f3cc447 SN |
14 | #include <linux/types.h> |
15 | #include <linux/errno.h> | |
16 | #include <linux/bug.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/device.h> | |
e9e21083 | 19 | #include <linux/pm_runtime.h> |
5f3cc447 SN |
20 | #include <linux/list.h> |
21 | #include <linux/slab.h> | |
5f3cc447 SN |
22 | |
23 | #include <linux/videodev2.h> | |
24 | #include <media/v4l2-device.h> | |
25 | #include <media/v4l2-ioctl.h> | |
26 | #include <media/v4l2-mem2mem.h> | |
2dab38e2 SN |
27 | #include <media/videobuf2-core.h> |
28 | #include <media/videobuf2-dma-contig.h> | |
5f3cc447 | 29 | |
131b6c61 | 30 | #include "fimc-mdevice.h" |
5f3cc447 SN |
31 | #include "fimc-core.h" |
32 | ||
9e803a04 SN |
33 | static int fimc_init_capture(struct fimc_dev *fimc) |
34 | { | |
35 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
36 | struct fimc_sensor_info *sensor; | |
37 | unsigned long flags; | |
38 | int ret = 0; | |
39 | ||
40 | if (fimc->pipeline.sensor == NULL || ctx == NULL) | |
41 | return -ENXIO; | |
42 | if (ctx->s_frame.fmt == NULL) | |
43 | return -EINVAL; | |
44 | ||
45 | sensor = v4l2_get_subdev_hostdata(fimc->pipeline.sensor); | |
46 | ||
47 | spin_lock_irqsave(&fimc->slock, flags); | |
48 | fimc_prepare_dma_offset(ctx, &ctx->d_frame); | |
49 | fimc_set_yuv_order(ctx); | |
50 | ||
51 | fimc_hw_set_camera_polarity(fimc, sensor->pdata); | |
52 | fimc_hw_set_camera_type(fimc, sensor->pdata); | |
53 | fimc_hw_set_camera_source(fimc, sensor->pdata); | |
54 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); | |
55 | ||
56 | ret = fimc_set_scaler_info(ctx); | |
57 | if (!ret) { | |
58 | fimc_hw_set_input_path(ctx); | |
59 | fimc_hw_set_prescaler(ctx); | |
60 | fimc_hw_set_mainscaler(ctx); | |
61 | fimc_hw_set_target_format(ctx); | |
62 | fimc_hw_set_rotation(ctx); | |
ee7160e5 | 63 | fimc_hw_set_effect(ctx, false); |
9e803a04 SN |
64 | fimc_hw_set_output_path(ctx); |
65 | fimc_hw_set_out_dma(ctx); | |
237e0265 | 66 | clear_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
9e803a04 SN |
67 | } |
68 | spin_unlock_irqrestore(&fimc->slock, flags); | |
69 | return ret; | |
70 | } | |
71 | ||
3e4748d8 | 72 | static int fimc_capture_state_cleanup(struct fimc_dev *fimc, bool suspend) |
5f3cc447 | 73 | { |
bd323e28 | 74 | struct fimc_vid_cap *cap = &fimc->vid_cap; |
2dab38e2 | 75 | struct fimc_vid_buffer *buf; |
bd323e28 | 76 | unsigned long flags; |
3e4748d8 | 77 | bool streaming; |
5f3cc447 SN |
78 | |
79 | spin_lock_irqsave(&fimc->slock, flags); | |
3e4748d8 | 80 | streaming = fimc->state & (1 << ST_CAPT_ISP_STREAM); |
5f3cc447 | 81 | |
3e4748d8 SN |
82 | fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_SHUT | |
83 | 1 << ST_CAPT_STREAM | 1 << ST_CAPT_ISP_STREAM); | |
84 | if (!suspend) | |
85 | fimc->state &= ~(1 << ST_CAPT_PEND | 1 << ST_CAPT_SUSPENDED); | |
2dab38e2 | 86 | |
3e4748d8 SN |
87 | /* Release unused buffers */ |
88 | while (!suspend && !list_empty(&cap->pending_buf_q)) { | |
0295202c | 89 | buf = fimc_pending_queue_pop(cap); |
2dab38e2 SN |
90 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
91 | } | |
3e4748d8 | 92 | /* If suspending put unused buffers onto pending queue */ |
2dab38e2 | 93 | while (!list_empty(&cap->active_buf_q)) { |
0295202c | 94 | buf = fimc_active_queue_pop(cap); |
3e4748d8 SN |
95 | if (suspend) |
96 | fimc_pending_queue_add(cap, buf); | |
97 | else | |
98 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | |
2dab38e2 | 99 | } |
3e4748d8 | 100 | set_bit(ST_CAPT_SUSPENDED, &fimc->state); |
2c1bb62e SN |
101 | |
102 | fimc_hw_reset(fimc); | |
103 | cap->buf_index = 0; | |
104 | ||
5f3cc447 | 105 | spin_unlock_irqrestore(&fimc->slock, flags); |
4db5e27e | 106 | |
3e4748d8 | 107 | if (streaming) |
4db5e27e SN |
108 | return fimc_pipeline_s_stream(fimc, 0); |
109 | else | |
110 | return 0; | |
bd323e28 MS |
111 | } |
112 | ||
3e4748d8 | 113 | static int fimc_stop_capture(struct fimc_dev *fimc, bool suspend) |
bd323e28 | 114 | { |
bd323e28 MS |
115 | unsigned long flags; |
116 | ||
117 | if (!fimc_capture_active(fimc)) | |
118 | return 0; | |
119 | ||
120 | spin_lock_irqsave(&fimc->slock, flags); | |
121 | set_bit(ST_CAPT_SHUT, &fimc->state); | |
122 | fimc_deactivate_capture(fimc); | |
123 | spin_unlock_irqrestore(&fimc->slock, flags); | |
124 | ||
125 | wait_event_timeout(fimc->irq_queue, | |
126 | !test_bit(ST_CAPT_SHUT, &fimc->state), | |
3e4748d8 | 127 | (2*HZ/10)); /* 200 ms */ |
5f3cc447 | 128 | |
3e4748d8 | 129 | return fimc_capture_state_cleanup(fimc, suspend); |
5f3cc447 SN |
130 | } |
131 | ||
237e0265 SN |
132 | /** |
133 | * fimc_capture_config_update - apply the camera interface configuration | |
134 | * | |
135 | * To be called from within the interrupt handler with fimc.slock | |
136 | * spinlock held. It updates the camera pixel crop, rotation and | |
137 | * image flip in H/W. | |
138 | */ | |
139 | int fimc_capture_config_update(struct fimc_ctx *ctx) | |
140 | { | |
141 | struct fimc_dev *fimc = ctx->fimc_dev; | |
142 | int ret; | |
143 | ||
2c1bb62e | 144 | if (!test_bit(ST_CAPT_APPLY_CFG, &fimc->state)) |
237e0265 SN |
145 | return 0; |
146 | ||
147 | spin_lock(&ctx->slock); | |
148 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); | |
149 | ret = fimc_set_scaler_info(ctx); | |
150 | if (ret == 0) { | |
151 | fimc_hw_set_prescaler(ctx); | |
152 | fimc_hw_set_mainscaler(ctx); | |
153 | fimc_hw_set_target_format(ctx); | |
154 | fimc_hw_set_rotation(ctx); | |
155 | fimc_prepare_dma_offset(ctx, &ctx->d_frame); | |
156 | fimc_hw_set_out_dma(ctx); | |
2c1bb62e | 157 | clear_bit(ST_CAPT_APPLY_CFG, &fimc->state); |
237e0265 SN |
158 | } |
159 | spin_unlock(&ctx->slock); | |
160 | return ret; | |
161 | } | |
bd323e28 MS |
162 | |
163 | static int start_streaming(struct vb2_queue *q, unsigned int count) | |
2dab38e2 SN |
164 | { |
165 | struct fimc_ctx *ctx = q->drv_priv; | |
166 | struct fimc_dev *fimc = ctx->fimc_dev; | |
9e803a04 | 167 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
bd323e28 | 168 | int min_bufs; |
2dab38e2 SN |
169 | int ret; |
170 | ||
9e803a04 | 171 | vid_cap->frame_count = 0; |
8ec737ff | 172 | |
9e803a04 | 173 | ret = fimc_init_capture(fimc); |
2dab38e2 | 174 | if (ret) |
bd323e28 | 175 | goto error; |
2dab38e2 | 176 | |
2dab38e2 SN |
177 | set_bit(ST_CAPT_PEND, &fimc->state); |
178 | ||
bd323e28 MS |
179 | min_bufs = fimc->vid_cap.reqbufs_count > 1 ? 2 : 1; |
180 | ||
4db5e27e SN |
181 | if (vid_cap->active_buf_cnt >= min_bufs && |
182 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) { | |
bd323e28 MS |
183 | fimc_activate_capture(ctx); |
184 | ||
4db5e27e SN |
185 | if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state)) |
186 | fimc_pipeline_s_stream(fimc, 1); | |
187 | } | |
188 | ||
2dab38e2 | 189 | return 0; |
bd323e28 | 190 | error: |
3e4748d8 | 191 | fimc_capture_state_cleanup(fimc, false); |
bd323e28 | 192 | return ret; |
2dab38e2 SN |
193 | } |
194 | ||
195 | static int stop_streaming(struct vb2_queue *q) | |
196 | { | |
197 | struct fimc_ctx *ctx = q->drv_priv; | |
198 | struct fimc_dev *fimc = ctx->fimc_dev; | |
2dab38e2 | 199 | |
4ecbf5d1 | 200 | if (!fimc_capture_active(fimc)) |
2dab38e2 | 201 | return -EINVAL; |
2dab38e2 | 202 | |
3e4748d8 | 203 | return fimc_stop_capture(fimc, false); |
2dab38e2 SN |
204 | } |
205 | ||
e9e21083 SN |
206 | int fimc_capture_suspend(struct fimc_dev *fimc) |
207 | { | |
3e4748d8 SN |
208 | bool suspend = fimc_capture_busy(fimc); |
209 | ||
210 | int ret = fimc_stop_capture(fimc, suspend); | |
211 | if (ret) | |
212 | return ret; | |
213 | return fimc_pipeline_shutdown(fimc); | |
e9e21083 SN |
214 | } |
215 | ||
3e4748d8 SN |
216 | static void buffer_queue(struct vb2_buffer *vb); |
217 | ||
e9e21083 SN |
218 | int fimc_capture_resume(struct fimc_dev *fimc) |
219 | { | |
3e4748d8 SN |
220 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
221 | struct fimc_vid_buffer *buf; | |
222 | int i; | |
223 | ||
224 | if (!test_and_clear_bit(ST_CAPT_SUSPENDED, &fimc->state)) | |
225 | return 0; | |
226 | ||
227 | INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q); | |
228 | vid_cap->buf_index = 0; | |
229 | fimc_pipeline_initialize(fimc, &fimc->vid_cap.vfd->entity, | |
230 | false); | |
231 | fimc_init_capture(fimc); | |
232 | ||
233 | clear_bit(ST_CAPT_SUSPENDED, &fimc->state); | |
234 | ||
235 | for (i = 0; i < vid_cap->reqbufs_count; i++) { | |
236 | if (list_empty(&vid_cap->pending_buf_q)) | |
237 | break; | |
238 | buf = fimc_pending_queue_pop(vid_cap); | |
239 | buffer_queue(&buf->vb); | |
240 | } | |
e9e21083 | 241 | return 0; |
3e4748d8 | 242 | |
e9e21083 SN |
243 | } |
244 | ||
ef7af59b | 245 | static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane) |
2dab38e2 | 246 | { |
ef7af59b | 247 | if (!fr || plane >= fr->fmt->memplanes) |
2dab38e2 | 248 | return 0; |
ef7af59b | 249 | return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8; |
2dab38e2 SN |
250 | } |
251 | ||
fc714e70 GL |
252 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt, |
253 | unsigned int *num_buffers, unsigned int *num_planes, | |
254 | unsigned int sizes[], void *allocators[]) | |
2dab38e2 SN |
255 | { |
256 | struct fimc_ctx *ctx = vq->drv_priv; | |
ef7af59b SN |
257 | struct fimc_fmt *fmt = ctx->d_frame.fmt; |
258 | int i; | |
2dab38e2 SN |
259 | |
260 | if (!fmt) | |
261 | return -EINVAL; | |
262 | ||
ef7af59b | 263 | *num_planes = fmt->memplanes; |
2dab38e2 | 264 | |
ef7af59b SN |
265 | for (i = 0; i < fmt->memplanes; i++) { |
266 | sizes[i] = get_plane_size(&ctx->d_frame, i); | |
ef7af59b SN |
267 | allocators[i] = ctx->fimc_dev->alloc_ctx; |
268 | } | |
2dab38e2 | 269 | |
ef7af59b | 270 | return 0; |
2dab38e2 SN |
271 | } |
272 | ||
2dab38e2 SN |
273 | static int buffer_prepare(struct vb2_buffer *vb) |
274 | { | |
275 | struct vb2_queue *vq = vb->vb2_queue; | |
276 | struct fimc_ctx *ctx = vq->drv_priv; | |
2dab38e2 SN |
277 | int i; |
278 | ||
4db5e27e | 279 | if (ctx->d_frame.fmt == NULL) |
ef7af59b | 280 | return -EINVAL; |
2dab38e2 | 281 | |
ef7af59b | 282 | for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) { |
4db5e27e | 283 | unsigned long size = ctx->d_frame.payload[i]; |
2dab38e2 SN |
284 | |
285 | if (vb2_plane_size(vb, i) < size) { | |
30c9939d SN |
286 | v4l2_err(ctx->fimc_dev->vid_cap.vfd, |
287 | "User buffer too small (%ld < %ld)\n", | |
2dab38e2 SN |
288 | vb2_plane_size(vb, i), size); |
289 | return -EINVAL; | |
290 | } | |
2dab38e2 SN |
291 | vb2_set_plane_payload(vb, i, size); |
292 | } | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static void buffer_queue(struct vb2_buffer *vb) | |
298 | { | |
2dab38e2 SN |
299 | struct fimc_vid_buffer *buf |
300 | = container_of(vb, struct fimc_vid_buffer, vb); | |
4db5e27e SN |
301 | struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
302 | struct fimc_dev *fimc = ctx->fimc_dev; | |
2dab38e2 SN |
303 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; |
304 | unsigned long flags; | |
8ec737ff | 305 | int min_bufs; |
2dab38e2 SN |
306 | |
307 | spin_lock_irqsave(&fimc->slock, flags); | |
8ec737ff SK |
308 | fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr); |
309 | ||
3e4748d8 SN |
310 | if (!test_bit(ST_CAPT_SUSPENDED, &fimc->state) && |
311 | !test_bit(ST_CAPT_STREAM, &fimc->state) && | |
312 | vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) { | |
8ec737ff SK |
313 | /* Setup the buffer directly for processing. */ |
314 | int buf_id = (vid_cap->reqbufs_count == 1) ? -1 : | |
315 | vid_cap->buf_index; | |
2dab38e2 | 316 | |
8ec737ff SK |
317 | fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id); |
318 | buf->index = vid_cap->buf_index; | |
0295202c | 319 | fimc_active_queue_add(vid_cap, buf); |
2dab38e2 | 320 | |
8ec737ff SK |
321 | if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS) |
322 | vid_cap->buf_index = 0; | |
323 | } else { | |
324 | fimc_pending_queue_add(vid_cap, buf); | |
2dab38e2 | 325 | } |
8ec737ff SK |
326 | |
327 | min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1; | |
328 | ||
4db5e27e | 329 | |
bd323e28 MS |
330 | if (vb2_is_streaming(&vid_cap->vbq) && |
331 | vid_cap->active_buf_cnt >= min_bufs && | |
4db5e27e | 332 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) { |
8ec737ff | 333 | fimc_activate_capture(ctx); |
4db5e27e | 334 | spin_unlock_irqrestore(&fimc->slock, flags); |
8ec737ff | 335 | |
4db5e27e SN |
336 | if (!test_and_set_bit(ST_CAPT_ISP_STREAM, &fimc->state)) |
337 | fimc_pipeline_s_stream(fimc, 1); | |
338 | return; | |
339 | } | |
2dab38e2 SN |
340 | spin_unlock_irqrestore(&fimc->slock, flags); |
341 | } | |
342 | ||
343 | static void fimc_lock(struct vb2_queue *vq) | |
344 | { | |
345 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); | |
346 | mutex_lock(&ctx->fimc_dev->lock); | |
347 | } | |
348 | ||
349 | static void fimc_unlock(struct vb2_queue *vq) | |
350 | { | |
351 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); | |
352 | mutex_unlock(&ctx->fimc_dev->lock); | |
353 | } | |
354 | ||
355 | static struct vb2_ops fimc_capture_qops = { | |
356 | .queue_setup = queue_setup, | |
357 | .buf_prepare = buffer_prepare, | |
358 | .buf_queue = buffer_queue, | |
2dab38e2 SN |
359 | .wait_prepare = fimc_unlock, |
360 | .wait_finish = fimc_lock, | |
361 | .start_streaming = start_streaming, | |
362 | .stop_streaming = stop_streaming, | |
363 | }; | |
364 | ||
131b6c61 SN |
365 | /** |
366 | * fimc_capture_ctrls_create - initialize the control handler | |
367 | * Initialize the capture video node control handler and fill it | |
368 | * with the FIMC controls. Inherit any sensor's controls if the | |
369 | * 'user_subdev_api' flag is false (default behaviour). | |
370 | * This function need to be called with the graph mutex held. | |
371 | */ | |
372 | int fimc_capture_ctrls_create(struct fimc_dev *fimc) | |
373 | { | |
374 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; | |
375 | int ret; | |
376 | ||
377 | if (WARN_ON(vid_cap->ctx == NULL)) | |
378 | return -ENXIO; | |
379 | if (vid_cap->ctx->ctrls_rdy) | |
380 | return 0; | |
381 | ||
382 | ret = fimc_ctrls_create(vid_cap->ctx); | |
383 | if (ret || vid_cap->user_subdev_api) | |
384 | return ret; | |
385 | ||
386 | return v4l2_ctrl_add_handler(&vid_cap->ctx->ctrl_handler, | |
387 | fimc->pipeline.sensor->ctrl_handler); | |
388 | } | |
389 | ||
237e0265 SN |
390 | static int fimc_capture_set_default_format(struct fimc_dev *fimc); |
391 | ||
5f3cc447 SN |
392 | static int fimc_capture_open(struct file *file) |
393 | { | |
394 | struct fimc_dev *fimc = video_drvdata(file); | |
e578588e SN |
395 | int ret = v4l2_fh_open(file); |
396 | ||
397 | if (ret) | |
398 | return ret; | |
5f3cc447 SN |
399 | |
400 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); | |
401 | ||
402 | /* Return if the corresponding video mem2mem node is already opened. */ | |
403 | if (fimc_m2m_active(fimc)) | |
404 | return -EBUSY; | |
405 | ||
3e4748d8 | 406 | set_bit(ST_CAPT_BUSY, &fimc->state); |
4db5e27e SN |
407 | pm_runtime_get_sync(&fimc->pdev->dev); |
408 | ||
409 | if (++fimc->vid_cap.refcnt == 1) { | |
410 | ret = fimc_pipeline_initialize(fimc, | |
411 | &fimc->vid_cap.vfd->entity, true); | |
412 | if (ret < 0) { | |
413 | dev_err(&fimc->pdev->dev, | |
414 | "Video pipeline initialization failed\n"); | |
415 | pm_runtime_put_sync(&fimc->pdev->dev); | |
416 | fimc->vid_cap.refcnt--; | |
417 | v4l2_fh_release(file); | |
3e4748d8 | 418 | clear_bit(ST_CAPT_BUSY, &fimc->state); |
4db5e27e SN |
419 | return ret; |
420 | } | |
131b6c61 | 421 | ret = fimc_capture_ctrls_create(fimc); |
237e0265 SN |
422 | |
423 | if (!ret && !fimc->vid_cap.user_subdev_api) | |
424 | ret = fimc_capture_set_default_format(fimc); | |
4db5e27e | 425 | } |
131b6c61 | 426 | return ret; |
5f3cc447 SN |
427 | } |
428 | ||
429 | static int fimc_capture_close(struct file *file) | |
430 | { | |
431 | struct fimc_dev *fimc = video_drvdata(file); | |
432 | ||
5f3cc447 SN |
433 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); |
434 | ||
435 | if (--fimc->vid_cap.refcnt == 0) { | |
3e4748d8 SN |
436 | clear_bit(ST_CAPT_BUSY, &fimc->state); |
437 | fimc_stop_capture(fimc, false); | |
4db5e27e | 438 | fimc_pipeline_shutdown(fimc); |
3e4748d8 | 439 | clear_bit(ST_CAPT_SUSPENDED, &fimc->state); |
5f3cc447 SN |
440 | } |
441 | ||
e9e21083 SN |
442 | pm_runtime_put(&fimc->pdev->dev); |
443 | ||
3e4748d8 SN |
444 | if (fimc->vid_cap.refcnt == 0) { |
445 | vb2_queue_release(&fimc->vid_cap.vbq); | |
446 | fimc_ctrls_delete(fimc->vid_cap.ctx); | |
447 | } | |
e578588e | 448 | return v4l2_fh_release(file); |
5f3cc447 SN |
449 | } |
450 | ||
451 | static unsigned int fimc_capture_poll(struct file *file, | |
452 | struct poll_table_struct *wait) | |
453 | { | |
e578588e | 454 | struct fimc_dev *fimc = video_drvdata(file); |
5f3cc447 | 455 | |
8293ebfc | 456 | return vb2_poll(&fimc->vid_cap.vbq, file, wait); |
5f3cc447 SN |
457 | } |
458 | ||
459 | static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma) | |
460 | { | |
e578588e | 461 | struct fimc_dev *fimc = video_drvdata(file); |
5f3cc447 | 462 | |
8293ebfc | 463 | return vb2_mmap(&fimc->vid_cap.vbq, vma); |
5f3cc447 SN |
464 | } |
465 | ||
5f3cc447 SN |
466 | static const struct v4l2_file_operations fimc_capture_fops = { |
467 | .owner = THIS_MODULE, | |
468 | .open = fimc_capture_open, | |
469 | .release = fimc_capture_close, | |
470 | .poll = fimc_capture_poll, | |
471 | .unlocked_ioctl = video_ioctl2, | |
472 | .mmap = fimc_capture_mmap, | |
473 | }; | |
474 | ||
237e0265 SN |
475 | /* |
476 | * Format and crop negotiation helpers | |
477 | */ | |
478 | ||
479 | static struct fimc_fmt *fimc_capture_try_format(struct fimc_ctx *ctx, | |
480 | u32 *width, u32 *height, | |
481 | u32 *code, u32 *fourcc, int pad) | |
482 | { | |
483 | bool rotation = ctx->rotation == 90 || ctx->rotation == 270; | |
484 | struct fimc_dev *fimc = ctx->fimc_dev; | |
485 | struct samsung_fimc_variant *var = fimc->variant; | |
486 | struct fimc_pix_limit *pl = var->pix_limit; | |
487 | struct fimc_frame *dst = &ctx->d_frame; | |
488 | u32 depth, min_w, max_w, min_h, align_h = 3; | |
489 | u32 mask = FMT_FLAGS_CAM; | |
490 | struct fimc_fmt *ffmt; | |
491 | ||
492 | /* Color conversion from/to JPEG is not supported */ | |
493 | if (code && ctx->s_frame.fmt && pad == FIMC_SD_PAD_SOURCE && | |
494 | fimc_fmt_is_jpeg(ctx->s_frame.fmt->color)) | |
495 | *code = V4L2_MBUS_FMT_JPEG_1X8; | |
496 | ||
497 | if (fourcc && *fourcc != V4L2_PIX_FMT_JPEG && pad != FIMC_SD_PAD_SINK) | |
498 | mask |= FMT_FLAGS_M2M; | |
499 | ||
500 | ffmt = fimc_find_format(fourcc, code, mask, 0); | |
501 | if (WARN_ON(!ffmt)) | |
502 | return NULL; | |
503 | if (code) | |
504 | *code = ffmt->mbus_code; | |
505 | if (fourcc) | |
506 | *fourcc = ffmt->fourcc; | |
507 | ||
508 | if (pad == FIMC_SD_PAD_SINK) { | |
509 | max_w = fimc_fmt_is_jpeg(ffmt->color) ? | |
510 | pl->scaler_dis_w : pl->scaler_en_w; | |
511 | /* Apply the camera input interface pixel constraints */ | |
512 | v4l_bound_align_image(width, max_t(u32, *width, 32), max_w, 4, | |
513 | height, max_t(u32, *height, 32), | |
514 | FIMC_CAMIF_MAX_HEIGHT, | |
515 | fimc_fmt_is_jpeg(ffmt->color) ? 3 : 1, | |
516 | 0); | |
517 | return ffmt; | |
518 | } | |
519 | /* Can't scale or crop in transparent (JPEG) transfer mode */ | |
520 | if (fimc_fmt_is_jpeg(ffmt->color)) { | |
521 | *width = ctx->s_frame.f_width; | |
522 | *height = ctx->s_frame.f_height; | |
523 | return ffmt; | |
524 | } | |
525 | /* Apply the scaler and the output DMA constraints */ | |
526 | max_w = rotation ? pl->out_rot_en_w : pl->out_rot_dis_w; | |
527 | min_w = ctx->state & FIMC_DST_CROP ? dst->width : var->min_out_pixsize; | |
528 | min_h = ctx->state & FIMC_DST_CROP ? dst->height : var->min_out_pixsize; | |
529 | if (fimc->id == 1 && var->pix_hoff) | |
530 | align_h = fimc_fmt_is_rgb(ffmt->color) ? 0 : 1; | |
531 | ||
532 | depth = fimc_get_format_depth(ffmt); | |
533 | v4l_bound_align_image(width, min_w, max_w, | |
534 | ffs(var->min_out_pixsize) - 1, | |
535 | height, min_h, FIMC_CAMIF_MAX_HEIGHT, | |
536 | align_h, | |
537 | 64/(ALIGN(depth, 8))); | |
538 | ||
539 | dbg("pad%d: code: 0x%x, %dx%d. dst fmt: %dx%d", | |
540 | pad, code ? *code : 0, *width, *height, | |
541 | dst->f_width, dst->f_height); | |
542 | ||
543 | return ffmt; | |
544 | } | |
545 | ||
546 | static void fimc_capture_try_crop(struct fimc_ctx *ctx, struct v4l2_rect *r, | |
547 | int pad) | |
548 | { | |
549 | bool rotate = ctx->rotation == 90 || ctx->rotation == 270; | |
550 | struct fimc_dev *fimc = ctx->fimc_dev; | |
551 | struct samsung_fimc_variant *var = fimc->variant; | |
552 | struct fimc_pix_limit *pl = var->pix_limit; | |
553 | struct fimc_frame *sink = &ctx->s_frame; | |
554 | u32 max_w, max_h, min_w = 0, min_h = 0, min_sz; | |
555 | u32 align_sz = 0, align_h = 4; | |
556 | u32 max_sc_h, max_sc_v; | |
557 | ||
558 | /* In JPEG transparent transfer mode cropping is not supported */ | |
559 | if (fimc_fmt_is_jpeg(ctx->d_frame.fmt->color)) { | |
560 | r->width = sink->f_width; | |
561 | r->height = sink->f_height; | |
562 | r->left = r->top = 0; | |
563 | return; | |
564 | } | |
565 | if (pad == FIMC_SD_PAD_SOURCE) { | |
566 | if (ctx->rotation != 90 && ctx->rotation != 270) | |
567 | align_h = 1; | |
568 | max_sc_h = min(SCALER_MAX_HRATIO, 1 << (ffs(sink->width) - 3)); | |
569 | max_sc_v = min(SCALER_MAX_VRATIO, 1 << (ffs(sink->height) - 1)); | |
570 | min_sz = var->min_out_pixsize; | |
571 | } else { | |
572 | u32 depth = fimc_get_format_depth(sink->fmt); | |
573 | align_sz = 64/ALIGN(depth, 8); | |
574 | min_sz = var->min_inp_pixsize; | |
575 | min_w = min_h = min_sz; | |
576 | max_sc_h = max_sc_v = 1; | |
577 | } | |
578 | /* | |
579 | * For the crop rectangle at source pad the following constraints | |
580 | * must be met: | |
581 | * - it must fit in the sink pad format rectangle (f_width/f_height); | |
582 | * - maximum downscaling ratio is 64; | |
583 | * - maximum crop size depends if the rotator is used or not; | |
584 | * - the sink pad format width/height must be 4 multiple of the | |
585 | * prescaler ratios determined by sink pad size and source pad crop, | |
586 | * the prescaler ratio is returned by fimc_get_scaler_factor(). | |
587 | */ | |
588 | max_w = min_t(u32, | |
589 | rotate ? pl->out_rot_en_w : pl->out_rot_dis_w, | |
590 | rotate ? sink->f_height : sink->f_width); | |
591 | max_h = min_t(u32, FIMC_CAMIF_MAX_HEIGHT, sink->f_height); | |
592 | if (pad == FIMC_SD_PAD_SOURCE) { | |
593 | min_w = min_t(u32, max_w, sink->f_width / max_sc_h); | |
594 | min_h = min_t(u32, max_h, sink->f_height / max_sc_v); | |
595 | if (rotate) { | |
596 | swap(max_sc_h, max_sc_v); | |
597 | swap(min_w, min_h); | |
598 | } | |
599 | } | |
600 | v4l_bound_align_image(&r->width, min_w, max_w, ffs(min_sz) - 1, | |
601 | &r->height, min_h, max_h, align_h, | |
602 | align_sz); | |
603 | /* Adjust left/top if cropping rectangle is out of bounds */ | |
604 | r->left = clamp_t(u32, r->left, 0, sink->f_width - r->width); | |
605 | r->top = clamp_t(u32, r->top, 0, sink->f_height - r->height); | |
606 | r->left = round_down(r->left, var->hor_offs_align); | |
607 | ||
608 | dbg("pad%d: (%d,%d)/%dx%d, sink fmt: %dx%d", | |
609 | pad, r->left, r->top, r->width, r->height, | |
610 | sink->f_width, sink->f_height); | |
611 | } | |
612 | ||
613 | /* | |
614 | * The video node ioctl operations | |
615 | */ | |
5f3cc447 SN |
616 | static int fimc_vidioc_querycap_capture(struct file *file, void *priv, |
617 | struct v4l2_capability *cap) | |
618 | { | |
e578588e | 619 | struct fimc_dev *fimc = video_drvdata(file); |
5f3cc447 SN |
620 | |
621 | strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1); | |
622 | strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1); | |
623 | cap->bus_info[0] = 0; | |
8f401543 | 624 | cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE_MPLANE; |
5f3cc447 SN |
625 | |
626 | return 0; | |
627 | } | |
628 | ||
cf52df8a SN |
629 | static int fimc_cap_enum_fmt_mplane(struct file *file, void *priv, |
630 | struct v4l2_fmtdesc *f) | |
631 | { | |
632 | struct fimc_fmt *fmt; | |
633 | ||
634 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM | FMT_FLAGS_M2M, | |
635 | f->index); | |
636 | if (!fmt) | |
637 | return -EINVAL; | |
638 | strncpy(f->description, fmt->name, sizeof(f->description) - 1); | |
639 | f->pixelformat = fmt->fourcc; | |
640 | if (fmt->fourcc == V4L2_MBUS_FMT_JPEG_1X8) | |
641 | f->flags |= V4L2_FMT_FLAG_COMPRESSED; | |
642 | return 0; | |
643 | } | |
644 | ||
237e0265 SN |
645 | /** |
646 | * fimc_pipeline_try_format - negotiate and/or set formats at pipeline | |
647 | * elements | |
648 | * @ctx: FIMC capture context | |
649 | * @tfmt: media bus format to try/set on subdevs | |
650 | * @fmt_id: fimc pixel format id corresponding to returned @tfmt (output) | |
651 | * @set: true to set format on subdevs, false to try only | |
652 | */ | |
653 | static int fimc_pipeline_try_format(struct fimc_ctx *ctx, | |
654 | struct v4l2_mbus_framefmt *tfmt, | |
655 | struct fimc_fmt **fmt_id, | |
656 | bool set) | |
657 | { | |
658 | struct fimc_dev *fimc = ctx->fimc_dev; | |
659 | struct v4l2_subdev *sd = fimc->pipeline.sensor; | |
660 | struct v4l2_subdev *csis = fimc->pipeline.csis; | |
661 | struct v4l2_subdev_format sfmt; | |
662 | struct v4l2_mbus_framefmt *mf = &sfmt.format; | |
663 | struct fimc_fmt *ffmt = NULL; | |
664 | int ret, i = 0; | |
665 | ||
666 | if (WARN_ON(!sd || !tfmt)) | |
667 | return -EINVAL; | |
5f3cc447 | 668 | |
237e0265 SN |
669 | memset(&sfmt, 0, sizeof(sfmt)); |
670 | sfmt.format = *tfmt; | |
671 | ||
672 | sfmt.which = set ? V4L2_SUBDEV_FORMAT_ACTIVE : V4L2_SUBDEV_FORMAT_TRY; | |
673 | while (1) { | |
674 | ffmt = fimc_find_format(NULL, mf->code != 0 ? &mf->code : NULL, | |
675 | FMT_FLAGS_CAM, i++); | |
676 | if (ffmt == NULL) { | |
677 | /* | |
678 | * Notify user-space if common pixel code for | |
679 | * host and sensor does not exist. | |
680 | */ | |
681 | return -EINVAL; | |
682 | } | |
683 | mf->code = tfmt->code = ffmt->mbus_code; | |
5f3cc447 | 684 | |
237e0265 SN |
685 | ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sfmt); |
686 | if (ret) | |
687 | return ret; | |
688 | if (mf->code != tfmt->code) { | |
689 | mf->code = 0; | |
690 | continue; | |
691 | } | |
692 | if (mf->width != tfmt->width || mf->width != tfmt->width) { | |
693 | u32 fcc = ffmt->fourcc; | |
694 | tfmt->width = mf->width; | |
695 | tfmt->height = mf->height; | |
696 | ffmt = fimc_capture_try_format(ctx, | |
697 | &tfmt->width, &tfmt->height, | |
698 | NULL, &fcc, FIMC_SD_PAD_SOURCE); | |
699 | if (ffmt && ffmt->mbus_code) | |
700 | mf->code = ffmt->mbus_code; | |
701 | if (mf->width != tfmt->width || mf->width != tfmt->width) | |
702 | continue; | |
703 | tfmt->code = mf->code; | |
704 | } | |
705 | if (csis) | |
706 | ret = v4l2_subdev_call(csis, pad, set_fmt, NULL, &sfmt); | |
5f3cc447 | 707 | |
237e0265 SN |
708 | if (mf->code == tfmt->code && |
709 | mf->width == tfmt->width && mf->width == tfmt->width) | |
710 | break; | |
711 | } | |
5f3cc447 | 712 | |
237e0265 SN |
713 | if (fmt_id && ffmt) |
714 | *fmt_id = ffmt; | |
715 | *tfmt = *mf; | |
5f3cc447 | 716 | |
237e0265 SN |
717 | dbg("code: 0x%x, %dx%d, %p", mf->code, mf->width, mf->height, ffmt); |
718 | return 0; | |
719 | } | |
5f3cc447 | 720 | |
e578588e SN |
721 | static int fimc_cap_g_fmt_mplane(struct file *file, void *fh, |
722 | struct v4l2_format *f) | |
723 | { | |
724 | struct fimc_dev *fimc = video_drvdata(file); | |
725 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
726 | ||
727 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | |
728 | return -EINVAL; | |
729 | ||
730 | return fimc_fill_format(&ctx->d_frame, f); | |
731 | } | |
732 | ||
733 | static int fimc_cap_try_fmt_mplane(struct file *file, void *fh, | |
734 | struct v4l2_format *f) | |
735 | { | |
237e0265 | 736 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; |
e578588e SN |
737 | struct fimc_dev *fimc = video_drvdata(file); |
738 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
237e0265 SN |
739 | struct v4l2_mbus_framefmt mf; |
740 | struct fimc_fmt *ffmt = NULL; | |
741 | ||
742 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | |
743 | return -EINVAL; | |
744 | ||
745 | if (pix->pixelformat == V4L2_PIX_FMT_JPEG) { | |
746 | fimc_capture_try_format(ctx, &pix->width, &pix->height, | |
747 | NULL, &pix->pixelformat, | |
748 | FIMC_SD_PAD_SINK); | |
749 | ctx->s_frame.f_width = pix->width; | |
750 | ctx->s_frame.f_height = pix->height; | |
751 | } | |
752 | ffmt = fimc_capture_try_format(ctx, &pix->width, &pix->height, | |
753 | NULL, &pix->pixelformat, | |
754 | FIMC_SD_PAD_SOURCE); | |
755 | if (!ffmt) | |
756 | return -EINVAL; | |
757 | ||
758 | if (!fimc->vid_cap.user_subdev_api) { | |
759 | mf.width = pix->width; | |
760 | mf.height = pix->height; | |
761 | mf.code = ffmt->mbus_code; | |
762 | fimc_md_graph_lock(fimc); | |
763 | fimc_pipeline_try_format(ctx, &mf, &ffmt, false); | |
764 | fimc_md_graph_unlock(fimc); | |
765 | ||
766 | pix->width = mf.width; | |
767 | pix->height = mf.height; | |
768 | if (ffmt) | |
769 | pix->pixelformat = ffmt->fourcc; | |
770 | } | |
e578588e | 771 | |
237e0265 | 772 | fimc_adjust_mplane_format(ffmt, pix->width, pix->height, pix); |
4db5e27e | 773 | return 0; |
e578588e SN |
774 | } |
775 | ||
ee7160e5 SN |
776 | static void fimc_capture_mark_jpeg_xfer(struct fimc_ctx *ctx, bool jpeg) |
777 | { | |
778 | ctx->scaler.enabled = !jpeg; | |
779 | fimc_ctrls_activate(ctx, !jpeg); | |
780 | ||
781 | if (jpeg) | |
782 | set_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state); | |
783 | else | |
784 | clear_bit(ST_CAPT_JPEG, &ctx->fimc_dev->state); | |
785 | } | |
786 | ||
237e0265 | 787 | static int fimc_capture_set_format(struct fimc_dev *fimc, struct v4l2_format *f) |
5f3cc447 | 788 | { |
e578588e | 789 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; |
237e0265 SN |
790 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; |
791 | struct v4l2_mbus_framefmt *mf = &fimc->vid_cap.mf; | |
792 | struct fimc_frame *ff = &ctx->d_frame; | |
793 | struct fimc_fmt *s_fmt = NULL; | |
794 | int ret, i; | |
5f3cc447 | 795 | |
ef7af59b | 796 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
5f3cc447 | 797 | return -EINVAL; |
237e0265 | 798 | if (vb2_is_busy(&fimc->vid_cap.vbq)) |
ef7af59b | 799 | return -EBUSY; |
5f3cc447 | 800 | |
237e0265 SN |
801 | /* Pre-configure format at camera interface input, for JPEG only */ |
802 | if (pix->pixelformat == V4L2_PIX_FMT_JPEG) { | |
803 | fimc_capture_try_format(ctx, &pix->width, &pix->height, | |
804 | NULL, &pix->pixelformat, | |
805 | FIMC_SD_PAD_SINK); | |
806 | ctx->s_frame.f_width = pix->width; | |
807 | ctx->s_frame.f_height = pix->height; | |
808 | } | |
809 | /* Try the format at the scaler and the DMA output */ | |
810 | ff->fmt = fimc_capture_try_format(ctx, &pix->width, &pix->height, | |
811 | NULL, &pix->pixelformat, | |
812 | FIMC_SD_PAD_SOURCE); | |
813 | if (!ff->fmt) | |
8293ebfc | 814 | return -EINVAL; |
237e0265 SN |
815 | /* Try to match format at the host and the sensor */ |
816 | if (!fimc->vid_cap.user_subdev_api) { | |
817 | mf->code = ff->fmt->mbus_code; | |
818 | mf->width = pix->width; | |
819 | mf->height = pix->height; | |
820 | ||
821 | fimc_md_graph_lock(fimc); | |
822 | ret = fimc_pipeline_try_format(ctx, mf, &s_fmt, true); | |
823 | fimc_md_graph_unlock(fimc); | |
824 | if (ret) | |
825 | return ret; | |
826 | pix->width = mf->width; | |
827 | pix->height = mf->height; | |
828 | } | |
829 | fimc_adjust_mplane_format(ff->fmt, pix->width, pix->height, pix); | |
830 | for (i = 0; i < ff->fmt->colplanes; i++) | |
831 | ff->payload[i] = | |
832 | (pix->width * pix->height * ff->fmt->depth[i]) / 8; | |
833 | ||
834 | set_frame_bounds(ff, pix->width, pix->height); | |
835 | /* Reset the composition rectangle if not yet configured */ | |
836 | if (!(ctx->state & FIMC_DST_CROP)) | |
837 | set_frame_crop(ff, 0, 0, pix->width, pix->height); | |
838 | ||
ee7160e5 SN |
839 | fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ff->fmt->color)); |
840 | ||
237e0265 SN |
841 | /* Reset cropping and set format at the camera interface input */ |
842 | if (!fimc->vid_cap.user_subdev_api) { | |
843 | ctx->s_frame.fmt = s_fmt; | |
844 | set_frame_bounds(&ctx->s_frame, pix->width, pix->height); | |
845 | set_frame_crop(&ctx->s_frame, 0, 0, pix->width, pix->height); | |
045030fa | 846 | } |
ef7af59b | 847 | |
237e0265 SN |
848 | return ret; |
849 | } | |
5f3cc447 | 850 | |
237e0265 SN |
851 | static int fimc_cap_s_fmt_mplane(struct file *file, void *priv, |
852 | struct v4l2_format *f) | |
853 | { | |
854 | struct fimc_dev *fimc = video_drvdata(file); | |
5f3cc447 | 855 | |
237e0265 | 856 | return fimc_capture_set_format(fimc, f); |
5f3cc447 SN |
857 | } |
858 | ||
859 | static int fimc_cap_enum_input(struct file *file, void *priv, | |
3e002182 | 860 | struct v4l2_input *i) |
5f3cc447 | 861 | { |
e578588e | 862 | struct fimc_dev *fimc = video_drvdata(file); |
4db5e27e | 863 | struct v4l2_subdev *sd = fimc->pipeline.sensor; |
5f3cc447 | 864 | |
3e002182 | 865 | if (i->index != 0) |
5f3cc447 SN |
866 | return -EINVAL; |
867 | ||
5f3cc447 | 868 | i->type = V4L2_INPUT_TYPE_CAMERA; |
4db5e27e SN |
869 | if (sd) |
870 | strlcpy(i->name, sd->name, sizeof(i->name)); | |
5f3cc447 SN |
871 | return 0; |
872 | } | |
873 | ||
3e002182 | 874 | static int fimc_cap_s_input(struct file *file, void *priv, unsigned int i) |
5f3cc447 | 875 | { |
3e002182 | 876 | return i == 0 ? i : -EINVAL; |
5f3cc447 SN |
877 | } |
878 | ||
3e002182 | 879 | static int fimc_cap_g_input(struct file *file, void *priv, unsigned int *i) |
5f3cc447 | 880 | { |
3e002182 | 881 | *i = 0; |
5f3cc447 SN |
882 | return 0; |
883 | } | |
884 | ||
237e0265 SN |
885 | /** |
886 | * fimc_pipeline_validate - check for formats inconsistencies | |
887 | * between source and sink pad of each link | |
888 | * | |
889 | * Return 0 if all formats match or -EPIPE otherwise. | |
890 | */ | |
891 | static int fimc_pipeline_validate(struct fimc_dev *fimc) | |
892 | { | |
893 | struct v4l2_subdev_format sink_fmt, src_fmt; | |
894 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; | |
895 | struct v4l2_subdev *sd; | |
896 | struct media_pad *pad; | |
897 | int ret; | |
898 | ||
899 | /* Start with the video capture node pad */ | |
900 | pad = media_entity_remote_source(&vid_cap->vd_pad); | |
901 | if (pad == NULL) | |
902 | return -EPIPE; | |
903 | /* FIMC.{N} subdevice */ | |
904 | sd = media_entity_to_v4l2_subdev(pad->entity); | |
905 | ||
906 | while (1) { | |
907 | /* Retrieve format at the sink pad */ | |
908 | pad = &sd->entity.pads[0]; | |
909 | if (!(pad->flags & MEDIA_PAD_FL_SINK)) | |
910 | break; | |
911 | /* Don't call FIMC subdev operation to avoid nested locking */ | |
912 | if (sd == fimc->vid_cap.subdev) { | |
913 | struct fimc_frame *ff = &vid_cap->ctx->s_frame; | |
914 | sink_fmt.format.width = ff->f_width; | |
915 | sink_fmt.format.height = ff->f_height; | |
916 | sink_fmt.format.code = ff->fmt ? ff->fmt->mbus_code : 0; | |
917 | } else { | |
918 | sink_fmt.pad = pad->index; | |
919 | sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
920 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt); | |
921 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
922 | return -EPIPE; | |
923 | } | |
924 | /* Retrieve format at the source pad */ | |
925 | pad = media_entity_remote_source(pad); | |
926 | if (pad == NULL || | |
927 | media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
928 | break; | |
929 | ||
930 | sd = media_entity_to_v4l2_subdev(pad->entity); | |
931 | src_fmt.pad = pad->index; | |
932 | src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
933 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt); | |
934 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
935 | return -EPIPE; | |
936 | ||
937 | if (src_fmt.format.width != sink_fmt.format.width || | |
938 | src_fmt.format.height != sink_fmt.format.height || | |
939 | src_fmt.format.code != sink_fmt.format.code) | |
940 | return -EPIPE; | |
941 | } | |
942 | return 0; | |
943 | } | |
944 | ||
5f3cc447 | 945 | static int fimc_cap_streamon(struct file *file, void *priv, |
2dab38e2 | 946 | enum v4l2_buf_type type) |
5f3cc447 | 947 | { |
e578588e | 948 | struct fimc_dev *fimc = video_drvdata(file); |
4db5e27e | 949 | struct fimc_pipeline *p = &fimc->pipeline; |
237e0265 | 950 | int ret; |
5f3cc447 | 951 | |
4db5e27e | 952 | if (fimc_capture_active(fimc)) |
8293ebfc | 953 | return -EBUSY; |
5f3cc447 | 954 | |
4db5e27e | 955 | media_entity_pipeline_start(&p->sensor->entity, p->pipe); |
5f3cc447 | 956 | |
237e0265 SN |
957 | if (fimc->vid_cap.user_subdev_api) { |
958 | ret = fimc_pipeline_validate(fimc); | |
959 | if (ret) | |
960 | return ret; | |
961 | } | |
8293ebfc | 962 | return vb2_streamon(&fimc->vid_cap.vbq, type); |
5f3cc447 SN |
963 | } |
964 | ||
965 | static int fimc_cap_streamoff(struct file *file, void *priv, | |
8293ebfc | 966 | enum v4l2_buf_type type) |
5f3cc447 | 967 | { |
e578588e | 968 | struct fimc_dev *fimc = video_drvdata(file); |
4db5e27e SN |
969 | struct v4l2_subdev *sd = fimc->pipeline.sensor; |
970 | int ret; | |
5f3cc447 | 971 | |
4db5e27e SN |
972 | ret = vb2_streamoff(&fimc->vid_cap.vbq, type); |
973 | if (ret == 0) | |
974 | media_entity_pipeline_stop(&sd->entity); | |
975 | return ret; | |
5f3cc447 SN |
976 | } |
977 | ||
978 | static int fimc_cap_reqbufs(struct file *file, void *priv, | |
ef7af59b | 979 | struct v4l2_requestbuffers *reqbufs) |
5f3cc447 | 980 | { |
e578588e SN |
981 | struct fimc_dev *fimc = video_drvdata(file); |
982 | int ret = vb2_reqbufs(&fimc->vid_cap.vbq, reqbufs); | |
5f3cc447 | 983 | |
5f3cc447 | 984 | if (!ret) |
e578588e | 985 | fimc->vid_cap.reqbufs_count = reqbufs->count; |
5f3cc447 SN |
986 | return ret; |
987 | } | |
988 | ||
989 | static int fimc_cap_querybuf(struct file *file, void *priv, | |
990 | struct v4l2_buffer *buf) | |
991 | { | |
e578588e | 992 | struct fimc_dev *fimc = video_drvdata(file); |
5f3cc447 | 993 | |
e578588e | 994 | return vb2_querybuf(&fimc->vid_cap.vbq, buf); |
5f3cc447 SN |
995 | } |
996 | ||
997 | static int fimc_cap_qbuf(struct file *file, void *priv, | |
998 | struct v4l2_buffer *buf) | |
999 | { | |
e578588e SN |
1000 | struct fimc_dev *fimc = video_drvdata(file); |
1001 | ||
1002 | return vb2_qbuf(&fimc->vid_cap.vbq, buf); | |
5f3cc447 SN |
1003 | } |
1004 | ||
1005 | static int fimc_cap_dqbuf(struct file *file, void *priv, | |
1006 | struct v4l2_buffer *buf) | |
1007 | { | |
e578588e SN |
1008 | struct fimc_dev *fimc = video_drvdata(file); |
1009 | ||
1010 | return vb2_dqbuf(&fimc->vid_cap.vbq, buf, file->f_flags & O_NONBLOCK); | |
5f3cc447 SN |
1011 | } |
1012 | ||
e004e02f SN |
1013 | static int fimc_cap_cropcap(struct file *file, void *fh, |
1014 | struct v4l2_cropcap *cr) | |
1015 | { | |
e578588e SN |
1016 | struct fimc_dev *fimc = video_drvdata(file); |
1017 | struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame; | |
e004e02f | 1018 | |
ef7af59b | 1019 | if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
e004e02f SN |
1020 | return -EINVAL; |
1021 | ||
e004e02f SN |
1022 | cr->bounds.left = 0; |
1023 | cr->bounds.top = 0; | |
1024 | cr->bounds.width = f->o_width; | |
1025 | cr->bounds.height = f->o_height; | |
1026 | cr->defrect = cr->bounds; | |
1027 | ||
e004e02f SN |
1028 | return 0; |
1029 | } | |
1030 | ||
1031 | static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr) | |
1032 | { | |
e578588e SN |
1033 | struct fimc_dev *fimc = video_drvdata(file); |
1034 | struct fimc_frame *f = &fimc->vid_cap.ctx->s_frame; | |
8293ebfc | 1035 | |
e004e02f SN |
1036 | cr->c.left = f->offs_h; |
1037 | cr->c.top = f->offs_v; | |
1038 | cr->c.width = f->width; | |
1039 | cr->c.height = f->height; | |
1040 | ||
e004e02f SN |
1041 | return 0; |
1042 | } | |
1043 | ||
e578588e | 1044 | static int fimc_cap_s_crop(struct file *file, void *fh, struct v4l2_crop *cr) |
5f3cc447 | 1045 | { |
e578588e SN |
1046 | struct fimc_dev *fimc = video_drvdata(file); |
1047 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
237e0265 SN |
1048 | struct fimc_frame *ff; |
1049 | unsigned long flags; | |
5f3cc447 | 1050 | |
237e0265 SN |
1051 | fimc_capture_try_crop(ctx, &cr->c, FIMC_SD_PAD_SINK); |
1052 | ff = &ctx->s_frame; | |
5f3cc447 | 1053 | |
237e0265 SN |
1054 | spin_lock_irqsave(&fimc->slock, flags); |
1055 | set_frame_crop(ff, cr->c.left, cr->c.top, cr->c.width, cr->c.height); | |
1056 | set_bit(ST_CAPT_APPLY_CFG, &fimc->state); | |
1057 | spin_unlock_irqrestore(&fimc->slock, flags); | |
8293ebfc SN |
1058 | |
1059 | return 0; | |
5f3cc447 SN |
1060 | } |
1061 | ||
5f3cc447 SN |
1062 | static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = { |
1063 | .vidioc_querycap = fimc_vidioc_querycap_capture, | |
1064 | ||
cf52df8a | 1065 | .vidioc_enum_fmt_vid_cap_mplane = fimc_cap_enum_fmt_mplane, |
e578588e | 1066 | .vidioc_try_fmt_vid_cap_mplane = fimc_cap_try_fmt_mplane, |
ef7af59b | 1067 | .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane, |
e578588e | 1068 | .vidioc_g_fmt_vid_cap_mplane = fimc_cap_g_fmt_mplane, |
5f3cc447 SN |
1069 | |
1070 | .vidioc_reqbufs = fimc_cap_reqbufs, | |
1071 | .vidioc_querybuf = fimc_cap_querybuf, | |
1072 | ||
1073 | .vidioc_qbuf = fimc_cap_qbuf, | |
1074 | .vidioc_dqbuf = fimc_cap_dqbuf, | |
1075 | ||
1076 | .vidioc_streamon = fimc_cap_streamon, | |
1077 | .vidioc_streamoff = fimc_cap_streamoff, | |
1078 | ||
e004e02f | 1079 | .vidioc_g_crop = fimc_cap_g_crop, |
5f3cc447 | 1080 | .vidioc_s_crop = fimc_cap_s_crop, |
e004e02f | 1081 | .vidioc_cropcap = fimc_cap_cropcap, |
5f3cc447 SN |
1082 | |
1083 | .vidioc_enum_input = fimc_cap_enum_input, | |
1084 | .vidioc_s_input = fimc_cap_s_input, | |
1085 | .vidioc_g_input = fimc_cap_g_input, | |
1086 | }; | |
1087 | ||
237e0265 | 1088 | /* Capture subdev media entity operations */ |
d09a7dc8 SN |
1089 | static int fimc_link_setup(struct media_entity *entity, |
1090 | const struct media_pad *local, | |
1091 | const struct media_pad *remote, u32 flags) | |
1092 | { | |
237e0265 SN |
1093 | struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity); |
1094 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); | |
1095 | ||
1096 | if (media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
1097 | return -EINVAL; | |
d09a7dc8 SN |
1098 | |
1099 | if (WARN_ON(fimc == NULL)) | |
1100 | return 0; | |
1101 | ||
1102 | dbg("%s --> %s, flags: 0x%x. input: 0x%x", | |
1103 | local->entity->name, remote->entity->name, flags, | |
1104 | fimc->vid_cap.input); | |
1105 | ||
1106 | if (flags & MEDIA_LNK_FL_ENABLED) { | |
1107 | if (fimc->vid_cap.input != 0) | |
1108 | return -EBUSY; | |
1109 | fimc->vid_cap.input = sd->grp_id; | |
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | fimc->vid_cap.input = 0; | |
1114 | return 0; | |
1115 | } | |
1116 | ||
237e0265 | 1117 | static const struct media_entity_operations fimc_sd_media_ops = { |
d09a7dc8 SN |
1118 | .link_setup = fimc_link_setup, |
1119 | }; | |
1120 | ||
e1d72f4d SN |
1121 | /** |
1122 | * fimc_sensor_notify - v4l2_device notification from a sensor subdev | |
1123 | * @sd: pointer to a subdev generating the notification | |
1124 | * @notification: the notification type, must be S5P_FIMC_TX_END_NOTIFY | |
1125 | * @arg: pointer to an u32 type integer that stores the frame payload value | |
1126 | * | |
1127 | * The End Of Frame notification sent by sensor subdev in its still capture | |
1128 | * mode. If there is only a single VSYNC generated by the sensor at the | |
1129 | * beginning of a frame transmission, FIMC does not issue the LastIrq | |
1130 | * (end of frame) interrupt. And this notification is used to complete the | |
1131 | * frame capture and returning a buffer to user-space. Subdev drivers should | |
1132 | * call this notification from their last 'End of frame capture' interrupt. | |
1133 | */ | |
1134 | void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification, | |
1135 | void *arg) | |
1136 | { | |
1137 | struct fimc_sensor_info *sensor; | |
1138 | struct fimc_vid_buffer *buf; | |
1139 | struct fimc_md *fmd; | |
1140 | struct fimc_dev *fimc; | |
1141 | unsigned long flags; | |
1142 | ||
1143 | if (sd == NULL) | |
1144 | return; | |
1145 | ||
1146 | sensor = v4l2_get_subdev_hostdata(sd); | |
1147 | fmd = entity_to_fimc_mdev(&sd->entity); | |
1148 | ||
1149 | spin_lock_irqsave(&fmd->slock, flags); | |
1150 | fimc = sensor ? sensor->host : NULL; | |
1151 | ||
1152 | if (fimc && arg && notification == S5P_FIMC_TX_END_NOTIFY && | |
1153 | test_bit(ST_CAPT_PEND, &fimc->state)) { | |
1154 | unsigned long irq_flags; | |
1155 | spin_lock_irqsave(&fimc->slock, irq_flags); | |
1156 | if (!list_empty(&fimc->vid_cap.active_buf_q)) { | |
1157 | buf = list_entry(fimc->vid_cap.active_buf_q.next, | |
1158 | struct fimc_vid_buffer, list); | |
1159 | vb2_set_plane_payload(&buf->vb, 0, *((u32 *)arg)); | |
1160 | } | |
1161 | fimc_capture_irq_handler(fimc, true); | |
1162 | fimc_deactivate_capture(fimc); | |
1163 | spin_unlock_irqrestore(&fimc->slock, irq_flags); | |
1164 | } | |
1165 | spin_unlock_irqrestore(&fmd->slock, flags); | |
1166 | } | |
1167 | ||
237e0265 SN |
1168 | static int fimc_subdev_enum_mbus_code(struct v4l2_subdev *sd, |
1169 | struct v4l2_subdev_fh *fh, | |
1170 | struct v4l2_subdev_mbus_code_enum *code) | |
1171 | { | |
1172 | struct fimc_fmt *fmt; | |
1173 | ||
1174 | fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, code->index); | |
1175 | if (!fmt) | |
1176 | return -EINVAL; | |
1177 | code->code = fmt->mbus_code; | |
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | static int fimc_subdev_get_fmt(struct v4l2_subdev *sd, | |
1182 | struct v4l2_subdev_fh *fh, | |
1183 | struct v4l2_subdev_format *fmt) | |
1184 | { | |
1185 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); | |
1186 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
1187 | struct v4l2_mbus_framefmt *mf; | |
1188 | struct fimc_frame *ff; | |
1189 | ||
1190 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { | |
1191 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); | |
1192 | fmt->format = *mf; | |
1193 | return 0; | |
1194 | } | |
1195 | mf = &fmt->format; | |
1196 | mf->colorspace = V4L2_COLORSPACE_JPEG; | |
1197 | ff = fmt->pad == FIMC_SD_PAD_SINK ? &ctx->s_frame : &ctx->d_frame; | |
1198 | ||
1199 | mutex_lock(&fimc->lock); | |
1200 | /* The pixel code is same on both input and output pad */ | |
1201 | if (!WARN_ON(ctx->s_frame.fmt == NULL)) | |
1202 | mf->code = ctx->s_frame.fmt->mbus_code; | |
1203 | mf->width = ff->f_width; | |
1204 | mf->height = ff->f_height; | |
1205 | mutex_unlock(&fimc->lock); | |
1206 | ||
1207 | return 0; | |
1208 | } | |
1209 | ||
1210 | static int fimc_subdev_set_fmt(struct v4l2_subdev *sd, | |
1211 | struct v4l2_subdev_fh *fh, | |
1212 | struct v4l2_subdev_format *fmt) | |
1213 | { | |
1214 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); | |
1215 | struct v4l2_mbus_framefmt *mf = &fmt->format; | |
1216 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
1217 | struct fimc_frame *ff; | |
1218 | struct fimc_fmt *ffmt; | |
1219 | ||
1220 | dbg("pad%d: code: 0x%x, %dx%d", | |
1221 | fmt->pad, mf->code, mf->width, mf->height); | |
1222 | ||
1223 | if (fmt->pad == FIMC_SD_PAD_SOURCE && | |
1224 | vb2_is_busy(&fimc->vid_cap.vbq)) | |
1225 | return -EBUSY; | |
1226 | ||
1227 | mutex_lock(&fimc->lock); | |
1228 | ffmt = fimc_capture_try_format(ctx, &mf->width, &mf->height, | |
1229 | &mf->code, NULL, fmt->pad); | |
1230 | mutex_unlock(&fimc->lock); | |
1231 | mf->colorspace = V4L2_COLORSPACE_JPEG; | |
1232 | ||
1233 | if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { | |
1234 | mf = v4l2_subdev_get_try_format(fh, fmt->pad); | |
1235 | *mf = fmt->format; | |
1236 | return 0; | |
1237 | } | |
ee7160e5 SN |
1238 | fimc_capture_mark_jpeg_xfer(ctx, fimc_fmt_is_jpeg(ffmt->color)); |
1239 | ||
237e0265 SN |
1240 | ff = fmt->pad == FIMC_SD_PAD_SINK ? |
1241 | &ctx->s_frame : &ctx->d_frame; | |
1242 | ||
1243 | mutex_lock(&fimc->lock); | |
1244 | set_frame_bounds(ff, mf->width, mf->height); | |
1245 | ff->fmt = ffmt; | |
1246 | ||
1247 | /* Reset the crop rectangle if required. */ | |
1248 | if (!(fmt->pad == FIMC_SD_PAD_SOURCE && (ctx->state & FIMC_DST_CROP))) | |
1249 | set_frame_crop(ff, 0, 0, mf->width, mf->height); | |
1250 | ||
1251 | if (fmt->pad == FIMC_SD_PAD_SINK) | |
1252 | ctx->state &= ~FIMC_DST_CROP; | |
1253 | mutex_unlock(&fimc->lock); | |
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | static int fimc_subdev_get_crop(struct v4l2_subdev *sd, | |
1258 | struct v4l2_subdev_fh *fh, | |
1259 | struct v4l2_subdev_crop *crop) | |
1260 | { | |
1261 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); | |
1262 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
1263 | struct v4l2_rect *r = &crop->rect; | |
1264 | struct fimc_frame *ff; | |
1265 | ||
1266 | if (crop->which == V4L2_SUBDEV_FORMAT_TRY) { | |
1267 | crop->rect = *v4l2_subdev_get_try_crop(fh, crop->pad); | |
1268 | return 0; | |
1269 | } | |
1270 | ff = crop->pad == FIMC_SD_PAD_SINK ? | |
1271 | &ctx->s_frame : &ctx->d_frame; | |
1272 | ||
1273 | mutex_lock(&fimc->lock); | |
1274 | r->left = ff->offs_h; | |
1275 | r->top = ff->offs_v; | |
1276 | r->width = ff->width; | |
1277 | r->height = ff->height; | |
1278 | mutex_unlock(&fimc->lock); | |
1279 | ||
1280 | dbg("ff:%p, pad%d: l:%d, t:%d, %dx%d, f_w: %d, f_h: %d", | |
1281 | ff, crop->pad, r->left, r->top, r->width, r->height, | |
1282 | ff->f_width, ff->f_height); | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | static int fimc_subdev_set_crop(struct v4l2_subdev *sd, | |
1288 | struct v4l2_subdev_fh *fh, | |
1289 | struct v4l2_subdev_crop *crop) | |
1290 | { | |
1291 | struct fimc_dev *fimc = v4l2_get_subdevdata(sd); | |
1292 | struct fimc_ctx *ctx = fimc->vid_cap.ctx; | |
1293 | struct v4l2_rect *r = &crop->rect; | |
1294 | struct fimc_frame *ff; | |
1295 | unsigned long flags; | |
1296 | ||
1297 | dbg("(%d,%d)/%dx%d", r->left, r->top, r->width, r->height); | |
1298 | ||
1299 | ff = crop->pad == FIMC_SD_PAD_SOURCE ? | |
1300 | &ctx->d_frame : &ctx->s_frame; | |
1301 | ||
1302 | mutex_lock(&fimc->lock); | |
1303 | fimc_capture_try_crop(ctx, r, crop->pad); | |
1304 | ||
1305 | if (crop->which == V4L2_SUBDEV_FORMAT_TRY) { | |
1306 | mutex_lock(&fimc->lock); | |
1307 | *v4l2_subdev_get_try_crop(fh, crop->pad) = *r; | |
1308 | return 0; | |
1309 | } | |
1310 | spin_lock_irqsave(&fimc->slock, flags); | |
1311 | set_frame_crop(ff, r->left, r->top, r->width, r->height); | |
1312 | if (crop->pad == FIMC_SD_PAD_SOURCE) | |
1313 | ctx->state |= FIMC_DST_CROP; | |
1314 | ||
1315 | set_bit(ST_CAPT_APPLY_CFG, &fimc->state); | |
1316 | spin_unlock_irqrestore(&fimc->slock, flags); | |
1317 | ||
1318 | dbg("pad%d: (%d,%d)/%dx%d", crop->pad, r->left, r->top, | |
1319 | r->width, r->height); | |
1320 | ||
1321 | mutex_unlock(&fimc->lock); | |
1322 | return 0; | |
1323 | } | |
1324 | ||
1325 | static struct v4l2_subdev_pad_ops fimc_subdev_pad_ops = { | |
1326 | .enum_mbus_code = fimc_subdev_enum_mbus_code, | |
1327 | .get_fmt = fimc_subdev_get_fmt, | |
1328 | .set_fmt = fimc_subdev_set_fmt, | |
1329 | .get_crop = fimc_subdev_get_crop, | |
1330 | .set_crop = fimc_subdev_set_crop, | |
1331 | }; | |
1332 | ||
1333 | static struct v4l2_subdev_ops fimc_subdev_ops = { | |
1334 | .pad = &fimc_subdev_pad_ops, | |
1335 | }; | |
1336 | ||
1337 | static int fimc_create_capture_subdev(struct fimc_dev *fimc, | |
1338 | struct v4l2_device *v4l2_dev) | |
1339 | { | |
1340 | struct v4l2_subdev *sd; | |
1341 | int ret; | |
1342 | ||
1343 | sd = kzalloc(sizeof(*sd), GFP_KERNEL); | |
1344 | if (!sd) | |
1345 | return -ENOMEM; | |
1346 | ||
1347 | v4l2_subdev_init(sd, &fimc_subdev_ops); | |
1348 | sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE; | |
1349 | snprintf(sd->name, sizeof(sd->name), "FIMC.%d", fimc->pdev->id); | |
1350 | ||
1351 | fimc->vid_cap.sd_pads[FIMC_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK; | |
1352 | fimc->vid_cap.sd_pads[FIMC_SD_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE; | |
1353 | ret = media_entity_init(&sd->entity, FIMC_SD_PADS_NUM, | |
1354 | fimc->vid_cap.sd_pads, 0); | |
1355 | if (ret) | |
1356 | goto me_err; | |
1357 | ret = v4l2_device_register_subdev(v4l2_dev, sd); | |
1358 | if (ret) | |
1359 | goto sd_err; | |
1360 | ||
1361 | fimc->vid_cap.subdev = sd; | |
1362 | v4l2_set_subdevdata(sd, fimc); | |
1363 | sd->entity.ops = &fimc_sd_media_ops; | |
1364 | return 0; | |
1365 | sd_err: | |
1366 | media_entity_cleanup(&sd->entity); | |
1367 | me_err: | |
1368 | kfree(sd); | |
1369 | return ret; | |
1370 | } | |
1371 | ||
1372 | static void fimc_destroy_capture_subdev(struct fimc_dev *fimc) | |
1373 | { | |
1374 | struct v4l2_subdev *sd = fimc->vid_cap.subdev; | |
1375 | ||
1376 | if (!sd) | |
1377 | return; | |
1378 | media_entity_cleanup(&sd->entity); | |
1379 | v4l2_device_unregister_subdev(sd); | |
1380 | kfree(sd); | |
64c570f5 | 1381 | fimc->vid_cap.subdev = NULL; |
237e0265 SN |
1382 | } |
1383 | ||
1384 | /* Set default format at the sensor and host interface */ | |
1385 | static int fimc_capture_set_default_format(struct fimc_dev *fimc) | |
1386 | { | |
1387 | struct v4l2_format fmt = { | |
1388 | .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, | |
1389 | .fmt.pix_mp = { | |
1390 | .width = 640, | |
1391 | .height = 480, | |
1392 | .pixelformat = V4L2_PIX_FMT_YUYV, | |
1393 | .field = V4L2_FIELD_NONE, | |
1394 | .colorspace = V4L2_COLORSPACE_JPEG, | |
1395 | }, | |
1396 | }; | |
1397 | ||
1398 | return fimc_capture_set_format(fimc, &fmt); | |
1399 | } | |
1400 | ||
ef7af59b | 1401 | /* fimc->lock must be already initialized */ |
30c9939d SN |
1402 | int fimc_register_capture_device(struct fimc_dev *fimc, |
1403 | struct v4l2_device *v4l2_dev) | |
5f3cc447 | 1404 | { |
5f3cc447 SN |
1405 | struct video_device *vfd; |
1406 | struct fimc_vid_cap *vid_cap; | |
1407 | struct fimc_ctx *ctx; | |
2dab38e2 | 1408 | struct vb2_queue *q; |
30c9939d | 1409 | int ret = -ENOMEM; |
5f3cc447 SN |
1410 | |
1411 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); | |
1412 | if (!ctx) | |
1413 | return -ENOMEM; | |
1414 | ||
1415 | ctx->fimc_dev = fimc; | |
1416 | ctx->in_path = FIMC_CAMERA; | |
1417 | ctx->out_path = FIMC_DMA; | |
1418 | ctx->state = FIMC_CTX_CAP; | |
237e0265 SN |
1419 | ctx->s_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0); |
1420 | ctx->d_frame.fmt = fimc_find_format(NULL, NULL, FMT_FLAGS_CAM, 0); | |
5f3cc447 | 1421 | |
5f3cc447 SN |
1422 | vfd = video_device_alloc(); |
1423 | if (!vfd) { | |
1424 | v4l2_err(v4l2_dev, "Failed to allocate video device\n"); | |
30c9939d | 1425 | goto err_vd_alloc; |
5f3cc447 SN |
1426 | } |
1427 | ||
30c9939d SN |
1428 | snprintf(vfd->name, sizeof(vfd->name), "%s.capture", |
1429 | dev_name(&fimc->pdev->dev)); | |
5f3cc447 SN |
1430 | |
1431 | vfd->fops = &fimc_capture_fops; | |
1432 | vfd->ioctl_ops = &fimc_capture_ioctl_ops; | |
574e1717 | 1433 | vfd->v4l2_dev = v4l2_dev; |
5f3cc447 SN |
1434 | vfd->minor = -1; |
1435 | vfd->release = video_device_release; | |
8293ebfc | 1436 | vfd->lock = &fimc->lock; |
5f3cc447 SN |
1437 | video_set_drvdata(vfd, fimc); |
1438 | ||
1439 | vid_cap = &fimc->vid_cap; | |
1440 | vid_cap->vfd = vfd; | |
1441 | vid_cap->active_buf_cnt = 0; | |
1442 | vid_cap->reqbufs_count = 0; | |
1443 | vid_cap->refcnt = 0; | |
5f3cc447 SN |
1444 | |
1445 | INIT_LIST_HEAD(&vid_cap->pending_buf_q); | |
1446 | INIT_LIST_HEAD(&vid_cap->active_buf_q); | |
1447 | spin_lock_init(&ctx->slock); | |
1448 | vid_cap->ctx = ctx; | |
1449 | ||
2dab38e2 SN |
1450 | q = &fimc->vid_cap.vbq; |
1451 | memset(q, 0, sizeof(*q)); | |
ef7af59b | 1452 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
2dab38e2 SN |
1453 | q->io_modes = VB2_MMAP | VB2_USERPTR; |
1454 | q->drv_priv = fimc->vid_cap.ctx; | |
1455 | q->ops = &fimc_capture_qops; | |
1456 | q->mem_ops = &vb2_dma_contig_memops; | |
1457 | q->buf_struct_size = sizeof(struct fimc_vid_buffer); | |
1458 | ||
1459 | vb2_queue_init(q); | |
5f3cc447 | 1460 | |
574e1717 SN |
1461 | fimc->vid_cap.vd_pad.flags = MEDIA_PAD_FL_SINK; |
1462 | ret = media_entity_init(&vfd->entity, 1, &fimc->vid_cap.vd_pad, 0); | |
1463 | if (ret) | |
1464 | goto err_ent; | |
237e0265 SN |
1465 | ret = fimc_create_capture_subdev(fimc, v4l2_dev); |
1466 | if (ret) | |
1467 | goto err_sd_reg; | |
574e1717 | 1468 | |
131b6c61 | 1469 | vfd->ctrl_handler = &ctx->ctrl_handler; |
5f3cc447 SN |
1470 | return 0; |
1471 | ||
237e0265 SN |
1472 | err_sd_reg: |
1473 | media_entity_cleanup(&vfd->entity); | |
574e1717 | 1474 | err_ent: |
5f3cc447 | 1475 | video_device_release(vfd); |
30c9939d | 1476 | err_vd_alloc: |
cfd77310 | 1477 | kfree(ctx); |
5f3cc447 SN |
1478 | return ret; |
1479 | } | |
1480 | ||
1481 | void fimc_unregister_capture_device(struct fimc_dev *fimc) | |
1482 | { | |
574e1717 | 1483 | struct video_device *vfd = fimc->vid_cap.vfd; |
5f3cc447 | 1484 | |
574e1717 SN |
1485 | if (vfd) { |
1486 | media_entity_cleanup(&vfd->entity); | |
96a85742 SN |
1487 | /* Can also be called if video device was |
1488 | not registered */ | |
574e1717 SN |
1489 | video_unregister_device(vfd); |
1490 | } | |
237e0265 | 1491 | fimc_destroy_capture_subdev(fimc); |
574e1717 | 1492 | kfree(fimc->vid_cap.ctx); |
96a85742 | 1493 | fimc->vid_cap.ctx = NULL; |
5f3cc447 | 1494 | } |