Lines Matching +full:gpio +full:- +full:charger

1 // SPDX-License-Identifier: GPL-2.0
3 * USB GPIO Based Connection Detection Driver
9 * Some code borrowed from drivers/extcon/extcon-usb-gpio.c
13 #include <linux/gpio/consumer.h>
44 struct power_supply *charger; member
54 * ------------------------------------
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
74 id = info->id_gpiod ? in usb_conn_detect_cable()
75 gpiod_get_value_cansleep(info->id_gpiod) : 1; in usb_conn_detect_cable()
76 vbus = info->vbus_gpiod ? in usb_conn_detect_cable()
77 gpiod_get_value_cansleep(info->vbus_gpiod) : id; in usb_conn_detect_cable()
86 dev_dbg(info->dev, "role %d/%d, gpios: id %d, vbus %d\n", in usb_conn_detect_cable()
87 info->last_role, role, id, vbus); in usb_conn_detect_cable()
89 if (info->last_role == role) { in usb_conn_detect_cable()
90 dev_warn(info->dev, "repeated role: %d\n", role); in usb_conn_detect_cable()
94 if (info->last_role == USB_ROLE_HOST && info->vbus) in usb_conn_detect_cable()
95 regulator_disable(info->vbus); in usb_conn_detect_cable()
97 ret = usb_role_switch_set_role(info->role_sw, role); in usb_conn_detect_cable()
99 dev_err(info->dev, "failed to set role: %d\n", ret); in usb_conn_detect_cable()
101 if (role == USB_ROLE_HOST && info->vbus) { in usb_conn_detect_cable()
102 ret = regulator_enable(info->vbus); in usb_conn_detect_cable()
104 dev_err(info->dev, "enable vbus regulator failed\n"); in usb_conn_detect_cable()
107 info->last_role = role; in usb_conn_detect_cable()
109 if (info->vbus) in usb_conn_detect_cable()
110 dev_dbg(info->dev, "vbus regulator is %s\n", in usb_conn_detect_cable()
111 regulator_is_enabled(info->vbus) ? "enabled" : "disabled"); in usb_conn_detect_cable()
113 power_supply_changed(info->charger); in usb_conn_detect_cable()
119 queue_delayed_work(system_power_efficient_wq, &info->dw_det, delay); in usb_conn_queue_dwork()
126 usb_conn_queue_dwork(info, info->debounce_jiffies); in usb_conn_isr()
143 val->intval = info->last_role == USB_ROLE_DEVICE; in usb_charger_get_property()
146 return -EINVAL; in usb_charger_get_property()
154 struct device *dev = &pdev->dev; in usb_conn_probe()
158 .of_node = dev->of_node, in usb_conn_probe()
165 return -ENOMEM; in usb_conn_probe()
167 info->dev = dev; in usb_conn_probe()
168 info->id_gpiod = devm_gpiod_get_optional(dev, "id", GPIOD_IN); in usb_conn_probe()
169 if (IS_ERR(info->id_gpiod)) in usb_conn_probe()
170 return PTR_ERR(info->id_gpiod); in usb_conn_probe()
172 info->vbus_gpiod = devm_gpiod_get_optional(dev, "vbus", GPIOD_IN); in usb_conn_probe()
173 if (IS_ERR(info->vbus_gpiod)) in usb_conn_probe()
174 return PTR_ERR(info->vbus_gpiod); in usb_conn_probe()
176 if (!info->id_gpiod && !info->vbus_gpiod) { in usb_conn_probe()
178 return -ENODEV; in usb_conn_probe()
181 if (info->id_gpiod) in usb_conn_probe()
182 ret = gpiod_set_debounce(info->id_gpiod, USB_GPIO_DEB_US); in usb_conn_probe()
183 if (!ret && info->vbus_gpiod) in usb_conn_probe()
184 ret = gpiod_set_debounce(info->vbus_gpiod, USB_GPIO_DEB_US); in usb_conn_probe()
186 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEB_MS); in usb_conn_probe()
188 INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable); in usb_conn_probe()
194 if (dev->parent && dev->parent->of_node) { in usb_conn_probe()
195 if (of_find_property(dev->parent->of_node, "vbus-supply", NULL)) in usb_conn_probe()
200 info->vbus = devm_regulator_get_optional(dev, "vbus"); in usb_conn_probe()
201 if (PTR_ERR(info->vbus) == -ENODEV) in usb_conn_probe()
202 info->vbus = NULL; in usb_conn_probe()
204 info->vbus = devm_regulator_get(dev, "vbus"); in usb_conn_probe()
207 if (IS_ERR(info->vbus)) { in usb_conn_probe()
208 if (PTR_ERR(info->vbus) != -EPROBE_DEFER) in usb_conn_probe()
209 dev_err(dev, "failed to get vbus: %ld\n", PTR_ERR(info->vbus)); in usb_conn_probe()
210 return PTR_ERR(info->vbus); in usb_conn_probe()
213 info->role_sw = usb_role_switch_get(dev); in usb_conn_probe()
214 if (IS_ERR(info->role_sw)) { in usb_conn_probe()
215 if (PTR_ERR(info->role_sw) != -EPROBE_DEFER) in usb_conn_probe()
218 return PTR_ERR(info->role_sw); in usb_conn_probe()
221 if (info->id_gpiod) { in usb_conn_probe()
222 info->id_irq = gpiod_to_irq(info->id_gpiod); in usb_conn_probe()
223 if (info->id_irq < 0) { in usb_conn_probe()
225 ret = info->id_irq; in usb_conn_probe()
229 ret = devm_request_threaded_irq(dev, info->id_irq, NULL, in usb_conn_probe()
231 pdev->name, info); in usb_conn_probe()
238 if (info->vbus_gpiod) { in usb_conn_probe()
239 info->vbus_irq = gpiod_to_irq(info->vbus_gpiod); in usb_conn_probe()
240 if (info->vbus_irq < 0) { in usb_conn_probe()
242 ret = info->vbus_irq; in usb_conn_probe()
246 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL, in usb_conn_probe()
248 pdev->name, info); in usb_conn_probe()
255 desc = &info->desc; in usb_conn_probe()
256 desc->name = "usb-charger"; in usb_conn_probe()
257 desc->properties = usb_charger_properties; in usb_conn_probe()
258 desc->num_properties = ARRAY_SIZE(usb_charger_properties); in usb_conn_probe()
259 desc->get_property = usb_charger_get_property; in usb_conn_probe()
260 desc->type = POWER_SUPPLY_TYPE_USB; in usb_conn_probe()
263 info->charger = devm_power_supply_register(dev, desc, &cfg); in usb_conn_probe()
264 if (IS_ERR(info->charger)) { in usb_conn_probe()
265 dev_err(dev, "Unable to register charger\n"); in usb_conn_probe()
266 return PTR_ERR(info->charger); in usb_conn_probe()
277 usb_role_switch_put(info->role_sw); in usb_conn_probe()
285 cancel_delayed_work_sync(&info->dw_det); in usb_conn_remove()
287 if (info->last_role == USB_ROLE_HOST && info->vbus) in usb_conn_remove()
288 regulator_disable(info->vbus); in usb_conn_remove()
290 usb_role_switch_put(info->role_sw); in usb_conn_remove()
299 if (info->id_gpiod) in usb_conn_suspend()
300 disable_irq(info->id_irq); in usb_conn_suspend()
301 if (info->vbus_gpiod) in usb_conn_suspend()
302 disable_irq(info->vbus_irq); in usb_conn_suspend()
315 if (info->id_gpiod) in usb_conn_resume()
316 enable_irq(info->id_irq); in usb_conn_resume()
317 if (info->vbus_gpiod) in usb_conn_resume()
318 enable_irq(info->vbus_irq); in usb_conn_resume()
329 { .compatible = "gpio-usb-b-connector", },
338 .name = "usb-conn-gpio",
347 MODULE_DESCRIPTION("USB GPIO based connection detection driver");