Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / media / dvb / frontends / it913x-fe.c
blobccc36bf2deb403b3a7e769963e0659004d34f75d
1 /*
2 * Driver for it913x-fe Frontend
4 * with support for on chip it9137 integral tuner
6 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
7 * IT9137 Copyright (C) ITE Tech Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
30 #include "dvb_frontend.h"
31 #include "it913x-fe.h"
32 #include "it913x-fe-priv.h"
34 static int it913x_debug;
36 module_param_named(debug, it913x_debug, int, 0644);
37 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
39 #define dprintk(level, args...) do { \
40 if (level & it913x_debug) \
41 printk(KERN_DEBUG "it913x-fe: " args); \
42 } while (0)
44 #define deb_info(args...) dprintk(0x01, args)
45 #define debug_data_snipet(level, name, p) \
46 dprintk(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
47 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
48 *(p+5), *(p+6), *(p+7));
49 #define info(format, arg...) \
50 printk(KERN_INFO "it913x-fe: " format "\n" , ## arg)
52 struct it913x_fe_state {
53 struct dvb_frontend frontend;
54 struct i2c_adapter *i2c_adap;
55 struct ite_config *config;
56 u8 i2c_addr;
57 u32 frequency;
58 fe_modulation_t constellation;
59 fe_transmit_mode_t transmission_mode;
60 u32 crystalFrequency;
61 u32 adcFrequency;
62 u8 tuner_type;
63 struct adctable *table;
64 fe_status_t it913x_status;
65 u16 tun_xtal;
66 u8 tun_fdiv;
67 u8 tun_clk_mode;
68 u32 tun_fn_min;
69 u32 ucblocks;
72 static int it913x_read_reg(struct it913x_fe_state *state,
73 u32 reg, u8 *data, u8 count)
75 int ret;
76 u8 pro = PRO_DMOD; /* All reads from demodulator */
77 u8 b[4];
78 struct i2c_msg msg[2] = {
79 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
80 .buf = b, .len = sizeof(b) },
81 { .addr = state->i2c_addr + (pro << 1), .flags = I2C_M_RD,
82 .buf = data, .len = count }
84 b[0] = (u8) reg >> 24;
85 b[1] = (u8)(reg >> 16) & 0xff;
86 b[2] = (u8)(reg >> 8) & 0xff;
87 b[3] = (u8) reg & 0xff;
89 ret = i2c_transfer(state->i2c_adap, msg, 2);
91 return ret;
94 static int it913x_read_reg_u8(struct it913x_fe_state *state, u32 reg)
96 int ret;
97 u8 b[1];
98 ret = it913x_read_reg(state, reg, &b[0], sizeof(b));
99 return (ret < 0) ? -ENODEV : b[0];
102 static int it913x_write(struct it913x_fe_state *state,
103 u8 pro, u32 reg, u8 buf[], u8 count)
105 u8 b[256];
106 struct i2c_msg msg[1] = {
107 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
108 .buf = b, .len = count + 4 }
110 int ret;
112 b[0] = (u8) reg >> 24;
113 b[1] = (u8)(reg >> 16) & 0xff;
114 b[2] = (u8)(reg >> 8) & 0xff;
115 b[3] = (u8) reg & 0xff;
116 memcpy(&b[4], buf, count);
118 ret = i2c_transfer(state->i2c_adap, msg, 1);
120 if (ret < 0)
121 return -EIO;
123 return 0;
126 static int it913x_write_reg(struct it913x_fe_state *state,
127 u8 pro, u32 reg, u32 data)
129 int ret;
130 u8 b[4];
131 u8 s;
133 b[0] = data >> 24;
134 b[1] = (data >> 16) & 0xff;
135 b[2] = (data >> 8) & 0xff;
136 b[3] = data & 0xff;
137 /* expand write as needed */
138 if (data < 0x100)
139 s = 3;
140 else if (data < 0x1000)
141 s = 2;
142 else if (data < 0x100000)
143 s = 1;
144 else
145 s = 0;
147 ret = it913x_write(state, pro, reg, &b[s], sizeof(b) - s);
149 return ret;
152 static int it913x_fe_script_loader(struct it913x_fe_state *state,
153 struct it913xset *loadscript)
155 int ret, i;
156 if (loadscript == NULL)
157 return -EINVAL;
159 for (i = 0; i < 1000; ++i) {
160 if (loadscript[i].pro == 0xff)
161 break;
162 ret = it913x_write(state, loadscript[i].pro,
163 loadscript[i].address,
164 loadscript[i].reg, loadscript[i].count);
165 if (ret < 0)
166 return -ENODEV;
168 return 0;
171 static int it913x_init_tuner(struct it913x_fe_state *state)
173 int ret, i, reg;
174 u8 val, nv_val;
175 u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
176 u8 b[2];
178 reg = it913x_read_reg_u8(state, 0xec86);
179 switch (reg) {
180 case 0:
181 state->tun_clk_mode = reg;
182 state->tun_xtal = 2000;
183 state->tun_fdiv = 3;
184 val = 16;
185 break;
186 case -ENODEV:
187 return -ENODEV;
188 case 1:
189 default:
190 state->tun_clk_mode = reg;
191 state->tun_xtal = 640;
192 state->tun_fdiv = 1;
193 val = 6;
194 break;
197 reg = it913x_read_reg_u8(state, 0xed03);
199 if (reg < 0)
200 return -ENODEV;
201 else if (reg < sizeof(nv))
202 nv_val = nv[reg];
203 else
204 nv_val = 2;
206 for (i = 0; i < 50; i++) {
207 ret = it913x_read_reg(state, 0xed23, &b[0], sizeof(b));
208 reg = (b[1] << 8) + b[0];
209 if (reg > 0)
210 break;
211 if (ret < 0)
212 return -ENODEV;
213 udelay(2000);
215 state->tun_fn_min = state->tun_xtal * reg;
216 state->tun_fn_min /= (state->tun_fdiv * nv_val);
217 deb_info("Tuner fn_min %d", state->tun_fn_min);
219 if (state->config->chip_ver > 1)
220 msleep(50);
221 else {
222 for (i = 0; i < 50; i++) {
223 reg = it913x_read_reg_u8(state, 0xec82);
224 if (reg > 0)
225 break;
226 if (reg < 0)
227 return -ENODEV;
228 udelay(2000);
232 return it913x_write_reg(state, PRO_DMOD, 0xed81, val);
235 static int it9137_set_tuner(struct it913x_fe_state *state,
236 u32 bandwidth, u32 frequency_m)
238 struct it913xset *set_tuner = set_it9137_template;
239 int ret, reg;
240 u32 frequency = frequency_m / 1000;
241 u32 freq, temp_f, tmp;
242 u16 iqik_m_cal;
243 u16 n_div;
244 u8 n;
245 u8 l_band;
246 u8 lna_band;
247 u8 bw;
249 if (state->config->firmware_ver == 1)
250 set_tuner = set_it9135_template;
251 else
252 set_tuner = set_it9137_template;
254 deb_info("Tuner Frequency %d Bandwidth %d", frequency, bandwidth);
256 if (frequency >= 51000 && frequency <= 440000) {
257 l_band = 0;
258 lna_band = 0;
259 } else if (frequency > 440000 && frequency <= 484000) {
260 l_band = 1;
261 lna_band = 1;
262 } else if (frequency > 484000 && frequency <= 533000) {
263 l_band = 1;
264 lna_band = 2;
265 } else if (frequency > 533000 && frequency <= 587000) {
266 l_band = 1;
267 lna_band = 3;
268 } else if (frequency > 587000 && frequency <= 645000) {
269 l_band = 1;
270 lna_band = 4;
271 } else if (frequency > 645000 && frequency <= 710000) {
272 l_band = 1;
273 lna_band = 5;
274 } else if (frequency > 710000 && frequency <= 782000) {
275 l_band = 1;
276 lna_band = 6;
277 } else if (frequency > 782000 && frequency <= 860000) {
278 l_band = 1;
279 lna_band = 7;
280 } else if (frequency > 1450000 && frequency <= 1492000) {
281 l_band = 1;
282 lna_band = 0;
283 } else if (frequency > 1660000 && frequency <= 1685000) {
284 l_band = 1;
285 lna_band = 1;
286 } else
287 return -EINVAL;
288 set_tuner[0].reg[0] = lna_band;
290 switch (bandwidth) {
291 case 5000000:
292 bw = 0;
293 break;
294 case 6000000:
295 bw = 2;
296 break;
297 case 7000000:
298 bw = 4;
299 break;
300 default:
301 case 8000000:
302 bw = 6;
303 break;
306 set_tuner[1].reg[0] = bw;
307 set_tuner[2].reg[0] = 0xa0 | (l_band << 3);
309 if (frequency > 53000 && frequency <= 74000) {
310 n_div = 48;
311 n = 0;
312 } else if (frequency > 74000 && frequency <= 111000) {
313 n_div = 32;
314 n = 1;
315 } else if (frequency > 111000 && frequency <= 148000) {
316 n_div = 24;
317 n = 2;
318 } else if (frequency > 148000 && frequency <= 222000) {
319 n_div = 16;
320 n = 3;
321 } else if (frequency > 222000 && frequency <= 296000) {
322 n_div = 12;
323 n = 4;
324 } else if (frequency > 296000 && frequency <= 445000) {
325 n_div = 8;
326 n = 5;
327 } else if (frequency > 445000 && frequency <= state->tun_fn_min) {
328 n_div = 6;
329 n = 6;
330 } else if (frequency > state->tun_fn_min && frequency <= 950000) {
331 n_div = 4;
332 n = 7;
333 } else if (frequency > 1450000 && frequency <= 1680000) {
334 n_div = 2;
335 n = 0;
336 } else
337 return -EINVAL;
339 reg = it913x_read_reg_u8(state, 0xed81);
340 iqik_m_cal = (u16)reg * n_div;
342 if (reg < 0x20) {
343 if (state->tun_clk_mode == 0)
344 iqik_m_cal = (iqik_m_cal * 9) >> 5;
345 else
346 iqik_m_cal >>= 1;
347 } else {
348 iqik_m_cal = 0x40 - iqik_m_cal;
349 if (state->tun_clk_mode == 0)
350 iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
351 else
352 iqik_m_cal = ~(iqik_m_cal >> 1);
355 temp_f = frequency * (u32)n_div * (u32)state->tun_fdiv;
356 freq = temp_f / state->tun_xtal;
357 tmp = freq * state->tun_xtal;
359 if ((temp_f - tmp) >= (state->tun_xtal >> 1))
360 freq++;
362 freq += (u32) n << 13;
363 /* Frequency OMEGA_IQIK_M_CAL_MID*/
364 temp_f = freq + (u32)iqik_m_cal;
366 set_tuner[3].reg[0] = temp_f & 0xff;
367 set_tuner[4].reg[0] = (temp_f >> 8) & 0xff;
369 deb_info("High Frequency = %04x", temp_f);
371 /* Lower frequency */
372 set_tuner[5].reg[0] = freq & 0xff;
373 set_tuner[6].reg[0] = (freq >> 8) & 0xff;
375 deb_info("low Frequency = %04x", freq);
377 ret = it913x_fe_script_loader(state, set_tuner);
379 return (ret < 0) ? -ENODEV : 0;
382 static int it913x_fe_select_bw(struct it913x_fe_state *state,
383 u32 bandwidth, u32 adcFrequency)
385 int ret, i;
386 u8 buffer[256];
387 u32 coeff[8];
388 u16 bfsfcw_fftinx_ratio;
389 u16 fftinx_bfsfcw_ratio;
390 u8 count;
391 u8 bw;
392 u8 adcmultiplier;
394 deb_info("Bandwidth %d Adc %d", bandwidth, adcFrequency);
396 switch (bandwidth) {
397 case 5000000:
398 bw = 3;
399 break;
400 case 6000000:
401 bw = 0;
402 break;
403 case 7000000:
404 bw = 1;
405 break;
406 default:
407 case 8000000:
408 bw = 2;
409 break;
411 ret = it913x_write_reg(state, PRO_DMOD, REG_BW, bw);
413 if (state->table == NULL)
414 return -EINVAL;
416 /* In write order */
417 coeff[0] = state->table[bw].coeff_1_2048;
418 coeff[1] = state->table[bw].coeff_2_2k;
419 coeff[2] = state->table[bw].coeff_1_8191;
420 coeff[3] = state->table[bw].coeff_1_8192;
421 coeff[4] = state->table[bw].coeff_1_8193;
422 coeff[5] = state->table[bw].coeff_2_8k;
423 coeff[6] = state->table[bw].coeff_1_4096;
424 coeff[7] = state->table[bw].coeff_2_4k;
425 bfsfcw_fftinx_ratio = state->table[bw].bfsfcw_fftinx_ratio;
426 fftinx_bfsfcw_ratio = state->table[bw].fftinx_bfsfcw_ratio;
428 /* ADC multiplier */
429 ret = it913x_read_reg_u8(state, ADC_X_2);
430 if (ret < 0)
431 return -EINVAL;
433 adcmultiplier = ret;
435 count = 0;
437 /* Build Buffer for COEFF Registers */
438 for (i = 0; i < 8; i++) {
439 if (adcmultiplier == 1)
440 coeff[i] /= 2;
441 buffer[count++] = (coeff[i] >> 24) & 0x3;
442 buffer[count++] = (coeff[i] >> 16) & 0xff;
443 buffer[count++] = (coeff[i] >> 8) & 0xff;
444 buffer[count++] = coeff[i] & 0xff;
447 /* bfsfcw_fftinx_ratio register 0x21-0x22 */
448 buffer[count++] = bfsfcw_fftinx_ratio & 0xff;
449 buffer[count++] = (bfsfcw_fftinx_ratio >> 8) & 0xff;
450 /* fftinx_bfsfcw_ratio register 0x23-0x24 */
451 buffer[count++] = fftinx_bfsfcw_ratio & 0xff;
452 buffer[count++] = (fftinx_bfsfcw_ratio >> 8) & 0xff;
453 /* start at COEFF_1_2048 and write through to fftinx_bfsfcw_ratio*/
454 ret = it913x_write(state, PRO_DMOD, COEFF_1_2048, buffer, count);
456 for (i = 0; i < 42; i += 8)
457 debug_data_snipet(0x1, "Buffer", &buffer[i]);
459 return ret;
464 static int it913x_fe_read_status(struct dvb_frontend *fe, fe_status_t *status)
466 struct it913x_fe_state *state = fe->demodulator_priv;
467 int ret, i;
468 fe_status_t old_status = state->it913x_status;
469 *status = 0;
471 if (state->it913x_status == 0) {
472 ret = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
473 if (ret == 0x1) {
474 *status |= FE_HAS_SIGNAL;
475 for (i = 0; i < 40; i++) {
476 ret = it913x_read_reg_u8(state, MP2IF_SYNC_LK);
477 if (ret == 0x1)
478 break;
479 msleep(25);
481 if (ret == 0x1)
482 *status |= FE_HAS_CARRIER
483 | FE_HAS_VITERBI
484 | FE_HAS_SYNC;
485 state->it913x_status = *status;
489 if (state->it913x_status & FE_HAS_SYNC) {
490 ret = it913x_read_reg_u8(state, TPSD_LOCK);
491 if (ret == 0x1)
492 *status |= FE_HAS_LOCK
493 | state->it913x_status;
494 else
495 state->it913x_status = 0;
496 if (old_status != state->it913x_status)
497 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, ret);
500 return 0;
503 static int it913x_fe_read_signal_strength(struct dvb_frontend *fe,
504 u16 *strength)
506 struct it913x_fe_state *state = fe->demodulator_priv;
507 int ret = it913x_read_reg_u8(state, SIGNAL_LEVEL);
508 /*SIGNAL_LEVEL always returns 100%! so using FE_HAS_SIGNAL as switch*/
509 if (state->it913x_status & FE_HAS_SIGNAL)
510 ret = (ret * 0xff) / 0x64;
511 else
512 ret = 0x0;
513 ret |= ret << 0x8;
514 *strength = ret;
515 return 0;
518 static int it913x_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
520 struct it913x_fe_state *state = fe->demodulator_priv;
521 int ret;
522 u8 reg[3];
523 u32 snr_val, snr_min, snr_max;
524 u32 temp;
526 ret = it913x_read_reg(state, 0x2c, reg, sizeof(reg));
528 snr_val = (u32)(reg[2] << 16) | (reg[1] << 8) | reg[0];
530 ret |= it913x_read_reg(state, 0xf78b, reg, 1);
531 if (reg[0])
532 snr_val /= reg[0];
534 if (state->transmission_mode == TRANSMISSION_MODE_2K)
535 snr_val *= 4;
536 else if (state->transmission_mode == TRANSMISSION_MODE_4K)
537 snr_val *= 2;
539 if (state->constellation == QPSK) {
540 snr_min = 0xb4711;
541 snr_max = 0x191451;
542 } else if (state->constellation == QAM_16) {
543 snr_min = 0x4f0d5;
544 snr_max = 0xc7925;
545 } else if (state->constellation == QAM_64) {
546 snr_min = 0x256d0;
547 snr_max = 0x626be;
548 } else
549 return -EINVAL;
551 if (snr_val < snr_min)
552 *snr = 0;
553 else if (snr_val < snr_max) {
554 temp = (snr_val - snr_min) >> 5;
555 temp *= 0xffff;
556 temp /= (snr_max - snr_min) >> 5;
557 *snr = (u16)temp;
558 } else
559 *snr = 0xffff;
561 return (ret < 0) ? -ENODEV : 0;
564 static int it913x_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
566 struct it913x_fe_state *state = fe->demodulator_priv;
567 int ret;
568 u8 reg[5];
569 /* Read Aborted Packets and Pre-Viterbi error rate 5 bytes */
570 ret = it913x_read_reg(state, RSD_ABORT_PKT_LSB, reg, sizeof(reg));
571 state->ucblocks += (u32)(reg[1] << 8) | reg[0];
572 *ber = (u32)(reg[4] << 16) | (reg[3] << 8) | reg[2];
573 return 0;
576 static int it913x_fe_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
578 struct it913x_fe_state *state = fe->demodulator_priv;
579 int ret;
580 u8 reg[2];
581 /* Aborted Packets */
582 ret = it913x_read_reg(state, RSD_ABORT_PKT_LSB, reg, sizeof(reg));
583 state->ucblocks += (u32)(reg[1] << 8) | reg[0];
584 *ucblocks = state->ucblocks;
585 return ret;
588 static int it913x_fe_get_frontend(struct dvb_frontend *fe)
590 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
591 struct it913x_fe_state *state = fe->demodulator_priv;
592 int ret;
593 u8 reg[8];
595 ret = it913x_read_reg(state, REG_TPSD_TX_MODE, reg, sizeof(reg));
597 if (reg[3] < 3)
598 p->modulation = fe_con[reg[3]];
600 if (reg[0] < 3)
601 p->transmission_mode = fe_mode[reg[0]];
603 if (reg[1] < 4)
604 p->guard_interval = fe_gi[reg[1]];
606 if (reg[2] < 4)
607 p->hierarchy = fe_hi[reg[2]];
609 p->code_rate_HP = (reg[6] < 6) ? fe_code[reg[6]] : FEC_NONE;
610 p->code_rate_LP = (reg[7] < 6) ? fe_code[reg[7]] : FEC_NONE;
612 /* Update internal state to reflect the autodetected props */
613 state->constellation = p->modulation;
614 state->transmission_mode = p->transmission_mode;
616 return 0;
619 static int it913x_fe_set_frontend(struct dvb_frontend *fe)
621 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
622 struct it913x_fe_state *state = fe->demodulator_priv;
623 int ret, i;
624 u8 empty_ch, last_ch;
626 state->it913x_status = 0;
628 /* Set bw*/
629 ret = it913x_fe_select_bw(state, p->bandwidth_hz,
630 state->adcFrequency);
632 /* Training Mode Off */
633 ret = it913x_write_reg(state, PRO_LINK, TRAINING_MODE, 0x0);
635 /* Clear Empty Channel */
636 ret = it913x_write_reg(state, PRO_DMOD, EMPTY_CHANNEL_STATUS, 0x0);
638 /* Clear bits */
639 ret = it913x_write_reg(state, PRO_DMOD, MP2IF_SYNC_LK, 0x0);
640 /* LED on */
641 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
642 /* Select Band*/
643 if ((p->frequency >= 51000000) && (p->frequency <= 230000000))
644 i = 0;
645 else if ((p->frequency >= 350000000) && (p->frequency <= 900000000))
646 i = 1;
647 else if ((p->frequency >= 1450000000) && (p->frequency <= 1680000000))
648 i = 2;
649 else
650 return -EOPNOTSUPP;
652 ret = it913x_write_reg(state, PRO_DMOD, FREE_BAND, i);
654 deb_info("Frontend Set Tuner Type %02x", state->tuner_type);
655 switch (state->tuner_type) {
656 case IT9135_38:
657 case IT9135_51:
658 case IT9135_52:
659 case IT9135_60:
660 case IT9135_61:
661 case IT9135_62:
662 ret = it9137_set_tuner(state,
663 p->bandwidth_hz, p->frequency);
664 break;
665 default:
666 if (fe->ops.tuner_ops.set_params) {
667 fe->ops.tuner_ops.set_params(fe);
668 if (fe->ops.i2c_gate_ctrl)
669 fe->ops.i2c_gate_ctrl(fe, 0);
671 break;
673 /* LED off */
674 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
675 /* Trigger ofsm */
676 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
677 last_ch = 2;
678 for (i = 0; i < 40; ++i) {
679 empty_ch = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
680 if (last_ch == 1 && empty_ch == 1)
681 break;
682 if (last_ch == 2 && empty_ch == 2)
683 return 0;
684 last_ch = empty_ch;
685 msleep(25);
687 for (i = 0; i < 40; ++i) {
688 if (it913x_read_reg_u8(state, D_TPSD_LOCK) == 1)
689 break;
690 msleep(25);
693 state->frequency = p->frequency;
694 return 0;
697 static int it913x_fe_suspend(struct it913x_fe_state *state)
699 int ret, i;
700 u8 b;
702 ret = it913x_write_reg(state, PRO_DMOD, SUSPEND_FLAG, 0x1);
704 ret |= it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
706 for (i = 0; i < 128; i++) {
707 ret = it913x_read_reg(state, SUSPEND_FLAG, &b, 1);
708 if (ret < 0)
709 return -ENODEV;
710 if (b == 0)
711 break;
715 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x8);
716 /* Turn LED off */
717 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
719 ret |= it913x_fe_script_loader(state, it9137_tuner_off);
721 return (ret < 0) ? -ENODEV : 0;
724 /* Power sequence */
725 /* Power Up Tuner on -> Frontend suspend off -> Tuner clk on */
726 /* Power Down Frontend suspend on -> Tuner clk off -> Tuner off */
728 static int it913x_fe_sleep(struct dvb_frontend *fe)
730 struct it913x_fe_state *state = fe->demodulator_priv;
731 return it913x_fe_suspend(state);
734 static u32 compute_div(u32 a, u32 b, u32 x)
736 u32 res = 0;
737 u32 c = 0;
738 u32 i = 0;
740 if (a > b) {
741 c = a / b;
742 a = a - c * b;
745 for (i = 0; i < x; i++) {
746 if (a >= b) {
747 res += 1;
748 a -= b;
750 a <<= 1;
751 res <<= 1;
754 res = (c << x) + res;
756 return res;
759 static int it913x_fe_start(struct it913x_fe_state *state)
761 struct it913xset *set_lna;
762 struct it913xset *set_mode;
763 int ret;
764 u8 adf = (state->config->adf & 0xf);
765 u32 adc, xtal;
766 u8 b[4];
768 if (state->config->chip_ver == 1)
769 ret = it913x_init_tuner(state);
771 info("ADF table value :%02x", adf);
773 if (adf < 10) {
774 state->crystalFrequency = fe_clockTable[adf].xtal ;
775 state->table = fe_clockTable[adf].table;
776 state->adcFrequency = state->table->adcFrequency;
778 adc = compute_div(state->adcFrequency, 1000000ul, 19ul);
779 xtal = compute_div(state->crystalFrequency, 1000000ul, 19ul);
781 } else
782 return -EINVAL;
784 /* Set LED indicator on GPIOH3 */
785 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_EN, 0x1);
786 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_ON, 0x1);
787 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
789 ret |= it913x_write_reg(state, PRO_LINK, 0xf641, state->tuner_type);
790 ret |= it913x_write_reg(state, PRO_DMOD, 0xf5ca, 0x01);
791 ret |= it913x_write_reg(state, PRO_DMOD, 0xf715, 0x01);
793 b[0] = xtal & 0xff;
794 b[1] = (xtal >> 8) & 0xff;
795 b[2] = (xtal >> 16) & 0xff;
796 b[3] = (xtal >> 24);
797 ret |= it913x_write(state, PRO_DMOD, XTAL_CLK, b , 4);
799 b[0] = adc & 0xff;
800 b[1] = (adc >> 8) & 0xff;
801 b[2] = (adc >> 16) & 0xff;
802 ret |= it913x_write(state, PRO_DMOD, ADC_FREQ, b, 3);
804 if (state->config->adc_x2)
805 ret |= it913x_write_reg(state, PRO_DMOD, ADC_X_2, 0x01);
806 b[0] = 0;
807 b[1] = 0;
808 b[2] = 0;
809 ret |= it913x_write(state, PRO_DMOD, 0x0029, b, 3);
811 info("Crystal Frequency :%d Adc Frequency :%d ADC X2: %02x",
812 state->crystalFrequency, state->adcFrequency,
813 state->config->adc_x2);
814 deb_info("Xtal value :%04x Adc value :%04x", xtal, adc);
816 if (ret < 0)
817 return -ENODEV;
819 /* v1 or v2 tuner script */
820 if (state->config->chip_ver > 1)
821 ret = it913x_fe_script_loader(state, it9135_v2);
822 else
823 ret = it913x_fe_script_loader(state, it9135_v1);
824 if (ret < 0)
825 return ret;
827 /* LNA Scripts */
828 switch (state->tuner_type) {
829 case IT9135_51:
830 set_lna = it9135_51;
831 break;
832 case IT9135_52:
833 set_lna = it9135_52;
834 break;
835 case IT9135_60:
836 set_lna = it9135_60;
837 break;
838 case IT9135_61:
839 set_lna = it9135_61;
840 break;
841 case IT9135_62:
842 set_lna = it9135_62;
843 break;
844 case IT9135_38:
845 default:
846 set_lna = it9135_38;
848 info("Tuner LNA type :%02x", state->tuner_type);
850 ret = it913x_fe_script_loader(state, set_lna);
851 if (ret < 0)
852 return ret;
854 if (state->config->chip_ver == 2) {
855 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x1);
856 ret |= it913x_write_reg(state, PRO_LINK, PADODPU, 0x0);
857 ret |= it913x_write_reg(state, PRO_LINK, AGC_O_D, 0x0);
858 ret |= it913x_init_tuner(state);
860 if (ret < 0)
861 return -ENODEV;
863 /* Always solo frontend */
864 set_mode = set_solo_fe;
865 ret |= it913x_fe_script_loader(state, set_mode);
867 ret |= it913x_fe_suspend(state);
868 return (ret < 0) ? -ENODEV : 0;
871 static int it913x_fe_init(struct dvb_frontend *fe)
873 struct it913x_fe_state *state = fe->demodulator_priv;
874 int ret = 0;
875 /* Power Up Tuner - common all versions */
876 ret = it913x_write_reg(state, PRO_DMOD, 0xec40, 0x1);
878 ret |= it913x_fe_script_loader(state, init_1);
880 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x0);
882 ret |= it913x_write_reg(state, PRO_DMOD, 0xfba8, 0x0);
884 return (ret < 0) ? -ENODEV : 0;
887 static void it913x_fe_release(struct dvb_frontend *fe)
889 struct it913x_fe_state *state = fe->demodulator_priv;
890 kfree(state);
893 static struct dvb_frontend_ops it913x_fe_ofdm_ops;
895 struct dvb_frontend *it913x_fe_attach(struct i2c_adapter *i2c_adap,
896 u8 i2c_addr, struct ite_config *config)
898 struct it913x_fe_state *state = NULL;
899 int ret;
901 /* allocate memory for the internal state */
902 state = kzalloc(sizeof(struct it913x_fe_state), GFP_KERNEL);
903 if (state == NULL)
904 return NULL;
905 if (config == NULL)
906 goto error;
908 state->i2c_adap = i2c_adap;
909 state->i2c_addr = i2c_addr;
910 state->config = config;
912 switch (state->config->tuner_id_0) {
913 case IT9135_51:
914 case IT9135_52:
915 case IT9135_60:
916 case IT9135_61:
917 case IT9135_62:
918 state->tuner_type = state->config->tuner_id_0;
919 break;
920 default:
921 case IT9135_38:
922 state->tuner_type = IT9135_38;
925 ret = it913x_fe_start(state);
926 if (ret < 0)
927 goto error;
930 /* create dvb_frontend */
931 memcpy(&state->frontend.ops, &it913x_fe_ofdm_ops,
932 sizeof(struct dvb_frontend_ops));
933 state->frontend.demodulator_priv = state;
935 return &state->frontend;
936 error:
937 kfree(state);
938 return NULL;
940 EXPORT_SYMBOL(it913x_fe_attach);
942 static struct dvb_frontend_ops it913x_fe_ofdm_ops = {
943 .delsys = { SYS_DVBT },
944 .info = {
945 .name = "it913x-fe DVB-T",
946 .frequency_min = 51000000,
947 .frequency_max = 1680000000,
948 .frequency_stepsize = 62500,
949 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
950 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
951 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
952 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
953 FE_CAN_TRANSMISSION_MODE_AUTO |
954 FE_CAN_GUARD_INTERVAL_AUTO |
955 FE_CAN_HIERARCHY_AUTO,
958 .release = it913x_fe_release,
960 .init = it913x_fe_init,
961 .sleep = it913x_fe_sleep,
963 .set_frontend = it913x_fe_set_frontend,
964 .get_frontend = it913x_fe_get_frontend,
966 .read_status = it913x_fe_read_status,
967 .read_signal_strength = it913x_fe_read_signal_strength,
968 .read_snr = it913x_fe_read_snr,
969 .read_ber = it913x_fe_read_ber,
970 .read_ucblocks = it913x_fe_read_ucblocks,
973 MODULE_DESCRIPTION("it913x Frontend and it9137 tuner");
974 MODULE_AUTHOR("Malcolm Priestley tvboxspy@gmail.com");
975 MODULE_VERSION("1.13");
976 MODULE_LICENSE("GPL");