Pull mem-attribute into release branch
[linux-2.6] / drivers / media / dvb / frontends / or51132.c
1 /*
2  *    Support for OR51132 (pcHDTV HD-3000) - VSB/QAM
3  *
4  *
5  *    Copyright (C) 2007 Trent Piepho <xyzzy@speakeasy.org>
6  *
7  *    Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
8  *
9  *    Based on code from Jack Kelliher (kelliher@xmission.com)
10  *                           Copyright (C) 2002 & pcHDTV, inc.
11  *
12  *    This program is free software; you can redistribute it and/or modify
13  *    it under the terms of the GNU General Public License as published by
14  *    the Free Software Foundation; either version 2 of the License, or
15  *    (at your option) any later version.
16  *
17  *    This program is distributed in the hope that it will be useful,
18  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *    GNU General Public License for more details.
21  *
22  *    You should have received a copy of the GNU General Public License
23  *    along with this program; if not, write to the Free Software
24  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26 */
27
28 /*
29  * This driver needs two external firmware files. Please copy
30  * "dvb-fe-or51132-vsb.fw" and "dvb-fe-or51132-qam.fw" to
31  * /usr/lib/hotplug/firmware/ or /lib/firmware/
32  * (depending on configuration of firmware hotplug).
33  */
34 #define OR51132_VSB_FIRMWARE "dvb-fe-or51132-vsb.fw"
35 #define OR51132_QAM_FIRMWARE "dvb-fe-or51132-qam.fw"
36
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/string.h>
43 #include <linux/slab.h>
44 #include <asm/byteorder.h>
45
46 #include "dvb_math.h"
47 #include "dvb_frontend.h"
48 #include "dvb-pll.h"
49 #include "or51132.h"
50
51 static int debug;
52 #define dprintk(args...) \
53         do { \
54                 if (debug) printk(KERN_DEBUG "or51132: " args); \
55         } while (0)
56
57
58 struct or51132_state
59 {
60         struct i2c_adapter* i2c;
61
62         /* Configuration settings */
63         const struct or51132_config* config;
64
65         struct dvb_frontend frontend;
66
67         /* Demodulator private data */
68         fe_modulation_t current_modulation;
69         u32 snr; /* Result of last SNR calculation */
70
71         /* Tuner private data */
72         u32 current_frequency;
73 };
74
75
76 /* Write buffer to demod */
77 static int or51132_writebuf(struct or51132_state *state, const u8 *buf, int len)
78 {
79         int err;
80         struct i2c_msg msg = { .addr = state->config->demod_address,
81                                .flags = 0, .buf = (u8*)buf, .len = len };
82
83         /* msleep(20); */ /* doesn't appear to be necessary */
84         if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
85                 printk(KERN_WARNING "or51132: I2C write (addr 0x%02x len %d) error: %d\n",
86                        msg.addr, msg.len, err);
87                 return -EREMOTEIO;
88         }
89         return 0;
90 }
91
92 /* Write constant bytes, e.g. or51132_writebytes(state, 0x04, 0x42, 0x00);
93    Less code and more efficient that loading a buffer on the stack with
94    the bytes to send and then calling or51132_writebuf() on that. */
95 #define or51132_writebytes(state, data...)  \
96         ({ const static u8 _data[] = {data}; \
97         or51132_writebuf(state, _data, sizeof(_data)); })
98
99 /* Read data from demod into buffer.  Returns 0 on success. */
100 static int or51132_readbuf(struct or51132_state *state, u8 *buf, int len)
101 {
102         int err;
103         struct i2c_msg msg = { .addr = state->config->demod_address,
104                                .flags = I2C_M_RD, .buf = buf, .len = len };
105
106         /* msleep(20); */ /* doesn't appear to be necessary */
107         if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
108                 printk(KERN_WARNING "or51132: I2C read (addr 0x%02x len %d) error: %d\n",
109                        msg.addr, msg.len, err);
110                 return -EREMOTEIO;
111         }
112         return 0;
113 }
114
115 /* Reads a 16-bit demod register.  Returns <0 on error. */
116 static int or51132_readreg(struct or51132_state *state, u8 reg)
117 {
118         u8 buf[2] = { 0x04, reg };
119         struct i2c_msg msg[2] = {
120                 {.addr = state->config->demod_address, .flags = 0,
121                  .buf = buf, .len = 2 },
122                 {.addr = state->config->demod_address, .flags = I2C_M_RD,
123                  .buf = buf, .len = 2 }};
124         int err;
125
126         if ((err = i2c_transfer(state->i2c, msg, 2)) != 2) {
127                 printk(KERN_WARNING "or51132: I2C error reading register %d: %d\n",
128                        reg, err);
129                 return -EREMOTEIO;
130         }
131         return le16_to_cpup((u16*)buf);
132 }
133
134 static int or51132_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
135 {
136         struct or51132_state* state = fe->demodulator_priv;
137         const static u8 run_buf[] = {0x7F,0x01};
138         u8 rec_buf[8];
139         u32 firmwareAsize, firmwareBsize;
140         int i,ret;
141
142         dprintk("Firmware is %Zd bytes\n",fw->size);
143
144         /* Get size of firmware A and B */
145         firmwareAsize = le32_to_cpu(*((u32*)fw->data));
146         dprintk("FirmwareA is %i bytes\n",firmwareAsize);
147         firmwareBsize = le32_to_cpu(*((u32*)(fw->data+4)));
148         dprintk("FirmwareB is %i bytes\n",firmwareBsize);
149
150         /* Upload firmware */
151         if ((ret = or51132_writebuf(state, &fw->data[8], firmwareAsize))) {
152                 printk(KERN_WARNING "or51132: load_firmware error 1\n");
153                 return ret;
154         }
155         if ((ret = or51132_writebuf(state, &fw->data[8+firmwareAsize],
156                                     firmwareBsize))) {
157                 printk(KERN_WARNING "or51132: load_firmware error 2\n");
158                 return ret;
159         }
160
161         if ((ret = or51132_writebuf(state, run_buf, 2))) {
162                 printk(KERN_WARNING "or51132: load_firmware error 3\n");
163                 return ret;
164         }
165         if ((ret = or51132_writebuf(state, run_buf, 2))) {
166                 printk(KERN_WARNING "or51132: load_firmware error 4\n");
167                 return ret;
168         }
169
170         /* 50ms for operation to begin */
171         msleep(50);
172
173         /* Read back ucode version to besure we loaded correctly and are really up and running */
174         /* Get uCode version */
175         if ((ret = or51132_writebytes(state, 0x10, 0x10, 0x00))) {
176                 printk(KERN_WARNING "or51132: load_firmware error a\n");
177                 return ret;
178         }
179         if ((ret = or51132_writebytes(state, 0x04, 0x17))) {
180                 printk(KERN_WARNING "or51132: load_firmware error b\n");
181                 return ret;
182         }
183         if ((ret = or51132_writebytes(state, 0x00, 0x00))) {
184                 printk(KERN_WARNING "or51132: load_firmware error c\n");
185                 return ret;
186         }
187         for (i=0;i<4;i++) {
188                 /* Once upon a time, this command might have had something
189                    to do with getting the firmware version, but it's
190                    not used anymore:
191                    {0x04,0x00,0x30,0x00,i+1} */
192                 /* Read 8 bytes, two bytes at a time */
193                 if ((ret = or51132_readbuf(state, &rec_buf[i*2], 2))) {
194                         printk(KERN_WARNING
195                                "or51132: load_firmware error d - %d\n",i);
196                         return ret;
197                 }
198         }
199
200         printk(KERN_WARNING
201                "or51132: Version: %02X%02X%02X%02X-%02X%02X%02X%02X (%02X%01X-%01X-%02X%01X-%01X)\n",
202                rec_buf[1],rec_buf[0],rec_buf[3],rec_buf[2],
203                rec_buf[5],rec_buf[4],rec_buf[7],rec_buf[6],
204                rec_buf[3],rec_buf[2]>>4,rec_buf[2]&0x0f,
205                rec_buf[5],rec_buf[4]>>4,rec_buf[4]&0x0f);
206
207         if ((ret = or51132_writebytes(state, 0x10, 0x00, 0x00))) {
208                 printk(KERN_WARNING "or51132: load_firmware error e\n");
209                 return ret;
210         }
211         return 0;
212 };
213
214 static int or51132_init(struct dvb_frontend* fe)
215 {
216         return 0;
217 }
218
219 static int or51132_read_ber(struct dvb_frontend* fe, u32* ber)
220 {
221         *ber = 0;
222         return 0;
223 }
224
225 static int or51132_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
226 {
227         *ucblocks = 0;
228         return 0;
229 }
230
231 static int or51132_sleep(struct dvb_frontend* fe)
232 {
233         return 0;
234 }
235
236 static int or51132_setmode(struct dvb_frontend* fe)
237 {
238         struct or51132_state* state = fe->demodulator_priv;
239         u8 cmd_buf1[3] = {0x04, 0x01, 0x5f};
240         u8 cmd_buf2[3] = {0x1c, 0x00, 0 };
241
242         dprintk("setmode %d\n",(int)state->current_modulation);
243
244         switch (state->current_modulation) {
245         case VSB_8:
246                 /* Auto CH, Auto NTSC rej, MPEGser, MPEG2tr, phase noise-high */
247                 cmd_buf1[2] = 0x50;
248                 /* REC MODE inv IF spectrum, Normal */
249                 cmd_buf2[1] = 0x03;
250                 /* Channel MODE ATSC/VSB8 */
251                 cmd_buf2[2] = 0x06;
252                 break;
253         /* All QAM modes are:
254            Auto-deinterleave; MPEGser, MPEG2tr, phase noise-high
255            REC MODE Normal Carrier Lock */
256         case QAM_AUTO:
257                 /* Channel MODE Auto QAM64/256 */
258                 cmd_buf2[2] = 0x4f;
259                 break;
260         case QAM_256:
261                 /* Channel MODE QAM256 */
262                 cmd_buf2[2] = 0x45;
263                 break;
264         case QAM_64:
265                 /* Channel MODE QAM64 */
266                 cmd_buf2[2] = 0x43;
267                 break;
268         default:
269                 printk(KERN_WARNING
270                        "or51132: setmode: Modulation set to unsupported value (%d)\n",
271                        state->current_modulation);
272                 return -EINVAL;
273         }
274
275         /* Set Receiver 1 register */
276         if (or51132_writebuf(state, cmd_buf1, 3)) {
277                 printk(KERN_WARNING "or51132: set_mode error 1\n");
278                 return -EREMOTEIO;
279         }
280         dprintk("set #1 to %02x\n", cmd_buf1[2]);
281
282         /* Set operation mode in Receiver 6 register */
283         if (or51132_writebuf(state, cmd_buf2, 3)) {
284                 printk(KERN_WARNING "or51132: set_mode error 2\n");
285                 return -EREMOTEIO;
286         }
287         dprintk("set #6 to 0x%02x%02x\n", cmd_buf2[1], cmd_buf2[2]);
288
289         return 0;
290 }
291
292 /* Some modulations use the same firmware.  This classifies modulations
293    by the firmware they use. */
294 #define MOD_FWCLASS_UNKNOWN     0
295 #define MOD_FWCLASS_VSB         1
296 #define MOD_FWCLASS_QAM         2
297 static int modulation_fw_class(fe_modulation_t modulation)
298 {
299         switch(modulation) {
300         case VSB_8:
301                 return MOD_FWCLASS_VSB;
302         case QAM_AUTO:
303         case QAM_64:
304         case QAM_256:
305                 return MOD_FWCLASS_QAM;
306         default:
307                 return MOD_FWCLASS_UNKNOWN;
308         }
309 }
310
311 static int or51132_set_parameters(struct dvb_frontend* fe,
312                                   struct dvb_frontend_parameters *param)
313 {
314         int ret;
315         struct or51132_state* state = fe->demodulator_priv;
316         const struct firmware *fw;
317         const char *fwname;
318         int clock_mode;
319
320         /* Upload new firmware only if we need a different one */
321         if (modulation_fw_class(state->current_modulation) !=
322             modulation_fw_class(param->u.vsb.modulation)) {
323                 switch(modulation_fw_class(param->u.vsb.modulation)) {
324                 case MOD_FWCLASS_VSB:
325                         dprintk("set_parameters VSB MODE\n");
326                         fwname = OR51132_VSB_FIRMWARE;
327
328                         /* Set non-punctured clock for VSB */
329                         clock_mode = 0;
330                         break;
331                 case MOD_FWCLASS_QAM:
332                         dprintk("set_parameters QAM MODE\n");
333                         fwname = OR51132_QAM_FIRMWARE;
334
335                         /* Set punctured clock for QAM */
336                         clock_mode = 1;
337                         break;
338                 default:
339                         printk("or51132: Modulation type(%d) UNSUPPORTED\n",
340                                param->u.vsb.modulation);
341                         return -1;
342                 }
343                 printk("or51132: Waiting for firmware upload(%s)...\n",
344                        fwname);
345                 ret = request_firmware(&fw, fwname, &state->i2c->dev);
346                 if (ret) {
347                         printk(KERN_WARNING "or51132: No firmware up"
348                                "loaded(timeout or file not found?)\n");
349                         return ret;
350                 }
351                 ret = or51132_load_firmware(fe, fw);
352                 release_firmware(fw);
353                 if (ret) {
354                         printk(KERN_WARNING "or51132: Writing firmware to "
355                                "device failed!\n");
356                         return ret;
357                 }
358                 printk("or51132: Firmware upload complete.\n");
359                 state->config->set_ts_params(fe, clock_mode);
360         }
361         /* Change only if we are actually changing the modulation */
362         if (state->current_modulation != param->u.vsb.modulation) {
363                 state->current_modulation = param->u.vsb.modulation;
364                 or51132_setmode(fe);
365         }
366
367         if (fe->ops.tuner_ops.set_params) {
368                 fe->ops.tuner_ops.set_params(fe, param);
369                 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
370         }
371
372         /* Set to current mode */
373         or51132_setmode(fe);
374
375         /* Update current frequency */
376         state->current_frequency = param->frequency;
377         return 0;
378 }
379
380 static int or51132_get_parameters(struct dvb_frontend* fe,
381                                   struct dvb_frontend_parameters *param)
382 {
383         struct or51132_state* state = fe->demodulator_priv;
384         int status;
385         int retry = 1;
386
387 start:
388         /* Receiver Status */
389         if ((status = or51132_readreg(state, 0x00)) < 0) {
390                 printk(KERN_WARNING "or51132: get_parameters: error reading receiver status\n");
391                 return -EREMOTEIO;
392         }
393         switch(status&0xff) {
394                 case 0x06: param->u.vsb.modulation = VSB_8; break;
395                 case 0x43: param->u.vsb.modulation = QAM_64; break;
396                 case 0x45: param->u.vsb.modulation = QAM_256; break;
397                 default:
398                         if (retry--) goto start;
399                         printk(KERN_WARNING "or51132: unknown status 0x%02x\n",
400                                status&0xff);
401                         return -EREMOTEIO;
402         }
403
404         /* FIXME: Read frequency from frontend, take AFC into account */
405         param->frequency = state->current_frequency;
406
407         /* FIXME: How to read inversion setting? Receiver 6 register? */
408         param->inversion = INVERSION_AUTO;
409
410         return 0;
411 }
412
413 static int or51132_read_status(struct dvb_frontend* fe, fe_status_t* status)
414 {
415         struct or51132_state* state = fe->demodulator_priv;
416         int reg;
417
418         /* Receiver Status */
419         if ((reg = or51132_readreg(state, 0x00)) < 0) {
420                 printk(KERN_WARNING "or51132: read_status: error reading receiver status: %d\n", reg);
421                 *status = 0;
422                 return -EREMOTEIO;
423         }
424         dprintk("%s: read_status %04x\n", __FUNCTION__, reg);
425
426         if (reg & 0x0100) /* Receiver Lock */
427                 *status = FE_HAS_SIGNAL|FE_HAS_CARRIER|FE_HAS_VITERBI|
428                           FE_HAS_SYNC|FE_HAS_LOCK;
429         else
430                 *status = 0;
431         return 0;
432 }
433
434 /* Calculate SNR estimation (scaled by 2^24)
435
436    8-VSB SNR and QAM equations from Oren datasheets
437
438    For 8-VSB:
439      SNR[dB] = 10 * log10(897152044.8282 / MSE^2 ) - K
440
441      Where K = 0 if NTSC rejection filter is OFF; and
442            K = 3 if NTSC rejection filter is ON
443
444    For QAM64:
445      SNR[dB] = 10 * log10(897152044.8282 / MSE^2 )
446
447    For QAM256:
448      SNR[dB] = 10 * log10(907832426.314266  / MSE^2 )
449
450    We re-write the snr equation as:
451      SNR * 2^24 = 10*(c - 2*intlog10(MSE))
452    Where for QAM256, c = log10(907832426.314266) * 2^24
453    and for 8-VSB and QAM64, c = log10(897152044.8282) * 2^24 */
454
455 static u32 calculate_snr(u32 mse, u32 c)
456 {
457         if (mse == 0) /* No signal */
458                 return 0;
459
460         mse = 2*intlog10(mse);
461         if (mse > c) {
462                 /* Negative SNR, which is possible, but realisticly the
463                 demod will lose lock before the signal gets this bad.  The
464                 API only allows for unsigned values, so just return 0 */
465                 return 0;
466         }
467         return 10*(c - mse);
468 }
469
470 static int or51132_read_snr(struct dvb_frontend* fe, u16* snr)
471 {
472         struct or51132_state* state = fe->demodulator_priv;
473         int noise, reg;
474         u32 c, usK = 0;
475         int retry = 1;
476
477 start:
478         /* SNR after Equalizer */
479         noise = or51132_readreg(state, 0x02);
480         if (noise < 0) {
481                 printk(KERN_WARNING "or51132: read_snr: error reading equalizer\n");
482                 return -EREMOTEIO;
483         }
484         dprintk("read_snr noise (%d)\n", noise);
485
486         /* Read status, contains modulation type for QAM_AUTO and
487            NTSC filter for VSB */
488         reg = or51132_readreg(state, 0x00);
489         if (reg < 0) {
490                 printk(KERN_WARNING "or51132: read_snr: error reading receiver status\n");
491                 return -EREMOTEIO;
492         }
493
494         switch (reg&0xff) {
495         case 0x06:
496                 if (reg & 0x1000) usK = 3 << 24;
497                 /* Fall through to QAM64 case */
498         case 0x43:
499                 c = 150204167;
500                 break;
501         case 0x45:
502                 c = 150290396;
503                 break;
504         default:
505                 printk(KERN_WARNING "or51132: unknown status 0x%02x\n", reg&0xff);
506                 if (retry--) goto start;
507                 return -EREMOTEIO;
508         }
509         dprintk("%s: modulation %02x, NTSC rej O%s\n", __FUNCTION__,
510                 reg&0xff, reg&0x1000?"n":"ff");
511
512         /* Calculate SNR using noise, c, and NTSC rejection correction */
513         state->snr = calculate_snr(noise, c) - usK;
514         *snr = (state->snr) >> 16;
515
516         dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __FUNCTION__, noise,
517                 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
518
519         return 0;
520 }
521
522 static int or51132_read_signal_strength(struct dvb_frontend* fe, u16* strength)
523 {
524         /* Calculate Strength from SNR up to 35dB */
525         /* Even though the SNR can go higher than 35dB, there is some comfort */
526         /* factor in having a range of strong signals that can show at 100%   */
527         struct or51132_state* state = (struct or51132_state*) fe->demodulator_priv;
528         u16 snr;
529         int ret;
530
531         ret = fe->ops.read_snr(fe, &snr);
532         if (ret != 0)
533                 return ret;
534         /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
535         /* scale the range 0 - 35*2^24 into 0 - 65535 */
536         if (state->snr >= 8960 * 0x10000)
537                 *strength = 0xffff;
538         else
539                 *strength = state->snr / 8960;
540
541         return 0;
542 }
543
544 static int or51132_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
545 {
546         fe_tune_settings->min_delay_ms = 500;
547         fe_tune_settings->step_size = 0;
548         fe_tune_settings->max_drift = 0;
549
550         return 0;
551 }
552
553 static void or51132_release(struct dvb_frontend* fe)
554 {
555         struct or51132_state* state = fe->demodulator_priv;
556         kfree(state);
557 }
558
559 static struct dvb_frontend_ops or51132_ops;
560
561 struct dvb_frontend* or51132_attach(const struct or51132_config* config,
562                                     struct i2c_adapter* i2c)
563 {
564         struct or51132_state* state = NULL;
565
566         /* Allocate memory for the internal state */
567         state = kmalloc(sizeof(struct or51132_state), GFP_KERNEL);
568         if (state == NULL)
569                 goto error;
570
571         /* Setup the state */
572         state->config = config;
573         state->i2c = i2c;
574         state->current_frequency = -1;
575         state->current_modulation = -1;
576
577         /* Create dvb_frontend */
578         memcpy(&state->frontend.ops, &or51132_ops, sizeof(struct dvb_frontend_ops));
579         state->frontend.demodulator_priv = state;
580         return &state->frontend;
581
582 error:
583         kfree(state);
584         return NULL;
585 }
586
587 static struct dvb_frontend_ops or51132_ops = {
588
589         .info = {
590                 .name                   = "Oren OR51132 VSB/QAM Frontend",
591                 .type                   = FE_ATSC,
592                 .frequency_min          = 44000000,
593                 .frequency_max          = 958000000,
594                 .frequency_stepsize     = 166666,
595                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
596                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
597                         FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
598                         FE_CAN_8VSB
599         },
600
601         .release = or51132_release,
602
603         .init = or51132_init,
604         .sleep = or51132_sleep,
605
606         .set_frontend = or51132_set_parameters,
607         .get_frontend = or51132_get_parameters,
608         .get_tune_settings = or51132_get_tune_settings,
609
610         .read_status = or51132_read_status,
611         .read_ber = or51132_read_ber,
612         .read_signal_strength = or51132_read_signal_strength,
613         .read_snr = or51132_read_snr,
614         .read_ucblocks = or51132_read_ucblocks,
615 };
616
617 module_param(debug, int, 0644);
618 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
619
620 MODULE_DESCRIPTION("OR51132 ATSC [pcHDTV HD-3000] (8VSB & ITU J83 AnnexB FEC QAM64/256) Demodulator Driver");
621 MODULE_AUTHOR("Kirk Lapray");
622 MODULE_AUTHOR("Trent Piepho");
623 MODULE_LICENSE("GPL");
624
625 EXPORT_SYMBOL(or51132_attach);
626
627 /*
628  * Local variables:
629  * c-basic-offset: 8
630  * End:
631  */