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