Full support for Ginger Console
[linux-ginger.git] / drivers / media / dvb / frontends / stv6110.c
blobdcf1b21ea9741971e15ad314dd59ecfcf7743cf8
1 /*
2 * stv6110.c
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.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/dvb/frontend.h>
28 #include <linux/types.h>
30 #include "stv6110.h"
32 static int debug;
34 struct stv6110_priv {
35 int i2c_address;
36 struct i2c_adapter *i2c;
38 u32 mclk;
39 u8 clk_div;
40 u8 regs[8];
43 #define dprintk(args...) \
44 do { \
45 if (debug) \
46 printk(KERN_DEBUG args); \
47 } while (0)
49 static s32 abssub(s32 a, s32 b)
51 if (a > b)
52 return a - b;
53 else
54 return b - a;
57 static int stv6110_release(struct dvb_frontend *fe)
59 kfree(fe->tuner_priv);
60 fe->tuner_priv = NULL;
61 return 0;
64 static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[],
65 int start, int len)
67 struct stv6110_priv *priv = fe->tuner_priv;
68 int rc;
69 u8 cmdbuf[len + 1];
70 struct i2c_msg msg = {
71 .addr = priv->i2c_address,
72 .flags = 0,
73 .buf = cmdbuf,
74 .len = len + 1
77 dprintk("%s\n", __func__);
79 if (start + len > 8)
80 return -EINVAL;
82 memcpy(&cmdbuf[1], buf, len);
83 cmdbuf[0] = start;
85 if (fe->ops.i2c_gate_ctrl)
86 fe->ops.i2c_gate_ctrl(fe, 1);
88 rc = i2c_transfer(priv->i2c, &msg, 1);
89 if (rc != 1)
90 dprintk("%s: i2c error\n", __func__);
92 if (fe->ops.i2c_gate_ctrl)
93 fe->ops.i2c_gate_ctrl(fe, 0);
95 return 0;
98 static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[],
99 int start, int len)
101 struct stv6110_priv *priv = fe->tuner_priv;
102 int rc;
103 u8 reg[] = { start };
104 struct i2c_msg msg[] = {
106 .addr = priv->i2c_address,
107 .flags = 0,
108 .buf = reg,
109 .len = 1,
110 }, {
111 .addr = priv->i2c_address,
112 .flags = I2C_M_RD,
113 .buf = regs,
114 .len = len,
118 if (fe->ops.i2c_gate_ctrl)
119 fe->ops.i2c_gate_ctrl(fe, 1);
121 rc = i2c_transfer(priv->i2c, msg, 2);
122 if (rc != 2)
123 dprintk("%s: i2c error\n", __func__);
125 if (fe->ops.i2c_gate_ctrl)
126 fe->ops.i2c_gate_ctrl(fe, 0);
128 memcpy(&priv->regs[start], regs, len);
130 return 0;
133 static int stv6110_read_reg(struct dvb_frontend *fe, int start)
135 u8 buf[] = { 0 };
136 stv6110_read_regs(fe, buf, start, 1);
138 return buf[0];
141 static int stv6110_sleep(struct dvb_frontend *fe)
143 u8 reg[] = { 0 };
144 stv6110_write_regs(fe, reg, 0, 1);
146 return 0;
149 static u32 carrier_width(u32 symbol_rate, fe_rolloff_t rolloff)
151 u32 rlf;
153 switch (rolloff) {
154 case ROLLOFF_20:
155 rlf = 20;
156 break;
157 case ROLLOFF_25:
158 rlf = 25;
159 break;
160 default:
161 rlf = 35;
162 break;
165 return symbol_rate + ((symbol_rate * rlf) / 100);
168 static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
170 struct stv6110_priv *priv = fe->tuner_priv;
171 u8 r8, ret = 0x04;
172 int i;
174 if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/
175 r8 = 31;
176 else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */
177 r8 = 0;
178 else /*if 5 < BW/2 < 36*/
179 r8 = (bandwidth / 2) / 1000000 - 5;
181 /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */
182 /* ctrl3, CF = r8 Set the LPF value */
183 priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f);
184 priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f);
185 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
186 /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/
187 priv->regs[RSTV6110_STAT1] |= 0x02;
188 stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1);
190 i = 0;
191 /* Wait for CALRCSTRT == 0 */
192 while ((i < 10) && (ret != 0)) {
193 ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02);
194 mdelay(1); /* wait for LPF auto calibration */
195 i++;
198 /* RCCLKOFF = 1 calibration done, desactivate the calibration Clock */
199 priv->regs[RSTV6110_CTRL3] |= (1 << 6);
200 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
201 return 0;
204 static int stv6110_init(struct dvb_frontend *fe)
206 struct stv6110_priv *priv = fe->tuner_priv;
207 u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
209 memcpy(priv->regs, buf0, 8);
210 /* K = (Reference / 1000000) - 16 */
211 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
212 priv->regs[RSTV6110_CTRL1] |=
213 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
215 /* divisor value for the output clock */
216 priv->regs[RSTV6110_CTRL2] &= ~0xc0;
217 priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6);
219 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8);
220 msleep(1);
221 stv6110_set_bandwidth(fe, 72000000);
223 return 0;
226 static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency)
228 struct stv6110_priv *priv = fe->tuner_priv;
229 u32 nbsteps, divider, psd2, freq;
230 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
232 stv6110_read_regs(fe, regs, 0, 8);
233 /*N*/
234 divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8;
235 divider += priv->regs[RSTV6110_TUNING1];
237 /*R*/
238 nbsteps = (priv->regs[RSTV6110_TUNING2] >> 6) & 3;
239 /*p*/
240 psd2 = (priv->regs[RSTV6110_TUNING2] >> 4) & 1;
242 freq = divider * (priv->mclk / 1000);
243 freq /= (1 << (nbsteps + psd2));
244 freq /= 4;
246 *frequency = freq;
248 return 0;
251 static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency)
253 struct stv6110_priv *priv = fe->tuner_priv;
254 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
255 u8 ret = 0x04;
256 u32 divider, ref, p, presc, i, result_freq, vco_freq;
257 s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val;
258 s32 srate; u8 gain;
260 dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__,
261 frequency, priv->mclk);
263 /* K = (Reference / 1000000) - 16 */
264 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
265 priv->regs[RSTV6110_CTRL1] |=
266 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
268 /* BB_GAIN = db/2 */
269 if (fe->ops.set_property && fe->ops.get_property) {
270 srate = c->symbol_rate;
271 dprintk("%s: Get Frontend parameters: srate=%d\n",
272 __func__, srate);
273 } else
274 srate = 15000000;
276 if (srate >= 15000000)
277 gain = 3; /* +6 dB */
278 else if (srate >= 5000000)
279 gain = 3; /* +6 dB */
280 else
281 gain = 3; /* +6 dB */
283 priv->regs[RSTV6110_CTRL2] &= ~0x0f;
284 priv->regs[RSTV6110_CTRL2] |= (gain & 0x0f);
286 if (frequency <= 1023000) {
287 p = 1;
288 presc = 0;
289 } else if (frequency <= 1300000) {
290 p = 1;
291 presc = 1;
292 } else if (frequency <= 2046000) {
293 p = 0;
294 presc = 0;
295 } else {
296 p = 0;
297 presc = 1;
299 /* DIV4SEL = p*/
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)))
312 r_div_opt = r_div;
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],
335 RSTV6110_CTRL1, 8);
337 i = 0;
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 */
342 i++;
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);
352 return 0;
355 static int stv6110_set_params(struct dvb_frontend *fe,
356 struct dvb_frontend_parameters *params)
358 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
359 u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff);
361 stv6110_set_frequency(fe, c->frequency);
362 stv6110_set_bandwidth(fe, bandwidth);
364 return 0;
367 static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
369 struct stv6110_priv *priv = fe->tuner_priv;
370 u8 r8 = 0;
371 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
372 stv6110_read_regs(fe, regs, 0, 8);
374 /* CF */
375 r8 = priv->regs[RSTV6110_CTRL3] & 0x1f;
376 *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */
378 return 0;
381 static struct dvb_tuner_ops stv6110_tuner_ops = {
382 .info = {
383 .name = "ST STV6110",
384 .frequency_min = 950000,
385 .frequency_max = 2150000,
386 .frequency_step = 1000,
388 .init = stv6110_init,
389 .release = stv6110_release,
390 .sleep = stv6110_sleep,
391 .set_params = stv6110_set_params,
392 .get_frequency = stv6110_get_frequency,
393 .set_frequency = stv6110_set_frequency,
394 .get_bandwidth = stv6110_get_bandwidth,
395 .set_bandwidth = stv6110_set_bandwidth,
399 struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe,
400 const struct stv6110_config *config,
401 struct i2c_adapter *i2c)
403 struct stv6110_priv *priv = NULL;
404 u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
406 struct i2c_msg msg[] = {
408 .addr = config->i2c_address,
409 .flags = 0,
410 .buf = reg0,
411 .len = 9
414 int ret;
416 /* divisor value for the output clock */
417 reg0[2] &= ~0xc0;
418 reg0[2] |= (config->clk_div << 6);
420 if (fe->ops.i2c_gate_ctrl)
421 fe->ops.i2c_gate_ctrl(fe, 1);
423 ret = i2c_transfer(i2c, msg, 1);
425 if (fe->ops.i2c_gate_ctrl)
426 fe->ops.i2c_gate_ctrl(fe, 0);
428 if (ret != 1)
429 return NULL;
431 priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL);
432 if (priv == NULL)
433 return NULL;
435 priv->i2c_address = config->i2c_address;
436 priv->i2c = i2c;
437 priv->mclk = config->mclk;
438 priv->clk_div = config->clk_div;
440 memcpy(&priv->regs, &reg0[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);
447 return fe;
449 EXPORT_SYMBOL(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");