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