blob: 262c244fcc3921ad46245de3d5c241284c8ae705 [file] [log] [blame]
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001/*
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>
Guennadi Liakhovetski3b23db32010-07-08 05:12:11 -030021#include <linux/slab.h>
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -030022#include <linux/videodev2.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040023#include <linux/module.h>
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -030024
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/videobuf-dma-contig.h>
31
32/* Mirror addresses are not available for all registers */
33#define VOUER 0
34#define VOUCR 4
35#define VOUSTR 8
36#define VOUVCR 0xc
37#define VOUISR 0x10
38#define VOUBCR 0x14
39#define VOUDPR 0x18
40#define VOUDSR 0x1c
41#define VOUVPR 0x20
42#define VOUIR 0x24
43#define VOUSRR 0x28
44#define VOUMSR 0x2c
45#define VOUHIR 0x30
46#define VOUDFR 0x34
47#define VOUAD1R 0x38
48#define VOUAD2R 0x3c
49#define VOUAIR 0x40
50#define VOUSWR 0x44
51#define VOURCR 0x48
52#define VOURPR 0x50
53
54enum sh_vou_status {
55 SH_VOU_IDLE,
56 SH_VOU_INITIALISING,
57 SH_VOU_RUNNING,
58};
59
60#define VOU_MAX_IMAGE_WIDTH 720
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -030061#define VOU_MAX_IMAGE_HEIGHT 576
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -030062
63struct sh_vou_device {
64 struct v4l2_device v4l2_dev;
Lad, Prabhakard079f992015-03-09 19:10:51 -030065 struct video_device vdev;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -030066 struct sh_vou_pdata *pdata;
67 spinlock_t lock;
68 void __iomem *base;
69 /* State information */
70 struct v4l2_pix_format pix;
71 struct v4l2_rect rect;
72 struct list_head queue;
73 v4l2_std_id std;
74 int pix_idx;
75 struct videobuf_buffer *active;
76 enum sh_vou_status status;
Hans Verkuil69756692010-11-16 18:12:16 -030077 struct mutex fop_lock;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -030078};
79
80struct sh_vou_file {
Hans Verkuilc5f98082015-06-07 05:57:58 -030081 struct v4l2_fh fh;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -030082 struct videobuf_queue vbq;
83};
84
85/* Register access routines for sides A, B and mirror addresses */
86static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
87 u32 value)
88{
89 __raw_writel(value, vou_dev->base + reg);
90}
91
92static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
93 u32 value)
94{
95 __raw_writel(value, vou_dev->base + reg);
96 __raw_writel(value, vou_dev->base + reg + 0x1000);
97}
98
99static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
100 u32 value)
101{
102 __raw_writel(value, vou_dev->base + reg + 0x2000);
103}
104
105static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
106{
107 return __raw_readl(vou_dev->base + reg);
108}
109
110static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
111 u32 value, u32 mask)
112{
113 u32 old = __raw_readl(vou_dev->base + reg);
114
115 value = (value & mask) | (old & ~mask);
116 __raw_writel(value, vou_dev->base + reg);
117}
118
119static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
120 u32 value, u32 mask)
121{
122 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
123}
124
125static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
126 u32 value, u32 mask)
127{
128 sh_vou_reg_a_set(vou_dev, reg, value, mask);
129 sh_vou_reg_b_set(vou_dev, reg, value, mask);
130}
131
132struct sh_vou_fmt {
133 u32 pfmt;
134 char *desc;
135 unsigned char bpp;
136 unsigned char rgb;
137 unsigned char yf;
138 unsigned char pkf;
139};
140
141/* Further pixel formats can be added */
142static struct sh_vou_fmt vou_fmt[] = {
143 {
144 .pfmt = V4L2_PIX_FMT_NV12,
145 .bpp = 12,
146 .desc = "YVU420 planar",
147 .yf = 0,
148 .rgb = 0,
149 },
150 {
151 .pfmt = V4L2_PIX_FMT_NV16,
152 .bpp = 16,
153 .desc = "YVYU planar",
154 .yf = 1,
155 .rgb = 0,
156 },
157 {
158 .pfmt = V4L2_PIX_FMT_RGB24,
159 .bpp = 24,
160 .desc = "RGB24",
161 .pkf = 2,
162 .rgb = 1,
163 },
164 {
165 .pfmt = V4L2_PIX_FMT_RGB565,
166 .bpp = 16,
167 .desc = "RGB565",
168 .pkf = 3,
169 .rgb = 1,
170 },
171 {
172 .pfmt = V4L2_PIX_FMT_RGB565X,
173 .bpp = 16,
174 .desc = "RGB565 byteswapped",
175 .pkf = 3,
176 .rgb = 1,
177 },
178};
179
180static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
181 struct videobuf_buffer *vb)
182{
183 dma_addr_t addr1, addr2;
184
185 addr1 = videobuf_to_dma_contig(vb);
186 switch (vou_dev->pix.pixelformat) {
187 case V4L2_PIX_FMT_NV12:
188 case V4L2_PIX_FMT_NV16:
189 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
190 break;
191 default:
192 addr2 = 0;
193 }
194
195 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
196 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
197}
198
199static void sh_vou_stream_start(struct sh_vou_device *vou_dev,
200 struct videobuf_buffer *vb)
201{
202 unsigned int row_coeff;
203#ifdef __LITTLE_ENDIAN
204 u32 dataswap = 7;
205#else
206 u32 dataswap = 0;
207#endif
208
209 switch (vou_dev->pix.pixelformat) {
Guennadi Liakhovetskibc1ebd72012-11-26 14:32:48 -0300210 default:
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300211 case V4L2_PIX_FMT_NV12:
212 case V4L2_PIX_FMT_NV16:
213 row_coeff = 1;
214 break;
215 case V4L2_PIX_FMT_RGB565:
216 dataswap ^= 1;
217 case V4L2_PIX_FMT_RGB565X:
218 row_coeff = 2;
219 break;
220 case V4L2_PIX_FMT_RGB24:
221 row_coeff = 3;
222 break;
223 }
224
225 sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
226 sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
227 sh_vou_schedule_next(vou_dev, vb);
228}
229
230static void free_buffer(struct videobuf_queue *vq, struct videobuf_buffer *vb)
231{
232 BUG_ON(in_interrupt());
233
234 /* Wait until this buffer is no longer in STATE_QUEUED or STATE_ACTIVE */
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300235 videobuf_waiton(vq, vb, 0, 0);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300236 videobuf_dma_contig_free(vq, vb);
237 vb->state = VIDEOBUF_NEEDS_INIT;
238}
239
Hans Verkuil69756692010-11-16 18:12:16 -0300240/* Locking: caller holds fop_lock mutex */
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300241static int sh_vou_buf_setup(struct videobuf_queue *vq, unsigned int *count,
242 unsigned int *size)
243{
244 struct video_device *vdev = vq->priv_data;
245 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
246
247 *size = vou_fmt[vou_dev->pix_idx].bpp * vou_dev->pix.width *
248 vou_dev->pix.height / 8;
249
250 if (*count < 2)
251 *count = 2;
252
253 /* Taking into account maximum frame size, *count will stay >= 2 */
254 if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
255 *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
256
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300257 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): count=%d, size=%d\n", __func__,
258 *count, *size);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300259
260 return 0;
261}
262
Hans Verkuil69756692010-11-16 18:12:16 -0300263/* Locking: caller holds fop_lock mutex */
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300264static int sh_vou_buf_prepare(struct videobuf_queue *vq,
265 struct videobuf_buffer *vb,
266 enum v4l2_field field)
267{
268 struct video_device *vdev = vq->priv_data;
269 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
270 struct v4l2_pix_format *pix = &vou_dev->pix;
271 int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
272 int ret;
273
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300274 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300275
276 if (vb->width != pix->width ||
277 vb->height != pix->height ||
278 vb->field != pix->field) {
279 vb->width = pix->width;
280 vb->height = pix->height;
281 vb->field = field;
282 if (vb->state != VIDEOBUF_NEEDS_INIT)
283 free_buffer(vq, vb);
284 }
285
286 vb->size = vb->height * bytes_per_line;
287 if (vb->baddr && vb->bsize < vb->size) {
288 /* User buffer too small */
Laurent Pinchart3f8965e2013-12-11 09:33:03 -0300289 dev_warn(vq->dev, "User buffer too small: [%zu] @ %lx\n",
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300290 vb->bsize, vb->baddr);
291 return -EINVAL;
292 }
293
294 if (vb->state == VIDEOBUF_NEEDS_INIT) {
295 ret = videobuf_iolock(vq, vb, NULL);
296 if (ret < 0) {
297 dev_warn(vq->dev, "IOLOCK buf-type %d: %d\n",
298 vb->memory, ret);
299 return ret;
300 }
301 vb->state = VIDEOBUF_PREPARED;
302 }
303
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300304 dev_dbg(vou_dev->v4l2_dev.dev,
Laurent Pinchart3f8965e2013-12-11 09:33:03 -0300305 "%s(): fmt #%d, %u bytes per line, phys %pad, type %d, state %d\n",
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300306 __func__, vou_dev->pix_idx, bytes_per_line,
Laurent Pinchart3f8965e2013-12-11 09:33:03 -0300307 ({ dma_addr_t addr = videobuf_to_dma_contig(vb); &addr; }),
308 vb->memory, vb->state);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300309
310 return 0;
311}
312
Hans Verkuil69756692010-11-16 18:12:16 -0300313/* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300314static void sh_vou_buf_queue(struct videobuf_queue *vq,
315 struct videobuf_buffer *vb)
316{
317 struct video_device *vdev = vq->priv_data;
318 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
319
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300320 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300321
322 vb->state = VIDEOBUF_QUEUED;
323 list_add_tail(&vb->queue, &vou_dev->queue);
324
325 if (vou_dev->status == SH_VOU_RUNNING) {
326 return;
327 } else if (!vou_dev->active) {
328 vou_dev->active = vb;
329 /* Start from side A: we use mirror addresses, so, set B */
330 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300331 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n",
332 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300333 sh_vou_schedule_next(vou_dev, vb);
334 /* Only activate VOU after the second buffer */
335 } else if (vou_dev->active->queue.next == &vb->queue) {
336 /* Second buffer - initialise register side B */
337 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
338 sh_vou_stream_start(vou_dev, vb);
339
340 /* Register side switching with frame VSYNC */
341 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300342 dev_dbg(vou_dev->v4l2_dev.dev, "%s: second buffer status 0x%x\n",
343 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300344
345 /* Enable End-of-Frame (VSYNC) interrupts */
346 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
347 /* Two buffers on the queue - activate the hardware */
348
349 vou_dev->status = SH_VOU_RUNNING;
350 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
351 }
352}
353
354static void sh_vou_buf_release(struct videobuf_queue *vq,
355 struct videobuf_buffer *vb)
356{
357 struct video_device *vdev = vq->priv_data;
358 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
359 unsigned long flags;
360
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300361 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300362
363 spin_lock_irqsave(&vou_dev->lock, flags);
364
365 if (vou_dev->active == vb) {
366 /* disable output */
367 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
368 /* ...but the current frame will complete */
369 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
370 vou_dev->active = NULL;
371 }
372
373 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED)) {
374 vb->state = VIDEOBUF_ERROR;
375 list_del(&vb->queue);
376 }
377
378 spin_unlock_irqrestore(&vou_dev->lock, flags);
379
380 free_buffer(vq, vb);
381}
382
383static struct videobuf_queue_ops sh_vou_video_qops = {
384 .buf_setup = sh_vou_buf_setup,
385 .buf_prepare = sh_vou_buf_prepare,
386 .buf_queue = sh_vou_buf_queue,
387 .buf_release = sh_vou_buf_release,
388};
389
390/* Video IOCTLs */
391static int sh_vou_querycap(struct file *file, void *priv,
392 struct v4l2_capability *cap)
393{
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300394 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300395
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300396 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300397
398 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
Hans Verkuild8046ee2015-06-07 05:57:57 -0300399 strlcpy(cap->driver, "sh-vou", sizeof(cap->driver));
400 strlcpy(cap->bus_info, "platform:sh-vou", sizeof(cap->bus_info));
Hans Verkuila020c742014-11-24 06:37:25 -0300401 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
402 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300403 return 0;
404}
405
406/* Enumerate formats, that the device can accept from the user */
407static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
408 struct v4l2_fmtdesc *fmt)
409{
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300410 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300411
412 if (fmt->index >= ARRAY_SIZE(vou_fmt))
413 return -EINVAL;
414
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300415 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300416
417 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
418 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
419 sizeof(fmt->description));
420 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
421
422 return 0;
423}
424
425static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
426 struct v4l2_format *fmt)
427{
Laurent Pinchartfd516252013-01-03 15:35:56 -0300428 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300429
430 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
431
432 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
433 fmt->fmt.pix = vou_dev->pix;
434
435 return 0;
436}
437
438static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
439static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
440static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
441static const unsigned char vou_scale_v_num[] = {1, 2, 4};
442static const unsigned char vou_scale_v_den[] = {1, 1, 1};
443static const unsigned char vou_scale_v_fld[] = {0, 1};
444
445static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
446 int pix_idx, int w_idx, int h_idx)
447{
448 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
Mauro Carvalho Chehaba622cc52014-01-13 11:30:26 -0200449 unsigned int black_left, black_top, width_max,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300450 frame_in_height, frame_out_height, frame_out_top;
451 struct v4l2_rect *rect = &vou_dev->rect;
452 struct v4l2_pix_format *pix = &vou_dev->pix;
453 u32 vouvcr = 0, dsr_h, dsr_v;
454
455 if (vou_dev->std & V4L2_STD_525_60) {
456 width_max = 858;
Mauro Carvalho Chehaba622cc52014-01-13 11:30:26 -0200457 /* height_max = 262; */
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300458 } else {
459 width_max = 864;
Mauro Carvalho Chehaba622cc52014-01-13 11:30:26 -0200460 /* height_max = 312; */
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300461 }
462
463 frame_in_height = pix->height / 2;
464 frame_out_height = rect->height / 2;
465 frame_out_top = rect->top / 2;
466
467 /*
468 * Cropping scheme: max useful image is 720x480, and the total video
469 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
470 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
471 * of which the first 33 / 25 clocks HSYNC must be held active. This
472 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
473 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
474 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
475 * beyond DSR, specified on the left and top by the VPR register "black
476 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
477 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
478 * client requires, and that's exactly what leaves us 720 pixels for the
479 * image; we leave VPR[VVP] at default 20 for now, because the client
480 * doesn't seem to have any special requirements for it. Otherwise we
481 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
482 * the selected standard, and DPR and DSR are selected according to
483 * cropping. Q: how does the client detect the first valid line? Does
484 * HSYNC stay inactive during invalid (black) lines?
485 */
486 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
487 black_top = 20;
488
489 dsr_h = rect->width + rect->left;
490 dsr_v = frame_out_height + frame_out_top;
491
492 dev_dbg(vou_dev->v4l2_dev.dev,
493 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
494 pix->width, frame_in_height, black_left, black_top,
495 rect->left, frame_out_top, dsr_h, dsr_v);
496
497 /* VOUISR height - half of a frame height in frame mode */
498 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
499 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
500 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
501 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
502
503 /*
504 * if necessary, we could set VOUHIR to
505 * max(black_left + dsr_h, width_max) here
506 */
507
508 if (w_idx)
509 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
510 if (h_idx)
511 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
512
513 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
514
515 /* To produce a colour bar for testing set bit 23 of VOUVCR */
516 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
517 sh_vou_reg_ab_write(vou_dev, VOUDFR,
518 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
519}
520
521struct sh_vou_geometry {
522 struct v4l2_rect output;
523 unsigned int in_width;
524 unsigned int in_height;
525 int scale_idx_h;
526 int scale_idx_v;
527};
528
529/*
530 * Find input geometry, that we can use to produce output, closest to the
531 * requested rectangle, using VOU scaling
532 */
533static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
534{
535 /* The compiler cannot know, that best and idx will indeed be set */
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300536 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300537 int i, idx = 0;
538
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300539 if (std & V4L2_STD_525_60)
540 img_height_max = 480;
541 else
542 img_height_max = 576;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300543
544 /* Image width must be a multiple of 4 */
545 v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300546 &geo->in_height, 0, img_height_max, 1, 0);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300547
548 /* Select scales to come as close as possible to the output image */
549 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
550 unsigned int err;
551 unsigned int found = geo->output.width * vou_scale_h_den[i] /
552 vou_scale_h_num[i];
553
554 if (found > VOU_MAX_IMAGE_WIDTH)
555 /* scales increase */
556 break;
557
558 err = abs(found - geo->in_width);
559 if (err < best_err) {
560 best_err = err;
561 idx = i;
562 best = found;
563 }
564 if (!err)
565 break;
566 }
567
568 geo->in_width = best;
569 geo->scale_idx_h = idx;
570
571 best_err = UINT_MAX;
572
573 /* This loop can be replaced with one division */
574 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
575 unsigned int err;
576 unsigned int found = geo->output.height * vou_scale_v_den[i] /
577 vou_scale_v_num[i];
578
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300579 if (found > img_height_max)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300580 /* scales increase */
581 break;
582
583 err = abs(found - geo->in_height);
584 if (err < best_err) {
585 best_err = err;
586 idx = i;
587 best = found;
588 }
589 if (!err)
590 break;
591 }
592
593 geo->in_height = best;
594 geo->scale_idx_v = idx;
595}
596
597/*
598 * Find output geometry, that we can produce, using VOU scaling, closest to
599 * the requested rectangle
600 */
601static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
602{
Guennadi Liakhovetskibc1ebd72012-11-26 14:32:48 -0300603 unsigned int best_err = UINT_MAX, best = geo->in_width,
604 width_max, height_max, img_height_max;
Mauro Carvalho Chehab5ac417e2015-06-05 08:07:19 -0300605 int i, idx_h = 0, idx_v = 0;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300606
607 if (std & V4L2_STD_525_60) {
608 width_max = 858;
609 height_max = 262 * 2;
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300610 img_height_max = 480;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300611 } else {
612 width_max = 864;
613 height_max = 312 * 2;
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300614 img_height_max = 576;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300615 }
616
617 /* Select scales to come as close as possible to the output image */
618 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
619 unsigned int err;
620 unsigned int found = geo->in_width * vou_scale_h_num[i] /
621 vou_scale_h_den[i];
622
623 if (found > VOU_MAX_IMAGE_WIDTH)
624 /* scales increase */
625 break;
626
627 err = abs(found - geo->output.width);
628 if (err < best_err) {
629 best_err = err;
Mauro Carvalho Chehab5ac417e2015-06-05 08:07:19 -0300630 idx_h = i;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300631 best = found;
632 }
633 if (!err)
634 break;
635 }
636
637 geo->output.width = best;
Mauro Carvalho Chehab5ac417e2015-06-05 08:07:19 -0300638 geo->scale_idx_h = idx_h;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300639 if (geo->output.left + best > width_max)
640 geo->output.left = width_max - best;
641
642 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
Mauro Carvalho Chehab5ac417e2015-06-05 08:07:19 -0300643 vou_scale_h_num[idx_h], vou_scale_h_den[idx_h], best);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300644
645 best_err = UINT_MAX;
646
647 /* This loop can be replaced with one division */
648 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
649 unsigned int err;
650 unsigned int found = geo->in_height * vou_scale_v_num[i] /
651 vou_scale_v_den[i];
652
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300653 if (found > img_height_max)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300654 /* scales increase */
655 break;
656
657 err = abs(found - geo->output.height);
658 if (err < best_err) {
659 best_err = err;
Mauro Carvalho Chehab5ac417e2015-06-05 08:07:19 -0300660 idx_v = i;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300661 best = found;
662 }
663 if (!err)
664 break;
665 }
666
667 geo->output.height = best;
Mauro Carvalho Chehab5ac417e2015-06-05 08:07:19 -0300668 geo->scale_idx_v = idx_v;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300669 if (geo->output.top + best > height_max)
670 geo->output.top = height_max - best;
671
672 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
Mauro Carvalho Chehab5ac417e2015-06-05 08:07:19 -0300673 vou_scale_v_num[idx_v], vou_scale_v_den[idx_v], best);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300674}
675
676static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
677 struct v4l2_format *fmt)
678{
Laurent Pinchartfd516252013-01-03 15:35:56 -0300679 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300680 struct v4l2_pix_format *pix = &fmt->fmt.pix;
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300681 unsigned int img_height_max;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300682 int pix_idx;
683 struct sh_vou_geometry geo;
Hans Verkuilebf984b2015-04-09 04:05:59 -0300684 struct v4l2_subdev_format format = {
685 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300686 /* Revisit: is this the correct code? */
Hans Verkuilebf984b2015-04-09 04:05:59 -0300687 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
688 .format.field = V4L2_FIELD_INTERLACED,
689 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300690 };
Hans Verkuilebf984b2015-04-09 04:05:59 -0300691 struct v4l2_mbus_framefmt *mbfmt = &format.format;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300692 int ret;
693
694 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
695 vou_dev->rect.width, vou_dev->rect.height,
696 pix->width, pix->height);
697
698 if (pix->field == V4L2_FIELD_ANY)
699 pix->field = V4L2_FIELD_NONE;
700
701 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
702 pix->field != V4L2_FIELD_NONE)
703 return -EINVAL;
704
705 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
706 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
707 break;
708
709 if (pix_idx == ARRAY_SIZE(vou_fmt))
710 return -EINVAL;
711
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300712 if (vou_dev->std & V4L2_STD_525_60)
713 img_height_max = 480;
714 else
715 img_height_max = 576;
716
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300717 /* Image width must be a multiple of 4 */
718 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300719 &pix->height, 0, img_height_max, 1, 0);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300720
721 geo.in_width = pix->width;
722 geo.in_height = pix->height;
723 geo.output = vou_dev->rect;
724
725 vou_adjust_output(&geo, vou_dev->std);
726
Hans Verkuilebf984b2015-04-09 04:05:59 -0300727 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);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300731 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
732 if (ret < 0)
733 return ret;
734
735 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
Hans Verkuilebf984b2015-04-09 04:05:59 -0300736 geo.output.width, geo.output.height, mbfmt->width, mbfmt->height);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300737
738 /* Sanity checks */
Hans Verkuilebf984b2015-04-09 04:05:59 -0300739 if ((unsigned)mbfmt->width > VOU_MAX_IMAGE_WIDTH ||
740 (unsigned)mbfmt->height > img_height_max ||
741 mbfmt->code != MEDIA_BUS_FMT_YUYV8_2X8)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300742 return -EIO;
743
Hans Verkuilebf984b2015-04-09 04:05:59 -0300744 if (mbfmt->width != geo.output.width ||
745 mbfmt->height != geo.output.height) {
746 geo.output.width = mbfmt->width;
747 geo.output.height = mbfmt->height;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300748
749 vou_adjust_input(&geo, vou_dev->std);
750 }
751
752 /* We tried to preserve output rectangle, but it could have changed */
753 vou_dev->rect = geo.output;
754 pix->width = geo.in_width;
755 pix->height = geo.in_height;
756
757 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
758 pix->width, pix->height);
759
760 vou_dev->pix_idx = pix_idx;
761
762 vou_dev->pix = *pix;
763
764 sh_vou_configure_geometry(vou_dev, pix_idx,
765 geo.scale_idx_h, geo.scale_idx_v);
766
767 return 0;
768}
769
770static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
771 struct v4l2_format *fmt)
772{
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300773 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300774 struct v4l2_pix_format *pix = &fmt->fmt.pix;
775 int i;
776
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300777 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300778
779 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
780 pix->field = V4L2_FIELD_NONE;
781
782 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
783 &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
784
Dan Carpenter47c32ec2013-08-23 06:54:44 -0300785 for (i = 0; i < ARRAY_SIZE(vou_fmt); i++)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300786 if (vou_fmt[i].pfmt == pix->pixelformat)
787 return 0;
788
789 pix->pixelformat = vou_fmt[0].pfmt;
790
791 return 0;
792}
793
794static int sh_vou_reqbufs(struct file *file, void *priv,
795 struct v4l2_requestbuffers *req)
796{
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300797 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300798 struct sh_vou_file *vou_file = priv;
799
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300800 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300801
802 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
803 return -EINVAL;
804
805 return videobuf_reqbufs(&vou_file->vbq, req);
806}
807
808static int sh_vou_querybuf(struct file *file, void *priv,
809 struct v4l2_buffer *b)
810{
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300811 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300812 struct sh_vou_file *vou_file = priv;
813
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300814 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300815
816 return videobuf_querybuf(&vou_file->vbq, b);
817}
818
819static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
820{
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300821 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300822 struct sh_vou_file *vou_file = priv;
823
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300824 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300825
826 return videobuf_qbuf(&vou_file->vbq, b);
827}
828
829static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
830{
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300831 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300832 struct sh_vou_file *vou_file = priv;
833
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300834 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300835
836 return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
837}
838
839static int sh_vou_streamon(struct file *file, void *priv,
840 enum v4l2_buf_type buftype)
841{
Laurent Pinchartfd516252013-01-03 15:35:56 -0300842 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300843 struct sh_vou_file *vou_file = priv;
844 int ret;
845
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300846 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300847
848 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
849 video, s_stream, 1);
850 if (ret < 0 && ret != -ENOIOCTLCMD)
851 return ret;
852
853 /* This calls our .buf_queue() (== sh_vou_buf_queue) */
854 return videobuf_streamon(&vou_file->vbq);
855}
856
857static int sh_vou_streamoff(struct file *file, void *priv,
858 enum v4l2_buf_type buftype)
859{
Laurent Pinchartfd516252013-01-03 15:35:56 -0300860 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300861 struct sh_vou_file *vou_file = priv;
862
Laurent Pinchartd899edd2013-01-03 15:35:57 -0300863 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300864
865 /*
866 * This calls buf_release from host driver's videobuf_queue_ops for all
867 * remaining buffers. When the last buffer is freed, stop streaming
868 */
869 videobuf_streamoff(&vou_file->vbq);
870 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
871
872 return 0;
873}
874
Hans Verkuil4de00d02015-06-07 05:57:59 -0300875static int sh_vou_enum_output(struct file *file, void *fh,
876 struct v4l2_output *a)
877{
878 struct sh_vou_device *vou_dev = video_drvdata(file);
879
880 if (a->index)
881 return -EINVAL;
882 strlcpy(a->name, "Video Out", sizeof(a->name));
883 a->type = V4L2_OUTPUT_TYPE_ANALOG;
884 a->std = vou_dev->vdev.tvnorms;
885 return 0;
886}
887
888int sh_vou_g_output(struct file *file, void *fh, unsigned int *i)
889{
890 *i = 0;
891 return 0;
892}
893
894int sh_vou_s_output(struct file *file, void *fh, unsigned int i)
895{
896 return i ? -EINVAL : 0;
897}
898
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300899static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
900{
901 switch (bus_fmt) {
902 default:
903 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
904 __func__, bus_fmt);
905 case SH_VOU_BUS_8BIT:
906 return 1;
907 case SH_VOU_BUS_16BIT:
908 return 0;
909 case SH_VOU_BUS_BT656:
910 return 3;
911 }
912}
913
Hans Verkuil314527a2013-03-15 06:10:40 -0300914static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300915{
Laurent Pinchartfd516252013-01-03 15:35:56 -0300916 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300917 int ret;
918
Hans Verkuil314527a2013-03-15 06:10:40 -0300919 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300920
Lad, Prabhakard079f992015-03-09 19:10:51 -0300921 if (std_id & ~vou_dev->vdev.tvnorms)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300922 return -EINVAL;
923
924 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300925 s_std_output, std_id);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300926 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
927 if (ret < 0 && ret != -ENOIOCTLCMD)
928 return ret;
929
Hans Verkuil314527a2013-03-15 06:10:40 -0300930 if (std_id & V4L2_STD_525_60)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300931 sh_vou_reg_ab_set(vou_dev, VOUCR,
932 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
933 else
934 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
935
Hans Verkuil314527a2013-03-15 06:10:40 -0300936 vou_dev->std = std_id;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300937
938 return 0;
939}
940
941static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
942{
Laurent Pinchartfd516252013-01-03 15:35:56 -0300943 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300944
945 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
946
947 *std = vou_dev->std;
948
949 return 0;
950}
951
952static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
953{
Laurent Pinchartfd516252013-01-03 15:35:56 -0300954 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300955
956 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
957
958 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
959 a->c = vou_dev->rect;
960
961 return 0;
962}
963
964/* Assume a dull encoder, do all the work ourselves. */
Hans Verkuil4f996592012-09-05 05:10:48 -0300965static int sh_vou_s_crop(struct file *file, void *fh, const struct v4l2_crop *a)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300966{
Guennadi Liakhovetski17803582012-09-28 07:28:58 -0300967 struct v4l2_crop a_writable = *a;
Laurent Pinchartfd516252013-01-03 15:35:56 -0300968 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetski17803582012-09-28 07:28:58 -0300969 struct v4l2_rect *rect = &a_writable.c;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300970 struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
971 struct v4l2_pix_format *pix = &vou_dev->pix;
972 struct sh_vou_geometry geo;
Hans Verkuilebf984b2015-04-09 04:05:59 -0300973 struct v4l2_subdev_format format = {
974 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300975 /* Revisit: is this the correct code? */
Hans Verkuilebf984b2015-04-09 04:05:59 -0300976 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
977 .format.field = V4L2_FIELD_INTERLACED,
978 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300979 };
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300980 unsigned int img_height_max;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300981 int ret;
982
983 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
984 rect->width, rect->height, rect->left, rect->top);
985
986 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
987 return -EINVAL;
988
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300989 if (vou_dev->std & V4L2_STD_525_60)
990 img_height_max = 480;
991 else
992 img_height_max = 576;
993
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300994 v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -0300995 &rect->height, 0, img_height_max, 1, 0);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -0300996
997 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
998 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
999
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -03001000 if (rect->height + rect->top > img_height_max)
1001 rect->top = img_height_max - rect->height;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001002
1003 geo.output = *rect;
1004 geo.in_width = pix->width;
1005 geo.in_height = pix->height;
1006
1007 /* Configure the encoder one-to-one, position at 0, ignore errors */
1008 sd_crop.c.width = geo.output.width;
1009 sd_crop.c.height = geo.output.height;
1010 /*
1011 * We first issue a S_CROP, so that the subsequent S_FMT delivers the
1012 * final encoder configuration.
1013 */
1014 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
1015 s_crop, &sd_crop);
Hans Verkuilebf984b2015-04-09 04:05:59 -03001016 format.format.width = geo.output.width;
1017 format.format.height = geo.output.height;
1018 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
1019 set_fmt, NULL, &format);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001020 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
1021 if (ret < 0)
1022 return ret;
1023
1024 /* Sanity checks */
Hans Verkuilebf984b2015-04-09 04:05:59 -03001025 if ((unsigned)format.format.width > VOU_MAX_IMAGE_WIDTH ||
1026 (unsigned)format.format.height > img_height_max ||
1027 format.format.code != MEDIA_BUS_FMT_YUYV8_2X8)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001028 return -EIO;
1029
Hans Verkuilebf984b2015-04-09 04:05:59 -03001030 geo.output.width = format.format.width;
1031 geo.output.height = format.format.height;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001032
1033 /*
1034 * No down-scaling. According to the API, current call has precedence:
1035 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1036 */
1037 vou_adjust_input(&geo, vou_dev->std);
1038
1039 /* We tried to preserve output rectangle, but it could have changed */
1040 vou_dev->rect = geo.output;
1041 pix->width = geo.in_width;
1042 pix->height = geo.in_height;
1043
1044 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1045 geo.scale_idx_h, geo.scale_idx_v);
1046
1047 return 0;
1048}
1049
1050/*
1051 * Total field: NTSC 858 x 2 * 262/263, PAL 864 x 2 * 312/313, default rectangle
1052 * is the initial register values, height takes the interlaced format into
1053 * account. The actual image can only go up to 720 x 2 * 240, So, VOUVPR can
1054 * actually only meaningfully contain values <= 720 and <= 240 respectively, and
1055 * not <= 864 and <= 312.
1056 */
1057static int sh_vou_cropcap(struct file *file, void *priv,
1058 struct v4l2_cropcap *a)
1059{
Laurent Pinchartd899edd2013-01-03 15:35:57 -03001060 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001061
Laurent Pinchartd899edd2013-01-03 15:35:57 -03001062 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001063
1064 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1065 a->bounds.left = 0;
1066 a->bounds.top = 0;
1067 a->bounds.width = VOU_MAX_IMAGE_WIDTH;
1068 a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
1069 /* Default = max, set VOUDPR = 0, which is not hardware default */
1070 a->defrect.left = 0;
1071 a->defrect.top = 0;
1072 a->defrect.width = VOU_MAX_IMAGE_WIDTH;
1073 a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
1074 a->pixelaspect.numerator = 1;
1075 a->pixelaspect.denominator = 1;
1076
1077 return 0;
1078}
1079
1080static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1081{
1082 struct sh_vou_device *vou_dev = dev_id;
1083 static unsigned long j;
1084 struct videobuf_buffer *vb;
1085 static int cnt;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001086 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1087 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1088
1089 if (!(irq_status & 0x300)) {
1090 if (printk_timed_ratelimit(&j, 500))
1091 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1092 irq_status);
1093 return IRQ_NONE;
1094 }
1095
1096 spin_lock(&vou_dev->lock);
1097 if (!vou_dev->active || list_empty(&vou_dev->queue)) {
1098 if (printk_timed_ratelimit(&j, 500))
1099 dev_warn(vou_dev->v4l2_dev.dev,
1100 "IRQ without active buffer: %x!\n", irq_status);
1101 /* Just ack: buf_release will disable further interrupts */
1102 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1103 spin_unlock(&vou_dev->lock);
1104 return IRQ_HANDLED;
1105 }
1106
1107 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1108 dev_dbg(vou_dev->v4l2_dev.dev,
1109 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1110 irq_status, masked, vou_status, cnt);
1111
1112 cnt++;
Mauro Carvalho Chehaba622cc52014-01-13 11:30:26 -02001113 /* side = vou_status & 0x10000; */
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001114
1115 /* Clear only set interrupts */
1116 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1117
1118 vb = vou_dev->active;
1119 list_del(&vb->queue);
1120
1121 vb->state = VIDEOBUF_DONE;
Sakari Ailus8e6057b2012-09-15 15:14:42 -03001122 v4l2_get_timestamp(&vb->ts);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001123 vb->field_count++;
1124 wake_up(&vb->done);
1125
1126 if (list_empty(&vou_dev->queue)) {
1127 /* Stop VOU */
1128 dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
1129 __func__, cnt);
1130 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
1131 vou_dev->active = NULL;
1132 vou_dev->status = SH_VOU_INITIALISING;
1133 /* Disable End-of-Frame (VSYNC) interrupts */
1134 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
1135 spin_unlock(&vou_dev->lock);
1136 return IRQ_HANDLED;
1137 }
1138
1139 vou_dev->active = list_entry(vou_dev->queue.next,
1140 struct videobuf_buffer, queue);
1141
1142 if (vou_dev->active->queue.next != &vou_dev->queue) {
1143 struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
1144 struct videobuf_buffer, queue);
1145 sh_vou_schedule_next(vou_dev, new);
1146 }
1147
1148 spin_unlock(&vou_dev->lock);
1149
1150 return IRQ_HANDLED;
1151}
1152
1153static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1154{
1155 struct sh_vou_pdata *pdata = vou_dev->pdata;
1156 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1157 int i = 100;
1158
1159 /* Disable all IRQs */
1160 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1161
1162 /* Reset VOU interfaces - registers unaffected */
1163 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1164 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1165 udelay(1);
1166
1167 if (!i)
1168 return -ETIMEDOUT;
1169
1170 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1171
1172 if (pdata->flags & SH_VOU_PCLK_FALLING)
1173 voucr |= 1 << 28;
1174 if (pdata->flags & SH_VOU_HSYNC_LOW)
1175 voucr |= 1 << 27;
1176 if (pdata->flags & SH_VOU_VSYNC_LOW)
1177 voucr |= 1 << 26;
1178 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1179
1180 /* Manual register side switching at first */
1181 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1182 /* Default - fixed HSYNC length, can be made configurable is required */
1183 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1184
1185 return 0;
1186}
1187
1188/* File operations */
1189static int sh_vou_open(struct file *file)
1190{
Laurent Pinchartfd516252013-01-03 15:35:56 -03001191 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001192 struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
1193 GFP_KERNEL);
1194
1195 if (!vou_file)
1196 return -ENOMEM;
1197
1198 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1199
Hans Verkuilc5f98082015-06-07 05:57:58 -03001200 v4l2_fh_init(&vou_file->fh, &vou_dev->vdev);
Alexey Khoroshilov2160abb22015-02-13 19:39:03 -03001201 if (mutex_lock_interruptible(&vou_dev->fop_lock)) {
1202 kfree(vou_file);
Hans Verkuilf135a8a2012-06-24 06:33:26 -03001203 return -ERESTARTSYS;
Alexey Khoroshilov2160abb22015-02-13 19:39:03 -03001204 }
Hans Verkuilc5f98082015-06-07 05:57:58 -03001205 v4l2_fh_add(&vou_file->fh);
1206 if (v4l2_fh_is_singular(&vou_file->fh)) {
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001207 int ret;
Hans Verkuilc5f98082015-06-07 05:57:58 -03001208
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001209 /* First open */
1210 vou_dev->status = SH_VOU_INITIALISING;
Laurent Pinchartfd516252013-01-03 15:35:56 -03001211 pm_runtime_get_sync(vou_dev->v4l2_dev.dev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001212 ret = sh_vou_hw_init(vou_dev);
1213 if (ret < 0) {
Laurent Pinchartfd516252013-01-03 15:35:56 -03001214 pm_runtime_put(vou_dev->v4l2_dev.dev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001215 vou_dev->status = SH_VOU_IDLE;
Hans Verkuilc5f98082015-06-07 05:57:58 -03001216 v4l2_fh_del(&vou_file->fh);
1217 v4l2_fh_exit(&vou_file->fh);
Hans Verkuilf135a8a2012-06-24 06:33:26 -03001218 mutex_unlock(&vou_dev->fop_lock);
Alexey Khoroshilov2160abb22015-02-13 19:39:03 -03001219 kfree(vou_file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001220 return ret;
1221 }
1222 }
1223
1224 videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
1225 vou_dev->v4l2_dev.dev, &vou_dev->lock,
1226 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1227 V4L2_FIELD_NONE,
Laurent Pinchartfd516252013-01-03 15:35:56 -03001228 sizeof(struct videobuf_buffer),
Lad, Prabhakard079f992015-03-09 19:10:51 -03001229 &vou_dev->vdev, &vou_dev->fop_lock);
Hans Verkuilf135a8a2012-06-24 06:33:26 -03001230 mutex_unlock(&vou_dev->fop_lock);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001231
Alexey Khoroshilov2160abb22015-02-13 19:39:03 -03001232 file->private_data = vou_file;
1233
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001234 return 0;
1235}
1236
1237static int sh_vou_release(struct file *file)
1238{
Laurent Pinchartfd516252013-01-03 15:35:56 -03001239 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001240 struct sh_vou_file *vou_file = file->private_data;
1241
Laurent Pinchartd899edd2013-01-03 15:35:57 -03001242 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001243
Hans Verkuilc5f98082015-06-07 05:57:58 -03001244 mutex_lock(&vou_dev->fop_lock);
1245 if (v4l2_fh_is_singular(&vou_file->fh)) {
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001246 /* Last close */
1247 vou_dev->status = SH_VOU_IDLE;
1248 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
Laurent Pinchartfd516252013-01-03 15:35:56 -03001249 pm_runtime_put(vou_dev->v4l2_dev.dev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001250 }
Hans Verkuilc5f98082015-06-07 05:57:58 -03001251 v4l2_fh_del(&vou_file->fh);
1252 v4l2_fh_exit(&vou_file->fh);
1253 mutex_unlock(&vou_dev->fop_lock);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001254
1255 file->private_data = NULL;
1256 kfree(vou_file);
1257
1258 return 0;
1259}
1260
1261static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
1262{
Laurent Pinchartfd516252013-01-03 15:35:56 -03001263 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001264 struct sh_vou_file *vou_file = file->private_data;
Hans Verkuilf135a8a2012-06-24 06:33:26 -03001265 int ret;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001266
Laurent Pinchartd899edd2013-01-03 15:35:57 -03001267 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001268
Hans Verkuilf135a8a2012-06-24 06:33:26 -03001269 if (mutex_lock_interruptible(&vou_dev->fop_lock))
1270 return -ERESTARTSYS;
1271 ret = videobuf_mmap_mapper(&vou_file->vbq, vma);
1272 mutex_unlock(&vou_dev->fop_lock);
1273 return ret;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001274}
1275
1276static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
1277{
Laurent Pinchartfd516252013-01-03 15:35:56 -03001278 struct sh_vou_device *vou_dev = video_drvdata(file);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001279 struct sh_vou_file *vou_file = file->private_data;
Hans Verkuilf135a8a2012-06-24 06:33:26 -03001280 unsigned int res;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001281
Laurent Pinchartd899edd2013-01-03 15:35:57 -03001282 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001283
Hans Verkuilf135a8a2012-06-24 06:33:26 -03001284 mutex_lock(&vou_dev->fop_lock);
1285 res = videobuf_poll_stream(file, &vou_file->vbq, wait);
1286 mutex_unlock(&vou_dev->fop_lock);
1287 return res;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001288}
1289
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001290/* sh_vou display ioctl operations */
1291static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1292 .vidioc_querycap = sh_vou_querycap,
1293 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1294 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1295 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1296 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1297 .vidioc_reqbufs = sh_vou_reqbufs,
1298 .vidioc_querybuf = sh_vou_querybuf,
1299 .vidioc_qbuf = sh_vou_qbuf,
1300 .vidioc_dqbuf = sh_vou_dqbuf,
1301 .vidioc_streamon = sh_vou_streamon,
1302 .vidioc_streamoff = sh_vou_streamoff,
Hans Verkuil4de00d02015-06-07 05:57:59 -03001303 .vidioc_g_output = sh_vou_g_output,
1304 .vidioc_s_output = sh_vou_s_output,
1305 .vidioc_enum_output = sh_vou_enum_output,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001306 .vidioc_s_std = sh_vou_s_std,
1307 .vidioc_g_std = sh_vou_g_std,
1308 .vidioc_cropcap = sh_vou_cropcap,
1309 .vidioc_g_crop = sh_vou_g_crop,
1310 .vidioc_s_crop = sh_vou_s_crop,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001311};
1312
1313static const struct v4l2_file_operations sh_vou_fops = {
1314 .owner = THIS_MODULE,
1315 .open = sh_vou_open,
1316 .release = sh_vou_release,
Hans Verkuil69756692010-11-16 18:12:16 -03001317 .unlocked_ioctl = video_ioctl2,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001318 .mmap = sh_vou_mmap,
1319 .poll = sh_vou_poll,
1320};
1321
1322static const struct video_device sh_vou_video_template = {
1323 .name = "sh_vou",
1324 .fops = &sh_vou_fops,
1325 .ioctl_ops = &sh_vou_ioctl_ops,
1326 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
Hans Verkuil954f3402012-09-05 06:05:50 -03001327 .vfl_dir = VFL_DIR_TX,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001328};
1329
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001330static int sh_vou_probe(struct platform_device *pdev)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001331{
1332 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1333 struct v4l2_rect *rect;
1334 struct v4l2_pix_format *pix;
1335 struct i2c_adapter *i2c_adap;
1336 struct video_device *vdev;
1337 struct sh_vou_device *vou_dev;
Hans Verkuil46902712015-06-07 05:57:56 -03001338 struct resource *reg_res;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001339 struct v4l2_subdev *subdev;
1340 int irq, ret;
1341
1342 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1343 irq = platform_get_irq(pdev, 0);
1344
1345 if (!vou_pdata || !reg_res || irq <= 0) {
1346 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1347 return -ENODEV;
1348 }
1349
Hans Verkuil46902712015-06-07 05:57:56 -03001350 vou_dev = devm_kzalloc(&pdev->dev, sizeof(*vou_dev), GFP_KERNEL);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001351 if (!vou_dev)
1352 return -ENOMEM;
1353
1354 INIT_LIST_HEAD(&vou_dev->queue);
1355 spin_lock_init(&vou_dev->lock);
Hans Verkuil69756692010-11-16 18:12:16 -03001356 mutex_init(&vou_dev->fop_lock);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001357 vou_dev->pdata = vou_pdata;
1358 vou_dev->status = SH_VOU_IDLE;
1359
1360 rect = &vou_dev->rect;
1361 pix = &vou_dev->pix;
1362
1363 /* Fill in defaults */
Hans Verkuil5d408102013-06-03 05:36:40 -03001364 vou_dev->std = V4L2_STD_NTSC_M;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001365 rect->left = 0;
1366 rect->top = 0;
1367 rect->width = VOU_MAX_IMAGE_WIDTH;
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -03001368 rect->height = 480;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001369 pix->width = VOU_MAX_IMAGE_WIDTH;
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -03001370 pix->height = 480;
Hans Verkuil22df2e72015-06-07 05:58:00 -03001371 pix->pixelformat = V4L2_PIX_FMT_NV16;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001372 pix->field = V4L2_FIELD_NONE;
1373 pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
Guennadi Liakhovetski765fe172010-07-29 04:11:42 -03001374 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001375 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1376
Hans Verkuil46902712015-06-07 05:57:56 -03001377 vou_dev->base = devm_ioremap_resource(&pdev->dev, reg_res);
1378 if (IS_ERR(vou_dev->base))
1379 return PTR_ERR(vou_dev->base);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001380
Hans Verkuil46902712015-06-07 05:57:56 -03001381 ret = devm_request_irq(&pdev->dev, irq, sh_vou_isr, 0, "vou", vou_dev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001382 if (ret < 0)
Hans Verkuil46902712015-06-07 05:57:56 -03001383 return ret;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001384
1385 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1386 if (ret < 0) {
1387 dev_err(&pdev->dev, "Error registering v4l2 device\n");
Hans Verkuil46902712015-06-07 05:57:56 -03001388 return ret;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001389 }
1390
Lad, Prabhakard079f992015-03-09 19:10:51 -03001391 vdev = &vou_dev->vdev;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001392 *vdev = sh_vou_video_template;
1393 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1394 vdev->tvnorms |= V4L2_STD_PAL;
1395 vdev->v4l2_dev = &vou_dev->v4l2_dev;
Lad, Prabhakard079f992015-03-09 19:10:51 -03001396 vdev->release = video_device_release_empty;
Hans Verkuil69756692010-11-16 18:12:16 -03001397 vdev->lock = &vou_dev->fop_lock;
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001398
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001399 video_set_drvdata(vdev, vou_dev);
1400
1401 pm_runtime_enable(&pdev->dev);
1402 pm_runtime_resume(&pdev->dev);
1403
1404 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1405 if (!i2c_adap) {
1406 ret = -ENODEV;
1407 goto ei2cgadap;
1408 }
1409
1410 ret = sh_vou_hw_init(vou_dev);
1411 if (ret < 0)
1412 goto ereset;
1413
1414 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001415 vou_pdata->board_info, NULL);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001416 if (!subdev) {
1417 ret = -ENOMEM;
1418 goto ei2cnd;
1419 }
1420
1421 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1422 if (ret < 0)
1423 goto evregdev;
1424
1425 return 0;
1426
1427evregdev:
1428ei2cnd:
1429ereset:
1430 i2c_put_adapter(i2c_adap);
1431ei2cgadap:
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001432 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001433 v4l2_device_unregister(&vou_dev->v4l2_dev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001434 return ret;
1435}
1436
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001437static int sh_vou_remove(struct platform_device *pdev)
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001438{
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001439 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1440 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1441 struct sh_vou_device, v4l2_dev);
1442 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1443 struct v4l2_subdev, list);
1444 struct i2c_client *client = v4l2_get_subdevdata(sd);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001445
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001446 pm_runtime_disable(&pdev->dev);
Lad, Prabhakard079f992015-03-09 19:10:51 -03001447 video_unregister_device(&vou_dev->vdev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001448 i2c_put_adapter(client->adapter);
1449 v4l2_device_unregister(&vou_dev->v4l2_dev);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001450 return 0;
1451}
1452
1453static struct platform_driver __refdata sh_vou = {
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001454 .remove = sh_vou_remove,
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001455 .driver = {
1456 .name = "sh-vou",
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001457 },
1458};
1459
Sachin Kamatb3fd87b2013-03-05 01:53:37 -03001460module_platform_driver_probe(sh_vou, sh_vou_probe);
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001461
1462MODULE_DESCRIPTION("SuperH VOU driver");
1463MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1464MODULE_LICENSE("GPL v2");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001465MODULE_VERSION("0.1.0");
Guennadi Liakhovetskia81fb9b2010-03-17 11:21:13 -03001466MODULE_ALIAS("platform:sh-vou");