blob: cef7a2d409cbc27c53c2aabe805528d62f40b894 [file] [log] [blame]
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03001/*
2 DVB device driver for em28xx
3
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -03004 (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03005
Devin Heitmuellerbdfbf952008-04-17 21:38:27 -03006 (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7 - Fixes for the driver to properly work with HVR-950
Devin Heitmueller4fd305b2008-06-04 13:43:46 -03008 - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
Devin Heitmuellere14b3652008-07-26 11:04:33 -03009 - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
Devin Heitmuellerbdfbf952008-04-17 21:38:27 -030010
Aidan Thornton3421b772008-04-17 21:40:36 -030011 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12
13 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030014 (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
15 (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License.
20 */
21
22#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030024#include <linux/usb.h>
25
26#include "em28xx.h"
27#include <media/v4l2-common.h>
28#include <media/videobuf-vmalloc.h>
Franklin Mengd7de5d82009-06-06 17:05:02 -030029#include <media/tuner.h>
30#include "tuner-simple.h"
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030031
32#include "lgdt330x.h"
Jarod Wilson7e48b302010-03-07 17:55:43 -030033#include "lgdt3305.h"
Aidan Thornton7e6388a2008-04-17 21:40:03 -030034#include "zl10353.h"
Robert Krakora6e7b9ea2009-01-18 21:59:34 -030035#include "s5h1409.h"
Devin Heitmueller4fb202a2009-07-12 17:51:12 -030036#include "mt352.h"
37#include "mt352_priv.h" /* FIXME */
Antti Palosaari285eb1a2009-09-15 14:42:13 -030038#include "tda1002x.h"
Jarod Wilson7e48b302010-03-07 17:55:43 -030039#include "tda18271.h"
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -030040#include "s921.h"
Devin Heitmueller75e2b862011-03-13 02:01:02 -030041#include "drxd.h"
Antti Palosaarid6a5f922011-04-07 16:34:44 -030042#include "cxd2820r.h"
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -030043#include "tda18271c2dd.h"
44#include "drxk.h"
Antti Palosaari36588712011-08-01 01:15:30 -030045#include "tda10071.h"
46#include "a8293.h"
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030047
48MODULE_DESCRIPTION("driver for em28xx based DVB cards");
49MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
50MODULE_LICENSE("GPL");
51
52static unsigned int debug;
53module_param(debug, int, 0644);
54MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
55
56DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
57
58#define dprintk(level, fmt, arg...) do { \
59if (debug >= level) \
Aidan Thornton3421b772008-04-17 21:40:36 -030060 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030061} while (0)
62
Aidan Thornton3421b772008-04-17 21:40:36 -030063#define EM28XX_DVB_NUM_BUFS 5
Aidan Thornton3421b772008-04-17 21:40:36 -030064#define EM28XX_DVB_MAX_PACKETS 64
65
66struct em28xx_dvb {
Antti Palosaarif71095b2011-04-07 15:51:52 -030067 struct dvb_frontend *fe[2];
Aidan Thornton3421b772008-04-17 21:40:36 -030068
69 /* feed count management */
70 struct mutex lock;
71 int nfeeds;
72
73 /* general boilerplate stuff */
74 struct dvb_adapter adapter;
75 struct dvb_demux demux;
76 struct dmxdev dmxdev;
77 struct dmx_frontend fe_hw;
78 struct dmx_frontend fe_mem;
79 struct dvb_net net;
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -030080
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -030081 /* Due to DRX-K - probably need changes */
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -030082 int (*gate_ctrl)(struct dvb_frontend *, int);
83 struct semaphore pll_mutex;
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -030084 bool dont_attach_fe1;
Aidan Thornton3421b772008-04-17 21:40:36 -030085};
86
87
88static inline void print_err_status(struct em28xx *dev,
89 int packet, int status)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030090{
Aidan Thornton3421b772008-04-17 21:40:36 -030091 char *errmsg = "Unknown";
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -030092
Aidan Thornton3421b772008-04-17 21:40:36 -030093 switch (status) {
94 case -ENOENT:
95 errmsg = "unlinked synchronuously";
96 break;
97 case -ECONNRESET:
98 errmsg = "unlinked asynchronuously";
99 break;
100 case -ENOSR:
101 errmsg = "Buffer error (overrun)";
102 break;
103 case -EPIPE:
104 errmsg = "Stalled (device not responding)";
105 break;
106 case -EOVERFLOW:
107 errmsg = "Babble (bad cable?)";
108 break;
109 case -EPROTO:
110 errmsg = "Bit-stuff error (bad cable?)";
111 break;
112 case -EILSEQ:
113 errmsg = "CRC/Timeout (could be anything)";
114 break;
115 case -ETIME:
116 errmsg = "Device does not respond";
117 break;
118 }
119 if (packet < 0) {
120 dprintk(1, "URB status %d [%s].\n", status, errmsg);
121 } else {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300122 dprintk(1, "URB packet %d, status %d [%s].\n",
123 packet, status, errmsg);
Aidan Thornton3421b772008-04-17 21:40:36 -0300124 }
125}
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300126
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300127static inline int em28xx_dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
Aidan Thornton3421b772008-04-17 21:40:36 -0300128{
129 int i;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300130
Aidan Thornton3421b772008-04-17 21:40:36 -0300131 if (!dev)
132 return 0;
133
134 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
135 return 0;
136
137 if (urb->status < 0) {
138 print_err_status(dev, -1, urb->status);
139 if (urb->status == -ENOENT)
140 return 0;
141 }
142
143 for (i = 0; i < urb->number_of_packets; i++) {
144 int status = urb->iso_frame_desc[i].status;
145
146 if (status < 0) {
147 print_err_status(dev, i, status);
148 if (urb->iso_frame_desc[i].status != -EPROTO)
149 continue;
150 }
151
152 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
153 urb->iso_frame_desc[i].offset,
154 urb->iso_frame_desc[i].actual_length);
155 }
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300156
157 return 0;
158}
159
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300160static int em28xx_start_streaming(struct em28xx_dvb *dvb)
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300161{
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300162 int rc;
Aidan Thornton3421b772008-04-17 21:40:36 -0300163 struct em28xx *dev = dvb->adapter.priv;
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300164 int max_dvb_packet_size;
Aidan Thornton3421b772008-04-17 21:40:36 -0300165
166 usb_set_interface(dev->udev, 0, 1);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300167 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
168 if (rc < 0)
169 return rc;
Aidan Thornton3421b772008-04-17 21:40:36 -0300170
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300171 max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
Mauro Carvalho Chehabf7acc4b2011-07-28 15:42:00 -0300172 if (max_dvb_packet_size < 0)
173 return max_dvb_packet_size;
174 dprintk(1, "Using %d buffers each with %d bytes\n",
175 EM28XX_DVB_NUM_BUFS,
176 max_dvb_packet_size);
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300177
Aidan Thornton3421b772008-04-17 21:40:36 -0300178 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
Devin Heitmuellerd18e2fd2009-05-16 17:09:28 -0300179 EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300180 em28xx_dvb_isoc_copy);
Aidan Thornton3421b772008-04-17 21:40:36 -0300181}
182
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300183static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300184{
Aidan Thornton3421b772008-04-17 21:40:36 -0300185 struct em28xx *dev = dvb->adapter.priv;
186
187 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300188
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -0300189 em28xx_set_mode(dev, EM28XX_SUSPEND);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300190
Aidan Thornton3421b772008-04-17 21:40:36 -0300191 return 0;
192}
193
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300194static int em28xx_start_feed(struct dvb_demux_feed *feed)
Aidan Thornton3421b772008-04-17 21:40:36 -0300195{
196 struct dvb_demux *demux = feed->demux;
197 struct em28xx_dvb *dvb = demux->priv;
198 int rc, ret;
199
200 if (!demux->dmx.frontend)
201 return -EINVAL;
202
203 mutex_lock(&dvb->lock);
204 dvb->nfeeds++;
205 rc = dvb->nfeeds;
206
207 if (dvb->nfeeds == 1) {
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300208 ret = em28xx_start_streaming(dvb);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300209 if (ret < 0)
210 rc = ret;
Aidan Thornton3421b772008-04-17 21:40:36 -0300211 }
212
213 mutex_unlock(&dvb->lock);
214 return rc;
215}
216
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300217static int em28xx_stop_feed(struct dvb_demux_feed *feed)
Aidan Thornton3421b772008-04-17 21:40:36 -0300218{
219 struct dvb_demux *demux = feed->demux;
220 struct em28xx_dvb *dvb = demux->priv;
221 int err = 0;
222
223 mutex_lock(&dvb->lock);
224 dvb->nfeeds--;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300225
226 if (0 == dvb->nfeeds)
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300227 err = em28xx_stop_streaming(dvb);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300228
Aidan Thornton3421b772008-04-17 21:40:36 -0300229 mutex_unlock(&dvb->lock);
230 return err;
231}
232
233
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300234
235/* ------------------------------------------------------------------ */
236static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
237{
238 struct em28xx *dev = fe->dvb->priv;
239
240 if (acquire)
241 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
242 else
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -0300243 return em28xx_set_mode(dev, EM28XX_SUSPEND);
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300244}
245
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300246/* ------------------------------------------------------------------ */
247
Mauro Carvalho Chehab227ad4a2008-04-17 21:37:40 -0300248static struct lgdt330x_config em2880_lgdt3303_dev = {
249 .demod_address = 0x0e,
250 .demod_chip = LGDT3303,
251};
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300252
Jarod Wilson7e48b302010-03-07 17:55:43 -0300253static struct lgdt3305_config em2870_lgdt3304_dev = {
254 .i2c_addr = 0x0e,
255 .demod_chip = LGDT3304,
256 .spectral_inversion = 1,
257 .deny_i2c_rptr = 1,
258 .mpeg_mode = LGDT3305_MPEG_PARALLEL,
259 .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
260 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
261 .vsb_if_khz = 3250,
262 .qam_if_khz = 4000,
263};
264
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300265static struct s921_config sharp_isdbt = {
266 .demod_address = 0x30 >> 1
267};
268
Aidan Thornton7e6388a2008-04-17 21:40:03 -0300269static struct zl10353_config em28xx_zl10353_with_xc3028 = {
270 .demod_address = (0x1e >> 1),
271 .no_tuner = 1,
272 .parallel_ts = 1,
273 .if2 = 45600,
274};
275
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300276static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
277 .demod_address = 0x32 >> 1,
278 .output_mode = S5H1409_PARALLEL_OUTPUT,
279 .gpio = S5H1409_GPIO_OFF,
280 .inversion = S5H1409_INVERSION_OFF,
281 .status_mode = S5H1409_DEMODLOCKING,
282 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
283};
284
Jarod Wilson7e48b302010-03-07 17:55:43 -0300285static struct tda18271_std_map kworld_a340_std_map = {
286 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
287 .if_lvl = 1, .rfagc_top = 0x37, },
288 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
289 .if_lvl = 1, .rfagc_top = 0x37, },
290};
291
292static struct tda18271_config kworld_a340_config = {
293 .std_map = &kworld_a340_std_map,
294};
295
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300296static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300297 .demod_address = (0x1e >> 1),
298 .no_tuner = 1,
299 .disable_i2c_gate_ctrl = 1,
300 .parallel_ts = 1,
301 .if2 = 45600,
302};
303
Devin Heitmueller75e2b862011-03-13 02:01:02 -0300304static struct drxd_config em28xx_drxd = {
305 .index = 0, .demod_address = 0x70, .demod_revision = 0xa2,
306 .demoda_address = 0x00, .pll_address = 0x00,
307 .pll_type = DRXD_PLL_NONE, .clock = 12000, .insert_rs_byte = 1,
308 .pll_set = NULL, .osc_deviation = NULL, .IF = 42800000,
Devin Heitmueller6b142b32011-03-13 02:02:01 -0300309 .disable_i2c_gate_ctrl = 1,
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300310};
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300311
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300312struct drxk_config terratec_h5_drxk = {
313 .adr = 0x29,
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300314 .single_master = 1,
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -0300315 .no_i2c_bridge = 1,
Mauro Carvalho Chehab8b9456a2011-07-11 14:33:51 -0300316 .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300317};
318
319static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
320{
321 struct em28xx_dvb *dvb = fe->sec_priv;
322 int status;
323
324 if (!dvb)
325 return -EINVAL;
326
327 if (enable) {
328 down(&dvb->pll_mutex);
329 status = dvb->gate_ctrl(fe, 1);
330 } else {
331 status = dvb->gate_ctrl(fe, 0);
332 up(&dvb->pll_mutex);
333 }
334 return status;
335}
336
337static void terratec_h5_init(struct em28xx *dev)
338{
339 int i;
340 struct em28xx_reg_seq terratec_h5_init[] = {
341 {EM28XX_R08_GPIO, 0xff, 0xff, 10},
342 {EM2874_R80_GPIO, 0xf6, 0xff, 100},
343 {EM2874_R80_GPIO, 0xf2, 0xff, 50},
344 {EM2874_R80_GPIO, 0xf6, 0xff, 100},
345 { -1, -1, -1, -1},
346 };
347 struct em28xx_reg_seq terratec_h5_end[] = {
348 {EM2874_R80_GPIO, 0xe6, 0xff, 100},
349 {EM2874_R80_GPIO, 0xa6, 0xff, 50},
350 {EM2874_R80_GPIO, 0xe6, 0xff, 100},
351 { -1, -1, -1, -1},
352 };
353 struct {
354 unsigned char r[4];
355 int len;
356 } regs[] = {
357 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
358 {{ 0x01, 0x02 }, 2},
359 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
360 {{ 0x01, 0x00 }, 2},
361 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
362 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
363 {{ 0x01, 0x00 }, 2},
364 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
365 {{ 0x04, 0x00 }, 2},
366 {{ 0x00, 0x04 }, 2},
367 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
368 {{ 0x04, 0x14 }, 2},
369 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
370 };
371
372 em28xx_gpio_set(dev, terratec_h5_init);
373 em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
374 msleep(10);
375 em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
376 msleep(10);
377
378 dev->i2c_client.addr = 0x82 >> 1;
379
380 for (i = 0; i < ARRAY_SIZE(regs); i++)
381 i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
382 em28xx_gpio_set(dev, terratec_h5_end);
383};
384
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300385static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300386{
387 /* Values extracted from a USB trace of the Terratec Windows driver */
388 static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
389 static u8 reset[] = { RESET, 0x80 };
390 static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
391 static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
392 static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
393 static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
394 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
395 static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
396 static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
Devin Heitmuellerff697862009-07-12 18:44:19 -0300397 static u8 tuner_go[] = { TUNER_GO, 0x01};
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300398
399 mt352_write(fe, clock_config, sizeof(clock_config));
400 udelay(200);
401 mt352_write(fe, reset, sizeof(reset));
402 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
403 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
404 mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
405 mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
406 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
407 mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
408 mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
409 mt352_write(fe, tuner_go, sizeof(tuner_go));
410 return 0;
411}
412
413static struct mt352_config terratec_xs_mt352_cfg = {
414 .demod_address = (0x1e >> 1),
415 .no_tuner = 1,
416 .if2 = 45600,
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300417 .demod_init = em28xx_mt352_terratec_xs_init,
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300418};
419
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300420static struct tda10023_config em28xx_tda10023_config = {
421 .demod_address = 0x0c,
422 .invert = 1,
423};
424
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300425static struct cxd2820r_config em28xx_cxd2820r_config = {
426 .i2c_address = (0xd8 >> 1),
427 .ts_mode = CXD2820R_TS_SERIAL,
428 .if_dvbt_6 = 3300,
429 .if_dvbt_7 = 3500,
430 .if_dvbt_8 = 4000,
431 .if_dvbt2_6 = 3300,
432 .if_dvbt2_7 = 3500,
433 .if_dvbt2_8 = 4000,
434 .if_dvbc = 5000,
435
436 /* enable LNA for DVB-T2 and DVB-C */
437 .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
438 .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
439};
440
441static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
442 .output_opt = TDA18271_OUTPUT_LT_OFF,
Steve Kerrison0db4bf42011-08-09 07:16:21 -0300443 .gate = TDA18271_GATE_DIGITAL,
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300444};
445
Antti Palosaari36588712011-08-01 01:15:30 -0300446static const struct tda10071_config em28xx_tda10071_config = {
447 .i2c_address = 0x55, /* (0xaa >> 1) */
448 .i2c_wr_max = 64,
449 .ts_mode = TDA10071_TS_SERIAL,
450 .spec_inv = 0,
451 .xtal = 40444000, /* 40.444 MHz */
452 .pll_multiplier = 20,
453};
454
455static const struct a8293_config em28xx_a8293_config = {
456 .i2c_addr = 0x08, /* (0x10 >> 1) */
457};
458
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300459/* ------------------------------------------------------------------ */
460
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300461static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300462{
463 struct dvb_frontend *fe;
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300464 struct xc2028_config cfg;
465
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300466 memset(&cfg, 0, sizeof(cfg));
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300467 cfg.i2c_adap = &dev->i2c_adap;
468 cfg.i2c_addr = addr;
Mauro Carvalho Chehab3ca9c092008-04-17 21:37:53 -0300469
Antti Palosaarif71095b2011-04-07 15:51:52 -0300470 if (!dev->dvb->fe[0]) {
Filipe Rosset480be182009-11-04 15:32:37 -0300471 em28xx_errdev("/2: dvb frontend not attached. "
472 "Can't attach xc3028\n");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300473 return -EINVAL;
474 }
475
Antti Palosaarif71095b2011-04-07 15:51:52 -0300476 fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300477 if (!fe) {
Filipe Rosset480be182009-11-04 15:32:37 -0300478 em28xx_errdev("/2: xc3028 attach failed\n");
Antti Palosaarif71095b2011-04-07 15:51:52 -0300479 dvb_frontend_detach(dev->dvb->fe[0]);
480 dev->dvb->fe[0] = NULL;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300481 return -EINVAL;
482 }
483
Filipe Rosset480be182009-11-04 15:32:37 -0300484 em28xx_info("%s/2: xc3028 attached\n", dev->name);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300485
486 return 0;
487}
488
Aidan Thornton3421b772008-04-17 21:40:36 -0300489/* ------------------------------------------------------------------ */
490
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300491static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
492 struct em28xx *dev, struct device *device)
Aidan Thornton3421b772008-04-17 21:40:36 -0300493{
494 int result;
495
496 mutex_init(&dvb->lock);
497
498 /* register adapter */
499 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
500 adapter_nr);
501 if (result < 0) {
502 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
503 dev->name, result);
504 goto fail_adapter;
505 }
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300506
507 /* Ensure all frontends negotiate bus access */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300508 dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
509 if (dvb->fe[1])
510 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300511
Aidan Thornton3421b772008-04-17 21:40:36 -0300512 dvb->adapter.priv = dev;
513
514 /* register frontend */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300515 result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
Aidan Thornton3421b772008-04-17 21:40:36 -0300516 if (result < 0) {
517 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
518 dev->name, result);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300519 goto fail_frontend0;
520 }
521
522 /* register 2nd frontend */
523 if (dvb->fe[1]) {
524 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
525 if (result < 0) {
526 printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
527 dev->name, result);
528 goto fail_frontend1;
529 }
Aidan Thornton3421b772008-04-17 21:40:36 -0300530 }
531
532 /* register demux stuff */
533 dvb->demux.dmx.capabilities =
534 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
535 DMX_MEMORY_BASED_FILTERING;
536 dvb->demux.priv = dvb;
537 dvb->demux.filternum = 256;
538 dvb->demux.feednum = 256;
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300539 dvb->demux.start_feed = em28xx_start_feed;
540 dvb->demux.stop_feed = em28xx_stop_feed;
Mauro Carvalho Chehabe3569ab2008-04-17 21:49:20 -0300541
Aidan Thornton3421b772008-04-17 21:40:36 -0300542 result = dvb_dmx_init(&dvb->demux);
543 if (result < 0) {
544 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
545 dev->name, result);
546 goto fail_dmx;
547 }
548
549 dvb->dmxdev.filternum = 256;
550 dvb->dmxdev.demux = &dvb->demux.dmx;
551 dvb->dmxdev.capabilities = 0;
552 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
553 if (result < 0) {
554 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
555 dev->name, result);
556 goto fail_dmxdev;
557 }
558
559 dvb->fe_hw.source = DMX_FRONTEND_0;
560 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
561 if (result < 0) {
562 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
563 dev->name, result);
564 goto fail_fe_hw;
565 }
566
567 dvb->fe_mem.source = DMX_MEMORY_FE;
568 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
569 if (result < 0) {
570 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
571 dev->name, result);
572 goto fail_fe_mem;
573 }
574
575 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
576 if (result < 0) {
577 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
578 dev->name, result);
579 goto fail_fe_conn;
580 }
581
582 /* register network adapter */
583 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
584 return 0;
585
586fail_fe_conn:
587 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
588fail_fe_mem:
589 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
590fail_fe_hw:
591 dvb_dmxdev_release(&dvb->dmxdev);
592fail_dmxdev:
593 dvb_dmx_release(&dvb->demux);
594fail_dmx:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300595 if (dvb->fe[1])
596 dvb_unregister_frontend(dvb->fe[1]);
597 dvb_unregister_frontend(dvb->fe[0]);
598fail_frontend1:
599 if (dvb->fe[1])
600 dvb_frontend_detach(dvb->fe[1]);
601fail_frontend0:
602 dvb_frontend_detach(dvb->fe[0]);
Aidan Thornton3421b772008-04-17 21:40:36 -0300603 dvb_unregister_adapter(&dvb->adapter);
604fail_adapter:
605 return result;
606}
607
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300608static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
Aidan Thornton3421b772008-04-17 21:40:36 -0300609{
610 dvb_net_release(&dvb->net);
611 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
612 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
613 dvb_dmxdev_release(&dvb->dmxdev);
614 dvb_dmx_release(&dvb->demux);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300615 if (dvb->fe[1])
616 dvb_unregister_frontend(dvb->fe[1]);
617 dvb_unregister_frontend(dvb->fe[0]);
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300618 if (dvb->fe[1] && !dvb->dont_attach_fe1)
Antti Palosaarif71095b2011-04-07 15:51:52 -0300619 dvb_frontend_detach(dvb->fe[1]);
620 dvb_frontend_detach(dvb->fe[0]);
Aidan Thornton3421b772008-04-17 21:40:36 -0300621 dvb_unregister_adapter(&dvb->adapter);
622}
623
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300624static int em28xx_dvb_init(struct em28xx *dev)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300625{
Antti Palosaarie3645432011-07-28 18:59:30 -0300626 int result = 0, mfe_shared = 0;
Aidan Thornton3421b772008-04-17 21:40:36 -0300627 struct em28xx_dvb *dvb;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300628
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300629 if (!dev->board.has_dvb) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300630 /* This device does not support the extension */
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300631 printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300632 return 0;
633 }
634
Aidan Thornton3421b772008-04-17 21:40:36 -0300635 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300636
637 if (dvb == NULL) {
Filipe Rosset480be182009-11-04 15:32:37 -0300638 em28xx_info("em28xx_dvb: memory allocation failed\n");
Aidan Thornton3421b772008-04-17 21:40:36 -0300639 return -ENOMEM;
640 }
641 dev->dvb = dvb;
Antti Palosaarif71095b2011-04-07 15:51:52 -0300642 dvb->fe[0] = dvb->fe[1] = NULL;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300643
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300644 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300645 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300646 /* init frontend */
647 switch (dev->model) {
Mauro Carvalho Chehabebaefdb2011-06-01 10:16:25 -0300648 case EM2874_BOARD_LEADERSHIP_ISDBT:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300649 dvb->fe[0] = dvb_attach(s921_attach,
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300650 &sharp_isdbt, &dev->i2c_adap);
651
Antti Palosaarif71095b2011-04-07 15:51:52 -0300652 if (!dvb->fe[0]) {
Mauro Carvalho Chehabca3dfd62010-09-10 17:29:14 -0300653 result = -EINVAL;
654 goto out_free;
655 }
656
657 break;
Douglas Schilling Landgraff89bc322008-12-01 21:01:04 -0300658 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
Mauro Carvalho Chehab10ac6602008-07-27 14:58:58 -0300659 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
Devin Heitmueller4fd305b2008-06-04 13:43:46 -0300660 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
Devin Heitmuellere14b3652008-07-26 11:04:33 -0300661 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300662 dvb->fe[0] = dvb_attach(lgdt330x_attach,
Aidan Thornton3421b772008-04-17 21:40:36 -0300663 &em2880_lgdt3303_dev,
664 &dev->i2c_adap);
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300665 if (em28xx_attach_xc3028(0x61, dev) < 0) {
Aidan Thornton3421b772008-04-17 21:40:36 -0300666 result = -EINVAL;
667 goto out_free;
668 }
Mauro Carvalho Chehab227ad4a2008-04-17 21:37:40 -0300669 break;
Thierry MERLE46510b52008-10-11 16:56:13 -0300670 case EM2880_BOARD_KWORLD_DVB_310U:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300671 dvb->fe[0] = dvb_attach(zl10353_attach,
Aidan Thornton3421b772008-04-17 21:40:36 -0300672 &em28xx_zl10353_with_xc3028,
673 &dev->i2c_adap);
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300674 if (em28xx_attach_xc3028(0x61, dev) < 0) {
Aidan Thornton3421b772008-04-17 21:40:36 -0300675 result = -EINVAL;
676 goto out_free;
677 }
Aidan Thornton7e6388a2008-04-17 21:40:03 -0300678 break;
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300679 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
Uroš Vamplec994d02009-09-30 22:53:37 -0300680 case EM2882_BOARD_TERRATEC_HYBRID_XS:
Devin Heitmueller01a5fd62009-08-07 09:25:06 -0300681 case EM2880_BOARD_EMPIRE_DUAL_TV:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300682 dvb->fe[0] = dvb_attach(zl10353_attach,
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300683 &em28xx_zl10353_xc3028_no_i2c_gate,
684 &dev->i2c_adap);
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300685 if (em28xx_attach_xc3028(0x61, dev) < 0) {
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300686 result = -EINVAL;
687 goto out_free;
688 }
689 break;
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300690 case EM2880_BOARD_TERRATEC_HYBRID_XS:
Catimimi65638012010-02-18 18:06:32 -0300691 case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
Devin Heitmuellerd5b3ba92009-07-08 21:51:35 -0300692 case EM2881_BOARD_PINNACLE_HYBRID_PRO:
Andrea.Amorosi76@gmail.com7ca7ef62010-02-09 17:53:38 -0300693 case EM2882_BOARD_DIKOM_DK300:
Antonio Larrosa811fab62010-03-04 22:19:48 -0300694 case EM2882_BOARD_KWORLD_VS_DVBT:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300695 dvb->fe[0] = dvb_attach(zl10353_attach,
Devin Heitmuellera84f79a2009-07-12 17:05:02 -0300696 &em28xx_zl10353_xc3028_no_i2c_gate,
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300697 &dev->i2c_adap);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300698 if (dvb->fe[0] == NULL) {
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300699 /* This board could have either a zl10353 or a mt352.
700 If the chip id isn't for zl10353, try mt352 */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300701 dvb->fe[0] = dvb_attach(mt352_attach,
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300702 &terratec_xs_mt352_cfg,
703 &dev->i2c_adap);
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300704 }
Devin Heitmueller4fb202a2009-07-12 17:51:12 -0300705
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300706 if (em28xx_attach_xc3028(0x61, dev) < 0) {
Devin Heitmuellerf7976082009-06-22 22:32:32 -0300707 result = -EINVAL;
708 goto out_free;
709 }
710 break;
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300711 case EM2883_BOARD_KWORLD_HYBRID_330U:
Devin Heitmueller19859222009-06-19 00:33:54 -0300712 case EM2882_BOARD_EVGA_INDTUBE:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300713 dvb->fe[0] = dvb_attach(s5h1409_attach,
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300714 &em28xx_s5h1409_with_xc3028,
715 &dev->i2c_adap);
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300716 if (em28xx_attach_xc3028(0x61, dev) < 0) {
Robert Krakora6e7b9ea2009-01-18 21:59:34 -0300717 result = -EINVAL;
718 goto out_free;
719 }
720 break;
Franklin Mengd7de5d82009-06-06 17:05:02 -0300721 case EM2882_BOARD_KWORLD_ATSC_315U:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300722 dvb->fe[0] = dvb_attach(lgdt330x_attach,
Franklin Mengd7de5d82009-06-06 17:05:02 -0300723 &em2880_lgdt3303_dev,
724 &dev->i2c_adap);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300725 if (dvb->fe[0] != NULL) {
726 if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
Franklin Mengd7de5d82009-06-06 17:05:02 -0300727 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
728 result = -EINVAL;
729 goto out_free;
730 }
731 }
732 break;
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300733 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
Devin Heitmuellerad9b4bb2011-03-13 02:09:59 -0300734 case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300735 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
Devin Heitmueller75e2b862011-03-13 02:01:02 -0300736 &dev->i2c_adap, &dev->udev->dev);
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300737 if (em28xx_attach_xc3028(0x61, dev) < 0) {
Devin Heitmueller17d9d552008-06-08 10:22:03 -0300738 result = -EINVAL;
739 goto out_free;
740 }
741 break;
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300742 case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
743 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300744 dvb->fe[0] = dvb_attach(tda10023_attach,
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300745 &em28xx_tda10023_config,
746 &dev->i2c_adap, 0x48);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300747 if (dvb->fe[0]) {
748 if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
Antti Palosaari285eb1a2009-09-15 14:42:13 -0300749 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
750 result = -EINVAL;
751 goto out_free;
752 }
753 }
754 break;
Jarod Wilson7e48b302010-03-07 17:55:43 -0300755 case EM2870_BOARD_KWORLD_A340:
Antti Palosaarif71095b2011-04-07 15:51:52 -0300756 dvb->fe[0] = dvb_attach(lgdt3305_attach,
Jarod Wilson7e48b302010-03-07 17:55:43 -0300757 &em2870_lgdt3304_dev,
758 &dev->i2c_adap);
Antti Palosaarif71095b2011-04-07 15:51:52 -0300759 if (dvb->fe[0] != NULL)
760 dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
Jarod Wilson7e48b302010-03-07 17:55:43 -0300761 &dev->i2c_adap, &kworld_a340_config);
762 break;
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300763 case EM28174_BOARD_PCTV_290E:
764 /* MFE
765 * FE 0 = DVB-T/T2 + FE 1 = DVB-C, both sharing same tuner. */
766 /* FE 0 */
767 dvb->fe[0] = dvb_attach(cxd2820r_attach,
768 &em28xx_cxd2820r_config, &dev->i2c_adap, NULL);
769 if (dvb->fe[0]) {
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300770 /* FE 0 attach tuner */
771 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
Steve Kerrison0db4bf42011-08-09 07:16:21 -0300772 &dev->i2c_adap, &em28xx_cxd2820r_tda18271_config)) {
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300773 dvb_frontend_detach(dvb->fe[0]);
774 result = -EINVAL;
775 goto out_free;
776 }
777 /* FE 1. This dvb_attach() cannot fail. */
778 dvb->fe[1] = dvb_attach(cxd2820r_attach, NULL, NULL,
779 dvb->fe[0]);
780 dvb->fe[1]->id = 1;
781 /* FE 1 attach tuner */
782 if (!dvb_attach(tda18271_attach, dvb->fe[1], 0x60,
Steve Kerrison0db4bf42011-08-09 07:16:21 -0300783 &dev->i2c_adap, &em28xx_cxd2820r_tda18271_config)) {
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300784 dvb_frontend_detach(dvb->fe[1]);
785 /* leave FE 0 still active */
786 }
Antti Palosaarie3645432011-07-28 18:59:30 -0300787
788 mfe_shared = 1;
Antti Palosaarid6a5f922011-04-07 16:34:44 -0300789 }
790 break;
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300791 case EM2884_BOARD_TERRATEC_H5:
792 terratec_h5_init(dev);
793
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300794 dvb->dont_attach_fe1 = 1;
795
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300796 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap, &dvb->fe[1]);
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300797 if (!dvb->fe[0]) {
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300798 result = -EINVAL;
799 goto out_free;
800 }
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300801
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300802 /* FIXME: do we need a pll semaphore? */
803 dvb->fe[0]->sec_priv = dvb;
804 sema_init(&dvb->pll_mutex, 1);
805 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
806 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300807 dvb->fe[1]->id = 1;
808
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300809 /* Attach tda18271 to DVB-C frontend */
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300810 if (dvb->fe[0]->ops.i2c_gate_ctrl)
811 dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
812 if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
813 result = -EINVAL;
814 goto out_free;
815 }
816 if (dvb->fe[0]->ops.i2c_gate_ctrl)
817 dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
Mauro Carvalho Chehabc4c3a3d2011-07-14 22:23:18 -0300818
819 /* Hack - needed by drxk/tda18271c2dd */
820 dvb->fe[1]->tuner_priv = dvb->fe[0]->tuner_priv;
821 memcpy(&dvb->fe[1]->ops.tuner_ops,
822 &dvb->fe[0]->ops.tuner_ops,
823 sizeof(dvb->fe[0]->ops.tuner_ops));
Mauro Carvalho Chehabfec528b2011-07-03 21:05:06 -0300824
825 break;
Antti Palosaari36588712011-08-01 01:15:30 -0300826 case EM28174_BOARD_PCTV_460E:
827 /* attach demod */
828 dvb->fe[0] = dvb_attach(tda10071_attach,
829 &em28xx_tda10071_config, &dev->i2c_adap);
830
831 /* attach SEC */
832 if (dvb->fe[0])
833 dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
834 &em28xx_a8293_config);
835 break;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300836 default:
Filipe Rosset480be182009-11-04 15:32:37 -0300837 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
838 " isn't supported yet\n");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300839 break;
840 }
Antti Palosaarif71095b2011-04-07 15:51:52 -0300841 if (NULL == dvb->fe[0]) {
Filipe Rosset480be182009-11-04 15:32:37 -0300842 em28xx_errdev("/2: frontend initialization failed\n");
Aidan Thornton3421b772008-04-17 21:40:36 -0300843 result = -EINVAL;
844 goto out_free;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300845 }
Michael Krufkyd7cba042008-09-12 13:31:45 -0300846 /* define general-purpose callback pointer */
Antti Palosaarif71095b2011-04-07 15:51:52 -0300847 dvb->fe[0]->callback = em28xx_tuner_callback;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300848
849 /* register everything */
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300850 result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
Aidan Thornton3421b772008-04-17 21:40:36 -0300851
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300852 if (result < 0)
Aidan Thornton3421b772008-04-17 21:40:36 -0300853 goto out_free;
Aidan Thornton3421b772008-04-17 21:40:36 -0300854
Antti Palosaarie3645432011-07-28 18:59:30 -0300855 /* MFE lock */
856 dvb->adapter.mfe_shared = mfe_shared;
857
Filipe Rosset480be182009-11-04 15:32:37 -0300858 em28xx_info("Successfully loaded em28xx-dvb\n");
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300859ret:
860 em28xx_set_mode(dev, EM28XX_SUSPEND);
861 mutex_unlock(&dev->lock);
862 return result;
Aidan Thornton3421b772008-04-17 21:40:36 -0300863
864out_free:
865 kfree(dvb);
866 dev->dvb = NULL;
Mauro Carvalho Chehab50133182010-04-07 17:07:58 -0300867 goto ret;
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300868}
869
Chris Rankin0b8bd832011-08-20 16:01:26 -0300870static inline void prevent_sleep(struct dvb_frontend_ops *ops)
871{
872 ops->set_voltage = NULL;
873 ops->sleep = NULL;
874 ops->tuner_ops.sleep = NULL;
875}
876
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300877static int em28xx_dvb_fini(struct em28xx *dev)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300878{
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300879 if (!dev->board.has_dvb) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300880 /* This device does not support the extension */
881 return 0;
882 }
883
Aidan Thornton3421b772008-04-17 21:40:36 -0300884 if (dev->dvb) {
Chris Rankin0b8bd832011-08-20 16:01:26 -0300885 struct em28xx_dvb *dvb = dev->dvb;
886
887 if (dev->state & DEV_DISCONNECTED) {
888 /* We cannot tell the device to sleep
889 * once it has been unplugged. */
890 if (dvb->fe[0])
891 prevent_sleep(&dvb->fe[0]->ops);
892 if (dvb->fe[1])
893 prevent_sleep(&dvb->fe[1]->ops);
894 }
895
896 em28xx_unregister_dvb(dvb);
897 kfree(dvb);
Aidan Thornton3421b772008-04-17 21:40:36 -0300898 dev->dvb = NULL;
899 }
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300900
901 return 0;
902}
903
904static struct em28xx_ops dvb_ops = {
905 .id = EM28XX_DVB,
906 .name = "Em28xx dvb Extension",
Jarod Wilsonf2d0c1c2011-07-14 19:06:08 -0300907 .init = em28xx_dvb_init,
908 .fini = em28xx_dvb_fini,
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -0300909};
910
911static int __init em28xx_dvb_register(void)
912{
913 return em28xx_register_extension(&dvb_ops);
914}
915
916static void __exit em28xx_dvb_unregister(void)
917{
918 em28xx_unregister_extension(&dvb_ops);
919}
920
921module_init(em28xx_dvb_register);
922module_exit(em28xx_dvb_unregister);