4 * Driver for ST STV6110 satellite tuner IC.
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
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
18 * GNU General Public License for more details.
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/dvb/frontend.h>
25 #include <linux/types.h>
29 /* Max transfer size done by I2C transfer functions */
30 #define MAX_XFER_SIZE 64
36 struct i2c_adapter *i2c;
44 #define dprintk(args...) \
47 printk(KERN_DEBUG args); \
50 static s32 abssub(s32 a, s32 b)
58 static void stv6110_release(struct dvb_frontend *fe)
60 kfree(fe->tuner_priv);
61 fe->tuner_priv = NULL;
64 static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[],
67 struct stv6110_priv *priv = fe->tuner_priv;
69 u8 cmdbuf[MAX_XFER_SIZE];
70 struct i2c_msg msg = {
71 .addr = priv->i2c_address,
77 dprintk("%s\n", __func__);
79 if (1 + len > sizeof(cmdbuf)) {
81 "%s: i2c wr: len=%d is too big!\n",
89 memcpy(&cmdbuf[1], buf, len);
92 if (fe->ops.i2c_gate_ctrl)
93 fe->ops.i2c_gate_ctrl(fe, 1);
95 rc = i2c_transfer(priv->i2c, &msg, 1);
97 dprintk("%s: i2c error\n", __func__);
99 if (fe->ops.i2c_gate_ctrl)
100 fe->ops.i2c_gate_ctrl(fe, 0);
105 static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[],
108 struct stv6110_priv *priv = fe->tuner_priv;
110 u8 reg[] = { start };
111 struct i2c_msg msg[] = {
113 .addr = priv->i2c_address,
118 .addr = priv->i2c_address,
125 if (fe->ops.i2c_gate_ctrl)
126 fe->ops.i2c_gate_ctrl(fe, 1);
128 rc = i2c_transfer(priv->i2c, msg, 2);
130 dprintk("%s: i2c error\n", __func__);
132 if (fe->ops.i2c_gate_ctrl)
133 fe->ops.i2c_gate_ctrl(fe, 0);
135 memcpy(&priv->regs[start], regs, len);
140 static int stv6110_read_reg(struct dvb_frontend *fe, int start)
143 stv6110_read_regs(fe, buf, start, 1);
148 static int stv6110_sleep(struct dvb_frontend *fe)
151 stv6110_write_regs(fe, reg, 0, 1);
156 static u32 carrier_width(u32 symbol_rate, enum fe_rolloff rolloff)
172 return symbol_rate + ((symbol_rate * rlf) / 100);
175 static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
177 struct stv6110_priv *priv = fe->tuner_priv;
181 if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/
183 else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */
185 else /*if 5 < BW/2 < 36*/
186 r8 = (bandwidth / 2) / 1000000 - 5;
188 /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */
189 /* ctrl3, CF = r8 Set the LPF value */
190 priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f);
191 priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f);
192 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
193 /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/
194 priv->regs[RSTV6110_STAT1] |= 0x02;
195 stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1);
198 /* Wait for CALRCSTRT == 0 */
199 while ((i < 10) && (ret != 0)) {
200 ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02);
201 mdelay(1); /* wait for LPF auto calibration */
205 /* RCCLKOFF = 1 calibration done, desactivate the calibration Clock */
206 priv->regs[RSTV6110_CTRL3] |= (1 << 6);
207 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
211 static int stv6110_init(struct dvb_frontend *fe)
213 struct stv6110_priv *priv = fe->tuner_priv;
214 u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
216 memcpy(priv->regs, buf0, 8);
217 /* K = (Reference / 1000000) - 16 */
218 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
219 priv->regs[RSTV6110_CTRL1] |=
220 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
222 /* divisor value for the output clock */
223 priv->regs[RSTV6110_CTRL2] &= ~0xc0;
224 priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6);
226 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8);
228 stv6110_set_bandwidth(fe, 72000000);
233 static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency)
235 struct stv6110_priv *priv = fe->tuner_priv;
236 u32 nbsteps, divider, psd2, freq;
237 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
239 stv6110_read_regs(fe, regs, 0, 8);
241 divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8;
242 divider += priv->regs[RSTV6110_TUNING1];
245 nbsteps = (priv->regs[RSTV6110_TUNING2] >> 6) & 3;
247 psd2 = (priv->regs[RSTV6110_TUNING2] >> 4) & 1;
249 freq = divider * (priv->mclk / 1000);
250 freq /= (1 << (nbsteps + psd2));
258 static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency)
260 struct stv6110_priv *priv = fe->tuner_priv;
261 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
263 u32 divider, ref, p, presc, i, result_freq, vco_freq;
264 s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val;
267 dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__,
268 frequency, priv->mclk);
270 /* K = (Reference / 1000000) - 16 */
271 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
272 priv->regs[RSTV6110_CTRL1] |=
273 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
276 if (fe->ops.set_property && fe->ops.get_property) {
277 srate = c->symbol_rate;
278 dprintk("%s: Get Frontend parameters: srate=%d\n",
283 priv->regs[RSTV6110_CTRL2] &= ~0x0f;
284 priv->regs[RSTV6110_CTRL2] |= (priv->gain & 0x0f);
286 if (frequency <= 1023000) {
289 } else if (frequency <= 1300000) {
292 } else if (frequency <= 2046000) {
300 priv->regs[RSTV6110_TUNING2] &= ~(1 << 4);
301 priv->regs[RSTV6110_TUNING2] |= (p << 4);
303 /* PRESC32ON = presc */
304 priv->regs[RSTV6110_TUNING2] &= ~(1 << 5);
305 priv->regs[RSTV6110_TUNING2] |= (presc << 5);
307 p_val = (int)(1 << (p + 1)) * 10;/* P = 2 or P = 4 */
308 for (r_div = 0; r_div <= 3; r_div++) {
309 p_calc = (priv->mclk / 100000);
310 p_calc /= (1 << (r_div + 1));
311 if ((abssub(p_calc, p_val)) < (abssub(p_calc_opt, p_val)))
314 p_calc_opt = (priv->mclk / 100000);
315 p_calc_opt /= (1 << (r_div_opt + 1));
318 ref = priv->mclk / ((1 << (r_div_opt + 1)) * (1 << (p + 1)));
319 divider = (((frequency * 1000) + (ref >> 1)) / ref);
321 /* RDIV = r_div_opt */
322 priv->regs[RSTV6110_TUNING2] &= ~(3 << 6);
323 priv->regs[RSTV6110_TUNING2] |= (((r_div_opt) & 3) << 6);
325 /* NDIV_MSB = MSB(divider) */
326 priv->regs[RSTV6110_TUNING2] &= ~0x0f;
327 priv->regs[RSTV6110_TUNING2] |= (((divider) >> 8) & 0x0f);
329 /* NDIV_LSB, LSB(divider) */
330 priv->regs[RSTV6110_TUNING1] = (divider & 0xff);
332 /* CALVCOSTRT = 1 VCO Auto Calibration */
333 priv->regs[RSTV6110_STAT1] |= 0x04;
334 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1],
338 /* Wait for CALVCOSTRT == 0 */
339 while ((i < 10) && (ret != 0)) {
340 ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x04);
341 msleep(1); /* wait for VCO auto calibration */
345 ret = stv6110_read_reg(fe, RSTV6110_STAT1);
346 stv6110_get_frequency(fe, &result_freq);
348 vco_freq = divider * ((priv->mclk / 1000) / ((1 << (r_div_opt + 1))));
349 dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__,
350 ret, result_freq, vco_freq);
355 static int stv6110_set_params(struct dvb_frontend *fe)
357 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
358 u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff);
360 stv6110_set_frequency(fe, c->frequency);
361 stv6110_set_bandwidth(fe, bandwidth);
366 static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
368 struct stv6110_priv *priv = fe->tuner_priv;
370 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
371 stv6110_read_regs(fe, regs, 0, 8);
374 r8 = priv->regs[RSTV6110_CTRL3] & 0x1f;
375 *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */
380 static const struct dvb_tuner_ops stv6110_tuner_ops = {
382 .name = "ST STV6110",
383 .frequency_min = 950000,
384 .frequency_max = 2150000,
385 .frequency_step = 1000,
387 .init = stv6110_init,
388 .release = stv6110_release,
389 .sleep = stv6110_sleep,
390 .set_params = stv6110_set_params,
391 .get_frequency = stv6110_get_frequency,
392 .set_frequency = stv6110_set_frequency,
393 .get_bandwidth = stv6110_get_bandwidth,
394 .set_bandwidth = stv6110_set_bandwidth,
398 struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe,
399 const struct stv6110_config *config,
400 struct i2c_adapter *i2c)
402 struct stv6110_priv *priv = NULL;
403 u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
405 struct i2c_msg msg[] = {
407 .addr = config->i2c_address,
415 /* divisor value for the output clock */
417 reg0[2] |= (config->clk_div << 6);
419 if (fe->ops.i2c_gate_ctrl)
420 fe->ops.i2c_gate_ctrl(fe, 1);
422 ret = i2c_transfer(i2c, msg, 1);
424 if (fe->ops.i2c_gate_ctrl)
425 fe->ops.i2c_gate_ctrl(fe, 0);
430 priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL);
434 priv->i2c_address = config->i2c_address;
436 priv->mclk = config->mclk;
437 priv->clk_div = config->clk_div;
438 priv->gain = config->gain;
440 memcpy(&priv->regs, ®0[1], 8);
442 memcpy(&fe->ops.tuner_ops, &stv6110_tuner_ops,
443 sizeof(struct dvb_tuner_ops));
444 fe->tuner_priv = priv;
445 printk(KERN_INFO "STV6110 attached on addr=%x!\n", priv->i2c_address);
449 EXPORT_SYMBOL_GPL(stv6110_attach);
451 module_param(debug, int, 0644);
452 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
454 MODULE_DESCRIPTION("ST STV6110 driver");
455 MODULE_AUTHOR("Igor M. Liplianin");
456 MODULE_LICENSE("GPL");