cxd2820r_c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_c(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. u8 buf[2];
  28. u16 if_ctl;
  29. u64 num;
  30. struct reg_val_mask tab[] = {
  31. { 0x00080, 0x01, 0xff },
  32. { 0x00081, 0x05, 0xff },
  33. { 0x00085, 0x07, 0xff },
  34. { 0x00088, 0x01, 0xff },
  35. { 0x00082, 0x20, 0x60 },
  36. { 0x1016a, 0x48, 0xff },
  37. { 0x100a5, 0x00, 0x01 },
  38. { 0x10020, 0x06, 0x07 },
  39. { 0x10059, 0x50, 0xff },
  40. { 0x10087, 0x0c, 0x3c },
  41. { 0x1008b, 0x07, 0xff },
  42. { 0x1001f, priv->cfg.if_agc_polarity << 7, 0x80 },
  43. { 0x10070, priv->cfg.ts_mode, 0xff },
  44. };
  45. dbg("%s: RF=%d SR=%d", __func__, c->frequency, c->symbol_rate);
  46. /* update GPIOs */
  47. ret = cxd2820r_gpio(fe);
  48. if (ret)
  49. goto error;
  50. /* program tuner */
  51. if (fe->ops.tuner_ops.set_params)
  52. fe->ops.tuner_ops.set_params(fe, params);
  53. if (priv->delivery_system != SYS_DVBC_ANNEX_AC) {
  54. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  55. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
  56. tab[i].val, tab[i].mask);
  57. if (ret)
  58. goto error;
  59. }
  60. }
  61. priv->delivery_system = SYS_DVBC_ANNEX_AC;
  62. priv->ber_running = 0; /* tune stops BER counter */
  63. num = priv->cfg.if_dvbc;
  64. num *= 0x4000;
  65. if_ctl = cxd2820r_div_u64_round_closest(num, 41000);
  66. buf[0] = (if_ctl >> 8) & 0x3f;
  67. buf[1] = (if_ctl >> 0) & 0xff;
  68. ret = cxd2820r_wr_regs(priv, 0x10042, buf, 2);
  69. if (ret)
  70. goto error;
  71. ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
  72. if (ret)
  73. goto error;
  74. ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
  75. if (ret)
  76. goto error;
  77. return ret;
  78. error:
  79. dbg("%s: failed:%d", __func__, ret);
  80. return ret;
  81. }
  82. int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
  83. struct dvb_frontend_parameters *p)
  84. {
  85. struct cxd2820r_priv *priv = fe->demodulator_priv;
  86. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  87. int ret;
  88. u8 buf[2];
  89. ret = cxd2820r_rd_regs(priv, 0x1001a, buf, 2);
  90. if (ret)
  91. goto error;
  92. c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
  93. ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]);
  94. if (ret)
  95. goto error;
  96. switch ((buf[0] >> 0) & 0x03) {
  97. case 0:
  98. c->modulation = QAM_16;
  99. break;
  100. case 1:
  101. c->modulation = QAM_32;
  102. break;
  103. case 2:
  104. c->modulation = QAM_64;
  105. break;
  106. case 3:
  107. c->modulation = QAM_128;
  108. break;
  109. case 4:
  110. c->modulation = QAM_256;
  111. break;
  112. }
  113. switch ((buf[0] >> 7) & 0x01) {
  114. case 0:
  115. c->inversion = INVERSION_OFF;
  116. break;
  117. case 1:
  118. c->inversion = INVERSION_ON;
  119. break;
  120. }
  121. return ret;
  122. error:
  123. dbg("%s: failed:%d", __func__, ret);
  124. return ret;
  125. }
  126. int cxd2820r_read_ber_c(struct dvb_frontend *fe, u32 *ber)
  127. {
  128. struct cxd2820r_priv *priv = fe->demodulator_priv;
  129. int ret;
  130. u8 buf[3], start_ber = 0;
  131. *ber = 0;
  132. if (priv->ber_running) {
  133. ret = cxd2820r_rd_regs(priv, 0x10076, buf, sizeof(buf));
  134. if (ret)
  135. goto error;
  136. if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
  137. *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
  138. start_ber = 1;
  139. }
  140. } else {
  141. priv->ber_running = 1;
  142. start_ber = 1;
  143. }
  144. if (start_ber) {
  145. /* (re)start BER */
  146. ret = cxd2820r_wr_reg(priv, 0x10079, 0x01);
  147. if (ret)
  148. goto error;
  149. }
  150. return ret;
  151. error:
  152. dbg("%s: failed:%d", __func__, ret);
  153. return ret;
  154. }
  155. int cxd2820r_read_signal_strength_c(struct dvb_frontend *fe,
  156. u16 *strength)
  157. {
  158. struct cxd2820r_priv *priv = fe->demodulator_priv;
  159. int ret;
  160. u8 buf[2];
  161. u16 tmp;
  162. ret = cxd2820r_rd_regs(priv, 0x10049, buf, sizeof(buf));
  163. if (ret)
  164. goto error;
  165. tmp = (buf[0] & 0x03) << 8 | buf[1];
  166. tmp = (~tmp & 0x03ff);
  167. if (tmp == 512)
  168. /* ~no signal */
  169. tmp = 0;
  170. else if (tmp > 350)
  171. tmp = 350;
  172. /* scale value to 0x0000-0xffff */
  173. *strength = tmp * 0xffff / (350-0);
  174. return ret;
  175. error:
  176. dbg("%s: failed:%d", __func__, ret);
  177. return ret;
  178. }
  179. int cxd2820r_read_snr_c(struct dvb_frontend *fe, u16 *snr)
  180. {
  181. struct cxd2820r_priv *priv = fe->demodulator_priv;
  182. int ret;
  183. u8 tmp;
  184. unsigned int A, B;
  185. /* report SNR in dB * 10 */
  186. ret = cxd2820r_rd_reg(priv, 0x10019, &tmp);
  187. if (ret)
  188. goto error;
  189. if (((tmp >> 0) & 0x03) % 2) {
  190. A = 875;
  191. B = 650;
  192. } else {
  193. A = 950;
  194. B = 760;
  195. }
  196. ret = cxd2820r_rd_reg(priv, 0x1004d, &tmp);
  197. if (ret)
  198. goto error;
  199. #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
  200. if (tmp)
  201. *snr = A * (intlog2(B / tmp) >> 5) / (CXD2820R_LOG2_E_24 >> 5)
  202. / 10;
  203. else
  204. *snr = 0;
  205. return ret;
  206. error:
  207. dbg("%s: failed:%d", __func__, ret);
  208. return ret;
  209. }
  210. int cxd2820r_read_ucblocks_c(struct dvb_frontend *fe, u32 *ucblocks)
  211. {
  212. *ucblocks = 0;
  213. /* no way to read ? */
  214. return 0;
  215. }
  216. int cxd2820r_read_status_c(struct dvb_frontend *fe, fe_status_t *status)
  217. {
  218. struct cxd2820r_priv *priv = fe->demodulator_priv;
  219. int ret;
  220. u8 buf[2];
  221. *status = 0;
  222. ret = cxd2820r_rd_regs(priv, 0x10088, buf, sizeof(buf));
  223. if (ret)
  224. goto error;
  225. if (((buf[0] >> 0) & 0x01) == 1) {
  226. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  227. FE_HAS_VITERBI | FE_HAS_SYNC;
  228. if (((buf[1] >> 3) & 0x01) == 1) {
  229. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  230. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  231. }
  232. }
  233. dbg("%s: lock=%02x %02x", __func__, buf[0], buf[1]);
  234. return ret;
  235. error:
  236. dbg("%s: failed:%d", __func__, ret);
  237. return ret;
  238. }
  239. int cxd2820r_init_c(struct dvb_frontend *fe)
  240. {
  241. struct cxd2820r_priv *priv = fe->demodulator_priv;
  242. int ret;
  243. ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
  244. if (ret)
  245. goto error;
  246. return ret;
  247. error:
  248. dbg("%s: failed:%d", __func__, ret);
  249. return ret;
  250. }
  251. int cxd2820r_sleep_c(struct dvb_frontend *fe)
  252. {
  253. struct cxd2820r_priv *priv = fe->demodulator_priv;
  254. int ret, i;
  255. struct reg_val_mask tab[] = {
  256. { 0x000ff, 0x1f, 0xff },
  257. { 0x00085, 0x00, 0xff },
  258. { 0x00088, 0x01, 0xff },
  259. { 0x00081, 0x00, 0xff },
  260. { 0x00080, 0x00, 0xff },
  261. };
  262. dbg("%s", __func__);
  263. priv->delivery_system = SYS_UNDEFINED;
  264. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  265. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  266. tab[i].mask);
  267. if (ret)
  268. goto error;
  269. }
  270. return ret;
  271. error:
  272. dbg("%s: failed:%d", __func__, ret);
  273. return ret;
  274. }
  275. int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
  276. struct dvb_frontend_tune_settings *s)
  277. {
  278. s->min_delay_ms = 500;
  279. s->step_size = 0; /* no zigzag */
  280. s->max_drift = 0;
  281. return 0;
  282. }