xref: /openbmc/linux/drivers/usb/phy/phy-ab8500-usb.c (revision 54dfbb08)
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 	if (mA)
582 		atomic_notifier_call_chain(&ab->phy.notifier,
583 				UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
584 	return 0;
585 }
586 
587 /* TODO: Implement some way for charging or other drivers to read
588  * ab->vbus_draw.
589  */
590 
591 static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
592 {
593 	/* TODO */
594 	return 0;
595 }
596 
597 static int ab8500_usb_set_peripheral(struct usb_otg *otg,
598 					struct usb_gadget *gadget)
599 {
600 	struct ab8500_usb *ab;
601 
602 	if (!otg)
603 		return -ENODEV;
604 
605 	ab = phy_to_ab(otg->phy);
606 
607 	/* Some drivers call this function in atomic context.
608 	 * Do not update ab8500 registers directly till this
609 	 * is fixed.
610 	 */
611 
612 	if (!gadget) {
613 		otg->gadget = NULL;
614 		schedule_work(&ab->phy_dis_work);
615 	} else {
616 		otg->gadget = gadget;
617 		otg->phy->state = OTG_STATE_B_IDLE;
618 
619 		/* Phy will not be enabled if cable is already
620 		 * plugged-in. Schedule to enable phy.
621 		 * Use same delay to avoid any race condition.
622 		 */
623 		schedule_delayed_work(&ab->dwork, ab->link_status_wait);
624 	}
625 
626 	return 0;
627 }
628 
629 static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
630 {
631 	struct ab8500_usb *ab;
632 
633 	if (!otg)
634 		return -ENODEV;
635 
636 	ab = phy_to_ab(otg->phy);
637 
638 	/* Some drivers call this function in atomic context.
639 	 * Do not update ab8500 registers directly till this
640 	 * is fixed.
641 	 */
642 
643 	if (!host) {
644 		otg->host = NULL;
645 		schedule_work(&ab->phy_dis_work);
646 	} else {
647 		otg->host = host;
648 		/* Phy will not be enabled if cable is already
649 		 * plugged-in. Schedule to enable phy.
650 		 * Use same delay to avoid any race condition.
651 		 */
652 		schedule_delayed_work(&ab->dwork, ab->link_status_wait);
653 	}
654 
655 	return 0;
656 }
657 
658 static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
659 {
660 	int err;
661 
662 	ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
663 	if (IS_ERR(ab->v_ape)) {
664 		dev_err(ab->dev, "Could not get v-ape supply\n");
665 		err = PTR_ERR(ab->v_ape);
666 		return err;
667 	}
668 
669 	ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
670 	if (IS_ERR(ab->v_ulpi)) {
671 		dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
672 		err = PTR_ERR(ab->v_ulpi);
673 		return err;
674 	}
675 
676 	ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
677 	if (IS_ERR(ab->v_musb)) {
678 		dev_err(ab->dev, "Could not get musb_1v8 supply\n");
679 		err = PTR_ERR(ab->v_musb);
680 		return err;
681 	}
682 
683 	return 0;
684 }
685 
686 static int ab8500_usb_irq_setup(struct platform_device *pdev,
687 		struct ab8500_usb *ab)
688 {
689 	int err;
690 	int irq;
691 
692 	irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
693 	if (irq < 0) {
694 		dev_err(&pdev->dev, "Link status irq not found\n");
695 		return irq;
696 	}
697 	err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
698 			ab8500_usb_link_status_irq,
699 			IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
700 	if (err < 0) {
701 		dev_err(ab->dev, "request_irq failed for link status irq\n");
702 		return err;
703 	}
704 
705 	irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
706 	if (irq < 0) {
707 		dev_err(&pdev->dev, "ID fall irq not found\n");
708 		return irq;
709 	}
710 	err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
711 			ab8500_usb_disconnect_irq,
712 			IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
713 	if (err < 0) {
714 		dev_err(ab->dev, "request_irq failed for ID fall irq\n");
715 		return err;
716 	}
717 
718 	irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
719 	if (irq < 0) {
720 		dev_err(&pdev->dev, "VBUS fall irq not found\n");
721 		return irq;
722 	}
723 	err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
724 			ab8500_usb_disconnect_irq,
725 			IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
726 	if (err < 0) {
727 		dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
728 		return err;
729 	}
730 
731 	return 0;
732 }
733 
734 static int ab8500_usb_probe(struct platform_device *pdev)
735 {
736 	struct ab8500_usb	*ab;
737 	struct ab8500		*ab8500;
738 	struct usb_otg		*otg;
739 	int err;
740 	int rev;
741 
742 	ab8500 = dev_get_drvdata(pdev->dev.parent);
743 	rev = abx500_get_chip_id(&pdev->dev);
744 
745 	if (is_ab8500_1p1_or_earlier(ab8500)) {
746 		dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
747 		return -ENODEV;
748 	}
749 
750 	ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
751 	if (!ab)
752 		return -ENOMEM;
753 
754 	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
755 	if (!otg)
756 		return -ENOMEM;
757 
758 	ab->dev			= &pdev->dev;
759 	ab->ab8500		= ab8500;
760 	ab->phy.dev		= ab->dev;
761 	ab->phy.otg		= otg;
762 	ab->phy.label		= "ab8500";
763 	ab->phy.set_suspend	= ab8500_usb_set_suspend;
764 	ab->phy.set_power	= ab8500_usb_set_power;
765 	ab->phy.state		= OTG_STATE_UNDEFINED;
766 
767 	otg->phy		= &ab->phy;
768 	otg->set_host		= ab8500_usb_set_host;
769 	otg->set_peripheral	= ab8500_usb_set_peripheral;
770 
771 	platform_set_drvdata(pdev, ab);
772 
773 	ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
774 
775 	/* v1: Wait for link status to become stable.
776 	 * all: Updates form set_host and set_peripheral as they are atomic.
777 	 */
778 	INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
779 
780 	/* all: Disable phy when called from set_host and set_peripheral */
781 	INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
782 
783 	err = ab8500_usb_regulator_get(ab);
784 	if (err)
785 		return err;
786 
787 	err = ab8500_usb_irq_setup(pdev, ab);
788 	if (err < 0)
789 		return err;
790 
791 	err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
792 	if (err) {
793 		dev_err(&pdev->dev, "Can't register transceiver\n");
794 		return err;
795 	}
796 
797 	/* Phy tuning values for AB8500 */
798 	if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
799 		/* Enable the PBT/Bank 0x12 access */
800 		err = abx500_set_register_interruptible(ab->dev,
801 				AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
802 		if (err < 0)
803 			dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
804 					err);
805 
806 		err = abx500_set_register_interruptible(ab->dev,
807 				AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
808 		if (err < 0)
809 			dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
810 					err);
811 
812 		err = abx500_set_register_interruptible(ab->dev,
813 				AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
814 		if (err < 0)
815 			dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
816 					err);
817 
818 		err = abx500_set_register_interruptible(ab->dev,
819 				AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
820 		if (err < 0)
821 			dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
822 					err);
823 
824 		/* Switch to normal mode/disable Bank 0x12 access */
825 		err = abx500_set_register_interruptible(ab->dev,
826 				AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
827 		if (err < 0)
828 			dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
829 					err);
830 	}
831 
832 	/* Phy tuning values for AB8505 */
833 	if (is_ab8505(ab->ab8500)) {
834 		/* Enable the PBT/Bank 0x12 access */
835 		err = abx500_mask_and_set_register_interruptible(ab->dev,
836 				AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
837 				0x01, 0x01);
838 		if (err < 0)
839 			dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
840 					err);
841 
842 		err = abx500_mask_and_set_register_interruptible(ab->dev,
843 				AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
844 				0xC8, 0xC8);
845 		if (err < 0)
846 			dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
847 					err);
848 
849 		err = abx500_mask_and_set_register_interruptible(ab->dev,
850 				AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
851 				0x60, 0x60);
852 		if (err < 0)
853 			dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
854 					err);
855 
856 		err = abx500_mask_and_set_register_interruptible(ab->dev,
857 				AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
858 				0xFC, 0x80);
859 
860 		if (err < 0)
861 			dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
862 					err);
863 
864 		/* Switch to normal mode/disable Bank 0x12 access */
865 		err = abx500_mask_and_set_register_interruptible(ab->dev,
866 				AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
867 				0x00, 0x00);
868 		if (err < 0)
869 			dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
870 					err);
871 	}
872 
873 	/* Needed to enable ID detection. */
874 	ab8500_usb_wd_workaround(ab);
875 
876 	dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
877 
878 	return 0;
879 }
880 
881 static int ab8500_usb_remove(struct platform_device *pdev)
882 {
883 	struct ab8500_usb *ab = platform_get_drvdata(pdev);
884 
885 	cancel_delayed_work_sync(&ab->dwork);
886 
887 	cancel_work_sync(&ab->phy_dis_work);
888 
889 	usb_remove_phy(&ab->phy);
890 
891 	ab8500_usb_host_phy_dis(ab);
892 	ab8500_usb_peri_phy_dis(ab);
893 
894 	platform_set_drvdata(pdev, NULL);
895 
896 	return 0;
897 }
898 
899 static struct platform_driver ab8500_usb_driver = {
900 	.probe		= ab8500_usb_probe,
901 	.remove		= ab8500_usb_remove,
902 	.driver		= {
903 		.name	= "ab8500-usb",
904 		.owner	= THIS_MODULE,
905 	},
906 };
907 
908 static int __init ab8500_usb_init(void)
909 {
910 	return platform_driver_register(&ab8500_usb_driver);
911 }
912 subsys_initcall(ab8500_usb_init);
913 
914 static void __exit ab8500_usb_exit(void)
915 {
916 	platform_driver_unregister(&ab8500_usb_driver);
917 }
918 module_exit(ab8500_usb_exit);
919 
920 MODULE_ALIAS("platform:ab8500_usb");
921 MODULE_AUTHOR("ST-Ericsson AB");
922 MODULE_DESCRIPTION("AB8500 usb transceiver driver");
923 MODULE_LICENSE("GPL");
924