rtl2830.c 16 KB

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