blob: 9bfdc01fed2c9ea8aefa4a5f6c6cdf6db19469ba [file] [log] [blame]
Chris Pascoe780dfef2006-02-28 08:34:59 -03001/*
2 * Driver for Zarlink DVB-T ZL10353 demodulator
3 *
4 * Copyright (C) 2006 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
5 *
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
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
20 */
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>
28
29#include "dvb_frontend.h"
30#include "zl10353_priv.h"
31#include "zl10353.h"
32
33struct zl10353_state {
34 struct i2c_adapter *i2c;
35 struct dvb_frontend frontend;
Chris Pascoe780dfef2006-02-28 08:34:59 -030036
37 struct zl10353_config config;
38};
39
Antti Palosaarif7f57772007-02-10 10:19:11 -030040static int debug;
41#define dprintk(args...) \
42 do { \
43 if (debug) printk(KERN_DEBUG "zl10353: " args); \
44 } while (0)
45
Chris Pascoe780dfef2006-02-28 08:34:59 -030046static int debug_regs = 0;
47
48static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
49{
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);
55 if (err != 1) {
56 printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
57 return err;
58 }
59 return 0;
60}
61
Adrian Bunk34630402007-02-06 21:50:36 -030062static int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
Chris Pascoe780dfef2006-02-28 08:34:59 -030063{
64 int err, i;
65 for (i = 0; i < ilen - 1; i++)
66 if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
67 return err;
68
69 return 0;
70}
71
72static int zl10353_read_register(struct zl10353_state *state, u8 reg)
73{
74 int ret;
75 u8 b0[1] = { reg };
76 u8 b1[1] = { 0 };
77 struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
78 .flags = 0,
79 .buf = b0, .len = 1 },
80 { .addr = state->config.demod_address,
81 .flags = I2C_M_RD,
82 .buf = b1, .len = 1 } };
83
84 ret = i2c_transfer(state->i2c, msg, 2);
85
86 if (ret != 2) {
87 printk("%s: readreg error (reg=%d, ret==%i)\n",
88 __FUNCTION__, reg, ret);
89 return ret;
90 }
91
92 return b1[0];
93}
94
Adrian Bunkc04e89b2006-03-15 16:17:11 -030095static void zl10353_dump_regs(struct dvb_frontend *fe)
Chris Pascoe780dfef2006-02-28 08:34:59 -030096{
97 struct zl10353_state *state = fe->demodulator_priv;
98 char buf[52], buf2[4];
99 int ret;
100 u8 reg;
101
102 /* Dump all registers. */
103 for (reg = 0; ; reg++) {
104 if (reg % 16 == 0) {
105 if (reg)
106 printk(KERN_DEBUG "%s\n", buf);
107 sprintf(buf, "%02x: ", reg);
108 }
109 ret = zl10353_read_register(state, reg);
110 if (ret >= 0)
111 sprintf(buf2, "%02x ", (u8)ret);
112 else
113 strcpy(buf2, "-- ");
114 strcat(buf, buf2);
115 if (reg == 0xff)
116 break;
117 }
118 printk(KERN_DEBUG "%s\n", buf);
119}
120
Antti Palosaarif7f57772007-02-10 10:19:11 -0300121static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
122 enum fe_bandwidth bandwidth,
123 u16 *nominal_rate)
124{
Chris Pascoe78f3b0b2007-11-19 03:47:07 -0300125 u32 adc_clock = 45056; /* 45.056 MHz */
Antti Palosaarif7f57772007-02-10 10:19:11 -0300126 u8 bw;
127 struct zl10353_state *state = fe->demodulator_priv;
128
129 if (state->config.adc_clock)
130 adc_clock = state->config.adc_clock;
131
132 switch (bandwidth) {
133 case BANDWIDTH_6_MHZ:
134 bw = 6;
135 break;
136 case BANDWIDTH_7_MHZ:
137 bw = 7;
138 break;
139 case BANDWIDTH_8_MHZ:
140 default:
141 bw = 8;
142 break;
143 }
144
Chris Pascoe78f3b0b2007-11-19 03:47:07 -0300145 *nominal_rate = (bw * (1 << 23) / 7 * 125 + adc_clock / 2) / adc_clock;
Antti Palosaarif7f57772007-02-10 10:19:11 -0300146
147 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
148 __FUNCTION__, bw, adc_clock, *nominal_rate);
149}
150
Chris Pascoe780dfef2006-02-28 08:34:59 -0300151static int zl10353_sleep(struct dvb_frontend *fe)
152{
153 static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
154
155 zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
156 return 0;
157}
158
159static int zl10353_set_parameters(struct dvb_frontend *fe,
160 struct dvb_frontend_parameters *param)
161{
Andrew de Quincey8dec0732006-04-18 17:47:12 -0300162 struct zl10353_state *state = fe->demodulator_priv;
Antti Palosaarif7f57772007-02-10 10:19:11 -0300163 u16 nominal_rate;
Chris Pascoe780dfef2006-02-28 08:34:59 -0300164 u8 pllbuf[6] = { 0x67 };
165
166 /* These settings set "auto-everything" and start the FSM. */
167 zl10353_single_write(fe, 0x55, 0x80);
168 udelay(200);
169 zl10353_single_write(fe, 0xEA, 0x01);
170 udelay(200);
171 zl10353_single_write(fe, 0xEA, 0x00);
172
173 zl10353_single_write(fe, 0x56, 0x28);
174 zl10353_single_write(fe, 0x89, 0x20);
175 zl10353_single_write(fe, 0x5E, 0x00);
Antti Palosaarif7f57772007-02-10 10:19:11 -0300176
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));
180
Chris Pascoefc3398d2006-08-10 03:17:42 -0300181 zl10353_single_write(fe, 0x6C, 0xCD);
182 zl10353_single_write(fe, 0x6D, 0x7E);
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300183 if (fe->ops.i2c_gate_ctrl)
184 fe->ops.i2c_gate_ctrl(fe, 0);
Chris Pascoe780dfef2006-02-28 08:34:59 -0300185
Chris Pascoe58d834e2007-11-19 03:32:06 -0300186 /*
187 * If there is no tuner attached to the secondary I2C bus, we call
188 * set_params to program a potential tuner attached somewhere else.
189 * Otherwise, we update the PLL registers via calc_regs.
190 */
Andrew de Quincey8dec0732006-04-18 17:47:12 -0300191 if (state->config.no_tuner) {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300192 if (fe->ops.tuner_ops.set_params) {
193 fe->ops.tuner_ops.set_params(fe, param);
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300194 if (fe->ops.i2c_gate_ctrl)
195 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey8dec0732006-04-18 17:47:12 -0300196 }
Chris Pascoe58d834e2007-11-19 03:32:06 -0300197 } else if (fe->ops.tuner_ops.calc_regs) {
198 fe->ops.tuner_ops.calc_regs(fe, param, pllbuf + 1, 5);
Andrew de Quinceye994b8d2006-04-18 17:47:11 -0300199 pllbuf[1] <<= 1;
Chris Pascoe58d834e2007-11-19 03:32:06 -0300200 zl10353_write(fe, pllbuf, sizeof(pllbuf));
Andrew de Quinceye994b8d2006-04-18 17:47:11 -0300201 }
Chris Pascoe780dfef2006-02-28 08:34:59 -0300202
Chris Pascoefc3398d2006-08-10 03:17:42 -0300203 zl10353_single_write(fe, 0x5F, 0x13);
Chris Pascoe58d834e2007-11-19 03:32:06 -0300204
205 /* If no attached tuner or invalid PLL registers, just start the FSM. */
206 if (state->config.no_tuner || fe->ops.tuner_ops.calc_regs == NULL)
207 zl10353_single_write(fe, FSM_GO, 0x01);
208 else
209 zl10353_single_write(fe, TUNER_GO, 0x01);
210
Chris Pascoe780dfef2006-02-28 08:34:59 -0300211 udelay(250);
212 zl10353_single_write(fe, 0xE4, 0x00);
213 zl10353_single_write(fe, 0xE5, 0x2A);
214 zl10353_single_write(fe, 0xE9, 0x02);
215 zl10353_single_write(fe, 0xE7, 0x40);
216 zl10353_single_write(fe, 0xE8, 0x10);
217
218 return 0;
219}
220
221static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
222{
223 struct zl10353_state *state = fe->demodulator_priv;
224 int s6, s7, s8;
225
226 if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
227 return -EREMOTEIO;
228 if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
229 return -EREMOTEIO;
230 if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
231 return -EREMOTEIO;
232
233 *status = 0;
234 if (s6 & (1 << 2))
235 *status |= FE_HAS_CARRIER;
236 if (s6 & (1 << 1))
237 *status |= FE_HAS_VITERBI;
238 if (s6 & (1 << 5))
239 *status |= FE_HAS_LOCK;
240 if (s7 & (1 << 4))
241 *status |= FE_HAS_SYNC;
242 if (s8 & (1 << 6))
243 *status |= FE_HAS_SIGNAL;
244
245 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
246 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
247 *status &= ~FE_HAS_LOCK;
248
249 return 0;
250}
251
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300252static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
253{
254 struct zl10353_state *state = fe->demodulator_priv;
255
Chris Pascoe6345f0f2007-02-10 10:19:16 -0300256 *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
257 zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
258 zl10353_read_register(state, RS_ERR_CNT_0);
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300259
260 return 0;
261}
262
263static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
264{
265 struct zl10353_state *state = fe->demodulator_priv;
266
Chris Pascoe6345f0f2007-02-10 10:19:16 -0300267 u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
268 zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300269
270 *strength = ~signal;
271
272 return 0;
273}
274
Chris Pascoe780dfef2006-02-28 08:34:59 -0300275static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
276{
277 struct zl10353_state *state = fe->demodulator_priv;
278 u8 _snr;
279
280 if (debug_regs)
281 zl10353_dump_regs(fe);
282
283 _snr = zl10353_read_register(state, SNR);
284 *snr = (_snr << 8) | _snr;
285
286 return 0;
287}
288
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300289static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
290{
291 struct zl10353_state *state = fe->demodulator_priv;
292
Chris Pascoe6345f0f2007-02-10 10:19:16 -0300293 *ucblocks = zl10353_read_register(state, RS_UBC_1) << 8 |
294 zl10353_read_register(state, RS_UBC_0);
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300295
296 return 0;
297}
298
Chris Pascoe780dfef2006-02-28 08:34:59 -0300299static int zl10353_get_tune_settings(struct dvb_frontend *fe,
300 struct dvb_frontend_tune_settings
301 *fe_tune_settings)
302{
303 fe_tune_settings->min_delay_ms = 1000;
304 fe_tune_settings->step_size = 0;
305 fe_tune_settings->max_drift = 0;
306
307 return 0;
308}
309
310static int zl10353_init(struct dvb_frontend *fe)
311{
312 struct zl10353_state *state = fe->demodulator_priv;
313 u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
314 int rc = 0;
315
316 if (debug_regs)
317 zl10353_dump_regs(fe);
Chris Pascoe8fb95782006-08-10 03:17:16 -0300318 if (state->config.parallel_ts)
319 zl10353_reset_attach[2] &= ~0x20;
Chris Pascoe780dfef2006-02-28 08:34:59 -0300320
321 /* Do a "hard" reset if not already done */
Chris Pascoe8fb95782006-08-10 03:17:16 -0300322 if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
323 zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
Chris Pascoe780dfef2006-02-28 08:34:59 -0300324 rc = zl10353_write(fe, zl10353_reset_attach,
325 sizeof(zl10353_reset_attach));
326 if (debug_regs)
327 zl10353_dump_regs(fe);
328 }
329
330 return 0;
331}
332
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300333static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
334{
335 u8 val = 0x0a;
336
337 if (enable)
338 val |= 0x10;
339
340 return zl10353_single_write(fe, 0x62, val);
341}
342
Chris Pascoe780dfef2006-02-28 08:34:59 -0300343static void zl10353_release(struct dvb_frontend *fe)
344{
345 struct zl10353_state *state = fe->demodulator_priv;
Chris Pascoe780dfef2006-02-28 08:34:59 -0300346 kfree(state);
347}
348
349static struct dvb_frontend_ops zl10353_ops;
350
351struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
352 struct i2c_adapter *i2c)
353{
354 struct zl10353_state *state = NULL;
355
356 /* allocate memory for the internal state */
357 state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
358 if (state == NULL)
359 goto error;
360
361 /* setup the state */
362 state->i2c = i2c;
363 memcpy(&state->config, config, sizeof(struct zl10353_config));
Chris Pascoe780dfef2006-02-28 08:34:59 -0300364
365 /* check if the demod is there */
366 if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
367 goto error;
368
369 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300370 memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
Chris Pascoe780dfef2006-02-28 08:34:59 -0300371 state->frontend.demodulator_priv = state;
372
373 return &state->frontend;
374error:
375 kfree(state);
376 return NULL;
377}
378
379static struct dvb_frontend_ops zl10353_ops = {
380
381 .info = {
382 .name = "Zarlink ZL10353 DVB-T",
383 .type = FE_OFDM,
384 .frequency_min = 174000000,
385 .frequency_max = 862000000,
386 .frequency_stepsize = 166667,
387 .frequency_tolerance = 0,
388 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
389 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
390 FE_CAN_FEC_AUTO |
391 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
392 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
393 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
394 FE_CAN_MUTE_TS
395 },
396
397 .release = zl10353_release,
398
399 .init = zl10353_init,
400 .sleep = zl10353_sleep,
Antti Palosaari0a11bb82007-02-10 10:19:08 -0300401 .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
Andrew de Quinceyc10d14d2006-08-08 09:10:08 -0300402 .write = zl10353_write,
Chris Pascoe780dfef2006-02-28 08:34:59 -0300403
404 .set_frontend = zl10353_set_parameters,
405 .get_tune_settings = zl10353_get_tune_settings,
406
407 .read_status = zl10353_read_status,
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300408 .read_ber = zl10353_read_ber,
409 .read_signal_strength = zl10353_read_signal_strength,
Chris Pascoe780dfef2006-02-28 08:34:59 -0300410 .read_snr = zl10353_read_snr,
Chris Pascoe67b60aa2007-02-10 10:17:57 -0300411 .read_ucblocks = zl10353_read_ucblocks,
Chris Pascoe780dfef2006-02-28 08:34:59 -0300412};
413
Antti Palosaarif7f57772007-02-10 10:19:11 -0300414module_param(debug, int, 0644);
415MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
416
Chris Pascoe780dfef2006-02-28 08:34:59 -0300417module_param(debug_regs, int, 0644);
418MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
419
420MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
421MODULE_AUTHOR("Chris Pascoe");
422MODULE_LICENSE("GPL");
423
424EXPORT_SYMBOL(zl10353_attach);