[media] media/platform: convert drivers to use the new vb2_queue dev field
[deliverable/linux.git] / drivers / media / platform / sh_vou.c
CommitLineData
a81fb9b2
GL
1/*
2 * SuperH Video Output Unit (VOU) driver
3 *
4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 *
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 */
10
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>
3b23db39 21#include <linux/slab.h>
a81fb9b2 22#include <linux/videodev2.h>
7a707b89 23#include <linux/module.h>
a81fb9b2 24
d647f0b7 25#include <media/drv-intf/sh_vou.h>
a81fb9b2
GL
26#include <media/v4l2-common.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-ioctl.h>
29#include <media/v4l2-mediabus.h>
2d700715 30#include <media/videobuf2-v4l2.h>
57af3ad5 31#include <media/videobuf2-dma-contig.h>
a81fb9b2
GL
32
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
54
55enum sh_vou_status {
56 SH_VOU_IDLE,
57 SH_VOU_INITIALISING,
58 SH_VOU_RUNNING,
59};
60
57af3ad5 61#define VOU_MIN_IMAGE_WIDTH 16
a81fb9b2 62#define VOU_MAX_IMAGE_WIDTH 720
57af3ad5
HV
63#define VOU_MIN_IMAGE_HEIGHT 16
64
65struct sh_vou_buffer {
2d700715 66 struct vb2_v4l2_buffer vb;
57af3ad5
HV
67 struct list_head list;
68};
69
2d700715
JS
70static inline struct
71sh_vou_buffer *to_sh_vou_buffer(struct vb2_v4l2_buffer *vb2)
57af3ad5
HV
72{
73 return container_of(vb2, struct sh_vou_buffer, vb);
74}
a81fb9b2
GL
75
76struct sh_vou_device {
77 struct v4l2_device v4l2_dev;
d079f99f 78 struct video_device vdev;
a81fb9b2
GL
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;
57af3ad5 85 struct list_head buf_list;
a81fb9b2
GL
86 v4l2_std_id std;
87 int pix_idx;
57af3ad5
HV
88 struct vb2_queue queue;
89 struct vb2_alloc_ctx *alloc_ctx;
90 struct sh_vou_buffer *active;
a81fb9b2 91 enum sh_vou_status status;
57af3ad5 92 unsigned sequence;
69756693 93 struct mutex fop_lock;
a81fb9b2
GL
94};
95
a81fb9b2
GL
96/* Register access routines for sides A, B and mirror addresses */
97static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
98 u32 value)
99{
100 __raw_writel(value, vou_dev->base + reg);
101}
102
103static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
104 u32 value)
105{
106 __raw_writel(value, vou_dev->base + reg);
107 __raw_writel(value, vou_dev->base + reg + 0x1000);
108}
109
110static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
111 u32 value)
112{
113 __raw_writel(value, vou_dev->base + reg + 0x2000);
114}
115
116static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
117{
118 return __raw_readl(vou_dev->base + reg);
119}
120
121static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
122 u32 value, u32 mask)
123{
124 u32 old = __raw_readl(vou_dev->base + reg);
125
126 value = (value & mask) | (old & ~mask);
127 __raw_writel(value, vou_dev->base + reg);
128}
129
130static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
131 u32 value, u32 mask)
132{
133 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
134}
135
136static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
137 u32 value, u32 mask)
138{
139 sh_vou_reg_a_set(vou_dev, reg, value, mask);
140 sh_vou_reg_b_set(vou_dev, reg, value, mask);
141}
142
143struct sh_vou_fmt {
144 u32 pfmt;
145 char *desc;
146 unsigned char bpp;
5c3edcb2 147 unsigned char bpl;
a81fb9b2
GL
148 unsigned char rgb;
149 unsigned char yf;
150 unsigned char pkf;
151};
152
153/* Further pixel formats can be added */
154static struct sh_vou_fmt vou_fmt[] = {
155 {
156 .pfmt = V4L2_PIX_FMT_NV12,
157 .bpp = 12,
5c3edcb2 158 .bpl = 1,
a81fb9b2
GL
159 .desc = "YVU420 planar",
160 .yf = 0,
161 .rgb = 0,
162 },
163 {
164 .pfmt = V4L2_PIX_FMT_NV16,
165 .bpp = 16,
5c3edcb2 166 .bpl = 1,
a81fb9b2
GL
167 .desc = "YVYU planar",
168 .yf = 1,
169 .rgb = 0,
170 },
171 {
172 .pfmt = V4L2_PIX_FMT_RGB24,
173 .bpp = 24,
5c3edcb2 174 .bpl = 3,
a81fb9b2
GL
175 .desc = "RGB24",
176 .pkf = 2,
177 .rgb = 1,
178 },
179 {
180 .pfmt = V4L2_PIX_FMT_RGB565,
181 .bpp = 16,
5c3edcb2 182 .bpl = 2,
a81fb9b2
GL
183 .desc = "RGB565",
184 .pkf = 3,
185 .rgb = 1,
186 },
187 {
188 .pfmt = V4L2_PIX_FMT_RGB565X,
189 .bpp = 16,
5c3edcb2 190 .bpl = 2,
a81fb9b2
GL
191 .desc = "RGB565 byteswapped",
192 .pkf = 3,
193 .rgb = 1,
194 },
195};
196
197static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
2d700715 198 struct vb2_v4l2_buffer *vbuf)
a81fb9b2
GL
199{
200 dma_addr_t addr1, addr2;
201
2d700715 202 addr1 = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
a81fb9b2
GL
203 switch (vou_dev->pix.pixelformat) {
204 case V4L2_PIX_FMT_NV12:
205 case V4L2_PIX_FMT_NV16:
206 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
207 break;
208 default:
209 addr2 = 0;
210 }
211
212 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
213 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
214}
215
57af3ad5 216static void sh_vou_stream_config(struct sh_vou_device *vou_dev)
a81fb9b2
GL
217{
218 unsigned int row_coeff;
219#ifdef __LITTLE_ENDIAN
220 u32 dataswap = 7;
221#else
222 u32 dataswap = 0;
223#endif
224
225 switch (vou_dev->pix.pixelformat) {
bc1ebd70 226 default:
a81fb9b2
GL
227 case V4L2_PIX_FMT_NV12:
228 case V4L2_PIX_FMT_NV16:
229 row_coeff = 1;
230 break;
231 case V4L2_PIX_FMT_RGB565:
232 dataswap ^= 1;
233 case V4L2_PIX_FMT_RGB565X:
234 row_coeff = 2;
235 break;
236 case V4L2_PIX_FMT_RGB24:
237 row_coeff = 3;
238 break;
239 }
240
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);
a81fb9b2
GL
243}
244
69756693 245/* Locking: caller holds fop_lock mutex */
df9ecb0c 246static int sh_vou_queue_setup(struct vb2_queue *vq,
57af3ad5
HV
247 unsigned int *nbuffers, unsigned int *nplanes,
248 unsigned int sizes[], void *alloc_ctxs[])
a81fb9b2 249{
57af3ad5
HV
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;
a81fb9b2 253
57af3ad5 254 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2 255
57af3ad5 256 alloc_ctxs[0] = vou_dev->alloc_ctx;
df9ecb0c
HV
257 if (*nplanes)
258 return sizes[0] < pix->height * bytes_per_line ? -EINVAL : 0;
259 *nplanes = 1;
260 sizes[0] = pix->height * bytes_per_line;
a81fb9b2
GL
261 return 0;
262}
263
57af3ad5 264static int sh_vou_buf_prepare(struct vb2_buffer *vb)
a81fb9b2 265{
57af3ad5 266 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue);
a81fb9b2 267 struct v4l2_pix_format *pix = &vou_dev->pix;
57af3ad5
HV
268 unsigned bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
269 unsigned size = pix->height * bytes_per_line;
a81fb9b2 270
d899eddd 271 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2 272
57af3ad5 273 if (vb2_plane_size(vb, 0) < size) {
a81fb9b2 274 /* User buffer too small */
57af3ad5
HV
275 dev_warn(vou_dev->v4l2_dev.dev, "buffer too small (%lu < %u)\n",
276 vb2_plane_size(vb, 0), size);
a81fb9b2
GL
277 return -EINVAL;
278 }
279
57af3ad5 280 vb2_set_plane_payload(vb, 0, size);
a81fb9b2
GL
281 return 0;
282}
283
69756693 284/* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
57af3ad5 285static void sh_vou_buf_queue(struct vb2_buffer *vb)
a81fb9b2 286{
2d700715 287 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
57af3ad5 288 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue);
2d700715 289 struct sh_vou_buffer *shbuf = to_sh_vou_buffer(vbuf);
57af3ad5 290 unsigned long flags;
a81fb9b2 291
57af3ad5
HV
292 spin_lock_irqsave(&vou_dev->lock, flags);
293 list_add_tail(&shbuf->list, &vou_dev->buf_list);
294 spin_unlock_irqrestore(&vou_dev->lock, flags);
295}
a81fb9b2 296
57af3ad5
HV
297static int sh_vou_start_streaming(struct vb2_queue *vq, unsigned int count)
298{
299 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
300 struct sh_vou_buffer *buf, *node;
301 int ret;
302
303 vou_dev->sequence = 0;
304 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
305 video, s_stream, 1);
306 if (ret < 0 && ret != -ENOIOCTLCMD) {
307 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) {
2d700715
JS
308 vb2_buffer_done(&buf->vb.vb2_buf,
309 VB2_BUF_STATE_QUEUED);
57af3ad5
HV
310 list_del(&buf->list);
311 }
312 vou_dev->active = NULL;
313 return ret;
a81fb9b2 314 }
57af3ad5
HV
315
316 buf = list_entry(vou_dev->buf_list.next, struct sh_vou_buffer, list);
317
318 vou_dev->active = buf;
319
320 /* Start from side A: we use mirror addresses, so, set B */
321 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
322 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n",
323 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
324 sh_vou_schedule_next(vou_dev, &buf->vb);
325
326 buf = list_entry(buf->list.next, struct sh_vou_buffer, list);
327
328 /* Second buffer - initialise register side B */
329 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
330 sh_vou_schedule_next(vou_dev, &buf->vb);
331
332 /* Register side switching with frame VSYNC */
333 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
334
335 sh_vou_stream_config(vou_dev);
336 /* Enable End-of-Frame (VSYNC) interrupts */
337 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
338
339 /* Two buffers on the queue - activate the hardware */
340 vou_dev->status = SH_VOU_RUNNING;
341 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
342 return 0;
a81fb9b2
GL
343}
344
57af3ad5 345static void sh_vou_stop_streaming(struct vb2_queue *vq)
a81fb9b2 346{
57af3ad5
HV
347 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
348 struct sh_vou_buffer *buf, *node;
a81fb9b2
GL
349 unsigned long flags;
350
57af3ad5
HV
351 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
352 video, s_stream, 0);
353 /* disable output */
354 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
355 /* ...but the current frame will complete */
356 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
357 msleep(50);
a81fb9b2 358 spin_lock_irqsave(&vou_dev->lock, flags);
57af3ad5 359 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) {
2d700715 360 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
57af3ad5 361 list_del(&buf->list);
a81fb9b2 362 }
57af3ad5 363 vou_dev->active = NULL;
a81fb9b2 364 spin_unlock_irqrestore(&vou_dev->lock, flags);
a81fb9b2
GL
365}
366
57af3ad5
HV
367static struct vb2_ops sh_vou_qops = {
368 .queue_setup = sh_vou_queue_setup,
369 .buf_prepare = sh_vou_buf_prepare,
370 .buf_queue = sh_vou_buf_queue,
371 .start_streaming = sh_vou_start_streaming,
372 .stop_streaming = sh_vou_stop_streaming,
373 .wait_prepare = vb2_ops_wait_prepare,
374 .wait_finish = vb2_ops_wait_finish,
a81fb9b2
GL
375};
376
377/* Video IOCTLs */
378static int sh_vou_querycap(struct file *file, void *priv,
379 struct v4l2_capability *cap)
380{
d899eddd 381 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 382
d899eddd 383 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
384
385 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
d8046ee0
HV
386 strlcpy(cap->driver, "sh-vou", sizeof(cap->driver));
387 strlcpy(cap->bus_info, "platform:sh-vou", sizeof(cap->bus_info));
57af3ad5
HV
388 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE |
389 V4L2_CAP_STREAMING;
a020c747 390 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
a81fb9b2
GL
391 return 0;
392}
393
394/* Enumerate formats, that the device can accept from the user */
395static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
396 struct v4l2_fmtdesc *fmt)
397{
d899eddd 398 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
399
400 if (fmt->index >= ARRAY_SIZE(vou_fmt))
401 return -EINVAL;
402
d899eddd 403 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
404
405 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
406 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
407 sizeof(fmt->description));
408 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
409
410 return 0;
411}
412
413static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
414 struct v4l2_format *fmt)
415{
fd51625d 416 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
417
418 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
419
420 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
421 fmt->fmt.pix = vou_dev->pix;
422
423 return 0;
424}
425
426static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
427static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
428static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
429static const unsigned char vou_scale_v_num[] = {1, 2, 4};
430static const unsigned char vou_scale_v_den[] = {1, 1, 1};
431static const unsigned char vou_scale_v_fld[] = {0, 1};
432
433static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
434 int pix_idx, int w_idx, int h_idx)
435{
436 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
a622cc51 437 unsigned int black_left, black_top, width_max,
a81fb9b2
GL
438 frame_in_height, frame_out_height, frame_out_top;
439 struct v4l2_rect *rect = &vou_dev->rect;
440 struct v4l2_pix_format *pix = &vou_dev->pix;
441 u32 vouvcr = 0, dsr_h, dsr_v;
442
443 if (vou_dev->std & V4L2_STD_525_60) {
444 width_max = 858;
a622cc51 445 /* height_max = 262; */
a81fb9b2
GL
446 } else {
447 width_max = 864;
a622cc51 448 /* height_max = 312; */
a81fb9b2
GL
449 }
450
451 frame_in_height = pix->height / 2;
452 frame_out_height = rect->height / 2;
453 frame_out_top = rect->top / 2;
454
455 /*
456 * Cropping scheme: max useful image is 720x480, and the total video
457 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
458 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
459 * of which the first 33 / 25 clocks HSYNC must be held active. This
460 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
461 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
462 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
463 * beyond DSR, specified on the left and top by the VPR register "black
464 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
465 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
466 * client requires, and that's exactly what leaves us 720 pixels for the
467 * image; we leave VPR[VVP] at default 20 for now, because the client
468 * doesn't seem to have any special requirements for it. Otherwise we
469 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
470 * the selected standard, and DPR and DSR are selected according to
471 * cropping. Q: how does the client detect the first valid line? Does
472 * HSYNC stay inactive during invalid (black) lines?
473 */
474 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
475 black_top = 20;
476
477 dsr_h = rect->width + rect->left;
478 dsr_v = frame_out_height + frame_out_top;
479
480 dev_dbg(vou_dev->v4l2_dev.dev,
481 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
482 pix->width, frame_in_height, black_left, black_top,
483 rect->left, frame_out_top, dsr_h, dsr_v);
484
485 /* VOUISR height - half of a frame height in frame mode */
486 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
487 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
488 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
489 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
490
491 /*
492 * if necessary, we could set VOUHIR to
493 * max(black_left + dsr_h, width_max) here
494 */
495
496 if (w_idx)
497 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
498 if (h_idx)
499 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
500
501 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
502
503 /* To produce a colour bar for testing set bit 23 of VOUVCR */
504 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
505 sh_vou_reg_ab_write(vou_dev, VOUDFR,
506 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
507}
508
509struct sh_vou_geometry {
510 struct v4l2_rect output;
511 unsigned int in_width;
512 unsigned int in_height;
513 int scale_idx_h;
514 int scale_idx_v;
515};
516
517/*
518 * Find input geometry, that we can use to produce output, closest to the
519 * requested rectangle, using VOU scaling
520 */
521static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
522{
523 /* The compiler cannot know, that best and idx will indeed be set */
765fe17c 524 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
a81fb9b2
GL
525 int i, idx = 0;
526
765fe17c
GL
527 if (std & V4L2_STD_525_60)
528 img_height_max = 480;
529 else
530 img_height_max = 576;
a81fb9b2
GL
531
532 /* Image width must be a multiple of 4 */
57af3ad5
HV
533 v4l_bound_align_image(&geo->in_width,
534 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2,
535 &geo->in_height,
536 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
a81fb9b2
GL
537
538 /* Select scales to come as close as possible to the output image */
539 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
540 unsigned int err;
541 unsigned int found = geo->output.width * vou_scale_h_den[i] /
542 vou_scale_h_num[i];
543
544 if (found > VOU_MAX_IMAGE_WIDTH)
545 /* scales increase */
546 break;
547
548 err = abs(found - geo->in_width);
549 if (err < best_err) {
550 best_err = err;
551 idx = i;
552 best = found;
553 }
554 if (!err)
555 break;
556 }
557
558 geo->in_width = best;
559 geo->scale_idx_h = idx;
560
561 best_err = UINT_MAX;
562
563 /* This loop can be replaced with one division */
564 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
565 unsigned int err;
566 unsigned int found = geo->output.height * vou_scale_v_den[i] /
567 vou_scale_v_num[i];
568
765fe17c 569 if (found > img_height_max)
a81fb9b2
GL
570 /* scales increase */
571 break;
572
573 err = abs(found - geo->in_height);
574 if (err < best_err) {
575 best_err = err;
576 idx = i;
577 best = found;
578 }
579 if (!err)
580 break;
581 }
582
583 geo->in_height = best;
584 geo->scale_idx_v = idx;
585}
586
587/*
588 * Find output geometry, that we can produce, using VOU scaling, closest to
589 * the requested rectangle
590 */
591static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
592{
bc1ebd70
GL
593 unsigned int best_err = UINT_MAX, best = geo->in_width,
594 width_max, height_max, img_height_max;
5ac417ef 595 int i, idx_h = 0, idx_v = 0;
a81fb9b2
GL
596
597 if (std & V4L2_STD_525_60) {
598 width_max = 858;
599 height_max = 262 * 2;
765fe17c 600 img_height_max = 480;
a81fb9b2
GL
601 } else {
602 width_max = 864;
603 height_max = 312 * 2;
765fe17c 604 img_height_max = 576;
a81fb9b2
GL
605 }
606
607 /* Select scales to come as close as possible to the output image */
608 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
609 unsigned int err;
610 unsigned int found = geo->in_width * vou_scale_h_num[i] /
611 vou_scale_h_den[i];
612
613 if (found > VOU_MAX_IMAGE_WIDTH)
614 /* scales increase */
615 break;
616
617 err = abs(found - geo->output.width);
618 if (err < best_err) {
619 best_err = err;
5ac417ef 620 idx_h = i;
a81fb9b2
GL
621 best = found;
622 }
623 if (!err)
624 break;
625 }
626
627 geo->output.width = best;
5ac417ef 628 geo->scale_idx_h = idx_h;
a81fb9b2
GL
629 if (geo->output.left + best > width_max)
630 geo->output.left = width_max - best;
631
632 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
5ac417ef 633 vou_scale_h_num[idx_h], vou_scale_h_den[idx_h], best);
a81fb9b2
GL
634
635 best_err = UINT_MAX;
636
637 /* This loop can be replaced with one division */
638 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
639 unsigned int err;
640 unsigned int found = geo->in_height * vou_scale_v_num[i] /
641 vou_scale_v_den[i];
642
765fe17c 643 if (found > img_height_max)
a81fb9b2
GL
644 /* scales increase */
645 break;
646
647 err = abs(found - geo->output.height);
648 if (err < best_err) {
649 best_err = err;
5ac417ef 650 idx_v = i;
a81fb9b2
GL
651 best = found;
652 }
653 if (!err)
654 break;
655 }
656
657 geo->output.height = best;
5ac417ef 658 geo->scale_idx_v = idx_v;
a81fb9b2
GL
659 if (geo->output.top + best > height_max)
660 geo->output.top = height_max - best;
661
662 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
5ac417ef 663 vou_scale_v_num[idx_v], vou_scale_v_den[idx_v], best);
a81fb9b2
GL
664}
665
66853ec4
HV
666static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
667 struct v4l2_format *fmt)
a81fb9b2 668{
fd51625d 669 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 670 struct v4l2_pix_format *pix = &fmt->fmt.pix;
765fe17c 671 unsigned int img_height_max;
a81fb9b2 672 int pix_idx;
a81fb9b2 673
66853ec4 674 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2 675
66853ec4
HV
676 pix->field = V4L2_FIELD_INTERLACED;
677 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
678 pix->ycbcr_enc = pix->quantization = 0;
a81fb9b2
GL
679
680 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
681 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
682 break;
683
684 if (pix_idx == ARRAY_SIZE(vou_fmt))
685 return -EINVAL;
686
765fe17c
GL
687 if (vou_dev->std & V4L2_STD_525_60)
688 img_height_max = 480;
689 else
690 img_height_max = 576;
691
57af3ad5
HV
692 v4l_bound_align_image(&pix->width,
693 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2,
694 &pix->height,
695 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
5c3edcb2
HV
696 pix->bytesperline = pix->width * vou_fmt[pix_idx].bpl;
697 pix->sizeimage = pix->height * ((pix->width * vou_fmt[pix_idx].bpp) >> 3);
66853ec4
HV
698
699 return 0;
700}
701
57af3ad5
HV
702static int sh_vou_set_fmt_vid_out(struct sh_vou_device *vou_dev,
703 struct v4l2_pix_format *pix)
66853ec4 704{
66853ec4
HV
705 unsigned int img_height_max;
706 struct sh_vou_geometry geo;
707 struct v4l2_subdev_format format = {
708 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
709 /* Revisit: is this the correct code? */
710 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
711 .format.field = V4L2_FIELD_INTERLACED,
712 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
713 };
714 struct v4l2_mbus_framefmt *mbfmt = &format.format;
66853ec4 715 int pix_idx;
57af3ad5 716 int ret;
66853ec4 717
57af3ad5
HV
718 if (vb2_is_busy(&vou_dev->queue))
719 return -EBUSY;
66853ec4
HV
720
721 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
722 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
723 break;
a81fb9b2
GL
724
725 geo.in_width = pix->width;
726 geo.in_height = pix->height;
727 geo.output = vou_dev->rect;
728
729 vou_adjust_output(&geo, vou_dev->std);
730
ebf984bb
HV
731 mbfmt->width = geo.output.width;
732 mbfmt->height = geo.output.height;
733 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
734 set_fmt, NULL, &format);
a81fb9b2
GL
735 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
736 if (ret < 0)
737 return ret;
738
739 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
ebf984bb 740 geo.output.width, geo.output.height, mbfmt->width, mbfmt->height);
a81fb9b2 741
66853ec4
HV
742 if (vou_dev->std & V4L2_STD_525_60)
743 img_height_max = 480;
744 else
745 img_height_max = 576;
746
a81fb9b2 747 /* Sanity checks */
ebf984bb
HV
748 if ((unsigned)mbfmt->width > VOU_MAX_IMAGE_WIDTH ||
749 (unsigned)mbfmt->height > img_height_max ||
750 mbfmt->code != MEDIA_BUS_FMT_YUYV8_2X8)
a81fb9b2
GL
751 return -EIO;
752
ebf984bb
HV
753 if (mbfmt->width != geo.output.width ||
754 mbfmt->height != geo.output.height) {
755 geo.output.width = mbfmt->width;
756 geo.output.height = mbfmt->height;
a81fb9b2
GL
757
758 vou_adjust_input(&geo, vou_dev->std);
759 }
760
761 /* We tried to preserve output rectangle, but it could have changed */
762 vou_dev->rect = geo.output;
763 pix->width = geo.in_width;
764 pix->height = geo.in_height;
765
766 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
767 pix->width, pix->height);
768
769 vou_dev->pix_idx = pix_idx;
770
771 vou_dev->pix = *pix;
772
773 sh_vou_configure_geometry(vou_dev, pix_idx,
774 geo.scale_idx_h, geo.scale_idx_v);
775
776 return 0;
777}
778
57af3ad5
HV
779static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
780 struct v4l2_format *fmt)
a81fb9b2 781{
fd51625d 782 struct sh_vou_device *vou_dev = video_drvdata(file);
57af3ad5 783 int ret = sh_vou_try_fmt_vid_out(file, priv, fmt);
a81fb9b2 784
57af3ad5 785 if (ret)
a81fb9b2 786 return ret;
57af3ad5 787 return sh_vou_set_fmt_vid_out(vou_dev, &fmt->fmt.pix);
a81fb9b2
GL
788}
789
4de00d0e
HV
790static int sh_vou_enum_output(struct file *file, void *fh,
791 struct v4l2_output *a)
792{
793 struct sh_vou_device *vou_dev = video_drvdata(file);
794
795 if (a->index)
796 return -EINVAL;
797 strlcpy(a->name, "Video Out", sizeof(a->name));
798 a->type = V4L2_OUTPUT_TYPE_ANALOG;
799 a->std = vou_dev->vdev.tvnorms;
800 return 0;
801}
802
2a1e91a1 803static int sh_vou_g_output(struct file *file, void *fh, unsigned int *i)
4de00d0e
HV
804{
805 *i = 0;
806 return 0;
807}
808
2a1e91a1 809static int sh_vou_s_output(struct file *file, void *fh, unsigned int i)
4de00d0e
HV
810{
811 return i ? -EINVAL : 0;
812}
813
a81fb9b2
GL
814static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
815{
816 switch (bus_fmt) {
817 default:
818 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
819 __func__, bus_fmt);
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;
826 }
827}
828
314527ac 829static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
a81fb9b2 830{
fd51625d 831 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
832 int ret;
833
314527ac 834 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
a81fb9b2 835
57af3ad5
HV
836 if (std_id == vou_dev->std)
837 return 0;
838
839 if (vb2_is_busy(&vou_dev->queue))
840 return -EBUSY;
a81fb9b2
GL
841
842 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
314527ac 843 s_std_output, std_id);
a81fb9b2
GL
844 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
845 if (ret < 0 && ret != -ENOIOCTLCMD)
846 return ret;
847
57af3ad5
HV
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) {
a81fb9b2
GL
851 sh_vou_reg_ab_set(vou_dev, VOUCR,
852 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
57af3ad5
HV
853 vou_dev->rect.height = 480;
854 } else {
a81fb9b2 855 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
57af3ad5
HV
856 vou_dev->rect.height = 576;
857 }
a81fb9b2 858
57af3ad5
HV
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);
314527ac 865 vou_dev->std = std_id;
57af3ad5 866 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix);
a81fb9b2
GL
867
868 return 0;
869}
870
871static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
872{
fd51625d 873 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
874
875 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
876
877 *std = vou_dev->std;
878
879 return 0;
880}
881
0b3474f0
HV
882static int sh_vou_log_status(struct file *file, void *priv)
883{
884 struct sh_vou_device *vou_dev = video_drvdata(file);
885
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;
907}
908
61fbacc1
HV
909static int sh_vou_g_selection(struct file *file, void *fh,
910 struct v4l2_selection *sel)
a81fb9b2 911{
fd51625d 912 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 913
61fbacc1
HV
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;
57af3ad5
HV
925 if (vou_dev->std & V4L2_STD_525_60)
926 sel->r.height = 480;
927 else
928 sel->r.height = 576;
61fbacc1
HV
929 break;
930 default:
931 return -EINVAL;
932 }
a81fb9b2
GL
933 return 0;
934}
935
936/* Assume a dull encoder, do all the work ourselves. */
61fbacc1
HV
937static int sh_vou_s_selection(struct file *file, void *fh,
938 struct v4l2_selection *sel)
a81fb9b2 939{
61fbacc1 940 struct v4l2_rect *rect = &sel->r;
fd51625d 941 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
942 struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
943 struct v4l2_pix_format *pix = &vou_dev->pix;
944 struct sh_vou_geometry geo;
ebf984bb
HV
945 struct v4l2_subdev_format format = {
946 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
a81fb9b2 947 /* Revisit: is this the correct code? */
ebf984bb
HV
948 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
949 .format.field = V4L2_FIELD_INTERLACED,
950 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
a81fb9b2 951 };
765fe17c 952 unsigned int img_height_max;
a81fb9b2
GL
953 int ret;
954
61fbacc1
HV
955 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
956 sel->target != V4L2_SEL_TGT_COMPOSE)
a81fb9b2
GL
957 return -EINVAL;
958
57af3ad5
HV
959 if (vb2_is_busy(&vou_dev->queue))
960 return -EBUSY;
961
765fe17c
GL
962 if (vou_dev->std & V4L2_STD_525_60)
963 img_height_max = 480;
964 else
965 img_height_max = 576;
966
57af3ad5
HV
967 v4l_bound_align_image(&rect->width,
968 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 1,
969 &rect->height,
970 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
a81fb9b2
GL
971
972 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
973 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
974
765fe17c
GL
975 if (rect->height + rect->top > img_height_max)
976 rect->top = img_height_max - rect->height;
a81fb9b2
GL
977
978 geo.output = *rect;
979 geo.in_width = pix->width;
980 geo.in_height = pix->height;
981
982 /* Configure the encoder one-to-one, position at 0, ignore errors */
983 sd_crop.c.width = geo.output.width;
984 sd_crop.c.height = geo.output.height;
985 /*
986 * We first issue a S_CROP, so that the subsequent S_FMT delivers the
987 * final encoder configuration.
988 */
989 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
990 s_crop, &sd_crop);
ebf984bb
HV
991 format.format.width = geo.output.width;
992 format.format.height = geo.output.height;
993 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
994 set_fmt, NULL, &format);
a81fb9b2
GL
995 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
996 if (ret < 0)
997 return ret;
998
999 /* Sanity checks */
ebf984bb
HV
1000 if ((unsigned)format.format.width > VOU_MAX_IMAGE_WIDTH ||
1001 (unsigned)format.format.height > img_height_max ||
1002 format.format.code != MEDIA_BUS_FMT_YUYV8_2X8)
a81fb9b2
GL
1003 return -EIO;
1004
ebf984bb
HV
1005 geo.output.width = format.format.width;
1006 geo.output.height = format.format.height;
a81fb9b2
GL
1007
1008 /*
1009 * No down-scaling. According to the API, current call has precedence:
1010 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1011 */
1012 vou_adjust_input(&geo, vou_dev->std);
1013
1014 /* We tried to preserve output rectangle, but it could have changed */
1015 vou_dev->rect = geo.output;
1016 pix->width = geo.in_width;
1017 pix->height = geo.in_height;
1018
1019 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1020 geo.scale_idx_h, geo.scale_idx_v);
1021
1022 return 0;
1023}
1024
a81fb9b2
GL
1025static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1026{
1027 struct sh_vou_device *vou_dev = dev_id;
1028 static unsigned long j;
57af3ad5 1029 struct sh_vou_buffer *vb;
a81fb9b2 1030 static int cnt;
a81fb9b2
GL
1031 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1032 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1033
1034 if (!(irq_status & 0x300)) {
1035 if (printk_timed_ratelimit(&j, 500))
1036 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1037 irq_status);
1038 return IRQ_NONE;
1039 }
1040
1041 spin_lock(&vou_dev->lock);
57af3ad5 1042 if (!vou_dev->active || list_empty(&vou_dev->buf_list)) {
a81fb9b2
GL
1043 if (printk_timed_ratelimit(&j, 500))
1044 dev_warn(vou_dev->v4l2_dev.dev,
1045 "IRQ without active buffer: %x!\n", irq_status);
1046 /* Just ack: buf_release will disable further interrupts */
1047 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1048 spin_unlock(&vou_dev->lock);
1049 return IRQ_HANDLED;
1050 }
1051
1052 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1053 dev_dbg(vou_dev->v4l2_dev.dev,
1054 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1055 irq_status, masked, vou_status, cnt);
1056
1057 cnt++;
a622cc51 1058 /* side = vou_status & 0x10000; */
a81fb9b2
GL
1059
1060 /* Clear only set interrupts */
1061 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1062
1063 vb = vou_dev->active;
57af3ad5
HV
1064 if (list_is_singular(&vb->list)) {
1065 /* Keep cycling while no next buffer is available */
1066 sh_vou_schedule_next(vou_dev, &vb->vb);
a81fb9b2
GL
1067 spin_unlock(&vou_dev->lock);
1068 return IRQ_HANDLED;
1069 }
1070
57af3ad5
HV
1071 list_del(&vb->list);
1072
d6dd645e 1073 vb->vb.vb2_buf.timestamp = ktime_get_ns();
2d700715
JS
1074 vb->vb.sequence = vou_dev->sequence++;
1075 vb->vb.field = V4L2_FIELD_INTERLACED;
1076 vb2_buffer_done(&vb->vb.vb2_buf, VB2_BUF_STATE_DONE);
57af3ad5
HV
1077
1078 vou_dev->active = list_entry(vou_dev->buf_list.next,
1079 struct sh_vou_buffer, list);
a81fb9b2 1080
57af3ad5
HV
1081 if (list_is_singular(&vou_dev->buf_list)) {
1082 /* Keep cycling while no next buffer is available */
1083 sh_vou_schedule_next(vou_dev, &vou_dev->active->vb);
1084 } else {
1085 struct sh_vou_buffer *new = list_entry(vou_dev->active->list.next,
1086 struct sh_vou_buffer, list);
1087 sh_vou_schedule_next(vou_dev, &new->vb);
a81fb9b2
GL
1088 }
1089
1090 spin_unlock(&vou_dev->lock);
1091
1092 return IRQ_HANDLED;
1093}
1094
1095static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1096{
1097 struct sh_vou_pdata *pdata = vou_dev->pdata;
1098 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1099 int i = 100;
1100
1101 /* Disable all IRQs */
1102 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1103
1104 /* Reset VOU interfaces - registers unaffected */
1105 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1106 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1107 udelay(1);
1108
1109 if (!i)
1110 return -ETIMEDOUT;
1111
1112 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1113
1114 if (pdata->flags & SH_VOU_PCLK_FALLING)
1115 voucr |= 1 << 28;
1116 if (pdata->flags & SH_VOU_HSYNC_LOW)
1117 voucr |= 1 << 27;
1118 if (pdata->flags & SH_VOU_VSYNC_LOW)
1119 voucr |= 1 << 26;
1120 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1121
1122 /* Manual register side switching at first */
1123 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1124 /* Default - fixed HSYNC length, can be made configurable is required */
1125 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1126
57af3ad5
HV
1127 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix);
1128
a81fb9b2
GL
1129 return 0;
1130}
1131
1132/* File operations */
1133static int sh_vou_open(struct file *file)
1134{
fd51625d 1135 struct sh_vou_device *vou_dev = video_drvdata(file);
57af3ad5 1136 int err;
a81fb9b2 1137
57af3ad5 1138 if (mutex_lock_interruptible(&vou_dev->fop_lock))
f135a8a2 1139 return -ERESTARTSYS;
c5f98085 1140
57af3ad5
HV
1141 err = v4l2_fh_open(file);
1142 if (err)
1143 goto done_open;
1144 if (v4l2_fh_is_singular_file(file) &&
1145 vou_dev->status == SH_VOU_INITIALISING) {
a81fb9b2 1146 /* First open */
fd51625d 1147 pm_runtime_get_sync(vou_dev->v4l2_dev.dev);
57af3ad5
HV
1148 err = sh_vou_hw_init(vou_dev);
1149 if (err < 0) {
fd51625d 1150 pm_runtime_put(vou_dev->v4l2_dev.dev);
57af3ad5
HV
1151 v4l2_fh_release(file);
1152 } else {
a81fb9b2 1153 vou_dev->status = SH_VOU_IDLE;
a81fb9b2
GL
1154 }
1155 }
57af3ad5 1156done_open:
f135a8a2 1157 mutex_unlock(&vou_dev->fop_lock);
57af3ad5 1158 return err;
a81fb9b2
GL
1159}
1160
1161static int sh_vou_release(struct file *file)
1162{
fd51625d 1163 struct sh_vou_device *vou_dev = video_drvdata(file);
57af3ad5 1164 bool is_last;
a81fb9b2 1165
c5f98085 1166 mutex_lock(&vou_dev->fop_lock);
57af3ad5
HV
1167 is_last = v4l2_fh_is_singular_file(file);
1168 _vb2_fop_release(file, NULL);
1169 if (is_last) {
a81fb9b2 1170 /* Last close */
57af3ad5 1171 vou_dev->status = SH_VOU_INITIALISING;
a81fb9b2 1172 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
fd51625d 1173 pm_runtime_put(vou_dev->v4l2_dev.dev);
a81fb9b2 1174 }
c5f98085 1175 mutex_unlock(&vou_dev->fop_lock);
a81fb9b2
GL
1176 return 0;
1177}
1178
a81fb9b2
GL
1179/* sh_vou display ioctl operations */
1180static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1181 .vidioc_querycap = sh_vou_querycap,
1182 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1183 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1184 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1185 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
57af3ad5
HV
1186 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1187 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1188 .vidioc_querybuf = vb2_ioctl_querybuf,
1189 .vidioc_qbuf = vb2_ioctl_qbuf,
1190 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1191 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1192 .vidioc_streamon = vb2_ioctl_streamon,
1193 .vidioc_streamoff = vb2_ioctl_streamoff,
1194 .vidioc_expbuf = vb2_ioctl_expbuf,
4de00d0e
HV
1195 .vidioc_g_output = sh_vou_g_output,
1196 .vidioc_s_output = sh_vou_s_output,
1197 .vidioc_enum_output = sh_vou_enum_output,
a81fb9b2
GL
1198 .vidioc_s_std = sh_vou_s_std,
1199 .vidioc_g_std = sh_vou_g_std,
61fbacc1
HV
1200 .vidioc_g_selection = sh_vou_g_selection,
1201 .vidioc_s_selection = sh_vou_s_selection,
0b3474f0 1202 .vidioc_log_status = sh_vou_log_status,
a81fb9b2
GL
1203};
1204
1205static const struct v4l2_file_operations sh_vou_fops = {
1206 .owner = THIS_MODULE,
1207 .open = sh_vou_open,
1208 .release = sh_vou_release,
69756693 1209 .unlocked_ioctl = video_ioctl2,
57af3ad5
HV
1210 .mmap = vb2_fop_mmap,
1211 .poll = vb2_fop_poll,
1212 .write = vb2_fop_write,
a81fb9b2
GL
1213};
1214
1215static const struct video_device sh_vou_video_template = {
1216 .name = "sh_vou",
1217 .fops = &sh_vou_fops,
1218 .ioctl_ops = &sh_vou_ioctl_ops,
1219 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
954f340f 1220 .vfl_dir = VFL_DIR_TX,
a81fb9b2
GL
1221};
1222
4c62e976 1223static int sh_vou_probe(struct platform_device *pdev)
a81fb9b2
GL
1224{
1225 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1226 struct v4l2_rect *rect;
1227 struct v4l2_pix_format *pix;
1228 struct i2c_adapter *i2c_adap;
1229 struct video_device *vdev;
1230 struct sh_vou_device *vou_dev;
4690271c 1231 struct resource *reg_res;
a81fb9b2 1232 struct v4l2_subdev *subdev;
57af3ad5 1233 struct vb2_queue *q;
a81fb9b2
GL
1234 int irq, ret;
1235
1236 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1237 irq = platform_get_irq(pdev, 0);
1238
1239 if (!vou_pdata || !reg_res || irq <= 0) {
1240 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1241 return -ENODEV;
1242 }
1243
4690271c 1244 vou_dev = devm_kzalloc(&pdev->dev, sizeof(*vou_dev), GFP_KERNEL);
a81fb9b2
GL
1245 if (!vou_dev)
1246 return -ENOMEM;
1247
57af3ad5 1248 INIT_LIST_HEAD(&vou_dev->buf_list);
a81fb9b2 1249 spin_lock_init(&vou_dev->lock);
69756693 1250 mutex_init(&vou_dev->fop_lock);
a81fb9b2 1251 vou_dev->pdata = vou_pdata;
57af3ad5
HV
1252 vou_dev->status = SH_VOU_INITIALISING;
1253 vou_dev->pix_idx = 1;
a81fb9b2
GL
1254
1255 rect = &vou_dev->rect;
1256 pix = &vou_dev->pix;
1257
1258 /* Fill in defaults */
5d408108 1259 vou_dev->std = V4L2_STD_NTSC_M;
a81fb9b2
GL
1260 rect->left = 0;
1261 rect->top = 0;
1262 rect->width = VOU_MAX_IMAGE_WIDTH;
765fe17c 1263 rect->height = 480;
a81fb9b2 1264 pix->width = VOU_MAX_IMAGE_WIDTH;
765fe17c 1265 pix->height = 480;
22df2e7a 1266 pix->pixelformat = V4L2_PIX_FMT_NV16;
57af3ad5 1267 pix->field = V4L2_FIELD_INTERLACED;
5c3edcb2 1268 pix->bytesperline = VOU_MAX_IMAGE_WIDTH;
765fe17c 1269 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
a81fb9b2
GL
1270 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1271
4690271c
HV
1272 vou_dev->base = devm_ioremap_resource(&pdev->dev, reg_res);
1273 if (IS_ERR(vou_dev->base))
1274 return PTR_ERR(vou_dev->base);
a81fb9b2 1275
4690271c 1276 ret = devm_request_irq(&pdev->dev, irq, sh_vou_isr, 0, "vou", vou_dev);
a81fb9b2 1277 if (ret < 0)
4690271c 1278 return ret;
a81fb9b2
GL
1279
1280 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1281 if (ret < 0) {
1282 dev_err(&pdev->dev, "Error registering v4l2 device\n");
4690271c 1283 return ret;
a81fb9b2
GL
1284 }
1285
d079f99f 1286 vdev = &vou_dev->vdev;
a81fb9b2
GL
1287 *vdev = sh_vou_video_template;
1288 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1289 vdev->tvnorms |= V4L2_STD_PAL;
1290 vdev->v4l2_dev = &vou_dev->v4l2_dev;
d079f99f 1291 vdev->release = video_device_release_empty;
69756693 1292 vdev->lock = &vou_dev->fop_lock;
a81fb9b2 1293
a81fb9b2
GL
1294 video_set_drvdata(vdev, vou_dev);
1295
57af3ad5
HV
1296 /* Initialize the vb2 queue */
1297 q = &vou_dev->queue;
1298 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1299 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_WRITE;
1300 q->drv_priv = vou_dev;
1301 q->buf_struct_size = sizeof(struct sh_vou_buffer);
1302 q->ops = &sh_vou_qops;
1303 q->mem_ops = &vb2_dma_contig_memops;
1304 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1305 q->min_buffers_needed = 2;
1306 q->lock = &vou_dev->fop_lock;
1307 ret = vb2_queue_init(q);
1308 if (ret)
1309 goto einitctx;
1310
1311 vou_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1312 if (IS_ERR(vou_dev->alloc_ctx)) {
1313 dev_err(&pdev->dev, "Can't allocate buffer context");
1314 ret = PTR_ERR(vou_dev->alloc_ctx);
1315 goto einitctx;
1316 }
1317 vdev->queue = q;
1318 INIT_LIST_HEAD(&vou_dev->buf_list);
1319
a81fb9b2
GL
1320 pm_runtime_enable(&pdev->dev);
1321 pm_runtime_resume(&pdev->dev);
1322
1323 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1324 if (!i2c_adap) {
1325 ret = -ENODEV;
1326 goto ei2cgadap;
1327 }
1328
1329 ret = sh_vou_hw_init(vou_dev);
1330 if (ret < 0)
1331 goto ereset;
1332
1333 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
9a1f8b34 1334 vou_pdata->board_info, NULL);
a81fb9b2
GL
1335 if (!subdev) {
1336 ret = -ENOMEM;
1337 goto ei2cnd;
1338 }
1339
1340 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1341 if (ret < 0)
1342 goto evregdev;
1343
1344 return 0;
1345
1346evregdev:
1347ei2cnd:
1348ereset:
1349 i2c_put_adapter(i2c_adap);
1350ei2cgadap:
57af3ad5
HV
1351 vb2_dma_contig_cleanup_ctx(vou_dev->alloc_ctx);
1352einitctx:
a81fb9b2 1353 pm_runtime_disable(&pdev->dev);
a81fb9b2 1354 v4l2_device_unregister(&vou_dev->v4l2_dev);
a81fb9b2
GL
1355 return ret;
1356}
1357
4c62e976 1358static int sh_vou_remove(struct platform_device *pdev)
a81fb9b2 1359{
a81fb9b2
GL
1360 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1361 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1362 struct sh_vou_device, v4l2_dev);
1363 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1364 struct v4l2_subdev, list);
1365 struct i2c_client *client = v4l2_get_subdevdata(sd);
a81fb9b2 1366
a81fb9b2 1367 pm_runtime_disable(&pdev->dev);
d079f99f 1368 video_unregister_device(&vou_dev->vdev);
a81fb9b2 1369 i2c_put_adapter(client->adapter);
57af3ad5 1370 vb2_dma_contig_cleanup_ctx(vou_dev->alloc_ctx);
a81fb9b2 1371 v4l2_device_unregister(&vou_dev->v4l2_dev);
a81fb9b2
GL
1372 return 0;
1373}
1374
1375static struct platform_driver __refdata sh_vou = {
4c62e976 1376 .remove = sh_vou_remove,
a81fb9b2
GL
1377 .driver = {
1378 .name = "sh-vou",
a81fb9b2
GL
1379 },
1380};
1381
b3fd87bc 1382module_platform_driver_probe(sh_vou, sh_vou_probe);
a81fb9b2
GL
1383
1384MODULE_DESCRIPTION("SuperH VOU driver");
1385MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1386MODULE_LICENSE("GPL v2");
64dc3c1a 1387MODULE_VERSION("0.1.0");
a81fb9b2 1388MODULE_ALIAS("platform:sh-vou");
This page took 0.437952 seconds and 5 git commands to generate.