]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/media/dvb/frontends/stb6100.c
Merge branch 'fix/hda' into for-linus
[mirror_ubuntu-zesty-kernel.git] / drivers / media / dvb / frontends / stb6100.c
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
30 static unsigned int verbose;
31 module_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
55 struct stb6100_lkup {
56 u32 val_low;
57 u32 val_high;
58 u8 reg;
59 };
60
61 static int stb6100_release(struct dvb_frontend *fe);
62
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 }
78 };
79
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",
94 };
95
96 /* Template for normalisation, i.e. setting unused or undocumented
97 * bits as required according to the documentation.
98 */
99 struct stb6100_regmask {
100 u8 mask;
101 u8 set;
102 };
103
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 },
117 };
118
119 static 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
127 static 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
154 static 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
168 static 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
203 static 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
213 static 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
219 static 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
230 static 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
247 static 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
278 static 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
303 static 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 = 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;
387
388 msleep(2);
389 if ((rc = stb6100_write_regs(state, regs)) < 0)
390 return rc;
391
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;
396
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;
400
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;
410
411 msleep(100);
412
413 return 0;
414 }
415
416 static int stb6100_sleep(struct dvb_frontend *fe)
417 {
418 /* TODO: power down */
419 return 0;
420 }
421
422 static int stb6100_init(struct dvb_frontend *fe)
423 {
424 struct stb6100_state *state = fe->tuner_priv;
425 struct tuner_state *status = &state->status;
426
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 */
434
435 /* Set default bandwidth. */
436 return stb6100_set_bandwidth(fe, state->bandwidth);
437 }
438
439 static int stb6100_get_state(struct dvb_frontend *fe,
440 enum tuner_param param,
441 struct tuner_state *state)
442 {
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;
458 }
459
460 return 0;
461 }
462
463 static int stb6100_set_state(struct dvb_frontend *fe,
464 enum tuner_param param,
465 struct tuner_state *state)
466 {
467 struct stb6100_state *tstate = fe->tuner_priv;
468
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;
486 }
487
488 return 0;
489 }
490
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,
497 },
498
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
505 };
506
507 struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
508 struct stb6100_config *config,
509 struct i2c_adapter *i2c)
510 {
511 struct stb6100_state *state = NULL;
512
513 state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
514 if (state == NULL)
515 goto error;
516
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;
523
524 printk("%s: Attaching STB6100 \n", __func__);
525 return fe;
526
527 error:
528 kfree(state);
529 return NULL;
530 }
531
532 static int stb6100_release(struct dvb_frontend *fe)
533 {
534 struct stb6100_state *state = fe->tuner_priv;
535
536 fe->tuner_priv = NULL;
537 kfree(state);
538
539 return 0;
540 }
541
542 EXPORT_SYMBOL(stb6100_attach);
543 MODULE_PARM_DESC(verbose, "Set Verbosity level");
544
545 MODULE_AUTHOR("Manu Abraham");
546 MODULE_DESCRIPTION("STB6100 Silicon tuner");
547 MODULE_LICENSE("GPL");