]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/dvb-frontends/stb6100.c
[media] dvb_frontend: merge duplicate dvb_tuner_ops.release implementations
[mirror_ubuntu-artful-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/slab.h>
26 #include <linux/string.h>
27
28 #include "dvb_frontend.h"
29 #include "stb6100.h"
30
31 static unsigned int verbose;
32 module_param(verbose, int, 0644);
33
34 /* Max transfer size done by I2C transfer functions */
35 #define MAX_XFER_SIZE 64
36
37 #define FE_ERROR 0
38 #define FE_NOTICE 1
39 #define FE_INFO 2
40 #define FE_DEBUG 3
41
42 #define dprintk(x, y, z, format, arg...) do { \
43 if (z) { \
44 if ((x > FE_ERROR) && (x > y)) \
45 printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
46 else if ((x > FE_NOTICE) && (x > y)) \
47 printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
48 else if ((x > FE_INFO) && (x > y)) \
49 printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
50 else if ((x > FE_DEBUG) && (x > y)) \
51 printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
52 } else { \
53 if (x > y) \
54 printk(format, ##arg); \
55 } \
56 } while (0)
57
58 struct stb6100_lkup {
59 u32 val_low;
60 u32 val_high;
61 u8 reg;
62 };
63
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 }
79 };
80
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",
95 };
96
97 /* Template for normalisation, i.e. setting unused or undocumented
98 * bits as required according to the documentation.
99 */
100 struct stb6100_regmask {
101 u8 mask;
102 u8 set;
103 };
104
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 },
118 };
119
120 /*
121 * Currently unused. Some boards might need it in the future
122 */
123 static inline void stb6100_normalise_regs(u8 regs[])
124 {
125 int i;
126
127 for (i = 0; i < STB6100_NUMREGS; i++)
128 regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
129 }
130
131 static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
132 {
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
139 };
140
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);
145
146 return -EREMOTEIO;
147 }
148 if (unlikely(verbose > FE_DEBUG)) {
149 int i;
150
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]);
154 }
155 return 0;
156 }
157
158 static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
159 {
160 u8 regs[STB6100_NUMREGS];
161
162 struct i2c_msg msg = {
163 .addr = state->config->tuner_address + reg,
164 .flags = I2C_M_RD,
165 .buf = regs,
166 .len = 1
167 };
168
169 i2c_transfer(state->i2c, &msg, 1);
170
171 if (unlikely(reg >= STB6100_NUMREGS)) {
172 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
173 return -EINVAL;
174 }
175 if (unlikely(verbose > FE_DEBUG)) {
176 dprintk(verbose, FE_DEBUG, 1, " Read from 0x%02x", state->config->tuner_address);
177 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[reg], regs[0]);
178 }
179
180 return (unsigned int)regs[0];
181 }
182
183 static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
184 {
185 int rc;
186 u8 cmdbuf[MAX_XFER_SIZE];
187 struct i2c_msg msg = {
188 .addr = state->config->tuner_address,
189 .flags = 0,
190 .buf = cmdbuf,
191 .len = len + 1
192 };
193
194 if (1 + len > sizeof(cmdbuf)) {
195 printk(KERN_WARNING
196 "%s: i2c wr: len=%d is too big!\n",
197 KBUILD_MODNAME, len);
198 return -EINVAL;
199 }
200
201 if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) {
202 dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d",
203 start, len);
204 return -EINVAL;
205 }
206 memcpy(&cmdbuf[1], buf, len);
207 cmdbuf[0] = start;
208
209 if (unlikely(verbose > FE_DEBUG)) {
210 int i;
211
212 dprintk(verbose, FE_DEBUG, 1, " Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len);
213 for (i = 0; i < len; i++)
214 dprintk(verbose, FE_DEBUG, 1, " %s: 0x%02x", stb6100_regnames[start + i], buf[i]);
215 }
216 rc = i2c_transfer(state->i2c, &msg, 1);
217 if (unlikely(rc != 1)) {
218 dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]",
219 (unsigned int)state->config->tuner_address, start, len, rc);
220 return -EREMOTEIO;
221 }
222 return 0;
223 }
224
225 static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
226 {
227 if (unlikely(reg >= STB6100_NUMREGS)) {
228 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
229 return -EREMOTEIO;
230 }
231 data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set;
232 return stb6100_write_reg_range(state, &data, reg, 1);
233 }
234
235
236 static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
237 {
238 int rc;
239 struct stb6100_state *state = fe->tuner_priv;
240
241 rc = stb6100_read_reg(state, STB6100_LD);
242 if (rc < 0) {
243 dprintk(verbose, FE_ERROR, 1, "%s failed", __func__);
244 return rc;
245 }
246 return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
247 }
248
249 static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
250 {
251 int rc;
252 u8 f;
253 u32 bw;
254 struct stb6100_state *state = fe->tuner_priv;
255
256 rc = stb6100_read_reg(state, STB6100_F);
257 if (rc < 0)
258 return rc;
259 f = rc & STB6100_F_F;
260
261 bw = (f + 5) * 2000; /* x2 for ZIF */
262
263 *bandwidth = state->bandwidth = bw * 1000;
264 dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
265 return 0;
266 }
267
268 static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
269 {
270 u32 tmp;
271 int rc;
272 struct stb6100_state *state = fe->tuner_priv;
273
274 dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
275
276 bandwidth /= 2; /* ZIF */
277
278 if (bandwidth >= 36000000) /* F[4:0] BW/2 max =31+5=36 mhz for F=31 */
279 tmp = 31;
280 else if (bandwidth <= 5000000) /* bw/2 min = 5Mhz for F=0 */
281 tmp = 0;
282 else /* if 5 < bw/2 < 36 */
283 tmp = (bandwidth + 500000) / 1000000 - 5;
284
285 /* Turn on LPF bandwidth setting clock control,
286 * set bandwidth, wait 10ms, turn off.
287 */
288 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK);
289 if (rc < 0)
290 return rc;
291 rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp);
292 if (rc < 0)
293 return rc;
294
295 msleep(5); /* This is dangerous as another (related) thread may start */
296
297 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
298 if (rc < 0)
299 return rc;
300
301 msleep(10); /* This is dangerous as another (related) thread may start */
302
303 return 0;
304 }
305
306 static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
307 {
308 int rc;
309 u32 nint, nfrac, fvco;
310 int psd2, odiv;
311 struct stb6100_state *state = fe->tuner_priv;
312 u8 regs[STB6100_NUMREGS];
313
314 rc = stb6100_read_regs(state, regs);
315 if (rc < 0)
316 return rc;
317
318 odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT;
319 psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT;
320 nint = regs[STB6100_NI];
321 nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB];
322 fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2);
323 *frequency = state->frequency = fvco >> (odiv + 1);
324
325 dprintk(verbose, FE_DEBUG, 1,
326 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
327 state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac);
328 return 0;
329 }
330
331
332 static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
333 {
334 int rc;
335 const struct stb6100_lkup *ptr;
336 struct stb6100_state *state = fe->tuner_priv;
337 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
338
339 u32 srate = 0, fvco, nint, nfrac;
340 u8 regs[STB6100_NUMREGS];
341 u8 g, psd2, odiv;
342
343 dprintk(verbose, FE_DEBUG, 1, "Version 2010-8-14 13:51");
344
345 if (fe->ops.get_frontend) {
346 dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
347 fe->ops.get_frontend(fe, p);
348 }
349 srate = p->symbol_rate;
350
351 /* Set up tuner cleanly, LPF calibration on */
352 rc = stb6100_write_reg(state, STB6100_FCCK, 0x4d | STB6100_FCCK_FCCK);
353 if (rc < 0)
354 return rc; /* allow LPF calibration */
355
356 /* PLL Loop disabled, bias on, VCO on, synth on */
357 regs[STB6100_LPEN] = 0xeb;
358 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
359 if (rc < 0)
360 return rc;
361
362 /* Program the registers with their data values */
363
364 /* VCO divide ratio (LO divide ratio, VCO prescaler enable). */
365 if (frequency <= 1075000)
366 odiv = 1;
367 else
368 odiv = 0;
369
370 /* VCO enabled, search clock off as per LL3.7, 3.4.1 */
371 regs[STB6100_VCO] = 0xe0 | (odiv << STB6100_VCO_ODIV_SHIFT);
372
373 /* OSM */
374 for (ptr = lkup;
375 (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high);
376 ptr++);
377
378 if (ptr->val_high == 0) {
379 printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency);
380 return -EINVAL;
381 }
382 regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
383 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
384 if (rc < 0)
385 return rc;
386
387 if ((frequency > 1075000) && (frequency <= 1325000))
388 psd2 = 0;
389 else
390 psd2 = 1;
391 /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4) */
392 fvco = frequency << (1 + odiv);
393 /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1))) */
394 nint = fvco / (state->reference << psd2);
395 /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9 */
396 nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
397 << (9 - psd2), state->reference);
398
399 /* NI */
400 regs[STB6100_NI] = nint;
401 rc = stb6100_write_reg(state, STB6100_NI, regs[STB6100_NI]);
402 if (rc < 0)
403 return rc;
404
405 /* NF */
406 regs[STB6100_NF_LSB] = nfrac;
407 rc = stb6100_write_reg(state, STB6100_NF_LSB, regs[STB6100_NF_LSB]);
408 if (rc < 0)
409 return rc;
410
411 /* K */
412 regs[STB6100_K] = (0x38 & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT);
413 regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
414 rc = stb6100_write_reg(state, STB6100_K, regs[STB6100_K]);
415 if (rc < 0)
416 return rc;
417
418 /* G Baseband gain. */
419 if (srate >= 15000000)
420 g = 9; /* +4 dB */
421 else if (srate >= 5000000)
422 g = 11; /* +8 dB */
423 else
424 g = 14; /* +14 dB */
425
426 regs[STB6100_G] = (0x10 & ~STB6100_G_G) | g;
427 regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */
428 regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */
429 rc = stb6100_write_reg(state, STB6100_G, regs[STB6100_G]);
430 if (rc < 0)
431 return rc;
432
433 /* F we don't write as it is set up in BW set */
434
435 /* DLB set DC servo loop BW to 160Hz (LLA 3.8 / 2.1) */
436 regs[STB6100_DLB] = 0xcc;
437 rc = stb6100_write_reg(state, STB6100_DLB, regs[STB6100_DLB]);
438 if (rc < 0)
439 return rc;
440
441 dprintk(verbose, FE_DEBUG, 1,
442 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
443 frequency, srate, (unsigned int)g, (unsigned int)odiv,
444 (unsigned int)psd2, state->reference,
445 ptr->reg, fvco, nint, nfrac);
446
447 /* Set up the test registers */
448 regs[STB6100_TEST1] = 0x8f;
449 rc = stb6100_write_reg(state, STB6100_TEST1, regs[STB6100_TEST1]);
450 if (rc < 0)
451 return rc;
452 regs[STB6100_TEST3] = 0xde;
453 rc = stb6100_write_reg(state, STB6100_TEST3, regs[STB6100_TEST3]);
454 if (rc < 0)
455 return rc;
456
457 /* Bring up tuner according to LLA 3.7 3.4.1, step 2 */
458 regs[STB6100_LPEN] = 0xfb; /* PLL Loop enabled, bias on, VCO on, synth on */
459 rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN]);
460 if (rc < 0)
461 return rc;
462
463 msleep(2);
464
465 /* Bring up tuner according to LLA 3.7 3.4.1, step 3 */
466 regs[STB6100_VCO] &= ~STB6100_VCO_OCK; /* VCO fast search */
467 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
468 if (rc < 0)
469 return rc;
470
471 msleep(10); /* This is dangerous as another (related) thread may start */ /* wait for LO to lock */
472
473 regs[STB6100_VCO] &= ~STB6100_VCO_OSCH; /* vco search disabled */
474 regs[STB6100_VCO] |= STB6100_VCO_OCK; /* search clock off */
475 rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO]);
476 if (rc < 0)
477 return rc;
478
479 rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d);
480 if (rc < 0)
481 return rc; /* Stop LPF calibration */
482
483 msleep(10); /* This is dangerous as another (related) thread may start */
484 /* wait for stabilisation, (should not be necessary) */
485 return 0;
486 }
487
488 static int stb6100_sleep(struct dvb_frontend *fe)
489 {
490 /* TODO: power down */
491 return 0;
492 }
493
494 static int stb6100_init(struct dvb_frontend *fe)
495 {
496 struct stb6100_state *state = fe->tuner_priv;
497 int refclk = 27000000; /* Hz */
498
499 /*
500 * iqsense = 1
501 * tunerstep = 125000
502 */
503 state->bandwidth = 36000000; /* Hz */
504 state->reference = refclk / 1000; /* kHz */
505
506 /* Set default bandwidth. Modified, PN 13-May-10 */
507 return 0;
508 }
509
510 static int stb6100_set_params(struct dvb_frontend *fe)
511 {
512 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
513
514 if (c->frequency > 0)
515 stb6100_set_frequency(fe, c->frequency);
516
517 if (c->bandwidth_hz > 0)
518 stb6100_set_bandwidth(fe, c->bandwidth_hz);
519
520 return 0;
521 }
522
523 static const struct dvb_tuner_ops stb6100_ops = {
524 .info = {
525 .name = "STB6100 Silicon Tuner",
526 .frequency_min = 950000,
527 .frequency_max = 2150000,
528 .frequency_step = 0,
529 },
530
531 .init = stb6100_init,
532 .sleep = stb6100_sleep,
533 .get_status = stb6100_get_status,
534 .set_params = stb6100_set_params,
535 .get_frequency = stb6100_get_frequency,
536 .get_bandwidth = stb6100_get_bandwidth,
537 .release = dvb_tuner_simple_release
538 };
539
540 struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
541 const struct stb6100_config *config,
542 struct i2c_adapter *i2c)
543 {
544 struct stb6100_state *state = NULL;
545
546 state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
547 if (!state)
548 return NULL;
549
550 state->config = config;
551 state->i2c = i2c;
552 state->frontend = fe;
553 state->reference = config->refclock / 1000; /* kHz */
554 fe->tuner_priv = state;
555 fe->ops.tuner_ops = stb6100_ops;
556
557 printk("%s: Attaching STB6100 \n", __func__);
558 return fe;
559 }
560
561 EXPORT_SYMBOL(stb6100_attach);
562 MODULE_PARM_DESC(verbose, "Set Verbosity level");
563
564 MODULE_AUTHOR("Manu Abraham");
565 MODULE_DESCRIPTION("STB6100 Silicon tuner");
566 MODULE_LICENSE("GPL");