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