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