blob: 5b92a59804eb7a088ce22ceba14c5e7629921af2 [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
Heikki Krogerus144713f2012-02-13 13:24:05 +0200488static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100489{
490 struct ab8500_usb *ab;
491
Heikki Krogerus144713f2012-02-13 13:24:05 +0200492 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100493 return -ENODEV;
494
Heikki Krogerus144713f2012-02-13 13:24:05 +0200495 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100496
497 ab->vbus_draw = mA;
498
499 if (mA)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200500 atomic_notifier_call_chain(&ab->phy.notifier,
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800501 UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100502 return 0;
503}
504
505/* TODO: Implement some way for charging or other drivers to read
506 * ab->vbus_draw.
507 */
508
Heikki Krogerus86753812012-02-13 13:24:02 +0200509static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100510{
511 /* TODO */
512 return 0;
513}
514
Heikki Krogerus144713f2012-02-13 13:24:05 +0200515static int ab8500_usb_set_peripheral(struct usb_otg *otg,
516 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100517{
518 struct ab8500_usb *ab;
519
520 if (!otg)
521 return -ENODEV;
522
Heikki Krogerus144713f2012-02-13 13:24:05 +0200523 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100524
525 /* Some drivers call this function in atomic context.
526 * Do not update ab8500 registers directly till this
527 * is fixed.
528 */
529
530 if (!gadget) {
531 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200532 otg->gadget = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100533 schedule_work(&ab->phy_dis_work);
534 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200535 otg->gadget = gadget;
536 otg->phy->state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100537
538 /* Phy will not be enabled if cable is already
539 * plugged-in. Schedule to enable phy.
540 * Use same delay to avoid any race condition.
541 */
542 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
543 }
544
545 return 0;
546}
547
Heikki Krogerus144713f2012-02-13 13:24:05 +0200548static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100549{
550 struct ab8500_usb *ab;
551
552 if (!otg)
553 return -ENODEV;
554
Heikki Krogerus144713f2012-02-13 13:24:05 +0200555 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100556
557 /* Some drivers call this function in atomic context.
558 * Do not update ab8500 registers directly till this
559 * is fixed.
560 */
561
562 if (!host) {
563 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200564 otg->host = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100565 schedule_work(&ab->phy_dis_work);
566 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200567 otg->host = host;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100568 /* Phy will not be enabled if cable is already
569 * plugged-in. Schedule to enable phy.
570 * Use same delay to avoid any race condition.
571 */
572 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
573 }
574
575 return 0;
576}
577
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800578static int ab8500_usb_irq_setup(struct platform_device *pdev,
579 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100580{
581 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800582 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100583
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800584 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
585 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100586 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800587 return irq;
588 }
589 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
590 ab8500_usb_link_status_irq,
591 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
592 if (err < 0) {
593 dev_err(ab->dev, "request_irq failed for link status irq\n");
594 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100595 }
596
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800597 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
598 if (irq < 0) {
599 dev_err(&pdev->dev, "ID fall irq not found\n");
600 return irq;
601 }
602 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
603 ab8500_usb_disconnect_irq,
604 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100605 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800606 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
607 return err;
608 }
609
610 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
611 if (irq < 0) {
612 dev_err(&pdev->dev, "VBUS fall irq not found\n");
613 return irq;
614 }
615 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
616 ab8500_usb_disconnect_irq,
617 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
618 if (err < 0) {
619 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100620 return err;
621 }
622
623 return 0;
624}
625
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500626static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100627{
628 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800629 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200630 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100631 int err;
632 int rev;
633
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800634 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100635 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800636
637 if (is_ab8500_1p1_or_earlier(ab8500)) {
638 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100639 return -ENODEV;
640 }
641
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200642 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100643 if (!ab)
644 return -ENOMEM;
645
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200646 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
647 if (!otg)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200648 return -ENOMEM;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200649
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100650 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800651 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200652 ab->phy.dev = ab->dev;
653 ab->phy.otg = otg;
654 ab->phy.label = "ab8500";
655 ab->phy.set_suspend = ab8500_usb_set_suspend;
656 ab->phy.set_power = ab8500_usb_set_power;
657 ab->phy.state = OTG_STATE_UNDEFINED;
658
659 otg->phy = &ab->phy;
660 otg->set_host = ab8500_usb_set_host;
661 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100662
663 platform_set_drvdata(pdev, ab);
664
Heikki Krogerus144713f2012-02-13 13:24:05 +0200665 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100666
667 /* v1: Wait for link status to become stable.
668 * all: Updates form set_host and set_peripheral as they are atomic.
669 */
670 INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
671
672 /* all: Disable phy when called from set_host and set_peripheral */
673 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
674
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800675 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100676 if (err < 0)
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200677 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100678
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530679 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100680 if (err) {
681 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieri81ef6722013-04-03 10:45:03 +0200682 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100683 }
684
Sakethram Bommisetti71246312013-04-03 10:45:04 +0200685 /* Phy tuning values for AB8500 */
686 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
687 /* Enable the PBT/Bank 0x12 access */
688 err = abx500_set_register_interruptible(ab->dev,
689 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
690 if (err < 0)
691 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
692 err);
693
694 err = abx500_set_register_interruptible(ab->dev,
695 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
696 if (err < 0)
697 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
698 err);
699
700 err = abx500_set_register_interruptible(ab->dev,
701 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
702 if (err < 0)
703 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
704 err);
705
706 err = abx500_set_register_interruptible(ab->dev,
707 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
708 if (err < 0)
709 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
710 err);
711
712 /* Switch to normal mode/disable Bank 0x12 access */
713 err = abx500_set_register_interruptible(ab->dev,
714 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
715 if (err < 0)
716 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
717 err);
718 }
719
720 /* Phy tuning values for AB8505 */
721 if (is_ab8505(ab->ab8500)) {
722 /* Enable the PBT/Bank 0x12 access */
723 err = abx500_mask_and_set_register_interruptible(ab->dev,
724 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
725 0x01, 0x01);
726 if (err < 0)
727 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
728 err);
729
730 err = abx500_mask_and_set_register_interruptible(ab->dev,
731 AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
732 0xC8, 0xC8);
733 if (err < 0)
734 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
735 err);
736
737 err = abx500_mask_and_set_register_interruptible(ab->dev,
738 AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
739 0x60, 0x60);
740 if (err < 0)
741 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
742 err);
743
744 err = abx500_mask_and_set_register_interruptible(ab->dev,
745 AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
746 0xFC, 0x80);
747
748 if (err < 0)
749 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n",
750 err);
751
752 /* Switch to normal mode/disable Bank 0x12 access */
753 err = abx500_mask_and_set_register_interruptible(ab->dev,
754 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
755 0x00, 0x00);
756 if (err < 0)
757 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
758 err);
759 }
760
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800761 /* Needed to enable ID detection. */
762 ab8500_usb_wd_workaround(ab);
763
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800764 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100765
766 return 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100767}
768
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500769static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100770{
771 struct ab8500_usb *ab = platform_get_drvdata(pdev);
772
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100773 cancel_delayed_work_sync(&ab->dwork);
774
775 cancel_work_sync(&ab->phy_dis_work);
776
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530777 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100778
779 ab8500_usb_host_phy_dis(ab);
780 ab8500_usb_peri_phy_dis(ab);
781
782 platform_set_drvdata(pdev, NULL);
783
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100784 return 0;
785}
786
787static struct platform_driver ab8500_usb_driver = {
788 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500789 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100790 .driver = {
791 .name = "ab8500-usb",
792 .owner = THIS_MODULE,
793 },
794};
795
796static int __init ab8500_usb_init(void)
797{
798 return platform_driver_register(&ab8500_usb_driver);
799}
800subsys_initcall(ab8500_usb_init);
801
802static void __exit ab8500_usb_exit(void)
803{
804 platform_driver_unregister(&ab8500_usb_driver);
805}
806module_exit(ab8500_usb_exit);
807
808MODULE_ALIAS("platform:ab8500_usb");
809MODULE_AUTHOR("ST-Ericsson AB");
810MODULE_DESCRIPTION("AB8500 usb transceiver driver");
811MODULE_LICENSE("GPL");