Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[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_regs = 0;
42
43 static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
44 {
45         struct zl10353_state *state = fe->demodulator_priv;
46         u8 buf[2] = { reg, val };
47         struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
48                                .buf = buf, .len = 2 };
49         int err = i2c_transfer(state->i2c, &msg, 1);
50         if (err != 1) {
51                 printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
52                 return err;
53         }
54         return 0;
55 }
56
57 int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
58 {
59         int err, i;
60         for (i = 0; i < ilen - 1; i++)
61                 if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
62                         return err;
63
64         return 0;
65 }
66
67 static int zl10353_read_register(struct zl10353_state *state, u8 reg)
68 {
69         int ret;
70         u8 b0[1] = { reg };
71         u8 b1[1] = { 0 };
72         struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
73                                     .flags = 0,
74                                     .buf = b0, .len = 1 },
75                                   { .addr = state->config.demod_address,
76                                     .flags = I2C_M_RD,
77                                     .buf = b1, .len = 1 } };
78
79         ret = i2c_transfer(state->i2c, msg, 2);
80
81         if (ret != 2) {
82                 printk("%s: readreg error (reg=%d, ret==%i)\n",
83                        __FUNCTION__, reg, ret);
84                 return ret;
85         }
86
87         return b1[0];
88 }
89
90 static void zl10353_dump_regs(struct dvb_frontend *fe)
91 {
92         struct zl10353_state *state = fe->demodulator_priv;
93         char buf[52], buf2[4];
94         int ret;
95         u8 reg;
96
97         /* Dump all registers. */
98         for (reg = 0; ; reg++) {
99                 if (reg % 16 == 0) {
100                         if (reg)
101                                 printk(KERN_DEBUG "%s\n", buf);
102                         sprintf(buf, "%02x: ", reg);
103                 }
104                 ret = zl10353_read_register(state, reg);
105                 if (ret >= 0)
106                         sprintf(buf2, "%02x ", (u8)ret);
107                 else
108                         strcpy(buf2, "-- ");
109                 strcat(buf, buf2);
110                 if (reg == 0xff)
111                         break;
112         }
113         printk(KERN_DEBUG "%s\n", buf);
114 }
115
116 static int zl10353_sleep(struct dvb_frontend *fe)
117 {
118         static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
119
120         zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
121         return 0;
122 }
123
124 static int zl10353_set_parameters(struct dvb_frontend *fe,
125                                   struct dvb_frontend_parameters *param)
126 {
127         struct zl10353_state *state = fe->demodulator_priv;
128
129         u8 pllbuf[6] = { 0x67 };
130
131         /* These settings set "auto-everything" and start the FSM. */
132         zl10353_single_write(fe, 0x55, 0x80);
133         udelay(200);
134         zl10353_single_write(fe, 0xEA, 0x01);
135         udelay(200);
136         zl10353_single_write(fe, 0xEA, 0x00);
137
138         zl10353_single_write(fe, 0x56, 0x28);
139         zl10353_single_write(fe, 0x89, 0x20);
140         zl10353_single_write(fe, 0x5E, 0x00);
141         zl10353_single_write(fe, 0x65, 0x5A);
142         zl10353_single_write(fe, 0x66, 0xE9);
143         zl10353_single_write(fe, 0x6C, 0xCD);
144         zl10353_single_write(fe, 0x6D, 0x7E);
145         zl10353_single_write(fe, 0x62, 0x0A);
146
147         // if there is no attached secondary tuner, we call set_params to program
148         // a potential tuner attached somewhere else
149         if (state->config.no_tuner) {
150                 if (fe->ops.tuner_ops.set_params) {
151                         fe->ops.tuner_ops.set_params(fe, param);
152                         if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
153                 }
154         }
155
156         // if pllbuf is defined, retrieve the settings
157         if (fe->ops.tuner_ops.calc_regs) {
158                 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf+1, 5);
159                 pllbuf[1] <<= 1;
160         } else {
161                 // fake pllbuf settings
162                 pllbuf[1] = 0x61 << 1;
163                 pllbuf[2] = 0;
164                 pllbuf[3] = 0;
165                 pllbuf[3] = 0;
166                 pllbuf[4] = 0;
167         }
168
169         // there is no call to _just_ start decoding, so we send the pllbuf anyway
170         // even if there isn't a PLL attached to the secondary bus
171         zl10353_write(fe, pllbuf, sizeof(pllbuf));
172
173         zl10353_single_write(fe, 0x5F, 0x13);
174         zl10353_single_write(fe, 0x70, 0x01);
175         udelay(250);
176         zl10353_single_write(fe, 0xE4, 0x00);
177         zl10353_single_write(fe, 0xE5, 0x2A);
178         zl10353_single_write(fe, 0xE9, 0x02);
179         zl10353_single_write(fe, 0xE7, 0x40);
180         zl10353_single_write(fe, 0xE8, 0x10);
181
182         return 0;
183 }
184
185 static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
186 {
187         struct zl10353_state *state = fe->demodulator_priv;
188         int s6, s7, s8;
189
190         if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
191                 return -EREMOTEIO;
192         if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
193                 return -EREMOTEIO;
194         if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
195                 return -EREMOTEIO;
196
197         *status = 0;
198         if (s6 & (1 << 2))
199                 *status |= FE_HAS_CARRIER;
200         if (s6 & (1 << 1))
201                 *status |= FE_HAS_VITERBI;
202         if (s6 & (1 << 5))
203                 *status |= FE_HAS_LOCK;
204         if (s7 & (1 << 4))
205                 *status |= FE_HAS_SYNC;
206         if (s8 & (1 << 6))
207                 *status |= FE_HAS_SIGNAL;
208
209         if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
210             (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
211                 *status &= ~FE_HAS_LOCK;
212
213         return 0;
214 }
215
216 static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
217 {
218         struct zl10353_state *state = fe->demodulator_priv;
219         u8 _snr;
220
221         if (debug_regs)
222                 zl10353_dump_regs(fe);
223
224         _snr = zl10353_read_register(state, SNR);
225         *snr = (_snr << 8) | _snr;
226
227         return 0;
228 }
229
230 static int zl10353_get_tune_settings(struct dvb_frontend *fe,
231                                      struct dvb_frontend_tune_settings
232                                          *fe_tune_settings)
233 {
234         fe_tune_settings->min_delay_ms = 1000;
235         fe_tune_settings->step_size = 0;
236         fe_tune_settings->max_drift = 0;
237
238         return 0;
239 }
240
241 static int zl10353_init(struct dvb_frontend *fe)
242 {
243         struct zl10353_state *state = fe->demodulator_priv;
244         u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
245         int rc = 0;
246
247         if (debug_regs)
248                 zl10353_dump_regs(fe);
249         if (state->config.parallel_ts)
250                 zl10353_reset_attach[2] &= ~0x20;
251
252         /* Do a "hard" reset if not already done */
253         if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
254             zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
255                 rc = zl10353_write(fe, zl10353_reset_attach,
256                                    sizeof(zl10353_reset_attach));
257                 if (debug_regs)
258                         zl10353_dump_regs(fe);
259         }
260
261         return 0;
262 }
263
264 static void zl10353_release(struct dvb_frontend *fe)
265 {
266         struct zl10353_state *state = fe->demodulator_priv;
267         kfree(state);
268 }
269
270 static struct dvb_frontend_ops zl10353_ops;
271
272 struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
273                                     struct i2c_adapter *i2c)
274 {
275         struct zl10353_state *state = NULL;
276
277         /* allocate memory for the internal state */
278         state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
279         if (state == NULL)
280                 goto error;
281
282         /* setup the state */
283         state->i2c = i2c;
284         memcpy(&state->config, config, sizeof(struct zl10353_config));
285
286         /* check if the demod is there */
287         if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
288                 goto error;
289
290         /* create dvb_frontend */
291         memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
292         state->frontend.demodulator_priv = state;
293
294         return &state->frontend;
295 error:
296         kfree(state);
297         return NULL;
298 }
299
300 static struct dvb_frontend_ops zl10353_ops = {
301
302         .info = {
303                 .name                   = "Zarlink ZL10353 DVB-T",
304                 .type                   = FE_OFDM,
305                 .frequency_min          = 174000000,
306                 .frequency_max          = 862000000,
307                 .frequency_stepsize     = 166667,
308                 .frequency_tolerance    = 0,
309                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
310                         FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
311                         FE_CAN_FEC_AUTO |
312                         FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
313                         FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
314                         FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
315                         FE_CAN_MUTE_TS
316         },
317
318         .release = zl10353_release,
319
320         .init = zl10353_init,
321         .sleep = zl10353_sleep,
322         .write = zl10353_write,
323
324         .set_frontend = zl10353_set_parameters,
325         .get_tune_settings = zl10353_get_tune_settings,
326
327         .read_status = zl10353_read_status,
328         .read_snr = zl10353_read_snr,
329 };
330
331 module_param(debug_regs, int, 0644);
332 MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
333
334 MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
335 MODULE_AUTHOR("Chris Pascoe");
336 MODULE_LICENSE("GPL");
337
338 EXPORT_SYMBOL(zl10353_attach);