mxl111sf-demod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * mxl111sf-demod.c - driver for the MaxLinear MXL111SF DVB-T demodulator
  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-demod.h"
  17. #include "mxl111sf-reg.h"
  18. /* debug */
  19. static int mxl111sf_demod_debug;
  20. module_param_named(debug, mxl111sf_demod_debug, int, 0644);
  21. MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
  22. #define mxl_dbg(fmt, arg...) \
  23. if (mxl111sf_demod_debug) \
  24. mxl_printk(KERN_DEBUG, fmt, ##arg)
  25. /* ------------------------------------------------------------------------ */
  26. struct mxl111sf_demod_state {
  27. struct mxl111sf_state *mxl_state;
  28. const struct mxl111sf_demod_config *cfg;
  29. struct dvb_frontend fe;
  30. };
  31. /* ------------------------------------------------------------------------ */
  32. static int mxl111sf_demod_read_reg(struct mxl111sf_demod_state *state,
  33. u8 addr, u8 *data)
  34. {
  35. return (state->cfg->read_reg) ?
  36. state->cfg->read_reg(state->mxl_state, addr, data) :
  37. -EINVAL;
  38. }
  39. static int mxl111sf_demod_write_reg(struct mxl111sf_demod_state *state,
  40. u8 addr, u8 data)
  41. {
  42. return (state->cfg->write_reg) ?
  43. state->cfg->write_reg(state->mxl_state, addr, data) :
  44. -EINVAL;
  45. }
  46. static
  47. int mxl111sf_demod_program_regs(struct mxl111sf_demod_state *state,
  48. struct mxl111sf_reg_ctrl_info *ctrl_reg_info)
  49. {
  50. return (state->cfg->program_regs) ?
  51. state->cfg->program_regs(state->mxl_state, ctrl_reg_info) :
  52. -EINVAL;
  53. }
  54. /* ------------------------------------------------------------------------ */
  55. /* TPS */
  56. static
  57. int mxl1x1sf_demod_get_tps_code_rate(struct mxl111sf_demod_state *state,
  58. enum fe_code_rate *code_rate)
  59. {
  60. u8 val;
  61. int ret = mxl111sf_demod_read_reg(state, V6_CODE_RATE_TPS_REG, &val);
  62. /* bit<2:0> - 000:1/2, 001:2/3, 010:3/4, 011:5/6, 100:7/8 */
  63. if (mxl_fail(ret))
  64. goto fail;
  65. switch (val & V6_CODE_RATE_TPS_MASK) {
  66. case 0:
  67. *code_rate = FEC_1_2;
  68. break;
  69. case 1:
  70. *code_rate = FEC_2_3;
  71. break;
  72. case 2:
  73. *code_rate = FEC_3_4;
  74. break;
  75. case 3:
  76. *code_rate = FEC_5_6;
  77. break;
  78. case 4:
  79. *code_rate = FEC_7_8;
  80. break;
  81. }
  82. fail:
  83. return ret;
  84. }
  85. static
  86. int mxl1x1sf_demod_get_tps_modulation(struct mxl111sf_demod_state *state,
  87. enum fe_modulation *modulation)
  88. {
  89. u8 val;
  90. int ret = mxl111sf_demod_read_reg(state, V6_MODORDER_TPS_REG, &val);
  91. /* Constellation, 00 : QPSK, 01 : 16QAM, 10:64QAM */
  92. if (mxl_fail(ret))
  93. goto fail;
  94. switch ((val & V6_PARAM_CONSTELLATION_MASK) >> 4) {
  95. case 0:
  96. *modulation = QPSK;
  97. break;
  98. case 1:
  99. *modulation = QAM_16;
  100. break;
  101. case 2:
  102. *modulation = QAM_64;
  103. break;
  104. }
  105. fail:
  106. return ret;
  107. }
  108. static
  109. int mxl1x1sf_demod_get_tps_guard_fft_mode(struct mxl111sf_demod_state *state,
  110. enum fe_transmit_mode *fft_mode)
  111. {
  112. u8 val;
  113. int ret = mxl111sf_demod_read_reg(state, V6_MODE_TPS_REG, &val);
  114. /* FFT Mode, 00:2K, 01:8K, 10:4K */
  115. if (mxl_fail(ret))
  116. goto fail;
  117. switch ((val & V6_PARAM_FFT_MODE_MASK) >> 2) {
  118. case 0:
  119. *fft_mode = TRANSMISSION_MODE_2K;
  120. break;
  121. case 1:
  122. *fft_mode = TRANSMISSION_MODE_8K;
  123. break;
  124. case 2:
  125. *fft_mode = TRANSMISSION_MODE_4K;
  126. break;
  127. }
  128. fail:
  129. return ret;
  130. }
  131. static
  132. int mxl1x1sf_demod_get_tps_guard_interval(struct mxl111sf_demod_state *state,
  133. enum fe_guard_interval *guard)
  134. {
  135. u8 val;
  136. int ret = mxl111sf_demod_read_reg(state, V6_CP_TPS_REG, &val);
  137. /* 00:1/32, 01:1/16, 10:1/8, 11:1/4 */
  138. if (mxl_fail(ret))
  139. goto fail;
  140. switch ((val & V6_PARAM_GI_MASK) >> 4) {
  141. case 0:
  142. *guard = GUARD_INTERVAL_1_32;
  143. break;
  144. case 1:
  145. *guard = GUARD_INTERVAL_1_16;
  146. break;
  147. case 2:
  148. *guard = GUARD_INTERVAL_1_8;
  149. break;
  150. case 3:
  151. *guard = GUARD_INTERVAL_1_4;
  152. break;
  153. }
  154. fail:
  155. return ret;
  156. }
  157. static
  158. int mxl1x1sf_demod_get_tps_hierarchy(struct mxl111sf_demod_state *state,
  159. enum fe_hierarchy *hierarchy)
  160. {
  161. u8 val;
  162. int ret = mxl111sf_demod_read_reg(state, V6_TPS_HIERACHY_REG, &val);
  163. /* bit<6:4> - 000:Non hierarchy, 001:1, 010:2, 011:4 */
  164. if (mxl_fail(ret))
  165. goto fail;
  166. switch ((val & V6_TPS_HIERARCHY_INFO_MASK) >> 6) {
  167. case 0:
  168. *hierarchy = HIERARCHY_NONE;
  169. break;
  170. case 1:
  171. *hierarchy = HIERARCHY_1;
  172. break;
  173. case 2:
  174. *hierarchy = HIERARCHY_2;
  175. break;
  176. case 3:
  177. *hierarchy = HIERARCHY_4;
  178. break;
  179. }
  180. fail:
  181. return ret;
  182. }
  183. /* ------------------------------------------------------------------------ */
  184. /* LOCKS */
  185. static
  186. int mxl1x1sf_demod_get_sync_lock_status(struct mxl111sf_demod_state *state,
  187. int *sync_lock)
  188. {
  189. u8 val = 0;
  190. int ret = mxl111sf_demod_read_reg(state, V6_SYNC_LOCK_REG, &val);
  191. if (mxl_fail(ret))
  192. goto fail;
  193. *sync_lock = (val & SYNC_LOCK_MASK) >> 4;
  194. fail:
  195. return ret;
  196. }
  197. static
  198. int mxl1x1sf_demod_get_rs_lock_status(struct mxl111sf_demod_state *state,
  199. int *rs_lock)
  200. {
  201. u8 val = 0;
  202. int ret = mxl111sf_demod_read_reg(state, V6_RS_LOCK_DET_REG, &val);
  203. if (mxl_fail(ret))
  204. goto fail;
  205. *rs_lock = (val & RS_LOCK_DET_MASK) >> 3;
  206. fail:
  207. return ret;
  208. }
  209. static
  210. int mxl1x1sf_demod_get_tps_lock_status(struct mxl111sf_demod_state *state,
  211. int *tps_lock)
  212. {
  213. u8 val = 0;
  214. int ret = mxl111sf_demod_read_reg(state, V6_TPS_LOCK_REG, &val);
  215. if (mxl_fail(ret))
  216. goto fail;
  217. *tps_lock = (val & V6_PARAM_TPS_LOCK_MASK) >> 6;
  218. fail:
  219. return ret;
  220. }
  221. static
  222. int mxl1x1sf_demod_get_fec_lock_status(struct mxl111sf_demod_state *state,
  223. int *fec_lock)
  224. {
  225. u8 val = 0;
  226. int ret = mxl111sf_demod_read_reg(state, V6_IRQ_STATUS_REG, &val);
  227. if (mxl_fail(ret))
  228. goto fail;
  229. *fec_lock = (val & IRQ_MASK_FEC_LOCK) >> 4;
  230. fail:
  231. return ret;
  232. }
  233. #if 0
  234. static
  235. int mxl1x1sf_demod_get_cp_lock_status(struct mxl111sf_demod_state *state,
  236. int *cp_lock)
  237. {
  238. u8 val = 0;
  239. int ret = mxl111sf_demod_read_reg(state, V6_CP_LOCK_DET_REG, &val);
  240. if (mxl_fail(ret))
  241. goto fail;
  242. *cp_lock = (val & V6_CP_LOCK_DET_MASK) >> 2;
  243. fail:
  244. return ret;
  245. }
  246. #endif
  247. static int mxl1x1sf_demod_reset_irq_status(struct mxl111sf_demod_state *state)
  248. {
  249. return mxl111sf_demod_write_reg(state, 0x0e, 0xff);
  250. }
  251. /* ------------------------------------------------------------------------ */
  252. static int mxl111sf_demod_set_frontend(struct dvb_frontend *fe)
  253. {
  254. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  255. int ret = 0;
  256. struct mxl111sf_reg_ctrl_info phy_pll_patch[] = {
  257. {0x00, 0xff, 0x01}, /* change page to 1 */
  258. {0x40, 0xff, 0x05},
  259. {0x40, 0xff, 0x01},
  260. {0x41, 0xff, 0xca},
  261. {0x41, 0xff, 0xc0},
  262. {0x00, 0xff, 0x00}, /* change page to 0 */
  263. {0, 0, 0}
  264. };
  265. mxl_dbg("()");
  266. if (fe->ops.tuner_ops.set_params) {
  267. ret = fe->ops.tuner_ops.set_params(fe);
  268. if (mxl_fail(ret))
  269. goto fail;
  270. msleep(50);
  271. }
  272. ret = mxl111sf_demod_program_regs(state, phy_pll_patch);
  273. mxl_fail(ret);
  274. msleep(50);
  275. ret = mxl1x1sf_demod_reset_irq_status(state);
  276. mxl_fail(ret);
  277. msleep(100);
  278. fail:
  279. return ret;
  280. }
  281. /* ------------------------------------------------------------------------ */
  282. #if 0
  283. /* resets TS Packet error count */
  284. /* After setting 7th bit of V5_PER_COUNT_RESET_REG, it should be reset to 0. */
  285. static
  286. int mxl1x1sf_demod_reset_packet_error_count(struct mxl111sf_demod_state *state)
  287. {
  288. struct mxl111sf_reg_ctrl_info reset_per_count[] = {
  289. {0x20, 0x01, 0x01},
  290. {0x20, 0x01, 0x00},
  291. {0, 0, 0}
  292. };
  293. return mxl111sf_demod_program_regs(state, reset_per_count);
  294. }
  295. #endif
  296. /* returns TS Packet error count */
  297. /* PER Count = FEC_PER_COUNT * (2 ** (FEC_PER_SCALE * 4)) */
  298. static int mxl111sf_demod_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  299. {
  300. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  301. u32 fec_per_count, fec_per_scale;
  302. u8 val;
  303. int ret;
  304. *ucblocks = 0;
  305. /* FEC_PER_COUNT Register */
  306. ret = mxl111sf_demod_read_reg(state, V6_FEC_PER_COUNT_REG, &val);
  307. if (mxl_fail(ret))
  308. goto fail;
  309. fec_per_count = val;
  310. /* FEC_PER_SCALE Register */
  311. ret = mxl111sf_demod_read_reg(state, V6_FEC_PER_SCALE_REG, &val);
  312. if (mxl_fail(ret))
  313. goto fail;
  314. val &= V6_FEC_PER_SCALE_MASK;
  315. val *= 4;
  316. fec_per_scale = 1 << val;
  317. fec_per_count *= fec_per_scale;
  318. *ucblocks = fec_per_count;
  319. fail:
  320. return ret;
  321. }
  322. #ifdef MXL111SF_DEMOD_ENABLE_CALCULATIONS
  323. /* FIXME: leaving this enabled breaks the build on some architectures,
  324. * and we shouldn't have any floating point math in the kernel, anyway.
  325. *
  326. * These macros need to be re-written, but it's harmless to simply
  327. * return zero for now. */
  328. #define CALCULATE_BER(avg_errors, count) \
  329. ((u32)(avg_errors * 4)/(count*64*188*8))
  330. #define CALCULATE_SNR(data) \
  331. ((u32)((10 * (u32)data / 64) - 2.5))
  332. #else
  333. #define CALCULATE_BER(avg_errors, count) 0
  334. #define CALCULATE_SNR(data) 0
  335. #endif
  336. static int mxl111sf_demod_read_ber(struct dvb_frontend *fe, u32 *ber)
  337. {
  338. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  339. u8 val1, val2, val3;
  340. int ret;
  341. *ber = 0;
  342. ret = mxl111sf_demod_read_reg(state, V6_RS_AVG_ERRORS_LSB_REG, &val1);
  343. if (mxl_fail(ret))
  344. goto fail;
  345. ret = mxl111sf_demod_read_reg(state, V6_RS_AVG_ERRORS_MSB_REG, &val2);
  346. if (mxl_fail(ret))
  347. goto fail;
  348. ret = mxl111sf_demod_read_reg(state, V6_N_ACCUMULATE_REG, &val3);
  349. if (mxl_fail(ret))
  350. goto fail;
  351. *ber = CALCULATE_BER((val1 | (val2 << 8)), val3);
  352. fail:
  353. return ret;
  354. }
  355. static int mxl111sf_demod_calc_snr(struct mxl111sf_demod_state *state,
  356. u16 *snr)
  357. {
  358. u8 val1, val2;
  359. int ret;
  360. *snr = 0;
  361. ret = mxl111sf_demod_read_reg(state, V6_SNR_RB_LSB_REG, &val1);
  362. if (mxl_fail(ret))
  363. goto fail;
  364. ret = mxl111sf_demod_read_reg(state, V6_SNR_RB_MSB_REG, &val2);
  365. if (mxl_fail(ret))
  366. goto fail;
  367. *snr = CALCULATE_SNR(val1 | ((val2 & 0x03) << 8));
  368. fail:
  369. return ret;
  370. }
  371. static int mxl111sf_demod_read_snr(struct dvb_frontend *fe, u16 *snr)
  372. {
  373. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  374. int ret = mxl111sf_demod_calc_snr(state, snr);
  375. if (mxl_fail(ret))
  376. goto fail;
  377. *snr /= 10; /* 0.1 dB */
  378. fail:
  379. return ret;
  380. }
  381. static int mxl111sf_demod_read_status(struct dvb_frontend *fe,
  382. enum fe_status *status)
  383. {
  384. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  385. int ret, locked, cr_lock, sync_lock, fec_lock;
  386. *status = 0;
  387. ret = mxl1x1sf_demod_get_rs_lock_status(state, &locked);
  388. if (mxl_fail(ret))
  389. goto fail;
  390. ret = mxl1x1sf_demod_get_tps_lock_status(state, &cr_lock);
  391. if (mxl_fail(ret))
  392. goto fail;
  393. ret = mxl1x1sf_demod_get_sync_lock_status(state, &sync_lock);
  394. if (mxl_fail(ret))
  395. goto fail;
  396. ret = mxl1x1sf_demod_get_fec_lock_status(state, &fec_lock);
  397. if (mxl_fail(ret))
  398. goto fail;
  399. if (locked)
  400. *status |= FE_HAS_SIGNAL;
  401. if (cr_lock)
  402. *status |= FE_HAS_CARRIER;
  403. if (sync_lock)
  404. *status |= FE_HAS_SYNC;
  405. if (fec_lock) /* false positives? */
  406. *status |= FE_HAS_VITERBI;
  407. if ((locked) && (cr_lock) && (sync_lock))
  408. *status |= FE_HAS_LOCK;
  409. fail:
  410. return ret;
  411. }
  412. static int mxl111sf_demod_read_signal_strength(struct dvb_frontend *fe,
  413. u16 *signal_strength)
  414. {
  415. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  416. enum fe_modulation modulation;
  417. u16 snr;
  418. mxl111sf_demod_calc_snr(state, &snr);
  419. mxl1x1sf_demod_get_tps_modulation(state, &modulation);
  420. switch (modulation) {
  421. case QPSK:
  422. *signal_strength = (snr >= 1300) ?
  423. min(65535, snr * 44) : snr * 38;
  424. break;
  425. case QAM_16:
  426. *signal_strength = (snr >= 1500) ?
  427. min(65535, snr * 38) : snr * 33;
  428. break;
  429. case QAM_64:
  430. *signal_strength = (snr >= 2000) ?
  431. min(65535, snr * 29) : snr * 25;
  432. break;
  433. default:
  434. *signal_strength = 0;
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. static int mxl111sf_demod_get_frontend(struct dvb_frontend *fe,
  440. struct dtv_frontend_properties *p)
  441. {
  442. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  443. mxl_dbg("()");
  444. #if 0
  445. p->inversion = /* FIXME */ ? INVERSION_ON : INVERSION_OFF;
  446. #endif
  447. if (fe->ops.tuner_ops.get_bandwidth)
  448. fe->ops.tuner_ops.get_bandwidth(fe, &p->bandwidth_hz);
  449. if (fe->ops.tuner_ops.get_frequency)
  450. fe->ops.tuner_ops.get_frequency(fe, &p->frequency);
  451. mxl1x1sf_demod_get_tps_code_rate(state, &p->code_rate_HP);
  452. mxl1x1sf_demod_get_tps_code_rate(state, &p->code_rate_LP);
  453. mxl1x1sf_demod_get_tps_modulation(state, &p->modulation);
  454. mxl1x1sf_demod_get_tps_guard_fft_mode(state,
  455. &p->transmission_mode);
  456. mxl1x1sf_demod_get_tps_guard_interval(state,
  457. &p->guard_interval);
  458. mxl1x1sf_demod_get_tps_hierarchy(state,
  459. &p->hierarchy);
  460. return 0;
  461. }
  462. static
  463. int mxl111sf_demod_get_tune_settings(struct dvb_frontend *fe,
  464. struct dvb_frontend_tune_settings *tune)
  465. {
  466. tune->min_delay_ms = 1000;
  467. return 0;
  468. }
  469. static void mxl111sf_demod_release(struct dvb_frontend *fe)
  470. {
  471. struct mxl111sf_demod_state *state = fe->demodulator_priv;
  472. mxl_dbg("()");
  473. kfree(state);
  474. fe->demodulator_priv = NULL;
  475. }
  476. static const struct dvb_frontend_ops mxl111sf_demod_ops = {
  477. .delsys = { SYS_DVBT },
  478. .info = {
  479. .name = "MaxLinear MxL111SF DVB-T demodulator",
  480. .frequency_min = 177000000,
  481. .frequency_max = 858000000,
  482. .frequency_stepsize = 166666,
  483. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  484. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  485. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  486. FE_CAN_QAM_AUTO |
  487. FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  488. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
  489. },
  490. .release = mxl111sf_demod_release,
  491. #if 0
  492. .init = mxl111sf_init,
  493. .i2c_gate_ctrl = mxl111sf_i2c_gate_ctrl,
  494. #endif
  495. .set_frontend = mxl111sf_demod_set_frontend,
  496. .get_frontend = mxl111sf_demod_get_frontend,
  497. .get_tune_settings = mxl111sf_demod_get_tune_settings,
  498. .read_status = mxl111sf_demod_read_status,
  499. .read_signal_strength = mxl111sf_demod_read_signal_strength,
  500. .read_ber = mxl111sf_demod_read_ber,
  501. .read_snr = mxl111sf_demod_read_snr,
  502. .read_ucblocks = mxl111sf_demod_read_ucblocks,
  503. };
  504. struct dvb_frontend *mxl111sf_demod_attach(struct mxl111sf_state *mxl_state,
  505. const struct mxl111sf_demod_config *cfg)
  506. {
  507. struct mxl111sf_demod_state *state = NULL;
  508. mxl_dbg("()");
  509. state = kzalloc(sizeof(struct mxl111sf_demod_state), GFP_KERNEL);
  510. if (state == NULL)
  511. return NULL;
  512. state->mxl_state = mxl_state;
  513. state->cfg = cfg;
  514. memcpy(&state->fe.ops, &mxl111sf_demod_ops,
  515. sizeof(struct dvb_frontend_ops));
  516. state->fe.demodulator_priv = state;
  517. return &state->fe;
  518. }
  519. EXPORT_SYMBOL_GPL(mxl111sf_demod_attach);
  520. MODULE_DESCRIPTION("MaxLinear MxL111SF DVB-T demodulator driver");
  521. MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
  522. MODULE_LICENSE("GPL");
  523. MODULE_VERSION("0.1");