include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[sfrench/cifs-2.6.git] / drivers / media / dvb / frontends / au8522_dig.c
1 /*
2     Auvitek AU8522 QAM/8VSB demodulator driver
3
4     Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
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     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
26 #include <linux/delay.h>
27 #include "dvb_frontend.h"
28 #include "au8522.h"
29 #include "au8522_priv.h"
30
31 static int debug;
32
33 /* Despite the name "hybrid_tuner", the framework works just as well for
34    hybrid demodulators as well... */
35 static LIST_HEAD(hybrid_tuner_instance_list);
36 static DEFINE_MUTEX(au8522_list_mutex);
37
38 #define dprintk(arg...)\
39         do { if (debug)\
40                 printk(arg);\
41         } while (0)
42
43 /* 16 bit registers, 8 bit values */
44 int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
45 {
46         int ret;
47         u8 buf[] = { (reg >> 8) | 0x80, reg & 0xff, data };
48
49         struct i2c_msg msg = { .addr = state->config->demod_address,
50                                .flags = 0, .buf = buf, .len = 3 };
51
52         ret = i2c_transfer(state->i2c, &msg, 1);
53
54         if (ret != 1)
55                 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
56                        "ret == %i)\n", __func__, reg, data, ret);
57
58         return (ret != 1) ? -1 : 0;
59 }
60
61 u8 au8522_readreg(struct au8522_state *state, u16 reg)
62 {
63         int ret;
64         u8 b0[] = { (reg >> 8) | 0x40, reg & 0xff };
65         u8 b1[] = { 0 };
66
67         struct i2c_msg msg[] = {
68                 { .addr = state->config->demod_address, .flags = 0,
69                   .buf = b0, .len = 2 },
70                 { .addr = state->config->demod_address, .flags = I2C_M_RD,
71                   .buf = b1, .len = 1 } };
72
73         ret = i2c_transfer(state->i2c, msg, 2);
74
75         if (ret != 2)
76                 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
77                        __func__, ret);
78         return b1[0];
79 }
80
81 static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
82 {
83         struct au8522_state *state = fe->demodulator_priv;
84
85         dprintk("%s(%d)\n", __func__, enable);
86
87         if (enable)
88                 return au8522_writereg(state, 0x106, 1);
89         else
90                 return au8522_writereg(state, 0x106, 0);
91 }
92
93 struct mse2snr_tab {
94         u16 val;
95         u16 data;
96 };
97
98 /* VSB SNR lookup table */
99 static struct mse2snr_tab vsb_mse2snr_tab[] = {
100         {   0, 270 },
101         {   2, 250 },
102         {   3, 240 },
103         {   5, 230 },
104         {   7, 220 },
105         {   9, 210 },
106         {  12, 200 },
107         {  13, 195 },
108         {  15, 190 },
109         {  17, 185 },
110         {  19, 180 },
111         {  21, 175 },
112         {  24, 170 },
113         {  27, 165 },
114         {  31, 160 },
115         {  32, 158 },
116         {  33, 156 },
117         {  36, 152 },
118         {  37, 150 },
119         {  39, 148 },
120         {  40, 146 },
121         {  41, 144 },
122         {  43, 142 },
123         {  44, 140 },
124         {  48, 135 },
125         {  50, 130 },
126         {  43, 142 },
127         {  53, 125 },
128         {  56, 120 },
129         { 256, 115 },
130 };
131
132 /* QAM64 SNR lookup table */
133 static struct mse2snr_tab qam64_mse2snr_tab[] = {
134         {  15,   0 },
135         {  16, 290 },
136         {  17, 288 },
137         {  18, 286 },
138         {  19, 284 },
139         {  20, 282 },
140         {  21, 281 },
141         {  22, 279 },
142         {  23, 277 },
143         {  24, 275 },
144         {  25, 273 },
145         {  26, 271 },
146         {  27, 269 },
147         {  28, 268 },
148         {  29, 266 },
149         {  30, 264 },
150         {  31, 262 },
151         {  32, 260 },
152         {  33, 259 },
153         {  34, 258 },
154         {  35, 256 },
155         {  36, 255 },
156         {  37, 254 },
157         {  38, 252 },
158         {  39, 251 },
159         {  40, 250 },
160         {  41, 249 },
161         {  42, 248 },
162         {  43, 246 },
163         {  44, 245 },
164         {  45, 244 },
165         {  46, 242 },
166         {  47, 241 },
167         {  48, 240 },
168         {  50, 239 },
169         {  51, 238 },
170         {  53, 237 },
171         {  54, 236 },
172         {  56, 235 },
173         {  57, 234 },
174         {  59, 233 },
175         {  60, 232 },
176         {  62, 231 },
177         {  63, 230 },
178         {  65, 229 },
179         {  67, 228 },
180         {  68, 227 },
181         {  70, 226 },
182         {  71, 225 },
183         {  73, 224 },
184         {  74, 223 },
185         {  76, 222 },
186         {  78, 221 },
187         {  80, 220 },
188         {  82, 219 },
189         {  85, 218 },
190         {  88, 217 },
191         {  90, 216 },
192         {  92, 215 },
193         {  93, 214 },
194         {  94, 212 },
195         {  95, 211 },
196         {  97, 210 },
197         {  99, 209 },
198         { 101, 208 },
199         { 102, 207 },
200         { 104, 206 },
201         { 107, 205 },
202         { 111, 204 },
203         { 114, 203 },
204         { 118, 202 },
205         { 122, 201 },
206         { 125, 200 },
207         { 128, 199 },
208         { 130, 198 },
209         { 132, 197 },
210         { 256, 190 },
211 };
212
213 /* QAM256 SNR lookup table */
214 static struct mse2snr_tab qam256_mse2snr_tab[] = {
215         {  16,   0 },
216         {  17, 400 },
217         {  18, 398 },
218         {  19, 396 },
219         {  20, 394 },
220         {  21, 392 },
221         {  22, 390 },
222         {  23, 388 },
223         {  24, 386 },
224         {  25, 384 },
225         {  26, 382 },
226         {  27, 380 },
227         {  28, 379 },
228         {  29, 378 },
229         {  30, 377 },
230         {  31, 376 },
231         {  32, 375 },
232         {  33, 374 },
233         {  34, 373 },
234         {  35, 372 },
235         {  36, 371 },
236         {  37, 370 },
237         {  38, 362 },
238         {  39, 354 },
239         {  40, 346 },
240         {  41, 338 },
241         {  42, 330 },
242         {  43, 328 },
243         {  44, 326 },
244         {  45, 324 },
245         {  46, 322 },
246         {  47, 320 },
247         {  48, 319 },
248         {  49, 318 },
249         {  50, 317 },
250         {  51, 316 },
251         {  52, 315 },
252         {  53, 314 },
253         {  54, 313 },
254         {  55, 312 },
255         {  56, 311 },
256         {  57, 310 },
257         {  58, 308 },
258         {  59, 306 },
259         {  60, 304 },
260         {  61, 302 },
261         {  62, 300 },
262         {  63, 298 },
263         {  65, 295 },
264         {  68, 294 },
265         {  70, 293 },
266         {  73, 292 },
267         {  76, 291 },
268         {  78, 290 },
269         {  79, 289 },
270         {  81, 288 },
271         {  82, 287 },
272         {  83, 286 },
273         {  84, 285 },
274         {  85, 284 },
275         {  86, 283 },
276         {  88, 282 },
277         {  89, 281 },
278         { 256, 280 },
279 };
280
281 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
282                                  u16 *snr)
283 {
284         int i, ret = -EINVAL;
285         dprintk("%s()\n", __func__);
286
287         for (i = 0; i < sz; i++) {
288                 if (mse < tab[i].val) {
289                         *snr = tab[i].data;
290                         ret = 0;
291                         break;
292                 }
293         }
294         dprintk("%s() snr=%d\n", __func__, *snr);
295         return ret;
296 }
297
298 static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
299 {
300         struct au8522_state *state = fe->demodulator_priv;
301         u8 r0b5, r0b6, r0b7;
302         char *ifmhz;
303
304         switch (if_freq) {
305         case AU8522_IF_3_25MHZ:
306                 ifmhz = "3.25";
307                 r0b5 = 0x00;
308                 r0b6 = 0x3d;
309                 r0b7 = 0xa0;
310                 break;
311         case AU8522_IF_4MHZ:
312                 ifmhz = "4.00";
313                 r0b5 = 0x00;
314                 r0b6 = 0x4b;
315                 r0b7 = 0xd9;
316                 break;
317         case AU8522_IF_6MHZ:
318                 ifmhz = "6.00";
319                 r0b5 = 0xfb;
320                 r0b6 = 0x8e;
321                 r0b7 = 0x39;
322                 break;
323         default:
324                 dprintk("%s() IF Frequency not supported\n", __func__);
325                 return -EINVAL;
326         }
327         dprintk("%s() %s MHz\n", __func__, ifmhz);
328         au8522_writereg(state, 0x80b5, r0b5);
329         au8522_writereg(state, 0x80b6, r0b6);
330         au8522_writereg(state, 0x80b7, r0b7);
331
332         return 0;
333 }
334
335 /* VSB Modulation table */
336 static struct {
337         u16 reg;
338         u16 data;
339 } VSB_mod_tab[] = {
340         { 0x8090, 0x84 },
341         { 0x4092, 0x11 },
342         { 0x2005, 0x00 },
343         { 0x8091, 0x80 },
344         { 0x80a3, 0x0c },
345         { 0x80a4, 0xe8 },
346         { 0x8081, 0xc4 },
347         { 0x80a5, 0x40 },
348         { 0x80a7, 0x40 },
349         { 0x80a6, 0x67 },
350         { 0x8262, 0x20 },
351         { 0x821c, 0x30 },
352         { 0x80d8, 0x1a },
353         { 0x8227, 0xa0 },
354         { 0x8121, 0xff },
355         { 0x80a8, 0xf0 },
356         { 0x80a9, 0x05 },
357         { 0x80aa, 0x77 },
358         { 0x80ab, 0xf0 },
359         { 0x80ac, 0x05 },
360         { 0x80ad, 0x77 },
361         { 0x80ae, 0x41 },
362         { 0x80af, 0x66 },
363         { 0x821b, 0xcc },
364         { 0x821d, 0x80 },
365         { 0x80a4, 0xe8 },
366         { 0x8231, 0x13 },
367 };
368
369 /* QAM64 Modulation table */
370 static struct {
371         u16 reg;
372         u16 data;
373 } QAM64_mod_tab[] = {
374         { 0x00a3, 0x09 },
375         { 0x00a4, 0x00 },
376         { 0x0081, 0xc4 },
377         { 0x00a5, 0x40 },
378         { 0x00aa, 0x77 },
379         { 0x00ad, 0x77 },
380         { 0x00a6, 0x67 },
381         { 0x0262, 0x20 },
382         { 0x021c, 0x30 },
383         { 0x00b8, 0x3e },
384         { 0x00b9, 0xf0 },
385         { 0x00ba, 0x01 },
386         { 0x00bb, 0x18 },
387         { 0x00bc, 0x50 },
388         { 0x00bd, 0x00 },
389         { 0x00be, 0xea },
390         { 0x00bf, 0xef },
391         { 0x00c0, 0xfc },
392         { 0x00c1, 0xbd },
393         { 0x00c2, 0x1f },
394         { 0x00c3, 0xfc },
395         { 0x00c4, 0xdd },
396         { 0x00c5, 0xaf },
397         { 0x00c6, 0x00 },
398         { 0x00c7, 0x38 },
399         { 0x00c8, 0x30 },
400         { 0x00c9, 0x05 },
401         { 0x00ca, 0x4a },
402         { 0x00cb, 0xd0 },
403         { 0x00cc, 0x01 },
404         { 0x00cd, 0xd9 },
405         { 0x00ce, 0x6f },
406         { 0x00cf, 0xf9 },
407         { 0x00d0, 0x70 },
408         { 0x00d1, 0xdf },
409         { 0x00d2, 0xf7 },
410         { 0x00d3, 0xc2 },
411         { 0x00d4, 0xdf },
412         { 0x00d5, 0x02 },
413         { 0x00d6, 0x9a },
414         { 0x00d7, 0xd0 },
415         { 0x0250, 0x0d },
416         { 0x0251, 0xcd },
417         { 0x0252, 0xe0 },
418         { 0x0253, 0x05 },
419         { 0x0254, 0xa7 },
420         { 0x0255, 0xff },
421         { 0x0256, 0xed },
422         { 0x0257, 0x5b },
423         { 0x0258, 0xae },
424         { 0x0259, 0xe6 },
425         { 0x025a, 0x3d },
426         { 0x025b, 0x0f },
427         { 0x025c, 0x0d },
428         { 0x025d, 0xea },
429         { 0x025e, 0xf2 },
430         { 0x025f, 0x51 },
431         { 0x0260, 0xf5 },
432         { 0x0261, 0x06 },
433         { 0x021a, 0x00 },
434         { 0x0546, 0x40 },
435         { 0x0210, 0xc7 },
436         { 0x0211, 0xaa },
437         { 0x0212, 0xab },
438         { 0x0213, 0x02 },
439         { 0x0502, 0x00 },
440         { 0x0121, 0x04 },
441         { 0x0122, 0x04 },
442         { 0x052e, 0x10 },
443         { 0x00a4, 0xca },
444         { 0x00a7, 0x40 },
445         { 0x0526, 0x01 },
446 };
447
448 /* QAM256 Modulation table */
449 static struct {
450         u16 reg;
451         u16 data;
452 } QAM256_mod_tab[] = {
453         { 0x80a3, 0x09 },
454         { 0x80a4, 0x00 },
455         { 0x8081, 0xc4 },
456         { 0x80a5, 0x40 },
457         { 0x80aa, 0x77 },
458         { 0x80ad, 0x77 },
459         { 0x80a6, 0x67 },
460         { 0x8262, 0x20 },
461         { 0x821c, 0x30 },
462         { 0x80b8, 0x3e },
463         { 0x80b9, 0xf0 },
464         { 0x80ba, 0x01 },
465         { 0x80bb, 0x18 },
466         { 0x80bc, 0x50 },
467         { 0x80bd, 0x00 },
468         { 0x80be, 0xea },
469         { 0x80bf, 0xef },
470         { 0x80c0, 0xfc },
471         { 0x80c1, 0xbd },
472         { 0x80c2, 0x1f },
473         { 0x80c3, 0xfc },
474         { 0x80c4, 0xdd },
475         { 0x80c5, 0xaf },
476         { 0x80c6, 0x00 },
477         { 0x80c7, 0x38 },
478         { 0x80c8, 0x30 },
479         { 0x80c9, 0x05 },
480         { 0x80ca, 0x4a },
481         { 0x80cb, 0xd0 },
482         { 0x80cc, 0x01 },
483         { 0x80cd, 0xd9 },
484         { 0x80ce, 0x6f },
485         { 0x80cf, 0xf9 },
486         { 0x80d0, 0x70 },
487         { 0x80d1, 0xdf },
488         { 0x80d2, 0xf7 },
489         { 0x80d3, 0xc2 },
490         { 0x80d4, 0xdf },
491         { 0x80d5, 0x02 },
492         { 0x80d6, 0x9a },
493         { 0x80d7, 0xd0 },
494         { 0x8250, 0x0d },
495         { 0x8251, 0xcd },
496         { 0x8252, 0xe0 },
497         { 0x8253, 0x05 },
498         { 0x8254, 0xa7 },
499         { 0x8255, 0xff },
500         { 0x8256, 0xed },
501         { 0x8257, 0x5b },
502         { 0x8258, 0xae },
503         { 0x8259, 0xe6 },
504         { 0x825a, 0x3d },
505         { 0x825b, 0x0f },
506         { 0x825c, 0x0d },
507         { 0x825d, 0xea },
508         { 0x825e, 0xf2 },
509         { 0x825f, 0x51 },
510         { 0x8260, 0xf5 },
511         { 0x8261, 0x06 },
512         { 0x821a, 0x00 },
513         { 0x8546, 0x40 },
514         { 0x8210, 0x26 },
515         { 0x8211, 0xf6 },
516         { 0x8212, 0x84 },
517         { 0x8213, 0x02 },
518         { 0x8502, 0x01 },
519         { 0x8121, 0x04 },
520         { 0x8122, 0x04 },
521         { 0x852e, 0x10 },
522         { 0x80a4, 0xca },
523         { 0x80a7, 0x40 },
524         { 0x8526, 0x01 },
525 };
526
527 static int au8522_enable_modulation(struct dvb_frontend *fe,
528                                     fe_modulation_t m)
529 {
530         struct au8522_state *state = fe->demodulator_priv;
531         int i;
532
533         dprintk("%s(0x%08x)\n", __func__, m);
534
535         switch (m) {
536         case VSB_8:
537                 dprintk("%s() VSB_8\n", __func__);
538                 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
539                         au8522_writereg(state,
540                                 VSB_mod_tab[i].reg,
541                                 VSB_mod_tab[i].data);
542                 au8522_set_if(fe, state->config->vsb_if);
543                 break;
544         case QAM_64:
545                 dprintk("%s() QAM 64\n", __func__);
546                 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
547                         au8522_writereg(state,
548                                 QAM64_mod_tab[i].reg,
549                                 QAM64_mod_tab[i].data);
550                 au8522_set_if(fe, state->config->qam_if);
551                 break;
552         case QAM_256:
553                 dprintk("%s() QAM 256\n", __func__);
554                 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
555                         au8522_writereg(state,
556                                 QAM256_mod_tab[i].reg,
557                                 QAM256_mod_tab[i].data);
558                 au8522_set_if(fe, state->config->qam_if);
559                 break;
560         default:
561                 dprintk("%s() Invalid modulation\n", __func__);
562                 return -EINVAL;
563         }
564
565         state->current_modulation = m;
566
567         return 0;
568 }
569
570 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
571 static int au8522_set_frontend(struct dvb_frontend *fe,
572                                struct dvb_frontend_parameters *p)
573 {
574         struct au8522_state *state = fe->demodulator_priv;
575         int ret = -EINVAL;
576
577         dprintk("%s(frequency=%d)\n", __func__, p->frequency);
578
579         if ((state->current_frequency == p->frequency) &&
580             (state->current_modulation == p->u.vsb.modulation))
581                 return 0;
582
583         au8522_enable_modulation(fe, p->u.vsb.modulation);
584
585         /* Allow the demod to settle */
586         msleep(100);
587
588         if (fe->ops.tuner_ops.set_params) {
589                 if (fe->ops.i2c_gate_ctrl)
590                         fe->ops.i2c_gate_ctrl(fe, 1);
591                 ret = fe->ops.tuner_ops.set_params(fe, p);
592                 if (fe->ops.i2c_gate_ctrl)
593                         fe->ops.i2c_gate_ctrl(fe, 0);
594         }
595
596         if (ret < 0)
597                 return ret;
598
599         state->current_frequency = p->frequency;
600
601         return 0;
602 }
603
604 /* Reset the demod hardware and reset all of the configuration registers
605    to a default state. */
606 int au8522_init(struct dvb_frontend *fe)
607 {
608         struct au8522_state *state = fe->demodulator_priv;
609         dprintk("%s()\n", __func__);
610
611         au8522_writereg(state, 0xa4, 1 << 5);
612
613         au8522_i2c_gate_ctrl(fe, 1);
614
615         return 0;
616 }
617
618 static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
619 {
620         struct au8522_led_config *led_config = state->config->led_cfg;
621         u8 val;
622
623         /* bail out if we cant control an LED */
624         if (!led_config || !led_config->gpio_output ||
625             !led_config->gpio_output_enable || !led_config->gpio_output_disable)
626                 return 0;
627
628         val = au8522_readreg(state, 0x4000 |
629                              (led_config->gpio_output & ~0xc000));
630         if (onoff) {
631                 /* enable GPIO output */
632                 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
633                 val |=  (led_config->gpio_output_enable & 0xff);
634         } else {
635                 /* disable GPIO output */
636                 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
637                 val |=  (led_config->gpio_output_disable & 0xff);
638         }
639         return au8522_writereg(state, 0x8000 |
640                                (led_config->gpio_output & ~0xc000), val);
641 }
642
643 /* led = 0 | off
644  * led = 1 | signal ok
645  * led = 2 | signal strong
646  * led < 0 | only light led if leds are currently off
647  */
648 static int au8522_led_ctrl(struct au8522_state *state, int led)
649 {
650         struct au8522_led_config *led_config = state->config->led_cfg;
651         int i, ret = 0;
652
653         /* bail out if we cant control an LED */
654         if (!led_config || !led_config->gpio_leds ||
655             !led_config->num_led_states || !led_config->led_states)
656                 return 0;
657
658         if (led < 0) {
659                 /* if LED is already lit, then leave it as-is */
660                 if (state->led_state)
661                         return 0;
662                 else
663                         led *= -1;
664         }
665
666         /* toggle LED if changing state */
667         if (state->led_state != led) {
668                 u8 val;
669
670                 dprintk("%s: %d\n", __func__, led);
671
672                 au8522_led_gpio_enable(state, 1);
673
674                 val = au8522_readreg(state, 0x4000 |
675                                      (led_config->gpio_leds & ~0xc000));
676
677                 /* start with all leds off */
678                 for (i = 0; i < led_config->num_led_states; i++)
679                         val &= ~led_config->led_states[i];
680
681                 /* set selected LED state */
682                 if (led < led_config->num_led_states)
683                         val |= led_config->led_states[led];
684                 else if (led_config->num_led_states)
685                         val |=
686                         led_config->led_states[led_config->num_led_states - 1];
687
688                 ret = au8522_writereg(state, 0x8000 |
689                                       (led_config->gpio_leds & ~0xc000), val);
690                 if (ret < 0)
691                         return ret;
692
693                 state->led_state = led;
694
695                 if (led == 0)
696                         au8522_led_gpio_enable(state, 0);
697         }
698
699         return 0;
700 }
701
702 int au8522_sleep(struct dvb_frontend *fe)
703 {
704         struct au8522_state *state = fe->demodulator_priv;
705         dprintk("%s()\n", __func__);
706
707         /* turn off led */
708         au8522_led_ctrl(state, 0);
709
710         /* Power down the chip */
711         au8522_writereg(state, 0xa4, 1 << 5);
712
713         state->current_frequency = 0;
714
715         return 0;
716 }
717
718 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
719 {
720         struct au8522_state *state = fe->demodulator_priv;
721         u8 reg;
722         u32 tuner_status = 0;
723
724         *status = 0;
725
726         if (state->current_modulation == VSB_8) {
727                 dprintk("%s() Checking VSB_8\n", __func__);
728                 reg = au8522_readreg(state, 0x4088);
729                 if ((reg & 0x03) == 0x03)
730                         *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
731         } else {
732                 dprintk("%s() Checking QAM\n", __func__);
733                 reg = au8522_readreg(state, 0x4541);
734                 if (reg & 0x80)
735                         *status |= FE_HAS_VITERBI;
736                 if (reg & 0x20)
737                         *status |= FE_HAS_LOCK | FE_HAS_SYNC;
738         }
739
740         switch (state->config->status_mode) {
741         case AU8522_DEMODLOCKING:
742                 dprintk("%s() DEMODLOCKING\n", __func__);
743                 if (*status & FE_HAS_VITERBI)
744                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
745                 break;
746         case AU8522_TUNERLOCKING:
747                 /* Get the tuner status */
748                 dprintk("%s() TUNERLOCKING\n", __func__);
749                 if (fe->ops.tuner_ops.get_status) {
750                         if (fe->ops.i2c_gate_ctrl)
751                                 fe->ops.i2c_gate_ctrl(fe, 1);
752
753                         fe->ops.tuner_ops.get_status(fe, &tuner_status);
754
755                         if (fe->ops.i2c_gate_ctrl)
756                                 fe->ops.i2c_gate_ctrl(fe, 0);
757                 }
758                 if (tuner_status)
759                         *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
760                 break;
761         }
762         state->fe_status = *status;
763
764         if (*status & FE_HAS_LOCK)
765                 /* turn on LED, if it isn't on already */
766                 au8522_led_ctrl(state, -1);
767         else
768                 /* turn off LED */
769                 au8522_led_ctrl(state, 0);
770
771         dprintk("%s() status 0x%08x\n", __func__, *status);
772
773         return 0;
774 }
775
776 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
777 {
778         struct au8522_led_config *led_config = state->config->led_cfg;
779         int led;
780         u16 strong;
781
782         /* bail out if we cant control an LED */
783         if (!led_config)
784                 return 0;
785
786         if (0 == (state->fe_status & FE_HAS_LOCK))
787                 return au8522_led_ctrl(state, 0);
788         else if (state->current_modulation == QAM_256)
789                 strong = led_config->qam256_strong;
790         else if (state->current_modulation == QAM_64)
791                 strong = led_config->qam64_strong;
792         else /* (state->current_modulation == VSB_8) */
793                 strong = led_config->vsb8_strong;
794
795         if (*snr >= strong)
796                 led = 2;
797         else
798                 led = 1;
799
800         if ((state->led_state) &&
801             (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
802                 /* snr didn't change enough to bother
803                  * changing the color of the led */
804                 return 0;
805
806         return au8522_led_ctrl(state, led);
807 }
808
809 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
810 {
811         struct au8522_state *state = fe->demodulator_priv;
812         int ret = -EINVAL;
813
814         dprintk("%s()\n", __func__);
815
816         if (state->current_modulation == QAM_256)
817                 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
818                                             ARRAY_SIZE(qam256_mse2snr_tab),
819                                             au8522_readreg(state, 0x4522),
820                                             snr);
821         else if (state->current_modulation == QAM_64)
822                 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
823                                             ARRAY_SIZE(qam64_mse2snr_tab),
824                                             au8522_readreg(state, 0x4522),
825                                             snr);
826         else /* VSB_8 */
827                 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
828                                             ARRAY_SIZE(vsb_mse2snr_tab),
829                                             au8522_readreg(state, 0x4311),
830                                             snr);
831
832         if (state->config->led_cfg)
833                 au8522_led_status(state, snr);
834
835         return ret;
836 }
837
838 static int au8522_read_signal_strength(struct dvb_frontend *fe,
839                                        u16 *signal_strength)
840 {
841         return au8522_read_snr(fe, signal_strength);
842 }
843
844 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
845 {
846         struct au8522_state *state = fe->demodulator_priv;
847
848         if (state->current_modulation == VSB_8)
849                 *ucblocks = au8522_readreg(state, 0x4087);
850         else
851                 *ucblocks = au8522_readreg(state, 0x4543);
852
853         return 0;
854 }
855
856 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
857 {
858         return au8522_read_ucblocks(fe, ber);
859 }
860
861 static int au8522_get_frontend(struct dvb_frontend *fe,
862                                 struct dvb_frontend_parameters *p)
863 {
864         struct au8522_state *state = fe->demodulator_priv;
865
866         p->frequency = state->current_frequency;
867         p->u.vsb.modulation = state->current_modulation;
868
869         return 0;
870 }
871
872 static int au8522_get_tune_settings(struct dvb_frontend *fe,
873                                     struct dvb_frontend_tune_settings *tune)
874 {
875         tune->min_delay_ms = 1000;
876         return 0;
877 }
878
879 static struct dvb_frontend_ops au8522_ops;
880
881 int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
882                      u8 client_address)
883 {
884         int ret;
885
886         mutex_lock(&au8522_list_mutex);
887         ret = hybrid_tuner_request_state(struct au8522_state, (*state),
888                                          hybrid_tuner_instance_list,
889                                          i2c, client_address, "au8522");
890         mutex_unlock(&au8522_list_mutex);
891
892         return ret;
893 }
894
895 void au8522_release_state(struct au8522_state *state)
896 {
897         mutex_lock(&au8522_list_mutex);
898         if (state != NULL)
899                 hybrid_tuner_release_state(state);
900         mutex_unlock(&au8522_list_mutex);
901 }
902
903
904 static void au8522_release(struct dvb_frontend *fe)
905 {
906         struct au8522_state *state = fe->demodulator_priv;
907         au8522_release_state(state);
908 }
909
910 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
911                                    struct i2c_adapter *i2c)
912 {
913         struct au8522_state *state = NULL;
914         int instance;
915
916         /* allocate memory for the internal state */
917         instance = au8522_get_state(&state, i2c, config->demod_address);
918         switch (instance) {
919         case 0:
920                 dprintk("%s state allocation failed\n", __func__);
921                 break;
922         case 1:
923                 /* new demod instance */
924                 dprintk("%s using new instance\n", __func__);
925                 break;
926         default:
927                 /* existing demod instance */
928                 dprintk("%s using existing instance\n", __func__);
929                 break;
930         }
931
932         /* setup the state */
933         state->config = config;
934         state->i2c = i2c;
935         /* create dvb_frontend */
936         memcpy(&state->frontend.ops, &au8522_ops,
937                sizeof(struct dvb_frontend_ops));
938         state->frontend.demodulator_priv = state;
939
940         if (au8522_init(&state->frontend) != 0) {
941                 printk(KERN_ERR "%s: Failed to initialize correctly\n",
942                         __func__);
943                 goto error;
944         }
945
946         /* Note: Leaving the I2C gate open here. */
947         au8522_i2c_gate_ctrl(&state->frontend, 1);
948
949         return &state->frontend;
950
951 error:
952         au8522_release_state(state);
953         return NULL;
954 }
955 EXPORT_SYMBOL(au8522_attach);
956
957 static struct dvb_frontend_ops au8522_ops = {
958
959         .info = {
960                 .name                   = "Auvitek AU8522 QAM/8VSB Frontend",
961                 .type                   = FE_ATSC,
962                 .frequency_min          = 54000000,
963                 .frequency_max          = 858000000,
964                 .frequency_stepsize     = 62500,
965                 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
966         },
967
968         .init                 = au8522_init,
969         .sleep                = au8522_sleep,
970         .i2c_gate_ctrl        = au8522_i2c_gate_ctrl,
971         .set_frontend         = au8522_set_frontend,
972         .get_frontend         = au8522_get_frontend,
973         .get_tune_settings    = au8522_get_tune_settings,
974         .read_status          = au8522_read_status,
975         .read_ber             = au8522_read_ber,
976         .read_signal_strength = au8522_read_signal_strength,
977         .read_snr             = au8522_read_snr,
978         .read_ucblocks        = au8522_read_ucblocks,
979         .release              = au8522_release,
980 };
981
982 module_param(debug, int, 0644);
983 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
984
985 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
986 MODULE_AUTHOR("Steven Toth");
987 MODULE_LICENSE("GPL");