[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / drivers / staging / go7007 / saa7134-go7007.c
blob665bbf59d026db08886708ef5d3927830e780bc2
1 /*
2 * Copyright (C) 2005-2006 Micronas USA Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/spinlock.h>
22 #include <linux/wait.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/mm.h>
27 #include <linux/usb.h>
28 #include <linux/i2c.h>
29 #include <asm/byteorder.h>
30 #include <media/v4l2-common.h>
32 #include "saa7134-reg.h"
33 #include "saa7134.h"
34 #include "go7007-priv.h"
36 #define GO7007_HPI_DEBUG
38 enum hpi_address {
39 HPI_ADDR_VIDEO_BUFFER = 0xe4,
40 HPI_ADDR_INIT_BUFFER = 0xea,
41 HPI_ADDR_INTR_RET_VALUE = 0xee,
42 HPI_ADDR_INTR_RET_DATA = 0xec,
43 HPI_ADDR_INTR_STATUS = 0xf4,
44 HPI_ADDR_INTR_WR_PARAM = 0xf6,
45 HPI_ADDR_INTR_WR_INDEX = 0xf8,
48 enum gpio_command {
49 GPIO_COMMAND_RESET = 0x00, /* 000b */
50 GPIO_COMMAND_REQ1 = 0x04, /* 001b */
51 GPIO_COMMAND_WRITE = 0x20, /* 010b */
52 GPIO_COMMAND_REQ2 = 0x24, /* 011b */
53 GPIO_COMMAND_READ = 0x80, /* 100b */
54 GPIO_COMMAND_VIDEO = 0x84, /* 101b */
55 GPIO_COMMAND_IDLE = 0xA0, /* 110b */
56 GPIO_COMMAND_ADDR = 0xA4, /* 111b */
59 struct saa7134_go7007 {
60 struct saa7134_dev *dev;
61 u8 *top;
62 u8 *bottom;
63 dma_addr_t top_dma;
64 dma_addr_t bottom_dma;
67 static struct go7007_board_info board_voyager = {
68 .firmware = "go7007tv.bin",
69 .flags = 0,
70 .sensor_flags = GO7007_SENSOR_656 |
71 GO7007_SENSOR_VALID_ENABLE |
72 GO7007_SENSOR_TV |
73 GO7007_SENSOR_VBI,
74 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
75 GO7007_AUDIO_WORD_16,
76 .audio_rate = 48000,
77 .audio_bclk_div = 8,
78 .audio_main_div = 2,
79 .hpi_buffer_cap = 7,
80 .num_inputs = 1,
81 .inputs = {
83 .name = "SAA7134",
88 /********************* Driver for GPIO HPI interface *********************/
90 static int gpio_write(struct saa7134_dev *dev, u8 addr, u16 data)
92 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
94 /* Write HPI address */
95 saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
96 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
97 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
99 /* Write low byte */
100 saa_writeb(SAA7134_GPIO_GPSTATUS0, data & 0xff);
101 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
102 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
104 /* Write high byte */
105 saa_writeb(SAA7134_GPIO_GPSTATUS0, data >> 8);
106 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
107 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
109 return 0;
112 static int gpio_read(struct saa7134_dev *dev, u8 addr, u16 *data)
114 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
116 /* Write HPI address */
117 saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
118 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
119 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
121 saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
123 /* Read low byte */
124 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
125 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
126 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
127 *data = saa_readb(SAA7134_GPIO_GPSTATUS0);
128 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
130 /* Read high byte */
131 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
132 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
133 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
134 *data |= saa_readb(SAA7134_GPIO_GPSTATUS0) << 8;
135 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
137 return 0;
140 static int saa7134_go7007_interface_reset(struct go7007 *go)
142 struct saa7134_go7007 *saa = go->hpi_context;
143 struct saa7134_dev *dev = saa->dev;
144 u32 status;
145 u16 intr_val, intr_data;
146 int count = 20;
148 saa_clearb(SAA7134_TS_PARALLEL, 0x80); /* Disable TS interface */
149 saa_writeb(SAA7134_GPIO_GPMODE2, 0xa4);
150 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
152 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
153 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_RESET);
154 msleep(1);
155 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
156 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
157 msleep(10);
159 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
160 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
162 status = saa_readb(SAA7134_GPIO_GPSTATUS2);
163 /*printk(KERN_DEBUG "status is %s\n", status & 0x40 ? "OK" : "not OK"); */
165 /* enter command mode...(?) */
166 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
167 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
169 do {
170 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
171 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
172 status = saa_readb(SAA7134_GPIO_GPSTATUS2);
173 /*printk(KERN_INFO "gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
174 } while (--count > 0);
176 /* Wait for an interrupt to indicate successful hardware reset */
177 if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
178 (intr_val & ~0x1) != 0x55aa) {
179 printk(KERN_ERR
180 "saa7134-go7007: unable to reset the GO7007\n");
181 return -1;
183 return 0;
186 static int saa7134_go7007_write_interrupt(struct go7007 *go, int addr, int data)
188 struct saa7134_go7007 *saa = go->hpi_context;
189 struct saa7134_dev *dev = saa->dev;
190 int i;
191 u16 status_reg;
193 #ifdef GO7007_HPI_DEBUG
194 printk(KERN_DEBUG
195 "saa7134-go7007: WriteInterrupt: %04x %04x\n", addr, data);
196 #endif
198 for (i = 0; i < 100; ++i) {
199 gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
200 if (!(status_reg & 0x0010))
201 break;
202 msleep(10);
204 if (i == 100) {
205 printk(KERN_ERR
206 "saa7134-go7007: device is hung, status reg = 0x%04x\n",
207 status_reg);
208 return -1;
210 gpio_write(dev, HPI_ADDR_INTR_WR_PARAM, data);
211 gpio_write(dev, HPI_ADDR_INTR_WR_INDEX, addr);
213 return 0;
216 static int saa7134_go7007_read_interrupt(struct go7007 *go)
218 struct saa7134_go7007 *saa = go->hpi_context;
219 struct saa7134_dev *dev = saa->dev;
221 /* XXX we need to wait if there is no interrupt available */
222 go->interrupt_available = 1;
223 gpio_read(dev, HPI_ADDR_INTR_RET_VALUE, &go->interrupt_value);
224 gpio_read(dev, HPI_ADDR_INTR_RET_DATA, &go->interrupt_data);
225 #ifdef GO7007_HPI_DEBUG
226 printk(KERN_DEBUG "saa7134-go7007: ReadInterrupt: %04x %04x\n",
227 go->interrupt_value, go->interrupt_data);
228 #endif
229 return 0;
232 static void saa7134_go7007_irq_ts_done(struct saa7134_dev *dev,
233 unsigned long status)
235 struct go7007 *go = video_get_drvdata(dev->empress_dev);
236 struct saa7134_go7007 *saa = go->hpi_context;
238 if (!go->streaming)
239 return;
240 if (0 != (status & 0x000f0000))
241 printk(KERN_DEBUG "saa7134-go7007: irq: lost %ld\n",
242 (status >> 16) & 0x0f);
243 if (status & 0x100000) {
244 dma_sync_single(&dev->pci->dev,
245 saa->bottom_dma, PAGE_SIZE, DMA_FROM_DEVICE);
246 go7007_parse_video_stream(go, saa->bottom, PAGE_SIZE);
247 saa_writel(SAA7134_RS_BA2(5), cpu_to_le32(saa->bottom_dma));
248 } else {
249 dma_sync_single(&dev->pci->dev,
250 saa->top_dma, PAGE_SIZE, DMA_FROM_DEVICE);
251 go7007_parse_video_stream(go, saa->top, PAGE_SIZE);
252 saa_writel(SAA7134_RS_BA1(5), cpu_to_le32(saa->top_dma));
256 static int saa7134_go7007_stream_start(struct go7007 *go)
258 struct saa7134_go7007 *saa = go->hpi_context;
259 struct saa7134_dev *dev = saa->dev;
261 saa->top_dma = dma_map_page(&dev->pci->dev, virt_to_page(saa->top),
262 0, PAGE_SIZE, DMA_FROM_DEVICE);
263 if (!saa->top_dma)
264 return -ENOMEM;
265 saa->bottom_dma = dma_map_page(&dev->pci->dev,
266 virt_to_page(saa->bottom),
267 0, PAGE_SIZE, DMA_FROM_DEVICE);
268 if (!saa->bottom_dma) {
269 dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
270 DMA_FROM_DEVICE);
271 return -ENOMEM;
274 saa_writel(SAA7134_VIDEO_PORT_CTRL0 >> 2, 0xA300B000);
275 saa_writel(SAA7134_VIDEO_PORT_CTRL4 >> 2, 0x40000200);
277 /* Set HPI interface for video */
278 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
279 saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_VIDEO_BUFFER);
280 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
281 saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
283 /* Enable TS interface */
284 saa_writeb(SAA7134_TS_PARALLEL, 0xe6);
286 /* Reset TS interface */
287 saa_setb(SAA7134_TS_SERIAL1, 0x01);
288 saa_clearb(SAA7134_TS_SERIAL1, 0x01);
290 /* Set up transfer block size */
291 saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 128 - 1);
292 saa_writeb(SAA7134_TS_DMA0, (PAGE_SIZE >> 7) - 1);
293 saa_writeb(SAA7134_TS_DMA1, 0);
294 saa_writeb(SAA7134_TS_DMA2, 0);
296 /* Enable video streaming mode */
297 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_VIDEO);
299 saa_writel(SAA7134_RS_BA1(5), cpu_to_le32(saa->top_dma));
300 saa_writel(SAA7134_RS_BA2(5), cpu_to_le32(saa->bottom_dma));
301 saa_writel(SAA7134_RS_PITCH(5), 128);
302 saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_MAX);
304 /* Enable TS FIFO */
305 saa_setl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
307 /* Enable DMA IRQ */
308 saa_setl(SAA7134_IRQ1,
309 SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
311 return 0;
314 static int saa7134_go7007_stream_stop(struct go7007 *go)
316 struct saa7134_go7007 *saa = go->hpi_context;
317 struct saa7134_dev *dev;
319 if (!saa)
320 return -EINVAL;
321 dev = saa->dev;
322 if (!dev)
323 return -EINVAL;
325 /* Shut down TS FIFO */
326 saa_clearl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
328 /* Disable DMA IRQ */
329 saa_clearl(SAA7134_IRQ1,
330 SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
332 /* Disable TS interface */
333 saa_clearb(SAA7134_TS_PARALLEL, 0x80);
335 dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
336 DMA_FROM_DEVICE);
337 dma_unmap_page(&dev->pci->dev, saa->bottom_dma, PAGE_SIZE,
338 DMA_FROM_DEVICE);
340 return 0;
343 static int saa7134_go7007_send_firmware(struct go7007 *go, u8 *data, int len)
345 struct saa7134_go7007 *saa = go->hpi_context;
346 struct saa7134_dev *dev = saa->dev;
347 u16 status_reg;
348 int i;
350 #ifdef GO7007_HPI_DEBUG
351 printk(KERN_DEBUG "saa7134-go7007: DownloadBuffer "
352 "sending %d bytes\n", len);
353 #endif
355 while (len > 0) {
356 i = len > 64 ? 64 : len;
357 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
358 saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_INIT_BUFFER);
359 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
360 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
361 while (i-- > 0) {
362 saa_writeb(SAA7134_GPIO_GPSTATUS0, *data);
363 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
364 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
365 ++data;
366 --len;
368 for (i = 0; i < 100; ++i) {
369 gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
370 if (!(status_reg & 0x0002))
371 break;
373 if (i == 100) {
374 printk(KERN_ERR "saa7134-go7007: device is hung, "
375 "status reg = 0x%04x\n", status_reg);
376 return -1;
379 return 0;
382 static int saa7134_go7007_send_command(struct go7007 *go, unsigned int cmd,
383 void *arg)
385 struct saa7134_go7007 *saa = go->hpi_context;
386 struct saa7134_dev *dev = saa->dev;
388 switch (cmd) {
389 case VIDIOC_S_STD:
391 v4l2_std_id *std = arg;
392 return saa7134_s_std_internal(dev, NULL, std);
394 case VIDIOC_G_STD:
396 v4l2_std_id *std = arg;
397 *std = dev->tvnorm->id;
398 return 0;
400 case VIDIOC_QUERYCTRL:
402 struct v4l2_queryctrl *ctrl = arg;
403 if (V4L2_CTRL_ID2CLASS(ctrl->id) == V4L2_CTRL_CLASS_USER)
404 return saa7134_queryctrl(NULL, NULL, ctrl);
406 case VIDIOC_G_CTRL:
408 struct v4l2_control *ctrl = arg;
409 if (V4L2_CTRL_ID2CLASS(ctrl->id) == V4L2_CTRL_CLASS_USER)
410 return saa7134_g_ctrl_internal(dev, NULL, ctrl);
412 case VIDIOC_S_CTRL:
414 struct v4l2_control *ctrl = arg;
415 if (V4L2_CTRL_ID2CLASS(ctrl->id) == V4L2_CTRL_CLASS_USER)
416 return saa7134_s_ctrl_internal(dev, NULL, ctrl);
419 return -EINVAL;
423 static struct go7007_hpi_ops saa7134_go7007_hpi_ops = {
424 .interface_reset = saa7134_go7007_interface_reset,
425 .write_interrupt = saa7134_go7007_write_interrupt,
426 .read_interrupt = saa7134_go7007_read_interrupt,
427 .stream_start = saa7134_go7007_stream_start,
428 .stream_stop = saa7134_go7007_stream_stop,
429 .send_firmware = saa7134_go7007_send_firmware,
430 .send_command = saa7134_go7007_send_command,
433 /********************* Add/remove functions *********************/
435 static int saa7134_go7007_init(struct saa7134_dev *dev)
437 struct go7007 *go;
438 struct saa7134_go7007 *saa;
440 printk(KERN_DEBUG "saa7134-go7007: probing new SAA713X board\n");
442 saa = kmalloc(sizeof(struct saa7134_go7007), GFP_KERNEL);
443 if (saa == NULL)
444 return -ENOMEM;
445 memset(saa, 0, sizeof(struct saa7134_go7007));
447 /* Allocate a couple pages for receiving the compressed stream */
448 saa->top = (u8 *)get_zeroed_page(GFP_KERNEL);
449 if (!saa->top)
450 goto allocfail;
451 saa->bottom = (u8 *)get_zeroed_page(GFP_KERNEL);
452 if (!saa->bottom)
453 goto allocfail;
455 go = go7007_alloc(&board_voyager, &dev->pci->dev);
456 if (go == NULL)
457 goto allocfail;
458 go->board_id = GO7007_BOARDID_PCI_VOYAGER;
459 strncpy(go->name, saa7134_boards[dev->board].name, sizeof(go->name));
460 go->hpi_ops = &saa7134_go7007_hpi_ops;
461 go->hpi_context = saa;
462 saa->dev = dev;
464 /* Boot the GO7007 */
465 if (go7007_boot_encoder(go, go->board_info->flags &
466 GO7007_BOARD_USE_ONBOARD_I2C) < 0)
467 goto initfail;
469 /* Do any final GO7007 initialization, then register the
470 * V4L2 and ALSA interfaces */
471 if (go7007_register_encoder(go) < 0)
472 goto initfail;
473 dev->empress_dev = go->video_dev;
474 video_set_drvdata(dev->empress_dev, go);
476 go->status = STATUS_ONLINE;
477 return 0;
479 initfail:
480 go->status = STATUS_SHUTDOWN;
481 return 0;
483 allocfail:
484 if (saa->top)
485 free_page((unsigned long)saa->top);
486 if (saa->bottom)
487 free_page((unsigned long)saa->bottom);
488 kfree(saa);
489 return -ENOMEM;
492 static int saa7134_go7007_fini(struct saa7134_dev *dev)
494 struct go7007 *go;
495 struct saa7134_go7007 *saa;
497 if (NULL == dev->empress_dev)
498 return 0;
500 go = video_get_drvdata(dev->empress_dev);
501 saa = go->hpi_context;
502 go->status = STATUS_SHUTDOWN;
503 free_page((unsigned long)saa->top);
504 free_page((unsigned long)saa->bottom);
505 kfree(saa);
506 go7007_remove(go);
507 dev->empress_dev = NULL;
509 return 0;
512 static struct saa7134_mpeg_ops saa7134_go7007_ops = {
513 .type = SAA7134_MPEG_GO7007,
514 .init = saa7134_go7007_init,
515 .fini = saa7134_go7007_fini,
516 .irq_ts_done = saa7134_go7007_irq_ts_done,
519 static int __init saa7134_go7007_mod_init(void)
521 return saa7134_ts_register(&saa7134_go7007_ops);
524 static void __exit saa7134_go7007_mod_cleanup(void)
526 saa7134_ts_unregister(&saa7134_go7007_ops);
529 module_init(saa7134_go7007_mod_init);
530 module_exit(saa7134_go7007_mod_cleanup);
532 MODULE_LICENSE("GPL v2");