core.c 30 KB

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