rtl28xxu.c 39 KB

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