2 * Support for OR51132 (pcHDTV HD-3000) - VSB/QAM
5 * Copyright (C) 2007 Trent Piepho <xyzzy@speakeasy.org>
7 * Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
9 * Based on code from Jack Kelliher (kelliher@xmission.com)
10 * Copyright (C) 2002 & pcHDTV, inc.
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.
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.
25 * This driver needs two external firmware files. Please copy
26 * "dvb-fe-or51132-vsb.fw" and "dvb-fe-or51132-qam.fw" to
27 * /usr/lib/hotplug/firmware/ or /lib/firmware/
28 * (depending on configuration of firmware hotplug).
30 #define OR51132_VSB_FIRMWARE "dvb-fe-or51132-vsb.fw"
31 #define OR51132_QAM_FIRMWARE "dvb-fe-or51132-qam.fw"
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
39 #include <asm/byteorder.h>
41 #include <media/dvb_math.h>
42 #include <media/dvb_frontend.h>
46 #define dprintk(args...) \
48 if (debug) printk(KERN_DEBUG "or51132: " args); \
54 struct i2c_adapter
* i2c
;
56 /* Configuration settings */
57 const struct or51132_config
* config
;
59 struct dvb_frontend frontend
;
61 /* Demodulator private data */
62 enum fe_modulation current_modulation
;
63 u32 snr
; /* Result of last SNR calculation */
65 /* Tuner private data */
66 u32 current_frequency
;
70 /* Write buffer to demod */
71 static int or51132_writebuf(struct or51132_state
*state
, const u8
*buf
, int len
)
74 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
,
75 .flags
= 0, .buf
= (u8
*)buf
, .len
= len
};
77 /* msleep(20); */ /* doesn't appear to be necessary */
78 if ((err
= i2c_transfer(state
->i2c
, &msg
, 1)) != 1) {
79 printk(KERN_WARNING
"or51132: I2C write (addr 0x%02x len %d) error: %d\n",
80 msg
.addr
, msg
.len
, err
);
86 /* Write constant bytes, e.g. or51132_writebytes(state, 0x04, 0x42, 0x00);
87 Less code and more efficient that loading a buffer on the stack with
88 the bytes to send and then calling or51132_writebuf() on that. */
89 #define or51132_writebytes(state, data...) \
90 ({ static const u8 _data[] = {data}; \
91 or51132_writebuf(state, _data, sizeof(_data)); })
93 /* Read data from demod into buffer. Returns 0 on success. */
94 static int or51132_readbuf(struct or51132_state
*state
, u8
*buf
, int len
)
97 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
,
98 .flags
= I2C_M_RD
, .buf
= buf
, .len
= len
};
100 /* msleep(20); */ /* doesn't appear to be necessary */
101 if ((err
= i2c_transfer(state
->i2c
, &msg
, 1)) != 1) {
102 printk(KERN_WARNING
"or51132: I2C read (addr 0x%02x len %d) error: %d\n",
103 msg
.addr
, msg
.len
, err
);
109 /* Reads a 16-bit demod register. Returns <0 on error. */
110 static int or51132_readreg(struct or51132_state
*state
, u8 reg
)
112 u8 buf
[2] = { 0x04, reg
};
113 struct i2c_msg msg
[2] = {
114 {.addr
= state
->config
->demod_address
, .flags
= 0,
115 .buf
= buf
, .len
= 2 },
116 {.addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
,
117 .buf
= buf
, .len
= 2 }};
120 if ((err
= i2c_transfer(state
->i2c
, msg
, 2)) != 2) {
121 printk(KERN_WARNING
"or51132: I2C error reading register %d: %d\n",
125 return buf
[0] | (buf
[1] << 8);
128 static int or51132_load_firmware (struct dvb_frontend
* fe
, const struct firmware
*fw
)
130 struct or51132_state
* state
= fe
->demodulator_priv
;
131 static const u8 run_buf
[] = {0x7F,0x01};
133 u32 firmwareAsize
, firmwareBsize
;
136 dprintk("Firmware is %zd bytes\n",fw
->size
);
138 /* Get size of firmware A and B */
139 firmwareAsize
= le32_to_cpu(*((__le32
*)fw
->data
));
140 dprintk("FirmwareA is %i bytes\n",firmwareAsize
);
141 firmwareBsize
= le32_to_cpu(*((__le32
*)(fw
->data
+4)));
142 dprintk("FirmwareB is %i bytes\n",firmwareBsize
);
144 /* Upload firmware */
145 if ((ret
= or51132_writebuf(state
, &fw
->data
[8], firmwareAsize
))) {
146 printk(KERN_WARNING
"or51132: load_firmware error 1\n");
149 if ((ret
= or51132_writebuf(state
, &fw
->data
[8+firmwareAsize
],
151 printk(KERN_WARNING
"or51132: load_firmware error 2\n");
155 if ((ret
= or51132_writebuf(state
, run_buf
, 2))) {
156 printk(KERN_WARNING
"or51132: load_firmware error 3\n");
159 if ((ret
= or51132_writebuf(state
, run_buf
, 2))) {
160 printk(KERN_WARNING
"or51132: load_firmware error 4\n");
164 /* 50ms for operation to begin */
167 /* Read back ucode version to besure we loaded correctly and are really up and running */
168 /* Get uCode version */
169 if ((ret
= or51132_writebytes(state
, 0x10, 0x10, 0x00))) {
170 printk(KERN_WARNING
"or51132: load_firmware error a\n");
173 if ((ret
= or51132_writebytes(state
, 0x04, 0x17))) {
174 printk(KERN_WARNING
"or51132: load_firmware error b\n");
177 if ((ret
= or51132_writebytes(state
, 0x00, 0x00))) {
178 printk(KERN_WARNING
"or51132: load_firmware error c\n");
182 /* Once upon a time, this command might have had something
183 to do with getting the firmware version, but it's
185 {0x04,0x00,0x30,0x00,i+1} */
186 /* Read 8 bytes, two bytes at a time */
187 if ((ret
= or51132_readbuf(state
, &rec_buf
[i
*2], 2))) {
189 "or51132: load_firmware error d - %d\n",i
);
195 "or51132: Version: %02X%02X%02X%02X-%02X%02X%02X%02X (%02X%01X-%01X-%02X%01X-%01X)\n",
196 rec_buf
[1],rec_buf
[0],rec_buf
[3],rec_buf
[2],
197 rec_buf
[5],rec_buf
[4],rec_buf
[7],rec_buf
[6],
198 rec_buf
[3],rec_buf
[2]>>4,rec_buf
[2]&0x0f,
199 rec_buf
[5],rec_buf
[4]>>4,rec_buf
[4]&0x0f);
201 if ((ret
= or51132_writebytes(state
, 0x10, 0x00, 0x00))) {
202 printk(KERN_WARNING
"or51132: load_firmware error e\n");
208 static int or51132_init(struct dvb_frontend
* fe
)
213 static int or51132_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
219 static int or51132_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
225 static int or51132_sleep(struct dvb_frontend
* fe
)
230 static int or51132_setmode(struct dvb_frontend
* fe
)
232 struct or51132_state
* state
= fe
->demodulator_priv
;
233 u8 cmd_buf1
[3] = {0x04, 0x01, 0x5f};
234 u8 cmd_buf2
[3] = {0x1c, 0x00, 0 };
236 dprintk("setmode %d\n",(int)state
->current_modulation
);
238 switch (state
->current_modulation
) {
240 /* Auto CH, Auto NTSC rej, MPEGser, MPEG2tr, phase noise-high */
242 /* REC MODE inv IF spectrum, Normal */
244 /* Channel MODE ATSC/VSB8 */
247 /* All QAM modes are:
248 Auto-deinterleave; MPEGser, MPEG2tr, phase noise-high
249 REC MODE Normal Carrier Lock */
251 /* Channel MODE Auto QAM64/256 */
255 /* Channel MODE QAM256 */
259 /* Channel MODE QAM64 */
264 "or51132: setmode: Modulation set to unsupported value (%d)\n",
265 state
->current_modulation
);
269 /* Set Receiver 1 register */
270 if (or51132_writebuf(state
, cmd_buf1
, 3)) {
271 printk(KERN_WARNING
"or51132: set_mode error 1\n");
274 dprintk("set #1 to %02x\n", cmd_buf1
[2]);
276 /* Set operation mode in Receiver 6 register */
277 if (or51132_writebuf(state
, cmd_buf2
, 3)) {
278 printk(KERN_WARNING
"or51132: set_mode error 2\n");
281 dprintk("set #6 to 0x%02x%02x\n", cmd_buf2
[1], cmd_buf2
[2]);
286 /* Some modulations use the same firmware. This classifies modulations
287 by the firmware they use. */
288 #define MOD_FWCLASS_UNKNOWN 0
289 #define MOD_FWCLASS_VSB 1
290 #define MOD_FWCLASS_QAM 2
291 static int modulation_fw_class(enum fe_modulation modulation
)
295 return MOD_FWCLASS_VSB
;
299 return MOD_FWCLASS_QAM
;
301 return MOD_FWCLASS_UNKNOWN
;
305 static int or51132_set_parameters(struct dvb_frontend
*fe
)
307 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
309 struct or51132_state
* state
= fe
->demodulator_priv
;
310 const struct firmware
*fw
;
314 /* Upload new firmware only if we need a different one */
315 if (modulation_fw_class(state
->current_modulation
) !=
316 modulation_fw_class(p
->modulation
)) {
317 switch (modulation_fw_class(p
->modulation
)) {
318 case MOD_FWCLASS_VSB
:
319 dprintk("set_parameters VSB MODE\n");
320 fwname
= OR51132_VSB_FIRMWARE
;
322 /* Set non-punctured clock for VSB */
325 case MOD_FWCLASS_QAM
:
326 dprintk("set_parameters QAM MODE\n");
327 fwname
= OR51132_QAM_FIRMWARE
;
329 /* Set punctured clock for QAM */
333 printk("or51132: Modulation type(%d) UNSUPPORTED\n",
337 printk("or51132: Waiting for firmware upload(%s)...\n",
339 ret
= request_firmware(&fw
, fwname
, state
->i2c
->dev
.parent
);
341 printk(KERN_WARNING
"or51132: No firmware uploaded(timeout or file not found?)\n");
344 ret
= or51132_load_firmware(fe
, fw
);
345 release_firmware(fw
);
347 printk(KERN_WARNING
"or51132: Writing firmware to device failed!\n");
350 printk("or51132: Firmware upload complete.\n");
351 state
->config
->set_ts_params(fe
, clock_mode
);
353 /* Change only if we are actually changing the modulation */
354 if (state
->current_modulation
!= p
->modulation
) {
355 state
->current_modulation
= p
->modulation
;
359 if (fe
->ops
.tuner_ops
.set_params
) {
360 fe
->ops
.tuner_ops
.set_params(fe
);
361 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
364 /* Set to current mode */
367 /* Update current frequency */
368 state
->current_frequency
= p
->frequency
;
372 static int or51132_get_parameters(struct dvb_frontend
* fe
,
373 struct dtv_frontend_properties
*p
)
375 struct or51132_state
* state
= fe
->demodulator_priv
;
380 /* Receiver Status */
381 if ((status
= or51132_readreg(state
, 0x00)) < 0) {
382 printk(KERN_WARNING
"or51132: get_parameters: error reading receiver status\n");
385 switch(status
&0xff) {
387 p
->modulation
= VSB_8
;
390 p
->modulation
= QAM_64
;
393 p
->modulation
= QAM_256
;
398 printk(KERN_WARNING
"or51132: unknown status 0x%02x\n",
403 /* FIXME: Read frequency from frontend, take AFC into account */
404 p
->frequency
= state
->current_frequency
;
406 /* FIXME: How to read inversion setting? Receiver 6 register? */
407 p
->inversion
= INVERSION_AUTO
;
412 static int or51132_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
414 struct or51132_state
* state
= fe
->demodulator_priv
;
417 /* Receiver Status */
418 if ((reg
= or51132_readreg(state
, 0x00)) < 0) {
419 printk(KERN_WARNING
"or51132: read_status: error reading receiver status: %d\n", reg
);
423 dprintk("%s: read_status %04x\n", __func__
, reg
);
425 if (reg
& 0x0100) /* Receiver Lock */
426 *status
= FE_HAS_SIGNAL
|FE_HAS_CARRIER
|FE_HAS_VITERBI
|
427 FE_HAS_SYNC
|FE_HAS_LOCK
;
433 /* Calculate SNR estimation (scaled by 2^24)
435 8-VSB SNR and QAM equations from Oren datasheets
438 SNR[dB] = 10 * log10(897152044.8282 / MSE^2 ) - K
440 Where K = 0 if NTSC rejection filter is OFF; and
441 K = 3 if NTSC rejection filter is ON
444 SNR[dB] = 10 * log10(897152044.8282 / MSE^2 )
447 SNR[dB] = 10 * log10(907832426.314266 / MSE^2 )
449 We re-write the snr equation as:
450 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
451 Where for QAM256, c = log10(907832426.314266) * 2^24
452 and for 8-VSB and QAM64, c = log10(897152044.8282) * 2^24 */
454 static u32
calculate_snr(u32 mse
, u32 c
)
456 if (mse
== 0) /* No signal */
459 mse
= 2*intlog10(mse
);
461 /* Negative SNR, which is possible, but realisticly the
462 demod will lose lock before the signal gets this bad. The
463 API only allows for unsigned values, so just return 0 */
469 static int or51132_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
471 struct or51132_state
* state
= fe
->demodulator_priv
;
477 /* SNR after Equalizer */
478 noise
= or51132_readreg(state
, 0x02);
480 printk(KERN_WARNING
"or51132: read_snr: error reading equalizer\n");
483 dprintk("read_snr noise (%d)\n", noise
);
485 /* Read status, contains modulation type for QAM_AUTO and
486 NTSC filter for VSB */
487 reg
= or51132_readreg(state
, 0x00);
489 printk(KERN_WARNING
"or51132: read_snr: error reading receiver status\n");
495 if (reg
& 0x1000) usK
= 3 << 24;
497 case 0x43: /* QAM64 */
504 printk(KERN_WARNING
"or51132: unknown status 0x%02x\n", reg
&0xff);
505 if (retry
--) goto start
;
508 dprintk("%s: modulation %02x, NTSC rej O%s\n", __func__
,
509 reg
&0xff, reg
&0x1000?"n":"ff");
511 /* Calculate SNR using noise, c, and NTSC rejection correction */
512 state
->snr
= calculate_snr(noise
, c
) - usK
;
513 *snr
= (state
->snr
) >> 16;
515 dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __func__
, noise
,
516 state
->snr
>> 24, (((state
->snr
>>8) & 0xffff) * 100) >> 16);
521 static int or51132_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
523 /* Calculate Strength from SNR up to 35dB */
524 /* Even though the SNR can go higher than 35dB, there is some comfort */
525 /* factor in having a range of strong signals that can show at 100% */
526 struct or51132_state
* state
= (struct or51132_state
*) fe
->demodulator_priv
;
530 ret
= fe
->ops
.read_snr(fe
, &snr
);
533 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
534 /* scale the range 0 - 35*2^24 into 0 - 65535 */
535 if (state
->snr
>= 8960 * 0x10000)
538 *strength
= state
->snr
/ 8960;
543 static int or51132_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
* fe_tune_settings
)
545 fe_tune_settings
->min_delay_ms
= 500;
546 fe_tune_settings
->step_size
= 0;
547 fe_tune_settings
->max_drift
= 0;
552 static void or51132_release(struct dvb_frontend
* fe
)
554 struct or51132_state
* state
= fe
->demodulator_priv
;
558 static const struct dvb_frontend_ops or51132_ops
;
560 struct dvb_frontend
* or51132_attach(const struct or51132_config
* config
,
561 struct i2c_adapter
* i2c
)
563 struct or51132_state
* state
= NULL
;
565 /* Allocate memory for the internal state */
566 state
= kzalloc(sizeof(struct or51132_state
), GFP_KERNEL
);
570 /* Setup the state */
571 state
->config
= config
;
573 state
->current_frequency
= -1;
574 state
->current_modulation
= -1;
576 /* Create dvb_frontend */
577 memcpy(&state
->frontend
.ops
, &or51132_ops
, sizeof(struct dvb_frontend_ops
));
578 state
->frontend
.demodulator_priv
= state
;
579 return &state
->frontend
;
582 static const struct dvb_frontend_ops or51132_ops
= {
583 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
585 .name
= "Oren OR51132 VSB/QAM Frontend",
586 .frequency_min_hz
= 44 * MHz
,
587 .frequency_max_hz
= 958 * MHz
,
588 .frequency_stepsize_hz
= 166666,
589 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
590 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
591 FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_QAM_AUTO
|
595 .release
= or51132_release
,
597 .init
= or51132_init
,
598 .sleep
= or51132_sleep
,
600 .set_frontend
= or51132_set_parameters
,
601 .get_frontend
= or51132_get_parameters
,
602 .get_tune_settings
= or51132_get_tune_settings
,
604 .read_status
= or51132_read_status
,
605 .read_ber
= or51132_read_ber
,
606 .read_signal_strength
= or51132_read_signal_strength
,
607 .read_snr
= or51132_read_snr
,
608 .read_ucblocks
= or51132_read_ucblocks
,
611 module_param(debug
, int, 0644);
612 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
614 MODULE_DESCRIPTION("OR51132 ATSC [pcHDTV HD-3000] (8VSB & ITU J83 AnnexB FEC QAM64/256) Demodulator Driver");
615 MODULE_AUTHOR("Kirk Lapray");
616 MODULE_AUTHOR("Trent Piepho");
617 MODULE_LICENSE("GPL");
619 EXPORT_SYMBOL(or51132_attach
);