Chris Pascoe | 780dfef | 2006-02-28 08:34:59 -0300 | [diff] [blame] | 1 | /* |
| 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> |
| 24 | #include <linux/moduleparam.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/string.h> |
| 28 | #include <linux/slab.h> |
| 29 | |
| 30 | #include "dvb_frontend.h" |
| 31 | #include "zl10353_priv.h" |
| 32 | #include "zl10353.h" |
| 33 | |
| 34 | struct zl10353_state { |
| 35 | struct i2c_adapter *i2c; |
| 36 | struct dvb_frontend frontend; |
| 37 | struct dvb_frontend_ops ops; |
| 38 | |
| 39 | struct zl10353_config config; |
| 40 | }; |
| 41 | |
| 42 | static int debug_regs = 0; |
| 43 | |
| 44 | static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val) |
| 45 | { |
| 46 | struct zl10353_state *state = fe->demodulator_priv; |
| 47 | u8 buf[2] = { reg, val }; |
| 48 | struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0, |
| 49 | .buf = buf, .len = 2 }; |
| 50 | int err = i2c_transfer(state->i2c, &msg, 1); |
| 51 | if (err != 1) { |
| 52 | printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err); |
| 53 | return err; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen) |
| 59 | { |
| 60 | int err, i; |
| 61 | for (i = 0; i < ilen - 1; i++) |
| 62 | if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1]))) |
| 63 | return err; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int zl10353_read_register(struct zl10353_state *state, u8 reg) |
| 69 | { |
| 70 | int ret; |
| 71 | u8 b0[1] = { reg }; |
| 72 | u8 b1[1] = { 0 }; |
| 73 | struct i2c_msg msg[2] = { { .addr = state->config.demod_address, |
| 74 | .flags = 0, |
| 75 | .buf = b0, .len = 1 }, |
| 76 | { .addr = state->config.demod_address, |
| 77 | .flags = I2C_M_RD, |
| 78 | .buf = b1, .len = 1 } }; |
| 79 | |
| 80 | ret = i2c_transfer(state->i2c, msg, 2); |
| 81 | |
| 82 | if (ret != 2) { |
| 83 | printk("%s: readreg error (reg=%d, ret==%i)\n", |
| 84 | __FUNCTION__, reg, ret); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | return b1[0]; |
| 89 | } |
| 90 | |
Adrian Bunk | c04e89b | 2006-03-15 16:17:11 -0300 | [diff] [blame] | 91 | static void zl10353_dump_regs(struct dvb_frontend *fe) |
Chris Pascoe | 780dfef | 2006-02-28 08:34:59 -0300 | [diff] [blame] | 92 | { |
| 93 | struct zl10353_state *state = fe->demodulator_priv; |
| 94 | char buf[52], buf2[4]; |
| 95 | int ret; |
| 96 | u8 reg; |
| 97 | |
| 98 | /* Dump all registers. */ |
| 99 | for (reg = 0; ; reg++) { |
| 100 | if (reg % 16 == 0) { |
| 101 | if (reg) |
| 102 | printk(KERN_DEBUG "%s\n", buf); |
| 103 | sprintf(buf, "%02x: ", reg); |
| 104 | } |
| 105 | ret = zl10353_read_register(state, reg); |
| 106 | if (ret >= 0) |
| 107 | sprintf(buf2, "%02x ", (u8)ret); |
| 108 | else |
| 109 | strcpy(buf2, "-- "); |
| 110 | strcat(buf, buf2); |
| 111 | if (reg == 0xff) |
| 112 | break; |
| 113 | } |
| 114 | printk(KERN_DEBUG "%s\n", buf); |
| 115 | } |
| 116 | |
| 117 | static int zl10353_sleep(struct dvb_frontend *fe) |
| 118 | { |
| 119 | static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 }; |
| 120 | |
| 121 | zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown)); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int zl10353_set_parameters(struct dvb_frontend *fe, |
| 126 | struct dvb_frontend_parameters *param) |
| 127 | { |
Chris Pascoe | 780dfef | 2006-02-28 08:34:59 -0300 | [diff] [blame] | 128 | u8 pllbuf[6] = { 0x67 }; |
| 129 | |
| 130 | /* These settings set "auto-everything" and start the FSM. */ |
| 131 | zl10353_single_write(fe, 0x55, 0x80); |
| 132 | udelay(200); |
| 133 | zl10353_single_write(fe, 0xEA, 0x01); |
| 134 | udelay(200); |
| 135 | zl10353_single_write(fe, 0xEA, 0x00); |
| 136 | |
| 137 | zl10353_single_write(fe, 0x56, 0x28); |
| 138 | zl10353_single_write(fe, 0x89, 0x20); |
| 139 | zl10353_single_write(fe, 0x5E, 0x00); |
| 140 | zl10353_single_write(fe, 0x65, 0x5A); |
| 141 | zl10353_single_write(fe, 0x66, 0xE9); |
| 142 | zl10353_single_write(fe, 0x62, 0x0A); |
| 143 | |
Andrew de Quincey | e994b8d | 2006-04-18 17:47:11 -0300 | [diff] [blame] | 144 | if (fe->ops->tuner_ops.pllbuf) { |
| 145 | fe->ops->tuner_ops.pllbuf(fe, param, pllbuf+1, 5); |
| 146 | pllbuf[1] <<= 1; |
| 147 | zl10353_write(fe, pllbuf, sizeof(pllbuf)); |
| 148 | } |
Chris Pascoe | 780dfef | 2006-02-28 08:34:59 -0300 | [diff] [blame] | 149 | |
| 150 | zl10353_single_write(fe, 0x70, 0x01); |
| 151 | udelay(250); |
| 152 | zl10353_single_write(fe, 0xE4, 0x00); |
| 153 | zl10353_single_write(fe, 0xE5, 0x2A); |
| 154 | zl10353_single_write(fe, 0xE9, 0x02); |
| 155 | zl10353_single_write(fe, 0xE7, 0x40); |
| 156 | zl10353_single_write(fe, 0xE8, 0x10); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status) |
| 162 | { |
| 163 | struct zl10353_state *state = fe->demodulator_priv; |
| 164 | int s6, s7, s8; |
| 165 | |
| 166 | if ((s6 = zl10353_read_register(state, STATUS_6)) < 0) |
| 167 | return -EREMOTEIO; |
| 168 | if ((s7 = zl10353_read_register(state, STATUS_7)) < 0) |
| 169 | return -EREMOTEIO; |
| 170 | if ((s8 = zl10353_read_register(state, STATUS_8)) < 0) |
| 171 | return -EREMOTEIO; |
| 172 | |
| 173 | *status = 0; |
| 174 | if (s6 & (1 << 2)) |
| 175 | *status |= FE_HAS_CARRIER; |
| 176 | if (s6 & (1 << 1)) |
| 177 | *status |= FE_HAS_VITERBI; |
| 178 | if (s6 & (1 << 5)) |
| 179 | *status |= FE_HAS_LOCK; |
| 180 | if (s7 & (1 << 4)) |
| 181 | *status |= FE_HAS_SYNC; |
| 182 | if (s8 & (1 << 6)) |
| 183 | *status |= FE_HAS_SIGNAL; |
| 184 | |
| 185 | if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) != |
| 186 | (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) |
| 187 | *status &= ~FE_HAS_LOCK; |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr) |
| 193 | { |
| 194 | struct zl10353_state *state = fe->demodulator_priv; |
| 195 | u8 _snr; |
| 196 | |
| 197 | if (debug_regs) |
| 198 | zl10353_dump_regs(fe); |
| 199 | |
| 200 | _snr = zl10353_read_register(state, SNR); |
| 201 | *snr = (_snr << 8) | _snr; |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int zl10353_get_tune_settings(struct dvb_frontend *fe, |
| 207 | struct dvb_frontend_tune_settings |
| 208 | *fe_tune_settings) |
| 209 | { |
| 210 | fe_tune_settings->min_delay_ms = 1000; |
| 211 | fe_tune_settings->step_size = 0; |
| 212 | fe_tune_settings->max_drift = 0; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int zl10353_init(struct dvb_frontend *fe) |
| 218 | { |
| 219 | struct zl10353_state *state = fe->demodulator_priv; |
| 220 | u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F }; |
| 221 | int rc = 0; |
| 222 | |
| 223 | if (debug_regs) |
| 224 | zl10353_dump_regs(fe); |
| 225 | |
| 226 | /* Do a "hard" reset if not already done */ |
| 227 | if (zl10353_read_register(state, 0x50) != 0x03) { |
| 228 | rc = zl10353_write(fe, zl10353_reset_attach, |
| 229 | sizeof(zl10353_reset_attach)); |
| 230 | if (debug_regs) |
| 231 | zl10353_dump_regs(fe); |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static void zl10353_release(struct dvb_frontend *fe) |
| 238 | { |
| 239 | struct zl10353_state *state = fe->demodulator_priv; |
| 240 | |
| 241 | kfree(state); |
| 242 | } |
| 243 | |
| 244 | static struct dvb_frontend_ops zl10353_ops; |
| 245 | |
| 246 | struct dvb_frontend *zl10353_attach(const struct zl10353_config *config, |
| 247 | struct i2c_adapter *i2c) |
| 248 | { |
| 249 | struct zl10353_state *state = NULL; |
| 250 | |
| 251 | /* allocate memory for the internal state */ |
| 252 | state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL); |
| 253 | if (state == NULL) |
| 254 | goto error; |
| 255 | |
| 256 | /* setup the state */ |
| 257 | state->i2c = i2c; |
| 258 | memcpy(&state->config, config, sizeof(struct zl10353_config)); |
| 259 | memcpy(&state->ops, &zl10353_ops, sizeof(struct dvb_frontend_ops)); |
| 260 | |
| 261 | /* check if the demod is there */ |
| 262 | if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353) |
| 263 | goto error; |
| 264 | |
| 265 | /* create dvb_frontend */ |
| 266 | state->frontend.ops = &state->ops; |
| 267 | state->frontend.demodulator_priv = state; |
| 268 | |
| 269 | return &state->frontend; |
| 270 | error: |
| 271 | kfree(state); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | static struct dvb_frontend_ops zl10353_ops = { |
| 276 | |
| 277 | .info = { |
| 278 | .name = "Zarlink ZL10353 DVB-T", |
| 279 | .type = FE_OFDM, |
| 280 | .frequency_min = 174000000, |
| 281 | .frequency_max = 862000000, |
| 282 | .frequency_stepsize = 166667, |
| 283 | .frequency_tolerance = 0, |
| 284 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | |
| 285 | FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | |
| 286 | FE_CAN_FEC_AUTO | |
| 287 | FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO | |
| 288 | FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | |
| 289 | FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | |
| 290 | FE_CAN_MUTE_TS |
| 291 | }, |
| 292 | |
| 293 | .release = zl10353_release, |
| 294 | |
| 295 | .init = zl10353_init, |
| 296 | .sleep = zl10353_sleep, |
| 297 | |
| 298 | .set_frontend = zl10353_set_parameters, |
| 299 | .get_tune_settings = zl10353_get_tune_settings, |
| 300 | |
| 301 | .read_status = zl10353_read_status, |
| 302 | .read_snr = zl10353_read_snr, |
| 303 | }; |
| 304 | |
| 305 | module_param(debug_regs, int, 0644); |
| 306 | MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off)."); |
| 307 | |
| 308 | MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver"); |
| 309 | MODULE_AUTHOR("Chris Pascoe"); |
| 310 | MODULE_LICENSE("GPL"); |
| 311 | |
| 312 | EXPORT_SYMBOL(zl10353_attach); |
| 313 | EXPORT_SYMBOL(zl10353_write); |