blob: 8088e62a20285736fec5e6d18696eacdeef84a30 [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));
Malcolm Priestleyed942c52011-11-27 18:14:05 -030049#define info(format, arg...) \
50 printk(KERN_INFO "it913x-fe: " format "\n" , ## arg)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030051
52struct it913x_fe_state {
53 struct dvb_frontend frontend;
54 struct i2c_adapter *i2c_adap;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -030055 struct ite_config *config;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030056 u8 i2c_addr;
57 u32 frequency;
Malcolm Priestley3339a5b2011-11-06 11:19:14 -030058 fe_modulation_t constellation;
59 fe_transmit_mode_t transmission_mode;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030060 u32 crystalFrequency;
61 u32 adcFrequency;
62 u8 tuner_type;
63 struct adctable *table;
64 fe_status_t it913x_status;
tvboxspy7c2808e2011-09-21 19:06:58 -030065 u16 tun_xtal;
66 u8 tun_fdiv;
67 u8 tun_clk_mode;
68 u32 tun_fn_min;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030069};
70
71static int it913x_read_reg(struct it913x_fe_state *state,
72 u32 reg, u8 *data, u8 count)
73{
74 int ret;
75 u8 pro = PRO_DMOD; /* All reads from demodulator */
76 u8 b[4];
77 struct i2c_msg msg[2] = {
78 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
79 .buf = b, .len = sizeof(b) },
80 { .addr = state->i2c_addr + (pro << 1), .flags = I2C_M_RD,
81 .buf = data, .len = count }
82 };
83 b[0] = (u8) reg >> 24;
84 b[1] = (u8)(reg >> 16) & 0xff;
85 b[2] = (u8)(reg >> 8) & 0xff;
86 b[3] = (u8) reg & 0xff;
87
88 ret = i2c_transfer(state->i2c_adap, msg, 2);
89
90 return ret;
91}
92
93static int it913x_read_reg_u8(struct it913x_fe_state *state, u32 reg)
94{
95 int ret;
96 u8 b[1];
97 ret = it913x_read_reg(state, reg, &b[0], sizeof(b));
98 return (ret < 0) ? -ENODEV : b[0];
99}
100
101static int it913x_write(struct it913x_fe_state *state,
102 u8 pro, u32 reg, u8 buf[], u8 count)
103{
104 u8 b[256];
105 struct i2c_msg msg[1] = {
106 { .addr = state->i2c_addr + (pro << 1), .flags = 0,
107 .buf = b, .len = count + 4 }
108 };
109 int ret;
110
111 b[0] = (u8) reg >> 24;
112 b[1] = (u8)(reg >> 16) & 0xff;
113 b[2] = (u8)(reg >> 8) & 0xff;
114 b[3] = (u8) reg & 0xff;
115 memcpy(&b[4], buf, count);
116
117 ret = i2c_transfer(state->i2c_adap, msg, 1);
118
119 if (ret < 0)
120 return -EIO;
121
122 return 0;
123}
124
125static int it913x_write_reg(struct it913x_fe_state *state,
126 u8 pro, u32 reg, u32 data)
127{
128 int ret;
129 u8 b[4];
130 u8 s;
131
132 b[0] = data >> 24;
133 b[1] = (data >> 16) & 0xff;
134 b[2] = (data >> 8) & 0xff;
135 b[3] = data & 0xff;
136 /* expand write as needed */
137 if (data < 0x100)
138 s = 3;
139 else if (data < 0x1000)
140 s = 2;
141 else if (data < 0x100000)
142 s = 1;
143 else
144 s = 0;
145
146 ret = it913x_write(state, pro, reg, &b[s], sizeof(b) - s);
147
148 return ret;
149}
150
151static int it913x_fe_script_loader(struct it913x_fe_state *state,
152 struct it913xset *loadscript)
153{
154 int ret, i;
155 if (loadscript == NULL)
156 return -EINVAL;
157
158 for (i = 0; i < 1000; ++i) {
159 if (loadscript[i].pro == 0xff)
160 break;
161 ret = it913x_write(state, loadscript[i].pro,
162 loadscript[i].address,
163 loadscript[i].reg, loadscript[i].count);
164 if (ret < 0)
165 return -ENODEV;
166 }
167 return 0;
168}
169
tvboxspy7c2808e2011-09-21 19:06:58 -0300170static int it913x_init_tuner(struct it913x_fe_state *state)
171{
172 int ret, i, reg;
173 u8 val, nv_val;
174 u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
175 u8 b[2];
176
177 reg = it913x_read_reg_u8(state, 0xec86);
178 switch (reg) {
179 case 0:
180 state->tun_clk_mode = reg;
181 state->tun_xtal = 2000;
182 state->tun_fdiv = 3;
183 val = 16;
184 break;
185 case -ENODEV:
186 return -ENODEV;
187 case 1:
188 default:
189 state->tun_clk_mode = reg;
190 state->tun_xtal = 640;
191 state->tun_fdiv = 1;
192 val = 6;
193 break;
194 }
195
196 reg = it913x_read_reg_u8(state, 0xed03);
197
198 if (reg < 0)
199 return -ENODEV;
200 else if (reg < sizeof(nv))
201 nv_val = nv[reg];
202 else
203 nv_val = 2;
204
205 for (i = 0; i < 50; i++) {
206 ret = it913x_read_reg(state, 0xed23, &b[0], sizeof(b));
207 reg = (b[1] << 8) + b[0];
208 if (reg > 0)
209 break;
210 if (ret < 0)
211 return -ENODEV;
212 udelay(2000);
213 }
214 state->tun_fn_min = state->tun_xtal * reg;
215 state->tun_fn_min /= (state->tun_fdiv * nv_val);
216 deb_info("Tuner fn_min %d", state->tun_fn_min);
217
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300218 if (state->config->chip_ver > 1)
219 msleep(50);
220 else {
221 for (i = 0; i < 50; i++) {
222 reg = it913x_read_reg_u8(state, 0xec82);
223 if (reg > 0)
224 break;
225 if (reg < 0)
226 return -ENODEV;
227 udelay(2000);
228 }
tvboxspy7c2808e2011-09-21 19:06:58 -0300229 }
230
231 return it913x_write_reg(state, PRO_DMOD, 0xed81, val);
232}
233
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300234static int it9137_set_tuner(struct it913x_fe_state *state,
235 enum fe_bandwidth bandwidth, u32 frequency_m)
236{
237 struct it913xset *set_tuner = set_it9137_template;
tvboxspy7c2808e2011-09-21 19:06:58 -0300238 int ret, reg;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300239 u32 frequency = frequency_m / 1000;
tvboxspy7c2808e2011-09-21 19:06:58 -0300240 u32 freq, temp_f, tmp;
241 u16 iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300242 u16 n_div;
243 u8 n;
244 u8 l_band;
245 u8 lna_band;
246 u8 bw;
247
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300248 if (state->config->firmware_ver == 1)
249 set_tuner = set_it9135_template;
250 else
251 set_tuner = set_it9137_template;
252
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300253 deb_info("Tuner Frequency %d Bandwidth %d", frequency, bandwidth);
254
255 if (frequency >= 51000 && frequency <= 440000) {
256 l_band = 0;
257 lna_band = 0;
258 } else if (frequency > 440000 && frequency <= 484000) {
259 l_band = 1;
260 lna_band = 1;
261 } else if (frequency > 484000 && frequency <= 533000) {
262 l_band = 1;
263 lna_band = 2;
264 } else if (frequency > 533000 && frequency <= 587000) {
265 l_band = 1;
266 lna_band = 3;
267 } else if (frequency > 587000 && frequency <= 645000) {
268 l_band = 1;
269 lna_band = 4;
270 } else if (frequency > 645000 && frequency <= 710000) {
271 l_band = 1;
272 lna_band = 5;
273 } else if (frequency > 710000 && frequency <= 782000) {
274 l_band = 1;
275 lna_band = 6;
276 } else if (frequency > 782000 && frequency <= 860000) {
277 l_band = 1;
278 lna_band = 7;
279 } else if (frequency > 1450000 && frequency <= 1492000) {
280 l_band = 1;
281 lna_band = 0;
282 } else if (frequency > 1660000 && frequency <= 1685000) {
283 l_band = 1;
284 lna_band = 1;
285 } else
286 return -EINVAL;
287 set_tuner[0].reg[0] = lna_band;
288
289 if (bandwidth == BANDWIDTH_5_MHZ)
290 bw = 0;
291 else if (bandwidth == BANDWIDTH_6_MHZ)
292 bw = 2;
293 else if (bandwidth == BANDWIDTH_7_MHZ)
294 bw = 4;
295 else if (bandwidth == BANDWIDTH_8_MHZ)
296 bw = 6;
297 else
298 bw = 6;
tvboxspy7c2808e2011-09-21 19:06:58 -0300299
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300300 set_tuner[1].reg[0] = bw;
301 set_tuner[2].reg[0] = 0xa0 | (l_band << 3);
302
tvboxspy7c2808e2011-09-21 19:06:58 -0300303 if (frequency > 53000 && frequency <= 74000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300304 n_div = 48;
305 n = 0;
306 } else if (frequency > 74000 && frequency <= 111000) {
307 n_div = 32;
308 n = 1;
309 } else if (frequency > 111000 && frequency <= 148000) {
310 n_div = 24;
311 n = 2;
312 } else if (frequency > 148000 && frequency <= 222000) {
313 n_div = 16;
314 n = 3;
315 } else if (frequency > 222000 && frequency <= 296000) {
316 n_div = 12;
317 n = 4;
318 } else if (frequency > 296000 && frequency <= 445000) {
319 n_div = 8;
320 n = 5;
tvboxspy7c2808e2011-09-21 19:06:58 -0300321 } else if (frequency > 445000 && frequency <= state->tun_fn_min) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300322 n_div = 6;
323 n = 6;
tvboxspy7c2808e2011-09-21 19:06:58 -0300324 } else if (frequency > state->tun_fn_min && frequency <= 950000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300325 n_div = 4;
326 n = 7;
327 } else if (frequency > 1450000 && frequency <= 1680000) {
328 n_div = 2;
329 n = 0;
330 } else
331 return -EINVAL;
332
tvboxspy7c2808e2011-09-21 19:06:58 -0300333 reg = it913x_read_reg_u8(state, 0xed81);
334 iqik_m_cal = (u16)reg * n_div;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300335
tvboxspy7c2808e2011-09-21 19:06:58 -0300336 if (reg < 0x20) {
337 if (state->tun_clk_mode == 0)
338 iqik_m_cal = (iqik_m_cal * 9) >> 5;
339 else
340 iqik_m_cal >>= 1;
341 } else {
342 iqik_m_cal = 0x40 - iqik_m_cal;
343 if (state->tun_clk_mode == 0)
344 iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
345 else
346 iqik_m_cal = ~(iqik_m_cal >> 1);
347 }
348
349 temp_f = frequency * (u32)n_div * (u32)state->tun_fdiv;
350 freq = temp_f / state->tun_xtal;
351 tmp = freq * state->tun_xtal;
352
353 if ((temp_f - tmp) >= (state->tun_xtal >> 1))
354 freq++;
355
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300356 freq += (u32) n << 13;
tvboxspy7c2808e2011-09-21 19:06:58 -0300357 /* Frequency OMEGA_IQIK_M_CAL_MID*/
358 temp_f = freq + (u32)iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300359
tvboxspy7c2808e2011-09-21 19:06:58 -0300360 set_tuner[3].reg[0] = temp_f & 0xff;
361 set_tuner[4].reg[0] = (temp_f >> 8) & 0xff;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300362
tvboxspy7c2808e2011-09-21 19:06:58 -0300363 deb_info("High Frequency = %04x", temp_f);
364
365 /* Lower frequency */
366 set_tuner[5].reg[0] = freq & 0xff;
367 set_tuner[6].reg[0] = (freq >> 8) & 0xff;
368
369 deb_info("low Frequency = %04x", freq);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300370
371 ret = it913x_fe_script_loader(state, set_tuner);
372
373 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300374}
375
376static int it913x_fe_select_bw(struct it913x_fe_state *state,
377 enum fe_bandwidth bandwidth, u32 adcFrequency)
378{
379 int ret, i;
380 u8 buffer[256];
381 u32 coeff[8];
382 u16 bfsfcw_fftinx_ratio;
383 u16 fftinx_bfsfcw_ratio;
384 u8 count;
385 u8 bw;
386 u8 adcmultiplier;
387
388 deb_info("Bandwidth %d Adc %d", bandwidth, adcFrequency);
389
390 if (bandwidth == BANDWIDTH_5_MHZ)
391 bw = 3;
392 else if (bandwidth == BANDWIDTH_6_MHZ)
393 bw = 0;
394 else if (bandwidth == BANDWIDTH_7_MHZ)
395 bw = 1;
396 else if (bandwidth == BANDWIDTH_8_MHZ)
397 bw = 2;
398 else
399 bw = 2;
400
401 ret = it913x_write_reg(state, PRO_DMOD, REG_BW, bw);
402
403 if (state->table == NULL)
404 return -EINVAL;
405
406 /* In write order */
407 coeff[0] = state->table[bw].coeff_1_2048;
408 coeff[1] = state->table[bw].coeff_2_2k;
409 coeff[2] = state->table[bw].coeff_1_8191;
410 coeff[3] = state->table[bw].coeff_1_8192;
411 coeff[4] = state->table[bw].coeff_1_8193;
412 coeff[5] = state->table[bw].coeff_2_8k;
413 coeff[6] = state->table[bw].coeff_1_4096;
414 coeff[7] = state->table[bw].coeff_2_4k;
415 bfsfcw_fftinx_ratio = state->table[bw].bfsfcw_fftinx_ratio;
416 fftinx_bfsfcw_ratio = state->table[bw].fftinx_bfsfcw_ratio;
417
418 /* ADC multiplier */
419 ret = it913x_read_reg_u8(state, ADC_X_2);
420 if (ret < 0)
421 return -EINVAL;
422
423 adcmultiplier = ret;
424
425 count = 0;
426
427 /* Build Buffer for COEFF Registers */
428 for (i = 0; i < 8; i++) {
429 if (adcmultiplier == 1)
430 coeff[i] /= 2;
431 buffer[count++] = (coeff[i] >> 24) & 0x3;
432 buffer[count++] = (coeff[i] >> 16) & 0xff;
433 buffer[count++] = (coeff[i] >> 8) & 0xff;
434 buffer[count++] = coeff[i] & 0xff;
435 }
436
437 /* bfsfcw_fftinx_ratio register 0x21-0x22 */
438 buffer[count++] = bfsfcw_fftinx_ratio & 0xff;
439 buffer[count++] = (bfsfcw_fftinx_ratio >> 8) & 0xff;
440 /* fftinx_bfsfcw_ratio register 0x23-0x24 */
441 buffer[count++] = fftinx_bfsfcw_ratio & 0xff;
442 buffer[count++] = (fftinx_bfsfcw_ratio >> 8) & 0xff;
443 /* start at COEFF_1_2048 and write through to fftinx_bfsfcw_ratio*/
444 ret = it913x_write(state, PRO_DMOD, COEFF_1_2048, buffer, count);
445
446 for (i = 0; i < 42; i += 8)
447 debug_data_snipet(0x1, "Buffer", &buffer[i]);
448
449 return ret;
450}
451
452
453
454static int it913x_fe_read_status(struct dvb_frontend *fe, fe_status_t *status)
455{
456 struct it913x_fe_state *state = fe->demodulator_priv;
457 int ret, i;
458 fe_status_t old_status = state->it913x_status;
459 *status = 0;
460
461 if (state->it913x_status == 0) {
462 ret = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
463 if (ret == 0x1) {
464 *status |= FE_HAS_SIGNAL;
465 for (i = 0; i < 40; i++) {
466 ret = it913x_read_reg_u8(state, MP2IF_SYNC_LK);
467 if (ret == 0x1)
468 break;
469 msleep(25);
470 }
471 if (ret == 0x1)
472 *status |= FE_HAS_CARRIER
473 | FE_HAS_VITERBI
474 | FE_HAS_SYNC;
475 state->it913x_status = *status;
476 }
477 }
478
479 if (state->it913x_status & FE_HAS_SYNC) {
480 ret = it913x_read_reg_u8(state, TPSD_LOCK);
481 if (ret == 0x1)
482 *status |= FE_HAS_LOCK
483 | state->it913x_status;
484 else
485 state->it913x_status = 0;
486 if (old_status != state->it913x_status)
487 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, ret);
488 }
489
490 return 0;
491}
492
493static int it913x_fe_read_signal_strength(struct dvb_frontend *fe,
494 u16 *strength)
495{
496 struct it913x_fe_state *state = fe->demodulator_priv;
497 int ret = it913x_read_reg_u8(state, SIGNAL_LEVEL);
498 /*SIGNAL_LEVEL always returns 100%! so using FE_HAS_SIGNAL as switch*/
499 if (state->it913x_status & FE_HAS_SIGNAL)
500 ret = (ret * 0xff) / 0x64;
501 else
502 ret = 0x0;
503 ret |= ret << 0x8;
504 *strength = ret;
505 return 0;
506}
507
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300508static int it913x_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300509{
510 struct it913x_fe_state *state = fe->demodulator_priv;
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300511 int ret;
512 u8 reg[3];
513 u32 snr_val, snr_min, snr_max;
514 u32 temp;
515
516 ret = it913x_read_reg(state, 0x2c, reg, sizeof(reg));
517
518 snr_val = (u32)(reg[2] << 16) | (reg[1] < 8) | reg[0];
519
520 ret |= it913x_read_reg(state, 0xf78b, reg, 1);
521 if (reg[0])
522 snr_val /= reg[0];
523
524 if (state->transmission_mode == TRANSMISSION_MODE_2K)
525 snr_val *= 4;
526 else if (state->transmission_mode == TRANSMISSION_MODE_4K)
527 snr_val *= 2;
528
529 if (state->constellation == QPSK) {
530 snr_min = 0xb4711;
531 snr_max = 0x191451;
532 } else if (state->constellation == QAM_16) {
533 snr_min = 0x4f0d5;
534 snr_max = 0xc7925;
535 } else if (state->constellation == QAM_64) {
536 snr_min = 0x256d0;
537 snr_max = 0x626be;
538 } else
539 return -EINVAL;
540
541 if (snr_val < snr_min)
542 *snr = 0;
543 else if (snr_val < snr_max) {
544 temp = (snr_val - snr_min) >> 5;
545 temp *= 0xffff;
546 temp /= (snr_max - snr_min) >> 5;
547 *snr = (u16)temp;
548 } else
549 *snr = 0xffff;
550
551 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300552}
553
554static int it913x_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
555{
556 *ber = 0;
557 return 0;
558}
559
560static int it913x_fe_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
561{
562 *ucblocks = 0;
563 return 0;
564}
565
566static int it913x_fe_get_frontend(struct dvb_frontend *fe,
567 struct dvb_frontend_parameters *p)
568{
569 struct it913x_fe_state *state = fe->demodulator_priv;
570 int ret;
571 u8 reg[8];
572
573 ret = it913x_read_reg(state, REG_TPSD_TX_MODE, reg, sizeof(reg));
574
575 if (reg[3] < 3)
576 p->u.ofdm.constellation = fe_con[reg[3]];
577
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300578 state->constellation = p->u.ofdm.constellation;
579
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300580 if (reg[0] < 3)
581 p->u.ofdm.transmission_mode = fe_mode[reg[0]];
582
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300583 state->transmission_mode = p->u.ofdm.transmission_mode;
584
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300585 if (reg[1] < 4)
586 p->u.ofdm.guard_interval = fe_gi[reg[1]];
587
588 if (reg[2] < 4)
589 p->u.ofdm.hierarchy_information = fe_hi[reg[2]];
590
591 p->u.ofdm.code_rate_HP = (reg[6] < 6) ? fe_code[reg[6]] : FEC_NONE;
592 p->u.ofdm.code_rate_LP = (reg[7] < 6) ? fe_code[reg[7]] : FEC_NONE;
593
594 return 0;
595}
596
597static int it913x_fe_set_frontend(struct dvb_frontend *fe,
598 struct dvb_frontend_parameters *p)
599{
600 struct it913x_fe_state *state = fe->demodulator_priv;
601 int ret, i;
602 u8 empty_ch, last_ch;
603
604 state->it913x_status = 0;
605
606 /* Set bw*/
607 ret = it913x_fe_select_bw(state, p->u.ofdm.bandwidth,
608 state->adcFrequency);
609
610 /* Training Mode Off */
611 ret = it913x_write_reg(state, PRO_LINK, TRAINING_MODE, 0x0);
612
613 /* Clear Empty Channel */
614 ret = it913x_write_reg(state, PRO_DMOD, EMPTY_CHANNEL_STATUS, 0x0);
615
616 /* Clear bits */
617 ret = it913x_write_reg(state, PRO_DMOD, MP2IF_SYNC_LK, 0x0);
618 /* LED on */
619 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
620 /* Select Band*/
621 if ((p->frequency >= 51000000) && (p->frequency <= 230000000))
622 i = 0;
623 else if ((p->frequency >= 350000000) && (p->frequency <= 900000000))
624 i = 1;
625 else if ((p->frequency >= 1450000000) && (p->frequency <= 1680000000))
626 i = 2;
627 else
628 return -EOPNOTSUPP;
629
630 ret = it913x_write_reg(state, PRO_DMOD, FREE_BAND, i);
631
632 deb_info("Frontend Set Tuner Type %02x", state->tuner_type);
633 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300634 case IT9135_38:
635 case IT9135_51:
636 case IT9135_52:
637 case IT9135_60:
638 case IT9135_61:
639 case IT9135_62:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300640 ret = it9137_set_tuner(state,
641 p->u.ofdm.bandwidth, p->frequency);
642 break;
643 default:
644 if (fe->ops.tuner_ops.set_params) {
645 fe->ops.tuner_ops.set_params(fe, p);
646 if (fe->ops.i2c_gate_ctrl)
647 fe->ops.i2c_gate_ctrl(fe, 0);
648 }
649 break;
650 }
651 /* LED off */
652 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
653 /* Trigger ofsm */
654 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
655 last_ch = 2;
656 for (i = 0; i < 40; ++i) {
657 empty_ch = it913x_read_reg_u8(state, EMPTY_CHANNEL_STATUS);
658 if (last_ch == 1 && empty_ch == 1)
659 break;
660 if (last_ch == 2 && empty_ch == 2)
661 return 0;
662 last_ch = empty_ch;
663 msleep(25);
664 }
665 for (i = 0; i < 40; ++i) {
666 if (it913x_read_reg_u8(state, D_TPSD_LOCK) == 1)
667 break;
668 msleep(25);
669 }
670
671 state->frequency = p->frequency;
672 return 0;
673}
674
675static int it913x_fe_suspend(struct it913x_fe_state *state)
676{
677 int ret, i;
678 u8 b;
679
680 ret = it913x_write_reg(state, PRO_DMOD, SUSPEND_FLAG, 0x1);
681
682 ret |= it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
683
684 for (i = 0; i < 128; i++) {
685 ret = it913x_read_reg(state, SUSPEND_FLAG, &b, 1);
686 if (ret < 0)
Malcolm Priestleye3052882011-10-01 09:24:16 -0300687 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300688 if (b == 0)
689 break;
690
691 }
692
693 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x8);
694 /* Turn LED off */
Malcolm Priestleye3052882011-10-01 09:24:16 -0300695 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300696
Malcolm Priestleye3052882011-10-01 09:24:16 -0300697 ret |= it913x_fe_script_loader(state, it9137_tuner_off);
698
699 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300700}
701
Malcolm Priestleye3052882011-10-01 09:24:16 -0300702/* Power sequence */
703/* Power Up Tuner on -> Frontend suspend off -> Tuner clk on */
704/* Power Down Frontend suspend on -> Tuner clk off -> Tuner off */
705
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300706static int it913x_fe_sleep(struct dvb_frontend *fe)
707{
708 struct it913x_fe_state *state = fe->demodulator_priv;
709 return it913x_fe_suspend(state);
710}
711
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300712static u32 compute_div(u32 a, u32 b, u32 x)
713{
714 u32 res = 0;
715 u32 c = 0;
716 u32 i = 0;
717
718 if (a > b) {
719 c = a / b;
720 a = a - c * b;
721 }
722
723 for (i = 0; i < x; i++) {
724 if (a >= b) {
725 res += 1;
726 a -= b;
727 }
728 a <<= 1;
729 res <<= 1;
730 }
731
732 res = (c << x) + res;
733
734 return res;
735}
736
737static int it913x_fe_start(struct it913x_fe_state *state)
738{
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300739 struct it913xset *set_lna;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300740 struct it913xset *set_mode;
741 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300742 u8 adf = (state->config->adf & 0xf);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300743 u32 adc, xtal;
744 u8 b[4];
745
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300746 if (state->config->chip_ver == 1)
747 ret = it913x_init_tuner(state);
tvboxspy7c2808e2011-09-21 19:06:58 -0300748
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300749 info("ADF table value :%02x", adf);
750
tvboxspy2b3c13e2011-10-31 12:06:34 -0300751 if (adf < 10) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300752 state->crystalFrequency = fe_clockTable[adf].xtal ;
753 state->table = fe_clockTable[adf].table;
754 state->adcFrequency = state->table->adcFrequency;
755
756 adc = compute_div(state->adcFrequency, 1000000ul, 19ul);
757 xtal = compute_div(state->crystalFrequency, 1000000ul, 19ul);
758
759 } else
760 return -EINVAL;
761
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300762 /* Set LED indicator on GPIOH3 */
763 ret = it913x_write_reg(state, PRO_LINK, GPIOH3_EN, 0x1);
764 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_ON, 0x1);
765 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x1);
766
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300767 ret |= it913x_write_reg(state, PRO_LINK, 0xf641, state->tuner_type);
768 ret |= it913x_write_reg(state, PRO_DMOD, 0xf5ca, 0x01);
769 ret |= it913x_write_reg(state, PRO_DMOD, 0xf715, 0x01);
770
771 b[0] = xtal & 0xff;
772 b[1] = (xtal >> 8) & 0xff;
773 b[2] = (xtal >> 16) & 0xff;
774 b[3] = (xtal >> 24);
775 ret |= it913x_write(state, PRO_DMOD, XTAL_CLK, b , 4);
776
777 b[0] = adc & 0xff;
778 b[1] = (adc >> 8) & 0xff;
779 b[2] = (adc >> 16) & 0xff;
780 ret |= it913x_write(state, PRO_DMOD, ADC_FREQ, b, 3);
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300781
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300782 if (state->config->adc_x2)
783 ret |= it913x_write_reg(state, PRO_DMOD, ADC_X_2, 0x01);
784 b[0] = 0;
785 b[1] = 0;
786 b[2] = 0;
787 ret |= it913x_write(state, PRO_DMOD, 0x0029, b, 3);
788
789 info("Crystal Frequency :%d Adc Frequency :%d ADC X2: %02x",
790 state->crystalFrequency, state->adcFrequency,
791 state->config->adc_x2);
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300792 deb_info("Xtal value :%04x Adc value :%04x", xtal, adc);
793
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300794 if (ret < 0)
795 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300796
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300797 /* v1 or v2 tuner script */
798 if (state->config->chip_ver > 1)
799 ret = it913x_fe_script_loader(state, it9135_v2);
800 else
801 ret = it913x_fe_script_loader(state, it9135_v1);
802 if (ret < 0)
803 return ret;
804
805 /* LNA Scripts */
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300806 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300807 case IT9135_51:
808 set_lna = it9135_51;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300809 break;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300810 case IT9135_52:
811 set_lna = it9135_52;
812 break;
813 case IT9135_60:
814 set_lna = it9135_60;
815 break;
816 case IT9135_61:
817 set_lna = it9135_61;
818 break;
819 case IT9135_62:
820 set_lna = it9135_62;
821 break;
822 case IT9135_38:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300823 default:
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300824 set_lna = it9135_38;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300825 }
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300826 info("Tuner LNA type :%02x", state->tuner_type);
827
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300828 ret = it913x_fe_script_loader(state, set_lna);
829 if (ret < 0)
830 return ret;
tvboxspy7c2808e2011-09-21 19:06:58 -0300831
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300832 if (state->config->chip_ver == 2) {
833 ret = it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x1);
834 ret |= it913x_write_reg(state, PRO_LINK, PADODPU, 0x0);
835 ret |= it913x_write_reg(state, PRO_LINK, AGC_O_D, 0x0);
836 ret |= it913x_init_tuner(state);
837 }
838 if (ret < 0)
839 return -ENODEV;
840
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300841 /* Always solo frontend */
842 set_mode = set_solo_fe;
843 ret |= it913x_fe_script_loader(state, set_mode);
844
845 ret |= it913x_fe_suspend(state);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300846 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300847}
848
849static int it913x_fe_init(struct dvb_frontend *fe)
850{
851 struct it913x_fe_state *state = fe->demodulator_priv;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300852 int ret = 0;
Malcolm Priestleye3052882011-10-01 09:24:16 -0300853 /* Power Up Tuner - common all versions */
854 ret = it913x_write_reg(state, PRO_DMOD, 0xec40, 0x1);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300855
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300856 ret |= it913x_fe_script_loader(state, init_1);
857
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300858 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x0);
859
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300860 ret |= it913x_write_reg(state, PRO_DMOD, 0xfba8, 0x0);
Malcolm Priestleye3052882011-10-01 09:24:16 -0300861
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300862 return (ret < 0) ? -ENODEV : 0;
863}
864
865static void it913x_fe_release(struct dvb_frontend *fe)
866{
867 struct it913x_fe_state *state = fe->demodulator_priv;
868 kfree(state);
869}
870
871static struct dvb_frontend_ops it913x_fe_ofdm_ops;
872
873struct dvb_frontend *it913x_fe_attach(struct i2c_adapter *i2c_adap,
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300874 u8 i2c_addr, struct ite_config *config)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300875{
876 struct it913x_fe_state *state = NULL;
877 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300878
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300879 /* allocate memory for the internal state */
880 state = kzalloc(sizeof(struct it913x_fe_state), GFP_KERNEL);
881 if (state == NULL)
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300882 return NULL;
883 if (config == NULL)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300884 goto error;
885
886 state->i2c_adap = i2c_adap;
887 state->i2c_addr = i2c_addr;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300888 state->config = config;
889
890 switch (state->config->tuner_id_0) {
891 case IT9135_51:
892 case IT9135_52:
893 case IT9135_60:
894 case IT9135_61:
895 case IT9135_62:
896 state->tuner_type = state->config->tuner_id_0;
897 break;
898 default:
899 case IT9135_38:
900 state->tuner_type = IT9135_38;
901 }
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300902
903 ret = it913x_fe_start(state);
904 if (ret < 0)
905 goto error;
906
907
908 /* create dvb_frontend */
909 memcpy(&state->frontend.ops, &it913x_fe_ofdm_ops,
910 sizeof(struct dvb_frontend_ops));
911 state->frontend.demodulator_priv = state;
912
913 return &state->frontend;
914error:
915 kfree(state);
916 return NULL;
917}
918EXPORT_SYMBOL(it913x_fe_attach);
919
920static struct dvb_frontend_ops it913x_fe_ofdm_ops = {
921
922 .info = {
923 .name = "it913x-fe DVB-T",
924 .type = FE_OFDM,
925 .frequency_min = 51000000,
926 .frequency_max = 1680000000,
927 .frequency_stepsize = 62500,
928 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
929 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
930 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
931 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
932 FE_CAN_TRANSMISSION_MODE_AUTO |
933 FE_CAN_GUARD_INTERVAL_AUTO |
934 FE_CAN_HIERARCHY_AUTO,
935 },
936
937 .release = it913x_fe_release,
938
939 .init = it913x_fe_init,
940 .sleep = it913x_fe_sleep,
941
942 .set_frontend = it913x_fe_set_frontend,
943 .get_frontend = it913x_fe_get_frontend,
944
945 .read_status = it913x_fe_read_status,
946 .read_signal_strength = it913x_fe_read_signal_strength,
947 .read_snr = it913x_fe_read_snr,
948 .read_ber = it913x_fe_read_ber,
949 .read_ucblocks = it913x_fe_read_ucblocks,
950};
951
952MODULE_DESCRIPTION("it913x Frontend and it9137 tuner");
953MODULE_AUTHOR("Malcolm Priestley tvboxspy@gmail.com");
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300954MODULE_VERSION("1.12");
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300955MODULE_LICENSE("GPL");