1 // SPDX-License-Identifier: GPL-2.0
3 * USB GPIO Based Connection Detection Driver
5 * Copyright (C) 2019 MediaTek Inc.
7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
9 * Some code borrowed from drivers/extcon/extcon-usb-gpio.c
12 #include <linux/device.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/usb/role.h>
24 #define USB_GPIO_DEB_MS 20 /* ms */
25 #define USB_GPIO_DEB_US ((USB_GPIO_DEB_MS) * 1000) /* us */
27 #define USB_CONN_IRQF \
28 (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT)
30 struct usb_conn_info
{
32 struct usb_role_switch
*role_sw
;
33 enum usb_role last_role
;
34 struct regulator
*vbus
;
35 struct delayed_work dw_det
;
36 unsigned long debounce_jiffies
;
38 struct gpio_desc
*id_gpiod
;
39 struct gpio_desc
*vbus_gpiod
;
43 struct power_supply_desc desc
;
44 struct power_supply
*charger
;
48 * "DEVICE" = VBUS and "HOST" = !ID, so we have:
49 * Both "DEVICE" and "HOST" can't be set as active at the same time
50 * so if "HOST" is active (i.e. ID is 0) we keep "DEVICE" inactive
54 * ------------------------------------
60 * In case we have only one of these signals:
61 * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1
62 * - ID only - we want to distinguish between [1] and [4], so VBUS = ID
64 static void usb_conn_detect_cable(struct work_struct
*work
)
66 struct usb_conn_info
*info
;
70 info
= container_of(to_delayed_work(work
),
71 struct usb_conn_info
, dw_det
);
73 /* check ID and VBUS */
75 gpiod_get_value_cansleep(info
->id_gpiod
) : 1;
76 vbus
= info
->vbus_gpiod
?
77 gpiod_get_value_cansleep(info
->vbus_gpiod
) : id
;
82 role
= USB_ROLE_DEVICE
;
86 dev_dbg(info
->dev
, "role %d/%d, gpios: id %d, vbus %d\n",
87 info
->last_role
, role
, id
, vbus
);
89 if (info
->last_role
== role
) {
90 dev_warn(info
->dev
, "repeated role: %d\n", role
);
94 if (info
->last_role
== USB_ROLE_HOST
&& info
->vbus
)
95 regulator_disable(info
->vbus
);
97 ret
= usb_role_switch_set_role(info
->role_sw
, role
);
99 dev_err(info
->dev
, "failed to set role: %d\n", ret
);
101 if (role
== USB_ROLE_HOST
&& info
->vbus
) {
102 ret
= regulator_enable(info
->vbus
);
104 dev_err(info
->dev
, "enable vbus regulator failed\n");
107 info
->last_role
= role
;
110 dev_dbg(info
->dev
, "vbus regulator is %s\n",
111 regulator_is_enabled(info
->vbus
) ? "enabled" : "disabled");
113 power_supply_changed(info
->charger
);
116 static void usb_conn_queue_dwork(struct usb_conn_info
*info
,
119 queue_delayed_work(system_power_efficient_wq
, &info
->dw_det
, delay
);
122 static irqreturn_t
usb_conn_isr(int irq
, void *dev_id
)
124 struct usb_conn_info
*info
= dev_id
;
126 usb_conn_queue_dwork(info
, info
->debounce_jiffies
);
131 static enum power_supply_property usb_charger_properties
[] = {
132 POWER_SUPPLY_PROP_ONLINE
,
135 static int usb_charger_get_property(struct power_supply
*psy
,
136 enum power_supply_property psp
,
137 union power_supply_propval
*val
)
139 struct usb_conn_info
*info
= power_supply_get_drvdata(psy
);
142 case POWER_SUPPLY_PROP_ONLINE
:
143 val
->intval
= info
->last_role
== USB_ROLE_DEVICE
;
152 static int usb_conn_probe(struct platform_device
*pdev
)
154 struct device
*dev
= &pdev
->dev
;
155 struct power_supply_desc
*desc
;
156 struct usb_conn_info
*info
;
157 struct power_supply_config cfg
= {
158 .of_node
= dev
->of_node
,
160 bool need_vbus
= true;
163 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
168 info
->id_gpiod
= devm_gpiod_get_optional(dev
, "id", GPIOD_IN
);
169 if (IS_ERR(info
->id_gpiod
))
170 return PTR_ERR(info
->id_gpiod
);
172 info
->vbus_gpiod
= devm_gpiod_get_optional(dev
, "vbus", GPIOD_IN
);
173 if (IS_ERR(info
->vbus_gpiod
))
174 return PTR_ERR(info
->vbus_gpiod
);
176 if (!info
->id_gpiod
&& !info
->vbus_gpiod
) {
177 dev_err(dev
, "failed to get gpios\n");
182 ret
= gpiod_set_debounce(info
->id_gpiod
, USB_GPIO_DEB_US
);
183 if (!ret
&& info
->vbus_gpiod
)
184 ret
= gpiod_set_debounce(info
->vbus_gpiod
, USB_GPIO_DEB_US
);
186 info
->debounce_jiffies
= msecs_to_jiffies(USB_GPIO_DEB_MS
);
188 INIT_DELAYED_WORK(&info
->dw_det
, usb_conn_detect_cable
);
191 * If the USB connector is a child of a USB port and that port already provides the VBUS
192 * supply, there's no need for the USB connector to provide it again.
194 if (dev
->parent
&& dev
->parent
->of_node
) {
195 if (of_find_property(dev
->parent
->of_node
, "vbus-supply", NULL
))
200 info
->vbus
= devm_regulator_get_optional(dev
, "vbus");
201 if (PTR_ERR(info
->vbus
) == -ENODEV
)
204 info
->vbus
= devm_regulator_get(dev
, "vbus");
207 if (IS_ERR(info
->vbus
)) {
208 if (PTR_ERR(info
->vbus
) != -EPROBE_DEFER
)
209 dev_err(dev
, "failed to get vbus: %ld\n", PTR_ERR(info
->vbus
));
210 return PTR_ERR(info
->vbus
);
213 info
->role_sw
= usb_role_switch_get(dev
);
214 if (IS_ERR(info
->role_sw
)) {
215 if (PTR_ERR(info
->role_sw
) != -EPROBE_DEFER
)
216 dev_err(dev
, "failed to get role switch\n");
218 return PTR_ERR(info
->role_sw
);
221 if (info
->id_gpiod
) {
222 info
->id_irq
= gpiod_to_irq(info
->id_gpiod
);
223 if (info
->id_irq
< 0) {
224 dev_err(dev
, "failed to get ID IRQ\n");
229 ret
= devm_request_threaded_irq(dev
, info
->id_irq
, NULL
,
230 usb_conn_isr
, USB_CONN_IRQF
,
233 dev_err(dev
, "failed to request ID IRQ\n");
238 if (info
->vbus_gpiod
) {
239 info
->vbus_irq
= gpiod_to_irq(info
->vbus_gpiod
);
240 if (info
->vbus_irq
< 0) {
241 dev_err(dev
, "failed to get VBUS IRQ\n");
242 ret
= info
->vbus_irq
;
246 ret
= devm_request_threaded_irq(dev
, info
->vbus_irq
, NULL
,
247 usb_conn_isr
, USB_CONN_IRQF
,
250 dev_err(dev
, "failed to request VBUS IRQ\n");
256 desc
->name
= "usb-charger";
257 desc
->properties
= usb_charger_properties
;
258 desc
->num_properties
= ARRAY_SIZE(usb_charger_properties
);
259 desc
->get_property
= usb_charger_get_property
;
260 desc
->type
= POWER_SUPPLY_TYPE_USB
;
263 info
->charger
= devm_power_supply_register(dev
, desc
, &cfg
);
264 if (IS_ERR(info
->charger
)) {
265 dev_err(dev
, "Unable to register charger\n");
266 return PTR_ERR(info
->charger
);
269 platform_set_drvdata(pdev
, info
);
271 /* Perform initial detection */
272 usb_conn_queue_dwork(info
, 0);
277 usb_role_switch_put(info
->role_sw
);
281 static int usb_conn_remove(struct platform_device
*pdev
)
283 struct usb_conn_info
*info
= platform_get_drvdata(pdev
);
285 cancel_delayed_work_sync(&info
->dw_det
);
287 if (info
->last_role
== USB_ROLE_HOST
&& info
->vbus
)
288 regulator_disable(info
->vbus
);
290 usb_role_switch_put(info
->role_sw
);
295 static int __maybe_unused
usb_conn_suspend(struct device
*dev
)
297 struct usb_conn_info
*info
= dev_get_drvdata(dev
);
300 disable_irq(info
->id_irq
);
301 if (info
->vbus_gpiod
)
302 disable_irq(info
->vbus_irq
);
304 pinctrl_pm_select_sleep_state(dev
);
309 static int __maybe_unused
usb_conn_resume(struct device
*dev
)
311 struct usb_conn_info
*info
= dev_get_drvdata(dev
);
313 pinctrl_pm_select_default_state(dev
);
316 enable_irq(info
->id_irq
);
317 if (info
->vbus_gpiod
)
318 enable_irq(info
->vbus_irq
);
320 usb_conn_queue_dwork(info
, 0);
325 static SIMPLE_DEV_PM_OPS(usb_conn_pm_ops
,
326 usb_conn_suspend
, usb_conn_resume
);
328 static const struct of_device_id usb_conn_dt_match
[] = {
329 { .compatible
= "gpio-usb-b-connector", },
332 MODULE_DEVICE_TABLE(of
, usb_conn_dt_match
);
334 static struct platform_driver usb_conn_driver
= {
335 .probe
= usb_conn_probe
,
336 .remove
= usb_conn_remove
,
338 .name
= "usb-conn-gpio",
339 .pm
= &usb_conn_pm_ops
,
340 .of_match_table
= usb_conn_dt_match
,
344 module_platform_driver(usb_conn_driver
);
346 MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
347 MODULE_DESCRIPTION("USB GPIO based connection detection driver");
348 MODULE_LICENSE("GPL v2");