core.c 30 KB

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