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