1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2021-2022 Bootlin
4 * Author: Paul Kocialkowski <[email protected]>
5 */
6
7 #include <media/v4l2-device.h>
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-ioctl.h>
10 #include <media/v4l2-mc.h>
11 #include <media/videobuf2-dma-contig.h>
12 #include <media/videobuf2-v4l2.h>
13
14 #include "sun6i_isp.h"
15 #include "sun6i_isp_capture.h"
16 #include "sun6i_isp_proc.h"
17 #include "sun6i_isp_reg.h"
18
19 /* Helpers */
20
sun6i_isp_capture_dimensions(struct sun6i_isp_device * isp_dev,unsigned int * width,unsigned int * height)21 void sun6i_isp_capture_dimensions(struct sun6i_isp_device *isp_dev,
22 unsigned int *width, unsigned int *height)
23 {
24 if (width)
25 *width = isp_dev->capture.format.fmt.pix.width;
26 if (height)
27 *height = isp_dev->capture.format.fmt.pix.height;
28 }
29
sun6i_isp_capture_format(struct sun6i_isp_device * isp_dev,u32 * pixelformat)30 void sun6i_isp_capture_format(struct sun6i_isp_device *isp_dev,
31 u32 *pixelformat)
32 {
33 if (pixelformat)
34 *pixelformat = isp_dev->capture.format.fmt.pix.pixelformat;
35 }
36
37 /* Format */
38
39 static const struct sun6i_isp_capture_format sun6i_isp_capture_formats[] = {
40 {
41 .pixelformat = V4L2_PIX_FMT_NV12,
42 .output_format = SUN6I_ISP_OUTPUT_FMT_YUV420SP,
43 },
44 {
45 .pixelformat = V4L2_PIX_FMT_NV21,
46 .output_format = SUN6I_ISP_OUTPUT_FMT_YVU420SP,
47 },
48 };
49
50 const struct sun6i_isp_capture_format *
sun6i_isp_capture_format_find(u32 pixelformat)51 sun6i_isp_capture_format_find(u32 pixelformat)
52 {
53 unsigned int i;
54
55 for (i = 0; i < ARRAY_SIZE(sun6i_isp_capture_formats); i++)
56 if (sun6i_isp_capture_formats[i].pixelformat == pixelformat)
57 return &sun6i_isp_capture_formats[i];
58
59 return NULL;
60 }
61
62 /* Capture */
63
64 static void
sun6i_isp_capture_buffer_configure(struct sun6i_isp_device * isp_dev,struct sun6i_isp_buffer * isp_buffer)65 sun6i_isp_capture_buffer_configure(struct sun6i_isp_device *isp_dev,
66 struct sun6i_isp_buffer *isp_buffer)
67 {
68 const struct v4l2_format_info *info;
69 struct vb2_buffer *vb2_buffer;
70 unsigned int width, height;
71 unsigned int width_aligned;
72 dma_addr_t address;
73 u32 pixelformat;
74
75 vb2_buffer = &isp_buffer->v4l2_buffer.vb2_buf;
76 address = vb2_dma_contig_plane_dma_addr(vb2_buffer, 0);
77
78 sun6i_isp_load_write(isp_dev, SUN6I_ISP_MCH_Y_ADDR0_REG,
79 SUN6I_ISP_ADDR_VALUE(address));
80
81 sun6i_isp_capture_dimensions(isp_dev, &width, &height);
82 sun6i_isp_capture_format(isp_dev, &pixelformat);
83
84 info = v4l2_format_info(pixelformat);
85 if (WARN_ON(!info))
86 return;
87
88 /* Stride needs to be aligned to 4. */
89 width_aligned = ALIGN(width, 2);
90
91 if (info->comp_planes > 1) {
92 address += info->bpp[0] * width_aligned * height;
93
94 sun6i_isp_load_write(isp_dev, SUN6I_ISP_MCH_U_ADDR0_REG,
95 SUN6I_ISP_ADDR_VALUE(address));
96 }
97
98 if (info->comp_planes > 2) {
99 address += info->bpp[1] *
100 DIV_ROUND_UP(width_aligned, info->hdiv) *
101 DIV_ROUND_UP(height, info->vdiv);
102
103 sun6i_isp_load_write(isp_dev, SUN6I_ISP_MCH_V_ADDR0_REG,
104 SUN6I_ISP_ADDR_VALUE(address));
105 }
106 }
107
sun6i_isp_capture_configure(struct sun6i_isp_device * isp_dev)108 void sun6i_isp_capture_configure(struct sun6i_isp_device *isp_dev)
109 {
110 unsigned int width, height;
111 unsigned int stride_luma, stride_chroma;
112 unsigned int stride_luma_div4, stride_chroma_div4 = 0;
113 const struct sun6i_isp_capture_format *format;
114 const struct v4l2_format_info *info;
115 u32 pixelformat;
116
117 sun6i_isp_capture_dimensions(isp_dev, &width, &height);
118 sun6i_isp_capture_format(isp_dev, &pixelformat);
119
120 format = sun6i_isp_capture_format_find(pixelformat);
121 if (WARN_ON(!format))
122 return;
123
124 sun6i_isp_load_write(isp_dev, SUN6I_ISP_MCH_SIZE_CFG_REG,
125 SUN6I_ISP_MCH_SIZE_CFG_WIDTH(width) |
126 SUN6I_ISP_MCH_SIZE_CFG_HEIGHT(height));
127
128 info = v4l2_format_info(pixelformat);
129 if (WARN_ON(!info))
130 return;
131
132 stride_luma = width * info->bpp[0];
133 stride_luma_div4 = DIV_ROUND_UP(stride_luma, 4);
134
135 if (info->comp_planes > 1) {
136 stride_chroma = width * info->bpp[1] / info->hdiv;
137 stride_chroma_div4 = DIV_ROUND_UP(stride_chroma, 4);
138 }
139
140 sun6i_isp_load_write(isp_dev, SUN6I_ISP_MCH_CFG_REG,
141 SUN6I_ISP_MCH_CFG_EN |
142 SUN6I_ISP_MCH_CFG_OUTPUT_FMT(format->output_format) |
143 SUN6I_ISP_MCH_CFG_STRIDE_Y_DIV4(stride_luma_div4) |
144 SUN6I_ISP_MCH_CFG_STRIDE_UV_DIV4(stride_chroma_div4));
145 }
146
147 /* State */
148
sun6i_isp_capture_state_cleanup(struct sun6i_isp_device * isp_dev,bool error)149 static void sun6i_isp_capture_state_cleanup(struct sun6i_isp_device *isp_dev,
150 bool error)
151 {
152 struct sun6i_isp_capture_state *state = &isp_dev->capture.state;
153 struct sun6i_isp_buffer **isp_buffer_states[] = {
154 &state->pending, &state->current, &state->complete,
155 };
156 struct sun6i_isp_buffer *isp_buffer;
157 struct vb2_buffer *vb2_buffer;
158 unsigned long flags;
159 unsigned int i;
160
161 spin_lock_irqsave(&state->lock, flags);
162
163 for (i = 0; i < ARRAY_SIZE(isp_buffer_states); i++) {
164 isp_buffer = *isp_buffer_states[i];
165 if (!isp_buffer)
166 continue;
167
168 vb2_buffer = &isp_buffer->v4l2_buffer.vb2_buf;
169 vb2_buffer_done(vb2_buffer, error ? VB2_BUF_STATE_ERROR :
170 VB2_BUF_STATE_QUEUED);
171
172 *isp_buffer_states[i] = NULL;
173 }
174
175 list_for_each_entry(isp_buffer, &state->queue, list) {
176 vb2_buffer = &isp_buffer->v4l2_buffer.vb2_buf;
177 vb2_buffer_done(vb2_buffer, error ? VB2_BUF_STATE_ERROR :
178 VB2_BUF_STATE_QUEUED);
179 }
180
181 INIT_LIST_HEAD(&state->queue);
182
183 spin_unlock_irqrestore(&state->lock, flags);
184 }
185
sun6i_isp_capture_state_update(struct sun6i_isp_device * isp_dev,bool * update)186 void sun6i_isp_capture_state_update(struct sun6i_isp_device *isp_dev,
187 bool *update)
188 {
189 struct sun6i_isp_capture_state *state = &isp_dev->capture.state;
190 struct sun6i_isp_buffer *isp_buffer;
191 unsigned long flags;
192
193 spin_lock_irqsave(&state->lock, flags);
194
195 if (list_empty(&state->queue))
196 goto complete;
197
198 if (state->pending)
199 goto complete;
200
201 isp_buffer = list_first_entry(&state->queue, struct sun6i_isp_buffer,
202 list);
203
204 sun6i_isp_capture_buffer_configure(isp_dev, isp_buffer);
205
206 list_del(&isp_buffer->list);
207
208 state->pending = isp_buffer;
209
210 if (update)
211 *update = true;
212
213 complete:
214 spin_unlock_irqrestore(&state->lock, flags);
215 }
216
sun6i_isp_capture_state_complete(struct sun6i_isp_device * isp_dev)217 void sun6i_isp_capture_state_complete(struct sun6i_isp_device *isp_dev)
218 {
219 struct sun6i_isp_capture_state *state = &isp_dev->capture.state;
220 unsigned long flags;
221
222 spin_lock_irqsave(&state->lock, flags);
223
224 if (!state->pending)
225 goto complete;
226
227 state->complete = state->current;
228 state->current = state->pending;
229 state->pending = NULL;
230
231 if (state->complete) {
232 struct sun6i_isp_buffer *isp_buffer = state->complete;
233 struct vb2_buffer *vb2_buffer =
234 &isp_buffer->v4l2_buffer.vb2_buf;
235
236 vb2_buffer->timestamp = ktime_get_ns();
237 isp_buffer->v4l2_buffer.sequence = state->sequence;
238
239 vb2_buffer_done(vb2_buffer, VB2_BUF_STATE_DONE);
240
241 state->complete = NULL;
242 }
243
244 complete:
245 spin_unlock_irqrestore(&state->lock, flags);
246 }
247
sun6i_isp_capture_finish(struct sun6i_isp_device * isp_dev)248 void sun6i_isp_capture_finish(struct sun6i_isp_device *isp_dev)
249 {
250 struct sun6i_isp_capture_state *state = &isp_dev->capture.state;
251 unsigned long flags;
252
253 spin_lock_irqsave(&state->lock, flags);
254 state->sequence++;
255 spin_unlock_irqrestore(&state->lock, flags);
256 }
257
258 /* Queue */
259
sun6i_isp_capture_queue_setup(struct vb2_queue * queue,unsigned int * buffers_count,unsigned int * planes_count,unsigned int sizes[],struct device * alloc_devs[])260 static int sun6i_isp_capture_queue_setup(struct vb2_queue *queue,
261 unsigned int *buffers_count,
262 unsigned int *planes_count,
263 unsigned int sizes[],
264 struct device *alloc_devs[])
265 {
266 struct sun6i_isp_device *isp_dev = vb2_get_drv_priv(queue);
267 unsigned int size = isp_dev->capture.format.fmt.pix.sizeimage;
268
269 if (*planes_count)
270 return sizes[0] < size ? -EINVAL : 0;
271
272 *planes_count = 1;
273 sizes[0] = size;
274
275 return 0;
276 }
277
sun6i_isp_capture_buffer_prepare(struct vb2_buffer * vb2_buffer)278 static int sun6i_isp_capture_buffer_prepare(struct vb2_buffer *vb2_buffer)
279 {
280 struct sun6i_isp_device *isp_dev =
281 vb2_get_drv_priv(vb2_buffer->vb2_queue);
282 struct v4l2_device *v4l2_dev = &isp_dev->v4l2.v4l2_dev;
283 unsigned int size = isp_dev->capture.format.fmt.pix.sizeimage;
284
285 if (vb2_plane_size(vb2_buffer, 0) < size) {
286 v4l2_err(v4l2_dev, "buffer too small (%lu < %u)\n",
287 vb2_plane_size(vb2_buffer, 0), size);
288 return -EINVAL;
289 }
290
291 vb2_set_plane_payload(vb2_buffer, 0, size);
292
293 return 0;
294 }
295
sun6i_isp_capture_buffer_queue(struct vb2_buffer * vb2_buffer)296 static void sun6i_isp_capture_buffer_queue(struct vb2_buffer *vb2_buffer)
297 {
298 struct sun6i_isp_device *isp_dev =
299 vb2_get_drv_priv(vb2_buffer->vb2_queue);
300 struct sun6i_isp_capture_state *state = &isp_dev->capture.state;
301 struct vb2_v4l2_buffer *v4l2_buffer = to_vb2_v4l2_buffer(vb2_buffer);
302 struct sun6i_isp_buffer *isp_buffer =
303 container_of(v4l2_buffer, struct sun6i_isp_buffer, v4l2_buffer);
304 unsigned long flags;
305
306 spin_lock_irqsave(&state->lock, flags);
307 list_add_tail(&isp_buffer->list, &state->queue);
308 spin_unlock_irqrestore(&state->lock, flags);
309
310 /* Update the state to schedule our buffer as soon as possible. */
311 if (state->streaming)
312 sun6i_isp_state_update(isp_dev, false);
313 }
314
sun6i_isp_capture_start_streaming(struct vb2_queue * queue,unsigned int count)315 static int sun6i_isp_capture_start_streaming(struct vb2_queue *queue,
316 unsigned int count)
317 {
318 struct sun6i_isp_device *isp_dev = vb2_get_drv_priv(queue);
319 struct sun6i_isp_capture_state *state = &isp_dev->capture.state;
320 struct video_device *video_dev = &isp_dev->capture.video_dev;
321 struct v4l2_subdev *subdev = &isp_dev->proc.subdev;
322 int ret;
323
324 state->sequence = 0;
325
326 ret = video_device_pipeline_alloc_start(video_dev);
327 if (ret < 0)
328 goto error_state;
329
330 state->streaming = true;
331
332 ret = v4l2_subdev_call(subdev, video, s_stream, 1);
333 if (ret && ret != -ENOIOCTLCMD)
334 goto error_streaming;
335
336 return 0;
337
338 error_streaming:
339 state->streaming = false;
340
341 video_device_pipeline_stop(video_dev);
342
343 error_state:
344 sun6i_isp_capture_state_cleanup(isp_dev, false);
345
346 return ret;
347 }
348
sun6i_isp_capture_stop_streaming(struct vb2_queue * queue)349 static void sun6i_isp_capture_stop_streaming(struct vb2_queue *queue)
350 {
351 struct sun6i_isp_device *isp_dev = vb2_get_drv_priv(queue);
352 struct sun6i_isp_capture_state *state = &isp_dev->capture.state;
353 struct video_device *video_dev = &isp_dev->capture.video_dev;
354 struct v4l2_subdev *subdev = &isp_dev->proc.subdev;
355
356 v4l2_subdev_call(subdev, video, s_stream, 0);
357
358 state->streaming = false;
359
360 video_device_pipeline_stop(video_dev);
361
362 sun6i_isp_capture_state_cleanup(isp_dev, true);
363 }
364
365 static const struct vb2_ops sun6i_isp_capture_queue_ops = {
366 .queue_setup = sun6i_isp_capture_queue_setup,
367 .buf_prepare = sun6i_isp_capture_buffer_prepare,
368 .buf_queue = sun6i_isp_capture_buffer_queue,
369 .start_streaming = sun6i_isp_capture_start_streaming,
370 .stop_streaming = sun6i_isp_capture_stop_streaming,
371 };
372
373 /* Video Device */
374
sun6i_isp_capture_format_prepare(struct v4l2_format * format)375 static void sun6i_isp_capture_format_prepare(struct v4l2_format *format)
376 {
377 struct v4l2_pix_format *pix_format = &format->fmt.pix;
378 const struct v4l2_format_info *info;
379 unsigned int width, height;
380 unsigned int width_aligned;
381 unsigned int i;
382
383 v4l_bound_align_image(&pix_format->width, SUN6I_ISP_CAPTURE_WIDTH_MIN,
384 SUN6I_ISP_CAPTURE_WIDTH_MAX, 1,
385 &pix_format->height, SUN6I_ISP_CAPTURE_HEIGHT_MIN,
386 SUN6I_ISP_CAPTURE_HEIGHT_MAX, 1, 0);
387
388 if (!sun6i_isp_capture_format_find(pix_format->pixelformat))
389 pix_format->pixelformat =
390 sun6i_isp_capture_formats[0].pixelformat;
391
392 info = v4l2_format_info(pix_format->pixelformat);
393 if (WARN_ON(!info))
394 return;
395
396 width = pix_format->width;
397 height = pix_format->height;
398
399 /* Stride needs to be aligned to 4. */
400 width_aligned = ALIGN(width, 2);
401
402 pix_format->bytesperline = width_aligned * info->bpp[0];
403 pix_format->sizeimage = 0;
404
405 for (i = 0; i < info->comp_planes; i++) {
406 unsigned int hdiv = (i == 0) ? 1 : info->hdiv;
407 unsigned int vdiv = (i == 0) ? 1 : info->vdiv;
408
409 pix_format->sizeimage += info->bpp[i] *
410 DIV_ROUND_UP(width_aligned, hdiv) *
411 DIV_ROUND_UP(height, vdiv);
412 }
413
414 pix_format->field = V4L2_FIELD_NONE;
415
416 pix_format->colorspace = V4L2_COLORSPACE_RAW;
417 pix_format->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
418 pix_format->quantization = V4L2_QUANTIZATION_DEFAULT;
419 pix_format->xfer_func = V4L2_XFER_FUNC_DEFAULT;
420 }
421
sun6i_isp_capture_querycap(struct file * file,void * private,struct v4l2_capability * capability)422 static int sun6i_isp_capture_querycap(struct file *file, void *private,
423 struct v4l2_capability *capability)
424 {
425 struct sun6i_isp_device *isp_dev = video_drvdata(file);
426 struct video_device *video_dev = &isp_dev->capture.video_dev;
427
428 strscpy(capability->driver, SUN6I_ISP_NAME, sizeof(capability->driver));
429 strscpy(capability->card, video_dev->name, sizeof(capability->card));
430 snprintf(capability->bus_info, sizeof(capability->bus_info),
431 "platform:%s", dev_name(isp_dev->dev));
432
433 return 0;
434 }
435
sun6i_isp_capture_enum_fmt(struct file * file,void * private,struct v4l2_fmtdesc * fmtdesc)436 static int sun6i_isp_capture_enum_fmt(struct file *file, void *private,
437 struct v4l2_fmtdesc *fmtdesc)
438 {
439 u32 index = fmtdesc->index;
440
441 if (index >= ARRAY_SIZE(sun6i_isp_capture_formats))
442 return -EINVAL;
443
444 fmtdesc->pixelformat = sun6i_isp_capture_formats[index].pixelformat;
445
446 return 0;
447 }
448
sun6i_isp_capture_g_fmt(struct file * file,void * private,struct v4l2_format * format)449 static int sun6i_isp_capture_g_fmt(struct file *file, void *private,
450 struct v4l2_format *format)
451 {
452 struct sun6i_isp_device *isp_dev = video_drvdata(file);
453
454 *format = isp_dev->capture.format;
455
456 return 0;
457 }
458
sun6i_isp_capture_s_fmt(struct file * file,void * private,struct v4l2_format * format)459 static int sun6i_isp_capture_s_fmt(struct file *file, void *private,
460 struct v4l2_format *format)
461 {
462 struct sun6i_isp_device *isp_dev = video_drvdata(file);
463
464 if (vb2_is_busy(&isp_dev->capture.queue))
465 return -EBUSY;
466
467 sun6i_isp_capture_format_prepare(format);
468
469 isp_dev->capture.format = *format;
470
471 return 0;
472 }
473
sun6i_isp_capture_try_fmt(struct file * file,void * private,struct v4l2_format * format)474 static int sun6i_isp_capture_try_fmt(struct file *file, void *private,
475 struct v4l2_format *format)
476 {
477 sun6i_isp_capture_format_prepare(format);
478
479 return 0;
480 }
481
sun6i_isp_capture_enum_input(struct file * file,void * private,struct v4l2_input * input)482 static int sun6i_isp_capture_enum_input(struct file *file, void *private,
483 struct v4l2_input *input)
484 {
485 if (input->index != 0)
486 return -EINVAL;
487
488 input->type = V4L2_INPUT_TYPE_CAMERA;
489 strscpy(input->name, "Camera", sizeof(input->name));
490
491 return 0;
492 }
493
sun6i_isp_capture_g_input(struct file * file,void * private,unsigned int * index)494 static int sun6i_isp_capture_g_input(struct file *file, void *private,
495 unsigned int *index)
496 {
497 *index = 0;
498
499 return 0;
500 }
501
sun6i_isp_capture_s_input(struct file * file,void * private,unsigned int index)502 static int sun6i_isp_capture_s_input(struct file *file, void *private,
503 unsigned int index)
504 {
505 if (index != 0)
506 return -EINVAL;
507
508 return 0;
509 }
510
511 static const struct v4l2_ioctl_ops sun6i_isp_capture_ioctl_ops = {
512 .vidioc_querycap = sun6i_isp_capture_querycap,
513
514 .vidioc_enum_fmt_vid_cap = sun6i_isp_capture_enum_fmt,
515 .vidioc_g_fmt_vid_cap = sun6i_isp_capture_g_fmt,
516 .vidioc_s_fmt_vid_cap = sun6i_isp_capture_s_fmt,
517 .vidioc_try_fmt_vid_cap = sun6i_isp_capture_try_fmt,
518
519 .vidioc_enum_input = sun6i_isp_capture_enum_input,
520 .vidioc_g_input = sun6i_isp_capture_g_input,
521 .vidioc_s_input = sun6i_isp_capture_s_input,
522
523 .vidioc_create_bufs = vb2_ioctl_create_bufs,
524 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
525 .vidioc_reqbufs = vb2_ioctl_reqbufs,
526 .vidioc_querybuf = vb2_ioctl_querybuf,
527 .vidioc_expbuf = vb2_ioctl_expbuf,
528 .vidioc_qbuf = vb2_ioctl_qbuf,
529 .vidioc_dqbuf = vb2_ioctl_dqbuf,
530 .vidioc_streamon = vb2_ioctl_streamon,
531 .vidioc_streamoff = vb2_ioctl_streamoff,
532 };
533
sun6i_isp_capture_open(struct file * file)534 static int sun6i_isp_capture_open(struct file *file)
535 {
536 struct sun6i_isp_device *isp_dev = video_drvdata(file);
537 struct video_device *video_dev = &isp_dev->capture.video_dev;
538 struct mutex *lock = &isp_dev->capture.lock;
539 int ret;
540
541 if (mutex_lock_interruptible(lock))
542 return -ERESTARTSYS;
543
544 ret = v4l2_pipeline_pm_get(&video_dev->entity);
545 if (ret)
546 goto error_mutex;
547
548 ret = v4l2_fh_open(file);
549 if (ret)
550 goto error_pipeline;
551
552 mutex_unlock(lock);
553
554 return 0;
555
556 error_pipeline:
557 v4l2_pipeline_pm_put(&video_dev->entity);
558
559 error_mutex:
560 mutex_unlock(lock);
561
562 return ret;
563 }
564
sun6i_isp_capture_release(struct file * file)565 static int sun6i_isp_capture_release(struct file *file)
566 {
567 struct sun6i_isp_device *isp_dev = video_drvdata(file);
568 struct video_device *video_dev = &isp_dev->capture.video_dev;
569 struct mutex *lock = &isp_dev->capture.lock;
570
571 mutex_lock(lock);
572
573 _vb2_fop_release(file, NULL);
574 v4l2_pipeline_pm_put(&video_dev->entity);
575
576 mutex_unlock(lock);
577
578 return 0;
579 }
580
581 static const struct v4l2_file_operations sun6i_isp_capture_fops = {
582 .owner = THIS_MODULE,
583 .open = sun6i_isp_capture_open,
584 .release = sun6i_isp_capture_release,
585 .unlocked_ioctl = video_ioctl2,
586 .poll = vb2_fop_poll,
587 .mmap = vb2_fop_mmap,
588 };
589
590 /* Media Entity */
591
sun6i_isp_capture_link_validate(struct media_link * link)592 static int sun6i_isp_capture_link_validate(struct media_link *link)
593 {
594 struct video_device *video_dev =
595 media_entity_to_video_device(link->sink->entity);
596 struct sun6i_isp_device *isp_dev = video_get_drvdata(video_dev);
597 struct v4l2_device *v4l2_dev = &isp_dev->v4l2.v4l2_dev;
598 unsigned int capture_width, capture_height;
599 unsigned int proc_width, proc_height;
600
601 sun6i_isp_capture_dimensions(isp_dev, &capture_width, &capture_height);
602 sun6i_isp_proc_dimensions(isp_dev, &proc_width, &proc_height);
603
604 /* No cropping/scaling is supported (yet). */
605 if (capture_width != proc_width || capture_height != proc_height) {
606 v4l2_err(v4l2_dev,
607 "invalid input/output dimensions: %ux%u/%ux%u\n",
608 proc_width, proc_height, capture_width,
609 capture_height);
610 return -EINVAL;
611 }
612
613 return 0;
614 }
615
616 static const struct media_entity_operations sun6i_isp_capture_entity_ops = {
617 .link_validate = sun6i_isp_capture_link_validate,
618 };
619
620 /* Capture */
621
sun6i_isp_capture_setup(struct sun6i_isp_device * isp_dev)622 int sun6i_isp_capture_setup(struct sun6i_isp_device *isp_dev)
623 {
624 struct sun6i_isp_capture *capture = &isp_dev->capture;
625 struct sun6i_isp_capture_state *state = &capture->state;
626 struct v4l2_device *v4l2_dev = &isp_dev->v4l2.v4l2_dev;
627 struct v4l2_subdev *proc_subdev = &isp_dev->proc.subdev;
628 struct video_device *video_dev = &capture->video_dev;
629 struct vb2_queue *queue = &capture->queue;
630 struct media_pad *pad = &capture->pad;
631 struct v4l2_format *format = &capture->format;
632 struct v4l2_pix_format *pix_format = &format->fmt.pix;
633 int ret;
634
635 /* State */
636
637 INIT_LIST_HEAD(&state->queue);
638 spin_lock_init(&state->lock);
639
640 /* Media Entity */
641
642 video_dev->entity.ops = &sun6i_isp_capture_entity_ops;
643
644 /* Media Pads */
645
646 pad->flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
647
648 ret = media_entity_pads_init(&video_dev->entity, 1, pad);
649 if (ret)
650 goto error_mutex;
651
652 /* Queue */
653
654 mutex_init(&capture->lock);
655
656 queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
657 queue->io_modes = VB2_MMAP | VB2_DMABUF;
658 queue->buf_struct_size = sizeof(struct sun6i_isp_buffer);
659 queue->ops = &sun6i_isp_capture_queue_ops;
660 queue->mem_ops = &vb2_dma_contig_memops;
661 queue->min_queued_buffers = 2;
662 queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
663 queue->lock = &capture->lock;
664 queue->dev = isp_dev->dev;
665 queue->drv_priv = isp_dev;
666
667 ret = vb2_queue_init(queue);
668 if (ret) {
669 v4l2_err(v4l2_dev, "failed to initialize vb2 queue: %d\n", ret);
670 goto error_media_entity;
671 }
672
673 /* V4L2 Format */
674
675 format->type = queue->type;
676 pix_format->pixelformat = sun6i_isp_capture_formats[0].pixelformat;
677 pix_format->width = 1280;
678 pix_format->height = 720;
679
680 sun6i_isp_capture_format_prepare(format);
681
682 /* Video Device */
683
684 strscpy(video_dev->name, SUN6I_ISP_CAPTURE_NAME,
685 sizeof(video_dev->name));
686 video_dev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
687 video_dev->vfl_dir = VFL_DIR_RX;
688 video_dev->release = video_device_release_empty;
689 video_dev->fops = &sun6i_isp_capture_fops;
690 video_dev->ioctl_ops = &sun6i_isp_capture_ioctl_ops;
691 video_dev->v4l2_dev = v4l2_dev;
692 video_dev->queue = queue;
693 video_dev->lock = &capture->lock;
694
695 video_set_drvdata(video_dev, isp_dev);
696
697 ret = video_register_device(video_dev, VFL_TYPE_VIDEO, -1);
698 if (ret) {
699 v4l2_err(v4l2_dev, "failed to register video device: %d\n",
700 ret);
701 goto error_media_entity;
702 }
703
704 /* Media Pad Link */
705
706 ret = media_create_pad_link(&proc_subdev->entity,
707 SUN6I_ISP_PROC_PAD_SOURCE,
708 &video_dev->entity, 0,
709 MEDIA_LNK_FL_ENABLED |
710 MEDIA_LNK_FL_IMMUTABLE);
711 if (ret < 0) {
712 v4l2_err(v4l2_dev, "failed to create %s:%u -> %s:%u link\n",
713 proc_subdev->entity.name, SUN6I_ISP_PROC_PAD_SOURCE,
714 video_dev->entity.name, 0);
715 goto error_video_device;
716 }
717
718 return 0;
719
720 error_video_device:
721 vb2_video_unregister_device(video_dev);
722
723 error_media_entity:
724 media_entity_cleanup(&video_dev->entity);
725
726 error_mutex:
727 mutex_destroy(&capture->lock);
728
729 return ret;
730 }
731
sun6i_isp_capture_cleanup(struct sun6i_isp_device * isp_dev)732 void sun6i_isp_capture_cleanup(struct sun6i_isp_device *isp_dev)
733 {
734 struct sun6i_isp_capture *capture = &isp_dev->capture;
735 struct video_device *video_dev = &capture->video_dev;
736
737 vb2_video_unregister_device(video_dev);
738 media_entity_cleanup(&video_dev->entity);
739 mutex_destroy(&capture->lock);
740 }
741