cxd2820r_c.c 7.1 KB

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