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"
27 /* Max transfer size done by I2C transfer functions */
28 #define MAX_XFER_SIZE 64
31 struct i2c_adapter
*i2c
;
32 struct dvb_frontend fe
;
33 struct af9013_config config
;
35 /* tuner/demod RF and IF AGC limits used for signal strength calc */
36 u8 signal_strength_en
, rf_50
, rf_80
, if_50
, if_80
;
42 enum fe_status fe_status
;
43 unsigned long set_frontend_jiffies
;
44 unsigned long read_status_jiffies
;
47 unsigned int statistics_step
:3;
48 struct delayed_work statistics_work
;
51 /* write multiple registers */
52 static int af9013_wr_regs_i2c(struct af9013_state
*priv
, u8 mbox
, u16 reg
,
53 const u8
*val
, int len
)
56 u8 buf
[MAX_XFER_SIZE
];
57 struct i2c_msg msg
[1] = {
59 .addr
= priv
->config
.i2c_addr
,
66 if (3 + len
> sizeof(buf
)) {
67 dev_warn(&priv
->i2c
->dev
,
68 "%s: i2c wr reg=%04x: len=%d is too big!\n",
69 KBUILD_MODNAME
, reg
, len
);
73 buf
[0] = (reg
>> 8) & 0xff;
74 buf
[1] = (reg
>> 0) & 0xff;
76 memcpy(&buf
[3], val
, len
);
78 ret
= i2c_transfer(priv
->i2c
, msg
, 1);
82 dev_warn(&priv
->i2c
->dev
, "%s: i2c wr failed=%d reg=%04x " \
83 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
89 /* read multiple registers */
90 static int af9013_rd_regs_i2c(struct af9013_state
*priv
, u8 mbox
, u16 reg
,
95 struct i2c_msg msg
[2] = {
97 .addr
= priv
->config
.i2c_addr
,
102 .addr
= priv
->config
.i2c_addr
,
109 buf
[0] = (reg
>> 8) & 0xff;
110 buf
[1] = (reg
>> 0) & 0xff;
113 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
117 dev_warn(&priv
->i2c
->dev
, "%s: i2c rd failed=%d reg=%04x " \
118 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
124 /* write multiple registers */
125 static int af9013_wr_regs(struct af9013_state
*priv
, u16 reg
, const u8
*val
,
129 u8 mbox
= (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0);
131 if ((priv
->config
.ts_mode
== AF9013_TS_USB
) &&
132 ((reg
& 0xff00) != 0xff00) && ((reg
& 0xff00) != 0xae00)) {
133 mbox
|= ((len
- 1) << 2);
134 ret
= af9013_wr_regs_i2c(priv
, mbox
, reg
, val
, len
);
136 for (i
= 0; i
< len
; i
++) {
137 ret
= af9013_wr_regs_i2c(priv
, mbox
, reg
+i
, val
+i
, 1);
147 /* read multiple registers */
148 static int af9013_rd_regs(struct af9013_state
*priv
, u16 reg
, u8
*val
, int len
)
151 u8 mbox
= (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0);
153 if ((priv
->config
.ts_mode
== AF9013_TS_USB
) &&
154 ((reg
& 0xff00) != 0xff00) && ((reg
& 0xff00) != 0xae00)) {
155 mbox
|= ((len
- 1) << 2);
156 ret
= af9013_rd_regs_i2c(priv
, mbox
, reg
, val
, len
);
158 for (i
= 0; i
< len
; i
++) {
159 ret
= af9013_rd_regs_i2c(priv
, mbox
, reg
+i
, val
+i
, 1);
169 /* write single register */
170 static int af9013_wr_reg(struct af9013_state
*priv
, u16 reg
, u8 val
)
172 return af9013_wr_regs(priv
, reg
, &val
, 1);
175 /* read single register */
176 static int af9013_rd_reg(struct af9013_state
*priv
, u16 reg
, u8
*val
)
178 return af9013_rd_regs(priv
, reg
, val
, 1);
181 static int af9013_write_ofsm_regs(struct af9013_state
*state
, u16 reg
, u8
*val
,
184 u8 mbox
= (1 << 7)|(1 << 6)|((len
- 1) << 2)|(1 << 1)|(1 << 0);
185 return af9013_wr_regs_i2c(state
, mbox
, reg
, val
, len
);
188 static int af9013_wr_reg_bits(struct af9013_state
*state
, u16 reg
, int pos
,
194 /* no need for read if whole reg is written */
196 ret
= af9013_rd_reg(state
, reg
, &tmp
);
200 mask
= (0xff >> (8 - len
)) << pos
;
206 return af9013_wr_reg(state
, reg
, val
);
209 static int af9013_rd_reg_bits(struct af9013_state
*state
, u16 reg
, int pos
,
215 ret
= af9013_rd_reg(state
, reg
, &tmp
);
220 *val
&= (0xff >> (8 - len
));
225 static int af9013_set_gpio(struct af9013_state
*state
, u8 gpio
, u8 gpioval
)
231 dev_dbg(&state
->i2c
->dev
, "%s: gpio=%d gpioval=%02x\n",
232 __func__
, gpio
, gpioval
);
235 * GPIO0 & GPIO1 0xd735
236 * GPIO2 & GPIO3 0xd736
250 dev_err(&state
->i2c
->dev
, "%s: invalid gpio=%d\n",
251 KBUILD_MODNAME
, gpio
);
268 ret
= af9013_wr_reg_bits(state
, addr
, pos
, 4, gpioval
);
274 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
278 static u32
af9013_div(struct af9013_state
*state
, u32 a
, u32 b
, u32 x
)
282 dev_dbg(&state
->i2c
->dev
, "%s: a=%d b=%d x=%d\n", __func__
, a
, b
, x
);
289 for (i
= 0; i
< x
; i
++) {
297 r
= (c
<< (u32
)x
) + r
;
299 dev_dbg(&state
->i2c
->dev
, "%s: a=%d b=%d x=%d r=%d r=%x\n",
300 __func__
, a
, b
, x
, r
, r
);
305 static int af9013_power_ctrl(struct af9013_state
*state
, u8 onoff
)
310 dev_dbg(&state
->i2c
->dev
, "%s: onoff=%d\n", __func__
, onoff
);
313 ret
= af9013_wr_reg_bits(state
, 0xd417, 4, 1, 1);
317 /* start reset mechanism */
318 ret
= af9013_wr_reg(state
, 0xaeff, 1);
322 /* wait reset performs */
323 for (i
= 0; i
< 150; i
++) {
324 ret
= af9013_rd_reg_bits(state
, 0xd417, 1, 1, &tmp
);
329 break; /* reset done */
331 usleep_range(5000, 25000);
339 ret
= af9013_wr_reg_bits(state
, 0xd417, 1, 1, 0);
344 ret
= af9013_wr_reg_bits(state
, 0xd417, 4, 1, 0);
347 ret
= af9013_wr_reg_bits(state
, 0xd73a, 3, 1, 0);
350 ret
= af9013_wr_reg_bits(state
, 0xd73a, 3, 1, 1);
355 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
359 static int af9013_statistics_ber_unc_start(struct dvb_frontend
*fe
)
361 struct af9013_state
*state
= fe
->demodulator_priv
;
364 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
366 /* reset and start BER counter */
367 ret
= af9013_wr_reg_bits(state
, 0xd391, 4, 1, 1);
373 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
377 static int af9013_statistics_ber_unc_result(struct dvb_frontend
*fe
)
379 struct af9013_state
*state
= fe
->demodulator_priv
;
383 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
385 /* check if error bit count is ready */
386 ret
= af9013_rd_reg_bits(state
, 0xd391, 4, 1, &buf
[0]);
391 dev_dbg(&state
->i2c
->dev
, "%s: not ready\n", __func__
);
395 ret
= af9013_rd_regs(state
, 0xd387, buf
, 5);
399 state
->ber
= (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
400 state
->ucblocks
+= (buf
[4] << 8) | buf
[3];
404 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
408 static int af9013_statistics_snr_start(struct dvb_frontend
*fe
)
410 struct af9013_state
*state
= fe
->demodulator_priv
;
413 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
416 ret
= af9013_wr_reg_bits(state
, 0xd2e1, 3, 1, 1);
422 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
426 static int af9013_statistics_snr_result(struct dvb_frontend
*fe
)
428 struct af9013_state
*state
= fe
->demodulator_priv
;
432 const struct af9013_snr
*uninitialized_var(snr_lut
);
434 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
436 /* check if SNR ready */
437 ret
= af9013_rd_reg_bits(state
, 0xd2e1, 3, 1, &tmp
);
442 dev_dbg(&state
->i2c
->dev
, "%s: not ready\n", __func__
);
447 ret
= af9013_rd_regs(state
, 0xd2e3, buf
, 3);
451 snr_val
= (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
453 /* read current modulation */
454 ret
= af9013_rd_reg(state
, 0xd3c1, &tmp
);
458 switch ((tmp
>> 6) & 3) {
460 len
= ARRAY_SIZE(qpsk_snr_lut
);
461 snr_lut
= qpsk_snr_lut
;
464 len
= ARRAY_SIZE(qam16_snr_lut
);
465 snr_lut
= qam16_snr_lut
;
468 len
= ARRAY_SIZE(qam64_snr_lut
);
469 snr_lut
= qam64_snr_lut
;
475 for (i
= 0; i
< len
; i
++) {
476 tmp
= snr_lut
[i
].snr
;
478 if (snr_val
< snr_lut
[i
].val
)
481 state
->snr
= tmp
* 10; /* dB/10 */
485 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
489 static int af9013_statistics_signal_strength(struct dvb_frontend
*fe
)
491 struct af9013_state
*state
= fe
->demodulator_priv
;
493 u8 buf
[2], rf_gain
, if_gain
;
496 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
498 if (!state
->signal_strength_en
)
501 ret
= af9013_rd_regs(state
, 0xd07c, buf
, 2);
508 signal_strength
= (0xffff / \
509 (9 * (state
->rf_50
+ state
->if_50
) - \
510 11 * (state
->rf_80
+ state
->if_80
))) * \
511 (10 * (rf_gain
+ if_gain
) - \
512 11 * (state
->rf_80
+ state
->if_80
));
513 if (signal_strength
< 0)
515 else if (signal_strength
> 0xffff)
516 signal_strength
= 0xffff;
518 state
->signal_strength
= signal_strength
;
522 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
526 static void af9013_statistics_work(struct work_struct
*work
)
528 struct af9013_state
*state
= container_of(work
,
529 struct af9013_state
, statistics_work
.work
);
530 unsigned int next_msec
;
532 /* update only signal strength when demod is not locked */
533 if (!(state
->fe_status
& FE_HAS_LOCK
)) {
534 state
->statistics_step
= 0;
539 switch (state
->statistics_step
) {
541 state
->statistics_step
= 0;
543 af9013_statistics_signal_strength(&state
->fe
);
544 state
->statistics_step
++;
548 af9013_statistics_snr_start(&state
->fe
);
549 state
->statistics_step
++;
553 af9013_statistics_ber_unc_start(&state
->fe
);
554 state
->statistics_step
++;
558 af9013_statistics_snr_result(&state
->fe
);
559 state
->statistics_step
++;
563 af9013_statistics_ber_unc_result(&state
->fe
);
564 state
->statistics_step
++;
569 schedule_delayed_work(&state
->statistics_work
,
570 msecs_to_jiffies(next_msec
));
573 static int af9013_get_tune_settings(struct dvb_frontend
*fe
,
574 struct dvb_frontend_tune_settings
*fesettings
)
576 fesettings
->min_delay_ms
= 800;
577 fesettings
->step_size
= 0;
578 fesettings
->max_drift
= 0;
583 static int af9013_set_frontend(struct dvb_frontend
*fe
)
585 struct af9013_state
*state
= fe
->demodulator_priv
;
586 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
587 int ret
, i
, sampling_freq
;
588 bool auto_mode
, spec_inv
;
590 u32 if_frequency
, freq_cw
;
592 dev_dbg(&state
->i2c
->dev
, "%s: frequency=%d bandwidth_hz=%d\n",
593 __func__
, c
->frequency
, c
->bandwidth_hz
);
596 if (fe
->ops
.tuner_ops
.set_params
)
597 fe
->ops
.tuner_ops
.set_params(fe
);
599 /* program CFOE coefficients */
600 if (c
->bandwidth_hz
!= state
->bandwidth_hz
) {
601 for (i
= 0; i
< ARRAY_SIZE(coeff_lut
); i
++) {
602 if (coeff_lut
[i
].clock
== state
->config
.clock
&&
603 coeff_lut
[i
].bandwidth_hz
== c
->bandwidth_hz
) {
608 /* Return an error if can't find bandwidth or the right clock */
609 if (i
== ARRAY_SIZE(coeff_lut
))
612 ret
= af9013_wr_regs(state
, 0xae00, coeff_lut
[i
].val
,
613 sizeof(coeff_lut
[i
].val
));
616 /* program frequency control */
617 if (c
->bandwidth_hz
!= state
->bandwidth_hz
|| state
->first_tune
) {
618 /* get used IF frequency */
619 if (fe
->ops
.tuner_ops
.get_if_frequency
)
620 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_frequency
);
622 if_frequency
= state
->config
.if_frequency
;
624 dev_dbg(&state
->i2c
->dev
, "%s: if_frequency=%d\n",
625 __func__
, if_frequency
);
627 sampling_freq
= if_frequency
;
629 while (sampling_freq
> (state
->config
.clock
/ 2))
630 sampling_freq
-= state
->config
.clock
;
632 if (sampling_freq
< 0) {
634 spec_inv
= state
->config
.spec_inv
;
636 spec_inv
= !state
->config
.spec_inv
;
639 freq_cw
= af9013_div(state
, sampling_freq
, state
->config
.clock
,
643 freq_cw
= 0x800000 - freq_cw
;
645 buf
[0] = (freq_cw
>> 0) & 0xff;
646 buf
[1] = (freq_cw
>> 8) & 0xff;
647 buf
[2] = (freq_cw
>> 16) & 0x7f;
649 freq_cw
= 0x800000 - freq_cw
;
651 buf
[3] = (freq_cw
>> 0) & 0xff;
652 buf
[4] = (freq_cw
>> 8) & 0xff;
653 buf
[5] = (freq_cw
>> 16) & 0x7f;
655 ret
= af9013_wr_regs(state
, 0xd140, buf
, 3);
659 ret
= af9013_wr_regs(state
, 0x9be7, buf
, 6);
664 /* clear TPS lock flag */
665 ret
= af9013_wr_reg_bits(state
, 0xd330, 3, 1, 1);
669 /* clear MPEG2 lock flag */
670 ret
= af9013_wr_reg_bits(state
, 0xd507, 6, 1, 0);
674 /* empty channel function */
675 ret
= af9013_wr_reg_bits(state
, 0x9bfe, 0, 1, 0);
679 /* empty DVB-T channel function */
680 ret
= af9013_wr_reg_bits(state
, 0x9bc2, 0, 1, 0);
684 /* transmission parameters */
688 switch (c
->transmission_mode
) {
689 case TRANSMISSION_MODE_AUTO
:
692 case TRANSMISSION_MODE_2K
:
694 case TRANSMISSION_MODE_8K
:
698 dev_dbg(&state
->i2c
->dev
, "%s: invalid transmission_mode\n",
703 switch (c
->guard_interval
) {
704 case GUARD_INTERVAL_AUTO
:
707 case GUARD_INTERVAL_1_32
:
709 case GUARD_INTERVAL_1_16
:
712 case GUARD_INTERVAL_1_8
:
715 case GUARD_INTERVAL_1_4
:
719 dev_dbg(&state
->i2c
->dev
, "%s: invalid guard_interval\n",
724 switch (c
->hierarchy
) {
740 dev_dbg(&state
->i2c
->dev
, "%s: invalid hierarchy\n", __func__
);
744 switch (c
->modulation
) {
757 dev_dbg(&state
->i2c
->dev
, "%s: invalid modulation\n", __func__
);
761 /* Use HP. How and which case we can switch to LP? */
764 switch (c
->code_rate_HP
) {
783 dev_dbg(&state
->i2c
->dev
, "%s: invalid code_rate_HP\n",
788 switch (c
->code_rate_LP
) {
809 dev_dbg(&state
->i2c
->dev
, "%s: invalid code_rate_LP\n",
814 switch (c
->bandwidth_hz
) {
824 dev_dbg(&state
->i2c
->dev
, "%s: invalid bandwidth_hz\n",
830 ret
= af9013_wr_regs(state
, 0xd3c0, buf
, 3);
835 /* clear easy mode flag */
836 ret
= af9013_wr_reg(state
, 0xaefd, 0);
840 dev_dbg(&state
->i2c
->dev
, "%s: auto params\n", __func__
);
842 /* set easy mode flag */
843 ret
= af9013_wr_reg(state
, 0xaefd, 1);
847 ret
= af9013_wr_reg(state
, 0xaefe, 0);
851 dev_dbg(&state
->i2c
->dev
, "%s: manual params\n", __func__
);
855 ret
= af9013_wr_reg(state
, 0xffff, 0);
859 state
->bandwidth_hz
= c
->bandwidth_hz
;
860 state
->set_frontend_jiffies
= jiffies
;
861 state
->first_tune
= false;
865 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
869 static int af9013_get_frontend(struct dvb_frontend
*fe
,
870 struct dtv_frontend_properties
*c
)
872 struct af9013_state
*state
= fe
->demodulator_priv
;
876 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
878 ret
= af9013_rd_regs(state
, 0xd3c0, buf
, 3);
882 switch ((buf
[1] >> 6) & 3) {
884 c
->modulation
= QPSK
;
887 c
->modulation
= QAM_16
;
890 c
->modulation
= QAM_64
;
894 switch ((buf
[0] >> 0) & 3) {
896 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
899 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
902 switch ((buf
[0] >> 2) & 3) {
904 c
->guard_interval
= GUARD_INTERVAL_1_32
;
907 c
->guard_interval
= GUARD_INTERVAL_1_16
;
910 c
->guard_interval
= GUARD_INTERVAL_1_8
;
913 c
->guard_interval
= GUARD_INTERVAL_1_4
;
917 switch ((buf
[0] >> 4) & 7) {
919 c
->hierarchy
= HIERARCHY_NONE
;
922 c
->hierarchy
= HIERARCHY_1
;
925 c
->hierarchy
= HIERARCHY_2
;
928 c
->hierarchy
= HIERARCHY_4
;
932 switch ((buf
[2] >> 0) & 7) {
934 c
->code_rate_HP
= FEC_1_2
;
937 c
->code_rate_HP
= FEC_2_3
;
940 c
->code_rate_HP
= FEC_3_4
;
943 c
->code_rate_HP
= FEC_5_6
;
946 c
->code_rate_HP
= FEC_7_8
;
950 switch ((buf
[2] >> 3) & 7) {
952 c
->code_rate_LP
= FEC_1_2
;
955 c
->code_rate_LP
= FEC_2_3
;
958 c
->code_rate_LP
= FEC_3_4
;
961 c
->code_rate_LP
= FEC_5_6
;
964 c
->code_rate_LP
= FEC_7_8
;
968 switch ((buf
[1] >> 2) & 3) {
970 c
->bandwidth_hz
= 6000000;
973 c
->bandwidth_hz
= 7000000;
976 c
->bandwidth_hz
= 8000000;
982 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
986 static int af9013_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
988 struct af9013_state
*state
= fe
->demodulator_priv
;
993 * Return status from the cache if it is younger than 2000ms with the
994 * exception of last tune is done during 4000ms.
996 if (time_is_after_jiffies(
997 state
->read_status_jiffies
+ msecs_to_jiffies(2000)) &&
998 time_is_before_jiffies(
999 state
->set_frontend_jiffies
+ msecs_to_jiffies(4000))
1001 *status
= state
->fe_status
;
1008 ret
= af9013_rd_reg_bits(state
, 0xd507, 6, 1, &tmp
);
1013 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
1014 FE_HAS_SYNC
| FE_HAS_LOCK
;
1018 ret
= af9013_rd_reg_bits(state
, 0xd330, 3, 1, &tmp
);
1023 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
1027 state
->fe_status
= *status
;
1028 state
->read_status_jiffies
= jiffies
;
1032 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
1036 static int af9013_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
1038 struct af9013_state
*state
= fe
->demodulator_priv
;
1043 static int af9013_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
1045 struct af9013_state
*state
= fe
->demodulator_priv
;
1046 *strength
= state
->signal_strength
;
1050 static int af9013_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
1052 struct af9013_state
*state
= fe
->demodulator_priv
;
1057 static int af9013_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
1059 struct af9013_state
*state
= fe
->demodulator_priv
;
1060 *ucblocks
= state
->ucblocks
;
1064 static int af9013_init(struct dvb_frontend
*fe
)
1066 struct af9013_state
*state
= fe
->demodulator_priv
;
1070 const struct af9013_reg_bit
*init
;
1072 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
1075 ret
= af9013_power_ctrl(state
, 1);
1080 ret
= af9013_wr_reg(state
, 0xd73a, 0xa4);
1084 /* write API version to firmware */
1085 ret
= af9013_wr_regs(state
, 0x9bf2, state
->config
.api_version
, 4);
1089 /* program ADC control */
1090 switch (state
->config
.clock
) {
1091 case 28800000: /* 28.800 MHz */
1094 case 20480000: /* 20.480 MHz */
1097 case 28000000: /* 28.000 MHz */
1100 case 25000000: /* 25.000 MHz */
1104 dev_err(&state
->i2c
->dev
, "%s: invalid clock\n",
1109 adc_cw
= af9013_div(state
, state
->config
.clock
, 1000000ul, 19);
1110 buf
[0] = (adc_cw
>> 0) & 0xff;
1111 buf
[1] = (adc_cw
>> 8) & 0xff;
1112 buf
[2] = (adc_cw
>> 16) & 0xff;
1114 ret
= af9013_wr_regs(state
, 0xd180, buf
, 3);
1118 ret
= af9013_wr_reg_bits(state
, 0x9bd2, 0, 4, tmp
);
1122 /* set I2C master clock */
1123 ret
= af9013_wr_reg(state
, 0xd416, 0x14);
1128 ret
= af9013_wr_reg_bits(state
, 0xd700, 1, 1, 1);
1132 /* set no trigger */
1133 ret
= af9013_wr_reg_bits(state
, 0xd700, 2, 1, 0);
1137 /* set read-update bit for constellation */
1138 ret
= af9013_wr_reg_bits(state
, 0xd371, 1, 1, 1);
1142 /* settings for mp2if */
1143 if (state
->config
.ts_mode
== AF9013_TS_USB
) {
1144 /* AF9015 split PSB to 1.5k + 0.5k */
1145 ret
= af9013_wr_reg_bits(state
, 0xd50b, 2, 1, 1);
1149 /* AF9013 change the output bit to data7 */
1150 ret
= af9013_wr_reg_bits(state
, 0xd500, 3, 1, 1);
1154 /* AF9013 set mpeg to full speed */
1155 ret
= af9013_wr_reg_bits(state
, 0xd502, 4, 1, 1);
1160 ret
= af9013_wr_reg_bits(state
, 0xd520, 4, 1, 1);
1164 /* load OFSM settings */
1165 dev_dbg(&state
->i2c
->dev
, "%s: load ofsm settings\n", __func__
);
1166 len
= ARRAY_SIZE(ofsm_init
);
1168 for (i
= 0; i
< len
; i
++) {
1169 ret
= af9013_wr_reg_bits(state
, init
[i
].addr
, init
[i
].pos
,
1170 init
[i
].len
, init
[i
].val
);
1175 /* load tuner specific settings */
1176 dev_dbg(&state
->i2c
->dev
, "%s: load tuner specific settings\n",
1178 switch (state
->config
.tuner
) {
1179 case AF9013_TUNER_MXL5003D
:
1180 len
= ARRAY_SIZE(tuner_init_mxl5003d
);
1181 init
= tuner_init_mxl5003d
;
1183 case AF9013_TUNER_MXL5005D
:
1184 case AF9013_TUNER_MXL5005R
:
1185 case AF9013_TUNER_MXL5007T
:
1186 len
= ARRAY_SIZE(tuner_init_mxl5005
);
1187 init
= tuner_init_mxl5005
;
1189 case AF9013_TUNER_ENV77H11D5
:
1190 len
= ARRAY_SIZE(tuner_init_env77h11d5
);
1191 init
= tuner_init_env77h11d5
;
1193 case AF9013_TUNER_MT2060
:
1194 len
= ARRAY_SIZE(tuner_init_mt2060
);
1195 init
= tuner_init_mt2060
;
1197 case AF9013_TUNER_MC44S803
:
1198 len
= ARRAY_SIZE(tuner_init_mc44s803
);
1199 init
= tuner_init_mc44s803
;
1201 case AF9013_TUNER_QT1010
:
1202 case AF9013_TUNER_QT1010A
:
1203 len
= ARRAY_SIZE(tuner_init_qt1010
);
1204 init
= tuner_init_qt1010
;
1206 case AF9013_TUNER_MT2060_2
:
1207 len
= ARRAY_SIZE(tuner_init_mt2060_2
);
1208 init
= tuner_init_mt2060_2
;
1210 case AF9013_TUNER_TDA18271
:
1211 case AF9013_TUNER_TDA18218
:
1212 len
= ARRAY_SIZE(tuner_init_tda18271
);
1213 init
= tuner_init_tda18271
;
1215 case AF9013_TUNER_UNKNOWN
:
1217 len
= ARRAY_SIZE(tuner_init_unknown
);
1218 init
= tuner_init_unknown
;
1222 for (i
= 0; i
< len
; i
++) {
1223 ret
= af9013_wr_reg_bits(state
, init
[i
].addr
, init
[i
].pos
,
1224 init
[i
].len
, init
[i
].val
);
1230 ret
= af9013_wr_reg_bits(state
, 0xd500, 1, 2, state
->config
.ts_mode
);
1234 /* enable lock led */
1235 ret
= af9013_wr_reg_bits(state
, 0xd730, 0, 1, 1);
1239 /* check if we support signal strength */
1240 if (!state
->signal_strength_en
) {
1241 ret
= af9013_rd_reg_bits(state
, 0x9bee, 0, 1,
1242 &state
->signal_strength_en
);
1247 /* read values needed for signal strength calculation */
1248 if (state
->signal_strength_en
&& !state
->rf_50
) {
1249 ret
= af9013_rd_reg(state
, 0x9bbd, &state
->rf_50
);
1253 ret
= af9013_rd_reg(state
, 0x9bd0, &state
->rf_80
);
1257 ret
= af9013_rd_reg(state
, 0x9be2, &state
->if_50
);
1261 ret
= af9013_rd_reg(state
, 0x9be4, &state
->if_80
);
1267 ret
= af9013_wr_reg(state
, 0xd2e2, 1);
1272 buf
[0] = (10000 >> 0) & 0xff;
1273 buf
[1] = (10000 >> 8) & 0xff;
1274 ret
= af9013_wr_regs(state
, 0xd385, buf
, 2);
1278 /* enable FEC monitor */
1279 ret
= af9013_wr_reg_bits(state
, 0xd392, 1, 1, 1);
1283 state
->first_tune
= true;
1284 schedule_delayed_work(&state
->statistics_work
, msecs_to_jiffies(400));
1288 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
1292 static int af9013_sleep(struct dvb_frontend
*fe
)
1294 struct af9013_state
*state
= fe
->demodulator_priv
;
1297 dev_dbg(&state
->i2c
->dev
, "%s:\n", __func__
);
1299 /* stop statistics polling */
1300 cancel_delayed_work_sync(&state
->statistics_work
);
1302 /* disable lock led */
1303 ret
= af9013_wr_reg_bits(state
, 0xd730, 0, 1, 0);
1308 ret
= af9013_power_ctrl(state
, 0);
1314 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
1318 static int af9013_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
1321 struct af9013_state
*state
= fe
->demodulator_priv
;
1323 dev_dbg(&state
->i2c
->dev
, "%s: enable=%d\n", __func__
, enable
);
1325 /* gate already open or close */
1326 if (state
->i2c_gate_state
== enable
)
1329 if (state
->config
.ts_mode
== AF9013_TS_USB
)
1330 ret
= af9013_wr_reg_bits(state
, 0xd417, 3, 1, enable
);
1332 ret
= af9013_wr_reg_bits(state
, 0xd607, 2, 1, enable
);
1336 state
->i2c_gate_state
= enable
;
1340 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
1344 static void af9013_release(struct dvb_frontend
*fe
)
1346 struct af9013_state
*state
= fe
->demodulator_priv
;
1348 /* stop statistics polling */
1349 cancel_delayed_work_sync(&state
->statistics_work
);
1354 static const struct dvb_frontend_ops af9013_ops
;
1356 static int af9013_download_firmware(struct af9013_state
*state
)
1358 int i
, len
, remaining
, ret
;
1359 const struct firmware
*fw
;
1363 u8
*fw_file
= AF9013_FIRMWARE
;
1366 /* check whether firmware is already running */
1367 ret
= af9013_rd_reg(state
, 0x98be, &val
);
1371 dev_dbg(&state
->i2c
->dev
, "%s: firmware status=%02x\n",
1374 if (val
== 0x0c) /* fw is running, no need for download */
1377 dev_info(&state
->i2c
->dev
, "%s: found a '%s' in cold state, will try " \
1378 "to load a firmware\n",
1379 KBUILD_MODNAME
, af9013_ops
.info
.name
);
1381 /* request the firmware, this will block and timeout */
1382 ret
= request_firmware(&fw
, fw_file
, state
->i2c
->dev
.parent
);
1384 dev_info(&state
->i2c
->dev
, "%s: did not find the firmware " \
1385 "file. (%s) Please see linux/Documentation/dvb/ for " \
1386 "more details on firmware-problems. (%d)\n",
1387 KBUILD_MODNAME
, fw_file
, ret
);
1391 dev_info(&state
->i2c
->dev
, "%s: downloading firmware from file '%s'\n",
1392 KBUILD_MODNAME
, fw_file
);
1395 for (i
= 0; i
< fw
->size
; i
++)
1396 checksum
+= fw
->data
[i
];
1398 fw_params
[0] = checksum
>> 8;
1399 fw_params
[1] = checksum
& 0xff;
1400 fw_params
[2] = fw
->size
>> 8;
1401 fw_params
[3] = fw
->size
& 0xff;
1403 /* write fw checksum & size */
1404 ret
= af9013_write_ofsm_regs(state
, 0x50fc,
1405 fw_params
, sizeof(fw_params
));
1409 #define FW_ADDR 0x5100 /* firmware start address */
1410 #define LEN_MAX 16 /* max packet size */
1411 for (remaining
= fw
->size
; remaining
> 0; remaining
-= LEN_MAX
) {
1416 ret
= af9013_write_ofsm_regs(state
,
1417 FW_ADDR
+ fw
->size
- remaining
,
1418 (u8
*) &fw
->data
[fw
->size
- remaining
], len
);
1420 dev_err(&state
->i2c
->dev
,
1421 "%s: firmware download failed=%d\n",
1422 KBUILD_MODNAME
, ret
);
1427 /* request boot firmware */
1428 ret
= af9013_wr_reg(state
, 0xe205, 1);
1432 for (i
= 0; i
< 15; i
++) {
1435 /* check firmware status */
1436 ret
= af9013_rd_reg(state
, 0x98be, &val
);
1440 dev_dbg(&state
->i2c
->dev
, "%s: firmware status=%02x\n",
1443 if (val
== 0x0c || val
== 0x04) /* success or fail */
1448 dev_err(&state
->i2c
->dev
, "%s: firmware did not run\n",
1451 } else if (val
!= 0x0c) {
1452 dev_err(&state
->i2c
->dev
, "%s: firmware boot timeout\n",
1458 release_firmware(fw
);
1462 dev_info(&state
->i2c
->dev
, "%s: found a '%s' in warm state\n",
1463 KBUILD_MODNAME
, af9013_ops
.info
.name
);
1467 struct dvb_frontend
*af9013_attach(const struct af9013_config
*config
,
1468 struct i2c_adapter
*i2c
)
1471 struct af9013_state
*state
= NULL
;
1474 /* allocate memory for the internal state */
1475 state
= kzalloc(sizeof(struct af9013_state
), GFP_KERNEL
);
1479 /* setup the state */
1481 memcpy(&state
->config
, config
, sizeof(struct af9013_config
));
1483 /* download firmware */
1484 if (state
->config
.ts_mode
!= AF9013_TS_USB
) {
1485 ret
= af9013_download_firmware(state
);
1490 /* firmware version */
1491 ret
= af9013_rd_regs(state
, 0x5103, buf
, 4);
1495 dev_info(&state
->i2c
->dev
, "%s: firmware version %d.%d.%d.%d\n",
1496 KBUILD_MODNAME
, buf
[0], buf
[1], buf
[2], buf
[3]);
1499 for (i
= 0; i
< sizeof(state
->config
.gpio
); i
++) {
1500 ret
= af9013_set_gpio(state
, i
, state
->config
.gpio
[i
]);
1505 /* create dvb_frontend */
1506 memcpy(&state
->fe
.ops
, &af9013_ops
,
1507 sizeof(struct dvb_frontend_ops
));
1508 state
->fe
.demodulator_priv
= state
;
1510 INIT_DELAYED_WORK(&state
->statistics_work
, af9013_statistics_work
);
1517 EXPORT_SYMBOL(af9013_attach
);
1519 static const struct dvb_frontend_ops af9013_ops
= {
1520 .delsys
= { SYS_DVBT
},
1522 .name
= "Afatech AF9013",
1523 .frequency_min
= 174000000,
1524 .frequency_max
= 862000000,
1525 .frequency_stepsize
= 250000,
1526 .frequency_tolerance
= 0,
1527 .caps
= FE_CAN_FEC_1_2
|
1537 FE_CAN_TRANSMISSION_MODE_AUTO
|
1538 FE_CAN_GUARD_INTERVAL_AUTO
|
1539 FE_CAN_HIERARCHY_AUTO
|
1544 .release
= af9013_release
,
1546 .init
= af9013_init
,
1547 .sleep
= af9013_sleep
,
1549 .get_tune_settings
= af9013_get_tune_settings
,
1550 .set_frontend
= af9013_set_frontend
,
1551 .get_frontend
= af9013_get_frontend
,
1553 .read_status
= af9013_read_status
,
1554 .read_snr
= af9013_read_snr
,
1555 .read_signal_strength
= af9013_read_signal_strength
,
1556 .read_ber
= af9013_read_ber
,
1557 .read_ucblocks
= af9013_read_ucblocks
,
1559 .i2c_gate_ctrl
= af9013_i2c_gate_ctrl
,
1562 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1563 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1564 MODULE_LICENSE("GPL");
1565 MODULE_FIRMWARE(AF9013_FIRMWARE
);