blob: 5113b89a814e72727a863be6e459f79c3778c4bd [file] [log] [blame]
Malcolm Priestley3dbbf822011-07-25 15:35:12 -03001/*
2 * Driver for it913x-fe Frontend
3 *
4 * with support for on chip it9137 integral tuner
5 *
6 * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
7 * IT9137 Copyright (C) ITE Tech Inc.
8 *
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.
13 *
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
17 *
18 * GNU General Public License for more details.
19 *
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.=
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/types.h>
29
30#include "dvb_frontend.h"
31#include "it913x-fe.h"
32#include "it913x-fe-priv.h"
33
34static int it913x_debug;
35
36module_param_named(debug, it913x_debug, int, 0644);
37MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
38
39#define dprintk(level, args...) do { \
40 if (level & it913x_debug) \
41 printk(KERN_DEBUG "it913x-fe: " args); \
42} while (0)
43
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
50struct it913x_fe_state {
51 struct dvb_frontend frontend;
52 struct i2c_adapter *i2c_adap;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -030053 struct ite_config *config;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030054 u8 i2c_addr;
55 u32 frequency;
56 u8 adf;
57 u32 crystalFrequency;
58 u32 adcFrequency;
59 u8 tuner_type;
60 struct adctable *table;
61 fe_status_t it913x_status;
tvboxspy7c2808e2011-09-21 19:06:58 -030062 u16 tun_xtal;
63 u8 tun_fdiv;
64 u8 tun_clk_mode;
65 u32 tun_fn_min;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030066};
67
68static int it913x_read_reg(struct it913x_fe_state *state,
69 u32 reg, u8 *data, u8 count)
70{
71 int ret;
72 u8 pro = PRO_DMOD; /* All reads from demodulator */
73 u8 b[4];
74 struct i2c_msg msg[2] = {
75 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
76 .buf = b, .len = sizeof(b) },
77 { .addr = state->i2c_addr + (pro << 1), .flags = I2C_M_RD,
78 .buf = data, .len = count }
79 };
80 b[0] = (u8) reg >> 24;
81 b[1] = (u8)(reg >> 16) & 0xff;
82 b[2] = (u8)(reg >> 8) & 0xff;
83 b[3] = (u8) reg & 0xff;
84
85 ret = i2c_transfer(state->i2c_adap, msg, 2);
86
87 return ret;
88}
89
90static int it913x_read_reg_u8(struct it913x_fe_state *state, u32 reg)
91{
92 int ret;
93 u8 b[1];
94 ret = it913x_read_reg(state, reg, &b[0], sizeof(b));
95 return (ret < 0) ? -ENODEV : b[0];
96}
97
98static int it913x_write(struct it913x_fe_state *state,
99 u8 pro, u32 reg, u8 buf[], u8 count)
100{
101 u8 b[256];
102 struct i2c_msg msg[1] = {
103 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
104 .buf = b, .len = count + 4 }
105 };
106 int ret;
107
108 b[0] = (u8) reg >> 24;
109 b[1] = (u8)(reg >> 16) & 0xff;
110 b[2] = (u8)(reg >> 8) & 0xff;
111 b[3] = (u8) reg & 0xff;
112 memcpy(&b[4], buf, count);
113
114 ret = i2c_transfer(state->i2c_adap, msg, 1);
115
116 if (ret < 0)
117 return -EIO;
118
119 return 0;
120}
121
122static int it913x_write_reg(struct it913x_fe_state *state,
123 u8 pro, u32 reg, u32 data)
124{
125 int ret;
126 u8 b[4];
127 u8 s;
128
129 b[0] = data >> 24;
130 b[1] = (data >> 16) & 0xff;
131 b[2] = (data >> 8) & 0xff;
132 b[3] = data & 0xff;
133 /* expand write as needed */
134 if (data < 0x100)
135 s = 3;
136 else if (data < 0x1000)
137 s = 2;
138 else if (data < 0x100000)
139 s = 1;
140 else
141 s = 0;
142
143 ret = it913x_write(state, pro, reg, &b[s], sizeof(b) - s);
144
145 return ret;
146}
147
148static int it913x_fe_script_loader(struct it913x_fe_state *state,
149 struct it913xset *loadscript)
150{
151 int ret, i;
152 if (loadscript == NULL)
153 return -EINVAL;
154
155 for (i = 0; i < 1000; ++i) {
156 if (loadscript[i].pro == 0xff)
157 break;
158 ret = it913x_write(state, loadscript[i].pro,
159 loadscript[i].address,
160 loadscript[i].reg, loadscript[i].count);
161 if (ret < 0)
162 return -ENODEV;
163 }
164 return 0;
165}
166
tvboxspy7c2808e2011-09-21 19:06:58 -0300167static int it913x_init_tuner(struct it913x_fe_state *state)
168{
169 int ret, i, reg;
170 u8 val, nv_val;
171 u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
172 u8 b[2];
173
174 reg = it913x_read_reg_u8(state, 0xec86);
175 switch (reg) {
176 case 0:
177 state->tun_clk_mode = reg;
178 state->tun_xtal = 2000;
179 state->tun_fdiv = 3;
180 val = 16;
181 break;
182 case -ENODEV:
183 return -ENODEV;
184 case 1:
185 default:
186 state->tun_clk_mode = reg;
187 state->tun_xtal = 640;
188 state->tun_fdiv = 1;
189 val = 6;
190 break;
191 }
192
193 reg = it913x_read_reg_u8(state, 0xed03);
194
195 if (reg < 0)
196 return -ENODEV;
197 else if (reg < sizeof(nv))
198 nv_val = nv[reg];
199 else
200 nv_val = 2;
201
202 for (i = 0; i < 50; i++) {
203 ret = it913x_read_reg(state, 0xed23, &b[0], sizeof(b));
204 reg = (b[1] << 8) + b[0];
205 if (reg > 0)
206 break;
207 if (ret < 0)
208 return -ENODEV;
209 udelay(2000);
210 }
211 state->tun_fn_min = state->tun_xtal * reg;
212 state->tun_fn_min /= (state->tun_fdiv * nv_val);
213 deb_info("Tuner fn_min %d", state->tun_fn_min);
214
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300215 if (state->config->chip_ver > 1)
216 msleep(50);
217 else {
218 for (i = 0; i < 50; i++) {
219 reg = it913x_read_reg_u8(state, 0xec82);
220 if (reg > 0)
221 break;
222 if (reg < 0)
223 return -ENODEV;
224 udelay(2000);
225 }
tvboxspy7c2808e2011-09-21 19:06:58 -0300226 }
227
228 return it913x_write_reg(state, PRO_DMOD, 0xed81, val);
229}
230
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300231static int it9137_set_tuner(struct it913x_fe_state *state,
232 enum fe_bandwidth bandwidth, u32 frequency_m)
233{
234 struct it913xset *set_tuner = set_it9137_template;
tvboxspy7c2808e2011-09-21 19:06:58 -0300235 int ret, reg;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300236 u32 frequency = frequency_m / 1000;
tvboxspy7c2808e2011-09-21 19:06:58 -0300237 u32 freq, temp_f, tmp;
238 u16 iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300239 u16 n_div;
240 u8 n;
241 u8 l_band;
242 u8 lna_band;
243 u8 bw;
244
245 deb_info("Tuner Frequency %d Bandwidth %d", frequency, bandwidth);
246
247 if (frequency >= 51000 && frequency <= 440000) {
248 l_band = 0;
249 lna_band = 0;
250 } else if (frequency > 440000 && frequency <= 484000) {
251 l_band = 1;
252 lna_band = 1;
253 } else if (frequency > 484000 && frequency <= 533000) {
254 l_band = 1;
255 lna_band = 2;
256 } else if (frequency > 533000 && frequency <= 587000) {
257 l_band = 1;
258 lna_band = 3;
259 } else if (frequency > 587000 && frequency <= 645000) {
260 l_band = 1;
261 lna_band = 4;
262 } else if (frequency > 645000 && frequency <= 710000) {
263 l_band = 1;
264 lna_band = 5;
265 } else if (frequency > 710000 && frequency <= 782000) {
266 l_band = 1;
267 lna_band = 6;
268 } else if (frequency > 782000 && frequency <= 860000) {
269 l_band = 1;
270 lna_band = 7;
271 } else if (frequency > 1450000 && frequency <= 1492000) {
272 l_band = 1;
273 lna_band = 0;
274 } else if (frequency > 1660000 && frequency <= 1685000) {
275 l_band = 1;
276 lna_band = 1;
277 } else
278 return -EINVAL;
279 set_tuner[0].reg[0] = lna_band;
280
281 if (bandwidth == BANDWIDTH_5_MHZ)
282 bw = 0;
283 else if (bandwidth == BANDWIDTH_6_MHZ)
284 bw = 2;
285 else if (bandwidth == BANDWIDTH_7_MHZ)
286 bw = 4;
287 else if (bandwidth == BANDWIDTH_8_MHZ)
288 bw = 6;
289 else
290 bw = 6;
tvboxspy7c2808e2011-09-21 19:06:58 -0300291
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300292 set_tuner[1].reg[0] = bw;
293 set_tuner[2].reg[0] = 0xa0 | (l_band << 3);
294
tvboxspy7c2808e2011-09-21 19:06:58 -0300295 if (frequency > 53000 && frequency <= 74000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300296 n_div = 48;
297 n = 0;
298 } else if (frequency > 74000 && frequency <= 111000) {
299 n_div = 32;
300 n = 1;
301 } else if (frequency > 111000 && frequency <= 148000) {
302 n_div = 24;
303 n = 2;
304 } else if (frequency > 148000 && frequency <= 222000) {
305 n_div = 16;
306 n = 3;
307 } else if (frequency > 222000 && frequency <= 296000) {
308 n_div = 12;
309 n = 4;
310 } else if (frequency > 296000 && frequency <= 445000) {
311 n_div = 8;
312 n = 5;
tvboxspy7c2808e2011-09-21 19:06:58 -0300313 } else if (frequency > 445000 && frequency <= state->tun_fn_min) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300314 n_div = 6;
315 n = 6;
tvboxspy7c2808e2011-09-21 19:06:58 -0300316 } else if (frequency > state->tun_fn_min && frequency <= 950000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300317 n_div = 4;
318 n = 7;
319 } else if (frequency > 1450000 && frequency <= 1680000) {
320 n_div = 2;
321 n = 0;
322 } else
323 return -EINVAL;
324
tvboxspy7c2808e2011-09-21 19:06:58 -0300325 reg = it913x_read_reg_u8(state, 0xed81);
326 iqik_m_cal = (u16)reg * n_div;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300327
tvboxspy7c2808e2011-09-21 19:06:58 -0300328 if (reg < 0x20) {
329 if (state->tun_clk_mode == 0)
330 iqik_m_cal = (iqik_m_cal * 9) >> 5;
331 else
332 iqik_m_cal >>= 1;
333 } else {
334 iqik_m_cal = 0x40 - iqik_m_cal;
335 if (state->tun_clk_mode == 0)
336 iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
337 else
338 iqik_m_cal = ~(iqik_m_cal >> 1);
339 }
340
341 temp_f = frequency * (u32)n_div * (u32)state->tun_fdiv;
342 freq = temp_f / state->tun_xtal;
343 tmp = freq * state->tun_xtal;
344
345 if ((temp_f - tmp) >= (state->tun_xtal >> 1))
346 freq++;
347
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300348 freq += (u32) n << 13;
tvboxspy7c2808e2011-09-21 19:06:58 -0300349 /* Frequency OMEGA_IQIK_M_CAL_MID*/
350 temp_f = freq + (u32)iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300351
tvboxspy7c2808e2011-09-21 19:06:58 -0300352 set_tuner[3].reg[0] = temp_f & 0xff;
353 set_tuner[4].reg[0] = (temp_f >> 8) & 0xff;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300354
tvboxspy7c2808e2011-09-21 19:06:58 -0300355 deb_info("High Frequency = %04x", temp_f);
356
357 /* Lower frequency */
358 set_tuner[5].reg[0] = freq & 0xff;
359 set_tuner[6].reg[0] = (freq >> 8) & 0xff;
360
361 deb_info("low Frequency = %04x", freq);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300362
363 ret = it913x_fe_script_loader(state, set_tuner);
364
365 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300366}
367
368static int it913x_fe_select_bw(struct it913x_fe_state *state,
369 enum fe_bandwidth bandwidth, u32 adcFrequency)
370{
371 int ret, i;
372 u8 buffer[256];
373 u32 coeff[8];
374 u16 bfsfcw_fftinx_ratio;
375 u16 fftinx_bfsfcw_ratio;
376 u8 count;
377 u8 bw;
378 u8 adcmultiplier;
379
380 deb_info("Bandwidth %d Adc %d", bandwidth, adcFrequency);
381
382 if (bandwidth == BANDWIDTH_5_MHZ)
383 bw = 3;
384 else if (bandwidth == BANDWIDTH_6_MHZ)
385 bw = 0;
386 else if (bandwidth == BANDWIDTH_7_MHZ)
387 bw = 1;
388 else if (bandwidth == BANDWIDTH_8_MHZ)
389 bw = 2;
390 else
391 bw = 2;
392
393 ret = it913x_write_reg(state, PRO_DMOD, REG_BW, bw);
394
395 if (state->table == NULL)
396 return -EINVAL;
397
398 /* In write order */
399 coeff[0] = state->table[bw].coeff_1_2048;
400 coeff[1] = state->table[bw].coeff_2_2k;
401 coeff[2] = state->table[bw].coeff_1_8191;
402 coeff[3] = state->table[bw].coeff_1_8192;
403 coeff[4] = state->table[bw].coeff_1_8193;
404 coeff[5] = state->table[bw].coeff_2_8k;
405 coeff[6] = state->table[bw].coeff_1_4096;
406 coeff[7] = state->table[bw].coeff_2_4k;
407 bfsfcw_fftinx_ratio = state->table[bw].bfsfcw_fftinx_ratio;
408 fftinx_bfsfcw_ratio = state->table[bw].fftinx_bfsfcw_ratio;
409
410 /* ADC multiplier */
411 ret = it913x_read_reg_u8(state, ADC_X_2);
412 if (ret < 0)
413 return -EINVAL;
414
415 adcmultiplier = ret;
416
417 count = 0;
418
419 /* Build Buffer for COEFF Registers */
420 for (i = 0; i < 8; i++) {
421 if (adcmultiplier == 1)
422 coeff[i] /= 2;
423 buffer[count++] = (coeff[i] >> 24) & 0x3;
424 buffer[count++] = (coeff[i] >> 16) & 0xff;
425 buffer[count++] = (coeff[i] >> 8) & 0xff;
426 buffer[count++] = coeff[i] & 0xff;
427 }
428
429 /* bfsfcw_fftinx_ratio register 0x21-0x22 */
430 buffer[count++] = bfsfcw_fftinx_ratio & 0xff;
431 buffer[count++] = (bfsfcw_fftinx_ratio >> 8) & 0xff;
432 /* fftinx_bfsfcw_ratio register 0x23-0x24 */
433 buffer[count++] = fftinx_bfsfcw_ratio & 0xff;
434 buffer[count++] = (fftinx_bfsfcw_ratio >> 8) & 0xff;
435 /* start at COEFF_1_2048 and write through to fftinx_bfsfcw_ratio*/
436 ret = it913x_write(state, PRO_DMOD, COEFF_1_2048, buffer, count);
437
438 for (i = 0; i < 42; i += 8)
439 debug_data_snipet(0x1, "Buffer", &buffer[i]);
440
441 return ret;
442}
443
444
445
446static int it913x_fe_read_status(struct dvb_frontend *fe, fe_status_t *status)
447{
448 struct it913x_fe_state *state = fe->demodulator_priv;
449 int ret, i;
450 fe_status_t old_status = state->it913x_status;
451 *status = 0;
452
453 if (state->it913x_status == 0) {
454 ret = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
455 if (ret == 0x1) {
456 *status |= FE_HAS_SIGNAL;
457 for (i = 0; i < 40; i++) {
458 ret = it913x_read_reg_u8(state, MP2IF_SYNC_LK);
459 if (ret == 0x1)
460 break;
461 msleep(25);
462 }
463 if (ret == 0x1)
464 *status |= FE_HAS_CARRIER
465 | FE_HAS_VITERBI
466 | FE_HAS_SYNC;
467 state->it913x_status = *status;
468 }
469 }
470
471 if (state->it913x_status & FE_HAS_SYNC) {
472 ret = it913x_read_reg_u8(state, TPSD_LOCK);
473 if (ret == 0x1)
474 *status |= FE_HAS_LOCK
475 | state->it913x_status;
476 else
477 state->it913x_status = 0;
478 if (old_status != state->it913x_status)
479 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, ret);
480 }
481
482 return 0;
483}
484
485static int it913x_fe_read_signal_strength(struct dvb_frontend *fe,
486 u16 *strength)
487{
488 struct it913x_fe_state *state = fe->demodulator_priv;
489 int ret = it913x_read_reg_u8(state, SIGNAL_LEVEL);
490 /*SIGNAL_LEVEL always returns 100%! so using FE_HAS_SIGNAL as switch*/
491 if (state->it913x_status & FE_HAS_SIGNAL)
492 ret = (ret * 0xff) / 0x64;
493 else
494 ret = 0x0;
495 ret |= ret << 0x8;
496 *strength = ret;
497 return 0;
498}
499
500static int it913x_fe_read_snr(struct dvb_frontend *fe, u16* snr)
501{
502 struct it913x_fe_state *state = fe->demodulator_priv;
503 int ret = it913x_read_reg_u8(state, SIGNAL_QUALITY);
504 ret = (ret * 0xff) / 0x64;
505 ret |= (ret << 0x8);
506 *snr = ~ret;
507 return 0;
508}
509
510static int it913x_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
511{
512 *ber = 0;
513 return 0;
514}
515
516static int it913x_fe_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
517{
518 *ucblocks = 0;
519 return 0;
520}
521
522static int it913x_fe_get_frontend(struct dvb_frontend *fe,
523 struct dvb_frontend_parameters *p)
524{
525 struct it913x_fe_state *state = fe->demodulator_priv;
526 int ret;
527 u8 reg[8];
528
529 ret = it913x_read_reg(state, REG_TPSD_TX_MODE, reg, sizeof(reg));
530
531 if (reg[3] < 3)
532 p->u.ofdm.constellation = fe_con[reg[3]];
533
534 if (reg[0] < 3)
535 p->u.ofdm.transmission_mode = fe_mode[reg[0]];
536
537 if (reg[1] < 4)
538 p->u.ofdm.guard_interval = fe_gi[reg[1]];
539
540 if (reg[2] < 4)
541 p->u.ofdm.hierarchy_information = fe_hi[reg[2]];
542
543 p->u.ofdm.code_rate_HP = (reg[6] < 6) ? fe_code[reg[6]] : FEC_NONE;
544 p->u.ofdm.code_rate_LP = (reg[7] < 6) ? fe_code[reg[7]] : FEC_NONE;
545
546 return 0;
547}
548
549static int it913x_fe_set_frontend(struct dvb_frontend *fe,
550 struct dvb_frontend_parameters *p)
551{
552 struct it913x_fe_state *state = fe->demodulator_priv;
553 int ret, i;
554 u8 empty_ch, last_ch;
555
556 state->it913x_status = 0;
557
558 /* Set bw*/
559 ret = it913x_fe_select_bw(state, p->u.ofdm.bandwidth,
560 state->adcFrequency);
561
562 /* Training Mode Off */
563 ret = it913x_write_reg(state, PRO_LINK, TRAINING_MODE, 0x0);
564
565 /* Clear Empty Channel */
566 ret = it913x_write_reg(state, PRO_DMOD, EMPTY_CHANNEL_STATUS, 0x0);
567
568 /* Clear bits */
569 ret = it913x_write_reg(state, PRO_DMOD, MP2IF_SYNC_LK, 0x0);
570 /* LED on */
571 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
572 /* Select Band*/
573 if ((p->frequency >= 51000000) && (p->frequency <= 230000000))
574 i = 0;
575 else if ((p->frequency >= 350000000) && (p->frequency <= 900000000))
576 i = 1;
577 else if ((p->frequency >= 1450000000) && (p->frequency <= 1680000000))
578 i = 2;
579 else
580 return -EOPNOTSUPP;
581
582 ret = it913x_write_reg(state, PRO_DMOD, FREE_BAND, i);
583
584 deb_info("Frontend Set Tuner Type %02x", state->tuner_type);
585 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300586 case IT9135_38:
587 case IT9135_51:
588 case IT9135_52:
589 case IT9135_60:
590 case IT9135_61:
591 case IT9135_62:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300592 ret = it9137_set_tuner(state,
593 p->u.ofdm.bandwidth, p->frequency);
594 break;
595 default:
596 if (fe->ops.tuner_ops.set_params) {
597 fe->ops.tuner_ops.set_params(fe, p);
598 if (fe->ops.i2c_gate_ctrl)
599 fe->ops.i2c_gate_ctrl(fe, 0);
600 }
601 break;
602 }
603 /* LED off */
604 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
605 /* Trigger ofsm */
606 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
607 last_ch = 2;
608 for (i = 0; i < 40; ++i) {
609 empty_ch = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
610 if (last_ch == 1 && empty_ch == 1)
611 break;
612 if (last_ch == 2 && empty_ch == 2)
613 return 0;
614 last_ch = empty_ch;
615 msleep(25);
616 }
617 for (i = 0; i < 40; ++i) {
618 if (it913x_read_reg_u8(state, D_TPSD_LOCK) == 1)
619 break;
620 msleep(25);
621 }
622
623 state->frequency = p->frequency;
624 return 0;
625}
626
627static int it913x_fe_suspend(struct it913x_fe_state *state)
628{
629 int ret, i;
630 u8 b;
631
632 ret = it913x_write_reg(state, PRO_DMOD, SUSPEND_FLAG, 0x1);
633
634 ret |= it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
635
636 for (i = 0; i < 128; i++) {
637 ret = it913x_read_reg(state, SUSPEND_FLAG, &b, 1);
638 if (ret < 0)
Malcolm Priestleye3052882011-10-01 09:24:16 -0300639 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300640 if (b == 0)
641 break;
642
643 }
644
645 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x8);
646 /* Turn LED off */
Malcolm Priestleye3052882011-10-01 09:24:16 -0300647 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300648
Malcolm Priestleye3052882011-10-01 09:24:16 -0300649 ret |= it913x_fe_script_loader(state, it9137_tuner_off);
650
651 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300652}
653
Malcolm Priestleye3052882011-10-01 09:24:16 -0300654/* Power sequence */
655/* Power Up Tuner on -> Frontend suspend off -> Tuner clk on */
656/* Power Down Frontend suspend on -> Tuner clk off -> Tuner off */
657
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300658static int it913x_fe_sleep(struct dvb_frontend *fe)
659{
660 struct it913x_fe_state *state = fe->demodulator_priv;
661 return it913x_fe_suspend(state);
662}
663
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300664static u32 compute_div(u32 a, u32 b, u32 x)
665{
666 u32 res = 0;
667 u32 c = 0;
668 u32 i = 0;
669
670 if (a > b) {
671 c = a / b;
672 a = a - c * b;
673 }
674
675 for (i = 0; i < x; i++) {
676 if (a >= b) {
677 res += 1;
678 a -= b;
679 }
680 a <<= 1;
681 res <<= 1;
682 }
683
684 res = (c << x) + res;
685
686 return res;
687}
688
689static int it913x_fe_start(struct it913x_fe_state *state)
690{
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300691 struct it913xset *set_lna;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300692 struct it913xset *set_mode;
693 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300694 u8 adf = (state->config->adf & 0xf);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300695 u32 adc, xtal;
696 u8 b[4];
697
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300698 if (state->config->chip_ver == 1)
699 ret = it913x_init_tuner(state);
tvboxspy7c2808e2011-09-21 19:06:58 -0300700
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300701 if (adf < 12) {
702 state->crystalFrequency = fe_clockTable[adf].xtal ;
703 state->table = fe_clockTable[adf].table;
704 state->adcFrequency = state->table->adcFrequency;
705
706 adc = compute_div(state->adcFrequency, 1000000ul, 19ul);
707 xtal = compute_div(state->crystalFrequency, 1000000ul, 19ul);
708
709 } else
710 return -EINVAL;
711
712 deb_info("Xtal Freq :%d Adc Freq :%d Adc %08x Xtal %08x",
713 state->crystalFrequency, state->adcFrequency, adc, xtal);
714
715 /* Set LED indicator on GPIOH3 */
716 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_EN, 0x1);
717 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_ON, 0x1);
718 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
719
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300720 ret |= it913x_write_reg(state, PRO_LINK, 0xf641, state->tuner_type);
721 ret |= it913x_write_reg(state, PRO_DMOD, 0xf5ca, 0x01);
722 ret |= it913x_write_reg(state, PRO_DMOD, 0xf715, 0x01);
723
724 b[0] = xtal & 0xff;
725 b[1] = (xtal >> 8) & 0xff;
726 b[2] = (xtal >> 16) & 0xff;
727 b[3] = (xtal >> 24);
728 ret |= it913x_write(state, PRO_DMOD, XTAL_CLK, b , 4);
729
730 b[0] = adc & 0xff;
731 b[1] = (adc >> 8) & 0xff;
732 b[2] = (adc >> 16) & 0xff;
733 ret |= it913x_write(state, PRO_DMOD, ADC_FREQ, b, 3);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300734 if (ret < 0)
735 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300736
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300737 /* v1 or v2 tuner script */
738 if (state->config->chip_ver > 1)
739 ret = it913x_fe_script_loader(state, it9135_v2);
740 else
741 ret = it913x_fe_script_loader(state, it9135_v1);
742 if (ret < 0)
743 return ret;
744
745 /* LNA Scripts */
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300746 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300747 case IT9135_51:
748 set_lna = it9135_51;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300749 break;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300750 case IT9135_52:
751 set_lna = it9135_52;
752 break;
753 case IT9135_60:
754 set_lna = it9135_60;
755 break;
756 case IT9135_61:
757 set_lna = it9135_61;
758 break;
759 case IT9135_62:
760 set_lna = it9135_62;
761 break;
762 case IT9135_38:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300763 default:
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300764 set_lna = it9135_38;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300765 }
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300766 ret = it913x_fe_script_loader(state, set_lna);
767 if (ret < 0)
768 return ret;
tvboxspy7c2808e2011-09-21 19:06:58 -0300769
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300770 if (state->config->chip_ver == 2) {
771 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x1);
772 ret |= it913x_write_reg(state, PRO_LINK, PADODPU, 0x0);
773 ret |= it913x_write_reg(state, PRO_LINK, AGC_O_D, 0x0);
774 ret |= it913x_init_tuner(state);
775 }
776 if (ret < 0)
777 return -ENODEV;
778
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300779 /* Always solo frontend */
780 set_mode = set_solo_fe;
781 ret |= it913x_fe_script_loader(state, set_mode);
782
783 ret |= it913x_fe_suspend(state);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300784 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300785}
786
787static int it913x_fe_init(struct dvb_frontend *fe)
788{
789 struct it913x_fe_state *state = fe->demodulator_priv;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300790 int ret = 0;
Malcolm Priestleye3052882011-10-01 09:24:16 -0300791 /* Power Up Tuner - common all versions */
792 ret = it913x_write_reg(state, PRO_DMOD, 0xec40, 0x1);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300793
Malcolm Priestleye3052882011-10-01 09:24:16 -0300794 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x0);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300795
796 ret |= it913x_fe_script_loader(state, init_1);
797
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300798 ret |= it913x_write_reg(state, PRO_DMOD, 0xfba8, 0x0);
Malcolm Priestleye3052882011-10-01 09:24:16 -0300799
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300800 return (ret < 0) ? -ENODEV : 0;
801}
802
803static void it913x_fe_release(struct dvb_frontend *fe)
804{
805 struct it913x_fe_state *state = fe->demodulator_priv;
806 kfree(state);
807}
808
809static struct dvb_frontend_ops it913x_fe_ofdm_ops;
810
811struct dvb_frontend *it913x_fe_attach(struct i2c_adapter *i2c_adap,
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300812 u8 i2c_addr, struct ite_config *config)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300813{
814 struct it913x_fe_state *state = NULL;
815 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300816
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300817 /* allocate memory for the internal state */
818 state = kzalloc(sizeof(struct it913x_fe_state), GFP_KERNEL);
819 if (state == NULL)
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300820 return NULL;
821 if (config == NULL)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300822 goto error;
823
824 state->i2c_adap = i2c_adap;
825 state->i2c_addr = i2c_addr;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300826 state->config = config;
827
828 switch (state->config->tuner_id_0) {
829 case IT9135_51:
830 case IT9135_52:
831 case IT9135_60:
832 case IT9135_61:
833 case IT9135_62:
834 state->tuner_type = state->config->tuner_id_0;
835 break;
836 default:
837 case IT9135_38:
838 state->tuner_type = IT9135_38;
839 }
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300840
841 ret = it913x_fe_start(state);
842 if (ret < 0)
843 goto error;
844
845
846 /* create dvb_frontend */
847 memcpy(&state->frontend.ops, &it913x_fe_ofdm_ops,
848 sizeof(struct dvb_frontend_ops));
849 state->frontend.demodulator_priv = state;
850
851 return &state->frontend;
852error:
853 kfree(state);
854 return NULL;
855}
856EXPORT_SYMBOL(it913x_fe_attach);
857
858static struct dvb_frontend_ops it913x_fe_ofdm_ops = {
859
860 .info = {
861 .name = "it913x-fe DVB-T",
862 .type = FE_OFDM,
863 .frequency_min = 51000000,
864 .frequency_max = 1680000000,
865 .frequency_stepsize = 62500,
866 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
867 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
868 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
869 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
870 FE_CAN_TRANSMISSION_MODE_AUTO |
871 FE_CAN_GUARD_INTERVAL_AUTO |
872 FE_CAN_HIERARCHY_AUTO,
873 },
874
875 .release = it913x_fe_release,
876
877 .init = it913x_fe_init,
878 .sleep = it913x_fe_sleep,
879
880 .set_frontend = it913x_fe_set_frontend,
881 .get_frontend = it913x_fe_get_frontend,
882
883 .read_status = it913x_fe_read_status,
884 .read_signal_strength = it913x_fe_read_signal_strength,
885 .read_snr = it913x_fe_read_snr,
886 .read_ber = it913x_fe_read_ber,
887 .read_ucblocks = it913x_fe_read_ucblocks,
888};
889
890MODULE_DESCRIPTION("it913x Frontend and it9137 tuner");
891MODULE_AUTHOR("Malcolm Priestley tvboxspy@gmail.com");
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300892MODULE_VERSION("1.08");
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300893MODULE_LICENSE("GPL");