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.
15 #include "saa7134-reg.h"
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/wait.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/time.h>
26 #include <linux/usb.h>
27 #include <linux/i2c.h>
28 #include <asm/byteorder.h>
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-subdev.h>
33 #include "go7007-priv.h"
35 /*#define GO7007_HPI_DEBUG*/
38 HPI_ADDR_VIDEO_BUFFER
= 0xe4,
39 HPI_ADDR_INIT_BUFFER
= 0xea,
40 HPI_ADDR_INTR_RET_VALUE
= 0xee,
41 HPI_ADDR_INTR_RET_DATA
= 0xec,
42 HPI_ADDR_INTR_STATUS
= 0xf4,
43 HPI_ADDR_INTR_WR_PARAM
= 0xf6,
44 HPI_ADDR_INTR_WR_INDEX
= 0xf8,
48 GPIO_COMMAND_RESET
= 0x00, /* 000b */
49 GPIO_COMMAND_REQ1
= 0x04, /* 001b */
50 GPIO_COMMAND_WRITE
= 0x20, /* 010b */
51 GPIO_COMMAND_REQ2
= 0x24, /* 011b */
52 GPIO_COMMAND_READ
= 0x80, /* 100b */
53 GPIO_COMMAND_VIDEO
= 0x84, /* 101b */
54 GPIO_COMMAND_IDLE
= 0xA0, /* 110b */
55 GPIO_COMMAND_ADDR
= 0xA4, /* 111b */
58 struct saa7134_go7007
{
59 struct v4l2_subdev sd
;
60 struct saa7134_dev
*dev
;
64 dma_addr_t bottom_dma
;
67 static inline struct saa7134_go7007
*to_state(struct v4l2_subdev
*sd
)
69 return container_of(sd
, struct saa7134_go7007
, sd
);
72 static const struct go7007_board_info board_voyager
= {
74 .sensor_flags
= GO7007_SENSOR_656
|
75 GO7007_SENSOR_VALID_ENABLE
|
78 .audio_flags
= GO7007_AUDIO_I2S_MODE_1
|
92 /********************* Driver for GPIO HPI interface *********************/
94 static int gpio_write(struct saa7134_dev
*dev
, u8 addr
, u16 data
)
96 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
98 /* Write HPI address */
99 saa_writeb(SAA7134_GPIO_GPSTATUS0
, addr
);
100 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
101 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
104 saa_writeb(SAA7134_GPIO_GPSTATUS0
, data
& 0xff);
105 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
106 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
108 /* Write high byte */
109 saa_writeb(SAA7134_GPIO_GPSTATUS0
, data
>> 8);
110 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
111 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
116 static int gpio_read(struct saa7134_dev
*dev
, u8 addr
, u16
*data
)
118 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
120 /* Write HPI address */
121 saa_writeb(SAA7134_GPIO_GPSTATUS0
, addr
);
122 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
123 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
125 saa_writeb(SAA7134_GPIO_GPMODE0
, 0x00);
128 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_READ
);
129 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
130 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
131 *data
= saa_readb(SAA7134_GPIO_GPSTATUS0
);
132 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
135 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_READ
);
136 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
137 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
138 *data
|= saa_readb(SAA7134_GPIO_GPSTATUS0
) << 8;
139 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
144 static int saa7134_go7007_interface_reset(struct go7007
*go
)
146 struct saa7134_go7007
*saa
= go
->hpi_context
;
147 struct saa7134_dev
*dev
= saa
->dev
;
148 u16 intr_val
, intr_data
;
151 saa_clearb(SAA7134_TS_PARALLEL
, 0x80); /* Disable TS interface */
152 saa_writeb(SAA7134_GPIO_GPMODE2
, 0xa4);
153 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
155 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
156 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_RESET
);
158 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
159 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ2
);
162 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
163 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
165 saa_readb(SAA7134_GPIO_GPSTATUS2
);
166 /*pr_debug("status is %s\n", saa_readb(SAA7134_GPIO_GPSTATUS2) & 0x40 ? "OK" : "not OK"); */
168 /* enter command mode...(?) */
169 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
170 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ2
);
173 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
174 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
175 saa_readb(SAA7134_GPIO_GPSTATUS2
);
176 /*pr_info("gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
177 } while (--count
> 0);
179 /* Wait for an interrupt to indicate successful hardware reset */
180 if (go7007_read_interrupt(go
, &intr_val
, &intr_data
) < 0 ||
181 (intr_val
& ~0x1) != 0x55aa) {
182 pr_err("saa7134-go7007: unable to reset the GO7007\n");
188 static int saa7134_go7007_write_interrupt(struct go7007
*go
, int addr
, int data
)
190 struct saa7134_go7007
*saa
= go
->hpi_context
;
191 struct saa7134_dev
*dev
= saa
->dev
;
195 #ifdef GO7007_HPI_DEBUG
196 pr_debug("saa7134-go7007: WriteInterrupt: %04x %04x\n", addr
, data
);
199 for (i
= 0; i
< 100; ++i
) {
200 gpio_read(dev
, HPI_ADDR_INTR_STATUS
, &status_reg
);
201 if (!(status_reg
& 0x0010))
206 pr_err("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 pr_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
;
238 if (!vb2_is_streaming(&go
->vidq
))
240 if (0 != (status
& 0x000f0000))
241 pr_debug("saa7134-go7007: irq: lost %ld\n",
242 (status
>> 16) & 0x0f);
243 if (status
& 0x100000) {
244 dma_sync_single_for_cpu(&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), saa
->bottom_dma
);
249 dma_sync_single_for_cpu(&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), 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 (dma_mapping_error(&dev
->pci
->dev
, saa
->top_dma
))
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 (dma_mapping_error(&dev
->pci
->dev
, 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) & 0xff);
293 saa_writeb(SAA7134_TS_DMA1
, (PAGE_SIZE
>> 15) & 0xff);
294 saa_writeb(SAA7134_TS_DMA2
, (PAGE_SIZE
>> 31) & 0x3f);
296 /* Enable video streaming mode */
297 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_VIDEO
);
299 saa_writel(SAA7134_RS_BA1(5), saa
->top_dma
);
300 saa_writel(SAA7134_RS_BA2(5), 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
;
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
,
337 dma_unmap_page(&dev
->pci
->dev
, saa
->bottom_dma
, PAGE_SIZE
,
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
;
350 #ifdef GO7007_HPI_DEBUG
351 pr_debug("saa7134-go7007: DownloadBuffer sending %d bytes\n", len
);
355 i
= len
> 64 ? 64 : len
;
356 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
357 saa_writeb(SAA7134_GPIO_GPSTATUS0
, HPI_ADDR_INIT_BUFFER
);
358 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
359 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
361 saa_writeb(SAA7134_GPIO_GPSTATUS0
, *data
);
362 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
363 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
367 for (i
= 0; i
< 100; ++i
) {
368 gpio_read(dev
, HPI_ADDR_INTR_STATUS
, &status_reg
);
369 if (!(status_reg
& 0x0002))
373 pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
381 static const struct go7007_hpi_ops saa7134_go7007_hpi_ops
= {
382 .interface_reset
= saa7134_go7007_interface_reset
,
383 .write_interrupt
= saa7134_go7007_write_interrupt
,
384 .read_interrupt
= saa7134_go7007_read_interrupt
,
385 .stream_start
= saa7134_go7007_stream_start
,
386 .stream_stop
= saa7134_go7007_stream_stop
,
387 .send_firmware
= saa7134_go7007_send_firmware
,
389 MODULE_FIRMWARE("go7007/go7007tv.bin");
391 /* --------------------------------------------------------------------------*/
393 static int saa7134_go7007_s_std(struct v4l2_subdev
*sd
, v4l2_std_id norm
)
396 struct saa7134_go7007
*saa
= to_state(sd
);
397 struct saa7134_dev
*dev
= saa
->dev
;
399 return saa7134_s_std_internal(dev
, NULL
, norm
);
405 static const struct v4l2_subdev_video_ops saa7134_go7007_video_ops
= {
406 .s_std
= saa7134_go7007_s_std
,
409 static const struct v4l2_subdev_ops saa7134_go7007_sd_ops
= {
410 .video
= &saa7134_go7007_video_ops
,
413 /* --------------------------------------------------------------------------*/
416 /********************* Add/remove functions *********************/
418 static int saa7134_go7007_init(struct saa7134_dev
*dev
)
421 struct saa7134_go7007
*saa
;
422 struct v4l2_subdev
*sd
;
424 pr_debug("saa7134-go7007: probing new SAA713X board\n");
426 go
= go7007_alloc(&board_voyager
, &dev
->pci
->dev
);
430 saa
= kzalloc(sizeof(struct saa7134_go7007
), GFP_KERNEL
);
436 go
->board_id
= GO7007_BOARDID_PCI_VOYAGER
;
437 snprintf(go
->bus_info
, sizeof(go
->bus_info
), "PCI:%s", pci_name(dev
->pci
));
438 strlcpy(go
->name
, saa7134_boards
[dev
->board
].name
, sizeof(go
->name
));
439 go
->hpi_ops
= &saa7134_go7007_hpi_ops
;
440 go
->hpi_context
= saa
;
443 /* Init the subdevice interface */
445 v4l2_subdev_init(sd
, &saa7134_go7007_sd_ops
);
446 v4l2_set_subdevdata(sd
, saa
);
447 strncpy(sd
->name
, "saa7134-go7007", sizeof(sd
->name
));
449 /* Allocate a couple pages for receiving the compressed stream */
450 saa
->top
= (u8
*)get_zeroed_page(GFP_KERNEL
);
453 saa
->bottom
= (u8
*)get_zeroed_page(GFP_KERNEL
);
457 /* Boot the GO7007 */
458 if (go7007_boot_encoder(go
, go
->board_info
->flags
&
459 GO7007_BOARD_USE_ONBOARD_I2C
) < 0)
462 /* Do any final GO7007 initialization, then register the
463 * V4L2 and ALSA interfaces */
464 if (go7007_register_encoder(go
, go
->board_info
->num_i2c_devs
) < 0)
467 /* Register the subdevice interface with the go7007 device */
468 if (v4l2_device_register_subdev(&go
->v4l2_dev
, sd
) < 0)
469 pr_info("saa7134-go7007: register subdev failed\n");
471 dev
->empress_dev
= &go
->vdev
;
473 go
->status
= STATUS_ONLINE
;
478 free_page((unsigned long)saa
->top
);
480 free_page((unsigned long)saa
->bottom
);
486 static int saa7134_go7007_fini(struct saa7134_dev
*dev
)
489 struct saa7134_go7007
*saa
;
491 if (NULL
== dev
->empress_dev
)
494 go
= video_get_drvdata(dev
->empress_dev
);
495 if (go
->audio_enabled
)
496 go7007_snd_remove(go
);
498 saa
= go
->hpi_context
;
499 go
->status
= STATUS_SHUTDOWN
;
500 free_page((unsigned long)saa
->top
);
501 free_page((unsigned long)saa
->bottom
);
502 v4l2_device_unregister_subdev(&saa
->sd
);
504 video_unregister_device(&go
->vdev
);
506 v4l2_device_put(&go
->v4l2_dev
);
507 dev
->empress_dev
= NULL
;
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");