phy-ab8500-usb.c 40 KB

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