xref: /openbmc/linux/drivers/usb/phy/phy-ab8500-usb.c (revision a44e4f3a)
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 AB8500_USB_PHY_CTRL_REG 0x8A
33 
34 /* Bank AB8500_DEVELOPMENT */
35 #define AB8500_BANK12_ACCESS 0x00
36 
37 /* Bank AB8500_DEBUG */
38 #define AB8500_USB_PHY_TUNE1 0x05
39 #define AB8500_USB_PHY_TUNE2 0x06
40 #define AB8500_USB_PHY_TUNE3 0x07
41 
42 /* Bank AB8500_INTERRUPT */
43 #define AB8500_IT_SOURCE2_REG 0x01
44 
45 #define AB8500_BIT_OTG_STAT_ID (1 << 0)
46 #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
47 #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
48 #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
49 #define AB8500_BIT_WD_CTRL_KICK (1 << 1)
50 #define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
51 
52 #define AB8500_WD_KICK_DELAY_US 100 /* usec */
53 #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
54 #define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
55 
56 /* Usb line status register */
57 enum ab8500_usb_link_status {
58 	USB_LINK_NOT_CONFIGURED_8500 = 0,
59 	USB_LINK_STD_HOST_NC_8500,
60 	USB_LINK_STD_HOST_C_NS_8500,
61 	USB_LINK_STD_HOST_C_S_8500,
62 	USB_LINK_HOST_CHG_NM_8500,
63 	USB_LINK_HOST_CHG_HS_8500,
64 	USB_LINK_HOST_CHG_HS_CHIRP_8500,
65 	USB_LINK_DEDICATED_CHG_8500,
66 	USB_LINK_ACA_RID_A_8500,
67 	USB_LINK_ACA_RID_B_8500,
68 	USB_LINK_ACA_RID_C_NM_8500,
69 	USB_LINK_ACA_RID_C_HS_8500,
70 	USB_LINK_ACA_RID_C_HS_CHIRP_8500,
71 	USB_LINK_HM_IDGND_8500,
72 	USB_LINK_RESERVED_8500,
73 	USB_LINK_NOT_VALID_LINK_8500,
74 };
75 
76 enum ab8505_usb_link_status {
77 	USB_LINK_NOT_CONFIGURED_8505 = 0,
78 	USB_LINK_STD_HOST_NC_8505,
79 	USB_LINK_STD_HOST_C_NS_8505,
80 	USB_LINK_STD_HOST_C_S_8505,
81 	USB_LINK_CDP_8505,
82 	USB_LINK_RESERVED0_8505,
83 	USB_LINK_RESERVED1_8505,
84 	USB_LINK_DEDICATED_CHG_8505,
85 	USB_LINK_ACA_RID_A_8505,
86 	USB_LINK_ACA_RID_B_8505,
87 	USB_LINK_ACA_RID_C_NM_8505,
88 	USB_LINK_RESERVED2_8505,
89 	USB_LINK_RESERVED3_8505,
90 	USB_LINK_HM_IDGND_8505,
91 	USB_LINK_CHARGERPORT_NOT_OK_8505,
92 	USB_LINK_CHARGER_DM_HIGH_8505,
93 	USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
94 	USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
95 	USB_LINK_STD_UPSTREAM_8505,
96 	USB_LINK_CHARGER_SE1_8505,
97 	USB_LINK_CARKIT_CHGR_1_8505,
98 	USB_LINK_CARKIT_CHGR_2_8505,
99 	USB_LINK_ACA_DOCK_CHGR_8505,
100 	USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
101 	USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
102 	USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
103 	USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
104 	USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
105 };
106 
107 enum ab8500_usb_mode {
108 	USB_IDLE = 0,
109 	USB_PERIPHERAL,
110 	USB_HOST,
111 	USB_DEDICATED_CHG
112 };
113 
114 /* Register USB_LINK_STATUS interrupt */
115 #define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ	(1 << 0)
116 /* Register ID_WAKEUP_F interrupt */
117 #define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ	(1 << 1)
118 /* Register VBUS_DET_F interrupt */
119 #define AB8500_USB_FLAG_USE_VBUS_DET_IRQ	(1 << 2)
120 /* Driver is using the ab-iddet driver*/
121 #define AB8500_USB_FLAG_USE_AB_IDDET		(1 << 3)
122 /* Enable setting regulators voltage */
123 #define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE	(1 << 4)
124 
125 struct ab8500_usb {
126 	struct usb_phy phy;
127 	struct device *dev;
128 	struct ab8500 *ab8500;
129 	unsigned vbus_draw;
130 	struct work_struct phy_dis_work;
131 	enum ab8500_usb_mode mode;
132 	struct clk *sysclk;
133 	struct regulator *v_ape;
134 	struct regulator *v_musb;
135 	struct regulator *v_ulpi;
136 	int saved_v_ulpi;
137 	int previous_link_status_state;
138 	struct pinctrl *pinctrl;
139 	struct pinctrl_state *pins_sleep;
140 	bool enabled_charging_detection;
141 	unsigned int flags;
142 };
143 
144 static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
145 {
146 	return container_of(x, struct ab8500_usb, phy);
147 }
148 
149 static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
150 {
151 	abx500_set_register_interruptible(ab->dev,
152 		AB8500_SYS_CTRL2_BLOCK,
153 		AB8500_MAIN_WD_CTRL_REG,
154 		AB8500_BIT_WD_CTRL_ENABLE);
155 
156 	udelay(AB8500_WD_KICK_DELAY_US);
157 
158 	abx500_set_register_interruptible(ab->dev,
159 		AB8500_SYS_CTRL2_BLOCK,
160 		AB8500_MAIN_WD_CTRL_REG,
161 		(AB8500_BIT_WD_CTRL_ENABLE
162 		| AB8500_BIT_WD_CTRL_KICK));
163 
164 	udelay(AB8500_WD_V11_DISABLE_DELAY_US);
165 
166 	abx500_set_register_interruptible(ab->dev,
167 		AB8500_SYS_CTRL2_BLOCK,
168 		AB8500_MAIN_WD_CTRL_REG,
169 		0);
170 }
171 
172 static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
173 {
174 	int ret, volt;
175 
176 	ret = regulator_enable(ab->v_ape);
177 	if (ret)
178 		dev_err(ab->dev, "Failed to enable v-ape\n");
179 
180 	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
181 		ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
182 		if (ab->saved_v_ulpi < 0)
183 			dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
184 
185 		ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
186 		if (ret < 0)
187 			dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
188 					ret);
189 
190 		ret = regulator_set_load(ab->v_ulpi, 28000);
191 		if (ret < 0)
192 			dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
193 					ret);
194 	}
195 
196 	ret = regulator_enable(ab->v_ulpi);
197 	if (ret)
198 		dev_err(ab->dev, "Failed to enable vddulpivio18\n");
199 
200 	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
201 		volt = regulator_get_voltage(ab->v_ulpi);
202 		if ((volt != 1300000) && (volt != 1350000))
203 			dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
204 					volt);
205 	}
206 
207 	ret = regulator_enable(ab->v_musb);
208 	if (ret)
209 		dev_err(ab->dev, "Failed to enable musb_1v8\n");
210 }
211 
212 static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
213 {
214 	int ret;
215 
216 	regulator_disable(ab->v_musb);
217 
218 	regulator_disable(ab->v_ulpi);
219 
220 	/* USB is not the only consumer of Vintcore, restore old settings */
221 	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
222 		if (ab->saved_v_ulpi > 0) {
223 			ret = regulator_set_voltage(ab->v_ulpi,
224 					ab->saved_v_ulpi, ab->saved_v_ulpi);
225 			if (ret < 0)
226 				dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
227 						ab->saved_v_ulpi, ret);
228 		}
229 
230 		ret = regulator_set_load(ab->v_ulpi, 0);
231 		if (ret < 0)
232 			dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
233 					ret);
234 	}
235 
236 	regulator_disable(ab->v_ape);
237 }
238 
239 static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
240 {
241 	/* Workaround for v2.0 bug # 31952 */
242 	if (is_ab8500_2p0(ab->ab8500)) {
243 		abx500_mask_and_set_register_interruptible(ab->dev,
244 				AB8500_USB, AB8500_USB_PHY_CTRL_REG,
245 				bit, bit);
246 		udelay(AB8500_V20_31952_DISABLE_DELAY_US);
247 	}
248 }
249 
250 static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
251 {
252 	u8 bit;
253 	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
254 		AB8500_BIT_PHY_CTRL_DEVICE_EN;
255 
256 	/* mux and configure USB pins to DEFAULT state */
257 	ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
258 	if (IS_ERR(ab->pinctrl))
259 		dev_err(ab->dev, "could not get/set default pinstate\n");
260 
261 	if (clk_prepare_enable(ab->sysclk))
262 		dev_err(ab->dev, "can't prepare/enable clock\n");
263 
264 	ab8500_usb_regulator_enable(ab);
265 
266 	abx500_mask_and_set_register_interruptible(ab->dev,
267 			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
268 			bit, bit);
269 }
270 
271 static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
272 {
273 	u8 bit;
274 	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
275 		AB8500_BIT_PHY_CTRL_DEVICE_EN;
276 
277 	ab8500_usb_wd_linkstatus(ab, bit);
278 
279 	abx500_mask_and_set_register_interruptible(ab->dev,
280 			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
281 			bit, 0);
282 
283 	/* Needed to disable the phy.*/
284 	ab8500_usb_wd_workaround(ab);
285 
286 	clk_disable_unprepare(ab->sysclk);
287 
288 	ab8500_usb_regulator_disable(ab);
289 
290 	if (!IS_ERR(ab->pinctrl)) {
291 		/* configure USB pins to SLEEP state */
292 		ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
293 				PINCTRL_STATE_SLEEP);
294 
295 		if (IS_ERR(ab->pins_sleep))
296 			dev_dbg(ab->dev, "could not get sleep pinstate\n");
297 		else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
298 			dev_err(ab->dev, "could not set pins to sleep state\n");
299 
300 		/*
301 		 * as USB pins are shared with iddet, release them to allow
302 		 * iddet to request them
303 		 */
304 		pinctrl_put(ab->pinctrl);
305 	}
306 }
307 
308 #define ab8500_usb_host_phy_en(ab)	ab8500_usb_phy_enable(ab, true)
309 #define ab8500_usb_host_phy_dis(ab)	ab8500_usb_phy_disable(ab, true)
310 #define ab8500_usb_peri_phy_en(ab)	ab8500_usb_phy_enable(ab, false)
311 #define ab8500_usb_peri_phy_dis(ab)	ab8500_usb_phy_disable(ab, false)
312 
313 static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
314 		enum ab8505_usb_link_status lsts)
315 {
316 	enum ux500_musb_vbus_id_status event = 0;
317 
318 	dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
319 
320 	/*
321 	 * Spurious link_status interrupts are seen at the time of
322 	 * disconnection of a device in RIDA state
323 	 */
324 	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
325 			(lsts == USB_LINK_STD_HOST_NC_8505))
326 		return 0;
327 
328 	ab->previous_link_status_state = lsts;
329 
330 	switch (lsts) {
331 	case USB_LINK_ACA_RID_B_8505:
332 		event = UX500_MUSB_RIDB;
333 		/* Fall through */
334 	case USB_LINK_NOT_CONFIGURED_8505:
335 	case USB_LINK_RESERVED0_8505:
336 	case USB_LINK_RESERVED1_8505:
337 	case USB_LINK_RESERVED2_8505:
338 	case USB_LINK_RESERVED3_8505:
339 		ab->mode = USB_IDLE;
340 		ab->phy.otg->default_a = false;
341 		ab->vbus_draw = 0;
342 		if (event != UX500_MUSB_RIDB)
343 			event = UX500_MUSB_NONE;
344 		/*
345 		 * Fallback to default B_IDLE as nothing
346 		 * is connected
347 		 */
348 		ab->phy.otg->state = OTG_STATE_B_IDLE;
349 		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
350 		break;
351 
352 	case USB_LINK_ACA_RID_C_NM_8505:
353 		event = UX500_MUSB_RIDC;
354 		/* Fall through */
355 	case USB_LINK_STD_HOST_NC_8505:
356 	case USB_LINK_STD_HOST_C_NS_8505:
357 	case USB_LINK_STD_HOST_C_S_8505:
358 	case USB_LINK_CDP_8505:
359 		if (ab->mode == USB_IDLE) {
360 			ab->mode = USB_PERIPHERAL;
361 			ab8500_usb_peri_phy_en(ab);
362 			atomic_notifier_call_chain(&ab->phy.notifier,
363 					UX500_MUSB_PREPARE, &ab->vbus_draw);
364 			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
365 		}
366 		if (event != UX500_MUSB_RIDC)
367 			event = UX500_MUSB_VBUS;
368 		break;
369 
370 	case USB_LINK_ACA_RID_A_8505:
371 	case USB_LINK_ACA_DOCK_CHGR_8505:
372 		event = UX500_MUSB_RIDA;
373 		/* Fall through */
374 	case USB_LINK_HM_IDGND_8505:
375 		if (ab->mode == USB_IDLE) {
376 			ab->mode = USB_HOST;
377 			ab8500_usb_host_phy_en(ab);
378 			atomic_notifier_call_chain(&ab->phy.notifier,
379 					UX500_MUSB_PREPARE, &ab->vbus_draw);
380 		}
381 		ab->phy.otg->default_a = true;
382 		if (event != UX500_MUSB_RIDA)
383 			event = UX500_MUSB_ID;
384 		atomic_notifier_call_chain(&ab->phy.notifier,
385 				event, &ab->vbus_draw);
386 		break;
387 
388 	case USB_LINK_DEDICATED_CHG_8505:
389 		ab->mode = USB_DEDICATED_CHG;
390 		event = UX500_MUSB_CHARGER;
391 		atomic_notifier_call_chain(&ab->phy.notifier,
392 				event, &ab->vbus_draw);
393 		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
394 		break;
395 
396 	default:
397 		break;
398 	}
399 
400 	return 0;
401 }
402 
403 static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
404 		enum ab8500_usb_link_status lsts)
405 {
406 	enum ux500_musb_vbus_id_status event = 0;
407 
408 	dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
409 
410 	/*
411 	 * Spurious link_status interrupts are seen in case of a
412 	 * disconnection of a device in IDGND and RIDA stage
413 	 */
414 	if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
415 			(lsts == USB_LINK_STD_HOST_C_NS_8500 ||
416 			 lsts == USB_LINK_STD_HOST_NC_8500))
417 		return 0;
418 
419 	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
420 			lsts == USB_LINK_STD_HOST_NC_8500)
421 		return 0;
422 
423 	ab->previous_link_status_state = lsts;
424 
425 	switch (lsts) {
426 	case USB_LINK_ACA_RID_B_8500:
427 		event = UX500_MUSB_RIDB;
428 		/* Fall through */
429 	case USB_LINK_NOT_CONFIGURED_8500:
430 	case USB_LINK_NOT_VALID_LINK_8500:
431 		ab->mode = USB_IDLE;
432 		ab->phy.otg->default_a = false;
433 		ab->vbus_draw = 0;
434 		if (event != UX500_MUSB_RIDB)
435 			event = UX500_MUSB_NONE;
436 		/* Fallback to default B_IDLE as nothing is connected */
437 		ab->phy.otg->state = OTG_STATE_B_IDLE;
438 		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
439 		break;
440 
441 	case USB_LINK_ACA_RID_C_NM_8500:
442 	case USB_LINK_ACA_RID_C_HS_8500:
443 	case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
444 		event = UX500_MUSB_RIDC;
445 		/* Fall through */
446 	case USB_LINK_STD_HOST_NC_8500:
447 	case USB_LINK_STD_HOST_C_NS_8500:
448 	case USB_LINK_STD_HOST_C_S_8500:
449 	case USB_LINK_HOST_CHG_NM_8500:
450 	case USB_LINK_HOST_CHG_HS_8500:
451 	case USB_LINK_HOST_CHG_HS_CHIRP_8500:
452 		if (ab->mode == USB_IDLE) {
453 			ab->mode = USB_PERIPHERAL;
454 			ab8500_usb_peri_phy_en(ab);
455 			atomic_notifier_call_chain(&ab->phy.notifier,
456 					UX500_MUSB_PREPARE, &ab->vbus_draw);
457 			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
458 		}
459 		if (event != UX500_MUSB_RIDC)
460 			event = UX500_MUSB_VBUS;
461 		break;
462 
463 	case USB_LINK_ACA_RID_A_8500:
464 		event = UX500_MUSB_RIDA;
465 		/* Fall through */
466 	case USB_LINK_HM_IDGND_8500:
467 		if (ab->mode == USB_IDLE) {
468 			ab->mode = USB_HOST;
469 			ab8500_usb_host_phy_en(ab);
470 			atomic_notifier_call_chain(&ab->phy.notifier,
471 					UX500_MUSB_PREPARE, &ab->vbus_draw);
472 		}
473 		ab->phy.otg->default_a = true;
474 		if (event != UX500_MUSB_RIDA)
475 			event = UX500_MUSB_ID;
476 		atomic_notifier_call_chain(&ab->phy.notifier,
477 				event, &ab->vbus_draw);
478 		break;
479 
480 	case USB_LINK_DEDICATED_CHG_8500:
481 		ab->mode = USB_DEDICATED_CHG;
482 		event = UX500_MUSB_CHARGER;
483 		atomic_notifier_call_chain(&ab->phy.notifier,
484 				event, &ab->vbus_draw);
485 		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
486 		break;
487 
488 	case USB_LINK_RESERVED_8500:
489 		break;
490 	}
491 
492 	return 0;
493 }
494 
495 /*
496  * Connection Sequence:
497  *   1. Link Status Interrupt
498  *   2. Enable AB clock
499  *   3. Enable AB regulators
500  *   4. Enable USB phy
501  *   5. Reset the musb controller
502  *   6. Switch the ULPI GPIO pins to fucntion mode
503  *   7. Enable the musb Peripheral5 clock
504  *   8. Restore MUSB context
505  */
506 static int abx500_usb_link_status_update(struct ab8500_usb *ab)
507 {
508 	u8 reg;
509 	int ret = 0;
510 
511 	if (is_ab8500(ab->ab8500)) {
512 		enum ab8500_usb_link_status lsts;
513 
514 		ret = abx500_get_register_interruptible(ab->dev,
515 				AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
516 		if (ret < 0)
517 			return ret;
518 		lsts = (reg >> 3) & 0x0F;
519 		ret = ab8500_usb_link_status_update(ab, lsts);
520 	} else if (is_ab8505(ab->ab8500)) {
521 		enum ab8505_usb_link_status lsts;
522 
523 		ret = abx500_get_register_interruptible(ab->dev,
524 				AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
525 		if (ret < 0)
526 			return ret;
527 		lsts = (reg >> 3) & 0x1F;
528 		ret = ab8505_usb_link_status_update(ab, lsts);
529 	}
530 
531 	return ret;
532 }
533 
534 /*
535  * Disconnection Sequence:
536  *   1. Disconnect Interrupt
537  *   2. Disable regulators
538  *   3. Disable AB clock
539  *   4. Disable the Phy
540  *   5. Link Status Interrupt
541  *   6. Disable Musb Clock
542  */
543 static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
544 {
545 	struct ab8500_usb *ab = (struct ab8500_usb *) data;
546 	enum usb_phy_events event = USB_EVENT_NONE;
547 
548 	/* Link status will not be updated till phy is disabled. */
549 	if (ab->mode == USB_HOST) {
550 		ab->phy.otg->default_a = false;
551 		ab->vbus_draw = 0;
552 		atomic_notifier_call_chain(&ab->phy.notifier,
553 				event, &ab->vbus_draw);
554 		ab8500_usb_host_phy_dis(ab);
555 		ab->mode = USB_IDLE;
556 	}
557 
558 	if (ab->mode == USB_PERIPHERAL) {
559 		atomic_notifier_call_chain(&ab->phy.notifier,
560 				event, &ab->vbus_draw);
561 		ab8500_usb_peri_phy_dis(ab);
562 		atomic_notifier_call_chain(&ab->phy.notifier,
563 				UX500_MUSB_CLEAN, &ab->vbus_draw);
564 		ab->mode = USB_IDLE;
565 		ab->phy.otg->default_a = false;
566 		ab->vbus_draw = 0;
567 	}
568 
569 	if (is_ab8500_2p0(ab->ab8500)) {
570 		if (ab->mode == USB_DEDICATED_CHG) {
571 			ab8500_usb_wd_linkstatus(ab,
572 					AB8500_BIT_PHY_CTRL_DEVICE_EN);
573 			abx500_mask_and_set_register_interruptible(ab->dev,
574 					AB8500_USB, AB8500_USB_PHY_CTRL_REG,
575 					AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
576 		}
577 	}
578 
579 	return IRQ_HANDLED;
580 }
581 
582 static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
583 {
584 	struct ab8500_usb *ab = (struct ab8500_usb *)data;
585 
586 	abx500_usb_link_status_update(ab);
587 
588 	return IRQ_HANDLED;
589 }
590 
591 static void ab8500_usb_phy_disable_work(struct work_struct *work)
592 {
593 	struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
594 						phy_dis_work);
595 
596 	if (!ab->phy.otg->host)
597 		ab8500_usb_host_phy_dis(ab);
598 
599 	if (!ab->phy.otg->gadget)
600 		ab8500_usb_peri_phy_dis(ab);
601 }
602 
603 static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
604 {
605 	/* TODO */
606 	return 0;
607 }
608 
609 static int ab8500_usb_set_peripheral(struct usb_otg *otg,
610 					struct usb_gadget *gadget)
611 {
612 	struct ab8500_usb *ab;
613 
614 	if (!otg)
615 		return -ENODEV;
616 
617 	ab = phy_to_ab(otg->usb_phy);
618 
619 	ab->phy.otg->gadget = gadget;
620 
621 	/* Some drivers call this function in atomic context.
622 	 * Do not update ab8500 registers directly till this
623 	 * is fixed.
624 	 */
625 
626 	if ((ab->mode != USB_IDLE) && !gadget) {
627 		ab->mode = USB_IDLE;
628 		schedule_work(&ab->phy_dis_work);
629 	}
630 
631 	return 0;
632 }
633 
634 static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
635 {
636 	struct ab8500_usb *ab;
637 
638 	if (!otg)
639 		return -ENODEV;
640 
641 	ab = phy_to_ab(otg->usb_phy);
642 
643 	ab->phy.otg->host = host;
644 
645 	/* Some drivers call this function in atomic context.
646 	 * Do not update ab8500 registers directly till this
647 	 * is fixed.
648 	 */
649 
650 	if ((ab->mode != USB_IDLE) && !host) {
651 		ab->mode = USB_IDLE;
652 		schedule_work(&ab->phy_dis_work);
653 	}
654 
655 	return 0;
656 }
657 
658 static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
659 {
660 	abx500_mask_and_set_register_interruptible(ab->dev,
661 			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
662 			AB8500_BIT_PHY_CTRL_DEVICE_EN,
663 			AB8500_BIT_PHY_CTRL_DEVICE_EN);
664 
665 	udelay(100);
666 
667 	abx500_mask_and_set_register_interruptible(ab->dev,
668 			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
669 			AB8500_BIT_PHY_CTRL_DEVICE_EN,
670 			0);
671 
672 	abx500_mask_and_set_register_interruptible(ab->dev,
673 			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
674 			AB8500_BIT_PHY_CTRL_HOST_EN,
675 			AB8500_BIT_PHY_CTRL_HOST_EN);
676 
677 	udelay(100);
678 
679 	abx500_mask_and_set_register_interruptible(ab->dev,
680 			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
681 			AB8500_BIT_PHY_CTRL_HOST_EN,
682 			0);
683 }
684 
685 static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
686 {
687 	int err;
688 
689 	ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
690 	if (IS_ERR(ab->v_ape)) {
691 		dev_err(ab->dev, "Could not get v-ape supply\n");
692 		err = PTR_ERR(ab->v_ape);
693 		return err;
694 	}
695 
696 	ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
697 	if (IS_ERR(ab->v_ulpi)) {
698 		dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
699 		err = PTR_ERR(ab->v_ulpi);
700 		return err;
701 	}
702 
703 	ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
704 	if (IS_ERR(ab->v_musb)) {
705 		dev_err(ab->dev, "Could not get musb_1v8 supply\n");
706 		err = PTR_ERR(ab->v_musb);
707 		return err;
708 	}
709 
710 	return 0;
711 }
712 
713 static int ab8500_usb_irq_setup(struct platform_device *pdev,
714 		struct ab8500_usb *ab)
715 {
716 	int err;
717 	int irq;
718 
719 	if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
720 		irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
721 		if (irq < 0)
722 			return irq;
723 		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
724 				ab8500_usb_link_status_irq,
725 				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
726 				"usb-link-status", ab);
727 		if (err < 0) {
728 			dev_err(ab->dev, "request_irq failed for link status irq\n");
729 			return err;
730 		}
731 	}
732 
733 	if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
734 		irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
735 		if (irq < 0)
736 			return irq;
737 		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
738 				ab8500_usb_disconnect_irq,
739 				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
740 				"usb-id-fall", ab);
741 		if (err < 0) {
742 			dev_err(ab->dev, "request_irq failed for ID fall irq\n");
743 			return err;
744 		}
745 	}
746 
747 	if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
748 		irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
749 		if (irq < 0)
750 			return irq;
751 		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
752 				ab8500_usb_disconnect_irq,
753 				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
754 				"usb-vbus-fall", ab);
755 		if (err < 0) {
756 			dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
757 			return err;
758 		}
759 	}
760 
761 	return 0;
762 }
763 
764 static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
765 {
766 	int err;
767 
768 	/* Enable the PBT/Bank 0x12 access */
769 	err = abx500_set_register_interruptible(ab->dev,
770 			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
771 	if (err < 0)
772 		dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
773 				err);
774 
775 	err = abx500_set_register_interruptible(ab->dev,
776 			AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
777 	if (err < 0)
778 		dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
779 				err);
780 
781 	err = abx500_set_register_interruptible(ab->dev,
782 			AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
783 	if (err < 0)
784 		dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
785 				err);
786 
787 	err = abx500_set_register_interruptible(ab->dev,
788 			AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
789 	if (err < 0)
790 		dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
791 				err);
792 
793 	/* Switch to normal mode/disable Bank 0x12 access */
794 	err = abx500_set_register_interruptible(ab->dev,
795 			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
796 	if (err < 0)
797 		dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
798 				err);
799 }
800 
801 static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
802 {
803 	int err;
804 
805 	/* Enable the PBT/Bank 0x12 access */
806 	err = abx500_mask_and_set_register_interruptible(ab->dev,
807 			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
808 			0x01, 0x01);
809 	if (err < 0)
810 		dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
811 				err);
812 
813 	err = abx500_mask_and_set_register_interruptible(ab->dev,
814 			AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
815 			0xC8, 0xC8);
816 	if (err < 0)
817 		dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
818 				err);
819 
820 	err = abx500_mask_and_set_register_interruptible(ab->dev,
821 			AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
822 			0x60, 0x60);
823 	if (err < 0)
824 		dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
825 				err);
826 
827 	err = abx500_mask_and_set_register_interruptible(ab->dev,
828 			AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
829 			0xFC, 0x80);
830 
831 	if (err < 0)
832 		dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
833 				err);
834 
835 	/* Switch to normal mode/disable Bank 0x12 access */
836 	err = abx500_mask_and_set_register_interruptible(ab->dev,
837 			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
838 			0x00, 0x00);
839 	if (err < 0)
840 		dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
841 				err);
842 }
843 
844 static int ab8500_usb_probe(struct platform_device *pdev)
845 {
846 	struct ab8500_usb	*ab;
847 	struct ab8500		*ab8500;
848 	struct usb_otg		*otg;
849 	int err;
850 	int rev;
851 
852 	ab8500 = dev_get_drvdata(pdev->dev.parent);
853 	rev = abx500_get_chip_id(&pdev->dev);
854 
855 	if (is_ab8500_1p1_or_earlier(ab8500)) {
856 		dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
857 		return -ENODEV;
858 	}
859 
860 	ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
861 	if (!ab)
862 		return -ENOMEM;
863 
864 	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
865 	if (!otg)
866 		return -ENOMEM;
867 
868 	ab->dev			= &pdev->dev;
869 	ab->ab8500		= ab8500;
870 	ab->phy.dev		= ab->dev;
871 	ab->phy.otg		= otg;
872 	ab->phy.label		= "ab8500";
873 	ab->phy.set_suspend	= ab8500_usb_set_suspend;
874 	ab->phy.otg->state	= OTG_STATE_UNDEFINED;
875 
876 	otg->usb_phy		= &ab->phy;
877 	otg->set_host		= ab8500_usb_set_host;
878 	otg->set_peripheral	= ab8500_usb_set_peripheral;
879 
880 	if (is_ab8500(ab->ab8500)) {
881 		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
882 			AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
883 			AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
884 			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
885 	} else if (is_ab8505(ab->ab8500)) {
886 		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
887 			AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
888 			AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
889 			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
890 	}
891 
892 	/* Disable regulator voltage setting for AB8500 <= v2.0 */
893 	if (is_ab8500_2p0_or_earlier(ab->ab8500))
894 		ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
895 
896 	platform_set_drvdata(pdev, ab);
897 
898 	/* all: Disable phy when called from set_host and set_peripheral */
899 	INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
900 
901 	err = ab8500_usb_regulator_get(ab);
902 	if (err)
903 		return err;
904 
905 	ab->sysclk = devm_clk_get(ab->dev, "sysclk");
906 	if (IS_ERR(ab->sysclk)) {
907 		dev_err(ab->dev, "Could not get sysclk.\n");
908 		return PTR_ERR(ab->sysclk);
909 	}
910 
911 	err = ab8500_usb_irq_setup(pdev, ab);
912 	if (err < 0)
913 		return err;
914 
915 	err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
916 	if (err) {
917 		dev_err(&pdev->dev, "Can't register transceiver\n");
918 		return err;
919 	}
920 
921 	if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
922 		/* Phy tuning values for AB8500 > v2.0 */
923 		ab8500_usb_set_ab8500_tuning_values(ab);
924 	else if (is_ab8505(ab->ab8500))
925 		/* Phy tuning values for AB8505 */
926 		ab8500_usb_set_ab8505_tuning_values(ab);
927 
928 	/* Needed to enable ID detection. */
929 	ab8500_usb_wd_workaround(ab);
930 
931 	/*
932 	 * This is required for usb-link-status to work properly when a
933 	 * cable is connected at boot time.
934 	 */
935 	ab8500_usb_restart_phy(ab);
936 
937 	abx500_usb_link_status_update(ab);
938 
939 	dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
940 
941 	return 0;
942 }
943 
944 static int ab8500_usb_remove(struct platform_device *pdev)
945 {
946 	struct ab8500_usb *ab = platform_get_drvdata(pdev);
947 
948 	cancel_work_sync(&ab->phy_dis_work);
949 
950 	usb_remove_phy(&ab->phy);
951 
952 	if (ab->mode == USB_HOST)
953 		ab8500_usb_host_phy_dis(ab);
954 	else if (ab->mode == USB_PERIPHERAL)
955 		ab8500_usb_peri_phy_dis(ab);
956 
957 	return 0;
958 }
959 
960 static const struct platform_device_id ab8500_usb_devtype[] = {
961 	{ .name = "ab8500-usb", },
962 	{ /* sentinel */ }
963 };
964 MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
965 
966 static struct platform_driver ab8500_usb_driver = {
967 	.probe		= ab8500_usb_probe,
968 	.remove		= ab8500_usb_remove,
969 	.id_table	= ab8500_usb_devtype,
970 	.driver		= {
971 		.name	= "abx5x0-usb",
972 	},
973 };
974 
975 static int __init ab8500_usb_init(void)
976 {
977 	return platform_driver_register(&ab8500_usb_driver);
978 }
979 subsys_initcall(ab8500_usb_init);
980 
981 static void __exit ab8500_usb_exit(void)
982 {
983 	platform_driver_unregister(&ab8500_usb_driver);
984 }
985 module_exit(ab8500_usb_exit);
986 
987 MODULE_AUTHOR("ST-Ericsson AB");
988 MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
989 MODULE_LICENSE("GPL");
990