cxd2820r_c.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Sony CXD2820R demodulator driver
  4. *
  5. * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
  6. */
  7. #include "cxd2820r_priv.h"
  8. int cxd2820r_set_frontend_c(struct dvb_frontend *fe)
  9. {
  10. struct cxd2820r_priv *priv = fe->demodulator_priv;
  11. struct i2c_client *client = priv->client[0];
  12. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  13. int ret;
  14. unsigned int utmp;
  15. u8 buf[2];
  16. u32 if_frequency;
  17. struct reg_val_mask tab[] = {
  18. { 0x00080, 0x01, 0xff },
  19. { 0x00081, 0x05, 0xff },
  20. { 0x00085, 0x07, 0xff },
  21. { 0x00088, 0x01, 0xff },
  22. { 0x00082, 0x20, 0x60 },
  23. { 0x1016a, 0x48, 0xff },
  24. { 0x100a5, 0x00, 0x01 },
  25. { 0x10020, 0x06, 0x07 },
  26. { 0x10059, 0x50, 0xff },
  27. { 0x10087, 0x0c, 0x3c },
  28. { 0x1008b, 0x07, 0xff },
  29. { 0x1001f, priv->if_agc_polarity << 7, 0x80 },
  30. { 0x10070, priv->ts_mode, 0xff },
  31. { 0x10071, !priv->ts_clk_inv << 4, 0x10 },
  32. };
  33. dev_dbg(&client->dev,
  34. "delivery_system=%d modulation=%d frequency=%u symbol_rate=%u inversion=%d\n",
  35. c->delivery_system, c->modulation, c->frequency,
  36. c->symbol_rate, c->inversion);
  37. /* program tuner */
  38. if (fe->ops.tuner_ops.set_params)
  39. fe->ops.tuner_ops.set_params(fe);
  40. if (priv->delivery_system != SYS_DVBC_ANNEX_A) {
  41. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  42. if (ret)
  43. goto error;
  44. }
  45. priv->delivery_system = SYS_DVBC_ANNEX_A;
  46. priv->ber_running = false; /* tune stops BER counter */
  47. /* program IF frequency */
  48. if (fe->ops.tuner_ops.get_if_frequency) {
  49. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  50. if (ret)
  51. goto error;
  52. dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
  53. } else {
  54. ret = -EINVAL;
  55. goto error;
  56. }
  57. utmp = 0x4000 - DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x4000, CXD2820R_CLK);
  58. buf[0] = (utmp >> 8) & 0xff;
  59. buf[1] = (utmp >> 0) & 0xff;
  60. ret = regmap_bulk_write(priv->regmap[1], 0x0042, buf, 2);
  61. if (ret)
  62. goto error;
  63. ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
  64. if (ret)
  65. goto error;
  66. ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
  67. if (ret)
  68. goto error;
  69. return ret;
  70. error:
  71. dev_dbg(&client->dev, "failed=%d\n", ret);
  72. return ret;
  73. }
  74. int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
  75. struct dtv_frontend_properties *c)
  76. {
  77. struct cxd2820r_priv *priv = fe->demodulator_priv;
  78. struct i2c_client *client = priv->client[0];
  79. int ret;
  80. unsigned int utmp;
  81. u8 buf[2];
  82. dev_dbg(&client->dev, "\n");
  83. ret = regmap_bulk_read(priv->regmap[1], 0x001a, buf, 2);
  84. if (ret)
  85. goto error;
  86. c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
  87. ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
  88. if (ret)
  89. goto error;
  90. switch ((utmp >> 0) & 0x07) {
  91. case 0:
  92. c->modulation = QAM_16;
  93. break;
  94. case 1:
  95. c->modulation = QAM_32;
  96. break;
  97. case 2:
  98. c->modulation = QAM_64;
  99. break;
  100. case 3:
  101. c->modulation = QAM_128;
  102. break;
  103. case 4:
  104. c->modulation = QAM_256;
  105. break;
  106. }
  107. switch ((utmp >> 7) & 0x01) {
  108. case 0:
  109. c->inversion = INVERSION_OFF;
  110. break;
  111. case 1:
  112. c->inversion = INVERSION_ON;
  113. break;
  114. }
  115. return ret;
  116. error:
  117. dev_dbg(&client->dev, "failed=%d\n", ret);
  118. return ret;
  119. }
  120. int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status)
  121. {
  122. struct cxd2820r_priv *priv = fe->demodulator_priv;
  123. struct i2c_client *client = priv->client[0];
  124. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  125. int ret;
  126. unsigned int utmp, utmp1, utmp2;
  127. u8 buf[3];
  128. /* Lock detection */
  129. ret = regmap_bulk_read(priv->regmap[1], 0x0088, &buf[0], 1);
  130. if (ret)
  131. goto error;
  132. ret = regmap_bulk_read(priv->regmap[1], 0x0073, &buf[1], 1);
  133. if (ret)
  134. goto error;
  135. utmp1 = (buf[0] >> 0) & 0x01;
  136. utmp2 = (buf[1] >> 3) & 0x01;
  137. if (utmp1 == 1 && utmp2 == 1) {
  138. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  139. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  140. } else if (utmp1 == 1 || utmp2 == 1) {
  141. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  142. FE_HAS_VITERBI | FE_HAS_SYNC;
  143. } else {
  144. *status = 0;
  145. }
  146. dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
  147. *status, 2, buf, utmp1, utmp2);
  148. /* Signal strength */
  149. if (*status & FE_HAS_SIGNAL) {
  150. unsigned int strength;
  151. ret = regmap_bulk_read(priv->regmap[1], 0x0049, buf, 2);
  152. if (ret)
  153. goto error;
  154. utmp = buf[0] << 8 | buf[1] << 0;
  155. utmp = 511 - sign_extend32(utmp, 9);
  156. /* Scale value to 0x0000-0xffff */
  157. strength = utmp << 6 | utmp >> 4;
  158. c->strength.len = 1;
  159. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  160. c->strength.stat[0].uvalue = strength;
  161. } else {
  162. c->strength.len = 1;
  163. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  164. }
  165. /* CNR */
  166. if (*status & FE_HAS_VITERBI) {
  167. unsigned int cnr, const_a, const_b;
  168. ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
  169. if (ret)
  170. goto error;
  171. if (((utmp >> 0) & 0x03) % 2) {
  172. const_a = 8750;
  173. const_b = 650;
  174. } else {
  175. const_a = 9500;
  176. const_b = 760;
  177. }
  178. ret = regmap_read(priv->regmap[1], 0x004d, &utmp);
  179. if (ret)
  180. goto error;
  181. #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
  182. if (utmp)
  183. cnr = div_u64((u64)(intlog2(const_b) - intlog2(utmp))
  184. * const_a, CXD2820R_LOG2_E_24);
  185. else
  186. cnr = 0;
  187. c->cnr.len = 1;
  188. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  189. c->cnr.stat[0].svalue = cnr;
  190. } else {
  191. c->cnr.len = 1;
  192. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  193. }
  194. /* BER */
  195. if (*status & FE_HAS_SYNC) {
  196. unsigned int post_bit_error;
  197. bool start_ber;
  198. if (priv->ber_running) {
  199. ret = regmap_bulk_read(priv->regmap[1], 0x0076, buf, 3);
  200. if (ret)
  201. goto error;
  202. if ((buf[2] >> 7) & 0x01) {
  203. post_bit_error = buf[2] << 16 | buf[1] << 8 |
  204. buf[0] << 0;
  205. post_bit_error &= 0x0fffff;
  206. start_ber = true;
  207. } else {
  208. post_bit_error = 0;
  209. start_ber = false;
  210. }
  211. } else {
  212. post_bit_error = 0;
  213. start_ber = true;
  214. }
  215. if (start_ber) {
  216. ret = regmap_write(priv->regmap[1], 0x0079, 0x01);
  217. if (ret)
  218. goto error;
  219. priv->ber_running = true;
  220. }
  221. priv->post_bit_error += post_bit_error;
  222. c->post_bit_error.len = 1;
  223. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  224. c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
  225. } else {
  226. c->post_bit_error.len = 1;
  227. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  228. }
  229. return ret;
  230. error:
  231. dev_dbg(&client->dev, "failed=%d\n", ret);
  232. return ret;
  233. }
  234. int cxd2820r_init_c(struct dvb_frontend *fe)
  235. {
  236. struct cxd2820r_priv *priv = fe->demodulator_priv;
  237. struct i2c_client *client = priv->client[0];
  238. int ret;
  239. dev_dbg(&client->dev, "\n");
  240. ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
  241. if (ret)
  242. goto error;
  243. return ret;
  244. error:
  245. dev_dbg(&client->dev, "failed=%d\n", ret);
  246. return ret;
  247. }
  248. int cxd2820r_sleep_c(struct dvb_frontend *fe)
  249. {
  250. struct cxd2820r_priv *priv = fe->demodulator_priv;
  251. struct i2c_client *client = priv->client[0];
  252. int ret;
  253. struct reg_val_mask tab[] = {
  254. { 0x000ff, 0x1f, 0xff },
  255. { 0x00085, 0x00, 0xff },
  256. { 0x00088, 0x01, 0xff },
  257. { 0x00081, 0x00, 0xff },
  258. { 0x00080, 0x00, 0xff },
  259. };
  260. dev_dbg(&client->dev, "\n");
  261. priv->delivery_system = SYS_UNDEFINED;
  262. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  263. if (ret)
  264. goto error;
  265. return ret;
  266. error:
  267. dev_dbg(&client->dev, "failed=%d\n", ret);
  268. return ret;
  269. }
  270. int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
  271. struct dvb_frontend_tune_settings *s)
  272. {
  273. s->min_delay_ms = 500;
  274. s->step_size = 0; /* no zigzag */
  275. s->max_drift = 0;
  276. return 0;
  277. }