cxd2820r_t.c 9.1 KB

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