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; |
Michael Büsch | ffc501f | 2012-04-02 12:18:36 -0300 | [diff] [blame] | 300 | case AF9033_TUNER_FC0011: |
| 301 | len = ARRAY_SIZE(tuner_init_fc0011); |
| 302 | init = tuner_init_fc0011; |
| 303 | break; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame^] | 304 | case AF9033_TUNER_MXL5007T: |
| 305 | len = ARRAY_SIZE(tuner_init_mxl5007t); |
| 306 | init = tuner_init_mxl5007t; |
| 307 | break; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 308 | default: |
| 309 | pr_debug("%s: unsupported tuner ID=%d\n", __func__, |
| 310 | state->cfg.tuner); |
| 311 | ret = -ENODEV; |
| 312 | goto err; |
| 313 | } |
| 314 | |
| 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 | state->bandwidth_hz = 0; /* force to program all parameters */ |
| 322 | |
| 323 | return 0; |
| 324 | |
| 325 | err: |
| 326 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | static int af9033_sleep(struct dvb_frontend *fe) |
| 332 | { |
| 333 | struct af9033_state *state = fe->demodulator_priv; |
| 334 | int ret, i; |
| 335 | u8 tmp; |
| 336 | |
| 337 | ret = af9033_wr_reg(state, 0x80004c, 1); |
| 338 | if (ret < 0) |
| 339 | goto err; |
| 340 | |
| 341 | ret = af9033_wr_reg(state, 0x800000, 0); |
| 342 | if (ret < 0) |
| 343 | goto err; |
| 344 | |
| 345 | for (i = 100, tmp = 1; i && tmp; i--) { |
| 346 | ret = af9033_rd_reg(state, 0x80004c, &tmp); |
| 347 | if (ret < 0) |
| 348 | goto err; |
| 349 | |
| 350 | usleep_range(200, 10000); |
| 351 | } |
| 352 | |
Antti Palosaari | 3a871ca | 2012-04-01 11:14:59 -0300 | [diff] [blame] | 353 | pr_debug("%s: loop=%d\n", __func__, i); |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 354 | |
| 355 | if (i == 0) { |
| 356 | ret = -ETIMEDOUT; |
| 357 | goto err; |
| 358 | } |
| 359 | |
| 360 | ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08); |
| 361 | if (ret < 0) |
| 362 | goto err; |
| 363 | |
| 364 | /* prevent current leak (?) */ |
| 365 | if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) { |
| 366 | /* enable parallel TS */ |
| 367 | ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01); |
| 368 | if (ret < 0) |
| 369 | goto err; |
| 370 | |
| 371 | ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01); |
| 372 | if (ret < 0) |
| 373 | goto err; |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | |
| 378 | err: |
| 379 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | static int af9033_get_tune_settings(struct dvb_frontend *fe, |
| 385 | struct dvb_frontend_tune_settings *fesettings) |
| 386 | { |
| 387 | fesettings->min_delay_ms = 800; |
| 388 | fesettings->step_size = 0; |
| 389 | fesettings->max_drift = 0; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int af9033_set_frontend(struct dvb_frontend *fe) |
| 395 | { |
| 396 | struct af9033_state *state = fe->demodulator_priv; |
| 397 | struct dtv_frontend_properties *c = &fe->dtv_property_cache; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame^] | 398 | int ret, i, spec_inv; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 399 | u8 tmp, buf[3], bandwidth_reg_val; |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame^] | 400 | u32 if_frequency, freq_cw, adc_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 401 | |
| 402 | pr_debug("%s: frequency=%d bandwidth_hz=%d\n", __func__, c->frequency, |
| 403 | c->bandwidth_hz); |
| 404 | |
| 405 | /* check bandwidth */ |
| 406 | switch (c->bandwidth_hz) { |
| 407 | case 6000000: |
| 408 | bandwidth_reg_val = 0x00; |
| 409 | break; |
| 410 | case 7000000: |
| 411 | bandwidth_reg_val = 0x01; |
| 412 | break; |
| 413 | case 8000000: |
| 414 | bandwidth_reg_val = 0x02; |
| 415 | break; |
| 416 | default: |
| 417 | pr_debug("%s: invalid bandwidth_hz\n", __func__); |
| 418 | ret = -EINVAL; |
| 419 | goto err; |
| 420 | } |
| 421 | |
| 422 | /* program tuner */ |
| 423 | if (fe->ops.tuner_ops.set_params) |
| 424 | fe->ops.tuner_ops.set_params(fe); |
| 425 | |
| 426 | /* program CFOE coefficients */ |
| 427 | if (c->bandwidth_hz != state->bandwidth_hz) { |
| 428 | for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { |
| 429 | if (coeff_lut[i].clock == state->cfg.clock && |
| 430 | coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | ret = af9033_wr_regs(state, 0x800001, |
| 435 | coeff_lut[i].val, sizeof(coeff_lut[i].val)); |
| 436 | } |
| 437 | |
| 438 | /* program frequency control */ |
| 439 | if (c->bandwidth_hz != state->bandwidth_hz) { |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame^] | 440 | spec_inv = state->cfg.spec_inv ? -1 : 1; |
| 441 | |
| 442 | for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) { |
| 443 | if (clock_adc_lut[i].clock == state->cfg.clock) |
| 444 | break; |
| 445 | } |
| 446 | adc_freq = clock_adc_lut[i].adc; |
| 447 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 448 | /* get used IF frequency */ |
| 449 | if (fe->ops.tuner_ops.get_if_frequency) |
| 450 | fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency); |
| 451 | else |
| 452 | if_frequency = 0; |
| 453 | |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame^] | 454 | while (if_frequency > (adc_freq / 2)) |
| 455 | if_frequency -= adc_freq; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 456 | |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame^] | 457 | if (if_frequency >= 0) |
| 458 | spec_inv *= -1; |
| 459 | else |
| 460 | if_frequency *= -1; |
| 461 | |
| 462 | freq_cw = af9033_div(if_frequency, adc_freq, 23ul); |
| 463 | |
| 464 | if (spec_inv == -1) |
| 465 | freq_cw *= -1; |
| 466 | |
| 467 | /* get adc multiplies */ |
| 468 | ret = af9033_rd_reg(state, 0x800045, &tmp); |
| 469 | if (ret < 0) |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 470 | goto err; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 471 | |
Hans-Frieder Vogt | 540fd4b | 2012-04-02 14:18:16 -0300 | [diff] [blame^] | 472 | if (tmp == 1) |
| 473 | freq_cw /= 2; |
| 474 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 475 | buf[0] = (freq_cw >> 0) & 0xff; |
| 476 | buf[1] = (freq_cw >> 8) & 0xff; |
| 477 | buf[2] = (freq_cw >> 16) & 0x7f; |
| 478 | ret = af9033_wr_regs(state, 0x800029, buf, 3); |
| 479 | if (ret < 0) |
| 480 | goto err; |
| 481 | |
| 482 | state->bandwidth_hz = c->bandwidth_hz; |
| 483 | } |
| 484 | |
| 485 | ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03); |
| 486 | if (ret < 0) |
| 487 | goto err; |
| 488 | |
| 489 | ret = af9033_wr_reg(state, 0x800040, 0x00); |
| 490 | if (ret < 0) |
| 491 | goto err; |
| 492 | |
| 493 | ret = af9033_wr_reg(state, 0x800047, 0x00); |
| 494 | if (ret < 0) |
| 495 | goto err; |
| 496 | |
| 497 | ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01); |
| 498 | if (ret < 0) |
| 499 | goto err; |
| 500 | |
| 501 | if (c->frequency <= 230000000) |
| 502 | tmp = 0x00; /* VHF */ |
| 503 | else |
| 504 | tmp = 0x01; /* UHF */ |
| 505 | |
| 506 | ret = af9033_wr_reg(state, 0x80004b, tmp); |
| 507 | if (ret < 0) |
| 508 | goto err; |
| 509 | |
| 510 | ret = af9033_wr_reg(state, 0x800000, 0x00); |
| 511 | if (ret < 0) |
| 512 | goto err; |
| 513 | |
| 514 | return 0; |
| 515 | |
| 516 | err: |
| 517 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 523 | { |
| 524 | struct af9033_state *state = fe->demodulator_priv; |
| 525 | int ret; |
| 526 | u8 tmp; |
| 527 | |
| 528 | *status = 0; |
| 529 | |
| 530 | /* radio channel status, 0=no result, 1=has signal, 2=no signal */ |
| 531 | ret = af9033_rd_reg(state, 0x800047, &tmp); |
| 532 | if (ret < 0) |
| 533 | goto err; |
| 534 | |
| 535 | /* has signal */ |
| 536 | if (tmp == 0x01) |
| 537 | *status |= FE_HAS_SIGNAL; |
| 538 | |
| 539 | if (tmp != 0x02) { |
| 540 | /* TPS lock */ |
| 541 | ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01); |
| 542 | if (ret < 0) |
| 543 | goto err; |
| 544 | |
| 545 | if (tmp) |
| 546 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 547 | FE_HAS_VITERBI; |
| 548 | |
| 549 | /* full lock */ |
| 550 | ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01); |
| 551 | if (ret < 0) |
| 552 | goto err; |
| 553 | |
| 554 | if (tmp) |
| 555 | *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | |
| 556 | FE_HAS_VITERBI | FE_HAS_SYNC | |
| 557 | FE_HAS_LOCK; |
| 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | |
| 562 | err: |
| 563 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 564 | |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 569 | { |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 570 | struct af9033_state *state = fe->demodulator_priv; |
| 571 | int ret, i, len; |
| 572 | u8 buf[3], tmp; |
| 573 | u32 snr_val; |
| 574 | const struct val_snr *uninitialized_var(snr_lut); |
| 575 | |
| 576 | /* read value */ |
| 577 | ret = af9033_rd_regs(state, 0x80002c, buf, 3); |
| 578 | if (ret < 0) |
| 579 | goto err; |
| 580 | |
| 581 | snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; |
| 582 | |
| 583 | /* read current modulation */ |
| 584 | ret = af9033_rd_reg(state, 0x80f903, &tmp); |
| 585 | if (ret < 0) |
| 586 | goto err; |
| 587 | |
| 588 | switch ((tmp >> 0) & 3) { |
| 589 | case 0: |
| 590 | len = ARRAY_SIZE(qpsk_snr_lut); |
| 591 | snr_lut = qpsk_snr_lut; |
| 592 | break; |
| 593 | case 1: |
| 594 | len = ARRAY_SIZE(qam16_snr_lut); |
| 595 | snr_lut = qam16_snr_lut; |
| 596 | break; |
| 597 | case 2: |
| 598 | len = ARRAY_SIZE(qam64_snr_lut); |
| 599 | snr_lut = qam64_snr_lut; |
| 600 | break; |
| 601 | default: |
| 602 | goto err; |
| 603 | } |
| 604 | |
| 605 | for (i = 0; i < len; i++) { |
| 606 | tmp = snr_lut[i].snr; |
| 607 | |
| 608 | if (snr_val < snr_lut[i].val) |
| 609 | break; |
| 610 | } |
| 611 | |
| 612 | *snr = tmp * 10; /* dB/10 */ |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 613 | |
| 614 | return 0; |
Antti Palosaari | e898ef6 | 2012-04-01 12:50:02 -0300 | [diff] [blame] | 615 | |
| 616 | err: |
| 617 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 618 | |
| 619 | return ret; |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength) |
| 623 | { |
| 624 | struct af9033_state *state = fe->demodulator_priv; |
| 625 | int ret; |
| 626 | u8 strength2; |
| 627 | |
| 628 | /* read signal strength of 0-100 scale */ |
| 629 | ret = af9033_rd_reg(state, 0x800048, &strength2); |
| 630 | if (ret < 0) |
| 631 | goto err; |
| 632 | |
| 633 | /* scale value to 0x0000-0xffff */ |
| 634 | *strength = strength2 * 0xffff / 100; |
| 635 | |
| 636 | return 0; |
| 637 | |
| 638 | err: |
| 639 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 640 | |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber) |
| 645 | { |
| 646 | *ber = 0; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
| 652 | { |
| 653 | *ucblocks = 0; |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
| 659 | { |
| 660 | struct af9033_state *state = fe->demodulator_priv; |
| 661 | int ret; |
| 662 | |
| 663 | pr_debug("%s: enable=%d\n", __func__, enable); |
| 664 | |
| 665 | ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01); |
| 666 | if (ret < 0) |
| 667 | goto err; |
| 668 | |
| 669 | return 0; |
| 670 | |
| 671 | err: |
| 672 | pr_debug("%s: failed=%d\n", __func__, ret); |
| 673 | |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | static struct dvb_frontend_ops af9033_ops; |
| 678 | |
| 679 | struct dvb_frontend *af9033_attach(const struct af9033_config *config, |
| 680 | struct i2c_adapter *i2c) |
| 681 | { |
| 682 | int ret; |
| 683 | struct af9033_state *state; |
| 684 | u8 buf[8]; |
| 685 | |
| 686 | pr_debug("%s:\n", __func__); |
| 687 | |
| 688 | /* allocate memory for the internal state */ |
| 689 | state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL); |
| 690 | if (state == NULL) |
| 691 | goto err; |
| 692 | |
| 693 | /* setup the state */ |
| 694 | state->i2c = i2c; |
| 695 | memcpy(&state->cfg, config, sizeof(struct af9033_config)); |
| 696 | |
Antti Palosaari | 8e8a5ac | 2012-04-01 14:13:36 -0300 | [diff] [blame] | 697 | if (state->cfg.clock != 12000000) { |
| 698 | printk(KERN_INFO "af9033: unsupported clock=%d, only " \ |
| 699 | "12000000 Hz is supported currently\n", |
| 700 | state->cfg.clock); |
| 701 | goto err; |
| 702 | } |
| 703 | |
Antti Palosaari | 4b64bb2 | 2012-03-30 08:21:25 -0300 | [diff] [blame] | 704 | /* firmware version */ |
| 705 | ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4); |
| 706 | if (ret < 0) |
| 707 | goto err; |
| 708 | |
| 709 | ret = af9033_rd_regs(state, 0x804191, &buf[4], 4); |
| 710 | if (ret < 0) |
| 711 | goto err; |
| 712 | |
| 713 | printk(KERN_INFO "af9033: firmware version: LINK=%d.%d.%d.%d " \ |
| 714 | "OFDM=%d.%d.%d.%d\n", buf[0], buf[1], buf[2], buf[3], |
| 715 | buf[4], buf[5], buf[6], buf[7]); |
| 716 | |
| 717 | /* configure internal TS mode */ |
| 718 | switch (state->cfg.ts_mode) { |
| 719 | case AF9033_TS_MODE_PARALLEL: |
| 720 | state->ts_mode_parallel = true; |
| 721 | break; |
| 722 | case AF9033_TS_MODE_SERIAL: |
| 723 | state->ts_mode_serial = true; |
| 724 | break; |
| 725 | case AF9033_TS_MODE_USB: |
| 726 | /* usb mode for AF9035 */ |
| 727 | default: |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | /* create dvb_frontend */ |
| 732 | memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops)); |
| 733 | state->fe.demodulator_priv = state; |
| 734 | |
| 735 | return &state->fe; |
| 736 | |
| 737 | err: |
| 738 | kfree(state); |
| 739 | return NULL; |
| 740 | } |
| 741 | EXPORT_SYMBOL(af9033_attach); |
| 742 | |
| 743 | static struct dvb_frontend_ops af9033_ops = { |
| 744 | .delsys = { SYS_DVBT }, |
| 745 | .info = { |
| 746 | .name = "Afatech AF9033 (DVB-T)", |
| 747 | .frequency_min = 174000000, |
| 748 | .frequency_max = 862000000, |
| 749 | .frequency_stepsize = 250000, |
| 750 | .frequency_tolerance = 0, |
| 751 | .caps = FE_CAN_FEC_1_2 | |
| 752 | FE_CAN_FEC_2_3 | |
| 753 | FE_CAN_FEC_3_4 | |
| 754 | FE_CAN_FEC_5_6 | |
| 755 | FE_CAN_FEC_7_8 | |
| 756 | FE_CAN_FEC_AUTO | |
| 757 | FE_CAN_QPSK | |
| 758 | FE_CAN_QAM_16 | |
| 759 | FE_CAN_QAM_64 | |
| 760 | FE_CAN_QAM_AUTO | |
| 761 | FE_CAN_TRANSMISSION_MODE_AUTO | |
| 762 | FE_CAN_GUARD_INTERVAL_AUTO | |
| 763 | FE_CAN_HIERARCHY_AUTO | |
| 764 | FE_CAN_RECOVER | |
| 765 | FE_CAN_MUTE_TS |
| 766 | }, |
| 767 | |
| 768 | .release = af9033_release, |
| 769 | |
| 770 | .init = af9033_init, |
| 771 | .sleep = af9033_sleep, |
| 772 | |
| 773 | .get_tune_settings = af9033_get_tune_settings, |
| 774 | .set_frontend = af9033_set_frontend, |
| 775 | |
| 776 | .read_status = af9033_read_status, |
| 777 | .read_snr = af9033_read_snr, |
| 778 | .read_signal_strength = af9033_read_signal_strength, |
| 779 | .read_ber = af9033_read_ber, |
| 780 | .read_ucblocks = af9033_read_ucblocks, |
| 781 | |
| 782 | .i2c_gate_ctrl = af9033_i2c_gate_ctrl, |
| 783 | }; |
| 784 | |
| 785 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 786 | MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver"); |
| 787 | MODULE_LICENSE("GPL"); |