core.c 29 KB

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