Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Conexant cx22700 DVB OFDM demodulator driver |
| 3 | |
| 4 | Copyright (C) 2001-2002 Convergence Integrated Media GmbH |
| 5 | Holger Waechtler <holger@convergence.de> |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 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 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/moduleparam.h> |
| 27 | #include <linux/string.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include "dvb_frontend.h" |
| 30 | #include "cx22700.h" |
| 31 | |
| 32 | |
| 33 | struct cx22700_state { |
| 34 | |
| 35 | struct i2c_adapter* i2c; |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | const struct cx22700_config* config; |
| 38 | |
| 39 | struct dvb_frontend frontend; |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | static int debug; |
| 44 | #define dprintk(args...) \ |
| 45 | do { \ |
| 46 | if (debug) printk(KERN_DEBUG "cx22700: " args); \ |
| 47 | } while (0) |
| 48 | |
| 49 | static u8 init_tab [] = { |
| 50 | 0x04, 0x10, |
| 51 | 0x05, 0x09, |
| 52 | 0x06, 0x00, |
| 53 | 0x08, 0x04, |
| 54 | 0x09, 0x00, |
| 55 | 0x0a, 0x01, |
| 56 | 0x15, 0x40, |
| 57 | 0x16, 0x10, |
| 58 | 0x17, 0x87, |
| 59 | 0x18, 0x17, |
| 60 | 0x1a, 0x10, |
| 61 | 0x25, 0x04, |
| 62 | 0x2e, 0x00, |
| 63 | 0x39, 0x00, |
| 64 | 0x3a, 0x04, |
| 65 | 0x45, 0x08, |
| 66 | 0x46, 0x02, |
| 67 | 0x47, 0x05, |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | static int cx22700_writereg (struct cx22700_state* state, u8 reg, u8 data) |
| 72 | { |
| 73 | int ret; |
| 74 | u8 buf [] = { reg, data }; |
| 75 | struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 }; |
| 76 | |
| 77 | dprintk ("%s\n", __FUNCTION__); |
| 78 | |
| 79 | ret = i2c_transfer (state->i2c, &msg, 1); |
| 80 | |
| 81 | if (ret != 1) |
| 82 | printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n", |
| 83 | __FUNCTION__, reg, data, ret); |
| 84 | |
| 85 | return (ret != 1) ? -1 : 0; |
| 86 | } |
| 87 | |
| 88 | static int cx22700_readreg (struct cx22700_state* state, u8 reg) |
| 89 | { |
| 90 | int ret; |
| 91 | u8 b0 [] = { reg }; |
| 92 | u8 b1 [] = { 0 }; |
| 93 | struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 }, |
| 94 | { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } }; |
| 95 | |
| 96 | dprintk ("%s\n", __FUNCTION__); |
| 97 | |
| 98 | ret = i2c_transfer (state->i2c, msg, 2); |
| 99 | |
| 100 | if (ret != 2) return -EIO; |
| 101 | |
| 102 | return b1[0]; |
| 103 | } |
| 104 | |
| 105 | static int cx22700_set_inversion (struct cx22700_state* state, int inversion) |
| 106 | { |
| 107 | u8 val; |
| 108 | |
| 109 | dprintk ("%s\n", __FUNCTION__); |
| 110 | |
| 111 | switch (inversion) { |
| 112 | case INVERSION_AUTO: |
| 113 | return -EOPNOTSUPP; |
| 114 | case INVERSION_ON: |
| 115 | val = cx22700_readreg (state, 0x09); |
| 116 | return cx22700_writereg (state, 0x09, val | 0x01); |
| 117 | case INVERSION_OFF: |
| 118 | val = cx22700_readreg (state, 0x09); |
| 119 | return cx22700_writereg (state, 0x09, val & 0xfe); |
| 120 | default: |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static int cx22700_set_tps (struct cx22700_state *state, struct dvb_ofdm_parameters *p) |
| 126 | { |
| 127 | static const u8 qam_tab [4] = { 0, 1, 0, 2 }; |
| 128 | static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 }; |
| 129 | u8 val; |
| 130 | |
| 131 | dprintk ("%s\n", __FUNCTION__); |
| 132 | |
| 133 | if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8) |
Eric Sesterhenn | 91a0791 | 2006-06-30 11:51:11 -0300 | [diff] [blame] | 137 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | if (p->guard_interval < GUARD_INTERVAL_1_32 || |
| 143 | p->guard_interval > GUARD_INTERVAL_1_4) |
| 144 | return -EINVAL; |
| 145 | |
| 146 | if (p->transmission_mode != TRANSMISSION_MODE_2K && |
| 147 | p->transmission_mode != TRANSMISSION_MODE_8K) |
| 148 | return -EINVAL; |
| 149 | |
| 150 | if (p->constellation != QPSK && |
| 151 | p->constellation != QAM_16 && |
| 152 | p->constellation != QAM_64) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | if (p->hierarchy_information < HIERARCHY_NONE || |
| 156 | p->hierarchy_information > HIERARCHY_4) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | if (p->bandwidth < BANDWIDTH_8_MHZ && p->bandwidth > BANDWIDTH_6_MHZ) |
| 160 | return -EINVAL; |
| 161 | |
| 162 | if (p->bandwidth == BANDWIDTH_7_MHZ) |
| 163 | cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 | 0x10)); |
| 164 | else |
| 165 | cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 & ~0x10)); |
| 166 | |
| 167 | val = qam_tab[p->constellation - QPSK]; |
| 168 | val |= p->hierarchy_information - HIERARCHY_NONE; |
| 169 | |
| 170 | cx22700_writereg (state, 0x04, val); |
| 171 | |
| 172 | val = fec_tab[p->code_rate_HP - FEC_1_2] << 3; |
| 173 | val |= fec_tab[p->code_rate_LP - FEC_1_2]; |
| 174 | |
| 175 | cx22700_writereg (state, 0x05, val); |
| 176 | |
| 177 | val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2; |
| 178 | val |= p->transmission_mode - TRANSMISSION_MODE_2K; |
| 179 | |
| 180 | cx22700_writereg (state, 0x06, val); |
| 181 | |
| 182 | cx22700_writereg (state, 0x08, 0x04 | 0x02); /* use user tps parameters */ |
| 183 | cx22700_writereg (state, 0x08, 0x04); /* restart aquisition */ |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int cx22700_get_tps (struct cx22700_state* state, struct dvb_ofdm_parameters *p) |
| 189 | { |
| 190 | static const fe_modulation_t qam_tab [3] = { QPSK, QAM_16, QAM_64 }; |
| 191 | static const fe_code_rate_t fec_tab [5] = { FEC_1_2, FEC_2_3, FEC_3_4, |
| 192 | FEC_5_6, FEC_7_8 }; |
| 193 | u8 val; |
| 194 | |
| 195 | dprintk ("%s\n", __FUNCTION__); |
| 196 | |
| 197 | if (!(cx22700_readreg(state, 0x07) & 0x20)) /* tps valid? */ |
| 198 | return -EAGAIN; |
| 199 | |
| 200 | val = cx22700_readreg (state, 0x01); |
| 201 | |
| 202 | if ((val & 0x7) > 4) |
| 203 | p->hierarchy_information = HIERARCHY_AUTO; |
| 204 | else |
| 205 | p->hierarchy_information = HIERARCHY_NONE + (val & 0x7); |
| 206 | |
| 207 | if (((val >> 3) & 0x3) > 2) |
| 208 | p->constellation = QAM_AUTO; |
| 209 | else |
| 210 | p->constellation = qam_tab[(val >> 3) & 0x3]; |
| 211 | |
| 212 | val = cx22700_readreg (state, 0x02); |
| 213 | |
| 214 | if (((val >> 3) & 0x07) > 4) |
| 215 | p->code_rate_HP = FEC_AUTO; |
| 216 | else |
| 217 | p->code_rate_HP = fec_tab[(val >> 3) & 0x07]; |
| 218 | |
| 219 | if ((val & 0x07) > 4) |
| 220 | p->code_rate_LP = FEC_AUTO; |
| 221 | else |
| 222 | p->code_rate_LP = fec_tab[val & 0x07]; |
| 223 | |
| 224 | val = cx22700_readreg (state, 0x03); |
| 225 | |
| 226 | p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3); |
| 227 | p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int cx22700_init (struct dvb_frontend* fe) |
| 233 | |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 234 | { struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | int i; |
| 236 | |
| 237 | dprintk("cx22700_init: init chip\n"); |
| 238 | |
| 239 | cx22700_writereg (state, 0x00, 0x02); /* soft reset */ |
| 240 | cx22700_writereg (state, 0x00, 0x00); |
| 241 | |
| 242 | msleep(10); |
| 243 | |
| 244 | for (i=0; i<sizeof(init_tab); i+=2) |
| 245 | cx22700_writereg (state, init_tab[i], init_tab[i+1]); |
| 246 | |
| 247 | cx22700_writereg (state, 0x00, 0x01); |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int cx22700_read_status(struct dvb_frontend* fe, fe_status_t* status) |
| 253 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 254 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9) |
| 257 | | (cx22700_readreg (state, 0x0e) << 1); |
| 258 | u8 sync = cx22700_readreg (state, 0x07); |
| 259 | |
| 260 | *status = 0; |
| 261 | |
| 262 | if (rs_ber < 0xff00) |
| 263 | *status |= FE_HAS_SIGNAL; |
| 264 | |
| 265 | if (sync & 0x20) |
| 266 | *status |= FE_HAS_CARRIER; |
| 267 | |
| 268 | if (sync & 0x10) |
| 269 | *status |= FE_HAS_VITERBI; |
| 270 | |
| 271 | if (sync & 0x10) |
| 272 | *status |= FE_HAS_SYNC; |
| 273 | |
| 274 | if (*status == 0x0f) |
| 275 | *status |= FE_HAS_LOCK; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int cx22700_read_ber(struct dvb_frontend* fe, u32* ber) |
| 281 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 282 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | *ber = cx22700_readreg (state, 0x0c) & 0x7f; |
| 285 | cx22700_writereg (state, 0x0c, 0x00); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int cx22700_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength) |
| 291 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 292 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9) |
| 295 | | (cx22700_readreg (state, 0x0e) << 1); |
| 296 | *signal_strength = ~rs_ber; |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int cx22700_read_snr(struct dvb_frontend* fe, u16* snr) |
| 302 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 303 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9) |
| 306 | | (cx22700_readreg (state, 0x0e) << 1); |
| 307 | *snr = ~rs_ber; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int cx22700_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks) |
| 313 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 314 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | *ucblocks = cx22700_readreg (state, 0x0f); |
| 317 | cx22700_writereg (state, 0x0f, 0x00); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int cx22700_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p) |
| 323 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 324 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/ |
| 327 | cx22700_writereg (state, 0x00, 0x00); |
| 328 | |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 329 | if (fe->ops.tuner_ops.set_params) { |
| 330 | fe->ops.tuner_ops.set_params(fe, p); |
| 331 | if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0); |
Andrew de Quincey | 5becb3b | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | cx22700_set_inversion (state, p->inversion); |
| 335 | cx22700_set_tps (state, &p->u.ofdm); |
| 336 | cx22700_writereg (state, 0x37, 0x01); /* PAL loop filter off */ |
| 337 | cx22700_writereg (state, 0x00, 0x01); /* restart acquire */ |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int cx22700_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p) |
| 343 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 344 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | u8 reg09 = cx22700_readreg (state, 0x09); |
| 346 | |
| 347 | p->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF; |
| 348 | return cx22700_get_tps (state, &p->u.ofdm); |
| 349 | } |
| 350 | |
Andrew de Quincey | 5becb3b | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 351 | static int cx22700_i2c_gate_ctrl(struct dvb_frontend* fe, int enable) |
| 352 | { |
| 353 | struct cx22700_state* state = fe->demodulator_priv; |
| 354 | |
| 355 | if (enable) { |
| 356 | return cx22700_writereg(state, 0x0a, 0x00); |
| 357 | } else { |
| 358 | return cx22700_writereg(state, 0x0a, 0x01); |
| 359 | } |
| 360 | } |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings) |
| 363 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 364 | fesettings->min_delay_ms = 150; |
| 365 | fesettings->step_size = 166667; |
| 366 | fesettings->max_drift = 166667*2; |
| 367 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void cx22700_release(struct dvb_frontend* fe) |
| 371 | { |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 372 | struct cx22700_state* state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | kfree(state); |
| 374 | } |
| 375 | |
| 376 | static struct dvb_frontend_ops cx22700_ops; |
| 377 | |
| 378 | struct dvb_frontend* cx22700_attach(const struct cx22700_config* config, |
| 379 | struct i2c_adapter* i2c) |
| 380 | { |
| 381 | struct cx22700_state* state = NULL; |
| 382 | |
| 383 | /* allocate memory for the internal state */ |
Johannes Stezenbach | b874270 | 2005-05-16 21:54:31 -0700 | [diff] [blame] | 384 | state = kmalloc(sizeof(struct cx22700_state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (state == NULL) goto error; |
| 386 | |
| 387 | /* setup the state */ |
| 388 | state->config = config; |
| 389 | state->i2c = i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | /* check if the demod is there */ |
| 392 | if (cx22700_readreg(state, 0x07) < 0) goto error; |
| 393 | |
| 394 | /* create dvb_frontend */ |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 395 | memcpy(&state->frontend.ops, &cx22700_ops, sizeof(struct dvb_frontend_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | state->frontend.demodulator_priv = state; |
| 397 | return &state->frontend; |
| 398 | |
| 399 | error: |
| 400 | kfree(state); |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | static struct dvb_frontend_ops cx22700_ops = { |
| 405 | |
| 406 | .info = { |
| 407 | .name = "Conexant CX22700 DVB-T", |
| 408 | .type = FE_OFDM, |
| 409 | .frequency_min = 470000000, |
| 410 | .frequency_max = 860000000, |
| 411 | .frequency_stepsize = 166667, |
| 412 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 413 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | |
| 414 | FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 415 | FE_CAN_RECOVER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | }, |
| 417 | |
| 418 | .release = cx22700_release, |
| 419 | |
| 420 | .init = cx22700_init, |
Andrew de Quincey | 5becb3b | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 421 | .i2c_gate_ctrl = cx22700_i2c_gate_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | .set_frontend = cx22700_set_frontend, |
| 424 | .get_frontend = cx22700_get_frontend, |
| 425 | .get_tune_settings = cx22700_get_tune_settings, |
| 426 | |
| 427 | .read_status = cx22700_read_status, |
| 428 | .read_ber = cx22700_read_ber, |
| 429 | .read_signal_strength = cx22700_read_signal_strength, |
| 430 | .read_snr = cx22700_read_snr, |
| 431 | .read_ucblocks = cx22700_read_ucblocks, |
| 432 | }; |
| 433 | |
| 434 | module_param(debug, int, 0644); |
| 435 | MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off)."); |
| 436 | |
| 437 | MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver"); |
| 438 | MODULE_AUTHOR("Holger Waechtler"); |
| 439 | MODULE_LICENSE("GPL"); |
| 440 | |
| 441 | EXPORT_SYMBOL(cx22700_attach); |