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