core.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /*
  2. * core.c - ChipIdea USB IP core family device controller
  3. *
  4. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Author: David Lopo
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * Description: ChipIdea USB IP core family device controller
  14. *
  15. * This driver is composed of several blocks:
  16. * - HW: hardware interface
  17. * - DBG: debug facilities (optional)
  18. * - UTIL: utilities
  19. * - ISR: interrupts handling
  20. * - ENDPT: endpoint operations (Gadget API)
  21. * - GADGET: gadget operations (Gadget API)
  22. * - BUS: bus glue code, bus abstraction layer
  23. *
  24. * Compile Options
  25. * - STALL_IN: non-empty bulk-in pipes cannot be halted
  26. * if defined mass storage compliance succeeds but with warnings
  27. * => case 4: Hi > Dn
  28. * => case 5: Hi > Di
  29. * => case 8: Hi <> Do
  30. * if undefined usbtest 13 fails
  31. * - TRACE: enable function tracing (depends on DEBUG)
  32. *
  33. * Main Features
  34. * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
  35. * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
  36. * - Normal & LPM support
  37. *
  38. * USBTEST Report
  39. * - OK: 0-12, 13 (STALL_IN defined) & 14
  40. * - Not Supported: 15 & 16 (ISO)
  41. *
  42. * TODO List
  43. * - Suspend & Remote Wakeup
  44. */
  45. #include <linux/delay.h>
  46. #include <linux/device.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/extcon.h>
  49. #include <linux/phy/phy.h>
  50. #include <linux/platform_device.h>
  51. #include <linux/module.h>
  52. #include <linux/idr.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/io.h>
  55. #include <linux/kernel.h>
  56. #include <linux/slab.h>
  57. #include <linux/pm_runtime.h>
  58. #include <linux/usb/ch9.h>
  59. #include <linux/usb/gadget.h>
  60. #include <linux/usb/otg.h>
  61. #include <linux/usb/chipidea.h>
  62. #include <linux/usb/of.h>
  63. #include <linux/of.h>
  64. #include <linux/regulator/consumer.h>
  65. #include <linux/usb/ehci_def.h>
  66. #include "ci.h"
  67. #include "udc.h"
  68. #include "bits.h"
  69. #include "host.h"
  70. #include "otg.h"
  71. #include "otg_fsm.h"
  72. /* Controller register map */
  73. static const u8 ci_regs_nolpm[] = {
  74. [CAP_CAPLENGTH] = 0x00U,
  75. [CAP_HCCPARAMS] = 0x08U,
  76. [CAP_DCCPARAMS] = 0x24U,
  77. [CAP_TESTMODE] = 0x38U,
  78. [OP_USBCMD] = 0x00U,
  79. [OP_USBSTS] = 0x04U,
  80. [OP_USBINTR] = 0x08U,
  81. [OP_DEVICEADDR] = 0x14U,
  82. [OP_ENDPTLISTADDR] = 0x18U,
  83. [OP_TTCTRL] = 0x1CU,
  84. [OP_BURSTSIZE] = 0x20U,
  85. [OP_ULPI_VIEWPORT] = 0x30U,
  86. [OP_PORTSC] = 0x44U,
  87. [OP_DEVLC] = 0x84U,
  88. [OP_OTGSC] = 0x64U,
  89. [OP_USBMODE] = 0x68U,
  90. [OP_ENDPTSETUPSTAT] = 0x6CU,
  91. [OP_ENDPTPRIME] = 0x70U,
  92. [OP_ENDPTFLUSH] = 0x74U,
  93. [OP_ENDPTSTAT] = 0x78U,
  94. [OP_ENDPTCOMPLETE] = 0x7CU,
  95. [OP_ENDPTCTRL] = 0x80U,
  96. };
  97. static const u8 ci_regs_lpm[] = {
  98. [CAP_CAPLENGTH] = 0x00U,
  99. [CAP_HCCPARAMS] = 0x08U,
  100. [CAP_DCCPARAMS] = 0x24U,
  101. [CAP_TESTMODE] = 0xFCU,
  102. [OP_USBCMD] = 0x00U,
  103. [OP_USBSTS] = 0x04U,
  104. [OP_USBINTR] = 0x08U,
  105. [OP_DEVICEADDR] = 0x14U,
  106. [OP_ENDPTLISTADDR] = 0x18U,
  107. [OP_TTCTRL] = 0x1CU,
  108. [OP_BURSTSIZE] = 0x20U,
  109. [OP_ULPI_VIEWPORT] = 0x30U,
  110. [OP_PORTSC] = 0x44U,
  111. [OP_DEVLC] = 0x84U,
  112. [OP_OTGSC] = 0xC4U,
  113. [OP_USBMODE] = 0xC8U,
  114. [OP_ENDPTSETUPSTAT] = 0xD8U,
  115. [OP_ENDPTPRIME] = 0xDCU,
  116. [OP_ENDPTFLUSH] = 0xE0U,
  117. [OP_ENDPTSTAT] = 0xE4U,
  118. [OP_ENDPTCOMPLETE] = 0xE8U,
  119. [OP_ENDPTCTRL] = 0xECU,
  120. };
  121. static void hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
  122. {
  123. int i;
  124. for (i = 0; i < OP_ENDPTCTRL; i++)
  125. ci->hw_bank.regmap[i] =
  126. (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
  127. (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
  128. for (; i <= OP_LAST; i++)
  129. ci->hw_bank.regmap[i] = ci->hw_bank.op +
  130. 4 * (i - OP_ENDPTCTRL) +
  131. (is_lpm
  132. ? ci_regs_lpm[OP_ENDPTCTRL]
  133. : ci_regs_nolpm[OP_ENDPTCTRL]);
  134. }
  135. static enum ci_revision ci_get_revision(struct ci_hdrc *ci)
  136. {
  137. int ver = hw_read_id_reg(ci, ID_ID, VERSION) >> __ffs(VERSION);
  138. enum ci_revision rev = CI_REVISION_UNKNOWN;
  139. if (ver == 0x2) {
  140. rev = hw_read_id_reg(ci, ID_ID, REVISION)
  141. >> __ffs(REVISION);
  142. rev += CI_REVISION_20;
  143. } else if (ver == 0x0) {
  144. rev = CI_REVISION_1X;
  145. }
  146. return rev;
  147. }
  148. /**
  149. * hw_read_intr_enable: returns interrupt enable register
  150. *
  151. * @ci: the controller
  152. *
  153. * This function returns register data
  154. */
  155. u32 hw_read_intr_enable(struct ci_hdrc *ci)
  156. {
  157. return hw_read(ci, OP_USBINTR, ~0);
  158. }
  159. /**
  160. * hw_read_intr_status: returns interrupt status register
  161. *
  162. * @ci: the controller
  163. *
  164. * This function returns register data
  165. */
  166. u32 hw_read_intr_status(struct ci_hdrc *ci)
  167. {
  168. return hw_read(ci, OP_USBSTS, ~0);
  169. }
  170. /**
  171. * hw_port_test_set: writes port test mode (execute without interruption)
  172. * @mode: new value
  173. *
  174. * This function returns an error code
  175. */
  176. int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
  177. {
  178. const u8 TEST_MODE_MAX = 7;
  179. if (mode > TEST_MODE_MAX)
  180. return -EINVAL;
  181. hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
  182. return 0;
  183. }
  184. /**
  185. * hw_port_test_get: reads port test mode value
  186. *
  187. * @ci: the controller
  188. *
  189. * This function returns port test mode value
  190. */
  191. u8 hw_port_test_get(struct ci_hdrc *ci)
  192. {
  193. return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
  194. }
  195. static void hw_wait_phy_stable(void)
  196. {
  197. /*
  198. * The phy needs some delay to output the stable status from low
  199. * power mode. And for OTGSC, the status inputs are debounced
  200. * using a 1 ms time constant, so, delay 2ms for controller to get
  201. * the stable status, like vbus and id when the phy leaves low power.
  202. */
  203. usleep_range(2000, 2500);
  204. }
  205. /* The PHY enters/leaves low power mode */
  206. static void ci_hdrc_enter_lpm(struct ci_hdrc *ci, bool enable)
  207. {
  208. enum ci_hw_regs reg = ci->hw_bank.lpm ? OP_DEVLC : OP_PORTSC;
  209. bool lpm = !!(hw_read(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm)));
  210. if (enable && !lpm)
  211. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  212. PORTSC_PHCD(ci->hw_bank.lpm));
  213. else if (!enable && lpm)
  214. hw_write(ci, reg, PORTSC_PHCD(ci->hw_bank.lpm),
  215. 0);
  216. }
  217. static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
  218. {
  219. u32 reg;
  220. /* bank is a module variable */
  221. ci->hw_bank.abs = base;
  222. ci->hw_bank.cap = ci->hw_bank.abs;
  223. ci->hw_bank.cap += ci->platdata->capoffset;
  224. ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
  225. hw_alloc_regmap(ci, false);
  226. reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
  227. __ffs(HCCPARAMS_LEN);
  228. ci->hw_bank.lpm = reg;
  229. if (reg)
  230. hw_alloc_regmap(ci, !!reg);
  231. ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
  232. ci->hw_bank.size += OP_LAST;
  233. ci->hw_bank.size /= sizeof(u32);
  234. reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
  235. __ffs(DCCPARAMS_DEN);
  236. ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  237. if (ci->hw_ep_max > ENDPT_MAX)
  238. return -ENODEV;
  239. ci_hdrc_enter_lpm(ci, false);
  240. /* Disable all interrupts bits */
  241. hw_write(ci, OP_USBINTR, 0xffffffff, 0);
  242. /* Clear all interrupts status bits*/
  243. hw_write(ci, OP_USBSTS, 0xffffffff, 0xffffffff);
  244. ci->rev = ci_get_revision(ci);
  245. dev_dbg(ci->dev,
  246. "ChipIdea HDRC found, revision: %d, lpm: %d; cap: %p op: %p\n",
  247. ci->rev, ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
  248. /* setup lock mode ? */
  249. /* ENDPTSETUPSTAT is '0' by default */
  250. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  251. return 0;
  252. }
  253. void hw_phymode_configure(struct ci_hdrc *ci)
  254. {
  255. u32 portsc, lpm, sts = 0;
  256. switch (ci->platdata->phy_mode) {
  257. case USBPHY_INTERFACE_MODE_UTMI:
  258. portsc = PORTSC_PTS(PTS_UTMI);
  259. lpm = DEVLC_PTS(PTS_UTMI);
  260. break;
  261. case USBPHY_INTERFACE_MODE_UTMIW:
  262. portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
  263. lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
  264. break;
  265. case USBPHY_INTERFACE_MODE_ULPI:
  266. portsc = PORTSC_PTS(PTS_ULPI);
  267. lpm = DEVLC_PTS(PTS_ULPI);
  268. break;
  269. case USBPHY_INTERFACE_MODE_SERIAL:
  270. portsc = PORTSC_PTS(PTS_SERIAL);
  271. lpm = DEVLC_PTS(PTS_SERIAL);
  272. sts = 1;
  273. break;
  274. case USBPHY_INTERFACE_MODE_HSIC:
  275. portsc = PORTSC_PTS(PTS_HSIC);
  276. lpm = DEVLC_PTS(PTS_HSIC);
  277. break;
  278. default:
  279. return;
  280. }
  281. if (ci->hw_bank.lpm) {
  282. hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
  283. if (sts)
  284. hw_write(ci, OP_DEVLC, DEVLC_STS, DEVLC_STS);
  285. } else {
  286. hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
  287. if (sts)
  288. hw_write(ci, OP_PORTSC, PORTSC_STS, PORTSC_STS);
  289. }
  290. }
  291. EXPORT_SYMBOL_GPL(hw_phymode_configure);
  292. /**
  293. * _ci_usb_phy_init: initialize phy taking in account both phy and usb_phy
  294. * interfaces
  295. * @ci: the controller
  296. *
  297. * This function returns an error code if the phy failed to init
  298. */
  299. static int _ci_usb_phy_init(struct ci_hdrc *ci)
  300. {
  301. int ret;
  302. if (ci->phy) {
  303. ret = phy_init(ci->phy);
  304. if (ret)
  305. return ret;
  306. ret = phy_power_on(ci->phy);
  307. if (ret) {
  308. phy_exit(ci->phy);
  309. return ret;
  310. }
  311. } else {
  312. ret = usb_phy_init(ci->usb_phy);
  313. }
  314. return ret;
  315. }
  316. /**
  317. * _ci_usb_phy_exit: deinitialize phy taking in account both phy and usb_phy
  318. * interfaces
  319. * @ci: the controller
  320. */
  321. static void ci_usb_phy_exit(struct ci_hdrc *ci)
  322. {
  323. if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
  324. return;
  325. if (ci->phy) {
  326. phy_power_off(ci->phy);
  327. phy_exit(ci->phy);
  328. } else {
  329. usb_phy_shutdown(ci->usb_phy);
  330. }
  331. }
  332. /**
  333. * ci_usb_phy_init: initialize phy according to different phy type
  334. * @ci: the controller
  335. *
  336. * This function returns an error code if usb_phy_init has failed
  337. */
  338. static int ci_usb_phy_init(struct ci_hdrc *ci)
  339. {
  340. int ret;
  341. if (ci->platdata->flags & CI_HDRC_OVERRIDE_PHY_CONTROL)
  342. return 0;
  343. switch (ci->platdata->phy_mode) {
  344. case USBPHY_INTERFACE_MODE_UTMI:
  345. case USBPHY_INTERFACE_MODE_UTMIW:
  346. case USBPHY_INTERFACE_MODE_HSIC:
  347. ret = _ci_usb_phy_init(ci);
  348. if (!ret)
  349. hw_wait_phy_stable();
  350. else
  351. return ret;
  352. hw_phymode_configure(ci);
  353. break;
  354. case USBPHY_INTERFACE_MODE_ULPI:
  355. case USBPHY_INTERFACE_MODE_SERIAL:
  356. hw_phymode_configure(ci);
  357. ret = _ci_usb_phy_init(ci);
  358. if (ret)
  359. return ret;
  360. break;
  361. default:
  362. ret = _ci_usb_phy_init(ci);
  363. if (!ret)
  364. hw_wait_phy_stable();
  365. }
  366. return ret;
  367. }
  368. /**
  369. * ci_platform_configure: do controller configure
  370. * @ci: the controller
  371. *
  372. */
  373. void ci_platform_configure(struct ci_hdrc *ci)
  374. {
  375. bool is_device_mode, is_host_mode;
  376. is_device_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_DC;
  377. is_host_mode = hw_read(ci, OP_USBMODE, USBMODE_CM) == USBMODE_CM_HC;
  378. if (is_device_mode) {
  379. phy_set_mode(ci->phy, PHY_MODE_USB_DEVICE);
  380. if (ci->platdata->flags & CI_HDRC_DISABLE_DEVICE_STREAMING)
  381. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS,
  382. USBMODE_CI_SDIS);
  383. }
  384. if (is_host_mode) {
  385. phy_set_mode(ci->phy, PHY_MODE_USB_HOST);
  386. if (ci->platdata->flags & CI_HDRC_DISABLE_HOST_STREAMING)
  387. hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS,
  388. USBMODE_CI_SDIS);
  389. }
  390. if (ci->platdata->flags & CI_HDRC_FORCE_FULLSPEED) {
  391. if (ci->hw_bank.lpm)
  392. hw_write(ci, OP_DEVLC, DEVLC_PFSC, DEVLC_PFSC);
  393. else
  394. hw_write(ci, OP_PORTSC, PORTSC_PFSC, PORTSC_PFSC);
  395. }
  396. if (ci->platdata->flags & CI_HDRC_SET_NON_ZERO_TTHA)
  397. hw_write(ci, OP_TTCTRL, TTCTRL_TTHA_MASK, TTCTRL_TTHA);
  398. hw_write(ci, OP_USBCMD, 0xff0000, ci->platdata->itc_setting << 16);
  399. if (ci->platdata->flags & CI_HDRC_OVERRIDE_AHB_BURST)
  400. hw_write_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK,
  401. ci->platdata->ahb_burst_config);
  402. /* override burst size, take effect only when ahb_burst_config is 0 */
  403. if (!hw_read_id_reg(ci, ID_SBUSCFG, AHBBRST_MASK)) {
  404. if (ci->platdata->flags & CI_HDRC_OVERRIDE_TX_BURST)
  405. hw_write(ci, OP_BURSTSIZE, TX_BURST_MASK,
  406. ci->platdata->tx_burst_size << __ffs(TX_BURST_MASK));
  407. if (ci->platdata->flags & CI_HDRC_OVERRIDE_RX_BURST)
  408. hw_write(ci, OP_BURSTSIZE, RX_BURST_MASK,
  409. ci->platdata->rx_burst_size);
  410. }
  411. }
  412. /**
  413. * hw_controller_reset: do controller reset
  414. * @ci: the controller
  415. *
  416. * This function returns an error code
  417. */
  418. static int hw_controller_reset(struct ci_hdrc *ci)
  419. {
  420. int count = 0;
  421. hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
  422. while (hw_read(ci, OP_USBCMD, USBCMD_RST)) {
  423. udelay(10);
  424. if (count++ > 1000)
  425. return -ETIMEDOUT;
  426. }
  427. return 0;
  428. }
  429. /**
  430. * hw_device_reset: resets chip (execute without interruption)
  431. * @ci: the controller
  432. *
  433. * This function returns an error code
  434. */
  435. int hw_device_reset(struct ci_hdrc *ci)
  436. {
  437. int ret;
  438. /* should flush & stop before reset */
  439. hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
  440. hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
  441. ret = hw_controller_reset(ci);
  442. if (ret) {
  443. dev_err(ci->dev, "error resetting controller, ret=%d\n", ret);
  444. return ret;
  445. }
  446. if (ci->platdata->notify_event) {
  447. ret = ci->platdata->notify_event(ci,
  448. CI_HDRC_CONTROLLER_RESET_EVENT);
  449. if (ret)
  450. return ret;
  451. }
  452. /* USBMODE should be configured step by step */
  453. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  454. hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DC);
  455. /* HW >= 2.3 */
  456. hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
  457. if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DC) {
  458. pr_err("cannot enter in %s device mode", ci_role(ci)->name);
  459. pr_err("lpm = %i", ci->hw_bank.lpm);
  460. return -ENODEV;
  461. }
  462. ci_platform_configure(ci);
  463. return 0;
  464. }
  465. static irqreturn_t ci_irq(int irq, void *data)
  466. {
  467. struct ci_hdrc *ci = data;
  468. irqreturn_t ret = IRQ_NONE;
  469. u32 otgsc = 0;
  470. if (ci->in_lpm) {
  471. disable_irq_nosync(irq);
  472. ci->wakeup_int = true;
  473. pm_runtime_get(ci->dev);
  474. return IRQ_HANDLED;
  475. }
  476. if (ci->is_otg) {
  477. otgsc = hw_read_otgsc(ci, ~0);
  478. if (ci_otg_is_fsm_mode(ci)) {
  479. ret = ci_otg_fsm_irq(ci);
  480. if (ret == IRQ_HANDLED)
  481. return ret;
  482. }
  483. }
  484. /*
  485. * Handle id change interrupt, it indicates device/host function
  486. * switch.
  487. */
  488. if (ci->is_otg && (otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) {
  489. ci->id_event = true;
  490. /* Clear ID change irq status */
  491. hw_write_otgsc(ci, OTGSC_IDIS, OTGSC_IDIS);
  492. ci_otg_queue_work(ci);
  493. return IRQ_HANDLED;
  494. }
  495. /*
  496. * Handle vbus change interrupt, it indicates device connection
  497. * and disconnection events.
  498. */
  499. if (ci->is_otg && (otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
  500. ci->b_sess_valid_event = true;
  501. /* Clear BSV irq */
  502. hw_write_otgsc(ci, OTGSC_BSVIS, OTGSC_BSVIS);
  503. ci_otg_queue_work(ci);
  504. return IRQ_HANDLED;
  505. }
  506. /* Handle device/host interrupt */
  507. if (ci->role != CI_ROLE_END)
  508. ret = ci_role(ci)->irq(ci);
  509. return ret;
  510. }
  511. static int ci_cable_notifier(struct notifier_block *nb, unsigned long event,
  512. void *ptr)
  513. {
  514. struct ci_hdrc_cable *cbl = container_of(nb, struct ci_hdrc_cable, nb);
  515. struct ci_hdrc *ci = cbl->ci;
  516. cbl->connected = event;
  517. cbl->changed = true;
  518. ci_irq(ci->irq, ci);
  519. return NOTIFY_DONE;
  520. }
  521. static int ci_get_platdata(struct device *dev,
  522. struct ci_hdrc_platform_data *platdata)
  523. {
  524. struct extcon_dev *ext_vbus, *ext_id;
  525. struct ci_hdrc_cable *cable;
  526. int ret;
  527. if (!platdata->phy_mode)
  528. platdata->phy_mode = of_usb_get_phy_mode(dev->of_node);
  529. if (!platdata->dr_mode)
  530. platdata->dr_mode = usb_get_dr_mode(dev);
  531. if (platdata->dr_mode == USB_DR_MODE_UNKNOWN)
  532. platdata->dr_mode = USB_DR_MODE_OTG;
  533. if (platdata->dr_mode != USB_DR_MODE_PERIPHERAL) {
  534. /* Get the vbus regulator */
  535. platdata->reg_vbus = devm_regulator_get(dev, "vbus");
  536. if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
  537. return -EPROBE_DEFER;
  538. } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
  539. /* no vbus regulator is needed */
  540. platdata->reg_vbus = NULL;
  541. } else if (IS_ERR(platdata->reg_vbus)) {
  542. dev_err(dev, "Getting regulator error: %ld\n",
  543. PTR_ERR(platdata->reg_vbus));
  544. return PTR_ERR(platdata->reg_vbus);
  545. }
  546. /* Get TPL support */
  547. if (!platdata->tpl_support)
  548. platdata->tpl_support =
  549. of_usb_host_tpl_support(dev->of_node);
  550. }
  551. if (platdata->dr_mode == USB_DR_MODE_OTG) {
  552. /* We can support HNP and SRP of OTG 2.0 */
  553. platdata->ci_otg_caps.otg_rev = 0x0200;
  554. platdata->ci_otg_caps.hnp_support = true;
  555. platdata->ci_otg_caps.srp_support = true;
  556. /* Update otg capabilities by DT properties */
  557. ret = of_usb_update_otg_caps(dev->of_node,
  558. &platdata->ci_otg_caps);
  559. if (ret)
  560. return ret;
  561. }
  562. if (usb_get_maximum_speed(dev) == USB_SPEED_FULL)
  563. platdata->flags |= CI_HDRC_FORCE_FULLSPEED;
  564. of_property_read_u32(dev->of_node, "phy-clkgate-delay-us",
  565. &platdata->phy_clkgate_delay_us);
  566. platdata->itc_setting = 1;
  567. of_property_read_u32(dev->of_node, "itc-setting",
  568. &platdata->itc_setting);
  569. ret = of_property_read_u32(dev->of_node, "ahb-burst-config",
  570. &platdata->ahb_burst_config);
  571. if (!ret) {
  572. platdata->flags |= CI_HDRC_OVERRIDE_AHB_BURST;
  573. } else if (ret != -EINVAL) {
  574. dev_err(dev, "failed to get ahb-burst-config\n");
  575. return ret;
  576. }
  577. ret = of_property_read_u32(dev->of_node, "tx-burst-size-dword",
  578. &platdata->tx_burst_size);
  579. if (!ret) {
  580. platdata->flags |= CI_HDRC_OVERRIDE_TX_BURST;
  581. } else if (ret != -EINVAL) {
  582. dev_err(dev, "failed to get tx-burst-size-dword\n");
  583. return ret;
  584. }
  585. ret = of_property_read_u32(dev->of_node, "rx-burst-size-dword",
  586. &platdata->rx_burst_size);
  587. if (!ret) {
  588. platdata->flags |= CI_HDRC_OVERRIDE_RX_BURST;
  589. } else if (ret != -EINVAL) {
  590. dev_err(dev, "failed to get rx-burst-size-dword\n");
  591. return ret;
  592. }
  593. if (of_find_property(dev->of_node, "non-zero-ttctrl-ttha", NULL))
  594. platdata->flags |= CI_HDRC_SET_NON_ZERO_TTHA;
  595. ext_id = ERR_PTR(-ENODEV);
  596. ext_vbus = ERR_PTR(-ENODEV);
  597. if (of_property_read_bool(dev->of_node, "extcon")) {
  598. /* Each one of them is not mandatory */
  599. ext_vbus = extcon_get_edev_by_phandle(dev, 0);
  600. if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
  601. return PTR_ERR(ext_vbus);
  602. ext_id = extcon_get_edev_by_phandle(dev, 1);
  603. if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
  604. return PTR_ERR(ext_id);
  605. }
  606. cable = &platdata->vbus_extcon;
  607. cable->nb.notifier_call = ci_cable_notifier;
  608. cable->edev = ext_vbus;
  609. if (!IS_ERR(ext_vbus)) {
  610. ret = extcon_get_state(cable->edev, EXTCON_USB);
  611. if (ret)
  612. cable->connected = true;
  613. else
  614. cable->connected = false;
  615. }
  616. cable = &platdata->id_extcon;
  617. cable->nb.notifier_call = ci_cable_notifier;
  618. cable->edev = ext_id;
  619. if (!IS_ERR(ext_id)) {
  620. ret = extcon_get_state(cable->edev, EXTCON_USB_HOST);
  621. if (ret)
  622. cable->connected = true;
  623. else
  624. cable->connected = false;
  625. }
  626. return 0;
  627. }
  628. static int ci_extcon_register(struct ci_hdrc *ci)
  629. {
  630. struct ci_hdrc_cable *id, *vbus;
  631. int ret;
  632. id = &ci->platdata->id_extcon;
  633. id->ci = ci;
  634. if (!IS_ERR_OR_NULL(id->edev)) {
  635. ret = devm_extcon_register_notifier(ci->dev, id->edev,
  636. EXTCON_USB_HOST, &id->nb);
  637. if (ret < 0) {
  638. dev_err(ci->dev, "register ID failed\n");
  639. return ret;
  640. }
  641. }
  642. vbus = &ci->platdata->vbus_extcon;
  643. vbus->ci = ci;
  644. if (!IS_ERR_OR_NULL(vbus->edev)) {
  645. ret = devm_extcon_register_notifier(ci->dev, vbus->edev,
  646. EXTCON_USB, &vbus->nb);
  647. if (ret < 0) {
  648. dev_err(ci->dev, "register VBUS failed\n");
  649. return ret;
  650. }
  651. }
  652. return 0;
  653. }
  654. static DEFINE_IDA(ci_ida);
  655. struct platform_device *ci_hdrc_add_device(struct device *dev,
  656. struct resource *res, int nres,
  657. struct ci_hdrc_platform_data *platdata)
  658. {
  659. struct platform_device *pdev;
  660. int id, ret;
  661. ret = ci_get_platdata(dev, platdata);
  662. if (ret)
  663. return ERR_PTR(ret);
  664. id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
  665. if (id < 0)
  666. return ERR_PTR(id);
  667. pdev = platform_device_alloc("ci_hdrc", id);
  668. if (!pdev) {
  669. ret = -ENOMEM;
  670. goto put_id;
  671. }
  672. pdev->dev.parent = dev;
  673. ret = platform_device_add_resources(pdev, res, nres);
  674. if (ret)
  675. goto err;
  676. ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
  677. if (ret)
  678. goto err;
  679. ret = platform_device_add(pdev);
  680. if (ret)
  681. goto err;
  682. return pdev;
  683. err:
  684. platform_device_put(pdev);
  685. put_id:
  686. ida_simple_remove(&ci_ida, id);
  687. return ERR_PTR(ret);
  688. }
  689. EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
  690. void ci_hdrc_remove_device(struct platform_device *pdev)
  691. {
  692. int id = pdev->id;
  693. platform_device_unregister(pdev);
  694. ida_simple_remove(&ci_ida, id);
  695. }
  696. EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
  697. static inline void ci_role_destroy(struct ci_hdrc *ci)
  698. {
  699. ci_hdrc_gadget_destroy(ci);
  700. ci_hdrc_host_destroy(ci);
  701. if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
  702. ci_hdrc_otg_destroy(ci);
  703. }
  704. static void ci_get_otg_capable(struct ci_hdrc *ci)
  705. {
  706. if (ci->platdata->flags & CI_HDRC_DUAL_ROLE_NOT_OTG)
  707. ci->is_otg = false;
  708. else
  709. ci->is_otg = (hw_read(ci, CAP_DCCPARAMS,
  710. DCCPARAMS_DC | DCCPARAMS_HC)
  711. == (DCCPARAMS_DC | DCCPARAMS_HC));
  712. if (ci->is_otg) {
  713. dev_dbg(ci->dev, "It is OTG capable controller\n");
  714. /* Disable and clear all OTG irq */
  715. hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
  716. OTGSC_INT_STATUS_BITS);
  717. }
  718. }
  719. static ssize_t ci_role_show(struct device *dev, struct device_attribute *attr,
  720. char *buf)
  721. {
  722. struct ci_hdrc *ci = dev_get_drvdata(dev);
  723. if (ci->role != CI_ROLE_END)
  724. return sprintf(buf, "%s\n", ci_role(ci)->name);
  725. return 0;
  726. }
  727. static ssize_t ci_role_store(struct device *dev,
  728. struct device_attribute *attr, const char *buf, size_t n)
  729. {
  730. struct ci_hdrc *ci = dev_get_drvdata(dev);
  731. enum ci_role role;
  732. int ret;
  733. if (!(ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET])) {
  734. dev_warn(dev, "Current configuration is not dual-role, quit\n");
  735. return -EPERM;
  736. }
  737. for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++)
  738. if (!strncmp(buf, ci->roles[role]->name,
  739. strlen(ci->roles[role]->name)))
  740. break;
  741. if (role == CI_ROLE_END || role == ci->role)
  742. return -EINVAL;
  743. pm_runtime_get_sync(dev);
  744. disable_irq(ci->irq);
  745. ci_role_stop(ci);
  746. ret = ci_role_start(ci, role);
  747. if (!ret && ci->role == CI_ROLE_GADGET)
  748. ci_handle_vbus_change(ci);
  749. enable_irq(ci->irq);
  750. pm_runtime_put_sync(dev);
  751. return (ret == 0) ? n : ret;
  752. }
  753. static DEVICE_ATTR(role, 0644, ci_role_show, ci_role_store);
  754. static struct attribute *ci_attrs[] = {
  755. &dev_attr_role.attr,
  756. NULL,
  757. };
  758. static const struct attribute_group ci_attr_group = {
  759. .attrs = ci_attrs,
  760. };
  761. static int ci_hdrc_probe(struct platform_device *pdev)
  762. {
  763. struct device *dev = &pdev->dev;
  764. struct ci_hdrc *ci;
  765. struct resource *res;
  766. void __iomem *base;
  767. int ret;
  768. enum usb_dr_mode dr_mode;
  769. if (!dev_get_platdata(dev)) {
  770. dev_err(dev, "platform data missing\n");
  771. return -ENODEV;
  772. }
  773. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  774. base = devm_ioremap_resource(dev, res);
  775. if (IS_ERR(base))
  776. return PTR_ERR(base);
  777. ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
  778. if (!ci)
  779. return -ENOMEM;
  780. spin_lock_init(&ci->lock);
  781. ci->dev = dev;
  782. ci->platdata = dev_get_platdata(dev);
  783. ci->imx28_write_fix = !!(ci->platdata->flags &
  784. CI_HDRC_IMX28_WRITE_FIX);
  785. ci->supports_runtime_pm = !!(ci->platdata->flags &
  786. CI_HDRC_SUPPORTS_RUNTIME_PM);
  787. platform_set_drvdata(pdev, ci);
  788. ret = hw_device_init(ci, base);
  789. if (ret < 0) {
  790. dev_err(dev, "can't initialize hardware\n");
  791. return -ENODEV;
  792. }
  793. ret = ci_ulpi_init(ci);
  794. if (ret)
  795. return ret;
  796. if (ci->platdata->phy) {
  797. ci->phy = ci->platdata->phy;
  798. } else if (ci->platdata->usb_phy) {
  799. ci->usb_phy = ci->platdata->usb_phy;
  800. } else {
  801. ci->phy = devm_phy_get(dev->parent, "usb-phy");
  802. ci->usb_phy = devm_usb_get_phy(dev->parent, USB_PHY_TYPE_USB2);
  803. /* if both generic PHY and USB PHY layers aren't enabled */
  804. if (PTR_ERR(ci->phy) == -ENOSYS &&
  805. PTR_ERR(ci->usb_phy) == -ENXIO) {
  806. ret = -ENXIO;
  807. goto ulpi_exit;
  808. }
  809. if (IS_ERR(ci->phy) && IS_ERR(ci->usb_phy)) {
  810. ret = -EPROBE_DEFER;
  811. goto ulpi_exit;
  812. }
  813. if (IS_ERR(ci->phy))
  814. ci->phy = NULL;
  815. else if (IS_ERR(ci->usb_phy))
  816. ci->usb_phy = NULL;
  817. }
  818. ret = ci_usb_phy_init(ci);
  819. if (ret) {
  820. dev_err(dev, "unable to init phy: %d\n", ret);
  821. return ret;
  822. }
  823. ci->hw_bank.phys = res->start;
  824. ci->irq = platform_get_irq(pdev, 0);
  825. if (ci->irq < 0) {
  826. dev_err(dev, "missing IRQ\n");
  827. ret = ci->irq;
  828. goto deinit_phy;
  829. }
  830. ci_get_otg_capable(ci);
  831. dr_mode = ci->platdata->dr_mode;
  832. /* initialize role(s) before the interrupt is requested */
  833. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
  834. ret = ci_hdrc_host_init(ci);
  835. if (ret) {
  836. if (ret == -ENXIO)
  837. dev_info(dev, "doesn't support host\n");
  838. else
  839. goto deinit_phy;
  840. }
  841. }
  842. if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
  843. ret = ci_hdrc_gadget_init(ci);
  844. if (ret) {
  845. if (ret == -ENXIO)
  846. dev_info(dev, "doesn't support gadget\n");
  847. else
  848. goto deinit_host;
  849. }
  850. }
  851. if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
  852. dev_err(dev, "no supported roles\n");
  853. ret = -ENODEV;
  854. goto deinit_gadget;
  855. }
  856. if (ci->is_otg && ci->roles[CI_ROLE_GADGET]) {
  857. ret = ci_hdrc_otg_init(ci);
  858. if (ret) {
  859. dev_err(dev, "init otg fails, ret = %d\n", ret);
  860. goto deinit_gadget;
  861. }
  862. }
  863. if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
  864. if (ci->is_otg) {
  865. ci->role = ci_otg_role(ci);
  866. /* Enable ID change irq */
  867. hw_write_otgsc(ci, OTGSC_IDIE, OTGSC_IDIE);
  868. } else {
  869. /*
  870. * If the controller is not OTG capable, but support
  871. * role switch, the defalt role is gadget, and the
  872. * user can switch it through debugfs.
  873. */
  874. ci->role = CI_ROLE_GADGET;
  875. }
  876. } else {
  877. ci->role = ci->roles[CI_ROLE_HOST]
  878. ? CI_ROLE_HOST
  879. : CI_ROLE_GADGET;
  880. }
  881. if (!ci_otg_is_fsm_mode(ci)) {
  882. /* only update vbus status for peripheral */
  883. if (ci->role == CI_ROLE_GADGET)
  884. ci_handle_vbus_change(ci);
  885. ret = ci_role_start(ci, ci->role);
  886. if (ret) {
  887. dev_err(dev, "can't start %s role\n",
  888. ci_role(ci)->name);
  889. goto stop;
  890. }
  891. }
  892. ret = devm_request_irq(dev, ci->irq, ci_irq, IRQF_SHARED,
  893. ci->platdata->name, ci);
  894. if (ret)
  895. goto stop;
  896. ret = ci_extcon_register(ci);
  897. if (ret)
  898. goto stop;
  899. if (ci->supports_runtime_pm) {
  900. pm_runtime_set_active(&pdev->dev);
  901. pm_runtime_enable(&pdev->dev);
  902. pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
  903. pm_runtime_mark_last_busy(ci->dev);
  904. pm_runtime_use_autosuspend(&pdev->dev);
  905. }
  906. if (ci_otg_is_fsm_mode(ci))
  907. ci_hdrc_otg_fsm_start(ci);
  908. device_set_wakeup_capable(&pdev->dev, true);
  909. ret = dbg_create_files(ci);
  910. if (ret)
  911. goto stop;
  912. ret = sysfs_create_group(&dev->kobj, &ci_attr_group);
  913. if (ret)
  914. goto remove_debug;
  915. return 0;
  916. remove_debug:
  917. dbg_remove_files(ci);
  918. stop:
  919. if (ci->is_otg && ci->roles[CI_ROLE_GADGET])
  920. ci_hdrc_otg_destroy(ci);
  921. deinit_gadget:
  922. ci_hdrc_gadget_destroy(ci);
  923. deinit_host:
  924. ci_hdrc_host_destroy(ci);
  925. deinit_phy:
  926. ci_usb_phy_exit(ci);
  927. ulpi_exit:
  928. ci_ulpi_exit(ci);
  929. return ret;
  930. }
  931. static int ci_hdrc_remove(struct platform_device *pdev)
  932. {
  933. struct ci_hdrc *ci = platform_get_drvdata(pdev);
  934. if (ci->supports_runtime_pm) {
  935. pm_runtime_get_sync(&pdev->dev);
  936. pm_runtime_disable(&pdev->dev);
  937. pm_runtime_put_noidle(&pdev->dev);
  938. }
  939. dbg_remove_files(ci);
  940. sysfs_remove_group(&ci->dev->kobj, &ci_attr_group);
  941. ci_role_destroy(ci);
  942. ci_hdrc_enter_lpm(ci, true);
  943. ci_usb_phy_exit(ci);
  944. ci_ulpi_exit(ci);
  945. return 0;
  946. }
  947. #ifdef CONFIG_PM
  948. /* Prepare wakeup by SRP before suspend */
  949. static void ci_otg_fsm_suspend_for_srp(struct ci_hdrc *ci)
  950. {
  951. if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
  952. !hw_read_otgsc(ci, OTGSC_ID)) {
  953. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_PP,
  954. PORTSC_PP);
  955. hw_write(ci, OP_PORTSC, PORTSC_W1C_BITS | PORTSC_WKCN,
  956. PORTSC_WKCN);
  957. }
  958. }
  959. /* Handle SRP when wakeup by data pulse */
  960. static void ci_otg_fsm_wakeup_by_srp(struct ci_hdrc *ci)
  961. {
  962. if ((ci->fsm.otg->state == OTG_STATE_A_IDLE) &&
  963. (ci->fsm.a_bus_drop == 1) && (ci->fsm.a_bus_req == 0)) {
  964. if (!hw_read_otgsc(ci, OTGSC_ID)) {
  965. ci->fsm.a_srp_det = 1;
  966. ci->fsm.a_bus_drop = 0;
  967. } else {
  968. ci->fsm.id = 1;
  969. }
  970. ci_otg_queue_work(ci);
  971. }
  972. }
  973. static void ci_controller_suspend(struct ci_hdrc *ci)
  974. {
  975. disable_irq(ci->irq);
  976. ci_hdrc_enter_lpm(ci, true);
  977. if (ci->platdata->phy_clkgate_delay_us)
  978. usleep_range(ci->platdata->phy_clkgate_delay_us,
  979. ci->platdata->phy_clkgate_delay_us + 50);
  980. usb_phy_set_suspend(ci->usb_phy, 1);
  981. ci->in_lpm = true;
  982. enable_irq(ci->irq);
  983. }
  984. static int ci_controller_resume(struct device *dev)
  985. {
  986. struct ci_hdrc *ci = dev_get_drvdata(dev);
  987. int ret;
  988. dev_dbg(dev, "at %s\n", __func__);
  989. if (!ci->in_lpm) {
  990. WARN_ON(1);
  991. return 0;
  992. }
  993. ci_hdrc_enter_lpm(ci, false);
  994. ret = ci_ulpi_resume(ci);
  995. if (ret)
  996. return ret;
  997. if (ci->usb_phy) {
  998. usb_phy_set_suspend(ci->usb_phy, 0);
  999. usb_phy_set_wakeup(ci->usb_phy, false);
  1000. hw_wait_phy_stable();
  1001. }
  1002. ci->in_lpm = false;
  1003. if (ci->wakeup_int) {
  1004. ci->wakeup_int = false;
  1005. pm_runtime_mark_last_busy(ci->dev);
  1006. pm_runtime_put_autosuspend(ci->dev);
  1007. enable_irq(ci->irq);
  1008. if (ci_otg_is_fsm_mode(ci))
  1009. ci_otg_fsm_wakeup_by_srp(ci);
  1010. }
  1011. return 0;
  1012. }
  1013. #ifdef CONFIG_PM_SLEEP
  1014. static int ci_suspend(struct device *dev)
  1015. {
  1016. struct ci_hdrc *ci = dev_get_drvdata(dev);
  1017. if (ci->wq)
  1018. flush_workqueue(ci->wq);
  1019. /*
  1020. * Controller needs to be active during suspend, otherwise the core
  1021. * may run resume when the parent is at suspend if other driver's
  1022. * suspend fails, it occurs before parent's suspend has not started,
  1023. * but the core suspend has finished.
  1024. */
  1025. if (ci->in_lpm)
  1026. pm_runtime_resume(dev);
  1027. if (ci->in_lpm) {
  1028. WARN_ON(1);
  1029. return 0;
  1030. }
  1031. if (device_may_wakeup(dev)) {
  1032. if (ci_otg_is_fsm_mode(ci))
  1033. ci_otg_fsm_suspend_for_srp(ci);
  1034. usb_phy_set_wakeup(ci->usb_phy, true);
  1035. enable_irq_wake(ci->irq);
  1036. }
  1037. ci_controller_suspend(ci);
  1038. return 0;
  1039. }
  1040. static int ci_resume(struct device *dev)
  1041. {
  1042. struct ci_hdrc *ci = dev_get_drvdata(dev);
  1043. int ret;
  1044. if (device_may_wakeup(dev))
  1045. disable_irq_wake(ci->irq);
  1046. ret = ci_controller_resume(dev);
  1047. if (ret)
  1048. return ret;
  1049. if (ci->supports_runtime_pm) {
  1050. pm_runtime_disable(dev);
  1051. pm_runtime_set_active(dev);
  1052. pm_runtime_enable(dev);
  1053. }
  1054. return ret;
  1055. }
  1056. #endif /* CONFIG_PM_SLEEP */
  1057. static int ci_runtime_suspend(struct device *dev)
  1058. {
  1059. struct ci_hdrc *ci = dev_get_drvdata(dev);
  1060. dev_dbg(dev, "at %s\n", __func__);
  1061. if (ci->in_lpm) {
  1062. WARN_ON(1);
  1063. return 0;
  1064. }
  1065. if (ci_otg_is_fsm_mode(ci))
  1066. ci_otg_fsm_suspend_for_srp(ci);
  1067. usb_phy_set_wakeup(ci->usb_phy, true);
  1068. ci_controller_suspend(ci);
  1069. return 0;
  1070. }
  1071. static int ci_runtime_resume(struct device *dev)
  1072. {
  1073. return ci_controller_resume(dev);
  1074. }
  1075. #endif /* CONFIG_PM */
  1076. static const struct dev_pm_ops ci_pm_ops = {
  1077. SET_SYSTEM_SLEEP_PM_OPS(ci_suspend, ci_resume)
  1078. SET_RUNTIME_PM_OPS(ci_runtime_suspend, ci_runtime_resume, NULL)
  1079. };
  1080. static struct platform_driver ci_hdrc_driver = {
  1081. .probe = ci_hdrc_probe,
  1082. .remove = ci_hdrc_remove,
  1083. .driver = {
  1084. .name = "ci_hdrc",
  1085. .pm = &ci_pm_ops,
  1086. },
  1087. };
  1088. static int __init ci_hdrc_platform_register(void)
  1089. {
  1090. ci_hdrc_host_driver_init();
  1091. return platform_driver_register(&ci_hdrc_driver);
  1092. }
  1093. module_init(ci_hdrc_platform_register);
  1094. static void __exit ci_hdrc_platform_unregister(void)
  1095. {
  1096. platform_driver_unregister(&ci_hdrc_driver);
  1097. }
  1098. module_exit(ci_hdrc_platform_unregister);
  1099. MODULE_ALIAS("platform:ci_hdrc");
  1100. MODULE_LICENSE("GPL v2");
  1101. MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
  1102. MODULE_DESCRIPTION("ChipIdea HDRC Driver");