Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Afatech AF9033 demodulator driver |
| 3 | * |
| 4 | * Copyright (C) 2009 Antti Palosaari <crope@iki.fi> |
| 5 | * Copyright (C) 2012 Antti Palosaari <crope@iki.fi> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "af9033_priv.h" |
| 23 | |
| 24 | struct af9033_state { |
| 25 | struct i2c_adapter *i2c; |
| 26 | struct dvb_frontend fe; |
| 27 | struct af9033_config cfg; |
| 28 | |
| 29 | u32 bandwidth_hz; |
| 30 | bool ts_mode_parallel; |
| 31 | bool ts_mode_serial; |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 32 | |
| 33 | u32 ber; |
| 34 | u32 ucb; |
| 35 | unsigned long last_stat_check; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /* write multiple registers */ |
| 39 | static int af9033_wr_regs(struct af9033_state *state, u32 reg, const u8 *val, |
| 40 | int len) |
| 41 | { |
| 42 | int ret; |
| 43 | u8 buf[3 + len]; |
| 44 | struct i2c_msg msg[1] = { |
| 45 | { |
| 46 | .addr = state->cfg.i2c_addr, |
| 47 | .flags = 0, |
| 48 | .len = sizeof(buf), |
| 49 | .buf = buf, |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | buf[0] = (reg >> 16) & 0xff; |
| 54 | buf[1] = (reg >> 8) & 0xff; |
| 55 | buf[2] = (reg >> 0) & 0xff; |
| 56 | memcpy(&buf[3], val, len); |
| 57 | |
| 58 | ret = i2c_transfer(state->i2c, msg, 1); |
| 59 | if (ret == 1) { |
| 60 | ret = 0; |
| 61 | } else { |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 62 | dev_warn(&state->i2c->dev, "%s: i2c wr failed=%d reg=%06x " \ |
| 63 | "len=%d\n", KBUILD_MODNAME, ret, reg, len); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 64 | ret = -EREMOTEIO; |
| 65 | } |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | /* read multiple registers */ |
| 71 | static int af9033_rd_regs(struct af9033_state *state, u32 reg, u8 *val, int len) |
| 72 | { |
| 73 | int ret; |
| 74 | u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff, |
| 75 | (reg >> 0) & 0xff }; |
| 76 | struct i2c_msg msg[2] = { |
| 77 | { |
| 78 | .addr = state->cfg.i2c_addr, |
| 79 | .flags = 0, |
| 80 | .len = sizeof(buf), |
| 81 | .buf = buf |
| 82 | }, { |
| 83 | .addr = state->cfg.i2c_addr, |
| 84 | .flags = I2C_M_RD, |
| 85 | .len = len, |
| 86 | .buf = val |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | ret = i2c_transfer(state->i2c, msg, 2); |
| 91 | if (ret == 2) { |
| 92 | ret = 0; |
| 93 | } else { |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 94 | dev_warn(&state->i2c->dev, "%s: i2c rd failed=%d reg=%06x " \ |
| 95 | "len=%d\n", KBUILD_MODNAME, ret, reg, len); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 96 | ret = -EREMOTEIO; |
| 97 | } |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* write single register */ |
| 104 | static int af9033_wr_reg(struct af9033_state *state, u32 reg, u8 val) |
| 105 | { |
| 106 | return af9033_wr_regs(state, reg, &val, 1); |
| 107 | } |
| 108 | |
| 109 | /* read single register */ |
| 110 | static int af9033_rd_reg(struct af9033_state *state, u32 reg, u8 *val) |
| 111 | { |
| 112 | return af9033_rd_regs(state, reg, val, 1); |
| 113 | } |
| 114 | |
| 115 | /* write single register with mask */ |
| 116 | static int af9033_wr_reg_mask(struct af9033_state *state, u32 reg, u8 val, |
| 117 | u8 mask) |
| 118 | { |
| 119 | int ret; |
| 120 | u8 tmp; |
| 121 | |
| 122 | /* no need for read if whole reg is written */ |
| 123 | if (mask != 0xff) { |
| 124 | ret = af9033_rd_regs(state, reg, &tmp, 1); |
| 125 | if (ret) |
| 126 | return ret; |
| 127 | |
| 128 | val &= mask; |
| 129 | tmp &= ~mask; |
| 130 | val |= tmp; |
| 131 | } |
| 132 | |
| 133 | return af9033_wr_regs(state, reg, &val, 1); |
| 134 | } |
| 135 | |
| 136 | /* read single register with mask */ |
| 137 | static int af9033_rd_reg_mask(struct af9033_state *state, u32 reg, u8 *val, |
| 138 | u8 mask) |
| 139 | { |
| 140 | int ret, i; |
| 141 | u8 tmp; |
| 142 | |
| 143 | ret = af9033_rd_regs(state, reg, &tmp, 1); |
| 144 | if (ret) |
| 145 | return ret; |
| 146 | |
| 147 | tmp &= mask; |
| 148 | |
| 149 | /* find position of the first bit */ |
| 150 | for (i = 0; i < 8; i++) { |
| 151 | if ((mask >> i) & 0x01) |
| 152 | break; |
| 153 | } |
| 154 | *val = tmp >> i; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 159 | static u32 af9033_div(struct af9033_state *state, u32 a, u32 b, u32 x) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 160 | { |
| 161 | u32 r = 0, c = 0, i; |
| 162 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 163 | dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 164 | |
| 165 | if (a > b) { |
| 166 | c = a / b; |
| 167 | a = a - c * b; |
| 168 | } |
| 169 | |
| 170 | for (i = 0; i < x; i++) { |
| 171 | if (a >= b) { |
| 172 | r += 1; |
| 173 | a -= b; |
| 174 | } |
| 175 | a <<= 1; |
| 176 | r <<= 1; |
| 177 | } |
| 178 | r = (c << (u32)x) + r; |
| 179 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 180 | dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n", |
| 181 | __func__, a, b, x, r, r); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 182 | |
| 183 | return r; |
| 184 | } |
| 185 | |
| 186 | static void af9033_release(struct dvb_frontend *fe) |
| 187 | { |
| 188 | struct af9033_state *state = fe->demodulator_priv; |
| 189 | |
| 190 | kfree(state); |
| 191 | } |
| 192 | |
| 193 | static int af9033_init(struct dvb_frontend *fe) |
| 194 | { |
| 195 | struct af9033_state *state = fe->demodulator_priv; |
| 196 | int ret, i, len; |
| 197 | const struct reg_val *init; |
| 198 | u8 buf[4]; |
| 199 | u32 adc_cw, clock_cw; |
| 200 | struct reg_val_mask tab[] = { |
| 201 | { 0x80fb24, 0x00, 0x08 }, |
| 202 | { 0x80004c, 0x00, 0xff }, |
| 203 | { 0x00f641, state->cfg.tuner, 0xff }, |
| 204 | { 0x80f5ca, 0x01, 0x01 }, |
| 205 | { 0x80f715, 0x01, 0x01 }, |
| 206 | { 0x00f41f, 0x04, 0x04 }, |
| 207 | { 0x00f41a, 0x01, 0x01 }, |
| 208 | { 0x80f731, 0x00, 0x01 }, |
| 209 | { 0x00d91e, 0x00, 0x01 }, |
| 210 | { 0x00d919, 0x00, 0x01 }, |
| 211 | { 0x80f732, 0x00, 0x01 }, |
| 212 | { 0x00d91f, 0x00, 0x01 }, |
| 213 | { 0x00d91a, 0x00, 0x01 }, |
| 214 | { 0x80f730, 0x00, 0x01 }, |
| 215 | { 0x80f778, 0x00, 0xff }, |
| 216 | { 0x80f73c, 0x01, 0x01 }, |
| 217 | { 0x80f776, 0x00, 0x01 }, |
| 218 | { 0x00d8fd, 0x01, 0xff }, |
| 219 | { 0x00d830, 0x01, 0xff }, |
| 220 | { 0x00d831, 0x00, 0xff }, |
| 221 | { 0x00d832, 0x00, 0xff }, |
| 222 | { 0x80f985, state->ts_mode_serial, 0x01 }, |
| 223 | { 0x80f986, state->ts_mode_parallel, 0x01 }, |
| 224 | { 0x00d827, 0x00, 0xff }, |
| 225 | { 0x00d829, 0x00, 0xff }, |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 226 | { 0x800045, state->cfg.adc_multiplier, 0xff }, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | /* program clock control */ |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 230 | clock_cw = af9033_div(state, state->cfg.clock, 1000000ul, 19ul); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 231 | buf[0] = (clock_cw >> 0) & 0xff; |
| 232 | buf[1] = (clock_cw >> 8) & 0xff; |
| 233 | buf[2] = (clock_cw >> 16) & 0xff; |
| 234 | buf[3] = (clock_cw >> 24) & 0xff; |
| 235 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 236 | dev_dbg(&state->i2c->dev, "%s: clock=%d clock_cw=%08x\n", |
| 237 | __func__, state->cfg.clock, clock_cw); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 238 | |
| 239 | ret = af9033_wr_regs(state, 0x800025, buf, 4); |
| 240 | if (ret < 0) |
| 241 | goto err; |
| 242 | |
| 243 | /* program ADC control */ |
| 244 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
| 245 | if (clock_adc_lut[i].clock == state->cfg.clock) |
| 246 | break; |
| 247 | } |
| 248 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 249 | adc_cw = af9033_div(state, clock_adc_lut[i].adc, 1000000ul, 19ul); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 250 | buf[0] = (adc_cw >> 0) & 0xff; |
| 251 | buf[1] = (adc_cw >> 8) & 0xff; |
| 252 | buf[2] = (adc_cw >> 16) & 0xff; |
| 253 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 254 | dev_dbg(&state->i2c->dev, "%s: adc=%d adc_cw=%06x\n", |
| 255 | __func__, clock_adc_lut[i].adc, adc_cw); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 256 | |
| 257 | ret = af9033_wr_regs(state, 0x80f1cd, buf, 3); |
| 258 | if (ret < 0) |
| 259 | goto err; |
| 260 | |
| 261 | /* program register table */ |
| 262 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 263 | ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val, |
| 264 | tab[i].mask); |
| 265 | if (ret < 0) |
| 266 | goto err; |
| 267 | } |
| 268 | |
| 269 | /* settings for TS interface */ |
| 270 | if (state->cfg.ts_mode == AF9033_TS_MODE_USB) { |
| 271 | ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01); |
| 272 | if (ret < 0) |
| 273 | goto err; |
| 274 | |
| 275 | ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01); |
| 276 | if (ret < 0) |
| 277 | goto err; |
| 278 | } else { |
| 279 | ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01); |
| 280 | if (ret < 0) |
| 281 | goto err; |
| 282 | |
| 283 | ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01); |
| 284 | if (ret < 0) |
| 285 | goto err; |
| 286 | } |
| 287 | |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 288 | /* |
| 289 | * FIXME: These inits are logically property of demodulator driver |
| 290 | * (that driver), but currently in case of IT9135 those are done by |
| 291 | * tuner driver. |
| 292 | */ |
| 293 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 294 | /* load OFSM settings */ |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 295 | dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__); |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 296 | switch (state->cfg.tuner) { |
| 297 | case AF9033_TUNER_IT9135_38: |
| 298 | case AF9033_TUNER_IT9135_51: |
| 299 | case AF9033_TUNER_IT9135_52: |
Antti Palosaari | 463c399 | 2013-03-07 00:03:06 -0300 | [diff] [blame] | 300 | len = ARRAY_SIZE(ofsm_init_it9135_v1); |
| 301 | init = ofsm_init_it9135_v1; |
| 302 | break; |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 303 | case AF9033_TUNER_IT9135_60: |
| 304 | case AF9033_TUNER_IT9135_61: |
| 305 | case AF9033_TUNER_IT9135_62: |
Antti Palosaari | 463c399 | 2013-03-07 00:03:06 -0300 | [diff] [blame] | 306 | len = ARRAY_SIZE(ofsm_init_it9135_v2); |
| 307 | init = ofsm_init_it9135_v2; |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 308 | break; |
| 309 | default: |
| 310 | len = ARRAY_SIZE(ofsm_init); |
| 311 | init = ofsm_init; |
| 312 | break; |
| 313 | } |
| 314 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 315 | for (i = 0; i < len; i++) { |
| 316 | ret = af9033_wr_reg(state, init[i].reg, init[i].val); |
| 317 | if (ret < 0) |
| 318 | goto err; |
| 319 | } |
| 320 | |
| 321 | /* load tuner specific settings */ |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 322 | dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n", |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 323 | __func__); |
| 324 | switch (state->cfg.tuner) { |
| 325 | case AF9033_TUNER_TUA9001: |
| 326 | len = ARRAY_SIZE(tuner_init_tua9001); |
| 327 | init = tuner_init_tua9001; |
| 328 | break; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 329 | case AF9033_TUNER_FC0011: |
| 330 | len = ARRAY_SIZE(tuner_init_fc0011); |
| 331 | init = tuner_init_fc0011; |
| 332 | break; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 333 | case AF9033_TUNER_MXL5007T: |
| 334 | len = ARRAY_SIZE(tuner_init_mxl5007t); |
| 335 | init = tuner_init_mxl5007t; |
| 336 | break; |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 337 | case AF9033_TUNER_TDA18218: |
| 338 | len = ARRAY_SIZE(tuner_init_tda18218); |
| 339 | init = tuner_init_tda18218; |
| 340 | break; |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame] | 341 | case AF9033_TUNER_FC2580: |
| 342 | len = ARRAY_SIZE(tuner_init_fc2580); |
| 343 | init = tuner_init_fc2580; |
| 344 | break; |
Antti Palosaari | e713ad1 | 2012-12-02 18:47:00 -0300 | [diff] [blame] | 345 | case AF9033_TUNER_FC0012: |
| 346 | len = ARRAY_SIZE(tuner_init_fc0012); |
| 347 | init = tuner_init_fc0012; |
| 348 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 349 | case AF9033_TUNER_IT9135_38: |
Antti Palosaari | a72cbb7 | 2013-03-07 21:17:55 -0300 | [diff] [blame] | 350 | len = ARRAY_SIZE(tuner_init_it9135_38); |
| 351 | init = tuner_init_it9135_38; |
| 352 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 353 | case AF9033_TUNER_IT9135_51: |
Antti Palosaari | bb2e12a | 2013-03-07 21:25:16 -0300 | [diff] [blame] | 354 | len = ARRAY_SIZE(tuner_init_it9135_51); |
| 355 | init = tuner_init_it9135_51; |
| 356 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 357 | case AF9033_TUNER_IT9135_52: |
Antti Palosaari | 22d729f | 2013-03-07 21:29:05 -0300 | [diff] [blame] | 358 | len = ARRAY_SIZE(tuner_init_it9135_52); |
| 359 | init = tuner_init_it9135_52; |
| 360 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 361 | case AF9033_TUNER_IT9135_60: |
Antti Palosaari | a49f53a | 2013-03-07 21:38:00 -0300 | [diff] [blame] | 362 | len = ARRAY_SIZE(tuner_init_it9135_60); |
| 363 | init = tuner_init_it9135_60; |
| 364 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 365 | case AF9033_TUNER_IT9135_61: |
Antti Palosaari | 8521132 | 2013-03-07 21:44:30 -0300 | [diff] [blame] | 366 | len = ARRAY_SIZE(tuner_init_it9135_61); |
| 367 | init = tuner_init_it9135_61; |
| 368 | break; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 369 | case AF9033_TUNER_IT9135_62: |
Antti Palosaari | dc4a2c4 | 2013-03-07 21:47:20 -0300 | [diff] [blame^] | 370 | len = ARRAY_SIZE(tuner_init_it9135_62); |
| 371 | init = tuner_init_it9135_62; |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 372 | break; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 373 | default: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 374 | dev_dbg(&state->i2c->dev, "%s: unsupported tuner ID=%d\n", |
| 375 | __func__, state->cfg.tuner); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 376 | ret = -ENODEV; |
| 377 | goto err; |
| 378 | } |
| 379 | |
| 380 | for (i = 0; i < len; i++) { |
| 381 | ret = af9033_wr_reg(state, init[i].reg, init[i].val); |
| 382 | if (ret < 0) |
| 383 | goto err; |
| 384 | } |
| 385 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 386 | if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
| 387 | ret = af9033_wr_reg_mask(state, 0x00d91c, 0x01, 0x01); |
| 388 | if (ret < 0) |
| 389 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 390 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 391 | ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); |
| 392 | if (ret < 0) |
| 393 | goto err; |
Antti Palosaari | bf97b63 | 2012-12-08 22:51:19 -0300 | [diff] [blame] | 394 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame] | 395 | ret = af9033_wr_reg_mask(state, 0x00d916, 0x00, 0x01); |
| 396 | if (ret < 0) |
| 397 | goto err; |
| 398 | } |
| 399 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 400 | state->bandwidth_hz = 0; /* force to program all parameters */ |
| 401 | |
| 402 | return 0; |
| 403 | |
| 404 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 405 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 406 | |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | static int af9033_sleep(struct dvb_frontend *fe) |
| 411 | { |
| 412 | struct af9033_state *state = fe->demodulator_priv; |
| 413 | int ret, i; |
| 414 | u8 tmp; |
| 415 | |
| 416 | ret = af9033_wr_reg(state, 0x80004c, 1); |
| 417 | if (ret < 0) |
| 418 | goto err; |
| 419 | |
| 420 | ret = af9033_wr_reg(state, 0x800000, 0); |
| 421 | if (ret < 0) |
| 422 | goto err; |
| 423 | |
| 424 | for (i = 100, tmp = 1; i && tmp; i--) { |
| 425 | ret = af9033_rd_reg(state, 0x80004c, &tmp); |
| 426 | if (ret < 0) |
| 427 | goto err; |
| 428 | |
| 429 | usleep_range(200, 10000); |
| 430 | } |
| 431 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 432 | dev_dbg(&state->i2c->dev, "%s: loop=%d\n", __func__, i); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 433 | |
| 434 | if (i == 0) { |
| 435 | ret = -ETIMEDOUT; |
| 436 | goto err; |
| 437 | } |
| 438 | |
| 439 | ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08); |
| 440 | if (ret < 0) |
| 441 | goto err; |
| 442 | |
| 443 | /* prevent current leak (?) */ |
| 444 | if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
| 445 | /* enable parallel TS */ |
| 446 | ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); |
| 447 | if (ret < 0) |
| 448 | goto err; |
| 449 | |
| 450 | ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01); |
| 451 | if (ret < 0) |
| 452 | goto err; |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | |
| 457 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 458 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | static int af9033_get_tune_settings(struct dvb_frontend *fe, |
| 464 | struct dvb_frontend_tune_settings *fesettings) |
| 465 | { |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 466 | /* 800 => 2000 because IT9135 v2 is slow to gain lock */ |
| 467 | fesettings->min_delay_ms = 2000; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 468 | fesettings->step_size = 0; |
| 469 | fesettings->max_drift = 0; |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static int af9033_set_frontend(struct dvb_frontend *fe) |
| 475 | { |
| 476 | struct af9033_state *state = fe->demodulator_priv; |
| 477 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 478 | int ret, i, spec_inv, sampling_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 479 | u8 tmp, buf[3], bandwidth_reg_val; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 480 | u32 if_frequency, freq_cw, adc_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 481 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 482 | dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", |
| 483 | __func__, c->frequency, c->bandwidth_hz); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 484 | |
| 485 | /* check bandwidth */ |
| 486 | switch (c->bandwidth_hz) { |
| 487 | case 6000000: |
| 488 | bandwidth_reg_val = 0x00; |
| 489 | break; |
| 490 | case 7000000: |
| 491 | bandwidth_reg_val = 0x01; |
| 492 | break; |
| 493 | case 8000000: |
| 494 | bandwidth_reg_val = 0x02; |
| 495 | break; |
| 496 | default: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 497 | dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n", |
| 498 | __func__); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 499 | ret = -EINVAL; |
| 500 | goto err; |
| 501 | } |
| 502 | |
| 503 | /* program tuner */ |
| 504 | if (fe->ops.tuner_ops.set_params) |
| 505 | fe->ops.tuner_ops.set_params(fe); |
| 506 | |
| 507 | /* program CFOE coefficients */ |
| 508 | if (c->bandwidth_hz != state->bandwidth_hz) { |
| 509 | for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { |
| 510 | if (coeff_lut[i].clock == state->cfg.clock && |
| 511 | coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | ret = af9033_wr_regs(state, 0x800001, |
| 516 | coeff_lut[i].val, sizeof(coeff_lut[i].val)); |
| 517 | } |
| 518 | |
| 519 | /* program frequency control */ |
| 520 | if (c->bandwidth_hz != state->bandwidth_hz) { |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 521 | spec_inv = state->cfg.spec_inv ? -1 : 1; |
| 522 | |
| 523 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
| 524 | if (clock_adc_lut[i].clock == state->cfg.clock) |
| 525 | break; |
| 526 | } |
| 527 | adc_freq = clock_adc_lut[i].adc; |
| 528 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 529 | /* get used IF frequency */ |
| 530 | if (fe->ops.tuner_ops.get_if_frequency) |
| 531 | fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); |
| 532 | else |
| 533 | if_frequency = 0; |
| 534 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 535 | sampling_freq = if_frequency; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 536 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 537 | while (sampling_freq > (adc_freq / 2)) |
| 538 | sampling_freq -= adc_freq; |
| 539 | |
| 540 | if (sampling_freq >= 0) |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 541 | spec_inv *= -1; |
| 542 | else |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 543 | sampling_freq *= -1; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 544 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 545 | freq_cw = af9033_div(state, sampling_freq, adc_freq, 23ul); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 546 | |
| 547 | if (spec_inv == -1) |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 548 | freq_cw = 0x800000 - freq_cw; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 549 | |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 550 | if (state->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X) |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 551 | freq_cw /= 2; |
| 552 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 553 | buf[0] = (freq_cw >> 0) & 0xff; |
| 554 | buf[1] = (freq_cw >> 8) & 0xff; |
| 555 | buf[2] = (freq_cw >> 16) & 0x7f; |
Antti Palosaari | fe8eece | 2013-02-03 13:39:55 -0300 | [diff] [blame] | 556 | |
| 557 | /* FIXME: there seems to be calculation error here... */ |
| 558 | if (if_frequency == 0) |
| 559 | buf[2] = 0; |
| 560 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 561 | ret = af9033_wr_regs(state, 0x800029, buf, 3); |
| 562 | if (ret < 0) |
| 563 | goto err; |
| 564 | |
| 565 | state->bandwidth_hz = c->bandwidth_hz; |
| 566 | } |
| 567 | |
| 568 | ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03); |
| 569 | if (ret < 0) |
| 570 | goto err; |
| 571 | |
| 572 | ret = af9033_wr_reg(state, 0x800040, 0x00); |
| 573 | if (ret < 0) |
| 574 | goto err; |
| 575 | |
| 576 | ret = af9033_wr_reg(state, 0x800047, 0x00); |
| 577 | if (ret < 0) |
| 578 | goto err; |
| 579 | |
| 580 | ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01); |
| 581 | if (ret < 0) |
| 582 | goto err; |
| 583 | |
| 584 | if (c->frequency <= 230000000) |
| 585 | tmp = 0x00; /* VHF */ |
| 586 | else |
| 587 | tmp = 0x01; /* UHF */ |
| 588 | |
| 589 | ret = af9033_wr_reg(state, 0x80004b, tmp); |
| 590 | if (ret < 0) |
| 591 | goto err; |
| 592 | |
| 593 | ret = af9033_wr_reg(state, 0x800000, 0x00); |
| 594 | if (ret < 0) |
| 595 | goto err; |
| 596 | |
| 597 | return 0; |
| 598 | |
| 599 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 600 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 601 | |
| 602 | return ret; |
| 603 | } |
| 604 | |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 605 | static int af9033_get_frontend(struct dvb_frontend *fe) |
| 606 | { |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 607 | struct af9033_state *state = fe->demodulator_priv; |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 608 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 609 | int ret; |
| 610 | u8 buf[8]; |
| 611 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 612 | dev_dbg(&state->i2c->dev, "%s:\n", __func__); |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 613 | |
| 614 | /* read all needed registers */ |
| 615 | ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf)); |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 616 | if (ret < 0) |
| 617 | goto err; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 618 | |
| 619 | switch ((buf[0] >> 0) & 3) { |
| 620 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 621 | c->transmission_mode = TRANSMISSION_MODE_2K; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 622 | break; |
| 623 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 624 | c->transmission_mode = TRANSMISSION_MODE_8K; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 625 | break; |
| 626 | } |
| 627 | |
| 628 | switch ((buf[1] >> 0) & 3) { |
| 629 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 630 | c->guard_interval = GUARD_INTERVAL_1_32; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 631 | break; |
| 632 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 633 | c->guard_interval = GUARD_INTERVAL_1_16; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 634 | break; |
| 635 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 636 | c->guard_interval = GUARD_INTERVAL_1_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 637 | break; |
| 638 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 639 | c->guard_interval = GUARD_INTERVAL_1_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
| 643 | switch ((buf[2] >> 0) & 7) { |
| 644 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 645 | c->hierarchy = HIERARCHY_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 646 | break; |
| 647 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 648 | c->hierarchy = HIERARCHY_1; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 649 | break; |
| 650 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 651 | c->hierarchy = HIERARCHY_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 652 | break; |
| 653 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 654 | c->hierarchy = HIERARCHY_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | |
| 658 | switch ((buf[3] >> 0) & 3) { |
| 659 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 660 | c->modulation = QPSK; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 661 | break; |
| 662 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 663 | c->modulation = QAM_16; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 664 | break; |
| 665 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 666 | c->modulation = QAM_64; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | |
| 670 | switch ((buf[4] >> 0) & 3) { |
| 671 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 672 | c->bandwidth_hz = 6000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 673 | break; |
| 674 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 675 | c->bandwidth_hz = 7000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 676 | break; |
| 677 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 678 | c->bandwidth_hz = 8000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | |
| 682 | switch ((buf[6] >> 0) & 7) { |
| 683 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 684 | c->code_rate_HP = FEC_1_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 685 | break; |
| 686 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 687 | c->code_rate_HP = FEC_2_3; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 688 | break; |
| 689 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 690 | c->code_rate_HP = FEC_3_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 691 | break; |
| 692 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 693 | c->code_rate_HP = FEC_5_6; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 694 | break; |
| 695 | case 4: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 696 | c->code_rate_HP = FEC_7_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 697 | break; |
| 698 | case 5: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 699 | c->code_rate_HP = FEC_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 700 | break; |
| 701 | } |
| 702 | |
| 703 | switch ((buf[7] >> 0) & 7) { |
| 704 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 705 | c->code_rate_LP = FEC_1_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 706 | break; |
| 707 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 708 | c->code_rate_LP = FEC_2_3; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 709 | break; |
| 710 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 711 | c->code_rate_LP = FEC_3_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 712 | break; |
| 713 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 714 | c->code_rate_LP = FEC_5_6; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 715 | break; |
| 716 | case 4: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 717 | c->code_rate_LP = FEC_7_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 718 | break; |
| 719 | case 5: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 720 | c->code_rate_LP = FEC_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 724 | return 0; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 725 | |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 726 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 727 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 728 | |
| 729 | return ret; |
| 730 | } |
| 731 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 732 | static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 733 | { |
| 734 | struct af9033_state *state = fe->demodulator_priv; |
| 735 | int ret; |
| 736 | u8 tmp; |
| 737 | |
| 738 | *status = 0; |
| 739 | |
| 740 | /* radio channel status, 0=no result, 1=has signal, 2=no signal */ |
| 741 | ret = af9033_rd_reg(state, 0x800047, &tmp); |
| 742 | if (ret < 0) |
| 743 | goto err; |
| 744 | |
| 745 | /* has signal */ |
| 746 | if (tmp == 0x01) |
| 747 | *status |= FE_HAS_SIGNAL; |
| 748 | |
| 749 | if (tmp != 0x02) { |
| 750 | /* TPS lock */ |
| 751 | ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01); |
| 752 | if (ret < 0) |
| 753 | goto err; |
| 754 | |
| 755 | if (tmp) |
| 756 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 757 | FE_HAS_VITERBI; |
| 758 | |
| 759 | /* full lock */ |
| 760 | ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01); |
| 761 | if (ret < 0) |
| 762 | goto err; |
| 763 | |
| 764 | if (tmp) |
| 765 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 766 | FE_HAS_VITERBI | FE_HAS_SYNC | |
| 767 | FE_HAS_LOCK; |
| 768 | } |
| 769 | |
| 770 | return 0; |
| 771 | |
| 772 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 773 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 774 | |
| 775 | return ret; |
| 776 | } |
| 777 | |
| 778 | static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 779 | { |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 780 | struct af9033_state *state = fe->demodulator_priv; |
| 781 | int ret, i, len; |
| 782 | u8 buf[3], tmp; |
| 783 | u32 snr_val; |
| 784 | const struct val_snr *uninitialized_var(snr_lut); |
| 785 | |
| 786 | /* read value */ |
| 787 | ret = af9033_rd_regs(state, 0x80002c, buf, 3); |
| 788 | if (ret < 0) |
| 789 | goto err; |
| 790 | |
| 791 | snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 792 | |
| 793 | /* read current modulation */ |
| 794 | ret = af9033_rd_reg(state, 0x80f903, &tmp); |
| 795 | if (ret < 0) |
| 796 | goto err; |
| 797 | |
| 798 | switch ((tmp >> 0) & 3) { |
| 799 | case 0: |
| 800 | len = ARRAY_SIZE(qpsk_snr_lut); |
| 801 | snr_lut = qpsk_snr_lut; |
| 802 | break; |
| 803 | case 1: |
| 804 | len = ARRAY_SIZE(qam16_snr_lut); |
| 805 | snr_lut = qam16_snr_lut; |
| 806 | break; |
| 807 | case 2: |
| 808 | len = ARRAY_SIZE(qam64_snr_lut); |
| 809 | snr_lut = qam64_snr_lut; |
| 810 | break; |
| 811 | default: |
| 812 | goto err; |
| 813 | } |
| 814 | |
| 815 | for (i = 0; i < len; i++) { |
| 816 | tmp = snr_lut[i].snr; |
| 817 | |
| 818 | if (snr_val < snr_lut[i].val) |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | *snr = tmp * 10; /* dB/10 */ |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 823 | |
| 824 | return 0; |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 825 | |
| 826 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 827 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 828 | |
| 829 | return ret; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 833 | { |
| 834 | struct af9033_state *state = fe->demodulator_priv; |
| 835 | int ret; |
| 836 | u8 strength2; |
| 837 | |
| 838 | /* read signal strength of 0-100 scale */ |
| 839 | ret = af9033_rd_reg(state, 0x800048, &strength2); |
| 840 | if (ret < 0) |
| 841 | goto err; |
| 842 | |
| 843 | /* scale value to 0x0000-0xffff */ |
| 844 | *strength = strength2 * 0xffff / 100; |
| 845 | |
| 846 | return 0; |
| 847 | |
| 848 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 849 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 850 | |
| 851 | return ret; |
| 852 | } |
| 853 | |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 854 | static int af9033_update_ch_stat(struct af9033_state *state) |
| 855 | { |
| 856 | int ret = 0; |
| 857 | u32 err_cnt, bit_cnt; |
| 858 | u16 abort_cnt; |
| 859 | u8 buf[7]; |
| 860 | |
| 861 | /* only update data every half second */ |
| 862 | if (time_after(jiffies, state->last_stat_check + msecs_to_jiffies(500))) { |
| 863 | ret = af9033_rd_regs(state, 0x800032, buf, sizeof(buf)); |
| 864 | if (ret < 0) |
| 865 | goto err; |
| 866 | /* in 8 byte packets? */ |
| 867 | abort_cnt = (buf[1] << 8) + buf[0]; |
| 868 | /* in bits */ |
| 869 | err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2]; |
| 870 | /* in 8 byte packets? always(?) 0x2710 = 10000 */ |
| 871 | bit_cnt = (buf[6] << 8) + buf[5]; |
| 872 | |
| 873 | if (bit_cnt < abort_cnt) { |
| 874 | abort_cnt = 1000; |
| 875 | state->ber = 0xffffffff; |
| 876 | } else { |
| 877 | /* 8 byte packets, that have not been rejected already */ |
| 878 | bit_cnt -= (u32)abort_cnt; |
| 879 | if (bit_cnt == 0) { |
| 880 | state->ber = 0xffffffff; |
| 881 | } else { |
| 882 | err_cnt -= (u32)abort_cnt * 8 * 8; |
| 883 | bit_cnt *= 8 * 8; |
| 884 | state->ber = err_cnt * (0xffffffff / bit_cnt); |
| 885 | } |
| 886 | } |
| 887 | state->ucb += abort_cnt; |
| 888 | state->last_stat_check = jiffies; |
| 889 | } |
| 890 | |
| 891 | return 0; |
| 892 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 893 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
| 894 | |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 895 | return ret; |
| 896 | } |
| 897 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 898 | static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 899 | { |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 900 | struct af9033_state *state = fe->demodulator_priv; |
| 901 | int ret; |
| 902 | |
| 903 | ret = af9033_update_ch_stat(state); |
| 904 | if (ret < 0) |
| 905 | return ret; |
| 906 | |
| 907 | *ber = state->ber; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 913 | { |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 914 | struct af9033_state *state = fe->demodulator_priv; |
| 915 | int ret; |
| 916 | |
| 917 | ret = af9033_update_ch_stat(state); |
| 918 | if (ret < 0) |
| 919 | return ret; |
| 920 | |
| 921 | *ucblocks = state->ucb; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
| 927 | { |
| 928 | struct af9033_state *state = fe->demodulator_priv; |
| 929 | int ret; |
| 930 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 931 | dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 932 | |
| 933 | ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01); |
| 934 | if (ret < 0) |
| 935 | goto err; |
| 936 | |
| 937 | return 0; |
| 938 | |
| 939 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 940 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 941 | |
| 942 | return ret; |
| 943 | } |
| 944 | |
| 945 | static struct dvb_frontend_ops af9033_ops; |
| 946 | |
| 947 | struct dvb_frontend *af9033_attach(const struct af9033_config *config, |
| 948 | struct i2c_adapter *i2c) |
| 949 | { |
| 950 | int ret; |
| 951 | struct af9033_state *state; |
| 952 | u8 buf[8]; |
| 953 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 954 | dev_dbg(&i2c->dev, "%s:\n", __func__); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 955 | |
| 956 | /* allocate memory for the internal state */ |
| 957 | state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL); |
| 958 | if (state == NULL) |
| 959 | goto err; |
| 960 | |
| 961 | /* setup the state */ |
| 962 | state->i2c = i2c; |
| 963 | memcpy(&state->cfg, config, sizeof(struct af9033_config)); |
| 964 | |
Antti Palosaari | 8e8a5ac | 2012-04-01 14:13:36 -0300 | [diff] [blame] | 965 | if (state->cfg.clock != 12000000) { |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 966 | dev_err(&state->i2c->dev, "%s: af9033: unsupported clock=%d, " \ |
| 967 | "only 12000000 Hz is supported currently\n", |
| 968 | KBUILD_MODNAME, state->cfg.clock); |
Antti Palosaari | 8e8a5ac | 2012-04-01 14:13:36 -0300 | [diff] [blame] | 969 | goto err; |
| 970 | } |
| 971 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 972 | /* firmware version */ |
| 973 | ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4); |
| 974 | if (ret < 0) |
| 975 | goto err; |
| 976 | |
| 977 | ret = af9033_rd_regs(state, 0x804191, &buf[4], 4); |
| 978 | if (ret < 0) |
| 979 | goto err; |
| 980 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 981 | dev_info(&state->i2c->dev, "%s: firmware version: LINK=%d.%d.%d.%d " \ |
| 982 | "OFDM=%d.%d.%d.%d\n", KBUILD_MODNAME, buf[0], buf[1], |
| 983 | buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 984 | |
Antti Palosaari | 12897dc | 2012-09-16 22:26:57 -0300 | [diff] [blame] | 985 | |
Antti Palosaari | 4902bb3 | 2013-01-07 09:48:03 -0300 | [diff] [blame] | 986 | /* FIXME: Do not abuse adc_multiplier for detecting IT9135 */ |
| 987 | if (state->cfg.adc_multiplier != AF9033_ADC_MULTIPLIER_2X) { |
| 988 | /* sleep */ |
| 989 | ret = af9033_wr_reg(state, 0x80004c, 1); |
| 990 | if (ret < 0) |
| 991 | goto err; |
| 992 | |
| 993 | ret = af9033_wr_reg(state, 0x800000, 0); |
| 994 | if (ret < 0) |
| 995 | goto err; |
| 996 | } |
Antti Palosaari | 12897dc | 2012-09-16 22:26:57 -0300 | [diff] [blame] | 997 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 998 | /* configure internal TS mode */ |
| 999 | switch (state->cfg.ts_mode) { |
| 1000 | case AF9033_TS_MODE_PARALLEL: |
| 1001 | state->ts_mode_parallel = true; |
| 1002 | break; |
| 1003 | case AF9033_TS_MODE_SERIAL: |
| 1004 | state->ts_mode_serial = true; |
| 1005 | break; |
| 1006 | case AF9033_TS_MODE_USB: |
| 1007 | /* usb mode for AF9035 */ |
| 1008 | default: |
| 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | /* create dvb_frontend */ |
| 1013 | memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); |
| 1014 | state->fe.demodulator_priv = state; |
| 1015 | |
| 1016 | return &state->fe; |
| 1017 | |
| 1018 | err: |
| 1019 | kfree(state); |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | EXPORT_SYMBOL(af9033_attach); |
| 1023 | |
| 1024 | static struct dvb_frontend_ops af9033_ops = { |
| 1025 | .delsys = { SYS_DVBT }, |
| 1026 | .info = { |
| 1027 | .name = "Afatech AF9033 (DVB-T)", |
| 1028 | .frequency_min = 174000000, |
| 1029 | .frequency_max = 862000000, |
| 1030 | .frequency_stepsize = 250000, |
| 1031 | .frequency_tolerance = 0, |
| 1032 | .caps = FE_CAN_FEC_1_2 | |
| 1033 | FE_CAN_FEC_2_3 | |
| 1034 | FE_CAN_FEC_3_4 | |
| 1035 | FE_CAN_FEC_5_6 | |
| 1036 | FE_CAN_FEC_7_8 | |
| 1037 | FE_CAN_FEC_AUTO | |
| 1038 | FE_CAN_QPSK | |
| 1039 | FE_CAN_QAM_16 | |
| 1040 | FE_CAN_QAM_64 | |
| 1041 | FE_CAN_QAM_AUTO | |
| 1042 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 1043 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 1044 | FE_CAN_HIERARCHY_AUTO | |
| 1045 | FE_CAN_RECOVER | |
| 1046 | FE_CAN_MUTE_TS |
| 1047 | }, |
| 1048 | |
| 1049 | .release = af9033_release, |
| 1050 | |
| 1051 | .init = af9033_init, |
| 1052 | .sleep = af9033_sleep, |
| 1053 | |
| 1054 | .get_tune_settings = af9033_get_tune_settings, |
| 1055 | .set_frontend = af9033_set_frontend, |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 1056 | .get_frontend = af9033_get_frontend, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1057 | |
| 1058 | .read_status = af9033_read_status, |
| 1059 | .read_snr = af9033_read_snr, |
| 1060 | .read_signal_strength = af9033_read_signal_strength, |
| 1061 | .read_ber = af9033_read_ber, |
| 1062 | .read_ucblocks = af9033_read_ucblocks, |
| 1063 | |
| 1064 | .i2c_gate_ctrl = af9033_i2c_gate_ctrl, |
| 1065 | }; |
| 1066 | |
| 1067 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1068 | MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); |
| 1069 | MODULE_LICENSE("GPL"); |