1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
3
4 #include <linux/module.h>
5 #include <linux/pm_runtime.h>
6
7 #include <media/v4l2-event.h>
8 #include <media/v4l2-ioctl.h>
9
10 #include "ipu3.h"
11 #include "ipu3-dmamap.h"
12
13 /******************** v4l2_subdev_ops ********************/
14
15 #define IPU3_RUNNING_MODE_VIDEO 0
16 #define IPU3_RUNNING_MODE_STILL 1
17
imgu_subdev_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)18 static int imgu_subdev_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
19 {
20 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
21 struct imgu_v4l2_subdev,
22 subdev);
23 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
24 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[imgu_sd->pipe];
25 struct v4l2_rect try_crop = {
26 .top = 0,
27 .left = 0,
28 };
29 unsigned int i;
30
31 try_crop.width =
32 imgu_pipe->nodes[IMGU_NODE_IN].vdev_fmt.fmt.pix_mp.width;
33 try_crop.height =
34 imgu_pipe->nodes[IMGU_NODE_IN].vdev_fmt.fmt.pix_mp.height;
35
36 /* Initialize try_fmt */
37 for (i = 0; i < IMGU_NODE_NUM; i++) {
38 struct v4l2_mbus_framefmt *try_fmt =
39 v4l2_subdev_get_try_format(sd, fh->pad, i);
40
41 try_fmt->width = try_crop.width;
42 try_fmt->height = try_crop.height;
43 try_fmt->code = imgu_pipe->nodes[i].pad_fmt.code;
44 try_fmt->field = V4L2_FIELD_NONE;
45 }
46
47 *v4l2_subdev_get_try_crop(sd, fh->pad, IMGU_NODE_IN) = try_crop;
48 *v4l2_subdev_get_try_compose(sd, fh->pad, IMGU_NODE_IN) = try_crop;
49
50 return 0;
51 }
52
imgu_subdev_s_stream(struct v4l2_subdev * sd,int enable)53 static int imgu_subdev_s_stream(struct v4l2_subdev *sd, int enable)
54 {
55 int i;
56 unsigned int node;
57 int r = 0;
58 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
59 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
60 struct imgu_v4l2_subdev,
61 subdev);
62 unsigned int pipe = imgu_sd->pipe;
63 struct device *dev = &imgu->pci_dev->dev;
64 struct v4l2_pix_format_mplane *fmts[IPU3_CSS_QUEUES] = { NULL };
65 struct v4l2_rect *rects[IPU3_CSS_RECTS] = { NULL };
66 struct imgu_css_pipe *css_pipe = &imgu->css.pipes[pipe];
67 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
68
69 dev_dbg(dev, "%s %d for pipe %u", __func__, enable, pipe);
70 /* grab ctrl after streamon and return after off */
71 v4l2_ctrl_grab(imgu_sd->ctrl, enable);
72
73 if (!enable) {
74 imgu_sd->active = false;
75 return 0;
76 }
77
78 for (i = 0; i < IMGU_NODE_NUM; i++)
79 imgu_pipe->queue_enabled[i] = imgu_pipe->nodes[i].enabled;
80
81 /* This is handled specially */
82 imgu_pipe->queue_enabled[IPU3_CSS_QUEUE_PARAMS] = false;
83
84 /* Initialize CSS formats */
85 for (i = 0; i < IPU3_CSS_QUEUES; i++) {
86 node = imgu_map_node(imgu, i);
87 /* No need to reconfig meta nodes */
88 if (node == IMGU_NODE_STAT_3A || node == IMGU_NODE_PARAMS)
89 continue;
90 fmts[i] = imgu_pipe->queue_enabled[node] ?
91 &imgu_pipe->nodes[node].vdev_fmt.fmt.pix_mp : NULL;
92 }
93
94 /* Enable VF output only when VF queue requested by user */
95 css_pipe->vf_output_en = false;
96 if (imgu_pipe->nodes[IMGU_NODE_VF].enabled)
97 css_pipe->vf_output_en = true;
98
99 if (atomic_read(&imgu_sd->running_mode) == IPU3_RUNNING_MODE_VIDEO)
100 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_VIDEO;
101 else
102 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_CAPTURE;
103
104 dev_dbg(dev, "IPU3 pipe %u pipe_id %u", pipe, css_pipe->pipe_id);
105
106 rects[IPU3_CSS_RECT_EFFECTIVE] = &imgu_sd->rect.eff;
107 rects[IPU3_CSS_RECT_BDS] = &imgu_sd->rect.bds;
108 rects[IPU3_CSS_RECT_GDC] = &imgu_sd->rect.gdc;
109
110 r = imgu_css_fmt_set(&imgu->css, fmts, rects, pipe);
111 if (r) {
112 dev_err(dev, "failed to set initial formats pipe %u with (%d)",
113 pipe, r);
114 return r;
115 }
116
117 imgu_sd->active = true;
118
119 return 0;
120 }
121
imgu_subdev_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)122 static int imgu_subdev_get_fmt(struct v4l2_subdev *sd,
123 struct v4l2_subdev_pad_config *cfg,
124 struct v4l2_subdev_format *fmt)
125 {
126 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
127 struct v4l2_mbus_framefmt *mf;
128 struct imgu_media_pipe *imgu_pipe;
129 u32 pad = fmt->pad;
130 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
131 struct imgu_v4l2_subdev,
132 subdev);
133 unsigned int pipe = imgu_sd->pipe;
134
135 imgu_pipe = &imgu->imgu_pipe[pipe];
136 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
137 fmt->format = imgu_pipe->nodes[pad].pad_fmt;
138 } else {
139 mf = v4l2_subdev_get_try_format(sd, cfg, pad);
140 fmt->format = *mf;
141 }
142
143 return 0;
144 }
145
imgu_subdev_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)146 static int imgu_subdev_set_fmt(struct v4l2_subdev *sd,
147 struct v4l2_subdev_pad_config *cfg,
148 struct v4l2_subdev_format *fmt)
149 {
150 struct imgu_media_pipe *imgu_pipe;
151 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
152 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
153 struct imgu_v4l2_subdev,
154 subdev);
155 struct v4l2_mbus_framefmt *mf;
156 u32 pad = fmt->pad;
157 unsigned int pipe = imgu_sd->pipe;
158
159 dev_dbg(&imgu->pci_dev->dev, "set subdev %u pad %u fmt to [%ux%u]",
160 pipe, pad, fmt->format.width, fmt->format.height);
161
162 imgu_pipe = &imgu->imgu_pipe[pipe];
163 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
164 mf = v4l2_subdev_get_try_format(sd, cfg, pad);
165 else
166 mf = &imgu_pipe->nodes[pad].pad_fmt;
167
168 fmt->format.code = mf->code;
169 /* Clamp the w and h based on the hardware capabilities */
170 if (imgu_sd->subdev_pads[pad].flags & MEDIA_PAD_FL_SOURCE) {
171 fmt->format.width = clamp(fmt->format.width,
172 IPU3_OUTPUT_MIN_WIDTH,
173 IPU3_OUTPUT_MAX_WIDTH);
174 fmt->format.height = clamp(fmt->format.height,
175 IPU3_OUTPUT_MIN_HEIGHT,
176 IPU3_OUTPUT_MAX_HEIGHT);
177 } else {
178 fmt->format.width = clamp(fmt->format.width,
179 IPU3_INPUT_MIN_WIDTH,
180 IPU3_INPUT_MAX_WIDTH);
181 fmt->format.height = clamp(fmt->format.height,
182 IPU3_INPUT_MIN_HEIGHT,
183 IPU3_INPUT_MAX_HEIGHT);
184 }
185
186 *mf = fmt->format;
187
188 return 0;
189 }
190
imgu_subdev_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)191 static int imgu_subdev_get_selection(struct v4l2_subdev *sd,
192 struct v4l2_subdev_pad_config *cfg,
193 struct v4l2_subdev_selection *sel)
194 {
195 struct v4l2_rect *try_sel, *r;
196 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
197 struct imgu_v4l2_subdev,
198 subdev);
199
200 if (sel->pad != IMGU_NODE_IN)
201 return -EINVAL;
202
203 switch (sel->target) {
204 case V4L2_SEL_TGT_CROP:
205 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
206 r = &imgu_sd->rect.eff;
207 break;
208 case V4L2_SEL_TGT_COMPOSE:
209 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
210 r = &imgu_sd->rect.bds;
211 break;
212 default:
213 return -EINVAL;
214 }
215
216 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
217 sel->r = *try_sel;
218 else
219 sel->r = *r;
220
221 return 0;
222 }
223
imgu_subdev_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)224 static int imgu_subdev_set_selection(struct v4l2_subdev *sd,
225 struct v4l2_subdev_pad_config *cfg,
226 struct v4l2_subdev_selection *sel)
227 {
228 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
229 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
230 struct imgu_v4l2_subdev,
231 subdev);
232 struct v4l2_rect *rect, *try_sel;
233
234 dev_dbg(&imgu->pci_dev->dev,
235 "set subdev %u sel which %u target 0x%4x rect [%ux%u]",
236 imgu_sd->pipe, sel->which, sel->target,
237 sel->r.width, sel->r.height);
238
239 if (sel->pad != IMGU_NODE_IN)
240 return -EINVAL;
241
242 switch (sel->target) {
243 case V4L2_SEL_TGT_CROP:
244 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
245 rect = &imgu_sd->rect.eff;
246 break;
247 case V4L2_SEL_TGT_COMPOSE:
248 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
249 rect = &imgu_sd->rect.bds;
250 break;
251 default:
252 return -EINVAL;
253 }
254
255 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
256 *try_sel = sel->r;
257 else
258 *rect = sel->r;
259
260 return 0;
261 }
262
263 /******************** media_entity_operations ********************/
264
imgu_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)265 static int imgu_link_setup(struct media_entity *entity,
266 const struct media_pad *local,
267 const struct media_pad *remote, u32 flags)
268 {
269 struct imgu_media_pipe *imgu_pipe;
270 struct v4l2_subdev *sd = container_of(entity, struct v4l2_subdev,
271 entity);
272 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
273 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
274 struct imgu_v4l2_subdev,
275 subdev);
276 unsigned int pipe = imgu_sd->pipe;
277 u32 pad = local->index;
278
279 WARN_ON(pad >= IMGU_NODE_NUM);
280
281 dev_dbg(&imgu->pci_dev->dev, "pipe %u pad %u is %s", pipe, pad,
282 flags & MEDIA_LNK_FL_ENABLED ? "enabled" : "disabled");
283
284 imgu_pipe = &imgu->imgu_pipe[pipe];
285 imgu_pipe->nodes[pad].enabled = flags & MEDIA_LNK_FL_ENABLED;
286
287 /* enable input node to enable the pipe */
288 if (pad != IMGU_NODE_IN)
289 return 0;
290
291 if (flags & MEDIA_LNK_FL_ENABLED)
292 __set_bit(pipe, imgu->css.enabled_pipes);
293 else
294 __clear_bit(pipe, imgu->css.enabled_pipes);
295
296 dev_dbg(&imgu->pci_dev->dev, "pipe %u is %s", pipe,
297 flags & MEDIA_LNK_FL_ENABLED ? "enabled" : "disabled");
298
299 return 0;
300 }
301
302 /******************** vb2_ops ********************/
303
imgu_vb2_buf_init(struct vb2_buffer * vb)304 static int imgu_vb2_buf_init(struct vb2_buffer *vb)
305 {
306 struct sg_table *sg = vb2_dma_sg_plane_desc(vb, 0);
307 struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
308 struct imgu_buffer *buf = container_of(vb,
309 struct imgu_buffer, vid_buf.vbb.vb2_buf);
310 struct imgu_video_device *node =
311 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
312 unsigned int queue = imgu_node_to_queue(node->id);
313
314 if (queue == IPU3_CSS_QUEUE_PARAMS)
315 return 0;
316
317 return imgu_dmamap_map_sg(imgu, sg->sgl, sg->nents, &buf->map);
318 }
319
320 /* Called when each buffer is freed */
imgu_vb2_buf_cleanup(struct vb2_buffer * vb)321 static void imgu_vb2_buf_cleanup(struct vb2_buffer *vb)
322 {
323 struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
324 struct imgu_buffer *buf = container_of(vb,
325 struct imgu_buffer, vid_buf.vbb.vb2_buf);
326 struct imgu_video_device *node =
327 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
328 unsigned int queue = imgu_node_to_queue(node->id);
329
330 if (queue == IPU3_CSS_QUEUE_PARAMS)
331 return;
332
333 imgu_dmamap_unmap(imgu, &buf->map);
334 }
335
336 /* Transfer buffer ownership to me */
imgu_vb2_buf_queue(struct vb2_buffer * vb)337 static void imgu_vb2_buf_queue(struct vb2_buffer *vb)
338 {
339 struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
340 struct imgu_video_device *node =
341 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
342 unsigned int queue = imgu_node_to_queue(node->id);
343 struct imgu_buffer *buf = container_of(vb, struct imgu_buffer,
344 vid_buf.vbb.vb2_buf);
345 unsigned long need_bytes;
346 unsigned long payload = vb2_get_plane_payload(vb, 0);
347
348 if (vb->vb2_queue->type == V4L2_BUF_TYPE_META_CAPTURE ||
349 vb->vb2_queue->type == V4L2_BUF_TYPE_META_OUTPUT)
350 need_bytes = node->vdev_fmt.fmt.meta.buffersize;
351 else
352 need_bytes = node->vdev_fmt.fmt.pix_mp.plane_fmt[0].sizeimage;
353
354 if (queue == IPU3_CSS_QUEUE_PARAMS && payload && payload < need_bytes) {
355 dev_err(&imgu->pci_dev->dev, "invalid data size for params.");
356 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
357 return;
358 }
359
360 mutex_lock(&imgu->lock);
361 if (queue != IPU3_CSS_QUEUE_PARAMS)
362 imgu_css_buf_init(&buf->css_buf, queue, buf->map.daddr);
363
364 list_add_tail(&buf->vid_buf.list, &node->buffers);
365 mutex_unlock(&imgu->lock);
366
367 vb2_set_plane_payload(vb, 0, need_bytes);
368
369 mutex_lock(&imgu->streaming_lock);
370 if (imgu->streaming)
371 imgu_queue_buffers(imgu, false, node->pipe);
372 mutex_unlock(&imgu->streaming_lock);
373
374 dev_dbg(&imgu->pci_dev->dev, "%s for pipe %u node %u", __func__,
375 node->pipe, node->id);
376 }
377
imgu_vb2_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])378 static int imgu_vb2_queue_setup(struct vb2_queue *vq,
379 unsigned int *num_buffers,
380 unsigned int *num_planes,
381 unsigned int sizes[],
382 struct device *alloc_devs[])
383 {
384 struct imgu_device *imgu = vb2_get_drv_priv(vq);
385 struct imgu_video_device *node =
386 container_of(vq, struct imgu_video_device, vbq);
387 const struct v4l2_format *fmt = &node->vdev_fmt;
388 unsigned int size;
389
390 *num_buffers = clamp_val(*num_buffers, 1, VB2_MAX_FRAME);
391 alloc_devs[0] = &imgu->pci_dev->dev;
392
393 if (vq->type == V4L2_BUF_TYPE_META_CAPTURE ||
394 vq->type == V4L2_BUF_TYPE_META_OUTPUT)
395 size = fmt->fmt.meta.buffersize;
396 else
397 size = fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
398
399 if (*num_planes) {
400 if (sizes[0] < size)
401 return -EINVAL;
402 size = sizes[0];
403 }
404
405 *num_planes = 1;
406 sizes[0] = size;
407
408 /* Initialize buffer queue */
409 INIT_LIST_HEAD(&node->buffers);
410
411 return 0;
412 }
413
414 /* Check if all enabled video nodes are streaming, exception ignored */
imgu_all_nodes_streaming(struct imgu_device * imgu,struct imgu_video_device * except)415 static bool imgu_all_nodes_streaming(struct imgu_device *imgu,
416 struct imgu_video_device *except)
417 {
418 unsigned int i, pipe, p;
419 struct imgu_video_device *node;
420 struct device *dev = &imgu->pci_dev->dev;
421
422 pipe = except->pipe;
423 if (!test_bit(pipe, imgu->css.enabled_pipes)) {
424 dev_warn(&imgu->pci_dev->dev,
425 "pipe %u link is not ready yet", pipe);
426 return false;
427 }
428
429 for_each_set_bit(p, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) {
430 for (i = 0; i < IMGU_NODE_NUM; i++) {
431 node = &imgu->imgu_pipe[p].nodes[i];
432 dev_dbg(dev, "%s pipe %u queue %u name %s enabled = %u",
433 __func__, p, i, node->name, node->enabled);
434 if (node == except)
435 continue;
436 if (node->enabled && !vb2_start_streaming_called(&node->vbq))
437 return false;
438 }
439 }
440
441 return true;
442 }
443
imgu_return_all_buffers(struct imgu_device * imgu,struct imgu_video_device * node,enum vb2_buffer_state state)444 static void imgu_return_all_buffers(struct imgu_device *imgu,
445 struct imgu_video_device *node,
446 enum vb2_buffer_state state)
447 {
448 struct imgu_vb2_buffer *b, *b0;
449
450 /* Return all buffers */
451 mutex_lock(&imgu->lock);
452 list_for_each_entry_safe(b, b0, &node->buffers, list) {
453 list_del(&b->list);
454 vb2_buffer_done(&b->vbb.vb2_buf, state);
455 }
456 mutex_unlock(&imgu->lock);
457 }
458
imgu_vb2_start_streaming(struct vb2_queue * vq,unsigned int count)459 static int imgu_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
460 {
461 struct imgu_media_pipe *imgu_pipe;
462 struct imgu_device *imgu = vb2_get_drv_priv(vq);
463 struct device *dev = &imgu->pci_dev->dev;
464 struct imgu_video_device *node =
465 container_of(vq, struct imgu_video_device, vbq);
466 int r;
467 unsigned int pipe;
468
469 dev_dbg(dev, "%s node name %s pipe %u id %u", __func__,
470 node->name, node->pipe, node->id);
471
472 mutex_lock(&imgu->streaming_lock);
473 if (imgu->streaming) {
474 r = -EBUSY;
475 mutex_unlock(&imgu->streaming_lock);
476 goto fail_return_bufs;
477 }
478 mutex_unlock(&imgu->streaming_lock);
479
480 if (!node->enabled) {
481 dev_err(dev, "IMGU node is not enabled");
482 r = -EINVAL;
483 goto fail_return_bufs;
484 }
485
486 pipe = node->pipe;
487 imgu_pipe = &imgu->imgu_pipe[pipe];
488 r = media_pipeline_start(&node->vdev.entity, &imgu_pipe->pipeline);
489 if (r < 0)
490 goto fail_return_bufs;
491
492 if (!imgu_all_nodes_streaming(imgu, node))
493 return 0;
494
495 for_each_set_bit(pipe, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) {
496 r = v4l2_subdev_call(&imgu->imgu_pipe[pipe].imgu_sd.subdev,
497 video, s_stream, 1);
498 if (r < 0)
499 goto fail_stop_pipeline;
500 }
501
502 /* Start streaming of the whole pipeline now */
503 dev_dbg(dev, "IMGU streaming is ready to start");
504 mutex_lock(&imgu->streaming_lock);
505 r = imgu_s_stream(imgu, true);
506 if (!r)
507 imgu->streaming = true;
508 mutex_unlock(&imgu->streaming_lock);
509
510 return 0;
511
512 fail_stop_pipeline:
513 media_pipeline_stop(&node->vdev.entity);
514 fail_return_bufs:
515 imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_QUEUED);
516
517 return r;
518 }
519
imgu_vb2_stop_streaming(struct vb2_queue * vq)520 static void imgu_vb2_stop_streaming(struct vb2_queue *vq)
521 {
522 struct imgu_media_pipe *imgu_pipe;
523 struct imgu_device *imgu = vb2_get_drv_priv(vq);
524 struct device *dev = &imgu->pci_dev->dev;
525 struct imgu_video_device *node =
526 container_of(vq, struct imgu_video_device, vbq);
527 int r;
528 unsigned int pipe;
529
530 WARN_ON(!node->enabled);
531
532 pipe = node->pipe;
533 dev_dbg(dev, "Try to stream off node [%u][%u]", pipe, node->id);
534 imgu_pipe = &imgu->imgu_pipe[pipe];
535 r = v4l2_subdev_call(&imgu_pipe->imgu_sd.subdev, video, s_stream, 0);
536 if (r)
537 dev_err(&imgu->pci_dev->dev,
538 "failed to stop subdev streaming\n");
539
540 mutex_lock(&imgu->streaming_lock);
541 /* Was this the first node with streaming disabled? */
542 if (imgu->streaming && imgu_all_nodes_streaming(imgu, node)) {
543 /* Yes, really stop streaming now */
544 dev_dbg(dev, "IMGU streaming is ready to stop");
545 r = imgu_s_stream(imgu, false);
546 if (!r)
547 imgu->streaming = false;
548 }
549
550 imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_ERROR);
551 mutex_unlock(&imgu->streaming_lock);
552
553 media_pipeline_stop(&node->vdev.entity);
554 }
555
556 /******************** v4l2_ioctl_ops ********************/
557
558 #define VID_CAPTURE 0
559 #define VID_OUTPUT 1
560 #define DEF_VID_CAPTURE 0
561 #define DEF_VID_OUTPUT 1
562
563 struct imgu_fmt {
564 u32 fourcc;
565 u16 type; /* VID_CAPTURE or VID_OUTPUT not both */
566 };
567
568 /* format descriptions for capture and preview */
569 static const struct imgu_fmt formats[] = {
570 { V4L2_PIX_FMT_NV12, VID_CAPTURE },
571 { V4L2_PIX_FMT_IPU3_SGRBG10, VID_OUTPUT },
572 { V4L2_PIX_FMT_IPU3_SBGGR10, VID_OUTPUT },
573 { V4L2_PIX_FMT_IPU3_SGBRG10, VID_OUTPUT },
574 { V4L2_PIX_FMT_IPU3_SRGGB10, VID_OUTPUT },
575 };
576
577 /* Find the first matched format, return default if not found */
find_format(struct v4l2_format * f,u32 type)578 static const struct imgu_fmt *find_format(struct v4l2_format *f, u32 type)
579 {
580 unsigned int i;
581
582 for (i = 0; i < ARRAY_SIZE(formats); i++) {
583 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
584 formats[i].type == type)
585 return &formats[i];
586 }
587
588 return type == VID_CAPTURE ? &formats[DEF_VID_CAPTURE] :
589 &formats[DEF_VID_OUTPUT];
590 }
591
imgu_vidioc_querycap(struct file * file,void * fh,struct v4l2_capability * cap)592 static int imgu_vidioc_querycap(struct file *file, void *fh,
593 struct v4l2_capability *cap)
594 {
595 struct imgu_video_device *node = file_to_intel_imgu_node(file);
596
597 strscpy(cap->driver, IMGU_NAME, sizeof(cap->driver));
598 strscpy(cap->card, IMGU_NAME, sizeof(cap->card));
599 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", node->name);
600
601 return 0;
602 }
603
enum_fmts(struct v4l2_fmtdesc * f,u32 type)604 static int enum_fmts(struct v4l2_fmtdesc *f, u32 type)
605 {
606 unsigned int i, j;
607
608 if (f->mbus_code != 0 && f->mbus_code != MEDIA_BUS_FMT_FIXED)
609 return -EINVAL;
610
611 for (i = j = 0; i < ARRAY_SIZE(formats); ++i) {
612 if (formats[i].type == type) {
613 if (j == f->index)
614 break;
615 ++j;
616 }
617 }
618
619 if (i < ARRAY_SIZE(formats)) {
620 f->pixelformat = formats[i].fourcc;
621 return 0;
622 }
623
624 return -EINVAL;
625 }
626
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)627 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
628 struct v4l2_fmtdesc *f)
629 {
630 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
631 return -EINVAL;
632
633 return enum_fmts(f, VID_CAPTURE);
634 }
635
vidioc_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)636 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
637 struct v4l2_fmtdesc *f)
638 {
639 if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
640 return -EINVAL;
641
642 return enum_fmts(f, VID_OUTPUT);
643 }
644
645 /* Propagate forward always the format from the CIO2 subdev */
imgu_vidioc_g_fmt(struct file * file,void * fh,struct v4l2_format * f)646 static int imgu_vidioc_g_fmt(struct file *file, void *fh,
647 struct v4l2_format *f)
648 {
649 struct imgu_video_device *node = file_to_intel_imgu_node(file);
650
651 f->fmt = node->vdev_fmt.fmt;
652
653 return 0;
654 }
655
656 /*
657 * Set input/output format. Unless it is just a try, this also resets
658 * selections (ie. effective and BDS resolutions) to defaults.
659 */
imgu_fmt(struct imgu_device * imgu,unsigned int pipe,int node,struct v4l2_format * f,bool try)660 static int imgu_fmt(struct imgu_device *imgu, unsigned int pipe, int node,
661 struct v4l2_format *f, bool try)
662 {
663 struct device *dev = &imgu->pci_dev->dev;
664 struct v4l2_pix_format_mplane *fmts[IPU3_CSS_QUEUES] = { NULL };
665 struct v4l2_rect *rects[IPU3_CSS_RECTS] = { NULL };
666 struct v4l2_mbus_framefmt pad_fmt;
667 unsigned int i, css_q;
668 int ret;
669 struct imgu_css_pipe *css_pipe = &imgu->css.pipes[pipe];
670 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
671 struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd;
672
673 dev_dbg(dev, "set fmt node [%u][%u](try = %u)", pipe, node, try);
674
675 for (i = 0; i < IMGU_NODE_NUM; i++)
676 dev_dbg(dev, "IMGU pipe %u node %u enabled = %u",
677 pipe, i, imgu_pipe->nodes[i].enabled);
678
679 if (imgu_pipe->nodes[IMGU_NODE_VF].enabled)
680 css_pipe->vf_output_en = true;
681
682 if (atomic_read(&imgu_sd->running_mode) == IPU3_RUNNING_MODE_VIDEO)
683 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_VIDEO;
684 else
685 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_CAPTURE;
686
687 dev_dbg(dev, "IPU3 pipe %u pipe_id = %u", pipe, css_pipe->pipe_id);
688
689 for (i = 0; i < IPU3_CSS_QUEUES; i++) {
690 unsigned int inode = imgu_map_node(imgu, i);
691
692 /* Skip the meta node */
693 if (inode == IMGU_NODE_STAT_3A || inode == IMGU_NODE_PARAMS)
694 continue;
695
696 if (try) {
697 fmts[i] = kmemdup(&imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp,
698 sizeof(struct v4l2_pix_format_mplane),
699 GFP_KERNEL);
700 if (!fmts[i]) {
701 ret = -ENOMEM;
702 goto out;
703 }
704 } else {
705 fmts[i] = &imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp;
706 }
707
708 /* CSS expects some format on OUT queue */
709 if (i != IPU3_CSS_QUEUE_OUT &&
710 !imgu_pipe->nodes[inode].enabled)
711 fmts[i] = NULL;
712 }
713
714 if (!try) {
715 /* eff and bds res got by imgu_s_sel */
716 struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd;
717
718 rects[IPU3_CSS_RECT_EFFECTIVE] = &imgu_sd->rect.eff;
719 rects[IPU3_CSS_RECT_BDS] = &imgu_sd->rect.bds;
720 rects[IPU3_CSS_RECT_GDC] = &imgu_sd->rect.gdc;
721
722 /* suppose that pad fmt was set by subdev s_fmt before */
723 pad_fmt = imgu_pipe->nodes[IMGU_NODE_IN].pad_fmt;
724 rects[IPU3_CSS_RECT_GDC]->width = pad_fmt.width;
725 rects[IPU3_CSS_RECT_GDC]->height = pad_fmt.height;
726 }
727
728 /*
729 * imgu doesn't set the node to the value given by user
730 * before we return success from this function, so set it here.
731 */
732 css_q = imgu_node_to_queue(node);
733 if (!fmts[css_q]) {
734 ret = -EINVAL;
735 goto out;
736 }
737 *fmts[css_q] = f->fmt.pix_mp;
738
739 if (try)
740 ret = imgu_css_fmt_try(&imgu->css, fmts, rects, pipe);
741 else
742 ret = imgu_css_fmt_set(&imgu->css, fmts, rects, pipe);
743
744 /* ret is the binary number in the firmware blob */
745 if (ret < 0)
746 goto out;
747
748 if (try)
749 f->fmt.pix_mp = *fmts[css_q];
750 else
751 f->fmt = imgu_pipe->nodes[node].vdev_fmt.fmt;
752
753 out:
754 if (try) {
755 for (i = 0; i < IPU3_CSS_QUEUES; i++)
756 kfree(fmts[i]);
757 }
758
759 return ret;
760 }
761
imgu_try_fmt(struct file * file,void * fh,struct v4l2_format * f)762 static int imgu_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
763 {
764 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
765 const struct imgu_fmt *fmt;
766
767 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
768 fmt = find_format(f, VID_CAPTURE);
769 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
770 fmt = find_format(f, VID_OUTPUT);
771 else
772 return -EINVAL;
773
774 pixm->pixelformat = fmt->fourcc;
775
776 memset(pixm->plane_fmt[0].reserved, 0,
777 sizeof(pixm->plane_fmt[0].reserved));
778
779 return 0;
780 }
781
imgu_vidioc_try_fmt(struct file * file,void * fh,struct v4l2_format * f)782 static int imgu_vidioc_try_fmt(struct file *file, void *fh,
783 struct v4l2_format *f)
784 {
785 struct imgu_device *imgu = video_drvdata(file);
786 struct device *dev = &imgu->pci_dev->dev;
787 struct imgu_video_device *node = file_to_intel_imgu_node(file);
788 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
789 int r;
790
791 dev_dbg(dev, "%s [%ux%u] for node %u\n", __func__,
792 pix_mp->width, pix_mp->height, node->id);
793
794 r = imgu_try_fmt(file, fh, f);
795 if (r)
796 return r;
797
798 return imgu_fmt(imgu, node->pipe, node->id, f, true);
799 }
800
imgu_vidioc_s_fmt(struct file * file,void * fh,struct v4l2_format * f)801 static int imgu_vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
802 {
803 struct imgu_device *imgu = video_drvdata(file);
804 struct device *dev = &imgu->pci_dev->dev;
805 struct imgu_video_device *node = file_to_intel_imgu_node(file);
806 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
807 int r;
808
809 dev_dbg(dev, "%s [%ux%u] for node %u\n", __func__,
810 pix_mp->width, pix_mp->height, node->id);
811
812 r = imgu_try_fmt(file, fh, f);
813 if (r)
814 return r;
815
816 return imgu_fmt(imgu, node->pipe, node->id, f, false);
817 }
818
819 struct imgu_meta_fmt {
820 __u32 fourcc;
821 char *name;
822 };
823
824 /* From drivers/media/v4l2-core/v4l2-ioctl.c */
825 static const struct imgu_meta_fmt meta_fmts[] = {
826 { V4L2_META_FMT_IPU3_PARAMS, "IPU3 processing parameters" },
827 { V4L2_META_FMT_IPU3_STAT_3A, "IPU3 3A statistics" },
828 };
829
imgu_meta_enum_format(struct file * file,void * fh,struct v4l2_fmtdesc * fmt)830 static int imgu_meta_enum_format(struct file *file, void *fh,
831 struct v4l2_fmtdesc *fmt)
832 {
833 struct imgu_video_device *node = file_to_intel_imgu_node(file);
834 unsigned int i = fmt->type == V4L2_BUF_TYPE_META_OUTPUT ? 0 : 1;
835
836 /* Each node is dedicated to only one meta format */
837 if (fmt->index > 0 || fmt->type != node->vbq.type)
838 return -EINVAL;
839
840 if (fmt->mbus_code != 0 && fmt->mbus_code != MEDIA_BUS_FMT_FIXED)
841 return -EINVAL;
842
843 strscpy(fmt->description, meta_fmts[i].name, sizeof(fmt->description));
844 fmt->pixelformat = meta_fmts[i].fourcc;
845
846 return 0;
847 }
848
imgu_vidioc_g_meta_fmt(struct file * file,void * fh,struct v4l2_format * f)849 static int imgu_vidioc_g_meta_fmt(struct file *file, void *fh,
850 struct v4l2_format *f)
851 {
852 struct imgu_video_device *node = file_to_intel_imgu_node(file);
853
854 if (f->type != node->vbq.type)
855 return -EINVAL;
856
857 f->fmt = node->vdev_fmt.fmt;
858
859 return 0;
860 }
861
862 /******************** function pointers ********************/
863
864 static struct v4l2_subdev_internal_ops imgu_subdev_internal_ops = {
865 .open = imgu_subdev_open,
866 };
867
868 static const struct v4l2_subdev_core_ops imgu_subdev_core_ops = {
869 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
870 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
871 };
872
873 static const struct v4l2_subdev_video_ops imgu_subdev_video_ops = {
874 .s_stream = imgu_subdev_s_stream,
875 };
876
877 static const struct v4l2_subdev_pad_ops imgu_subdev_pad_ops = {
878 .link_validate = v4l2_subdev_link_validate_default,
879 .get_fmt = imgu_subdev_get_fmt,
880 .set_fmt = imgu_subdev_set_fmt,
881 .get_selection = imgu_subdev_get_selection,
882 .set_selection = imgu_subdev_set_selection,
883 };
884
885 static const struct v4l2_subdev_ops imgu_subdev_ops = {
886 .core = &imgu_subdev_core_ops,
887 .video = &imgu_subdev_video_ops,
888 .pad = &imgu_subdev_pad_ops,
889 };
890
891 static const struct media_entity_operations imgu_media_ops = {
892 .link_setup = imgu_link_setup,
893 .link_validate = v4l2_subdev_link_validate,
894 };
895
896 /****************** vb2_ops of the Q ********************/
897
898 static const struct vb2_ops imgu_vb2_ops = {
899 .buf_init = imgu_vb2_buf_init,
900 .buf_cleanup = imgu_vb2_buf_cleanup,
901 .buf_queue = imgu_vb2_buf_queue,
902 .queue_setup = imgu_vb2_queue_setup,
903 .start_streaming = imgu_vb2_start_streaming,
904 .stop_streaming = imgu_vb2_stop_streaming,
905 .wait_prepare = vb2_ops_wait_prepare,
906 .wait_finish = vb2_ops_wait_finish,
907 };
908
909 /****************** v4l2_file_operations *****************/
910
911 static const struct v4l2_file_operations imgu_v4l2_fops = {
912 .unlocked_ioctl = video_ioctl2,
913 .open = v4l2_fh_open,
914 .release = vb2_fop_release,
915 .poll = vb2_fop_poll,
916 .mmap = vb2_fop_mmap,
917 };
918
919 /******************** v4l2_ioctl_ops ********************/
920
921 static const struct v4l2_ioctl_ops imgu_v4l2_ioctl_ops = {
922 .vidioc_querycap = imgu_vidioc_querycap,
923
924 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
925 .vidioc_g_fmt_vid_cap_mplane = imgu_vidioc_g_fmt,
926 .vidioc_s_fmt_vid_cap_mplane = imgu_vidioc_s_fmt,
927 .vidioc_try_fmt_vid_cap_mplane = imgu_vidioc_try_fmt,
928
929 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
930 .vidioc_g_fmt_vid_out_mplane = imgu_vidioc_g_fmt,
931 .vidioc_s_fmt_vid_out_mplane = imgu_vidioc_s_fmt,
932 .vidioc_try_fmt_vid_out_mplane = imgu_vidioc_try_fmt,
933
934 /* buffer queue management */
935 .vidioc_reqbufs = vb2_ioctl_reqbufs,
936 .vidioc_create_bufs = vb2_ioctl_create_bufs,
937 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
938 .vidioc_querybuf = vb2_ioctl_querybuf,
939 .vidioc_qbuf = vb2_ioctl_qbuf,
940 .vidioc_dqbuf = vb2_ioctl_dqbuf,
941 .vidioc_streamon = vb2_ioctl_streamon,
942 .vidioc_streamoff = vb2_ioctl_streamoff,
943 .vidioc_expbuf = vb2_ioctl_expbuf,
944 };
945
946 static const struct v4l2_ioctl_ops imgu_v4l2_meta_ioctl_ops = {
947 .vidioc_querycap = imgu_vidioc_querycap,
948
949 /* meta capture */
950 .vidioc_enum_fmt_meta_cap = imgu_meta_enum_format,
951 .vidioc_g_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
952 .vidioc_s_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
953 .vidioc_try_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
954
955 /* meta output */
956 .vidioc_enum_fmt_meta_out = imgu_meta_enum_format,
957 .vidioc_g_fmt_meta_out = imgu_vidioc_g_meta_fmt,
958 .vidioc_s_fmt_meta_out = imgu_vidioc_g_meta_fmt,
959 .vidioc_try_fmt_meta_out = imgu_vidioc_g_meta_fmt,
960
961 .vidioc_reqbufs = vb2_ioctl_reqbufs,
962 .vidioc_create_bufs = vb2_ioctl_create_bufs,
963 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
964 .vidioc_querybuf = vb2_ioctl_querybuf,
965 .vidioc_qbuf = vb2_ioctl_qbuf,
966 .vidioc_dqbuf = vb2_ioctl_dqbuf,
967 .vidioc_streamon = vb2_ioctl_streamon,
968 .vidioc_streamoff = vb2_ioctl_streamoff,
969 .vidioc_expbuf = vb2_ioctl_expbuf,
970 };
971
imgu_sd_s_ctrl(struct v4l2_ctrl * ctrl)972 static int imgu_sd_s_ctrl(struct v4l2_ctrl *ctrl)
973 {
974 struct imgu_v4l2_subdev *imgu_sd =
975 container_of(ctrl->handler, struct imgu_v4l2_subdev, ctrl_handler);
976 struct imgu_device *imgu = v4l2_get_subdevdata(&imgu_sd->subdev);
977 struct device *dev = &imgu->pci_dev->dev;
978
979 dev_dbg(dev, "set val %d to ctrl 0x%8x for subdev %u",
980 ctrl->val, ctrl->id, imgu_sd->pipe);
981
982 switch (ctrl->id) {
983 case V4L2_CID_INTEL_IPU3_MODE:
984 atomic_set(&imgu_sd->running_mode, ctrl->val);
985 return 0;
986 default:
987 return -EINVAL;
988 }
989 }
990
991 static const struct v4l2_ctrl_ops imgu_subdev_ctrl_ops = {
992 .s_ctrl = imgu_sd_s_ctrl,
993 };
994
995 static const char * const imgu_ctrl_mode_strings[] = {
996 "Video mode",
997 "Still mode",
998 };
999
1000 static const struct v4l2_ctrl_config imgu_subdev_ctrl_mode = {
1001 .ops = &imgu_subdev_ctrl_ops,
1002 .id = V4L2_CID_INTEL_IPU3_MODE,
1003 .name = "IPU3 Pipe Mode",
1004 .type = V4L2_CTRL_TYPE_MENU,
1005 .max = ARRAY_SIZE(imgu_ctrl_mode_strings) - 1,
1006 .def = IPU3_RUNNING_MODE_VIDEO,
1007 .qmenu = imgu_ctrl_mode_strings,
1008 };
1009
1010 /******************** Framework registration ********************/
1011
1012 /* helper function to config node's video properties */
imgu_node_to_v4l2(u32 node,struct video_device * vdev,struct v4l2_format * f)1013 static void imgu_node_to_v4l2(u32 node, struct video_device *vdev,
1014 struct v4l2_format *f)
1015 {
1016 u32 cap;
1017
1018 /* Should not happen */
1019 WARN_ON(node >= IMGU_NODE_NUM);
1020
1021 switch (node) {
1022 case IMGU_NODE_IN:
1023 cap = V4L2_CAP_VIDEO_OUTPUT_MPLANE;
1024 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1025 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops;
1026 break;
1027 case IMGU_NODE_PARAMS:
1028 cap = V4L2_CAP_META_OUTPUT;
1029 f->type = V4L2_BUF_TYPE_META_OUTPUT;
1030 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_PARAMS;
1031 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops;
1032 imgu_css_meta_fmt_set(&f->fmt.meta);
1033 break;
1034 case IMGU_NODE_STAT_3A:
1035 cap = V4L2_CAP_META_CAPTURE;
1036 f->type = V4L2_BUF_TYPE_META_CAPTURE;
1037 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_STAT_3A;
1038 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops;
1039 imgu_css_meta_fmt_set(&f->fmt.meta);
1040 break;
1041 default:
1042 cap = V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1043 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1044 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops;
1045 }
1046
1047 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_IO_MC | cap;
1048 }
1049
imgu_v4l2_subdev_register(struct imgu_device * imgu,struct imgu_v4l2_subdev * imgu_sd,unsigned int pipe)1050 static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
1051 struct imgu_v4l2_subdev *imgu_sd,
1052 unsigned int pipe)
1053 {
1054 int i, r;
1055 struct v4l2_ctrl_handler *hdl = &imgu_sd->ctrl_handler;
1056 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1057
1058 /* Initialize subdev media entity */
1059 r = media_entity_pads_init(&imgu_sd->subdev.entity, IMGU_NODE_NUM,
1060 imgu_sd->subdev_pads);
1061 if (r) {
1062 dev_err(&imgu->pci_dev->dev,
1063 "failed initialize subdev media entity (%d)\n", r);
1064 return r;
1065 }
1066 imgu_sd->subdev.entity.ops = &imgu_media_ops;
1067 for (i = 0; i < IMGU_NODE_NUM; i++) {
1068 imgu_sd->subdev_pads[i].flags = imgu_pipe->nodes[i].output ?
1069 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1070 }
1071
1072 /* Initialize subdev */
1073 v4l2_subdev_init(&imgu_sd->subdev, &imgu_subdev_ops);
1074 imgu_sd->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_STATISTICS;
1075 imgu_sd->subdev.internal_ops = &imgu_subdev_internal_ops;
1076 imgu_sd->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE |
1077 V4L2_SUBDEV_FL_HAS_EVENTS;
1078 snprintf(imgu_sd->subdev.name, sizeof(imgu_sd->subdev.name),
1079 "%s %u", IMGU_NAME, pipe);
1080 v4l2_set_subdevdata(&imgu_sd->subdev, imgu);
1081 atomic_set(&imgu_sd->running_mode, IPU3_RUNNING_MODE_VIDEO);
1082 v4l2_ctrl_handler_init(hdl, 1);
1083 imgu_sd->subdev.ctrl_handler = hdl;
1084 imgu_sd->ctrl = v4l2_ctrl_new_custom(hdl, &imgu_subdev_ctrl_mode, NULL);
1085 if (hdl->error) {
1086 r = hdl->error;
1087 dev_err(&imgu->pci_dev->dev,
1088 "failed to create subdev v4l2 ctrl with err %d", r);
1089 goto fail_subdev;
1090 }
1091 r = v4l2_device_register_subdev(&imgu->v4l2_dev, &imgu_sd->subdev);
1092 if (r) {
1093 dev_err(&imgu->pci_dev->dev,
1094 "failed initialize subdev (%d)\n", r);
1095 goto fail_subdev;
1096 }
1097
1098 imgu_sd->pipe = pipe;
1099 return 0;
1100
1101 fail_subdev:
1102 v4l2_ctrl_handler_free(imgu_sd->subdev.ctrl_handler);
1103 media_entity_cleanup(&imgu_sd->subdev.entity);
1104
1105 return r;
1106 }
1107
imgu_v4l2_node_setup(struct imgu_device * imgu,unsigned int pipe,int node_num)1108 static int imgu_v4l2_node_setup(struct imgu_device *imgu, unsigned int pipe,
1109 int node_num)
1110 {
1111 int r;
1112 u32 flags;
1113 struct v4l2_mbus_framefmt def_bus_fmt = { 0 };
1114 struct v4l2_pix_format_mplane def_pix_fmt = { 0 };
1115 struct device *dev = &imgu->pci_dev->dev;
1116 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1117 struct v4l2_subdev *sd = &imgu_pipe->imgu_sd.subdev;
1118 struct imgu_video_device *node = &imgu_pipe->nodes[node_num];
1119 struct video_device *vdev = &node->vdev;
1120 struct vb2_queue *vbq = &node->vbq;
1121
1122 /* Initialize formats to default values */
1123 def_bus_fmt.width = 1920;
1124 def_bus_fmt.height = 1080;
1125 def_bus_fmt.code = MEDIA_BUS_FMT_FIXED;
1126 def_bus_fmt.field = V4L2_FIELD_NONE;
1127 def_bus_fmt.colorspace = V4L2_COLORSPACE_RAW;
1128 def_bus_fmt.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1129 def_bus_fmt.quantization = V4L2_QUANTIZATION_DEFAULT;
1130 def_bus_fmt.xfer_func = V4L2_XFER_FUNC_DEFAULT;
1131
1132 def_pix_fmt.width = def_bus_fmt.width;
1133 def_pix_fmt.height = def_bus_fmt.height;
1134 def_pix_fmt.field = def_bus_fmt.field;
1135 def_pix_fmt.num_planes = 1;
1136 def_pix_fmt.plane_fmt[0].bytesperline = def_pix_fmt.width * 2;
1137 def_pix_fmt.plane_fmt[0].sizeimage =
1138 def_pix_fmt.height * def_pix_fmt.plane_fmt[0].bytesperline;
1139 def_pix_fmt.flags = 0;
1140 def_pix_fmt.colorspace = def_bus_fmt.colorspace;
1141 def_pix_fmt.ycbcr_enc = def_bus_fmt.ycbcr_enc;
1142 def_pix_fmt.quantization = def_bus_fmt.quantization;
1143 def_pix_fmt.xfer_func = def_bus_fmt.xfer_func;
1144
1145 /* Initialize miscellaneous variables */
1146 mutex_init(&node->lock);
1147 INIT_LIST_HEAD(&node->buffers);
1148
1149 /* Initialize formats to default values */
1150 node->pad_fmt = def_bus_fmt;
1151 node->id = node_num;
1152 node->pipe = pipe;
1153 imgu_node_to_v4l2(node_num, vdev, &node->vdev_fmt);
1154 if (node->vdev_fmt.type ==
1155 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
1156 node->vdev_fmt.type ==
1157 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1158 def_pix_fmt.pixelformat = node->output ?
1159 V4L2_PIX_FMT_IPU3_SGRBG10 :
1160 V4L2_PIX_FMT_NV12;
1161 node->vdev_fmt.fmt.pix_mp = def_pix_fmt;
1162 }
1163
1164 /* Initialize media entities */
1165 r = media_entity_pads_init(&vdev->entity, 1, &node->vdev_pad);
1166 if (r) {
1167 dev_err(dev, "failed initialize media entity (%d)\n", r);
1168 mutex_destroy(&node->lock);
1169 return r;
1170 }
1171 node->vdev_pad.flags = node->output ?
1172 MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK;
1173 vdev->entity.ops = NULL;
1174
1175 /* Initialize vbq */
1176 vbq->type = node->vdev_fmt.type;
1177 vbq->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF;
1178 vbq->ops = &imgu_vb2_ops;
1179 vbq->mem_ops = &vb2_dma_sg_memops;
1180 if (imgu->buf_struct_size <= 0)
1181 imgu->buf_struct_size =
1182 sizeof(struct imgu_vb2_buffer);
1183 vbq->buf_struct_size = imgu->buf_struct_size;
1184 vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1185 /* can streamon w/o buffers */
1186 vbq->min_buffers_needed = 0;
1187 vbq->drv_priv = imgu;
1188 vbq->lock = &node->lock;
1189 r = vb2_queue_init(vbq);
1190 if (r) {
1191 dev_err(dev, "failed to initialize video queue (%d)", r);
1192 media_entity_cleanup(&vdev->entity);
1193 return r;
1194 }
1195
1196 /* Initialize vdev */
1197 snprintf(vdev->name, sizeof(vdev->name), "%s %u %s",
1198 IMGU_NAME, pipe, node->name);
1199 vdev->release = video_device_release_empty;
1200 vdev->fops = &imgu_v4l2_fops;
1201 vdev->lock = &node->lock;
1202 vdev->v4l2_dev = &imgu->v4l2_dev;
1203 vdev->queue = &node->vbq;
1204 vdev->vfl_dir = node->output ? VFL_DIR_TX : VFL_DIR_RX;
1205 video_set_drvdata(vdev, imgu);
1206 r = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1207 if (r) {
1208 dev_err(dev, "failed to register video device (%d)", r);
1209 media_entity_cleanup(&vdev->entity);
1210 return r;
1211 }
1212
1213 /* Create link between video node and the subdev pad */
1214 flags = 0;
1215 if (node->enabled)
1216 flags |= MEDIA_LNK_FL_ENABLED;
1217 if (node->output) {
1218 r = media_create_pad_link(&vdev->entity, 0, &sd->entity,
1219 node_num, flags);
1220 } else {
1221 if (node->id == IMGU_NODE_OUT) {
1222 flags |= MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
1223 node->enabled = true;
1224 }
1225
1226 r = media_create_pad_link(&sd->entity, node_num, &vdev->entity,
1227 0, flags);
1228 }
1229 if (r) {
1230 dev_err(dev, "failed to create pad link (%d)", r);
1231 video_unregister_device(vdev);
1232 return r;
1233 }
1234
1235 return 0;
1236 }
1237
imgu_v4l2_nodes_cleanup_pipe(struct imgu_device * imgu,unsigned int pipe,int node)1238 static void imgu_v4l2_nodes_cleanup_pipe(struct imgu_device *imgu,
1239 unsigned int pipe, int node)
1240 {
1241 int i;
1242 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1243
1244 for (i = 0; i < node; i++) {
1245 video_unregister_device(&imgu_pipe->nodes[i].vdev);
1246 media_entity_cleanup(&imgu_pipe->nodes[i].vdev.entity);
1247 mutex_destroy(&imgu_pipe->nodes[i].lock);
1248 }
1249 }
1250
imgu_v4l2_nodes_setup_pipe(struct imgu_device * imgu,int pipe)1251 static int imgu_v4l2_nodes_setup_pipe(struct imgu_device *imgu, int pipe)
1252 {
1253 int i;
1254
1255 for (i = 0; i < IMGU_NODE_NUM; i++) {
1256 int r = imgu_v4l2_node_setup(imgu, pipe, i);
1257
1258 if (r) {
1259 imgu_v4l2_nodes_cleanup_pipe(imgu, pipe, i);
1260 return r;
1261 }
1262 }
1263 return 0;
1264 }
1265
imgu_v4l2_subdev_cleanup(struct imgu_device * imgu,unsigned int i)1266 static void imgu_v4l2_subdev_cleanup(struct imgu_device *imgu, unsigned int i)
1267 {
1268 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[i];
1269
1270 v4l2_device_unregister_subdev(&imgu_pipe->imgu_sd.subdev);
1271 v4l2_ctrl_handler_free(imgu_pipe->imgu_sd.subdev.ctrl_handler);
1272 media_entity_cleanup(&imgu_pipe->imgu_sd.subdev.entity);
1273 }
1274
imgu_v4l2_cleanup_pipes(struct imgu_device * imgu,unsigned int pipe)1275 static void imgu_v4l2_cleanup_pipes(struct imgu_device *imgu, unsigned int pipe)
1276 {
1277 int i;
1278
1279 for (i = 0; i < pipe; i++) {
1280 imgu_v4l2_nodes_cleanup_pipe(imgu, i, IMGU_NODE_NUM);
1281 imgu_v4l2_subdev_cleanup(imgu, i);
1282 }
1283 }
1284
imgu_v4l2_register_pipes(struct imgu_device * imgu)1285 static int imgu_v4l2_register_pipes(struct imgu_device *imgu)
1286 {
1287 struct imgu_media_pipe *imgu_pipe;
1288 int i, r;
1289
1290 for (i = 0; i < IMGU_MAX_PIPE_NUM; i++) {
1291 imgu_pipe = &imgu->imgu_pipe[i];
1292 r = imgu_v4l2_subdev_register(imgu, &imgu_pipe->imgu_sd, i);
1293 if (r) {
1294 dev_err(&imgu->pci_dev->dev,
1295 "failed to register subdev%u ret (%d)\n", i, r);
1296 goto pipes_cleanup;
1297 }
1298 r = imgu_v4l2_nodes_setup_pipe(imgu, i);
1299 if (r) {
1300 imgu_v4l2_subdev_cleanup(imgu, i);
1301 goto pipes_cleanup;
1302 }
1303 }
1304
1305 return 0;
1306
1307 pipes_cleanup:
1308 imgu_v4l2_cleanup_pipes(imgu, i);
1309 return r;
1310 }
1311
imgu_v4l2_register(struct imgu_device * imgu)1312 int imgu_v4l2_register(struct imgu_device *imgu)
1313 {
1314 int r;
1315
1316 /* Initialize miscellaneous variables */
1317 imgu->streaming = false;
1318
1319 /* Set up media device */
1320 media_device_pci_init(&imgu->media_dev, imgu->pci_dev, IMGU_NAME);
1321
1322 /* Set up v4l2 device */
1323 imgu->v4l2_dev.mdev = &imgu->media_dev;
1324 imgu->v4l2_dev.ctrl_handler = NULL;
1325 r = v4l2_device_register(&imgu->pci_dev->dev, &imgu->v4l2_dev);
1326 if (r) {
1327 dev_err(&imgu->pci_dev->dev,
1328 "failed to register V4L2 device (%d)\n", r);
1329 goto fail_v4l2_dev;
1330 }
1331
1332 r = imgu_v4l2_register_pipes(imgu);
1333 if (r) {
1334 dev_err(&imgu->pci_dev->dev,
1335 "failed to register pipes (%d)\n", r);
1336 goto fail_v4l2_pipes;
1337 }
1338
1339 r = v4l2_device_register_subdev_nodes(&imgu->v4l2_dev);
1340 if (r) {
1341 dev_err(&imgu->pci_dev->dev,
1342 "failed to register subdevs (%d)\n", r);
1343 goto fail_subdevs;
1344 }
1345
1346 r = media_device_register(&imgu->media_dev);
1347 if (r) {
1348 dev_err(&imgu->pci_dev->dev,
1349 "failed to register media device (%d)\n", r);
1350 goto fail_subdevs;
1351 }
1352
1353 return 0;
1354
1355 fail_subdevs:
1356 imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM);
1357 fail_v4l2_pipes:
1358 v4l2_device_unregister(&imgu->v4l2_dev);
1359 fail_v4l2_dev:
1360 media_device_cleanup(&imgu->media_dev);
1361
1362 return r;
1363 }
1364
imgu_v4l2_unregister(struct imgu_device * imgu)1365 int imgu_v4l2_unregister(struct imgu_device *imgu)
1366 {
1367 media_device_unregister(&imgu->media_dev);
1368 imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM);
1369 v4l2_device_unregister(&imgu->v4l2_dev);
1370 media_device_cleanup(&imgu->media_dev);
1371
1372 return 0;
1373 }
1374
imgu_v4l2_buffer_done(struct vb2_buffer * vb,enum vb2_buffer_state state)1375 void imgu_v4l2_buffer_done(struct vb2_buffer *vb,
1376 enum vb2_buffer_state state)
1377 {
1378 struct imgu_vb2_buffer *b =
1379 container_of(vb, struct imgu_vb2_buffer, vbb.vb2_buf);
1380
1381 list_del(&b->list);
1382 vb2_buffer_done(&b->vbb.vb2_buf, state);
1383 }
1384