rtl2830.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 hardware registers */
  30. static int rtl2830_wr(struct rtl2830_priv *priv, u8 reg, u8 *val, int len)
  31. {
  32. int ret;
  33. u8 buf[1+len];
  34. struct i2c_msg msg[1] = {
  35. {
  36. .addr = priv->cfg.i2c_addr,
  37. .flags = 0,
  38. .len = 1+len,
  39. .buf = buf,
  40. }
  41. };
  42. buf[0] = reg;
  43. memcpy(&buf[1], val, len);
  44. ret = i2c_transfer(priv->i2c, msg, 1);
  45. if (ret == 1) {
  46. ret = 0;
  47. } else {
  48. warn("i2c wr failed=%d reg=%02x len=%d", ret, reg, len);
  49. ret = -EREMOTEIO;
  50. }
  51. return ret;
  52. }
  53. /* read multiple hardware registers */
  54. static int rtl2830_rd(struct rtl2830_priv *priv, u8 reg, u8 *val, int len)
  55. {
  56. int ret;
  57. struct i2c_msg msg[2] = {
  58. {
  59. .addr = priv->cfg.i2c_addr,
  60. .flags = 0,
  61. .len = 1,
  62. .buf = &reg,
  63. }, {
  64. .addr = priv->cfg.i2c_addr,
  65. .flags = I2C_M_RD,
  66. .len = len,
  67. .buf = val,
  68. }
  69. };
  70. ret = i2c_transfer(priv->i2c, msg, 2);
  71. if (ret == 2) {
  72. ret = 0;
  73. } else {
  74. warn("i2c rd failed=%d reg=%02x len=%d", ret, reg, len);
  75. ret = -EREMOTEIO;
  76. }
  77. return ret;
  78. }
  79. /* write multiple registers */
  80. static int rtl2830_wr_regs(struct rtl2830_priv *priv, u16 reg, u8 *val, int len)
  81. {
  82. int ret;
  83. u8 reg2 = (reg >> 0) & 0xff;
  84. u8 page = (reg >> 8) & 0xff;
  85. /* switch bank if needed */
  86. if (page != priv->page) {
  87. ret = rtl2830_wr(priv, 0x00, &page, 1);
  88. if (ret)
  89. return ret;
  90. priv->page = page;
  91. }
  92. return rtl2830_wr(priv, reg2, val, len);
  93. }
  94. /* read multiple registers */
  95. static int rtl2830_rd_regs(struct rtl2830_priv *priv, u16 reg, u8 *val, int len)
  96. {
  97. int ret;
  98. u8 reg2 = (reg >> 0) & 0xff;
  99. u8 page = (reg >> 8) & 0xff;
  100. /* switch bank if needed */
  101. if (page != priv->page) {
  102. ret = rtl2830_wr(priv, 0x00, &page, 1);
  103. if (ret)
  104. return ret;
  105. priv->page = page;
  106. }
  107. return rtl2830_rd(priv, reg2, val, len);
  108. }
  109. #if 0 /* currently not used */
  110. /* write single register */
  111. static int rtl2830_wr_reg(struct rtl2830_priv *priv, u16 reg, u8 val)
  112. {
  113. return rtl2830_wr_regs(priv, reg, &val, 1);
  114. }
  115. #endif
  116. /* read single register */
  117. static int rtl2830_rd_reg(struct rtl2830_priv *priv, u16 reg, u8 *val)
  118. {
  119. return rtl2830_rd_regs(priv, reg, val, 1);
  120. }
  121. /* write single register with mask */
  122. int rtl2830_wr_reg_mask(struct rtl2830_priv *priv, u16 reg, u8 val, u8 mask)
  123. {
  124. int ret;
  125. u8 tmp;
  126. /* no need for read if whole reg is written */
  127. if (mask != 0xff) {
  128. ret = rtl2830_rd_regs(priv, reg, &tmp, 1);
  129. if (ret)
  130. return ret;
  131. val &= mask;
  132. tmp &= ~mask;
  133. val |= tmp;
  134. }
  135. return rtl2830_wr_regs(priv, reg, &val, 1);
  136. }
  137. /* read single register with mask */
  138. int rtl2830_rd_reg_mask(struct rtl2830_priv *priv, u16 reg, u8 *val, u8 mask)
  139. {
  140. int ret, i;
  141. u8 tmp;
  142. ret = rtl2830_rd_regs(priv, reg, &tmp, 1);
  143. if (ret)
  144. return ret;
  145. tmp &= mask;
  146. /* find position of the first bit */
  147. for (i = 0; i < 8; i++) {
  148. if ((mask >> i) & 0x01)
  149. break;
  150. }
  151. *val = tmp >> i;
  152. return 0;
  153. }
  154. static int rtl2830_init(struct dvb_frontend *fe)
  155. {
  156. struct rtl2830_priv *priv = fe->demodulator_priv;
  157. int ret, i;
  158. u64 num;
  159. u8 buf[3], tmp;
  160. u32 if_ctl;
  161. struct rtl2830_reg_val_mask tab[] = {
  162. { 0x00d, 0x01, 0x03 },
  163. { 0x00d, 0x10, 0x10 },
  164. { 0x104, 0x00, 0x1e },
  165. { 0x105, 0x80, 0x80 },
  166. { 0x110, 0x02, 0x03 },
  167. { 0x110, 0x08, 0x0c },
  168. { 0x17b, 0x00, 0x40 },
  169. { 0x17d, 0x05, 0x0f },
  170. { 0x17d, 0x50, 0xf0 },
  171. { 0x18c, 0x08, 0x0f },
  172. { 0x18d, 0x00, 0xc0 },
  173. { 0x188, 0x05, 0x0f },
  174. { 0x189, 0x00, 0xfc },
  175. { 0x2d5, 0x02, 0x02 },
  176. { 0x2f1, 0x02, 0x06 },
  177. { 0x2f1, 0x20, 0xf8 },
  178. { 0x16d, 0x00, 0x01 },
  179. { 0x1a6, 0x00, 0x80 },
  180. { 0x106, priv->cfg.vtop, 0x3f },
  181. { 0x107, priv->cfg.krf, 0x3f },
  182. { 0x112, 0x28, 0xff },
  183. { 0x103, priv->cfg.agc_targ_val, 0xff },
  184. { 0x00a, 0x02, 0x07 },
  185. { 0x140, 0x0c, 0x3c },
  186. { 0x140, 0x40, 0xc0 },
  187. { 0x15b, 0x05, 0x07 },
  188. { 0x15b, 0x28, 0x38 },
  189. { 0x15c, 0x05, 0x07 },
  190. { 0x15c, 0x28, 0x38 },
  191. { 0x115, priv->cfg.spec_inv, 0x01 },
  192. { 0x16f, 0x01, 0x07 },
  193. { 0x170, 0x18, 0x38 },
  194. { 0x172, 0x0f, 0x0f },
  195. { 0x173, 0x08, 0x38 },
  196. { 0x175, 0x01, 0x07 },
  197. { 0x176, 0x00, 0xc0 },
  198. };
  199. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  200. ret = rtl2830_wr_reg_mask(priv, tab[i].reg, tab[i].val,
  201. tab[i].mask);
  202. if (ret)
  203. goto err;
  204. }
  205. ret = rtl2830_wr_regs(priv, 0x18f, "\x28\x00", 2);
  206. if (ret)
  207. goto err;
  208. ret = rtl2830_wr_regs(priv, 0x195,
  209. "\x04\x06\x0a\x12\x0a\x12\x1e\x28", 8);
  210. if (ret)
  211. goto err;
  212. num = priv->cfg.if_dvbt % priv->cfg.xtal;
  213. num *= 0x400000;
  214. num = div_u64(num, priv->cfg.xtal);
  215. num = -num;
  216. if_ctl = num & 0x3fffff;
  217. dbg("%s: if_ctl=%08x", __func__, if_ctl);
  218. ret = rtl2830_rd_reg_mask(priv, 0x119, &tmp, 0xc0); /* b[7:6] */
  219. if (ret)
  220. goto err;
  221. buf[0] = tmp << 6;
  222. buf[0] = (if_ctl >> 16) & 0x3f;
  223. buf[1] = (if_ctl >> 8) & 0xff;
  224. buf[2] = (if_ctl >> 0) & 0xff;
  225. ret = rtl2830_wr_regs(priv, 0x119, buf, 3);
  226. if (ret)
  227. goto err;
  228. /* TODO: spec init */
  229. /* soft reset */
  230. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x04, 0x04);
  231. if (ret)
  232. goto err;
  233. ret = rtl2830_wr_reg_mask(priv, 0x101, 0x00, 0x04);
  234. if (ret)
  235. goto err;
  236. priv->sleeping = false;
  237. return ret;
  238. err:
  239. dbg("%s: failed=%d", __func__, ret);
  240. return ret;
  241. }
  242. static int rtl2830_sleep(struct dvb_frontend *fe)
  243. {
  244. struct rtl2830_priv *priv = fe->demodulator_priv;
  245. priv->sleeping = true;
  246. return 0;
  247. }
  248. int rtl2830_get_tune_settings(struct dvb_frontend *fe,
  249. struct dvb_frontend_tune_settings *s)
  250. {
  251. s->min_delay_ms = 500;
  252. s->step_size = fe->ops.info.frequency_stepsize * 2;
  253. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  254. return 0;
  255. }
  256. static int rtl2830_set_frontend(struct dvb_frontend *fe)
  257. {
  258. struct rtl2830_priv *priv = fe->demodulator_priv;
  259. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  260. int ret, i;
  261. static u8 bw_params1[3][34] = {
  262. {
  263. 0x1f, 0xf0, 0x1f, 0xf0, 0x1f, 0xfa, 0x00, 0x17, 0x00, 0x41,
  264. 0x00, 0x64, 0x00, 0x67, 0x00, 0x38, 0x1f, 0xde, 0x1f, 0x7a,
  265. 0x1f, 0x47, 0x1f, 0x7c, 0x00, 0x30, 0x01, 0x4b, 0x02, 0x82,
  266. 0x03, 0x73, 0x03, 0xcf, /* 6 MHz */
  267. }, {
  268. 0x1f, 0xfa, 0x1f, 0xda, 0x1f, 0xc1, 0x1f, 0xb3, 0x1f, 0xca,
  269. 0x00, 0x07, 0x00, 0x4d, 0x00, 0x6d, 0x00, 0x40, 0x1f, 0xca,
  270. 0x1f, 0x4d, 0x1f, 0x2a, 0x1f, 0xb2, 0x00, 0xec, 0x02, 0x7e,
  271. 0x03, 0xd0, 0x04, 0x53, /* 7 MHz */
  272. }, {
  273. 0x00, 0x10, 0x00, 0x0e, 0x1f, 0xf7, 0x1f, 0xc9, 0x1f, 0xa0,
  274. 0x1f, 0xa6, 0x1f, 0xec, 0x00, 0x4e, 0x00, 0x7d, 0x00, 0x3a,
  275. 0x1f, 0x98, 0x1f, 0x10, 0x1f, 0x40, 0x00, 0x75, 0x02, 0x5f,
  276. 0x04, 0x24, 0x04, 0xdb, /* 8 MHz */
  277. },
  278. };
  279. static u8 bw_params2[3][6] = {
  280. {0xc3, 0x0c, 0x44, 0x33, 0x33, 0x30,}, /* 6 MHz */
  281. {0xb8, 0xe3, 0x93, 0x99, 0x99, 0x98,}, /* 7 MHz */
  282. {0xae, 0xba, 0xf3, 0x26, 0x66, 0x64,}, /* 8 MHz */
  283. };
  284. dbg("%s: frequency=%d bandwidth_hz=%d inversion=%d", __func__,
  285. 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. dbg("invalid bandwidth");
  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. dbg("%s: failed=%d", __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. dbg("%s: TPS=%02x %02x %02x", __func__, buf[0], buf[1], buf[2]);
  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. dbg("%s: failed=%d", __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. dbg("%s: failed=%d", __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. dbg("%s: failed=%d", __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. dbg("%s: failed=%d", __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. dbg("%s: failed=%d", __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. warn("tuner i2c failed=%d", ret);
  545. return ret;
  546. err:
  547. dbg("%s: failed=%d", __func__, ret);
  548. return ret;
  549. }
  550. static struct i2c_algorithm rtl2830_tuner_i2c_algo = {
  551. .master_xfer = rtl2830_tuner_i2c_xfer,
  552. .functionality = rtl2830_tuner_i2c_func,
  553. };
  554. struct i2c_adapter *rtl2830_get_tuner_i2c_adapter(struct dvb_frontend *fe)
  555. {
  556. struct rtl2830_priv *priv = fe->demodulator_priv;
  557. return &priv->tuner_i2c_adapter;
  558. }
  559. EXPORT_SYMBOL(rtl2830_get_tuner_i2c_adapter);
  560. static void rtl2830_release(struct dvb_frontend *fe)
  561. {
  562. struct rtl2830_priv *priv = fe->demodulator_priv;
  563. i2c_del_adapter(&priv->tuner_i2c_adapter);
  564. kfree(priv);
  565. }
  566. struct dvb_frontend *rtl2830_attach(const struct rtl2830_config *cfg,
  567. struct i2c_adapter *i2c)
  568. {
  569. struct rtl2830_priv *priv = NULL;
  570. int ret = 0;
  571. u8 tmp;
  572. /* allocate memory for the internal state */
  573. priv = kzalloc(sizeof(struct rtl2830_priv), GFP_KERNEL);
  574. if (priv == NULL)
  575. goto err;
  576. /* setup the priv */
  577. priv->i2c = i2c;
  578. memcpy(&priv->cfg, cfg, sizeof(struct rtl2830_config));
  579. /* check if the demod is there */
  580. ret = rtl2830_rd_reg(priv, 0x000, &tmp);
  581. if (ret)
  582. goto err;
  583. /* create dvb_frontend */
  584. memcpy(&priv->fe.ops, &rtl2830_ops, sizeof(struct dvb_frontend_ops));
  585. priv->fe.demodulator_priv = priv;
  586. /* create tuner i2c adapter */
  587. strlcpy(priv->tuner_i2c_adapter.name, "RTL2830 tuner I2C adapter",
  588. sizeof(priv->tuner_i2c_adapter.name));
  589. priv->tuner_i2c_adapter.algo = &rtl2830_tuner_i2c_algo;
  590. priv->tuner_i2c_adapter.algo_data = NULL;
  591. i2c_set_adapdata(&priv->tuner_i2c_adapter, priv);
  592. if (i2c_add_adapter(&priv->tuner_i2c_adapter) < 0) {
  593. err("tuner I2C bus could not be initialized");
  594. goto err;
  595. }
  596. priv->sleeping = true;
  597. return &priv->fe;
  598. err:
  599. dbg("%s: failed=%d", __func__, ret);
  600. kfree(priv);
  601. return NULL;
  602. }
  603. EXPORT_SYMBOL(rtl2830_attach);
  604. static struct dvb_frontend_ops rtl2830_ops = {
  605. .delsys = { SYS_DVBT },
  606. .info = {
  607. .name = "Realtek RTL2830 (DVB-T)",
  608. .caps = FE_CAN_FEC_1_2 |
  609. FE_CAN_FEC_2_3 |
  610. FE_CAN_FEC_3_4 |
  611. FE_CAN_FEC_5_6 |
  612. FE_CAN_FEC_7_8 |
  613. FE_CAN_FEC_AUTO |
  614. FE_CAN_QPSK |
  615. FE_CAN_QAM_16 |
  616. FE_CAN_QAM_64 |
  617. FE_CAN_QAM_AUTO |
  618. FE_CAN_TRANSMISSION_MODE_AUTO |
  619. FE_CAN_GUARD_INTERVAL_AUTO |
  620. FE_CAN_HIERARCHY_AUTO |
  621. FE_CAN_RECOVER |
  622. FE_CAN_MUTE_TS
  623. },
  624. .release = rtl2830_release,
  625. .init = rtl2830_init,
  626. .sleep = rtl2830_sleep,
  627. .get_tune_settings = rtl2830_get_tune_settings,
  628. .set_frontend = rtl2830_set_frontend,
  629. .get_frontend = rtl2830_get_frontend,
  630. .read_status = rtl2830_read_status,
  631. .read_snr = rtl2830_read_snr,
  632. .read_ber = rtl2830_read_ber,
  633. .read_ucblocks = rtl2830_read_ucblocks,
  634. .read_signal_strength = rtl2830_read_signal_strength,
  635. };
  636. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  637. MODULE_DESCRIPTION("Realtek RTL2830 DVB-T demodulator driver");
  638. MODULE_LICENSE("GPL");