phy-msm-usb.c 52 KB

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