1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Afatech AF9013 demodulator driver
5 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
6 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
8 * Thanks to Afatech who kindly provided information.
11 #include "af9013_priv.h"
14 struct i2c_client
*client
;
15 struct regmap
*regmap
;
16 struct i2c_mux_core
*muxc
;
17 struct dvb_frontend fe
;
28 enum fe_status fe_status
;
29 /* RF and IF AGC limits used for signal strength calc */
30 u8 strength_en
, rf_agc_50
, rf_agc_80
, if_agc_50
, if_agc_80
;
31 unsigned long set_frontend_jiffies
;
32 unsigned long read_status_jiffies
;
33 unsigned long strength_jiffies
;
34 unsigned long cnr_jiffies
;
35 unsigned long ber_ucb_jiffies
;
43 static int af9013_set_gpio(struct af9013_state
*state
, u8 gpio
, u8 gpioval
)
45 struct i2c_client
*client
= state
->client
;
50 dev_dbg(&client
->dev
, "gpio %u, gpioval %02x\n", gpio
, gpioval
);
53 * GPIO0 & GPIO1 0xd735
54 * GPIO2 & GPIO3 0xd736
84 ret
= regmap_update_bits(state
->regmap
, addr
, 0x0f << pos
,
91 dev_dbg(&client
->dev
, "failed %d\n", ret
);
95 static int af9013_get_tune_settings(struct dvb_frontend
*fe
,
96 struct dvb_frontend_tune_settings
*fesettings
)
98 fesettings
->min_delay_ms
= 800;
99 fesettings
->step_size
= 0;
100 fesettings
->max_drift
= 0;
105 static int af9013_set_frontend(struct dvb_frontend
*fe
)
107 struct af9013_state
*state
= fe
->demodulator_priv
;
108 struct i2c_client
*client
= state
->client
;
109 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
110 int ret
, i
, sampling_freq
;
111 bool auto_mode
, spec_inv
;
113 u32 if_frequency
, freq_cw
;
115 dev_dbg(&client
->dev
, "frequency %u, bandwidth_hz %u\n",
116 c
->frequency
, c
->bandwidth_hz
);
119 if (fe
->ops
.tuner_ops
.set_params
) {
120 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
125 /* program CFOE coefficients */
126 if (c
->bandwidth_hz
!= state
->bandwidth_hz
) {
127 for (i
= 0; i
< ARRAY_SIZE(coeff_lut
); i
++) {
128 if (coeff_lut
[i
].clock
== state
->clk
&&
129 coeff_lut
[i
].bandwidth_hz
== c
->bandwidth_hz
) {
134 /* Return an error if can't find bandwidth or the right clock */
135 if (i
== ARRAY_SIZE(coeff_lut
)) {
140 ret
= regmap_bulk_write(state
->regmap
, 0xae00, coeff_lut
[i
].val
,
141 sizeof(coeff_lut
[i
].val
));
146 /* program frequency control */
147 if (c
->bandwidth_hz
!= state
->bandwidth_hz
|| state
->first_tune
) {
148 /* get used IF frequency */
149 if (fe
->ops
.tuner_ops
.get_if_frequency
) {
150 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
,
155 if_frequency
= state
->if_frequency
;
158 dev_dbg(&client
->dev
, "if_frequency %u\n", if_frequency
);
160 sampling_freq
= if_frequency
;
162 while (sampling_freq
> (state
->clk
/ 2))
163 sampling_freq
-= state
->clk
;
165 if (sampling_freq
< 0) {
167 spec_inv
= state
->spec_inv
;
169 spec_inv
= !state
->spec_inv
;
172 freq_cw
= DIV_ROUND_CLOSEST_ULL((u64
)sampling_freq
* 0x800000,
176 freq_cw
= 0x800000 - freq_cw
;
178 buf
[0] = (freq_cw
>> 0) & 0xff;
179 buf
[1] = (freq_cw
>> 8) & 0xff;
180 buf
[2] = (freq_cw
>> 16) & 0x7f;
182 freq_cw
= 0x800000 - freq_cw
;
184 buf
[3] = (freq_cw
>> 0) & 0xff;
185 buf
[4] = (freq_cw
>> 8) & 0xff;
186 buf
[5] = (freq_cw
>> 16) & 0x7f;
188 ret
= regmap_bulk_write(state
->regmap
, 0xd140, buf
, 3);
192 ret
= regmap_bulk_write(state
->regmap
, 0x9be7, buf
, 6);
197 /* clear TPS lock flag */
198 ret
= regmap_update_bits(state
->regmap
, 0xd330, 0x08, 0x08);
202 /* clear MPEG2 lock flag */
203 ret
= regmap_update_bits(state
->regmap
, 0xd507, 0x40, 0x00);
207 /* empty channel function */
208 ret
= regmap_update_bits(state
->regmap
, 0x9bfe, 0x01, 0x00);
212 /* empty DVB-T channel function */
213 ret
= regmap_update_bits(state
->regmap
, 0x9bc2, 0x01, 0x00);
217 /* transmission parameters */
221 switch (c
->transmission_mode
) {
222 case TRANSMISSION_MODE_AUTO
:
225 case TRANSMISSION_MODE_2K
:
227 case TRANSMISSION_MODE_8K
:
231 dev_dbg(&client
->dev
, "invalid transmission_mode\n");
235 switch (c
->guard_interval
) {
236 case GUARD_INTERVAL_AUTO
:
239 case GUARD_INTERVAL_1_32
:
241 case GUARD_INTERVAL_1_16
:
244 case GUARD_INTERVAL_1_8
:
247 case GUARD_INTERVAL_1_4
:
251 dev_dbg(&client
->dev
, "invalid guard_interval\n");
255 switch (c
->hierarchy
) {
271 dev_dbg(&client
->dev
, "invalid hierarchy\n");
275 switch (c
->modulation
) {
288 dev_dbg(&client
->dev
, "invalid modulation\n");
292 /* Use HP. How and which case we can switch to LP? */
295 switch (c
->code_rate_HP
) {
314 dev_dbg(&client
->dev
, "invalid code_rate_HP\n");
318 switch (c
->code_rate_LP
) {
339 dev_dbg(&client
->dev
, "invalid code_rate_LP\n");
343 switch (c
->bandwidth_hz
) {
353 dev_dbg(&client
->dev
, "invalid bandwidth_hz\n");
358 ret
= regmap_bulk_write(state
->regmap
, 0xd3c0, buf
, 3);
363 /* clear easy mode flag */
364 ret
= regmap_write(state
->regmap
, 0xaefd, 0x00);
368 dev_dbg(&client
->dev
, "auto params\n");
370 /* set easy mode flag */
371 ret
= regmap_write(state
->regmap
, 0xaefd, 0x01);
375 ret
= regmap_write(state
->regmap
, 0xaefe, 0x00);
379 dev_dbg(&client
->dev
, "manual params\n");
383 ret
= regmap_write(state
->regmap
, 0xffff, 0x00);
387 state
->bandwidth_hz
= c
->bandwidth_hz
;
388 state
->set_frontend_jiffies
= jiffies
;
389 state
->first_tune
= false;
393 dev_dbg(&client
->dev
, "failed %d\n", ret
);
397 static int af9013_get_frontend(struct dvb_frontend
*fe
,
398 struct dtv_frontend_properties
*c
)
400 struct af9013_state
*state
= fe
->demodulator_priv
;
401 struct i2c_client
*client
= state
->client
;
405 dev_dbg(&client
->dev
, "\n");
407 ret
= regmap_bulk_read(state
->regmap
, 0xd3c0, buf
, 3);
411 switch ((buf
[1] >> 6) & 3) {
413 c
->modulation
= QPSK
;
416 c
->modulation
= QAM_16
;
419 c
->modulation
= QAM_64
;
423 switch ((buf
[0] >> 0) & 3) {
425 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
428 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
431 switch ((buf
[0] >> 2) & 3) {
433 c
->guard_interval
= GUARD_INTERVAL_1_32
;
436 c
->guard_interval
= GUARD_INTERVAL_1_16
;
439 c
->guard_interval
= GUARD_INTERVAL_1_8
;
442 c
->guard_interval
= GUARD_INTERVAL_1_4
;
446 switch ((buf
[0] >> 4) & 7) {
448 c
->hierarchy
= HIERARCHY_NONE
;
451 c
->hierarchy
= HIERARCHY_1
;
454 c
->hierarchy
= HIERARCHY_2
;
457 c
->hierarchy
= HIERARCHY_4
;
461 switch ((buf
[2] >> 0) & 7) {
463 c
->code_rate_HP
= FEC_1_2
;
466 c
->code_rate_HP
= FEC_2_3
;
469 c
->code_rate_HP
= FEC_3_4
;
472 c
->code_rate_HP
= FEC_5_6
;
475 c
->code_rate_HP
= FEC_7_8
;
479 switch ((buf
[2] >> 3) & 7) {
481 c
->code_rate_LP
= FEC_1_2
;
484 c
->code_rate_LP
= FEC_2_3
;
487 c
->code_rate_LP
= FEC_3_4
;
490 c
->code_rate_LP
= FEC_5_6
;
493 c
->code_rate_LP
= FEC_7_8
;
497 switch ((buf
[1] >> 2) & 3) {
499 c
->bandwidth_hz
= 6000000;
502 c
->bandwidth_hz
= 7000000;
505 c
->bandwidth_hz
= 8000000;
511 dev_dbg(&client
->dev
, "failed %d\n", ret
);
515 static int af9013_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
517 struct af9013_state
*state
= fe
->demodulator_priv
;
518 struct i2c_client
*client
= state
->client
;
519 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
521 unsigned int utmp
, utmp1
, utmp2
, utmp3
, utmp4
;
524 dev_dbg(&client
->dev
, "\n");
527 * Return status from the cache if it is younger than 2000ms with the
528 * exception of last tune is done during 4000ms.
530 if (time_is_after_jiffies(state
->read_status_jiffies
+ msecs_to_jiffies(2000)) &&
531 time_is_before_jiffies(state
->set_frontend_jiffies
+ msecs_to_jiffies(4000))) {
532 *status
= state
->fe_status
;
535 ret
= regmap_read(state
->regmap
, 0xd507, &utmp
);
539 if ((utmp
>> 6) & 0x01) {
540 utmp1
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
541 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
544 ret
= regmap_read(state
->regmap
, 0xd330, &utmp
);
548 if ((utmp
>> 3) & 0x01)
549 utmp1
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
555 dev_dbg(&client
->dev
, "fe_status %02x\n", utmp1
);
557 state
->read_status_jiffies
= jiffies
;
559 state
->fe_status
= utmp1
;
563 /* Signal strength */
564 switch (state
->strength_en
) {
566 /* Check if we support signal strength */
567 ret
= regmap_read(state
->regmap
, 0x9bee, &utmp
);
571 if ((utmp
>> 0) & 0x01) {
572 /* Read agc values for signal strength estimation */
573 ret
= regmap_read(state
->regmap
, 0x9bbd, &utmp1
);
576 ret
= regmap_read(state
->regmap
, 0x9bd0, &utmp2
);
579 ret
= regmap_read(state
->regmap
, 0x9be2, &utmp3
);
582 ret
= regmap_read(state
->regmap
, 0x9be4, &utmp4
);
586 state
->rf_agc_50
= utmp1
;
587 state
->rf_agc_80
= utmp2
;
588 state
->if_agc_50
= utmp3
;
589 state
->if_agc_80
= utmp4
;
590 dev_dbg(&client
->dev
,
591 "rf_agc_50 %u, rf_agc_80 %u, if_agc_50 %u, if_agc_80 %u\n",
592 utmp1
, utmp2
, utmp3
, utmp4
);
594 state
->strength_en
= 1;
596 /* Signal strength is not supported */
597 state
->strength_en
= 2;
602 if (time_is_after_jiffies(state
->strength_jiffies
+ msecs_to_jiffies(2000)))
606 ret
= regmap_bulk_read(state
->regmap
, 0xd07c, buf
, 2);
611 * Construct line equation from tuner dependent -80/-50 dBm agc
612 * limits and use it to map current agc value to dBm estimate
614 #define agc_gain (buf[0] + buf[1])
615 #define agc_gain_50dbm (state->rf_agc_50 + state->if_agc_50)
616 #define agc_gain_80dbm (state->rf_agc_80 + state->if_agc_80)
617 stmp1
= 30000 * (agc_gain
- agc_gain_80dbm
) /
618 (agc_gain_50dbm
- agc_gain_80dbm
) - 80000;
620 dev_dbg(&client
->dev
,
621 "strength %d, agc_gain %d, agc_gain_50dbm %d, agc_gain_80dbm %d\n",
622 stmp1
, agc_gain
, agc_gain_50dbm
, agc_gain_80dbm
);
624 state
->strength_jiffies
= jiffies
;
625 /* Convert [-90, -30] dBm to [0x0000, 0xffff] for dvbv3 */
626 utmp1
= clamp(stmp1
+ 90000, 0, 60000);
627 state
->dvbv3_strength
= div_u64((u64
)utmp1
* 0xffff, 60000);
629 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
630 c
->strength
.stat
[0].svalue
= stmp1
;
633 c
->strength
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
638 switch (state
->fe_status
& FE_HAS_VITERBI
) {
640 if (time_is_after_jiffies(state
->cnr_jiffies
+ msecs_to_jiffies(2000)))
643 /* Check if cnr ready */
644 ret
= regmap_read(state
->regmap
, 0xd2e1, &utmp
);
648 if (!((utmp
>> 3) & 0x01)) {
649 dev_dbg(&client
->dev
, "cnr not ready\n");
654 ret
= regmap_bulk_read(state
->regmap
, 0xd2e3, buf
, 3);
658 utmp1
= buf
[2] << 16 | buf
[1] << 8 | buf
[0] << 0;
660 /* Read current modulation */
661 ret
= regmap_read(state
->regmap
, 0xd3c1, &utmp
);
665 switch ((utmp
>> 6) & 3) {
669 * CNR[dB] 13 * -log10((1690000 - value) / value) + 2.6
670 * value [653799, 1689999], 2.6 / 13 = 3355443
672 utmp1
= clamp(utmp1
, 653799U, 1689999U);
673 utmp1
= ((u64
)(intlog10(utmp1
)
674 - intlog10(1690000 - utmp1
)
675 + 3355443) * 13 * 1000) >> 24;
680 * CNR[dB] 6 * log10((value - 370000) / (828000 - value)) + 15.7
681 * value [371105, 827999], 15.7 / 6 = 43900382
683 utmp1
= clamp(utmp1
, 371105U, 827999U);
684 utmp1
= ((u64
)(intlog10(utmp1
- 370000)
685 - intlog10(828000 - utmp1
)
686 + 43900382) * 6 * 1000) >> 24;
691 * CNR[dB] 8 * log10((value - 193000) / (425000 - value)) + 23.8
692 * value [193246, 424999], 23.8 / 8 = 49912218
694 utmp1
= clamp(utmp1
, 193246U, 424999U);
695 utmp1
= ((u64
)(intlog10(utmp1
- 193000)
696 - intlog10(425000 - utmp1
)
697 + 49912218) * 8 * 1000) >> 24;
700 dev_dbg(&client
->dev
, "invalid modulation %u\n",
706 dev_dbg(&client
->dev
, "cnr %u\n", utmp1
);
708 state
->cnr_jiffies
= jiffies
;
709 state
->dvbv3_snr
= utmp1
/ 100;
711 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
712 c
->cnr
.stat
[0].svalue
= utmp1
;
715 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
720 switch (state
->fe_status
& FE_HAS_SYNC
) {
722 if (time_is_after_jiffies(state
->ber_ucb_jiffies
+ msecs_to_jiffies(2000)))
725 /* Check if ber / ucb is ready */
726 ret
= regmap_read(state
->regmap
, 0xd391, &utmp
);
730 if (!((utmp
>> 4) & 0x01)) {
731 dev_dbg(&client
->dev
, "ber not ready\n");
736 ret
= regmap_bulk_read(state
->regmap
, 0xd385, buf
, 7);
740 utmp1
= buf
[4] << 16 | buf
[3] << 8 | buf
[2] << 0;
741 utmp2
= (buf
[1] << 8 | buf
[0] << 0) * 204 * 8;
742 utmp3
= buf
[6] << 8 | buf
[5] << 0;
743 utmp4
= buf
[1] << 8 | buf
[0] << 0;
745 /* Use 10000 TS packets for measure */
746 if (utmp4
!= 10000) {
747 buf
[0] = (10000 >> 0) & 0xff;
748 buf
[1] = (10000 >> 8) & 0xff;
749 ret
= regmap_bulk_write(state
->regmap
, 0xd385, buf
, 2);
754 /* Reset ber / ucb counter */
755 ret
= regmap_update_bits(state
->regmap
, 0xd391, 0x20, 0x20);
759 dev_dbg(&client
->dev
, "post_bit_error %u, post_bit_count %u\n",
761 dev_dbg(&client
->dev
, "block_error %u, block_count %u\n",
764 state
->ber_ucb_jiffies
= jiffies
;
765 state
->dvbv3_ber
= utmp1
;
766 state
->dvbv3_ucblocks
+= utmp3
;
768 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
769 c
->post_bit_error
.stat
[0].uvalue
+= utmp1
;
770 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
771 c
->post_bit_count
.stat
[0].uvalue
+= utmp2
;
773 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
774 c
->block_error
.stat
[0].uvalue
+= utmp3
;
775 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
776 c
->block_count
.stat
[0].uvalue
+= utmp4
;
779 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
780 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
782 c
->block_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
783 c
->block_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
789 dev_dbg(&client
->dev
, "failed %d\n", ret
);
793 static int af9013_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
795 struct af9013_state
*state
= fe
->demodulator_priv
;
797 *snr
= state
->dvbv3_snr
;
802 static int af9013_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
804 struct af9013_state
*state
= fe
->demodulator_priv
;
806 *strength
= state
->dvbv3_strength
;
811 static int af9013_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
813 struct af9013_state
*state
= fe
->demodulator_priv
;
815 *ber
= state
->dvbv3_ber
;
820 static int af9013_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
822 struct af9013_state
*state
= fe
->demodulator_priv
;
824 *ucblocks
= state
->dvbv3_ucblocks
;
829 static int af9013_init(struct dvb_frontend
*fe
)
831 struct af9013_state
*state
= fe
->demodulator_priv
;
832 struct i2c_client
*client
= state
->client
;
836 const struct af9013_reg_mask_val
*tab
;
838 dev_dbg(&client
->dev
, "\n");
841 ret
= regmap_update_bits(state
->regmap
, 0xd73a, 0x08, 0x00);
846 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x02, 0x00);
851 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x10, 0x00);
855 /* write API version to firmware */
856 ret
= regmap_bulk_write(state
->regmap
, 0x9bf2, state
->api_version
, 4);
860 /* program ADC control */
861 switch (state
->clk
) {
862 case 28800000: /* 28.800 MHz */
865 case 20480000: /* 20.480 MHz */
868 case 28000000: /* 28.000 MHz */
871 case 25000000: /* 25.000 MHz */
879 ret
= regmap_update_bits(state
->regmap
, 0x9bd2, 0x0f, utmp
);
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);
891 /* Demod core settings */
892 dev_dbg(&client
->dev
, "load demod core settings\n");
893 len
= ARRAY_SIZE(demod_init_tab
);
894 tab
= demod_init_tab
;
895 for (i
= 0; i
< len
; i
++) {
896 ret
= regmap_update_bits(state
->regmap
, tab
[i
].reg
, tab
[i
].mask
,
902 /* Demod tuner specific settings */
903 dev_dbg(&client
->dev
, "load tuner specific settings\n");
904 switch (state
->tuner
) {
905 case AF9013_TUNER_MXL5003D
:
906 len
= ARRAY_SIZE(tuner_init_tab_mxl5003d
);
907 tab
= tuner_init_tab_mxl5003d
;
909 case AF9013_TUNER_MXL5005D
:
910 case AF9013_TUNER_MXL5005R
:
911 case AF9013_TUNER_MXL5007T
:
912 len
= ARRAY_SIZE(tuner_init_tab_mxl5005
);
913 tab
= tuner_init_tab_mxl5005
;
915 case AF9013_TUNER_ENV77H11D5
:
916 len
= ARRAY_SIZE(tuner_init_tab_env77h11d5
);
917 tab
= tuner_init_tab_env77h11d5
;
919 case AF9013_TUNER_MT2060
:
920 len
= ARRAY_SIZE(tuner_init_tab_mt2060
);
921 tab
= tuner_init_tab_mt2060
;
923 case AF9013_TUNER_MC44S803
:
924 len
= ARRAY_SIZE(tuner_init_tab_mc44s803
);
925 tab
= tuner_init_tab_mc44s803
;
927 case AF9013_TUNER_QT1010
:
928 case AF9013_TUNER_QT1010A
:
929 len
= ARRAY_SIZE(tuner_init_tab_qt1010
);
930 tab
= tuner_init_tab_qt1010
;
932 case AF9013_TUNER_MT2060_2
:
933 len
= ARRAY_SIZE(tuner_init_tab_mt2060_2
);
934 tab
= tuner_init_tab_mt2060_2
;
936 case AF9013_TUNER_TDA18271
:
937 case AF9013_TUNER_TDA18218
:
938 len
= ARRAY_SIZE(tuner_init_tab_tda18271
);
939 tab
= tuner_init_tab_tda18271
;
941 case AF9013_TUNER_UNKNOWN
:
943 len
= ARRAY_SIZE(tuner_init_tab_unknown
);
944 tab
= tuner_init_tab_unknown
;
948 for (i
= 0; i
< len
; i
++) {
949 ret
= regmap_update_bits(state
->regmap
, tab
[i
].reg
, tab
[i
].mask
,
956 if (state
->ts_output_pin
== 7)
957 utmp
= 1 << 3 | state
->ts_mode
<< 1;
959 utmp
= 0 << 3 | state
->ts_mode
<< 1;
960 ret
= regmap_update_bits(state
->regmap
, 0xd500, 0x0e, utmp
);
964 /* enable lock led */
965 ret
= regmap_update_bits(state
->regmap
, 0xd730, 0x01, 0x01);
969 state
->first_tune
= true;
973 dev_dbg(&client
->dev
, "failed %d\n", ret
);
977 static int af9013_sleep(struct dvb_frontend
*fe
)
979 struct af9013_state
*state
= fe
->demodulator_priv
;
980 struct i2c_client
*client
= state
->client
;
984 dev_dbg(&client
->dev
, "\n");
986 /* disable lock led */
987 ret
= regmap_update_bits(state
->regmap
, 0xd730, 0x01, 0x00);
992 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x10, 0x10);
996 /* Start reset execution */
997 ret
= regmap_write(state
->regmap
, 0xaeff, 0x01);
1001 /* Wait reset performs */
1002 ret
= regmap_read_poll_timeout(state
->regmap
, 0xd417, utmp
,
1003 (utmp
>> 1) & 0x01, 5000, 1000000);
1007 if (!((utmp
>> 1) & 0x01)) {
1013 ret
= regmap_update_bits(state
->regmap
, 0xd73a, 0x08, 0x08);
1019 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1023 static const struct dvb_frontend_ops af9013_ops
;
1025 static int af9013_download_firmware(struct af9013_state
*state
)
1027 struct i2c_client
*client
= state
->client
;
1028 int ret
, i
, len
, rem
;
1032 const struct firmware
*firmware
;
1033 const char *name
= AF9013_FIRMWARE
;
1035 dev_dbg(&client
->dev
, "\n");
1037 /* Check whether firmware is already running */
1038 ret
= regmap_read(state
->regmap
, 0x98be, &utmp
);
1042 dev_dbg(&client
->dev
, "firmware status %02x\n", utmp
);
1047 dev_info(&client
->dev
, "found a '%s' in cold state, will try to load a firmware\n",
1048 af9013_ops
.info
.name
);
1050 /* Request the firmware, will block and timeout */
1051 ret
= request_firmware(&firmware
, name
, &client
->dev
);
1053 dev_info(&client
->dev
, "firmware file '%s' not found %d\n",
1058 dev_info(&client
->dev
, "downloading firmware from file '%s'\n",
1061 /* Write firmware checksum & size */
1062 for (i
= 0; i
< firmware
->size
; i
++)
1063 checksum
+= firmware
->data
[i
];
1065 buf
[0] = (checksum
>> 8) & 0xff;
1066 buf
[1] = (checksum
>> 0) & 0xff;
1067 buf
[2] = (firmware
->size
>> 8) & 0xff;
1068 buf
[3] = (firmware
->size
>> 0) & 0xff;
1069 ret
= regmap_bulk_write(state
->regmap
, 0x50fc, buf
, 4);
1071 goto err_release_firmware
;
1073 /* Download firmware */
1075 for (rem
= firmware
->size
; rem
> 0; rem
-= LEN_MAX
) {
1076 len
= min(LEN_MAX
, rem
);
1077 ret
= regmap_bulk_write(state
->regmap
,
1078 0x5100 + firmware
->size
- rem
,
1079 &firmware
->data
[firmware
->size
- rem
],
1082 dev_err(&client
->dev
, "firmware download failed %d\n",
1084 goto err_release_firmware
;
1088 release_firmware(firmware
);
1091 ret
= regmap_write(state
->regmap
, 0xe205, 0x01);
1095 /* Check firmware status. 0c=OK, 04=fail */
1096 ret
= regmap_read_poll_timeout(state
->regmap
, 0x98be, utmp
,
1097 (utmp
== 0x0c || utmp
== 0x04),
1102 dev_dbg(&client
->dev
, "firmware status %02x\n", utmp
);
1106 dev_err(&client
->dev
, "firmware did not run\n");
1108 } else if (utmp
!= 0x0c) {
1110 dev_err(&client
->dev
, "firmware boot timeout\n");
1114 dev_info(&client
->dev
, "found a '%s' in warm state\n",
1115 af9013_ops
.info
.name
);
1118 err_release_firmware
:
1119 release_firmware(firmware
);
1121 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1125 static const struct dvb_frontend_ops af9013_ops
= {
1126 .delsys
= { SYS_DVBT
},
1128 .name
= "Afatech AF9013",
1129 .frequency_min_hz
= 174 * MHz
,
1130 .frequency_max_hz
= 862 * MHz
,
1131 .frequency_stepsize_hz
= 250 * kHz
,
1132 .caps
= FE_CAN_FEC_1_2
|
1142 FE_CAN_TRANSMISSION_MODE_AUTO
|
1143 FE_CAN_GUARD_INTERVAL_AUTO
|
1144 FE_CAN_HIERARCHY_AUTO
|
1149 .init
= af9013_init
,
1150 .sleep
= af9013_sleep
,
1152 .get_tune_settings
= af9013_get_tune_settings
,
1153 .set_frontend
= af9013_set_frontend
,
1154 .get_frontend
= af9013_get_frontend
,
1156 .read_status
= af9013_read_status
,
1157 .read_snr
= af9013_read_snr
,
1158 .read_signal_strength
= af9013_read_signal_strength
,
1159 .read_ber
= af9013_read_ber
,
1160 .read_ucblocks
= af9013_read_ucblocks
,
1163 static int af9013_pid_filter_ctrl(struct dvb_frontend
*fe
, int onoff
)
1165 struct af9013_state
*state
= fe
->demodulator_priv
;
1166 struct i2c_client
*client
= state
->client
;
1169 dev_dbg(&client
->dev
, "onoff %d\n", onoff
);
1171 ret
= regmap_update_bits(state
->regmap
, 0xd503, 0x01, onoff
);
1177 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1181 static int af9013_pid_filter(struct dvb_frontend
*fe
, u8 index
, u16 pid
,
1184 struct af9013_state
*state
= fe
->demodulator_priv
;
1185 struct i2c_client
*client
= state
->client
;
1189 dev_dbg(&client
->dev
, "index %d, pid %04x, onoff %d\n",
1193 /* 0x2000 is kernel virtual pid for whole ts (all pids) */
1198 buf
[0] = (pid
>> 0) & 0xff;
1199 buf
[1] = (pid
>> 8) & 0xff;
1200 ret
= regmap_bulk_write(state
->regmap
, 0xd505, buf
, 2);
1203 ret
= regmap_write(state
->regmap
, 0xd504, onoff
<< 5 | index
<< 0);
1209 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1213 static struct dvb_frontend
*af9013_get_dvb_frontend(struct i2c_client
*client
)
1215 struct af9013_state
*state
= i2c_get_clientdata(client
);
1217 dev_dbg(&client
->dev
, "\n");
1222 static struct i2c_adapter
*af9013_get_i2c_adapter(struct i2c_client
*client
)
1224 struct af9013_state
*state
= i2c_get_clientdata(client
);
1226 dev_dbg(&client
->dev
, "\n");
1228 return state
->muxc
->adapter
[0];
1232 * XXX: Hackish solution. We use virtual register, reg bit 16, to carry info
1233 * about i2c adapter locking. Own locking is needed because i2c mux call has
1234 * already locked i2c adapter.
1236 static int af9013_select(struct i2c_mux_core
*muxc
, u32 chan
)
1238 struct af9013_state
*state
= i2c_mux_priv(muxc
);
1239 struct i2c_client
*client
= state
->client
;
1242 dev_dbg(&client
->dev
, "\n");
1244 if (state
->ts_mode
== AF9013_TS_MODE_USB
)
1245 ret
= regmap_update_bits(state
->regmap
, 0x1d417, 0x08, 0x08);
1247 ret
= regmap_update_bits(state
->regmap
, 0x1d607, 0x04, 0x04);
1253 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1257 static int af9013_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
1259 struct af9013_state
*state
= i2c_mux_priv(muxc
);
1260 struct i2c_client
*client
= state
->client
;
1263 dev_dbg(&client
->dev
, "\n");
1265 if (state
->ts_mode
== AF9013_TS_MODE_USB
)
1266 ret
= regmap_update_bits(state
->regmap
, 0x1d417, 0x08, 0x00);
1268 ret
= regmap_update_bits(state
->regmap
, 0x1d607, 0x04, 0x00);
1274 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1278 /* Own I2C access routines needed for regmap as chip uses extra command byte */
1279 static int af9013_wregs(struct i2c_client
*client
, u8 cmd
, u16 reg
,
1280 const u8
*val
, int len
, u8 lock
)
1284 struct i2c_msg msg
[1] = {
1286 .addr
= client
->addr
,
1293 if (3 + len
> sizeof(buf
)) {
1298 buf
[0] = (reg
>> 8) & 0xff;
1299 buf
[1] = (reg
>> 0) & 0xff;
1301 memcpy(&buf
[3], val
, len
);
1304 i2c_lock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1305 ret
= __i2c_transfer(client
->adapter
, msg
, 1);
1307 i2c_unlock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1310 } else if (ret
!= 1) {
1317 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1321 static int af9013_rregs(struct i2c_client
*client
, u8 cmd
, u16 reg
,
1322 u8
*val
, int len
, u8 lock
)
1326 struct i2c_msg msg
[2] = {
1328 .addr
= client
->addr
,
1333 .addr
= client
->addr
,
1340 buf
[0] = (reg
>> 8) & 0xff;
1341 buf
[1] = (reg
>> 0) & 0xff;
1345 i2c_lock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1346 ret
= __i2c_transfer(client
->adapter
, msg
, 2);
1348 i2c_unlock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1351 } else if (ret
!= 2) {
1358 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1362 static int af9013_regmap_write(void *context
, const void *data
, size_t count
)
1364 struct i2c_client
*client
= context
;
1365 struct af9013_state
*state
= i2c_get_clientdata(client
);
1368 u8 lock
= !((u8
*)data
)[0];
1369 u16 reg
= ((u8
*)data
)[1] << 8 | ((u8
*)data
)[2] << 0;
1370 u8
*val
= &((u8
*)data
)[3];
1371 const unsigned int len
= count
- 3;
1373 if (state
->ts_mode
== AF9013_TS_MODE_USB
&& (reg
& 0xff00) != 0xae00) {
1374 cmd
= 0 << 7|0 << 6|(len
- 1) << 2|1 << 1|1 << 0;
1375 ret
= af9013_wregs(client
, cmd
, reg
, val
, len
, lock
);
1378 } else if (reg
>= 0x5100 && reg
< 0x8fff) {
1379 /* Firmware download */
1380 cmd
= 1 << 7|1 << 6|(len
- 1) << 2|1 << 1|1 << 0;
1381 ret
= af9013_wregs(client
, cmd
, reg
, val
, len
, lock
);
1385 cmd
= 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|1 << 0;
1386 for (i
= 0; i
< len
; i
++) {
1387 ret
= af9013_wregs(client
, cmd
, reg
+ i
, val
+ i
, 1,
1396 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1400 static int af9013_regmap_read(void *context
, const void *reg_buf
,
1401 size_t reg_size
, void *val_buf
, size_t val_size
)
1403 struct i2c_client
*client
= context
;
1404 struct af9013_state
*state
= i2c_get_clientdata(client
);
1407 u8 lock
= !((u8
*)reg_buf
)[0];
1408 u16 reg
= ((u8
*)reg_buf
)[1] << 8 | ((u8
*)reg_buf
)[2] << 0;
1409 u8
*val
= &((u8
*)val_buf
)[0];
1410 const unsigned int len
= val_size
;
1412 if (state
->ts_mode
== AF9013_TS_MODE_USB
&& (reg
& 0xff00) != 0xae00) {
1413 cmd
= 0 << 7|0 << 6|(len
- 1) << 2|1 << 1|0 << 0;
1414 ret
= af9013_rregs(client
, cmd
, reg
, val_buf
, len
, lock
);
1418 cmd
= 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|0 << 0;
1419 for (i
= 0; i
< len
; i
++) {
1420 ret
= af9013_rregs(client
, cmd
, reg
+ i
, val
+ i
, 1,
1429 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1433 static int af9013_probe(struct i2c_client
*client
,
1434 const struct i2c_device_id
*id
)
1436 struct af9013_state
*state
;
1437 struct af9013_platform_data
*pdata
= client
->dev
.platform_data
;
1438 struct dtv_frontend_properties
*c
;
1440 u8 firmware_version
[4];
1441 static const struct regmap_bus regmap_bus
= {
1442 .read
= af9013_regmap_read
,
1443 .write
= af9013_regmap_write
,
1445 static const struct regmap_config regmap_config
= {
1446 /* Actual reg is 16 bits, see i2c adapter lock */
1451 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1457 dev_dbg(&client
->dev
, "\n");
1459 /* Setup the state */
1460 state
->client
= client
;
1461 i2c_set_clientdata(client
, state
);
1462 state
->clk
= pdata
->clk
;
1463 state
->tuner
= pdata
->tuner
;
1464 state
->if_frequency
= pdata
->if_frequency
;
1465 state
->ts_mode
= pdata
->ts_mode
;
1466 state
->ts_output_pin
= pdata
->ts_output_pin
;
1467 state
->spec_inv
= pdata
->spec_inv
;
1468 memcpy(&state
->api_version
, pdata
->api_version
, sizeof(state
->api_version
));
1469 memcpy(&state
->gpio
, pdata
->gpio
, sizeof(state
->gpio
));
1470 state
->regmap
= regmap_init(&client
->dev
, ®map_bus
, client
,
1472 if (IS_ERR(state
->regmap
)) {
1473 ret
= PTR_ERR(state
->regmap
);
1476 /* Create mux i2c adapter */
1477 state
->muxc
= i2c_mux_alloc(client
->adapter
, &client
->dev
, 1, 0, 0,
1478 af9013_select
, af9013_deselect
);
1481 goto err_regmap_exit
;
1483 state
->muxc
->priv
= state
;
1484 ret
= i2c_mux_add_adapter(state
->muxc
, 0, 0, 0);
1486 goto err_regmap_exit
;
1488 /* Download firmware */
1489 if (state
->ts_mode
!= AF9013_TS_MODE_USB
) {
1490 ret
= af9013_download_firmware(state
);
1492 goto err_i2c_mux_del_adapters
;
1495 /* Firmware version */
1496 ret
= regmap_bulk_read(state
->regmap
, 0x5103, firmware_version
,
1497 sizeof(firmware_version
));
1499 goto err_i2c_mux_del_adapters
;
1502 for (i
= 0; i
< sizeof(state
->gpio
); i
++) {
1503 ret
= af9013_set_gpio(state
, i
, state
->gpio
[i
]);
1505 goto err_i2c_mux_del_adapters
;
1508 /* Create dvb frontend */
1509 memcpy(&state
->fe
.ops
, &af9013_ops
, sizeof(state
->fe
.ops
));
1510 state
->fe
.demodulator_priv
= state
;
1512 /* Setup callbacks */
1513 pdata
->get_dvb_frontend
= af9013_get_dvb_frontend
;
1514 pdata
->get_i2c_adapter
= af9013_get_i2c_adapter
;
1515 pdata
->pid_filter
= af9013_pid_filter
;
1516 pdata
->pid_filter_ctrl
= af9013_pid_filter_ctrl
;
1518 /* Init stats to indicate which stats are supported */
1519 c
= &state
->fe
.dtv_property_cache
;
1520 c
->strength
.len
= 1;
1522 c
->post_bit_error
.len
= 1;
1523 c
->post_bit_count
.len
= 1;
1524 c
->block_error
.len
= 1;
1525 c
->block_count
.len
= 1;
1527 dev_info(&client
->dev
, "Afatech AF9013 successfully attached\n");
1528 dev_info(&client
->dev
, "firmware version: %d.%d.%d.%d\n",
1529 firmware_version
[0], firmware_version
[1],
1530 firmware_version
[2], firmware_version
[3]);
1532 err_i2c_mux_del_adapters
:
1533 i2c_mux_del_adapters(state
->muxc
);
1535 regmap_exit(state
->regmap
);
1539 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1543 static int af9013_remove(struct i2c_client
*client
)
1545 struct af9013_state
*state
= i2c_get_clientdata(client
);
1547 dev_dbg(&client
->dev
, "\n");
1549 i2c_mux_del_adapters(state
->muxc
);
1551 regmap_exit(state
->regmap
);
1558 static const struct i2c_device_id af9013_id_table
[] = {
1562 MODULE_DEVICE_TABLE(i2c
, af9013_id_table
);
1564 static struct i2c_driver af9013_driver
= {
1567 .suppress_bind_attrs
= true,
1569 .probe
= af9013_probe
,
1570 .remove
= af9013_remove
,
1571 .id_table
= af9013_id_table
,
1574 module_i2c_driver(af9013_driver
);
1576 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1577 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1578 MODULE_LICENSE("GPL");
1579 MODULE_FIRMWARE(AF9013_FIRMWARE
);