blob: 31e7a123d19a954f86808ec80ae8978fd044d3d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Colour AR M64278(VGA) driver for Video4Linux
3 *
4 * Copyright (C) 2003 Takeo Takahashi <takahashi.takeo@renesas.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * Some code is taken from AR driver sample program for M3T-M32700UT.
12 *
13 * AR driver sample (M32R SDK):
14 * Copyright (c) 2003 RENESAS TECHNOROGY CORPORATION
15 * AND RENESAS SOLUTIONS CORPORATION
16 * All Rights Reserved.
17 *
18 * 2003-09-01: Support w3cam by Takeo Takahashi
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/mm.h>
29#include <linux/sched.h>
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -030030#include <linux/version.h>
31#include <linux/videodev2.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030032#include <media/v4l2-common.h>
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -030033#include <media/v4l2-device.h>
Adrian Bunkdfb9aff2008-07-28 16:58:19 -030034#include <media/v4l2-ioctl.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38#include <asm/m32r.h>
39#include <asm/io.h>
40#include <asm/dma.h>
41#include <asm/byteorder.h>
42
43#if 0
Hans Verkuil88f44232010-03-22 04:44:29 -030044#define DEBUG(n, args...) printk(KERN_INFO args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define CHECK_LOST 1
46#else
47#define DEBUG(n, args...)
48#define CHECK_LOST 0
49#endif
50
51/*
52 * USE_INT is always 0, interrupt mode is not available
53 * on linux due to lack of speed
54 */
55#define USE_INT 0 /* Don't modify */
56
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -030057#define VERSION "0.04"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define ar_inl(addr) inl((unsigned long)(addr))
Hans Verkuil88f44232010-03-22 04:44:29 -030060#define ar_outl(val, addr) outl((unsigned long)(val), (unsigned long)(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62extern struct cpuinfo_m32r boot_cpu_data;
63
64/*
65 * CCD pixel size
66 * Note that M32700UT does not support CIF mode, but QVGA is
67 * supported by M32700UT hardware using VGA mode of AR LSI.
68 *
69 * Supported: VGA (Normal mode, Interlace mode)
70 * QVGA (Always Interlace mode of VGA)
71 *
72 */
73#define AR_WIDTH_VGA 640
74#define AR_HEIGHT_VGA 480
75#define AR_WIDTH_QVGA 320
76#define AR_HEIGHT_QVGA 240
77#define MIN_AR_WIDTH AR_WIDTH_QVGA
78#define MIN_AR_HEIGHT AR_HEIGHT_QVGA
79#define MAX_AR_WIDTH AR_WIDTH_VGA
80#define MAX_AR_HEIGHT AR_HEIGHT_VGA
81
82/* bits & bytes per pixel */
83#define AR_BITS_PER_PIXEL 16
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -030084#define AR_BYTES_PER_PIXEL (AR_BITS_PER_PIXEL / 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* line buffer size */
87#define AR_LINE_BYTES_VGA (AR_WIDTH_VGA * AR_BYTES_PER_PIXEL)
88#define AR_LINE_BYTES_QVGA (AR_WIDTH_QVGA * AR_BYTES_PER_PIXEL)
89#define MAX_AR_LINE_BYTES AR_LINE_BYTES_VGA
90
91/* frame size & type */
92#define AR_FRAME_BYTES_VGA \
93 (AR_WIDTH_VGA * AR_HEIGHT_VGA * AR_BYTES_PER_PIXEL)
94#define AR_FRAME_BYTES_QVGA \
95 (AR_WIDTH_QVGA * AR_HEIGHT_QVGA * AR_BYTES_PER_PIXEL)
96#define MAX_AR_FRAME_BYTES \
97 (MAX_AR_WIDTH * MAX_AR_HEIGHT * AR_BYTES_PER_PIXEL)
98
99#define AR_MAX_FRAME 15
100
101/* capture size */
102#define AR_SIZE_VGA 0
103#define AR_SIZE_QVGA 1
104
105/* capture mode */
106#define AR_MODE_INTERLACE 0
107#define AR_MODE_NORMAL 1
108
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300109struct ar {
110 struct v4l2_device v4l2_dev;
111 struct video_device vdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned int start_capture; /* duaring capture in INT. mode. */
113#if USE_INT
114 unsigned char *line_buff; /* DMA line buffer */
115#endif
116 unsigned char *frame[MAX_AR_HEIGHT]; /* frame data */
117 short size; /* capture size */
118 short mode; /* capture mode */
119 int width, height;
120 int frame_bytes, line_bytes;
121 wait_queue_head_t wait;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200122 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300125static struct ar ardev;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int video_nr = -1; /* video device number (first free) */
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300128static unsigned char yuv[MAX_AR_FRAME_BYTES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* module parameters */
131/* default frequency */
132#define DEFAULT_FREQ 50 /* 50 or 75 (MHz) is available as BCLK */
133static int freq = DEFAULT_FREQ; /* BCLK: available 50 or 70 (MHz) */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300134static int vga; /* default mode(0:QVGA mode, other:VGA mode) */
135static int vga_interlace; /* 0 is normal mode for, else interlace mode */
Eric Sesterhenn / snakebyte9b565eb2006-01-13 14:10:23 -0200136module_param(freq, int, 0);
137module_param(vga, int, 0);
138module_param(vga_interlace, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300140static void wait_for_vsync(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 while (ar_inl(ARVCR0) & ARVCR0_VDS) /* wait for VSYNC */
143 cpu_relax();
144 while (!(ar_inl(ARVCR0) & ARVCR0_VDS)) /* wait for VSYNC */
145 cpu_relax();
146}
147
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300148static void wait_acknowledge(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int i;
151
152 for (i = 0; i < 1000; i++)
153 cpu_relax();
154 while (ar_inl(PLDI2CSTS) & PLDI2CSTS_NOACK)
155 cpu_relax();
156}
157
158/*******************************************************************
159 * I2C functions
160 *******************************************************************/
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300161static void iic(int n, unsigned long addr, unsigned long data1, unsigned long data2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 unsigned long data3)
163{
164 int i;
165
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300166 /* Slave Address */
167 ar_outl(addr, PLDI2CDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 wait_for_vsync();
169
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300170 /* Start */
171 ar_outl(1, PLDI2CCND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 wait_acknowledge();
173
174 /* Transfer data 1 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300175 ar_outl(data1, PLDI2CDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 wait_for_vsync();
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300177 ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 wait_acknowledge();
179
180 /* Transfer data 2 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300181 ar_outl(data2, PLDI2CDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 wait_for_vsync();
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300183 ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 wait_acknowledge();
185
186 if (n == 3) {
187 /* Transfer data 3 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300188 ar_outl(data3, PLDI2CDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 wait_for_vsync();
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300190 ar_outl(PLDI2CSTEN_STEN, PLDI2CSTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 wait_acknowledge();
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300194 /* Stop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 for (i = 0; i < 100; i++)
196 cpu_relax();
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300197 ar_outl(2, PLDI2CCND);
198 ar_outl(2, PLDI2CCND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 while (ar_inl(PLDI2CSTS) & PLDI2CSTS_BB)
201 cpu_relax();
202}
203
204
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300205static void init_iic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 DEBUG(1, "init_iic:\n");
208
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * ICU Setting (iic)
211 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300212 /* I2C Setting */
213 ar_outl(0x0, PLDI2CCR); /* I2CCR Disable */
214 ar_outl(0x0300, PLDI2CMOD); /* I2CMOD ACK/8b-data/7b-addr/auto */
215 ar_outl(0x1, PLDI2CACK); /* I2CACK ACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Trent Piepho657de3c2006-06-20 00:30:57 -0300217 /* I2C CLK */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300218 /* 50MH-100k */
Hans Verkuil88f44232010-03-22 04:44:29 -0300219 if (freq == 75)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300220 ar_outl(369, PLDI2CFREQ); /* BCLK = 75MHz */
Hans Verkuil88f44232010-03-22 04:44:29 -0300221 else if (freq == 50)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ar_outl(244, PLDI2CFREQ); /* BCLK = 50MHz */
Hans Verkuil88f44232010-03-22 04:44:29 -0300223 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 ar_outl(244, PLDI2CFREQ); /* default: BCLK = 50MHz */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300225 ar_outl(0x1, PLDI2CCR); /* I2CCR Enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/**************************************************************************
229 *
230 * Video4Linux Interface functions
231 *
232 **************************************************************************/
233
234static inline void disable_dma(void)
235{
236 ar_outl(0x8000, M32R_DMAEN_PORTL); /* disable DMA0 */
237}
238
239static inline void enable_dma(void)
240{
241 ar_outl(0x8080, M32R_DMAEN_PORTL); /* enable DMA0 */
242}
243
244static inline void clear_dma_status(void)
245{
246 ar_outl(0x8000, M32R_DMAEDET_PORTL); /* clear status */
247}
248
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300249static void wait_for_vertical_sync(struct ar *ar, int exp_line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251#if CHECK_LOST
252 int tmout = 10000; /* FIXME */
253 int l;
254
255 /*
256 * check HCOUNT because we cannot check vertical sync.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 for (; tmout >= 0; tmout--) {
259 l = ar_inl(ARVHCOUNT);
260 if (l == exp_line)
261 break;
262 }
263 if (tmout < 0)
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300264 v4l2_err(&ar->v4l2_dev, "lost %d -> %d\n", exp_line, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#else
266 while (ar_inl(ARVHCOUNT) != exp_line)
267 cpu_relax();
268#endif
269}
270
271static ssize_t ar_read(struct file *file, char *buf, size_t count, loff_t *ppos)
272{
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300273 struct ar *ar = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 long ret = ar->frame_bytes; /* return read bytes */
275 unsigned long arvcr1 = 0;
276 unsigned long flags;
277 unsigned char *p;
278 int h, w;
279 unsigned char *py, *pu, *pv;
Hans Verkuil88f44232010-03-22 04:44:29 -0300280#if !USE_INT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int l;
282#endif
283
284 DEBUG(1, "ar_read()\n");
285
286 if (ar->size == AR_SIZE_QVGA)
287 arvcr1 |= ARVCR1_QVGA;
288 if (ar->mode == AR_MODE_NORMAL)
289 arvcr1 |= ARVCR1_NORMAL;
290
Ingo Molnar3593cab2006-02-07 06:49:14 -0200291 mutex_lock(&ar->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293#if USE_INT
294 local_irq_save(flags);
295 disable_dma();
296 ar_outl(0xa1871300, M32R_DMA0CR0_PORTL);
297 ar_outl(0x01000000, M32R_DMA0CR1_PORTL);
298
299 /* set AR FIFO address as source(BSEL5) */
300 ar_outl(ARDATA32, M32R_DMA0CSA_PORTL);
301 ar_outl(ARDATA32, M32R_DMA0RSA_PORTL);
302 ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL); /* destination addr. */
303 ar_outl(ar->line_buff, M32R_DMA0RDA_PORTL); /* reload address */
304 ar_outl(ar->line_bytes, M32R_DMA0CBCUT_PORTL); /* byte count (bytes) */
305 ar_outl(ar->line_bytes, M32R_DMA0RBCUT_PORTL); /* reload count (bytes) */
306
307 /*
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300308 * Okay, kick AR LSI to invoke an interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
310 ar->start_capture = 0;
311 ar_outl(arvcr1 | ARVCR1_HIEN, ARVCR1);
312 local_irq_restore(flags);
313 /* .... AR interrupts .... */
314 interruptible_sleep_on(&ar->wait);
315 if (signal_pending(current)) {
Hans Verkuil88f44232010-03-22 04:44:29 -0300316 printk(KERN_ERR "arv: interrupted while get frame data.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ret = -EINTR;
318 goto out_up;
319 }
320#else /* ! USE_INT */
321 /* polling */
322 ar_outl(arvcr1, ARVCR1);
323 disable_dma();
324 ar_outl(0x8000, M32R_DMAEDET_PORTL);
325 ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
326 ar_outl(0x01000000, M32R_DMA0CR1_PORTL);
327 ar_outl(ARDATA32, M32R_DMA0CSA_PORTL);
328 ar_outl(ARDATA32, M32R_DMA0RSA_PORTL);
329 ar_outl(ar->line_bytes, M32R_DMA0CBCUT_PORTL);
330 ar_outl(ar->line_bytes, M32R_DMA0RBCUT_PORTL);
331
332 local_irq_save(flags);
333 while (ar_inl(ARVHCOUNT) != 0) /* wait for 0 */
334 cpu_relax();
335 if (ar->mode == AR_MODE_INTERLACE && ar->size == AR_SIZE_VGA) {
336 for (h = 0; h < ar->height; h++) {
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300337 wait_for_vertical_sync(ar, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (h < (AR_HEIGHT_VGA/2))
339 l = h << 1;
340 else
341 l = (((h - (AR_HEIGHT_VGA/2)) << 1) + 1);
342 ar_outl(virt_to_phys(ar->frame[l]), M32R_DMA0CDA_PORTL);
343 enable_dma();
344 while (!(ar_inl(M32R_DMAEDET_PORTL) & 0x8000))
345 cpu_relax();
346 disable_dma();
347 clear_dma_status();
348 ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
349 }
350 } else {
351 for (h = 0; h < ar->height; h++) {
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300352 wait_for_vertical_sync(ar, h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ar_outl(virt_to_phys(ar->frame[h]), M32R_DMA0CDA_PORTL);
354 enable_dma();
355 while (!(ar_inl(M32R_DMAEDET_PORTL) & 0x8000))
356 cpu_relax();
357 disable_dma();
358 clear_dma_status();
359 ar_outl(0xa0861300, M32R_DMA0CR0_PORTL);
360 }
361 }
362 local_irq_restore(flags);
363#endif /* ! USE_INT */
364
365 /*
366 * convert YUV422 to YUV422P
367 * +--------------------+
368 * | Y0,Y1,... |
369 * | ..............Yn |
370 * +--------------------+
371 * | U0,U1,........Un |
372 * +--------------------+
373 * | V0,V1,........Vn |
374 * +--------------------+
375 */
376 py = yuv;
377 pu = py + (ar->frame_bytes / 2);
378 pv = pu + (ar->frame_bytes / 4);
379 for (h = 0; h < ar->height; h++) {
380 p = ar->frame[h];
381 for (w = 0; w < ar->line_bytes; w += 4) {
382 *py++ = *p++;
383 *pu++ = *p++;
384 *py++ = *p++;
385 *pv++ = *p++;
386 }
387 }
388 if (copy_to_user(buf, yuv, ar->frame_bytes)) {
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300389 v4l2_err(&ar->v4l2_dev, "failed while copy_to_user yuv.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 ret = -EFAULT;
391 goto out_up;
392 }
393 DEBUG(1, "ret = %d\n", ret);
394out_up:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200395 mutex_unlock(&ar->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return ret;
397}
398
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300399static int ar_querycap(struct file *file, void *priv,
400 struct v4l2_capability *vcap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300402 struct ar *ar = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300404 strlcpy(vcap->driver, ar->vdev.name, sizeof(vcap->driver));
405 strlcpy(vcap->card, "Colour AR VGA", sizeof(vcap->card));
406 strlcpy(vcap->bus_info, "Platform", sizeof(vcap->bus_info));
407 vcap->version = KERNEL_VERSION(0, 0, 4);
408 vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
410}
411
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300412static int ar_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300414 if (vin->index > 0)
415 return -EINVAL;
416 strlcpy(vin->name, "Camera", sizeof(vin->name));
417 vin->type = V4L2_INPUT_TYPE_CAMERA;
418 vin->audioset = 0;
419 vin->tuner = 0;
420 vin->std = V4L2_STD_ALL;
421 vin->status = 0;
422 return 0;
423}
424
425static int ar_g_input(struct file *file, void *fh, unsigned int *inp)
426{
427 *inp = 0;
428 return 0;
429}
430
431static int ar_s_input(struct file *file, void *fh, unsigned int inp)
432{
433 return inp ? -EINVAL : 0;
434}
435
436static int ar_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
437{
438 struct ar *ar = video_drvdata(file);
439 struct v4l2_pix_format *pix = &fmt->fmt.pix;
440
441 pix->width = ar->width;
442 pix->height = ar->height;
443 pix->pixelformat = V4L2_PIX_FMT_YUV422P;
444 pix->field = (ar->mode == AR_MODE_NORMAL) ? V4L2_FIELD_NONE : V4L2_FIELD_INTERLACED;
445 pix->bytesperline = ar->width;
446 pix->sizeimage = 2 * ar->width * ar->height;
447 /* Just a guess */
448 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
449 return 0;
450}
451
452static int ar_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
453{
454 struct ar *ar = video_drvdata(file);
455 struct v4l2_pix_format *pix = &fmt->fmt.pix;
456
457 if (pix->height <= AR_HEIGHT_QVGA || pix->width <= AR_WIDTH_QVGA) {
458 pix->height = AR_HEIGHT_QVGA;
459 pix->width = AR_WIDTH_QVGA;
460 pix->field = V4L2_FIELD_INTERLACED;
461 } else {
462 pix->height = AR_HEIGHT_VGA;
463 pix->width = AR_WIDTH_VGA;
464 pix->field = vga_interlace ? V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE;
465 }
466 pix->pixelformat = V4L2_PIX_FMT_YUV422P;
467 pix->bytesperline = ar->width;
468 pix->sizeimage = 2 * ar->width * ar->height;
469 /* Just a guess */
470 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
471 return 0;
472}
473
474static int ar_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
475{
476 struct ar *ar = video_drvdata(file);
477 struct v4l2_pix_format *pix = &fmt->fmt.pix;
478 int ret = ar_try_fmt_vid_cap(file, fh, fmt);
479
480 if (ret)
481 return ret;
482 mutex_lock(&ar->lock);
483 ar->width = pix->width;
484 ar->height = pix->height;
485 if (ar->width == AR_WIDTH_VGA) {
486 ar->size = AR_SIZE_VGA;
487 ar->frame_bytes = AR_FRAME_BYTES_VGA;
488 ar->line_bytes = AR_LINE_BYTES_VGA;
489 if (vga_interlace)
490 ar->mode = AR_MODE_INTERLACE;
491 else
492 ar->mode = AR_MODE_NORMAL;
493 } else {
494 ar->size = AR_SIZE_QVGA;
495 ar->frame_bytes = AR_FRAME_BYTES_QVGA;
496 ar->line_bytes = AR_LINE_BYTES_QVGA;
497 ar->mode = AR_MODE_INTERLACE;
498 }
499 /* Ok we figured out what to use from our wide choice */
500 mutex_unlock(&ar->lock);
501 return 0;
502}
503
504static int ar_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
505{
506 static struct v4l2_fmtdesc formats[] = {
507 { 0, 0, 0,
508 "YUV 4:2:2 Planar", V4L2_PIX_FMT_YUV422P,
509 { 0, 0, 0, 0 }
510 },
511 };
512 enum v4l2_buf_type type = fmt->type;
513
514 if (fmt->index > 0)
515 return -EINVAL;
516
517 *fmt = formats[fmt->index];
518 fmt->type = type;
519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522#if USE_INT
523/*
524 * Interrupt handler
525 */
David Howells7d12e782006-10-05 14:55:46 +0100526static void ar_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300528 struct ar *ar = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 unsigned int line_count;
530 unsigned int line_number;
531 unsigned int arvcr1;
532
533 line_count = ar_inl(ARVHCOUNT); /* line number */
534 if (ar->mode == AR_MODE_INTERLACE && ar->size == AR_SIZE_VGA) {
535 /* operations for interlace mode */
Hans Verkuil88f44232010-03-22 04:44:29 -0300536 if (line_count < (AR_HEIGHT_VGA / 2)) /* even line */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 line_number = (line_count << 1);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300538 else /* odd line */
539 line_number =
Hans Verkuil88f44232010-03-22 04:44:29 -0300540 (((line_count - (AR_HEIGHT_VGA / 2)) << 1) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 } else {
542 line_number = line_count;
543 }
544
545 if (line_number == 0) {
546 /*
547 * It is an interrupt for line 0.
548 * we have to start capture.
549 */
550 disable_dma();
551#if 0
552 ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL); /* needless? */
553#endif
554 memcpy(ar->frame[0], ar->line_buff, ar->line_bytes);
555#if 0
556 ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
557#endif
558 enable_dma();
559 ar->start_capture = 1; /* during capture */
560 return;
561 }
562
563 if (ar->start_capture == 1 && line_number <= (ar->height - 1)) {
564 disable_dma();
565 memcpy(ar->frame[line_number], ar->line_buff, ar->line_bytes);
566
567 /*
568 * if captured all line of a frame, disable AR interrupt
569 * and wake a process up.
570 */
571 if (line_number == (ar->height - 1)) { /* end of line */
572
573 ar->start_capture = 0;
574
575 /* disable AR interrupt request */
576 arvcr1 = ar_inl(ARVCR1);
577 arvcr1 &= ~ARVCR1_HIEN; /* clear int. flag */
578 ar_outl(arvcr1, ARVCR1); /* disable */
579 wake_up_interruptible(&ar->wait);
580 } else {
581#if 0
582 ar_outl(ar->line_buff, M32R_DMA0CDA_PORTL);
583 ar_outl(0xa1861300, M32R_DMA0CR0_PORTL);
584#endif
585 enable_dma();
586 }
587 }
588}
589#endif
590
591/*
592 * ar_initialize()
593 * ar_initialize() is called by video_register_device() and
594 * initializes AR LSI and peripherals.
595 *
596 * -1 is returned in all failures.
597 * 0 is returned in success.
598 *
599 */
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300600static int ar_initialize(struct ar *ar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unsigned long cr = 0;
Hans Verkuil88f44232010-03-22 04:44:29 -0300603 int i, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 DEBUG(1, "ar_initialize:\n");
606
607 /*
608 * initialize AR LSI
609 */
610 ar_outl(0, ARVCR0); /* assert reset of AR LSI */
611 for (i = 0; i < 0x18; i++) /* wait for over 10 cycles @ 27MHz */
612 cpu_relax();
613 ar_outl(ARVCR0_RST, ARVCR0); /* negate reset of AR LSI (enable) */
614 for (i = 0; i < 0x40d; i++) /* wait for over 420 cycles @ 27MHz */
615 cpu_relax();
616
617 /* AR uses INT3 of CPU as interrupt pin. */
618 ar_outl(ARINTSEL_INT3, ARINTSEL);
619
620 if (ar->size == AR_SIZE_QVGA)
621 cr |= ARVCR1_QVGA;
622 if (ar->mode == AR_MODE_NORMAL)
623 cr |= ARVCR1_NORMAL;
624 ar_outl(cr, ARVCR1);
625
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300626 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * Initialize IIC so that CPU can communicate with AR LSI,
628 * and send boot commands to AR LSI.
629 */
630 init_iic();
631
632 for (i = 0; i < 0x100000; i++) { /* > 0xa1d10, 56ms */
633 if ((ar_inl(ARVCR0) & ARVCR0_VDS)) { /* VSYNC */
634 found = 1;
635 break;
636 }
637 }
638
639 if (found == 0)
640 return -ENODEV;
641
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300642 v4l2_info(&ar->v4l2_dev, "Initializing ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Hans Verkuil88f44232010-03-22 04:44:29 -0300644 iic(2, 0x78, 0x11, 0x01, 0x00); /* start */
645 iic(3, 0x78, 0x12, 0x00, 0x06);
646 iic(3, 0x78, 0x12, 0x12, 0x30);
647 iic(3, 0x78, 0x12, 0x15, 0x58);
648 iic(3, 0x78, 0x12, 0x17, 0x30);
649 printk(KERN_CONT ".");
650 iic(3, 0x78, 0x12, 0x1a, 0x97);
651 iic(3, 0x78, 0x12, 0x1b, 0xff);
652 iic(3, 0x78, 0x12, 0x1c, 0xff);
653 iic(3, 0x78, 0x12, 0x26, 0x10);
654 iic(3, 0x78, 0x12, 0x27, 0x00);
655 printk(KERN_CONT ".");
656 iic(2, 0x78, 0x34, 0x02, 0x00);
657 iic(2, 0x78, 0x7a, 0x10, 0x00);
658 iic(2, 0x78, 0x80, 0x39, 0x00);
659 iic(2, 0x78, 0x81, 0xe6, 0x00);
660 iic(2, 0x78, 0x8d, 0x00, 0x00);
661 printk(KERN_CONT ".");
662 iic(2, 0x78, 0x8e, 0x0c, 0x00);
663 iic(2, 0x78, 0x8f, 0x00, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664#if 0
Hans Verkuil88f44232010-03-22 04:44:29 -0300665 iic(2, 0x78, 0x90, 0x00, 0x00); /* AWB on=1 off=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#endif
Hans Verkuil88f44232010-03-22 04:44:29 -0300667 iic(2, 0x78, 0x93, 0x01, 0x00);
668 iic(2, 0x78, 0x94, 0xcd, 0x00);
669 iic(2, 0x78, 0x95, 0x00, 0x00);
670 printk(KERN_CONT ".");
671 iic(2, 0x78, 0x96, 0xa0, 0x00);
672 iic(2, 0x78, 0x97, 0x00, 0x00);
673 iic(2, 0x78, 0x98, 0x60, 0x00);
674 iic(2, 0x78, 0x99, 0x01, 0x00);
675 iic(2, 0x78, 0x9a, 0x19, 0x00);
676 printk(KERN_CONT ".");
677 iic(2, 0x78, 0x9b, 0x02, 0x00);
678 iic(2, 0x78, 0x9c, 0xe8, 0x00);
679 iic(2, 0x78, 0x9d, 0x02, 0x00);
680 iic(2, 0x78, 0x9e, 0x2e, 0x00);
681 iic(2, 0x78, 0xb8, 0x78, 0x00);
682 iic(2, 0x78, 0xba, 0x05, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#if 0
Hans Verkuil88f44232010-03-22 04:44:29 -0300684 iic(2, 0x78, 0x83, 0x8c, 0x00); /* brightness */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#endif
Hans Verkuil88f44232010-03-22 04:44:29 -0300686 printk(KERN_CONT ".");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* color correction */
Hans Verkuil88f44232010-03-22 04:44:29 -0300689 iic(3, 0x78, 0x49, 0x00, 0x95); /* a */
690 iic(3, 0x78, 0x49, 0x01, 0x96); /* b */
691 iic(3, 0x78, 0x49, 0x03, 0x85); /* c */
692 iic(3, 0x78, 0x49, 0x04, 0x97); /* d */
693 iic(3, 0x78, 0x49, 0x02, 0x7e); /* e(Lo) */
694 iic(3, 0x78, 0x49, 0x05, 0xa4); /* f(Lo) */
695 iic(3, 0x78, 0x49, 0x06, 0x04); /* e(Hi) */
696 iic(3, 0x78, 0x49, 0x07, 0x04); /* e(Hi) */
697 iic(2, 0x78, 0x48, 0x01, 0x00); /* on=1 off=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Hans Verkuil88f44232010-03-22 04:44:29 -0300699 printk(KERN_CONT ".");
700 iic(2, 0x78, 0x11, 0x00, 0x00); /* end */
701 printk(KERN_CONT " done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
703}
704
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/****************************************************************************
707 *
708 * Video4Linux Module functions
709 *
710 ****************************************************************************/
Hans Verkuil7d43cd52008-08-23 05:31:47 -0300711
Hans Verkuilbec43662008-12-30 06:58:20 -0300712static const struct v4l2_file_operations ar_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 .read = ar_read,
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300715 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716};
717
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300718static const struct v4l2_ioctl_ops ar_ioctl_ops = {
719 .vidioc_querycap = ar_querycap,
720 .vidioc_g_input = ar_g_input,
721 .vidioc_s_input = ar_s_input,
722 .vidioc_enum_input = ar_enum_input,
723 .vidioc_enum_fmt_vid_cap = ar_enum_fmt_vid_cap,
724 .vidioc_g_fmt_vid_cap = ar_g_fmt_vid_cap,
725 .vidioc_s_fmt_vid_cap = ar_s_fmt_vid_cap,
726 .vidioc_try_fmt_vid_cap = ar_try_fmt_vid_cap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727};
728
729#define ALIGN4(x) ((((int)(x)) & 0x3) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731static int __init ar_init(void)
732{
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300733 struct ar *ar;
734 struct v4l2_device *v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int ret;
736 int i;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ar = &ardev;
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300739 v4l2_dev = &ar->v4l2_dev;
740 strlcpy(v4l2_dev->name, "arv", sizeof(v4l2_dev->name));
741 v4l2_info(v4l2_dev, "Colour AR VGA driver %s\n", VERSION);
742
743 ret = v4l2_device_register(NULL, v4l2_dev);
744 if (ret < 0) {
745 v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
746 return ret;
747 }
748 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750#if USE_INT
751 /* allocate a DMA buffer for 1 line. */
752 ar->line_buff = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL | GFP_DMA);
Hans Verkuil88f44232010-03-22 04:44:29 -0300753 if (ar->line_buff == NULL || !ALIGN4(ar->line_buff)) {
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300754 v4l2_err(v4l2_dev, "buffer allocation failed for DMA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 ret = -ENOMEM;
756 goto out_end;
757 }
758#endif
759 /* allocate buffers for a frame */
760 for (i = 0; i < MAX_AR_HEIGHT; i++) {
761 ar->frame[i] = kmalloc(MAX_AR_LINE_BYTES, GFP_KERNEL);
Hans Verkuil88f44232010-03-22 04:44:29 -0300762 if (ar->frame[i] == NULL || !ALIGN4(ar->frame[i])) {
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300763 v4l2_err(v4l2_dev, "buffer allocation failed for frame.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ret = -ENOMEM;
765 goto out_line_buff;
766 }
767 }
768
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300769 strlcpy(ar->vdev.name, "Colour AR VGA", sizeof(ar->vdev.name));
770 ar->vdev.v4l2_dev = v4l2_dev;
771 ar->vdev.fops = &ar_fops;
772 ar->vdev.ioctl_ops = &ar_ioctl_ops;
773 ar->vdev.release = video_device_release_empty;
774 video_set_drvdata(&ar->vdev, ar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (vga) {
777 ar->width = AR_WIDTH_VGA;
778 ar->height = AR_HEIGHT_VGA;
779 ar->size = AR_SIZE_VGA;
780 ar->frame_bytes = AR_FRAME_BYTES_VGA;
781 ar->line_bytes = AR_LINE_BYTES_VGA;
782 if (vga_interlace)
783 ar->mode = AR_MODE_INTERLACE;
784 else
785 ar->mode = AR_MODE_NORMAL;
786 } else {
787 ar->width = AR_WIDTH_QVGA;
788 ar->height = AR_HEIGHT_QVGA;
789 ar->size = AR_SIZE_QVGA;
790 ar->frame_bytes = AR_FRAME_BYTES_QVGA;
791 ar->line_bytes = AR_LINE_BYTES_QVGA;
792 ar->mode = AR_MODE_INTERLACE;
793 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200794 mutex_init(&ar->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 init_waitqueue_head(&ar->wait);
796
797#if USE_INT
798 if (request_irq(M32R_IRQ_INT3, ar_interrupt, 0, "arv", ar)) {
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300799 v4l2_err("request_irq(%d) failed.\n", M32R_IRQ_INT3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ret = -EIO;
801 goto out_irq;
802 }
803#endif
804
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300805 if (ar_initialize(ar) != 0) {
806 v4l2_err(v4l2_dev, "M64278 not found.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ret = -ENODEV;
808 goto out_dev;
809 }
810
811 /*
812 * ok, we can initialize h/w according to parameters,
813 * so register video device as a frame grabber type.
814 * device is named "video[0-64]".
815 * video_register_device() initializes h/w using ar_initialize().
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300816 */
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300817 if (video_register_device(&ar->vdev, VFL_TYPE_GRABBER, video_nr) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* return -1, -ENFILE(full) or others */
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300819 v4l2_err(v4l2_dev, "register video (Colour AR) failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 ret = -ENODEV;
821 goto out_dev;
822 }
823
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300824 v4l2_info(v4l2_dev, "%s: Found M64278 VGA (IRQ %d, Freq %dMHz).\n",
825 video_device_node_name(&ar->vdev), M32R_IRQ_INT3, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 return 0;
828
829out_dev:
830#if USE_INT
831 free_irq(M32R_IRQ_INT3, ar);
832
833out_irq:
834#endif
Jesper Juhl2ea75332005-11-07 01:01:31 -0800835 for (i = 0; i < MAX_AR_HEIGHT; i++)
836 kfree(ar->frame[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838out_line_buff:
839#if USE_INT
840 kfree(ar->line_buff);
841
842out_end:
843#endif
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300844 v4l2_device_unregister(&ar->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return ret;
846}
847
848
849static int __init ar_init_module(void)
850{
851 freq = (boot_cpu_data.bus_clock / 1000000);
Hans Verkuil88f44232010-03-22 04:44:29 -0300852 printk(KERN_INFO "arv: Bus clock %d\n", freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (freq != 50 && freq != 75)
854 freq = DEFAULT_FREQ;
855 return ar_init();
856}
857
858static void __exit ar_cleanup_module(void)
859{
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300860 struct ar *ar;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 int i;
862
863 ar = &ardev;
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300864 video_unregister_device(&ar->vdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865#if USE_INT
866 free_irq(M32R_IRQ_INT3, ar);
867#endif
Jesper Juhl2ea75332005-11-07 01:01:31 -0800868 for (i = 0; i < MAX_AR_HEIGHT; i++)
869 kfree(ar->frame[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870#if USE_INT
871 kfree(ar->line_buff);
872#endif
Hans Verkuilb8e56b6f2010-03-29 16:05:33 -0300873 v4l2_device_unregister(&ar->v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876module_init(ar_init_module);
877module_exit(ar_cleanup_module);
878
879MODULE_AUTHOR("Takeo Takahashi <takahashi.takeo@renesas.com>");
880MODULE_DESCRIPTION("Colour AR M64278(VGA) for Video4Linux");
881MODULE_LICENSE("GPL");