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 }, |
| 226 | }; |
| 227 | |
| 228 | /* program clock control */ |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 229 | clock_cw = af9033_div(state, state->cfg.clock, 1000000ul, 19ul); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 230 | buf[0] = (clock_cw >> 0) & 0xff; |
| 231 | buf[1] = (clock_cw >> 8) & 0xff; |
| 232 | buf[2] = (clock_cw >> 16) & 0xff; |
| 233 | buf[3] = (clock_cw >> 24) & 0xff; |
| 234 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 235 | dev_dbg(&state->i2c->dev, "%s: clock=%d clock_cw=%08x\n", |
| 236 | __func__, state->cfg.clock, clock_cw); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 237 | |
| 238 | ret = af9033_wr_regs(state, 0x800025, buf, 4); |
| 239 | if (ret < 0) |
| 240 | goto err; |
| 241 | |
| 242 | /* program ADC control */ |
| 243 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
| 244 | if (clock_adc_lut[i].clock == state->cfg.clock) |
| 245 | break; |
| 246 | } |
| 247 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 248 | adc_cw = af9033_div(state, clock_adc_lut[i].adc, 1000000ul, 19ul); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 249 | buf[0] = (adc_cw >> 0) & 0xff; |
| 250 | buf[1] = (adc_cw >> 8) & 0xff; |
| 251 | buf[2] = (adc_cw >> 16) & 0xff; |
| 252 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 253 | dev_dbg(&state->i2c->dev, "%s: adc=%d adc_cw=%06x\n", |
| 254 | __func__, clock_adc_lut[i].adc, adc_cw); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 255 | |
| 256 | ret = af9033_wr_regs(state, 0x80f1cd, buf, 3); |
| 257 | if (ret < 0) |
| 258 | goto err; |
| 259 | |
| 260 | /* program register table */ |
| 261 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 262 | ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val, |
| 263 | tab[i].mask); |
| 264 | if (ret < 0) |
| 265 | goto err; |
| 266 | } |
| 267 | |
| 268 | /* settings for TS interface */ |
| 269 | if (state->cfg.ts_mode == AF9033_TS_MODE_USB) { |
| 270 | ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01); |
| 271 | if (ret < 0) |
| 272 | goto err; |
| 273 | |
| 274 | ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01); |
| 275 | if (ret < 0) |
| 276 | goto err; |
| 277 | } else { |
| 278 | ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01); |
| 279 | if (ret < 0) |
| 280 | goto err; |
| 281 | |
| 282 | ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01); |
| 283 | if (ret < 0) |
| 284 | goto err; |
| 285 | } |
| 286 | |
| 287 | /* load OFSM settings */ |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 288 | dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 289 | len = ARRAY_SIZE(ofsm_init); |
| 290 | init = ofsm_init; |
| 291 | for (i = 0; i < len; i++) { |
| 292 | ret = af9033_wr_reg(state, init[i].reg, init[i].val); |
| 293 | if (ret < 0) |
| 294 | goto err; |
| 295 | } |
| 296 | |
| 297 | /* load tuner specific settings */ |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 298 | dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n", |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 299 | __func__); |
| 300 | switch (state->cfg.tuner) { |
| 301 | case AF9033_TUNER_TUA9001: |
| 302 | len = ARRAY_SIZE(tuner_init_tua9001); |
| 303 | init = tuner_init_tua9001; |
| 304 | break; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 305 | case AF9033_TUNER_FC0011: |
| 306 | len = ARRAY_SIZE(tuner_init_fc0011); |
| 307 | init = tuner_init_fc0011; |
| 308 | break; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 309 | case AF9033_TUNER_MXL5007T: |
| 310 | len = ARRAY_SIZE(tuner_init_mxl5007t); |
| 311 | init = tuner_init_mxl5007t; |
| 312 | break; |
Gianluca Gennari | ce1fe37 | 2012-04-02 17:25:14 -0300 | [diff] [blame] | 313 | case AF9033_TUNER_TDA18218: |
| 314 | len = ARRAY_SIZE(tuner_init_tda18218); |
| 315 | init = tuner_init_tda18218; |
| 316 | break; |
Oliver Schinagl | d67ceb3 | 2012-09-20 14:57:17 -0300 | [diff] [blame] | 317 | case AF9033_TUNER_FC2580: |
| 318 | len = ARRAY_SIZE(tuner_init_fc2580); |
| 319 | init = tuner_init_fc2580; |
| 320 | break; |
Antti Palosaari | e713ad1 | 2012-12-02 18:47:00 -0300 | [diff] [blame] | 321 | case AF9033_TUNER_FC0012: |
| 322 | len = ARRAY_SIZE(tuner_init_fc0012); |
| 323 | init = tuner_init_fc0012; |
| 324 | break; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 325 | default: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 326 | dev_dbg(&state->i2c->dev, "%s: unsupported tuner ID=%d\n", |
| 327 | __func__, state->cfg.tuner); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 328 | ret = -ENODEV; |
| 329 | goto err; |
| 330 | } |
| 331 | |
| 332 | for (i = 0; i < len; i++) { |
| 333 | ret = af9033_wr_reg(state, init[i].reg, init[i].val); |
| 334 | if (ret < 0) |
| 335 | goto err; |
| 336 | } |
| 337 | |
Jose Alberto Reguero | 9805992 | 2012-09-23 16:48:47 -0300 | [diff] [blame^] | 338 | if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
| 339 | ret = af9033_wr_reg_mask(state, 0x00d91c, 0x01, 0x01); |
| 340 | if (ret < 0) |
| 341 | goto err; |
| 342 | ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); |
| 343 | if (ret < 0) |
| 344 | goto err; |
| 345 | ret = af9033_wr_reg_mask(state, 0x00d916, 0x00, 0x01); |
| 346 | if (ret < 0) |
| 347 | goto err; |
| 348 | } |
| 349 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 350 | state->bandwidth_hz = 0; /* force to program all parameters */ |
| 351 | |
| 352 | return 0; |
| 353 | |
| 354 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 355 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | static int af9033_sleep(struct dvb_frontend *fe) |
| 361 | { |
| 362 | struct af9033_state *state = fe->demodulator_priv; |
| 363 | int ret, i; |
| 364 | u8 tmp; |
| 365 | |
| 366 | ret = af9033_wr_reg(state, 0x80004c, 1); |
| 367 | if (ret < 0) |
| 368 | goto err; |
| 369 | |
| 370 | ret = af9033_wr_reg(state, 0x800000, 0); |
| 371 | if (ret < 0) |
| 372 | goto err; |
| 373 | |
| 374 | for (i = 100, tmp = 1; i && tmp; i--) { |
| 375 | ret = af9033_rd_reg(state, 0x80004c, &tmp); |
| 376 | if (ret < 0) |
| 377 | goto err; |
| 378 | |
| 379 | usleep_range(200, 10000); |
| 380 | } |
| 381 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 382 | dev_dbg(&state->i2c->dev, "%s: loop=%d\n", __func__, i); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 383 | |
| 384 | if (i == 0) { |
| 385 | ret = -ETIMEDOUT; |
| 386 | goto err; |
| 387 | } |
| 388 | |
| 389 | ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08); |
| 390 | if (ret < 0) |
| 391 | goto err; |
| 392 | |
| 393 | /* prevent current leak (?) */ |
| 394 | if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
| 395 | /* enable parallel TS */ |
| 396 | ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); |
| 397 | if (ret < 0) |
| 398 | goto err; |
| 399 | |
| 400 | ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01); |
| 401 | if (ret < 0) |
| 402 | goto err; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | |
| 407 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 408 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static int af9033_get_tune_settings(struct dvb_frontend *fe, |
| 414 | struct dvb_frontend_tune_settings *fesettings) |
| 415 | { |
| 416 | fesettings->min_delay_ms = 800; |
| 417 | fesettings->step_size = 0; |
| 418 | fesettings->max_drift = 0; |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int af9033_set_frontend(struct dvb_frontend *fe) |
| 424 | { |
| 425 | struct af9033_state *state = fe->demodulator_priv; |
| 426 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 427 | int ret, i, spec_inv, sampling_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 428 | u8 tmp, buf[3], bandwidth_reg_val; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 429 | u32 if_frequency, freq_cw, adc_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 430 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 431 | dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", |
| 432 | __func__, c->frequency, c->bandwidth_hz); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 433 | |
| 434 | /* check bandwidth */ |
| 435 | switch (c->bandwidth_hz) { |
| 436 | case 6000000: |
| 437 | bandwidth_reg_val = 0x00; |
| 438 | break; |
| 439 | case 7000000: |
| 440 | bandwidth_reg_val = 0x01; |
| 441 | break; |
| 442 | case 8000000: |
| 443 | bandwidth_reg_val = 0x02; |
| 444 | break; |
| 445 | default: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 446 | dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n", |
| 447 | __func__); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 448 | ret = -EINVAL; |
| 449 | goto err; |
| 450 | } |
| 451 | |
| 452 | /* program tuner */ |
| 453 | if (fe->ops.tuner_ops.set_params) |
| 454 | fe->ops.tuner_ops.set_params(fe); |
| 455 | |
| 456 | /* program CFOE coefficients */ |
| 457 | if (c->bandwidth_hz != state->bandwidth_hz) { |
| 458 | for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { |
| 459 | if (coeff_lut[i].clock == state->cfg.clock && |
| 460 | coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | ret = af9033_wr_regs(state, 0x800001, |
| 465 | coeff_lut[i].val, sizeof(coeff_lut[i].val)); |
| 466 | } |
| 467 | |
| 468 | /* program frequency control */ |
| 469 | if (c->bandwidth_hz != state->bandwidth_hz) { |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 470 | spec_inv = state->cfg.spec_inv ? -1 : 1; |
| 471 | |
| 472 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
| 473 | if (clock_adc_lut[i].clock == state->cfg.clock) |
| 474 | break; |
| 475 | } |
| 476 | adc_freq = clock_adc_lut[i].adc; |
| 477 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 478 | /* get used IF frequency */ |
| 479 | if (fe->ops.tuner_ops.get_if_frequency) |
| 480 | fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); |
| 481 | else |
| 482 | if_frequency = 0; |
| 483 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 484 | sampling_freq = if_frequency; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 485 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 486 | while (sampling_freq > (adc_freq / 2)) |
| 487 | sampling_freq -= adc_freq; |
| 488 | |
| 489 | if (sampling_freq >= 0) |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 490 | spec_inv *= -1; |
| 491 | else |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 492 | sampling_freq *= -1; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 493 | |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 494 | freq_cw = af9033_div(state, sampling_freq, adc_freq, 23ul); |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 495 | |
| 496 | if (spec_inv == -1) |
Hans-Frieder Vogt | 182b967 | 2012-10-03 05:25:40 -0300 | [diff] [blame] | 497 | freq_cw = 0x800000 - freq_cw; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 498 | |
| 499 | /* get adc multiplies */ |
| 500 | ret = af9033_rd_reg(state, 0x800045, &tmp); |
| 501 | if (ret < 0) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 502 | goto err; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 503 | |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame] | 504 | if (tmp == 1) |
| 505 | freq_cw /= 2; |
| 506 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 507 | buf[0] = (freq_cw >> 0) & 0xff; |
| 508 | buf[1] = (freq_cw >> 8) & 0xff; |
| 509 | buf[2] = (freq_cw >> 16) & 0x7f; |
| 510 | ret = af9033_wr_regs(state, 0x800029, buf, 3); |
| 511 | if (ret < 0) |
| 512 | goto err; |
| 513 | |
| 514 | state->bandwidth_hz = c->bandwidth_hz; |
| 515 | } |
| 516 | |
| 517 | ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03); |
| 518 | if (ret < 0) |
| 519 | goto err; |
| 520 | |
| 521 | ret = af9033_wr_reg(state, 0x800040, 0x00); |
| 522 | if (ret < 0) |
| 523 | goto err; |
| 524 | |
| 525 | ret = af9033_wr_reg(state, 0x800047, 0x00); |
| 526 | if (ret < 0) |
| 527 | goto err; |
| 528 | |
| 529 | ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01); |
| 530 | if (ret < 0) |
| 531 | goto err; |
| 532 | |
| 533 | if (c->frequency <= 230000000) |
| 534 | tmp = 0x00; /* VHF */ |
| 535 | else |
| 536 | tmp = 0x01; /* UHF */ |
| 537 | |
| 538 | ret = af9033_wr_reg(state, 0x80004b, tmp); |
| 539 | if (ret < 0) |
| 540 | goto err; |
| 541 | |
| 542 | ret = af9033_wr_reg(state, 0x800000, 0x00); |
| 543 | if (ret < 0) |
| 544 | goto err; |
| 545 | |
| 546 | return 0; |
| 547 | |
| 548 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 549 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 550 | |
| 551 | return ret; |
| 552 | } |
| 553 | |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 554 | static int af9033_get_frontend(struct dvb_frontend *fe) |
| 555 | { |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 556 | struct af9033_state *state = fe->demodulator_priv; |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 557 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 558 | int ret; |
| 559 | u8 buf[8]; |
| 560 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 561 | dev_dbg(&state->i2c->dev, "%s:\n", __func__); |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 562 | |
| 563 | /* read all needed registers */ |
| 564 | ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf)); |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 565 | if (ret < 0) |
| 566 | goto err; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 567 | |
| 568 | switch ((buf[0] >> 0) & 3) { |
| 569 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 570 | c->transmission_mode = TRANSMISSION_MODE_2K; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 571 | break; |
| 572 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 573 | c->transmission_mode = TRANSMISSION_MODE_8K; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | |
| 577 | switch ((buf[1] >> 0) & 3) { |
| 578 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 579 | c->guard_interval = GUARD_INTERVAL_1_32; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 580 | break; |
| 581 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 582 | c->guard_interval = GUARD_INTERVAL_1_16; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 583 | break; |
| 584 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 585 | c->guard_interval = GUARD_INTERVAL_1_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 586 | break; |
| 587 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 588 | c->guard_interval = GUARD_INTERVAL_1_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | |
| 592 | switch ((buf[2] >> 0) & 7) { |
| 593 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 594 | c->hierarchy = HIERARCHY_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 595 | break; |
| 596 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 597 | c->hierarchy = HIERARCHY_1; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 598 | break; |
| 599 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 600 | c->hierarchy = HIERARCHY_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 601 | break; |
| 602 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 603 | c->hierarchy = HIERARCHY_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 604 | break; |
| 605 | } |
| 606 | |
| 607 | switch ((buf[3] >> 0) & 3) { |
| 608 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 609 | c->modulation = QPSK; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 610 | break; |
| 611 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 612 | c->modulation = QAM_16; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 613 | break; |
| 614 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 615 | c->modulation = QAM_64; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | |
| 619 | switch ((buf[4] >> 0) & 3) { |
| 620 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 621 | c->bandwidth_hz = 6000000; |
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->bandwidth_hz = 7000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 625 | break; |
| 626 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 627 | c->bandwidth_hz = 8000000; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | |
| 631 | switch ((buf[6] >> 0) & 7) { |
| 632 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 633 | c->code_rate_HP = FEC_1_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 634 | break; |
| 635 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 636 | c->code_rate_HP = FEC_2_3; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 637 | break; |
| 638 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 639 | c->code_rate_HP = FEC_3_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 640 | break; |
| 641 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 642 | c->code_rate_HP = FEC_5_6; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 643 | break; |
| 644 | case 4: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 645 | c->code_rate_HP = FEC_7_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 646 | break; |
| 647 | case 5: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 648 | c->code_rate_HP = FEC_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | |
| 652 | switch ((buf[7] >> 0) & 7) { |
| 653 | case 0: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 654 | c->code_rate_LP = FEC_1_2; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 655 | break; |
| 656 | case 1: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 657 | c->code_rate_LP = FEC_2_3; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 658 | break; |
| 659 | case 2: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 660 | c->code_rate_LP = FEC_3_4; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 661 | break; |
| 662 | case 3: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 663 | c->code_rate_LP = FEC_5_6; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 664 | break; |
| 665 | case 4: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 666 | c->code_rate_LP = FEC_7_8; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 667 | break; |
| 668 | case 5: |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 669 | c->code_rate_LP = FEC_NONE; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 670 | break; |
| 671 | } |
| 672 | |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 673 | return 0; |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 674 | |
Antti Palosaari | de7f14fc | 2012-04-05 21:14:32 -0300 | [diff] [blame] | 675 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 676 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 677 | |
| 678 | return ret; |
| 679 | } |
| 680 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 681 | static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 682 | { |
| 683 | struct af9033_state *state = fe->demodulator_priv; |
| 684 | int ret; |
| 685 | u8 tmp; |
| 686 | |
| 687 | *status = 0; |
| 688 | |
| 689 | /* radio channel status, 0=no result, 1=has signal, 2=no signal */ |
| 690 | ret = af9033_rd_reg(state, 0x800047, &tmp); |
| 691 | if (ret < 0) |
| 692 | goto err; |
| 693 | |
| 694 | /* has signal */ |
| 695 | if (tmp == 0x01) |
| 696 | *status |= FE_HAS_SIGNAL; |
| 697 | |
| 698 | if (tmp != 0x02) { |
| 699 | /* TPS lock */ |
| 700 | ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01); |
| 701 | if (ret < 0) |
| 702 | goto err; |
| 703 | |
| 704 | if (tmp) |
| 705 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 706 | FE_HAS_VITERBI; |
| 707 | |
| 708 | /* full lock */ |
| 709 | ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01); |
| 710 | if (ret < 0) |
| 711 | goto err; |
| 712 | |
| 713 | if (tmp) |
| 714 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 715 | FE_HAS_VITERBI | FE_HAS_SYNC | |
| 716 | FE_HAS_LOCK; |
| 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | |
| 721 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 722 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 723 | |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 728 | { |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 729 | struct af9033_state *state = fe->demodulator_priv; |
| 730 | int ret, i, len; |
| 731 | u8 buf[3], tmp; |
| 732 | u32 snr_val; |
| 733 | const struct val_snr *uninitialized_var(snr_lut); |
| 734 | |
| 735 | /* read value */ |
| 736 | ret = af9033_rd_regs(state, 0x80002c, buf, 3); |
| 737 | if (ret < 0) |
| 738 | goto err; |
| 739 | |
| 740 | snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 741 | |
| 742 | /* read current modulation */ |
| 743 | ret = af9033_rd_reg(state, 0x80f903, &tmp); |
| 744 | if (ret < 0) |
| 745 | goto err; |
| 746 | |
| 747 | switch ((tmp >> 0) & 3) { |
| 748 | case 0: |
| 749 | len = ARRAY_SIZE(qpsk_snr_lut); |
| 750 | snr_lut = qpsk_snr_lut; |
| 751 | break; |
| 752 | case 1: |
| 753 | len = ARRAY_SIZE(qam16_snr_lut); |
| 754 | snr_lut = qam16_snr_lut; |
| 755 | break; |
| 756 | case 2: |
| 757 | len = ARRAY_SIZE(qam64_snr_lut); |
| 758 | snr_lut = qam64_snr_lut; |
| 759 | break; |
| 760 | default: |
| 761 | goto err; |
| 762 | } |
| 763 | |
| 764 | for (i = 0; i < len; i++) { |
| 765 | tmp = snr_lut[i].snr; |
| 766 | |
| 767 | if (snr_val < snr_lut[i].val) |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | *snr = tmp * 10; /* dB/10 */ |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 772 | |
| 773 | return 0; |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 774 | |
| 775 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 776 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 777 | |
| 778 | return ret; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 782 | { |
| 783 | struct af9033_state *state = fe->demodulator_priv; |
| 784 | int ret; |
| 785 | u8 strength2; |
| 786 | |
| 787 | /* read signal strength of 0-100 scale */ |
| 788 | ret = af9033_rd_reg(state, 0x800048, &strength2); |
| 789 | if (ret < 0) |
| 790 | goto err; |
| 791 | |
| 792 | /* scale value to 0x0000-0xffff */ |
| 793 | *strength = strength2 * 0xffff / 100; |
| 794 | |
| 795 | return 0; |
| 796 | |
| 797 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 798 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 799 | |
| 800 | return ret; |
| 801 | } |
| 802 | |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 803 | static int af9033_update_ch_stat(struct af9033_state *state) |
| 804 | { |
| 805 | int ret = 0; |
| 806 | u32 err_cnt, bit_cnt; |
| 807 | u16 abort_cnt; |
| 808 | u8 buf[7]; |
| 809 | |
| 810 | /* only update data every half second */ |
| 811 | if (time_after(jiffies, state->last_stat_check + msecs_to_jiffies(500))) { |
| 812 | ret = af9033_rd_regs(state, 0x800032, buf, sizeof(buf)); |
| 813 | if (ret < 0) |
| 814 | goto err; |
| 815 | /* in 8 byte packets? */ |
| 816 | abort_cnt = (buf[1] << 8) + buf[0]; |
| 817 | /* in bits */ |
| 818 | err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2]; |
| 819 | /* in 8 byte packets? always(?) 0x2710 = 10000 */ |
| 820 | bit_cnt = (buf[6] << 8) + buf[5]; |
| 821 | |
| 822 | if (bit_cnt < abort_cnt) { |
| 823 | abort_cnt = 1000; |
| 824 | state->ber = 0xffffffff; |
| 825 | } else { |
| 826 | /* 8 byte packets, that have not been rejected already */ |
| 827 | bit_cnt -= (u32)abort_cnt; |
| 828 | if (bit_cnt == 0) { |
| 829 | state->ber = 0xffffffff; |
| 830 | } else { |
| 831 | err_cnt -= (u32)abort_cnt * 8 * 8; |
| 832 | bit_cnt *= 8 * 8; |
| 833 | state->ber = err_cnt * (0xffffffff / bit_cnt); |
| 834 | } |
| 835 | } |
| 836 | state->ucb += abort_cnt; |
| 837 | state->last_stat_check = jiffies; |
| 838 | } |
| 839 | |
| 840 | return 0; |
| 841 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 842 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
| 843 | |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 844 | return ret; |
| 845 | } |
| 846 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 847 | static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 848 | { |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 849 | struct af9033_state *state = fe->demodulator_priv; |
| 850 | int ret; |
| 851 | |
| 852 | ret = af9033_update_ch_stat(state); |
| 853 | if (ret < 0) |
| 854 | return ret; |
| 855 | |
| 856 | *ber = state->ber; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 862 | { |
Hans-Frieder Vogt | 47eafa5 | 2012-04-07 10:34:34 -0300 | [diff] [blame] | 863 | struct af9033_state *state = fe->demodulator_priv; |
| 864 | int ret; |
| 865 | |
| 866 | ret = af9033_update_ch_stat(state); |
| 867 | if (ret < 0) |
| 868 | return ret; |
| 869 | |
| 870 | *ucblocks = state->ucb; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
| 876 | { |
| 877 | struct af9033_state *state = fe->demodulator_priv; |
| 878 | int ret; |
| 879 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 880 | dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 881 | |
| 882 | ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01); |
| 883 | if (ret < 0) |
| 884 | goto err; |
| 885 | |
| 886 | return 0; |
| 887 | |
| 888 | err: |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 889 | dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 890 | |
| 891 | return ret; |
| 892 | } |
| 893 | |
| 894 | static struct dvb_frontend_ops af9033_ops; |
| 895 | |
| 896 | struct dvb_frontend *af9033_attach(const struct af9033_config *config, |
| 897 | struct i2c_adapter *i2c) |
| 898 | { |
| 899 | int ret; |
| 900 | struct af9033_state *state; |
| 901 | u8 buf[8]; |
| 902 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 903 | dev_dbg(&i2c->dev, "%s:\n", __func__); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 904 | |
| 905 | /* allocate memory for the internal state */ |
| 906 | state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL); |
| 907 | if (state == NULL) |
| 908 | goto err; |
| 909 | |
| 910 | /* setup the state */ |
| 911 | state->i2c = i2c; |
| 912 | memcpy(&state->cfg, config, sizeof(struct af9033_config)); |
| 913 | |
Antti Palosaari | 8e8a5ac | 2012-04-01 14:13:36 -0300 | [diff] [blame] | 914 | if (state->cfg.clock != 12000000) { |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 915 | dev_err(&state->i2c->dev, "%s: af9033: unsupported clock=%d, " \ |
| 916 | "only 12000000 Hz is supported currently\n", |
| 917 | KBUILD_MODNAME, state->cfg.clock); |
Antti Palosaari | 8e8a5ac | 2012-04-01 14:13:36 -0300 | [diff] [blame] | 918 | goto err; |
| 919 | } |
| 920 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 921 | /* firmware version */ |
| 922 | ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4); |
| 923 | if (ret < 0) |
| 924 | goto err; |
| 925 | |
| 926 | ret = af9033_rd_regs(state, 0x804191, &buf[4], 4); |
| 927 | if (ret < 0) |
| 928 | goto err; |
| 929 | |
Antti Palosaari | 0a73f2d | 2012-09-12 20:23:42 -0300 | [diff] [blame] | 930 | dev_info(&state->i2c->dev, "%s: firmware version: LINK=%d.%d.%d.%d " \ |
| 931 | "OFDM=%d.%d.%d.%d\n", KBUILD_MODNAME, buf[0], buf[1], |
| 932 | buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 933 | |
Antti Palosaari | 12897dc | 2012-09-16 22:26:57 -0300 | [diff] [blame] | 934 | /* sleep */ |
| 935 | ret = af9033_wr_reg(state, 0x80004c, 1); |
| 936 | if (ret < 0) |
| 937 | goto err; |
| 938 | |
| 939 | ret = af9033_wr_reg(state, 0x800000, 0); |
| 940 | if (ret < 0) |
| 941 | goto err; |
| 942 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 943 | /* configure internal TS mode */ |
| 944 | switch (state->cfg.ts_mode) { |
| 945 | case AF9033_TS_MODE_PARALLEL: |
| 946 | state->ts_mode_parallel = true; |
| 947 | break; |
| 948 | case AF9033_TS_MODE_SERIAL: |
| 949 | state->ts_mode_serial = true; |
| 950 | break; |
| 951 | case AF9033_TS_MODE_USB: |
| 952 | /* usb mode for AF9035 */ |
| 953 | default: |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | /* create dvb_frontend */ |
| 958 | memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); |
| 959 | state->fe.demodulator_priv = state; |
| 960 | |
| 961 | return &state->fe; |
| 962 | |
| 963 | err: |
| 964 | kfree(state); |
| 965 | return NULL; |
| 966 | } |
| 967 | EXPORT_SYMBOL(af9033_attach); |
| 968 | |
| 969 | static struct dvb_frontend_ops af9033_ops = { |
| 970 | .delsys = { SYS_DVBT }, |
| 971 | .info = { |
| 972 | .name = "Afatech AF9033 (DVB-T)", |
| 973 | .frequency_min = 174000000, |
| 974 | .frequency_max = 862000000, |
| 975 | .frequency_stepsize = 250000, |
| 976 | .frequency_tolerance = 0, |
| 977 | .caps = FE_CAN_FEC_1_2 | |
| 978 | FE_CAN_FEC_2_3 | |
| 979 | FE_CAN_FEC_3_4 | |
| 980 | FE_CAN_FEC_5_6 | |
| 981 | FE_CAN_FEC_7_8 | |
| 982 | FE_CAN_FEC_AUTO | |
| 983 | FE_CAN_QPSK | |
| 984 | FE_CAN_QAM_16 | |
| 985 | FE_CAN_QAM_64 | |
| 986 | FE_CAN_QAM_AUTO | |
| 987 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 988 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 989 | FE_CAN_HIERARCHY_AUTO | |
| 990 | FE_CAN_RECOVER | |
| 991 | FE_CAN_MUTE_TS |
| 992 | }, |
| 993 | |
| 994 | .release = af9033_release, |
| 995 | |
| 996 | .init = af9033_init, |
| 997 | .sleep = af9033_sleep, |
| 998 | |
| 999 | .get_tune_settings = af9033_get_tune_settings, |
| 1000 | .set_frontend = af9033_set_frontend, |
Gianluca Gennari | 0a4df23 | 2012-04-05 12:47:19 -0300 | [diff] [blame] | 1001 | .get_frontend = af9033_get_frontend, |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 1002 | |
| 1003 | .read_status = af9033_read_status, |
| 1004 | .read_snr = af9033_read_snr, |
| 1005 | .read_signal_strength = af9033_read_signal_strength, |
| 1006 | .read_ber = af9033_read_ber, |
| 1007 | .read_ucblocks = af9033_read_ucblocks, |
| 1008 | |
| 1009 | .i2c_gate_ctrl = af9033_i2c_gate_ctrl, |
| 1010 | }; |
| 1011 | |
| 1012 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1013 | MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); |
| 1014 | MODULE_LICENSE("GPL"); |