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