cxd2820r_t2.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 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. { 0x1c, 0xb3, 0x33, 0x33, 0x33 }, /* 5 MHz */
  31. { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
  32. { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
  33. { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
  34. };
  35. struct reg_val_mask tab[] = {
  36. { 0x00080, 0x02, 0xff },
  37. { 0x00081, 0x20, 0xff },
  38. { 0x00085, 0x07, 0xff },
  39. { 0x00088, 0x01, 0xff },
  40. { 0x02069, 0x01, 0xff },
  41. { 0x0207f, 0x2a, 0xff },
  42. { 0x02082, 0x0a, 0xff },
  43. { 0x02083, 0x0a, 0xff },
  44. { 0x020cb, priv->cfg.if_agc_polarity << 6, 0x40 },
  45. { 0x02070, priv->cfg.ts_mode, 0xff },
  46. { 0x02071, !priv->cfg.ts_clock_inv << 6, 0x40 },
  47. { 0x020b5, priv->cfg.spec_inv << 4, 0x10 },
  48. { 0x02567, 0x07, 0x0f },
  49. { 0x02569, 0x03, 0x03 },
  50. { 0x02595, 0x1a, 0xff },
  51. { 0x02596, 0x50, 0xff },
  52. { 0x02a8c, 0x00, 0xff },
  53. { 0x02a8d, 0x34, 0xff },
  54. { 0x02a45, 0x06, 0x07 },
  55. { 0x03f10, 0x0d, 0xff },
  56. { 0x03f11, 0x02, 0xff },
  57. { 0x03f12, 0x01, 0xff },
  58. { 0x03f23, 0x2c, 0xff },
  59. { 0x03f51, 0x13, 0xff },
  60. { 0x03f52, 0x01, 0xff },
  61. { 0x03f53, 0x00, 0xff },
  62. { 0x027e6, 0x14, 0xff },
  63. { 0x02786, 0x02, 0x07 },
  64. { 0x02787, 0x40, 0xe0 },
  65. { 0x027ef, 0x10, 0x18 },
  66. };
  67. dev_dbg(&priv->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", __func__,
  68. c->frequency, c->bandwidth_hz);
  69. switch (c->bandwidth_hz) {
  70. case 5000000:
  71. bw_i = 0;
  72. bw_param = 3;
  73. break;
  74. case 6000000:
  75. bw_i = 1;
  76. bw_param = 2;
  77. break;
  78. case 7000000:
  79. bw_i = 2;
  80. bw_param = 1;
  81. break;
  82. case 8000000:
  83. bw_i = 3;
  84. bw_param = 0;
  85. break;
  86. default:
  87. return -EINVAL;
  88. }
  89. /* program tuner */
  90. if (fe->ops.tuner_ops.set_params)
  91. fe->ops.tuner_ops.set_params(fe);
  92. if (priv->delivery_system != SYS_DVBT2) {
  93. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  94. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
  95. tab[i].val, tab[i].mask);
  96. if (ret)
  97. goto error;
  98. }
  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_freq);
  104. if (ret)
  105. goto error;
  106. } else
  107. if_freq = 0;
  108. dev_dbg(&priv->i2c->dev, "%s: if_freq=%d\n", __func__, if_freq);
  109. num = if_freq / 1000; /* Hz => kHz */
  110. num *= 0x1000000;
  111. if_ctl = DIV_ROUND_CLOSEST_ULL(num, 41000);
  112. buf[0] = ((if_ctl >> 16) & 0xff);
  113. buf[1] = ((if_ctl >> 8) & 0xff);
  114. buf[2] = ((if_ctl >> 0) & 0xff);
  115. /* PLP filtering */
  116. if (c->stream_id > 255) {
  117. dev_dbg(&priv->i2c->dev, "%s: Disable PLP filtering\n", __func__);
  118. ret = cxd2820r_wr_reg(priv, 0x023ad , 0);
  119. if (ret)
  120. goto error;
  121. } else {
  122. dev_dbg(&priv->i2c->dev, "%s: Enable PLP filtering = %d\n", __func__,
  123. c->stream_id);
  124. ret = cxd2820r_wr_reg(priv, 0x023af , c->stream_id & 0xFF);
  125. if (ret)
  126. goto error;
  127. ret = cxd2820r_wr_reg(priv, 0x023ad , 1);
  128. if (ret)
  129. goto error;
  130. }
  131. ret = cxd2820r_wr_regs(priv, 0x020b6, buf, 3);
  132. if (ret)
  133. goto error;
  134. ret = cxd2820r_wr_regs(priv, 0x0209f, bw_params1[bw_i], 5);
  135. if (ret)
  136. goto error;
  137. ret = cxd2820r_wr_reg_mask(priv, 0x020d7, bw_param << 6, 0xc0);
  138. if (ret)
  139. goto error;
  140. ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
  141. if (ret)
  142. goto error;
  143. ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
  144. if (ret)
  145. goto error;
  146. return ret;
  147. error:
  148. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  149. return ret;
  150. }
  151. int cxd2820r_get_frontend_t2(struct dvb_frontend *fe)
  152. {
  153. struct cxd2820r_priv *priv = fe->demodulator_priv;
  154. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  155. int ret;
  156. u8 buf[2];
  157. ret = cxd2820r_rd_regs(priv, 0x0205c, buf, 2);
  158. if (ret)
  159. goto error;
  160. switch ((buf[0] >> 0) & 0x07) {
  161. case 0:
  162. c->transmission_mode = TRANSMISSION_MODE_2K;
  163. break;
  164. case 1:
  165. c->transmission_mode = TRANSMISSION_MODE_8K;
  166. break;
  167. case 2:
  168. c->transmission_mode = TRANSMISSION_MODE_4K;
  169. break;
  170. case 3:
  171. c->transmission_mode = TRANSMISSION_MODE_1K;
  172. break;
  173. case 4:
  174. c->transmission_mode = TRANSMISSION_MODE_16K;
  175. break;
  176. case 5:
  177. c->transmission_mode = TRANSMISSION_MODE_32K;
  178. break;
  179. }
  180. switch ((buf[1] >> 4) & 0x07) {
  181. case 0:
  182. c->guard_interval = GUARD_INTERVAL_1_32;
  183. break;
  184. case 1:
  185. c->guard_interval = GUARD_INTERVAL_1_16;
  186. break;
  187. case 2:
  188. c->guard_interval = GUARD_INTERVAL_1_8;
  189. break;
  190. case 3:
  191. c->guard_interval = GUARD_INTERVAL_1_4;
  192. break;
  193. case 4:
  194. c->guard_interval = GUARD_INTERVAL_1_128;
  195. break;
  196. case 5:
  197. c->guard_interval = GUARD_INTERVAL_19_128;
  198. break;
  199. case 6:
  200. c->guard_interval = GUARD_INTERVAL_19_256;
  201. break;
  202. }
  203. ret = cxd2820r_rd_regs(priv, 0x0225b, buf, 2);
  204. if (ret)
  205. goto error;
  206. switch ((buf[0] >> 0) & 0x07) {
  207. case 0:
  208. c->fec_inner = FEC_1_2;
  209. break;
  210. case 1:
  211. c->fec_inner = FEC_3_5;
  212. break;
  213. case 2:
  214. c->fec_inner = FEC_2_3;
  215. break;
  216. case 3:
  217. c->fec_inner = FEC_3_4;
  218. break;
  219. case 4:
  220. c->fec_inner = FEC_4_5;
  221. break;
  222. case 5:
  223. c->fec_inner = FEC_5_6;
  224. break;
  225. }
  226. switch ((buf[1] >> 0) & 0x07) {
  227. case 0:
  228. c->modulation = QPSK;
  229. break;
  230. case 1:
  231. c->modulation = QAM_16;
  232. break;
  233. case 2:
  234. c->modulation = QAM_64;
  235. break;
  236. case 3:
  237. c->modulation = QAM_256;
  238. break;
  239. }
  240. ret = cxd2820r_rd_reg(priv, 0x020b5, &buf[0]);
  241. if (ret)
  242. goto error;
  243. switch ((buf[0] >> 4) & 0x01) {
  244. case 0:
  245. c->inversion = INVERSION_OFF;
  246. break;
  247. case 1:
  248. c->inversion = INVERSION_ON;
  249. break;
  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_status_t2(struct dvb_frontend *fe, enum fe_status *status)
  257. {
  258. struct cxd2820r_priv *priv = fe->demodulator_priv;
  259. int ret;
  260. u8 buf[1];
  261. *status = 0;
  262. ret = cxd2820r_rd_reg(priv, 0x02010 , &buf[0]);
  263. if (ret)
  264. goto error;
  265. if ((buf[0] & 0x07) == 6) {
  266. if (((buf[0] >> 5) & 0x01) == 1) {
  267. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  268. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  269. } else {
  270. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  271. FE_HAS_VITERBI | FE_HAS_SYNC;
  272. }
  273. }
  274. dev_dbg(&priv->i2c->dev, "%s: lock=%02x\n", __func__, buf[0]);
  275. return ret;
  276. error:
  277. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  278. return ret;
  279. }
  280. int cxd2820r_read_ber_t2(struct dvb_frontend *fe, u32 *ber)
  281. {
  282. struct cxd2820r_priv *priv = fe->demodulator_priv;
  283. int ret;
  284. u8 buf[4];
  285. unsigned int errbits;
  286. *ber = 0;
  287. /* FIXME: correct calculation */
  288. ret = cxd2820r_rd_regs(priv, 0x02039, buf, sizeof(buf));
  289. if (ret)
  290. goto error;
  291. if ((buf[0] >> 4) & 0x01) {
  292. errbits = (buf[0] & 0x0f) << 24 | buf[1] << 16 |
  293. buf[2] << 8 | buf[3];
  294. if (errbits)
  295. *ber = errbits * 64 / 16588800;
  296. }
  297. return ret;
  298. error:
  299. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  300. return ret;
  301. }
  302. int cxd2820r_read_signal_strength_t2(struct dvb_frontend *fe,
  303. u16 *strength)
  304. {
  305. struct cxd2820r_priv *priv = fe->demodulator_priv;
  306. int ret;
  307. u8 buf[2];
  308. u16 tmp;
  309. ret = cxd2820r_rd_regs(priv, 0x02026, buf, sizeof(buf));
  310. if (ret)
  311. goto error;
  312. tmp = (buf[0] & 0x0f) << 8 | buf[1];
  313. tmp = ~tmp & 0x0fff;
  314. /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
  315. *strength = tmp * 0xffff / 0x0fff;
  316. return ret;
  317. error:
  318. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  319. return ret;
  320. }
  321. int cxd2820r_read_snr_t2(struct dvb_frontend *fe, u16 *snr)
  322. {
  323. struct cxd2820r_priv *priv = fe->demodulator_priv;
  324. int ret;
  325. u8 buf[2];
  326. u16 tmp;
  327. /* report SNR in dB * 10 */
  328. ret = cxd2820r_rd_regs(priv, 0x02028, buf, sizeof(buf));
  329. if (ret)
  330. goto error;
  331. tmp = (buf[0] & 0x0f) << 8 | buf[1];
  332. #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
  333. if (tmp)
  334. *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
  335. / 100);
  336. else
  337. *snr = 0;
  338. dev_dbg(&priv->i2c->dev, "%s: dBx10=%d val=%04x\n", __func__, *snr,
  339. tmp);
  340. return ret;
  341. error:
  342. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  343. return ret;
  344. }
  345. int cxd2820r_read_ucblocks_t2(struct dvb_frontend *fe, u32 *ucblocks)
  346. {
  347. *ucblocks = 0;
  348. /* no way to read ? */
  349. return 0;
  350. }
  351. int cxd2820r_sleep_t2(struct dvb_frontend *fe)
  352. {
  353. struct cxd2820r_priv *priv = fe->demodulator_priv;
  354. int ret, i;
  355. struct reg_val_mask tab[] = {
  356. { 0x000ff, 0x1f, 0xff },
  357. { 0x00085, 0x00, 0xff },
  358. { 0x00088, 0x01, 0xff },
  359. { 0x02069, 0x00, 0xff },
  360. { 0x00081, 0x00, 0xff },
  361. { 0x00080, 0x00, 0xff },
  362. };
  363. dev_dbg(&priv->i2c->dev, "%s\n", __func__);
  364. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  365. ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  366. tab[i].mask);
  367. if (ret)
  368. goto error;
  369. }
  370. priv->delivery_system = SYS_UNDEFINED;
  371. return ret;
  372. error:
  373. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  374. return ret;
  375. }
  376. int cxd2820r_get_tune_settings_t2(struct dvb_frontend *fe,
  377. struct dvb_frontend_tune_settings *s)
  378. {
  379. s->min_delay_ms = 1500;
  380. s->step_size = fe->ops.info.frequency_stepsize * 2;
  381. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  382. return 0;
  383. }