blob: ceab508b90e8671bfde7eec754d94bdea95027fd [file] [log] [blame]
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001/*
2 * drivers/usb/otg/ab8500_usb.c
3 *
Fabio Baltieri0c380c02013-05-15 14:03:32 +02004 * USB transceiver driver for AB8500 family chips
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01005 *
Fabio Baltieri0c380c02013-05-15 14:03:32 +02006 * Copyright (C) 2010-2013 ST-Ericsson AB
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01007 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
Fabio Baltieri0c380c02013-05-15 14:03:32 +02008 * Avinash Kumar <avinash.kumar@stericsson.com>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/platform_device.h>
28#include <linux/usb/otg.h>
29#include <linux/slab.h>
30#include <linux/notifier.h>
31#include <linux/interrupt.h>
32#include <linux/delay.h>
Mian Yousaf Kaukabd0ed0642013-05-15 14:03:26 +020033#include <linux/clk.h>
34#include <linux/err.h>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010035#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010036#include <linux/mfd/abx500/ab8500.h>
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080037#include <linux/usb/musb-ux500.h>
Fabio Baltierie65b36c2013-04-03 10:45:06 +020038#include <linux/regulator/consumer.h>
Patrice Chotard899f0f52013-04-03 10:45:11 +020039#include <linux/pinctrl/consumer.h>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010040
Sakethram Bommisetti71246312013-04-03 10:45:04 +020041/* Bank AB8500_SYS_CTRL2_BLOCK */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010042#define AB8500_MAIN_WD_CTRL_REG 0x01
Sakethram Bommisetti71246312013-04-03 10:45:04 +020043
44/* Bank AB8500_USB */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010045#define AB8500_USB_LINE_STAT_REG 0x80
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080046#define AB8505_USB_LINE_STAT_REG 0x94
Fabio Baltieri0c380c02013-05-15 14:03:32 +020047#define AB8540_USB_LINK_STAT_REG 0x94
48#define AB8540_USB_OTG_CTL_REG 0x87
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010049#define AB8500_USB_PHY_CTRL_REG 0x8A
Fabio Baltieri0c380c02013-05-15 14:03:32 +020050#define AB8540_VBUS_CTRL_REG 0x82
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010051
Sakethram Bommisetti71246312013-04-03 10:45:04 +020052/* Bank AB8500_DEVELOPMENT */
53#define AB8500_BANK12_ACCESS 0x00
54
55/* Bank AB8500_DEBUG */
Fabio Baltieri0c380c02013-05-15 14:03:32 +020056#define AB8540_DEBUG 0x32
Sakethram Bommisetti71246312013-04-03 10:45:04 +020057#define AB8500_USB_PHY_TUNE1 0x05
58#define AB8500_USB_PHY_TUNE2 0x06
59#define AB8500_USB_PHY_TUNE3 0x07
60
Fabio Baltieri0c380c02013-05-15 14:03:32 +020061/* Bank AB8500_INTERRUPT */
62#define AB8500_IT_SOURCE2_REG 0x01
63
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010064#define AB8500_BIT_OTG_STAT_ID (1 << 0)
65#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
66#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
67#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
68#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
Fabio Baltieri0c380c02013-05-15 14:03:32 +020069#define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
70#define AB8540_BIT_OTG_CTL_VBUS_VALID_ENA (1 << 0)
71#define AB8540_BIT_OTG_CTL_ID_HOST_ENA (1 << 1)
72#define AB8540_BIT_OTG_CTL_ID_DEV_ENA (1 << 5)
73#define AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA (1 << 0)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010074
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010075#define AB8500_WD_KICK_DELAY_US 100 /* usec */
76#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080077#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010078
79/* Usb line status register */
80enum ab8500_usb_link_status {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080081 USB_LINK_NOT_CONFIGURED_8500 = 0,
82 USB_LINK_STD_HOST_NC_8500,
83 USB_LINK_STD_HOST_C_NS_8500,
84 USB_LINK_STD_HOST_C_S_8500,
85 USB_LINK_HOST_CHG_NM_8500,
86 USB_LINK_HOST_CHG_HS_8500,
87 USB_LINK_HOST_CHG_HS_CHIRP_8500,
88 USB_LINK_DEDICATED_CHG_8500,
89 USB_LINK_ACA_RID_A_8500,
90 USB_LINK_ACA_RID_B_8500,
91 USB_LINK_ACA_RID_C_NM_8500,
92 USB_LINK_ACA_RID_C_HS_8500,
93 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
94 USB_LINK_HM_IDGND_8500,
95 USB_LINK_RESERVED_8500,
96 USB_LINK_NOT_VALID_LINK_8500,
97};
98
99enum ab8505_usb_link_status {
100 USB_LINK_NOT_CONFIGURED_8505 = 0,
101 USB_LINK_STD_HOST_NC_8505,
102 USB_LINK_STD_HOST_C_NS_8505,
103 USB_LINK_STD_HOST_C_S_8505,
104 USB_LINK_CDP_8505,
105 USB_LINK_RESERVED0_8505,
106 USB_LINK_RESERVED1_8505,
107 USB_LINK_DEDICATED_CHG_8505,
108 USB_LINK_ACA_RID_A_8505,
109 USB_LINK_ACA_RID_B_8505,
110 USB_LINK_ACA_RID_C_NM_8505,
111 USB_LINK_RESERVED2_8505,
112 USB_LINK_RESERVED3_8505,
113 USB_LINK_HM_IDGND_8505,
114 USB_LINK_CHARGERPORT_NOT_OK_8505,
115 USB_LINK_CHARGER_DM_HIGH_8505,
116 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
117 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
118 USB_LINK_STD_UPSTREAM_8505,
119 USB_LINK_CHARGER_SE1_8505,
120 USB_LINK_CARKIT_CHGR_1_8505,
121 USB_LINK_CARKIT_CHGR_2_8505,
122 USB_LINK_ACA_DOCK_CHGR_8505,
123 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
124 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
125 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
126 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
127 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
128};
129
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200130enum ab8540_usb_link_status {
131 USB_LINK_NOT_CONFIGURED_8540 = 0,
132 USB_LINK_STD_HOST_NC_8540,
133 USB_LINK_STD_HOST_C_NS_8540,
134 USB_LINK_STD_HOST_C_S_8540,
135 USB_LINK_CDP_8540,
136 USB_LINK_RESERVED0_8540,
137 USB_LINK_RESERVED1_8540,
138 USB_LINK_DEDICATED_CHG_8540,
139 USB_LINK_ACA_RID_A_8540,
140 USB_LINK_ACA_RID_B_8540,
141 USB_LINK_ACA_RID_C_NM_8540,
142 USB_LINK_RESERVED2_8540,
143 USB_LINK_RESERVED3_8540,
144 USB_LINK_HM_IDGND_8540,
145 USB_LINK_CHARGERPORT_NOT_OK_8540,
146 USB_LINK_CHARGER_DM_HIGH_8540,
147 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540,
148 USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540,
149 USB_LINK_STD_UPSTREAM_8540,
150 USB_LINK_CHARGER_SE1_8540,
151 USB_LINK_CARKIT_CHGR_1_8540,
152 USB_LINK_CARKIT_CHGR_2_8540,
153 USB_LINK_ACA_DOCK_CHGR_8540,
154 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8540,
155 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8540,
156 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8540,
157 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8540,
158 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8540
159};
160
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800161enum ab8500_usb_mode {
162 USB_IDLE = 0,
163 USB_PERIPHERAL,
164 USB_HOST,
165 USB_DEDICATED_CHG
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100166};
167
Fabio Baltieribd4c9f02013-05-15 14:03:31 +0200168/* Register USB_LINK_STATUS interrupt */
169#define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ (1 << 0)
170/* Register ID_WAKEUP_F interrupt */
171#define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ (1 << 1)
172/* Register VBUS_DET_F interrupt */
173#define AB8500_USB_FLAG_USE_VBUS_DET_IRQ (1 << 2)
174/* Driver is using the ab-iddet driver*/
175#define AB8500_USB_FLAG_USE_AB_IDDET (1 << 3)
176/* Enable setting regulators voltage */
177#define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE (1 << 4)
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200178/* Enable the check_vbus_status workaround */
179#define AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS (1 << 5)
180/* Enable the vbus host workaround */
181#define AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK (1 << 6)
Fabio Baltieribd4c9f02013-05-15 14:03:31 +0200182
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100183struct ab8500_usb {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200184 struct usb_phy phy;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100185 struct device *dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800186 struct ab8500 *ab8500;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100187 unsigned vbus_draw;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100188 struct work_struct phy_dis_work;
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200189 struct work_struct vbus_event_work;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800190 enum ab8500_usb_mode mode;
Mian Yousaf Kaukabd0ed0642013-05-15 14:03:26 +0200191 struct clk *sysclk;
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200192 struct regulator *v_ape;
193 struct regulator *v_musb;
194 struct regulator *v_ulpi;
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200195 int saved_v_ulpi;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800196 int previous_link_status_state;
Patrice Chotard899f0f52013-04-03 10:45:11 +0200197 struct pinctrl *pinctrl;
198 struct pinctrl_state *pins_sleep;
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200199 bool enabled_charging_detection;
Fabio Baltieribd4c9f02013-05-15 14:03:31 +0200200 unsigned int flags;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100201};
202
Heikki Krogerus144713f2012-02-13 13:24:05 +0200203static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100204{
Heikki Krogerus144713f2012-02-13 13:24:05 +0200205 return container_of(x, struct ab8500_usb, phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100206}
207
208static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
209{
210 abx500_set_register_interruptible(ab->dev,
211 AB8500_SYS_CTRL2_BLOCK,
212 AB8500_MAIN_WD_CTRL_REG,
213 AB8500_BIT_WD_CTRL_ENABLE);
214
215 udelay(AB8500_WD_KICK_DELAY_US);
216
217 abx500_set_register_interruptible(ab->dev,
218 AB8500_SYS_CTRL2_BLOCK,
219 AB8500_MAIN_WD_CTRL_REG,
220 (AB8500_BIT_WD_CTRL_ENABLE
221 | AB8500_BIT_WD_CTRL_KICK));
222
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800223 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100224
225 abx500_set_register_interruptible(ab->dev,
226 AB8500_SYS_CTRL2_BLOCK,
227 AB8500_MAIN_WD_CTRL_REG,
228 0);
229}
230
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200231static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
232{
233 int ret, volt;
234
Fabio Baltieri88b1c782013-04-03 16:02:25 +0200235 ret = regulator_enable(ab->v_ape);
236 if (ret)
237 dev_err(ab->dev, "Failed to enable v-ape\n");
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200238
Fabio Baltieribd4c9f02013-05-15 14:03:31 +0200239 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200240 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
241 if (ab->saved_v_ulpi < 0)
242 dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
243
244 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
245 if (ret < 0)
246 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
247 ret);
248
249 ret = regulator_set_optimum_mode(ab->v_ulpi, 28000);
250 if (ret < 0)
251 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
252 ret);
253 }
254
Fabio Baltieri88b1c782013-04-03 16:02:25 +0200255 ret = regulator_enable(ab->v_ulpi);
256 if (ret)
257 dev_err(ab->dev, "Failed to enable vddulpivio18\n");
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200258
Fabio Baltieribd4c9f02013-05-15 14:03:31 +0200259 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200260 volt = regulator_get_voltage(ab->v_ulpi);
261 if ((volt != 1300000) && (volt != 1350000))
262 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
263 volt);
264 }
265
Fabio Baltieri88b1c782013-04-03 16:02:25 +0200266 ret = regulator_enable(ab->v_musb);
267 if (ret)
268 dev_err(ab->dev, "Failed to enable musb_1v8\n");
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200269}
270
271static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
272{
273 int ret;
274
275 regulator_disable(ab->v_musb);
276
277 regulator_disable(ab->v_ulpi);
278
279 /* USB is not the only consumer of Vintcore, restore old settings */
Fabio Baltieribd4c9f02013-05-15 14:03:31 +0200280 if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200281 if (ab->saved_v_ulpi > 0) {
282 ret = regulator_set_voltage(ab->v_ulpi,
283 ab->saved_v_ulpi, ab->saved_v_ulpi);
284 if (ret < 0)
285 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
286 ab->saved_v_ulpi, ret);
287 }
288
289 ret = regulator_set_optimum_mode(ab->v_ulpi, 0);
290 if (ret < 0)
291 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
292 ret);
293 }
294
295 regulator_disable(ab->v_ape);
296}
297
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800298static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
299{
300 /* Workaround for v2.0 bug # 31952 */
301 if (is_ab8500_2p0(ab->ab8500)) {
302 abx500_mask_and_set_register_interruptible(ab->dev,
303 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
304 bit, bit);
305 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
306 }
307}
308
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200309static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100310{
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200311 u8 bit;
312 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
313 AB8500_BIT_PHY_CTRL_DEVICE_EN;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100314
Patrice Chotard899f0f52013-04-03 10:45:11 +0200315 /* mux and configure USB pins to DEFAULT state */
316 ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
317 if (IS_ERR(ab->pinctrl))
318 dev_err(ab->dev, "could not get/set default pinstate\n");
319
Mian Yousaf Kaukabd0ed0642013-05-15 14:03:26 +0200320 if (clk_prepare_enable(ab->sysclk))
321 dev_err(ab->dev, "can't prepare/enable clock\n");
322
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200323 ab8500_usb_regulator_enable(ab);
324
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200325 abx500_mask_and_set_register_interruptible(ab->dev,
326 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
327 bit, bit);
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200328
329 if (ab->flags & AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK) {
330 if (sel_host)
331 abx500_set_register_interruptible(ab->dev,
332 AB8500_USB, AB8540_USB_OTG_CTL_REG,
333 AB8540_BIT_OTG_CTL_VBUS_VALID_ENA |
334 AB8540_BIT_OTG_CTL_ID_HOST_ENA |
335 AB8540_BIT_OTG_CTL_ID_DEV_ENA);
336 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100337}
338
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200339static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
340{
341 u8 bit;
342 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
343 AB8500_BIT_PHY_CTRL_DEVICE_EN;
344
345 ab8500_usb_wd_linkstatus(ab, bit);
346
347 abx500_mask_and_set_register_interruptible(ab->dev,
348 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
349 bit, 0);
350
351 /* Needed to disable the phy.*/
352 ab8500_usb_wd_workaround(ab);
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200353
Mian Yousaf Kaukabd0ed0642013-05-15 14:03:26 +0200354 clk_disable_unprepare(ab->sysclk);
355
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200356 ab8500_usb_regulator_disable(ab);
Patrice Chotard899f0f52013-04-03 10:45:11 +0200357
358 if (!IS_ERR(ab->pinctrl)) {
359 /* configure USB pins to SLEEP state */
360 ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
361 PINCTRL_STATE_SLEEP);
362
363 if (IS_ERR(ab->pins_sleep))
364 dev_dbg(ab->dev, "could not get sleep pinstate\n");
365 else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
366 dev_err(ab->dev, "could not set pins to sleep state\n");
367
Fabio Baltieri3147dad2013-05-15 14:03:25 +0200368 /*
369 * as USB pins are shared with iddet, release them to allow
Patrice Chotard899f0f52013-04-03 10:45:11 +0200370 * iddet to request them
371 */
372 pinctrl_put(ab->pinctrl);
373 }
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200374}
375
376#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
377#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
378#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
379#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100380
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200381static int ab8540_usb_link_status_update(struct ab8500_usb *ab,
382 enum ab8540_usb_link_status lsts)
383{
384 enum ux500_musb_vbus_id_status event = 0;
385
386 dev_dbg(ab->dev, "ab8540_usb_link_status_update %d\n", lsts);
387
388 if (ab->enabled_charging_detection) {
389 /* Disable USB Charger detection */
390 abx500_mask_and_set_register_interruptible(ab->dev,
391 AB8500_USB, AB8540_VBUS_CTRL_REG,
392 AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA, 0x00);
393 ab->enabled_charging_detection = false;
394 }
395
396 /*
397 * Spurious link_status interrupts are seen in case of a
398 * disconnection of a device in IDGND and RIDA stage
399 */
400 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8540 &&
401 (lsts == USB_LINK_STD_HOST_C_NS_8540 ||
402 lsts == USB_LINK_STD_HOST_NC_8540))
403 return 0;
404
405 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8540 &&
406 (lsts == USB_LINK_STD_HOST_NC_8540))
407 return 0;
408
409 ab->previous_link_status_state = lsts;
410
411 switch (lsts) {
412 case USB_LINK_ACA_RID_B_8540:
413 event = UX500_MUSB_RIDB;
414 case USB_LINK_NOT_CONFIGURED_8540:
415 case USB_LINK_RESERVED0_8540:
416 case USB_LINK_RESERVED1_8540:
417 case USB_LINK_RESERVED2_8540:
418 case USB_LINK_RESERVED3_8540:
419 ab->mode = USB_IDLE;
420 ab->phy.otg->default_a = false;
421 ab->vbus_draw = 0;
422 if (event != UX500_MUSB_RIDB)
423 event = UX500_MUSB_NONE;
424 /*
425 * Fallback to default B_IDLE as nothing
426 * is connected
427 */
428 ab->phy.state = OTG_STATE_B_IDLE;
429 break;
430
431 case USB_LINK_ACA_RID_C_NM_8540:
432 event = UX500_MUSB_RIDC;
433 case USB_LINK_STD_HOST_NC_8540:
434 case USB_LINK_STD_HOST_C_NS_8540:
435 case USB_LINK_STD_HOST_C_S_8540:
436 case USB_LINK_CDP_8540:
437 if (ab->mode == USB_IDLE) {
438 ab->mode = USB_PERIPHERAL;
439 ab8500_usb_peri_phy_en(ab);
440 atomic_notifier_call_chain(&ab->phy.notifier,
441 UX500_MUSB_PREPARE, &ab->vbus_draw);
442 }
443 if (event != UX500_MUSB_RIDC)
444 event = UX500_MUSB_VBUS;
445 break;
446
447 case USB_LINK_ACA_RID_A_8540:
448 case USB_LINK_ACA_DOCK_CHGR_8540:
449 event = UX500_MUSB_RIDA;
450 case USB_LINK_HM_IDGND_8540:
451 case USB_LINK_STD_UPSTREAM_8540:
452 if (ab->mode == USB_IDLE) {
453 ab->mode = USB_HOST;
454 ab8500_usb_host_phy_en(ab);
455 atomic_notifier_call_chain(&ab->phy.notifier,
456 UX500_MUSB_PREPARE, &ab->vbus_draw);
457 }
458 ab->phy.otg->default_a = true;
459 if (event != UX500_MUSB_RIDA)
460 event = UX500_MUSB_ID;
461 atomic_notifier_call_chain(&ab->phy.notifier,
462 event, &ab->vbus_draw);
463 break;
464
465 case USB_LINK_DEDICATED_CHG_8540:
466 ab->mode = USB_DEDICATED_CHG;
467 event = UX500_MUSB_CHARGER;
468 atomic_notifier_call_chain(&ab->phy.notifier,
469 event, &ab->vbus_draw);
470 break;
471
472 case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540:
473 case USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540:
474 event = UX500_MUSB_NONE;
475 if (ab->mode == USB_HOST) {
476 ab->phy.otg->default_a = false;
477 ab->vbus_draw = 0;
478 atomic_notifier_call_chain(&ab->phy.notifier,
479 event, &ab->vbus_draw);
480 ab8500_usb_host_phy_dis(ab);
481 ab->mode = USB_IDLE;
482 }
483 if (ab->mode == USB_PERIPHERAL) {
484 atomic_notifier_call_chain(&ab->phy.notifier,
485 event, &ab->vbus_draw);
486 ab8500_usb_peri_phy_dis(ab);
487 atomic_notifier_call_chain(&ab->phy.notifier,
488 UX500_MUSB_CLEAN, &ab->vbus_draw);
489 ab->mode = USB_IDLE;
490 ab->phy.otg->default_a = false;
491 ab->vbus_draw = 0;
492 }
493 break;
494
495 default:
496 event = UX500_MUSB_NONE;
497 break;
498 }
499
500 return 0;
501}
502
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800503static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
504 enum ab8505_usb_link_status lsts)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100505{
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800506 enum ux500_musb_vbus_id_status event = 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100507
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800508 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100509
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800510 /*
511 * Spurious link_status interrupts are seen at the time of
512 * disconnection of a device in RIDA state
513 */
514 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
515 (lsts == USB_LINK_STD_HOST_NC_8505))
516 return 0;
517
518 ab->previous_link_status_state = lsts;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100519
520 switch (lsts) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800521 case USB_LINK_ACA_RID_B_8505:
522 event = UX500_MUSB_RIDB;
523 case USB_LINK_NOT_CONFIGURED_8505:
524 case USB_LINK_RESERVED0_8505:
525 case USB_LINK_RESERVED1_8505:
526 case USB_LINK_RESERVED2_8505:
527 case USB_LINK_RESERVED3_8505:
528 ab->mode = USB_IDLE;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200529 ab->phy.otg->default_a = false;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100530 ab->vbus_draw = 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800531 if (event != UX500_MUSB_RIDB)
532 event = UX500_MUSB_NONE;
533 /*
534 * Fallback to default B_IDLE as nothing
535 * is connected
536 */
537 ab->phy.state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100538 break;
539
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800540 case USB_LINK_ACA_RID_C_NM_8505:
541 event = UX500_MUSB_RIDC;
542 case USB_LINK_STD_HOST_NC_8505:
543 case USB_LINK_STD_HOST_C_NS_8505:
544 case USB_LINK_STD_HOST_C_S_8505:
545 case USB_LINK_CDP_8505:
546 if (ab->mode == USB_IDLE) {
547 ab->mode = USB_PERIPHERAL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100548 ab8500_usb_peri_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800549 atomic_notifier_call_chain(&ab->phy.notifier,
550 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100551 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800552 if (event != UX500_MUSB_RIDC)
553 event = UX500_MUSB_VBUS;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100554 break;
555
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800556 case USB_LINK_ACA_RID_A_8505:
557 case USB_LINK_ACA_DOCK_CHGR_8505:
558 event = UX500_MUSB_RIDA;
559 case USB_LINK_HM_IDGND_8505:
560 if (ab->mode == USB_IDLE) {
561 ab->mode = USB_HOST;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100562 ab8500_usb_host_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800563 atomic_notifier_call_chain(&ab->phy.notifier,
564 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100565 }
Heikki Krogerus144713f2012-02-13 13:24:05 +0200566 ab->phy.otg->default_a = true;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800567 if (event != UX500_MUSB_RIDA)
568 event = UX500_MUSB_ID;
569 atomic_notifier_call_chain(&ab->phy.notifier,
570 event, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100571 break;
572
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800573 case USB_LINK_DEDICATED_CHG_8505:
574 ab->mode = USB_DEDICATED_CHG;
575 event = UX500_MUSB_CHARGER;
576 atomic_notifier_call_chain(&ab->phy.notifier,
577 event, &ab->vbus_draw);
578 break;
579
580 default:
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100581 break;
582 }
583
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800584 return 0;
585}
586
587static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
588 enum ab8500_usb_link_status lsts)
589{
590 enum ux500_musb_vbus_id_status event = 0;
591
592 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
593
594 /*
595 * Spurious link_status interrupts are seen in case of a
596 * disconnection of a device in IDGND and RIDA stage
597 */
598 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
599 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
600 lsts == USB_LINK_STD_HOST_NC_8500))
601 return 0;
602
603 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
604 lsts == USB_LINK_STD_HOST_NC_8500)
605 return 0;
606
607 ab->previous_link_status_state = lsts;
608
609 switch (lsts) {
610 case USB_LINK_ACA_RID_B_8500:
611 event = UX500_MUSB_RIDB;
612 case USB_LINK_NOT_CONFIGURED_8500:
613 case USB_LINK_NOT_VALID_LINK_8500:
614 ab->mode = USB_IDLE;
615 ab->phy.otg->default_a = false;
616 ab->vbus_draw = 0;
617 if (event != UX500_MUSB_RIDB)
618 event = UX500_MUSB_NONE;
619 /* Fallback to default B_IDLE as nothing is connected */
620 ab->phy.state = OTG_STATE_B_IDLE;
621 break;
622
623 case USB_LINK_ACA_RID_C_NM_8500:
624 case USB_LINK_ACA_RID_C_HS_8500:
625 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
626 event = UX500_MUSB_RIDC;
627 case USB_LINK_STD_HOST_NC_8500:
628 case USB_LINK_STD_HOST_C_NS_8500:
629 case USB_LINK_STD_HOST_C_S_8500:
630 case USB_LINK_HOST_CHG_NM_8500:
631 case USB_LINK_HOST_CHG_HS_8500:
632 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
633 if (ab->mode == USB_IDLE) {
634 ab->mode = USB_PERIPHERAL;
635 ab8500_usb_peri_phy_en(ab);
636 atomic_notifier_call_chain(&ab->phy.notifier,
637 UX500_MUSB_PREPARE, &ab->vbus_draw);
638 }
639 if (event != UX500_MUSB_RIDC)
640 event = UX500_MUSB_VBUS;
641 break;
642
643 case USB_LINK_ACA_RID_A_8500:
644 event = UX500_MUSB_RIDA;
645 case USB_LINK_HM_IDGND_8500:
646 if (ab->mode == USB_IDLE) {
647 ab->mode = USB_HOST;
648 ab8500_usb_host_phy_en(ab);
649 atomic_notifier_call_chain(&ab->phy.notifier,
650 UX500_MUSB_PREPARE, &ab->vbus_draw);
651 }
652 ab->phy.otg->default_a = true;
653 if (event != UX500_MUSB_RIDA)
654 event = UX500_MUSB_ID;
655 atomic_notifier_call_chain(&ab->phy.notifier,
656 event, &ab->vbus_draw);
657 break;
658
659 case USB_LINK_DEDICATED_CHG_8500:
660 ab->mode = USB_DEDICATED_CHG;
661 event = UX500_MUSB_CHARGER;
662 atomic_notifier_call_chain(&ab->phy.notifier,
663 event, &ab->vbus_draw);
664 break;
665
666 case USB_LINK_RESERVED_8500:
667 break;
668 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100669
670 return 0;
671}
672
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800673/*
674 * Connection Sequence:
675 * 1. Link Status Interrupt
676 * 2. Enable AB clock
677 * 3. Enable AB regulators
678 * 4. Enable USB phy
679 * 5. Reset the musb controller
680 * 6. Switch the ULPI GPIO pins to fucntion mode
681 * 7. Enable the musb Peripheral5 clock
682 * 8. Restore MUSB context
683 */
684static int abx500_usb_link_status_update(struct ab8500_usb *ab)
685{
686 u8 reg;
687 int ret = 0;
688
689 if (is_ab8500(ab->ab8500)) {
690 enum ab8500_usb_link_status lsts;
691
692 abx500_get_register_interruptible(ab->dev,
693 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
694 lsts = (reg >> 3) & 0x0F;
695 ret = ab8500_usb_link_status_update(ab, lsts);
696 } else if (is_ab8505(ab->ab8500)) {
697 enum ab8505_usb_link_status lsts;
698
699 abx500_get_register_interruptible(ab->dev,
700 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
701 lsts = (reg >> 3) & 0x1F;
702 ret = ab8505_usb_link_status_update(ab, lsts);
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200703 } else if (is_ab8540(ab->ab8500)) {
704 enum ab8540_usb_link_status lsts;
705
706 abx500_get_register_interruptible(ab->dev,
707 AB8500_USB, AB8540_USB_LINK_STAT_REG, &reg);
708 lsts = (reg >> 3) & 0xFF;
709 ret = ab8540_usb_link_status_update(ab, lsts);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800710 }
711
712 return ret;
713}
714
715/*
716 * Disconnection Sequence:
717 * 1. Disconect Interrupt
718 * 2. Disable regulators
719 * 3. Disable AB clock
720 * 4. Disable the Phy
721 * 5. Link Status Interrupt
722 * 6. Disable Musb Clock
723 */
724static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
725{
726 struct ab8500_usb *ab = (struct ab8500_usb *) data;
727 enum usb_phy_events event = UX500_MUSB_NONE;
728
729 /* Link status will not be updated till phy is disabled. */
730 if (ab->mode == USB_HOST) {
731 ab->phy.otg->default_a = false;
732 ab->vbus_draw = 0;
733 atomic_notifier_call_chain(&ab->phy.notifier,
734 event, &ab->vbus_draw);
735 ab8500_usb_host_phy_dis(ab);
736 ab->mode = USB_IDLE;
737 }
738
739 if (ab->mode == USB_PERIPHERAL) {
740 atomic_notifier_call_chain(&ab->phy.notifier,
741 event, &ab->vbus_draw);
742 ab8500_usb_peri_phy_dis(ab);
743 atomic_notifier_call_chain(&ab->phy.notifier,
744 UX500_MUSB_CLEAN, &ab->vbus_draw);
745 ab->mode = USB_IDLE;
746 ab->phy.otg->default_a = false;
747 ab->vbus_draw = 0;
748 }
749
750 if (is_ab8500_2p0(ab->ab8500)) {
751 if (ab->mode == USB_DEDICATED_CHG) {
752 ab8500_usb_wd_linkstatus(ab,
753 AB8500_BIT_PHY_CTRL_DEVICE_EN);
754 abx500_mask_and_set_register_interruptible(ab->dev,
755 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
756 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
757 }
758 }
759
760 return IRQ_HANDLED;
761}
762
763static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
764{
Fabio Baltieri3147dad2013-05-15 14:03:25 +0200765 struct ab8500_usb *ab = (struct ab8500_usb *)data;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800766
767 abx500_usb_link_status_update(ab);
768
769 return IRQ_HANDLED;
770}
771
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100772static void ab8500_usb_phy_disable_work(struct work_struct *work)
773{
774 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
775 phy_dis_work);
776
Heikki Krogerus144713f2012-02-13 13:24:05 +0200777 if (!ab->phy.otg->host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100778 ab8500_usb_host_phy_dis(ab);
779
Heikki Krogerus144713f2012-02-13 13:24:05 +0200780 if (!ab->phy.otg->gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100781 ab8500_usb_peri_phy_dis(ab);
782}
783
Fabio Baltieri0c380c02013-05-15 14:03:32 +0200784/* Check if VBUS is set and linkstatus has not detected a cable. */
785static bool ab8500_usb_check_vbus_status(struct ab8500_usb *ab)
786{
787 u8 isource2;
788 u8 reg;
789 enum ab8540_usb_link_status lsts;
790
791 abx500_get_register_interruptible(ab->dev,
792 AB8500_INTERRUPT, AB8500_IT_SOURCE2_REG,
793 &isource2);
794
795 /* If Vbus is below 3.6V abort */
796 if (!(isource2 & AB8500_BIT_SOURCE2_VBUSDET))
797 return false;
798
799 abx500_get_register_interruptible(ab->dev,
800 AB8500_USB, AB8540_USB_LINK_STAT_REG,
801 &reg);
802
803 lsts = (reg >> 3) & 0xFF;
804
805 /* Check if linkstatus has detected a cable */
806 if (lsts)
807 return false;
808
809 return true;
810}
811
812/* re-trigger charger detection again with watchdog re-kick. */
813static void ab8500_usb_vbus_turn_on_event_work(struct work_struct *work)
814{
815 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
816 vbus_event_work);
817
818 if (ab->mode != USB_IDLE)
819 return;
820
821 abx500_set_register_interruptible(ab->dev,
822 AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
823 AB8500_BIT_WD_CTRL_ENABLE);
824
825 udelay(100);
826
827 abx500_set_register_interruptible(ab->dev,
828 AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
829 AB8500_BIT_WD_CTRL_ENABLE | AB8500_BIT_WD_CTRL_KICK);
830
831 udelay(100);
832
833 /* Disable Main watchdog */
834 abx500_set_register_interruptible(ab->dev,
835 AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
836 0x0);
837
838 /* Enable USB Charger detection */
839 abx500_mask_and_set_register_interruptible(ab->dev,
840 AB8500_USB, AB8540_VBUS_CTRL_REG,
841 AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA,
842 AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA);
843
844 ab->enabled_charging_detection = true;
845}
846
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200847static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
848{
849 /*
850 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
851 * VBUS. Set charging current to 100mA in case of standard host
852 */
853 if (is_ab8500_2p0_or_earlier(ab->ab8500))
854 if (mA > 100)
855 mA = 100;
856
857 return mA;
858}
859
Heikki Krogerus144713f2012-02-13 13:24:05 +0200860static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100861{
862 struct ab8500_usb *ab;
863
Heikki Krogerus144713f2012-02-13 13:24:05 +0200864 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100865 return -ENODEV;
866
Heikki Krogerus144713f2012-02-13 13:24:05 +0200867 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100868
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200869 mA = ab8500_eyediagram_workaroud(ab, mA);
870
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100871 ab->vbus_draw = mA;
872
Fabio Baltieri77f43962013-04-03 10:45:10 +0200873 atomic_notifier_call_chain(&ab->phy.notifier,
874 UX500_MUSB_VBUS, &ab->vbus_draw);
875
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100876 return 0;
877}
878
Heikki Krogerus86753812012-02-13 13:24:02 +0200879static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100880{
881 /* TODO */
882 return 0;
883}
884
Heikki Krogerus144713f2012-02-13 13:24:05 +0200885static int ab8500_usb_set_peripheral(struct usb_otg *otg,
886 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100887{
888 struct ab8500_usb *ab;
889
890 if (!otg)
891 return -ENODEV;
892
Heikki Krogerus144713f2012-02-13 13:24:05 +0200893 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100894
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200895 ab->phy.otg->gadget = gadget;
896
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100897 /* Some drivers call this function in atomic context.
898 * Do not update ab8500 registers directly till this
899 * is fixed.
900 */
901
Fabio Baltieri3147dad2013-05-15 14:03:25 +0200902 if ((ab->mode != USB_IDLE) && !gadget) {
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200903 ab->mode = USB_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100904 schedule_work(&ab->phy_dis_work);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100905 }
906
907 return 0;
908}
909
Heikki Krogerus144713f2012-02-13 13:24:05 +0200910static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100911{
912 struct ab8500_usb *ab;
913
914 if (!otg)
915 return -ENODEV;
916
Heikki Krogerus144713f2012-02-13 13:24:05 +0200917 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100918
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200919 ab->phy.otg->host = host;
920
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100921 /* Some drivers call this function in atomic context.
922 * Do not update ab8500 registers directly till this
923 * is fixed.
924 */
925
Fabio Baltieri3147dad2013-05-15 14:03:25 +0200926 if ((ab->mode != USB_IDLE) && !host) {
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200927 ab->mode = USB_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100928 schedule_work(&ab->phy_dis_work);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100929 }
930
931 return 0;
932}
933
Sakethram Bommisettifb21f372013-05-15 14:03:27 +0200934static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
935{
936 abx500_mask_and_set_register_interruptible(ab->dev,
937 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
938 AB8500_BIT_PHY_CTRL_DEVICE_EN,
939 AB8500_BIT_PHY_CTRL_DEVICE_EN);
940
941 udelay(100);
942
943 abx500_mask_and_set_register_interruptible(ab->dev,
944 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
945 AB8500_BIT_PHY_CTRL_DEVICE_EN,
946 0);
947
948 abx500_mask_and_set_register_interruptible(ab->dev,
949 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
950 AB8500_BIT_PHY_CTRL_HOST_EN,
951 AB8500_BIT_PHY_CTRL_HOST_EN);
952
953 udelay(100);
954
955 abx500_mask_and_set_register_interruptible(ab->dev,
956 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
957 AB8500_BIT_PHY_CTRL_HOST_EN,
958 0);
959}
960
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200961static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
962{
963 int err;
964
965 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
966 if (IS_ERR(ab->v_ape)) {
967 dev_err(ab->dev, "Could not get v-ape supply\n");
968 err = PTR_ERR(ab->v_ape);
969 return err;
970 }
971
972 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
973 if (IS_ERR(ab->v_ulpi)) {
974 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
975 err = PTR_ERR(ab->v_ulpi);
976 return err;
977 }
978
979 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
980 if (IS_ERR(ab->v_musb)) {
981 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
982 err = PTR_ERR(ab->v_musb);
983 return err;
984 }
985
986 return 0;
987}
988
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800989static int ab8500_usb_irq_setup(struct platform_device *pdev,
990 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100991{
992 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800993 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100994
Fabio Baltieribd4c9f02013-05-15 14:03:31 +0200995 if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
996 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
997 if (irq < 0) {
998 dev_err(&pdev->dev, "Link status irq not found\n");
999 return irq;
1000 }
1001 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1002 ab8500_usb_link_status_irq,
1003 IRQF_NO_SUSPEND | IRQF_SHARED,
1004 "usb-link-status", ab);
1005 if (err < 0) {
1006 dev_err(ab->dev, "request_irq failed for link status irq\n");
1007 return err;
1008 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001009 }
1010
Fabio Baltieribd4c9f02013-05-15 14:03:31 +02001011 if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
1012 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
1013 if (irq < 0) {
1014 dev_err(&pdev->dev, "ID fall irq not found\n");
1015 return irq;
1016 }
1017 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1018 ab8500_usb_disconnect_irq,
1019 IRQF_NO_SUSPEND | IRQF_SHARED,
1020 "usb-id-fall", ab);
1021 if (err < 0) {
1022 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
1023 return err;
1024 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +08001025 }
1026
Fabio Baltieribd4c9f02013-05-15 14:03:31 +02001027 if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
1028 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
1029 if (irq < 0) {
1030 dev_err(&pdev->dev, "VBUS fall irq not found\n");
1031 return irq;
1032 }
1033 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1034 ab8500_usb_disconnect_irq,
1035 IRQF_NO_SUSPEND | IRQF_SHARED,
1036 "usb-vbus-fall", ab);
1037 if (err < 0) {
1038 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
1039 return err;
1040 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001041 }
1042
1043 return 0;
1044}
1045
Fabio Baltieri16604a32013-05-15 14:03:30 +02001046static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
1047{
1048 int err;
1049
1050 /* Enable the PBT/Bank 0x12 access */
1051 err = abx500_set_register_interruptible(ab->dev,
1052 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
1053 if (err < 0)
1054 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1055 err);
1056
1057 err = abx500_set_register_interruptible(ab->dev,
1058 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
1059 if (err < 0)
1060 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1061 err);
1062
1063 err = abx500_set_register_interruptible(ab->dev,
1064 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
1065 if (err < 0)
1066 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1067 err);
1068
1069 err = abx500_set_register_interruptible(ab->dev,
1070 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
1071 if (err < 0)
1072 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
1073 err);
1074
1075 /* Switch to normal mode/disable Bank 0x12 access */
1076 err = abx500_set_register_interruptible(ab->dev,
1077 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
1078 if (err < 0)
1079 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1080 err);
1081}
1082
1083static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
1084{
1085 int err;
1086
1087 /* Enable the PBT/Bank 0x12 access */
1088 err = abx500_mask_and_set_register_interruptible(ab->dev,
1089 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
1090 0x01, 0x01);
1091 if (err < 0)
1092 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1093 err);
1094
1095 err = abx500_mask_and_set_register_interruptible(ab->dev,
1096 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
1097 0xC8, 0xC8);
1098 if (err < 0)
1099 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1100 err);
1101
1102 err = abx500_mask_and_set_register_interruptible(ab->dev,
1103 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
1104 0x60, 0x60);
1105 if (err < 0)
1106 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1107 err);
1108
1109 err = abx500_mask_and_set_register_interruptible(ab->dev,
1110 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
1111 0xFC, 0x80);
1112
1113 if (err < 0)
1114 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
1115 err);
1116
1117 /* Switch to normal mode/disable Bank 0x12 access */
1118 err = abx500_mask_and_set_register_interruptible(ab->dev,
1119 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
1120 0x00, 0x00);
1121 if (err < 0)
1122 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1123 err);
1124}
1125
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001126static void ab8500_usb_set_ab8540_tuning_values(struct ab8500_usb *ab)
1127{
1128 int err;
1129
1130 err = abx500_set_register_interruptible(ab->dev,
1131 AB8540_DEBUG, AB8500_USB_PHY_TUNE1, 0xCC);
1132 if (err < 0)
1133 dev_err(ab->dev, "Failed to set PHY_TUNE1 register ret=%d\n",
1134 err);
1135
1136 err = abx500_set_register_interruptible(ab->dev,
1137 AB8540_DEBUG, AB8500_USB_PHY_TUNE2, 0x60);
1138 if (err < 0)
1139 dev_err(ab->dev, "Failed to set PHY_TUNE2 register ret=%d\n",
1140 err);
1141
1142 err = abx500_set_register_interruptible(ab->dev,
1143 AB8540_DEBUG, AB8500_USB_PHY_TUNE3, 0x90);
1144 if (err < 0)
1145 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester ret=%d\n",
1146 err);
1147}
1148
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001149static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001150{
1151 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +08001152 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +02001153 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001154 int err;
1155 int rev;
1156
Fabio Baltieri73f226c2013-03-08 10:27:08 +08001157 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001158 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +08001159
1160 if (is_ab8500_1p1_or_earlier(ab8500)) {
1161 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001162 return -ENODEV;
1163 }
1164
Fabio Baltieri81ef6722013-04-03 10:45:03 +02001165 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001166 if (!ab)
1167 return -ENOMEM;
1168
Fabio Baltieri81ef6722013-04-03 10:45:03 +02001169 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
1170 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +02001171 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +02001172
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001173 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +08001174 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +02001175 ab->phy.dev = ab->dev;
1176 ab->phy.otg = otg;
1177 ab->phy.label = "ab8500";
1178 ab->phy.set_suspend = ab8500_usb_set_suspend;
1179 ab->phy.set_power = ab8500_usb_set_power;
1180 ab->phy.state = OTG_STATE_UNDEFINED;
1181
1182 otg->phy = &ab->phy;
1183 otg->set_host = ab8500_usb_set_host;
1184 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001185
Fabio Baltieribd4c9f02013-05-15 14:03:31 +02001186 if (is_ab8500(ab->ab8500)) {
1187 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1188 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1189 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
1190 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1191 } else if (is_ab8505(ab->ab8500)) {
1192 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1193 AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1194 AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
1195 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001196 } else if (is_ab8540(ab->ab8500)) {
1197 ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1198 AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS |
1199 AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK |
1200 AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
Fabio Baltieribd4c9f02013-05-15 14:03:31 +02001201 }
1202
1203 /* Disable regulator voltage setting for AB8500 <= v2.0 */
1204 if (is_ab8500_2p0_or_earlier(ab->ab8500))
1205 ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1206
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001207 platform_set_drvdata(pdev, ab);
1208
Heikki Krogerus144713f2012-02-13 13:24:05 +02001209 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001210
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001211 /* all: Disable phy when called from set_host and set_peripheral */
1212 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
1213
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001214 INIT_WORK(&ab->vbus_event_work, ab8500_usb_vbus_turn_on_event_work);
1215
Fabio Baltierie65b36c2013-04-03 10:45:06 +02001216 err = ab8500_usb_regulator_get(ab);
1217 if (err)
1218 return err;
1219
Mian Yousaf Kaukabd0ed0642013-05-15 14:03:26 +02001220 ab->sysclk = devm_clk_get(ab->dev, "sysclk");
1221 if (IS_ERR(ab->sysclk)) {
1222 dev_err(ab->dev, "Could not get sysclk.\n");
1223 return PTR_ERR(ab->sysclk);
1224 }
1225
Fabio Baltieriaf6882b2013-03-08 10:27:09 +08001226 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001227 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +02001228 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001229
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301230 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001231 if (err) {
1232 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +02001233 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001234 }
1235
Fabio Baltieri16604a32013-05-15 14:03:30 +02001236 if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
1237 /* Phy tuning values for AB8500 > v2.0 */
1238 ab8500_usb_set_ab8500_tuning_values(ab);
1239 else if (is_ab8505(ab->ab8500))
1240 /* Phy tuning values for AB8505 */
1241 ab8500_usb_set_ab8505_tuning_values(ab);
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001242 else if (is_ab8540(ab->ab8500))
1243 /* Phy tuning values for AB8540 */
1244 ab8500_usb_set_ab8540_tuning_values(ab);
Sakethram Bommisetti71246312013-04-03 10:45:04 +02001245
Fabio Baltieriaf6882b2013-03-08 10:27:09 +08001246 /* Needed to enable ID detection. */
1247 ab8500_usb_wd_workaround(ab);
1248
Sakethram Bommisettifb21f372013-05-15 14:03:27 +02001249 /*
1250 * This is required for usb-link-status to work properly when a
1251 * cable is connected at boot time.
1252 */
1253 ab8500_usb_restart_phy(ab);
1254
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001255 if (ab->flags & AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS) {
1256 if (ab8500_usb_check_vbus_status(ab))
1257 schedule_work(&ab->vbus_event_work);
1258 }
1259
Sakethram Bommisetti8db12232013-04-03 10:45:12 +02001260 abx500_usb_link_status_update(ab);
1261
Fabio Baltieri73f226c2013-03-08 10:27:08 +08001262 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001263
1264 return 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001265}
1266
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05001267static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001268{
1269 struct ab8500_usb *ab = platform_get_drvdata(pdev);
1270
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001271 cancel_work_sync(&ab->phy_dis_work);
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001272 cancel_work_sync(&ab->vbus_event_work);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001273
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301274 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001275
Mian Yousaf Kaukabf5ef7b42013-04-03 10:45:09 +02001276 if (ab->mode == USB_HOST)
1277 ab8500_usb_host_phy_dis(ab);
1278 else if (ab->mode == USB_PERIPHERAL)
1279 ab8500_usb_peri_phy_dis(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001280
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001281 return 0;
1282}
1283
Fabio Baltierib3affc32013-05-15 14:03:29 +02001284static struct platform_device_id ab8500_usb_devtype[] = {
1285 { .name = "ab8500-usb", },
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001286 { .name = "ab8540-usb", },
Fabio Baltierib3affc32013-05-15 14:03:29 +02001287 { /* sentinel */ }
1288};
1289MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
1290
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001291static struct platform_driver ab8500_usb_driver = {
1292 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05001293 .remove = ab8500_usb_remove,
Fabio Baltierib3affc32013-05-15 14:03:29 +02001294 .id_table = ab8500_usb_devtype,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001295 .driver = {
Fabio Baltierib3affc32013-05-15 14:03:29 +02001296 .name = "abx5x0-usb",
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001297 .owner = THIS_MODULE,
1298 },
1299};
1300
1301static int __init ab8500_usb_init(void)
1302{
1303 return platform_driver_register(&ab8500_usb_driver);
1304}
1305subsys_initcall(ab8500_usb_init);
1306
1307static void __exit ab8500_usb_exit(void)
1308{
1309 platform_driver_unregister(&ab8500_usb_driver);
1310}
1311module_exit(ab8500_usb_exit);
1312
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001313MODULE_AUTHOR("ST-Ericsson AB");
Fabio Baltieri0c380c02013-05-15 14:03:32 +02001314MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001315MODULE_LICENSE("GPL");