blob: e874ec8df60c37b70d3c26cbbac22de1b9fa9055 [file] [log] [blame]
Chris Pascoe780dfef2006-02-28 08:34:59 -03001/*
2 * Driver for Zarlink DVB-T ZL10353 demodulator
3 *
Chris Pascoe794604c2007-11-19 03:55:45 -03004 * Copyright (C) 2006, 2007 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
Chris Pascoe780dfef2006-02-28 08:34:59 -03005 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
16 *
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
Chris Pascoe794604c2007-11-19 03:55:45 -030019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Chris Pascoe780dfef2006-02-28 08:34:59 -030020 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
Chris Pascoe780dfef2006-02-28 08:34:59 -030024#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/string.h>
27#include <linux/slab.h>
Chris Pascoe794604c2007-11-19 03:55:45 -030028#include <asm/div64.h>
Chris Pascoe780dfef2006-02-28 08:34:59 -030029
30#include "dvb_frontend.h"
31#include "zl10353_priv.h"
32#include "zl10353.h"
33
34struct zl10353_state {
35 struct i2c_adapter *i2c;
36 struct dvb_frontend frontend;
Chris Pascoe780dfef2006-02-28 08:34:59 -030037
38 struct zl10353_config config;
39};
40
Antti Palosaarif7f57772007-02-10 10:19:11 -030041static int debug;
42#define dprintk(args...) \
43 do { \
44 if (debug) printk(KERN_DEBUG "zl10353: " args); \
45 } while (0)
46
Chris Pascoe780dfef2006-02-28 08:34:59 -030047static int debug_regs = 0;
48
49static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
50{
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);
56 if (err != 1) {
57 printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
58 return err;
59 }
60 return 0;
61}
62
Adrian Bunk34630402007-02-06 21:50:36 -030063static int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
Chris Pascoe780dfef2006-02-28 08:34:59 -030064{
65 int err, i;
66 for (i = 0; i < ilen - 1; i++)
67 if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
68 return err;
69
70 return 0;
71}
72
73static int zl10353_read_register(struct zl10353_state *state, u8 reg)
74{
75 int ret;
76 u8 b0[1] = { reg };
77 u8 b1[1] = { 0 };
78 struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
79 .flags = 0,
80 .buf = b0, .len = 1 },
81 { .addr = state->config.demod_address,
82 .flags = I2C_M_RD,
83 .buf = b1, .len = 1 } };
84
85 ret = i2c_transfer(state->i2c, msg, 2);
86
87 if (ret != 2) {
88 printk("%s: readreg error (reg=%d, ret==%i)\n",
89 __FUNCTION__, reg, ret);
90 return ret;
91 }
92
93 return b1[0];
94}
95
Adrian Bunkc04e89b2006-03-15 16:17:11 -030096static void zl10353_dump_regs(struct dvb_frontend *fe)
Chris Pascoe780dfef2006-02-28 08:34:59 -030097{
98 struct zl10353_state *state = fe->demodulator_priv;
99 char buf[52], buf2[4];
100 int ret;
101 u8 reg;
102
103 /* Dump all registers. */
104 for (reg = 0; ; reg++) {
105 if (reg % 16 == 0) {
106 if (reg)
107 printk(KERN_DEBUG "%s\n", buf);
108 sprintf(buf, "%02x: ", reg);
109 }
110 ret = zl10353_read_register(state, reg);
111 if (ret >= 0)
112 sprintf(buf2, "%02x ", (u8)ret);
113 else
114 strcpy(buf2, "-- ");
115 strcat(buf, buf2);
116 if (reg == 0xff)
117 break;
118 }
119 printk(KERN_DEBUG "%s\n", buf);
120}
121
Antti Palosaarif7f57772007-02-10 10:19:11 -0300122static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
123 enum fe_bandwidth bandwidth,
124 u16 *nominal_rate)
125{
Antti Palosaarif7f57772007-02-10 10:19:11 -0300126 struct zl10353_state *state = fe->demodulator_priv;
Chris Pascoea1dcd9d2007-11-20 08:17:54 -0300127 u32 adc_clock = 450560; /* 45.056 MHz */
128 u64 value;
129 u8 bw;
Antti Palosaarif7f57772007-02-10 10:19:11 -0300130
131 if (state->config.adc_clock)
132 adc_clock = state->config.adc_clock;
133
134 switch (bandwidth) {
135 case BANDWIDTH_6_MHZ:
136 bw = 6;
137 break;
138 case BANDWIDTH_7_MHZ:
139 bw = 7;
140 break;
141 case BANDWIDTH_8_MHZ:
142 default:
143 bw = 8;
144 break;
145 }
146
Andrew Morton18ff6052007-12-12 21:43:57 -0300147 value = (u64)10 * (1 << 23) / 7 * 125;
148 value = (bw * value) + adc_clock / 2;
Chris Pascoea1dcd9d2007-11-20 08:17:54 -0300149 do_div(value, adc_clock);
150 *nominal_rate = value;
Antti Palosaarif7f57772007-02-10 10:19:11 -0300151
152 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
153 __FUNCTION__, bw, adc_clock, *nominal_rate);
154}
155
Chris Pascoe794604c2007-11-19 03:55:45 -0300156static void zl10353_calc_input_freq(struct dvb_frontend *fe,
157 u16 *input_freq)
158{
159 struct zl10353_state *state = fe->demodulator_priv;
Chris Pascoea1dcd9d2007-11-20 08:17:54 -0300160 u32 adc_clock = 450560; /* 45.056 MHz */
161 int if2 = 361667; /* 36.1667 MHz */
Chris Pascoe794604c2007-11-19 03:55:45 -0300162 int ife;
163 u64 value;
164
165 if (state->config.adc_clock)
166 adc_clock = state->config.adc_clock;
167 if (state->config.if2)
168 if2 = state->config.if2;
169
170 if (adc_clock >= if2 * 2)
171 ife = if2;
172 else {
173 ife = adc_clock - (if2 % adc_clock);
174 if (ife > adc_clock / 2)
175 ife = adc_clock - ife;
176 }
Chris Pascoea1dcd9d2007-11-20 08:17:54 -0300177 value = (u64)65536 * ife + adc_clock / 2;
Chris Pascoe794604c2007-11-19 03:55:45 -0300178 do_div(value, adc_clock);
179 *input_freq = -value;
180
181 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
182 __FUNCTION__, if2, ife, adc_clock, -(int)value, *input_freq);
183}
184
Chris Pascoe780dfef2006-02-28 08:34:59 -0300185static int zl10353_sleep(struct dvb_frontend *fe)
186{
187 static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
188
189 zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
190 return 0;
191}
192
193static int zl10353_set_parameters(struct dvb_frontend *fe,
194 struct dvb_frontend_parameters *param)
195{
Andrew de Quincey8dec0732006-04-18 17:47:12 -0300196 struct zl10353_state *state = fe->demodulator_priv;
Chris Pascoe794604c2007-11-19 03:55:45 -0300197 u16 nominal_rate, input_freq;
Chris Pascoe780dfef2006-02-28 08:34:59 -0300198 u8 pllbuf[6] = { 0x67 };
199
200 /* These settings set "auto-everything" and start the FSM. */
201 zl10353_single_write(fe, 0x55, 0x80);
202 udelay(200);
203 zl10353_single_write(fe, 0xEA, 0x01);
204 udelay(200);
205 zl10353_single_write(fe, 0xEA, 0x00);
206
207 zl10353_single_write(fe, 0x56, 0x28);
208 zl10353_single_write(fe, 0x89, 0x20);
209 zl10353_single_write(fe, 0x5E, 0x00);
Antti Palosaarif7f57772007-02-10 10:19:11 -0300210
211 zl10353_calc_nominal_rate(fe, param->u.ofdm.bandwidth, &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
Chris Pascoe794604c2007-11-19 03:55:45 -0300215 zl10353_calc_input_freq(fe, &input_freq);
216 zl10353_single_write(fe, INPUT_FREQ_1, msb(input_freq));
217 zl10353_single_write(fe, INPUT_FREQ_0, lsb(input_freq));
218
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300219 if (fe->ops.i2c_gate_ctrl)
220 fe->ops.i2c_gate_ctrl(fe, 0);
Chris Pascoe780dfef2006-02-28 08:34:59 -0300221
Chris Pascoe58d834e2007-11-19 03:32:06 -0300222 /*
223 * If there is no tuner attached to the secondary I2C bus, we call
224 * set_params to program a potential tuner attached somewhere else.
225 * Otherwise, we update the PLL registers via calc_regs.
226 */
Andrew de Quincey8dec0732006-04-18 17:47:12 -0300227 if (state->config.no_tuner) {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300228 if (fe->ops.tuner_ops.set_params) {
229 fe->ops.tuner_ops.set_params(fe, param);
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300230 if (fe->ops.i2c_gate_ctrl)
231 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey8dec0732006-04-18 17:47:12 -0300232 }
Chris Pascoe58d834e2007-11-19 03:32:06 -0300233 } else if (fe->ops.tuner_ops.calc_regs) {
234 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf + 1, 5);
Andrew de Quinceye994b8d2006-04-18 17:47:11 -0300235 pllbuf[1] <<= 1;
Chris Pascoe58d834e2007-11-19 03:32:06 -0300236 zl10353_write(fe, pllbuf, sizeof(pllbuf));
Andrew de Quinceye994b8d2006-04-18 17:47:11 -0300237 }
Chris Pascoe780dfef2006-02-28 08:34:59 -0300238
Chris Pascoefc3398d2006-08-10 03:17:42 -0300239 zl10353_single_write(fe, 0x5F, 0x13);
Chris Pascoe58d834e2007-11-19 03:32:06 -0300240
241 /* If no attached tuner or invalid PLL registers, just start the FSM. */
242 if (state->config.no_tuner || fe->ops.tuner_ops.calc_regs == NULL)
243 zl10353_single_write(fe, FSM_GO, 0x01);
244 else
245 zl10353_single_write(fe, TUNER_GO, 0x01);
246
Chris Pascoe780dfef2006-02-28 08:34:59 -0300247 udelay(250);
248 zl10353_single_write(fe, 0xE4, 0x00);
249 zl10353_single_write(fe, 0xE5, 0x2A);
250 zl10353_single_write(fe, 0xE9, 0x02);
251 zl10353_single_write(fe, 0xE7, 0x40);
252 zl10353_single_write(fe, 0xE8, 0x10);
253
254 return 0;
255}
256
257static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
258{
259 struct zl10353_state *state = fe->demodulator_priv;
260 int s6, s7, s8;
261
262 if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
263 return -EREMOTEIO;
264 if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
265 return -EREMOTEIO;
266 if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
267 return -EREMOTEIO;
268
269 *status = 0;
270 if (s6 & (1 << 2))
271 *status |= FE_HAS_CARRIER;
272 if (s6 & (1 << 1))
273 *status |= FE_HAS_VITERBI;
274 if (s6 & (1 << 5))
275 *status |= FE_HAS_LOCK;
276 if (s7 & (1 << 4))
277 *status |= FE_HAS_SYNC;
278 if (s8 & (1 << 6))
279 *status |= FE_HAS_SIGNAL;
280
281 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
282 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
283 *status &= ~FE_HAS_LOCK;
284
285 return 0;
286}
287
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300288static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
289{
290 struct zl10353_state *state = fe->demodulator_priv;
291
Chris Pascoe6345f0f2007-02-10 10:19:16 -0300292 *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
293 zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
294 zl10353_read_register(state, RS_ERR_CNT_0);
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300295
296 return 0;
297}
298
299static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
300{
301 struct zl10353_state *state = fe->demodulator_priv;
302
Chris Pascoe6345f0f2007-02-10 10:19:16 -0300303 u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
304 zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300305
306 *strength = ~signal;
307
308 return 0;
309}
310
Chris Pascoe780dfef2006-02-28 08:34:59 -0300311static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
312{
313 struct zl10353_state *state = fe->demodulator_priv;
314 u8 _snr;
315
316 if (debug_regs)
317 zl10353_dump_regs(fe);
318
319 _snr = zl10353_read_register(state, SNR);
320 *snr = (_snr << 8) | _snr;
321
322 return 0;
323}
324
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300325static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
326{
327 struct zl10353_state *state = fe->demodulator_priv;
328
Chris Pascoe6345f0f2007-02-10 10:19:16 -0300329 *ucblocks = zl10353_read_register(state, RS_UBC_1) << 8 |
330 zl10353_read_register(state, RS_UBC_0);
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300331
332 return 0;
333}
334
Chris Pascoe780dfef2006-02-28 08:34:59 -0300335static int zl10353_get_tune_settings(struct dvb_frontend *fe,
336 struct dvb_frontend_tune_settings
337 *fe_tune_settings)
338{
339 fe_tune_settings->min_delay_ms = 1000;
340 fe_tune_settings->step_size = 0;
341 fe_tune_settings->max_drift = 0;
342
343 return 0;
344}
345
346static int zl10353_init(struct dvb_frontend *fe)
347{
348 struct zl10353_state *state = fe->demodulator_priv;
349 u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
350 int rc = 0;
351
352 if (debug_regs)
353 zl10353_dump_regs(fe);
Chris Pascoe8fb95782006-08-10 03:17:16 -0300354 if (state->config.parallel_ts)
355 zl10353_reset_attach[2] &= ~0x20;
Chris Pascoe780dfef2006-02-28 08:34:59 -0300356
357 /* Do a "hard" reset if not already done */
Chris Pascoe8fb95782006-08-10 03:17:16 -0300358 if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
359 zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
Chris Pascoe780dfef2006-02-28 08:34:59 -0300360 rc = zl10353_write(fe, zl10353_reset_attach,
361 sizeof(zl10353_reset_attach));
362 if (debug_regs)
363 zl10353_dump_regs(fe);
364 }
365
366 return 0;
367}
368
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300369static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
370{
371 u8 val = 0x0a;
372
373 if (enable)
374 val |= 0x10;
375
376 return zl10353_single_write(fe, 0x62, val);
377}
378
Chris Pascoe780dfef2006-02-28 08:34:59 -0300379static void zl10353_release(struct dvb_frontend *fe)
380{
381 struct zl10353_state *state = fe->demodulator_priv;
Chris Pascoe780dfef2006-02-28 08:34:59 -0300382 kfree(state);
383}
384
385static struct dvb_frontend_ops zl10353_ops;
386
387struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
388 struct i2c_adapter *i2c)
389{
390 struct zl10353_state *state = NULL;
391
392 /* allocate memory for the internal state */
393 state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
394 if (state == NULL)
395 goto error;
396
397 /* setup the state */
398 state->i2c = i2c;
399 memcpy(&state->config, config, sizeof(struct zl10353_config));
Chris Pascoe780dfef2006-02-28 08:34:59 -0300400
401 /* check if the demod is there */
402 if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
403 goto error;
404
405 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300406 memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
Chris Pascoe780dfef2006-02-28 08:34:59 -0300407 state->frontend.demodulator_priv = state;
408
409 return &state->frontend;
410error:
411 kfree(state);
412 return NULL;
413}
414
415static struct dvb_frontend_ops zl10353_ops = {
416
417 .info = {
418 .name = "Zarlink ZL10353 DVB-T",
419 .type = FE_OFDM,
420 .frequency_min = 174000000,
421 .frequency_max = 862000000,
422 .frequency_stepsize = 166667,
423 .frequency_tolerance = 0,
424 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
425 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
426 FE_CAN_FEC_AUTO |
427 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
428 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
429 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
430 FE_CAN_MUTE_TS
431 },
432
433 .release = zl10353_release,
434
435 .init = zl10353_init,
436 .sleep = zl10353_sleep,
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300437 .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300438 .write = zl10353_write,
Chris Pascoe780dfef2006-02-28 08:34:59 -0300439
440 .set_frontend = zl10353_set_parameters,
441 .get_tune_settings = zl10353_get_tune_settings,
442
443 .read_status = zl10353_read_status,
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300444 .read_ber = zl10353_read_ber,
445 .read_signal_strength = zl10353_read_signal_strength,
Chris Pascoe780dfef2006-02-28 08:34:59 -0300446 .read_snr = zl10353_read_snr,
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300447 .read_ucblocks = zl10353_read_ucblocks,
Chris Pascoe780dfef2006-02-28 08:34:59 -0300448};
449
Antti Palosaarif7f57772007-02-10 10:19:11 -0300450module_param(debug, int, 0644);
451MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
452
Chris Pascoe780dfef2006-02-28 08:34:59 -0300453module_param(debug_regs, int, 0644);
454MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
455
456MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
457MODULE_AUTHOR("Chris Pascoe");
458MODULE_LICENSE("GPL");
459
460EXPORT_SYMBOL(zl10353_attach);