rtl2830.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Realtek RTL2830 DVB-T demodulator driver
  3. *
  4. * Copyright (C) 2011 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. /*
  21. * Driver implements own I2C-adapter for tuner I2C access. That's since chip
  22. * have unusual I2C-gate control which closes gate automatically after each
  23. * I2C transfer. Using own I2C adapter we can workaround that.
  24. */
  25. #include "rtl2830_priv.h"
  26. int rtl2830_debug;
  27. module_param_named(debug, rtl2830_debug, int, 0644);
  28. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  29. /* write multiple registers */
  30. static int rtl2830_wr_regs(struct rtl2830_priv *priv, u16 reg, u8 *val, int len)
  31. {
  32. int ret;
  33. u8 buf[2+len];
  34. struct i2c_msg msg[1] = {
  35. {
  36. .addr = priv->cfg.i2c_addr,
  37. .flags = 0,
  38. .len = sizeof(buf),
  39. .buf = buf,
  40. }
  41. };
  42. buf[0] = (reg >> 8) & 0xff;
  43. buf[1] = (reg >> 0) & 0xff;
  44. memcpy(&buf[2], val, len);
  45. ret = i2c_transfer(priv->i2c, msg, 1);
  46. if (ret == 1) {
  47. ret = 0;
  48. } else {
  49. warn("i2c wr failed=%d reg=%04x len=%d", ret, reg, len);
  50. ret = -EREMOTEIO;
  51. }
  52. return ret;
  53. }
  54. /* read multiple registers */
  55. static int rtl2830_rd_regs(struct rtl2830_priv *priv, u16 reg, u8 *val, int len)
  56. {
  57. int ret;
  58. u8 buf[2];
  59. struct i2c_msg msg[2] = {
  60. {
  61. .addr = priv->cfg.i2c_addr,
  62. .flags = 0,
  63. .len = sizeof(buf),
  64. .buf = buf,
  65. }, {
  66. .addr = priv->cfg.i2c_addr,
  67. .flags = I2C_M_RD,
  68. .len = len,
  69. .buf = val,
  70. }
  71. };
  72. buf[0] = (reg >> 8) & 0xff;
  73. buf[1] = (reg >> 0) & 0xff;
  74. ret = i2c_transfer(priv->i2c, msg, 2);
  75. if (ret == 2) {
  76. ret = 0;
  77. } else {
  78. warn("i2c rd failed=%d reg=%04x len=%d", ret, reg, len);
  79. ret = -EREMOTEIO;
  80. }
  81. return ret;
  82. }
  83. #if 0 /* currently not used */
  84. /* write single register */
  85. static int rtl2830_wr_reg(struct rtl2830_priv *priv, u16 reg, u8 val)
  86. {
  87. return rtl2830_wr_regs(priv, reg, &val, 1);
  88. }
  89. #endif
  90. /* read single register */
  91. static int rtl2830_rd_reg(struct rtl2830_priv *priv, u16 reg, u8 *val)
  92. {
  93. return rtl2830_rd_regs(priv, reg, val, 1);
  94. }
  95. /* write single register with mask */
  96. int rtl2830_wr_reg_mask(struct rtl2830_priv *priv, u16 reg, u8 val, u8 mask)
  97. {
  98. int ret;
  99. u8 tmp;
  100. /* no need for read if whole reg is written */
  101. if (mask != 0xff) {
  102. ret = rtl2830_rd_regs(priv, reg, &tmp, 1);
  103. if (ret)
  104. return ret;
  105. val &= mask;
  106. tmp &= ~mask;
  107. val |= tmp;
  108. }
  109. return rtl2830_wr_regs(priv, reg, &val, 1);
  110. }
  111. /* read single register with mask */
  112. int rtl2830_rd_reg_mask(struct rtl2830_priv *priv, u16 reg, u8 *val, u8 mask)
  113. {
  114. int ret, i;
  115. u8 tmp;
  116. ret = rtl2830_rd_regs(priv, reg, &tmp, 1);
  117. if (ret)
  118. return ret;
  119. tmp &= mask;
  120. /* find position of the first bit */
  121. for (i = 0; i < 8; i++) {
  122. if ((mask >> i) & 0x01)
  123. break;
  124. }
  125. *val = tmp >> i;
  126. return 0;
  127. }
  128. static int rtl2830_init(struct dvb_frontend *fe)
  129. {
  130. struct rtl2830_priv *priv = fe->demodulator_priv;
  131. int ret, i;
  132. u64 num;
  133. u8 buf[3], tmp;
  134. u32 if_ctl;
  135. struct rtl2830_reg_val_mask tab[] = {
  136. { 0x00d, 0x01, 0x03 },
  137. { 0x00d, 0x10, 0x10 },
  138. { 0x104, 0x00, 0x1e },
  139. { 0x105, 0x80, 0x80 },
  140. { 0x110, 0x02, 0x03 },
  141. { 0x110, 0x08, 0x0c },
  142. { 0x17b, 0x00, 0x40 },
  143. { 0x17d, 0x05, 0x0f },
  144. { 0x17d, 0x50, 0xf0 },
  145. { 0x18c, 0x08, 0x0f },
  146. { 0x18d, 0x00, 0xc0 },
  147. { 0x188, 0x05, 0x0f },
  148. { 0x189, 0x00, 0xfc },
  149. { 0x2d5, 0x02, 0x02 },
  150. { 0x2f1, 0x02, 0x06 },
  151. { 0x2f1, 0x20, 0xf8 },
  152. { 0x16d, 0x00, 0x01 },
  153. { 0x1a6, 0x00, 0x80 },
  154. { 0x106, priv->cfg.vtop, 0x3f },
  155. { 0x107, priv->cfg.krf, 0x3f },
  156. { 0x112, 0x28, 0xff },
  157. { 0x103, priv->cfg.agc_targ_val, 0xff },
  158. { 0x00a, 0x02, 0x07 },
  159. { 0x140, 0x0c, 0x3c },
  160. { 0x140, 0x40, 0xc0 },
  161. { 0x15b, 0x05, 0x07 },
  162. { 0x15b, 0x28, 0x38 },
  163. { 0x15c, 0x05, 0x07 },
  164. { 0x15c, 0x28, 0x38 },
  165. { 0x115, priv->cfg.spec_inv, 0x01 },
  166. { 0x16f, 0x01, 0x07 },
  167. { 0x170, 0x18, 0x38 },
  168. { 0x172, 0x0f, 0x0f },
  169. { 0x173, 0x08, 0x38 },
  170. { 0x175, 0x01, 0x07 },
  171. { 0x176, 0x00, 0xc0 },
  172. };
  173. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  174. ret = rtl2830_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  175. tab[i].mask);
  176. if (ret)
  177. goto err;
  178. }
  179. ret = rtl2830_wr_regs(priv, 0x18f, "\x28\x00", 2);
  180. if (ret)
  181. goto err;
  182. ret = rtl2830_wr_regs(priv, 0x195,
  183. "\x04\x06\x0a\x12\x0a\x12\x1e\x28", 8);
  184. if (ret)
  185. goto err;
  186. num = priv->cfg.if_dvbt % priv->cfg.xtal;
  187. num *= 0x400000;
  188. num /= priv->cfg.xtal;
  189. num = -num;
  190. if_ctl = num & 0x3fffff;
  191. dbg("%s: if_ctl=%08x", __func__, if_ctl);
  192. ret = rtl2830_rd_reg_mask(priv, 0x119, &tmp, 0xc0); /* b[7:6] */
  193. if (ret)
  194. goto err;
  195. buf[0] = tmp << 6;
  196. buf[0] = (if_ctl >> 16) & 0x3f;
  197. buf[1] = (if_ctl >> 8) & 0xff;
  198. buf[2] = (if_ctl >> 0) & 0xff;
  199. ret = rtl2830_wr_regs(priv, 0x119, buf, 3);
  200. if (ret)
  201. goto err;
  202. /* TODO: spec init */
  203. /* soft reset */
  204. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x04, 0x04);
  205. if (ret)
  206. goto err;
  207. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x00, 0x04);
  208. if (ret)
  209. goto err;
  210. return ret;
  211. err:
  212. dbg("%s: failed=%d", __func__, ret);
  213. return ret;
  214. }
  215. int rtl2830_get_tune_settings(struct dvb_frontend *fe,
  216. struct dvb_frontend_tune_settings *s)
  217. {
  218. s->min_delay_ms = 500;
  219. s->step_size = fe->ops.info.frequency_stepsize * 2;
  220. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  221. return 0;
  222. }
  223. static int rtl2830_set_frontend(struct dvb_frontend *fe)
  224. {
  225. struct rtl2830_priv *priv = fe->demodulator_priv;
  226. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  227. int ret, i;
  228. static u8 bw_params1[3][34] = {
  229. {
  230. 0x1f, 0xf0, 0x1f, 0xf0, 0x1f, 0xfa, 0x00, 0x17, 0x00, 0x41,
  231. 0x00, 0x64, 0x00, 0x67, 0x00, 0x38, 0x1f, 0xde, 0x1f, 0x7a,
  232. 0x1f, 0x47, 0x1f, 0x7c, 0x00, 0x30, 0x01, 0x4b, 0x02, 0x82,
  233. 0x03, 0x73, 0x03, 0xcf, /* 6 MHz */
  234. }, {
  235. 0x1f, 0xfa, 0x1f, 0xda, 0x1f, 0xc1, 0x1f, 0xb3, 0x1f, 0xca,
  236. 0x00, 0x07, 0x00, 0x4d, 0x00, 0x6d, 0x00, 0x40, 0x1f, 0xca,
  237. 0x1f, 0x4d, 0x1f, 0x2a, 0x1f, 0xb2, 0x00, 0xec, 0x02, 0x7e,
  238. 0x03, 0xd0, 0x04, 0x53, /* 7 MHz */
  239. }, {
  240. 0x00, 0x10, 0x00, 0x0e, 0x1f, 0xf7, 0x1f, 0xc9, 0x1f, 0xa0,
  241. 0x1f, 0xa6, 0x1f, 0xec, 0x00, 0x4e, 0x00, 0x7d, 0x00, 0x3a,
  242. 0x1f, 0x98, 0x1f, 0x10, 0x1f, 0x40, 0x00, 0x75, 0x02, 0x5f,
  243. 0x04, 0x24, 0x04, 0xdb, /* 8 MHz */
  244. },
  245. };
  246. static u8 bw_params2[3][6] = {
  247. {0xc3, 0x0c, 0x44, 0x33, 0x33, 0x30,}, /* 6 MHz */
  248. {0xb8, 0xe3, 0x93, 0x99, 0x99, 0x98,}, /* 7 MHz */
  249. {0xae, 0xba, 0xf3, 0x26, 0x66, 0x64,}, /* 8 MHz */
  250. };
  251. dbg("%s: frequency=%d bandwidth_hz=%d inversion=%d", __func__,
  252. c->frequency, c->bandwidth_hz, c->inversion);
  253. /* program tuner */
  254. if (fe->ops.tuner_ops.set_params)
  255. fe->ops.tuner_ops.set_params(fe);
  256. switch (c->bandwidth_hz) {
  257. case 6000000:
  258. i = 0;
  259. break;
  260. case 7000000:
  261. i = 1;
  262. break;
  263. case 8000000:
  264. i = 2;
  265. break;
  266. default:
  267. dbg("invalid bandwidth");
  268. return -EINVAL;
  269. }
  270. ret = rtl2830_wr_reg_mask(priv, 0x008, i << 1, 0x06);
  271. if (ret)
  272. goto err;
  273. /* 1/2 split I2C write */
  274. ret = rtl2830_wr_regs(priv, 0x11c, &bw_params1[i][0], 17);
  275. if (ret)
  276. goto err;
  277. /* 2/2 split I2C write */
  278. ret = rtl2830_wr_regs(priv, 0x12d, &bw_params1[i][17], 17);
  279. if (ret)
  280. goto err;
  281. ret = rtl2830_wr_regs(priv, 0x19d, bw_params2[i], 6);
  282. if (ret)
  283. goto err;
  284. return ret;
  285. err:
  286. dbg("%s: failed=%d", __func__, ret);
  287. return ret;
  288. }
  289. static int rtl2830_read_status(struct dvb_frontend *fe, fe_status_t *status)
  290. {
  291. struct rtl2830_priv *priv = fe->demodulator_priv;
  292. int ret;
  293. u8 tmp;
  294. *status = 0;
  295. ret = rtl2830_rd_reg_mask(priv, 0x351, &tmp, 0x78); /* [6:3] */
  296. if (ret)
  297. goto err;
  298. if (tmp == 11) {
  299. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  300. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  301. } else if (tmp == 10) {
  302. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  303. FE_HAS_VITERBI;
  304. }
  305. return ret;
  306. err:
  307. dbg("%s: failed=%d", __func__, ret);
  308. return ret;
  309. }
  310. static int rtl2830_read_snr(struct dvb_frontend *fe, u16 *snr)
  311. {
  312. *snr = 0;
  313. return 0;
  314. }
  315. static int rtl2830_read_ber(struct dvb_frontend *fe, u32 *ber)
  316. {
  317. *ber = 0;
  318. return 0;
  319. }
  320. static int rtl2830_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  321. {
  322. *ucblocks = 0;
  323. return 0;
  324. }
  325. static int rtl2830_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  326. {
  327. *strength = 0;
  328. return 0;
  329. }
  330. static struct dvb_frontend_ops rtl2830_ops;
  331. static u32 rtl2830_tuner_i2c_func(struct i2c_adapter *adapter)
  332. {
  333. return I2C_FUNC_I2C;
  334. }
  335. static int rtl2830_tuner_i2c_xfer(struct i2c_adapter *i2c_adap,
  336. struct i2c_msg msg[], int num)
  337. {
  338. struct rtl2830_priv *priv = i2c_get_adapdata(i2c_adap);
  339. int ret;
  340. /* open i2c-gate */
  341. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x08, 0x08);
  342. if (ret)
  343. goto err;
  344. ret = i2c_transfer(priv->i2c, msg, num);
  345. if (ret < 0)
  346. warn("tuner i2c failed=%d", ret);
  347. return ret;
  348. err:
  349. dbg("%s: failed=%d", __func__, ret);
  350. return ret;
  351. }
  352. static struct i2c_algorithm rtl2830_tuner_i2c_algo = {
  353. .master_xfer = rtl2830_tuner_i2c_xfer,
  354. .functionality = rtl2830_tuner_i2c_func,
  355. };
  356. struct i2c_adapter *rtl2830_get_tuner_i2c_adapter(struct dvb_frontend *fe)
  357. {
  358. struct rtl2830_priv *priv = fe->demodulator_priv;
  359. return &priv->tuner_i2c_adapter;
  360. }
  361. EXPORT_SYMBOL(rtl2830_get_tuner_i2c_adapter);
  362. static void rtl2830_release(struct dvb_frontend *fe)
  363. {
  364. struct rtl2830_priv *priv = fe->demodulator_priv;
  365. i2c_del_adapter(&priv->tuner_i2c_adapter);
  366. kfree(priv);
  367. }
  368. struct dvb_frontend *rtl2830_attach(const struct rtl2830_config *cfg,
  369. struct i2c_adapter *i2c)
  370. {
  371. struct rtl2830_priv *priv = NULL;
  372. int ret = 0;
  373. u8 tmp;
  374. /* allocate memory for the internal state */
  375. priv = kzalloc(sizeof(struct rtl2830_priv), GFP_KERNEL);
  376. if (priv == NULL)
  377. goto err;
  378. /* setup the priv */
  379. priv->i2c = i2c;
  380. memcpy(&priv->cfg, cfg, sizeof(struct rtl2830_config));
  381. /* check if the demod is there */
  382. ret = rtl2830_rd_reg(priv, 0x000, &tmp);
  383. if (ret)
  384. goto err;
  385. /* create dvb_frontend */
  386. memcpy(&priv->fe.ops, &rtl2830_ops, sizeof(struct dvb_frontend_ops));
  387. priv->fe.demodulator_priv = priv;
  388. /* create tuner i2c adapter */
  389. strlcpy(priv->tuner_i2c_adapter.name, "RTL2830 tuner I2C adapter",
  390. sizeof(priv->tuner_i2c_adapter.name));
  391. priv->tuner_i2c_adapter.algo = &rtl2830_tuner_i2c_algo;
  392. priv->tuner_i2c_adapter.algo_data = NULL;
  393. i2c_set_adapdata(&priv->tuner_i2c_adapter, priv);
  394. if (i2c_add_adapter(&priv->tuner_i2c_adapter) < 0) {
  395. err("tuner I2C bus could not be initialized");
  396. goto err;
  397. }
  398. return &priv->fe;
  399. err:
  400. dbg("%s: failed=%d", __func__, ret);
  401. kfree(priv);
  402. return NULL;
  403. }
  404. EXPORT_SYMBOL(rtl2830_attach);
  405. static struct dvb_frontend_ops rtl2830_ops = {
  406. .delsys = { SYS_DVBT },
  407. .info = {
  408. .name = "Realtek RTL2830 (DVB-T)",
  409. .caps = FE_CAN_FEC_1_2 |
  410. FE_CAN_FEC_2_3 |
  411. FE_CAN_FEC_3_4 |
  412. FE_CAN_FEC_5_6 |
  413. FE_CAN_FEC_7_8 |
  414. FE_CAN_FEC_AUTO |
  415. FE_CAN_QPSK |
  416. FE_CAN_QAM_16 |
  417. FE_CAN_QAM_64 |
  418. FE_CAN_QAM_AUTO |
  419. FE_CAN_TRANSMISSION_MODE_AUTO |
  420. FE_CAN_GUARD_INTERVAL_AUTO |
  421. FE_CAN_HIERARCHY_AUTO |
  422. FE_CAN_RECOVER |
  423. FE_CAN_MUTE_TS
  424. },
  425. .release = rtl2830_release,
  426. .init = rtl2830_init,
  427. .get_tune_settings = rtl2830_get_tune_settings,
  428. .set_frontend = rtl2830_set_frontend,
  429. .read_status = rtl2830_read_status,
  430. .read_snr = rtl2830_read_snr,
  431. .read_ber = rtl2830_read_ber,
  432. .read_ucblocks = rtl2830_read_ucblocks,
  433. .read_signal_strength = rtl2830_read_signal_strength,
  434. };
  435. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  436. MODULE_DESCRIPTION("Realtek RTL2830 DVB-T demodulator driver");
  437. MODULE_LICENSE("GPL");