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