2 Driver for Spase SP8870 demodulator
4 Copyright (C) 1999 Juergen Peitz
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.
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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * This driver needs external firmware. Please use the command
24 * "<kerneldir>/Documentation/dvb/get_dvb_firmware alps_tdlb7" to
25 * download/extract it, and then copy it to /usr/lib/hotplug/firmware.
27 #define SP8870_DEFAULT_FIRMWARE "dvb-fe-sp8870.fw"
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/device.h>
33 #include <linux/firmware.h>
34 #include <linux/delay.h>
35 #include <linux/string.h>
36 #include <linux/slab.h>
38 #include "dvb_frontend.h"
44 struct i2c_adapter* i2c;
46 struct dvb_frontend_ops ops;
48 const struct sp8870_config* config;
50 struct dvb_frontend frontend;
52 /* demodulator private data */
57 #define dprintk(args...) \
59 if (debug) printk(KERN_DEBUG "sp8870: " args); \
62 /* firmware size for sp8870 */
63 #define SP8870_FIRMWARE_SIZE 16382
65 /* starting point for firmware in file 'Sc_main.mc' */
66 #define SP8870_FIRMWARE_OFFSET 0x0A
68 static int sp8870_writereg (struct sp8870_state* state, u16 reg, u16 data)
70 u8 buf [] = { reg >> 8, reg & 0xff, data >> 8, data & 0xff };
71 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 4 };
74 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
75 dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __FUNCTION__, err, reg, data);
82 static int sp8870_readreg (struct sp8870_state* state, u16 reg)
85 u8 b0 [] = { reg >> 8 , reg & 0xff };
87 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 },
88 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 2 } };
90 ret = i2c_transfer (state->i2c, msg, 2);
93 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
97 return (b1[0] << 8 | b1[1]);
100 static int sp8870_firmware_upload (struct sp8870_state* state, const struct firmware *fw)
103 char *fw_buf = fw->data;
109 dprintk ("%s: ...\n", __FUNCTION__);
111 if (fw->size < SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET)
114 // system controller stop
115 sp8870_writereg(state, 0x0F00, 0x0000);
117 // instruction RAM register hiword
118 sp8870_writereg(state, 0x8F08, ((SP8870_FIRMWARE_SIZE / 2) & 0xFFFF));
120 // instruction RAM MWR
121 sp8870_writereg(state, 0x8F0A, ((SP8870_FIRMWARE_SIZE / 2) >> 16));
123 // do firmware upload
124 fw_pos = SP8870_FIRMWARE_OFFSET;
125 while (fw_pos < SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET){
126 tx_len = (fw_pos <= SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET - 252) ? 252 : SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET - fw_pos;
127 // write register 0xCF0A
130 memcpy(&tx_buf[2], fw_buf + fw_pos, tx_len);
131 msg.addr = state->config->demod_address;
134 msg.len = tx_len + 2;
135 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
136 printk("%s: firmware upload failed!\n", __FUNCTION__);
137 printk ("%s: i2c error (err == %i)\n", __FUNCTION__, err);
143 dprintk ("%s: done!\n", __FUNCTION__);
147 static void sp8870_microcontroller_stop (struct sp8870_state* state)
149 sp8870_writereg(state, 0x0F08, 0x000);
150 sp8870_writereg(state, 0x0F09, 0x000);
152 // microcontroller STOP
153 sp8870_writereg(state, 0x0F00, 0x000);
156 static void sp8870_microcontroller_start (struct sp8870_state* state)
158 sp8870_writereg(state, 0x0F08, 0x000);
159 sp8870_writereg(state, 0x0F09, 0x000);
161 // microcontroller START
162 sp8870_writereg(state, 0x0F00, 0x001);
163 // not documented but if we don't read 0x0D01 out here
164 // we don't get a correct data valid signal
165 sp8870_readreg(state, 0x0D01);
168 static int sp8870_read_data_valid_signal(struct sp8870_state* state)
170 return (sp8870_readreg(state, 0x0D02) > 0);
173 static int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
175 int known_parameters = 1;
179 switch (p->u.ofdm.constellation) {
183 *reg0xc05 |= (1 << 10);
186 *reg0xc05 |= (2 << 10);
189 known_parameters = 0;
195 switch (p->u.ofdm.hierarchy_information) {
199 *reg0xc05 |= (1 << 7);
202 *reg0xc05 |= (2 << 7);
205 *reg0xc05 |= (3 << 7);
208 known_parameters = 0;
214 switch (p->u.ofdm.code_rate_HP) {
218 *reg0xc05 |= (1 << 3);
221 *reg0xc05 |= (2 << 3);
224 *reg0xc05 |= (3 << 3);
227 *reg0xc05 |= (4 << 3);
230 known_parameters = 0;
236 if (known_parameters)
237 *reg0xc05 |= (2 << 1); /* use specified parameters */
239 *reg0xc05 |= (1 << 1); /* enable autoprobing */
244 static int sp8870_wake_up(struct sp8870_state* state)
246 // enable TS output and interface pins
247 return sp8870_writereg(state, 0xC18, 0x00D);
250 static int sp8870_set_frontend_parameters (struct dvb_frontend* fe,
251 struct dvb_frontend_parameters *p)
253 struct sp8870_state* state = fe->demodulator_priv;
257 if ((err = configure_reg0xc05(p, ®0xc05)))
260 // system controller stop
261 sp8870_microcontroller_stop(state);
263 // set tuner parameters
264 sp8870_writereg(state, 0x206, 0x001);
265 state->config->pll_set(fe, p);
266 sp8870_writereg(state, 0x206, 0x000);
268 // sample rate correction bit [23..17]
269 sp8870_writereg(state, 0x0319, 0x000A);
271 // sample rate correction bit [16..0]
272 sp8870_writereg(state, 0x031A, 0x0AAB);
274 // integer carrier offset
275 sp8870_writereg(state, 0x0309, 0x0400);
277 // fractional carrier offset
278 sp8870_writereg(state, 0x030A, 0x0000);
280 // filter for 6/7/8 Mhz channel
281 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
282 sp8870_writereg(state, 0x0311, 0x0002);
283 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
284 sp8870_writereg(state, 0x0311, 0x0001);
286 sp8870_writereg(state, 0x0311, 0x0000);
288 // scan order: 2k first = 0x0000, 8k first = 0x0001
289 if (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_2K)
290 sp8870_writereg(state, 0x0338, 0x0000);
292 sp8870_writereg(state, 0x0338, 0x0001);
294 sp8870_writereg(state, 0xc05, reg0xc05);
296 // read status reg in order to clear pending irqs
297 sp8870_readreg(state, 0x200);
299 // system controller start
300 sp8870_microcontroller_start(state);
305 static int sp8870_init (struct dvb_frontend* fe)
307 struct sp8870_state* state = fe->demodulator_priv;
308 const struct firmware *fw = NULL;
310 sp8870_wake_up(state);
311 if (state->initialised) return 0;
312 state->initialised = 1;
314 dprintk ("%s\n", __FUNCTION__);
317 /* request the firmware, this will block until someone uploads it */
318 printk("sp8870: waiting for firmware upload (%s)...\n", SP8870_DEFAULT_FIRMWARE);
319 if (state->config->request_firmware(fe, &fw, SP8870_DEFAULT_FIRMWARE)) {
320 printk("sp8870: no firmware upload (timeout or file not found?)\n");
321 release_firmware(fw);
325 if (sp8870_firmware_upload(state, fw)) {
326 printk("sp8870: writing firmware to device failed\n");
327 release_firmware(fw);
330 printk("sp8870: firmware upload complete\n");
332 /* enable TS output and interface pins */
333 sp8870_writereg(state, 0xc18, 0x00d);
335 // system controller stop
336 sp8870_microcontroller_stop(state);
339 sp8870_writereg(state, 0x0301, 0x0003);
341 // Reed Solomon parity bytes passed to output
342 sp8870_writereg(state, 0x0C13, 0x0001);
344 // MPEG clock is suppressed if no valid data
345 sp8870_writereg(state, 0x0C14, 0x0001);
347 /* bit 0x010: enable data valid signal */
348 sp8870_writereg(state, 0x0D00, 0x010);
349 sp8870_writereg(state, 0x0D01, 0x000);
352 if (state->config->pll_init) {
353 sp8870_writereg(state, 0x206, 0x001);
354 state->config->pll_init(fe);
355 sp8870_writereg(state, 0x206, 0x000);
361 static int sp8870_read_status (struct dvb_frontend* fe, fe_status_t * fe_status)
363 struct sp8870_state* state = fe->demodulator_priv;
369 status = sp8870_readreg (state, 0x0200);
373 signal = sp8870_readreg (state, 0x0303);
378 *fe_status |= FE_HAS_SIGNAL;
380 *fe_status |= FE_HAS_SYNC;
382 *fe_status |= FE_HAS_LOCK | FE_HAS_CARRIER | FE_HAS_VITERBI;
387 static int sp8870_read_ber (struct dvb_frontend* fe, u32 * ber)
389 struct sp8870_state* state = fe->demodulator_priv;
395 ret = sp8870_readreg(state, 0xC08);
401 ret = sp8870_readreg(state, 0xC07);
415 static int sp8870_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
417 struct sp8870_state* state = fe->demodulator_priv;
423 ret = sp8870_readreg (state, 0x306);
429 ret = sp8870_readreg (state, 0x303);
436 *signal = 0xFFFF - tmp;
441 static int sp8870_read_uncorrected_blocks (struct dvb_frontend* fe, u32* ublocks)
443 struct sp8870_state* state = fe->demodulator_priv;
448 ret = sp8870_readreg(state, 0xC0C);
460 // number of trials to recover from lockup
462 // maximum checks for data valid signal
463 #define MAXCHECKS 100
465 // only for debugging: counter for detected lockups
466 static int lockups = 0;
467 // only for debugging: counter for channel switches
468 static int switches = 0;
470 static int sp8870_set_frontend (struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
472 struct sp8870_state* state = fe->demodulator_priv;
475 The firmware of the sp8870 sometimes locks up after setting frontend parameters.
476 We try to detect this by checking the data valid signal.
477 If it is not set after MAXCHECKS we try to recover the lockup by setting
478 the frontend parameters again.
486 dprintk("%s: frequency = %i\n", __FUNCTION__, p->frequency);
488 for (trials = 1; trials <= MAXTRIALS; trials++) {
490 if ((err = sp8870_set_frontend_parameters(fe, p)))
493 for (check_count = 0; check_count < MAXCHECKS; check_count++) {
494 // valid = ((sp8870_readreg(i2c, 0x0200) & 4) == 0);
495 valid = sp8870_read_data_valid_signal(state);
497 dprintk("%s: delay = %i usec\n",
498 __FUNCTION__, check_count * 10);
508 printk("%s: firmware crash!!!!!!\n", __FUNCTION__);
515 printk("%s: firmware lockup!!!\n", __FUNCTION__);
516 printk("%s: recovered after %i trial(s))\n", __FUNCTION__, trials - 1);
521 printk("%s: switches = %i lockups = %i\n", __FUNCTION__, switches, lockups);
527 static int sp8870_sleep(struct dvb_frontend* fe)
529 struct sp8870_state* state = fe->demodulator_priv;
531 // tristate TS output and disable interface pins
532 return sp8870_writereg(state, 0xC18, 0x000);
535 static int sp8870_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
537 fesettings->min_delay_ms = 350;
538 fesettings->step_size = 0;
539 fesettings->max_drift = 0;
543 static void sp8870_release(struct dvb_frontend* fe)
545 struct sp8870_state* state = fe->demodulator_priv;
549 static struct dvb_frontend_ops sp8870_ops;
551 struct dvb_frontend* sp8870_attach(const struct sp8870_config* config,
552 struct i2c_adapter* i2c)
554 struct sp8870_state* state = NULL;
556 /* allocate memory for the internal state */
557 state = kmalloc(sizeof(struct sp8870_state), GFP_KERNEL);
558 if (state == NULL) goto error;
560 /* setup the state */
561 state->config = config;
563 memcpy(&state->ops, &sp8870_ops, sizeof(struct dvb_frontend_ops));
564 state->initialised = 0;
566 /* check if the demod is there */
567 if (sp8870_readreg(state, 0x0200) < 0) goto error;
569 /* create dvb_frontend */
570 state->frontend.ops = &state->ops;
571 state->frontend.demodulator_priv = state;
572 return &state->frontend;
579 static struct dvb_frontend_ops sp8870_ops = {
582 .name = "Spase SP8870 DVB-T",
584 .frequency_min = 470000000,
585 .frequency_max = 860000000,
586 .frequency_stepsize = 166666,
587 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
588 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 |
589 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
590 FE_CAN_QPSK | FE_CAN_QAM_16 |
591 FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
592 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER
595 .release = sp8870_release,
598 .sleep = sp8870_sleep,
600 .set_frontend = sp8870_set_frontend,
601 .get_tune_settings = sp8870_get_tune_settings,
603 .read_status = sp8870_read_status,
604 .read_ber = sp8870_read_ber,
605 .read_signal_strength = sp8870_read_signal_strength,
606 .read_ucblocks = sp8870_read_uncorrected_blocks,
609 module_param(debug, int, 0644);
610 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
612 MODULE_DESCRIPTION("Spase SP8870 DVB-T Demodulator driver");
613 MODULE_AUTHOR("Juergen Peitz");
614 MODULE_LICENSE("GPL");
616 EXPORT_SYMBOL(sp8870_attach);