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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "af9013_priv.h"
28 module_param_named(debug
, af9013_debug
, int, 0644);
29 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
32 struct i2c_adapter
*i2c
;
33 struct dvb_frontend fe
;
34 struct af9013_config config
;
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 fe_status_t 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 /* write multiple registers */
53 static int af9013_wr_regs_i2c(struct af9013_state
*priv
, u8 mbox
, u16 reg
,
54 const u8
*val
, int len
)
58 struct i2c_msg msg
[1] = {
60 .addr
= priv
->config
.i2c_addr
,
67 buf
[0] = (reg
>> 8) & 0xff;
68 buf
[1] = (reg
>> 0) & 0xff;
70 memcpy(&buf
[3], val
, len
);
72 ret
= i2c_transfer(priv
->i2c
, msg
, 1);
76 warn("i2c wr failed=%d reg=%04x len=%d", ret
, reg
, len
);
82 /* read multiple registers */
83 static int af9013_rd_regs_i2c(struct af9013_state
*priv
, u8 mbox
, u16 reg
,
88 struct i2c_msg msg
[2] = {
90 .addr
= priv
->config
.i2c_addr
,
95 .addr
= priv
->config
.i2c_addr
,
102 buf
[0] = (reg
>> 8) & 0xff;
103 buf
[1] = (reg
>> 0) & 0xff;
106 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
110 warn("i2c rd failed=%d reg=%04x len=%d", ret
, reg
, len
);
116 /* write multiple registers */
117 static int af9013_wr_regs(struct af9013_state
*priv
, u16 reg
, const u8
*val
,
121 u8 mbox
= (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0);
123 if ((priv
->config
.ts_mode
== AF9013_TS_USB
) &&
124 ((reg
& 0xff00) != 0xff00) && ((reg
& 0xff00) != 0xae00)) {
125 mbox
|= ((len
- 1) << 2);
126 ret
= af9013_wr_regs_i2c(priv
, mbox
, reg
, val
, len
);
128 for (i
= 0; i
< len
; i
++) {
129 ret
= af9013_wr_regs_i2c(priv
, mbox
, reg
+i
, val
+i
, 1);
139 /* read multiple registers */
140 static int af9013_rd_regs(struct af9013_state
*priv
, u16 reg
, u8
*val
, int len
)
143 u8 mbox
= (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0);
145 if ((priv
->config
.ts_mode
== AF9013_TS_USB
) &&
146 ((reg
& 0xff00) != 0xff00) && ((reg
& 0xff00) != 0xae00)) {
147 mbox
|= ((len
- 1) << 2);
148 ret
= af9013_rd_regs_i2c(priv
, mbox
, reg
, val
, len
);
150 for (i
= 0; i
< len
; i
++) {
151 ret
= af9013_rd_regs_i2c(priv
, mbox
, reg
+i
, val
+i
, 1);
161 /* write single register */
162 static int af9013_wr_reg(struct af9013_state
*priv
, u16 reg
, u8 val
)
164 return af9013_wr_regs(priv
, reg
, &val
, 1);
167 /* read single register */
168 static int af9013_rd_reg(struct af9013_state
*priv
, u16 reg
, u8
*val
)
170 return af9013_rd_regs(priv
, reg
, val
, 1);
173 static int af9013_write_ofsm_regs(struct af9013_state
*state
, u16 reg
, u8
*val
,
176 u8 mbox
= (1 << 7)|(1 << 6)|((len
- 1) << 2)|(1 << 1)|(1 << 0);
177 return af9013_wr_regs_i2c(state
, mbox
, reg
, val
, len
);
180 static int af9013_wr_reg_bits(struct af9013_state
*state
, u16 reg
, int pos
,
186 /* no need for read if whole reg is written */
188 ret
= af9013_rd_reg(state
, reg
, &tmp
);
192 mask
= (0xff >> (8 - len
)) << pos
;
198 return af9013_wr_reg(state
, reg
, val
);
201 static int af9013_rd_reg_bits(struct af9013_state
*state
, u16 reg
, int pos
,
207 ret
= af9013_rd_reg(state
, reg
, &tmp
);
212 *val
&= (0xff >> (8 - len
));
217 static int af9013_set_gpio(struct af9013_state
*state
, u8 gpio
, u8 gpioval
)
223 dbg("%s: gpio=%d gpioval=%02x", __func__
, gpio
, gpioval
);
226 * GPIO0 & GPIO1 0xd735
227 * GPIO2 & GPIO3 0xd736
241 err("invalid gpio:%d\n", gpio
);
258 ret
= af9013_wr_reg_bits(state
, addr
, pos
, 4, gpioval
);
264 dbg("%s: failed=%d", __func__
, ret
);
268 static u32
af913_div(u32 a
, u32 b
, u32 x
)
272 dbg("%s: a=%d b=%d x=%d", __func__
, a
, b
, x
);
279 for (i
= 0; i
< x
; i
++) {
287 r
= (c
<< (u32
)x
) + r
;
289 dbg("%s: a=%d b=%d x=%d r=%x", __func__
, a
, b
, x
, r
);
293 static int af9013_power_ctrl(struct af9013_state
*state
, u8 onoff
)
298 dbg("%s: onoff=%d", __func__
, onoff
);
301 ret
= af9013_wr_reg_bits(state
, 0xd417, 4, 1, 1);
305 /* start reset mechanism */
306 ret
= af9013_wr_reg(state
, 0xaeff, 1);
310 /* wait reset performs */
311 for (i
= 0; i
< 150; i
++) {
312 ret
= af9013_rd_reg_bits(state
, 0xd417, 1, 1, &tmp
);
317 break; /* reset done */
319 usleep_range(5000, 25000);
327 ret
= af9013_wr_reg_bits(state
, 0xd417, 1, 1, 0);
332 ret
= af9013_wr_reg_bits(state
, 0xd417, 4, 1, 0);
335 ret
= af9013_wr_reg_bits(state
, 0xd73a, 3, 1, 0);
338 ret
= af9013_wr_reg_bits(state
, 0xd73a, 3, 1, 1);
343 dbg("%s: failed=%d", __func__
, ret
);
347 static int af9013_statistics_ber_unc_start(struct dvb_frontend
*fe
)
349 struct af9013_state
*state
= fe
->demodulator_priv
;
354 /* reset and start BER counter */
355 ret
= af9013_wr_reg_bits(state
, 0xd391, 4, 1, 1);
361 dbg("%s: failed=%d", __func__
, ret
);
365 static int af9013_statistics_ber_unc_result(struct dvb_frontend
*fe
)
367 struct af9013_state
*state
= fe
->demodulator_priv
;
373 /* check if error bit count is ready */
374 ret
= af9013_rd_reg_bits(state
, 0xd391, 4, 1, &buf
[0]);
379 dbg("%s: not ready", __func__
);
383 ret
= af9013_rd_regs(state
, 0xd387, buf
, 5);
387 state
->ber
= (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
388 state
->ucblocks
+= (buf
[4] << 8) | buf
[3];
392 dbg("%s: failed=%d", __func__
, ret
);
396 static int af9013_statistics_snr_start(struct dvb_frontend
*fe
)
398 struct af9013_state
*state
= fe
->demodulator_priv
;
404 ret
= af9013_wr_reg_bits(state
, 0xd2e1, 3, 1, 1);
410 dbg("%s: failed=%d", __func__
, ret
);
414 static int af9013_statistics_snr_result(struct dvb_frontend
*fe
)
416 struct af9013_state
*state
= fe
->demodulator_priv
;
420 const struct af9013_snr
*uninitialized_var(snr_lut
);
424 /* check if SNR ready */
425 ret
= af9013_rd_reg_bits(state
, 0xd2e1, 3, 1, &tmp
);
430 dbg("%s: not ready", __func__
);
435 ret
= af9013_rd_regs(state
, 0xd2e3, buf
, 3);
439 snr_val
= (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
441 /* read current modulation */
442 ret
= af9013_rd_reg(state
, 0xd3c1, &tmp
);
446 switch ((tmp
>> 6) & 3) {
448 len
= ARRAY_SIZE(qpsk_snr_lut
);
449 snr_lut
= qpsk_snr_lut
;
452 len
= ARRAY_SIZE(qam16_snr_lut
);
453 snr_lut
= qam16_snr_lut
;
456 len
= ARRAY_SIZE(qam64_snr_lut
);
457 snr_lut
= qam64_snr_lut
;
464 for (i
= 0; i
< len
; i
++) {
465 tmp
= snr_lut
[i
].snr
;
467 if (snr_val
< snr_lut
[i
].val
)
470 state
->snr
= tmp
* 10; /* dB/10 */
474 dbg("%s: failed=%d", __func__
, ret
);
478 static int af9013_statistics_signal_strength(struct dvb_frontend
*fe
)
480 struct af9013_state
*state
= fe
->demodulator_priv
;
482 u8 buf
[2], rf_gain
, if_gain
;
487 if (!state
->signal_strength_en
)
490 ret
= af9013_rd_regs(state
, 0xd07c, buf
, 2);
497 signal_strength
= (0xffff / \
498 (9 * (state
->rf_50
+ state
->if_50
) - \
499 11 * (state
->rf_80
+ state
->if_80
))) * \
500 (10 * (rf_gain
+ if_gain
) - \
501 11 * (state
->rf_80
+ state
->if_80
));
502 if (signal_strength
< 0)
504 else if (signal_strength
> 0xffff)
505 signal_strength
= 0xffff;
507 state
->signal_strength
= signal_strength
;
511 dbg("%s: failed=%d", __func__
, ret
);
515 static void af9013_statistics_work(struct work_struct
*work
)
518 struct af9013_state
*state
= container_of(work
,
519 struct af9013_state
, statistics_work
.work
);
520 unsigned int next_msec
;
522 /* update only signal strength when demod is not locked */
523 if (!(state
->fe_status
& FE_HAS_LOCK
)) {
524 state
->statistics_step
= 0;
529 switch (state
->statistics_step
) {
531 state
->statistics_step
= 0;
533 ret
= af9013_statistics_signal_strength(&state
->fe
);
534 state
->statistics_step
++;
538 ret
= af9013_statistics_snr_start(&state
->fe
);
539 state
->statistics_step
++;
543 ret
= af9013_statistics_ber_unc_start(&state
->fe
);
544 state
->statistics_step
++;
548 ret
= af9013_statistics_snr_result(&state
->fe
);
549 state
->statistics_step
++;
553 ret
= af9013_statistics_ber_unc_result(&state
->fe
);
554 state
->statistics_step
++;
559 schedule_delayed_work(&state
->statistics_work
,
560 msecs_to_jiffies(next_msec
));
565 static int af9013_get_tune_settings(struct dvb_frontend
*fe
,
566 struct dvb_frontend_tune_settings
*fesettings
)
568 fesettings
->min_delay_ms
= 800;
569 fesettings
->step_size
= 0;
570 fesettings
->max_drift
= 0;
575 static int af9013_set_frontend(struct dvb_frontend
*fe
)
577 struct af9013_state
*state
= fe
->demodulator_priv
;
578 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
579 int ret
, i
, sampling_freq
;
580 bool auto_mode
, spec_inv
;
582 u32 if_frequency
, freq_cw
;
584 dbg("%s: frequency=%d bandwidth_hz=%d", __func__
,
585 c
->frequency
, c
->bandwidth_hz
);
588 if (fe
->ops
.tuner_ops
.set_params
)
589 fe
->ops
.tuner_ops
.set_params(fe
);
591 /* program CFOE coefficients */
592 if (c
->bandwidth_hz
!= state
->bandwidth_hz
) {
593 for (i
= 0; i
< ARRAY_SIZE(coeff_lut
); i
++) {
594 if (coeff_lut
[i
].clock
== state
->config
.clock
&&
595 coeff_lut
[i
].bandwidth_hz
== c
->bandwidth_hz
) {
600 ret
= af9013_wr_regs(state
, 0xae00, coeff_lut
[i
].val
,
601 sizeof(coeff_lut
[i
].val
));
604 /* program frequency control */
605 if (c
->bandwidth_hz
!= state
->bandwidth_hz
|| state
->first_tune
) {
606 /* get used IF frequency */
607 if (fe
->ops
.tuner_ops
.get_if_frequency
)
608 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_frequency
);
610 if_frequency
= state
->config
.if_frequency
;
612 sampling_freq
= if_frequency
;
614 while (sampling_freq
> (state
->config
.clock
/ 2))
615 sampling_freq
-= state
->config
.clock
;
617 if (sampling_freq
< 0) {
619 spec_inv
= state
->config
.spec_inv
;
621 spec_inv
= !state
->config
.spec_inv
;
624 freq_cw
= af913_div(sampling_freq
, state
->config
.clock
, 23);
627 freq_cw
= 0x800000 - freq_cw
;
629 buf
[0] = (freq_cw
>> 0) & 0xff;
630 buf
[1] = (freq_cw
>> 8) & 0xff;
631 buf
[2] = (freq_cw
>> 16) & 0x7f;
633 freq_cw
= 0x800000 - freq_cw
;
635 buf
[3] = (freq_cw
>> 0) & 0xff;
636 buf
[4] = (freq_cw
>> 8) & 0xff;
637 buf
[5] = (freq_cw
>> 16) & 0x7f;
639 ret
= af9013_wr_regs(state
, 0xd140, buf
, 3);
643 ret
= af9013_wr_regs(state
, 0x9be7, buf
, 6);
648 /* clear TPS lock flag */
649 ret
= af9013_wr_reg_bits(state
, 0xd330, 3, 1, 1);
653 /* clear MPEG2 lock flag */
654 ret
= af9013_wr_reg_bits(state
, 0xd507, 6, 1, 0);
658 /* empty channel function */
659 ret
= af9013_wr_reg_bits(state
, 0x9bfe, 0, 1, 0);
663 /* empty DVB-T channel function */
664 ret
= af9013_wr_reg_bits(state
, 0x9bc2, 0, 1, 0);
668 /* transmission parameters */
672 switch (c
->transmission_mode
) {
673 case TRANSMISSION_MODE_AUTO
:
676 case TRANSMISSION_MODE_2K
:
678 case TRANSMISSION_MODE_8K
:
682 dbg("%s: invalid transmission_mode", __func__
);
686 switch (c
->guard_interval
) {
687 case GUARD_INTERVAL_AUTO
:
690 case GUARD_INTERVAL_1_32
:
692 case GUARD_INTERVAL_1_16
:
695 case GUARD_INTERVAL_1_8
:
698 case GUARD_INTERVAL_1_4
:
702 dbg("%s: invalid guard_interval", __func__
);
706 switch (c
->hierarchy
) {
722 dbg("%s: invalid hierarchy", __func__
);
726 switch (c
->modulation
) {
739 dbg("%s: invalid modulation", __func__
);
743 /* Use HP. How and which case we can switch to LP? */
746 switch (c
->code_rate_HP
) {
765 dbg("%s: invalid code_rate_HP", __func__
);
769 switch (c
->code_rate_LP
) {
790 dbg("%s: invalid code_rate_LP", __func__
);
794 switch (c
->bandwidth_hz
) {
804 dbg("%s: invalid bandwidth_hz", __func__
);
809 ret
= af9013_wr_regs(state
, 0xd3c0, buf
, 3);
814 /* clear easy mode flag */
815 ret
= af9013_wr_reg(state
, 0xaefd, 0);
819 dbg("%s: auto params", __func__
);
821 /* set easy mode flag */
822 ret
= af9013_wr_reg(state
, 0xaefd, 1);
826 ret
= af9013_wr_reg(state
, 0xaefe, 0);
830 dbg("%s: manual params", __func__
);
834 ret
= af9013_wr_reg(state
, 0xffff, 0);
838 state
->bandwidth_hz
= c
->bandwidth_hz
;
839 state
->set_frontend_jiffies
= jiffies
;
840 state
->first_tune
= false;
844 dbg("%s: failed=%d", __func__
, ret
);
848 static int af9013_get_frontend(struct dvb_frontend
*fe
)
850 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
851 struct af9013_state
*state
= fe
->demodulator_priv
;
857 ret
= af9013_rd_regs(state
, 0xd3c0, buf
, 3);
861 switch ((buf
[1] >> 6) & 3) {
863 c
->modulation
= QPSK
;
866 c
->modulation
= QAM_16
;
869 c
->modulation
= QAM_64
;
873 switch ((buf
[0] >> 0) & 3) {
875 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
878 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
881 switch ((buf
[0] >> 2) & 3) {
883 c
->guard_interval
= GUARD_INTERVAL_1_32
;
886 c
->guard_interval
= GUARD_INTERVAL_1_16
;
889 c
->guard_interval
= GUARD_INTERVAL_1_8
;
892 c
->guard_interval
= GUARD_INTERVAL_1_4
;
896 switch ((buf
[0] >> 4) & 7) {
898 c
->hierarchy
= HIERARCHY_NONE
;
901 c
->hierarchy
= HIERARCHY_1
;
904 c
->hierarchy
= HIERARCHY_2
;
907 c
->hierarchy
= HIERARCHY_4
;
911 switch ((buf
[2] >> 0) & 7) {
913 c
->code_rate_HP
= FEC_1_2
;
916 c
->code_rate_HP
= FEC_2_3
;
919 c
->code_rate_HP
= FEC_3_4
;
922 c
->code_rate_HP
= FEC_5_6
;
925 c
->code_rate_HP
= FEC_7_8
;
929 switch ((buf
[2] >> 3) & 7) {
931 c
->code_rate_LP
= FEC_1_2
;
934 c
->code_rate_LP
= FEC_2_3
;
937 c
->code_rate_LP
= FEC_3_4
;
940 c
->code_rate_LP
= FEC_5_6
;
943 c
->code_rate_LP
= FEC_7_8
;
947 switch ((buf
[1] >> 2) & 3) {
949 c
->bandwidth_hz
= 6000000;
952 c
->bandwidth_hz
= 7000000;
955 c
->bandwidth_hz
= 8000000;
961 dbg("%s: failed=%d", __func__
, ret
);
965 static int af9013_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
967 struct af9013_state
*state
= fe
->demodulator_priv
;
972 * Return status from the cache if it is younger than 2000ms with the
973 * exception of last tune is done during 4000ms.
975 if (time_is_after_jiffies(
976 state
->read_status_jiffies
+ msecs_to_jiffies(2000)) &&
977 time_is_before_jiffies(
978 state
->set_frontend_jiffies
+ msecs_to_jiffies(4000))
980 *status
= state
->fe_status
;
987 ret
= af9013_rd_reg_bits(state
, 0xd507, 6, 1, &tmp
);
992 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
993 FE_HAS_SYNC
| FE_HAS_LOCK
;
997 ret
= af9013_rd_reg_bits(state
, 0xd330, 3, 1, &tmp
);
1002 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
1006 state
->fe_status
= *status
;
1007 state
->read_status_jiffies
= jiffies
;
1011 dbg("%s: failed=%d", __func__
, ret
);
1015 static int af9013_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
1017 struct af9013_state
*state
= fe
->demodulator_priv
;
1022 static int af9013_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
1024 struct af9013_state
*state
= fe
->demodulator_priv
;
1025 *strength
= state
->signal_strength
;
1029 static int af9013_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
1031 struct af9013_state
*state
= fe
->demodulator_priv
;
1036 static int af9013_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
1038 struct af9013_state
*state
= fe
->demodulator_priv
;
1039 *ucblocks
= state
->ucblocks
;
1043 static int af9013_init(struct dvb_frontend
*fe
)
1045 struct af9013_state
*state
= fe
->demodulator_priv
;
1049 const struct af9013_reg_bit
*init
;
1051 dbg("%s", __func__
);
1054 ret
= af9013_power_ctrl(state
, 1);
1059 ret
= af9013_wr_reg(state
, 0xd73a, 0xa4);
1063 /* write API version to firmware */
1064 ret
= af9013_wr_regs(state
, 0x9bf2, state
->config
.api_version
, 4);
1068 /* program ADC control */
1069 switch (state
->config
.clock
) {
1070 case 28800000: /* 28.800 MHz */
1073 case 20480000: /* 20.480 MHz */
1076 case 28000000: /* 28.000 MHz */
1079 case 25000000: /* 25.000 MHz */
1083 err("invalid clock");
1087 adc_cw
= af913_div(state
->config
.clock
, 1000000ul, 19);
1088 buf
[0] = (adc_cw
>> 0) & 0xff;
1089 buf
[1] = (adc_cw
>> 8) & 0xff;
1090 buf
[2] = (adc_cw
>> 16) & 0xff;
1092 ret
= af9013_wr_regs(state
, 0xd180, buf
, 3);
1096 ret
= af9013_wr_reg_bits(state
, 0x9bd2, 0, 4, tmp
);
1100 /* set I2C master clock */
1101 ret
= af9013_wr_reg(state
, 0xd416, 0x14);
1106 ret
= af9013_wr_reg_bits(state
, 0xd700, 1, 1, 1);
1110 /* set no trigger */
1111 ret
= af9013_wr_reg_bits(state
, 0xd700, 2, 1, 0);
1115 /* set read-update bit for constellation */
1116 ret
= af9013_wr_reg_bits(state
, 0xd371, 1, 1, 1);
1120 /* settings for mp2if */
1121 if (state
->config
.ts_mode
== AF9013_TS_USB
) {
1122 /* AF9015 split PSB to 1.5k + 0.5k */
1123 ret
= af9013_wr_reg_bits(state
, 0xd50b, 2, 1, 1);
1127 /* AF9013 change the output bit to data7 */
1128 ret
= af9013_wr_reg_bits(state
, 0xd500, 3, 1, 1);
1132 /* AF9013 set mpeg to full speed */
1133 ret
= af9013_wr_reg_bits(state
, 0xd502, 4, 1, 1);
1138 ret
= af9013_wr_reg_bits(state
, 0xd520, 4, 1, 1);
1142 /* load OFSM settings */
1143 dbg("%s: load ofsm settings", __func__
);
1144 len
= ARRAY_SIZE(ofsm_init
);
1146 for (i
= 0; i
< len
; i
++) {
1147 ret
= af9013_wr_reg_bits(state
, init
[i
].addr
, init
[i
].pos
,
1148 init
[i
].len
, init
[i
].val
);
1153 /* load tuner specific settings */
1154 dbg("%s: load tuner specific settings", __func__
);
1155 switch (state
->config
.tuner
) {
1156 case AF9013_TUNER_MXL5003D
:
1157 len
= ARRAY_SIZE(tuner_init_mxl5003d
);
1158 init
= tuner_init_mxl5003d
;
1160 case AF9013_TUNER_MXL5005D
:
1161 case AF9013_TUNER_MXL5005R
:
1162 case AF9013_TUNER_MXL5007T
:
1163 len
= ARRAY_SIZE(tuner_init_mxl5005
);
1164 init
= tuner_init_mxl5005
;
1166 case AF9013_TUNER_ENV77H11D5
:
1167 len
= ARRAY_SIZE(tuner_init_env77h11d5
);
1168 init
= tuner_init_env77h11d5
;
1170 case AF9013_TUNER_MT2060
:
1171 len
= ARRAY_SIZE(tuner_init_mt2060
);
1172 init
= tuner_init_mt2060
;
1174 case AF9013_TUNER_MC44S803
:
1175 len
= ARRAY_SIZE(tuner_init_mc44s803
);
1176 init
= tuner_init_mc44s803
;
1178 case AF9013_TUNER_QT1010
:
1179 case AF9013_TUNER_QT1010A
:
1180 len
= ARRAY_SIZE(tuner_init_qt1010
);
1181 init
= tuner_init_qt1010
;
1183 case AF9013_TUNER_MT2060_2
:
1184 len
= ARRAY_SIZE(tuner_init_mt2060_2
);
1185 init
= tuner_init_mt2060_2
;
1187 case AF9013_TUNER_TDA18271
:
1188 case AF9013_TUNER_TDA18218
:
1189 len
= ARRAY_SIZE(tuner_init_tda18271
);
1190 init
= tuner_init_tda18271
;
1192 case AF9013_TUNER_UNKNOWN
:
1194 len
= ARRAY_SIZE(tuner_init_unknown
);
1195 init
= tuner_init_unknown
;
1199 for (i
= 0; i
< len
; i
++) {
1200 ret
= af9013_wr_reg_bits(state
, init
[i
].addr
, init
[i
].pos
,
1201 init
[i
].len
, init
[i
].val
);
1207 ret
= af9013_wr_reg_bits(state
, 0xd500, 1, 2, state
->config
.ts_mode
);
1211 /* enable lock led */
1212 ret
= af9013_wr_reg_bits(state
, 0xd730, 0, 1, 1);
1216 /* check if we support signal strength */
1217 if (!state
->signal_strength_en
) {
1218 ret
= af9013_rd_reg_bits(state
, 0x9bee, 0, 1,
1219 &state
->signal_strength_en
);
1224 /* read values needed for signal strength calculation */
1225 if (state
->signal_strength_en
&& !state
->rf_50
) {
1226 ret
= af9013_rd_reg(state
, 0x9bbd, &state
->rf_50
);
1230 ret
= af9013_rd_reg(state
, 0x9bd0, &state
->rf_80
);
1234 ret
= af9013_rd_reg(state
, 0x9be2, &state
->if_50
);
1238 ret
= af9013_rd_reg(state
, 0x9be4, &state
->if_80
);
1244 ret
= af9013_wr_reg(state
, 0xd2e2, 1);
1249 buf
[0] = (10000 >> 0) & 0xff;
1250 buf
[1] = (10000 >> 8) & 0xff;
1251 ret
= af9013_wr_regs(state
, 0xd385, buf
, 2);
1255 /* enable FEC monitor */
1256 ret
= af9013_wr_reg_bits(state
, 0xd392, 1, 1, 1);
1260 state
->first_tune
= true;
1261 schedule_delayed_work(&state
->statistics_work
, msecs_to_jiffies(400));
1265 dbg("%s: failed=%d", __func__
, ret
);
1269 static int af9013_sleep(struct dvb_frontend
*fe
)
1271 struct af9013_state
*state
= fe
->demodulator_priv
;
1274 dbg("%s", __func__
);
1276 /* stop statistics polling */
1277 cancel_delayed_work_sync(&state
->statistics_work
);
1279 /* disable lock led */
1280 ret
= af9013_wr_reg_bits(state
, 0xd730, 0, 1, 0);
1285 ret
= af9013_power_ctrl(state
, 0);
1291 dbg("%s: failed=%d", __func__
, ret
);
1295 static int af9013_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
1298 struct af9013_state
*state
= fe
->demodulator_priv
;
1300 dbg("%s: enable=%d", __func__
, enable
);
1302 /* gate already open or close */
1303 if (state
->i2c_gate_state
== enable
)
1306 if (state
->config
.ts_mode
== AF9013_TS_USB
)
1307 ret
= af9013_wr_reg_bits(state
, 0xd417, 3, 1, enable
);
1309 ret
= af9013_wr_reg_bits(state
, 0xd607, 2, 1, enable
);
1313 state
->i2c_gate_state
= enable
;
1317 dbg("%s: failed=%d", __func__
, ret
);
1321 static void af9013_release(struct dvb_frontend
*fe
)
1323 struct af9013_state
*state
= fe
->demodulator_priv
;
1327 static struct dvb_frontend_ops af9013_ops
;
1329 static int af9013_download_firmware(struct af9013_state
*state
)
1331 int i
, len
, remaining
, ret
;
1332 const struct firmware
*fw
;
1336 u8
*fw_file
= AF9013_DEFAULT_FIRMWARE
;
1339 /* check whether firmware is already running */
1340 ret
= af9013_rd_reg(state
, 0x98be, &val
);
1344 dbg("%s: firmware status=%02x", __func__
, val
);
1346 if (val
== 0x0c) /* fw is running, no need for download */
1349 info("found a '%s' in cold state, will try to load a firmware",
1350 af9013_ops
.info
.name
);
1352 /* request the firmware, this will block and timeout */
1353 ret
= request_firmware(&fw
, fw_file
, state
->i2c
->dev
.parent
);
1355 err("did not find the firmware file. (%s) "
1356 "Please see linux/Documentation/dvb/ for more details" \
1357 " on firmware-problems. (%d)",
1362 info("downloading firmware from file '%s'", fw_file
);
1365 for (i
= 0; i
< fw
->size
; i
++)
1366 checksum
+= fw
->data
[i
];
1368 fw_params
[0] = checksum
>> 8;
1369 fw_params
[1] = checksum
& 0xff;
1370 fw_params
[2] = fw
->size
>> 8;
1371 fw_params
[3] = fw
->size
& 0xff;
1373 /* write fw checksum & size */
1374 ret
= af9013_write_ofsm_regs(state
, 0x50fc,
1375 fw_params
, sizeof(fw_params
));
1379 #define FW_ADDR 0x5100 /* firmware start address */
1380 #define LEN_MAX 16 /* max packet size */
1381 for (remaining
= fw
->size
; remaining
> 0; remaining
-= LEN_MAX
) {
1386 ret
= af9013_write_ofsm_regs(state
,
1387 FW_ADDR
+ fw
->size
- remaining
,
1388 (u8
*) &fw
->data
[fw
->size
- remaining
], len
);
1390 err("firmware download failed:%d", ret
);
1395 /* request boot firmware */
1396 ret
= af9013_wr_reg(state
, 0xe205, 1);
1400 for (i
= 0; i
< 15; i
++) {
1403 /* check firmware status */
1404 ret
= af9013_rd_reg(state
, 0x98be, &val
);
1408 dbg("%s: firmware status=%02x", __func__
, val
);
1410 if (val
== 0x0c || val
== 0x04) /* success or fail */
1415 err("firmware did not run");
1417 } else if (val
!= 0x0c) {
1418 err("firmware boot timeout");
1423 release_firmware(fw
);
1427 info("found a '%s' in warm state.", af9013_ops
.info
.name
);
1431 struct dvb_frontend
*af9013_attach(const struct af9013_config
*config
,
1432 struct i2c_adapter
*i2c
)
1435 struct af9013_state
*state
= NULL
;
1438 /* allocate memory for the internal state */
1439 state
= kzalloc(sizeof(struct af9013_state
), GFP_KERNEL
);
1443 /* setup the state */
1445 memcpy(&state
->config
, config
, sizeof(struct af9013_config
));
1447 /* download firmware */
1448 if (state
->config
.ts_mode
!= AF9013_TS_USB
) {
1449 ret
= af9013_download_firmware(state
);
1454 /* firmware version */
1455 ret
= af9013_rd_regs(state
, 0x5103, buf
, 4);
1459 info("firmware version %d.%d.%d.%d", buf
[0], buf
[1], buf
[2], buf
[3]);
1462 for (i
= 0; i
< sizeof(state
->config
.gpio
); i
++) {
1463 ret
= af9013_set_gpio(state
, i
, state
->config
.gpio
[i
]);
1468 /* create dvb_frontend */
1469 memcpy(&state
->fe
.ops
, &af9013_ops
,
1470 sizeof(struct dvb_frontend_ops
));
1471 state
->fe
.demodulator_priv
= state
;
1473 INIT_DELAYED_WORK(&state
->statistics_work
, af9013_statistics_work
);
1480 EXPORT_SYMBOL(af9013_attach
);
1482 static struct dvb_frontend_ops af9013_ops
= {
1483 .delsys
= { SYS_DVBT
},
1485 .name
= "Afatech AF9013",
1486 .frequency_min
= 174000000,
1487 .frequency_max
= 862000000,
1488 .frequency_stepsize
= 250000,
1489 .frequency_tolerance
= 0,
1490 .caps
= FE_CAN_FEC_1_2
|
1500 FE_CAN_TRANSMISSION_MODE_AUTO
|
1501 FE_CAN_GUARD_INTERVAL_AUTO
|
1502 FE_CAN_HIERARCHY_AUTO
|
1507 .release
= af9013_release
,
1509 .init
= af9013_init
,
1510 .sleep
= af9013_sleep
,
1512 .get_tune_settings
= af9013_get_tune_settings
,
1513 .set_frontend
= af9013_set_frontend
,
1514 .get_frontend
= af9013_get_frontend
,
1516 .read_status
= af9013_read_status
,
1517 .read_snr
= af9013_read_snr
,
1518 .read_signal_strength
= af9013_read_signal_strength
,
1519 .read_ber
= af9013_read_ber
,
1520 .read_ucblocks
= af9013_read_ucblocks
,
1522 .i2c_gate_ctrl
= af9013_i2c_gate_ctrl
,
1525 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1526 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1527 MODULE_LICENSE("GPL");