cxd2820r_t2.c 8.9 KB

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