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/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
71 struct otg_transceiver otg
;
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
*xceiv_to_ab(struct otg_transceiver
*x
)
87 return container_of(x
, struct ab8500_usb
, otg
);
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_xceiv_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
->otg
.state
= OTG_STATE_B_IDLE
;
173 ab
->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
->otg
.gadget
) {
185 /* TODO: Enable regulators. */
186 ab8500_usb_peri_phy_en(ab
);
189 event
= USB_EVENT_VBUS
;
192 case USB_LINK_HM_IDGND
:
194 /* TODO: Enable regulators. */
195 ab8500_usb_host_phy_en(ab
);
198 ab
->otg
.state
= OTG_STATE_A_IDLE
;
199 ab
->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 blocking_notifier_call_chain(&ab
->otg
.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
,
266 ab8500_usb_host_phy_dis(ab
);
269 ab8500_usb_peri_phy_dis(ab
);
272 static int ab8500_usb_set_power(struct otg_transceiver
*otg
, unsigned mA
)
274 struct ab8500_usb
*ab
;
279 ab
= xceiv_to_ab(otg
);
284 blocking_notifier_call_chain(&ab
->otg
.notifier
,
285 USB_EVENT_ENUMERATED
, ab
->otg
.gadget
);
289 /* TODO: Implement some way for charging or other drivers to read
293 static int ab8500_usb_set_suspend(struct otg_transceiver
*x
, int suspend
)
299 static int ab8500_usb_set_peripheral(struct otg_transceiver
*otg
,
300 struct usb_gadget
*gadget
)
302 struct ab8500_usb
*ab
;
307 ab
= xceiv_to_ab(otg
);
309 /* Some drivers call this function in atomic context.
310 * Do not update ab8500 registers directly till this
315 /* TODO: Disable regulators. */
316 ab
->otg
.gadget
= NULL
;
317 schedule_work(&ab
->phy_dis_work
);
319 ab
->otg
.gadget
= gadget
;
320 ab
->otg
.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 otg_transceiver
*otg
,
333 struct usb_bus
*host
)
335 struct ab8500_usb
*ab
;
340 ab
= xceiv_to_ab(otg
);
342 /* Some drivers call this function in atomic context.
343 * Do not update ab8500 registers directly till this
348 /* TODO: Disable regulators. */
350 schedule_work(&ab
->phy_dis_work
);
353 /* Phy will not be enabled if cable is already
354 * plugged-in. Schedule to enable phy.
355 * Use same delay to avoid any race condition.
357 schedule_delayed_work(&ab
->dwork
, ab
->link_status_wait
);
363 static void ab8500_usb_irq_free(struct ab8500_usb
*ab
)
365 if (ab
->rev
< 0x20) {
366 free_irq(ab
->irq_num_id_rise
, ab
);
367 free_irq(ab
->irq_num_id_fall
, ab
);
368 free_irq(ab
->irq_num_vbus_rise
, ab
);
369 free_irq(ab
->irq_num_vbus_fall
, ab
);
371 free_irq(ab
->irq_num_link_status
, ab
);
375 static int ab8500_usb_v1x_res_setup(struct platform_device
*pdev
,
376 struct ab8500_usb
*ab
)
380 ab
->irq_num_id_rise
= platform_get_irq_byname(pdev
, "ID_WAKEUP_R");
381 if (ab
->irq_num_id_rise
< 0) {
382 dev_err(&pdev
->dev
, "ID rise irq not found\n");
383 return ab
->irq_num_id_rise
;
385 err
= request_threaded_irq(ab
->irq_num_id_rise
, NULL
,
386 ab8500_usb_v1x_common_irq
,
387 IRQF_NO_SUSPEND
| IRQF_SHARED
,
390 dev_err(ab
->dev
, "request_irq failed for ID rise irq\n");
394 ab
->irq_num_id_fall
= platform_get_irq_byname(pdev
, "ID_WAKEUP_F");
395 if (ab
->irq_num_id_fall
< 0) {
396 dev_err(&pdev
->dev
, "ID fall irq not found\n");
397 return ab
->irq_num_id_fall
;
399 err
= request_threaded_irq(ab
->irq_num_id_fall
, NULL
,
400 ab8500_usb_v1x_common_irq
,
401 IRQF_NO_SUSPEND
| IRQF_SHARED
,
404 dev_err(ab
->dev
, "request_irq failed for ID fall irq\n");
408 ab
->irq_num_vbus_rise
= platform_get_irq_byname(pdev
, "VBUS_DET_R");
409 if (ab
->irq_num_vbus_rise
< 0) {
410 dev_err(&pdev
->dev
, "VBUS rise irq not found\n");
411 return ab
->irq_num_vbus_rise
;
413 err
= request_threaded_irq(ab
->irq_num_vbus_rise
, NULL
,
414 ab8500_usb_v1x_common_irq
,
415 IRQF_NO_SUSPEND
| IRQF_SHARED
,
416 "usb-vbus-rise", ab
);
418 dev_err(ab
->dev
, "request_irq failed for Vbus rise irq\n");
422 ab
->irq_num_vbus_fall
= platform_get_irq_byname(pdev
, "VBUS_DET_F");
423 if (ab
->irq_num_vbus_fall
< 0) {
424 dev_err(&pdev
->dev
, "VBUS fall irq not found\n");
425 return ab
->irq_num_vbus_fall
;
427 err
= request_threaded_irq(ab
->irq_num_vbus_fall
, NULL
,
428 ab8500_usb_v1x_vbus_fall_irq
,
429 IRQF_NO_SUSPEND
| IRQF_SHARED
,
430 "usb-vbus-fall", ab
);
432 dev_err(ab
->dev
, "request_irq failed for Vbus fall irq\n");
438 free_irq(ab
->irq_num_vbus_rise
, ab
);
440 free_irq(ab
->irq_num_id_fall
, ab
);
442 free_irq(ab
->irq_num_id_rise
, ab
);
447 static int ab8500_usb_v2_res_setup(struct platform_device
*pdev
,
448 struct ab8500_usb
*ab
)
452 ab
->irq_num_link_status
= platform_get_irq_byname(pdev
,
454 if (ab
->irq_num_link_status
< 0) {
455 dev_err(&pdev
->dev
, "Link status irq not found\n");
456 return ab
->irq_num_link_status
;
459 err
= request_threaded_irq(ab
->irq_num_link_status
, NULL
,
461 IRQF_NO_SUSPEND
| IRQF_SHARED
,
462 "usb-link-status", ab
);
465 "request_irq failed for link status irq\n");
472 static int __devinit
ab8500_usb_probe(struct platform_device
*pdev
)
474 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 ab
->dev
= &pdev
->dev
;
493 ab
->otg
.dev
= ab
->dev
;
494 ab
->otg
.label
= "ab8500";
495 ab
->otg
.state
= OTG_STATE_UNDEFINED
;
496 ab
->otg
.set_host
= ab8500_usb_set_host
;
497 ab
->otg
.set_peripheral
= ab8500_usb_set_peripheral
;
498 ab
->otg
.set_suspend
= ab8500_usb_set_suspend
;
499 ab
->otg
.set_power
= ab8500_usb_set_power
;
501 platform_set_drvdata(pdev
, ab
);
503 BLOCKING_INIT_NOTIFIER_HEAD(&ab
->otg
.notifier
);
505 /* v1: Wait for link status to become stable.
506 * all: Updates form set_host and set_peripheral as they are atomic.
508 INIT_DELAYED_WORK(&ab
->dwork
, ab8500_usb_delayed_work
);
510 /* all: Disable phy when called from set_host and set_peripheral */
511 INIT_WORK(&ab
->phy_dis_work
, ab8500_usb_phy_disable_work
);
513 if (ab
->rev
< 0x20) {
514 err
= ab8500_usb_v1x_res_setup(pdev
, ab
);
515 ab
->link_status_wait
= AB8500_V1x_LINK_STAT_WAIT
;
517 err
= ab8500_usb_v2_res_setup(pdev
, ab
);
523 err
= otg_set_transceiver(&ab
->otg
);
525 dev_err(&pdev
->dev
, "Can't register transceiver\n");
529 dev_info(&pdev
->dev
, "AB8500 usb driver initialized\n");
533 ab8500_usb_irq_free(ab
);
539 static int __devexit
ab8500_usb_remove(struct platform_device
*pdev
)
541 struct ab8500_usb
*ab
= platform_get_drvdata(pdev
);
543 ab8500_usb_irq_free(ab
);
545 cancel_delayed_work_sync(&ab
->dwork
);
547 cancel_work_sync(&ab
->phy_dis_work
);
549 otg_set_transceiver(NULL
);
551 ab8500_usb_host_phy_dis(ab
);
552 ab8500_usb_peri_phy_dis(ab
);
554 platform_set_drvdata(pdev
, NULL
);
561 static struct platform_driver ab8500_usb_driver
= {
562 .probe
= ab8500_usb_probe
,
563 .remove
= __devexit_p(ab8500_usb_remove
),
565 .name
= "ab8500-usb",
566 .owner
= THIS_MODULE
,
570 static int __init
ab8500_usb_init(void)
572 return platform_driver_register(&ab8500_usb_driver
);
574 subsys_initcall(ab8500_usb_init
);
576 static void __exit
ab8500_usb_exit(void)
578 platform_driver_unregister(&ab8500_usb_driver
);
580 module_exit(ab8500_usb_exit
);
582 MODULE_ALIAS("platform:ab8500_usb");
583 MODULE_AUTHOR("ST-Ericsson AB");
584 MODULE_DESCRIPTION("AB8500 usb transceiver driver");
585 MODULE_LICENSE("GPL");