cxd2820r_t2.c 8.9 KB

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