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