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