diff options
Diffstat (limited to 'drivers/media/video/s5p-fimc/fimc-capture.c')
-rw-r--r-- | drivers/media/video/s5p-fimc/fimc-capture.c | 901 |
1 files changed, 901 insertions, 0 deletions
diff --git a/drivers/media/video/s5p-fimc/fimc-capture.c b/drivers/media/video/s5p-fimc/fimc-capture.c new file mode 100644 index 00000000000..0d730e55605 --- /dev/null +++ b/drivers/media/video/s5p-fimc/fimc-capture.c | |||
@@ -0,0 +1,901 @@ | |||
1 | /* | ||
2 | * Samsung S5P/EXYNOS4 SoC series camera interface (camera capture) driver | ||
3 | * | ||
4 | * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd. | ||
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> | ||
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> | ||
19 | #include <linux/platform_device.h> | ||
20 | #include <linux/list.h> | ||
21 | #include <linux/slab.h> | ||
22 | #include <linux/clk.h> | ||
23 | #include <linux/i2c.h> | ||
24 | |||
25 | #include <linux/videodev2.h> | ||
26 | #include <media/v4l2-device.h> | ||
27 | #include <media/v4l2-ioctl.h> | ||
28 | #include <media/v4l2-mem2mem.h> | ||
29 | #include <media/videobuf2-core.h> | ||
30 | #include <media/videobuf2-dma-contig.h> | ||
31 | |||
32 | #include "fimc-core.h" | ||
33 | |||
34 | static struct v4l2_subdev *fimc_subdev_register(struct fimc_dev *fimc, | ||
35 | struct s5p_fimc_isp_info *isp_info) | ||
36 | { | ||
37 | struct i2c_adapter *i2c_adap; | ||
38 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; | ||
39 | struct v4l2_subdev *sd = NULL; | ||
40 | |||
41 | i2c_adap = i2c_get_adapter(isp_info->i2c_bus_num); | ||
42 | if (!i2c_adap) | ||
43 | return ERR_PTR(-ENOMEM); | ||
44 | |||
45 | sd = v4l2_i2c_new_subdev_board(&vid_cap->v4l2_dev, i2c_adap, | ||
46 | isp_info->board_info, NULL); | ||
47 | if (!sd) { | ||
48 | v4l2_err(&vid_cap->v4l2_dev, "failed to acquire subdev\n"); | ||
49 | return NULL; | ||
50 | } | ||
51 | |||
52 | v4l2_info(&vid_cap->v4l2_dev, "subdevice %s registered successfuly\n", | ||
53 | isp_info->board_info->type); | ||
54 | |||
55 | return sd; | ||
56 | } | ||
57 | |||
58 | static void fimc_subdev_unregister(struct fimc_dev *fimc) | ||
59 | { | ||
60 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; | ||
61 | struct i2c_client *client; | ||
62 | |||
63 | if (vid_cap->input_index < 0) | ||
64 | return; /* Subdevice already released or not registered. */ | ||
65 | |||
66 | if (vid_cap->sd) { | ||
67 | v4l2_device_unregister_subdev(vid_cap->sd); | ||
68 | client = v4l2_get_subdevdata(vid_cap->sd); | ||
69 | i2c_unregister_device(client); | ||
70 | i2c_put_adapter(client->adapter); | ||
71 | vid_cap->sd = NULL; | ||
72 | } | ||
73 | |||
74 | vid_cap->input_index = -1; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * fimc_subdev_attach - attach v4l2_subdev to camera host interface | ||
79 | * | ||
80 | * @fimc: FIMC device information | ||
81 | * @index: index to the array of available subdevices, | ||
82 | * -1 for full array search or non negative value | ||
83 | * to select specific subdevice | ||
84 | */ | ||
85 | static int fimc_subdev_attach(struct fimc_dev *fimc, int index) | ||
86 | { | ||
87 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; | ||
88 | struct s5p_platform_fimc *pdata = fimc->pdata; | ||
89 | struct s5p_fimc_isp_info *isp_info; | ||
90 | struct v4l2_subdev *sd; | ||
91 | int i; | ||
92 | |||
93 | for (i = 0; i < pdata->num_clients; ++i) { | ||
94 | isp_info = &pdata->isp_info[i]; | ||
95 | |||
96 | if (index >= 0 && i != index) | ||
97 | continue; | ||
98 | |||
99 | sd = fimc_subdev_register(fimc, isp_info); | ||
100 | if (!IS_ERR_OR_NULL(sd)) { | ||
101 | vid_cap->sd = sd; | ||
102 | vid_cap->input_index = i; | ||
103 | |||
104 | return 0; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | vid_cap->input_index = -1; | ||
109 | vid_cap->sd = NULL; | ||
110 | v4l2_err(&vid_cap->v4l2_dev, "fimc%d: sensor attach failed\n", | ||
111 | fimc->id); | ||
112 | return -ENODEV; | ||
113 | } | ||
114 | |||
115 | static int fimc_isp_subdev_init(struct fimc_dev *fimc, unsigned int index) | ||
116 | { | ||
117 | struct s5p_fimc_isp_info *isp_info; | ||
118 | struct s5p_platform_fimc *pdata = fimc->pdata; | ||
119 | int ret; | ||
120 | |||
121 | if (index >= pdata->num_clients) | ||
122 | return -EINVAL; | ||
123 | |||
124 | isp_info = &pdata->isp_info[index]; | ||
125 | |||
126 | if (isp_info->clk_frequency) | ||
127 | clk_set_rate(fimc->clock[CLK_CAM], isp_info->clk_frequency); | ||
128 | |||
129 | ret = clk_enable(fimc->clock[CLK_CAM]); | ||
130 | if (ret) | ||
131 | return ret; | ||
132 | |||
133 | ret = fimc_subdev_attach(fimc, index); | ||
134 | if (ret) | ||
135 | return ret; | ||
136 | |||
137 | ret = fimc_hw_set_camera_polarity(fimc, isp_info); | ||
138 | if (ret) | ||
139 | return ret; | ||
140 | |||
141 | ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 1); | ||
142 | if (!ret) | ||
143 | return ret; | ||
144 | |||
145 | /* enabling power failed so unregister subdev */ | ||
146 | fimc_subdev_unregister(fimc); | ||
147 | |||
148 | v4l2_err(&fimc->vid_cap.v4l2_dev, "ISP initialization failed: %d\n", | ||
149 | ret); | ||
150 | |||
151 | return ret; | ||
152 | } | ||
153 | |||
154 | static int fimc_stop_capture(struct fimc_dev *fimc) | ||
155 | { | ||
156 | unsigned long flags; | ||
157 | struct fimc_vid_cap *cap; | ||
158 | struct fimc_vid_buffer *buf; | ||
159 | |||
160 | cap = &fimc->vid_cap; | ||
161 | |||
162 | if (!fimc_capture_active(fimc)) | ||
163 | return 0; | ||
164 | |||
165 | spin_lock_irqsave(&fimc->slock, flags); | ||
166 | set_bit(ST_CAPT_SHUT, &fimc->state); | ||
167 | fimc_deactivate_capture(fimc); | ||
168 | spin_unlock_irqrestore(&fimc->slock, flags); | ||
169 | |||
170 | wait_event_timeout(fimc->irq_queue, | ||
171 | !test_bit(ST_CAPT_SHUT, &fimc->state), | ||
172 | FIMC_SHUTDOWN_TIMEOUT); | ||
173 | |||
174 | v4l2_subdev_call(cap->sd, video, s_stream, 0); | ||
175 | |||
176 | spin_lock_irqsave(&fimc->slock, flags); | ||
177 | fimc->state &= ~(1 << ST_CAPT_RUN | 1 << ST_CAPT_PEND | | ||
178 | 1 << ST_CAPT_SHUT | 1 << ST_CAPT_STREAM); | ||
179 | |||
180 | fimc->vid_cap.active_buf_cnt = 0; | ||
181 | |||
182 | /* Release buffers that were enqueued in the driver by videobuf2. */ | ||
183 | while (!list_empty(&cap->pending_buf_q)) { | ||
184 | buf = pending_queue_pop(cap); | ||
185 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
186 | } | ||
187 | |||
188 | while (!list_empty(&cap->active_buf_q)) { | ||
189 | buf = active_queue_pop(cap); | ||
190 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
191 | } | ||
192 | |||
193 | spin_unlock_irqrestore(&fimc->slock, flags); | ||
194 | |||
195 | dbg("state: 0x%lx", fimc->state); | ||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | static int start_streaming(struct vb2_queue *q) | ||
200 | { | ||
201 | struct fimc_ctx *ctx = q->drv_priv; | ||
202 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
203 | struct s5p_fimc_isp_info *isp_info; | ||
204 | int ret; | ||
205 | |||
206 | fimc_hw_reset(fimc); | ||
207 | |||
208 | ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_stream, 1); | ||
209 | if (ret && ret != -ENOIOCTLCMD) | ||
210 | return ret; | ||
211 | |||
212 | ret = fimc_prepare_config(ctx, ctx->state); | ||
213 | if (ret) | ||
214 | return ret; | ||
215 | |||
216 | isp_info = &fimc->pdata->isp_info[fimc->vid_cap.input_index]; | ||
217 | fimc_hw_set_camera_type(fimc, isp_info); | ||
218 | fimc_hw_set_camera_source(fimc, isp_info); | ||
219 | fimc_hw_set_camera_offset(fimc, &ctx->s_frame); | ||
220 | |||
221 | if (ctx->state & FIMC_PARAMS) { | ||
222 | ret = fimc_set_scaler_info(ctx); | ||
223 | if (ret) { | ||
224 | err("Scaler setup error"); | ||
225 | return ret; | ||
226 | } | ||
227 | fimc_hw_set_input_path(ctx); | ||
228 | fimc_hw_set_prescaler(ctx); | ||
229 | fimc_hw_set_mainscaler(ctx); | ||
230 | fimc_hw_set_target_format(ctx); | ||
231 | fimc_hw_set_rotation(ctx); | ||
232 | fimc_hw_set_effect(ctx); | ||
233 | } | ||
234 | |||
235 | fimc_hw_set_output_path(ctx); | ||
236 | fimc_hw_set_out_dma(ctx); | ||
237 | |||
238 | INIT_LIST_HEAD(&fimc->vid_cap.pending_buf_q); | ||
239 | INIT_LIST_HEAD(&fimc->vid_cap.active_buf_q); | ||
240 | fimc->vid_cap.active_buf_cnt = 0; | ||
241 | fimc->vid_cap.frame_count = 0; | ||
242 | fimc->vid_cap.buf_index = 0; | ||
243 | |||
244 | set_bit(ST_CAPT_PEND, &fimc->state); | ||
245 | |||
246 | return 0; | ||
247 | } | ||
248 | |||
249 | static int stop_streaming(struct vb2_queue *q) | ||
250 | { | ||
251 | struct fimc_ctx *ctx = q->drv_priv; | ||
252 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
253 | |||
254 | if (!fimc_capture_active(fimc)) | ||
255 | return -EINVAL; | ||
256 | |||
257 | return fimc_stop_capture(fimc); | ||
258 | } | ||
259 | |||
260 | static unsigned int get_plane_size(struct fimc_frame *fr, unsigned int plane) | ||
261 | { | ||
262 | if (!fr || plane >= fr->fmt->memplanes) | ||
263 | return 0; | ||
264 | return fr->f_width * fr->f_height * fr->fmt->depth[plane] / 8; | ||
265 | } | ||
266 | |||
267 | static int queue_setup(struct vb2_queue *vq, unsigned int *num_buffers, | ||
268 | unsigned int *num_planes, unsigned long sizes[], | ||
269 | void *allocators[]) | ||
270 | { | ||
271 | struct fimc_ctx *ctx = vq->drv_priv; | ||
272 | struct fimc_fmt *fmt = ctx->d_frame.fmt; | ||
273 | int i; | ||
274 | |||
275 | if (!fmt) | ||
276 | return -EINVAL; | ||
277 | |||
278 | *num_planes = fmt->memplanes; | ||
279 | |||
280 | for (i = 0; i < fmt->memplanes; i++) { | ||
281 | sizes[i] = get_plane_size(&ctx->d_frame, i); | ||
282 | allocators[i] = ctx->fimc_dev->alloc_ctx; | ||
283 | } | ||
284 | |||
285 | return 0; | ||
286 | } | ||
287 | |||
288 | static int buffer_prepare(struct vb2_buffer *vb) | ||
289 | { | ||
290 | struct vb2_queue *vq = vb->vb2_queue; | ||
291 | struct fimc_ctx *ctx = vq->drv_priv; | ||
292 | struct v4l2_device *v4l2_dev = &ctx->fimc_dev->m2m.v4l2_dev; | ||
293 | int i; | ||
294 | |||
295 | if (!ctx->d_frame.fmt || vq->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | ||
296 | return -EINVAL; | ||
297 | |||
298 | for (i = 0; i < ctx->d_frame.fmt->memplanes; i++) { | ||
299 | unsigned long size = get_plane_size(&ctx->d_frame, i); | ||
300 | |||
301 | if (vb2_plane_size(vb, i) < size) { | ||
302 | v4l2_err(v4l2_dev, "User buffer too small (%ld < %ld)\n", | ||
303 | vb2_plane_size(vb, i), size); | ||
304 | return -EINVAL; | ||
305 | } | ||
306 | |||
307 | vb2_set_plane_payload(vb, i, size); | ||
308 | } | ||
309 | |||
310 | return 0; | ||
311 | } | ||
312 | |||
313 | static void buffer_queue(struct vb2_buffer *vb) | ||
314 | { | ||
315 | struct fimc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); | ||
316 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
317 | struct fimc_vid_buffer *buf | ||
318 | = container_of(vb, struct fimc_vid_buffer, vb); | ||
319 | struct fimc_vid_cap *vid_cap = &fimc->vid_cap; | ||
320 | unsigned long flags; | ||
321 | int min_bufs; | ||
322 | |||
323 | spin_lock_irqsave(&fimc->slock, flags); | ||
324 | fimc_prepare_addr(ctx, &buf->vb, &ctx->d_frame, &buf->paddr); | ||
325 | |||
326 | if (!test_bit(ST_CAPT_STREAM, &fimc->state) | ||
327 | && vid_cap->active_buf_cnt < FIMC_MAX_OUT_BUFS) { | ||
328 | /* Setup the buffer directly for processing. */ | ||
329 | int buf_id = (vid_cap->reqbufs_count == 1) ? -1 : | ||
330 | vid_cap->buf_index; | ||
331 | |||
332 | fimc_hw_set_output_addr(fimc, &buf->paddr, buf_id); | ||
333 | buf->index = vid_cap->buf_index; | ||
334 | active_queue_add(vid_cap, buf); | ||
335 | |||
336 | if (++vid_cap->buf_index >= FIMC_MAX_OUT_BUFS) | ||
337 | vid_cap->buf_index = 0; | ||
338 | } else { | ||
339 | fimc_pending_queue_add(vid_cap, buf); | ||
340 | } | ||
341 | |||
342 | min_bufs = vid_cap->reqbufs_count > 1 ? 2 : 1; | ||
343 | |||
344 | if (vid_cap->active_buf_cnt >= min_bufs && | ||
345 | !test_and_set_bit(ST_CAPT_STREAM, &fimc->state)) | ||
346 | fimc_activate_capture(ctx); | ||
347 | |||
348 | spin_unlock_irqrestore(&fimc->slock, flags); | ||
349 | } | ||
350 | |||
351 | static void fimc_lock(struct vb2_queue *vq) | ||
352 | { | ||
353 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); | ||
354 | mutex_lock(&ctx->fimc_dev->lock); | ||
355 | } | ||
356 | |||
357 | static void fimc_unlock(struct vb2_queue *vq) | ||
358 | { | ||
359 | struct fimc_ctx *ctx = vb2_get_drv_priv(vq); | ||
360 | mutex_unlock(&ctx->fimc_dev->lock); | ||
361 | } | ||
362 | |||
363 | static struct vb2_ops fimc_capture_qops = { | ||
364 | .queue_setup = queue_setup, | ||
365 | .buf_prepare = buffer_prepare, | ||
366 | .buf_queue = buffer_queue, | ||
367 | .wait_prepare = fimc_unlock, | ||
368 | .wait_finish = fimc_lock, | ||
369 | .start_streaming = start_streaming, | ||
370 | .stop_streaming = stop_streaming, | ||
371 | }; | ||
372 | |||
373 | static int fimc_capture_open(struct file *file) | ||
374 | { | ||
375 | struct fimc_dev *fimc = video_drvdata(file); | ||
376 | int ret = 0; | ||
377 | |||
378 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); | ||
379 | |||
380 | /* Return if the corresponding video mem2mem node is already opened. */ | ||
381 | if (fimc_m2m_active(fimc)) | ||
382 | return -EBUSY; | ||
383 | |||
384 | if (++fimc->vid_cap.refcnt == 1) { | ||
385 | ret = fimc_isp_subdev_init(fimc, 0); | ||
386 | if (ret) { | ||
387 | fimc->vid_cap.refcnt--; | ||
388 | return -EIO; | ||
389 | } | ||
390 | } | ||
391 | |||
392 | file->private_data = fimc->vid_cap.ctx; | ||
393 | |||
394 | return 0; | ||
395 | } | ||
396 | |||
397 | static int fimc_capture_close(struct file *file) | ||
398 | { | ||
399 | struct fimc_dev *fimc = video_drvdata(file); | ||
400 | |||
401 | dbg("pid: %d, state: 0x%lx", task_pid_nr(current), fimc->state); | ||
402 | |||
403 | if (--fimc->vid_cap.refcnt == 0) { | ||
404 | fimc_stop_capture(fimc); | ||
405 | vb2_queue_release(&fimc->vid_cap.vbq); | ||
406 | |||
407 | v4l2_err(&fimc->vid_cap.v4l2_dev, "releasing ISP\n"); | ||
408 | |||
409 | v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0); | ||
410 | clk_disable(fimc->clock[CLK_CAM]); | ||
411 | fimc_subdev_unregister(fimc); | ||
412 | } | ||
413 | |||
414 | return 0; | ||
415 | } | ||
416 | |||
417 | static unsigned int fimc_capture_poll(struct file *file, | ||
418 | struct poll_table_struct *wait) | ||
419 | { | ||
420 | struct fimc_ctx *ctx = file->private_data; | ||
421 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
422 | |||
423 | return vb2_poll(&fimc->vid_cap.vbq, file, wait); | ||
424 | } | ||
425 | |||
426 | static int fimc_capture_mmap(struct file *file, struct vm_area_struct *vma) | ||
427 | { | ||
428 | struct fimc_ctx *ctx = file->private_data; | ||
429 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
430 | |||
431 | return vb2_mmap(&fimc->vid_cap.vbq, vma); | ||
432 | } | ||
433 | |||
434 | /* video device file operations */ | ||
435 | static const struct v4l2_file_operations fimc_capture_fops = { | ||
436 | .owner = THIS_MODULE, | ||
437 | .open = fimc_capture_open, | ||
438 | .release = fimc_capture_close, | ||
439 | .poll = fimc_capture_poll, | ||
440 | .unlocked_ioctl = video_ioctl2, | ||
441 | .mmap = fimc_capture_mmap, | ||
442 | }; | ||
443 | |||
444 | static int fimc_vidioc_querycap_capture(struct file *file, void *priv, | ||
445 | struct v4l2_capability *cap) | ||
446 | { | ||
447 | struct fimc_ctx *ctx = file->private_data; | ||
448 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
449 | |||
450 | strncpy(cap->driver, fimc->pdev->name, sizeof(cap->driver) - 1); | ||
451 | strncpy(cap->card, fimc->pdev->name, sizeof(cap->card) - 1); | ||
452 | cap->bus_info[0] = 0; | ||
453 | cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE | | ||
454 | V4L2_CAP_VIDEO_CAPTURE_MPLANE; | ||
455 | |||
456 | return 0; | ||
457 | } | ||
458 | |||
459 | /* Synchronize formats of the camera interface input and attached sensor. */ | ||
460 | static int sync_capture_fmt(struct fimc_ctx *ctx) | ||
461 | { | ||
462 | struct fimc_frame *frame = &ctx->s_frame; | ||
463 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
464 | struct v4l2_mbus_framefmt *fmt = &fimc->vid_cap.fmt; | ||
465 | int ret; | ||
466 | |||
467 | fmt->width = ctx->d_frame.o_width; | ||
468 | fmt->height = ctx->d_frame.o_height; | ||
469 | |||
470 | ret = v4l2_subdev_call(fimc->vid_cap.sd, video, s_mbus_fmt, fmt); | ||
471 | if (ret == -ENOIOCTLCMD) { | ||
472 | err("s_mbus_fmt failed"); | ||
473 | return ret; | ||
474 | } | ||
475 | dbg("w: %d, h: %d, code= %d", fmt->width, fmt->height, fmt->code); | ||
476 | |||
477 | frame->fmt = find_mbus_format(fmt, FMT_FLAGS_CAM); | ||
478 | if (!frame->fmt) { | ||
479 | err("fimc source format not found\n"); | ||
480 | return -EINVAL; | ||
481 | } | ||
482 | |||
483 | frame->f_width = fmt->width; | ||
484 | frame->f_height = fmt->height; | ||
485 | frame->width = fmt->width; | ||
486 | frame->height = fmt->height; | ||
487 | frame->o_width = fmt->width; | ||
488 | frame->o_height = fmt->height; | ||
489 | frame->offs_h = 0; | ||
490 | frame->offs_v = 0; | ||
491 | |||
492 | return 0; | ||
493 | } | ||
494 | |||
495 | static int fimc_cap_s_fmt_mplane(struct file *file, void *priv, | ||
496 | struct v4l2_format *f) | ||
497 | { | ||
498 | struct fimc_ctx *ctx = priv; | ||
499 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
500 | struct fimc_frame *frame; | ||
501 | struct v4l2_pix_format_mplane *pix; | ||
502 | int ret; | ||
503 | int i; | ||
504 | |||
505 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | ||
506 | return -EINVAL; | ||
507 | |||
508 | ret = fimc_vidioc_try_fmt_mplane(file, priv, f); | ||
509 | if (ret) | ||
510 | return ret; | ||
511 | |||
512 | if (vb2_is_busy(&fimc->vid_cap.vbq) || fimc_capture_active(fimc)) | ||
513 | return -EBUSY; | ||
514 | |||
515 | frame = &ctx->d_frame; | ||
516 | |||
517 | pix = &f->fmt.pix_mp; | ||
518 | frame->fmt = find_format(f, FMT_FLAGS_M2M | FMT_FLAGS_CAM); | ||
519 | if (!frame->fmt) { | ||
520 | err("fimc target format not found\n"); | ||
521 | return -EINVAL; | ||
522 | } | ||
523 | |||
524 | for (i = 0; i < frame->fmt->colplanes; i++) { | ||
525 | frame->payload[i] = | ||
526 | (pix->width * pix->height * frame->fmt->depth[i]) >> 3; | ||
527 | } | ||
528 | |||
529 | /* Output DMA frame pixel size and offsets. */ | ||
530 | frame->f_width = pix->plane_fmt[0].bytesperline * 8 | ||
531 | / frame->fmt->depth[0]; | ||
532 | frame->f_height = pix->height; | ||
533 | frame->width = pix->width; | ||
534 | frame->height = pix->height; | ||
535 | frame->o_width = pix->width; | ||
536 | frame->o_height = pix->height; | ||
537 | frame->offs_h = 0; | ||
538 | frame->offs_v = 0; | ||
539 | |||
540 | ctx->state |= (FIMC_PARAMS | FIMC_DST_FMT); | ||
541 | |||
542 | ret = sync_capture_fmt(ctx); | ||
543 | return ret; | ||
544 | } | ||
545 | |||
546 | static int fimc_cap_enum_input(struct file *file, void *priv, | ||
547 | struct v4l2_input *i) | ||
548 | { | ||
549 | struct fimc_ctx *ctx = priv; | ||
550 | struct s5p_platform_fimc *pldata = ctx->fimc_dev->pdata; | ||
551 | struct s5p_fimc_isp_info *isp_info; | ||
552 | |||
553 | if (i->index >= pldata->num_clients) | ||
554 | return -EINVAL; | ||
555 | |||
556 | isp_info = &pldata->isp_info[i->index]; | ||
557 | |||
558 | i->type = V4L2_INPUT_TYPE_CAMERA; | ||
559 | strncpy(i->name, isp_info->board_info->type, 32); | ||
560 | return 0; | ||
561 | } | ||
562 | |||
563 | static int fimc_cap_s_input(struct file *file, void *priv, | ||
564 | unsigned int i) | ||
565 | { | ||
566 | struct fimc_ctx *ctx = priv; | ||
567 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
568 | struct s5p_platform_fimc *pdata = fimc->pdata; | ||
569 | |||
570 | if (fimc_capture_active(ctx->fimc_dev)) | ||
571 | return -EBUSY; | ||
572 | |||
573 | if (i >= pdata->num_clients) | ||
574 | return -EINVAL; | ||
575 | |||
576 | |||
577 | if (fimc->vid_cap.sd) { | ||
578 | int ret = v4l2_subdev_call(fimc->vid_cap.sd, core, s_power, 0); | ||
579 | if (ret) | ||
580 | err("s_power failed: %d", ret); | ||
581 | |||
582 | clk_disable(fimc->clock[CLK_CAM]); | ||
583 | } | ||
584 | |||
585 | /* Release the attached sensor subdevice. */ | ||
586 | fimc_subdev_unregister(fimc); | ||
587 | |||
588 | return fimc_isp_subdev_init(fimc, i); | ||
589 | } | ||
590 | |||
591 | static int fimc_cap_g_input(struct file *file, void *priv, | ||
592 | unsigned int *i) | ||
593 | { | ||
594 | struct fimc_ctx *ctx = priv; | ||
595 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; | ||
596 | |||
597 | *i = cap->input_index; | ||
598 | return 0; | ||
599 | } | ||
600 | |||
601 | static int fimc_cap_streamon(struct file *file, void *priv, | ||
602 | enum v4l2_buf_type type) | ||
603 | { | ||
604 | struct fimc_ctx *ctx = priv; | ||
605 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
606 | |||
607 | if (fimc_capture_active(fimc) || !fimc->vid_cap.sd) | ||
608 | return -EBUSY; | ||
609 | |||
610 | if (!(ctx->state & FIMC_DST_FMT)) { | ||
611 | v4l2_err(&fimc->vid_cap.v4l2_dev, "Format is not set\n"); | ||
612 | return -EINVAL; | ||
613 | } | ||
614 | |||
615 | return vb2_streamon(&fimc->vid_cap.vbq, type); | ||
616 | } | ||
617 | |||
618 | static int fimc_cap_streamoff(struct file *file, void *priv, | ||
619 | enum v4l2_buf_type type) | ||
620 | { | ||
621 | struct fimc_ctx *ctx = priv; | ||
622 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
623 | |||
624 | return vb2_streamoff(&fimc->vid_cap.vbq, type); | ||
625 | } | ||
626 | |||
627 | static int fimc_cap_reqbufs(struct file *file, void *priv, | ||
628 | struct v4l2_requestbuffers *reqbufs) | ||
629 | { | ||
630 | struct fimc_ctx *ctx = priv; | ||
631 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; | ||
632 | int ret; | ||
633 | |||
634 | |||
635 | ret = vb2_reqbufs(&cap->vbq, reqbufs); | ||
636 | if (!ret) | ||
637 | cap->reqbufs_count = reqbufs->count; | ||
638 | |||
639 | return ret; | ||
640 | } | ||
641 | |||
642 | static int fimc_cap_querybuf(struct file *file, void *priv, | ||
643 | struct v4l2_buffer *buf) | ||
644 | { | ||
645 | struct fimc_ctx *ctx = priv; | ||
646 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; | ||
647 | |||
648 | return vb2_querybuf(&cap->vbq, buf); | ||
649 | } | ||
650 | |||
651 | static int fimc_cap_qbuf(struct file *file, void *priv, | ||
652 | struct v4l2_buffer *buf) | ||
653 | { | ||
654 | struct fimc_ctx *ctx = priv; | ||
655 | struct fimc_vid_cap *cap = &ctx->fimc_dev->vid_cap; | ||
656 | return vb2_qbuf(&cap->vbq, buf); | ||
657 | } | ||
658 | |||
659 | static int fimc_cap_dqbuf(struct file *file, void *priv, | ||
660 | struct v4l2_buffer *buf) | ||
661 | { | ||
662 | struct fimc_ctx *ctx = priv; | ||
663 | return vb2_dqbuf(&ctx->fimc_dev->vid_cap.vbq, buf, | ||
664 | file->f_flags & O_NONBLOCK); | ||
665 | } | ||
666 | |||
667 | static int fimc_cap_s_ctrl(struct file *file, void *priv, | ||
668 | struct v4l2_control *ctrl) | ||
669 | { | ||
670 | struct fimc_ctx *ctx = priv; | ||
671 | int ret = -EINVAL; | ||
672 | |||
673 | /* Allow any controls but 90/270 rotation while streaming */ | ||
674 | if (!fimc_capture_active(ctx->fimc_dev) || | ||
675 | ctrl->id != V4L2_CID_ROTATE || | ||
676 | (ctrl->value != 90 && ctrl->value != 270)) { | ||
677 | ret = check_ctrl_val(ctx, ctrl); | ||
678 | if (!ret) { | ||
679 | ret = fimc_s_ctrl(ctx, ctrl); | ||
680 | if (!ret) | ||
681 | ctx->state |= FIMC_PARAMS; | ||
682 | } | ||
683 | } | ||
684 | if (ret == -EINVAL) | ||
685 | ret = v4l2_subdev_call(ctx->fimc_dev->vid_cap.sd, | ||
686 | core, s_ctrl, ctrl); | ||
687 | return ret; | ||
688 | } | ||
689 | |||
690 | static int fimc_cap_cropcap(struct file *file, void *fh, | ||
691 | struct v4l2_cropcap *cr) | ||
692 | { | ||
693 | struct fimc_frame *f; | ||
694 | struct fimc_ctx *ctx = fh; | ||
695 | |||
696 | if (cr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | ||
697 | return -EINVAL; | ||
698 | |||
699 | f = &ctx->s_frame; | ||
700 | |||
701 | cr->bounds.left = 0; | ||
702 | cr->bounds.top = 0; | ||
703 | cr->bounds.width = f->o_width; | ||
704 | cr->bounds.height = f->o_height; | ||
705 | cr->defrect = cr->bounds; | ||
706 | |||
707 | return 0; | ||
708 | } | ||
709 | |||
710 | static int fimc_cap_g_crop(struct file *file, void *fh, struct v4l2_crop *cr) | ||
711 | { | ||
712 | struct fimc_frame *f; | ||
713 | struct fimc_ctx *ctx = file->private_data; | ||
714 | |||
715 | f = &ctx->s_frame; | ||
716 | |||
717 | cr->c.left = f->offs_h; | ||
718 | cr->c.top = f->offs_v; | ||
719 | cr->c.width = f->width; | ||
720 | cr->c.height = f->height; | ||
721 | |||
722 | return 0; | ||
723 | } | ||
724 | |||
725 | static int fimc_cap_s_crop(struct file *file, void *fh, | ||
726 | struct v4l2_crop *cr) | ||
727 | { | ||
728 | struct fimc_frame *f; | ||
729 | struct fimc_ctx *ctx = file->private_data; | ||
730 | struct fimc_dev *fimc = ctx->fimc_dev; | ||
731 | int ret = -EINVAL; | ||
732 | |||
733 | if (fimc_capture_active(fimc)) | ||
734 | return -EBUSY; | ||
735 | |||
736 | ret = fimc_try_crop(ctx, cr); | ||
737 | if (ret) | ||
738 | return ret; | ||
739 | |||
740 | if (!(ctx->state & FIMC_DST_FMT)) { | ||
741 | v4l2_err(&fimc->vid_cap.v4l2_dev, | ||
742 | "Capture color format not set\n"); | ||
743 | return -EINVAL; /* TODO: make sure this is the right value */ | ||
744 | } | ||
745 | |||
746 | f = &ctx->s_frame; | ||
747 | /* Check for the pixel scaling ratio when cropping input image. */ | ||
748 | ret = fimc_check_scaler_ratio(cr->c.width, cr->c.height, | ||
749 | ctx->d_frame.width, ctx->d_frame.height, | ||
750 | ctx->rotation); | ||
751 | if (ret) { | ||
752 | v4l2_err(&fimc->vid_cap.v4l2_dev, "Out of the scaler range\n"); | ||
753 | return ret; | ||
754 | } | ||
755 | |||
756 | f->offs_h = cr->c.left; | ||
757 | f->offs_v = cr->c.top; | ||
758 | f->width = cr->c.width; | ||
759 | f->height = cr->c.height; | ||
760 | |||
761 | return 0; | ||
762 | } | ||
763 | |||
764 | |||
765 | static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = { | ||
766 | .vidioc_querycap = fimc_vidioc_querycap_capture, | ||
767 | |||
768 | .vidioc_enum_fmt_vid_cap_mplane = fimc_vidioc_enum_fmt_mplane, | ||
769 | .vidioc_try_fmt_vid_cap_mplane = fimc_vidioc_try_fmt_mplane, | ||
770 | .vidioc_s_fmt_vid_cap_mplane = fimc_cap_s_fmt_mplane, | ||
771 | .vidioc_g_fmt_vid_cap_mplane = fimc_vidioc_g_fmt_mplane, | ||
772 | |||
773 | .vidioc_reqbufs = fimc_cap_reqbufs, | ||
774 | .vidioc_querybuf = fimc_cap_querybuf, | ||
775 | |||
776 | .vidioc_qbuf = fimc_cap_qbuf, | ||
777 | .vidioc_dqbuf = fimc_cap_dqbuf, | ||
778 | |||
779 | .vidioc_streamon = fimc_cap_streamon, | ||
780 | .vidioc_streamoff = fimc_cap_streamoff, | ||
781 | |||
782 | .vidioc_queryctrl = fimc_vidioc_queryctrl, | ||
783 | .vidioc_g_ctrl = fimc_vidioc_g_ctrl, | ||
784 | .vidioc_s_ctrl = fimc_cap_s_ctrl, | ||
785 | |||
786 | .vidioc_g_crop = fimc_cap_g_crop, | ||
787 | .vidioc_s_crop = fimc_cap_s_crop, | ||
788 | .vidioc_cropcap = fimc_cap_cropcap, | ||
789 | |||
790 | .vidioc_enum_input = fimc_cap_enum_input, | ||
791 | .vidioc_s_input = fimc_cap_s_input, | ||
792 | .vidioc_g_input = fimc_cap_g_input, | ||
793 | }; | ||
794 | |||
795 | /* fimc->lock must be already initialized */ | ||
796 | int fimc_register_capture_device(struct fimc_dev *fimc) | ||
797 | { | ||
798 | struct v4l2_device *v4l2_dev = &fimc->vid_cap.v4l2_dev; | ||
799 | struct video_device *vfd; | ||
800 | struct fimc_vid_cap *vid_cap; | ||
801 | struct fimc_ctx *ctx; | ||
802 | struct v4l2_format f; | ||
803 | struct fimc_frame *fr; | ||
804 | struct vb2_queue *q; | ||
805 | int ret; | ||
806 | |||
807 | ctx = kzalloc(sizeof *ctx, GFP_KERNEL); | ||
808 | if (!ctx) | ||
809 | return -ENOMEM; | ||
810 | |||
811 | ctx->fimc_dev = fimc; | ||
812 | ctx->in_path = FIMC_CAMERA; | ||
813 | ctx->out_path = FIMC_DMA; | ||
814 | ctx->state = FIMC_CTX_CAP; | ||
815 | |||
816 | /* Default format of the output frames */ | ||
817 | f.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32; | ||
818 | fr = &ctx->d_frame; | ||
819 | fr->fmt = find_format(&f, FMT_FLAGS_M2M); | ||
820 | fr->width = fr->f_width = fr->o_width = 640; | ||
821 | fr->height = fr->f_height = fr->o_height = 480; | ||
822 | |||
823 | if (!v4l2_dev->name[0]) | ||
824 | snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), | ||
825 | "%s.capture", dev_name(&fimc->pdev->dev)); | ||
826 | |||
827 | ret = v4l2_device_register(NULL, v4l2_dev); | ||
828 | if (ret) | ||
829 | goto err_info; | ||
830 | |||
831 | vfd = video_device_alloc(); | ||
832 | if (!vfd) { | ||
833 | v4l2_err(v4l2_dev, "Failed to allocate video device\n"); | ||
834 | goto err_v4l2_reg; | ||
835 | } | ||
836 | |||
837 | snprintf(vfd->name, sizeof(vfd->name), "%s:cap", | ||
838 | dev_name(&fimc->pdev->dev)); | ||
839 | |||
840 | vfd->fops = &fimc_capture_fops; | ||
841 | vfd->ioctl_ops = &fimc_capture_ioctl_ops; | ||
842 | vfd->minor = -1; | ||
843 | vfd->release = video_device_release; | ||
844 | vfd->lock = &fimc->lock; | ||
845 | video_set_drvdata(vfd, fimc); | ||
846 | |||
847 | vid_cap = &fimc->vid_cap; | ||
848 | vid_cap->vfd = vfd; | ||
849 | vid_cap->active_buf_cnt = 0; | ||
850 | vid_cap->reqbufs_count = 0; | ||
851 | vid_cap->refcnt = 0; | ||
852 | /* Default color format for image sensor */ | ||
853 | vid_cap->fmt.code = V4L2_MBUS_FMT_YUYV8_2X8; | ||
854 | |||
855 | INIT_LIST_HEAD(&vid_cap->pending_buf_q); | ||
856 | INIT_LIST_HEAD(&vid_cap->active_buf_q); | ||
857 | spin_lock_init(&ctx->slock); | ||
858 | vid_cap->ctx = ctx; | ||
859 | |||
860 | q = &fimc->vid_cap.vbq; | ||
861 | memset(q, 0, sizeof(*q)); | ||
862 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | ||
863 | q->io_modes = VB2_MMAP | VB2_USERPTR; | ||
864 | q->drv_priv = fimc->vid_cap.ctx; | ||
865 | q->ops = &fimc_capture_qops; | ||
866 | q->mem_ops = &vb2_dma_contig_memops; | ||
867 | q->buf_struct_size = sizeof(struct fimc_vid_buffer); | ||
868 | |||
869 | vb2_queue_init(q); | ||
870 | |||
871 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1); | ||
872 | if (ret) { | ||
873 | v4l2_err(v4l2_dev, "Failed to register video device\n"); | ||
874 | goto err_vd_reg; | ||
875 | } | ||
876 | |||
877 | v4l2_info(v4l2_dev, | ||
878 | "FIMC capture driver registered as /dev/video%d\n", | ||
879 | vfd->num); | ||
880 | |||
881 | return 0; | ||
882 | |||
883 | err_vd_reg: | ||
884 | video_device_release(vfd); | ||
885 | err_v4l2_reg: | ||
886 | v4l2_device_unregister(v4l2_dev); | ||
887 | err_info: | ||
888 | kfree(ctx); | ||
889 | dev_err(&fimc->pdev->dev, "failed to install\n"); | ||
890 | return ret; | ||
891 | } | ||
892 | |||
893 | void fimc_unregister_capture_device(struct fimc_dev *fimc) | ||
894 | { | ||
895 | struct fimc_vid_cap *capture = &fimc->vid_cap; | ||
896 | |||
897 | if (capture->vfd) | ||
898 | video_unregister_device(capture->vfd); | ||
899 | |||
900 | kfree(capture->ctx); | ||
901 | } | ||