phy-msm-usb.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  1. /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/extcon.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/clk.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/err.h>
  27. #include <linux/delay.h>
  28. #include <linux/io.h>
  29. #include <linux/ioport.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/of.h>
  35. #include <linux/of_device.h>
  36. #include <linux/reboot.h>
  37. #include <linux/reset.h>
  38. #include <linux/types.h>
  39. #include <linux/usb/otg.h>
  40. #include <linux/usb.h>
  41. #include <linux/usb/otg.h>
  42. #include <linux/usb/of.h>
  43. #include <linux/usb/ulpi.h>
  44. #include <linux/usb/gadget.h>
  45. #include <linux/usb/hcd.h>
  46. #include <linux/usb/msm_hsusb_hw.h>
  47. #include <linux/regulator/consumer.h>
  48. /**
  49. * OTG control
  50. *
  51. * OTG_NO_CONTROL Id/VBUS notifications not required. Useful in host
  52. * only configuration.
  53. * OTG_PHY_CONTROL Id/VBUS notifications comes form USB PHY.
  54. * OTG_PMIC_CONTROL Id/VBUS notifications comes from PMIC hardware.
  55. * OTG_USER_CONTROL Id/VBUS notifcations comes from User via sysfs.
  56. *
  57. */
  58. enum otg_control_type {
  59. OTG_NO_CONTROL = 0,
  60. OTG_PHY_CONTROL,
  61. OTG_PMIC_CONTROL,
  62. OTG_USER_CONTROL,
  63. };
  64. /**
  65. * PHY used in
  66. *
  67. * INVALID_PHY Unsupported PHY
  68. * CI_45NM_INTEGRATED_PHY Chipidea 45nm integrated PHY
  69. * SNPS_28NM_INTEGRATED_PHY Synopsis 28nm integrated PHY
  70. *
  71. */
  72. enum msm_usb_phy_type {
  73. INVALID_PHY = 0,
  74. CI_45NM_INTEGRATED_PHY,
  75. SNPS_28NM_INTEGRATED_PHY,
  76. };
  77. #define IDEV_CHG_MAX 1500
  78. #define IUNIT 100
  79. /**
  80. * Different states involved in USB charger detection.
  81. *
  82. * USB_CHG_STATE_UNDEFINED USB charger is not connected or detection
  83. * process is not yet started.
  84. * USB_CHG_STATE_WAIT_FOR_DCD Waiting for Data pins contact.
  85. * USB_CHG_STATE_DCD_DONE Data pin contact is detected.
  86. * USB_CHG_STATE_PRIMARY_DONE Primary detection is completed (Detects
  87. * between SDP and DCP/CDP).
  88. * USB_CHG_STATE_SECONDARY_DONE Secondary detection is completed (Detects
  89. * between DCP and CDP).
  90. * USB_CHG_STATE_DETECTED USB charger type is determined.
  91. *
  92. */
  93. enum usb_chg_state {
  94. USB_CHG_STATE_UNDEFINED = 0,
  95. USB_CHG_STATE_WAIT_FOR_DCD,
  96. USB_CHG_STATE_DCD_DONE,
  97. USB_CHG_STATE_PRIMARY_DONE,
  98. USB_CHG_STATE_SECONDARY_DONE,
  99. USB_CHG_STATE_DETECTED,
  100. };
  101. /**
  102. * USB charger types
  103. *
  104. * USB_INVALID_CHARGER Invalid USB charger.
  105. * USB_SDP_CHARGER Standard downstream port. Refers to a downstream port
  106. * on USB2.0 compliant host/hub.
  107. * USB_DCP_CHARGER Dedicated charger port (AC charger/ Wall charger).
  108. * USB_CDP_CHARGER Charging downstream port. Enumeration can happen and
  109. * IDEV_CHG_MAX can be drawn irrespective of USB state.
  110. *
  111. */
  112. enum usb_chg_type {
  113. USB_INVALID_CHARGER = 0,
  114. USB_SDP_CHARGER,
  115. USB_DCP_CHARGER,
  116. USB_CDP_CHARGER,
  117. };
  118. /**
  119. * struct msm_otg_platform_data - platform device data
  120. * for msm_otg driver.
  121. * @phy_init_seq: PHY configuration sequence values. Value of -1 is reserved as
  122. * "do not overwrite default vaule at this address".
  123. * @phy_init_sz: PHY configuration sequence size.
  124. * @vbus_power: VBUS power on/off routine.
  125. * @power_budget: VBUS power budget in mA (0 will be treated as 500mA).
  126. * @mode: Supported mode (OTG/peripheral/host).
  127. * @otg_control: OTG switch controlled by user/Id pin
  128. */
  129. struct msm_otg_platform_data {
  130. int *phy_init_seq;
  131. int phy_init_sz;
  132. void (*vbus_power)(bool on);
  133. unsigned power_budget;
  134. enum usb_dr_mode mode;
  135. enum otg_control_type otg_control;
  136. enum msm_usb_phy_type phy_type;
  137. void (*setup_gpio)(enum usb_otg_state state);
  138. };
  139. /**
  140. * struct msm_otg: OTG driver data. Shared by HCD and DCD.
  141. * @otg: USB OTG Transceiver structure.
  142. * @pdata: otg device platform data.
  143. * @irq: IRQ number assigned for HSUSB controller.
  144. * @clk: clock struct of usb_hs_clk.
  145. * @pclk: clock struct of usb_hs_pclk.
  146. * @core_clk: clock struct of usb_hs_core_clk.
  147. * @regs: ioremapped register base address.
  148. * @inputs: OTG state machine inputs(Id, SessValid etc).
  149. * @sm_work: OTG state machine work.
  150. * @in_lpm: indicates low power mode (LPM) state.
  151. * @async_int: Async interrupt arrived.
  152. * @cur_power: The amount of mA available from downstream port.
  153. * @chg_work: Charger detection work.
  154. * @chg_state: The state of charger detection process.
  155. * @chg_type: The type of charger attached.
  156. * @dcd_retires: The retry count used to track Data contact
  157. * detection process.
  158. * @manual_pullup: true if VBUS is not routed to USB controller/phy
  159. * and controller driver therefore enables pull-up explicitly before
  160. * starting controller using usbcmd run/stop bit.
  161. * @vbus: VBUS signal state trakining, using extcon framework
  162. * @id: ID signal state trakining, using extcon framework
  163. * @switch_gpio: Descriptor for GPIO used to control external Dual
  164. * SPDT USB Switch.
  165. * @reboot: Used to inform the driver to route USB D+/D- line to Device
  166. * connector
  167. */
  168. struct msm_otg {
  169. struct usb_phy phy;
  170. struct msm_otg_platform_data *pdata;
  171. int irq;
  172. struct clk *clk;
  173. struct clk *pclk;
  174. struct clk *core_clk;
  175. void __iomem *regs;
  176. #define ID 0
  177. #define B_SESS_VLD 1
  178. unsigned long inputs;
  179. struct work_struct sm_work;
  180. atomic_t in_lpm;
  181. int async_int;
  182. unsigned cur_power;
  183. int phy_number;
  184. struct delayed_work chg_work;
  185. enum usb_chg_state chg_state;
  186. enum usb_chg_type chg_type;
  187. u8 dcd_retries;
  188. struct regulator *v3p3;
  189. struct regulator *v1p8;
  190. struct regulator *vddcx;
  191. struct regulator_bulk_data supplies[3];
  192. struct reset_control *phy_rst;
  193. struct reset_control *link_rst;
  194. int vdd_levels[3];
  195. bool manual_pullup;
  196. struct gpio_desc *switch_gpio;
  197. struct notifier_block reboot;
  198. };
  199. #define MSM_USB_BASE (motg->regs)
  200. #define DRIVER_NAME "msm_otg"
  201. #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
  202. #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
  203. #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
  204. #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
  205. #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
  206. #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
  207. #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
  208. #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
  209. #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
  210. #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
  211. #define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
  212. #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
  213. #define USB_PHY_SUSP_DIG_VOL 500000 /* uV */
  214. enum vdd_levels {
  215. VDD_LEVEL_NONE = 0,
  216. VDD_LEVEL_MIN,
  217. VDD_LEVEL_MAX,
  218. };
  219. static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
  220. {
  221. int ret = 0;
  222. if (init) {
  223. ret = regulator_set_voltage(motg->vddcx,
  224. motg->vdd_levels[VDD_LEVEL_MIN],
  225. motg->vdd_levels[VDD_LEVEL_MAX]);
  226. if (ret) {
  227. dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
  228. return ret;
  229. }
  230. ret = regulator_enable(motg->vddcx);
  231. if (ret)
  232. dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
  233. } else {
  234. ret = regulator_set_voltage(motg->vddcx, 0,
  235. motg->vdd_levels[VDD_LEVEL_MAX]);
  236. if (ret)
  237. dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
  238. ret = regulator_disable(motg->vddcx);
  239. if (ret)
  240. dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
  241. }
  242. return ret;
  243. }
  244. static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
  245. {
  246. int rc = 0;
  247. if (init) {
  248. rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
  249. USB_PHY_3P3_VOL_MAX);
  250. if (rc) {
  251. dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
  252. goto exit;
  253. }
  254. rc = regulator_enable(motg->v3p3);
  255. if (rc) {
  256. dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
  257. goto exit;
  258. }
  259. rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
  260. USB_PHY_1P8_VOL_MAX);
  261. if (rc) {
  262. dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
  263. goto disable_3p3;
  264. }
  265. rc = regulator_enable(motg->v1p8);
  266. if (rc) {
  267. dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
  268. goto disable_3p3;
  269. }
  270. return 0;
  271. }
  272. regulator_disable(motg->v1p8);
  273. disable_3p3:
  274. regulator_disable(motg->v3p3);
  275. exit:
  276. return rc;
  277. }
  278. static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
  279. {
  280. int ret = 0;
  281. if (on) {
  282. ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_HPM_LOAD);
  283. if (ret < 0) {
  284. pr_err("Could not set HPM for v1p8\n");
  285. return ret;
  286. }
  287. ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_HPM_LOAD);
  288. if (ret < 0) {
  289. pr_err("Could not set HPM for v3p3\n");
  290. regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
  291. return ret;
  292. }
  293. } else {
  294. ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
  295. if (ret < 0)
  296. pr_err("Could not set LPM for v1p8\n");
  297. ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_LPM_LOAD);
  298. if (ret < 0)
  299. pr_err("Could not set LPM for v3p3\n");
  300. }
  301. pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
  302. return ret < 0 ? ret : 0;
  303. }
  304. static int ulpi_read(struct usb_phy *phy, u32 reg)
  305. {
  306. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  307. int cnt = 0;
  308. /* initiate read operation */
  309. writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
  310. USB_ULPI_VIEWPORT);
  311. /* wait for completion */
  312. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  313. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  314. break;
  315. udelay(1);
  316. cnt++;
  317. }
  318. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  319. dev_err(phy->dev, "ulpi_read: timeout %08x\n",
  320. readl(USB_ULPI_VIEWPORT));
  321. return -ETIMEDOUT;
  322. }
  323. return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
  324. }
  325. static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
  326. {
  327. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  328. int cnt = 0;
  329. /* initiate write operation */
  330. writel(ULPI_RUN | ULPI_WRITE |
  331. ULPI_ADDR(reg) | ULPI_DATA(val),
  332. USB_ULPI_VIEWPORT);
  333. /* wait for completion */
  334. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  335. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  336. break;
  337. udelay(1);
  338. cnt++;
  339. }
  340. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  341. dev_err(phy->dev, "ulpi_write: timeout\n");
  342. return -ETIMEDOUT;
  343. }
  344. return 0;
  345. }
  346. static struct usb_phy_io_ops msm_otg_io_ops = {
  347. .read = ulpi_read,
  348. .write = ulpi_write,
  349. };
  350. static void ulpi_init(struct msm_otg *motg)
  351. {
  352. struct msm_otg_platform_data *pdata = motg->pdata;
  353. int *seq = pdata->phy_init_seq, idx;
  354. u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
  355. for (idx = 0; idx < pdata->phy_init_sz; idx++) {
  356. if (seq[idx] == -1)
  357. continue;
  358. dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
  359. seq[idx], addr + idx);
  360. ulpi_write(&motg->phy, seq[idx], addr + idx);
  361. }
  362. }
  363. static int msm_phy_notify_disconnect(struct usb_phy *phy,
  364. enum usb_device_speed speed)
  365. {
  366. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  367. int val;
  368. if (motg->manual_pullup) {
  369. val = ULPI_MISC_A_VBUSVLDEXT | ULPI_MISC_A_VBUSVLDEXTSEL;
  370. usb_phy_io_write(phy, val, ULPI_CLR(ULPI_MISC_A));
  371. }
  372. /*
  373. * Put the transceiver in non-driving mode. Otherwise host
  374. * may not detect soft-disconnection.
  375. */
  376. val = ulpi_read(phy, ULPI_FUNC_CTRL);
  377. val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  378. val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  379. ulpi_write(phy, val, ULPI_FUNC_CTRL);
  380. return 0;
  381. }
  382. static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
  383. {
  384. int ret;
  385. if (assert)
  386. ret = reset_control_assert(motg->link_rst);
  387. else
  388. ret = reset_control_deassert(motg->link_rst);
  389. if (ret)
  390. dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
  391. assert ? "assert" : "deassert");
  392. return ret;
  393. }
  394. static int msm_otg_phy_clk_reset(struct msm_otg *motg)
  395. {
  396. int ret = 0;
  397. if (motg->phy_rst)
  398. ret = reset_control_reset(motg->phy_rst);
  399. if (ret)
  400. dev_err(motg->phy.dev, "usb phy clk reset failed\n");
  401. return ret;
  402. }
  403. static int msm_link_reset(struct msm_otg *motg)
  404. {
  405. u32 val;
  406. int ret;
  407. ret = msm_otg_link_clk_reset(motg, 1);
  408. if (ret)
  409. return ret;
  410. /* wait for 1ms delay as suggested in HPG. */
  411. usleep_range(1000, 1200);
  412. ret = msm_otg_link_clk_reset(motg, 0);
  413. if (ret)
  414. return ret;
  415. if (motg->phy_number)
  416. writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
  417. /* put transceiver in serial mode as part of reset */
  418. val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
  419. writel(val | PORTSC_PTS_SERIAL, USB_PORTSC);
  420. return 0;
  421. }
  422. static int msm_otg_reset(struct usb_phy *phy)
  423. {
  424. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  425. int cnt = 0;
  426. writel(USBCMD_RESET, USB_USBCMD);
  427. while (cnt < LINK_RESET_TIMEOUT_USEC) {
  428. if (!(readl(USB_USBCMD) & USBCMD_RESET))
  429. break;
  430. udelay(1);
  431. cnt++;
  432. }
  433. if (cnt >= LINK_RESET_TIMEOUT_USEC)
  434. return -ETIMEDOUT;
  435. /* select ULPI phy and clear other status/control bits in PORTSC */
  436. writel(PORTSC_PTS_ULPI, USB_PORTSC);
  437. writel(0x0, USB_AHBBURST);
  438. writel(0x08, USB_AHBMODE);
  439. if (motg->phy_number)
  440. writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
  441. return 0;
  442. }
  443. static void msm_phy_reset(struct msm_otg *motg)
  444. {
  445. void __iomem *addr;
  446. if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY) {
  447. msm_otg_phy_clk_reset(motg);
  448. return;
  449. }
  450. addr = USB_PHY_CTRL;
  451. if (motg->phy_number)
  452. addr = USB_PHY_CTRL2;
  453. /* Assert USB PHY_POR */
  454. writel(readl(addr) | PHY_POR_ASSERT, addr);
  455. /*
  456. * wait for minimum 10 microseconds as suggested in HPG.
  457. * Use a slightly larger value since the exact value didn't
  458. * work 100% of the time.
  459. */
  460. udelay(12);
  461. /* Deassert USB PHY_POR */
  462. writel(readl(addr) & ~PHY_POR_ASSERT, addr);
  463. }
  464. static int msm_usb_reset(struct usb_phy *phy)
  465. {
  466. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  467. int ret;
  468. if (!IS_ERR(motg->core_clk))
  469. clk_prepare_enable(motg->core_clk);
  470. ret = msm_link_reset(motg);
  471. if (ret) {
  472. dev_err(phy->dev, "phy_reset failed\n");
  473. return ret;
  474. }
  475. ret = msm_otg_reset(&motg->phy);
  476. if (ret) {
  477. dev_err(phy->dev, "link reset failed\n");
  478. return ret;
  479. }
  480. msleep(100);
  481. /* Reset USB PHY after performing USB Link RESET */
  482. msm_phy_reset(motg);
  483. if (!IS_ERR(motg->core_clk))
  484. clk_disable_unprepare(motg->core_clk);
  485. return 0;
  486. }
  487. static int msm_phy_init(struct usb_phy *phy)
  488. {
  489. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  490. struct msm_otg_platform_data *pdata = motg->pdata;
  491. u32 val, ulpi_val = 0;
  492. /* Program USB PHY Override registers. */
  493. ulpi_init(motg);
  494. /*
  495. * It is recommended in HPG to reset USB PHY after programming
  496. * USB PHY Override registers.
  497. */
  498. msm_phy_reset(motg);
  499. if (pdata->otg_control == OTG_PHY_CONTROL) {
  500. val = readl(USB_OTGSC);
  501. if (pdata->mode == USB_DR_MODE_OTG) {
  502. ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
  503. val |= OTGSC_IDIE | OTGSC_BSVIE;
  504. } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
  505. ulpi_val = ULPI_INT_SESS_VALID;
  506. val |= OTGSC_BSVIE;
  507. }
  508. writel(val, USB_OTGSC);
  509. ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
  510. ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
  511. }
  512. if (motg->manual_pullup) {
  513. val = ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT;
  514. ulpi_write(phy, val, ULPI_SET(ULPI_MISC_A));
  515. val = readl(USB_GENCONFIG_2);
  516. val |= GENCONFIG_2_SESS_VLD_CTRL_EN;
  517. writel(val, USB_GENCONFIG_2);
  518. val = readl(USB_USBCMD);
  519. val |= USBCMD_SESS_VLD_CTRL;
  520. writel(val, USB_USBCMD);
  521. val = ulpi_read(phy, ULPI_FUNC_CTRL);
  522. val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  523. val |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  524. ulpi_write(phy, val, ULPI_FUNC_CTRL);
  525. }
  526. if (motg->phy_number)
  527. writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
  528. return 0;
  529. }
  530. #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
  531. #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
  532. #ifdef CONFIG_PM
  533. static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
  534. {
  535. int max_vol = motg->vdd_levels[VDD_LEVEL_MAX];
  536. int min_vol;
  537. int ret;
  538. if (high)
  539. min_vol = motg->vdd_levels[VDD_LEVEL_MIN];
  540. else
  541. min_vol = motg->vdd_levels[VDD_LEVEL_NONE];
  542. ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
  543. if (ret) {
  544. pr_err("Cannot set vddcx voltage\n");
  545. return ret;
  546. }
  547. pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
  548. return ret;
  549. }
  550. static int msm_otg_suspend(struct msm_otg *motg)
  551. {
  552. struct usb_phy *phy = &motg->phy;
  553. struct usb_bus *bus = phy->otg->host;
  554. struct msm_otg_platform_data *pdata = motg->pdata;
  555. void __iomem *addr;
  556. int cnt = 0;
  557. if (atomic_read(&motg->in_lpm))
  558. return 0;
  559. disable_irq(motg->irq);
  560. /*
  561. * Chipidea 45-nm PHY suspend sequence:
  562. *
  563. * Interrupt Latch Register auto-clear feature is not present
  564. * in all PHY versions. Latch register is clear on read type.
  565. * Clear latch register to avoid spurious wakeup from
  566. * low power mode (LPM).
  567. *
  568. * PHY comparators are disabled when PHY enters into low power
  569. * mode (LPM). Keep PHY comparators ON in LPM only when we expect
  570. * VBUS/Id notifications from USB PHY. Otherwise turn off USB
  571. * PHY comparators. This save significant amount of power.
  572. *
  573. * PLL is not turned off when PHY enters into low power mode (LPM).
  574. * Disable PLL for maximum power savings.
  575. */
  576. if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
  577. ulpi_read(phy, 0x14);
  578. if (pdata->otg_control == OTG_PHY_CONTROL)
  579. ulpi_write(phy, 0x01, 0x30);
  580. ulpi_write(phy, 0x08, 0x09);
  581. }
  582. /*
  583. * PHY may take some time or even fail to enter into low power
  584. * mode (LPM). Hence poll for 500 msec and reset the PHY and link
  585. * in failure case.
  586. */
  587. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  588. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  589. if (readl(USB_PORTSC) & PORTSC_PHCD)
  590. break;
  591. udelay(1);
  592. cnt++;
  593. }
  594. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
  595. dev_err(phy->dev, "Unable to suspend PHY\n");
  596. msm_otg_reset(phy);
  597. enable_irq(motg->irq);
  598. return -ETIMEDOUT;
  599. }
  600. /*
  601. * PHY has capability to generate interrupt asynchronously in low
  602. * power mode (LPM). This interrupt is level triggered. So USB IRQ
  603. * line must be disabled till async interrupt enable bit is cleared
  604. * in USBCMD register. Assert STP (ULPI interface STOP signal) to
  605. * block data communication from PHY.
  606. */
  607. writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
  608. addr = USB_PHY_CTRL;
  609. if (motg->phy_number)
  610. addr = USB_PHY_CTRL2;
  611. if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
  612. motg->pdata->otg_control == OTG_PMIC_CONTROL)
  613. writel(readl(addr) | PHY_RETEN, addr);
  614. clk_disable_unprepare(motg->pclk);
  615. clk_disable_unprepare(motg->clk);
  616. if (!IS_ERR(motg->core_clk))
  617. clk_disable_unprepare(motg->core_clk);
  618. if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
  619. motg->pdata->otg_control == OTG_PMIC_CONTROL) {
  620. msm_hsusb_ldo_set_mode(motg, 0);
  621. msm_hsusb_config_vddcx(motg, 0);
  622. }
  623. if (device_may_wakeup(phy->dev))
  624. enable_irq_wake(motg->irq);
  625. if (bus)
  626. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  627. atomic_set(&motg->in_lpm, 1);
  628. enable_irq(motg->irq);
  629. dev_info(phy->dev, "USB in low power mode\n");
  630. return 0;
  631. }
  632. static int msm_otg_resume(struct msm_otg *motg)
  633. {
  634. struct usb_phy *phy = &motg->phy;
  635. struct usb_bus *bus = phy->otg->host;
  636. void __iomem *addr;
  637. int cnt = 0;
  638. unsigned temp;
  639. if (!atomic_read(&motg->in_lpm))
  640. return 0;
  641. clk_prepare_enable(motg->pclk);
  642. clk_prepare_enable(motg->clk);
  643. if (!IS_ERR(motg->core_clk))
  644. clk_prepare_enable(motg->core_clk);
  645. if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
  646. motg->pdata->otg_control == OTG_PMIC_CONTROL) {
  647. addr = USB_PHY_CTRL;
  648. if (motg->phy_number)
  649. addr = USB_PHY_CTRL2;
  650. msm_hsusb_ldo_set_mode(motg, 1);
  651. msm_hsusb_config_vddcx(motg, 1);
  652. writel(readl(addr) & ~PHY_RETEN, addr);
  653. }
  654. temp = readl(USB_USBCMD);
  655. temp &= ~ASYNC_INTR_CTRL;
  656. temp &= ~ULPI_STP_CTRL;
  657. writel(temp, USB_USBCMD);
  658. /*
  659. * PHY comes out of low power mode (LPM) in case of wakeup
  660. * from asynchronous interrupt.
  661. */
  662. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  663. goto skip_phy_resume;
  664. writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
  665. while (cnt < PHY_RESUME_TIMEOUT_USEC) {
  666. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  667. break;
  668. udelay(1);
  669. cnt++;
  670. }
  671. if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
  672. /*
  673. * This is a fatal error. Reset the link and
  674. * PHY. USB state can not be restored. Re-insertion
  675. * of USB cable is the only way to get USB working.
  676. */
  677. dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
  678. msm_otg_reset(phy);
  679. }
  680. skip_phy_resume:
  681. if (device_may_wakeup(phy->dev))
  682. disable_irq_wake(motg->irq);
  683. if (bus)
  684. set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  685. atomic_set(&motg->in_lpm, 0);
  686. if (motg->async_int) {
  687. motg->async_int = 0;
  688. pm_runtime_put(phy->dev);
  689. enable_irq(motg->irq);
  690. }
  691. dev_info(phy->dev, "USB exited from low power mode\n");
  692. return 0;
  693. }
  694. #endif
  695. static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
  696. {
  697. if (motg->cur_power == mA)
  698. return;
  699. /* TODO: Notify PMIC about available current */
  700. dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
  701. motg->cur_power = mA;
  702. }
  703. static void msm_otg_start_host(struct usb_phy *phy, int on)
  704. {
  705. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  706. struct msm_otg_platform_data *pdata = motg->pdata;
  707. struct usb_hcd *hcd;
  708. if (!phy->otg->host)
  709. return;
  710. hcd = bus_to_hcd(phy->otg->host);
  711. if (on) {
  712. dev_dbg(phy->dev, "host on\n");
  713. if (pdata->vbus_power)
  714. pdata->vbus_power(1);
  715. /*
  716. * Some boards have a switch cotrolled by gpio
  717. * to enable/disable internal HUB. Enable internal
  718. * HUB before kicking the host.
  719. */
  720. if (pdata->setup_gpio)
  721. pdata->setup_gpio(OTG_STATE_A_HOST);
  722. #ifdef CONFIG_USB
  723. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  724. device_wakeup_enable(hcd->self.controller);
  725. #endif
  726. } else {
  727. dev_dbg(phy->dev, "host off\n");
  728. #ifdef CONFIG_USB
  729. usb_remove_hcd(hcd);
  730. #endif
  731. if (pdata->setup_gpio)
  732. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  733. if (pdata->vbus_power)
  734. pdata->vbus_power(0);
  735. }
  736. }
  737. static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  738. {
  739. struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
  740. struct usb_hcd *hcd;
  741. /*
  742. * Fail host registration if this board can support
  743. * only peripheral configuration.
  744. */
  745. if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
  746. dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
  747. return -ENODEV;
  748. }
  749. if (!host) {
  750. if (otg->state == OTG_STATE_A_HOST) {
  751. pm_runtime_get_sync(otg->usb_phy->dev);
  752. msm_otg_start_host(otg->usb_phy, 0);
  753. otg->host = NULL;
  754. otg->state = OTG_STATE_UNDEFINED;
  755. schedule_work(&motg->sm_work);
  756. } else {
  757. otg->host = NULL;
  758. }
  759. return 0;
  760. }
  761. hcd = bus_to_hcd(host);
  762. hcd->power_budget = motg->pdata->power_budget;
  763. otg->host = host;
  764. dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
  765. pm_runtime_get_sync(otg->usb_phy->dev);
  766. schedule_work(&motg->sm_work);
  767. return 0;
  768. }
  769. static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
  770. {
  771. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  772. struct msm_otg_platform_data *pdata = motg->pdata;
  773. if (!phy->otg->gadget)
  774. return;
  775. if (on) {
  776. dev_dbg(phy->dev, "gadget on\n");
  777. /*
  778. * Some boards have a switch cotrolled by gpio
  779. * to enable/disable internal HUB. Disable internal
  780. * HUB before kicking the gadget.
  781. */
  782. if (pdata->setup_gpio)
  783. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  784. usb_gadget_vbus_connect(phy->otg->gadget);
  785. } else {
  786. dev_dbg(phy->dev, "gadget off\n");
  787. usb_gadget_vbus_disconnect(phy->otg->gadget);
  788. if (pdata->setup_gpio)
  789. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  790. }
  791. }
  792. static int msm_otg_set_peripheral(struct usb_otg *otg,
  793. struct usb_gadget *gadget)
  794. {
  795. struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
  796. /*
  797. * Fail peripheral registration if this board can support
  798. * only host configuration.
  799. */
  800. if (motg->pdata->mode == USB_DR_MODE_HOST) {
  801. dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
  802. return -ENODEV;
  803. }
  804. if (!gadget) {
  805. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  806. pm_runtime_get_sync(otg->usb_phy->dev);
  807. msm_otg_start_peripheral(otg->usb_phy, 0);
  808. otg->gadget = NULL;
  809. otg->state = OTG_STATE_UNDEFINED;
  810. schedule_work(&motg->sm_work);
  811. } else {
  812. otg->gadget = NULL;
  813. }
  814. return 0;
  815. }
  816. otg->gadget = gadget;
  817. dev_dbg(otg->usb_phy->dev,
  818. "peripheral driver registered w/ tranceiver\n");
  819. pm_runtime_get_sync(otg->usb_phy->dev);
  820. schedule_work(&motg->sm_work);
  821. return 0;
  822. }
  823. static bool msm_chg_check_secondary_det(struct msm_otg *motg)
  824. {
  825. struct usb_phy *phy = &motg->phy;
  826. u32 chg_det;
  827. bool ret = false;
  828. switch (motg->pdata->phy_type) {
  829. case CI_45NM_INTEGRATED_PHY:
  830. chg_det = ulpi_read(phy, 0x34);
  831. ret = chg_det & (1 << 4);
  832. break;
  833. case SNPS_28NM_INTEGRATED_PHY:
  834. chg_det = ulpi_read(phy, 0x87);
  835. ret = chg_det & 1;
  836. break;
  837. default:
  838. break;
  839. }
  840. return ret;
  841. }
  842. static void msm_chg_enable_secondary_det(struct msm_otg *motg)
  843. {
  844. struct usb_phy *phy = &motg->phy;
  845. u32 chg_det;
  846. switch (motg->pdata->phy_type) {
  847. case CI_45NM_INTEGRATED_PHY:
  848. chg_det = ulpi_read(phy, 0x34);
  849. /* Turn off charger block */
  850. chg_det |= ~(1 << 1);
  851. ulpi_write(phy, chg_det, 0x34);
  852. udelay(20);
  853. /* control chg block via ULPI */
  854. chg_det &= ~(1 << 3);
  855. ulpi_write(phy, chg_det, 0x34);
  856. /* put it in host mode for enabling D- source */
  857. chg_det &= ~(1 << 2);
  858. ulpi_write(phy, chg_det, 0x34);
  859. /* Turn on chg detect block */
  860. chg_det &= ~(1 << 1);
  861. ulpi_write(phy, chg_det, 0x34);
  862. udelay(20);
  863. /* enable chg detection */
  864. chg_det &= ~(1 << 0);
  865. ulpi_write(phy, chg_det, 0x34);
  866. break;
  867. case SNPS_28NM_INTEGRATED_PHY:
  868. /*
  869. * Configure DM as current source, DP as current sink
  870. * and enable battery charging comparators.
  871. */
  872. ulpi_write(phy, 0x8, 0x85);
  873. ulpi_write(phy, 0x2, 0x85);
  874. ulpi_write(phy, 0x1, 0x85);
  875. break;
  876. default:
  877. break;
  878. }
  879. }
  880. static bool msm_chg_check_primary_det(struct msm_otg *motg)
  881. {
  882. struct usb_phy *phy = &motg->phy;
  883. u32 chg_det;
  884. bool ret = false;
  885. switch (motg->pdata->phy_type) {
  886. case CI_45NM_INTEGRATED_PHY:
  887. chg_det = ulpi_read(phy, 0x34);
  888. ret = chg_det & (1 << 4);
  889. break;
  890. case SNPS_28NM_INTEGRATED_PHY:
  891. chg_det = ulpi_read(phy, 0x87);
  892. ret = chg_det & 1;
  893. break;
  894. default:
  895. break;
  896. }
  897. return ret;
  898. }
  899. static void msm_chg_enable_primary_det(struct msm_otg *motg)
  900. {
  901. struct usb_phy *phy = &motg->phy;
  902. u32 chg_det;
  903. switch (motg->pdata->phy_type) {
  904. case CI_45NM_INTEGRATED_PHY:
  905. chg_det = ulpi_read(phy, 0x34);
  906. /* enable chg detection */
  907. chg_det &= ~(1 << 0);
  908. ulpi_write(phy, chg_det, 0x34);
  909. break;
  910. case SNPS_28NM_INTEGRATED_PHY:
  911. /*
  912. * Configure DP as current source, DM as current sink
  913. * and enable battery charging comparators.
  914. */
  915. ulpi_write(phy, 0x2, 0x85);
  916. ulpi_write(phy, 0x1, 0x85);
  917. break;
  918. default:
  919. break;
  920. }
  921. }
  922. static bool msm_chg_check_dcd(struct msm_otg *motg)
  923. {
  924. struct usb_phy *phy = &motg->phy;
  925. u32 line_state;
  926. bool ret = false;
  927. switch (motg->pdata->phy_type) {
  928. case CI_45NM_INTEGRATED_PHY:
  929. line_state = ulpi_read(phy, 0x15);
  930. ret = !(line_state & 1);
  931. break;
  932. case SNPS_28NM_INTEGRATED_PHY:
  933. line_state = ulpi_read(phy, 0x87);
  934. ret = line_state & 2;
  935. break;
  936. default:
  937. break;
  938. }
  939. return ret;
  940. }
  941. static void msm_chg_disable_dcd(struct msm_otg *motg)
  942. {
  943. struct usb_phy *phy = &motg->phy;
  944. u32 chg_det;
  945. switch (motg->pdata->phy_type) {
  946. case CI_45NM_INTEGRATED_PHY:
  947. chg_det = ulpi_read(phy, 0x34);
  948. chg_det &= ~(1 << 5);
  949. ulpi_write(phy, chg_det, 0x34);
  950. break;
  951. case SNPS_28NM_INTEGRATED_PHY:
  952. ulpi_write(phy, 0x10, 0x86);
  953. break;
  954. default:
  955. break;
  956. }
  957. }
  958. static void msm_chg_enable_dcd(struct msm_otg *motg)
  959. {
  960. struct usb_phy *phy = &motg->phy;
  961. u32 chg_det;
  962. switch (motg->pdata->phy_type) {
  963. case CI_45NM_INTEGRATED_PHY:
  964. chg_det = ulpi_read(phy, 0x34);
  965. /* Turn on D+ current source */
  966. chg_det |= (1 << 5);
  967. ulpi_write(phy, chg_det, 0x34);
  968. break;
  969. case SNPS_28NM_INTEGRATED_PHY:
  970. /* Data contact detection enable */
  971. ulpi_write(phy, 0x10, 0x85);
  972. break;
  973. default:
  974. break;
  975. }
  976. }
  977. static void msm_chg_block_on(struct msm_otg *motg)
  978. {
  979. struct usb_phy *phy = &motg->phy;
  980. u32 func_ctrl, chg_det;
  981. /* put the controller in non-driving mode */
  982. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  983. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  984. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  985. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  986. switch (motg->pdata->phy_type) {
  987. case CI_45NM_INTEGRATED_PHY:
  988. chg_det = ulpi_read(phy, 0x34);
  989. /* control chg block via ULPI */
  990. chg_det &= ~(1 << 3);
  991. ulpi_write(phy, chg_det, 0x34);
  992. /* Turn on chg detect block */
  993. chg_det &= ~(1 << 1);
  994. ulpi_write(phy, chg_det, 0x34);
  995. udelay(20);
  996. break;
  997. case SNPS_28NM_INTEGRATED_PHY:
  998. /* Clear charger detecting control bits */
  999. ulpi_write(phy, 0x3F, 0x86);
  1000. /* Clear alt interrupt latch and enable bits */
  1001. ulpi_write(phy, 0x1F, 0x92);
  1002. ulpi_write(phy, 0x1F, 0x95);
  1003. udelay(100);
  1004. break;
  1005. default:
  1006. break;
  1007. }
  1008. }
  1009. static void msm_chg_block_off(struct msm_otg *motg)
  1010. {
  1011. struct usb_phy *phy = &motg->phy;
  1012. u32 func_ctrl, chg_det;
  1013. switch (motg->pdata->phy_type) {
  1014. case CI_45NM_INTEGRATED_PHY:
  1015. chg_det = ulpi_read(phy, 0x34);
  1016. /* Turn off charger block */
  1017. chg_det |= ~(1 << 1);
  1018. ulpi_write(phy, chg_det, 0x34);
  1019. break;
  1020. case SNPS_28NM_INTEGRATED_PHY:
  1021. /* Clear charger detecting control bits */
  1022. ulpi_write(phy, 0x3F, 0x86);
  1023. /* Clear alt interrupt latch and enable bits */
  1024. ulpi_write(phy, 0x1F, 0x92);
  1025. ulpi_write(phy, 0x1F, 0x95);
  1026. break;
  1027. default:
  1028. break;
  1029. }
  1030. /* put the controller in normal mode */
  1031. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  1032. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  1033. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  1034. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  1035. }
  1036. #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
  1037. #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
  1038. #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
  1039. #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
  1040. static void msm_chg_detect_work(struct work_struct *w)
  1041. {
  1042. struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
  1043. struct usb_phy *phy = &motg->phy;
  1044. bool is_dcd, tmout, vout;
  1045. unsigned long delay;
  1046. dev_dbg(phy->dev, "chg detection work\n");
  1047. switch (motg->chg_state) {
  1048. case USB_CHG_STATE_UNDEFINED:
  1049. pm_runtime_get_sync(phy->dev);
  1050. msm_chg_block_on(motg);
  1051. msm_chg_enable_dcd(motg);
  1052. motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  1053. motg->dcd_retries = 0;
  1054. delay = MSM_CHG_DCD_POLL_TIME;
  1055. break;
  1056. case USB_CHG_STATE_WAIT_FOR_DCD:
  1057. is_dcd = msm_chg_check_dcd(motg);
  1058. tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
  1059. if (is_dcd || tmout) {
  1060. msm_chg_disable_dcd(motg);
  1061. msm_chg_enable_primary_det(motg);
  1062. delay = MSM_CHG_PRIMARY_DET_TIME;
  1063. motg->chg_state = USB_CHG_STATE_DCD_DONE;
  1064. } else {
  1065. delay = MSM_CHG_DCD_POLL_TIME;
  1066. }
  1067. break;
  1068. case USB_CHG_STATE_DCD_DONE:
  1069. vout = msm_chg_check_primary_det(motg);
  1070. if (vout) {
  1071. msm_chg_enable_secondary_det(motg);
  1072. delay = MSM_CHG_SECONDARY_DET_TIME;
  1073. motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  1074. } else {
  1075. motg->chg_type = USB_SDP_CHARGER;
  1076. motg->chg_state = USB_CHG_STATE_DETECTED;
  1077. delay = 0;
  1078. }
  1079. break;
  1080. case USB_CHG_STATE_PRIMARY_DONE:
  1081. vout = msm_chg_check_secondary_det(motg);
  1082. if (vout)
  1083. motg->chg_type = USB_DCP_CHARGER;
  1084. else
  1085. motg->chg_type = USB_CDP_CHARGER;
  1086. motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
  1087. /* fall through */
  1088. case USB_CHG_STATE_SECONDARY_DONE:
  1089. motg->chg_state = USB_CHG_STATE_DETECTED;
  1090. case USB_CHG_STATE_DETECTED:
  1091. msm_chg_block_off(motg);
  1092. dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
  1093. schedule_work(&motg->sm_work);
  1094. return;
  1095. default:
  1096. return;
  1097. }
  1098. schedule_delayed_work(&motg->chg_work, delay);
  1099. }
  1100. /*
  1101. * We support OTG, Peripheral only and Host only configurations. In case
  1102. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  1103. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  1104. * enabled when switch is controlled by user and default mode is supplied
  1105. * by board file, which can be changed by userspace later.
  1106. */
  1107. static void msm_otg_init_sm(struct msm_otg *motg)
  1108. {
  1109. struct msm_otg_platform_data *pdata = motg->pdata;
  1110. u32 otgsc = readl(USB_OTGSC);
  1111. switch (pdata->mode) {
  1112. case USB_DR_MODE_OTG:
  1113. if (pdata->otg_control == OTG_PHY_CONTROL) {
  1114. if (otgsc & OTGSC_ID)
  1115. set_bit(ID, &motg->inputs);
  1116. else
  1117. clear_bit(ID, &motg->inputs);
  1118. if (otgsc & OTGSC_BSV)
  1119. set_bit(B_SESS_VLD, &motg->inputs);
  1120. else
  1121. clear_bit(B_SESS_VLD, &motg->inputs);
  1122. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  1123. set_bit(ID, &motg->inputs);
  1124. clear_bit(B_SESS_VLD, &motg->inputs);
  1125. }
  1126. break;
  1127. case USB_DR_MODE_HOST:
  1128. clear_bit(ID, &motg->inputs);
  1129. break;
  1130. case USB_DR_MODE_PERIPHERAL:
  1131. set_bit(ID, &motg->inputs);
  1132. if (otgsc & OTGSC_BSV)
  1133. set_bit(B_SESS_VLD, &motg->inputs);
  1134. else
  1135. clear_bit(B_SESS_VLD, &motg->inputs);
  1136. break;
  1137. default:
  1138. break;
  1139. }
  1140. }
  1141. static void msm_otg_sm_work(struct work_struct *w)
  1142. {
  1143. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  1144. struct usb_otg *otg = motg->phy.otg;
  1145. switch (otg->state) {
  1146. case OTG_STATE_UNDEFINED:
  1147. dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
  1148. msm_otg_reset(otg->usb_phy);
  1149. msm_otg_init_sm(motg);
  1150. otg->state = OTG_STATE_B_IDLE;
  1151. /* FALL THROUGH */
  1152. case OTG_STATE_B_IDLE:
  1153. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
  1154. if (!test_bit(ID, &motg->inputs) && otg->host) {
  1155. /* disable BSV bit */
  1156. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  1157. msm_otg_start_host(otg->usb_phy, 1);
  1158. otg->state = OTG_STATE_A_HOST;
  1159. } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1160. switch (motg->chg_state) {
  1161. case USB_CHG_STATE_UNDEFINED:
  1162. msm_chg_detect_work(&motg->chg_work.work);
  1163. break;
  1164. case USB_CHG_STATE_DETECTED:
  1165. switch (motg->chg_type) {
  1166. case USB_DCP_CHARGER:
  1167. msm_otg_notify_charger(motg,
  1168. IDEV_CHG_MAX);
  1169. break;
  1170. case USB_CDP_CHARGER:
  1171. msm_otg_notify_charger(motg,
  1172. IDEV_CHG_MAX);
  1173. msm_otg_start_peripheral(otg->usb_phy,
  1174. 1);
  1175. otg->state
  1176. = OTG_STATE_B_PERIPHERAL;
  1177. break;
  1178. case USB_SDP_CHARGER:
  1179. msm_otg_notify_charger(motg, IUNIT);
  1180. msm_otg_start_peripheral(otg->usb_phy,
  1181. 1);
  1182. otg->state
  1183. = OTG_STATE_B_PERIPHERAL;
  1184. break;
  1185. default:
  1186. break;
  1187. }
  1188. break;
  1189. default:
  1190. break;
  1191. }
  1192. } else {
  1193. /*
  1194. * If charger detection work is pending, decrement
  1195. * the pm usage counter to balance with the one that
  1196. * is incremented in charger detection work.
  1197. */
  1198. if (cancel_delayed_work_sync(&motg->chg_work)) {
  1199. pm_runtime_put_sync(otg->usb_phy->dev);
  1200. msm_otg_reset(otg->usb_phy);
  1201. }
  1202. msm_otg_notify_charger(motg, 0);
  1203. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1204. motg->chg_type = USB_INVALID_CHARGER;
  1205. }
  1206. if (otg->state == OTG_STATE_B_IDLE)
  1207. pm_runtime_put_sync(otg->usb_phy->dev);
  1208. break;
  1209. case OTG_STATE_B_PERIPHERAL:
  1210. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
  1211. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  1212. !test_bit(ID, &motg->inputs)) {
  1213. msm_otg_notify_charger(motg, 0);
  1214. msm_otg_start_peripheral(otg->usb_phy, 0);
  1215. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1216. motg->chg_type = USB_INVALID_CHARGER;
  1217. otg->state = OTG_STATE_B_IDLE;
  1218. msm_otg_reset(otg->usb_phy);
  1219. schedule_work(w);
  1220. }
  1221. break;
  1222. case OTG_STATE_A_HOST:
  1223. dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
  1224. if (test_bit(ID, &motg->inputs)) {
  1225. msm_otg_start_host(otg->usb_phy, 0);
  1226. otg->state = OTG_STATE_B_IDLE;
  1227. msm_otg_reset(otg->usb_phy);
  1228. schedule_work(w);
  1229. }
  1230. break;
  1231. default:
  1232. break;
  1233. }
  1234. }
  1235. static irqreturn_t msm_otg_irq(int irq, void *data)
  1236. {
  1237. struct msm_otg *motg = data;
  1238. struct usb_phy *phy = &motg->phy;
  1239. u32 otgsc = 0;
  1240. if (atomic_read(&motg->in_lpm)) {
  1241. disable_irq_nosync(irq);
  1242. motg->async_int = 1;
  1243. pm_runtime_get(phy->dev);
  1244. return IRQ_HANDLED;
  1245. }
  1246. otgsc = readl(USB_OTGSC);
  1247. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  1248. return IRQ_NONE;
  1249. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  1250. if (otgsc & OTGSC_ID)
  1251. set_bit(ID, &motg->inputs);
  1252. else
  1253. clear_bit(ID, &motg->inputs);
  1254. dev_dbg(phy->dev, "ID set/clear\n");
  1255. pm_runtime_get_noresume(phy->dev);
  1256. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  1257. if (otgsc & OTGSC_BSV)
  1258. set_bit(B_SESS_VLD, &motg->inputs);
  1259. else
  1260. clear_bit(B_SESS_VLD, &motg->inputs);
  1261. dev_dbg(phy->dev, "BSV set/clear\n");
  1262. pm_runtime_get_noresume(phy->dev);
  1263. }
  1264. writel(otgsc, USB_OTGSC);
  1265. schedule_work(&motg->sm_work);
  1266. return IRQ_HANDLED;
  1267. }
  1268. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  1269. {
  1270. struct msm_otg *motg = s->private;
  1271. struct usb_otg *otg = motg->phy.otg;
  1272. switch (otg->state) {
  1273. case OTG_STATE_A_HOST:
  1274. seq_puts(s, "host\n");
  1275. break;
  1276. case OTG_STATE_B_PERIPHERAL:
  1277. seq_puts(s, "peripheral\n");
  1278. break;
  1279. default:
  1280. seq_puts(s, "none\n");
  1281. break;
  1282. }
  1283. return 0;
  1284. }
  1285. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  1286. {
  1287. return single_open(file, msm_otg_mode_show, inode->i_private);
  1288. }
  1289. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  1290. size_t count, loff_t *ppos)
  1291. {
  1292. struct seq_file *s = file->private_data;
  1293. struct msm_otg *motg = s->private;
  1294. char buf[16];
  1295. struct usb_otg *otg = motg->phy.otg;
  1296. int status = count;
  1297. enum usb_dr_mode req_mode;
  1298. memset(buf, 0x00, sizeof(buf));
  1299. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  1300. status = -EFAULT;
  1301. goto out;
  1302. }
  1303. if (!strncmp(buf, "host", 4)) {
  1304. req_mode = USB_DR_MODE_HOST;
  1305. } else if (!strncmp(buf, "peripheral", 10)) {
  1306. req_mode = USB_DR_MODE_PERIPHERAL;
  1307. } else if (!strncmp(buf, "none", 4)) {
  1308. req_mode = USB_DR_MODE_UNKNOWN;
  1309. } else {
  1310. status = -EINVAL;
  1311. goto out;
  1312. }
  1313. switch (req_mode) {
  1314. case USB_DR_MODE_UNKNOWN:
  1315. switch (otg->state) {
  1316. case OTG_STATE_A_HOST:
  1317. case OTG_STATE_B_PERIPHERAL:
  1318. set_bit(ID, &motg->inputs);
  1319. clear_bit(B_SESS_VLD, &motg->inputs);
  1320. break;
  1321. default:
  1322. goto out;
  1323. }
  1324. break;
  1325. case USB_DR_MODE_PERIPHERAL:
  1326. switch (otg->state) {
  1327. case OTG_STATE_B_IDLE:
  1328. case OTG_STATE_A_HOST:
  1329. set_bit(ID, &motg->inputs);
  1330. set_bit(B_SESS_VLD, &motg->inputs);
  1331. break;
  1332. default:
  1333. goto out;
  1334. }
  1335. break;
  1336. case USB_DR_MODE_HOST:
  1337. switch (otg->state) {
  1338. case OTG_STATE_B_IDLE:
  1339. case OTG_STATE_B_PERIPHERAL:
  1340. clear_bit(ID, &motg->inputs);
  1341. break;
  1342. default:
  1343. goto out;
  1344. }
  1345. break;
  1346. default:
  1347. goto out;
  1348. }
  1349. pm_runtime_get_sync(otg->usb_phy->dev);
  1350. schedule_work(&motg->sm_work);
  1351. out:
  1352. return status;
  1353. }
  1354. static const struct file_operations msm_otg_mode_fops = {
  1355. .open = msm_otg_mode_open,
  1356. .read = seq_read,
  1357. .write = msm_otg_mode_write,
  1358. .llseek = seq_lseek,
  1359. .release = single_release,
  1360. };
  1361. static struct dentry *msm_otg_dbg_root;
  1362. static struct dentry *msm_otg_dbg_mode;
  1363. static int msm_otg_debugfs_init(struct msm_otg *motg)
  1364. {
  1365. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  1366. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  1367. return -ENODEV;
  1368. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  1369. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  1370. if (!msm_otg_dbg_mode) {
  1371. debugfs_remove(msm_otg_dbg_root);
  1372. msm_otg_dbg_root = NULL;
  1373. return -ENODEV;
  1374. }
  1375. return 0;
  1376. }
  1377. static void msm_otg_debugfs_cleanup(void)
  1378. {
  1379. debugfs_remove(msm_otg_dbg_mode);
  1380. debugfs_remove(msm_otg_dbg_root);
  1381. }
  1382. static const struct of_device_id msm_otg_dt_match[] = {
  1383. {
  1384. .compatible = "qcom,usb-otg-ci",
  1385. .data = (void *) CI_45NM_INTEGRATED_PHY
  1386. },
  1387. {
  1388. .compatible = "qcom,usb-otg-snps",
  1389. .data = (void *) SNPS_28NM_INTEGRATED_PHY
  1390. },
  1391. { }
  1392. };
  1393. MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
  1394. static int msm_otg_vbus_notifier(struct notifier_block *nb, unsigned long event,
  1395. void *ptr)
  1396. {
  1397. struct usb_phy *usb_phy = container_of(nb, struct usb_phy, vbus_nb);
  1398. struct msm_otg *motg = container_of(usb_phy, struct msm_otg, phy);
  1399. if (event)
  1400. set_bit(B_SESS_VLD, &motg->inputs);
  1401. else
  1402. clear_bit(B_SESS_VLD, &motg->inputs);
  1403. if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1404. /* Switch D+/D- lines to Device connector */
  1405. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1406. } else {
  1407. /* Switch D+/D- lines to Hub */
  1408. gpiod_set_value_cansleep(motg->switch_gpio, 1);
  1409. }
  1410. schedule_work(&motg->sm_work);
  1411. return NOTIFY_DONE;
  1412. }
  1413. static int msm_otg_id_notifier(struct notifier_block *nb, unsigned long event,
  1414. void *ptr)
  1415. {
  1416. struct usb_phy *usb_phy = container_of(nb, struct usb_phy, id_nb);
  1417. struct msm_otg *motg = container_of(usb_phy, struct msm_otg, phy);
  1418. if (event)
  1419. clear_bit(ID, &motg->inputs);
  1420. else
  1421. set_bit(ID, &motg->inputs);
  1422. schedule_work(&motg->sm_work);
  1423. return NOTIFY_DONE;
  1424. }
  1425. static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
  1426. {
  1427. struct msm_otg_platform_data *pdata;
  1428. struct device_node *node = pdev->dev.of_node;
  1429. struct property *prop;
  1430. int len, ret, words;
  1431. u32 val, tmp[3];
  1432. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1433. if (!pdata)
  1434. return -ENOMEM;
  1435. motg->pdata = pdata;
  1436. pdata->phy_type = (enum msm_usb_phy_type)of_device_get_match_data(&pdev->dev);
  1437. if (!pdata->phy_type)
  1438. return 1;
  1439. motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
  1440. if (IS_ERR(motg->link_rst))
  1441. return PTR_ERR(motg->link_rst);
  1442. motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
  1443. if (IS_ERR(motg->phy_rst))
  1444. motg->phy_rst = NULL;
  1445. pdata->mode = usb_get_dr_mode(&pdev->dev);
  1446. if (pdata->mode == USB_DR_MODE_UNKNOWN)
  1447. pdata->mode = USB_DR_MODE_OTG;
  1448. pdata->otg_control = OTG_PHY_CONTROL;
  1449. if (!of_property_read_u32(node, "qcom,otg-control", &val))
  1450. if (val == OTG_PMIC_CONTROL)
  1451. pdata->otg_control = val;
  1452. if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
  1453. motg->phy_number = val;
  1454. motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
  1455. motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
  1456. motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
  1457. if (of_get_property(node, "qcom,vdd-levels", &len) &&
  1458. len == sizeof(tmp)) {
  1459. of_property_read_u32_array(node, "qcom,vdd-levels",
  1460. tmp, len / sizeof(*tmp));
  1461. motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
  1462. motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
  1463. motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
  1464. }
  1465. motg->manual_pullup = of_property_read_bool(node, "qcom,manual-pullup");
  1466. motg->switch_gpio = devm_gpiod_get_optional(&pdev->dev, "switch",
  1467. GPIOD_OUT_LOW);
  1468. if (IS_ERR(motg->switch_gpio))
  1469. return PTR_ERR(motg->switch_gpio);
  1470. prop = of_find_property(node, "qcom,phy-init-sequence", &len);
  1471. if (!prop || !len)
  1472. return 0;
  1473. words = len / sizeof(u32);
  1474. if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
  1475. dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
  1476. return 0;
  1477. }
  1478. pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  1479. if (!pdata->phy_init_seq)
  1480. return 0;
  1481. ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
  1482. pdata->phy_init_seq, words);
  1483. if (!ret)
  1484. pdata->phy_init_sz = words;
  1485. return 0;
  1486. }
  1487. static int msm_otg_reboot_notify(struct notifier_block *this,
  1488. unsigned long code, void *unused)
  1489. {
  1490. struct msm_otg *motg = container_of(this, struct msm_otg, reboot);
  1491. /*
  1492. * Ensure that D+/D- lines are routed to uB connector, so
  1493. * we could load bootloader/kernel at next reboot
  1494. */
  1495. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1496. return NOTIFY_DONE;
  1497. }
  1498. static int msm_otg_probe(struct platform_device *pdev)
  1499. {
  1500. int ret = 0;
  1501. struct device_node *np = pdev->dev.of_node;
  1502. struct msm_otg_platform_data *pdata;
  1503. struct resource *res;
  1504. struct msm_otg *motg;
  1505. struct usb_phy *phy;
  1506. void __iomem *phy_select;
  1507. motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
  1508. if (!motg)
  1509. return -ENOMEM;
  1510. motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  1511. GFP_KERNEL);
  1512. if (!motg->phy.otg)
  1513. return -ENOMEM;
  1514. phy = &motg->phy;
  1515. phy->dev = &pdev->dev;
  1516. motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
  1517. if (IS_ERR(motg->clk)) {
  1518. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  1519. return PTR_ERR(motg->clk);
  1520. }
  1521. /*
  1522. * If USB Core is running its protocol engine based on CORE CLK,
  1523. * CORE CLK must be running at >55Mhz for correct HSUSB
  1524. * operation and USB core cannot tolerate frequency changes on
  1525. * CORE CLK.
  1526. */
  1527. motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
  1528. if (IS_ERR(motg->pclk)) {
  1529. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  1530. return PTR_ERR(motg->pclk);
  1531. }
  1532. /*
  1533. * USB core clock is not present on all MSM chips. This
  1534. * clock is introduced to remove the dependency on AXI
  1535. * bus frequency.
  1536. */
  1537. motg->core_clk = devm_clk_get(&pdev->dev,
  1538. np ? "alt_core" : "usb_hs_core_clk");
  1539. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1540. if (!res)
  1541. return -EINVAL;
  1542. motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  1543. if (!motg->regs)
  1544. return -ENOMEM;
  1545. pdata = dev_get_platdata(&pdev->dev);
  1546. if (!pdata) {
  1547. if (!np)
  1548. return -ENXIO;
  1549. ret = msm_otg_read_dt(pdev, motg);
  1550. if (ret)
  1551. return ret;
  1552. }
  1553. /*
  1554. * NOTE: The PHYs can be multiplexed between the chipidea controller
  1555. * and the dwc3 controller, using a single bit. It is important that
  1556. * the dwc3 driver does not set this bit in an incompatible way.
  1557. */
  1558. if (motg->phy_number) {
  1559. phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
  1560. if (!phy_select)
  1561. return -ENOMEM;
  1562. /* Enable second PHY with the OTG port */
  1563. writel(0x1, phy_select);
  1564. }
  1565. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  1566. motg->irq = platform_get_irq(pdev, 0);
  1567. if (motg->irq < 0) {
  1568. dev_err(&pdev->dev, "platform_get_irq failed\n");
  1569. ret = motg->irq;
  1570. return motg->irq;
  1571. }
  1572. motg->supplies[0].supply = "vddcx";
  1573. motg->supplies[1].supply = "v3p3";
  1574. motg->supplies[2].supply = "v1p8";
  1575. ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(motg->supplies),
  1576. motg->supplies);
  1577. if (ret)
  1578. return ret;
  1579. motg->vddcx = motg->supplies[0].consumer;
  1580. motg->v3p3 = motg->supplies[1].consumer;
  1581. motg->v1p8 = motg->supplies[2].consumer;
  1582. clk_set_rate(motg->clk, 60000000);
  1583. clk_prepare_enable(motg->clk);
  1584. clk_prepare_enable(motg->pclk);
  1585. if (!IS_ERR(motg->core_clk))
  1586. clk_prepare_enable(motg->core_clk);
  1587. ret = msm_hsusb_init_vddcx(motg, 1);
  1588. if (ret) {
  1589. dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
  1590. goto disable_clks;
  1591. }
  1592. ret = msm_hsusb_ldo_init(motg, 1);
  1593. if (ret) {
  1594. dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
  1595. goto disable_vddcx;
  1596. }
  1597. ret = msm_hsusb_ldo_set_mode(motg, 1);
  1598. if (ret) {
  1599. dev_err(&pdev->dev, "hsusb vreg enable failed\n");
  1600. goto disable_ldo;
  1601. }
  1602. writel(0, USB_USBINTR);
  1603. writel(0, USB_OTGSC);
  1604. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  1605. INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
  1606. ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
  1607. "msm_otg", motg);
  1608. if (ret) {
  1609. dev_err(&pdev->dev, "request irq failed\n");
  1610. goto disable_ldo;
  1611. }
  1612. phy->init = msm_phy_init;
  1613. phy->notify_disconnect = msm_phy_notify_disconnect;
  1614. phy->type = USB_PHY_TYPE_USB2;
  1615. phy->vbus_nb.notifier_call = msm_otg_vbus_notifier;
  1616. phy->id_nb.notifier_call = msm_otg_id_notifier;
  1617. phy->io_ops = &msm_otg_io_ops;
  1618. phy->otg->usb_phy = &motg->phy;
  1619. phy->otg->set_host = msm_otg_set_host;
  1620. phy->otg->set_peripheral = msm_otg_set_peripheral;
  1621. msm_usb_reset(phy);
  1622. ret = usb_add_phy_dev(&motg->phy);
  1623. if (ret) {
  1624. dev_err(&pdev->dev, "usb_add_phy failed\n");
  1625. goto disable_ldo;
  1626. }
  1627. ret = extcon_get_state(phy->edev, EXTCON_USB);
  1628. if (ret)
  1629. set_bit(B_SESS_VLD, &motg->inputs);
  1630. else
  1631. clear_bit(B_SESS_VLD, &motg->inputs);
  1632. ret = extcon_get_state(phy->id_edev, EXTCON_USB_HOST);
  1633. if (ret)
  1634. clear_bit(ID, &motg->inputs);
  1635. else
  1636. set_bit(ID, &motg->inputs);
  1637. platform_set_drvdata(pdev, motg);
  1638. device_init_wakeup(&pdev->dev, 1);
  1639. if (motg->pdata->mode == USB_DR_MODE_OTG &&
  1640. motg->pdata->otg_control == OTG_USER_CONTROL) {
  1641. ret = msm_otg_debugfs_init(motg);
  1642. if (ret)
  1643. dev_dbg(&pdev->dev, "Can not create mode change file\n");
  1644. }
  1645. if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1646. /* Switch D+/D- lines to Device connector */
  1647. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1648. } else {
  1649. /* Switch D+/D- lines to Hub */
  1650. gpiod_set_value_cansleep(motg->switch_gpio, 1);
  1651. }
  1652. motg->reboot.notifier_call = msm_otg_reboot_notify;
  1653. register_reboot_notifier(&motg->reboot);
  1654. pm_runtime_set_active(&pdev->dev);
  1655. pm_runtime_enable(&pdev->dev);
  1656. return 0;
  1657. disable_ldo:
  1658. msm_hsusb_ldo_init(motg, 0);
  1659. disable_vddcx:
  1660. msm_hsusb_init_vddcx(motg, 0);
  1661. disable_clks:
  1662. clk_disable_unprepare(motg->pclk);
  1663. clk_disable_unprepare(motg->clk);
  1664. if (!IS_ERR(motg->core_clk))
  1665. clk_disable_unprepare(motg->core_clk);
  1666. return ret;
  1667. }
  1668. static int msm_otg_remove(struct platform_device *pdev)
  1669. {
  1670. struct msm_otg *motg = platform_get_drvdata(pdev);
  1671. struct usb_phy *phy = &motg->phy;
  1672. int cnt = 0;
  1673. if (phy->otg->host || phy->otg->gadget)
  1674. return -EBUSY;
  1675. unregister_reboot_notifier(&motg->reboot);
  1676. /*
  1677. * Ensure that D+/D- lines are routed to uB connector, so
  1678. * we could load bootloader/kernel at next reboot
  1679. */
  1680. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1681. msm_otg_debugfs_cleanup();
  1682. cancel_delayed_work_sync(&motg->chg_work);
  1683. cancel_work_sync(&motg->sm_work);
  1684. pm_runtime_resume(&pdev->dev);
  1685. device_init_wakeup(&pdev->dev, 0);
  1686. pm_runtime_disable(&pdev->dev);
  1687. usb_remove_phy(phy);
  1688. disable_irq(motg->irq);
  1689. /*
  1690. * Put PHY in low power mode.
  1691. */
  1692. ulpi_read(phy, 0x14);
  1693. ulpi_write(phy, 0x08, 0x09);
  1694. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  1695. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  1696. if (readl(USB_PORTSC) & PORTSC_PHCD)
  1697. break;
  1698. udelay(1);
  1699. cnt++;
  1700. }
  1701. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  1702. dev_err(phy->dev, "Unable to suspend PHY\n");
  1703. clk_disable_unprepare(motg->pclk);
  1704. clk_disable_unprepare(motg->clk);
  1705. if (!IS_ERR(motg->core_clk))
  1706. clk_disable_unprepare(motg->core_clk);
  1707. msm_hsusb_ldo_init(motg, 0);
  1708. pm_runtime_set_suspended(&pdev->dev);
  1709. return 0;
  1710. }
  1711. #ifdef CONFIG_PM
  1712. static int msm_otg_runtime_idle(struct device *dev)
  1713. {
  1714. struct msm_otg *motg = dev_get_drvdata(dev);
  1715. struct usb_otg *otg = motg->phy.otg;
  1716. dev_dbg(dev, "OTG runtime idle\n");
  1717. /*
  1718. * It is observed some times that a spurious interrupt
  1719. * comes when PHY is put into LPM immediately after PHY reset.
  1720. * This 1 sec delay also prevents entering into LPM immediately
  1721. * after asynchronous interrupt.
  1722. */
  1723. if (otg->state != OTG_STATE_UNDEFINED)
  1724. pm_schedule_suspend(dev, 1000);
  1725. return -EAGAIN;
  1726. }
  1727. static int msm_otg_runtime_suspend(struct device *dev)
  1728. {
  1729. struct msm_otg *motg = dev_get_drvdata(dev);
  1730. dev_dbg(dev, "OTG runtime suspend\n");
  1731. return msm_otg_suspend(motg);
  1732. }
  1733. static int msm_otg_runtime_resume(struct device *dev)
  1734. {
  1735. struct msm_otg *motg = dev_get_drvdata(dev);
  1736. dev_dbg(dev, "OTG runtime resume\n");
  1737. return msm_otg_resume(motg);
  1738. }
  1739. #endif
  1740. #ifdef CONFIG_PM_SLEEP
  1741. static int msm_otg_pm_suspend(struct device *dev)
  1742. {
  1743. struct msm_otg *motg = dev_get_drvdata(dev);
  1744. dev_dbg(dev, "OTG PM suspend\n");
  1745. return msm_otg_suspend(motg);
  1746. }
  1747. static int msm_otg_pm_resume(struct device *dev)
  1748. {
  1749. struct msm_otg *motg = dev_get_drvdata(dev);
  1750. int ret;
  1751. dev_dbg(dev, "OTG PM resume\n");
  1752. ret = msm_otg_resume(motg);
  1753. if (ret)
  1754. return ret;
  1755. /*
  1756. * Runtime PM Documentation recommends bringing the
  1757. * device to full powered state upon resume.
  1758. */
  1759. pm_runtime_disable(dev);
  1760. pm_runtime_set_active(dev);
  1761. pm_runtime_enable(dev);
  1762. return 0;
  1763. }
  1764. #endif
  1765. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  1766. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  1767. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  1768. msm_otg_runtime_idle)
  1769. };
  1770. static struct platform_driver msm_otg_driver = {
  1771. .probe = msm_otg_probe,
  1772. .remove = msm_otg_remove,
  1773. .driver = {
  1774. .name = DRIVER_NAME,
  1775. .pm = &msm_otg_dev_pm_ops,
  1776. .of_match_table = msm_otg_dt_match,
  1777. },
  1778. };
  1779. module_platform_driver(msm_otg_driver);
  1780. MODULE_LICENSE("GPL v2");
  1781. MODULE_DESCRIPTION("MSM USB transceiver driver");