Merge branch 'drm-intel-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/ickle...
[pandora-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
35 #define FE_ERROR                0
36 #define FE_NOTICE               1
37 #define FE_INFO                 2
38 #define FE_DEBUG                3
39
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)
55
56 struct stb6100_lkup {
57         u32 val_low;
58         u32 val_high;
59         u8   reg;
60 };
61
62 static int stb6100_release(struct dvb_frontend *fe);
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 static void stb6100_normalise_regs(u8 regs[])
121 {
122         int i;
123
124         for (i = 0; i < STB6100_NUMREGS; i++)
125                 regs[i] = (regs[i] & stb6100_template[i].mask) | stb6100_template[i].set;
126 }
127
128 static int stb6100_read_regs(struct stb6100_state *state, u8 regs[])
129 {
130         int rc;
131         struct i2c_msg msg = {
132                 .addr   = state->config->tuner_address,
133                 .flags  = I2C_M_RD,
134                 .buf    = regs,
135                 .len    = STB6100_NUMREGS
136         };
137
138         rc = i2c_transfer(state->i2c, &msg, 1);
139         if (unlikely(rc != 1)) {
140                 dprintk(verbose, FE_ERROR, 1, "Read (0x%x) err, rc=[%d]",
141                         state->config->tuner_address, rc);
142
143                 return -EREMOTEIO;
144         }
145         if (unlikely(verbose > FE_DEBUG)) {
146                 int i;
147
148                 dprintk(verbose, FE_DEBUG, 1, "    Read from 0x%02x", state->config->tuner_address);
149                 for (i = 0; i < STB6100_NUMREGS; i++)
150                         dprintk(verbose, FE_DEBUG, 1, "        %s: 0x%02x", stb6100_regnames[i], regs[i]);
151         }
152         return 0;
153 }
154
155 static int stb6100_read_reg(struct stb6100_state *state, u8 reg)
156 {
157         u8 regs[STB6100_NUMREGS];
158         int rc;
159
160         if (unlikely(reg >= STB6100_NUMREGS)) {
161                 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
162                 return -EINVAL;
163         }
164         if ((rc = stb6100_read_regs(state, regs)) < 0)
165                 return rc;
166         return (unsigned int)regs[reg];
167 }
168
169 static int stb6100_write_reg_range(struct stb6100_state *state, u8 buf[], int start, int len)
170 {
171         int rc;
172         u8 cmdbuf[len + 1];
173         struct i2c_msg msg = {
174                 .addr   = state->config->tuner_address,
175                 .flags  = 0,
176                 .buf    = cmdbuf,
177                 .len    = len + 1
178         };
179
180         if (unlikely(start < 1 || start + len > STB6100_NUMREGS)) {
181                 dprintk(verbose, FE_ERROR, 1, "Invalid register range %d:%d",
182                         start, len);
183                 return -EINVAL;
184         }
185         memcpy(&cmdbuf[1], buf, len);
186         cmdbuf[0] = start;
187
188         if (unlikely(verbose > FE_DEBUG)) {
189                 int i;
190
191                 dprintk(verbose, FE_DEBUG, 1, "    Write @ 0x%02x: [%d:%d]", state->config->tuner_address, start, len);
192                 for (i = 0; i < len; i++)
193                         dprintk(verbose, FE_DEBUG, 1, "        %s: 0x%02x", stb6100_regnames[start + i], buf[i]);
194         }
195         rc = i2c_transfer(state->i2c, &msg, 1);
196         if (unlikely(rc != 1)) {
197                 dprintk(verbose, FE_ERROR, 1, "(0x%x) write err [%d:%d], rc=[%d]",
198                         (unsigned int)state->config->tuner_address, start, len, rc);
199                 return -EREMOTEIO;
200         }
201         return 0;
202 }
203
204 static int stb6100_write_reg(struct stb6100_state *state, u8 reg, u8 data)
205 {
206         if (unlikely(reg >= STB6100_NUMREGS)) {
207                 dprintk(verbose, FE_ERROR, 1, "Invalid register offset 0x%x", reg);
208                 return -EREMOTEIO;
209         }
210         data = (data & stb6100_template[reg].mask) | stb6100_template[reg].set;
211         return stb6100_write_reg_range(state, &data, reg, 1);
212 }
213
214 static int stb6100_write_regs(struct stb6100_state *state, u8 regs[])
215 {
216         stb6100_normalise_regs(regs);
217         return stb6100_write_reg_range(state, &regs[1], 1, STB6100_NUMREGS - 1);
218 }
219
220 static int stb6100_get_status(struct dvb_frontend *fe, u32 *status)
221 {
222         int rc;
223         struct stb6100_state *state = fe->tuner_priv;
224
225         if ((rc = stb6100_read_reg(state, STB6100_LD)) < 0)
226                 return rc;
227
228         return (rc & STB6100_LD_LOCK) ? TUNER_STATUS_LOCKED : 0;
229 }
230
231 static int stb6100_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
232 {
233         int rc;
234         u8 f;
235         struct stb6100_state *state = fe->tuner_priv;
236
237         if ((rc = stb6100_read_reg(state, STB6100_F)) < 0)
238                 return rc;
239         f = rc & STB6100_F_F;
240
241         state->status.bandwidth = (f + 5) * 2000;       /* x2 for ZIF   */
242
243         *bandwidth = state->bandwidth = state->status.bandwidth * 1000;
244         dprintk(verbose, FE_DEBUG, 1, "bandwidth = %u Hz", state->bandwidth);
245         return 0;
246 }
247
248 static int stb6100_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
249 {
250         u32 tmp;
251         int rc;
252         struct stb6100_state *state = fe->tuner_priv;
253
254         dprintk(verbose, FE_DEBUG, 1, "set bandwidth to %u Hz", bandwidth);
255
256         bandwidth /= 2; /* ZIF */
257
258         if (bandwidth >= 36000000)      /* F[4:0] BW/2 max =31+5=36 mhz for F=31        */
259                 tmp = 31;
260         else if (bandwidth <= 5000000)  /* bw/2 min = 5Mhz for F=0                      */
261                 tmp = 0;
262         else                            /* if 5 < bw/2 < 36                             */
263                 tmp = (bandwidth + 500000) / 1000000 - 5;
264
265         /* Turn on LPF bandwidth setting clock control,
266          * set bandwidth, wait 10ms, turn off.
267          */
268         if ((rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d | STB6100_FCCK_FCCK)) < 0)
269                 return rc;
270         if ((rc = stb6100_write_reg(state, STB6100_F, 0xc0 | tmp)) < 0)
271                 return rc;
272         msleep(1);
273         if ((rc = stb6100_write_reg(state, STB6100_FCCK, 0x0d)) < 0)
274                 return rc;
275
276         return 0;
277 }
278
279 static int stb6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
280 {
281         int rc;
282         u32 nint, nfrac, fvco;
283         int psd2, odiv;
284         struct stb6100_state *state = fe->tuner_priv;
285         u8 regs[STB6100_NUMREGS];
286
287         if ((rc = stb6100_read_regs(state, regs)) < 0)
288                 return rc;
289
290         odiv = (regs[STB6100_VCO] & STB6100_VCO_ODIV) >> STB6100_VCO_ODIV_SHIFT;
291         psd2 = (regs[STB6100_K] & STB6100_K_PSD2) >> STB6100_K_PSD2_SHIFT;
292         nint = regs[STB6100_NI];
293         nfrac = ((regs[STB6100_K] & STB6100_K_NF_MSB) << 8) | regs[STB6100_NF_LSB];
294         fvco = (nfrac * state->reference >> (9 - psd2)) + (nint * state->reference << psd2);
295         *frequency = state->frequency = fvco >> (odiv + 1);
296
297         dprintk(verbose, FE_DEBUG, 1,
298                 "frequency = %u kHz, odiv = %u, psd2 = %u, fxtal = %u kHz, fvco = %u kHz, N(I) = %u, N(F) = %u",
299                 state->frequency, odiv, psd2, state->reference, fvco, nint, nfrac);
300         return 0;
301 }
302
303
304 static int stb6100_set_frequency(struct dvb_frontend *fe, u32 frequency)
305 {
306         int rc;
307         const struct stb6100_lkup *ptr;
308         struct stb6100_state *state = fe->tuner_priv;
309         struct dvb_frontend_parameters p;
310
311         u32 srate = 0, fvco, nint, nfrac;
312         u8 regs[STB6100_NUMREGS];
313         u8 g, psd2, odiv;
314
315         if ((rc = stb6100_read_regs(state, regs)) < 0)
316                 return rc;
317
318         if (fe->ops.get_frontend) {
319                 dprintk(verbose, FE_DEBUG, 1, "Get frontend parameters");
320                 fe->ops.get_frontend(fe, &p);
321         }
322         srate = p.u.qpsk.symbol_rate;
323
324         regs[STB6100_DLB] = 0xdc;
325         /* Disable LPEN */
326         regs[STB6100_LPEN] &= ~STB6100_LPEN_LPEN; /* PLL Loop disabled */
327
328         if ((rc = stb6100_write_regs(state, regs)) < 0)
329                 return rc;
330
331         /* Baseband gain.       */
332         if (srate >= 15000000)
333                 g = 9;  //  +4 dB
334         else if (srate >= 5000000)
335                 g = 11; //  +8 dB
336         else
337                 g = 14; // +14 dB
338
339         regs[STB6100_G] = (regs[STB6100_G] & ~STB6100_G_G) | g;
340         regs[STB6100_G] &= ~STB6100_G_GCT; /* mask GCT */
341         regs[STB6100_G] |= (1 << 5); /* 2Vp-p Mode */
342
343         /* VCO divide ratio (LO divide ratio, VCO prescaler enable).    */
344         if (frequency <= 1075000)
345                 odiv = 1;
346         else
347                 odiv = 0;
348         regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_ODIV) | (odiv << STB6100_VCO_ODIV_SHIFT);
349
350         if ((frequency > 1075000) && (frequency <= 1325000))
351                 psd2 = 0;
352         else
353                 psd2 = 1;
354         regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_PSD2) | (psd2 << STB6100_K_PSD2_SHIFT);
355
356         /* OSM  */
357         for (ptr = lkup;
358              (ptr->val_high != 0) && !CHKRANGE(frequency, ptr->val_low, ptr->val_high);
359              ptr++);
360         if (ptr->val_high == 0) {
361                 printk(KERN_ERR "%s: frequency out of range: %u kHz\n", __func__, frequency);
362                 return -EINVAL;
363         }
364         regs[STB6100_VCO] = (regs[STB6100_VCO] & ~STB6100_VCO_OSM) | ptr->reg;
365
366         /* F(VCO) = F(LO) * (ODIV == 0 ? 2 : 4)                 */
367         fvco = frequency << (1 + odiv);
368         /* N(I) = floor(f(VCO) / (f(XTAL) * (PSD2 ? 2 : 1)))    */
369         nint = fvco / (state->reference << psd2);
370         /* N(F) = round(f(VCO) / f(XTAL) * (PSD2 ? 2 : 1) - N(I)) * 2 ^ 9       */
371         nfrac = DIV_ROUND_CLOSEST((fvco - (nint * state->reference << psd2))
372                                          << (9 - psd2),
373                                   state->reference);
374         dprintk(verbose, FE_DEBUG, 1,
375                 "frequency = %u, srate = %u, g = %u, odiv = %u, psd2 = %u, fxtal = %u, osm = %u, fvco = %u, N(I) = %u, N(F) = %u",
376                 frequency, srate, (unsigned int)g, (unsigned int)odiv,
377                 (unsigned int)psd2, state->reference,
378                 ptr->reg, fvco, nint, nfrac);
379         regs[STB6100_NI] = nint;
380         regs[STB6100_NF_LSB] = nfrac;
381         regs[STB6100_K] = (regs[STB6100_K] & ~STB6100_K_NF_MSB) | ((nfrac >> 8) & STB6100_K_NF_MSB);
382         regs[STB6100_VCO] |= STB6100_VCO_OSCH;          /* VCO search enabled           */
383         regs[STB6100_VCO] |= STB6100_VCO_OCK;           /* VCO search clock off         */
384         regs[STB6100_FCCK] |= STB6100_FCCK_FCCK;        /* LPF BW setting clock enabled */
385         regs[STB6100_LPEN] &= ~STB6100_LPEN_LPEN;       /* PLL loop disabled            */
386         /* Power up. */
387         regs[STB6100_LPEN] |= STB6100_LPEN_SYNP | STB6100_LPEN_OSCP | STB6100_LPEN_BEN;
388
389         msleep(2);
390         if ((rc = stb6100_write_regs(state, regs)) < 0)
391                 return rc;
392
393         msleep(2);
394         regs[STB6100_LPEN] |= STB6100_LPEN_LPEN;        /* PLL loop enabled             */
395         if ((rc = stb6100_write_reg(state, STB6100_LPEN, regs[STB6100_LPEN])) < 0)
396                 return rc;
397
398         regs[STB6100_VCO] &= ~STB6100_VCO_OCK;          /* VCO fast search              */
399         if ((rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO])) < 0)
400                 return rc;
401
402         msleep(10);                                     /* wait for LO to lock          */
403         regs[STB6100_VCO] &= ~STB6100_VCO_OSCH;         /* vco search disabled          */
404         regs[STB6100_VCO] |= STB6100_VCO_OCK;           /* search clock off             */
405         if ((rc = stb6100_write_reg(state, STB6100_VCO, regs[STB6100_VCO])) < 0)
406                 return rc;
407         regs[STB6100_FCCK] &= ~STB6100_FCCK_FCCK;       /* LPF BW clock disabled        */
408         stb6100_normalise_regs(regs);
409         if ((rc = stb6100_write_reg_range(state, &regs[1], 1, STB6100_NUMREGS - 3)) < 0)
410                 return rc;
411
412         msleep(100);
413
414         return 0;
415 }
416
417 static int stb6100_sleep(struct dvb_frontend *fe)
418 {
419         /* TODO: power down     */
420         return 0;
421 }
422
423 static int stb6100_init(struct dvb_frontend *fe)
424 {
425         struct stb6100_state *state = fe->tuner_priv;
426         struct tuner_state *status = &state->status;
427
428         status->tunerstep       = 125000;
429         status->ifreq           = 0;
430         status->refclock        = 27000000;     /* Hz   */
431         status->iqsense         = 1;
432         status->bandwidth       = 36000;        /* kHz  */
433         state->bandwidth        = status->bandwidth * 1000;     /* Hz   */
434         state->reference        = status->refclock / 1000;      /* kHz  */
435
436         /* Set default bandwidth.       */
437         return stb6100_set_bandwidth(fe, state->bandwidth);
438 }
439
440 static int stb6100_get_state(struct dvb_frontend *fe,
441                              enum tuner_param param,
442                              struct tuner_state *state)
443 {
444         switch (param) {
445         case DVBFE_TUNER_FREQUENCY:
446                 stb6100_get_frequency(fe, &state->frequency);
447                 break;
448         case DVBFE_TUNER_TUNERSTEP:
449                 break;
450         case DVBFE_TUNER_IFFREQ:
451                 break;
452         case DVBFE_TUNER_BANDWIDTH:
453                 stb6100_get_bandwidth(fe, &state->bandwidth);
454                 break;
455         case DVBFE_TUNER_REFCLOCK:
456                 break;
457         default:
458                 break;
459         }
460
461         return 0;
462 }
463
464 static int stb6100_set_state(struct dvb_frontend *fe,
465                              enum tuner_param param,
466                              struct tuner_state *state)
467 {
468         struct stb6100_state *tstate = fe->tuner_priv;
469
470         switch (param) {
471         case DVBFE_TUNER_FREQUENCY:
472                 stb6100_set_frequency(fe, state->frequency);
473                 tstate->frequency = state->frequency;
474                 break;
475         case DVBFE_TUNER_TUNERSTEP:
476                 break;
477         case DVBFE_TUNER_IFFREQ:
478                 break;
479         case DVBFE_TUNER_BANDWIDTH:
480                 stb6100_set_bandwidth(fe, state->bandwidth);
481                 tstate->bandwidth = state->bandwidth;
482                 break;
483         case DVBFE_TUNER_REFCLOCK:
484                 break;
485         default:
486                 break;
487         }
488
489         return 0;
490 }
491
492 static struct dvb_tuner_ops stb6100_ops = {
493         .info = {
494                 .name                   = "STB6100 Silicon Tuner",
495                 .frequency_min          = 950000,
496                 .frequency_max          = 2150000,
497                 .frequency_step         = 0,
498         },
499
500         .init           = stb6100_init,
501         .sleep          = stb6100_sleep,
502         .get_status     = stb6100_get_status,
503         .get_state      = stb6100_get_state,
504         .set_state      = stb6100_set_state,
505         .release        = stb6100_release
506 };
507
508 struct dvb_frontend *stb6100_attach(struct dvb_frontend *fe,
509                                     const struct stb6100_config *config,
510                                     struct i2c_adapter *i2c)
511 {
512         struct stb6100_state *state = NULL;
513
514         state = kzalloc(sizeof (struct stb6100_state), GFP_KERNEL);
515         if (state == NULL)
516                 goto error;
517
518         state->config           = config;
519         state->i2c              = i2c;
520         state->frontend         = fe;
521         state->reference        = config->refclock / 1000; /* kHz */
522         fe->tuner_priv          = state;
523         fe->ops.tuner_ops       = stb6100_ops;
524
525         printk("%s: Attaching STB6100 \n", __func__);
526         return fe;
527
528 error:
529         kfree(state);
530         return NULL;
531 }
532
533 static int stb6100_release(struct dvb_frontend *fe)
534 {
535         struct stb6100_state *state = fe->tuner_priv;
536
537         fe->tuner_priv = NULL;
538         kfree(state);
539
540         return 0;
541 }
542
543 EXPORT_SYMBOL(stb6100_attach);
544 MODULE_PARM_DESC(verbose, "Set Verbosity level");
545
546 MODULE_AUTHOR("Manu Abraham");
547 MODULE_DESCRIPTION("STB6100 Silicon tuner");
548 MODULE_LICENSE("GPL");