2 * Afatech AF9033 demodulator driver
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "af9033_priv.h"
25 struct i2c_adapter
*i2c
;
26 struct dvb_frontend fe
;
27 struct af9033_config cfg
;
30 bool ts_mode_parallel
;
35 unsigned long last_stat_check
;
38 /* write multiple registers */
39 static int af9033_wr_regs(struct af9033_state
*state
, u32 reg
, const u8
*val
,
44 struct i2c_msg msg
[1] = {
46 .addr
= state
->cfg
.i2c_addr
,
53 buf
[0] = (reg
>> 16) & 0xff;
54 buf
[1] = (reg
>> 8) & 0xff;
55 buf
[2] = (reg
>> 0) & 0xff;
56 memcpy(&buf
[3], val
, len
);
58 ret
= i2c_transfer(state
->i2c
, msg
, 1);
62 printk(KERN_WARNING
"%s: i2c wr failed=%d reg=%06x len=%d\n",
63 __func__
, ret
, reg
, len
);
70 /* read multiple registers */
71 static int af9033_rd_regs(struct af9033_state
*state
, u32 reg
, u8
*val
, int len
)
74 u8 buf
[3] = { (reg
>> 16) & 0xff, (reg
>> 8) & 0xff,
76 struct i2c_msg msg
[2] = {
78 .addr
= state
->cfg
.i2c_addr
,
83 .addr
= state
->cfg
.i2c_addr
,
90 ret
= i2c_transfer(state
->i2c
, msg
, 2);
94 printk(KERN_WARNING
"%s: i2c rd failed=%d reg=%06x len=%d\n",
95 __func__
, ret
, reg
, len
);
103 /* write single register */
104 static int af9033_wr_reg(struct af9033_state
*state
, u32 reg
, u8 val
)
106 return af9033_wr_regs(state
, reg
, &val
, 1);
109 /* read single register */
110 static int af9033_rd_reg(struct af9033_state
*state
, u32 reg
, u8
*val
)
112 return af9033_rd_regs(state
, reg
, val
, 1);
115 /* write single register with mask */
116 static int af9033_wr_reg_mask(struct af9033_state
*state
, u32 reg
, u8 val
,
122 /* no need for read if whole reg is written */
124 ret
= af9033_rd_regs(state
, reg
, &tmp
, 1);
133 return af9033_wr_regs(state
, reg
, &val
, 1);
136 /* read single register with mask */
137 static int af9033_rd_reg_mask(struct af9033_state
*state
, u32 reg
, u8
*val
,
143 ret
= af9033_rd_regs(state
, reg
, &tmp
, 1);
149 /* find position of the first bit */
150 for (i
= 0; i
< 8; i
++) {
151 if ((mask
>> i
) & 0x01)
159 static u32
af9033_div(u32 a
, u32 b
, u32 x
)
163 pr_debug("%s: a=%d b=%d x=%d\n", __func__
, a
, b
, x
);
170 for (i
= 0; i
< x
; i
++) {
178 r
= (c
<< (u32
)x
) + r
;
180 pr_debug("%s: a=%d b=%d x=%d r=%d r=%x\n", __func__
, a
, b
, x
, r
, r
);
185 static void af9033_release(struct dvb_frontend
*fe
)
187 struct af9033_state
*state
= fe
->demodulator_priv
;
192 static int af9033_init(struct dvb_frontend
*fe
)
194 struct af9033_state
*state
= fe
->demodulator_priv
;
196 const struct reg_val
*init
;
198 u32 adc_cw
, clock_cw
;
199 struct reg_val_mask tab
[] = {
200 { 0x80fb24, 0x00, 0x08 },
201 { 0x80004c, 0x00, 0xff },
202 { 0x00f641, state
->cfg
.tuner
, 0xff },
203 { 0x80f5ca, 0x01, 0x01 },
204 { 0x80f715, 0x01, 0x01 },
205 { 0x00f41f, 0x04, 0x04 },
206 { 0x00f41a, 0x01, 0x01 },
207 { 0x80f731, 0x00, 0x01 },
208 { 0x00d91e, 0x00, 0x01 },
209 { 0x00d919, 0x00, 0x01 },
210 { 0x80f732, 0x00, 0x01 },
211 { 0x00d91f, 0x00, 0x01 },
212 { 0x00d91a, 0x00, 0x01 },
213 { 0x80f730, 0x00, 0x01 },
214 { 0x80f778, 0x00, 0xff },
215 { 0x80f73c, 0x01, 0x01 },
216 { 0x80f776, 0x00, 0x01 },
217 { 0x00d8fd, 0x01, 0xff },
218 { 0x00d830, 0x01, 0xff },
219 { 0x00d831, 0x00, 0xff },
220 { 0x00d832, 0x00, 0xff },
221 { 0x80f985, state
->ts_mode_serial
, 0x01 },
222 { 0x80f986, state
->ts_mode_parallel
, 0x01 },
223 { 0x00d827, 0x00, 0xff },
224 { 0x00d829, 0x00, 0xff },
227 /* program clock control */
228 clock_cw
= af9033_div(state
->cfg
.clock
, 1000000ul, 19ul);
229 buf
[0] = (clock_cw
>> 0) & 0xff;
230 buf
[1] = (clock_cw
>> 8) & 0xff;
231 buf
[2] = (clock_cw
>> 16) & 0xff;
232 buf
[3] = (clock_cw
>> 24) & 0xff;
234 pr_debug("%s: clock=%d clock_cw=%08x\n", __func__
, state
->cfg
.clock
,
237 ret
= af9033_wr_regs(state
, 0x800025, buf
, 4);
241 /* program ADC control */
242 for (i
= 0; i
< ARRAY_SIZE(clock_adc_lut
); i
++) {
243 if (clock_adc_lut
[i
].clock
== state
->cfg
.clock
)
247 adc_cw
= af9033_div(clock_adc_lut
[i
].adc
, 1000000ul, 19ul);
248 buf
[0] = (adc_cw
>> 0) & 0xff;
249 buf
[1] = (adc_cw
>> 8) & 0xff;
250 buf
[2] = (adc_cw
>> 16) & 0xff;
252 pr_debug("%s: adc=%d adc_cw=%06x\n", __func__
, clock_adc_lut
[i
].adc
,
255 ret
= af9033_wr_regs(state
, 0x80f1cd, buf
, 3);
259 /* program register table */
260 for (i
= 0; i
< ARRAY_SIZE(tab
); i
++) {
261 ret
= af9033_wr_reg_mask(state
, tab
[i
].reg
, tab
[i
].val
,
267 /* settings for TS interface */
268 if (state
->cfg
.ts_mode
== AF9033_TS_MODE_USB
) {
269 ret
= af9033_wr_reg_mask(state
, 0x80f9a5, 0x00, 0x01);
273 ret
= af9033_wr_reg_mask(state
, 0x80f9b5, 0x01, 0x01);
277 ret
= af9033_wr_reg_mask(state
, 0x80f990, 0x00, 0x01);
281 ret
= af9033_wr_reg_mask(state
, 0x80f9b5, 0x00, 0x01);
286 /* load OFSM settings */
287 pr_debug("%s: load ofsm settings\n", __func__
);
288 len
= ARRAY_SIZE(ofsm_init
);
290 for (i
= 0; i
< len
; i
++) {
291 ret
= af9033_wr_reg(state
, init
[i
].reg
, init
[i
].val
);
296 /* load tuner specific settings */
297 pr_debug("%s: load tuner specific settings\n",
299 switch (state
->cfg
.tuner
) {
300 case AF9033_TUNER_TUA9001
:
301 len
= ARRAY_SIZE(tuner_init_tua9001
);
302 init
= tuner_init_tua9001
;
304 case AF9033_TUNER_FC0011
:
305 len
= ARRAY_SIZE(tuner_init_fc0011
);
306 init
= tuner_init_fc0011
;
308 case AF9033_TUNER_MXL5007T
:
309 len
= ARRAY_SIZE(tuner_init_mxl5007t
);
310 init
= tuner_init_mxl5007t
;
312 case AF9033_TUNER_TDA18218
:
313 len
= ARRAY_SIZE(tuner_init_tda18218
);
314 init
= tuner_init_tda18218
;
317 pr_debug("%s: unsupported tuner ID=%d\n", __func__
,
323 for (i
= 0; i
< len
; i
++) {
324 ret
= af9033_wr_reg(state
, init
[i
].reg
, init
[i
].val
);
329 state
->bandwidth_hz
= 0; /* force to program all parameters */
334 pr_debug("%s: failed=%d\n", __func__
, ret
);
339 static int af9033_sleep(struct dvb_frontend
*fe
)
341 struct af9033_state
*state
= fe
->demodulator_priv
;
345 ret
= af9033_wr_reg(state
, 0x80004c, 1);
349 ret
= af9033_wr_reg(state
, 0x800000, 0);
353 for (i
= 100, tmp
= 1; i
&& tmp
; i
--) {
354 ret
= af9033_rd_reg(state
, 0x80004c, &tmp
);
358 usleep_range(200, 10000);
361 pr_debug("%s: loop=%d\n", __func__
, i
);
368 ret
= af9033_wr_reg_mask(state
, 0x80fb24, 0x08, 0x08);
372 /* prevent current leak (?) */
373 if (state
->cfg
.ts_mode
== AF9033_TS_MODE_SERIAL
) {
374 /* enable parallel TS */
375 ret
= af9033_wr_reg_mask(state
, 0x00d917, 0x00, 0x01);
379 ret
= af9033_wr_reg_mask(state
, 0x00d916, 0x01, 0x01);
387 pr_debug("%s: failed=%d\n", __func__
, ret
);
392 static int af9033_get_tune_settings(struct dvb_frontend
*fe
,
393 struct dvb_frontend_tune_settings
*fesettings
)
395 fesettings
->min_delay_ms
= 800;
396 fesettings
->step_size
= 0;
397 fesettings
->max_drift
= 0;
402 static int af9033_set_frontend(struct dvb_frontend
*fe
)
404 struct af9033_state
*state
= fe
->demodulator_priv
;
405 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
406 int ret
, i
, spec_inv
;
407 u8 tmp
, buf
[3], bandwidth_reg_val
;
408 u32 if_frequency
, freq_cw
, adc_freq
;
410 pr_debug("%s: frequency=%d bandwidth_hz=%d\n", __func__
, c
->frequency
,
413 /* check bandwidth */
414 switch (c
->bandwidth_hz
) {
416 bandwidth_reg_val
= 0x00;
419 bandwidth_reg_val
= 0x01;
422 bandwidth_reg_val
= 0x02;
425 pr_debug("%s: invalid bandwidth_hz\n", __func__
);
431 if (fe
->ops
.tuner_ops
.set_params
)
432 fe
->ops
.tuner_ops
.set_params(fe
);
434 /* program CFOE coefficients */
435 if (c
->bandwidth_hz
!= state
->bandwidth_hz
) {
436 for (i
= 0; i
< ARRAY_SIZE(coeff_lut
); i
++) {
437 if (coeff_lut
[i
].clock
== state
->cfg
.clock
&&
438 coeff_lut
[i
].bandwidth_hz
== c
->bandwidth_hz
) {
442 ret
= af9033_wr_regs(state
, 0x800001,
443 coeff_lut
[i
].val
, sizeof(coeff_lut
[i
].val
));
446 /* program frequency control */
447 if (c
->bandwidth_hz
!= state
->bandwidth_hz
) {
448 spec_inv
= state
->cfg
.spec_inv
? -1 : 1;
450 for (i
= 0; i
< ARRAY_SIZE(clock_adc_lut
); i
++) {
451 if (clock_adc_lut
[i
].clock
== state
->cfg
.clock
)
454 adc_freq
= clock_adc_lut
[i
].adc
;
456 /* get used IF frequency */
457 if (fe
->ops
.tuner_ops
.get_if_frequency
)
458 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_frequency
);
462 while (if_frequency
> (adc_freq
/ 2))
463 if_frequency
-= adc_freq
;
465 if (if_frequency
>= 0)
470 freq_cw
= af9033_div(if_frequency
, adc_freq
, 23ul);
475 /* get adc multiplies */
476 ret
= af9033_rd_reg(state
, 0x800045, &tmp
);
483 buf
[0] = (freq_cw
>> 0) & 0xff;
484 buf
[1] = (freq_cw
>> 8) & 0xff;
485 buf
[2] = (freq_cw
>> 16) & 0x7f;
486 ret
= af9033_wr_regs(state
, 0x800029, buf
, 3);
490 state
->bandwidth_hz
= c
->bandwidth_hz
;
493 ret
= af9033_wr_reg_mask(state
, 0x80f904, bandwidth_reg_val
, 0x03);
497 ret
= af9033_wr_reg(state
, 0x800040, 0x00);
501 ret
= af9033_wr_reg(state
, 0x800047, 0x00);
505 ret
= af9033_wr_reg_mask(state
, 0x80f999, 0x00, 0x01);
509 if (c
->frequency
<= 230000000)
510 tmp
= 0x00; /* VHF */
512 tmp
= 0x01; /* UHF */
514 ret
= af9033_wr_reg(state
, 0x80004b, tmp
);
518 ret
= af9033_wr_reg(state
, 0x800000, 0x00);
525 pr_debug("%s: failed=%d\n", __func__
, ret
);
530 static int af9033_get_frontend(struct dvb_frontend
*fe
)
532 struct af9033_state
*state
= fe
->demodulator_priv
;
533 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
537 pr_debug("%s\n", __func__
);
539 /* read all needed registers */
540 ret
= af9033_rd_regs(state
, 0x80f900, buf
, sizeof(buf
));
544 switch ((buf
[0] >> 0) & 3) {
546 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
549 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
553 switch ((buf
[1] >> 0) & 3) {
555 c
->guard_interval
= GUARD_INTERVAL_1_32
;
558 c
->guard_interval
= GUARD_INTERVAL_1_16
;
561 c
->guard_interval
= GUARD_INTERVAL_1_8
;
564 c
->guard_interval
= GUARD_INTERVAL_1_4
;
568 switch ((buf
[2] >> 0) & 7) {
570 c
->hierarchy
= HIERARCHY_NONE
;
573 c
->hierarchy
= HIERARCHY_1
;
576 c
->hierarchy
= HIERARCHY_2
;
579 c
->hierarchy
= HIERARCHY_4
;
583 switch ((buf
[3] >> 0) & 3) {
585 c
->modulation
= QPSK
;
588 c
->modulation
= QAM_16
;
591 c
->modulation
= QAM_64
;
595 switch ((buf
[4] >> 0) & 3) {
597 c
->bandwidth_hz
= 6000000;
600 c
->bandwidth_hz
= 7000000;
603 c
->bandwidth_hz
= 8000000;
607 switch ((buf
[6] >> 0) & 7) {
609 c
->code_rate_HP
= FEC_1_2
;
612 c
->code_rate_HP
= FEC_2_3
;
615 c
->code_rate_HP
= FEC_3_4
;
618 c
->code_rate_HP
= FEC_5_6
;
621 c
->code_rate_HP
= FEC_7_8
;
624 c
->code_rate_HP
= FEC_NONE
;
628 switch ((buf
[7] >> 0) & 7) {
630 c
->code_rate_LP
= FEC_1_2
;
633 c
->code_rate_LP
= FEC_2_3
;
636 c
->code_rate_LP
= FEC_3_4
;
639 c
->code_rate_LP
= FEC_5_6
;
642 c
->code_rate_LP
= FEC_7_8
;
645 c
->code_rate_LP
= FEC_NONE
;
652 pr_debug("%s: failed=%d\n", __func__
, ret
);
657 static int af9033_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
659 struct af9033_state
*state
= fe
->demodulator_priv
;
665 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
666 ret
= af9033_rd_reg(state
, 0x800047, &tmp
);
672 *status
|= FE_HAS_SIGNAL
;
676 ret
= af9033_rd_reg_mask(state
, 0x80f5a9, &tmp
, 0x01);
681 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
685 ret
= af9033_rd_reg_mask(state
, 0x80f999, &tmp
, 0x01);
690 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
691 FE_HAS_VITERBI
| FE_HAS_SYNC
|
698 pr_debug("%s: failed=%d\n", __func__
, ret
);
703 static int af9033_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
705 struct af9033_state
*state
= fe
->demodulator_priv
;
709 const struct val_snr
*uninitialized_var(snr_lut
);
712 ret
= af9033_rd_regs(state
, 0x80002c, buf
, 3);
716 snr_val
= (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
718 /* read current modulation */
719 ret
= af9033_rd_reg(state
, 0x80f903, &tmp
);
723 switch ((tmp
>> 0) & 3) {
725 len
= ARRAY_SIZE(qpsk_snr_lut
);
726 snr_lut
= qpsk_snr_lut
;
729 len
= ARRAY_SIZE(qam16_snr_lut
);
730 snr_lut
= qam16_snr_lut
;
733 len
= ARRAY_SIZE(qam64_snr_lut
);
734 snr_lut
= qam64_snr_lut
;
740 for (i
= 0; i
< len
; i
++) {
741 tmp
= snr_lut
[i
].snr
;
743 if (snr_val
< snr_lut
[i
].val
)
747 *snr
= tmp
* 10; /* dB/10 */
752 pr_debug("%s: failed=%d\n", __func__
, ret
);
757 static int af9033_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
759 struct af9033_state
*state
= fe
->demodulator_priv
;
763 /* read signal strength of 0-100 scale */
764 ret
= af9033_rd_reg(state
, 0x800048, &strength2
);
768 /* scale value to 0x0000-0xffff */
769 *strength
= strength2
* 0xffff / 100;
774 pr_debug("%s: failed=%d\n", __func__
, ret
);
779 static int af9033_update_ch_stat(struct af9033_state
*state
)
782 u32 err_cnt
, bit_cnt
;
786 /* only update data every half second */
787 if (time_after(jiffies
, state
->last_stat_check
+ msecs_to_jiffies(500))) {
788 ret
= af9033_rd_regs(state
, 0x800032, buf
, sizeof(buf
));
791 /* in 8 byte packets? */
792 abort_cnt
= (buf
[1] << 8) + buf
[0];
794 err_cnt
= (buf
[4] << 16) + (buf
[3] << 8) + buf
[2];
795 /* in 8 byte packets? always(?) 0x2710 = 10000 */
796 bit_cnt
= (buf
[6] << 8) + buf
[5];
798 if (bit_cnt
< abort_cnt
) {
800 state
->ber
= 0xffffffff;
802 /* 8 byte packets, that have not been rejected already */
803 bit_cnt
-= (u32
)abort_cnt
;
805 state
->ber
= 0xffffffff;
807 err_cnt
-= (u32
)abort_cnt
* 8 * 8;
809 state
->ber
= err_cnt
* (0xffffffff / bit_cnt
);
812 state
->ucb
+= abort_cnt
;
813 state
->last_stat_check
= jiffies
;
818 pr_debug("%s: failed=%d\n", __func__
, ret
);
822 static int af9033_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
824 struct af9033_state
*state
= fe
->demodulator_priv
;
827 ret
= af9033_update_ch_stat(state
);
836 static int af9033_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
838 struct af9033_state
*state
= fe
->demodulator_priv
;
841 ret
= af9033_update_ch_stat(state
);
845 *ucblocks
= state
->ucb
;
850 static int af9033_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
852 struct af9033_state
*state
= fe
->demodulator_priv
;
855 pr_debug("%s: enable=%d\n", __func__
, enable
);
857 ret
= af9033_wr_reg_mask(state
, 0x00fa04, enable
, 0x01);
864 pr_debug("%s: failed=%d\n", __func__
, ret
);
869 static struct dvb_frontend_ops af9033_ops
;
871 struct dvb_frontend
*af9033_attach(const struct af9033_config
*config
,
872 struct i2c_adapter
*i2c
)
875 struct af9033_state
*state
;
878 pr_debug("%s:\n", __func__
);
880 /* allocate memory for the internal state */
881 state
= kzalloc(sizeof(struct af9033_state
), GFP_KERNEL
);
885 /* setup the state */
887 memcpy(&state
->cfg
, config
, sizeof(struct af9033_config
));
889 if (state
->cfg
.clock
!= 12000000) {
890 printk(KERN_INFO
"af9033: unsupported clock=%d, only " \
891 "12000000 Hz is supported currently\n",
896 /* firmware version */
897 ret
= af9033_rd_regs(state
, 0x0083e9, &buf
[0], 4);
901 ret
= af9033_rd_regs(state
, 0x804191, &buf
[4], 4);
905 printk(KERN_INFO
"af9033: firmware version: LINK=%d.%d.%d.%d " \
906 "OFDM=%d.%d.%d.%d\n", buf
[0], buf
[1], buf
[2], buf
[3],
907 buf
[4], buf
[5], buf
[6], buf
[7]);
909 /* configure internal TS mode */
910 switch (state
->cfg
.ts_mode
) {
911 case AF9033_TS_MODE_PARALLEL
:
912 state
->ts_mode_parallel
= true;
914 case AF9033_TS_MODE_SERIAL
:
915 state
->ts_mode_serial
= true;
917 case AF9033_TS_MODE_USB
:
918 /* usb mode for AF9035 */
923 /* create dvb_frontend */
924 memcpy(&state
->fe
.ops
, &af9033_ops
, sizeof(struct dvb_frontend_ops
));
925 state
->fe
.demodulator_priv
= state
;
933 EXPORT_SYMBOL(af9033_attach
);
935 static struct dvb_frontend_ops af9033_ops
= {
936 .delsys
= { SYS_DVBT
},
938 .name
= "Afatech AF9033 (DVB-T)",
939 .frequency_min
= 174000000,
940 .frequency_max
= 862000000,
941 .frequency_stepsize
= 250000,
942 .frequency_tolerance
= 0,
943 .caps
= FE_CAN_FEC_1_2
|
953 FE_CAN_TRANSMISSION_MODE_AUTO
|
954 FE_CAN_GUARD_INTERVAL_AUTO
|
955 FE_CAN_HIERARCHY_AUTO
|
960 .release
= af9033_release
,
963 .sleep
= af9033_sleep
,
965 .get_tune_settings
= af9033_get_tune_settings
,
966 .set_frontend
= af9033_set_frontend
,
967 .get_frontend
= af9033_get_frontend
,
969 .read_status
= af9033_read_status
,
970 .read_snr
= af9033_read_snr
,
971 .read_signal_strength
= af9033_read_signal_strength
,
972 .read_ber
= af9033_read_ber
,
973 .read_ucblocks
= af9033_read_ucblocks
,
975 .i2c_gate_ctrl
= af9033_i2c_gate_ctrl
,
978 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
979 MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
980 MODULE_LICENSE("GPL");