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"
24 struct i2c_client
*client
;
25 struct regmap
*regmap
;
26 struct i2c_mux_core
*muxc
;
27 struct dvb_frontend fe
;
38 enum fe_status fe_status
;
39 /* RF and IF AGC limits used for signal strength calc */
40 u8 strength_en
, rf_agc_50
, rf_agc_80
, if_agc_50
, if_agc_80
;
41 unsigned long set_frontend_jiffies
;
42 unsigned long read_status_jiffies
;
43 unsigned long strength_jiffies
;
44 unsigned long cnr_jiffies
;
45 unsigned long ber_ucb_jiffies
;
53 static int af9013_set_gpio(struct af9013_state
*state
, u8 gpio
, u8 gpioval
)
55 struct i2c_client
*client
= state
->client
;
60 dev_dbg(&client
->dev
, "gpio %u, gpioval %02x\n", gpio
, gpioval
);
63 * GPIO0 & GPIO1 0xd735
64 * GPIO2 & GPIO3 0xd736
94 ret
= regmap_update_bits(state
->regmap
, addr
, 0x0f << pos
,
101 dev_dbg(&client
->dev
, "failed %d\n", ret
);
105 static int af9013_get_tune_settings(struct dvb_frontend
*fe
,
106 struct dvb_frontend_tune_settings
*fesettings
)
108 fesettings
->min_delay_ms
= 800;
109 fesettings
->step_size
= 0;
110 fesettings
->max_drift
= 0;
115 static int af9013_set_frontend(struct dvb_frontend
*fe
)
117 struct af9013_state
*state
= fe
->demodulator_priv
;
118 struct i2c_client
*client
= state
->client
;
119 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
120 int ret
, i
, sampling_freq
;
121 bool auto_mode
, spec_inv
;
123 u32 if_frequency
, freq_cw
;
125 dev_dbg(&client
->dev
, "frequency %u, bandwidth_hz %u\n",
126 c
->frequency
, c
->bandwidth_hz
);
129 if (fe
->ops
.tuner_ops
.set_params
) {
130 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
135 /* program CFOE coefficients */
136 if (c
->bandwidth_hz
!= state
->bandwidth_hz
) {
137 for (i
= 0; i
< ARRAY_SIZE(coeff_lut
); i
++) {
138 if (coeff_lut
[i
].clock
== state
->clk
&&
139 coeff_lut
[i
].bandwidth_hz
== c
->bandwidth_hz
) {
144 /* Return an error if can't find bandwidth or the right clock */
145 if (i
== ARRAY_SIZE(coeff_lut
)) {
150 ret
= regmap_bulk_write(state
->regmap
, 0xae00, coeff_lut
[i
].val
,
151 sizeof(coeff_lut
[i
].val
));
156 /* program frequency control */
157 if (c
->bandwidth_hz
!= state
->bandwidth_hz
|| state
->first_tune
) {
158 /* get used IF frequency */
159 if (fe
->ops
.tuner_ops
.get_if_frequency
) {
160 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
,
165 if_frequency
= state
->if_frequency
;
168 dev_dbg(&client
->dev
, "if_frequency %u\n", if_frequency
);
170 sampling_freq
= if_frequency
;
172 while (sampling_freq
> (state
->clk
/ 2))
173 sampling_freq
-= state
->clk
;
175 if (sampling_freq
< 0) {
177 spec_inv
= state
->spec_inv
;
179 spec_inv
= !state
->spec_inv
;
182 freq_cw
= DIV_ROUND_CLOSEST_ULL((u64
)sampling_freq
* 0x800000,
186 freq_cw
= 0x800000 - freq_cw
;
188 buf
[0] = (freq_cw
>> 0) & 0xff;
189 buf
[1] = (freq_cw
>> 8) & 0xff;
190 buf
[2] = (freq_cw
>> 16) & 0x7f;
192 freq_cw
= 0x800000 - freq_cw
;
194 buf
[3] = (freq_cw
>> 0) & 0xff;
195 buf
[4] = (freq_cw
>> 8) & 0xff;
196 buf
[5] = (freq_cw
>> 16) & 0x7f;
198 ret
= regmap_bulk_write(state
->regmap
, 0xd140, buf
, 3);
202 ret
= regmap_bulk_write(state
->regmap
, 0x9be7, buf
, 6);
207 /* clear TPS lock flag */
208 ret
= regmap_update_bits(state
->regmap
, 0xd330, 0x08, 0x08);
212 /* clear MPEG2 lock flag */
213 ret
= regmap_update_bits(state
->regmap
, 0xd507, 0x40, 0x00);
217 /* empty channel function */
218 ret
= regmap_update_bits(state
->regmap
, 0x9bfe, 0x01, 0x00);
222 /* empty DVB-T channel function */
223 ret
= regmap_update_bits(state
->regmap
, 0x9bc2, 0x01, 0x00);
227 /* transmission parameters */
231 switch (c
->transmission_mode
) {
232 case TRANSMISSION_MODE_AUTO
:
235 case TRANSMISSION_MODE_2K
:
237 case TRANSMISSION_MODE_8K
:
241 dev_dbg(&client
->dev
, "invalid transmission_mode\n");
245 switch (c
->guard_interval
) {
246 case GUARD_INTERVAL_AUTO
:
249 case GUARD_INTERVAL_1_32
:
251 case GUARD_INTERVAL_1_16
:
254 case GUARD_INTERVAL_1_8
:
257 case GUARD_INTERVAL_1_4
:
261 dev_dbg(&client
->dev
, "invalid guard_interval\n");
265 switch (c
->hierarchy
) {
281 dev_dbg(&client
->dev
, "invalid hierarchy\n");
285 switch (c
->modulation
) {
298 dev_dbg(&client
->dev
, "invalid modulation\n");
302 /* Use HP. How and which case we can switch to LP? */
305 switch (c
->code_rate_HP
) {
324 dev_dbg(&client
->dev
, "invalid code_rate_HP\n");
328 switch (c
->code_rate_LP
) {
349 dev_dbg(&client
->dev
, "invalid code_rate_LP\n");
353 switch (c
->bandwidth_hz
) {
363 dev_dbg(&client
->dev
, "invalid bandwidth_hz\n");
368 ret
= regmap_bulk_write(state
->regmap
, 0xd3c0, buf
, 3);
373 /* clear easy mode flag */
374 ret
= regmap_write(state
->regmap
, 0xaefd, 0x00);
378 dev_dbg(&client
->dev
, "auto params\n");
380 /* set easy mode flag */
381 ret
= regmap_write(state
->regmap
, 0xaefd, 0x01);
385 ret
= regmap_write(state
->regmap
, 0xaefe, 0x00);
389 dev_dbg(&client
->dev
, "manual params\n");
393 ret
= regmap_write(state
->regmap
, 0xffff, 0x00);
397 state
->bandwidth_hz
= c
->bandwidth_hz
;
398 state
->set_frontend_jiffies
= jiffies
;
399 state
->first_tune
= false;
403 dev_dbg(&client
->dev
, "failed %d\n", ret
);
407 static int af9013_get_frontend(struct dvb_frontend
*fe
,
408 struct dtv_frontend_properties
*c
)
410 struct af9013_state
*state
= fe
->demodulator_priv
;
411 struct i2c_client
*client
= state
->client
;
415 dev_dbg(&client
->dev
, "\n");
417 ret
= regmap_bulk_read(state
->regmap
, 0xd3c0, buf
, 3);
421 switch ((buf
[1] >> 6) & 3) {
423 c
->modulation
= QPSK
;
426 c
->modulation
= QAM_16
;
429 c
->modulation
= QAM_64
;
433 switch ((buf
[0] >> 0) & 3) {
435 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
438 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
441 switch ((buf
[0] >> 2) & 3) {
443 c
->guard_interval
= GUARD_INTERVAL_1_32
;
446 c
->guard_interval
= GUARD_INTERVAL_1_16
;
449 c
->guard_interval
= GUARD_INTERVAL_1_8
;
452 c
->guard_interval
= GUARD_INTERVAL_1_4
;
456 switch ((buf
[0] >> 4) & 7) {
458 c
->hierarchy
= HIERARCHY_NONE
;
461 c
->hierarchy
= HIERARCHY_1
;
464 c
->hierarchy
= HIERARCHY_2
;
467 c
->hierarchy
= HIERARCHY_4
;
471 switch ((buf
[2] >> 0) & 7) {
473 c
->code_rate_HP
= FEC_1_2
;
476 c
->code_rate_HP
= FEC_2_3
;
479 c
->code_rate_HP
= FEC_3_4
;
482 c
->code_rate_HP
= FEC_5_6
;
485 c
->code_rate_HP
= FEC_7_8
;
489 switch ((buf
[2] >> 3) & 7) {
491 c
->code_rate_LP
= FEC_1_2
;
494 c
->code_rate_LP
= FEC_2_3
;
497 c
->code_rate_LP
= FEC_3_4
;
500 c
->code_rate_LP
= FEC_5_6
;
503 c
->code_rate_LP
= FEC_7_8
;
507 switch ((buf
[1] >> 2) & 3) {
509 c
->bandwidth_hz
= 6000000;
512 c
->bandwidth_hz
= 7000000;
515 c
->bandwidth_hz
= 8000000;
521 dev_dbg(&client
->dev
, "failed %d\n", ret
);
525 static int af9013_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
527 struct af9013_state
*state
= fe
->demodulator_priv
;
528 struct i2c_client
*client
= state
->client
;
529 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
531 unsigned int utmp
, utmp1
, utmp2
, utmp3
, utmp4
;
534 dev_dbg(&client
->dev
, "\n");
537 * Return status from the cache if it is younger than 2000ms with the
538 * exception of last tune is done during 4000ms.
540 if (time_is_after_jiffies(state
->read_status_jiffies
+ msecs_to_jiffies(2000)) &&
541 time_is_before_jiffies(state
->set_frontend_jiffies
+ msecs_to_jiffies(4000))) {
542 *status
= state
->fe_status
;
545 ret
= regmap_read(state
->regmap
, 0xd507, &utmp
);
549 if ((utmp
>> 6) & 0x01) {
550 utmp1
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
551 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
554 ret
= regmap_read(state
->regmap
, 0xd330, &utmp
);
558 if ((utmp
>> 3) & 0x01)
559 utmp1
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
565 dev_dbg(&client
->dev
, "fe_status %02x\n", utmp1
);
567 state
->read_status_jiffies
= jiffies
;
569 state
->fe_status
= utmp1
;
573 /* Signal strength */
574 switch (state
->strength_en
) {
576 /* Check if we support signal strength */
577 ret
= regmap_read(state
->regmap
, 0x9bee, &utmp
);
581 if ((utmp
>> 0) & 0x01) {
582 /* Read agc values for signal strength estimation */
583 ret
= regmap_read(state
->regmap
, 0x9bbd, &utmp1
);
586 ret
= regmap_read(state
->regmap
, 0x9bd0, &utmp2
);
589 ret
= regmap_read(state
->regmap
, 0x9be2, &utmp3
);
592 ret
= regmap_read(state
->regmap
, 0x9be4, &utmp4
);
596 state
->rf_agc_50
= utmp1
;
597 state
->rf_agc_80
= utmp2
;
598 state
->if_agc_50
= utmp3
;
599 state
->if_agc_80
= utmp4
;
600 dev_dbg(&client
->dev
,
601 "rf_agc_50 %u, rf_agc_80 %u, if_agc_50 %u, if_agc_80 %u\n",
602 utmp1
, utmp2
, utmp3
, utmp4
);
604 state
->strength_en
= 1;
606 /* Signal strength is not supported */
607 state
->strength_en
= 2;
612 if (time_is_after_jiffies(state
->strength_jiffies
+ msecs_to_jiffies(2000)))
616 ret
= regmap_bulk_read(state
->regmap
, 0xd07c, buf
, 2);
621 * Construct line equation from tuner dependent -80/-50 dBm agc
622 * limits and use it to map current agc value to dBm estimate
624 #define agc_gain (buf[0] + buf[1])
625 #define agc_gain_50dbm (state->rf_agc_50 + state->if_agc_50)
626 #define agc_gain_80dbm (state->rf_agc_80 + state->if_agc_80)
627 stmp1
= 30000 * (agc_gain
- agc_gain_80dbm
) /
628 (agc_gain_50dbm
- agc_gain_80dbm
) - 80000;
630 dev_dbg(&client
->dev
,
631 "strength %d, agc_gain %d, agc_gain_50dbm %d, agc_gain_80dbm %d\n",
632 stmp1
, agc_gain
, agc_gain_50dbm
, agc_gain_80dbm
);
634 state
->strength_jiffies
= jiffies
;
635 /* Convert [-90, -30] dBm to [0x0000, 0xffff] for dvbv3 */
636 utmp1
= clamp(stmp1
+ 90000, 0, 60000);
637 state
->dvbv3_strength
= div_u64((u64
)utmp1
* 0xffff, 60000);
639 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
640 c
->strength
.stat
[0].svalue
= stmp1
;
643 c
->strength
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
648 switch (state
->fe_status
& FE_HAS_VITERBI
) {
650 if (time_is_after_jiffies(state
->cnr_jiffies
+ msecs_to_jiffies(2000)))
653 /* Check if cnr ready */
654 ret
= regmap_read(state
->regmap
, 0xd2e1, &utmp
);
658 if (!((utmp
>> 3) & 0x01)) {
659 dev_dbg(&client
->dev
, "cnr not ready\n");
664 ret
= regmap_bulk_read(state
->regmap
, 0xd2e3, buf
, 3);
668 utmp1
= buf
[2] << 16 | buf
[1] << 8 | buf
[0] << 0;
670 /* Read current modulation */
671 ret
= regmap_read(state
->regmap
, 0xd3c1, &utmp
);
675 switch ((utmp
>> 6) & 3) {
679 * CNR[dB] 13 * -log10((1690000 - value) / value) + 2.6
680 * value [653799, 1689999], 2.6 / 13 = 3355443
682 utmp1
= clamp(utmp1
, 653799U, 1689999U);
683 utmp1
= ((u64
)(intlog10(utmp1
)
684 - intlog10(1690000 - utmp1
)
685 + 3355443) * 13 * 1000) >> 24;
690 * CNR[dB] 6 * log10((value - 370000) / (828000 - value)) + 15.7
691 * value [371105, 827999], 15.7 / 6 = 43900382
693 utmp1
= clamp(utmp1
, 371105U, 827999U);
694 utmp1
= ((u64
)(intlog10(utmp1
- 370000)
695 - intlog10(828000 - utmp1
)
696 + 43900382) * 6 * 1000) >> 24;
701 * CNR[dB] 8 * log10((value - 193000) / (425000 - value)) + 23.8
702 * value [193246, 424999], 23.8 / 8 = 49912218
704 utmp1
= clamp(utmp1
, 193246U, 424999U);
705 utmp1
= ((u64
)(intlog10(utmp1
- 193000)
706 - intlog10(425000 - utmp1
)
707 + 49912218) * 8 * 1000) >> 24;
710 dev_dbg(&client
->dev
, "invalid modulation %u\n",
716 dev_dbg(&client
->dev
, "cnr %u\n", utmp1
);
718 state
->cnr_jiffies
= jiffies
;
719 state
->dvbv3_snr
= utmp1
/ 100;
721 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
722 c
->cnr
.stat
[0].svalue
= utmp1
;
725 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
730 switch (state
->fe_status
& FE_HAS_SYNC
) {
732 if (time_is_after_jiffies(state
->ber_ucb_jiffies
+ msecs_to_jiffies(2000)))
735 /* Check if ber / ucb is ready */
736 ret
= regmap_read(state
->regmap
, 0xd391, &utmp
);
740 if (!((utmp
>> 4) & 0x01)) {
741 dev_dbg(&client
->dev
, "ber not ready\n");
746 ret
= regmap_bulk_read(state
->regmap
, 0xd385, buf
, 7);
750 utmp1
= buf
[4] << 16 | buf
[3] << 8 | buf
[2] << 0;
751 utmp2
= (buf
[1] << 8 | buf
[0] << 0) * 204 * 8;
752 utmp3
= buf
[6] << 8 | buf
[5] << 0;
753 utmp4
= buf
[1] << 8 | buf
[0] << 0;
755 /* Use 10000 TS packets for measure */
756 if (utmp4
!= 10000) {
757 buf
[0] = (10000 >> 0) & 0xff;
758 buf
[1] = (10000 >> 8) & 0xff;
759 ret
= regmap_bulk_write(state
->regmap
, 0xd385, buf
, 2);
764 /* Reset ber / ucb counter */
765 ret
= regmap_update_bits(state
->regmap
, 0xd391, 0x20, 0x20);
769 dev_dbg(&client
->dev
, "post_bit_error %u, post_bit_count %u\n",
771 dev_dbg(&client
->dev
, "block_error %u, block_count %u\n",
774 state
->ber_ucb_jiffies
= jiffies
;
775 state
->dvbv3_ber
= utmp1
;
776 state
->dvbv3_ucblocks
+= utmp3
;
778 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
779 c
->post_bit_error
.stat
[0].uvalue
+= utmp1
;
780 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
781 c
->post_bit_count
.stat
[0].uvalue
+= utmp2
;
783 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
784 c
->block_error
.stat
[0].uvalue
+= utmp3
;
785 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
786 c
->block_count
.stat
[0].uvalue
+= utmp4
;
789 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
790 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
792 c
->block_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
793 c
->block_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
799 dev_dbg(&client
->dev
, "failed %d\n", ret
);
803 static int af9013_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
805 struct af9013_state
*state
= fe
->demodulator_priv
;
807 *snr
= state
->dvbv3_snr
;
812 static int af9013_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
814 struct af9013_state
*state
= fe
->demodulator_priv
;
816 *strength
= state
->dvbv3_strength
;
821 static int af9013_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
823 struct af9013_state
*state
= fe
->demodulator_priv
;
825 *ber
= state
->dvbv3_ber
;
830 static int af9013_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
832 struct af9013_state
*state
= fe
->demodulator_priv
;
834 *ucblocks
= state
->dvbv3_ucblocks
;
839 static int af9013_init(struct dvb_frontend
*fe
)
841 struct af9013_state
*state
= fe
->demodulator_priv
;
842 struct i2c_client
*client
= state
->client
;
846 const struct af9013_reg_mask_val
*tab
;
848 dev_dbg(&client
->dev
, "\n");
851 ret
= regmap_update_bits(state
->regmap
, 0xd73a, 0x08, 0x00);
856 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x02, 0x00);
861 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x10, 0x00);
865 /* write API version to firmware */
866 ret
= regmap_bulk_write(state
->regmap
, 0x9bf2, state
->api_version
, 4);
870 /* program ADC control */
871 switch (state
->clk
) {
872 case 28800000: /* 28.800 MHz */
875 case 20480000: /* 20.480 MHz */
878 case 28000000: /* 28.000 MHz */
881 case 25000000: /* 25.000 MHz */
889 ret
= regmap_update_bits(state
->regmap
, 0x9bd2, 0x0f, utmp
);
893 utmp
= div_u64((u64
)state
->clk
* 0x80000, 1000000);
894 buf
[0] = (utmp
>> 0) & 0xff;
895 buf
[1] = (utmp
>> 8) & 0xff;
896 buf
[2] = (utmp
>> 16) & 0xff;
897 ret
= regmap_bulk_write(state
->regmap
, 0xd180, buf
, 3);
901 /* Demod core settings */
902 dev_dbg(&client
->dev
, "load demod core settings\n");
903 len
= ARRAY_SIZE(demod_init_tab
);
904 tab
= demod_init_tab
;
905 for (i
= 0; i
< len
; i
++) {
906 ret
= regmap_update_bits(state
->regmap
, tab
[i
].reg
, tab
[i
].mask
,
912 /* Demod tuner specific settings */
913 dev_dbg(&client
->dev
, "load tuner specific settings\n");
914 switch (state
->tuner
) {
915 case AF9013_TUNER_MXL5003D
:
916 len
= ARRAY_SIZE(tuner_init_tab_mxl5003d
);
917 tab
= tuner_init_tab_mxl5003d
;
919 case AF9013_TUNER_MXL5005D
:
920 case AF9013_TUNER_MXL5005R
:
921 case AF9013_TUNER_MXL5007T
:
922 len
= ARRAY_SIZE(tuner_init_tab_mxl5005
);
923 tab
= tuner_init_tab_mxl5005
;
925 case AF9013_TUNER_ENV77H11D5
:
926 len
= ARRAY_SIZE(tuner_init_tab_env77h11d5
);
927 tab
= tuner_init_tab_env77h11d5
;
929 case AF9013_TUNER_MT2060
:
930 len
= ARRAY_SIZE(tuner_init_tab_mt2060
);
931 tab
= tuner_init_tab_mt2060
;
933 case AF9013_TUNER_MC44S803
:
934 len
= ARRAY_SIZE(tuner_init_tab_mc44s803
);
935 tab
= tuner_init_tab_mc44s803
;
937 case AF9013_TUNER_QT1010
:
938 case AF9013_TUNER_QT1010A
:
939 len
= ARRAY_SIZE(tuner_init_tab_qt1010
);
940 tab
= tuner_init_tab_qt1010
;
942 case AF9013_TUNER_MT2060_2
:
943 len
= ARRAY_SIZE(tuner_init_tab_mt2060_2
);
944 tab
= tuner_init_tab_mt2060_2
;
946 case AF9013_TUNER_TDA18271
:
947 case AF9013_TUNER_TDA18218
:
948 len
= ARRAY_SIZE(tuner_init_tab_tda18271
);
949 tab
= tuner_init_tab_tda18271
;
951 case AF9013_TUNER_UNKNOWN
:
953 len
= ARRAY_SIZE(tuner_init_tab_unknown
);
954 tab
= tuner_init_tab_unknown
;
958 for (i
= 0; i
< len
; i
++) {
959 ret
= regmap_update_bits(state
->regmap
, tab
[i
].reg
, tab
[i
].mask
,
966 if (state
->ts_output_pin
== 7)
967 utmp
= 1 << 3 | state
->ts_mode
<< 1;
969 utmp
= 0 << 3 | state
->ts_mode
<< 1;
970 ret
= regmap_update_bits(state
->regmap
, 0xd500, 0x0e, utmp
);
974 /* enable lock led */
975 ret
= regmap_update_bits(state
->regmap
, 0xd730, 0x01, 0x01);
979 state
->first_tune
= true;
983 dev_dbg(&client
->dev
, "failed %d\n", ret
);
987 static int af9013_sleep(struct dvb_frontend
*fe
)
989 struct af9013_state
*state
= fe
->demodulator_priv
;
990 struct i2c_client
*client
= state
->client
;
994 dev_dbg(&client
->dev
, "\n");
996 /* disable lock led */
997 ret
= regmap_update_bits(state
->regmap
, 0xd730, 0x01, 0x00);
1002 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x10, 0x10);
1006 /* Start reset execution */
1007 ret
= regmap_write(state
->regmap
, 0xaeff, 0x01);
1011 /* Wait reset performs */
1012 ret
= regmap_read_poll_timeout(state
->regmap
, 0xd417, utmp
,
1013 (utmp
>> 1) & 0x01, 5000, 1000000);
1017 if (!((utmp
>> 1) & 0x01)) {
1023 ret
= regmap_update_bits(state
->regmap
, 0xd73a, 0x08, 0x08);
1029 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1033 static const struct dvb_frontend_ops af9013_ops
;
1035 static int af9013_download_firmware(struct af9013_state
*state
)
1037 struct i2c_client
*client
= state
->client
;
1038 int ret
, i
, len
, rem
;
1042 const struct firmware
*firmware
;
1043 const char *name
= AF9013_FIRMWARE
;
1045 dev_dbg(&client
->dev
, "\n");
1047 /* Check whether firmware is already running */
1048 ret
= regmap_read(state
->regmap
, 0x98be, &utmp
);
1052 dev_dbg(&client
->dev
, "firmware status %02x\n", utmp
);
1057 dev_info(&client
->dev
, "found a '%s' in cold state, will try to load a firmware\n",
1058 af9013_ops
.info
.name
);
1060 /* Request the firmware, will block and timeout */
1061 ret
= request_firmware(&firmware
, name
, &client
->dev
);
1063 dev_info(&client
->dev
, "firmware file '%s' not found %d\n",
1068 dev_info(&client
->dev
, "downloading firmware from file '%s'\n",
1071 /* Write firmware checksum & size */
1072 for (i
= 0; i
< firmware
->size
; i
++)
1073 checksum
+= firmware
->data
[i
];
1075 buf
[0] = (checksum
>> 8) & 0xff;
1076 buf
[1] = (checksum
>> 0) & 0xff;
1077 buf
[2] = (firmware
->size
>> 8) & 0xff;
1078 buf
[3] = (firmware
->size
>> 0) & 0xff;
1079 ret
= regmap_bulk_write(state
->regmap
, 0x50fc, buf
, 4);
1081 goto err_release_firmware
;
1083 /* Download firmware */
1085 for (rem
= firmware
->size
; rem
> 0; rem
-= LEN_MAX
) {
1086 len
= min(LEN_MAX
, rem
);
1087 ret
= regmap_bulk_write(state
->regmap
,
1088 0x5100 + firmware
->size
- rem
,
1089 &firmware
->data
[firmware
->size
- rem
],
1092 dev_err(&client
->dev
, "firmware download failed %d\n",
1094 goto err_release_firmware
;
1098 release_firmware(firmware
);
1101 ret
= regmap_write(state
->regmap
, 0xe205, 0x01);
1105 /* Check firmware status. 0c=OK, 04=fail */
1106 ret
= regmap_read_poll_timeout(state
->regmap
, 0x98be, utmp
,
1107 (utmp
== 0x0c || utmp
== 0x04),
1112 dev_dbg(&client
->dev
, "firmware status %02x\n", utmp
);
1116 dev_err(&client
->dev
, "firmware did not run\n");
1118 } else if (utmp
!= 0x0c) {
1120 dev_err(&client
->dev
, "firmware boot timeout\n");
1124 dev_info(&client
->dev
, "found a '%s' in warm state\n",
1125 af9013_ops
.info
.name
);
1128 err_release_firmware
:
1129 release_firmware(firmware
);
1131 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1135 static const struct dvb_frontend_ops af9013_ops
= {
1136 .delsys
= { SYS_DVBT
},
1138 .name
= "Afatech AF9013",
1139 .frequency_min_hz
= 174 * MHz
,
1140 .frequency_max_hz
= 862 * MHz
,
1141 .frequency_stepsize_hz
= 250 * kHz
,
1142 .caps
= FE_CAN_FEC_1_2
|
1152 FE_CAN_TRANSMISSION_MODE_AUTO
|
1153 FE_CAN_GUARD_INTERVAL_AUTO
|
1154 FE_CAN_HIERARCHY_AUTO
|
1159 .init
= af9013_init
,
1160 .sleep
= af9013_sleep
,
1162 .get_tune_settings
= af9013_get_tune_settings
,
1163 .set_frontend
= af9013_set_frontend
,
1164 .get_frontend
= af9013_get_frontend
,
1166 .read_status
= af9013_read_status
,
1167 .read_snr
= af9013_read_snr
,
1168 .read_signal_strength
= af9013_read_signal_strength
,
1169 .read_ber
= af9013_read_ber
,
1170 .read_ucblocks
= af9013_read_ucblocks
,
1173 static int af9013_pid_filter_ctrl(struct dvb_frontend
*fe
, int onoff
)
1175 struct af9013_state
*state
= fe
->demodulator_priv
;
1176 struct i2c_client
*client
= state
->client
;
1179 dev_dbg(&client
->dev
, "onoff %d\n", onoff
);
1181 ret
= regmap_update_bits(state
->regmap
, 0xd503, 0x01, onoff
);
1187 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1191 static int af9013_pid_filter(struct dvb_frontend
*fe
, u8 index
, u16 pid
,
1194 struct af9013_state
*state
= fe
->demodulator_priv
;
1195 struct i2c_client
*client
= state
->client
;
1199 dev_dbg(&client
->dev
, "index %d, pid %04x, onoff %d\n",
1203 /* 0x2000 is kernel virtual pid for whole ts (all pids) */
1208 buf
[0] = (pid
>> 0) & 0xff;
1209 buf
[1] = (pid
>> 8) & 0xff;
1210 ret
= regmap_bulk_write(state
->regmap
, 0xd505, buf
, 2);
1213 ret
= regmap_write(state
->regmap
, 0xd504, onoff
<< 5 | index
<< 0);
1219 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1223 static struct dvb_frontend
*af9013_get_dvb_frontend(struct i2c_client
*client
)
1225 struct af9013_state
*state
= i2c_get_clientdata(client
);
1227 dev_dbg(&client
->dev
, "\n");
1232 static struct i2c_adapter
*af9013_get_i2c_adapter(struct i2c_client
*client
)
1234 struct af9013_state
*state
= i2c_get_clientdata(client
);
1236 dev_dbg(&client
->dev
, "\n");
1238 return state
->muxc
->adapter
[0];
1242 * XXX: Hackish solution. We use virtual register, reg bit 16, to carry info
1243 * about i2c adapter locking. Own locking is needed because i2c mux call has
1244 * already locked i2c adapter.
1246 static int af9013_select(struct i2c_mux_core
*muxc
, u32 chan
)
1248 struct af9013_state
*state
= i2c_mux_priv(muxc
);
1249 struct i2c_client
*client
= state
->client
;
1252 dev_dbg(&client
->dev
, "\n");
1254 if (state
->ts_mode
== AF9013_TS_MODE_USB
)
1255 ret
= regmap_update_bits(state
->regmap
, 0x1d417, 0x08, 0x08);
1257 ret
= regmap_update_bits(state
->regmap
, 0x1d607, 0x04, 0x04);
1263 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1267 static int af9013_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
1269 struct af9013_state
*state
= i2c_mux_priv(muxc
);
1270 struct i2c_client
*client
= state
->client
;
1273 dev_dbg(&client
->dev
, "\n");
1275 if (state
->ts_mode
== AF9013_TS_MODE_USB
)
1276 ret
= regmap_update_bits(state
->regmap
, 0x1d417, 0x08, 0x00);
1278 ret
= regmap_update_bits(state
->regmap
, 0x1d607, 0x04, 0x00);
1284 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1288 /* Own I2C access routines needed for regmap as chip uses extra command byte */
1289 static int af9013_wregs(struct i2c_client
*client
, u8 cmd
, u16 reg
,
1290 const u8
*val
, int len
, u8 lock
)
1294 struct i2c_msg msg
[1] = {
1296 .addr
= client
->addr
,
1303 if (3 + len
> sizeof(buf
)) {
1308 buf
[0] = (reg
>> 8) & 0xff;
1309 buf
[1] = (reg
>> 0) & 0xff;
1311 memcpy(&buf
[3], val
, len
);
1314 i2c_lock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1315 ret
= __i2c_transfer(client
->adapter
, msg
, 1);
1317 i2c_unlock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1320 } else if (ret
!= 1) {
1327 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1331 static int af9013_rregs(struct i2c_client
*client
, u8 cmd
, u16 reg
,
1332 u8
*val
, int len
, u8 lock
)
1336 struct i2c_msg msg
[2] = {
1338 .addr
= client
->addr
,
1343 .addr
= client
->addr
,
1350 buf
[0] = (reg
>> 8) & 0xff;
1351 buf
[1] = (reg
>> 0) & 0xff;
1355 i2c_lock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1356 ret
= __i2c_transfer(client
->adapter
, msg
, 2);
1358 i2c_unlock_bus(client
->adapter
, I2C_LOCK_SEGMENT
);
1361 } else if (ret
!= 2) {
1368 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1372 static int af9013_regmap_write(void *context
, const void *data
, size_t count
)
1374 struct i2c_client
*client
= context
;
1375 struct af9013_state
*state
= i2c_get_clientdata(client
);
1378 u8 lock
= !((u8
*)data
)[0];
1379 u16 reg
= ((u8
*)data
)[1] << 8 | ((u8
*)data
)[2] << 0;
1380 u8
*val
= &((u8
*)data
)[3];
1381 const unsigned int len
= count
- 3;
1383 if (state
->ts_mode
== AF9013_TS_MODE_USB
&& (reg
& 0xff00) != 0xae00) {
1384 cmd
= 0 << 7|0 << 6|(len
- 1) << 2|1 << 1|1 << 0;
1385 ret
= af9013_wregs(client
, cmd
, reg
, val
, len
, lock
);
1388 } else if (reg
>= 0x5100 && reg
< 0x8fff) {
1389 /* Firmware download */
1390 cmd
= 1 << 7|1 << 6|(len
- 1) << 2|1 << 1|1 << 0;
1391 ret
= af9013_wregs(client
, cmd
, reg
, val
, len
, lock
);
1395 cmd
= 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|1 << 0;
1396 for (i
= 0; i
< len
; i
++) {
1397 ret
= af9013_wregs(client
, cmd
, reg
+ i
, val
+ i
, 1,
1406 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1410 static int af9013_regmap_read(void *context
, const void *reg_buf
,
1411 size_t reg_size
, void *val_buf
, size_t val_size
)
1413 struct i2c_client
*client
= context
;
1414 struct af9013_state
*state
= i2c_get_clientdata(client
);
1417 u8 lock
= !((u8
*)reg_buf
)[0];
1418 u16 reg
= ((u8
*)reg_buf
)[1] << 8 | ((u8
*)reg_buf
)[2] << 0;
1419 u8
*val
= &((u8
*)val_buf
)[0];
1420 const unsigned int len
= val_size
;
1422 if (state
->ts_mode
== AF9013_TS_MODE_USB
&& (reg
& 0xff00) != 0xae00) {
1423 cmd
= 0 << 7|0 << 6|(len
- 1) << 2|1 << 1|0 << 0;
1424 ret
= af9013_rregs(client
, cmd
, reg
, val_buf
, len
, lock
);
1428 cmd
= 0 << 7|0 << 6|(1 - 1) << 2|1 << 1|0 << 0;
1429 for (i
= 0; i
< len
; i
++) {
1430 ret
= af9013_rregs(client
, cmd
, reg
+ i
, val
+ i
, 1,
1439 dev_dbg(&client
->dev
, "failed %d\n", 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
;
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 /* Actual reg is 16 bits, see i2c adapter lock */
1461 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1467 dev_dbg(&client
->dev
, "\n");
1469 /* Setup the state */
1470 state
->client
= client
;
1471 i2c_set_clientdata(client
, state
);
1472 state
->clk
= pdata
->clk
;
1473 state
->tuner
= pdata
->tuner
;
1474 state
->if_frequency
= pdata
->if_frequency
;
1475 state
->ts_mode
= pdata
->ts_mode
;
1476 state
->ts_output_pin
= pdata
->ts_output_pin
;
1477 state
->spec_inv
= pdata
->spec_inv
;
1478 memcpy(&state
->api_version
, pdata
->api_version
, sizeof(state
->api_version
));
1479 memcpy(&state
->gpio
, pdata
->gpio
, sizeof(state
->gpio
));
1480 state
->regmap
= regmap_init(&client
->dev
, ®map_bus
, client
,
1482 if (IS_ERR(state
->regmap
)) {
1483 ret
= PTR_ERR(state
->regmap
);
1486 /* Create mux i2c adapter */
1487 state
->muxc
= i2c_mux_alloc(client
->adapter
, &client
->dev
, 1, 0, 0,
1488 af9013_select
, af9013_deselect
);
1491 goto err_regmap_exit
;
1493 state
->muxc
->priv
= state
;
1494 ret
= i2c_mux_add_adapter(state
->muxc
, 0, 0, 0);
1496 goto err_regmap_exit
;
1498 /* Download firmware */
1499 if (state
->ts_mode
!= AF9013_TS_MODE_USB
) {
1500 ret
= af9013_download_firmware(state
);
1502 goto err_i2c_mux_del_adapters
;
1505 /* Firmware version */
1506 ret
= regmap_bulk_read(state
->regmap
, 0x5103, firmware_version
,
1507 sizeof(firmware_version
));
1509 goto err_i2c_mux_del_adapters
;
1512 for (i
= 0; i
< sizeof(state
->gpio
); i
++) {
1513 ret
= af9013_set_gpio(state
, i
, state
->gpio
[i
]);
1515 goto err_i2c_mux_del_adapters
;
1518 /* Create dvb frontend */
1519 memcpy(&state
->fe
.ops
, &af9013_ops
, sizeof(state
->fe
.ops
));
1520 state
->fe
.demodulator_priv
= state
;
1522 /* Setup callbacks */
1523 pdata
->get_dvb_frontend
= af9013_get_dvb_frontend
;
1524 pdata
->get_i2c_adapter
= af9013_get_i2c_adapter
;
1525 pdata
->pid_filter
= af9013_pid_filter
;
1526 pdata
->pid_filter_ctrl
= af9013_pid_filter_ctrl
;
1528 /* Init stats to indicate which stats are supported */
1529 c
= &state
->fe
.dtv_property_cache
;
1530 c
->strength
.len
= 1;
1532 c
->post_bit_error
.len
= 1;
1533 c
->post_bit_count
.len
= 1;
1534 c
->block_error
.len
= 1;
1535 c
->block_count
.len
= 1;
1537 dev_info(&client
->dev
, "Afatech AF9013 successfully attached\n");
1538 dev_info(&client
->dev
, "firmware version: %d.%d.%d.%d\n",
1539 firmware_version
[0], firmware_version
[1],
1540 firmware_version
[2], firmware_version
[3]);
1542 err_i2c_mux_del_adapters
:
1543 i2c_mux_del_adapters(state
->muxc
);
1545 regmap_exit(state
->regmap
);
1549 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1553 static int af9013_remove(struct i2c_client
*client
)
1555 struct af9013_state
*state
= i2c_get_clientdata(client
);
1557 dev_dbg(&client
->dev
, "\n");
1559 i2c_mux_del_adapters(state
->muxc
);
1561 regmap_exit(state
->regmap
);
1568 static const struct i2c_device_id af9013_id_table
[] = {
1572 MODULE_DEVICE_TABLE(i2c
, af9013_id_table
);
1574 static struct i2c_driver af9013_driver
= {
1577 .suppress_bind_attrs
= true,
1579 .probe
= af9013_probe
,
1580 .remove
= af9013_remove
,
1581 .id_table
= af9013_id_table
,
1584 module_i2c_driver(af9013_driver
);
1586 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1587 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1588 MODULE_LICENSE("GPL");
1589 MODULE_FIRMWARE(AF9013_FIRMWARE
);