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