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