core.c 30 KB

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