cxd2820r_t.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 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. { 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. u8 bw_params2[][2] = {
  36. { 0x1f, 0xdc }, /* 6 MHz */
  37. { 0x12, 0xf8 }, /* 7 MHz */
  38. { 0x01, 0xe0 }, /* 8 MHz */
  39. };
  40. struct reg_val_mask tab[] = {
  41. { 0x00080, 0x00, 0xff },
  42. { 0x00081, 0x03, 0xff },
  43. { 0x00085, 0x07, 0xff },
  44. { 0x00088, 0x01, 0xff },
  45. { 0x00070, priv->ts_mode, 0xff },
  46. { 0x00071, !priv->ts_clk_inv << 4, 0x10 },
  47. { 0x000cb, priv->if_agc_polarity << 6, 0x40 },
  48. { 0x000a5, 0x00, 0x01 },
  49. { 0x00082, 0x20, 0x60 },
  50. { 0x000c2, 0xc3, 0xff },
  51. { 0x0016a, 0x50, 0xff },
  52. { 0x00427, 0x41, 0xff },
  53. };
  54. dev_dbg(&client->dev,
  55. "delivery_system=%d modulation=%d frequency=%u bandwidth_hz=%u inversion=%d\n",
  56. c->delivery_system, c->modulation, c->frequency,
  57. c->bandwidth_hz, c->inversion);
  58. switch (c->bandwidth_hz) {
  59. case 6000000:
  60. bw_i = 0;
  61. bw_param = 2;
  62. break;
  63. case 7000000:
  64. bw_i = 1;
  65. bw_param = 1;
  66. break;
  67. case 8000000:
  68. bw_i = 2;
  69. bw_param = 0;
  70. break;
  71. default:
  72. return -EINVAL;
  73. }
  74. /* program tuner */
  75. if (fe->ops.tuner_ops.set_params)
  76. fe->ops.tuner_ops.set_params(fe);
  77. if (priv->delivery_system != SYS_DVBT) {
  78. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  79. if (ret)
  80. goto error;
  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_frequency);
  87. if (ret)
  88. goto error;
  89. dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
  90. } else {
  91. ret = -EINVAL;
  92. goto error;
  93. }
  94. utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, CXD2820R_CLK);
  95. buf[0] = (utmp >> 16) & 0xff;
  96. buf[1] = (utmp >> 8) & 0xff;
  97. buf[2] = (utmp >> 0) & 0xff;
  98. ret = regmap_bulk_write(priv->regmap[0], 0x00b6, buf, 3);
  99. if (ret)
  100. goto error;
  101. ret = regmap_bulk_write(priv->regmap[0], 0x009f, bw_params1[bw_i], 5);
  102. if (ret)
  103. goto error;
  104. ret = regmap_update_bits(priv->regmap[0], 0x00d7, 0xc0, bw_param << 6);
  105. if (ret)
  106. goto error;
  107. ret = regmap_bulk_write(priv->regmap[0], 0x00d9, bw_params2[bw_i], 2);
  108. if (ret)
  109. goto error;
  110. ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
  111. if (ret)
  112. goto error;
  113. ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
  114. if (ret)
  115. goto error;
  116. return ret;
  117. error:
  118. dev_dbg(&client->dev, "failed=%d\n", ret);
  119. return ret;
  120. }
  121. int cxd2820r_get_frontend_t(struct dvb_frontend *fe,
  122. struct dtv_frontend_properties *c)
  123. {
  124. struct cxd2820r_priv *priv = fe->demodulator_priv;
  125. struct i2c_client *client = priv->client[0];
  126. int ret;
  127. unsigned int utmp;
  128. u8 buf[2];
  129. dev_dbg(&client->dev, "\n");
  130. ret = regmap_bulk_read(priv->regmap[0], 0x002f, buf, sizeof(buf));
  131. if (ret)
  132. goto error;
  133. switch ((buf[0] >> 6) & 0x03) {
  134. case 0:
  135. c->modulation = QPSK;
  136. break;
  137. case 1:
  138. c->modulation = QAM_16;
  139. break;
  140. case 2:
  141. c->modulation = QAM_64;
  142. break;
  143. }
  144. switch ((buf[1] >> 1) & 0x03) {
  145. case 0:
  146. c->transmission_mode = TRANSMISSION_MODE_2K;
  147. break;
  148. case 1:
  149. c->transmission_mode = TRANSMISSION_MODE_8K;
  150. break;
  151. }
  152. switch ((buf[1] >> 3) & 0x03) {
  153. case 0:
  154. c->guard_interval = GUARD_INTERVAL_1_32;
  155. break;
  156. case 1:
  157. c->guard_interval = GUARD_INTERVAL_1_16;
  158. break;
  159. case 2:
  160. c->guard_interval = GUARD_INTERVAL_1_8;
  161. break;
  162. case 3:
  163. c->guard_interval = GUARD_INTERVAL_1_4;
  164. break;
  165. }
  166. switch ((buf[0] >> 3) & 0x07) {
  167. case 0:
  168. c->hierarchy = HIERARCHY_NONE;
  169. break;
  170. case 1:
  171. c->hierarchy = HIERARCHY_1;
  172. break;
  173. case 2:
  174. c->hierarchy = HIERARCHY_2;
  175. break;
  176. case 3:
  177. c->hierarchy = HIERARCHY_4;
  178. break;
  179. }
  180. switch ((buf[0] >> 0) & 0x07) {
  181. case 0:
  182. c->code_rate_HP = FEC_1_2;
  183. break;
  184. case 1:
  185. c->code_rate_HP = FEC_2_3;
  186. break;
  187. case 2:
  188. c->code_rate_HP = FEC_3_4;
  189. break;
  190. case 3:
  191. c->code_rate_HP = FEC_5_6;
  192. break;
  193. case 4:
  194. c->code_rate_HP = FEC_7_8;
  195. break;
  196. }
  197. switch ((buf[1] >> 5) & 0x07) {
  198. case 0:
  199. c->code_rate_LP = FEC_1_2;
  200. break;
  201. case 1:
  202. c->code_rate_LP = FEC_2_3;
  203. break;
  204. case 2:
  205. c->code_rate_LP = FEC_3_4;
  206. break;
  207. case 3:
  208. c->code_rate_LP = FEC_5_6;
  209. break;
  210. case 4:
  211. c->code_rate_LP = FEC_7_8;
  212. break;
  213. }
  214. ret = regmap_read(priv->regmap[0], 0x07c6, &utmp);
  215. if (ret)
  216. goto error;
  217. switch ((utmp >> 0) & 0x01) {
  218. case 0:
  219. c->inversion = INVERSION_OFF;
  220. break;
  221. case 1:
  222. c->inversion = INVERSION_ON;
  223. break;
  224. }
  225. return ret;
  226. error:
  227. dev_dbg(&client->dev, "failed=%d\n", ret);
  228. return ret;
  229. }
  230. int cxd2820r_read_status_t(struct dvb_frontend *fe, enum fe_status *status)
  231. {
  232. struct cxd2820r_priv *priv = fe->demodulator_priv;
  233. struct i2c_client *client = priv->client[0];
  234. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  235. int ret;
  236. unsigned int utmp, utmp1, utmp2;
  237. u8 buf[3];
  238. /* Lock detection */
  239. ret = regmap_bulk_read(priv->regmap[0], 0x0010, &buf[0], 1);
  240. if (ret)
  241. goto error;
  242. ret = regmap_bulk_read(priv->regmap[0], 0x0073, &buf[1], 1);
  243. if (ret)
  244. goto error;
  245. utmp1 = (buf[0] >> 0) & 0x07;
  246. utmp2 = (buf[1] >> 3) & 0x01;
  247. if (utmp1 == 6 && utmp2 == 1) {
  248. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  249. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  250. } else if (utmp1 == 6 || utmp2 == 1) {
  251. *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  252. FE_HAS_VITERBI | FE_HAS_SYNC;
  253. } else {
  254. *status = 0;
  255. }
  256. dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
  257. *status, 2, buf, utmp1, utmp2);
  258. /* Signal strength */
  259. if (*status & FE_HAS_SIGNAL) {
  260. unsigned int strength;
  261. ret = regmap_bulk_read(priv->regmap[0], 0x0026, buf, 2);
  262. if (ret)
  263. goto error;
  264. utmp = buf[0] << 8 | buf[1] << 0;
  265. utmp = ~utmp & 0x0fff;
  266. /* Scale value to 0x0000-0xffff */
  267. strength = utmp << 4 | utmp >> 8;
  268. c->strength.len = 1;
  269. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  270. c->strength.stat[0].uvalue = strength;
  271. } else {
  272. c->strength.len = 1;
  273. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  274. }
  275. /* CNR */
  276. if (*status & FE_HAS_VITERBI) {
  277. unsigned int cnr;
  278. ret = regmap_bulk_read(priv->regmap[0], 0x002c, buf, 2);
  279. if (ret)
  280. goto error;
  281. utmp = buf[0] << 8 | buf[1] << 0;
  282. if (utmp)
  283. cnr = div_u64((u64)(intlog10(utmp)
  284. - intlog10(32000 - utmp) + 55532585)
  285. * 10000, (1 << 24));
  286. else
  287. cnr = 0;
  288. c->cnr.len = 1;
  289. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  290. c->cnr.stat[0].svalue = cnr;
  291. } else {
  292. c->cnr.len = 1;
  293. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  294. }
  295. /* BER */
  296. if (*status & FE_HAS_SYNC) {
  297. unsigned int post_bit_error;
  298. bool start_ber;
  299. if (priv->ber_running) {
  300. ret = regmap_bulk_read(priv->regmap[0], 0x0076, buf, 3);
  301. if (ret)
  302. goto error;
  303. if ((buf[2] >> 7) & 0x01) {
  304. post_bit_error = buf[2] << 16 | buf[1] << 8 |
  305. buf[0] << 0;
  306. post_bit_error &= 0x0fffff;
  307. start_ber = true;
  308. } else {
  309. post_bit_error = 0;
  310. start_ber = false;
  311. }
  312. } else {
  313. post_bit_error = 0;
  314. start_ber = true;
  315. }
  316. if (start_ber) {
  317. ret = regmap_write(priv->regmap[0], 0x0079, 0x01);
  318. if (ret)
  319. goto error;
  320. priv->ber_running = true;
  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_init_t(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. dev_dbg(&client->dev, "\n");
  341. ret = regmap_write(priv->regmap[0], 0x0085, 0x07);
  342. if (ret)
  343. goto error;
  344. return ret;
  345. error:
  346. dev_dbg(&client->dev, "failed=%d\n", ret);
  347. return ret;
  348. }
  349. int cxd2820r_sleep_t(struct dvb_frontend *fe)
  350. {
  351. struct cxd2820r_priv *priv = fe->demodulator_priv;
  352. struct i2c_client *client = priv->client[0];
  353. int ret;
  354. struct reg_val_mask tab[] = {
  355. { 0x000ff, 0x1f, 0xff },
  356. { 0x00085, 0x00, 0xff },
  357. { 0x00088, 0x01, 0xff },
  358. { 0x00081, 0x00, 0xff },
  359. { 0x00080, 0x00, 0xff },
  360. };
  361. dev_dbg(&client->dev, "\n");
  362. priv->delivery_system = SYS_UNDEFINED;
  363. ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
  364. if (ret)
  365. goto error;
  366. return ret;
  367. error:
  368. dev_dbg(&client->dev, "failed=%d\n", ret);
  369. return ret;
  370. }
  371. int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
  372. struct dvb_frontend_tune_settings *s)
  373. {
  374. s->min_delay_ms = 500;
  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. }