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