xref: /openbmc/linux/drivers/media/usb/em28xx/em28xx-dvb.c (revision b45e34f2a6724042c068bf588322598c5ae435de)
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 
13a950e4a7SFrank Schaefer  (c) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
14a950e4a7SFrank Schaefer 
150c0d06caSMauro Carvalho Chehab  Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
160c0d06caSMauro Carvalho Chehab 	(c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
170c0d06caSMauro Carvalho Chehab 	(c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
180c0d06caSMauro Carvalho Chehab 
190c0d06caSMauro Carvalho Chehab  This program is free software; you can redistribute it and/or modify
200c0d06caSMauro Carvalho Chehab  it under the terms of the GNU General Public License as published by
210c0d06caSMauro Carvalho Chehab  the Free Software Foundation; either version 2 of the License.
220c0d06caSMauro Carvalho Chehab  */
230c0d06caSMauro Carvalho Chehab 
240c0d06caSMauro Carvalho Chehab #include <linux/kernel.h>
250c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
260c0d06caSMauro Carvalho Chehab #include <linux/usb.h>
270c0d06caSMauro Carvalho Chehab 
280c0d06caSMauro Carvalho Chehab #include "em28xx.h"
290c0d06caSMauro Carvalho Chehab #include <media/v4l2-common.h>
30d3829fadSDevin Heitmueller #include <dvb_demux.h>
31d3829fadSDevin Heitmueller #include <dvb_net.h>
32d3829fadSDevin Heitmueller #include <dmxdev.h>
330c0d06caSMauro Carvalho Chehab #include <media/tuner.h>
340c0d06caSMauro Carvalho Chehab #include "tuner-simple.h"
351e8f31f3SAntti Palosaari #include <linux/gpio.h>
360c0d06caSMauro Carvalho Chehab 
370c0d06caSMauro Carvalho Chehab #include "lgdt330x.h"
380c0d06caSMauro Carvalho Chehab #include "lgdt3305.h"
390c0d06caSMauro Carvalho Chehab #include "zl10353.h"
400c0d06caSMauro Carvalho Chehab #include "s5h1409.h"
410c0d06caSMauro Carvalho Chehab #include "mt352.h"
420c0d06caSMauro Carvalho Chehab #include "mt352_priv.h" /* FIXME */
430c0d06caSMauro Carvalho Chehab #include "tda1002x.h"
4438b2df95SDevin Heitmueller #include "drx39xyj/drx39xxj.h"
450c0d06caSMauro Carvalho Chehab #include "tda18271.h"
460c0d06caSMauro Carvalho Chehab #include "s921.h"
470c0d06caSMauro Carvalho Chehab #include "drxd.h"
480c0d06caSMauro Carvalho Chehab #include "cxd2820r.h"
490c0d06caSMauro Carvalho Chehab #include "tda18271c2dd.h"
500c0d06caSMauro Carvalho Chehab #include "drxk.h"
510c0d06caSMauro Carvalho Chehab #include "tda10071.h"
5202bc1f55SMauro Carvalho Chehab #include "tda18212.h"
530c0d06caSMauro Carvalho Chehab #include "a8293.h"
540c0d06caSMauro Carvalho Chehab #include "qt1010.h"
554159d01bSMauro Carvalho Chehab #include "mb86a20s.h"
56ec573362SAntti Palosaari #include "m88ds3103.h"
57ec573362SAntti Palosaari #include "m88ts2022.h"
580c0d06caSMauro Carvalho Chehab 
590c0d06caSMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
600c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
61d8992b09SMauro Carvalho Chehab MODULE_DESCRIPTION(DRIVER_DESC " - digital TV interface");
62d8992b09SMauro Carvalho Chehab MODULE_VERSION(EM28XX_VERSION);
63d8992b09SMauro Carvalho Chehab 
640c0d06caSMauro Carvalho Chehab 
650c0d06caSMauro Carvalho Chehab static unsigned int debug;
660c0d06caSMauro Carvalho Chehab module_param(debug, int, 0644);
670c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
680c0d06caSMauro Carvalho Chehab 
690c0d06caSMauro Carvalho Chehab DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
700c0d06caSMauro Carvalho Chehab 
710c0d06caSMauro Carvalho Chehab #define dprintk(level, fmt, arg...) do {			\
720c0d06caSMauro Carvalho Chehab if (debug >= level) 						\
730c0d06caSMauro Carvalho Chehab 	printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg);	\
740c0d06caSMauro Carvalho Chehab } while (0)
750c0d06caSMauro Carvalho Chehab 
760c0d06caSMauro Carvalho Chehab struct em28xx_dvb {
770c0d06caSMauro Carvalho Chehab 	struct dvb_frontend        *fe[2];
780c0d06caSMauro Carvalho Chehab 
790c0d06caSMauro Carvalho Chehab 	/* feed count management */
800c0d06caSMauro Carvalho Chehab 	struct mutex               lock;
810c0d06caSMauro Carvalho Chehab 	int                        nfeeds;
820c0d06caSMauro Carvalho Chehab 
830c0d06caSMauro Carvalho Chehab 	/* general boilerplate stuff */
840c0d06caSMauro Carvalho Chehab 	struct dvb_adapter         adapter;
850c0d06caSMauro Carvalho Chehab 	struct dvb_demux           demux;
860c0d06caSMauro Carvalho Chehab 	struct dmxdev              dmxdev;
870c0d06caSMauro Carvalho Chehab 	struct dmx_frontend        fe_hw;
880c0d06caSMauro Carvalho Chehab 	struct dmx_frontend        fe_mem;
890c0d06caSMauro Carvalho Chehab 	struct dvb_net             net;
900c0d06caSMauro Carvalho Chehab 
910c0d06caSMauro Carvalho Chehab 	/* Due to DRX-K - probably need changes */
920c0d06caSMauro Carvalho Chehab 	int (*gate_ctrl)(struct dvb_frontend *, int);
930c0d06caSMauro Carvalho Chehab 	struct semaphore      pll_mutex;
940c0d06caSMauro Carvalho Chehab 	bool			dont_attach_fe1;
9513a5336eSAntti Palosaari 	int			lna_gpio;
96eafa2ad6SAntti Palosaari 	struct i2c_client	*i2c_client_tuner;
970c0d06caSMauro Carvalho Chehab };
980c0d06caSMauro Carvalho Chehab 
990c0d06caSMauro Carvalho Chehab 
1000c0d06caSMauro Carvalho Chehab static inline void print_err_status(struct em28xx *dev,
1010c0d06caSMauro Carvalho Chehab 				     int packet, int status)
1020c0d06caSMauro Carvalho Chehab {
1030c0d06caSMauro Carvalho Chehab 	char *errmsg = "Unknown";
1040c0d06caSMauro Carvalho Chehab 
1050c0d06caSMauro Carvalho Chehab 	switch (status) {
1060c0d06caSMauro Carvalho Chehab 	case -ENOENT:
1070c0d06caSMauro Carvalho Chehab 		errmsg = "unlinked synchronuously";
1080c0d06caSMauro Carvalho Chehab 		break;
1090c0d06caSMauro Carvalho Chehab 	case -ECONNRESET:
1100c0d06caSMauro Carvalho Chehab 		errmsg = "unlinked asynchronuously";
1110c0d06caSMauro Carvalho Chehab 		break;
1120c0d06caSMauro Carvalho Chehab 	case -ENOSR:
1130c0d06caSMauro Carvalho Chehab 		errmsg = "Buffer error (overrun)";
1140c0d06caSMauro Carvalho Chehab 		break;
1150c0d06caSMauro Carvalho Chehab 	case -EPIPE:
1160c0d06caSMauro Carvalho Chehab 		errmsg = "Stalled (device not responding)";
1170c0d06caSMauro Carvalho Chehab 		break;
1180c0d06caSMauro Carvalho Chehab 	case -EOVERFLOW:
1190c0d06caSMauro Carvalho Chehab 		errmsg = "Babble (bad cable?)";
1200c0d06caSMauro Carvalho Chehab 		break;
1210c0d06caSMauro Carvalho Chehab 	case -EPROTO:
1220c0d06caSMauro Carvalho Chehab 		errmsg = "Bit-stuff error (bad cable?)";
1230c0d06caSMauro Carvalho Chehab 		break;
1240c0d06caSMauro Carvalho Chehab 	case -EILSEQ:
1250c0d06caSMauro Carvalho Chehab 		errmsg = "CRC/Timeout (could be anything)";
1260c0d06caSMauro Carvalho Chehab 		break;
1270c0d06caSMauro Carvalho Chehab 	case -ETIME:
1280c0d06caSMauro Carvalho Chehab 		errmsg = "Device does not respond";
1290c0d06caSMauro Carvalho Chehab 		break;
1300c0d06caSMauro Carvalho Chehab 	}
1310c0d06caSMauro Carvalho Chehab 	if (packet < 0) {
1320c0d06caSMauro Carvalho Chehab 		dprintk(1, "URB status %d [%s].\n", status, errmsg);
1330c0d06caSMauro Carvalho Chehab 	} else {
1340c0d06caSMauro Carvalho Chehab 		dprintk(1, "URB packet %d, status %d [%s].\n",
1350c0d06caSMauro Carvalho Chehab 			packet, status, errmsg);
1360c0d06caSMauro Carvalho Chehab 	}
1370c0d06caSMauro Carvalho Chehab }
1380c0d06caSMauro Carvalho Chehab 
139a950e4a7SFrank Schaefer static inline int em28xx_dvb_urb_data_copy(struct em28xx *dev, struct urb *urb)
1400c0d06caSMauro Carvalho Chehab {
141a950e4a7SFrank Schaefer 	int xfer_bulk, num_packets, i;
1420c0d06caSMauro Carvalho Chehab 
1430c0d06caSMauro Carvalho Chehab 	if (!dev)
1440c0d06caSMauro Carvalho Chehab 		return 0;
1450c0d06caSMauro Carvalho Chehab 
1462665c299SFrank Schaefer 	if (dev->disconnected)
1470c0d06caSMauro Carvalho Chehab 		return 0;
1480c0d06caSMauro Carvalho Chehab 
1491653cb0cSFrank Schaefer 	if (urb->status < 0)
1500c0d06caSMauro Carvalho Chehab 		print_err_status(dev, -1, urb->status);
1510c0d06caSMauro Carvalho Chehab 
152a950e4a7SFrank Schaefer 	xfer_bulk = usb_pipebulk(urb->pipe);
1530c0d06caSMauro Carvalho Chehab 
154a950e4a7SFrank Schaefer 	if (xfer_bulk) /* bulk */
155a950e4a7SFrank Schaefer 		num_packets = 1;
156a950e4a7SFrank Schaefer 	else /* isoc */
157a950e4a7SFrank Schaefer 		num_packets = urb->number_of_packets;
158a950e4a7SFrank Schaefer 
159a950e4a7SFrank Schaefer 	for (i = 0; i < num_packets; i++) {
160a950e4a7SFrank Schaefer 		if (xfer_bulk) {
161a950e4a7SFrank Schaefer 			if (urb->status < 0) {
162a950e4a7SFrank Schaefer 				print_err_status(dev, i, urb->status);
163a950e4a7SFrank Schaefer 				if (urb->status != -EPROTO)
164a950e4a7SFrank Schaefer 					continue;
165a950e4a7SFrank Schaefer 			}
166ffdeca88SMauro Carvalho Chehab 			if (!urb->actual_length)
167ffdeca88SMauro Carvalho Chehab 				continue;
168a950e4a7SFrank Schaefer 			dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
169a950e4a7SFrank Schaefer 					urb->actual_length);
170a950e4a7SFrank Schaefer 		} else {
171a950e4a7SFrank Schaefer 			if (urb->iso_frame_desc[i].status < 0) {
172a950e4a7SFrank Schaefer 				print_err_status(dev, i,
173a950e4a7SFrank Schaefer 						 urb->iso_frame_desc[i].status);
1740c0d06caSMauro Carvalho Chehab 				if (urb->iso_frame_desc[i].status != -EPROTO)
1750c0d06caSMauro Carvalho Chehab 					continue;
1760c0d06caSMauro Carvalho Chehab 			}
177ffdeca88SMauro Carvalho Chehab 			if (!urb->iso_frame_desc[i].actual_length)
178ffdeca88SMauro Carvalho Chehab 				continue;
179a950e4a7SFrank Schaefer 			dvb_dmx_swfilter(&dev->dvb->demux,
180a950e4a7SFrank Schaefer 					 urb->transfer_buffer +
1810c0d06caSMauro Carvalho Chehab 					 urb->iso_frame_desc[i].offset,
1820c0d06caSMauro Carvalho Chehab 					 urb->iso_frame_desc[i].actual_length);
1830c0d06caSMauro Carvalho Chehab 		}
184a950e4a7SFrank Schaefer 	}
1850c0d06caSMauro Carvalho Chehab 
1860c0d06caSMauro Carvalho Chehab 	return 0;
1870c0d06caSMauro Carvalho Chehab }
1880c0d06caSMauro Carvalho Chehab 
1890c0d06caSMauro Carvalho Chehab static int em28xx_start_streaming(struct em28xx_dvb *dvb)
1900c0d06caSMauro Carvalho Chehab {
1910c0d06caSMauro Carvalho Chehab 	int rc;
1923de09fbbSHans Verkuil 	struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
1933de09fbbSHans Verkuil 	struct em28xx *dev = i2c_bus->dev;
194c647a91aSFrank Schaefer 	int dvb_max_packet_size, packet_multiplier, dvb_alt;
1950c0d06caSMauro Carvalho Chehab 
196c647a91aSFrank Schaefer 	if (dev->dvb_xfer_bulk) {
197c647a91aSFrank Schaefer 		if (!dev->dvb_ep_bulk)
198c647a91aSFrank Schaefer 			return -ENODEV;
199c647a91aSFrank Schaefer 		dvb_max_packet_size = 512; /* USB 2.0 spec */
200c647a91aSFrank Schaefer 		packet_multiplier = EM28XX_DVB_BULK_PACKET_MULTIPLIER;
201c647a91aSFrank Schaefer 		dvb_alt = 0;
202c647a91aSFrank Schaefer 	} else { /* isoc */
203c647a91aSFrank Schaefer 		if (!dev->dvb_ep_isoc)
204c647a91aSFrank Schaefer 			return -ENODEV;
205c647a91aSFrank Schaefer 		dvb_max_packet_size = dev->dvb_max_pkt_size_isoc;
206c647a91aSFrank Schaefer 		if (dvb_max_packet_size < 0)
207c647a91aSFrank Schaefer 			return dvb_max_packet_size;
208c647a91aSFrank Schaefer 		packet_multiplier = EM28XX_DVB_NUM_ISOC_PACKETS;
209c647a91aSFrank Schaefer 		dvb_alt = dev->dvb_alt_isoc;
210c647a91aSFrank Schaefer 	}
211c647a91aSFrank Schaefer 
212961717b4SFrank Schaefer 	usb_set_interface(dev->udev, dev->ifnum, dvb_alt);
2130c0d06caSMauro Carvalho Chehab 	rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
2140c0d06caSMauro Carvalho Chehab 	if (rc < 0)
2150c0d06caSMauro Carvalho Chehab 		return rc;
2160c0d06caSMauro Carvalho Chehab 
21752f7b00eSMauro Carvalho Chehab 	dprintk(1, "Using %d buffers each with %d x %d bytes, alternate %d\n",
2180c0d06caSMauro Carvalho Chehab 		EM28XX_DVB_NUM_BUFS,
219c647a91aSFrank Schaefer 		packet_multiplier,
22052f7b00eSMauro Carvalho Chehab 		dvb_max_packet_size, dvb_alt);
2210c0d06caSMauro Carvalho Chehab 
222c647a91aSFrank Schaefer 	return em28xx_init_usb_xfer(dev, EM28XX_DIGITAL_MODE,
223c647a91aSFrank Schaefer 				    dev->dvb_xfer_bulk,
224515688a8SFrank Schaefer 				    EM28XX_DVB_NUM_BUFS,
225c647a91aSFrank Schaefer 				    dvb_max_packet_size,
226c647a91aSFrank Schaefer 				    packet_multiplier,
227a950e4a7SFrank Schaefer 				    em28xx_dvb_urb_data_copy);
2280c0d06caSMauro Carvalho Chehab }
2290c0d06caSMauro Carvalho Chehab 
2300c0d06caSMauro Carvalho Chehab static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
2310c0d06caSMauro Carvalho Chehab {
2323de09fbbSHans Verkuil 	struct em28xx_i2c_bus *i2c_bus = dvb->adapter.priv;
2333de09fbbSHans Verkuil 	struct em28xx *dev = i2c_bus->dev;
2340c0d06caSMauro Carvalho Chehab 
2350c0d06caSMauro Carvalho Chehab 	em28xx_stop_urbs(dev);
2360c0d06caSMauro Carvalho Chehab 
2370c0d06caSMauro Carvalho Chehab 	return 0;
2380c0d06caSMauro Carvalho Chehab }
2390c0d06caSMauro Carvalho Chehab 
2400c0d06caSMauro Carvalho Chehab static int em28xx_start_feed(struct dvb_demux_feed *feed)
2410c0d06caSMauro Carvalho Chehab {
2420c0d06caSMauro Carvalho Chehab 	struct dvb_demux *demux  = feed->demux;
2430c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb = demux->priv;
2440c0d06caSMauro Carvalho Chehab 	int rc, ret;
2450c0d06caSMauro Carvalho Chehab 
2460c0d06caSMauro Carvalho Chehab 	if (!demux->dmx.frontend)
2470c0d06caSMauro Carvalho Chehab 		return -EINVAL;
2480c0d06caSMauro Carvalho Chehab 
2490c0d06caSMauro Carvalho Chehab 	mutex_lock(&dvb->lock);
2500c0d06caSMauro Carvalho Chehab 	dvb->nfeeds++;
2510c0d06caSMauro Carvalho Chehab 	rc = dvb->nfeeds;
2520c0d06caSMauro Carvalho Chehab 
2530c0d06caSMauro Carvalho Chehab 	if (dvb->nfeeds == 1) {
2540c0d06caSMauro Carvalho Chehab 		ret = em28xx_start_streaming(dvb);
2550c0d06caSMauro Carvalho Chehab 		if (ret < 0)
2560c0d06caSMauro Carvalho Chehab 			rc = ret;
2570c0d06caSMauro Carvalho Chehab 	}
2580c0d06caSMauro Carvalho Chehab 
2590c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dvb->lock);
2600c0d06caSMauro Carvalho Chehab 	return rc;
2610c0d06caSMauro Carvalho Chehab }
2620c0d06caSMauro Carvalho Chehab 
2630c0d06caSMauro Carvalho Chehab static int em28xx_stop_feed(struct dvb_demux_feed *feed)
2640c0d06caSMauro Carvalho Chehab {
2650c0d06caSMauro Carvalho Chehab 	struct dvb_demux *demux  = feed->demux;
2660c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb = demux->priv;
2670c0d06caSMauro Carvalho Chehab 	int err = 0;
2680c0d06caSMauro Carvalho Chehab 
2690c0d06caSMauro Carvalho Chehab 	mutex_lock(&dvb->lock);
2700c0d06caSMauro Carvalho Chehab 	dvb->nfeeds--;
2710c0d06caSMauro Carvalho Chehab 
2720c0d06caSMauro Carvalho Chehab 	if (0 == dvb->nfeeds)
2730c0d06caSMauro Carvalho Chehab 		err = em28xx_stop_streaming(dvb);
2740c0d06caSMauro Carvalho Chehab 
2750c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dvb->lock);
2760c0d06caSMauro Carvalho Chehab 	return err;
2770c0d06caSMauro Carvalho Chehab }
2780c0d06caSMauro Carvalho Chehab 
2790c0d06caSMauro Carvalho Chehab 
2800c0d06caSMauro Carvalho Chehab 
2810c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
2820c0d06caSMauro Carvalho Chehab static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
2830c0d06caSMauro Carvalho Chehab {
284a3b60209SMauro Carvalho Chehab 	struct em28xx_i2c_bus *i2c_bus = fe->dvb->priv;
285a3b60209SMauro Carvalho Chehab 	struct em28xx *dev = i2c_bus->dev;
2860c0d06caSMauro Carvalho Chehab 
2870c0d06caSMauro Carvalho Chehab 	if (acquire)
2880c0d06caSMauro Carvalho Chehab 		return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
2890c0d06caSMauro Carvalho Chehab 	else
2900c0d06caSMauro Carvalho Chehab 		return em28xx_set_mode(dev, EM28XX_SUSPEND);
2910c0d06caSMauro Carvalho Chehab }
2920c0d06caSMauro Carvalho Chehab 
2930c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
2940c0d06caSMauro Carvalho Chehab 
2950c0d06caSMauro Carvalho Chehab static struct lgdt330x_config em2880_lgdt3303_dev = {
2960c0d06caSMauro Carvalho Chehab 	.demod_address = 0x0e,
2970c0d06caSMauro Carvalho Chehab 	.demod_chip = LGDT3303,
2980c0d06caSMauro Carvalho Chehab };
2990c0d06caSMauro Carvalho Chehab 
3000c0d06caSMauro Carvalho Chehab static struct lgdt3305_config em2870_lgdt3304_dev = {
3010c0d06caSMauro Carvalho Chehab 	.i2c_addr           = 0x0e,
3020c0d06caSMauro Carvalho Chehab 	.demod_chip         = LGDT3304,
3030c0d06caSMauro Carvalho Chehab 	.spectral_inversion = 1,
3040c0d06caSMauro Carvalho Chehab 	.deny_i2c_rptr      = 1,
3050c0d06caSMauro Carvalho Chehab 	.mpeg_mode          = LGDT3305_MPEG_PARALLEL,
3060c0d06caSMauro Carvalho Chehab 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
3070c0d06caSMauro Carvalho Chehab 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
3080c0d06caSMauro Carvalho Chehab 	.vsb_if_khz         = 3250,
3090c0d06caSMauro Carvalho Chehab 	.qam_if_khz         = 4000,
3100c0d06caSMauro Carvalho Chehab };
3110c0d06caSMauro Carvalho Chehab 
3126dbea9f0SJean-Francois Thibert static struct lgdt3305_config em2874_lgdt3305_dev = {
3136dbea9f0SJean-Francois Thibert 	.i2c_addr           = 0x0e,
3146dbea9f0SJean-Francois Thibert 	.demod_chip         = LGDT3305,
3156dbea9f0SJean-Francois Thibert 	.spectral_inversion = 1,
3166dbea9f0SJean-Francois Thibert 	.deny_i2c_rptr      = 0,
3176dbea9f0SJean-Francois Thibert 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
3186dbea9f0SJean-Francois Thibert 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
3196dbea9f0SJean-Francois Thibert 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
3206dbea9f0SJean-Francois Thibert 	.vsb_if_khz         = 3250,
3216dbea9f0SJean-Francois Thibert 	.qam_if_khz         = 4000,
3226dbea9f0SJean-Francois Thibert };
3236dbea9f0SJean-Francois Thibert 
32402bc1f55SMauro Carvalho Chehab static struct lgdt3305_config em2874_lgdt3305_nogate_dev = {
32502bc1f55SMauro Carvalho Chehab 	.i2c_addr           = 0x0e,
32602bc1f55SMauro Carvalho Chehab 	.demod_chip         = LGDT3305,
32702bc1f55SMauro Carvalho Chehab 	.spectral_inversion = 1,
32802bc1f55SMauro Carvalho Chehab 	.deny_i2c_rptr      = 1,
32902bc1f55SMauro Carvalho Chehab 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
33002bc1f55SMauro Carvalho Chehab 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
33102bc1f55SMauro Carvalho Chehab 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
33202bc1f55SMauro Carvalho Chehab 	.vsb_if_khz         = 3600,
33302bc1f55SMauro Carvalho Chehab 	.qam_if_khz         = 3600,
33402bc1f55SMauro Carvalho Chehab };
33502bc1f55SMauro Carvalho Chehab 
3360c0d06caSMauro Carvalho Chehab static struct s921_config sharp_isdbt = {
3370c0d06caSMauro Carvalho Chehab 	.demod_address = 0x30 >> 1
3380c0d06caSMauro Carvalho Chehab };
3390c0d06caSMauro Carvalho Chehab 
3400c0d06caSMauro Carvalho Chehab static struct zl10353_config em28xx_zl10353_with_xc3028 = {
3410c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
3420c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
3430c0d06caSMauro Carvalho Chehab 	.parallel_ts = 1,
3440c0d06caSMauro Carvalho Chehab 	.if2 = 45600,
3450c0d06caSMauro Carvalho Chehab };
3460c0d06caSMauro Carvalho Chehab 
3470c0d06caSMauro Carvalho Chehab static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
3480c0d06caSMauro Carvalho Chehab 	.demod_address = 0x32 >> 1,
3490c0d06caSMauro Carvalho Chehab 	.output_mode   = S5H1409_PARALLEL_OUTPUT,
3500c0d06caSMauro Carvalho Chehab 	.gpio          = S5H1409_GPIO_OFF,
3510c0d06caSMauro Carvalho Chehab 	.inversion     = S5H1409_INVERSION_OFF,
3520c0d06caSMauro Carvalho Chehab 	.status_mode   = S5H1409_DEMODLOCKING,
3530c0d06caSMauro Carvalho Chehab 	.mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
3540c0d06caSMauro Carvalho Chehab };
3550c0d06caSMauro Carvalho Chehab 
3560c0d06caSMauro Carvalho Chehab static struct tda18271_std_map kworld_a340_std_map = {
3570c0d06caSMauro Carvalho Chehab 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
3580c0d06caSMauro Carvalho Chehab 		      .if_lvl = 1, .rfagc_top = 0x37, },
3590c0d06caSMauro Carvalho Chehab 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
3600c0d06caSMauro Carvalho Chehab 		      .if_lvl = 1, .rfagc_top = 0x37, },
3610c0d06caSMauro Carvalho Chehab };
3620c0d06caSMauro Carvalho Chehab 
3630c0d06caSMauro Carvalho Chehab static struct tda18271_config kworld_a340_config = {
3640c0d06caSMauro Carvalho Chehab 	.std_map           = &kworld_a340_std_map,
3650c0d06caSMauro Carvalho Chehab };
3660c0d06caSMauro Carvalho Chehab 
3676dbea9f0SJean-Francois Thibert static struct tda18271_config kworld_ub435q_v2_config = {
3686dbea9f0SJean-Francois Thibert 	.std_map	= &kworld_a340_std_map,
3696dbea9f0SJean-Francois Thibert 	.gate		= TDA18271_GATE_DIGITAL,
3706dbea9f0SJean-Francois Thibert };
3716dbea9f0SJean-Francois Thibert 
37202bc1f55SMauro Carvalho Chehab static struct tda18212_config kworld_ub435q_v3_config = {
37302bc1f55SMauro Carvalho Chehab 	.i2c_address	= 0x60,
37402bc1f55SMauro Carvalho Chehab 	.if_atsc_vsb	= 3600,
37502bc1f55SMauro Carvalho Chehab 	.if_atsc_qam	= 3600,
37602bc1f55SMauro Carvalho Chehab };
37702bc1f55SMauro Carvalho Chehab 
3780c0d06caSMauro Carvalho Chehab static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
3790c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
3800c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
3810c0d06caSMauro Carvalho Chehab 	.disable_i2c_gate_ctrl = 1,
3820c0d06caSMauro Carvalho Chehab 	.parallel_ts = 1,
3830c0d06caSMauro Carvalho Chehab 	.if2 = 45600,
3840c0d06caSMauro Carvalho Chehab };
3850c0d06caSMauro Carvalho Chehab 
3860c0d06caSMauro Carvalho Chehab static struct drxd_config em28xx_drxd = {
3870c0d06caSMauro Carvalho Chehab 	.demod_address = 0x70,
3880c0d06caSMauro Carvalho Chehab 	.demod_revision = 0xa2,
3890c0d06caSMauro Carvalho Chehab 	.pll_type = DRXD_PLL_NONE,
3900c0d06caSMauro Carvalho Chehab 	.clock = 12000,
3910c0d06caSMauro Carvalho Chehab 	.insert_rs_byte = 1,
3920c0d06caSMauro Carvalho Chehab 	.IF = 42800000,
3930c0d06caSMauro Carvalho Chehab 	.disable_i2c_gate_ctrl = 1,
3940c0d06caSMauro Carvalho Chehab };
3950c0d06caSMauro Carvalho Chehab 
3960c0d06caSMauro Carvalho Chehab static struct drxk_config terratec_h5_drxk = {
3970c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
3980c0d06caSMauro Carvalho Chehab 	.single_master = 1,
3990c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
4000c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-usb-terratec-h5-drxk.fw",
4010c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
4020c0d06caSMauro Carvalho Chehab };
4030c0d06caSMauro Carvalho Chehab 
4040c0d06caSMauro Carvalho Chehab static struct drxk_config hauppauge_930c_drxk = {
4050c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
4060c0d06caSMauro Carvalho Chehab 	.single_master = 1,
4070c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
4080c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
4090c0d06caSMauro Carvalho Chehab 	.chunk_size = 56,
4100c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
4110c0d06caSMauro Carvalho Chehab };
4120c0d06caSMauro Carvalho Chehab 
41389040136SMartin Blumenstingl static struct drxk_config terratec_htc_stick_drxk = {
4140c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
4150c0d06caSMauro Carvalho Chehab 	.single_master = 1,
4160c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
4170c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-usb-terratec-htc-stick-drxk.fw",
4180c0d06caSMauro Carvalho Chehab 	.chunk_size = 54,
4190c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
4200c0d06caSMauro Carvalho Chehab 	/* Required for the antenna_gpio to disable LNA. */
4210c0d06caSMauro Carvalho Chehab 	.antenna_dvbt = true,
4220c0d06caSMauro Carvalho Chehab 	/* The windows driver uses the same. This will disable LNA. */
4230c0d06caSMauro Carvalho Chehab 	.antenna_gpio = 0x6,
4240c0d06caSMauro Carvalho Chehab };
4250c0d06caSMauro Carvalho Chehab 
4260c0d06caSMauro Carvalho Chehab static struct drxk_config maxmedia_ub425_tc_drxk = {
4270c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
4280c0d06caSMauro Carvalho Chehab 	.single_master = 1,
4290c0d06caSMauro Carvalho Chehab 	.no_i2c_bridge = 1,
4308d100b27SAntti Palosaari 	.microcode_name = "dvb-demod-drxk-01.fw",
4318d100b27SAntti Palosaari 	.chunk_size = 62,
432de0fc463SAntti Palosaari 	.qam_demod_parameter_count = 2,
4330c0d06caSMauro Carvalho Chehab };
4340c0d06caSMauro Carvalho Chehab 
4350c0d06caSMauro Carvalho Chehab static struct drxk_config pctv_520e_drxk = {
4360c0d06caSMauro Carvalho Chehab 	.adr = 0x29,
4370c0d06caSMauro Carvalho Chehab 	.single_master = 1,
4380c0d06caSMauro Carvalho Chehab 	.microcode_name = "dvb-demod-drxk-pctv.fw",
4390c0d06caSMauro Carvalho Chehab 	.qam_demod_parameter_count = 2,
4400c0d06caSMauro Carvalho Chehab 	.chunk_size = 58,
4410c0d06caSMauro Carvalho Chehab 	.antenna_dvbt = true, /* disable LNA */
4420c0d06caSMauro Carvalho Chehab 	.antenna_gpio = (1 << 2), /* disable LNA */
4430c0d06caSMauro Carvalho Chehab };
4440c0d06caSMauro Carvalho Chehab 
4450c0d06caSMauro Carvalho Chehab static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
4460c0d06caSMauro Carvalho Chehab {
4470c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb = fe->sec_priv;
4480c0d06caSMauro Carvalho Chehab 	int status;
4490c0d06caSMauro Carvalho Chehab 
4500c0d06caSMauro Carvalho Chehab 	if (!dvb)
4510c0d06caSMauro Carvalho Chehab 		return -EINVAL;
4520c0d06caSMauro Carvalho Chehab 
4530c0d06caSMauro Carvalho Chehab 	if (enable) {
4540c0d06caSMauro Carvalho Chehab 		down(&dvb->pll_mutex);
4550c0d06caSMauro Carvalho Chehab 		status = dvb->gate_ctrl(fe, 1);
4560c0d06caSMauro Carvalho Chehab 	} else {
4570c0d06caSMauro Carvalho Chehab 		status = dvb->gate_ctrl(fe, 0);
4580c0d06caSMauro Carvalho Chehab 		up(&dvb->pll_mutex);
4590c0d06caSMauro Carvalho Chehab 	}
4600c0d06caSMauro Carvalho Chehab 	return status;
4610c0d06caSMauro Carvalho Chehab }
4620c0d06caSMauro Carvalho Chehab 
4630c0d06caSMauro Carvalho Chehab static void hauppauge_hvr930c_init(struct em28xx *dev)
4640c0d06caSMauro Carvalho Chehab {
4650c0d06caSMauro Carvalho Chehab 	int i;
4660c0d06caSMauro Carvalho Chehab 
4670c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
468907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xff,	0xff,	0x65},
469907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xfb,	0xff,	0x32},
470907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xff,	0xff,	0xb8},
4710c0d06caSMauro Carvalho Chehab 		{	-1,			-1,	-1,	-1},
4720c0d06caSMauro Carvalho Chehab 	};
4730c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
474907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x01},
475907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xaf,	0xff,	0x65},
476907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x76},
477907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x01},
478907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x0b},
479907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x40},
4800c0d06caSMauro Carvalho Chehab 
481907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x65},
482907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x65},
483907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xcf,	0xff,	0x0b},
484907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xef,	0xff,	0x65},
4850c0d06caSMauro Carvalho Chehab 
4860c0d06caSMauro Carvalho Chehab 		{	-1,			-1,	-1,	-1},
4870c0d06caSMauro Carvalho Chehab 	};
4880c0d06caSMauro Carvalho Chehab 
4890c0d06caSMauro Carvalho Chehab 	struct {
4900c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
4910c0d06caSMauro Carvalho Chehab 		int len;
4920c0d06caSMauro Carvalho Chehab 	} regs[] = {
4930c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
4940c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
4950c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
4960c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
4970c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
4980c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
4990c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
5000c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
5010c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x00 }, 2},
5020c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04 }, 2},
5030c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
5040c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14 }, 2},
5050c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
5060c0d06caSMauro Carvalho Chehab 	};
5070c0d06caSMauro Carvalho Chehab 
5080c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, hauppauge_hvr930c_init);
5090c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
5100c0d06caSMauro Carvalho Chehab 	msleep(10);
5110c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
5120c0d06caSMauro Carvalho Chehab 	msleep(10);
5130c0d06caSMauro Carvalho Chehab 
514c7a45e5bSMauro Carvalho Chehab 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
5150c0d06caSMauro Carvalho Chehab 
5160c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
517c7a45e5bSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
5180c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, hauppauge_hvr930c_end);
5190c0d06caSMauro Carvalho Chehab 
5200c0d06caSMauro Carvalho Chehab 	msleep(100);
5210c0d06caSMauro Carvalho Chehab 
5220c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
5230c0d06caSMauro Carvalho Chehab 	msleep(30);
5240c0d06caSMauro Carvalho Chehab 
5250c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
5260c0d06caSMauro Carvalho Chehab 	msleep(10);
5270c0d06caSMauro Carvalho Chehab 
5280c0d06caSMauro Carvalho Chehab }
5290c0d06caSMauro Carvalho Chehab 
5300c0d06caSMauro Carvalho Chehab static void terratec_h5_init(struct em28xx *dev)
5310c0d06caSMauro Carvalho Chehab {
5320c0d06caSMauro Carvalho Chehab 	int i;
5330c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_h5_init[] = {
534c074fc4cSFrank Schaefer 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
535907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
536907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xf2,	0xff,	50},
537907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
5380c0d06caSMauro Carvalho Chehab 		{	-1,			-1,	-1,	-1},
5390c0d06caSMauro Carvalho Chehab 	};
5400c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_h5_end[] = {
541907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
542907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	50},
543907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
5440c0d06caSMauro Carvalho Chehab 		{	-1,			-1,	-1,	-1},
5450c0d06caSMauro Carvalho Chehab 	};
5460c0d06caSMauro Carvalho Chehab 	struct {
5470c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
5480c0d06caSMauro Carvalho Chehab 		int len;
5490c0d06caSMauro Carvalho Chehab 	} regs[] = {
5500c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
5510c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
5520c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
5530c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
5540c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
5550c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
5560c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
5570c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
5580c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x00 }, 2},
5590c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04 }, 2},
5600c0d06caSMauro Carvalho Chehab 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
5610c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14 }, 2},
5620c0d06caSMauro Carvalho Chehab 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
5630c0d06caSMauro Carvalho Chehab 	};
5640c0d06caSMauro Carvalho Chehab 
5650c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_h5_init);
5660c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
5670c0d06caSMauro Carvalho Chehab 	msleep(10);
5680c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
5690c0d06caSMauro Carvalho Chehab 	msleep(10);
5700c0d06caSMauro Carvalho Chehab 
571c7a45e5bSMauro Carvalho Chehab 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
5720c0d06caSMauro Carvalho Chehab 
5730c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
574c7a45e5bSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
5750c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_h5_end);
5760c0d06caSMauro Carvalho Chehab };
5770c0d06caSMauro Carvalho Chehab 
5780c0d06caSMauro Carvalho Chehab static void terratec_htc_stick_init(struct em28xx *dev)
5790c0d06caSMauro Carvalho Chehab {
5800c0d06caSMauro Carvalho Chehab 	int i;
5810c0d06caSMauro Carvalho Chehab 
5820c0d06caSMauro Carvalho Chehab 	/*
5830c0d06caSMauro Carvalho Chehab 	 * GPIO configuration:
5840c0d06caSMauro Carvalho Chehab 	 * 0xff: unknown (does not affect DVB-T).
5850c0d06caSMauro Carvalho Chehab 	 * 0xf6: DRX-K (demodulator).
5860c0d06caSMauro Carvalho Chehab 	 * 0xe6: unknown (does not affect DVB-T).
5870c0d06caSMauro Carvalho Chehab 	 * 0xb6: unknown (does not affect DVB-T).
5880c0d06caSMauro Carvalho Chehab 	 */
5890c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_htc_stick_init[] = {
590c074fc4cSFrank Schaefer 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
591907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
592907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	50},
593907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	100},
5940c0d06caSMauro Carvalho Chehab 		{	-1,			-1,	-1,	-1},
5950c0d06caSMauro Carvalho Chehab 	};
5960c0d06caSMauro Carvalho Chehab 	struct em28xx_reg_seq terratec_htc_stick_end[] = {
597907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xb6,	0xff,	100},
598907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xf6,	0xff,	50},
5990c0d06caSMauro Carvalho Chehab 		{	-1,			-1,	-1,	-1},
6000c0d06caSMauro Carvalho Chehab 	};
6010c0d06caSMauro Carvalho Chehab 
60289040136SMartin Blumenstingl 	/*
60389040136SMartin Blumenstingl 	 * Init the analog decoder (not yet supported), but
60489040136SMartin Blumenstingl 	 * it's probably still a good idea.
60589040136SMartin Blumenstingl 	 */
6060c0d06caSMauro Carvalho Chehab 	struct {
6070c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
6080c0d06caSMauro Carvalho Chehab 		int len;
6090c0d06caSMauro Carvalho Chehab 	} regs[] = {
6100c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
6110c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
6120c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
6130c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
6140c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
6150c0d06caSMauro Carvalho Chehab 	};
6160c0d06caSMauro Carvalho Chehab 
6170c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_htc_stick_init);
6180c0d06caSMauro Carvalho Chehab 
6190c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
6200c0d06caSMauro Carvalho Chehab 	msleep(10);
6210c0d06caSMauro Carvalho Chehab 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
6220c0d06caSMauro Carvalho Chehab 	msleep(10);
6230c0d06caSMauro Carvalho Chehab 
624c7a45e5bSMauro Carvalho Chehab 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
6250c0d06caSMauro Carvalho Chehab 
6260c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
627c7a45e5bSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
6280c0d06caSMauro Carvalho Chehab 
6290c0d06caSMauro Carvalho Chehab 	em28xx_gpio_set(dev, terratec_htc_stick_end);
6300c0d06caSMauro Carvalho Chehab };
6310c0d06caSMauro Carvalho Chehab 
63289040136SMartin Blumenstingl static void terratec_htc_usb_xs_init(struct em28xx *dev)
63389040136SMartin Blumenstingl {
63489040136SMartin Blumenstingl 	int i;
63589040136SMartin Blumenstingl 
63689040136SMartin Blumenstingl 	struct em28xx_reg_seq terratec_htc_usb_xs_init[] = {
637c074fc4cSFrank Schaefer 		{EM2820_R08_GPIO_CTRL,		0xff,	0xff,	10},
638907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xb2,	0xff,	100},
639907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xb2,	0xff,	50},
640907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xb6,	0xff,	100},
64189040136SMartin Blumenstingl 		{	-1,			-1,	-1,	-1},
64289040136SMartin Blumenstingl 	};
64389040136SMartin Blumenstingl 	struct em28xx_reg_seq terratec_htc_usb_xs_end[] = {
644907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	100},
645907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xa6,	0xff,	50},
646907d109bSFrank Schaefer 		{EM2874_R80_GPIO_P0_CTRL,	0xe6,	0xff,	100},
64789040136SMartin Blumenstingl 		{	-1,			-1,	-1,	-1},
64889040136SMartin Blumenstingl 	};
64989040136SMartin Blumenstingl 
65089040136SMartin Blumenstingl 	/*
65189040136SMartin Blumenstingl 	 * Init the analog decoder (not yet supported), but
65289040136SMartin Blumenstingl 	 * it's probably still a good idea.
65389040136SMartin Blumenstingl 	 */
65489040136SMartin Blumenstingl 	struct {
65589040136SMartin Blumenstingl 		unsigned char r[4];
65689040136SMartin Blumenstingl 		int len;
65789040136SMartin Blumenstingl 	} regs[] = {
65889040136SMartin Blumenstingl 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
65989040136SMartin Blumenstingl 		{{ 0x01, 0x02 }, 2},
66089040136SMartin Blumenstingl 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
66189040136SMartin Blumenstingl 		{{ 0x01, 0x00 }, 2},
66289040136SMartin Blumenstingl 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
66389040136SMartin Blumenstingl 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
66489040136SMartin Blumenstingl 		{{ 0x01, 0x00 }, 2},
66589040136SMartin Blumenstingl 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
66689040136SMartin Blumenstingl 		{{ 0x04, 0x00 }, 2},
66789040136SMartin Blumenstingl 		{{ 0x00, 0x04 }, 2},
66889040136SMartin Blumenstingl 		{{ 0x00, 0x04, 0x00, 0x0a }, 4},
66989040136SMartin Blumenstingl 		{{ 0x04, 0x14 }, 2},
67089040136SMartin Blumenstingl 		{{ 0x04, 0x14, 0x00, 0x00 }, 4},
67189040136SMartin Blumenstingl 	};
67289040136SMartin Blumenstingl 
67389040136SMartin Blumenstingl 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
67489040136SMartin Blumenstingl 
67589040136SMartin Blumenstingl 	em28xx_gpio_set(dev, terratec_htc_usb_xs_init);
67689040136SMartin Blumenstingl 
67789040136SMartin Blumenstingl 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
67889040136SMartin Blumenstingl 	msleep(10);
67989040136SMartin Blumenstingl 	em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
68089040136SMartin Blumenstingl 	msleep(10);
68189040136SMartin Blumenstingl 
682c7a45e5bSMauro Carvalho Chehab 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1;
68389040136SMartin Blumenstingl 
68489040136SMartin Blumenstingl 	for (i = 0; i < ARRAY_SIZE(regs); i++)
685c7a45e5bSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
68689040136SMartin Blumenstingl 
68789040136SMartin Blumenstingl 	em28xx_gpio_set(dev, terratec_htc_usb_xs_end);
68889040136SMartin Blumenstingl };
68989040136SMartin Blumenstingl 
6900c0d06caSMauro Carvalho Chehab static void pctv_520e_init(struct em28xx *dev)
6910c0d06caSMauro Carvalho Chehab {
6920c0d06caSMauro Carvalho Chehab 	/*
6930c0d06caSMauro Carvalho Chehab 	 * Init AVF4910B analog decoder. Looks like I2C traffic to
6940c0d06caSMauro Carvalho Chehab 	 * digital demodulator and tuner are routed via AVF4910B.
6950c0d06caSMauro Carvalho Chehab 	 */
6960c0d06caSMauro Carvalho Chehab 	int i;
6970c0d06caSMauro Carvalho Chehab 	struct {
6980c0d06caSMauro Carvalho Chehab 		unsigned char r[4];
6990c0d06caSMauro Carvalho Chehab 		int len;
7000c0d06caSMauro Carvalho Chehab 	} regs[] = {
7010c0d06caSMauro Carvalho Chehab 		{{ 0x06, 0x02, 0x00, 0x31 }, 4},
7020c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02 }, 2},
7030c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x02, 0x00, 0xc6 }, 4},
7040c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
7050c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0xff, 0xaf }, 4},
7060c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x03, 0xa0 }, 4},
7070c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00 }, 2},
7080c0d06caSMauro Carvalho Chehab 		{{ 0x01, 0x00, 0x73, 0xaf }, 4},
7090c0d06caSMauro Carvalho Chehab 	};
7100c0d06caSMauro Carvalho Chehab 
711c7a45e5bSMauro Carvalho Chehab 	dev->i2c_client[dev->def_i2c_bus].addr = 0x82 >> 1; /* 0x41 */
7120c0d06caSMauro Carvalho Chehab 
7130c0d06caSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(regs); i++)
714c7a45e5bSMauro Carvalho Chehab 		i2c_master_send(&dev->i2c_client[dev->def_i2c_bus], regs[i].r, regs[i].len);
7150c0d06caSMauro Carvalho Chehab };
7160c0d06caSMauro Carvalho Chehab 
71733eebec5SAntti Palosaari static int em28xx_pctv_290e_set_lna(struct dvb_frontend *fe)
71813a5336eSAntti Palosaari {
71933eebec5SAntti Palosaari 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
72013a5336eSAntti Palosaari 	struct em28xx *dev = fe->dvb->priv;
72113a5336eSAntti Palosaari #ifdef CONFIG_GPIOLIB
72213a5336eSAntti Palosaari 	struct em28xx_dvb *dvb = dev->dvb;
72313a5336eSAntti Palosaari 	int ret;
72413a5336eSAntti Palosaari 	unsigned long flags;
72513a5336eSAntti Palosaari 
72633eebec5SAntti Palosaari 	if (c->lna == 1)
72733eebec5SAntti Palosaari 		flags = GPIOF_OUT_INIT_HIGH; /* enable LNA */
72813a5336eSAntti Palosaari 	else
72933eebec5SAntti Palosaari 		flags = GPIOF_OUT_INIT_LOW; /* disable LNA */
73013a5336eSAntti Palosaari 
73113a5336eSAntti Palosaari 	ret = gpio_request_one(dvb->lna_gpio, flags, NULL);
73213a5336eSAntti Palosaari 	if (ret)
73313a5336eSAntti Palosaari 		em28xx_errdev("gpio request failed %d\n", ret);
73413a5336eSAntti Palosaari 	else
73513a5336eSAntti Palosaari 		gpio_free(dvb->lna_gpio);
73613a5336eSAntti Palosaari 
73713a5336eSAntti Palosaari 	return ret;
73813a5336eSAntti Palosaari #else
73933eebec5SAntti Palosaari 	dev_warn(&dev->udev->dev, "%s: LNA control is disabled (lna=%u)\n",
74033eebec5SAntti Palosaari 			KBUILD_MODNAME, c->lna);
74113a5336eSAntti Palosaari 	return 0;
74213a5336eSAntti Palosaari #endif
74313a5336eSAntti Palosaari }
74413a5336eSAntti Palosaari 
7450c0d06caSMauro Carvalho Chehab static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
7460c0d06caSMauro Carvalho Chehab {
7470c0d06caSMauro Carvalho Chehab 	/* Values extracted from a USB trace of the Terratec Windows driver */
7480c0d06caSMauro Carvalho Chehab 	static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
7490c0d06caSMauro Carvalho Chehab 	static u8 reset[]          = { RESET,      0x80 };
7500c0d06caSMauro Carvalho Chehab 	static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
7510c0d06caSMauro Carvalho Chehab 	static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
7520c0d06caSMauro Carvalho Chehab 	static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
7530c0d06caSMauro Carvalho Chehab 	static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
7540c0d06caSMauro Carvalho Chehab 	static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
7550c0d06caSMauro Carvalho Chehab 	static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
7560c0d06caSMauro Carvalho Chehab 	static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
7570c0d06caSMauro Carvalho Chehab 	static u8 tuner_go[]       = { TUNER_GO, 0x01};
7580c0d06caSMauro Carvalho Chehab 
7590c0d06caSMauro Carvalho Chehab 	mt352_write(fe, clock_config,   sizeof(clock_config));
7600c0d06caSMauro Carvalho Chehab 	udelay(200);
7610c0d06caSMauro Carvalho Chehab 	mt352_write(fe, reset,          sizeof(reset));
7620c0d06caSMauro Carvalho Chehab 	mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
7630c0d06caSMauro Carvalho Chehab 	mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
7640c0d06caSMauro Carvalho Chehab 	mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
7650c0d06caSMauro Carvalho Chehab 	mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
7660c0d06caSMauro Carvalho Chehab 	mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
7670c0d06caSMauro Carvalho Chehab 	mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
7680c0d06caSMauro Carvalho Chehab 	mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
7690c0d06caSMauro Carvalho Chehab 	mt352_write(fe, tuner_go,       sizeof(tuner_go));
7700c0d06caSMauro Carvalho Chehab 	return 0;
7710c0d06caSMauro Carvalho Chehab }
7720c0d06caSMauro Carvalho Chehab 
7730c0d06caSMauro Carvalho Chehab static struct mt352_config terratec_xs_mt352_cfg = {
7740c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
7750c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
7760c0d06caSMauro Carvalho Chehab 	.if2 = 45600,
7770c0d06caSMauro Carvalho Chehab 	.demod_init = em28xx_mt352_terratec_xs_init,
7780c0d06caSMauro Carvalho Chehab };
7790c0d06caSMauro Carvalho Chehab 
7800c0d06caSMauro Carvalho Chehab static struct tda10023_config em28xx_tda10023_config = {
7810c0d06caSMauro Carvalho Chehab 	.demod_address = 0x0c,
7820c0d06caSMauro Carvalho Chehab 	.invert = 1,
7830c0d06caSMauro Carvalho Chehab };
7840c0d06caSMauro Carvalho Chehab 
7850c0d06caSMauro Carvalho Chehab static struct cxd2820r_config em28xx_cxd2820r_config = {
7860c0d06caSMauro Carvalho Chehab 	.i2c_address = (0xd8 >> 1),
7870c0d06caSMauro Carvalho Chehab 	.ts_mode = CXD2820R_TS_SERIAL,
7880c0d06caSMauro Carvalho Chehab };
7890c0d06caSMauro Carvalho Chehab 
7900c0d06caSMauro Carvalho Chehab static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
7910c0d06caSMauro Carvalho Chehab 	.output_opt = TDA18271_OUTPUT_LT_OFF,
7920c0d06caSMauro Carvalho Chehab 	.gate = TDA18271_GATE_DIGITAL,
7930c0d06caSMauro Carvalho Chehab };
7940c0d06caSMauro Carvalho Chehab 
7950c0d06caSMauro Carvalho Chehab static const struct tda10071_config em28xx_tda10071_config = {
79641f55d57SMichael Krufky 	.demod_i2c_addr = 0x55, /* (0xaa >> 1) */
79741f55d57SMichael Krufky 	.tuner_i2c_addr = 0x14,
7980c0d06caSMauro Carvalho Chehab 	.i2c_wr_max = 64,
7990c0d06caSMauro Carvalho Chehab 	.ts_mode = TDA10071_TS_SERIAL,
8000c0d06caSMauro Carvalho Chehab 	.spec_inv = 0,
8010c0d06caSMauro Carvalho Chehab 	.xtal = 40444000, /* 40.444 MHz */
8020c0d06caSMauro Carvalho Chehab 	.pll_multiplier = 20,
8030c0d06caSMauro Carvalho Chehab };
8040c0d06caSMauro Carvalho Chehab 
8050c0d06caSMauro Carvalho Chehab static const struct a8293_config em28xx_a8293_config = {
8060c0d06caSMauro Carvalho Chehab 	.i2c_addr = 0x08, /* (0x10 >> 1) */
8070c0d06caSMauro Carvalho Chehab };
8080c0d06caSMauro Carvalho Chehab 
8090c0d06caSMauro Carvalho Chehab static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
8100c0d06caSMauro Carvalho Chehab 	.demod_address = (0x1e >> 1),
8110c0d06caSMauro Carvalho Chehab 	.disable_i2c_gate_ctrl = 1,
8120c0d06caSMauro Carvalho Chehab 	.no_tuner = 1,
8130c0d06caSMauro Carvalho Chehab 	.parallel_ts = 1,
8140c0d06caSMauro Carvalho Chehab };
8150c0d06caSMauro Carvalho Chehab static struct qt1010_config em28xx_qt1010_config = {
8160c0d06caSMauro Carvalho Chehab 	.i2c_address = 0x62
8170c0d06caSMauro Carvalho Chehab };
8180c0d06caSMauro Carvalho Chehab 
8194159d01bSMauro Carvalho Chehab static const struct mb86a20s_config c3tech_duo_mb86a20s_config = {
8204159d01bSMauro Carvalho Chehab 	.demod_address = 0x10,
8214159d01bSMauro Carvalho Chehab 	.is_serial = true,
8224159d01bSMauro Carvalho Chehab };
8234159d01bSMauro Carvalho Chehab 
8244159d01bSMauro Carvalho Chehab static struct tda18271_std_map mb86a20s_tda18271_config = {
8254159d01bSMauro Carvalho Chehab 	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
8264159d01bSMauro Carvalho Chehab 		      .if_lvl = 1, .rfagc_top = 0x37, },
8274159d01bSMauro Carvalho Chehab };
8284159d01bSMauro Carvalho Chehab 
8294159d01bSMauro Carvalho Chehab static struct tda18271_config c3tech_duo_tda18271_config = {
8304159d01bSMauro Carvalho Chehab 	.std_map = &mb86a20s_tda18271_config,
8314159d01bSMauro Carvalho Chehab 	.gate    = TDA18271_GATE_DIGITAL,
8324159d01bSMauro Carvalho Chehab 	.small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
8334159d01bSMauro Carvalho Chehab };
8344159d01bSMauro Carvalho Chehab 
835ec573362SAntti Palosaari static const struct m88ds3103_config pctv_461e_m88ds3103_config = {
836ec573362SAntti Palosaari 	.i2c_addr = 0x68,
837ec573362SAntti Palosaari 	.clock = 27000000,
838ec573362SAntti Palosaari 	.i2c_wr_max = 33,
839ec573362SAntti Palosaari 	.clock_out = 0,
840ec573362SAntti Palosaari 	.ts_mode = M88DS3103_TS_PARALLEL_16,
841ec573362SAntti Palosaari 	.agc = 0x99,
842ec573362SAntti Palosaari };
843ec573362SAntti Palosaari 
84438b2df95SDevin Heitmueller 
84538b2df95SDevin Heitmueller static struct tda18271_std_map drx_j_std_map = {
84638b2df95SDevin Heitmueller 	.atsc_6   = { .if_freq = 5000, .agc_mode = 3, .std = 0, .if_lvl = 1,
84738b2df95SDevin Heitmueller 		      .rfagc_top = 0x37, },
84838b2df95SDevin Heitmueller 	.qam_6    = { .if_freq = 5380, .agc_mode = 3, .std = 3, .if_lvl = 1,
84938b2df95SDevin Heitmueller 		      .rfagc_top = 0x37, },
85038b2df95SDevin Heitmueller };
85138b2df95SDevin Heitmueller 
85238b2df95SDevin Heitmueller static struct tda18271_config pinnacle_80e_dvb_config = {
85338b2df95SDevin Heitmueller 	.std_map = &drx_j_std_map,
85438b2df95SDevin Heitmueller 	.gate    = TDA18271_GATE_DIGITAL,
85538b2df95SDevin Heitmueller 	.role    = TDA18271_MASTER,
85638b2df95SDevin Heitmueller };
85738b2df95SDevin Heitmueller 
8580c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
8590c0d06caSMauro Carvalho Chehab 
8600c0d06caSMauro Carvalho Chehab static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
8610c0d06caSMauro Carvalho Chehab {
8620c0d06caSMauro Carvalho Chehab 	struct dvb_frontend *fe;
8630c0d06caSMauro Carvalho Chehab 	struct xc2028_config cfg;
864ee97207cSMauro Carvalho Chehab 	struct xc2028_ctrl ctl;
8650c0d06caSMauro Carvalho Chehab 
8660c0d06caSMauro Carvalho Chehab 	memset(&cfg, 0, sizeof(cfg));
867c7a45e5bSMauro Carvalho Chehab 	cfg.i2c_adap  = &dev->i2c_adap[dev->def_i2c_bus];
8680c0d06caSMauro Carvalho Chehab 	cfg.i2c_addr  = addr;
8690c0d06caSMauro Carvalho Chehab 
870ee97207cSMauro Carvalho Chehab 	memset(&ctl, 0, sizeof(ctl));
871ee97207cSMauro Carvalho Chehab 	em28xx_setup_xc3028(dev, &ctl);
872ee97207cSMauro Carvalho Chehab 	cfg.ctrl  = &ctl;
873ee97207cSMauro Carvalho Chehab 
8740c0d06caSMauro Carvalho Chehab 	if (!dev->dvb->fe[0]) {
8750c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: dvb frontend not attached. "
8760c0d06caSMauro Carvalho Chehab 				"Can't attach xc3028\n");
8770c0d06caSMauro Carvalho Chehab 		return -EINVAL;
8780c0d06caSMauro Carvalho Chehab 	}
8790c0d06caSMauro Carvalho Chehab 
8800c0d06caSMauro Carvalho Chehab 	fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
8810c0d06caSMauro Carvalho Chehab 	if (!fe) {
8820c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: xc3028 attach failed\n");
8830c0d06caSMauro Carvalho Chehab 		dvb_frontend_detach(dev->dvb->fe[0]);
8840c0d06caSMauro Carvalho Chehab 		dev->dvb->fe[0] = NULL;
8850c0d06caSMauro Carvalho Chehab 		return -EINVAL;
8860c0d06caSMauro Carvalho Chehab 	}
8870c0d06caSMauro Carvalho Chehab 
8880c0d06caSMauro Carvalho Chehab 	em28xx_info("%s/2: xc3028 attached\n", dev->name);
8890c0d06caSMauro Carvalho Chehab 
8900c0d06caSMauro Carvalho Chehab 	return 0;
8910c0d06caSMauro Carvalho Chehab }
8920c0d06caSMauro Carvalho Chehab 
8930c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
8940c0d06caSMauro Carvalho Chehab 
8950c0d06caSMauro Carvalho Chehab static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
8960c0d06caSMauro Carvalho Chehab 			       struct em28xx *dev, struct device *device)
8970c0d06caSMauro Carvalho Chehab {
8980c0d06caSMauro Carvalho Chehab 	int result;
8990c0d06caSMauro Carvalho Chehab 
9000c0d06caSMauro Carvalho Chehab 	mutex_init(&dvb->lock);
9010c0d06caSMauro Carvalho Chehab 
9020c0d06caSMauro Carvalho Chehab 	/* register adapter */
9030c0d06caSMauro Carvalho Chehab 	result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
9040c0d06caSMauro Carvalho Chehab 				      adapter_nr);
9050c0d06caSMauro Carvalho Chehab 	if (result < 0) {
9060c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
9070c0d06caSMauro Carvalho Chehab 		       dev->name, result);
9080c0d06caSMauro Carvalho Chehab 		goto fail_adapter;
9090c0d06caSMauro Carvalho Chehab 	}
9100c0d06caSMauro Carvalho Chehab 
9110c0d06caSMauro Carvalho Chehab 	/* Ensure all frontends negotiate bus access */
9120c0d06caSMauro Carvalho Chehab 	dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
9130c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
9140c0d06caSMauro Carvalho Chehab 		dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
9150c0d06caSMauro Carvalho Chehab 
9163de09fbbSHans Verkuil 	dvb->adapter.priv = &dev->i2c_bus[dev->def_i2c_bus];
9170c0d06caSMauro Carvalho Chehab 
9180c0d06caSMauro Carvalho Chehab 	/* register frontend */
9190c0d06caSMauro Carvalho Chehab 	result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
9200c0d06caSMauro Carvalho Chehab 	if (result < 0) {
9210c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
9220c0d06caSMauro Carvalho Chehab 		       dev->name, result);
9230c0d06caSMauro Carvalho Chehab 		goto fail_frontend0;
9240c0d06caSMauro Carvalho Chehab 	}
9250c0d06caSMauro Carvalho Chehab 
9260c0d06caSMauro Carvalho Chehab 	/* register 2nd frontend */
9270c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1]) {
9280c0d06caSMauro Carvalho Chehab 		result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
9290c0d06caSMauro Carvalho Chehab 		if (result < 0) {
9300c0d06caSMauro Carvalho Chehab 			printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
9310c0d06caSMauro Carvalho Chehab 				dev->name, result);
9320c0d06caSMauro Carvalho Chehab 			goto fail_frontend1;
9330c0d06caSMauro Carvalho Chehab 		}
9340c0d06caSMauro Carvalho Chehab 	}
9350c0d06caSMauro Carvalho Chehab 
9360c0d06caSMauro Carvalho Chehab 	/* register demux stuff */
9370c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.capabilities =
9380c0d06caSMauro Carvalho Chehab 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
9390c0d06caSMauro Carvalho Chehab 		DMX_MEMORY_BASED_FILTERING;
9400c0d06caSMauro Carvalho Chehab 	dvb->demux.priv       = dvb;
9410c0d06caSMauro Carvalho Chehab 	dvb->demux.filternum  = 256;
9420c0d06caSMauro Carvalho Chehab 	dvb->demux.feednum    = 256;
9430c0d06caSMauro Carvalho Chehab 	dvb->demux.start_feed = em28xx_start_feed;
9440c0d06caSMauro Carvalho Chehab 	dvb->demux.stop_feed  = em28xx_stop_feed;
9450c0d06caSMauro Carvalho Chehab 
9460c0d06caSMauro Carvalho Chehab 	result = dvb_dmx_init(&dvb->demux);
9470c0d06caSMauro Carvalho Chehab 	if (result < 0) {
9480c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
9490c0d06caSMauro Carvalho Chehab 		       dev->name, result);
9500c0d06caSMauro Carvalho Chehab 		goto fail_dmx;
9510c0d06caSMauro Carvalho Chehab 	}
9520c0d06caSMauro Carvalho Chehab 
9530c0d06caSMauro Carvalho Chehab 	dvb->dmxdev.filternum    = 256;
9540c0d06caSMauro Carvalho Chehab 	dvb->dmxdev.demux        = &dvb->demux.dmx;
9550c0d06caSMauro Carvalho Chehab 	dvb->dmxdev.capabilities = 0;
9560c0d06caSMauro Carvalho Chehab 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
9570c0d06caSMauro Carvalho Chehab 	if (result < 0) {
9580c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
9590c0d06caSMauro Carvalho Chehab 		       dev->name, result);
9600c0d06caSMauro Carvalho Chehab 		goto fail_dmxdev;
9610c0d06caSMauro Carvalho Chehab 	}
9620c0d06caSMauro Carvalho Chehab 
9630c0d06caSMauro Carvalho Chehab 	dvb->fe_hw.source = DMX_FRONTEND_0;
9640c0d06caSMauro Carvalho Chehab 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
9650c0d06caSMauro Carvalho Chehab 	if (result < 0) {
9660c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
9670c0d06caSMauro Carvalho Chehab 		       dev->name, result);
9680c0d06caSMauro Carvalho Chehab 		goto fail_fe_hw;
9690c0d06caSMauro Carvalho Chehab 	}
9700c0d06caSMauro Carvalho Chehab 
9710c0d06caSMauro Carvalho Chehab 	dvb->fe_mem.source = DMX_MEMORY_FE;
9720c0d06caSMauro Carvalho Chehab 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
9730c0d06caSMauro Carvalho Chehab 	if (result < 0) {
9740c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
9750c0d06caSMauro Carvalho Chehab 		       dev->name, result);
9760c0d06caSMauro Carvalho Chehab 		goto fail_fe_mem;
9770c0d06caSMauro Carvalho Chehab 	}
9780c0d06caSMauro Carvalho Chehab 
9790c0d06caSMauro Carvalho Chehab 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
9800c0d06caSMauro Carvalho Chehab 	if (result < 0) {
9810c0d06caSMauro Carvalho Chehab 		printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
9820c0d06caSMauro Carvalho Chehab 		       dev->name, result);
9830c0d06caSMauro Carvalho Chehab 		goto fail_fe_conn;
9840c0d06caSMauro Carvalho Chehab 	}
9850c0d06caSMauro Carvalho Chehab 
9860c0d06caSMauro Carvalho Chehab 	/* register network adapter */
9870c0d06caSMauro Carvalho Chehab 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
9880c0d06caSMauro Carvalho Chehab 	return 0;
9890c0d06caSMauro Carvalho Chehab 
9900c0d06caSMauro Carvalho Chehab fail_fe_conn:
9910c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
9920c0d06caSMauro Carvalho Chehab fail_fe_mem:
9930c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
9940c0d06caSMauro Carvalho Chehab fail_fe_hw:
9950c0d06caSMauro Carvalho Chehab 	dvb_dmxdev_release(&dvb->dmxdev);
9960c0d06caSMauro Carvalho Chehab fail_dmxdev:
9970c0d06caSMauro Carvalho Chehab 	dvb_dmx_release(&dvb->demux);
9980c0d06caSMauro Carvalho Chehab fail_dmx:
9990c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
10000c0d06caSMauro Carvalho Chehab 		dvb_unregister_frontend(dvb->fe[1]);
10010c0d06caSMauro Carvalho Chehab 	dvb_unregister_frontend(dvb->fe[0]);
10020c0d06caSMauro Carvalho Chehab fail_frontend1:
10030c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
10040c0d06caSMauro Carvalho Chehab 		dvb_frontend_detach(dvb->fe[1]);
10050c0d06caSMauro Carvalho Chehab fail_frontend0:
10060c0d06caSMauro Carvalho Chehab 	dvb_frontend_detach(dvb->fe[0]);
10070c0d06caSMauro Carvalho Chehab 	dvb_unregister_adapter(&dvb->adapter);
10080c0d06caSMauro Carvalho Chehab fail_adapter:
10090c0d06caSMauro Carvalho Chehab 	return result;
10100c0d06caSMauro Carvalho Chehab }
10110c0d06caSMauro Carvalho Chehab 
10120c0d06caSMauro Carvalho Chehab static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
10130c0d06caSMauro Carvalho Chehab {
10140c0d06caSMauro Carvalho Chehab 	dvb_net_release(&dvb->net);
10150c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
10160c0d06caSMauro Carvalho Chehab 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
10170c0d06caSMauro Carvalho Chehab 	dvb_dmxdev_release(&dvb->dmxdev);
10180c0d06caSMauro Carvalho Chehab 	dvb_dmx_release(&dvb->demux);
10190c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
10200c0d06caSMauro Carvalho Chehab 		dvb_unregister_frontend(dvb->fe[1]);
10210c0d06caSMauro Carvalho Chehab 	dvb_unregister_frontend(dvb->fe[0]);
10220c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1] && !dvb->dont_attach_fe1)
10230c0d06caSMauro Carvalho Chehab 		dvb_frontend_detach(dvb->fe[1]);
10240c0d06caSMauro Carvalho Chehab 	dvb_frontend_detach(dvb->fe[0]);
10250c0d06caSMauro Carvalho Chehab 	dvb_unregister_adapter(&dvb->adapter);
10260c0d06caSMauro Carvalho Chehab }
10270c0d06caSMauro Carvalho Chehab 
10280c0d06caSMauro Carvalho Chehab static int em28xx_dvb_init(struct em28xx *dev)
10290c0d06caSMauro Carvalho Chehab {
103013a5336eSAntti Palosaari 	int result = 0, mfe_shared = 0;
10310c0d06caSMauro Carvalho Chehab 	struct em28xx_dvb *dvb;
10320c0d06caSMauro Carvalho Chehab 
1033822b8deaSMauro Carvalho Chehab 	if (dev->is_audio_only) {
1034822b8deaSMauro Carvalho Chehab 		/* Shouldn't initialize IR for this interface */
1035822b8deaSMauro Carvalho Chehab 		return 0;
1036822b8deaSMauro Carvalho Chehab 	}
1037822b8deaSMauro Carvalho Chehab 
10380c0d06caSMauro Carvalho Chehab 	if (!dev->board.has_dvb) {
10390c0d06caSMauro Carvalho Chehab 		/* This device does not support the extension */
10400c0d06caSMauro Carvalho Chehab 		return 0;
10410c0d06caSMauro Carvalho Chehab 	}
10420c0d06caSMauro Carvalho Chehab 
10439634614fSMauro Carvalho Chehab 	em28xx_info("Binding DVB extension\n");
10449634614fSMauro Carvalho Chehab 
10450c0d06caSMauro Carvalho Chehab 	dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
10460c0d06caSMauro Carvalho Chehab 	if (dvb == NULL) {
10470c0d06caSMauro Carvalho Chehab 		em28xx_info("em28xx_dvb: memory allocation failed\n");
10480c0d06caSMauro Carvalho Chehab 		return -ENOMEM;
10490c0d06caSMauro Carvalho Chehab 	}
10500c0d06caSMauro Carvalho Chehab 	dev->dvb = dvb;
10510c0d06caSMauro Carvalho Chehab 	dvb->fe[0] = dvb->fe[1] = NULL;
10520c0d06caSMauro Carvalho Chehab 
105327ba0dacSFrank Schaefer 	/* pre-allocate DVB usb transfer buffers */
105427ba0dacSFrank Schaefer 	if (dev->dvb_xfer_bulk) {
105527ba0dacSFrank Schaefer 		result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
105627ba0dacSFrank Schaefer 					   dev->dvb_xfer_bulk,
105727ba0dacSFrank Schaefer 					   EM28XX_DVB_NUM_BUFS,
105827ba0dacSFrank Schaefer 					   512,
105927ba0dacSFrank Schaefer 					   EM28XX_DVB_BULK_PACKET_MULTIPLIER);
106027ba0dacSFrank Schaefer 	} else {
106127ba0dacSFrank Schaefer 		result = em28xx_alloc_urbs(dev, EM28XX_DIGITAL_MODE,
106227ba0dacSFrank Schaefer 					   dev->dvb_xfer_bulk,
106327ba0dacSFrank Schaefer 					   EM28XX_DVB_NUM_BUFS,
106427ba0dacSFrank Schaefer 					   dev->dvb_max_pkt_size_isoc,
106527ba0dacSFrank Schaefer 					   EM28XX_DVB_NUM_ISOC_PACKETS);
106627ba0dacSFrank Schaefer 	}
106727ba0dacSFrank Schaefer 	if (result) {
106827ba0dacSFrank Schaefer 		em28xx_errdev("em28xx_dvb: failed to pre-allocate USB transfer buffers for DVB.\n");
106927ba0dacSFrank Schaefer 		kfree(dvb);
107027ba0dacSFrank Schaefer 		dev->dvb = NULL;
107127ba0dacSFrank Schaefer 		return result;
107227ba0dacSFrank Schaefer 	}
107327ba0dacSFrank Schaefer 
10740c0d06caSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
10750c0d06caSMauro Carvalho Chehab 	em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
10760c0d06caSMauro Carvalho Chehab 	/* init frontend */
10770c0d06caSMauro Carvalho Chehab 	switch (dev->model) {
10780c0d06caSMauro Carvalho Chehab 	case EM2874_BOARD_LEADERSHIP_ISDBT:
10790c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(s921_attach,
1080c7a45e5bSMauro Carvalho Chehab 				&sharp_isdbt, &dev->i2c_adap[dev->def_i2c_bus]);
10810c0d06caSMauro Carvalho Chehab 
10820c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
10830c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
10840c0d06caSMauro Carvalho Chehab 			goto out_free;
10850c0d06caSMauro Carvalho Chehab 		}
10860c0d06caSMauro Carvalho Chehab 
10870c0d06caSMauro Carvalho Chehab 		break;
10880c0d06caSMauro Carvalho Chehab 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
10890c0d06caSMauro Carvalho Chehab 	case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
10900c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
10910c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
10920c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
10930c0d06caSMauro Carvalho Chehab 					   &em2880_lgdt3303_dev,
1094c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
10950c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
10960c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
10970c0d06caSMauro Carvalho Chehab 			goto out_free;
10980c0d06caSMauro Carvalho Chehab 		}
10990c0d06caSMauro Carvalho Chehab 		break;
11000c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_KWORLD_DVB_310U:
11010c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
11020c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_with_xc3028,
1103c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
11040c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
11050c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
11060c0d06caSMauro Carvalho Chehab 			goto out_free;
11070c0d06caSMauro Carvalho Chehab 		}
11080c0d06caSMauro Carvalho Chehab 		break;
11090c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
11100c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_TERRATEC_HYBRID_XS:
11110c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_EMPIRE_DUAL_TV:
11120c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
11130c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_xc3028_no_i2c_gate,
1114c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
11150c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
11160c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
11170c0d06caSMauro Carvalho Chehab 			goto out_free;
11180c0d06caSMauro Carvalho Chehab 		}
11190c0d06caSMauro Carvalho Chehab 		break;
11200c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_TERRATEC_HYBRID_XS:
11210c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
11220c0d06caSMauro Carvalho Chehab 	case EM2881_BOARD_PINNACLE_HYBRID_PRO:
11230c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_DIKOM_DK300:
11240c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_KWORLD_VS_DVBT:
11250c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
11260c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_xc3028_no_i2c_gate,
1127c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
11280c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] == NULL) {
11290c0d06caSMauro Carvalho Chehab 			/* This board could have either a zl10353 or a mt352.
11300c0d06caSMauro Carvalho Chehab 			   If the chip id isn't for zl10353, try mt352 */
11310c0d06caSMauro Carvalho Chehab 			dvb->fe[0] = dvb_attach(mt352_attach,
11320c0d06caSMauro Carvalho Chehab 						   &terratec_xs_mt352_cfg,
1133c7a45e5bSMauro Carvalho Chehab 						   &dev->i2c_adap[dev->def_i2c_bus]);
11340c0d06caSMauro Carvalho Chehab 		}
11350c0d06caSMauro Carvalho Chehab 
11360c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
11370c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
11380c0d06caSMauro Carvalho Chehab 			goto out_free;
11390c0d06caSMauro Carvalho Chehab 		}
11400c0d06caSMauro Carvalho Chehab 		break;
11410c0d06caSMauro Carvalho Chehab 	case EM2870_BOARD_KWORLD_355U:
11420c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(zl10353_attach,
11430c0d06caSMauro Carvalho Chehab 					   &em28xx_zl10353_no_i2c_gate_dev,
1144c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
11450c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] != NULL)
11460c0d06caSMauro Carvalho Chehab 			dvb_attach(qt1010_attach, dvb->fe[0],
1147c7a45e5bSMauro Carvalho Chehab 				   &dev->i2c_adap[dev->def_i2c_bus], &em28xx_qt1010_config);
11480c0d06caSMauro Carvalho Chehab 		break;
11490c0d06caSMauro Carvalho Chehab 	case EM2883_BOARD_KWORLD_HYBRID_330U:
11500c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_EVGA_INDTUBE:
11510c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(s5h1409_attach,
11520c0d06caSMauro Carvalho Chehab 					   &em28xx_s5h1409_with_xc3028,
1153c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
11540c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
11550c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
11560c0d06caSMauro Carvalho Chehab 			goto out_free;
11570c0d06caSMauro Carvalho Chehab 		}
11580c0d06caSMauro Carvalho Chehab 		break;
11590c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_KWORLD_ATSC_315U:
11600c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(lgdt330x_attach,
11610c0d06caSMauro Carvalho Chehab 					   &em2880_lgdt3303_dev,
1162c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
11630c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] != NULL) {
11640c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1165c7a45e5bSMauro Carvalho Chehab 				&dev->i2c_adap[dev->def_i2c_bus], 0x61, TUNER_THOMSON_DTT761X)) {
11660c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
11670c0d06caSMauro Carvalho Chehab 				goto out_free;
11680c0d06caSMauro Carvalho Chehab 			}
11690c0d06caSMauro Carvalho Chehab 		}
11700c0d06caSMauro Carvalho Chehab 		break;
11710c0d06caSMauro Carvalho Chehab 	case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
11720c0d06caSMauro Carvalho Chehab 	case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
11730c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
1174c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus], &dev->udev->dev);
11750c0d06caSMauro Carvalho Chehab 		if (em28xx_attach_xc3028(0x61, dev) < 0) {
11760c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
11770c0d06caSMauro Carvalho Chehab 			goto out_free;
11780c0d06caSMauro Carvalho Chehab 		}
11790c0d06caSMauro Carvalho Chehab 		break;
11800c0d06caSMauro Carvalho Chehab 	case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
11810c0d06caSMauro Carvalho Chehab 		/* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
11820c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(tda10023_attach,
11830c0d06caSMauro Carvalho Chehab 			&em28xx_tda10023_config,
1184c7a45e5bSMauro Carvalho Chehab 			&dev->i2c_adap[dev->def_i2c_bus], 0x48);
11850c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
11860c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
1187c7a45e5bSMauro Carvalho Chehab 				&dev->i2c_adap[dev->def_i2c_bus], 0x60, TUNER_PHILIPS_CU1216L)) {
11880c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
11890c0d06caSMauro Carvalho Chehab 				goto out_free;
11900c0d06caSMauro Carvalho Chehab 			}
11910c0d06caSMauro Carvalho Chehab 		}
11920c0d06caSMauro Carvalho Chehab 		break;
11930c0d06caSMauro Carvalho Chehab 	case EM2870_BOARD_KWORLD_A340:
11940c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
11950c0d06caSMauro Carvalho Chehab 					   &em2870_lgdt3304_dev,
1196c7a45e5bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
11970c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0] != NULL)
11980c0d06caSMauro Carvalho Chehab 			dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1199c7a45e5bSMauro Carvalho Chehab 				   &dev->i2c_adap[dev->def_i2c_bus], &kworld_a340_config);
12000c0d06caSMauro Carvalho Chehab 		break;
12010c0d06caSMauro Carvalho Chehab 	case EM28174_BOARD_PCTV_290E:
1202a36a66d7SAntti Palosaari 		/* set default GPIO0 for LNA, used if GPIOLIB is undefined */
1203a36a66d7SAntti Palosaari 		dvb->lna_gpio = CXD2820R_GPIO_E | CXD2820R_GPIO_O |
1204a36a66d7SAntti Palosaari 				CXD2820R_GPIO_L;
12050c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(cxd2820r_attach,
12060c0d06caSMauro Carvalho Chehab 					&em28xx_cxd2820r_config,
1207c7a45e5bSMauro Carvalho Chehab 					&dev->i2c_adap[dev->def_i2c_bus],
120813a5336eSAntti Palosaari 					&dvb->lna_gpio);
12090c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
12100c0d06caSMauro Carvalho Chehab 			/* FE 0 attach tuner */
12110c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(tda18271_attach,
12120c0d06caSMauro Carvalho Chehab 					dvb->fe[0],
12130c0d06caSMauro Carvalho Chehab 					0x60,
1214c7a45e5bSMauro Carvalho Chehab 					&dev->i2c_adap[dev->def_i2c_bus],
12150c0d06caSMauro Carvalho Chehab 					&em28xx_cxd2820r_tda18271_config)) {
12160c0d06caSMauro Carvalho Chehab 
12170c0d06caSMauro Carvalho Chehab 				dvb_frontend_detach(dvb->fe[0]);
12180c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
12190c0d06caSMauro Carvalho Chehab 				goto out_free;
12200c0d06caSMauro Carvalho Chehab 			}
12211e8f31f3SAntti Palosaari 
122213a5336eSAntti Palosaari #ifdef CONFIG_GPIOLIB
12231e8f31f3SAntti Palosaari 			/* enable LNA for DVB-T, DVB-T2 and DVB-C */
12240c42a55cSAntti Palosaari 			result = gpio_request_one(dvb->lna_gpio,
12250c42a55cSAntti Palosaari 					GPIOF_OUT_INIT_LOW, NULL);
12261e8f31f3SAntti Palosaari 			if (result)
12270c42a55cSAntti Palosaari 				em28xx_errdev("gpio request failed %d\n",
12280c42a55cSAntti Palosaari 						result);
12291e8f31f3SAntti Palosaari 			else
123013a5336eSAntti Palosaari 				gpio_free(dvb->lna_gpio);
12311e8f31f3SAntti Palosaari 
12321e8f31f3SAntti Palosaari 			result = 0; /* continue even set LNA fails */
123313a5336eSAntti Palosaari #endif
123413a5336eSAntti Palosaari 			dvb->fe[0]->ops.set_lna = em28xx_pctv_290e_set_lna;
12350c42a55cSAntti Palosaari 		}
12360c42a55cSAntti Palosaari 
12370c0d06caSMauro Carvalho Chehab 		break;
12380c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
12390c0d06caSMauro Carvalho Chehab 	{
12400c0d06caSMauro Carvalho Chehab 		struct xc5000_config cfg;
12410c0d06caSMauro Carvalho Chehab 		hauppauge_hvr930c_init(dev);
12420c0d06caSMauro Carvalho Chehab 
12430c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach,
1244c7a45e5bSMauro Carvalho Chehab 					&hauppauge_930c_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
12450c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
12460c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
12470c0d06caSMauro Carvalho Chehab 			goto out_free;
12480c0d06caSMauro Carvalho Chehab 		}
12490c0d06caSMauro Carvalho Chehab 		/* FIXME: do we need a pll semaphore? */
12500c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->sec_priv = dvb;
12510c0d06caSMauro Carvalho Chehab 		sema_init(&dvb->pll_mutex, 1);
12520c0d06caSMauro Carvalho Chehab 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
12530c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
12540c0d06caSMauro Carvalho Chehab 
12550c0d06caSMauro Carvalho Chehab 		/* Attach xc5000 */
12560c0d06caSMauro Carvalho Chehab 		memset(&cfg, 0, sizeof(cfg));
12570c0d06caSMauro Carvalho Chehab 		cfg.i2c_address  = 0x61;
12580c0d06caSMauro Carvalho Chehab 		cfg.if_khz = 4000;
12590c0d06caSMauro Carvalho Chehab 
12600c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
12610c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1262c7a45e5bSMauro Carvalho Chehab 		if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
12630c0d06caSMauro Carvalho Chehab 				&cfg)) {
12640c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
12650c0d06caSMauro Carvalho Chehab 			goto out_free;
12660c0d06caSMauro Carvalho Chehab 		}
12670c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
12680c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
12690c0d06caSMauro Carvalho Chehab 
12700c0d06caSMauro Carvalho Chehab 		break;
12710c0d06caSMauro Carvalho Chehab 	}
12720c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_TERRATEC_H5:
12730c0d06caSMauro Carvalho Chehab 		terratec_h5_init(dev);
12740c0d06caSMauro Carvalho Chehab 
1275c7a45e5bSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap[dev->def_i2c_bus]);
12760c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
12770c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
12780c0d06caSMauro Carvalho Chehab 			goto out_free;
12790c0d06caSMauro Carvalho Chehab 		}
12800c0d06caSMauro Carvalho Chehab 		/* FIXME: do we need a pll semaphore? */
12810c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->sec_priv = dvb;
12820c0d06caSMauro Carvalho Chehab 		sema_init(&dvb->pll_mutex, 1);
12830c0d06caSMauro Carvalho Chehab 		dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
12840c0d06caSMauro Carvalho Chehab 		dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
12850c0d06caSMauro Carvalho Chehab 
12860c0d06caSMauro Carvalho Chehab 		/* Attach tda18271 to DVB-C frontend */
12870c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
12880c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
1289c7a45e5bSMauro Carvalho Chehab 		if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus], 0x60)) {
12900c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
12910c0d06caSMauro Carvalho Chehab 			goto out_free;
12920c0d06caSMauro Carvalho Chehab 		}
12930c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]->ops.i2c_gate_ctrl)
12940c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
12950c0d06caSMauro Carvalho Chehab 
12960c0d06caSMauro Carvalho Chehab 		break;
12974159d01bSMauro Carvalho Chehab 	case EM2884_BOARD_C3TECH_DIGITAL_DUO:
12984159d01bSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(mb86a20s_attach,
12994159d01bSMauro Carvalho Chehab 					   &c3tech_duo_mb86a20s_config,
13004159d01bSMauro Carvalho Chehab 					   &dev->i2c_adap[dev->def_i2c_bus]);
13014159d01bSMauro Carvalho Chehab 		if (dvb->fe[0] != NULL)
13024159d01bSMauro Carvalho Chehab 			dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
13034159d01bSMauro Carvalho Chehab 				   &dev->i2c_adap[dev->def_i2c_bus],
13044159d01bSMauro Carvalho Chehab 				   &c3tech_duo_tda18271_config);
13054159d01bSMauro Carvalho Chehab 		break;
13060c0d06caSMauro Carvalho Chehab 	case EM28174_BOARD_PCTV_460E:
13070c0d06caSMauro Carvalho Chehab 		/* attach demod */
13080c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(tda10071_attach,
1309c7a45e5bSMauro Carvalho Chehab 			&em28xx_tda10071_config, &dev->i2c_adap[dev->def_i2c_bus]);
13100c0d06caSMauro Carvalho Chehab 
13110c0d06caSMauro Carvalho Chehab 		/* attach SEC */
13120c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0])
1313c7a45e5bSMauro Carvalho Chehab 			dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap[dev->def_i2c_bus],
13140c0d06caSMauro Carvalho Chehab 				&em28xx_a8293_config);
13150c0d06caSMauro Carvalho Chehab 		break;
13167c1dfdb0SJakob Haufe 	case EM2874_BOARD_DELOCK_61959:
13170c0d06caSMauro Carvalho Chehab 	case EM2874_BOARD_MAXMEDIA_UB425_TC:
13180c0d06caSMauro Carvalho Chehab 		/* attach demodulator */
13190c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1320c7a45e5bSMauro Carvalho Chehab 				&dev->i2c_adap[dev->def_i2c_bus]);
13210c0d06caSMauro Carvalho Chehab 
13220c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
13230c0d06caSMauro Carvalho Chehab 			/* disable I2C-gate */
13240c0d06caSMauro Carvalho Chehab 			dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
13250c0d06caSMauro Carvalho Chehab 
13260c0d06caSMauro Carvalho Chehab 			/* attach tuner */
1327b6c7abb1SAntti Palosaari 			if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1328b6c7abb1SAntti Palosaari 					&dev->i2c_adap[dev->def_i2c_bus],
1329b6c7abb1SAntti Palosaari 					&em28xx_cxd2820r_tda18271_config)) {
13300c0d06caSMauro Carvalho Chehab 				dvb_frontend_detach(dvb->fe[0]);
13310c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
13320c0d06caSMauro Carvalho Chehab 				goto out_free;
13330c0d06caSMauro Carvalho Chehab 			}
13340c0d06caSMauro Carvalho Chehab 		}
13350c0d06caSMauro Carvalho Chehab 		break;
13360c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_PCTV_510E:
13370c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_PCTV_520E:
13380c0d06caSMauro Carvalho Chehab 		pctv_520e_init(dev);
13390c0d06caSMauro Carvalho Chehab 
13400c0d06caSMauro Carvalho Chehab 		/* attach demodulator */
13410c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1342c7a45e5bSMauro Carvalho Chehab 				&dev->i2c_adap[dev->def_i2c_bus]);
13430c0d06caSMauro Carvalho Chehab 
13440c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0]) {
13450c0d06caSMauro Carvalho Chehab 			/* attach tuner */
13460c0d06caSMauro Carvalho Chehab 			if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1347c7a45e5bSMauro Carvalho Chehab 					&dev->i2c_adap[dev->def_i2c_bus],
13480c0d06caSMauro Carvalho Chehab 					&em28xx_cxd2820r_tda18271_config)) {
13490c0d06caSMauro Carvalho Chehab 				dvb_frontend_detach(dvb->fe[0]);
13500c0d06caSMauro Carvalho Chehab 				result = -EINVAL;
13510c0d06caSMauro Carvalho Chehab 				goto out_free;
13520c0d06caSMauro Carvalho Chehab 			}
13530c0d06caSMauro Carvalho Chehab 		}
13540c0d06caSMauro Carvalho Chehab 		break;
13550c0d06caSMauro Carvalho Chehab 	case EM2884_BOARD_CINERGY_HTC_STICK:
13560c0d06caSMauro Carvalho Chehab 		terratec_htc_stick_init(dev);
13570c0d06caSMauro Carvalho Chehab 
13580c0d06caSMauro Carvalho Chehab 		/* attach demodulator */
13590c0d06caSMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1360c7a45e5bSMauro Carvalho Chehab 					&dev->i2c_adap[dev->def_i2c_bus]);
13610c0d06caSMauro Carvalho Chehab 		if (!dvb->fe[0]) {
13620c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
13630c0d06caSMauro Carvalho Chehab 			goto out_free;
13640c0d06caSMauro Carvalho Chehab 		}
13650c0d06caSMauro Carvalho Chehab 
13660c0d06caSMauro Carvalho Chehab 		/* Attach the demodulator. */
13670c0d06caSMauro Carvalho Chehab 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1368c7a45e5bSMauro Carvalho Chehab 				&dev->i2c_adap[dev->def_i2c_bus],
13690c0d06caSMauro Carvalho Chehab 				&em28xx_cxd2820r_tda18271_config)) {
13700c0d06caSMauro Carvalho Chehab 			result = -EINVAL;
13710c0d06caSMauro Carvalho Chehab 			goto out_free;
13720c0d06caSMauro Carvalho Chehab 		}
13730c0d06caSMauro Carvalho Chehab 		break;
137489040136SMartin Blumenstingl 	case EM2884_BOARD_TERRATEC_HTC_USB_XS:
137589040136SMartin Blumenstingl 		terratec_htc_usb_xs_init(dev);
137689040136SMartin Blumenstingl 
137789040136SMartin Blumenstingl 		/* attach demodulator */
137889040136SMartin Blumenstingl 		dvb->fe[0] = dvb_attach(drxk_attach, &terratec_htc_stick_drxk,
1379c7a45e5bSMauro Carvalho Chehab 					&dev->i2c_adap[dev->def_i2c_bus]);
138089040136SMartin Blumenstingl 		if (!dvb->fe[0]) {
138189040136SMartin Blumenstingl 			result = -EINVAL;
138289040136SMartin Blumenstingl 			goto out_free;
138389040136SMartin Blumenstingl 		}
138489040136SMartin Blumenstingl 
138589040136SMartin Blumenstingl 		/* Attach the demodulator. */
138689040136SMartin Blumenstingl 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1387c7a45e5bSMauro Carvalho Chehab 				&dev->i2c_adap[dev->def_i2c_bus],
138889040136SMartin Blumenstingl 				&em28xx_cxd2820r_tda18271_config)) {
138989040136SMartin Blumenstingl 			result = -EINVAL;
139089040136SMartin Blumenstingl 			goto out_free;
139189040136SMartin Blumenstingl 		}
139289040136SMartin Blumenstingl 		break;
13936dbea9f0SJean-Francois Thibert 	case EM2874_BOARD_KWORLD_UB435Q_V2:
13946dbea9f0SJean-Francois Thibert 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
13956dbea9f0SJean-Francois Thibert 					&em2874_lgdt3305_dev,
13966dbea9f0SJean-Francois Thibert 					&dev->i2c_adap[dev->def_i2c_bus]);
13976dbea9f0SJean-Francois Thibert 		if (!dvb->fe[0]) {
13986dbea9f0SJean-Francois Thibert 			result = -EINVAL;
13996dbea9f0SJean-Francois Thibert 			goto out_free;
14006dbea9f0SJean-Francois Thibert 		}
14016dbea9f0SJean-Francois Thibert 
14026dbea9f0SJean-Francois Thibert 		/* Attach the demodulator. */
14036dbea9f0SJean-Francois Thibert 		if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
14046dbea9f0SJean-Francois Thibert 				&dev->i2c_adap[dev->def_i2c_bus],
14056dbea9f0SJean-Francois Thibert 				&kworld_ub435q_v2_config)) {
14066dbea9f0SJean-Francois Thibert 			result = -EINVAL;
14076dbea9f0SJean-Francois Thibert 			goto out_free;
14086dbea9f0SJean-Francois Thibert 		}
14096dbea9f0SJean-Francois Thibert 		break;
141002bc1f55SMauro Carvalho Chehab 	case EM2874_BOARD_KWORLD_UB435Q_V3:
141102bc1f55SMauro Carvalho Chehab 		dvb->fe[0] = dvb_attach(lgdt3305_attach,
141202bc1f55SMauro Carvalho Chehab 					&em2874_lgdt3305_nogate_dev,
141302bc1f55SMauro Carvalho Chehab 					&dev->i2c_adap[dev->def_i2c_bus]);
141402bc1f55SMauro Carvalho Chehab 		if (!dvb->fe[0]) {
141502bc1f55SMauro Carvalho Chehab 			result = -EINVAL;
141602bc1f55SMauro Carvalho Chehab 			goto out_free;
141702bc1f55SMauro Carvalho Chehab 		}
141802bc1f55SMauro Carvalho Chehab 
141902bc1f55SMauro Carvalho Chehab 		/* Attach the demodulator. */
142002bc1f55SMauro Carvalho Chehab 		if (!dvb_attach(tda18212_attach, dvb->fe[0],
142102bc1f55SMauro Carvalho Chehab 				&dev->i2c_adap[dev->def_i2c_bus],
142202bc1f55SMauro Carvalho Chehab 				&kworld_ub435q_v3_config)) {
142302bc1f55SMauro Carvalho Chehab 			result = -EINVAL;
142402bc1f55SMauro Carvalho Chehab 			goto out_free;
142502bc1f55SMauro Carvalho Chehab 		}
142602bc1f55SMauro Carvalho Chehab 		break;
142738b2df95SDevin Heitmueller 	case EM2874_BOARD_PCTV_HD_MINI_80E:
142838b2df95SDevin Heitmueller 		dvb->fe[0] = dvb_attach(drx39xxj_attach, &dev->i2c_adap[dev->def_i2c_bus]);
142938b2df95SDevin Heitmueller 		if (dvb->fe[0] != NULL) {
143038b2df95SDevin Heitmueller 			dvb->fe[0] = dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
143138b2df95SDevin Heitmueller 						&dev->i2c_adap[dev->def_i2c_bus],
143238b2df95SDevin Heitmueller 						&pinnacle_80e_dvb_config);
143338b2df95SDevin Heitmueller 			if (!dvb->fe[0]) {
143438b2df95SDevin Heitmueller 				result = -EINVAL;
143538b2df95SDevin Heitmueller 				goto out_free;
143638b2df95SDevin Heitmueller 			}
143738b2df95SDevin Heitmueller 		}
143838b2df95SDevin Heitmueller 		break;
1439ec573362SAntti Palosaari 	case EM28178_BOARD_PCTV_461E:
1440ec573362SAntti Palosaari 		{
1441ec573362SAntti Palosaari 			/* demod I2C adapter */
1442ec573362SAntti Palosaari 			struct i2c_adapter *i2c_adapter;
1443425f53aaSAntti Palosaari 			struct i2c_client *client;
1444eafa2ad6SAntti Palosaari 			struct i2c_board_info info;
1445eafa2ad6SAntti Palosaari 			struct m88ts2022_config m88ts2022_config = {
1446eafa2ad6SAntti Palosaari 				.clock = 27000000,
1447eafa2ad6SAntti Palosaari 			};
1448eafa2ad6SAntti Palosaari 			memset(&info, 0, sizeof(struct i2c_board_info));
1449ec573362SAntti Palosaari 
1450ec573362SAntti Palosaari 			/* attach demod */
1451ec573362SAntti Palosaari 			dvb->fe[0] = dvb_attach(m88ds3103_attach,
1452ec573362SAntti Palosaari 					&pctv_461e_m88ds3103_config,
1453ec573362SAntti Palosaari 					&dev->i2c_adap[dev->def_i2c_bus],
1454ec573362SAntti Palosaari 					&i2c_adapter);
1455ec573362SAntti Palosaari 			if (dvb->fe[0] == NULL) {
1456ec573362SAntti Palosaari 				result = -ENODEV;
1457ec573362SAntti Palosaari 				goto out_free;
1458ec573362SAntti Palosaari 			}
1459ec573362SAntti Palosaari 
1460ec573362SAntti Palosaari 			/* attach tuner */
1461eafa2ad6SAntti Palosaari 			m88ts2022_config.fe = dvb->fe[0];
1462eafa2ad6SAntti Palosaari 			strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE);
1463eafa2ad6SAntti Palosaari 			info.addr = 0x60;
1464eafa2ad6SAntti Palosaari 			info.platform_data = &m88ts2022_config;
1465eafa2ad6SAntti Palosaari 			request_module("m88ts2022");
1466425f53aaSAntti Palosaari 			client = i2c_new_device(i2c_adapter, &info);
1467425f53aaSAntti Palosaari 			if (client == NULL || client->dev.driver == NULL) {
1468425f53aaSAntti Palosaari 				dvb_frontend_detach(dvb->fe[0]);
1469425f53aaSAntti Palosaari 				result = -ENODEV;
1470425f53aaSAntti Palosaari 				goto out_free;
1471425f53aaSAntti Palosaari 			}
1472425f53aaSAntti Palosaari 
1473425f53aaSAntti Palosaari 			if (!try_module_get(client->dev.driver->owner)) {
1474425f53aaSAntti Palosaari 				i2c_unregister_device(client);
1475425f53aaSAntti Palosaari 				dvb_frontend_detach(dvb->fe[0]);
1476425f53aaSAntti Palosaari 				result = -ENODEV;
1477425f53aaSAntti Palosaari 				goto out_free;
1478425f53aaSAntti Palosaari 			}
1479ec573362SAntti Palosaari 
1480ec573362SAntti Palosaari 			/* delegate signal strength measurement to tuner */
1481ec573362SAntti Palosaari 			dvb->fe[0]->ops.read_signal_strength =
1482ec573362SAntti Palosaari 					dvb->fe[0]->ops.tuner_ops.get_rf_strength;
1483ec573362SAntti Palosaari 
1484ec573362SAntti Palosaari 			/* attach SEC */
1485ec573362SAntti Palosaari 			if (!dvb_attach(a8293_attach, dvb->fe[0],
1486ec573362SAntti Palosaari 					&dev->i2c_adap[dev->def_i2c_bus],
1487ec573362SAntti Palosaari 					&em28xx_a8293_config)) {
1488425f53aaSAntti Palosaari 				module_put(client->dev.driver->owner);
1489425f53aaSAntti Palosaari 				i2c_unregister_device(client);
1490ec573362SAntti Palosaari 				dvb_frontend_detach(dvb->fe[0]);
1491ec573362SAntti Palosaari 				result = -ENODEV;
1492ec573362SAntti Palosaari 				goto out_free;
1493ec573362SAntti Palosaari 			}
1494425f53aaSAntti Palosaari 
1495425f53aaSAntti Palosaari 			dvb->i2c_client_tuner = client;
1496ec573362SAntti Palosaari 		}
1497ec573362SAntti Palosaari 		break;
14980c0d06caSMauro Carvalho Chehab 	default:
14990c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: The frontend of your DVB/ATSC card"
15000c0d06caSMauro Carvalho Chehab 				" isn't supported yet\n");
15010c0d06caSMauro Carvalho Chehab 		break;
15020c0d06caSMauro Carvalho Chehab 	}
15030c0d06caSMauro Carvalho Chehab 	if (NULL == dvb->fe[0]) {
15040c0d06caSMauro Carvalho Chehab 		em28xx_errdev("/2: frontend initialization failed\n");
15050c0d06caSMauro Carvalho Chehab 		result = -EINVAL;
15060c0d06caSMauro Carvalho Chehab 		goto out_free;
15070c0d06caSMauro Carvalho Chehab 	}
15080c0d06caSMauro Carvalho Chehab 	/* define general-purpose callback pointer */
15090c0d06caSMauro Carvalho Chehab 	dvb->fe[0]->callback = em28xx_tuner_callback;
15100c0d06caSMauro Carvalho Chehab 	if (dvb->fe[1])
15110c0d06caSMauro Carvalho Chehab 		dvb->fe[1]->callback = em28xx_tuner_callback;
15120c0d06caSMauro Carvalho Chehab 
15130c0d06caSMauro Carvalho Chehab 	/* register everything */
15140c0d06caSMauro Carvalho Chehab 	result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
15150c0d06caSMauro Carvalho Chehab 
15160c0d06caSMauro Carvalho Chehab 	if (result < 0)
15170c0d06caSMauro Carvalho Chehab 		goto out_free;
15180c0d06caSMauro Carvalho Chehab 
15190c0d06caSMauro Carvalho Chehab 	/* MFE lock */
15200c0d06caSMauro Carvalho Chehab 	dvb->adapter.mfe_shared = mfe_shared;
15210c0d06caSMauro Carvalho Chehab 
15229634614fSMauro Carvalho Chehab 	em28xx_info("DVB extension successfully initialized\n");
152347677e51SMauro Carvalho Chehab 
152447677e51SMauro Carvalho Chehab 	kref_get(&dev->ref);
152547677e51SMauro Carvalho Chehab 
15260c0d06caSMauro Carvalho Chehab ret:
15270c0d06caSMauro Carvalho Chehab 	em28xx_set_mode(dev, EM28XX_SUSPEND);
15280c0d06caSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
15290c0d06caSMauro Carvalho Chehab 	return result;
15300c0d06caSMauro Carvalho Chehab 
15310c0d06caSMauro Carvalho Chehab out_free:
15320c0d06caSMauro Carvalho Chehab 	kfree(dvb);
15330c0d06caSMauro Carvalho Chehab 	dev->dvb = NULL;
15340c0d06caSMauro Carvalho Chehab 	goto ret;
15350c0d06caSMauro Carvalho Chehab }
15360c0d06caSMauro Carvalho Chehab 
15370c0d06caSMauro Carvalho Chehab static inline void prevent_sleep(struct dvb_frontend_ops *ops)
15380c0d06caSMauro Carvalho Chehab {
15390c0d06caSMauro Carvalho Chehab 	ops->set_voltage = NULL;
15400c0d06caSMauro Carvalho Chehab 	ops->sleep = NULL;
15410c0d06caSMauro Carvalho Chehab 	ops->tuner_ops.sleep = NULL;
15420c0d06caSMauro Carvalho Chehab }
15430c0d06caSMauro Carvalho Chehab 
15440c0d06caSMauro Carvalho Chehab static int em28xx_dvb_fini(struct em28xx *dev)
15450c0d06caSMauro Carvalho Chehab {
1546*b45e34f2SMauro Carvalho Chehab 	struct em28xx_dvb *dvb;
1547*b45e34f2SMauro Carvalho Chehab 	struct i2c_client *client;
1548*b45e34f2SMauro Carvalho Chehab 
1549822b8deaSMauro Carvalho Chehab 	if (dev->is_audio_only) {
1550822b8deaSMauro Carvalho Chehab 		/* Shouldn't initialize IR for this interface */
1551822b8deaSMauro Carvalho Chehab 		return 0;
1552822b8deaSMauro Carvalho Chehab 	}
1553822b8deaSMauro Carvalho Chehab 
15540c0d06caSMauro Carvalho Chehab 	if (!dev->board.has_dvb) {
15550c0d06caSMauro Carvalho Chehab 		/* This device does not support the extension */
15560c0d06caSMauro Carvalho Chehab 		return 0;
15570c0d06caSMauro Carvalho Chehab 	}
15580c0d06caSMauro Carvalho Chehab 
1559*b45e34f2SMauro Carvalho Chehab 	if (!dev->dvb)
1560*b45e34f2SMauro Carvalho Chehab 		return 0;
1561*b45e34f2SMauro Carvalho Chehab 
1562aa929ad7SMauro Carvalho Chehab 	em28xx_info("Closing DVB extension");
1563aa929ad7SMauro Carvalho Chehab 
1564*b45e34f2SMauro Carvalho Chehab 	dvb = dev->dvb;
1565*b45e34f2SMauro Carvalho Chehab 	client = dvb->i2c_client_tuner;
15660c0d06caSMauro Carvalho Chehab 
156727ba0dacSFrank Schaefer 	em28xx_uninit_usb_xfer(dev, EM28XX_DIGITAL_MODE);
156827ba0dacSFrank Schaefer 
15692665c299SFrank Schaefer 	if (dev->disconnected) {
15700c0d06caSMauro Carvalho Chehab 		/* We cannot tell the device to sleep
15710c0d06caSMauro Carvalho Chehab 		 * once it has been unplugged. */
15720c0d06caSMauro Carvalho Chehab 		if (dvb->fe[0])
15730c0d06caSMauro Carvalho Chehab 			prevent_sleep(&dvb->fe[0]->ops);
15740c0d06caSMauro Carvalho Chehab 		if (dvb->fe[1])
15750c0d06caSMauro Carvalho Chehab 			prevent_sleep(&dvb->fe[1]->ops);
15760c0d06caSMauro Carvalho Chehab 	}
15770c0d06caSMauro Carvalho Chehab 
1578425f53aaSAntti Palosaari 	/* remove I2C tuner */
1579425f53aaSAntti Palosaari 	if (client) {
1580425f53aaSAntti Palosaari 		module_put(client->dev.driver->owner);
1581425f53aaSAntti Palosaari 		i2c_unregister_device(client);
1582425f53aaSAntti Palosaari 	}
1583425f53aaSAntti Palosaari 
15840c0d06caSMauro Carvalho Chehab 	em28xx_unregister_dvb(dvb);
15850c0d06caSMauro Carvalho Chehab 	kfree(dvb);
15860c0d06caSMauro Carvalho Chehab 	dev->dvb = NULL;
158747677e51SMauro Carvalho Chehab 	kref_put(&dev->ref, em28xx_free_device);
15880c0d06caSMauro Carvalho Chehab 
15890c0d06caSMauro Carvalho Chehab 	return 0;
15900c0d06caSMauro Carvalho Chehab }
15910c0d06caSMauro Carvalho Chehab 
1592ca2b46daSShuah Khan static int em28xx_dvb_suspend(struct em28xx *dev)
1593ca2b46daSShuah Khan {
1594ca2b46daSShuah Khan 	int ret = 0;
1595ca2b46daSShuah Khan 
1596ca2b46daSShuah Khan 	if (dev->is_audio_only)
1597ca2b46daSShuah Khan 		return 0;
1598ca2b46daSShuah Khan 
1599ca2b46daSShuah Khan 	if (!dev->board.has_dvb)
1600ca2b46daSShuah Khan 		return 0;
1601ca2b46daSShuah Khan 
1602ca2b46daSShuah Khan 	em28xx_info("Suspending DVB extension");
1603ca2b46daSShuah Khan 	if (dev->dvb) {
1604ca2b46daSShuah Khan 		struct em28xx_dvb *dvb = dev->dvb;
1605ca2b46daSShuah Khan 
1606ca2b46daSShuah Khan 		if (dvb->fe[0]) {
1607ca2b46daSShuah Khan 			ret = dvb_frontend_suspend(dvb->fe[0]);
1608ca2b46daSShuah Khan 			em28xx_info("fe0 suspend %d", ret);
1609ca2b46daSShuah Khan 		}
1610ca2b46daSShuah Khan 		if (dvb->fe[1]) {
1611ca2b46daSShuah Khan 			dvb_frontend_suspend(dvb->fe[1]);
1612ca2b46daSShuah Khan 			em28xx_info("fe1 suspend %d", ret);
1613ca2b46daSShuah Khan 		}
1614ca2b46daSShuah Khan 	}
1615ca2b46daSShuah Khan 
1616ca2b46daSShuah Khan 	return 0;
1617ca2b46daSShuah Khan }
1618ca2b46daSShuah Khan 
1619ca2b46daSShuah Khan static int em28xx_dvb_resume(struct em28xx *dev)
1620ca2b46daSShuah Khan {
1621ca2b46daSShuah Khan 	int ret = 0;
1622ca2b46daSShuah Khan 
1623ca2b46daSShuah Khan 	if (dev->is_audio_only)
1624ca2b46daSShuah Khan 		return 0;
1625ca2b46daSShuah Khan 
1626ca2b46daSShuah Khan 	if (!dev->board.has_dvb)
1627ca2b46daSShuah Khan 		return 0;
1628ca2b46daSShuah Khan 
1629ca2b46daSShuah Khan 	em28xx_info("Resuming DVB extension");
1630ca2b46daSShuah Khan 	if (dev->dvb) {
1631ca2b46daSShuah Khan 		struct em28xx_dvb *dvb = dev->dvb;
1632ca2b46daSShuah Khan 
1633ca2b46daSShuah Khan 		if (dvb->fe[0]) {
1634ca2b46daSShuah Khan 			ret = dvb_frontend_resume(dvb->fe[0]);
1635ca2b46daSShuah Khan 			em28xx_info("fe0 resume %d", ret);
1636ca2b46daSShuah Khan 		}
1637ca2b46daSShuah Khan 
1638ca2b46daSShuah Khan 		if (dvb->fe[1]) {
1639ca2b46daSShuah Khan 			ret = dvb_frontend_resume(dvb->fe[1]);
1640ca2b46daSShuah Khan 			em28xx_info("fe1 resume %d", ret);
1641ca2b46daSShuah Khan 		}
1642ca2b46daSShuah Khan 	}
1643ca2b46daSShuah Khan 
1644ca2b46daSShuah Khan 	return 0;
1645ca2b46daSShuah Khan }
1646ca2b46daSShuah Khan 
16470c0d06caSMauro Carvalho Chehab static struct em28xx_ops dvb_ops = {
16480c0d06caSMauro Carvalho Chehab 	.id   = EM28XX_DVB,
16490c0d06caSMauro Carvalho Chehab 	.name = "Em28xx dvb Extension",
16500c0d06caSMauro Carvalho Chehab 	.init = em28xx_dvb_init,
16510c0d06caSMauro Carvalho Chehab 	.fini = em28xx_dvb_fini,
1652ca2b46daSShuah Khan 	.suspend = em28xx_dvb_suspend,
1653ca2b46daSShuah Khan 	.resume = em28xx_dvb_resume,
16540c0d06caSMauro Carvalho Chehab };
16550c0d06caSMauro Carvalho Chehab 
16560c0d06caSMauro Carvalho Chehab static int __init em28xx_dvb_register(void)
16570c0d06caSMauro Carvalho Chehab {
16580c0d06caSMauro Carvalho Chehab 	return em28xx_register_extension(&dvb_ops);
16590c0d06caSMauro Carvalho Chehab }
16600c0d06caSMauro Carvalho Chehab 
16610c0d06caSMauro Carvalho Chehab static void __exit em28xx_dvb_unregister(void)
16620c0d06caSMauro Carvalho Chehab {
16630c0d06caSMauro Carvalho Chehab 	em28xx_unregister_extension(&dvb_ops);
16640c0d06caSMauro Carvalho Chehab }
16650c0d06caSMauro Carvalho Chehab 
16660c0d06caSMauro Carvalho Chehab module_init(em28xx_dvb_register);
16670c0d06caSMauro Carvalho Chehab module_exit(em28xx_dvb_unregister);
1668