1
2
3
4
5
6
7
8
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>
21#include <linux/slab.h>
22#include <linux/videodev2.h>
23#include <linux/module.h>
24
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
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
61#define VOU_MAX_IMAGE_HEIGHT 576
62
63struct sh_vou_device {
64 struct v4l2_device v4l2_dev;
65 struct video_device *vdev;
66 atomic_t use_count;
67 struct sh_vou_pdata *pdata;
68 spinlock_t lock;
69 void __iomem *base;
70
71 struct v4l2_pix_format pix;
72 struct v4l2_rect rect;
73 struct list_head queue;
74 v4l2_std_id std;
75 int pix_idx;
76 struct videobuf_buffer *active;
77 enum sh_vou_status status;
78 struct mutex fop_lock;
79};
80
81struct sh_vou_file {
82 struct videobuf_queue vbq;
83};
84
85
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
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) {
210 default:
211 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
235 videobuf_waiton(vq, vb, 0, 0);
236 videobuf_dma_contig_free(vq, vb);
237 vb->state = VIDEOBUF_NEEDS_INIT;
238}
239
240
241static 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
254 if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
255 *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
256
257 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): count=%d, size=%d\n", __func__,
258 *count, *size);
259
260 return 0;
261}
262
263
264static 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
274 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
275
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
289 dev_warn(vq->dev, "User buffer too small: [%zu] @ %lx\n",
290 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
304 dev_dbg(vou_dev->v4l2_dev.dev,
305 "%s(): fmt #%d, %u bytes per line, phys %pad, type %d, state %d\n",
306 __func__, vou_dev->pix_idx, bytes_per_line,
307 ({ dma_addr_t addr = videobuf_to_dma_contig(vb); &addr; }),
308 vb->memory, vb->state);
309
310 return 0;
311}
312
313
314static 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
320 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
321
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
330 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
331 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n",
332 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
333 sh_vou_schedule_next(vou_dev, vb);
334
335 } else if (vou_dev->active->queue.next == &vb->queue) {
336
337 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
338 sh_vou_stream_start(vou_dev, vb);
339
340
341 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
342 dev_dbg(vou_dev->v4l2_dev.dev, "%s: second buffer status 0x%x\n",
343 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
344
345
346 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
347
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
361 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
362
363 spin_lock_irqsave(&vou_dev->lock, flags);
364
365 if (vou_dev->active == vb) {
366
367 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
368
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
391static int sh_vou_querycap(struct file *file, void *priv,
392 struct v4l2_capability *cap)
393{
394 struct sh_vou_device *vou_dev = video_drvdata(file);
395
396 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
397
398 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
399 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
400 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
401 return 0;
402}
403
404
405static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
406 struct v4l2_fmtdesc *fmt)
407{
408 struct sh_vou_device *vou_dev = video_drvdata(file);
409
410 if (fmt->index >= ARRAY_SIZE(vou_fmt))
411 return -EINVAL;
412
413 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
414
415 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
416 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
417 sizeof(fmt->description));
418 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
419
420 return 0;
421}
422
423static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
424 struct v4l2_format *fmt)
425{
426 struct sh_vou_device *vou_dev = video_drvdata(file);
427
428 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
429
430 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
431 fmt->fmt.pix = vou_dev->pix;
432
433 return 0;
434}
435
436static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
437static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
438static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
439static const unsigned char vou_scale_v_num[] = {1, 2, 4};
440static const unsigned char vou_scale_v_den[] = {1, 1, 1};
441static const unsigned char vou_scale_v_fld[] = {0, 1};
442
443static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
444 int pix_idx, int w_idx, int h_idx)
445{
446 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
447 unsigned int black_left, black_top, width_max,
448 frame_in_height, frame_out_height, frame_out_top;
449 struct v4l2_rect *rect = &vou_dev->rect;
450 struct v4l2_pix_format *pix = &vou_dev->pix;
451 u32 vouvcr = 0, dsr_h, dsr_v;
452
453 if (vou_dev->std & V4L2_STD_525_60) {
454 width_max = 858;
455
456 } else {
457 width_max = 864;
458
459 }
460
461 frame_in_height = pix->height / 2;
462 frame_out_height = rect->height / 2;
463 frame_out_top = rect->top / 2;
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
485 black_top = 20;
486
487 dsr_h = rect->width + rect->left;
488 dsr_v = frame_out_height + frame_out_top;
489
490 dev_dbg(vou_dev->v4l2_dev.dev,
491 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
492 pix->width, frame_in_height, black_left, black_top,
493 rect->left, frame_out_top, dsr_h, dsr_v);
494
495
496 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
497 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
498 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
499 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
500
501
502
503
504
505
506 if (w_idx)
507 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
508 if (h_idx)
509 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
510
511 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
512
513
514 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
515 sh_vou_reg_ab_write(vou_dev, VOUDFR,
516 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
517}
518
519struct sh_vou_geometry {
520 struct v4l2_rect output;
521 unsigned int in_width;
522 unsigned int in_height;
523 int scale_idx_h;
524 int scale_idx_v;
525};
526
527
528
529
530
531static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
532{
533
534 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
535 int i, idx = 0;
536
537 if (std & V4L2_STD_525_60)
538 img_height_max = 480;
539 else
540 img_height_max = 576;
541
542
543 v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
544 &geo->in_height, 0, img_height_max, 1, 0);
545
546
547 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
548 unsigned int err;
549 unsigned int found = geo->output.width * vou_scale_h_den[i] /
550 vou_scale_h_num[i];
551
552 if (found > VOU_MAX_IMAGE_WIDTH)
553
554 break;
555
556 err = abs(found - geo->in_width);
557 if (err < best_err) {
558 best_err = err;
559 idx = i;
560 best = found;
561 }
562 if (!err)
563 break;
564 }
565
566 geo->in_width = best;
567 geo->scale_idx_h = idx;
568
569 best_err = UINT_MAX;
570
571
572 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
573 unsigned int err;
574 unsigned int found = geo->output.height * vou_scale_v_den[i] /
575 vou_scale_v_num[i];
576
577 if (found > img_height_max)
578
579 break;
580
581 err = abs(found - geo->in_height);
582 if (err < best_err) {
583 best_err = err;
584 idx = i;
585 best = found;
586 }
587 if (!err)
588 break;
589 }
590
591 geo->in_height = best;
592 geo->scale_idx_v = idx;
593}
594
595
596
597
598
599static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
600{
601 unsigned int best_err = UINT_MAX, best = geo->in_width,
602 width_max, height_max, img_height_max;
603 int i, idx = 0;
604
605 if (std & V4L2_STD_525_60) {
606 width_max = 858;
607 height_max = 262 * 2;
608 img_height_max = 480;
609 } else {
610 width_max = 864;
611 height_max = 312 * 2;
612 img_height_max = 576;
613 }
614
615
616 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
617 unsigned int err;
618 unsigned int found = geo->in_width * vou_scale_h_num[i] /
619 vou_scale_h_den[i];
620
621 if (found > VOU_MAX_IMAGE_WIDTH)
622
623 break;
624
625 err = abs(found - geo->output.width);
626 if (err < best_err) {
627 best_err = err;
628 idx = i;
629 best = found;
630 }
631 if (!err)
632 break;
633 }
634
635 geo->output.width = best;
636 geo->scale_idx_h = idx;
637 if (geo->output.left + best > width_max)
638 geo->output.left = width_max - best;
639
640 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
641 vou_scale_h_num[idx], vou_scale_h_den[idx], best);
642
643 best_err = UINT_MAX;
644
645
646 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
647 unsigned int err;
648 unsigned int found = geo->in_height * vou_scale_v_num[i] /
649 vou_scale_v_den[i];
650
651 if (found > img_height_max)
652
653 break;
654
655 err = abs(found - geo->output.height);
656 if (err < best_err) {
657 best_err = err;
658 idx = i;
659 best = found;
660 }
661 if (!err)
662 break;
663 }
664
665 geo->output.height = best;
666 geo->scale_idx_v = idx;
667 if (geo->output.top + best > height_max)
668 geo->output.top = height_max - best;
669
670 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
671 vou_scale_v_num[idx], vou_scale_v_den[idx], best);
672}
673
674static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
675 struct v4l2_format *fmt)
676{
677 struct sh_vou_device *vou_dev = video_drvdata(file);
678 struct v4l2_pix_format *pix = &fmt->fmt.pix;
679 unsigned int img_height_max;
680 int pix_idx;
681 struct sh_vou_geometry geo;
682 struct v4l2_mbus_framefmt mbfmt = {
683
684 .code = MEDIA_BUS_FMT_YUYV8_2X8,
685 .field = V4L2_FIELD_INTERLACED,
686 .colorspace = V4L2_COLORSPACE_SMPTE170M,
687 };
688 int ret;
689
690 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
691 vou_dev->rect.width, vou_dev->rect.height,
692 pix->width, pix->height);
693
694 if (pix->field == V4L2_FIELD_ANY)
695 pix->field = V4L2_FIELD_NONE;
696
697 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
698 pix->field != V4L2_FIELD_NONE)
699 return -EINVAL;
700
701 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
702 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
703 break;
704
705 if (pix_idx == ARRAY_SIZE(vou_fmt))
706 return -EINVAL;
707
708 if (vou_dev->std & V4L2_STD_525_60)
709 img_height_max = 480;
710 else
711 img_height_max = 576;
712
713
714 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
715 &pix->height, 0, img_height_max, 1, 0);
716
717 geo.in_width = pix->width;
718 geo.in_height = pix->height;
719 geo.output = vou_dev->rect;
720
721 vou_adjust_output(&geo, vou_dev->std);
722
723 mbfmt.width = geo.output.width;
724 mbfmt.height = geo.output.height;
725 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
726 s_mbus_fmt, &mbfmt);
727
728 if (ret < 0)
729 return ret;
730
731 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
732 geo.output.width, geo.output.height, mbfmt.width, mbfmt.height);
733
734
735 if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
736 (unsigned)mbfmt.height > img_height_max ||
737 mbfmt.code != MEDIA_BUS_FMT_YUYV8_2X8)
738 return -EIO;
739
740 if (mbfmt.width != geo.output.width ||
741 mbfmt.height != geo.output.height) {
742 geo.output.width = mbfmt.width;
743 geo.output.height = mbfmt.height;
744
745 vou_adjust_input(&geo, vou_dev->std);
746 }
747
748
749 vou_dev->rect = geo.output;
750 pix->width = geo.in_width;
751 pix->height = geo.in_height;
752
753 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
754 pix->width, pix->height);
755
756 vou_dev->pix_idx = pix_idx;
757
758 vou_dev->pix = *pix;
759
760 sh_vou_configure_geometry(vou_dev, pix_idx,
761 geo.scale_idx_h, geo.scale_idx_v);
762
763 return 0;
764}
765
766static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
767 struct v4l2_format *fmt)
768{
769 struct sh_vou_device *vou_dev = video_drvdata(file);
770 struct v4l2_pix_format *pix = &fmt->fmt.pix;
771 int i;
772
773 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
774
775 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
776 pix->field = V4L2_FIELD_NONE;
777
778 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
779 &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
780
781 for (i = 0; i < ARRAY_SIZE(vou_fmt); i++)
782 if (vou_fmt[i].pfmt == pix->pixelformat)
783 return 0;
784
785 pix->pixelformat = vou_fmt[0].pfmt;
786
787 return 0;
788}
789
790static int sh_vou_reqbufs(struct file *file, void *priv,
791 struct v4l2_requestbuffers *req)
792{
793 struct sh_vou_device *vou_dev = video_drvdata(file);
794 struct sh_vou_file *vou_file = priv;
795
796 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
797
798 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
799 return -EINVAL;
800
801 return videobuf_reqbufs(&vou_file->vbq, req);
802}
803
804static int sh_vou_querybuf(struct file *file, void *priv,
805 struct v4l2_buffer *b)
806{
807 struct sh_vou_device *vou_dev = video_drvdata(file);
808 struct sh_vou_file *vou_file = priv;
809
810 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
811
812 return videobuf_querybuf(&vou_file->vbq, b);
813}
814
815static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
816{
817 struct sh_vou_device *vou_dev = video_drvdata(file);
818 struct sh_vou_file *vou_file = priv;
819
820 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
821
822 return videobuf_qbuf(&vou_file->vbq, b);
823}
824
825static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
826{
827 struct sh_vou_device *vou_dev = video_drvdata(file);
828 struct sh_vou_file *vou_file = priv;
829
830 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
831
832 return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
833}
834
835static int sh_vou_streamon(struct file *file, void *priv,
836 enum v4l2_buf_type buftype)
837{
838 struct sh_vou_device *vou_dev = video_drvdata(file);
839 struct sh_vou_file *vou_file = priv;
840 int ret;
841
842 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
843
844 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
845 video, s_stream, 1);
846 if (ret < 0 && ret != -ENOIOCTLCMD)
847 return ret;
848
849
850 return videobuf_streamon(&vou_file->vbq);
851}
852
853static int sh_vou_streamoff(struct file *file, void *priv,
854 enum v4l2_buf_type buftype)
855{
856 struct sh_vou_device *vou_dev = video_drvdata(file);
857 struct sh_vou_file *vou_file = priv;
858
859 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
860
861
862
863
864
865 videobuf_streamoff(&vou_file->vbq);
866 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
867
868 return 0;
869}
870
871static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
872{
873 switch (bus_fmt) {
874 default:
875 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
876 __func__, bus_fmt);
877 case SH_VOU_BUS_8BIT:
878 return 1;
879 case SH_VOU_BUS_16BIT:
880 return 0;
881 case SH_VOU_BUS_BT656:
882 return 3;
883 }
884}
885
886static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
887{
888 struct sh_vou_device *vou_dev = video_drvdata(file);
889 int ret;
890
891 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
892
893 if (std_id & ~vou_dev->vdev->tvnorms)
894 return -EINVAL;
895
896 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
897 s_std_output, std_id);
898
899 if (ret < 0 && ret != -ENOIOCTLCMD)
900 return ret;
901
902 if (std_id & V4L2_STD_525_60)
903 sh_vou_reg_ab_set(vou_dev, VOUCR,
904 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
905 else
906 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
907
908 vou_dev->std = std_id;
909
910 return 0;
911}
912
913static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
914{
915 struct sh_vou_device *vou_dev = video_drvdata(file);
916
917 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
918
919 *std = vou_dev->std;
920
921 return 0;
922}
923
924static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
925{
926 struct sh_vou_device *vou_dev = video_drvdata(file);
927
928 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
929
930 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
931 a->c = vou_dev->rect;
932
933 return 0;
934}
935
936
937static int sh_vou_s_crop(struct file *file, void *fh, const struct v4l2_crop *a)
938{
939 struct v4l2_crop a_writable = *a;
940 struct sh_vou_device *vou_dev = video_drvdata(file);
941 struct v4l2_rect *rect = &a_writable.c;
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;
945 struct v4l2_mbus_framefmt mbfmt = {
946
947 .code = MEDIA_BUS_FMT_YUYV8_2X8,
948 .field = V4L2_FIELD_INTERLACED,
949 .colorspace = V4L2_COLORSPACE_SMPTE170M,
950 };
951 unsigned int img_height_max;
952 int ret;
953
954 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
955 rect->width, rect->height, rect->left, rect->top);
956
957 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
958 return -EINVAL;
959
960 if (vou_dev->std & V4L2_STD_525_60)
961 img_height_max = 480;
962 else
963 img_height_max = 576;
964
965 v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
966 &rect->height, 0, img_height_max, 1, 0);
967
968 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
969 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
970
971 if (rect->height + rect->top > img_height_max)
972 rect->top = img_height_max - rect->height;
973
974 geo.output = *rect;
975 geo.in_width = pix->width;
976 geo.in_height = pix->height;
977
978
979 sd_crop.c.width = geo.output.width;
980 sd_crop.c.height = geo.output.height;
981
982
983
984
985 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
986 s_crop, &sd_crop);
987 mbfmt.width = geo.output.width;
988 mbfmt.height = geo.output.height;
989 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
990 s_mbus_fmt, &mbfmt);
991
992 if (ret < 0)
993 return ret;
994
995
996 if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
997 (unsigned)mbfmt.height > img_height_max ||
998 mbfmt.code != MEDIA_BUS_FMT_YUYV8_2X8)
999 return -EIO;
1000
1001 geo.output.width = mbfmt.width;
1002 geo.output.height = mbfmt.height;
1003
1004
1005
1006
1007
1008 vou_adjust_input(&geo, vou_dev->std);
1009
1010
1011 vou_dev->rect = geo.output;
1012 pix->width = geo.in_width;
1013 pix->height = geo.in_height;
1014
1015 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1016 geo.scale_idx_h, geo.scale_idx_v);
1017
1018 return 0;
1019}
1020
1021
1022
1023
1024
1025
1026
1027
1028static int sh_vou_cropcap(struct file *file, void *priv,
1029 struct v4l2_cropcap *a)
1030{
1031 struct sh_vou_device *vou_dev = video_drvdata(file);
1032
1033 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1034
1035 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1036 a->bounds.left = 0;
1037 a->bounds.top = 0;
1038 a->bounds.width = VOU_MAX_IMAGE_WIDTH;
1039 a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
1040
1041 a->defrect.left = 0;
1042 a->defrect.top = 0;
1043 a->defrect.width = VOU_MAX_IMAGE_WIDTH;
1044 a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
1045 a->pixelaspect.numerator = 1;
1046 a->pixelaspect.denominator = 1;
1047
1048 return 0;
1049}
1050
1051static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1052{
1053 struct sh_vou_device *vou_dev = dev_id;
1054 static unsigned long j;
1055 struct videobuf_buffer *vb;
1056 static int cnt;
1057 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1058 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1059
1060 if (!(irq_status & 0x300)) {
1061 if (printk_timed_ratelimit(&j, 500))
1062 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1063 irq_status);
1064 return IRQ_NONE;
1065 }
1066
1067 spin_lock(&vou_dev->lock);
1068 if (!vou_dev->active || list_empty(&vou_dev->queue)) {
1069 if (printk_timed_ratelimit(&j, 500))
1070 dev_warn(vou_dev->v4l2_dev.dev,
1071 "IRQ without active buffer: %x!\n", irq_status);
1072
1073 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1074 spin_unlock(&vou_dev->lock);
1075 return IRQ_HANDLED;
1076 }
1077
1078 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1079 dev_dbg(vou_dev->v4l2_dev.dev,
1080 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1081 irq_status, masked, vou_status, cnt);
1082
1083 cnt++;
1084
1085
1086
1087 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1088
1089 vb = vou_dev->active;
1090 list_del(&vb->queue);
1091
1092 vb->state = VIDEOBUF_DONE;
1093 v4l2_get_timestamp(&vb->ts);
1094 vb->field_count++;
1095 wake_up(&vb->done);
1096
1097 if (list_empty(&vou_dev->queue)) {
1098
1099 dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
1100 __func__, cnt);
1101 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
1102 vou_dev->active = NULL;
1103 vou_dev->status = SH_VOU_INITIALISING;
1104
1105 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
1106 spin_unlock(&vou_dev->lock);
1107 return IRQ_HANDLED;
1108 }
1109
1110 vou_dev->active = list_entry(vou_dev->queue.next,
1111 struct videobuf_buffer, queue);
1112
1113 if (vou_dev->active->queue.next != &vou_dev->queue) {
1114 struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
1115 struct videobuf_buffer, queue);
1116 sh_vou_schedule_next(vou_dev, new);
1117 }
1118
1119 spin_unlock(&vou_dev->lock);
1120
1121 return IRQ_HANDLED;
1122}
1123
1124static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1125{
1126 struct sh_vou_pdata *pdata = vou_dev->pdata;
1127 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1128 int i = 100;
1129
1130
1131 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1132
1133
1134 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1135 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1136 udelay(1);
1137
1138 if (!i)
1139 return -ETIMEDOUT;
1140
1141 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1142
1143 if (pdata->flags & SH_VOU_PCLK_FALLING)
1144 voucr |= 1 << 28;
1145 if (pdata->flags & SH_VOU_HSYNC_LOW)
1146 voucr |= 1 << 27;
1147 if (pdata->flags & SH_VOU_VSYNC_LOW)
1148 voucr |= 1 << 26;
1149 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1150
1151
1152 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1153
1154 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1155
1156 return 0;
1157}
1158
1159
1160static int sh_vou_open(struct file *file)
1161{
1162 struct sh_vou_device *vou_dev = video_drvdata(file);
1163 struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
1164 GFP_KERNEL);
1165
1166 if (!vou_file)
1167 return -ENOMEM;
1168
1169 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1170
1171 file->private_data = vou_file;
1172
1173 if (mutex_lock_interruptible(&vou_dev->fop_lock))
1174 return -ERESTARTSYS;
1175 if (atomic_inc_return(&vou_dev->use_count) == 1) {
1176 int ret;
1177
1178 vou_dev->status = SH_VOU_INITIALISING;
1179 pm_runtime_get_sync(vou_dev->v4l2_dev.dev);
1180 ret = sh_vou_hw_init(vou_dev);
1181 if (ret < 0) {
1182 atomic_dec(&vou_dev->use_count);
1183 pm_runtime_put(vou_dev->v4l2_dev.dev);
1184 vou_dev->status = SH_VOU_IDLE;
1185 mutex_unlock(&vou_dev->fop_lock);
1186 return ret;
1187 }
1188 }
1189
1190 videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
1191 vou_dev->v4l2_dev.dev, &vou_dev->lock,
1192 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1193 V4L2_FIELD_NONE,
1194 sizeof(struct videobuf_buffer),
1195 vou_dev->vdev, &vou_dev->fop_lock);
1196 mutex_unlock(&vou_dev->fop_lock);
1197
1198 return 0;
1199}
1200
1201static int sh_vou_release(struct file *file)
1202{
1203 struct sh_vou_device *vou_dev = video_drvdata(file);
1204 struct sh_vou_file *vou_file = file->private_data;
1205
1206 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1207
1208 if (!atomic_dec_return(&vou_dev->use_count)) {
1209 mutex_lock(&vou_dev->fop_lock);
1210
1211 vou_dev->status = SH_VOU_IDLE;
1212 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
1213 pm_runtime_put(vou_dev->v4l2_dev.dev);
1214 mutex_unlock(&vou_dev->fop_lock);
1215 }
1216
1217 file->private_data = NULL;
1218 kfree(vou_file);
1219
1220 return 0;
1221}
1222
1223static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
1224{
1225 struct sh_vou_device *vou_dev = video_drvdata(file);
1226 struct sh_vou_file *vou_file = file->private_data;
1227 int ret;
1228
1229 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1230
1231 if (mutex_lock_interruptible(&vou_dev->fop_lock))
1232 return -ERESTARTSYS;
1233 ret = videobuf_mmap_mapper(&vou_file->vbq, vma);
1234 mutex_unlock(&vou_dev->fop_lock);
1235 return ret;
1236}
1237
1238static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
1239{
1240 struct sh_vou_device *vou_dev = video_drvdata(file);
1241 struct sh_vou_file *vou_file = file->private_data;
1242 unsigned int res;
1243
1244 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1245
1246 mutex_lock(&vou_dev->fop_lock);
1247 res = videobuf_poll_stream(file, &vou_file->vbq, wait);
1248 mutex_unlock(&vou_dev->fop_lock);
1249 return res;
1250}
1251
1252
1253static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1254 .vidioc_querycap = sh_vou_querycap,
1255 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1256 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1257 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1258 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1259 .vidioc_reqbufs = sh_vou_reqbufs,
1260 .vidioc_querybuf = sh_vou_querybuf,
1261 .vidioc_qbuf = sh_vou_qbuf,
1262 .vidioc_dqbuf = sh_vou_dqbuf,
1263 .vidioc_streamon = sh_vou_streamon,
1264 .vidioc_streamoff = sh_vou_streamoff,
1265 .vidioc_s_std = sh_vou_s_std,
1266 .vidioc_g_std = sh_vou_g_std,
1267 .vidioc_cropcap = sh_vou_cropcap,
1268 .vidioc_g_crop = sh_vou_g_crop,
1269 .vidioc_s_crop = sh_vou_s_crop,
1270};
1271
1272static const struct v4l2_file_operations sh_vou_fops = {
1273 .owner = THIS_MODULE,
1274 .open = sh_vou_open,
1275 .release = sh_vou_release,
1276 .unlocked_ioctl = video_ioctl2,
1277 .mmap = sh_vou_mmap,
1278 .poll = sh_vou_poll,
1279};
1280
1281static const struct video_device sh_vou_video_template = {
1282 .name = "sh_vou",
1283 .fops = &sh_vou_fops,
1284 .ioctl_ops = &sh_vou_ioctl_ops,
1285 .tvnorms = V4L2_STD_525_60,
1286 .vfl_dir = VFL_DIR_TX,
1287};
1288
1289static int sh_vou_probe(struct platform_device *pdev)
1290{
1291 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1292 struct v4l2_rect *rect;
1293 struct v4l2_pix_format *pix;
1294 struct i2c_adapter *i2c_adap;
1295 struct video_device *vdev;
1296 struct sh_vou_device *vou_dev;
1297 struct resource *reg_res, *region;
1298 struct v4l2_subdev *subdev;
1299 int irq, ret;
1300
1301 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1302 irq = platform_get_irq(pdev, 0);
1303
1304 if (!vou_pdata || !reg_res || irq <= 0) {
1305 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1306 return -ENODEV;
1307 }
1308
1309 vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
1310 if (!vou_dev)
1311 return -ENOMEM;
1312
1313 INIT_LIST_HEAD(&vou_dev->queue);
1314 spin_lock_init(&vou_dev->lock);
1315 mutex_init(&vou_dev->fop_lock);
1316 atomic_set(&vou_dev->use_count, 0);
1317 vou_dev->pdata = vou_pdata;
1318 vou_dev->status = SH_VOU_IDLE;
1319
1320 rect = &vou_dev->rect;
1321 pix = &vou_dev->pix;
1322
1323
1324 vou_dev->std = V4L2_STD_NTSC_M;
1325 rect->left = 0;
1326 rect->top = 0;
1327 rect->width = VOU_MAX_IMAGE_WIDTH;
1328 rect->height = 480;
1329 pix->width = VOU_MAX_IMAGE_WIDTH;
1330 pix->height = 480;
1331 pix->pixelformat = V4L2_PIX_FMT_YVYU;
1332 pix->field = V4L2_FIELD_NONE;
1333 pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
1334 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
1335 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1336
1337 region = request_mem_region(reg_res->start, resource_size(reg_res),
1338 pdev->name);
1339 if (!region) {
1340 dev_err(&pdev->dev, "VOU region already claimed\n");
1341 ret = -EBUSY;
1342 goto ereqmemreg;
1343 }
1344
1345 vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
1346 if (!vou_dev->base) {
1347 ret = -ENOMEM;
1348 goto emap;
1349 }
1350
1351 ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
1352 if (ret < 0)
1353 goto ereqirq;
1354
1355 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1356 if (ret < 0) {
1357 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1358 goto ev4l2devreg;
1359 }
1360
1361
1362 vdev = video_device_alloc();
1363 if (vdev == NULL) {
1364 ret = -ENOMEM;
1365 goto evdevalloc;
1366 }
1367
1368 *vdev = sh_vou_video_template;
1369 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1370 vdev->tvnorms |= V4L2_STD_PAL;
1371 vdev->v4l2_dev = &vou_dev->v4l2_dev;
1372 vdev->release = video_device_release;
1373 vdev->lock = &vou_dev->fop_lock;
1374
1375 vou_dev->vdev = vdev;
1376 video_set_drvdata(vdev, vou_dev);
1377
1378 pm_runtime_enable(&pdev->dev);
1379 pm_runtime_resume(&pdev->dev);
1380
1381 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1382 if (!i2c_adap) {
1383 ret = -ENODEV;
1384 goto ei2cgadap;
1385 }
1386
1387 ret = sh_vou_hw_init(vou_dev);
1388 if (ret < 0)
1389 goto ereset;
1390
1391 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
1392 vou_pdata->board_info, NULL);
1393 if (!subdev) {
1394 ret = -ENOMEM;
1395 goto ei2cnd;
1396 }
1397
1398 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1399 if (ret < 0)
1400 goto evregdev;
1401
1402 return 0;
1403
1404evregdev:
1405ei2cnd:
1406ereset:
1407 i2c_put_adapter(i2c_adap);
1408ei2cgadap:
1409 video_device_release(vdev);
1410 pm_runtime_disable(&pdev->dev);
1411evdevalloc:
1412 v4l2_device_unregister(&vou_dev->v4l2_dev);
1413ev4l2devreg:
1414 free_irq(irq, vou_dev);
1415ereqirq:
1416 iounmap(vou_dev->base);
1417emap:
1418 release_mem_region(reg_res->start, resource_size(reg_res));
1419ereqmemreg:
1420 kfree(vou_dev);
1421 return ret;
1422}
1423
1424static int sh_vou_remove(struct platform_device *pdev)
1425{
1426 int irq = platform_get_irq(pdev, 0);
1427 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1428 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1429 struct sh_vou_device, v4l2_dev);
1430 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1431 struct v4l2_subdev, list);
1432 struct i2c_client *client = v4l2_get_subdevdata(sd);
1433 struct resource *reg_res;
1434
1435 if (irq > 0)
1436 free_irq(irq, vou_dev);
1437 pm_runtime_disable(&pdev->dev);
1438 video_unregister_device(vou_dev->vdev);
1439 i2c_put_adapter(client->adapter);
1440 v4l2_device_unregister(&vou_dev->v4l2_dev);
1441 iounmap(vou_dev->base);
1442 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1443 if (reg_res)
1444 release_mem_region(reg_res->start, resource_size(reg_res));
1445 kfree(vou_dev);
1446 return 0;
1447}
1448
1449static struct platform_driver __refdata sh_vou = {
1450 .remove = sh_vou_remove,
1451 .driver = {
1452 .name = "sh-vou",
1453 },
1454};
1455
1456module_platform_driver_probe(sh_vou, sh_vou_probe);
1457
1458MODULE_DESCRIPTION("SuperH VOU driver");
1459MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1460MODULE_LICENSE("GPL v2");
1461MODULE_VERSION("0.1.0");
1462MODULE_ALIAS("platform:sh-vou");
1463