rtl28xxu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * Realtek RTL28xxU DVB USB driver
  3. *
  4. * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
  5. * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "rtl28xxu.h"
  22. #include "rtl2830.h"
  23. #include "qt1010.h"
  24. #include "mt2060.h"
  25. #include "mxl5005s.h"
  26. /* debug */
  27. static int dvb_usb_rtl28xxu_debug;
  28. module_param_named(debug, dvb_usb_rtl28xxu_debug, int, 0644);
  29. MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
  30. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  31. static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
  32. {
  33. int ret;
  34. unsigned int pipe;
  35. u8 requesttype;
  36. u8 *buf;
  37. buf = kmalloc(req->size, GFP_KERNEL);
  38. if (!buf) {
  39. ret = -ENOMEM;
  40. goto err;
  41. }
  42. if (req->index & CMD_WR_FLAG) {
  43. /* write */
  44. memcpy(buf, req->data, req->size);
  45. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  46. pipe = usb_sndctrlpipe(d->udev, 0);
  47. } else {
  48. /* read */
  49. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  50. pipe = usb_rcvctrlpipe(d->udev, 0);
  51. }
  52. ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
  53. req->index, buf, req->size, 1000);
  54. if (ret > 0)
  55. ret = 0;
  56. deb_dump(0, requesttype, req->value, req->index, buf, req->size,
  57. deb_xfer);
  58. /* read request, copy returned data to return buf */
  59. if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
  60. memcpy(req->data, buf, req->size);
  61. kfree(buf);
  62. if (ret)
  63. goto err;
  64. return ret;
  65. err:
  66. deb_info("%s: failed=%d\n", __func__, ret);
  67. return ret;
  68. }
  69. static int rtl2831_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  70. {
  71. struct rtl28xxu_req req;
  72. if (reg < 0x3000)
  73. req.index = CMD_USB_WR;
  74. else if (reg < 0x4000)
  75. req.index = CMD_SYS_WR;
  76. else
  77. req.index = CMD_IR_WR;
  78. req.value = reg;
  79. req.size = len;
  80. req.data = val;
  81. return rtl28xxu_ctrl_msg(d, &req);
  82. }
  83. static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  84. {
  85. struct rtl28xxu_req req;
  86. if (reg < 0x3000)
  87. req.index = CMD_USB_RD;
  88. else if (reg < 0x4000)
  89. req.index = CMD_SYS_RD;
  90. else
  91. req.index = CMD_IR_RD;
  92. req.value = reg;
  93. req.size = len;
  94. req.data = val;
  95. return rtl28xxu_ctrl_msg(d, &req);
  96. }
  97. static int rtl2831_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
  98. {
  99. return rtl2831_wr_regs(d, reg, &val, 1);
  100. }
  101. static int rtl2831_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
  102. {
  103. return rtl2831_rd_regs(d, reg, val, 1);
  104. }
  105. /* I2C */
  106. static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  107. int num)
  108. {
  109. int ret;
  110. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  111. struct rtl28xxu_priv *priv = d->priv;
  112. struct rtl28xxu_req req;
  113. /*
  114. * It is not known which are real I2C bus xfer limits, but testing
  115. * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
  116. * TODO: find out RTL2832U lens
  117. */
  118. /*
  119. * I2C adapter logic looks rather complicated due to fact it handles
  120. * three different access methods. Those methods are;
  121. * 1) integrated demod access
  122. * 2) old I2C access
  123. * 3) new I2C access
  124. *
  125. * Used method is selected in order 1, 2, 3. Method 3 can handle all
  126. * requests but there is two reasons why not use it always;
  127. * 1) It is most expensive, usually two USB messages are needed
  128. * 2) At least RTL2831U does not support it
  129. *
  130. * Method 3 is needed in case of I2C write+read (typical register read)
  131. * where write is more than one byte.
  132. */
  133. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  134. return -EAGAIN;
  135. if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
  136. (msg[1].flags & I2C_M_RD)) {
  137. if (msg[0].len > 24 || msg[1].len > 24) {
  138. /* TODO: check msg[0].len max */
  139. ret = -EOPNOTSUPP;
  140. goto err_mutex_unlock;
  141. } else if (msg[0].addr == 0x10) {
  142. /* method 1 - integrated demod */
  143. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  144. req.index = CMD_DEMOD_RD | priv->page;
  145. req.size = msg[1].len;
  146. req.data = &msg[1].buf[0];
  147. ret = rtl28xxu_ctrl_msg(d, &req);
  148. } else if (msg[0].len < 2) {
  149. /* method 2 - old I2C */
  150. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  151. req.index = CMD_I2C_RD;
  152. req.size = msg[1].len;
  153. req.data = &msg[1].buf[0];
  154. ret = rtl28xxu_ctrl_msg(d, &req);
  155. } else {
  156. /* method 3 - new I2C */
  157. req.value = (msg[0].addr << 1);
  158. req.index = CMD_I2C_DA_WR;
  159. req.size = msg[0].len;
  160. req.data = msg[0].buf;
  161. ret = rtl28xxu_ctrl_msg(d, &req);
  162. if (ret)
  163. goto err_mutex_unlock;
  164. req.value = (msg[0].addr << 1);
  165. req.index = CMD_I2C_DA_RD;
  166. req.size = msg[1].len;
  167. req.data = msg[1].buf;
  168. ret = rtl28xxu_ctrl_msg(d, &req);
  169. }
  170. } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
  171. if (msg[0].len > 22) {
  172. /* TODO: check msg[0].len max */
  173. ret = -EOPNOTSUPP;
  174. goto err_mutex_unlock;
  175. } else if (msg[0].addr == 0x10) {
  176. /* method 1 - integrated demod */
  177. if (msg[0].buf[0] == 0x00) {
  178. /* save demod page for later demod access */
  179. priv->page = msg[0].buf[1];
  180. ret = 0;
  181. } else {
  182. req.value = (msg[0].buf[0] << 8) |
  183. (msg[0].addr << 1);
  184. req.index = CMD_DEMOD_WR | priv->page;
  185. req.size = msg[0].len-1;
  186. req.data = &msg[0].buf[1];
  187. ret = rtl28xxu_ctrl_msg(d, &req);
  188. }
  189. } else if (msg[0].len < 23) {
  190. /* method 2 - old I2C */
  191. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  192. req.index = CMD_I2C_WR;
  193. req.size = msg[0].len-1;
  194. req.data = &msg[0].buf[1];
  195. ret = rtl28xxu_ctrl_msg(d, &req);
  196. } else {
  197. /* method 3 - new I2C */
  198. req.value = (msg[0].addr << 1);
  199. req.index = CMD_I2C_DA_WR;
  200. req.size = msg[0].len;
  201. req.data = msg[0].buf;
  202. ret = rtl28xxu_ctrl_msg(d, &req);
  203. }
  204. } else {
  205. ret = -EINVAL;
  206. }
  207. err_mutex_unlock:
  208. mutex_unlock(&d->i2c_mutex);
  209. return ret ? ret : num;
  210. }
  211. static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
  212. {
  213. return I2C_FUNC_I2C;
  214. }
  215. static struct i2c_algorithm rtl28xxu_i2c_algo = {
  216. .master_xfer = rtl28xxu_i2c_xfer,
  217. .functionality = rtl28xxu_i2c_func,
  218. };
  219. static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
  220. .i2c_addr = 0x10, /* 0x20 */
  221. .xtal = 28800000,
  222. .ts_mode = 0,
  223. .spec_inv = 1,
  224. .if_dvbt = 36150000,
  225. .vtop = 0x20,
  226. .krf = 0x04,
  227. .agc_targ_val = 0x2d,
  228. };
  229. static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
  230. .i2c_addr = 0x10, /* 0x20 */
  231. .xtal = 28800000,
  232. .ts_mode = 0,
  233. .spec_inv = 1,
  234. .if_dvbt = 36125000,
  235. .vtop = 0x20,
  236. .krf = 0x04,
  237. .agc_targ_val = 0x2d,
  238. };
  239. static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
  240. .i2c_addr = 0x10, /* 0x20 */
  241. .xtal = 28800000,
  242. .ts_mode = 0,
  243. .spec_inv = 0,
  244. .if_dvbt = 4570000,
  245. .vtop = 0x3f,
  246. .krf = 0x04,
  247. .agc_targ_val = 0x3e,
  248. };
  249. static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
  250. {
  251. int ret;
  252. struct rtl28xxu_priv *priv = adap->dev->priv;
  253. u8 buf[1];
  254. struct rtl2830_config *rtl2830_config;
  255. /* open RTL2831U/RTL2830 I2C gate */
  256. struct rtl28xxu_req req_gate = { 0x0120, 0x0011, 0x0001, "\x08" };
  257. /* for MT2060 tuner probe */
  258. struct rtl28xxu_req req_mt2060 = { 0x00c0, CMD_I2C_RD, 1, buf };
  259. /* for QT1010 tuner probe */
  260. struct rtl28xxu_req req_qt1010 = { 0x0fc4, CMD_I2C_RD, 1, buf };
  261. deb_info("%s:\n", __func__);
  262. /*
  263. * RTL2831U GPIOs
  264. * =========================================================
  265. * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
  266. * GPIO2 | LED | 0 off | 1 on |
  267. * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
  268. */
  269. /* GPIO direction */
  270. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
  271. if (ret)
  272. goto err;
  273. /* enable as output GPIO0, GPIO2, GPIO4 */
  274. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
  275. if (ret)
  276. goto err;
  277. /*
  278. * Probe used tuner. We need to know used tuner before demod attach
  279. * since there is some demod params needed to set according to tuner.
  280. */
  281. /* open demod I2C gate */
  282. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
  283. if (ret)
  284. goto err;
  285. /* check QT1010 ID(?) register; reg=0f val=2c */
  286. ret = rtl28xxu_ctrl_msg(adap->dev, &req_qt1010);
  287. if (ret == 0 && buf[0] == 0x2c) {
  288. priv->tuner = TUNER_RTL2830_QT1010;
  289. rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
  290. deb_info("%s: QT1010\n", __func__);
  291. goto found;
  292. } else {
  293. deb_info("%s: QT1010 probe failed=%d - %02x\n",
  294. __func__, ret, buf[0]);
  295. }
  296. /* open demod I2C gate */
  297. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
  298. if (ret)
  299. goto err;
  300. /* check MT2060 ID register; reg=00 val=63 */
  301. ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2060);
  302. if (ret == 0 && buf[0] == 0x63) {
  303. priv->tuner = TUNER_RTL2830_MT2060;
  304. rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
  305. deb_info("%s: MT2060\n", __func__);
  306. goto found;
  307. } else {
  308. deb_info("%s: MT2060 probe failed=%d - %02x\n",
  309. __func__, ret, buf[0]);
  310. }
  311. /* assume MXL5005S */
  312. ret = 0;
  313. priv->tuner = TUNER_RTL2830_MXL5005S;
  314. rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
  315. deb_info("%s: MXL5005S\n", __func__);
  316. goto found;
  317. found:
  318. /* attach demodulator */
  319. adap->fe_adap[0].fe = dvb_attach(rtl2830_attach, rtl2830_config,
  320. &adap->dev->i2c_adap);
  321. if (adap->fe_adap[0].fe == NULL) {
  322. ret = -ENODEV;
  323. goto err;
  324. }
  325. return ret;
  326. err:
  327. deb_info("%s: failed=%d\n", __func__, ret);
  328. return ret;
  329. }
  330. static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
  331. {
  332. int ret;
  333. struct rtl28xxu_priv *priv = adap->dev->priv;
  334. u8 buf[1];
  335. /* open RTL2832U/RTL2832 I2C gate */
  336. struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
  337. /* close RTL2832U/RTL2832 I2C gate */
  338. struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
  339. /* for FC2580 tuner probe */
  340. struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
  341. deb_info("%s:\n", __func__);
  342. /* GPIO direction */
  343. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
  344. if (ret)
  345. goto err;
  346. /* enable as output GPIO0, GPIO2, GPIO4 */
  347. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
  348. if (ret)
  349. goto err;
  350. ret = rtl2831_wr_reg(adap->dev, SYS_DEMOD_CTL, 0xe8);
  351. if (ret)
  352. goto err;
  353. /*
  354. * Probe used tuner. We need to know used tuner before demod attach
  355. * since there is some demod params needed to set according to tuner.
  356. */
  357. /* open demod I2C gate */
  358. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_open);
  359. if (ret)
  360. goto err;
  361. /* check FC2580 ID register; reg=01 val=56 */
  362. ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc2580);
  363. if (ret == 0 && buf[0] == 0x56) {
  364. priv->tuner = TUNER_RTL2832_FC2580;
  365. deb_info("%s: FC2580\n", __func__);
  366. goto found;
  367. } else {
  368. deb_info("%s: FC2580 probe failed=%d - %02x\n",
  369. __func__, ret, buf[0]);
  370. }
  371. /* close demod I2C gate */
  372. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
  373. if (ret)
  374. goto err;
  375. /* tuner not found */
  376. ret = -ENODEV;
  377. goto err;
  378. found:
  379. /* close demod I2C gate */
  380. ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
  381. if (ret)
  382. goto err;
  383. /* attach demodulator */
  384. /* TODO: */
  385. return ret;
  386. err:
  387. deb_info("%s: failed=%d\n", __func__, ret);
  388. return ret;
  389. }
  390. static struct qt1010_config rtl28xxu_qt1010_config = {
  391. .i2c_address = 0x62, /* 0xc4 */
  392. };
  393. static struct mt2060_config rtl28xxu_mt2060_config = {
  394. .i2c_address = 0x60, /* 0xc0 */
  395. .clock_out = 0,
  396. };
  397. static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
  398. .i2c_address = 0x63, /* 0xc6 */
  399. .if_freq = IF_FREQ_4570000HZ,
  400. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  401. .agc_mode = MXL_SINGLE_AGC,
  402. .tracking_filter = MXL_TF_C_H,
  403. .rssi_enable = MXL_RSSI_ENABLE,
  404. .cap_select = MXL_CAP_SEL_ENABLE,
  405. .div_out = MXL_DIV_OUT_4,
  406. .clock_out = MXL_CLOCK_OUT_DISABLE,
  407. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  408. .top = MXL5005S_TOP_25P2,
  409. .mod_mode = MXL_DIGITAL_MODE,
  410. .if_mode = MXL_ZERO_IF,
  411. .AgcMasterByte = 0x00,
  412. };
  413. static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
  414. {
  415. int ret;
  416. struct rtl28xxu_priv *priv = adap->dev->priv;
  417. struct i2c_adapter *rtl2830_tuner_i2c;
  418. struct dvb_frontend *fe;
  419. deb_info("%s:\n", __func__);
  420. /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
  421. rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe_adap[0].fe);
  422. switch (priv->tuner) {
  423. case TUNER_RTL2830_QT1010:
  424. fe = dvb_attach(qt1010_attach, adap->fe_adap[0].fe,
  425. rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
  426. break;
  427. case TUNER_RTL2830_MT2060:
  428. fe = dvb_attach(mt2060_attach, adap->fe_adap[0].fe,
  429. rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
  430. 1220);
  431. break;
  432. case TUNER_RTL2830_MXL5005S:
  433. fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
  434. rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
  435. break;
  436. default:
  437. fe = NULL;
  438. err("unknown tuner=%d", priv->tuner);
  439. }
  440. if (fe == NULL) {
  441. ret = -ENODEV;
  442. goto err;
  443. }
  444. return 0;
  445. err:
  446. deb_info("%s: failed=%d\n", __func__, ret);
  447. return ret;
  448. }
  449. static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
  450. {
  451. int ret;
  452. struct rtl28xxu_priv *priv = adap->dev->priv;
  453. struct dvb_frontend *fe;
  454. deb_info("%s:\n", __func__);
  455. switch (priv->tuner) {
  456. case TUNER_RTL2832_FC2580:
  457. /* TODO: */
  458. fe = NULL;
  459. break;
  460. default:
  461. fe = NULL;
  462. err("unknown tuner=%d", priv->tuner);
  463. }
  464. if (fe == NULL) {
  465. ret = -ENODEV;
  466. goto err;
  467. }
  468. return 0;
  469. err:
  470. deb_info("%s: failed=%d\n", __func__, ret);
  471. return ret;
  472. }
  473. static int rtl28xxu_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff)
  474. {
  475. int ret;
  476. u8 buf[2], gpio;
  477. deb_info("%s: onoff=%d\n", __func__, onoff);
  478. ret = rtl2831_rd_reg(adap->dev, SYS_GPIO_OUT_VAL, &gpio);
  479. if (ret)
  480. goto err;
  481. if (onoff) {
  482. buf[0] = 0x00;
  483. buf[1] = 0x00;
  484. gpio |= 0x04; /* LED on */
  485. } else {
  486. buf[0] = 0x10; /* stall EPA */
  487. buf[1] = 0x02; /* reset EPA */
  488. gpio &= (~0x04); /* LED off */
  489. }
  490. ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_VAL, gpio);
  491. if (ret)
  492. goto err;
  493. ret = rtl2831_wr_regs(adap->dev, USB_EPA_CTL, buf, 2);
  494. if (ret)
  495. goto err;
  496. return ret;
  497. err:
  498. deb_info("%s: failed=%d\n", __func__, ret);
  499. return ret;
  500. }
  501. static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
  502. {
  503. int ret;
  504. u8 gpio, sys0;
  505. deb_info("%s: onoff=%d\n", __func__, onoff);
  506. /* demod adc */
  507. ret = rtl2831_rd_reg(d, SYS_SYS0, &sys0);
  508. if (ret)
  509. goto err;
  510. /* tuner power, read GPIOs */
  511. ret = rtl2831_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
  512. if (ret)
  513. goto err;
  514. deb_info("%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
  515. if (onoff) {
  516. gpio |= 0x01; /* GPIO0 = 1 */
  517. gpio &= (~0x10); /* GPIO4 = 0 */
  518. sys0 = sys0 & 0x0f;
  519. sys0 |= 0xe0;
  520. } else {
  521. gpio &= (~0x01); /* GPIO0 = 0 */
  522. gpio |= 0x10; /* GPIO4 = 1 */
  523. sys0 = sys0 & (~0xc0);
  524. }
  525. deb_info("%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
  526. /* demod adc */
  527. ret = rtl2831_wr_reg(d, SYS_SYS0, sys0);
  528. if (ret)
  529. goto err;
  530. /* tuner power, write GPIOs */
  531. ret = rtl2831_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
  532. if (ret)
  533. goto err;
  534. return ret;
  535. err:
  536. deb_info("%s: failed=%d\n", __func__, ret);
  537. return ret;
  538. }
  539. static int rtl2831u_rc_query(struct dvb_usb_device *d)
  540. {
  541. int ret, i;
  542. struct rtl28xxu_priv *priv = d->priv;
  543. u8 buf[5];
  544. u32 rc_code;
  545. struct rtl28xxu_reg_val rc_nec_tab[] = {
  546. { 0x3033, 0x80 },
  547. { 0x3020, 0x43 },
  548. { 0x3021, 0x16 },
  549. { 0x3022, 0x16 },
  550. { 0x3023, 0x5a },
  551. { 0x3024, 0x2d },
  552. { 0x3025, 0x16 },
  553. { 0x3026, 0x01 },
  554. { 0x3028, 0xb0 },
  555. { 0x3029, 0x04 },
  556. { 0x302c, 0x88 },
  557. { 0x302e, 0x13 },
  558. { 0x3030, 0xdf },
  559. { 0x3031, 0x05 },
  560. };
  561. /* init remote controller */
  562. if (!priv->rc_active) {
  563. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  564. ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
  565. rc_nec_tab[i].val);
  566. if (ret)
  567. goto err;
  568. }
  569. priv->rc_active = true;
  570. }
  571. ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
  572. if (ret)
  573. goto err;
  574. if (buf[4] & 0x01) {
  575. if (buf[2] == (u8) ~buf[3]) {
  576. if (buf[0] == (u8) ~buf[1]) {
  577. /* NEC standard (16 bit) */
  578. rc_code = buf[0] << 8 | buf[2];
  579. } else {
  580. /* NEC extended (24 bit) */
  581. rc_code = buf[0] << 16 |
  582. buf[1] << 8 | buf[2];
  583. }
  584. } else {
  585. /* NEC full (32 bit) */
  586. rc_code = buf[0] << 24 | buf[1] << 16 |
  587. buf[2] << 8 | buf[3];
  588. }
  589. rc_keydown(d->rc_dev, rc_code, 0);
  590. ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
  591. if (ret)
  592. goto err;
  593. /* repeated intentionally to avoid extra keypress */
  594. ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
  595. if (ret)
  596. goto err;
  597. }
  598. return ret;
  599. err:
  600. deb_info("%s: failed=%d\n", __func__, ret);
  601. return ret;
  602. }
  603. static int rtl2832u_rc_query(struct dvb_usb_device *d)
  604. {
  605. int ret, i;
  606. struct rtl28xxu_priv *priv = d->priv;
  607. u8 buf[128];
  608. int len;
  609. struct rtl28xxu_reg_val rc_nec_tab[] = {
  610. { IR_RX_CTRL, 0x20 },
  611. { IR_RX_BUF_CTRL, 0x80 },
  612. { IR_RX_IF, 0xff },
  613. { IR_RX_IE, 0xff },
  614. { IR_MAX_DURATION0, 0xd0 },
  615. { IR_MAX_DURATION1, 0x07 },
  616. { IR_IDLE_LEN0, 0xc0 },
  617. { IR_IDLE_LEN1, 0x00 },
  618. { IR_GLITCH_LEN, 0x03 },
  619. { IR_RX_CLK, 0x09 },
  620. { IR_RX_CFG, 0x1c },
  621. { IR_MAX_H_TOL_LEN, 0x1e },
  622. { IR_MAX_L_TOL_LEN, 0x1e },
  623. { IR_RX_CTRL, 0x80 },
  624. };
  625. /* init remote controller */
  626. if (!priv->rc_active) {
  627. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  628. ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
  629. rc_nec_tab[i].val);
  630. if (ret)
  631. goto err;
  632. }
  633. priv->rc_active = true;
  634. }
  635. ret = rtl2831_rd_reg(d, IR_RX_IF, &buf[0]);
  636. if (ret)
  637. goto err;
  638. if (buf[0] != 0x83)
  639. goto exit;
  640. ret = rtl2831_rd_reg(d, IR_RX_BC, &buf[0]);
  641. if (ret)
  642. goto err;
  643. len = buf[0];
  644. ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
  645. /* TODO: pass raw IR to Kernel IR decoder */
  646. ret = rtl2831_wr_reg(d, IR_RX_IF, 0x03);
  647. ret = rtl2831_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
  648. ret = rtl2831_wr_reg(d, IR_RX_CTRL, 0x80);
  649. exit:
  650. return ret;
  651. err:
  652. deb_info("%s: failed=%d\n", __func__, ret);
  653. return ret;
  654. }
  655. enum rtl28xxu_usb_table_entry {
  656. RTL2831U_0BDA_2831,
  657. RTL2831U_14AA_0160,
  658. RTL2831U_14AA_0161,
  659. };
  660. static struct usb_device_id rtl28xxu_table[] = {
  661. /* RTL2831U */
  662. [RTL2831U_0BDA_2831] = {
  663. USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U)},
  664. [RTL2831U_14AA_0160] = {
  665. USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT)},
  666. [RTL2831U_14AA_0161] = {
  667. USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2)},
  668. /* RTL2832U */
  669. {} /* terminating entry */
  670. };
  671. MODULE_DEVICE_TABLE(usb, rtl28xxu_table);
  672. static struct dvb_usb_device_properties rtl28xxu_properties[] = {
  673. {
  674. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  675. .usb_ctrl = DEVICE_SPECIFIC,
  676. .no_reconnect = 1,
  677. .size_of_priv = sizeof(struct rtl28xxu_priv),
  678. .num_adapters = 1,
  679. .adapter = {
  680. {
  681. .num_frontends = 1,
  682. .fe = {
  683. {
  684. .frontend_attach = rtl2831u_frontend_attach,
  685. .tuner_attach = rtl2831u_tuner_attach,
  686. .streaming_ctrl = rtl28xxu_streaming_ctrl,
  687. .stream = {
  688. .type = USB_BULK,
  689. .count = 6,
  690. .endpoint = 0x81,
  691. .u = {
  692. .bulk = {
  693. .buffersize = 8*512,
  694. }
  695. }
  696. }
  697. }
  698. }
  699. }
  700. },
  701. .power_ctrl = rtl28xxu_power_ctrl,
  702. .rc.core = {
  703. .protocol = RC_TYPE_NEC,
  704. .module_name = "rtl28xxu",
  705. .rc_query = rtl2831u_rc_query,
  706. .rc_interval = 400,
  707. .allowed_protos = RC_TYPE_NEC,
  708. .rc_codes = RC_MAP_EMPTY,
  709. },
  710. .i2c_algo = &rtl28xxu_i2c_algo,
  711. .num_device_descs = 2,
  712. .devices = {
  713. {
  714. .name = "Realtek RTL2831U reference design",
  715. .warm_ids = {
  716. &rtl28xxu_table[RTL2831U_0BDA_2831],
  717. },
  718. },
  719. {
  720. .name = "Freecom USB2.0 DVB-T",
  721. .warm_ids = {
  722. &rtl28xxu_table[RTL2831U_14AA_0160],
  723. &rtl28xxu_table[RTL2831U_14AA_0161],
  724. },
  725. },
  726. }
  727. },
  728. {
  729. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  730. .usb_ctrl = DEVICE_SPECIFIC,
  731. .no_reconnect = 1,
  732. .size_of_priv = sizeof(struct rtl28xxu_priv),
  733. .num_adapters = 1,
  734. .adapter = {
  735. {
  736. .num_frontends = 1,
  737. .fe = {
  738. {
  739. .frontend_attach = rtl2832u_frontend_attach,
  740. .tuner_attach = rtl2832u_tuner_attach,
  741. .streaming_ctrl = rtl28xxu_streaming_ctrl,
  742. .stream = {
  743. .type = USB_BULK,
  744. .count = 6,
  745. .endpoint = 0x81,
  746. .u = {
  747. .bulk = {
  748. .buffersize = 8*512,
  749. }
  750. }
  751. }
  752. }
  753. }
  754. }
  755. },
  756. .power_ctrl = rtl28xxu_power_ctrl,
  757. .rc.core = {
  758. .protocol = RC_TYPE_NEC,
  759. .module_name = "rtl28xxu",
  760. .rc_query = rtl2832u_rc_query,
  761. .rc_interval = 400,
  762. .allowed_protos = RC_TYPE_NEC,
  763. .rc_codes = RC_MAP_EMPTY,
  764. },
  765. .i2c_algo = &rtl28xxu_i2c_algo,
  766. .num_device_descs = 0, /* disabled as no support for RTL2832 */
  767. .devices = {
  768. {
  769. .name = "Realtek RTL2832U reference design",
  770. },
  771. }
  772. },
  773. };
  774. static int rtl28xxu_probe(struct usb_interface *intf,
  775. const struct usb_device_id *id)
  776. {
  777. int ret, i;
  778. int properties_count = ARRAY_SIZE(rtl28xxu_properties);
  779. struct dvb_usb_device *d;
  780. deb_info("%s: interface=%d\n", __func__,
  781. intf->cur_altsetting->desc.bInterfaceNumber);
  782. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  783. return 0;
  784. for (i = 0; i < properties_count; i++) {
  785. ret = dvb_usb_device_init(intf, &rtl28xxu_properties[i],
  786. THIS_MODULE, &d, adapter_nr);
  787. if (ret == 0 || ret != -ENODEV)
  788. break;
  789. }
  790. if (ret)
  791. goto err;
  792. /* init USB endpoints */
  793. ret = rtl2831_wr_reg(d, USB_SYSCTL_0, 0x09);
  794. if (ret)
  795. goto err;
  796. ret = rtl2831_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
  797. if (ret)
  798. goto err;
  799. ret = rtl2831_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
  800. if (ret)
  801. goto err;
  802. return ret;
  803. err:
  804. deb_info("%s: failed=%d\n", __func__, ret);
  805. return ret;
  806. }
  807. static struct usb_driver rtl28xxu_driver = {
  808. .name = "dvb_usb_rtl28xxu",
  809. .probe = rtl28xxu_probe,
  810. .disconnect = dvb_usb_device_exit,
  811. .id_table = rtl28xxu_table,
  812. };
  813. /* module stuff */
  814. static int __init rtl28xxu_module_init(void)
  815. {
  816. int ret;
  817. deb_info("%s:\n", __func__);
  818. ret = usb_register(&rtl28xxu_driver);
  819. if (ret)
  820. err("usb_register failed=%d", ret);
  821. return ret;
  822. }
  823. static void __exit rtl28xxu_module_exit(void)
  824. {
  825. deb_info("%s:\n", __func__);
  826. /* deregister this driver from the USB subsystem */
  827. usb_deregister(&rtl28xxu_driver);
  828. }
  829. module_init(rtl28xxu_module_init);
  830. module_exit(rtl28xxu_module_exit);
  831. MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
  832. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  833. MODULE_LICENSE("GPL");