cxd2820r_t.c 9.0 KB

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