cxd2820r_c.c 6.9 KB

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