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