blob: ccc36bf2deb403b3a7e769963e0659004d34f75d [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 Priestley82a05012012-01-03 06:28:32 -030069 u32 ucblocks;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -030070};
71
72static int it913x_read_reg(struct it913x_fe_state *state,
73 u32 reg, u8 *data, u8 count)
74{
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 }
83 };
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;
88
89 ret = i2c_transfer(state->i2c_adap, msg, 2);
90
91 return ret;
92}
93
94static int it913x_read_reg_u8(struct it913x_fe_state *state, u32 reg)
95{
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];
100}
101
102static int it913x_write(struct it913x_fe_state *state,
103 u8 pro, u32 reg, u8 buf[], u8 count)
104{
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 }
109 };
110 int ret;
111
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);
117
118 ret = i2c_transfer(state->i2c_adap, msg, 1);
119
120 if (ret < 0)
121 return -EIO;
122
123 return 0;
124}
125
126static int it913x_write_reg(struct it913x_fe_state *state,
127 u8 pro, u32 reg, u32 data)
128{
129 int ret;
130 u8 b[4];
131 u8 s;
132
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;
146
147 ret = it913x_write(state, pro, reg, &b[s], sizeof(b) - s);
148
149 return ret;
150}
151
152static int it913x_fe_script_loader(struct it913x_fe_state *state,
153 struct it913xset *loadscript)
154{
155 int ret, i;
156 if (loadscript == NULL)
157 return -EINVAL;
158
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;
167 }
168 return 0;
169}
170
tvboxspy7c2808e2011-09-21 19:06:58 -0300171static int it913x_init_tuner(struct it913x_fe_state *state)
172{
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];
177
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;
195 }
196
197 reg = it913x_read_reg_u8(state, 0xed03);
198
199 if (reg < 0)
200 return -ENODEV;
201 else if (reg < sizeof(nv))
202 nv_val = nv[reg];
203 else
204 nv_val = 2;
205
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);
214 }
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);
218
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300219 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);
229 }
tvboxspy7c2808e2011-09-21 19:06:58 -0300230 }
231
232 return it913x_write_reg(state, PRO_DMOD, 0xed81, val);
233}
234
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300235static int it9137_set_tuner(struct it913x_fe_state *state,
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300236 u32 bandwidth, u32 frequency_m)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300237{
238 struct it913xset *set_tuner = set_it9137_template;
tvboxspy7c2808e2011-09-21 19:06:58 -0300239 int ret, reg;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300240 u32 frequency = frequency_m / 1000;
tvboxspy7c2808e2011-09-21 19:06:58 -0300241 u32 freq, temp_f, tmp;
242 u16 iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300243 u16 n_div;
244 u8 n;
245 u8 l_band;
246 u8 lna_band;
247 u8 bw;
248
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300249 if (state->config->firmware_ver == 1)
250 set_tuner = set_it9135_template;
251 else
252 set_tuner = set_it9137_template;
253
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300254 deb_info("Tuner Frequency %d Bandwidth %d", frequency, bandwidth);
255
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;
289
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300290 switch (bandwidth) {
291 case 5000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300292 bw = 0;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300293 break;
294 case 6000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300295 bw = 2;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300296 break;
297 case 7000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300298 bw = 4;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300299 break;
300 default:
301 case 8000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300302 bw = 6;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300303 break;
304 }
tvboxspy7c2808e2011-09-21 19:06:58 -0300305
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300306 set_tuner[1].reg[0] = bw;
307 set_tuner[2].reg[0] = 0xa0 | (l_band << 3);
308
tvboxspy7c2808e2011-09-21 19:06:58 -0300309 if (frequency > 53000 && frequency <= 74000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300310 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;
tvboxspy7c2808e2011-09-21 19:06:58 -0300327 } else if (frequency > 445000 && frequency <= state->tun_fn_min) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300328 n_div = 6;
329 n = 6;
tvboxspy7c2808e2011-09-21 19:06:58 -0300330 } else if (frequency > state->tun_fn_min && frequency <= 950000) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300331 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;
338
tvboxspy7c2808e2011-09-21 19:06:58 -0300339 reg = it913x_read_reg_u8(state, 0xed81);
340 iqik_m_cal = (u16)reg * n_div;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300341
tvboxspy7c2808e2011-09-21 19:06:58 -0300342 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);
353 }
354
355 temp_f = frequency * (u32)n_div * (u32)state->tun_fdiv;
356 freq = temp_f / state->tun_xtal;
357 tmp = freq * state->tun_xtal;
358
359 if ((temp_f - tmp) >= (state->tun_xtal >> 1))
360 freq++;
361
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300362 freq += (u32) n << 13;
tvboxspy7c2808e2011-09-21 19:06:58 -0300363 /* Frequency OMEGA_IQIK_M_CAL_MID*/
364 temp_f = freq + (u32)iqik_m_cal;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300365
tvboxspy7c2808e2011-09-21 19:06:58 -0300366 set_tuner[3].reg[0] = temp_f & 0xff;
367 set_tuner[4].reg[0] = (temp_f >> 8) & 0xff;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300368
tvboxspy7c2808e2011-09-21 19:06:58 -0300369 deb_info("High Frequency = %04x", temp_f);
370
371 /* Lower frequency */
372 set_tuner[5].reg[0] = freq & 0xff;
373 set_tuner[6].reg[0] = (freq >> 8) & 0xff;
374
375 deb_info("low Frequency = %04x", freq);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300376
377 ret = it913x_fe_script_loader(state, set_tuner);
378
379 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300380}
381
382static int it913x_fe_select_bw(struct it913x_fe_state *state,
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300383 u32 bandwidth, u32 adcFrequency)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300384{
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;
393
394 deb_info("Bandwidth %d Adc %d", bandwidth, adcFrequency);
395
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300396 switch (bandwidth) {
397 case 5000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300398 bw = 3;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300399 break;
400 case 6000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300401 bw = 0;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300402 break;
403 case 7000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300404 bw = 1;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300405 break;
406 default:
407 case 8000000:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300408 bw = 2;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300409 break;
410 }
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300411 ret = it913x_write_reg(state, PRO_DMOD, REG_BW, bw);
412
413 if (state->table == NULL)
414 return -EINVAL;
415
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;
427
428 /* ADC multiplier */
429 ret = it913x_read_reg_u8(state, ADC_X_2);
430 if (ret < 0)
431 return -EINVAL;
432
433 adcmultiplier = ret;
434
435 count = 0;
436
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;
445 }
446
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);
455
456 for (i = 0; i < 42; i += 8)
457 debug_data_snipet(0x1, "Buffer", &buffer[i]);
458
459 return ret;
460}
461
462
463
464static int it913x_fe_read_status(struct dvb_frontend *fe, fe_status_t *status)
465{
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;
470
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);
480 }
481 if (ret == 0x1)
482 *status |= FE_HAS_CARRIER
483 | FE_HAS_VITERBI
484 | FE_HAS_SYNC;
485 state->it913x_status = *status;
486 }
487 }
488
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);
498 }
499
500 return 0;
501}
502
503static int it913x_fe_read_signal_strength(struct dvb_frontend *fe,
504 u16 *strength)
505{
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;
516}
517
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300518static int it913x_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300519{
520 struct it913x_fe_state *state = fe->demodulator_priv;
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300521 int ret;
522 u8 reg[3];
523 u32 snr_val, snr_min, snr_max;
524 u32 temp;
525
526 ret = it913x_read_reg(state, 0x2c, reg, sizeof(reg));
527
Malcolm Priestley9544e8a2012-01-10 19:45:31 -0300528 snr_val = (u32)(reg[2] << 16) | (reg[1] << 8) | reg[0];
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300529
530 ret |= it913x_read_reg(state, 0xf78b, reg, 1);
531 if (reg[0])
532 snr_val /= reg[0];
533
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;
538
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;
550
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;
560
561 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300562}
563
564static int it913x_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
565{
Malcolm Priestley82a05012012-01-03 06:28:32 -0300566 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];
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300573 return 0;
574}
575
576static int it913x_fe_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
577{
Malcolm Priestley82a05012012-01-03 06:28:32 -0300578 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;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300586}
587
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300588static int it913x_fe_get_frontend(struct dvb_frontend *fe)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300589{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300590 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300591 struct it913x_fe_state *state = fe->demodulator_priv;
592 int ret;
593 u8 reg[8];
594
595 ret = it913x_read_reg(state, REG_TPSD_TX_MODE, reg, sizeof(reg));
596
597 if (reg[3] < 3)
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300598 p->modulation = fe_con[reg[3]];
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300599
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300600 if (reg[0] < 3)
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300601 p->transmission_mode = fe_mode[reg[0]];
Malcolm Priestley3339a5b2011-11-06 11:19:14 -0300602
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300603 if (reg[1] < 4)
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300604 p->guard_interval = fe_gi[reg[1]];
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300605
606 if (reg[2] < 4)
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300607 p->hierarchy = fe_hi[reg[2]];
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300608
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300609 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;
611
612 /* Update internal state to reflect the autodetected props */
613 state->constellation = p->modulation;
614 state->transmission_mode = p->transmission_mode;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300615
616 return 0;
617}
618
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300619static int it913x_fe_set_frontend(struct dvb_frontend *fe)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300620{
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300621 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300622 struct it913x_fe_state *state = fe->demodulator_priv;
623 int ret, i;
624 u8 empty_ch, last_ch;
625
626 state->it913x_status = 0;
627
628 /* Set bw*/
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300629 ret = it913x_fe_select_bw(state, p->bandwidth_hz,
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300630 state->adcFrequency);
631
632 /* Training Mode Off */
633 ret = it913x_write_reg(state, PRO_LINK, TRAINING_MODE, 0x0);
634
635 /* Clear Empty Channel */
636 ret = it913x_write_reg(state, PRO_DMOD, EMPTY_CHANNEL_STATUS, 0x0);
637
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;
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300649 else
650 return -EOPNOTSUPP;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300651
652 ret = it913x_write_reg(state, PRO_DMOD, FREE_BAND, i);
653
654 deb_info("Frontend Set Tuner Type %02x", state->tuner_type);
655 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300656 case IT9135_38:
657 case IT9135_51:
658 case IT9135_52:
659 case IT9135_60:
660 case IT9135_61:
661 case IT9135_62:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300662 ret = it9137_set_tuner(state,
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300663 p->bandwidth_hz, p->frequency);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300664 break;
665 default:
666 if (fe->ops.tuner_ops.set_params) {
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300667 fe->ops.tuner_ops.set_params(fe);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300668 if (fe->ops.i2c_gate_ctrl)
669 fe->ops.i2c_gate_ctrl(fe, 0);
670 }
671 break;
672 }
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);
686 }
687 for (i = 0; i < 40; ++i) {
688 if (it913x_read_reg_u8(state, D_TPSD_LOCK) == 1)
689 break;
690 msleep(25);
691 }
692
693 state->frequency = p->frequency;
694 return 0;
695}
696
697static int it913x_fe_suspend(struct it913x_fe_state *state)
698{
699 int ret, i;
700 u8 b;
701
702 ret = it913x_write_reg(state, PRO_DMOD, SUSPEND_FLAG, 0x1);
703
704 ret |= it913x_write_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x0);
705
706 for (i = 0; i < 128; i++) {
707 ret = it913x_read_reg(state, SUSPEND_FLAG, &b, 1);
708 if (ret < 0)
Malcolm Priestleye3052882011-10-01 09:24:16 -0300709 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300710 if (b == 0)
711 break;
712
713 }
714
715 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x8);
716 /* Turn LED off */
Malcolm Priestleye3052882011-10-01 09:24:16 -0300717 ret |= it913x_write_reg(state, PRO_LINK, GPIOH3_O, 0x0);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300718
Malcolm Priestleye3052882011-10-01 09:24:16 -0300719 ret |= it913x_fe_script_loader(state, it9137_tuner_off);
720
721 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300722}
723
Malcolm Priestleye3052882011-10-01 09:24:16 -0300724/* Power sequence */
725/* Power Up Tuner on -> Frontend suspend off -> Tuner clk on */
726/* Power Down Frontend suspend on -> Tuner clk off -> Tuner off */
727
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300728static int it913x_fe_sleep(struct dvb_frontend *fe)
729{
730 struct it913x_fe_state *state = fe->demodulator_priv;
731 return it913x_fe_suspend(state);
732}
733
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300734static u32 compute_div(u32 a, u32 b, u32 x)
735{
736 u32 res = 0;
737 u32 c = 0;
738 u32 i = 0;
739
740 if (a > b) {
741 c = a / b;
742 a = a - c * b;
743 }
744
745 for (i = 0; i < x; i++) {
746 if (a >= b) {
747 res += 1;
748 a -= b;
749 }
750 a <<= 1;
751 res <<= 1;
752 }
753
754 res = (c << x) + res;
755
756 return res;
757}
758
759static int it913x_fe_start(struct it913x_fe_state *state)
760{
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300761 struct it913xset *set_lna;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300762 struct it913xset *set_mode;
763 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300764 u8 adf = (state->config->adf & 0xf);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300765 u32 adc, xtal;
766 u8 b[4];
767
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300768 if (state->config->chip_ver == 1)
769 ret = it913x_init_tuner(state);
tvboxspy7c2808e2011-09-21 19:06:58 -0300770
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300771 info("ADF table value :%02x", adf);
772
tvboxspy2b3c13e2011-10-31 12:06:34 -0300773 if (adf < 10) {
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300774 state->crystalFrequency = fe_clockTable[adf].xtal ;
775 state->table = fe_clockTable[adf].table;
776 state->adcFrequency = state->table->adcFrequency;
777
778 adc = compute_div(state->adcFrequency, 1000000ul, 19ul);
779 xtal = compute_div(state->crystalFrequency, 1000000ul, 19ul);
780
781 } else
782 return -EINVAL;
783
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300784 /* 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);
788
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300789 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);
792
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);
798
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);
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300803
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300804 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);
810
811 info("Crystal Frequency :%d Adc Frequency :%d ADC X2: %02x",
812 state->crystalFrequency, state->adcFrequency,
813 state->config->adc_x2);
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300814 deb_info("Xtal value :%04x Adc value :%04x", xtal, adc);
815
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300816 if (ret < 0)
817 return -ENODEV;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300818
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300819 /* 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;
826
827 /* LNA Scripts */
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300828 switch (state->tuner_type) {
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300829 case IT9135_51:
830 set_lna = it9135_51;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300831 break;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300832 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:
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300845 default:
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300846 set_lna = it9135_38;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300847 }
Malcolm Priestleyed942c52011-11-27 18:14:05 -0300848 info("Tuner LNA type :%02x", state->tuner_type);
849
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300850 ret = it913x_fe_script_loader(state, set_lna);
851 if (ret < 0)
852 return ret;
tvboxspy7c2808e2011-09-21 19:06:58 -0300853
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300854 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);
859 }
860 if (ret < 0)
861 return -ENODEV;
862
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300863 /* Always solo frontend */
864 set_mode = set_solo_fe;
865 ret |= it913x_fe_script_loader(state, set_mode);
866
867 ret |= it913x_fe_suspend(state);
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300868 return (ret < 0) ? -ENODEV : 0;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300869}
870
871static int it913x_fe_init(struct dvb_frontend *fe)
872{
873 struct it913x_fe_state *state = fe->demodulator_priv;
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300874 int ret = 0;
Malcolm Priestleye3052882011-10-01 09:24:16 -0300875 /* Power Up Tuner - common all versions */
876 ret = it913x_write_reg(state, PRO_DMOD, 0xec40, 0x1);
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300877
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300878 ret |= it913x_fe_script_loader(state, init_1);
879
Malcolm Priestley990f49a2011-11-28 18:04:21 -0300880 ret |= it913x_write_reg(state, PRO_DMOD, AFE_MEM0, 0x0);
881
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300882 ret |= it913x_write_reg(state, PRO_DMOD, 0xfba8, 0x0);
Malcolm Priestleye3052882011-10-01 09:24:16 -0300883
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300884 return (ret < 0) ? -ENODEV : 0;
885}
886
887static void it913x_fe_release(struct dvb_frontend *fe)
888{
889 struct it913x_fe_state *state = fe->demodulator_priv;
890 kfree(state);
891}
892
893static struct dvb_frontend_ops it913x_fe_ofdm_ops;
894
895struct dvb_frontend *it913x_fe_attach(struct i2c_adapter *i2c_adap,
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300896 u8 i2c_addr, struct ite_config *config)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300897{
898 struct it913x_fe_state *state = NULL;
899 int ret;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300900
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300901 /* allocate memory for the internal state */
902 state = kzalloc(sizeof(struct it913x_fe_state), GFP_KERNEL);
903 if (state == NULL)
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300904 return NULL;
905 if (config == NULL)
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300906 goto error;
907
908 state->i2c_adap = i2c_adap;
909 state->i2c_addr = i2c_addr;
Malcolm Priestleyb7d425d392011-10-31 12:02:08 -0300910 state->config = config;
911
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;
923 }
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300924
925 ret = it913x_fe_start(state);
926 if (ret < 0)
927 goto error;
928
929
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;
934
935 return &state->frontend;
936error:
937 kfree(state);
938 return NULL;
939}
940EXPORT_SYMBOL(it913x_fe_attach);
941
942static struct dvb_frontend_ops it913x_fe_ofdm_ops = {
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300943 .delsys = { SYS_DVBT },
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300944 .info = {
945 .name = "it913x-fe DVB-T",
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300946 .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,
956 },
957
958 .release = it913x_fe_release,
959
960 .init = it913x_fe_init,
961 .sleep = it913x_fe_sleep,
962
Mauro Carvalho Chehabf908cf12011-12-26 11:00:09 -0300963 .set_frontend = it913x_fe_set_frontend,
964 .get_frontend = it913x_fe_get_frontend,
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300965
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,
971};
972
973MODULE_DESCRIPTION("it913x Frontend and it9137 tuner");
974MODULE_AUTHOR("Malcolm Priestley tvboxspy@gmail.com");
Malcolm Priestley82a05012012-01-03 06:28:32 -0300975MODULE_VERSION("1.13");
Malcolm Priestley3dbbf822011-07-25 15:35:12 -0300976MODULE_LICENSE("GPL");