1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Zarlink DVB-T ZL10353 demodulator
5 * Copyright (C) 2006, 2007 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <asm/div64.h>
16 #include <media/dvb_frontend.h>
17 #include "zl10353_priv.h"
20 struct zl10353_state {
21 struct i2c_adapter *i2c;
22 struct dvb_frontend frontend;
24 struct zl10353_config config;
32 #define dprintk(args...) \
34 if (debug) printk(KERN_DEBUG "zl10353: " args); \
37 static int debug_regs;
39 static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
41 struct zl10353_state *state = fe->demodulator_priv;
42 u8 buf[2] = { reg, val };
43 struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
44 .buf = buf, .len = 2 };
45 int err = i2c_transfer(state->i2c, &msg, 1);
47 printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
53 static int zl10353_write(struct dvb_frontend *fe, const u8 ibuf[], int ilen)
56 for (i = 0; i < ilen - 1; i++)
57 if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
63 static int zl10353_read_register(struct zl10353_state *state, u8 reg)
68 struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
70 .buf = b0, .len = 1 },
71 { .addr = state->config.demod_address,
73 .buf = b1, .len = 1 } };
75 ret = i2c_transfer(state->i2c, msg, 2);
78 printk("%s: readreg error (reg=%d, ret==%i)\n",
86 static void zl10353_dump_regs(struct dvb_frontend *fe)
88 struct zl10353_state *state = fe->demodulator_priv;
92 /* Dump all registers. */
93 for (reg = 0; ; reg++) {
96 printk(KERN_CONT "\n");
97 printk(KERN_DEBUG "%02x:", reg);
99 ret = zl10353_read_register(state, reg);
101 printk(KERN_CONT " %02x", (u8)ret);
103 printk(KERN_CONT " --");
107 printk(KERN_CONT "\n");
110 static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
114 struct zl10353_state *state = fe->demodulator_priv;
115 u32 adc_clock = 450560; /* 45.056 MHz */
117 u8 bw = bandwidth / 1000000;
119 if (state->config.adc_clock)
120 adc_clock = state->config.adc_clock;
122 value = (u64)10 * (1 << 23) / 7 * 125;
123 value = (bw * value) + adc_clock / 2;
124 *nominal_rate = div_u64(value, adc_clock);
126 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
127 __func__, bw, adc_clock, *nominal_rate);
130 static void zl10353_calc_input_freq(struct dvb_frontend *fe,
133 struct zl10353_state *state = fe->demodulator_priv;
134 u32 adc_clock = 450560; /* 45.056 MHz */
135 int if2 = 361667; /* 36.1667 MHz */
139 if (state->config.adc_clock)
140 adc_clock = state->config.adc_clock;
141 if (state->config.if2)
142 if2 = state->config.if2;
144 if (adc_clock >= if2 * 2)
147 ife = adc_clock - (if2 % adc_clock);
148 if (ife > adc_clock / 2)
149 ife = adc_clock - ife;
151 value = div_u64((u64)65536 * ife + adc_clock / 2, adc_clock);
152 *input_freq = -value;
154 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
155 __func__, if2, ife, adc_clock, -(int)value, *input_freq);
158 static int zl10353_sleep(struct dvb_frontend *fe)
160 static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
162 zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
166 static int zl10353_set_parameters(struct dvb_frontend *fe)
168 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
169 struct zl10353_state *state = fe->demodulator_priv;
170 u16 nominal_rate, input_freq;
171 u8 pllbuf[6] = { 0x67 }, acq_ctl = 0;
174 state->frequency = c->frequency;
176 zl10353_single_write(fe, RESET, 0x80);
178 zl10353_single_write(fe, 0xEA, 0x01);
180 zl10353_single_write(fe, 0xEA, 0x00);
182 zl10353_single_write(fe, AGC_TARGET, 0x28);
184 if (c->transmission_mode != TRANSMISSION_MODE_AUTO)
186 if (c->guard_interval != GUARD_INTERVAL_AUTO)
188 zl10353_single_write(fe, ACQ_CTL, acq_ctl);
190 switch (c->bandwidth_hz) {
192 /* These are extrapolated from the 7 and 8MHz values */
193 zl10353_single_write(fe, MCLK_RATIO, 0x97);
194 zl10353_single_write(fe, 0x64, 0x34);
195 zl10353_single_write(fe, 0xcc, 0xdd);
198 zl10353_single_write(fe, MCLK_RATIO, 0x86);
199 zl10353_single_write(fe, 0x64, 0x35);
200 zl10353_single_write(fe, 0xcc, 0x73);
203 c->bandwidth_hz = 8000000;
206 zl10353_single_write(fe, MCLK_RATIO, 0x75);
207 zl10353_single_write(fe, 0x64, 0x36);
208 zl10353_single_write(fe, 0xcc, 0x73);
211 zl10353_calc_nominal_rate(fe, c->bandwidth_hz, &nominal_rate);
212 zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate));
213 zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate));
214 state->bandwidth = c->bandwidth_hz;
216 zl10353_calc_input_freq(fe, &input_freq);
217 zl10353_single_write(fe, INPUT_FREQ_1, msb(input_freq));
218 zl10353_single_write(fe, INPUT_FREQ_0, lsb(input_freq));
220 /* Hint at TPS settings */
221 switch (c->code_rate_HP) {
241 switch (c->code_rate_LP) {
258 if (c->hierarchy == HIERARCHY_AUTO ||
259 c->hierarchy == HIERARCHY_NONE)
266 switch (c->modulation) {
280 switch (c->transmission_mode) {
281 case TRANSMISSION_MODE_2K:
282 case TRANSMISSION_MODE_AUTO:
284 case TRANSMISSION_MODE_8K:
291 switch (c->guard_interval) {
292 case GUARD_INTERVAL_1_32:
293 case GUARD_INTERVAL_AUTO:
295 case GUARD_INTERVAL_1_16:
298 case GUARD_INTERVAL_1_8:
301 case GUARD_INTERVAL_1_4:
308 switch (c->hierarchy) {
325 zl10353_single_write(fe, TPS_GIVEN_1, msb(tps));
326 zl10353_single_write(fe, TPS_GIVEN_0, lsb(tps));
328 if (fe->ops.i2c_gate_ctrl)
329 fe->ops.i2c_gate_ctrl(fe, 0);
332 * If there is no tuner attached to the secondary I2C bus, we call
333 * set_params to program a potential tuner attached somewhere else.
334 * Otherwise, we update the PLL registers via calc_regs.
336 if (state->config.no_tuner) {
337 if (fe->ops.tuner_ops.set_params) {
338 fe->ops.tuner_ops.set_params(fe);
339 if (fe->ops.i2c_gate_ctrl)
340 fe->ops.i2c_gate_ctrl(fe, 0);
342 } else if (fe->ops.tuner_ops.calc_regs) {
343 fe->ops.tuner_ops.calc_regs(fe, pllbuf + 1, 5);
345 zl10353_write(fe, pllbuf, sizeof(pllbuf));
348 zl10353_single_write(fe, 0x5F, 0x13);
350 /* If no attached tuner or invalid PLL registers, just start the FSM. */
351 if (state->config.no_tuner || fe->ops.tuner_ops.calc_regs == NULL)
352 zl10353_single_write(fe, FSM_GO, 0x01);
354 zl10353_single_write(fe, TUNER_GO, 0x01);
359 static int zl10353_get_parameters(struct dvb_frontend *fe,
360 struct dtv_frontend_properties *c)
362 struct zl10353_state *state = fe->demodulator_priv;
365 static const u8 tps_fec_to_api[8] = {
376 s6 = zl10353_read_register(state, STATUS_6);
377 s9 = zl10353_read_register(state, STATUS_9);
378 if (s6 < 0 || s9 < 0)
380 if ((s6 & (1 << 5)) == 0 || (s9 & (1 << 4)) == 0)
381 return -EINVAL; /* no FE or TPS lock */
383 tps = zl10353_read_register(state, TPS_RECEIVED_1) << 8 |
384 zl10353_read_register(state, TPS_RECEIVED_0);
386 c->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
387 c->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
389 switch ((tps >> 13) & 3) {
391 c->modulation = QPSK;
394 c->modulation = QAM_16;
397 c->modulation = QAM_64;
400 c->modulation = QAM_AUTO;
404 c->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K :
405 TRANSMISSION_MODE_2K;
407 switch ((tps >> 2) & 3) {
409 c->guard_interval = GUARD_INTERVAL_1_32;
412 c->guard_interval = GUARD_INTERVAL_1_16;
415 c->guard_interval = GUARD_INTERVAL_1_8;
418 c->guard_interval = GUARD_INTERVAL_1_4;
421 c->guard_interval = GUARD_INTERVAL_AUTO;
425 switch ((tps >> 10) & 7) {
427 c->hierarchy = HIERARCHY_NONE;
430 c->hierarchy = HIERARCHY_1;
433 c->hierarchy = HIERARCHY_2;
436 c->hierarchy = HIERARCHY_4;
439 c->hierarchy = HIERARCHY_AUTO;
443 c->frequency = state->frequency;
444 c->bandwidth_hz = state->bandwidth;
445 c->inversion = INVERSION_AUTO;
450 static int zl10353_read_status(struct dvb_frontend *fe, enum fe_status *status)
452 struct zl10353_state *state = fe->demodulator_priv;
455 if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
457 if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
459 if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
464 *status |= FE_HAS_CARRIER;
466 *status |= FE_HAS_VITERBI;
468 *status |= FE_HAS_LOCK;
470 *status |= FE_HAS_SYNC;
472 *status |= FE_HAS_SIGNAL;
474 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
475 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
476 *status &= ~FE_HAS_LOCK;
481 static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
483 struct zl10353_state *state = fe->demodulator_priv;
485 *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
486 zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
487 zl10353_read_register(state, RS_ERR_CNT_0);
492 static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
494 struct zl10353_state *state = fe->demodulator_priv;
496 u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
497 zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
504 static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
506 struct zl10353_state *state = fe->demodulator_priv;
510 zl10353_dump_regs(fe);
512 _snr = zl10353_read_register(state, SNR);
513 *snr = 10 * _snr / 8;
518 static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
520 struct zl10353_state *state = fe->demodulator_priv;
523 ubl = zl10353_read_register(state, RS_UBC_1) << 8 |
524 zl10353_read_register(state, RS_UBC_0);
526 state->ucblocks += ubl;
527 *ucblocks = state->ucblocks;
532 static int zl10353_get_tune_settings(struct dvb_frontend *fe,
533 struct dvb_frontend_tune_settings
536 fe_tune_settings->min_delay_ms = 1000;
537 fe_tune_settings->step_size = 0;
538 fe_tune_settings->max_drift = 0;
543 static int zl10353_init(struct dvb_frontend *fe)
545 struct zl10353_state *state = fe->demodulator_priv;
546 u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
549 zl10353_dump_regs(fe);
550 if (state->config.parallel_ts)
551 zl10353_reset_attach[2] &= ~0x20;
552 if (state->config.clock_ctl_1)
553 zl10353_reset_attach[3] = state->config.clock_ctl_1;
554 if (state->config.pll_0)
555 zl10353_reset_attach[4] = state->config.pll_0;
557 /* Do a "hard" reset if not already done */
558 if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
559 zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
560 zl10353_write(fe, zl10353_reset_attach,
561 sizeof(zl10353_reset_attach));
563 zl10353_dump_regs(fe);
569 static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
571 struct zl10353_state *state = fe->demodulator_priv;
574 if (state->config.disable_i2c_gate_ctrl) {
575 /* No tuner attached to the internal I2C bus */
576 /* If set enable I2C bridge, the main I2C bus stopped hardly */
583 return zl10353_single_write(fe, 0x62, val);
586 static void zl10353_release(struct dvb_frontend *fe)
588 struct zl10353_state *state = fe->demodulator_priv;
592 static const struct dvb_frontend_ops zl10353_ops;
594 struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
595 struct i2c_adapter *i2c)
597 struct zl10353_state *state = NULL;
600 /* allocate memory for the internal state */
601 state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
605 /* setup the state */
607 memcpy(&state->config, config, sizeof(struct zl10353_config));
609 /* check if the demod is there */
610 id = zl10353_read_register(state, CHIP_ID);
611 if ((id != ID_ZL10353) && (id != ID_CE6230) && (id != ID_CE6231))
614 /* create dvb_frontend */
615 memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
616 state->frontend.demodulator_priv = state;
618 return &state->frontend;
624 static const struct dvb_frontend_ops zl10353_ops = {
625 .delsys = { SYS_DVBT },
627 .name = "Zarlink ZL10353 DVB-T",
628 .frequency_min_hz = 174 * MHz,
629 .frequency_max_hz = 862 * MHz,
630 .frequency_stepsize_hz = 166667,
631 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
632 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
634 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
635 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
636 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
640 .release = zl10353_release,
642 .init = zl10353_init,
643 .sleep = zl10353_sleep,
644 .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
645 .write = zl10353_write,
647 .set_frontend = zl10353_set_parameters,
648 .get_frontend = zl10353_get_parameters,
649 .get_tune_settings = zl10353_get_tune_settings,
651 .read_status = zl10353_read_status,
652 .read_ber = zl10353_read_ber,
653 .read_signal_strength = zl10353_read_signal_strength,
654 .read_snr = zl10353_read_snr,
655 .read_ucblocks = zl10353_read_ucblocks,
658 module_param(debug, int, 0644);
659 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
661 module_param(debug_regs, int, 0644);
662 MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
664 MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
665 MODULE_AUTHOR("Chris Pascoe");
666 MODULE_LICENSE("GPL");
668 EXPORT_SYMBOL(zl10353_attach);