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