blob: 58b194b7243229b0ddd95747a3f907c8e54f01c5 [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>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010036
Sakethram Bommisetti71246312013-04-03 10:45:04 +020037/* Bank AB8500_SYS_CTRL2_BLOCK */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010038#define AB8500_MAIN_WD_CTRL_REG 0x01
Sakethram Bommisetti71246312013-04-03 10:45:04 +020039
40/* Bank AB8500_USB */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010041#define AB8500_USB_LINE_STAT_REG 0x80
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080042#define AB8505_USB_LINE_STAT_REG 0x94
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010043#define AB8500_USB_PHY_CTRL_REG 0x8A
44
Sakethram Bommisetti71246312013-04-03 10:45:04 +020045/* Bank AB8500_DEVELOPMENT */
46#define AB8500_BANK12_ACCESS 0x00
47
48/* Bank AB8500_DEBUG */
49#define AB8500_USB_PHY_TUNE1 0x05
50#define AB8500_USB_PHY_TUNE2 0x06
51#define AB8500_USB_PHY_TUNE3 0x07
52
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010053#define AB8500_BIT_OTG_STAT_ID (1 << 0)
54#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
55#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
56#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
57#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
58
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010059#define AB8500_WD_KICK_DELAY_US 100 /* usec */
60#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080061#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010062
63/* Usb line status register */
64enum ab8500_usb_link_status {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080065 USB_LINK_NOT_CONFIGURED_8500 = 0,
66 USB_LINK_STD_HOST_NC_8500,
67 USB_LINK_STD_HOST_C_NS_8500,
68 USB_LINK_STD_HOST_C_S_8500,
69 USB_LINK_HOST_CHG_NM_8500,
70 USB_LINK_HOST_CHG_HS_8500,
71 USB_LINK_HOST_CHG_HS_CHIRP_8500,
72 USB_LINK_DEDICATED_CHG_8500,
73 USB_LINK_ACA_RID_A_8500,
74 USB_LINK_ACA_RID_B_8500,
75 USB_LINK_ACA_RID_C_NM_8500,
76 USB_LINK_ACA_RID_C_HS_8500,
77 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
78 USB_LINK_HM_IDGND_8500,
79 USB_LINK_RESERVED_8500,
80 USB_LINK_NOT_VALID_LINK_8500,
81};
82
83enum ab8505_usb_link_status {
84 USB_LINK_NOT_CONFIGURED_8505 = 0,
85 USB_LINK_STD_HOST_NC_8505,
86 USB_LINK_STD_HOST_C_NS_8505,
87 USB_LINK_STD_HOST_C_S_8505,
88 USB_LINK_CDP_8505,
89 USB_LINK_RESERVED0_8505,
90 USB_LINK_RESERVED1_8505,
91 USB_LINK_DEDICATED_CHG_8505,
92 USB_LINK_ACA_RID_A_8505,
93 USB_LINK_ACA_RID_B_8505,
94 USB_LINK_ACA_RID_C_NM_8505,
95 USB_LINK_RESERVED2_8505,
96 USB_LINK_RESERVED3_8505,
97 USB_LINK_HM_IDGND_8505,
98 USB_LINK_CHARGERPORT_NOT_OK_8505,
99 USB_LINK_CHARGER_DM_HIGH_8505,
100 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
101 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
102 USB_LINK_STD_UPSTREAM_8505,
103 USB_LINK_CHARGER_SE1_8505,
104 USB_LINK_CARKIT_CHGR_1_8505,
105 USB_LINK_CARKIT_CHGR_2_8505,
106 USB_LINK_ACA_DOCK_CHGR_8505,
107 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
108 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
109 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
110 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
111 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
112};
113
114enum ab8500_usb_mode {
115 USB_IDLE = 0,
116 USB_PERIPHERAL,
117 USB_HOST,
118 USB_DEDICATED_CHG
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100119};
120
121struct ab8500_usb {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200122 struct usb_phy phy;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100123 struct device *dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800124 struct ab8500 *ab8500;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100125 unsigned vbus_draw;
126 struct delayed_work dwork;
127 struct work_struct phy_dis_work;
128 unsigned long link_status_wait;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800129 enum ab8500_usb_mode mode;
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200130 struct regulator *v_ape;
131 struct regulator *v_musb;
132 struct regulator *v_ulpi;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800133 int previous_link_status_state;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100134};
135
Heikki Krogerus144713f2012-02-13 13:24:05 +0200136static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100137{
Heikki Krogerus144713f2012-02-13 13:24:05 +0200138 return container_of(x, struct ab8500_usb, phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100139}
140
141static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
142{
143 abx500_set_register_interruptible(ab->dev,
144 AB8500_SYS_CTRL2_BLOCK,
145 AB8500_MAIN_WD_CTRL_REG,
146 AB8500_BIT_WD_CTRL_ENABLE);
147
148 udelay(AB8500_WD_KICK_DELAY_US);
149
150 abx500_set_register_interruptible(ab->dev,
151 AB8500_SYS_CTRL2_BLOCK,
152 AB8500_MAIN_WD_CTRL_REG,
153 (AB8500_BIT_WD_CTRL_ENABLE
154 | AB8500_BIT_WD_CTRL_KICK));
155
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800156 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100157
158 abx500_set_register_interruptible(ab->dev,
159 AB8500_SYS_CTRL2_BLOCK,
160 AB8500_MAIN_WD_CTRL_REG,
161 0);
162}
163
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800164static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
165{
166 /* Workaround for v2.0 bug # 31952 */
167 if (is_ab8500_2p0(ab->ab8500)) {
168 abx500_mask_and_set_register_interruptible(ab->dev,
169 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
170 bit, bit);
171 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
172 }
173}
174
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200175static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100176{
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200177 u8 bit;
178 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
179 AB8500_BIT_PHY_CTRL_DEVICE_EN;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100180
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200181 abx500_mask_and_set_register_interruptible(ab->dev,
182 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
183 bit, bit);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100184}
185
Fabio Baltieric0ea7062013-04-03 10:45:07 +0200186static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
187{
188 u8 bit;
189 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
190 AB8500_BIT_PHY_CTRL_DEVICE_EN;
191
192 ab8500_usb_wd_linkstatus(ab, bit);
193
194 abx500_mask_and_set_register_interruptible(ab->dev,
195 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
196 bit, 0);
197
198 /* Needed to disable the phy.*/
199 ab8500_usb_wd_workaround(ab);
200}
201
202#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true)
203#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true)
204#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false)
205#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100206
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800207static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
208 enum ab8505_usb_link_status lsts)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100209{
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800210 enum ux500_musb_vbus_id_status event = 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100211
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800212 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100213
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800214 /*
215 * Spurious link_status interrupts are seen at the time of
216 * disconnection of a device in RIDA state
217 */
218 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
219 (lsts == USB_LINK_STD_HOST_NC_8505))
220 return 0;
221
222 ab->previous_link_status_state = lsts;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100223
224 switch (lsts) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800225 case USB_LINK_ACA_RID_B_8505:
226 event = UX500_MUSB_RIDB;
227 case USB_LINK_NOT_CONFIGURED_8505:
228 case USB_LINK_RESERVED0_8505:
229 case USB_LINK_RESERVED1_8505:
230 case USB_LINK_RESERVED2_8505:
231 case USB_LINK_RESERVED3_8505:
232 ab->mode = USB_IDLE;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200233 ab->phy.otg->default_a = false;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100234 ab->vbus_draw = 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800235 if (event != UX500_MUSB_RIDB)
236 event = UX500_MUSB_NONE;
237 /*
238 * Fallback to default B_IDLE as nothing
239 * is connected
240 */
241 ab->phy.state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100242 break;
243
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800244 case USB_LINK_ACA_RID_C_NM_8505:
245 event = UX500_MUSB_RIDC;
246 case USB_LINK_STD_HOST_NC_8505:
247 case USB_LINK_STD_HOST_C_NS_8505:
248 case USB_LINK_STD_HOST_C_S_8505:
249 case USB_LINK_CDP_8505:
250 if (ab->mode == USB_IDLE) {
251 ab->mode = USB_PERIPHERAL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100252 ab8500_usb_peri_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800253 atomic_notifier_call_chain(&ab->phy.notifier,
254 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100255 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800256 if (event != UX500_MUSB_RIDC)
257 event = UX500_MUSB_VBUS;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100258 break;
259
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800260 case USB_LINK_ACA_RID_A_8505:
261 case USB_LINK_ACA_DOCK_CHGR_8505:
262 event = UX500_MUSB_RIDA;
263 case USB_LINK_HM_IDGND_8505:
264 if (ab->mode == USB_IDLE) {
265 ab->mode = USB_HOST;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100266 ab8500_usb_host_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800267 atomic_notifier_call_chain(&ab->phy.notifier,
268 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100269 }
Heikki Krogerus144713f2012-02-13 13:24:05 +0200270 ab->phy.otg->default_a = true;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800271 if (event != UX500_MUSB_RIDA)
272 event = UX500_MUSB_ID;
273 atomic_notifier_call_chain(&ab->phy.notifier,
274 event, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100275 break;
276
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800277 case USB_LINK_DEDICATED_CHG_8505:
278 ab->mode = USB_DEDICATED_CHG;
279 event = UX500_MUSB_CHARGER;
280 atomic_notifier_call_chain(&ab->phy.notifier,
281 event, &ab->vbus_draw);
282 break;
283
284 default:
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100285 break;
286 }
287
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800288 return 0;
289}
290
291static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
292 enum ab8500_usb_link_status lsts)
293{
294 enum ux500_musb_vbus_id_status event = 0;
295
296 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
297
298 /*
299 * Spurious link_status interrupts are seen in case of a
300 * disconnection of a device in IDGND and RIDA stage
301 */
302 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
303 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
304 lsts == USB_LINK_STD_HOST_NC_8500))
305 return 0;
306
307 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
308 lsts == USB_LINK_STD_HOST_NC_8500)
309 return 0;
310
311 ab->previous_link_status_state = lsts;
312
313 switch (lsts) {
314 case USB_LINK_ACA_RID_B_8500:
315 event = UX500_MUSB_RIDB;
316 case USB_LINK_NOT_CONFIGURED_8500:
317 case USB_LINK_NOT_VALID_LINK_8500:
318 ab->mode = USB_IDLE;
319 ab->phy.otg->default_a = false;
320 ab->vbus_draw = 0;
321 if (event != UX500_MUSB_RIDB)
322 event = UX500_MUSB_NONE;
323 /* Fallback to default B_IDLE as nothing is connected */
324 ab->phy.state = OTG_STATE_B_IDLE;
325 break;
326
327 case USB_LINK_ACA_RID_C_NM_8500:
328 case USB_LINK_ACA_RID_C_HS_8500:
329 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
330 event = UX500_MUSB_RIDC;
331 case USB_LINK_STD_HOST_NC_8500:
332 case USB_LINK_STD_HOST_C_NS_8500:
333 case USB_LINK_STD_HOST_C_S_8500:
334 case USB_LINK_HOST_CHG_NM_8500:
335 case USB_LINK_HOST_CHG_HS_8500:
336 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
337 if (ab->mode == USB_IDLE) {
338 ab->mode = USB_PERIPHERAL;
339 ab8500_usb_peri_phy_en(ab);
340 atomic_notifier_call_chain(&ab->phy.notifier,
341 UX500_MUSB_PREPARE, &ab->vbus_draw);
342 }
343 if (event != UX500_MUSB_RIDC)
344 event = UX500_MUSB_VBUS;
345 break;
346
347 case USB_LINK_ACA_RID_A_8500:
348 event = UX500_MUSB_RIDA;
349 case USB_LINK_HM_IDGND_8500:
350 if (ab->mode == USB_IDLE) {
351 ab->mode = USB_HOST;
352 ab8500_usb_host_phy_en(ab);
353 atomic_notifier_call_chain(&ab->phy.notifier,
354 UX500_MUSB_PREPARE, &ab->vbus_draw);
355 }
356 ab->phy.otg->default_a = true;
357 if (event != UX500_MUSB_RIDA)
358 event = UX500_MUSB_ID;
359 atomic_notifier_call_chain(&ab->phy.notifier,
360 event, &ab->vbus_draw);
361 break;
362
363 case USB_LINK_DEDICATED_CHG_8500:
364 ab->mode = USB_DEDICATED_CHG;
365 event = UX500_MUSB_CHARGER;
366 atomic_notifier_call_chain(&ab->phy.notifier,
367 event, &ab->vbus_draw);
368 break;
369
370 case USB_LINK_RESERVED_8500:
371 break;
372 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100373
374 return 0;
375}
376
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800377/*
378 * Connection Sequence:
379 * 1. Link Status Interrupt
380 * 2. Enable AB clock
381 * 3. Enable AB regulators
382 * 4. Enable USB phy
383 * 5. Reset the musb controller
384 * 6. Switch the ULPI GPIO pins to fucntion mode
385 * 7. Enable the musb Peripheral5 clock
386 * 8. Restore MUSB context
387 */
388static int abx500_usb_link_status_update(struct ab8500_usb *ab)
389{
390 u8 reg;
391 int ret = 0;
392
393 if (is_ab8500(ab->ab8500)) {
394 enum ab8500_usb_link_status lsts;
395
396 abx500_get_register_interruptible(ab->dev,
397 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
398 lsts = (reg >> 3) & 0x0F;
399 ret = ab8500_usb_link_status_update(ab, lsts);
400 } else if (is_ab8505(ab->ab8500)) {
401 enum ab8505_usb_link_status lsts;
402
403 abx500_get_register_interruptible(ab->dev,
404 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
405 lsts = (reg >> 3) & 0x1F;
406 ret = ab8505_usb_link_status_update(ab, lsts);
407 }
408
409 return ret;
410}
411
412/*
413 * Disconnection Sequence:
414 * 1. Disconect Interrupt
415 * 2. Disable regulators
416 * 3. Disable AB clock
417 * 4. Disable the Phy
418 * 5. Link Status Interrupt
419 * 6. Disable Musb Clock
420 */
421static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
422{
423 struct ab8500_usb *ab = (struct ab8500_usb *) data;
424 enum usb_phy_events event = UX500_MUSB_NONE;
425
426 /* Link status will not be updated till phy is disabled. */
427 if (ab->mode == USB_HOST) {
428 ab->phy.otg->default_a = false;
429 ab->vbus_draw = 0;
430 atomic_notifier_call_chain(&ab->phy.notifier,
431 event, &ab->vbus_draw);
432 ab8500_usb_host_phy_dis(ab);
433 ab->mode = USB_IDLE;
434 }
435
436 if (ab->mode == USB_PERIPHERAL) {
437 atomic_notifier_call_chain(&ab->phy.notifier,
438 event, &ab->vbus_draw);
439 ab8500_usb_peri_phy_dis(ab);
440 atomic_notifier_call_chain(&ab->phy.notifier,
441 UX500_MUSB_CLEAN, &ab->vbus_draw);
442 ab->mode = USB_IDLE;
443 ab->phy.otg->default_a = false;
444 ab->vbus_draw = 0;
445 }
446
447 if (is_ab8500_2p0(ab->ab8500)) {
448 if (ab->mode == USB_DEDICATED_CHG) {
449 ab8500_usb_wd_linkstatus(ab,
450 AB8500_BIT_PHY_CTRL_DEVICE_EN);
451 abx500_mask_and_set_register_interruptible(ab->dev,
452 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
453 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
454 }
455 }
456
457 return IRQ_HANDLED;
458}
459
460static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
461{
462 struct ab8500_usb *ab = (struct ab8500_usb *) data;
463
464 abx500_usb_link_status_update(ab);
465
466 return IRQ_HANDLED;
467}
468
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100469static void ab8500_usb_delayed_work(struct work_struct *work)
470{
471 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
472 dwork.work);
473
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800474 abx500_usb_link_status_update(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100475}
476
477static void ab8500_usb_phy_disable_work(struct work_struct *work)
478{
479 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
480 phy_dis_work);
481
Heikki Krogerus144713f2012-02-13 13:24:05 +0200482 if (!ab->phy.otg->host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100483 ab8500_usb_host_phy_dis(ab);
484
Heikki Krogerus144713f2012-02-13 13:24:05 +0200485 if (!ab->phy.otg->gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100486 ab8500_usb_peri_phy_dis(ab);
487}
488
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200489static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
490{
491 /*
492 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
493 * VBUS. Set charging current to 100mA in case of standard host
494 */
495 if (is_ab8500_2p0_or_earlier(ab->ab8500))
496 if (mA > 100)
497 mA = 100;
498
499 return mA;
500}
501
Heikki Krogerus144713f2012-02-13 13:24:05 +0200502static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100503{
504 struct ab8500_usb *ab;
505
Heikki Krogerus144713f2012-02-13 13:24:05 +0200506 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100507 return -ENODEV;
508
Heikki Krogerus144713f2012-02-13 13:24:05 +0200509 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100510
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200511 mA = ab8500_eyediagram_workaroud(ab, mA);
512
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100513 ab->vbus_draw = mA;
514
515 if (mA)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200516 atomic_notifier_call_chain(&ab->phy.notifier,
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800517 UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100518 return 0;
519}
520
521/* TODO: Implement some way for charging or other drivers to read
522 * ab->vbus_draw.
523 */
524
Heikki Krogerus86753812012-02-13 13:24:02 +0200525static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100526{
527 /* TODO */
528 return 0;
529}
530
Heikki Krogerus144713f2012-02-13 13:24:05 +0200531static int ab8500_usb_set_peripheral(struct usb_otg *otg,
532 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100533{
534 struct ab8500_usb *ab;
535
536 if (!otg)
537 return -ENODEV;
538
Heikki Krogerus144713f2012-02-13 13:24:05 +0200539 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100540
541 /* Some drivers call this function in atomic context.
542 * Do not update ab8500 registers directly till this
543 * is fixed.
544 */
545
546 if (!gadget) {
547 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200548 otg->gadget = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100549 schedule_work(&ab->phy_dis_work);
550 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200551 otg->gadget = gadget;
552 otg->phy->state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100553
554 /* Phy will not be enabled if cable is already
555 * plugged-in. Schedule to enable phy.
556 * Use same delay to avoid any race condition.
557 */
558 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
559 }
560
561 return 0;
562}
563
Heikki Krogerus144713f2012-02-13 13:24:05 +0200564static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100565{
566 struct ab8500_usb *ab;
567
568 if (!otg)
569 return -ENODEV;
570
Heikki Krogerus144713f2012-02-13 13:24:05 +0200571 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100572
573 /* Some drivers call this function in atomic context.
574 * Do not update ab8500 registers directly till this
575 * is fixed.
576 */
577
578 if (!host) {
579 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200580 otg->host = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100581 schedule_work(&ab->phy_dis_work);
582 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200583 otg->host = host;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100584 /* Phy will not be enabled if cable is already
585 * plugged-in. Schedule to enable phy.
586 * Use same delay to avoid any race condition.
587 */
588 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
589 }
590
591 return 0;
592}
593
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200594static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
595{
596 int err;
597
598 ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
599 if (IS_ERR(ab->v_ape)) {
600 dev_err(ab->dev, "Could not get v-ape supply\n");
601 err = PTR_ERR(ab->v_ape);
602 return err;
603 }
604
605 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
606 if (IS_ERR(ab->v_ulpi)) {
607 dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
608 err = PTR_ERR(ab->v_ulpi);
609 return err;
610 }
611
612 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
613 if (IS_ERR(ab->v_musb)) {
614 dev_err(ab->dev, "Could not get musb_1v8 supply\n");
615 err = PTR_ERR(ab->v_musb);
616 return err;
617 }
618
619 return 0;
620}
621
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800622static int ab8500_usb_irq_setup(struct platform_device *pdev,
623 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100624{
625 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800626 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100627
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800628 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
629 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100630 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800631 return irq;
632 }
633 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
634 ab8500_usb_link_status_irq,
635 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
636 if (err < 0) {
637 dev_err(ab->dev, "request_irq failed for link status irq\n");
638 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100639 }
640
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800641 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
642 if (irq < 0) {
643 dev_err(&pdev->dev, "ID fall irq not found\n");
644 return irq;
645 }
646 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
647 ab8500_usb_disconnect_irq,
648 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100649 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800650 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
651 return err;
652 }
653
654 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
655 if (irq < 0) {
656 dev_err(&pdev->dev, "VBUS fall irq not found\n");
657 return irq;
658 }
659 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
660 ab8500_usb_disconnect_irq,
661 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
662 if (err < 0) {
663 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100664 return err;
665 }
666
667 return 0;
668}
669
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500670static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100671{
672 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800673 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200674 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100675 int err;
676 int rev;
677
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800678 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100679 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800680
681 if (is_ab8500_1p1_or_earlier(ab8500)) {
682 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100683 return -ENODEV;
684 }
685
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200686 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100687 if (!ab)
688 return -ENOMEM;
689
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200690 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
691 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200692 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200693
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100694 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800695 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200696 ab->phy.dev = ab->dev;
697 ab->phy.otg = otg;
698 ab->phy.label = "ab8500";
699 ab->phy.set_suspend = ab8500_usb_set_suspend;
700 ab->phy.set_power = ab8500_usb_set_power;
701 ab->phy.state = OTG_STATE_UNDEFINED;
702
703 otg->phy = &ab->phy;
704 otg->set_host = ab8500_usb_set_host;
705 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100706
707 platform_set_drvdata(pdev, ab);
708
Heikki Krogerus144713f2012-02-13 13:24:05 +0200709 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100710
711 /* v1: Wait for link status to become stable.
712 * all: Updates form set_host and set_peripheral as they are atomic.
713 */
714 INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
715
716 /* all: Disable phy when called from set_host and set_peripheral */
717 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
718
Fabio Baltierie65b36c2013-04-03 10:45:06 +0200719 err = ab8500_usb_regulator_get(ab);
720 if (err)
721 return err;
722
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800723 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100724 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200725 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100726
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530727 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100728 if (err) {
729 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200730 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100731 }
732
Sakethram Bommisetti71246312013-04-03 10:45:04 +0200733 /* Phy tuning values for AB8500 */
734 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
735 /* Enable the PBT/Bank 0x12 access */
736 err = abx500_set_register_interruptible(ab->dev,
737 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
738 if (err < 0)
739 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
740 err);
741
742 err = abx500_set_register_interruptible(ab->dev,
743 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
744 if (err < 0)
745 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
746 err);
747
748 err = abx500_set_register_interruptible(ab->dev,
749 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
750 if (err < 0)
751 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
752 err);
753
754 err = abx500_set_register_interruptible(ab->dev,
755 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
756 if (err < 0)
757 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
758 err);
759
760 /* Switch to normal mode/disable Bank 0x12 access */
761 err = abx500_set_register_interruptible(ab->dev,
762 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
763 if (err < 0)
764 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
765 err);
766 }
767
768 /* Phy tuning values for AB8505 */
769 if (is_ab8505(ab->ab8500)) {
770 /* Enable the PBT/Bank 0x12 access */
771 err = abx500_mask_and_set_register_interruptible(ab->dev,
772 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
773 0x01, 0x01);
774 if (err < 0)
775 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
776 err);
777
778 err = abx500_mask_and_set_register_interruptible(ab->dev,
779 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
780 0xC8, 0xC8);
781 if (err < 0)
782 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
783 err);
784
785 err = abx500_mask_and_set_register_interruptible(ab->dev,
786 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
787 0x60, 0x60);
788 if (err < 0)
789 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
790 err);
791
792 err = abx500_mask_and_set_register_interruptible(ab->dev,
793 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
794 0xFC, 0x80);
795
796 if (err < 0)
797 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
798 err);
799
800 /* Switch to normal mode/disable Bank 0x12 access */
801 err = abx500_mask_and_set_register_interruptible(ab->dev,
802 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
803 0x00, 0x00);
804 if (err < 0)
805 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
806 err);
807 }
808
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800809 /* Needed to enable ID detection. */
810 ab8500_usb_wd_workaround(ab);
811
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800812 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100813
814 return 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100815}
816
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500817static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100818{
819 struct ab8500_usb *ab = platform_get_drvdata(pdev);
820
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100821 cancel_delayed_work_sync(&ab->dwork);
822
823 cancel_work_sync(&ab->phy_dis_work);
824
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530825 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100826
827 ab8500_usb_host_phy_dis(ab);
828 ab8500_usb_peri_phy_dis(ab);
829
830 platform_set_drvdata(pdev, NULL);
831
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100832 return 0;
833}
834
835static struct platform_driver ab8500_usb_driver = {
836 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500837 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100838 .driver = {
839 .name = "ab8500-usb",
840 .owner = THIS_MODULE,
841 },
842};
843
844static int __init ab8500_usb_init(void)
845{
846 return platform_driver_register(&ab8500_usb_driver);
847}
848subsys_initcall(ab8500_usb_init);
849
850static void __exit ab8500_usb_exit(void)
851{
852 platform_driver_unregister(&ab8500_usb_driver);
853}
854module_exit(ab8500_usb_exit);
855
856MODULE_ALIAS("platform:ab8500_usb");
857MODULE_AUTHOR("ST-Ericsson AB");
858MODULE_DESCRIPTION("AB8500 usb transceiver driver");
859MODULE_LICENSE("GPL");