mxl111sf-tuner.c 12 KB

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