Michael Krufky | 4c66c92 | 2011-08-29 00:05:35 -0300 | [diff] [blame] | 1 | /* |
| 2 | * mxl111sf-tuner.c - driver for the MaxLinear MXL111SF CMOS tuner |
| 3 | * |
| 4 | * Copyright (C) 2010 Michael Krufky <mkrufky@kernellabs.com> |
| 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 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "mxl111sf-tuner.h" |
| 22 | #include "mxl111sf-phy.h" |
| 23 | #include "mxl111sf-reg.h" |
| 24 | |
| 25 | /* debug */ |
| 26 | static int mxl111sf_tuner_debug; |
| 27 | module_param_named(debug, mxl111sf_tuner_debug, int, 0644); |
| 28 | MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."); |
| 29 | |
| 30 | #define mxl_dbg(fmt, arg...) \ |
| 31 | if (mxl111sf_tuner_debug) \ |
| 32 | mxl_printk(KERN_DEBUG, fmt, ##arg) |
| 33 | |
| 34 | /* ------------------------------------------------------------------------ */ |
| 35 | |
| 36 | struct mxl111sf_tuner_state { |
| 37 | struct mxl111sf_state *mxl_state; |
| 38 | |
| 39 | struct mxl111sf_tuner_config *cfg; |
| 40 | |
| 41 | u32 frequency; |
| 42 | u32 bandwidth; |
| 43 | }; |
| 44 | |
| 45 | static int mxl111sf_tuner_read_reg(struct mxl111sf_tuner_state *state, |
| 46 | u8 addr, u8 *data) |
| 47 | { |
| 48 | return (state->cfg->read_reg) ? |
| 49 | state->cfg->read_reg(state->mxl_state, addr, data) : |
| 50 | -EINVAL; |
| 51 | } |
| 52 | |
| 53 | static int mxl111sf_tuner_write_reg(struct mxl111sf_tuner_state *state, |
| 54 | u8 addr, u8 data) |
| 55 | { |
| 56 | return (state->cfg->write_reg) ? |
| 57 | state->cfg->write_reg(state->mxl_state, addr, data) : |
| 58 | -EINVAL; |
| 59 | } |
| 60 | |
| 61 | static int mxl111sf_tuner_program_regs(struct mxl111sf_tuner_state *state, |
| 62 | struct mxl111sf_reg_ctrl_info *ctrl_reg_info) |
| 63 | { |
| 64 | return (state->cfg->program_regs) ? |
| 65 | state->cfg->program_regs(state->mxl_state, ctrl_reg_info) : |
| 66 | -EINVAL; |
| 67 | } |
| 68 | |
| 69 | static int mxl1x1sf_tuner_top_master_ctrl(struct mxl111sf_tuner_state *state, |
| 70 | int onoff) |
| 71 | { |
| 72 | return (state->cfg->top_master_ctrl) ? |
| 73 | state->cfg->top_master_ctrl(state->mxl_state, onoff) : |
| 74 | -EINVAL; |
| 75 | } |
| 76 | |
| 77 | /* ------------------------------------------------------------------------ */ |
| 78 | |
| 79 | static struct mxl111sf_reg_ctrl_info mxl_phy_tune_rf[] = { |
| 80 | {0x1d, 0x7f, 0x00}, /* channel bandwidth section 1/2/3, |
| 81 | DIG_MODEINDEX, _A, _CSF, */ |
| 82 | {0x1e, 0xff, 0x00}, /* channel frequency (lo and fractional) */ |
| 83 | {0x1f, 0xff, 0x00}, /* channel frequency (hi for integer portion) */ |
| 84 | {0, 0, 0} |
| 85 | }; |
| 86 | |
| 87 | /* ------------------------------------------------------------------------ */ |
| 88 | |
| 89 | static struct mxl111sf_reg_ctrl_info *mxl111sf_calc_phy_tune_regs(u32 freq, |
| 90 | u8 bw) |
| 91 | { |
| 92 | u8 filt_bw; |
| 93 | |
| 94 | /* set channel bandwidth */ |
| 95 | switch (bw) { |
| 96 | case 0: /* ATSC */ |
| 97 | filt_bw = 25; |
| 98 | break; |
| 99 | case 1: /* QAM */ |
| 100 | filt_bw = 69; |
| 101 | break; |
| 102 | case 6: |
| 103 | filt_bw = 21; |
| 104 | break; |
| 105 | case 7: |
| 106 | filt_bw = 42; |
| 107 | break; |
| 108 | case 8: |
| 109 | filt_bw = 63; |
| 110 | break; |
| 111 | default: |
| 112 | err("%s: invalid bandwidth setting!", __func__); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | /* calculate RF channel */ |
| 117 | freq /= 1000000; |
| 118 | |
| 119 | freq *= 64; |
| 120 | #if 0 |
| 121 | /* do round */ |
| 122 | freq += 0.5; |
| 123 | #endif |
| 124 | /* set bandwidth */ |
| 125 | mxl_phy_tune_rf[0].data = filt_bw; |
| 126 | |
| 127 | /* set RF */ |
| 128 | mxl_phy_tune_rf[1].data = (freq & 0xff); |
| 129 | mxl_phy_tune_rf[2].data = (freq >> 8) & 0xff; |
| 130 | |
| 131 | /* start tune */ |
| 132 | return mxl_phy_tune_rf; |
| 133 | } |
| 134 | |
| 135 | static int mxl1x1sf_tuner_set_if_output_freq(struct mxl111sf_tuner_state *state) |
| 136 | { |
| 137 | int ret; |
| 138 | u8 ctrl; |
| 139 | #if 0 |
| 140 | u16 iffcw; |
| 141 | u32 if_freq; |
| 142 | #endif |
| 143 | mxl_dbg("(IF polarity = %d, IF freq = 0x%02x)", |
| 144 | state->cfg->invert_spectrum, state->cfg->if_freq); |
| 145 | |
| 146 | /* set IF polarity */ |
| 147 | ctrl = state->cfg->invert_spectrum; |
| 148 | |
| 149 | ctrl |= state->cfg->if_freq; |
| 150 | |
| 151 | ret = mxl111sf_tuner_write_reg(state, V6_TUNER_IF_SEL_REG, ctrl); |
| 152 | if (mxl_fail(ret)) |
| 153 | goto fail; |
| 154 | |
| 155 | #if 0 |
| 156 | if_freq /= 1000000; |
| 157 | |
| 158 | /* do round */ |
| 159 | if_freq += 0.5; |
| 160 | |
| 161 | if (MXL_IF_LO == state->cfg->if_freq) { |
| 162 | ctrl = 0x08; |
| 163 | iffcw = (u16)(if_freq / (108 * 4096)); |
| 164 | } else if (MXL_IF_HI == state->cfg->if_freq) { |
| 165 | ctrl = 0x08; |
| 166 | iffcw = (u16)(if_freq / (216 * 4096)); |
| 167 | } else { |
| 168 | ctrl = 0; |
| 169 | iffcw = 0; |
| 170 | } |
| 171 | |
| 172 | ctrl |= (iffcw >> 8); |
| 173 | #endif |
| 174 | ret = mxl111sf_tuner_read_reg(state, V6_TUNER_IF_FCW_BYP_REG, &ctrl); |
| 175 | if (mxl_fail(ret)) |
| 176 | goto fail; |
| 177 | |
| 178 | ctrl &= 0xf0; |
| 179 | ctrl |= 0x90; |
| 180 | |
| 181 | ret = mxl111sf_tuner_write_reg(state, V6_TUNER_IF_FCW_BYP_REG, ctrl); |
| 182 | if (mxl_fail(ret)) |
| 183 | goto fail; |
| 184 | |
| 185 | #if 0 |
| 186 | ctrl = iffcw & 0x00ff; |
| 187 | #endif |
| 188 | ret = mxl111sf_tuner_write_reg(state, V6_TUNER_IF_FCW_REG, ctrl); |
| 189 | mxl_fail(ret); |
| 190 | fail: |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static int mxl1x1sf_tune_rf(struct dvb_frontend *fe, u32 freq, u8 bw) |
| 195 | { |
| 196 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 197 | static struct mxl111sf_reg_ctrl_info *reg_ctrl_array; |
| 198 | int ret; |
| 199 | u8 mxl_mode; |
| 200 | |
| 201 | mxl_dbg("(freq = %d, bw = 0x%x)", freq, bw); |
| 202 | |
| 203 | /* stop tune */ |
| 204 | ret = mxl111sf_tuner_write_reg(state, START_TUNE_REG, 0); |
| 205 | if (mxl_fail(ret)) |
| 206 | goto fail; |
| 207 | |
| 208 | /* check device mode */ |
| 209 | ret = mxl111sf_tuner_read_reg(state, MXL_MODE_REG, &mxl_mode); |
| 210 | if (mxl_fail(ret)) |
| 211 | goto fail; |
| 212 | |
| 213 | /* Fill out registers for channel tune */ |
| 214 | reg_ctrl_array = mxl111sf_calc_phy_tune_regs(freq, bw); |
| 215 | if (!reg_ctrl_array) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | ret = mxl111sf_tuner_program_regs(state, reg_ctrl_array); |
| 219 | if (mxl_fail(ret)) |
| 220 | goto fail; |
| 221 | |
| 222 | if ((mxl_mode & MXL_DEV_MODE_MASK) == MXL_TUNER_MODE) { |
| 223 | /* IF tuner mode only */ |
| 224 | mxl1x1sf_tuner_top_master_ctrl(state, 0); |
| 225 | mxl1x1sf_tuner_top_master_ctrl(state, 1); |
| 226 | mxl1x1sf_tuner_set_if_output_freq(state); |
| 227 | } |
| 228 | |
| 229 | ret = mxl111sf_tuner_write_reg(state, START_TUNE_REG, 1); |
| 230 | if (mxl_fail(ret)) |
| 231 | goto fail; |
| 232 | |
| 233 | if (state->cfg->ant_hunt) |
| 234 | state->cfg->ant_hunt(fe); |
| 235 | fail: |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | static int mxl1x1sf_tuner_get_lock_status(struct mxl111sf_tuner_state *state, |
| 240 | int *rf_synth_lock, |
| 241 | int *ref_synth_lock) |
| 242 | { |
| 243 | int ret; |
| 244 | u8 data; |
| 245 | |
| 246 | *rf_synth_lock = 0; |
| 247 | *ref_synth_lock = 0; |
| 248 | |
| 249 | ret = mxl111sf_tuner_read_reg(state, V6_RF_LOCK_STATUS_REG, &data); |
| 250 | if (mxl_fail(ret)) |
| 251 | goto fail; |
| 252 | |
| 253 | *ref_synth_lock = ((data & 0x03) == 0x03) ? 1 : 0; |
| 254 | *rf_synth_lock = ((data & 0x0c) == 0x0c) ? 1 : 0; |
| 255 | fail: |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | #if 0 |
| 260 | static int mxl1x1sf_tuner_loop_thru_ctrl(struct mxl111sf_tuner_state *state, |
| 261 | int onoff) |
| 262 | { |
| 263 | return mxl111sf_tuner_write_reg(state, V6_TUNER_LOOP_THRU_CTRL_REG, |
| 264 | onoff ? 1 : 0); |
| 265 | } |
| 266 | #endif |
| 267 | |
| 268 | /* ------------------------------------------------------------------------ */ |
| 269 | |
| 270 | static int mxl111sf_tuner_set_params(struct dvb_frontend *fe, |
| 271 | struct dvb_frontend_parameters *params) |
| 272 | { |
| 273 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 274 | int ret; |
| 275 | u8 bw; |
| 276 | |
| 277 | mxl_dbg("()"); |
| 278 | |
| 279 | if (fe->ops.info.type == FE_ATSC) { |
| 280 | switch (params->u.vsb.modulation) { |
| 281 | case VSB_8: |
| 282 | case VSB_16: |
| 283 | bw = 0; /* ATSC */ |
| 284 | break; |
| 285 | case QAM_64: |
| 286 | case QAM_256: |
| 287 | bw = 1; /* US CABLE */ |
| 288 | break; |
| 289 | default: |
| 290 | err("%s: modulation not set!", __func__); |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | } else if (fe->ops.info.type == FE_OFDM) { |
| 294 | switch (params->u.ofdm.bandwidth) { |
| 295 | case BANDWIDTH_6_MHZ: |
| 296 | bw = 6; |
| 297 | break; |
| 298 | case BANDWIDTH_7_MHZ: |
| 299 | bw = 7; |
| 300 | break; |
| 301 | case BANDWIDTH_8_MHZ: |
| 302 | bw = 8; |
| 303 | break; |
| 304 | default: |
| 305 | err("%s: bandwidth not set!", __func__); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | } else { |
| 309 | err("%s: modulation type not supported!", __func__); |
| 310 | return -EINVAL; |
| 311 | } |
| 312 | ret = mxl1x1sf_tune_rf(fe, params->frequency, bw); |
| 313 | if (mxl_fail(ret)) |
| 314 | goto fail; |
| 315 | |
| 316 | state->frequency = params->frequency; |
| 317 | state->bandwidth = (fe->ops.info.type == FE_OFDM) ? |
| 318 | params->u.ofdm.bandwidth : 0; |
| 319 | fail: |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /* ------------------------------------------------------------------------ */ |
| 324 | |
| 325 | #if 0 |
| 326 | static int mxl111sf_tuner_init(struct dvb_frontend *fe) |
| 327 | { |
| 328 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 329 | int ret; |
| 330 | |
| 331 | /* wake from standby handled by usb driver */ |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static int mxl111sf_tuner_sleep(struct dvb_frontend *fe) |
| 337 | { |
| 338 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 339 | int ret; |
| 340 | |
| 341 | /* enter standby mode handled by usb driver */ |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | /* ------------------------------------------------------------------------ */ |
| 348 | |
| 349 | static int mxl111sf_tuner_get_status(struct dvb_frontend *fe, u32 *status) |
| 350 | { |
| 351 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 352 | int rf_locked, ref_locked, ret; |
| 353 | |
| 354 | *status = 0; |
| 355 | |
| 356 | ret = mxl1x1sf_tuner_get_lock_status(state, &rf_locked, &ref_locked); |
| 357 | if (mxl_fail(ret)) |
| 358 | goto fail; |
| 359 | mxl_info("%s%s", rf_locked ? "rf locked " : "", |
| 360 | ref_locked ? "ref locked" : ""); |
| 361 | |
| 362 | if ((rf_locked) || (ref_locked)) |
| 363 | *status |= TUNER_STATUS_LOCKED; |
| 364 | fail: |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | static int mxl111sf_get_rf_strength(struct dvb_frontend *fe, u16 *strength) |
| 369 | { |
| 370 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 371 | u8 val1, val2; |
| 372 | int ret; |
| 373 | |
| 374 | *strength = 0; |
| 375 | |
| 376 | ret = mxl111sf_tuner_write_reg(state, 0x00, 0x02); |
| 377 | if (mxl_fail(ret)) |
| 378 | goto fail; |
| 379 | ret = mxl111sf_tuner_read_reg(state, V6_DIG_RF_PWR_LSB_REG, &val1); |
| 380 | if (mxl_fail(ret)) |
| 381 | goto fail; |
| 382 | ret = mxl111sf_tuner_read_reg(state, V6_DIG_RF_PWR_MSB_REG, &val2); |
| 383 | if (mxl_fail(ret)) |
| 384 | goto fail; |
| 385 | |
| 386 | *strength = val1 | ((val2 & 0x07) << 8); |
| 387 | fail: |
| 388 | ret = mxl111sf_tuner_write_reg(state, 0x00, 0x00); |
| 389 | mxl_fail(ret); |
| 390 | |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | /* ------------------------------------------------------------------------ */ |
| 395 | |
| 396 | static int mxl111sf_tuner_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
| 397 | { |
| 398 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 399 | *frequency = state->frequency; |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static int mxl111sf_tuner_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) |
| 404 | { |
| 405 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 406 | *bandwidth = state->bandwidth; |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int mxl111sf_tuner_release(struct dvb_frontend *fe) |
| 411 | { |
| 412 | struct mxl111sf_tuner_state *state = fe->tuner_priv; |
| 413 | mxl_dbg("()"); |
| 414 | kfree(state); |
| 415 | fe->tuner_priv = NULL; |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | /* ------------------------------------------------------------------------- */ |
| 420 | |
| 421 | static struct dvb_tuner_ops mxl111sf_tuner_tuner_ops = { |
| 422 | .info = { |
| 423 | .name = "MaxLinear MxL111SF", |
| 424 | #if 0 |
| 425 | .frequency_min = , |
| 426 | .frequency_max = , |
| 427 | .frequency_step = , |
| 428 | #endif |
| 429 | }, |
| 430 | #if 0 |
| 431 | .init = mxl111sf_tuner_init, |
| 432 | .sleep = mxl111sf_tuner_sleep, |
| 433 | #endif |
| 434 | .set_params = mxl111sf_tuner_set_params, |
| 435 | .get_status = mxl111sf_tuner_get_status, |
| 436 | .get_rf_strength = mxl111sf_get_rf_strength, |
| 437 | .get_frequency = mxl111sf_tuner_get_frequency, |
| 438 | .get_bandwidth = mxl111sf_tuner_get_bandwidth, |
| 439 | .release = mxl111sf_tuner_release, |
| 440 | }; |
| 441 | |
| 442 | struct dvb_frontend *mxl111sf_tuner_attach(struct dvb_frontend *fe, |
| 443 | struct mxl111sf_state *mxl_state, |
| 444 | struct mxl111sf_tuner_config *cfg) |
| 445 | { |
| 446 | struct mxl111sf_tuner_state *state = NULL; |
| 447 | |
| 448 | mxl_dbg("()"); |
| 449 | |
| 450 | state = kzalloc(sizeof(struct mxl111sf_tuner_state), GFP_KERNEL); |
| 451 | if (state == NULL) |
| 452 | return NULL; |
| 453 | |
| 454 | state->mxl_state = mxl_state; |
| 455 | state->cfg = cfg; |
| 456 | |
| 457 | memcpy(&fe->ops.tuner_ops, &mxl111sf_tuner_tuner_ops, |
| 458 | sizeof(struct dvb_tuner_ops)); |
| 459 | |
| 460 | fe->tuner_priv = state; |
| 461 | return fe; |
| 462 | } |
| 463 | EXPORT_SYMBOL_GPL(mxl111sf_tuner_attach); |
| 464 | |
| 465 | MODULE_DESCRIPTION("MaxLinear MxL111SF CMOS tuner driver"); |
| 466 | MODULE_AUTHOR("Michael Krufky <mkrufky@kernellabs.com>"); |
| 467 | MODULE_LICENSE("GPL"); |
| 468 | MODULE_VERSION("0.1"); |
| 469 | |
| 470 | /* |
| 471 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 472 | * --------------------------------------------------------------------------- |
| 473 | * Local variables: |
| 474 | * c-basic-offset: 8 |
| 475 | * End: |
| 476 | */ |