phy-msm-usb.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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 int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
  715. {
  716. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  717. /*
  718. * Gadget driver uses set_power method to notify about the
  719. * available current based on suspend/configured states.
  720. *
  721. * IDEV_CHG can be drawn irrespective of suspend/un-configured
  722. * states when CDP/ACA is connected.
  723. */
  724. if (motg->chg_type == USB_SDP_CHARGER)
  725. msm_otg_notify_charger(motg, mA);
  726. return 0;
  727. }
  728. static void msm_otg_start_host(struct usb_phy *phy, int on)
  729. {
  730. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  731. struct msm_otg_platform_data *pdata = motg->pdata;
  732. struct usb_hcd *hcd;
  733. if (!phy->otg->host)
  734. return;
  735. hcd = bus_to_hcd(phy->otg->host);
  736. if (on) {
  737. dev_dbg(phy->dev, "host on\n");
  738. if (pdata->vbus_power)
  739. pdata->vbus_power(1);
  740. /*
  741. * Some boards have a switch cotrolled by gpio
  742. * to enable/disable internal HUB. Enable internal
  743. * HUB before kicking the host.
  744. */
  745. if (pdata->setup_gpio)
  746. pdata->setup_gpio(OTG_STATE_A_HOST);
  747. #ifdef CONFIG_USB
  748. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  749. device_wakeup_enable(hcd->self.controller);
  750. #endif
  751. } else {
  752. dev_dbg(phy->dev, "host off\n");
  753. #ifdef CONFIG_USB
  754. usb_remove_hcd(hcd);
  755. #endif
  756. if (pdata->setup_gpio)
  757. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  758. if (pdata->vbus_power)
  759. pdata->vbus_power(0);
  760. }
  761. }
  762. static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  763. {
  764. struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
  765. struct usb_hcd *hcd;
  766. /*
  767. * Fail host registration if this board can support
  768. * only peripheral configuration.
  769. */
  770. if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
  771. dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
  772. return -ENODEV;
  773. }
  774. if (!host) {
  775. if (otg->state == OTG_STATE_A_HOST) {
  776. pm_runtime_get_sync(otg->usb_phy->dev);
  777. msm_otg_start_host(otg->usb_phy, 0);
  778. otg->host = NULL;
  779. otg->state = OTG_STATE_UNDEFINED;
  780. schedule_work(&motg->sm_work);
  781. } else {
  782. otg->host = NULL;
  783. }
  784. return 0;
  785. }
  786. hcd = bus_to_hcd(host);
  787. hcd->power_budget = motg->pdata->power_budget;
  788. otg->host = host;
  789. dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
  790. pm_runtime_get_sync(otg->usb_phy->dev);
  791. schedule_work(&motg->sm_work);
  792. return 0;
  793. }
  794. static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
  795. {
  796. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  797. struct msm_otg_platform_data *pdata = motg->pdata;
  798. if (!phy->otg->gadget)
  799. return;
  800. if (on) {
  801. dev_dbg(phy->dev, "gadget on\n");
  802. /*
  803. * Some boards have a switch cotrolled by gpio
  804. * to enable/disable internal HUB. Disable internal
  805. * HUB before kicking the gadget.
  806. */
  807. if (pdata->setup_gpio)
  808. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  809. usb_gadget_vbus_connect(phy->otg->gadget);
  810. } else {
  811. dev_dbg(phy->dev, "gadget off\n");
  812. usb_gadget_vbus_disconnect(phy->otg->gadget);
  813. if (pdata->setup_gpio)
  814. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  815. }
  816. }
  817. static int msm_otg_set_peripheral(struct usb_otg *otg,
  818. struct usb_gadget *gadget)
  819. {
  820. struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
  821. /*
  822. * Fail peripheral registration if this board can support
  823. * only host configuration.
  824. */
  825. if (motg->pdata->mode == USB_DR_MODE_HOST) {
  826. dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
  827. return -ENODEV;
  828. }
  829. if (!gadget) {
  830. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  831. pm_runtime_get_sync(otg->usb_phy->dev);
  832. msm_otg_start_peripheral(otg->usb_phy, 0);
  833. otg->gadget = NULL;
  834. otg->state = OTG_STATE_UNDEFINED;
  835. schedule_work(&motg->sm_work);
  836. } else {
  837. otg->gadget = NULL;
  838. }
  839. return 0;
  840. }
  841. otg->gadget = gadget;
  842. dev_dbg(otg->usb_phy->dev,
  843. "peripheral driver registered w/ tranceiver\n");
  844. pm_runtime_get_sync(otg->usb_phy->dev);
  845. schedule_work(&motg->sm_work);
  846. return 0;
  847. }
  848. static bool msm_chg_check_secondary_det(struct msm_otg *motg)
  849. {
  850. struct usb_phy *phy = &motg->phy;
  851. u32 chg_det;
  852. bool ret = false;
  853. switch (motg->pdata->phy_type) {
  854. case CI_45NM_INTEGRATED_PHY:
  855. chg_det = ulpi_read(phy, 0x34);
  856. ret = chg_det & (1 << 4);
  857. break;
  858. case SNPS_28NM_INTEGRATED_PHY:
  859. chg_det = ulpi_read(phy, 0x87);
  860. ret = chg_det & 1;
  861. break;
  862. default:
  863. break;
  864. }
  865. return ret;
  866. }
  867. static void msm_chg_enable_secondary_det(struct msm_otg *motg)
  868. {
  869. struct usb_phy *phy = &motg->phy;
  870. u32 chg_det;
  871. switch (motg->pdata->phy_type) {
  872. case CI_45NM_INTEGRATED_PHY:
  873. chg_det = ulpi_read(phy, 0x34);
  874. /* Turn off charger block */
  875. chg_det |= ~(1 << 1);
  876. ulpi_write(phy, chg_det, 0x34);
  877. udelay(20);
  878. /* control chg block via ULPI */
  879. chg_det &= ~(1 << 3);
  880. ulpi_write(phy, chg_det, 0x34);
  881. /* put it in host mode for enabling D- source */
  882. chg_det &= ~(1 << 2);
  883. ulpi_write(phy, chg_det, 0x34);
  884. /* Turn on chg detect block */
  885. chg_det &= ~(1 << 1);
  886. ulpi_write(phy, chg_det, 0x34);
  887. udelay(20);
  888. /* enable chg detection */
  889. chg_det &= ~(1 << 0);
  890. ulpi_write(phy, chg_det, 0x34);
  891. break;
  892. case SNPS_28NM_INTEGRATED_PHY:
  893. /*
  894. * Configure DM as current source, DP as current sink
  895. * and enable battery charging comparators.
  896. */
  897. ulpi_write(phy, 0x8, 0x85);
  898. ulpi_write(phy, 0x2, 0x85);
  899. ulpi_write(phy, 0x1, 0x85);
  900. break;
  901. default:
  902. break;
  903. }
  904. }
  905. static bool msm_chg_check_primary_det(struct msm_otg *motg)
  906. {
  907. struct usb_phy *phy = &motg->phy;
  908. u32 chg_det;
  909. bool ret = false;
  910. switch (motg->pdata->phy_type) {
  911. case CI_45NM_INTEGRATED_PHY:
  912. chg_det = ulpi_read(phy, 0x34);
  913. ret = chg_det & (1 << 4);
  914. break;
  915. case SNPS_28NM_INTEGRATED_PHY:
  916. chg_det = ulpi_read(phy, 0x87);
  917. ret = chg_det & 1;
  918. break;
  919. default:
  920. break;
  921. }
  922. return ret;
  923. }
  924. static void msm_chg_enable_primary_det(struct msm_otg *motg)
  925. {
  926. struct usb_phy *phy = &motg->phy;
  927. u32 chg_det;
  928. switch (motg->pdata->phy_type) {
  929. case CI_45NM_INTEGRATED_PHY:
  930. chg_det = ulpi_read(phy, 0x34);
  931. /* enable chg detection */
  932. chg_det &= ~(1 << 0);
  933. ulpi_write(phy, chg_det, 0x34);
  934. break;
  935. case SNPS_28NM_INTEGRATED_PHY:
  936. /*
  937. * Configure DP as current source, DM as current sink
  938. * and enable battery charging comparators.
  939. */
  940. ulpi_write(phy, 0x2, 0x85);
  941. ulpi_write(phy, 0x1, 0x85);
  942. break;
  943. default:
  944. break;
  945. }
  946. }
  947. static bool msm_chg_check_dcd(struct msm_otg *motg)
  948. {
  949. struct usb_phy *phy = &motg->phy;
  950. u32 line_state;
  951. bool ret = false;
  952. switch (motg->pdata->phy_type) {
  953. case CI_45NM_INTEGRATED_PHY:
  954. line_state = ulpi_read(phy, 0x15);
  955. ret = !(line_state & 1);
  956. break;
  957. case SNPS_28NM_INTEGRATED_PHY:
  958. line_state = ulpi_read(phy, 0x87);
  959. ret = line_state & 2;
  960. break;
  961. default:
  962. break;
  963. }
  964. return ret;
  965. }
  966. static void msm_chg_disable_dcd(struct msm_otg *motg)
  967. {
  968. struct usb_phy *phy = &motg->phy;
  969. u32 chg_det;
  970. switch (motg->pdata->phy_type) {
  971. case CI_45NM_INTEGRATED_PHY:
  972. chg_det = ulpi_read(phy, 0x34);
  973. chg_det &= ~(1 << 5);
  974. ulpi_write(phy, chg_det, 0x34);
  975. break;
  976. case SNPS_28NM_INTEGRATED_PHY:
  977. ulpi_write(phy, 0x10, 0x86);
  978. break;
  979. default:
  980. break;
  981. }
  982. }
  983. static void msm_chg_enable_dcd(struct msm_otg *motg)
  984. {
  985. struct usb_phy *phy = &motg->phy;
  986. u32 chg_det;
  987. switch (motg->pdata->phy_type) {
  988. case CI_45NM_INTEGRATED_PHY:
  989. chg_det = ulpi_read(phy, 0x34);
  990. /* Turn on D+ current source */
  991. chg_det |= (1 << 5);
  992. ulpi_write(phy, chg_det, 0x34);
  993. break;
  994. case SNPS_28NM_INTEGRATED_PHY:
  995. /* Data contact detection enable */
  996. ulpi_write(phy, 0x10, 0x85);
  997. break;
  998. default:
  999. break;
  1000. }
  1001. }
  1002. static void msm_chg_block_on(struct msm_otg *motg)
  1003. {
  1004. struct usb_phy *phy = &motg->phy;
  1005. u32 func_ctrl, chg_det;
  1006. /* put the controller in non-driving mode */
  1007. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  1008. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  1009. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  1010. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  1011. switch (motg->pdata->phy_type) {
  1012. case CI_45NM_INTEGRATED_PHY:
  1013. chg_det = ulpi_read(phy, 0x34);
  1014. /* control chg block via ULPI */
  1015. chg_det &= ~(1 << 3);
  1016. ulpi_write(phy, chg_det, 0x34);
  1017. /* Turn on chg detect block */
  1018. chg_det &= ~(1 << 1);
  1019. ulpi_write(phy, chg_det, 0x34);
  1020. udelay(20);
  1021. break;
  1022. case SNPS_28NM_INTEGRATED_PHY:
  1023. /* Clear charger detecting control bits */
  1024. ulpi_write(phy, 0x3F, 0x86);
  1025. /* Clear alt interrupt latch and enable bits */
  1026. ulpi_write(phy, 0x1F, 0x92);
  1027. ulpi_write(phy, 0x1F, 0x95);
  1028. udelay(100);
  1029. break;
  1030. default:
  1031. break;
  1032. }
  1033. }
  1034. static void msm_chg_block_off(struct msm_otg *motg)
  1035. {
  1036. struct usb_phy *phy = &motg->phy;
  1037. u32 func_ctrl, chg_det;
  1038. switch (motg->pdata->phy_type) {
  1039. case CI_45NM_INTEGRATED_PHY:
  1040. chg_det = ulpi_read(phy, 0x34);
  1041. /* Turn off charger block */
  1042. chg_det |= ~(1 << 1);
  1043. ulpi_write(phy, chg_det, 0x34);
  1044. break;
  1045. case SNPS_28NM_INTEGRATED_PHY:
  1046. /* Clear charger detecting control bits */
  1047. ulpi_write(phy, 0x3F, 0x86);
  1048. /* Clear alt interrupt latch and enable bits */
  1049. ulpi_write(phy, 0x1F, 0x92);
  1050. ulpi_write(phy, 0x1F, 0x95);
  1051. break;
  1052. default:
  1053. break;
  1054. }
  1055. /* put the controller in normal mode */
  1056. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  1057. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  1058. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  1059. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  1060. }
  1061. #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
  1062. #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
  1063. #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
  1064. #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
  1065. static void msm_chg_detect_work(struct work_struct *w)
  1066. {
  1067. struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
  1068. struct usb_phy *phy = &motg->phy;
  1069. bool is_dcd, tmout, vout;
  1070. unsigned long delay;
  1071. dev_dbg(phy->dev, "chg detection work\n");
  1072. switch (motg->chg_state) {
  1073. case USB_CHG_STATE_UNDEFINED:
  1074. pm_runtime_get_sync(phy->dev);
  1075. msm_chg_block_on(motg);
  1076. msm_chg_enable_dcd(motg);
  1077. motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  1078. motg->dcd_retries = 0;
  1079. delay = MSM_CHG_DCD_POLL_TIME;
  1080. break;
  1081. case USB_CHG_STATE_WAIT_FOR_DCD:
  1082. is_dcd = msm_chg_check_dcd(motg);
  1083. tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
  1084. if (is_dcd || tmout) {
  1085. msm_chg_disable_dcd(motg);
  1086. msm_chg_enable_primary_det(motg);
  1087. delay = MSM_CHG_PRIMARY_DET_TIME;
  1088. motg->chg_state = USB_CHG_STATE_DCD_DONE;
  1089. } else {
  1090. delay = MSM_CHG_DCD_POLL_TIME;
  1091. }
  1092. break;
  1093. case USB_CHG_STATE_DCD_DONE:
  1094. vout = msm_chg_check_primary_det(motg);
  1095. if (vout) {
  1096. msm_chg_enable_secondary_det(motg);
  1097. delay = MSM_CHG_SECONDARY_DET_TIME;
  1098. motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  1099. } else {
  1100. motg->chg_type = USB_SDP_CHARGER;
  1101. motg->chg_state = USB_CHG_STATE_DETECTED;
  1102. delay = 0;
  1103. }
  1104. break;
  1105. case USB_CHG_STATE_PRIMARY_DONE:
  1106. vout = msm_chg_check_secondary_det(motg);
  1107. if (vout)
  1108. motg->chg_type = USB_DCP_CHARGER;
  1109. else
  1110. motg->chg_type = USB_CDP_CHARGER;
  1111. motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
  1112. /* fall through */
  1113. case USB_CHG_STATE_SECONDARY_DONE:
  1114. motg->chg_state = USB_CHG_STATE_DETECTED;
  1115. case USB_CHG_STATE_DETECTED:
  1116. msm_chg_block_off(motg);
  1117. dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
  1118. schedule_work(&motg->sm_work);
  1119. return;
  1120. default:
  1121. return;
  1122. }
  1123. schedule_delayed_work(&motg->chg_work, delay);
  1124. }
  1125. /*
  1126. * We support OTG, Peripheral only and Host only configurations. In case
  1127. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  1128. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  1129. * enabled when switch is controlled by user and default mode is supplied
  1130. * by board file, which can be changed by userspace later.
  1131. */
  1132. static void msm_otg_init_sm(struct msm_otg *motg)
  1133. {
  1134. struct msm_otg_platform_data *pdata = motg->pdata;
  1135. u32 otgsc = readl(USB_OTGSC);
  1136. switch (pdata->mode) {
  1137. case USB_DR_MODE_OTG:
  1138. if (pdata->otg_control == OTG_PHY_CONTROL) {
  1139. if (otgsc & OTGSC_ID)
  1140. set_bit(ID, &motg->inputs);
  1141. else
  1142. clear_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. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  1148. set_bit(ID, &motg->inputs);
  1149. clear_bit(B_SESS_VLD, &motg->inputs);
  1150. }
  1151. break;
  1152. case USB_DR_MODE_HOST:
  1153. clear_bit(ID, &motg->inputs);
  1154. break;
  1155. case USB_DR_MODE_PERIPHERAL:
  1156. set_bit(ID, &motg->inputs);
  1157. if (otgsc & OTGSC_BSV)
  1158. set_bit(B_SESS_VLD, &motg->inputs);
  1159. else
  1160. clear_bit(B_SESS_VLD, &motg->inputs);
  1161. break;
  1162. default:
  1163. break;
  1164. }
  1165. }
  1166. static void msm_otg_sm_work(struct work_struct *w)
  1167. {
  1168. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  1169. struct usb_otg *otg = motg->phy.otg;
  1170. switch (otg->state) {
  1171. case OTG_STATE_UNDEFINED:
  1172. dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
  1173. msm_otg_reset(otg->usb_phy);
  1174. msm_otg_init_sm(motg);
  1175. otg->state = OTG_STATE_B_IDLE;
  1176. /* FALL THROUGH */
  1177. case OTG_STATE_B_IDLE:
  1178. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
  1179. if (!test_bit(ID, &motg->inputs) && otg->host) {
  1180. /* disable BSV bit */
  1181. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  1182. msm_otg_start_host(otg->usb_phy, 1);
  1183. otg->state = OTG_STATE_A_HOST;
  1184. } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1185. switch (motg->chg_state) {
  1186. case USB_CHG_STATE_UNDEFINED:
  1187. msm_chg_detect_work(&motg->chg_work.work);
  1188. break;
  1189. case USB_CHG_STATE_DETECTED:
  1190. switch (motg->chg_type) {
  1191. case USB_DCP_CHARGER:
  1192. msm_otg_notify_charger(motg,
  1193. IDEV_CHG_MAX);
  1194. break;
  1195. case USB_CDP_CHARGER:
  1196. msm_otg_notify_charger(motg,
  1197. IDEV_CHG_MAX);
  1198. msm_otg_start_peripheral(otg->usb_phy,
  1199. 1);
  1200. otg->state
  1201. = OTG_STATE_B_PERIPHERAL;
  1202. break;
  1203. case USB_SDP_CHARGER:
  1204. msm_otg_notify_charger(motg, IUNIT);
  1205. msm_otg_start_peripheral(otg->usb_phy,
  1206. 1);
  1207. otg->state
  1208. = OTG_STATE_B_PERIPHERAL;
  1209. break;
  1210. default:
  1211. break;
  1212. }
  1213. break;
  1214. default:
  1215. break;
  1216. }
  1217. } else {
  1218. /*
  1219. * If charger detection work is pending, decrement
  1220. * the pm usage counter to balance with the one that
  1221. * is incremented in charger detection work.
  1222. */
  1223. if (cancel_delayed_work_sync(&motg->chg_work)) {
  1224. pm_runtime_put_sync(otg->usb_phy->dev);
  1225. msm_otg_reset(otg->usb_phy);
  1226. }
  1227. msm_otg_notify_charger(motg, 0);
  1228. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1229. motg->chg_type = USB_INVALID_CHARGER;
  1230. }
  1231. if (otg->state == OTG_STATE_B_IDLE)
  1232. pm_runtime_put_sync(otg->usb_phy->dev);
  1233. break;
  1234. case OTG_STATE_B_PERIPHERAL:
  1235. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
  1236. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  1237. !test_bit(ID, &motg->inputs)) {
  1238. msm_otg_notify_charger(motg, 0);
  1239. msm_otg_start_peripheral(otg->usb_phy, 0);
  1240. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1241. motg->chg_type = USB_INVALID_CHARGER;
  1242. otg->state = OTG_STATE_B_IDLE;
  1243. msm_otg_reset(otg->usb_phy);
  1244. schedule_work(w);
  1245. }
  1246. break;
  1247. case OTG_STATE_A_HOST:
  1248. dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
  1249. if (test_bit(ID, &motg->inputs)) {
  1250. msm_otg_start_host(otg->usb_phy, 0);
  1251. otg->state = OTG_STATE_B_IDLE;
  1252. msm_otg_reset(otg->usb_phy);
  1253. schedule_work(w);
  1254. }
  1255. break;
  1256. default:
  1257. break;
  1258. }
  1259. }
  1260. static irqreturn_t msm_otg_irq(int irq, void *data)
  1261. {
  1262. struct msm_otg *motg = data;
  1263. struct usb_phy *phy = &motg->phy;
  1264. u32 otgsc = 0;
  1265. if (atomic_read(&motg->in_lpm)) {
  1266. disable_irq_nosync(irq);
  1267. motg->async_int = 1;
  1268. pm_runtime_get(phy->dev);
  1269. return IRQ_HANDLED;
  1270. }
  1271. otgsc = readl(USB_OTGSC);
  1272. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  1273. return IRQ_NONE;
  1274. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  1275. if (otgsc & OTGSC_ID)
  1276. set_bit(ID, &motg->inputs);
  1277. else
  1278. clear_bit(ID, &motg->inputs);
  1279. dev_dbg(phy->dev, "ID set/clear\n");
  1280. pm_runtime_get_noresume(phy->dev);
  1281. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  1282. if (otgsc & OTGSC_BSV)
  1283. set_bit(B_SESS_VLD, &motg->inputs);
  1284. else
  1285. clear_bit(B_SESS_VLD, &motg->inputs);
  1286. dev_dbg(phy->dev, "BSV set/clear\n");
  1287. pm_runtime_get_noresume(phy->dev);
  1288. }
  1289. writel(otgsc, USB_OTGSC);
  1290. schedule_work(&motg->sm_work);
  1291. return IRQ_HANDLED;
  1292. }
  1293. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  1294. {
  1295. struct msm_otg *motg = s->private;
  1296. struct usb_otg *otg = motg->phy.otg;
  1297. switch (otg->state) {
  1298. case OTG_STATE_A_HOST:
  1299. seq_puts(s, "host\n");
  1300. break;
  1301. case OTG_STATE_B_PERIPHERAL:
  1302. seq_puts(s, "peripheral\n");
  1303. break;
  1304. default:
  1305. seq_puts(s, "none\n");
  1306. break;
  1307. }
  1308. return 0;
  1309. }
  1310. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  1311. {
  1312. return single_open(file, msm_otg_mode_show, inode->i_private);
  1313. }
  1314. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  1315. size_t count, loff_t *ppos)
  1316. {
  1317. struct seq_file *s = file->private_data;
  1318. struct msm_otg *motg = s->private;
  1319. char buf[16];
  1320. struct usb_otg *otg = motg->phy.otg;
  1321. int status = count;
  1322. enum usb_dr_mode req_mode;
  1323. memset(buf, 0x00, sizeof(buf));
  1324. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  1325. status = -EFAULT;
  1326. goto out;
  1327. }
  1328. if (!strncmp(buf, "host", 4)) {
  1329. req_mode = USB_DR_MODE_HOST;
  1330. } else if (!strncmp(buf, "peripheral", 10)) {
  1331. req_mode = USB_DR_MODE_PERIPHERAL;
  1332. } else if (!strncmp(buf, "none", 4)) {
  1333. req_mode = USB_DR_MODE_UNKNOWN;
  1334. } else {
  1335. status = -EINVAL;
  1336. goto out;
  1337. }
  1338. switch (req_mode) {
  1339. case USB_DR_MODE_UNKNOWN:
  1340. switch (otg->state) {
  1341. case OTG_STATE_A_HOST:
  1342. case OTG_STATE_B_PERIPHERAL:
  1343. set_bit(ID, &motg->inputs);
  1344. clear_bit(B_SESS_VLD, &motg->inputs);
  1345. break;
  1346. default:
  1347. goto out;
  1348. }
  1349. break;
  1350. case USB_DR_MODE_PERIPHERAL:
  1351. switch (otg->state) {
  1352. case OTG_STATE_B_IDLE:
  1353. case OTG_STATE_A_HOST:
  1354. set_bit(ID, &motg->inputs);
  1355. set_bit(B_SESS_VLD, &motg->inputs);
  1356. break;
  1357. default:
  1358. goto out;
  1359. }
  1360. break;
  1361. case USB_DR_MODE_HOST:
  1362. switch (otg->state) {
  1363. case OTG_STATE_B_IDLE:
  1364. case OTG_STATE_B_PERIPHERAL:
  1365. clear_bit(ID, &motg->inputs);
  1366. break;
  1367. default:
  1368. goto out;
  1369. }
  1370. break;
  1371. default:
  1372. goto out;
  1373. }
  1374. pm_runtime_get_sync(otg->usb_phy->dev);
  1375. schedule_work(&motg->sm_work);
  1376. out:
  1377. return status;
  1378. }
  1379. static const struct file_operations msm_otg_mode_fops = {
  1380. .open = msm_otg_mode_open,
  1381. .read = seq_read,
  1382. .write = msm_otg_mode_write,
  1383. .llseek = seq_lseek,
  1384. .release = single_release,
  1385. };
  1386. static struct dentry *msm_otg_dbg_root;
  1387. static struct dentry *msm_otg_dbg_mode;
  1388. static int msm_otg_debugfs_init(struct msm_otg *motg)
  1389. {
  1390. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  1391. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  1392. return -ENODEV;
  1393. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  1394. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  1395. if (!msm_otg_dbg_mode) {
  1396. debugfs_remove(msm_otg_dbg_root);
  1397. msm_otg_dbg_root = NULL;
  1398. return -ENODEV;
  1399. }
  1400. return 0;
  1401. }
  1402. static void msm_otg_debugfs_cleanup(void)
  1403. {
  1404. debugfs_remove(msm_otg_dbg_mode);
  1405. debugfs_remove(msm_otg_dbg_root);
  1406. }
  1407. static const struct of_device_id msm_otg_dt_match[] = {
  1408. {
  1409. .compatible = "qcom,usb-otg-ci",
  1410. .data = (void *) CI_45NM_INTEGRATED_PHY
  1411. },
  1412. {
  1413. .compatible = "qcom,usb-otg-snps",
  1414. .data = (void *) SNPS_28NM_INTEGRATED_PHY
  1415. },
  1416. { }
  1417. };
  1418. MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
  1419. static int msm_otg_vbus_notifier(struct notifier_block *nb, unsigned long event,
  1420. void *ptr)
  1421. {
  1422. struct msm_usb_cable *vbus = container_of(nb, struct msm_usb_cable, nb);
  1423. struct msm_otg *motg = container_of(vbus, struct msm_otg, vbus);
  1424. if (event)
  1425. set_bit(B_SESS_VLD, &motg->inputs);
  1426. else
  1427. clear_bit(B_SESS_VLD, &motg->inputs);
  1428. if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1429. /* Switch D+/D- lines to Device connector */
  1430. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1431. } else {
  1432. /* Switch D+/D- lines to Hub */
  1433. gpiod_set_value_cansleep(motg->switch_gpio, 1);
  1434. }
  1435. schedule_work(&motg->sm_work);
  1436. return NOTIFY_DONE;
  1437. }
  1438. static int msm_otg_id_notifier(struct notifier_block *nb, unsigned long event,
  1439. void *ptr)
  1440. {
  1441. struct msm_usb_cable *id = container_of(nb, struct msm_usb_cable, nb);
  1442. struct msm_otg *motg = container_of(id, struct msm_otg, id);
  1443. if (event)
  1444. clear_bit(ID, &motg->inputs);
  1445. else
  1446. set_bit(ID, &motg->inputs);
  1447. schedule_work(&motg->sm_work);
  1448. return NOTIFY_DONE;
  1449. }
  1450. static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
  1451. {
  1452. struct msm_otg_platform_data *pdata;
  1453. struct extcon_dev *ext_id, *ext_vbus;
  1454. struct device_node *node = pdev->dev.of_node;
  1455. struct property *prop;
  1456. int len, ret, words;
  1457. u32 val, tmp[3];
  1458. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1459. if (!pdata)
  1460. return -ENOMEM;
  1461. motg->pdata = pdata;
  1462. pdata->phy_type = (enum msm_usb_phy_type)of_device_get_match_data(&pdev->dev);
  1463. if (!pdata->phy_type)
  1464. return 1;
  1465. motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
  1466. if (IS_ERR(motg->link_rst))
  1467. return PTR_ERR(motg->link_rst);
  1468. motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
  1469. if (IS_ERR(motg->phy_rst))
  1470. motg->phy_rst = NULL;
  1471. pdata->mode = usb_get_dr_mode(&pdev->dev);
  1472. if (pdata->mode == USB_DR_MODE_UNKNOWN)
  1473. pdata->mode = USB_DR_MODE_OTG;
  1474. pdata->otg_control = OTG_PHY_CONTROL;
  1475. if (!of_property_read_u32(node, "qcom,otg-control", &val))
  1476. if (val == OTG_PMIC_CONTROL)
  1477. pdata->otg_control = val;
  1478. if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
  1479. motg->phy_number = val;
  1480. motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
  1481. motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
  1482. motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
  1483. if (of_get_property(node, "qcom,vdd-levels", &len) &&
  1484. len == sizeof(tmp)) {
  1485. of_property_read_u32_array(node, "qcom,vdd-levels",
  1486. tmp, len / sizeof(*tmp));
  1487. motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
  1488. motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
  1489. motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
  1490. }
  1491. motg->manual_pullup = of_property_read_bool(node, "qcom,manual-pullup");
  1492. motg->switch_gpio = devm_gpiod_get_optional(&pdev->dev, "switch",
  1493. GPIOD_OUT_LOW);
  1494. if (IS_ERR(motg->switch_gpio))
  1495. return PTR_ERR(motg->switch_gpio);
  1496. ext_id = ERR_PTR(-ENODEV);
  1497. ext_vbus = ERR_PTR(-ENODEV);
  1498. if (of_property_read_bool(node, "extcon")) {
  1499. /* Each one of them is not mandatory */
  1500. ext_vbus = extcon_get_edev_by_phandle(&pdev->dev, 0);
  1501. if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
  1502. return PTR_ERR(ext_vbus);
  1503. ext_id = extcon_get_edev_by_phandle(&pdev->dev, 1);
  1504. if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
  1505. return PTR_ERR(ext_id);
  1506. }
  1507. if (!IS_ERR(ext_vbus)) {
  1508. motg->vbus.extcon = ext_vbus;
  1509. motg->vbus.nb.notifier_call = msm_otg_vbus_notifier;
  1510. ret = extcon_register_notifier(ext_vbus, EXTCON_USB,
  1511. &motg->vbus.nb);
  1512. if (ret < 0) {
  1513. dev_err(&pdev->dev, "register VBUS notifier failed\n");
  1514. return ret;
  1515. }
  1516. ret = extcon_get_cable_state_(ext_vbus, EXTCON_USB);
  1517. if (ret)
  1518. set_bit(B_SESS_VLD, &motg->inputs);
  1519. else
  1520. clear_bit(B_SESS_VLD, &motg->inputs);
  1521. }
  1522. if (!IS_ERR(ext_id)) {
  1523. motg->id.extcon = ext_id;
  1524. motg->id.nb.notifier_call = msm_otg_id_notifier;
  1525. ret = extcon_register_notifier(ext_id, EXTCON_USB_HOST,
  1526. &motg->id.nb);
  1527. if (ret < 0) {
  1528. dev_err(&pdev->dev, "register ID notifier failed\n");
  1529. extcon_unregister_notifier(motg->vbus.extcon,
  1530. EXTCON_USB, &motg->vbus.nb);
  1531. return ret;
  1532. }
  1533. ret = extcon_get_cable_state_(ext_id, EXTCON_USB_HOST);
  1534. if (ret)
  1535. clear_bit(ID, &motg->inputs);
  1536. else
  1537. set_bit(ID, &motg->inputs);
  1538. }
  1539. prop = of_find_property(node, "qcom,phy-init-sequence", &len);
  1540. if (!prop || !len)
  1541. return 0;
  1542. words = len / sizeof(u32);
  1543. if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
  1544. dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
  1545. return 0;
  1546. }
  1547. pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  1548. if (!pdata->phy_init_seq)
  1549. return 0;
  1550. ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
  1551. pdata->phy_init_seq, words);
  1552. if (!ret)
  1553. pdata->phy_init_sz = words;
  1554. return 0;
  1555. }
  1556. static int msm_otg_reboot_notify(struct notifier_block *this,
  1557. unsigned long code, void *unused)
  1558. {
  1559. struct msm_otg *motg = container_of(this, struct msm_otg, reboot);
  1560. /*
  1561. * Ensure that D+/D- lines are routed to uB connector, so
  1562. * we could load bootloader/kernel at next reboot
  1563. */
  1564. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1565. return NOTIFY_DONE;
  1566. }
  1567. static int msm_otg_probe(struct platform_device *pdev)
  1568. {
  1569. struct regulator_bulk_data regs[3];
  1570. int ret = 0;
  1571. struct device_node *np = pdev->dev.of_node;
  1572. struct msm_otg_platform_data *pdata;
  1573. struct resource *res;
  1574. struct msm_otg *motg;
  1575. struct usb_phy *phy;
  1576. void __iomem *phy_select;
  1577. motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
  1578. if (!motg)
  1579. return -ENOMEM;
  1580. motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  1581. GFP_KERNEL);
  1582. if (!motg->phy.otg)
  1583. return -ENOMEM;
  1584. phy = &motg->phy;
  1585. phy->dev = &pdev->dev;
  1586. motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
  1587. if (IS_ERR(motg->clk)) {
  1588. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  1589. return PTR_ERR(motg->clk);
  1590. }
  1591. /*
  1592. * If USB Core is running its protocol engine based on CORE CLK,
  1593. * CORE CLK must be running at >55Mhz for correct HSUSB
  1594. * operation and USB core cannot tolerate frequency changes on
  1595. * CORE CLK.
  1596. */
  1597. motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
  1598. if (IS_ERR(motg->pclk)) {
  1599. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  1600. return PTR_ERR(motg->pclk);
  1601. }
  1602. /*
  1603. * USB core clock is not present on all MSM chips. This
  1604. * clock is introduced to remove the dependency on AXI
  1605. * bus frequency.
  1606. */
  1607. motg->core_clk = devm_clk_get(&pdev->dev,
  1608. np ? "alt_core" : "usb_hs_core_clk");
  1609. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1610. if (!res)
  1611. return -EINVAL;
  1612. motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  1613. if (!motg->regs)
  1614. return -ENOMEM;
  1615. pdata = dev_get_platdata(&pdev->dev);
  1616. if (!pdata) {
  1617. if (!np)
  1618. return -ENXIO;
  1619. ret = msm_otg_read_dt(pdev, motg);
  1620. if (ret)
  1621. return ret;
  1622. }
  1623. /*
  1624. * NOTE: The PHYs can be multiplexed between the chipidea controller
  1625. * and the dwc3 controller, using a single bit. It is important that
  1626. * the dwc3 driver does not set this bit in an incompatible way.
  1627. */
  1628. if (motg->phy_number) {
  1629. phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
  1630. if (!phy_select) {
  1631. ret = -ENOMEM;
  1632. goto unregister_extcon;
  1633. }
  1634. /* Enable second PHY with the OTG port */
  1635. writel(0x1, phy_select);
  1636. }
  1637. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  1638. motg->irq = platform_get_irq(pdev, 0);
  1639. if (motg->irq < 0) {
  1640. dev_err(&pdev->dev, "platform_get_irq failed\n");
  1641. ret = motg->irq;
  1642. goto unregister_extcon;
  1643. }
  1644. regs[0].supply = "vddcx";
  1645. regs[1].supply = "v3p3";
  1646. regs[2].supply = "v1p8";
  1647. ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
  1648. if (ret)
  1649. goto unregister_extcon;
  1650. motg->vddcx = regs[0].consumer;
  1651. motg->v3p3 = regs[1].consumer;
  1652. motg->v1p8 = regs[2].consumer;
  1653. clk_set_rate(motg->clk, 60000000);
  1654. clk_prepare_enable(motg->clk);
  1655. clk_prepare_enable(motg->pclk);
  1656. if (!IS_ERR(motg->core_clk))
  1657. clk_prepare_enable(motg->core_clk);
  1658. ret = msm_hsusb_init_vddcx(motg, 1);
  1659. if (ret) {
  1660. dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
  1661. goto disable_clks;
  1662. }
  1663. ret = msm_hsusb_ldo_init(motg, 1);
  1664. if (ret) {
  1665. dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
  1666. goto disable_vddcx;
  1667. }
  1668. ret = msm_hsusb_ldo_set_mode(motg, 1);
  1669. if (ret) {
  1670. dev_err(&pdev->dev, "hsusb vreg enable failed\n");
  1671. goto disable_ldo;
  1672. }
  1673. writel(0, USB_USBINTR);
  1674. writel(0, USB_OTGSC);
  1675. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  1676. INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
  1677. ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
  1678. "msm_otg", motg);
  1679. if (ret) {
  1680. dev_err(&pdev->dev, "request irq failed\n");
  1681. goto disable_ldo;
  1682. }
  1683. phy->init = msm_phy_init;
  1684. phy->set_power = msm_otg_set_power;
  1685. phy->notify_disconnect = msm_phy_notify_disconnect;
  1686. phy->type = USB_PHY_TYPE_USB2;
  1687. phy->io_ops = &msm_otg_io_ops;
  1688. phy->otg->usb_phy = &motg->phy;
  1689. phy->otg->set_host = msm_otg_set_host;
  1690. phy->otg->set_peripheral = msm_otg_set_peripheral;
  1691. msm_usb_reset(phy);
  1692. ret = usb_add_phy_dev(&motg->phy);
  1693. if (ret) {
  1694. dev_err(&pdev->dev, "usb_add_phy failed\n");
  1695. goto disable_ldo;
  1696. }
  1697. platform_set_drvdata(pdev, motg);
  1698. device_init_wakeup(&pdev->dev, 1);
  1699. if (motg->pdata->mode == USB_DR_MODE_OTG &&
  1700. motg->pdata->otg_control == OTG_USER_CONTROL) {
  1701. ret = msm_otg_debugfs_init(motg);
  1702. if (ret)
  1703. dev_dbg(&pdev->dev, "Can not create mode change file\n");
  1704. }
  1705. if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1706. /* Switch D+/D- lines to Device connector */
  1707. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1708. } else {
  1709. /* Switch D+/D- lines to Hub */
  1710. gpiod_set_value_cansleep(motg->switch_gpio, 1);
  1711. }
  1712. motg->reboot.notifier_call = msm_otg_reboot_notify;
  1713. register_reboot_notifier(&motg->reboot);
  1714. pm_runtime_set_active(&pdev->dev);
  1715. pm_runtime_enable(&pdev->dev);
  1716. return 0;
  1717. disable_ldo:
  1718. msm_hsusb_ldo_init(motg, 0);
  1719. disable_vddcx:
  1720. msm_hsusb_init_vddcx(motg, 0);
  1721. disable_clks:
  1722. clk_disable_unprepare(motg->pclk);
  1723. clk_disable_unprepare(motg->clk);
  1724. if (!IS_ERR(motg->core_clk))
  1725. clk_disable_unprepare(motg->core_clk);
  1726. unregister_extcon:
  1727. extcon_unregister_notifier(motg->id.extcon,
  1728. EXTCON_USB_HOST, &motg->id.nb);
  1729. extcon_unregister_notifier(motg->vbus.extcon,
  1730. EXTCON_USB, &motg->vbus.nb);
  1731. return ret;
  1732. }
  1733. static int msm_otg_remove(struct platform_device *pdev)
  1734. {
  1735. struct msm_otg *motg = platform_get_drvdata(pdev);
  1736. struct usb_phy *phy = &motg->phy;
  1737. int cnt = 0;
  1738. if (phy->otg->host || phy->otg->gadget)
  1739. return -EBUSY;
  1740. unregister_reboot_notifier(&motg->reboot);
  1741. /*
  1742. * Ensure that D+/D- lines are routed to uB connector, so
  1743. * we could load bootloader/kernel at next reboot
  1744. */
  1745. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1746. extcon_unregister_notifier(motg->id.extcon, EXTCON_USB_HOST, &motg->id.nb);
  1747. extcon_unregister_notifier(motg->vbus.extcon, EXTCON_USB, &motg->vbus.nb);
  1748. msm_otg_debugfs_cleanup();
  1749. cancel_delayed_work_sync(&motg->chg_work);
  1750. cancel_work_sync(&motg->sm_work);
  1751. pm_runtime_resume(&pdev->dev);
  1752. device_init_wakeup(&pdev->dev, 0);
  1753. pm_runtime_disable(&pdev->dev);
  1754. usb_remove_phy(phy);
  1755. disable_irq(motg->irq);
  1756. /*
  1757. * Put PHY in low power mode.
  1758. */
  1759. ulpi_read(phy, 0x14);
  1760. ulpi_write(phy, 0x08, 0x09);
  1761. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  1762. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  1763. if (readl(USB_PORTSC) & PORTSC_PHCD)
  1764. break;
  1765. udelay(1);
  1766. cnt++;
  1767. }
  1768. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  1769. dev_err(phy->dev, "Unable to suspend PHY\n");
  1770. clk_disable_unprepare(motg->pclk);
  1771. clk_disable_unprepare(motg->clk);
  1772. if (!IS_ERR(motg->core_clk))
  1773. clk_disable_unprepare(motg->core_clk);
  1774. msm_hsusb_ldo_init(motg, 0);
  1775. pm_runtime_set_suspended(&pdev->dev);
  1776. return 0;
  1777. }
  1778. #ifdef CONFIG_PM
  1779. static int msm_otg_runtime_idle(struct device *dev)
  1780. {
  1781. struct msm_otg *motg = dev_get_drvdata(dev);
  1782. struct usb_otg *otg = motg->phy.otg;
  1783. dev_dbg(dev, "OTG runtime idle\n");
  1784. /*
  1785. * It is observed some times that a spurious interrupt
  1786. * comes when PHY is put into LPM immediately after PHY reset.
  1787. * This 1 sec delay also prevents entering into LPM immediately
  1788. * after asynchronous interrupt.
  1789. */
  1790. if (otg->state != OTG_STATE_UNDEFINED)
  1791. pm_schedule_suspend(dev, 1000);
  1792. return -EAGAIN;
  1793. }
  1794. static int msm_otg_runtime_suspend(struct device *dev)
  1795. {
  1796. struct msm_otg *motg = dev_get_drvdata(dev);
  1797. dev_dbg(dev, "OTG runtime suspend\n");
  1798. return msm_otg_suspend(motg);
  1799. }
  1800. static int msm_otg_runtime_resume(struct device *dev)
  1801. {
  1802. struct msm_otg *motg = dev_get_drvdata(dev);
  1803. dev_dbg(dev, "OTG runtime resume\n");
  1804. return msm_otg_resume(motg);
  1805. }
  1806. #endif
  1807. #ifdef CONFIG_PM_SLEEP
  1808. static int msm_otg_pm_suspend(struct device *dev)
  1809. {
  1810. struct msm_otg *motg = dev_get_drvdata(dev);
  1811. dev_dbg(dev, "OTG PM suspend\n");
  1812. return msm_otg_suspend(motg);
  1813. }
  1814. static int msm_otg_pm_resume(struct device *dev)
  1815. {
  1816. struct msm_otg *motg = dev_get_drvdata(dev);
  1817. int ret;
  1818. dev_dbg(dev, "OTG PM resume\n");
  1819. ret = msm_otg_resume(motg);
  1820. if (ret)
  1821. return ret;
  1822. /*
  1823. * Runtime PM Documentation recommends bringing the
  1824. * device to full powered state upon resume.
  1825. */
  1826. pm_runtime_disable(dev);
  1827. pm_runtime_set_active(dev);
  1828. pm_runtime_enable(dev);
  1829. return 0;
  1830. }
  1831. #endif
  1832. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  1833. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  1834. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  1835. msm_otg_runtime_idle)
  1836. };
  1837. static struct platform_driver msm_otg_driver = {
  1838. .probe = msm_otg_probe,
  1839. .remove = msm_otg_remove,
  1840. .driver = {
  1841. .name = DRIVER_NAME,
  1842. .pm = &msm_otg_dev_pm_ops,
  1843. .of_match_table = msm_otg_dt_match,
  1844. },
  1845. };
  1846. module_platform_driver(msm_otg_driver);
  1847. MODULE_LICENSE("GPL v2");
  1848. MODULE_DESCRIPTION("MSM USB transceiver driver");