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