xref: /openbmc/linux/drivers/media/usb/em28xx/em28xx-dvb.c (revision a36a66d7aaa6468a91941ffcfb6d324f28825910)
10c0d06caSMauro Carvalho Chehab /*
20c0d06caSMauro Carvalho Chehab  DVB device driver for em28xx
30c0d06caSMauro Carvalho Chehab 
40c0d06caSMauro Carvalho Chehab  (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
50c0d06caSMauro Carvalho Chehab 
60c0d06caSMauro Carvalho Chehab  (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
70c0d06caSMauro Carvalho Chehab 	- Fixes for the driver to properly work with HVR-950
80c0d06caSMauro Carvalho Chehab 	- Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
90c0d06caSMauro Carvalho Chehab 	- Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
100c0d06caSMauro Carvalho Chehab 
110c0d06caSMauro Carvalho Chehab  (c) 2008 Aidan Thornton <makosoft@googlemail.com>
120c0d06caSMauro Carvalho Chehab 
130c0d06caSMauro Carvalho Chehab  Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
140c0d06caSMauro Carvalho Chehab 	(c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
150c0d06caSMauro Carvalho Chehab 	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
160c0d06caSMauro Carvalho Chehab 
170c0d06caSMauro Carvalho Chehab  This program is free software; you can redistribute it and/or modify
180c0d06caSMauro Carvalho Chehab  it under the terms of the GNU General Public License as published by
190c0d06caSMauro Carvalho Chehab  the Free Software Foundation; either version 2 of the License.
200c0d06caSMauro Carvalho Chehab  */
210c0d06caSMauro Carvalho Chehab 
220c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
230c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
240c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
250c0d06caSMauro Carvalho Chehab 
260c0d06caSMauro Carvalho Chehab #include "em28xx.h"
270c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
280c0d06caSMauro Carvalho Chehab #include <media/videobuf-vmalloc.h>
290c0d06caSMauro Carvalho Chehab #include <media/tuner.h>
300c0d06caSMauro Carvalho Chehab #include "tuner-simple.h"
311e8f31f3SAntti Palosaari #include <linux/gpio.h>
320c0d06caSMauro Carvalho Chehab 
330c0d06caSMauro Carvalho Chehab #include "lgdt330x.h"
340c0d06caSMauro Carvalho Chehab #include "lgdt3305.h"
350c0d06caSMauro Carvalho Chehab #include "zl10353.h"
360c0d06caSMauro Carvalho Chehab #include "s5h1409.h"
370c0d06caSMauro Carvalho Chehab #include "mt352.h"
380c0d06caSMauro Carvalho Chehab #include "mt352_priv.h" /* FIXME */
390c0d06caSMauro Carvalho Chehab #include "tda1002x.h"
400c0d06caSMauro Carvalho Chehab #include "tda18271.h"
410c0d06caSMauro Carvalho Chehab #include "s921.h"
420c0d06caSMauro Carvalho Chehab #include "drxd.h"
430c0d06caSMauro Carvalho Chehab #include "cxd2820r.h"
440c0d06caSMauro Carvalho Chehab #include "tda18271c2dd.h"
450c0d06caSMauro Carvalho Chehab #include "drxk.h"
460c0d06caSMauro Carvalho Chehab #include "tda10071.h"
470c0d06caSMauro Carvalho Chehab #include "a8293.h"
480c0d06caSMauro Carvalho Chehab #include "qt1010.h"
490c0d06caSMauro Carvalho Chehab 
500c0d06caSMauro Carvalho Chehab MODULE_DESCRIPTION("driver for em28xx based DVB cards");
510c0d06caSMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
520c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
530c0d06caSMauro Carvalho Chehab 
540c0d06caSMauro Carvalho Chehab static unsigned int debug;
550c0d06caSMauro Carvalho Chehab module_param(debug, int, 0644);
560c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
570c0d06caSMauro Carvalho Chehab 
580c0d06caSMauro Carvalho Chehab DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
590c0d06caSMauro Carvalho Chehab 
600c0d06caSMauro Carvalho Chehab #define dprintk(level, fmt, arg...) do {			\
610c0d06caSMauro Carvalho Chehab if (debug >= level) 						\
620c0d06caSMauro Carvalho Chehab 	printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg);	\
630c0d06caSMauro Carvalho Chehab } while (0)
640c0d06caSMauro Carvalho Chehab 
650c0d06caSMauro Carvalho Chehab struct em28xx_dvb {
660c0d06caSMauro Carvalho Chehab 	struct dvb_frontend        *fe[2];
670c0d06caSMauro Carvalho Chehab 
680c0d06caSMauro Carvalho Chehab 	/* feed count management */
690c0d06caSMauro Carvalho Chehab 	struct mutex               lock;
700c0d06caSMauro Carvalho Chehab 	int                        nfeeds;
710c0d06caSMauro Carvalho Chehab 
720c0d06caSMauro Carvalho Chehab 	/* general boilerplate stuff */
730c0d06caSMauro Carvalho Chehab 	struct dvb_adapter         adapter;
740c0d06caSMauro Carvalho Chehab 	struct dvb_demux           demux;
750c0d06caSMauro Carvalho Chehab 	struct dmxdev              dmxdev;
760c0d06caSMauro Carvalho Chehab 	struct dmx_frontend        fe_hw;
770c0d06caSMauro Carvalho Chehab 	struct dmx_frontend        fe_mem;
780c0d06caSMauro Carvalho Chehab 	struct dvb_net             net;
790c0d06caSMauro Carvalho Chehab 
800c0d06caSMauro Carvalho Chehab 	/* Due to DRX-K - probably need changes */
810c0d06caSMauro Carvalho Chehab 	int (*gate_ctrl)(struct dvb_frontend *, int);
820c0d06caSMauro Carvalho Chehab 	struct semaphore      pll_mutex;
830c0d06caSMauro Carvalho Chehab 	bool			dont_attach_fe1;
8413a5336eSAntti Palosaari 	int			lna_gpio;
850c0d06caSMauro Carvalho Chehab };
860c0d06caSMauro Carvalho Chehab 
870c0d06caSMauro Carvalho Chehab 
880c0d06caSMauro Carvalho Chehab static inline void print_err_status(struct em28xx *dev,
890c0d06caSMauro Carvalho Chehab 				     int packet, int status)
900c0d06caSMauro Carvalho Chehab {
910c0d06caSMauro Carvalho Chehab 	char *errmsg = "Unknown";
920c0d06caSMauro Carvalho Chehab 
930c0d06caSMauro Carvalho Chehab 	switch (status) {
940c0d06caSMauro Carvalho Chehab 	case -ENOENT:
950c0d06caSMauro Carvalho Chehab 		errmsg = "unlinked synchronuously";
960c0d06caSMauro Carvalho Chehab 		break;
970c0d06caSMauro Carvalho Chehab 	case -ECONNRESET:
980c0d06caSMauro Carvalho Chehab 		errmsg = "unlinked asynchronuously";
990c0d06caSMauro Carvalho Chehab 		break;
1000c0d06caSMauro Carvalho Chehab 	case -ENOSR:
1010c0d06caSMauro Carvalho Chehab 		errmsg = "Buffer error (overrun)";
1020c0d06caSMauro Carvalho Chehab 		break;
1030c0d06caSMauro Carvalho Chehab 	case -EPIPE:
1040c0d06caSMauro Carvalho Chehab 		errmsg = "Stalled (device not responding)";
1050c0d06caSMauro Carvalho Chehab 		break;
1060c0d06caSMauro Carvalho Chehab 	case -EOVERFLOW:
1070c0d06caSMauro Carvalho Chehab 		errmsg = "Babble (bad cable?)";
1080c0d06caSMauro Carvalho Chehab 		break;
1090c0d06caSMauro Carvalho Chehab 	case -EPROTO:
1100c0d06caSMauro Carvalho Chehab 		errmsg = "Bit-stuff error (bad cable?)";
1110c0d06caSMauro Carvalho Chehab 		break;
1120c0d06caSMauro Carvalho Chehab 	case -EILSEQ:
1130c0d06caSMauro Carvalho Chehab 		errmsg = "CRC/Timeout (could be anything)";
1140c0d06caSMauro Carvalho Chehab 		break;
1150c0d06caSMauro Carvalho Chehab 	case -ETIME:
1160c0d06caSMauro Carvalho Chehab 		errmsg = "Device does not respond";
1170c0d06caSMauro Carvalho Chehab 		break;
1180c0d06caSMauro Carvalho Chehab 	}
1190c0d06caSMauro Carvalho Chehab 	if (packet < 0) {
1200c0d06caSMauro Carvalho Chehab 		dprintk(1, "URB status %d [%s].\n", status, errmsg);
1210c0d06caSMauro Carvalho Chehab 	} else {
1220c0d06caSMauro Carvalho Chehab 		dprintk(1, "URB packet %d, status %d [%s].\n",
1230c0d06caSMauro Carvalho Chehab 			packet, status, errmsg);
1240c0d06caSMauro Carvalho Chehab 	}
1250c0d06caSMauro Carvalho Chehab }
1260c0d06caSMauro Carvalho Chehab 
1270c0d06caSMauro Carvalho Chehab static inline int em28xx_dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
1280c0d06caSMauro Carvalho Chehab {
1290c0d06caSMauro Carvalho Chehab 	int i;
1300c0d06caSMauro Carvalho Chehab 
1310c0d06caSMauro Carvalho Chehab 	if (!dev)
1320c0d06caSMauro Carvalho Chehab 		return 0;
1330c0d06caSMauro Carvalho Chehab 
1340c0d06caSMauro Carvalho Chehab 	if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
1350c0d06caSMauro Carvalho Chehab 		return 0;
1360c0d06caSMauro Carvalho Chehab 
1370c0d06caSMauro Carvalho Chehab 	if (urb->status < 0) {
1380c0d06caSMauro Carvalho Chehab 		print_err_status(dev, -1, urb->status);
1390c0d06caSMauro Carvalho Chehab 		if (urb->status == -ENOENT)
1400c0d06caSMauro Carvalho Chehab 			return 0;
1410c0d06caSMauro Carvalho Chehab 	}
1420c0d06caSMauro Carvalho Chehab 
1430c0d06caSMauro Carvalho Chehab 	for (i = 0; i < urb->number_of_packets; i++) {
1440c0d06caSMauro Carvalho Chehab 		int status = urb->iso_frame_desc[i].status;
1450c0d06caSMauro Carvalho Chehab 
1460c0d06caSMauro Carvalho Chehab 		if (status < 0) {
1470c0d06caSMauro Carvalho Chehab 			print_err_status(dev, i, status);
1480c0d06caSMauro Carvalho Chehab 			if (urb->iso_frame_desc[i].status != -EPROTO)
1490c0d06caSMauro Carvalho Chehab 				continue;
1500c0d06caSMauro Carvalho Chehab 		}
1510c0d06caSMauro Carvalho Chehab 
1520c0d06caSMauro Carvalho Chehab 		dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
1530c0d06caSMauro Carvalho Chehab 				 urb->iso_frame_desc[i].offset,
1540c0d06caSMauro Carvalho Chehab 				 urb->iso_frame_desc[i].actual_length);
1550c0d06caSMauro Carvalho Chehab 	}
1560c0d06caSMauro Carvalho Chehab 
1570c0d06caSMauro Carvalho Chehab 	return 0;
1580c0d06caSMauro Carvalho Chehab }
1590c0d06caSMauro Carvalho Chehab 
1600c0d06caSMauro Carvalho Chehab static int em28xx_start_streaming(struct em28xx_dvb *dvb)
1610c0d06caSMauro Carvalho Chehab {
1620c0d06caSMauro Carvalho Chehab 	int rc;
1630c0d06caSMauro Carvalho Chehab 	struct em28xx *dev = dvb->adapter.priv;
1640c0d06caSMauro Carvalho Chehab 	int max_dvb_packet_size;
1650c0d06caSMauro Carvalho Chehab 
1660c0d06caSMauro Carvalho Chehab 	usb_set_interface(dev->udev, 0, dev->dvb_alt);
1670c0d06caSMauro Carvalho Chehab 	rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
1680c0d06caSMauro Carvalho Chehab 	if (rc < 0)
1690c0d06caSMauro Carvalho Chehab 		return rc;
1700c0d06caSMauro Carvalho Chehab 
1710c0d06caSMauro Carvalho Chehab 	max_dvb_packet_size = dev->dvb_max_pkt_size;
1720c0d06caSMauro Carvalho Chehab 	if (max_dvb_packet_size < 0)
1730c0d06caSMauro Carvalho Chehab 		return max_dvb_packet_size;
1740c0d06caSMauro Carvalho Chehab 	dprintk(1, "Using %d buffers each with %d x %d bytes\n",
1750c0d06caSMauro Carvalho Chehab 		EM28XX_DVB_NUM_BUFS,
1760c0d06caSMauro Carvalho Chehab 		EM28XX_DVB_MAX_PACKETS,
1770c0d06caSMauro Carvalho Chehab 		max_dvb_packet_size);
1780c0d06caSMauro Carvalho Chehab 
1790c0d06caSMauro Carvalho Chehab 	return em28xx_init_isoc(dev, EM28XX_DIGITAL_MODE,
1800c0d06caSMauro Carvalho Chehab 				EM28XX_DVB_MAX_PACKETS, EM28XX_DVB_NUM_BUFS,
1810c0d06caSMauro Carvalho Chehab 				max_dvb_packet_size, em28xx_dvb_isoc_copy);
1820c0d06caSMauro Carvalho Chehab }
1830c0d06caSMauro Carvalho Chehab 
1840c0d06caSMauro Carvalho Chehab static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
1850c0d06caSMauro Carvalho Chehab {
1860c0d06caSMauro Carvalho Chehab 	struct em28xx *dev = dvb->adapter.priv;
1870c0d06caSMauro Carvalho Chehab 
1880c0d06caSMauro Carvalho Chehab 	em28xx_stop_urbs(dev);
1890c0d06caSMauro Carvalho Chehab 
1900c0d06caSMauro Carvalho Chehab 	em28xx_set_mode(dev, EM28XX_SUSPEND);
1910c0d06caSMauro Carvalho Chehab 
1920c0d06caSMauro Carvalho Chehab 	return 0;
1930c0d06caSMauro Carvalho Chehab }
1940c0d06caSMauro Carvalho Chehab 
1950c0d06caSMauro Carvalho Chehab static int em28xx_start_feed(struct dvb_demux_feed *feed)
1960c0d06caSMauro Carvalho Chehab {
1970c0d06caSMauro Carvalho Chehab 	struct dvb_demux *demux  = feed->demux;
1980c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb = demux->priv;
1990c0d06caSMauro Carvalho Chehab 	int rc, ret;
2000c0d06caSMauro Carvalho Chehab 
2010c0d06caSMauro Carvalho Chehab 	if (!demux->dmx.frontend)
2020c0d06caSMauro Carvalho Chehab 		return -EINVAL;
2030c0d06caSMauro Carvalho Chehab 
2040c0d06caSMauro Carvalho Chehab 	mutex_lock(&dvb->lock);
2050c0d06caSMauro Carvalho Chehab 	dvb->nfeeds++;
2060c0d06caSMauro Carvalho Chehab 	rc = dvb->nfeeds;
2070c0d06caSMauro Carvalho Chehab 
2080c0d06caSMauro Carvalho Chehab 	if (dvb->nfeeds == 1) {
2090c0d06caSMauro Carvalho Chehab 		ret = em28xx_start_streaming(dvb);
2100c0d06caSMauro Carvalho Chehab 		if (ret < 0)
2110c0d06caSMauro Carvalho Chehab 			rc = ret;
2120c0d06caSMauro Carvalho Chehab 	}
2130c0d06caSMauro Carvalho Chehab 
2140c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dvb->lock);
2150c0d06caSMauro Carvalho Chehab 	return rc;
2160c0d06caSMauro Carvalho Chehab }
2170c0d06caSMauro Carvalho Chehab 
2180c0d06caSMauro Carvalho Chehab static int em28xx_stop_feed(struct dvb_demux_feed *feed)
2190c0d06caSMauro Carvalho Chehab {
2200c0d06caSMauro Carvalho Chehab 	struct dvb_demux *demux  = feed->demux;
2210c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb = demux->priv;
2220c0d06caSMauro Carvalho Chehab 	int err = 0;
2230c0d06caSMauro Carvalho Chehab 
2240c0d06caSMauro Carvalho Chehab 	mutex_lock(&dvb->lock);
2250c0d06caSMauro Carvalho Chehab 	dvb->nfeeds--;
2260c0d06caSMauro Carvalho Chehab 
2270c0d06caSMauro Carvalho Chehab 	if (0 == dvb->nfeeds)
2280c0d06caSMauro Carvalho Chehab 		err = em28xx_stop_streaming(dvb);
2290c0d06caSMauro Carvalho Chehab 
2300c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dvb->lock);
2310c0d06caSMauro Carvalho Chehab 	return err;
2320c0d06caSMauro Carvalho Chehab }
2330c0d06caSMauro Carvalho Chehab 
2340c0d06caSMauro Carvalho Chehab 
2350c0d06caSMauro Carvalho Chehab 
2360c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
2370c0d06caSMauro Carvalho Chehab static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
2380c0d06caSMauro Carvalho Chehab {
2390c0d06caSMauro Carvalho Chehab 	struct em28xx *dev = fe->dvb->priv;
2400c0d06caSMauro Carvalho Chehab 
2410c0d06caSMauro Carvalho Chehab 	if (acquire)
2420c0d06caSMauro Carvalho Chehab 		return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
2430c0d06caSMauro Carvalho Chehab 	else
2440c0d06caSMauro Carvalho Chehab 		return em28xx_set_mode(dev, EM28XX_SUSPEND);
2450c0d06caSMauro Carvalho Chehab }
2460c0d06caSMauro Carvalho Chehab 
2470c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
2480c0d06caSMauro Carvalho Chehab 
2490c0d06caSMauro Carvalho Chehab static struct lgdt330x_config em2880_lgdt3303_dev = {
2500c0d06caSMauro Carvalho Chehab 	.demod_address = 0x0e,
2510c0d06caSMauro Carvalho Chehab 	.demod_chip = LGDT3303,
2520c0d06caSMauro Carvalho Chehab };
2530c0d06caSMauro Carvalho Chehab 
2540c0d06caSMauro Carvalho Chehab static struct lgdt3305_config em2870_lgdt3304_dev = {
2550c0d06caSMauro Carvalho Chehab 	.i2c_addr           = 0x0e,
2560c0d06caSMauro Carvalho Chehab 	.demod_chip         = LGDT3304,
2570c0d06caSMauro Carvalho Chehab 	.spectral_inversion = 1,
2580c0d06caSMauro Carvalho Chehab 	.deny_i2c_rptr      = 1,
2590c0d06caSMauro Carvalho Chehab 	.mpeg_mode          = LGDT3305_MPEG_PARALLEL,
2600c0d06caSMauro Carvalho Chehab 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
2610c0d06caSMauro Carvalho Chehab 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
2620c0d06caSMauro Carvalho Chehab 	.vsb_if_khz         = 3250,
2630c0d06caSMauro Carvalho Chehab 	.qam_if_khz         = 4000,
2640c0d06caSMauro Carvalho Chehab };
2650c0d06caSMauro Carvalho Chehab 
2660c0d06caSMauro Carvalho Chehab static struct s921_config sharp_isdbt = {
2670c0d06caSMauro Carvalho Chehab 	.demod_address = 0x30 >> 1
2680c0d06caSMauro Carvalho Chehab };
2690c0d06caSMauro Carvalho Chehab 
2700c0d06caSMauro Carvalho Chehab static struct zl10353_config em28xx_zl10353_with_xc3028 = {
2710c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
2720c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
2730c0d06caSMauro Carvalho Chehab 	.parallel_ts = 1,
2740c0d06caSMauro Carvalho Chehab 	.if2 = 45600,
2750c0d06caSMauro Carvalho Chehab };
2760c0d06caSMauro Carvalho Chehab 
2770c0d06caSMauro Carvalho Chehab static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
2780c0d06caSMauro Carvalho Chehab 	.demod_address = 0x32 >> 1,
2790c0d06caSMauro Carvalho Chehab 	.output_mode   = S5H1409_PARALLEL_OUTPUT,
2800c0d06caSMauro Carvalho Chehab 	.gpio          = S5H1409_GPIO_OFF,
2810c0d06caSMauro Carvalho Chehab 	.inversion     = S5H1409_INVERSION_OFF,
2820c0d06caSMauro Carvalho Chehab 	.status_mode   = S5H1409_DEMODLOCKING,
2830c0d06caSMauro Carvalho Chehab 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
2840c0d06caSMauro Carvalho Chehab };
2850c0d06caSMauro Carvalho Chehab 
2860c0d06caSMauro Carvalho Chehab static struct tda18271_std_map kworld_a340_std_map = {
2870c0d06caSMauro Carvalho Chehab 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
2880c0d06caSMauro Carvalho Chehab 		      .if_lvl = 1, .rfagc_top = 0x37, },
2890c0d06caSMauro Carvalho Chehab 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
2900c0d06caSMauro Carvalho Chehab 		      .if_lvl = 1, .rfagc_top = 0x37, },
2910c0d06caSMauro Carvalho Chehab };
2920c0d06caSMauro Carvalho Chehab 
2930c0d06caSMauro Carvalho Chehab static struct tda18271_config kworld_a340_config = {
2940c0d06caSMauro Carvalho Chehab 	.std_map           = &kworld_a340_std_map,
2950c0d06caSMauro Carvalho Chehab };
2960c0d06caSMauro Carvalho Chehab 
2970c0d06caSMauro Carvalho Chehab static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
2980c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
2990c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
3000c0d06caSMauro Carvalho Chehab 	.disable_i2c_gate_ctrl = 1,
3010c0d06caSMauro Carvalho Chehab 	.parallel_ts = 1,
3020c0d06caSMauro Carvalho Chehab 	.if2 = 45600,
3030c0d06caSMauro Carvalho Chehab };
3040c0d06caSMauro Carvalho Chehab 
3050c0d06caSMauro Carvalho Chehab static struct drxd_config em28xx_drxd = {
3060c0d06caSMauro Carvalho Chehab 	.demod_address = 0x70,
3070c0d06caSMauro Carvalho Chehab 	.demod_revision = 0xa2,
3080c0d06caSMauro Carvalho Chehab 	.pll_type = DRXD_PLL_NONE,
3090c0d06caSMauro Carvalho Chehab 	.clock = 12000,
3100c0d06caSMauro Carvalho Chehab 	.insert_rs_byte = 1,
3110c0d06caSMauro Carvalho Chehab 	.IF = 42800000,
3120c0d06caSMauro Carvalho Chehab 	.disable_i2c_gate_ctrl = 1,
3130c0d06caSMauro Carvalho Chehab };
3140c0d06caSMauro Carvalho Chehab 
3150c0d06caSMauro Carvalho Chehab static struct drxk_config terratec_h5_drxk = {
3160c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
3170c0d06caSMauro Carvalho Chehab 	.single_master = 1,
3180c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
3190c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-usb-terratec-h5-drxk.fw",
3200c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
3210c0d06caSMauro Carvalho Chehab };
3220c0d06caSMauro Carvalho Chehab 
3230c0d06caSMauro Carvalho Chehab static struct drxk_config hauppauge_930c_drxk = {
3240c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
3250c0d06caSMauro Carvalho Chehab 	.single_master = 1,
3260c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
3270c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
3280c0d06caSMauro Carvalho Chehab 	.chunk_size = 56,
3290c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
3300c0d06caSMauro Carvalho Chehab };
3310c0d06caSMauro Carvalho Chehab 
3320c0d06caSMauro Carvalho Chehab struct drxk_config terratec_htc_stick_drxk = {
3330c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
3340c0d06caSMauro Carvalho Chehab 	.single_master = 1,
3350c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
3360c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
3370c0d06caSMauro Carvalho Chehab 	.chunk_size = 54,
3380c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
3390c0d06caSMauro Carvalho Chehab 	/* Required for the antenna_gpio to disable LNA. */
3400c0d06caSMauro Carvalho Chehab 	.antenna_dvbt = true,
3410c0d06caSMauro Carvalho Chehab 	/* The windows driver uses the same. This will disable LNA. */
3420c0d06caSMauro Carvalho Chehab 	.antenna_gpio = 0x6,
3430c0d06caSMauro Carvalho Chehab };
3440c0d06caSMauro Carvalho Chehab 
3450c0d06caSMauro Carvalho Chehab static struct drxk_config maxmedia_ub425_tc_drxk = {
3460c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
3470c0d06caSMauro Carvalho Chehab 	.single_master = 1,
3480c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
3490c0d06caSMauro Carvalho Chehab };
3500c0d06caSMauro Carvalho Chehab 
3510c0d06caSMauro Carvalho Chehab static struct drxk_config pctv_520e_drxk = {
3520c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
3530c0d06caSMauro Carvalho Chehab 	.single_master = 1,
3540c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-demod-drxk-pctv.fw",
3550c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
3560c0d06caSMauro Carvalho Chehab 	.chunk_size = 58,
3570c0d06caSMauro Carvalho Chehab 	.antenna_dvbt = true, /* disable LNA */
3580c0d06caSMauro Carvalho Chehab 	.antenna_gpio = (1 << 2), /* disable LNA */
3590c0d06caSMauro Carvalho Chehab };
3600c0d06caSMauro Carvalho Chehab 
3610c0d06caSMauro Carvalho Chehab static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
3620c0d06caSMauro Carvalho Chehab {
3630c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb = fe->sec_priv;
3640c0d06caSMauro Carvalho Chehab 	int status;
3650c0d06caSMauro Carvalho Chehab 
3660c0d06caSMauro Carvalho Chehab 	if (!dvb)
3670c0d06caSMauro Carvalho Chehab 		return -EINVAL;
3680c0d06caSMauro Carvalho Chehab 
3690c0d06caSMauro Carvalho Chehab 	if (enable) {
3700c0d06caSMauro Carvalho Chehab 		down(&dvb->pll_mutex);
3710c0d06caSMauro Carvalho Chehab 		status = dvb->gate_ctrl(fe, 1);
3720c0d06caSMauro Carvalho Chehab 	} else {
3730c0d06caSMauro Carvalho Chehab 		status = dvb->gate_ctrl(fe, 0);
3740c0d06caSMauro Carvalho Chehab 		up(&dvb->pll_mutex);
3750c0d06caSMauro Carvalho Chehab 	}
3760c0d06caSMauro Carvalho Chehab 	return status;
3770c0d06caSMauro Carvalho Chehab }
3780c0d06caSMauro Carvalho Chehab 
3790c0d06caSMauro Carvalho Chehab static void hauppauge_hvr930c_init(struct em28xx *dev)
3800c0d06caSMauro Carvalho Chehab {
3810c0d06caSMauro Carvalho Chehab 	int i;
3820c0d06caSMauro Carvalho Chehab 
3830c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
3840c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xff,	0xff,	0x65},
3850c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xfb,	0xff,	0x32},
3860c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xff,	0xff,	0xb8},
3870c0d06caSMauro Carvalho Chehab 		{ -1,                   -1,     -1,     -1},
3880c0d06caSMauro Carvalho Chehab 	};
3890c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
3900c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xef,	0xff,	0x01},
3910c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xaf,	0xff,	0x65},
3920c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xef,	0xff,	0x76},
3930c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xef,	0xff,	0x01},
3940c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xcf,	0xff,	0x0b},
3950c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xef,	0xff,	0x40},
3960c0d06caSMauro Carvalho Chehab 
3970c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xcf,	0xff,	0x65},
3980c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xef,	0xff,	0x65},
3990c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xcf,	0xff,	0x0b},
4000c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xef,	0xff,	0x65},
4010c0d06caSMauro Carvalho Chehab 
4020c0d06caSMauro Carvalho Chehab 		{ -1,                   -1,     -1,     -1},
4030c0d06caSMauro Carvalho Chehab 	};
4040c0d06caSMauro Carvalho Chehab 
4050c0d06caSMauro Carvalho Chehab 	struct {
4060c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
4070c0d06caSMauro Carvalho Chehab 		int len;
4080c0d06caSMauro Carvalho Chehab 	} regs[] = {
4090c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
4100c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
4110c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
4120c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
4130c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
4140c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
4150c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
4160c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
4170c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x00 }, 2},
4180c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04 }, 2},
4190c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
4200c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14 }, 2},
4210c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
4220c0d06caSMauro Carvalho Chehab 	};
4230c0d06caSMauro Carvalho Chehab 
4240c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, hauppauge_hvr930c_init);
4250c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
4260c0d06caSMauro Carvalho Chehab 	msleep(10);
4270c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
4280c0d06caSMauro Carvalho Chehab 	msleep(10);
4290c0d06caSMauro Carvalho Chehab 
4300c0d06caSMauro Carvalho Chehab 	dev->i2c_client.addr = 0x82 >> 1;
4310c0d06caSMauro Carvalho Chehab 
4320c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
4330c0d06caSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
4340c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, hauppauge_hvr930c_end);
4350c0d06caSMauro Carvalho Chehab 
4360c0d06caSMauro Carvalho Chehab 	msleep(100);
4370c0d06caSMauro Carvalho Chehab 
4380c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
4390c0d06caSMauro Carvalho Chehab 	msleep(30);
4400c0d06caSMauro Carvalho Chehab 
4410c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
4420c0d06caSMauro Carvalho Chehab 	msleep(10);
4430c0d06caSMauro Carvalho Chehab 
4440c0d06caSMauro Carvalho Chehab }
4450c0d06caSMauro Carvalho Chehab 
4460c0d06caSMauro Carvalho Chehab static void terratec_h5_init(struct em28xx *dev)
4470c0d06caSMauro Carvalho Chehab {
4480c0d06caSMauro Carvalho Chehab 	int i;
4490c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_h5_init[] = {
4500c0d06caSMauro Carvalho Chehab 		{EM28XX_R08_GPIO,	0xff,	0xff,	10},
4510c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xf6,	0xff,	100},
4520c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xf2,	0xff,	50},
4530c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xf6,	0xff,	100},
4540c0d06caSMauro Carvalho Chehab 		{ -1,                   -1,     -1,     -1},
4550c0d06caSMauro Carvalho Chehab 	};
4560c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_h5_end[] = {
4570c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xe6,	0xff,	100},
4580c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xa6,	0xff,	50},
4590c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xe6,	0xff,	100},
4600c0d06caSMauro Carvalho Chehab 		{ -1,                   -1,     -1,     -1},
4610c0d06caSMauro Carvalho Chehab 	};
4620c0d06caSMauro Carvalho Chehab 	struct {
4630c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
4640c0d06caSMauro Carvalho Chehab 		int len;
4650c0d06caSMauro Carvalho Chehab 	} regs[] = {
4660c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
4670c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
4680c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
4690c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
4700c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
4710c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
4720c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
4730c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
4740c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x00 }, 2},
4750c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04 }, 2},
4760c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
4770c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14 }, 2},
4780c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
4790c0d06caSMauro Carvalho Chehab 	};
4800c0d06caSMauro Carvalho Chehab 
4810c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_h5_init);
4820c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
4830c0d06caSMauro Carvalho Chehab 	msleep(10);
4840c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
4850c0d06caSMauro Carvalho Chehab 	msleep(10);
4860c0d06caSMauro Carvalho Chehab 
4870c0d06caSMauro Carvalho Chehab 	dev->i2c_client.addr = 0x82 >> 1;
4880c0d06caSMauro Carvalho Chehab 
4890c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
4900c0d06caSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
4910c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_h5_end);
4920c0d06caSMauro Carvalho Chehab };
4930c0d06caSMauro Carvalho Chehab 
4940c0d06caSMauro Carvalho Chehab static void terratec_htc_stick_init(struct em28xx *dev)
4950c0d06caSMauro Carvalho Chehab {
4960c0d06caSMauro Carvalho Chehab 	int i;
4970c0d06caSMauro Carvalho Chehab 
4980c0d06caSMauro Carvalho Chehab 	/*
4990c0d06caSMauro Carvalho Chehab 	 * GPIO configuration:
5000c0d06caSMauro Carvalho Chehab 	 * 0xff: unknown (does not affect DVB-T).
5010c0d06caSMauro Carvalho Chehab 	 * 0xf6: DRX-K (demodulator).
5020c0d06caSMauro Carvalho Chehab 	 * 0xe6: unknown (does not affect DVB-T).
5030c0d06caSMauro Carvalho Chehab 	 * 0xb6: unknown (does not affect DVB-T).
5040c0d06caSMauro Carvalho Chehab 	 */
5050c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_htc_stick_init[] = {
5060c0d06caSMauro Carvalho Chehab 		{EM28XX_R08_GPIO,	0xff,	0xff,	10},
5070c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xf6,	0xff,	100},
5080c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xe6,	0xff,	50},
5090c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xf6,	0xff,	100},
5100c0d06caSMauro Carvalho Chehab 		{ -1,                   -1,     -1,     -1},
5110c0d06caSMauro Carvalho Chehab 	};
5120c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_htc_stick_end[] = {
5130c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xb6,	0xff,	100},
5140c0d06caSMauro Carvalho Chehab 		{EM2874_R80_GPIO,	0xf6,	0xff,	50},
5150c0d06caSMauro Carvalho Chehab 		{ -1,                   -1,     -1,     -1},
5160c0d06caSMauro Carvalho Chehab 	};
5170c0d06caSMauro Carvalho Chehab 
5180c0d06caSMauro Carvalho Chehab 	/* Init the analog decoder? */
5190c0d06caSMauro Carvalho Chehab 	struct {
5200c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
5210c0d06caSMauro Carvalho Chehab 		int len;
5220c0d06caSMauro Carvalho Chehab 	} regs[] = {
5230c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
5240c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
5250c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
5260c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
5270c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
5280c0d06caSMauro Carvalho Chehab 	};
5290c0d06caSMauro Carvalho Chehab 
5300c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_htc_stick_init);
5310c0d06caSMauro Carvalho Chehab 
5320c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
5330c0d06caSMauro Carvalho Chehab 	msleep(10);
5340c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
5350c0d06caSMauro Carvalho Chehab 	msleep(10);
5360c0d06caSMauro Carvalho Chehab 
5370c0d06caSMauro Carvalho Chehab 	dev->i2c_client.addr = 0x82 >> 1;
5380c0d06caSMauro Carvalho Chehab 
5390c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
5400c0d06caSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
5410c0d06caSMauro Carvalho Chehab 
5420c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_htc_stick_end);
5430c0d06caSMauro Carvalho Chehab };
5440c0d06caSMauro Carvalho Chehab 
5450c0d06caSMauro Carvalho Chehab static void pctv_520e_init(struct em28xx *dev)
5460c0d06caSMauro Carvalho Chehab {
5470c0d06caSMauro Carvalho Chehab 	/*
5480c0d06caSMauro Carvalho Chehab 	 * Init AVF4910B analog decoder. Looks like I2C traffic to
5490c0d06caSMauro Carvalho Chehab 	 * digital demodulator and tuner are routed via AVF4910B.
5500c0d06caSMauro Carvalho Chehab 	 */
5510c0d06caSMauro Carvalho Chehab 	int i;
5520c0d06caSMauro Carvalho Chehab 	struct {
5530c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
5540c0d06caSMauro Carvalho Chehab 		int len;
5550c0d06caSMauro Carvalho Chehab 	} regs[] = {
5560c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
5570c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
5580c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
5590c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
5600c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
5610c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
5620c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
5630c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
5640c0d06caSMauro Carvalho Chehab 	};
5650c0d06caSMauro Carvalho Chehab 
5660c0d06caSMauro Carvalho Chehab 	dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
5670c0d06caSMauro Carvalho Chehab 
5680c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
5690c0d06caSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
5700c0d06caSMauro Carvalho Chehab };
5710c0d06caSMauro Carvalho Chehab 
57213a5336eSAntti Palosaari static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe, int val)
57313a5336eSAntti Palosaari {
57413a5336eSAntti Palosaari 	struct em28xx *dev = fe->dvb->priv;
57513a5336eSAntti Palosaari #ifdef CONFIG_GPIOLIB
57613a5336eSAntti Palosaari 	struct em28xx_dvb *dvb = dev->dvb;
57713a5336eSAntti Palosaari 	int ret;
57813a5336eSAntti Palosaari 	unsigned long flags;
57913a5336eSAntti Palosaari 
58013a5336eSAntti Palosaari 	if (val)
58113a5336eSAntti Palosaari 		flags = GPIOF_OUT_INIT_LOW;
58213a5336eSAntti Palosaari 	else
58313a5336eSAntti Palosaari 		flags = GPIOF_OUT_INIT_HIGH;
58413a5336eSAntti Palosaari 
58513a5336eSAntti Palosaari 	ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
58613a5336eSAntti Palosaari 	if (ret)
58713a5336eSAntti Palosaari 		em28xx_errdev("gpio request failed %d\n", ret);
58813a5336eSAntti Palosaari 	else
58913a5336eSAntti Palosaari 		gpio_free(dvb->lna_gpio);
59013a5336eSAntti Palosaari 
59113a5336eSAntti Palosaari 	return ret;
59213a5336eSAntti Palosaari #else
59313a5336eSAntti Palosaari 	dev_warn(&dev->udev->dev, "%s: LNA control is disabled\n",
59413a5336eSAntti Palosaari 			KBUILD_MODNAME);
59513a5336eSAntti Palosaari 	return 0;
59613a5336eSAntti Palosaari #endif
59713a5336eSAntti Palosaari }
59813a5336eSAntti Palosaari 
5990c0d06caSMauro Carvalho Chehab static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
6000c0d06caSMauro Carvalho Chehab {
6010c0d06caSMauro Carvalho Chehab 	/* Values extracted from a USB trace of the Terratec Windows driver */
6020c0d06caSMauro Carvalho Chehab 	static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
6030c0d06caSMauro Carvalho Chehab 	static u8 reset[]          = { RESET,      0x80 };
6040c0d06caSMauro Carvalho Chehab 	static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
6050c0d06caSMauro Carvalho Chehab 	static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
6060c0d06caSMauro Carvalho Chehab 	static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
6070c0d06caSMauro Carvalho Chehab 	static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
6080c0d06caSMauro Carvalho Chehab 	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
6090c0d06caSMauro Carvalho Chehab 	static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
6100c0d06caSMauro Carvalho Chehab 	static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
6110c0d06caSMauro Carvalho Chehab 	static u8 tuner_go[]       = { TUNER_GO, 0x01};
6120c0d06caSMauro Carvalho Chehab 
6130c0d06caSMauro Carvalho Chehab 	mt352_write(fe, clock_config,   sizeof(clock_config));
6140c0d06caSMauro Carvalho Chehab 	udelay(200);
6150c0d06caSMauro Carvalho Chehab 	mt352_write(fe, reset,          sizeof(reset));
6160c0d06caSMauro Carvalho Chehab 	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
6170c0d06caSMauro Carvalho Chehab 	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
6180c0d06caSMauro Carvalho Chehab 	mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
6190c0d06caSMauro Carvalho Chehab 	mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
6200c0d06caSMauro Carvalho Chehab 	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
6210c0d06caSMauro Carvalho Chehab 	mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
6220c0d06caSMauro Carvalho Chehab 	mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
6230c0d06caSMauro Carvalho Chehab 	mt352_write(fe, tuner_go,       sizeof(tuner_go));
6240c0d06caSMauro Carvalho Chehab 	return 0;
6250c0d06caSMauro Carvalho Chehab }
6260c0d06caSMauro Carvalho Chehab 
6270c0d06caSMauro Carvalho Chehab static struct mt352_config terratec_xs_mt352_cfg = {
6280c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
6290c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
6300c0d06caSMauro Carvalho Chehab 	.if2 = 45600,
6310c0d06caSMauro Carvalho Chehab 	.demod_init = em28xx_mt352_terratec_xs_init,
6320c0d06caSMauro Carvalho Chehab };
6330c0d06caSMauro Carvalho Chehab 
6340c0d06caSMauro Carvalho Chehab static struct tda10023_config em28xx_tda10023_config = {
6350c0d06caSMauro Carvalho Chehab 	.demod_address = 0x0c,
6360c0d06caSMauro Carvalho Chehab 	.invert = 1,
6370c0d06caSMauro Carvalho Chehab };
6380c0d06caSMauro Carvalho Chehab 
6390c0d06caSMauro Carvalho Chehab static struct cxd2820r_config em28xx_cxd2820r_config = {
6400c0d06caSMauro Carvalho Chehab 	.i2c_address = (0xd8 >> 1),
6410c0d06caSMauro Carvalho Chehab 	.ts_mode = CXD2820R_TS_SERIAL,
6420c0d06caSMauro Carvalho Chehab };
6430c0d06caSMauro Carvalho Chehab 
6440c0d06caSMauro Carvalho Chehab static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
6450c0d06caSMauro Carvalho Chehab 	.output_opt = TDA18271_OUTPUT_LT_OFF,
6460c0d06caSMauro Carvalho Chehab 	.gate = TDA18271_GATE_DIGITAL,
6470c0d06caSMauro Carvalho Chehab };
6480c0d06caSMauro Carvalho Chehab 
6490c0d06caSMauro Carvalho Chehab static const struct tda10071_config em28xx_tda10071_config = {
6500c0d06caSMauro Carvalho Chehab 	.i2c_address = 0x55, /* (0xaa >> 1) */
6510c0d06caSMauro Carvalho Chehab 	.i2c_wr_max = 64,
6520c0d06caSMauro Carvalho Chehab 	.ts_mode = TDA10071_TS_SERIAL,
6530c0d06caSMauro Carvalho Chehab 	.spec_inv = 0,
6540c0d06caSMauro Carvalho Chehab 	.xtal = 40444000, /* 40.444 MHz */
6550c0d06caSMauro Carvalho Chehab 	.pll_multiplier = 20,
6560c0d06caSMauro Carvalho Chehab };
6570c0d06caSMauro Carvalho Chehab 
6580c0d06caSMauro Carvalho Chehab static const struct a8293_config em28xx_a8293_config = {
6590c0d06caSMauro Carvalho Chehab 	.i2c_addr = 0x08, /* (0x10 >> 1) */
6600c0d06caSMauro Carvalho Chehab };
6610c0d06caSMauro Carvalho Chehab 
6620c0d06caSMauro Carvalho Chehab static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
6630c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
6640c0d06caSMauro Carvalho Chehab 	.disable_i2c_gate_ctrl = 1,
6650c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
6660c0d06caSMauro Carvalho Chehab 	.parallel_ts = 1,
6670c0d06caSMauro Carvalho Chehab };
6680c0d06caSMauro Carvalho Chehab static struct qt1010_config em28xx_qt1010_config = {
6690c0d06caSMauro Carvalho Chehab 	.i2c_address = 0x62
6700c0d06caSMauro Carvalho Chehab 
6710c0d06caSMauro Carvalho Chehab };
6720c0d06caSMauro Carvalho Chehab 
6730c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
6740c0d06caSMauro Carvalho Chehab 
6750c0d06caSMauro Carvalho Chehab static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
6760c0d06caSMauro Carvalho Chehab {
6770c0d06caSMauro Carvalho Chehab 	struct dvb_frontend *fe;
6780c0d06caSMauro Carvalho Chehab 	struct xc2028_config cfg;
6790c0d06caSMauro Carvalho Chehab 
6800c0d06caSMauro Carvalho Chehab 	memset(&cfg, 0, sizeof(cfg));
6810c0d06caSMauro Carvalho Chehab 	cfg.i2c_adap  = &dev->i2c_adap;
6820c0d06caSMauro Carvalho Chehab 	cfg.i2c_addr  = addr;
6830c0d06caSMauro Carvalho Chehab 
6840c0d06caSMauro Carvalho Chehab 	if (!dev->dvb->fe[0]) {
6850c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: dvb frontend not attached. "
6860c0d06caSMauro Carvalho Chehab 				"Can't attach xc3028\n");
6870c0d06caSMauro Carvalho Chehab 		return -EINVAL;
6880c0d06caSMauro Carvalho Chehab 	}
6890c0d06caSMauro Carvalho Chehab 
6900c0d06caSMauro Carvalho Chehab 	fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
6910c0d06caSMauro Carvalho Chehab 	if (!fe) {
6920c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: xc3028 attach failed\n");
6930c0d06caSMauro Carvalho Chehab 		dvb_frontend_detach(dev->dvb->fe[0]);
6940c0d06caSMauro Carvalho Chehab 		dev->dvb->fe[0] = NULL;
6950c0d06caSMauro Carvalho Chehab 		return -EINVAL;
6960c0d06caSMauro Carvalho Chehab 	}
6970c0d06caSMauro Carvalho Chehab 
6980c0d06caSMauro Carvalho Chehab 	em28xx_info("%s/2: xc3028 attached\n", dev->name);
6990c0d06caSMauro Carvalho Chehab 
7000c0d06caSMauro Carvalho Chehab 	return 0;
7010c0d06caSMauro Carvalho Chehab }
7020c0d06caSMauro Carvalho Chehab 
7030c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
7040c0d06caSMauro Carvalho Chehab 
7050c0d06caSMauro Carvalho Chehab static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
7060c0d06caSMauro Carvalho Chehab 			       struct em28xx *dev, struct device *device)
7070c0d06caSMauro Carvalho Chehab {
7080c0d06caSMauro Carvalho Chehab 	int result;
7090c0d06caSMauro Carvalho Chehab 
7100c0d06caSMauro Carvalho Chehab 	mutex_init(&dvb->lock);
7110c0d06caSMauro Carvalho Chehab 
7120c0d06caSMauro Carvalho Chehab 	/* register adapter */
7130c0d06caSMauro Carvalho Chehab 	result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
7140c0d06caSMauro Carvalho Chehab 				      adapter_nr);
7150c0d06caSMauro Carvalho Chehab 	if (result < 0) {
7160c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
7170c0d06caSMauro Carvalho Chehab 		       dev->name, result);
7180c0d06caSMauro Carvalho Chehab 		goto fail_adapter;
7190c0d06caSMauro Carvalho Chehab 	}
7200c0d06caSMauro Carvalho Chehab 
7210c0d06caSMauro Carvalho Chehab 	/* Ensure all frontends negotiate bus access */
7220c0d06caSMauro Carvalho Chehab 	dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
7230c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
7240c0d06caSMauro Carvalho Chehab 		dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
7250c0d06caSMauro Carvalho Chehab 
7260c0d06caSMauro Carvalho Chehab 	dvb->adapter.priv = dev;
7270c0d06caSMauro Carvalho Chehab 
7280c0d06caSMauro Carvalho Chehab 	/* register frontend */
7290c0d06caSMauro Carvalho Chehab 	result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
7300c0d06caSMauro Carvalho Chehab 	if (result < 0) {
7310c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
7320c0d06caSMauro Carvalho Chehab 		       dev->name, result);
7330c0d06caSMauro Carvalho Chehab 		goto fail_frontend0;
7340c0d06caSMauro Carvalho Chehab 	}
7350c0d06caSMauro Carvalho Chehab 
7360c0d06caSMauro Carvalho Chehab 	/* register 2nd frontend */
7370c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1]) {
7380c0d06caSMauro Carvalho Chehab 		result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
7390c0d06caSMauro Carvalho Chehab 		if (result < 0) {
7400c0d06caSMauro Carvalho Chehab 			printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
7410c0d06caSMauro Carvalho Chehab 				dev->name, result);
7420c0d06caSMauro Carvalho Chehab 			goto fail_frontend1;
7430c0d06caSMauro Carvalho Chehab 		}
7440c0d06caSMauro Carvalho Chehab 	}
7450c0d06caSMauro Carvalho Chehab 
7460c0d06caSMauro Carvalho Chehab 	/* register demux stuff */
7470c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.capabilities =
7480c0d06caSMauro Carvalho Chehab 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
7490c0d06caSMauro Carvalho Chehab 		DMX_MEMORY_BASED_FILTERING;
7500c0d06caSMauro Carvalho Chehab 	dvb->demux.priv       = dvb;
7510c0d06caSMauro Carvalho Chehab 	dvb->demux.filternum  = 256;
7520c0d06caSMauro Carvalho Chehab 	dvb->demux.feednum    = 256;
7530c0d06caSMauro Carvalho Chehab 	dvb->demux.start_feed = em28xx_start_feed;
7540c0d06caSMauro Carvalho Chehab 	dvb->demux.stop_feed  = em28xx_stop_feed;
7550c0d06caSMauro Carvalho Chehab 
7560c0d06caSMauro Carvalho Chehab 	result = dvb_dmx_init(&dvb->demux);
7570c0d06caSMauro Carvalho Chehab 	if (result < 0) {
7580c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
7590c0d06caSMauro Carvalho Chehab 		       dev->name, result);
7600c0d06caSMauro Carvalho Chehab 		goto fail_dmx;
7610c0d06caSMauro Carvalho Chehab 	}
7620c0d06caSMauro Carvalho Chehab 
7630c0d06caSMauro Carvalho Chehab 	dvb->dmxdev.filternum    = 256;
7640c0d06caSMauro Carvalho Chehab 	dvb->dmxdev.demux        = &dvb->demux.dmx;
7650c0d06caSMauro Carvalho Chehab 	dvb->dmxdev.capabilities = 0;
7660c0d06caSMauro Carvalho Chehab 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
7670c0d06caSMauro Carvalho Chehab 	if (result < 0) {
7680c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
7690c0d06caSMauro Carvalho Chehab 		       dev->name, result);
7700c0d06caSMauro Carvalho Chehab 		goto fail_dmxdev;
7710c0d06caSMauro Carvalho Chehab 	}
7720c0d06caSMauro Carvalho Chehab 
7730c0d06caSMauro Carvalho Chehab 	dvb->fe_hw.source = DMX_FRONTEND_0;
7740c0d06caSMauro Carvalho Chehab 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
7750c0d06caSMauro Carvalho Chehab 	if (result < 0) {
7760c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
7770c0d06caSMauro Carvalho Chehab 		       dev->name, result);
7780c0d06caSMauro Carvalho Chehab 		goto fail_fe_hw;
7790c0d06caSMauro Carvalho Chehab 	}
7800c0d06caSMauro Carvalho Chehab 
7810c0d06caSMauro Carvalho Chehab 	dvb->fe_mem.source = DMX_MEMORY_FE;
7820c0d06caSMauro Carvalho Chehab 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
7830c0d06caSMauro Carvalho Chehab 	if (result < 0) {
7840c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
7850c0d06caSMauro Carvalho Chehab 		       dev->name, result);
7860c0d06caSMauro Carvalho Chehab 		goto fail_fe_mem;
7870c0d06caSMauro Carvalho Chehab 	}
7880c0d06caSMauro Carvalho Chehab 
7890c0d06caSMauro Carvalho Chehab 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
7900c0d06caSMauro Carvalho Chehab 	if (result < 0) {
7910c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
7920c0d06caSMauro Carvalho Chehab 		       dev->name, result);
7930c0d06caSMauro Carvalho Chehab 		goto fail_fe_conn;
7940c0d06caSMauro Carvalho Chehab 	}
7950c0d06caSMauro Carvalho Chehab 
7960c0d06caSMauro Carvalho Chehab 	/* register network adapter */
7970c0d06caSMauro Carvalho Chehab 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
7980c0d06caSMauro Carvalho Chehab 	return 0;
7990c0d06caSMauro Carvalho Chehab 
8000c0d06caSMauro Carvalho Chehab fail_fe_conn:
8010c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
8020c0d06caSMauro Carvalho Chehab fail_fe_mem:
8030c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
8040c0d06caSMauro Carvalho Chehab fail_fe_hw:
8050c0d06caSMauro Carvalho Chehab 	dvb_dmxdev_release(&dvb->dmxdev);
8060c0d06caSMauro Carvalho Chehab fail_dmxdev:
8070c0d06caSMauro Carvalho Chehab 	dvb_dmx_release(&dvb->demux);
8080c0d06caSMauro Carvalho Chehab fail_dmx:
8090c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
8100c0d06caSMauro Carvalho Chehab 		dvb_unregister_frontend(dvb->fe[1]);
8110c0d06caSMauro Carvalho Chehab 	dvb_unregister_frontend(dvb->fe[0]);
8120c0d06caSMauro Carvalho Chehab fail_frontend1:
8130c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
8140c0d06caSMauro Carvalho Chehab 		dvb_frontend_detach(dvb->fe[1]);
8150c0d06caSMauro Carvalho Chehab fail_frontend0:
8160c0d06caSMauro Carvalho Chehab 	dvb_frontend_detach(dvb->fe[0]);
8170c0d06caSMauro Carvalho Chehab 	dvb_unregister_adapter(&dvb->adapter);
8180c0d06caSMauro Carvalho Chehab fail_adapter:
8190c0d06caSMauro Carvalho Chehab 	return result;
8200c0d06caSMauro Carvalho Chehab }
8210c0d06caSMauro Carvalho Chehab 
8220c0d06caSMauro Carvalho Chehab static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
8230c0d06caSMauro Carvalho Chehab {
8240c0d06caSMauro Carvalho Chehab 	dvb_net_release(&dvb->net);
8250c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
8260c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
8270c0d06caSMauro Carvalho Chehab 	dvb_dmxdev_release(&dvb->dmxdev);
8280c0d06caSMauro Carvalho Chehab 	dvb_dmx_release(&dvb->demux);
8290c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
8300c0d06caSMauro Carvalho Chehab 		dvb_unregister_frontend(dvb->fe[1]);
8310c0d06caSMauro Carvalho Chehab 	dvb_unregister_frontend(dvb->fe[0]);
8320c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1] && !dvb->dont_attach_fe1)
8330c0d06caSMauro Carvalho Chehab 		dvb_frontend_detach(dvb->fe[1]);
8340c0d06caSMauro Carvalho Chehab 	dvb_frontend_detach(dvb->fe[0]);
8350c0d06caSMauro Carvalho Chehab 	dvb_unregister_adapter(&dvb->adapter);
8360c0d06caSMauro Carvalho Chehab }
8370c0d06caSMauro Carvalho Chehab 
8380c0d06caSMauro Carvalho Chehab static int em28xx_dvb_init(struct em28xx *dev)
8390c0d06caSMauro Carvalho Chehab {
84013a5336eSAntti Palosaari 	int result = 0, mfe_shared = 0;
8410c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb;
8420c0d06caSMauro Carvalho Chehab 
8430c0d06caSMauro Carvalho Chehab 	if (!dev->board.has_dvb) {
8440c0d06caSMauro Carvalho Chehab 		/* This device does not support the extension */
8450c0d06caSMauro Carvalho Chehab 		printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
8460c0d06caSMauro Carvalho Chehab 		return 0;
8470c0d06caSMauro Carvalho Chehab 	}
8480c0d06caSMauro Carvalho Chehab 
8490c0d06caSMauro Carvalho Chehab 	dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
8500c0d06caSMauro Carvalho Chehab 
8510c0d06caSMauro Carvalho Chehab 	if (dvb == NULL) {
8520c0d06caSMauro Carvalho Chehab 		em28xx_info("em28xx_dvb: memory allocation failed\n");
8530c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
8540c0d06caSMauro Carvalho Chehab 	}
8550c0d06caSMauro Carvalho Chehab 	dev->dvb = dvb;
8560c0d06caSMauro Carvalho Chehab 	dvb->fe[0] = dvb->fe[1] = NULL;
8570c0d06caSMauro Carvalho Chehab 
8580c0d06caSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
8590c0d06caSMauro Carvalho Chehab 	em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
8600c0d06caSMauro Carvalho Chehab 	/* init frontend */
8610c0d06caSMauro Carvalho Chehab 	switch (dev->model) {
8620c0d06caSMauro Carvalho Chehab 	case EM2874_BOARD_LEADERSHIP_ISDBT:
8630c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(s921_attach,
8640c0d06caSMauro Carvalho Chehab 				&sharp_isdbt, &dev->i2c_adap);
8650c0d06caSMauro Carvalho Chehab 
8660c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
8670c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
8680c0d06caSMauro Carvalho Chehab 			goto out_free;
8690c0d06caSMauro Carvalho Chehab 		}
8700c0d06caSMauro Carvalho Chehab 
8710c0d06caSMauro Carvalho Chehab 		break;
8720c0d06caSMauro Carvalho Chehab 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
8730c0d06caSMauro Carvalho Chehab 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
8740c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
8750c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
8760c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
8770c0d06caSMauro Carvalho Chehab 					   &em2880_lgdt3303_dev,
8780c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
8790c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
8800c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
8810c0d06caSMauro Carvalho Chehab 			goto out_free;
8820c0d06caSMauro Carvalho Chehab 		}
8830c0d06caSMauro Carvalho Chehab 		break;
8840c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_KWORLD_DVB_310U:
8850c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
8860c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_with_xc3028,
8870c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
8880c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
8890c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
8900c0d06caSMauro Carvalho Chehab 			goto out_free;
8910c0d06caSMauro Carvalho Chehab 		}
8920c0d06caSMauro Carvalho Chehab 		break;
8930c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
8940c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_TERRATEC_HYBRID_XS:
8950c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_EMPIRE_DUAL_TV:
8960c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
8970c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_xc3028_no_i2c_gate,
8980c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
8990c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
9000c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
9010c0d06caSMauro Carvalho Chehab 			goto out_free;
9020c0d06caSMauro Carvalho Chehab 		}
9030c0d06caSMauro Carvalho Chehab 		break;
9040c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_TERRATEC_HYBRID_XS:
9050c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
9060c0d06caSMauro Carvalho Chehab 	case EM2881_BOARD_PINNACLE_HYBRID_PRO:
9070c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_DIKOM_DK300:
9080c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_KWORLD_VS_DVBT:
9090c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
9100c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_xc3028_no_i2c_gate,
9110c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
9120c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] == NULL) {
9130c0d06caSMauro Carvalho Chehab 			/* This board could have either a zl10353 or a mt352.
9140c0d06caSMauro Carvalho Chehab 			   If the chip id isn't for zl10353, try mt352 */
9150c0d06caSMauro Carvalho Chehab 			dvb->fe[0] = dvb_attach(mt352_attach,
9160c0d06caSMauro Carvalho Chehab 						   &terratec_xs_mt352_cfg,
9170c0d06caSMauro Carvalho Chehab 						   &dev->i2c_adap);
9180c0d06caSMauro Carvalho Chehab 		}
9190c0d06caSMauro Carvalho Chehab 
9200c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
9210c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
9220c0d06caSMauro Carvalho Chehab 			goto out_free;
9230c0d06caSMauro Carvalho Chehab 		}
9240c0d06caSMauro Carvalho Chehab 		break;
9250c0d06caSMauro Carvalho Chehab 	case EM2870_BOARD_KWORLD_355U:
9260c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
9270c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_no_i2c_gate_dev,
9280c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
9290c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] != NULL)
9300c0d06caSMauro Carvalho Chehab 			dvb_attach(qt1010_attach, dvb->fe[0],
9310c0d06caSMauro Carvalho Chehab 				   &dev->i2c_adap, &em28xx_qt1010_config);
9320c0d06caSMauro Carvalho Chehab 		break;
9330c0d06caSMauro Carvalho Chehab 	case EM2883_BOARD_KWORLD_HYBRID_330U:
9340c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_EVGA_INDTUBE:
9350c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(s5h1409_attach,
9360c0d06caSMauro Carvalho Chehab 					   &em28xx_s5h1409_with_xc3028,
9370c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
9380c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
9390c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
9400c0d06caSMauro Carvalho Chehab 			goto out_free;
9410c0d06caSMauro Carvalho Chehab 		}
9420c0d06caSMauro Carvalho Chehab 		break;
9430c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_KWORLD_ATSC_315U:
9440c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
9450c0d06caSMauro Carvalho Chehab 					   &em2880_lgdt3303_dev,
9460c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
9470c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] != NULL) {
9480c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
9490c0d06caSMauro Carvalho Chehab 				&dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
9500c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
9510c0d06caSMauro Carvalho Chehab 				goto out_free;
9520c0d06caSMauro Carvalho Chehab 			}
9530c0d06caSMauro Carvalho Chehab 		}
9540c0d06caSMauro Carvalho Chehab 		break;
9550c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
9560c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
9570c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
9580c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap, &dev->udev->dev);
9590c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
9600c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
9610c0d06caSMauro Carvalho Chehab 			goto out_free;
9620c0d06caSMauro Carvalho Chehab 		}
9630c0d06caSMauro Carvalho Chehab 		break;
9640c0d06caSMauro Carvalho Chehab 	case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
9650c0d06caSMauro Carvalho Chehab 		/* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
9660c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(tda10023_attach,
9670c0d06caSMauro Carvalho Chehab 			&em28xx_tda10023_config,
9680c0d06caSMauro Carvalho Chehab 			&dev->i2c_adap, 0x48);
9690c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
9700c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
9710c0d06caSMauro Carvalho Chehab 				&dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
9720c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
9730c0d06caSMauro Carvalho Chehab 				goto out_free;
9740c0d06caSMauro Carvalho Chehab 			}
9750c0d06caSMauro Carvalho Chehab 		}
9760c0d06caSMauro Carvalho Chehab 		break;
9770c0d06caSMauro Carvalho Chehab 	case EM2870_BOARD_KWORLD_A340:
9780c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
9790c0d06caSMauro Carvalho Chehab 					   &em2870_lgdt3304_dev,
9800c0d06caSMauro Carvalho Chehab 					   &dev->i2c_adap);
9810c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] != NULL)
9820c0d06caSMauro Carvalho Chehab 			dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
9830c0d06caSMauro Carvalho Chehab 				   &dev->i2c_adap, &kworld_a340_config);
9840c0d06caSMauro Carvalho Chehab 		break;
9850c0d06caSMauro Carvalho Chehab 	case EM28174_BOARD_PCTV_290E:
986*a36a66d7SAntti Palosaari 		/* set default GPIO0 for LNA, used if GPIOLIB is undefined */
987*a36a66d7SAntti Palosaari 		dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
988*a36a66d7SAntti Palosaari 				CXD2820R_GPIO_L;
9890c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(cxd2820r_attach,
9900c0d06caSMauro Carvalho Chehab 					&em28xx_cxd2820r_config,
9911e8f31f3SAntti Palosaari 					&dev->i2c_adap,
99213a5336eSAntti Palosaari 					&dvb->lna_gpio);
9930c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
9940c0d06caSMauro Carvalho Chehab 			/* FE 0 attach tuner */
9950c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(tda18271_attach,
9960c0d06caSMauro Carvalho Chehab 					dvb->fe[0],
9970c0d06caSMauro Carvalho Chehab 					0x60,
9980c0d06caSMauro Carvalho Chehab 					&dev->i2c_adap,
9990c0d06caSMauro Carvalho Chehab 					&em28xx_cxd2820r_tda18271_config)) {
10000c0d06caSMauro Carvalho Chehab 
10010c0d06caSMauro Carvalho Chehab 				dvb_frontend_detach(dvb->fe[0]);
10020c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
10030c0d06caSMauro Carvalho Chehab 				goto out_free;
10040c0d06caSMauro Carvalho Chehab 			}
10050c0d06caSMauro Carvalho Chehab 		}
10061e8f31f3SAntti Palosaari 
100713a5336eSAntti Palosaari #ifdef CONFIG_GPIOLIB
10081e8f31f3SAntti Palosaari 		/* enable LNA for DVB-T, DVB-T2 and DVB-C */
100913a5336eSAntti Palosaari 		result = gpio_request_one(dvb->lna_gpio, GPIOF_OUT_INIT_LOW,
101013a5336eSAntti Palosaari 				NULL);
10111e8f31f3SAntti Palosaari 		if (result)
10121e8f31f3SAntti Palosaari 			em28xx_errdev("gpio request failed %d\n", result);
10131e8f31f3SAntti Palosaari 		else
101413a5336eSAntti Palosaari 			gpio_free(dvb->lna_gpio);
10151e8f31f3SAntti Palosaari 
10161e8f31f3SAntti Palosaari 		result = 0; /* continue even set LNA fails */
101713a5336eSAntti Palosaari #endif
101813a5336eSAntti Palosaari 		dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
10190c0d06caSMauro Carvalho Chehab 		break;
10200c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
10210c0d06caSMauro Carvalho Chehab 	{
10220c0d06caSMauro Carvalho Chehab 		struct xc5000_config cfg;
10230c0d06caSMauro Carvalho Chehab 		hauppauge_hvr930c_init(dev);
10240c0d06caSMauro Carvalho Chehab 
10250c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach,
10260c0d06caSMauro Carvalho Chehab 					&hauppauge_930c_drxk, &dev->i2c_adap);
10270c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
10280c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
10290c0d06caSMauro Carvalho Chehab 			goto out_free;
10300c0d06caSMauro Carvalho Chehab 		}
10310c0d06caSMauro Carvalho Chehab 		/* FIXME: do we need a pll semaphore? */
10320c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->sec_priv = dvb;
10330c0d06caSMauro Carvalho Chehab 		sema_init(&dvb->pll_mutex, 1);
10340c0d06caSMauro Carvalho Chehab 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
10350c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
10360c0d06caSMauro Carvalho Chehab 
10370c0d06caSMauro Carvalho Chehab 		/* Attach xc5000 */
10380c0d06caSMauro Carvalho Chehab 		memset(&cfg, 0, sizeof(cfg));
10390c0d06caSMauro Carvalho Chehab 		cfg.i2c_address  = 0x61;
10400c0d06caSMauro Carvalho Chehab 		cfg.if_khz = 4000;
10410c0d06caSMauro Carvalho Chehab 
10420c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
10430c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
10440c0d06caSMauro Carvalho Chehab 		if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
10450c0d06caSMauro Carvalho Chehab 				&cfg)) {
10460c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
10470c0d06caSMauro Carvalho Chehab 			goto out_free;
10480c0d06caSMauro Carvalho Chehab 		}
10490c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
10500c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
10510c0d06caSMauro Carvalho Chehab 
10520c0d06caSMauro Carvalho Chehab 		break;
10530c0d06caSMauro Carvalho Chehab 	}
10540c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_TERRATEC_H5:
10550c0d06caSMauro Carvalho Chehab 		terratec_h5_init(dev);
10560c0d06caSMauro Carvalho Chehab 
10570c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
10580c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
10590c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
10600c0d06caSMauro Carvalho Chehab 			goto out_free;
10610c0d06caSMauro Carvalho Chehab 		}
10620c0d06caSMauro Carvalho Chehab 		/* FIXME: do we need a pll semaphore? */
10630c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->sec_priv = dvb;
10640c0d06caSMauro Carvalho Chehab 		sema_init(&dvb->pll_mutex, 1);
10650c0d06caSMauro Carvalho Chehab 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
10660c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
10670c0d06caSMauro Carvalho Chehab 
10680c0d06caSMauro Carvalho Chehab 		/* Attach tda18271 to DVB-C frontend */
10690c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
10700c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
10710c0d06caSMauro Carvalho Chehab 		if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
10720c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
10730c0d06caSMauro Carvalho Chehab 			goto out_free;
10740c0d06caSMauro Carvalho Chehab 		}
10750c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
10760c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
10770c0d06caSMauro Carvalho Chehab 
10780c0d06caSMauro Carvalho Chehab 		break;
10790c0d06caSMauro Carvalho Chehab 	case EM28174_BOARD_PCTV_460E:
10800c0d06caSMauro Carvalho Chehab 		/* attach demod */
10810c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(tda10071_attach,
10820c0d06caSMauro Carvalho Chehab 			&em28xx_tda10071_config, &dev->i2c_adap);
10830c0d06caSMauro Carvalho Chehab 
10840c0d06caSMauro Carvalho Chehab 		/* attach SEC */
10850c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0])
10860c0d06caSMauro Carvalho Chehab 			dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
10870c0d06caSMauro Carvalho Chehab 				&em28xx_a8293_config);
10880c0d06caSMauro Carvalho Chehab 		break;
10890c0d06caSMauro Carvalho Chehab 	case EM2874_BOARD_MAXMEDIA_UB425_TC:
10900c0d06caSMauro Carvalho Chehab 		/* attach demodulator */
10910c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
10920c0d06caSMauro Carvalho Chehab 				&dev->i2c_adap);
10930c0d06caSMauro Carvalho Chehab 
10940c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
10950c0d06caSMauro Carvalho Chehab 			/* disable I2C-gate */
10960c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
10970c0d06caSMauro Carvalho Chehab 
10980c0d06caSMauro Carvalho Chehab 			/* attach tuner */
10990c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
11000c0d06caSMauro Carvalho Chehab 					&dev->i2c_adap, 0x60)) {
11010c0d06caSMauro Carvalho Chehab 				dvb_frontend_detach(dvb->fe[0]);
11020c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
11030c0d06caSMauro Carvalho Chehab 				goto out_free;
11040c0d06caSMauro Carvalho Chehab 			}
11050c0d06caSMauro Carvalho Chehab 		}
11060c0d06caSMauro Carvalho Chehab 
11070c0d06caSMauro Carvalho Chehab 		/* TODO: we need drx-3913k firmware in order to support DVB-T */
11080c0d06caSMauro Carvalho Chehab 		em28xx_info("MaxMedia UB425-TC: only DVB-C supported by that " \
11090c0d06caSMauro Carvalho Chehab 				"driver version\n");
11100c0d06caSMauro Carvalho Chehab 
11110c0d06caSMauro Carvalho Chehab 		break;
11120c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_PCTV_510E:
11130c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_PCTV_520E:
11140c0d06caSMauro Carvalho Chehab 		pctv_520e_init(dev);
11150c0d06caSMauro Carvalho Chehab 
11160c0d06caSMauro Carvalho Chehab 		/* attach demodulator */
11170c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
11180c0d06caSMauro Carvalho Chehab 				&dev->i2c_adap);
11190c0d06caSMauro Carvalho Chehab 
11200c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
11210c0d06caSMauro Carvalho Chehab 			/* attach tuner */
11220c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
11230c0d06caSMauro Carvalho Chehab 					&dev->i2c_adap,
11240c0d06caSMauro Carvalho Chehab 					&em28xx_cxd2820r_tda18271_config)) {
11250c0d06caSMauro Carvalho Chehab 				dvb_frontend_detach(dvb->fe[0]);
11260c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
11270c0d06caSMauro Carvalho Chehab 				goto out_free;
11280c0d06caSMauro Carvalho Chehab 			}
11290c0d06caSMauro Carvalho Chehab 		}
11300c0d06caSMauro Carvalho Chehab 		break;
11310c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_CINERGY_HTC_STICK:
11320c0d06caSMauro Carvalho Chehab 		terratec_htc_stick_init(dev);
11330c0d06caSMauro Carvalho Chehab 
11340c0d06caSMauro Carvalho Chehab 		/* attach demodulator */
11350c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
11360c0d06caSMauro Carvalho Chehab 					&dev->i2c_adap);
11370c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
11380c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
11390c0d06caSMauro Carvalho Chehab 			goto out_free;
11400c0d06caSMauro Carvalho Chehab 		}
11410c0d06caSMauro Carvalho Chehab 
11420c0d06caSMauro Carvalho Chehab 		/* Attach the demodulator. */
11430c0d06caSMauro Carvalho Chehab 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
11440c0d06caSMauro Carvalho Chehab 				&dev->i2c_adap,
11450c0d06caSMauro Carvalho Chehab 				&em28xx_cxd2820r_tda18271_config)) {
11460c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
11470c0d06caSMauro Carvalho Chehab 			goto out_free;
11480c0d06caSMauro Carvalho Chehab 		}
11490c0d06caSMauro Carvalho Chehab 		break;
11500c0d06caSMauro Carvalho Chehab 	default:
11510c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: The frontend of your DVB/ATSC card"
11520c0d06caSMauro Carvalho Chehab 				" isn't supported yet\n");
11530c0d06caSMauro Carvalho Chehab 		break;
11540c0d06caSMauro Carvalho Chehab 	}
11550c0d06caSMauro Carvalho Chehab 	if (NULL == dvb->fe[0]) {
11560c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: frontend initialization failed\n");
11570c0d06caSMauro Carvalho Chehab 		result = -EINVAL;
11580c0d06caSMauro Carvalho Chehab 		goto out_free;
11590c0d06caSMauro Carvalho Chehab 	}
11600c0d06caSMauro Carvalho Chehab 	/* define general-purpose callback pointer */
11610c0d06caSMauro Carvalho Chehab 	dvb->fe[0]->callback = em28xx_tuner_callback;
11620c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
11630c0d06caSMauro Carvalho Chehab 		dvb->fe[1]->callback = em28xx_tuner_callback;
11640c0d06caSMauro Carvalho Chehab 
11650c0d06caSMauro Carvalho Chehab 	/* register everything */
11660c0d06caSMauro Carvalho Chehab 	result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
11670c0d06caSMauro Carvalho Chehab 
11680c0d06caSMauro Carvalho Chehab 	if (result < 0)
11690c0d06caSMauro Carvalho Chehab 		goto out_free;
11700c0d06caSMauro Carvalho Chehab 
11710c0d06caSMauro Carvalho Chehab 	/* MFE lock */
11720c0d06caSMauro Carvalho Chehab 	dvb->adapter.mfe_shared = mfe_shared;
11730c0d06caSMauro Carvalho Chehab 
11740c0d06caSMauro Carvalho Chehab 	em28xx_info("Successfully loaded em28xx-dvb\n");
11750c0d06caSMauro Carvalho Chehab ret:
11760c0d06caSMauro Carvalho Chehab 	em28xx_set_mode(dev, EM28XX_SUSPEND);
11770c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
11780c0d06caSMauro Carvalho Chehab 	return result;
11790c0d06caSMauro Carvalho Chehab 
11800c0d06caSMauro Carvalho Chehab out_free:
11810c0d06caSMauro Carvalho Chehab 	kfree(dvb);
11820c0d06caSMauro Carvalho Chehab 	dev->dvb = NULL;
11830c0d06caSMauro Carvalho Chehab 	goto ret;
11840c0d06caSMauro Carvalho Chehab }
11850c0d06caSMauro Carvalho Chehab 
11860c0d06caSMauro Carvalho Chehab static inline void prevent_sleep(struct dvb_frontend_ops *ops)
11870c0d06caSMauro Carvalho Chehab {
11880c0d06caSMauro Carvalho Chehab 	ops->set_voltage = NULL;
11890c0d06caSMauro Carvalho Chehab 	ops->sleep = NULL;
11900c0d06caSMauro Carvalho Chehab 	ops->tuner_ops.sleep = NULL;
11910c0d06caSMauro Carvalho Chehab }
11920c0d06caSMauro Carvalho Chehab 
11930c0d06caSMauro Carvalho Chehab static int em28xx_dvb_fini(struct em28xx *dev)
11940c0d06caSMauro Carvalho Chehab {
11950c0d06caSMauro Carvalho Chehab 	if (!dev->board.has_dvb) {
11960c0d06caSMauro Carvalho Chehab 		/* This device does not support the extension */
11970c0d06caSMauro Carvalho Chehab 		return 0;
11980c0d06caSMauro Carvalho Chehab 	}
11990c0d06caSMauro Carvalho Chehab 
12000c0d06caSMauro Carvalho Chehab 	if (dev->dvb) {
12010c0d06caSMauro Carvalho Chehab 		struct em28xx_dvb *dvb = dev->dvb;
12020c0d06caSMauro Carvalho Chehab 
12030c0d06caSMauro Carvalho Chehab 		if (dev->state & DEV_DISCONNECTED) {
12040c0d06caSMauro Carvalho Chehab 			/* We cannot tell the device to sleep
12050c0d06caSMauro Carvalho Chehab 			 * once it has been unplugged. */
12060c0d06caSMauro Carvalho Chehab 			if (dvb->fe[0])
12070c0d06caSMauro Carvalho Chehab 				prevent_sleep(&dvb->fe[0]->ops);
12080c0d06caSMauro Carvalho Chehab 			if (dvb->fe[1])
12090c0d06caSMauro Carvalho Chehab 				prevent_sleep(&dvb->fe[1]->ops);
12100c0d06caSMauro Carvalho Chehab 		}
12110c0d06caSMauro Carvalho Chehab 
12120c0d06caSMauro Carvalho Chehab 		em28xx_unregister_dvb(dvb);
12130c0d06caSMauro Carvalho Chehab 		kfree(dvb);
12140c0d06caSMauro Carvalho Chehab 		dev->dvb = NULL;
12150c0d06caSMauro Carvalho Chehab 	}
12160c0d06caSMauro Carvalho Chehab 
12170c0d06caSMauro Carvalho Chehab 	return 0;
12180c0d06caSMauro Carvalho Chehab }
12190c0d06caSMauro Carvalho Chehab 
12200c0d06caSMauro Carvalho Chehab static struct em28xx_ops dvb_ops = {
12210c0d06caSMauro Carvalho Chehab 	.id   = EM28XX_DVB,
12220c0d06caSMauro Carvalho Chehab 	.name = "Em28xx dvb Extension",
12230c0d06caSMauro Carvalho Chehab 	.init = em28xx_dvb_init,
12240c0d06caSMauro Carvalho Chehab 	.fini = em28xx_dvb_fini,
12250c0d06caSMauro Carvalho Chehab };
12260c0d06caSMauro Carvalho Chehab 
12270c0d06caSMauro Carvalho Chehab static int __init em28xx_dvb_register(void)
12280c0d06caSMauro Carvalho Chehab {
12290c0d06caSMauro Carvalho Chehab 	return em28xx_register_extension(&dvb_ops);
12300c0d06caSMauro Carvalho Chehab }
12310c0d06caSMauro Carvalho Chehab 
12320c0d06caSMauro Carvalho Chehab static void __exit em28xx_dvb_unregister(void)
12330c0d06caSMauro Carvalho Chehab {
12340c0d06caSMauro Carvalho Chehab 	em28xx_unregister_extension(&dvb_ops);
12350c0d06caSMauro Carvalho Chehab }
12360c0d06caSMauro Carvalho Chehab 
12370c0d06caSMauro Carvalho Chehab module_init(em28xx_dvb_register);
12380c0d06caSMauro Carvalho Chehab module_exit(em28xx_dvb_unregister);
1239