1 /* 2 * Afatech AF9013 demodulator driver 3 * 4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi> 5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi> 6 * 7 * Thanks to Afatech who kindly provided information. 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 */ 20 21 #include "af9013_priv.h" 22 23 struct af9013_state { 24 struct i2c_client *client; 25 struct regmap *regmap; 26 struct dvb_frontend fe; 27 u32 clk; 28 u8 tuner; 29 u32 if_frequency; 30 u8 ts_mode; 31 u8 ts_output_pin; 32 bool spec_inv; 33 u8 api_version[4]; 34 u8 gpio[4]; 35 36 /* tuner/demod RF and IF AGC limits used for signal strength calc */ 37 u8 signal_strength_en, rf_50, rf_80, if_50, if_80; 38 u16 signal_strength; 39 u32 ber; 40 u32 ucblocks; 41 u16 snr; 42 u32 bandwidth_hz; 43 enum fe_status fe_status; 44 /* RF and IF AGC limits used for signal strength calc */ 45 u8 strength_en, rf_agc_50, rf_agc_80, if_agc_50, if_agc_80; 46 unsigned long set_frontend_jiffies; 47 unsigned long read_status_jiffies; 48 unsigned long strength_jiffies; 49 bool first_tune; 50 bool i2c_gate_state; 51 unsigned int statistics_step:3; 52 struct delayed_work statistics_work; 53 }; 54 55 static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval) 56 { 57 struct i2c_client *client = state->client; 58 int ret; 59 u8 pos; 60 u16 addr; 61 62 dev_dbg(&client->dev, "gpio %u, gpioval %02x\n", gpio, gpioval); 63 64 /* 65 * GPIO0 & GPIO1 0xd735 66 * GPIO2 & GPIO3 0xd736 67 */ 68 69 switch (gpio) { 70 case 0: 71 case 1: 72 addr = 0xd735; 73 break; 74 case 2: 75 case 3: 76 addr = 0xd736; 77 break; 78 79 default: 80 ret = -EINVAL; 81 goto err; 82 } 83 84 switch (gpio) { 85 case 0: 86 case 2: 87 pos = 0; 88 break; 89 case 1: 90 case 3: 91 default: 92 pos = 4; 93 break; 94 } 95 96 ret = regmap_update_bits(state->regmap, addr, 0x0f << pos, 97 gpioval << pos); 98 if (ret) 99 goto err; 100 101 return 0; 102 err: 103 dev_dbg(&client->dev, "failed %d\n", ret); 104 return ret; 105 } 106 107 static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe) 108 { 109 struct af9013_state *state = fe->demodulator_priv; 110 struct i2c_client *client = state->client; 111 int ret; 112 113 dev_dbg(&client->dev, "\n"); 114 115 /* reset and start BER counter */ 116 ret = regmap_update_bits(state->regmap, 0xd391, 0x10, 0x10); 117 if (ret) 118 goto err; 119 120 return 0; 121 err: 122 dev_dbg(&client->dev, "failed %d\n", ret); 123 return ret; 124 } 125 126 static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe) 127 { 128 struct af9013_state *state = fe->demodulator_priv; 129 struct i2c_client *client = state->client; 130 int ret; 131 unsigned int utmp; 132 u8 buf[5]; 133 134 dev_dbg(&client->dev, "\n"); 135 136 /* check if error bit count is ready */ 137 ret = regmap_read(state->regmap, 0xd391, &utmp); 138 if (ret) 139 goto err; 140 141 if (!((utmp >> 4) & 0x01)) { 142 dev_dbg(&client->dev, "not ready\n"); 143 return 0; 144 } 145 146 ret = regmap_bulk_read(state->regmap, 0xd387, buf, 5); 147 if (ret) 148 goto err; 149 150 state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0]; 151 state->ucblocks += (buf[4] << 8) | buf[3]; 152 153 return 0; 154 err: 155 dev_dbg(&client->dev, "failed %d\n", ret); 156 return ret; 157 } 158 159 static int af9013_statistics_snr_start(struct dvb_frontend *fe) 160 { 161 struct af9013_state *state = fe->demodulator_priv; 162 struct i2c_client *client = state->client; 163 int ret; 164 165 dev_dbg(&client->dev, "\n"); 166 167 /* start SNR meas */ 168 ret = regmap_update_bits(state->regmap, 0xd2e1, 0x08, 0x08); 169 if (ret) 170 goto err; 171 172 return 0; 173 err: 174 dev_dbg(&client->dev, "failed %d\n", ret); 175 return ret; 176 } 177 178 static int af9013_statistics_snr_result(struct dvb_frontend *fe) 179 { 180 struct af9013_state *state = fe->demodulator_priv; 181 struct i2c_client *client = state->client; 182 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 183 int ret, i, len; 184 unsigned int utmp; 185 u8 buf[3]; 186 u32 snr_val; 187 const struct af9013_snr *uninitialized_var(snr_lut); 188 189 dev_dbg(&client->dev, "\n"); 190 191 /* check if SNR ready */ 192 ret = regmap_read(state->regmap, 0xd2e1, &utmp); 193 if (ret) 194 goto err; 195 196 if (!((utmp >> 3) & 0x01)) { 197 dev_dbg(&client->dev, "not ready\n"); 198 return 0; 199 } 200 201 /* read value */ 202 ret = regmap_bulk_read(state->regmap, 0xd2e3, buf, 3); 203 if (ret) 204 goto err; 205 206 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0]; 207 208 /* read current modulation */ 209 ret = regmap_read(state->regmap, 0xd3c1, &utmp); 210 if (ret) 211 goto err; 212 213 switch ((utmp >> 6) & 3) { 214 case 0: 215 len = ARRAY_SIZE(qpsk_snr_lut); 216 snr_lut = qpsk_snr_lut; 217 break; 218 case 1: 219 len = ARRAY_SIZE(qam16_snr_lut); 220 snr_lut = qam16_snr_lut; 221 break; 222 case 2: 223 len = ARRAY_SIZE(qam64_snr_lut); 224 snr_lut = qam64_snr_lut; 225 break; 226 default: 227 goto err; 228 } 229 230 for (i = 0; i < len; i++) { 231 utmp = snr_lut[i].snr; 232 233 if (snr_val < snr_lut[i].val) 234 break; 235 } 236 state->snr = utmp * 10; /* dB/10 */ 237 238 c->cnr.stat[0].svalue = 1000 * utmp; 239 c->cnr.stat[0].scale = FE_SCALE_DECIBEL; 240 241 return 0; 242 err: 243 dev_dbg(&client->dev, "failed %d\n", ret); 244 return ret; 245 } 246 247 static int af9013_statistics_signal_strength(struct dvb_frontend *fe) 248 { 249 struct af9013_state *state = fe->demodulator_priv; 250 struct i2c_client *client = state->client; 251 int ret = 0; 252 u8 buf[2], rf_gain, if_gain; 253 int signal_strength; 254 255 dev_dbg(&client->dev, "\n"); 256 257 if (!state->signal_strength_en) 258 return 0; 259 260 ret = regmap_bulk_read(state->regmap, 0xd07c, buf, 2); 261 if (ret) 262 goto err; 263 264 rf_gain = buf[0]; 265 if_gain = buf[1]; 266 267 signal_strength = (0xffff / \ 268 (9 * (state->rf_50 + state->if_50) - \ 269 11 * (state->rf_80 + state->if_80))) * \ 270 (10 * (rf_gain + if_gain) - \ 271 11 * (state->rf_80 + state->if_80)); 272 if (signal_strength < 0) 273 signal_strength = 0; 274 else if (signal_strength > 0xffff) 275 signal_strength = 0xffff; 276 277 state->signal_strength = signal_strength; 278 279 return 0; 280 err: 281 dev_dbg(&client->dev, "failed %d\n", ret); 282 return ret; 283 } 284 285 static void af9013_statistics_work(struct work_struct *work) 286 { 287 struct af9013_state *state = container_of(work, 288 struct af9013_state, statistics_work.work); 289 unsigned int next_msec; 290 291 /* update only signal strength when demod is not locked */ 292 if (!(state->fe_status & FE_HAS_LOCK)) { 293 state->statistics_step = 0; 294 state->ber = 0; 295 state->snr = 0; 296 } 297 298 switch (state->statistics_step) { 299 default: 300 state->statistics_step = 0; 301 /* fall-through */ 302 case 0: 303 af9013_statistics_signal_strength(&state->fe); 304 state->statistics_step++; 305 next_msec = 300; 306 break; 307 case 1: 308 af9013_statistics_snr_start(&state->fe); 309 state->statistics_step++; 310 next_msec = 200; 311 break; 312 case 2: 313 af9013_statistics_ber_unc_start(&state->fe); 314 state->statistics_step++; 315 next_msec = 1000; 316 break; 317 case 3: 318 af9013_statistics_snr_result(&state->fe); 319 state->statistics_step++; 320 next_msec = 400; 321 break; 322 case 4: 323 af9013_statistics_ber_unc_result(&state->fe); 324 state->statistics_step++; 325 next_msec = 100; 326 break; 327 } 328 329 schedule_delayed_work(&state->statistics_work, 330 msecs_to_jiffies(next_msec)); 331 } 332 333 static int af9013_get_tune_settings(struct dvb_frontend *fe, 334 struct dvb_frontend_tune_settings *fesettings) 335 { 336 fesettings->min_delay_ms = 800; 337 fesettings->step_size = 0; 338 fesettings->max_drift = 0; 339 340 return 0; 341 } 342 343 static int af9013_set_frontend(struct dvb_frontend *fe) 344 { 345 struct af9013_state *state = fe->demodulator_priv; 346 struct i2c_client *client = state->client; 347 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 348 int ret, i, sampling_freq; 349 bool auto_mode, spec_inv; 350 u8 buf[6]; 351 u32 if_frequency, freq_cw; 352 353 dev_dbg(&client->dev, "frequency %u, bandwidth_hz %u\n", 354 c->frequency, c->bandwidth_hz); 355 356 /* program tuner */ 357 if (fe->ops.tuner_ops.set_params) { 358 ret = fe->ops.tuner_ops.set_params(fe); 359 if (ret) 360 goto err; 361 } 362 363 /* program CFOE coefficients */ 364 if (c->bandwidth_hz != state->bandwidth_hz) { 365 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) { 366 if (coeff_lut[i].clock == state->clk && 367 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) { 368 break; 369 } 370 } 371 372 /* Return an error if can't find bandwidth or the right clock */ 373 if (i == ARRAY_SIZE(coeff_lut)) { 374 ret = -EINVAL; 375 goto err; 376 } 377 378 ret = regmap_bulk_write(state->regmap, 0xae00, coeff_lut[i].val, 379 sizeof(coeff_lut[i].val)); 380 if (ret) 381 goto err; 382 } 383 384 /* program frequency control */ 385 if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) { 386 /* get used IF frequency */ 387 if (fe->ops.tuner_ops.get_if_frequency) { 388 ret = fe->ops.tuner_ops.get_if_frequency(fe, 389 &if_frequency); 390 if (ret) 391 goto err; 392 } else { 393 if_frequency = state->if_frequency; 394 } 395 396 dev_dbg(&client->dev, "if_frequency %u\n", if_frequency); 397 398 sampling_freq = if_frequency; 399 400 while (sampling_freq > (state->clk / 2)) 401 sampling_freq -= state->clk; 402 403 if (sampling_freq < 0) { 404 sampling_freq *= -1; 405 spec_inv = state->spec_inv; 406 } else { 407 spec_inv = !state->spec_inv; 408 } 409 410 freq_cw = DIV_ROUND_CLOSEST_ULL((u64)sampling_freq * 0x800000, 411 state->clk); 412 413 if (spec_inv) 414 freq_cw = 0x800000 - freq_cw; 415 416 buf[0] = (freq_cw >> 0) & 0xff; 417 buf[1] = (freq_cw >> 8) & 0xff; 418 buf[2] = (freq_cw >> 16) & 0x7f; 419 420 freq_cw = 0x800000 - freq_cw; 421 422 buf[3] = (freq_cw >> 0) & 0xff; 423 buf[4] = (freq_cw >> 8) & 0xff; 424 buf[5] = (freq_cw >> 16) & 0x7f; 425 426 ret = regmap_bulk_write(state->regmap, 0xd140, buf, 3); 427 if (ret) 428 goto err; 429 430 ret = regmap_bulk_write(state->regmap, 0x9be7, buf, 6); 431 if (ret) 432 goto err; 433 } 434 435 /* clear TPS lock flag */ 436 ret = regmap_update_bits(state->regmap, 0xd330, 0x08, 0x08); 437 if (ret) 438 goto err; 439 440 /* clear MPEG2 lock flag */ 441 ret = regmap_update_bits(state->regmap, 0xd507, 0x40, 0x00); 442 if (ret) 443 goto err; 444 445 /* empty channel function */ 446 ret = regmap_update_bits(state->regmap, 0x9bfe, 0x01, 0x00); 447 if (ret) 448 goto err; 449 450 /* empty DVB-T channel function */ 451 ret = regmap_update_bits(state->regmap, 0x9bc2, 0x01, 0x00); 452 if (ret) 453 goto err; 454 455 /* transmission parameters */ 456 auto_mode = false; 457 memset(buf, 0, 3); 458 459 switch (c->transmission_mode) { 460 case TRANSMISSION_MODE_AUTO: 461 auto_mode = true; 462 break; 463 case TRANSMISSION_MODE_2K: 464 break; 465 case TRANSMISSION_MODE_8K: 466 buf[0] |= (1 << 0); 467 break; 468 default: 469 dev_dbg(&client->dev, "invalid transmission_mode\n"); 470 auto_mode = true; 471 } 472 473 switch (c->guard_interval) { 474 case GUARD_INTERVAL_AUTO: 475 auto_mode = true; 476 break; 477 case GUARD_INTERVAL_1_32: 478 break; 479 case GUARD_INTERVAL_1_16: 480 buf[0] |= (1 << 2); 481 break; 482 case GUARD_INTERVAL_1_8: 483 buf[0] |= (2 << 2); 484 break; 485 case GUARD_INTERVAL_1_4: 486 buf[0] |= (3 << 2); 487 break; 488 default: 489 dev_dbg(&client->dev, "invalid guard_interval\n"); 490 auto_mode = true; 491 } 492 493 switch (c->hierarchy) { 494 case HIERARCHY_AUTO: 495 auto_mode = true; 496 break; 497 case HIERARCHY_NONE: 498 break; 499 case HIERARCHY_1: 500 buf[0] |= (1 << 4); 501 break; 502 case HIERARCHY_2: 503 buf[0] |= (2 << 4); 504 break; 505 case HIERARCHY_4: 506 buf[0] |= (3 << 4); 507 break; 508 default: 509 dev_dbg(&client->dev, "invalid hierarchy\n"); 510 auto_mode = true; 511 } 512 513 switch (c->modulation) { 514 case QAM_AUTO: 515 auto_mode = true; 516 break; 517 case QPSK: 518 break; 519 case QAM_16: 520 buf[1] |= (1 << 6); 521 break; 522 case QAM_64: 523 buf[1] |= (2 << 6); 524 break; 525 default: 526 dev_dbg(&client->dev, "invalid modulation\n"); 527 auto_mode = true; 528 } 529 530 /* Use HP. How and which case we can switch to LP? */ 531 buf[1] |= (1 << 4); 532 533 switch (c->code_rate_HP) { 534 case FEC_AUTO: 535 auto_mode = true; 536 break; 537 case FEC_1_2: 538 break; 539 case FEC_2_3: 540 buf[2] |= (1 << 0); 541 break; 542 case FEC_3_4: 543 buf[2] |= (2 << 0); 544 break; 545 case FEC_5_6: 546 buf[2] |= (3 << 0); 547 break; 548 case FEC_7_8: 549 buf[2] |= (4 << 0); 550 break; 551 default: 552 dev_dbg(&client->dev, "invalid code_rate_HP\n"); 553 auto_mode = true; 554 } 555 556 switch (c->code_rate_LP) { 557 case FEC_AUTO: 558 auto_mode = true; 559 break; 560 case FEC_1_2: 561 break; 562 case FEC_2_3: 563 buf[2] |= (1 << 3); 564 break; 565 case FEC_3_4: 566 buf[2] |= (2 << 3); 567 break; 568 case FEC_5_6: 569 buf[2] |= (3 << 3); 570 break; 571 case FEC_7_8: 572 buf[2] |= (4 << 3); 573 break; 574 case FEC_NONE: 575 break; 576 default: 577 dev_dbg(&client->dev, "invalid code_rate_LP\n"); 578 auto_mode = true; 579 } 580 581 switch (c->bandwidth_hz) { 582 case 6000000: 583 break; 584 case 7000000: 585 buf[1] |= (1 << 2); 586 break; 587 case 8000000: 588 buf[1] |= (2 << 2); 589 break; 590 default: 591 dev_dbg(&client->dev, "invalid bandwidth_hz\n"); 592 ret = -EINVAL; 593 goto err; 594 } 595 596 ret = regmap_bulk_write(state->regmap, 0xd3c0, buf, 3); 597 if (ret) 598 goto err; 599 600 if (auto_mode) { 601 /* clear easy mode flag */ 602 ret = regmap_write(state->regmap, 0xaefd, 0x00); 603 if (ret) 604 goto err; 605 606 dev_dbg(&client->dev, "auto params\n"); 607 } else { 608 /* set easy mode flag */ 609 ret = regmap_write(state->regmap, 0xaefd, 0x01); 610 if (ret) 611 goto err; 612 613 ret = regmap_write(state->regmap, 0xaefe, 0x00); 614 if (ret) 615 goto err; 616 617 dev_dbg(&client->dev, "manual params\n"); 618 } 619 620 /* Reset FSM */ 621 ret = regmap_write(state->regmap, 0xffff, 0x00); 622 if (ret) 623 goto err; 624 625 state->bandwidth_hz = c->bandwidth_hz; 626 state->set_frontend_jiffies = jiffies; 627 state->first_tune = false; 628 629 return 0; 630 err: 631 dev_dbg(&client->dev, "failed %d\n", ret); 632 return ret; 633 } 634 635 static int af9013_get_frontend(struct dvb_frontend *fe, 636 struct dtv_frontend_properties *c) 637 { 638 struct af9013_state *state = fe->demodulator_priv; 639 struct i2c_client *client = state->client; 640 int ret; 641 u8 buf[3]; 642 643 dev_dbg(&client->dev, "\n"); 644 645 ret = regmap_bulk_read(state->regmap, 0xd3c0, buf, 3); 646 if (ret) 647 goto err; 648 649 switch ((buf[1] >> 6) & 3) { 650 case 0: 651 c->modulation = QPSK; 652 break; 653 case 1: 654 c->modulation = QAM_16; 655 break; 656 case 2: 657 c->modulation = QAM_64; 658 break; 659 } 660 661 switch ((buf[0] >> 0) & 3) { 662 case 0: 663 c->transmission_mode = TRANSMISSION_MODE_2K; 664 break; 665 case 1: 666 c->transmission_mode = TRANSMISSION_MODE_8K; 667 } 668 669 switch ((buf[0] >> 2) & 3) { 670 case 0: 671 c->guard_interval = GUARD_INTERVAL_1_32; 672 break; 673 case 1: 674 c->guard_interval = GUARD_INTERVAL_1_16; 675 break; 676 case 2: 677 c->guard_interval = GUARD_INTERVAL_1_8; 678 break; 679 case 3: 680 c->guard_interval = GUARD_INTERVAL_1_4; 681 break; 682 } 683 684 switch ((buf[0] >> 4) & 7) { 685 case 0: 686 c->hierarchy = HIERARCHY_NONE; 687 break; 688 case 1: 689 c->hierarchy = HIERARCHY_1; 690 break; 691 case 2: 692 c->hierarchy = HIERARCHY_2; 693 break; 694 case 3: 695 c->hierarchy = HIERARCHY_4; 696 break; 697 } 698 699 switch ((buf[2] >> 0) & 7) { 700 case 0: 701 c->code_rate_HP = FEC_1_2; 702 break; 703 case 1: 704 c->code_rate_HP = FEC_2_3; 705 break; 706 case 2: 707 c->code_rate_HP = FEC_3_4; 708 break; 709 case 3: 710 c->code_rate_HP = FEC_5_6; 711 break; 712 case 4: 713 c->code_rate_HP = FEC_7_8; 714 break; 715 } 716 717 switch ((buf[2] >> 3) & 7) { 718 case 0: 719 c->code_rate_LP = FEC_1_2; 720 break; 721 case 1: 722 c->code_rate_LP = FEC_2_3; 723 break; 724 case 2: 725 c->code_rate_LP = FEC_3_4; 726 break; 727 case 3: 728 c->code_rate_LP = FEC_5_6; 729 break; 730 case 4: 731 c->code_rate_LP = FEC_7_8; 732 break; 733 } 734 735 switch ((buf[1] >> 2) & 3) { 736 case 0: 737 c->bandwidth_hz = 6000000; 738 break; 739 case 1: 740 c->bandwidth_hz = 7000000; 741 break; 742 case 2: 743 c->bandwidth_hz = 8000000; 744 break; 745 } 746 747 return 0; 748 err: 749 dev_dbg(&client->dev, "failed %d\n", ret); 750 return ret; 751 } 752 753 static int af9013_read_status(struct dvb_frontend *fe, enum fe_status *status) 754 { 755 struct af9013_state *state = fe->demodulator_priv; 756 struct i2c_client *client = state->client; 757 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 758 int ret, stmp1; 759 unsigned int utmp, utmp1, utmp2, utmp3, utmp4; 760 u8 buf[2]; 761 762 dev_dbg(&client->dev, "\n"); 763 764 /* 765 * Return status from the cache if it is younger than 2000ms with the 766 * exception of last tune is done during 4000ms. 767 */ 768 if (time_is_after_jiffies(state->read_status_jiffies + msecs_to_jiffies(2000)) && 769 time_is_before_jiffies(state->set_frontend_jiffies + msecs_to_jiffies(4000))) { 770 *status = state->fe_status; 771 } else { 772 /* MPEG2 lock */ 773 ret = regmap_read(state->regmap, 0xd507, &utmp); 774 if (ret) 775 goto err; 776 777 if ((utmp >> 6) & 0x01) { 778 utmp1 = FE_HAS_SIGNAL | FE_HAS_CARRIER | 779 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; 780 } else { 781 /* TPS lock */ 782 ret = regmap_read(state->regmap, 0xd330, &utmp); 783 if (ret) 784 goto err; 785 786 if ((utmp >> 3) & 0x01) 787 utmp1 = FE_HAS_SIGNAL | FE_HAS_CARRIER | 788 FE_HAS_VITERBI; 789 else 790 utmp1 = 0; 791 } 792 793 dev_dbg(&client->dev, "fe_status %02x\n", utmp1); 794 795 state->read_status_jiffies = jiffies; 796 797 state->fe_status = utmp1; 798 *status = utmp1; 799 } 800 801 /* Signal strength */ 802 switch (state->strength_en) { 803 case 0: 804 /* Check if we support signal strength */ 805 ret = regmap_read(state->regmap, 0x9bee, &utmp); 806 if (ret) 807 goto err; 808 809 if ((utmp >> 0) & 0x01) { 810 /* Read agc values for signal strength estimation */ 811 ret = regmap_read(state->regmap, 0x9bbd, &utmp1); 812 if (ret) 813 goto err; 814 ret = regmap_read(state->regmap, 0x9bd0, &utmp2); 815 if (ret) 816 goto err; 817 ret = regmap_read(state->regmap, 0x9be2, &utmp3); 818 if (ret) 819 goto err; 820 ret = regmap_read(state->regmap, 0x9be4, &utmp4); 821 if (ret) 822 goto err; 823 824 state->rf_agc_50 = utmp1; 825 state->rf_agc_80 = utmp2; 826 state->if_agc_50 = utmp3; 827 state->if_agc_80 = utmp4; 828 dev_dbg(&client->dev, 829 "rf_agc_50 %u, rf_agc_80 %u, if_agc_50 %u, if_agc_80 %u\n", 830 utmp1, utmp2, utmp3, utmp4); 831 832 state->strength_en = 1; 833 } else { 834 /* Signal strength is not supported */ 835 state->strength_en = 2; 836 break; 837 } 838 /* Fall through */ 839 case 1: 840 if (time_is_after_jiffies(state->strength_jiffies + msecs_to_jiffies(2000))) 841 break; 842 843 /* Read value */ 844 ret = regmap_bulk_read(state->regmap, 0xd07c, buf, 2); 845 if (ret) 846 goto err; 847 848 /* 849 * Construct line equation from tuner dependent -80/-50 dBm agc 850 * limits and use it to map current agc value to dBm estimate 851 */ 852 #define agc_gain (buf[0] + buf[1]) 853 #define agc_gain_50dbm (state->rf_agc_50 + state->if_agc_50) 854 #define agc_gain_80dbm (state->rf_agc_80 + state->if_agc_80) 855 stmp1 = 30000 * (agc_gain - agc_gain_80dbm) / 856 (agc_gain_50dbm - agc_gain_80dbm) - 80000; 857 858 dev_dbg(&client->dev, 859 "strength %d, agc_gain %d, agc_gain_50dbm %d, agc_gain_80dbm %d\n", 860 stmp1, agc_gain, agc_gain_50dbm, agc_gain_80dbm); 861 862 state->strength_jiffies = jiffies; 863 864 c->strength.stat[0].scale = FE_SCALE_DECIBEL; 865 c->strength.stat[0].svalue = stmp1; 866 break; 867 default: 868 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE; 869 break; 870 } 871 872 return 0; 873 err: 874 dev_dbg(&client->dev, "failed %d\n", ret); 875 return ret; 876 } 877 878 static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr) 879 { 880 struct af9013_state *state = fe->demodulator_priv; 881 *snr = state->snr; 882 return 0; 883 } 884 885 static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength) 886 { 887 struct af9013_state *state = fe->demodulator_priv; 888 *strength = state->signal_strength; 889 return 0; 890 } 891 892 static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber) 893 { 894 struct af9013_state *state = fe->demodulator_priv; 895 *ber = state->ber; 896 return 0; 897 } 898 899 static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) 900 { 901 struct af9013_state *state = fe->demodulator_priv; 902 *ucblocks = state->ucblocks; 903 return 0; 904 } 905 906 static int af9013_init(struct dvb_frontend *fe) 907 { 908 struct af9013_state *state = fe->demodulator_priv; 909 struct i2c_client *client = state->client; 910 int ret, i, len; 911 unsigned int utmp; 912 u8 buf[3]; 913 const struct af9013_reg_bit *init; 914 915 dev_dbg(&client->dev, "\n"); 916 917 /* ADC on */ 918 ret = regmap_update_bits(state->regmap, 0xd73a, 0x08, 0x00); 919 if (ret) 920 goto err; 921 922 /* Clear reset */ 923 ret = regmap_update_bits(state->regmap, 0xd417, 0x02, 0x00); 924 if (ret) 925 goto err; 926 927 /* Disable reset */ 928 ret = regmap_update_bits(state->regmap, 0xd417, 0x10, 0x00); 929 if (ret) 930 goto err; 931 932 /* write API version to firmware */ 933 ret = regmap_bulk_write(state->regmap, 0x9bf2, state->api_version, 4); 934 if (ret) 935 goto err; 936 937 /* program ADC control */ 938 switch (state->clk) { 939 case 28800000: /* 28.800 MHz */ 940 utmp = 0; 941 break; 942 case 20480000: /* 20.480 MHz */ 943 utmp = 1; 944 break; 945 case 28000000: /* 28.000 MHz */ 946 utmp = 2; 947 break; 948 case 25000000: /* 25.000 MHz */ 949 utmp = 3; 950 break; 951 default: 952 ret = -EINVAL; 953 goto err; 954 } 955 956 ret = regmap_update_bits(state->regmap, 0x9bd2, 0x0f, utmp); 957 if (ret) 958 goto err; 959 960 utmp = div_u64((u64)state->clk * 0x80000, 1000000); 961 buf[0] = (utmp >> 0) & 0xff; 962 buf[1] = (utmp >> 8) & 0xff; 963 buf[2] = (utmp >> 16) & 0xff; 964 ret = regmap_bulk_write(state->regmap, 0xd180, buf, 3); 965 if (ret) 966 goto err; 967 968 /* load OFSM settings */ 969 dev_dbg(&client->dev, "load ofsm settings\n"); 970 len = ARRAY_SIZE(ofsm_init); 971 init = ofsm_init; 972 for (i = 0; i < len; i++) { 973 u16 reg = init[i].addr; 974 u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos); 975 u8 val = init[i].val << init[i].pos; 976 977 ret = regmap_update_bits(state->regmap, reg, mask, val); 978 if (ret) 979 goto err; 980 } 981 982 /* load tuner specific settings */ 983 dev_dbg(&client->dev, "load tuner specific settings\n"); 984 switch (state->tuner) { 985 case AF9013_TUNER_MXL5003D: 986 len = ARRAY_SIZE(tuner_init_mxl5003d); 987 init = tuner_init_mxl5003d; 988 break; 989 case AF9013_TUNER_MXL5005D: 990 case AF9013_TUNER_MXL5005R: 991 case AF9013_TUNER_MXL5007T: 992 len = ARRAY_SIZE(tuner_init_mxl5005); 993 init = tuner_init_mxl5005; 994 break; 995 case AF9013_TUNER_ENV77H11D5: 996 len = ARRAY_SIZE(tuner_init_env77h11d5); 997 init = tuner_init_env77h11d5; 998 break; 999 case AF9013_TUNER_MT2060: 1000 len = ARRAY_SIZE(tuner_init_mt2060); 1001 init = tuner_init_mt2060; 1002 break; 1003 case AF9013_TUNER_MC44S803: 1004 len = ARRAY_SIZE(tuner_init_mc44s803); 1005 init = tuner_init_mc44s803; 1006 break; 1007 case AF9013_TUNER_QT1010: 1008 case AF9013_TUNER_QT1010A: 1009 len = ARRAY_SIZE(tuner_init_qt1010); 1010 init = tuner_init_qt1010; 1011 break; 1012 case AF9013_TUNER_MT2060_2: 1013 len = ARRAY_SIZE(tuner_init_mt2060_2); 1014 init = tuner_init_mt2060_2; 1015 break; 1016 case AF9013_TUNER_TDA18271: 1017 case AF9013_TUNER_TDA18218: 1018 len = ARRAY_SIZE(tuner_init_tda18271); 1019 init = tuner_init_tda18271; 1020 break; 1021 case AF9013_TUNER_UNKNOWN: 1022 default: 1023 len = ARRAY_SIZE(tuner_init_unknown); 1024 init = tuner_init_unknown; 1025 break; 1026 } 1027 1028 for (i = 0; i < len; i++) { 1029 u16 reg = init[i].addr; 1030 u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos); 1031 u8 val = init[i].val << init[i].pos; 1032 1033 ret = regmap_update_bits(state->regmap, reg, mask, val); 1034 if (ret) 1035 goto err; 1036 } 1037 1038 /* TS interface */ 1039 if (state->ts_output_pin == 7) 1040 utmp = 1 << 3 | state->ts_mode << 1; 1041 else 1042 utmp = 0 << 3 | state->ts_mode << 1; 1043 ret = regmap_update_bits(state->regmap, 0xd500, 0x0e, utmp); 1044 if (ret) 1045 goto err; 1046 1047 /* enable lock led */ 1048 ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x01); 1049 if (ret) 1050 goto err; 1051 1052 /* check if we support signal strength */ 1053 if (!state->signal_strength_en) { 1054 ret = regmap_read(state->regmap, 0x9bee, &utmp); 1055 if (ret) 1056 goto err; 1057 1058 state->signal_strength_en = (utmp >> 0) & 0x01; 1059 } 1060 1061 /* read values needed for signal strength calculation */ 1062 if (state->signal_strength_en && !state->rf_50) { 1063 ret = regmap_bulk_read(state->regmap, 0x9bbd, &state->rf_50, 1); 1064 if (ret) 1065 goto err; 1066 ret = regmap_bulk_read(state->regmap, 0x9bd0, &state->rf_80, 1); 1067 if (ret) 1068 goto err; 1069 ret = regmap_bulk_read(state->regmap, 0x9be2, &state->if_50, 1); 1070 if (ret) 1071 goto err; 1072 ret = regmap_bulk_read(state->regmap, 0x9be4, &state->if_80, 1); 1073 if (ret) 1074 goto err; 1075 } 1076 1077 /* SNR */ 1078 ret = regmap_write(state->regmap, 0xd2e2, 0x01); 1079 if (ret) 1080 goto err; 1081 1082 /* BER / UCB */ 1083 buf[0] = (10000 >> 0) & 0xff; 1084 buf[1] = (10000 >> 8) & 0xff; 1085 ret = regmap_bulk_write(state->regmap, 0xd385, buf, 2); 1086 if (ret) 1087 goto err; 1088 1089 /* enable FEC monitor */ 1090 ret = regmap_update_bits(state->regmap, 0xd392, 0x02, 0x02); 1091 if (ret) 1092 goto err; 1093 1094 state->first_tune = true; 1095 schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400)); 1096 1097 return 0; 1098 err: 1099 dev_dbg(&client->dev, "failed %d\n", ret); 1100 return ret; 1101 } 1102 1103 static int af9013_sleep(struct dvb_frontend *fe) 1104 { 1105 struct af9013_state *state = fe->demodulator_priv; 1106 struct i2c_client *client = state->client; 1107 int ret; 1108 unsigned int utmp; 1109 1110 dev_dbg(&client->dev, "\n"); 1111 1112 /* stop statistics polling */ 1113 cancel_delayed_work_sync(&state->statistics_work); 1114 1115 /* disable lock led */ 1116 ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x00); 1117 if (ret) 1118 goto err; 1119 1120 /* Enable reset */ 1121 ret = regmap_update_bits(state->regmap, 0xd417, 0x10, 0x10); 1122 if (ret) 1123 goto err; 1124 1125 /* Start reset execution */ 1126 ret = regmap_write(state->regmap, 0xaeff, 0x01); 1127 if (ret) 1128 goto err; 1129 1130 /* Wait reset performs */ 1131 ret = regmap_read_poll_timeout(state->regmap, 0xd417, utmp, 1132 (utmp >> 1) & 0x01, 5000, 1000000); 1133 if (ret) 1134 goto err; 1135 1136 if (!((utmp >> 1) & 0x01)) { 1137 ret = -ETIMEDOUT; 1138 goto err; 1139 } 1140 1141 /* ADC off */ 1142 ret = regmap_update_bits(state->regmap, 0xd73a, 0x08, 0x08); 1143 if (ret) 1144 goto err; 1145 1146 return 0; 1147 err: 1148 dev_dbg(&client->dev, "failed %d\n", ret); 1149 return ret; 1150 } 1151 1152 static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) 1153 { 1154 int ret; 1155 struct af9013_state *state = fe->demodulator_priv; 1156 struct i2c_client *client = state->client; 1157 1158 dev_dbg(&client->dev, "enable %d\n", enable); 1159 1160 /* gate already open or close */ 1161 if (state->i2c_gate_state == enable) 1162 return 0; 1163 1164 if (state->ts_mode == AF9013_TS_MODE_USB) 1165 ret = regmap_update_bits(state->regmap, 0xd417, 0x08, 1166 enable << 3); 1167 else 1168 ret = regmap_update_bits(state->regmap, 0xd607, 0x04, 1169 enable << 2); 1170 if (ret) 1171 goto err; 1172 1173 state->i2c_gate_state = enable; 1174 1175 return 0; 1176 err: 1177 dev_dbg(&client->dev, "failed %d\n", ret); 1178 return ret; 1179 } 1180 1181 static void af9013_release(struct dvb_frontend *fe) 1182 { 1183 struct af9013_state *state = fe->demodulator_priv; 1184 struct i2c_client *client = state->client; 1185 1186 dev_dbg(&client->dev, "\n"); 1187 1188 i2c_unregister_device(client); 1189 } 1190 1191 static const struct dvb_frontend_ops af9013_ops; 1192 1193 static int af9013_download_firmware(struct af9013_state *state) 1194 { 1195 struct i2c_client *client = state->client; 1196 int ret, i, len, rem; 1197 unsigned int utmp; 1198 u8 buf[4]; 1199 u16 checksum = 0; 1200 const struct firmware *firmware; 1201 const char *name = AF9013_FIRMWARE; 1202 1203 dev_dbg(&client->dev, "\n"); 1204 1205 /* Check whether firmware is already running */ 1206 ret = regmap_read(state->regmap, 0x98be, &utmp); 1207 if (ret) 1208 goto err; 1209 1210 dev_dbg(&client->dev, "firmware status %02x\n", utmp); 1211 1212 if (utmp == 0x0c) 1213 return 0; 1214 1215 dev_info(&client->dev, "found a '%s' in cold state, will try to load a firmware\n", 1216 af9013_ops.info.name); 1217 1218 /* Request the firmware, will block and timeout */ 1219 ret = request_firmware(&firmware, name, &client->dev); 1220 if (ret) { 1221 dev_info(&client->dev, "firmware file '%s' not found %d\n", 1222 name, ret); 1223 goto err; 1224 } 1225 1226 dev_info(&client->dev, "downloading firmware from file '%s'\n", 1227 name); 1228 1229 /* Write firmware checksum & size */ 1230 for (i = 0; i < firmware->size; i++) 1231 checksum += firmware->data[i]; 1232 1233 buf[0] = (checksum >> 8) & 0xff; 1234 buf[1] = (checksum >> 0) & 0xff; 1235 buf[2] = (firmware->size >> 8) & 0xff; 1236 buf[3] = (firmware->size >> 0) & 0xff; 1237 ret = regmap_bulk_write(state->regmap, 0x50fc, buf, 4); 1238 if (ret) 1239 goto err_release_firmware; 1240 1241 /* Download firmware */ 1242 #define LEN_MAX 16 1243 for (rem = firmware->size; rem > 0; rem -= LEN_MAX) { 1244 len = min(LEN_MAX, rem); 1245 ret = regmap_bulk_write(state->regmap, 1246 0x5100 + firmware->size - rem, 1247 &firmware->data[firmware->size - rem], 1248 len); 1249 if (ret) { 1250 dev_err(&client->dev, "firmware download failed %d\n", 1251 ret); 1252 goto err_release_firmware; 1253 } 1254 } 1255 1256 release_firmware(firmware); 1257 1258 /* Boot firmware */ 1259 ret = regmap_write(state->regmap, 0xe205, 0x01); 1260 if (ret) 1261 goto err; 1262 1263 /* Check firmware status. 0c=OK, 04=fail */ 1264 ret = regmap_read_poll_timeout(state->regmap, 0x98be, utmp, 1265 (utmp == 0x0c || utmp == 0x04), 1266 5000, 1000000); 1267 if (ret) 1268 goto err; 1269 1270 dev_dbg(&client->dev, "firmware status %02x\n", utmp); 1271 1272 if (utmp == 0x04) { 1273 ret = -ENODEV; 1274 dev_err(&client->dev, "firmware did not run\n"); 1275 goto err; 1276 } else if (utmp != 0x0c) { 1277 ret = -ENODEV; 1278 dev_err(&client->dev, "firmware boot timeout\n"); 1279 goto err; 1280 } 1281 1282 dev_info(&client->dev, "found a '%s' in warm state\n", 1283 af9013_ops.info.name); 1284 1285 return 0; 1286 err_release_firmware: 1287 release_firmware(firmware); 1288 err: 1289 dev_dbg(&client->dev, "failed %d\n", ret); 1290 return ret; 1291 } 1292 1293 /* 1294 * XXX: That is wrapper to af9013_probe() via driver core in order to provide 1295 * proper I2C client for legacy media attach binding. 1296 * New users must use I2C client binding directly! 1297 */ 1298 struct dvb_frontend *af9013_attach(const struct af9013_config *config, 1299 struct i2c_adapter *i2c) 1300 { 1301 struct i2c_client *client; 1302 struct i2c_board_info board_info; 1303 struct af9013_platform_data pdata; 1304 1305 pdata.clk = config->clock; 1306 pdata.tuner = config->tuner; 1307 pdata.if_frequency = config->if_frequency; 1308 pdata.ts_mode = config->ts_mode; 1309 pdata.ts_output_pin = 7; 1310 pdata.spec_inv = config->spec_inv; 1311 memcpy(&pdata.api_version, config->api_version, sizeof(pdata.api_version)); 1312 memcpy(&pdata.gpio, config->gpio, sizeof(pdata.gpio)); 1313 pdata.attach_in_use = true; 1314 1315 memset(&board_info, 0, sizeof(board_info)); 1316 strlcpy(board_info.type, "af9013", sizeof(board_info.type)); 1317 board_info.addr = config->i2c_addr; 1318 board_info.platform_data = &pdata; 1319 client = i2c_new_device(i2c, &board_info); 1320 if (!client || !client->dev.driver) 1321 return NULL; 1322 1323 return pdata.get_dvb_frontend(client); 1324 } 1325 EXPORT_SYMBOL(af9013_attach); 1326 1327 static const struct dvb_frontend_ops af9013_ops = { 1328 .delsys = { SYS_DVBT }, 1329 .info = { 1330 .name = "Afatech AF9013", 1331 .frequency_min = 174000000, 1332 .frequency_max = 862000000, 1333 .frequency_stepsize = 250000, 1334 .frequency_tolerance = 0, 1335 .caps = FE_CAN_FEC_1_2 | 1336 FE_CAN_FEC_2_3 | 1337 FE_CAN_FEC_3_4 | 1338 FE_CAN_FEC_5_6 | 1339 FE_CAN_FEC_7_8 | 1340 FE_CAN_FEC_AUTO | 1341 FE_CAN_QPSK | 1342 FE_CAN_QAM_16 | 1343 FE_CAN_QAM_64 | 1344 FE_CAN_QAM_AUTO | 1345 FE_CAN_TRANSMISSION_MODE_AUTO | 1346 FE_CAN_GUARD_INTERVAL_AUTO | 1347 FE_CAN_HIERARCHY_AUTO | 1348 FE_CAN_RECOVER | 1349 FE_CAN_MUTE_TS 1350 }, 1351 1352 .release = af9013_release, 1353 1354 .init = af9013_init, 1355 .sleep = af9013_sleep, 1356 1357 .get_tune_settings = af9013_get_tune_settings, 1358 .set_frontend = af9013_set_frontend, 1359 .get_frontend = af9013_get_frontend, 1360 1361 .read_status = af9013_read_status, 1362 .read_snr = af9013_read_snr, 1363 .read_signal_strength = af9013_read_signal_strength, 1364 .read_ber = af9013_read_ber, 1365 .read_ucblocks = af9013_read_ucblocks, 1366 1367 .i2c_gate_ctrl = af9013_i2c_gate_ctrl, 1368 }; 1369 1370 static struct dvb_frontend *af9013_get_dvb_frontend(struct i2c_client *client) 1371 { 1372 struct af9013_state *state = i2c_get_clientdata(client); 1373 1374 dev_dbg(&client->dev, "\n"); 1375 1376 return &state->fe; 1377 } 1378 1379 /* Own I2C access routines needed for regmap as chip uses extra command byte */ 1380 static int af9013_wregs(struct i2c_client *client, u8 cmd, u16 reg, 1381 const u8 *val, int len) 1382 { 1383 int ret; 1384 u8 buf[21]; 1385 struct i2c_msg msg[1] = { 1386 { 1387 .addr = client->addr, 1388 .flags = 0, 1389 .len = 3 + len, 1390 .buf = buf, 1391 } 1392 }; 1393 1394 if (3 + len > sizeof(buf)) { 1395 ret = -EINVAL; 1396 goto err; 1397 } 1398 1399 buf[0] = (reg >> 8) & 0xff; 1400 buf[1] = (reg >> 0) & 0xff; 1401 buf[2] = cmd; 1402 memcpy(&buf[3], val, len); 1403 ret = i2c_transfer(client->adapter, msg, 1); 1404 if (ret < 0) { 1405 goto err; 1406 } else if (ret != 1) { 1407 ret = -EREMOTEIO; 1408 goto err; 1409 } 1410 1411 return 0; 1412 err: 1413 dev_dbg(&client->dev, "failed %d\n", ret); 1414 return ret; 1415 } 1416 1417 static int af9013_rregs(struct i2c_client *client, u8 cmd, u16 reg, 1418 u8 *val, int len) 1419 { 1420 int ret; 1421 u8 buf[3]; 1422 struct i2c_msg msg[2] = { 1423 { 1424 .addr = client->addr, 1425 .flags = 0, 1426 .len = 3, 1427 .buf = buf, 1428 }, { 1429 .addr = client->addr, 1430 .flags = I2C_M_RD, 1431 .len = len, 1432 .buf = val, 1433 } 1434 }; 1435 1436 buf[0] = (reg >> 8) & 0xff; 1437 buf[1] = (reg >> 0) & 0xff; 1438 buf[2] = cmd; 1439 ret = i2c_transfer(client->adapter, msg, 2); 1440 if (ret < 0) { 1441 goto err; 1442 } else if (ret != 2) { 1443 ret = -EREMOTEIO; 1444 goto err; 1445 } 1446 1447 return 0; 1448 err: 1449 dev_dbg(&client->dev, "failed %d\n", ret); 1450 return ret; 1451 } 1452 1453 static int af9013_regmap_write(void *context, const void *data, size_t count) 1454 { 1455 struct i2c_client *client = context; 1456 struct af9013_state *state = i2c_get_clientdata(client); 1457 int ret, i; 1458 u8 cmd; 1459 u16 reg = ((u8 *)data)[0] << 8|((u8 *)data)[1] << 0; 1460 u8 *val = &((u8 *)data)[2]; 1461 const unsigned int len = count - 2; 1462 1463 if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) { 1464 cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|1 << 0; 1465 ret = af9013_wregs(client, cmd, reg, val, len); 1466 if (ret) 1467 goto err; 1468 } else if (reg >= 0x5100 && reg < 0x8fff) { 1469 /* Firmware download */ 1470 cmd = 1 << 7|1 << 6|(len - 1) << 2|1 << 1|1 << 0; 1471 ret = af9013_wregs(client, cmd, reg, val, len); 1472 if (ret) 1473 goto err; 1474 } else { 1475 cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|1 << 0; 1476 for (i = 0; i < len; i++) { 1477 ret = af9013_wregs(client, cmd, reg + i, val + i, 1); 1478 if (ret) 1479 goto err; 1480 } 1481 } 1482 1483 return 0; 1484 err: 1485 dev_dbg(&client->dev, "failed %d\n", ret); 1486 return ret; 1487 } 1488 1489 static int af9013_regmap_read(void *context, const void *reg_buf, 1490 size_t reg_size, void *val_buf, size_t val_size) 1491 { 1492 struct i2c_client *client = context; 1493 struct af9013_state *state = i2c_get_clientdata(client); 1494 int ret, i; 1495 u8 cmd; 1496 u16 reg = ((u8 *)reg_buf)[0] << 8|((u8 *)reg_buf)[1] << 0; 1497 u8 *val = &((u8 *)val_buf)[0]; 1498 const unsigned int len = val_size; 1499 1500 if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) { 1501 cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|0 << 0; 1502 ret = af9013_rregs(client, cmd, reg, val_buf, len); 1503 if (ret) 1504 goto err; 1505 } else { 1506 cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|0 << 0; 1507 for (i = 0; i < len; i++) { 1508 ret = af9013_rregs(client, cmd, reg + i, val + i, 1); 1509 if (ret) 1510 goto err; 1511 } 1512 } 1513 1514 return 0; 1515 err: 1516 dev_dbg(&client->dev, "failed %d\n", ret); 1517 return ret; 1518 } 1519 1520 static int af9013_probe(struct i2c_client *client, 1521 const struct i2c_device_id *id) 1522 { 1523 struct af9013_state *state; 1524 struct af9013_platform_data *pdata = client->dev.platform_data; 1525 struct dtv_frontend_properties *c; 1526 int ret, i; 1527 u8 firmware_version[4]; 1528 static const struct regmap_bus regmap_bus = { 1529 .read = af9013_regmap_read, 1530 .write = af9013_regmap_write, 1531 }; 1532 static const struct regmap_config regmap_config = { 1533 .reg_bits = 16, 1534 .val_bits = 8, 1535 }; 1536 1537 state = kzalloc(sizeof(*state), GFP_KERNEL); 1538 if (!state) { 1539 ret = -ENOMEM; 1540 goto err; 1541 } 1542 1543 /* Setup the state */ 1544 state->client = client; 1545 i2c_set_clientdata(client, state); 1546 state->clk = pdata->clk; 1547 state->tuner = pdata->tuner; 1548 state->if_frequency = pdata->if_frequency; 1549 state->ts_mode = pdata->ts_mode; 1550 state->ts_output_pin = pdata->ts_output_pin; 1551 state->spec_inv = pdata->spec_inv; 1552 memcpy(&state->api_version, pdata->api_version, sizeof(state->api_version)); 1553 memcpy(&state->gpio, pdata->gpio, sizeof(state->gpio)); 1554 INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work); 1555 state->regmap = regmap_init(&client->dev, ®map_bus, client, 1556 ®map_config); 1557 if (IS_ERR(state->regmap)) { 1558 ret = PTR_ERR(state->regmap); 1559 goto err_kfree; 1560 } 1561 1562 /* Download firmware */ 1563 if (state->ts_mode != AF9013_TS_MODE_USB) { 1564 ret = af9013_download_firmware(state); 1565 if (ret) 1566 goto err_regmap_exit; 1567 } 1568 1569 /* Firmware version */ 1570 ret = regmap_bulk_read(state->regmap, 0x5103, firmware_version, 1571 sizeof(firmware_version)); 1572 if (ret) 1573 goto err_regmap_exit; 1574 1575 /* Set GPIOs */ 1576 for (i = 0; i < sizeof(state->gpio); i++) { 1577 ret = af9013_set_gpio(state, i, state->gpio[i]); 1578 if (ret) 1579 goto err_regmap_exit; 1580 } 1581 1582 /* Create dvb frontend */ 1583 memcpy(&state->fe.ops, &af9013_ops, sizeof(state->fe.ops)); 1584 if (!pdata->attach_in_use) 1585 state->fe.ops.release = NULL; 1586 state->fe.demodulator_priv = state; 1587 1588 /* Setup callbacks */ 1589 pdata->get_dvb_frontend = af9013_get_dvb_frontend; 1590 1591 /* Init stats to indicate which stats are supported */ 1592 c = &state->fe.dtv_property_cache; 1593 c->strength.len = 1; 1594 c->cnr.len = 1; 1595 1596 dev_info(&client->dev, "Afatech AF9013 successfully attached\n"); 1597 dev_info(&client->dev, "firmware version: %d.%d.%d.%d\n", 1598 firmware_version[0], firmware_version[1], 1599 firmware_version[2], firmware_version[3]); 1600 return 0; 1601 err_regmap_exit: 1602 regmap_exit(state->regmap); 1603 err_kfree: 1604 kfree(state); 1605 err: 1606 dev_dbg(&client->dev, "failed %d\n", ret); 1607 return ret; 1608 } 1609 1610 static int af9013_remove(struct i2c_client *client) 1611 { 1612 struct af9013_state *state = i2c_get_clientdata(client); 1613 1614 dev_dbg(&client->dev, "\n"); 1615 1616 /* Stop statistics polling */ 1617 cancel_delayed_work_sync(&state->statistics_work); 1618 1619 regmap_exit(state->regmap); 1620 1621 kfree(state); 1622 1623 return 0; 1624 } 1625 1626 static const struct i2c_device_id af9013_id_table[] = { 1627 {"af9013", 0}, 1628 {} 1629 }; 1630 MODULE_DEVICE_TABLE(i2c, af9013_id_table); 1631 1632 static struct i2c_driver af9013_driver = { 1633 .driver = { 1634 .name = "af9013", 1635 .suppress_bind_attrs = true, 1636 }, 1637 .probe = af9013_probe, 1638 .remove = af9013_remove, 1639 .id_table = af9013_id_table, 1640 }; 1641 1642 module_i2c_driver(af9013_driver); 1643 1644 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1645 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver"); 1646 MODULE_LICENSE("GPL"); 1647 MODULE_FIRMWARE(AF9013_FIRMWARE); 1648