Merge branch 'for-greg' of git://gitorious.org/usb/usb into usb-linus
[pandora-kernel.git] / drivers / media / dvb / frontends / mb86a20s.c
1 /*
2  *   Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
3  *
4  *   Copyright (C) 2010 Mauro Carvalho Chehab <mchehab@redhat.com>
5  *   Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
6  *
7  *   FIXME: Need to port to DVB v5.2 API
8  *
9  *   This program is free software; you can redistribute it and/or
10  *   modify it under the terms of the GNU General Public License as
11  *   published by the Free Software Foundation version 2.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *   General Public License for more details.
17  */
18
19 #include <linux/kernel.h>
20 #include <asm/div64.h>
21
22 #include "dvb_frontend.h"
23 #include "mb86a20s.h"
24
25 static int debug = 1;
26 module_param(debug, int, 0644);
27 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
28
29 #define rc(args...)  do {                                               \
30         printk(KERN_ERR  "mb86a20s: " args);                            \
31 } while (0)
32
33 #define dprintk(args...)                                                \
34         do {                                                            \
35                 if (debug) {                                            \
36                         printk(KERN_DEBUG "mb86a20s: %s: ", __func__);  \
37                         printk(args);                                   \
38                 }                                                       \
39         } while (0)
40
41 struct mb86a20s_state {
42         struct i2c_adapter *i2c;
43         const struct mb86a20s_config *config;
44
45         struct dvb_frontend frontend;
46
47         bool need_init;
48 };
49
50 struct regdata {
51         u8 reg;
52         u8 data;
53 };
54
55 /*
56  * Initialization sequence: Use whatevere default values that PV SBTVD
57  * does on its initialisation, obtained via USB snoop
58  */
59 static struct regdata mb86a20s_init[] = {
60         { 0x70, 0x0f },
61         { 0x70, 0xff },
62         { 0x08, 0x01 },
63         { 0x09, 0x3e },
64         { 0x50, 0xd1 },
65         { 0x51, 0x22 },
66         { 0x39, 0x01 },
67         { 0x71, 0x00 },
68         { 0x28, 0x2a },
69         { 0x29, 0x00 },
70         { 0x2a, 0xff },
71         { 0x2b, 0x80 },
72         { 0x28, 0x20 },
73         { 0x29, 0x33 },
74         { 0x2a, 0xdf },
75         { 0x2b, 0xa9 },
76         { 0x3b, 0x21 },
77         { 0x3c, 0x3a },
78         { 0x01, 0x0d },
79         { 0x04, 0x08 },
80         { 0x05, 0x05 },
81         { 0x04, 0x0e },
82         { 0x05, 0x00 },
83         { 0x04, 0x0f },
84         { 0x05, 0x14 },
85         { 0x04, 0x0b },
86         { 0x05, 0x8c },
87         { 0x04, 0x00 },
88         { 0x05, 0x00 },
89         { 0x04, 0x01 },
90         { 0x05, 0x07 },
91         { 0x04, 0x02 },
92         { 0x05, 0x0f },
93         { 0x04, 0x03 },
94         { 0x05, 0xa0 },
95         { 0x04, 0x09 },
96         { 0x05, 0x00 },
97         { 0x04, 0x0a },
98         { 0x05, 0xff },
99         { 0x04, 0x27 },
100         { 0x05, 0x64 },
101         { 0x04, 0x28 },
102         { 0x05, 0x00 },
103         { 0x04, 0x1e },
104         { 0x05, 0xff },
105         { 0x04, 0x29 },
106         { 0x05, 0x0a },
107         { 0x04, 0x32 },
108         { 0x05, 0x0a },
109         { 0x04, 0x14 },
110         { 0x05, 0x02 },
111         { 0x04, 0x04 },
112         { 0x05, 0x00 },
113         { 0x04, 0x05 },
114         { 0x05, 0x22 },
115         { 0x04, 0x06 },
116         { 0x05, 0x0e },
117         { 0x04, 0x07 },
118         { 0x05, 0xd8 },
119         { 0x04, 0x12 },
120         { 0x05, 0x00 },
121         { 0x04, 0x13 },
122         { 0x05, 0xff },
123         { 0x52, 0x01 },
124         { 0x50, 0xa7 },
125         { 0x51, 0x00 },
126         { 0x50, 0xa8 },
127         { 0x51, 0xff },
128         { 0x50, 0xa9 },
129         { 0x51, 0xff },
130         { 0x50, 0xaa },
131         { 0x51, 0x00 },
132         { 0x50, 0xab },
133         { 0x51, 0xff },
134         { 0x50, 0xac },
135         { 0x51, 0xff },
136         { 0x50, 0xad },
137         { 0x51, 0x00 },
138         { 0x50, 0xae },
139         { 0x51, 0xff },
140         { 0x50, 0xaf },
141         { 0x51, 0xff },
142         { 0x5e, 0x07 },
143         { 0x50, 0xdc },
144         { 0x51, 0x01 },
145         { 0x50, 0xdd },
146         { 0x51, 0xf4 },
147         { 0x50, 0xde },
148         { 0x51, 0x01 },
149         { 0x50, 0xdf },
150         { 0x51, 0xf4 },
151         { 0x50, 0xe0 },
152         { 0x51, 0x01 },
153         { 0x50, 0xe1 },
154         { 0x51, 0xf4 },
155         { 0x50, 0xb0 },
156         { 0x51, 0x07 },
157         { 0x50, 0xb2 },
158         { 0x51, 0xff },
159         { 0x50, 0xb3 },
160         { 0x51, 0xff },
161         { 0x50, 0xb4 },
162         { 0x51, 0xff },
163         { 0x50, 0xb5 },
164         { 0x51, 0xff },
165         { 0x50, 0xb6 },
166         { 0x51, 0xff },
167         { 0x50, 0xb7 },
168         { 0x51, 0xff },
169         { 0x50, 0x50 },
170         { 0x51, 0x02 },
171         { 0x50, 0x51 },
172         { 0x51, 0x04 },
173         { 0x45, 0x04 },
174         { 0x48, 0x04 },
175         { 0x50, 0xd5 },
176         { 0x51, 0x01 },         /* Serial */
177         { 0x50, 0xd6 },
178         { 0x51, 0x1f },
179         { 0x50, 0xd2 },
180         { 0x51, 0x03 },
181         { 0x50, 0xd7 },
182         { 0x51, 0x3f },
183         { 0x1c, 0x01 },
184         { 0x28, 0x06 },
185         { 0x29, 0x00 },
186         { 0x2a, 0x00 },
187         { 0x2b, 0x03 },
188         { 0x28, 0x07 },
189         { 0x29, 0x00 },
190         { 0x2a, 0x00 },
191         { 0x2b, 0x0d },
192         { 0x28, 0x08 },
193         { 0x29, 0x00 },
194         { 0x2a, 0x00 },
195         { 0x2b, 0x02 },
196         { 0x28, 0x09 },
197         { 0x29, 0x00 },
198         { 0x2a, 0x00 },
199         { 0x2b, 0x01 },
200         { 0x28, 0x0a },
201         { 0x29, 0x00 },
202         { 0x2a, 0x00 },
203         { 0x2b, 0x21 },
204         { 0x28, 0x0b },
205         { 0x29, 0x00 },
206         { 0x2a, 0x00 },
207         { 0x2b, 0x29 },
208         { 0x28, 0x0c },
209         { 0x29, 0x00 },
210         { 0x2a, 0x00 },
211         { 0x2b, 0x16 },
212         { 0x28, 0x0d },
213         { 0x29, 0x00 },
214         { 0x2a, 0x00 },
215         { 0x2b, 0x31 },
216         { 0x28, 0x0e },
217         { 0x29, 0x00 },
218         { 0x2a, 0x00 },
219         { 0x2b, 0x0e },
220         { 0x28, 0x0f },
221         { 0x29, 0x00 },
222         { 0x2a, 0x00 },
223         { 0x2b, 0x4e },
224         { 0x28, 0x10 },
225         { 0x29, 0x00 },
226         { 0x2a, 0x00 },
227         { 0x2b, 0x46 },
228         { 0x28, 0x11 },
229         { 0x29, 0x00 },
230         { 0x2a, 0x00 },
231         { 0x2b, 0x0f },
232         { 0x28, 0x12 },
233         { 0x29, 0x00 },
234         { 0x2a, 0x00 },
235         { 0x2b, 0x56 },
236         { 0x28, 0x13 },
237         { 0x29, 0x00 },
238         { 0x2a, 0x00 },
239         { 0x2b, 0x35 },
240         { 0x28, 0x14 },
241         { 0x29, 0x00 },
242         { 0x2a, 0x01 },
243         { 0x2b, 0xbe },
244         { 0x28, 0x15 },
245         { 0x29, 0x00 },
246         { 0x2a, 0x01 },
247         { 0x2b, 0x84 },
248         { 0x28, 0x16 },
249         { 0x29, 0x00 },
250         { 0x2a, 0x03 },
251         { 0x2b, 0xee },
252         { 0x28, 0x17 },
253         { 0x29, 0x00 },
254         { 0x2a, 0x00 },
255         { 0x2b, 0x98 },
256         { 0x28, 0x18 },
257         { 0x29, 0x00 },
258         { 0x2a, 0x00 },
259         { 0x2b, 0x9f },
260         { 0x28, 0x19 },
261         { 0x29, 0x00 },
262         { 0x2a, 0x07 },
263         { 0x2b, 0xb2 },
264         { 0x28, 0x1a },
265         { 0x29, 0x00 },
266         { 0x2a, 0x06 },
267         { 0x2b, 0xc2 },
268         { 0x28, 0x1b },
269         { 0x29, 0x00 },
270         { 0x2a, 0x07 },
271         { 0x2b, 0x4a },
272         { 0x28, 0x1c },
273         { 0x29, 0x00 },
274         { 0x2a, 0x01 },
275         { 0x2b, 0xbc },
276         { 0x28, 0x1d },
277         { 0x29, 0x00 },
278         { 0x2a, 0x04 },
279         { 0x2b, 0xba },
280         { 0x28, 0x1e },
281         { 0x29, 0x00 },
282         { 0x2a, 0x06 },
283         { 0x2b, 0x14 },
284         { 0x50, 0x1e },
285         { 0x51, 0x5d },
286         { 0x50, 0x22 },
287         { 0x51, 0x00 },
288         { 0x50, 0x23 },
289         { 0x51, 0xc8 },
290         { 0x50, 0x24 },
291         { 0x51, 0x00 },
292         { 0x50, 0x25 },
293         { 0x51, 0xf0 },
294         { 0x50, 0x26 },
295         { 0x51, 0x00 },
296         { 0x50, 0x27 },
297         { 0x51, 0xc3 },
298         { 0x50, 0x39 },
299         { 0x51, 0x02 },
300         { 0x50, 0xd5 },
301         { 0x51, 0x01 },
302         { 0xd0, 0x00 },
303 };
304
305 static struct regdata mb86a20s_reset_reception[] = {
306         { 0x70, 0xf0 },
307         { 0x70, 0xff },
308         { 0x08, 0x01 },
309         { 0x08, 0x00 },
310 };
311
312 static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
313                              u8 i2c_addr, int reg, int data)
314 {
315         u8 buf[] = { reg, data };
316         struct i2c_msg msg = {
317                 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
318         };
319         int rc;
320
321         rc = i2c_transfer(state->i2c, &msg, 1);
322         if (rc != 1) {
323                 printk("%s: writereg error (rc == %i, reg == 0x%02x,"
324                          " data == 0x%02x)\n", __func__, rc, reg, data);
325                 return rc;
326         }
327
328         return 0;
329 }
330
331 static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
332                                      u8 i2c_addr, struct regdata *rd, int size)
333 {
334         int i, rc;
335
336         for (i = 0; i < size; i++) {
337                 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
338                                            rd[i].data);
339                 if (rc < 0)
340                         return rc;
341         }
342         return 0;
343 }
344
345 static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
346                                 u8 i2c_addr, u8 reg)
347 {
348         u8 val;
349         int rc;
350         struct i2c_msg msg[] = {
351                 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
352                 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
353         };
354
355         rc = i2c_transfer(state->i2c, msg, 2);
356
357         if (rc != 2) {
358                 rc("%s: reg=0x%x (error=%d)\n", __func__, reg, rc);
359                 return rc;
360         }
361
362         return val;
363 }
364
365 #define mb86a20s_readreg(state, reg) \
366         mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
367 #define mb86a20s_writereg(state, reg, val) \
368         mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
369 #define mb86a20s_writeregdata(state, regdata) \
370         mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
371         regdata, ARRAY_SIZE(regdata))
372
373 static int mb86a20s_initfe(struct dvb_frontend *fe)
374 {
375         struct mb86a20s_state *state = fe->demodulator_priv;
376         int rc;
377         u8  regD5 = 1;
378
379         dprintk("\n");
380
381         if (fe->ops.i2c_gate_ctrl)
382                 fe->ops.i2c_gate_ctrl(fe, 0);
383
384         /* Initialize the frontend */
385         rc = mb86a20s_writeregdata(state, mb86a20s_init);
386         if (rc < 0)
387                 goto err;
388
389         if (!state->config->is_serial) {
390                 regD5 &= ~1;
391
392                 rc = mb86a20s_writereg(state, 0x50, 0xd5);
393                 if (rc < 0)
394                         goto err;
395                 rc = mb86a20s_writereg(state, 0x51, regD5);
396                 if (rc < 0)
397                         goto err;
398         }
399
400         if (fe->ops.i2c_gate_ctrl)
401                 fe->ops.i2c_gate_ctrl(fe, 1);
402
403 err:
404         if (rc < 0) {
405                 state->need_init = true;
406                 printk(KERN_INFO "mb86a20s: Init failed. Will try again later\n");
407         } else {
408                 state->need_init = false;
409                 dprintk("Initialization succeeded.\n");
410         }
411         return rc;
412 }
413
414 static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
415 {
416         struct mb86a20s_state *state = fe->demodulator_priv;
417         unsigned rf_max, rf_min, rf;
418         u8       val;
419
420         dprintk("\n");
421
422         if (fe->ops.i2c_gate_ctrl)
423                 fe->ops.i2c_gate_ctrl(fe, 0);
424
425         /* Does a binary search to get RF strength */
426         rf_max = 0xfff;
427         rf_min = 0;
428         do {
429                 rf = (rf_max + rf_min) / 2;
430                 mb86a20s_writereg(state, 0x04, 0x1f);
431                 mb86a20s_writereg(state, 0x05, rf >> 8);
432                 mb86a20s_writereg(state, 0x04, 0x20);
433                 mb86a20s_writereg(state, 0x04, rf);
434
435                 val = mb86a20s_readreg(state, 0x02);
436                 if (val & 0x08)
437                         rf_min = (rf_max + rf_min) / 2;
438                 else
439                         rf_max = (rf_max + rf_min) / 2;
440                 if (rf_max - rf_min < 4) {
441                         *strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
442                         break;
443                 }
444         } while (1);
445
446         dprintk("signal strength = %d\n", *strength);
447
448         if (fe->ops.i2c_gate_ctrl)
449                 fe->ops.i2c_gate_ctrl(fe, 1);
450
451         return 0;
452 }
453
454 static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
455 {
456         struct mb86a20s_state *state = fe->demodulator_priv;
457         u8 val;
458
459         dprintk("\n");
460         *status = 0;
461
462         if (fe->ops.i2c_gate_ctrl)
463                 fe->ops.i2c_gate_ctrl(fe, 0);
464         val = mb86a20s_readreg(state, 0x0a) & 0xf;
465         if (fe->ops.i2c_gate_ctrl)
466                 fe->ops.i2c_gate_ctrl(fe, 1);
467
468         if (val >= 2)
469                 *status |= FE_HAS_SIGNAL;
470
471         if (val >= 4)
472                 *status |= FE_HAS_CARRIER;
473
474         if (val >= 5)
475                 *status |= FE_HAS_VITERBI;
476
477         if (val >= 7)
478                 *status |= FE_HAS_SYNC;
479
480         if (val >= 8)                           /* Maybe 9? */
481                 *status |= FE_HAS_LOCK;
482
483         dprintk("val = %d, status = 0x%02x\n", val, *status);
484
485         return 0;
486 }
487
488 static int mb86a20s_set_frontend(struct dvb_frontend *fe,
489         struct dvb_frontend_parameters *p)
490 {
491         struct mb86a20s_state *state = fe->demodulator_priv;
492         int rc;
493
494         dprintk("\n");
495
496         if (fe->ops.i2c_gate_ctrl)
497                 fe->ops.i2c_gate_ctrl(fe, 1);
498         dprintk("Calling tuner set parameters\n");
499         fe->ops.tuner_ops.set_params(fe, p);
500
501         /*
502          * Make it more reliable: if, for some reason, the initial
503          * device initialization doesn't happen, initialize it when
504          * a SBTVD parameters are adjusted.
505          *
506          * Unfortunately, due to a hard to track bug at tda829x/tda18271,
507          * the agc callback logic is not called during DVB attach time,
508          * causing mb86a20s to not be initialized with Kworld SBTVD.
509          * So, this hack is needed, in order to make Kworld SBTVD to work.
510          */
511         if (state->need_init)
512                 mb86a20s_initfe(fe);
513
514         if (fe->ops.i2c_gate_ctrl)
515                 fe->ops.i2c_gate_ctrl(fe, 0);
516         rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
517         if (fe->ops.i2c_gate_ctrl)
518                 fe->ops.i2c_gate_ctrl(fe, 1);
519
520         return rc;
521 }
522
523 static int mb86a20s_get_frontend(struct dvb_frontend *fe,
524         struct dvb_frontend_parameters *p)
525 {
526
527         /* FIXME: For now, it does nothing */
528
529         fe->dtv_property_cache.bandwidth_hz = 6000000;
530         fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
531         fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
532         fe->dtv_property_cache.isdbt_partial_reception = 0;
533
534         return 0;
535 }
536
537 static int mb86a20s_tune(struct dvb_frontend *fe,
538                         struct dvb_frontend_parameters *params,
539                         unsigned int mode_flags,
540                         unsigned int *delay,
541                         fe_status_t *status)
542 {
543         int rc = 0;
544
545         dprintk("\n");
546
547         if (params != NULL)
548                 rc = mb86a20s_set_frontend(fe, params);
549
550         if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
551                 mb86a20s_read_status(fe, status);
552
553         return rc;
554 }
555
556 static void mb86a20s_release(struct dvb_frontend *fe)
557 {
558         struct mb86a20s_state *state = fe->demodulator_priv;
559
560         dprintk("\n");
561
562         kfree(state);
563 }
564
565 static struct dvb_frontend_ops mb86a20s_ops;
566
567 struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
568                                     struct i2c_adapter *i2c)
569 {
570         u8      rev;
571
572         /* allocate memory for the internal state */
573         struct mb86a20s_state *state =
574                 kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
575
576         dprintk("\n");
577         if (state == NULL) {
578                 rc("Unable to kzalloc\n");
579                 goto error;
580         }
581
582         /* setup the state */
583         state->config = config;
584         state->i2c = i2c;
585
586         /* create dvb_frontend */
587         memcpy(&state->frontend.ops, &mb86a20s_ops,
588                 sizeof(struct dvb_frontend_ops));
589         state->frontend.demodulator_priv = state;
590
591         /* Check if it is a mb86a20s frontend */
592         rev = mb86a20s_readreg(state, 0);
593
594         if (rev == 0x13) {
595                 printk(KERN_INFO "Detected a Fujitsu mb86a20s frontend\n");
596         } else {
597                 printk(KERN_ERR "Frontend revision %d is unknown - aborting.\n",
598                        rev);
599                 goto error;
600         }
601
602         return &state->frontend;
603
604 error:
605         kfree(state);
606         return NULL;
607 }
608 EXPORT_SYMBOL(mb86a20s_attach);
609
610 static struct dvb_frontend_ops mb86a20s_ops = {
611         /* Use dib8000 values per default */
612         .info = {
613                 .name = "Fujitsu mb86A20s",
614                 .type = FE_OFDM,
615                 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
616                         FE_CAN_FEC_1_2  | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
617                         FE_CAN_FEC_5_6  | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
618                         FE_CAN_QPSK     | FE_CAN_QAM_16  | FE_CAN_QAM_64 |
619                         FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
620                         FE_CAN_GUARD_INTERVAL_AUTO    | FE_CAN_HIERARCHY_AUTO,
621                 /* Actually, those values depend on the used tuner */
622                 .frequency_min = 45000000,
623                 .frequency_max = 864000000,
624                 .frequency_stepsize = 62500,
625         },
626
627         .release = mb86a20s_release,
628
629         .init = mb86a20s_initfe,
630         .set_frontend = mb86a20s_set_frontend,
631         .get_frontend = mb86a20s_get_frontend,
632         .read_status = mb86a20s_read_status,
633         .read_signal_strength = mb86a20s_read_signal_strength,
634         .tune = mb86a20s_tune,
635 };
636
637 MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
638 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
639 MODULE_LICENSE("GPL");