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; |
| 32 | }; |
| 33 | |
| 34 | /* write multiple registers */ |
| 35 | static int af9033_wr_regs(struct af9033_state *state, u32 reg, const u8 *val, |
| 36 | int len) |
| 37 | { |
| 38 | int ret; |
| 39 | u8 buf[3 + len]; |
| 40 | struct i2c_msg msg[1] = { |
| 41 | { |
| 42 | .addr = state->cfg.i2c_addr, |
| 43 | .flags = 0, |
| 44 | .len = sizeof(buf), |
| 45 | .buf = buf, |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | buf[0] = (reg >> 16) & 0xff; |
| 50 | buf[1] = (reg >> 8) & 0xff; |
| 51 | buf[2] = (reg >> 0) & 0xff; |
| 52 | memcpy(&buf[3], val, len); |
| 53 | |
| 54 | ret = i2c_transfer(state->i2c, msg, 1); |
| 55 | if (ret == 1) { |
| 56 | ret = 0; |
| 57 | } else { |
| 58 | printk(KERN_WARNING "%s: i2c wr failed=%d reg=%06x len=%d\n", |
| 59 | __func__, ret, reg, len); |
| 60 | ret = -EREMOTEIO; |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | /* read multiple registers */ |
| 67 | static int af9033_rd_regs(struct af9033_state *state, u32 reg, u8 *val, int len) |
| 68 | { |
| 69 | int ret; |
| 70 | u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff, |
| 71 | (reg >> 0) & 0xff }; |
| 72 | struct i2c_msg msg[2] = { |
| 73 | { |
| 74 | .addr = state->cfg.i2c_addr, |
| 75 | .flags = 0, |
| 76 | .len = sizeof(buf), |
| 77 | .buf = buf |
| 78 | }, { |
| 79 | .addr = state->cfg.i2c_addr, |
| 80 | .flags = I2C_M_RD, |
| 81 | .len = len, |
| 82 | .buf = val |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | ret = i2c_transfer(state->i2c, msg, 2); |
| 87 | if (ret == 2) { |
| 88 | ret = 0; |
| 89 | } else { |
| 90 | printk(KERN_WARNING "%s: i2c rd failed=%d reg=%06x len=%d\n", |
| 91 | __func__, ret, reg, len); |
| 92 | ret = -EREMOTEIO; |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* write single register */ |
| 100 | static int af9033_wr_reg(struct af9033_state *state, u32 reg, u8 val) |
| 101 | { |
| 102 | return af9033_wr_regs(state, reg, &val, 1); |
| 103 | } |
| 104 | |
| 105 | /* read single register */ |
| 106 | static int af9033_rd_reg(struct af9033_state *state, u32 reg, u8 *val) |
| 107 | { |
| 108 | return af9033_rd_regs(state, reg, val, 1); |
| 109 | } |
| 110 | |
| 111 | /* write single register with mask */ |
| 112 | static int af9033_wr_reg_mask(struct af9033_state *state, u32 reg, u8 val, |
| 113 | u8 mask) |
| 114 | { |
| 115 | int ret; |
| 116 | u8 tmp; |
| 117 | |
| 118 | /* no need for read if whole reg is written */ |
| 119 | if (mask != 0xff) { |
| 120 | ret = af9033_rd_regs(state, reg, &tmp, 1); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
| 124 | val &= mask; |
| 125 | tmp &= ~mask; |
| 126 | val |= tmp; |
| 127 | } |
| 128 | |
| 129 | return af9033_wr_regs(state, reg, &val, 1); |
| 130 | } |
| 131 | |
| 132 | /* read single register with mask */ |
| 133 | static int af9033_rd_reg_mask(struct af9033_state *state, u32 reg, u8 *val, |
| 134 | u8 mask) |
| 135 | { |
| 136 | int ret, i; |
| 137 | u8 tmp; |
| 138 | |
| 139 | ret = af9033_rd_regs(state, reg, &tmp, 1); |
| 140 | if (ret) |
| 141 | return ret; |
| 142 | |
| 143 | tmp &= mask; |
| 144 | |
| 145 | /* find position of the first bit */ |
| 146 | for (i = 0; i < 8; i++) { |
| 147 | if ((mask >> i) & 0x01) |
| 148 | break; |
| 149 | } |
| 150 | *val = tmp >> i; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static u32 af9033_div(u32 a, u32 b, u32 x) |
| 156 | { |
| 157 | u32 r = 0, c = 0, i; |
| 158 | |
| 159 | pr_debug("%s: a=%d b=%d x=%d\n", __func__, a, b, x); |
| 160 | |
| 161 | if (a > b) { |
| 162 | c = a / b; |
| 163 | a = a - c * b; |
| 164 | } |
| 165 | |
| 166 | for (i = 0; i < x; i++) { |
| 167 | if (a >= b) { |
| 168 | r += 1; |
| 169 | a -= b; |
| 170 | } |
| 171 | a <<= 1; |
| 172 | r <<= 1; |
| 173 | } |
| 174 | r = (c << (u32)x) + r; |
| 175 | |
| 176 | pr_debug("%s: a=%d b=%d x=%d r=%d r=%x\n", __func__, a, b, x, r, r); |
| 177 | |
| 178 | return r; |
| 179 | } |
| 180 | |
| 181 | static void af9033_release(struct dvb_frontend *fe) |
| 182 | { |
| 183 | struct af9033_state *state = fe->demodulator_priv; |
| 184 | |
| 185 | kfree(state); |
| 186 | } |
| 187 | |
| 188 | static int af9033_init(struct dvb_frontend *fe) |
| 189 | { |
| 190 | struct af9033_state *state = fe->demodulator_priv; |
| 191 | int ret, i, len; |
| 192 | const struct reg_val *init; |
| 193 | u8 buf[4]; |
| 194 | u32 adc_cw, clock_cw; |
| 195 | struct reg_val_mask tab[] = { |
| 196 | { 0x80fb24, 0x00, 0x08 }, |
| 197 | { 0x80004c, 0x00, 0xff }, |
| 198 | { 0x00f641, state->cfg.tuner, 0xff }, |
| 199 | { 0x80f5ca, 0x01, 0x01 }, |
| 200 | { 0x80f715, 0x01, 0x01 }, |
| 201 | { 0x00f41f, 0x04, 0x04 }, |
| 202 | { 0x00f41a, 0x01, 0x01 }, |
| 203 | { 0x80f731, 0x00, 0x01 }, |
| 204 | { 0x00d91e, 0x00, 0x01 }, |
| 205 | { 0x00d919, 0x00, 0x01 }, |
| 206 | { 0x80f732, 0x00, 0x01 }, |
| 207 | { 0x00d91f, 0x00, 0x01 }, |
| 208 | { 0x00d91a, 0x00, 0x01 }, |
| 209 | { 0x80f730, 0x00, 0x01 }, |
| 210 | { 0x80f778, 0x00, 0xff }, |
| 211 | { 0x80f73c, 0x01, 0x01 }, |
| 212 | { 0x80f776, 0x00, 0x01 }, |
| 213 | { 0x00d8fd, 0x01, 0xff }, |
| 214 | { 0x00d830, 0x01, 0xff }, |
| 215 | { 0x00d831, 0x00, 0xff }, |
| 216 | { 0x00d832, 0x00, 0xff }, |
| 217 | { 0x80f985, state->ts_mode_serial, 0x01 }, |
| 218 | { 0x80f986, state->ts_mode_parallel, 0x01 }, |
| 219 | { 0x00d827, 0x00, 0xff }, |
| 220 | { 0x00d829, 0x00, 0xff }, |
| 221 | }; |
| 222 | |
| 223 | /* program clock control */ |
| 224 | clock_cw = af9033_div(state->cfg.clock, 1000000ul, 19ul); |
| 225 | buf[0] = (clock_cw >> 0) & 0xff; |
| 226 | buf[1] = (clock_cw >> 8) & 0xff; |
| 227 | buf[2] = (clock_cw >> 16) & 0xff; |
| 228 | buf[3] = (clock_cw >> 24) & 0xff; |
| 229 | |
| 230 | pr_debug("%s: clock=%d clock_cw=%08x\n", __func__, state->cfg.clock, |
| 231 | clock_cw); |
| 232 | |
| 233 | ret = af9033_wr_regs(state, 0x800025, buf, 4); |
| 234 | if (ret < 0) |
| 235 | goto err; |
| 236 | |
| 237 | /* program ADC control */ |
| 238 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
| 239 | if (clock_adc_lut[i].clock == state->cfg.clock) |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | adc_cw = af9033_div(clock_adc_lut[i].adc, 1000000ul, 19ul); |
| 244 | buf[0] = (adc_cw >> 0) & 0xff; |
| 245 | buf[1] = (adc_cw >> 8) & 0xff; |
| 246 | buf[2] = (adc_cw >> 16) & 0xff; |
| 247 | |
| 248 | pr_debug("%s: adc=%d adc_cw=%06x\n", __func__, clock_adc_lut[i].adc, |
| 249 | adc_cw); |
| 250 | |
| 251 | ret = af9033_wr_regs(state, 0x80f1cd, buf, 3); |
| 252 | if (ret < 0) |
| 253 | goto err; |
| 254 | |
| 255 | /* program register table */ |
| 256 | for (i = 0; i < ARRAY_SIZE(tab); i++) { |
| 257 | ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val, |
| 258 | tab[i].mask); |
| 259 | if (ret < 0) |
| 260 | goto err; |
| 261 | } |
| 262 | |
| 263 | /* settings for TS interface */ |
| 264 | if (state->cfg.ts_mode == AF9033_TS_MODE_USB) { |
| 265 | ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01); |
| 266 | if (ret < 0) |
| 267 | goto err; |
| 268 | |
| 269 | ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01); |
| 270 | if (ret < 0) |
| 271 | goto err; |
| 272 | } else { |
| 273 | ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01); |
| 274 | if (ret < 0) |
| 275 | goto err; |
| 276 | |
| 277 | ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01); |
| 278 | if (ret < 0) |
| 279 | goto err; |
| 280 | } |
| 281 | |
| 282 | /* load OFSM settings */ |
| 283 | pr_debug("%s: load ofsm settings\n", __func__); |
| 284 | len = ARRAY_SIZE(ofsm_init); |
| 285 | init = ofsm_init; |
| 286 | for (i = 0; i < len; i++) { |
| 287 | ret = af9033_wr_reg(state, init[i].reg, init[i].val); |
| 288 | if (ret < 0) |
| 289 | goto err; |
| 290 | } |
| 291 | |
| 292 | /* load tuner specific settings */ |
| 293 | pr_debug("%s: load tuner specific settings\n", |
| 294 | __func__); |
| 295 | switch (state->cfg.tuner) { |
| 296 | case AF9033_TUNER_TUA9001: |
| 297 | len = ARRAY_SIZE(tuner_init_tua9001); |
| 298 | init = tuner_init_tua9001; |
| 299 | break; |
| 300 | default: |
| 301 | pr_debug("%s: unsupported tuner ID=%d\n", __func__, |
| 302 | state->cfg.tuner); |
| 303 | ret = -ENODEV; |
| 304 | goto err; |
| 305 | } |
| 306 | |
| 307 | for (i = 0; i < len; i++) { |
| 308 | ret = af9033_wr_reg(state, init[i].reg, init[i].val); |
| 309 | if (ret < 0) |
| 310 | goto err; |
| 311 | } |
| 312 | |
| 313 | state->bandwidth_hz = 0; /* force to program all parameters */ |
| 314 | |
| 315 | return 0; |
| 316 | |
| 317 | err: |
| 318 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 319 | |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | static int af9033_sleep(struct dvb_frontend *fe) |
| 324 | { |
| 325 | struct af9033_state *state = fe->demodulator_priv; |
| 326 | int ret, i; |
| 327 | u8 tmp; |
| 328 | |
| 329 | ret = af9033_wr_reg(state, 0x80004c, 1); |
| 330 | if (ret < 0) |
| 331 | goto err; |
| 332 | |
| 333 | ret = af9033_wr_reg(state, 0x800000, 0); |
| 334 | if (ret < 0) |
| 335 | goto err; |
| 336 | |
| 337 | for (i = 100, tmp = 1; i && tmp; i--) { |
| 338 | ret = af9033_rd_reg(state, 0x80004c, &tmp); |
| 339 | if (ret < 0) |
| 340 | goto err; |
| 341 | |
| 342 | usleep_range(200, 10000); |
| 343 | } |
| 344 | |
Antti Palosaari | 3a871ca | 2012-04-01 11:14:59 -0300 | [diff] [blame] | 345 | pr_debug("%s: loop=%d\n", __func__, i); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 346 | |
| 347 | if (i == 0) { |
| 348 | ret = -ETIMEDOUT; |
| 349 | goto err; |
| 350 | } |
| 351 | |
| 352 | ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08); |
| 353 | if (ret < 0) |
| 354 | goto err; |
| 355 | |
| 356 | /* prevent current leak (?) */ |
| 357 | if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
| 358 | /* enable parallel TS */ |
| 359 | ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); |
| 360 | if (ret < 0) |
| 361 | goto err; |
| 362 | |
| 363 | ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01); |
| 364 | if (ret < 0) |
| 365 | goto err; |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | |
| 370 | err: |
| 371 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static int af9033_get_tune_settings(struct dvb_frontend *fe, |
| 377 | struct dvb_frontend_tune_settings *fesettings) |
| 378 | { |
| 379 | fesettings->min_delay_ms = 800; |
| 380 | fesettings->step_size = 0; |
| 381 | fesettings->max_drift = 0; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static int af9033_set_frontend(struct dvb_frontend *fe) |
| 387 | { |
| 388 | struct af9033_state *state = fe->demodulator_priv; |
| 389 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
| 390 | int ret, i; |
| 391 | u8 tmp, buf[3], bandwidth_reg_val; |
| 392 | u32 if_frequency, freq_cw; |
| 393 | |
| 394 | pr_debug("%s: frequency=%d bandwidth_hz=%d\n", __func__, c->frequency, |
| 395 | c->bandwidth_hz); |
| 396 | |
| 397 | /* check bandwidth */ |
| 398 | switch (c->bandwidth_hz) { |
| 399 | case 6000000: |
| 400 | bandwidth_reg_val = 0x00; |
| 401 | break; |
| 402 | case 7000000: |
| 403 | bandwidth_reg_val = 0x01; |
| 404 | break; |
| 405 | case 8000000: |
| 406 | bandwidth_reg_val = 0x02; |
| 407 | break; |
| 408 | default: |
| 409 | pr_debug("%s: invalid bandwidth_hz\n", __func__); |
| 410 | ret = -EINVAL; |
| 411 | goto err; |
| 412 | } |
| 413 | |
| 414 | /* program tuner */ |
| 415 | if (fe->ops.tuner_ops.set_params) |
| 416 | fe->ops.tuner_ops.set_params(fe); |
| 417 | |
| 418 | /* program CFOE coefficients */ |
| 419 | if (c->bandwidth_hz != state->bandwidth_hz) { |
| 420 | for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { |
| 421 | if (coeff_lut[i].clock == state->cfg.clock && |
| 422 | coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | ret = af9033_wr_regs(state, 0x800001, |
| 427 | coeff_lut[i].val, sizeof(coeff_lut[i].val)); |
| 428 | } |
| 429 | |
| 430 | /* program frequency control */ |
| 431 | if (c->bandwidth_hz != state->bandwidth_hz) { |
| 432 | /* get used IF frequency */ |
| 433 | if (fe->ops.tuner_ops.get_if_frequency) |
| 434 | fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); |
| 435 | else |
| 436 | if_frequency = 0; |
| 437 | |
| 438 | /* FIXME: we support only Zero-IF currently */ |
| 439 | if (if_frequency != 0) { |
| 440 | pr_debug("%s: only Zero-IF supported currently\n", |
| 441 | __func__); |
| 442 | |
| 443 | ret = -ENODEV; |
| 444 | goto err; |
| 445 | } |
| 446 | |
| 447 | freq_cw = 0; |
| 448 | buf[0] = (freq_cw >> 0) & 0xff; |
| 449 | buf[1] = (freq_cw >> 8) & 0xff; |
| 450 | buf[2] = (freq_cw >> 16) & 0x7f; |
| 451 | ret = af9033_wr_regs(state, 0x800029, buf, 3); |
| 452 | if (ret < 0) |
| 453 | goto err; |
| 454 | |
| 455 | state->bandwidth_hz = c->bandwidth_hz; |
| 456 | } |
| 457 | |
| 458 | ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03); |
| 459 | if (ret < 0) |
| 460 | goto err; |
| 461 | |
| 462 | ret = af9033_wr_reg(state, 0x800040, 0x00); |
| 463 | if (ret < 0) |
| 464 | goto err; |
| 465 | |
| 466 | ret = af9033_wr_reg(state, 0x800047, 0x00); |
| 467 | if (ret < 0) |
| 468 | goto err; |
| 469 | |
| 470 | ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01); |
| 471 | if (ret < 0) |
| 472 | goto err; |
| 473 | |
| 474 | if (c->frequency <= 230000000) |
| 475 | tmp = 0x00; /* VHF */ |
| 476 | else |
| 477 | tmp = 0x01; /* UHF */ |
| 478 | |
| 479 | ret = af9033_wr_reg(state, 0x80004b, tmp); |
| 480 | if (ret < 0) |
| 481 | goto err; |
| 482 | |
| 483 | ret = af9033_wr_reg(state, 0x800000, 0x00); |
| 484 | if (ret < 0) |
| 485 | goto err; |
| 486 | |
| 487 | return 0; |
| 488 | |
| 489 | err: |
| 490 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 491 | |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 496 | { |
| 497 | struct af9033_state *state = fe->demodulator_priv; |
| 498 | int ret; |
| 499 | u8 tmp; |
| 500 | |
| 501 | *status = 0; |
| 502 | |
| 503 | /* radio channel status, 0=no result, 1=has signal, 2=no signal */ |
| 504 | ret = af9033_rd_reg(state, 0x800047, &tmp); |
| 505 | if (ret < 0) |
| 506 | goto err; |
| 507 | |
| 508 | /* has signal */ |
| 509 | if (tmp == 0x01) |
| 510 | *status |= FE_HAS_SIGNAL; |
| 511 | |
| 512 | if (tmp != 0x02) { |
| 513 | /* TPS lock */ |
| 514 | ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01); |
| 515 | if (ret < 0) |
| 516 | goto err; |
| 517 | |
| 518 | if (tmp) |
| 519 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 520 | FE_HAS_VITERBI; |
| 521 | |
| 522 | /* full lock */ |
| 523 | ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01); |
| 524 | if (ret < 0) |
| 525 | goto err; |
| 526 | |
| 527 | if (tmp) |
| 528 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 529 | FE_HAS_VITERBI | FE_HAS_SYNC | |
| 530 | FE_HAS_LOCK; |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | |
| 535 | err: |
| 536 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 537 | |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 542 | { |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 543 | struct af9033_state *state = fe->demodulator_priv; |
| 544 | int ret, i, len; |
| 545 | u8 buf[3], tmp; |
| 546 | u32 snr_val; |
| 547 | const struct val_snr *uninitialized_var(snr_lut); |
| 548 | |
| 549 | /* read value */ |
| 550 | ret = af9033_rd_regs(state, 0x80002c, buf, 3); |
| 551 | if (ret < 0) |
| 552 | goto err; |
| 553 | |
| 554 | snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 555 | |
| 556 | /* read current modulation */ |
| 557 | ret = af9033_rd_reg(state, 0x80f903, &tmp); |
| 558 | if (ret < 0) |
| 559 | goto err; |
| 560 | |
| 561 | switch ((tmp >> 0) & 3) { |
| 562 | case 0: |
| 563 | len = ARRAY_SIZE(qpsk_snr_lut); |
| 564 | snr_lut = qpsk_snr_lut; |
| 565 | break; |
| 566 | case 1: |
| 567 | len = ARRAY_SIZE(qam16_snr_lut); |
| 568 | snr_lut = qam16_snr_lut; |
| 569 | break; |
| 570 | case 2: |
| 571 | len = ARRAY_SIZE(qam64_snr_lut); |
| 572 | snr_lut = qam64_snr_lut; |
| 573 | break; |
| 574 | default: |
| 575 | goto err; |
| 576 | } |
| 577 | |
| 578 | for (i = 0; i < len; i++) { |
| 579 | tmp = snr_lut[i].snr; |
| 580 | |
| 581 | if (snr_val < snr_lut[i].val) |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | *snr = tmp * 10; /* dB/10 */ |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 586 | |
| 587 | return 0; |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 588 | |
| 589 | err: |
| 590 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 591 | |
| 592 | return ret; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 596 | { |
| 597 | struct af9033_state *state = fe->demodulator_priv; |
| 598 | int ret; |
| 599 | u8 strength2; |
| 600 | |
| 601 | /* read signal strength of 0-100 scale */ |
| 602 | ret = af9033_rd_reg(state, 0x800048, &strength2); |
| 603 | if (ret < 0) |
| 604 | goto err; |
| 605 | |
| 606 | /* scale value to 0x0000-0xffff */ |
| 607 | *strength = strength2 * 0xffff / 100; |
| 608 | |
| 609 | return 0; |
| 610 | |
| 611 | err: |
| 612 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 613 | |
| 614 | return ret; |
| 615 | } |
| 616 | |
| 617 | static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 618 | { |
| 619 | *ber = 0; |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 625 | { |
| 626 | *ucblocks = 0; |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
| 632 | { |
| 633 | struct af9033_state *state = fe->demodulator_priv; |
| 634 | int ret; |
| 635 | |
| 636 | pr_debug("%s: enable=%d\n", __func__, enable); |
| 637 | |
| 638 | ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01); |
| 639 | if (ret < 0) |
| 640 | goto err; |
| 641 | |
| 642 | return 0; |
| 643 | |
| 644 | err: |
| 645 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 646 | |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | static struct dvb_frontend_ops af9033_ops; |
| 651 | |
| 652 | struct dvb_frontend *af9033_attach(const struct af9033_config *config, |
| 653 | struct i2c_adapter *i2c) |
| 654 | { |
| 655 | int ret; |
| 656 | struct af9033_state *state; |
| 657 | u8 buf[8]; |
| 658 | |
| 659 | pr_debug("%s:\n", __func__); |
| 660 | |
| 661 | /* allocate memory for the internal state */ |
| 662 | state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL); |
| 663 | if (state == NULL) |
| 664 | goto err; |
| 665 | |
| 666 | /* setup the state */ |
| 667 | state->i2c = i2c; |
| 668 | memcpy(&state->cfg, config, sizeof(struct af9033_config)); |
| 669 | |
Antti Palosaari | 8e8a5ac | 2012-04-01 14:13:36 -0300 | [diff] [blame^] | 670 | if (state->cfg.clock != 12000000) { |
| 671 | printk(KERN_INFO "af9033: unsupported clock=%d, only " \ |
| 672 | "12000000 Hz is supported currently\n", |
| 673 | state->cfg.clock); |
| 674 | goto err; |
| 675 | } |
| 676 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 677 | /* firmware version */ |
| 678 | ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4); |
| 679 | if (ret < 0) |
| 680 | goto err; |
| 681 | |
| 682 | ret = af9033_rd_regs(state, 0x804191, &buf[4], 4); |
| 683 | if (ret < 0) |
| 684 | goto err; |
| 685 | |
| 686 | printk(KERN_INFO "af9033: firmware version: LINK=%d.%d.%d.%d " \ |
| 687 | "OFDM=%d.%d.%d.%d\n", buf[0], buf[1], buf[2], buf[3], |
| 688 | buf[4], buf[5], buf[6], buf[7]); |
| 689 | |
| 690 | /* configure internal TS mode */ |
| 691 | switch (state->cfg.ts_mode) { |
| 692 | case AF9033_TS_MODE_PARALLEL: |
| 693 | state->ts_mode_parallel = true; |
| 694 | break; |
| 695 | case AF9033_TS_MODE_SERIAL: |
| 696 | state->ts_mode_serial = true; |
| 697 | break; |
| 698 | case AF9033_TS_MODE_USB: |
| 699 | /* usb mode for AF9035 */ |
| 700 | default: |
| 701 | break; |
| 702 | } |
| 703 | |
| 704 | /* create dvb_frontend */ |
| 705 | memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); |
| 706 | state->fe.demodulator_priv = state; |
| 707 | |
| 708 | return &state->fe; |
| 709 | |
| 710 | err: |
| 711 | kfree(state); |
| 712 | return NULL; |
| 713 | } |
| 714 | EXPORT_SYMBOL(af9033_attach); |
| 715 | |
| 716 | static struct dvb_frontend_ops af9033_ops = { |
| 717 | .delsys = { SYS_DVBT }, |
| 718 | .info = { |
| 719 | .name = "Afatech AF9033 (DVB-T)", |
| 720 | .frequency_min = 174000000, |
| 721 | .frequency_max = 862000000, |
| 722 | .frequency_stepsize = 250000, |
| 723 | .frequency_tolerance = 0, |
| 724 | .caps = FE_CAN_FEC_1_2 | |
| 725 | FE_CAN_FEC_2_3 | |
| 726 | FE_CAN_FEC_3_4 | |
| 727 | FE_CAN_FEC_5_6 | |
| 728 | FE_CAN_FEC_7_8 | |
| 729 | FE_CAN_FEC_AUTO | |
| 730 | FE_CAN_QPSK | |
| 731 | FE_CAN_QAM_16 | |
| 732 | FE_CAN_QAM_64 | |
| 733 | FE_CAN_QAM_AUTO | |
| 734 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 735 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 736 | FE_CAN_HIERARCHY_AUTO | |
| 737 | FE_CAN_RECOVER | |
| 738 | FE_CAN_MUTE_TS |
| 739 | }, |
| 740 | |
| 741 | .release = af9033_release, |
| 742 | |
| 743 | .init = af9033_init, |
| 744 | .sleep = af9033_sleep, |
| 745 | |
| 746 | .get_tune_settings = af9033_get_tune_settings, |
| 747 | .set_frontend = af9033_set_frontend, |
| 748 | |
| 749 | .read_status = af9033_read_status, |
| 750 | .read_snr = af9033_read_snr, |
| 751 | .read_signal_strength = af9033_read_signal_strength, |
| 752 | .read_ber = af9033_read_ber, |
| 753 | .read_ucblocks = af9033_read_ucblocks, |
| 754 | |
| 755 | .i2c_gate_ctrl = af9033_i2c_gate_ctrl, |
| 756 | }; |
| 757 | |
| 758 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 759 | MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); |
| 760 | MODULE_LICENSE("GPL"); |