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