1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Driver for ST STV6110 satellite tuner IC.
7 * Copyright (C) 2009 NetUP Inc.
8 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/dvb/frontend.h>
15 #include <linux/types.h>
19 /* Max transfer size done by I2C transfer functions */
20 #define MAX_XFER_SIZE 64
26 struct i2c_adapter
*i2c
;
34 #define dprintk(args...) \
37 printk(KERN_DEBUG args); \
40 static s32
abssub(s32 a
, s32 b
)
48 static void stv6110_release(struct dvb_frontend
*fe
)
50 kfree(fe
->tuner_priv
);
51 fe
->tuner_priv
= NULL
;
54 static int stv6110_write_regs(struct dvb_frontend
*fe
, u8 buf
[],
57 struct stv6110_priv
*priv
= fe
->tuner_priv
;
59 u8 cmdbuf
[MAX_XFER_SIZE
];
60 struct i2c_msg msg
= {
61 .addr
= priv
->i2c_address
,
67 dprintk("%s\n", __func__
);
69 if (1 + len
> sizeof(cmdbuf
)) {
71 "%s: i2c wr: len=%d is too big!\n",
79 memcpy(&cmdbuf
[1], buf
, len
);
82 if (fe
->ops
.i2c_gate_ctrl
)
83 fe
->ops
.i2c_gate_ctrl(fe
, 1);
85 rc
= i2c_transfer(priv
->i2c
, &msg
, 1);
87 dprintk("%s: i2c error\n", __func__
);
89 if (fe
->ops
.i2c_gate_ctrl
)
90 fe
->ops
.i2c_gate_ctrl(fe
, 0);
95 static int stv6110_read_regs(struct dvb_frontend
*fe
, u8 regs
[],
98 struct stv6110_priv
*priv
= fe
->tuner_priv
;
100 u8 reg
[] = { start
};
101 struct i2c_msg msg
[] = {
103 .addr
= priv
->i2c_address
,
108 .addr
= priv
->i2c_address
,
115 if (fe
->ops
.i2c_gate_ctrl
)
116 fe
->ops
.i2c_gate_ctrl(fe
, 1);
118 rc
= i2c_transfer(priv
->i2c
, msg
, 2);
120 dprintk("%s: i2c error\n", __func__
);
122 if (fe
->ops
.i2c_gate_ctrl
)
123 fe
->ops
.i2c_gate_ctrl(fe
, 0);
125 memcpy(&priv
->regs
[start
], regs
, len
);
130 static int stv6110_read_reg(struct dvb_frontend
*fe
, int start
)
133 stv6110_read_regs(fe
, buf
, start
, 1);
138 static int stv6110_sleep(struct dvb_frontend
*fe
)
141 stv6110_write_regs(fe
, reg
, 0, 1);
146 static u32
carrier_width(u32 symbol_rate
, enum fe_rolloff rolloff
)
162 return symbol_rate
+ ((symbol_rate
* rlf
) / 100);
165 static int stv6110_set_bandwidth(struct dvb_frontend
*fe
, u32 bandwidth
)
167 struct stv6110_priv
*priv
= fe
->tuner_priv
;
171 if ((bandwidth
/ 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/
173 else if ((bandwidth
/ 2) < 5000000) /* BW/2 min=5Mhz for F=0 */
175 else /*if 5 < BW/2 < 36*/
176 r8
= (bandwidth
/ 2) / 1000000 - 5;
178 /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */
179 /* ctrl3, CF = r8 Set the LPF value */
180 priv
->regs
[RSTV6110_CTRL3
] &= ~((1 << 6) | 0x1f);
181 priv
->regs
[RSTV6110_CTRL3
] |= (r8
& 0x1f);
182 stv6110_write_regs(fe
, &priv
->regs
[RSTV6110_CTRL3
], RSTV6110_CTRL3
, 1);
183 /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/
184 priv
->regs
[RSTV6110_STAT1
] |= 0x02;
185 stv6110_write_regs(fe
, &priv
->regs
[RSTV6110_STAT1
], RSTV6110_STAT1
, 1);
188 /* Wait for CALRCSTRT == 0 */
189 while ((i
< 10) && (ret
!= 0)) {
190 ret
= ((stv6110_read_reg(fe
, RSTV6110_STAT1
)) & 0x02);
191 mdelay(1); /* wait for LPF auto calibration */
195 /* RCCLKOFF = 1 calibration done, deactivate the calibration Clock */
196 priv
->regs
[RSTV6110_CTRL3
] |= (1 << 6);
197 stv6110_write_regs(fe
, &priv
->regs
[RSTV6110_CTRL3
], RSTV6110_CTRL3
, 1);
201 static int stv6110_init(struct dvb_frontend
*fe
)
203 struct stv6110_priv
*priv
= fe
->tuner_priv
;
204 u8 buf0
[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
206 memcpy(priv
->regs
, buf0
, 8);
207 /* K = (Reference / 1000000) - 16 */
208 priv
->regs
[RSTV6110_CTRL1
] &= ~(0x1f << 3);
209 priv
->regs
[RSTV6110_CTRL1
] |=
210 ((((priv
->mclk
/ 1000000) - 16) & 0x1f) << 3);
212 /* divisor value for the output clock */
213 priv
->regs
[RSTV6110_CTRL2
] &= ~0xc0;
214 priv
->regs
[RSTV6110_CTRL2
] |= (priv
->clk_div
<< 6);
216 stv6110_write_regs(fe
, &priv
->regs
[RSTV6110_CTRL1
], RSTV6110_CTRL1
, 8);
218 stv6110_set_bandwidth(fe
, 72000000);
223 static int stv6110_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
225 struct stv6110_priv
*priv
= fe
->tuner_priv
;
226 u32 nbsteps
, divider
, psd2
, freq
;
227 u8 regs
[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
229 stv6110_read_regs(fe
, regs
, 0, 8);
231 divider
= (priv
->regs
[RSTV6110_TUNING2
] & 0x0f) << 8;
232 divider
+= priv
->regs
[RSTV6110_TUNING1
];
235 nbsteps
= (priv
->regs
[RSTV6110_TUNING2
] >> 6) & 3;
237 psd2
= (priv
->regs
[RSTV6110_TUNING2
] >> 4) & 1;
239 freq
= divider
* (priv
->mclk
/ 1000);
240 freq
/= (1 << (nbsteps
+ psd2
));
248 static int stv6110_set_frequency(struct dvb_frontend
*fe
, u32 frequency
)
250 struct stv6110_priv
*priv
= fe
->tuner_priv
;
252 u32 divider
, ref
, p
, presc
, i
, result_freq
, vco_freq
;
253 s32 p_calc
, p_calc_opt
= 1000, r_div
, r_div_opt
= 0, p_val
;
255 dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__
,
256 frequency
, priv
->mclk
);
258 /* K = (Reference / 1000000) - 16 */
259 priv
->regs
[RSTV6110_CTRL1
] &= ~(0x1f << 3);
260 priv
->regs
[RSTV6110_CTRL1
] |=
261 ((((priv
->mclk
/ 1000000) - 16) & 0x1f) << 3);
264 priv
->regs
[RSTV6110_CTRL2
] &= ~0x0f;
265 priv
->regs
[RSTV6110_CTRL2
] |= (priv
->gain
& 0x0f);
267 if (frequency
<= 1023000) {
270 } else if (frequency
<= 1300000) {
273 } else if (frequency
<= 2046000) {
281 priv
->regs
[RSTV6110_TUNING2
] &= ~(1 << 4);
282 priv
->regs
[RSTV6110_TUNING2
] |= (p
<< 4);
284 /* PRESC32ON = presc */
285 priv
->regs
[RSTV6110_TUNING2
] &= ~(1 << 5);
286 priv
->regs
[RSTV6110_TUNING2
] |= (presc
<< 5);
288 p_val
= (int)(1 << (p
+ 1)) * 10;/* P = 2 or P = 4 */
289 for (r_div
= 0; r_div
<= 3; r_div
++) {
290 p_calc
= (priv
->mclk
/ 100000);
291 p_calc
/= (1 << (r_div
+ 1));
292 if ((abssub(p_calc
, p_val
)) < (abssub(p_calc_opt
, p_val
)))
295 p_calc_opt
= (priv
->mclk
/ 100000);
296 p_calc_opt
/= (1 << (r_div_opt
+ 1));
299 ref
= priv
->mclk
/ ((1 << (r_div_opt
+ 1)) * (1 << (p
+ 1)));
300 divider
= (((frequency
* 1000) + (ref
>> 1)) / ref
);
302 /* RDIV = r_div_opt */
303 priv
->regs
[RSTV6110_TUNING2
] &= ~(3 << 6);
304 priv
->regs
[RSTV6110_TUNING2
] |= (((r_div_opt
) & 3) << 6);
306 /* NDIV_MSB = MSB(divider) */
307 priv
->regs
[RSTV6110_TUNING2
] &= ~0x0f;
308 priv
->regs
[RSTV6110_TUNING2
] |= (((divider
) >> 8) & 0x0f);
310 /* NDIV_LSB, LSB(divider) */
311 priv
->regs
[RSTV6110_TUNING1
] = (divider
& 0xff);
313 /* CALVCOSTRT = 1 VCO Auto Calibration */
314 priv
->regs
[RSTV6110_STAT1
] |= 0x04;
315 stv6110_write_regs(fe
, &priv
->regs
[RSTV6110_CTRL1
],
319 /* Wait for CALVCOSTRT == 0 */
320 while ((i
< 10) && (ret
!= 0)) {
321 ret
= ((stv6110_read_reg(fe
, RSTV6110_STAT1
)) & 0x04);
322 msleep(1); /* wait for VCO auto calibration */
326 ret
= stv6110_read_reg(fe
, RSTV6110_STAT1
);
327 stv6110_get_frequency(fe
, &result_freq
);
329 vco_freq
= divider
* ((priv
->mclk
/ 1000) / ((1 << (r_div_opt
+ 1))));
330 dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__
,
331 ret
, result_freq
, vco_freq
);
336 static int stv6110_set_params(struct dvb_frontend
*fe
)
338 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
339 u32 bandwidth
= carrier_width(c
->symbol_rate
, c
->rolloff
);
341 stv6110_set_frequency(fe
, c
->frequency
);
342 stv6110_set_bandwidth(fe
, bandwidth
);
347 static int stv6110_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
349 struct stv6110_priv
*priv
= fe
->tuner_priv
;
351 u8 regs
[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
352 stv6110_read_regs(fe
, regs
, 0, 8);
355 r8
= priv
->regs
[RSTV6110_CTRL3
] & 0x1f;
356 *bandwidth
= (r8
+ 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */
361 static const struct dvb_tuner_ops stv6110_tuner_ops
= {
363 .name
= "ST STV6110",
364 .frequency_min_hz
= 950 * MHz
,
365 .frequency_max_hz
= 2150 * MHz
,
366 .frequency_step_hz
= 1 * MHz
,
368 .init
= stv6110_init
,
369 .release
= stv6110_release
,
370 .sleep
= stv6110_sleep
,
371 .set_params
= stv6110_set_params
,
372 .get_frequency
= stv6110_get_frequency
,
373 .set_frequency
= stv6110_set_frequency
,
374 .get_bandwidth
= stv6110_get_bandwidth
,
375 .set_bandwidth
= stv6110_set_bandwidth
,
379 struct dvb_frontend
*stv6110_attach(struct dvb_frontend
*fe
,
380 const struct stv6110_config
*config
,
381 struct i2c_adapter
*i2c
)
383 struct stv6110_priv
*priv
= NULL
;
384 u8 reg0
[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
386 struct i2c_msg msg
[] = {
388 .addr
= config
->i2c_address
,
396 /* divisor value for the output clock */
398 reg0
[2] |= (config
->clk_div
<< 6);
400 if (fe
->ops
.i2c_gate_ctrl
)
401 fe
->ops
.i2c_gate_ctrl(fe
, 1);
403 ret
= i2c_transfer(i2c
, msg
, 1);
405 if (fe
->ops
.i2c_gate_ctrl
)
406 fe
->ops
.i2c_gate_ctrl(fe
, 0);
411 priv
= kzalloc(sizeof(struct stv6110_priv
), GFP_KERNEL
);
415 priv
->i2c_address
= config
->i2c_address
;
417 priv
->mclk
= config
->mclk
;
418 priv
->clk_div
= config
->clk_div
;
419 priv
->gain
= config
->gain
;
421 memcpy(&priv
->regs
, ®0
[1], 8);
423 memcpy(&fe
->ops
.tuner_ops
, &stv6110_tuner_ops
,
424 sizeof(struct dvb_tuner_ops
));
425 fe
->tuner_priv
= priv
;
426 printk(KERN_INFO
"STV6110 attached on addr=%x!\n", priv
->i2c_address
);
430 EXPORT_SYMBOL(stv6110_attach
);
432 module_param(debug
, int, 0644);
433 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
435 MODULE_DESCRIPTION("ST STV6110 driver");
436 MODULE_AUTHOR("Igor M. Liplianin");
437 MODULE_LICENSE("GPL");