blob: 07c1b4601b1f78fbaf112bb72d505ff7e0318710 [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;
Malcolm Priestley3339a5b2011-11-06 11:19:14 -030056 fe_modulation_t constellation;
57 fe_transmit_mode_t transmission_mode;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030058 u32 crystalFrequency;
59 u32 adcFrequency;
60 u8 tuner_type;
61 struct adctable *table;
62 fe_status_t it913x_status;
tvboxspy7c2808e2011-09-21 19:06:58 -030063 u16 tun_xtal;
64 u8 tun_fdiv;
65 u8 tun_clk_mode;
66 u32 tun_fn_min;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030067};
68
69static int it913x_read_reg(struct it913x_fe_state *state,
70 u32 reg, u8 *data, u8 count)
71{
72 int ret;
73 u8 pro = PRO_DMOD; /* All reads from demodulator */
74 u8 b[4];
75 struct i2c_msg msg[2] = {
76 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
77 .buf = b, .len = sizeof(b) },
78 { .addr = state->i2c_addr + (pro << 1), .flags = I2C_M_RD,
79 .buf = data, .len = count }
80 };
81 b[0] = (u8) reg >> 24;
82 b[1] = (u8)(reg >> 16) & 0xff;
83 b[2] = (u8)(reg >> 8) & 0xff;
84 b[3] = (u8) reg & 0xff;
85
86 ret = i2c_transfer(state->i2c_adap, msg, 2);
87
88 return ret;
89}
90
91static int it913x_read_reg_u8(struct it913x_fe_state *state, u32 reg)
92{
93 int ret;
94 u8 b[1];
95 ret = it913x_read_reg(state, reg, &b[0], sizeof(b));
96 return (ret < 0) ? -ENODEV : b[0];
97}
98
99static int it913x_write(struct it913x_fe_state *state,
100 u8 pro, u32 reg, u8 buf[], u8 count)
101{
102 u8 b[256];
103 struct i2c_msg msg[1] = {
104 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
105 .buf = b, .len = count + 4 }
106 };
107 int ret;
108
109 b[0] = (u8) reg >> 24;
110 b[1] = (u8)(reg >> 16) & 0xff;
111 b[2] = (u8)(reg >> 8) & 0xff;
112 b[3] = (u8) reg & 0xff;
113 memcpy(&b[4], buf, count);
114
115 ret = i2c_transfer(state->i2c_adap, msg, 1);
116
117 if (ret < 0)
118 return -EIO;
119
120 return 0;
121}
122
123static int it913x_write_reg(struct it913x_fe_state *state,
124 u8 pro, u32 reg, u32 data)
125{
126 int ret;
127 u8 b[4];
128 u8 s;
129
130 b[0] = data >> 24;
131 b[1] = (data >> 16) & 0xff;
132 b[2] = (data >> 8) & 0xff;
133 b[3] = data & 0xff;
134 /* expand write as needed */
135 if (data < 0x100)
136 s = 3;
137 else if (data < 0x1000)
138 s = 2;
139 else if (data < 0x100000)
140 s = 1;
141 else
142 s = 0;
143
144 ret = it913x_write(state, pro, reg, &b[s], sizeof(b) - s);
145
146 return ret;
147}
148
149static int it913x_fe_script_loader(struct it913x_fe_state *state,
150 struct it913xset *loadscript)
151{
152 int ret, i;
153 if (loadscript == NULL)
154 return -EINVAL;
155
156 for (i = 0; i < 1000; ++i) {
157 if (loadscript[i].pro == 0xff)
158 break;
159 ret = it913x_write(state, loadscript[i].pro,
160 loadscript[i].address,
161 loadscript[i].reg, loadscript[i].count);
162 if (ret < 0)
163 return -ENODEV;
164 }
165 return 0;
166}
167
tvboxspy7c2808e2011-09-21 19:06:58 -0300168static int it913x_init_tuner(struct it913x_fe_state *state)
169{
170 int ret, i, reg;
171 u8 val, nv_val;
172 u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
173 u8 b[2];
174
175 reg = it913x_read_reg_u8(state, 0xec86);
176 switch (reg) {
177 case 0:
178 state->tun_clk_mode = reg;
179 state->tun_xtal = 2000;
180 state->tun_fdiv = 3;
181 val = 16;
182 break;
183 case -ENODEV:
184 return -ENODEV;
185 case 1:
186 default:
187 state->tun_clk_mode = reg;
188 state->tun_xtal = 640;
189 state->tun_fdiv = 1;
190 val = 6;
191 break;
192 }
193
194 reg = it913x_read_reg_u8(state, 0xed03);
195
196 if (reg < 0)
197 return -ENODEV;
198 else if (reg < sizeof(nv))
199 nv_val = nv[reg];
200 else
201 nv_val = 2;
202
203 for (i = 0; i < 50; i++) {
204 ret = it913x_read_reg(state, 0xed23, &b[0], sizeof(b));
205 reg = (b[1] << 8) + b[0];
206 if (reg > 0)
207 break;
208 if (ret < 0)
209 return -ENODEV;
210 udelay(2000);
211 }
212 state->tun_fn_min = state->tun_xtal * reg;
213 state->tun_fn_min /= (state->tun_fdiv * nv_val);
214 deb_info("Tuner fn_min %d", state->tun_fn_min);
215
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300216 if (state->config->chip_ver > 1)
217 msleep(50);
218 else {
219 for (i = 0; i < 50; i++) {
220 reg = it913x_read_reg_u8(state, 0xec82);
221 if (reg > 0)
222 break;
223 if (reg < 0)
224 return -ENODEV;
225 udelay(2000);
226 }
tvboxspy7c2808e2011-09-21 19:06:58 -0300227 }
228
229 return it913x_write_reg(state, PRO_DMOD, 0xed81, val);
230}
231
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300232static int it9137_set_tuner(struct it913x_fe_state *state,
233 enum fe_bandwidth bandwidth, u32 frequency_m)
234{
235 struct it913xset *set_tuner = set_it9137_template;
tvboxspy7c2808e2011-09-21 19:06:58 -0300236 int ret, reg;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300237 u32 frequency = frequency_m / 1000;
tvboxspy7c2808e2011-09-21 19:06:58 -0300238 u32 freq, temp_f, tmp;
239 u16 iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300240 u16 n_div;
241 u8 n;
242 u8 l_band;
243 u8 lna_band;
244 u8 bw;
245
246 deb_info("Tuner Frequency %d Bandwidth %d", frequency, bandwidth);
247
248 if (frequency >= 51000 && frequency <= 440000) {
249 l_band = 0;
250 lna_band = 0;
251 } else if (frequency > 440000 && frequency <= 484000) {
252 l_band = 1;
253 lna_band = 1;
254 } else if (frequency > 484000 && frequency <= 533000) {
255 l_band = 1;
256 lna_band = 2;
257 } else if (frequency > 533000 && frequency <= 587000) {
258 l_band = 1;
259 lna_band = 3;
260 } else if (frequency > 587000 && frequency <= 645000) {
261 l_band = 1;
262 lna_band = 4;
263 } else if (frequency > 645000 && frequency <= 710000) {
264 l_band = 1;
265 lna_band = 5;
266 } else if (frequency > 710000 && frequency <= 782000) {
267 l_band = 1;
268 lna_band = 6;
269 } else if (frequency > 782000 && frequency <= 860000) {
270 l_band = 1;
271 lna_band = 7;
272 } else if (frequency > 1450000 && frequency <= 1492000) {
273 l_band = 1;
274 lna_band = 0;
275 } else if (frequency > 1660000 && frequency <= 1685000) {
276 l_band = 1;
277 lna_band = 1;
278 } else
279 return -EINVAL;
280 set_tuner[0].reg[0] = lna_band;
281
282 if (bandwidth == BANDWIDTH_5_MHZ)
283 bw = 0;
284 else if (bandwidth == BANDWIDTH_6_MHZ)
285 bw = 2;
286 else if (bandwidth == BANDWIDTH_7_MHZ)
287 bw = 4;
288 else if (bandwidth == BANDWIDTH_8_MHZ)
289 bw = 6;
290 else
291 bw = 6;
tvboxspy7c2808e2011-09-21 19:06:58 -0300292
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300293 set_tuner[1].reg[0] = bw;
294 set_tuner[2].reg[0] = 0xa0 | (l_band << 3);
295
tvboxspy7c2808e2011-09-21 19:06:58 -0300296 if (frequency > 53000 && frequency <= 74000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300297 n_div = 48;
298 n = 0;
299 } else if (frequency > 74000 && frequency <= 111000) {
300 n_div = 32;
301 n = 1;
302 } else if (frequency > 111000 && frequency <= 148000) {
303 n_div = 24;
304 n = 2;
305 } else if (frequency > 148000 && frequency <= 222000) {
306 n_div = 16;
307 n = 3;
308 } else if (frequency > 222000 && frequency <= 296000) {
309 n_div = 12;
310 n = 4;
311 } else if (frequency > 296000 && frequency <= 445000) {
312 n_div = 8;
313 n = 5;
tvboxspy7c2808e2011-09-21 19:06:58 -0300314 } else if (frequency > 445000 && frequency <= state->tun_fn_min) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300315 n_div = 6;
316 n = 6;
tvboxspy7c2808e2011-09-21 19:06:58 -0300317 } else if (frequency > state->tun_fn_min && frequency <= 950000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300318 n_div = 4;
319 n = 7;
320 } else if (frequency > 1450000 && frequency <= 1680000) {
321 n_div = 2;
322 n = 0;
323 } else
324 return -EINVAL;
325
tvboxspy7c2808e2011-09-21 19:06:58 -0300326 reg = it913x_read_reg_u8(state, 0xed81);
327 iqik_m_cal = (u16)reg * n_div;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300328
tvboxspy7c2808e2011-09-21 19:06:58 -0300329 if (reg < 0x20) {
330 if (state->tun_clk_mode == 0)
331 iqik_m_cal = (iqik_m_cal * 9) >> 5;
332 else
333 iqik_m_cal >>= 1;
334 } else {
335 iqik_m_cal = 0x40 - iqik_m_cal;
336 if (state->tun_clk_mode == 0)
337 iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
338 else
339 iqik_m_cal = ~(iqik_m_cal >> 1);
340 }
341
342 temp_f = frequency * (u32)n_div * (u32)state->tun_fdiv;
343 freq = temp_f / state->tun_xtal;
344 tmp = freq * state->tun_xtal;
345
346 if ((temp_f - tmp) >= (state->tun_xtal >> 1))
347 freq++;
348
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300349 freq += (u32) n << 13;
tvboxspy7c2808e2011-09-21 19:06:58 -0300350 /* Frequency OMEGA_IQIK_M_CAL_MID*/
351 temp_f = freq + (u32)iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300352
tvboxspy7c2808e2011-09-21 19:06:58 -0300353 set_tuner[3].reg[0] = temp_f & 0xff;
354 set_tuner[4].reg[0] = (temp_f >> 8) & 0xff;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300355
tvboxspy7c2808e2011-09-21 19:06:58 -0300356 deb_info("High Frequency = %04x", temp_f);
357
358 /* Lower frequency */
359 set_tuner[5].reg[0] = freq & 0xff;
360 set_tuner[6].reg[0] = (freq >> 8) & 0xff;
361
362 deb_info("low Frequency = %04x", freq);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300363
364 ret = it913x_fe_script_loader(state, set_tuner);
365
366 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300367}
368
369static int it913x_fe_select_bw(struct it913x_fe_state *state,
370 enum fe_bandwidth bandwidth, u32 adcFrequency)
371{
372 int ret, i;
373 u8 buffer[256];
374 u32 coeff[8];
375 u16 bfsfcw_fftinx_ratio;
376 u16 fftinx_bfsfcw_ratio;
377 u8 count;
378 u8 bw;
379 u8 adcmultiplier;
380
381 deb_info("Bandwidth %d Adc %d", bandwidth, adcFrequency);
382
383 if (bandwidth == BANDWIDTH_5_MHZ)
384 bw = 3;
385 else if (bandwidth == BANDWIDTH_6_MHZ)
386 bw = 0;
387 else if (bandwidth == BANDWIDTH_7_MHZ)
388 bw = 1;
389 else if (bandwidth == BANDWIDTH_8_MHZ)
390 bw = 2;
391 else
392 bw = 2;
393
394 ret = it913x_write_reg(state, PRO_DMOD, REG_BW, bw);
395
396 if (state->table == NULL)
397 return -EINVAL;
398
399 /* In write order */
400 coeff[0] = state->table[bw].coeff_1_2048;
401 coeff[1] = state->table[bw].coeff_2_2k;
402 coeff[2] = state->table[bw].coeff_1_8191;
403 coeff[3] = state->table[bw].coeff_1_8192;
404 coeff[4] = state->table[bw].coeff_1_8193;
405 coeff[5] = state->table[bw].coeff_2_8k;
406 coeff[6] = state->table[bw].coeff_1_4096;
407 coeff[7] = state->table[bw].coeff_2_4k;
408 bfsfcw_fftinx_ratio = state->table[bw].bfsfcw_fftinx_ratio;
409 fftinx_bfsfcw_ratio = state->table[bw].fftinx_bfsfcw_ratio;
410
411 /* ADC multiplier */
412 ret = it913x_read_reg_u8(state, ADC_X_2);
413 if (ret < 0)
414 return -EINVAL;
415
416 adcmultiplier = ret;
417
418 count = 0;
419
420 /* Build Buffer for COEFF Registers */
421 for (i = 0; i < 8; i++) {
422 if (adcmultiplier == 1)
423 coeff[i] /= 2;
424 buffer[count++] = (coeff[i] >> 24) & 0x3;
425 buffer[count++] = (coeff[i] >> 16) & 0xff;
426 buffer[count++] = (coeff[i] >> 8) & 0xff;
427 buffer[count++] = coeff[i] & 0xff;
428 }
429
430 /* bfsfcw_fftinx_ratio register 0x21-0x22 */
431 buffer[count++] = bfsfcw_fftinx_ratio & 0xff;
432 buffer[count++] = (bfsfcw_fftinx_ratio >> 8) & 0xff;
433 /* fftinx_bfsfcw_ratio register 0x23-0x24 */
434 buffer[count++] = fftinx_bfsfcw_ratio & 0xff;
435 buffer[count++] = (fftinx_bfsfcw_ratio >> 8) & 0xff;
436 /* start at COEFF_1_2048 and write through to fftinx_bfsfcw_ratio*/
437 ret = it913x_write(state, PRO_DMOD, COEFF_1_2048, buffer, count);
438
439 for (i = 0; i < 42; i += 8)
440 debug_data_snipet(0x1, "Buffer", &buffer[i]);
441
442 return ret;
443}
444
445
446
447static int it913x_fe_read_status(struct dvb_frontend *fe, fe_status_t *status)
448{
449 struct it913x_fe_state *state = fe->demodulator_priv;
450 int ret, i;
451 fe_status_t old_status = state->it913x_status;
452 *status = 0;
453
454 if (state->it913x_status == 0) {
455 ret = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
456 if (ret == 0x1) {
457 *status |= FE_HAS_SIGNAL;
458 for (i = 0; i < 40; i++) {
459 ret = it913x_read_reg_u8(state, MP2IF_SYNC_LK);
460 if (ret == 0x1)
461 break;
462 msleep(25);
463 }
464 if (ret == 0x1)
465 *status |= FE_HAS_CARRIER
466 | FE_HAS_VITERBI
467 | FE_HAS_SYNC;
468 state->it913x_status = *status;
469 }
470 }
471
472 if (state->it913x_status & FE_HAS_SYNC) {
473 ret = it913x_read_reg_u8(state, TPSD_LOCK);
474 if (ret == 0x1)
475 *status |= FE_HAS_LOCK
476 | state->it913x_status;
477 else
478 state->it913x_status = 0;
479 if (old_status != state->it913x_status)
480 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, ret);
481 }
482
483 return 0;
484}
485
486static int it913x_fe_read_signal_strength(struct dvb_frontend *fe,
487 u16 *strength)
488{
489 struct it913x_fe_state *state = fe->demodulator_priv;
490 int ret = it913x_read_reg_u8(state, SIGNAL_LEVEL);
491 /*SIGNAL_LEVEL always returns 100%! so using FE_HAS_SIGNAL as switch*/
492 if (state->it913x_status & FE_HAS_SIGNAL)
493 ret = (ret * 0xff) / 0x64;
494 else
495 ret = 0x0;
496 ret |= ret << 0x8;
497 *strength = ret;
498 return 0;
499}
500
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300501static int it913x_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300502{
503 struct it913x_fe_state *state = fe->demodulator_priv;
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300504 int ret;
505 u8 reg[3];
506 u32 snr_val, snr_min, snr_max;
507 u32 temp;
508
509 ret = it913x_read_reg(state, 0x2c, reg, sizeof(reg));
510
511 snr_val = (u32)(reg[2] << 16) | (reg[1] < 8) | reg[0];
512
513 ret |= it913x_read_reg(state, 0xf78b, reg, 1);
514 if (reg[0])
515 snr_val /= reg[0];
516
517 if (state->transmission_mode == TRANSMISSION_MODE_2K)
518 snr_val *= 4;
519 else if (state->transmission_mode == TRANSMISSION_MODE_4K)
520 snr_val *= 2;
521
522 if (state->constellation == QPSK) {
523 snr_min = 0xb4711;
524 snr_max = 0x191451;
525 } else if (state->constellation == QAM_16) {
526 snr_min = 0x4f0d5;
527 snr_max = 0xc7925;
528 } else if (state->constellation == QAM_64) {
529 snr_min = 0x256d0;
530 snr_max = 0x626be;
531 } else
532 return -EINVAL;
533
534 if (snr_val < snr_min)
535 *snr = 0;
536 else if (snr_val < snr_max) {
537 temp = (snr_val - snr_min) >> 5;
538 temp *= 0xffff;
539 temp /= (snr_max - snr_min) >> 5;
540 *snr = (u16)temp;
541 } else
542 *snr = 0xffff;
543
544 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300545}
546
547static int it913x_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
548{
549 *ber = 0;
550 return 0;
551}
552
553static int it913x_fe_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
554{
555 *ucblocks = 0;
556 return 0;
557}
558
559static int it913x_fe_get_frontend(struct dvb_frontend *fe,
560 struct dvb_frontend_parameters *p)
561{
562 struct it913x_fe_state *state = fe->demodulator_priv;
563 int ret;
564 u8 reg[8];
565
566 ret = it913x_read_reg(state, REG_TPSD_TX_MODE, reg, sizeof(reg));
567
568 if (reg[3] < 3)
569 p->u.ofdm.constellation = fe_con[reg[3]];
570
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300571 state->constellation = p->u.ofdm.constellation;
572
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300573 if (reg[0] < 3)
574 p->u.ofdm.transmission_mode = fe_mode[reg[0]];
575
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300576 state->transmission_mode = p->u.ofdm.transmission_mode;
577
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300578 if (reg[1] < 4)
579 p->u.ofdm.guard_interval = fe_gi[reg[1]];
580
581 if (reg[2] < 4)
582 p->u.ofdm.hierarchy_information = fe_hi[reg[2]];
583
584 p->u.ofdm.code_rate_HP = (reg[6] < 6) ? fe_code[reg[6]] : FEC_NONE;
585 p->u.ofdm.code_rate_LP = (reg[7] < 6) ? fe_code[reg[7]] : FEC_NONE;
586
587 return 0;
588}
589
590static int it913x_fe_set_frontend(struct dvb_frontend *fe,
591 struct dvb_frontend_parameters *p)
592{
593 struct it913x_fe_state *state = fe->demodulator_priv;
594 int ret, i;
595 u8 empty_ch, last_ch;
596
597 state->it913x_status = 0;
598
599 /* Set bw*/
600 ret = it913x_fe_select_bw(state, p->u.ofdm.bandwidth,
601 state->adcFrequency);
602
603 /* Training Mode Off */
604 ret = it913x_write_reg(state, PRO_LINK, TRAINING_MODE, 0x0);
605
606 /* Clear Empty Channel */
607 ret = it913x_write_reg(state, PRO_DMOD, EMPTY_CHANNEL_STATUS, 0x0);
608
609 /* Clear bits */
610 ret = it913x_write_reg(state, PRO_DMOD, MP2IF_SYNC_LK, 0x0);
611 /* LED on */
612 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
613 /* Select Band*/
614 if ((p->frequency >= 51000000) && (p->frequency <= 230000000))
615 i = 0;
616 else if ((p->frequency >= 350000000) && (p->frequency <= 900000000))
617 i = 1;
618 else if ((p->frequency >= 1450000000) && (p->frequency <= 1680000000))
619 i = 2;
620 else
621 return -EOPNOTSUPP;
622
623 ret = it913x_write_reg(state, PRO_DMOD, FREE_BAND, i);
624
625 deb_info("Frontend Set Tuner Type %02x", state->tuner_type);
626 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300627 case IT9135_38:
628 case IT9135_51:
629 case IT9135_52:
630 case IT9135_60:
631 case IT9135_61:
632 case IT9135_62:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300633 ret = it9137_set_tuner(state,
634 p->u.ofdm.bandwidth, p->frequency);
635 break;
636 default:
637 if (fe->ops.tuner_ops.set_params) {
638 fe->ops.tuner_ops.set_params(fe, p);
639 if (fe->ops.i2c_gate_ctrl)
640 fe->ops.i2c_gate_ctrl(fe, 0);
641 }
642 break;
643 }
644 /* LED off */
645 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
646 /* Trigger ofsm */
647 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
648 last_ch = 2;
649 for (i = 0; i < 40; ++i) {
650 empty_ch = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
651 if (last_ch == 1 && empty_ch == 1)
652 break;
653 if (last_ch == 2 && empty_ch == 2)
654 return 0;
655 last_ch = empty_ch;
656 msleep(25);
657 }
658 for (i = 0; i < 40; ++i) {
659 if (it913x_read_reg_u8(state, D_TPSD_LOCK) == 1)
660 break;
661 msleep(25);
662 }
663
664 state->frequency = p->frequency;
665 return 0;
666}
667
668static int it913x_fe_suspend(struct it913x_fe_state *state)
669{
670 int ret, i;
671 u8 b;
672
673 ret = it913x_write_reg(state, PRO_DMOD, SUSPEND_FLAG, 0x1);
674
675 ret |= it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
676
677 for (i = 0; i < 128; i++) {
678 ret = it913x_read_reg(state, SUSPEND_FLAG, &b, 1);
679 if (ret < 0)
Malcolm Priestleye3052882011-10-01 09:24:16 -0300680 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300681 if (b == 0)
682 break;
683
684 }
685
686 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x8);
687 /* Turn LED off */
Malcolm Priestleye3052882011-10-01 09:24:16 -0300688 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300689
Malcolm Priestleye3052882011-10-01 09:24:16 -0300690 ret |= it913x_fe_script_loader(state, it9137_tuner_off);
691
692 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300693}
694
Malcolm Priestleye3052882011-10-01 09:24:16 -0300695/* Power sequence */
696/* Power Up Tuner on -> Frontend suspend off -> Tuner clk on */
697/* Power Down Frontend suspend on -> Tuner clk off -> Tuner off */
698
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300699static int it913x_fe_sleep(struct dvb_frontend *fe)
700{
701 struct it913x_fe_state *state = fe->demodulator_priv;
702 return it913x_fe_suspend(state);
703}
704
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300705static u32 compute_div(u32 a, u32 b, u32 x)
706{
707 u32 res = 0;
708 u32 c = 0;
709 u32 i = 0;
710
711 if (a > b) {
712 c = a / b;
713 a = a - c * b;
714 }
715
716 for (i = 0; i < x; i++) {
717 if (a >= b) {
718 res += 1;
719 a -= b;
720 }
721 a <<= 1;
722 res <<= 1;
723 }
724
725 res = (c << x) + res;
726
727 return res;
728}
729
730static int it913x_fe_start(struct it913x_fe_state *state)
731{
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300732 struct it913xset *set_lna;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300733 struct it913xset *set_mode;
734 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300735 u8 adf = (state->config->adf & 0xf);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300736 u32 adc, xtal;
737 u8 b[4];
738
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300739 if (state->config->chip_ver == 1)
740 ret = it913x_init_tuner(state);
tvboxspy7c2808e2011-09-21 19:06:58 -0300741
tvboxspy2b3c13e2011-10-31 12:06:34 -0300742 if (adf < 10) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300743 state->crystalFrequency = fe_clockTable[adf].xtal ;
744 state->table = fe_clockTable[adf].table;
745 state->adcFrequency = state->table->adcFrequency;
746
747 adc = compute_div(state->adcFrequency, 1000000ul, 19ul);
748 xtal = compute_div(state->crystalFrequency, 1000000ul, 19ul);
749
750 } else
751 return -EINVAL;
752
753 deb_info("Xtal Freq :%d Adc Freq :%d Adc %08x Xtal %08x",
754 state->crystalFrequency, state->adcFrequency, adc, xtal);
755
756 /* Set LED indicator on GPIOH3 */
757 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_EN, 0x1);
758 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_ON, 0x1);
759 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
760
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300761 ret |= it913x_write_reg(state, PRO_LINK, 0xf641, state->tuner_type);
762 ret |= it913x_write_reg(state, PRO_DMOD, 0xf5ca, 0x01);
763 ret |= it913x_write_reg(state, PRO_DMOD, 0xf715, 0x01);
764
765 b[0] = xtal & 0xff;
766 b[1] = (xtal >> 8) & 0xff;
767 b[2] = (xtal >> 16) & 0xff;
768 b[3] = (xtal >> 24);
769 ret |= it913x_write(state, PRO_DMOD, XTAL_CLK, b , 4);
770
771 b[0] = adc & 0xff;
772 b[1] = (adc >> 8) & 0xff;
773 b[2] = (adc >> 16) & 0xff;
774 ret |= it913x_write(state, PRO_DMOD, ADC_FREQ, b, 3);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300775 if (ret < 0)
776 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300777
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300778 /* v1 or v2 tuner script */
779 if (state->config->chip_ver > 1)
780 ret = it913x_fe_script_loader(state, it9135_v2);
781 else
782 ret = it913x_fe_script_loader(state, it9135_v1);
783 if (ret < 0)
784 return ret;
785
786 /* LNA Scripts */
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300787 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300788 case IT9135_51:
789 set_lna = it9135_51;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300790 break;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300791 case IT9135_52:
792 set_lna = it9135_52;
793 break;
794 case IT9135_60:
795 set_lna = it9135_60;
796 break;
797 case IT9135_61:
798 set_lna = it9135_61;
799 break;
800 case IT9135_62:
801 set_lna = it9135_62;
802 break;
803 case IT9135_38:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300804 default:
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300805 set_lna = it9135_38;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300806 }
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300807 ret = it913x_fe_script_loader(state, set_lna);
808 if (ret < 0)
809 return ret;
tvboxspy7c2808e2011-09-21 19:06:58 -0300810
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300811 if (state->config->chip_ver == 2) {
812 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x1);
813 ret |= it913x_write_reg(state, PRO_LINK, PADODPU, 0x0);
814 ret |= it913x_write_reg(state, PRO_LINK, AGC_O_D, 0x0);
815 ret |= it913x_init_tuner(state);
816 }
817 if (ret < 0)
818 return -ENODEV;
819
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300820 /* Always solo frontend */
821 set_mode = set_solo_fe;
822 ret |= it913x_fe_script_loader(state, set_mode);
823
824 ret |= it913x_fe_suspend(state);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300825 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300826}
827
828static int it913x_fe_init(struct dvb_frontend *fe)
829{
830 struct it913x_fe_state *state = fe->demodulator_priv;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300831 int ret = 0;
Malcolm Priestleye3052882011-10-01 09:24:16 -0300832 /* Power Up Tuner - common all versions */
833 ret = it913x_write_reg(state, PRO_DMOD, 0xec40, 0x1);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300834
Malcolm Priestleye3052882011-10-01 09:24:16 -0300835 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x0);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300836
837 ret |= it913x_fe_script_loader(state, init_1);
838
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300839 ret |= it913x_write_reg(state, PRO_DMOD, 0xfba8, 0x0);
Malcolm Priestleye3052882011-10-01 09:24:16 -0300840
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300841 return (ret < 0) ? -ENODEV : 0;
842}
843
844static void it913x_fe_release(struct dvb_frontend *fe)
845{
846 struct it913x_fe_state *state = fe->demodulator_priv;
847 kfree(state);
848}
849
850static struct dvb_frontend_ops it913x_fe_ofdm_ops;
851
852struct dvb_frontend *it913x_fe_attach(struct i2c_adapter *i2c_adap,
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300853 u8 i2c_addr, struct ite_config *config)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300854{
855 struct it913x_fe_state *state = NULL;
856 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300857
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300858 /* allocate memory for the internal state */
859 state = kzalloc(sizeof(struct it913x_fe_state), GFP_KERNEL);
860 if (state == NULL)
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300861 return NULL;
862 if (config == NULL)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300863 goto error;
864
865 state->i2c_adap = i2c_adap;
866 state->i2c_addr = i2c_addr;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300867 state->config = config;
868
869 switch (state->config->tuner_id_0) {
870 case IT9135_51:
871 case IT9135_52:
872 case IT9135_60:
873 case IT9135_61:
874 case IT9135_62:
875 state->tuner_type = state->config->tuner_id_0;
876 break;
877 default:
878 case IT9135_38:
879 state->tuner_type = IT9135_38;
880 }
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300881
882 ret = it913x_fe_start(state);
883 if (ret < 0)
884 goto error;
885
886
887 /* create dvb_frontend */
888 memcpy(&state->frontend.ops, &it913x_fe_ofdm_ops,
889 sizeof(struct dvb_frontend_ops));
890 state->frontend.demodulator_priv = state;
891
892 return &state->frontend;
893error:
894 kfree(state);
895 return NULL;
896}
897EXPORT_SYMBOL(it913x_fe_attach);
898
899static struct dvb_frontend_ops it913x_fe_ofdm_ops = {
900
901 .info = {
902 .name = "it913x-fe DVB-T",
903 .type = FE_OFDM,
904 .frequency_min = 51000000,
905 .frequency_max = 1680000000,
906 .frequency_stepsize = 62500,
907 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
908 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
909 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
910 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
911 FE_CAN_TRANSMISSION_MODE_AUTO |
912 FE_CAN_GUARD_INTERVAL_AUTO |
913 FE_CAN_HIERARCHY_AUTO,
914 },
915
916 .release = it913x_fe_release,
917
918 .init = it913x_fe_init,
919 .sleep = it913x_fe_sleep,
920
921 .set_frontend = it913x_fe_set_frontend,
922 .get_frontend = it913x_fe_get_frontend,
923
924 .read_status = it913x_fe_read_status,
925 .read_signal_strength = it913x_fe_read_signal_strength,
926 .read_snr = it913x_fe_read_snr,
927 .read_ber = it913x_fe_read_ber,
928 .read_ucblocks = it913x_fe_read_ucblocks,
929};
930
931MODULE_DESCRIPTION("it913x Frontend and it9137 tuner");
932MODULE_AUTHOR("Malcolm Priestley tvboxspy@gmail.com");
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300933MODULE_VERSION("1.10");
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300934MODULE_LICENSE("GPL");