Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / platform / sh_vou.c
blob4dccf29e9d7817fd0525135590447a931c12e151
1 /*
2 * SuperH Video Output Unit (VOU) driver
4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/dma-mapping.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/fs.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22 #include <linux/videodev2.h>
23 #include <linux/module.h>
25 #include <media/drv-intf/sh_vou.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-mediabus.h>
30 #include <media/videobuf2-v4l2.h>
31 #include <media/videobuf2-dma-contig.h>
33 /* Mirror addresses are not available for all registers */
34 #define VOUER 0
35 #define VOUCR 4
36 #define VOUSTR 8
37 #define VOUVCR 0xc
38 #define VOUISR 0x10
39 #define VOUBCR 0x14
40 #define VOUDPR 0x18
41 #define VOUDSR 0x1c
42 #define VOUVPR 0x20
43 #define VOUIR 0x24
44 #define VOUSRR 0x28
45 #define VOUMSR 0x2c
46 #define VOUHIR 0x30
47 #define VOUDFR 0x34
48 #define VOUAD1R 0x38
49 #define VOUAD2R 0x3c
50 #define VOUAIR 0x40
51 #define VOUSWR 0x44
52 #define VOURCR 0x48
53 #define VOURPR 0x50
55 enum sh_vou_status {
56 SH_VOU_IDLE,
57 SH_VOU_INITIALISING,
58 SH_VOU_RUNNING,
61 #define VOU_MIN_IMAGE_WIDTH 16
62 #define VOU_MAX_IMAGE_WIDTH 720
63 #define VOU_MIN_IMAGE_HEIGHT 16
65 struct sh_vou_buffer {
66 struct vb2_v4l2_buffer vb;
67 struct list_head list;
70 static inline struct
71 sh_vou_buffer *to_sh_vou_buffer(struct vb2_v4l2_buffer *vb2)
73 return container_of(vb2, struct sh_vou_buffer, vb);
76 struct sh_vou_device {
77 struct v4l2_device v4l2_dev;
78 struct video_device vdev;
79 struct sh_vou_pdata *pdata;
80 spinlock_t lock;
81 void __iomem *base;
82 /* State information */
83 struct v4l2_pix_format pix;
84 struct v4l2_rect rect;
85 struct list_head buf_list;
86 v4l2_std_id std;
87 int pix_idx;
88 struct vb2_queue queue;
89 struct sh_vou_buffer *active;
90 enum sh_vou_status status;
91 unsigned sequence;
92 struct mutex fop_lock;
95 /* Register access routines for sides A, B and mirror addresses */
96 static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
97 u32 value)
99 __raw_writel(value, vou_dev->base + reg);
102 static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
103 u32 value)
105 __raw_writel(value, vou_dev->base + reg);
106 __raw_writel(value, vou_dev->base + reg + 0x1000);
109 static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
110 u32 value)
112 __raw_writel(value, vou_dev->base + reg + 0x2000);
115 static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
117 return __raw_readl(vou_dev->base + reg);
120 static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
121 u32 value, u32 mask)
123 u32 old = __raw_readl(vou_dev->base + reg);
125 value = (value & mask) | (old & ~mask);
126 __raw_writel(value, vou_dev->base + reg);
129 static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
130 u32 value, u32 mask)
132 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
135 static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
136 u32 value, u32 mask)
138 sh_vou_reg_a_set(vou_dev, reg, value, mask);
139 sh_vou_reg_b_set(vou_dev, reg, value, mask);
142 struct sh_vou_fmt {
143 u32 pfmt;
144 char *desc;
145 unsigned char bpp;
146 unsigned char bpl;
147 unsigned char rgb;
148 unsigned char yf;
149 unsigned char pkf;
152 /* Further pixel formats can be added */
153 static struct sh_vou_fmt vou_fmt[] = {
155 .pfmt = V4L2_PIX_FMT_NV12,
156 .bpp = 12,
157 .bpl = 1,
158 .desc = "YVU420 planar",
159 .yf = 0,
160 .rgb = 0,
163 .pfmt = V4L2_PIX_FMT_NV16,
164 .bpp = 16,
165 .bpl = 1,
166 .desc = "YVYU planar",
167 .yf = 1,
168 .rgb = 0,
171 .pfmt = V4L2_PIX_FMT_RGB24,
172 .bpp = 24,
173 .bpl = 3,
174 .desc = "RGB24",
175 .pkf = 2,
176 .rgb = 1,
179 .pfmt = V4L2_PIX_FMT_RGB565,
180 .bpp = 16,
181 .bpl = 2,
182 .desc = "RGB565",
183 .pkf = 3,
184 .rgb = 1,
187 .pfmt = V4L2_PIX_FMT_RGB565X,
188 .bpp = 16,
189 .bpl = 2,
190 .desc = "RGB565 byteswapped",
191 .pkf = 3,
192 .rgb = 1,
196 static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
197 struct vb2_v4l2_buffer *vbuf)
199 dma_addr_t addr1, addr2;
201 addr1 = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
202 switch (vou_dev->pix.pixelformat) {
203 case V4L2_PIX_FMT_NV12:
204 case V4L2_PIX_FMT_NV16:
205 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
206 break;
207 default:
208 addr2 = 0;
211 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
212 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
215 static void sh_vou_stream_config(struct sh_vou_device *vou_dev)
217 unsigned int row_coeff;
218 #ifdef __LITTLE_ENDIAN
219 u32 dataswap = 7;
220 #else
221 u32 dataswap = 0;
222 #endif
224 switch (vou_dev->pix.pixelformat) {
225 default:
226 case V4L2_PIX_FMT_NV12:
227 case V4L2_PIX_FMT_NV16:
228 row_coeff = 1;
229 break;
230 case V4L2_PIX_FMT_RGB565:
231 dataswap ^= 1;
232 /* fall through */
233 case V4L2_PIX_FMT_RGB565X:
234 row_coeff = 2;
235 break;
236 case V4L2_PIX_FMT_RGB24:
237 row_coeff = 3;
238 break;
241 sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
242 sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
245 /* Locking: caller holds fop_lock mutex */
246 static int sh_vou_queue_setup(struct vb2_queue *vq,
247 unsigned int *nbuffers, unsigned int *nplanes,
248 unsigned int sizes[], struct device *alloc_devs[])
250 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
251 struct v4l2_pix_format *pix = &vou_dev->pix;
252 int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
254 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
256 if (*nplanes)
257 return sizes[0] < pix->height * bytes_per_line ? -EINVAL : 0;
258 *nplanes = 1;
259 sizes[0] = pix->height * bytes_per_line;
260 return 0;
263 static int sh_vou_buf_prepare(struct vb2_buffer *vb)
265 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue);
266 struct v4l2_pix_format *pix = &vou_dev->pix;
267 unsigned bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
268 unsigned size = pix->height * bytes_per_line;
270 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
272 if (vb2_plane_size(vb, 0) < size) {
273 /* User buffer too small */
274 dev_warn(vou_dev->v4l2_dev.dev, "buffer too small (%lu < %u)\n",
275 vb2_plane_size(vb, 0), size);
276 return -EINVAL;
279 vb2_set_plane_payload(vb, 0, size);
280 return 0;
283 /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
284 static void sh_vou_buf_queue(struct vb2_buffer *vb)
286 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
287 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue);
288 struct sh_vou_buffer *shbuf = to_sh_vou_buffer(vbuf);
289 unsigned long flags;
291 spin_lock_irqsave(&vou_dev->lock, flags);
292 list_add_tail(&shbuf->list, &vou_dev->buf_list);
293 spin_unlock_irqrestore(&vou_dev->lock, flags);
296 static int sh_vou_start_streaming(struct vb2_queue *vq, unsigned int count)
298 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
299 struct sh_vou_buffer *buf, *node;
300 int ret;
302 vou_dev->sequence = 0;
303 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
304 video, s_stream, 1);
305 if (ret < 0 && ret != -ENOIOCTLCMD) {
306 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) {
307 vb2_buffer_done(&buf->vb.vb2_buf,
308 VB2_BUF_STATE_QUEUED);
309 list_del(&buf->list);
311 vou_dev->active = NULL;
312 return ret;
315 buf = list_entry(vou_dev->buf_list.next, struct sh_vou_buffer, list);
317 vou_dev->active = buf;
319 /* Start from side A: we use mirror addresses, so, set B */
320 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
321 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n",
322 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
323 sh_vou_schedule_next(vou_dev, &buf->vb);
325 buf = list_entry(buf->list.next, struct sh_vou_buffer, list);
327 /* Second buffer - initialise register side B */
328 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
329 sh_vou_schedule_next(vou_dev, &buf->vb);
331 /* Register side switching with frame VSYNC */
332 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
334 sh_vou_stream_config(vou_dev);
335 /* Enable End-of-Frame (VSYNC) interrupts */
336 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
338 /* Two buffers on the queue - activate the hardware */
339 vou_dev->status = SH_VOU_RUNNING;
340 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
341 return 0;
344 static void sh_vou_stop_streaming(struct vb2_queue *vq)
346 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
347 struct sh_vou_buffer *buf, *node;
348 unsigned long flags;
350 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
351 video, s_stream, 0);
352 /* disable output */
353 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
354 /* ...but the current frame will complete */
355 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
356 msleep(50);
357 spin_lock_irqsave(&vou_dev->lock, flags);
358 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) {
359 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
360 list_del(&buf->list);
362 vou_dev->active = NULL;
363 spin_unlock_irqrestore(&vou_dev->lock, flags);
366 static const struct vb2_ops sh_vou_qops = {
367 .queue_setup = sh_vou_queue_setup,
368 .buf_prepare = sh_vou_buf_prepare,
369 .buf_queue = sh_vou_buf_queue,
370 .start_streaming = sh_vou_start_streaming,
371 .stop_streaming = sh_vou_stop_streaming,
372 .wait_prepare = vb2_ops_wait_prepare,
373 .wait_finish = vb2_ops_wait_finish,
376 /* Video IOCTLs */
377 static int sh_vou_querycap(struct file *file, void *priv,
378 struct v4l2_capability *cap)
380 struct sh_vou_device *vou_dev = video_drvdata(file);
382 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
384 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
385 strlcpy(cap->driver, "sh-vou", sizeof(cap->driver));
386 strlcpy(cap->bus_info, "platform:sh-vou", sizeof(cap->bus_info));
387 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE |
388 V4L2_CAP_STREAMING;
389 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
390 return 0;
393 /* Enumerate formats, that the device can accept from the user */
394 static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
395 struct v4l2_fmtdesc *fmt)
397 struct sh_vou_device *vou_dev = video_drvdata(file);
399 if (fmt->index >= ARRAY_SIZE(vou_fmt))
400 return -EINVAL;
402 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
404 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
405 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
406 sizeof(fmt->description));
407 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
409 return 0;
412 static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
413 struct v4l2_format *fmt)
415 struct sh_vou_device *vou_dev = video_drvdata(file);
417 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
419 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
420 fmt->fmt.pix = vou_dev->pix;
422 return 0;
425 static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
426 static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
427 static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
428 static const unsigned char vou_scale_v_num[] = {1, 2, 4};
429 static const unsigned char vou_scale_v_den[] = {1, 1, 1};
430 static const unsigned char vou_scale_v_fld[] = {0, 1};
432 static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
433 int pix_idx, int w_idx, int h_idx)
435 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
436 unsigned int black_left, black_top, width_max,
437 frame_in_height, frame_out_height, frame_out_top;
438 struct v4l2_rect *rect = &vou_dev->rect;
439 struct v4l2_pix_format *pix = &vou_dev->pix;
440 u32 vouvcr = 0, dsr_h, dsr_v;
442 if (vou_dev->std & V4L2_STD_525_60) {
443 width_max = 858;
444 /* height_max = 262; */
445 } else {
446 width_max = 864;
447 /* height_max = 312; */
450 frame_in_height = pix->height / 2;
451 frame_out_height = rect->height / 2;
452 frame_out_top = rect->top / 2;
455 * Cropping scheme: max useful image is 720x480, and the total video
456 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
457 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
458 * of which the first 33 / 25 clocks HSYNC must be held active. This
459 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
460 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
461 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
462 * beyond DSR, specified on the left and top by the VPR register "black
463 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
464 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
465 * client requires, and that's exactly what leaves us 720 pixels for the
466 * image; we leave VPR[VVP] at default 20 for now, because the client
467 * doesn't seem to have any special requirements for it. Otherwise we
468 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
469 * the selected standard, and DPR and DSR are selected according to
470 * cropping. Q: how does the client detect the first valid line? Does
471 * HSYNC stay inactive during invalid (black) lines?
473 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
474 black_top = 20;
476 dsr_h = rect->width + rect->left;
477 dsr_v = frame_out_height + frame_out_top;
479 dev_dbg(vou_dev->v4l2_dev.dev,
480 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
481 pix->width, frame_in_height, black_left, black_top,
482 rect->left, frame_out_top, dsr_h, dsr_v);
484 /* VOUISR height - half of a frame height in frame mode */
485 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
486 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
487 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
488 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
491 * if necessary, we could set VOUHIR to
492 * max(black_left + dsr_h, width_max) here
495 if (w_idx)
496 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
497 if (h_idx)
498 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
500 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
502 /* To produce a colour bar for testing set bit 23 of VOUVCR */
503 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
504 sh_vou_reg_ab_write(vou_dev, VOUDFR,
505 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
508 struct sh_vou_geometry {
509 struct v4l2_rect output;
510 unsigned int in_width;
511 unsigned int in_height;
512 int scale_idx_h;
513 int scale_idx_v;
517 * Find input geometry, that we can use to produce output, closest to the
518 * requested rectangle, using VOU scaling
520 static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
522 /* The compiler cannot know, that best and idx will indeed be set */
523 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
524 int i, idx = 0;
526 if (std & V4L2_STD_525_60)
527 img_height_max = 480;
528 else
529 img_height_max = 576;
531 /* Image width must be a multiple of 4 */
532 v4l_bound_align_image(&geo->in_width,
533 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2,
534 &geo->in_height,
535 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
537 /* Select scales to come as close as possible to the output image */
538 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
539 unsigned int err;
540 unsigned int found = geo->output.width * vou_scale_h_den[i] /
541 vou_scale_h_num[i];
543 if (found > VOU_MAX_IMAGE_WIDTH)
544 /* scales increase */
545 break;
547 err = abs(found - geo->in_width);
548 if (err < best_err) {
549 best_err = err;
550 idx = i;
551 best = found;
553 if (!err)
554 break;
557 geo->in_width = best;
558 geo->scale_idx_h = idx;
560 best_err = UINT_MAX;
562 /* This loop can be replaced with one division */
563 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
564 unsigned int err;
565 unsigned int found = geo->output.height * vou_scale_v_den[i] /
566 vou_scale_v_num[i];
568 if (found > img_height_max)
569 /* scales increase */
570 break;
572 err = abs(found - geo->in_height);
573 if (err < best_err) {
574 best_err = err;
575 idx = i;
576 best = found;
578 if (!err)
579 break;
582 geo->in_height = best;
583 geo->scale_idx_v = idx;
587 * Find output geometry, that we can produce, using VOU scaling, closest to
588 * the requested rectangle
590 static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
592 unsigned int best_err = UINT_MAX, best = geo->in_width,
593 width_max, height_max, img_height_max;
594 int i, idx_h = 0, idx_v = 0;
596 if (std & V4L2_STD_525_60) {
597 width_max = 858;
598 height_max = 262 * 2;
599 img_height_max = 480;
600 } else {
601 width_max = 864;
602 height_max = 312 * 2;
603 img_height_max = 576;
606 /* Select scales to come as close as possible to the output image */
607 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
608 unsigned int err;
609 unsigned int found = geo->in_width * vou_scale_h_num[i] /
610 vou_scale_h_den[i];
612 if (found > VOU_MAX_IMAGE_WIDTH)
613 /* scales increase */
614 break;
616 err = abs(found - geo->output.width);
617 if (err < best_err) {
618 best_err = err;
619 idx_h = i;
620 best = found;
622 if (!err)
623 break;
626 geo->output.width = best;
627 geo->scale_idx_h = idx_h;
628 if (geo->output.left + best > width_max)
629 geo->output.left = width_max - best;
631 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
632 vou_scale_h_num[idx_h], vou_scale_h_den[idx_h], best);
634 best_err = UINT_MAX;
636 /* This loop can be replaced with one division */
637 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
638 unsigned int err;
639 unsigned int found = geo->in_height * vou_scale_v_num[i] /
640 vou_scale_v_den[i];
642 if (found > img_height_max)
643 /* scales increase */
644 break;
646 err = abs(found - geo->output.height);
647 if (err < best_err) {
648 best_err = err;
649 idx_v = i;
650 best = found;
652 if (!err)
653 break;
656 geo->output.height = best;
657 geo->scale_idx_v = idx_v;
658 if (geo->output.top + best > height_max)
659 geo->output.top = height_max - best;
661 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
662 vou_scale_v_num[idx_v], vou_scale_v_den[idx_v], best);
665 static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
666 struct v4l2_format *fmt)
668 struct sh_vou_device *vou_dev = video_drvdata(file);
669 struct v4l2_pix_format *pix = &fmt->fmt.pix;
670 unsigned int img_height_max;
671 int pix_idx;
673 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
675 pix->field = V4L2_FIELD_INTERLACED;
676 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
677 pix->ycbcr_enc = pix->quantization = 0;
679 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
680 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
681 break;
683 if (pix_idx == ARRAY_SIZE(vou_fmt))
684 return -EINVAL;
686 if (vou_dev->std & V4L2_STD_525_60)
687 img_height_max = 480;
688 else
689 img_height_max = 576;
691 v4l_bound_align_image(&pix->width,
692 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2,
693 &pix->height,
694 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
695 pix->bytesperline = pix->width * vou_fmt[pix_idx].bpl;
696 pix->sizeimage = pix->height * ((pix->width * vou_fmt[pix_idx].bpp) >> 3);
698 return 0;
701 static int sh_vou_set_fmt_vid_out(struct sh_vou_device *vou_dev,
702 struct v4l2_pix_format *pix)
704 unsigned int img_height_max;
705 struct sh_vou_geometry geo;
706 struct v4l2_subdev_format format = {
707 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
708 /* Revisit: is this the correct code? */
709 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
710 .format.field = V4L2_FIELD_INTERLACED,
711 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
713 struct v4l2_mbus_framefmt *mbfmt = &format.format;
714 int pix_idx;
715 int ret;
717 if (vb2_is_busy(&vou_dev->queue))
718 return -EBUSY;
720 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
721 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
722 break;
724 geo.in_width = pix->width;
725 geo.in_height = pix->height;
726 geo.output = vou_dev->rect;
728 vou_adjust_output(&geo, vou_dev->std);
730 mbfmt->width = geo.output.width;
731 mbfmt->height = geo.output.height;
732 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
733 set_fmt, NULL, &format);
734 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
735 if (ret < 0)
736 return ret;
738 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
739 geo.output.width, geo.output.height, mbfmt->width, mbfmt->height);
741 if (vou_dev->std & V4L2_STD_525_60)
742 img_height_max = 480;
743 else
744 img_height_max = 576;
746 /* Sanity checks */
747 if ((unsigned)mbfmt->width > VOU_MAX_IMAGE_WIDTH ||
748 (unsigned)mbfmt->height > img_height_max ||
749 mbfmt->code != MEDIA_BUS_FMT_YUYV8_2X8)
750 return -EIO;
752 if (mbfmt->width != geo.output.width ||
753 mbfmt->height != geo.output.height) {
754 geo.output.width = mbfmt->width;
755 geo.output.height = mbfmt->height;
757 vou_adjust_input(&geo, vou_dev->std);
760 /* We tried to preserve output rectangle, but it could have changed */
761 vou_dev->rect = geo.output;
762 pix->width = geo.in_width;
763 pix->height = geo.in_height;
765 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
766 pix->width, pix->height);
768 vou_dev->pix_idx = pix_idx;
770 vou_dev->pix = *pix;
772 sh_vou_configure_geometry(vou_dev, pix_idx,
773 geo.scale_idx_h, geo.scale_idx_v);
775 return 0;
778 static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
779 struct v4l2_format *fmt)
781 struct sh_vou_device *vou_dev = video_drvdata(file);
782 int ret = sh_vou_try_fmt_vid_out(file, priv, fmt);
784 if (ret)
785 return ret;
786 return sh_vou_set_fmt_vid_out(vou_dev, &fmt->fmt.pix);
789 static int sh_vou_enum_output(struct file *file, void *fh,
790 struct v4l2_output *a)
792 struct sh_vou_device *vou_dev = video_drvdata(file);
794 if (a->index)
795 return -EINVAL;
796 strlcpy(a->name, "Video Out", sizeof(a->name));
797 a->type = V4L2_OUTPUT_TYPE_ANALOG;
798 a->std = vou_dev->vdev.tvnorms;
799 return 0;
802 static int sh_vou_g_output(struct file *file, void *fh, unsigned int *i)
804 *i = 0;
805 return 0;
808 static int sh_vou_s_output(struct file *file, void *fh, unsigned int i)
810 return i ? -EINVAL : 0;
813 static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
815 switch (bus_fmt) {
816 default:
817 pr_warn("%s(): Invalid bus-format code %d, using default 8-bit\n",
818 __func__, bus_fmt);
819 /* fall through */
820 case SH_VOU_BUS_8BIT:
821 return 1;
822 case SH_VOU_BUS_16BIT:
823 return 0;
824 case SH_VOU_BUS_BT656:
825 return 3;
829 static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
831 struct sh_vou_device *vou_dev = video_drvdata(file);
832 int ret;
834 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
836 if (std_id == vou_dev->std)
837 return 0;
839 if (vb2_is_busy(&vou_dev->queue))
840 return -EBUSY;
842 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
843 s_std_output, std_id);
844 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
845 if (ret < 0 && ret != -ENOIOCTLCMD)
846 return ret;
848 vou_dev->rect.top = vou_dev->rect.left = 0;
849 vou_dev->rect.width = VOU_MAX_IMAGE_WIDTH;
850 if (std_id & V4L2_STD_525_60) {
851 sh_vou_reg_ab_set(vou_dev, VOUCR,
852 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
853 vou_dev->rect.height = 480;
854 } else {
855 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
856 vou_dev->rect.height = 576;
859 vou_dev->pix.width = vou_dev->rect.width;
860 vou_dev->pix.height = vou_dev->rect.height;
861 vou_dev->pix.bytesperline =
862 vou_dev->pix.width * vou_fmt[vou_dev->pix_idx].bpl;
863 vou_dev->pix.sizeimage = vou_dev->pix.height *
864 ((vou_dev->pix.width * vou_fmt[vou_dev->pix_idx].bpp) >> 3);
865 vou_dev->std = std_id;
866 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix);
868 return 0;
871 static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
873 struct sh_vou_device *vou_dev = video_drvdata(file);
875 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
877 *std = vou_dev->std;
879 return 0;
882 static int sh_vou_log_status(struct file *file, void *priv)
884 struct sh_vou_device *vou_dev = video_drvdata(file);
886 pr_info("VOUER: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUER));
887 pr_info("VOUCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUCR));
888 pr_info("VOUSTR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSTR));
889 pr_info("VOUVCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUVCR));
890 pr_info("VOUISR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUISR));
891 pr_info("VOUBCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUBCR));
892 pr_info("VOUDPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDPR));
893 pr_info("VOUDSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDSR));
894 pr_info("VOUVPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUVPR));
895 pr_info("VOUIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUIR));
896 pr_info("VOUSRR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSRR));
897 pr_info("VOUMSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUMSR));
898 pr_info("VOUHIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUHIR));
899 pr_info("VOUDFR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDFR));
900 pr_info("VOUAD1R: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAD1R));
901 pr_info("VOUAD2R: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAD2R));
902 pr_info("VOUAIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAIR));
903 pr_info("VOUSWR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSWR));
904 pr_info("VOURCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOURCR));
905 pr_info("VOURPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOURPR));
906 return 0;
909 static int sh_vou_g_selection(struct file *file, void *fh,
910 struct v4l2_selection *sel)
912 struct sh_vou_device *vou_dev = video_drvdata(file);
914 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
915 return -EINVAL;
916 switch (sel->target) {
917 case V4L2_SEL_TGT_COMPOSE:
918 sel->r = vou_dev->rect;
919 break;
920 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
921 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
922 sel->r.left = 0;
923 sel->r.top = 0;
924 sel->r.width = VOU_MAX_IMAGE_WIDTH;
925 if (vou_dev->std & V4L2_STD_525_60)
926 sel->r.height = 480;
927 else
928 sel->r.height = 576;
929 break;
930 default:
931 return -EINVAL;
933 return 0;
936 /* Assume a dull encoder, do all the work ourselves. */
937 static int sh_vou_s_selection(struct file *file, void *fh,
938 struct v4l2_selection *sel)
940 struct v4l2_rect *rect = &sel->r;
941 struct sh_vou_device *vou_dev = video_drvdata(file);
942 struct v4l2_subdev_selection sd_sel = {
943 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
944 .target = V4L2_SEL_TGT_COMPOSE,
946 struct v4l2_pix_format *pix = &vou_dev->pix;
947 struct sh_vou_geometry geo;
948 struct v4l2_subdev_format format = {
949 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
950 /* Revisit: is this the correct code? */
951 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
952 .format.field = V4L2_FIELD_INTERLACED,
953 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
955 unsigned int img_height_max;
956 int ret;
958 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
959 sel->target != V4L2_SEL_TGT_COMPOSE)
960 return -EINVAL;
962 if (vb2_is_busy(&vou_dev->queue))
963 return -EBUSY;
965 if (vou_dev->std & V4L2_STD_525_60)
966 img_height_max = 480;
967 else
968 img_height_max = 576;
970 v4l_bound_align_image(&rect->width,
971 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 1,
972 &rect->height,
973 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
975 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
976 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
978 if (rect->height + rect->top > img_height_max)
979 rect->top = img_height_max - rect->height;
981 geo.output = *rect;
982 geo.in_width = pix->width;
983 geo.in_height = pix->height;
985 /* Configure the encoder one-to-one, position at 0, ignore errors */
986 sd_sel.r.width = geo.output.width;
987 sd_sel.r.height = geo.output.height;
989 * We first issue a S_SELECTION, so that the subsequent S_FMT delivers the
990 * final encoder configuration.
992 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
993 set_selection, NULL, &sd_sel);
994 format.format.width = geo.output.width;
995 format.format.height = geo.output.height;
996 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
997 set_fmt, NULL, &format);
998 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
999 if (ret < 0)
1000 return ret;
1002 /* Sanity checks */
1003 if ((unsigned)format.format.width > VOU_MAX_IMAGE_WIDTH ||
1004 (unsigned)format.format.height > img_height_max ||
1005 format.format.code != MEDIA_BUS_FMT_YUYV8_2X8)
1006 return -EIO;
1008 geo.output.width = format.format.width;
1009 geo.output.height = format.format.height;
1012 * No down-scaling. According to the API, current call has precedence:
1013 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1015 vou_adjust_input(&geo, vou_dev->std);
1017 /* We tried to preserve output rectangle, but it could have changed */
1018 vou_dev->rect = geo.output;
1019 pix->width = geo.in_width;
1020 pix->height = geo.in_height;
1022 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1023 geo.scale_idx_h, geo.scale_idx_v);
1025 return 0;
1028 static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1030 struct sh_vou_device *vou_dev = dev_id;
1031 static unsigned long j;
1032 struct sh_vou_buffer *vb;
1033 static int cnt;
1034 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1035 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1037 if (!(irq_status & 0x300)) {
1038 if (printk_timed_ratelimit(&j, 500))
1039 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1040 irq_status);
1041 return IRQ_NONE;
1044 spin_lock(&vou_dev->lock);
1045 if (!vou_dev->active || list_empty(&vou_dev->buf_list)) {
1046 if (printk_timed_ratelimit(&j, 500))
1047 dev_warn(vou_dev->v4l2_dev.dev,
1048 "IRQ without active buffer: %x!\n", irq_status);
1049 /* Just ack: buf_release will disable further interrupts */
1050 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1051 spin_unlock(&vou_dev->lock);
1052 return IRQ_HANDLED;
1055 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1056 dev_dbg(vou_dev->v4l2_dev.dev,
1057 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1058 irq_status, masked, vou_status, cnt);
1060 cnt++;
1061 /* side = vou_status & 0x10000; */
1063 /* Clear only set interrupts */
1064 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1066 vb = vou_dev->active;
1067 if (list_is_singular(&vb->list)) {
1068 /* Keep cycling while no next buffer is available */
1069 sh_vou_schedule_next(vou_dev, &vb->vb);
1070 spin_unlock(&vou_dev->lock);
1071 return IRQ_HANDLED;
1074 list_del(&vb->list);
1076 vb->vb.vb2_buf.timestamp = ktime_get_ns();
1077 vb->vb.sequence = vou_dev->sequence++;
1078 vb->vb.field = V4L2_FIELD_INTERLACED;
1079 vb2_buffer_done(&vb->vb.vb2_buf, VB2_BUF_STATE_DONE);
1081 vou_dev->active = list_entry(vou_dev->buf_list.next,
1082 struct sh_vou_buffer, list);
1084 if (list_is_singular(&vou_dev->buf_list)) {
1085 /* Keep cycling while no next buffer is available */
1086 sh_vou_schedule_next(vou_dev, &vou_dev->active->vb);
1087 } else {
1088 struct sh_vou_buffer *new = list_entry(vou_dev->active->list.next,
1089 struct sh_vou_buffer, list);
1090 sh_vou_schedule_next(vou_dev, &new->vb);
1093 spin_unlock(&vou_dev->lock);
1095 return IRQ_HANDLED;
1098 static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1100 struct sh_vou_pdata *pdata = vou_dev->pdata;
1101 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1102 int i = 100;
1104 /* Disable all IRQs */
1105 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1107 /* Reset VOU interfaces - registers unaffected */
1108 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1109 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1110 udelay(1);
1112 if (!i)
1113 return -ETIMEDOUT;
1115 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1117 if (pdata->flags & SH_VOU_PCLK_FALLING)
1118 voucr |= 1 << 28;
1119 if (pdata->flags & SH_VOU_HSYNC_LOW)
1120 voucr |= 1 << 27;
1121 if (pdata->flags & SH_VOU_VSYNC_LOW)
1122 voucr |= 1 << 26;
1123 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1125 /* Manual register side switching at first */
1126 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1127 /* Default - fixed HSYNC length, can be made configurable is required */
1128 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1130 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix);
1132 return 0;
1135 /* File operations */
1136 static int sh_vou_open(struct file *file)
1138 struct sh_vou_device *vou_dev = video_drvdata(file);
1139 int err;
1141 if (mutex_lock_interruptible(&vou_dev->fop_lock))
1142 return -ERESTARTSYS;
1144 err = v4l2_fh_open(file);
1145 if (err)
1146 goto done_open;
1147 if (v4l2_fh_is_singular_file(file) &&
1148 vou_dev->status == SH_VOU_INITIALISING) {
1149 /* First open */
1150 pm_runtime_get_sync(vou_dev->v4l2_dev.dev);
1151 err = sh_vou_hw_init(vou_dev);
1152 if (err < 0) {
1153 pm_runtime_put(vou_dev->v4l2_dev.dev);
1154 v4l2_fh_release(file);
1155 } else {
1156 vou_dev->status = SH_VOU_IDLE;
1159 done_open:
1160 mutex_unlock(&vou_dev->fop_lock);
1161 return err;
1164 static int sh_vou_release(struct file *file)
1166 struct sh_vou_device *vou_dev = video_drvdata(file);
1167 bool is_last;
1169 mutex_lock(&vou_dev->fop_lock);
1170 is_last = v4l2_fh_is_singular_file(file);
1171 _vb2_fop_release(file, NULL);
1172 if (is_last) {
1173 /* Last close */
1174 vou_dev->status = SH_VOU_INITIALISING;
1175 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
1176 pm_runtime_put(vou_dev->v4l2_dev.dev);
1178 mutex_unlock(&vou_dev->fop_lock);
1179 return 0;
1182 /* sh_vou display ioctl operations */
1183 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1184 .vidioc_querycap = sh_vou_querycap,
1185 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1186 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1187 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1188 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1189 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1190 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1191 .vidioc_querybuf = vb2_ioctl_querybuf,
1192 .vidioc_qbuf = vb2_ioctl_qbuf,
1193 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1194 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1195 .vidioc_streamon = vb2_ioctl_streamon,
1196 .vidioc_streamoff = vb2_ioctl_streamoff,
1197 .vidioc_expbuf = vb2_ioctl_expbuf,
1198 .vidioc_g_output = sh_vou_g_output,
1199 .vidioc_s_output = sh_vou_s_output,
1200 .vidioc_enum_output = sh_vou_enum_output,
1201 .vidioc_s_std = sh_vou_s_std,
1202 .vidioc_g_std = sh_vou_g_std,
1203 .vidioc_g_selection = sh_vou_g_selection,
1204 .vidioc_s_selection = sh_vou_s_selection,
1205 .vidioc_log_status = sh_vou_log_status,
1208 static const struct v4l2_file_operations sh_vou_fops = {
1209 .owner = THIS_MODULE,
1210 .open = sh_vou_open,
1211 .release = sh_vou_release,
1212 .unlocked_ioctl = video_ioctl2,
1213 .mmap = vb2_fop_mmap,
1214 .poll = vb2_fop_poll,
1215 .write = vb2_fop_write,
1218 static const struct video_device sh_vou_video_template = {
1219 .name = "sh_vou",
1220 .fops = &sh_vou_fops,
1221 .ioctl_ops = &sh_vou_ioctl_ops,
1222 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
1223 .vfl_dir = VFL_DIR_TX,
1226 static int sh_vou_probe(struct platform_device *pdev)
1228 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1229 struct v4l2_rect *rect;
1230 struct v4l2_pix_format *pix;
1231 struct i2c_adapter *i2c_adap;
1232 struct video_device *vdev;
1233 struct sh_vou_device *vou_dev;
1234 struct resource *reg_res;
1235 struct v4l2_subdev *subdev;
1236 struct vb2_queue *q;
1237 int irq, ret;
1239 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1240 irq = platform_get_irq(pdev, 0);
1242 if (!vou_pdata || !reg_res || irq <= 0) {
1243 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1244 return -ENODEV;
1247 vou_dev = devm_kzalloc(&pdev->dev, sizeof(*vou_dev), GFP_KERNEL);
1248 if (!vou_dev)
1249 return -ENOMEM;
1251 INIT_LIST_HEAD(&vou_dev->buf_list);
1252 spin_lock_init(&vou_dev->lock);
1253 mutex_init(&vou_dev->fop_lock);
1254 vou_dev->pdata = vou_pdata;
1255 vou_dev->status = SH_VOU_INITIALISING;
1256 vou_dev->pix_idx = 1;
1258 rect = &vou_dev->rect;
1259 pix = &vou_dev->pix;
1261 /* Fill in defaults */
1262 vou_dev->std = V4L2_STD_NTSC_M;
1263 rect->left = 0;
1264 rect->top = 0;
1265 rect->width = VOU_MAX_IMAGE_WIDTH;
1266 rect->height = 480;
1267 pix->width = VOU_MAX_IMAGE_WIDTH;
1268 pix->height = 480;
1269 pix->pixelformat = V4L2_PIX_FMT_NV16;
1270 pix->field = V4L2_FIELD_INTERLACED;
1271 pix->bytesperline = VOU_MAX_IMAGE_WIDTH;
1272 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
1273 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1275 vou_dev->base = devm_ioremap_resource(&pdev->dev, reg_res);
1276 if (IS_ERR(vou_dev->base))
1277 return PTR_ERR(vou_dev->base);
1279 ret = devm_request_irq(&pdev->dev, irq, sh_vou_isr, 0, "vou", vou_dev);
1280 if (ret < 0)
1281 return ret;
1283 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1284 if (ret < 0) {
1285 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1286 return ret;
1289 vdev = &vou_dev->vdev;
1290 *vdev = sh_vou_video_template;
1291 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1292 vdev->tvnorms |= V4L2_STD_PAL;
1293 vdev->v4l2_dev = &vou_dev->v4l2_dev;
1294 vdev->release = video_device_release_empty;
1295 vdev->lock = &vou_dev->fop_lock;
1297 video_set_drvdata(vdev, vou_dev);
1299 /* Initialize the vb2 queue */
1300 q = &vou_dev->queue;
1301 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1302 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_WRITE;
1303 q->drv_priv = vou_dev;
1304 q->buf_struct_size = sizeof(struct sh_vou_buffer);
1305 q->ops = &sh_vou_qops;
1306 q->mem_ops = &vb2_dma_contig_memops;
1307 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1308 q->min_buffers_needed = 2;
1309 q->lock = &vou_dev->fop_lock;
1310 q->dev = &pdev->dev;
1311 ret = vb2_queue_init(q);
1312 if (ret)
1313 goto ei2cgadap;
1315 vdev->queue = q;
1316 INIT_LIST_HEAD(&vou_dev->buf_list);
1318 pm_runtime_enable(&pdev->dev);
1319 pm_runtime_resume(&pdev->dev);
1321 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1322 if (!i2c_adap) {
1323 ret = -ENODEV;
1324 goto ei2cgadap;
1327 ret = sh_vou_hw_init(vou_dev);
1328 if (ret < 0)
1329 goto ereset;
1331 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
1332 vou_pdata->board_info, NULL);
1333 if (!subdev) {
1334 ret = -ENOMEM;
1335 goto ei2cnd;
1338 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1339 if (ret < 0)
1340 goto evregdev;
1342 return 0;
1344 evregdev:
1345 ei2cnd:
1346 ereset:
1347 i2c_put_adapter(i2c_adap);
1348 ei2cgadap:
1349 pm_runtime_disable(&pdev->dev);
1350 v4l2_device_unregister(&vou_dev->v4l2_dev);
1351 return ret;
1354 static int sh_vou_remove(struct platform_device *pdev)
1356 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1357 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1358 struct sh_vou_device, v4l2_dev);
1359 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1360 struct v4l2_subdev, list);
1361 struct i2c_client *client = v4l2_get_subdevdata(sd);
1363 pm_runtime_disable(&pdev->dev);
1364 video_unregister_device(&vou_dev->vdev);
1365 i2c_put_adapter(client->adapter);
1366 v4l2_device_unregister(&vou_dev->v4l2_dev);
1367 return 0;
1370 static struct platform_driver __refdata sh_vou = {
1371 .remove = sh_vou_remove,
1372 .driver = {
1373 .name = "sh-vou",
1377 module_platform_driver_probe(sh_vou, sh_vou_probe);
1379 MODULE_DESCRIPTION("SuperH VOU driver");
1380 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1381 MODULE_LICENSE("GPL v2");
1382 MODULE_VERSION("0.1.0");
1383 MODULE_ALIAS("platform:sh-vou");