7dba17576edfd5e01a6c0812ecf5de9120d939ca
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / usb / em28xx / em28xx-dvb.c
1 /*
2  DVB device driver for em28xx
3
4  (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6  (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7         - Fixes for the driver to properly work with HVR-950
8         - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
9         - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
10
11  (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12
13  (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
14
15  Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
16         (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
17         (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
18
19  This program is free software; you can redistribute it and/or modify
20  it under the terms of the GNU General Public License as published by
21  the Free Software Foundation; either version 2 of the License.
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/usb.h>
27
28 #include "em28xx.h"
29 #include <media/v4l2-common.h>
30 #include <dvb_demux.h>
31 #include <dvb_net.h>
32 #include <dmxdev.h>
33 #include <media/tuner.h>
34 #include "tuner-simple.h"
35 #include <linux/gpio.h>
36
37 #include "lgdt330x.h"
38 #include "lgdt3305.h"
39 #include "zl10353.h"
40 #include "s5h1409.h"
41 #include "mt352.h"
42 #include "mt352_priv.h" /* FIXME */
43 #include "tda1002x.h"
44 #include "tda18271.h"
45 #include "s921.h"
46 #include "drxd.h"
47 #include "cxd2820r.h"
48 #include "tda18271c2dd.h"
49 #include "drxk.h"
50 #include "tda10071.h"
51 #include "a8293.h"
52 #include "qt1010.h"
53 #include "mb86a20s.h"
54 #include "m88ds3103.h"
55 #include "m88ts2022.h"
56
57 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
58 MODULE_LICENSE("GPL");
59 MODULE_DESCRIPTION(DRIVER_DESC " - digital TV interface");
60 MODULE_VERSION(EM28XX_VERSION);
61
62
63 static unsigned int debug;
64 module_param(debug, int, 0644);
65 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
66
67 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
68
69 #define dprintk(level, fmt, arg...) do {                        \
70 if (debug >= level)                                             \
71         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
72 } while (0)
73
74 struct em28xx_dvb {
75         struct dvb_frontend        *fe[2];
76
77         /* feed count management */
78         struct mutex               lock;
79         int                        nfeeds;
80
81         /* general boilerplate stuff */
82         struct dvb_adapter         adapter;
83         struct dvb_demux           demux;
84         struct dmxdev              dmxdev;
85         struct dmx_frontend        fe_hw;
86         struct dmx_frontend        fe_mem;
87         struct dvb_net             net;
88
89         /* Due to DRX-K - probably need changes */
90         int (*gate_ctrl)(struct dvb_frontend *, int);
91         struct semaphore      pll_mutex;
92         bool                    dont_attach_fe1;
93         int                     lna_gpio;
94         struct i2c_client       *i2c_client_tuner;
95 };
96
97
98 static inline void print_err_status(struct em28xx *dev,
99                                      int packet, int status)
100 {
101         char *errmsg = "Unknown";
102
103         switch (status) {
104         case -ENOENT:
105                 errmsg = "unlinked synchronuously";
106                 break;
107         case -ECONNRESET:
108                 errmsg = "unlinked asynchronuously";
109                 break;
110         case -ENOSR:
111                 errmsg = "Buffer error (overrun)";
112                 break;
113         case -EPIPE:
114                 errmsg = "Stalled (device not responding)";
115                 break;
116         case -EOVERFLOW:
117                 errmsg = "Babble (bad cable?)";
118                 break;
119         case -EPROTO:
120                 errmsg = "Bit-stuff error (bad cable?)";
121                 break;
122         case -EILSEQ:
123                 errmsg = "CRC/Timeout (could be anything)";
124                 break;
125         case -ETIME:
126                 errmsg = "Device does not respond";
127                 break;
128         }
129         if (packet < 0) {
130                 dprintk(1, "URB status %d [%s].\n", status, errmsg);
131         } else {
132                 dprintk(1, "URB packet %d, status %d [%s].\n",
133                         packet, status, errmsg);
134         }
135 }
136
137 static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
138 {
139         int xfer_bulk, num_packets, i;
140
141         if (!dev)
142                 return 0;
143
144         if (dev->disconnected)
145                 return 0;
146
147         if (urb->status < 0)
148                 print_err_status(dev, -1, urb->status);
149
150         xfer_bulk = usb_pipebulk(urb->pipe);
151
152         if (xfer_bulk) /* bulk */
153                 num_packets = 1;
154         else /* isoc */
155                 num_packets = urb->number_of_packets;
156
157         for (i = 0; i < num_packets; i++) {
158                 if (xfer_bulk) {
159                         if (urb->status < 0) {
160                                 print_err_status(dev, i, urb->status);
161                                 if (urb->status != -EPROTO)
162                                         continue;
163                         }
164                         dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
165                                         urb->actual_length);
166                 } else {
167                         if (urb->iso_frame_desc[i].status < 0) {
168                                 print_err_status(dev, i,
169                                                  urb->iso_frame_desc[i].status);
170                                 if (urb->iso_frame_desc[i].status != -EPROTO)
171                                         continue;
172                         }
173                         dvb_dmx_swfilter(&dev->dvb->demux,
174                                          urb->transfer_buffer +
175                                          urb->iso_frame_desc[i].offset,
176                                          urb->iso_frame_desc[i].actual_length);
177                 }
178         }
179
180         return 0;
181 }
182
183 static int em28xx_start_streaming(struct em28xx_dvb *dvb)
184 {
185         int rc;
186         struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
187         struct em28xx *dev = i2c_bus->dev;
188         int dvb_max_packet_size, packet_multiplier, dvb_alt;
189
190         if (dev->dvb_xfer_bulk) {
191                 if (!dev->dvb_ep_bulk)
192                         return -ENODEV;
193                 dvb_max_packet_size = 512; /* USB 2.0 spec */
194                 packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
195                 dvb_alt = 0;
196         } else { /* isoc */
197                 if (!dev->dvb_ep_isoc)
198                         return -ENODEV;
199                 dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
200                 if (dvb_max_packet_size < 0)
201                         return dvb_max_packet_size;
202                 packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
203                 dvb_alt = dev->dvb_alt_isoc;
204         }
205
206         usb_set_interface(dev->udev, 0, dvb_alt);
207         rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
208         if (rc < 0)
209                 return rc;
210
211         dprintk(1, "Using %d buffers each with %d x %d bytes\n",
212                 EM28XX_DVB_NUM_BUFS,
213                 packet_multiplier,
214                 dvb_max_packet_size);
215
216         return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
217                                     dev->dvb_xfer_bulk,
218                                     EM28XX_DVB_NUM_BUFS,
219                                     dvb_max_packet_size,
220                                     packet_multiplier,
221                                     em28xx_dvb_urb_data_copy);
222 }
223
224 static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
225 {
226         struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
227         struct em28xx *dev = i2c_bus->dev;
228
229         em28xx_stop_urbs(dev);
230
231         return 0;
232 }
233
234 static int em28xx_start_feed(struct dvb_demux_feed *feed)
235 {
236         struct dvb_demux *demux  = feed->demux;
237         struct em28xx_dvb *dvb = demux->priv;
238         int rc, ret;
239
240         if (!demux->dmx.frontend)
241                 return -EINVAL;
242
243         mutex_lock(&dvb->lock);
244         dvb->nfeeds++;
245         rc = dvb->nfeeds;
246
247         if (dvb->nfeeds == 1) {
248                 ret = em28xx_start_streaming(dvb);
249                 if (ret < 0)
250                         rc = ret;
251         }
252
253         mutex_unlock(&dvb->lock);
254         return rc;
255 }
256
257 static int em28xx_stop_feed(struct dvb_demux_feed *feed)
258 {
259         struct dvb_demux *demux  = feed->demux;
260         struct em28xx_dvb *dvb = demux->priv;
261         int err = 0;
262
263         mutex_lock(&dvb->lock);
264         dvb->nfeeds--;
265
266         if (0 == dvb->nfeeds)
267                 err = em28xx_stop_streaming(dvb);
268
269         mutex_unlock(&dvb->lock);
270         return err;
271 }
272
273
274
275 /* ------------------------------------------------------------------ */
276 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
277 {
278         struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
279         struct em28xx *dev = i2c_bus->dev;
280
281         if (acquire)
282                 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
283         else
284                 return em28xx_set_mode(dev, EM28XX_SUSPEND);
285 }
286
287 /* ------------------------------------------------------------------ */
288
289 static struct lgdt330x_config em2880_lgdt3303_dev = {
290         .demod_address = 0x0e,
291         .demod_chip = LGDT3303,
292 };
293
294 static struct lgdt3305_config em2870_lgdt3304_dev = {
295         .i2c_addr           = 0x0e,
296         .demod_chip         = LGDT3304,
297         .spectral_inversion = 1,
298         .deny_i2c_rptr      = 1,
299         .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
300         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
301         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
302         .vsb_if_khz         = 3250,
303         .qam_if_khz         = 4000,
304 };
305
306 static struct lgdt3305_config em2874_lgdt3305_dev = {
307         .i2c_addr           = 0x0e,
308         .demod_chip         = LGDT3305,
309         .spectral_inversion = 1,
310         .deny_i2c_rptr      = 0,
311         .mpeg_mode          = LGDT3305_MPEG_SERIAL,
312         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
313         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
314         .vsb_if_khz         = 3250,
315         .qam_if_khz         = 4000,
316 };
317
318 static struct s921_config sharp_isdbt = {
319         .demod_address = 0x30 >> 1
320 };
321
322 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
323         .demod_address = (0x1e >> 1),
324         .no_tuner = 1,
325         .parallel_ts = 1,
326         .if2 = 45600,
327 };
328
329 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
330         .demod_address = 0x32 >> 1,
331         .output_mode   = S5H1409_PARALLEL_OUTPUT,
332         .gpio          = S5H1409_GPIO_OFF,
333         .inversion     = S5H1409_INVERSION_OFF,
334         .status_mode   = S5H1409_DEMODLOCKING,
335         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
336 };
337
338 static struct tda18271_std_map kworld_a340_std_map = {
339         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
340                       .if_lvl = 1, .rfagc_top = 0x37, },
341         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
342                       .if_lvl = 1, .rfagc_top = 0x37, },
343 };
344
345 static struct tda18271_config kworld_a340_config = {
346         .std_map           = &kworld_a340_std_map,
347 };
348
349 static struct tda18271_config kworld_ub435q_v2_config = {
350         .std_map        = &kworld_a340_std_map,
351         .gate           = TDA18271_GATE_DIGITAL,
352 };
353
354 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
355         .demod_address = (0x1e >> 1),
356         .no_tuner = 1,
357         .disable_i2c_gate_ctrl = 1,
358         .parallel_ts = 1,
359         .if2 = 45600,
360 };
361
362 static struct drxd_config em28xx_drxd = {
363         .demod_address = 0x70,
364         .demod_revision = 0xa2,
365         .pll_type = DRXD_PLL_NONE,
366         .clock = 12000,
367         .insert_rs_byte = 1,
368         .IF = 42800000,
369         .disable_i2c_gate_ctrl = 1,
370 };
371
372 static struct drxk_config terratec_h5_drxk = {
373         .adr = 0x29,
374         .single_master = 1,
375         .no_i2c_bridge = 1,
376         .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
377         .qam_demod_parameter_count = 2,
378         .load_firmware_sync = true,
379 };
380
381 static struct drxk_config hauppauge_930c_drxk = {
382         .adr = 0x29,
383         .single_master = 1,
384         .no_i2c_bridge = 1,
385         .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
386         .chunk_size = 56,
387         .qam_demod_parameter_count = 2,
388         .load_firmware_sync = true,
389 };
390
391 static struct drxk_config terratec_htc_stick_drxk = {
392         .adr = 0x29,
393         .single_master = 1,
394         .no_i2c_bridge = 1,
395         .microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
396         .chunk_size = 54,
397         .qam_demod_parameter_count = 2,
398         /* Required for the antenna_gpio to disable LNA. */
399         .antenna_dvbt = true,
400         /* The windows driver uses the same. This will disable LNA. */
401         .antenna_gpio = 0x6,
402         .load_firmware_sync = true,
403 };
404
405 static struct drxk_config maxmedia_ub425_tc_drxk = {
406         .adr = 0x29,
407         .single_master = 1,
408         .no_i2c_bridge = 1,
409         .microcode_name = "dvb-demod-drxk-01.fw",
410         .chunk_size = 62,
411         .load_firmware_sync = true,
412         .qam_demod_parameter_count = 2,
413 };
414
415 static struct drxk_config pctv_520e_drxk = {
416         .adr = 0x29,
417         .single_master = 1,
418         .microcode_name = "dvb-demod-drxk-pctv.fw",
419         .qam_demod_parameter_count = 2,
420         .chunk_size = 58,
421         .antenna_dvbt = true, /* disable LNA */
422         .antenna_gpio = (1 << 2), /* disable LNA */
423         .load_firmware_sync = true,
424 };
425
426 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
427 {
428         struct em28xx_dvb *dvb = fe->sec_priv;
429         int status;
430
431         if (!dvb)
432                 return -EINVAL;
433
434         if (enable) {
435                 down(&dvb->pll_mutex);
436                 status = dvb->gate_ctrl(fe, 1);
437         } else {
438                 status = dvb->gate_ctrl(fe, 0);
439                 up(&dvb->pll_mutex);
440         }
441         return status;
442 }
443
444 static void hauppauge_hvr930c_init(struct em28xx *dev)
445 {
446         int i;
447
448         struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
449                 {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0x65},
450                 {EM2874_R80_GPIO_P0_CTRL,       0xfb,   0xff,   0x32},
451                 {EM2874_R80_GPIO_P0_CTRL,       0xff,   0xff,   0xb8},
452                 {       -1,                     -1,     -1,     -1},
453         };
454         struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
455                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
456                 {EM2874_R80_GPIO_P0_CTRL,       0xaf,   0xff,   0x65},
457                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x76},
458                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x01},
459                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
460                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x40},
461
462                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x65},
463                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
464                 {EM2874_R80_GPIO_P0_CTRL,       0xcf,   0xff,   0x0b},
465                 {EM2874_R80_GPIO_P0_CTRL,       0xef,   0xff,   0x65},
466
467                 {       -1,                     -1,     -1,     -1},
468         };
469
470         struct {
471                 unsigned char r[4];
472                 int len;
473         } regs[] = {
474                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
475                 {{ 0x01, 0x02 }, 2},
476                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
477                 {{ 0x01, 0x00 }, 2},
478                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
479                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
480                 {{ 0x01, 0x00 }, 2},
481                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
482                 {{ 0x04, 0x00 }, 2},
483                 {{ 0x00, 0x04 }, 2},
484                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
485                 {{ 0x04, 0x14 }, 2},
486                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
487         };
488
489         em28xx_gpio_set(dev, hauppauge_hvr930c_init);
490         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
491         msleep(10);
492         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
493         msleep(10);
494
495         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
496
497         for (i = 0; i < ARRAY_SIZE(regs); i++)
498                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
499         em28xx_gpio_set(dev, hauppauge_hvr930c_end);
500
501         msleep(100);
502
503         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
504         msleep(30);
505
506         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
507         msleep(10);
508
509 }
510
511 static void terratec_h5_init(struct em28xx *dev)
512 {
513         int i;
514         struct em28xx_reg_seq terratec_h5_init[] = {
515                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
516                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
517                 {EM2874_R80_GPIO_P0_CTRL,       0xf2,   0xff,   50},
518                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
519                 {       -1,                     -1,     -1,     -1},
520         };
521         struct em28xx_reg_seq terratec_h5_end[] = {
522                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
523                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
524                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
525                 {       -1,                     -1,     -1,     -1},
526         };
527         struct {
528                 unsigned char r[4];
529                 int len;
530         } regs[] = {
531                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
532                 {{ 0x01, 0x02 }, 2},
533                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
534                 {{ 0x01, 0x00 }, 2},
535                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
536                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
537                 {{ 0x01, 0x00 }, 2},
538                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
539                 {{ 0x04, 0x00 }, 2},
540                 {{ 0x00, 0x04 }, 2},
541                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
542                 {{ 0x04, 0x14 }, 2},
543                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
544         };
545
546         em28xx_gpio_set(dev, terratec_h5_init);
547         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
548         msleep(10);
549         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
550         msleep(10);
551
552         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
553
554         for (i = 0; i < ARRAY_SIZE(regs); i++)
555                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
556         em28xx_gpio_set(dev, terratec_h5_end);
557 };
558
559 static void terratec_htc_stick_init(struct em28xx *dev)
560 {
561         int i;
562
563         /*
564          * GPIO configuration:
565          * 0xff: unknown (does not affect DVB-T).
566          * 0xf6: DRX-K (demodulator).
567          * 0xe6: unknown (does not affect DVB-T).
568          * 0xb6: unknown (does not affect DVB-T).
569          */
570         struct em28xx_reg_seq terratec_htc_stick_init[] = {
571                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
572                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
573                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   50},
574                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   100},
575                 {       -1,                     -1,     -1,     -1},
576         };
577         struct em28xx_reg_seq terratec_htc_stick_end[] = {
578                 {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
579                 {EM2874_R80_GPIO_P0_CTRL,       0xf6,   0xff,   50},
580                 {       -1,                     -1,     -1,     -1},
581         };
582
583         /*
584          * Init the analog decoder (not yet supported), but
585          * it's probably still a good idea.
586          */
587         struct {
588                 unsigned char r[4];
589                 int len;
590         } regs[] = {
591                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
592                 {{ 0x01, 0x02 }, 2},
593                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
594                 {{ 0x01, 0x00 }, 2},
595                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
596         };
597
598         em28xx_gpio_set(dev, terratec_htc_stick_init);
599
600         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
601         msleep(10);
602         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
603         msleep(10);
604
605         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
606
607         for (i = 0; i < ARRAY_SIZE(regs); i++)
608                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
609
610         em28xx_gpio_set(dev, terratec_htc_stick_end);
611 };
612
613 static void terratec_htc_usb_xs_init(struct em28xx *dev)
614 {
615         int i;
616
617         struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
618                 {EM2820_R08_GPIO_CTRL,          0xff,   0xff,   10},
619                 {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   100},
620                 {EM2874_R80_GPIO_P0_CTRL,       0xb2,   0xff,   50},
621                 {EM2874_R80_GPIO_P0_CTRL,       0xb6,   0xff,   100},
622                 {       -1,                     -1,     -1,     -1},
623         };
624         struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
625                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   100},
626                 {EM2874_R80_GPIO_P0_CTRL,       0xa6,   0xff,   50},
627                 {EM2874_R80_GPIO_P0_CTRL,       0xe6,   0xff,   100},
628                 {       -1,                     -1,     -1,     -1},
629         };
630
631         /*
632          * Init the analog decoder (not yet supported), but
633          * it's probably still a good idea.
634          */
635         struct {
636                 unsigned char r[4];
637                 int len;
638         } regs[] = {
639                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
640                 {{ 0x01, 0x02 }, 2},
641                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
642                 {{ 0x01, 0x00 }, 2},
643                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
644                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
645                 {{ 0x01, 0x00 }, 2},
646                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
647                 {{ 0x04, 0x00 }, 2},
648                 {{ 0x00, 0x04 }, 2},
649                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
650                 {{ 0x04, 0x14 }, 2},
651                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
652         };
653
654         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
655
656         em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
657
658         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
659         msleep(10);
660         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
661         msleep(10);
662
663         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
664
665         for (i = 0; i < ARRAY_SIZE(regs); i++)
666                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
667
668         em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
669 };
670
671 static void pctv_520e_init(struct em28xx *dev)
672 {
673         /*
674          * Init AVF4910B analog decoder. Looks like I2C traffic to
675          * digital demodulator and tuner are routed via AVF4910B.
676          */
677         int i;
678         struct {
679                 unsigned char r[4];
680                 int len;
681         } regs[] = {
682                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
683                 {{ 0x01, 0x02 }, 2},
684                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
685                 {{ 0x01, 0x00 }, 2},
686                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
687                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
688                 {{ 0x01, 0x00 }, 2},
689                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
690         };
691
692         dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
693
694         for (i = 0; i < ARRAY_SIZE(regs); i++)
695                 i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
696 };
697
698 static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
699 {
700         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
701         struct em28xx *dev = fe->dvb->priv;
702 #ifdef CONFIG_GPIOLIB
703         struct em28xx_dvb *dvb = dev->dvb;
704         int ret;
705         unsigned long flags;
706
707         if (c->lna == 1)
708                 flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
709         else
710                 flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
711
712         ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
713         if (ret)
714                 em28xx_errdev("gpio request failed %d\n", ret);
715         else
716                 gpio_free(dvb->lna_gpio);
717
718         return ret;
719 #else
720         dev_warn(&dev->udev->dev, "%s: LNA control is disabled (lna=%u)\n",
721                         KBUILD_MODNAME, c->lna);
722         return 0;
723 #endif
724 }
725
726 static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
727 {
728         /* Values extracted from a USB trace of the Terratec Windows driver */
729         static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
730         static u8 reset[]          = { RESET,      0x80 };
731         static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
732         static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
733         static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
734         static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
735         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
736         static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
737         static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
738         static u8 tuner_go[]       = { TUNER_GO, 0x01};
739
740         mt352_write(fe, clock_config,   sizeof(clock_config));
741         udelay(200);
742         mt352_write(fe, reset,          sizeof(reset));
743         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
744         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
745         mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
746         mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
747         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
748         mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
749         mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
750         mt352_write(fe, tuner_go,       sizeof(tuner_go));
751         return 0;
752 }
753
754 static struct mt352_config terratec_xs_mt352_cfg = {
755         .demod_address = (0x1e >> 1),
756         .no_tuner = 1,
757         .if2 = 45600,
758         .demod_init = em28xx_mt352_terratec_xs_init,
759 };
760
761 static struct tda10023_config em28xx_tda10023_config = {
762         .demod_address = 0x0c,
763         .invert = 1,
764 };
765
766 static struct cxd2820r_config em28xx_cxd2820r_config = {
767         .i2c_address = (0xd8 >> 1),
768         .ts_mode = CXD2820R_TS_SERIAL,
769 };
770
771 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
772         .output_opt = TDA18271_OUTPUT_LT_OFF,
773         .gate = TDA18271_GATE_DIGITAL,
774 };
775
776 static const struct tda10071_config em28xx_tda10071_config = {
777         .demod_i2c_addr = 0x55, /* (0xaa >> 1) */
778         .tuner_i2c_addr = 0x14,
779         .i2c_wr_max = 64,
780         .ts_mode = TDA10071_TS_SERIAL,
781         .spec_inv = 0,
782         .xtal = 40444000, /* 40.444 MHz */
783         .pll_multiplier = 20,
784 };
785
786 static const struct a8293_config em28xx_a8293_config = {
787         .i2c_addr = 0x08, /* (0x10 >> 1) */
788 };
789
790 static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
791         .demod_address = (0x1e >> 1),
792         .disable_i2c_gate_ctrl = 1,
793         .no_tuner = 1,
794         .parallel_ts = 1,
795 };
796 static struct qt1010_config em28xx_qt1010_config = {
797         .i2c_address = 0x62
798 };
799
800 static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
801         .demod_address = 0x10,
802         .is_serial = true,
803 };
804
805 static struct tda18271_std_map mb86a20s_tda18271_config = {
806         .dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
807                       .if_lvl = 1, .rfagc_top = 0x37, },
808 };
809
810 static struct tda18271_config c3tech_duo_tda18271_config = {
811         .std_map = &mb86a20s_tda18271_config,
812         .gate    = TDA18271_GATE_DIGITAL,
813         .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
814 };
815
816 static const struct m88ds3103_config pctv_461e_m88ds3103_config = {
817         .i2c_addr = 0x68,
818         .clock = 27000000,
819         .i2c_wr_max = 33,
820         .clock_out = 0,
821         .ts_mode = M88DS3103_TS_PARALLEL_16,
822         .agc = 0x99,
823 };
824
825 /* ------------------------------------------------------------------ */
826
827 static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
828 {
829         struct dvb_frontend *fe;
830         struct xc2028_config cfg;
831         struct xc2028_ctrl ctl;
832
833         memset(&cfg, 0, sizeof(cfg));
834         cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
835         cfg.i2c_addr  = addr;
836
837         memset(&ctl, 0, sizeof(ctl));
838         em28xx_setup_xc3028(dev, &ctl);
839         cfg.ctrl  = &ctl;
840
841         if (!dev->dvb->fe[0]) {
842                 em28xx_errdev("/2: dvb frontend not attached. "
843                                 "Can't attach xc3028\n");
844                 return -EINVAL;
845         }
846
847         fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
848         if (!fe) {
849                 em28xx_errdev("/2: xc3028 attach failed\n");
850                 dvb_frontend_detach(dev->dvb->fe[0]);
851                 dev->dvb->fe[0] = NULL;
852                 return -EINVAL;
853         }
854
855         em28xx_info("%s/2: xc3028 attached\n", dev->name);
856
857         return 0;
858 }
859
860 /* ------------------------------------------------------------------ */
861
862 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
863                                struct em28xx *dev, struct device *device)
864 {
865         int result;
866
867         mutex_init(&dvb->lock);
868
869         /* register adapter */
870         result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
871                                       adapter_nr);
872         if (result < 0) {
873                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
874                        dev->name, result);
875                 goto fail_adapter;
876         }
877
878         /* Ensure all frontends negotiate bus access */
879         dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
880         if (dvb->fe[1])
881                 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
882
883         dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
884
885         /* register frontend */
886         result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
887         if (result < 0) {
888                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
889                        dev->name, result);
890                 goto fail_frontend0;
891         }
892
893         /* register 2nd frontend */
894         if (dvb->fe[1]) {
895                 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
896                 if (result < 0) {
897                         printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
898                                 dev->name, result);
899                         goto fail_frontend1;
900                 }
901         }
902
903         /* register demux stuff */
904         dvb->demux.dmx.capabilities =
905                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
906                 DMX_MEMORY_BASED_FILTERING;
907         dvb->demux.priv       = dvb;
908         dvb->demux.filternum  = 256;
909         dvb->demux.feednum    = 256;
910         dvb->demux.start_feed = em28xx_start_feed;
911         dvb->demux.stop_feed  = em28xx_stop_feed;
912
913         result = dvb_dmx_init(&dvb->demux);
914         if (result < 0) {
915                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
916                        dev->name, result);
917                 goto fail_dmx;
918         }
919
920         dvb->dmxdev.filternum    = 256;
921         dvb->dmxdev.demux        = &dvb->demux.dmx;
922         dvb->dmxdev.capabilities = 0;
923         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
924         if (result < 0) {
925                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
926                        dev->name, result);
927                 goto fail_dmxdev;
928         }
929
930         dvb->fe_hw.source = DMX_FRONTEND_0;
931         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
932         if (result < 0) {
933                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
934                        dev->name, result);
935                 goto fail_fe_hw;
936         }
937
938         dvb->fe_mem.source = DMX_MEMORY_FE;
939         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
940         if (result < 0) {
941                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
942                        dev->name, result);
943                 goto fail_fe_mem;
944         }
945
946         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
947         if (result < 0) {
948                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
949                        dev->name, result);
950                 goto fail_fe_conn;
951         }
952
953         /* register network adapter */
954         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
955         return 0;
956
957 fail_fe_conn:
958         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
959 fail_fe_mem:
960         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
961 fail_fe_hw:
962         dvb_dmxdev_release(&dvb->dmxdev);
963 fail_dmxdev:
964         dvb_dmx_release(&dvb->demux);
965 fail_dmx:
966         if (dvb->fe[1])
967                 dvb_unregister_frontend(dvb->fe[1]);
968         dvb_unregister_frontend(dvb->fe[0]);
969 fail_frontend1:
970         if (dvb->fe[1])
971                 dvb_frontend_detach(dvb->fe[1]);
972 fail_frontend0:
973         dvb_frontend_detach(dvb->fe[0]);
974         dvb_unregister_adapter(&dvb->adapter);
975 fail_adapter:
976         return result;
977 }
978
979 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
980 {
981         dvb_net_release(&dvb->net);
982         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
983         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
984         dvb_dmxdev_release(&dvb->dmxdev);
985         dvb_dmx_release(&dvb->demux);
986         if (dvb->fe[1])
987                 dvb_unregister_frontend(dvb->fe[1]);
988         dvb_unregister_frontend(dvb->fe[0]);
989         if (dvb->fe[1] && !dvb->dont_attach_fe1)
990                 dvb_frontend_detach(dvb->fe[1]);
991         dvb_frontend_detach(dvb->fe[0]);
992         dvb_unregister_adapter(&dvb->adapter);
993 }
994
995 static int em28xx_dvb_init(struct em28xx *dev)
996 {
997         int result = 0, mfe_shared = 0;
998         struct em28xx_dvb *dvb;
999
1000         if (dev->is_audio_only) {
1001                 /* Shouldn't initialize IR for this interface */
1002                 return 0;
1003         }
1004
1005         if (!dev->board.has_dvb) {
1006                 /* This device does not support the extension */
1007                 return 0;
1008         }
1009
1010         em28xx_info("Binding DVB extension\n");
1011
1012         dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
1013
1014         if (dvb == NULL) {
1015                 em28xx_info("em28xx_dvb: memory allocation failed\n");
1016                 return -ENOMEM;
1017         }
1018         dev->dvb = dvb;
1019         dvb->fe[0] = dvb->fe[1] = NULL;
1020
1021         mutex_lock(&dev->lock);
1022         em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1023         /* init frontend */
1024         switch (dev->model) {
1025         case EM2874_BOARD_LEADERSHIP_ISDBT:
1026                 dvb->fe[0] = dvb_attach(s921_attach,
1027                                 &sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
1028
1029                 if (!dvb->fe[0]) {
1030                         result = -EINVAL;
1031                         goto out_free;
1032                 }
1033
1034                 break;
1035         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
1036         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
1037         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
1038         case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
1039                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1040                                            &em2880_lgdt3303_dev,
1041                                            &dev->i2c_adap[dev->def_i2c_bus]);
1042                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1043                         result = -EINVAL;
1044                         goto out_free;
1045                 }
1046                 break;
1047         case EM2880_BOARD_KWORLD_DVB_310U:
1048                 dvb->fe[0] = dvb_attach(zl10353_attach,
1049                                            &em28xx_zl10353_with_xc3028,
1050                                            &dev->i2c_adap[dev->def_i2c_bus]);
1051                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1052                         result = -EINVAL;
1053                         goto out_free;
1054                 }
1055                 break;
1056         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
1057         case EM2882_BOARD_TERRATEC_HYBRID_XS:
1058         case EM2880_BOARD_EMPIRE_DUAL_TV:
1059                 dvb->fe[0] = dvb_attach(zl10353_attach,
1060                                            &em28xx_zl10353_xc3028_no_i2c_gate,
1061                                            &dev->i2c_adap[dev->def_i2c_bus]);
1062                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1063                         result = -EINVAL;
1064                         goto out_free;
1065                 }
1066                 break;
1067         case EM2880_BOARD_TERRATEC_HYBRID_XS:
1068         case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
1069         case EM2881_BOARD_PINNACLE_HYBRID_PRO:
1070         case EM2882_BOARD_DIKOM_DK300:
1071         case EM2882_BOARD_KWORLD_VS_DVBT:
1072                 dvb->fe[0] = dvb_attach(zl10353_attach,
1073                                            &em28xx_zl10353_xc3028_no_i2c_gate,
1074                                            &dev->i2c_adap[dev->def_i2c_bus]);
1075                 if (dvb->fe[0] == NULL) {
1076                         /* This board could have either a zl10353 or a mt352.
1077                            If the chip id isn't for zl10353, try mt352 */
1078                         dvb->fe[0] = dvb_attach(mt352_attach,
1079                                                    &terratec_xs_mt352_cfg,
1080                                                    &dev->i2c_adap[dev->def_i2c_bus]);
1081                 }
1082
1083                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1084                         result = -EINVAL;
1085                         goto out_free;
1086                 }
1087                 break;
1088         case EM2870_BOARD_KWORLD_355U:
1089                 dvb->fe[0] = dvb_attach(zl10353_attach,
1090                                            &em28xx_zl10353_no_i2c_gate_dev,
1091                                            &dev->i2c_adap[dev->def_i2c_bus]);
1092                 if (dvb->fe[0] != NULL)
1093                         dvb_attach(qt1010_attach, dvb->fe[0],
1094                                    &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
1095                 break;
1096         case EM2883_BOARD_KWORLD_HYBRID_330U:
1097         case EM2882_BOARD_EVGA_INDTUBE:
1098                 dvb->fe[0] = dvb_attach(s5h1409_attach,
1099                                            &em28xx_s5h1409_with_xc3028,
1100                                            &dev->i2c_adap[dev->def_i2c_bus]);
1101                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1102                         result = -EINVAL;
1103                         goto out_free;
1104                 }
1105                 break;
1106         case EM2882_BOARD_KWORLD_ATSC_315U:
1107                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
1108                                            &em2880_lgdt3303_dev,
1109                                            &dev->i2c_adap[dev->def_i2c_bus]);
1110                 if (dvb->fe[0] != NULL) {
1111                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1112                                 &dev->i2c_adap[dev->def_i2c_bus], 0x61, TUNER_THOMSON_DTT761X)) {
1113                                 result = -EINVAL;
1114                                 goto out_free;
1115                         }
1116                 }
1117                 break;
1118         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
1119         case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
1120                 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1121                                            &dev->i2c_adap[dev->def_i2c_bus], &dev->udev->dev);
1122                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
1123                         result = -EINVAL;
1124                         goto out_free;
1125                 }
1126                 break;
1127         case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
1128                 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
1129                 dvb->fe[0] = dvb_attach(tda10023_attach,
1130                         &em28xx_tda10023_config,
1131                         &dev->i2c_adap[dev->def_i2c_bus], 0x48);
1132                 if (dvb->fe[0]) {
1133                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1134                                 &dev->i2c_adap[dev->def_i2c_bus], 0x60, TUNER_PHILIPS_CU1216L)) {
1135                                 result = -EINVAL;
1136                                 goto out_free;
1137                         }
1138                 }
1139                 break;
1140         case EM2870_BOARD_KWORLD_A340:
1141                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1142                                            &em2870_lgdt3304_dev,
1143                                            &dev->i2c_adap[dev->def_i2c_bus]);
1144                 if (dvb->fe[0] != NULL)
1145                         dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1146                                    &dev->i2c_adap[dev->def_i2c_bus], &kworld_a340_config);
1147                 break;
1148         case EM28174_BOARD_PCTV_290E:
1149                 /* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1150                 dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1151                                 CXD2820R_GPIO_L;
1152                 dvb->fe[0] = dvb_attach(cxd2820r_attach,
1153                                         &em28xx_cxd2820r_config,
1154                                         &dev->i2c_adap[dev->def_i2c_bus],
1155                                         &dvb->lna_gpio);
1156                 if (dvb->fe[0]) {
1157                         /* FE 0 attach tuner */
1158                         if (!dvb_attach(tda18271_attach,
1159                                         dvb->fe[0],
1160                                         0x60,
1161                                         &dev->i2c_adap[dev->def_i2c_bus],
1162                                         &em28xx_cxd2820r_tda18271_config)) {
1163
1164                                 dvb_frontend_detach(dvb->fe[0]);
1165                                 result = -EINVAL;
1166                                 goto out_free;
1167                         }
1168
1169 #ifdef CONFIG_GPIOLIB
1170                         /* enable LNA for DVB-T, DVB-T2 and DVB-C */
1171                         result = gpio_request_one(dvb->lna_gpio,
1172                                         GPIOF_OUT_INIT_LOW, NULL);
1173                         if (result)
1174                                 em28xx_errdev("gpio request failed %d\n",
1175                                                 result);
1176                         else
1177                                 gpio_free(dvb->lna_gpio);
1178
1179                         result = 0; /* continue even set LNA fails */
1180 #endif
1181                         dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
1182                 }
1183
1184                 break;
1185         case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
1186         {
1187                 struct xc5000_config cfg;
1188                 hauppauge_hvr930c_init(dev);
1189
1190                 dvb->fe[0] = dvb_attach(drxk_attach,
1191                                         &hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1192                 if (!dvb->fe[0]) {
1193                         result = -EINVAL;
1194                         goto out_free;
1195                 }
1196                 /* FIXME: do we need a pll semaphore? */
1197                 dvb->fe[0]->sec_priv = dvb;
1198                 sema_init(&dvb->pll_mutex, 1);
1199                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1200                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1201
1202                 /* Attach xc5000 */
1203                 memset(&cfg, 0, sizeof(cfg));
1204                 cfg.i2c_address  = 0x61;
1205                 cfg.if_khz = 4000;
1206
1207                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1208                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1209                 if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1210                                 &cfg)) {
1211                         result = -EINVAL;
1212                         goto out_free;
1213                 }
1214                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1215                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1216
1217                 break;
1218         }
1219         case EM2884_BOARD_TERRATEC_H5:
1220                 terratec_h5_init(dev);
1221
1222                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
1223                 if (!dvb->fe[0]) {
1224                         result = -EINVAL;
1225                         goto out_free;
1226                 }
1227                 /* FIXME: do we need a pll semaphore? */
1228                 dvb->fe[0]->sec_priv = dvb;
1229                 sema_init(&dvb->pll_mutex, 1);
1230                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
1231                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
1232
1233                 /* Attach tda18271 to DVB-C frontend */
1234                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1235                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1236                 if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
1237                         result = -EINVAL;
1238                         goto out_free;
1239                 }
1240                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1241                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1242
1243                 break;
1244         case EM2884_BOARD_C3TECH_DIGITAL_DUO:
1245                 dvb->fe[0] = dvb_attach(mb86a20s_attach,
1246                                            &c3tech_duo_mb86a20s_config,
1247                                            &dev->i2c_adap[dev->def_i2c_bus]);
1248                 if (dvb->fe[0] != NULL)
1249                         dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1250                                    &dev->i2c_adap[dev->def_i2c_bus],
1251                                    &c3tech_duo_tda18271_config);
1252                 break;
1253         case EM28174_BOARD_PCTV_460E:
1254                 /* attach demod */
1255                 dvb->fe[0] = dvb_attach(tda10071_attach,
1256                         &em28xx_tda10071_config, &dev->i2c_adap[dev->def_i2c_bus]);
1257
1258                 /* attach SEC */
1259                 if (dvb->fe[0])
1260                         dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
1261                                 &em28xx_a8293_config);
1262                 break;
1263         case EM2874_BOARD_DELOCK_61959:
1264         case EM2874_BOARD_MAXMEDIA_UB425_TC:
1265                 /* attach demodulator */
1266                 dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1267                                 &dev->i2c_adap[dev->def_i2c_bus]);
1268
1269                 if (dvb->fe[0]) {
1270                         /* disable I2C-gate */
1271                         dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1272
1273                         /* attach tuner */
1274                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1275                                         &dev->i2c_adap[dev->def_i2c_bus],
1276                                         &em28xx_cxd2820r_tda18271_config)) {
1277                                 dvb_frontend_detach(dvb->fe[0]);
1278                                 result = -EINVAL;
1279                                 goto out_free;
1280                         }
1281                 }
1282                 break;
1283         case EM2884_BOARD_PCTV_510E:
1284         case EM2884_BOARD_PCTV_520E:
1285                 pctv_520e_init(dev);
1286
1287                 /* attach demodulator */
1288                 dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1289                                 &dev->i2c_adap[dev->def_i2c_bus]);
1290
1291                 if (dvb->fe[0]) {
1292                         /* attach tuner */
1293                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1294                                         &dev->i2c_adap[dev->def_i2c_bus],
1295                                         &em28xx_cxd2820r_tda18271_config)) {
1296                                 dvb_frontend_detach(dvb->fe[0]);
1297                                 result = -EINVAL;
1298                                 goto out_free;
1299                         }
1300                 }
1301                 break;
1302         case EM2884_BOARD_CINERGY_HTC_STICK:
1303                 terratec_htc_stick_init(dev);
1304
1305                 /* attach demodulator */
1306                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1307                                         &dev->i2c_adap[dev->def_i2c_bus]);
1308                 if (!dvb->fe[0]) {
1309                         result = -EINVAL;
1310                         goto out_free;
1311                 }
1312
1313                 /* Attach the demodulator. */
1314                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1315                                 &dev->i2c_adap[dev->def_i2c_bus],
1316                                 &em28xx_cxd2820r_tda18271_config)) {
1317                         result = -EINVAL;
1318                         goto out_free;
1319                 }
1320                 break;
1321         case EM2884_BOARD_TERRATEC_HTC_USB_XS:
1322                 terratec_htc_usb_xs_init(dev);
1323
1324                 /* attach demodulator */
1325                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1326                                         &dev->i2c_adap[dev->def_i2c_bus]);
1327                 if (!dvb->fe[0]) {
1328                         result = -EINVAL;
1329                         goto out_free;
1330                 }
1331
1332                 /* Attach the demodulator. */
1333                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1334                                 &dev->i2c_adap[dev->def_i2c_bus],
1335                                 &em28xx_cxd2820r_tda18271_config)) {
1336                         result = -EINVAL;
1337                         goto out_free;
1338                 }
1339                 break;
1340         case EM2874_BOARD_KWORLD_UB435Q_V2:
1341                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
1342                                         &em2874_lgdt3305_dev,
1343                                         &dev->i2c_adap[dev->def_i2c_bus]);
1344                 if (!dvb->fe[0]) {
1345                         result = -EINVAL;
1346                         goto out_free;
1347                 }
1348
1349                 /* Attach the demodulator. */
1350                 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1351                                 &dev->i2c_adap[dev->def_i2c_bus],
1352                                 &kworld_ub435q_v2_config)) {
1353                         result = -EINVAL;
1354                         goto out_free;
1355                 }
1356                 break;
1357         case EM28178_BOARD_PCTV_461E:
1358                 {
1359                         /* demod I2C adapter */
1360                         struct i2c_adapter *i2c_adapter;
1361                         struct i2c_board_info info;
1362                         struct m88ts2022_config m88ts2022_config = {
1363                                 .clock = 27000000,
1364                         };
1365                         memset(&info, 0, sizeof(struct i2c_board_info));
1366
1367                         /* attach demod */
1368                         dvb->fe[0] = dvb_attach(m88ds3103_attach,
1369                                         &pctv_461e_m88ds3103_config,
1370                                         &dev->i2c_adap[dev->def_i2c_bus],
1371                                         &i2c_adapter);
1372                         if (dvb->fe[0] == NULL) {
1373                                 result = -ENODEV;
1374                                 goto out_free;
1375                         }
1376
1377                         /* attach tuner */
1378                         m88ts2022_config.fe = dvb->fe[0];
1379                         strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
1380                         info.addr = 0x60;
1381                         info.platform_data = &m88ts2022_config;
1382                         request_module("m88ts2022");
1383                         dvb->i2c_client_tuner = i2c_new_device(i2c_adapter, &info);
1384
1385                         /* delegate signal strength measurement to tuner */
1386                         dvb->fe[0]->ops.read_signal_strength =
1387                                         dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1388
1389                         /* attach SEC */
1390                         if (!dvb_attach(a8293_attach, dvb->fe[0],
1391                                         &dev->i2c_adap[dev->def_i2c_bus],
1392                                         &em28xx_a8293_config)) {
1393                                 dvb_frontend_detach(dvb->fe[0]);
1394                                 result = -ENODEV;
1395                                 goto out_free;
1396                         }
1397                 }
1398                 break;
1399         default:
1400                 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
1401                                 " isn't supported yet\n");
1402                 break;
1403         }
1404         if (NULL == dvb->fe[0]) {
1405                 em28xx_errdev("/2: frontend initialization failed\n");
1406                 result = -EINVAL;
1407                 goto out_free;
1408         }
1409         /* define general-purpose callback pointer */
1410         dvb->fe[0]->callback = em28xx_tuner_callback;
1411         if (dvb->fe[1])
1412                 dvb->fe[1]->callback = em28xx_tuner_callback;
1413
1414         /* register everything */
1415         result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
1416
1417         if (result < 0)
1418                 goto out_free;
1419
1420         /* MFE lock */
1421         dvb->adapter.mfe_shared = mfe_shared;
1422
1423         em28xx_info("DVB extension successfully initialized\n");
1424 ret:
1425         em28xx_set_mode(dev, EM28XX_SUSPEND);
1426         mutex_unlock(&dev->lock);
1427         return result;
1428
1429 out_free:
1430         kfree(dvb);
1431         dev->dvb = NULL;
1432         goto ret;
1433 }
1434
1435 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1436 {
1437         ops->set_voltage = NULL;
1438         ops->sleep = NULL;
1439         ops->tuner_ops.sleep = NULL;
1440 }
1441
1442 static int em28xx_dvb_fini(struct em28xx *dev)
1443 {
1444         if (dev->is_audio_only) {
1445                 /* Shouldn't initialize IR for this interface */
1446                 return 0;
1447         }
1448
1449         if (!dev->board.has_dvb) {
1450                 /* This device does not support the extension */
1451                 return 0;
1452         }
1453
1454         if (dev->dvb) {
1455                 struct em28xx_dvb *dvb = dev->dvb;
1456
1457                 if (dev->disconnected) {
1458                         /* We cannot tell the device to sleep
1459                          * once it has been unplugged. */
1460                         if (dvb->fe[0])
1461                                 prevent_sleep(&dvb->fe[0]->ops);
1462                         if (dvb->fe[1])
1463                                 prevent_sleep(&dvb->fe[1]->ops);
1464                 }
1465
1466                 i2c_release_client(dvb->i2c_client_tuner);
1467                 em28xx_unregister_dvb(dvb);
1468                 kfree(dvb);
1469                 dev->dvb = NULL;
1470         }
1471
1472         return 0;
1473 }
1474
1475 static struct em28xx_ops dvb_ops = {
1476         .id   = EM28XX_DVB,
1477         .name = "Em28xx dvb Extension",
1478         .init = em28xx_dvb_init,
1479         .fini = em28xx_dvb_fini,
1480 };
1481
1482 static int __init em28xx_dvb_register(void)
1483 {
1484         return em28xx_register_extension(&dvb_ops);
1485 }
1486
1487 static void __exit em28xx_dvb_unregister(void)
1488 {
1489         em28xx_unregister_extension(&dvb_ops);
1490 }
1491
1492 module_init(em28xx_dvb_register);
1493 module_exit(em28xx_dvb_unregister);