cxd2820r_t.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Sony CXD2820R demodulator driver
  3. *
  4. * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
  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 along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "cxd2820r_priv.h"
  21. int cxd2820r_set_frontend_t(struct dvb_frontend *fe)
  22. {
  23. struct cxd2820r_priv *priv = fe->demodulator_priv;
  24. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  25. int ret, i, bw_i;
  26. u32 if_freq, if_ctl;
  27. u64 num;
  28. u8 buf[3], bw_param;
  29. u8 bw_params1[][5] = {
  30. { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
  31. { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
  32. { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
  33. };
  34. u8 bw_params2[][2] = {
  35. { 0x1f, 0xdc }, /* 6 MHz */
  36. { 0x12, 0xf8 }, /* 7 MHz */
  37. { 0x01, 0xe0 }, /* 8 MHz */
  38. };
  39. struct reg_val_mask tab[] = {
  40. { 0x00080, 0x00, 0xff },
  41. { 0x00081, 0x03, 0xff },
  42. { 0x00085, 0x07, 0xff },
  43. { 0x00088, 0x01, 0xff },
  44. { 0x00070, priv->cfg.ts_mode, 0xff },
  45. { 0x000cb, priv->cfg.if_agc_polarity << 6, 0x40 },
  46. { 0x000a5, 0x00, 0x01 },
  47. { 0x00082, 0x20, 0x60 },
  48. { 0x000c2, 0xc3, 0xff },
  49. { 0x0016a, 0x50, 0xff },
  50. { 0x00427, 0x41, 0xff },
  51. };
  52. dbg("%s: RF=%d BW=%d", __func__, c->frequency, c->bandwidth_hz);
  53. switch (c->bandwidth_hz) {
  54. case 6000000:
  55. bw_i = 0;
  56. bw_param = 2;
  57. break;
  58. case 7000000:
  59. bw_i = 1;
  60. bw_param = 1;
  61. break;
  62. case 8000000:
  63. bw_i = 2;
  64. bw_param = 0;
  65. break;
  66. default:
  67. return -EINVAL;
  68. }
  69. /* update GPIOs */
  70. ret = cxd2820r_gpio(fe);
  71. if (ret)
  72. goto error;
  73. /* program tuner */
  74. if (fe->ops.tuner_ops.set_params)
  75. fe->ops.tuner_ops.set_params(fe);
  76. if (priv->delivery_system != SYS_DVBT) {
  77. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  78. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
  79. tab[i].val, tab[i].mask);
  80. if (ret)
  81. goto error;
  82. }
  83. }
  84. priv->delivery_system = SYS_DVBT;
  85. priv->ber_running = 0; /* tune stops BER counter */
  86. /* program IF frequency */
  87. if (fe->ops.tuner_ops.get_if_frequency) {
  88. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
  89. if (ret)
  90. goto error;
  91. } else
  92. if_freq = 0;
  93. dbg("%s: if_freq=%d", __func__, if_freq);
  94. num = if_freq / 1000; /* Hz => kHz */
  95. num *= 0x1000000;
  96. if_ctl = cxd2820r_div_u64_round_closest(num, 41000);
  97. buf[0] = ((if_ctl >> 16) & 0xff);
  98. buf[1] = ((if_ctl >> 8) & 0xff);
  99. buf[2] = ((if_ctl >> 0) & 0xff);
  100. ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3);
  101. if (ret)
  102. goto error;
  103. ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[bw_i], 5);
  104. if (ret)
  105. goto error;
  106. ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0);
  107. if (ret)
  108. goto error;
  109. ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[bw_i], 2);
  110. if (ret)
  111. goto error;
  112. ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
  113. if (ret)
  114. goto error;
  115. ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
  116. if (ret)
  117. goto error;
  118. return ret;
  119. error:
  120. dbg("%s: failed:%d", __func__, ret);
  121. return ret;
  122. }
  123. int cxd2820r_get_frontend_t(struct dvb_frontend *fe)
  124. {
  125. struct cxd2820r_priv *priv = fe->demodulator_priv;
  126. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  127. int ret;
  128. u8 buf[2];
  129. ret = cxd2820r_rd_regs(priv, 0x0002f, buf, sizeof(buf));
  130. if (ret)
  131. goto error;
  132. switch ((buf[0] >> 6) & 0x03) {
  133. case 0:
  134. c->modulation = QPSK;
  135. break;
  136. case 1:
  137. c->modulation = QAM_16;
  138. break;
  139. case 2:
  140. c->modulation = QAM_64;
  141. break;
  142. }
  143. switch ((buf[1] >> 1) & 0x03) {
  144. case 0:
  145. c->transmission_mode = TRANSMISSION_MODE_2K;
  146. break;
  147. case 1:
  148. c->transmission_mode = TRANSMISSION_MODE_8K;
  149. break;
  150. }
  151. switch ((buf[1] >> 3) & 0x03) {
  152. case 0:
  153. c->guard_interval = GUARD_INTERVAL_1_32;
  154. break;
  155. case 1:
  156. c->guard_interval = GUARD_INTERVAL_1_16;
  157. break;
  158. case 2:
  159. c->guard_interval = GUARD_INTERVAL_1_8;
  160. break;
  161. case 3:
  162. c->guard_interval = GUARD_INTERVAL_1_4;
  163. break;
  164. }
  165. switch ((buf[0] >> 3) & 0x07) {
  166. case 0:
  167. c->hierarchy = HIERARCHY_NONE;
  168. break;
  169. case 1:
  170. c->hierarchy = HIERARCHY_1;
  171. break;
  172. case 2:
  173. c->hierarchy = HIERARCHY_2;
  174. break;
  175. case 3:
  176. c->hierarchy = HIERARCHY_4;
  177. break;
  178. }
  179. switch ((buf[0] >> 0) & 0x07) {
  180. case 0:
  181. c->code_rate_HP = FEC_1_2;
  182. break;
  183. case 1:
  184. c->code_rate_HP = FEC_2_3;
  185. break;
  186. case 2:
  187. c->code_rate_HP = FEC_3_4;
  188. break;
  189. case 3:
  190. c->code_rate_HP = FEC_5_6;
  191. break;
  192. case 4:
  193. c->code_rate_HP = FEC_7_8;
  194. break;
  195. }
  196. switch ((buf[1] >> 5) & 0x07) {
  197. case 0:
  198. c->code_rate_LP = FEC_1_2;
  199. break;
  200. case 1:
  201. c->code_rate_LP = FEC_2_3;
  202. break;
  203. case 2:
  204. c->code_rate_LP = FEC_3_4;
  205. break;
  206. case 3:
  207. c->code_rate_LP = FEC_5_6;
  208. break;
  209. case 4:
  210. c->code_rate_LP = FEC_7_8;
  211. break;
  212. }
  213. ret = cxd2820r_rd_reg(priv, 0x007c6, &buf[0]);
  214. if (ret)
  215. goto error;
  216. switch ((buf[0] >> 0) & 0x01) {
  217. case 0:
  218. c->inversion = INVERSION_OFF;
  219. break;
  220. case 1:
  221. c->inversion = INVERSION_ON;
  222. break;
  223. }
  224. return ret;
  225. error:
  226. dbg("%s: failed:%d", __func__, ret);
  227. return ret;
  228. }
  229. int cxd2820r_read_ber_t(struct dvb_frontend *fe, u32 *ber)
  230. {
  231. struct cxd2820r_priv *priv = fe->demodulator_priv;
  232. int ret;
  233. u8 buf[3], start_ber = 0;
  234. *ber = 0;
  235. if (priv->ber_running) {
  236. ret = cxd2820r_rd_regs(priv, 0x00076, buf, sizeof(buf));
  237. if (ret)
  238. goto error;
  239. if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
  240. *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
  241. start_ber = 1;
  242. }
  243. } else {
  244. priv->ber_running = 1;
  245. start_ber = 1;
  246. }
  247. if (start_ber) {
  248. /* (re)start BER */
  249. ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
  250. if (ret)
  251. goto error;
  252. }
  253. return ret;
  254. error:
  255. dbg("%s: failed:%d", __func__, ret);
  256. return ret;
  257. }
  258. int cxd2820r_read_signal_strength_t(struct dvb_frontend *fe,
  259. u16 *strength)
  260. {
  261. struct cxd2820r_priv *priv = fe->demodulator_priv;
  262. int ret;
  263. u8 buf[2];
  264. u16 tmp;
  265. ret = cxd2820r_rd_regs(priv, 0x00026, buf, sizeof(buf));
  266. if (ret)
  267. goto error;
  268. tmp = (buf[0] & 0x0f) << 8 | buf[1];
  269. tmp = ~tmp & 0x0fff;
  270. /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
  271. *strength = tmp * 0xffff / 0x0fff;
  272. return ret;
  273. error:
  274. dbg("%s: failed:%d", __func__, ret);
  275. return ret;
  276. }
  277. int cxd2820r_read_snr_t(struct dvb_frontend *fe, u16 *snr)
  278. {
  279. struct cxd2820r_priv *priv = fe->demodulator_priv;
  280. int ret;
  281. u8 buf[2];
  282. u16 tmp;
  283. /* report SNR in dB * 10 */
  284. ret = cxd2820r_rd_regs(priv, 0x00028, buf, sizeof(buf));
  285. if (ret)
  286. goto error;
  287. tmp = (buf[0] & 0x1f) << 8 | buf[1];
  288. #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
  289. if (tmp)
  290. *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
  291. / 100);
  292. else
  293. *snr = 0;
  294. dbg("%s: dBx10=%d val=%04x", __func__, *snr, tmp);
  295. return ret;
  296. error:
  297. dbg("%s: failed:%d", __func__, ret);
  298. return ret;
  299. }
  300. int cxd2820r_read_ucblocks_t(struct dvb_frontend *fe, u32 *ucblocks)
  301. {
  302. *ucblocks = 0;
  303. /* no way to read ? */
  304. return 0;
  305. }
  306. int cxd2820r_read_status_t(struct dvb_frontend *fe, fe_status_t *status)
  307. {
  308. struct cxd2820r_priv *priv = fe->demodulator_priv;
  309. int ret;
  310. u8 buf[4];
  311. *status = 0;
  312. ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]);
  313. if (ret)
  314. goto error;
  315. if ((buf[0] & 0x07) == 6) {
  316. ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]);
  317. if (ret)
  318. goto error;
  319. if (((buf[1] >> 3) & 0x01) == 1) {
  320. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  321. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  322. } else {
  323. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  324. FE_HAS_VITERBI | FE_HAS_SYNC;
  325. }
  326. } else {
  327. ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]);
  328. if (ret)
  329. goto error;
  330. if ((buf[2] & 0x0f) >= 4) {
  331. ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]);
  332. if (ret)
  333. goto error;
  334. if (((buf[3] >> 4) & 0x01) == 1)
  335. *status |= FE_HAS_SIGNAL;
  336. }
  337. }
  338. dbg("%s: lock=%02x %02x %02x %02x", __func__,
  339. buf[0], buf[1], buf[2], buf[3]);
  340. return ret;
  341. error:
  342. dbg("%s: failed:%d", __func__, ret);
  343. return ret;
  344. }
  345. int cxd2820r_init_t(struct dvb_frontend *fe)
  346. {
  347. struct cxd2820r_priv *priv = fe->demodulator_priv;
  348. int ret;
  349. ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
  350. if (ret)
  351. goto error;
  352. return ret;
  353. error:
  354. dbg("%s: failed:%d", __func__, ret);
  355. return ret;
  356. }
  357. int cxd2820r_sleep_t(struct dvb_frontend *fe)
  358. {
  359. struct cxd2820r_priv *priv = fe->demodulator_priv;
  360. int ret, i;
  361. struct reg_val_mask tab[] = {
  362. { 0x000ff, 0x1f, 0xff },
  363. { 0x00085, 0x00, 0xff },
  364. { 0x00088, 0x01, 0xff },
  365. { 0x00081, 0x00, 0xff },
  366. { 0x00080, 0x00, 0xff },
  367. };
  368. dbg("%s", __func__);
  369. priv->delivery_system = SYS_UNDEFINED;
  370. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  371. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  372. tab[i].mask);
  373. if (ret)
  374. goto error;
  375. }
  376. return ret;
  377. error:
  378. dbg("%s: failed:%d", __func__, ret);
  379. return ret;
  380. }
  381. int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
  382. struct dvb_frontend_tune_settings *s)
  383. {
  384. s->min_delay_ms = 500;
  385. s->step_size = fe->ops.info.frequency_stepsize * 2;
  386. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  387. return 0;
  388. }