cxd2820r_t2.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_t2(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. { 0x1c, 0xb3, 0x33, 0x33, 0x33 }, /* 5 MHz */
  19. { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
  20. { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
  21. { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
  22. };
  23. struct reg_val_mask tab[] = {
  24. { 0x00080, 0x02, 0xff },
  25. { 0x00081, 0x20, 0xff },
  26. { 0x00085, 0x07, 0xff },
  27. { 0x00088, 0x01, 0xff },
  28. { 0x02069, 0x01, 0xff },
  29. { 0x0207f, 0x2a, 0xff },
  30. { 0x02082, 0x0a, 0xff },
  31. { 0x02083, 0x0a, 0xff },
  32. { 0x020cb, priv->if_agc_polarity << 6, 0x40 },
  33. { 0x02070, priv->ts_mode, 0xff },
  34. { 0x02071, !priv->ts_clk_inv << 6, 0x40 },
  35. { 0x020b5, priv->spec_inv << 4, 0x10 },
  36. { 0x02567, 0x07, 0x0f },
  37. { 0x02569, 0x03, 0x03 },
  38. { 0x02595, 0x1a, 0xff },
  39. { 0x02596, 0x50, 0xff },
  40. { 0x02a8c, 0x00, 0xff },
  41. { 0x02a8d, 0x34, 0xff },
  42. { 0x02a45, 0x06, 0x07 },
  43. { 0x03f10, 0x0d, 0xff },
  44. { 0x03f11, 0x02, 0xff },
  45. { 0x03f12, 0x01, 0xff },
  46. { 0x03f23, 0x2c, 0xff },
  47. { 0x03f51, 0x13, 0xff },
  48. { 0x03f52, 0x01, 0xff },
  49. { 0x03f53, 0x00, 0xff },
  50. { 0x027e6, 0x14, 0xff },
  51. { 0x02786, 0x02, 0x07 },
  52. { 0x02787, 0x40, 0xe0 },
  53. { 0x027ef, 0x10, 0x18 },
  54. };
  55. dev_dbg(&client->dev,
  56. "delivery_system=%d modulation=%d frequency=%u bandwidth_hz=%u inversion=%d stream_id=%u\n",
  57. c->delivery_system, c->modulation, c->frequency,
  58. c->bandwidth_hz, c->inversion, c->stream_id);
  59. switch (c->bandwidth_hz) {
  60. case 5000000:
  61. bw_i = 0;
  62. bw_param = 3;
  63. break;
  64. case 6000000:
  65. bw_i = 1;
  66. bw_param = 2;
  67. break;
  68. case 7000000:
  69. bw_i = 2;
  70. bw_param = 1;
  71. break;
  72. case 8000000:
  73. bw_i = 3;
  74. bw_param = 0;
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. /* program tuner */
  80. if (fe->ops.tuner_ops.set_params)
  81. fe->ops.tuner_ops.set_params(fe);
  82. if (priv->delivery_system != SYS_DVBT2) {
  83. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  84. if (ret)
  85. goto error;
  86. }
  87. priv->delivery_system = SYS_DVBT2;
  88. /* program IF frequency */
  89. if (fe->ops.tuner_ops.get_if_frequency) {
  90. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  91. if (ret)
  92. goto error;
  93. dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
  94. } else {
  95. ret = -EINVAL;
  96. goto error;
  97. }
  98. utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, CXD2820R_CLK);
  99. buf[0] = (utmp >> 16) & 0xff;
  100. buf[1] = (utmp >> 8) & 0xff;
  101. buf[2] = (utmp >> 0) & 0xff;
  102. ret = regmap_bulk_write(priv->regmap[0], 0x20b6, buf, 3);
  103. if (ret)
  104. goto error;
  105. /* PLP filtering */
  106. if (c->stream_id > 255) {
  107. dev_dbg(&client->dev, "disable PLP filtering\n");
  108. ret = regmap_write(priv->regmap[0], 0x23ad, 0x00);
  109. if (ret)
  110. goto error;
  111. } else {
  112. dev_dbg(&client->dev, "enable PLP filtering\n");
  113. ret = regmap_write(priv->regmap[0], 0x23af, c->stream_id & 0xff);
  114. if (ret)
  115. goto error;
  116. ret = regmap_write(priv->regmap[0], 0x23ad, 0x01);
  117. if (ret)
  118. goto error;
  119. }
  120. ret = regmap_bulk_write(priv->regmap[0], 0x209f, bw_params1[bw_i], 5);
  121. if (ret)
  122. goto error;
  123. ret = regmap_update_bits(priv->regmap[0], 0x20d7, 0xc0, bw_param << 6);
  124. if (ret)
  125. goto error;
  126. ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
  127. if (ret)
  128. goto error;
  129. ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
  130. if (ret)
  131. goto error;
  132. return ret;
  133. error:
  134. dev_dbg(&client->dev, "failed=%d\n", ret);
  135. return ret;
  136. }
  137. int cxd2820r_get_frontend_t2(struct dvb_frontend *fe,
  138. struct dtv_frontend_properties *c)
  139. {
  140. struct cxd2820r_priv *priv = fe->demodulator_priv;
  141. struct i2c_client *client = priv->client[0];
  142. int ret;
  143. unsigned int utmp;
  144. u8 buf[2];
  145. dev_dbg(&client->dev, "\n");
  146. ret = regmap_bulk_read(priv->regmap[0], 0x205c, buf, 2);
  147. if (ret)
  148. goto error;
  149. switch ((buf[0] >> 0) & 0x07) {
  150. case 0:
  151. c->transmission_mode = TRANSMISSION_MODE_2K;
  152. break;
  153. case 1:
  154. c->transmission_mode = TRANSMISSION_MODE_8K;
  155. break;
  156. case 2:
  157. c->transmission_mode = TRANSMISSION_MODE_4K;
  158. break;
  159. case 3:
  160. c->transmission_mode = TRANSMISSION_MODE_1K;
  161. break;
  162. case 4:
  163. c->transmission_mode = TRANSMISSION_MODE_16K;
  164. break;
  165. case 5:
  166. c->transmission_mode = TRANSMISSION_MODE_32K;
  167. break;
  168. }
  169. switch ((buf[1] >> 4) & 0x07) {
  170. case 0:
  171. c->guard_interval = GUARD_INTERVAL_1_32;
  172. break;
  173. case 1:
  174. c->guard_interval = GUARD_INTERVAL_1_16;
  175. break;
  176. case 2:
  177. c->guard_interval = GUARD_INTERVAL_1_8;
  178. break;
  179. case 3:
  180. c->guard_interval = GUARD_INTERVAL_1_4;
  181. break;
  182. case 4:
  183. c->guard_interval = GUARD_INTERVAL_1_128;
  184. break;
  185. case 5:
  186. c->guard_interval = GUARD_INTERVAL_19_128;
  187. break;
  188. case 6:
  189. c->guard_interval = GUARD_INTERVAL_19_256;
  190. break;
  191. }
  192. ret = regmap_bulk_read(priv->regmap[0], 0x225b, buf, 2);
  193. if (ret)
  194. goto error;
  195. switch ((buf[0] >> 0) & 0x07) {
  196. case 0:
  197. c->fec_inner = FEC_1_2;
  198. break;
  199. case 1:
  200. c->fec_inner = FEC_3_5;
  201. break;
  202. case 2:
  203. c->fec_inner = FEC_2_3;
  204. break;
  205. case 3:
  206. c->fec_inner = FEC_3_4;
  207. break;
  208. case 4:
  209. c->fec_inner = FEC_4_5;
  210. break;
  211. case 5:
  212. c->fec_inner = FEC_5_6;
  213. break;
  214. }
  215. switch ((buf[1] >> 0) & 0x07) {
  216. case 0:
  217. c->modulation = QPSK;
  218. break;
  219. case 1:
  220. c->modulation = QAM_16;
  221. break;
  222. case 2:
  223. c->modulation = QAM_64;
  224. break;
  225. case 3:
  226. c->modulation = QAM_256;
  227. break;
  228. }
  229. ret = regmap_read(priv->regmap[0], 0x20b5, &utmp);
  230. if (ret)
  231. goto error;
  232. switch ((utmp >> 4) & 0x01) {
  233. case 0:
  234. c->inversion = INVERSION_OFF;
  235. break;
  236. case 1:
  237. c->inversion = INVERSION_ON;
  238. break;
  239. }
  240. return ret;
  241. error:
  242. dev_dbg(&client->dev, "failed=%d\n", ret);
  243. return ret;
  244. }
  245. int cxd2820r_read_status_t2(struct dvb_frontend *fe, enum fe_status *status)
  246. {
  247. struct cxd2820r_priv *priv = fe->demodulator_priv;
  248. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  249. struct i2c_client *client = priv->client[0];
  250. int ret;
  251. unsigned int utmp, utmp1, utmp2;
  252. u8 buf[4];
  253. /* Lock detection */
  254. ret = regmap_bulk_read(priv->regmap[0], 0x2010, &buf[0], 1);
  255. if (ret)
  256. goto error;
  257. utmp1 = (buf[0] >> 0) & 0x07;
  258. utmp2 = (buf[0] >> 5) & 0x01;
  259. if (utmp1 == 6 && utmp2 == 1) {
  260. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  261. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  262. } else if (utmp1 == 6 || utmp2 == 1) {
  263. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  264. FE_HAS_VITERBI | FE_HAS_SYNC;
  265. } else {
  266. *status = 0;
  267. }
  268. dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
  269. *status, 1, buf, utmp1, utmp2);
  270. /* Signal strength */
  271. if (*status & FE_HAS_SIGNAL) {
  272. unsigned int strength;
  273. ret = regmap_bulk_read(priv->regmap[0], 0x2026, buf, 2);
  274. if (ret)
  275. goto error;
  276. utmp = buf[0] << 8 | buf[1] << 0;
  277. utmp = ~utmp & 0x0fff;
  278. /* Scale value to 0x0000-0xffff */
  279. strength = utmp << 4 | utmp >> 8;
  280. c->strength.len = 1;
  281. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  282. c->strength.stat[0].uvalue = strength;
  283. } else {
  284. c->strength.len = 1;
  285. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  286. }
  287. /* CNR */
  288. if (*status & FE_HAS_VITERBI) {
  289. unsigned int cnr;
  290. ret = regmap_bulk_read(priv->regmap[0], 0x2028, buf, 2);
  291. if (ret)
  292. goto error;
  293. utmp = buf[0] << 8 | buf[1] << 0;
  294. utmp = utmp & 0x0fff;
  295. #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
  296. if (utmp)
  297. cnr = div_u64((u64)(intlog10(utmp)
  298. - CXD2820R_LOG10_8_24) * 10000,
  299. (1 << 24));
  300. else
  301. cnr = 0;
  302. c->cnr.len = 1;
  303. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  304. c->cnr.stat[0].svalue = cnr;
  305. } else {
  306. c->cnr.len = 1;
  307. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  308. }
  309. /* BER */
  310. if (*status & FE_HAS_SYNC) {
  311. unsigned int post_bit_error;
  312. ret = regmap_bulk_read(priv->regmap[0], 0x2039, buf, 4);
  313. if (ret)
  314. goto error;
  315. if ((buf[0] >> 4) & 0x01) {
  316. post_bit_error = buf[0] << 24 | buf[1] << 16 |
  317. buf[2] << 8 | buf[3] << 0;
  318. post_bit_error &= 0x0fffffff;
  319. } else {
  320. post_bit_error = 0;
  321. }
  322. priv->post_bit_error += post_bit_error;
  323. c->post_bit_error.len = 1;
  324. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  325. c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
  326. } else {
  327. c->post_bit_error.len = 1;
  328. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  329. }
  330. return ret;
  331. error:
  332. dev_dbg(&client->dev, "failed=%d\n", ret);
  333. return ret;
  334. }
  335. int cxd2820r_sleep_t2(struct dvb_frontend *fe)
  336. {
  337. struct cxd2820r_priv *priv = fe->demodulator_priv;
  338. struct i2c_client *client = priv->client[0];
  339. int ret;
  340. struct reg_val_mask tab[] = {
  341. { 0x000ff, 0x1f, 0xff },
  342. { 0x00085, 0x00, 0xff },
  343. { 0x00088, 0x01, 0xff },
  344. { 0x02069, 0x00, 0xff },
  345. { 0x00081, 0x00, 0xff },
  346. { 0x00080, 0x00, 0xff },
  347. };
  348. dev_dbg(&client->dev, "\n");
  349. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  350. if (ret)
  351. goto error;
  352. priv->delivery_system = SYS_UNDEFINED;
  353. return ret;
  354. error:
  355. dev_dbg(&client->dev, "failed=%d\n", ret);
  356. return ret;
  357. }
  358. int cxd2820r_get_tune_settings_t2(struct dvb_frontend *fe,
  359. struct dvb_frontend_tune_settings *s)
  360. {
  361. s->min_delay_ms = 1500;
  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. }