Merge branch 'for-linus' of git://git.o-hand.com/linux-rpurdie-leds
[linux-2.6] / drivers / staging / go7007 / saa7134-go7007.c
1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
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.
7  *
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.
12  *
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.
16  */
17
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/audiochip.h>
31
32 #include "saa7134-reg.h"
33 #include "saa7134.h"
34 #include "go7007-priv.h"
35
36 #define GO7007_HPI_DEBUG
37
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,
46 };
47
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 */
57 };
58
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;
65 };
66
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          = {
82                 {
83                         .name           = "SAA7134",
84                 },
85         },
86 };
87
88 /********************* Driver for GPIO HPI interface *********************/
89
90 static int gpio_write(struct saa7134_dev *dev, u8 addr, u16 data)
91 {
92         saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
93
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);
98
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);
103
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);
108
109         return 0;
110 }
111
112 static int gpio_read(struct saa7134_dev *dev, u8 addr, u16 *data)
113 {
114         saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
115
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);
120
121         saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
122
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);
129
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);
136
137         return 0;
138 }
139
140 static int saa7134_go7007_interface_reset(struct go7007 *go)
141 {
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;
147
148         saa_clearb(SAA7134_TS_PARALLEL, 0x80); /* Disable TS interface */
149         saa_writeb(SAA7134_GPIO_GPMODE2, 0xa4);
150         saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
151
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);
158
159         saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
160         saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
161
162         status = saa_readb(SAA7134_GPIO_GPSTATUS2);
163         /*printk(KERN_DEBUG "status is %s\n", status & 0x40 ? "OK" : "not OK"); */
164
165         /* enter command mode...(?) */
166         saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
167         saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
168
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);
175
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;
182         }
183         return 0;
184 }
185
186 static int saa7134_go7007_write_interrupt(struct go7007 *go, int addr, int data)
187 {
188         struct saa7134_go7007 *saa = go->hpi_context;
189         struct saa7134_dev *dev = saa->dev;
190         int i;
191         u16 status_reg;
192
193 #ifdef GO7007_HPI_DEBUG
194         printk(KERN_DEBUG
195                 "saa7134-go7007: WriteInterrupt: %04x %04x\n", addr, data);
196 #endif
197
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);
203         }
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;
209         }
210         gpio_write(dev, HPI_ADDR_INTR_WR_PARAM, data);
211         gpio_write(dev, HPI_ADDR_INTR_WR_INDEX, addr);
212
213         return 0;
214 }
215
216 static int saa7134_go7007_read_interrupt(struct go7007 *go)
217 {
218         struct saa7134_go7007 *saa = go->hpi_context;
219         struct saa7134_dev *dev = saa->dev;
220
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;
230 }
231
232 static void saa7134_go7007_irq_ts_done(struct saa7134_dev *dev,
233                                                 unsigned long status)
234 {
235         struct go7007 *go = video_get_drvdata(dev->empress_dev);
236         struct saa7134_go7007 *saa = go->hpi_context;
237
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));
253         }
254 }
255
256 static int saa7134_go7007_stream_start(struct go7007 *go)
257 {
258         struct saa7134_go7007 *saa = go->hpi_context;
259         struct saa7134_dev *dev = saa->dev;
260
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;
272         }
273
274         saa_writel(SAA7134_VIDEO_PORT_CTRL0 >> 2, 0xA300B000);
275         saa_writel(SAA7134_VIDEO_PORT_CTRL4 >> 2, 0x40000200);
276
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);
282
283         /* Enable TS interface */
284         saa_writeb(SAA7134_TS_PARALLEL, 0xe6);
285
286         /* Reset TS interface */
287         saa_setb(SAA7134_TS_SERIAL1, 0x01);
288         saa_clearb(SAA7134_TS_SERIAL1, 0x01);
289
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);
295
296         /* Enable video streaming mode */
297         saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_VIDEO);
298
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);
303
304         /* Enable TS FIFO */
305         saa_setl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
306
307         /* Enable DMA IRQ */
308         saa_setl(SAA7134_IRQ1,
309                         SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
310
311         return 0;
312 }
313
314 static int saa7134_go7007_stream_stop(struct go7007 *go)
315 {
316         struct saa7134_go7007 *saa = go->hpi_context;
317         struct saa7134_dev *dev = saa->dev;
318
319         /* Shut down TS FIFO */
320         saa_clearl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
321
322         /* Disable DMA IRQ */
323         saa_clearl(SAA7134_IRQ1,
324                         SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
325
326         /* Disable TS interface */
327         saa_clearb(SAA7134_TS_PARALLEL, 0x80);
328
329         dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
330                         DMA_FROM_DEVICE);
331         dma_unmap_page(&dev->pci->dev, saa->bottom_dma, PAGE_SIZE,
332                         DMA_FROM_DEVICE);
333
334         return 0;
335 }
336
337 static int saa7134_go7007_send_firmware(struct go7007 *go, u8 *data, int len)
338 {
339         struct saa7134_go7007 *saa = go->hpi_context;
340         struct saa7134_dev *dev = saa->dev;
341         u16 status_reg;
342         int i;
343
344 #ifdef GO7007_HPI_DEBUG
345         printk(KERN_DEBUG "saa7134-go7007: DownloadBuffer "
346                         "sending %d bytes\n", len);
347 #endif
348
349         while (len > 0) {
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);
355                 while (i-- > 0) {
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);
359                         ++data;
360                         --len;
361                 }
362                 for (i = 0; i < 100; ++i) {
363                         gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
364                         if (!(status_reg & 0x0002))
365                                 break;
366                 }
367                 if (i == 100) {
368                         printk(KERN_ERR "saa7134-go7007: device is hung, "
369                                         "status reg = 0x%04x\n", status_reg);
370                         return -1;
371                 }
372         }
373         return 0;
374 }
375
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,
383 };
384
385 /********************* Add/remove functions *********************/
386
387 static int saa7134_go7007_init(struct saa7134_dev *dev)
388 {
389         struct go7007 *go;
390         struct saa7134_go7007 *saa;
391
392         printk(KERN_DEBUG "saa7134-go7007: probing new SAA713X board\n");
393
394         saa = kmalloc(sizeof(struct saa7134_go7007), GFP_KERNEL);
395         if (saa == NULL)
396                 return -ENOMEM;
397         memset(saa, 0, sizeof(struct saa7134_go7007));
398
399         /* Allocate a couple pages for receiving the compressed stream */
400         saa->top = (u8 *)get_zeroed_page(GFP_KERNEL);
401         if (!saa->top)
402                 goto allocfail;
403         saa->bottom = (u8 *)get_zeroed_page(GFP_KERNEL);
404         if (!saa->bottom)
405                 goto allocfail;
406
407         go = go7007_alloc(&board_voyager, &dev->pci->dev);
408         if (go == NULL)
409                 goto allocfail;
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;
414         saa->dev = dev;
415
416         /* Boot the GO7007 */
417         if (go7007_boot_encoder(go, go->board_info->flags &
418                                         GO7007_BOARD_USE_ONBOARD_I2C) < 0)
419                 goto initfail;
420
421         /* Do any final GO7007 initialization, then register the
422          * V4L2 and ALSA interfaces */
423         if (go7007_register_encoder(go) < 0)
424                 goto initfail;
425         dev->empress_dev = go->video_dev;
426         video_set_drvdata(dev->empress_dev, go);
427
428         go->status = STATUS_ONLINE;
429         return 0;
430
431 initfail:
432         go->status = STATUS_SHUTDOWN;
433         return 0;
434
435 allocfail:
436         if (saa->top)
437                 free_page((unsigned long)saa->top);
438         if (saa->bottom)
439                 free_page((unsigned long)saa->bottom);
440         kfree(saa);
441         return -ENOMEM;
442 }
443
444 static int saa7134_go7007_fini(struct saa7134_dev *dev)
445 {
446         struct go7007 *go;
447         struct saa7134_go7007 *saa;
448
449         if (NULL == dev->empress_dev)
450                 return 0;
451
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);
457         kfree(saa);
458         go7007_remove(go);
459         dev->empress_dev = NULL;
460
461         return 0;
462 }
463
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,
469 };
470
471 static int __init saa7134_go7007_mod_init(void)
472 {
473         return saa7134_ts_register(&saa7134_go7007_ops);
474 }
475
476 static void __exit saa7134_go7007_mod_cleanup(void)
477 {
478         saa7134_ts_unregister(&saa7134_go7007_ops);
479 }
480
481 module_init(saa7134_go7007_mod_init);
482 module_exit(saa7134_go7007_mod_cleanup);
483
484 MODULE_LICENSE("GPL v2");