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