cxd2820r_t2.c 9.8 KB

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