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