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
;
46 #define dprintk(args...) \
48 if (debug) printk(KERN_DEBUG "zl10353: " args); \
51 static int debug_regs
;
53 static int zl10353_single_write(struct dvb_frontend
*fe
, u8 reg
, u8 val
)
55 struct zl10353_state
*state
= fe
->demodulator_priv
;
56 u8 buf
[2] = { reg
, val
};
57 struct i2c_msg msg
= { .addr
= state
->config
.demod_address
, .flags
= 0,
58 .buf
= buf
, .len
= 2 };
59 int err
= i2c_transfer(state
->i2c
, &msg
, 1);
61 printk("zl10353: write to reg %x failed (err = %d)!\n", reg
, err
);
67 static int zl10353_write(struct dvb_frontend
*fe
, const u8 ibuf
[], int ilen
)
70 for (i
= 0; i
< ilen
- 1; i
++)
71 if ((err
= zl10353_single_write(fe
, ibuf
[0] + i
, ibuf
[i
+ 1])))
77 static int zl10353_read_register(struct zl10353_state
*state
, u8 reg
)
82 struct i2c_msg msg
[2] = { { .addr
= state
->config
.demod_address
,
84 .buf
= b0
, .len
= 1 },
85 { .addr
= state
->config
.demod_address
,
87 .buf
= b1
, .len
= 1 } };
89 ret
= i2c_transfer(state
->i2c
, msg
, 2);
92 printk("%s: readreg error (reg=%d, ret==%i)\n",
100 static void zl10353_dump_regs(struct dvb_frontend
*fe
)
102 struct zl10353_state
*state
= fe
->demodulator_priv
;
106 /* Dump all registers. */
107 for (reg
= 0; ; reg
++) {
110 printk(KERN_CONT
"\n");
111 printk(KERN_DEBUG
"%02x:", reg
);
113 ret
= zl10353_read_register(state
, reg
);
115 printk(KERN_CONT
" %02x", (u8
)ret
);
117 printk(KERN_CONT
" --");
121 printk(KERN_CONT
"\n");
124 static void zl10353_calc_nominal_rate(struct dvb_frontend
*fe
,
128 struct zl10353_state
*state
= fe
->demodulator_priv
;
129 u32 adc_clock
= 450560; /* 45.056 MHz */
131 u8 bw
= bandwidth
/ 1000000;
133 if (state
->config
.adc_clock
)
134 adc_clock
= state
->config
.adc_clock
;
136 value
= (u64
)10 * (1 << 23) / 7 * 125;
137 value
= (bw
* value
) + adc_clock
/ 2;
138 do_div(value
, adc_clock
);
139 *nominal_rate
= value
;
141 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
142 __func__
, bw
, adc_clock
, *nominal_rate
);
145 static void zl10353_calc_input_freq(struct dvb_frontend
*fe
,
148 struct zl10353_state
*state
= fe
->demodulator_priv
;
149 u32 adc_clock
= 450560; /* 45.056 MHz */
150 int if2
= 361667; /* 36.1667 MHz */
154 if (state
->config
.adc_clock
)
155 adc_clock
= state
->config
.adc_clock
;
156 if (state
->config
.if2
)
157 if2
= state
->config
.if2
;
159 if (adc_clock
>= if2
* 2)
162 ife
= adc_clock
- (if2
% adc_clock
);
163 if (ife
> adc_clock
/ 2)
164 ife
= adc_clock
- ife
;
166 value
= (u64
)65536 * ife
+ adc_clock
/ 2;
167 do_div(value
, adc_clock
);
168 *input_freq
= -value
;
170 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
171 __func__
, if2
, ife
, adc_clock
, -(int)value
, *input_freq
);
174 static int zl10353_sleep(struct dvb_frontend
*fe
)
176 static u8 zl10353_softdown
[] = { 0x50, 0x0C, 0x44 };
178 zl10353_write(fe
, zl10353_softdown
, sizeof(zl10353_softdown
));
182 static int zl10353_set_parameters(struct dvb_frontend
*fe
)
184 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
185 struct zl10353_state
*state
= fe
->demodulator_priv
;
186 u16 nominal_rate
, input_freq
;
187 u8 pllbuf
[6] = { 0x67 }, acq_ctl
= 0;
190 state
->frequency
= c
->frequency
;
192 zl10353_single_write(fe
, RESET
, 0x80);
194 zl10353_single_write(fe
, 0xEA, 0x01);
196 zl10353_single_write(fe
, 0xEA, 0x00);
198 zl10353_single_write(fe
, AGC_TARGET
, 0x28);
200 if (c
->transmission_mode
!= TRANSMISSION_MODE_AUTO
)
202 if (c
->guard_interval
!= GUARD_INTERVAL_AUTO
)
204 zl10353_single_write(fe
, ACQ_CTL
, acq_ctl
);
206 switch (c
->bandwidth_hz
) {
208 /* These are extrapolated from the 7 and 8MHz values */
209 zl10353_single_write(fe
, MCLK_RATIO
, 0x97);
210 zl10353_single_write(fe
, 0x64, 0x34);
211 zl10353_single_write(fe
, 0xcc, 0xdd);
214 zl10353_single_write(fe
, MCLK_RATIO
, 0x86);
215 zl10353_single_write(fe
, 0x64, 0x35);
216 zl10353_single_write(fe
, 0xcc, 0x73);
219 c
->bandwidth_hz
= 8000000;
222 zl10353_single_write(fe
, MCLK_RATIO
, 0x75);
223 zl10353_single_write(fe
, 0x64, 0x36);
224 zl10353_single_write(fe
, 0xcc, 0x73);
227 zl10353_calc_nominal_rate(fe
, c
->bandwidth_hz
, &nominal_rate
);
228 zl10353_single_write(fe
, TRL_NOMINAL_RATE_1
, msb(nominal_rate
));
229 zl10353_single_write(fe
, TRL_NOMINAL_RATE_0
, lsb(nominal_rate
));
230 state
->bandwidth
= c
->bandwidth_hz
;
232 zl10353_calc_input_freq(fe
, &input_freq
);
233 zl10353_single_write(fe
, INPUT_FREQ_1
, msb(input_freq
));
234 zl10353_single_write(fe
, INPUT_FREQ_0
, lsb(input_freq
));
236 /* Hint at TPS settings */
237 switch (c
->code_rate_HP
) {
257 switch (c
->code_rate_LP
) {
274 if (c
->hierarchy
== HIERARCHY_AUTO
||
275 c
->hierarchy
== HIERARCHY_NONE
)
281 switch (c
->modulation
) {
295 switch (c
->transmission_mode
) {
296 case TRANSMISSION_MODE_2K
:
297 case TRANSMISSION_MODE_AUTO
:
299 case TRANSMISSION_MODE_8K
:
306 switch (c
->guard_interval
) {
307 case GUARD_INTERVAL_1_32
:
308 case GUARD_INTERVAL_AUTO
:
310 case GUARD_INTERVAL_1_16
:
313 case GUARD_INTERVAL_1_8
:
316 case GUARD_INTERVAL_1_4
:
323 switch (c
->hierarchy
) {
340 zl10353_single_write(fe
, TPS_GIVEN_1
, msb(tps
));
341 zl10353_single_write(fe
, TPS_GIVEN_0
, lsb(tps
));
343 if (fe
->ops
.i2c_gate_ctrl
)
344 fe
->ops
.i2c_gate_ctrl(fe
, 0);
347 * If there is no tuner attached to the secondary I2C bus, we call
348 * set_params to program a potential tuner attached somewhere else.
349 * Otherwise, we update the PLL registers via calc_regs.
351 if (state
->config
.no_tuner
) {
352 if (fe
->ops
.tuner_ops
.set_params
) {
353 fe
->ops
.tuner_ops
.set_params(fe
);
354 if (fe
->ops
.i2c_gate_ctrl
)
355 fe
->ops
.i2c_gate_ctrl(fe
, 0);
357 } else if (fe
->ops
.tuner_ops
.calc_regs
) {
358 fe
->ops
.tuner_ops
.calc_regs(fe
, pllbuf
+ 1, 5);
360 zl10353_write(fe
, pllbuf
, sizeof(pllbuf
));
363 zl10353_single_write(fe
, 0x5F, 0x13);
365 /* If no attached tuner or invalid PLL registers, just start the FSM. */
366 if (state
->config
.no_tuner
|| fe
->ops
.tuner_ops
.calc_regs
== NULL
)
367 zl10353_single_write(fe
, FSM_GO
, 0x01);
369 zl10353_single_write(fe
, TUNER_GO
, 0x01);
374 static int zl10353_get_parameters(struct dvb_frontend
*fe
)
376 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
377 struct zl10353_state
*state
= fe
->demodulator_priv
;
380 static const u8 tps_fec_to_api
[8] = {
391 s6
= zl10353_read_register(state
, STATUS_6
);
392 s9
= zl10353_read_register(state
, STATUS_9
);
393 if (s6
< 0 || s9
< 0)
395 if ((s6
& (1 << 5)) == 0 || (s9
& (1 << 4)) == 0)
396 return -EINVAL
; /* no FE or TPS lock */
398 tps
= zl10353_read_register(state
, TPS_RECEIVED_1
) << 8 |
399 zl10353_read_register(state
, TPS_RECEIVED_0
);
401 c
->code_rate_HP
= tps_fec_to_api
[(tps
>> 7) & 7];
402 c
->code_rate_LP
= tps_fec_to_api
[(tps
>> 4) & 7];
404 switch ((tps
>> 13) & 3) {
406 c
->modulation
= QPSK
;
409 c
->modulation
= QAM_16
;
412 c
->modulation
= QAM_64
;
415 c
->modulation
= QAM_AUTO
;
419 c
->transmission_mode
= (tps
& 0x01) ? TRANSMISSION_MODE_8K
:
420 TRANSMISSION_MODE_2K
;
422 switch ((tps
>> 2) & 3) {
424 c
->guard_interval
= GUARD_INTERVAL_1_32
;
427 c
->guard_interval
= GUARD_INTERVAL_1_16
;
430 c
->guard_interval
= GUARD_INTERVAL_1_8
;
433 c
->guard_interval
= GUARD_INTERVAL_1_4
;
436 c
->guard_interval
= GUARD_INTERVAL_AUTO
;
440 switch ((tps
>> 10) & 7) {
442 c
->hierarchy
= HIERARCHY_NONE
;
445 c
->hierarchy
= HIERARCHY_1
;
448 c
->hierarchy
= HIERARCHY_2
;
451 c
->hierarchy
= HIERARCHY_4
;
454 c
->hierarchy
= HIERARCHY_AUTO
;
458 c
->frequency
= state
->frequency
;
459 c
->bandwidth_hz
= state
->bandwidth
;
460 c
->inversion
= INVERSION_AUTO
;
465 static int zl10353_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
467 struct zl10353_state
*state
= fe
->demodulator_priv
;
470 if ((s6
= zl10353_read_register(state
, STATUS_6
)) < 0)
472 if ((s7
= zl10353_read_register(state
, STATUS_7
)) < 0)
474 if ((s8
= zl10353_read_register(state
, STATUS_8
)) < 0)
479 *status
|= FE_HAS_CARRIER
;
481 *status
|= FE_HAS_VITERBI
;
483 *status
|= FE_HAS_LOCK
;
485 *status
|= FE_HAS_SYNC
;
487 *status
|= FE_HAS_SIGNAL
;
489 if ((*status
& (FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_SYNC
)) !=
490 (FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_SYNC
))
491 *status
&= ~FE_HAS_LOCK
;
496 static int zl10353_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
498 struct zl10353_state
*state
= fe
->demodulator_priv
;
500 *ber
= zl10353_read_register(state
, RS_ERR_CNT_2
) << 16 |
501 zl10353_read_register(state
, RS_ERR_CNT_1
) << 8 |
502 zl10353_read_register(state
, RS_ERR_CNT_0
);
507 static int zl10353_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
509 struct zl10353_state
*state
= fe
->demodulator_priv
;
511 u16 signal
= zl10353_read_register(state
, AGC_GAIN_1
) << 10 |
512 zl10353_read_register(state
, AGC_GAIN_0
) << 2 | 3;
519 static int zl10353_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
521 struct zl10353_state
*state
= fe
->demodulator_priv
;
525 zl10353_dump_regs(fe
);
527 _snr
= zl10353_read_register(state
, SNR
);
528 *snr
= 10 * _snr
/ 8;
533 static int zl10353_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
535 struct zl10353_state
*state
= fe
->demodulator_priv
;
538 ubl
= zl10353_read_register(state
, RS_UBC_1
) << 8 |
539 zl10353_read_register(state
, RS_UBC_0
);
541 state
->ucblocks
+= ubl
;
542 *ucblocks
= state
->ucblocks
;
547 static int zl10353_get_tune_settings(struct dvb_frontend
*fe
,
548 struct dvb_frontend_tune_settings
551 fe_tune_settings
->min_delay_ms
= 1000;
552 fe_tune_settings
->step_size
= 0;
553 fe_tune_settings
->max_drift
= 0;
558 static int zl10353_init(struct dvb_frontend
*fe
)
560 struct zl10353_state
*state
= fe
->demodulator_priv
;
561 u8 zl10353_reset_attach
[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
564 zl10353_dump_regs(fe
);
565 if (state
->config
.parallel_ts
)
566 zl10353_reset_attach
[2] &= ~0x20;
567 if (state
->config
.clock_ctl_1
)
568 zl10353_reset_attach
[3] = state
->config
.clock_ctl_1
;
569 if (state
->config
.pll_0
)
570 zl10353_reset_attach
[4] = state
->config
.pll_0
;
572 /* Do a "hard" reset if not already done */
573 if (zl10353_read_register(state
, 0x50) != zl10353_reset_attach
[1] ||
574 zl10353_read_register(state
, 0x51) != zl10353_reset_attach
[2]) {
575 zl10353_write(fe
, zl10353_reset_attach
,
576 sizeof(zl10353_reset_attach
));
578 zl10353_dump_regs(fe
);
584 static int zl10353_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
586 struct zl10353_state
*state
= fe
->demodulator_priv
;
589 if (state
->config
.disable_i2c_gate_ctrl
) {
590 /* No tuner attached to the internal I2C bus */
591 /* If set enable I2C bridge, the main I2C bus stopped hardly */
598 return zl10353_single_write(fe
, 0x62, val
);
601 static void zl10353_release(struct dvb_frontend
*fe
)
603 struct zl10353_state
*state
= fe
->demodulator_priv
;
607 static struct dvb_frontend_ops zl10353_ops
;
609 struct dvb_frontend
*zl10353_attach(const struct zl10353_config
*config
,
610 struct i2c_adapter
*i2c
)
612 struct zl10353_state
*state
= NULL
;
615 /* allocate memory for the internal state */
616 state
= kzalloc(sizeof(struct zl10353_state
), GFP_KERNEL
);
620 /* setup the state */
622 memcpy(&state
->config
, config
, sizeof(struct zl10353_config
));
624 /* check if the demod is there */
625 id
= zl10353_read_register(state
, CHIP_ID
);
626 if ((id
!= ID_ZL10353
) && (id
!= ID_CE6230
) && (id
!= ID_CE6231
))
629 /* create dvb_frontend */
630 memcpy(&state
->frontend
.ops
, &zl10353_ops
, sizeof(struct dvb_frontend_ops
));
631 state
->frontend
.demodulator_priv
= state
;
633 return &state
->frontend
;
639 static struct dvb_frontend_ops zl10353_ops
= {
640 .delsys
= { SYS_DVBT
},
642 .name
= "Zarlink ZL10353 DVB-T",
643 .frequency_min
= 174000000,
644 .frequency_max
= 862000000,
645 .frequency_stepsize
= 166667,
646 .frequency_tolerance
= 0,
647 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
|
648 FE_CAN_FEC_3_4
| FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
|
650 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
| FE_CAN_QAM_AUTO
|
651 FE_CAN_TRANSMISSION_MODE_AUTO
| FE_CAN_GUARD_INTERVAL_AUTO
|
652 FE_CAN_HIERARCHY_AUTO
| FE_CAN_RECOVER
|
656 .release
= zl10353_release
,
658 .init
= zl10353_init
,
659 .sleep
= zl10353_sleep
,
660 .i2c_gate_ctrl
= zl10353_i2c_gate_ctrl
,
661 .write
= zl10353_write
,
663 .set_frontend
= zl10353_set_parameters
,
664 .get_frontend
= zl10353_get_parameters
,
665 .get_tune_settings
= zl10353_get_tune_settings
,
667 .read_status
= zl10353_read_status
,
668 .read_ber
= zl10353_read_ber
,
669 .read_signal_strength
= zl10353_read_signal_strength
,
670 .read_snr
= zl10353_read_snr
,
671 .read_ucblocks
= zl10353_read_ucblocks
,
674 module_param(debug
, int, 0644);
675 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
677 module_param(debug_regs
, int, 0644);
678 MODULE_PARM_DESC(debug_regs
, "Turn on/off frontend register dumps (default:off).");
680 MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
681 MODULE_AUTHOR("Chris Pascoe");
682 MODULE_LICENSE("GPL");
684 EXPORT_SYMBOL(zl10353_attach
);