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
)
517 struct af9013_state
*state
= container_of(work
,
518 struct af9013_state
, statistics_work
.work
);
519 unsigned int next_msec
;
521 /* update only signal strength when demod is not locked */
522 if (!(state
->fe_status
& FE_HAS_LOCK
)) {
523 state
->statistics_step
= 0;
528 switch (state
->statistics_step
) {
530 state
->statistics_step
= 0;
532 af9013_statistics_signal_strength(&state
->fe
);
533 state
->statistics_step
++;
537 af9013_statistics_snr_start(&state
->fe
);
538 state
->statistics_step
++;
542 af9013_statistics_ber_unc_start(&state
->fe
);
543 state
->statistics_step
++;
547 af9013_statistics_snr_result(&state
->fe
);
548 state
->statistics_step
++;
552 af9013_statistics_ber_unc_result(&state
->fe
);
553 state
->statistics_step
++;
558 schedule_delayed_work(&state
->statistics_work
,
559 msecs_to_jiffies(next_msec
));
562 static int af9013_get_tune_settings(struct dvb_frontend
*fe
,
563 struct dvb_frontend_tune_settings
*fesettings
)
565 fesettings
->min_delay_ms
= 800;
566 fesettings
->step_size
= 0;
567 fesettings
->max_drift
= 0;
572 static int af9013_set_frontend(struct dvb_frontend
*fe
)
574 struct af9013_state
*state
= fe
->demodulator_priv
;
575 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
576 int ret
, i
, sampling_freq
;
577 bool auto_mode
, spec_inv
;
579 u32 if_frequency
, freq_cw
;
581 dbg("%s: frequency=%d bandwidth_hz=%d", __func__
,
582 c
->frequency
, c
->bandwidth_hz
);
585 if (fe
->ops
.tuner_ops
.set_params
)
586 fe
->ops
.tuner_ops
.set_params(fe
);
588 /* program CFOE coefficients */
589 if (c
->bandwidth_hz
!= state
->bandwidth_hz
) {
590 for (i
= 0; i
< ARRAY_SIZE(coeff_lut
); i
++) {
591 if (coeff_lut
[i
].clock
== state
->config
.clock
&&
592 coeff_lut
[i
].bandwidth_hz
== c
->bandwidth_hz
) {
597 ret
= af9013_wr_regs(state
, 0xae00, coeff_lut
[i
].val
,
598 sizeof(coeff_lut
[i
].val
));
601 /* program frequency control */
602 if (c
->bandwidth_hz
!= state
->bandwidth_hz
|| state
->first_tune
) {
603 /* get used IF frequency */
604 if (fe
->ops
.tuner_ops
.get_if_frequency
)
605 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_frequency
);
607 if_frequency
= state
->config
.if_frequency
;
609 sampling_freq
= if_frequency
;
611 while (sampling_freq
> (state
->config
.clock
/ 2))
612 sampling_freq
-= state
->config
.clock
;
614 if (sampling_freq
< 0) {
616 spec_inv
= state
->config
.spec_inv
;
618 spec_inv
= !state
->config
.spec_inv
;
621 freq_cw
= af913_div(sampling_freq
, state
->config
.clock
, 23);
624 freq_cw
= 0x800000 - freq_cw
;
626 buf
[0] = (freq_cw
>> 0) & 0xff;
627 buf
[1] = (freq_cw
>> 8) & 0xff;
628 buf
[2] = (freq_cw
>> 16) & 0x7f;
630 freq_cw
= 0x800000 - freq_cw
;
632 buf
[3] = (freq_cw
>> 0) & 0xff;
633 buf
[4] = (freq_cw
>> 8) & 0xff;
634 buf
[5] = (freq_cw
>> 16) & 0x7f;
636 ret
= af9013_wr_regs(state
, 0xd140, buf
, 3);
640 ret
= af9013_wr_regs(state
, 0x9be7, buf
, 6);
645 /* clear TPS lock flag */
646 ret
= af9013_wr_reg_bits(state
, 0xd330, 3, 1, 1);
650 /* clear MPEG2 lock flag */
651 ret
= af9013_wr_reg_bits(state
, 0xd507, 6, 1, 0);
655 /* empty channel function */
656 ret
= af9013_wr_reg_bits(state
, 0x9bfe, 0, 1, 0);
660 /* empty DVB-T channel function */
661 ret
= af9013_wr_reg_bits(state
, 0x9bc2, 0, 1, 0);
665 /* transmission parameters */
669 switch (c
->transmission_mode
) {
670 case TRANSMISSION_MODE_AUTO
:
673 case TRANSMISSION_MODE_2K
:
675 case TRANSMISSION_MODE_8K
:
679 dbg("%s: invalid transmission_mode", __func__
);
683 switch (c
->guard_interval
) {
684 case GUARD_INTERVAL_AUTO
:
687 case GUARD_INTERVAL_1_32
:
689 case GUARD_INTERVAL_1_16
:
692 case GUARD_INTERVAL_1_8
:
695 case GUARD_INTERVAL_1_4
:
699 dbg("%s: invalid guard_interval", __func__
);
703 switch (c
->hierarchy
) {
719 dbg("%s: invalid hierarchy", __func__
);
723 switch (c
->modulation
) {
736 dbg("%s: invalid modulation", __func__
);
740 /* Use HP. How and which case we can switch to LP? */
743 switch (c
->code_rate_HP
) {
762 dbg("%s: invalid code_rate_HP", __func__
);
766 switch (c
->code_rate_LP
) {
787 dbg("%s: invalid code_rate_LP", __func__
);
791 switch (c
->bandwidth_hz
) {
801 dbg("%s: invalid bandwidth_hz", __func__
);
806 ret
= af9013_wr_regs(state
, 0xd3c0, buf
, 3);
811 /* clear easy mode flag */
812 ret
= af9013_wr_reg(state
, 0xaefd, 0);
816 dbg("%s: auto params", __func__
);
818 /* set easy mode flag */
819 ret
= af9013_wr_reg(state
, 0xaefd, 1);
823 ret
= af9013_wr_reg(state
, 0xaefe, 0);
827 dbg("%s: manual params", __func__
);
831 ret
= af9013_wr_reg(state
, 0xffff, 0);
835 state
->bandwidth_hz
= c
->bandwidth_hz
;
836 state
->set_frontend_jiffies
= jiffies
;
837 state
->first_tune
= false;
841 dbg("%s: failed=%d", __func__
, ret
);
845 static int af9013_get_frontend(struct dvb_frontend
*fe
)
847 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
848 struct af9013_state
*state
= fe
->demodulator_priv
;
854 ret
= af9013_rd_regs(state
, 0xd3c0, buf
, 3);
858 switch ((buf
[1] >> 6) & 3) {
860 c
->modulation
= QPSK
;
863 c
->modulation
= QAM_16
;
866 c
->modulation
= QAM_64
;
870 switch ((buf
[0] >> 0) & 3) {
872 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
875 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
878 switch ((buf
[0] >> 2) & 3) {
880 c
->guard_interval
= GUARD_INTERVAL_1_32
;
883 c
->guard_interval
= GUARD_INTERVAL_1_16
;
886 c
->guard_interval
= GUARD_INTERVAL_1_8
;
889 c
->guard_interval
= GUARD_INTERVAL_1_4
;
893 switch ((buf
[0] >> 4) & 7) {
895 c
->hierarchy
= HIERARCHY_NONE
;
898 c
->hierarchy
= HIERARCHY_1
;
901 c
->hierarchy
= HIERARCHY_2
;
904 c
->hierarchy
= HIERARCHY_4
;
908 switch ((buf
[2] >> 0) & 7) {
910 c
->code_rate_HP
= FEC_1_2
;
913 c
->code_rate_HP
= FEC_2_3
;
916 c
->code_rate_HP
= FEC_3_4
;
919 c
->code_rate_HP
= FEC_5_6
;
922 c
->code_rate_HP
= FEC_7_8
;
926 switch ((buf
[2] >> 3) & 7) {
928 c
->code_rate_LP
= FEC_1_2
;
931 c
->code_rate_LP
= FEC_2_3
;
934 c
->code_rate_LP
= FEC_3_4
;
937 c
->code_rate_LP
= FEC_5_6
;
940 c
->code_rate_LP
= FEC_7_8
;
944 switch ((buf
[1] >> 2) & 3) {
946 c
->bandwidth_hz
= 6000000;
949 c
->bandwidth_hz
= 7000000;
952 c
->bandwidth_hz
= 8000000;
958 dbg("%s: failed=%d", __func__
, ret
);
962 static int af9013_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
964 struct af9013_state
*state
= fe
->demodulator_priv
;
969 * Return status from the cache if it is younger than 2000ms with the
970 * exception of last tune is done during 4000ms.
972 if (time_is_after_jiffies(
973 state
->read_status_jiffies
+ msecs_to_jiffies(2000)) &&
974 time_is_before_jiffies(
975 state
->set_frontend_jiffies
+ msecs_to_jiffies(4000))
977 *status
= state
->fe_status
;
984 ret
= af9013_rd_reg_bits(state
, 0xd507, 6, 1, &tmp
);
989 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
990 FE_HAS_SYNC
| FE_HAS_LOCK
;
994 ret
= af9013_rd_reg_bits(state
, 0xd330, 3, 1, &tmp
);
999 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
1003 state
->fe_status
= *status
;
1004 state
->read_status_jiffies
= jiffies
;
1008 dbg("%s: failed=%d", __func__
, ret
);
1012 static int af9013_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
1014 struct af9013_state
*state
= fe
->demodulator_priv
;
1019 static int af9013_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
1021 struct af9013_state
*state
= fe
->demodulator_priv
;
1022 *strength
= state
->signal_strength
;
1026 static int af9013_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
1028 struct af9013_state
*state
= fe
->demodulator_priv
;
1033 static int af9013_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
1035 struct af9013_state
*state
= fe
->demodulator_priv
;
1036 *ucblocks
= state
->ucblocks
;
1040 static int af9013_init(struct dvb_frontend
*fe
)
1042 struct af9013_state
*state
= fe
->demodulator_priv
;
1046 const struct af9013_reg_bit
*init
;
1048 dbg("%s", __func__
);
1051 ret
= af9013_power_ctrl(state
, 1);
1056 ret
= af9013_wr_reg(state
, 0xd73a, 0xa4);
1060 /* write API version to firmware */
1061 ret
= af9013_wr_regs(state
, 0x9bf2, state
->config
.api_version
, 4);
1065 /* program ADC control */
1066 switch (state
->config
.clock
) {
1067 case 28800000: /* 28.800 MHz */
1070 case 20480000: /* 20.480 MHz */
1073 case 28000000: /* 28.000 MHz */
1076 case 25000000: /* 25.000 MHz */
1080 err("invalid clock");
1084 adc_cw
= af913_div(state
->config
.clock
, 1000000ul, 19);
1085 buf
[0] = (adc_cw
>> 0) & 0xff;
1086 buf
[1] = (adc_cw
>> 8) & 0xff;
1087 buf
[2] = (adc_cw
>> 16) & 0xff;
1089 ret
= af9013_wr_regs(state
, 0xd180, buf
, 3);
1093 ret
= af9013_wr_reg_bits(state
, 0x9bd2, 0, 4, tmp
);
1097 /* set I2C master clock */
1098 ret
= af9013_wr_reg(state
, 0xd416, 0x14);
1103 ret
= af9013_wr_reg_bits(state
, 0xd700, 1, 1, 1);
1107 /* set no trigger */
1108 ret
= af9013_wr_reg_bits(state
, 0xd700, 2, 1, 0);
1112 /* set read-update bit for constellation */
1113 ret
= af9013_wr_reg_bits(state
, 0xd371, 1, 1, 1);
1117 /* settings for mp2if */
1118 if (state
->config
.ts_mode
== AF9013_TS_USB
) {
1119 /* AF9015 split PSB to 1.5k + 0.5k */
1120 ret
= af9013_wr_reg_bits(state
, 0xd50b, 2, 1, 1);
1124 /* AF9013 change the output bit to data7 */
1125 ret
= af9013_wr_reg_bits(state
, 0xd500, 3, 1, 1);
1129 /* AF9013 set mpeg to full speed */
1130 ret
= af9013_wr_reg_bits(state
, 0xd502, 4, 1, 1);
1135 ret
= af9013_wr_reg_bits(state
, 0xd520, 4, 1, 1);
1139 /* load OFSM settings */
1140 dbg("%s: load ofsm settings", __func__
);
1141 len
= ARRAY_SIZE(ofsm_init
);
1143 for (i
= 0; i
< len
; i
++) {
1144 ret
= af9013_wr_reg_bits(state
, init
[i
].addr
, init
[i
].pos
,
1145 init
[i
].len
, init
[i
].val
);
1150 /* load tuner specific settings */
1151 dbg("%s: load tuner specific settings", __func__
);
1152 switch (state
->config
.tuner
) {
1153 case AF9013_TUNER_MXL5003D
:
1154 len
= ARRAY_SIZE(tuner_init_mxl5003d
);
1155 init
= tuner_init_mxl5003d
;
1157 case AF9013_TUNER_MXL5005D
:
1158 case AF9013_TUNER_MXL5005R
:
1159 case AF9013_TUNER_MXL5007T
:
1160 len
= ARRAY_SIZE(tuner_init_mxl5005
);
1161 init
= tuner_init_mxl5005
;
1163 case AF9013_TUNER_ENV77H11D5
:
1164 len
= ARRAY_SIZE(tuner_init_env77h11d5
);
1165 init
= tuner_init_env77h11d5
;
1167 case AF9013_TUNER_MT2060
:
1168 len
= ARRAY_SIZE(tuner_init_mt2060
);
1169 init
= tuner_init_mt2060
;
1171 case AF9013_TUNER_MC44S803
:
1172 len
= ARRAY_SIZE(tuner_init_mc44s803
);
1173 init
= tuner_init_mc44s803
;
1175 case AF9013_TUNER_QT1010
:
1176 case AF9013_TUNER_QT1010A
:
1177 len
= ARRAY_SIZE(tuner_init_qt1010
);
1178 init
= tuner_init_qt1010
;
1180 case AF9013_TUNER_MT2060_2
:
1181 len
= ARRAY_SIZE(tuner_init_mt2060_2
);
1182 init
= tuner_init_mt2060_2
;
1184 case AF9013_TUNER_TDA18271
:
1185 case AF9013_TUNER_TDA18218
:
1186 len
= ARRAY_SIZE(tuner_init_tda18271
);
1187 init
= tuner_init_tda18271
;
1189 case AF9013_TUNER_UNKNOWN
:
1191 len
= ARRAY_SIZE(tuner_init_unknown
);
1192 init
= tuner_init_unknown
;
1196 for (i
= 0; i
< len
; i
++) {
1197 ret
= af9013_wr_reg_bits(state
, init
[i
].addr
, init
[i
].pos
,
1198 init
[i
].len
, init
[i
].val
);
1204 ret
= af9013_wr_reg_bits(state
, 0xd500, 1, 2, state
->config
.ts_mode
);
1208 /* enable lock led */
1209 ret
= af9013_wr_reg_bits(state
, 0xd730, 0, 1, 1);
1213 /* check if we support signal strength */
1214 if (!state
->signal_strength_en
) {
1215 ret
= af9013_rd_reg_bits(state
, 0x9bee, 0, 1,
1216 &state
->signal_strength_en
);
1221 /* read values needed for signal strength calculation */
1222 if (state
->signal_strength_en
&& !state
->rf_50
) {
1223 ret
= af9013_rd_reg(state
, 0x9bbd, &state
->rf_50
);
1227 ret
= af9013_rd_reg(state
, 0x9bd0, &state
->rf_80
);
1231 ret
= af9013_rd_reg(state
, 0x9be2, &state
->if_50
);
1235 ret
= af9013_rd_reg(state
, 0x9be4, &state
->if_80
);
1241 ret
= af9013_wr_reg(state
, 0xd2e2, 1);
1246 buf
[0] = (10000 >> 0) & 0xff;
1247 buf
[1] = (10000 >> 8) & 0xff;
1248 ret
= af9013_wr_regs(state
, 0xd385, buf
, 2);
1252 /* enable FEC monitor */
1253 ret
= af9013_wr_reg_bits(state
, 0xd392, 1, 1, 1);
1257 state
->first_tune
= true;
1258 schedule_delayed_work(&state
->statistics_work
, msecs_to_jiffies(400));
1262 dbg("%s: failed=%d", __func__
, ret
);
1266 static int af9013_sleep(struct dvb_frontend
*fe
)
1268 struct af9013_state
*state
= fe
->demodulator_priv
;
1271 dbg("%s", __func__
);
1273 /* stop statistics polling */
1274 cancel_delayed_work_sync(&state
->statistics_work
);
1276 /* disable lock led */
1277 ret
= af9013_wr_reg_bits(state
, 0xd730, 0, 1, 0);
1282 ret
= af9013_power_ctrl(state
, 0);
1288 dbg("%s: failed=%d", __func__
, ret
);
1292 static int af9013_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
1295 struct af9013_state
*state
= fe
->demodulator_priv
;
1297 dbg("%s: enable=%d", __func__
, enable
);
1299 /* gate already open or close */
1300 if (state
->i2c_gate_state
== enable
)
1303 if (state
->config
.ts_mode
== AF9013_TS_USB
)
1304 ret
= af9013_wr_reg_bits(state
, 0xd417, 3, 1, enable
);
1306 ret
= af9013_wr_reg_bits(state
, 0xd607, 2, 1, enable
);
1310 state
->i2c_gate_state
= enable
;
1314 dbg("%s: failed=%d", __func__
, ret
);
1318 static void af9013_release(struct dvb_frontend
*fe
)
1320 struct af9013_state
*state
= fe
->demodulator_priv
;
1324 static struct dvb_frontend_ops af9013_ops
;
1326 static int af9013_download_firmware(struct af9013_state
*state
)
1328 int i
, len
, remaining
, ret
;
1329 const struct firmware
*fw
;
1333 u8
*fw_file
= AF9013_DEFAULT_FIRMWARE
;
1336 /* check whether firmware is already running */
1337 ret
= af9013_rd_reg(state
, 0x98be, &val
);
1341 dbg("%s: firmware status=%02x", __func__
, val
);
1343 if (val
== 0x0c) /* fw is running, no need for download */
1346 info("found a '%s' in cold state, will try to load a firmware",
1347 af9013_ops
.info
.name
);
1349 /* request the firmware, this will block and timeout */
1350 ret
= request_firmware(&fw
, fw_file
, state
->i2c
->dev
.parent
);
1352 err("did not find the firmware file. (%s) "
1353 "Please see linux/Documentation/dvb/ for more details" \
1354 " on firmware-problems. (%d)",
1359 info("downloading firmware from file '%s'", fw_file
);
1362 for (i
= 0; i
< fw
->size
; i
++)
1363 checksum
+= fw
->data
[i
];
1365 fw_params
[0] = checksum
>> 8;
1366 fw_params
[1] = checksum
& 0xff;
1367 fw_params
[2] = fw
->size
>> 8;
1368 fw_params
[3] = fw
->size
& 0xff;
1370 /* write fw checksum & size */
1371 ret
= af9013_write_ofsm_regs(state
, 0x50fc,
1372 fw_params
, sizeof(fw_params
));
1376 #define FW_ADDR 0x5100 /* firmware start address */
1377 #define LEN_MAX 16 /* max packet size */
1378 for (remaining
= fw
->size
; remaining
> 0; remaining
-= LEN_MAX
) {
1383 ret
= af9013_write_ofsm_regs(state
,
1384 FW_ADDR
+ fw
->size
- remaining
,
1385 (u8
*) &fw
->data
[fw
->size
- remaining
], len
);
1387 err("firmware download failed:%d", ret
);
1392 /* request boot firmware */
1393 ret
= af9013_wr_reg(state
, 0xe205, 1);
1397 for (i
= 0; i
< 15; i
++) {
1400 /* check firmware status */
1401 ret
= af9013_rd_reg(state
, 0x98be, &val
);
1405 dbg("%s: firmware status=%02x", __func__
, val
);
1407 if (val
== 0x0c || val
== 0x04) /* success or fail */
1412 err("firmware did not run");
1414 } else if (val
!= 0x0c) {
1415 err("firmware boot timeout");
1420 release_firmware(fw
);
1424 info("found a '%s' in warm state.", af9013_ops
.info
.name
);
1428 struct dvb_frontend
*af9013_attach(const struct af9013_config
*config
,
1429 struct i2c_adapter
*i2c
)
1432 struct af9013_state
*state
= NULL
;
1435 /* allocate memory for the internal state */
1436 state
= kzalloc(sizeof(struct af9013_state
), GFP_KERNEL
);
1440 /* setup the state */
1442 memcpy(&state
->config
, config
, sizeof(struct af9013_config
));
1444 /* download firmware */
1445 if (state
->config
.ts_mode
!= AF9013_TS_USB
) {
1446 ret
= af9013_download_firmware(state
);
1451 /* firmware version */
1452 ret
= af9013_rd_regs(state
, 0x5103, buf
, 4);
1456 info("firmware version %d.%d.%d.%d", buf
[0], buf
[1], buf
[2], buf
[3]);
1459 for (i
= 0; i
< sizeof(state
->config
.gpio
); i
++) {
1460 ret
= af9013_set_gpio(state
, i
, state
->config
.gpio
[i
]);
1465 /* create dvb_frontend */
1466 memcpy(&state
->fe
.ops
, &af9013_ops
,
1467 sizeof(struct dvb_frontend_ops
));
1468 state
->fe
.demodulator_priv
= state
;
1470 INIT_DELAYED_WORK(&state
->statistics_work
, af9013_statistics_work
);
1477 EXPORT_SYMBOL(af9013_attach
);
1479 static struct dvb_frontend_ops af9013_ops
= {
1480 .delsys
= { SYS_DVBT
},
1482 .name
= "Afatech AF9013",
1483 .frequency_min
= 174000000,
1484 .frequency_max
= 862000000,
1485 .frequency_stepsize
= 250000,
1486 .frequency_tolerance
= 0,
1487 .caps
= FE_CAN_FEC_1_2
|
1497 FE_CAN_TRANSMISSION_MODE_AUTO
|
1498 FE_CAN_GUARD_INTERVAL_AUTO
|
1499 FE_CAN_HIERARCHY_AUTO
|
1504 .release
= af9013_release
,
1506 .init
= af9013_init
,
1507 .sleep
= af9013_sleep
,
1509 .get_tune_settings
= af9013_get_tune_settings
,
1510 .set_frontend
= af9013_set_frontend
,
1511 .get_frontend
= af9013_get_frontend
,
1513 .read_status
= af9013_read_status
,
1514 .read_snr
= af9013_read_snr
,
1515 .read_signal_strength
= af9013_read_signal_strength
,
1516 .read_ber
= af9013_read_ber
,
1517 .read_ucblocks
= af9013_read_ucblocks
,
1519 .i2c_gate_ctrl
= af9013_i2c_gate_ctrl
,
1522 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1523 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1524 MODULE_LICENSE("GPL");