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