rtl28xxu.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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. * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include "rtl28xxu.h"
  23. #include "rtl2830.h"
  24. #include "rtl2832.h"
  25. #include "qt1010.h"
  26. #include "mt2060.h"
  27. #include "mxl5005s.h"
  28. #include "fc0012.h"
  29. #include "fc0013.h"
  30. #include "e4000.h"
  31. #include "fc2580.h"
  32. #include "tua9001.h"
  33. #include "r820t.h"
  34. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  35. static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
  36. {
  37. int ret;
  38. unsigned int pipe;
  39. u8 requesttype;
  40. u8 *buf;
  41. buf = kmalloc(req->size, GFP_KERNEL);
  42. if (!buf) {
  43. ret = -ENOMEM;
  44. goto err;
  45. }
  46. if (req->index & CMD_WR_FLAG) {
  47. /* write */
  48. memcpy(buf, req->data, req->size);
  49. requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
  50. pipe = usb_sndctrlpipe(d->udev, 0);
  51. } else {
  52. /* read */
  53. requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
  54. pipe = usb_rcvctrlpipe(d->udev, 0);
  55. }
  56. ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
  57. req->index, buf, req->size, 1000);
  58. dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
  59. req->index, buf, req->size);
  60. if (ret > 0)
  61. ret = 0;
  62. /* read request, copy returned data to return buf */
  63. if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
  64. memcpy(req->data, buf, req->size);
  65. kfree(buf);
  66. if (ret)
  67. goto err;
  68. return ret;
  69. err:
  70. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  71. return ret;
  72. }
  73. static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  74. {
  75. struct rtl28xxu_req req;
  76. if (reg < 0x3000)
  77. req.index = CMD_USB_WR;
  78. else if (reg < 0x4000)
  79. req.index = CMD_SYS_WR;
  80. else
  81. req.index = CMD_IR_WR;
  82. req.value = reg;
  83. req.size = len;
  84. req.data = val;
  85. return rtl28xxu_ctrl_msg(d, &req);
  86. }
  87. static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
  88. {
  89. struct rtl28xxu_req req;
  90. if (reg < 0x3000)
  91. req.index = CMD_USB_RD;
  92. else if (reg < 0x4000)
  93. req.index = CMD_SYS_RD;
  94. else
  95. req.index = CMD_IR_RD;
  96. req.value = reg;
  97. req.size = len;
  98. req.data = val;
  99. return rtl28xxu_ctrl_msg(d, &req);
  100. }
  101. static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
  102. {
  103. return rtl28xx_wr_regs(d, reg, &val, 1);
  104. }
  105. static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
  106. {
  107. return rtl2831_rd_regs(d, reg, val, 1);
  108. }
  109. static int rtl28xx_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
  110. u8 mask)
  111. {
  112. int ret;
  113. u8 tmp;
  114. /* no need for read if whole reg is written */
  115. if (mask != 0xff) {
  116. ret = rtl28xx_rd_reg(d, reg, &tmp);
  117. if (ret)
  118. return ret;
  119. val &= mask;
  120. tmp &= ~mask;
  121. val |= tmp;
  122. }
  123. return rtl28xx_wr_reg(d, reg, val);
  124. }
  125. /* I2C */
  126. static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  127. int num)
  128. {
  129. int ret;
  130. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  131. struct rtl28xxu_priv *priv = d->priv;
  132. struct rtl28xxu_req req;
  133. /*
  134. * It is not known which are real I2C bus xfer limits, but testing
  135. * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
  136. * TODO: find out RTL2832U lens
  137. */
  138. /*
  139. * I2C adapter logic looks rather complicated due to fact it handles
  140. * three different access methods. Those methods are;
  141. * 1) integrated demod access
  142. * 2) old I2C access
  143. * 3) new I2C access
  144. *
  145. * Used method is selected in order 1, 2, 3. Method 3 can handle all
  146. * requests but there is two reasons why not use it always;
  147. * 1) It is most expensive, usually two USB messages are needed
  148. * 2) At least RTL2831U does not support it
  149. *
  150. * Method 3 is needed in case of I2C write+read (typical register read)
  151. * where write is more than one byte.
  152. */
  153. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  154. return -EAGAIN;
  155. if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
  156. (msg[1].flags & I2C_M_RD)) {
  157. if (msg[0].len > 24 || msg[1].len > 24) {
  158. /* TODO: check msg[0].len max */
  159. ret = -EOPNOTSUPP;
  160. goto err_mutex_unlock;
  161. } else if (msg[0].addr == 0x10) {
  162. /* method 1 - integrated demod */
  163. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  164. req.index = CMD_DEMOD_RD | priv->page;
  165. req.size = msg[1].len;
  166. req.data = &msg[1].buf[0];
  167. ret = rtl28xxu_ctrl_msg(d, &req);
  168. } else if (msg[0].len < 2) {
  169. /* method 2 - old I2C */
  170. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  171. req.index = CMD_I2C_RD;
  172. req.size = msg[1].len;
  173. req.data = &msg[1].buf[0];
  174. ret = rtl28xxu_ctrl_msg(d, &req);
  175. } else {
  176. /* method 3 - new I2C */
  177. req.value = (msg[0].addr << 1);
  178. req.index = CMD_I2C_DA_WR;
  179. req.size = msg[0].len;
  180. req.data = msg[0].buf;
  181. ret = rtl28xxu_ctrl_msg(d, &req);
  182. if (ret)
  183. goto err_mutex_unlock;
  184. req.value = (msg[0].addr << 1);
  185. req.index = CMD_I2C_DA_RD;
  186. req.size = msg[1].len;
  187. req.data = msg[1].buf;
  188. ret = rtl28xxu_ctrl_msg(d, &req);
  189. }
  190. } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
  191. if (msg[0].len > 22) {
  192. /* TODO: check msg[0].len max */
  193. ret = -EOPNOTSUPP;
  194. goto err_mutex_unlock;
  195. } else if (msg[0].addr == 0x10) {
  196. /* method 1 - integrated demod */
  197. if (msg[0].buf[0] == 0x00) {
  198. /* save demod page for later demod access */
  199. priv->page = msg[0].buf[1];
  200. ret = 0;
  201. } else {
  202. req.value = (msg[0].buf[0] << 8) |
  203. (msg[0].addr << 1);
  204. req.index = CMD_DEMOD_WR | priv->page;
  205. req.size = msg[0].len-1;
  206. req.data = &msg[0].buf[1];
  207. ret = rtl28xxu_ctrl_msg(d, &req);
  208. }
  209. } else if (msg[0].len < 23) {
  210. /* method 2 - old I2C */
  211. req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
  212. req.index = CMD_I2C_WR;
  213. req.size = msg[0].len-1;
  214. req.data = &msg[0].buf[1];
  215. ret = rtl28xxu_ctrl_msg(d, &req);
  216. } else {
  217. /* method 3 - new I2C */
  218. req.value = (msg[0].addr << 1);
  219. req.index = CMD_I2C_DA_WR;
  220. req.size = msg[0].len;
  221. req.data = msg[0].buf;
  222. ret = rtl28xxu_ctrl_msg(d, &req);
  223. }
  224. } else {
  225. ret = -EINVAL;
  226. }
  227. err_mutex_unlock:
  228. mutex_unlock(&d->i2c_mutex);
  229. return ret ? ret : num;
  230. }
  231. static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
  232. {
  233. return I2C_FUNC_I2C;
  234. }
  235. static struct i2c_algorithm rtl28xxu_i2c_algo = {
  236. .master_xfer = rtl28xxu_i2c_xfer,
  237. .functionality = rtl28xxu_i2c_func,
  238. };
  239. static int rtl2831u_read_config(struct dvb_usb_device *d)
  240. {
  241. struct rtl28xxu_priv *priv = d_to_priv(d);
  242. int ret;
  243. u8 buf[1];
  244. /* open RTL2831U/RTL2830 I2C gate */
  245. struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
  246. /* tuner probes */
  247. struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
  248. struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
  249. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  250. /*
  251. * RTL2831U GPIOs
  252. * =========================================================
  253. * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
  254. * GPIO2 | LED | 0 off | 1 on |
  255. * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
  256. */
  257. /* GPIO direction */
  258. ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a);
  259. if (ret)
  260. goto err;
  261. /* enable as output GPIO0, GPIO2, GPIO4 */
  262. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
  263. if (ret)
  264. goto err;
  265. /*
  266. * Probe used tuner. We need to know used tuner before demod attach
  267. * since there is some demod params needed to set according to tuner.
  268. */
  269. /* demod needs some time to wake up */
  270. msleep(20);
  271. priv->tuner_name = "NONE";
  272. /* open demod I2C gate */
  273. ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
  274. if (ret)
  275. goto err;
  276. /* check QT1010 ID(?) register; reg=0f val=2c */
  277. ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
  278. if (ret == 0 && buf[0] == 0x2c) {
  279. priv->tuner = TUNER_RTL2830_QT1010;
  280. priv->tuner_name = "QT1010";
  281. goto found;
  282. }
  283. /* open demod I2C gate */
  284. ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
  285. if (ret)
  286. goto err;
  287. /* check MT2060 ID register; reg=00 val=63 */
  288. ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
  289. if (ret == 0 && buf[0] == 0x63) {
  290. priv->tuner = TUNER_RTL2830_MT2060;
  291. priv->tuner_name = "MT2060";
  292. goto found;
  293. }
  294. /* assume MXL5005S */
  295. priv->tuner = TUNER_RTL2830_MXL5005S;
  296. priv->tuner_name = "MXL5005S";
  297. goto found;
  298. found:
  299. dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
  300. return 0;
  301. err:
  302. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  303. return ret;
  304. }
  305. static int rtl2832u_read_config(struct dvb_usb_device *d)
  306. {
  307. struct rtl28xxu_priv *priv = d_to_priv(d);
  308. int ret;
  309. u8 buf[2];
  310. /* open RTL2832U/RTL2832 I2C gate */
  311. struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
  312. /* close RTL2832U/RTL2832 I2C gate */
  313. struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
  314. /* tuner probes */
  315. struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
  316. struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
  317. struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
  318. struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
  319. struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
  320. struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
  321. struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
  322. struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
  323. struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
  324. struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
  325. struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 5, buf};
  326. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  327. /* enable GPIO3 and GPIO6 as output */
  328. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
  329. if (ret)
  330. goto err;
  331. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
  332. if (ret)
  333. goto err;
  334. /*
  335. * Probe used tuner. We need to know used tuner before demod attach
  336. * since there is some demod params needed to set according to tuner.
  337. */
  338. /* open demod I2C gate */
  339. ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
  340. if (ret)
  341. goto err;
  342. priv->tuner_name = "NONE";
  343. /* check FC0012 ID register; reg=00 val=a1 */
  344. ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
  345. if (ret == 0 && buf[0] == 0xa1) {
  346. priv->tuner = TUNER_RTL2832_FC0012;
  347. priv->tuner_name = "FC0012";
  348. goto found;
  349. }
  350. /* check FC0013 ID register; reg=00 val=a3 */
  351. ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
  352. if (ret == 0 && buf[0] == 0xa3) {
  353. priv->tuner = TUNER_RTL2832_FC0013;
  354. priv->tuner_name = "FC0013";
  355. goto found;
  356. }
  357. /* check MT2266 ID register; reg=00 val=85 */
  358. ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
  359. if (ret == 0 && buf[0] == 0x85) {
  360. priv->tuner = TUNER_RTL2832_MT2266;
  361. priv->tuner_name = "MT2266";
  362. goto found;
  363. }
  364. /* check FC2580 ID register; reg=01 val=56 */
  365. ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
  366. if (ret == 0 && buf[0] == 0x56) {
  367. priv->tuner = TUNER_RTL2832_FC2580;
  368. priv->tuner_name = "FC2580";
  369. goto found;
  370. }
  371. /* check MT2063 ID register; reg=00 val=9e || 9c */
  372. ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
  373. if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
  374. priv->tuner = TUNER_RTL2832_MT2063;
  375. priv->tuner_name = "MT2063";
  376. goto found;
  377. }
  378. /* check MAX3543 ID register; reg=00 val=38 */
  379. ret = rtl28xxu_ctrl_msg(d, &req_max3543);
  380. if (ret == 0 && buf[0] == 0x38) {
  381. priv->tuner = TUNER_RTL2832_MAX3543;
  382. priv->tuner_name = "MAX3543";
  383. goto found;
  384. }
  385. /* check TUA9001 ID register; reg=7e val=2328 */
  386. ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
  387. if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
  388. priv->tuner = TUNER_RTL2832_TUA9001;
  389. priv->tuner_name = "TUA9001";
  390. goto found;
  391. }
  392. /* check MXL5007R ID register; reg=d9 val=14 */
  393. ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
  394. if (ret == 0 && buf[0] == 0x14) {
  395. priv->tuner = TUNER_RTL2832_MXL5007T;
  396. priv->tuner_name = "MXL5007T";
  397. goto found;
  398. }
  399. /* check E4000 ID register; reg=02 val=40 */
  400. ret = rtl28xxu_ctrl_msg(d, &req_e4000);
  401. if (ret == 0 && buf[0] == 0x40) {
  402. priv->tuner = TUNER_RTL2832_E4000;
  403. priv->tuner_name = "E4000";
  404. goto found;
  405. }
  406. /* check TDA18272 ID register; reg=00 val=c760 */
  407. ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
  408. if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
  409. priv->tuner = TUNER_RTL2832_TDA18272;
  410. priv->tuner_name = "TDA18272";
  411. goto found;
  412. }
  413. /* check R820T by reading tuner stats at I2C addr 0x1a */
  414. ret = rtl28xxu_ctrl_msg(d, &req_r820t);
  415. if (ret == 0) {
  416. priv->tuner = TUNER_RTL2832_R820T;
  417. priv->tuner_name = "R820T";
  418. goto found;
  419. }
  420. found:
  421. dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
  422. /* close demod I2C gate */
  423. ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
  424. if (ret < 0)
  425. goto err;
  426. return 0;
  427. err:
  428. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  429. return ret;
  430. }
  431. static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
  432. .i2c_addr = 0x10, /* 0x20 */
  433. .xtal = 28800000,
  434. .ts_mode = 0,
  435. .spec_inv = 1,
  436. .vtop = 0x20,
  437. .krf = 0x04,
  438. .agc_targ_val = 0x2d,
  439. };
  440. static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
  441. .i2c_addr = 0x10, /* 0x20 */
  442. .xtal = 28800000,
  443. .ts_mode = 0,
  444. .spec_inv = 1,
  445. .vtop = 0x20,
  446. .krf = 0x04,
  447. .agc_targ_val = 0x2d,
  448. };
  449. static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
  450. .i2c_addr = 0x10, /* 0x20 */
  451. .xtal = 28800000,
  452. .ts_mode = 0,
  453. .spec_inv = 0,
  454. .vtop = 0x3f,
  455. .krf = 0x04,
  456. .agc_targ_val = 0x3e,
  457. };
  458. static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
  459. {
  460. struct dvb_usb_device *d = adap_to_d(adap);
  461. struct rtl28xxu_priv *priv = d_to_priv(d);
  462. struct rtl2830_config *rtl2830_config;
  463. int ret;
  464. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  465. switch (priv->tuner) {
  466. case TUNER_RTL2830_QT1010:
  467. rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
  468. break;
  469. case TUNER_RTL2830_MT2060:
  470. rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
  471. break;
  472. case TUNER_RTL2830_MXL5005S:
  473. rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
  474. break;
  475. default:
  476. dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
  477. KBUILD_MODNAME, priv->tuner_name);
  478. ret = -ENODEV;
  479. goto err;
  480. }
  481. /* attach demodulator */
  482. adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap);
  483. if (!adap->fe[0]) {
  484. ret = -ENODEV;
  485. goto err;
  486. }
  487. return 0;
  488. err:
  489. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  490. return ret;
  491. }
  492. static struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
  493. .i2c_addr = 0x10, /* 0x20 */
  494. .xtal = 28800000,
  495. .if_dvbt = 0,
  496. .tuner = TUNER_RTL2832_FC0012
  497. };
  498. static struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
  499. .i2c_addr = 0x10, /* 0x20 */
  500. .xtal = 28800000,
  501. .if_dvbt = 0,
  502. .tuner = TUNER_RTL2832_FC0013
  503. };
  504. static struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
  505. .i2c_addr = 0x10, /* 0x20 */
  506. .xtal = 28800000,
  507. .tuner = TUNER_RTL2832_TUA9001,
  508. };
  509. static struct rtl2832_config rtl28xxu_rtl2832_e4000_config = {
  510. .i2c_addr = 0x10, /* 0x20 */
  511. .xtal = 28800000,
  512. .tuner = TUNER_RTL2832_E4000,
  513. };
  514. static struct rtl2832_config rtl28xxu_rtl2832_r820t_config = {
  515. .i2c_addr = 0x10,
  516. .xtal = 28800000,
  517. .tuner = TUNER_RTL2832_R820T,
  518. };
  519. static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
  520. int cmd, int arg)
  521. {
  522. int ret;
  523. u8 val;
  524. dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
  525. switch (cmd) {
  526. case FC_FE_CALLBACK_VHF_ENABLE:
  527. /* set output values */
  528. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  529. if (ret)
  530. goto err;
  531. if (arg)
  532. val &= 0xbf; /* set GPIO6 low */
  533. else
  534. val |= 0x40; /* set GPIO6 high */
  535. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  536. if (ret)
  537. goto err;
  538. break;
  539. default:
  540. ret = -EINVAL;
  541. goto err;
  542. }
  543. return 0;
  544. err:
  545. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  546. return ret;
  547. }
  548. static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
  549. int cmd, int arg)
  550. {
  551. int ret;
  552. u8 val;
  553. dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
  554. /*
  555. * CEN always enabled by hardware wiring
  556. * RESETN GPIO4
  557. * RXEN GPIO1
  558. */
  559. switch (cmd) {
  560. case TUA9001_CMD_RESETN:
  561. if (arg)
  562. val = (1 << 4);
  563. else
  564. val = (0 << 4);
  565. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
  566. if (ret)
  567. goto err;
  568. break;
  569. case TUA9001_CMD_RXEN:
  570. if (arg)
  571. val = (1 << 1);
  572. else
  573. val = (0 << 1);
  574. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
  575. if (ret)
  576. goto err;
  577. break;
  578. }
  579. return 0;
  580. err:
  581. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  582. return ret;
  583. }
  584. static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
  585. {
  586. struct rtl28xxu_priv *priv = d->priv;
  587. switch (priv->tuner) {
  588. case TUNER_RTL2832_FC0012:
  589. return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
  590. case TUNER_RTL2832_TUA9001:
  591. return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
  592. default:
  593. break;
  594. }
  595. return 0;
  596. }
  597. static int rtl2832u_frontend_callback(void *adapter_priv, int component,
  598. int cmd, int arg)
  599. {
  600. struct i2c_adapter *adap = adapter_priv;
  601. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  602. dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
  603. __func__, component, cmd, arg);
  604. switch (component) {
  605. case DVB_FRONTEND_COMPONENT_TUNER:
  606. return rtl2832u_tuner_callback(d, cmd, arg);
  607. default:
  608. break;
  609. }
  610. return 0;
  611. }
  612. static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
  613. {
  614. int ret;
  615. struct dvb_usb_device *d = adap_to_d(adap);
  616. struct rtl28xxu_priv *priv = d_to_priv(d);
  617. struct rtl2832_config *rtl2832_config;
  618. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  619. switch (priv->tuner) {
  620. case TUNER_RTL2832_FC0012:
  621. rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
  622. break;
  623. case TUNER_RTL2832_FC0013:
  624. rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
  625. break;
  626. case TUNER_RTL2832_FC2580:
  627. /* FIXME: do not abuse fc0012 settings */
  628. rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
  629. break;
  630. case TUNER_RTL2832_TUA9001:
  631. rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
  632. break;
  633. case TUNER_RTL2832_E4000:
  634. rtl2832_config = &rtl28xxu_rtl2832_e4000_config;
  635. break;
  636. case TUNER_RTL2832_R820T:
  637. rtl2832_config = &rtl28xxu_rtl2832_r820t_config;
  638. break;
  639. default:
  640. dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
  641. KBUILD_MODNAME, priv->tuner_name);
  642. ret = -ENODEV;
  643. goto err;
  644. }
  645. /* attach demodulator */
  646. adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
  647. if (!adap->fe[0]) {
  648. ret = -ENODEV;
  649. goto err;
  650. }
  651. /* set fe callback */
  652. adap->fe[0]->callback = rtl2832u_frontend_callback;
  653. return 0;
  654. err:
  655. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  656. return ret;
  657. }
  658. static struct qt1010_config rtl28xxu_qt1010_config = {
  659. .i2c_address = 0x62, /* 0xc4 */
  660. };
  661. static struct mt2060_config rtl28xxu_mt2060_config = {
  662. .i2c_address = 0x60, /* 0xc0 */
  663. .clock_out = 0,
  664. };
  665. static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
  666. .i2c_address = 0x63, /* 0xc6 */
  667. .if_freq = IF_FREQ_4570000HZ,
  668. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  669. .agc_mode = MXL_SINGLE_AGC,
  670. .tracking_filter = MXL_TF_C_H,
  671. .rssi_enable = MXL_RSSI_ENABLE,
  672. .cap_select = MXL_CAP_SEL_ENABLE,
  673. .div_out = MXL_DIV_OUT_4,
  674. .clock_out = MXL_CLOCK_OUT_DISABLE,
  675. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  676. .top = MXL5005S_TOP_25P2,
  677. .mod_mode = MXL_DIGITAL_MODE,
  678. .if_mode = MXL_ZERO_IF,
  679. .AgcMasterByte = 0x00,
  680. };
  681. static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
  682. {
  683. int ret;
  684. struct dvb_usb_device *d = adap_to_d(adap);
  685. struct rtl28xxu_priv *priv = d_to_priv(d);
  686. struct i2c_adapter *rtl2830_tuner_i2c;
  687. struct dvb_frontend *fe;
  688. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  689. /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
  690. rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
  691. switch (priv->tuner) {
  692. case TUNER_RTL2830_QT1010:
  693. fe = dvb_attach(qt1010_attach, adap->fe[0],
  694. rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
  695. break;
  696. case TUNER_RTL2830_MT2060:
  697. fe = dvb_attach(mt2060_attach, adap->fe[0],
  698. rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
  699. 1220);
  700. break;
  701. case TUNER_RTL2830_MXL5005S:
  702. fe = dvb_attach(mxl5005s_attach, adap->fe[0],
  703. rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
  704. break;
  705. default:
  706. fe = NULL;
  707. dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
  708. priv->tuner);
  709. }
  710. if (fe == NULL) {
  711. ret = -ENODEV;
  712. goto err;
  713. }
  714. return 0;
  715. err:
  716. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  717. return ret;
  718. }
  719. static const struct e4000_config rtl2832u_e4000_config = {
  720. .i2c_addr = 0x64,
  721. .clock = 28800000,
  722. };
  723. static const struct fc2580_config rtl2832u_fc2580_config = {
  724. .i2c_addr = 0x56,
  725. .clock = 16384000,
  726. };
  727. static struct tua9001_config rtl2832u_tua9001_config = {
  728. .i2c_addr = 0x60,
  729. };
  730. static const struct fc0012_config rtl2832u_fc0012_config = {
  731. .i2c_address = 0x63, /* 0xc6 >> 1 */
  732. .xtal_freq = FC_XTAL_28_8_MHZ,
  733. };
  734. static const struct r820t_config rtl2832u_r820t_config = {
  735. .i2c_addr = 0x1a,
  736. .xtal = 28800000,
  737. .max_i2c_msg_len = 2,
  738. .rafael_chip = CHIP_R820T,
  739. };
  740. static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
  741. {
  742. int ret;
  743. struct dvb_usb_device *d = adap_to_d(adap);
  744. struct rtl28xxu_priv *priv = d_to_priv(d);
  745. struct dvb_frontend *fe;
  746. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  747. switch (priv->tuner) {
  748. case TUNER_RTL2832_FC0012:
  749. fe = dvb_attach(fc0012_attach, adap->fe[0],
  750. &d->i2c_adap, &rtl2832u_fc0012_config);
  751. /* since fc0012 includs reading the signal strength delegate
  752. * that to the tuner driver */
  753. adap->fe[0]->ops.read_signal_strength =
  754. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  755. return 0;
  756. break;
  757. case TUNER_RTL2832_FC0013:
  758. fe = dvb_attach(fc0013_attach, adap->fe[0],
  759. &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
  760. /* fc0013 also supports signal strength reading */
  761. adap->fe[0]->ops.read_signal_strength =
  762. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  763. return 0;
  764. case TUNER_RTL2832_E4000:
  765. fe = dvb_attach(e4000_attach, adap->fe[0], &d->i2c_adap,
  766. &rtl2832u_e4000_config);
  767. break;
  768. case TUNER_RTL2832_FC2580:
  769. fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
  770. &rtl2832u_fc2580_config);
  771. break;
  772. case TUNER_RTL2832_TUA9001:
  773. /* enable GPIO1 and GPIO4 as output */
  774. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
  775. if (ret)
  776. goto err;
  777. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
  778. if (ret)
  779. goto err;
  780. fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
  781. &rtl2832u_tua9001_config);
  782. break;
  783. case TUNER_RTL2832_R820T:
  784. fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
  785. &rtl2832u_r820t_config);
  786. /* Use tuner to get the signal strength */
  787. adap->fe[0]->ops.read_signal_strength =
  788. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  789. break;
  790. default:
  791. fe = NULL;
  792. dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
  793. priv->tuner);
  794. }
  795. if (fe == NULL) {
  796. ret = -ENODEV;
  797. goto err;
  798. }
  799. return 0;
  800. err:
  801. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  802. return ret;
  803. }
  804. static int rtl28xxu_init(struct dvb_usb_device *d)
  805. {
  806. int ret;
  807. u8 val;
  808. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  809. /* init USB endpoints */
  810. ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
  811. if (ret)
  812. goto err;
  813. /* enable DMA and Full Packet Mode*/
  814. val |= 0x09;
  815. ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
  816. if (ret)
  817. goto err;
  818. /* set EPA maximum packet size to 0x0200 */
  819. ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
  820. if (ret)
  821. goto err;
  822. /* change EPA FIFO length */
  823. ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
  824. if (ret)
  825. goto err;
  826. return ret;
  827. err:
  828. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  829. return ret;
  830. }
  831. static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
  832. {
  833. int ret;
  834. u8 gpio, sys0, epa_ctl[2];
  835. dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
  836. /* demod adc */
  837. ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
  838. if (ret)
  839. goto err;
  840. /* tuner power, read GPIOs */
  841. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
  842. if (ret)
  843. goto err;
  844. dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
  845. sys0, gpio);
  846. if (onoff) {
  847. gpio |= 0x01; /* GPIO0 = 1 */
  848. gpio &= (~0x10); /* GPIO4 = 0 */
  849. gpio |= 0x04; /* GPIO2 = 1, LED on */
  850. sys0 = sys0 & 0x0f;
  851. sys0 |= 0xe0;
  852. epa_ctl[0] = 0x00; /* clear stall */
  853. epa_ctl[1] = 0x00; /* clear reset */
  854. } else {
  855. gpio &= (~0x01); /* GPIO0 = 0 */
  856. gpio |= 0x10; /* GPIO4 = 1 */
  857. gpio &= (~0x04); /* GPIO2 = 1, LED off */
  858. sys0 = sys0 & (~0xc0);
  859. epa_ctl[0] = 0x10; /* set stall */
  860. epa_ctl[1] = 0x02; /* set reset */
  861. }
  862. dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
  863. sys0, gpio);
  864. /* demod adc */
  865. ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
  866. if (ret)
  867. goto err;
  868. /* tuner power, write GPIOs */
  869. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
  870. if (ret)
  871. goto err;
  872. /* streaming EP: stall & reset */
  873. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
  874. if (ret)
  875. goto err;
  876. if (onoff)
  877. usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
  878. return ret;
  879. err:
  880. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  881. return ret;
  882. }
  883. static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
  884. {
  885. int ret;
  886. u8 val;
  887. dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
  888. if (onoff) {
  889. /* set output values */
  890. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  891. if (ret)
  892. goto err;
  893. val |= 0x08;
  894. val &= 0xef;
  895. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  896. if (ret)
  897. goto err;
  898. /* demod_ctl_1 */
  899. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
  900. if (ret)
  901. goto err;
  902. val &= 0xef;
  903. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
  904. if (ret)
  905. goto err;
  906. /* demod control */
  907. /* PLL enable */
  908. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  909. if (ret)
  910. goto err;
  911. /* bit 7 to 1 */
  912. val |= 0x80;
  913. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  914. if (ret)
  915. goto err;
  916. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  917. if (ret)
  918. goto err;
  919. val |= 0x20;
  920. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  921. if (ret)
  922. goto err;
  923. mdelay(5);
  924. /*enable ADC_Q and ADC_I */
  925. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  926. if (ret)
  927. goto err;
  928. val |= 0x48;
  929. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  930. if (ret)
  931. goto err;
  932. /* streaming EP: clear stall & reset */
  933. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
  934. if (ret)
  935. goto err;
  936. ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
  937. if (ret)
  938. goto err;
  939. } else {
  940. /* demod_ctl_1 */
  941. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
  942. if (ret)
  943. goto err;
  944. val |= 0x0c;
  945. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
  946. if (ret)
  947. goto err;
  948. /* set output values */
  949. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  950. if (ret)
  951. goto err;
  952. val |= 0x10;
  953. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  954. if (ret)
  955. goto err;
  956. /* demod control */
  957. ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
  958. if (ret)
  959. goto err;
  960. val &= 0x37;
  961. ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
  962. if (ret)
  963. goto err;
  964. /* streaming EP: set stall & reset */
  965. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
  966. if (ret)
  967. goto err;
  968. }
  969. return ret;
  970. err:
  971. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  972. return ret;
  973. }
  974. #if IS_ENABLED(CONFIG_RC_CORE)
  975. static int rtl2831u_rc_query(struct dvb_usb_device *d)
  976. {
  977. int ret, i;
  978. struct rtl28xxu_priv *priv = d->priv;
  979. u8 buf[5];
  980. u32 rc_code;
  981. struct rtl28xxu_reg_val rc_nec_tab[] = {
  982. { 0x3033, 0x80 },
  983. { 0x3020, 0x43 },
  984. { 0x3021, 0x16 },
  985. { 0x3022, 0x16 },
  986. { 0x3023, 0x5a },
  987. { 0x3024, 0x2d },
  988. { 0x3025, 0x16 },
  989. { 0x3026, 0x01 },
  990. { 0x3028, 0xb0 },
  991. { 0x3029, 0x04 },
  992. { 0x302c, 0x88 },
  993. { 0x302e, 0x13 },
  994. { 0x3030, 0xdf },
  995. { 0x3031, 0x05 },
  996. };
  997. /* init remote controller */
  998. if (!priv->rc_active) {
  999. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  1000. ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
  1001. rc_nec_tab[i].val);
  1002. if (ret)
  1003. goto err;
  1004. }
  1005. priv->rc_active = true;
  1006. }
  1007. ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
  1008. if (ret)
  1009. goto err;
  1010. if (buf[4] & 0x01) {
  1011. if (buf[2] == (u8) ~buf[3]) {
  1012. if (buf[0] == (u8) ~buf[1]) {
  1013. /* NEC standard (16 bit) */
  1014. rc_code = buf[0] << 8 | buf[2];
  1015. } else {
  1016. /* NEC extended (24 bit) */
  1017. rc_code = buf[0] << 16 |
  1018. buf[1] << 8 | buf[2];
  1019. }
  1020. } else {
  1021. /* NEC full (32 bit) */
  1022. rc_code = buf[0] << 24 | buf[1] << 16 |
  1023. buf[2] << 8 | buf[3];
  1024. }
  1025. rc_keydown(d->rc_dev, rc_code, 0);
  1026. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  1027. if (ret)
  1028. goto err;
  1029. /* repeated intentionally to avoid extra keypress */
  1030. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  1031. if (ret)
  1032. goto err;
  1033. }
  1034. return ret;
  1035. err:
  1036. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  1037. return ret;
  1038. }
  1039. static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
  1040. struct dvb_usb_rc *rc)
  1041. {
  1042. rc->map_name = RC_MAP_EMPTY;
  1043. rc->allowed_protos = RC_BIT_NEC;
  1044. rc->query = rtl2831u_rc_query;
  1045. rc->interval = 400;
  1046. return 0;
  1047. }
  1048. #else
  1049. #define rtl2831u_get_rc_config NULL
  1050. #endif
  1051. #if IS_ENABLED(CONFIG_RC_CORE)
  1052. static int rtl2832u_rc_query(struct dvb_usb_device *d)
  1053. {
  1054. int ret, i;
  1055. struct rtl28xxu_priv *priv = d->priv;
  1056. u8 buf[128];
  1057. int len;
  1058. struct rtl28xxu_reg_val rc_nec_tab[] = {
  1059. { IR_RX_CTRL, 0x20 },
  1060. { IR_RX_BUF_CTRL, 0x80 },
  1061. { IR_RX_IF, 0xff },
  1062. { IR_RX_IE, 0xff },
  1063. { IR_MAX_DURATION0, 0xd0 },
  1064. { IR_MAX_DURATION1, 0x07 },
  1065. { IR_IDLE_LEN0, 0xc0 },
  1066. { IR_IDLE_LEN1, 0x00 },
  1067. { IR_GLITCH_LEN, 0x03 },
  1068. { IR_RX_CLK, 0x09 },
  1069. { IR_RX_CFG, 0x1c },
  1070. { IR_MAX_H_TOL_LEN, 0x1e },
  1071. { IR_MAX_L_TOL_LEN, 0x1e },
  1072. { IR_RX_CTRL, 0x80 },
  1073. };
  1074. /* init remote controller */
  1075. if (!priv->rc_active) {
  1076. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  1077. ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
  1078. rc_nec_tab[i].val);
  1079. if (ret)
  1080. goto err;
  1081. }
  1082. priv->rc_active = true;
  1083. }
  1084. ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
  1085. if (ret)
  1086. goto err;
  1087. if (buf[0] != 0x83)
  1088. goto exit;
  1089. ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
  1090. if (ret)
  1091. goto err;
  1092. len = buf[0];
  1093. ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
  1094. /* TODO: pass raw IR to Kernel IR decoder */
  1095. ret = rtl28xx_wr_reg(d, IR_RX_IF, 0x03);
  1096. ret = rtl28xx_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
  1097. ret = rtl28xx_wr_reg(d, IR_RX_CTRL, 0x80);
  1098. exit:
  1099. return ret;
  1100. err:
  1101. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  1102. return ret;
  1103. }
  1104. static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
  1105. struct dvb_usb_rc *rc)
  1106. {
  1107. rc->map_name = RC_MAP_EMPTY;
  1108. rc->allowed_protos = RC_BIT_NEC;
  1109. rc->query = rtl2832u_rc_query;
  1110. rc->interval = 400;
  1111. return 0;
  1112. }
  1113. #else
  1114. #define rtl2832u_get_rc_config NULL
  1115. #endif
  1116. static const struct dvb_usb_device_properties rtl2831u_props = {
  1117. .driver_name = KBUILD_MODNAME,
  1118. .owner = THIS_MODULE,
  1119. .adapter_nr = adapter_nr,
  1120. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1121. .power_ctrl = rtl2831u_power_ctrl,
  1122. .i2c_algo = &rtl28xxu_i2c_algo,
  1123. .read_config = rtl2831u_read_config,
  1124. .frontend_attach = rtl2831u_frontend_attach,
  1125. .tuner_attach = rtl2831u_tuner_attach,
  1126. .init = rtl28xxu_init,
  1127. .get_rc_config = rtl2831u_get_rc_config,
  1128. .num_adapters = 1,
  1129. .adapter = {
  1130. {
  1131. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1132. },
  1133. },
  1134. };
  1135. static const struct dvb_usb_device_properties rtl2832u_props = {
  1136. .driver_name = KBUILD_MODNAME,
  1137. .owner = THIS_MODULE,
  1138. .adapter_nr = adapter_nr,
  1139. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1140. .power_ctrl = rtl2832u_power_ctrl,
  1141. .i2c_algo = &rtl28xxu_i2c_algo,
  1142. .read_config = rtl2832u_read_config,
  1143. .frontend_attach = rtl2832u_frontend_attach,
  1144. .tuner_attach = rtl2832u_tuner_attach,
  1145. .init = rtl28xxu_init,
  1146. .get_rc_config = rtl2832u_get_rc_config,
  1147. .num_adapters = 1,
  1148. .adapter = {
  1149. {
  1150. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1151. },
  1152. },
  1153. };
  1154. static const struct usb_device_id rtl28xxu_id_table[] = {
  1155. { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
  1156. &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
  1157. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
  1158. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1159. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
  1160. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1161. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
  1162. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1163. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
  1164. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1165. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
  1166. &rtl2832u_props, "TerraTec Cinergy T Stick Black", NULL) },
  1167. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
  1168. &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
  1169. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
  1170. &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
  1171. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
  1172. &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
  1173. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
  1174. &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
  1175. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
  1176. &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
  1177. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
  1178. &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
  1179. { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
  1180. &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
  1181. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
  1182. &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
  1183. { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
  1184. &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
  1185. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
  1186. &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
  1187. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
  1188. &rtl2832u_props, "GIGABYTE U7300", NULL) },
  1189. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
  1190. &rtl2832u_props, "Digivox Micro Hd", NULL) },
  1191. { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
  1192. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  1193. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  1194. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  1195. { }
  1196. };
  1197. MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
  1198. static struct usb_driver rtl28xxu_usb_driver = {
  1199. .name = KBUILD_MODNAME,
  1200. .id_table = rtl28xxu_id_table,
  1201. .probe = dvb_usbv2_probe,
  1202. .disconnect = dvb_usbv2_disconnect,
  1203. .suspend = dvb_usbv2_suspend,
  1204. .resume = dvb_usbv2_resume,
  1205. .reset_resume = dvb_usbv2_reset_resume,
  1206. .no_dynamic_id = 1,
  1207. .soft_unbind = 1,
  1208. };
  1209. module_usb_driver(rtl28xxu_usb_driver);
  1210. MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
  1211. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1212. MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
  1213. MODULE_LICENSE("GPL");