2 * dvb-dibusb-fe-i2c.c is part of the driver for mobile USB Budget DVB-T devices
3 * based on reference design made by DiBcom (http://www.dibcom.fr/)
5 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
7 * see dvb-dibusb-core.c for more copyright details.
9 * This file contains functions for attaching, initializing of an appropriate
10 * demodulator/frontend. I2C-stuff is also located here.
13 #include "dvb-dibusb.h"
15 #include <linux/usb.h>
17 int dibusb_i2c_msg(struct usb_dibusb *dib, u8 addr,
18 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
20 u8 sndbuf[wlen+4]; /* lead(1) devaddr,direction(1) addr(2) data(wlen) (len(2) (when reading)) */
22 int wo = (rbuf == NULL || rlen == 0),
23 len = 2 + wlen + (wo ? 0 : 2);
25 sndbuf[0] = wo ? DIBUSB_REQ_I2C_WRITE : DIBUSB_REQ_I2C_READ;
26 sndbuf[1] = (addr << 1) | (wo ? 0 : 1);
28 memcpy(&sndbuf[2],wbuf,wlen);
31 sndbuf[wlen+2] = (rlen >> 8) & 0xff;
32 sndbuf[wlen+3] = rlen & 0xff;
35 return dibusb_readwrite_usb(dib,sndbuf,len,rbuf,rlen);
39 * I2C master xfer function
41 static int dibusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
43 struct usb_dibusb *dib = i2c_get_adapdata(adap);
46 if (down_interruptible(&dib->i2c_sem) < 0)
50 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
52 for (i = 0; i < num; i++) {
53 /* write/read request */
54 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
55 if (dibusb_i2c_msg(dib, msg[i].addr, msg[i].buf,msg[i].len,
56 msg[i+1].buf,msg[i+1].len) < 0)
60 if (dibusb_i2c_msg(dib, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
68 static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
73 static struct i2c_algorithm dibusb_algo = {
74 .name = "DiBcom USB i2c algorithm",
76 .master_xfer = dibusb_i2c_xfer,
77 .functionality = dibusb_i2c_func,
80 static int dibusb_general_demod_init(struct dvb_frontend *fe);
81 static u8 dibusb_general_pll_addr(struct dvb_frontend *fe);
82 static int dibusb_general_pll_init(struct dvb_frontend *fe, u8 pll_buf[5]);
83 static int dibusb_general_pll_set(struct dvb_frontend *fe,
84 struct dvb_frontend_parameters* params, u8 pll_buf[5]);
86 static struct mt352_config mt352_hanftek_umt_010_config = {
87 .demod_address = 0x1e,
88 .demod_init = dibusb_general_demod_init,
89 .pll_set = dibusb_general_pll_set,
92 static int dibusb_tuner_quirk(struct usb_dibusb *dib)
94 switch (dib->dibdev->dev_cl->id) {
95 case DIBUSB1_1: /* some these device have the ENV77H11D5 and some the THOMSON CABLE */
96 case DIBUSB1_1_AN2235: { /* actually its this device, but in warm state they are indistinguishable */
97 struct dibusb_tuner *t;
98 u8 b[2] = { 0,0 } ,b2[1];
99 struct i2c_msg msg[2] = {
100 { .flags = 0, .buf = b, .len = 2 },
101 { .flags = I2C_M_RD, .buf = b2, .len = 1},
104 t = &dibusb_tuner[DIBUSB_TUNER_COFDM_PANASONIC_ENV77H11D5];
106 msg[0].addr = msg[1].addr = t->pll_addr;
108 if (dib->xfer_ops.tuner_pass_ctrl != NULL)
109 dib->xfer_ops.tuner_pass_ctrl(dib->fe,1,t->pll_addr);
110 dibusb_i2c_xfer(&dib->i2c_adap,msg,2);
111 if (dib->xfer_ops.tuner_pass_ctrl != NULL)
112 dib->xfer_ops.tuner_pass_ctrl(dib->fe,0,t->pll_addr);
115 info("this device has the Thomson Cable onboard. Which is default.");
118 info("this device has the Panasonic ENV77H11D5 onboard.");
128 /* there is a ugly pid_filter in the firmware of the umt devices, it is accessible
129 * by i2c address 0x8. Don't know how to deactivate it and how many rows it has.
131 static int dibusb_umt_pid_control(struct dvb_frontend *fe, int index, int pid, int onoff)
133 struct usb_dibusb *dib = fe->dvb->priv;
137 b[1] = (pid >> 8) & 0xff;
143 dibusb_i2c_msg(dib, 0x8, b, 3, NULL,0);
144 dibusb_set_streaming_mode(dib,0);
145 dibusb_set_streaming_mode(dib,1);
149 int dibusb_fe_init(struct usb_dibusb* dib)
151 struct dib3000_config demod_cfg;
154 if (dib->init_state & DIBUSB_STATE_I2C) {
155 for (i = 0; i < sizeof(dib->dibdev->dev_cl->demod->i2c_addrs) / sizeof(unsigned char) &&
156 dib->dibdev->dev_cl->demod->i2c_addrs[i] != 0; i++) {
158 demod_cfg.demod_address = dib->dibdev->dev_cl->demod->i2c_addrs[i];
159 demod_cfg.pll_addr = dibusb_general_pll_addr;
160 demod_cfg.pll_set = dibusb_general_pll_set;
161 demod_cfg.pll_init = dibusb_general_pll_init;
163 switch (dib->dibdev->dev_cl->demod->id) {
164 case DIBUSB_DIB3000MB:
165 dib->fe = dib3000mb_attach(&demod_cfg,&dib->i2c_adap,&dib->xfer_ops);
167 case DIBUSB_DIB3000MC:
168 dib->fe = dib3000mc_attach(&demod_cfg,&dib->i2c_adap,&dib->xfer_ops);
171 mt352_hanftek_umt_010_config.demod_address = dib->dibdev->dev_cl->demod->i2c_addrs[i];
172 dib->fe = mt352_attach(&mt352_hanftek_umt_010_config, &dib->i2c_adap);
173 dib->xfer_ops.pid_ctrl = dibusb_umt_pid_control;
176 if (dib->fe != NULL) {
177 info("found demodulator at i2c address 0x%x",dib->dibdev->dev_cl->demod->i2c_addrs[i]);
181 if (dib->fe->ops->sleep != NULL)
182 dib->fe_sleep = dib->fe->ops->sleep;
183 dib->fe->ops->sleep = dibusb_hw_sleep;
185 if (dib->fe->ops->init != NULL )
186 dib->fe_init = dib->fe->ops->init;
187 dib->fe->ops->init = dibusb_hw_wakeup;
189 /* setting the default tuner */
190 dib->tuner = dib->dibdev->dev_cl->tuner;
192 /* check which tuner is mounted on this device, in case this is unsure */
193 dibusb_tuner_quirk(dib);
195 if (dib->fe == NULL) {
196 err("A frontend driver was not found for device '%s'.",
200 if (dvb_register_frontend(dib->adapter, dib->fe)) {
201 err("Frontend registration failed.");
202 if (dib->fe->ops->release)
203 dib->fe->ops->release(dib->fe);
211 int dibusb_fe_exit(struct usb_dibusb *dib)
214 dvb_unregister_frontend(dib->fe);
218 int dibusb_i2c_init(struct usb_dibusb *dib)
222 dib->adapter->priv = dib;
224 strncpy(dib->i2c_adap.name,dib->dibdev->name,I2C_NAME_SIZE);
225 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
226 dib->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
228 dib->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
230 dib->i2c_adap.algo = &dibusb_algo;
231 dib->i2c_adap.algo_data = NULL;
232 dib->i2c_adap.id = I2C_ALGO_BIT;
234 i2c_set_adapdata(&dib->i2c_adap, dib);
236 if ((ret = i2c_add_adapter(&dib->i2c_adap)) < 0)
237 err("could not add i2c adapter");
239 dib->init_state |= DIBUSB_STATE_I2C;
244 int dibusb_i2c_exit(struct usb_dibusb *dib)
246 if (dib->init_state & DIBUSB_STATE_I2C)
247 i2c_del_adapter(&dib->i2c_adap);
248 dib->init_state &= ~DIBUSB_STATE_I2C;
253 /* pll stuff, maybe removed soon (thx to Gerd/Andrew in advance) */
254 static int thomson_cable_eu_pll_set(struct dvb_frontend_parameters *fep, u8 pllbuf[4])
256 u32 tfreq = (fep->frequency + 36125000) / 62500;
259 if (fep->frequency > 403250000)
260 vu = 1, p2 = 1, p1 = 0, p0 = 1;
261 else if (fep->frequency > 115750000)
262 vu = 0, p2 = 1, p1 = 1, p0 = 0;
263 else if (fep->frequency > 44250000)
264 vu = 0, p2 = 0, p1 = 1, p0 = 1;
268 pllbuf[0] = (tfreq >> 8) & 0x7f;
269 pllbuf[1] = tfreq & 0xff;
271 pllbuf[3] = (vu << 7) | (p2 << 2) | (p1 << 1) | p0;
275 static int panasonic_cofdm_env57h1xd5_pll_set(struct dvb_frontend_parameters *fep, u8 pllbuf[4])
277 u32 freq = fep->frequency;
278 u32 tfreq = ((freq + 36125000)*6 + 500000) / 1000000;
279 u8 TA, T210, R210, ctrl1, cp210, p4321;
280 if (freq > 858000000) {
281 err("frequency cannot be larger than 858 MHz.");
285 // contol data 1 : 1 | T/A=1 | T2,T1,T0 = 0,0,0 | R2,R1,R0 = 0,1,0
289 ctrl1 = (1 << 7) | (TA << 6) | (T210 << 3) | R210;
291 // ******** CHARGE PUMP CONFIG vs RF FREQUENCIES *****************
292 if (freq < 470000000)
293 cp210 = 2; // VHF Low and High band ch E12 to E4 to E12
294 else if (freq < 526000000)
295 cp210 = 4; // UHF band Ch E21 to E27
296 else // if (freq < 862000000)
297 cp210 = 5; // UHF band ch E28 to E69
299 //********************* BW select *******************************
300 if (freq < 153000000)
301 p4321 = 1; // BW selected for VHF low
302 else if (freq < 470000000)
303 p4321 = 2; // BW selected for VHF high E5 to E12
304 else // if (freq < 862000000)
305 p4321 = 4; // BW selection for UHF E21 to E69
307 pllbuf[0] = (tfreq >> 8) & 0xff;
308 pllbuf[1] = (tfreq >> 0) & 0xff;
309 pllbuf[2] = 0xff & ctrl1;
310 pllbuf[3] = (cp210 << 5) | (p4321);
317 * Address Byte 1 1 0 0 0 MA1 MA0 R/~W=0
319 * Program divider byte 1 0 n14 n13 n12 n11 n10 n9 n8
320 * Program divider byte 2 n7 n6 n5 n4 n3 n2 n1 n0
322 * Control byte 1 1 T/A=1 T2 T1 T0 R2 R1 R0
323 * 1 T/A=0 0 0 ATC AL2 AL1 AL0
325 * Control byte 2 CP2 CP1 CP0 BS5 BS4 BS3 BS2 BS1
327 * MA0/1 = programmable address bits
328 * R/~W = read/write bit (0 for writing)
329 * N14-0 = programmable LO frequency
331 * T/A = test AGC bit (0 = next 6 bits AGC setting,
332 * 1 = next 6 bits test and reference divider ratio settings)
334 * R2-0 = reference divider ratio and programmable frequency step
335 * ATC = AGC current setting and time constant
336 * ATC = 0: AGC current = 220nA, AGC time constant = 2s
337 * ATC = 1: AGC current = 9uA, AGC time constant = 50ms
338 * AL2-0 = AGC take-over point bits
339 * CP2-0 = charge pump current
340 * BS5-1 = PMOS ports control bits;
341 * BSn = 0 corresponding port is off, high-impedance state (at power-on)
342 * BSn = 1 corresponding port is on
346 static int panasonic_cofdm_env77h11d5_tda6650_init(struct dvb_frontend *fe, u8 pllbuf[4])
355 static int panasonic_cofdm_env77h11d5_tda6650_set (struct dvb_frontend_parameters *fep,u8 pllbuf[4])
357 int tuner_frequency = 0;
360 // determine charge pump
361 tuner_frequency = fep->frequency + 36166000;
362 if (tuner_frequency < 87000000)
364 else if (tuner_frequency < 130000000)
366 else if (tuner_frequency < 160000000)
368 else if (tuner_frequency < 200000000)
370 else if (tuner_frequency < 290000000)
372 else if (tuner_frequency < 420000000)
374 else if (tuner_frequency < 480000000)
376 else if (tuner_frequency < 620000000)
378 else if (tuner_frequency < 830000000)
380 else if (tuner_frequency < 895000000)
386 if (fep->frequency < 49000000)
388 else if (fep->frequency < 161000000)
390 else if (fep->frequency < 444000000)
392 else if (fep->frequency < 861000000)
398 switch (fep->u.ofdm.bandwidth) {
399 case BANDWIDTH_6_MHZ:
400 case BANDWIDTH_7_MHZ:
403 case BANDWIDTH_8_MHZ:
411 // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
412 tuner_frequency = (((fep->frequency / 1000) * 6) + 217496) / 1000;
414 // setup tuner buffer
415 pllbuf[0] = (tuner_frequency >> 8) & 0x7f;
416 pllbuf[1] = tuner_frequency & 0xff;
418 pllbuf[3] = (cp << 5) | (filter << 3) | band;
424 * Address Byte 1 1 0 0 0 MA1 MA0 R/~W=0
426 * Program divider byte 1 0 n14 n13 n12 n11 n10 n9 n8
427 * Program divider byte 2 n7 n6 n5 n4 n3 n2 n1 n0
429 * Control byte 1 CP T2 T1 T0 RSA RSB OS
431 * Band Switch byte X X X P4 P3 P2 P1 P0
433 * Auxiliary byte ATC AL2 AL1 AL0 0 0 0 0
435 * Address: MA1 MA0 Address
437 * 0 1 c2 (always valid)
445 static int lg_tdtp_e102p_tua6034(struct dvb_frontend_parameters* fep, u8 pllbuf[4])
450 #define TUNER_MUL 62500
452 div = (fep->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
454 if (fep->frequency < 174500000)
455 p3210 = 1; // not supported by the tdtp_e102p
456 else if (fep->frequency < 230000000) // VHF
461 if (fep->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
466 pllbuf[0] = (div >> 8) & 0x7f;
467 pllbuf[1] = div & 0xff;
469 pllbuf[3] = (p4 << 4) | p3210;
474 static int lg_tdtp_e102p_mt352_demod_init(struct dvb_frontend *fe)
476 static u8 mt352_clock_config[] = { 0x89, 0xb0, 0x2d };
477 static u8 mt352_reset[] = { 0x50, 0x80 };
478 static u8 mt352_mclk_ratio[] = { 0x8b, 0x00 };
479 static u8 mt352_adc_ctl_1_cfg[] = { 0x8E, 0x40 };
480 static u8 mt352_agc_cfg[] = { 0x67, 0x14, 0x22 };
481 static u8 mt352_sec_agc_cfg[] = { 0x69, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0x40, 0x40 };
483 static u8 mt352_unk [] = { 0xb5, 0x7a };
485 static u8 mt352_acq_ctl[] = { 0x53, 0x5f };
486 static u8 mt352_input_freq_1[] = { 0x56, 0xf1, 0x05 };
488 // static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
490 mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
492 mt352_write(fe, mt352_reset, sizeof(mt352_reset));
493 mt352_write(fe, mt352_mclk_ratio, sizeof(mt352_mclk_ratio));
495 mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
496 mt352_write(fe, mt352_agc_cfg, sizeof(mt352_agc_cfg));
498 mt352_write(fe, mt352_sec_agc_cfg, sizeof(mt352_sec_agc_cfg));
500 mt352_write(fe, mt352_unk, sizeof(mt352_unk));
502 mt352_write(fe, mt352_acq_ctl, sizeof(mt352_acq_ctl));
503 mt352_write(fe, mt352_input_freq_1, sizeof(mt352_input_freq_1));
505 // mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
510 static int dibusb_general_demod_init(struct dvb_frontend *fe)
512 struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
513 switch (dib->dibdev->dev_cl->id) {
515 return lg_tdtp_e102p_mt352_demod_init(fe);
516 default: /* other device classes do not have device specific demod inits */
522 static u8 dibusb_general_pll_addr(struct dvb_frontend *fe)
524 struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
525 return dib->tuner->pll_addr;
528 static int dibusb_pll_i2c_helper(struct usb_dibusb *dib, u8 pll_buf[5], u8 buf[4])
530 if (pll_buf == NULL) {
531 struct i2c_msg msg = {
532 .addr = dib->tuner->pll_addr,
537 if (i2c_transfer (&dib->i2c_adap, &msg, 1) != 1)
541 pll_buf[0] = dib->tuner->pll_addr << 1;
542 memcpy(&pll_buf[1],buf,4);
548 static int dibusb_general_pll_init(struct dvb_frontend *fe,
551 struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
554 switch (dib->tuner->id) {
555 case DIBUSB_TUNER_COFDM_PANASONIC_ENV77H11D5:
556 ret = panasonic_cofdm_env77h11d5_tda6650_init(fe,buf);
565 return dibusb_pll_i2c_helper(dib,pll_buf,buf);
568 static int dibusb_general_pll_set(struct dvb_frontend *fe,
569 struct dvb_frontend_parameters *fep, u8 pll_buf[5])
571 struct usb_dibusb* dib = (struct usb_dibusb*) fe->dvb->priv;
575 switch (dib->tuner->id) {
576 case DIBUSB_TUNER_CABLE_THOMSON:
577 ret = thomson_cable_eu_pll_set(fep, buf);
579 case DIBUSB_TUNER_COFDM_PANASONIC_ENV57H1XD5:
580 ret = panasonic_cofdm_env57h1xd5_pll_set(fep, buf);
582 case DIBUSB_TUNER_CABLE_LG_TDTP_E102P:
583 ret = lg_tdtp_e102p_tua6034(fep, buf);
585 case DIBUSB_TUNER_COFDM_PANASONIC_ENV77H11D5:
586 ret = panasonic_cofdm_env77h11d5_tda6650_set(fep,buf);
589 warn("no pll programming routine found for tuner %d.\n",dib->tuner->id);
597 return dibusb_pll_i2c_helper(dib,pll_buf,buf);