cxd2820r_c.c 7.3 KB

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