aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-ux500/musb_db8500.c
blob: 89e00b364ab532720c19916e0ce651deefbc4b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (C) 2009 ST Ericsson.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/err.h>
#include <linux/usb/musb.h>
#include <mach/ab8500.h>
#include <mach/musb_db8500.h>

#ifdef CONFIG_REGULATOR
#include <linux/regulator/consumer.h>
struct regulator *musb_vape_supply, *musb_vbus_supply;
#endif

int boot_time_flag;
/**
 * stm_musb_states - Different states of musb_chip
 *
 * Used for USB cable plug-in state machine
 */
enum stm_musb_states {
	USB_IDLE,
	USB_DEVICE,
	USB_HOST,
};
enum stm_musb_states stm_musb_curr_state = USB_IDLE;

void musb_set_session(void);

/**
 * usb_kick_watchdog() - Kick the watch dog timer
 *
 * This function used to Kick the watch dog timer
 */
void usb_kick_watchdog(void)
{
	ab8500_write(AB8500_SYS_CTRL2_BLOCK,
			AB8500_MAIN_WDOG_CTRL_REG, AB8500_MAIN_WATCHDOG_ENABLE);
	ab8500_write(AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WDOG_CTRL_REG,
			(AB8500_MAIN_WATCHDOG_ENABLE | AB8500_MAIN_WATCHDOG_KICK));
	mdelay(WATCHDOG_DELAY);
	ab8500_write(AB8500_SYS_CTRL2_BLOCK,
			AB8500_MAIN_WDOG_CTRL_REG, AB8500_MAIN_WATCHDOG_DISABLE);
	mdelay(WATCHDOG_DELAY);
}
/**
 * usb_host_phy_en() - for enabling the 5V to usb host
 * @enable: to enabling the Phy for host.
 *
 * This function used to set the voltage for USB host mode
 */
void usb_host_phy_en(int enable)
{
	if (enable == USB_ENABLE) {
		if (boot_time_flag) {
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_ENABLE);
			ab8500_write(AB8500_USB, AB8500_USB_PHY_CTRL_REG,
					AB8500_USB_HOST_ENABLE);
		} else {
		#ifdef CONFIG_REGULATOR
			regulator_enable(musb_vbus_supply);
			regulator_set_optimum_mode(musb_vape_supply, 1);
		#else
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_ENABLE);
		#endif
			ab8500_write(AB8500_USB,
					AB8500_USB_PHY_CTRL_REG, AB8500_USB_HOST_ENABLE);
		}
	} else {
		if (boot_time_flag) {
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_DISABLE);
			ab8500_write(AB8500_USB,
					AB8500_USB_PHY_CTRL_REG, AB8500_USB_HOST_DISABLE);
			boot_time_flag = USB_DISABLE;
		} else {
		#ifdef CONFIG_REGULATOR
			regulator_enable(musb_vbus_supply);
			regulator_set_optimum_mode(musb_vape_supply, 0);
		#else
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_DISABLE);
		#endif
			ab8500_write(AB8500_USB,
					AB8500_USB_PHY_CTRL_REG, AB8500_USB_HOST_DISABLE);
		}
	}
}
/**
 * usb_host_insert_handler() - Detect the USB host cable insertion
 *
 * This function used to detect the USB host cable insertion.
 */
void usb_host_insert_handler(void)
{
	if (stm_musb_curr_state == USB_IDLE) {
		stm_musb_curr_state = USB_HOST;
		usb_host_phy_en(USB_ENABLE);
		musb_set_session();
	}
}
/**
 * usb_host_remove_handler() - Removed the USB host cable
 *
 * This function used to detect the USB host cable removed.
 */
void usb_host_remove_handler(void)
{
	if (stm_musb_curr_state == USB_HOST) {
		stm_musb_curr_state = USB_IDLE;
		usb_host_phy_en(USB_DISABLE);
	}
}
/**
 * usb_device_phy_en() - for enabling the 5V to usb gadget
 * @enable: to enabling the Phy for device.
 *
 * This function used to set the voltage for USB gadget mode.
 */
void usb_device_phy_en(int enable)
{
	if (enable == USB_ENABLE) {
		usb_kick_watchdog();
		if (boot_time_flag) {
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_ENABLE);
			ab8500_write(AB8500_USB, AB8500_USB_PHY_CTRL_REG, AB8500_USB_DEVICE_ENABLE);
		} else {
		#ifdef CONFIG_REGULATOR
			regulator_enable(musb_vbus_supply);
			regulator_set_optimum_mode(musb_vape_supply, 1);
		#else
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_ENABLE);
		#endif
			ab8500_write(AB8500_USB, AB8500_USB_PHY_CTRL_REG,
					AB8500_USB_DEVICE_ENABLE);
		}
	} else {
		if (boot_time_flag) {
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_DISABLE);
			ab8500_write(AB8500_USB,
					AB8500_USB_PHY_CTRL_REG, AB8500_USB_DEVICE_DISABLE);
			boot_time_flag = USB_DISABLE;
		} else {
		#ifdef CONFIG_REGULATOR
			regulator_disable(musb_vbus_supply);
			regulator_set_optimum_mode(musb_vape_supply, 0);
		#else
			ab8500_write(AB8500_REGU_CTRL1,
					AB8500_REGU_VUSB_CTRL_REG, AB8500_VBUS_DISABLE);
		#endif
			ab8500_write(AB8500_USB, AB8500_USB_PHY_CTRL_REG,
					AB8500_USB_DEVICE_DISABLE);
		}
	}
}
/**
 * usb_device_insert_handler() - for enabling the 5V to usb device
 *
 * This function used to set the voltage for USB device mode
 */
