2 * Palmas USB transceiver driver
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Author: Graeme Gregory <gg@slimlogic.co.uk>
11 * Author: Kishon Vijay Abraham I <kishon@ti.com>
13 * Based on twl6030_usb.c
15 * Author: Hema HK <hemahk@ti.com>
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/err.h>
28 #include <linux/mfd/palmas.h>
30 #include <linux/of_platform.h>
32 static const char *palmas_extcon_cable
[] = {
38 static const int mutually_exclusive
[] = {0x3, 0x0};
40 static void palmas_usb_wakeup(struct palmas
*palmas
, int enable
)
43 palmas_write(palmas
, PALMAS_USB_OTG_BASE
, PALMAS_USB_WAKEUP
,
44 PALMAS_USB_WAKEUP_ID_WK_UP_COMP
);
46 palmas_write(palmas
, PALMAS_USB_OTG_BASE
, PALMAS_USB_WAKEUP
, 0);
49 static irqreturn_t
palmas_vbus_irq_handler(int irq
, void *_palmas_usb
)
51 struct palmas_usb
*palmas_usb
= _palmas_usb
;
52 unsigned int vbus_line_state
;
54 palmas_read(palmas_usb
->palmas
, PALMAS_INTERRUPT_BASE
,
55 PALMAS_INT3_LINE_STATE
, &vbus_line_state
);
57 if (vbus_line_state
& PALMAS_INT3_LINE_STATE_VBUS
) {
58 if (palmas_usb
->linkstat
!= PALMAS_USB_STATE_VBUS
) {
59 palmas_usb
->linkstat
= PALMAS_USB_STATE_VBUS
;
60 extcon_set_cable_state(palmas_usb
->edev
, "USB", true);
61 dev_info(palmas_usb
->dev
, "USB cable is attached\n");
63 dev_dbg(palmas_usb
->dev
,
64 "Spurious connect event detected\n");
66 } else if (!(vbus_line_state
& PALMAS_INT3_LINE_STATE_VBUS
)) {
67 if (palmas_usb
->linkstat
== PALMAS_USB_STATE_VBUS
) {
68 palmas_usb
->linkstat
= PALMAS_USB_STATE_DISCONNECT
;
69 extcon_set_cable_state(palmas_usb
->edev
, "USB", false);
70 dev_info(palmas_usb
->dev
, "USB cable is detached\n");
72 dev_dbg(palmas_usb
->dev
,
73 "Spurious disconnect event detected\n");
80 static irqreturn_t
palmas_id_irq_handler(int irq
, void *_palmas_usb
)
82 unsigned int set
, id_src
;
83 struct palmas_usb
*palmas_usb
= _palmas_usb
;
85 palmas_read(palmas_usb
->palmas
, PALMAS_USB_OTG_BASE
,
86 PALMAS_USB_ID_INT_LATCH_SET
, &set
);
87 palmas_read(palmas_usb
->palmas
, PALMAS_USB_OTG_BASE
,
88 PALMAS_USB_ID_INT_SRC
, &id_src
);
90 if ((set
& PALMAS_USB_ID_INT_SRC_ID_GND
) &&
91 (id_src
& PALMAS_USB_ID_INT_SRC_ID_GND
)) {
92 palmas_write(palmas_usb
->palmas
, PALMAS_USB_OTG_BASE
,
93 PALMAS_USB_ID_INT_LATCH_CLR
,
94 PALMAS_USB_ID_INT_EN_HI_CLR_ID_GND
);
95 palmas_usb
->linkstat
= PALMAS_USB_STATE_ID
;
96 extcon_set_cable_state(palmas_usb
->edev
, "USB-HOST", true);
97 dev_info(palmas_usb
->dev
, "USB-HOST cable is attached\n");
98 } else if ((set
& PALMAS_USB_ID_INT_SRC_ID_FLOAT
) &&
99 (id_src
& PALMAS_USB_ID_INT_SRC_ID_FLOAT
)) {
100 palmas_write(palmas_usb
->palmas
, PALMAS_USB_OTG_BASE
,
101 PALMAS_USB_ID_INT_LATCH_CLR
,
102 PALMAS_USB_ID_INT_EN_HI_CLR_ID_FLOAT
);
103 palmas_usb
->linkstat
= PALMAS_USB_STATE_DISCONNECT
;
104 extcon_set_cable_state(palmas_usb
->edev
, "USB-HOST", false);
105 dev_info(palmas_usb
->dev
, "USB-HOST cable is detached\n");
106 } else if ((palmas_usb
->linkstat
== PALMAS_USB_STATE_ID
) &&
107 (!(set
& PALMAS_USB_ID_INT_SRC_ID_GND
))) {
108 palmas_usb
->linkstat
= PALMAS_USB_STATE_DISCONNECT
;
109 extcon_set_cable_state(palmas_usb
->edev
, "USB-HOST", false);
110 dev_info(palmas_usb
->dev
, "USB-HOST cable is detached\n");
111 } else if ((palmas_usb
->linkstat
== PALMAS_USB_STATE_DISCONNECT
) &&
112 (id_src
& PALMAS_USB_ID_INT_SRC_ID_GND
)) {
113 palmas_usb
->linkstat
= PALMAS_USB_STATE_ID
;
114 extcon_set_cable_state(palmas_usb
->edev
, "USB-HOST", true);
115 dev_info(palmas_usb
->dev
, " USB-HOST cable is attached\n");
121 static void palmas_enable_irq(struct palmas_usb
*palmas_usb
)
123 palmas_write(palmas_usb
->palmas
, PALMAS_USB_OTG_BASE
,
124 PALMAS_USB_VBUS_CTRL_SET
,
125 PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP
);
127 palmas_write(palmas_usb
->palmas
, PALMAS_USB_OTG_BASE
,
128 PALMAS_USB_ID_CTRL_SET
, PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP
);
130 palmas_write(palmas_usb
->palmas
, PALMAS_USB_OTG_BASE
,
131 PALMAS_USB_ID_INT_EN_HI_SET
,
132 PALMAS_USB_ID_INT_EN_HI_SET_ID_GND
|
133 PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT
);
135 if (palmas_usb
->enable_vbus_detection
)
136 palmas_vbus_irq_handler(palmas_usb
->vbus_irq
, palmas_usb
);
138 /* cold plug for host mode needs this delay */
139 if (palmas_usb
->enable_id_detection
) {
141 palmas_id_irq_handler(palmas_usb
->id_irq
, palmas_usb
);
145 static int palmas_usb_probe(struct platform_device
*pdev
)
147 struct palmas
*palmas
= dev_get_drvdata(pdev
->dev
.parent
);
148 struct palmas_usb_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
149 struct device_node
*node
= pdev
->dev
.of_node
;
150 struct palmas_usb
*palmas_usb
;
154 dev_err(&pdev
->dev
, "failed to get valid parent\n");
158 palmas_usb
= devm_kzalloc(&pdev
->dev
, sizeof(*palmas_usb
), GFP_KERNEL
);
162 if (node
&& !pdata
) {
163 palmas_usb
->wakeup
= of_property_read_bool(node
, "ti,wakeup");
164 palmas_usb
->enable_id_detection
= of_property_read_bool(node
,
165 "ti,enable-id-detection");
166 palmas_usb
->enable_vbus_detection
= of_property_read_bool(node
,
167 "ti,enable-vbus-detection");
169 palmas_usb
->wakeup
= true;
170 palmas_usb
->enable_id_detection
= true;
171 palmas_usb
->enable_vbus_detection
= true;
174 palmas_usb
->wakeup
= pdata
->wakeup
;
177 palmas
->usb
= palmas_usb
;
178 palmas_usb
->palmas
= palmas
;
180 palmas_usb
->dev
= &pdev
->dev
;
182 palmas_usb
->id_otg_irq
= regmap_irq_get_virq(palmas
->irq_data
,
184 palmas_usb
->id_irq
= regmap_irq_get_virq(palmas
->irq_data
,
186 palmas_usb
->vbus_otg_irq
= regmap_irq_get_virq(palmas
->irq_data
,
187 PALMAS_VBUS_OTG_IRQ
);
188 palmas_usb
->vbus_irq
= regmap_irq_get_virq(palmas
->irq_data
,
191 palmas_usb_wakeup(palmas
, palmas_usb
->wakeup
);
193 platform_set_drvdata(pdev
, palmas_usb
);
195 palmas_usb
->edev
= devm_extcon_dev_allocate(&pdev
->dev
,
196 palmas_extcon_cable
);
197 if (IS_ERR(palmas_usb
->edev
)) {
198 dev_err(&pdev
->dev
, "failed to allocate extcon device\n");
201 palmas_usb
->edev
->name
= kstrdup(node
->name
, GFP_KERNEL
);
202 palmas_usb
->edev
->mutually_exclusive
= mutually_exclusive
;
204 status
= devm_extcon_dev_register(&pdev
->dev
, palmas_usb
->edev
);
206 dev_err(&pdev
->dev
, "failed to register extcon device\n");
207 kfree(palmas_usb
->edev
->name
);
211 if (palmas_usb
->enable_id_detection
) {
212 status
= devm_request_threaded_irq(palmas_usb
->dev
,
214 NULL
, palmas_id_irq_handler
,
215 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
|
216 IRQF_ONESHOT
| IRQF_EARLY_RESUME
,
217 "palmas_usb_id", palmas_usb
);
219 dev_err(&pdev
->dev
, "can't get IRQ %d, err %d\n",
220 palmas_usb
->id_irq
, status
);
221 kfree(palmas_usb
->edev
->name
);
226 if (palmas_usb
->enable_vbus_detection
) {
227 status
= devm_request_threaded_irq(palmas_usb
->dev
,
228 palmas_usb
->vbus_irq
, NULL
,
229 palmas_vbus_irq_handler
,
230 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
|
231 IRQF_ONESHOT
| IRQF_EARLY_RESUME
,
232 "palmas_usb_vbus", palmas_usb
);
234 dev_err(&pdev
->dev
, "can't get IRQ %d, err %d\n",
235 palmas_usb
->vbus_irq
, status
);
236 kfree(palmas_usb
->edev
->name
);
241 palmas_enable_irq(palmas_usb
);
242 device_set_wakeup_capable(&pdev
->dev
, true);
246 static int palmas_usb_remove(struct platform_device
*pdev
)
248 struct palmas_usb
*palmas_usb
= platform_get_drvdata(pdev
);
250 kfree(palmas_usb
->edev
->name
);
255 #ifdef CONFIG_PM_SLEEP
256 static int palmas_usb_suspend(struct device
*dev
)
258 struct palmas_usb
*palmas_usb
= dev_get_drvdata(dev
);
260 if (device_may_wakeup(dev
)) {
261 if (palmas_usb
->enable_vbus_detection
)
262 enable_irq_wake(palmas_usb
->vbus_irq
);
263 if (palmas_usb
->enable_id_detection
)
264 enable_irq_wake(palmas_usb
->id_irq
);
269 static int palmas_usb_resume(struct device
*dev
)
271 struct palmas_usb
*palmas_usb
= dev_get_drvdata(dev
);
273 if (device_may_wakeup(dev
)) {
274 if (palmas_usb
->enable_vbus_detection
)
275 disable_irq_wake(palmas_usb
->vbus_irq
);
276 if (palmas_usb
->enable_id_detection
)
277 disable_irq_wake(palmas_usb
->id_irq
);
283 static SIMPLE_DEV_PM_OPS(palmas_pm_ops
, palmas_usb_suspend
, palmas_usb_resume
);
285 static const struct of_device_id of_palmas_match_tbl
[] = {
286 { .compatible
= "ti,palmas-usb", },
287 { .compatible
= "ti,palmas-usb-vid", },
288 { .compatible
= "ti,twl6035-usb", },
289 { .compatible
= "ti,twl6035-usb-vid", },
293 static struct platform_driver palmas_usb_driver
= {
294 .probe
= palmas_usb_probe
,
295 .remove
= palmas_usb_remove
,
297 .name
= "palmas-usb",
298 .of_match_table
= of_palmas_match_tbl
,
299 .owner
= THIS_MODULE
,
300 .pm
= &palmas_pm_ops
,
304 module_platform_driver(palmas_usb_driver
);
306 MODULE_ALIAS("platform:palmas-usb");
307 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
308 MODULE_DESCRIPTION("Palmas USB transceiver driver");
309 MODULE_LICENSE("GPL");
310 MODULE_DEVICE_TABLE(of
, of_palmas_match_tbl
);