staging: usbip: bugfix for isochronous packets and optimization
[zen-stable.git] / drivers / media / dvb / frontends / stb6100.c
blob64673b8b64a2ff0232c67de5f0e6f428c3e3b52e
1 /*
2 STB6100 Silicon Tuner
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
5 Copyright (C) ST Microelectronics
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
28 #include "dvb_frontend.h"
29 #include "stb6100.h"
31 static unsigned int verbose;
32 module_param(verbose, int, 0644);
35 #define FE_ERROR 0
36 #define FE_NOTICE 1
37 #define FE_INFO 2
38 #define FE_DEBUG 3
40 #define dprintk(x, y, z, format, arg...) do { \
41 if (z) { \
42 if ((x > FE_ERROR) && (x > y)) \
43 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
44 else if ((x > FE_NOTICE) && (x > y)) \
45 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
46 else if ((x > FE_INFO) && (x > y)) \
47 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
48 else if ((x > FE_DEBUG) && (x > y)) \
49 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
50 } else { \
51 if (x > y) \
52 printk(format, ##arg); \
53 } \
54 } while (0)
56 struct stb6100_lkup {
57 u32 val_low;
58 u32 val_high;
59 u8 reg;
62 static int stb6100_release(struct dvb_frontend *fe);
64 static const struct stb6100_lkup lkup[] = {
65 { 0, 950000, 0x0a },
66 { 950000, 1000000, 0x0a },
67 { 1000000, 1075000, 0x0c },
68 { 1075000, 1200000, 0x00 },
69 { 1200000, 1300000, 0x01 },
70 { 1300000, 1370000, 0x02 },
71 { 1370000, 1470000, 0x04 },
72 { 1470000, 1530000, 0x05 },
73 { 1530000, 1650000, 0x06 },
74 { 1650000, 1800000, 0x08 },
75 { 1800000, 1950000, 0x0a },
76 { 1950000, 2150000, 0x0c },
77 { 2150000, 9999999, 0x0c },
78 { 0, 0, 0x00 }
81 /* Register names for easy debugging. */
82 static const char *stb6100_regnames[] = {
83 [STB6100_LD] = "LD",
84 [STB6100_VCO] = "VCO",
85 [STB6100_NI] = "NI",
86 [STB6100_NF_LSB] = "NF",
87 [STB6100_K] = "K",
88 [STB6100_G] = "G",
89 [STB6100_F] = "F",
90 [STB6100_DLB] = "DLB",
91 [STB6100_TEST1] = "TEST1",
92 [STB6100_FCCK] = "FCCK",
93 [STB6100_LPEN] = "LPEN",
94 [STB6100_TEST3] = "TEST3",
97 /* Template for normalisation, i.e. setting unused or undocumented
98 * bits as required according to the documentation.
100 struct stb6100_regmask {
101 u8 mask;
102 u8 set;
105 static const struct stb6100_regmask stb6100_template[] = {
106 [STB6100_LD] = { 0xff, 0x00 },
107 [STB6100_VCO] = { 0xff, 0x00 },
108 [STB6100_NI] = { 0xff, 0x00 },
109 [STB6100_NF_LSB] = { 0xff, 0x00 },
110 [STB6100_K] = { 0xc7, 0x38 },
111 [STB6100_G] = { 0xef, 0x10 },
112 [STB6100_F] = { 0x1f, 0xc0 },
113 [STB6100_DLB] = { 0x38, 0xc4 },
114 [STB6100_TEST1] = { 0x00, 0x8f },
115 [STB6100_FCCK] = { 0x40, 0x0d },
116 [STB6100_LPEN] = { 0xf0, 0x0b },
117 [STB6100_TEST3] = { 0x00, 0xde },
121 * Currently unused. Some boards might need it in the future
123 static inline void stb6100_normalise_regs(u8 regs[])
125 int i;
127 for (i = 0; i < STB6100_NUMREGS; i++)
128 regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
131 static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
133 int rc;
134 struct i2c_msg msg = {
135 .addr = state->config->tuner_address,
136 .flags = I2C_M_RD,
137 .buf = regs,
138 .len = STB6100_NUMREGS
141 rc = i2c_transfer(state->i2c, &msg, 1);
142 if (unlikely(rc != 1)) {
143 dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]",
144 state->config->tuner_address, rc);
146 return -EREMOTEIO;
148 if (unlikely(verbose > FE_DEBUG)) {
149 int i;
151 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
152 for (i = 0; i < STB6100_NUMREGS; i++)
153 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[i], regs[i]);
155 return 0;
158 static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
160 u8 regs[STB6100_NUMREGS];
161 int rc;
163 struct i2c_msg msg = {
164 .addr = state->config->tuner_address + reg,
165 .flags = I2C_M_RD,
166 .buf = regs,
167 .len = 1
170 rc = i2c_transfer(state->i2c, &msg, 1);
172 if (unlikely(reg >= STB6100_NUMREGS)) {
173 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
174 return -EINVAL;
176 if (unlikely(verbose > FE_DEBUG)) {
177 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
178 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[reg], regs[0]);
181 return (unsigned int)regs[0];
184 static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
186 int rc;
187 u8 cmdbuf[len + 1];
188 struct i2c_msg msg = {
189 .addr = state->config->tuner_address,
190 .flags = 0,
191 .buf = cmdbuf,
192 .len = len + 1
195 if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) {
196 dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d",
197 start, len);
198 return -EINVAL;
200 memcpy(&cmdbuf[1], buf, len);
201 cmdbuf[0] = start;
203 if (unlikely(verbose > FE_DEBUG)) {
204 int i;
206 dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len);
207 for (i = 0; i < len; i++)
208 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]);
210 rc = i2c_transfer(state->i2c, &msg, 1);
211 if (unlikely(rc != 1)) {
212 dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]",
213 (unsigned int)state->config->tuner_address, start, len, rc);
214 return -EREMOTEIO;
216 return 0;
219 static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
221 if (unlikely(reg >= STB6100_NUMREGS)) {
222 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
223 return -EREMOTEIO;
225 data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set;
226 return stb6100_write_reg_range(state, &data, reg, 1);
230 static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
232 int rc;
233 struct stb6100_state *state = fe->tuner_priv;
235 rc = stb6100_read_reg(state, STB6100_LD);
236 if (rc < 0) {
237 dprintk(verbose, FE_ERROR, 1, "%s failed", __func__);
238 return rc;
240 return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
243 static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
245 int rc;
246 u8 f;
247 struct stb6100_state *state = fe->tuner_priv;
249 rc = stb6100_read_reg(state, STB6100_F);
250 if (rc < 0)
251 return rc;
252 f = rc & STB6100_F_F;
254 state->status.bandwidth = (f + 5) * 2000; /* x2 for ZIF */
256 *bandwidth = state->bandwidth = state->status.bandwidth * 1000;
257 dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
258 return 0;
261 static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
263 u32 tmp;
264 int rc;
265 struct stb6100_state *state = fe->tuner_priv;
267 dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
269 bandwidth /= 2; /* ZIF */
271 if (bandwidth >= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */
272 tmp = 31;
273 else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */
274 tmp = 0;
275 else /* if 5 < bw/2 < 36 */
276 tmp = (bandwidth + 500000) / 1000000 - 5;
278 /* Turn on LPF bandwidth setting clock control,
279 * set bandwidth, wait 10ms, turn off.
281 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK);
282 if (rc < 0)
283 return rc;
284 rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp);
285 if (rc < 0)
286 return rc;
288 msleep(5); /* This is dangerous as another (related) thread may start */
290 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
291 if (rc < 0)
292 return rc;
294 msleep(10); /* This is dangerous as another (related) thread may start */
296 return 0;
299 static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
301 int rc;
302 u32 nint, nfrac, fvco;
303 int psd2, odiv;
304 struct stb6100_state *state = fe->tuner_priv;
305 u8 regs[STB6100_NUMREGS];
307 rc = stb6100_read_regs(state, regs);
308 if (rc < 0)
309 return rc;
311 odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT;
312 psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT;
313 nint = regs[STB6100_NI];
314 nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB];
315 fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2);
316 *frequency = state->frequency = fvco >> (odiv + 1);
318 dprintk(verbose, FE_DEBUG, 1,
319 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
320 state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac);
321 return 0;
325 static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
327 int rc;
328 const struct stb6100_lkup *ptr;
329 struct stb6100_state *state = fe->tuner_priv;
330 struct dvb_frontend_parameters p;
332 u32 srate = 0, fvco, nint, nfrac;
333 u8 regs[STB6100_NUMREGS];
334 u8 g, psd2, odiv;
336 dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51");
338 if (fe->ops.get_frontend) {
339 dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
340 fe->ops.get_frontend(fe, &p);
342 srate = p.u.qpsk.symbol_rate;
344 /* Set up tuner cleanly, LPF calibration on */
345 rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK);
346 if (rc < 0)
347 return rc; /* allow LPF calibration */
349 /* PLL Loop disabled, bias on, VCO on, synth on */
350 regs[STB6100_LPEN] = 0xeb;
351 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
352 if (rc < 0)
353 return rc;
355 /* Program the registers with their data values */
357 /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */
358 if (frequency <= 1075000)
359 odiv = 1;
360 else
361 odiv = 0;
363 /* VCO enabled, seach clock off as per LL3.7, 3.4.1 */
364 regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT);
366 /* OSM */
367 for (ptr = lkup;
368 (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high);
369 ptr++);
371 if (ptr->val_high == 0) {
372 printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency);
373 return -EINVAL;
375 regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
376 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
377 if (rc < 0)
378 return rc;
380 if ((frequency > 1075000) && (frequency <= 1325000))
381 psd2 = 0;
382 else
383 psd2 = 1;
384 /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */
385 fvco = frequency << (1 + odiv);
386 /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */
387 nint = fvco / (state->reference << psd2);
388 /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */
389 nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
390 << (9 - psd2), state->reference);
392 /* NI */
393 regs[STB6100_NI] = nint;
394 rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]);
395 if (rc < 0)
396 return rc;
398 /* NF */
399 regs[STB6100_NF_LSB] = nfrac;
400 rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]);
401 if (rc < 0)
402 return rc;
404 /* K */
405 regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT);
406 regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
407 rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]);
408 if (rc < 0)
409 return rc;
411 /* G Baseband gain. */
412 if (srate >= 15000000)
413 g = 9; /* +4 dB */
414 else if (srate >= 5000000)
415 g = 11; /* +8 dB */
416 else
417 g = 14; /* +14 dB */
419 regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g;
420 regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */
421 regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */
422 rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]);
423 if (rc < 0)
424 return rc;
426 /* F we don't write as it is set up in BW set */
428 /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */
429 regs[STB6100_DLB] = 0xcc;
430 rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]);
431 if (rc < 0)
432 return rc;
434 dprintk(verbose, FE_DEBUG, 1,
435 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
436 frequency, srate, (unsigned int)g, (unsigned int)odiv,
437 (unsigned int)psd2, state->reference,
438 ptr->reg, fvco, nint, nfrac);
440 /* Set up the test registers */
441 regs[STB6100_TEST1] = 0x8f;
442 rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]);
443 if (rc < 0)
444 return rc;
445 regs[STB6100_TEST3] = 0xde;
446 rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]);
447 if (rc < 0)
448 return rc;
450 /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */
451 regs[STB6100_LPEN] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */
452 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
453 if (rc < 0)
454 return rc;
456 msleep(2);
458 /* Bring up tuner according to LLA 3.7 3.4.1, step 3 */
459 regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */
460 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
461 if (rc < 0)
462 return rc;
464 msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */
466 regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */
467 regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */
468 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
469 if (rc < 0)
470 return rc;
472 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
473 if (rc < 0)
474 return rc; /* Stop LPF calibration */
476 msleep(10); /* This is dangerous as another (related) thread may start */
477 /* wait for stabilisation, (should not be necessary) */
478 return 0;
481 static int stb6100_sleep(struct dvb_frontend *fe)
483 /* TODO: power down */
484 return 0;
487 static int stb6100_init(struct dvb_frontend *fe)
489 struct stb6100_state *state = fe->tuner_priv;
490 struct tuner_state *status = &state->status;
492 status->tunerstep = 125000;
493 status->ifreq = 0;
494 status->refclock = 27000000; /* Hz */
495 status->iqsense = 1;
496 status->bandwidth = 36000; /* kHz */
497 state->bandwidth = status->bandwidth * 1000; /* Hz */
498 state->reference = status->refclock / 1000; /* kHz */
500 /* Set default bandwidth. Modified, PN 13-May-10 */
501 return 0;
504 static int stb6100_get_state(struct dvb_frontend *fe,
505 enum tuner_param param,
506 struct tuner_state *state)
508 switch (param) {
509 case DVBFE_TUNER_FREQUENCY:
510 stb6100_get_frequency(fe, &state->frequency);
511 break;
512 case DVBFE_TUNER_TUNERSTEP:
513 break;
514 case DVBFE_TUNER_IFFREQ:
515 break;
516 case DVBFE_TUNER_BANDWIDTH:
517 stb6100_get_bandwidth(fe, &state->bandwidth);
518 break;
519 case DVBFE_TUNER_REFCLOCK:
520 break;
521 default:
522 break;
525 return 0;
528 static int stb6100_set_state(struct dvb_frontend *fe,
529 enum tuner_param param,
530 struct tuner_state *state)
532 struct stb6100_state *tstate = fe->tuner_priv;
534 switch (param) {
535 case DVBFE_TUNER_FREQUENCY:
536 stb6100_set_frequency(fe, state->frequency);
537 tstate->frequency = state->frequency;
538 break;
539 case DVBFE_TUNER_TUNERSTEP:
540 break;
541 case DVBFE_TUNER_IFFREQ:
542 break;
543 case DVBFE_TUNER_BANDWIDTH:
544 stb6100_set_bandwidth(fe, state->bandwidth);
545 tstate->bandwidth = state->bandwidth;
546 break;
547 case DVBFE_TUNER_REFCLOCK:
548 break;
549 default:
550 break;
553 return 0;
556 static struct dvb_tuner_ops stb6100_ops = {
557 .info = {
558 .name = "STB6100 Silicon Tuner",
559 .frequency_min = 950000,
560 .frequency_max = 2150000,
561 .frequency_step = 0,
564 .init = stb6100_init,
565 .sleep = stb6100_sleep,
566 .get_status = stb6100_get_status,
567 .get_state = stb6100_get_state,
568 .set_state = stb6100_set_state,
569 .release = stb6100_release
572 struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
573 const struct stb6100_config *config,
574 struct i2c_adapter *i2c)
576 struct stb6100_state *state = NULL;
578 state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
579 if (state == NULL)
580 goto error;
582 state->config = config;
583 state->i2c = i2c;
584 state->frontend = fe;
585 state->reference = config->refclock / 1000; /* kHz */
586 fe->tuner_priv = state;
587 fe->ops.tuner_ops = stb6100_ops;
589 printk("%s: Attaching STB6100 \n", __func__);
590 return fe;
592 error:
593 kfree(state);
594 return NULL;
597 static int stb6100_release(struct dvb_frontend *fe)
599 struct stb6100_state *state = fe->tuner_priv;
601 fe->tuner_priv = NULL;
602 kfree(state);
604 return 0;
607 EXPORT_SYMBOL(stb6100_attach);
608 MODULE_PARM_DESC(verbose, "Set Verbosity level");
610 MODULE_AUTHOR("Manu Abraham");
611 MODULE_DESCRIPTION("STB6100 Silicon tuner");
612 MODULE_LICENSE("GPL");