rtl28xxu.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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, 1, 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 ID register; reg=00 val=69 */
  414. ret = rtl28xxu_ctrl_msg(d, &req_r820t);
  415. if (ret == 0 && buf[0] == 0x69) {
  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. dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
  887. if (onoff) {
  888. /* GPIO3=1, GPIO4=0 */
  889. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
  890. if (ret)
  891. goto err;
  892. /* suspend? */
  893. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
  894. if (ret)
  895. goto err;
  896. /* enable PLL */
  897. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
  898. if (ret)
  899. goto err;
  900. /* disable reset */
  901. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
  902. if (ret)
  903. goto err;
  904. mdelay(5);
  905. /* enable ADC */
  906. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x48, 0x48);
  907. if (ret)
  908. goto err;
  909. /* streaming EP: clear stall & reset */
  910. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
  911. if (ret)
  912. goto err;
  913. ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
  914. if (ret)
  915. goto err;
  916. } else {
  917. /* GPIO4=1 */
  918. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
  919. if (ret)
  920. goto err;
  921. /* disable ADC */
  922. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x48);
  923. if (ret)
  924. goto err;
  925. /* disable PLL */
  926. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
  927. if (ret)
  928. goto err;
  929. /* streaming EP: set stall & reset */
  930. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
  931. if (ret)
  932. goto err;
  933. }
  934. return ret;
  935. err:
  936. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  937. return ret;
  938. }
  939. #if IS_ENABLED(CONFIG_RC_CORE)
  940. static int rtl2831u_rc_query(struct dvb_usb_device *d)
  941. {
  942. int ret, i;
  943. struct rtl28xxu_priv *priv = d->priv;
  944. u8 buf[5];
  945. u32 rc_code;
  946. struct rtl28xxu_reg_val rc_nec_tab[] = {
  947. { 0x3033, 0x80 },
  948. { 0x3020, 0x43 },
  949. { 0x3021, 0x16 },
  950. { 0x3022, 0x16 },
  951. { 0x3023, 0x5a },
  952. { 0x3024, 0x2d },
  953. { 0x3025, 0x16 },
  954. { 0x3026, 0x01 },
  955. { 0x3028, 0xb0 },
  956. { 0x3029, 0x04 },
  957. { 0x302c, 0x88 },
  958. { 0x302e, 0x13 },
  959. { 0x3030, 0xdf },
  960. { 0x3031, 0x05 },
  961. };
  962. /* init remote controller */
  963. if (!priv->rc_active) {
  964. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  965. ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
  966. rc_nec_tab[i].val);
  967. if (ret)
  968. goto err;
  969. }
  970. priv->rc_active = true;
  971. }
  972. ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
  973. if (ret)
  974. goto err;
  975. if (buf[4] & 0x01) {
  976. if (buf[2] == (u8) ~buf[3]) {
  977. if (buf[0] == (u8) ~buf[1]) {
  978. /* NEC standard (16 bit) */
  979. rc_code = buf[0] << 8 | buf[2];
  980. } else {
  981. /* NEC extended (24 bit) */
  982. rc_code = buf[0] << 16 |
  983. buf[1] << 8 | buf[2];
  984. }
  985. } else {
  986. /* NEC full (32 bit) */
  987. rc_code = buf[0] << 24 | buf[1] << 16 |
  988. buf[2] << 8 | buf[3];
  989. }
  990. rc_keydown(d->rc_dev, rc_code, 0);
  991. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  992. if (ret)
  993. goto err;
  994. /* repeated intentionally to avoid extra keypress */
  995. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  996. if (ret)
  997. goto err;
  998. }
  999. return ret;
  1000. err:
  1001. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  1002. return ret;
  1003. }
  1004. static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
  1005. struct dvb_usb_rc *rc)
  1006. {
  1007. rc->map_name = RC_MAP_EMPTY;
  1008. rc->allowed_protos = RC_BIT_NEC;
  1009. rc->query = rtl2831u_rc_query;
  1010. rc->interval = 400;
  1011. return 0;
  1012. }
  1013. static int rtl2832u_rc_query(struct dvb_usb_device *d)
  1014. {
  1015. int ret, i, len;
  1016. struct rtl28xxu_priv *priv = d->priv;
  1017. struct ir_raw_event ev;
  1018. u8 buf[128];
  1019. static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
  1020. {IR_RX_IF, 0x03, 0xff},
  1021. {IR_RX_BUF_CTRL, 0x80, 0xff},
  1022. {IR_RX_CTRL, 0x80, 0xff},
  1023. };
  1024. /* init remote controller */
  1025. if (!priv->rc_active) {
  1026. static const struct rtl28xxu_reg_val_mask init_tab[] = {
  1027. {SYS_DEMOD_CTL1, 0x00, 0x04},
  1028. {SYS_DEMOD_CTL1, 0x00, 0x08},
  1029. {USB_CTRL, 0x20, 0x20},
  1030. {SYS_GPIO_DIR, 0x00, 0x08},
  1031. {SYS_GPIO_OUT_EN, 0x08, 0x08},
  1032. {SYS_GPIO_OUT_VAL, 0x08, 0x08},
  1033. {IR_MAX_DURATION0, 0xd0, 0xff},
  1034. {IR_MAX_DURATION1, 0x07, 0xff},
  1035. {IR_IDLE_LEN0, 0xc0, 0xff},
  1036. {IR_IDLE_LEN1, 0x00, 0xff},
  1037. {IR_GLITCH_LEN, 0x03, 0xff},
  1038. {IR_RX_CLK, 0x09, 0xff},
  1039. {IR_RX_CFG, 0x1c, 0xff},
  1040. {IR_MAX_H_TOL_LEN, 0x1e, 0xff},
  1041. {IR_MAX_L_TOL_LEN, 0x1e, 0xff},
  1042. {IR_RX_CTRL, 0x80, 0xff},
  1043. };
  1044. for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
  1045. ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg,
  1046. init_tab[i].val, init_tab[i].mask);
  1047. if (ret)
  1048. goto err;
  1049. }
  1050. priv->rc_active = true;
  1051. }
  1052. ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
  1053. if (ret)
  1054. goto err;
  1055. if (buf[0] != 0x83)
  1056. goto exit;
  1057. ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
  1058. if (ret)
  1059. goto err;
  1060. len = buf[0];
  1061. /* read raw code from hw */
  1062. ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
  1063. if (ret)
  1064. goto err;
  1065. /* let hw receive new code */
  1066. for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
  1067. ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg,
  1068. refresh_tab[i].val, refresh_tab[i].mask);
  1069. if (ret)
  1070. goto err;
  1071. }
  1072. /* pass data to Kernel IR decoder */
  1073. init_ir_raw_event(&ev);
  1074. for (i = 0; i < len; i++) {
  1075. ev.pulse = buf[i] >> 7;
  1076. ev.duration = 50800 * (buf[i] & 0x7f);
  1077. ir_raw_event_store_with_filter(d->rc_dev, &ev);
  1078. }
  1079. /* 'flush' ir_raw_event_store_with_filter() */
  1080. ir_raw_event_set_idle(d->rc_dev, true);
  1081. ir_raw_event_handle(d->rc_dev);
  1082. exit:
  1083. return ret;
  1084. err:
  1085. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  1086. return ret;
  1087. }
  1088. static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
  1089. struct dvb_usb_rc *rc)
  1090. {
  1091. /* load empty to enable rc */
  1092. if (!rc->map_name)
  1093. rc->map_name = RC_MAP_EMPTY;
  1094. rc->allowed_protos = RC_BIT_ALL;
  1095. rc->driver_type = RC_DRIVER_IR_RAW;
  1096. rc->query = rtl2832u_rc_query;
  1097. rc->interval = 400;
  1098. return 0;
  1099. }
  1100. #else
  1101. #define rtl2831u_get_rc_config NULL
  1102. #define rtl2832u_get_rc_config NULL
  1103. #endif
  1104. static const struct dvb_usb_device_properties rtl2831u_props = {
  1105. .driver_name = KBUILD_MODNAME,
  1106. .owner = THIS_MODULE,
  1107. .adapter_nr = adapter_nr,
  1108. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1109. .power_ctrl = rtl2831u_power_ctrl,
  1110. .i2c_algo = &rtl28xxu_i2c_algo,
  1111. .read_config = rtl2831u_read_config,
  1112. .frontend_attach = rtl2831u_frontend_attach,
  1113. .tuner_attach = rtl2831u_tuner_attach,
  1114. .init = rtl28xxu_init,
  1115. .get_rc_config = rtl2831u_get_rc_config,
  1116. .num_adapters = 1,
  1117. .adapter = {
  1118. {
  1119. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1120. },
  1121. },
  1122. };
  1123. static const struct dvb_usb_device_properties rtl2832u_props = {
  1124. .driver_name = KBUILD_MODNAME,
  1125. .owner = THIS_MODULE,
  1126. .adapter_nr = adapter_nr,
  1127. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1128. .power_ctrl = rtl2832u_power_ctrl,
  1129. .i2c_algo = &rtl28xxu_i2c_algo,
  1130. .read_config = rtl2832u_read_config,
  1131. .frontend_attach = rtl2832u_frontend_attach,
  1132. .tuner_attach = rtl2832u_tuner_attach,
  1133. .init = rtl28xxu_init,
  1134. .get_rc_config = rtl2832u_get_rc_config,
  1135. .num_adapters = 1,
  1136. .adapter = {
  1137. {
  1138. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1139. },
  1140. },
  1141. };
  1142. static const struct usb_device_id rtl28xxu_id_table[] = {
  1143. { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
  1144. &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
  1145. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
  1146. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1147. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
  1148. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1149. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
  1150. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1151. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
  1152. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1153. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
  1154. &rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
  1155. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
  1156. &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
  1157. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
  1158. &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
  1159. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
  1160. &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
  1161. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
  1162. &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
  1163. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
  1164. &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
  1165. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
  1166. &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
  1167. { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
  1168. &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
  1169. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
  1170. &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
  1171. { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
  1172. &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
  1173. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
  1174. &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
  1175. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
  1176. &rtl2832u_props, "GIGABYTE U7300", NULL) },
  1177. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
  1178. &rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) },
  1179. { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
  1180. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  1181. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  1182. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  1183. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
  1184. &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
  1185. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
  1186. &rtl2832u_props, "Crypto ReDi PC 50 A", NULL) },
  1187. { }
  1188. };
  1189. MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
  1190. static struct usb_driver rtl28xxu_usb_driver = {
  1191. .name = KBUILD_MODNAME,
  1192. .id_table = rtl28xxu_id_table,
  1193. .probe = dvb_usbv2_probe,
  1194. .disconnect = dvb_usbv2_disconnect,
  1195. .suspend = dvb_usbv2_suspend,
  1196. .resume = dvb_usbv2_resume,
  1197. .reset_resume = dvb_usbv2_reset_resume,
  1198. .no_dynamic_id = 1,
  1199. .soft_unbind = 1,
  1200. };
  1201. module_usb_driver(rtl28xxu_usb_driver);
  1202. MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
  1203. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1204. MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
  1205. MODULE_LICENSE("GPL");