WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / platform / ti-vpe / cal-video.c
blobdf472a175e839f18e8d727456be10651dffa0454
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TI Camera Access Layer (CAL) - Video Device
5 * Copyright (c) 2015-2020 Texas Instruments Inc.
7 * Authors:
8 * Benoit Parrot <bparrot@ti.com>
9 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 #include <linux/delay.h>
13 #include <linux/ioctl.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/videodev2.h>
17 #include <media/media-device.h>
18 #include <media/v4l2-common.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-fh.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/videobuf2-core.h>
25 #include <media/videobuf2-dma-contig.h>
27 #include "cal.h"
29 /* ------------------------------------------------------------------
30 * Format Handling
31 * ------------------------------------------------------------------
34 static const struct cal_fmt cal_formats[] = {
36 .fourcc = V4L2_PIX_FMT_YUYV,
37 .code = MEDIA_BUS_FMT_YUYV8_2X8,
38 .bpp = 16,
39 }, {
40 .fourcc = V4L2_PIX_FMT_UYVY,
41 .code = MEDIA_BUS_FMT_UYVY8_2X8,
42 .bpp = 16,
43 }, {
44 .fourcc = V4L2_PIX_FMT_YVYU,
45 .code = MEDIA_BUS_FMT_YVYU8_2X8,
46 .bpp = 16,
47 }, {
48 .fourcc = V4L2_PIX_FMT_VYUY,
49 .code = MEDIA_BUS_FMT_VYUY8_2X8,
50 .bpp = 16,
51 }, {
52 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
53 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
54 .bpp = 16,
55 }, {
56 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
57 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
58 .bpp = 16,
59 }, {
60 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
61 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
62 .bpp = 16,
63 }, {
64 .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
65 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
66 .bpp = 16,
67 }, {
68 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
69 .code = MEDIA_BUS_FMT_RGB888_2X12_LE,
70 .bpp = 24,
71 }, {
72 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
73 .code = MEDIA_BUS_FMT_RGB888_2X12_BE,
74 .bpp = 24,
75 }, {
76 .fourcc = V4L2_PIX_FMT_RGB32, /* argb */
77 .code = MEDIA_BUS_FMT_ARGB8888_1X32,
78 .bpp = 32,
79 }, {
80 .fourcc = V4L2_PIX_FMT_SBGGR8,
81 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
82 .bpp = 8,
83 }, {
84 .fourcc = V4L2_PIX_FMT_SGBRG8,
85 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
86 .bpp = 8,
87 }, {
88 .fourcc = V4L2_PIX_FMT_SGRBG8,
89 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
90 .bpp = 8,
91 }, {
92 .fourcc = V4L2_PIX_FMT_SRGGB8,
93 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
94 .bpp = 8,
95 }, {
96 .fourcc = V4L2_PIX_FMT_SBGGR10,
97 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
98 .bpp = 10,
99 }, {
100 .fourcc = V4L2_PIX_FMT_SGBRG10,
101 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
102 .bpp = 10,
103 }, {
104 .fourcc = V4L2_PIX_FMT_SGRBG10,
105 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
106 .bpp = 10,
107 }, {
108 .fourcc = V4L2_PIX_FMT_SRGGB10,
109 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
110 .bpp = 10,
111 }, {
112 .fourcc = V4L2_PIX_FMT_SBGGR12,
113 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
114 .bpp = 12,
115 }, {
116 .fourcc = V4L2_PIX_FMT_SGBRG12,
117 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
118 .bpp = 12,
119 }, {
120 .fourcc = V4L2_PIX_FMT_SGRBG12,
121 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
122 .bpp = 12,
123 }, {
124 .fourcc = V4L2_PIX_FMT_SRGGB12,
125 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
126 .bpp = 12,
130 /* Print Four-character-code (FOURCC) */
131 static char *fourcc_to_str(u32 fmt)
133 static char code[5];
135 code[0] = (unsigned char)(fmt & 0xff);
136 code[1] = (unsigned char)((fmt >> 8) & 0xff);
137 code[2] = (unsigned char)((fmt >> 16) & 0xff);
138 code[3] = (unsigned char)((fmt >> 24) & 0xff);
139 code[4] = '\0';
141 return code;
144 /* ------------------------------------------------------------------
145 * V4L2 Video IOCTLs
146 * ------------------------------------------------------------------
149 static const struct cal_fmt *find_format_by_pix(struct cal_ctx *ctx,
150 u32 pixelformat)
152 const struct cal_fmt *fmt;
153 unsigned int k;
155 for (k = 0; k < ctx->num_active_fmt; k++) {
156 fmt = ctx->active_fmt[k];
157 if (fmt->fourcc == pixelformat)
158 return fmt;
161 return NULL;
164 static const struct cal_fmt *find_format_by_code(struct cal_ctx *ctx,
165 u32 code)
167 const struct cal_fmt *fmt;
168 unsigned int k;
170 for (k = 0; k < ctx->num_active_fmt; k++) {
171 fmt = ctx->active_fmt[k];
172 if (fmt->code == code)
173 return fmt;
176 return NULL;
179 static int cal_querycap(struct file *file, void *priv,
180 struct v4l2_capability *cap)
182 struct cal_ctx *ctx = video_drvdata(file);
184 strscpy(cap->driver, CAL_MODULE_NAME, sizeof(cap->driver));
185 strscpy(cap->card, CAL_MODULE_NAME, sizeof(cap->card));
187 snprintf(cap->bus_info, sizeof(cap->bus_info),
188 "platform:%s", dev_name(ctx->cal->dev));
189 return 0;
192 static int cal_enum_fmt_vid_cap(struct file *file, void *priv,
193 struct v4l2_fmtdesc *f)
195 struct cal_ctx *ctx = video_drvdata(file);
196 const struct cal_fmt *fmt;
198 if (f->index >= ctx->num_active_fmt)
199 return -EINVAL;
201 fmt = ctx->active_fmt[f->index];
203 f->pixelformat = fmt->fourcc;
204 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
205 return 0;
208 static int __subdev_get_format(struct cal_ctx *ctx,
209 struct v4l2_mbus_framefmt *fmt)
211 struct v4l2_subdev_format sd_fmt;
212 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
213 int ret;
215 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
216 sd_fmt.pad = 0;
218 ret = v4l2_subdev_call(ctx->phy->sensor, pad, get_fmt, NULL, &sd_fmt);
219 if (ret)
220 return ret;
222 *fmt = *mbus_fmt;
224 ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
225 fmt->width, fmt->height, fmt->code);
227 return 0;
230 static int __subdev_set_format(struct cal_ctx *ctx,
231 struct v4l2_mbus_framefmt *fmt)
233 struct v4l2_subdev_format sd_fmt;
234 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
235 int ret;
237 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
238 sd_fmt.pad = 0;
239 *mbus_fmt = *fmt;
241 ret = v4l2_subdev_call(ctx->phy->sensor, pad, set_fmt, NULL, &sd_fmt);
242 if (ret)
243 return ret;
245 ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
246 fmt->width, fmt->height, fmt->code);
248 return 0;
251 static int cal_calc_format_size(struct cal_ctx *ctx,
252 const struct cal_fmt *fmt,
253 struct v4l2_format *f)
255 u32 bpl, max_width;
257 if (!fmt) {
258 ctx_dbg(3, ctx, "No cal_fmt provided!\n");
259 return -EINVAL;
263 * Maximum width is bound by the DMA max width in bytes.
264 * We need to recalculate the actual maxi width depending on the
265 * number of bytes per pixels required.
267 max_width = MAX_WIDTH_BYTES / (ALIGN(fmt->bpp, 8) >> 3);
268 v4l_bound_align_image(&f->fmt.pix.width, 48, max_width, 2,
269 &f->fmt.pix.height, 32, MAX_HEIGHT_LINES, 0, 0);
271 bpl = (f->fmt.pix.width * ALIGN(fmt->bpp, 8)) >> 3;
272 f->fmt.pix.bytesperline = ALIGN(bpl, 16);
274 f->fmt.pix.sizeimage = f->fmt.pix.height *
275 f->fmt.pix.bytesperline;
277 ctx_dbg(3, ctx, "%s: fourcc: %s size: %dx%d bpl:%d img_size:%d\n",
278 __func__, fourcc_to_str(f->fmt.pix.pixelformat),
279 f->fmt.pix.width, f->fmt.pix.height,
280 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
282 return 0;
285 static int cal_g_fmt_vid_cap(struct file *file, void *priv,
286 struct v4l2_format *f)
288 struct cal_ctx *ctx = video_drvdata(file);
290 *f = ctx->v_fmt;
292 return 0;
295 static int cal_try_fmt_vid_cap(struct file *file, void *priv,
296 struct v4l2_format *f)
298 struct cal_ctx *ctx = video_drvdata(file);
299 const struct cal_fmt *fmt;
300 struct v4l2_subdev_frame_size_enum fse;
301 int ret, found;
303 fmt = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
304 if (!fmt) {
305 ctx_dbg(3, ctx, "Fourcc format (0x%08x) not found.\n",
306 f->fmt.pix.pixelformat);
308 /* Just get the first one enumerated */
309 fmt = ctx->active_fmt[0];
310 f->fmt.pix.pixelformat = fmt->fourcc;
313 f->fmt.pix.field = ctx->v_fmt.fmt.pix.field;
315 /* check for/find a valid width/height */
316 ret = 0;
317 found = false;
318 fse.pad = 0;
319 fse.code = fmt->code;
320 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
321 for (fse.index = 0; ; fse.index++) {
322 ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_size,
323 NULL, &fse);
324 if (ret)
325 break;
327 if ((f->fmt.pix.width == fse.max_width) &&
328 (f->fmt.pix.height == fse.max_height)) {
329 found = true;
330 break;
331 } else if ((f->fmt.pix.width >= fse.min_width) &&
332 (f->fmt.pix.width <= fse.max_width) &&
333 (f->fmt.pix.height >= fse.min_height) &&
334 (f->fmt.pix.height <= fse.max_height)) {
335 found = true;
336 break;
340 if (!found) {
341 /* use existing values as default */
342 f->fmt.pix.width = ctx->v_fmt.fmt.pix.width;
343 f->fmt.pix.height = ctx->v_fmt.fmt.pix.height;
347 * Use current colorspace for now, it will get
348 * updated properly during s_fmt
350 f->fmt.pix.colorspace = ctx->v_fmt.fmt.pix.colorspace;
351 return cal_calc_format_size(ctx, fmt, f);
354 static int cal_s_fmt_vid_cap(struct file *file, void *priv,
355 struct v4l2_format *f)
357 struct cal_ctx *ctx = video_drvdata(file);
358 struct vb2_queue *q = &ctx->vb_vidq;
359 const struct cal_fmt *fmt;
360 struct v4l2_mbus_framefmt mbus_fmt;
361 int ret;
363 if (vb2_is_busy(q)) {
364 ctx_dbg(3, ctx, "%s device busy\n", __func__);
365 return -EBUSY;
368 ret = cal_try_fmt_vid_cap(file, priv, f);
369 if (ret < 0)
370 return ret;
372 fmt = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
374 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, fmt->code);
376 ret = __subdev_set_format(ctx, &mbus_fmt);
377 if (ret)
378 return ret;
380 /* Just double check nothing has gone wrong */
381 if (mbus_fmt.code != fmt->code) {
382 ctx_dbg(3, ctx,
383 "%s subdev changed format on us, this should not happen\n",
384 __func__);
385 return -EINVAL;
388 v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
389 ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
390 ctx->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
391 cal_calc_format_size(ctx, fmt, &ctx->v_fmt);
392 ctx->fmt = fmt;
393 ctx->m_fmt = mbus_fmt;
394 *f = ctx->v_fmt;
396 return 0;
399 static int cal_enum_framesizes(struct file *file, void *fh,
400 struct v4l2_frmsizeenum *fsize)
402 struct cal_ctx *ctx = video_drvdata(file);
403 const struct cal_fmt *fmt;
404 struct v4l2_subdev_frame_size_enum fse;
405 int ret;
407 /* check for valid format */
408 fmt = find_format_by_pix(ctx, fsize->pixel_format);
409 if (!fmt) {
410 ctx_dbg(3, ctx, "Invalid pixel code: %x\n",
411 fsize->pixel_format);
412 return -EINVAL;
415 fse.index = fsize->index;
416 fse.pad = 0;
417 fse.code = fmt->code;
418 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
420 ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_size, NULL,
421 &fse);
422 if (ret)
423 return ret;
425 ctx_dbg(1, ctx, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
426 __func__, fse.index, fse.code, fse.min_width, fse.max_width,
427 fse.min_height, fse.max_height);
429 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
430 fsize->discrete.width = fse.max_width;
431 fsize->discrete.height = fse.max_height;
433 return 0;
436 static int cal_enum_input(struct file *file, void *priv,
437 struct v4l2_input *inp)
439 if (inp->index > 0)
440 return -EINVAL;
442 inp->type = V4L2_INPUT_TYPE_CAMERA;
443 sprintf(inp->name, "Camera %u", inp->index);
444 return 0;
447 static int cal_g_input(struct file *file, void *priv, unsigned int *i)
449 *i = 0;
450 return 0;
453 static int cal_s_input(struct file *file, void *priv, unsigned int i)
455 return i > 0 ? -EINVAL : 0;
458 /* timeperframe is arbitrary and continuous */
459 static int cal_enum_frameintervals(struct file *file, void *priv,
460 struct v4l2_frmivalenum *fival)
462 struct cal_ctx *ctx = video_drvdata(file);
463 const struct cal_fmt *fmt;
464 struct v4l2_subdev_frame_interval_enum fie = {
465 .index = fival->index,
466 .width = fival->width,
467 .height = fival->height,
468 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
470 int ret;
472 fmt = find_format_by_pix(ctx, fival->pixel_format);
473 if (!fmt)
474 return -EINVAL;
476 fie.code = fmt->code;
477 ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_frame_interval,
478 NULL, &fie);
479 if (ret)
480 return ret;
481 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
482 fival->discrete = fie.interval;
484 return 0;
487 static const struct v4l2_file_operations cal_fops = {
488 .owner = THIS_MODULE,
489 .open = v4l2_fh_open,
490 .release = vb2_fop_release,
491 .read = vb2_fop_read,
492 .poll = vb2_fop_poll,
493 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
494 .mmap = vb2_fop_mmap,
497 static const struct v4l2_ioctl_ops cal_ioctl_ops = {
498 .vidioc_querycap = cal_querycap,
499 .vidioc_enum_fmt_vid_cap = cal_enum_fmt_vid_cap,
500 .vidioc_g_fmt_vid_cap = cal_g_fmt_vid_cap,
501 .vidioc_try_fmt_vid_cap = cal_try_fmt_vid_cap,
502 .vidioc_s_fmt_vid_cap = cal_s_fmt_vid_cap,
503 .vidioc_enum_framesizes = cal_enum_framesizes,
504 .vidioc_reqbufs = vb2_ioctl_reqbufs,
505 .vidioc_create_bufs = vb2_ioctl_create_bufs,
506 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
507 .vidioc_querybuf = vb2_ioctl_querybuf,
508 .vidioc_qbuf = vb2_ioctl_qbuf,
509 .vidioc_dqbuf = vb2_ioctl_dqbuf,
510 .vidioc_expbuf = vb2_ioctl_expbuf,
511 .vidioc_enum_input = cal_enum_input,
512 .vidioc_g_input = cal_g_input,
513 .vidioc_s_input = cal_s_input,
514 .vidioc_enum_frameintervals = cal_enum_frameintervals,
515 .vidioc_streamon = vb2_ioctl_streamon,
516 .vidioc_streamoff = vb2_ioctl_streamoff,
517 .vidioc_log_status = v4l2_ctrl_log_status,
518 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
519 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
522 /* ------------------------------------------------------------------
523 * videobuf2 Operations
524 * ------------------------------------------------------------------
527 static int cal_queue_setup(struct vb2_queue *vq,
528 unsigned int *nbuffers, unsigned int *nplanes,
529 unsigned int sizes[], struct device *alloc_devs[])
531 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
532 unsigned int size = ctx->v_fmt.fmt.pix.sizeimage;
534 if (vq->num_buffers + *nbuffers < 3)
535 *nbuffers = 3 - vq->num_buffers;
537 if (*nplanes) {
538 if (sizes[0] < size)
539 return -EINVAL;
540 size = sizes[0];
543 *nplanes = 1;
544 sizes[0] = size;
546 ctx_dbg(3, ctx, "nbuffers=%d, size=%d\n", *nbuffers, sizes[0]);
548 return 0;
551 static int cal_buffer_prepare(struct vb2_buffer *vb)
553 struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
554 struct cal_buffer *buf = container_of(vb, struct cal_buffer,
555 vb.vb2_buf);
556 unsigned long size;
558 if (WARN_ON(!ctx->fmt))
559 return -EINVAL;
561 size = ctx->v_fmt.fmt.pix.sizeimage;
562 if (vb2_plane_size(vb, 0) < size) {
563 ctx_err(ctx,
564 "data will not fit into plane (%lu < %lu)\n",
565 vb2_plane_size(vb, 0), size);
566 return -EINVAL;
569 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
570 return 0;
573 static void cal_buffer_queue(struct vb2_buffer *vb)
575 struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
576 struct cal_buffer *buf = container_of(vb, struct cal_buffer,
577 vb.vb2_buf);
578 struct cal_dmaqueue *vidq = &ctx->vidq;
579 unsigned long flags;
581 /* recheck locking */
582 spin_lock_irqsave(&ctx->slock, flags);
583 list_add_tail(&buf->list, &vidq->active);
584 spin_unlock_irqrestore(&ctx->slock, flags);
587 static int cal_start_streaming(struct vb2_queue *vq, unsigned int count)
589 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
590 struct cal_dmaqueue *dma_q = &ctx->vidq;
591 struct cal_buffer *buf, *tmp;
592 unsigned long addr;
593 unsigned long flags;
594 int ret;
596 spin_lock_irqsave(&ctx->slock, flags);
597 if (list_empty(&dma_q->active)) {
598 spin_unlock_irqrestore(&ctx->slock, flags);
599 ctx_dbg(3, ctx, "buffer queue is empty\n");
600 return -EIO;
603 buf = list_entry(dma_q->active.next, struct cal_buffer, list);
604 ctx->cur_frm = buf;
605 ctx->next_frm = buf;
606 list_del(&buf->list);
607 spin_unlock_irqrestore(&ctx->slock, flags);
609 addr = vb2_dma_contig_plane_dma_addr(&ctx->cur_frm->vb.vb2_buf, 0);
610 ctx->sequence = 0;
612 pm_runtime_get_sync(ctx->cal->dev);
614 cal_ctx_csi2_config(ctx);
615 cal_ctx_pix_proc_config(ctx);
616 cal_ctx_wr_dma_config(ctx, ctx->v_fmt.fmt.pix.bytesperline,
617 ctx->v_fmt.fmt.pix.height);
619 cal_camerarx_enable_irqs(ctx->phy);
621 ret = cal_camerarx_start(ctx->phy, ctx->fmt);
622 if (ret)
623 goto err;
625 cal_ctx_wr_dma_addr(ctx, addr);
626 cal_camerarx_ppi_enable(ctx->phy);
628 if (cal_debug >= 4)
629 cal_quickdump_regs(ctx->cal);
631 return 0;
633 err:
634 spin_lock_irqsave(&ctx->slock, flags);
635 vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
636 ctx->cur_frm = NULL;
637 ctx->next_frm = NULL;
638 list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
639 list_del(&buf->list);
640 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
642 spin_unlock_irqrestore(&ctx->slock, flags);
643 return ret;
646 static void cal_stop_streaming(struct vb2_queue *vq)
648 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
649 struct cal_dmaqueue *dma_q = &ctx->vidq;
650 struct cal_buffer *buf, *tmp;
651 unsigned long timeout;
652 unsigned long flags;
653 bool dma_act;
655 cal_camerarx_ppi_disable(ctx->phy);
657 /* wait for stream and dma to finish */
658 dma_act = true;
659 timeout = jiffies + msecs_to_jiffies(500);
660 while (dma_act && time_before(jiffies, timeout)) {
661 msleep(50);
663 spin_lock_irqsave(&ctx->slock, flags);
664 dma_act = ctx->dma_act;
665 spin_unlock_irqrestore(&ctx->slock, flags);
668 if (dma_act)
669 ctx_err(ctx, "failed to disable dma cleanly\n");
671 cal_camerarx_disable_irqs(ctx->phy);
672 cal_camerarx_stop(ctx->phy);
674 /* Release all active buffers */
675 spin_lock_irqsave(&ctx->slock, flags);
676 list_for_each_entry_safe(buf, tmp, &dma_q->active, list) {
677 list_del(&buf->list);
678 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
681 if (ctx->cur_frm == ctx->next_frm) {
682 vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
683 } else {
684 vb2_buffer_done(&ctx->cur_frm->vb.vb2_buf, VB2_BUF_STATE_ERROR);
685 vb2_buffer_done(&ctx->next_frm->vb.vb2_buf,
686 VB2_BUF_STATE_ERROR);
688 ctx->cur_frm = NULL;
689 ctx->next_frm = NULL;
690 spin_unlock_irqrestore(&ctx->slock, flags);
692 pm_runtime_put_sync(ctx->cal->dev);
695 static const struct vb2_ops cal_video_qops = {
696 .queue_setup = cal_queue_setup,
697 .buf_prepare = cal_buffer_prepare,
698 .buf_queue = cal_buffer_queue,
699 .start_streaming = cal_start_streaming,
700 .stop_streaming = cal_stop_streaming,
701 .wait_prepare = vb2_ops_wait_prepare,
702 .wait_finish = vb2_ops_wait_finish,
705 /* ------------------------------------------------------------------
706 * V4L2 Initialization and Registration
707 * ------------------------------------------------------------------
710 static const struct video_device cal_videodev = {
711 .name = CAL_MODULE_NAME,
712 .fops = &cal_fops,
713 .ioctl_ops = &cal_ioctl_ops,
714 .minor = -1,
715 .release = video_device_release_empty,
716 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
717 V4L2_CAP_READWRITE,
720 static int cal_ctx_v4l2_init_formats(struct cal_ctx *ctx)
722 struct v4l2_subdev_mbus_code_enum mbus_code;
723 struct v4l2_mbus_framefmt mbus_fmt;
724 const struct cal_fmt *fmt;
725 unsigned int i, j, k;
726 int ret = 0;
728 /* Enumerate sub device formats and enable all matching local formats */
729 ctx->active_fmt = devm_kcalloc(ctx->cal->dev, ARRAY_SIZE(cal_formats),
730 sizeof(*ctx->active_fmt), GFP_KERNEL);
731 ctx->num_active_fmt = 0;
733 for (j = 0, i = 0; ret != -EINVAL; ++j) {
735 memset(&mbus_code, 0, sizeof(mbus_code));
736 mbus_code.index = j;
737 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
738 ret = v4l2_subdev_call(ctx->phy->sensor, pad, enum_mbus_code,
739 NULL, &mbus_code);
740 if (ret)
741 continue;
743 ctx_dbg(2, ctx,
744 "subdev %s: code: %04x idx: %u\n",
745 ctx->phy->sensor->name, mbus_code.code, j);
747 for (k = 0; k < ARRAY_SIZE(cal_formats); k++) {
748 const struct cal_fmt *fmt = &cal_formats[k];
750 if (mbus_code.code == fmt->code) {
751 ctx->active_fmt[i] = fmt;
752 ctx_dbg(2, ctx,
753 "matched fourcc: %s: code: %04x idx: %u\n",
754 fourcc_to_str(fmt->fourcc),
755 fmt->code, i);
756 ctx->num_active_fmt = ++i;
761 if (i == 0) {
762 ctx_err(ctx, "No suitable format reported by subdev %s\n",
763 ctx->phy->sensor->name);
764 return -EINVAL;
767 ret = __subdev_get_format(ctx, &mbus_fmt);
768 if (ret)
769 return ret;
771 fmt = find_format_by_code(ctx, mbus_fmt.code);
772 if (!fmt) {
773 ctx_dbg(3, ctx, "mbus code format (0x%08x) not found.\n",
774 mbus_fmt.code);
775 return -EINVAL;
778 /* Save current subdev format */
779 v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
780 ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
781 ctx->v_fmt.fmt.pix.pixelformat = fmt->fourcc;
782 cal_calc_format_size(ctx, fmt, &ctx->v_fmt);
783 ctx->fmt = fmt;
784 ctx->m_fmt = mbus_fmt;
786 return 0;
789 int cal_ctx_v4l2_register(struct cal_ctx *ctx)
791 struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;
792 struct video_device *vfd = &ctx->vdev;
793 int ret;
795 ret = cal_ctx_v4l2_init_formats(ctx);
796 if (ret)
797 return ret;
799 ret = v4l2_ctrl_add_handler(hdl, ctx->phy->sensor->ctrl_handler, NULL,
800 true);
801 if (ret < 0) {
802 ctx_err(ctx, "Failed to add sensor ctrl handler\n");
803 return ret;
806 ret = video_register_device(vfd, VFL_TYPE_VIDEO, cal_video_nr);
807 if (ret < 0) {
808 ctx_err(ctx, "Failed to register video device\n");
809 return ret;
812 ctx_info(ctx, "V4L2 device registered as %s\n",
813 video_device_node_name(vfd));
815 return 0;
818 void cal_ctx_v4l2_unregister(struct cal_ctx *ctx)
820 ctx_dbg(1, ctx, "unregistering %s\n",
821 video_device_node_name(&ctx->vdev));
823 video_unregister_device(&ctx->vdev);
826 int cal_ctx_v4l2_init(struct cal_ctx *ctx)
828 struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;
829 struct video_device *vfd = &ctx->vdev;
830 struct vb2_queue *q = &ctx->vb_vidq;
831 int ret;
833 INIT_LIST_HEAD(&ctx->vidq.active);
834 spin_lock_init(&ctx->slock);
835 mutex_init(&ctx->mutex);
837 /* Initialize the vb2 queue. */
838 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
839 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
840 q->drv_priv = ctx;
841 q->buf_struct_size = sizeof(struct cal_buffer);
842 q->ops = &cal_video_qops;
843 q->mem_ops = &vb2_dma_contig_memops;
844 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
845 q->lock = &ctx->mutex;
846 q->min_buffers_needed = 3;
847 q->dev = ctx->cal->dev;
849 ret = vb2_queue_init(q);
850 if (ret)
851 return ret;
853 /* Initialize the video device and media entity. */
854 *vfd = cal_videodev;
855 vfd->v4l2_dev = &ctx->cal->v4l2_dev;
856 vfd->queue = q;
857 snprintf(vfd->name, sizeof(vfd->name), "CAL output %u", ctx->index);
858 vfd->lock = &ctx->mutex;
859 video_set_drvdata(vfd, ctx);
861 ctx->pad.flags = MEDIA_PAD_FL_SINK;
862 ret = media_entity_pads_init(&vfd->entity, 1, &ctx->pad);
863 if (ret < 0)
864 return ret;
866 /* Initialize the control handler. */
867 ret = v4l2_ctrl_handler_init(hdl, 11);
868 if (ret < 0) {
869 ctx_err(ctx, "Failed to init ctrl handler\n");
870 goto error;
873 vfd->ctrl_handler = hdl;
875 return 0;
877 error:
878 media_entity_cleanup(&vfd->entity);
879 return ret;
882 void cal_ctx_v4l2_cleanup(struct cal_ctx *ctx)
884 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
885 media_entity_cleanup(&ctx->vdev.entity);