1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Silicon Labs Si2161 DVB-T and Si2165 DVB-C/-T Demodulator
5 * Copyright (C) 2013-2017 Matthias Schwarzott <zzam@gentoo.org>
8 * https://www.silabs.com/Support%20Documents/TechnicalDocs/Si2165-short.pdf
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/firmware.h>
19 #include <linux/regmap.h>
21 #include <media/dvb_frontend.h>
22 #include <linux/int_log.h>
23 #include "si2165_priv.h"
27 * Hauppauge WinTV-HVR-930C-HD B130 / PCTV QuatroStick 521e 1113xx
30 * Hauppauge WinTV-HVR-930C-HD B131 / PCTV QuatroStick 522e 1114xx
31 * uses 24 MHz clock provided by tuner
35 struct i2c_client
*client
;
37 struct regmap
*regmap
;
39 struct dvb_frontend fe
;
41 struct si2165_config config
;
46 /* calculated by xtal and div settings */
59 static int si2165_write(struct si2165_state
*state
, const u16 reg
,
60 const u8
*src
, const int count
)
64 dev_dbg(&state
->client
->dev
, "i2c write: reg: 0x%04x, data: %*ph\n",
67 ret
= regmap_bulk_write(state
->regmap
, reg
, src
, count
);
70 dev_err(&state
->client
->dev
, "%s: ret == %d\n", __func__
, ret
);
75 static int si2165_read(struct si2165_state
*state
,
76 const u16 reg
, u8
*val
, const int count
)
78 int ret
= regmap_bulk_read(state
->regmap
, reg
, val
, count
);
81 dev_err(&state
->client
->dev
, "%s: error (addr %02x reg %04x error (ret == %i)\n",
82 __func__
, state
->config
.i2c_addr
, reg
, ret
);
86 dev_dbg(&state
->client
->dev
, "i2c read: reg: 0x%04x, data: %*ph\n",
92 static int si2165_readreg8(struct si2165_state
*state
,
93 const u16 reg
, u8
*val
)
96 int ret
= regmap_read(state
->regmap
, reg
, &val_tmp
);
98 dev_dbg(&state
->client
->dev
, "reg read: R(0x%04x)=0x%02x\n", reg
, *val
);
102 static int si2165_readreg16(struct si2165_state
*state
,
103 const u16 reg
, u16
*val
)
107 int ret
= si2165_read(state
, reg
, buf
, 2);
108 *val
= buf
[0] | buf
[1] << 8;
109 dev_dbg(&state
->client
->dev
, "reg read: R(0x%04x)=0x%04x\n", reg
, *val
);
113 static int si2165_readreg24(struct si2165_state
*state
,
114 const u16 reg
, u32
*val
)
118 int ret
= si2165_read(state
, reg
, buf
, 3);
119 *val
= buf
[0] | buf
[1] << 8 | buf
[2] << 16;
120 dev_dbg(&state
->client
->dev
, "reg read: R(0x%04x)=0x%06x\n", reg
, *val
);
124 static int si2165_writereg8(struct si2165_state
*state
, const u16 reg
, u8 val
)
126 return regmap_write(state
->regmap
, reg
, val
);
129 static int si2165_writereg16(struct si2165_state
*state
, const u16 reg
, u16 val
)
131 u8 buf
[2] = { val
& 0xff, (val
>> 8) & 0xff };
133 return si2165_write(state
, reg
, buf
, 2);
136 static int si2165_writereg24(struct si2165_state
*state
, const u16 reg
, u32 val
)
138 u8 buf
[3] = { val
& 0xff, (val
>> 8) & 0xff, (val
>> 16) & 0xff };
140 return si2165_write(state
, reg
, buf
, 3);
143 static int si2165_writereg32(struct si2165_state
*state
, const u16 reg
, u32 val
)
151 return si2165_write(state
, reg
, buf
, 4);
154 static int si2165_writereg_mask8(struct si2165_state
*state
, const u16 reg
,
159 int ret
= si2165_readreg8(state
, reg
, &tmp
);
168 return si2165_writereg8(state
, reg
, val
);
171 #define REG16(reg, val) \
172 { (reg), (val) & 0xff }, \
173 { (reg) + 1, (val) >> 8 & 0xff }
174 struct si2165_reg_value_pair
{
179 static int si2165_write_reg_list(struct si2165_state
*state
,
180 const struct si2165_reg_value_pair
*regs
,
186 for (i
= 0; i
< count
; i
++) {
187 ret
= si2165_writereg8(state
, regs
[i
].reg
, regs
[i
].val
);
194 static int si2165_get_tune_settings(struct dvb_frontend
*fe
,
195 struct dvb_frontend_tune_settings
*s
)
197 s
->min_delay_ms
= 1000;
201 static int si2165_init_pll(struct si2165_state
*state
)
203 u32 ref_freq_hz
= state
->config
.ref_freq_hz
;
204 u8 divr
= 1; /* 1..7 */
205 u8 divp
= 1; /* only 1 or 4 */
206 u8 divn
= 56; /* 1..63 */
212 * hardcoded values can be deleted if calculation is verified
213 * or it yields the same values as the windows driver
215 switch (ref_freq_hz
) {
225 /* ref_freq / divr must be between 4 and 16 MHz */
226 if (ref_freq_hz
> 16000000u)
230 * now select divn and divp such that
231 * fvco is in 1624..1824 MHz
233 if (1624000000u * divr
> ref_freq_hz
* 2u * 63u)
236 /* is this already correct regarding rounding? */
237 divn
= 1624000000u * divr
/ (ref_freq_hz
* 2u * divp
);
241 /* adc_clk and sys_clk depend on xtal and pll settings */
242 state
->fvco_hz
= ref_freq_hz
/ divr
244 state
->adc_clk
= state
->fvco_hz
/ (divm
* 4u);
245 state
->sys_clk
= state
->fvco_hz
/ (divl
* 2u);
247 /* write all 4 pll registers 0x00a0..0x00a3 at once */
250 buf
[2] = (divn
& 0x3f) | ((divp
== 1) ? 0x40 : 0x00) | 0x80;
252 return si2165_write(state
, REG_PLL_DIVL
, buf
, 4);
255 static int si2165_adjust_pll_divl(struct si2165_state
*state
, u8 divl
)
257 state
->sys_clk
= state
->fvco_hz
/ (divl
* 2u);
258 return si2165_writereg8(state
, REG_PLL_DIVL
, divl
);
261 static u32
si2165_get_fe_clk(struct si2165_state
*state
)
263 /* assume Oversampling mode Ovr4 is used */
264 return state
->adc_clk
;
267 static int si2165_wait_init_done(struct si2165_state
*state
)
273 for (i
= 0; i
< 3; ++i
) {
274 ret
= si2165_readreg8(state
, REG_INIT_DONE
, &val
);
279 usleep_range(1000, 50000);
281 dev_err(&state
->client
->dev
, "init_done was not set\n");
285 static int si2165_upload_firmware_block(struct si2165_state
*state
,
286 const u8
*data
, u32 len
, u32
*poffset
,
290 u8 buf_ctrl
[4] = { 0x00, 0x00, 0x00, 0xc0 };
293 u32 offset
= poffset
? *poffset
: 0;
300 dev_dbg(&state
->client
->dev
,
301 "fw load: %s: called with len=0x%x offset=0x%x blockcount=0x%x\n",
302 __func__
, len
, offset
, block_count
);
303 while (offset
+ 12 <= len
&& cur_block
< block_count
) {
304 dev_dbg(&state
->client
->dev
,
305 "fw load: %s: in while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n",
306 __func__
, len
, offset
, cur_block
, block_count
);
307 wordcount
= data
[offset
];
308 if (wordcount
< 1 || data
[offset
+ 1] ||
309 data
[offset
+ 2] || data
[offset
+ 3]) {
310 dev_warn(&state
->client
->dev
,
311 "bad fw data[0..3] = %*ph\n",
316 if (offset
+ 8 + wordcount
* 4 > len
) {
317 dev_warn(&state
->client
->dev
,
318 "len is too small for block len=%d, wordcount=%d\n",
323 buf_ctrl
[0] = wordcount
- 1;
325 ret
= si2165_write(state
, REG_DCOM_CONTROL_BYTE
, buf_ctrl
, 4);
328 ret
= si2165_write(state
, REG_DCOM_ADDR
, data
+ offset
+ 4, 4);
334 while (wordcount
> 0) {
335 ret
= si2165_write(state
, REG_DCOM_DATA
,
345 dev_dbg(&state
->client
->dev
,
346 "fw load: %s: after while len=0x%x offset=0x%x cur_block=0x%x blockcount=0x%x\n",
347 __func__
, len
, offset
, cur_block
, block_count
);
352 dev_dbg(&state
->client
->dev
,
353 "fw load: %s: returned offset=0x%x\n",
361 static int si2165_upload_firmware(struct si2165_state
*state
)
368 const struct firmware
*fw
= NULL
;
377 switch (state
->chip_revcode
) {
378 case 0x03: /* revision D */
379 fw_file
= SI2165_FIRMWARE_REV_D
;
382 dev_info(&state
->client
->dev
, "no firmware file for revision=%d\n",
383 state
->chip_revcode
);
387 /* request the firmware, this will block and timeout */
388 ret
= request_firmware(&fw
, fw_file
, &state
->client
->dev
);
390 dev_warn(&state
->client
->dev
, "firmware file '%s' not found\n",
398 dev_info(&state
->client
->dev
, "downloading firmware from file '%s' size=%d\n",
402 dev_warn(&state
->client
->dev
, "firmware size is not multiple of 4\n");
407 /* check header (8 bytes) */
409 dev_warn(&state
->client
->dev
, "firmware header is missing\n");
414 if (data
[0] != 1 || data
[1] != 0) {
415 dev_warn(&state
->client
->dev
, "firmware file version is wrong\n");
420 patch_version
= data
[2];
421 block_count
= data
[4];
422 crc_expected
= data
[7] << 8 | data
[6];
424 /* start uploading fw */
425 /* boot/wdog status */
426 ret
= si2165_writereg8(state
, REG_WDOG_AND_BOOT
, 0x00);
430 ret
= si2165_writereg8(state
, REG_RST_ALL
, 0x00);
433 /* boot/wdog status */
434 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, val
);
438 /* enable reset on error */
439 ret
= si2165_readreg8(state
, REG_EN_RST_ERROR
, val
);
442 ret
= si2165_readreg8(state
, REG_EN_RST_ERROR
, val
);
445 ret
= si2165_writereg8(state
, REG_EN_RST_ERROR
, 0x02);
449 /* start right after the header */
452 dev_info(&state
->client
->dev
, "%s: extracted patch_version=0x%02x, block_count=0x%02x, crc_expected=0x%04x\n",
453 __func__
, patch_version
, block_count
, crc_expected
);
455 ret
= si2165_upload_firmware_block(state
, data
, len
, &offset
, 1);
459 ret
= si2165_writereg8(state
, REG_PATCH_VERSION
, patch_version
);
464 ret
= si2165_writereg8(state
, REG_RST_CRC
, 0x01);
468 ret
= si2165_upload_firmware_block(state
, data
, len
,
469 &offset
, block_count
);
471 dev_err(&state
->client
->dev
,
472 "firmware could not be uploaded\n");
477 ret
= si2165_readreg16(state
, REG_CRC
, &val16
);
481 if (val16
!= crc_expected
) {
482 dev_err(&state
->client
->dev
,
483 "firmware crc mismatch %04x != %04x\n",
484 val16
, crc_expected
);
489 ret
= si2165_upload_firmware_block(state
, data
, len
, &offset
, 5);
494 dev_err(&state
->client
->dev
,
495 "firmware len mismatch %04x != %04x\n",
501 /* reset watchdog error register */
502 ret
= si2165_writereg_mask8(state
, REG_WDOG_AND_BOOT
, 0x02, 0x02);
506 /* enable reset on error */
507 ret
= si2165_writereg_mask8(state
, REG_EN_RST_ERROR
, 0x01, 0x01);
511 dev_info(&state
->client
->dev
, "fw load finished\n");
514 state
->firmware_loaded
= true;
516 release_firmware(fw
);
522 static int si2165_init(struct dvb_frontend
*fe
)
525 struct si2165_state
*state
= fe
->demodulator_priv
;
526 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
528 u8 patch_version
= 0x00;
530 dev_dbg(&state
->client
->dev
, "%s: called\n", __func__
);
533 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, state
->config
.chip_mode
);
536 /* dsp_clock_enable */
537 ret
= si2165_writereg8(state
, REG_DSP_CLOCK
, 0x01);
540 /* verify chip_mode */
541 ret
= si2165_readreg8(state
, REG_CHIP_MODE
, &val
);
544 if (val
!= state
->config
.chip_mode
) {
545 dev_err(&state
->client
->dev
, "could not set chip_mode\n");
550 ret
= si2165_writereg8(state
, REG_AGC_IF_TRI
, 0x00);
553 ret
= si2165_writereg8(state
, REG_AGC_IF_SLR
, 0x01);
556 ret
= si2165_writereg8(state
, REG_AGC2_OUTPUT
, 0x00);
559 ret
= si2165_writereg8(state
, REG_AGC2_CLKDIV
, 0x07);
563 ret
= si2165_writereg8(state
, REG_RSSI_PAD_CTRL
, 0x00);
566 ret
= si2165_writereg8(state
, REG_RSSI_ENABLE
, 0x00);
570 ret
= si2165_init_pll(state
);
574 /* enable chip_init */
575 ret
= si2165_writereg8(state
, REG_CHIP_INIT
, 0x01);
579 ret
= si2165_writereg8(state
, REG_START_INIT
, 0x01);
582 ret
= si2165_wait_init_done(state
);
586 /* disable chip_init */
587 ret
= si2165_writereg8(state
, REG_CHIP_INIT
, 0x00);
591 /* ber_pkt - default 65535 */
592 ret
= si2165_writereg16(state
, REG_BER_PKT
,
593 STATISTICS_PERIOD_PKT_COUNT
);
597 ret
= si2165_readreg8(state
, REG_PATCH_VERSION
, &patch_version
);
601 ret
= si2165_writereg8(state
, REG_AUTO_RESET
, 0x00);
606 ret
= si2165_writereg32(state
, REG_ADDR_JUMP
, 0xf4000000);
609 /* boot/wdog status */
610 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, &val
);
614 if (patch_version
== 0x00) {
615 ret
= si2165_upload_firmware(state
);
620 /* ts output config */
621 ret
= si2165_writereg8(state
, REG_TS_DATA_MODE
, 0x20);
624 ret
= si2165_writereg16(state
, REG_TS_TRI
, 0x00fe);
627 ret
= si2165_writereg24(state
, REG_TS_SLR
, 0x555555);
630 ret
= si2165_writereg8(state
, REG_TS_CLK_MODE
, 0x01);
633 ret
= si2165_writereg8(state
, REG_TS_PARALLEL_MODE
, 0x00);
637 c
= &state
->fe
.dtv_property_cache
;
639 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
640 c
->post_bit_error
.len
= 1;
641 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
642 c
->post_bit_count
.len
= 1;
643 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
650 static int si2165_sleep(struct dvb_frontend
*fe
)
653 struct si2165_state
*state
= fe
->demodulator_priv
;
655 /* dsp clock disable */
656 ret
= si2165_writereg8(state
, REG_DSP_CLOCK
, 0x00);
660 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, SI2165_MODE_OFF
);
666 static int si2165_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
671 struct si2165_state
*state
= fe
->demodulator_priv
;
672 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
673 u32 delsys
= c
->delivery_system
;
679 /* check fast signal type */
680 ret
= si2165_readreg8(state
, REG_CHECK_SIGNAL
, &u8tmp
);
683 switch (u8tmp
& 0x3) {
684 case 0: /* searching */
685 case 1: /* nothing */
687 case 2: /* digital signal */
688 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
692 case SYS_DVBC_ANNEX_A
:
693 /* check packet sync lock */
694 ret
= si2165_readreg8(state
, REG_PS_LOCK
, &u8tmp
);
698 *status
|= FE_HAS_SIGNAL
;
699 *status
|= FE_HAS_CARRIER
;
700 *status
|= FE_HAS_VITERBI
;
701 *status
|= FE_HAS_SYNC
;
707 ret
= si2165_readreg8(state
, REG_FEC_LOCK
, &u8tmp
);
711 *status
|= FE_HAS_SIGNAL
;
712 *status
|= FE_HAS_CARRIER
;
713 *status
|= FE_HAS_VITERBI
;
714 *status
|= FE_HAS_SYNC
;
715 *status
|= FE_HAS_LOCK
;
719 if (delsys
== SYS_DVBC_ANNEX_A
&& *status
& FE_HAS_VITERBI
) {
720 ret
= si2165_readreg24(state
, REG_C_N
, &u32tmp
);
726 * 1000 * 10 * log10(2^24 / regval) =
727 * 1000 * 10 * (log10(2^24) - log10(regval)) =
728 * 1000 * 10 * (intlog10(2^24) - intlog10(regval)) / 2^24
730 * intlog10(x) = log10(x) * 2^24
731 * intlog10(2^24) = log10(2^24) * 2^24 = 121210686
733 u32tmp
= (1000 * 10 * (121210686 - (u64
)intlog10(u32tmp
)))
735 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
736 c
->cnr
.stat
[0].svalue
= u32tmp
;
738 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
741 if (*status
& FE_HAS_VITERBI
) {
742 if (c
->post_bit_error
.stat
[0].scale
== FE_SCALE_NOT_AVAILABLE
) {
743 /* start new sampling period to get rid of old data*/
744 ret
= si2165_writereg8(state
, REG_BER_RST
, 0x01);
748 /* set scale to enter read code on next call */
749 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
750 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
751 c
->post_bit_error
.stat
[0].uvalue
= 0;
752 c
->post_bit_count
.stat
[0].uvalue
= 0;
755 * reset DVBv3 value to deliver a good result
761 ret
= si2165_readreg8(state
, REG_BER_AVAIL
, &u8tmp
);
768 ret
= si2165_readreg24(state
, REG_BER_BIT
,
773 c
->post_bit_error
.stat
[0].uvalue
+=
775 c
->post_bit_count
.stat
[0].uvalue
+=
776 STATISTICS_PERIOD_BIT_COUNT
;
778 /* start new sampling period */
779 ret
= si2165_writereg8(state
,
784 dev_dbg(&state
->client
->dev
,
785 "post_bit_error=%u post_bit_count=%u\n",
786 biterrcnt
, STATISTICS_PERIOD_BIT_COUNT
);
790 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
791 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
797 static int si2165_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
799 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
801 if (c
->cnr
.stat
[0].scale
== FE_SCALE_DECIBEL
)
802 *snr
= div_s64(c
->cnr
.stat
[0].svalue
, 100);
808 static int si2165_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
810 struct si2165_state
*state
= fe
->demodulator_priv
;
811 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
813 if (c
->post_bit_error
.stat
[0].scale
!= FE_SCALE_COUNTER
) {
818 *ber
= c
->post_bit_error
.stat
[0].uvalue
- state
->ber_prev
;
819 state
->ber_prev
= c
->post_bit_error
.stat
[0].uvalue
;
824 static int si2165_set_oversamp(struct si2165_state
*state
, u32 dvb_rate
)
832 oversamp
= si2165_get_fe_clk(state
);
834 do_div(oversamp
, dvb_rate
);
835 reg_value
= oversamp
& 0x3fffffff;
837 dev_dbg(&state
->client
->dev
, "Write oversamp=%#x\n", reg_value
);
838 return si2165_writereg32(state
, REG_OVERSAMP
, reg_value
);
841 static int si2165_set_if_freq_shift(struct si2165_state
*state
)
843 struct dvb_frontend
*fe
= &state
->fe
;
846 u32 fe_clk
= si2165_get_fe_clk(state
);
849 if (!fe
->ops
.tuner_ops
.get_if_frequency
) {
850 dev_err(&state
->client
->dev
,
851 "Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
858 fe
->ops
.tuner_ops
.get_if_frequency(fe
, &IF
);
860 if_freq_shift
<<= 29;
862 do_div(if_freq_shift
, fe_clk
);
863 reg_value
= (s32
)if_freq_shift
;
865 if (state
->config
.inversion
)
866 reg_value
= -reg_value
;
868 reg_value
= reg_value
& 0x1fffffff;
870 /* if_freq_shift, usbdump contained 0x023ee08f; */
871 return si2165_writereg32(state
, REG_IF_FREQ_SHIFT
, reg_value
);
874 static const struct si2165_reg_value_pair dvbt_regs
[] = {
875 /* standard = DVB-T */
876 { REG_DVB_STANDARD
, 0x01 },
877 /* impulsive_noise_remover */
878 { REG_IMPULSIVE_NOISE_REM
, 0x01 },
879 { REG_AUTO_RESET
, 0x00 },
881 { REG_AGC2_MIN
, 0x41 },
882 { REG_AGC2_KACQ
, 0x0e },
883 { REG_AGC2_KLOC
, 0x10 },
885 { REG_AGC_UNFREEZE_THR
, 0x03 },
886 { REG_AGC_CRESTF_DBX8
, 0x78 },
888 { REG_AAF_CRESTF_DBX8
, 0x78 },
889 { REG_ACI_CRESTF_DBX8
, 0x68 },
890 /* freq_sync_range */
891 REG16(REG_FREQ_SYNC_RANGE
, 0x0064),
893 { REG_GP_REG0_MSB
, 0x00 }
896 static int si2165_set_frontend_dvbt(struct dvb_frontend
*fe
)
899 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
900 struct si2165_state
*state
= fe
->demodulator_priv
;
903 u32 bw_hz
= p
->bandwidth_hz
;
905 dev_dbg(&state
->client
->dev
, "%s: called\n", __func__
);
907 if (!state
->has_dvbt
)
910 /* no bandwidth auto-detection */
914 dvb_rate
= bw_hz
* 8 / 7;
915 bw10k
= bw_hz
/ 10000;
917 ret
= si2165_adjust_pll_divl(state
, 12);
921 /* bandwidth in 10KHz steps */
922 ret
= si2165_writereg16(state
, REG_T_BANDWIDTH
, bw10k
);
925 ret
= si2165_set_oversamp(state
, dvb_rate
);
929 ret
= si2165_write_reg_list(state
, dvbt_regs
, ARRAY_SIZE(dvbt_regs
));
936 static const struct si2165_reg_value_pair dvbc_regs
[] = {
937 /* standard = DVB-C */
938 { REG_DVB_STANDARD
, 0x05 },
941 { REG_AGC2_MIN
, 0x50 },
942 { REG_AGC2_KACQ
, 0x0e },
943 { REG_AGC2_KLOC
, 0x10 },
945 { REG_AGC_UNFREEZE_THR
, 0x03 },
946 { REG_AGC_CRESTF_DBX8
, 0x68 },
948 { REG_AAF_CRESTF_DBX8
, 0x68 },
949 { REG_ACI_CRESTF_DBX8
, 0x50 },
951 { REG_EQ_AUTO_CONTROL
, 0x0d },
953 { REG_KP_LOCK
, 0x05 },
954 { REG_CENTRAL_TAP
, 0x09 },
955 REG16(REG_UNKNOWN_350
, 0x3e80),
957 { REG_AUTO_RESET
, 0x01 },
958 REG16(REG_UNKNOWN_24C
, 0x0000),
959 REG16(REG_UNKNOWN_27C
, 0x0000),
960 { REG_SWEEP_STEP
, 0x03 },
961 { REG_AGC_IF_TRI
, 0x00 },
964 static int si2165_set_frontend_dvbc(struct dvb_frontend
*fe
)
966 struct si2165_state
*state
= fe
->demodulator_priv
;
968 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
969 const u32 dvb_rate
= p
->symbol_rate
;
972 if (!state
->has_dvbc
)
978 ret
= si2165_adjust_pll_divl(state
, 14);
983 ret
= si2165_set_oversamp(state
, dvb_rate
);
987 switch (p
->modulation
) {
1008 ret
= si2165_writereg8(state
, REG_REQ_CONSTELLATION
, u8tmp
);
1012 ret
= si2165_writereg32(state
, REG_LOCK_TIMEOUT
, 0x007a1200);
1016 ret
= si2165_write_reg_list(state
, dvbc_regs
, ARRAY_SIZE(dvbc_regs
));
1023 static const struct si2165_reg_value_pair adc_rewrite
[] = {
1024 { REG_ADC_RI1
, 0x46 },
1025 { REG_ADC_RI3
, 0x00 },
1026 { REG_ADC_RI5
, 0x0a },
1027 { REG_ADC_RI6
, 0xff },
1028 { REG_ADC_RI8
, 0x70 }
1031 static int si2165_set_frontend(struct dvb_frontend
*fe
)
1033 struct si2165_state
*state
= fe
->demodulator_priv
;
1034 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
1035 u32 delsys
= p
->delivery_system
;
1039 /* initial setting of if freq shift */
1040 ret
= si2165_set_if_freq_shift(state
);
1046 ret
= si2165_set_frontend_dvbt(fe
);
1050 case SYS_DVBC_ANNEX_A
:
1051 ret
= si2165_set_frontend_dvbc(fe
);
1060 ret
= si2165_writereg32(state
, REG_ADDR_JUMP
, 0xf4000000);
1064 if (fe
->ops
.tuner_ops
.set_params
)
1065 fe
->ops
.tuner_ops
.set_params(fe
);
1067 /* recalc if_freq_shift if IF might has changed */
1068 ret
= si2165_set_if_freq_shift(state
);
1072 /* boot/wdog status */
1073 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, val
);
1076 ret
= si2165_writereg8(state
, REG_WDOG_AND_BOOT
, 0x00);
1081 ret
= si2165_writereg8(state
, REG_RST_ALL
, 0x00);
1085 ret
= si2165_writereg32(state
, REG_GP_REG0_LSB
, 0x00000000);
1089 /* write adc values after each reset*/
1090 ret
= si2165_write_reg_list(state
, adc_rewrite
,
1091 ARRAY_SIZE(adc_rewrite
));
1096 ret
= si2165_writereg8(state
, REG_START_SYNCHRO
, 0x01);
1099 /* boot/wdog status */
1100 ret
= si2165_readreg8(state
, REG_WDOG_AND_BOOT
, val
);
1107 static const struct dvb_frontend_ops si2165_ops
= {
1109 .name
= "Silicon Labs ",
1111 .symbol_rate_min
= 1000000,
1112 .symbol_rate_max
= 7200000,
1114 .frequency_stepsize_hz
= 166667,
1115 .caps
= FE_CAN_FEC_1_2
|
1127 FE_CAN_GUARD_INTERVAL_AUTO
|
1128 FE_CAN_HIERARCHY_AUTO
|
1130 FE_CAN_TRANSMISSION_MODE_AUTO
|
1134 .get_tune_settings
= si2165_get_tune_settings
,
1136 .init
= si2165_init
,
1137 .sleep
= si2165_sleep
,
1139 .set_frontend
= si2165_set_frontend
,
1140 .read_status
= si2165_read_status
,
1141 .read_snr
= si2165_read_snr
,
1142 .read_ber
= si2165_read_ber
,
1145 static int si2165_probe(struct i2c_client
*client
)
1147 struct si2165_state
*state
= NULL
;
1148 struct si2165_platform_data
*pdata
= client
->dev
.platform_data
;
1153 const char *chip_name
;
1154 static const struct regmap_config regmap_config
= {
1157 .max_register
= 0x08ff,
1160 /* allocate memory for the internal state */
1161 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
1168 state
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
1169 if (IS_ERR(state
->regmap
)) {
1170 ret
= PTR_ERR(state
->regmap
);
1174 /* setup the state */
1175 state
->client
= client
;
1176 state
->config
.i2c_addr
= client
->addr
;
1177 state
->config
.chip_mode
= pdata
->chip_mode
;
1178 state
->config
.ref_freq_hz
= pdata
->ref_freq_hz
;
1179 state
->config
.inversion
= pdata
->inversion
;
1181 if (state
->config
.ref_freq_hz
< 4000000 ||
1182 state
->config
.ref_freq_hz
> 27000000) {
1183 dev_err(&state
->client
->dev
, "ref_freq of %d Hz not supported by this driver\n",
1184 state
->config
.ref_freq_hz
);
1189 /* create dvb_frontend */
1190 memcpy(&state
->fe
.ops
, &si2165_ops
,
1191 sizeof(struct dvb_frontend_ops
));
1192 state
->fe
.ops
.release
= NULL
;
1193 state
->fe
.demodulator_priv
= state
;
1194 i2c_set_clientdata(client
, state
);
1197 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, state
->config
.chip_mode
);
1201 ret
= si2165_readreg8(state
, REG_CHIP_MODE
, &val
);
1204 if (val
!= state
->config
.chip_mode
)
1207 ret
= si2165_readreg8(state
, REG_CHIP_REVCODE
, &state
->chip_revcode
);
1211 ret
= si2165_readreg8(state
, REV_CHIP_TYPE
, &state
->chip_type
);
1216 ret
= si2165_writereg8(state
, REG_CHIP_MODE
, SI2165_MODE_OFF
);
1220 if (state
->chip_revcode
< 26)
1221 rev_char
= 'A' + state
->chip_revcode
;
1225 switch (state
->chip_type
) {
1227 chip_name
= "Si2161";
1228 state
->has_dvbt
= true;
1231 chip_name
= "Si2165";
1232 state
->has_dvbt
= true;
1233 state
->has_dvbc
= true;
1236 dev_err(&state
->client
->dev
, "Unsupported Silicon Labs chip (type %d, rev %d)\n",
1237 state
->chip_type
, state
->chip_revcode
);
1241 dev_info(&state
->client
->dev
,
1242 "Detected Silicon Labs %s-%c (type %d, rev %d)\n",
1243 chip_name
, rev_char
, state
->chip_type
,
1244 state
->chip_revcode
);
1246 strlcat(state
->fe
.ops
.info
.name
, chip_name
,
1247 sizeof(state
->fe
.ops
.info
.name
));
1250 if (state
->has_dvbt
) {
1251 state
->fe
.ops
.delsys
[n
++] = SYS_DVBT
;
1252 strlcat(state
->fe
.ops
.info
.name
, " DVB-T",
1253 sizeof(state
->fe
.ops
.info
.name
));
1255 if (state
->has_dvbc
) {
1256 state
->fe
.ops
.delsys
[n
++] = SYS_DVBC_ANNEX_A
;
1257 strlcat(state
->fe
.ops
.info
.name
, " DVB-C",
1258 sizeof(state
->fe
.ops
.info
.name
));
1261 /* return fe pointer */
1262 *pdata
->fe
= &state
->fe
;
1270 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
1274 static void si2165_remove(struct i2c_client
*client
)
1276 struct si2165_state
*state
= i2c_get_clientdata(client
);
1278 dev_dbg(&client
->dev
, "\n");
1283 static const struct i2c_device_id si2165_id_table
[] = {
1287 MODULE_DEVICE_TABLE(i2c
, si2165_id_table
);
1289 static struct i2c_driver si2165_driver
= {
1293 .probe
= si2165_probe
,
1294 .remove
= si2165_remove
,
1295 .id_table
= si2165_id_table
,
1298 module_i2c_driver(si2165_driver
);
1300 MODULE_DESCRIPTION("Silicon Labs Si2165 DVB-C/-T Demodulator driver");
1301 MODULE_AUTHOR("Matthias Schwarzott <zzam@gentoo.org>");
1302 MODULE_LICENSE("GPL");
1303 MODULE_FIRMWARE(SI2165_FIRMWARE_REV_D
);