blob: f0ec55134b4596bd6f77b575c6c4e6c5c76b1abc [file] [log] [blame]
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001/*
2 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Freescale VIU video driver
5 *
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Porting to 2.6.35 by DENX Software Engineering,
8 * Anatolij Gustschin <agust@denx.de>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/clk.h>
19#include <linux/kernel.h>
20#include <linux/i2c.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Rob Herring5af50732013-09-17 14:28:33 -050024#include <linux/of_address.h>
25#include <linux/of_irq.h>
Anatolij Gustschin95c5d602010-07-02 10:10:09 -030026#include <linux/of_platform.h>
Anatolij Gustschine69e34e2010-09-15 18:31:09 -030027#include <linux/slab.h>
Anatolij Gustschin95c5d602010-07-02 10:10:09 -030028#include <media/v4l2-common.h>
29#include <media/v4l2-device.h>
30#include <media/v4l2-ioctl.h>
Hans Verkuil1fea3d62015-07-20 10:09:28 -030031#include <media/v4l2-ctrls.h>
Anatolij Gustschin95c5d602010-07-02 10:10:09 -030032#include <media/videobuf-dma-contig.h>
33
34#define DRV_NAME "fsl_viu"
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030035#define VIU_VERSION "0.5.1"
Anatolij Gustschin95c5d602010-07-02 10:10:09 -030036
37#define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
38
39#define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
40
41/* I2C address of video decoder chip is 0x4A */
42#define VIU_VIDEO_DECODER_ADDR 0x25
43
Anatolij Gustschin95c5d602010-07-02 10:10:09 -030044static int info_level;
45
46#define dprintk(level, fmt, arg...) \
47 do { \
48 if (level <= info_level) \
49 printk(KERN_DEBUG "viu: " fmt , ## arg); \
50 } while (0)
51
52/*
53 * Basic structures
54 */
55struct viu_fmt {
56 char name[32];
57 u32 fourcc; /* v4l2 format id */
58 u32 pixelformat;
59 int depth;
60};
61
62static struct viu_fmt formats[] = {
63 {
64 .name = "RGB-16 (5/B-6/G-5/R)",
65 .fourcc = V4L2_PIX_FMT_RGB565,
66 .pixelformat = V4L2_PIX_FMT_RGB565,
67 .depth = 16,
68 }, {
69 .name = "RGB-32 (A-R-G-B)",
70 .fourcc = V4L2_PIX_FMT_RGB32,
71 .pixelformat = V4L2_PIX_FMT_RGB32,
72 .depth = 32,
73 }
74};
75
76struct viu_dev;
77struct viu_buf;
78
79/* buffer for one video frame */
80struct viu_buf {
81 /* common v4l buffer stuff -- must be first */
82 struct videobuf_buffer vb;
83 struct viu_fmt *fmt;
84};
85
86struct viu_dmaqueue {
87 struct viu_dev *dev;
88 struct list_head active;
89 struct list_head queued;
90 struct timer_list timeout;
91};
92
93struct viu_status {
94 u32 field_irq;
95 u32 vsync_irq;
96 u32 hsync_irq;
97 u32 vstart_irq;
98 u32 dma_end_irq;
99 u32 error_irq;
100};
101
102struct viu_reg {
103 u32 status_cfg;
104 u32 luminance;
105 u32 chroma_r;
106 u32 chroma_g;
107 u32 chroma_b;
108 u32 field_base_addr;
109 u32 dma_inc;
110 u32 picture_count;
111 u32 req_alarm;
112 u32 alpha;
113} __attribute__ ((packed));
114
115struct viu_dev {
116 struct v4l2_device v4l2_dev;
Hans Verkuil1fea3d62015-07-20 10:09:28 -0300117 struct v4l2_ctrl_handler hdl;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300118 struct mutex lock;
119 spinlock_t slock;
120 int users;
121
122 struct device *dev;
123 /* various device info */
124 struct video_device *vdev;
125 struct viu_dmaqueue vidq;
126 enum v4l2_field capfield;
127 int field;
128 int first;
129 int dma_done;
130
131 /* Hardware register area */
132 struct viu_reg *vr;
133
134 /* Interrupt vector */
135 int irq;
136 struct viu_status irqs;
137
138 /* video overlay */
139 struct v4l2_framebuffer ovbuf;
140 struct viu_fmt *ovfmt;
141 unsigned int ovenable;
142 enum v4l2_field ovfield;
143
144 /* crop */
145 struct v4l2_rect crop_current;
146
147 /* clock pointer */
148 struct clk *clk;
149
150 /* decoder */
151 struct v4l2_subdev *decoder;
Anatolij Gustschin50155c22010-12-22 17:31:59 -0300152
153 v4l2_std_id std;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300154};
155
156struct viu_fh {
157 struct viu_dev *dev;
158
159 /* video capture */
160 struct videobuf_queue vb_vidq;
161 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
162
163 /* video overlay */
164 struct v4l2_window win;
165 struct v4l2_clip clips[1];
166
167 /* video capture */
168 struct viu_fmt *fmt;
169 int width, height, sizeimage;
170 enum v4l2_buf_type type;
171};
172
173static struct viu_reg reg_val;
174
175/*
176 * Macro definitions of VIU registers
177 */
178
179/* STATUS_CONFIG register */
180enum status_config {
181 SOFT_RST = 1 << 0,
182
183 ERR_MASK = 0x0f << 4, /* Error code mask */
184 ERR_NO = 0x00, /* No error */
185 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
186 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
187 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
188 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
189 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
190 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
191 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
192 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
193 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
194 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
195
196 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
197 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
198 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
199 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
200 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
201 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
202 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
203
204 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
205 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
206 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
207 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
208 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
209 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
210
211 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
212 FIELD_NO = 0x01 << 28, /* Field number */
213 DITHER_ON = 0x01 << 29, /* Dithering is on */
214 ROUND_ON = 0x01 << 30, /* Round is on */
215 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
216 * 0 in RGB565
217 */
218};
219
220#define norm_maxw() 720
221#define norm_maxh() 576
222
223#define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
224 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
225 INT_DMA_END_STATUS | INT_ERROR_STATUS)
226
227#define NUM_FORMATS ARRAY_SIZE(formats)
228
229static irqreturn_t viu_intr(int irq, void *dev_id);
230
231struct viu_fmt *format_by_fourcc(int fourcc)
232{
233 int i;
234
235 for (i = 0; i < NUM_FORMATS; i++) {
236 if (formats[i].pixelformat == fourcc)
237 return formats + i;
238 }
239
240 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
241 return NULL;
242}
243
244void viu_start_dma(struct viu_dev *dev)
245{
246 struct viu_reg *vr = dev->vr;
247
248 dev->field = 0;
249
250 /* Enable DMA operation */
251 out_be32(&vr->status_cfg, SOFT_RST);
252 out_be32(&vr->status_cfg, INT_FIELD_EN);
253}
254
255void viu_stop_dma(struct viu_dev *dev)
256{
257 struct viu_reg *vr = dev->vr;
258 int cnt = 100;
259 u32 status_cfg;
260
261 out_be32(&vr->status_cfg, 0);
262
263 /* Clear pending interrupts */
264 status_cfg = in_be32(&vr->status_cfg);
265 if (status_cfg & 0x3f0000)
266 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
267
268 if (status_cfg & DMA_ACT) {
269 do {
270 status_cfg = in_be32(&vr->status_cfg);
271 if (status_cfg & INT_DMA_END_STATUS)
272 break;
273 } while (cnt--);
274
275 if (cnt < 0) {
276 /* timed out, issue soft reset */
277 out_be32(&vr->status_cfg, SOFT_RST);
278 out_be32(&vr->status_cfg, 0);
279 } else {
280 /* clear DMA_END and other pending irqs */
281 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
282 }
283 }
284
285 dev->field = 0;
286}
287
288static int restart_video_queue(struct viu_dmaqueue *vidq)
289{
290 struct viu_buf *buf, *prev;
291
292 dprintk(1, "%s vidq=0x%08lx\n", __func__, (unsigned long)vidq);
293 if (!list_empty(&vidq->active)) {
294 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
295 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
296 buf, buf->vb.i);
297
298 viu_stop_dma(vidq->dev);
299
300 /* cancel all outstanding capture requests */
301 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
302 list_del(&buf->vb.queue);
303 buf->vb.state = VIDEOBUF_ERROR;
304 wake_up(&buf->vb.done);
305 }
306 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
307 return 0;
308 }
309
310 prev = NULL;
311 for (;;) {
312 if (list_empty(&vidq->queued))
313 return 0;
314 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
315 if (prev == NULL) {
Wei Yongjun0666c6b2012-10-08 08:34:09 -0300316 list_move_tail(&buf->vb.queue, &vidq->active);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300317
318 dprintk(1, "Restarting video dma\n");
319 viu_stop_dma(vidq->dev);
320 viu_start_dma(vidq->dev);
321
322 buf->vb.state = VIDEOBUF_ACTIVE;
323 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
324 dprintk(2, "[%p/%d] restart_queue - first active\n",
325 buf, buf->vb.i);
326
327 } else if (prev->vb.width == buf->vb.width &&
328 prev->vb.height == buf->vb.height &&
329 prev->fmt == buf->fmt) {
Wei Yongjun0666c6b2012-10-08 08:34:09 -0300330 list_move_tail(&buf->vb.queue, &vidq->active);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300331 buf->vb.state = VIDEOBUF_ACTIVE;
332 dprintk(2, "[%p/%d] restart_queue - move to active\n",
333 buf, buf->vb.i);
334 } else {
335 return 0;
336 }
337 prev = buf;
338 }
339}
340
341static void viu_vid_timeout(unsigned long data)
342{
343 struct viu_dev *dev = (struct viu_dev *)data;
344 struct viu_buf *buf;
345 struct viu_dmaqueue *vidq = &dev->vidq;
346
347 while (!list_empty(&vidq->active)) {
348 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
349 list_del(&buf->vb.queue);
350 buf->vb.state = VIDEOBUF_ERROR;
351 wake_up(&buf->vb.done);
352 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
353 }
354
355 restart_video_queue(vidq);
356}
357
358/*
359 * Videobuf operations
360 */
361static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
362 unsigned int *size)
363{
364 struct viu_fh *fh = vq->priv_data;
365
366 *size = fh->width * fh->height * fh->fmt->depth >> 3;
367 if (*count == 0)
368 *count = 32;
369
370 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
371 (*count)--;
372
373 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
374 return 0;
375}
376
377static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
378{
379 struct videobuf_buffer *vb = &buf->vb;
380 void *vaddr = NULL;
381
382 BUG_ON(in_interrupt());
383
Hans Verkuil0e0809a2010-09-26 09:01:26 -0300384 videobuf_waiton(vq, &buf->vb, 0, 0);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300385
386 if (vq->int_ops && vq->int_ops->vaddr)
387 vaddr = vq->int_ops->vaddr(vb);
388
389 if (vaddr)
390 videobuf_dma_contig_free(vq, &buf->vb);
391
392 buf->vb.state = VIDEOBUF_NEEDS_INIT;
393}
394
395inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
396{
397 struct viu_reg *vr = dev->vr;
398 int bpp;
399
400 /* setup the DMA base address */
401 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
402
403 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
404 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
405
406 /* interlace is on by default, set horizontal DMA increment */
407 reg_val.status_cfg = 0;
408 bpp = buf->fmt->depth >> 3;
409 switch (bpp) {
410 case 2:
411 reg_val.status_cfg &= ~MODE_32BIT;
412 reg_val.dma_inc = buf->vb.width * 2;
413 break;
414 case 4:
415 reg_val.status_cfg |= MODE_32BIT;
416 reg_val.dma_inc = buf->vb.width * 4;
417 break;
418 default:
419 dprintk(0, "doesn't support color depth(%d)\n",
420 bpp * 8);
421 return -EINVAL;
422 }
423
424 /* setup picture_count register */
425 reg_val.picture_count = (buf->vb.height / 2) << 16 |
426 buf->vb.width;
427
428 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
429
430 buf->vb.state = VIDEOBUF_ACTIVE;
431 dev->capfield = buf->vb.field;
432
433 /* reset dma increment if needed */
434 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
435 reg_val.dma_inc = 0;
436
437 out_be32(&vr->dma_inc, reg_val.dma_inc);
438 out_be32(&vr->picture_count, reg_val.picture_count);
439 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
440 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
441 return 0;
442}
443
444static int buffer_prepare(struct videobuf_queue *vq,
445 struct videobuf_buffer *vb,
446 enum v4l2_field field)
447{
448 struct viu_fh *fh = vq->priv_data;
449 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
450 int rc;
451
452 BUG_ON(fh->fmt == NULL);
453
454 if (fh->width < 48 || fh->width > norm_maxw() ||
455 fh->height < 32 || fh->height > norm_maxh())
456 return -EINVAL;
457 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
458 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
459 return -EINVAL;
460
461 if (buf->fmt != fh->fmt ||
462 buf->vb.width != fh->width ||
463 buf->vb.height != fh->height ||
464 buf->vb.field != field) {
465 buf->fmt = fh->fmt;
466 buf->vb.width = fh->width;
467 buf->vb.height = fh->height;
468 buf->vb.field = field;
469 }
470
471 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
472 rc = videobuf_iolock(vq, &buf->vb, NULL);
473 if (rc != 0)
474 goto fail;
475
476 buf->vb.width = fh->width;
477 buf->vb.height = fh->height;
478 buf->vb.field = field;
479 buf->fmt = fh->fmt;
480 }
481
482 buf->vb.state = VIDEOBUF_PREPARED;
483 return 0;
484
485fail:
486 free_buffer(vq, buf);
487 return rc;
488}
489
490static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
491{
492 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
493 struct viu_fh *fh = vq->priv_data;
494 struct viu_dev *dev = fh->dev;
495 struct viu_dmaqueue *vidq = &dev->vidq;
496 struct viu_buf *prev;
497
498 if (!list_empty(&vidq->queued)) {
499 dprintk(1, "adding vb queue=0x%08lx\n",
500 (unsigned long)&buf->vb.queue);
501 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
502 vidq, &vidq->queued);
503 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
504 dev, &vidq->queued, vidq->queued.next,
505 vidq->queued.prev);
506 list_add_tail(&buf->vb.queue, &vidq->queued);
507 buf->vb.state = VIDEOBUF_QUEUED;
508 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
509 buf, buf->vb.i);
510 } else if (list_empty(&vidq->active)) {
511 dprintk(1, "adding vb active=0x%08lx\n",
512 (unsigned long)&buf->vb.queue);
513 list_add_tail(&buf->vb.queue, &vidq->active);
514 buf->vb.state = VIDEOBUF_ACTIVE;
515 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
516 dprintk(2, "[%p/%d] buffer_queue - first active\n",
517 buf, buf->vb.i);
518
519 buffer_activate(dev, buf);
520 } else {
521 dprintk(1, "adding vb queue2=0x%08lx\n",
522 (unsigned long)&buf->vb.queue);
523 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
524 if (prev->vb.width == buf->vb.width &&
525 prev->vb.height == buf->vb.height &&
526 prev->fmt == buf->fmt) {
527 list_add_tail(&buf->vb.queue, &vidq->active);
528 buf->vb.state = VIDEOBUF_ACTIVE;
529 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
530 buf, buf->vb.i);
531 } else {
532 list_add_tail(&buf->vb.queue, &vidq->queued);
533 buf->vb.state = VIDEOBUF_QUEUED;
534 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
535 buf, buf->vb.i);
536 }
537 }
538}
539
540static void buffer_release(struct videobuf_queue *vq,
541 struct videobuf_buffer *vb)
542{
543 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
544 struct viu_fh *fh = vq->priv_data;
545 struct viu_dev *dev = (struct viu_dev *)fh->dev;
546
547 viu_stop_dma(dev);
548 free_buffer(vq, buf);
549}
550
551static struct videobuf_queue_ops viu_video_qops = {
552 .buf_setup = buffer_setup,
553 .buf_prepare = buffer_prepare,
554 .buf_queue = buffer_queue,
555 .buf_release = buffer_release,
556};
557
558/*
559 * IOCTL vidioc handling
560 */
561static int vidioc_querycap(struct file *file, void *priv,
562 struct v4l2_capability *cap)
563{
564 strcpy(cap->driver, "viu");
565 strcpy(cap->card, "viu");
Hans Verkuila020c742014-11-24 06:37:25 -0300566 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300567 V4L2_CAP_STREAMING |
568 V4L2_CAP_VIDEO_OVERLAY |
569 V4L2_CAP_READWRITE;
Hans Verkuila020c742014-11-24 06:37:25 -0300570 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300571 return 0;
572}
573
574static int vidioc_enum_fmt(struct file *file, void *priv,
575 struct v4l2_fmtdesc *f)
576{
577 int index = f->index;
578
579 if (f->index > NUM_FORMATS)
580 return -EINVAL;
581
582 strlcpy(f->description, formats[index].name, sizeof(f->description));
583 f->pixelformat = formats[index].fourcc;
584 return 0;
585}
586
587static int vidioc_g_fmt_cap(struct file *file, void *priv,
588 struct v4l2_format *f)
589{
590 struct viu_fh *fh = priv;
591
592 f->fmt.pix.width = fh->width;
593 f->fmt.pix.height = fh->height;
594 f->fmt.pix.field = fh->vb_vidq.field;
595 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
596 f->fmt.pix.bytesperline =
597 (f->fmt.pix.width * fh->fmt->depth) >> 3;
598 f->fmt.pix.sizeimage = fh->sizeimage;
599 return 0;
600}
601
602static int vidioc_try_fmt_cap(struct file *file, void *priv,
603 struct v4l2_format *f)
604{
605 struct viu_fmt *fmt;
606 enum v4l2_field field;
607 unsigned int maxw, maxh;
608
609 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
610 if (!fmt) {
611 dprintk(1, "Fourcc format (0x%08x) invalid.",
612 f->fmt.pix.pixelformat);
613 return -EINVAL;
614 }
615
616 field = f->fmt.pix.field;
617
618 if (field == V4L2_FIELD_ANY) {
619 field = V4L2_FIELD_INTERLACED;
620 } else if (field != V4L2_FIELD_INTERLACED) {
621 dprintk(1, "Field type invalid.\n");
622 return -EINVAL;
623 }
624
625 maxw = norm_maxw();
626 maxh = norm_maxh();
627
628 f->fmt.pix.field = field;
629 if (f->fmt.pix.height < 32)
630 f->fmt.pix.height = 32;
631 if (f->fmt.pix.height > maxh)
632 f->fmt.pix.height = maxh;
633 if (f->fmt.pix.width < 48)
634 f->fmt.pix.width = 48;
635 if (f->fmt.pix.width > maxw)
636 f->fmt.pix.width = maxw;
637 f->fmt.pix.width &= ~0x03;
638 f->fmt.pix.bytesperline =
639 (f->fmt.pix.width * fmt->depth) >> 3;
640
641 return 0;
642}
643
644static int vidioc_s_fmt_cap(struct file *file, void *priv,
645 struct v4l2_format *f)
646{
647 struct viu_fh *fh = priv;
648 int ret;
649
650 ret = vidioc_try_fmt_cap(file, fh, f);
651 if (ret < 0)
652 return ret;
653
654 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
655 fh->width = f->fmt.pix.width;
656 fh->height = f->fmt.pix.height;
657 fh->sizeimage = f->fmt.pix.sizeimage;
658 fh->vb_vidq.field = f->fmt.pix.field;
659 fh->type = f->type;
660 dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh->fmt->name);
661 return 0;
662}
663
664static int vidioc_g_fmt_overlay(struct file *file, void *priv,
665 struct v4l2_format *f)
666{
667 struct viu_fh *fh = priv;
668
669 f->fmt.win = fh->win;
670 return 0;
671}
672
673static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
674{
675 enum v4l2_field field;
676 int maxw, maxh;
677
678 if (dev->ovbuf.base == NULL)
679 return -EINVAL;
680 if (dev->ovfmt == NULL)
681 return -EINVAL;
682 if (win->w.width < 48 || win->w.height < 32)
683 return -EINVAL;
684
685 field = win->field;
686 maxw = dev->crop_current.width;
687 maxh = dev->crop_current.height;
688
689 if (field == V4L2_FIELD_ANY) {
690 field = (win->w.height > maxh/2)
691 ? V4L2_FIELD_INTERLACED
692 : V4L2_FIELD_TOP;
693 }
694 switch (field) {
695 case V4L2_FIELD_TOP:
696 case V4L2_FIELD_BOTTOM:
697 maxh = maxh / 2;
698 break;
699 case V4L2_FIELD_INTERLACED:
700 break;
701 default:
702 return -EINVAL;
703 }
704
705 win->field = field;
706 if (win->w.width > maxw)
707 win->w.width = maxw;
708 if (win->w.height > maxh)
709 win->w.height = maxh;
710 return 0;
711}
712
713inline void viu_activate_overlay(struct viu_reg *viu_reg)
714{
715 struct viu_reg *vr = viu_reg;
716
717 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
718 out_be32(&vr->dma_inc, reg_val.dma_inc);
719 out_be32(&vr->picture_count, reg_val.picture_count);
720}
721
Anatolij Gustschin791ae692011-05-04 17:19:28 -0300722static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300723{
724 int bpp;
725
726 dprintk(1, "%s %dx%d %s\n", __func__,
727 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
728
729 reg_val.status_cfg = 0;
730
731 /* setup window */
732 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
733 fh->win.w.width;
734
735 /* setup color depth and dma increment */
736 bpp = dev->ovfmt->depth / 8;
737 switch (bpp) {
738 case 2:
739 reg_val.status_cfg &= ~MODE_32BIT;
740 reg_val.dma_inc = fh->win.w.width * 2;
741 break;
742 case 4:
743 reg_val.status_cfg |= MODE_32BIT;
744 reg_val.dma_inc = fh->win.w.width * 4;
745 break;
746 default:
747 dprintk(0, "device doesn't support color depth(%d)\n",
748 bpp * 8);
749 return -EINVAL;
750 }
751
752 dev->ovfield = fh->win.field;
753 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
754 reg_val.dma_inc = 0;
755
756 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
757
758 /* setup the base address of the overlay buffer */
759 reg_val.field_base_addr = (u32)dev->ovbuf.base;
760
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300761 return 0;
762}
763
764static int vidioc_s_fmt_overlay(struct file *file, void *priv,
765 struct v4l2_format *f)
766{
767 struct viu_fh *fh = priv;
768 struct viu_dev *dev = (struct viu_dev *)fh->dev;
769 unsigned long flags;
770 int err;
771
772 err = verify_preview(dev, &f->fmt.win);
773 if (err)
774 return err;
775
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300776 fh->win = f->fmt.win;
777
778 spin_lock_irqsave(&dev->slock, flags);
Anatolij Gustschin791ae692011-05-04 17:19:28 -0300779 viu_setup_preview(dev, fh);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300780 spin_unlock_irqrestore(&dev->slock, flags);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300781 return 0;
782}
783
784static int vidioc_try_fmt_overlay(struct file *file, void *priv,
785 struct v4l2_format *f)
786{
787 return 0;
788}
789
Anatolij Gustschin791ae692011-05-04 17:19:28 -0300790static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
791{
792 struct viu_fh *fh = priv;
793 struct viu_dev *dev = (struct viu_dev *)fh->dev;
794 unsigned long flags;
795
796 if (on) {
797 spin_lock_irqsave(&dev->slock, flags);
798 viu_activate_overlay(dev->vr);
799 dev->ovenable = 1;
800
801 /* start dma */
802 viu_start_dma(dev);
803 spin_unlock_irqrestore(&dev->slock, flags);
804 } else {
805 viu_stop_dma(dev);
806 dev->ovenable = 0;
807 }
808
809 return 0;
810}
811
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300812int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
813{
814 struct viu_fh *fh = priv;
815 struct viu_dev *dev = fh->dev;
816 struct v4l2_framebuffer *fb = arg;
817
818 *fb = dev->ovbuf;
819 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
820 return 0;
821}
822
Hans Verkuile6eb28c2012-09-04 10:26:45 -0300823int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *arg)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300824{
825 struct viu_fh *fh = priv;
826 struct viu_dev *dev = fh->dev;
Hans Verkuil99b32b22012-09-04 10:26:45 -0300827 const struct v4l2_framebuffer *fb = arg;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300828 struct viu_fmt *fmt;
829
830 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
831 return -EPERM;
832
833 /* check args */
834 fmt = format_by_fourcc(fb->fmt.pixelformat);
835 if (fmt == NULL)
836 return -EINVAL;
837
838 /* ok, accept it */
839 dev->ovbuf = *fb;
840 dev->ovfmt = fmt;
841 if (dev->ovbuf.fmt.bytesperline == 0) {
842 dev->ovbuf.fmt.bytesperline =
843 dev->ovbuf.fmt.width * fmt->depth / 8;
844 }
845 return 0;
846}
847
848static int vidioc_reqbufs(struct file *file, void *priv,
849 struct v4l2_requestbuffers *p)
850{
851 struct viu_fh *fh = priv;
852
853 return videobuf_reqbufs(&fh->vb_vidq, p);
854}
855
856static int vidioc_querybuf(struct file *file, void *priv,
857 struct v4l2_buffer *p)
858{
859 struct viu_fh *fh = priv;
860
861 return videobuf_querybuf(&fh->vb_vidq, p);
862}
863
864static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
865{
866 struct viu_fh *fh = priv;
867
868 return videobuf_qbuf(&fh->vb_vidq, p);
869}
870
871static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
872{
873 struct viu_fh *fh = priv;
874
875 return videobuf_dqbuf(&fh->vb_vidq, p,
876 file->f_flags & O_NONBLOCK);
877}
878
879static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
880{
881 struct viu_fh *fh = priv;
Anatolij Gustschinf3b1af12011-04-12 08:15:58 -0300882 struct viu_dev *dev = fh->dev;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300883
884 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
885 return -EINVAL;
886 if (fh->type != i)
887 return -EINVAL;
888
Anatolij Gustschinf3b1af12011-04-12 08:15:58 -0300889 if (dev->ovenable)
890 dev->ovenable = 0;
891
Anatolij Gustschinc3353332010-12-17 06:40:50 -0300892 viu_start_dma(fh->dev);
893
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300894 return videobuf_streamon(&fh->vb_vidq);
895}
896
897static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
898{
899 struct viu_fh *fh = priv;
900
901 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
902 return -EINVAL;
903 if (fh->type != i)
904 return -EINVAL;
905
Anatolij Gustschinc3353332010-12-17 06:40:50 -0300906 viu_stop_dma(fh->dev);
907
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300908 return videobuf_streamoff(&fh->vb_vidq);
909}
910
911#define decoder_call(viu, o, f, args...) \
912 v4l2_subdev_call(viu->decoder, o, f, ##args)
913
Anatolij Gustschin50155c22010-12-22 17:31:59 -0300914static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
915{
916 struct viu_fh *fh = priv;
917
918 decoder_call(fh->dev, video, querystd, std_id);
919 return 0;
920}
921
Hans Verkuil314527a2013-03-15 06:10:40 -0300922static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300923{
924 struct viu_fh *fh = priv;
925
Hans Verkuil314527a2013-03-15 06:10:40 -0300926 fh->dev->std = id;
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300927 decoder_call(fh->dev, video, s_std, id);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300928 return 0;
929}
930
Anatolij Gustschin50155c22010-12-22 17:31:59 -0300931static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
932{
933 struct viu_fh *fh = priv;
934
935 *std_id = fh->dev->std;
936 return 0;
937}
938
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300939/* only one input in this driver */
940static int vidioc_enum_input(struct file *file, void *priv,
941 struct v4l2_input *inp)
942{
943 struct viu_fh *fh = priv;
944
945 if (inp->index != 0)
946 return -EINVAL;
947
948 inp->type = V4L2_INPUT_TYPE_CAMERA;
949 inp->std = fh->dev->vdev->tvnorms;
950 strcpy(inp->name, "Camera");
951 return 0;
952}
953
954static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
955{
956 *i = 0;
957 return 0;
958}
959
960static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
961{
962 struct viu_fh *fh = priv;
963
964 if (i > 1)
965 return -EINVAL;
966
967 decoder_call(fh->dev, video, s_routing, i, 0, 0);
968 return 0;
969}
970
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300971inline void viu_activate_next_buf(struct viu_dev *dev,
972 struct viu_dmaqueue *viuq)
973{
974 struct viu_dmaqueue *vidq = viuq;
975 struct viu_buf *buf;
976
977 /* launch another DMA operation for an active/queued buffer */
978 if (!list_empty(&vidq->active)) {
979 buf = list_entry(vidq->active.next, struct viu_buf,
980 vb.queue);
981 dprintk(1, "start another queued buffer: 0x%p\n", buf);
982 buffer_activate(dev, buf);
983 } else if (!list_empty(&vidq->queued)) {
984 buf = list_entry(vidq->queued.next, struct viu_buf,
985 vb.queue);
986 list_del(&buf->vb.queue);
987
988 dprintk(1, "start another queued buffer: 0x%p\n", buf);
989 list_add_tail(&buf->vb.queue, &vidq->active);
990 buf->vb.state = VIDEOBUF_ACTIVE;
991 buffer_activate(dev, buf);
992 }
993}
994
995inline void viu_default_settings(struct viu_reg *viu_reg)
996{
997 struct viu_reg *vr = viu_reg;
998
999 out_be32(&vr->luminance, 0x9512A254);
1000 out_be32(&vr->chroma_r, 0x03310000);
1001 out_be32(&vr->chroma_g, 0x06600F38);
1002 out_be32(&vr->chroma_b, 0x00000409);
1003 out_be32(&vr->alpha, 0x000000ff);
1004 out_be32(&vr->req_alarm, 0x00000090);
1005 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1006 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1007}
1008
1009static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1010{
1011 struct viu_reg *vr = dev->vr;
1012
1013 if (status & INT_DMA_END_STATUS)
1014 dev->dma_done = 1;
1015
1016 if (status & INT_FIELD_STATUS) {
1017 if (dev->dma_done) {
1018 u32 addr = reg_val.field_base_addr;
1019
1020 dev->dma_done = 0;
1021 if (status & FIELD_NO)
1022 addr += reg_val.dma_inc;
1023
1024 out_be32(&vr->field_base_addr, addr);
1025 out_be32(&vr->dma_inc, reg_val.dma_inc);
1026 out_be32(&vr->status_cfg,
1027 (status & 0xffc0ffff) |
1028 (status & INT_ALL_STATUS) |
1029 reg_val.status_cfg);
1030 } else if (status & INT_VSYNC_STATUS) {
1031 out_be32(&vr->status_cfg,
1032 (status & 0xffc0ffff) |
1033 (status & INT_ALL_STATUS) |
1034 reg_val.status_cfg);
1035 }
1036 }
1037}
1038
1039static void viu_capture_intr(struct viu_dev *dev, u32 status)
1040{
1041 struct viu_dmaqueue *vidq = &dev->vidq;
1042 struct viu_reg *vr = dev->vr;
1043 struct viu_buf *buf;
1044 int field_num;
1045 int need_two;
1046 int dma_done = 0;
1047
1048 field_num = status & FIELD_NO;
1049 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1050
1051 if (status & INT_DMA_END_STATUS) {
1052 dma_done = 1;
1053 if (((field_num == 0) && (dev->field == 0)) ||
1054 (field_num && (dev->field == 1)))
1055 dev->field++;
1056 }
1057
1058 if (status & INT_FIELD_STATUS) {
1059 dprintk(1, "irq: field %d, done %d\n",
1060 !!field_num, dma_done);
1061 if (unlikely(dev->first)) {
1062 if (field_num == 0) {
1063 dev->first = 0;
1064 dprintk(1, "activate first buf\n");
1065 viu_activate_next_buf(dev, vidq);
1066 } else
1067 dprintk(1, "wait field 0\n");
1068 return;
1069 }
1070
1071 /* setup buffer address for next dma operation */
1072 if (!list_empty(&vidq->active)) {
1073 u32 addr = reg_val.field_base_addr;
1074
1075 if (field_num && need_two) {
1076 addr += reg_val.dma_inc;
1077 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1078 (unsigned long)addr, dev->field);
1079 }
1080 out_be32(&vr->field_base_addr, addr);
1081 out_be32(&vr->dma_inc, reg_val.dma_inc);
1082 out_be32(&vr->status_cfg,
1083 (status & 0xffc0ffff) |
1084 (status & INT_ALL_STATUS) |
1085 reg_val.status_cfg);
1086 return;
1087 }
1088 }
1089
1090 if (dma_done && field_num && (dev->field == 2)) {
1091 dev->field = 0;
1092 buf = list_entry(vidq->active.next,
1093 struct viu_buf, vb.queue);
1094 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1095 buf, buf->vb.i,
1096 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1097 (unsigned long)in_be32(&vr->field_base_addr));
1098
1099 if (waitqueue_active(&buf->vb.done)) {
1100 list_del(&buf->vb.queue);
Sakari Ailus8e6057b2012-09-15 15:14:42 -03001101 v4l2_get_timestamp(&buf->vb.ts);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001102 buf->vb.state = VIDEOBUF_DONE;
1103 buf->vb.field_count++;
1104 wake_up(&buf->vb.done);
1105 }
1106 /* activate next dma buffer */
1107 viu_activate_next_buf(dev, vidq);
1108 }
1109}
1110
1111static irqreturn_t viu_intr(int irq, void *dev_id)
1112{
1113 struct viu_dev *dev = (struct viu_dev *)dev_id;
1114 struct viu_reg *vr = dev->vr;
1115 u32 status;
1116 u32 error;
1117
1118 status = in_be32(&vr->status_cfg);
1119
1120 if (status & INT_ERROR_STATUS) {
1121 dev->irqs.error_irq++;
1122 error = status & ERR_MASK;
1123 if (error)
1124 dprintk(1, "Err: error(%d), times:%d!\n",
1125 error >> 4, dev->irqs.error_irq);
1126 /* Clear interrupt error bit and error flags */
1127 out_be32(&vr->status_cfg,
1128 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1129 }
1130
1131 if (status & INT_DMA_END_STATUS) {
1132 dev->irqs.dma_end_irq++;
1133 dev->dma_done = 1;
1134 dprintk(2, "VIU DMA end interrupt times: %d\n",
1135 dev->irqs.dma_end_irq);
1136 }
1137
1138 if (status & INT_HSYNC_STATUS)
1139 dev->irqs.hsync_irq++;
1140
1141 if (status & INT_FIELD_STATUS) {
1142 dev->irqs.field_irq++;
1143 dprintk(2, "VIU field interrupt times: %d\n",
1144 dev->irqs.field_irq);
1145 }
1146
1147 if (status & INT_VSTART_STATUS)
1148 dev->irqs.vstart_irq++;
1149
1150 if (status & INT_VSYNC_STATUS) {
1151 dev->irqs.vsync_irq++;
1152 dprintk(2, "VIU vsync interrupt times: %d\n",
1153 dev->irqs.vsync_irq);
1154 }
1155
1156 /* clear all pending irqs */
1157 status = in_be32(&vr->status_cfg);
1158 out_be32(&vr->status_cfg,
1159 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1160
1161 if (dev->ovenable) {
1162 viu_overlay_intr(dev, status);
1163 return IRQ_HANDLED;
1164 }
1165
1166 /* Capture mode */
1167 viu_capture_intr(dev, status);
1168 return IRQ_HANDLED;
1169}
1170
1171/*
1172 * File operations for the device
1173 */
1174static int viu_open(struct file *file)
1175{
1176 struct video_device *vdev = video_devdata(file);
1177 struct viu_dev *dev = video_get_drvdata(vdev);
1178 struct viu_fh *fh;
1179 struct viu_reg *vr;
1180 int minor = vdev->minor;
1181 u32 status_cfg;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001182
1183 dprintk(1, "viu: open (minor=%d)\n", minor);
1184
1185 dev->users++;
1186 if (dev->users > 1) {
1187 dev->users--;
1188 return -EBUSY;
1189 }
1190
1191 vr = dev->vr;
1192
1193 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1194 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1195
Hans Verkuil87cb0272012-06-23 08:46:43 -03001196 if (mutex_lock_interruptible(&dev->lock)) {
1197 dev->users--;
1198 return -ERESTARTSYS;
1199 }
1200
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001201 /* allocate and initialize per filehandle data */
1202 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1203 if (!fh) {
1204 dev->users--;
Hans Verkuil87cb0272012-06-23 08:46:43 -03001205 mutex_unlock(&dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001206 return -ENOMEM;
1207 }
1208
1209 file->private_data = fh;
1210 fh->dev = dev;
1211
1212 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1213 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1214 fh->width = norm_maxw();
1215 fh->height = norm_maxh();
1216 dev->crop_current.width = fh->width;
1217 dev->crop_current.height = fh->height;
1218
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001219 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1220 (unsigned long)fh, (unsigned long)dev,
1221 (unsigned long)&dev->vidq);
1222 dprintk(1, "Open: list_empty queued=%d\n",
1223 list_empty(&dev->vidq.queued));
1224 dprintk(1, "Open: list_empty active=%d\n",
1225 list_empty(&dev->vidq.active));
1226
1227 viu_default_settings(vr);
1228
1229 status_cfg = in_be32(&vr->status_cfg);
1230 out_be32(&vr->status_cfg,
1231 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1232 INT_FIELD_EN | INT_VSTART_EN |
1233 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1234
1235 status_cfg = in_be32(&vr->status_cfg);
1236 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1237
1238 spin_lock_init(&fh->vbq_lock);
1239 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1240 dev->dev, &fh->vbq_lock,
1241 fh->type, V4L2_FIELD_INTERLACED,
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001242 sizeof(struct viu_buf), fh,
1243 &fh->dev->lock);
Hans Verkuil87cb0272012-06-23 08:46:43 -03001244 mutex_unlock(&dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001245 return 0;
1246}
1247
1248static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1249 loff_t *ppos)
1250{
1251 struct viu_fh *fh = file->private_data;
1252 struct viu_dev *dev = fh->dev;
1253 int ret = 0;
1254
1255 dprintk(2, "%s\n", __func__);
1256 if (dev->ovenable)
1257 dev->ovenable = 0;
1258
1259 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Hans Verkuil87cb0272012-06-23 08:46:43 -03001260 if (mutex_lock_interruptible(&dev->lock))
1261 return -ERESTARTSYS;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001262 viu_start_dma(dev);
1263 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1264 ppos, 0, file->f_flags & O_NONBLOCK);
Hans Verkuil87cb0272012-06-23 08:46:43 -03001265 mutex_unlock(&dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001266 return ret;
1267 }
1268 return 0;
1269}
1270
1271static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1272{
1273 struct viu_fh *fh = file->private_data;
1274 struct videobuf_queue *q = &fh->vb_vidq;
Hans Verkuil87cb0272012-06-23 08:46:43 -03001275 struct viu_dev *dev = fh->dev;
1276 unsigned int res;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001277
1278 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1279 return POLLERR;
1280
Hans Verkuil87cb0272012-06-23 08:46:43 -03001281 mutex_lock(&dev->lock);
1282 res = videobuf_poll_stream(file, q, wait);
1283 mutex_unlock(&dev->lock);
1284 return res;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001285}
1286
1287static int viu_release(struct file *file)
1288{
1289 struct viu_fh *fh = file->private_data;
1290 struct viu_dev *dev = fh->dev;
1291 int minor = video_devdata(file)->minor;
1292
Hans Verkuil87cb0272012-06-23 08:46:43 -03001293 mutex_lock(&dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001294 viu_stop_dma(dev);
1295 videobuf_stop(&fh->vb_vidq);
Anatolij Gustschinc3353332010-12-17 06:40:50 -03001296 videobuf_mmap_free(&fh->vb_vidq);
Hans Verkuil87cb0272012-06-23 08:46:43 -03001297 mutex_unlock(&dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001298
1299 kfree(fh);
1300
1301 dev->users--;
1302 dprintk(1, "close (minor=%d, users=%d)\n",
1303 minor, dev->users);
1304 return 0;
1305}
1306
1307void viu_reset(struct viu_reg *reg)
1308{
1309 out_be32(&reg->status_cfg, 0);
1310 out_be32(&reg->luminance, 0x9512a254);
1311 out_be32(&reg->chroma_r, 0x03310000);
1312 out_be32(&reg->chroma_g, 0x06600f38);
1313 out_be32(&reg->chroma_b, 0x00000409);
1314 out_be32(&reg->field_base_addr, 0);
1315 out_be32(&reg->dma_inc, 0);
1316 out_be32(&reg->picture_count, 0x01e002d0);
1317 out_be32(&reg->req_alarm, 0x00000090);
1318 out_be32(&reg->alpha, 0x000000ff);
1319}
1320
1321static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1322{
1323 struct viu_fh *fh = file->private_data;
Hans Verkuil87cb0272012-06-23 08:46:43 -03001324 struct viu_dev *dev = fh->dev;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001325 int ret;
1326
1327 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1328
Hans Verkuil87cb0272012-06-23 08:46:43 -03001329 if (mutex_lock_interruptible(&dev->lock))
1330 return -ERESTARTSYS;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001331 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
Hans Verkuil87cb0272012-06-23 08:46:43 -03001332 mutex_unlock(&dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001333
1334 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1335 (unsigned long)vma->vm_start,
1336 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1337 ret);
1338
1339 return ret;
1340}
1341
1342static struct v4l2_file_operations viu_fops = {
1343 .owner = THIS_MODULE,
1344 .open = viu_open,
1345 .release = viu_release,
1346 .read = viu_read,
1347 .poll = viu_poll,
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001348 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001349 .mmap = viu_mmap,
1350};
1351
1352static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1353 .vidioc_querycap = vidioc_querycap,
1354 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1355 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1356 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1357 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1358 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1359 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1360 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1361 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
Anatolij Gustschin791ae692011-05-04 17:19:28 -03001362 .vidioc_overlay = vidioc_overlay,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001363 .vidioc_g_fbuf = vidioc_g_fbuf,
1364 .vidioc_s_fbuf = vidioc_s_fbuf,
1365 .vidioc_reqbufs = vidioc_reqbufs,
1366 .vidioc_querybuf = vidioc_querybuf,
1367 .vidioc_qbuf = vidioc_qbuf,
1368 .vidioc_dqbuf = vidioc_dqbuf,
Anatolij Gustschin50155c22010-12-22 17:31:59 -03001369 .vidioc_g_std = vidioc_g_std,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001370 .vidioc_s_std = vidioc_s_std,
Anatolij Gustschin50155c22010-12-22 17:31:59 -03001371 .vidioc_querystd = vidioc_querystd,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001372 .vidioc_enum_input = vidioc_enum_input,
1373 .vidioc_g_input = vidioc_g_input,
1374 .vidioc_s_input = vidioc_s_input,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001375 .vidioc_streamon = vidioc_streamon,
1376 .vidioc_streamoff = vidioc_streamoff,
1377};
1378
1379static struct video_device viu_template = {
1380 .name = "FSL viu",
1381 .fops = &viu_fops,
1382 .minor = -1,
1383 .ioctl_ops = &viu_ioctl_ops,
1384 .release = video_device_release,
1385
1386 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001387};
1388
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001389static int viu_of_probe(struct platform_device *op)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001390{
1391 struct viu_dev *viu_dev;
1392 struct video_device *vdev;
1393 struct resource r;
1394 struct viu_reg __iomem *viu_regs;
1395 struct i2c_adapter *ad;
1396 int ret, viu_irq;
Gerhard Sittiga5d7a6d2013-07-01 18:54:41 +02001397 struct clk *clk;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001398
1399 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1400 if (ret) {
1401 dev_err(&op->dev, "Can't parse device node resource\n");
1402 return -ENODEV;
1403 }
1404
1405 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1406 if (viu_irq == NO_IRQ) {
1407 dev_err(&op->dev, "Error while mapping the irq\n");
1408 return -EINVAL;
1409 }
1410
1411 /* request mem region */
1412 if (!devm_request_mem_region(&op->dev, r.start,
1413 sizeof(struct viu_reg), DRV_NAME)) {
1414 dev_err(&op->dev, "Error while requesting mem region\n");
1415 ret = -EBUSY;
1416 goto err;
1417 }
1418
1419 /* remap registers */
1420 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1421 if (!viu_regs) {
1422 dev_err(&op->dev, "Can't map register set\n");
1423 ret = -ENOMEM;
1424 goto err;
1425 }
1426
1427 /* Prepare our private structure */
1428 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1429 if (!viu_dev) {
1430 dev_err(&op->dev, "Can't allocate private structure\n");
1431 ret = -ENOMEM;
1432 goto err;
1433 }
1434
1435 viu_dev->vr = viu_regs;
1436 viu_dev->irq = viu_irq;
1437 viu_dev->dev = &op->dev;
1438
1439 /* init video dma queues */
1440 INIT_LIST_HEAD(&viu_dev->vidq.active);
1441 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1442
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001443 snprintf(viu_dev->v4l2_dev.name,
1444 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1445 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1446 if (ret < 0) {
1447 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1448 goto err;
1449 }
1450
1451 ad = i2c_get_adapter(0);
Hans Verkuil1fea3d62015-07-20 10:09:28 -03001452
1453 v4l2_ctrl_handler_init(&viu_dev->hdl, 5);
1454 if (viu_dev->hdl.error) {
1455 ret = viu_dev->hdl.error;
1456 dev_err(&op->dev, "couldn't register control\n");
1457 goto err_vdev;
1458 }
1459 /* This control handler will inherit the control(s) from the
1460 sub-device(s). */
1461 viu_dev->v4l2_dev.ctrl_handler = &viu_dev->hdl;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001462 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001463 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001464
1465 viu_dev->vidq.timeout.function = viu_vid_timeout;
1466 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1467 init_timer(&viu_dev->vidq.timeout);
Hans Verkuil28221f82013-06-03 05:36:43 -03001468 viu_dev->std = V4L2_STD_NTSC_M;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001469 viu_dev->first = 1;
1470
1471 /* Allocate memory for video device */
1472 vdev = video_device_alloc();
1473 if (vdev == NULL) {
1474 ret = -ENOMEM;
1475 goto err_vdev;
1476 }
1477
1478 memcpy(vdev, &viu_template, sizeof(viu_template));
1479
1480 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1481
1482 viu_dev->vdev = vdev;
1483
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001484 /* initialize locks */
1485 mutex_init(&viu_dev->lock);
1486 viu_dev->vdev->lock = &viu_dev->lock;
1487 spin_lock_init(&viu_dev->slock);
1488
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001489 video_set_drvdata(viu_dev->vdev, viu_dev);
1490
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001491 mutex_lock(&viu_dev->lock);
1492
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001493 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1494 if (ret < 0) {
1495 video_device_release(viu_dev->vdev);
1496 goto err_vdev;
1497 }
1498
1499 /* enable VIU clock */
Gerhard Sittig17552182013-11-30 23:51:33 +01001500 clk = devm_clk_get(&op->dev, "ipg");
Gerhard Sittiga5d7a6d2013-07-01 18:54:41 +02001501 if (IS_ERR(clk)) {
1502 dev_err(&op->dev, "failed to lookup the clock!\n");
1503 ret = PTR_ERR(clk);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001504 goto err_clk;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001505 }
Gerhard Sittiga5d7a6d2013-07-01 18:54:41 +02001506 ret = clk_prepare_enable(clk);
1507 if (ret) {
1508 dev_err(&op->dev, "failed to enable the clock!\n");
1509 goto err_clk;
1510 }
1511 viu_dev->clk = clk;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001512
1513 /* reset VIU module */
1514 viu_reset(viu_dev->vr);
1515
1516 /* install interrupt handler */
1517 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1518 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1519 ret = -ENODEV;
1520 goto err_irq;
1521 }
1522
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001523 mutex_unlock(&viu_dev->lock);
1524
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001525 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1526 return ret;
1527
1528err_irq:
Gerhard Sittiga5d7a6d2013-07-01 18:54:41 +02001529 clk_disable_unprepare(viu_dev->clk);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001530err_clk:
1531 video_unregister_device(viu_dev->vdev);
1532err_vdev:
Hans Verkuil1fea3d62015-07-20 10:09:28 -03001533 v4l2_ctrl_handler_free(&viu_dev->hdl);
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001534 mutex_unlock(&viu_dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001535 i2c_put_adapter(ad);
1536 v4l2_device_unregister(&viu_dev->v4l2_dev);
1537err:
1538 irq_dispose_mapping(viu_irq);
1539 return ret;
1540}
1541
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001542static int viu_of_remove(struct platform_device *op)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001543{
1544 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1545 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1546 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1547 struct v4l2_subdev, list);
1548 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1549
1550 free_irq(dev->irq, (void *)dev);
1551 irq_dispose_mapping(dev->irq);
1552
Gerhard Sittiga5d7a6d2013-07-01 18:54:41 +02001553 clk_disable_unprepare(dev->clk);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001554
Hans Verkuil1fea3d62015-07-20 10:09:28 -03001555 v4l2_ctrl_handler_free(&dev->hdl);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001556 video_unregister_device(dev->vdev);
1557 i2c_put_adapter(client->adapter);
1558 v4l2_device_unregister(&dev->v4l2_dev);
1559 return 0;
1560}
1561
1562#ifdef CONFIG_PM
Grant Likely2dc11582010-08-06 09:25:50 -06001563static int viu_suspend(struct platform_device *op, pm_message_t state)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001564{
1565 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1566 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1567
1568 clk_disable(dev->clk);
1569 return 0;
1570}
1571
Grant Likely2dc11582010-08-06 09:25:50 -06001572static int viu_resume(struct platform_device *op)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001573{
1574 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1575 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1576
1577 clk_enable(dev->clk);
1578 return 0;
1579}
1580#endif
1581
1582/*
1583 * Initialization and module stuff
1584 */
Fabian Frederick7f099a72015-03-16 16:54:33 -03001585static const struct of_device_id mpc512x_viu_of_match[] = {
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001586 {
1587 .compatible = "fsl,mpc5121-viu",
1588 },
1589 {},
1590};
1591MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1592
Grant Likely1c48a5c2011-02-17 02:43:24 -07001593static struct platform_driver viu_of_platform_driver = {
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001594 .probe = viu_of_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001595 .remove = viu_of_remove,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001596#ifdef CONFIG_PM
1597 .suspend = viu_suspend,
1598 .resume = viu_resume,
1599#endif
1600 .driver = {
1601 .name = DRV_NAME,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001602 .of_match_table = mpc512x_viu_of_match,
1603 },
1604};
1605
Axel Lin1d6629b2012-01-10 03:21:49 -03001606module_platform_driver(viu_of_platform_driver);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001607
1608MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1609MODULE_AUTHOR("Hongjun Chen");
1610MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001611MODULE_VERSION(VIU_VERSION);