2 * Driver for Silicon Labs Si2161 DVB-T and Si2165 DVB-C/-T Demodulator
4 * Copyright (C) 2013-2017 Matthias Schwarzott <zzam@gentoo.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 * http://www.silabs.com/Support%20Documents/TechnicalDocs/Si2165-short.pdf
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
27 #include <linux/firmware.h>
28 #include <linux/regmap.h>
30 #include <media/dvb_frontend.h>
31 #include <media/dvb_math.h>
32 #include "si2165_priv.h"
36 * Hauppauge WinTV-HVR-930C-HD B130 / PCTV QuatroStick 521e 1113xx
39 * Hauppauge WinTV-HVR-930C-HD B131 / PCTV QuatroStick 522e 1114xx
40 * uses 24 MHz clock provided by tuner
44 struct i2c_client
*client
;
46 struct regmap
*regmap
;
48 struct dvb_frontend fe
;
50 struct si2165_config config
;
55 /* calculated by xtal and div settings */
68 static int si2165_write(struct si2165_state
*state
, const u16 reg
,
69 const u8
*src
, const int count
)
73 dev_dbg(&state
->client
->dev
, "i2c write: reg: 0x%04x, data: %*ph\n",
76 ret
= regmap_bulk_write(state
->regmap
, reg
, src
, count
);
79 dev_err(&state
->client
->dev
, "%s: ret == %d\n", __func__
, ret
);
84 static int si2165_read(struct si2165_state
*state
,
85 const u16 reg
, u8
*val
, const int count
)
87 int ret
= regmap_bulk_read(state
->regmap
, reg
, val
, count
);
90 dev_err(&state
->client
->dev
, "%s: error (addr %02x reg %04x error (ret == %i)\n",
91 __func__
, state
->config
.i2c_addr
, reg
, ret
);
95 dev_dbg(&state
->client
->dev
, "i2c read: reg: 0x%04x, data: %*ph\n",
101 static int si2165_readreg8(struct si2165_state
*state
,
102 const u16 reg
, u8
*val
)
104 unsigned int val_tmp
;
105 int ret
= regmap_read(state
->regmap
, reg
, &val_tmp
);
107 dev_dbg(&state
->client
->dev
, "reg read: R(0x%04x)=0x%02x\n", reg
, *val
);
111 static int si2165_readreg16(struct si2165_state
*state
,
112 const u16 reg
, u16
*val
)
116 int ret
= si2165_read(state
, reg
, buf
, 2);
117 *val
= buf
[0] | buf
[1] << 8;
118 dev_dbg(&state
->client
->dev
, "reg read: R(0x%04x)=0x%04x\n", reg
, *val
);
122 static int si2165_readreg24(struct si2165_state
*state
,
123 const u16 reg
, u32
*val
)
127 int ret
= si2165_read(state
, reg
, buf
, 3);
128 *val
= buf
[0] | buf
[1] << 8 | buf
[2] << 16;
129 dev_dbg(&state
->client
->dev
, "reg read: R(0x%04x)=0x%06x\n", reg
, *val
);
133 static int si2165_writereg8(struct si2165_state
*state
, const u16 reg
, u8 val
)
135 return regmap_write(state
->regmap
, reg
, val
);
138 static int si2165_writereg16(struct si2165_state
*state
, const u16 reg
, u16 val
)
140 u8 buf
[2] = { val
& 0xff, (val
>> 8) & 0xff };
142 return si2165_write(state
, reg
, buf
, 2);
145 static int si2165_writereg24(struct si2165_state
*state
, const u16 reg
, u32 val
)
147 u8 buf
[3] = { val
& 0xff, (val
>> 8) & 0xff, (val
>> 16) & 0xff };
149 return si2165_write(state
, reg
, buf
, 3);
152 static int si2165_writereg32(struct si2165_state
*state
, const u16 reg
, u32 val
)
160 return si2165_write(state
, reg
, buf
, 4);
163 static int si2165_writereg_mask8(struct si2165_state
*state
, const u16 reg
,
168 int ret
= si2165_readreg8(state
, reg
, &tmp
);
177 return si2165_writereg8(state
, reg
, val
);
180 #define REG16(reg, val) \
181 { (reg), (val) & 0xff }, \
182 { (reg) + 1, (val) >> 8 & 0xff }
183 struct si2165_reg_value_pair
{
188 static int si2165_write_reg_list(struct si2165_state
*state
,
189 const struct si2165_reg_value_pair
*regs
,
195 for (i
= 0; i
< count
; i
++) {
196 ret
= si2165_writereg8(state
, regs
[i
].reg
, regs
[i
].val
);
203 static int si2165_get_tune_settings(struct dvb_frontend
*fe
,
204 struct dvb_frontend_tune_settings
*s
)
206 s
->min_delay_ms
= 1000;
210 static int si2165_init_pll(struct si2165_state
*state
)
212 u32 ref_freq_hz
= state
->config
.ref_freq_hz
;
213 u8 divr
= 1; /* 1..7 */
214 u8 divp
= 1; /* only 1 or 4 */
215 u8 divn
= 56; /* 1..63 */
221 * hardcoded values can be deleted if calculation is verified
222 * or it yields the same values as the windows driver
224 switch (ref_freq_hz
) {
234 /* ref_freq / divr must be between 4 and 16 MHz */
235 if (ref_freq_hz
> 16000000u)
239 * now select divn and divp such that
240 * fvco is in 1624..1824 MHz
242 if (1624000000u * divr
> ref_freq_hz
* 2u * 63u)
245 /* is this already correct regarding rounding? */
246 divn
= 1624000000u * divr
/ (ref_freq_hz
* 2u * divp
);
250 /* adc_clk and sys_clk depend on xtal and pll settings */
251 state
->fvco_hz
= ref_freq_hz
/ divr
253 state
->adc_clk
= state
->fvco_hz
/ (divm
* 4u);
254 state
->sys_clk
= state
->fvco_hz
/ (divl
* 2u);
256 /* write all 4 pll registers 0x00a0..0x00a3 at once */
259 buf
[2] = (divn
& 0x3f) | ((divp
== 1) ? 0x40 : 0x00) | 0x80;
261 return si2165_write(state
, REG_PLL_DIVL
, buf
, 4);
264 static int si2165_adjust_pll_divl(struct si2165_state
*state
, u8 divl
)
266 state
->sys_clk
= state
->fvco_hz
/ (divl
* 2u);
267 return si2165_writereg8(state
, REG_PLL_DIVL
, divl
);
270 static u32
si2165_get_fe_clk(struct si2165_state
*state
)
272 /* assume Oversampling mode Ovr4 is used */
273 return state
->adc_clk
;
276 static int si2165_wait_init_done(struct si2165_state
*state
)
282 for (i
= 0; i
< 3; ++i
) {
283 si2165_readreg8(state
, REG_INIT_DONE
, &val
);
286 usleep_range(1000, 50000);
288 dev_err(&state
->client
->dev
, "init_done was not set\n");
292 static int si2165_upload_firmware_block(struct si2165_state
*state
,
293 const u8
*data
, u32 len
, u32
*poffset
,
297 u8 buf_ctrl
[4] = { 0x00, 0x00, 0x00, 0xc0 };
300 u32 offset
= poffset
? *poffset
: 0;
307 dev_dbg(&state
->client
->dev
,
308 "fw load: %s: called with len=0x%x offset=0x%x blockcount=0x%x\n",
309 __func__
, len
, offset
, block_count
);
310 while (offset
+ 12 <= len
&& cur_block
< block_count
) {
311 dev_dbg(&state
->client
->dev
,
312 "fw load: %s: in while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n",
313 __func__
, len
, offset
, cur_block
, block_count
);
314 wordcount
= data
[offset
];
315 if (wordcount
< 1 || data
[offset
+ 1] ||
316 data
[offset
+ 2] || data
[offset
+ 3]) {
317 dev_warn(&state
->client
->dev
,
318 "bad fw data[0..3] = %*ph\n",
323 if (offset
+ 8 + wordcount
* 4 > len
) {
324 dev_warn(&state
->client
->dev
,
325 "len is too small for block len=%d, wordcount=%d\n",
330 buf_ctrl
[0] = wordcount
- 1;
332 ret
= si2165_write(state
, REG_DCOM_CONTROL_BYTE
, buf_ctrl
, 4);
335 ret
= si2165_write(state
, REG_DCOM_ADDR
, data
+ offset
+ 4, 4);
341 while (wordcount
> 0) {
342 ret
= si2165_write(state
, REG_DCOM_DATA
,
352 dev_dbg(&state
->client
->dev
,
353 "fw load: %s: after while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n",
354 __func__
, len
, offset
, cur_block
, block_count
);
359 dev_dbg(&state
->client
->dev
,
360 "fw load: %s: returned offset=0x%x\n",
368 static int si2165_upload_firmware(struct si2165_state
*state
)
375 const struct firmware
*fw
= NULL
;
384 switch (state
->chip_revcode
) {
385 case 0x03: /* revision D */
386 fw_file
= SI2165_FIRMWARE_REV_D
;
389 dev_info(&state
->client
->dev
, "no firmware file for revision=%d\n",
390 state
->chip_revcode
);
394 /* request the firmware, this will block and timeout */
395 ret
= request_firmware(&fw
, fw_file
, &state
->client
->dev
);
397 dev_warn(&state
->client
->dev
, "firmware file '%s' not found\n",
405 dev_info(&state
->client
->dev
, "downloading firmware from file '%s' size=%d\n",
409 dev_warn(&state
->client
->dev
, "firmware size is not multiple of 4\n");
414 /* check header (8 bytes) */
416 dev_warn(&state
->client
->dev
, "firmware header is missing\n");
421 if (data
[0] != 1 || data
[1] != 0) {
422 dev_warn(&state
->client
->dev
, "firmware file version is wrong\n");
427 patch_version
= data
[2];
428 block_count
= data
[4];
429 crc_expected
= data
[7] << 8 | data
[6];
431 /* start uploading fw */
432 /* boot/wdog status */
433 ret
= si2165_writereg8(state
, REG_WDOG_AND_BOOT
, 0x00);
437 ret
= si2165_writereg8(state
, REG_RST_ALL
, 0x00);
440 /* boot/wdog status */
441 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, val
);
445 /* enable reset on error */
446 ret
= si2165_readreg8(state
, REG_EN_RST_ERROR
, val
);
449 ret
= si2165_readreg8(state
, REG_EN_RST_ERROR
, val
);
452 ret
= si2165_writereg8(state
, REG_EN_RST_ERROR
, 0x02);
456 /* start right after the header */
459 dev_info(&state
->client
->dev
, "%s: extracted patch_version=0x%02x, block_count=0x%02x, crc_expected=0x%04x\n",
460 __func__
, patch_version
, block_count
, crc_expected
);
462 ret
= si2165_upload_firmware_block(state
, data
, len
, &offset
, 1);
466 ret
= si2165_writereg8(state
, REG_PATCH_VERSION
, patch_version
);
471 ret
= si2165_writereg8(state
, REG_RST_CRC
, 0x01);
475 ret
= si2165_upload_firmware_block(state
, data
, len
,
476 &offset
, block_count
);
478 dev_err(&state
->client
->dev
,
479 "firmware could not be uploaded\n");
484 ret
= si2165_readreg16(state
, REG_CRC
, &val16
);
488 if (val16
!= crc_expected
) {
489 dev_err(&state
->client
->dev
,
490 "firmware crc mismatch %04x != %04x\n",
491 val16
, crc_expected
);
496 ret
= si2165_upload_firmware_block(state
, data
, len
, &offset
, 5);
501 dev_err(&state
->client
->dev
,
502 "firmware len mismatch %04x != %04x\n",
508 /* reset watchdog error register */
509 ret
= si2165_writereg_mask8(state
, REG_WDOG_AND_BOOT
, 0x02, 0x02);
513 /* enable reset on error */
514 ret
= si2165_writereg_mask8(state
, REG_EN_RST_ERROR
, 0x01, 0x01);
518 dev_info(&state
->client
->dev
, "fw load finished\n");
521 state
->firmware_loaded
= true;
524 release_firmware(fw
);
531 static int si2165_init(struct dvb_frontend
*fe
)
534 struct si2165_state
*state
= fe
->demodulator_priv
;
535 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
537 u8 patch_version
= 0x00;
539 dev_dbg(&state
->client
->dev
, "%s: called\n", __func__
);
542 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, state
->config
.chip_mode
);
545 /* dsp_clock_enable */
546 ret
= si2165_writereg8(state
, REG_DSP_CLOCK
, 0x01);
549 /* verify chip_mode */
550 ret
= si2165_readreg8(state
, REG_CHIP_MODE
, &val
);
553 if (val
!= state
->config
.chip_mode
) {
554 dev_err(&state
->client
->dev
, "could not set chip_mode\n");
559 ret
= si2165_writereg8(state
, REG_AGC_IF_TRI
, 0x00);
562 ret
= si2165_writereg8(state
, REG_AGC_IF_SLR
, 0x01);
565 ret
= si2165_writereg8(state
, REG_AGC2_OUTPUT
, 0x00);
568 ret
= si2165_writereg8(state
, REG_AGC2_CLKDIV
, 0x07);
572 ret
= si2165_writereg8(state
, REG_RSSI_PAD_CTRL
, 0x00);
575 ret
= si2165_writereg8(state
, REG_RSSI_ENABLE
, 0x00);
579 ret
= si2165_init_pll(state
);
583 /* enable chip_init */
584 ret
= si2165_writereg8(state
, REG_CHIP_INIT
, 0x01);
588 ret
= si2165_writereg8(state
, REG_START_INIT
, 0x01);
591 ret
= si2165_wait_init_done(state
);
595 /* disable chip_init */
596 ret
= si2165_writereg8(state
, REG_CHIP_INIT
, 0x00);
600 /* ber_pkt - default 65535 */
601 ret
= si2165_writereg16(state
, REG_BER_PKT
,
602 STATISTICS_PERIOD_PKT_COUNT
);
606 ret
= si2165_readreg8(state
, REG_PATCH_VERSION
, &patch_version
);
610 ret
= si2165_writereg8(state
, REG_AUTO_RESET
, 0x00);
615 ret
= si2165_writereg32(state
, REG_ADDR_JUMP
, 0xf4000000);
618 /* boot/wdog status */
619 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, &val
);
623 if (patch_version
== 0x00) {
624 ret
= si2165_upload_firmware(state
);
629 /* ts output config */
630 ret
= si2165_writereg8(state
, REG_TS_DATA_MODE
, 0x20);
633 ret
= si2165_writereg16(state
, REG_TS_TRI
, 0x00fe);
636 ret
= si2165_writereg24(state
, REG_TS_SLR
, 0x555555);
639 ret
= si2165_writereg8(state
, REG_TS_CLK_MODE
, 0x01);
642 ret
= si2165_writereg8(state
, REG_TS_PARALLEL_MODE
, 0x00);
646 c
= &state
->fe
.dtv_property_cache
;
648 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
649 c
->post_bit_error
.len
= 1;
650 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
651 c
->post_bit_count
.len
= 1;
652 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
659 static int si2165_sleep(struct dvb_frontend
*fe
)
662 struct si2165_state
*state
= fe
->demodulator_priv
;
664 /* dsp clock disable */
665 ret
= si2165_writereg8(state
, REG_DSP_CLOCK
, 0x00);
669 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, SI2165_MODE_OFF
);
675 static int si2165_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
680 struct si2165_state
*state
= fe
->demodulator_priv
;
681 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
682 u32 delsys
= c
->delivery_system
;
688 /* check fast signal type */
689 ret
= si2165_readreg8(state
, REG_CHECK_SIGNAL
, &u8tmp
);
692 switch (u8tmp
& 0x3) {
693 case 0: /* searching */
694 case 1: /* nothing */
696 case 2: /* digital signal */
697 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
701 case SYS_DVBC_ANNEX_A
:
702 /* check packet sync lock */
703 ret
= si2165_readreg8(state
, REG_PS_LOCK
, &u8tmp
);
707 *status
|= FE_HAS_SIGNAL
;
708 *status
|= FE_HAS_CARRIER
;
709 *status
|= FE_HAS_VITERBI
;
710 *status
|= FE_HAS_SYNC
;
716 ret
= si2165_readreg8(state
, REG_FEC_LOCK
, &u8tmp
);
720 *status
|= FE_HAS_SIGNAL
;
721 *status
|= FE_HAS_CARRIER
;
722 *status
|= FE_HAS_VITERBI
;
723 *status
|= FE_HAS_SYNC
;
724 *status
|= FE_HAS_LOCK
;
728 if (delsys
== SYS_DVBC_ANNEX_A
&& *status
& FE_HAS_VITERBI
) {
729 ret
= si2165_readreg24(state
, REG_C_N
, &u32tmp
);
735 * 1000 * 10 * log10(2^24 / regval) =
736 * 1000 * 10 * (log10(2^24) - log10(regval)) =
737 * 1000 * 10 * (intlog10(2^24) - intlog10(regval)) / 2^24
739 * intlog10(x) = log10(x) * 2^24
740 * intlog10(2^24) = log10(2^24) * 2^24 = 121210686
742 u32tmp
= (1000 * 10 * (121210686 - (u64
)intlog10(u32tmp
)))
744 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
745 c
->cnr
.stat
[0].svalue
= u32tmp
;
747 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
750 if (*status
& FE_HAS_VITERBI
) {
751 if (c
->post_bit_error
.stat
[0].scale
== FE_SCALE_NOT_AVAILABLE
) {
752 /* start new sampling period to get rid of old data*/
753 ret
= si2165_writereg8(state
, REG_BER_RST
, 0x01);
757 /* set scale to enter read code on next call */
758 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
759 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
760 c
->post_bit_error
.stat
[0].uvalue
= 0;
761 c
->post_bit_count
.stat
[0].uvalue
= 0;
764 * reset DVBv3 value to deliver a good result
770 ret
= si2165_readreg8(state
, REG_BER_AVAIL
, &u8tmp
);
777 ret
= si2165_readreg24(state
, REG_BER_BIT
,
782 c
->post_bit_error
.stat
[0].uvalue
+=
784 c
->post_bit_count
.stat
[0].uvalue
+=
785 STATISTICS_PERIOD_BIT_COUNT
;
787 /* start new sampling period */
788 ret
= si2165_writereg8(state
,
793 dev_dbg(&state
->client
->dev
,
794 "post_bit_error=%u post_bit_count=%u\n",
795 biterrcnt
, STATISTICS_PERIOD_BIT_COUNT
);
799 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
800 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
806 static int si2165_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
808 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
810 if (c
->cnr
.stat
[0].scale
== FE_SCALE_DECIBEL
)
811 *snr
= div_s64(c
->cnr
.stat
[0].svalue
, 100);
817 static int si2165_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
819 struct si2165_state
*state
= fe
->demodulator_priv
;
820 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
822 if (c
->post_bit_error
.stat
[0].scale
!= FE_SCALE_COUNTER
) {
827 *ber
= c
->post_bit_error
.stat
[0].uvalue
- state
->ber_prev
;
828 state
->ber_prev
= c
->post_bit_error
.stat
[0].uvalue
;
833 static int si2165_set_oversamp(struct si2165_state
*state
, u32 dvb_rate
)
841 oversamp
= si2165_get_fe_clk(state
);
843 do_div(oversamp
, dvb_rate
);
844 reg_value
= oversamp
& 0x3fffffff;
846 dev_dbg(&state
->client
->dev
, "Write oversamp=%#x\n", reg_value
);
847 return si2165_writereg32(state
, REG_OVERSAMP
, reg_value
);
850 static int si2165_set_if_freq_shift(struct si2165_state
*state
)
852 struct dvb_frontend
*fe
= &state
->fe
;
855 u32 fe_clk
= si2165_get_fe_clk(state
);
858 if (!fe
->ops
.tuner_ops
.get_if_frequency
) {
859 dev_err(&state
->client
->dev
,
860 "Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
867 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &IF
);
869 if_freq_shift
<<= 29;
871 do_div(if_freq_shift
, fe_clk
);
872 reg_value
= (s32
)if_freq_shift
;
874 if (state
->config
.inversion
)
875 reg_value
= -reg_value
;
877 reg_value
= reg_value
& 0x1fffffff;
879 /* if_freq_shift, usbdump contained 0x023ee08f; */
880 return si2165_writereg32(state
, REG_IF_FREQ_SHIFT
, reg_value
);
883 static const struct si2165_reg_value_pair dvbt_regs
[] = {
884 /* standard = DVB-T */
885 { REG_DVB_STANDARD
, 0x01 },
886 /* impulsive_noise_remover */
887 { REG_IMPULSIVE_NOISE_REM
, 0x01 },
888 { REG_AUTO_RESET
, 0x00 },
890 { REG_AGC2_MIN
, 0x41 },
891 { REG_AGC2_KACQ
, 0x0e },
892 { REG_AGC2_KLOC
, 0x10 },
894 { REG_AGC_UNFREEZE_THR
, 0x03 },
895 { REG_AGC_CRESTF_DBX8
, 0x78 },
897 { REG_AAF_CRESTF_DBX8
, 0x78 },
898 { REG_ACI_CRESTF_DBX8
, 0x68 },
899 /* freq_sync_range */
900 REG16(REG_FREQ_SYNC_RANGE
, 0x0064),
902 { REG_GP_REG0_MSB
, 0x00 }
905 static int si2165_set_frontend_dvbt(struct dvb_frontend
*fe
)
908 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
909 struct si2165_state
*state
= fe
->demodulator_priv
;
912 u32 bw_hz
= p
->bandwidth_hz
;
914 dev_dbg(&state
->client
->dev
, "%s: called\n", __func__
);
916 if (!state
->has_dvbt
)
919 /* no bandwidth auto-detection */
923 dvb_rate
= bw_hz
* 8 / 7;
924 bw10k
= bw_hz
/ 10000;
926 ret
= si2165_adjust_pll_divl(state
, 12);
930 /* bandwidth in 10KHz steps */
931 ret
= si2165_writereg16(state
, REG_T_BANDWIDTH
, bw10k
);
934 ret
= si2165_set_oversamp(state
, dvb_rate
);
938 ret
= si2165_write_reg_list(state
, dvbt_regs
, ARRAY_SIZE(dvbt_regs
));
945 static const struct si2165_reg_value_pair dvbc_regs
[] = {
946 /* standard = DVB-C */
947 { REG_DVB_STANDARD
, 0x05 },
950 { REG_AGC2_MIN
, 0x50 },
951 { REG_AGC2_KACQ
, 0x0e },
952 { REG_AGC2_KLOC
, 0x10 },
954 { REG_AGC_UNFREEZE_THR
, 0x03 },
955 { REG_AGC_CRESTF_DBX8
, 0x68 },
957 { REG_AAF_CRESTF_DBX8
, 0x68 },
958 { REG_ACI_CRESTF_DBX8
, 0x50 },
960 { REG_EQ_AUTO_CONTROL
, 0x0d },
962 { REG_KP_LOCK
, 0x05 },
963 { REG_CENTRAL_TAP
, 0x09 },
964 REG16(REG_UNKNOWN_350
, 0x3e80),
966 { REG_AUTO_RESET
, 0x01 },
967 REG16(REG_UNKNOWN_24C
, 0x0000),
968 REG16(REG_UNKNOWN_27C
, 0x0000),
969 { REG_SWEEP_STEP
, 0x03 },
970 { REG_AGC_IF_TRI
, 0x00 },
973 static int si2165_set_frontend_dvbc(struct dvb_frontend
*fe
)
975 struct si2165_state
*state
= fe
->demodulator_priv
;
977 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
978 const u32 dvb_rate
= p
->symbol_rate
;
981 if (!state
->has_dvbc
)
987 ret
= si2165_adjust_pll_divl(state
, 14);
992 ret
= si2165_set_oversamp(state
, dvb_rate
);
996 switch (p
->modulation
) {
1017 ret
= si2165_writereg8(state
, REG_REQ_CONSTELLATION
, u8tmp
);
1021 ret
= si2165_writereg32(state
, REG_LOCK_TIMEOUT
, 0x007a1200);
1025 ret
= si2165_write_reg_list(state
, dvbc_regs
, ARRAY_SIZE(dvbc_regs
));
1032 static const struct si2165_reg_value_pair adc_rewrite
[] = {
1033 { REG_ADC_RI1
, 0x46 },
1034 { REG_ADC_RI3
, 0x00 },
1035 { REG_ADC_RI5
, 0x0a },
1036 { REG_ADC_RI6
, 0xff },
1037 { REG_ADC_RI8
, 0x70 }
1040 static int si2165_set_frontend(struct dvb_frontend
*fe
)
1042 struct si2165_state
*state
= fe
->demodulator_priv
;
1043 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
1044 u32 delsys
= p
->delivery_system
;
1048 /* initial setting of if freq shift */
1049 ret
= si2165_set_if_freq_shift(state
);
1055 ret
= si2165_set_frontend_dvbt(fe
);
1059 case SYS_DVBC_ANNEX_A
:
1060 ret
= si2165_set_frontend_dvbc(fe
);
1069 ret
= si2165_writereg32(state
, REG_ADDR_JUMP
, 0xf4000000);
1073 if (fe
->ops
.tuner_ops
.set_params
)
1074 fe
->ops
.tuner_ops
.set_params(fe
);
1076 /* recalc if_freq_shift if IF might has changed */
1077 ret
= si2165_set_if_freq_shift(state
);
1081 /* boot/wdog status */
1082 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, val
);
1085 ret
= si2165_writereg8(state
, REG_WDOG_AND_BOOT
, 0x00);
1090 ret
= si2165_writereg8(state
, REG_RST_ALL
, 0x00);
1094 ret
= si2165_writereg32(state
, REG_GP_REG0_LSB
, 0x00000000);
1098 /* write adc values after each reset*/
1099 ret
= si2165_write_reg_list(state
, adc_rewrite
,
1100 ARRAY_SIZE(adc_rewrite
));
1105 ret
= si2165_writereg8(state
, REG_START_SYNCHRO
, 0x01);
1108 /* boot/wdog status */
1109 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, val
);
1116 static const struct dvb_frontend_ops si2165_ops
= {
1118 .name
= "Silicon Labs ",
1120 .symbol_rate_min
= 1000000,
1121 .symbol_rate_max
= 7200000,
1123 .frequency_stepsize
= 166667,
1124 .caps
= FE_CAN_FEC_1_2
|
1136 FE_CAN_GUARD_INTERVAL_AUTO
|
1137 FE_CAN_HIERARCHY_AUTO
|
1139 FE_CAN_TRANSMISSION_MODE_AUTO
|
1143 .get_tune_settings
= si2165_get_tune_settings
,
1145 .init
= si2165_init
,
1146 .sleep
= si2165_sleep
,
1148 .set_frontend
= si2165_set_frontend
,
1149 .read_status
= si2165_read_status
,
1150 .read_snr
= si2165_read_snr
,
1151 .read_ber
= si2165_read_ber
,
1154 static int si2165_probe(struct i2c_client
*client
,
1155 const struct i2c_device_id
*id
)
1157 struct si2165_state
*state
= NULL
;
1158 struct si2165_platform_data
*pdata
= client
->dev
.platform_data
;
1163 const char *chip_name
;
1164 static const struct regmap_config regmap_config
= {
1167 .max_register
= 0x08ff,
1170 /* allocate memory for the internal state */
1171 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1178 state
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
1179 if (IS_ERR(state
->regmap
)) {
1180 ret
= PTR_ERR(state
->regmap
);
1184 /* setup the state */
1185 state
->client
= client
;
1186 state
->config
.i2c_addr
= client
->addr
;
1187 state
->config
.chip_mode
= pdata
->chip_mode
;
1188 state
->config
.ref_freq_hz
= pdata
->ref_freq_hz
;
1189 state
->config
.inversion
= pdata
->inversion
;
1191 if (state
->config
.ref_freq_hz
< 4000000 ||
1192 state
->config
.ref_freq_hz
> 27000000) {
1193 dev_err(&state
->client
->dev
, "ref_freq of %d Hz not supported by this driver\n",
1194 state
->config
.ref_freq_hz
);
1199 /* create dvb_frontend */
1200 memcpy(&state
->fe
.ops
, &si2165_ops
,
1201 sizeof(struct dvb_frontend_ops
));
1202 state
->fe
.ops
.release
= NULL
;
1203 state
->fe
.demodulator_priv
= state
;
1204 i2c_set_clientdata(client
, state
);
1207 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, state
->config
.chip_mode
);
1211 ret
= si2165_readreg8(state
, REG_CHIP_MODE
, &val
);
1214 if (val
!= state
->config
.chip_mode
)
1217 ret
= si2165_readreg8(state
, REG_CHIP_REVCODE
, &state
->chip_revcode
);
1221 ret
= si2165_readreg8(state
, REV_CHIP_TYPE
, &state
->chip_type
);
1226 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, SI2165_MODE_OFF
);
1230 if (state
->chip_revcode
< 26)
1231 rev_char
= 'A' + state
->chip_revcode
;
1235 switch (state
->chip_type
) {
1237 chip_name
= "Si2161";
1238 state
->has_dvbt
= true;
1241 chip_name
= "Si2165";
1242 state
->has_dvbt
= true;
1243 state
->has_dvbc
= true;
1246 dev_err(&state
->client
->dev
, "Unsupported Silicon Labs chip (type %d, rev %d)\n",
1247 state
->chip_type
, state
->chip_revcode
);
1251 dev_info(&state
->client
->dev
,
1252 "Detected Silicon Labs %s-%c (type %d, rev %d)\n",
1253 chip_name
, rev_char
, state
->chip_type
,
1254 state
->chip_revcode
);
1256 strlcat(state
->fe
.ops
.info
.name
, chip_name
,
1257 sizeof(state
->fe
.ops
.info
.name
));
1260 if (state
->has_dvbt
) {
1261 state
->fe
.ops
.delsys
[n
++] = SYS_DVBT
;
1262 strlcat(state
->fe
.ops
.info
.name
, " DVB-T",
1263 sizeof(state
->fe
.ops
.info
.name
));
1265 if (state
->has_dvbc
) {
1266 state
->fe
.ops
.delsys
[n
++] = SYS_DVBC_ANNEX_A
;
1267 strlcat(state
->fe
.ops
.info
.name
, " DVB-C",
1268 sizeof(state
->fe
.ops
.info
.name
));
1271 /* return fe pointer */
1272 *pdata
->fe
= &state
->fe
;
1280 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
1284 static int si2165_remove(struct i2c_client
*client
)
1286 struct si2165_state
*state
= i2c_get_clientdata(client
);
1288 dev_dbg(&client
->dev
, "\n");
1294 static const struct i2c_device_id si2165_id_table
[] = {
1298 MODULE_DEVICE_TABLE(i2c
, si2165_id_table
);
1300 static struct i2c_driver si2165_driver
= {
1302 .owner
= THIS_MODULE
,
1305 .probe
= si2165_probe
,
1306 .remove
= si2165_remove
,
1307 .id_table
= si2165_id_table
,
1310 module_i2c_driver(si2165_driver
);
1312 MODULE_DESCRIPTION("Silicon Labs Si2165 DVB-C/-T Demodulator driver");
1313 MODULE_AUTHOR("Matthias Schwarzott <zzam@gentoo.org>");
1314 MODULE_LICENSE("GPL");
1315 MODULE_FIRMWARE(SI2165_FIRMWARE_REV_D
);