cxd2820r_t.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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_t(struct dvb_frontend *fe)
  22. {
  23. struct cxd2820r_priv *priv = fe->demodulator_priv;
  24. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  25. int ret, i, bw_i;
  26. u32 if_freq, if_ctl;
  27. u64 num;
  28. u8 buf[3], bw_param;
  29. u8 bw_params1[][5] = {
  30. { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
  31. { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
  32. { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
  33. };
  34. u8 bw_params2[][2] = {
  35. { 0x1f, 0xdc }, /* 6 MHz */
  36. { 0x12, 0xf8 }, /* 7 MHz */
  37. { 0x01, 0xe0 }, /* 8 MHz */
  38. };
  39. struct reg_val_mask tab[] = {
  40. { 0x00080, 0x00, 0xff },
  41. { 0x00081, 0x03, 0xff },
  42. { 0x00085, 0x07, 0xff },
  43. { 0x00088, 0x01, 0xff },
  44. { 0x00070, priv->cfg.ts_mode, 0xff },
  45. { 0x00071, !priv->cfg.ts_clock_inv << 4, 0x10 },
  46. { 0x000cb, priv->cfg.if_agc_polarity << 6, 0x40 },
  47. { 0x000a5, 0x00, 0x01 },
  48. { 0x00082, 0x20, 0x60 },
  49. { 0x000c2, 0xc3, 0xff },
  50. { 0x0016a, 0x50, 0xff },
  51. { 0x00427, 0x41, 0xff },
  52. };
  53. dev_dbg(&priv->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", __func__,
  54. c->frequency, c->bandwidth_hz);
  55. switch (c->bandwidth_hz) {
  56. case 6000000:
  57. bw_i = 0;
  58. bw_param = 2;
  59. break;
  60. case 7000000:
  61. bw_i = 1;
  62. bw_param = 1;
  63. break;
  64. case 8000000:
  65. bw_i = 2;
  66. bw_param = 0;
  67. break;
  68. default:
  69. return -EINVAL;
  70. }
  71. /* program tuner */
  72. if (fe->ops.tuner_ops.set_params)
  73. fe->ops.tuner_ops.set_params(fe);
  74. if (priv->delivery_system != SYS_DVBT) {
  75. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  76. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
  77. tab[i].val, tab[i].mask);
  78. if (ret)
  79. goto error;
  80. }
  81. }
  82. priv->delivery_system = SYS_DVBT;
  83. priv->ber_running = false; /* tune stops BER counter */
  84. /* program IF frequency */
  85. if (fe->ops.tuner_ops.get_if_frequency) {
  86. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
  87. if (ret)
  88. goto error;
  89. } else
  90. if_freq = 0;
  91. dev_dbg(&priv->i2c->dev, "%s: if_freq=%d\n", __func__, if_freq);
  92. num = if_freq / 1000; /* Hz => kHz */
  93. num *= 0x1000000;
  94. if_ctl = DIV_ROUND_CLOSEST_ULL(num, 41000);
  95. buf[0] = ((if_ctl >> 16) & 0xff);
  96. buf[1] = ((if_ctl >> 8) & 0xff);
  97. buf[2] = ((if_ctl >> 0) & 0xff);
  98. ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3);
  99. if (ret)
  100. goto error;
  101. ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[bw_i], 5);
  102. if (ret)
  103. goto error;
  104. ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0);
  105. if (ret)
  106. goto error;
  107. ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[bw_i], 2);
  108. if (ret)
  109. goto error;
  110. ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
  111. if (ret)
  112. goto error;
  113. ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
  114. if (ret)
  115. goto error;
  116. return ret;
  117. error:
  118. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  119. return ret;
  120. }
  121. int cxd2820r_get_frontend_t(struct dvb_frontend *fe)
  122. {
  123. struct cxd2820r_priv *priv = fe->demodulator_priv;
  124. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  125. int ret;
  126. u8 buf[2];
  127. ret = cxd2820r_rd_regs(priv, 0x0002f, buf, sizeof(buf));
  128. if (ret)
  129. goto error;
  130. switch ((buf[0] >> 6) & 0x03) {
  131. case 0:
  132. c->modulation = QPSK;
  133. break;
  134. case 1:
  135. c->modulation = QAM_16;
  136. break;
  137. case 2:
  138. c->modulation = QAM_64;
  139. break;
  140. }
  141. switch ((buf[1] >> 1) & 0x03) {
  142. case 0:
  143. c->transmission_mode = TRANSMISSION_MODE_2K;
  144. break;
  145. case 1:
  146. c->transmission_mode = TRANSMISSION_MODE_8K;
  147. break;
  148. }
  149. switch ((buf[1] >> 3) & 0x03) {
  150. case 0:
  151. c->guard_interval = GUARD_INTERVAL_1_32;
  152. break;
  153. case 1:
  154. c->guard_interval = GUARD_INTERVAL_1_16;
  155. break;
  156. case 2:
  157. c->guard_interval = GUARD_INTERVAL_1_8;
  158. break;
  159. case 3:
  160. c->guard_interval = GUARD_INTERVAL_1_4;
  161. break;
  162. }
  163. switch ((buf[0] >> 3) & 0x07) {
  164. case 0:
  165. c->hierarchy = HIERARCHY_NONE;
  166. break;
  167. case 1:
  168. c->hierarchy = HIERARCHY_1;
  169. break;
  170. case 2:
  171. c->hierarchy = HIERARCHY_2;
  172. break;
  173. case 3:
  174. c->hierarchy = HIERARCHY_4;
  175. break;
  176. }
  177. switch ((buf[0] >> 0) & 0x07) {
  178. case 0:
  179. c->code_rate_HP = FEC_1_2;
  180. break;
  181. case 1:
  182. c->code_rate_HP = FEC_2_3;
  183. break;
  184. case 2:
  185. c->code_rate_HP = FEC_3_4;
  186. break;
  187. case 3:
  188. c->code_rate_HP = FEC_5_6;
  189. break;
  190. case 4:
  191. c->code_rate_HP = FEC_7_8;
  192. break;
  193. }
  194. switch ((buf[1] >> 5) & 0x07) {
  195. case 0:
  196. c->code_rate_LP = FEC_1_2;
  197. break;
  198. case 1:
  199. c->code_rate_LP = FEC_2_3;
  200. break;
  201. case 2:
  202. c->code_rate_LP = FEC_3_4;
  203. break;
  204. case 3:
  205. c->code_rate_LP = FEC_5_6;
  206. break;
  207. case 4:
  208. c->code_rate_LP = FEC_7_8;
  209. break;
  210. }
  211. ret = cxd2820r_rd_reg(priv, 0x007c6, &buf[0]);
  212. if (ret)
  213. goto error;
  214. switch ((buf[0] >> 0) & 0x01) {
  215. case 0:
  216. c->inversion = INVERSION_OFF;
  217. break;
  218. case 1:
  219. c->inversion = INVERSION_ON;
  220. break;
  221. }
  222. return ret;
  223. error:
  224. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  225. return ret;
  226. }
  227. int cxd2820r_read_ber_t(struct dvb_frontend *fe, u32 *ber)
  228. {
  229. struct cxd2820r_priv *priv = fe->demodulator_priv;
  230. int ret;
  231. u8 buf[3], start_ber = 0;
  232. *ber = 0;
  233. if (priv->ber_running) {
  234. ret = cxd2820r_rd_regs(priv, 0x00076, buf, sizeof(buf));
  235. if (ret)
  236. goto error;
  237. if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
  238. *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
  239. start_ber = 1;
  240. }
  241. } else {
  242. priv->ber_running = true;
  243. start_ber = 1;
  244. }
  245. if (start_ber) {
  246. /* (re)start BER */
  247. ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
  248. if (ret)
  249. goto error;
  250. }
  251. return ret;
  252. error:
  253. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  254. return ret;
  255. }
  256. int cxd2820r_read_signal_strength_t(struct dvb_frontend *fe,
  257. u16 *strength)
  258. {
  259. struct cxd2820r_priv *priv = fe->demodulator_priv;
  260. int ret;
  261. u8 buf[2];
  262. u16 tmp;
  263. ret = cxd2820r_rd_regs(priv, 0x00026, buf, sizeof(buf));
  264. if (ret)
  265. goto error;
  266. tmp = (buf[0] & 0x0f) << 8 | buf[1];
  267. tmp = ~tmp & 0x0fff;
  268. /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
  269. *strength = tmp * 0xffff / 0x0fff;
  270. return ret;
  271. error:
  272. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  273. return ret;
  274. }
  275. int cxd2820r_read_snr_t(struct dvb_frontend *fe, u16 *snr)
  276. {
  277. struct cxd2820r_priv *priv = fe->demodulator_priv;
  278. int ret;
  279. u8 buf[2];
  280. u16 tmp;
  281. /* report SNR in dB * 10 */
  282. ret = cxd2820r_rd_regs(priv, 0x00028, buf, sizeof(buf));
  283. if (ret)
  284. goto error;
  285. tmp = (buf[0] & 0x1f) << 8 | buf[1];
  286. #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
  287. if (tmp)
  288. *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
  289. / 100);
  290. else
  291. *snr = 0;
  292. dev_dbg(&priv->i2c->dev, "%s: dBx10=%d val=%04x\n", __func__, *snr,
  293. tmp);
  294. return ret;
  295. error:
  296. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  297. return ret;
  298. }
  299. int cxd2820r_read_ucblocks_t(struct dvb_frontend *fe, u32 *ucblocks)
  300. {
  301. *ucblocks = 0;
  302. /* no way to read ? */
  303. return 0;
  304. }
  305. int cxd2820r_read_status_t(struct dvb_frontend *fe, enum fe_status *status)
  306. {
  307. struct cxd2820r_priv *priv = fe->demodulator_priv;
  308. int ret;
  309. u8 buf[4];
  310. *status = 0;
  311. ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]);
  312. if (ret)
  313. goto error;
  314. if ((buf[0] & 0x07) == 6) {
  315. ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]);
  316. if (ret)
  317. goto error;
  318. if (((buf[1] >> 3) & 0x01) == 1) {
  319. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  320. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  321. } else {
  322. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  323. FE_HAS_VITERBI | FE_HAS_SYNC;
  324. }
  325. } else {
  326. ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]);
  327. if (ret)
  328. goto error;
  329. if ((buf[2] & 0x0f) >= 4) {
  330. ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]);
  331. if (ret)
  332. goto error;
  333. if (((buf[3] >> 4) & 0x01) == 1)
  334. *status |= FE_HAS_SIGNAL;
  335. }
  336. }
  337. dev_dbg(&priv->i2c->dev, "%s: lock=%*ph\n", __func__, 4, buf);
  338. return ret;
  339. error:
  340. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  341. return ret;
  342. }
  343. int cxd2820r_init_t(struct dvb_frontend *fe)
  344. {
  345. struct cxd2820r_priv *priv = fe->demodulator_priv;
  346. int ret;
  347. ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
  348. if (ret)
  349. goto error;
  350. return ret;
  351. error:
  352. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  353. return ret;
  354. }
  355. int cxd2820r_sleep_t(struct dvb_frontend *fe)
  356. {
  357. struct cxd2820r_priv *priv = fe->demodulator_priv;
  358. int ret, i;
  359. struct reg_val_mask tab[] = {
  360. { 0x000ff, 0x1f, 0xff },
  361. { 0x00085, 0x00, 0xff },
  362. { 0x00088, 0x01, 0xff },
  363. { 0x00081, 0x00, 0xff },
  364. { 0x00080, 0x00, 0xff },
  365. };
  366. dev_dbg(&priv->i2c->dev, "%s\n", __func__);
  367. priv->delivery_system = SYS_UNDEFINED;
  368. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  369. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  370. tab[i].mask);
  371. if (ret)
  372. goto error;
  373. }
  374. return ret;
  375. error:
  376. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  377. return ret;
  378. }
  379. int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
  380. struct dvb_frontend_tune_settings *s)
  381. {
  382. s->min_delay_ms = 500;
  383. s->step_size = fe->ops.info.frequency_stepsize * 2;
  384. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  385. return 0;
  386. }