Merge branch 'oprofile-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / media / dvb / frontends / cx22700.c
1 /*
2     Conexant cx22700 DVB OFDM demodulator driver
3
4     Copyright (C) 2001-2002 Convergence Integrated Media GmbH
5         Holger Waechtler <holger@convergence.de>
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
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include "dvb_frontend.h"
29 #include "cx22700.h"
30
31
32 struct cx22700_state {
33
34         struct i2c_adapter* i2c;
35
36         const struct cx22700_config* config;
37
38         struct dvb_frontend frontend;
39 };
40
41
42 static int debug;
43 #define dprintk(args...) \
44         do { \
45                 if (debug) printk(KERN_DEBUG "cx22700: " args); \
46         } while (0)
47
48 static u8 init_tab [] = {
49         0x04, 0x10,
50         0x05, 0x09,
51         0x06, 0x00,
52         0x08, 0x04,
53         0x09, 0x00,
54         0x0a, 0x01,
55         0x15, 0x40,
56         0x16, 0x10,
57         0x17, 0x87,
58         0x18, 0x17,
59         0x1a, 0x10,
60         0x25, 0x04,
61         0x2e, 0x00,
62         0x39, 0x00,
63         0x3a, 0x04,
64         0x45, 0x08,
65         0x46, 0x02,
66         0x47, 0x05,
67 };
68
69
70 static int cx22700_writereg (struct cx22700_state* state, u8 reg, u8 data)
71 {
72         int ret;
73         u8 buf [] = { reg, data };
74         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
75
76         dprintk ("%s\n", __func__);
77
78         ret = i2c_transfer (state->i2c, &msg, 1);
79
80         if (ret != 1)
81                 printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
82                         __func__, reg, data, ret);
83
84         return (ret != 1) ? -1 : 0;
85 }
86
87 static int cx22700_readreg (struct cx22700_state* state, u8 reg)
88 {
89         int ret;
90         u8 b0 [] = { reg };
91         u8 b1 [] = { 0 };
92         struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
93                            { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
94
95         dprintk ("%s\n", __func__);
96
97         ret = i2c_transfer (state->i2c, msg, 2);
98
99         if (ret != 2) return -EIO;
100
101         return b1[0];
102 }
103
104 static int cx22700_set_inversion (struct cx22700_state* state, int inversion)
105 {
106         u8 val;
107
108         dprintk ("%s\n", __func__);
109
110         switch (inversion) {
111         case INVERSION_AUTO:
112                 return -EOPNOTSUPP;
113         case INVERSION_ON:
114                 val = cx22700_readreg (state, 0x09);
115                 return cx22700_writereg (state, 0x09, val | 0x01);
116         case INVERSION_OFF:
117                 val = cx22700_readreg (state, 0x09);
118                 return cx22700_writereg (state, 0x09, val & 0xfe);
119         default:
120                 return -EINVAL;
121         }
122 }
123
124 static int cx22700_set_tps (struct cx22700_state *state, struct dvb_ofdm_parameters *p)
125 {
126         static const u8 qam_tab [4] = { 0, 1, 0, 2 };
127         static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 };
128         u8 val;
129
130         dprintk ("%s\n", __func__);
131
132         if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8)
133                 return -EINVAL;
134
135         if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8)
136                 return -EINVAL;
137
138         if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5)
139                 return -EINVAL;
140
141         if (p->guard_interval < GUARD_INTERVAL_1_32 ||
142             p->guard_interval > GUARD_INTERVAL_1_4)
143                 return -EINVAL;
144
145         if (p->transmission_mode != TRANSMISSION_MODE_2K &&
146             p->transmission_mode != TRANSMISSION_MODE_8K)
147                 return -EINVAL;
148
149         if (p->constellation != QPSK &&
150             p->constellation != QAM_16 &&
151             p->constellation != QAM_64)
152                 return -EINVAL;
153
154         if (p->hierarchy_information < HIERARCHY_NONE ||
155             p->hierarchy_information > HIERARCHY_4)
156                 return -EINVAL;
157
158         if (p->bandwidth < BANDWIDTH_8_MHZ && p->bandwidth > BANDWIDTH_6_MHZ)
159                 return -EINVAL;
160
161         if (p->bandwidth == BANDWIDTH_7_MHZ)
162                 cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 | 0x10));
163         else
164                 cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 & ~0x10));
165
166         val = qam_tab[p->constellation - QPSK];
167         val |= p->hierarchy_information - HIERARCHY_NONE;
168
169         cx22700_writereg (state, 0x04, val);
170
171         val = fec_tab[p->code_rate_HP - FEC_1_2] << 3;
172         val |= fec_tab[p->code_rate_LP - FEC_1_2];
173
174         cx22700_writereg (state, 0x05, val);
175
176         val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2;
177         val |= p->transmission_mode - TRANSMISSION_MODE_2K;
178
179         cx22700_writereg (state, 0x06, val);
180
181         cx22700_writereg (state, 0x08, 0x04 | 0x02);  /* use user tps parameters */
182         cx22700_writereg (state, 0x08, 0x04);         /* restart aquisition */
183
184         return 0;
185 }
186
187 static int cx22700_get_tps (struct cx22700_state* state, struct dvb_ofdm_parameters *p)
188 {
189         static const fe_modulation_t qam_tab [3] = { QPSK, QAM_16, QAM_64 };
190         static const fe_code_rate_t fec_tab [5] = { FEC_1_2, FEC_2_3, FEC_3_4,
191                                                     FEC_5_6, FEC_7_8 };
192         u8 val;
193
194         dprintk ("%s\n", __func__);
195
196         if (!(cx22700_readreg(state, 0x07) & 0x20))  /*  tps valid? */
197                 return -EAGAIN;
198
199         val = cx22700_readreg (state, 0x01);
200
201         if ((val & 0x7) > 4)
202                 p->hierarchy_information = HIERARCHY_AUTO;
203         else
204                 p->hierarchy_information = HIERARCHY_NONE + (val & 0x7);
205
206         if (((val >> 3) & 0x3) > 2)
207                 p->constellation = QAM_AUTO;
208         else
209                 p->constellation = qam_tab[(val >> 3) & 0x3];
210
211         val = cx22700_readreg (state, 0x02);
212
213         if (((val >> 3) & 0x07) > 4)
214                 p->code_rate_HP = FEC_AUTO;
215         else
216                 p->code_rate_HP = fec_tab[(val >> 3) & 0x07];
217
218         if ((val & 0x07) > 4)
219                 p->code_rate_LP = FEC_AUTO;
220         else
221                 p->code_rate_LP = fec_tab[val & 0x07];
222
223         val = cx22700_readreg (state, 0x03);
224
225         p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3);
226         p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1);
227
228         return 0;
229 }
230
231 static int cx22700_init (struct dvb_frontend* fe)
232
233 {       struct cx22700_state* state = fe->demodulator_priv;
234         int i;
235
236         dprintk("cx22700_init: init chip\n");
237
238         cx22700_writereg (state, 0x00, 0x02);   /*  soft reset */
239         cx22700_writereg (state, 0x00, 0x00);
240
241         msleep(10);
242
243         for (i=0; i<sizeof(init_tab); i+=2)
244                 cx22700_writereg (state, init_tab[i], init_tab[i+1]);
245
246         cx22700_writereg (state, 0x00, 0x01);
247
248         return 0;
249 }
250
251 static int cx22700_read_status(struct dvb_frontend* fe, fe_status_t* status)
252 {
253         struct cx22700_state* state = fe->demodulator_priv;
254
255         u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
256                    | (cx22700_readreg (state, 0x0e) << 1);
257         u8 sync = cx22700_readreg (state, 0x07);
258
259         *status = 0;
260
261         if (rs_ber < 0xff00)
262                 *status |= FE_HAS_SIGNAL;
263
264         if (sync & 0x20)
265                 *status |= FE_HAS_CARRIER;
266
267         if (sync & 0x10)
268                 *status |= FE_HAS_VITERBI;
269
270         if (sync & 0x10)
271                 *status |= FE_HAS_SYNC;
272
273         if (*status == 0x0f)
274                 *status |= FE_HAS_LOCK;
275
276         return 0;
277 }
278
279 static int cx22700_read_ber(struct dvb_frontend* fe, u32* ber)
280 {
281         struct cx22700_state* state = fe->demodulator_priv;
282
283         *ber = cx22700_readreg (state, 0x0c) & 0x7f;
284         cx22700_writereg (state, 0x0c, 0x00);
285
286         return 0;
287 }
288
289 static int cx22700_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
290 {
291         struct cx22700_state* state = fe->demodulator_priv;
292
293         u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
294                    | (cx22700_readreg (state, 0x0e) << 1);
295         *signal_strength = ~rs_ber;
296
297         return 0;
298 }
299
300 static int cx22700_read_snr(struct dvb_frontend* fe, u16* snr)
301 {
302         struct cx22700_state* state = fe->demodulator_priv;
303
304         u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)
305                    | (cx22700_readreg (state, 0x0e) << 1);
306         *snr = ~rs_ber;
307
308         return 0;
309 }
310
311 static int cx22700_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
312 {
313         struct cx22700_state* state = fe->demodulator_priv;
314
315         *ucblocks = cx22700_readreg (state, 0x0f);
316         cx22700_writereg (state, 0x0f, 0x00);
317
318         return 0;
319 }
320
321 static int cx22700_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
322 {
323         struct cx22700_state* state = fe->demodulator_priv;
324
325         cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/
326         cx22700_writereg (state, 0x00, 0x00);
327
328         if (fe->ops.tuner_ops.set_params) {
329                 fe->ops.tuner_ops.set_params(fe, p);
330                 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
331         }
332
333         cx22700_set_inversion (state, p->inversion);
334         cx22700_set_tps (state, &p->u.ofdm);
335         cx22700_writereg (state, 0x37, 0x01);  /* PAL loop filter off */
336         cx22700_writereg (state, 0x00, 0x01);  /* restart acquire */
337
338         return 0;
339 }
340
341 static int cx22700_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
342 {
343         struct cx22700_state* state = fe->demodulator_priv;
344         u8 reg09 = cx22700_readreg (state, 0x09);
345
346         p->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF;
347         return cx22700_get_tps (state, &p->u.ofdm);
348 }
349
350 static int cx22700_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
351 {
352         struct cx22700_state* state = fe->demodulator_priv;
353
354         if (enable) {
355                 return cx22700_writereg(state, 0x0a, 0x00);
356         } else {
357                 return cx22700_writereg(state, 0x0a, 0x01);
358         }
359 }
360
361 static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
362 {
363         fesettings->min_delay_ms = 150;
364         fesettings->step_size = 166667;
365         fesettings->max_drift = 166667*2;
366         return 0;
367 }
368
369 static void cx22700_release(struct dvb_frontend* fe)
370 {
371         struct cx22700_state* state = fe->demodulator_priv;
372         kfree(state);
373 }
374
375 static struct dvb_frontend_ops cx22700_ops;
376
377 struct dvb_frontend* cx22700_attach(const struct cx22700_config* config,
378                                     struct i2c_adapter* i2c)
379 {
380         struct cx22700_state* state = NULL;
381
382         /* allocate memory for the internal state */
383         state = kzalloc(sizeof(struct cx22700_state), GFP_KERNEL);
384         if (state == NULL) goto error;
385
386         /* setup the state */
387         state->config = config;
388         state->i2c = i2c;
389
390         /* check if the demod is there */
391         if (cx22700_readreg(state, 0x07) < 0) goto error;
392
393         /* create dvb_frontend */
394         memcpy(&state->frontend.ops, &cx22700_ops, sizeof(struct dvb_frontend_ops));
395         state->frontend.demodulator_priv = state;
396         return &state->frontend;
397
398 error:
399         kfree(state);
400         return NULL;
401 }
402
403 static struct dvb_frontend_ops cx22700_ops = {
404
405         .info = {
406                 .name                   = "Conexant CX22700 DVB-T",
407                 .type                   = FE_OFDM,
408                 .frequency_min          = 470000000,
409                 .frequency_max          = 860000000,
410                 .frequency_stepsize     = 166667,
411                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
412                       FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
413                       FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
414                       FE_CAN_RECOVER
415         },
416
417         .release = cx22700_release,
418
419         .init = cx22700_init,
420         .i2c_gate_ctrl = cx22700_i2c_gate_ctrl,
421
422         .set_frontend = cx22700_set_frontend,
423         .get_frontend = cx22700_get_frontend,
424         .get_tune_settings = cx22700_get_tune_settings,
425
426         .read_status = cx22700_read_status,
427         .read_ber = cx22700_read_ber,
428         .read_signal_strength = cx22700_read_signal_strength,
429         .read_snr = cx22700_read_snr,
430         .read_ucblocks = cx22700_read_ucblocks,
431 };
432
433 module_param(debug, int, 0644);
434 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
435
436 MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver");
437 MODULE_AUTHOR("Holger Waechtler");
438 MODULE_LICENSE("GPL");
439
440 EXPORT_SYMBOL(cx22700_attach);