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 dvb_frontend fe
;
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
;
43 enum fe_status fe_status
;
44 unsigned long set_frontend_jiffies
;
45 unsigned long read_status_jiffies
;
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
;
59 dev_dbg(&client
->dev
, "gpio %u, gpioval %02x\n", gpio
, gpioval
);
62 * GPIO0 & GPIO1 0xd735
63 * GPIO2 & GPIO3 0xd736
93 ret
= regmap_update_bits(state
->regmap
, addr
, 0x0f << pos
,
100 dev_dbg(&client
->dev
, "failed %d\n", 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
;
110 dev_dbg(&client
->dev
, "\n");
112 /* reset and start BER counter */
113 ret
= regmap_update_bits(state
->regmap
, 0xd391, 0x10, 0x10);
119 dev_dbg(&client
->dev
, "failed %d\n", 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
;
131 dev_dbg(&client
->dev
, "\n");
133 /* check if error bit count is ready */
134 ret
= regmap_read(state
->regmap
, 0xd391, &utmp
);
138 if (!((utmp
>> 4) & 0x01)) {
139 dev_dbg(&client
->dev
, "not ready\n");
143 ret
= regmap_bulk_read(state
->regmap
, 0xd387, buf
, 5);
147 state
->ber
= (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
148 state
->ucblocks
+= (buf
[4] << 8) | buf
[3];
152 dev_dbg(&client
->dev
, "failed %d\n", 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
;
162 dev_dbg(&client
->dev
, "\n");
165 ret
= regmap_update_bits(state
->regmap
, 0xd2e1, 0x08, 0x08);
171 dev_dbg(&client
->dev
, "failed %d\n", 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
;
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
);
193 if (!((utmp
>> 3) & 0x01)) {
194 dev_dbg(&client
->dev
, "not ready\n");
199 ret
= regmap_bulk_read(state
->regmap
, 0xd2e3, buf
, 3);
203 snr_val
= (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
205 /* read current modulation */
206 ret
= regmap_read(state
->regmap
, 0xd3c1, &utmp
);
210 switch ((utmp
>> 6) & 3) {
212 len
= ARRAY_SIZE(qpsk_snr_lut
);
213 snr_lut
= qpsk_snr_lut
;
216 len
= ARRAY_SIZE(qam16_snr_lut
);
217 snr_lut
= qam16_snr_lut
;
220 len
= ARRAY_SIZE(qam64_snr_lut
);
221 snr_lut
= qam64_snr_lut
;
227 for (i
= 0; i
< len
; i
++) {
228 utmp
= snr_lut
[i
].snr
;
230 if (snr_val
< snr_lut
[i
].val
)
233 state
->snr
= utmp
* 10; /* dB/10 */
235 c
->cnr
.stat
[0].svalue
= 1000 * utmp
;
236 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
240 dev_dbg(&client
->dev
, "failed %d\n", 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
;
249 u8 buf
[2], rf_gain
, if_gain
;
252 dev_dbg(&client
->dev
, "\n");
254 if (!state
->signal_strength_en
)
257 ret
= regmap_bulk_read(state
->regmap
, 0xd07c, buf
, 2);
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)
271 else if (signal_strength
> 0xffff)
272 signal_strength
= 0xffff;
274 state
->signal_strength
= signal_strength
;
278 dev_dbg(&client
->dev
, "failed %d\n", 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;
295 switch (state
->statistics_step
) {
297 state
->statistics_step
= 0;
300 af9013_statistics_signal_strength(&state
->fe
);
301 state
->statistics_step
++;
305 af9013_statistics_snr_start(&state
->fe
);
306 state
->statistics_step
++;
310 af9013_statistics_ber_unc_start(&state
->fe
);
311 state
->statistics_step
++;
315 af9013_statistics_snr_result(&state
->fe
);
316 state
->statistics_step
++;
320 af9013_statistics_ber_unc_result(&state
->fe
);
321 state
->statistics_step
++;
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;
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
;
348 u32 if_frequency
, freq_cw
;
350 dev_dbg(&client
->dev
, "frequency %u, bandwidth_hz %u\n",
351 c
->frequency
, c
->bandwidth_hz
);
354 if (fe
->ops
.tuner_ops
.set_params
) {
355 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
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
) {
369 /* Return an error if can't find bandwidth or the right clock */
370 if (i
== ARRAY_SIZE(coeff_lut
)) {
375 ret
= regmap_bulk_write(state
->regmap
, 0xae00, coeff_lut
[i
].val
,
376 sizeof(coeff_lut
[i
].val
));
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
,
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) {
402 spec_inv
= state
->spec_inv
;
404 spec_inv
= !state
->spec_inv
;
407 freq_cw
= DIV_ROUND_CLOSEST_ULL((u64
)sampling_freq
* 0x800000,
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);
427 ret
= regmap_bulk_write(state
->regmap
, 0x9be7, buf
, 6);
432 /* clear TPS lock flag */
433 ret
= regmap_update_bits(state
->regmap
, 0xd330, 0x08, 0x08);
437 /* clear MPEG2 lock flag */
438 ret
= regmap_update_bits(state
->regmap
, 0xd507, 0x40, 0x00);
442 /* empty channel function */
443 ret
= regmap_update_bits(state
->regmap
, 0x9bfe, 0x01, 0x00);
447 /* empty DVB-T channel function */
448 ret
= regmap_update_bits(state
->regmap
, 0x9bc2, 0x01, 0x00);
452 /* transmission parameters */
456 switch (c
->transmission_mode
) {
457 case TRANSMISSION_MODE_AUTO
:
460 case TRANSMISSION_MODE_2K
:
462 case TRANSMISSION_MODE_8K
:
466 dev_dbg(&client
->dev
, "invalid transmission_mode\n");
470 switch (c
->guard_interval
) {
471 case GUARD_INTERVAL_AUTO
:
474 case GUARD_INTERVAL_1_32
:
476 case GUARD_INTERVAL_1_16
:
479 case GUARD_INTERVAL_1_8
:
482 case GUARD_INTERVAL_1_4
:
486 dev_dbg(&client
->dev
, "invalid guard_interval\n");
490 switch (c
->hierarchy
) {
506 dev_dbg(&client
->dev
, "invalid hierarchy\n");
510 switch (c
->modulation
) {
523 dev_dbg(&client
->dev
, "invalid modulation\n");
527 /* Use HP. How and which case we can switch to LP? */
530 switch (c
->code_rate_HP
) {
549 dev_dbg(&client
->dev
, "invalid code_rate_HP\n");
553 switch (c
->code_rate_LP
) {
574 dev_dbg(&client
->dev
, "invalid code_rate_LP\n");
578 switch (c
->bandwidth_hz
) {
588 dev_dbg(&client
->dev
, "invalid bandwidth_hz\n");
593 ret
= regmap_bulk_write(state
->regmap
, 0xd3c0, buf
, 3);
598 /* clear easy mode flag */
599 ret
= regmap_write(state
->regmap
, 0xaefd, 0x00);
603 dev_dbg(&client
->dev
, "auto params\n");
605 /* set easy mode flag */
606 ret
= regmap_write(state
->regmap
, 0xaefd, 0x01);
610 ret
= regmap_write(state
->regmap
, 0xaefe, 0x00);
614 dev_dbg(&client
->dev
, "manual params\n");
618 ret
= regmap_write(state
->regmap
, 0xffff, 0x00);
622 state
->bandwidth_hz
= c
->bandwidth_hz
;
623 state
->set_frontend_jiffies
= jiffies
;
624 state
->first_tune
= false;
628 dev_dbg(&client
->dev
, "failed %d\n", 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
;
640 dev_dbg(&client
->dev
, "\n");
642 ret
= regmap_bulk_read(state
->regmap
, 0xd3c0, buf
, 3);
646 switch ((buf
[1] >> 6) & 3) {
648 c
->modulation
= QPSK
;
651 c
->modulation
= QAM_16
;
654 c
->modulation
= QAM_64
;
658 switch ((buf
[0] >> 0) & 3) {
660 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
663 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
666 switch ((buf
[0] >> 2) & 3) {
668 c
->guard_interval
= GUARD_INTERVAL_1_32
;
671 c
->guard_interval
= GUARD_INTERVAL_1_16
;
674 c
->guard_interval
= GUARD_INTERVAL_1_8
;
677 c
->guard_interval
= GUARD_INTERVAL_1_4
;
681 switch ((buf
[0] >> 4) & 7) {
683 c
->hierarchy
= HIERARCHY_NONE
;
686 c
->hierarchy
= HIERARCHY_1
;
689 c
->hierarchy
= HIERARCHY_2
;
692 c
->hierarchy
= HIERARCHY_4
;
696 switch ((buf
[2] >> 0) & 7) {
698 c
->code_rate_HP
= FEC_1_2
;
701 c
->code_rate_HP
= FEC_2_3
;
704 c
->code_rate_HP
= FEC_3_4
;
707 c
->code_rate_HP
= FEC_5_6
;
710 c
->code_rate_HP
= FEC_7_8
;
714 switch ((buf
[2] >> 3) & 7) {
716 c
->code_rate_LP
= FEC_1_2
;
719 c
->code_rate_LP
= FEC_2_3
;
722 c
->code_rate_LP
= FEC_3_4
;
725 c
->code_rate_LP
= FEC_5_6
;
728 c
->code_rate_LP
= FEC_7_8
;
732 switch ((buf
[1] >> 2) & 3) {
734 c
->bandwidth_hz
= 6000000;
737 c
->bandwidth_hz
= 7000000;
740 c
->bandwidth_hz
= 8000000;
746 dev_dbg(&client
->dev
, "failed %d\n", 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
;
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
;
773 ret
= regmap_read(state
->regmap
, 0xd507, &utmp
);
777 if ((utmp
>> 6) & 0x01)
778 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
779 FE_HAS_SYNC
| FE_HAS_LOCK
;
783 ret
= regmap_read(state
->regmap
, 0xd330, &utmp
);
787 if ((utmp
>> 3) & 0x01)
788 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
792 state
->fe_status
= *status
;
793 state
->read_status_jiffies
= jiffies
;
797 dev_dbg(&client
->dev
, "failed %d\n", ret
);
801 static int af9013_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
803 struct af9013_state
*state
= fe
->demodulator_priv
;
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
;
815 static int af9013_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
817 struct af9013_state
*state
= fe
->demodulator_priv
;
822 static int af9013_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
824 struct af9013_state
*state
= fe
->demodulator_priv
;
825 *ucblocks
= state
->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_bit
*init
;
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 /* load OFSM settings */
892 dev_dbg(&client
->dev
, "load ofsm settings\n");
893 len
= ARRAY_SIZE(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
);
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
;
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
;
918 case AF9013_TUNER_ENV77H11D5
:
919 len
= ARRAY_SIZE(tuner_init_env77h11d5
);
920 init
= tuner_init_env77h11d5
;
922 case AF9013_TUNER_MT2060
:
923 len
= ARRAY_SIZE(tuner_init_mt2060
);
924 init
= tuner_init_mt2060
;
926 case AF9013_TUNER_MC44S803
:
927 len
= ARRAY_SIZE(tuner_init_mc44s803
);
928 init
= tuner_init_mc44s803
;
930 case AF9013_TUNER_QT1010
:
931 case AF9013_TUNER_QT1010A
:
932 len
= ARRAY_SIZE(tuner_init_qt1010
);
933 init
= tuner_init_qt1010
;
935 case AF9013_TUNER_MT2060_2
:
936 len
= ARRAY_SIZE(tuner_init_mt2060_2
);
937 init
= tuner_init_mt2060_2
;
939 case AF9013_TUNER_TDA18271
:
940 case AF9013_TUNER_TDA18218
:
941 len
= ARRAY_SIZE(tuner_init_tda18271
);
942 init
= tuner_init_tda18271
;
944 case AF9013_TUNER_UNKNOWN
:
946 len
= ARRAY_SIZE(tuner_init_unknown
);
947 init
= tuner_init_unknown
;
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
);
962 if (state
->ts_output_pin
== 7)
963 utmp
= 1 << 3 | state
->ts_mode
<< 1;
965 utmp
= 0 << 3 | state
->ts_mode
<< 1;
966 ret
= regmap_update_bits(state
->regmap
, 0xd500, 0x0e, utmp
);
970 /* enable lock led */
971 ret
= regmap_update_bits(state
->regmap
, 0xd730, 0x01, 0x01);
975 /* check if we support signal strength */
976 if (!state
->signal_strength_en
) {
977 ret
= regmap_read(state
->regmap
, 0x9bee, &utmp
);
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);
989 ret
= regmap_bulk_read(state
->regmap
, 0x9bd0, &state
->rf_80
, 1);
992 ret
= regmap_bulk_read(state
->regmap
, 0x9be2, &state
->if_50
, 1);
995 ret
= regmap_bulk_read(state
->regmap
, 0x9be4, &state
->if_80
, 1);
1001 ret
= regmap_write(state
->regmap
, 0xd2e2, 0x01);
1006 buf
[0] = (10000 >> 0) & 0xff;
1007 buf
[1] = (10000 >> 8) & 0xff;
1008 ret
= regmap_bulk_write(state
->regmap
, 0xd385, buf
, 2);
1012 /* enable FEC monitor */
1013 ret
= regmap_update_bits(state
->regmap
, 0xd392, 0x02, 0x02);
1017 state
->first_tune
= true;
1018 schedule_delayed_work(&state
->statistics_work
, msecs_to_jiffies(400));
1022 dev_dbg(&client
->dev
, "failed %d\n", 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
;
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);
1044 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x10, 0x10);
1048 /* Start reset execution */
1049 ret
= regmap_write(state
->regmap
, 0xaeff, 0x01);
1053 /* Wait reset performs */
1054 ret
= regmap_read_poll_timeout(state
->regmap
, 0xd417, utmp
,
1055 (utmp
>> 1) & 0x01, 5000, 1000000);
1059 if (!((utmp
>> 1) & 0x01)) {
1065 ret
= regmap_update_bits(state
->regmap
, 0xd73a, 0x08, 0x08);
1071 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1075 static int af9013_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
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
)
1087 if (state
->ts_mode
== AF9013_TS_MODE_USB
)
1088 ret
= regmap_update_bits(state
->regmap
, 0xd417, 0x08,
1091 ret
= regmap_update_bits(state
->regmap
, 0xd607, 0x04,
1096 state
->i2c_gate_state
= enable
;
1100 dev_dbg(&client
->dev
, "failed %d\n", 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
;
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
);
1133 dev_dbg(&client
->dev
, "firmware status %02x\n", utmp
);
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
);
1144 dev_info(&client
->dev
, "firmware file '%s' not found %d\n",
1149 dev_info(&client
->dev
, "downloading firmware from file '%s'\n",
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);
1162 goto err_release_firmware
;
1164 /* Download firmware */
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
],
1173 dev_err(&client
->dev
, "firmware download failed %d\n",
1175 goto err_release_firmware
;
1179 release_firmware(firmware
);
1182 ret
= regmap_write(state
->regmap
, 0xe205, 0x01);
1186 /* Check firmware status. 0c=OK, 04=fail */
1187 ret
= regmap_read_poll_timeout(state
->regmap
, 0x98be, utmp
,
1188 (utmp
== 0x0c || utmp
== 0x04),
1193 dev_dbg(&client
->dev
, "firmware status %02x\n", utmp
);
1197 dev_err(&client
->dev
, "firmware did not run\n");
1199 } else if (utmp
!= 0x0c) {
1201 dev_err(&client
->dev
, "firmware boot timeout\n");
1205 dev_info(&client
->dev
, "found a '%s' in warm state\n",
1206 af9013_ops
.info
.name
);
1209 err_release_firmware
:
1210 release_firmware(firmware
);
1212 dev_dbg(&client
->dev
, "failed %d\n", 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
)
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
},
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
|
1268 FE_CAN_TRANSMISSION_MODE_AUTO
|
1269 FE_CAN_GUARD_INTERVAL_AUTO
|
1270 FE_CAN_HIERARCHY_AUTO
|
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");
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
)
1308 struct i2c_msg msg
[1] = {
1310 .addr
= client
->addr
,
1317 if (3 + len
> sizeof(buf
)) {
1322 buf
[0] = (reg
>> 8) & 0xff;
1323 buf
[1] = (reg
>> 0) & 0xff;
1325 memcpy(&buf
[3], val
, len
);
1326 ret
= i2c_transfer(client
->adapter
, msg
, 1);
1329 } else if (ret
!= 1) {
1336 dev_dbg(&client
->dev
, "failed %d\n", ret
);
1340 static int af9013_rregs(struct i2c_client
*client
, u8 cmd
, u16 reg
,
1345 struct i2c_msg msg
[2] = {
1347 .addr
= client
->addr
,
1352 .addr
= client
->addr
,
1359 buf
[0] = (reg
>> 8) & 0xff;
1360 buf
[1] = (reg
>> 0) & 0xff;
1362 ret
= i2c_transfer(client
->adapter
, msg
, 2);
1365 } else if (ret
!= 2) {
1372 dev_dbg(&client
->dev
, "failed %d\n", 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
);
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
);
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
);
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);
1408 dev_dbg(&client
->dev
, "failed %d\n", 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
);
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
);
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);
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
= {
1460 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
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
, ®map_bus
, client
,
1480 if (IS_ERR(state
->regmap
)) {
1481 ret
= PTR_ERR(state
->regmap
);
1485 /* Download firmware */
1486 if (state
->ts_mode
!= AF9013_TS_MODE_USB
) {
1487 ret
= af9013_download_firmware(state
);
1489 goto err_regmap_exit
;
1492 /* Firmware version */
1493 ret
= regmap_bulk_read(state
->regmap
, 0x5103, firmware_version
,
1494 sizeof(firmware_version
));
1496 goto err_regmap_exit
;
1499 for (i
= 0; i
< sizeof(state
->gpio
); i
++) {
1500 ret
= af9013_set_gpio(state
, i
, state
->gpio
[i
]);
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
;
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]);
1524 regmap_exit(state
->regmap
);
1528 dev_dbg(&client
->dev
, "failed %d\n", 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
);
1548 static const struct i2c_device_id af9013_id_table
[] = {
1552 MODULE_DEVICE_TABLE(i2c
, af9013_id_table
);
1554 static struct i2c_driver af9013_driver
= {
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
);