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 const struct go7007_board_info board_voyager
= {
61 .sensor_flags
= GO7007_SENSOR_656
|
62 GO7007_SENSOR_VALID_ENABLE
|
65 .audio_flags
= GO7007_AUDIO_I2S_MODE_1
|
79 /********************* Driver for GPIO HPI interface *********************/
81 static int gpio_write(struct saa7134_dev
*dev
, u8 addr
, u16 data
)
83 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
85 /* Write HPI address */
86 saa_writeb(SAA7134_GPIO_GPSTATUS0
, addr
);
87 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
88 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
91 saa_writeb(SAA7134_GPIO_GPSTATUS0
, data
& 0xff);
92 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
93 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
96 saa_writeb(SAA7134_GPIO_GPSTATUS0
, data
>> 8);
97 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
98 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
103 static int gpio_read(struct saa7134_dev
*dev
, u8 addr
, u16
*data
)
105 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
107 /* Write HPI address */
108 saa_writeb(SAA7134_GPIO_GPSTATUS0
, addr
);
109 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
110 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
112 saa_writeb(SAA7134_GPIO_GPMODE0
, 0x00);
115 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_READ
);
116 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
117 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
118 *data
= saa_readb(SAA7134_GPIO_GPSTATUS0
);
119 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
122 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_READ
);
123 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
124 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
125 *data
|= saa_readb(SAA7134_GPIO_GPSTATUS0
) << 8;
126 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
131 static int saa7134_go7007_interface_reset(struct go7007
*go
)
133 struct saa7134_go7007
*saa
= go
->hpi_context
;
134 struct saa7134_dev
*dev
= saa
->dev
;
135 u16 intr_val
, intr_data
;
138 saa_clearb(SAA7134_TS_PARALLEL
, 0x80); /* Disable TS interface */
139 saa_writeb(SAA7134_GPIO_GPMODE2
, 0xa4);
140 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
142 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
143 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_RESET
);
145 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
146 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ2
);
149 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
150 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
152 saa_readb(SAA7134_GPIO_GPSTATUS2
);
153 /*pr_debug("status is %s\n", saa_readb(SAA7134_GPIO_GPSTATUS2) & 0x40 ? "OK" : "not OK"); */
155 /* enter command mode...(?) */
156 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ1
);
157 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_REQ2
);
160 saa_clearb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
161 saa_setb(SAA7134_GPIO_GPMODE3
, SAA7134_GPIO_GPRESCAN
);
162 saa_readb(SAA7134_GPIO_GPSTATUS2
);
163 /*pr_info("gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
164 } while (--count
> 0);
166 /* Wait for an interrupt to indicate successful hardware reset */
167 if (go7007_read_interrupt(go
, &intr_val
, &intr_data
) < 0 ||
168 (intr_val
& ~0x1) != 0x55aa) {
169 pr_err("saa7134-go7007: unable to reset the GO7007\n");
175 static int saa7134_go7007_write_interrupt(struct go7007
*go
, int addr
, int data
)
177 struct saa7134_go7007
*saa
= go
->hpi_context
;
178 struct saa7134_dev
*dev
= saa
->dev
;
182 #ifdef GO7007_HPI_DEBUG
183 pr_debug("saa7134-go7007: WriteInterrupt: %04x %04x\n", addr
, data
);
186 for (i
= 0; i
< 100; ++i
) {
187 gpio_read(dev
, HPI_ADDR_INTR_STATUS
, &status_reg
);
188 if (!(status_reg
& 0x0010))
193 pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
197 gpio_write(dev
, HPI_ADDR_INTR_WR_PARAM
, data
);
198 gpio_write(dev
, HPI_ADDR_INTR_WR_INDEX
, addr
);
203 static int saa7134_go7007_read_interrupt(struct go7007
*go
)
205 struct saa7134_go7007
*saa
= go
->hpi_context
;
206 struct saa7134_dev
*dev
= saa
->dev
;
208 /* XXX we need to wait if there is no interrupt available */
209 go
->interrupt_available
= 1;
210 gpio_read(dev
, HPI_ADDR_INTR_RET_VALUE
, &go
->interrupt_value
);
211 gpio_read(dev
, HPI_ADDR_INTR_RET_DATA
, &go
->interrupt_data
);
212 #ifdef GO7007_HPI_DEBUG
213 pr_debug("saa7134-go7007: ReadInterrupt: %04x %04x\n",
214 go
->interrupt_value
, go
->interrupt_data
);
219 static void saa7134_go7007_irq_ts_done(struct saa7134_dev
*dev
,
220 unsigned long status
)
222 struct go7007
*go
= video_get_drvdata(dev
->empress_dev
);
223 struct saa7134_go7007
*saa
= go
->hpi_context
;
225 if (!vb2_is_streaming(&go
->vidq
))
227 if (0 != (status
& 0x000f0000))
228 pr_debug("saa7134-go7007: irq: lost %ld\n",
229 (status
>> 16) & 0x0f);
230 if (status
& 0x100000) {
231 dma_sync_single_for_cpu(&dev
->pci
->dev
,
232 saa
->bottom_dma
, PAGE_SIZE
, DMA_FROM_DEVICE
);
233 go7007_parse_video_stream(go
, saa
->bottom
, PAGE_SIZE
);
234 saa_writel(SAA7134_RS_BA2(5), saa
->bottom_dma
);
236 dma_sync_single_for_cpu(&dev
->pci
->dev
,
237 saa
->top_dma
, PAGE_SIZE
, DMA_FROM_DEVICE
);
238 go7007_parse_video_stream(go
, saa
->top
, PAGE_SIZE
);
239 saa_writel(SAA7134_RS_BA1(5), saa
->top_dma
);
243 static int saa7134_go7007_stream_start(struct go7007
*go
)
245 struct saa7134_go7007
*saa
= go
->hpi_context
;
246 struct saa7134_dev
*dev
= saa
->dev
;
248 saa
->top_dma
= dma_map_page(&dev
->pci
->dev
, virt_to_page(saa
->top
),
249 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
250 if (dma_mapping_error(&dev
->pci
->dev
, saa
->top_dma
))
252 saa
->bottom_dma
= dma_map_page(&dev
->pci
->dev
,
253 virt_to_page(saa
->bottom
),
254 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
255 if (dma_mapping_error(&dev
->pci
->dev
, saa
->bottom_dma
)) {
256 dma_unmap_page(&dev
->pci
->dev
, saa
->top_dma
, PAGE_SIZE
,
261 saa_writel(SAA7134_VIDEO_PORT_CTRL0
>> 2, 0xA300B000);
262 saa_writel(SAA7134_VIDEO_PORT_CTRL4
>> 2, 0x40000200);
264 /* Set HPI interface for video */
265 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
266 saa_writeb(SAA7134_GPIO_GPSTATUS0
, HPI_ADDR_VIDEO_BUFFER
);
267 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
268 saa_writeb(SAA7134_GPIO_GPMODE0
, 0x00);
270 /* Enable TS interface */
271 saa_writeb(SAA7134_TS_PARALLEL
, 0xe6);
273 /* Reset TS interface */
274 saa_setb(SAA7134_TS_SERIAL1
, 0x01);
275 saa_clearb(SAA7134_TS_SERIAL1
, 0x01);
277 /* Set up transfer block size */
278 saa_writeb(SAA7134_TS_PARALLEL_SERIAL
, 128 - 1);
279 saa_writeb(SAA7134_TS_DMA0
, ((PAGE_SIZE
>> 7) - 1) & 0xff);
280 saa_writeb(SAA7134_TS_DMA1
, (PAGE_SIZE
>> 15) & 0xff);
281 saa_writeb(SAA7134_TS_DMA2
, (PAGE_SIZE
>> 31) & 0x3f);
283 /* Enable video streaming mode */
284 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_VIDEO
);
286 saa_writel(SAA7134_RS_BA1(5), saa
->top_dma
);
287 saa_writel(SAA7134_RS_BA2(5), saa
->bottom_dma
);
288 saa_writel(SAA7134_RS_PITCH(5), 128);
289 saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_MAX
);
292 saa_setl(SAA7134_MAIN_CTRL
, SAA7134_MAIN_CTRL_TE5
);
295 saa_setl(SAA7134_IRQ1
,
296 SAA7134_IRQ1_INTE_RA2_1
| SAA7134_IRQ1_INTE_RA2_0
);
301 static int saa7134_go7007_stream_stop(struct go7007
*go
)
303 struct saa7134_go7007
*saa
= go
->hpi_context
;
304 struct saa7134_dev
*dev
;
312 /* Shut down TS FIFO */
313 saa_clearl(SAA7134_MAIN_CTRL
, SAA7134_MAIN_CTRL_TE5
);
315 /* Disable DMA IRQ */
316 saa_clearl(SAA7134_IRQ1
,
317 SAA7134_IRQ1_INTE_RA2_1
| SAA7134_IRQ1_INTE_RA2_0
);
319 /* Disable TS interface */
320 saa_clearb(SAA7134_TS_PARALLEL
, 0x80);
322 dma_unmap_page(&dev
->pci
->dev
, saa
->top_dma
, PAGE_SIZE
,
324 dma_unmap_page(&dev
->pci
->dev
, saa
->bottom_dma
, PAGE_SIZE
,
330 static int saa7134_go7007_send_firmware(struct go7007
*go
, u8
*data
, int len
)
332 struct saa7134_go7007
*saa
= go
->hpi_context
;
333 struct saa7134_dev
*dev
= saa
->dev
;
337 #ifdef GO7007_HPI_DEBUG
338 pr_debug("saa7134-go7007: DownloadBuffer sending %d bytes\n", len
);
342 i
= len
> 64 ? 64 : len
;
343 saa_writeb(SAA7134_GPIO_GPMODE0
, 0xff);
344 saa_writeb(SAA7134_GPIO_GPSTATUS0
, HPI_ADDR_INIT_BUFFER
);
345 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_ADDR
);
346 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
348 saa_writeb(SAA7134_GPIO_GPSTATUS0
, *data
);
349 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_WRITE
);
350 saa_writeb(SAA7134_GPIO_GPSTATUS2
, GPIO_COMMAND_IDLE
);
354 for (i
= 0; i
< 100; ++i
) {
355 gpio_read(dev
, HPI_ADDR_INTR_STATUS
, &status_reg
);
356 if (!(status_reg
& 0x0002))
360 pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
368 static const struct go7007_hpi_ops saa7134_go7007_hpi_ops
= {
369 .interface_reset
= saa7134_go7007_interface_reset
,
370 .write_interrupt
= saa7134_go7007_write_interrupt
,
371 .read_interrupt
= saa7134_go7007_read_interrupt
,
372 .stream_start
= saa7134_go7007_stream_start
,
373 .stream_stop
= saa7134_go7007_stream_stop
,
374 .send_firmware
= saa7134_go7007_send_firmware
,
376 MODULE_FIRMWARE("go7007/go7007tv.bin");
378 /* --------------------------------------------------------------------------*/
380 static int saa7134_go7007_s_std(struct v4l2_subdev
*sd
, v4l2_std_id norm
)
383 struct saa7134_go7007
*saa
= container_of(sd
, struct saa7134_go7007
, sd
);
384 struct saa7134_dev
*dev
= saa
->dev
;
386 return saa7134_s_std_internal(dev
, NULL
, norm
);
392 static const struct v4l2_subdev_video_ops saa7134_go7007_video_ops
= {
393 .s_std
= saa7134_go7007_s_std
,
396 static const struct v4l2_subdev_ops saa7134_go7007_sd_ops
= {
397 .video
= &saa7134_go7007_video_ops
,
400 /* --------------------------------------------------------------------------*/
403 /********************* Add/remove functions *********************/
405 static int saa7134_go7007_init(struct saa7134_dev
*dev
)
408 struct saa7134_go7007
*saa
;
409 struct v4l2_subdev
*sd
;
411 pr_debug("saa7134-go7007: probing new SAA713X board\n");
413 go
= go7007_alloc(&board_voyager
, &dev
->pci
->dev
);
417 saa
= kzalloc(sizeof(struct saa7134_go7007
), GFP_KERNEL
);
423 go
->board_id
= GO7007_BOARDID_PCI_VOYAGER
;
424 snprintf(go
->bus_info
, sizeof(go
->bus_info
), "PCI:%s", pci_name(dev
->pci
));
425 strscpy(go
->name
, saa7134_boards
[dev
->board
].name
, sizeof(go
->name
));
426 go
->hpi_ops
= &saa7134_go7007_hpi_ops
;
427 go
->hpi_context
= saa
;
430 /* Init the subdevice interface */
432 v4l2_subdev_init(sd
, &saa7134_go7007_sd_ops
);
433 v4l2_set_subdevdata(sd
, saa
);
434 strscpy(sd
->name
, "saa7134-go7007", sizeof(sd
->name
));
436 /* Allocate a couple pages for receiving the compressed stream */
437 saa
->top
= (u8
*)get_zeroed_page(GFP_KERNEL
);
440 saa
->bottom
= (u8
*)get_zeroed_page(GFP_KERNEL
);
444 /* Boot the GO7007 */
445 if (go7007_boot_encoder(go
, go
->board_info
->flags
&
446 GO7007_BOARD_USE_ONBOARD_I2C
) < 0)
449 /* Do any final GO7007 initialization, then register the
450 * V4L2 and ALSA interfaces */
451 if (go7007_register_encoder(go
, go
->board_info
->num_i2c_devs
) < 0)
454 /* Register the subdevice interface with the go7007 device */
455 if (v4l2_device_register_subdev(&go
->v4l2_dev
, sd
) < 0)
456 pr_info("saa7134-go7007: register subdev failed\n");
458 dev
->empress_dev
= &go
->vdev
;
460 go
->status
= STATUS_ONLINE
;
465 free_page((unsigned long)saa
->top
);
467 free_page((unsigned long)saa
->bottom
);
473 static int saa7134_go7007_fini(struct saa7134_dev
*dev
)
476 struct saa7134_go7007
*saa
;
478 if (NULL
== dev
->empress_dev
)
481 go
= video_get_drvdata(dev
->empress_dev
);
482 if (go
->audio_enabled
)
483 go7007_snd_remove(go
);
485 saa
= go
->hpi_context
;
486 go
->status
= STATUS_SHUTDOWN
;
487 free_page((unsigned long)saa
->top
);
488 free_page((unsigned long)saa
->bottom
);
489 v4l2_device_unregister_subdev(&saa
->sd
);
491 vb2_video_unregister_device(&go
->vdev
);
493 v4l2_device_put(&go
->v4l2_dev
);
494 dev
->empress_dev
= NULL
;
499 static struct saa7134_mpeg_ops saa7134_go7007_ops
= {
500 .type
= SAA7134_MPEG_GO7007
,
501 .init
= saa7134_go7007_init
,
502 .fini
= saa7134_go7007_fini
,
503 .irq_ts_done
= saa7134_go7007_irq_ts_done
,
506 static int __init
saa7134_go7007_mod_init(void)
508 return saa7134_ts_register(&saa7134_go7007_ops
);
511 static void __exit
saa7134_go7007_mod_cleanup(void)
513 saa7134_ts_unregister(&saa7134_go7007_ops
);
516 module_init(saa7134_go7007_mod_init
);
517 module_exit(saa7134_go7007_mod_cleanup
);
519 MODULE_DESCRIPTION("go7007 support for saa7134 based TV cards");
520 MODULE_LICENSE("GPL v2");