rtl28xxu.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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. struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf};
  327. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  328. /* enable GPIO3 and GPIO6 as output */
  329. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
  330. if (ret)
  331. goto err;
  332. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
  333. if (ret)
  334. goto err;
  335. /*
  336. * Probe used tuner. We need to know used tuner before demod attach
  337. * since there is some demod params needed to set according to tuner.
  338. */
  339. /* open demod I2C gate */
  340. ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
  341. if (ret)
  342. goto err;
  343. priv->tuner_name = "NONE";
  344. /* check FC0012 ID register; reg=00 val=a1 */
  345. ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
  346. if (ret == 0 && buf[0] == 0xa1) {
  347. priv->tuner = TUNER_RTL2832_FC0012;
  348. priv->tuner_name = "FC0012";
  349. goto found;
  350. }
  351. /* check FC0013 ID register; reg=00 val=a3 */
  352. ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
  353. if (ret == 0 && buf[0] == 0xa3) {
  354. priv->tuner = TUNER_RTL2832_FC0013;
  355. priv->tuner_name = "FC0013";
  356. goto found;
  357. }
  358. /* check MT2266 ID register; reg=00 val=85 */
  359. ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
  360. if (ret == 0 && buf[0] == 0x85) {
  361. priv->tuner = TUNER_RTL2832_MT2266;
  362. priv->tuner_name = "MT2266";
  363. goto found;
  364. }
  365. /* check FC2580 ID register; reg=01 val=56 */
  366. ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
  367. if (ret == 0 && buf[0] == 0x56) {
  368. priv->tuner = TUNER_RTL2832_FC2580;
  369. priv->tuner_name = "FC2580";
  370. goto found;
  371. }
  372. /* check MT2063 ID register; reg=00 val=9e || 9c */
  373. ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
  374. if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
  375. priv->tuner = TUNER_RTL2832_MT2063;
  376. priv->tuner_name = "MT2063";
  377. goto found;
  378. }
  379. /* check MAX3543 ID register; reg=00 val=38 */
  380. ret = rtl28xxu_ctrl_msg(d, &req_max3543);
  381. if (ret == 0 && buf[0] == 0x38) {
  382. priv->tuner = TUNER_RTL2832_MAX3543;
  383. priv->tuner_name = "MAX3543";
  384. goto found;
  385. }
  386. /* check TUA9001 ID register; reg=7e val=2328 */
  387. ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
  388. if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
  389. priv->tuner = TUNER_RTL2832_TUA9001;
  390. priv->tuner_name = "TUA9001";
  391. goto found;
  392. }
  393. /* check MXL5007R ID register; reg=d9 val=14 */
  394. ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
  395. if (ret == 0 && buf[0] == 0x14) {
  396. priv->tuner = TUNER_RTL2832_MXL5007T;
  397. priv->tuner_name = "MXL5007T";
  398. goto found;
  399. }
  400. /* check E4000 ID register; reg=02 val=40 */
  401. ret = rtl28xxu_ctrl_msg(d, &req_e4000);
  402. if (ret == 0 && buf[0] == 0x40) {
  403. priv->tuner = TUNER_RTL2832_E4000;
  404. priv->tuner_name = "E4000";
  405. goto found;
  406. }
  407. /* check TDA18272 ID register; reg=00 val=c760 */
  408. ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
  409. if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
  410. priv->tuner = TUNER_RTL2832_TDA18272;
  411. priv->tuner_name = "TDA18272";
  412. goto found;
  413. }
  414. /* check R820T ID register; reg=00 val=69 */
  415. ret = rtl28xxu_ctrl_msg(d, &req_r820t);
  416. if (ret == 0 && buf[0] == 0x69) {
  417. priv->tuner = TUNER_RTL2832_R820T;
  418. priv->tuner_name = "R820T";
  419. goto found;
  420. }
  421. /* check R828D ID register; reg=00 val=69 */
  422. ret = rtl28xxu_ctrl_msg(d, &req_r828d);
  423. if (ret == 0 && buf[0] == 0x69) {
  424. priv->tuner = TUNER_RTL2832_R828D;
  425. priv->tuner_name = "R828D";
  426. goto found;
  427. }
  428. found:
  429. dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
  430. /* close demod I2C gate */
  431. ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
  432. if (ret < 0)
  433. goto err;
  434. return 0;
  435. err:
  436. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  437. return ret;
  438. }
  439. static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
  440. .i2c_addr = 0x10, /* 0x20 */
  441. .xtal = 28800000,
  442. .ts_mode = 0,
  443. .spec_inv = 1,
  444. .vtop = 0x20,
  445. .krf = 0x04,
  446. .agc_targ_val = 0x2d,
  447. };
  448. static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
  449. .i2c_addr = 0x10, /* 0x20 */
  450. .xtal = 28800000,
  451. .ts_mode = 0,
  452. .spec_inv = 1,
  453. .vtop = 0x20,
  454. .krf = 0x04,
  455. .agc_targ_val = 0x2d,
  456. };
  457. static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
  458. .i2c_addr = 0x10, /* 0x20 */
  459. .xtal = 28800000,
  460. .ts_mode = 0,
  461. .spec_inv = 0,
  462. .vtop = 0x3f,
  463. .krf = 0x04,
  464. .agc_targ_val = 0x3e,
  465. };
  466. static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
  467. {
  468. struct dvb_usb_device *d = adap_to_d(adap);
  469. struct rtl28xxu_priv *priv = d_to_priv(d);
  470. struct rtl2830_config *rtl2830_config;
  471. int ret;
  472. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  473. switch (priv->tuner) {
  474. case TUNER_RTL2830_QT1010:
  475. rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
  476. break;
  477. case TUNER_RTL2830_MT2060:
  478. rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
  479. break;
  480. case TUNER_RTL2830_MXL5005S:
  481. rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
  482. break;
  483. default:
  484. dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
  485. KBUILD_MODNAME, priv->tuner_name);
  486. ret = -ENODEV;
  487. goto err;
  488. }
  489. /* attach demodulator */
  490. adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap);
  491. if (!adap->fe[0]) {
  492. ret = -ENODEV;
  493. goto err;
  494. }
  495. return 0;
  496. err:
  497. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  498. return ret;
  499. }
  500. static struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
  501. .i2c_addr = 0x10, /* 0x20 */
  502. .xtal = 28800000,
  503. .if_dvbt = 0,
  504. .tuner = TUNER_RTL2832_FC0012
  505. };
  506. static struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
  507. .i2c_addr = 0x10, /* 0x20 */
  508. .xtal = 28800000,
  509. .if_dvbt = 0,
  510. .tuner = TUNER_RTL2832_FC0013
  511. };
  512. static struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
  513. .i2c_addr = 0x10, /* 0x20 */
  514. .xtal = 28800000,
  515. .tuner = TUNER_RTL2832_TUA9001,
  516. };
  517. static struct rtl2832_config rtl28xxu_rtl2832_e4000_config = {
  518. .i2c_addr = 0x10, /* 0x20 */
  519. .xtal = 28800000,
  520. .tuner = TUNER_RTL2832_E4000,
  521. };
  522. static struct rtl2832_config rtl28xxu_rtl2832_r820t_config = {
  523. .i2c_addr = 0x10,
  524. .xtal = 28800000,
  525. .tuner = TUNER_RTL2832_R820T,
  526. };
  527. static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
  528. int cmd, int arg)
  529. {
  530. int ret;
  531. u8 val;
  532. dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
  533. switch (cmd) {
  534. case FC_FE_CALLBACK_VHF_ENABLE:
  535. /* set output values */
  536. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
  537. if (ret)
  538. goto err;
  539. if (arg)
  540. val &= 0xbf; /* set GPIO6 low */
  541. else
  542. val |= 0x40; /* set GPIO6 high */
  543. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
  544. if (ret)
  545. goto err;
  546. break;
  547. default:
  548. ret = -EINVAL;
  549. goto err;
  550. }
  551. return 0;
  552. err:
  553. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  554. return ret;
  555. }
  556. static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
  557. int cmd, int arg)
  558. {
  559. int ret;
  560. u8 val;
  561. dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
  562. /*
  563. * CEN always enabled by hardware wiring
  564. * RESETN GPIO4
  565. * RXEN GPIO1
  566. */
  567. switch (cmd) {
  568. case TUA9001_CMD_RESETN:
  569. if (arg)
  570. val = (1 << 4);
  571. else
  572. val = (0 << 4);
  573. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
  574. if (ret)
  575. goto err;
  576. break;
  577. case TUA9001_CMD_RXEN:
  578. if (arg)
  579. val = (1 << 1);
  580. else
  581. val = (0 << 1);
  582. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
  583. if (ret)
  584. goto err;
  585. break;
  586. }
  587. return 0;
  588. err:
  589. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  590. return ret;
  591. }
  592. static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
  593. {
  594. struct rtl28xxu_priv *priv = d->priv;
  595. switch (priv->tuner) {
  596. case TUNER_RTL2832_FC0012:
  597. return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
  598. case TUNER_RTL2832_TUA9001:
  599. return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
  600. default:
  601. break;
  602. }
  603. return 0;
  604. }
  605. static int rtl2832u_frontend_callback(void *adapter_priv, int component,
  606. int cmd, int arg)
  607. {
  608. struct i2c_adapter *adap = adapter_priv;
  609. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  610. dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
  611. __func__, component, cmd, arg);
  612. switch (component) {
  613. case DVB_FRONTEND_COMPONENT_TUNER:
  614. return rtl2832u_tuner_callback(d, cmd, arg);
  615. default:
  616. break;
  617. }
  618. return 0;
  619. }
  620. static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
  621. {
  622. int ret;
  623. struct dvb_usb_device *d = adap_to_d(adap);
  624. struct rtl28xxu_priv *priv = d_to_priv(d);
  625. struct rtl2832_config *rtl2832_config;
  626. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  627. switch (priv->tuner) {
  628. case TUNER_RTL2832_FC0012:
  629. rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
  630. break;
  631. case TUNER_RTL2832_FC0013:
  632. rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
  633. break;
  634. case TUNER_RTL2832_FC2580:
  635. /* FIXME: do not abuse fc0012 settings */
  636. rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
  637. break;
  638. case TUNER_RTL2832_TUA9001:
  639. rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
  640. break;
  641. case TUNER_RTL2832_E4000:
  642. rtl2832_config = &rtl28xxu_rtl2832_e4000_config;
  643. break;
  644. case TUNER_RTL2832_R820T:
  645. case TUNER_RTL2832_R828D:
  646. rtl2832_config = &rtl28xxu_rtl2832_r820t_config;
  647. break;
  648. default:
  649. dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
  650. KBUILD_MODNAME, priv->tuner_name);
  651. ret = -ENODEV;
  652. goto err;
  653. }
  654. /* attach demodulator */
  655. adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
  656. if (!adap->fe[0]) {
  657. ret = -ENODEV;
  658. goto err;
  659. }
  660. /* set fe callback */
  661. adap->fe[0]->callback = rtl2832u_frontend_callback;
  662. return 0;
  663. err:
  664. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  665. return ret;
  666. }
  667. static struct qt1010_config rtl28xxu_qt1010_config = {
  668. .i2c_address = 0x62, /* 0xc4 */
  669. };
  670. static struct mt2060_config rtl28xxu_mt2060_config = {
  671. .i2c_address = 0x60, /* 0xc0 */
  672. .clock_out = 0,
  673. };
  674. static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
  675. .i2c_address = 0x63, /* 0xc6 */
  676. .if_freq = IF_FREQ_4570000HZ,
  677. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  678. .agc_mode = MXL_SINGLE_AGC,
  679. .tracking_filter = MXL_TF_C_H,
  680. .rssi_enable = MXL_RSSI_ENABLE,
  681. .cap_select = MXL_CAP_SEL_ENABLE,
  682. .div_out = MXL_DIV_OUT_4,
  683. .clock_out = MXL_CLOCK_OUT_DISABLE,
  684. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  685. .top = MXL5005S_TOP_25P2,
  686. .mod_mode = MXL_DIGITAL_MODE,
  687. .if_mode = MXL_ZERO_IF,
  688. .AgcMasterByte = 0x00,
  689. };
  690. static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
  691. {
  692. int ret;
  693. struct dvb_usb_device *d = adap_to_d(adap);
  694. struct rtl28xxu_priv *priv = d_to_priv(d);
  695. struct i2c_adapter *rtl2830_tuner_i2c;
  696. struct dvb_frontend *fe;
  697. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  698. /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
  699. rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
  700. switch (priv->tuner) {
  701. case TUNER_RTL2830_QT1010:
  702. fe = dvb_attach(qt1010_attach, adap->fe[0],
  703. rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
  704. break;
  705. case TUNER_RTL2830_MT2060:
  706. fe = dvb_attach(mt2060_attach, adap->fe[0],
  707. rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
  708. 1220);
  709. break;
  710. case TUNER_RTL2830_MXL5005S:
  711. fe = dvb_attach(mxl5005s_attach, adap->fe[0],
  712. rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
  713. break;
  714. default:
  715. fe = NULL;
  716. dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
  717. priv->tuner);
  718. }
  719. if (fe == NULL) {
  720. ret = -ENODEV;
  721. goto err;
  722. }
  723. return 0;
  724. err:
  725. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  726. return ret;
  727. }
  728. static const struct e4000_config rtl2832u_e4000_config = {
  729. .i2c_addr = 0x64,
  730. .clock = 28800000,
  731. };
  732. static const struct fc2580_config rtl2832u_fc2580_config = {
  733. .i2c_addr = 0x56,
  734. .clock = 16384000,
  735. };
  736. static struct tua9001_config rtl2832u_tua9001_config = {
  737. .i2c_addr = 0x60,
  738. };
  739. static const struct fc0012_config rtl2832u_fc0012_config = {
  740. .i2c_address = 0x63, /* 0xc6 >> 1 */
  741. .xtal_freq = FC_XTAL_28_8_MHZ,
  742. };
  743. static const struct r820t_config rtl2832u_r820t_config = {
  744. .i2c_addr = 0x1a,
  745. .xtal = 28800000,
  746. .max_i2c_msg_len = 2,
  747. .rafael_chip = CHIP_R820T,
  748. };
  749. static const struct r820t_config rtl2832u_r828d_config = {
  750. .i2c_addr = 0x3a,
  751. .xtal = 16000000,
  752. .max_i2c_msg_len = 2,
  753. .rafael_chip = CHIP_R828D,
  754. };
  755. static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
  756. {
  757. int ret;
  758. struct dvb_usb_device *d = adap_to_d(adap);
  759. struct rtl28xxu_priv *priv = d_to_priv(d);
  760. struct dvb_frontend *fe;
  761. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  762. switch (priv->tuner) {
  763. case TUNER_RTL2832_FC0012:
  764. fe = dvb_attach(fc0012_attach, adap->fe[0],
  765. &d->i2c_adap, &rtl2832u_fc0012_config);
  766. /* since fc0012 includs reading the signal strength delegate
  767. * that to the tuner driver */
  768. adap->fe[0]->ops.read_signal_strength =
  769. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  770. return 0;
  771. break;
  772. case TUNER_RTL2832_FC0013:
  773. fe = dvb_attach(fc0013_attach, adap->fe[0],
  774. &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
  775. /* fc0013 also supports signal strength reading */
  776. adap->fe[0]->ops.read_signal_strength =
  777. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  778. return 0;
  779. case TUNER_RTL2832_E4000:
  780. fe = dvb_attach(e4000_attach, adap->fe[0], &d->i2c_adap,
  781. &rtl2832u_e4000_config);
  782. break;
  783. case TUNER_RTL2832_FC2580:
  784. fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
  785. &rtl2832u_fc2580_config);
  786. break;
  787. case TUNER_RTL2832_TUA9001:
  788. /* enable GPIO1 and GPIO4 as output */
  789. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
  790. if (ret)
  791. goto err;
  792. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
  793. if (ret)
  794. goto err;
  795. fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
  796. &rtl2832u_tua9001_config);
  797. break;
  798. case TUNER_RTL2832_R820T:
  799. fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
  800. &rtl2832u_r820t_config);
  801. /* Use tuner to get the signal strength */
  802. adap->fe[0]->ops.read_signal_strength =
  803. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  804. break;
  805. case TUNER_RTL2832_R828D:
  806. /* power off mn88472 demod on GPIO0 */
  807. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x01);
  808. if (ret)
  809. goto err;
  810. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01);
  811. if (ret)
  812. goto err;
  813. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01);
  814. if (ret)
  815. goto err;
  816. fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
  817. &rtl2832u_r828d_config);
  818. /* Use tuner to get the signal strength */
  819. adap->fe[0]->ops.read_signal_strength =
  820. adap->fe[0]->ops.tuner_ops.get_rf_strength;
  821. break;
  822. default:
  823. fe = NULL;
  824. dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
  825. priv->tuner);
  826. }
  827. if (fe == NULL) {
  828. ret = -ENODEV;
  829. goto err;
  830. }
  831. return 0;
  832. err:
  833. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  834. return ret;
  835. }
  836. static int rtl28xxu_init(struct dvb_usb_device *d)
  837. {
  838. int ret;
  839. u8 val;
  840. dev_dbg(&d->udev->dev, "%s:\n", __func__);
  841. /* init USB endpoints */
  842. ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
  843. if (ret)
  844. goto err;
  845. /* enable DMA and Full Packet Mode*/
  846. val |= 0x09;
  847. ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
  848. if (ret)
  849. goto err;
  850. /* set EPA maximum packet size to 0x0200 */
  851. ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
  852. if (ret)
  853. goto err;
  854. /* change EPA FIFO length */
  855. ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
  856. if (ret)
  857. goto err;
  858. return ret;
  859. err:
  860. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  861. return ret;
  862. }
  863. static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
  864. {
  865. int ret;
  866. u8 gpio, sys0, epa_ctl[2];
  867. dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
  868. /* demod adc */
  869. ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
  870. if (ret)
  871. goto err;
  872. /* tuner power, read GPIOs */
  873. ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
  874. if (ret)
  875. goto err;
  876. dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
  877. sys0, gpio);
  878. if (onoff) {
  879. gpio |= 0x01; /* GPIO0 = 1 */
  880. gpio &= (~0x10); /* GPIO4 = 0 */
  881. gpio |= 0x04; /* GPIO2 = 1, LED on */
  882. sys0 = sys0 & 0x0f;
  883. sys0 |= 0xe0;
  884. epa_ctl[0] = 0x00; /* clear stall */
  885. epa_ctl[1] = 0x00; /* clear reset */
  886. } else {
  887. gpio &= (~0x01); /* GPIO0 = 0 */
  888. gpio |= 0x10; /* GPIO4 = 1 */
  889. gpio &= (~0x04); /* GPIO2 = 1, LED off */
  890. sys0 = sys0 & (~0xc0);
  891. epa_ctl[0] = 0x10; /* set stall */
  892. epa_ctl[1] = 0x02; /* set reset */
  893. }
  894. dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
  895. sys0, gpio);
  896. /* demod adc */
  897. ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
  898. if (ret)
  899. goto err;
  900. /* tuner power, write GPIOs */
  901. ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
  902. if (ret)
  903. goto err;
  904. /* streaming EP: stall & reset */
  905. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
  906. if (ret)
  907. goto err;
  908. if (onoff)
  909. usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
  910. return ret;
  911. err:
  912. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  913. return ret;
  914. }
  915. static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
  916. {
  917. int ret;
  918. dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
  919. if (onoff) {
  920. /* GPIO3=1, GPIO4=0 */
  921. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
  922. if (ret)
  923. goto err;
  924. /* suspend? */
  925. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
  926. if (ret)
  927. goto err;
  928. /* enable PLL */
  929. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
  930. if (ret)
  931. goto err;
  932. /* disable reset */
  933. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
  934. if (ret)
  935. goto err;
  936. mdelay(5);
  937. /* enable ADC */
  938. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x48, 0x48);
  939. if (ret)
  940. goto err;
  941. /* streaming EP: clear stall & reset */
  942. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
  943. if (ret)
  944. goto err;
  945. ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
  946. if (ret)
  947. goto err;
  948. } else {
  949. /* GPIO4=1 */
  950. ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
  951. if (ret)
  952. goto err;
  953. /* disable ADC */
  954. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x48);
  955. if (ret)
  956. goto err;
  957. /* disable PLL */
  958. ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
  959. if (ret)
  960. goto err;
  961. /* streaming EP: set stall & reset */
  962. ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
  963. if (ret)
  964. goto err;
  965. }
  966. return ret;
  967. err:
  968. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  969. return ret;
  970. }
  971. #if IS_ENABLED(CONFIG_RC_CORE)
  972. static int rtl2831u_rc_query(struct dvb_usb_device *d)
  973. {
  974. int ret, i;
  975. struct rtl28xxu_priv *priv = d->priv;
  976. u8 buf[5];
  977. u32 rc_code;
  978. struct rtl28xxu_reg_val rc_nec_tab[] = {
  979. { 0x3033, 0x80 },
  980. { 0x3020, 0x43 },
  981. { 0x3021, 0x16 },
  982. { 0x3022, 0x16 },
  983. { 0x3023, 0x5a },
  984. { 0x3024, 0x2d },
  985. { 0x3025, 0x16 },
  986. { 0x3026, 0x01 },
  987. { 0x3028, 0xb0 },
  988. { 0x3029, 0x04 },
  989. { 0x302c, 0x88 },
  990. { 0x302e, 0x13 },
  991. { 0x3030, 0xdf },
  992. { 0x3031, 0x05 },
  993. };
  994. /* init remote controller */
  995. if (!priv->rc_active) {
  996. for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
  997. ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
  998. rc_nec_tab[i].val);
  999. if (ret)
  1000. goto err;
  1001. }
  1002. priv->rc_active = true;
  1003. }
  1004. ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
  1005. if (ret)
  1006. goto err;
  1007. if (buf[4] & 0x01) {
  1008. if (buf[2] == (u8) ~buf[3]) {
  1009. if (buf[0] == (u8) ~buf[1]) {
  1010. /* NEC standard (16 bit) */
  1011. rc_code = buf[0] << 8 | buf[2];
  1012. } else {
  1013. /* NEC extended (24 bit) */
  1014. rc_code = buf[0] << 16 |
  1015. buf[1] << 8 | buf[2];
  1016. }
  1017. } else {
  1018. /* NEC full (32 bit) */
  1019. rc_code = buf[0] << 24 | buf[1] << 16 |
  1020. buf[2] << 8 | buf[3];
  1021. }
  1022. rc_keydown(d->rc_dev, rc_code, 0);
  1023. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  1024. if (ret)
  1025. goto err;
  1026. /* repeated intentionally to avoid extra keypress */
  1027. ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
  1028. if (ret)
  1029. goto err;
  1030. }
  1031. return ret;
  1032. err:
  1033. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  1034. return ret;
  1035. }
  1036. static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
  1037. struct dvb_usb_rc *rc)
  1038. {
  1039. rc->map_name = RC_MAP_EMPTY;
  1040. rc->allowed_protos = RC_BIT_NEC;
  1041. rc->query = rtl2831u_rc_query;
  1042. rc->interval = 400;
  1043. return 0;
  1044. }
  1045. static int rtl2832u_rc_query(struct dvb_usb_device *d)
  1046. {
  1047. int ret, i, len;
  1048. struct rtl28xxu_priv *priv = d->priv;
  1049. struct ir_raw_event ev;
  1050. u8 buf[128];
  1051. static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
  1052. {IR_RX_IF, 0x03, 0xff},
  1053. {IR_RX_BUF_CTRL, 0x80, 0xff},
  1054. {IR_RX_CTRL, 0x80, 0xff},
  1055. };
  1056. /* init remote controller */
  1057. if (!priv->rc_active) {
  1058. static const struct rtl28xxu_reg_val_mask init_tab[] = {
  1059. {SYS_DEMOD_CTL1, 0x00, 0x04},
  1060. {SYS_DEMOD_CTL1, 0x00, 0x08},
  1061. {USB_CTRL, 0x20, 0x20},
  1062. {SYS_GPIO_DIR, 0x00, 0x08},
  1063. {SYS_GPIO_OUT_EN, 0x08, 0x08},
  1064. {SYS_GPIO_OUT_VAL, 0x08, 0x08},
  1065. {IR_MAX_DURATION0, 0xd0, 0xff},
  1066. {IR_MAX_DURATION1, 0x07, 0xff},
  1067. {IR_IDLE_LEN0, 0xc0, 0xff},
  1068. {IR_IDLE_LEN1, 0x00, 0xff},
  1069. {IR_GLITCH_LEN, 0x03, 0xff},
  1070. {IR_RX_CLK, 0x09, 0xff},
  1071. {IR_RX_CFG, 0x1c, 0xff},
  1072. {IR_MAX_H_TOL_LEN, 0x1e, 0xff},
  1073. {IR_MAX_L_TOL_LEN, 0x1e, 0xff},
  1074. {IR_RX_CTRL, 0x80, 0xff},
  1075. };
  1076. for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
  1077. ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg,
  1078. init_tab[i].val, init_tab[i].mask);
  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. /* read raw code from hw */
  1094. ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
  1095. if (ret)
  1096. goto err;
  1097. /* let hw receive new code */
  1098. for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
  1099. ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg,
  1100. refresh_tab[i].val, refresh_tab[i].mask);
  1101. if (ret)
  1102. goto err;
  1103. }
  1104. /* pass data to Kernel IR decoder */
  1105. init_ir_raw_event(&ev);
  1106. for (i = 0; i < len; i++) {
  1107. ev.pulse = buf[i] >> 7;
  1108. ev.duration = 50800 * (buf[i] & 0x7f);
  1109. ir_raw_event_store_with_filter(d->rc_dev, &ev);
  1110. }
  1111. /* 'flush' ir_raw_event_store_with_filter() */
  1112. ir_raw_event_set_idle(d->rc_dev, true);
  1113. ir_raw_event_handle(d->rc_dev);
  1114. exit:
  1115. return ret;
  1116. err:
  1117. dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
  1118. return ret;
  1119. }
  1120. static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
  1121. struct dvb_usb_rc *rc)
  1122. {
  1123. /* load empty to enable rc */
  1124. if (!rc->map_name)
  1125. rc->map_name = RC_MAP_EMPTY;
  1126. rc->allowed_protos = RC_BIT_ALL;
  1127. rc->driver_type = RC_DRIVER_IR_RAW;
  1128. rc->query = rtl2832u_rc_query;
  1129. rc->interval = 400;
  1130. return 0;
  1131. }
  1132. #else
  1133. #define rtl2831u_get_rc_config NULL
  1134. #define rtl2832u_get_rc_config NULL
  1135. #endif
  1136. static const struct dvb_usb_device_properties rtl2831u_props = {
  1137. .driver_name = KBUILD_MODNAME,
  1138. .owner = THIS_MODULE,
  1139. .adapter_nr = adapter_nr,
  1140. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1141. .power_ctrl = rtl2831u_power_ctrl,
  1142. .i2c_algo = &rtl28xxu_i2c_algo,
  1143. .read_config = rtl2831u_read_config,
  1144. .frontend_attach = rtl2831u_frontend_attach,
  1145. .tuner_attach = rtl2831u_tuner_attach,
  1146. .init = rtl28xxu_init,
  1147. .get_rc_config = rtl2831u_get_rc_config,
  1148. .num_adapters = 1,
  1149. .adapter = {
  1150. {
  1151. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1152. },
  1153. },
  1154. };
  1155. static const struct dvb_usb_device_properties rtl2832u_props = {
  1156. .driver_name = KBUILD_MODNAME,
  1157. .owner = THIS_MODULE,
  1158. .adapter_nr = adapter_nr,
  1159. .size_of_priv = sizeof(struct rtl28xxu_priv),
  1160. .power_ctrl = rtl2832u_power_ctrl,
  1161. .i2c_algo = &rtl28xxu_i2c_algo,
  1162. .read_config = rtl2832u_read_config,
  1163. .frontend_attach = rtl2832u_frontend_attach,
  1164. .tuner_attach = rtl2832u_tuner_attach,
  1165. .init = rtl28xxu_init,
  1166. .get_rc_config = rtl2832u_get_rc_config,
  1167. .num_adapters = 1,
  1168. .adapter = {
  1169. {
  1170. .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
  1171. },
  1172. },
  1173. };
  1174. static const struct usb_device_id rtl28xxu_id_table[] = {
  1175. { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
  1176. &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
  1177. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
  1178. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1179. { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
  1180. &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
  1181. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
  1182. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1183. { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
  1184. &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
  1185. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
  1186. &rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
  1187. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
  1188. &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
  1189. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
  1190. &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
  1191. { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
  1192. &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
  1193. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
  1194. &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
  1195. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
  1196. &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
  1197. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
  1198. &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
  1199. { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
  1200. &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
  1201. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
  1202. &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
  1203. { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
  1204. &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
  1205. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
  1206. &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
  1207. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
  1208. &rtl2832u_props, "GIGABYTE U7300", NULL) },
  1209. { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
  1210. &rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) },
  1211. { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
  1212. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  1213. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  1214. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  1215. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
  1216. &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
  1217. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
  1218. &rtl2832u_props, "Crypto ReDi PC 50 A", NULL) },
  1219. { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
  1220. &rtl2832u_props, "Astrometa DVB-T2", NULL) },
  1221. { }
  1222. };
  1223. MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
  1224. static struct usb_driver rtl28xxu_usb_driver = {
  1225. .name = KBUILD_MODNAME,
  1226. .id_table = rtl28xxu_id_table,
  1227. .probe = dvb_usbv2_probe,
  1228. .disconnect = dvb_usbv2_disconnect,
  1229. .suspend = dvb_usbv2_suspend,
  1230. .resume = dvb_usbv2_resume,
  1231. .reset_resume = dvb_usbv2_reset_resume,
  1232. .no_dynamic_id = 1,
  1233. .soft_unbind = 1,
  1234. };
  1235. module_usb_driver(rtl28xxu_usb_driver);
  1236. MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
  1237. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1238. MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
  1239. MODULE_LICENSE("GPL");