Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Linux-DVB Driver for DiBcom's DiB0070 base-band RF Tuner. |
| 3 | * |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 4 | * Copyright (C) 2005-9 DiBcom (http://www.dibcom.fr/) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 7 | * 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 Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 25 | */ |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 26 | |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 27 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 29 | #include <linux/i2c.h> |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 31 | |
| 32 | #include "dvb_frontend.h" |
| 33 | |
| 34 | #include "dib0070.h" |
| 35 | #include "dibx000_common.h" |
| 36 | |
| 37 | static int debug; |
| 38 | module_param(debug, int, 0644); |
| 39 | MODULE_PARM_DESC(debug, "turn on debugging (default: 0)"); |
| 40 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 41 | #define dprintk(args...) do { \ |
| 42 | if (debug) { \ |
| 43 | printk(KERN_DEBUG "DiB0070: "); \ |
| 44 | printk(args); \ |
| 45 | printk("\n"); \ |
| 46 | } \ |
| 47 | } while (0) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 48 | |
| 49 | #define DIB0070_P1D 0x00 |
| 50 | #define DIB0070_P1F 0x01 |
| 51 | #define DIB0070_P1G 0x03 |
| 52 | #define DIB0070S_P1A 0x02 |
| 53 | |
| 54 | struct dib0070_state { |
| 55 | struct i2c_adapter *i2c; |
| 56 | struct dvb_frontend *fe; |
| 57 | const struct dib0070_config *cfg; |
| 58 | u16 wbd_ff_offset; |
| 59 | u8 revision; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 60 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 61 | enum frontend_tune_state tune_state; |
| 62 | u32 current_rf; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 63 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 64 | /* for the captrim binary search */ |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 65 | s8 step; |
| 66 | u16 adc_diff; |
| 67 | |
| 68 | s8 captrim; |
| 69 | s8 fcaptrim; |
| 70 | u16 lo4; |
| 71 | |
| 72 | const struct dib0070_tuning *current_tune_table_index; |
| 73 | const struct dib0070_lna_match *lna_match; |
| 74 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 75 | u8 wbd_gain_current; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 76 | u16 wbd_offset_3_3[2]; |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 77 | |
| 78 | /* for the I2C transfer */ |
| 79 | struct i2c_msg msg[2]; |
| 80 | u8 i2c_write_buffer[3]; |
| 81 | u8 i2c_read_buffer[2]; |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 82 | struct mutex i2c_buffer_lock; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 83 | }; |
| 84 | |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 85 | static u16 dib0070_read_reg(struct dib0070_state *state, u8 reg) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 86 | { |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 87 | u16 ret; |
| 88 | |
| 89 | if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) { |
| 90 | dprintk("could not acquire lock"); |
| 91 | return 0; |
| 92 | } |
| 93 | |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 94 | state->i2c_write_buffer[0] = reg; |
| 95 | |
| 96 | memset(state->msg, 0, 2 * sizeof(struct i2c_msg)); |
| 97 | state->msg[0].addr = state->cfg->i2c_address; |
| 98 | state->msg[0].flags = 0; |
| 99 | state->msg[0].buf = state->i2c_write_buffer; |
| 100 | state->msg[0].len = 1; |
| 101 | state->msg[1].addr = state->cfg->i2c_address; |
| 102 | state->msg[1].flags = I2C_M_RD; |
| 103 | state->msg[1].buf = state->i2c_read_buffer; |
| 104 | state->msg[1].len = 2; |
| 105 | |
| 106 | if (i2c_transfer(state->i2c, state->msg, 2) != 2) { |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 107 | printk(KERN_WARNING "DiB0070 I2C read failed\n"); |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 108 | ret = 0; |
| 109 | } else |
| 110 | ret = (state->i2c_read_buffer[0] << 8) |
| 111 | | state->i2c_read_buffer[1]; |
| 112 | |
| 113 | mutex_unlock(&state->i2c_buffer_lock); |
| 114 | return ret; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int dib0070_write_reg(struct dib0070_state *state, u8 reg, u16 val) |
| 118 | { |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 119 | int ret; |
| 120 | |
| 121 | if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) { |
| 122 | dprintk("could not acquire lock"); |
| 123 | return -EINVAL; |
| 124 | } |
Olivier Grenie | 5a0deee | 2011-05-03 12:27:33 -0300 | [diff] [blame] | 125 | state->i2c_write_buffer[0] = reg; |
| 126 | state->i2c_write_buffer[1] = val >> 8; |
| 127 | state->i2c_write_buffer[2] = val & 0xff; |
| 128 | |
| 129 | memset(state->msg, 0, sizeof(struct i2c_msg)); |
| 130 | state->msg[0].addr = state->cfg->i2c_address; |
| 131 | state->msg[0].flags = 0; |
| 132 | state->msg[0].buf = state->i2c_write_buffer; |
| 133 | state->msg[0].len = 3; |
| 134 | |
| 135 | if (i2c_transfer(state->i2c, state->msg, 1) != 1) { |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 136 | printk(KERN_WARNING "DiB0070 I2C write failed\n"); |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 137 | ret = -EREMOTEIO; |
| 138 | } else |
| 139 | ret = 0; |
| 140 | |
| 141 | mutex_unlock(&state->i2c_buffer_lock); |
| 142 | return ret; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 143 | } |
| 144 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 145 | #define HARD_RESET(state) do { \ |
| 146 | state->cfg->sleep(state->fe, 0); \ |
| 147 | if (state->cfg->reset) { \ |
| 148 | state->cfg->reset(state->fe,1); msleep(10); \ |
| 149 | state->cfg->reset(state->fe,0); msleep(10); \ |
| 150 | } \ |
| 151 | } while (0) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 152 | |
Mauro Carvalho Chehab | c79c9fb | 2011-12-22 18:19:55 -0300 | [diff] [blame] | 153 | static int dib0070_set_bandwidth(struct dvb_frontend *fe) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 154 | { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 155 | struct dib0070_state *state = fe->tuner_priv; |
| 156 | u16 tmp = dib0070_read_reg(state, 0x02) & 0x3fff; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 157 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 158 | if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 7000) |
| 159 | tmp |= (0 << 14); |
| 160 | else if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 6000) |
| 161 | tmp |= (1 << 14); |
| 162 | else if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 5000) |
| 163 | tmp |= (2 << 14); |
| 164 | else |
| 165 | tmp |= (3 << 14); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 166 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 167 | dib0070_write_reg(state, 0x02, tmp); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 168 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 169 | /* sharpen the BB filter in ISDB-T to have higher immunity to adjacent channels */ |
| 170 | if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) { |
| 171 | u16 value = dib0070_read_reg(state, 0x17); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 172 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 173 | dib0070_write_reg(state, 0x17, value & 0xfffc); |
| 174 | tmp = dib0070_read_reg(state, 0x01) & 0x01ff; |
| 175 | dib0070_write_reg(state, 0x01, tmp | (60 << 9)); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 176 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 177 | dib0070_write_reg(state, 0x17, value); |
| 178 | } |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 182 | static int dib0070_captrim(struct dib0070_state *state, enum frontend_tune_state *tune_state) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 183 | { |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 184 | int8_t step_sign; |
| 185 | u16 adc; |
| 186 | int ret = 0; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 187 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 188 | if (*tune_state == CT_TUNER_STEP_0) { |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 189 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 190 | dib0070_write_reg(state, 0x0f, 0xed10); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 191 | dib0070_write_reg(state, 0x17, 0x0034); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 192 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 193 | dib0070_write_reg(state, 0x18, 0x0032); |
| 194 | state->step = state->captrim = state->fcaptrim = 64; |
| 195 | state->adc_diff = 3000; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 196 | ret = 20; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 197 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 198 | *tune_state = CT_TUNER_STEP_1; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 199 | } else if (*tune_state == CT_TUNER_STEP_1) { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 200 | state->step /= 2; |
| 201 | dib0070_write_reg(state, 0x14, state->lo4 | state->captrim); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 202 | ret = 15; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 203 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 204 | *tune_state = CT_TUNER_STEP_2; |
| 205 | } else if (*tune_state == CT_TUNER_STEP_2) { |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 206 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 207 | adc = dib0070_read_reg(state, 0x19); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 208 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 209 | dprintk("CAPTRIM=%hd; ADC = %hd (ADC) & %dmV", state->captrim, adc, (u32) adc*(u32)1800/(u32)1024); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 210 | |
| 211 | if (adc >= 400) { |
| 212 | adc -= 400; |
| 213 | step_sign = -1; |
| 214 | } else { |
| 215 | adc = 400 - adc; |
| 216 | step_sign = 1; |
| 217 | } |
| 218 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 219 | if (adc < state->adc_diff) { |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 220 | dprintk("CAPTRIM=%hd is closer to target (%hd/%hd)", state->captrim, adc, state->adc_diff); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 221 | state->adc_diff = adc; |
| 222 | state->fcaptrim = state->captrim; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 223 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 224 | |
| 225 | |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 226 | } |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 227 | state->captrim += (step_sign * state->step); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 228 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 229 | if (state->step >= 1) |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 230 | *tune_state = CT_TUNER_STEP_1; |
| 231 | else |
| 232 | *tune_state = CT_TUNER_STEP_3; |
| 233 | |
| 234 | } else if (*tune_state == CT_TUNER_STEP_3) { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 235 | dib0070_write_reg(state, 0x14, state->lo4 | state->fcaptrim); |
| 236 | dib0070_write_reg(state, 0x18, 0x07ff); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 237 | *tune_state = CT_TUNER_STEP_4; |
| 238 | } |
| 239 | |
| 240 | return ret; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 241 | } |
| 242 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 243 | static int dib0070_set_ctrl_lo5(struct dvb_frontend *fe, u8 vco_bias_trim, u8 hf_div_trim, u8 cp_current, u8 third_order_filt) |
| 244 | { |
| 245 | struct dib0070_state *state = fe->tuner_priv; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 246 | u16 lo5 = (third_order_filt << 14) | (0 << 13) | (1 << 12) | (3 << 9) | (cp_current << 6) | (hf_div_trim << 3) | (vco_bias_trim << 0); |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 247 | dprintk("CTRL_LO5: 0x%x", lo5); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 248 | return dib0070_write_reg(state, 0x15, lo5); |
| 249 | } |
| 250 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 251 | void dib0070_ctrl_agc_filter(struct dvb_frontend *fe, u8 open) |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 252 | { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 253 | struct dib0070_state *state = fe->tuner_priv; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 254 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 255 | if (open) { |
| 256 | dib0070_write_reg(state, 0x1b, 0xff00); |
| 257 | dib0070_write_reg(state, 0x1a, 0x0000); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 258 | } else { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 259 | dib0070_write_reg(state, 0x1b, 0x4112); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 260 | if (state->cfg->vga_filter != 0) { |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 261 | dib0070_write_reg(state, 0x1a, state->cfg->vga_filter); |
| 262 | dprintk("vga filter register is set to %x", state->cfg->vga_filter); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 263 | } else |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 264 | dib0070_write_reg(state, 0x1a, 0x0009); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 265 | } |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 266 | } |
| 267 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 268 | EXPORT_SYMBOL(dib0070_ctrl_agc_filter); |
| 269 | struct dib0070_tuning { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 270 | u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */ |
| 271 | u8 switch_trim; |
| 272 | u8 vco_band; |
| 273 | u8 hfdiv; |
| 274 | u8 vco_multi; |
| 275 | u8 presc; |
| 276 | u8 wbdmux; |
| 277 | u16 tuner_enable; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | struct dib0070_lna_match { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 281 | u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */ |
| 282 | u8 lna_band; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | static const struct dib0070_tuning dib0070s_tuning_table[] = { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 286 | { 570000, 2, 1, 3, 6, 6, 2, 0x4000 | 0x0800 }, /* UHF */ |
| 287 | { 700000, 2, 0, 2, 4, 2, 2, 0x4000 | 0x0800 }, |
| 288 | { 863999, 2, 1, 2, 4, 2, 2, 0x4000 | 0x0800 }, |
| 289 | { 1500000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND */ |
| 290 | { 1600000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 }, |
| 291 | { 2000000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 }, |
| 292 | { 0xffffffff, 0, 0, 8, 1, 2, 1, 0x8000 | 0x1000 }, /* SBAND */ |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static const struct dib0070_tuning dib0070_tuning_table[] = { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 296 | { 115000, 1, 0, 7, 24, 2, 1, 0x8000 | 0x1000 }, /* FM below 92MHz cannot be tuned */ |
| 297 | { 179500, 1, 0, 3, 16, 2, 1, 0x8000 | 0x1000 }, /* VHF */ |
| 298 | { 189999, 1, 1, 3, 16, 2, 1, 0x8000 | 0x1000 }, |
| 299 | { 250000, 1, 0, 6, 12, 2, 1, 0x8000 | 0x1000 }, |
| 300 | { 569999, 2, 1, 5, 6, 2, 2, 0x4000 | 0x0800 }, /* UHF */ |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 301 | { 699999, 2, 0, 1, 4, 2, 2, 0x4000 | 0x0800 }, |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 302 | { 863999, 2, 1, 1, 4, 2, 2, 0x4000 | 0x0800 }, |
| 303 | { 0xffffffff, 0, 1, 0, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND or everything higher than UHF */ |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | static const struct dib0070_lna_match dib0070_lna_flip_chip[] = { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 307 | { 180000, 0 }, /* VHF */ |
| 308 | { 188000, 1 }, |
| 309 | { 196400, 2 }, |
| 310 | { 250000, 3 }, |
| 311 | { 550000, 0 }, /* UHF */ |
| 312 | { 590000, 1 }, |
| 313 | { 666000, 3 }, |
| 314 | { 864000, 5 }, |
| 315 | { 1500000, 0 }, /* LBAND or everything higher than UHF */ |
| 316 | { 1600000, 1 }, |
| 317 | { 2000000, 3 }, |
| 318 | { 0xffffffff, 7 }, |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | static const struct dib0070_lna_match dib0070_lna[] = { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 322 | { 180000, 0 }, /* VHF */ |
| 323 | { 188000, 1 }, |
| 324 | { 196400, 2 }, |
| 325 | { 250000, 3 }, |
| 326 | { 550000, 2 }, /* UHF */ |
| 327 | { 650000, 3 }, |
| 328 | { 750000, 5 }, |
| 329 | { 850000, 6 }, |
| 330 | { 864000, 7 }, |
| 331 | { 1500000, 0 }, /* LBAND or everything higher than UHF */ |
| 332 | { 1600000, 1 }, |
| 333 | { 2000000, 3 }, |
| 334 | { 0xffffffff, 7 }, |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 335 | }; |
| 336 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 337 | #define LPF 100 |
Mauro Carvalho Chehab | c79c9fb | 2011-12-22 18:19:55 -0300 | [diff] [blame] | 338 | static int dib0070_tune_digital(struct dvb_frontend *fe) |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 339 | { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 340 | struct dib0070_state *state = fe->tuner_priv; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 341 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 342 | const struct dib0070_tuning *tune; |
| 343 | const struct dib0070_lna_match *lna_match; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 344 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 345 | enum frontend_tune_state *tune_state = &state->tune_state; |
| 346 | int ret = 10; /* 1ms is the default delay most of the time */ |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 347 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 348 | u8 band = (u8)BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency/1000); |
| 349 | u32 freq = fe->dtv_property_cache.frequency/1000 + (band == BAND_VHF ? state->cfg->freq_offset_khz_vhf : state->cfg->freq_offset_khz_uhf); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 350 | |
| 351 | #ifdef CONFIG_SYS_ISDBT |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 352 | if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1) |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 353 | if (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) |
| 354 | && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1))) |
| 355 | || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0) |
| 356 | && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == (state->fe->dtv_property_cache.isdbt_sb_segment_count / 2))) |
| 357 | || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0) |
| 358 | && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))) |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 359 | freq += 850; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 360 | #endif |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 361 | if (state->current_rf != freq) { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 362 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 363 | switch (state->revision) { |
| 364 | case DIB0070S_P1A: |
| 365 | tune = dib0070s_tuning_table; |
| 366 | lna_match = dib0070_lna; |
| 367 | break; |
| 368 | default: |
| 369 | tune = dib0070_tuning_table; |
| 370 | if (state->cfg->flip_chip) |
| 371 | lna_match = dib0070_lna_flip_chip; |
| 372 | else |
| 373 | lna_match = dib0070_lna; |
| 374 | break; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 375 | } |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 376 | while (freq > tune->max_freq) /* find the right one */ |
| 377 | tune++; |
| 378 | while (freq > lna_match->max_freq) /* find the right one */ |
| 379 | lna_match++; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 380 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 381 | state->current_tune_table_index = tune; |
| 382 | state->lna_match = lna_match; |
| 383 | } |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 384 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 385 | if (*tune_state == CT_TUNER_START) { |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 386 | dprintk("Tuning for Band: %hd (%d kHz)", band, freq); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 387 | if (state->current_rf != freq) { |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 388 | u8 REFDIV; |
| 389 | u32 FBDiv, Rest, FREF, VCOF_kHz; |
| 390 | u8 Den; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 391 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 392 | state->current_rf = freq; |
| 393 | state->lo4 = (state->current_tune_table_index->vco_band << 11) | (state->current_tune_table_index->hfdiv << 7); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 394 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 395 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 396 | dib0070_write_reg(state, 0x17, 0x30); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 397 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 398 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 399 | VCOF_kHz = state->current_tune_table_index->vco_multi * freq * 2; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 400 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 401 | switch (band) { |
| 402 | case BAND_VHF: |
| 403 | REFDIV = (u8) ((state->cfg->clock_khz + 9999) / 10000); |
| 404 | break; |
| 405 | case BAND_FM: |
| 406 | REFDIV = (u8) ((state->cfg->clock_khz) / 1000); |
| 407 | break; |
| 408 | default: |
| 409 | REFDIV = (u8) (state->cfg->clock_khz / 10000); |
| 410 | break; |
| 411 | } |
| 412 | FREF = state->cfg->clock_khz / REFDIV; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 413 | |
| 414 | |
| 415 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 416 | switch (state->revision) { |
| 417 | case DIB0070S_P1A: |
| 418 | FBDiv = (VCOF_kHz / state->current_tune_table_index->presc / FREF); |
| 419 | Rest = (VCOF_kHz / state->current_tune_table_index->presc) - FBDiv * FREF; |
| 420 | break; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 421 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 422 | case DIB0070_P1G: |
| 423 | case DIB0070_P1F: |
| 424 | default: |
| 425 | FBDiv = (freq / (FREF / 2)); |
| 426 | Rest = 2 * freq - FBDiv * FREF; |
| 427 | break; |
| 428 | } |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 429 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 430 | if (Rest < LPF) |
| 431 | Rest = 0; |
| 432 | else if (Rest < 2 * LPF) |
| 433 | Rest = 2 * LPF; |
| 434 | else if (Rest > (FREF - LPF)) { |
| 435 | Rest = 0; |
| 436 | FBDiv += 1; |
| 437 | } else if (Rest > (FREF - 2 * LPF)) |
| 438 | Rest = FREF - 2 * LPF; |
| 439 | Rest = (Rest * 6528) / (FREF / 10); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 440 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 441 | Den = 1; |
| 442 | if (Rest > 0) { |
| 443 | state->lo4 |= (1 << 14) | (1 << 12); |
| 444 | Den = 255; |
| 445 | } |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 446 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 447 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 448 | dib0070_write_reg(state, 0x11, (u16)FBDiv); |
| 449 | dib0070_write_reg(state, 0x12, (Den << 8) | REFDIV); |
| 450 | dib0070_write_reg(state, 0x13, (u16) Rest); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 451 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 452 | if (state->revision == DIB0070S_P1A) { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 453 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 454 | if (band == BAND_SBAND) { |
| 455 | dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0); |
| 456 | dib0070_write_reg(state, 0x1d, 0xFFFF); |
| 457 | } else |
| 458 | dib0070_set_ctrl_lo5(fe, 5, 4, 3, 1); |
| 459 | } |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 460 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 461 | dib0070_write_reg(state, 0x20, |
| 462 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001 | state->current_tune_table_index->tuner_enable); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 463 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 464 | dprintk("REFDIV: %hd, FREF: %d", REFDIV, FREF); |
| 465 | dprintk("FBDIV: %d, Rest: %d", FBDiv, Rest); |
| 466 | dprintk("Num: %hd, Den: %hd, SD: %hd", (u16) Rest, Den, (state->lo4 >> 12) & 0x1); |
| 467 | dprintk("HFDIV code: %hd", state->current_tune_table_index->hfdiv); |
| 468 | dprintk("VCO = %hd", state->current_tune_table_index->vco_band); |
| 469 | dprintk("VCOF: ((%hd*%d) << 1))", state->current_tune_table_index->vco_multi, freq); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 470 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 471 | *tune_state = CT_TUNER_STEP_0; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 472 | } else { /* we are already tuned to this frequency - the configuration is correct */ |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 473 | ret = 50; /* wakeup time */ |
| 474 | *tune_state = CT_TUNER_STEP_5; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 475 | } |
| 476 | } else if ((*tune_state > CT_TUNER_START) && (*tune_state < CT_TUNER_STEP_4)) { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 477 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 478 | ret = dib0070_captrim(state, tune_state); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 479 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 480 | } else if (*tune_state == CT_TUNER_STEP_4) { |
| 481 | const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain; |
| 482 | if (tmp != NULL) { |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 483 | while (freq/1000 > tmp->freq) /* find the right one */ |
| 484 | tmp++; |
| 485 | dib0070_write_reg(state, 0x0f, |
| 486 | (0 << 15) | (1 << 14) | (3 << 12) |
| 487 | | (tmp->wbd_gain_val << 9) | (0 << 8) | (1 << 7) |
| 488 | | (state->current_tune_table_index->wbdmux << 0)); |
| 489 | state->wbd_gain_current = tmp->wbd_gain_val; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 490 | } else { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 491 | dib0070_write_reg(state, 0x0f, |
| 492 | (0 << 15) | (1 << 14) | (3 << 12) | (6 << 9) | (0 << 8) | (1 << 7) | (state->current_tune_table_index-> |
| 493 | wbdmux << 0)); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 494 | state->wbd_gain_current = 6; |
| 495 | } |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 496 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 497 | dib0070_write_reg(state, 0x06, 0x3fff); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 498 | dib0070_write_reg(state, 0x07, |
| 499 | (state->current_tune_table_index->switch_trim << 11) | (7 << 8) | (state->lna_match->lna_band << 3) | (3 << 0)); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 500 | dib0070_write_reg(state, 0x08, (state->lna_match->lna_band << 10) | (3 << 7) | (127)); |
| 501 | dib0070_write_reg(state, 0x0d, 0x0d80); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 502 | |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 503 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 504 | dib0070_write_reg(state, 0x18, 0x07ff); |
| 505 | dib0070_write_reg(state, 0x17, 0x0033); |
| 506 | |
| 507 | |
| 508 | *tune_state = CT_TUNER_STEP_5; |
| 509 | } else if (*tune_state == CT_TUNER_STEP_5) { |
Mauro Carvalho Chehab | c79c9fb | 2011-12-22 18:19:55 -0300 | [diff] [blame] | 510 | dib0070_set_bandwidth(fe); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 511 | *tune_state = CT_TUNER_STOP; |
| 512 | } else { |
| 513 | ret = FE_CALLBACK_TIME_NEVER; /* tuner finished, time to call again infinite */ |
| 514 | } |
| 515 | return ret; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 516 | } |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 517 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 518 | |
Mauro Carvalho Chehab | 14d24d1 | 2011-12-24 12:24:33 -0300 | [diff] [blame] | 519 | static int dib0070_tune(struct dvb_frontend *fe) |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 520 | { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 521 | struct dib0070_state *state = fe->tuner_priv; |
| 522 | uint32_t ret; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 523 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 524 | state->tune_state = CT_TUNER_START; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 525 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 526 | do { |
Mauro Carvalho Chehab | c79c9fb | 2011-12-22 18:19:55 -0300 | [diff] [blame] | 527 | ret = dib0070_tune_digital(fe); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 528 | if (ret != FE_CALLBACK_TIME_NEVER) |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 529 | msleep(ret/10); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 530 | else |
| 531 | break; |
| 532 | } while (state->tune_state != CT_TUNER_STOP); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 533 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 534 | return 0; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static int dib0070_wakeup(struct dvb_frontend *fe) |
| 538 | { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 539 | struct dib0070_state *state = fe->tuner_priv; |
| 540 | if (state->cfg->sleep) |
| 541 | state->cfg->sleep(fe, 0); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int dib0070_sleep(struct dvb_frontend *fe) |
| 546 | { |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 547 | struct dib0070_state *state = fe->tuner_priv; |
| 548 | if (state->cfg->sleep) |
| 549 | state->cfg->sleep(fe, 1); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 553 | u8 dib0070_get_rf_output(struct dvb_frontend *fe) |
| 554 | { |
| 555 | struct dib0070_state *state = fe->tuner_priv; |
| 556 | return (dib0070_read_reg(state, 0x07) >> 11) & 0x3; |
| 557 | } |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 558 | EXPORT_SYMBOL(dib0070_get_rf_output); |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 559 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 560 | int dib0070_set_rf_output(struct dvb_frontend *fe, u8 no) |
| 561 | { |
| 562 | struct dib0070_state *state = fe->tuner_priv; |
| 563 | u16 rxrf2 = dib0070_read_reg(state, 0x07) & 0xfe7ff; |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 564 | if (no > 3) |
| 565 | no = 3; |
| 566 | if (no < 1) |
| 567 | no = 1; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 568 | return dib0070_write_reg(state, 0x07, rxrf2 | (no << 11)); |
| 569 | } |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 570 | EXPORT_SYMBOL(dib0070_set_rf_output); |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 571 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 572 | static const u16 dib0070_p1f_defaults[] = |
| 573 | |
| 574 | { |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 575 | 7, 0x02, |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 576 | 0x0008, |
| 577 | 0x0000, |
| 578 | 0x0000, |
| 579 | 0x0000, |
| 580 | 0x0000, |
| 581 | 0x0002, |
| 582 | 0x0100, |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 583 | |
| 584 | 3, 0x0d, |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 585 | 0x0d80, |
| 586 | 0x0001, |
| 587 | 0x0000, |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 588 | |
| 589 | 4, 0x11, |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 590 | 0x0000, |
| 591 | 0x0103, |
| 592 | 0x0000, |
| 593 | 0x0000, |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 594 | |
| 595 | 3, 0x16, |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 596 | 0x0004 | 0x0040, |
| 597 | 0x0030, |
| 598 | 0x07ff, |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 599 | |
| 600 | 6, 0x1b, |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 601 | 0x4112, |
| 602 | 0xff00, |
| 603 | 0xc07f, |
| 604 | 0x0000, |
| 605 | 0x0180, |
| 606 | 0x4000 | 0x0800 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001, |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 607 | |
| 608 | 0, |
| 609 | }; |
| 610 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 611 | static u16 dib0070_read_wbd_offset(struct dib0070_state *state, u8 gain) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 612 | { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 613 | u16 tuner_en = dib0070_read_reg(state, 0x20); |
| 614 | u16 offset; |
Patrick Boettcher | 3cb2c39 | 2008-01-25 07:25:20 -0300 | [diff] [blame] | 615 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 616 | dib0070_write_reg(state, 0x18, 0x07ff); |
| 617 | dib0070_write_reg(state, 0x20, 0x0800 | 0x4000 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001); |
| 618 | dib0070_write_reg(state, 0x0f, (1 << 14) | (2 << 12) | (gain << 9) | (1 << 8) | (1 << 7) | (0 << 0)); |
| 619 | msleep(9); |
| 620 | offset = dib0070_read_reg(state, 0x19); |
| 621 | dib0070_write_reg(state, 0x20, tuner_en); |
| 622 | return offset; |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 623 | } |
Patrick Boettcher | 3cb2c39 | 2008-01-25 07:25:20 -0300 | [diff] [blame] | 624 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 625 | static void dib0070_wbd_offset_calibration(struct dib0070_state *state) |
| 626 | { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 627 | u8 gain; |
| 628 | for (gain = 6; gain < 8; gain++) { |
| 629 | state->wbd_offset_3_3[gain - 6] = ((dib0070_read_wbd_offset(state, gain) * 8 * 18 / 33 + 1) / 2); |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 630 | dprintk("Gain: %d, WBDOffset (3.3V) = %hd", gain, state->wbd_offset_3_3[gain-6]); |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 631 | } |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | u16 dib0070_wbd_offset(struct dvb_frontend *fe) |
| 635 | { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 636 | struct dib0070_state *state = fe->tuner_priv; |
| 637 | const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain; |
| 638 | u32 freq = fe->dtv_property_cache.frequency/1000; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 639 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 640 | if (tmp != NULL) { |
| 641 | while (freq/1000 > tmp->freq) /* find the right one */ |
| 642 | tmp++; |
| 643 | state->wbd_gain_current = tmp->wbd_gain_val; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 644 | } else |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 645 | state->wbd_gain_current = 6; |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 646 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 647 | return state->wbd_offset_3_3[state->wbd_gain_current - 6]; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 648 | } |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 649 | EXPORT_SYMBOL(dib0070_wbd_offset); |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 650 | |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 651 | #define pgm_read_word(w) (*w) |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 652 | static int dib0070_reset(struct dvb_frontend *fe) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 653 | { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 654 | struct dib0070_state *state = fe->tuner_priv; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 655 | u16 l, r, *n; |
| 656 | |
| 657 | HARD_RESET(state); |
| 658 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 659 | |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 660 | #ifndef FORCE_SBAND_TUNER |
| 661 | if ((dib0070_read_reg(state, 0x22) >> 9) & 0x1) |
| 662 | state->revision = (dib0070_read_reg(state, 0x1f) >> 8) & 0xff; |
| 663 | else |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 664 | #else |
| 665 | #warning forcing SBAND |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 666 | #endif |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 667 | state->revision = DIB0070S_P1A; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 668 | |
| 669 | /* P1F or not */ |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 670 | dprintk("Revision: %x", state->revision); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 671 | |
| 672 | if (state->revision == DIB0070_P1D) { |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 673 | dprintk("Error: this driver is not to be used meant for P1D or earlier"); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 674 | return -EINVAL; |
| 675 | } |
| 676 | |
| 677 | n = (u16 *) dib0070_p1f_defaults; |
| 678 | l = pgm_read_word(n++); |
| 679 | while (l) { |
| 680 | r = pgm_read_word(n++); |
| 681 | do { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 682 | dib0070_write_reg(state, (u8)r, pgm_read_word(n++)); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 683 | r++; |
| 684 | } while (--l); |
| 685 | l = pgm_read_word(n++); |
| 686 | } |
| 687 | |
| 688 | if (state->cfg->force_crystal_mode != 0) |
| 689 | r = state->cfg->force_crystal_mode; |
| 690 | else if (state->cfg->clock_khz >= 24000) |
| 691 | r = 1; |
| 692 | else |
| 693 | r = 2; |
| 694 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 695 | |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 696 | r |= state->cfg->osc_buffer_state << 3; |
| 697 | |
| 698 | dib0070_write_reg(state, 0x10, r); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 699 | dib0070_write_reg(state, 0x1f, (1 << 8) | ((state->cfg->clock_pad_drive & 0xf) << 5)); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 700 | |
| 701 | if (state->cfg->invert_iq) { |
| 702 | r = dib0070_read_reg(state, 0x02) & 0xffdf; |
| 703 | dib0070_write_reg(state, 0x02, r | (1 << 5)); |
| 704 | } |
| 705 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 706 | if (state->revision == DIB0070S_P1A) |
| 707 | dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0); |
| 708 | else |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 709 | dib0070_set_ctrl_lo5(fe, 5, 4, state->cfg->charge_pump, state->cfg->enable_third_order_filter); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 710 | |
| 711 | dib0070_write_reg(state, 0x01, (54 << 9) | 0xc8); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 712 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 713 | dib0070_wbd_offset_calibration(state); |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 714 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | static int dib0070_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 719 | { |
| 720 | struct dib0070_state *state = fe->tuner_priv; |
| 721 | |
| 722 | *frequency = 1000 * state->current_rf; |
| 723 | return 0; |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 724 | } |
| 725 | |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 726 | static int dib0070_release(struct dvb_frontend *fe) |
| 727 | { |
| 728 | kfree(fe->tuner_priv); |
| 729 | fe->tuner_priv = NULL; |
| 730 | return 0; |
| 731 | } |
| 732 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 733 | static const struct dvb_tuner_ops dib0070_ops = { |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 734 | .info = { |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 735 | .name = "DiBcom DiB0070", |
| 736 | .frequency_min = 45000000, |
| 737 | .frequency_max = 860000000, |
| 738 | .frequency_step = 1000, |
| 739 | }, |
| 740 | .release = dib0070_release, |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 741 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 742 | .init = dib0070_wakeup, |
| 743 | .sleep = dib0070_sleep, |
| 744 | .set_params = dib0070_tune, |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 745 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 746 | .get_frequency = dib0070_get_frequency, |
Patrick Boettcher | 2a6a30e | 2009-08-17 05:13:28 -0300 | [diff] [blame] | 747 | // .get_bandwidth = dib0070_get_bandwidth |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 748 | }; |
| 749 | |
Olivier Grenie | 9c78303 | 2009-12-07 07:49:40 -0300 | [diff] [blame] | 750 | struct dvb_frontend *dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dib0070_config *cfg) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 751 | { |
| 752 | struct dib0070_state *state = kzalloc(sizeof(struct dib0070_state), GFP_KERNEL); |
| 753 | if (state == NULL) |
| 754 | return NULL; |
| 755 | |
| 756 | state->cfg = cfg; |
| 757 | state->i2c = i2c; |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 758 | state->fe = fe; |
Patrick Boettcher | 79fcce3 | 2011-08-03 12:08:21 -0300 | [diff] [blame] | 759 | mutex_init(&state->i2c_buffer_lock); |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 760 | fe->tuner_priv = state; |
| 761 | |
Patrick Boettcher | 7e5ce65 | 2009-08-03 13:43:40 -0300 | [diff] [blame] | 762 | if (dib0070_reset(fe) != 0) |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 763 | goto free_mem; |
| 764 | |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 765 | printk(KERN_INFO "DiB0070: successfully identified\n"); |
| 766 | memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops)); |
| 767 | |
| 768 | fe->tuner_priv = state; |
| 769 | return fe; |
| 770 | |
Olivier Grenie | 03245a5 | 2009-12-04 13:27:57 -0300 | [diff] [blame] | 771 | free_mem: |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 772 | kfree(state); |
| 773 | fe->tuner_priv = NULL; |
| 774 | return NULL; |
| 775 | } |
Patrick Boettcher | 01373a5 | 2007-07-30 12:49:04 -0300 | [diff] [blame] | 776 | EXPORT_SYMBOL(dib0070_attach); |
| 777 | |
| 778 | MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>"); |
| 779 | MODULE_DESCRIPTION("Driver for the DiBcom 0070 base-band RF Tuner"); |
| 780 | MODULE_LICENSE("GPL"); |