phy-ab8500-usb.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * USB transceiver driver for AB8500 family chips
  4. *
  5. * Copyright (C) 2010-2013 ST-Ericsson AB
  6. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  7. * Avinash Kumar <avinash.kumar@stericsson.com>
  8. * Thirupathi Chippakurthy <thirupathi.chippakurthy@stericsson.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/usb/otg.h>
  13. #include <linux/slab.h>
  14. #include <linux/notifier.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/mfd/abx500.h>
  20. #include <linux/mfd/abx500/ab8500.h>
  21. #include <linux/usb/musb-ux500.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/pinctrl/consumer.h>
  24. /* Bank AB8500_SYS_CTRL2_BLOCK */
  25. #define AB8500_MAIN_WD_CTRL_REG 0x01
  26. /* Bank AB8500_USB */
  27. #define AB8500_USB_LINE_STAT_REG 0x80
  28. #define AB8505_USB_LINE_STAT_REG 0x94
  29. #define AB8540_USB_LINK_STAT_REG 0x94
  30. #define AB9540_USB_LINK_STAT_REG 0x94
  31. #define AB8540_USB_OTG_CTL_REG 0x87
  32. #define AB8500_USB_PHY_CTRL_REG 0x8A
  33. #define AB8540_VBUS_CTRL_REG 0x82
  34. /* Bank AB8500_DEVELOPMENT */
  35. #define AB8500_BANK12_ACCESS 0x00
  36. /* Bank AB8500_DEBUG */
  37. #define AB8540_DEBUG 0x32
  38. #define AB8500_USB_PHY_TUNE1 0x05
  39. #define AB8500_USB_PHY_TUNE2 0x06
  40. #define AB8500_USB_PHY_TUNE3 0x07
  41. /* Bank AB8500_INTERRUPT */
  42. #define AB8500_IT_SOURCE2_REG 0x01
  43. #define AB8500_BIT_OTG_STAT_ID (1 << 0)
  44. #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
  45. #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
  46. #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
  47. #define AB8500_BIT_WD_CTRL_KICK (1 << 1)
  48. #define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
  49. #define AB8540_BIT_OTG_CTL_VBUS_VALID_ENA (1 << 0)
  50. #define AB8540_BIT_OTG_CTL_ID_HOST_ENA (1 << 1)
  51. #define AB8540_BIT_OTG_CTL_ID_DEV_ENA (1 << 5)
  52. #define AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA (1 << 0)
  53. #define AB8500_WD_KICK_DELAY_US 100 /* usec */
  54. #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
  55. #define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
  56. /* Usb line status register */
  57. enum ab8500_usb_link_status {
  58. USB_LINK_NOT_CONFIGURED_8500 = 0,
  59. USB_LINK_STD_HOST_NC_8500,
  60. USB_LINK_STD_HOST_C_NS_8500,
  61. USB_LINK_STD_HOST_C_S_8500,
  62. USB_LINK_HOST_CHG_NM_8500,
  63. USB_LINK_HOST_CHG_HS_8500,
  64. USB_LINK_HOST_CHG_HS_CHIRP_8500,
  65. USB_LINK_DEDICATED_CHG_8500,
  66. USB_LINK_ACA_RID_A_8500,
  67. USB_LINK_ACA_RID_B_8500,
  68. USB_LINK_ACA_RID_C_NM_8500,
  69. USB_LINK_ACA_RID_C_HS_8500,
  70. USB_LINK_ACA_RID_C_HS_CHIRP_8500,
  71. USB_LINK_HM_IDGND_8500,
  72. USB_LINK_RESERVED_8500,
  73. USB_LINK_NOT_VALID_LINK_8500,
  74. };
  75. enum ab8505_usb_link_status {
  76. USB_LINK_NOT_CONFIGURED_8505 = 0,
  77. USB_LINK_STD_HOST_NC_8505,
  78. USB_LINK_STD_HOST_C_NS_8505,
  79. USB_LINK_STD_HOST_C_S_8505,
  80. USB_LINK_CDP_8505,
  81. USB_LINK_RESERVED0_8505,
  82. USB_LINK_RESERVED1_8505,
  83. USB_LINK_DEDICATED_CHG_8505,
  84. USB_LINK_ACA_RID_A_8505,
  85. USB_LINK_ACA_RID_B_8505,
  86. USB_LINK_ACA_RID_C_NM_8505,
  87. USB_LINK_RESERVED2_8505,
  88. USB_LINK_RESERVED3_8505,
  89. USB_LINK_HM_IDGND_8505,
  90. USB_LINK_CHARGERPORT_NOT_OK_8505,
  91. USB_LINK_CHARGER_DM_HIGH_8505,
  92. USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
  93. USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
  94. USB_LINK_STD_UPSTREAM_8505,
  95. USB_LINK_CHARGER_SE1_8505,
  96. USB_LINK_CARKIT_CHGR_1_8505,
  97. USB_LINK_CARKIT_CHGR_2_8505,
  98. USB_LINK_ACA_DOCK_CHGR_8505,
  99. USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
  100. USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
  101. USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
  102. USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
  103. USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
  104. };
  105. enum ab8540_usb_link_status {
  106. USB_LINK_NOT_CONFIGURED_8540 = 0,
  107. USB_LINK_STD_HOST_NC_8540,
  108. USB_LINK_STD_HOST_C_NS_8540,
  109. USB_LINK_STD_HOST_C_S_8540,
  110. USB_LINK_CDP_8540,
  111. USB_LINK_RESERVED0_8540,
  112. USB_LINK_RESERVED1_8540,
  113. USB_LINK_DEDICATED_CHG_8540,
  114. USB_LINK_ACA_RID_A_8540,
  115. USB_LINK_ACA_RID_B_8540,
  116. USB_LINK_ACA_RID_C_NM_8540,
  117. USB_LINK_RESERVED2_8540,
  118. USB_LINK_RESERVED3_8540,
  119. USB_LINK_HM_IDGND_8540,
  120. USB_LINK_CHARGERPORT_NOT_OK_8540,
  121. USB_LINK_CHARGER_DM_HIGH_8540,
  122. USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540,
  123. USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540,
  124. USB_LINK_STD_UPSTREAM_8540,
  125. USB_LINK_CHARGER_SE1_8540,
  126. USB_LINK_CARKIT_CHGR_1_8540,
  127. USB_LINK_CARKIT_CHGR_2_8540,
  128. USB_LINK_ACA_DOCK_CHGR_8540,
  129. USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8540,
  130. USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8540,
  131. USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8540,
  132. USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8540,
  133. USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8540
  134. };
  135. enum ab9540_usb_link_status {
  136. USB_LINK_NOT_CONFIGURED_9540 = 0,
  137. USB_LINK_STD_HOST_NC_9540,
  138. USB_LINK_STD_HOST_C_NS_9540,
  139. USB_LINK_STD_HOST_C_S_9540,
  140. USB_LINK_CDP_9540,
  141. USB_LINK_RESERVED0_9540,
  142. USB_LINK_RESERVED1_9540,
  143. USB_LINK_DEDICATED_CHG_9540,
  144. USB_LINK_ACA_RID_A_9540,
  145. USB_LINK_ACA_RID_B_9540,
  146. USB_LINK_ACA_RID_C_NM_9540,
  147. USB_LINK_RESERVED2_9540,
  148. USB_LINK_RESERVED3_9540,
  149. USB_LINK_HM_IDGND_9540,
  150. USB_LINK_CHARGERPORT_NOT_OK_9540,
  151. USB_LINK_CHARGER_DM_HIGH_9540,
  152. USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540,
  153. USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_9540,
  154. USB_LINK_STD_UPSTREAM_9540,
  155. USB_LINK_CHARGER_SE1_9540,
  156. USB_LINK_CARKIT_CHGR_1_9540,
  157. USB_LINK_CARKIT_CHGR_2_9540,
  158. USB_LINK_ACA_DOCK_CHGR_9540,
  159. USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_9540,
  160. USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_9540,
  161. USB_LINK_SAMSUNG_UART_CBL_PHY_EN_9540,
  162. USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_9540,
  163. USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_9540
  164. };
  165. enum ab8500_usb_mode {
  166. USB_IDLE = 0,
  167. USB_PERIPHERAL,
  168. USB_HOST,
  169. USB_DEDICATED_CHG
  170. };
  171. /* Register USB_LINK_STATUS interrupt */
  172. #define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ (1 << 0)
  173. /* Register ID_WAKEUP_F interrupt */
  174. #define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ (1 << 1)
  175. /* Register VBUS_DET_F interrupt */
  176. #define AB8500_USB_FLAG_USE_VBUS_DET_IRQ (1 << 2)
  177. /* Driver is using the ab-iddet driver*/
  178. #define AB8500_USB_FLAG_USE_AB_IDDET (1 << 3)
  179. /* Enable setting regulators voltage */
  180. #define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE (1 << 4)
  181. /* Enable the check_vbus_status workaround */
  182. #define AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS (1 << 5)
  183. /* Enable the vbus host workaround */
  184. #define AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK (1 << 6)
  185. struct ab8500_usb {
  186. struct usb_phy phy;
  187. struct device *dev;
  188. struct ab8500 *ab8500;
  189. unsigned vbus_draw;
  190. struct work_struct phy_dis_work;
  191. struct work_struct vbus_event_work;
  192. enum ab8500_usb_mode mode;
  193. struct clk *sysclk;
  194. struct regulator *v_ape;
  195. struct regulator *v_musb;
  196. struct regulator *v_ulpi;
  197. int saved_v_ulpi;
  198. int previous_link_status_state;
  199. struct pinctrl *pinctrl;
  200. struct pinctrl_state *pins_sleep;
  201. bool enabled_charging_detection;
  202. unsigned int flags;
  203. };
  204. static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
  205. {
  206. return container_of(x, struct ab8500_usb, phy);
  207. }
  208. static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
  209. {
  210. abx500_set_register_interruptible(ab->dev,
  211. AB8500_SYS_CTRL2_BLOCK,
  212. AB8500_MAIN_WD_CTRL_REG,
  213. AB8500_BIT_WD_CTRL_ENABLE);
  214. udelay(AB8500_WD_KICK_DELAY_US);
  215. abx500_set_register_interruptible(ab->dev,
  216. AB8500_SYS_CTRL2_BLOCK,
  217. AB8500_MAIN_WD_CTRL_REG,
  218. (AB8500_BIT_WD_CTRL_ENABLE
  219. | AB8500_BIT_WD_CTRL_KICK));
  220. udelay(AB8500_WD_V11_DISABLE_DELAY_US);
  221. abx500_set_register_interruptible(ab->dev,
  222. AB8500_SYS_CTRL2_BLOCK,
  223. AB8500_MAIN_WD_CTRL_REG,
  224. 0);
  225. }
  226. static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
  227. {
  228. int ret, volt;
  229. ret = regulator_enable(ab->v_ape);
  230. if (ret)
  231. dev_err(ab->dev, "Failed to enable v-ape\n");
  232. if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
  233. ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
  234. if (ab->saved_v_ulpi < 0)
  235. dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
  236. ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
  237. if (ret < 0)
  238. dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
  239. ret);
  240. ret = regulator_set_load(ab->v_ulpi, 28000);
  241. if (ret < 0)
  242. dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
  243. ret);
  244. }
  245. ret = regulator_enable(ab->v_ulpi);
  246. if (ret)
  247. dev_err(ab->dev, "Failed to enable vddulpivio18\n");
  248. if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
  249. volt = regulator_get_voltage(ab->v_ulpi);
  250. if ((volt != 1300000) && (volt != 1350000))
  251. dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
  252. volt);
  253. }
  254. ret = regulator_enable(ab->v_musb);
  255. if (ret)
  256. dev_err(ab->dev, "Failed to enable musb_1v8\n");
  257. }
  258. static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
  259. {
  260. int ret;
  261. regulator_disable(ab->v_musb);
  262. regulator_disable(ab->v_ulpi);
  263. /* USB is not the only consumer of Vintcore, restore old settings */
  264. if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
  265. if (ab->saved_v_ulpi > 0) {
  266. ret = regulator_set_voltage(ab->v_ulpi,
  267. ab->saved_v_ulpi, ab->saved_v_ulpi);
  268. if (ret < 0)
  269. dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
  270. ab->saved_v_ulpi, ret);
  271. }
  272. ret = regulator_set_load(ab->v_ulpi, 0);
  273. if (ret < 0)
  274. dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
  275. ret);
  276. }
  277. regulator_disable(ab->v_ape);
  278. }
  279. static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
  280. {
  281. /* Workaround for v2.0 bug # 31952 */
  282. if (is_ab8500_2p0(ab->ab8500)) {
  283. abx500_mask_and_set_register_interruptible(ab->dev,
  284. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  285. bit, bit);
  286. udelay(AB8500_V20_31952_DISABLE_DELAY_US);
  287. }
  288. }
  289. static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
  290. {
  291. u8 bit;
  292. bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
  293. AB8500_BIT_PHY_CTRL_DEVICE_EN;
  294. /* mux and configure USB pins to DEFAULT state */
  295. ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
  296. if (IS_ERR(ab->pinctrl))
  297. dev_err(ab->dev, "could not get/set default pinstate\n");
  298. if (clk_prepare_enable(ab->sysclk))
  299. dev_err(ab->dev, "can't prepare/enable clock\n");
  300. ab8500_usb_regulator_enable(ab);
  301. abx500_mask_and_set_register_interruptible(ab->dev,
  302. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  303. bit, bit);
  304. if (ab->flags & AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK) {
  305. if (sel_host)
  306. abx500_set_register_interruptible(ab->dev,
  307. AB8500_USB, AB8540_USB_OTG_CTL_REG,
  308. AB8540_BIT_OTG_CTL_VBUS_VALID_ENA |
  309. AB8540_BIT_OTG_CTL_ID_HOST_ENA |
  310. AB8540_BIT_OTG_CTL_ID_DEV_ENA);
  311. }
  312. }
  313. static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
  314. {
  315. u8 bit;
  316. bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
  317. AB8500_BIT_PHY_CTRL_DEVICE_EN;
  318. ab8500_usb_wd_linkstatus(ab, bit);
  319. abx500_mask_and_set_register_interruptible(ab->dev,
  320. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  321. bit, 0);
  322. /* Needed to disable the phy.*/
  323. ab8500_usb_wd_workaround(ab);
  324. clk_disable_unprepare(ab->sysclk);
  325. ab8500_usb_regulator_disable(ab);
  326. if (!IS_ERR(ab->pinctrl)) {
  327. /* configure USB pins to SLEEP state */
  328. ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
  329. PINCTRL_STATE_SLEEP);
  330. if (IS_ERR(ab->pins_sleep))
  331. dev_dbg(ab->dev, "could not get sleep pinstate\n");
  332. else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
  333. dev_err(ab->dev, "could not set pins to sleep state\n");
  334. /*
  335. * as USB pins are shared with iddet, release them to allow
  336. * iddet to request them
  337. */
  338. pinctrl_put(ab->pinctrl);
  339. }
  340. }
  341. #define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
  342. #define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
  343. #define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
  344. #define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
  345. static int ab9540_usb_link_status_update(struct ab8500_usb *ab,
  346. enum ab9540_usb_link_status lsts)
  347. {
  348. enum ux500_musb_vbus_id_status event = 0;
  349. dev_dbg(ab->dev, "ab9540_usb_link_status_update %d\n", lsts);
  350. if (ab->previous_link_status_state == USB_LINK_HM_IDGND_9540 &&
  351. (lsts == USB_LINK_STD_HOST_C_NS_9540 ||
  352. lsts == USB_LINK_STD_HOST_NC_9540))
  353. return 0;
  354. if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_9540 &&
  355. (lsts == USB_LINK_STD_HOST_NC_9540))
  356. return 0;
  357. ab->previous_link_status_state = lsts;
  358. switch (lsts) {
  359. case USB_LINK_ACA_RID_B_9540:
  360. event = UX500_MUSB_RIDB;
  361. case USB_LINK_NOT_CONFIGURED_9540:
  362. case USB_LINK_RESERVED0_9540:
  363. case USB_LINK_RESERVED1_9540:
  364. case USB_LINK_RESERVED2_9540:
  365. case USB_LINK_RESERVED3_9540:
  366. if (ab->mode == USB_PERIPHERAL)
  367. atomic_notifier_call_chain(&ab->phy.notifier,
  368. UX500_MUSB_CLEAN, &ab->vbus_draw);
  369. ab->mode = USB_IDLE;
  370. ab->phy.otg->default_a = false;
  371. ab->vbus_draw = 0;
  372. if (event != UX500_MUSB_RIDB)
  373. event = UX500_MUSB_NONE;
  374. /* Fallback to default B_IDLE as nothing is connected. */
  375. ab->phy.otg->state = OTG_STATE_B_IDLE;
  376. usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
  377. break;
  378. case USB_LINK_ACA_RID_C_NM_9540:
  379. event = UX500_MUSB_RIDC;
  380. case USB_LINK_STD_HOST_NC_9540:
  381. case USB_LINK_STD_HOST_C_NS_9540:
  382. case USB_LINK_STD_HOST_C_S_9540:
  383. case USB_LINK_CDP_9540:
  384. if (ab->mode == USB_HOST) {
  385. ab->mode = USB_PERIPHERAL;
  386. ab8500_usb_host_phy_dis(ab);
  387. ab8500_usb_peri_phy_en(ab);
  388. atomic_notifier_call_chain(&ab->phy.notifier,
  389. UX500_MUSB_PREPARE, &ab->vbus_draw);
  390. usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
  391. }
  392. if (ab->mode == USB_IDLE) {
  393. ab->mode = USB_PERIPHERAL;
  394. ab8500_usb_peri_phy_en(ab);
  395. atomic_notifier_call_chain(&ab->phy.notifier,
  396. UX500_MUSB_PREPARE, &ab->vbus_draw);
  397. usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
  398. }
  399. if (event != UX500_MUSB_RIDC)
  400. event = UX500_MUSB_VBUS;
  401. break;
  402. case USB_LINK_ACA_RID_A_9540:
  403. event = UX500_MUSB_RIDA;
  404. case USB_LINK_HM_IDGND_9540:
  405. case USB_LINK_STD_UPSTREAM_9540:
  406. if (ab->mode == USB_PERIPHERAL) {
  407. ab->mode = USB_HOST;
  408. ab8500_usb_peri_phy_dis(ab);
  409. ab8500_usb_host_phy_en(ab);
  410. atomic_notifier_call_chain(&ab->phy.notifier,
  411. UX500_MUSB_PREPARE, &ab->vbus_draw);
  412. }
  413. if (ab->mode == USB_IDLE) {
  414. ab->mode = USB_HOST;
  415. ab8500_usb_host_phy_en(ab);
  416. atomic_notifier_call_chain(&ab->phy.notifier,
  417. UX500_MUSB_PREPARE, &ab->vbus_draw);
  418. }
  419. ab->phy.otg->default_a = true;
  420. if (event != UX500_MUSB_RIDA)
  421. event = UX500_MUSB_ID;
  422. atomic_notifier_call_chain(&ab->phy.notifier,
  423. event, &ab->vbus_draw);
  424. break;
  425. case USB_LINK_DEDICATED_CHG_9540:
  426. ab->mode = USB_DEDICATED_CHG;
  427. event = UX500_MUSB_CHARGER;
  428. atomic_notifier_call_chain(&ab->phy.notifier,
  429. event, &ab->vbus_draw);
  430. usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
  431. break;
  432. case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540:
  433. case USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_9540:
  434. if (!(is_ab9540_2p0_or_earlier(ab->ab8500))) {
  435. event = UX500_MUSB_NONE;
  436. if (ab->mode == USB_HOST) {
  437. ab->phy.otg->default_a = false;
  438. ab->vbus_draw = 0;
  439. atomic_notifier_call_chain(&ab->phy.notifier,
  440. event, &ab->vbus_draw);
  441. ab8500_usb_host_phy_dis(ab);
  442. ab->mode = USB_IDLE;
  443. }
  444. if (ab->mode == USB_PERIPHERAL) {
  445. atomic_notifier_call_chain(&ab->phy.notifier,
  446. event, &ab->vbus_draw);
  447. ab8500_usb_peri_phy_dis(ab);
  448. atomic_notifier_call_chain(&ab->phy.notifier,
  449. UX500_MUSB_CLEAN,
  450. &ab->vbus_draw);
  451. ab->mode = USB_IDLE;
  452. ab->phy.otg->default_a = false;
  453. ab->vbus_draw = 0;
  454. usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
  455. }
  456. }
  457. break;
  458. default:
  459. break;
  460. }
  461. return 0;
  462. }
  463. static int ab8540_usb_link_status_update(struct ab8500_usb *ab,
  464. enum ab8540_usb_link_status lsts)
  465. {
  466. enum ux500_musb_vbus_id_status event = 0;
  467. dev_dbg(ab->dev, "ab8540_usb_link_status_update %d\n", lsts);
  468. if (ab->enabled_charging_detection) {
  469. /* Disable USB Charger detection */
  470. abx500_mask_and_set_register_interruptible(ab->dev,
  471. AB8500_USB, AB8540_VBUS_CTRL_REG,
  472. AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA, 0x00);
  473. ab->enabled_charging_detection = false;
  474. }
  475. /*
  476. * Spurious link_status interrupts are seen in case of a
  477. * disconnection of a device in IDGND and RIDA stage
  478. */
  479. if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8540 &&
  480. (lsts == USB_LINK_STD_HOST_C_NS_8540 ||
  481. lsts == USB_LINK_STD_HOST_NC_8540))
  482. return 0;
  483. if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8540 &&
  484. (lsts == USB_LINK_STD_HOST_NC_8540))
  485. return 0;
  486. ab->previous_link_status_state = lsts;
  487. switch (lsts) {
  488. case USB_LINK_ACA_RID_B_8540:
  489. event = UX500_MUSB_RIDB;
  490. case USB_LINK_NOT_CONFIGURED_8540:
  491. case USB_LINK_RESERVED0_8540:
  492. case USB_LINK_RESERVED1_8540:
  493. case USB_LINK_RESERVED2_8540:
  494. case USB_LINK_RESERVED3_8540:
  495. ab->mode = USB_IDLE;
  496. ab->phy.otg->default_a = false;
  497. ab->vbus_draw = 0;
  498. if (event != UX500_MUSB_RIDB)
  499. event = UX500_MUSB_NONE;
  500. /*
  501. * Fallback to default B_IDLE as nothing
  502. * is connected
  503. */
  504. ab->phy.otg->state = OTG_STATE_B_IDLE;
  505. usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
  506. break;
  507. case USB_LINK_ACA_RID_C_NM_8540:
  508. event = UX500_MUSB_RIDC;
  509. case USB_LINK_STD_HOST_NC_8540:
  510. case USB_LINK_STD_HOST_C_NS_8540:
  511. case USB_LINK_STD_HOST_C_S_8540:
  512. case USB_LINK_CDP_8540:
  513. if (ab->mode == USB_IDLE) {
  514. ab->mode = USB_PERIPHERAL;
  515. ab8500_usb_peri_phy_en(ab);
  516. atomic_notifier_call_chain(&ab->phy.notifier,
  517. UX500_MUSB_PREPARE, &ab->vbus_draw);
  518. usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
  519. }
  520. if (event != UX500_MUSB_RIDC)
  521. event = UX500_MUSB_VBUS;
  522. break;
  523. case USB_LINK_ACA_RID_A_8540:
  524. case USB_LINK_ACA_DOCK_CHGR_8540:
  525. event = UX500_MUSB_RIDA;
  526. case USB_LINK_HM_IDGND_8540:
  527. case USB_LINK_STD_UPSTREAM_8540:
  528. if (ab->mode == USB_IDLE) {
  529. ab->mode = USB_HOST;
  530. ab8500_usb_host_phy_en(ab);
  531. atomic_notifier_call_chain(&ab->phy.notifier,
  532. UX500_MUSB_PREPARE, &ab->vbus_draw);
  533. }
  534. ab->phy.otg->default_a = true;
  535. if (event != UX500_MUSB_RIDA)
  536. event = UX500_MUSB_ID;
  537. atomic_notifier_call_chain(&ab->phy.notifier,
  538. event, &ab->vbus_draw);
  539. break;
  540. case USB_LINK_DEDICATED_CHG_8540:
  541. ab->mode = USB_DEDICATED_CHG;
  542. event = UX500_MUSB_CHARGER;
  543. atomic_notifier_call_chain(&ab->phy.notifier,
  544. event, &ab->vbus_draw);
  545. usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
  546. break;
  547. case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540:
  548. case USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540:
  549. event = UX500_MUSB_NONE;
  550. if (ab->mode == USB_HOST) {
  551. ab->phy.otg->default_a = false;
  552. ab->vbus_draw = 0;
  553. atomic_notifier_call_chain(&ab->phy.notifier,
  554. event, &ab->vbus_draw);
  555. ab8500_usb_host_phy_dis(ab);
  556. ab->mode = USB_IDLE;
  557. }
  558. if (ab->mode == USB_PERIPHERAL) {
  559. atomic_notifier_call_chain(&ab->phy.notifier,
  560. event, &ab->vbus_draw);
  561. ab8500_usb_peri_phy_dis(ab);
  562. atomic_notifier_call_chain(&ab->phy.notifier,
  563. UX500_MUSB_CLEAN, &ab->vbus_draw);
  564. ab->mode = USB_IDLE;
  565. ab->phy.otg->default_a = false;
  566. ab->vbus_draw = 0;
  567. usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
  568. }
  569. break;
  570. default:
  571. event = UX500_MUSB_NONE;
  572. break;
  573. }
  574. return 0;
  575. }
  576. static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
  577. enum ab8505_usb_link_status lsts)
  578. {
  579. enum ux500_musb_vbus_id_status event = 0;
  580. dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
  581. /*
  582. * Spurious link_status interrupts are seen at the time of
  583. * disconnection of a device in RIDA state
  584. */
  585. if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
  586. (lsts == USB_LINK_STD_HOST_NC_8505))
  587. return 0;
  588. ab->previous_link_status_state = lsts;
  589. switch (lsts) {
  590. case USB_LINK_ACA_RID_B_8505:
  591. event = UX500_MUSB_RIDB;
  592. case USB_LINK_NOT_CONFIGURED_8505:
  593. case USB_LINK_RESERVED0_8505:
  594. case USB_LINK_RESERVED1_8505:
  595. case USB_LINK_RESERVED2_8505:
  596. case USB_LINK_RESERVED3_8505:
  597. ab->mode = USB_IDLE;
  598. ab->phy.otg->default_a = false;
  599. ab->vbus_draw = 0;
  600. if (event != UX500_MUSB_RIDB)
  601. event = UX500_MUSB_NONE;
  602. /*
  603. * Fallback to default B_IDLE as nothing
  604. * is connected
  605. */
  606. ab->phy.otg->state = OTG_STATE_B_IDLE;
  607. usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
  608. break;
  609. case USB_LINK_ACA_RID_C_NM_8505:
  610. event = UX500_MUSB_RIDC;
  611. case USB_LINK_STD_HOST_NC_8505:
  612. case USB_LINK_STD_HOST_C_NS_8505:
  613. case USB_LINK_STD_HOST_C_S_8505:
  614. case USB_LINK_CDP_8505:
  615. if (ab->mode == USB_IDLE) {
  616. ab->mode = USB_PERIPHERAL;
  617. ab8500_usb_peri_phy_en(ab);
  618. atomic_notifier_call_chain(&ab->phy.notifier,
  619. UX500_MUSB_PREPARE, &ab->vbus_draw);
  620. usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
  621. }
  622. if (event != UX500_MUSB_RIDC)
  623. event = UX500_MUSB_VBUS;
  624. break;
  625. case USB_LINK_ACA_RID_A_8505:
  626. case USB_LINK_ACA_DOCK_CHGR_8505:
  627. event = UX500_MUSB_RIDA;
  628. case USB_LINK_HM_IDGND_8505:
  629. if (ab->mode == USB_IDLE) {
  630. ab->mode = USB_HOST;
  631. ab8500_usb_host_phy_en(ab);
  632. atomic_notifier_call_chain(&ab->phy.notifier,
  633. UX500_MUSB_PREPARE, &ab->vbus_draw);
  634. }
  635. ab->phy.otg->default_a = true;
  636. if (event != UX500_MUSB_RIDA)
  637. event = UX500_MUSB_ID;
  638. atomic_notifier_call_chain(&ab->phy.notifier,
  639. event, &ab->vbus_draw);
  640. break;
  641. case USB_LINK_DEDICATED_CHG_8505:
  642. ab->mode = USB_DEDICATED_CHG;
  643. event = UX500_MUSB_CHARGER;
  644. atomic_notifier_call_chain(&ab->phy.notifier,
  645. event, &ab->vbus_draw);
  646. usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
  647. break;
  648. default:
  649. break;
  650. }
  651. return 0;
  652. }
  653. static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
  654. enum ab8500_usb_link_status lsts)
  655. {
  656. enum ux500_musb_vbus_id_status event = 0;
  657. dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
  658. /*
  659. * Spurious link_status interrupts are seen in case of a
  660. * disconnection of a device in IDGND and RIDA stage
  661. */
  662. if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
  663. (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
  664. lsts == USB_LINK_STD_HOST_NC_8500))
  665. return 0;
  666. if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
  667. lsts == USB_LINK_STD_HOST_NC_8500)
  668. return 0;
  669. ab->previous_link_status_state = lsts;
  670. switch (lsts) {
  671. case USB_LINK_ACA_RID_B_8500:
  672. event = UX500_MUSB_RIDB;
  673. case USB_LINK_NOT_CONFIGURED_8500:
  674. case USB_LINK_NOT_VALID_LINK_8500:
  675. ab->mode = USB_IDLE;
  676. ab->phy.otg->default_a = false;
  677. ab->vbus_draw = 0;
  678. if (event != UX500_MUSB_RIDB)
  679. event = UX500_MUSB_NONE;
  680. /* Fallback to default B_IDLE as nothing is connected */
  681. ab->phy.otg->state = OTG_STATE_B_IDLE;
  682. usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
  683. break;
  684. case USB_LINK_ACA_RID_C_NM_8500:
  685. case USB_LINK_ACA_RID_C_HS_8500:
  686. case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
  687. event = UX500_MUSB_RIDC;
  688. case USB_LINK_STD_HOST_NC_8500:
  689. case USB_LINK_STD_HOST_C_NS_8500:
  690. case USB_LINK_STD_HOST_C_S_8500:
  691. case USB_LINK_HOST_CHG_NM_8500:
  692. case USB_LINK_HOST_CHG_HS_8500:
  693. case USB_LINK_HOST_CHG_HS_CHIRP_8500:
  694. if (ab->mode == USB_IDLE) {
  695. ab->mode = USB_PERIPHERAL;
  696. ab8500_usb_peri_phy_en(ab);
  697. atomic_notifier_call_chain(&ab->phy.notifier,
  698. UX500_MUSB_PREPARE, &ab->vbus_draw);
  699. usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
  700. }
  701. if (event != UX500_MUSB_RIDC)
  702. event = UX500_MUSB_VBUS;
  703. break;
  704. case USB_LINK_ACA_RID_A_8500:
  705. event = UX500_MUSB_RIDA;
  706. case USB_LINK_HM_IDGND_8500:
  707. if (ab->mode == USB_IDLE) {
  708. ab->mode = USB_HOST;
  709. ab8500_usb_host_phy_en(ab);
  710. atomic_notifier_call_chain(&ab->phy.notifier,
  711. UX500_MUSB_PREPARE, &ab->vbus_draw);
  712. }
  713. ab->phy.otg->default_a = true;
  714. if (event != UX500_MUSB_RIDA)
  715. event = UX500_MUSB_ID;
  716. atomic_notifier_call_chain(&ab->phy.notifier,
  717. event, &ab->vbus_draw);
  718. break;
  719. case USB_LINK_DEDICATED_CHG_8500:
  720. ab->mode = USB_DEDICATED_CHG;
  721. event = UX500_MUSB_CHARGER;
  722. atomic_notifier_call_chain(&ab->phy.notifier,
  723. event, &ab->vbus_draw);
  724. usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
  725. break;
  726. case USB_LINK_RESERVED_8500:
  727. break;
  728. }
  729. return 0;
  730. }
  731. /*
  732. * Connection Sequence:
  733. * 1. Link Status Interrupt
  734. * 2. Enable AB clock
  735. * 3. Enable AB regulators
  736. * 4. Enable USB phy
  737. * 5. Reset the musb controller
  738. * 6. Switch the ULPI GPIO pins to fucntion mode
  739. * 7. Enable the musb Peripheral5 clock
  740. * 8. Restore MUSB context
  741. */
  742. static int abx500_usb_link_status_update(struct ab8500_usb *ab)
  743. {
  744. u8 reg;
  745. int ret = 0;
  746. if (is_ab8500(ab->ab8500)) {
  747. enum ab8500_usb_link_status lsts;
  748. abx500_get_register_interruptible(ab->dev,
  749. AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
  750. lsts = (reg >> 3) & 0x0F;
  751. ret = ab8500_usb_link_status_update(ab, lsts);
  752. } else if (is_ab8505(ab->ab8500)) {
  753. enum ab8505_usb_link_status lsts;
  754. abx500_get_register_interruptible(ab->dev,
  755. AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
  756. lsts = (reg >> 3) & 0x1F;
  757. ret = ab8505_usb_link_status_update(ab, lsts);
  758. } else if (is_ab8540(ab->ab8500)) {
  759. enum ab8540_usb_link_status lsts;
  760. abx500_get_register_interruptible(ab->dev,
  761. AB8500_USB, AB8540_USB_LINK_STAT_REG, &reg);
  762. lsts = (reg >> 3) & 0xFF;
  763. ret = ab8540_usb_link_status_update(ab, lsts);
  764. } else if (is_ab9540(ab->ab8500)) {
  765. enum ab9540_usb_link_status lsts;
  766. abx500_get_register_interruptible(ab->dev,
  767. AB8500_USB, AB9540_USB_LINK_STAT_REG, &reg);
  768. lsts = (reg >> 3) & 0xFF;
  769. ret = ab9540_usb_link_status_update(ab, lsts);
  770. }
  771. return ret;
  772. }
  773. /*
  774. * Disconnection Sequence:
  775. * 1. Disconnect Interrupt
  776. * 2. Disable regulators
  777. * 3. Disable AB clock
  778. * 4. Disable the Phy
  779. * 5. Link Status Interrupt
  780. * 6. Disable Musb Clock
  781. */
  782. static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
  783. {
  784. struct ab8500_usb *ab = (struct ab8500_usb *) data;
  785. enum usb_phy_events event = UX500_MUSB_NONE;
  786. /* Link status will not be updated till phy is disabled. */
  787. if (ab->mode == USB_HOST) {
  788. ab->phy.otg->default_a = false;
  789. ab->vbus_draw = 0;
  790. atomic_notifier_call_chain(&ab->phy.notifier,
  791. event, &ab->vbus_draw);
  792. ab8500_usb_host_phy_dis(ab);
  793. ab->mode = USB_IDLE;
  794. }
  795. if (ab->mode == USB_PERIPHERAL) {
  796. atomic_notifier_call_chain(&ab->phy.notifier,
  797. event, &ab->vbus_draw);
  798. ab8500_usb_peri_phy_dis(ab);
  799. atomic_notifier_call_chain(&ab->phy.notifier,
  800. UX500_MUSB_CLEAN, &ab->vbus_draw);
  801. ab->mode = USB_IDLE;
  802. ab->phy.otg->default_a = false;
  803. ab->vbus_draw = 0;
  804. }
  805. if (is_ab8500_2p0(ab->ab8500)) {
  806. if (ab->mode == USB_DEDICATED_CHG) {
  807. ab8500_usb_wd_linkstatus(ab,
  808. AB8500_BIT_PHY_CTRL_DEVICE_EN);
  809. abx500_mask_and_set_register_interruptible(ab->dev,
  810. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  811. AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
  812. }
  813. }
  814. return IRQ_HANDLED;
  815. }
  816. static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
  817. {
  818. struct ab8500_usb *ab = (struct ab8500_usb *)data;
  819. abx500_usb_link_status_update(ab);
  820. return IRQ_HANDLED;
  821. }
  822. static void ab8500_usb_phy_disable_work(struct work_struct *work)
  823. {
  824. struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
  825. phy_dis_work);
  826. if (!ab->phy.otg->host)
  827. ab8500_usb_host_phy_dis(ab);
  828. if (!ab->phy.otg->gadget)
  829. ab8500_usb_peri_phy_dis(ab);
  830. }
  831. /* Check if VBUS is set and linkstatus has not detected a cable. */
  832. static bool ab8500_usb_check_vbus_status(struct ab8500_usb *ab)
  833. {
  834. u8 isource2;
  835. u8 reg;
  836. enum ab8540_usb_link_status lsts;
  837. abx500_get_register_interruptible(ab->dev,
  838. AB8500_INTERRUPT, AB8500_IT_SOURCE2_REG,
  839. &isource2);
  840. /* If Vbus is below 3.6V abort */
  841. if (!(isource2 & AB8500_BIT_SOURCE2_VBUSDET))
  842. return false;
  843. abx500_get_register_interruptible(ab->dev,
  844. AB8500_USB, AB8540_USB_LINK_STAT_REG,
  845. &reg);
  846. lsts = (reg >> 3) & 0xFF;
  847. /* Check if linkstatus has detected a cable */
  848. if (lsts)
  849. return false;
  850. return true;
  851. }
  852. /* re-trigger charger detection again with watchdog re-kick. */
  853. static void ab8500_usb_vbus_turn_on_event_work(struct work_struct *work)
  854. {
  855. struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
  856. vbus_event_work);
  857. if (ab->mode != USB_IDLE)
  858. return;
  859. abx500_set_register_interruptible(ab->dev,
  860. AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
  861. AB8500_BIT_WD_CTRL_ENABLE);
  862. udelay(100);
  863. abx500_set_register_interruptible(ab->dev,
  864. AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
  865. AB8500_BIT_WD_CTRL_ENABLE | AB8500_BIT_WD_CTRL_KICK);
  866. udelay(100);
  867. /* Disable Main watchdog */
  868. abx500_set_register_interruptible(ab->dev,
  869. AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
  870. 0x0);
  871. /* Enable USB Charger detection */
  872. abx500_mask_and_set_register_interruptible(ab->dev,
  873. AB8500_USB, AB8540_VBUS_CTRL_REG,
  874. AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA,
  875. AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA);
  876. ab->enabled_charging_detection = true;
  877. }
  878. static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
  879. {
  880. /* TODO */
  881. return 0;
  882. }
  883. static int ab8500_usb_set_peripheral(struct usb_otg *otg,
  884. struct usb_gadget *gadget)
  885. {
  886. struct ab8500_usb *ab;
  887. if (!otg)
  888. return -ENODEV;
  889. ab = phy_to_ab(otg->usb_phy);
  890. ab->phy.otg->gadget = gadget;
  891. /* Some drivers call this function in atomic context.
  892. * Do not update ab8500 registers directly till this
  893. * is fixed.
  894. */
  895. if ((ab->mode != USB_IDLE) && !gadget) {
  896. ab->mode = USB_IDLE;
  897. schedule_work(&ab->phy_dis_work);
  898. }
  899. return 0;
  900. }
  901. static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
  902. {
  903. struct ab8500_usb *ab;
  904. if (!otg)
  905. return -ENODEV;
  906. ab = phy_to_ab(otg->usb_phy);
  907. ab->phy.otg->host = host;
  908. /* Some drivers call this function in atomic context.
  909. * Do not update ab8500 registers directly till this
  910. * is fixed.
  911. */
  912. if ((ab->mode != USB_IDLE) && !host) {
  913. ab->mode = USB_IDLE;
  914. schedule_work(&ab->phy_dis_work);
  915. }
  916. return 0;
  917. }
  918. static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
  919. {
  920. abx500_mask_and_set_register_interruptible(ab->dev,
  921. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  922. AB8500_BIT_PHY_CTRL_DEVICE_EN,
  923. AB8500_BIT_PHY_CTRL_DEVICE_EN);
  924. udelay(100);
  925. abx500_mask_and_set_register_interruptible(ab->dev,
  926. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  927. AB8500_BIT_PHY_CTRL_DEVICE_EN,
  928. 0);
  929. abx500_mask_and_set_register_interruptible(ab->dev,
  930. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  931. AB8500_BIT_PHY_CTRL_HOST_EN,
  932. AB8500_BIT_PHY_CTRL_HOST_EN);
  933. udelay(100);
  934. abx500_mask_and_set_register_interruptible(ab->dev,
  935. AB8500_USB, AB8500_USB_PHY_CTRL_REG,
  936. AB8500_BIT_PHY_CTRL_HOST_EN,
  937. 0);
  938. }
  939. static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
  940. {
  941. int err;
  942. ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
  943. if (IS_ERR(ab->v_ape)) {
  944. dev_err(ab->dev, "Could not get v-ape supply\n");
  945. err = PTR_ERR(ab->v_ape);
  946. return err;
  947. }
  948. ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
  949. if (IS_ERR(ab->v_ulpi)) {
  950. dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
  951. err = PTR_ERR(ab->v_ulpi);
  952. return err;
  953. }
  954. ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
  955. if (IS_ERR(ab->v_musb)) {
  956. dev_err(ab->dev, "Could not get musb_1v8 supply\n");
  957. err = PTR_ERR(ab->v_musb);
  958. return err;
  959. }
  960. return 0;
  961. }
  962. static int ab8500_usb_irq_setup(struct platform_device *pdev,
  963. struct ab8500_usb *ab)
  964. {
  965. int err;
  966. int irq;
  967. if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
  968. irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
  969. if (irq < 0) {
  970. dev_err(&pdev->dev, "Link status irq not found\n");
  971. return irq;
  972. }
  973. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  974. ab8500_usb_link_status_irq,
  975. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  976. "usb-link-status", ab);
  977. if (err < 0) {
  978. dev_err(ab->dev, "request_irq failed for link status irq\n");
  979. return err;
  980. }
  981. }
  982. if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
  983. irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
  984. if (irq < 0) {
  985. dev_err(&pdev->dev, "ID fall irq not found\n");
  986. return irq;
  987. }
  988. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  989. ab8500_usb_disconnect_irq,
  990. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  991. "usb-id-fall", ab);
  992. if (err < 0) {
  993. dev_err(ab->dev, "request_irq failed for ID fall irq\n");
  994. return err;
  995. }
  996. }
  997. if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
  998. irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
  999. if (irq < 0) {
  1000. dev_err(&pdev->dev, "VBUS fall irq not found\n");
  1001. return irq;
  1002. }
  1003. err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  1004. ab8500_usb_disconnect_irq,
  1005. IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
  1006. "usb-vbus-fall", ab);
  1007. if (err < 0) {
  1008. dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
  1009. return err;
  1010. }
  1011. }
  1012. return 0;
  1013. }
  1014. static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
  1015. {
  1016. int err;
  1017. /* Enable the PBT/Bank 0x12 access */
  1018. err = abx500_set_register_interruptible(ab->dev,
  1019. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
  1020. if (err < 0)
  1021. dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
  1022. err);
  1023. err = abx500_set_register_interruptible(ab->dev,
  1024. AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
  1025. if (err < 0)
  1026. dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
  1027. err);
  1028. err = abx500_set_register_interruptible(ab->dev,
  1029. AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
  1030. if (err < 0)
  1031. dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
  1032. err);
  1033. err = abx500_set_register_interruptible(ab->dev,
  1034. AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
  1035. if (err < 0)
  1036. dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
  1037. err);
  1038. /* Switch to normal mode/disable Bank 0x12 access */
  1039. err = abx500_set_register_interruptible(ab->dev,
  1040. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
  1041. if (err < 0)
  1042. dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
  1043. err);
  1044. }
  1045. static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
  1046. {
  1047. int err;
  1048. /* Enable the PBT/Bank 0x12 access */
  1049. err = abx500_mask_and_set_register_interruptible(ab->dev,
  1050. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
  1051. 0x01, 0x01);
  1052. if (err < 0)
  1053. dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
  1054. err);
  1055. err = abx500_mask_and_set_register_interruptible(ab->dev,
  1056. AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
  1057. 0xC8, 0xC8);
  1058. if (err < 0)
  1059. dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
  1060. err);
  1061. err = abx500_mask_and_set_register_interruptible(ab->dev,
  1062. AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
  1063. 0x60, 0x60);
  1064. if (err < 0)
  1065. dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
  1066. err);
  1067. err = abx500_mask_and_set_register_interruptible(ab->dev,
  1068. AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
  1069. 0xFC, 0x80);
  1070. if (err < 0)
  1071. dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
  1072. err);
  1073. /* Switch to normal mode/disable Bank 0x12 access */
  1074. err = abx500_mask_and_set_register_interruptible(ab->dev,
  1075. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
  1076. 0x00, 0x00);
  1077. if (err < 0)
  1078. dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
  1079. err);
  1080. }
  1081. static void ab8500_usb_set_ab8540_tuning_values(struct ab8500_usb *ab)
  1082. {
  1083. int err;
  1084. err = abx500_set_register_interruptible(ab->dev,
  1085. AB8540_DEBUG, AB8500_USB_PHY_TUNE1, 0xCC);
  1086. if (err < 0)
  1087. dev_err(ab->dev, "Failed to set PHY_TUNE1 register ret=%d\n",
  1088. err);
  1089. err = abx500_set_register_interruptible(ab->dev,
  1090. AB8540_DEBUG, AB8500_USB_PHY_TUNE2, 0x60);
  1091. if (err < 0)
  1092. dev_err(ab->dev, "Failed to set PHY_TUNE2 register ret=%d\n",
  1093. err);
  1094. err = abx500_set_register_interruptible(ab->dev,
  1095. AB8540_DEBUG, AB8500_USB_PHY_TUNE3, 0x90);
  1096. if (err < 0)
  1097. dev_err(ab->dev, "Failed to set PHY_TUNE3 register ret=%d\n",
  1098. err);
  1099. }
  1100. static void ab8500_usb_set_ab9540_tuning_values(struct ab8500_usb *ab)
  1101. {
  1102. int err;
  1103. /* Enable the PBT/Bank 0x12 access */
  1104. err = abx500_set_register_interruptible(ab->dev,
  1105. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
  1106. if (err < 0)
  1107. dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
  1108. err);
  1109. err = abx500_set_register_interruptible(ab->dev,
  1110. AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
  1111. if (err < 0)
  1112. dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
  1113. err);
  1114. err = abx500_set_register_interruptible(ab->dev,
  1115. AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x60);
  1116. if (err < 0)
  1117. dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
  1118. err);
  1119. err = abx500_set_register_interruptible(ab->dev,
  1120. AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x80);
  1121. if (err < 0)
  1122. dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
  1123. err);
  1124. /* Switch to normal mode/disable Bank 0x12 access */
  1125. err = abx500_set_register_interruptible(ab->dev,
  1126. AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
  1127. if (err < 0)
  1128. dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
  1129. err);
  1130. }
  1131. static int ab8500_usb_probe(struct platform_device *pdev)
  1132. {
  1133. struct ab8500_usb *ab;
  1134. struct ab8500 *ab8500;
  1135. struct usb_otg *otg;
  1136. int err;
  1137. int rev;
  1138. ab8500 = dev_get_drvdata(pdev->dev.parent);
  1139. rev = abx500_get_chip_id(&pdev->dev);
  1140. if (is_ab8500_1p1_or_earlier(ab8500)) {
  1141. dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
  1142. return -ENODEV;
  1143. }
  1144. ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
  1145. if (!ab)
  1146. return -ENOMEM;
  1147. otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
  1148. if (!otg)
  1149. return -ENOMEM;
  1150. ab->dev = &pdev->dev;
  1151. ab->ab8500 = ab8500;
  1152. ab->phy.dev = ab->dev;
  1153. ab->phy.otg = otg;
  1154. ab->phy.label = "ab8500";
  1155. ab->phy.set_suspend = ab8500_usb_set_suspend;
  1156. ab->phy.otg->state = OTG_STATE_UNDEFINED;
  1157. otg->usb_phy = &ab->phy;
  1158. otg->set_host = ab8500_usb_set_host;
  1159. otg->set_peripheral = ab8500_usb_set_peripheral;
  1160. if (is_ab8500(ab->ab8500)) {
  1161. ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
  1162. AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
  1163. AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
  1164. AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  1165. } else if (is_ab8505(ab->ab8500)) {
  1166. ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
  1167. AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
  1168. AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
  1169. AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  1170. } else if (is_ab8540(ab->ab8500)) {
  1171. ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
  1172. AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS |
  1173. AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK |
  1174. AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  1175. } else if (is_ab9540(ab->ab8500)) {
  1176. ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
  1177. AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  1178. if (is_ab9540_2p0_or_earlier(ab->ab8500))
  1179. ab->flags |= AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
  1180. AB8500_USB_FLAG_USE_VBUS_DET_IRQ;
  1181. }
  1182. /* Disable regulator voltage setting for AB8500 <= v2.0 */
  1183. if (is_ab8500_2p0_or_earlier(ab->ab8500))
  1184. ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
  1185. platform_set_drvdata(pdev, ab);
  1186. /* all: Disable phy when called from set_host and set_peripheral */
  1187. INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
  1188. INIT_WORK(&ab->vbus_event_work, ab8500_usb_vbus_turn_on_event_work);
  1189. err = ab8500_usb_regulator_get(ab);
  1190. if (err)
  1191. return err;
  1192. ab->sysclk = devm_clk_get(ab->dev, "sysclk");
  1193. if (IS_ERR(ab->sysclk)) {
  1194. dev_err(ab->dev, "Could not get sysclk.\n");
  1195. return PTR_ERR(ab->sysclk);
  1196. }
  1197. err = ab8500_usb_irq_setup(pdev, ab);
  1198. if (err < 0)
  1199. return err;
  1200. err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
  1201. if (err) {
  1202. dev_err(&pdev->dev, "Can't register transceiver\n");
  1203. return err;
  1204. }
  1205. if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
  1206. /* Phy tuning values for AB8500 > v2.0 */
  1207. ab8500_usb_set_ab8500_tuning_values(ab);
  1208. else if (is_ab8505(ab->ab8500))
  1209. /* Phy tuning values for AB8505 */
  1210. ab8500_usb_set_ab8505_tuning_values(ab);
  1211. else if (is_ab8540(ab->ab8500))
  1212. /* Phy tuning values for AB8540 */
  1213. ab8500_usb_set_ab8540_tuning_values(ab);
  1214. else if (is_ab9540(ab->ab8500))
  1215. /* Phy tuning values for AB9540 */
  1216. ab8500_usb_set_ab9540_tuning_values(ab);
  1217. /* Needed to enable ID detection. */
  1218. ab8500_usb_wd_workaround(ab);
  1219. /*
  1220. * This is required for usb-link-status to work properly when a
  1221. * cable is connected at boot time.
  1222. */
  1223. ab8500_usb_restart_phy(ab);
  1224. if (ab->flags & AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS) {
  1225. if (ab8500_usb_check_vbus_status(ab))
  1226. schedule_work(&ab->vbus_event_work);
  1227. }
  1228. abx500_usb_link_status_update(ab);
  1229. dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
  1230. return 0;
  1231. }
  1232. static int ab8500_usb_remove(struct platform_device *pdev)
  1233. {
  1234. struct ab8500_usb *ab = platform_get_drvdata(pdev);
  1235. cancel_work_sync(&ab->phy_dis_work);
  1236. cancel_work_sync(&ab->vbus_event_work);
  1237. usb_remove_phy(&ab->phy);
  1238. if (ab->mode == USB_HOST)
  1239. ab8500_usb_host_phy_dis(ab);
  1240. else if (ab->mode == USB_PERIPHERAL)
  1241. ab8500_usb_peri_phy_dis(ab);
  1242. return 0;
  1243. }
  1244. static const struct platform_device_id ab8500_usb_devtype[] = {
  1245. { .name = "ab8500-usb", },
  1246. { .name = "ab8540-usb", },
  1247. { .name = "ab9540-usb", },
  1248. { /* sentinel */ }
  1249. };
  1250. MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
  1251. static struct platform_driver ab8500_usb_driver = {
  1252. .probe = ab8500_usb_probe,
  1253. .remove = ab8500_usb_remove,
  1254. .id_table = ab8500_usb_devtype,
  1255. .driver = {
  1256. .name = "abx5x0-usb",
  1257. },
  1258. };
  1259. static int __init ab8500_usb_init(void)
  1260. {
  1261. return platform_driver_register(&ab8500_usb_driver);
  1262. }
  1263. subsys_initcall(ab8500_usb_init);
  1264. static void __exit ab8500_usb_exit(void)
  1265. {
  1266. platform_driver_unregister(&ab8500_usb_driver);
  1267. }
  1268. module_exit(ab8500_usb_exit);
  1269. MODULE_AUTHOR("ST-Ericsson AB");
  1270. MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
  1271. MODULE_LICENSE("GPL");