2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
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.
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
15 * GNU General Public License for more details.
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.
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/delay.h>
29 /* The Bridge API needs to understand register widths (in bytes) for the
30 * attached I2C devices, so we can simplify the virtual i2c mechansms
31 * and keep the -i2c.c implementation clean.
34 #define REGLEN_16bit 2
36 struct saa7164_board saa7164_boards[] = {
37 [SAA7164_BOARD_UNKNOWN] = {
38 /* Bridge will not load any firmware, without knowing
39 * the rev this would be fatal. */
42 [SAA7164_BOARD_UNKNOWN_REV2] = {
43 /* Bridge will load the v2 f/w and dump descriptors */
44 /* Required during new board bringup */
45 .name = "Generic Rev2",
46 .chiprev = SAA7164_CHIP_REV2,
48 [SAA7164_BOARD_UNKNOWN_REV3] = {
49 /* Bridge will load the v2 f/w and dump descriptors */
50 /* Required during new board bringup */
51 .name = "Generic Rev3",
52 .chiprev = SAA7164_CHIP_REV3,
54 [SAA7164_BOARD_HAUPPAUGE_HVR2200] = {
55 .name = "Hauppauge WinTV-HVR2200",
56 .porta = SAA7164_MPEG_DVB,
57 .portb = SAA7164_MPEG_DVB,
58 .portc = SAA7164_MPEG_ENCODER,
59 .portd = SAA7164_MPEG_ENCODER,
60 .porte = SAA7164_MPEG_VBI,
61 .portf = SAA7164_MPEG_VBI,
62 .chiprev = SAA7164_CHIP_REV3,
65 .type = SAA7164_UNIT_EEPROM,
67 .i2c_bus_nr = SAA7164_I2C_BUS_0,
68 .i2c_bus_addr = 0xa0 >> 1,
69 .i2c_reg_len = REGLEN_8bit,
72 .type = SAA7164_UNIT_TUNER,
74 .i2c_bus_nr = SAA7164_I2C_BUS_1,
75 .i2c_bus_addr = 0xc0 >> 1,
76 .i2c_reg_len = REGLEN_8bit,
79 .type = SAA7164_UNIT_TUNER,
81 .i2c_bus_nr = SAA7164_I2C_BUS_2,
82 .i2c_bus_addr = 0xc0 >> 1,
83 .i2c_reg_len = REGLEN_8bit,
86 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
88 .i2c_bus_nr = SAA7164_I2C_BUS_1,
89 .i2c_bus_addr = 0x10 >> 1,
90 .i2c_reg_len = REGLEN_8bit,
93 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
95 .i2c_bus_nr = SAA7164_I2C_BUS_2,
96 .i2c_bus_addr = 0x12 >> 1,
97 .i2c_reg_len = REGLEN_8bit,
100 [SAA7164_BOARD_HAUPPAUGE_HVR2200_2] = {
101 .name = "Hauppauge WinTV-HVR2200",
102 .porta = SAA7164_MPEG_DVB,
103 .portb = SAA7164_MPEG_DVB,
104 .portc = SAA7164_MPEG_ENCODER,
105 .portd = SAA7164_MPEG_ENCODER,
106 .porte = SAA7164_MPEG_VBI,
107 .portf = SAA7164_MPEG_VBI,
108 .chiprev = SAA7164_CHIP_REV2,
111 .type = SAA7164_UNIT_EEPROM,
113 .i2c_bus_nr = SAA7164_I2C_BUS_0,
114 .i2c_bus_addr = 0xa0 >> 1,
115 .i2c_reg_len = REGLEN_8bit,
118 .type = SAA7164_UNIT_TUNER,
119 .name = "TDA18271-1",
120 .i2c_bus_nr = SAA7164_I2C_BUS_1,
121 .i2c_bus_addr = 0xc0 >> 1,
122 .i2c_reg_len = REGLEN_8bit,
125 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
126 .name = "TDA10048-1",
127 .i2c_bus_nr = SAA7164_I2C_BUS_1,
128 .i2c_bus_addr = 0x10 >> 1,
129 .i2c_reg_len = REGLEN_8bit,
132 .type = SAA7164_UNIT_TUNER,
133 .name = "TDA18271-2",
134 .i2c_bus_nr = SAA7164_I2C_BUS_2,
135 .i2c_bus_addr = 0xc0 >> 1,
136 .i2c_reg_len = REGLEN_8bit,
139 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
140 .name = "TDA10048-2",
141 .i2c_bus_nr = SAA7164_I2C_BUS_2,
142 .i2c_bus_addr = 0x12 >> 1,
143 .i2c_reg_len = REGLEN_8bit,
146 [SAA7164_BOARD_HAUPPAUGE_HVR2200_3] = {
147 .name = "Hauppauge WinTV-HVR2200",
148 .porta = SAA7164_MPEG_DVB,
149 .portb = SAA7164_MPEG_DVB,
150 .portc = SAA7164_MPEG_ENCODER,
151 .portd = SAA7164_MPEG_ENCODER,
152 .porte = SAA7164_MPEG_VBI,
153 .portf = SAA7164_MPEG_VBI,
154 .chiprev = SAA7164_CHIP_REV2,
157 .type = SAA7164_UNIT_EEPROM,
159 .i2c_bus_nr = SAA7164_I2C_BUS_0,
160 .i2c_bus_addr = 0xa0 >> 1,
161 .i2c_reg_len = REGLEN_8bit,
164 .type = SAA7164_UNIT_TUNER,
165 .name = "TDA18271-1",
166 .i2c_bus_nr = SAA7164_I2C_BUS_1,
167 .i2c_bus_addr = 0xc0 >> 1,
168 .i2c_reg_len = REGLEN_8bit,
171 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
173 .i2c_bus_nr = SAA7164_I2C_BUS_1,
174 .i2c_bus_addr = 0x84 >> 1,
175 .i2c_reg_len = REGLEN_8bit,
178 .type = SAA7164_UNIT_TUNER,
179 .name = "TDA18271-2",
180 .i2c_bus_nr = SAA7164_I2C_BUS_2,
181 .i2c_bus_addr = 0xc0 >> 1,
182 .i2c_reg_len = REGLEN_8bit,
185 .type = SAA7164_UNIT_ANALOG_DEMODULATOR,
187 .i2c_bus_nr = SAA7164_I2C_BUS_2,
188 .i2c_bus_addr = 0x84 >> 1,
189 .i2c_reg_len = REGLEN_8bit,
192 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
193 .name = "TDA10048-1",
194 .i2c_bus_nr = SAA7164_I2C_BUS_1,
195 .i2c_bus_addr = 0x10 >> 1,
196 .i2c_reg_len = REGLEN_8bit,
199 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
200 .name = "TDA10048-2",
201 .i2c_bus_nr = SAA7164_I2C_BUS_2,
202 .i2c_bus_addr = 0x12 >> 1,
203 .i2c_reg_len = REGLEN_8bit,
206 [SAA7164_BOARD_HAUPPAUGE_HVR2250] = {
207 .name = "Hauppauge WinTV-HVR2250",
208 .porta = SAA7164_MPEG_DVB,
209 .portb = SAA7164_MPEG_DVB,
210 .portc = SAA7164_MPEG_ENCODER,
211 .portd = SAA7164_MPEG_ENCODER,
212 .portc = SAA7164_MPEG_ENCODER,
213 .portd = SAA7164_MPEG_ENCODER,
214 .porte = SAA7164_MPEG_VBI,
215 .portf = SAA7164_MPEG_VBI,
216 .chiprev = SAA7164_CHIP_REV3,
219 .type = SAA7164_UNIT_EEPROM,
221 .i2c_bus_nr = SAA7164_I2C_BUS_0,
222 .i2c_bus_addr = 0xa0 >> 1,
223 .i2c_reg_len = REGLEN_8bit,
226 .type = SAA7164_UNIT_TUNER,
227 .name = "TDA18271-1",
228 .i2c_bus_nr = SAA7164_I2C_BUS_1,
229 .i2c_bus_addr = 0xc0 >> 1,
230 .i2c_reg_len = REGLEN_8bit,
233 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
234 .name = "CX24228/S5H1411-1 (TOP)",
235 .i2c_bus_nr = SAA7164_I2C_BUS_1,
236 .i2c_bus_addr = 0x32 >> 1,
237 .i2c_reg_len = REGLEN_8bit,
240 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
241 .name = "CX24228/S5H1411-1 (QAM)",
242 .i2c_bus_nr = SAA7164_I2C_BUS_1,
243 .i2c_bus_addr = 0x34 >> 1,
244 .i2c_reg_len = REGLEN_8bit,
247 .type = SAA7164_UNIT_TUNER,
248 .name = "TDA18271-2",
249 .i2c_bus_nr = SAA7164_I2C_BUS_2,
250 .i2c_bus_addr = 0xc0 >> 1,
251 .i2c_reg_len = REGLEN_8bit,
254 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
255 .name = "CX24228/S5H1411-2 (TOP)",
256 .i2c_bus_nr = SAA7164_I2C_BUS_2,
257 .i2c_bus_addr = 0x32 >> 1,
258 .i2c_reg_len = REGLEN_8bit,
261 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
262 .name = "CX24228/S5H1411-2 (QAM)",
263 .i2c_bus_nr = SAA7164_I2C_BUS_2,
264 .i2c_bus_addr = 0x34 >> 1,
265 .i2c_reg_len = REGLEN_8bit,
268 [SAA7164_BOARD_HAUPPAUGE_HVR2250_2] = {
269 .name = "Hauppauge WinTV-HVR2250",
270 .porta = SAA7164_MPEG_DVB,
271 .portb = SAA7164_MPEG_DVB,
272 .portc = SAA7164_MPEG_ENCODER,
273 .portd = SAA7164_MPEG_ENCODER,
274 .porte = SAA7164_MPEG_VBI,
275 .portf = SAA7164_MPEG_VBI,
276 .porte = SAA7164_MPEG_VBI,
277 .portf = SAA7164_MPEG_VBI,
278 .chiprev = SAA7164_CHIP_REV3,
281 .type = SAA7164_UNIT_EEPROM,
283 .i2c_bus_nr = SAA7164_I2C_BUS_0,
284 .i2c_bus_addr = 0xa0 >> 1,
285 .i2c_reg_len = REGLEN_8bit,
288 .type = SAA7164_UNIT_TUNER,
289 .name = "TDA18271-1",
290 .i2c_bus_nr = SAA7164_I2C_BUS_1,
291 .i2c_bus_addr = 0xc0 >> 1,
292 .i2c_reg_len = REGLEN_8bit,
295 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
296 .name = "CX24228/S5H1411-1 (TOP)",
297 .i2c_bus_nr = SAA7164_I2C_BUS_1,
298 .i2c_bus_addr = 0x32 >> 1,
299 .i2c_reg_len = REGLEN_8bit,
302 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
303 .name = "CX24228/S5H1411-1 (QAM)",
304 .i2c_bus_nr = SAA7164_I2C_BUS_1,
305 .i2c_bus_addr = 0x34 >> 1,
306 .i2c_reg_len = REGLEN_8bit,
309 .type = SAA7164_UNIT_TUNER,
310 .name = "TDA18271-2",
311 .i2c_bus_nr = SAA7164_I2C_BUS_2,
312 .i2c_bus_addr = 0xc0 >> 1,
313 .i2c_reg_len = REGLEN_8bit,
316 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
317 .name = "CX24228/S5H1411-2 (TOP)",
318 .i2c_bus_nr = SAA7164_I2C_BUS_2,
319 .i2c_bus_addr = 0x32 >> 1,
320 .i2c_reg_len = REGLEN_8bit,
323 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
324 .name = "CX24228/S5H1411-2 (QAM)",
325 .i2c_bus_nr = SAA7164_I2C_BUS_2,
326 .i2c_bus_addr = 0x34 >> 1,
327 .i2c_reg_len = REGLEN_8bit,
330 [SAA7164_BOARD_HAUPPAUGE_HVR2250_3] = {
331 .name = "Hauppauge WinTV-HVR2250",
332 .porta = SAA7164_MPEG_DVB,
333 .portb = SAA7164_MPEG_DVB,
334 .portc = SAA7164_MPEG_ENCODER,
335 .portd = SAA7164_MPEG_ENCODER,
336 .porte = SAA7164_MPEG_VBI,
337 .portf = SAA7164_MPEG_VBI,
338 .chiprev = SAA7164_CHIP_REV3,
341 .type = SAA7164_UNIT_EEPROM,
343 .i2c_bus_nr = SAA7164_I2C_BUS_0,
344 .i2c_bus_addr = 0xa0 >> 1,
345 .i2c_reg_len = REGLEN_8bit,
348 .type = SAA7164_UNIT_TUNER,
349 .name = "TDA18271-1",
350 .i2c_bus_nr = SAA7164_I2C_BUS_1,
351 .i2c_bus_addr = 0xc0 >> 1,
352 .i2c_reg_len = REGLEN_8bit,
355 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
356 .name = "CX24228/S5H1411-1 (TOP)",
357 .i2c_bus_nr = SAA7164_I2C_BUS_1,
358 .i2c_bus_addr = 0x32 >> 1,
359 .i2c_reg_len = REGLEN_8bit,
362 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
363 .name = "CX24228/S5H1411-1 (QAM)",
364 .i2c_bus_nr = SAA7164_I2C_BUS_1,
365 .i2c_bus_addr = 0x34 >> 1,
366 .i2c_reg_len = REGLEN_8bit,
369 .type = SAA7164_UNIT_TUNER,
370 .name = "TDA18271-2",
371 .i2c_bus_nr = SAA7164_I2C_BUS_2,
372 .i2c_bus_addr = 0xc0 >> 1,
373 .i2c_reg_len = REGLEN_8bit,
376 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
377 .name = "CX24228/S5H1411-2 (TOP)",
378 .i2c_bus_nr = SAA7164_I2C_BUS_2,
379 .i2c_bus_addr = 0x32 >> 1,
380 .i2c_reg_len = REGLEN_8bit,
383 .type = SAA7164_UNIT_DIGITAL_DEMODULATOR,
384 .name = "CX24228/S5H1411-2 (QAM)",
385 .i2c_bus_nr = SAA7164_I2C_BUS_2,
386 .i2c_bus_addr = 0x34 >> 1,
387 .i2c_reg_len = REGLEN_8bit,
391 const unsigned int saa7164_bcount = ARRAY_SIZE(saa7164_boards);
393 /* ------------------------------------------------------------------ */
394 /* PCI subsystem IDs */
396 struct saa7164_subid saa7164_subids[] = {
400 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250,
404 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250,
408 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200,
412 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200_2,
416 .card = SAA7164_BOARD_HAUPPAUGE_HVR2200_3,
420 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250_3,
424 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250_2,
428 .card = SAA7164_BOARD_HAUPPAUGE_HVR2250_2,
431 const unsigned int saa7164_idcount = ARRAY_SIZE(saa7164_subids);
433 void saa7164_card_list(struct saa7164_dev *dev)
437 if (0 == dev->pci->subsystem_vendor &&
438 0 == dev->pci->subsystem_device) {
440 "%s: Board has no valid PCIe Subsystem ID and can't\n"
441 "%s: be autodetected. Pass card=<n> insmod option to\n"
442 "%s: workaround that. Send complaints to the vendor\n"
443 "%s: of the TV card. Best regards,\n"
445 dev->name, dev->name, dev->name, dev->name, dev->name);
448 "%s: Your board isn't known (yet) to the driver.\n"
449 "%s: Try to pick one of the existing card configs via\n"
450 "%s: card=<n> insmod option. Updating to the latest\n"
451 "%s: version might help as well.\n",
452 dev->name, dev->name, dev->name, dev->name);
455 printk(KERN_ERR "%s: Here are valid choices for the card=<n> insmod "
456 "option:\n", dev->name);
458 for (i = 0; i < saa7164_bcount; i++)
459 printk(KERN_ERR "%s: card=%d -> %s\n",
460 dev->name, i, saa7164_boards[i].name);
463 /* TODO: clean this define up into the -cards.c structs */
464 #define PCIEBRIDGE_UNITID 2
466 void saa7164_gpio_setup(struct saa7164_dev *dev)
468 switch (dev->board) {
469 case SAA7164_BOARD_HAUPPAUGE_HVR2200:
470 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
471 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
472 case SAA7164_BOARD_HAUPPAUGE_HVR2250:
473 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
474 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
476 GPIO 2: s5h1411 / tda10048-1 demod reset
477 GPIO 3: s5h1411 / tda10048-2 demod reset
478 GPIO 7: IRBlaster Zilog reset
481 /* Reset parts by going in and out of reset */
482 saa7164_api_clear_gpiobit(dev, PCIEBRIDGE_UNITID, 2);
483 saa7164_api_clear_gpiobit(dev, PCIEBRIDGE_UNITID, 3);
487 saa7164_api_set_gpiobit(dev, PCIEBRIDGE_UNITID, 2);
488 saa7164_api_set_gpiobit(dev, PCIEBRIDGE_UNITID, 3);
493 static void hauppauge_eeprom(struct saa7164_dev *dev, u8 *eeprom_data)
497 /* TODO: Assumption: eeprom on bus 0 */
498 tveeprom_hauppauge_analog(&dev->i2c_bus[0].i2c_client, &tv,
501 /* Make sure we support the board model */
504 /* Development board - Limit circulation */
505 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
506 * ATSC/QAM (TDA18271/S5H1411) and basic analog, no IR, FM */
508 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
509 * ATSC/QAM (TDA18271/S5H1411) and basic analog, MCE CIR, FM */
512 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
513 * ATSC/QAM (TDA18271/S5H1411) and basic analog, no IR, FM */
516 /* WinTV-HVR2250 (PCIe, Retail, full-height bracket)
517 * ATSC/QAM (TDA18271/S5H1411) and basic analog, FM */
521 /* WinTV-HVR2200 (PCIe, Retail, full-height)
522 * DVB-T (TDA18271/TDA10048) and basic analog, no IR */
525 /* WinTV-HVR2200 (PCIe, Retail, half-height)
526 * DVB-T (TDA18271/TDA10048) and basic analog, no IR */
529 printk(KERN_ERR "%s: Warning: Unknown Hauppauge model #%d\n",
530 dev->name, tv.model);
534 printk(KERN_INFO "%s: Hauppauge eeprom: model=%d\n", dev->name,
538 void saa7164_card_setup(struct saa7164_dev *dev)
540 static u8 eeprom[256];
542 if (dev->i2c_bus[0].i2c_rc == 0) {
543 if (saa7164_api_read_eeprom(dev, &eeprom[0],
548 switch (dev->board) {
549 case SAA7164_BOARD_HAUPPAUGE_HVR2200:
550 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
551 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
552 case SAA7164_BOARD_HAUPPAUGE_HVR2250:
553 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
554 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
555 hauppauge_eeprom(dev, &eeprom[0]);
560 /* With most other drivers, the kernel expects to communicate with subdrivers
561 * through i2c. This bridge does not allow that, it does not expose any direct
562 * access to I2C. Instead we have to communicate through the device f/w for
563 * register access to 'processing units'. Each unit has a unique
564 * id, regardless of how the physical implementation occurs across
565 * the three physical i2c busses. The being said if we want leverge of
566 * the existing kernel drivers for tuners and demods we have to 'speak i2c',
567 * to this bridge implements 3 virtual i2c buses. This is a helper function
570 * Description: Translate the kernels notion of an i2c address and bus into
571 * the appropriate unitid.
573 int saa7164_i2caddr_to_unitid(struct saa7164_i2c *bus, int addr)
575 /* For a given bus and i2c device address, return the saa7164 unique
576 * unitid. < 0 on error */
578 struct saa7164_dev *dev = bus->dev;
579 struct saa7164_unit *unit;
582 for (i = 0; i < SAA7164_MAX_UNITS; i++) {
583 unit = &saa7164_boards[dev->board].unit[i];
585 if (unit->type == SAA7164_UNIT_UNDEFINED)
587 if ((bus->nr == unit->i2c_bus_nr) &&
588 (addr == unit->i2c_bus_addr))
595 /* The 7164 API needs to know the i2c register length in advance.
596 * this is a helper function. Based on a specific chip addr and bus return the
599 int saa7164_i2caddr_to_reglen(struct saa7164_i2c *bus, int addr)
601 /* For a given bus and i2c device address, return the
602 * saa7164 registry address width. < 0 on error
605 struct saa7164_dev *dev = bus->dev;
606 struct saa7164_unit *unit;
609 for (i = 0; i < SAA7164_MAX_UNITS; i++) {
610 unit = &saa7164_boards[dev->board].unit[i];
612 if (unit->type == SAA7164_UNIT_UNDEFINED)
615 if ((bus->nr == unit->i2c_bus_nr) &&
616 (addr == unit->i2c_bus_addr))
617 return unit->i2c_reg_len;
622 /* TODO: implement a 'findeeprom' functio like the above and fix any other
623 * eeprom related todo's in -api.c.
626 /* Translate a unitid into a x readable device name, for display purposes. */
627 char *saa7164_unitid_name(struct saa7164_dev *dev, u8 unitid)
629 char *undefed = "UNDEFINED";
630 char *bridge = "BRIDGE";
631 struct saa7164_unit *unit;
637 for (i = 0; i < SAA7164_MAX_UNITS; i++) {
638 unit = &saa7164_boards[dev->board].unit[i];
640 if (unit->type == SAA7164_UNIT_UNDEFINED)
643 if (unitid == unit->id)