blob: d4e466a90e43d08d0e3fb6c3a24589ccd8f91857 [file] [log] [blame]
Patrick Boettcher01373a52007-07-30 12:49:04 -03001/*
2 * Linux-DVB Driver for DiBcom's DiB0070 base-band RF Tuner.
3 *
Patrick Boettcher7e5ce652009-08-03 13:43:40 -03004 * Copyright (C) 2005-9 DiBcom (http://www.dibcom.fr/)
Patrick Boettcher01373a52007-07-30 12:49:04 -03005 *
6 * This program is free software; you can redistribute it and/or
Patrick Boettcher7e5ce652009-08-03 13:43:40 -03007 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 *
22 * This code is more or less generated from another driver, please
23 * excuse some codingstyle oddities.
24 *
Patrick Boettcher01373a52007-07-30 12:49:04 -030025 */
Patrick Boettcher7e5ce652009-08-03 13:43:40 -030026
Patrick Boettcher01373a52007-07-30 12:49:04 -030027#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Patrick Boettcher01373a52007-07-30 12:49:04 -030029#include <linux/i2c.h>
30
31#include "dvb_frontend.h"
32
33#include "dib0070.h"
34#include "dibx000_common.h"
35
36static int debug;
37module_param(debug, int, 0644);
38MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
39
Patrick Boettcher7e5ce652009-08-03 13:43:40 -030040#define dprintk(args...) do { \
41 if (debug) { \
42 printk(KERN_DEBUG "DiB0070: "); \
43 printk(args); \
44 printk("\n"); \
45 } \
46} while (0)
Patrick Boettcher01373a52007-07-30 12:49:04 -030047
48#define DIB0070_P1D 0x00
49#define DIB0070_P1F 0x01
50#define DIB0070_P1G 0x03
51#define DIB0070S_P1A 0x02
52
53struct dib0070_state {
54 struct i2c_adapter *i2c;
55 struct dvb_frontend *fe;
56 const struct dib0070_config *cfg;
57 u16 wbd_ff_offset;
58 u8 revision;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -030059
Olivier Grenie03245a52009-12-04 13:27:57 -030060 enum frontend_tune_state tune_state;
61 u32 current_rf;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -030062
Olivier Grenie03245a52009-12-04 13:27:57 -030063 /* for the captrim binary search */
Patrick Boettcher7e5ce652009-08-03 13:43:40 -030064 s8 step;
65 u16 adc_diff;
66
67 s8 captrim;
68 s8 fcaptrim;
69 u16 lo4;
70
71 const struct dib0070_tuning *current_tune_table_index;
72 const struct dib0070_lna_match *lna_match;
73
Olivier Grenie03245a52009-12-04 13:27:57 -030074 u8 wbd_gain_current;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -030075 u16 wbd_offset_3_3[2];
Patrick Boettcher01373a52007-07-30 12:49:04 -030076};
77
78static uint16_t dib0070_read_reg(struct dib0070_state *state, u8 reg)
79{
80 u8 b[2];
81 struct i2c_msg msg[2] = {
Olivier Grenie03245a52009-12-04 13:27:57 -030082 { .addr = state->cfg->i2c_address, .flags = 0, .buf = &reg, .len = 1 },
83 { .addr = state->cfg->i2c_address, .flags = I2C_M_RD, .buf = b, .len = 2 },
Patrick Boettcher01373a52007-07-30 12:49:04 -030084 };
85 if (i2c_transfer(state->i2c, msg, 2) != 2) {
86 printk(KERN_WARNING "DiB0070 I2C read failed\n");
87 return 0;
88 }
89 return (b[0] << 8) | b[1];
90}
91
92static int dib0070_write_reg(struct dib0070_state *state, u8 reg, u16 val)
93{
94 u8 b[3] = { reg, val >> 8, val & 0xff };
Olivier Grenie03245a52009-12-04 13:27:57 -030095 struct i2c_msg msg = { .addr = state->cfg->i2c_address, .flags = 0, .buf = b, .len = 3 };
Patrick Boettcher01373a52007-07-30 12:49:04 -030096 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
97 printk(KERN_WARNING "DiB0070 I2C write failed\n");
98 return -EREMOTEIO;
99 }
100 return 0;
101}
102
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300103#define HARD_RESET(state) do { \
104 state->cfg->sleep(state->fe, 0); \
105 if (state->cfg->reset) { \
106 state->cfg->reset(state->fe,1); msleep(10); \
107 state->cfg->reset(state->fe,0); msleep(10); \
108 } \
109} while (0)
Patrick Boettcher01373a52007-07-30 12:49:04 -0300110
111static int dib0070_set_bandwidth(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
112{
Olivier Grenie03245a52009-12-04 13:27:57 -0300113 struct dib0070_state *state = fe->tuner_priv;
114 u16 tmp = dib0070_read_reg(state, 0x02) & 0x3fff;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300115
Olivier Grenie03245a52009-12-04 13:27:57 -0300116 if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 7000)
117 tmp |= (0 << 14);
118 else if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 6000)
119 tmp |= (1 << 14);
120 else if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 5000)
121 tmp |= (2 << 14);
122 else
123 tmp |= (3 << 14);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300124
Olivier Grenie03245a52009-12-04 13:27:57 -0300125 dib0070_write_reg(state, 0x02, tmp);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300126
Olivier Grenie03245a52009-12-04 13:27:57 -0300127 /* sharpen the BB filter in ISDB-T to have higher immunity to adjacent channels */
128 if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) {
129 u16 value = dib0070_read_reg(state, 0x17);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300130
Olivier Grenie03245a52009-12-04 13:27:57 -0300131 dib0070_write_reg(state, 0x17, value & 0xfffc);
132 tmp = dib0070_read_reg(state, 0x01) & 0x01ff;
133 dib0070_write_reg(state, 0x01, tmp | (60 << 9));
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300134
Olivier Grenie03245a52009-12-04 13:27:57 -0300135 dib0070_write_reg(state, 0x17, value);
136 }
Patrick Boettcher01373a52007-07-30 12:49:04 -0300137 return 0;
138}
139
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300140static int dib0070_captrim(struct dib0070_state *state, enum frontend_tune_state *tune_state)
Patrick Boettcher01373a52007-07-30 12:49:04 -0300141{
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300142 int8_t step_sign;
143 u16 adc;
144 int ret = 0;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300145
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300146 if (*tune_state == CT_TUNER_STEP_0) {
Patrick Boettcher01373a52007-07-30 12:49:04 -0300147
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300148 dib0070_write_reg(state, 0x0f, 0xed10);
Olivier Grenie03245a52009-12-04 13:27:57 -0300149 dib0070_write_reg(state, 0x17, 0x0034);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300150
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300151 dib0070_write_reg(state, 0x18, 0x0032);
152 state->step = state->captrim = state->fcaptrim = 64;
153 state->adc_diff = 3000;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300154 ret = 20;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300155
Olivier Grenie03245a52009-12-04 13:27:57 -0300156 *tune_state = CT_TUNER_STEP_1;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300157 } else if (*tune_state == CT_TUNER_STEP_1) {
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300158 state->step /= 2;
159 dib0070_write_reg(state, 0x14, state->lo4 | state->captrim);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300160 ret = 15;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300161
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300162 *tune_state = CT_TUNER_STEP_2;
163 } else if (*tune_state == CT_TUNER_STEP_2) {
Patrick Boettcher01373a52007-07-30 12:49:04 -0300164
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300165 adc = dib0070_read_reg(state, 0x19);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300166
Olivier Grenie9c783032009-12-07 07:49:40 -0300167 dprintk("CAPTRIM=%hd; ADC = %hd (ADC) & %dmV", state->captrim, adc, (u32) adc*(u32)1800/(u32)1024);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300168
169 if (adc >= 400) {
170 adc -= 400;
171 step_sign = -1;
172 } else {
173 adc = 400 - adc;
174 step_sign = 1;
175 }
176
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300177 if (adc < state->adc_diff) {
Olivier Grenie9c783032009-12-07 07:49:40 -0300178 dprintk("CAPTRIM=%hd is closer to target (%hd/%hd)", state->captrim, adc, state->adc_diff);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300179 state->adc_diff = adc;
180 state->fcaptrim = state->captrim;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300181
Olivier Grenie03245a52009-12-04 13:27:57 -0300182
183
Patrick Boettcher01373a52007-07-30 12:49:04 -0300184 }
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300185 state->captrim += (step_sign * state->step);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300186
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300187 if (state->step >= 1)
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300188 *tune_state = CT_TUNER_STEP_1;
189 else
190 *tune_state = CT_TUNER_STEP_3;
191
192 } else if (*tune_state == CT_TUNER_STEP_3) {
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300193 dib0070_write_reg(state, 0x14, state->lo4 | state->fcaptrim);
194 dib0070_write_reg(state, 0x18, 0x07ff);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300195 *tune_state = CT_TUNER_STEP_4;
196 }
197
198 return ret;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300199}
200
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300201static int dib0070_set_ctrl_lo5(struct dvb_frontend *fe, u8 vco_bias_trim, u8 hf_div_trim, u8 cp_current, u8 third_order_filt)
202{
203 struct dib0070_state *state = fe->tuner_priv;
Olivier Grenie03245a52009-12-04 13:27:57 -0300204 u16 lo5 = (third_order_filt << 14) | (0 << 13) | (1 << 12) | (3 << 9) | (cp_current << 6) | (hf_div_trim << 3) | (vco_bias_trim << 0);
Olivier Grenie9c783032009-12-07 07:49:40 -0300205 dprintk("CTRL_LO5: 0x%x", lo5);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300206 return dib0070_write_reg(state, 0x15, lo5);
207}
208
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300209void dib0070_ctrl_agc_filter(struct dvb_frontend *fe, u8 open)
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300210{
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300211 struct dib0070_state *state = fe->tuner_priv;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300212
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300213 if (open) {
214 dib0070_write_reg(state, 0x1b, 0xff00);
215 dib0070_write_reg(state, 0x1a, 0x0000);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300216 } else {
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300217 dib0070_write_reg(state, 0x1b, 0x4112);
Olivier Grenie03245a52009-12-04 13:27:57 -0300218 if (state->cfg->vga_filter != 0) {
Olivier Grenie9c783032009-12-07 07:49:40 -0300219 dib0070_write_reg(state, 0x1a, state->cfg->vga_filter);
220 dprintk("vga filter register is set to %x", state->cfg->vga_filter);
Olivier Grenie03245a52009-12-04 13:27:57 -0300221 } else
Olivier Grenie9c783032009-12-07 07:49:40 -0300222 dib0070_write_reg(state, 0x1a, 0x0009);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300223 }
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300224}
225
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300226EXPORT_SYMBOL(dib0070_ctrl_agc_filter);
227struct dib0070_tuning {
Olivier Grenie03245a52009-12-04 13:27:57 -0300228 u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
229 u8 switch_trim;
230 u8 vco_band;
231 u8 hfdiv;
232 u8 vco_multi;
233 u8 presc;
234 u8 wbdmux;
235 u16 tuner_enable;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300236};
237
238struct dib0070_lna_match {
Olivier Grenie03245a52009-12-04 13:27:57 -0300239 u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
240 u8 lna_band;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300241};
242
243static const struct dib0070_tuning dib0070s_tuning_table[] = {
Olivier Grenie03245a52009-12-04 13:27:57 -0300244 { 570000, 2, 1, 3, 6, 6, 2, 0x4000 | 0x0800 }, /* UHF */
245 { 700000, 2, 0, 2, 4, 2, 2, 0x4000 | 0x0800 },
246 { 863999, 2, 1, 2, 4, 2, 2, 0x4000 | 0x0800 },
247 { 1500000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND */
248 { 1600000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 },
249 { 2000000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 },
250 { 0xffffffff, 0, 0, 8, 1, 2, 1, 0x8000 | 0x1000 }, /* SBAND */
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300251};
252
253static const struct dib0070_tuning dib0070_tuning_table[] = {
Olivier Grenie03245a52009-12-04 13:27:57 -0300254 { 115000, 1, 0, 7, 24, 2, 1, 0x8000 | 0x1000 }, /* FM below 92MHz cannot be tuned */
255 { 179500, 1, 0, 3, 16, 2, 1, 0x8000 | 0x1000 }, /* VHF */
256 { 189999, 1, 1, 3, 16, 2, 1, 0x8000 | 0x1000 },
257 { 250000, 1, 0, 6, 12, 2, 1, 0x8000 | 0x1000 },
258 { 569999, 2, 1, 5, 6, 2, 2, 0x4000 | 0x0800 }, /* UHF */
Olivier Grenie9c783032009-12-07 07:49:40 -0300259 { 699999, 2, 0, 1, 4, 2, 2, 0x4000 | 0x0800 },
Olivier Grenie03245a52009-12-04 13:27:57 -0300260 { 863999, 2, 1, 1, 4, 2, 2, 0x4000 | 0x0800 },
261 { 0xffffffff, 0, 1, 0, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND or everything higher than UHF */
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300262};
263
264static const struct dib0070_lna_match dib0070_lna_flip_chip[] = {
Olivier Grenie03245a52009-12-04 13:27:57 -0300265 { 180000, 0 }, /* VHF */
266 { 188000, 1 },
267 { 196400, 2 },
268 { 250000, 3 },
269 { 550000, 0 }, /* UHF */
270 { 590000, 1 },
271 { 666000, 3 },
272 { 864000, 5 },
273 { 1500000, 0 }, /* LBAND or everything higher than UHF */
274 { 1600000, 1 },
275 { 2000000, 3 },
276 { 0xffffffff, 7 },
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300277};
278
279static const struct dib0070_lna_match dib0070_lna[] = {
Olivier Grenie03245a52009-12-04 13:27:57 -0300280 { 180000, 0 }, /* VHF */
281 { 188000, 1 },
282 { 196400, 2 },
283 { 250000, 3 },
284 { 550000, 2 }, /* UHF */
285 { 650000, 3 },
286 { 750000, 5 },
287 { 850000, 6 },
288 { 864000, 7 },
289 { 1500000, 0 }, /* LBAND or everything higher than UHF */
290 { 1600000, 1 },
291 { 2000000, 3 },
292 { 0xffffffff, 7 },
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300293};
294
Olivier Grenie9c783032009-12-07 07:49:40 -0300295#define LPF 100
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300296static int dib0070_tune_digital(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
297{
Olivier Grenie03245a52009-12-04 13:27:57 -0300298 struct dib0070_state *state = fe->tuner_priv;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300299
Olivier Grenie03245a52009-12-04 13:27:57 -0300300 const struct dib0070_tuning *tune;
301 const struct dib0070_lna_match *lna_match;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300302
Olivier Grenie03245a52009-12-04 13:27:57 -0300303 enum frontend_tune_state *tune_state = &state->tune_state;
304 int ret = 10; /* 1ms is the default delay most of the time */
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300305
Olivier Grenie03245a52009-12-04 13:27:57 -0300306 u8 band = (u8)BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency/1000);
307 u32 freq = fe->dtv_property_cache.frequency/1000 + (band == BAND_VHF ? state->cfg->freq_offset_khz_vhf : state->cfg->freq_offset_khz_uhf);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300308
309#ifdef CONFIG_SYS_ISDBT
Olivier Grenie03245a52009-12-04 13:27:57 -0300310 if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1)
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300311 if (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2)
312 && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
313 || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
314 && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == (state->fe->dtv_property_cache.isdbt_sb_segment_count / 2)))
315 || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
316 && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1))))
Olivier Grenie9c783032009-12-07 07:49:40 -0300317 freq += 850;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300318#endif
Olivier Grenie03245a52009-12-04 13:27:57 -0300319 if (state->current_rf != freq) {
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300320
Olivier Grenie03245a52009-12-04 13:27:57 -0300321 switch (state->revision) {
322 case DIB0070S_P1A:
323 tune = dib0070s_tuning_table;
324 lna_match = dib0070_lna;
325 break;
326 default:
327 tune = dib0070_tuning_table;
328 if (state->cfg->flip_chip)
329 lna_match = dib0070_lna_flip_chip;
330 else
331 lna_match = dib0070_lna;
332 break;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300333 }
Olivier Grenie03245a52009-12-04 13:27:57 -0300334 while (freq > tune->max_freq) /* find the right one */
335 tune++;
336 while (freq > lna_match->max_freq) /* find the right one */
337 lna_match++;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300338
Olivier Grenie03245a52009-12-04 13:27:57 -0300339 state->current_tune_table_index = tune;
340 state->lna_match = lna_match;
341 }
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300342
Olivier Grenie03245a52009-12-04 13:27:57 -0300343 if (*tune_state == CT_TUNER_START) {
Olivier Grenie9c783032009-12-07 07:49:40 -0300344 dprintk("Tuning for Band: %hd (%d kHz)", band, freq);
Olivier Grenie03245a52009-12-04 13:27:57 -0300345 if (state->current_rf != freq) {
Olivier Grenie9c783032009-12-07 07:49:40 -0300346 u8 REFDIV;
347 u32 FBDiv, Rest, FREF, VCOF_kHz;
348 u8 Den;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300349
Olivier Grenie9c783032009-12-07 07:49:40 -0300350 state->current_rf = freq;
351 state->lo4 = (state->current_tune_table_index->vco_band << 11) | (state->current_tune_table_index->hfdiv << 7);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300352
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300353
Olivier Grenie9c783032009-12-07 07:49:40 -0300354 dib0070_write_reg(state, 0x17, 0x30);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300355
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300356
Olivier Grenie9c783032009-12-07 07:49:40 -0300357 VCOF_kHz = state->current_tune_table_index->vco_multi * freq * 2;
Olivier Grenie03245a52009-12-04 13:27:57 -0300358
Olivier Grenie9c783032009-12-07 07:49:40 -0300359 switch (band) {
360 case BAND_VHF:
361 REFDIV = (u8) ((state->cfg->clock_khz + 9999) / 10000);
362 break;
363 case BAND_FM:
364 REFDIV = (u8) ((state->cfg->clock_khz) / 1000);
365 break;
366 default:
367 REFDIV = (u8) (state->cfg->clock_khz / 10000);
368 break;
369 }
370 FREF = state->cfg->clock_khz / REFDIV;
Olivier Grenie03245a52009-12-04 13:27:57 -0300371
372
373
Olivier Grenie9c783032009-12-07 07:49:40 -0300374 switch (state->revision) {
375 case DIB0070S_P1A:
376 FBDiv = (VCOF_kHz / state->current_tune_table_index->presc / FREF);
377 Rest = (VCOF_kHz / state->current_tune_table_index->presc) - FBDiv * FREF;
378 break;
Olivier Grenie03245a52009-12-04 13:27:57 -0300379
Olivier Grenie9c783032009-12-07 07:49:40 -0300380 case DIB0070_P1G:
381 case DIB0070_P1F:
382 default:
383 FBDiv = (freq / (FREF / 2));
384 Rest = 2 * freq - FBDiv * FREF;
385 break;
386 }
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300387
Olivier Grenie9c783032009-12-07 07:49:40 -0300388 if (Rest < LPF)
389 Rest = 0;
390 else if (Rest < 2 * LPF)
391 Rest = 2 * LPF;
392 else if (Rest > (FREF - LPF)) {
393 Rest = 0;
394 FBDiv += 1;
395 } else if (Rest > (FREF - 2 * LPF))
396 Rest = FREF - 2 * LPF;
397 Rest = (Rest * 6528) / (FREF / 10);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300398
Olivier Grenie9c783032009-12-07 07:49:40 -0300399 Den = 1;
400 if (Rest > 0) {
401 state->lo4 |= (1 << 14) | (1 << 12);
402 Den = 255;
403 }
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300404
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300405
Olivier Grenie9c783032009-12-07 07:49:40 -0300406 dib0070_write_reg(state, 0x11, (u16)FBDiv);
407 dib0070_write_reg(state, 0x12, (Den << 8) | REFDIV);
408 dib0070_write_reg(state, 0x13, (u16) Rest);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300409
Olivier Grenie9c783032009-12-07 07:49:40 -0300410 if (state->revision == DIB0070S_P1A) {
Olivier Grenie03245a52009-12-04 13:27:57 -0300411
Olivier Grenie9c783032009-12-07 07:49:40 -0300412 if (band == BAND_SBAND) {
413 dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
414 dib0070_write_reg(state, 0x1d, 0xFFFF);
415 } else
416 dib0070_set_ctrl_lo5(fe, 5, 4, 3, 1);
417 }
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300418
Olivier Grenie9c783032009-12-07 07:49:40 -0300419 dib0070_write_reg(state, 0x20,
420 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001 | state->current_tune_table_index->tuner_enable);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300421
Olivier Grenie9c783032009-12-07 07:49:40 -0300422 dprintk("REFDIV: %hd, FREF: %d", REFDIV, FREF);
423 dprintk("FBDIV: %d, Rest: %d", FBDiv, Rest);
424 dprintk("Num: %hd, Den: %hd, SD: %hd", (u16) Rest, Den, (state->lo4 >> 12) & 0x1);
425 dprintk("HFDIV code: %hd", state->current_tune_table_index->hfdiv);
426 dprintk("VCO = %hd", state->current_tune_table_index->vco_band);
427 dprintk("VCOF: ((%hd*%d) << 1))", state->current_tune_table_index->vco_multi, freq);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300428
Olivier Grenie9c783032009-12-07 07:49:40 -0300429 *tune_state = CT_TUNER_STEP_0;
Olivier Grenie03245a52009-12-04 13:27:57 -0300430 } else { /* we are already tuned to this frequency - the configuration is correct */
Olivier Grenie9c783032009-12-07 07:49:40 -0300431 ret = 50; /* wakeup time */
432 *tune_state = CT_TUNER_STEP_5;
Olivier Grenie03245a52009-12-04 13:27:57 -0300433 }
434 } else if ((*tune_state > CT_TUNER_START) && (*tune_state < CT_TUNER_STEP_4)) {
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300435
Olivier Grenie03245a52009-12-04 13:27:57 -0300436 ret = dib0070_captrim(state, tune_state);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300437
Olivier Grenie03245a52009-12-04 13:27:57 -0300438 } else if (*tune_state == CT_TUNER_STEP_4) {
439 const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
440 if (tmp != NULL) {
Olivier Grenie9c783032009-12-07 07:49:40 -0300441 while (freq/1000 > tmp->freq) /* find the right one */
442 tmp++;
443 dib0070_write_reg(state, 0x0f,
444 (0 << 15) | (1 << 14) | (3 << 12)
445 | (tmp->wbd_gain_val << 9) | (0 << 8) | (1 << 7)
446 | (state->current_tune_table_index->wbdmux << 0));
447 state->wbd_gain_current = tmp->wbd_gain_val;
Olivier Grenie03245a52009-12-04 13:27:57 -0300448 } else {
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300449 dib0070_write_reg(state, 0x0f,
450 (0 << 15) | (1 << 14) | (3 << 12) | (6 << 9) | (0 << 8) | (1 << 7) | (state->current_tune_table_index->
451 wbdmux << 0));
Olivier Grenie03245a52009-12-04 13:27:57 -0300452 state->wbd_gain_current = 6;
453 }
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300454
Olivier Grenie03245a52009-12-04 13:27:57 -0300455 dib0070_write_reg(state, 0x06, 0x3fff);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300456 dib0070_write_reg(state, 0x07,
457 (state->current_tune_table_index->switch_trim << 11) | (7 << 8) | (state->lna_match->lna_band << 3) | (3 << 0));
Olivier Grenie03245a52009-12-04 13:27:57 -0300458 dib0070_write_reg(state, 0x08, (state->lna_match->lna_band << 10) | (3 << 7) | (127));
459 dib0070_write_reg(state, 0x0d, 0x0d80);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300460
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300461
Olivier Grenie03245a52009-12-04 13:27:57 -0300462 dib0070_write_reg(state, 0x18, 0x07ff);
463 dib0070_write_reg(state, 0x17, 0x0033);
464
465
466 *tune_state = CT_TUNER_STEP_5;
467 } else if (*tune_state == CT_TUNER_STEP_5) {
468 dib0070_set_bandwidth(fe, ch);
469 *tune_state = CT_TUNER_STOP;
470 } else {
471 ret = FE_CALLBACK_TIME_NEVER; /* tuner finished, time to call again infinite */
472 }
473 return ret;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300474}
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300475
Olivier Grenie03245a52009-12-04 13:27:57 -0300476
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300477static int dib0070_tune(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
478{
Olivier Grenie03245a52009-12-04 13:27:57 -0300479 struct dib0070_state *state = fe->tuner_priv;
480 uint32_t ret;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300481
Olivier Grenie03245a52009-12-04 13:27:57 -0300482 state->tune_state = CT_TUNER_START;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300483
Olivier Grenie03245a52009-12-04 13:27:57 -0300484 do {
485 ret = dib0070_tune_digital(fe, p);
486 if (ret != FE_CALLBACK_TIME_NEVER)
Olivier Grenie9c783032009-12-07 07:49:40 -0300487 msleep(ret/10);
Olivier Grenie03245a52009-12-04 13:27:57 -0300488 else
489 break;
490 } while (state->tune_state != CT_TUNER_STOP);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300491
Olivier Grenie03245a52009-12-04 13:27:57 -0300492 return 0;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300493}
494
495static int dib0070_wakeup(struct dvb_frontend *fe)
496{
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300497 struct dib0070_state *state = fe->tuner_priv;
498 if (state->cfg->sleep)
499 state->cfg->sleep(fe, 0);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300500 return 0;
501}
502
503static int dib0070_sleep(struct dvb_frontend *fe)
504{
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300505 struct dib0070_state *state = fe->tuner_priv;
506 if (state->cfg->sleep)
507 state->cfg->sleep(fe, 1);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300508 return 0;
509}
510
Olivier Grenie03245a52009-12-04 13:27:57 -0300511u8 dib0070_get_rf_output(struct dvb_frontend *fe)
512{
513 struct dib0070_state *state = fe->tuner_priv;
514 return (dib0070_read_reg(state, 0x07) >> 11) & 0x3;
515}
Olivier Grenie03245a52009-12-04 13:27:57 -0300516EXPORT_SYMBOL(dib0070_get_rf_output);
Olivier Grenie9c783032009-12-07 07:49:40 -0300517
Olivier Grenie03245a52009-12-04 13:27:57 -0300518int dib0070_set_rf_output(struct dvb_frontend *fe, u8 no)
519{
520 struct dib0070_state *state = fe->tuner_priv;
521 u16 rxrf2 = dib0070_read_reg(state, 0x07) & 0xfe7ff;
Olivier Grenie9c783032009-12-07 07:49:40 -0300522 if (no > 3)
523 no = 3;
524 if (no < 1)
525 no = 1;
Olivier Grenie03245a52009-12-04 13:27:57 -0300526 return dib0070_write_reg(state, 0x07, rxrf2 | (no << 11));
527}
Olivier Grenie03245a52009-12-04 13:27:57 -0300528EXPORT_SYMBOL(dib0070_set_rf_output);
Olivier Grenie9c783032009-12-07 07:49:40 -0300529
Olivier Grenie03245a52009-12-04 13:27:57 -0300530static const u16 dib0070_p1f_defaults[] =
531
532{
Patrick Boettcher01373a52007-07-30 12:49:04 -0300533 7, 0x02,
Olivier Grenie03245a52009-12-04 13:27:57 -0300534 0x0008,
535 0x0000,
536 0x0000,
537 0x0000,
538 0x0000,
539 0x0002,
540 0x0100,
Patrick Boettcher01373a52007-07-30 12:49:04 -0300541
542 3, 0x0d,
Olivier Grenie03245a52009-12-04 13:27:57 -0300543 0x0d80,
544 0x0001,
545 0x0000,
Patrick Boettcher01373a52007-07-30 12:49:04 -0300546
547 4, 0x11,
Olivier Grenie03245a52009-12-04 13:27:57 -0300548 0x0000,
549 0x0103,
550 0x0000,
551 0x0000,
Patrick Boettcher01373a52007-07-30 12:49:04 -0300552
553 3, 0x16,
Olivier Grenie03245a52009-12-04 13:27:57 -0300554 0x0004 | 0x0040,
555 0x0030,
556 0x07ff,
Patrick Boettcher01373a52007-07-30 12:49:04 -0300557
558 6, 0x1b,
Olivier Grenie03245a52009-12-04 13:27:57 -0300559 0x4112,
560 0xff00,
561 0xc07f,
562 0x0000,
563 0x0180,
564 0x4000 | 0x0800 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001,
Patrick Boettcher01373a52007-07-30 12:49:04 -0300565
566 0,
567};
568
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300569static u16 dib0070_read_wbd_offset(struct dib0070_state *state, u8 gain)
Patrick Boettcher01373a52007-07-30 12:49:04 -0300570{
Olivier Grenie03245a52009-12-04 13:27:57 -0300571 u16 tuner_en = dib0070_read_reg(state, 0x20);
572 u16 offset;
Patrick Boettcher3cb2c392008-01-25 07:25:20 -0300573
Olivier Grenie03245a52009-12-04 13:27:57 -0300574 dib0070_write_reg(state, 0x18, 0x07ff);
575 dib0070_write_reg(state, 0x20, 0x0800 | 0x4000 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001);
576 dib0070_write_reg(state, 0x0f, (1 << 14) | (2 << 12) | (gain << 9) | (1 << 8) | (1 << 7) | (0 << 0));
577 msleep(9);
578 offset = dib0070_read_reg(state, 0x19);
579 dib0070_write_reg(state, 0x20, tuner_en);
580 return offset;
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300581}
Patrick Boettcher3cb2c392008-01-25 07:25:20 -0300582
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300583static void dib0070_wbd_offset_calibration(struct dib0070_state *state)
584{
Olivier Grenie03245a52009-12-04 13:27:57 -0300585 u8 gain;
586 for (gain = 6; gain < 8; gain++) {
587 state->wbd_offset_3_3[gain - 6] = ((dib0070_read_wbd_offset(state, gain) * 8 * 18 / 33 + 1) / 2);
Olivier Grenie9c783032009-12-07 07:49:40 -0300588 dprintk("Gain: %d, WBDOffset (3.3V) = %hd", gain, state->wbd_offset_3_3[gain-6]);
Olivier Grenie03245a52009-12-04 13:27:57 -0300589 }
Patrick Boettcher01373a52007-07-30 12:49:04 -0300590}
591
592u16 dib0070_wbd_offset(struct dvb_frontend *fe)
593{
Olivier Grenie03245a52009-12-04 13:27:57 -0300594 struct dib0070_state *state = fe->tuner_priv;
595 const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
596 u32 freq = fe->dtv_property_cache.frequency/1000;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300597
Olivier Grenie03245a52009-12-04 13:27:57 -0300598 if (tmp != NULL) {
599 while (freq/1000 > tmp->freq) /* find the right one */
600 tmp++;
601 state->wbd_gain_current = tmp->wbd_gain_val;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300602 } else
Olivier Grenie03245a52009-12-04 13:27:57 -0300603 state->wbd_gain_current = 6;
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300604
Olivier Grenie03245a52009-12-04 13:27:57 -0300605 return state->wbd_offset_3_3[state->wbd_gain_current - 6];
Patrick Boettcher01373a52007-07-30 12:49:04 -0300606}
Patrick Boettcher01373a52007-07-30 12:49:04 -0300607EXPORT_SYMBOL(dib0070_wbd_offset);
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300608
Patrick Boettcher01373a52007-07-30 12:49:04 -0300609#define pgm_read_word(w) (*w)
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300610static int dib0070_reset(struct dvb_frontend *fe)
Patrick Boettcher01373a52007-07-30 12:49:04 -0300611{
Olivier Grenie03245a52009-12-04 13:27:57 -0300612 struct dib0070_state *state = fe->tuner_priv;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300613 u16 l, r, *n;
614
615 HARD_RESET(state);
616
Olivier Grenie03245a52009-12-04 13:27:57 -0300617
Patrick Boettcher01373a52007-07-30 12:49:04 -0300618#ifndef FORCE_SBAND_TUNER
619 if ((dib0070_read_reg(state, 0x22) >> 9) & 0x1)
620 state->revision = (dib0070_read_reg(state, 0x1f) >> 8) & 0xff;
621 else
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300622#else
623#warning forcing SBAND
Patrick Boettcher01373a52007-07-30 12:49:04 -0300624#endif
Olivier Grenie03245a52009-12-04 13:27:57 -0300625 state->revision = DIB0070S_P1A;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300626
627 /* P1F or not */
Olivier Grenie9c783032009-12-07 07:49:40 -0300628 dprintk("Revision: %x", state->revision);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300629
630 if (state->revision == DIB0070_P1D) {
Olivier Grenie9c783032009-12-07 07:49:40 -0300631 dprintk("Error: this driver is not to be used meant for P1D or earlier");
Patrick Boettcher01373a52007-07-30 12:49:04 -0300632 return -EINVAL;
633 }
634
635 n = (u16 *) dib0070_p1f_defaults;
636 l = pgm_read_word(n++);
637 while (l) {
638 r = pgm_read_word(n++);
639 do {
Olivier Grenie03245a52009-12-04 13:27:57 -0300640 dib0070_write_reg(state, (u8)r, pgm_read_word(n++));
Patrick Boettcher01373a52007-07-30 12:49:04 -0300641 r++;
642 } while (--l);
643 l = pgm_read_word(n++);
644 }
645
646 if (state->cfg->force_crystal_mode != 0)
647 r = state->cfg->force_crystal_mode;
648 else if (state->cfg->clock_khz >= 24000)
649 r = 1;
650 else
651 r = 2;
652
Olivier Grenie03245a52009-12-04 13:27:57 -0300653
Patrick Boettcher01373a52007-07-30 12:49:04 -0300654 r |= state->cfg->osc_buffer_state << 3;
655
656 dib0070_write_reg(state, 0x10, r);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300657 dib0070_write_reg(state, 0x1f, (1 << 8) | ((state->cfg->clock_pad_drive & 0xf) << 5));
Patrick Boettcher01373a52007-07-30 12:49:04 -0300658
659 if (state->cfg->invert_iq) {
660 r = dib0070_read_reg(state, 0x02) & 0xffdf;
661 dib0070_write_reg(state, 0x02, r | (1 << 5));
662 }
663
Olivier Grenie03245a52009-12-04 13:27:57 -0300664 if (state->revision == DIB0070S_P1A)
665 dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
666 else
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300667 dib0070_set_ctrl_lo5(fe, 5, 4, state->cfg->charge_pump, state->cfg->enable_third_order_filter);
Patrick Boettcher01373a52007-07-30 12:49:04 -0300668
669 dib0070_write_reg(state, 0x01, (54 << 9) | 0xc8);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300670
Olivier Grenie03245a52009-12-04 13:27:57 -0300671 dib0070_wbd_offset_calibration(state);
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300672
Olivier Grenie03245a52009-12-04 13:27:57 -0300673 return 0;
674}
675
676static int dib0070_get_frequency(struct dvb_frontend *fe, u32 *frequency)
677{
678 struct dib0070_state *state = fe->tuner_priv;
679
680 *frequency = 1000 * state->current_rf;
681 return 0;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300682}
683
Patrick Boettcher01373a52007-07-30 12:49:04 -0300684static int dib0070_release(struct dvb_frontend *fe)
685{
686 kfree(fe->tuner_priv);
687 fe->tuner_priv = NULL;
688 return 0;
689}
690
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300691static const struct dvb_tuner_ops dib0070_ops = {
Patrick Boettcher01373a52007-07-30 12:49:04 -0300692 .info = {
Olivier Grenie03245a52009-12-04 13:27:57 -0300693 .name = "DiBcom DiB0070",
694 .frequency_min = 45000000,
695 .frequency_max = 860000000,
696 .frequency_step = 1000,
697 },
698 .release = dib0070_release,
Patrick Boettcher01373a52007-07-30 12:49:04 -0300699
Olivier Grenie03245a52009-12-04 13:27:57 -0300700 .init = dib0070_wakeup,
701 .sleep = dib0070_sleep,
702 .set_params = dib0070_tune,
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300703
Olivier Grenie03245a52009-12-04 13:27:57 -0300704 .get_frequency = dib0070_get_frequency,
Patrick Boettcher2a6a30e2009-08-17 05:13:28 -0300705// .get_bandwidth = dib0070_get_bandwidth
Patrick Boettcher01373a52007-07-30 12:49:04 -0300706};
707
Olivier Grenie9c783032009-12-07 07:49:40 -0300708struct dvb_frontend *dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dib0070_config *cfg)
Patrick Boettcher01373a52007-07-30 12:49:04 -0300709{
710 struct dib0070_state *state = kzalloc(sizeof(struct dib0070_state), GFP_KERNEL);
711 if (state == NULL)
712 return NULL;
713
714 state->cfg = cfg;
715 state->i2c = i2c;
Olivier Grenie03245a52009-12-04 13:27:57 -0300716 state->fe = fe;
Patrick Boettcher01373a52007-07-30 12:49:04 -0300717 fe->tuner_priv = state;
718
Patrick Boettcher7e5ce652009-08-03 13:43:40 -0300719 if (dib0070_reset(fe) != 0)
Patrick Boettcher01373a52007-07-30 12:49:04 -0300720 goto free_mem;
721
Patrick Boettcher01373a52007-07-30 12:49:04 -0300722 printk(KERN_INFO "DiB0070: successfully identified\n");
723 memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops));
724
725 fe->tuner_priv = state;
726 return fe;
727
Olivier Grenie03245a52009-12-04 13:27:57 -0300728free_mem:
Patrick Boettcher01373a52007-07-30 12:49:04 -0300729 kfree(state);
730 fe->tuner_priv = NULL;
731 return NULL;
732}
Patrick Boettcher01373a52007-07-30 12:49:04 -0300733EXPORT_SYMBOL(dib0070_attach);
734
735MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
736MODULE_DESCRIPTION("Driver for the DiBcom 0070 base-band RF Tuner");
737MODULE_LICENSE("GPL");