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>
27 #include <linux/usb.h>
28 #include <linux/i2c.h>
29 #include <asm/byteorder.h>
30 #include <media/audiochip.h>
32 #include "saa7134-reg.h"
34 #include "go7007-priv.h"
36 #define GO7007_HPI_DEBUG
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,
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;
64 dma_addr_t bottom_dma;
67 static struct go7007_board_info board_voyager = {
68 .firmware = "go7007tv.bin",
70 .sensor_flags = GO7007_SENSOR_656 |
71 GO7007_SENSOR_VALID_ENABLE |
74 .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
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);
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);
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);
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);
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);
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;
145 u16 intr_val, intr_data;
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);
155 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
156 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
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);
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) {
180 "saa7134-go7007: unable to reset the GO7007\n");
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;
193 #ifdef GO7007_HPI_DEBUG
195 "saa7134-go7007: WriteInterrupt: %04x %04x\n", addr, data);
198 for (i = 0; i < 100; ++i) {
199 gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
200 if (!(status_reg & 0x0010))
206 "saa7134-go7007: device is hung, status reg = 0x%04x\n",
210 gpio_write(dev, HPI_ADDR_INTR_WR_PARAM, data);
211 gpio_write(dev, HPI_ADDR_INTR_WR_INDEX, addr);
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);
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;
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));
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);
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,
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);
305 saa_setl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
308 saa_setl(SAA7134_IRQ1,
309 SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
314 static int saa7134_go7007_stream_stop(struct go7007 *go)
316 struct saa7134_go7007 *saa = go->hpi_context;
317 struct saa7134_dev *dev = saa->dev;
319 /* Shut down TS FIFO */
320 saa_clearl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
322 /* Disable DMA IRQ */
323 saa_clearl(SAA7134_IRQ1,
324 SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
326 /* Disable TS interface */
327 saa_clearb(SAA7134_TS_PARALLEL, 0x80);
329 dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
331 dma_unmap_page(&dev->pci->dev, saa->bottom_dma, PAGE_SIZE,
337 static int saa7134_go7007_send_firmware(struct go7007 *go, u8 *data, int len)
339 struct saa7134_go7007 *saa = go->hpi_context;
340 struct saa7134_dev *dev = saa->dev;
344 #ifdef GO7007_HPI_DEBUG
345 printk(KERN_DEBUG "saa7134-go7007: DownloadBuffer "
346 "sending %d bytes\n", len);
350 i = len > 64 ? 64 : len;
351 saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
352 saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_INIT_BUFFER);
353 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
354 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
356 saa_writeb(SAA7134_GPIO_GPSTATUS0, *data);
357 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
358 saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
362 for (i = 0; i < 100; ++i) {
363 gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
364 if (!(status_reg & 0x0002))
368 printk(KERN_ERR "saa7134-go7007: device is hung, "
369 "status reg = 0x%04x\n", status_reg);
376 static struct go7007_hpi_ops saa7134_go7007_hpi_ops = {
377 .interface_reset = saa7134_go7007_interface_reset,
378 .write_interrupt = saa7134_go7007_write_interrupt,
379 .read_interrupt = saa7134_go7007_read_interrupt,
380 .stream_start = saa7134_go7007_stream_start,
381 .stream_stop = saa7134_go7007_stream_stop,
382 .send_firmware = saa7134_go7007_send_firmware,
385 /********************* Add/remove functions *********************/
387 static int saa7134_go7007_init(struct saa7134_dev *dev)
390 struct saa7134_go7007 *saa;
392 printk(KERN_DEBUG "saa7134-go7007: probing new SAA713X board\n");
394 saa = kmalloc(sizeof(struct saa7134_go7007), GFP_KERNEL);
397 memset(saa, 0, sizeof(struct saa7134_go7007));
399 /* Allocate a couple pages for receiving the compressed stream */
400 saa->top = (u8 *)get_zeroed_page(GFP_KERNEL);
403 saa->bottom = (u8 *)get_zeroed_page(GFP_KERNEL);
407 go = go7007_alloc(&board_voyager, &dev->pci->dev);
410 go->board_id = GO7007_BOARDID_PCI_VOYAGER;
411 strncpy(go->name, saa7134_boards[dev->board].name, sizeof(go->name));
412 go->hpi_ops = &saa7134_go7007_hpi_ops;
413 go->hpi_context = saa;
416 /* Boot the GO7007 */
417 if (go7007_boot_encoder(go, go->board_info->flags &
418 GO7007_BOARD_USE_ONBOARD_I2C) < 0)
421 /* Do any final GO7007 initialization, then register the
422 * V4L2 and ALSA interfaces */
423 if (go7007_register_encoder(go) < 0)
425 dev->empress_dev = go->video_dev;
426 video_set_drvdata(dev->empress_dev, go);
428 go->status = STATUS_ONLINE;
432 go->status = STATUS_SHUTDOWN;
437 free_page((unsigned long)saa->top);
439 free_page((unsigned long)saa->bottom);
444 static int saa7134_go7007_fini(struct saa7134_dev *dev)
447 struct saa7134_go7007 *saa;
449 if (NULL == dev->empress_dev)
452 go = video_get_drvdata(dev->empress_dev);
453 saa = go->hpi_context;
454 go->status = STATUS_SHUTDOWN;
455 free_page((unsigned long)saa->top);
456 free_page((unsigned long)saa->bottom);
459 dev->empress_dev = NULL;
464 static struct saa7134_mpeg_ops saa7134_go7007_ops = {
465 .type = SAA7134_MPEG_GO7007,
466 .init = saa7134_go7007_init,
467 .fini = saa7134_go7007_fini,
468 .irq_ts_done = saa7134_go7007_irq_ts_done,
471 static int __init saa7134_go7007_mod_init(void)
473 return saa7134_ts_register(&saa7134_go7007_ops);
476 static void __exit saa7134_go7007_mod_cleanup(void)
478 saa7134_ts_unregister(&saa7134_go7007_ops);
481 module_init(saa7134_go7007_mod_init);
482 module_exit(saa7134_go7007_mod_cleanup);
484 MODULE_LICENSE("GPL v2");