1 /*
2 * stv6110.c
3 *
4 * Driver for ST STV6110 satellite tuner IC.
5 *
6 * Copyright (C) 2009 NetUP Inc.
7 * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
8 *
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.
13 *
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
17 *
18 * GNU General Public License for more details.
19 *
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.
23 */
24
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/dvb/frontend.h>
28
29 #include <linux/types.h>
30
31 #include "stv6110.h"
32
33 static int debug;
34
35 struct stv6110_priv {
36 int i2c_address;
37 struct i2c_adapter *i2c;
38
39 u32 mclk;
40 u8 clk_div;
41 u8 gain;
42 u8 regs[8];
43 };
44
45 #define dprintk(args...) \
46 do { \
47 if (debug) \
48 printk(KERN_DEBUG args); \
49 } while (0)
50
abssub(s32 a,s32 b)51 static s32 abssub(s32 a, s32 b)
52 {
53 if (a > b)
54 return a - b;
55 else
56 return b - a;
57 };
58
stv6110_release(struct dvb_frontend * fe)59 static int stv6110_release(struct dvb_frontend *fe)
60 {
61 kfree(fe->tuner_priv);
62 fe->tuner_priv = NULL;
63 return 0;
64 }
65
stv6110_write_regs(struct dvb_frontend * fe,u8 buf[],int start,int len)66 static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[],
67 int start, int len)
68 {
69 struct stv6110_priv *priv = fe->tuner_priv;
70 int rc;
71 u8 cmdbuf[len + 1];
72 struct i2c_msg msg = {
73 .addr = priv->i2c_address,
74 .flags = 0,
75 .buf = cmdbuf,
76 .len = len + 1
77 };
78
79 dprintk("%s\n", __func__);
80
81 if (start + len > 8)
82 return -EINVAL;
83
84 memcpy(&cmdbuf[1], buf, len);
85 cmdbuf[0] = start;
86
87 if (fe->ops.i2c_gate_ctrl)
88 fe->ops.i2c_gate_ctrl(fe, 1);
89
90 rc = i2c_transfer(priv->i2c, &msg, 1);
91 if (rc != 1)
92 dprintk("%s: i2c error\n", __func__);
93
94 if (fe->ops.i2c_gate_ctrl)
95 fe->ops.i2c_gate_ctrl(fe, 0);
96
97 return 0;
98 }
99
stv6110_read_regs(struct dvb_frontend * fe,u8 regs[],int start,int len)100 static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[],
101 int start, int len)
102 {
103 struct stv6110_priv *priv = fe->tuner_priv;
104 int rc;
105 u8 reg[] = { start };
106 struct i2c_msg msg[] = {
107 {
108 .addr = priv->i2c_address,
109 .flags = 0,
110 .buf = reg,
111 .len = 1,
112 }, {
113 .addr = priv->i2c_address,
114 .flags = I2C_M_RD,
115 .buf = regs,
116 .len = len,
117 },
118 };
119
120 if (fe->ops.i2c_gate_ctrl)
121 fe->ops.i2c_gate_ctrl(fe, 1);
122
123 rc = i2c_transfer(priv->i2c, msg, 2);
124 if (rc != 2)
125 dprintk("%s: i2c error\n", __func__);
126
127 if (fe->ops.i2c_gate_ctrl)
128 fe->ops.i2c_gate_ctrl(fe, 0);
129
130 memcpy(&priv->regs[start], regs, len);
131
132 return 0;
133 }
134
stv6110_read_reg(struct dvb_frontend * fe,int start)135 static int stv6110_read_reg(struct dvb_frontend *fe, int start)
136 {
137 u8 buf[] = { 0 };
138 stv6110_read_regs(fe, buf, start, 1);
139
140 return buf[0];
141 }
142
stv6110_sleep(struct dvb_frontend * fe)143 static int stv6110_sleep(struct dvb_frontend *fe)
144 {
145 u8 reg[] = { 0 };
146 stv6110_write_regs(fe, reg, 0, 1);
147
148 return 0;
149 }
150
carrier_width(u32 symbol_rate,fe_rolloff_t rolloff)151 static u32 carrier_width(u32 symbol_rate, fe_rolloff_t rolloff)
152 {
153 u32 rlf;
154
155 switch (rolloff) {
156 case ROLLOFF_20:
157 rlf = 20;
158 break;
159 case ROLLOFF_25:
160 rlf = 25;
161 break;
162 default:
163 rlf = 35;
164 break;
165 }
166
167 return symbol_rate + ((symbol_rate * rlf) / 100);
168 }
169
stv6110_set_bandwidth(struct dvb_frontend * fe,u32 bandwidth)170 static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
171 {
172 struct stv6110_priv *priv = fe->tuner_priv;
173 u8 r8, ret = 0x04;
174 int i;
175
176 if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/
177 r8 = 31;
178 else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */
179 r8 = 0;
180 else /*if 5 < BW/2 < 36*/
181 r8 = (bandwidth / 2) / 1000000 - 5;
182
183 /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */
184 /* ctrl3, CF = r8 Set the LPF value */
185 priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f);
186 priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f);
187 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
188 /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/
189 priv->regs[RSTV6110_STAT1] |= 0x02;
190 stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1);
191
192 i = 0;
193 /* Wait for CALRCSTRT == 0 */
194 while ((i < 10) && (ret != 0)) {
195 ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02);
196 mdelay(1); /* wait for LPF auto calibration */
197 i++;
198 }
199
200 /* RCCLKOFF = 1 calibration done, desactivate the calibration Clock */
201 priv->regs[RSTV6110_CTRL3] |= (1 << 6);
202 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
203 return 0;
204 }
205
stv6110_init(struct dvb_frontend * fe)206 static int stv6110_init(struct dvb_frontend *fe)
207 {
208 struct stv6110_priv *priv = fe->tuner_priv;
209 u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
210
211 memcpy(priv->regs, buf0, 8);
212 /* K = (Reference / 1000000) - 16 */
213 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
214 priv->regs[RSTV6110_CTRL1] |=
215 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
216
217 /* divisor value for the output clock */
218 priv->regs[RSTV6110_CTRL2] &= ~0xc0;
219 priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6);
220
221 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8);
222 msleep(1);
223 stv6110_set_bandwidth(fe, 72000000);
224
225 return 0;
226 }
227
stv6110_get_frequency(struct dvb_frontend * fe,u32 * frequency)228 static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency)
229 {
230 struct stv6110_priv *priv = fe->tuner_priv;
231 u32 nbsteps, divider, psd2, freq;
232 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
233
234 stv6110_read_regs(fe, regs, 0, 8);
235 /*N*/
236 divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8;
237 divider += priv->regs[RSTV6110_TUNING1];
238
239 /*R*/
240 nbsteps = (priv->regs[RSTV6110_TUNING2] >> 6) & 3;
241 /*p*/
242 psd2 = (priv->regs[RSTV6110_TUNING2] >> 4) & 1;
243
244 freq = divider * (priv->mclk / 1000);
245 freq /= (1 << (nbsteps + psd2));
246 freq /= 4;
247
248 *frequency = freq;
249
250 return 0;
251 }
252
stv6110_set_frequency(struct dvb_frontend * fe,u32 frequency)253 static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency)
254 {
255 struct stv6110_priv *priv = fe->tuner_priv;
256 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
257 u8 ret = 0x04;
258 u32 divider, ref, p, presc, i, result_freq, vco_freq;
259 s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val;
260 s32 srate;
261
262 dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__,
263 frequency, priv->mclk);
264
265 /* K = (Reference / 1000000) - 16 */
266 priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
267 priv->regs[RSTV6110_CTRL1] |=
268 ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
269
270 /* BB_GAIN = db/2 */
271 if (fe->ops.set_property && fe->ops.get_property) {
272 srate = c->symbol_rate;
273 dprintk("%s: Get Frontend parameters: srate=%d\n",
274 __func__, srate);
275 } else
276 srate = 15000000;
277
278 priv->regs[RSTV6110_CTRL2] &= ~0x0f;
279 priv->regs[RSTV6110_CTRL2] |= (priv->gain & 0x0f);
280
281 if (frequency <= 1023000) {
282 p = 1;
283 presc = 0;
284 } else if (frequency <= 1300000) {
285 p = 1;
286 presc = 1;
287 } else if (frequency <= 2046000) {
288 p = 0;
289 presc = 0;
290 } else {
291 p = 0;
292 presc = 1;
293 }
294 /* DIV4SEL = p*/
295 priv->regs[RSTV6110_TUNING2] &= ~(1 << 4);
296 priv->regs[RSTV6110_TUNING2] |= (p << 4);
297
298 /* PRESC32ON = presc */
299 priv->regs[RSTV6110_TUNING2] &= ~(1 << 5);
300 priv->regs[RSTV6110_TUNING2] |= (presc << 5);
301
302 p_val = (int)(1 << (p + 1)) * 10;/* P = 2 or P = 4 */
303 for (r_div = 0; r_div <= 3; r_div++) {
304 p_calc = (priv->mclk / 100000);
305 p_calc /= (1 << (r_div + 1));
306 if ((abssub(p_calc, p_val)) < (abssub(p_calc_opt, p_val)))
307 r_div_opt = r_div;
308
309 p_calc_opt = (priv->mclk / 100000);
310 p_calc_opt /= (1 << (r_div_opt + 1));
311 }
312
313 ref = priv->mclk / ((1 << (r_div_opt + 1)) * (1 << (p + 1)));
314 divider = (((frequency * 1000) + (ref >> 1)) / ref);
315
316 /* RDIV = r_div_opt */
317 priv->regs[RSTV6110_TUNING2] &= ~(3 << 6);
318 priv->regs[RSTV6110_TUNING2] |= (((r_div_opt) & 3) << 6);
319
320 /* NDIV_MSB = MSB(divider) */
321 priv->regs[RSTV6110_TUNING2] &= ~0x0f;
322 priv->regs[RSTV6110_TUNING2] |= (((divider) >> 8) & 0x0f);
323
324 /* NDIV_LSB, LSB(divider) */
325 priv->regs[RSTV6110_TUNING1] = (divider & 0xff);
326
327 /* CALVCOSTRT = 1 VCO Auto Calibration */
328 priv->regs[RSTV6110_STAT1] |= 0x04;
329 stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1],
330 RSTV6110_CTRL1, 8);
331
332 i = 0;
333 /* Wait for CALVCOSTRT == 0 */
334 while ((i < 10) && (ret != 0)) {
335 ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x04);
336 msleep(1); /* wait for VCO auto calibration */
337 i++;
338 }
339
340 ret = stv6110_read_reg(fe, RSTV6110_STAT1);
341 stv6110_get_frequency(fe, &result_freq);
342
343 vco_freq = divider * ((priv->mclk / 1000) / ((1 << (r_div_opt + 1))));
344 dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__,
345 ret, result_freq, vco_freq);
346
347 return 0;
348 }
349
stv6110_set_params(struct dvb_frontend * fe,struct dvb_frontend_parameters * params)350 static int stv6110_set_params(struct dvb_frontend *fe,
351 struct dvb_frontend_parameters *params)
352 {
353 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
354 u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff);
355
356 stv6110_set_frequency(fe, c->frequency);
357 stv6110_set_bandwidth(fe, bandwidth);
358
359 return 0;
360 }
361
stv6110_get_bandwidth(struct dvb_frontend * fe,u32 * bandwidth)362 static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
363 {
364 struct stv6110_priv *priv = fe->tuner_priv;
365 u8 r8 = 0;
366 u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
367 stv6110_read_regs(fe, regs, 0, 8);
368
369 /* CF */
370 r8 = priv->regs[RSTV6110_CTRL3] & 0x1f;
371 *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */
372
373 return 0;
374 }
375
376 static struct dvb_tuner_ops stv6110_tuner_ops = {
377 .info = {
378 .name = "ST STV6110",
379 .frequency_min = 950000,
380 .frequency_max = 2150000,
381 .frequency_step = 1000,
382 },
383 .init = stv6110_init,
384 .release = stv6110_release,
385 .sleep = stv6110_sleep,
386 .set_params = stv6110_set_params,
387 .get_frequency = stv6110_get_frequency,
388 .set_frequency = stv6110_set_frequency,
389 .get_bandwidth = stv6110_get_bandwidth,
390 .set_bandwidth = stv6110_set_bandwidth,
391
392 };
393
stv6110_attach(struct dvb_frontend * fe,const struct stv6110_config * config,struct i2c_adapter * i2c)394 struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe,
395 const struct stv6110_config *config,
396 struct i2c_adapter *i2c)
397 {
398 struct stv6110_priv *priv = NULL;
399 u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
400
401 struct i2c_msg msg[] = {
402 {
403 .addr = config->i2c_address,
404 .flags = 0,
405 .buf = reg0,
406 .len = 9
407 }
408 };
409 int ret;
410
411 /* divisor value for the output clock */
412 reg0[2] &= ~0xc0;
413 reg0[2] |= (config->clk_div << 6);
414
415 if (fe->ops.i2c_gate_ctrl)
416 fe->ops.i2c_gate_ctrl(fe, 1);
417
418 ret = i2c_transfer(i2c, msg, 1);
419
420 if (fe->ops.i2c_gate_ctrl)
421 fe->ops.i2c_gate_ctrl(fe, 0);
422
423 if (ret != 1)
424 return NULL;
425
426 priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL);
427 if (priv == NULL)
428 return NULL;
429
430 priv->i2c_address = config->i2c_address;
431 priv->i2c = i2c;
432 priv->mclk = config->mclk;
433 priv->clk_div = config->clk_div;
434 priv->gain = config->gain;
435
436 memcpy(&priv->regs, ®0[1], 8);
437
438 memcpy(&fe->ops.tuner_ops, &stv6110_tuner_ops,
439 sizeof(struct dvb_tuner_ops));
440 fe->tuner_priv = priv;
441 printk(KERN_INFO "STV6110 attached on addr=%x!\n", priv->i2c_address);
442
443 return fe;
444 }
445 EXPORT_SYMBOL(stv6110_attach);
446
447 module_param(debug, int, 0644);
448 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
449
450 MODULE_DESCRIPTION("ST STV6110 driver");
451 MODULE_AUTHOR("Igor M. Liplianin");
452 MODULE_LICENSE("GPL");
453