void usb_device_insert_handler(void)
{
	if (stm_musb_curr_state == USB_IDLE) {
		stm_musb_curr_state = USB_DEVICE;
		usb_device_phy_en(USB_ENABLE);
	}
}
/**
 * usb_device_remove_handler() - remove the 5V to usb device
 *
 * This function used to remove the voltage for USB device mode.
 */
void usb_device_remove_handler(void)
{
	if (stm_musb_curr_state == USB_DEVICE) {
		stm_musb_curr_state = USB_IDLE;
		usb_device_phy_en(USB_DISABLE);
	}
}
/**
 * musb_phy_en : register USB callback handlers for ab8500
 * @mode: value for mode.
 *
 * This function is used to register USB callback handlers for ab8500.
 */
int musb_phy_en(u8 mode)
{
	u8 ab8500_rev;
	int ret;

#ifdef CONFIG_REGULATOR
	musb_vape_supply = regulator_get(NULL, "v-ape");
	if (IS_ERR(musb_vape_supply)) {
		ret = PTR_ERR(musb_vape_supply);
		printk(KERN_WARNING "failed to get v-ape supply\n");
		return -1;
	}
	regulator_enable(musb_vape_supply);
	musb_vbus_supply = regulator_get(NULL, "v-bus");
	if (IS_ERR(musb_vbus_supply)) {
		ret = PTR_ERR(musb_vbus_supply);
		printk(KERN_WARNING "failed to get v-bus supply\n");
		return -1;
	}
#endif
	ab8500_rev = ab8500_read(AB8500_MISC, AB8500_REV_REG);
	if ((ab8500_rev == AB8500_REV_10) || (ab8500_rev == AB8500_REV_11)) {
		if (mode == MUSB_HOST || mode == MUSB_OTG) {
			ab8500_write
				(AB8500_INTERRUPT,
					AB8500_IT_MASK20_REG, AB8500_IT_MASK20_MASK);
			ret = ab8500_set_callback_handler
				(AB8500_ID_WAKEUP_RISING,
					usb_host_insert_handler, NULL);
			if (ret < 0) {
				printk(KERN_ERR "failed to set the callback"
						" handler for usb host"
						" insertion\n");
				return ret;
			}
			ab8500_write
				(AB8500_INTERRUPT, AB8500_IT_MASK21_REG,
					AB8500_IT_MASK21_MASK);
			ret = ab8500_set_callback_handler
				(AB8500_ID_WAKEUP_FALLING,
					usb_host_remove_handler, NULL);
			if (ret < 0) {
				printk(KERN_ERR "failed to set the callback"
						" handler for usb host"
						" removal\n");
				return ret;
			}
			usb_kick_watchdog();
		}
		if (mode == MUSB_PERIPHERAL || mode == MUSB_OTG) {
			ab8500_write
				(AB8500_INTERRUPT, AB8500_IT_MASK2_REG,
						AB8500_IT_MASK2_MASK);
			ret = ab8500_set_callback_handler
				(AB8500_VBUS_RISING, usb_device_insert_handler, NULL);
			if (ret < 0) {
				printk(KERN_ERR "failed to set the callback"
						" handler for usb device"
						" insertion\n");
				return ret;
			}
			ret = ab8500_set_callback_handler
				(AB8500_VBUS_FALLING, usb_device_remove_handler, NULL);
			if (ret < 0) {
				printk(KERN_ERR "failed to set the callback"
						" handler for usb host"
						" removal\n");
				return ret;
			}
		}
	}
	return 0;
}
/**
 * musb_force_detect : detect the USB cable during boot time.
 * @mode: value for mode.
 *
 * This function is used to detect the USB cable during boot time.
 */
int musb_force_detect(u8 mode)
{
	u8 ab8500_rev;
	int usb_status = 0;
	ab8500_rev = ab8500_read(AB8500_MISC, AB8500_REV_REG);
	if ((ab8500_rev == AB8500_REV_10) || (ab8500_rev == AB8500_REV_11)) {
		if (mode == MUSB_HOST || mode == MUSB_OTG) {
			usb_status = ab8500_read
				(AB8500_INTERRUPT, AB8500_IT_SOURCE20_REG);
			if (usb_status & AB8500_SRC_INT_USB_HOST) {
				boot_time_flag = USB_ENABLE;
				usb_host_phy_en(USB_ENABLE);
			}
		}
		if (mode == MUSB_PERIPHERAL || mode == MUSB_OTG) {
			usb_status = ab8500_read
				(AB8500_INTERRUPT, AB8500_IT_SOURCE2_REG);
			if (usb_status & AB8500_SRC_INT_USB_DEVICE) {
				boot_time_flag = USB_ENABLE;
				usb_device_phy_en(USB_ENABLE);
			}
		}
	}
	return 0;
}