Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
34947b8a SN |
2 | /* |
3 | * Samsung EXYNOS4x12 FIMC-IS (Imaging Subsystem) driver | |
4 | * | |
5 | * FIMC-IS ISP video input and video output DMA interface driver | |
6 | * | |
7 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. | |
8 | * Author: Sylwester Nawrocki <s.nawrocki@samsung.com> | |
9 | * | |
10 | * The hardware handling code derived from a driver written by | |
11 | * Younghwan Joo <yhwan.joo@samsung.com>. | |
34947b8a SN |
12 | */ |
13 | ||
14 | #include <linux/bitops.h> | |
15 | #include <linux/device.h> | |
16 | #include <linux/delay.h> | |
17 | #include <linux/errno.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/types.h> | |
21 | #include <linux/printk.h> | |
22 | #include <linux/pm_runtime.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/videodev2.h> | |
25 | ||
26 | #include <media/v4l2-device.h> | |
27 | #include <media/v4l2-ioctl.h> | |
c139990e | 28 | #include <media/videobuf2-v4l2.h> |
34947b8a | 29 | #include <media/videobuf2-dma-contig.h> |
d647f0b7 | 30 | #include <media/drv-intf/exynos-fimc.h> |
34947b8a SN |
31 | |
32 | #include "common.h" | |
33 | #include "media-dev.h" | |
34 | #include "fimc-is.h" | |
35 | #include "fimc-isp-video.h" | |
36 | #include "fimc-is-param.h" | |
37 | ||
38 | static int isp_video_capture_queue_setup(struct vb2_queue *vq, | |
34947b8a | 39 | unsigned int *num_buffers, unsigned int *num_planes, |
36c0f8b3 | 40 | unsigned int sizes[], struct device *alloc_devs[]) |
34947b8a SN |
41 | { |
42 | struct fimc_isp *isp = vb2_get_drv_priv(vq); | |
43 | struct v4l2_pix_format_mplane *vid_fmt = &isp->video_capture.pixfmt; | |
df9ecb0c | 44 | const struct fimc_fmt *fmt = isp->video_capture.format; |
34947b8a SN |
45 | unsigned int wh, i; |
46 | ||
df9ecb0c | 47 | wh = vid_fmt->width * vid_fmt->height; |
34947b8a SN |
48 | |
49 | if (fmt == NULL) | |
50 | return -EINVAL; | |
51 | ||
52 | *num_buffers = clamp_t(u32, *num_buffers, FIMC_ISP_REQ_BUFS_MIN, | |
53 | FIMC_ISP_REQ_BUFS_MAX); | |
df9ecb0c HV |
54 | if (*num_planes) { |
55 | if (*num_planes != fmt->memplanes) | |
56 | return -EINVAL; | |
2548fee6 | 57 | for (i = 0; i < *num_planes; i++) |
df9ecb0c HV |
58 | if (sizes[i] < (wh * fmt->depth[i]) / 8) |
59 | return -EINVAL; | |
df9ecb0c HV |
60 | return 0; |
61 | } | |
62 | ||
34947b8a SN |
63 | *num_planes = fmt->memplanes; |
64 | ||
2548fee6 | 65 | for (i = 0; i < fmt->memplanes; i++) |
df9ecb0c | 66 | sizes[i] = (wh * fmt->depth[i]) / 8; |
34947b8a SN |
67 | |
68 | return 0; | |
69 | } | |
70 | ||
71 | static inline struct param_dma_output *__get_isp_dma2(struct fimc_is *is) | |
72 | { | |
73 | return &__get_curr_is_config(is)->isp.dma2_output; | |
74 | } | |
75 | ||
76 | static int isp_video_capture_start_streaming(struct vb2_queue *q, | |
77 | unsigned int count) | |
78 | { | |
79 | struct fimc_isp *isp = vb2_get_drv_priv(q); | |
80 | struct fimc_is *is = fimc_isp_to_is(isp); | |
81 | struct param_dma_output *dma = __get_isp_dma2(is); | |
82 | struct fimc_is_video *video = &isp->video_capture; | |
83 | int ret; | |
84 | ||
85 | if (!test_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state) || | |
86 | test_bit(ST_ISP_VID_CAP_STREAMING, &isp->state)) | |
87 | return 0; | |
88 | ||
89 | ||
90 | dma->cmd = DMA_OUTPUT_COMMAND_ENABLE; | |
91 | dma->notify_dma_done = DMA_OUTPUT_NOTIFY_DMA_DONE_ENABLE; | |
92 | dma->buffer_address = is->is_dma_p_region + | |
93 | DMA2_OUTPUT_ADDR_ARRAY_OFFS; | |
94 | dma->buffer_number = video->reqbufs_count; | |
95 | dma->dma_out_mask = video->buf_mask; | |
96 | ||
97 | isp_dbg(2, &video->ve.vdev, | |
98 | "buf_count: %d, planes: %d, dma addr table: %#x\n", | |
99 | video->buf_count, video->format->memplanes, | |
100 | dma->buffer_address); | |
101 | ||
102 | fimc_is_mem_barrier(); | |
103 | ||
104 | fimc_is_set_param_bit(is, PARAM_ISP_DMA2_OUTPUT); | |
105 | __fimc_is_hw_update_param(is, PARAM_ISP_DMA2_OUTPUT); | |
106 | ||
107 | ret = fimc_is_itf_s_param(is, false); | |
108 | if (ret < 0) | |
109 | return ret; | |
110 | ||
111 | ret = fimc_pipeline_call(&video->ve, set_stream, 1); | |
112 | if (ret < 0) | |
113 | return ret; | |
114 | ||
115 | set_bit(ST_ISP_VID_CAP_STREAMING, &isp->state); | |
116 | return ret; | |
117 | } | |
118 | ||
e37559b2 | 119 | static void isp_video_capture_stop_streaming(struct vb2_queue *q) |
34947b8a SN |
120 | { |
121 | struct fimc_isp *isp = vb2_get_drv_priv(q); | |
122 | struct fimc_is *is = fimc_isp_to_is(isp); | |
123 | struct param_dma_output *dma = __get_isp_dma2(is); | |
124 | int ret; | |
125 | ||
126 | ret = fimc_pipeline_call(&isp->video_capture.ve, set_stream, 0); | |
127 | if (ret < 0) | |
e37559b2 | 128 | return; |
34947b8a SN |
129 | |
130 | dma->cmd = DMA_OUTPUT_COMMAND_DISABLE; | |
131 | dma->notify_dma_done = DMA_OUTPUT_NOTIFY_DMA_DONE_DISABLE; | |
132 | dma->buffer_number = 0; | |
133 | dma->buffer_address = 0; | |
134 | dma->dma_out_mask = 0; | |
135 | ||
136 | fimc_is_set_param_bit(is, PARAM_ISP_DMA2_OUTPUT); | |
137 | __fimc_is_hw_update_param(is, PARAM_ISP_DMA2_OUTPUT); | |
138 | ||
139 | ret = fimc_is_itf_s_param(is, false); | |
140 | if (ret < 0) | |
141 | dev_warn(&is->pdev->dev, "%s: DMA stop failed\n", __func__); | |
142 | ||
143 | fimc_is_hw_set_isp_buf_mask(is, 0); | |
144 | ||
145 | clear_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state); | |
146 | clear_bit(ST_ISP_VID_CAP_STREAMING, &isp->state); | |
147 | ||
148 | isp->video_capture.buf_count = 0; | |
34947b8a SN |
149 | } |
150 | ||
151 | static int isp_video_capture_buffer_prepare(struct vb2_buffer *vb) | |
152 | { | |
153 | struct fimc_isp *isp = vb2_get_drv_priv(vb->vb2_queue); | |
154 | struct fimc_is_video *video = &isp->video_capture; | |
155 | int i; | |
156 | ||
157 | if (video->format == NULL) | |
158 | return -EINVAL; | |
159 | ||
160 | for (i = 0; i < video->format->memplanes; i++) { | |
161 | unsigned long size = video->pixfmt.plane_fmt[i].sizeimage; | |
162 | ||
163 | if (vb2_plane_size(vb, i) < size) { | |
164 | v4l2_err(&video->ve.vdev, | |
165 | "User buffer too small (%ld < %ld)\n", | |
166 | vb2_plane_size(vb, i), size); | |
167 | return -EINVAL; | |
168 | } | |
169 | vb2_set_plane_payload(vb, i, size); | |
170 | } | |
171 | ||
172 | /* Check if we get one of the already known buffers. */ | |
173 | if (test_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state)) { | |
174 | dma_addr_t dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0); | |
175 | int i; | |
176 | ||
177 | for (i = 0; i < video->buf_count; i++) | |
178 | if (video->buffers[i]->dma_addr[0] == dma_addr) | |
179 | return 0; | |
180 | return -ENXIO; | |
181 | } | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
186 | static void isp_video_capture_buffer_queue(struct vb2_buffer *vb) | |
187 | { | |
2d700715 | 188 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
34947b8a SN |
189 | struct fimc_isp *isp = vb2_get_drv_priv(vb->vb2_queue); |
190 | struct fimc_is_video *video = &isp->video_capture; | |
191 | struct fimc_is *is = fimc_isp_to_is(isp); | |
2d700715 | 192 | struct isp_video_buf *ivb = to_isp_video_buf(vbuf); |
34947b8a SN |
193 | unsigned long flags; |
194 | unsigned int i; | |
195 | ||
196 | if (test_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state)) { | |
197 | spin_lock_irqsave(&is->slock, flags); | |
198 | video->buf_mask |= BIT(ivb->index); | |
199 | spin_unlock_irqrestore(&is->slock, flags); | |
200 | } else { | |
201 | unsigned int num_planes = video->format->memplanes; | |
202 | ||
203 | ivb->index = video->buf_count; | |
204 | video->buffers[ivb->index] = ivb; | |
205 | ||
206 | for (i = 0; i < num_planes; i++) { | |
207 | int buf_index = ivb->index * num_planes + i; | |
208 | ||
209 | ivb->dma_addr[i] = vb2_dma_contig_plane_dma_addr(vb, i); | |
210 | is->is_p_region->shared[32 + buf_index] = | |
211 | ivb->dma_addr[i]; | |
212 | ||
213 | isp_dbg(2, &video->ve.vdev, | |
76a56367 RV |
214 | "dma_buf %d (%d/%d/%d) addr: %pad\n", |
215 | buf_index, ivb->index, i, vb->index, | |
7d4020c3 | 216 | &ivb->dma_addr[i]); |
34947b8a SN |
217 | } |
218 | ||
219 | if (++video->buf_count < video->reqbufs_count) | |
220 | return; | |
221 | ||
222 | video->buf_mask = (1UL << video->buf_count) - 1; | |
223 | set_bit(ST_ISP_VID_CAP_BUF_PREP, &isp->state); | |
224 | } | |
225 | ||
226 | if (!test_bit(ST_ISP_VID_CAP_STREAMING, &isp->state)) | |
227 | isp_video_capture_start_streaming(vb->vb2_queue, 0); | |
228 | } | |
229 | ||
230 | /* | |
231 | * FIMC-IS ISP input and output DMA interface interrupt handler. | |
232 | * Locking: called with is->slock spinlock held. | |
233 | */ | |
234 | void fimc_isp_video_irq_handler(struct fimc_is *is) | |
235 | { | |
236 | struct fimc_is_video *video = &is->isp.video_capture; | |
2d700715 | 237 | struct vb2_v4l2_buffer *vbuf; |
34947b8a SN |
238 | int buf_index; |
239 | ||
240 | /* TODO: Ensure the DMA is really stopped in stop_streaming callback */ | |
241 | if (!test_bit(ST_ISP_VID_CAP_STREAMING, &is->isp.state)) | |
242 | return; | |
243 | ||
244 | buf_index = (is->i2h_cmd.args[1] - 1) % video->buf_count; | |
2d700715 | 245 | vbuf = &video->buffers[buf_index]->vb; |
34947b8a | 246 | |
d6dd645e | 247 | vbuf->vb2_buf.timestamp = ktime_get_ns(); |
2d700715 | 248 | vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE); |
34947b8a SN |
249 | |
250 | video->buf_mask &= ~BIT(buf_index); | |
251 | fimc_is_hw_set_isp_buf_mask(is, video->buf_mask); | |
252 | } | |
253 | ||
254 | static const struct vb2_ops isp_video_capture_qops = { | |
255 | .queue_setup = isp_video_capture_queue_setup, | |
256 | .buf_prepare = isp_video_capture_buffer_prepare, | |
257 | .buf_queue = isp_video_capture_buffer_queue, | |
258 | .wait_prepare = vb2_ops_wait_prepare, | |
259 | .wait_finish = vb2_ops_wait_finish, | |
260 | .start_streaming = isp_video_capture_start_streaming, | |
261 | .stop_streaming = isp_video_capture_stop_streaming, | |
262 | }; | |
263 | ||
264 | static int isp_video_open(struct file *file) | |
265 | { | |
266 | struct fimc_isp *isp = video_drvdata(file); | |
267 | struct exynos_video_entity *ve = &isp->video_capture.ve; | |
268 | struct media_entity *me = &ve->vdev.entity; | |
269 | int ret; | |
270 | ||
271 | if (mutex_lock_interruptible(&isp->video_lock)) | |
272 | return -ERESTARTSYS; | |
273 | ||
274 | ret = v4l2_fh_open(file); | |
275 | if (ret < 0) | |
276 | goto unlock; | |
277 | ||
278 | ret = pm_runtime_get_sync(&isp->pdev->dev); | |
279 | if (ret < 0) | |
280 | goto rel_fh; | |
281 | ||
282 | if (v4l2_fh_is_singular_file(file)) { | |
d10c9894 | 283 | mutex_lock(&me->graph_obj.mdev->graph_mutex); |
34947b8a SN |
284 | |
285 | ret = fimc_pipeline_call(ve, open, me, true); | |
286 | ||
287 | /* Mark the video pipeline as in use. */ | |
288 | if (ret == 0) | |
289 | me->use_count++; | |
290 | ||
d10c9894 | 291 | mutex_unlock(&me->graph_obj.mdev->graph_mutex); |
34947b8a SN |
292 | } |
293 | if (!ret) | |
294 | goto unlock; | |
295 | rel_fh: | |
296 | v4l2_fh_release(file); | |
297 | unlock: | |
298 | mutex_unlock(&isp->video_lock); | |
299 | return ret; | |
300 | } | |
301 | ||
302 | static int isp_video_release(struct file *file) | |
303 | { | |
304 | struct fimc_isp *isp = video_drvdata(file); | |
305 | struct fimc_is_video *ivc = &isp->video_capture; | |
306 | struct media_entity *entity = &ivc->ve.vdev.entity; | |
d10c9894 | 307 | struct media_device *mdev = entity->graph_obj.mdev; |
34947b8a SN |
308 | |
309 | mutex_lock(&isp->video_lock); | |
310 | ||
311 | if (v4l2_fh_is_singular_file(file) && ivc->streaming) { | |
20b85227 | 312 | media_pipeline_stop(entity); |
34947b8a SN |
313 | ivc->streaming = 0; |
314 | } | |
315 | ||
316 | vb2_fop_release(file); | |
317 | ||
318 | if (v4l2_fh_is_singular_file(file)) { | |
319 | fimc_pipeline_call(&ivc->ve, close); | |
320 | ||
321 | mutex_lock(&mdev->graph_mutex); | |
322 | entity->use_count--; | |
323 | mutex_unlock(&mdev->graph_mutex); | |
324 | } | |
325 | ||
326 | pm_runtime_put(&isp->pdev->dev); | |
327 | mutex_unlock(&isp->video_lock); | |
328 | ||
6cf77d18 | 329 | return 0; |
34947b8a SN |
330 | } |
331 | ||
332 | static const struct v4l2_file_operations isp_video_fops = { | |
333 | .owner = THIS_MODULE, | |
334 | .open = isp_video_open, | |
335 | .release = isp_video_release, | |
336 | .poll = vb2_fop_poll, | |
337 | .unlocked_ioctl = video_ioctl2, | |
338 | .mmap = vb2_fop_mmap, | |
339 | }; | |
340 | ||
341 | /* | |
342 | * Video node ioctl operations | |
343 | */ | |
344 | static int isp_video_querycap(struct file *file, void *priv, | |
345 | struct v4l2_capability *cap) | |
346 | { | |
347 | struct fimc_isp *isp = video_drvdata(file); | |
348 | ||
349 | __fimc_vidioc_querycap(&isp->pdev->dev, cap, V4L2_CAP_STREAMING); | |
350 | return 0; | |
351 | } | |
352 | ||
353 | static int isp_video_enum_fmt_mplane(struct file *file, void *priv, | |
354 | struct v4l2_fmtdesc *f) | |
355 | { | |
356 | const struct fimc_fmt *fmt; | |
357 | ||
358 | if (f->index >= FIMC_ISP_NUM_FORMATS) | |
359 | return -EINVAL; | |
360 | ||
361 | fmt = fimc_isp_find_format(NULL, NULL, f->index); | |
362 | if (WARN_ON(fmt == NULL)) | |
363 | return -EINVAL; | |
364 | ||
c0decac1 | 365 | strscpy(f->description, fmt->name, sizeof(f->description)); |
34947b8a SN |
366 | f->pixelformat = fmt->fourcc; |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
371 | static int isp_video_g_fmt_mplane(struct file *file, void *fh, | |
372 | struct v4l2_format *f) | |
373 | { | |
374 | struct fimc_isp *isp = video_drvdata(file); | |
375 | ||
376 | f->fmt.pix_mp = isp->video_capture.pixfmt; | |
377 | return 0; | |
378 | } | |
379 | ||
380 | static void __isp_video_try_fmt(struct fimc_isp *isp, | |
381 | struct v4l2_pix_format_mplane *pixm, | |
382 | const struct fimc_fmt **fmt) | |
383 | { | |
7c1b9a5a SN |
384 | const struct fimc_fmt *__fmt; |
385 | ||
386 | __fmt = fimc_isp_find_format(&pixm->pixelformat, NULL, 2); | |
387 | ||
388 | if (fmt) | |
389 | *fmt = __fmt; | |
34947b8a SN |
390 | |
391 | pixm->colorspace = V4L2_COLORSPACE_SRGB; | |
392 | pixm->field = V4L2_FIELD_NONE; | |
7c1b9a5a SN |
393 | pixm->num_planes = __fmt->memplanes; |
394 | pixm->pixelformat = __fmt->fourcc; | |
34947b8a SN |
395 | /* |
396 | * TODO: double check with the docmentation these width/height | |
397 | * constraints are correct. | |
398 | */ | |
399 | v4l_bound_align_image(&pixm->width, FIMC_ISP_SOURCE_WIDTH_MIN, | |
400 | FIMC_ISP_SOURCE_WIDTH_MAX, 3, | |
401 | &pixm->height, FIMC_ISP_SOURCE_HEIGHT_MIN, | |
402 | FIMC_ISP_SOURCE_HEIGHT_MAX, 0, 0); | |
403 | } | |
404 | ||
405 | static int isp_video_try_fmt_mplane(struct file *file, void *fh, | |
406 | struct v4l2_format *f) | |
407 | { | |
408 | struct fimc_isp *isp = video_drvdata(file); | |
409 | ||
410 | __isp_video_try_fmt(isp, &f->fmt.pix_mp, NULL); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | static int isp_video_s_fmt_mplane(struct file *file, void *priv, | |
415 | struct v4l2_format *f) | |
416 | { | |
417 | struct fimc_isp *isp = video_drvdata(file); | |
418 | struct fimc_is *is = fimc_isp_to_is(isp); | |
419 | struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp; | |
420 | const struct fimc_fmt *ifmt = NULL; | |
421 | struct param_dma_output *dma = __get_isp_dma2(is); | |
422 | ||
423 | __isp_video_try_fmt(isp, pixm, &ifmt); | |
424 | ||
425 | if (WARN_ON(ifmt == NULL)) | |
426 | return -EINVAL; | |
427 | ||
428 | dma->format = DMA_OUTPUT_FORMAT_BAYER; | |
429 | dma->order = DMA_OUTPUT_ORDER_GB_BG; | |
430 | dma->plane = ifmt->memplanes; | |
431 | dma->bitwidth = ifmt->depth[0]; | |
432 | dma->width = pixm->width; | |
433 | dma->height = pixm->height; | |
434 | ||
435 | fimc_is_mem_barrier(); | |
436 | ||
437 | isp->video_capture.format = ifmt; | |
438 | isp->video_capture.pixfmt = *pixm; | |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
443 | /* | |
444 | * Check for source/sink format differences at each link. | |
445 | * Return 0 if the formats match or -EPIPE otherwise. | |
446 | */ | |
447 | static int isp_video_pipeline_validate(struct fimc_isp *isp) | |
448 | { | |
449 | struct v4l2_subdev *sd = &isp->subdev; | |
450 | struct v4l2_subdev_format sink_fmt, src_fmt; | |
451 | struct media_pad *pad; | |
452 | int ret; | |
453 | ||
454 | while (1) { | |
455 | /* Retrieve format at the sink pad */ | |
456 | pad = &sd->entity.pads[0]; | |
457 | if (!(pad->flags & MEDIA_PAD_FL_SINK)) | |
458 | break; | |
459 | sink_fmt.pad = pad->index; | |
460 | sink_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
461 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sink_fmt); | |
462 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
463 | return -EPIPE; | |
464 | ||
465 | /* Retrieve format at the source pad */ | |
466 | pad = media_entity_remote_pad(pad); | |
3efdf62c | 467 | if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) |
34947b8a SN |
468 | break; |
469 | ||
470 | sd = media_entity_to_v4l2_subdev(pad->entity); | |
471 | src_fmt.pad = pad->index; | |
472 | src_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
473 | ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &src_fmt); | |
474 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
475 | return -EPIPE; | |
476 | ||
477 | if (src_fmt.format.width != sink_fmt.format.width || | |
478 | src_fmt.format.height != sink_fmt.format.height || | |
479 | src_fmt.format.code != sink_fmt.format.code) | |
480 | return -EPIPE; | |
481 | } | |
482 | ||
483 | return 0; | |
484 | } | |
485 | ||
486 | static int isp_video_streamon(struct file *file, void *priv, | |
487 | enum v4l2_buf_type type) | |
488 | { | |
489 | struct fimc_isp *isp = video_drvdata(file); | |
490 | struct exynos_video_entity *ve = &isp->video_capture.ve; | |
491 | struct media_entity *me = &ve->vdev.entity; | |
492 | int ret; | |
493 | ||
20b85227 | 494 | ret = media_pipeline_start(me, &ve->pipe->mp); |
34947b8a SN |
495 | if (ret < 0) |
496 | return ret; | |
497 | ||
498 | ret = isp_video_pipeline_validate(isp); | |
499 | if (ret < 0) | |
500 | goto p_stop; | |
501 | ||
502 | ret = vb2_ioctl_streamon(file, priv, type); | |
503 | if (ret < 0) | |
504 | goto p_stop; | |
505 | ||
506 | isp->video_capture.streaming = 1; | |
507 | return 0; | |
508 | p_stop: | |
20b85227 | 509 | media_pipeline_stop(me); |
34947b8a SN |
510 | return ret; |
511 | } | |
512 | ||
513 | static int isp_video_streamoff(struct file *file, void *priv, | |
514 | enum v4l2_buf_type type) | |
515 | { | |
516 | struct fimc_isp *isp = video_drvdata(file); | |
517 | struct fimc_is_video *video = &isp->video_capture; | |
518 | int ret; | |
519 | ||
520 | ret = vb2_ioctl_streamoff(file, priv, type); | |
521 | if (ret < 0) | |
522 | return ret; | |
523 | ||
20b85227 | 524 | media_pipeline_stop(&video->ve.vdev.entity); |
34947b8a SN |
525 | video->streaming = 0; |
526 | return 0; | |
527 | } | |
528 | ||
529 | static int isp_video_reqbufs(struct file *file, void *priv, | |
530 | struct v4l2_requestbuffers *rb) | |
531 | { | |
532 | struct fimc_isp *isp = video_drvdata(file); | |
533 | int ret; | |
534 | ||
535 | ret = vb2_ioctl_reqbufs(file, priv, rb); | |
536 | if (ret < 0) | |
537 | return ret; | |
538 | ||
539 | if (rb->count && rb->count < FIMC_ISP_REQ_BUFS_MIN) { | |
540 | rb->count = 0; | |
541 | vb2_ioctl_reqbufs(file, priv, rb); | |
542 | ret = -ENOMEM; | |
543 | } | |
544 | ||
545 | isp->video_capture.reqbufs_count = rb->count; | |
546 | return ret; | |
547 | } | |
548 | ||
549 | static const struct v4l2_ioctl_ops isp_video_ioctl_ops = { | |
550 | .vidioc_querycap = isp_video_querycap, | |
551 | .vidioc_enum_fmt_vid_cap_mplane = isp_video_enum_fmt_mplane, | |
552 | .vidioc_try_fmt_vid_cap_mplane = isp_video_try_fmt_mplane, | |
553 | .vidioc_s_fmt_vid_cap_mplane = isp_video_s_fmt_mplane, | |
554 | .vidioc_g_fmt_vid_cap_mplane = isp_video_g_fmt_mplane, | |
555 | .vidioc_reqbufs = isp_video_reqbufs, | |
556 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
557 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
558 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
559 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
560 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
561 | .vidioc_streamon = isp_video_streamon, | |
562 | .vidioc_streamoff = isp_video_streamoff, | |
563 | }; | |
564 | ||
565 | int fimc_isp_video_device_register(struct fimc_isp *isp, | |
566 | struct v4l2_device *v4l2_dev, | |
567 | enum v4l2_buf_type type) | |
568 | { | |
569 | struct vb2_queue *q = &isp->video_capture.vb_queue; | |
570 | struct fimc_is_video *iv; | |
571 | struct video_device *vdev; | |
572 | int ret; | |
573 | ||
574 | if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | |
575 | iv = &isp->video_capture; | |
576 | else | |
577 | return -ENOSYS; | |
578 | ||
579 | mutex_init(&isp->video_lock); | |
580 | INIT_LIST_HEAD(&iv->pending_buf_q); | |
581 | INIT_LIST_HEAD(&iv->active_buf_q); | |
582 | iv->format = fimc_isp_find_format(NULL, NULL, 0); | |
583 | iv->pixfmt.width = IS_DEFAULT_WIDTH; | |
584 | iv->pixfmt.height = IS_DEFAULT_HEIGHT; | |
585 | iv->pixfmt.pixelformat = iv->format->fourcc; | |
586 | iv->pixfmt.colorspace = V4L2_COLORSPACE_SRGB; | |
587 | iv->reqbufs_count = 0; | |
588 | ||
589 | memset(q, 0, sizeof(*q)); | |
590 | q->type = type; | |
591 | q->io_modes = VB2_MMAP | VB2_USERPTR; | |
592 | q->ops = &isp_video_capture_qops; | |
593 | q->mem_ops = &vb2_dma_contig_memops; | |
594 | q->buf_struct_size = sizeof(struct isp_video_buf); | |
595 | q->drv_priv = isp; | |
596 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
597 | q->lock = &isp->video_lock; | |
2548fee6 | 598 | q->dev = &isp->pdev->dev; |
34947b8a SN |
599 | |
600 | ret = vb2_queue_init(q); | |
601 | if (ret < 0) | |
602 | return ret; | |
603 | ||
604 | vdev = &iv->ve.vdev; | |
605 | memset(vdev, 0, sizeof(*vdev)); | |
ffaec3b2 | 606 | strscpy(vdev->name, "fimc-is-isp.capture", sizeof(vdev->name)); |
34947b8a SN |
607 | vdev->queue = q; |
608 | vdev->fops = &isp_video_fops; | |
609 | vdev->ioctl_ops = &isp_video_ioctl_ops; | |
610 | vdev->v4l2_dev = v4l2_dev; | |
611 | vdev->minor = -1; | |
612 | vdev->release = video_device_release_empty; | |
613 | vdev->lock = &isp->video_lock; | |
614 | ||
615 | iv->pad.flags = MEDIA_PAD_FL_SINK; | |
ab22e77c | 616 | ret = media_entity_pads_init(&vdev->entity, 1, &iv->pad); |
34947b8a SN |
617 | if (ret < 0) |
618 | return ret; | |
619 | ||
620 | video_set_drvdata(vdev, isp); | |
621 | ||
622 | ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); | |
623 | if (ret < 0) { | |
624 | media_entity_cleanup(&vdev->entity); | |
625 | return ret; | |
626 | } | |
627 | ||
628 | v4l2_info(v4l2_dev, "Registered %s as /dev/%s\n", | |
629 | vdev->name, video_device_node_name(vdev)); | |
630 | ||
631 | return 0; | |
632 | } | |
633 | ||
634 | void fimc_isp_video_device_unregister(struct fimc_isp *isp, | |
635 | enum v4l2_buf_type type) | |
636 | { | |
637 | struct exynos_video_entity *ve; | |
638 | ||
639 | if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | |
640 | ve = &isp->video_capture.ve; | |
641 | else | |
642 | return; | |
643 | ||
644 | mutex_lock(&isp->video_lock); | |
645 | ||
646 | if (video_is_registered(&ve->vdev)) { | |
647 | video_unregister_device(&ve->vdev); | |
648 | media_entity_cleanup(&ve->vdev.entity); | |
649 | ve->pipe = NULL; | |
650 | } | |
651 | ||
652 | mutex_unlock(&isp->video_lock); | |
653 | } |