core.c 29 KB

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