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