mxl111sf-tuner.c 12 KB

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