cxd2820r_t.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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_t(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, bw_i;
  14. unsigned int utmp;
  15. u32 if_frequency;
  16. u8 buf[3], bw_param;
  17. u8 bw_params1[][5] = {
  18. { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
  19. { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
  20. { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
  21. };
  22. u8 bw_params2[][2] = {
  23. { 0x1f, 0xdc }, /* 6 MHz */
  24. { 0x12, 0xf8 }, /* 7 MHz */
  25. { 0x01, 0xe0 }, /* 8 MHz */
  26. };
  27. struct reg_val_mask tab[] = {
  28. { 0x00080, 0x00, 0xff },
  29. { 0x00081, 0x03, 0xff },
  30. { 0x00085, 0x07, 0xff },
  31. { 0x00088, 0x01, 0xff },
  32. { 0x00070, priv->ts_mode, 0xff },
  33. { 0x00071, !priv->ts_clk_inv << 4, 0x10 },
  34. { 0x000cb, priv->if_agc_polarity << 6, 0x40 },
  35. { 0x000a5, 0x00, 0x01 },
  36. { 0x00082, 0x20, 0x60 },
  37. { 0x000c2, 0xc3, 0xff },
  38. { 0x0016a, 0x50, 0xff },
  39. { 0x00427, 0x41, 0xff },
  40. };
  41. dev_dbg(&client->dev,
  42. "delivery_system=%d modulation=%d frequency=%u bandwidth_hz=%u inversion=%d\n",
  43. c->delivery_system, c->modulation, c->frequency,
  44. c->bandwidth_hz, c->inversion);
  45. switch (c->bandwidth_hz) {
  46. case 6000000:
  47. bw_i = 0;
  48. bw_param = 2;
  49. break;
  50. case 7000000:
  51. bw_i = 1;
  52. bw_param = 1;
  53. break;
  54. case 8000000:
  55. bw_i = 2;
  56. bw_param = 0;
  57. break;
  58. default:
  59. return -EINVAL;
  60. }
  61. /* program tuner */
  62. if (fe->ops.tuner_ops.set_params)
  63. fe->ops.tuner_ops.set_params(fe);
  64. if (priv->delivery_system != SYS_DVBT) {
  65. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  66. if (ret)
  67. goto error;
  68. }
  69. priv->delivery_system = SYS_DVBT;
  70. priv->ber_running = false; /* tune stops BER counter */
  71. /* program IF frequency */
  72. if (fe->ops.tuner_ops.get_if_frequency) {
  73. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  74. if (ret)
  75. goto error;
  76. dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
  77. } else {
  78. ret = -EINVAL;
  79. goto error;
  80. }
  81. utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, CXD2820R_CLK);
  82. buf[0] = (utmp >> 16) & 0xff;
  83. buf[1] = (utmp >> 8) & 0xff;
  84. buf[2] = (utmp >> 0) & 0xff;
  85. ret = regmap_bulk_write(priv->regmap[0], 0x00b6, buf, 3);
  86. if (ret)
  87. goto error;
  88. ret = regmap_bulk_write(priv->regmap[0], 0x009f, bw_params1[bw_i], 5);
  89. if (ret)
  90. goto error;
  91. ret = regmap_update_bits(priv->regmap[0], 0x00d7, 0xc0, bw_param << 6);
  92. if (ret)
  93. goto error;
  94. ret = regmap_bulk_write(priv->regmap[0], 0x00d9, bw_params2[bw_i], 2);
  95. if (ret)
  96. goto error;
  97. ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
  98. if (ret)
  99. goto error;
  100. ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
  101. if (ret)
  102. goto error;
  103. return ret;
  104. error:
  105. dev_dbg(&client->dev, "failed=%d\n", ret);
  106. return ret;
  107. }
  108. int cxd2820r_get_frontend_t(struct dvb_frontend *fe,
  109. struct dtv_frontend_properties *c)
  110. {
  111. struct cxd2820r_priv *priv = fe->demodulator_priv;
  112. struct i2c_client *client = priv->client[0];
  113. int ret;
  114. unsigned int utmp;
  115. u8 buf[2];
  116. dev_dbg(&client->dev, "\n");
  117. ret = regmap_bulk_read(priv->regmap[0], 0x002f, buf, sizeof(buf));
  118. if (ret)
  119. goto error;
  120. switch ((buf[0] >> 6) & 0x03) {
  121. case 0:
  122. c->modulation = QPSK;
  123. break;
  124. case 1:
  125. c->modulation = QAM_16;
  126. break;
  127. case 2:
  128. c->modulation = QAM_64;
  129. break;
  130. }
  131. switch ((buf[1] >> 1) & 0x03) {
  132. case 0:
  133. c->transmission_mode = TRANSMISSION_MODE_2K;
  134. break;
  135. case 1:
  136. c->transmission_mode = TRANSMISSION_MODE_8K;
  137. break;
  138. }
  139. switch ((buf[1] >> 3) & 0x03) {
  140. case 0:
  141. c->guard_interval = GUARD_INTERVAL_1_32;
  142. break;
  143. case 1:
  144. c->guard_interval = GUARD_INTERVAL_1_16;
  145. break;
  146. case 2:
  147. c->guard_interval = GUARD_INTERVAL_1_8;
  148. break;
  149. case 3:
  150. c->guard_interval = GUARD_INTERVAL_1_4;
  151. break;
  152. }
  153. switch ((buf[0] >> 3) & 0x07) {
  154. case 0:
  155. c->hierarchy = HIERARCHY_NONE;
  156. break;
  157. case 1:
  158. c->hierarchy = HIERARCHY_1;
  159. break;
  160. case 2:
  161. c->hierarchy = HIERARCHY_2;
  162. break;
  163. case 3:
  164. c->hierarchy = HIERARCHY_4;
  165. break;
  166. }
  167. switch ((buf[0] >> 0) & 0x07) {
  168. case 0:
  169. c->code_rate_HP = FEC_1_2;
  170. break;
  171. case 1:
  172. c->code_rate_HP = FEC_2_3;
  173. break;
  174. case 2:
  175. c->code_rate_HP = FEC_3_4;
  176. break;
  177. case 3:
  178. c->code_rate_HP = FEC_5_6;
  179. break;
  180. case 4:
  181. c->code_rate_HP = FEC_7_8;
  182. break;
  183. }
  184. switch ((buf[1] >> 5) & 0x07) {
  185. case 0:
  186. c->code_rate_LP = FEC_1_2;
  187. break;
  188. case 1:
  189. c->code_rate_LP = FEC_2_3;
  190. break;
  191. case 2:
  192. c->code_rate_LP = FEC_3_4;
  193. break;
  194. case 3:
  195. c->code_rate_LP = FEC_5_6;
  196. break;
  197. case 4:
  198. c->code_rate_LP = FEC_7_8;
  199. break;
  200. }
  201. ret = regmap_read(priv->regmap[0], 0x07c6, &utmp);
  202. if (ret)
  203. goto error;
  204. switch ((utmp >> 0) & 0x01) {
  205. case 0:
  206. c->inversion = INVERSION_OFF;
  207. break;
  208. case 1:
  209. c->inversion = INVERSION_ON;
  210. break;
  211. }
  212. return ret;
  213. error:
  214. dev_dbg(&client->dev, "failed=%d\n", ret);
  215. return ret;
  216. }
  217. int cxd2820r_read_status_t(struct dvb_frontend *fe, enum fe_status *status)
  218. {
  219. struct cxd2820r_priv *priv = fe->demodulator_priv;
  220. struct i2c_client *client = priv->client[0];
  221. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  222. int ret;
  223. unsigned int utmp, utmp1, utmp2;
  224. u8 buf[3];
  225. /* Lock detection */
  226. ret = regmap_bulk_read(priv->regmap[0], 0x0010, &buf[0], 1);
  227. if (ret)
  228. goto error;
  229. ret = regmap_bulk_read(priv->regmap[0], 0x0073, &buf[1], 1);
  230. if (ret)
  231. goto error;
  232. utmp1 = (buf[0] >> 0) & 0x07;
  233. utmp2 = (buf[1] >> 3) & 0x01;
  234. if (utmp1 == 6 && utmp2 == 1) {
  235. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  236. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  237. } else if (utmp1 == 6 || utmp2 == 1) {
  238. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  239. FE_HAS_VITERBI | FE_HAS_SYNC;
  240. } else {
  241. *status = 0;
  242. }
  243. dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
  244. *status, 2, buf, utmp1, utmp2);
  245. /* Signal strength */
  246. if (*status & FE_HAS_SIGNAL) {
  247. unsigned int strength;
  248. ret = regmap_bulk_read(priv->regmap[0], 0x0026, buf, 2);
  249. if (ret)
  250. goto error;
  251. utmp = buf[0] << 8 | buf[1] << 0;
  252. utmp = ~utmp & 0x0fff;
  253. /* Scale value to 0x0000-0xffff */
  254. strength = utmp << 4 | utmp >> 8;
  255. c->strength.len = 1;
  256. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  257. c->strength.stat[0].uvalue = strength;
  258. } else {
  259. c->strength.len = 1;
  260. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  261. }
  262. /* CNR */
  263. if (*status & FE_HAS_VITERBI) {
  264. unsigned int cnr;
  265. ret = regmap_bulk_read(priv->regmap[0], 0x002c, buf, 2);
  266. if (ret)
  267. goto error;
  268. utmp = buf[0] << 8 | buf[1] << 0;
  269. if (utmp)
  270. cnr = div_u64((u64)(intlog10(utmp)
  271. - intlog10(32000 - utmp) + 55532585)
  272. * 10000, (1 << 24));
  273. else
  274. cnr = 0;
  275. c->cnr.len = 1;
  276. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  277. c->cnr.stat[0].svalue = cnr;
  278. } else {
  279. c->cnr.len = 1;
  280. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  281. }
  282. /* BER */
  283. if (*status & FE_HAS_SYNC) {
  284. unsigned int post_bit_error;
  285. bool start_ber;
  286. if (priv->ber_running) {
  287. ret = regmap_bulk_read(priv->regmap[0], 0x0076, buf, 3);
  288. if (ret)
  289. goto error;
  290. if ((buf[2] >> 7) & 0x01) {
  291. post_bit_error = buf[2] << 16 | buf[1] << 8 |
  292. buf[0] << 0;
  293. post_bit_error &= 0x0fffff;
  294. start_ber = true;
  295. } else {
  296. post_bit_error = 0;
  297. start_ber = false;
  298. }
  299. } else {
  300. post_bit_error = 0;
  301. start_ber = true;
  302. }
  303. if (start_ber) {
  304. ret = regmap_write(priv->regmap[0], 0x0079, 0x01);
  305. if (ret)
  306. goto error;
  307. priv->ber_running = true;
  308. }
  309. priv->post_bit_error += post_bit_error;
  310. c->post_bit_error.len = 1;
  311. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  312. c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
  313. } else {
  314. c->post_bit_error.len = 1;
  315. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  316. }
  317. return ret;
  318. error:
  319. dev_dbg(&client->dev, "failed=%d\n", ret);
  320. return ret;
  321. }
  322. int cxd2820r_init_t(struct dvb_frontend *fe)
  323. {
  324. struct cxd2820r_priv *priv = fe->demodulator_priv;
  325. struct i2c_client *client = priv->client[0];
  326. int ret;
  327. dev_dbg(&client->dev, "\n");
  328. ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
  329. if (ret)
  330. goto error;
  331. return ret;
  332. error:
  333. dev_dbg(&client->dev, "failed=%d\n", ret);
  334. return ret;
  335. }
  336. int cxd2820r_sleep_t(struct dvb_frontend *fe)
  337. {
  338. struct cxd2820r_priv *priv = fe->demodulator_priv;
  339. struct i2c_client *client = priv->client[0];
  340. int ret;
  341. struct reg_val_mask tab[] = {
  342. { 0x000ff, 0x1f, 0xff },
  343. { 0x00085, 0x00, 0xff },
  344. { 0x00088, 0x01, 0xff },
  345. { 0x00081, 0x00, 0xff },
  346. { 0x00080, 0x00, 0xff },
  347. };
  348. dev_dbg(&client->dev, "\n");
  349. priv->delivery_system = SYS_UNDEFINED;
  350. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  351. if (ret)
  352. goto error;
  353. return ret;
  354. error:
  355. dev_dbg(&client->dev, "failed=%d\n", ret);
  356. return ret;
  357. }
  358. int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
  359. struct dvb_frontend_tune_settings *s)
  360. {
  361. s->min_delay_ms = 500;
  362. s->step_size = fe->ops.info.frequency_stepsize_hz * 2;
  363. s->max_drift = (fe->ops.info.frequency_stepsize_hz * 2) + 1;
  364. return 0;
  365. }