1 /*
2  *  Driver for the Auvitek USB bridge
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "au0828.h"
23 #include "au0828-cards.h"
24 #include "au8522.h"
25 #include "media/tuner.h"
26 #include "media/v4l2-common.h"
27 
28 static void hvr950q_cs5340_audio(void *priv, int enable)
29 {
30 	/* Because the HVR-950q shares an i2s bus between the cs5340 and the
31 	   au8522, we need to hold cs5340 in reset when using the au8522 */
32 	struct au0828_dev *dev = priv;
33 	if (enable == 1)
34 		au0828_set(dev, REG_000, 0x10);
35 	else
36 		au0828_clear(dev, REG_000, 0x10);
37 }
38 
39 struct au0828_board au0828_boards[] = {
40 	[AU0828_BOARD_UNKNOWN] = {
41 		.name	= "Unknown board",
42 		.tuner_type = UNSET,
43 		.tuner_addr = ADDR_UNSET,
44 	},
45 	[AU0828_BOARD_HAUPPAUGE_HVR850] = {
46 		.name	= "Hauppauge HVR850",
47 		.tuner_type = TUNER_XC5000,
48 		.tuner_addr = 0x61,
49 		.i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
50 		.input = {
51 			{
52 				.type = AU0828_VMUX_TELEVISION,
53 				.vmux = AU8522_COMPOSITE_CH4_SIF,
54 				.amux = AU8522_AUDIO_SIF,
55 			},
56 			{
57 				.type = AU0828_VMUX_COMPOSITE,
58 				.vmux = AU8522_COMPOSITE_CH1,
59 				.amux = AU8522_AUDIO_NONE,
60 				.audio_setup = hvr950q_cs5340_audio,
61 			},
62 			{
63 				.type = AU0828_VMUX_SVIDEO,
64 				.vmux = AU8522_SVIDEO_CH13,
65 				.amux = AU8522_AUDIO_NONE,
66 				.audio_setup = hvr950q_cs5340_audio,
67 			},
68 		},
69 	},
70 	[AU0828_BOARD_HAUPPAUGE_HVR950Q] = {
71 		.name	= "Hauppauge HVR950Q",
72 		.tuner_type = TUNER_XC5000,
73 		.tuner_addr = 0x61,
74 		.has_ir_i2c = 1,
75 		/* The au0828 hardware i2c implementation does not properly
76 		   support the xc5000's i2c clock stretching.  So we need to
77 		   lower the clock frequency enough where the 15us clock
78 		   stretch fits inside of a normal clock cycle, or else the
79 		   au0828 fails to set the STOP bit.  A 30 KHz clock puts the
80 		   clock pulse width at 18us */
81 		.i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
82 		.input = {
83 			{
84 				.type = AU0828_VMUX_TELEVISION,
85 				.vmux = AU8522_COMPOSITE_CH4_SIF,
86 				.amux = AU8522_AUDIO_SIF,
87 			},
88 			{
89 				.type = AU0828_VMUX_COMPOSITE,
90 				.vmux = AU8522_COMPOSITE_CH1,
91 				.amux = AU8522_AUDIO_NONE,
92 				.audio_setup = hvr950q_cs5340_audio,
93 			},
94 			{
95 				.type = AU0828_VMUX_SVIDEO,
96 				.vmux = AU8522_SVIDEO_CH13,
97 				.amux = AU8522_AUDIO_NONE,
98 				.audio_setup = hvr950q_cs5340_audio,
99 			},
100 		},
101 	},
102 	[AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL] = {
103 		.name	= "Hauppauge HVR950Q rev xxF8",
104 		.tuner_type = UNSET,
105 		.tuner_addr = ADDR_UNSET,
106 		.i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
107 	},
108 	[AU0828_BOARD_DVICO_FUSIONHDTV7] = {
109 		.name	= "DViCO FusionHDTV USB",
110 		.tuner_type = UNSET,
111 		.tuner_addr = ADDR_UNSET,
112 		.i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
113 	},
114 	[AU0828_BOARD_HAUPPAUGE_WOODBURY] = {
115 		.name = "Hauppauge Woodbury",
116 		.tuner_type = UNSET,
117 		.tuner_addr = ADDR_UNSET,
118 		.i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
119 	},
120 };
121 
122 /* Tuner callback function for au0828 boards. Currently only needed
123  * for HVR1500Q, which has an xc5000 tuner.
124  */
125 int au0828_tuner_callback(void *priv, int component, int command, int arg)
126 {
127 	struct au0828_dev *dev = priv;
128 
129 	dprintk(1, "%s()\n", __func__);
130 
131 	switch (dev->boardnr) {
132 	case AU0828_BOARD_HAUPPAUGE_HVR850:
133 	case AU0828_BOARD_HAUPPAUGE_HVR950Q:
134 	case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
135 	case AU0828_BOARD_DVICO_FUSIONHDTV7:
136 		if (command == 0) {
137 			/* Tuner Reset Command from xc5000 */
138 			/* Drive the tuner into reset and out */
139 			au0828_clear(dev, REG_001, 2);
140 			mdelay(10);
141 			au0828_set(dev, REG_001, 2);
142 			mdelay(10);
143 			return 0;
144 		} else {
145 			printk(KERN_ERR
146 				"%s(): Unknown command.\n", __func__);
147 			return -EINVAL;
148 		}
149 		break;
150 	}
151 
152 	return 0; /* Should never be here */
153 }
154 
155 static void hauppauge_eeprom(struct au0828_dev *dev, u8 *eeprom_data)
156 {
157 	struct tveeprom tv;
158 
159 	tveeprom_hauppauge_analog(&dev->i2c_client, &tv, eeprom_data);
160 	dev->board.tuner_type = tv.tuner_type;
161 
162 	/* Make sure we support the board model */
163 	switch (tv.model) {
164 	case 72000: /* WinTV-HVR950q (Retail, IR, ATSC/QAM */
165 	case 72001: /* WinTV-HVR950q (Retail, IR, ATSC/QAM and analog video */
166 	case 72101: /* WinTV-HVR950q (Retail, IR, ATSC/QAM and analog video */
167 	case 72201: /* WinTV-HVR950q (OEM, IR, ATSC/QAM and analog video */
168 	case 72211: /* WinTV-HVR950q (OEM, IR, ATSC/QAM and analog video */
169 	case 72221: /* WinTV-HVR950q (OEM, IR, ATSC/QAM and analog video */
170 	case 72231: /* WinTV-HVR950q (OEM, IR, ATSC/QAM and analog video */
171 	case 72241: /* WinTV-HVR950q (OEM, No IR, ATSC/QAM and analog video */
172 	case 72251: /* WinTV-HVR950q (Retail, IR, ATSC/QAM and analog video */
173 	case 72261: /* WinTV-HVR950q (OEM, No IR, ATSC/QAM and analog video */
174 	case 72271: /* WinTV-HVR950q (OEM, No IR, ATSC/QAM and analog video */
175 	case 72281: /* WinTV-HVR950q (OEM, No IR, ATSC/QAM and analog video */
176 	case 72301: /* WinTV-HVR850 (Retail, IR, ATSC and analog video */
177 	case 72500: /* WinTV-HVR950q (OEM, No IR, ATSC/QAM */
178 		break;
179 	default:
180 		printk(KERN_WARNING "%s: warning: "
181 		       "unknown hauppauge model #%d\n", __func__, tv.model);
182 		break;
183 	}
184 
185 	printk(KERN_INFO "%s: hauppauge eeprom: model=%d\n",
186 	       __func__, tv.model);
187 }
188 
189 void au0828_card_analog_fe_setup(struct au0828_dev *dev);
190 
191 void au0828_card_setup(struct au0828_dev *dev)
192 {
193 	static u8 eeprom[256];
194 
195 	dprintk(1, "%s()\n", __func__);
196 
197 	dev->board = au0828_boards[dev->boardnr];
198 
199 	if (dev->i2c_rc == 0) {
200 		dev->i2c_client.addr = 0xa0 >> 1;
201 		tveeprom_read(&dev->i2c_client, eeprom, sizeof(eeprom));
202 	}
203 
204 	switch (dev->boardnr) {
205 	case AU0828_BOARD_HAUPPAUGE_HVR850:
206 	case AU0828_BOARD_HAUPPAUGE_HVR950Q:
207 	case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
208 	case AU0828_BOARD_HAUPPAUGE_WOODBURY:
209 		if (dev->i2c_rc == 0)
210 			hauppauge_eeprom(dev, eeprom+0xa0);
211 		break;
212 	}
213 
214 	au0828_card_analog_fe_setup(dev);
215 }
216 
217 void au0828_card_analog_fe_setup(struct au0828_dev *dev)
218 {
219 #ifdef CONFIG_VIDEO_AU0828_V4L2
220 	struct tuner_setup tun_setup;
221 	struct v4l2_subdev *sd;
222 	unsigned int mode_mask = T_ANALOG_TV;
223 
224 	if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED) {
225 		/* Load the analog demodulator driver (note this would need to
226 		   be abstracted out if we ever need to support a different
227 		   demod) */
228 		sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
229 				"au8522", 0x8e >> 1, NULL);
230 		if (sd == NULL)
231 			printk(KERN_ERR "analog subdev registration failed\n");
232 	}
233 
234 	/* Setup tuners */
235 	if (dev->board.tuner_type != TUNER_ABSENT) {
236 		/* Load the tuner module, which does the attach */
237 		sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
238 				"tuner", dev->board.tuner_addr, NULL);
239 		if (sd == NULL)
240 			printk(KERN_ERR "tuner subdev registration fail\n");
241 
242 		tun_setup.mode_mask      = mode_mask;
243 		tun_setup.type           = dev->board.tuner_type;
244 		tun_setup.addr           = dev->board.tuner_addr;
245 		tun_setup.tuner_callback = au0828_tuner_callback;
246 		v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_type_addr,
247 				     &tun_setup);
248 	}
249 #endif
250 }
251 
252 /*
253  * The bridge has between 8 and 12 gpios.
254  * Regs 1 and 0 deal with output enables.
255  * Regs 3 and 2 deal with direction.
256  */
257 void au0828_gpio_setup(struct au0828_dev *dev)
258 {
259 	dprintk(1, "%s()\n", __func__);
260 
261 	switch (dev->boardnr) {
262 	case AU0828_BOARD_HAUPPAUGE_HVR850:
263 	case AU0828_BOARD_HAUPPAUGE_HVR950Q:
264 	case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
265 	case AU0828_BOARD_HAUPPAUGE_WOODBURY:
266 		/* GPIO's
267 		 * 4 - CS5340
268 		 * 5 - AU8522 Demodulator
269 		 * 6 - eeprom W/P
270 		 * 7 - power supply
271 		 * 9 - XC5000 Tuner
272 		 */
273 
274 		/* Set relevant GPIOs as outputs (leave the EEPROM W/P
275 		   as an input since we will never touch it and it has
276 		   a pullup) */
277 		au0828_write(dev, REG_003, 0x02);
278 		au0828_write(dev, REG_002, 0x80 | 0x20 | 0x10);
279 
280 		/* Into reset */
281 		au0828_write(dev, REG_001, 0x0);
282 		au0828_write(dev, REG_000, 0x0);
283 		msleep(50);
284 
285 		/* Bring power supply out of reset */
286 		au0828_write(dev, REG_000, 0x80);
287 		msleep(50);
288 
289 		/* Bring xc5000 and au8522 out of reset (leave the
290 		   cs5340 in reset until needed) */
291 		au0828_write(dev, REG_001, 0x02); /* xc5000 */
292 		au0828_write(dev, REG_000, 0x80 | 0x20); /* PS + au8522 */
293 
294 		msleep(250);
295 		break;
296 	case AU0828_BOARD_DVICO_FUSIONHDTV7:
297 		/* GPIO's
298 		 * 6 - ?
299 		 * 8 - AU8522 Demodulator
300 		 * 9 - XC5000 Tuner
301 		 */
302 
303 		/* Into reset */
304 		au0828_write(dev, REG_003, 0x02);
305 		au0828_write(dev, REG_002, 0xa0);
306 		au0828_write(dev, REG_001, 0x0);
307 		au0828_write(dev, REG_000, 0x0);
308 		msleep(100);
309 
310 		/* Out of reset */
311 		au0828_write(dev, REG_003, 0x02);
312 		au0828_write(dev, REG_002, 0xa0);
313 		au0828_write(dev, REG_001, 0x02);
314 		au0828_write(dev, REG_000, 0xa0);
315 		msleep(250);
316 		break;
317 	}
318 }
319 
320 /* table of devices that work with this driver */
321 struct usb_device_id au0828_usb_id_table[] = {
322 	{ USB_DEVICE(0x2040, 0x7200),
323 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
324 	{ USB_DEVICE(0x2040, 0x7240),
325 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR850 },
326 	{ USB_DEVICE(0x0fe9, 0xd620),
327 		.driver_info = AU0828_BOARD_DVICO_FUSIONHDTV7 },
328 	{ USB_DEVICE(0x2040, 0x7210),
329 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
330 	{ USB_DEVICE(0x2040, 0x7217),
331 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
332 	{ USB_DEVICE(0x2040, 0x721b),
333 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
334 	{ USB_DEVICE(0x2040, 0x721e),
335 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
336 	{ USB_DEVICE(0x2040, 0x721f),
337 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
338 	{ USB_DEVICE(0x2040, 0x7280),
339 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
340 	{ USB_DEVICE(0x0fd9, 0x0008),
341 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
342 	{ USB_DEVICE(0x2040, 0x7201),
343 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL },
344 	{ USB_DEVICE(0x2040, 0x7211),
345 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL },
346 	{ USB_DEVICE(0x2040, 0x7281),
347 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL },
348 	{ USB_DEVICE(0x05e1, 0x0480),
349 		.driver_info = AU0828_BOARD_HAUPPAUGE_WOODBURY },
350 	{ USB_DEVICE(0x2040, 0x8200),
351 		.driver_info = AU0828_BOARD_HAUPPAUGE_WOODBURY },
352 	{ USB_DEVICE(0x2040, 0x7260),
353 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
354 	{ USB_DEVICE(0x2040, 0x7213),
355 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
356 	{ USB_DEVICE(0x2040, 0x7270),
357 		.driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
358 	{ },
359 };
360 
361 MODULE_DEVICE_TABLE(usb, au0828_usb_id_table);
362