aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/frontends/stb6100.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/dvb/frontends/stb6100.c')
-rw-r--r--drivers/media/dvb/frontends/stb6100.c545
1 files changed, 545 insertions, 0 deletions
diff --git a/drivers/media/dvb/frontends/stb6100.c b/drivers/media/dvb/frontends/stb6100.c
new file mode 100644
index 000000000000..ff39275ab49c
--- /dev/null
+++ b/drivers/media/dvb/frontends/stb6100.c
@@ -0,0 +1,545 @@
1/*
2 STB6100 Silicon Tuner
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
4
5 Copyright (C) ST Microelectronics
6
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.
11
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.
16
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.
20*/
21
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/string.h>
26
27#include "dvb_frontend.h"
28#include "stb6100.h"
29
30static unsigned int verbose;
31module_param(verbose, int, 0644);
32
33
34#define FE_ERROR 0
35#define FE_NOTICE 1
36#define FE_INFO 2
37#define FE_DEBUG 3
38
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)
54
55struct stb6100_lkup {
56 u32 val_low;
57 u32 val_high;
58 u8 reg;
59};
60
61static int stb6100_release(struct dvb_frontend *fe);
62
63static 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 }
78};
79
80/* Register names for easy debugging. */
81static 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",
94};
95
96/* Template for normalisation, i.e. setting unused or undocumented
97 * bits as required according to the documentation.
98 */
99struct stb6100_regmask {
100 u8 mask;
101 u8 set;
102};
103
104static 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 },
117};
118
119static void stb6100_normalise_regs(u8 regs[])
120{
121 int i;
122
123 for (i = 0; i < STB6100_NUMREGS; i++)
124 regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
125}
126
127static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
128{
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
135 };
136
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);
141
142 return -EREMOTEIO;
143 }
144 if (unlikely(verbose > FE_DEBUG)) {
145 int i;
146
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]);
150 }
151 return 0;
152}
153
154static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
155{
156 u8 regs[STB6100_NUMREGS];
157 int rc;
158
159 if (unlikely(reg >= STB6100_NUMREGS)) {
160 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
161 return -EINVAL;
162 }
163 if ((rc = stb6100_read_regs(state, regs)) < 0)
164 return rc;
165 return (unsigned int)regs[reg];
166}
167
168static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
169{
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
177 };
178
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;
183 }
184 memcpy(&cmdbuf[1], buf, len);
185 cmdbuf[0] = start;
186
187 if (unlikely(verbose > FE_DEBUG)) {
188 int i;
189
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]);
193 }
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;
199 }
200 return 0;
201}
202
203static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
204{
205 if (unlikely(reg >= STB6100_NUMREGS)) {
206 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
207 return -EREMOTEIO;
208 }
209 data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set;
210 return stb6100_write_reg_range(state, &data, reg, 1);
211}
212
213static int stb6100_write_regs(struct stb6100_state *state, u8 regs[])
214{
215 stb6100_normalise_regs(regs);
216 return stb6100_write_reg_range(state, &regs[1], 1, STB6100_NUMREGS - 1);
217}
218
219static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
220{
221 int rc;
222 struct stb6100_state *state = fe->tuner_priv;
223
224 if ((rc = stb6100_read_reg(state, STB6100_LD)) < 0)
225 return rc;
226
227 return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
228}
229
230static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
231{
232 int rc;
233 u8 f;
234 struct stb6100_state *state = fe->tuner_priv;
235
236 if ((rc = stb6100_read_reg(state, STB6100_F)) < 0)
237 return rc;
238 f = rc & STB6100_F_F;
239
240 state->status.bandwidth = (f + 5) * 2000; /* x2 for ZIF */
241
242 *bandwidth = state->bandwidth = state->status.bandwidth * 1000;
243 dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
244 return 0;
245}
246
247static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
248{
249 u32 tmp;
250 int rc;
251 struct stb6100_state *state = fe->tuner_priv;
252
253 dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
254
255 bandwidth /= 2; /* ZIF */
256
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;
263
264 /* Turn on LPF bandwidth setting clock control,
265 * set bandwidth, wait 10ms, turn off.
266 */
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;
274
275 return 0;
276}
277
278static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
279{
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];
285
286 if ((rc = stb6100_read_regs(state, regs)) < 0)
287 return rc;
288
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);
295
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;
300}
301
302
303static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
304{
305 int rc;
306 const struct stb6100_lkup *ptr;
307 struct stb6100_state *state = fe->tuner_priv;
308 struct dvb_frontend_parameters p;
309
310 u32 srate = 0, fvco, nint, nfrac;
311 u8 regs[STB6100_NUMREGS];
312 u8 g, psd2, odiv;
313
314 if ((rc = stb6100_read_regs(state, regs)) < 0)
315 return rc;
316
317 if (fe->ops.get_frontend) {
318 dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
319 fe->ops.get_frontend(fe, &p);
320 }
321 srate = p.u.qpsk.symbol_rate;
322
323 regs[STB6100_DLB] = 0xdc;
324 /* Disable LPEN */
325 regs[STB6100_LPEN] &= ~STB6100_LPEN_LPEN; /* PLL Loop disabled */
326
327 if ((rc = stb6100_write_regs(state, regs)) < 0)
328 return rc;
329
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
337
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 */
341
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);
348
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);
354
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;
362 }
363 regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
364
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 = (((fvco - (nint * state->reference << psd2)) << (9 - psd2)) + state->reference / 2) / state->reference;
371 dprintk(verbose, FE_DEBUG, 1,
372 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
373 frequency, srate, (unsigned int)g, (unsigned int)odiv,
374 (unsigned int)psd2, state->reference,
375 ptr->reg, fvco, nint, nfrac);
376 regs[STB6100_NI] = nint;
377 regs[STB6100_NF_LSB] = nfrac;
378 regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
379 regs[STB6100_VCO] |= STB6100_VCO_OSCH; /* VCO search enabled */
380 regs[STB6100_VCO] |= STB6100_VCO_OCK; /* VCO search clock off */
381 regs[STB6100_FCCK] |= STB6100_FCCK_FCCK; /* LPF BW setting clock enabled */
382 regs[STB6100_LPEN] &= ~STB6100_LPEN_LPEN; /* PLL loop disabled */
383 /* Power up. */
384 regs[STB6100_LPEN] |= STB6100_LPEN_SYNP | STB6100_LPEN_OSCP | STB6100_LPEN_BEN;
385
386 msleep(2);
387 if ((rc = stb6100_write_regs(state, regs)) < 0)
388 return rc;
389
390 msleep(2);
391 regs[STB6100_LPEN] |= STB6100_LPEN_LPEN; /* PLL loop enabled */
392 if ((rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN])) < 0)
393 return rc;
394
395 regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */
396 if ((rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO])) < 0)
397 return rc;
398
399 msleep(10); /* wait for LO to lock */
400 regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */
401 regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */
402 if ((rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO])) < 0)
403 return rc;
404 regs[STB6100_FCCK] &= ~STB6100_FCCK_FCCK; /* LPF BW clock disabled */
405 stb6100_normalise_regs(regs);
406 if ((rc = stb6100_write_reg_range(state, &regs[1], 1, STB6100_NUMREGS - 3)) < 0)
407 return rc;
408
409 msleep(100);
410
411 return 0;
412}
413
414static int stb6100_sleep(struct dvb_frontend *fe)
415{
416 /* TODO: power down */
417 return 0;
418}
419
420static int stb6100_init(struct dvb_frontend *fe)
421{
422 struct stb6100_state *state = fe->tuner_priv;
423 struct tuner_state *status = &state->status;
424
425 status->tunerstep = 125000;
426 status->ifreq = 0;
427 status->refclock = 27000000; /* Hz */
428 status->iqsense = 1;
429 status->bandwidth = 36000; /* kHz */
430 state->bandwidth = status->bandwidth * 1000; /* MHz */
431 state->reference = status->refclock / 1000; /* kHz */
432
433 /* Set default bandwidth. */
434 return stb6100_set_bandwidth(fe, status->bandwidth);
435}
436
437static int stb6100_get_state(struct dvb_frontend *fe,
438 enum tuner_param param,
439 struct tuner_state *state)
440{
441 switch (param) {
442 case DVBFE_TUNER_FREQUENCY:
443 stb6100_get_frequency(fe, &state->frequency);
444 break;
445 case DVBFE_TUNER_TUNERSTEP:
446 break;
447 case DVBFE_TUNER_IFFREQ:
448 break;
449 case DVBFE_TUNER_BANDWIDTH:
450 stb6100_get_bandwidth(fe, &state->bandwidth);
451 break;
452 case DVBFE_TUNER_REFCLOCK:
453 break;
454 default:
455 break;
456 }
457
458 return 0;
459}
460
461static int stb6100_set_state(struct dvb_frontend *fe,
462 enum tuner_param param,
463 struct tuner_state *state)
464{
465 struct stb6100_state *tstate = fe->tuner_priv;
466
467 switch (param) {
468 case DVBFE_TUNER_FREQUENCY:
469 stb6100_set_frequency(fe, state->frequency);
470 tstate->frequency = state->frequency;
471 break;
472 case DVBFE_TUNER_TUNERSTEP:
473 break;
474 case DVBFE_TUNER_IFFREQ:
475 break;
476 case DVBFE_TUNER_BANDWIDTH:
477 stb6100_set_bandwidth(fe, state->bandwidth);
478 tstate->bandwidth = state->bandwidth;
479 break;
480 case DVBFE_TUNER_REFCLOCK:
481 break;
482 default:
483 break;
484 }
485
486 return 0;
487}
488
489static struct dvb_tuner_ops stb6100_ops = {
490 .info = {
491 .name = "STB6100 Silicon Tuner",
492 .frequency_min = 950000,
493 .frequency_max = 2150000,
494 .frequency_step = 0,
495 },
496
497 .init = stb6100_init,
498 .sleep = stb6100_sleep,
499 .get_status = stb6100_get_status,
500 .get_state = stb6100_get_state,
501 .set_state = stb6100_set_state,
502 .release = stb6100_release
503};
504
505struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
506 struct stb6100_config *config,
507 struct i2c_adapter *i2c)
508{
509 struct stb6100_state *state = NULL;
510
511 state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
512 if (state == NULL)
513 goto error;
514
515 state->config = config;
516 state->i2c = i2c;
517 state->frontend = fe;
518 state->reference = config->refclock / 1000; /* kHz */
519 fe->tuner_priv = state;
520 fe->ops.tuner_ops = stb6100_ops;
521
522 printk("%s: Attaching STB6100 \n", __func__);
523 return fe;
524
525error:
526 kfree(state);
527 return NULL;
528}
529
530static int stb6100_release(struct dvb_frontend *fe)
531{
532 struct stb6100_state *state = fe->tuner_priv;
533
534 fe->tuner_priv = NULL;
535 kfree(state);
536
537 return 0;
538}
539
540EXPORT_SYMBOL(stb6100_attach);
541MODULE_PARM_DESC(verbose, "Set Verbosity level");
542
543MODULE_AUTHOR("Manu Abraham");
544MODULE_DESCRIPTION("STB6100 Silicon tuner");
545MODULE_LICENSE("GPL");