cxd2820r_t2.c 9.5 KB

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