Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / dvb-frontends / af9013.c
blobb8f3ebfc3e272af4979cca68a10f0eb993127785
1 /*
2 * Afatech AF9013 demodulator driver
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
7 * Thanks to Afatech who kindly provided information.
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.
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.
21 #include "af9013_priv.h"
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];
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;
52 static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
54 struct i2c_client *client = state->client;
55 int ret;
56 u8 pos;
57 u16 addr;
59 dev_dbg(&client->dev, "gpio %u, gpioval %02x\n", gpio, gpioval);
62 * GPIO0 & GPIO1 0xd735
63 * GPIO2 & GPIO3 0xd736
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;
76 default:
77 ret = -EINVAL;
78 goto err;
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;
93 ret = regmap_update_bits(state->regmap, addr, 0x0f << pos,
94 gpioval << pos);
95 if (ret)
96 goto err;
98 return 0;
99 err:
100 dev_dbg(&client->dev, "failed %d\n", ret);
101 return ret;
104 static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe)
106 struct af9013_state *state = fe->demodulator_priv;
107 struct i2c_client *client = state->client;
108 int ret;
110 dev_dbg(&client->dev, "\n");
112 /* reset and start BER counter */
113 ret = regmap_update_bits(state->regmap, 0xd391, 0x10, 0x10);
114 if (ret)
115 goto err;
117 return 0;
118 err:
119 dev_dbg(&client->dev, "failed %d\n", ret);
120 return ret;
123 static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe)
125 struct af9013_state *state = fe->demodulator_priv;
126 struct i2c_client *client = state->client;
127 int ret;
128 unsigned int utmp;
129 u8 buf[5];
131 dev_dbg(&client->dev, "\n");
133 /* check if error bit count is ready */
134 ret = regmap_read(state->regmap, 0xd391, &utmp);
135 if (ret)
136 goto err;
138 if (!((utmp >> 4) & 0x01)) {
139 dev_dbg(&client->dev, "not ready\n");
140 return 0;
143 ret = regmap_bulk_read(state->regmap, 0xd387, buf, 5);
144 if (ret)
145 goto err;
147 state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0];
148 state->ucblocks += (buf[4] << 8) | buf[3];
150 return 0;
151 err:
152 dev_dbg(&client->dev, "failed %d\n", ret);
153 return ret;
156 static int af9013_statistics_snr_start(struct dvb_frontend *fe)
158 struct af9013_state *state = fe->demodulator_priv;
159 struct i2c_client *client = state->client;
160 int ret;
162 dev_dbg(&client->dev, "\n");
164 /* start SNR meas */
165 ret = regmap_update_bits(state->regmap, 0xd2e1, 0x08, 0x08);
166 if (ret)
167 goto err;
169 return 0;
170 err:
171 dev_dbg(&client->dev, "failed %d\n", ret);
172 return ret;
175 static int af9013_statistics_snr_result(struct dvb_frontend *fe)
177 struct af9013_state *state = fe->demodulator_priv;
178 struct i2c_client *client = state->client;
179 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
180 int ret, i, len;
181 unsigned int utmp;
182 u8 buf[3];
183 u32 snr_val;
184 const struct af9013_snr *uninitialized_var(snr_lut);
186 dev_dbg(&client->dev, "\n");
188 /* check if SNR ready */
189 ret = regmap_read(state->regmap, 0xd2e1, &utmp);
190 if (ret)
191 goto err;
193 if (!((utmp >> 3) & 0x01)) {
194 dev_dbg(&client->dev, "not ready\n");
195 return 0;
198 /* read value */
199 ret = regmap_bulk_read(state->regmap, 0xd2e3, buf, 3);
200 if (ret)
201 goto err;
203 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
205 /* read current modulation */
206 ret = regmap_read(state->regmap, 0xd3c1, &utmp);
207 if (ret)
208 goto err;
210 switch ((utmp >> 6) & 3) {
211 case 0:
212 len = ARRAY_SIZE(qpsk_snr_lut);
213 snr_lut = qpsk_snr_lut;
214 break;
215 case 1:
216 len = ARRAY_SIZE(qam16_snr_lut);
217 snr_lut = qam16_snr_lut;
218 break;
219 case 2:
220 len = ARRAY_SIZE(qam64_snr_lut);
221 snr_lut = qam64_snr_lut;
222 break;
223 default:
224 goto err;
227 for (i = 0; i < len; i++) {
228 utmp = snr_lut[i].snr;
230 if (snr_val < snr_lut[i].val)
231 break;
233 state->snr = utmp * 10; /* dB/10 */
235 c->cnr.stat[0].svalue = 1000 * utmp;
236 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
238 return 0;
239 err:
240 dev_dbg(&client->dev, "failed %d\n", ret);
241 return ret;
244 static int af9013_statistics_signal_strength(struct dvb_frontend *fe)
246 struct af9013_state *state = fe->demodulator_priv;
247 struct i2c_client *client = state->client;
248 int ret = 0;
249 u8 buf[2], rf_gain, if_gain;
250 int signal_strength;
252 dev_dbg(&client->dev, "\n");
254 if (!state->signal_strength_en)
255 return 0;
257 ret = regmap_bulk_read(state->regmap, 0xd07c, buf, 2);
258 if (ret)
259 goto err;
261 rf_gain = buf[0];
262 if_gain = buf[1];
264 signal_strength = (0xffff / \
265 (9 * (state->rf_50 + state->if_50) - \
266 11 * (state->rf_80 + state->if_80))) * \
267 (10 * (rf_gain + if_gain) - \
268 11 * (state->rf_80 + state->if_80));
269 if (signal_strength < 0)
270 signal_strength = 0;
271 else if (signal_strength > 0xffff)
272 signal_strength = 0xffff;
274 state->signal_strength = signal_strength;
276 return 0;
277 err:
278 dev_dbg(&client->dev, "failed %d\n", ret);
279 return ret;
282 static void af9013_statistics_work(struct work_struct *work)
284 struct af9013_state *state = container_of(work,
285 struct af9013_state, statistics_work.work);
286 unsigned int next_msec;
288 /* update only signal strength when demod is not locked */
289 if (!(state->fe_status & FE_HAS_LOCK)) {
290 state->statistics_step = 0;
291 state->ber = 0;
292 state->snr = 0;
295 switch (state->statistics_step) {
296 default:
297 state->statistics_step = 0;
298 /* fall-through */
299 case 0:
300 af9013_statistics_signal_strength(&state->fe);
301 state->statistics_step++;
302 next_msec = 300;
303 break;
304 case 1:
305 af9013_statistics_snr_start(&state->fe);
306 state->statistics_step++;
307 next_msec = 200;
308 break;
309 case 2:
310 af9013_statistics_ber_unc_start(&state->fe);
311 state->statistics_step++;
312 next_msec = 1000;
313 break;
314 case 3:
315 af9013_statistics_snr_result(&state->fe);
316 state->statistics_step++;
317 next_msec = 400;
318 break;
319 case 4:
320 af9013_statistics_ber_unc_result(&state->fe);
321 state->statistics_step++;
322 next_msec = 100;
323 break;
326 schedule_delayed_work(&state->statistics_work,
327 msecs_to_jiffies(next_msec));
330 static int af9013_get_tune_settings(struct dvb_frontend *fe,
331 struct dvb_frontend_tune_settings *fesettings)
333 fesettings->min_delay_ms = 800;
334 fesettings->step_size = 0;
335 fesettings->max_drift = 0;
337 return 0;
340 static int af9013_set_frontend(struct dvb_frontend *fe)
342 struct af9013_state *state = fe->demodulator_priv;
343 struct i2c_client *client = state->client;
344 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
345 int ret, i, sampling_freq;
346 bool auto_mode, spec_inv;
347 u8 buf[6];
348 u32 if_frequency, freq_cw;
350 dev_dbg(&client->dev, "frequency %u, bandwidth_hz %u\n",
351 c->frequency, c->bandwidth_hz);
353 /* program tuner */
354 if (fe->ops.tuner_ops.set_params) {
355 ret = fe->ops.tuner_ops.set_params(fe);
356 if (ret)
357 goto err;
360 /* program CFOE coefficients */
361 if (c->bandwidth_hz != state->bandwidth_hz) {
362 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
363 if (coeff_lut[i].clock == state->clk &&
364 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
365 break;
369 /* Return an error if can't find bandwidth or the right clock */
370 if (i == ARRAY_SIZE(coeff_lut)) {
371 ret = -EINVAL;
372 goto err;
375 ret = regmap_bulk_write(state->regmap, 0xae00, coeff_lut[i].val,
376 sizeof(coeff_lut[i].val));
377 if (ret)
378 goto err;
381 /* program frequency control */
382 if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) {
383 /* get used IF frequency */
384 if (fe->ops.tuner_ops.get_if_frequency) {
385 ret = fe->ops.tuner_ops.get_if_frequency(fe,
386 &if_frequency);
387 if (ret)
388 goto err;
389 } else {
390 if_frequency = state->if_frequency;
393 dev_dbg(&client->dev, "if_frequency %u\n", if_frequency);
395 sampling_freq = if_frequency;
397 while (sampling_freq > (state->clk / 2))
398 sampling_freq -= state->clk;
400 if (sampling_freq < 0) {
401 sampling_freq *= -1;
402 spec_inv = state->spec_inv;
403 } else {
404 spec_inv = !state->spec_inv;
407 freq_cw = DIV_ROUND_CLOSEST_ULL((u64)sampling_freq * 0x800000,
408 state->clk);
410 if (spec_inv)
411 freq_cw = 0x800000 - freq_cw;
413 buf[0] = (freq_cw >> 0) & 0xff;
414 buf[1] = (freq_cw >> 8) & 0xff;
415 buf[2] = (freq_cw >> 16) & 0x7f;
417 freq_cw = 0x800000 - freq_cw;
419 buf[3] = (freq_cw >> 0) & 0xff;
420 buf[4] = (freq_cw >> 8) & 0xff;
421 buf[5] = (freq_cw >> 16) & 0x7f;
423 ret = regmap_bulk_write(state->regmap, 0xd140, buf, 3);
424 if (ret)
425 goto err;
427 ret = regmap_bulk_write(state->regmap, 0x9be7, buf, 6);
428 if (ret)
429 goto err;
432 /* clear TPS lock flag */
433 ret = regmap_update_bits(state->regmap, 0xd330, 0x08, 0x08);
434 if (ret)
435 goto err;
437 /* clear MPEG2 lock flag */
438 ret = regmap_update_bits(state->regmap, 0xd507, 0x40, 0x00);
439 if (ret)
440 goto err;
442 /* empty channel function */
443 ret = regmap_update_bits(state->regmap, 0x9bfe, 0x01, 0x00);
444 if (ret)
445 goto err;
447 /* empty DVB-T channel function */
448 ret = regmap_update_bits(state->regmap, 0x9bc2, 0x01, 0x00);
449 if (ret)
450 goto err;
452 /* transmission parameters */
453 auto_mode = false;
454 memset(buf, 0, 3);
456 switch (c->transmission_mode) {
457 case TRANSMISSION_MODE_AUTO:
458 auto_mode = true;
459 break;
460 case TRANSMISSION_MODE_2K:
461 break;
462 case TRANSMISSION_MODE_8K:
463 buf[0] |= (1 << 0);
464 break;
465 default:
466 dev_dbg(&client->dev, "invalid transmission_mode\n");
467 auto_mode = true;
470 switch (c->guard_interval) {
471 case GUARD_INTERVAL_AUTO:
472 auto_mode = true;
473 break;
474 case GUARD_INTERVAL_1_32:
475 break;
476 case GUARD_INTERVAL_1_16:
477 buf[0] |= (1 << 2);
478 break;
479 case GUARD_INTERVAL_1_8:
480 buf[0] |= (2 << 2);
481 break;
482 case GUARD_INTERVAL_1_4:
483 buf[0] |= (3 << 2);
484 break;
485 default:
486 dev_dbg(&client->dev, "invalid guard_interval\n");
487 auto_mode = true;
490 switch (c->hierarchy) {
491 case HIERARCHY_AUTO:
492 auto_mode = true;
493 break;
494 case HIERARCHY_NONE:
495 break;
496 case HIERARCHY_1:
497 buf[0] |= (1 << 4);
498 break;
499 case HIERARCHY_2:
500 buf[0] |= (2 << 4);
501 break;
502 case HIERARCHY_4:
503 buf[0] |= (3 << 4);
504 break;
505 default:
506 dev_dbg(&client->dev, "invalid hierarchy\n");
507 auto_mode = true;
510 switch (c->modulation) {
511 case QAM_AUTO:
512 auto_mode = true;
513 break;
514 case QPSK:
515 break;
516 case QAM_16:
517 buf[1] |= (1 << 6);
518 break;
519 case QAM_64:
520 buf[1] |= (2 << 6);
521 break;
522 default:
523 dev_dbg(&client->dev, "invalid modulation\n");
524 auto_mode = true;
527 /* Use HP. How and which case we can switch to LP? */
528 buf[1] |= (1 << 4);
530 switch (c->code_rate_HP) {
531 case FEC_AUTO:
532 auto_mode = true;
533 break;
534 case FEC_1_2:
535 break;
536 case FEC_2_3:
537 buf[2] |= (1 << 0);
538 break;
539 case FEC_3_4:
540 buf[2] |= (2 << 0);
541 break;
542 case FEC_5_6:
543 buf[2] |= (3 << 0);
544 break;
545 case FEC_7_8:
546 buf[2] |= (4 << 0);
547 break;
548 default:
549 dev_dbg(&client->dev, "invalid code_rate_HP\n");
550 auto_mode = true;
553 switch (c->code_rate_LP) {
554 case FEC_AUTO:
555 auto_mode = true;
556 break;
557 case FEC_1_2:
558 break;
559 case FEC_2_3:
560 buf[2] |= (1 << 3);
561 break;
562 case FEC_3_4:
563 buf[2] |= (2 << 3);
564 break;
565 case FEC_5_6:
566 buf[2] |= (3 << 3);
567 break;
568 case FEC_7_8:
569 buf[2] |= (4 << 3);
570 break;
571 case FEC_NONE:
572 break;
573 default:
574 dev_dbg(&client->dev, "invalid code_rate_LP\n");
575 auto_mode = true;
578 switch (c->bandwidth_hz) {
579 case 6000000:
580 break;
581 case 7000000:
582 buf[1] |= (1 << 2);
583 break;
584 case 8000000:
585 buf[1] |= (2 << 2);
586 break;
587 default:
588 dev_dbg(&client->dev, "invalid bandwidth_hz\n");
589 ret = -EINVAL;
590 goto err;
593 ret = regmap_bulk_write(state->regmap, 0xd3c0, buf, 3);
594 if (ret)
595 goto err;
597 if (auto_mode) {
598 /* clear easy mode flag */
599 ret = regmap_write(state->regmap, 0xaefd, 0x00);
600 if (ret)
601 goto err;
603 dev_dbg(&client->dev, "auto params\n");
604 } else {
605 /* set easy mode flag */
606 ret = regmap_write(state->regmap, 0xaefd, 0x01);
607 if (ret)
608 goto err;
610 ret = regmap_write(state->regmap, 0xaefe, 0x00);
611 if (ret)
612 goto err;
614 dev_dbg(&client->dev, "manual params\n");
617 /* Reset FSM */
618 ret = regmap_write(state->regmap, 0xffff, 0x00);
619 if (ret)
620 goto err;
622 state->bandwidth_hz = c->bandwidth_hz;
623 state->set_frontend_jiffies = jiffies;
624 state->first_tune = false;
626 return 0;
627 err:
628 dev_dbg(&client->dev, "failed %d\n", ret);
629 return ret;
632 static int af9013_get_frontend(struct dvb_frontend *fe,
633 struct dtv_frontend_properties *c)
635 struct af9013_state *state = fe->demodulator_priv;
636 struct i2c_client *client = state->client;
637 int ret;
638 u8 buf[3];
640 dev_dbg(&client->dev, "\n");
642 ret = regmap_bulk_read(state->regmap, 0xd3c0, buf, 3);
643 if (ret)
644 goto err;
646 switch ((buf[1] >> 6) & 3) {
647 case 0:
648 c->modulation = QPSK;
649 break;
650 case 1:
651 c->modulation = QAM_16;
652 break;
653 case 2:
654 c->modulation = QAM_64;
655 break;
658 switch ((buf[0] >> 0) & 3) {
659 case 0:
660 c->transmission_mode = TRANSMISSION_MODE_2K;
661 break;
662 case 1:
663 c->transmission_mode = TRANSMISSION_MODE_8K;
666 switch ((buf[0] >> 2) & 3) {
667 case 0:
668 c->guard_interval = GUARD_INTERVAL_1_32;
669 break;
670 case 1:
671 c->guard_interval = GUARD_INTERVAL_1_16;
672 break;
673 case 2:
674 c->guard_interval = GUARD_INTERVAL_1_8;
675 break;
676 case 3:
677 c->guard_interval = GUARD_INTERVAL_1_4;
678 break;
681 switch ((buf[0] >> 4) & 7) {
682 case 0:
683 c->hierarchy = HIERARCHY_NONE;
684 break;
685 case 1:
686 c->hierarchy = HIERARCHY_1;
687 break;
688 case 2:
689 c->hierarchy = HIERARCHY_2;
690 break;
691 case 3:
692 c->hierarchy = HIERARCHY_4;
693 break;
696 switch ((buf[2] >> 0) & 7) {
697 case 0:
698 c->code_rate_HP = FEC_1_2;
699 break;
700 case 1:
701 c->code_rate_HP = FEC_2_3;
702 break;
703 case 2:
704 c->code_rate_HP = FEC_3_4;
705 break;
706 case 3:
707 c->code_rate_HP = FEC_5_6;
708 break;
709 case 4:
710 c->code_rate_HP = FEC_7_8;
711 break;
714 switch ((buf[2] >> 3) & 7) {
715 case 0:
716 c->code_rate_LP = FEC_1_2;
717 break;
718 case 1:
719 c->code_rate_LP = FEC_2_3;
720 break;
721 case 2:
722 c->code_rate_LP = FEC_3_4;
723 break;
724 case 3:
725 c->code_rate_LP = FEC_5_6;
726 break;
727 case 4:
728 c->code_rate_LP = FEC_7_8;
729 break;
732 switch ((buf[1] >> 2) & 3) {
733 case 0:
734 c->bandwidth_hz = 6000000;
735 break;
736 case 1:
737 c->bandwidth_hz = 7000000;
738 break;
739 case 2:
740 c->bandwidth_hz = 8000000;
741 break;
744 return 0;
745 err:
746 dev_dbg(&client->dev, "failed %d\n", ret);
747 return ret;
750 static int af9013_read_status(struct dvb_frontend *fe, enum fe_status *status)
752 struct af9013_state *state = fe->demodulator_priv;
753 struct i2c_client *client = state->client;
754 int ret;
755 unsigned int utmp;
758 * Return status from the cache if it is younger than 2000ms with the
759 * exception of last tune is done during 4000ms.
761 if (time_is_after_jiffies(
762 state->read_status_jiffies + msecs_to_jiffies(2000)) &&
763 time_is_before_jiffies(
764 state->set_frontend_jiffies + msecs_to_jiffies(4000))
766 *status = state->fe_status;
767 return 0;
768 } else {
769 *status = 0;
772 /* MPEG2 lock */
773 ret = regmap_read(state->regmap, 0xd507, &utmp);
774 if (ret)
775 goto err;
777 if ((utmp >> 6) & 0x01)
778 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
779 FE_HAS_SYNC | FE_HAS_LOCK;
781 if (!*status) {
782 /* TPS lock */
783 ret = regmap_read(state->regmap, 0xd330, &utmp);
784 if (ret)
785 goto err;
787 if ((utmp >> 3) & 0x01)
788 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
789 FE_HAS_VITERBI;
792 state->fe_status = *status;
793 state->read_status_jiffies = jiffies;
795 return 0;
796 err:
797 dev_dbg(&client->dev, "failed %d\n", ret);
798 return ret;
801 static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
803 struct af9013_state *state = fe->demodulator_priv;
804 *snr = state->snr;
805 return 0;
808 static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
810 struct af9013_state *state = fe->demodulator_priv;
811 *strength = state->signal_strength;
812 return 0;
815 static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
817 struct af9013_state *state = fe->demodulator_priv;
818 *ber = state->ber;
819 return 0;
822 static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
824 struct af9013_state *state = fe->demodulator_priv;
825 *ucblocks = state->ucblocks;
826 return 0;
829 static int af9013_init(struct dvb_frontend *fe)
831 struct af9013_state *state = fe->demodulator_priv;
832 struct i2c_client *client = state->client;
833 int ret, i, len;
834 unsigned int utmp;
835 u8 buf[3];
836 const struct af9013_reg_bit *init;
838 dev_dbg(&client->dev, "\n");
840 /* ADC on */
841 ret = regmap_update_bits(state->regmap, 0xd73a, 0x08, 0x00);
842 if (ret)
843 goto err;
845 /* Clear reset */
846 ret = regmap_update_bits(state->regmap, 0xd417, 0x02, 0x00);
847 if (ret)
848 goto err;
850 /* Disable reset */
851 ret = regmap_update_bits(state->regmap, 0xd417, 0x10, 0x00);
852 if (ret)
853 goto err;
855 /* write API version to firmware */
856 ret = regmap_bulk_write(state->regmap, 0x9bf2, state->api_version, 4);
857 if (ret)
858 goto err;
860 /* program ADC control */
861 switch (state->clk) {
862 case 28800000: /* 28.800 MHz */
863 utmp = 0;
864 break;
865 case 20480000: /* 20.480 MHz */
866 utmp = 1;
867 break;
868 case 28000000: /* 28.000 MHz */
869 utmp = 2;
870 break;
871 case 25000000: /* 25.000 MHz */
872 utmp = 3;
873 break;
874 default:
875 ret = -EINVAL;
876 goto err;
879 ret = regmap_update_bits(state->regmap, 0x9bd2, 0x0f, utmp);
880 if (ret)
881 goto err;
883 utmp = div_u64((u64)state->clk * 0x80000, 1000000);
884 buf[0] = (utmp >> 0) & 0xff;
885 buf[1] = (utmp >> 8) & 0xff;
886 buf[2] = (utmp >> 16) & 0xff;
887 ret = regmap_bulk_write(state->regmap, 0xd180, buf, 3);
888 if (ret)
889 goto err;
891 /* load OFSM settings */
892 dev_dbg(&client->dev, "load ofsm settings\n");
893 len = ARRAY_SIZE(ofsm_init);
894 init = ofsm_init;
895 for (i = 0; i < len; i++) {
896 u16 reg = init[i].addr;
897 u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos);
898 u8 val = init[i].val << init[i].pos;
900 ret = regmap_update_bits(state->regmap, reg, mask, val);
901 if (ret)
902 goto err;
905 /* load tuner specific settings */
906 dev_dbg(&client->dev, "load tuner specific settings\n");
907 switch (state->tuner) {
908 case AF9013_TUNER_MXL5003D:
909 len = ARRAY_SIZE(tuner_init_mxl5003d);
910 init = tuner_init_mxl5003d;
911 break;
912 case AF9013_TUNER_MXL5005D:
913 case AF9013_TUNER_MXL5005R:
914 case AF9013_TUNER_MXL5007T:
915 len = ARRAY_SIZE(tuner_init_mxl5005);
916 init = tuner_init_mxl5005;
917 break;
918 case AF9013_TUNER_ENV77H11D5:
919 len = ARRAY_SIZE(tuner_init_env77h11d5);
920 init = tuner_init_env77h11d5;
921 break;
922 case AF9013_TUNER_MT2060:
923 len = ARRAY_SIZE(tuner_init_mt2060);
924 init = tuner_init_mt2060;
925 break;
926 case AF9013_TUNER_MC44S803:
927 len = ARRAY_SIZE(tuner_init_mc44s803);
928 init = tuner_init_mc44s803;
929 break;
930 case AF9013_TUNER_QT1010:
931 case AF9013_TUNER_QT1010A:
932 len = ARRAY_SIZE(tuner_init_qt1010);
933 init = tuner_init_qt1010;
934 break;
935 case AF9013_TUNER_MT2060_2:
936 len = ARRAY_SIZE(tuner_init_mt2060_2);
937 init = tuner_init_mt2060_2;
938 break;
939 case AF9013_TUNER_TDA18271:
940 case AF9013_TUNER_TDA18218:
941 len = ARRAY_SIZE(tuner_init_tda18271);
942 init = tuner_init_tda18271;
943 break;
944 case AF9013_TUNER_UNKNOWN:
945 default:
946 len = ARRAY_SIZE(tuner_init_unknown);
947 init = tuner_init_unknown;
948 break;
951 for (i = 0; i < len; i++) {
952 u16 reg = init[i].addr;
953 u8 mask = GENMASK(init[i].pos + init[i].len - 1, init[i].pos);
954 u8 val = init[i].val << init[i].pos;
956 ret = regmap_update_bits(state->regmap, reg, mask, val);
957 if (ret)
958 goto err;
961 /* TS interface */
962 if (state->ts_output_pin == 7)
963 utmp = 1 << 3 | state->ts_mode << 1;
964 else
965 utmp = 0 << 3 | state->ts_mode << 1;
966 ret = regmap_update_bits(state->regmap, 0xd500, 0x0e, utmp);
967 if (ret)
968 goto err;
970 /* enable lock led */
971 ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x01);
972 if (ret)
973 goto err;
975 /* check if we support signal strength */
976 if (!state->signal_strength_en) {
977 ret = regmap_read(state->regmap, 0x9bee, &utmp);
978 if (ret)
979 goto err;
981 state->signal_strength_en = (utmp >> 0) & 0x01;
984 /* read values needed for signal strength calculation */
985 if (state->signal_strength_en && !state->rf_50) {
986 ret = regmap_bulk_read(state->regmap, 0x9bbd, &state->rf_50, 1);
987 if (ret)
988 goto err;
989 ret = regmap_bulk_read(state->regmap, 0x9bd0, &state->rf_80, 1);
990 if (ret)
991 goto err;
992 ret = regmap_bulk_read(state->regmap, 0x9be2, &state->if_50, 1);
993 if (ret)
994 goto err;
995 ret = regmap_bulk_read(state->regmap, 0x9be4, &state->if_80, 1);
996 if (ret)
997 goto err;
1000 /* SNR */
1001 ret = regmap_write(state->regmap, 0xd2e2, 0x01);
1002 if (ret)
1003 goto err;
1005 /* BER / UCB */
1006 buf[0] = (10000 >> 0) & 0xff;
1007 buf[1] = (10000 >> 8) & 0xff;
1008 ret = regmap_bulk_write(state->regmap, 0xd385, buf, 2);
1009 if (ret)
1010 goto err;
1012 /* enable FEC monitor */
1013 ret = regmap_update_bits(state->regmap, 0xd392, 0x02, 0x02);
1014 if (ret)
1015 goto err;
1017 state->first_tune = true;
1018 schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400));
1020 return 0;
1021 err:
1022 dev_dbg(&client->dev, "failed %d\n", ret);
1023 return ret;
1026 static int af9013_sleep(struct dvb_frontend *fe)
1028 struct af9013_state *state = fe->demodulator_priv;
1029 struct i2c_client *client = state->client;
1030 int ret;
1031 unsigned int utmp;
1033 dev_dbg(&client->dev, "\n");
1035 /* stop statistics polling */
1036 cancel_delayed_work_sync(&state->statistics_work);
1038 /* disable lock led */
1039 ret = regmap_update_bits(state->regmap, 0xd730, 0x01, 0x00);
1040 if (ret)
1041 goto err;
1043 /* Enable reset */
1044 ret = regmap_update_bits(state->regmap, 0xd417, 0x10, 0x10);
1045 if (ret)
1046 goto err;
1048 /* Start reset execution */
1049 ret = regmap_write(state->regmap, 0xaeff, 0x01);
1050 if (ret)
1051 goto err;
1053 /* Wait reset performs */
1054 ret = regmap_read_poll_timeout(state->regmap, 0xd417, utmp,
1055 (utmp >> 1) & 0x01, 5000, 1000000);
1056 if (ret)
1057 goto err;
1059 if (!((utmp >> 1) & 0x01)) {
1060 ret = -ETIMEDOUT;
1061 goto err;
1064 /* ADC off */
1065 ret = regmap_update_bits(state->regmap, 0xd73a, 0x08, 0x08);
1066 if (ret)
1067 goto err;
1069 return 0;
1070 err:
1071 dev_dbg(&client->dev, "failed %d\n", ret);
1072 return ret;
1075 static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1077 int ret;
1078 struct af9013_state *state = fe->demodulator_priv;
1079 struct i2c_client *client = state->client;
1081 dev_dbg(&client->dev, "enable %d\n", enable);
1083 /* gate already open or close */
1084 if (state->i2c_gate_state == enable)
1085 return 0;
1087 if (state->ts_mode == AF9013_TS_MODE_USB)
1088 ret = regmap_update_bits(state->regmap, 0xd417, 0x08,
1089 enable << 3);
1090 else
1091 ret = regmap_update_bits(state->regmap, 0xd607, 0x04,
1092 enable << 2);
1093 if (ret)
1094 goto err;
1096 state->i2c_gate_state = enable;
1098 return 0;
1099 err:
1100 dev_dbg(&client->dev, "failed %d\n", ret);
1101 return ret;
1104 static void af9013_release(struct dvb_frontend *fe)
1106 struct af9013_state *state = fe->demodulator_priv;
1107 struct i2c_client *client = state->client;
1109 dev_dbg(&client->dev, "\n");
1111 i2c_unregister_device(client);
1114 static const struct dvb_frontend_ops af9013_ops;
1116 static int af9013_download_firmware(struct af9013_state *state)
1118 struct i2c_client *client = state->client;
1119 int ret, i, len, rem;
1120 unsigned int utmp;
1121 u8 buf[4];
1122 u16 checksum = 0;
1123 const struct firmware *firmware;
1124 const char *name = AF9013_FIRMWARE;
1126 dev_dbg(&client->dev, "\n");
1128 /* Check whether firmware is already running */
1129 ret = regmap_read(state->regmap, 0x98be, &utmp);
1130 if (ret)
1131 goto err;
1133 dev_dbg(&client->dev, "firmware status %02x\n", utmp);
1135 if (utmp == 0x0c)
1136 return 0;
1138 dev_info(&client->dev, "found a '%s' in cold state, will try to load a firmware\n",
1139 af9013_ops.info.name);
1141 /* Request the firmware, will block and timeout */
1142 ret = request_firmware(&firmware, name, &client->dev);
1143 if (ret) {
1144 dev_info(&client->dev, "firmware file '%s' not found %d\n",
1145 name, ret);
1146 goto err;
1149 dev_info(&client->dev, "downloading firmware from file '%s'\n",
1150 name);
1152 /* Write firmware checksum & size */
1153 for (i = 0; i < firmware->size; i++)
1154 checksum += firmware->data[i];
1156 buf[0] = (checksum >> 8) & 0xff;
1157 buf[1] = (checksum >> 0) & 0xff;
1158 buf[2] = (firmware->size >> 8) & 0xff;
1159 buf[3] = (firmware->size >> 0) & 0xff;
1160 ret = regmap_bulk_write(state->regmap, 0x50fc, buf, 4);
1161 if (ret)
1162 goto err_release_firmware;
1164 /* Download firmware */
1165 #define LEN_MAX 16
1166 for (rem = firmware->size; rem > 0; rem -= LEN_MAX) {
1167 len = min(LEN_MAX, rem);
1168 ret = regmap_bulk_write(state->regmap,
1169 0x5100 + firmware->size - rem,
1170 &firmware->data[firmware->size - rem],
1171 len);
1172 if (ret) {
1173 dev_err(&client->dev, "firmware download failed %d\n",
1174 ret);
1175 goto err_release_firmware;
1179 release_firmware(firmware);
1181 /* Boot firmware */
1182 ret = regmap_write(state->regmap, 0xe205, 0x01);
1183 if (ret)
1184 goto err;
1186 /* Check firmware status. 0c=OK, 04=fail */
1187 ret = regmap_read_poll_timeout(state->regmap, 0x98be, utmp,
1188 (utmp == 0x0c || utmp == 0x04),
1189 5000, 1000000);
1190 if (ret)
1191 goto err;
1193 dev_dbg(&client->dev, "firmware status %02x\n", utmp);
1195 if (utmp == 0x04) {
1196 ret = -ENODEV;
1197 dev_err(&client->dev, "firmware did not run\n");
1198 goto err;
1199 } else if (utmp != 0x0c) {
1200 ret = -ENODEV;
1201 dev_err(&client->dev, "firmware boot timeout\n");
1202 goto err;
1205 dev_info(&client->dev, "found a '%s' in warm state\n",
1206 af9013_ops.info.name);
1208 return 0;
1209 err_release_firmware:
1210 release_firmware(firmware);
1211 err:
1212 dev_dbg(&client->dev, "failed %d\n", ret);
1213 return ret;
1217 * XXX: That is wrapper to af9013_probe() via driver core in order to provide
1218 * proper I2C client for legacy media attach binding.
1219 * New users must use I2C client binding directly!
1221 struct dvb_frontend *af9013_attach(const struct af9013_config *config,
1222 struct i2c_adapter *i2c)
1224 struct i2c_client *client;
1225 struct i2c_board_info board_info;
1226 struct af9013_platform_data pdata;
1228 pdata.clk = config->clock;
1229 pdata.tuner = config->tuner;
1230 pdata.if_frequency = config->if_frequency;
1231 pdata.ts_mode = config->ts_mode;
1232 pdata.ts_output_pin = 7;
1233 pdata.spec_inv = config->spec_inv;
1234 memcpy(&pdata.api_version, config->api_version, sizeof(pdata.api_version));
1235 memcpy(&pdata.gpio, config->gpio, sizeof(pdata.gpio));
1236 pdata.attach_in_use = true;
1238 memset(&board_info, 0, sizeof(board_info));
1239 strlcpy(board_info.type, "af9013", sizeof(board_info.type));
1240 board_info.addr = config->i2c_addr;
1241 board_info.platform_data = &pdata;
1242 client = i2c_new_device(i2c, &board_info);
1243 if (!client || !client->dev.driver)
1244 return NULL;
1246 return pdata.get_dvb_frontend(client);
1248 EXPORT_SYMBOL(af9013_attach);
1250 static const struct dvb_frontend_ops af9013_ops = {
1251 .delsys = { SYS_DVBT },
1252 .info = {
1253 .name = "Afatech AF9013",
1254 .frequency_min = 174000000,
1255 .frequency_max = 862000000,
1256 .frequency_stepsize = 250000,
1257 .frequency_tolerance = 0,
1258 .caps = FE_CAN_FEC_1_2 |
1259 FE_CAN_FEC_2_3 |
1260 FE_CAN_FEC_3_4 |
1261 FE_CAN_FEC_5_6 |
1262 FE_CAN_FEC_7_8 |
1263 FE_CAN_FEC_AUTO |
1264 FE_CAN_QPSK |
1265 FE_CAN_QAM_16 |
1266 FE_CAN_QAM_64 |
1267 FE_CAN_QAM_AUTO |
1268 FE_CAN_TRANSMISSION_MODE_AUTO |
1269 FE_CAN_GUARD_INTERVAL_AUTO |
1270 FE_CAN_HIERARCHY_AUTO |
1271 FE_CAN_RECOVER |
1272 FE_CAN_MUTE_TS
1275 .release = af9013_release,
1277 .init = af9013_init,
1278 .sleep = af9013_sleep,
1280 .get_tune_settings = af9013_get_tune_settings,
1281 .set_frontend = af9013_set_frontend,
1282 .get_frontend = af9013_get_frontend,
1284 .read_status = af9013_read_status,
1285 .read_snr = af9013_read_snr,
1286 .read_signal_strength = af9013_read_signal_strength,
1287 .read_ber = af9013_read_ber,
1288 .read_ucblocks = af9013_read_ucblocks,
1290 .i2c_gate_ctrl = af9013_i2c_gate_ctrl,
1293 static struct dvb_frontend *af9013_get_dvb_frontend(struct i2c_client *client)
1295 struct af9013_state *state = i2c_get_clientdata(client);
1297 dev_dbg(&client->dev, "\n");
1299 return &state->fe;
1302 /* Own I2C access routines needed for regmap as chip uses extra command byte */
1303 static int af9013_wregs(struct i2c_client *client, u8 cmd, u16 reg,
1304 const u8 *val, int len)
1306 int ret;
1307 u8 buf[21];
1308 struct i2c_msg msg[1] = {
1310 .addr = client->addr,
1311 .flags = 0,
1312 .len = 3 + len,
1313 .buf = buf,
1317 if (3 + len > sizeof(buf)) {
1318 ret = -EINVAL;
1319 goto err;
1322 buf[0] = (reg >> 8) & 0xff;
1323 buf[1] = (reg >> 0) & 0xff;
1324 buf[2] = cmd;
1325 memcpy(&buf[3], val, len);
1326 ret = i2c_transfer(client->adapter, msg, 1);
1327 if (ret < 0) {
1328 goto err;
1329 } else if (ret != 1) {
1330 ret = -EREMOTEIO;
1331 goto err;
1334 return 0;
1335 err:
1336 dev_dbg(&client->dev, "failed %d\n", ret);
1337 return ret;
1340 static int af9013_rregs(struct i2c_client *client, u8 cmd, u16 reg,
1341 u8 *val, int len)
1343 int ret;
1344 u8 buf[3];
1345 struct i2c_msg msg[2] = {
1347 .addr = client->addr,
1348 .flags = 0,
1349 .len = 3,
1350 .buf = buf,
1351 }, {
1352 .addr = client->addr,
1353 .flags = I2C_M_RD,
1354 .len = len,
1355 .buf = val,
1359 buf[0] = (reg >> 8) & 0xff;
1360 buf[1] = (reg >> 0) & 0xff;
1361 buf[2] = cmd;
1362 ret = i2c_transfer(client->adapter, msg, 2);
1363 if (ret < 0) {
1364 goto err;
1365 } else if (ret != 2) {
1366 ret = -EREMOTEIO;
1367 goto err;
1370 return 0;
1371 err:
1372 dev_dbg(&client->dev, "failed %d\n", ret);
1373 return ret;
1376 static int af9013_regmap_write(void *context, const void *data, size_t count)
1378 struct i2c_client *client = context;
1379 struct af9013_state *state = i2c_get_clientdata(client);
1380 int ret, i;
1381 u8 cmd;
1382 u16 reg = ((u8 *)data)[0] << 8|((u8 *)data)[1] << 0;
1383 u8 *val = &((u8 *)data)[2];
1384 const unsigned int len = count - 2;
1386 if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) {
1387 cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|1 << 0;
1388 ret = af9013_wregs(client, cmd, reg, val, len);
1389 if (ret)
1390 goto err;
1391 } else if (reg >= 0x5100 && reg < 0x8fff) {
1392 /* Firmware download */
1393 cmd = 1 << 7|1 << 6|(len - 1) << 2|1 << 1|1 << 0;
1394 ret = af9013_wregs(client, cmd, reg, val, len);
1395 if (ret)
1396 goto err;
1397 } else {
1398 cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|1 << 0;
1399 for (i = 0; i < len; i++) {
1400 ret = af9013_wregs(client, cmd, reg + i, val + i, 1);
1401 if (ret)
1402 goto err;
1406 return 0;
1407 err:
1408 dev_dbg(&client->dev, "failed %d\n", ret);
1409 return ret;
1412 static int af9013_regmap_read(void *context, const void *reg_buf,
1413 size_t reg_size, void *val_buf, size_t val_size)
1415 struct i2c_client *client = context;
1416 struct af9013_state *state = i2c_get_clientdata(client);
1417 int ret, i;
1418 u8 cmd;
1419 u16 reg = ((u8 *)reg_buf)[0] << 8|((u8 *)reg_buf)[1] << 0;
1420 u8 *val = &((u8 *)val_buf)[0];
1421 const unsigned int len = val_size;
1423 if (state->ts_mode == AF9013_TS_MODE_USB && (reg & 0xff00) != 0xae00) {
1424 cmd = 0 << 7|0 << 6|(len - 1) << 2|1 << 1|0 << 0;
1425 ret = af9013_rregs(client, cmd, reg, val_buf, len);
1426 if (ret)
1427 goto err;
1428 } else {
1429 cmd = 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|0 << 0;
1430 for (i = 0; i < len; i++) {
1431 ret = af9013_rregs(client, cmd, reg + i, val + i, 1);
1432 if (ret)
1433 goto err;
1437 return 0;
1438 err:
1439 dev_dbg(&client->dev, "failed %d\n", ret);
1440 return ret;
1443 static int af9013_probe(struct i2c_client *client,
1444 const struct i2c_device_id *id)
1446 struct af9013_state *state;
1447 struct af9013_platform_data *pdata = client->dev.platform_data;
1448 struct dtv_frontend_properties *c;
1449 int ret, i;
1450 u8 firmware_version[4];
1451 static const struct regmap_bus regmap_bus = {
1452 .read = af9013_regmap_read,
1453 .write = af9013_regmap_write,
1455 static const struct regmap_config regmap_config = {
1456 .reg_bits = 16,
1457 .val_bits = 8,
1460 state = kzalloc(sizeof(*state), GFP_KERNEL);
1461 if (!state) {
1462 ret = -ENOMEM;
1463 goto err;
1466 /* Setup the state */
1467 state->client = client;
1468 i2c_set_clientdata(client, state);
1469 state->clk = pdata->clk;
1470 state->tuner = pdata->tuner;
1471 state->if_frequency = pdata->if_frequency;
1472 state->ts_mode = pdata->ts_mode;
1473 state->ts_output_pin = pdata->ts_output_pin;
1474 state->spec_inv = pdata->spec_inv;
1475 memcpy(&state->api_version, pdata->api_version, sizeof(state->api_version));
1476 memcpy(&state->gpio, pdata->gpio, sizeof(state->gpio));
1477 INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work);
1478 state->regmap = regmap_init(&client->dev, &regmap_bus, client,
1479 &regmap_config);
1480 if (IS_ERR(state->regmap)) {
1481 ret = PTR_ERR(state->regmap);
1482 goto err_kfree;
1485 /* Download firmware */
1486 if (state->ts_mode != AF9013_TS_MODE_USB) {
1487 ret = af9013_download_firmware(state);
1488 if (ret)
1489 goto err_regmap_exit;
1492 /* Firmware version */
1493 ret = regmap_bulk_read(state->regmap, 0x5103, firmware_version,
1494 sizeof(firmware_version));
1495 if (ret)
1496 goto err_regmap_exit;
1498 /* Set GPIOs */
1499 for (i = 0; i < sizeof(state->gpio); i++) {
1500 ret = af9013_set_gpio(state, i, state->gpio[i]);
1501 if (ret)
1502 goto err_regmap_exit;
1505 /* Create dvb frontend */
1506 memcpy(&state->fe.ops, &af9013_ops, sizeof(state->fe.ops));
1507 if (!pdata->attach_in_use)
1508 state->fe.ops.release = NULL;
1509 state->fe.demodulator_priv = state;
1511 /* Setup callbacks */
1512 pdata->get_dvb_frontend = af9013_get_dvb_frontend;
1514 /* Init stats to indicate which stats are supported */
1515 c = &state->fe.dtv_property_cache;
1516 c->cnr.len = 1;
1518 dev_info(&client->dev, "Afatech AF9013 successfully attached\n");
1519 dev_info(&client->dev, "firmware version: %d.%d.%d.%d\n",
1520 firmware_version[0], firmware_version[1],
1521 firmware_version[2], firmware_version[3]);
1522 return 0;
1523 err_regmap_exit:
1524 regmap_exit(state->regmap);
1525 err_kfree:
1526 kfree(state);
1527 err:
1528 dev_dbg(&client->dev, "failed %d\n", ret);
1529 return ret;
1532 static int af9013_remove(struct i2c_client *client)
1534 struct af9013_state *state = i2c_get_clientdata(client);
1536 dev_dbg(&client->dev, "\n");
1538 /* Stop statistics polling */
1539 cancel_delayed_work_sync(&state->statistics_work);
1541 regmap_exit(state->regmap);
1543 kfree(state);
1545 return 0;
1548 static const struct i2c_device_id af9013_id_table[] = {
1549 {"af9013", 0},
1552 MODULE_DEVICE_TABLE(i2c, af9013_id_table);
1554 static struct i2c_driver af9013_driver = {
1555 .driver = {
1556 .name = "af9013",
1557 .suppress_bind_attrs = true,
1559 .probe = af9013_probe,
1560 .remove = af9013_remove,
1561 .id_table = af9013_id_table,
1564 module_i2c_driver(af9013_driver);
1566 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1567 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1568 MODULE_LICENSE("GPL");
1569 MODULE_FIRMWARE(AF9013_FIRMWARE);