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, rem; 1141 unsigned int utmp; 1142 u8 buf[4]; 1143 u16 checksum = 0; 1144 const struct firmware *firmware; 1145 const char *name = AF9013_FIRMWARE; 1146 1147 dev_dbg(&client->dev, "\n"); 1148 1149 /* Check whether firmware is already running */ 1150 ret = regmap_read(state->regmap, 0x98be, &utmp); 1151 if (ret) 1152 goto err; 1153 1154 dev_dbg(&client->dev, "firmware status %02x\n", utmp); 1155 1156 if (utmp == 0x0c) 1157 return 0; 1158 1159 dev_info(&client->dev, "found a '%s' in cold state, will try to load a firmware\n", 1160 af9013_ops.info.name); 1161 1162 /* Request the firmware, will block and timeout */ 1163 ret = request_firmware(&firmware, name, &client->dev); 1164 if (ret) { 1165 dev_info(&client->dev, "firmware file '%s' not found %d\n", 1166 name, ret); 1167 goto err; 1168 } 1169 1170 dev_info(&client->dev, "downloading firmware from file '%s'\n", 1171 name); 1172 1173 /* Write firmware checksum & size */ 1174 for (i = 0; i < firmware->size; i++) 1175 checksum += firmware->data[i]; 1176 1177 buf[0] = (checksum >> 8) & 0xff; 1178 buf[1] = (checksum >> 0) & 0xff; 1179 buf[2] = (firmware->size >> 8) & 0xff; 1180 buf[3] = (firmware->size >> 0) & 0xff; 1181 ret = regmap_bulk_write(state->regmap, 0x50fc, buf, 4); 1182 if (ret) 1183 goto err_release_firmware; 1184 1185 /* Download firmware */ 1186 #define LEN_MAX 16 1187 for (rem = firmware->size; rem > 0; rem -= LEN_MAX) { 1188 len = min(LEN_MAX, rem); 1189 ret = regmap_bulk_write(state->regmap, 1190 0x5100 + firmware->size - rem, 1191 &firmware->data[firmware->size - rem], 1192 len); 1193 if (ret) { 1194 dev_err(&client->dev, "firmware download failed %d\n", 1195 ret); 1196 goto err_release_firmware; 1197 } 1198 } 1199 1200 release_firmware(firmware); 1201 1202 /* Boot firmware */ 1203 ret = regmap_write(state->regmap, 0xe205, 0x01); 1204 if (ret) 1205 goto err; 1206 1207 /* Check firmware status. 0c=OK, 04=fail */ 1208 ret = regmap_read_poll_timeout(state->regmap, 0x98be, utmp, 1209 (utmp == 0x0c || utmp == 0x04), 1210 5000, 1000000); 1211 if (ret) 1212 goto err; 1213 1214 dev_dbg(&client->dev, "firmware status %02x\n", utmp); 1215 1216 if (utmp == 0x04) { 1217 ret = -ENODEV; 1218 dev_err(&client->dev, "firmware did not run\n"); 1219 goto err; 1220 } else if (utmp != 0x0c) { 1221 ret = -ENODEV; 1222 dev_err(&client->dev, "firmware boot timeout\n"); 1223 goto err; 1224 } 1225 1226 dev_info(&client->dev, "found a '%s' in warm state\n", 1227 af9013_ops.info.name); 1228 1229 return 0; 1230 err_release_firmware: 1231 release_firmware(firmware); 1232 err: 1233 dev_dbg(&client->dev, "failed %d\n", ret); 1234 return ret; 1235 } 1236 1237 /* 1238 * XXX: That is wrapper to af9013_probe() via driver core in order to provide 1239 * proper I2C client for legacy media attach binding. 1240 * New users must use I2C client binding directly! 1241 */ 1242 struct dvb_frontend *af9013_attach(const struct af9013_config *config, 1243 struct i2c_adapter *i2c) 1244 { 1245 struct i2c_client *client; 1246 struct i2c_board_info board_info; 1247 struct af9013_platform_data pdata; 1248 1249 pdata.clk = config->clock; 1250 pdata.tuner = config->tuner; 1251 pdata.if_frequency = config->if_frequency; 1252 pdata.ts_mode = config->ts_mode; 1253 pdata.ts_output_pin = 7; 1254 pdata.spec_inv = config->spec_inv; 1255 memcpy(&pdata.api_version, config->api_version, sizeof(pdata.api_version)); 1256 memcpy(&pdata.gpio, config->gpio, sizeof(pdata.gpio)); 1257 pdata.attach_in_use = true; 1258 1259 memset(&board_info, 0, sizeof(board_info)); 1260 strlcpy(board_info.type, "af9013", sizeof(board_info.type)); 1261 board_info.addr = config->i2c_addr; 1262 board_info.platform_data = &pdata; 1263 client = i2c_new_device(i2c, &board_info); 1264 if (!client || !client->dev.driver) 1265 return NULL; 1266 1267 return pdata.get_dvb_frontend(client); 1268 } 1269 EXPORT_SYMBOL(af9013_attach); 1270 1271 static const struct dvb_frontend_ops af9013_ops = { 1272 .delsys = { SYS_DVBT }, 1273 .info = { 1274 .name = "Afatech AF9013", 1275 .frequency_min = 174000000, 1276 .frequency_max = 862000000, 1277 .frequency_stepsize = 250000, 1278 .frequency_tolerance = 0, 1279 .caps = FE_CAN_FEC_1_2 | 1280 FE_CAN_FEC_2_3 | 1281 FE_CAN_FEC_3_4 | 1282 FE_CAN_FEC_5_6 | 1283 FE_CAN_FEC_7_8 | 1284 FE_CAN_FEC_AUTO | 1285 FE_CAN_QPSK | 1286 FE_CAN_QAM_16 | 1287 FE_CAN_QAM_64 | 1288 FE_CAN_QAM_AUTO | 1289 FE_CAN_TRANSMISSION_MODE_AUTO | 1290 FE_CAN_GUARD_INTERVAL_AUTO | 1291 FE_CAN_HIERARCHY_AUTO | 1292 FE_CAN_RECOVER | 1293 FE_CAN_MUTE_TS 1294 }, 1295 1296 .release = af9013_release, 1297 1298 .init = af9013_init, 1299 .sleep = af9013_sleep, 1300 1301 .get_tune_settings = af9013_get_tune_settings, 1302 .set_frontend = af9013_set_frontend, 1303 .get_frontend = af9013_get_frontend, 1304 1305 .read_status = af9013_read_status, 1306 .read_snr = af9013_read_snr, 1307 .read_signal_strength = af9013_read_signal_strength, 1308 .read_ber = af9013_read_ber, 1309 .read_ucblocks = af9013_read_ucblocks, 1310 1311 .i2c_gate_ctrl = af9013_i2c_gate_ctrl, 1312 }; 1313 1314 static struct dvb_frontend *af9013_get_dvb_frontend(struct i2c_client *client) 1315 { 1316 struct af9013_state *state = i2c_get_clientdata(client); 1317 1318 dev_dbg(&client->dev, "\n"); 1319 1320 return &state->fe; 1321 } 1322 1323 /* Own I2C access routines needed for regmap as chip uses extra command byte */ 1324 static int af9013_wregs(struct i2c_client *client, u8 cmd, u16 reg, 1325 const u8 *val, int len) 1326 { 1327 int ret; 1328 u8 buf[21]; 1329 struct i2c_msg msg[1] = { 1330 { 1331 .addr = client->addr, 1332 .flags = 0, 1333 .len = 3 + len, 1334 .buf = buf, 1335 } 1336 }; 1337 1338 if (3 + len > sizeof(buf)) { 1339 ret = -EINVAL; 1340 goto err; 1341 } 1342 1343 buf[0] = (reg >> 8) & 0xff; 1344 buf[1] = (reg >> 0) & 0xff; 1345 buf[2] = cmd; 1346 memcpy(&buf[3], val, len); 1347 ret = i2c_transfer(client->adapter, msg, 1); 1348 if (ret < 0) { 1349 goto err; 1350 } else if (ret != 1) { 1351 ret = -EREMOTEIO; 1352 goto err; 1353 } 1354 1355 return 0; 1356 err: 1357 dev_dbg(&client->dev, "failed %d\n", ret); 1358 return ret; 1359 } 1360 1361 static int af9013_rregs(struct i2c_client *client, u8 cmd, u16 reg, 1362 u8 *val, int len) 1363 { 1364 int ret; 1365 u8 buf[3]; 1366 struct i2c_msg msg[2] = { 1367 { 1368 .addr = client->addr, 1369 .flags = 0, 1370 .len = 3, 1371 .buf = buf, 1372 }, { 1373 .addr = client->addr, 1374 .flags = I2C_M_RD, 1375 .len = len, 1376 .buf = val, 1377 } 1378 }; 1379 1380 buf[0] = (reg >> 8) & 0xff; 1381 buf[1] = (reg >> 0) & 0xff; 1382 buf[2] = cmd; 1383 ret = i2c_transfer(client->adapter, msg, 2); 1384 if (ret < 0) { 1385 goto err; 1386 } else if (ret != 2) { 1387 ret = -EREMOTEIO; 1388 goto err; 1389 } 1390 1391 return 0; 1392 err: 1393 dev_dbg(&client->dev, "failed %d\n", ret); 1394 return ret; 1395 } 1396 1397 static int af9013_regmap_write(void *context, const void *data, size_t count) 1398 { 1399 struct i2c_client *client = context; 1400 struct af9013_state *state = i2c_get_clientdata(client); 1401 int ret, i; 1402 u8 cmd; 1403 u16 reg = ((u8 *)data)[0] << 8|((u8 *)data)[1] << 0; 1404 u8 *val = &((u8 *)data)[2]; 1405 const unsigned int len = count - 2; 1406 1407 if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) { 1408 cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|1 << 0; 1409 ret = af9013_wregs(client, cmd, reg, val, len); 1410 if (ret) 1411 goto err; 1412 } else if (reg >= 0x5100 && reg < 0x8fff) { 1413 /* Firmware download */ 1414 cmd = 1 << 7|1 << 6|(len - 1) << 2|1 << 1|1 << 0; 1415 ret = af9013_wregs(client, cmd, reg, val, len); 1416 if (ret) 1417 goto err; 1418 } else { 1419 cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|1 << 0; 1420 for (i = 0; i < len; i++) { 1421 ret = af9013_wregs(client, cmd, reg + i, val + i, 1); 1422 if (ret) 1423 goto err; 1424 } 1425 } 1426 1427 return 0; 1428 err: 1429 dev_dbg(&client->dev, "failed %d\n", ret); 1430 return ret; 1431 } 1432 1433 static int af9013_regmap_read(void *context, const void *reg_buf, 1434 size_t reg_size, void *val_buf, size_t val_size) 1435 { 1436 struct i2c_client *client = context; 1437 struct af9013_state *state = i2c_get_clientdata(client); 1438 int ret, i; 1439 u8 cmd; 1440 u16 reg = ((u8 *)reg_buf)[0] << 8|((u8 *)reg_buf)[1] << 0; 1441 u8 *val = &((u8 *)val_buf)[0]; 1442 const unsigned int len = val_size; 1443 1444 if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) { 1445 cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|0 << 0; 1446 ret = af9013_rregs(client, cmd, reg, val_buf, len); 1447 if (ret) 1448 goto err; 1449 } else { 1450 cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|0 << 0; 1451 for (i = 0; i < len; i++) { 1452 ret = af9013_rregs(client, cmd, reg + i, val + i, 1); 1453 if (ret) 1454 goto err; 1455 } 1456 } 1457 1458 return 0; 1459 err: 1460 dev_dbg(&client->dev, "failed %d\n", ret); 1461 return ret; 1462 } 1463 1464 static int af9013_probe(struct i2c_client *client, 1465 const struct i2c_device_id *id) 1466 { 1467 struct af9013_state *state; 1468 struct af9013_platform_data *pdata = client->dev.platform_data; 1469 struct dtv_frontend_properties *c; 1470 int ret, i; 1471 u8 firmware_version[4]; 1472 static const struct regmap_bus regmap_bus = { 1473 .read = af9013_regmap_read, 1474 .write = af9013_regmap_write, 1475 }; 1476 static const struct regmap_config regmap_config = { 1477 .reg_bits = 16, 1478 .val_bits = 8, 1479 }; 1480 1481 state = kzalloc(sizeof(*state), GFP_KERNEL); 1482 if (!state) { 1483 ret = -ENOMEM; 1484 goto err; 1485 } 1486 1487 /* Setup the state */ 1488 state->client = client; 1489 i2c_set_clientdata(client, state); 1490 state->clk = pdata->clk; 1491 state->tuner = pdata->tuner; 1492 state->if_frequency = pdata->if_frequency; 1493 state->ts_mode = pdata->ts_mode; 1494 state->ts_output_pin = pdata->ts_output_pin; 1495 state->spec_inv = pdata->spec_inv; 1496 memcpy(&state->api_version, pdata->api_version, sizeof(state->api_version)); 1497 memcpy(&state->gpio, pdata->gpio, sizeof(state->gpio)); 1498 INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work); 1499 state->regmap = regmap_init(&client->dev, ®map_bus, client, 1500 ®map_config); 1501 if (IS_ERR(state->regmap)) { 1502 ret = PTR_ERR(state->regmap); 1503 goto err_kfree; 1504 } 1505 1506 /* Download firmware */ 1507 if (state->ts_mode != AF9013_TS_MODE_USB) { 1508 ret = af9013_download_firmware(state); 1509 if (ret) 1510 goto err_regmap_exit; 1511 } 1512 1513 /* Firmware version */ 1514 ret = regmap_bulk_read(state->regmap, 0x5103, firmware_version, 1515 sizeof(firmware_version)); 1516 if (ret) 1517 goto err_regmap_exit; 1518 1519 /* Set GPIOs */ 1520 for (i = 0; i < sizeof(state->gpio); i++) { 1521 ret = af9013_set_gpio(state, i, state->gpio[i]); 1522 if (ret) 1523 goto err_regmap_exit; 1524 } 1525 1526 /* Create dvb frontend */ 1527 memcpy(&state->fe.ops, &af9013_ops, sizeof(state->fe.ops)); 1528 if (!pdata->attach_in_use) 1529 state->fe.ops.release = NULL; 1530 state->fe.demodulator_priv = state; 1531 1532 /* Setup callbacks */ 1533 pdata->get_dvb_frontend = af9013_get_dvb_frontend; 1534 1535 /* Init stats to indicate which stats are supported */ 1536 c = &state->fe.dtv_property_cache; 1537 c->cnr.len = 1; 1538 1539 dev_info(&client->dev, "Afatech AF9013 successfully attached\n"); 1540 dev_info(&client->dev, "firmware version: %d.%d.%d.%d\n", 1541 firmware_version[0], firmware_version[1], 1542 firmware_version[2], firmware_version[3]); 1543 return 0; 1544 err_regmap_exit: 1545 regmap_exit(state->regmap); 1546 err_kfree: 1547 kfree(state); 1548 err: 1549 dev_dbg(&client->dev, "failed %d\n", ret); 1550 return ret; 1551 } 1552 1553 static int af9013_remove(struct i2c_client *client) 1554 { 1555 struct af9013_state *state = i2c_get_clientdata(client); 1556 1557 dev_dbg(&client->dev, "\n"); 1558 1559 /* Stop statistics polling */ 1560 cancel_delayed_work_sync(&state->statistics_work); 1561 1562 regmap_exit(state->regmap); 1563 1564 kfree(state); 1565 1566 return 0; 1567 } 1568 1569 static const struct i2c_device_id af9013_id_table[] = { 1570 {"af9013", 0}, 1571 {} 1572 }; 1573 MODULE_DEVICE_TABLE(i2c, af9013_id_table); 1574 1575 static struct i2c_driver af9013_driver = { 1576 .driver = { 1577 .name = "af9013", 1578 .suppress_bind_attrs = true, 1579 }, 1580 .probe = af9013_probe, 1581 .remove = af9013_remove, 1582 .id_table = af9013_id_table, 1583 }; 1584 1585 module_i2c_driver(af9013_driver); 1586 1587 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1588 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver"); 1589 MODULE_LICENSE("GPL"); 1590 MODULE_FIRMWARE(AF9013_FIRMWARE); 1591