1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2006 Micronas USA Inc.
7 #include "saa7134-reg.h"
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/wait.h>
14 #include <linux/list.h>
15 #include <linux/slab.h>
16 #include <linux/time.h>
18 #include <linux/usb.h>
19 #include <linux/i2c.h>
20 #include <asm/byteorder.h>
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-subdev.h>
25 #include "go7007-priv.h"
27 /*#define GO7007_HPI_DEBUG*/
30 HPI_ADDR_VIDEO_BUFFER
= 0xe4,
31 HPI_ADDR_INIT_BUFFER
= 0xea,
32 HPI_ADDR_INTR_RET_VALUE
= 0xee,
33 HPI_ADDR_INTR_RET_DATA
= 0xec,
34 HPI_ADDR_INTR_STATUS
= 0xf4,
35 HPI_ADDR_INTR_WR_PARAM
= 0xf6,
36 HPI_ADDR_INTR_WR_INDEX
= 0xf8,
40 GPIO_COMMAND_RESET
= 0x00, /* 000b */
41 GPIO_COMMAND_REQ1
= 0x04, /* 001b */
42 GPIO_COMMAND_WRITE
= 0x20, /* 010b */
43 GPIO_COMMAND_REQ2
= 0x24, /* 011b */
44 GPIO_COMMAND_READ
= 0x80, /* 100b */
45 GPIO_COMMAND_VIDEO
= 0x84, /* 101b */
46 GPIO_COMMAND_IDLE
= 0xA0, /* 110b */
47 GPIO_COMMAND_ADDR
= 0xA4, /* 111b */
50 struct saa7134_go7007
{
51 struct v4l2_subdev sd
;
52 struct saa7134_dev
*dev
;
56 dma_addr_t bottom_dma
;
59 static inline struct saa7134_go7007
*to_state(struct v4l2_subdev
*sd
)
61 return container_of(sd
, struct saa7134_go7007
, sd
);
64 static const struct go7007_board_info board_voyager
= {
66 .sensor_flags
= GO7007_SENSOR_656
|
67 GO7007_SENSOR_VALID_ENABLE
|
70 .audio_flags
= GO7007_AUDIO_I2S_MODE_1
|
84 /********************* Driver for GPIO HPI interface *********************/
86 static int gpio_write(struct saa7134_dev
*dev
, u8 addr
, u16 data
)
88 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
90 /* Write HPI address */
91 saa_writeb(SAA7134_GPIO_GPSTATUS0
, addr
);
92 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
93 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
96 saa_writeb(SAA7134_GPIO_GPSTATUS0
, data
& 0xff);
97 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
98 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
100 /* Write high byte */
101 saa_writeb(SAA7134_GPIO_GPSTATUS0
, data
>> 8);
102 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
103 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
108 static int gpio_read(struct saa7134_dev
*dev
, u8 addr
, u16
*data
)
110 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
112 /* Write HPI address */
113 saa_writeb(SAA7134_GPIO_GPSTATUS0
, addr
);
114 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
115 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
117 saa_writeb(SAA7134_GPIO_GPMODE0
, 0x00);
120 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_READ
);
121 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
122 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
123 *data
= saa_readb(SAA7134_GPIO_GPSTATUS0
);
124 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
127 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_READ
);
128 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
129 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
130 *data
|= saa_readb(SAA7134_GPIO_GPSTATUS0
) << 8;
131 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
136 static int saa7134_go7007_interface_reset(struct go7007
*go
)
138 struct saa7134_go7007
*saa
= go
->hpi_context
;
139 struct saa7134_dev
*dev
= saa
->dev
;
140 u16 intr_val
, intr_data
;
143 saa_clearb(SAA7134_TS_PARALLEL
, 0x80); /* Disable TS interface */
144 saa_writeb(SAA7134_GPIO_GPMODE2
, 0xa4);
145 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
147 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
148 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_RESET
);
150 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
151 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ2
);
154 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
155 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
157 saa_readb(SAA7134_GPIO_GPSTATUS2
);
158 /*pr_debug("status is %s\n", saa_readb(SAA7134_GPIO_GPSTATUS2) & 0x40 ? "OK" : "not OK"); */
160 /* enter command mode...(?) */
161 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
162 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ2
);
165 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
166 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
167 saa_readb(SAA7134_GPIO_GPSTATUS2
);
168 /*pr_info("gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
169 } while (--count
> 0);
171 /* Wait for an interrupt to indicate successful hardware reset */
172 if (go7007_read_interrupt(go
, &intr_val
, &intr_data
) < 0 ||
173 (intr_val
& ~0x1) != 0x55aa) {
174 pr_err("saa7134-go7007: unable to reset the GO7007\n");
180 static int saa7134_go7007_write_interrupt(struct go7007
*go
, int addr
, int data
)
182 struct saa7134_go7007
*saa
= go
->hpi_context
;
183 struct saa7134_dev
*dev
= saa
->dev
;
187 #ifdef GO7007_HPI_DEBUG
188 pr_debug("saa7134-go7007: WriteInterrupt: %04x %04x\n", addr
, data
);
191 for (i
= 0; i
< 100; ++i
) {
192 gpio_read(dev
, HPI_ADDR_INTR_STATUS
, &status_reg
);
193 if (!(status_reg
& 0x0010))
198 pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
202 gpio_write(dev
, HPI_ADDR_INTR_WR_PARAM
, data
);
203 gpio_write(dev
, HPI_ADDR_INTR_WR_INDEX
, addr
);
208 static int saa7134_go7007_read_interrupt(struct go7007
*go
)
210 struct saa7134_go7007
*saa
= go
->hpi_context
;
211 struct saa7134_dev
*dev
= saa
->dev
;
213 /* XXX we need to wait if there is no interrupt available */
214 go
->interrupt_available
= 1;
215 gpio_read(dev
, HPI_ADDR_INTR_RET_VALUE
, &go
->interrupt_value
);
216 gpio_read(dev
, HPI_ADDR_INTR_RET_DATA
, &go
->interrupt_data
);
217 #ifdef GO7007_HPI_DEBUG
218 pr_debug("saa7134-go7007: ReadInterrupt: %04x %04x\n",
219 go
->interrupt_value
, go
->interrupt_data
);
224 static void saa7134_go7007_irq_ts_done(struct saa7134_dev
*dev
,
225 unsigned long status
)
227 struct go7007
*go
= video_get_drvdata(dev
->empress_dev
);
228 struct saa7134_go7007
*saa
= go
->hpi_context
;
230 if (!vb2_is_streaming(&go
->vidq
))
232 if (0 != (status
& 0x000f0000))
233 pr_debug("saa7134-go7007: irq: lost %ld\n",
234 (status
>> 16) & 0x0f);
235 if (status
& 0x100000) {
236 dma_sync_single_for_cpu(&dev
->pci
->dev
,
237 saa
->bottom_dma
, PAGE_SIZE
, DMA_FROM_DEVICE
);
238 go7007_parse_video_stream(go
, saa
->bottom
, PAGE_SIZE
);
239 saa_writel(SAA7134_RS_BA2(5), saa
->bottom_dma
);
241 dma_sync_single_for_cpu(&dev
->pci
->dev
,
242 saa
->top_dma
, PAGE_SIZE
, DMA_FROM_DEVICE
);
243 go7007_parse_video_stream(go
, saa
->top
, PAGE_SIZE
);
244 saa_writel(SAA7134_RS_BA1(5), saa
->top_dma
);
248 static int saa7134_go7007_stream_start(struct go7007
*go
)
250 struct saa7134_go7007
*saa
= go
->hpi_context
;
251 struct saa7134_dev
*dev
= saa
->dev
;
253 saa
->top_dma
= dma_map_page(&dev
->pci
->dev
, virt_to_page(saa
->top
),
254 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
255 if (dma_mapping_error(&dev
->pci
->dev
, saa
->top_dma
))
257 saa
->bottom_dma
= dma_map_page(&dev
->pci
->dev
,
258 virt_to_page(saa
->bottom
),
259 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
260 if (dma_mapping_error(&dev
->pci
->dev
, saa
->bottom_dma
)) {
261 dma_unmap_page(&dev
->pci
->dev
, saa
->top_dma
, PAGE_SIZE
,
266 saa_writel(SAA7134_VIDEO_PORT_CTRL0
>> 2, 0xA300B000);
267 saa_writel(SAA7134_VIDEO_PORT_CTRL4
>> 2, 0x40000200);
269 /* Set HPI interface for video */
270 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
271 saa_writeb(SAA7134_GPIO_GPSTATUS0
, HPI_ADDR_VIDEO_BUFFER
);
272 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
273 saa_writeb(SAA7134_GPIO_GPMODE0
, 0x00);
275 /* Enable TS interface */
276 saa_writeb(SAA7134_TS_PARALLEL
, 0xe6);
278 /* Reset TS interface */
279 saa_setb(SAA7134_TS_SERIAL1
, 0x01);
280 saa_clearb(SAA7134_TS_SERIAL1
, 0x01);
282 /* Set up transfer block size */
283 saa_writeb(SAA7134_TS_PARALLEL_SERIAL
, 128 - 1);
284 saa_writeb(SAA7134_TS_DMA0
, ((PAGE_SIZE
>> 7) - 1) & 0xff);
285 saa_writeb(SAA7134_TS_DMA1
, (PAGE_SIZE
>> 15) & 0xff);
286 saa_writeb(SAA7134_TS_DMA2
, (PAGE_SIZE
>> 31) & 0x3f);
288 /* Enable video streaming mode */
289 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_VIDEO
);
291 saa_writel(SAA7134_RS_BA1(5), saa
->top_dma
);
292 saa_writel(SAA7134_RS_BA2(5), saa
->bottom_dma
);
293 saa_writel(SAA7134_RS_PITCH(5), 128);
294 saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_MAX
);
297 saa_setl(SAA7134_MAIN_CTRL
, SAA7134_MAIN_CTRL_TE5
);
300 saa_setl(SAA7134_IRQ1
,
301 SAA7134_IRQ1_INTE_RA2_1
| SAA7134_IRQ1_INTE_RA2_0
);
306 static int saa7134_go7007_stream_stop(struct go7007
*go
)
308 struct saa7134_go7007
*saa
= go
->hpi_context
;
309 struct saa7134_dev
*dev
;
317 /* Shut down TS FIFO */
318 saa_clearl(SAA7134_MAIN_CTRL
, SAA7134_MAIN_CTRL_TE5
);
320 /* Disable DMA IRQ */
321 saa_clearl(SAA7134_IRQ1
,
322 SAA7134_IRQ1_INTE_RA2_1
| SAA7134_IRQ1_INTE_RA2_0
);
324 /* Disable TS interface */
325 saa_clearb(SAA7134_TS_PARALLEL
, 0x80);
327 dma_unmap_page(&dev
->pci
->dev
, saa
->top_dma
, PAGE_SIZE
,
329 dma_unmap_page(&dev
->pci
->dev
, saa
->bottom_dma
, PAGE_SIZE
,
335 static int saa7134_go7007_send_firmware(struct go7007
*go
, u8
*data
, int len
)
337 struct saa7134_go7007
*saa
= go
->hpi_context
;
338 struct saa7134_dev
*dev
= saa
->dev
;
342 #ifdef GO7007_HPI_DEBUG
343 pr_debug("saa7134-go7007: DownloadBuffer sending %d bytes\n", len
);
347 i
= len
> 64 ? 64 : len
;
348 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
349 saa_writeb(SAA7134_GPIO_GPSTATUS0
, HPI_ADDR_INIT_BUFFER
);
350 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
351 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
353 saa_writeb(SAA7134_GPIO_GPSTATUS0
, *data
);
354 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
355 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
359 for (i
= 0; i
< 100; ++i
) {
360 gpio_read(dev
, HPI_ADDR_INTR_STATUS
, &status_reg
);
361 if (!(status_reg
& 0x0002))
365 pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
373 static const struct go7007_hpi_ops saa7134_go7007_hpi_ops
= {
374 .interface_reset
= saa7134_go7007_interface_reset
,
375 .write_interrupt
= saa7134_go7007_write_interrupt
,
376 .read_interrupt
= saa7134_go7007_read_interrupt
,
377 .stream_start
= saa7134_go7007_stream_start
,
378 .stream_stop
= saa7134_go7007_stream_stop
,
379 .send_firmware
= saa7134_go7007_send_firmware
,
381 MODULE_FIRMWARE("go7007/go7007tv.bin");
383 /* --------------------------------------------------------------------------*/
385 static int saa7134_go7007_s_std(struct v4l2_subdev
*sd
, v4l2_std_id norm
)
388 struct saa7134_go7007
*saa
= to_state(sd
);
389 struct saa7134_dev
*dev
= saa
->dev
;
391 return saa7134_s_std_internal(dev
, NULL
, norm
);
397 static const struct v4l2_subdev_video_ops saa7134_go7007_video_ops
= {
398 .s_std
= saa7134_go7007_s_std
,
401 static const struct v4l2_subdev_ops saa7134_go7007_sd_ops
= {
402 .video
= &saa7134_go7007_video_ops
,
405 /* --------------------------------------------------------------------------*/
408 /********************* Add/remove functions *********************/
410 static int saa7134_go7007_init(struct saa7134_dev
*dev
)
413 struct saa7134_go7007
*saa
;
414 struct v4l2_subdev
*sd
;
416 pr_debug("saa7134-go7007: probing new SAA713X board\n");
418 go
= go7007_alloc(&board_voyager
, &dev
->pci
->dev
);
422 saa
= kzalloc(sizeof(struct saa7134_go7007
), GFP_KERNEL
);
428 go
->board_id
= GO7007_BOARDID_PCI_VOYAGER
;
429 snprintf(go
->bus_info
, sizeof(go
->bus_info
), "PCI:%s", pci_name(dev
->pci
));
430 strscpy(go
->name
, saa7134_boards
[dev
->board
].name
, sizeof(go
->name
));
431 go
->hpi_ops
= &saa7134_go7007_hpi_ops
;
432 go
->hpi_context
= saa
;
435 /* Init the subdevice interface */
437 v4l2_subdev_init(sd
, &saa7134_go7007_sd_ops
);
438 v4l2_set_subdevdata(sd
, saa
);
439 strscpy(sd
->name
, "saa7134-go7007", sizeof(sd
->name
));
441 /* Allocate a couple pages for receiving the compressed stream */
442 saa
->top
= (u8
*)get_zeroed_page(GFP_KERNEL
);
445 saa
->bottom
= (u8
*)get_zeroed_page(GFP_KERNEL
);
449 /* Boot the GO7007 */
450 if (go7007_boot_encoder(go
, go
->board_info
->flags
&
451 GO7007_BOARD_USE_ONBOARD_I2C
) < 0)
454 /* Do any final GO7007 initialization, then register the
455 * V4L2 and ALSA interfaces */
456 if (go7007_register_encoder(go
, go
->board_info
->num_i2c_devs
) < 0)
459 /* Register the subdevice interface with the go7007 device */
460 if (v4l2_device_register_subdev(&go
->v4l2_dev
, sd
) < 0)
461 pr_info("saa7134-go7007: register subdev failed\n");
463 dev
->empress_dev
= &go
->vdev
;
465 go
->status
= STATUS_ONLINE
;
470 free_page((unsigned long)saa
->top
);
472 free_page((unsigned long)saa
->bottom
);
478 static int saa7134_go7007_fini(struct saa7134_dev
*dev
)
481 struct saa7134_go7007
*saa
;
483 if (NULL
== dev
->empress_dev
)
486 go
= video_get_drvdata(dev
->empress_dev
);
487 if (go
->audio_enabled
)
488 go7007_snd_remove(go
);
490 saa
= go
->hpi_context
;
491 go
->status
= STATUS_SHUTDOWN
;
492 free_page((unsigned long)saa
->top
);
493 free_page((unsigned long)saa
->bottom
);
494 v4l2_device_unregister_subdev(&saa
->sd
);
496 video_unregister_device(&go
->vdev
);
498 v4l2_device_put(&go
->v4l2_dev
);
499 dev
->empress_dev
= NULL
;
504 static struct saa7134_mpeg_ops saa7134_go7007_ops
= {
505 .type
= SAA7134_MPEG_GO7007
,
506 .init
= saa7134_go7007_init
,
507 .fini
= saa7134_go7007_fini
,
508 .irq_ts_done
= saa7134_go7007_irq_ts_done
,
511 static int __init
saa7134_go7007_mod_init(void)
513 return saa7134_ts_register(&saa7134_go7007_ops
);
516 static void __exit
saa7134_go7007_mod_cleanup(void)
518 saa7134_ts_unregister(&saa7134_go7007_ops
);
521 module_init(saa7134_go7007_mod_init
);
522 module_exit(saa7134_go7007_mod_cleanup
);
524 MODULE_LICENSE("GPL v2");