2 * Driver for Zarlink DVB-T ZL10353 demodulator
4 * Copyright (C) 2006, 2007 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
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.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <asm/div64.h>
30 #include "dvb_frontend.h"
31 #include "zl10353_priv.h"
34 struct zl10353_state {
35 struct i2c_adapter *i2c;
36 struct dvb_frontend frontend;
38 struct zl10353_config config;
42 #define dprintk(args...) \
44 if (debug) printk(KERN_DEBUG "zl10353: " args); \
47 static int debug_regs = 0;
49 static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
51 struct zl10353_state *state = fe->demodulator_priv;
52 u8 buf[2] = { reg, val };
53 struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
54 .buf = buf, .len = 2 };
55 int err = i2c_transfer(state->i2c, &msg, 1);
57 printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
63 static int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
66 for (i = 0; i < ilen - 1; i++)
67 if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
73 static int zl10353_read_register(struct zl10353_state *state, u8 reg)
78 struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
80 .buf = b0, .len = 1 },
81 { .addr = state->config.demod_address,
83 .buf = b1, .len = 1 } };
85 ret = i2c_transfer(state->i2c, msg, 2);
88 printk("%s: readreg error (reg=%d, ret==%i)\n",
89 __FUNCTION__, reg, ret);
96 static void zl10353_dump_regs(struct dvb_frontend *fe)
98 struct zl10353_state *state = fe->demodulator_priv;
99 char buf[52], buf2[4];
103 /* Dump all registers. */
104 for (reg = 0; ; reg++) {
107 printk(KERN_DEBUG "%s\n", buf);
108 sprintf(buf, "%02x: ", reg);
110 ret = zl10353_read_register(state, reg);
112 sprintf(buf2, "%02x ", (u8)ret);
119 printk(KERN_DEBUG "%s\n", buf);
122 static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
123 enum fe_bandwidth bandwidth,
126 u32 adc_clock = 45056; /* 45.056 MHz */
128 struct zl10353_state *state = fe->demodulator_priv;
130 if (state->config.adc_clock)
131 adc_clock = state->config.adc_clock;
134 case BANDWIDTH_6_MHZ:
137 case BANDWIDTH_7_MHZ:
140 case BANDWIDTH_8_MHZ:
146 *nominal_rate = (bw * (1 << 23) / 7 * 125 + adc_clock / 2) / adc_clock;
148 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
149 __FUNCTION__, bw, adc_clock, *nominal_rate);
152 static void zl10353_calc_input_freq(struct dvb_frontend *fe,
155 struct zl10353_state *state = fe->demodulator_priv;
156 u32 adc_clock = 45056; /* 45.056 MHz */
157 int if2 = 36167; /* 36.167 MHz */
161 if (state->config.adc_clock)
162 adc_clock = state->config.adc_clock;
163 if (state->config.if2)
164 if2 = state->config.if2;
166 if (adc_clock >= if2 * 2)
169 ife = adc_clock - (if2 % adc_clock);
170 if (ife > adc_clock / 2)
171 ife = adc_clock - ife;
173 value = 65536ULL * ife + adc_clock / 2;
174 do_div(value, adc_clock);
175 *input_freq = -value;
177 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
178 __FUNCTION__, if2, ife, adc_clock, -(int)value, *input_freq);
181 static int zl10353_sleep(struct dvb_frontend *fe)
183 static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
185 zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
189 static int zl10353_set_parameters(struct dvb_frontend *fe,
190 struct dvb_frontend_parameters *param)
192 struct zl10353_state *state = fe->demodulator_priv;
193 u16 nominal_rate, input_freq;
194 u8 pllbuf[6] = { 0x67 };
196 /* These settings set "auto-everything" and start the FSM. */
197 zl10353_single_write(fe, 0x55, 0x80);
199 zl10353_single_write(fe, 0xEA, 0x01);
201 zl10353_single_write(fe, 0xEA, 0x00);
203 zl10353_single_write(fe, 0x56, 0x28);
204 zl10353_single_write(fe, 0x89, 0x20);
205 zl10353_single_write(fe, 0x5E, 0x00);
207 zl10353_calc_nominal_rate(fe, param->u.ofdm.bandwidth, &nominal_rate);
208 zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate));
209 zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate));
211 zl10353_calc_input_freq(fe, &input_freq);
212 zl10353_single_write(fe, INPUT_FREQ_1, msb(input_freq));
213 zl10353_single_write(fe, INPUT_FREQ_0, lsb(input_freq));
215 if (fe->ops.i2c_gate_ctrl)
216 fe->ops.i2c_gate_ctrl(fe, 0);
219 * If there is no tuner attached to the secondary I2C bus, we call
220 * set_params to program a potential tuner attached somewhere else.
221 * Otherwise, we update the PLL registers via calc_regs.
223 if (state->config.no_tuner) {
224 if (fe->ops.tuner_ops.set_params) {
225 fe->ops.tuner_ops.set_params(fe, param);
226 if (fe->ops.i2c_gate_ctrl)
227 fe->ops.i2c_gate_ctrl(fe, 0);
229 } else if (fe->ops.tuner_ops.calc_regs) {
230 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf + 1, 5);
232 zl10353_write(fe, pllbuf, sizeof(pllbuf));
235 zl10353_single_write(fe, 0x5F, 0x13);
237 /* If no attached tuner or invalid PLL registers, just start the FSM. */
238 if (state->config.no_tuner || fe->ops.tuner_ops.calc_regs == NULL)
239 zl10353_single_write(fe, FSM_GO, 0x01);
241 zl10353_single_write(fe, TUNER_GO, 0x01);
244 zl10353_single_write(fe, 0xE4, 0x00);
245 zl10353_single_write(fe, 0xE5, 0x2A);
246 zl10353_single_write(fe, 0xE9, 0x02);
247 zl10353_single_write(fe, 0xE7, 0x40);
248 zl10353_single_write(fe, 0xE8, 0x10);
253 static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
255 struct zl10353_state *state = fe->demodulator_priv;
258 if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
260 if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
262 if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
267 *status |= FE_HAS_CARRIER;
269 *status |= FE_HAS_VITERBI;
271 *status |= FE_HAS_LOCK;
273 *status |= FE_HAS_SYNC;
275 *status |= FE_HAS_SIGNAL;
277 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
278 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
279 *status &= ~FE_HAS_LOCK;
284 static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
286 struct zl10353_state *state = fe->demodulator_priv;
288 *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
289 zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
290 zl10353_read_register(state, RS_ERR_CNT_0);
295 static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
297 struct zl10353_state *state = fe->demodulator_priv;
299 u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
300 zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
307 static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
309 struct zl10353_state *state = fe->demodulator_priv;
313 zl10353_dump_regs(fe);
315 _snr = zl10353_read_register(state, SNR);
316 *snr = (_snr << 8) | _snr;
321 static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
323 struct zl10353_state *state = fe->demodulator_priv;
325 *ucblocks = zl10353_read_register(state, RS_UBC_1) << 8 |
326 zl10353_read_register(state, RS_UBC_0);
331 static int zl10353_get_tune_settings(struct dvb_frontend *fe,
332 struct dvb_frontend_tune_settings
335 fe_tune_settings->min_delay_ms = 1000;
336 fe_tune_settings->step_size = 0;
337 fe_tune_settings->max_drift = 0;
342 static int zl10353_init(struct dvb_frontend *fe)
344 struct zl10353_state *state = fe->demodulator_priv;
345 u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
349 zl10353_dump_regs(fe);
350 if (state->config.parallel_ts)
351 zl10353_reset_attach[2] &= ~0x20;
353 /* Do a "hard" reset if not already done */
354 if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
355 zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
356 rc = zl10353_write(fe, zl10353_reset_attach,
357 sizeof(zl10353_reset_attach));
359 zl10353_dump_regs(fe);
365 static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
372 return zl10353_single_write(fe, 0x62, val);
375 static void zl10353_release(struct dvb_frontend *fe)
377 struct zl10353_state *state = fe->demodulator_priv;
381 static struct dvb_frontend_ops zl10353_ops;
383 struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
384 struct i2c_adapter *i2c)
386 struct zl10353_state *state = NULL;
388 /* allocate memory for the internal state */
389 state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
393 /* setup the state */
395 memcpy(&state->config, config, sizeof(struct zl10353_config));
397 /* check if the demod is there */
398 if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
401 /* create dvb_frontend */
402 memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
403 state->frontend.demodulator_priv = state;
405 return &state->frontend;
411 static struct dvb_frontend_ops zl10353_ops = {
414 .name = "Zarlink ZL10353 DVB-T",
416 .frequency_min = 174000000,
417 .frequency_max = 862000000,
418 .frequency_stepsize = 166667,
419 .frequency_tolerance = 0,
420 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
421 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
423 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
424 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
425 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
429 .release = zl10353_release,
431 .init = zl10353_init,
432 .sleep = zl10353_sleep,
433 .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
434 .write = zl10353_write,
436 .set_frontend = zl10353_set_parameters,
437 .get_tune_settings = zl10353_get_tune_settings,
439 .read_status = zl10353_read_status,
440 .read_ber = zl10353_read_ber,
441 .read_signal_strength = zl10353_read_signal_strength,
442 .read_snr = zl10353_read_snr,
443 .read_ucblocks = zl10353_read_ucblocks,
446 module_param(debug, int, 0644);
447 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
449 module_param(debug_regs, int, 0644);
450 MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
452 MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
453 MODULE_AUTHOR("Chris Pascoe");
454 MODULE_LICENSE("GPL");
456 EXPORT_SYMBOL(zl10353_attach);