]> err.no Git - linux-2.6/blob - drivers/media/dvb/frontends/zl10353.c
V4L/DVB (6656): zl10353: store frequencies in 0.1kHz to eliminate rounding errors
[linux-2.6] / drivers / media / dvb / frontends / zl10353.c
1 /*
2  * Driver for Zarlink DVB-T ZL10353 demodulator
3  *
4  * Copyright (C) 2006, 2007 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
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/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <asm/div64.h>
29
30 #include "dvb_frontend.h"
31 #include "zl10353_priv.h"
32 #include "zl10353.h"
33
34 struct zl10353_state {
35         struct i2c_adapter *i2c;
36         struct dvb_frontend frontend;
37
38         struct zl10353_config config;
39 };
40
41 static int debug;
42 #define dprintk(args...) \
43         do { \
44                 if (debug) printk(KERN_DEBUG "zl10353: " args); \
45         } while (0)
46
47 static int debug_regs = 0;
48
49 static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
50 {
51         struct zl10353_state *state = fe->demodulator_priv;
52         u8 buf[2] = { reg, val };
53         struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
54                                .buf = buf, .len = 2 };
55         int err = i2c_transfer(state->i2c, &msg, 1);
56         if (err != 1) {
57                 printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
58                 return err;
59         }
60         return 0;
61 }
62
63 static int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
64 {
65         int err, i;
66         for (i = 0; i < ilen - 1; i++)
67                 if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
68                         return err;
69
70         return 0;
71 }
72
73 static int zl10353_read_register(struct zl10353_state *state, u8 reg)
74 {
75         int ret;
76         u8 b0[1] = { reg };
77         u8 b1[1] = { 0 };
78         struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
79                                     .flags = 0,
80                                     .buf = b0, .len = 1 },
81                                   { .addr = state->config.demod_address,
82                                     .flags = I2C_M_RD,
83                                     .buf = b1, .len = 1 } };
84
85         ret = i2c_transfer(state->i2c, msg, 2);
86
87         if (ret != 2) {
88                 printk("%s: readreg error (reg=%d, ret==%i)\n",
89                        __FUNCTION__, reg, ret);
90                 return ret;
91         }
92
93         return b1[0];
94 }
95
96 static void zl10353_dump_regs(struct dvb_frontend *fe)
97 {
98         struct zl10353_state *state = fe->demodulator_priv;
99         char buf[52], buf2[4];
100         int ret;
101         u8 reg;
102
103         /* Dump all registers. */
104         for (reg = 0; ; reg++) {
105                 if (reg % 16 == 0) {
106                         if (reg)
107                                 printk(KERN_DEBUG "%s\n", buf);
108                         sprintf(buf, "%02x: ", reg);
109                 }
110                 ret = zl10353_read_register(state, reg);
111                 if (ret >= 0)
112                         sprintf(buf2, "%02x ", (u8)ret);
113                 else
114                         strcpy(buf2, "-- ");
115                 strcat(buf, buf2);
116                 if (reg == 0xff)
117                         break;
118         }
119         printk(KERN_DEBUG "%s\n", buf);
120 }
121
122 static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
123                                       enum fe_bandwidth bandwidth,
124                                       u16 *nominal_rate)
125 {
126         struct zl10353_state *state = fe->demodulator_priv;
127         u32 adc_clock = 450560; /* 45.056 MHz */
128         u64 value;
129         u8 bw;
130
131         if (state->config.adc_clock)
132                 adc_clock = state->config.adc_clock;
133
134         switch (bandwidth) {
135         case BANDWIDTH_6_MHZ:
136                 bw = 6;
137                 break;
138         case BANDWIDTH_7_MHZ:
139                 bw = 7;
140                 break;
141         case BANDWIDTH_8_MHZ:
142         default:
143                 bw = 8;
144                 break;
145         }
146
147         value = (bw * (u64)10 * (1 << 23) / 7 * 125 + adc_clock / 2);
148         do_div(value, adc_clock);
149         *nominal_rate = value;
150
151         dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
152                 __FUNCTION__, bw, adc_clock, *nominal_rate);
153 }
154
155 static void zl10353_calc_input_freq(struct dvb_frontend *fe,
156                                     u16 *input_freq)
157 {
158         struct zl10353_state *state = fe->demodulator_priv;
159         u32 adc_clock = 450560; /* 45.056  MHz */
160         int if2 = 361667;       /* 36.1667 MHz */
161         int ife;
162         u64 value;
163
164         if (state->config.adc_clock)
165                 adc_clock = state->config.adc_clock;
166         if (state->config.if2)
167                 if2 = state->config.if2;
168
169         if (adc_clock >= if2 * 2)
170                 ife = if2;
171         else {
172                 ife = adc_clock - (if2 % adc_clock);
173                 if (ife > adc_clock / 2)
174                         ife = adc_clock - ife;
175         }
176         value = (u64)65536 * ife + adc_clock / 2;
177         do_div(value, adc_clock);
178         *input_freq = -value;
179
180         dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
181                 __FUNCTION__, if2, ife, adc_clock, -(int)value, *input_freq);
182 }
183
184 static int zl10353_sleep(struct dvb_frontend *fe)
185 {
186         static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
187
188         zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
189         return 0;
190 }
191
192 static int zl10353_set_parameters(struct dvb_frontend *fe,
193                                   struct dvb_frontend_parameters *param)
194 {
195         struct zl10353_state *state = fe->demodulator_priv;
196         u16 nominal_rate, input_freq;
197         u8 pllbuf[6] = { 0x67 };
198
199         /* These settings set "auto-everything" and start the FSM. */
200         zl10353_single_write(fe, 0x55, 0x80);
201         udelay(200);
202         zl10353_single_write(fe, 0xEA, 0x01);
203         udelay(200);
204         zl10353_single_write(fe, 0xEA, 0x00);
205
206         zl10353_single_write(fe, 0x56, 0x28);
207         zl10353_single_write(fe, 0x89, 0x20);
208         zl10353_single_write(fe, 0x5E, 0x00);
209
210         zl10353_calc_nominal_rate(fe, param->u.ofdm.bandwidth, &nominal_rate);
211         zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate));
212         zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate));
213
214         zl10353_calc_input_freq(fe, &input_freq);
215         zl10353_single_write(fe, INPUT_FREQ_1, msb(input_freq));
216         zl10353_single_write(fe, INPUT_FREQ_0, lsb(input_freq));
217
218         if (fe->ops.i2c_gate_ctrl)
219                 fe->ops.i2c_gate_ctrl(fe, 0);
220
221         /*
222          * If there is no tuner attached to the secondary I2C bus, we call
223          * set_params to program a potential tuner attached somewhere else.
224          * Otherwise, we update the PLL registers via calc_regs.
225          */
226         if (state->config.no_tuner) {
227                 if (fe->ops.tuner_ops.set_params) {
228                         fe->ops.tuner_ops.set_params(fe, param);
229                         if (fe->ops.i2c_gate_ctrl)
230                                 fe->ops.i2c_gate_ctrl(fe, 0);
231                 }
232         } else if (fe->ops.tuner_ops.calc_regs) {
233                 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf + 1, 5);
234                 pllbuf[1] <<= 1;
235                 zl10353_write(fe, pllbuf, sizeof(pllbuf));
236         }
237
238         zl10353_single_write(fe, 0x5F, 0x13);
239
240         /* If no attached tuner or invalid PLL registers, just start the FSM. */
241         if (state->config.no_tuner || fe->ops.tuner_ops.calc_regs == NULL)
242                 zl10353_single_write(fe, FSM_GO, 0x01);
243         else
244                 zl10353_single_write(fe, TUNER_GO, 0x01);
245
246         udelay(250);
247         zl10353_single_write(fe, 0xE4, 0x00);
248         zl10353_single_write(fe, 0xE5, 0x2A);
249         zl10353_single_write(fe, 0xE9, 0x02);
250         zl10353_single_write(fe, 0xE7, 0x40);
251         zl10353_single_write(fe, 0xE8, 0x10);
252
253         return 0;
254 }
255
256 static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
257 {
258         struct zl10353_state *state = fe->demodulator_priv;
259         int s6, s7, s8;
260
261         if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
262                 return -EREMOTEIO;
263         if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
264                 return -EREMOTEIO;
265         if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
266                 return -EREMOTEIO;
267
268         *status = 0;
269         if (s6 & (1 << 2))
270                 *status |= FE_HAS_CARRIER;
271         if (s6 & (1 << 1))
272                 *status |= FE_HAS_VITERBI;
273         if (s6 & (1 << 5))
274                 *status |= FE_HAS_LOCK;
275         if (s7 & (1 << 4))
276                 *status |= FE_HAS_SYNC;
277         if (s8 & (1 << 6))
278                 *status |= FE_HAS_SIGNAL;
279
280         if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
281             (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
282                 *status &= ~FE_HAS_LOCK;
283
284         return 0;
285 }
286
287 static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
288 {
289         struct zl10353_state *state = fe->demodulator_priv;
290
291         *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
292                zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
293                zl10353_read_register(state, RS_ERR_CNT_0);
294
295         return 0;
296 }
297
298 static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
299 {
300         struct zl10353_state *state = fe->demodulator_priv;
301
302         u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
303                      zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
304
305         *strength = ~signal;
306
307         return 0;
308 }
309
310 static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
311 {
312         struct zl10353_state *state = fe->demodulator_priv;
313         u8 _snr;
314
315         if (debug_regs)
316                 zl10353_dump_regs(fe);
317
318         _snr = zl10353_read_register(state, SNR);
319         *snr = (_snr << 8) | _snr;
320
321         return 0;
322 }
323
324 static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
325 {
326         struct zl10353_state *state = fe->demodulator_priv;
327
328         *ucblocks = zl10353_read_register(state, RS_UBC_1) << 8 |
329                     zl10353_read_register(state, RS_UBC_0);
330
331         return 0;
332 }
333
334 static int zl10353_get_tune_settings(struct dvb_frontend *fe,
335                                      struct dvb_frontend_tune_settings
336                                          *fe_tune_settings)
337 {
338         fe_tune_settings->min_delay_ms = 1000;
339         fe_tune_settings->step_size = 0;
340         fe_tune_settings->max_drift = 0;
341
342         return 0;
343 }
344
345 static int zl10353_init(struct dvb_frontend *fe)
346 {
347         struct zl10353_state *state = fe->demodulator_priv;
348         u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
349         int rc = 0;
350
351         if (debug_regs)
352                 zl10353_dump_regs(fe);
353         if (state->config.parallel_ts)
354                 zl10353_reset_attach[2] &= ~0x20;
355
356         /* Do a "hard" reset if not already done */
357         if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
358             zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
359                 rc = zl10353_write(fe, zl10353_reset_attach,
360                                    sizeof(zl10353_reset_attach));
361                 if (debug_regs)
362                         zl10353_dump_regs(fe);
363         }
364
365         return 0;
366 }
367
368 static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
369 {
370         u8 val = 0x0a;
371
372         if (enable)
373                 val |= 0x10;
374
375         return zl10353_single_write(fe, 0x62, val);
376 }
377
378 static void zl10353_release(struct dvb_frontend *fe)
379 {
380         struct zl10353_state *state = fe->demodulator_priv;
381         kfree(state);
382 }
383
384 static struct dvb_frontend_ops zl10353_ops;
385
386 struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
387                                     struct i2c_adapter *i2c)
388 {
389         struct zl10353_state *state = NULL;
390
391         /* allocate memory for the internal state */
392         state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
393         if (state == NULL)
394                 goto error;
395
396         /* setup the state */
397         state->i2c = i2c;
398         memcpy(&state->config, config, sizeof(struct zl10353_config));
399
400         /* check if the demod is there */
401         if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
402                 goto error;
403
404         /* create dvb_frontend */
405         memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
406         state->frontend.demodulator_priv = state;
407
408         return &state->frontend;
409 error:
410         kfree(state);
411         return NULL;
412 }
413
414 static struct dvb_frontend_ops zl10353_ops = {
415
416         .info = {
417                 .name                   = "Zarlink ZL10353 DVB-T",
418                 .type                   = FE_OFDM,
419                 .frequency_min          = 174000000,
420                 .frequency_max          = 862000000,
421                 .frequency_stepsize     = 166667,
422                 .frequency_tolerance    = 0,
423                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
424                         FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
425                         FE_CAN_FEC_AUTO |
426                         FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
427                         FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
428                         FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
429                         FE_CAN_MUTE_TS
430         },
431
432         .release = zl10353_release,
433
434         .init = zl10353_init,
435         .sleep = zl10353_sleep,
436         .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
437         .write = zl10353_write,
438
439         .set_frontend = zl10353_set_parameters,
440         .get_tune_settings = zl10353_get_tune_settings,
441
442         .read_status = zl10353_read_status,
443         .read_ber = zl10353_read_ber,
444         .read_signal_strength = zl10353_read_signal_strength,
445         .read_snr = zl10353_read_snr,
446         .read_ucblocks = zl10353_read_ucblocks,
447 };
448
449 module_param(debug, int, 0644);
450 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
451
452 module_param(debug_regs, int, 0644);
453 MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
454
455 MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
456 MODULE_AUTHOR("Chris Pascoe");
457 MODULE_LICENSE("GPL");
458
459 EXPORT_SYMBOL(zl10353_attach);