cxd2820r_c.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 i2c_client *client = priv->client[0];
  25. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  26. int ret;
  27. unsigned int utmp;
  28. u8 buf[2];
  29. u32 if_frequency;
  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->if_agc_polarity << 7, 0x80 },
  43. { 0x10070, priv->ts_mode, 0xff },
  44. { 0x10071, !priv->ts_clk_inv << 4, 0x10 },
  45. };
  46. dev_dbg(&client->dev,
  47. "delivery_system=%d modulation=%d frequency=%u symbol_rate=%u inversion=%d\n",
  48. c->delivery_system, c->modulation, c->frequency,
  49. c->symbol_rate, c->inversion);
  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. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  55. if (ret)
  56. goto error;
  57. }
  58. priv->delivery_system = SYS_DVBC_ANNEX_A;
  59. priv->ber_running = false; /* 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_frequency);
  63. if (ret)
  64. goto error;
  65. dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
  66. } else {
  67. ret = -EINVAL;
  68. goto error;
  69. }
  70. utmp = 0x4000 - DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x4000, CXD2820R_CLK);
  71. buf[0] = (utmp >> 8) & 0xff;
  72. buf[1] = (utmp >> 0) & 0xff;
  73. ret = regmap_bulk_write(priv->regmap[1], 0x0042, buf, 2);
  74. if (ret)
  75. goto error;
  76. ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
  77. if (ret)
  78. goto error;
  79. ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
  80. if (ret)
  81. goto error;
  82. return ret;
  83. error:
  84. dev_dbg(&client->dev, "failed=%d\n", ret);
  85. return ret;
  86. }
  87. int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
  88. struct dtv_frontend_properties *c)
  89. {
  90. struct cxd2820r_priv *priv = fe->demodulator_priv;
  91. struct i2c_client *client = priv->client[0];
  92. int ret;
  93. unsigned int utmp;
  94. u8 buf[2];
  95. dev_dbg(&client->dev, "\n");
  96. ret = regmap_bulk_read(priv->regmap[1], 0x001a, buf, 2);
  97. if (ret)
  98. goto error;
  99. c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
  100. ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
  101. if (ret)
  102. goto error;
  103. switch ((utmp >> 0) & 0x07) {
  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 ((utmp >> 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. dev_dbg(&client->dev, "failed=%d\n", ret);
  131. return ret;
  132. }
  133. int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status)
  134. {
  135. struct cxd2820r_priv *priv = fe->demodulator_priv;
  136. struct i2c_client *client = priv->client[0];
  137. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  138. int ret;
  139. unsigned int utmp, utmp1, utmp2;
  140. u8 buf[3];
  141. /* Lock detection */
  142. ret = regmap_bulk_read(priv->regmap[1], 0x0088, &buf[0], 1);
  143. if (ret)
  144. goto error;
  145. ret = regmap_bulk_read(priv->regmap[1], 0x0073, &buf[1], 1);
  146. if (ret)
  147. goto error;
  148. utmp1 = (buf[0] >> 0) & 0x01;
  149. utmp2 = (buf[1] >> 3) & 0x01;
  150. if (utmp1 == 1 && utmp2 == 1) {
  151. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  152. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  153. } else if (utmp1 == 1 || utmp2 == 1) {
  154. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  155. FE_HAS_VITERBI | FE_HAS_SYNC;
  156. } else {
  157. *status = 0;
  158. }
  159. dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
  160. *status, 2, buf, utmp1, utmp2);
  161. /* Signal strength */
  162. if (*status & FE_HAS_SIGNAL) {
  163. unsigned int strength;
  164. ret = regmap_bulk_read(priv->regmap[1], 0x0049, buf, 2);
  165. if (ret)
  166. goto error;
  167. utmp = buf[0] << 8 | buf[1] << 0;
  168. utmp = 511 - sign_extend32(utmp, 9);
  169. /* Scale value to 0x0000-0xffff */
  170. strength = utmp << 6 | utmp >> 4;
  171. c->strength.len = 1;
  172. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  173. c->strength.stat[0].uvalue = strength;
  174. } else {
  175. c->strength.len = 1;
  176. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  177. }
  178. /* CNR */
  179. if (*status & FE_HAS_VITERBI) {
  180. unsigned int cnr, const_a, const_b;
  181. ret = regmap_read(priv->regmap[1], 0x0019, &utmp);
  182. if (ret)
  183. goto error;
  184. if (((utmp >> 0) & 0x03) % 2) {
  185. const_a = 8750;
  186. const_b = 650;
  187. } else {
  188. const_a = 9500;
  189. const_b = 760;
  190. }
  191. ret = regmap_read(priv->regmap[1], 0x004d, &utmp);
  192. if (ret)
  193. goto error;
  194. #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
  195. if (utmp)
  196. cnr = div_u64((u64)(intlog2(const_b) - intlog2(utmp))
  197. * const_a, CXD2820R_LOG2_E_24);
  198. else
  199. cnr = 0;
  200. c->cnr.len = 1;
  201. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  202. c->cnr.stat[0].svalue = cnr;
  203. } else {
  204. c->cnr.len = 1;
  205. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  206. }
  207. /* BER */
  208. if (*status & FE_HAS_SYNC) {
  209. unsigned int post_bit_error;
  210. bool start_ber;
  211. if (priv->ber_running) {
  212. ret = regmap_bulk_read(priv->regmap[1], 0x0076, buf, 3);
  213. if (ret)
  214. goto error;
  215. if ((buf[2] >> 7) & 0x01) {
  216. post_bit_error = buf[2] << 16 | buf[1] << 8 |
  217. buf[0] << 0;
  218. post_bit_error &= 0x0fffff;
  219. start_ber = true;
  220. } else {
  221. post_bit_error = 0;
  222. start_ber = false;
  223. }
  224. } else {
  225. post_bit_error = 0;
  226. start_ber = true;
  227. }
  228. if (start_ber) {
  229. ret = regmap_write(priv->regmap[1], 0x0079, 0x01);
  230. if (ret)
  231. goto error;
  232. priv->ber_running = true;
  233. }
  234. priv->post_bit_error += post_bit_error;
  235. c->post_bit_error.len = 1;
  236. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  237. c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
  238. } else {
  239. c->post_bit_error.len = 1;
  240. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  241. }
  242. return ret;
  243. error:
  244. dev_dbg(&client->dev, "failed=%d\n", ret);
  245. return ret;
  246. }
  247. int cxd2820r_init_c(struct dvb_frontend *fe)
  248. {
  249. struct cxd2820r_priv *priv = fe->demodulator_priv;
  250. struct i2c_client *client = priv->client[0];
  251. int ret;
  252. dev_dbg(&client->dev, "\n");
  253. ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
  254. if (ret)
  255. goto error;
  256. return ret;
  257. error:
  258. dev_dbg(&client->dev, "failed=%d\n", ret);
  259. return ret;
  260. }
  261. int cxd2820r_sleep_c(struct dvb_frontend *fe)
  262. {
  263. struct cxd2820r_priv *priv = fe->demodulator_priv;
  264. struct i2c_client *client = priv->client[0];
  265. int ret;
  266. struct reg_val_mask tab[] = {
  267. { 0x000ff, 0x1f, 0xff },
  268. { 0x00085, 0x00, 0xff },
  269. { 0x00088, 0x01, 0xff },
  270. { 0x00081, 0x00, 0xff },
  271. { 0x00080, 0x00, 0xff },
  272. };
  273. dev_dbg(&client->dev, "\n");
  274. priv->delivery_system = SYS_UNDEFINED;
  275. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  276. if (ret)
  277. goto error;
  278. return ret;
  279. error:
  280. dev_dbg(&client->dev, "failed=%d\n", ret);
  281. return ret;
  282. }
  283. int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
  284. struct dvb_frontend_tune_settings *s)
  285. {
  286. s->min_delay_ms = 500;
  287. s->step_size = 0; /* no zigzag */
  288. s->max_drift = 0;
  289. return 0;
  290. }