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
55 * ------------------------------------
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
75 id = info->id_gpiod ? in usb_conn_detect_cable()
76 gpiod_get_value_cansleep(info->id_gpiod) : 1; in usb_conn_detect_cable()
77 vbus = info->vbus_gpiod ? in usb_conn_detect_cable()
78 gpiod_get_value_cansleep(info->vbus_gpiod) : id; in usb_conn_detect_cable()
87 dev_dbg(info->dev, "role %s -> %s, gpios: id %d, vbus %d\n", in usb_conn_detect_cable()
88 usb_role_string(info->last_role), usb_role_string(role), id, vbus); in usb_conn_detect_cable()
90 if (!info->initial_detection && info->last_role == role) { in usb_conn_detect_cable()
91 dev_warn(info->dev, "repeated role: %s\n", usb_role_string(role)); in usb_conn_detect_cable()
95 info->initial_detection = false; in usb_conn_detect_cable()
97 if (info->last_role == USB_ROLE_HOST && info->vbus) in usb_conn_detect_cable()
98 regulator_disable(info->vbus); in usb_conn_detect_cable()
100 ret = usb_role_switch_set_role(info->role_sw, role); in usb_conn_detect_cable()
102 dev_err(info->dev, "failed to set role: %d\n", ret); in usb_conn_detect_cable()
104 if (role == USB_ROLE_HOST && info->vbus) { in usb_conn_detect_cable()
105 ret = regulator_enable(info->vbus); in usb_conn_detect_cable()
107 dev_err(info->dev, "enable vbus regulator failed\n"); in usb_conn_detect_cable()
110 info->last_role = role; in usb_conn_detect_cable()
112 if (info->vbus) in usb_conn_detect_cable()
113 dev_dbg(info->dev, "vbus regulator is %s\n", in usb_conn_detect_cable()
114 regulator_is_enabled(info->vbus) ? "enabled" : "disabled"); in usb_conn_detect_cable()
116 power_supply_changed(info->charger); in usb_conn_detect_cable()
122 queue_delayed_work(system_power_efficient_wq, &info->dw_det, delay); in usb_conn_queue_dwork()
129 usb_conn_queue_dwork(info, info->debounce_jiffies); in usb_conn_isr()
146 val->intval = info->last_role == USB_ROLE_DEVICE; in usb_charger_get_property()
149 return -EINVAL; in usb_charger_get_property()
157 struct device *dev = info->dev; in usb_conn_psy_register()
158 struct power_supply_desc *desc = &info->desc; in usb_conn_psy_register()
160 .of_node = dev->of_node, in usb_conn_psy_register()
163 desc->name = "usb-charger"; in usb_conn_psy_register()
164 desc->properties = usb_charger_properties; in usb_conn_psy_register()
165 desc->num_properties = ARRAY_SIZE(usb_charger_properties); in usb_conn_psy_register()
166 desc->get_property = usb_charger_get_property; in usb_conn_psy_register()
167 desc->type = POWER_SUPPLY_TYPE_USB; in usb_conn_psy_register()
170 info->charger = devm_power_supply_register(dev, desc, &cfg); in usb_conn_psy_register()
171 if (IS_ERR(info->charger)) in usb_conn_psy_register()
172 dev_err(dev, "Unable to register charger\n"); in usb_conn_psy_register()
174 return PTR_ERR_OR_ZERO(info->charger); in usb_conn_psy_register()
179 struct device *dev = &pdev->dev; in usb_conn_probe()
185 return -ENOMEM; in usb_conn_probe()
187 info->dev = dev; in usb_conn_probe()
188 info->id_gpiod = devm_gpiod_get_optional(dev, "id", GPIOD_IN); in usb_conn_probe()
189 if (IS_ERR(info->id_gpiod)) in usb_conn_probe()
190 return PTR_ERR(info->id_gpiod); in usb_conn_probe()
192 info->vbus_gpiod = devm_gpiod_get_optional(dev, "vbus", GPIOD_IN); in usb_conn_probe()
193 if (IS_ERR(info->vbus_gpiod)) in usb_conn_probe()
194 return PTR_ERR(info->vbus_gpiod); in usb_conn_probe()
196 if (!info->id_gpiod && !info->vbus_gpiod) { in usb_conn_probe()
198 return -ENODEV; in usb_conn_probe()
201 if (info->id_gpiod) in usb_conn_probe()
202 ret = gpiod_set_debounce(info->id_gpiod, USB_GPIO_DEB_US); in usb_conn_probe()
203 if (!ret && info->vbus_gpiod) in usb_conn_probe()
204 ret = gpiod_set_debounce(info->vbus_gpiod, USB_GPIO_DEB_US); in usb_conn_probe()
206 info->debounce_jiffies = msecs_to_jiffies(USB_GPIO_DEB_MS); in usb_conn_probe()
208 INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable); in usb_conn_probe()
210 info->vbus = devm_regulator_get_optional(dev, "vbus"); in usb_conn_probe()
211 if (PTR_ERR(info->vbus) == -ENODEV) in usb_conn_probe()
212 info->vbus = NULL; in usb_conn_probe()
214 if (IS_ERR(info->vbus)) in usb_conn_probe()
215 return dev_err_probe(dev, PTR_ERR(info->vbus), "failed to get vbus\n"); in usb_conn_probe()
217 info->role_sw = usb_role_switch_get(dev); in usb_conn_probe()
218 if (IS_ERR(info->role_sw)) in usb_conn_probe()
219 return dev_err_probe(dev, PTR_ERR(info->role_sw), in usb_conn_probe()
226 if (info->id_gpiod) { in usb_conn_probe()
227 info->id_irq = gpiod_to_irq(info->id_gpiod); in usb_conn_probe()
228 if (info->id_irq < 0) { in usb_conn_probe()
230 ret = info->id_irq; in usb_conn_probe()
234 ret = devm_request_threaded_irq(dev, info->id_irq, NULL, in usb_conn_probe()
236 pdev->name, info); in usb_conn_probe()
243 if (info->vbus_gpiod) { in usb_conn_probe()
244 info->vbus_irq = gpiod_to_irq(info->vbus_gpiod); in usb_conn_probe()
245 if (info->vbus_irq < 0) { in usb_conn_probe()
247 ret = info->vbus_irq; in usb_conn_probe()
251 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL, in usb_conn_probe()
253 pdev->name, info); in usb_conn_probe()
261 device_set_wakeup_capable(&pdev->dev, true); in usb_conn_probe()
264 info->initial_detection = true; in usb_conn_probe()
270 usb_role_switch_put(info->role_sw); in usb_conn_probe()
278 cancel_delayed_work_sync(&info->dw_det); in usb_conn_remove()
280 if (info->last_role == USB_ROLE_HOST && info->vbus) in usb_conn_remove()
281 regulator_disable(info->vbus); in usb_conn_remove()
283 usb_role_switch_put(info->role_sw); in usb_conn_remove()
291 if (info->id_gpiod) in usb_conn_suspend()
292 enable_irq_wake(info->id_irq); in usb_conn_suspend()
293 if (info->vbus_gpiod) in usb_conn_suspend()
294 enable_irq_wake(info->vbus_irq); in usb_conn_suspend()
298 if (info->id_gpiod) in usb_conn_suspend()
299 disable_irq(info->id_irq); in usb_conn_suspend()
300 if (info->vbus_gpiod) in usb_conn_suspend()
301 disable_irq(info->vbus_irq); in usb_conn_suspend()
313 if (info->id_gpiod) in usb_conn_resume()
314 disable_irq_wake(info->id_irq); in usb_conn_resume()
315 if (info->vbus_gpiod) in usb_conn_resume()
316 disable_irq_wake(info->vbus_irq); in usb_conn_resume()
322 if (info->id_gpiod) in usb_conn_resume()
323 enable_irq(info->id_irq); in usb_conn_resume()
324 if (info->vbus_gpiod) in usb_conn_resume()
325 enable_irq(info->vbus_irq); in usb_conn_resume()
336 { .compatible = "gpio-usb-b-connector", },
345 .name = "usb-conn-gpio",
354 MODULE_DESCRIPTION("USB GPIO based connection detection driver");