cxd2820r_t2.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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_t2(struct dvb_frontend *fe,
  22. struct dvb_frontend_parameters *params)
  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. { 0x1c, 0xb3, 0x33, 0x33, 0x33 }, /* 5 MHz */
  32. { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
  33. { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
  34. { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
  35. };
  36. struct reg_val_mask tab[] = {
  37. { 0x00080, 0x02, 0xff },
  38. { 0x00081, 0x20, 0xff },
  39. { 0x00085, 0x07, 0xff },
  40. { 0x00088, 0x01, 0xff },
  41. { 0x02069, 0x01, 0xff },
  42. { 0x0207f, 0x2a, 0xff },
  43. { 0x02082, 0x0a, 0xff },
  44. { 0x02083, 0x0a, 0xff },
  45. { 0x020cb, priv->cfg.if_agc_polarity << 6, 0x40 },
  46. { 0x02070, priv->cfg.ts_mode, 0xff },
  47. { 0x020b5, priv->cfg.spec_inv << 4, 0x10 },
  48. { 0x02567, 0x07, 0x0f },
  49. { 0x02569, 0x03, 0x03 },
  50. { 0x02595, 0x1a, 0xff },
  51. { 0x02596, 0x50, 0xff },
  52. { 0x02a8c, 0x00, 0xff },
  53. { 0x02a8d, 0x34, 0xff },
  54. { 0x02a45, 0x06, 0x07 },
  55. { 0x03f10, 0x0d, 0xff },
  56. { 0x03f11, 0x02, 0xff },
  57. { 0x03f12, 0x01, 0xff },
  58. { 0x03f23, 0x2c, 0xff },
  59. { 0x03f51, 0x13, 0xff },
  60. { 0x03f52, 0x01, 0xff },
  61. { 0x03f53, 0x00, 0xff },
  62. { 0x027e6, 0x14, 0xff },
  63. { 0x02786, 0x02, 0x07 },
  64. { 0x02787, 0x40, 0xe0 },
  65. { 0x027ef, 0x10, 0x18 },
  66. };
  67. dbg("%s: RF=%d BW=%d", __func__, c->frequency, c->bandwidth_hz);
  68. /* update GPIOs */
  69. ret = cxd2820r_gpio(fe);
  70. if (ret)
  71. goto error;
  72. /* program tuner */
  73. if (fe->ops.tuner_ops.set_params)
  74. fe->ops.tuner_ops.set_params(fe, params);
  75. if (priv->delivery_system != SYS_DVBT2) {
  76. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  77. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
  78. tab[i].val, tab[i].mask);
  79. if (ret)
  80. goto error;
  81. }
  82. }
  83. priv->delivery_system = SYS_DVBT2;
  84. switch (c->bandwidth_hz) {
  85. case 5000000:
  86. if_khz = priv->cfg.if_dvbt2_5;
  87. i = 0;
  88. bw_param = 3;
  89. break;
  90. case 6000000:
  91. if_khz = priv->cfg.if_dvbt2_6;
  92. i = 1;
  93. bw_param = 2;
  94. break;
  95. case 7000000:
  96. if_khz = priv->cfg.if_dvbt2_7;
  97. i = 2;
  98. bw_param = 1;
  99. break;
  100. case 8000000:
  101. if_khz = priv->cfg.if_dvbt2_8;
  102. i = 3;
  103. bw_param = 0;
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. num = if_khz;
  109. num *= 0x1000000;
  110. if_ctl = cxd2820r_div_u64_round_closest(num, 41000);
  111. buf[0] = ((if_ctl >> 16) & 0xff);
  112. buf[1] = ((if_ctl >> 8) & 0xff);
  113. buf[2] = ((if_ctl >> 0) & 0xff);
  114. ret = cxd2820r_wr_regs(priv, 0x020b6, buf, 3);
  115. if (ret)
  116. goto error;
  117. ret = cxd2820r_wr_regs(priv, 0x0209f, bw_params1[i], 5);
  118. if (ret)
  119. goto error;
  120. ret = cxd2820r_wr_reg_mask(priv, 0x020d7, bw_param << 6, 0xc0);
  121. if (ret)
  122. goto error;
  123. ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
  124. if (ret)
  125. goto error;
  126. ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
  127. if (ret)
  128. goto error;
  129. return ret;
  130. error:
  131. dbg("%s: failed:%d", __func__, ret);
  132. return ret;
  133. }
  134. int cxd2820r_get_frontend_t2(struct dvb_frontend *fe,
  135. struct dvb_frontend_parameters *p)
  136. {
  137. struct cxd2820r_priv *priv = fe->demodulator_priv;
  138. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  139. int ret;
  140. u8 buf[2];
  141. ret = cxd2820r_rd_regs(priv, 0x0205c, buf, 2);
  142. if (ret)
  143. goto error;
  144. switch ((buf[0] >> 0) & 0x07) {
  145. case 0:
  146. c->transmission_mode = TRANSMISSION_MODE_2K;
  147. break;
  148. case 1:
  149. c->transmission_mode = TRANSMISSION_MODE_8K;
  150. break;
  151. case 2:
  152. c->transmission_mode = TRANSMISSION_MODE_4K;
  153. break;
  154. case 3:
  155. c->transmission_mode = TRANSMISSION_MODE_1K;
  156. break;
  157. case 4:
  158. c->transmission_mode = TRANSMISSION_MODE_16K;
  159. break;
  160. case 5:
  161. c->transmission_mode = TRANSMISSION_MODE_32K;
  162. break;
  163. }
  164. switch ((buf[1] >> 4) & 0x07) {
  165. case 0:
  166. c->guard_interval = GUARD_INTERVAL_1_32;
  167. break;
  168. case 1:
  169. c->guard_interval = GUARD_INTERVAL_1_16;
  170. break;
  171. case 2:
  172. c->guard_interval = GUARD_INTERVAL_1_8;
  173. break;
  174. case 3:
  175. c->guard_interval = GUARD_INTERVAL_1_4;
  176. break;
  177. case 4:
  178. c->guard_interval = GUARD_INTERVAL_1_128;
  179. break;
  180. case 5:
  181. c->guard_interval = GUARD_INTERVAL_19_128;
  182. break;
  183. case 6:
  184. c->guard_interval = GUARD_INTERVAL_19_256;
  185. break;
  186. }
  187. ret = cxd2820r_rd_regs(priv, 0x0225b, buf, 2);
  188. if (ret)
  189. goto error;
  190. switch ((buf[0] >> 0) & 0x07) {
  191. case 0:
  192. c->fec_inner = FEC_1_2;
  193. break;
  194. case 1:
  195. c->fec_inner = FEC_3_5;
  196. break;
  197. case 2:
  198. c->fec_inner = FEC_2_3;
  199. break;
  200. case 3:
  201. c->fec_inner = FEC_3_4;
  202. break;
  203. case 4:
  204. c->fec_inner = FEC_4_5;
  205. break;
  206. case 5:
  207. c->fec_inner = FEC_5_6;
  208. break;
  209. }
  210. switch ((buf[1] >> 0) & 0x07) {
  211. case 0:
  212. c->modulation = QPSK;
  213. break;
  214. case 1:
  215. c->modulation = QAM_16;
  216. break;
  217. case 2:
  218. c->modulation = QAM_64;
  219. break;
  220. case 3:
  221. c->modulation = QAM_256;
  222. break;
  223. }
  224. ret = cxd2820r_rd_reg(priv, 0x020b5, &buf[0]);
  225. if (ret)
  226. goto error;
  227. switch ((buf[0] >> 4) & 0x01) {
  228. case 0:
  229. c->inversion = INVERSION_OFF;
  230. break;
  231. case 1:
  232. c->inversion = INVERSION_ON;
  233. break;
  234. }
  235. return ret;
  236. error:
  237. dbg("%s: failed:%d", __func__, ret);
  238. return ret;
  239. }
  240. int cxd2820r_read_status_t2(struct dvb_frontend *fe, fe_status_t *status)
  241. {
  242. struct cxd2820r_priv *priv = fe->demodulator_priv;
  243. int ret;
  244. u8 buf[1];
  245. *status = 0;
  246. ret = cxd2820r_rd_reg(priv, 0x02010 , &buf[0]);
  247. if (ret)
  248. goto error;
  249. if ((buf[0] & 0x07) == 6) {
  250. if (((buf[0] >> 5) & 0x01) == 1) {
  251. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  252. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  253. } else {
  254. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  255. FE_HAS_VITERBI | FE_HAS_SYNC;
  256. }
  257. }
  258. dbg("%s: lock=%02x", __func__, buf[0]);
  259. return ret;
  260. error:
  261. dbg("%s: failed:%d", __func__, ret);
  262. return ret;
  263. }
  264. int cxd2820r_read_ber_t2(struct dvb_frontend *fe, u32 *ber)
  265. {
  266. struct cxd2820r_priv *priv = fe->demodulator_priv;
  267. int ret;
  268. u8 buf[4];
  269. unsigned int errbits;
  270. *ber = 0;
  271. /* FIXME: correct calculation */
  272. ret = cxd2820r_rd_regs(priv, 0x02039, buf, sizeof(buf));
  273. if (ret)
  274. goto error;
  275. if ((buf[0] >> 4) & 0x01) {
  276. errbits = (buf[0] & 0x0f) << 24 | buf[1] << 16 |
  277. buf[2] << 8 | buf[3];
  278. if (errbits)
  279. *ber = errbits * 64 / 16588800;
  280. }
  281. return ret;
  282. error:
  283. dbg("%s: failed:%d", __func__, ret);
  284. return ret;
  285. }
  286. int cxd2820r_read_signal_strength_t2(struct dvb_frontend *fe,
  287. u16 *strength)
  288. {
  289. struct cxd2820r_priv *priv = fe->demodulator_priv;
  290. int ret;
  291. u8 buf[2];
  292. u16 tmp;
  293. ret = cxd2820r_rd_regs(priv, 0x02026, buf, sizeof(buf));
  294. if (ret)
  295. goto error;
  296. tmp = (buf[0] & 0x0f) << 8 | buf[1];
  297. tmp = ~tmp & 0x0fff;
  298. /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
  299. *strength = tmp * 0xffff / 0x0fff;
  300. return ret;
  301. error:
  302. dbg("%s: failed:%d", __func__, ret);
  303. return ret;
  304. }
  305. int cxd2820r_read_snr_t2(struct dvb_frontend *fe, u16 *snr)
  306. {
  307. struct cxd2820r_priv *priv = fe->demodulator_priv;
  308. int ret;
  309. u8 buf[2];
  310. u16 tmp;
  311. /* report SNR in dB * 10 */
  312. ret = cxd2820r_rd_regs(priv, 0x02028, buf, sizeof(buf));
  313. if (ret)
  314. goto error;
  315. tmp = (buf[0] & 0x0f) << 8 | buf[1];
  316. #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
  317. if (tmp)
  318. *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
  319. / 100);
  320. else
  321. *snr = 0;
  322. dbg("%s: dBx10=%d val=%04x", __func__, *snr, tmp);
  323. return ret;
  324. error:
  325. dbg("%s: failed:%d", __func__, ret);
  326. return ret;
  327. }
  328. int cxd2820r_read_ucblocks_t2(struct dvb_frontend *fe, u32 *ucblocks)
  329. {
  330. *ucblocks = 0;
  331. /* no way to read ? */
  332. return 0;
  333. }
  334. int cxd2820r_sleep_t2(struct dvb_frontend *fe)
  335. {
  336. struct cxd2820r_priv *priv = fe->demodulator_priv;
  337. int ret, i;
  338. struct reg_val_mask tab[] = {
  339. { 0x000ff, 0x1f, 0xff },
  340. { 0x00085, 0x00, 0xff },
  341. { 0x00088, 0x01, 0xff },
  342. { 0x02069, 0x00, 0xff },
  343. { 0x00081, 0x00, 0xff },
  344. { 0x00080, 0x00, 0xff },
  345. };
  346. dbg("%s", __func__);
  347. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  348. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  349. tab[i].mask);
  350. if (ret)
  351. goto error;
  352. }
  353. priv->delivery_system = SYS_UNDEFINED;
  354. return ret;
  355. error:
  356. dbg("%s: failed:%d", __func__, ret);
  357. return ret;
  358. }
  359. int cxd2820r_get_tune_settings_t2(struct dvb_frontend *fe,
  360. struct dvb_frontend_tune_settings *s)
  361. {
  362. s->min_delay_ms = 1500;
  363. s->step_size = fe->ops.info.frequency_stepsize * 2;
  364. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  365. return 0;
  366. }