core.c 29 KB

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