2 * Driver for Zarlink DVB-T ZL10353 demodulator
4 * Copyright (C) 2006 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>
29 #include "dvb_frontend.h"
30 #include "zl10353_priv.h"
33 struct zl10353_state {
34 struct i2c_adapter *i2c;
35 struct dvb_frontend frontend;
37 struct zl10353_config config;
41 #define dprintk(args...) \
43 if (debug) printk(KERN_DEBUG "zl10353: " args); \
46 static int debug_regs = 0;
48 static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
50 struct zl10353_state *state = fe->demodulator_priv;
51 u8 buf[2] = { reg, val };
52 struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
53 .buf = buf, .len = 2 };
54 int err = i2c_transfer(state->i2c, &msg, 1);
56 printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
62 static int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
65 for (i = 0; i < ilen - 1; i++)
66 if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
72 static int zl10353_read_register(struct zl10353_state *state, u8 reg)
77 struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
79 .buf = b0, .len = 1 },
80 { .addr = state->config.demod_address,
82 .buf = b1, .len = 1 } };
84 ret = i2c_transfer(state->i2c, msg, 2);
87 printk("%s: readreg error (reg=%d, ret==%i)\n",
88 __FUNCTION__, reg, ret);
95 static void zl10353_dump_regs(struct dvb_frontend *fe)
97 struct zl10353_state *state = fe->demodulator_priv;
98 char buf[52], buf2[4];
102 /* Dump all registers. */
103 for (reg = 0; ; reg++) {
106 printk(KERN_DEBUG "%s\n", buf);
107 sprintf(buf, "%02x: ", reg);
109 ret = zl10353_read_register(state, reg);
111 sprintf(buf2, "%02x ", (u8)ret);
118 printk(KERN_DEBUG "%s\n", buf);
121 static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
122 enum fe_bandwidth bandwidth,
125 u32 adc_clock = 22528; /* 20.480 MHz on the board(!?) */
127 struct zl10353_state *state = fe->demodulator_priv;
129 if (state->config.adc_clock)
130 adc_clock = state->config.adc_clock;
133 case BANDWIDTH_6_MHZ:
136 case BANDWIDTH_7_MHZ:
139 case BANDWIDTH_8_MHZ:
145 *nominal_rate = (64 * bw * (1<<16) / (7 * 8) * 4000 / adc_clock + 2) / 4;
147 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
148 __FUNCTION__, bw, adc_clock, *nominal_rate);
151 static int zl10353_sleep(struct dvb_frontend *fe)
153 static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
155 zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
159 static int zl10353_set_parameters(struct dvb_frontend *fe,
160 struct dvb_frontend_parameters *param)
162 struct zl10353_state *state = fe->demodulator_priv;
164 u8 pllbuf[6] = { 0x67 };
166 /* These settings set "auto-everything" and start the FSM. */
167 zl10353_single_write(fe, 0x55, 0x80);
169 zl10353_single_write(fe, 0xEA, 0x01);
171 zl10353_single_write(fe, 0xEA, 0x00);
173 zl10353_single_write(fe, 0x56, 0x28);
174 zl10353_single_write(fe, 0x89, 0x20);
175 zl10353_single_write(fe, 0x5E, 0x00);
177 zl10353_calc_nominal_rate(fe, param->u.ofdm.bandwidth, &nominal_rate);
178 zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate));
179 zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate));
181 zl10353_single_write(fe, 0x6C, 0xCD);
182 zl10353_single_write(fe, 0x6D, 0x7E);
183 if (fe->ops.i2c_gate_ctrl)
184 fe->ops.i2c_gate_ctrl(fe, 0);
186 // if there is no attached secondary tuner, we call set_params to program
187 // a potential tuner attached somewhere else
188 if (state->config.no_tuner) {
189 if (fe->ops.tuner_ops.set_params) {
190 fe->ops.tuner_ops.set_params(fe, param);
191 if (fe->ops.i2c_gate_ctrl)
192 fe->ops.i2c_gate_ctrl(fe, 0);
196 // if pllbuf is defined, retrieve the settings
197 if (fe->ops.tuner_ops.calc_regs) {
198 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf+1, 5);
201 // fake pllbuf settings
202 pllbuf[1] = 0x61 << 1;
209 // there is no call to _just_ start decoding, so we send the pllbuf anyway
210 // even if there isn't a PLL attached to the secondary bus
211 zl10353_write(fe, pllbuf, sizeof(pllbuf));
213 zl10353_single_write(fe, 0x5F, 0x13);
214 zl10353_single_write(fe, 0x70, 0x01);
216 zl10353_single_write(fe, 0xE4, 0x00);
217 zl10353_single_write(fe, 0xE5, 0x2A);
218 zl10353_single_write(fe, 0xE9, 0x02);
219 zl10353_single_write(fe, 0xE7, 0x40);
220 zl10353_single_write(fe, 0xE8, 0x10);
225 static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
227 struct zl10353_state *state = fe->demodulator_priv;
230 if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
232 if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
234 if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
239 *status |= FE_HAS_CARRIER;
241 *status |= FE_HAS_VITERBI;
243 *status |= FE_HAS_LOCK;
245 *status |= FE_HAS_SYNC;
247 *status |= FE_HAS_SIGNAL;
249 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
250 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
251 *status &= ~FE_HAS_LOCK;
256 static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
258 struct zl10353_state *state = fe->demodulator_priv;
260 *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
261 zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
262 zl10353_read_register(state, RS_ERR_CNT_0);
267 static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
269 struct zl10353_state *state = fe->demodulator_priv;
271 u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
272 zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
279 static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
281 struct zl10353_state *state = fe->demodulator_priv;
285 zl10353_dump_regs(fe);
287 _snr = zl10353_read_register(state, SNR);
288 *snr = (_snr << 8) | _snr;
293 static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
295 struct zl10353_state *state = fe->demodulator_priv;
297 *ucblocks = zl10353_read_register(state, RS_UBC_1) << 8 |
298 zl10353_read_register(state, RS_UBC_0);
303 static int zl10353_get_tune_settings(struct dvb_frontend *fe,
304 struct dvb_frontend_tune_settings
307 fe_tune_settings->min_delay_ms = 1000;
308 fe_tune_settings->step_size = 0;
309 fe_tune_settings->max_drift = 0;
314 static int zl10353_init(struct dvb_frontend *fe)
316 struct zl10353_state *state = fe->demodulator_priv;
317 u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
321 zl10353_dump_regs(fe);
322 if (state->config.parallel_ts)
323 zl10353_reset_attach[2] &= ~0x20;
325 /* Do a "hard" reset if not already done */
326 if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
327 zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
328 rc = zl10353_write(fe, zl10353_reset_attach,
329 sizeof(zl10353_reset_attach));
331 zl10353_dump_regs(fe);
337 static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
344 return zl10353_single_write(fe, 0x62, val);
347 static void zl10353_release(struct dvb_frontend *fe)
349 struct zl10353_state *state = fe->demodulator_priv;
353 static struct dvb_frontend_ops zl10353_ops;
355 struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
356 struct i2c_adapter *i2c)
358 struct zl10353_state *state = NULL;
360 /* allocate memory for the internal state */
361 state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
365 /* setup the state */
367 memcpy(&state->config, config, sizeof(struct zl10353_config));
369 /* check if the demod is there */
370 if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
373 /* create dvb_frontend */
374 memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
375 state->frontend.demodulator_priv = state;
377 return &state->frontend;
383 static struct dvb_frontend_ops zl10353_ops = {
386 .name = "Zarlink ZL10353 DVB-T",
388 .frequency_min = 174000000,
389 .frequency_max = 862000000,
390 .frequency_stepsize = 166667,
391 .frequency_tolerance = 0,
392 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
393 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
395 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
396 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
397 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
401 .release = zl10353_release,
403 .init = zl10353_init,
404 .sleep = zl10353_sleep,
405 .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
406 .write = zl10353_write,
408 .set_frontend = zl10353_set_parameters,
409 .get_tune_settings = zl10353_get_tune_settings,
411 .read_status = zl10353_read_status,
412 .read_ber = zl10353_read_ber,
413 .read_signal_strength = zl10353_read_signal_strength,
414 .read_snr = zl10353_read_snr,
415 .read_ucblocks = zl10353_read_ucblocks,
418 module_param(debug, int, 0644);
419 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
421 module_param(debug_regs, int, 0644);
422 MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
424 MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
425 MODULE_AUTHOR("Chris Pascoe");
426 MODULE_LICENSE("GPL");
428 EXPORT_SYMBOL(zl10353_attach);