Linux Audio

Check our new training course

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