blob: 351b0369a6112a0626bd0e209f34ba34fca8b0ec [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
36#define AB8500_MAIN_WD_CTRL_REG 0x01
37#define AB8500_USB_LINE_STAT_REG 0x80
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080038#define AB8505_USB_LINE_STAT_REG 0x94
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010039#define AB8500_USB_PHY_CTRL_REG 0x8A
40
41#define AB8500_BIT_OTG_STAT_ID (1 << 0)
42#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
43#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
44#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
45#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
46
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010047#define AB8500_WD_KICK_DELAY_US 100 /* usec */
48#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080049#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +010050
51/* Usb line status register */
52enum ab8500_usb_link_status {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080053 USB_LINK_NOT_CONFIGURED_8500 = 0,
54 USB_LINK_STD_HOST_NC_8500,
55 USB_LINK_STD_HOST_C_NS_8500,
56 USB_LINK_STD_HOST_C_S_8500,
57 USB_LINK_HOST_CHG_NM_8500,
58 USB_LINK_HOST_CHG_HS_8500,
59 USB_LINK_HOST_CHG_HS_CHIRP_8500,
60 USB_LINK_DEDICATED_CHG_8500,
61 USB_LINK_ACA_RID_A_8500,
62 USB_LINK_ACA_RID_B_8500,
63 USB_LINK_ACA_RID_C_NM_8500,
64 USB_LINK_ACA_RID_C_HS_8500,
65 USB_LINK_ACA_RID_C_HS_CHIRP_8500,
66 USB_LINK_HM_IDGND_8500,
67 USB_LINK_RESERVED_8500,
68 USB_LINK_NOT_VALID_LINK_8500,
69};
70
71enum ab8505_usb_link_status {
72 USB_LINK_NOT_CONFIGURED_8505 = 0,
73 USB_LINK_STD_HOST_NC_8505,
74 USB_LINK_STD_HOST_C_NS_8505,
75 USB_LINK_STD_HOST_C_S_8505,
76 USB_LINK_CDP_8505,
77 USB_LINK_RESERVED0_8505,
78 USB_LINK_RESERVED1_8505,
79 USB_LINK_DEDICATED_CHG_8505,
80 USB_LINK_ACA_RID_A_8505,
81 USB_LINK_ACA_RID_B_8505,
82 USB_LINK_ACA_RID_C_NM_8505,
83 USB_LINK_RESERVED2_8505,
84 USB_LINK_RESERVED3_8505,
85 USB_LINK_HM_IDGND_8505,
86 USB_LINK_CHARGERPORT_NOT_OK_8505,
87 USB_LINK_CHARGER_DM_HIGH_8505,
88 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
89 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
90 USB_LINK_STD_UPSTREAM_8505,
91 USB_LINK_CHARGER_SE1_8505,
92 USB_LINK_CARKIT_CHGR_1_8505,
93 USB_LINK_CARKIT_CHGR_2_8505,
94 USB_LINK_ACA_DOCK_CHGR_8505,
95 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
96 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
97 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
98 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
99 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
100};
101
102enum ab8500_usb_mode {
103 USB_IDLE = 0,
104 USB_PERIPHERAL,
105 USB_HOST,
106 USB_DEDICATED_CHG
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100107};
108
109struct ab8500_usb {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200110 struct usb_phy phy;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100111 struct device *dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800112 struct ab8500 *ab8500;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100113 unsigned vbus_draw;
114 struct delayed_work dwork;
115 struct work_struct phy_dis_work;
116 unsigned long link_status_wait;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800117 enum ab8500_usb_mode mode;
118 int previous_link_status_state;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100119};
120
Heikki Krogerus144713f2012-02-13 13:24:05 +0200121static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100122{
Heikki Krogerus144713f2012-02-13 13:24:05 +0200123 return container_of(x, struct ab8500_usb, phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100124}
125
126static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
127{
128 abx500_set_register_interruptible(ab->dev,
129 AB8500_SYS_CTRL2_BLOCK,
130 AB8500_MAIN_WD_CTRL_REG,
131 AB8500_BIT_WD_CTRL_ENABLE);
132
133 udelay(AB8500_WD_KICK_DELAY_US);
134
135 abx500_set_register_interruptible(ab->dev,
136 AB8500_SYS_CTRL2_BLOCK,
137 AB8500_MAIN_WD_CTRL_REG,
138 (AB8500_BIT_WD_CTRL_ENABLE
139 | AB8500_BIT_WD_CTRL_KICK));
140
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800141 udelay(AB8500_WD_V11_DISABLE_DELAY_US);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100142
143 abx500_set_register_interruptible(ab->dev,
144 AB8500_SYS_CTRL2_BLOCK,
145 AB8500_MAIN_WD_CTRL_REG,
146 0);
147}
148
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800149static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
150{
151 /* Workaround for v2.0 bug # 31952 */
152 if (is_ab8500_2p0(ab->ab8500)) {
153 abx500_mask_and_set_register_interruptible(ab->dev,
154 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
155 bit, bit);
156 udelay(AB8500_V20_31952_DISABLE_DELAY_US);
157 }
158}
159
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100160static void ab8500_usb_phy_ctrl(struct ab8500_usb *ab, bool sel_host,
161 bool enable)
162{
163 u8 ctrl_reg;
164 abx500_get_register_interruptible(ab->dev,
165 AB8500_USB,
166 AB8500_USB_PHY_CTRL_REG,
167 &ctrl_reg);
168 if (sel_host) {
169 if (enable)
170 ctrl_reg |= AB8500_BIT_PHY_CTRL_HOST_EN;
171 else
172 ctrl_reg &= ~AB8500_BIT_PHY_CTRL_HOST_EN;
173 } else {
174 if (enable)
175 ctrl_reg |= AB8500_BIT_PHY_CTRL_DEVICE_EN;
176 else
177 ctrl_reg &= ~AB8500_BIT_PHY_CTRL_DEVICE_EN;
178 }
179
180 abx500_set_register_interruptible(ab->dev,
181 AB8500_USB,
182 AB8500_USB_PHY_CTRL_REG,
183 ctrl_reg);
184
185 /* Needed to enable the phy.*/
186 if (enable)
187 ab8500_usb_wd_workaround(ab);
188}
189
190#define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_ctrl(ab, true, true)
191#define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_ctrl(ab, true, false)
192#define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_ctrl(ab, false, true)
193#define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_ctrl(ab, false, false)
194
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800195static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
196 enum ab8505_usb_link_status lsts)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100197{
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800198 enum ux500_musb_vbus_id_status event = 0;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100199
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800200 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100201
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800202 /*
203 * Spurious link_status interrupts are seen at the time of
204 * disconnection of a device in RIDA state
205 */
206 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
207 (lsts == USB_LINK_STD_HOST_NC_8505))
208 return 0;
209
210 ab->previous_link_status_state = lsts;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100211
212 switch (lsts) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800213 case USB_LINK_ACA_RID_B_8505:
214 event = UX500_MUSB_RIDB;
215 case USB_LINK_NOT_CONFIGURED_8505:
216 case USB_LINK_RESERVED0_8505:
217 case USB_LINK_RESERVED1_8505:
218 case USB_LINK_RESERVED2_8505:
219 case USB_LINK_RESERVED3_8505:
220 ab->mode = USB_IDLE;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200221 ab->phy.otg->default_a = false;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100222 ab->vbus_draw = 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800223 if (event != UX500_MUSB_RIDB)
224 event = UX500_MUSB_NONE;
225 /*
226 * Fallback to default B_IDLE as nothing
227 * is connected
228 */
229 ab->phy.state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100230 break;
231
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800232 case USB_LINK_ACA_RID_C_NM_8505:
233 event = UX500_MUSB_RIDC;
234 case USB_LINK_STD_HOST_NC_8505:
235 case USB_LINK_STD_HOST_C_NS_8505:
236 case USB_LINK_STD_HOST_C_S_8505:
237 case USB_LINK_CDP_8505:
238 if (ab->mode == USB_IDLE) {
239 ab->mode = USB_PERIPHERAL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100240 ab8500_usb_peri_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800241 atomic_notifier_call_chain(&ab->phy.notifier,
242 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100243 }
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800244 if (event != UX500_MUSB_RIDC)
245 event = UX500_MUSB_VBUS;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100246 break;
247
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800248 case USB_LINK_ACA_RID_A_8505:
249 case USB_LINK_ACA_DOCK_CHGR_8505:
250 event = UX500_MUSB_RIDA;
251 case USB_LINK_HM_IDGND_8505:
252 if (ab->mode == USB_IDLE) {
253 ab->mode = USB_HOST;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100254 ab8500_usb_host_phy_en(ab);
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800255 atomic_notifier_call_chain(&ab->phy.notifier,
256 UX500_MUSB_PREPARE, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100257 }
Heikki Krogerus144713f2012-02-13 13:24:05 +0200258 ab->phy.otg->default_a = true;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800259 if (event != UX500_MUSB_RIDA)
260 event = UX500_MUSB_ID;
261 atomic_notifier_call_chain(&ab->phy.notifier,
262 event, &ab->vbus_draw);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100263 break;
264
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800265 case USB_LINK_DEDICATED_CHG_8505:
266 ab->mode = USB_DEDICATED_CHG;
267 event = UX500_MUSB_CHARGER;
268 atomic_notifier_call_chain(&ab->phy.notifier,
269 event, &ab->vbus_draw);
270 break;
271
272 default:
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100273 break;
274 }
275
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800276 return 0;
277}
278
279static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
280 enum ab8500_usb_link_status lsts)
281{
282 enum ux500_musb_vbus_id_status event = 0;
283
284 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
285
286 /*
287 * Spurious link_status interrupts are seen in case of a
288 * disconnection of a device in IDGND and RIDA stage
289 */
290 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
291 (lsts == USB_LINK_STD_HOST_C_NS_8500 ||
292 lsts == USB_LINK_STD_HOST_NC_8500))
293 return 0;
294
295 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
296 lsts == USB_LINK_STD_HOST_NC_8500)
297 return 0;
298
299 ab->previous_link_status_state = lsts;
300
301 switch (lsts) {
302 case USB_LINK_ACA_RID_B_8500:
303 event = UX500_MUSB_RIDB;
304 case USB_LINK_NOT_CONFIGURED_8500:
305 case USB_LINK_NOT_VALID_LINK_8500:
306 ab->mode = USB_IDLE;
307 ab->phy.otg->default_a = false;
308 ab->vbus_draw = 0;
309 if (event != UX500_MUSB_RIDB)
310 event = UX500_MUSB_NONE;
311 /* Fallback to default B_IDLE as nothing is connected */
312 ab->phy.state = OTG_STATE_B_IDLE;
313 break;
314
315 case USB_LINK_ACA_RID_C_NM_8500:
316 case USB_LINK_ACA_RID_C_HS_8500:
317 case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
318 event = UX500_MUSB_RIDC;
319 case USB_LINK_STD_HOST_NC_8500:
320 case USB_LINK_STD_HOST_C_NS_8500:
321 case USB_LINK_STD_HOST_C_S_8500:
322 case USB_LINK_HOST_CHG_NM_8500:
323 case USB_LINK_HOST_CHG_HS_8500:
324 case USB_LINK_HOST_CHG_HS_CHIRP_8500:
325 if (ab->mode == USB_IDLE) {
326 ab->mode = USB_PERIPHERAL;
327 ab8500_usb_peri_phy_en(ab);
328 atomic_notifier_call_chain(&ab->phy.notifier,
329 UX500_MUSB_PREPARE, &ab->vbus_draw);
330 }
331 if (event != UX500_MUSB_RIDC)
332 event = UX500_MUSB_VBUS;
333 break;
334
335 case USB_LINK_ACA_RID_A_8500:
336 event = UX500_MUSB_RIDA;
337 case USB_LINK_HM_IDGND_8500:
338 if (ab->mode == USB_IDLE) {
339 ab->mode = USB_HOST;
340 ab8500_usb_host_phy_en(ab);
341 atomic_notifier_call_chain(&ab->phy.notifier,
342 UX500_MUSB_PREPARE, &ab->vbus_draw);
343 }
344 ab->phy.otg->default_a = true;
345 if (event != UX500_MUSB_RIDA)
346 event = UX500_MUSB_ID;
347 atomic_notifier_call_chain(&ab->phy.notifier,
348 event, &ab->vbus_draw);
349 break;
350
351 case USB_LINK_DEDICATED_CHG_8500:
352 ab->mode = USB_DEDICATED_CHG;
353 event = UX500_MUSB_CHARGER;
354 atomic_notifier_call_chain(&ab->phy.notifier,
355 event, &ab->vbus_draw);
356 break;
357
358 case USB_LINK_RESERVED_8500:
359 break;
360 }
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100361
362 return 0;
363}
364
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800365/*
366 * Connection Sequence:
367 * 1. Link Status Interrupt
368 * 2. Enable AB clock
369 * 3. Enable AB regulators
370 * 4. Enable USB phy
371 * 5. Reset the musb controller
372 * 6. Switch the ULPI GPIO pins to fucntion mode
373 * 7. Enable the musb Peripheral5 clock
374 * 8. Restore MUSB context
375 */
376static int abx500_usb_link_status_update(struct ab8500_usb *ab)
377{
378 u8 reg;
379 int ret = 0;
380
381 if (is_ab8500(ab->ab8500)) {
382 enum ab8500_usb_link_status lsts;
383
384 abx500_get_register_interruptible(ab->dev,
385 AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
386 lsts = (reg >> 3) & 0x0F;
387 ret = ab8500_usb_link_status_update(ab, lsts);
388 } else if (is_ab8505(ab->ab8500)) {
389 enum ab8505_usb_link_status lsts;
390
391 abx500_get_register_interruptible(ab->dev,
392 AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
393 lsts = (reg >> 3) & 0x1F;
394 ret = ab8505_usb_link_status_update(ab, lsts);
395 }
396
397 return ret;
398}
399
400/*
401 * Disconnection Sequence:
402 * 1. Disconect Interrupt
403 * 2. Disable regulators
404 * 3. Disable AB clock
405 * 4. Disable the Phy
406 * 5. Link Status Interrupt
407 * 6. Disable Musb Clock
408 */
409static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
410{
411 struct ab8500_usb *ab = (struct ab8500_usb *) data;
412 enum usb_phy_events event = UX500_MUSB_NONE;
413
414 /* Link status will not be updated till phy is disabled. */
415 if (ab->mode == USB_HOST) {
416 ab->phy.otg->default_a = false;
417 ab->vbus_draw = 0;
418 atomic_notifier_call_chain(&ab->phy.notifier,
419 event, &ab->vbus_draw);
420 ab8500_usb_host_phy_dis(ab);
421 ab->mode = USB_IDLE;
422 }
423
424 if (ab->mode == USB_PERIPHERAL) {
425 atomic_notifier_call_chain(&ab->phy.notifier,
426 event, &ab->vbus_draw);
427 ab8500_usb_peri_phy_dis(ab);
428 atomic_notifier_call_chain(&ab->phy.notifier,
429 UX500_MUSB_CLEAN, &ab->vbus_draw);
430 ab->mode = USB_IDLE;
431 ab->phy.otg->default_a = false;
432 ab->vbus_draw = 0;
433 }
434
435 if (is_ab8500_2p0(ab->ab8500)) {
436 if (ab->mode == USB_DEDICATED_CHG) {
437 ab8500_usb_wd_linkstatus(ab,
438 AB8500_BIT_PHY_CTRL_DEVICE_EN);
439 abx500_mask_and_set_register_interruptible(ab->dev,
440 AB8500_USB, AB8500_USB_PHY_CTRL_REG,
441 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
442 }
443 }
444
445 return IRQ_HANDLED;
446}
447
448static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
449{
450 struct ab8500_usb *ab = (struct ab8500_usb *) data;
451
452 abx500_usb_link_status_update(ab);
453
454 return IRQ_HANDLED;
455}
456
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100457static void ab8500_usb_delayed_work(struct work_struct *work)
458{
459 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
460 dwork.work);
461
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800462 abx500_usb_link_status_update(ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100463}
464
465static void ab8500_usb_phy_disable_work(struct work_struct *work)
466{
467 struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
468 phy_dis_work);
469
Heikki Krogerus144713f2012-02-13 13:24:05 +0200470 if (!ab->phy.otg->host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100471 ab8500_usb_host_phy_dis(ab);
472
Heikki Krogerus144713f2012-02-13 13:24:05 +0200473 if (!ab->phy.otg->gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100474 ab8500_usb_peri_phy_dis(ab);
475}
476
Heikki Krogerus144713f2012-02-13 13:24:05 +0200477static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100478{
479 struct ab8500_usb *ab;
480
Heikki Krogerus144713f2012-02-13 13:24:05 +0200481 if (!phy)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100482 return -ENODEV;
483
Heikki Krogerus144713f2012-02-13 13:24:05 +0200484 ab = phy_to_ab(phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100485
486 ab->vbus_draw = mA;
487
488 if (mA)
Heikki Krogerus144713f2012-02-13 13:24:05 +0200489 atomic_notifier_call_chain(&ab->phy.notifier,
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800490 UX500_MUSB_ENUMERATED, ab->phy.otg->gadget);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100491 return 0;
492}
493
494/* TODO: Implement some way for charging or other drivers to read
495 * ab->vbus_draw.
496 */
497
Heikki Krogerus86753812012-02-13 13:24:02 +0200498static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100499{
500 /* TODO */
501 return 0;
502}
503
Heikki Krogerus144713f2012-02-13 13:24:05 +0200504static int ab8500_usb_set_peripheral(struct usb_otg *otg,
505 struct usb_gadget *gadget)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100506{
507 struct ab8500_usb *ab;
508
509 if (!otg)
510 return -ENODEV;
511
Heikki Krogerus144713f2012-02-13 13:24:05 +0200512 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100513
514 /* Some drivers call this function in atomic context.
515 * Do not update ab8500 registers directly till this
516 * is fixed.
517 */
518
519 if (!gadget) {
520 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200521 otg->gadget = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100522 schedule_work(&ab->phy_dis_work);
523 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200524 otg->gadget = gadget;
525 otg->phy->state = OTG_STATE_B_IDLE;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100526
527 /* Phy will not be enabled if cable is already
528 * plugged-in. Schedule to enable phy.
529 * Use same delay to avoid any race condition.
530 */
531 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
532 }
533
534 return 0;
535}
536
Heikki Krogerus144713f2012-02-13 13:24:05 +0200537static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100538{
539 struct ab8500_usb *ab;
540
541 if (!otg)
542 return -ENODEV;
543
Heikki Krogerus144713f2012-02-13 13:24:05 +0200544 ab = phy_to_ab(otg->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100545
546 /* Some drivers call this function in atomic context.
547 * Do not update ab8500 registers directly till this
548 * is fixed.
549 */
550
551 if (!host) {
552 /* TODO: Disable regulators. */
Heikki Krogerus144713f2012-02-13 13:24:05 +0200553 otg->host = NULL;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100554 schedule_work(&ab->phy_dis_work);
555 } else {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200556 otg->host = host;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100557 /* Phy will not be enabled if cable is already
558 * plugged-in. Schedule to enable phy.
559 * Use same delay to avoid any race condition.
560 */
561 schedule_delayed_work(&ab->dwork, ab->link_status_wait);
562 }
563
564 return 0;
565}
566
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800567static int ab8500_usb_irq_setup(struct platform_device *pdev,
568 struct ab8500_usb *ab)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100569{
570 int err;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800571 int irq;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100572
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800573 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
574 if (irq < 0) {
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100575 dev_err(&pdev->dev, "Link status irq not found\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800576 return irq;
577 }
578 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
579 ab8500_usb_link_status_irq,
580 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab);
581 if (err < 0) {
582 dev_err(ab->dev, "request_irq failed for link status irq\n");
583 return err;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100584 }
585
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800586 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
587 if (irq < 0) {
588 dev_err(&pdev->dev, "ID fall irq not found\n");
589 return irq;
590 }
591 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
592 ab8500_usb_disconnect_irq,
593 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100594 if (err < 0) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800595 dev_err(ab->dev, "request_irq failed for ID fall irq\n");
596 return err;
597 }
598
599 irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
600 if (irq < 0) {
601 dev_err(&pdev->dev, "VBUS fall irq not found\n");
602 return irq;
603 }
604 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
605 ab8500_usb_disconnect_irq,
606 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab);
607 if (err < 0) {
608 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100609 return err;
610 }
611
612 return 0;
613}
614
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500615static int ab8500_usb_probe(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100616{
617 struct ab8500_usb *ab;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800618 struct ab8500 *ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200619 struct usb_otg *otg;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100620 int err;
621 int rev;
622
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800623 ab8500 = dev_get_drvdata(pdev->dev.parent);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100624 rev = abx500_get_chip_id(&pdev->dev);
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800625
626 if (is_ab8500_1p1_or_earlier(ab8500)) {
627 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100628 return -ENODEV;
629 }
630
631 ab = kzalloc(sizeof *ab, GFP_KERNEL);
632 if (!ab)
633 return -ENOMEM;
634
Heikki Krogerus144713f2012-02-13 13:24:05 +0200635 otg = kzalloc(sizeof *otg, GFP_KERNEL);
Dan Carpenter5f0b3f92012-03-02 11:08:14 +0300636 if (!otg) {
Heikki Krogerus144713f2012-02-13 13:24:05 +0200637 kfree(ab);
638 return -ENOMEM;
639 }
640
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100641 ab->dev = &pdev->dev;
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800642 ab->ab8500 = ab8500;
Heikki Krogerus144713f2012-02-13 13:24:05 +0200643 ab->phy.dev = ab->dev;
644 ab->phy.otg = otg;
645 ab->phy.label = "ab8500";
646 ab->phy.set_suspend = ab8500_usb_set_suspend;
647 ab->phy.set_power = ab8500_usb_set_power;
648 ab->phy.state = OTG_STATE_UNDEFINED;
649
650 otg->phy = &ab->phy;
651 otg->set_host = ab8500_usb_set_host;
652 otg->set_peripheral = ab8500_usb_set_peripheral;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100653
654 platform_set_drvdata(pdev, ab);
655
Heikki Krogerus144713f2012-02-13 13:24:05 +0200656 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100657
658 /* v1: Wait for link status to become stable.
659 * all: Updates form set_host and set_peripheral as they are atomic.
660 */
661 INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);
662
663 /* all: Disable phy when called from set_host and set_peripheral */
664 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
665
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800666 err = ab8500_usb_irq_setup(pdev, ab);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100667 if (err < 0)
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800668 goto fail;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100669
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530670 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100671 if (err) {
672 dev_err(&pdev->dev, "Can't register transceiver\n");
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800673 goto fail;
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100674 }
675
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800676 /* Needed to enable ID detection. */
677 ab8500_usb_wd_workaround(ab);
678
Fabio Baltieri73f226c2013-03-08 10:27:08 +0800679 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100680
681 return 0;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800682fail:
Heikki Krogerus144713f2012-02-13 13:24:05 +0200683 kfree(otg);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100684 kfree(ab);
685 return err;
686}
687
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500688static int ab8500_usb_remove(struct platform_device *pdev)
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100689{
690 struct ab8500_usb *ab = platform_get_drvdata(pdev);
691
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100692 cancel_delayed_work_sync(&ab->dwork);
693
694 cancel_work_sync(&ab->phy_dis_work);
695
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530696 usb_remove_phy(&ab->phy);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100697
698 ab8500_usb_host_phy_dis(ab);
699 ab8500_usb_peri_phy_dis(ab);
700
701 platform_set_drvdata(pdev, NULL);
702
Heikki Krogerus144713f2012-02-13 13:24:05 +0200703 kfree(ab->phy.otg);
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100704 kfree(ab);
705
706 return 0;
707}
708
709static struct platform_driver ab8500_usb_driver = {
710 .probe = ab8500_usb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500711 .remove = ab8500_usb_remove,
Mian Yousaf Kaukab96915232010-12-07 15:00:09 +0100712 .driver = {
713 .name = "ab8500-usb",
714 .owner = THIS_MODULE,
715 },
716};
717
718static int __init ab8500_usb_init(void)
719{
720 return platform_driver_register(&ab8500_usb_driver);
721}
722subsys_initcall(ab8500_usb_init);
723
724static void __exit ab8500_usb_exit(void)
725{
726 platform_driver_unregister(&ab8500_usb_driver);
727}
728module_exit(ab8500_usb_exit);
729
730MODULE_ALIAS("platform:ab8500_usb");
731MODULE_AUTHOR("ST-Ericsson AB");
732MODULE_DESCRIPTION("AB8500 usb transceiver driver");
733MODULE_LICENSE("GPL");