blob: 441b2954cffd8e617037f8ea773b8e43c992bca3 [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>
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010035
Sakethram Bommisetti71246312013-04-03 10:45:04 +020036/* Bank AB8500_SYS_CTRL2_BLOCK */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010037#define AB8500_MAIN_WD_CTRL_REG 0x01
Sakethram Bommisetti71246312013-04-03 10:45:04 +020038
39/* Bank AB8500_USB */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010040#define AB8500_USB_LINE_STAT_REG 0x80
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080041#define AB8505_USB_LINE_STAT_REG 0x94
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010042#define AB8500_USB_PHY_CTRL_REG 0x8A
43
Sakethram Bommisetti71246312013-04-03 10:45:04 +020044/* Bank AB8500_DEVELOPMENT */
45#define AB8500_BANK12_ACCESS 0x00
46
47/* Bank AB8500_DEBUG */
48#define AB8500_USB_PHY_TUNE1 0x05
49#define AB8500_USB_PHY_TUNE2 0x06
50#define AB8500_USB_PHY_TUNE3 0x07
51
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010052#define AB8500_BIT_OTG_STAT_ID (1 << 0)
53#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
54#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
55#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
56#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
57
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010058#define AB8500_WD_KICK_DELAY_US 100 /* usec */
59#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080060#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010061
62/* Usb line status register */
63enum ab8500_usb_link_status {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080064 USB_LINK_NOT_CONFIGURED_8500 = 0,
65 USB_LINK_STD_HOST_NC_8500,
66 USB_LINK_STD_HOST_C_NS_8500,
67 USB_LINK_STD_HOST_C_S_8500,
68 USB_LINK_HOST_CHG_NM_8500,
69 USB_LINK_HOST_CHG_HS_8500,
70 USB_LINK_HOST_CHG_HS_CHIRP_8500,
71 USB_LINK_DEDICATED_CHG_8500,
72 USB_LINK_ACA_RID_A_8500,
73 USB_LINK_ACA_RID_B_8500,
74 USB_LINK_ACA_RID_C_NM_8500,
75 USB_LINK_ACA_RID_C_HS_8500,
76 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
77 USB_LINK_HM_IDGND_8500,
78 USB_LINK_RESERVED_8500,
79 USB_LINK_NOT_VALID_LINK_8500,
80};
81
82enum ab8505_usb_link_status {
83 USB_LINK_NOT_CONFIGURED_8505 = 0,
84 USB_LINK_STD_HOST_NC_8505,
85 USB_LINK_STD_HOST_C_NS_8505,
86 USB_LINK_STD_HOST_C_S_8505,
87 USB_LINK_CDP_8505,
88 USB_LINK_RESERVED0_8505,
89 USB_LINK_RESERVED1_8505,
90 USB_LINK_DEDICATED_CHG_8505,
91 USB_LINK_ACA_RID_A_8505,
92 USB_LINK_ACA_RID_B_8505,
93 USB_LINK_ACA_RID_C_NM_8505,
94 USB_LINK_RESERVED2_8505,
95 USB_LINK_RESERVED3_8505,
96 USB_LINK_HM_IDGND_8505,
97 USB_LINK_CHARGERPORT_NOT_OK_8505,
98 USB_LINK_CHARGER_DM_HIGH_8505,
99 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
100 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
101 USB_LINK_STD_UPSTREAM_8505,
102 USB_LINK_CHARGER_SE1_8505,
103 USB_LINK_CARKIT_CHGR_1_8505,
104 USB_LINK_CARKIT_CHGR_2_8505,
105 USB_LINK_ACA_DOCK_CHGR_8505,
106 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
107 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
108 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
109 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
110 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
111};
112
113enum ab8500_usb_mode {
114 USB_IDLE = 0,
115 USB_PERIPHERAL,
116 USB_HOST,
117 USB_DEDICATED_CHG
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100118};
119
120struct ab8500_usb {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200121 struct usb_phy phy;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100122 struct device *dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800123 struct ab8500 *ab8500;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100124 unsigned vbus_draw;
125 struct delayed_work dwork;
126 struct work_struct phy_dis_work;
127 unsigned long link_status_wait;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800128 enum ab8500_usb_mode mode;
129 int previous_link_status_state;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100130};
131
Heikki Krogerus144713f2012-02-13 13:24:05 +0200132static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100133{
Heikki Krogerus144713f2012-02-13 13:24:05 +0200134 return container_of(x, struct ab8500_usb, phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100135}
136
137static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
138{
139 abx500_set_register_interruptible(ab->dev,
140 AB8500_SYS_CTRL2_BLOCK,
141 AB8500_MAIN_WD_CTRL_REG,
142 AB8500_BIT_WD_CTRL_ENABLE);
143
144 udelay(AB8500_WD_KICK_DELAY_US);
145
146 abx500_set_register_interruptible(ab->dev,
147 AB8500_SYS_CTRL2_BLOCK,
148 AB8500_MAIN_WD_CTRL_REG,
149 (AB8500_BIT_WD_CTRL_ENABLE
150 | AB8500_BIT_WD_CTRL_KICK));
151
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800152 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100153
154 abx500_set_register_interruptible(ab->dev,
155 AB8500_SYS_CTRL2_BLOCK,
156 AB8500_MAIN_WD_CTRL_REG,
157 0);
158}
159
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800160static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
161{
162 /* Workaround for v2.0 bug # 31952 */
163 if (is_ab8500_2p0(ab->ab8500)) {
164 abx500_mask_and_set_register_interruptible(ab->dev,
165 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
166 bit, bit);
167 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
168 }
169}
170
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100171static void ab8500_usb_phy_ctrl(struct ab8500_usb *ab, bool sel_host,
172 bool enable)
173{
174 u8 ctrl_reg;
175 abx500_get_register_interruptible(ab->dev,
176 AB8500_USB,
177 AB8500_USB_PHY_CTRL_REG,
178 &ctrl_reg);
179 if (sel_host) {
180 if (enable)
181 ctrl_reg |= AB8500_BIT_PHY_CTRL_HOST_EN;
182 else
183 ctrl_reg &= ~AB8500_BIT_PHY_CTRL_HOST_EN;
184 } else {
185 if (enable)
186 ctrl_reg |= AB8500_BIT_PHY_CTRL_DEVICE_EN;
187 else
188 ctrl_reg &= ~AB8500_BIT_PHY_CTRL_DEVICE_EN;
189 }
190
191 abx500_set_register_interruptible(ab->dev,
192 AB8500_USB,
193 AB8500_USB_PHY_CTRL_REG,
194 ctrl_reg);
195
196 /* Needed to enable the phy.*/
197 if (enable)
198 ab8500_usb_wd_workaround(ab);
199}
200
201#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_ctrl(ab, true, true)
202#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_ctrl(ab, true, false)
203#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_ctrl(ab, false, true)
204#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_ctrl(ab, false, false)
205
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800206static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
207 enum ab8505_usb_link_status lsts)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100208{
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800209 enum ux500_musb_vbus_id_status event = 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100210
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800211 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100212
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800213 /*
214 * Spurious link_status interrupts are seen at the time of
215 * disconnection of a device in RIDA state
216 */
217 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
218 (lsts == USB_LINK_STD_HOST_NC_8505))
219 return 0;
220
221 ab->previous_link_status_state = lsts;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100222
223 switch (lsts) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800224 case USB_LINK_ACA_RID_B_8505:
225 event = UX500_MUSB_RIDB;
226 case USB_LINK_NOT_CONFIGURED_8505:
227 case USB_LINK_RESERVED0_8505:
228 case USB_LINK_RESERVED1_8505:
229 case USB_LINK_RESERVED2_8505:
230 case USB_LINK_RESERVED3_8505:
231 ab->mode = USB_IDLE;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200232 ab->phy.otg->default_a = false;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100233 ab->vbus_draw = 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800234 if (event != UX500_MUSB_RIDB)
235 event = UX500_MUSB_NONE;
236 /*
237 * Fallback to default B_IDLE as nothing
238 * is connected
239 */
240 ab->phy.state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100241 break;
242
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800243 case USB_LINK_ACA_RID_C_NM_8505:
244 event = UX500_MUSB_RIDC;
245 case USB_LINK_STD_HOST_NC_8505:
246 case USB_LINK_STD_HOST_C_NS_8505:
247 case USB_LINK_STD_HOST_C_S_8505:
248 case USB_LINK_CDP_8505:
249 if (ab->mode == USB_IDLE) {
250 ab->mode = USB_PERIPHERAL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100251 ab8500_usb_peri_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800252 atomic_notifier_call_chain(&ab->phy.notifier,
253 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100254 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800255 if (event != UX500_MUSB_RIDC)
256 event = UX500_MUSB_VBUS;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100257 break;
258
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800259 case USB_LINK_ACA_RID_A_8505:
260 case USB_LINK_ACA_DOCK_CHGR_8505:
261 event = UX500_MUSB_RIDA;
262 case USB_LINK_HM_IDGND_8505:
263 if (ab->mode == USB_IDLE) {
264 ab->mode = USB_HOST;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100265 ab8500_usb_host_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800266 atomic_notifier_call_chain(&ab->phy.notifier,
267 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100268 }
Heikki Krogerus144713f2012-02-13 13:24:05 +0200269 ab->phy.otg->default_a = true;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800270 if (event != UX500_MUSB_RIDA)
271 event = UX500_MUSB_ID;
272 atomic_notifier_call_chain(&ab->phy.notifier,
273 event, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100274 break;
275
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800276 case USB_LINK_DEDICATED_CHG_8505:
277 ab->mode = USB_DEDICATED_CHG;
278 event = UX500_MUSB_CHARGER;
279 atomic_notifier_call_chain(&ab->phy.notifier,
280 event, &ab->vbus_draw);
281 break;
282
283 default:
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100284 break;
285 }
286
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800287 return 0;
288}
289
290static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
291 enum ab8500_usb_link_status lsts)
292{
293 enum ux500_musb_vbus_id_status event = 0;
294
295 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
296
297 /*
298 * Spurious link_status interrupts are seen in case of a
299 * disconnection of a device in IDGND and RIDA stage
300 */
301 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
302 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
303 lsts == USB_LINK_STD_HOST_NC_8500))
304 return 0;
305
306 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
307 lsts == USB_LINK_STD_HOST_NC_8500)
308 return 0;
309
310 ab->previous_link_status_state = lsts;
311
312 switch (lsts) {
313 case USB_LINK_ACA_RID_B_8500:
314 event = UX500_MUSB_RIDB;
315 case USB_LINK_NOT_CONFIGURED_8500:
316 case USB_LINK_NOT_VALID_LINK_8500:
317 ab->mode = USB_IDLE;
318 ab->phy.otg->default_a = false;
319 ab->vbus_draw = 0;
320 if (event != UX500_MUSB_RIDB)
321 event = UX500_MUSB_NONE;
322 /* Fallback to default B_IDLE as nothing is connected */
323 ab->phy.state = OTG_STATE_B_IDLE;
324 break;
325
326 case USB_LINK_ACA_RID_C_NM_8500:
327 case USB_LINK_ACA_RID_C_HS_8500:
328 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
329 event = UX500_MUSB_RIDC;
330 case USB_LINK_STD_HOST_NC_8500:
331 case USB_LINK_STD_HOST_C_NS_8500:
332 case USB_LINK_STD_HOST_C_S_8500:
333 case USB_LINK_HOST_CHG_NM_8500:
334 case USB_LINK_HOST_CHG_HS_8500:
335 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
336 if (ab->mode == USB_IDLE) {
337 ab->mode = USB_PERIPHERAL;
338 ab8500_usb_peri_phy_en(ab);
339 atomic_notifier_call_chain(&ab->phy.notifier,
340 UX500_MUSB_PREPARE, &ab->vbus_draw);
341 }
342 if (event != UX500_MUSB_RIDC)
343 event = UX500_MUSB_VBUS;
344 break;
345
346 case USB_LINK_ACA_RID_A_8500:
347 event = UX500_MUSB_RIDA;
348 case USB_LINK_HM_IDGND_8500:
349 if (ab->mode == USB_IDLE) {
350 ab->mode = USB_HOST;
351 ab8500_usb_host_phy_en(ab);
352 atomic_notifier_call_chain(&ab->phy.notifier,
353 UX500_MUSB_PREPARE, &ab->vbus_draw);
354 }
355 ab->phy.otg->default_a = true;
356 if (event != UX500_MUSB_RIDA)
357 event = UX500_MUSB_ID;
358 atomic_notifier_call_chain(&ab->phy.notifier,
359 event, &ab->vbus_draw);
360 break;
361
362 case USB_LINK_DEDICATED_CHG_8500:
363 ab->mode = USB_DEDICATED_CHG;
364 event = UX500_MUSB_CHARGER;
365 atomic_notifier_call_chain(&ab->phy.notifier,
366 event, &ab->vbus_draw);
367 break;
368
369 case USB_LINK_RESERVED_8500:
370 break;
371 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100372
373 return 0;
374}
375
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800376/*
377 * Connection Sequence:
378 * 1. Link Status Interrupt
379 * 2. Enable AB clock
380 * 3. Enable AB regulators
381 * 4. Enable USB phy
382 * 5. Reset the musb controller
383 * 6. Switch the ULPI GPIO pins to fucntion mode
384 * 7. Enable the musb Peripheral5 clock
385 * 8. Restore MUSB context
386 */
387static int abx500_usb_link_status_update(struct ab8500_usb *ab)
388{
389 u8 reg;
390 int ret = 0;
391
392 if (is_ab8500(ab->ab8500)) {
393 enum ab8500_usb_link_status lsts;
394
395 abx500_get_register_interruptible(ab->dev,
396 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
397 lsts = (reg >> 3) & 0x0F;
398 ret = ab8500_usb_link_status_update(ab, lsts);
399 } else if (is_ab8505(ab->ab8500)) {
400 enum ab8505_usb_link_status lsts;
401
402 abx500_get_register_interruptible(ab->dev,
403 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
404 lsts = (reg >> 3) & 0x1F;
405 ret = ab8505_usb_link_status_update(ab, lsts);
406 }
407
408 return ret;
409}
410
411/*
412 * Disconnection Sequence:
413 * 1. Disconect Interrupt
414 * 2. Disable regulators
415 * 3. Disable AB clock
416 * 4. Disable the Phy
417 * 5. Link Status Interrupt
418 * 6. Disable Musb Clock
419 */
420static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
421{
422 struct ab8500_usb *ab = (struct ab8500_usb *) data;
423 enum usb_phy_events event = UX500_MUSB_NONE;
424
425 /* Link status will not be updated till phy is disabled. */
426 if (ab->mode == USB_HOST) {
427 ab->phy.otg->default_a = false;
428 ab->vbus_draw = 0;
429 atomic_notifier_call_chain(&ab->phy.notifier,
430 event, &ab->vbus_draw);
431 ab8500_usb_host_phy_dis(ab);
432 ab->mode = USB_IDLE;
433 }
434
435 if (ab->mode == USB_PERIPHERAL) {
436 atomic_notifier_call_chain(&ab->phy.notifier,
437 event, &ab->vbus_draw);
438 ab8500_usb_peri_phy_dis(ab);
439 atomic_notifier_call_chain(&ab->phy.notifier,
440 UX500_MUSB_CLEAN, &ab->vbus_draw);
441 ab->mode = USB_IDLE;
442 ab->phy.otg->default_a = false;
443 ab->vbus_draw = 0;
444 }
445
446 if (is_ab8500_2p0(ab->ab8500)) {
447 if (ab->mode == USB_DEDICATED_CHG) {
448 ab8500_usb_wd_linkstatus(ab,
449 AB8500_BIT_PHY_CTRL_DEVICE_EN);
450 abx500_mask_and_set_register_interruptible(ab->dev,
451 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
452 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
453 }
454 }
455
456 return IRQ_HANDLED;
457}
458
459static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
460{
461 struct ab8500_usb *ab = (struct ab8500_usb *) data;
462
463 abx500_usb_link_status_update(ab);
464
465 return IRQ_HANDLED;
466}
467
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100468static void ab8500_usb_delayed_work(struct work_struct *work)
469{
470 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
471 dwork.work);
472
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800473 abx500_usb_link_status_update(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100474}
475
476static void ab8500_usb_phy_disable_work(struct work_struct *work)
477{
478 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
479 phy_dis_work);
480
Heikki Krogerus144713f2012-02-13 13:24:05 +0200481 if (!ab->phy.otg->host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100482 ab8500_usb_host_phy_dis(ab);
483
Heikki Krogerus144713f2012-02-13 13:24:05 +0200484 if (!ab->phy.otg->gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100485 ab8500_usb_peri_phy_dis(ab);
486}
487
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200488static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
489{
490 /*
491 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
492 * VBUS. Set charging current to 100mA in case of standard host
493 */
494 if (is_ab8500_2p0_or_earlier(ab->ab8500))
495 if (mA > 100)
496 mA = 100;
497
498 return mA;
499}
500
Heikki Krogerus144713f2012-02-13 13:24:05 +0200501static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100502{
503 struct ab8500_usb *ab;
504
Heikki Krogerus144713f2012-02-13 13:24:05 +0200505 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100506 return -ENODEV;
507
Heikki Krogerus144713f2012-02-13 13:24:05 +0200508 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100509
Sakethram Bommisettic2a0ab62013-04-03 10:45:05 +0200510 mA = ab8500_eyediagram_workaroud(ab, mA);
511
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100512 ab->vbus_draw = mA;
513
514 if (mA)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200515 atomic_notifier_call_chain(&ab->phy.notifier,
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800516 UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100517 return 0;
518}
519
520/* TODO: Implement some way for charging or other drivers to read
521 * ab->vbus_draw.
522 */
523
Heikki Krogerus86753812012-02-13 13:24:02 +0200524static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100525{
526 /* TODO */
527 return 0;
528}
529
Heikki Krogerus144713f2012-02-13 13:24:05 +0200530static int ab8500_usb_set_peripheral(struct usb_otg *otg,
531 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100532{
533 struct ab8500_usb *ab;
534
535 if (!otg)
536 return -ENODEV;
537
Heikki Krogerus144713f2012-02-13 13:24:05 +0200538 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100539
540 /* Some drivers call this function in atomic context.
541 * Do not update ab8500 registers directly till this
542 * is fixed.
543 */
544
545 if (!gadget) {
546 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200547 otg->gadget = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100548 schedule_work(&ab->phy_dis_work);
549 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200550 otg->gadget = gadget;
551 otg->phy->state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100552
553 /* Phy will not be enabled if cable is already
554 * plugged-in. Schedule to enable phy.
555 * Use same delay to avoid any race condition.
556 */
557 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
558 }
559
560 return 0;
561}
562
Heikki Krogerus144713f2012-02-13 13:24:05 +0200563static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100564{
565 struct ab8500_usb *ab;
566
567 if (!otg)
568 return -ENODEV;
569
Heikki Krogerus144713f2012-02-13 13:24:05 +0200570 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100571
572 /* Some drivers call this function in atomic context.
573 * Do not update ab8500 registers directly till this
574 * is fixed.
575 */
576
577 if (!host) {
578 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200579 otg->host = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100580 schedule_work(&ab->phy_dis_work);
581 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200582 otg->host = host;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100583 /* Phy will not be enabled if cable is already
584 * plugged-in. Schedule to enable phy.
585 * Use same delay to avoid any race condition.
586 */
587 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
588 }
589
590 return 0;
591}
592
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800593static int ab8500_usb_irq_setup(struct platform_device *pdev,
594 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100595{
596 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800597 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100598
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800599 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
600 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100601 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800602 return irq;
603 }
604 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
605 ab8500_usb_link_status_irq,
606 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
607 if (err < 0) {
608 dev_err(ab->dev, "request_irq failed for link status irq\n");
609 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100610 }
611
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800612 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
613 if (irq < 0) {
614 dev_err(&pdev->dev, "ID fall irq not found\n");
615 return irq;
616 }
617 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
618 ab8500_usb_disconnect_irq,
619 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100620 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800621 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
622 return err;
623 }
624
625 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
626 if (irq < 0) {
627 dev_err(&pdev->dev, "VBUS fall irq not found\n");
628 return irq;
629 }
630 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
631 ab8500_usb_disconnect_irq,
632 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
633 if (err < 0) {
634 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100635 return err;
636 }
637
638 return 0;
639}
640
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500641static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100642{
643 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800644 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200645 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100646 int err;
647 int rev;
648
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800649 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100650 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800651
652 if (is_ab8500_1p1_or_earlier(ab8500)) {
653 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100654 return -ENODEV;
655 }
656
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200657 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100658 if (!ab)
659 return -ENOMEM;
660
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200661 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
662 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200663 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200664
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100665 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800666 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200667 ab->phy.dev = ab->dev;
668 ab->phy.otg = otg;
669 ab->phy.label = "ab8500";
670 ab->phy.set_suspend = ab8500_usb_set_suspend;
671 ab->phy.set_power = ab8500_usb_set_power;
672 ab->phy.state = OTG_STATE_UNDEFINED;
673
674 otg->phy = &ab->phy;
675 otg->set_host = ab8500_usb_set_host;
676 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100677
678 platform_set_drvdata(pdev, ab);
679
Heikki Krogerus144713f2012-02-13 13:24:05 +0200680 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100681
682 /* v1: Wait for link status to become stable.
683 * all: Updates form set_host and set_peripheral as they are atomic.
684 */
685 INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
686
687 /* all: Disable phy when called from set_host and set_peripheral */
688 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
689
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800690 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100691 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200692 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100693
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530694 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100695 if (err) {
696 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200697 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100698 }
699
Sakethram Bommisetti71246312013-04-03 10:45:04 +0200700 /* Phy tuning values for AB8500 */
701 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
702 /* Enable the PBT/Bank 0x12 access */
703 err = abx500_set_register_interruptible(ab->dev,
704 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
705 if (err < 0)
706 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
707 err);
708
709 err = abx500_set_register_interruptible(ab->dev,
710 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
711 if (err < 0)
712 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
713 err);
714
715 err = abx500_set_register_interruptible(ab->dev,
716 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
717 if (err < 0)
718 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
719 err);
720
721 err = abx500_set_register_interruptible(ab->dev,
722 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
723 if (err < 0)
724 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
725 err);
726
727 /* Switch to normal mode/disable Bank 0x12 access */
728 err = abx500_set_register_interruptible(ab->dev,
729 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
730 if (err < 0)
731 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
732 err);
733 }
734
735 /* Phy tuning values for AB8505 */
736 if (is_ab8505(ab->ab8500)) {
737 /* Enable the PBT/Bank 0x12 access */
738 err = abx500_mask_and_set_register_interruptible(ab->dev,
739 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
740 0x01, 0x01);
741 if (err < 0)
742 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
743 err);
744
745 err = abx500_mask_and_set_register_interruptible(ab->dev,
746 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
747 0xC8, 0xC8);
748 if (err < 0)
749 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
750 err);
751
752 err = abx500_mask_and_set_register_interruptible(ab->dev,
753 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
754 0x60, 0x60);
755 if (err < 0)
756 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
757 err);
758
759 err = abx500_mask_and_set_register_interruptible(ab->dev,
760 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
761 0xFC, 0x80);
762
763 if (err < 0)
764 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
765 err);
766
767 /* Switch to normal mode/disable Bank 0x12 access */
768 err = abx500_mask_and_set_register_interruptible(ab->dev,
769 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
770 0x00, 0x00);
771 if (err < 0)
772 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
773 err);
774 }
775
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800776 /* Needed to enable ID detection. */
777 ab8500_usb_wd_workaround(ab);
778
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800779 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100780
781 return 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100782}
783
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500784static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100785{
786 struct ab8500_usb *ab = platform_get_drvdata(pdev);
787
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100788 cancel_delayed_work_sync(&ab->dwork);
789
790 cancel_work_sync(&ab->phy_dis_work);
791
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530792 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100793
794 ab8500_usb_host_phy_dis(ab);
795 ab8500_usb_peri_phy_dis(ab);
796
797 platform_set_drvdata(pdev, NULL);
798
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100799 return 0;
800}
801
802static struct platform_driver ab8500_usb_driver = {
803 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500804 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100805 .driver = {
806 .name = "ab8500-usb",
807 .owner = THIS_MODULE,
808 },
809};
810
811static int __init ab8500_usb_init(void)
812{
813 return platform_driver_register(&ab8500_usb_driver);
814}
815subsys_initcall(ab8500_usb_init);
816
817static void __exit ab8500_usb_exit(void)
818{
819 platform_driver_unregister(&ab8500_usb_driver);
820}
821module_exit(ab8500_usb_exit);
822
823MODULE_ALIAS("platform:ab8500_usb");
824MODULE_AUTHOR("ST-Ericsson AB");
825MODULE_DESCRIPTION("AB8500 usb transceiver driver");
826MODULE_LICENSE("GPL");