1 // 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/power_supply.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/usb/role.h>
23 #include <linux/idr.h>
24
25 static DEFINE_IDA(usb_conn_ida);
26
27 #define USB_GPIO_DEB_MS 20 /* ms */
28 #define USB_GPIO_DEB_US ((USB_GPIO_DEB_MS) * 1000) /* us */
29
30 #define USB_CONN_IRQF \
31 (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT)
32
33 struct usb_conn_info {
34 struct device *dev;
35 int conn_id; /* store the IDA-allocated ID */
36 struct usb_role_switch *role_sw;
37 enum usb_role last_role;
38 struct regulator *vbus;
39 struct delayed_work dw_det;
40 unsigned long debounce_jiffies;
41
42 struct gpio_desc *id_gpiod;
43 struct gpio_desc *vbus_gpiod;
44 int id_irq;
45 int vbus_irq;
46
47 struct power_supply_desc desc;
48 struct power_supply *charger;
49 bool initial_detection;
50 };
51
52 /*
53 * "DEVICE" = VBUS and "HOST" = !ID, so we have:
54 * Both "DEVICE" and "HOST" can't be set as active at the same time
55 * so if "HOST" is active (i.e. ID is 0) we keep "DEVICE" inactive
56 * even if VBUS is on.
57 *
58 * Role | ID | VBUS
59 * ------------------------------------
60 * [1] DEVICE | H | H
61 * [2] NONE | H | L
62 * [3] HOST | L | H
63 * [4] HOST | L | L
64 *
65 * In case we have only one of these signals:
66 * - VBUS only - we want to distinguish between [1] and [2], so ID is always 1
67 * - ID only - we want to distinguish between [1] and [4], so VBUS = ID
68 */
usb_conn_detect_cable(struct work_struct * work)69 static void usb_conn_detect_cable(struct work_struct *work)
70 {
71 struct usb_conn_info *info;
72 enum usb_role role;
73 int id, vbus, ret;
74
75 info = container_of(to_delayed_work(work),
76 struct usb_conn_info, dw_det);
77
78 /* check ID and VBUS */
79 id = info->id_gpiod ?
80 gpiod_get_value_cansleep(info->id_gpiod) : 1;
81 vbus = info->vbus_gpiod ?
82 gpiod_get_value_cansleep(info->vbus_gpiod) : id;
83
84 if (!id)
85 role = USB_ROLE_HOST;
86 else if (vbus)
87 role = USB_ROLE_DEVICE;
88 else
89 role = USB_ROLE_NONE;
90
91 dev_dbg(info->dev, "role %s -> %s, gpios: id %d, vbus %d\n",
92 usb_role_string(info->last_role), usb_role_string(role), id, vbus);
93
94 if (!info->initial_detection && info->last_role == role) {
95 dev_warn(info->dev, "repeated role: %s\n", usb_role_string(role));
96 return;
97 }
98
99 info->initial_detection = false;
100
101 if (info->last_role == USB_ROLE_HOST && info->vbus)
102 regulator_disable(info->vbus);
103
104 ret = usb_role_switch_set_role(info->role_sw, role);
105 if (ret)
106 dev_err(info->dev, "failed to set role: %d\n", ret);
107
108 if (role == USB_ROLE_HOST && info->vbus) {
109 ret = regulator_enable(info->vbus);
110 if (ret)
111 dev_err(info->dev, "enable vbus regulator failed\n");
112 }
113
114 info->last_role = role;
115
116 if (info->vbus)
117 dev_dbg(info->dev, "vbus regulator is %s\n",
118 regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
119
120 power_supply_changed(info->charger);
121 }
122
usb_conn_queue_dwork(struct usb_conn_info * info,unsigned long delay)123 static void usb_conn_queue_dwork(struct usb_conn_info *info,
124 unsigned long delay)
125 {
126 queue_delayed_work(system_power_efficient_wq, &info->dw_det, delay);
127 }
128
usb_conn_isr(int irq,void * dev_id)129 static irqreturn_t usb_conn_isr(int irq, void *dev_id)
130 {
131 struct usb_conn_info *info = dev_id;
132
133 usb_conn_queue_dwork(info, info->debounce_jiffies);
134
135 return IRQ_HANDLED;
136 }
137
138 static enum power_supply_property usb_charger_properties[] = {
139 POWER_SUPPLY_PROP_ONLINE,
140 };
141
usb_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)142 static int usb_charger_get_property(struct power_supply *psy,
143 enum power_supply_property psp,
144 union power_supply_propval *val)
145 {
146 struct usb_conn_info *info = power_supply_get_drvdata(psy);
147
148 switch (psp) {
149 case POWER_SUPPLY_PROP_ONLINE:
150 val->intval = info->last_role == USB_ROLE_DEVICE;
151 break;
152 default:
153 return -EINVAL;
154 }
155
156 return 0;
157 }
158
usb_conn_psy_register(struct usb_conn_info * info)159 static int usb_conn_psy_register(struct usb_conn_info *info)
160 {
161 struct device *dev = info->dev;
162 struct power_supply_desc *desc = &info->desc;
163 struct power_supply_config cfg = {
164 .of_node = dev->of_node,
165 };
166
167 info->conn_id = ida_alloc(&usb_conn_ida, GFP_KERNEL);
168 if (info->conn_id < 0)
169 return info->conn_id;
170
171 desc->name = devm_kasprintf(dev, GFP_KERNEL, "usb-charger-%d",
172 info->conn_id);
173 if (!desc->name) {
174 ida_free(&usb_conn_ida, info->conn_id);
175 return -ENOMEM;
176 }
177
178 desc->properties = usb_charger_properties;
179 desc->num_properties = ARRAY_SIZE(usb_charger_properties);
180 desc->get_property = usb_charger_get_property;
181 desc->type = POWER_SUPPLY_TYPE_USB;
182 cfg.drv_data = info;
183
184 info->charger = devm_power_supply_register(dev, desc, &cfg);
185 if (IS_ERR(info->charger)) {
186 dev_err(dev, "Unable to register charger %d\n", info->conn_id);
187 ida_free(&usb_conn_ida, info->conn_id);
188 }
189
190 return PTR_ERR_OR_ZERO(info->charger);
191 }
192
usb_conn_probe(struct platform_device * pdev)193 static int usb_conn_probe(struct platform_device *pdev)
194 {
195 struct device *dev = &pdev->dev;
196 struct usb_conn_info *info;
197 int ret = 0;
198
199 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
200 if (!info)
201 return -ENOMEM;
202
203 info->dev = dev;
204 info->id_gpiod = devm_gpiod_get_optional(dev, "id", GPIOD_IN);
205 if (IS_ERR(info->id_gpiod))
206 return PTR_ERR(info->id_gpiod);
207
208 info->vbus_gpiod = devm_gpiod_get_optional(dev, "vbus", GPIOD_IN);
209 if (IS_ERR(info->vbus_gpiod))
210 return PTR_ERR(info->vbus_gpiod);
211
212 if (!info->id_gpiod && !info->vbus_gpiod) {
213 dev_err(dev, "failed to get gpios\n");
214 return -ENODEV;
215 }
216
217 if (info->id_gpiod)
218 ret = gpiod_set_debounce(info->id_gpiod, USB_GPIO_DEB_US);
219 if (!ret && info->vbus_gpiod)
220 ret = gpiod_set_debounce(info->vbus_gpiod, USB_GPIO_DEB_US);
221 if (ret < 0)
222 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEB_MS);
223
224 INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
225
226 info->vbus = devm_regulator_get_optional(dev, "vbus");
227 if (PTR_ERR(info->vbus) == -ENODEV)
228 info->vbus = NULL;
229
230 if (IS_ERR(info->vbus))
231 return dev_err_probe(dev, PTR_ERR(info->vbus), "failed to get vbus\n");
232
233 info->role_sw = usb_role_switch_get(dev);
234 if (IS_ERR(info->role_sw))
235 return dev_err_probe(dev, PTR_ERR(info->role_sw),
236 "failed to get role switch\n");
237
238 ret = usb_conn_psy_register(info);
239 if (ret)
240 goto put_role_sw;
241
242 if (info->id_gpiod) {
243 info->id_irq = gpiod_to_irq(info->id_gpiod);
244 if (info->id_irq < 0) {
245 dev_err(dev, "failed to get ID IRQ\n");
246 ret = info->id_irq;
247 goto put_role_sw;
248 }
249
250 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
251 usb_conn_isr, USB_CONN_IRQF,
252 pdev->name, info);
253 if (ret < 0) {
254 dev_err(dev, "failed to request ID IRQ\n");
255 goto put_role_sw;
256 }
257 }
258
259 if (info->vbus_gpiod) {
260 info->vbus_irq = gpiod_to_irq(info->vbus_gpiod);
261 if (info->vbus_irq < 0) {
262 dev_err(dev, "failed to get VBUS IRQ\n");
263 ret = info->vbus_irq;
264 goto put_role_sw;
265 }
266
267 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
268 usb_conn_isr, USB_CONN_IRQF,
269 pdev->name, info);
270 if (ret < 0) {
271 dev_err(dev, "failed to request VBUS IRQ\n");
272 goto put_role_sw;
273 }
274 }
275
276 platform_set_drvdata(pdev, info);
277 device_set_wakeup_capable(&pdev->dev, true);
278
279 /* Perform initial detection */
280 info->initial_detection = true;
281 usb_conn_queue_dwork(info, 0);
282
283 return 0;
284
285 put_role_sw:
286 usb_role_switch_put(info->role_sw);
287 return ret;
288 }
289
usb_conn_remove(struct platform_device * pdev)290 static void usb_conn_remove(struct platform_device *pdev)
291 {
292 struct usb_conn_info *info = platform_get_drvdata(pdev);
293
294 cancel_delayed_work_sync(&info->dw_det);
295
296 if (info->charger)
297 ida_free(&usb_conn_ida, info->conn_id);
298
299 if (info->last_role == USB_ROLE_HOST && info->vbus)
300 regulator_disable(info->vbus);
301
302 usb_role_switch_put(info->role_sw);
303 }
304
usb_conn_suspend(struct device * dev)305 static int __maybe_unused usb_conn_suspend(struct device *dev)
306 {
307 struct usb_conn_info *info = dev_get_drvdata(dev);
308
309 if (device_may_wakeup(dev)) {
310 if (info->id_gpiod)
311 enable_irq_wake(info->id_irq);
312 if (info->vbus_gpiod)
313 enable_irq_wake(info->vbus_irq);
314 return 0;
315 }
316
317 if (info->id_gpiod)
318 disable_irq(info->id_irq);
319 if (info->vbus_gpiod)
320 disable_irq(info->vbus_irq);
321
322 pinctrl_pm_select_sleep_state(dev);
323
324 return 0;
325 }
326
usb_conn_resume(struct device * dev)327 static int __maybe_unused usb_conn_resume(struct device *dev)
328 {
329 struct usb_conn_info *info = dev_get_drvdata(dev);
330
331 if (device_may_wakeup(dev)) {
332 if (info->id_gpiod)
333 disable_irq_wake(info->id_irq);
334 if (info->vbus_gpiod)
335 disable_irq_wake(info->vbus_irq);
336 return 0;
337 }
338
339 pinctrl_pm_select_default_state(dev);
340
341 if (info->id_gpiod)
342 enable_irq(info->id_irq);
343 if (info->vbus_gpiod)
344 enable_irq(info->vbus_irq);
345
346 usb_conn_queue_dwork(info, 0);
347
348 return 0;
349 }
350
351 static SIMPLE_DEV_PM_OPS(usb_conn_pm_ops,
352 usb_conn_suspend, usb_conn_resume);
353
354 static const struct of_device_id usb_conn_dt_match[] = {
355 { .compatible = "gpio-usb-b-connector", },
356 { }
357 };
358 MODULE_DEVICE_TABLE(of, usb_conn_dt_match);
359
360 static struct platform_driver usb_conn_driver = {
361 .probe = usb_conn_probe,
362 .remove_new = usb_conn_remove,
363 .driver = {
364 .name = "usb-conn-gpio",
365 .pm = &usb_conn_pm_ops,
366 .of_match_table = usb_conn_dt_match,
367 },
368 };
369
370 module_platform_driver(usb_conn_driver);
371
372 MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
373 MODULE_DESCRIPTION("USB GPIO based connection detection driver");
374 MODULE_LICENSE("GPL v2");
375