blob: 67d00d0ef621c875bfe9fd4360c34835157bea31 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * USB GPIO Based Connection Detection Driver
4 *
5 * Copyright (C) 2019 MediaTek Inc.
6 *
7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
8 *
9 * Some code borrowed from drivers/extcon/extcon-usb-gpio.c
10 */
11
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>
17#include <linux/of.h>
18#include <linux/pinctrl/consumer.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/consumer.h>
21#include <linux/usb/role.h>
22
23#define USB_GPIO_DEB_MS 20 /* ms */
24#define USB_GPIO_DEB_US ((USB_GPIO_DEB_MS) * 1000) /* us */
25
26#define USB_CONN_IRQF \
27 (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT)
28
29struct usb_conn_info {
30 struct device *dev;
31 struct usb_role_switch *role_sw;
32 enum usb_role last_role;
33 struct regulator *vbus;
34 struct delayed_work dw_det;
35 unsigned long debounce_jiffies;
36
37 struct gpio_desc *id_gpiod;
38 struct gpio_desc *vbus_gpiod;
39 int id_irq;
40 int vbus_irq;
41 bool initial_detection;
42};
43
44/**
45 * "DEVICE" = VBUS and "HOST" = !ID, so we have:
46 * Both "DEVICE" and "HOST" can't be set as active at the same time
47 * so if "HOST" is active (i.e. ID is 0) we keep "DEVICE" inactive
48 * even if VBUS is on.
49 *
50 * Role | ID | VBUS
51 * ------------------------------------
52 * [1] DEVICE | H | H
53 * [2] NONE | H | L
54 * [3] HOST | L | H
55 * [4] HOST | L | L
56 *
57 * In case we have only one of these signals:
58 * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1
59 * - ID only - we want to distinguish between [1] and [4], so VBUS = ID
60 */
61static void usb_conn_detect_cable(struct work_struct *work)
62{
63 struct usb_conn_info *info;
64 enum usb_role role;
65 int id, vbus, ret;
66
67 info = container_of(to_delayed_work(work),
68 struct usb_conn_info, dw_det);
69
70 /* check ID and VBUS */
71 id = info->id_gpiod ?
72 gpiod_get_value_cansleep(info->id_gpiod) : 1;
73 vbus = info->vbus_gpiod ?
74 gpiod_get_value_cansleep(info->vbus_gpiod) : id;
75
76 if (!id)
77 role = USB_ROLE_HOST;
78 else if (vbus)
79 role = USB_ROLE_DEVICE;
80 else
81 role = USB_ROLE_NONE;
82
83 dev_dbg(info->dev, "role %d/%d, gpios: id %d, vbus %d\n",
84 info->last_role, role, id, vbus);
85
86 if (!info->initial_detection && info->last_role == role) {
87 dev_warn(info->dev, "repeated role: %d\n", role);
88 return;
89 }
90
91 info->initial_detection = false;
92
93 if (info->last_role == USB_ROLE_HOST)
94 regulator_disable(info->vbus);
95
96 ret = usb_role_switch_set_role(info->role_sw, role);
97 if (ret)
98 dev_err(info->dev, "failed to set role: %d\n", ret);
99
100 if (role == USB_ROLE_HOST) {
101 ret = regulator_enable(info->vbus);
102 if (ret)
103 dev_err(info->dev, "enable vbus regulator failed\n");
104 }
105
106 info->last_role = role;
107
108 dev_dbg(info->dev, "vbus regulator is %s\n",
109 regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
110}
111
112static void usb_conn_queue_dwork(struct usb_conn_info *info,
113 unsigned long delay)
114{
115 queue_delayed_work(system_power_efficient_wq, &info->dw_det, delay);
116}
117
118static irqreturn_t usb_conn_isr(int irq, void *dev_id)
119{
120 struct usb_conn_info *info = dev_id;
121
122 usb_conn_queue_dwork(info, info->debounce_jiffies);
123
124 return IRQ_HANDLED;
125}
126
127static int usb_conn_probe(struct platform_device *pdev)
128{
129 struct device *dev = &pdev->dev;
130 struct usb_conn_info *info;
131 int ret = 0;
132
133 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
134 if (!info)
135 return -ENOMEM;
136
137 info->dev = dev;
138 info->id_gpiod = devm_gpiod_get_optional(dev, "id", GPIOD_IN);
139 if (IS_ERR(info->id_gpiod))
140 return PTR_ERR(info->id_gpiod);
141
142 info->vbus_gpiod = devm_gpiod_get_optional(dev, "vbus", GPIOD_IN);
143 if (IS_ERR(info->vbus_gpiod))
144 return PTR_ERR(info->vbus_gpiod);
145
146 if (!info->id_gpiod && !info->vbus_gpiod) {
147 dev_err(dev, "failed to get gpios\n");
148 return -ENODEV;
149 }
150
151 if (info->id_gpiod)
152 ret = gpiod_set_debounce(info->id_gpiod, USB_GPIO_DEB_US);
153 if (!ret && info->vbus_gpiod)
154 ret = gpiod_set_debounce(info->vbus_gpiod, USB_GPIO_DEB_US);
155 if (ret < 0)
156 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEB_MS);
157
158 INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
159
160 info->vbus = devm_regulator_get(dev, "vbus");
161 if (IS_ERR(info->vbus)) {
162 if (PTR_ERR(info->vbus) != -EPROBE_DEFER)
163 dev_err(dev, "failed to get vbus\n");
164 return PTR_ERR(info->vbus);
165 }
166
167 info->role_sw = usb_role_switch_get(dev);
168 if (IS_ERR(info->role_sw)) {
169 if (PTR_ERR(info->role_sw) != -EPROBE_DEFER)
170 dev_err(dev, "failed to get role switch\n");
171
172 return PTR_ERR(info->role_sw);
173 }
174
175 if (info->id_gpiod) {
176 info->id_irq = gpiod_to_irq(info->id_gpiod);
177 if (info->id_irq < 0) {
178 dev_err(dev, "failed to get ID IRQ\n");
179 ret = info->id_irq;
180 goto put_role_sw;
181 }
182
183 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
184 usb_conn_isr, USB_CONN_IRQF,
185 pdev->name, info);
186 if (ret < 0) {
187 dev_err(dev, "failed to request ID IRQ\n");
188 goto put_role_sw;
189 }
190 }
191
192 if (info->vbus_gpiod) {
193 info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
194 if (info->vbus_irq < 0) {
195 dev_err(dev, "failed to get VBUS IRQ\n");
196 ret = info->vbus_irq;
197 goto put_role_sw;
198 }
199
200 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
201 usb_conn_isr, USB_CONN_IRQF,
202 pdev->name, info);
203 if (ret < 0) {
204 dev_err(dev, "failed to request VBUS IRQ\n");
205 goto put_role_sw;
206 }
207 }
208
209 platform_set_drvdata(pdev, info);
210
211 /* Perform initial detection */
212 info->initial_detection = true;
213 usb_conn_queue_dwork(info, 0);
214
215 return 0;
216
217put_role_sw:
218 usb_role_switch_put(info->role_sw);
219 return ret;
220}
221
222static int usb_conn_remove(struct platform_device *pdev)
223{
224 struct usb_conn_info *info = platform_get_drvdata(pdev);
225
226 cancel_delayed_work_sync(&info->dw_det);
227
228 if (info->last_role == USB_ROLE_HOST)
229 regulator_disable(info->vbus);
230
231 usb_role_switch_put(info->role_sw);
232
233 return 0;
234}
235
236static int __maybe_unused usb_conn_suspend(struct device *dev)
237{
238 struct usb_conn_info *info = dev_get_drvdata(dev);
239
240 if (info->id_gpiod)
241 disable_irq(info->id_irq);
242 if (info->vbus_gpiod)
243 disable_irq(info->vbus_irq);
244
245 pinctrl_pm_select_sleep_state(dev);
246
247 return 0;
248}
249
250static int __maybe_unused usb_conn_resume(struct device *dev)
251{
252 struct usb_conn_info *info = dev_get_drvdata(dev);
253
254 pinctrl_pm_select_default_state(dev);
255
256 if (info->id_gpiod)
257 enable_irq(info->id_irq);
258 if (info->vbus_gpiod)
259 enable_irq(info->vbus_irq);
260
261 usb_conn_queue_dwork(info, 0);
262
263 return 0;
264}
265
266static SIMPLE_DEV_PM_OPS(usb_conn_pm_ops,
267 usb_conn_suspend, usb_conn_resume);
268
269static const struct of_device_id usb_conn_dt_match[] = {
270 { .compatible = "gpio-usb-b-connector", },
271 { }
272};
273MODULE_DEVICE_TABLE(of, usb_conn_dt_match);
274
275static struct platform_driver usb_conn_driver = {
276 .probe = usb_conn_probe,
277 .remove = usb_conn_remove,
278 .driver = {
279 .name = "usb-conn-gpio",
280 .pm = &usb_conn_pm_ops,
281 .of_match_table = usb_conn_dt_match,
282 },
283};
284
285module_platform_driver(usb_conn_driver);
286
287MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
288MODULE_DESCRIPTION("USB GPIO based connection detection driver");
289MODULE_LICENSE("GPL v2");