Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / dvb / frontends / stb6100.c
blob60ee18a94f43228b74a944cadad7c5ed430301d2
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/string.h>
27 #include "dvb_frontend.h"
28 #include "stb6100.h"
30 static unsigned int verbose;
31 module_param(verbose, int, 0644);
34 #define FE_ERROR 0
35 #define FE_NOTICE 1
36 #define FE_INFO 2
37 #define FE_DEBUG 3
39 #define dprintk(x, y, z, format, arg...) do { \
40 if (z) { \
41 if ((x > FE_ERROR) && (x > y)) \
42 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
43 else if ((x > FE_NOTICE) && (x > y)) \
44 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
45 else if ((x > FE_INFO) && (x > y)) \
46 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
47 else if ((x > FE_DEBUG) && (x > y)) \
48 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
49 } else { \
50 if (x > y) \
51 printk(format, ##arg); \
52 } \
53 } while(0)
55 struct stb6100_lkup {
56 u32 val_low;
57 u32 val_high;
58 u8 reg;
61 static int stb6100_release(struct dvb_frontend *fe);
63 static const struct stb6100_lkup lkup[] = {
64 { 0, 950000, 0x0a },
65 { 950000, 1000000, 0x0a },
66 { 1000000, 1075000, 0x0c },
67 { 1075000, 1200000, 0x00 },
68 { 1200000, 1300000, 0x01 },
69 { 1300000, 1370000, 0x02 },
70 { 1370000, 1470000, 0x04 },
71 { 1470000, 1530000, 0x05 },
72 { 1530000, 1650000, 0x06 },
73 { 1650000, 1800000, 0x08 },
74 { 1800000, 1950000, 0x0a },
75 { 1950000, 2150000, 0x0c },
76 { 2150000, 9999999, 0x0c },
77 { 0, 0, 0x00 }
80 /* Register names for easy debugging. */
81 static const char *stb6100_regnames[] = {
82 [STB6100_LD] = "LD",
83 [STB6100_VCO] = "VCO",
84 [STB6100_NI] = "NI",
85 [STB6100_NF_LSB] = "NF",
86 [STB6100_K] = "K",
87 [STB6100_G] = "G",
88 [STB6100_F] = "F",
89 [STB6100_DLB] = "DLB",
90 [STB6100_TEST1] = "TEST1",
91 [STB6100_FCCK] = "FCCK",
92 [STB6100_LPEN] = "LPEN",
93 [STB6100_TEST3] = "TEST3",
96 /* Template for normalisation, i.e. setting unused or undocumented
97 * bits as required according to the documentation.
99 struct stb6100_regmask {
100 u8 mask;
101 u8 set;
104 static const struct stb6100_regmask stb6100_template[] = {
105 [STB6100_LD] = { 0xff, 0x00 },
106 [STB6100_VCO] = { 0xff, 0x00 },
107 [STB6100_NI] = { 0xff, 0x00 },
108 [STB6100_NF_LSB] = { 0xff, 0x00 },
109 [STB6100_K] = { 0xc7, 0x38 },
110 [STB6100_G] = { 0xef, 0x10 },
111 [STB6100_F] = { 0x1f, 0xc0 },
112 [STB6100_DLB] = { 0x38, 0xc4 },
113 [STB6100_TEST1] = { 0x00, 0x8f },
114 [STB6100_FCCK] = { 0x40, 0x0d },
115 [STB6100_LPEN] = { 0xf0, 0x0b },
116 [STB6100_TEST3] = { 0x00, 0xde },
119 static void stb6100_normalise_regs(u8 regs[])
121 int i;
123 for (i = 0; i < STB6100_NUMREGS; i++)
124 regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
127 static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
129 int rc;
130 struct i2c_msg msg = {
131 .addr = state->config->tuner_address,
132 .flags = I2C_M_RD,
133 .buf = regs,
134 .len = STB6100_NUMREGS
137 rc = i2c_transfer(state->i2c, &msg, 1);
138 if (unlikely(rc != 1)) {
139 dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]",
140 state->config->tuner_address, rc);
142 return -EREMOTEIO;
144 if (unlikely(verbose > FE_DEBUG)) {
145 int i;
147 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
148 for (i = 0; i < STB6100_NUMREGS; i++)
149 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[i], regs[i]);
151 return 0;
154 static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
156 u8 regs[STB6100_NUMREGS];
157 int rc;
159 if (unlikely(reg >= STB6100_NUMREGS)) {
160 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
161 return -EINVAL;
163 if ((rc = stb6100_read_regs(state, regs)) < 0)
164 return rc;
165 return (unsigned int)regs[reg];
168 static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
170 int rc;
171 u8 cmdbuf[len + 1];
172 struct i2c_msg msg = {
173 .addr = state->config->tuner_address,
174 .flags = 0,
175 .buf = cmdbuf,
176 .len = len + 1
179 if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) {
180 dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d",
181 start, len);
182 return -EINVAL;
184 memcpy(&cmdbuf[1], buf, len);
185 cmdbuf[0] = start;
187 if (unlikely(verbose > FE_DEBUG)) {
188 int i;
190 dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len);
191 for (i = 0; i < len; i++)
192 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]);
194 rc = i2c_transfer(state->i2c, &msg, 1);
195 if (unlikely(rc != 1)) {
196 dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]",
197 (unsigned int)state->config->tuner_address, start, len, rc);
198 return -EREMOTEIO;
200 return 0;
203 static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
205 if (unlikely(reg >= STB6100_NUMREGS)) {
206 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
207 return -EREMOTEIO;
209 data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set;
210 return stb6100_write_reg_range(state, &data, reg, 1);
213 static int stb6100_write_regs(struct stb6100_state *state, u8 regs[])
215 stb6100_normalise_regs(regs);
216 return stb6100_write_reg_range(state, &regs[1], 1, STB6100_NUMREGS - 1);
219 static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
221 int rc;
222 struct stb6100_state *state = fe->tuner_priv;
224 if ((rc = stb6100_read_reg(state, STB6100_LD)) < 0)
225 return rc;
227 return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
230 static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
232 int rc;
233 u8 f;
234 struct stb6100_state *state = fe->tuner_priv;
236 if ((rc = stb6100_read_reg(state, STB6100_F)) < 0)
237 return rc;
238 f = rc & STB6100_F_F;
240 state->status.bandwidth = (f + 5) * 2000; /* x2 for ZIF */
242 *bandwidth = state->bandwidth = state->status.bandwidth * 1000;
243 dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
244 return 0;
247 static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
249 u32 tmp;
250 int rc;
251 struct stb6100_state *state = fe->tuner_priv;
253 dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
255 bandwidth /= 2; /* ZIF */
257 if (bandwidth >= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */
258 tmp = 31;
259 else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */
260 tmp = 0;
261 else /* if 5 < bw/2 < 36 */
262 tmp = (bandwidth + 500000) / 1000000 - 5;
264 /* Turn on LPF bandwidth setting clock control,
265 * set bandwidth, wait 10ms, turn off.
267 if ((rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK)) < 0)
268 return rc;
269 if ((rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp)) < 0)
270 return rc;
271 msleep(1);
272 if ((rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d)) < 0)
273 return rc;
275 return 0;
278 static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
280 int rc;
281 u32 nint, nfrac, fvco;
282 int psd2, odiv;
283 struct stb6100_state *state = fe->tuner_priv;
284 u8 regs[STB6100_NUMREGS];
286 if ((rc = stb6100_read_regs(state, regs)) < 0)
287 return rc;
289 odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT;
290 psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT;
291 nint = regs[STB6100_NI];
292 nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB];
293 fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2);
294 *frequency = state->frequency = fvco >> (odiv + 1);
296 dprintk(verbose, FE_DEBUG, 1,
297 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
298 state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac);
299 return 0;
303 static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
305 int rc;
306 const struct stb6100_lkup *ptr;
307 struct stb6100_state *state = fe->tuner_priv;
308 struct dvb_frontend_parameters p;
310 u32 srate = 0, fvco, nint, nfrac;
311 u8 regs[STB6100_NUMREGS];
312 u8 g, psd2, odiv;
314 if ((rc = stb6100_read_regs(state, regs)) < 0)
315 return rc;
317 if (fe->ops.get_frontend) {
318 dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
319 fe->ops.get_frontend(fe, &p);
321 srate = p.u.qpsk.symbol_rate;
323 regs[STB6100_DLB] = 0xdc;
324 /* Disable LPEN */
325 regs[STB6100_LPEN] &= ~STB6100_LPEN_LPEN; /* PLL Loop disabled */
327 if ((rc = stb6100_write_regs(state, regs)) < 0)
328 return rc;
330 /* Baseband gain. */
331 if (srate >= 15000000)
332 g = 9; // +4 dB
333 else if (srate >= 5000000)
334 g = 11; // +8 dB
335 else
336 g = 14; // +14 dB
338 regs[STB6100_G] = (regs[STB6100_G] & ~STB6100_G_G) | g;
339 regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */
340 regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */
342 /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */
343 if (frequency <= 1075000)
344 odiv = 1;
345 else
346 odiv = 0;
347 regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_ODIV) | (odiv << STB6100_VCO_ODIV_SHIFT);
349 if ((frequency > 1075000) && (frequency <= 1325000))
350 psd2 = 0;
351 else
352 psd2 = 1;
353 regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT);
355 /* OSM */
356 for (ptr = lkup;
357 (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high);
358 ptr++);
359 if (ptr->val_high == 0) {
360 printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency);
361 return -EINVAL;
363 regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
365 /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */
366 fvco = frequency << (1 + odiv);
367 /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */
368 nint = fvco / (state->reference << psd2);
369 /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */
370 nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
371 << (9 - psd2),
372 state->reference);
373 dprintk(verbose, FE_DEBUG, 1,
374 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
375 frequency, srate, (unsigned int)g, (unsigned int)odiv,
376 (unsigned int)psd2, state->reference,
377 ptr->reg, fvco, nint, nfrac);
378 regs[STB6100_NI] = nint;
379 regs[STB6100_NF_LSB] = nfrac;
380 regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
381 regs[STB6100_VCO] |= STB6100_VCO_OSCH; /* VCO search enabled */
382 regs[STB6100_VCO] |= STB6100_VCO_OCK; /* VCO search clock off */
383 regs[STB6100_FCCK] |= STB6100_FCCK_FCCK; /* LPF BW setting clock enabled */
384 regs[STB6100_LPEN] &= ~STB6100_LPEN_LPEN; /* PLL loop disabled */
385 /* Power up. */
386 regs[STB6100_LPEN] |= STB6100_LPEN_SYNP | STB6100_LPEN_OSCP | STB6100_LPEN_BEN;
388 msleep(2);
389 if ((rc = stb6100_write_regs(state, regs)) < 0)
390 return rc;
392 msleep(2);
393 regs[STB6100_LPEN] |= STB6100_LPEN_LPEN; /* PLL loop enabled */
394 if ((rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN])) < 0)
395 return rc;
397 regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */
398 if ((rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO])) < 0)
399 return rc;
401 msleep(10); /* wait for LO to lock */
402 regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */
403 regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */
404 if ((rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO])) < 0)
405 return rc;
406 regs[STB6100_FCCK] &= ~STB6100_FCCK_FCCK; /* LPF BW clock disabled */
407 stb6100_normalise_regs(regs);
408 if ((rc = stb6100_write_reg_range(state, &regs[1], 1, STB6100_NUMREGS - 3)) < 0)
409 return rc;
411 msleep(100);
413 return 0;
416 static int stb6100_sleep(struct dvb_frontend *fe)
418 /* TODO: power down */
419 return 0;
422 static int stb6100_init(struct dvb_frontend *fe)
424 struct stb6100_state *state = fe->tuner_priv;
425 struct tuner_state *status = &state->status;
427 status->tunerstep = 125000;
428 status->ifreq = 0;
429 status->refclock = 27000000; /* Hz */
430 status->iqsense = 1;
431 status->bandwidth = 36000; /* kHz */
432 state->bandwidth = status->bandwidth * 1000; /* Hz */
433 state->reference = status->refclock / 1000; /* kHz */
435 /* Set default bandwidth. */
436 return stb6100_set_bandwidth(fe, state->bandwidth);
439 static int stb6100_get_state(struct dvb_frontend *fe,
440 enum tuner_param param,
441 struct tuner_state *state)
443 switch (param) {
444 case DVBFE_TUNER_FREQUENCY:
445 stb6100_get_frequency(fe, &state->frequency);
446 break;
447 case DVBFE_TUNER_TUNERSTEP:
448 break;
449 case DVBFE_TUNER_IFFREQ:
450 break;
451 case DVBFE_TUNER_BANDWIDTH:
452 stb6100_get_bandwidth(fe, &state->bandwidth);
453 break;
454 case DVBFE_TUNER_REFCLOCK:
455 break;
456 default:
457 break;
460 return 0;
463 static int stb6100_set_state(struct dvb_frontend *fe,
464 enum tuner_param param,
465 struct tuner_state *state)
467 struct stb6100_state *tstate = fe->tuner_priv;
469 switch (param) {
470 case DVBFE_TUNER_FREQUENCY:
471 stb6100_set_frequency(fe, state->frequency);
472 tstate->frequency = state->frequency;
473 break;
474 case DVBFE_TUNER_TUNERSTEP:
475 break;
476 case DVBFE_TUNER_IFFREQ:
477 break;
478 case DVBFE_TUNER_BANDWIDTH:
479 stb6100_set_bandwidth(fe, state->bandwidth);
480 tstate->bandwidth = state->bandwidth;
481 break;
482 case DVBFE_TUNER_REFCLOCK:
483 break;
484 default:
485 break;
488 return 0;
491 static struct dvb_tuner_ops stb6100_ops = {
492 .info = {
493 .name = "STB6100 Silicon Tuner",
494 .frequency_min = 950000,
495 .frequency_max = 2150000,
496 .frequency_step = 0,
499 .init = stb6100_init,
500 .sleep = stb6100_sleep,
501 .get_status = stb6100_get_status,
502 .get_state = stb6100_get_state,
503 .set_state = stb6100_set_state,
504 .release = stb6100_release
507 struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
508 struct stb6100_config *config,
509 struct i2c_adapter *i2c)
511 struct stb6100_state *state = NULL;
513 state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
514 if (state == NULL)
515 goto error;
517 state->config = config;
518 state->i2c = i2c;
519 state->frontend = fe;
520 state->reference = config->refclock / 1000; /* kHz */
521 fe->tuner_priv = state;
522 fe->ops.tuner_ops = stb6100_ops;
524 printk("%s: Attaching STB6100 \n", __func__);
525 return fe;
527 error:
528 kfree(state);
529 return NULL;
532 static int stb6100_release(struct dvb_frontend *fe)
534 struct stb6100_state *state = fe->tuner_priv;
536 fe->tuner_priv = NULL;
537 kfree(state);
539 return 0;
542 EXPORT_SYMBOL(stb6100_attach);
543 MODULE_PARM_DESC(verbose, "Set Verbosity level");
545 MODULE_AUTHOR("Manu Abraham");
546 MODULE_DESCRIPTION("STB6100 Silicon tuner");
547 MODULE_LICENSE("GPL");