cxd2820r_c.c 7.3 KB

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