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.
11 #include <linux/dma-mapping.h>
12 #include <linux/delay.h>
13 #include <linux/errno.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/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-dma-contig.h>
32 /* Mirror addresses are not available for all registers */
60 #define VOU_MIN_IMAGE_WIDTH 16
61 #define VOU_MAX_IMAGE_WIDTH 720
62 #define VOU_MIN_IMAGE_HEIGHT 16
64 struct sh_vou_buffer
{
66 struct list_head list
;
69 static inline struct sh_vou_buffer
*to_sh_vou_buffer(struct vb2_buffer
*vb2
)
71 return container_of(vb2
, struct sh_vou_buffer
, vb
);
74 struct sh_vou_device
{
75 struct v4l2_device v4l2_dev
;
76 struct video_device vdev
;
77 struct sh_vou_pdata
*pdata
;
80 /* State information */
81 struct v4l2_pix_format pix
;
82 struct v4l2_rect rect
;
83 struct list_head buf_list
;
86 struct vb2_queue queue
;
87 struct vb2_alloc_ctx
*alloc_ctx
;
88 struct sh_vou_buffer
*active
;
89 enum sh_vou_status status
;
91 struct mutex fop_lock
;
94 /* Register access routines for sides A, B and mirror addresses */
95 static void sh_vou_reg_a_write(struct sh_vou_device
*vou_dev
, unsigned int reg
,
98 __raw_writel(value
, vou_dev
->base
+ reg
);
101 static void sh_vou_reg_ab_write(struct sh_vou_device
*vou_dev
, unsigned int reg
,
104 __raw_writel(value
, vou_dev
->base
+ reg
);
105 __raw_writel(value
, vou_dev
->base
+ reg
+ 0x1000);
108 static void sh_vou_reg_m_write(struct sh_vou_device
*vou_dev
, unsigned int reg
,
111 __raw_writel(value
, vou_dev
->base
+ reg
+ 0x2000);
114 static u32
sh_vou_reg_a_read(struct sh_vou_device
*vou_dev
, unsigned int reg
)
116 return __raw_readl(vou_dev
->base
+ reg
);
119 static void sh_vou_reg_a_set(struct sh_vou_device
*vou_dev
, unsigned int reg
,
122 u32 old
= __raw_readl(vou_dev
->base
+ reg
);
124 value
= (value
& mask
) | (old
& ~mask
);
125 __raw_writel(value
, vou_dev
->base
+ reg
);
128 static void sh_vou_reg_b_set(struct sh_vou_device
*vou_dev
, unsigned int reg
,
131 sh_vou_reg_a_set(vou_dev
, reg
+ 0x1000, value
, mask
);
134 static void sh_vou_reg_ab_set(struct sh_vou_device
*vou_dev
, unsigned int reg
,
137 sh_vou_reg_a_set(vou_dev
, reg
, value
, mask
);
138 sh_vou_reg_b_set(vou_dev
, reg
, value
, mask
);
151 /* Further pixel formats can be added */
152 static struct sh_vou_fmt vou_fmt
[] = {
154 .pfmt
= V4L2_PIX_FMT_NV12
,
157 .desc
= "YVU420 planar",
162 .pfmt
= V4L2_PIX_FMT_NV16
,
165 .desc
= "YVYU planar",
170 .pfmt
= V4L2_PIX_FMT_RGB24
,
178 .pfmt
= V4L2_PIX_FMT_RGB565
,
186 .pfmt
= V4L2_PIX_FMT_RGB565X
,
189 .desc
= "RGB565 byteswapped",
195 static void sh_vou_schedule_next(struct sh_vou_device
*vou_dev
,
196 struct vb2_buffer
*vb
)
198 dma_addr_t addr1
, addr2
;
200 addr1
= vb2_dma_contig_plane_dma_addr(vb
, 0);
201 switch (vou_dev
->pix
.pixelformat
) {
202 case V4L2_PIX_FMT_NV12
:
203 case V4L2_PIX_FMT_NV16
:
204 addr2
= addr1
+ vou_dev
->pix
.width
* vou_dev
->pix
.height
;
210 sh_vou_reg_m_write(vou_dev
, VOUAD1R
, addr1
);
211 sh_vou_reg_m_write(vou_dev
, VOUAD2R
, addr2
);
214 static void sh_vou_stream_config(struct sh_vou_device
*vou_dev
)
216 unsigned int row_coeff
;
217 #ifdef __LITTLE_ENDIAN
223 switch (vou_dev
->pix
.pixelformat
) {
225 case V4L2_PIX_FMT_NV12
:
226 case V4L2_PIX_FMT_NV16
:
229 case V4L2_PIX_FMT_RGB565
:
231 case V4L2_PIX_FMT_RGB565X
:
234 case V4L2_PIX_FMT_RGB24
:
239 sh_vou_reg_a_write(vou_dev
, VOUSWR
, dataswap
);
240 sh_vou_reg_ab_write(vou_dev
, VOUAIR
, vou_dev
->pix
.width
* row_coeff
);
243 /* Locking: caller holds fop_lock mutex */
244 static int sh_vou_queue_setup(struct vb2_queue
*vq
, const struct v4l2_format
*fmt
,
245 unsigned int *nbuffers
, unsigned int *nplanes
,
246 unsigned int sizes
[], void *alloc_ctxs
[])
248 struct sh_vou_device
*vou_dev
= vb2_get_drv_priv(vq
);
249 struct v4l2_pix_format
*pix
= &vou_dev
->pix
;
250 int bytes_per_line
= vou_fmt
[vou_dev
->pix_idx
].bpp
* pix
->width
/ 8;
252 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s()\n", __func__
);
254 if (fmt
&& fmt
->fmt
.pix
.sizeimage
< pix
->height
* bytes_per_line
)
257 sizes
[0] = fmt
? fmt
->fmt
.pix
.sizeimage
: pix
->height
* bytes_per_line
;
258 alloc_ctxs
[0] = vou_dev
->alloc_ctx
;
262 static int sh_vou_buf_prepare(struct vb2_buffer
*vb
)
264 struct sh_vou_device
*vou_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
265 struct v4l2_pix_format
*pix
= &vou_dev
->pix
;
266 unsigned bytes_per_line
= vou_fmt
[vou_dev
->pix_idx
].bpp
* pix
->width
/ 8;
267 unsigned size
= pix
->height
* bytes_per_line
;
269 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s()\n", __func__
);
271 if (vb2_plane_size(vb
, 0) < size
) {
272 /* User buffer too small */
273 dev_warn(vou_dev
->v4l2_dev
.dev
, "buffer too small (%lu < %u)\n",
274 vb2_plane_size(vb
, 0), size
);
278 vb2_set_plane_payload(vb
, 0, size
);
282 /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
283 static void sh_vou_buf_queue(struct vb2_buffer
*vb
)
285 struct sh_vou_device
*vou_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
286 struct sh_vou_buffer
*shbuf
= to_sh_vou_buffer(vb
);
289 spin_lock_irqsave(&vou_dev
->lock
, flags
);
290 list_add_tail(&shbuf
->list
, &vou_dev
->buf_list
);
291 spin_unlock_irqrestore(&vou_dev
->lock
, flags
);
294 static int sh_vou_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
296 struct sh_vou_device
*vou_dev
= vb2_get_drv_priv(vq
);
297 struct sh_vou_buffer
*buf
, *node
;
300 vou_dev
->sequence
= 0;
301 ret
= v4l2_device_call_until_err(&vou_dev
->v4l2_dev
, 0,
303 if (ret
< 0 && ret
!= -ENOIOCTLCMD
) {
304 list_for_each_entry_safe(buf
, node
, &vou_dev
->buf_list
, list
) {
305 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_QUEUED
);
306 list_del(&buf
->list
);
308 vou_dev
->active
= NULL
;
312 buf
= list_entry(vou_dev
->buf_list
.next
, struct sh_vou_buffer
, list
);
314 vou_dev
->active
= buf
;
316 /* Start from side A: we use mirror addresses, so, set B */
317 sh_vou_reg_a_write(vou_dev
, VOURPR
, 1);
318 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s: first buffer status 0x%x\n",
319 __func__
, sh_vou_reg_a_read(vou_dev
, VOUSTR
));
320 sh_vou_schedule_next(vou_dev
, &buf
->vb
);
322 buf
= list_entry(buf
->list
.next
, struct sh_vou_buffer
, list
);
324 /* Second buffer - initialise register side B */
325 sh_vou_reg_a_write(vou_dev
, VOURPR
, 0);
326 sh_vou_schedule_next(vou_dev
, &buf
->vb
);
328 /* Register side switching with frame VSYNC */
329 sh_vou_reg_a_write(vou_dev
, VOURCR
, 5);
331 sh_vou_stream_config(vou_dev
);
332 /* Enable End-of-Frame (VSYNC) interrupts */
333 sh_vou_reg_a_write(vou_dev
, VOUIR
, 0x10004);
335 /* Two buffers on the queue - activate the hardware */
336 vou_dev
->status
= SH_VOU_RUNNING
;
337 sh_vou_reg_a_write(vou_dev
, VOUER
, 0x107);
341 static void sh_vou_stop_streaming(struct vb2_queue
*vq
)
343 struct sh_vou_device
*vou_dev
= vb2_get_drv_priv(vq
);
344 struct sh_vou_buffer
*buf
, *node
;
347 v4l2_device_call_until_err(&vou_dev
->v4l2_dev
, 0,
350 sh_vou_reg_a_set(vou_dev
, VOUER
, 0, 1);
351 /* ...but the current frame will complete */
352 sh_vou_reg_a_set(vou_dev
, VOUIR
, 0, 0x30000);
354 spin_lock_irqsave(&vou_dev
->lock
, flags
);
355 list_for_each_entry_safe(buf
, node
, &vou_dev
->buf_list
, list
) {
356 vb2_buffer_done(&buf
->vb
, VB2_BUF_STATE_ERROR
);
357 list_del(&buf
->list
);
359 vou_dev
->active
= NULL
;
360 spin_unlock_irqrestore(&vou_dev
->lock
, flags
);
363 static struct vb2_ops sh_vou_qops
= {
364 .queue_setup
= sh_vou_queue_setup
,
365 .buf_prepare
= sh_vou_buf_prepare
,
366 .buf_queue
= sh_vou_buf_queue
,
367 .start_streaming
= sh_vou_start_streaming
,
368 .stop_streaming
= sh_vou_stop_streaming
,
369 .wait_prepare
= vb2_ops_wait_prepare
,
370 .wait_finish
= vb2_ops_wait_finish
,
374 static int sh_vou_querycap(struct file
*file
, void *priv
,
375 struct v4l2_capability
*cap
)
377 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
379 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s()\n", __func__
);
381 strlcpy(cap
->card
, "SuperH VOU", sizeof(cap
->card
));
382 strlcpy(cap
->driver
, "sh-vou", sizeof(cap
->driver
));
383 strlcpy(cap
->bus_info
, "platform:sh-vou", sizeof(cap
->bus_info
));
384 cap
->device_caps
= V4L2_CAP_VIDEO_OUTPUT
| V4L2_CAP_READWRITE
|
386 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
390 /* Enumerate formats, that the device can accept from the user */
391 static int sh_vou_enum_fmt_vid_out(struct file
*file
, void *priv
,
392 struct v4l2_fmtdesc
*fmt
)
394 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
396 if (fmt
->index
>= ARRAY_SIZE(vou_fmt
))
399 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s()\n", __func__
);
401 fmt
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
402 strlcpy(fmt
->description
, vou_fmt
[fmt
->index
].desc
,
403 sizeof(fmt
->description
));
404 fmt
->pixelformat
= vou_fmt
[fmt
->index
].pfmt
;
409 static int sh_vou_g_fmt_vid_out(struct file
*file
, void *priv
,
410 struct v4l2_format
*fmt
)
412 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
414 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s()\n", __func__
);
416 fmt
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
417 fmt
->fmt
.pix
= vou_dev
->pix
;
422 static const unsigned char vou_scale_h_num
[] = {1, 9, 2, 9, 4};
423 static const unsigned char vou_scale_h_den
[] = {1, 8, 1, 4, 1};
424 static const unsigned char vou_scale_h_fld
[] = {0, 2, 1, 3};
425 static const unsigned char vou_scale_v_num
[] = {1, 2, 4};
426 static const unsigned char vou_scale_v_den
[] = {1, 1, 1};
427 static const unsigned char vou_scale_v_fld
[] = {0, 1};
429 static void sh_vou_configure_geometry(struct sh_vou_device
*vou_dev
,
430 int pix_idx
, int w_idx
, int h_idx
)
432 struct sh_vou_fmt
*fmt
= vou_fmt
+ pix_idx
;
433 unsigned int black_left
, black_top
, width_max
,
434 frame_in_height
, frame_out_height
, frame_out_top
;
435 struct v4l2_rect
*rect
= &vou_dev
->rect
;
436 struct v4l2_pix_format
*pix
= &vou_dev
->pix
;
437 u32 vouvcr
= 0, dsr_h
, dsr_v
;
439 if (vou_dev
->std
& V4L2_STD_525_60
) {
441 /* height_max = 262; */
444 /* height_max = 312; */
447 frame_in_height
= pix
->height
/ 2;
448 frame_out_height
= rect
->height
/ 2;
449 frame_out_top
= rect
->top
/ 2;
452 * Cropping scheme: max useful image is 720x480, and the total video
453 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
454 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
455 * of which the first 33 / 25 clocks HSYNC must be held active. This
456 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
457 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
458 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
459 * beyond DSR, specified on the left and top by the VPR register "black
460 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
461 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
462 * client requires, and that's exactly what leaves us 720 pixels for the
463 * image; we leave VPR[VVP] at default 20 for now, because the client
464 * doesn't seem to have any special requirements for it. Otherwise we
465 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
466 * the selected standard, and DPR and DSR are selected according to
467 * cropping. Q: how does the client detect the first valid line? Does
468 * HSYNC stay inactive during invalid (black) lines?
470 black_left
= width_max
- VOU_MAX_IMAGE_WIDTH
;
473 dsr_h
= rect
->width
+ rect
->left
;
474 dsr_v
= frame_out_height
+ frame_out_top
;
476 dev_dbg(vou_dev
->v4l2_dev
.dev
,
477 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
478 pix
->width
, frame_in_height
, black_left
, black_top
,
479 rect
->left
, frame_out_top
, dsr_h
, dsr_v
);
481 /* VOUISR height - half of a frame height in frame mode */
482 sh_vou_reg_ab_write(vou_dev
, VOUISR
, (pix
->width
<< 16) | frame_in_height
);
483 sh_vou_reg_ab_write(vou_dev
, VOUVPR
, (black_left
<< 16) | black_top
);
484 sh_vou_reg_ab_write(vou_dev
, VOUDPR
, (rect
->left
<< 16) | frame_out_top
);
485 sh_vou_reg_ab_write(vou_dev
, VOUDSR
, (dsr_h
<< 16) | dsr_v
);
488 * if necessary, we could set VOUHIR to
489 * max(black_left + dsr_h, width_max) here
493 vouvcr
|= (1 << 15) | (vou_scale_h_fld
[w_idx
- 1] << 4);
495 vouvcr
|= (1 << 14) | vou_scale_v_fld
[h_idx
- 1];
497 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s: scaling 0x%x\n", fmt
->desc
, vouvcr
);
499 /* To produce a colour bar for testing set bit 23 of VOUVCR */
500 sh_vou_reg_ab_write(vou_dev
, VOUVCR
, vouvcr
);
501 sh_vou_reg_ab_write(vou_dev
, VOUDFR
,
502 fmt
->pkf
| (fmt
->yf
<< 8) | (fmt
->rgb
<< 16));
505 struct sh_vou_geometry
{
506 struct v4l2_rect output
;
507 unsigned int in_width
;
508 unsigned int in_height
;
514 * Find input geometry, that we can use to produce output, closest to the
515 * requested rectangle, using VOU scaling
517 static void vou_adjust_input(struct sh_vou_geometry
*geo
, v4l2_std_id std
)
519 /* The compiler cannot know, that best and idx will indeed be set */
520 unsigned int best_err
= UINT_MAX
, best
= 0, img_height_max
;
523 if (std
& V4L2_STD_525_60
)
524 img_height_max
= 480;
526 img_height_max
= 576;
528 /* Image width must be a multiple of 4 */
529 v4l_bound_align_image(&geo
->in_width
,
530 VOU_MIN_IMAGE_WIDTH
, VOU_MAX_IMAGE_WIDTH
, 2,
532 VOU_MIN_IMAGE_HEIGHT
, img_height_max
, 1, 0);
534 /* Select scales to come as close as possible to the output image */
535 for (i
= ARRAY_SIZE(vou_scale_h_num
) - 1; i
>= 0; i
--) {
537 unsigned int found
= geo
->output
.width
* vou_scale_h_den
[i
] /
540 if (found
> VOU_MAX_IMAGE_WIDTH
)
541 /* scales increase */
544 err
= abs(found
- geo
->in_width
);
545 if (err
< best_err
) {
554 geo
->in_width
= best
;
555 geo
->scale_idx_h
= idx
;
559 /* This loop can be replaced with one division */
560 for (i
= ARRAY_SIZE(vou_scale_v_num
) - 1; i
>= 0; i
--) {
562 unsigned int found
= geo
->output
.height
* vou_scale_v_den
[i
] /
565 if (found
> img_height_max
)
566 /* scales increase */
569 err
= abs(found
- geo
->in_height
);
570 if (err
< best_err
) {
579 geo
->in_height
= best
;
580 geo
->scale_idx_v
= idx
;
584 * Find output geometry, that we can produce, using VOU scaling, closest to
585 * the requested rectangle
587 static void vou_adjust_output(struct sh_vou_geometry
*geo
, v4l2_std_id std
)
589 unsigned int best_err
= UINT_MAX
, best
= geo
->in_width
,
590 width_max
, height_max
, img_height_max
;
591 int i
, idx_h
= 0, idx_v
= 0;
593 if (std
& V4L2_STD_525_60
) {
595 height_max
= 262 * 2;
596 img_height_max
= 480;
599 height_max
= 312 * 2;
600 img_height_max
= 576;
603 /* Select scales to come as close as possible to the output image */
604 for (i
= 0; i
< ARRAY_SIZE(vou_scale_h_num
); i
++) {
606 unsigned int found
= geo
->in_width
* vou_scale_h_num
[i
] /
609 if (found
> VOU_MAX_IMAGE_WIDTH
)
610 /* scales increase */
613 err
= abs(found
- geo
->output
.width
);
614 if (err
< best_err
) {
623 geo
->output
.width
= best
;
624 geo
->scale_idx_h
= idx_h
;
625 if (geo
->output
.left
+ best
> width_max
)
626 geo
->output
.left
= width_max
- best
;
628 pr_debug("%s(): W %u * %u/%u = %u\n", __func__
, geo
->in_width
,
629 vou_scale_h_num
[idx_h
], vou_scale_h_den
[idx_h
], best
);
633 /* This loop can be replaced with one division */
634 for (i
= 0; i
< ARRAY_SIZE(vou_scale_v_num
); i
++) {
636 unsigned int found
= geo
->in_height
* vou_scale_v_num
[i
] /
639 if (found
> img_height_max
)
640 /* scales increase */
643 err
= abs(found
- geo
->output
.height
);
644 if (err
< best_err
) {
653 geo
->output
.height
= best
;
654 geo
->scale_idx_v
= idx_v
;
655 if (geo
->output
.top
+ best
> height_max
)
656 geo
->output
.top
= height_max
- best
;
658 pr_debug("%s(): H %u * %u/%u = %u\n", __func__
, geo
->in_height
,
659 vou_scale_v_num
[idx_v
], vou_scale_v_den
[idx_v
], best
);
662 static int sh_vou_try_fmt_vid_out(struct file
*file
, void *priv
,
663 struct v4l2_format
*fmt
)
665 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
666 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
667 unsigned int img_height_max
;
670 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s()\n", __func__
);
672 pix
->field
= V4L2_FIELD_INTERLACED
;
673 pix
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
674 pix
->ycbcr_enc
= pix
->quantization
= 0;
676 for (pix_idx
= 0; pix_idx
< ARRAY_SIZE(vou_fmt
); pix_idx
++)
677 if (vou_fmt
[pix_idx
].pfmt
== pix
->pixelformat
)
680 if (pix_idx
== ARRAY_SIZE(vou_fmt
))
683 if (vou_dev
->std
& V4L2_STD_525_60
)
684 img_height_max
= 480;
686 img_height_max
= 576;
688 v4l_bound_align_image(&pix
->width
,
689 VOU_MIN_IMAGE_WIDTH
, VOU_MAX_IMAGE_WIDTH
, 2,
691 VOU_MIN_IMAGE_HEIGHT
, img_height_max
, 1, 0);
692 pix
->bytesperline
= pix
->width
* vou_fmt
[pix_idx
].bpl
;
693 pix
->sizeimage
= pix
->height
* ((pix
->width
* vou_fmt
[pix_idx
].bpp
) >> 3);
698 static int sh_vou_set_fmt_vid_out(struct sh_vou_device
*vou_dev
,
699 struct v4l2_pix_format
*pix
)
701 unsigned int img_height_max
;
702 struct sh_vou_geometry geo
;
703 struct v4l2_subdev_format format
= {
704 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
705 /* Revisit: is this the correct code? */
706 .format
.code
= MEDIA_BUS_FMT_YUYV8_2X8
,
707 .format
.field
= V4L2_FIELD_INTERLACED
,
708 .format
.colorspace
= V4L2_COLORSPACE_SMPTE170M
,
710 struct v4l2_mbus_framefmt
*mbfmt
= &format
.format
;
714 if (vb2_is_busy(&vou_dev
->queue
))
717 for (pix_idx
= 0; pix_idx
< ARRAY_SIZE(vou_fmt
); pix_idx
++)
718 if (vou_fmt
[pix_idx
].pfmt
== pix
->pixelformat
)
721 geo
.in_width
= pix
->width
;
722 geo
.in_height
= pix
->height
;
723 geo
.output
= vou_dev
->rect
;
725 vou_adjust_output(&geo
, vou_dev
->std
);
727 mbfmt
->width
= geo
.output
.width
;
728 mbfmt
->height
= geo
.output
.height
;
729 ret
= v4l2_device_call_until_err(&vou_dev
->v4l2_dev
, 0, pad
,
730 set_fmt
, NULL
, &format
);
731 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
735 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s(): %ux%u -> %ux%u\n", __func__
,
736 geo
.output
.width
, geo
.output
.height
, mbfmt
->width
, mbfmt
->height
);
738 if (vou_dev
->std
& V4L2_STD_525_60
)
739 img_height_max
= 480;
741 img_height_max
= 576;
744 if ((unsigned)mbfmt
->width
> VOU_MAX_IMAGE_WIDTH
||
745 (unsigned)mbfmt
->height
> img_height_max
||
746 mbfmt
->code
!= MEDIA_BUS_FMT_YUYV8_2X8
)
749 if (mbfmt
->width
!= geo
.output
.width
||
750 mbfmt
->height
!= geo
.output
.height
) {
751 geo
.output
.width
= mbfmt
->width
;
752 geo
.output
.height
= mbfmt
->height
;
754 vou_adjust_input(&geo
, vou_dev
->std
);
757 /* We tried to preserve output rectangle, but it could have changed */
758 vou_dev
->rect
= geo
.output
;
759 pix
->width
= geo
.in_width
;
760 pix
->height
= geo
.in_height
;
762 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s(): %ux%u\n", __func__
,
763 pix
->width
, pix
->height
);
765 vou_dev
->pix_idx
= pix_idx
;
769 sh_vou_configure_geometry(vou_dev
, pix_idx
,
770 geo
.scale_idx_h
, geo
.scale_idx_v
);
775 static int sh_vou_s_fmt_vid_out(struct file
*file
, void *priv
,
776 struct v4l2_format
*fmt
)
778 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
779 int ret
= sh_vou_try_fmt_vid_out(file
, priv
, fmt
);
783 return sh_vou_set_fmt_vid_out(vou_dev
, &fmt
->fmt
.pix
);
786 static int sh_vou_enum_output(struct file
*file
, void *fh
,
787 struct v4l2_output
*a
)
789 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
793 strlcpy(a
->name
, "Video Out", sizeof(a
->name
));
794 a
->type
= V4L2_OUTPUT_TYPE_ANALOG
;
795 a
->std
= vou_dev
->vdev
.tvnorms
;
799 int sh_vou_g_output(struct file
*file
, void *fh
, unsigned int *i
)
805 int sh_vou_s_output(struct file
*file
, void *fh
, unsigned int i
)
807 return i
? -EINVAL
: 0;
810 static u32
sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt
)
814 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
816 case SH_VOU_BUS_8BIT
:
818 case SH_VOU_BUS_16BIT
:
820 case SH_VOU_BUS_BT656
:
825 static int sh_vou_s_std(struct file
*file
, void *priv
, v4l2_std_id std_id
)
827 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
830 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s(): 0x%llx\n", __func__
, std_id
);
832 if (std_id
== vou_dev
->std
)
835 if (vb2_is_busy(&vou_dev
->queue
))
838 ret
= v4l2_device_call_until_err(&vou_dev
->v4l2_dev
, 0, video
,
839 s_std_output
, std_id
);
840 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
841 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
844 vou_dev
->rect
.top
= vou_dev
->rect
.left
= 0;
845 vou_dev
->rect
.width
= VOU_MAX_IMAGE_WIDTH
;
846 if (std_id
& V4L2_STD_525_60
) {
847 sh_vou_reg_ab_set(vou_dev
, VOUCR
,
848 sh_vou_ntsc_mode(vou_dev
->pdata
->bus_fmt
) << 29, 7 << 29);
849 vou_dev
->rect
.height
= 480;
851 sh_vou_reg_ab_set(vou_dev
, VOUCR
, 5 << 29, 7 << 29);
852 vou_dev
->rect
.height
= 576;
855 vou_dev
->pix
.width
= vou_dev
->rect
.width
;
856 vou_dev
->pix
.height
= vou_dev
->rect
.height
;
857 vou_dev
->pix
.bytesperline
=
858 vou_dev
->pix
.width
* vou_fmt
[vou_dev
->pix_idx
].bpl
;
859 vou_dev
->pix
.sizeimage
= vou_dev
->pix
.height
*
860 ((vou_dev
->pix
.width
* vou_fmt
[vou_dev
->pix_idx
].bpp
) >> 3);
861 vou_dev
->std
= std_id
;
862 sh_vou_set_fmt_vid_out(vou_dev
, &vou_dev
->pix
);
867 static int sh_vou_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
869 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
871 dev_dbg(vou_dev
->v4l2_dev
.dev
, "%s()\n", __func__
);
878 static int sh_vou_log_status(struct file
*file
, void *priv
)
880 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
882 pr_info("VOUER: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUER
));
883 pr_info("VOUCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUCR
));
884 pr_info("VOUSTR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUSTR
));
885 pr_info("VOUVCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUVCR
));
886 pr_info("VOUISR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUISR
));
887 pr_info("VOUBCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUBCR
));
888 pr_info("VOUDPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUDPR
));
889 pr_info("VOUDSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUDSR
));
890 pr_info("VOUVPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUVPR
));
891 pr_info("VOUIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUIR
));
892 pr_info("VOUSRR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUSRR
));
893 pr_info("VOUMSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUMSR
));
894 pr_info("VOUHIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUHIR
));
895 pr_info("VOUDFR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUDFR
));
896 pr_info("VOUAD1R: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUAD1R
));
897 pr_info("VOUAD2R: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUAD2R
));
898 pr_info("VOUAIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUAIR
));
899 pr_info("VOUSWR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOUSWR
));
900 pr_info("VOURCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOURCR
));
901 pr_info("VOURPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev
, VOURPR
));
905 static int sh_vou_g_selection(struct file
*file
, void *fh
,
906 struct v4l2_selection
*sel
)
908 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
910 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
)
912 switch (sel
->target
) {
913 case V4L2_SEL_TGT_COMPOSE
:
914 sel
->r
= vou_dev
->rect
;
916 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
917 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
920 sel
->r
.width
= VOU_MAX_IMAGE_WIDTH
;
921 if (vou_dev
->std
& V4L2_STD_525_60
)
932 /* Assume a dull encoder, do all the work ourselves. */
933 static int sh_vou_s_selection(struct file
*file
, void *fh
,
934 struct v4l2_selection
*sel
)
936 struct v4l2_rect
*rect
= &sel
->r
;
937 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
938 struct v4l2_crop sd_crop
= {.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
};
939 struct v4l2_pix_format
*pix
= &vou_dev
->pix
;
940 struct sh_vou_geometry geo
;
941 struct v4l2_subdev_format format
= {
942 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
943 /* Revisit: is this the correct code? */
944 .format
.code
= MEDIA_BUS_FMT_YUYV8_2X8
,
945 .format
.field
= V4L2_FIELD_INTERLACED
,
946 .format
.colorspace
= V4L2_COLORSPACE_SMPTE170M
,
948 unsigned int img_height_max
;
951 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_OUTPUT
||
952 sel
->target
!= V4L2_SEL_TGT_COMPOSE
)
955 if (vb2_is_busy(&vou_dev
->queue
))
958 if (vou_dev
->std
& V4L2_STD_525_60
)
959 img_height_max
= 480;
961 img_height_max
= 576;
963 v4l_bound_align_image(&rect
->width
,
964 VOU_MIN_IMAGE_WIDTH
, VOU_MAX_IMAGE_WIDTH
, 1,
966 VOU_MIN_IMAGE_HEIGHT
, img_height_max
, 1, 0);
968 if (rect
->width
+ rect
->left
> VOU_MAX_IMAGE_WIDTH
)
969 rect
->left
= VOU_MAX_IMAGE_WIDTH
- rect
->width
;
971 if (rect
->height
+ rect
->top
> img_height_max
)
972 rect
->top
= img_height_max
- rect
->height
;
975 geo
.in_width
= pix
->width
;
976 geo
.in_height
= pix
->height
;
978 /* Configure the encoder one-to-one, position at 0, ignore errors */
979 sd_crop
.c
.width
= geo
.output
.width
;
980 sd_crop
.c
.height
= geo
.output
.height
;
982 * We first issue a S_CROP, so that the subsequent S_FMT delivers the
983 * final encoder configuration.
985 v4l2_device_call_until_err(&vou_dev
->v4l2_dev
, 0, video
,
987 format
.format
.width
= geo
.output
.width
;
988 format
.format
.height
= geo
.output
.height
;
989 ret
= v4l2_device_call_until_err(&vou_dev
->v4l2_dev
, 0, pad
,
990 set_fmt
, NULL
, &format
);
991 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
996 if ((unsigned)format
.format
.width
> VOU_MAX_IMAGE_WIDTH
||
997 (unsigned)format
.format
.height
> img_height_max
||
998 format
.format
.code
!= MEDIA_BUS_FMT_YUYV8_2X8
)
1001 geo
.output
.width
= format
.format
.width
;
1002 geo
.output
.height
= format
.format
.height
;
1005 * No down-scaling. According to the API, current call has precedence:
1006 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1008 vou_adjust_input(&geo
, vou_dev
->std
);
1010 /* We tried to preserve output rectangle, but it could have changed */
1011 vou_dev
->rect
= geo
.output
;
1012 pix
->width
= geo
.in_width
;
1013 pix
->height
= geo
.in_height
;
1015 sh_vou_configure_geometry(vou_dev
, vou_dev
->pix_idx
,
1016 geo
.scale_idx_h
, geo
.scale_idx_v
);
1021 static irqreturn_t
sh_vou_isr(int irq
, void *dev_id
)
1023 struct sh_vou_device
*vou_dev
= dev_id
;
1024 static unsigned long j
;
1025 struct sh_vou_buffer
*vb
;
1027 u32 irq_status
= sh_vou_reg_a_read(vou_dev
, VOUIR
), masked
;
1028 u32 vou_status
= sh_vou_reg_a_read(vou_dev
, VOUSTR
);
1030 if (!(irq_status
& 0x300)) {
1031 if (printk_timed_ratelimit(&j
, 500))
1032 dev_warn(vou_dev
->v4l2_dev
.dev
, "IRQ status 0x%x!\n",
1037 spin_lock(&vou_dev
->lock
);
1038 if (!vou_dev
->active
|| list_empty(&vou_dev
->buf_list
)) {
1039 if (printk_timed_ratelimit(&j
, 500))
1040 dev_warn(vou_dev
->v4l2_dev
.dev
,
1041 "IRQ without active buffer: %x!\n", irq_status
);
1042 /* Just ack: buf_release will disable further interrupts */
1043 sh_vou_reg_a_set(vou_dev
, VOUIR
, 0, 0x300);
1044 spin_unlock(&vou_dev
->lock
);
1048 masked
= ~(0x300 & irq_status
) & irq_status
& 0x30304;
1049 dev_dbg(vou_dev
->v4l2_dev
.dev
,
1050 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1051 irq_status
, masked
, vou_status
, cnt
);
1054 /* side = vou_status & 0x10000; */
1056 /* Clear only set interrupts */
1057 sh_vou_reg_a_write(vou_dev
, VOUIR
, masked
);
1059 vb
= vou_dev
->active
;
1060 if (list_is_singular(&vb
->list
)) {
1061 /* Keep cycling while no next buffer is available */
1062 sh_vou_schedule_next(vou_dev
, &vb
->vb
);
1063 spin_unlock(&vou_dev
->lock
);
1067 list_del(&vb
->list
);
1069 v4l2_get_timestamp(&vb
->vb
.v4l2_buf
.timestamp
);
1070 vb
->vb
.v4l2_buf
.sequence
= vou_dev
->sequence
++;
1071 vb
->vb
.v4l2_buf
.field
= V4L2_FIELD_INTERLACED
;
1072 vb2_buffer_done(&vb
->vb
, VB2_BUF_STATE_DONE
);
1074 vou_dev
->active
= list_entry(vou_dev
->buf_list
.next
,
1075 struct sh_vou_buffer
, list
);
1077 if (list_is_singular(&vou_dev
->buf_list
)) {
1078 /* Keep cycling while no next buffer is available */
1079 sh_vou_schedule_next(vou_dev
, &vou_dev
->active
->vb
);
1081 struct sh_vou_buffer
*new = list_entry(vou_dev
->active
->list
.next
,
1082 struct sh_vou_buffer
, list
);
1083 sh_vou_schedule_next(vou_dev
, &new->vb
);
1086 spin_unlock(&vou_dev
->lock
);
1091 static int sh_vou_hw_init(struct sh_vou_device
*vou_dev
)
1093 struct sh_vou_pdata
*pdata
= vou_dev
->pdata
;
1094 u32 voucr
= sh_vou_ntsc_mode(pdata
->bus_fmt
) << 29;
1097 /* Disable all IRQs */
1098 sh_vou_reg_a_write(vou_dev
, VOUIR
, 0);
1100 /* Reset VOU interfaces - registers unaffected */
1101 sh_vou_reg_a_write(vou_dev
, VOUSRR
, 0x101);
1102 while (--i
&& (sh_vou_reg_a_read(vou_dev
, VOUSRR
) & 0x101))
1108 dev_dbg(vou_dev
->v4l2_dev
.dev
, "Reset took %dus\n", 100 - i
);
1110 if (pdata
->flags
& SH_VOU_PCLK_FALLING
)
1112 if (pdata
->flags
& SH_VOU_HSYNC_LOW
)
1114 if (pdata
->flags
& SH_VOU_VSYNC_LOW
)
1116 sh_vou_reg_ab_set(vou_dev
, VOUCR
, voucr
, 0xfc000000);
1118 /* Manual register side switching at first */
1119 sh_vou_reg_a_write(vou_dev
, VOURCR
, 4);
1120 /* Default - fixed HSYNC length, can be made configurable is required */
1121 sh_vou_reg_ab_write(vou_dev
, VOUMSR
, 0x800000);
1123 sh_vou_set_fmt_vid_out(vou_dev
, &vou_dev
->pix
);
1128 /* File operations */
1129 static int sh_vou_open(struct file
*file
)
1131 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
1134 if (mutex_lock_interruptible(&vou_dev
->fop_lock
))
1135 return -ERESTARTSYS
;
1137 err
= v4l2_fh_open(file
);
1140 if (v4l2_fh_is_singular_file(file
) &&
1141 vou_dev
->status
== SH_VOU_INITIALISING
) {
1143 pm_runtime_get_sync(vou_dev
->v4l2_dev
.dev
);
1144 err
= sh_vou_hw_init(vou_dev
);
1146 pm_runtime_put(vou_dev
->v4l2_dev
.dev
);
1147 v4l2_fh_release(file
);
1149 vou_dev
->status
= SH_VOU_IDLE
;
1153 mutex_unlock(&vou_dev
->fop_lock
);
1157 static int sh_vou_release(struct file
*file
)
1159 struct sh_vou_device
*vou_dev
= video_drvdata(file
);
1162 mutex_lock(&vou_dev
->fop_lock
);
1163 is_last
= v4l2_fh_is_singular_file(file
);
1164 _vb2_fop_release(file
, NULL
);
1167 vou_dev
->status
= SH_VOU_INITIALISING
;
1168 sh_vou_reg_a_set(vou_dev
, VOUER
, 0, 0x101);
1169 pm_runtime_put(vou_dev
->v4l2_dev
.dev
);
1171 mutex_unlock(&vou_dev
->fop_lock
);
1175 /* sh_vou display ioctl operations */
1176 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops
= {
1177 .vidioc_querycap
= sh_vou_querycap
,
1178 .vidioc_enum_fmt_vid_out
= sh_vou_enum_fmt_vid_out
,
1179 .vidioc_g_fmt_vid_out
= sh_vou_g_fmt_vid_out
,
1180 .vidioc_s_fmt_vid_out
= sh_vou_s_fmt_vid_out
,
1181 .vidioc_try_fmt_vid_out
= sh_vou_try_fmt_vid_out
,
1182 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1183 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1184 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1185 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1186 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1187 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1188 .vidioc_streamon
= vb2_ioctl_streamon
,
1189 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1190 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1191 .vidioc_g_output
= sh_vou_g_output
,
1192 .vidioc_s_output
= sh_vou_s_output
,
1193 .vidioc_enum_output
= sh_vou_enum_output
,
1194 .vidioc_s_std
= sh_vou_s_std
,
1195 .vidioc_g_std
= sh_vou_g_std
,
1196 .vidioc_g_selection
= sh_vou_g_selection
,
1197 .vidioc_s_selection
= sh_vou_s_selection
,
1198 .vidioc_log_status
= sh_vou_log_status
,
1201 static const struct v4l2_file_operations sh_vou_fops
= {
1202 .owner
= THIS_MODULE
,
1203 .open
= sh_vou_open
,
1204 .release
= sh_vou_release
,
1205 .unlocked_ioctl
= video_ioctl2
,
1206 .mmap
= vb2_fop_mmap
,
1207 .poll
= vb2_fop_poll
,
1208 .write
= vb2_fop_write
,
1211 static const struct video_device sh_vou_video_template
= {
1213 .fops
= &sh_vou_fops
,
1214 .ioctl_ops
= &sh_vou_ioctl_ops
,
1215 .tvnorms
= V4L2_STD_525_60
, /* PAL only supported in 8-bit non-bt656 mode */
1216 .vfl_dir
= VFL_DIR_TX
,
1219 static int sh_vou_probe(struct platform_device
*pdev
)
1221 struct sh_vou_pdata
*vou_pdata
= pdev
->dev
.platform_data
;
1222 struct v4l2_rect
*rect
;
1223 struct v4l2_pix_format
*pix
;
1224 struct i2c_adapter
*i2c_adap
;
1225 struct video_device
*vdev
;
1226 struct sh_vou_device
*vou_dev
;
1227 struct resource
*reg_res
;
1228 struct v4l2_subdev
*subdev
;
1229 struct vb2_queue
*q
;
1232 reg_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1233 irq
= platform_get_irq(pdev
, 0);
1235 if (!vou_pdata
|| !reg_res
|| irq
<= 0) {
1236 dev_err(&pdev
->dev
, "Insufficient VOU platform information.\n");
1240 vou_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*vou_dev
), GFP_KERNEL
);
1244 INIT_LIST_HEAD(&vou_dev
->buf_list
);
1245 spin_lock_init(&vou_dev
->lock
);
1246 mutex_init(&vou_dev
->fop_lock
);
1247 vou_dev
->pdata
= vou_pdata
;
1248 vou_dev
->status
= SH_VOU_INITIALISING
;
1249 vou_dev
->pix_idx
= 1;
1251 rect
= &vou_dev
->rect
;
1252 pix
= &vou_dev
->pix
;
1254 /* Fill in defaults */
1255 vou_dev
->std
= V4L2_STD_NTSC_M
;
1258 rect
->width
= VOU_MAX_IMAGE_WIDTH
;
1260 pix
->width
= VOU_MAX_IMAGE_WIDTH
;
1262 pix
->pixelformat
= V4L2_PIX_FMT_NV16
;
1263 pix
->field
= V4L2_FIELD_INTERLACED
;
1264 pix
->bytesperline
= VOU_MAX_IMAGE_WIDTH
;
1265 pix
->sizeimage
= VOU_MAX_IMAGE_WIDTH
* 2 * 480;
1266 pix
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1268 vou_dev
->base
= devm_ioremap_resource(&pdev
->dev
, reg_res
);
1269 if (IS_ERR(vou_dev
->base
))
1270 return PTR_ERR(vou_dev
->base
);
1272 ret
= devm_request_irq(&pdev
->dev
, irq
, sh_vou_isr
, 0, "vou", vou_dev
);
1276 ret
= v4l2_device_register(&pdev
->dev
, &vou_dev
->v4l2_dev
);
1278 dev_err(&pdev
->dev
, "Error registering v4l2 device\n");
1282 vdev
= &vou_dev
->vdev
;
1283 *vdev
= sh_vou_video_template
;
1284 if (vou_pdata
->bus_fmt
== SH_VOU_BUS_8BIT
)
1285 vdev
->tvnorms
|= V4L2_STD_PAL
;
1286 vdev
->v4l2_dev
= &vou_dev
->v4l2_dev
;
1287 vdev
->release
= video_device_release_empty
;
1288 vdev
->lock
= &vou_dev
->fop_lock
;
1290 video_set_drvdata(vdev
, vou_dev
);
1292 /* Initialize the vb2 queue */
1293 q
= &vou_dev
->queue
;
1294 q
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT
;
1295 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
| VB2_WRITE
;
1296 q
->drv_priv
= vou_dev
;
1297 q
->buf_struct_size
= sizeof(struct sh_vou_buffer
);
1298 q
->ops
= &sh_vou_qops
;
1299 q
->mem_ops
= &vb2_dma_contig_memops
;
1300 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1301 q
->min_buffers_needed
= 2;
1302 q
->lock
= &vou_dev
->fop_lock
;
1303 ret
= vb2_queue_init(q
);
1307 vou_dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
1308 if (IS_ERR(vou_dev
->alloc_ctx
)) {
1309 dev_err(&pdev
->dev
, "Can't allocate buffer context");
1310 ret
= PTR_ERR(vou_dev
->alloc_ctx
);
1314 INIT_LIST_HEAD(&vou_dev
->buf_list
);
1316 pm_runtime_enable(&pdev
->dev
);
1317 pm_runtime_resume(&pdev
->dev
);
1319 i2c_adap
= i2c_get_adapter(vou_pdata
->i2c_adap
);
1325 ret
= sh_vou_hw_init(vou_dev
);
1329 subdev
= v4l2_i2c_new_subdev_board(&vou_dev
->v4l2_dev
, i2c_adap
,
1330 vou_pdata
->board_info
, NULL
);
1336 ret
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
1345 i2c_put_adapter(i2c_adap
);
1347 vb2_dma_contig_cleanup_ctx(vou_dev
->alloc_ctx
);
1349 pm_runtime_disable(&pdev
->dev
);
1350 v4l2_device_unregister(&vou_dev
->v4l2_dev
);
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 vb2_dma_contig_cleanup_ctx(vou_dev
->alloc_ctx
);
1367 v4l2_device_unregister(&vou_dev
->v4l2_dev
);
1371 static struct platform_driver __refdata sh_vou
= {
1372 .remove
= sh_vou_remove
,
1378 module_platform_driver_probe(sh_vou
, sh_vou_probe
);
1380 MODULE_DESCRIPTION("SuperH VOU driver");
1381 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1382 MODULE_LICENSE("GPL v2");
1383 MODULE_VERSION("0.1.0");
1384 MODULE_ALIAS("platform:sh-vou");