phy-ab8500-usb.c 41 KB

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