2 * drivers/usb/otg/ab8500_usb.c
4 * USB transceiver driver for AB8500 chip
6 * Copyright (C) 2010 ST-Ericsson AB
7 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
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.
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.
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.
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>
33 #include <linux/mfd/abx500/ab8500.h>
35 #define AB8500_MAIN_WD_CTRL_REG 0x01
36 #define AB8500_USB_LINE_STAT_REG 0x80
37 #define AB8500_USB_PHY_CTRL_REG 0x8A
39 #define AB8500_BIT_OTG_STAT_ID (1 << 0)
40 #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
41 #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
42 #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
43 #define AB8500_BIT_WD_CTRL_KICK (1 << 1)
45 #define AB8500_V1x_LINK_STAT_WAIT (HZ/10)
46 #define AB8500_WD_KICK_DELAY_US 100 /* usec */
47 #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
48 #define AB8500_WD_V10_DISABLE_DELAY_MS 100 /* ms */
50 /* Usb line status register */
51 enum ab8500_usb_link_status
{
52 USB_LINK_NOT_CONFIGURED
= 0,
54 USB_LINK_STD_HOST_C_NS
,
55 USB_LINK_STD_HOST_C_S
,
58 USB_LINK_HOST_CHG_HS_CHIRP
,
59 USB_LINK_DEDICATED_CHG
,
62 USB_LINK_ACA_RID_C_NM
,
63 USB_LINK_ACA_RID_C_HS
,
64 USB_LINK_ACA_RID_C_HS_CHIRP
,
67 USB_LINK_NOT_VALID_LINK
75 int irq_num_vbus_rise
;
76 int irq_num_vbus_fall
;
77 int irq_num_link_status
;
79 struct delayed_work dwork
;
80 struct work_struct phy_dis_work
;
81 unsigned long link_status_wait
;
85 static inline struct ab8500_usb
*phy_to_ab(struct usb_phy
*x
)
87 return container_of(x
, struct ab8500_usb
, phy
);
90 static void ab8500_usb_wd_workaround(struct ab8500_usb
*ab
)
92 abx500_set_register_interruptible(ab
->dev
,
93 AB8500_SYS_CTRL2_BLOCK
,
94 AB8500_MAIN_WD_CTRL_REG
,
95 AB8500_BIT_WD_CTRL_ENABLE
);
97 udelay(AB8500_WD_KICK_DELAY_US
);
99 abx500_set_register_interruptible(ab
->dev
,
100 AB8500_SYS_CTRL2_BLOCK
,
101 AB8500_MAIN_WD_CTRL_REG
,
102 (AB8500_BIT_WD_CTRL_ENABLE
103 | AB8500_BIT_WD_CTRL_KICK
));
105 if (ab
->rev
> 0x10) /* v1.1 v2.0 */
106 udelay(AB8500_WD_V11_DISABLE_DELAY_US
);
108 msleep(AB8500_WD_V10_DISABLE_DELAY_MS
);
110 abx500_set_register_interruptible(ab
->dev
,
111 AB8500_SYS_CTRL2_BLOCK
,
112 AB8500_MAIN_WD_CTRL_REG
,
116 static void ab8500_usb_phy_ctrl(struct ab8500_usb
*ab
, bool sel_host
,
120 abx500_get_register_interruptible(ab
->dev
,
122 AB8500_USB_PHY_CTRL_REG
,
126 ctrl_reg
|= AB8500_BIT_PHY_CTRL_HOST_EN
;
128 ctrl_reg
&= ~AB8500_BIT_PHY_CTRL_HOST_EN
;
131 ctrl_reg
|= AB8500_BIT_PHY_CTRL_DEVICE_EN
;
133 ctrl_reg
&= ~AB8500_BIT_PHY_CTRL_DEVICE_EN
;
136 abx500_set_register_interruptible(ab
->dev
,
138 AB8500_USB_PHY_CTRL_REG
,
141 /* Needed to enable the phy.*/
143 ab8500_usb_wd_workaround(ab
);
146 #define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_ctrl(ab, true, true)
147 #define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_ctrl(ab, true, false)
148 #define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_ctrl(ab, false, true)
149 #define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_ctrl(ab, false, false)
151 static int ab8500_usb_link_status_update(struct ab8500_usb
*ab
)
154 enum ab8500_usb_link_status lsts
;
156 enum usb_phy_events event
;
158 abx500_get_register_interruptible(ab
->dev
,
160 AB8500_USB_LINE_STAT_REG
,
163 lsts
= (reg
>> 3) & 0x0F;
166 case USB_LINK_NOT_CONFIGURED
:
167 case USB_LINK_RESERVED
:
168 case USB_LINK_NOT_VALID_LINK
:
169 /* TODO: Disable regulators. */
170 ab8500_usb_host_phy_dis(ab
);
171 ab8500_usb_peri_phy_dis(ab
);
172 ab
->phy
.state
= OTG_STATE_B_IDLE
;
173 ab
->phy
.otg
->default_a
= false;
175 event
= USB_EVENT_NONE
;
178 case USB_LINK_STD_HOST_NC
:
179 case USB_LINK_STD_HOST_C_NS
:
180 case USB_LINK_STD_HOST_C_S
:
181 case USB_LINK_HOST_CHG_NM
:
182 case USB_LINK_HOST_CHG_HS
:
183 case USB_LINK_HOST_CHG_HS_CHIRP
:
184 if (ab
->phy
.otg
->gadget
) {
185 /* TODO: Enable regulators. */
186 ab8500_usb_peri_phy_en(ab
);
187 v
= ab
->phy
.otg
->gadget
;
189 event
= USB_EVENT_VBUS
;
192 case USB_LINK_HM_IDGND
:
193 if (ab
->phy
.otg
->host
) {
194 /* TODO: Enable regulators. */
195 ab8500_usb_host_phy_en(ab
);
196 v
= ab
->phy
.otg
->host
;
198 ab
->phy
.state
= OTG_STATE_A_IDLE
;
199 ab
->phy
.otg
->default_a
= true;
200 event
= USB_EVENT_ID
;
203 case USB_LINK_ACA_RID_A
:
204 case USB_LINK_ACA_RID_B
:
206 case USB_LINK_ACA_RID_C_NM
:
207 case USB_LINK_ACA_RID_C_HS
:
208 case USB_LINK_ACA_RID_C_HS_CHIRP
:
209 case USB_LINK_DEDICATED_CHG
:
210 /* TODO: vbus_draw */
211 event
= USB_EVENT_CHARGER
;
215 atomic_notifier_call_chain(&ab
->phy
.notifier
, event
, v
);
220 static void ab8500_usb_delayed_work(struct work_struct
*work
)
222 struct ab8500_usb
*ab
= container_of(work
, struct ab8500_usb
,
225 ab8500_usb_link_status_update(ab
);
228 static irqreturn_t
ab8500_usb_v1x_common_irq(int irq
, void *data
)
230 struct ab8500_usb
*ab
= (struct ab8500_usb
*) data
;
232 /* Wait for link status to become stable. */
233 schedule_delayed_work(&ab
->dwork
, ab
->link_status_wait
);
238 static irqreturn_t
ab8500_usb_v1x_vbus_fall_irq(int irq
, void *data
)
240 struct ab8500_usb
*ab
= (struct ab8500_usb
*) data
;
242 /* Link status will not be updated till phy is disabled. */
243 ab8500_usb_peri_phy_dis(ab
);
245 /* Wait for link status to become stable. */
246 schedule_delayed_work(&ab
->dwork
, ab
->link_status_wait
);
251 static irqreturn_t
ab8500_usb_v20_irq(int irq
, void *data
)
253 struct ab8500_usb
*ab
= (struct ab8500_usb
*) data
;
255 ab8500_usb_link_status_update(ab
);
260 static void ab8500_usb_phy_disable_work(struct work_struct
*work
)
262 struct ab8500_usb
*ab
= container_of(work
, struct ab8500_usb
,
265 if (!ab
->phy
.otg
->host
)
266 ab8500_usb_host_phy_dis(ab
);
268 if (!ab
->phy
.otg
->gadget
)
269 ab8500_usb_peri_phy_dis(ab
);
272 static int ab8500_usb_set_power(struct usb_phy
*phy
, unsigned mA
)
274 struct ab8500_usb
*ab
;
284 atomic_notifier_call_chain(&ab
->phy
.notifier
,
285 USB_EVENT_ENUMERATED
, ab
->phy
.otg
->gadget
);
289 /* TODO: Implement some way for charging or other drivers to read
293 static int ab8500_usb_set_suspend(struct usb_phy
*x
, int suspend
)
299 static int ab8500_usb_set_peripheral(struct usb_otg
*otg
,
300 struct usb_gadget
*gadget
)
302 struct ab8500_usb
*ab
;
307 ab
= phy_to_ab(otg
->phy
);
309 /* Some drivers call this function in atomic context.
310 * Do not update ab8500 registers directly till this
315 /* TODO: Disable regulators. */
317 schedule_work(&ab
->phy_dis_work
);
319 otg
->gadget
= gadget
;
320 otg
->phy
->state
= OTG_STATE_B_IDLE
;
322 /* Phy will not be enabled if cable is already
323 * plugged-in. Schedule to enable phy.
324 * Use same delay to avoid any race condition.
326 schedule_delayed_work(&ab
->dwork
, ab
->link_status_wait
);
332 static int ab8500_usb_set_host(struct usb_otg
*otg
, struct usb_bus
*host
)
334 struct ab8500_usb
*ab
;
339 ab
= phy_to_ab(otg
->phy
);
341 /* Some drivers call this function in atomic context.
342 * Do not update ab8500 registers directly till this
347 /* TODO: Disable regulators. */
349 schedule_work(&ab
->phy_dis_work
);
352 /* Phy will not be enabled if cable is already
353 * plugged-in. Schedule to enable phy.
354 * Use same delay to avoid any race condition.
356 schedule_delayed_work(&ab
->dwork
, ab
->link_status_wait
);
362 static void ab8500_usb_irq_free(struct ab8500_usb
*ab
)
364 if (ab
->rev
< 0x20) {
365 free_irq(ab
->irq_num_id_rise
, ab
);
366 free_irq(ab
->irq_num_id_fall
, ab
);
367 free_irq(ab
->irq_num_vbus_rise
, ab
);
368 free_irq(ab
->irq_num_vbus_fall
, ab
);
370 free_irq(ab
->irq_num_link_status
, ab
);
374 static int ab8500_usb_v1x_res_setup(struct platform_device
*pdev
,
375 struct ab8500_usb
*ab
)
379 ab
->irq_num_id_rise
= platform_get_irq_byname(pdev
, "ID_WAKEUP_R");
380 if (ab
->irq_num_id_rise
< 0) {
381 dev_err(&pdev
->dev
, "ID rise irq not found\n");
382 return ab
->irq_num_id_rise
;
384 err
= request_threaded_irq(ab
->irq_num_id_rise
, NULL
,
385 ab8500_usb_v1x_common_irq
,
386 IRQF_NO_SUSPEND
| IRQF_SHARED
,
389 dev_err(ab
->dev
, "request_irq failed for ID rise irq\n");
393 ab
->irq_num_id_fall
= platform_get_irq_byname(pdev
, "ID_WAKEUP_F");
394 if (ab
->irq_num_id_fall
< 0) {
395 dev_err(&pdev
->dev
, "ID fall irq not found\n");
396 return ab
->irq_num_id_fall
;
398 err
= request_threaded_irq(ab
->irq_num_id_fall
, NULL
,
399 ab8500_usb_v1x_common_irq
,
400 IRQF_NO_SUSPEND
| IRQF_SHARED
,
403 dev_err(ab
->dev
, "request_irq failed for ID fall irq\n");
407 ab
->irq_num_vbus_rise
= platform_get_irq_byname(pdev
, "VBUS_DET_R");
408 if (ab
->irq_num_vbus_rise
< 0) {
409 dev_err(&pdev
->dev
, "VBUS rise irq not found\n");
410 return ab
->irq_num_vbus_rise
;
412 err
= request_threaded_irq(ab
->irq_num_vbus_rise
, NULL
,
413 ab8500_usb_v1x_common_irq
,
414 IRQF_NO_SUSPEND
| IRQF_SHARED
,
415 "usb-vbus-rise", ab
);
417 dev_err(ab
->dev
, "request_irq failed for Vbus rise irq\n");
421 ab
->irq_num_vbus_fall
= platform_get_irq_byname(pdev
, "VBUS_DET_F");
422 if (ab
->irq_num_vbus_fall
< 0) {
423 dev_err(&pdev
->dev
, "VBUS fall irq not found\n");
424 return ab
->irq_num_vbus_fall
;
426 err
= request_threaded_irq(ab
->irq_num_vbus_fall
, NULL
,
427 ab8500_usb_v1x_vbus_fall_irq
,
428 IRQF_NO_SUSPEND
| IRQF_SHARED
,
429 "usb-vbus-fall", ab
);
431 dev_err(ab
->dev
, "request_irq failed for Vbus fall irq\n");
437 free_irq(ab
->irq_num_vbus_rise
, ab
);
439 free_irq(ab
->irq_num_id_fall
, ab
);
441 free_irq(ab
->irq_num_id_rise
, ab
);
446 static int ab8500_usb_v2_res_setup(struct platform_device
*pdev
,
447 struct ab8500_usb
*ab
)
451 ab
->irq_num_link_status
= platform_get_irq_byname(pdev
,
453 if (ab
->irq_num_link_status
< 0) {
454 dev_err(&pdev
->dev
, "Link status irq not found\n");
455 return ab
->irq_num_link_status
;
458 err
= request_threaded_irq(ab
->irq_num_link_status
, NULL
,
460 IRQF_NO_SUSPEND
| IRQF_SHARED
,
461 "usb-link-status", ab
);
464 "request_irq failed for link status irq\n");
471 static int ab8500_usb_probe(struct platform_device
*pdev
)
473 struct ab8500_usb
*ab
;
478 rev
= abx500_get_chip_id(&pdev
->dev
);
480 dev_err(&pdev
->dev
, "Chip id read failed\n");
482 } else if (rev
< 0x10) {
483 dev_err(&pdev
->dev
, "Unsupported AB8500 chip\n");
487 ab
= kzalloc(sizeof *ab
, GFP_KERNEL
);
491 otg
= kzalloc(sizeof *otg
, GFP_KERNEL
);
497 ab
->dev
= &pdev
->dev
;
499 ab
->phy
.dev
= ab
->dev
;
501 ab
->phy
.label
= "ab8500";
502 ab
->phy
.set_suspend
= ab8500_usb_set_suspend
;
503 ab
->phy
.set_power
= ab8500_usb_set_power
;
504 ab
->phy
.state
= OTG_STATE_UNDEFINED
;
507 otg
->set_host
= ab8500_usb_set_host
;
508 otg
->set_peripheral
= ab8500_usb_set_peripheral
;
510 platform_set_drvdata(pdev
, ab
);
512 ATOMIC_INIT_NOTIFIER_HEAD(&ab
->phy
.notifier
);
514 /* v1: Wait for link status to become stable.
515 * all: Updates form set_host and set_peripheral as they are atomic.
517 INIT_DELAYED_WORK(&ab
->dwork
, ab8500_usb_delayed_work
);
519 /* all: Disable phy when called from set_host and set_peripheral */
520 INIT_WORK(&ab
->phy_dis_work
, ab8500_usb_phy_disable_work
);
522 if (ab
->rev
< 0x20) {
523 err
= ab8500_usb_v1x_res_setup(pdev
, ab
);
524 ab
->link_status_wait
= AB8500_V1x_LINK_STAT_WAIT
;
526 err
= ab8500_usb_v2_res_setup(pdev
, ab
);
532 err
= usb_add_phy(&ab
->phy
, USB_PHY_TYPE_USB2
);
534 dev_err(&pdev
->dev
, "Can't register transceiver\n");
538 dev_info(&pdev
->dev
, "AB8500 usb driver initialized\n");
542 ab8500_usb_irq_free(ab
);
549 static int ab8500_usb_remove(struct platform_device
*pdev
)
551 struct ab8500_usb
*ab
= platform_get_drvdata(pdev
);
553 ab8500_usb_irq_free(ab
);
555 cancel_delayed_work_sync(&ab
->dwork
);
557 cancel_work_sync(&ab
->phy_dis_work
);
559 usb_remove_phy(&ab
->phy
);
561 ab8500_usb_host_phy_dis(ab
);
562 ab8500_usb_peri_phy_dis(ab
);
564 platform_set_drvdata(pdev
, NULL
);
572 static struct platform_driver ab8500_usb_driver
= {
573 .probe
= ab8500_usb_probe
,
574 .remove
= ab8500_usb_remove
,
576 .name
= "ab8500-usb",
577 .owner
= THIS_MODULE
,
581 static int __init
ab8500_usb_init(void)
583 return platform_driver_register(&ab8500_usb_driver
);
585 subsys_initcall(ab8500_usb_init
);
587 static void __exit
ab8500_usb_exit(void)
589 platform_driver_unregister(&ab8500_usb_driver
);
591 module_exit(ab8500_usb_exit
);
593 MODULE_ALIAS("platform:ab8500_usb");
594 MODULE_AUTHOR("ST-Ericsson AB");
595 MODULE_DESCRIPTION("AB8500 usb transceiver driver");
596 MODULE_LICENSE("GPL");