cxd2820r_c.c 7.2 KB

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