Fix misspellings collected by members of KJ list.
[linux-2.6] / drivers / media / dvb / frontends / zl10353.c
1 /*
2  * Driver for Zarlink DVB-T ZL10353 demodulator
3  *
4  * Copyright (C) 2006 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/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/string.h>
28 #include <linux/slab.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         u32 adc_clock = 22528; /* 20.480 MHz on the board(!?) */
127         u8 bw;
128         struct zl10353_state *state = fe->demodulator_priv;
129
130         if (state->config.adc_clock)
131                 adc_clock = state->config.adc_clock;
132
133         switch (bandwidth) {
134         case BANDWIDTH_6_MHZ:
135                 bw = 6;
136                 break;
137         case BANDWIDTH_7_MHZ:
138                 bw = 7;
139                 break;
140         case BANDWIDTH_8_MHZ:
141         default:
142                 bw = 8;
143                 break;
144         }
145
146         *nominal_rate = (64 * bw * (1<<16) / (7 * 8) * 4000 / adc_clock + 2) / 4;
147
148         dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
149                 __FUNCTION__, bw, adc_clock, *nominal_rate);
150 }
151
152 static int zl10353_sleep(struct dvb_frontend *fe)
153 {
154         static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
155
156         zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
157         return 0;
158 }
159
160 static int zl10353_set_parameters(struct dvb_frontend *fe,
161                                   struct dvb_frontend_parameters *param)
162 {
163         struct zl10353_state *state = fe->demodulator_priv;
164         u16 nominal_rate;
165         u8 pllbuf[6] = { 0x67 };
166
167         /* These settings set "auto-everything" and start the FSM. */
168         zl10353_single_write(fe, 0x55, 0x80);
169         udelay(200);
170         zl10353_single_write(fe, 0xEA, 0x01);
171         udelay(200);
172         zl10353_single_write(fe, 0xEA, 0x00);
173
174         zl10353_single_write(fe, 0x56, 0x28);
175         zl10353_single_write(fe, 0x89, 0x20);
176         zl10353_single_write(fe, 0x5E, 0x00);
177
178         zl10353_calc_nominal_rate(fe, param->u.ofdm.bandwidth, &nominal_rate);
179         zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate));
180         zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate));
181
182         zl10353_single_write(fe, 0x6C, 0xCD);
183         zl10353_single_write(fe, 0x6D, 0x7E);
184         if (fe->ops.i2c_gate_ctrl)
185                 fe->ops.i2c_gate_ctrl(fe, 0);
186
187         // if there is no attached secondary tuner, we call set_params to program
188         // a potential tuner attached somewhere else
189         if (state->config.no_tuner) {
190                 if (fe->ops.tuner_ops.set_params) {
191                         fe->ops.tuner_ops.set_params(fe, param);
192                         if (fe->ops.i2c_gate_ctrl)
193                                 fe->ops.i2c_gate_ctrl(fe, 0);
194                 }
195         }
196
197         // if pllbuf is defined, retrieve the settings
198         if (fe->ops.tuner_ops.calc_regs) {
199                 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf+1, 5);
200                 pllbuf[1] <<= 1;
201         } else {
202                 // fake pllbuf settings
203                 pllbuf[1] = 0x61 << 1;
204                 pllbuf[2] = 0;
205                 pllbuf[3] = 0;
206                 pllbuf[3] = 0;
207                 pllbuf[4] = 0;
208         }
209
210         // there is no call to _just_ start decoding, so we send the pllbuf anyway
211         // even if there isn't a PLL attached to the secondary bus
212         zl10353_write(fe, pllbuf, sizeof(pllbuf));
213
214         zl10353_single_write(fe, 0x5F, 0x13);
215         zl10353_single_write(fe, 0x70, 0x01);
216         udelay(250);
217         zl10353_single_write(fe, 0xE4, 0x00);
218         zl10353_single_write(fe, 0xE5, 0x2A);
219         zl10353_single_write(fe, 0xE9, 0x02);
220         zl10353_single_write(fe, 0xE7, 0x40);
221         zl10353_single_write(fe, 0xE8, 0x10);
222
223         return 0;
224 }
225
226 static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
227 {
228         struct zl10353_state *state = fe->demodulator_priv;
229         int s6, s7, s8;
230
231         if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
232                 return -EREMOTEIO;
233         if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
234                 return -EREMOTEIO;
235         if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
236                 return -EREMOTEIO;
237
238         *status = 0;
239         if (s6 & (1 << 2))
240                 *status |= FE_HAS_CARRIER;
241         if (s6 & (1 << 1))
242                 *status |= FE_HAS_VITERBI;
243         if (s6 & (1 << 5))
244                 *status |= FE_HAS_LOCK;
245         if (s7 & (1 << 4))
246                 *status |= FE_HAS_SYNC;
247         if (s8 & (1 << 6))
248                 *status |= FE_HAS_SIGNAL;
249
250         if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
251             (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
252                 *status &= ~FE_HAS_LOCK;
253
254         return 0;
255 }
256
257 static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
258 {
259         struct zl10353_state *state = fe->demodulator_priv;
260
261         *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
262                zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
263                zl10353_read_register(state, RS_ERR_CNT_0);
264
265         return 0;
266 }
267
268 static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
269 {
270         struct zl10353_state *state = fe->demodulator_priv;
271
272         u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
273                      zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
274
275         *strength = ~signal;
276
277         return 0;
278 }
279
280 static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
281 {
282         struct zl10353_state *state = fe->demodulator_priv;
283         u8 _snr;
284
285         if (debug_regs)
286                 zl10353_dump_regs(fe);
287
288         _snr = zl10353_read_register(state, SNR);
289         *snr = (_snr << 8) | _snr;
290
291         return 0;
292 }
293
294 static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
295 {
296         struct zl10353_state *state = fe->demodulator_priv;
297
298         *ucblocks = zl10353_read_register(state, RS_UBC_1) << 8 |
299                     zl10353_read_register(state, RS_UBC_0);
300
301         return 0;
302 }
303
304 static int zl10353_get_tune_settings(struct dvb_frontend *fe,
305                                      struct dvb_frontend_tune_settings
306                                          *fe_tune_settings)
307 {
308         fe_tune_settings->min_delay_ms = 1000;
309         fe_tune_settings->step_size = 0;
310         fe_tune_settings->max_drift = 0;
311
312         return 0;
313 }
314
315 static int zl10353_init(struct dvb_frontend *fe)
316 {
317         struct zl10353_state *state = fe->demodulator_priv;
318         u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
319         int rc = 0;
320
321         if (debug_regs)
322                 zl10353_dump_regs(fe);
323         if (state->config.parallel_ts)
324                 zl10353_reset_attach[2] &= ~0x20;
325
326         /* Do a "hard" reset if not already done */
327         if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
328             zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
329                 rc = zl10353_write(fe, zl10353_reset_attach,
330                                    sizeof(zl10353_reset_attach));
331                 if (debug_regs)
332                         zl10353_dump_regs(fe);
333         }
334
335         return 0;
336 }
337
338 static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
339 {
340         u8 val = 0x0a;
341
342         if (enable)
343                 val |= 0x10;
344
345         return zl10353_single_write(fe, 0x62, val);
346 }
347
348 static void zl10353_release(struct dvb_frontend *fe)
349 {
350         struct zl10353_state *state = fe->demodulator_priv;
351         kfree(state);
352 }
353
354 static struct dvb_frontend_ops zl10353_ops;
355
356 struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
357                                     struct i2c_adapter *i2c)
358 {
359         struct zl10353_state *state = NULL;
360
361         /* allocate memory for the internal state */
362         state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
363         if (state == NULL)
364                 goto error;
365
366         /* setup the state */
367         state->i2c = i2c;
368         memcpy(&state->config, config, sizeof(struct zl10353_config));
369
370         /* check if the demod is there */
371         if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
372                 goto error;
373
374         /* create dvb_frontend */
375         memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
376         state->frontend.demodulator_priv = state;
377
378         return &state->frontend;
379 error:
380         kfree(state);
381         return NULL;
382 }
383
384 static struct dvb_frontend_ops zl10353_ops = {
385
386         .info = {
387                 .name                   = "Zarlink ZL10353 DVB-T",
388                 .type                   = FE_OFDM,
389                 .frequency_min          = 174000000,
390                 .frequency_max          = 862000000,
391                 .frequency_stepsize     = 166667,
392                 .frequency_tolerance    = 0,
393                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
394                         FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
395                         FE_CAN_FEC_AUTO |
396                         FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
397                         FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
398                         FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
399                         FE_CAN_MUTE_TS
400         },
401
402         .release = zl10353_release,
403
404         .init = zl10353_init,
405         .sleep = zl10353_sleep,
406         .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
407         .write = zl10353_write,
408
409         .set_frontend = zl10353_set_parameters,
410         .get_tune_settings = zl10353_get_tune_settings,
411
412         .read_status = zl10353_read_status,
413         .read_ber = zl10353_read_ber,
414         .read_signal_strength = zl10353_read_signal_strength,
415         .read_snr = zl10353_read_snr,
416         .read_ucblocks = zl10353_read_ucblocks,
417 };
418
419 module_param(debug, int, 0644);
420 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
421
422 module_param(debug_regs, int, 0644);
423 MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
424
425 MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
426 MODULE_AUTHOR("Chris Pascoe");
427 MODULE_LICENSE("GPL");
428
429 EXPORT_SYMBOL(zl10353_attach);