1 /*
2  DVB device driver for cx231xx
3 
4  Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5 		Based on em28xx driver
6 
7  This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "cx231xx.h"
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 
26 #include <media/dvbdev.h>
27 #include <media/dmxdev.h>
28 #include <media/dvb_demux.h>
29 #include <media/dvb_net.h>
30 #include <media/dvb_frontend.h>
31 #include <media/v4l2-common.h>
32 #include <media/tuner.h>
33 
34 #include "xc5000.h"
35 #include "s5h1432.h"
36 #include "tda18271.h"
37 #include "s5h1411.h"
38 #include "lgdt3305.h"
39 #include "si2165.h"
40 #include "si2168.h"
41 #include "mb86a20s.h"
42 #include "si2157.h"
43 #include "lgdt3306a.h"
44 #include "r820t.h"
45 #include "mn88473.h"
46 
47 MODULE_DESCRIPTION("driver for cx231xx based DVB cards");
48 MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
49 MODULE_LICENSE("GPL");
50 
51 static unsigned int debug;
52 module_param(debug, int, 0644);
53 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
54 
55 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
56 
57 #define CX231XX_DVB_NUM_BUFS 5
58 #define CX231XX_DVB_MAX_PACKETSIZE 564
59 #define CX231XX_DVB_MAX_PACKETS 64
60 #define CX231XX_DVB_MAX_FRONTENDS 2
61 
62 struct cx231xx_dvb {
63 	struct dvb_frontend *frontend[CX231XX_DVB_MAX_FRONTENDS];
64 
65 	/* feed count management */
66 	struct mutex lock;
67 	int nfeeds;
68 
69 	/* general boilerplate stuff */
70 	struct dvb_adapter adapter;
71 	struct dvb_demux demux;
72 	struct dmxdev dmxdev;
73 	struct dmx_frontend fe_hw;
74 	struct dmx_frontend fe_mem;
75 	struct dvb_net net;
76 	struct i2c_client *i2c_client_demod[2];
77 	struct i2c_client *i2c_client_tuner;
78 };
79 
80 static struct s5h1432_config dvico_s5h1432_config = {
81 	.output_mode   = S5H1432_SERIAL_OUTPUT,
82 	.gpio          = S5H1432_GPIO_ON,
83 	.qam_if        = S5H1432_IF_4000,
84 	.vsb_if        = S5H1432_IF_4000,
85 	.inversion     = S5H1432_INVERSION_OFF,
86 	.status_mode   = S5H1432_DEMODLOCKING,
87 	.mpeg_timing   = S5H1432_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
88 };
89 
90 static struct tda18271_std_map cnxt_rde253s_tda18271_std_map = {
91 	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
92 		      .if_lvl = 1, .rfagc_top = 0x37, },
93 	.dvbt_7   = { .if_freq = 4000, .agc_mode = 3, .std = 5,
94 		      .if_lvl = 1, .rfagc_top = 0x37, },
95 	.dvbt_8   = { .if_freq = 4000, .agc_mode = 3, .std = 6,
96 		      .if_lvl = 1, .rfagc_top = 0x37, },
97 };
98 
99 static struct tda18271_std_map mb86a20s_tda18271_config = {
100 	.dvbt_6   = { .if_freq = 4000, .agc_mode = 3, .std = 4,
101 		      .if_lvl = 0, .rfagc_top = 0x37, },
102 };
103 
104 static struct tda18271_config cnxt_rde253s_tunerconfig = {
105 	.std_map = &cnxt_rde253s_tda18271_std_map,
106 	.gate    = TDA18271_GATE_ANALOG,
107 };
108 
109 static struct s5h1411_config tda18271_s5h1411_config = {
110 	.output_mode   = S5H1411_SERIAL_OUTPUT,
111 	.gpio          = S5H1411_GPIO_OFF,
112 	.vsb_if        = S5H1411_IF_3250,
113 	.qam_if        = S5H1411_IF_4000,
114 	.inversion     = S5H1411_INVERSION_ON,
115 	.status_mode   = S5H1411_DEMODLOCKING,
116 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
117 };
118 static struct s5h1411_config xc5000_s5h1411_config = {
119 	.output_mode   = S5H1411_SERIAL_OUTPUT,
120 	.gpio          = S5H1411_GPIO_OFF,
121 	.vsb_if        = S5H1411_IF_3250,
122 	.qam_if        = S5H1411_IF_3250,
123 	.inversion     = S5H1411_INVERSION_OFF,
124 	.status_mode   = S5H1411_DEMODLOCKING,
125 	.mpeg_timing   = S5H1411_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK,
126 };
127 
128 static struct lgdt3305_config hcw_lgdt3305_config = {
129 	.i2c_addr           = 0x0e,
130 	.mpeg_mode          = LGDT3305_MPEG_SERIAL,
131 	.tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
132 	.tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
133 	.deny_i2c_rptr      = 1,
134 	.spectral_inversion = 1,
135 	.qam_if_khz         = 4000,
136 	.vsb_if_khz         = 3250,
137 };
138 
139 static struct tda18271_std_map hauppauge_tda18271_std_map = {
140 	.atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 4,
141 		      .if_lvl = 1, .rfagc_top = 0x58, },
142 	.qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 5,
143 		      .if_lvl = 1, .rfagc_top = 0x58, },
144 };
145 
146 static struct tda18271_config hcw_tda18271_config = {
147 	.std_map = &hauppauge_tda18271_std_map,
148 	.gate    = TDA18271_GATE_DIGITAL,
149 };
150 
151 static const struct mb86a20s_config pv_mb86a20s_config = {
152 	.demod_address = 0x10,
153 	.is_serial = true,
154 };
155 
156 static struct tda18271_config pv_tda18271_config = {
157 	.std_map = &mb86a20s_tda18271_config,
158 	.gate    = TDA18271_GATE_DIGITAL,
159 	.small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
160 };
161 
162 static struct lgdt3306a_config hauppauge_955q_lgdt3306a_config = {
163 	.qam_if_khz         = 4000,
164 	.vsb_if_khz         = 3250,
165 	.spectral_inversion = 1,
166 	.mpeg_mode          = LGDT3306A_MPEG_SERIAL,
167 	.tpclk_edge         = LGDT3306A_TPCLK_RISING_EDGE,
168 	.tpvalid_polarity   = LGDT3306A_TP_VALID_HIGH,
169 	.xtalMHz            = 25,
170 };
171 
172 static struct r820t_config astrometa_t2hybrid_r820t_config = {
173 	.i2c_addr		= 0x3a, /* 0x74 >> 1 */
174 	.xtal			= 16000000,
175 	.rafael_chip		= CHIP_R828D,
176 	.max_i2c_msg_len	= 2,
177 };
178 
179 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
180 {
181 	char *errmsg = "Unknown";
182 
183 	switch (status) {
184 	case -ENOENT:
185 		errmsg = "unlinked synchronously";
186 		break;
187 	case -ECONNRESET:
188 		errmsg = "unlinked asynchronously";
189 		break;
190 	case -ENOSR:
191 		errmsg = "Buffer error (overrun)";
192 		break;
193 	case -EPIPE:
194 		errmsg = "Stalled (device not responding)";
195 		break;
196 	case -EOVERFLOW:
197 		errmsg = "Babble (bad cable?)";
198 		break;
199 	case -EPROTO:
200 		errmsg = "Bit-stuff error (bad cable?)";
201 		break;
202 	case -EILSEQ:
203 		errmsg = "CRC/Timeout (could be anything)";
204 		break;
205 	case -ETIME:
206 		errmsg = "Device does not respond";
207 		break;
208 	}
209 	if (packet < 0) {
210 		dev_dbg(dev->dev,
211 			"URB status %d [%s].\n", status, errmsg);
212 	} else {
213 		dev_dbg(dev->dev,
214 			"URB packet %d, status %d [%s].\n",
215 			packet, status, errmsg);
216 	}
217 }
218 
219 static inline int dvb_isoc_copy(struct cx231xx *dev, struct urb *urb)
220 {
221 	int i;
222 
223 	if (!dev)
224 		return 0;
225 
226 	if (dev->state & DEV_DISCONNECTED)
227 		return 0;
228 
229 	if (urb->status < 0) {
230 		print_err_status(dev, -1, urb->status);
231 		if (urb->status == -ENOENT)
232 			return 0;
233 	}
234 
235 	for (i = 0; i < urb->number_of_packets; i++) {
236 		int status = urb->iso_frame_desc[i].status;
237 
238 		if (status < 0) {
239 			print_err_status(dev, i, status);
240 			if (urb->iso_frame_desc[i].status != -EPROTO)
241 				continue;
242 		}
243 
244 		dvb_dmx_swfilter(&dev->dvb->demux,
245 				 urb->transfer_buffer +
246 				urb->iso_frame_desc[i].offset,
247 				urb->iso_frame_desc[i].actual_length);
248 	}
249 
250 	return 0;
251 }
252 
253 static inline int dvb_bulk_copy(struct cx231xx *dev, struct urb *urb)
254 {
255 	if (!dev)
256 		return 0;
257 
258 	if (dev->state & DEV_DISCONNECTED)
259 		return 0;
260 
261 	if (urb->status < 0) {
262 		print_err_status(dev, -1, urb->status);
263 		if (urb->status == -ENOENT)
264 			return 0;
265 	}
266 
267 	/* Feed the transport payload into the kernel demux */
268 	dvb_dmx_swfilter(&dev->dvb->demux,
269 		urb->transfer_buffer, urb->actual_length);
270 
271 	return 0;
272 }
273 
274 static int start_streaming(struct cx231xx_dvb *dvb)
275 {
276 	int rc;
277 	struct cx231xx *dev = dvb->adapter.priv;
278 
279 	if (dev->USE_ISO) {
280 		dev_dbg(dev->dev, "DVB transfer mode is ISO.\n");
281 		cx231xx_set_alt_setting(dev, INDEX_TS1, 5);
282 		rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
283 		if (rc < 0)
284 			return rc;
285 		dev->mode_tv = 1;
286 		return cx231xx_init_isoc(dev, CX231XX_DVB_MAX_PACKETS,
287 					CX231XX_DVB_NUM_BUFS,
288 					dev->ts1_mode.max_pkt_size,
289 					dvb_isoc_copy);
290 	} else {
291 		dev_dbg(dev->dev, "DVB transfer mode is BULK.\n");
292 		cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
293 		rc = cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
294 		if (rc < 0)
295 			return rc;
296 		dev->mode_tv = 1;
297 		return cx231xx_init_bulk(dev, CX231XX_DVB_MAX_PACKETS,
298 					CX231XX_DVB_NUM_BUFS,
299 					dev->ts1_mode.max_pkt_size,
300 					dvb_bulk_copy);
301 	}
302 
303 }
304 
305 static int stop_streaming(struct cx231xx_dvb *dvb)
306 {
307 	struct cx231xx *dev = dvb->adapter.priv;
308 
309 	if (dev->USE_ISO)
310 		cx231xx_uninit_isoc(dev);
311 	else
312 		cx231xx_uninit_bulk(dev);
313 
314 	cx231xx_set_mode(dev, CX231XX_SUSPEND);
315 
316 	return 0;
317 }
318 
319 static int start_feed(struct dvb_demux_feed *feed)
320 {
321 	struct dvb_demux *demux = feed->demux;
322 	struct cx231xx_dvb *dvb = demux->priv;
323 	int rc, ret;
324 
325 	if (!demux->dmx.frontend)
326 		return -EINVAL;
327 
328 	mutex_lock(&dvb->lock);
329 	dvb->nfeeds++;
330 	rc = dvb->nfeeds;
331 
332 	if (dvb->nfeeds == 1) {
333 		ret = start_streaming(dvb);
334 		if (ret < 0)
335 			rc = ret;
336 	}
337 
338 	mutex_unlock(&dvb->lock);
339 	return rc;
340 }
341 
342 static int stop_feed(struct dvb_demux_feed *feed)
343 {
344 	struct dvb_demux *demux = feed->demux;
345 	struct cx231xx_dvb *dvb = demux->priv;
346 	int err = 0;
347 
348 	mutex_lock(&dvb->lock);
349 	dvb->nfeeds--;
350 
351 	if (0 == dvb->nfeeds)
352 		err = stop_streaming(dvb);
353 
354 	mutex_unlock(&dvb->lock);
355 	return err;
356 }
357 
358 /* ------------------------------------------------------------------ */
359 static int cx231xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
360 {
361 	struct cx231xx *dev = fe->dvb->priv;
362 
363 	if (acquire)
364 		return cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
365 	else
366 		return cx231xx_set_mode(dev, CX231XX_SUSPEND);
367 }
368 
369 /* ------------------------------------------------------------------ */
370 
371 static struct xc5000_config cnxt_rde250_tunerconfig = {
372 	.i2c_address = 0x61,
373 	.if_khz = 4000,
374 };
375 static struct xc5000_config cnxt_rdu250_tunerconfig = {
376 	.i2c_address = 0x61,
377 	.if_khz = 3250,
378 };
379 
380 /* ------------------------------------------------------------------ */
381 #if 0
382 static int attach_xc5000(u8 addr, struct cx231xx *dev)
383 {
384 
385 	struct dvb_frontend *fe;
386 	struct xc5000_config cfg;
387 
388 	memset(&cfg, 0, sizeof(cfg));
389 	cfg.i2c_adap = cx231xx_get_i2c_adap(dev, dev->board.tuner_i2c_master);
390 	cfg.i2c_addr = addr;
391 
392 	if (!dev->dvb->frontend[0]) {
393 		dev_err(dev->dev, "%s/2: dvb frontend not attached. Can't attach xc5000\n",
394 			dev->name);
395 		return -EINVAL;
396 	}
397 
398 	fe = dvb_attach(xc5000_attach, dev->dvb->frontend[0], &cfg);
399 	if (!fe) {
400 		dev_err(dev->dev, "%s/2: xc5000 attach failed\n", dev->name);
401 		dvb_frontend_detach(dev->dvb->frontend[0]);
402 		dev->dvb->frontend[0] = NULL;
403 		return -EINVAL;
404 	}
405 
406 	dev_info(dev->dev, "%s/2: xc5000 attached\n", dev->name);
407 
408 	return 0;
409 }
410 #endif
411 
412 int cx231xx_set_analog_freq(struct cx231xx *dev, u32 freq)
413 {
414 	if (dev->dvb && dev->dvb->frontend[0]) {
415 
416 		struct dvb_tuner_ops *dops = &dev->dvb->frontend[0]->ops.tuner_ops;
417 
418 		if (dops->set_analog_params != NULL) {
419 			struct analog_parameters params;
420 
421 			params.frequency = freq;
422 			params.std = dev->norm;
423 			params.mode = 0;	/* 0- Air; 1 - cable */
424 			/*params.audmode = ;       */
425 
426 			/* Set the analog parameters to set the frequency */
427 			dops->set_analog_params(dev->dvb->frontend[0], &params);
428 		}
429 
430 	}
431 
432 	return 0;
433 }
434 
435 int cx231xx_reset_analog_tuner(struct cx231xx *dev)
436 {
437 	int status = 0;
438 
439 	if (dev->dvb && dev->dvb->frontend[0]) {
440 
441 		struct dvb_tuner_ops *dops = &dev->dvb->frontend[0]->ops.tuner_ops;
442 
443 		if (dops->init != NULL && !dev->xc_fw_load_done) {
444 
445 			dev_dbg(dev->dev,
446 				"Reloading firmware for XC5000\n");
447 			status = dops->init(dev->dvb->frontend[0]);
448 			if (status == 0) {
449 				dev->xc_fw_load_done = 1;
450 				dev_dbg(dev->dev,
451 					"XC5000 firmware download completed\n");
452 			} else {
453 				dev->xc_fw_load_done = 0;
454 				dev_dbg(dev->dev,
455 					"XC5000 firmware download failed !!!\n");
456 			}
457 		}
458 
459 	}
460 
461 	return status;
462 }
463 
464 /* ------------------------------------------------------------------ */
465 
466 static int register_dvb(struct cx231xx_dvb *dvb,
467 			struct module *module,
468 			struct cx231xx *dev, struct device *device)
469 {
470 	int result;
471 
472 	mutex_init(&dvb->lock);
473 
474 
475 	/* register adapter */
476 	result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
477 				      adapter_nr);
478 	if (result < 0) {
479 		dev_warn(dev->dev,
480 		       "%s: dvb_register_adapter failed (errno = %d)\n",
481 		       dev->name, result);
482 		goto fail_adapter;
483 	}
484 	dvb_register_media_controller(&dvb->adapter, dev->media_dev);
485 
486 	/* Ensure all frontends negotiate bus access */
487 	dvb->frontend[0]->ops.ts_bus_ctrl = cx231xx_dvb_bus_ctrl;
488 	if (dvb->frontend[1])
489 		dvb->frontend[1]->ops.ts_bus_ctrl = cx231xx_dvb_bus_ctrl;
490 
491 	dvb->adapter.priv = dev;
492 
493 	/* register frontend */
494 	result = dvb_register_frontend(&dvb->adapter, dvb->frontend[0]);
495 	if (result < 0) {
496 		dev_warn(dev->dev,
497 		       "%s: dvb_register_frontend failed (errno = %d)\n",
498 		       dev->name, result);
499 		goto fail_frontend0;
500 	}
501 
502 	if (dvb->frontend[1]) {
503 		result = dvb_register_frontend(&dvb->adapter, dvb->frontend[1]);
504 		if (result < 0) {
505 			dev_warn(dev->dev,
506 				 "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
507 				dev->name, result);
508 			goto fail_frontend1;
509 		}
510 
511 		/* MFE lock */
512 		dvb->adapter.mfe_shared = 1;
513 	}
514 
515 	/* register demux stuff */
516 	dvb->demux.dmx.capabilities =
517 	    DMX_TS_FILTERING | DMX_SECTION_FILTERING |
518 	    DMX_MEMORY_BASED_FILTERING;
519 	dvb->demux.priv = dvb;
520 	dvb->demux.filternum = 256;
521 	dvb->demux.feednum = 256;
522 	dvb->demux.start_feed = start_feed;
523 	dvb->demux.stop_feed = stop_feed;
524 
525 	result = dvb_dmx_init(&dvb->demux);
526 	if (result < 0) {
527 		dev_warn(dev->dev,
528 			 "%s: dvb_dmx_init failed (errno = %d)\n",
529 		       dev->name, result);
530 		goto fail_dmx;
531 	}
532 
533 	dvb->dmxdev.filternum = 256;
534 	dvb->dmxdev.demux = &dvb->demux.dmx;
535 	dvb->dmxdev.capabilities = 0;
536 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
537 	if (result < 0) {
538 		dev_warn(dev->dev,
539 			 "%s: dvb_dmxdev_init failed (errno = %d)\n",
540 			 dev->name, result);
541 		goto fail_dmxdev;
542 	}
543 
544 	dvb->fe_hw.source = DMX_FRONTEND_0;
545 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
546 	if (result < 0) {
547 		dev_warn(dev->dev,
548 		       "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
549 		       dev->name, result);
550 		goto fail_fe_hw;
551 	}
552 
553 	dvb->fe_mem.source = DMX_MEMORY_FE;
554 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
555 	if (result < 0) {
556 		dev_warn(dev->dev,
557 			 "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
558 			 dev->name, result);
559 		goto fail_fe_mem;
560 	}
561 
562 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
563 	if (result < 0) {
564 		dev_warn(dev->dev,
565 			 "%s: connect_frontend failed (errno = %d)\n",
566 			 dev->name, result);
567 		goto fail_fe_conn;
568 	}
569 
570 	/* register network adapter */
571 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
572 	result = dvb_create_media_graph(&dvb->adapter,
573 					dev->tuner_type == TUNER_ABSENT);
574 	if (result < 0)
575 		goto fail_create_graph;
576 
577 	return 0;
578 
579 fail_create_graph:
580 	dvb_net_release(&dvb->net);
581 fail_fe_conn:
582 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
583 fail_fe_mem:
584 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
585 fail_fe_hw:
586 	dvb_dmxdev_release(&dvb->dmxdev);
587 fail_dmxdev:
588 	dvb_dmx_release(&dvb->demux);
589 fail_dmx:
590 	if (dvb->frontend[1])
591 		dvb_unregister_frontend(dvb->frontend[1]);
592 	dvb_unregister_frontend(dvb->frontend[0]);
593 fail_frontend1:
594 	if (dvb->frontend[1])
595 		dvb_frontend_detach(dvb->frontend[1]);
596 fail_frontend0:
597 	dvb_frontend_detach(dvb->frontend[0]);
598 	dvb_unregister_adapter(&dvb->adapter);
599 fail_adapter:
600 	return result;
601 }
602 
603 static void unregister_dvb(struct cx231xx_dvb *dvb)
604 {
605 	dvb_net_release(&dvb->net);
606 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
607 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
608 	dvb_dmxdev_release(&dvb->dmxdev);
609 	dvb_dmx_release(&dvb->demux);
610 	if (dvb->frontend[1])
611 		dvb_unregister_frontend(dvb->frontend[1]);
612 	dvb_unregister_frontend(dvb->frontend[0]);
613 	if (dvb->frontend[1])
614 		dvb_frontend_detach(dvb->frontend[1]);
615 	dvb_frontend_detach(dvb->frontend[0]);
616 	dvb_unregister_adapter(&dvb->adapter);
617 
618 	/* remove I2C tuner */
619 	dvb_module_release(dvb->i2c_client_tuner);
620 	dvb->i2c_client_tuner = NULL;
621 	/* remove I2C demod(s) */
622 	dvb_module_release(dvb->i2c_client_demod[1]);
623 	dvb->i2c_client_demod[1] = NULL;
624 	dvb_module_release(dvb->i2c_client_demod[0]);
625 	dvb->i2c_client_demod[0] = NULL;
626 }
627 
628 static int dvb_init(struct cx231xx *dev)
629 {
630 	int result;
631 	struct cx231xx_dvb *dvb;
632 	struct i2c_adapter *tuner_i2c;
633 	struct i2c_adapter *demod_i2c;
634 	struct i2c_client *client;
635 	struct i2c_adapter *adapter;
636 
637 	if (!dev->board.has_dvb) {
638 		/* This device does not support the extension */
639 		return 0;
640 	}
641 
642 	dvb = kzalloc(sizeof(struct cx231xx_dvb), GFP_KERNEL);
643 
644 	if (dvb == NULL) {
645 		dev_info(dev->dev,
646 			 "cx231xx_dvb: memory allocation failed\n");
647 		return -ENOMEM;
648 	}
649 	dev->dvb = dvb;
650 	dev->cx231xx_set_analog_freq = cx231xx_set_analog_freq;
651 	dev->cx231xx_reset_analog_tuner = cx231xx_reset_analog_tuner;
652 
653 	tuner_i2c = cx231xx_get_i2c_adap(dev, dev->board.tuner_i2c_master);
654 	demod_i2c = cx231xx_get_i2c_adap(dev, dev->board.demod_i2c_master);
655 	mutex_lock(&dev->lock);
656 	cx231xx_set_mode(dev, CX231XX_DIGITAL_MODE);
657 	cx231xx_demod_reset(dev);
658 	/* init frontend */
659 	switch (dev->model) {
660 	case CX231XX_BOARD_CNXT_CARRAERA:
661 	case CX231XX_BOARD_CNXT_RDE_250:
662 
663 		dev->dvb->frontend[0] = dvb_attach(s5h1432_attach,
664 					&dvico_s5h1432_config,
665 					demod_i2c);
666 
667 		if (!dev->dvb->frontend[0]) {
668 			dev_err(dev->dev,
669 				"Failed to attach s5h1432 front end\n");
670 			result = -EINVAL;
671 			goto out_free;
672 		}
673 
674 		/* define general-purpose callback pointer */
675 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
676 
677 		if (!dvb_attach(xc5000_attach, dev->dvb->frontend[0],
678 			       tuner_i2c,
679 			       &cnxt_rde250_tunerconfig)) {
680 			result = -EINVAL;
681 			goto out_free;
682 		}
683 
684 		break;
685 	case CX231XX_BOARD_CNXT_SHELBY:
686 	case CX231XX_BOARD_CNXT_RDU_250:
687 
688 		dev->dvb->frontend[0] = dvb_attach(s5h1411_attach,
689 					       &xc5000_s5h1411_config,
690 					       demod_i2c);
691 
692 		if (!dev->dvb->frontend[0]) {
693 			dev_err(dev->dev,
694 				"Failed to attach s5h1411 front end\n");
695 			result = -EINVAL;
696 			goto out_free;
697 		}
698 
699 		/* define general-purpose callback pointer */
700 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
701 
702 		if (!dvb_attach(xc5000_attach, dev->dvb->frontend[0],
703 			       tuner_i2c,
704 			       &cnxt_rdu250_tunerconfig)) {
705 			result = -EINVAL;
706 			goto out_free;
707 		}
708 		break;
709 	case CX231XX_BOARD_CNXT_RDE_253S:
710 
711 		dev->dvb->frontend[0] = dvb_attach(s5h1432_attach,
712 					&dvico_s5h1432_config,
713 					demod_i2c);
714 
715 		if (!dev->dvb->frontend[0]) {
716 			dev_err(dev->dev,
717 				"Failed to attach s5h1432 front end\n");
718 			result = -EINVAL;
719 			goto out_free;
720 		}
721 
722 		/* define general-purpose callback pointer */
723 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
724 
725 		if (!dvb_attach(tda18271_attach, dev->dvb->frontend[0],
726 			       dev->board.tuner_addr, tuner_i2c,
727 			       &cnxt_rde253s_tunerconfig)) {
728 			result = -EINVAL;
729 			goto out_free;
730 		}
731 		break;
732 	case CX231XX_BOARD_CNXT_RDU_253S:
733 	case CX231XX_BOARD_KWORLD_UB445_USB_HYBRID:
734 
735 		dev->dvb->frontend[0] = dvb_attach(s5h1411_attach,
736 					       &tda18271_s5h1411_config,
737 					       demod_i2c);
738 
739 		if (!dev->dvb->frontend[0]) {
740 			dev_err(dev->dev,
741 				"Failed to attach s5h1411 front end\n");
742 			result = -EINVAL;
743 			goto out_free;
744 		}
745 
746 		/* define general-purpose callback pointer */
747 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
748 
749 		if (!dvb_attach(tda18271_attach, dev->dvb->frontend[0],
750 			       dev->board.tuner_addr, tuner_i2c,
751 			       &cnxt_rde253s_tunerconfig)) {
752 			result = -EINVAL;
753 			goto out_free;
754 		}
755 		break;
756 	case CX231XX_BOARD_HAUPPAUGE_EXETER:
757 
758 		dev_info(dev->dev,
759 			 "%s: looking for tuner / demod on i2c bus: %d\n",
760 		       __func__, i2c_adapter_id(tuner_i2c));
761 
762 		dev->dvb->frontend[0] = dvb_attach(lgdt3305_attach,
763 						&hcw_lgdt3305_config,
764 						demod_i2c);
765 
766 		if (!dev->dvb->frontend[0]) {
767 			dev_err(dev->dev,
768 				"Failed to attach LG3305 front end\n");
769 			result = -EINVAL;
770 			goto out_free;
771 		}
772 
773 		/* define general-purpose callback pointer */
774 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
775 
776 		dvb_attach(tda18271_attach, dev->dvb->frontend[0],
777 			   dev->board.tuner_addr, tuner_i2c,
778 			   &hcw_tda18271_config);
779 		break;
780 
781 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
782 	{
783 		struct si2165_platform_data si2165_pdata = {};
784 
785 		/* attach demod */
786 		si2165_pdata.fe = &dev->dvb->frontend[0];
787 		si2165_pdata.chip_mode = SI2165_MODE_PLL_XTAL;
788 		si2165_pdata.ref_freq_hz = 16000000;
789 
790 		/* perform probe/init/attach */
791 		client = dvb_module_probe("si2165", NULL, demod_i2c,
792 						dev->board.demod_addr,
793 						&si2165_pdata);
794 		if (!client) {
795 			result = -ENODEV;
796 			goto out_free;
797 		}
798 		dvb->i2c_client_demod[0] = client;
799 
800 		dev->dvb->frontend[0]->ops.i2c_gate_ctrl = NULL;
801 
802 		/* define general-purpose callback pointer */
803 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
804 
805 		dvb_attach(tda18271_attach, dev->dvb->frontend[0],
806 			dev->board.tuner_addr, tuner_i2c,
807 			&hcw_tda18271_config);
808 
809 		dev->cx231xx_reset_analog_tuner = NULL;
810 		break;
811 	}
812 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
813 	{
814 		struct si2165_platform_data si2165_pdata = {};
815 		struct si2157_config si2157_config = {};
816 
817 		/* attach demod */
818 		si2165_pdata.fe = &dev->dvb->frontend[0];
819 		si2165_pdata.chip_mode = SI2165_MODE_PLL_EXT;
820 		si2165_pdata.ref_freq_hz = 24000000;
821 
822 		/* perform probe/init/attach */
823 		client = dvb_module_probe("si2165", NULL, demod_i2c,
824 						dev->board.demod_addr,
825 						&si2165_pdata);
826 		if (!client) {
827 			result = -ENODEV;
828 			goto out_free;
829 		}
830 		dvb->i2c_client_demod[0] = client;
831 
832 		dev->dvb->frontend[0]->ops.i2c_gate_ctrl = NULL;
833 
834 		/* define general-purpose callback pointer */
835 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
836 
837 		/* attach tuner */
838 		si2157_config.fe = dev->dvb->frontend[0];
839 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
840 		si2157_config.mdev = dev->media_dev;
841 #endif
842 		si2157_config.if_port = 1;
843 		si2157_config.inversion = true;
844 
845 		/* perform probe/init/attach */
846 		client = dvb_module_probe("si2157", NULL, tuner_i2c,
847 						dev->board.tuner_addr,
848 						&si2157_config);
849 		if (!client) {
850 			result = -ENODEV;
851 			goto out_free;
852 		}
853 		dev->cx231xx_reset_analog_tuner = NULL;
854 
855 		dev->dvb->i2c_client_tuner = client;
856 		break;
857 	}
858 	case CX231XX_BOARD_HAUPPAUGE_955Q:
859 	{
860 		struct si2157_config si2157_config = {};
861 		struct lgdt3306a_config lgdt3306a_config = {};
862 
863 		lgdt3306a_config = hauppauge_955q_lgdt3306a_config;
864 		lgdt3306a_config.fe = &dev->dvb->frontend[0];
865 		lgdt3306a_config.i2c_adapter = &adapter;
866 
867 		/* perform probe/init/attach */
868 		client = dvb_module_probe("lgdt3306a", NULL, demod_i2c,
869 						dev->board.demod_addr,
870 						&lgdt3306a_config);
871 		if (!client) {
872 			result = -ENODEV;
873 			goto out_free;
874 		}
875 		dvb->i2c_client_demod[0] = client;
876 
877 		dev->dvb->frontend[0]->ops.i2c_gate_ctrl = NULL;
878 
879 		/* define general-purpose callback pointer */
880 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
881 
882 		/* attach tuner */
883 		si2157_config.fe = dev->dvb->frontend[0];
884 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
885 		si2157_config.mdev = dev->media_dev;
886 #endif
887 		si2157_config.if_port = 1;
888 		si2157_config.inversion = true;
889 
890 		/* perform probe/init/attach */
891 		client = dvb_module_probe("si2157", NULL, tuner_i2c,
892 						dev->board.tuner_addr,
893 						&si2157_config);
894 		if (!client) {
895 			result = -ENODEV;
896 			goto out_free;
897 		}
898 		dev->cx231xx_reset_analog_tuner = NULL;
899 
900 		dev->dvb->i2c_client_tuner = client;
901 		break;
902 	}
903 	case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
904 	case CX231XX_BOARD_KWORLD_UB430_USB_HYBRID:
905 
906 		dev_info(dev->dev,
907 			 "%s: looking for demod on i2c bus: %d\n",
908 			 __func__, i2c_adapter_id(tuner_i2c));
909 
910 		dev->dvb->frontend[0] = dvb_attach(mb86a20s_attach,
911 						&pv_mb86a20s_config,
912 						demod_i2c);
913 
914 		if (!dev->dvb->frontend[0]) {
915 			dev_err(dev->dev,
916 				"Failed to attach mb86a20s demod\n");
917 			result = -EINVAL;
918 			goto out_free;
919 		}
920 
921 		/* define general-purpose callback pointer */
922 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
923 
924 		dvb_attach(tda18271_attach, dev->dvb->frontend[0],
925 			   dev->board.tuner_addr, tuner_i2c,
926 			   &pv_tda18271_config);
927 		break;
928 
929 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
930 	{
931 		struct si2157_config si2157_config = {};
932 		struct si2168_config si2168_config = {};
933 
934 		/* attach demodulator chip */
935 		si2168_config.ts_mode = SI2168_TS_SERIAL; /* from *.inf file */
936 		si2168_config.fe = &dev->dvb->frontend[0];
937 		si2168_config.i2c_adapter = &adapter;
938 		si2168_config.ts_clock_inv = true;
939 
940 		/* perform probe/init/attach */
941 		client = dvb_module_probe("si2168", NULL, demod_i2c,
942 						dev->board.demod_addr,
943 						&si2168_config);
944 		if (!client) {
945 			result = -ENODEV;
946 			goto out_free;
947 		}
948 		dvb->i2c_client_demod[0] = client;
949 
950 		/* attach tuner chip */
951 		si2157_config.fe = dev->dvb->frontend[0];
952 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
953 		si2157_config.mdev = dev->media_dev;
954 #endif
955 		si2157_config.if_port = 1;
956 		si2157_config.inversion = false;
957 
958 		/* perform probe/init/attach */
959 		client = dvb_module_probe("si2157", NULL, tuner_i2c,
960 						dev->board.tuner_addr,
961 						&si2157_config);
962 		if (!client) {
963 			result = -ENODEV;
964 			goto out_free;
965 		}
966 		dev->cx231xx_reset_analog_tuner = NULL;
967 		dev->dvb->i2c_client_tuner = client;
968 		break;
969 	}
970 	case CX231XX_BOARD_ASTROMETA_T2HYBRID:
971 	{
972 		struct mn88473_config mn88473_config = {};
973 
974 		/* attach demodulator chip */
975 		mn88473_config.i2c_wr_max = 16;
976 		mn88473_config.xtal = 25000000;
977 		mn88473_config.fe = &dev->dvb->frontend[0];
978 
979 		/* perform probe/init/attach */
980 		client = dvb_module_probe("mn88473", NULL, demod_i2c,
981 						dev->board.demod_addr,
982 						&mn88473_config);
983 		if (!client) {
984 			result = -ENODEV;
985 			goto out_free;
986 		}
987 		dvb->i2c_client_demod[0] = client;
988 
989 		/* define general-purpose callback pointer */
990 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
991 
992 		/* attach tuner chip */
993 		dvb_attach(r820t_attach, dev->dvb->frontend[0],
994 			   tuner_i2c,
995 			   &astrometa_t2hybrid_r820t_config);
996 		break;
997 	}
998 	case CX231XX_BOARD_HAUPPAUGE_935C:
999 	{
1000 		struct si2157_config si2157_config = {};
1001 		struct si2168_config si2168_config = {};
1002 
1003 		/* attach demodulator chip */
1004 		si2168_config.ts_mode = SI2168_TS_SERIAL;
1005 		si2168_config.fe = &dev->dvb->frontend[0];
1006 		si2168_config.i2c_adapter = &adapter;
1007 		si2168_config.ts_clock_inv = true;
1008 
1009 		/* perform probe/init/attach */
1010 		client = dvb_module_probe("si2168", NULL, demod_i2c,
1011 						dev->board.demod_addr,
1012 						&si2168_config);
1013 		if (!client) {
1014 			result = -ENODEV;
1015 			goto out_free;
1016 		}
1017 		dvb->i2c_client_demod[0] = client;
1018 		dev->dvb->frontend[0]->ops.i2c_gate_ctrl = NULL;
1019 
1020 		/* define general-purpose callback pointer */
1021 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
1022 
1023 		/* attach tuner */
1024 		si2157_config.fe = dev->dvb->frontend[0];
1025 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1026 		si2157_config.mdev = dev->media_dev;
1027 #endif
1028 		si2157_config.if_port = 1;
1029 		si2157_config.inversion = true;
1030 
1031 		/* perform probe/init/attach */
1032 		client = dvb_module_probe("si2157", NULL, tuner_i2c,
1033 						dev->board.tuner_addr,
1034 						&si2157_config);
1035 		if (!client) {
1036 			result = -ENODEV;
1037 			goto out_free;
1038 		}
1039 		dev->cx231xx_reset_analog_tuner = NULL;
1040 		dev->dvb->i2c_client_tuner = client;
1041 		break;
1042 	}
1043 	case CX231XX_BOARD_HAUPPAUGE_975:
1044 	{
1045 		struct i2c_adapter *adapter2;
1046 		struct si2157_config si2157_config = {};
1047 		struct lgdt3306a_config lgdt3306a_config = {};
1048 		struct si2168_config si2168_config = {};
1049 
1050 		/* attach first demodulator chip */
1051 		lgdt3306a_config = hauppauge_955q_lgdt3306a_config;
1052 		lgdt3306a_config.fe = &dev->dvb->frontend[0];
1053 		lgdt3306a_config.i2c_adapter = &adapter;
1054 
1055 		/* perform probe/init/attach */
1056 		client = dvb_module_probe("lgdt3306a", NULL, demod_i2c,
1057 						dev->board.demod_addr,
1058 						&lgdt3306a_config);
1059 		if (!client) {
1060 			result = -ENODEV;
1061 			goto out_free;
1062 		}
1063 		dvb->i2c_client_demod[0] = client;
1064 
1065 		/* attach second demodulator chip */
1066 		si2168_config.ts_mode = SI2168_TS_SERIAL;
1067 		si2168_config.fe = &dev->dvb->frontend[1];
1068 		si2168_config.i2c_adapter = &adapter2;
1069 		si2168_config.ts_clock_inv = true;
1070 
1071 		/* perform probe/init/attach */
1072 		client = dvb_module_probe("si2168", NULL, adapter,
1073 						dev->board.demod_addr2,
1074 						&si2168_config);
1075 		if (!client) {
1076 			result = -ENODEV;
1077 			goto out_free;
1078 		}
1079 		dvb->i2c_client_demod[1] = client;
1080 		dvb->frontend[1]->id = 1;
1081 
1082 		/* define general-purpose callback pointer */
1083 		dvb->frontend[0]->callback = cx231xx_tuner_callback;
1084 		dvb->frontend[1]->callback = cx231xx_tuner_callback;
1085 
1086 		/* attach tuner */
1087 		si2157_config.fe = dev->dvb->frontend[0];
1088 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1089 		si2157_config.mdev = dev->media_dev;
1090 #endif
1091 		si2157_config.if_port = 1;
1092 		si2157_config.inversion = true;
1093 
1094 		/* perform probe/init/attach */
1095 		client = dvb_module_probe("si2157", NULL, adapter,
1096 						dev->board.tuner_addr,
1097 						&si2157_config);
1098 		if (!client) {
1099 			result = -ENODEV;
1100 			goto out_free;
1101 		}
1102 		dev->cx231xx_reset_analog_tuner = NULL;
1103 		dvb->i2c_client_tuner = client;
1104 
1105 		dvb->frontend[1]->tuner_priv = dvb->frontend[0]->tuner_priv;
1106 
1107 		memcpy(&dvb->frontend[1]->ops.tuner_ops,
1108 			&dvb->frontend[0]->ops.tuner_ops,
1109 			sizeof(struct dvb_tuner_ops));
1110 		break;
1111 	}
1112 	default:
1113 		dev_err(dev->dev,
1114 			"%s/2: The frontend of your DVB/ATSC card isn't supported yet\n",
1115 			dev->name);
1116 		break;
1117 	}
1118 	if (!dvb->frontend[0]) {
1119 		dev_err(dev->dev,
1120 		       "%s/2: frontend initialization failed\n", dev->name);
1121 		result = -EINVAL;
1122 		goto out_free;
1123 	}
1124 
1125 	/* register everything */
1126 	result = register_dvb(dvb, THIS_MODULE, dev, dev->dev);
1127 
1128 	if (result < 0)
1129 		goto out_free;
1130 
1131 
1132 	dev_info(dev->dev, "Successfully loaded cx231xx-dvb\n");
1133 
1134 ret:
1135 	cx231xx_set_mode(dev, CX231XX_SUSPEND);
1136 	mutex_unlock(&dev->lock);
1137 	return result;
1138 
1139 out_free:
1140 	/* remove I2C tuner */
1141 	dvb_module_release(dvb->i2c_client_tuner);
1142 	dvb->i2c_client_tuner = NULL;
1143 	/* remove I2C demod(s) */
1144 	dvb_module_release(dvb->i2c_client_demod[1]);
1145 	dvb->i2c_client_demod[1] = NULL;
1146 	dvb_module_release(dvb->i2c_client_demod[0]);
1147 	dvb->i2c_client_demod[0] = NULL;
1148 	kfree(dvb);
1149 	dev->dvb = NULL;
1150 	goto ret;
1151 }
1152 
1153 static int dvb_fini(struct cx231xx *dev)
1154 {
1155 	if (!dev->board.has_dvb) {
1156 		/* This device does not support the extension */
1157 		return 0;
1158 	}
1159 
1160 	if (dev->dvb) {
1161 		unregister_dvb(dev->dvb);
1162 		dev->dvb = NULL;
1163 	}
1164 
1165 	return 0;
1166 }
1167 
1168 static struct cx231xx_ops dvb_ops = {
1169 	.id = CX231XX_DVB,
1170 	.name = "Cx231xx dvb Extension",
1171 	.init = dvb_init,
1172 	.fini = dvb_fini,
1173 };
1174 
1175 static int __init cx231xx_dvb_register(void)
1176 {
1177 	return cx231xx_register_extension(&dvb_ops);
1178 }
1179 
1180 static void __exit cx231xx_dvb_unregister(void)
1181 {
1182 	cx231xx_unregister_extension(&dvb_ops);
1183 }
1184 
1185 module_init(cx231xx_dvb_register);
1186 module_exit(cx231xx_dvb_unregister);
1187