mxl111sf-tuner.c 11 KB

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