core.c 30 KB

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