rtl2830.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. /* Max transfer size done by I2C transfer functions */
  27. #define MAX_XFER_SIZE 64
  28. /* write multiple hardware registers */
  29. static int rtl2830_wr(struct rtl2830_priv *priv, u8 reg, const u8 *val, int len)
  30. {
  31. int ret;
  32. u8 buf[MAX_XFER_SIZE];
  33. struct i2c_msg msg[1] = {
  34. {
  35. .addr = priv->cfg.i2c_addr,
  36. .flags = 0,
  37. .len = 1 + len,
  38. .buf = buf,
  39. }
  40. };
  41. if (1 + len > sizeof(buf)) {
  42. dev_warn(&priv->i2c->dev,
  43. "%s: i2c wr reg=%04x: len=%d is too big!\n",
  44. KBUILD_MODNAME, reg, len);
  45. return -EINVAL;
  46. }
  47. buf[0] = reg;
  48. memcpy(&buf[1], val, len);
  49. ret = i2c_transfer(priv->i2c, msg, 1);
  50. if (ret == 1) {
  51. ret = 0;
  52. } else {
  53. dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \
  54. "len=%d\n", KBUILD_MODNAME, ret, reg, len);
  55. ret = -EREMOTEIO;
  56. }
  57. return ret;
  58. }
  59. /* read multiple hardware registers */
  60. static int rtl2830_rd(struct rtl2830_priv *priv, u8 reg, u8 *val, int len)
  61. {
  62. int ret;
  63. struct i2c_msg msg[2] = {
  64. {
  65. .addr = priv->cfg.i2c_addr,
  66. .flags = 0,
  67. .len = 1,
  68. .buf = &reg,
  69. }, {
  70. .addr = priv->cfg.i2c_addr,
  71. .flags = I2C_M_RD,
  72. .len = len,
  73. .buf = val,
  74. }
  75. };
  76. ret = i2c_transfer(priv->i2c, msg, 2);
  77. if (ret == 2) {
  78. ret = 0;
  79. } else {
  80. dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \
  81. "len=%d\n", KBUILD_MODNAME, ret, reg, len);
  82. ret = -EREMOTEIO;
  83. }
  84. return ret;
  85. }
  86. /* write multiple registers */
  87. static int rtl2830_wr_regs(struct rtl2830_priv *priv, u16 reg, const u8 *val,
  88. int len)
  89. {
  90. int ret;
  91. u8 reg2 = (reg >> 0) & 0xff;
  92. u8 page = (reg >> 8) & 0xff;
  93. /* switch bank if needed */
  94. if (page != priv->page) {
  95. ret = rtl2830_wr(priv, 0x00, &page, 1);
  96. if (ret)
  97. return ret;
  98. priv->page = page;
  99. }
  100. return rtl2830_wr(priv, reg2, val, len);
  101. }
  102. /* read multiple registers */
  103. static int rtl2830_rd_regs(struct rtl2830_priv *priv, u16 reg, u8 *val, int len)
  104. {
  105. int ret;
  106. u8 reg2 = (reg >> 0) & 0xff;
  107. u8 page = (reg >> 8) & 0xff;
  108. /* switch bank if needed */
  109. if (page != priv->page) {
  110. ret = rtl2830_wr(priv, 0x00, &page, 1);
  111. if (ret)
  112. return ret;
  113. priv->page = page;
  114. }
  115. return rtl2830_rd(priv, reg2, val, len);
  116. }
  117. /* read single register */
  118. static int rtl2830_rd_reg(struct rtl2830_priv *priv, u16 reg, u8 *val)
  119. {
  120. return rtl2830_rd_regs(priv, reg, val, 1);
  121. }
  122. /* write single register with mask */
  123. static int rtl2830_wr_reg_mask(struct rtl2830_priv *priv, u16 reg, u8 val, u8 mask)
  124. {
  125. int ret;
  126. u8 tmp;
  127. /* no need for read if whole reg is written */
  128. if (mask != 0xff) {
  129. ret = rtl2830_rd_regs(priv, reg, &tmp, 1);
  130. if (ret)
  131. return ret;
  132. val &= mask;
  133. tmp &= ~mask;
  134. val |= tmp;
  135. }
  136. return rtl2830_wr_regs(priv, reg, &val, 1);
  137. }
  138. /* read single register with mask */
  139. static int rtl2830_rd_reg_mask(struct rtl2830_priv *priv, u16 reg, u8 *val, u8 mask)
  140. {
  141. int ret, i;
  142. u8 tmp;
  143. ret = rtl2830_rd_regs(priv, reg, &tmp, 1);
  144. if (ret)
  145. return ret;
  146. tmp &= mask;
  147. /* find position of the first bit */
  148. for (i = 0; i < 8; i++) {
  149. if ((mask >> i) & 0x01)
  150. break;
  151. }
  152. *val = tmp >> i;
  153. return 0;
  154. }
  155. static int rtl2830_init(struct dvb_frontend *fe)
  156. {
  157. struct rtl2830_priv *priv = fe->demodulator_priv;
  158. int ret, i;
  159. struct rtl2830_reg_val_mask tab[] = {
  160. { 0x00d, 0x01, 0x03 },
  161. { 0x00d, 0x10, 0x10 },
  162. { 0x104, 0x00, 0x1e },
  163. { 0x105, 0x80, 0x80 },
  164. { 0x110, 0x02, 0x03 },
  165. { 0x110, 0x08, 0x0c },
  166. { 0x17b, 0x00, 0x40 },
  167. { 0x17d, 0x05, 0x0f },
  168. { 0x17d, 0x50, 0xf0 },
  169. { 0x18c, 0x08, 0x0f },
  170. { 0x18d, 0x00, 0xc0 },
  171. { 0x188, 0x05, 0x0f },
  172. { 0x189, 0x00, 0xfc },
  173. { 0x2d5, 0x02, 0x02 },
  174. { 0x2f1, 0x02, 0x06 },
  175. { 0x2f1, 0x20, 0xf8 },
  176. { 0x16d, 0x00, 0x01 },
  177. { 0x1a6, 0x00, 0x80 },
  178. { 0x106, priv->cfg.vtop, 0x3f },
  179. { 0x107, priv->cfg.krf, 0x3f },
  180. { 0x112, 0x28, 0xff },
  181. { 0x103, priv->cfg.agc_targ_val, 0xff },
  182. { 0x00a, 0x02, 0x07 },
  183. { 0x140, 0x0c, 0x3c },
  184. { 0x140, 0x40, 0xc0 },
  185. { 0x15b, 0x05, 0x07 },
  186. { 0x15b, 0x28, 0x38 },
  187. { 0x15c, 0x05, 0x07 },
  188. { 0x15c, 0x28, 0x38 },
  189. { 0x115, priv->cfg.spec_inv, 0x01 },
  190. { 0x16f, 0x01, 0x07 },
  191. { 0x170, 0x18, 0x38 },
  192. { 0x172, 0x0f, 0x0f },
  193. { 0x173, 0x08, 0x38 },
  194. { 0x175, 0x01, 0x07 },
  195. { 0x176, 0x00, 0xc0 },
  196. };
  197. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  198. ret = rtl2830_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  199. tab[i].mask);
  200. if (ret)
  201. goto err;
  202. }
  203. ret = rtl2830_wr_regs(priv, 0x18f, "\x28\x00", 2);
  204. if (ret)
  205. goto err;
  206. ret = rtl2830_wr_regs(priv, 0x195,
  207. "\x04\x06\x0a\x12\x0a\x12\x1e\x28", 8);
  208. if (ret)
  209. goto err;
  210. /* TODO: spec init */
  211. /* soft reset */
  212. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x04, 0x04);
  213. if (ret)
  214. goto err;
  215. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x00, 0x04);
  216. if (ret)
  217. goto err;
  218. priv->sleeping = false;
  219. return ret;
  220. err:
  221. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  222. return ret;
  223. }
  224. static int rtl2830_sleep(struct dvb_frontend *fe)
  225. {
  226. struct rtl2830_priv *priv = fe->demodulator_priv;
  227. priv->sleeping = true;
  228. return 0;
  229. }
  230. static int rtl2830_get_tune_settings(struct dvb_frontend *fe,
  231. struct dvb_frontend_tune_settings *s)
  232. {
  233. s->min_delay_ms = 500;
  234. s->step_size = fe->ops.info.frequency_stepsize * 2;
  235. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  236. return 0;
  237. }
  238. static int rtl2830_set_frontend(struct dvb_frontend *fe)
  239. {
  240. struct rtl2830_priv *priv = fe->demodulator_priv;
  241. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  242. int ret, i;
  243. u64 num;
  244. u8 buf[3], tmp;
  245. u32 if_ctl, if_frequency;
  246. static const u8 bw_params1[3][34] = {
  247. {
  248. 0x1f, 0xf0, 0x1f, 0xf0, 0x1f, 0xfa, 0x00, 0x17, 0x00, 0x41,
  249. 0x00, 0x64, 0x00, 0x67, 0x00, 0x38, 0x1f, 0xde, 0x1f, 0x7a,
  250. 0x1f, 0x47, 0x1f, 0x7c, 0x00, 0x30, 0x01, 0x4b, 0x02, 0x82,
  251. 0x03, 0x73, 0x03, 0xcf, /* 6 MHz */
  252. }, {
  253. 0x1f, 0xfa, 0x1f, 0xda, 0x1f, 0xc1, 0x1f, 0xb3, 0x1f, 0xca,
  254. 0x00, 0x07, 0x00, 0x4d, 0x00, 0x6d, 0x00, 0x40, 0x1f, 0xca,
  255. 0x1f, 0x4d, 0x1f, 0x2a, 0x1f, 0xb2, 0x00, 0xec, 0x02, 0x7e,
  256. 0x03, 0xd0, 0x04, 0x53, /* 7 MHz */
  257. }, {
  258. 0x00, 0x10, 0x00, 0x0e, 0x1f, 0xf7, 0x1f, 0xc9, 0x1f, 0xa0,
  259. 0x1f, 0xa6, 0x1f, 0xec, 0x00, 0x4e, 0x00, 0x7d, 0x00, 0x3a,
  260. 0x1f, 0x98, 0x1f, 0x10, 0x1f, 0x40, 0x00, 0x75, 0x02, 0x5f,
  261. 0x04, 0x24, 0x04, 0xdb, /* 8 MHz */
  262. },
  263. };
  264. static const u8 bw_params2[3][6] = {
  265. {0xc3, 0x0c, 0x44, 0x33, 0x33, 0x30}, /* 6 MHz */
  266. {0xb8, 0xe3, 0x93, 0x99, 0x99, 0x98}, /* 7 MHz */
  267. {0xae, 0xba, 0xf3, 0x26, 0x66, 0x64}, /* 8 MHz */
  268. };
  269. dev_dbg(&priv->i2c->dev,
  270. "%s: frequency=%d bandwidth_hz=%d inversion=%d\n",
  271. __func__, c->frequency, c->bandwidth_hz, c->inversion);
  272. /* program tuner */
  273. if (fe->ops.tuner_ops.set_params)
  274. fe->ops.tuner_ops.set_params(fe);
  275. switch (c->bandwidth_hz) {
  276. case 6000000:
  277. i = 0;
  278. break;
  279. case 7000000:
  280. i = 1;
  281. break;
  282. case 8000000:
  283. i = 2;
  284. break;
  285. default:
  286. dev_dbg(&priv->i2c->dev, "%s: invalid bandwidth\n", __func__);
  287. return -EINVAL;
  288. }
  289. ret = rtl2830_wr_reg_mask(priv, 0x008, i << 1, 0x06);
  290. if (ret)
  291. goto err;
  292. /* program if frequency */
  293. if (fe->ops.tuner_ops.get_if_frequency)
  294. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  295. else
  296. ret = -EINVAL;
  297. if (ret < 0)
  298. goto err;
  299. num = if_frequency % priv->cfg.xtal;
  300. num *= 0x400000;
  301. num = div_u64(num, priv->cfg.xtal);
  302. num = -num;
  303. if_ctl = num & 0x3fffff;
  304. dev_dbg(&priv->i2c->dev, "%s: if_frequency=%d if_ctl=%08x\n",
  305. __func__, if_frequency, if_ctl);
  306. ret = rtl2830_rd_reg_mask(priv, 0x119, &tmp, 0xc0); /* b[7:6] */
  307. if (ret)
  308. goto err;
  309. buf[0] = tmp << 6;
  310. buf[0] |= (if_ctl >> 16) & 0x3f;
  311. buf[1] = (if_ctl >> 8) & 0xff;
  312. buf[2] = (if_ctl >> 0) & 0xff;
  313. ret = rtl2830_wr_regs(priv, 0x119, buf, 3);
  314. if (ret)
  315. goto err;
  316. /* 1/2 split I2C write */
  317. ret = rtl2830_wr_regs(priv, 0x11c, &bw_params1[i][0], 17);
  318. if (ret)
  319. goto err;
  320. /* 2/2 split I2C write */
  321. ret = rtl2830_wr_regs(priv, 0x12d, &bw_params1[i][17], 17);
  322. if (ret)
  323. goto err;
  324. ret = rtl2830_wr_regs(priv, 0x19d, bw_params2[i], 6);
  325. if (ret)
  326. goto err;
  327. return ret;
  328. err:
  329. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  330. return ret;
  331. }
  332. static int rtl2830_get_frontend(struct dvb_frontend *fe)
  333. {
  334. struct rtl2830_priv *priv = fe->demodulator_priv;
  335. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  336. int ret;
  337. u8 buf[3];
  338. if (priv->sleeping)
  339. return 0;
  340. ret = rtl2830_rd_regs(priv, 0x33c, buf, 2);
  341. if (ret)
  342. goto err;
  343. ret = rtl2830_rd_reg(priv, 0x351, &buf[2]);
  344. if (ret)
  345. goto err;
  346. dev_dbg(&priv->i2c->dev, "%s: TPS=%*ph\n", __func__, 3, buf);
  347. switch ((buf[0] >> 2) & 3) {
  348. case 0:
  349. c->modulation = QPSK;
  350. break;
  351. case 1:
  352. c->modulation = QAM_16;
  353. break;
  354. case 2:
  355. c->modulation = QAM_64;
  356. break;
  357. }
  358. switch ((buf[2] >> 2) & 1) {
  359. case 0:
  360. c->transmission_mode = TRANSMISSION_MODE_2K;
  361. break;
  362. case 1:
  363. c->transmission_mode = TRANSMISSION_MODE_8K;
  364. }
  365. switch ((buf[2] >> 0) & 3) {
  366. case 0:
  367. c->guard_interval = GUARD_INTERVAL_1_32;
  368. break;
  369. case 1:
  370. c->guard_interval = GUARD_INTERVAL_1_16;
  371. break;
  372. case 2:
  373. c->guard_interval = GUARD_INTERVAL_1_8;
  374. break;
  375. case 3:
  376. c->guard_interval = GUARD_INTERVAL_1_4;
  377. break;
  378. }
  379. switch ((buf[0] >> 4) & 7) {
  380. case 0:
  381. c->hierarchy = HIERARCHY_NONE;
  382. break;
  383. case 1:
  384. c->hierarchy = HIERARCHY_1;
  385. break;
  386. case 2:
  387. c->hierarchy = HIERARCHY_2;
  388. break;
  389. case 3:
  390. c->hierarchy = HIERARCHY_4;
  391. break;
  392. }
  393. switch ((buf[1] >> 3) & 7) {
  394. case 0:
  395. c->code_rate_HP = FEC_1_2;
  396. break;
  397. case 1:
  398. c->code_rate_HP = FEC_2_3;
  399. break;
  400. case 2:
  401. c->code_rate_HP = FEC_3_4;
  402. break;
  403. case 3:
  404. c->code_rate_HP = FEC_5_6;
  405. break;
  406. case 4:
  407. c->code_rate_HP = FEC_7_8;
  408. break;
  409. }
  410. switch ((buf[1] >> 0) & 7) {
  411. case 0:
  412. c->code_rate_LP = FEC_1_2;
  413. break;
  414. case 1:
  415. c->code_rate_LP = FEC_2_3;
  416. break;
  417. case 2:
  418. c->code_rate_LP = FEC_3_4;
  419. break;
  420. case 3:
  421. c->code_rate_LP = FEC_5_6;
  422. break;
  423. case 4:
  424. c->code_rate_LP = FEC_7_8;
  425. break;
  426. }
  427. return 0;
  428. err:
  429. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  430. return ret;
  431. }
  432. static int rtl2830_read_status(struct dvb_frontend *fe, fe_status_t *status)
  433. {
  434. struct rtl2830_priv *priv = fe->demodulator_priv;
  435. int ret;
  436. u8 tmp;
  437. *status = 0;
  438. if (priv->sleeping)
  439. return 0;
  440. ret = rtl2830_rd_reg_mask(priv, 0x351, &tmp, 0x78); /* [6:3] */
  441. if (ret)
  442. goto err;
  443. if (tmp == 11) {
  444. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  445. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  446. } else if (tmp == 10) {
  447. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  448. FE_HAS_VITERBI;
  449. }
  450. return ret;
  451. err:
  452. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  453. return ret;
  454. }
  455. static int rtl2830_read_snr(struct dvb_frontend *fe, u16 *snr)
  456. {
  457. struct rtl2830_priv *priv = fe->demodulator_priv;
  458. int ret, hierarchy, constellation;
  459. u8 buf[2], tmp;
  460. u16 tmp16;
  461. #define CONSTELLATION_NUM 3
  462. #define HIERARCHY_NUM 4
  463. static const u32 snr_constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
  464. { 70705899, 70705899, 70705899, 70705899 },
  465. { 82433173, 82433173, 87483115, 94445660 },
  466. { 92888734, 92888734, 95487525, 99770748 },
  467. };
  468. if (priv->sleeping)
  469. return 0;
  470. /* reports SNR in resolution of 0.1 dB */
  471. ret = rtl2830_rd_reg(priv, 0x33c, &tmp);
  472. if (ret)
  473. goto err;
  474. constellation = (tmp >> 2) & 0x03; /* [3:2] */
  475. if (constellation > CONSTELLATION_NUM - 1)
  476. goto err;
  477. hierarchy = (tmp >> 4) & 0x07; /* [6:4] */
  478. if (hierarchy > HIERARCHY_NUM - 1)
  479. goto err;
  480. ret = rtl2830_rd_regs(priv, 0x40c, buf, 2);
  481. if (ret)
  482. goto err;
  483. tmp16 = buf[0] << 8 | buf[1];
  484. if (tmp16)
  485. *snr = (snr_constant[constellation][hierarchy] -
  486. intlog10(tmp16)) / ((1 << 24) / 100);
  487. else
  488. *snr = 0;
  489. return 0;
  490. err:
  491. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  492. return ret;
  493. }
  494. static int rtl2830_read_ber(struct dvb_frontend *fe, u32 *ber)
  495. {
  496. struct rtl2830_priv *priv = fe->demodulator_priv;
  497. int ret;
  498. u8 buf[2];
  499. if (priv->sleeping)
  500. return 0;
  501. ret = rtl2830_rd_regs(priv, 0x34e, buf, 2);
  502. if (ret)
  503. goto err;
  504. *ber = buf[0] << 8 | buf[1];
  505. return 0;
  506. err:
  507. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  508. return ret;
  509. }
  510. static int rtl2830_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  511. {
  512. *ucblocks = 0;
  513. return 0;
  514. }
  515. static int rtl2830_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  516. {
  517. struct rtl2830_priv *priv = fe->demodulator_priv;
  518. int ret;
  519. u8 buf[2];
  520. u16 if_agc_raw, if_agc;
  521. if (priv->sleeping)
  522. return 0;
  523. ret = rtl2830_rd_regs(priv, 0x359, buf, 2);
  524. if (ret)
  525. goto err;
  526. if_agc_raw = (buf[0] << 8 | buf[1]) & 0x3fff;
  527. if (if_agc_raw & (1 << 9))
  528. if_agc = -(~(if_agc_raw - 1) & 0x1ff);
  529. else
  530. if_agc = if_agc_raw;
  531. *strength = (u8) (55 - if_agc / 182);
  532. *strength |= *strength << 8;
  533. return 0;
  534. err:
  535. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  536. return ret;
  537. }
  538. static struct dvb_frontend_ops rtl2830_ops;
  539. static u32 rtl2830_tuner_i2c_func(struct i2c_adapter *adapter)
  540. {
  541. return I2C_FUNC_I2C;
  542. }
  543. static int rtl2830_tuner_i2c_xfer(struct i2c_adapter *i2c_adap,
  544. struct i2c_msg msg[], int num)
  545. {
  546. struct rtl2830_priv *priv = i2c_get_adapdata(i2c_adap);
  547. int ret;
  548. /* open i2c-gate */
  549. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x08, 0x08);
  550. if (ret)
  551. goto err;
  552. ret = i2c_transfer(priv->i2c, msg, num);
  553. if (ret < 0)
  554. dev_warn(&priv->i2c->dev, "%s: tuner i2c failed=%d\n",
  555. KBUILD_MODNAME, ret);
  556. return ret;
  557. err:
  558. dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
  559. return ret;
  560. }
  561. static struct i2c_algorithm rtl2830_tuner_i2c_algo = {
  562. .master_xfer = rtl2830_tuner_i2c_xfer,
  563. .functionality = rtl2830_tuner_i2c_func,
  564. };
  565. struct i2c_adapter *rtl2830_get_tuner_i2c_adapter(struct dvb_frontend *fe)
  566. {
  567. struct rtl2830_priv *priv = fe->demodulator_priv;
  568. return &priv->tuner_i2c_adapter;
  569. }
  570. EXPORT_SYMBOL(rtl2830_get_tuner_i2c_adapter);
  571. static void rtl2830_release(struct dvb_frontend *fe)
  572. {
  573. struct rtl2830_priv *priv = fe->demodulator_priv;
  574. i2c_del_adapter(&priv->tuner_i2c_adapter);
  575. kfree(priv);
  576. }
  577. struct dvb_frontend *rtl2830_attach(const struct rtl2830_config *cfg,
  578. struct i2c_adapter *i2c)
  579. {
  580. struct rtl2830_priv *priv = NULL;
  581. int ret = 0;
  582. u8 tmp;
  583. /* allocate memory for the internal state */
  584. priv = kzalloc(sizeof(struct rtl2830_priv), GFP_KERNEL);
  585. if (priv == NULL)
  586. goto err;
  587. /* setup the priv */
  588. priv->i2c = i2c;
  589. memcpy(&priv->cfg, cfg, sizeof(struct rtl2830_config));
  590. /* check if the demod is there */
  591. ret = rtl2830_rd_reg(priv, 0x000, &tmp);
  592. if (ret)
  593. goto err;
  594. /* create dvb_frontend */
  595. memcpy(&priv->fe.ops, &rtl2830_ops, sizeof(struct dvb_frontend_ops));
  596. priv->fe.demodulator_priv = priv;
  597. /* create tuner i2c adapter */
  598. strlcpy(priv->tuner_i2c_adapter.name, "RTL2830 tuner I2C adapter",
  599. sizeof(priv->tuner_i2c_adapter.name));
  600. priv->tuner_i2c_adapter.algo = &rtl2830_tuner_i2c_algo;
  601. priv->tuner_i2c_adapter.algo_data = NULL;
  602. priv->tuner_i2c_adapter.dev.parent = &i2c->dev;
  603. i2c_set_adapdata(&priv->tuner_i2c_adapter, priv);
  604. if (i2c_add_adapter(&priv->tuner_i2c_adapter) < 0) {
  605. dev_err(&i2c->dev,
  606. "%s: tuner i2c bus could not be initialized\n",
  607. KBUILD_MODNAME);
  608. goto err;
  609. }
  610. priv->sleeping = true;
  611. return &priv->fe;
  612. err:
  613. dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
  614. kfree(priv);
  615. return NULL;
  616. }
  617. EXPORT_SYMBOL(rtl2830_attach);
  618. static struct dvb_frontend_ops rtl2830_ops = {
  619. .delsys = { SYS_DVBT },
  620. .info = {
  621. .name = "Realtek RTL2830 (DVB-T)",
  622. .caps = FE_CAN_FEC_1_2 |
  623. FE_CAN_FEC_2_3 |
  624. FE_CAN_FEC_3_4 |
  625. FE_CAN_FEC_5_6 |
  626. FE_CAN_FEC_7_8 |
  627. FE_CAN_FEC_AUTO |
  628. FE_CAN_QPSK |
  629. FE_CAN_QAM_16 |
  630. FE_CAN_QAM_64 |
  631. FE_CAN_QAM_AUTO |
  632. FE_CAN_TRANSMISSION_MODE_AUTO |
  633. FE_CAN_GUARD_INTERVAL_AUTO |
  634. FE_CAN_HIERARCHY_AUTO |
  635. FE_CAN_RECOVER |
  636. FE_CAN_MUTE_TS
  637. },
  638. .release = rtl2830_release,
  639. .init = rtl2830_init,
  640. .sleep = rtl2830_sleep,
  641. .get_tune_settings = rtl2830_get_tune_settings,
  642. .set_frontend = rtl2830_set_frontend,
  643. .get_frontend = rtl2830_get_frontend,
  644. .read_status = rtl2830_read_status,
  645. .read_snr = rtl2830_read_snr,
  646. .read_ber = rtl2830_read_ber,
  647. .read_ucblocks = rtl2830_read_ucblocks,
  648. .read_signal_strength = rtl2830_read_signal_strength,
  649. };
  650. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  651. MODULE_DESCRIPTION("Realtek RTL2830 DVB-T demodulator driver");
  652. MODULE_LICENSE("GPL");