blob: 749614cf6e2ba9a102c5014dca8592f5a3ce734c [file] [log] [blame]
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +01001/*
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>
Linus Walleijee66e652011-12-02 14:16:33 +010033#include <linux/mfd/abx500/ab8500.h>
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080034#include <linux/usb/musb-ux500.h>
Fabio Baltierie65b36c2013-04-03 10:45:06 +020035#include <linux/regulator/consumer.h>
Patrice Chotard899f0f52013-04-03 10:45:11 +020036#include <linux/pinctrl/consumer.h>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010037
Sakethram Bommisetti71246312013-04-03 10:45:04 +020038/* Bank AB8500_SYS_CTRL2_BLOCK */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010039#define AB8500_MAIN_WD_CTRL_REG 0x01
Sakethram Bommisetti71246312013-04-03 10:45:04 +020040
41/* Bank AB8500_USB */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010042#define AB8500_USB_LINE_STAT_REG 0x80
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080043#define AB8505_USB_LINE_STAT_REG 0x94
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010044#define AB8500_USB_PHY_CTRL_REG 0x8A
45
Sakethram Bommisetti71246312013-04-03 10:45:04 +020046/* 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
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010054#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
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010060#define AB8500_WD_KICK_DELAY_US 100 /* usec */
61#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080062#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010063
64/* Usb line status register */
65enum ab8500_usb_link_status {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080066 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
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100120};
121
122struct ab8500_usb {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200123 struct usb_phy phy;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100124 struct device *dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800125 struct ab8500 *ab8500;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100126 unsigned vbus_draw;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100127 struct work_struct phy_dis_work;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800128 enum ab8500_usb_mode mode;
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200129 struct regulator *v_ape;
130 struct regulator *v_musb;
131 struct regulator *v_ulpi;
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200132 int saved_v_ulpi;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800133 int previous_link_status_state;
Patrice Chotard899f0f52013-04-03 10:45:11 +0200134 struct pinctrl *pinctrl;
135 struct pinctrl_state *pins_sleep;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100136};
137
Heikki Krogerus144713f2012-02-13 13:24:05 +0200138static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100139{
Heikki Krogerus144713f2012-02-13 13:24:05 +0200140 return container_of(x, struct ab8500_usb, phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100141}
142
143static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
144{
145 abx500_set_register_interruptible(ab->dev,
146 AB8500_SYS_CTRL2_BLOCK,
147 AB8500_MAIN_WD_CTRL_REG,
148 AB8500_BIT_WD_CTRL_ENABLE);
149
150 udelay(AB8500_WD_KICK_DELAY_US);
151
152 abx500_set_register_interruptible(ab->dev,
153 AB8500_SYS_CTRL2_BLOCK,
154 AB8500_MAIN_WD_CTRL_REG,
155 (AB8500_BIT_WD_CTRL_ENABLE
156 | AB8500_BIT_WD_CTRL_KICK));
157
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800158 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100159
160 abx500_set_register_interruptible(ab->dev,
161 AB8500_SYS_CTRL2_BLOCK,
162 AB8500_MAIN_WD_CTRL_REG,
163 0);
164}
165
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200166static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
167{
168 int ret, volt;
169
170 regulator_enable(ab->v_ape);
171
172 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
173 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
174 if (ab->saved_v_ulpi < 0)
175 dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
176
177 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
178 if (ret < 0)
179 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
180 ret);
181
182 ret = regulator_set_optimum_mode(ab->v_ulpi, 28000);
183 if (ret < 0)
184 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
185 ret);
186 }
187
188 regulator_enable(ab->v_ulpi);
189
190 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
191 volt = regulator_get_voltage(ab->v_ulpi);
192 if ((volt != 1300000) && (volt != 1350000))
193 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
194 volt);
195 }
196
197 regulator_enable(ab->v_musb);
198}
199
200static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
201{
202 int ret;
203
204 regulator_disable(ab->v_musb);
205
206 regulator_disable(ab->v_ulpi);
207
208 /* USB is not the only consumer of Vintcore, restore old settings */
209 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
210 if (ab->saved_v_ulpi > 0) {
211 ret = regulator_set_voltage(ab->v_ulpi,
212 ab->saved_v_ulpi, ab->saved_v_ulpi);
213 if (ret < 0)
214 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
215 ab->saved_v_ulpi, ret);
216 }
217
218 ret = regulator_set_optimum_mode(ab->v_ulpi, 0);
219 if (ret < 0)
220 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
221 ret);
222 }
223
224 regulator_disable(ab->v_ape);
225}
226
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800227static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
228{
229 /* Workaround for v2.0 bug # 31952 */
230 if (is_ab8500_2p0(ab->ab8500)) {
231 abx500_mask_and_set_register_interruptible(ab->dev,
232 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
233 bit, bit);
234 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
235 }
236}
237
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200238static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100239{
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200240 u8 bit;
241 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
242 AB8500_BIT_PHY_CTRL_DEVICE_EN;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100243
Patrice Chotard899f0f52013-04-03 10:45:11 +0200244 /* mux and configure USB pins to DEFAULT state */
245 ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
246 if (IS_ERR(ab->pinctrl))
247 dev_err(ab->dev, "could not get/set default pinstate\n");
248
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200249 ab8500_usb_regulator_enable(ab);
250
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200251 abx500_mask_and_set_register_interruptible(ab->dev,
252 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
253 bit, bit);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100254}
255
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200256static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
257{
258 u8 bit;
259 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
260 AB8500_BIT_PHY_CTRL_DEVICE_EN;
261
262 ab8500_usb_wd_linkstatus(ab, bit);
263
264 abx500_mask_and_set_register_interruptible(ab->dev,
265 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
266 bit, 0);
267
268 /* Needed to disable the phy.*/
269 ab8500_usb_wd_workaround(ab);
Fabio Baltieri54dfbb02013-04-03 10:45:08 +0200270
271 ab8500_usb_regulator_disable(ab);
Patrice Chotard899f0f52013-04-03 10:45:11 +0200272
273 if (!IS_ERR(ab->pinctrl)) {
274 /* configure USB pins to SLEEP state */
275 ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
276 PINCTRL_STATE_SLEEP);
277
278 if (IS_ERR(ab->pins_sleep))
279 dev_dbg(ab->dev, "could not get sleep pinstate\n");
280 else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
281 dev_err(ab->dev, "could not set pins to sleep state\n");
282
283 /* as USB pins are shared with idddet, release them to allow
284 * iddet to request them
285 */
286 pinctrl_put(ab->pinctrl);
287 }
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200288}
289
290#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
291#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
292#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
293#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100294
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800295static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
296 enum ab8505_usb_link_status lsts)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100297{
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800298 enum ux500_musb_vbus_id_status event = 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100299
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800300 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100301
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800302 /*
303 * Spurious link_status interrupts are seen at the time of
304 * disconnection of a device in RIDA state
305 */
306 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
307 (lsts == USB_LINK_STD_HOST_NC_8505))
308 return 0;
309
310 ab->previous_link_status_state = lsts;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100311
312 switch (lsts) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800313 case USB_LINK_ACA_RID_B_8505:
314 event = UX500_MUSB_RIDB;
315 case USB_LINK_NOT_CONFIGURED_8505:
316 case USB_LINK_RESERVED0_8505:
317 case USB_LINK_RESERVED1_8505:
318 case USB_LINK_RESERVED2_8505:
319 case USB_LINK_RESERVED3_8505:
320 ab->mode = USB_IDLE;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200321 ab->phy.otg->default_a = false;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100322 ab->vbus_draw = 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800323 if (event != UX500_MUSB_RIDB)
324 event = UX500_MUSB_NONE;
325 /*
326 * Fallback to default B_IDLE as nothing
327 * is connected
328 */
329 ab->phy.state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100330 break;
331
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800332 case USB_LINK_ACA_RID_C_NM_8505:
333 event = UX500_MUSB_RIDC;
334 case USB_LINK_STD_HOST_NC_8505:
335 case USB_LINK_STD_HOST_C_NS_8505:
336 case USB_LINK_STD_HOST_C_S_8505:
337 case USB_LINK_CDP_8505:
338 if (ab->mode == USB_IDLE) {
339 ab->mode = USB_PERIPHERAL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100340 ab8500_usb_peri_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800341 atomic_notifier_call_chain(&ab->phy.notifier,
342 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100343 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800344 if (event != UX500_MUSB_RIDC)
345 event = UX500_MUSB_VBUS;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100346 break;
347
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800348 case USB_LINK_ACA_RID_A_8505:
349 case USB_LINK_ACA_DOCK_CHGR_8505:
350 event = UX500_MUSB_RIDA;
351 case USB_LINK_HM_IDGND_8505:
352 if (ab->mode == USB_IDLE) {
353 ab->mode = USB_HOST;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100354 ab8500_usb_host_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800355 atomic_notifier_call_chain(&ab->phy.notifier,
356 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100357 }
Heikki Krogerus144713f2012-02-13 13:24:05 +0200358 ab->phy.otg->default_a = true;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800359 if (event != UX500_MUSB_RIDA)
360 event = UX500_MUSB_ID;
361 atomic_notifier_call_chain(&ab->phy.notifier,
362 event, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100363 break;
364
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800365 case USB_LINK_DEDICATED_CHG_8505:
366 ab->mode = USB_DEDICATED_CHG;
367 event = UX500_MUSB_CHARGER;
368 atomic_notifier_call_chain(&ab->phy.notifier,
369 event, &ab->vbus_draw);
370 break;
371
372 default:
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100373 break;
374 }
375
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800376 return 0;
377}
378
379static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
380 enum ab8500_usb_link_status lsts)
381{
382 enum ux500_musb_vbus_id_status event = 0;
383
384 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
385
386 /*
387 * Spurious link_status interrupts are seen in case of a
388 * disconnection of a device in IDGND and RIDA stage
389 */
390 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
391 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
392 lsts == USB_LINK_STD_HOST_NC_8500))
393 return 0;
394
395 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
396 lsts == USB_LINK_STD_HOST_NC_8500)
397 return 0;
398
399 ab->previous_link_status_state = lsts;
400
401 switch (lsts) {
402 case USB_LINK_ACA_RID_B_8500:
403 event = UX500_MUSB_RIDB;
404 case USB_LINK_NOT_CONFIGURED_8500:
405 case USB_LINK_NOT_VALID_LINK_8500:
406 ab->mode = USB_IDLE;
407 ab->phy.otg->default_a = false;
408 ab->vbus_draw = 0;
409 if (event != UX500_MUSB_RIDB)
410 event = UX500_MUSB_NONE;
411 /* Fallback to default B_IDLE as nothing is connected */
412 ab->phy.state = OTG_STATE_B_IDLE;
413 break;
414
415 case USB_LINK_ACA_RID_C_NM_8500:
416 case USB_LINK_ACA_RID_C_HS_8500:
417 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
418 event = UX500_MUSB_RIDC;
419 case USB_LINK_STD_HOST_NC_8500:
420 case USB_LINK_STD_HOST_C_NS_8500:
421 case USB_LINK_STD_HOST_C_S_8500:
422 case USB_LINK_HOST_CHG_NM_8500:
423 case USB_LINK_HOST_CHG_HS_8500:
424 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
425 if (ab->mode == USB_IDLE) {
426 ab->mode = USB_PERIPHERAL;
427 ab8500_usb_peri_phy_en(ab);
428 atomic_notifier_call_chain(&ab->phy.notifier,
429 UX500_MUSB_PREPARE, &ab->vbus_draw);
430 }
431 if (event != UX500_MUSB_RIDC)
432 event = UX500_MUSB_VBUS;
433 break;
434
435 case USB_LINK_ACA_RID_A_8500:
436 event = UX500_MUSB_RIDA;
437 case USB_LINK_HM_IDGND_8500:
438 if (ab->mode == USB_IDLE) {
439 ab->mode = USB_HOST;
440 ab8500_usb_host_phy_en(ab);
441 atomic_notifier_call_chain(&ab->phy.notifier,
442 UX500_MUSB_PREPARE, &ab->vbus_draw);
443 }
444 ab->phy.otg->default_a = true;
445 if (event != UX500_MUSB_RIDA)
446 event = UX500_MUSB_ID;
447 atomic_notifier_call_chain(&ab->phy.notifier,
448 event, &ab->vbus_draw);
449 break;
450
451 case USB_LINK_DEDICATED_CHG_8500:
452 ab->mode = USB_DEDICATED_CHG;
453 event = UX500_MUSB_CHARGER;
454 atomic_notifier_call_chain(&ab->phy.notifier,
455 event, &ab->vbus_draw);
456 break;
457
458 case USB_LINK_RESERVED_8500:
459 break;
460 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100461
462 return 0;
463}
464
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800465/*
466 * Connection Sequence:
467 * 1. Link Status Interrupt
468 * 2. Enable AB clock
469 * 3. Enable AB regulators
470 * 4. Enable USB phy
471 * 5. Reset the musb controller
472 * 6. Switch the ULPI GPIO pins to fucntion mode
473 * 7. Enable the musb Peripheral5 clock
474 * 8. Restore MUSB context
475 */
476static int abx500_usb_link_status_update(struct ab8500_usb *ab)
477{
478 u8 reg;
479 int ret = 0;
480
481 if (is_ab8500(ab->ab8500)) {
482 enum ab8500_usb_link_status lsts;
483
484 abx500_get_register_interruptible(ab->dev,
485 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
486 lsts = (reg >> 3) & 0x0F;
487 ret = ab8500_usb_link_status_update(ab, lsts);
488 } else if (is_ab8505(ab->ab8500)) {
489 enum ab8505_usb_link_status lsts;
490
491 abx500_get_register_interruptible(ab->dev,
492 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
493 lsts = (reg >> 3) & 0x1F;
494 ret = ab8505_usb_link_status_update(ab, lsts);
495 }
496
497 return ret;
498}
499
500/*
501 * Disconnection Sequence:
502 * 1. Disconect Interrupt
503 * 2. Disable regulators
504 * 3. Disable AB clock
505 * 4. Disable the Phy
506 * 5. Link Status Interrupt
507 * 6. Disable Musb Clock
508 */
509static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
510{
511 struct ab8500_usb *ab = (struct ab8500_usb *) data;
512 enum usb_phy_events event = UX500_MUSB_NONE;
513
514 /* Link status will not be updated till phy is disabled. */
515 if (ab->mode == USB_HOST) {
516 ab->phy.otg->default_a = false;
517 ab->vbus_draw = 0;
518 atomic_notifier_call_chain(&ab->phy.notifier,
519 event, &ab->vbus_draw);
520 ab8500_usb_host_phy_dis(ab);
521 ab->mode = USB_IDLE;
522 }
523
524 if (ab->mode == USB_PERIPHERAL) {
525 atomic_notifier_call_chain(&ab->phy.notifier,
526 event, &ab->vbus_draw);
527 ab8500_usb_peri_phy_dis(ab);
528 atomic_notifier_call_chain(&ab->phy.notifier,
529 UX500_MUSB_CLEAN, &ab->vbus_draw);
530 ab->mode = USB_IDLE;
531 ab->phy.otg->default_a = false;
532 ab->vbus_draw = 0;
533 }
534
535 if (is_ab8500_2p0(ab->ab8500)) {
536 if (ab->mode == USB_DEDICATED_CHG) {
537 ab8500_usb_wd_linkstatus(ab,
538 AB8500_BIT_PHY_CTRL_DEVICE_EN);
539 abx500_mask_and_set_register_interruptible(ab->dev,
540 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
541 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
542 }
543 }
544
545 return IRQ_HANDLED;
546}
547
548static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
549{
550 struct ab8500_usb *ab = (struct ab8500_usb *) data;
551
552 abx500_usb_link_status_update(ab);
553
554 return IRQ_HANDLED;
555}
556
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100557static void ab8500_usb_phy_disable_work(struct work_struct *work)
558{
559 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
560 phy_dis_work);
561
Heikki Krogerus144713f2012-02-13 13:24:05 +0200562 if (!ab->phy.otg->host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100563 ab8500_usb_host_phy_dis(ab);
564
Heikki Krogerus144713f2012-02-13 13:24:05 +0200565 if (!ab->phy.otg->gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100566 ab8500_usb_peri_phy_dis(ab);
567}
568
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200569static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
570{
571 /*
572 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
573 * VBUS. Set charging current to 100mA in case of standard host
574 */
575 if (is_ab8500_2p0_or_earlier(ab->ab8500))
576 if (mA > 100)
577 mA = 100;
578
579 return mA;
580}
581
Heikki Krogerus144713f2012-02-13 13:24:05 +0200582static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100583{
584 struct ab8500_usb *ab;
585
Heikki Krogerus144713f2012-02-13 13:24:05 +0200586 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100587 return -ENODEV;
588
Heikki Krogerus144713f2012-02-13 13:24:05 +0200589 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100590
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200591 mA = ab8500_eyediagram_workaroud(ab, mA);
592
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100593 ab->vbus_draw = mA;
594
Fabio Baltieri77f43962013-04-03 10:45:10 +0200595 atomic_notifier_call_chain(&ab->phy.notifier,
596 UX500_MUSB_VBUS, &ab->vbus_draw);
597
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100598 return 0;
599}
600
Heikki Krogerus86753812012-02-13 13:24:02 +0200601static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100602{
603 /* TODO */
604 return 0;
605}
606
Heikki Krogerus144713f2012-02-13 13:24:05 +0200607static int ab8500_usb_set_peripheral(struct usb_otg *otg,
608 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100609{
610 struct ab8500_usb *ab;
611
612 if (!otg)
613 return -ENODEV;
614
Heikki Krogerus144713f2012-02-13 13:24:05 +0200615 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100616
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200617 ab->phy.otg->gadget = gadget;
618
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100619 /* Some drivers call this function in atomic context.
620 * Do not update ab8500 registers directly till this
621 * is fixed.
622 */
623
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200624 if ((ab->mode != USB_IDLE) && (!gadget)) {
625 ab->mode = USB_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100626 schedule_work(&ab->phy_dis_work);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100627 }
628
629 return 0;
630}
631
Heikki Krogerus144713f2012-02-13 13:24:05 +0200632static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100633{
634 struct ab8500_usb *ab;
635
636 if (!otg)
637 return -ENODEV;
638
Heikki Krogerus144713f2012-02-13 13:24:05 +0200639 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100640
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200641 ab->phy.otg->host = host;
642
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100643 /* Some drivers call this function in atomic context.
644 * Do not update ab8500 registers directly till this
645 * is fixed.
646 */
647
Sakethram Bommisetti58823372013-04-03 10:45:13 +0200648 if ((ab->mode != USB_IDLE) && (!host)) {
649 ab->mode = USB_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100650 schedule_work(&ab->phy_dis_work);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100651 }
652
653 return 0;
654}
655
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200656static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
657{
658 int err;
659
660 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
661 if (IS_ERR(ab->v_ape)) {
662 dev_err(ab->dev, "Could not get v-ape supply\n");
663 err = PTR_ERR(ab->v_ape);
664 return err;
665 }
666
667 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
668 if (IS_ERR(ab->v_ulpi)) {
669 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
670 err = PTR_ERR(ab->v_ulpi);
671 return err;
672 }
673
674 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
675 if (IS_ERR(ab->v_musb)) {
676 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
677 err = PTR_ERR(ab->v_musb);
678 return err;
679 }
680
681 return 0;
682}
683
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800684static int ab8500_usb_irq_setup(struct platform_device *pdev,
685 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100686{
687 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800688 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100689
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800690 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
691 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100692 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800693 return irq;
694 }
695 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
696 ab8500_usb_link_status_irq,
697 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
698 if (err < 0) {
699 dev_err(ab->dev, "request_irq failed for link status irq\n");
700 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100701 }
702
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800703 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
704 if (irq < 0) {
705 dev_err(&pdev->dev, "ID fall irq not found\n");
706 return irq;
707 }
708 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
709 ab8500_usb_disconnect_irq,
710 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100711 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800712 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
713 return err;
714 }
715
716 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
717 if (irq < 0) {
718 dev_err(&pdev->dev, "VBUS fall irq not found\n");
719 return irq;
720 }
721 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
722 ab8500_usb_disconnect_irq,
723 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
724 if (err < 0) {
725 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100726 return err;
727 }
728
729 return 0;
730}
731
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500732static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100733{
734 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800735 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200736 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100737 int err;
738 int rev;
739
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800740 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100741 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800742
743 if (is_ab8500_1p1_or_earlier(ab8500)) {
744 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100745 return -ENODEV;
746 }
747
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200748 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100749 if (!ab)
750 return -ENOMEM;
751
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200752 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
753 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200754 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200755
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100756 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800757 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200758 ab->phy.dev = ab->dev;
759 ab->phy.otg = otg;
760 ab->phy.label = "ab8500";
761 ab->phy.set_suspend = ab8500_usb_set_suspend;
762 ab->phy.set_power = ab8500_usb_set_power;
763 ab->phy.state = OTG_STATE_UNDEFINED;
764
765 otg->phy = &ab->phy;
766 otg->set_host = ab8500_usb_set_host;
767 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100768
769 platform_set_drvdata(pdev, ab);
770
Heikki Krogerus144713f2012-02-13 13:24:05 +0200771 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100772
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100773 /* all: Disable phy when called from set_host and set_peripheral */
774 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
775
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200776 err = ab8500_usb_regulator_get(ab);
777 if (err)
778 return err;
779
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800780 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100781 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200782 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100783
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530784 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100785 if (err) {
786 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200787 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100788 }
789
Sakethram Bommisetti71246312013-04-03 10:45:04 +0200790 /* Phy tuning values for AB8500 */
791 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
792 /* Enable the PBT/Bank 0x12 access */
793 err = abx500_set_register_interruptible(ab->dev,
794 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
795 if (err < 0)
796 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
797 err);
798
799 err = abx500_set_register_interruptible(ab->dev,
800 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
801 if (err < 0)
802 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
803 err);
804
805 err = abx500_set_register_interruptible(ab->dev,
806 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
807 if (err < 0)
808 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
809 err);
810
811 err = abx500_set_register_interruptible(ab->dev,
812 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
813 if (err < 0)
814 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
815 err);
816
817 /* Switch to normal mode/disable Bank 0x12 access */
818 err = abx500_set_register_interruptible(ab->dev,
819 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
820 if (err < 0)
821 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
822 err);
823 }
824
825 /* Phy tuning values for AB8505 */
826 if (is_ab8505(ab->ab8500)) {
827 /* Enable the PBT/Bank 0x12 access */
828 err = abx500_mask_and_set_register_interruptible(ab->dev,
829 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
830 0x01, 0x01);
831 if (err < 0)
832 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
833 err);
834
835 err = abx500_mask_and_set_register_interruptible(ab->dev,
836 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
837 0xC8, 0xC8);
838 if (err < 0)
839 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
840 err);
841
842 err = abx500_mask_and_set_register_interruptible(ab->dev,
843 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
844 0x60, 0x60);
845 if (err < 0)
846 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
847 err);
848
849 err = abx500_mask_and_set_register_interruptible(ab->dev,
850 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
851 0xFC, 0x80);
852
853 if (err < 0)
854 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
855 err);
856
857 /* Switch to normal mode/disable Bank 0x12 access */
858 err = abx500_mask_and_set_register_interruptible(ab->dev,
859 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
860 0x00, 0x00);
861 if (err < 0)
862 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
863 err);
864 }
865
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800866 /* Needed to enable ID detection. */
867 ab8500_usb_wd_workaround(ab);
868
Sakethram Bommisetti8db12232013-04-03 10:45:12 +0200869 abx500_usb_link_status_update(ab);
870
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800871 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100872
873 return 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100874}
875
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500876static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100877{
878 struct ab8500_usb *ab = platform_get_drvdata(pdev);
879
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100880 cancel_work_sync(&ab->phy_dis_work);
881
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530882 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100883
Mian Yousaf Kaukabf5ef7b42013-04-03 10:45:09 +0200884 if (ab->mode == USB_HOST)
885 ab8500_usb_host_phy_dis(ab);
886 else if (ab->mode == USB_PERIPHERAL)
887 ab8500_usb_peri_phy_dis(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100888
889 platform_set_drvdata(pdev, NULL);
890
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100891 return 0;
892}
893
894static struct platform_driver ab8500_usb_driver = {
895 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500896 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100897 .driver = {
898 .name = "ab8500-usb",
899 .owner = THIS_MODULE,
900 },
901};
902
903static int __init ab8500_usb_init(void)
904{
905 return platform_driver_register(&ab8500_usb_driver);
906}
907subsys_initcall(ab8500_usb_init);
908
909static void __exit ab8500_usb_exit(void)
910{
911 platform_driver_unregister(&ab8500_usb_driver);
912}
913module_exit(ab8500_usb_exit);
914
915MODULE_ALIAS("platform:ab8500_usb");
916MODULE_AUTHOR("ST-Ericsson AB");
917MODULE_DESCRIPTION("AB8500 usb transceiver driver");
918MODULE_LICENSE("GPL");