1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#define OR51132_VSB_FIRMWARE "dvb-fe-or51132-vsb.fw"
31#define OR51132_QAM_FIRMWARE "dvb-fe-or51132-qam.fw"
32
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>
40
41#include <media/dvb_math.h>
42#include <media/dvb_frontend.h>
43#include "or51132.h"
44
45static int debug;
46#define dprintk(args...) \
47 do { \
48 if (debug) printk(KERN_DEBUG "or51132: " args); \
49 } while (0)
50
51
52struct or51132_state
53{
54 struct i2c_adapter* i2c;
55
56
57 const struct or51132_config* config;
58
59 struct dvb_frontend frontend;
60
61
62 enum fe_modulation current_modulation;
63 u32 snr;
64
65
66 u32 current_frequency;
67};
68
69
70
71static int or51132_writebuf(struct or51132_state *state, const u8 *buf, int len)
72{
73 int err;
74 struct i2c_msg msg = { .addr = state->config->demod_address,
75 .flags = 0, .buf = (u8*)buf, .len = len };
76
77
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);
81 return -EREMOTEIO;
82 }
83 return 0;
84}
85
86
87
88
89#define or51132_writebytes(state, data...) \
90 ({ static const u8 _data[] = {data}; \
91 or51132_writebuf(state, _data, sizeof(_data)); })
92
93
94static int or51132_readbuf(struct or51132_state *state, u8 *buf, int len)
95{
96 int err;
97 struct i2c_msg msg = { .addr = state->config->demod_address,
98 .flags = I2C_M_RD, .buf = buf, .len = len };
99
100
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);
104 return -EREMOTEIO;
105 }
106 return 0;
107}
108
109
110static int or51132_readreg(struct or51132_state *state, u8 reg)
111{
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 }};
118 int err;
119
120 if ((err = i2c_transfer(state->i2c, msg, 2)) != 2) {
121 printk(KERN_WARNING "or51132: I2C error reading register %d: %d\n",
122 reg, err);
123 return -EREMOTEIO;
124 }
125 return buf[0] | (buf[1] << 8);
126}
127
128static int or51132_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
129{
130 struct or51132_state* state = fe->demodulator_priv;
131 static const u8 run_buf[] = {0x7F,0x01};
132 u8 rec_buf[8];
133 u32 firmwareAsize, firmwareBsize;
134 int i,ret;
135
136 dprintk("Firmware is %zd bytes\n",fw->size);
137
138
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);
143
144
145 if ((ret = or51132_writebuf(state, &fw->data[8], firmwareAsize))) {
146 printk(KERN_WARNING "or51132: load_firmware error 1\n");
147 return ret;
148 }
149 if ((ret = or51132_writebuf(state, &fw->data[8+firmwareAsize],
150 firmwareBsize))) {
151 printk(KERN_WARNING "or51132: load_firmware error 2\n");
152 return ret;
153 }
154
155 if ((ret = or51132_writebuf(state, run_buf, 2))) {
156 printk(KERN_WARNING "or51132: load_firmware error 3\n");
157 return ret;
158 }
159 if ((ret = or51132_writebuf(state, run_buf, 2))) {
160 printk(KERN_WARNING "or51132: load_firmware error 4\n");
161 return ret;
162 }
163
164
165 msleep(50);
166
167
168
169 if ((ret = or51132_writebytes(state, 0x10, 0x10, 0x00))) {
170 printk(KERN_WARNING "or51132: load_firmware error a\n");
171 return ret;
172 }
173 if ((ret = or51132_writebytes(state, 0x04, 0x17))) {
174 printk(KERN_WARNING "or51132: load_firmware error b\n");
175 return ret;
176 }
177 if ((ret = or51132_writebytes(state, 0x00, 0x00))) {
178 printk(KERN_WARNING "or51132: load_firmware error c\n");
179 return ret;
180 }
181 for (i=0;i<4;i++) {
182
183
184
185
186
187 if ((ret = or51132_readbuf(state, &rec_buf[i*2], 2))) {
188 printk(KERN_WARNING
189 "or51132: load_firmware error d - %d\n",i);
190 return ret;
191 }
192 }
193
194 printk(KERN_WARNING
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);
200
201 if ((ret = or51132_writebytes(state, 0x10, 0x00, 0x00))) {
202 printk(KERN_WARNING "or51132: load_firmware error e\n");
203 return ret;
204 }
205 return 0;
206};
207
208static int or51132_init(struct dvb_frontend* fe)
209{
210 return 0;
211}
212
213static int or51132_read_ber(struct dvb_frontend* fe, u32* ber)
214{
215 *ber = 0;
216 return 0;
217}
218
219static int or51132_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
220{
221 *ucblocks = 0;
222 return 0;
223}
224
225static int or51132_sleep(struct dvb_frontend* fe)
226{
227 return 0;
228}
229
230static int or51132_setmode(struct dvb_frontend* fe)
231{
232 struct or51132_state* state = fe->demodulator_priv;
233 u8 cmd_buf1[3] = {0x04, 0x01, 0x5f};
234 u8 cmd_buf2[3] = {0x1c, 0x00, 0 };
235
236 dprintk("setmode %d\n",(int)state->current_modulation);
237
238 switch (state->current_modulation) {
239 case VSB_8:
240
241 cmd_buf1[2] = 0x50;
242
243 cmd_buf2[1] = 0x03;
244
245 cmd_buf2[2] = 0x06;
246 break;
247
248
249
250 case QAM_AUTO:
251
252 cmd_buf2[2] = 0x4f;
253 break;
254 case QAM_256:
255
256 cmd_buf2[2] = 0x45;
257 break;
258 case QAM_64:
259
260 cmd_buf2[2] = 0x43;
261 break;
262 default:
263 printk(KERN_WARNING
264 "or51132: setmode: Modulation set to unsupported value (%d)\n",
265 state->current_modulation);
266 return -EINVAL;
267 }
268
269
270 if (or51132_writebuf(state, cmd_buf1, 3)) {
271 printk(KERN_WARNING "or51132: set_mode error 1\n");
272 return -EREMOTEIO;
273 }
274 dprintk("set #1 to %02x\n", cmd_buf1[2]);
275
276
277 if (or51132_writebuf(state, cmd_buf2, 3)) {
278 printk(KERN_WARNING "or51132: set_mode error 2\n");
279 return -EREMOTEIO;
280 }
281 dprintk("set #6 to 0x%02x%02x\n", cmd_buf2[1], cmd_buf2[2]);
282
283 return 0;
284}
285
286
287
288#define MOD_FWCLASS_UNKNOWN 0
289#define MOD_FWCLASS_VSB 1
290#define MOD_FWCLASS_QAM 2
291static int modulation_fw_class(enum fe_modulation modulation)
292{
293 switch(modulation) {
294 case VSB_8:
295 return MOD_FWCLASS_VSB;
296 case QAM_AUTO:
297 case QAM_64:
298 case QAM_256:
299 return MOD_FWCLASS_QAM;
300 default:
301 return MOD_FWCLASS_UNKNOWN;
302 }
303}
304
305static int or51132_set_parameters(struct dvb_frontend *fe)
306{
307 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
308 int ret;
309 struct or51132_state* state = fe->demodulator_priv;
310 const struct firmware *fw;
311 const char *fwname;
312 int clock_mode;
313
314
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;
321
322
323 clock_mode = 0;
324 break;
325 case MOD_FWCLASS_QAM:
326 dprintk("set_parameters QAM MODE\n");
327 fwname = OR51132_QAM_FIRMWARE;
328
329
330 clock_mode = 1;
331 break;
332 default:
333 printk("or51132: Modulation type(%d) UNSUPPORTED\n",
334 p->modulation);
335 return -1;
336 }
337 printk("or51132: Waiting for firmware upload(%s)...\n",
338 fwname);
339 ret = request_firmware(&fw, fwname, state->i2c->dev.parent);
340 if (ret) {
341 printk(KERN_WARNING "or51132: No firmware uploaded(timeout or file not found?)\n");
342 return ret;
343 }
344 ret = or51132_load_firmware(fe, fw);
345 release_firmware(fw);
346 if (ret) {
347 printk(KERN_WARNING "or51132: Writing firmware to device failed!\n");
348 return ret;
349 }
350 printk("or51132: Firmware upload complete.\n");
351 state->config->set_ts_params(fe, clock_mode);
352 }
353
354 if (state->current_modulation != p->modulation) {
355 state->current_modulation = p->modulation;
356 or51132_setmode(fe);
357 }
358
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);
362 }
363
364
365 or51132_setmode(fe);
366
367
368 state->current_frequency = p->frequency;
369 return 0;
370}
371
372static int or51132_get_parameters(struct dvb_frontend* fe,
373 struct dtv_frontend_properties *p)
374{
375 struct or51132_state* state = fe->demodulator_priv;
376 int status;
377 int retry = 1;
378
379start:
380
381 if ((status = or51132_readreg(state, 0x00)) < 0) {
382 printk(KERN_WARNING "or51132: get_parameters: error reading receiver status\n");
383 return -EREMOTEIO;
384 }
385 switch(status&0xff) {
386 case 0x06:
387 p->modulation = VSB_8;
388 break;
389 case 0x43:
390 p->modulation = QAM_64;
391 break;
392 case 0x45:
393 p->modulation = QAM_256;
394 break;
395 default:
396 if (retry--)
397 goto start;
398 printk(KERN_WARNING "or51132: unknown status 0x%02x\n",
399 status&0xff);
400 return -EREMOTEIO;
401 }
402
403
404 p->frequency = state->current_frequency;
405
406
407 p->inversion = INVERSION_AUTO;
408
409 return 0;
410}
411
412static int or51132_read_status(struct dvb_frontend *fe, enum fe_status *status)
413{
414 struct or51132_state* state = fe->demodulator_priv;
415 int reg;
416
417
418 if ((reg = or51132_readreg(state, 0x00)) < 0) {
419 printk(KERN_WARNING "or51132: read_status: error reading receiver status: %d\n", reg);
420 *status = 0;
421 return -EREMOTEIO;
422 }
423 dprintk("%s: read_status %04x\n", __func__, reg);
424
425 if (reg & 0x0100)
426 *status = FE_HAS_SIGNAL|FE_HAS_CARRIER|FE_HAS_VITERBI|
427 FE_HAS_SYNC|FE_HAS_LOCK;
428 else
429 *status = 0;
430 return 0;
431}
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454static u32 calculate_snr(u32 mse, u32 c)
455{
456 if (mse == 0)
457 return 0;
458
459 mse = 2*intlog10(mse);
460 if (mse > c) {
461
462
463
464 return 0;
465 }
466 return 10*(c - mse);
467}
468
469static int or51132_read_snr(struct dvb_frontend* fe, u16* snr)
470{
471 struct or51132_state* state = fe->demodulator_priv;
472 int noise, reg;
473 u32 c, usK = 0;
474 int retry = 1;
475
476start:
477
478 noise = or51132_readreg(state, 0x02);
479 if (noise < 0) {
480 printk(KERN_WARNING "or51132: read_snr: error reading equalizer\n");
481 return -EREMOTEIO;
482 }
483 dprintk("read_snr noise (%d)\n", noise);
484
485
486
487 reg = or51132_readreg(state, 0x00);
488 if (reg < 0) {
489 printk(KERN_WARNING "or51132: read_snr: error reading receiver status\n");
490 return -EREMOTEIO;
491 }
492
493 switch (reg&0xff) {
494 case 0x06:
495 if (reg & 0x1000) usK = 3 << 24;
496
497 case 0x43:
498 c = 150204167;
499 break;
500 case 0x45:
501 c = 150290396;
502 break;
503 default:
504 printk(KERN_WARNING "or51132: unknown status 0x%02x\n", reg&0xff);
505 if (retry--) goto start;
506 return -EREMOTEIO;
507 }
508 dprintk("%s: modulation %02x, NTSC rej O%s\n", __func__,
509 reg&0xff, reg&0x1000?"n":"ff");
510
511
512 state->snr = calculate_snr(noise, c) - usK;
513 *snr = (state->snr) >> 16;
514
515 dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __func__, noise,
516 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
517
518 return 0;
519}
520
521static int or51132_read_signal_strength(struct dvb_frontend* fe, u16* strength)
522{
523
524
525
526 struct or51132_state* state = (struct or51132_state*) fe->demodulator_priv;
527 u16 snr;
528 int ret;
529
530 ret = fe->ops.read_snr(fe, &snr);
531 if (ret != 0)
532 return ret;
533
534
535 if (state->snr >= 8960 * 0x10000)
536 *strength = 0xffff;
537 else
538 *strength = state->snr / 8960;
539
540 return 0;
541}
542
543static int or51132_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
544{
545 fe_tune_settings->min_delay_ms = 500;
546 fe_tune_settings->step_size = 0;
547 fe_tune_settings->max_drift = 0;
548
549 return 0;
550}
551
552static void or51132_release(struct dvb_frontend* fe)
553{
554 struct or51132_state* state = fe->demodulator_priv;
555 kfree(state);
556}
557
558static const struct dvb_frontend_ops or51132_ops;
559
560struct dvb_frontend* or51132_attach(const struct or51132_config* config,
561 struct i2c_adapter* i2c)
562{
563 struct or51132_state* state = NULL;
564
565
566 state = kzalloc(sizeof(struct or51132_state), GFP_KERNEL);
567 if (state == NULL)
568 return NULL;
569
570
571 state->config = config;
572 state->i2c = i2c;
573 state->current_frequency = -1;
574 state->current_modulation = -1;
575
576
577 memcpy(&state->frontend.ops, &or51132_ops, sizeof(struct dvb_frontend_ops));
578 state->frontend.demodulator_priv = state;
579 return &state->frontend;
580}
581
582static const struct dvb_frontend_ops or51132_ops = {
583 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
584 .info = {
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 |
592 FE_CAN_8VSB
593 },
594
595 .release = or51132_release,
596
597 .init = or51132_init,
598 .sleep = or51132_sleep,
599
600 .set_frontend = or51132_set_parameters,
601 .get_frontend = or51132_get_parameters,
602 .get_tune_settings = or51132_get_tune_settings,
603
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,
609};
610
611module_param(debug, int, 0644);
612MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
613
614MODULE_DESCRIPTION("OR51132 ATSC [pcHDTV HD-3000] (8VSB & ITU J83 AnnexB FEC QAM64/256) Demodulator Driver");
615MODULE_AUTHOR("Kirk Lapray");
616MODULE_AUTHOR("Trent Piepho");
617MODULE_LICENSE("GPL");
618
619EXPORT_SYMBOL(or51132_attach);
620