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
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/spinlock.h>
18#include <linux/wait.h>
19#include <linux/list.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/mm.h>
23#include <linux/usb.h>
24#include <linux/i2c.h>
25#include <asm/byteorder.h>
26#include <media/v4l2-common.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-subdev.h>
29
30#include "saa7134.h"
31#include "saa7134-reg.h"
32#include "go7007-priv.h"
33
34/*#define GO7007_HPI_DEBUG*/
35
36enum hpi_address {
37	HPI_ADDR_VIDEO_BUFFER = 0xe4,
38	HPI_ADDR_INIT_BUFFER = 0xea,
39	HPI_ADDR_INTR_RET_VALUE = 0xee,
40	HPI_ADDR_INTR_RET_DATA = 0xec,
41	HPI_ADDR_INTR_STATUS = 0xf4,
42	HPI_ADDR_INTR_WR_PARAM = 0xf6,
43	HPI_ADDR_INTR_WR_INDEX = 0xf8,
44};
45
46enum gpio_command {
47	GPIO_COMMAND_RESET = 0x00, /* 000b */
48	GPIO_COMMAND_REQ1  = 0x04, /* 001b */
49	GPIO_COMMAND_WRITE = 0x20, /* 010b */
50	GPIO_COMMAND_REQ2  = 0x24, /* 011b */
51	GPIO_COMMAND_READ  = 0x80, /* 100b */
52	GPIO_COMMAND_VIDEO = 0x84, /* 101b */
53	GPIO_COMMAND_IDLE  = 0xA0, /* 110b */
54	GPIO_COMMAND_ADDR  = 0xA4, /* 111b */
55};
56
57struct saa7134_go7007 {
58	struct v4l2_subdev sd;
59	struct saa7134_dev *dev;
60	u8 *top;
61	u8 *bottom;
62	dma_addr_t top_dma;
63	dma_addr_t bottom_dma;
64};
65
66static inline struct saa7134_go7007 *to_state(struct v4l2_subdev *sd)
67{
68	return container_of(sd, struct saa7134_go7007, sd);
69}
70
71static const struct go7007_board_info board_voyager = {
72	.flags		 = 0,
73	.sensor_flags	 = GO7007_SENSOR_656 |
74				GO7007_SENSOR_VALID_ENABLE |
75				GO7007_SENSOR_TV |
76				GO7007_SENSOR_VBI,
77	.audio_flags	= GO7007_AUDIO_I2S_MODE_1 |
78				GO7007_AUDIO_WORD_16,
79	.audio_rate	 = 48000,
80	.audio_bclk_div	 = 8,
81	.audio_main_div	 = 2,
82	.hpi_buffer_cap  = 7,
83	.num_inputs	 = 1,
84	.inputs		 = {
85		{
86			.name		= "SAA7134",
87		},
88	},
89};
90
91/********************* Driver for GPIO HPI interface *********************/
92
93static int gpio_write(struct saa7134_dev *dev, u8 addr, u16 data)
94{
95	saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
96
97	/* Write HPI address */
98	saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
99	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
100	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
101
102	/* Write low byte */
103	saa_writeb(SAA7134_GPIO_GPSTATUS0, data & 0xff);
104	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
105	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
106
107	/* Write high byte */
108	saa_writeb(SAA7134_GPIO_GPSTATUS0, data >> 8);
109	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
110	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
111
112	return 0;
113}
114
115static int gpio_read(struct saa7134_dev *dev, u8 addr, u16 *data)
116{
117	saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
118
119	/* Write HPI address */
120	saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
121	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
122	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
123
124	saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
125
126	/* Read low byte */
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);
131	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
132
133	/* Read high byte */
134	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
135	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
136	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
137	*data |= saa_readb(SAA7134_GPIO_GPSTATUS0) << 8;
138	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
139
140	return 0;
141}
142
143static int saa7134_go7007_interface_reset(struct go7007 *go)
144{
145	struct saa7134_go7007 *saa = go->hpi_context;
146	struct saa7134_dev *dev = saa->dev;
147	u16 intr_val, intr_data;
148	int count = 20;
149
150	saa_clearb(SAA7134_TS_PARALLEL, 0x80); /* Disable TS interface */
151	saa_writeb(SAA7134_GPIO_GPMODE2, 0xa4);
152	saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
153
154	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
155	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_RESET);
156	msleep(1);
157	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
158	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
159	msleep(10);
160
161	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
162	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
163
164	saa_readb(SAA7134_GPIO_GPSTATUS2);
165	/*pr_debug("status is %s\n", saa_readb(SAA7134_GPIO_GPSTATUS2) & 0x40 ? "OK" : "not OK"); */
166
167	/* enter command mode...(?) */
168	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
169	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
170
171	do {
172		saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
173		saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
174		saa_readb(SAA7134_GPIO_GPSTATUS2);
175		/*pr_info("gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
176	} while (--count > 0);
177
178	/* Wait for an interrupt to indicate successful hardware reset */
179	if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
180			(intr_val & ~0x1) != 0x55aa) {
181		pr_err("saa7134-go7007: unable to reset the GO7007\n");
182		return -1;
183	}
184	return 0;
185}
186
187static int saa7134_go7007_write_interrupt(struct go7007 *go, int addr, int data)
188{
189	struct saa7134_go7007 *saa = go->hpi_context;
190	struct saa7134_dev *dev = saa->dev;
191	int i;
192	u16 status_reg;
193
194#ifdef GO7007_HPI_DEBUG
195	pr_debug("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		pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
206			status_reg);
207		return -1;
208	}
209	gpio_write(dev, HPI_ADDR_INTR_WR_PARAM, data);
210	gpio_write(dev, HPI_ADDR_INTR_WR_INDEX, addr);
211
212	return 0;
213}
214
215static int saa7134_go7007_read_interrupt(struct go7007 *go)
216{
217	struct saa7134_go7007 *saa = go->hpi_context;
218	struct saa7134_dev *dev = saa->dev;
219
220	/* XXX we need to wait if there is no interrupt available */
221	go->interrupt_available = 1;
222	gpio_read(dev, HPI_ADDR_INTR_RET_VALUE, &go->interrupt_value);
223	gpio_read(dev, HPI_ADDR_INTR_RET_DATA, &go->interrupt_data);
224#ifdef GO7007_HPI_DEBUG
225	pr_debug("saa7134-go7007: ReadInterrupt: %04x %04x\n",
226			go->interrupt_value, go->interrupt_data);
227#endif
228	return 0;
229}
230
231static void saa7134_go7007_irq_ts_done(struct saa7134_dev *dev,
232						unsigned long status)
233{
234	struct go7007 *go = video_get_drvdata(dev->empress_dev);
235	struct saa7134_go7007 *saa = go->hpi_context;
236
237	if (!vb2_is_streaming(&go->vidq))
238		return;
239	if (0 != (status & 0x000f0000))
240		pr_debug("saa7134-go7007: irq: lost %ld\n",
241				(status >> 16) & 0x0f);
242	if (status & 0x100000) {
243		dma_sync_single_for_cpu(&dev->pci->dev,
244					saa->bottom_dma, PAGE_SIZE, DMA_FROM_DEVICE);
245		go7007_parse_video_stream(go, saa->bottom, PAGE_SIZE);
246		saa_writel(SAA7134_RS_BA2(5), saa->bottom_dma);
247	} else {
248		dma_sync_single_for_cpu(&dev->pci->dev,
249					saa->top_dma, PAGE_SIZE, DMA_FROM_DEVICE);
250		go7007_parse_video_stream(go, saa->top, PAGE_SIZE);
251		saa_writel(SAA7134_RS_BA1(5), saa->top_dma);
252	}
253}
254
255static int saa7134_go7007_stream_start(struct go7007 *go)
256{
257	struct saa7134_go7007 *saa = go->hpi_context;
258	struct saa7134_dev *dev = saa->dev;
259
260	saa->top_dma = dma_map_page(&dev->pci->dev, virt_to_page(saa->top),
261			0, PAGE_SIZE, DMA_FROM_DEVICE);
262	if (dma_mapping_error(&dev->pci->dev, saa->top_dma))
263		return -ENOMEM;
264	saa->bottom_dma = dma_map_page(&dev->pci->dev,
265			virt_to_page(saa->bottom),
266			0, PAGE_SIZE, DMA_FROM_DEVICE);
267	if (dma_mapping_error(&dev->pci->dev, saa->bottom_dma)) {
268		dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
269				DMA_FROM_DEVICE);
270		return -ENOMEM;
271	}
272
273	saa_writel(SAA7134_VIDEO_PORT_CTRL0 >> 2, 0xA300B000);
274	saa_writel(SAA7134_VIDEO_PORT_CTRL4 >> 2, 0x40000200);
275
276	/* Set HPI interface for video */
277	saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
278	saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_VIDEO_BUFFER);
279	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
280	saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
281
282	/* Enable TS interface */
283	saa_writeb(SAA7134_TS_PARALLEL, 0xe6);
284
285	/* Reset TS interface */
286	saa_setb(SAA7134_TS_SERIAL1, 0x01);
287	saa_clearb(SAA7134_TS_SERIAL1, 0x01);
288
289	/* Set up transfer block size */
290	saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 128 - 1);
291	saa_writeb(SAA7134_TS_DMA0, (PAGE_SIZE >> 7) - 1);
292	saa_writeb(SAA7134_TS_DMA1, 0);
293	saa_writeb(SAA7134_TS_DMA2, 0);
294
295	/* Enable video streaming mode */
296	saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_VIDEO);
297
298	saa_writel(SAA7134_RS_BA1(5), saa->top_dma);
299	saa_writel(SAA7134_RS_BA2(5), saa->bottom_dma);
300	saa_writel(SAA7134_RS_PITCH(5), 128);
301	saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_MAX);
302
303	/* Enable TS FIFO */
304	saa_setl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
305
306	/* Enable DMA IRQ */
307	saa_setl(SAA7134_IRQ1,
308			SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
309
310	return 0;
311}
312
313static int saa7134_go7007_stream_stop(struct go7007 *go)
314{
315	struct saa7134_go7007 *saa = go->hpi_context;
316	struct saa7134_dev *dev;
317
318	if (!saa)
319		return -EINVAL;
320	dev = saa->dev;
321	if (!dev)
322		return -EINVAL;
323
324	/* Shut down TS FIFO */
325	saa_clearl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
326
327	/* Disable DMA IRQ */
328	saa_clearl(SAA7134_IRQ1,
329			SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
330
331	/* Disable TS interface */
332	saa_clearb(SAA7134_TS_PARALLEL, 0x80);
333
334	dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
335			DMA_FROM_DEVICE);
336	dma_unmap_page(&dev->pci->dev, saa->bottom_dma, PAGE_SIZE,
337			DMA_FROM_DEVICE);
338
339	return 0;
340}
341
342static int saa7134_go7007_send_firmware(struct go7007 *go, u8 *data, int len)
343{
344	struct saa7134_go7007 *saa = go->hpi_context;
345	struct saa7134_dev *dev = saa->dev;
346	u16 status_reg;
347	int i;
348
349#ifdef GO7007_HPI_DEBUG
350	pr_debug("saa7134-go7007: DownloadBuffer sending %d bytes\n", len);
351#endif
352
353	while (len > 0) {
354		i = len > 64 ? 64 : len;
355		saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
356		saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_INIT_BUFFER);
357		saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
358		saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
359		while (i-- > 0) {
360			saa_writeb(SAA7134_GPIO_GPSTATUS0, *data);
361			saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
362			saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
363			++data;
364			--len;
365		}
366		for (i = 0; i < 100; ++i) {
367			gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
368			if (!(status_reg & 0x0002))
369				break;
370		}
371		if (i == 100) {
372			pr_err("saa7134-go7007: device is hung, status reg = 0x%04x\n",
373			       status_reg);
374			return -1;
375		}
376	}
377	return 0;
378}
379
380static struct go7007_hpi_ops saa7134_go7007_hpi_ops = {
381	.interface_reset	= saa7134_go7007_interface_reset,
382	.write_interrupt	= saa7134_go7007_write_interrupt,
383	.read_interrupt		= saa7134_go7007_read_interrupt,
384	.stream_start		= saa7134_go7007_stream_start,
385	.stream_stop		= saa7134_go7007_stream_stop,
386	.send_firmware		= saa7134_go7007_send_firmware,
387};
388MODULE_FIRMWARE("go7007/go7007tv.bin");
389
390/* --------------------------------------------------------------------------*/
391
392static int saa7134_go7007_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
393{
394#if 0
395	struct saa7134_go7007 *saa = to_state(sd);
396	struct saa7134_dev *dev = saa->dev;
397
398	return saa7134_s_std_internal(dev, NULL, norm);
399#else
400	return 0;
401#endif
402}
403
404static const struct v4l2_subdev_video_ops saa7134_go7007_video_ops = {
405	.s_std = saa7134_go7007_s_std,
406};
407
408static const struct v4l2_subdev_ops saa7134_go7007_sd_ops = {
409	.video = &saa7134_go7007_video_ops,
410};
411
412/* --------------------------------------------------------------------------*/
413
414
415/********************* Add/remove functions *********************/
416
417static int saa7134_go7007_init(struct saa7134_dev *dev)
418{
419	struct go7007 *go;
420	struct saa7134_go7007 *saa;
421	struct v4l2_subdev *sd;
422
423	pr_debug("saa7134-go7007: probing new SAA713X board\n");
424
425	go = go7007_alloc(&board_voyager, &dev->pci->dev);
426	if (go == NULL)
427		return -ENOMEM;
428
429	saa = kzalloc(sizeof(struct saa7134_go7007), GFP_KERNEL);
430	if (saa == NULL) {
431		kfree(go);
432		return -ENOMEM;
433	}
434
435	go->board_id = GO7007_BOARDID_PCI_VOYAGER;
436	snprintf(go->bus_info, sizeof(go->bus_info), "PCI:%s", pci_name(dev->pci));
437	strlcpy(go->name, saa7134_boards[dev->board].name, sizeof(go->name));
438	go->hpi_ops = &saa7134_go7007_hpi_ops;
439	go->hpi_context = saa;
440	saa->dev = dev;
441
442	/* Init the subdevice interface */
443	sd = &saa->sd;
444	v4l2_subdev_init(sd, &saa7134_go7007_sd_ops);
445	v4l2_set_subdevdata(sd, saa);
446	strncpy(sd->name, "saa7134-go7007", sizeof(sd->name));
447
448	/* Allocate a couple pages for receiving the compressed stream */
449	saa->top = (u8 *)get_zeroed_page(GFP_KERNEL);
450	if (!saa->top)
451		goto allocfail;
452	saa->bottom = (u8 *)get_zeroed_page(GFP_KERNEL);
453	if (!saa->bottom)
454		goto allocfail;
455
456	/* Boot the GO7007 */
457	if (go7007_boot_encoder(go, go->board_info->flags &
458					GO7007_BOARD_USE_ONBOARD_I2C) < 0)
459		goto allocfail;
460
461	/* Do any final GO7007 initialization, then register the
462	 * V4L2 and ALSA interfaces */
463	if (go7007_register_encoder(go, go->board_info->num_i2c_devs) < 0)
464		goto allocfail;
465
466	/* Register the subdevice interface with the go7007 device */
467	if (v4l2_device_register_subdev(&go->v4l2_dev, sd) < 0)
468		pr_info("saa7134-go7007: register subdev failed\n");
469
470	dev->empress_dev = &go->vdev;
471
472	go->status = STATUS_ONLINE;
473	return 0;
474
475allocfail:
476	if (saa->top)
477		free_page((unsigned long)saa->top);
478	if (saa->bottom)
479		free_page((unsigned long)saa->bottom);
480	kfree(saa);
481	kfree(go);
482	return -ENOMEM;
483}
484
485static int saa7134_go7007_fini(struct saa7134_dev *dev)
486{
487	struct go7007 *go;
488	struct saa7134_go7007 *saa;
489
490	if (NULL == dev->empress_dev)
491		return 0;
492
493	go = video_get_drvdata(dev->empress_dev);
494	if (go->audio_enabled)
495		go7007_snd_remove(go);
496
497	saa = go->hpi_context;
498	go->status = STATUS_SHUTDOWN;
499	free_page((unsigned long)saa->top);
500	free_page((unsigned long)saa->bottom);
501	v4l2_device_unregister_subdev(&saa->sd);
502	kfree(saa);
503	video_unregister_device(&go->vdev);
504
505	v4l2_device_put(&go->v4l2_dev);
506	dev->empress_dev = NULL;
507
508	return 0;
509}
510
511static struct saa7134_mpeg_ops saa7134_go7007_ops = {
512	.type          = SAA7134_MPEG_GO7007,
513	.init          = saa7134_go7007_init,
514	.fini          = saa7134_go7007_fini,
515	.irq_ts_done   = saa7134_go7007_irq_ts_done,
516};
517
518static int __init saa7134_go7007_mod_init(void)
519{
520	return saa7134_ts_register(&saa7134_go7007_ops);
521}
522
523static void __exit saa7134_go7007_mod_cleanup(void)
524{
525	saa7134_ts_unregister(&saa7134_go7007_ops);
526}
527
528module_init(saa7134_go7007_mod_init);
529module_exit(saa7134_go7007_mod_cleanup);
530
531MODULE_LICENSE("GPL v2");
532