1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID 4 * and VBUS detection based on extcon-usb-gpio.c. 5 * 6 * Copyright (C) 2016 Linaro, Ltd. 7 * Stephen Boyd <stephen.boyd@linaro.org> 8 */ 9 10 #include <linux/devm-helpers.h> 11 #include <linux/extcon-provider.h> 12 #include <linux/init.h> 13 #include <linux/interrupt.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/platform_device.h> 17 #include <linux/slab.h> 18 #include <linux/workqueue.h> 19 20 #define USB_ID_DEBOUNCE_MS 5 /* ms */ 21 22 struct qcom_usb_extcon_info { 23 struct extcon_dev *edev; 24 int id_irq; 25 int vbus_irq; 26 struct delayed_work wq_detcable; 27 unsigned long debounce_jiffies; 28 }; 29 30 static const unsigned int qcom_usb_extcon_cable[] = { 31 EXTCON_USB, 32 EXTCON_USB_HOST, 33 EXTCON_NONE, 34 }; 35 36 static void qcom_usb_extcon_detect_cable(struct work_struct *work) 37 { 38 bool state = false; 39 int ret; 40 union extcon_property_value val; 41 struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work), 42 struct qcom_usb_extcon_info, 43 wq_detcable); 44 45 if (info->id_irq > 0) { 46 /* check ID and update cable state */ 47 ret = irq_get_irqchip_state(info->id_irq, 48 IRQCHIP_STATE_LINE_LEVEL, &state); 49 if (ret) 50 return; 51 52 if (!state) { 53 val.intval = true; 54 extcon_set_property(info->edev, EXTCON_USB_HOST, 55 EXTCON_PROP_USB_SS, val); 56 } 57 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !state); 58 } 59 60 if (info->vbus_irq > 0) { 61 /* check VBUS and update cable state */ 62 ret = irq_get_irqchip_state(info->vbus_irq, 63 IRQCHIP_STATE_LINE_LEVEL, &state); 64 if (ret) 65 return; 66 67 if (state) { 68 val.intval = true; 69 extcon_set_property(info->edev, EXTCON_USB, 70 EXTCON_PROP_USB_SS, val); 71 } 72 extcon_set_state_sync(info->edev, EXTCON_USB, state); 73 } 74 } 75 76 static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id) 77 { 78 struct qcom_usb_extcon_info *info = dev_id; 79 80 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, 81 info->debounce_jiffies); 82 83 return IRQ_HANDLED; 84 } 85 86 static int qcom_usb_extcon_probe(struct platform_device *pdev) 87 { 88 struct device *dev = &pdev->dev; 89 struct qcom_usb_extcon_info *info; 90 int ret; 91 92 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 93 if (!info) 94 return -ENOMEM; 95 96 info->edev = devm_extcon_dev_allocate(dev, qcom_usb_extcon_cable); 97 if (IS_ERR(info->edev)) { 98 dev_err(dev, "failed to allocate extcon device\n"); 99 return -ENOMEM; 100 } 101 102 ret = devm_extcon_dev_register(dev, info->edev); 103 if (ret < 0) { 104 dev_err(dev, "failed to register extcon device\n"); 105 return ret; 106 } 107 108 ret = extcon_set_property_capability(info->edev, 109 EXTCON_USB, EXTCON_PROP_USB_SS); 110 ret |= extcon_set_property_capability(info->edev, 111 EXTCON_USB_HOST, EXTCON_PROP_USB_SS); 112 if (ret) { 113 dev_err(dev, "failed to register extcon props rc=%d\n", 114 ret); 115 return ret; 116 } 117 118 info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS); 119 120 ret = devm_delayed_work_autocancel(dev, &info->wq_detcable, 121 qcom_usb_extcon_detect_cable); 122 if (ret) 123 return ret; 124 125 info->id_irq = platform_get_irq_byname_optional(pdev, "usb_id"); 126 if (info->id_irq > 0) { 127 ret = devm_request_threaded_irq(dev, info->id_irq, NULL, 128 qcom_usb_irq_handler, 129 IRQF_TRIGGER_RISING | 130 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 131 pdev->name, info); 132 if (ret < 0) { 133 dev_err(dev, "failed to request handler for ID IRQ\n"); 134 return ret; 135 } 136 } 137 138 info->vbus_irq = platform_get_irq_byname_optional(pdev, "usb_vbus"); 139 if (info->vbus_irq > 0) { 140 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL, 141 qcom_usb_irq_handler, 142 IRQF_TRIGGER_RISING | 143 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 144 pdev->name, info); 145 if (ret < 0) { 146 dev_err(dev, "failed to request handler for VBUS IRQ\n"); 147 return ret; 148 } 149 } 150 151 if (info->id_irq < 0 && info->vbus_irq < 0) { 152 dev_err(dev, "ID and VBUS IRQ not found\n"); 153 return -EINVAL; 154 } 155 156 platform_set_drvdata(pdev, info); 157 devm_device_init_wakeup(dev); 158 159 /* Perform initial detection */ 160 qcom_usb_extcon_detect_cable(&info->wq_detcable.work); 161 162 return 0; 163 } 164 165 #ifdef CONFIG_PM_SLEEP 166 static int qcom_usb_extcon_suspend(struct device *dev) 167 { 168 struct qcom_usb_extcon_info *info = dev_get_drvdata(dev); 169 int ret = 0; 170 171 if (device_may_wakeup(dev)) { 172 if (info->id_irq > 0) 173 ret = enable_irq_wake(info->id_irq); 174 if (info->vbus_irq > 0) 175 ret = enable_irq_wake(info->vbus_irq); 176 } 177 178 return ret; 179 } 180 181 static int qcom_usb_extcon_resume(struct device *dev) 182 { 183 struct qcom_usb_extcon_info *info = dev_get_drvdata(dev); 184 int ret = 0; 185 186 if (device_may_wakeup(dev)) { 187 if (info->id_irq > 0) 188 ret = disable_irq_wake(info->id_irq); 189 if (info->vbus_irq > 0) 190 ret = disable_irq_wake(info->vbus_irq); 191 } 192 193 return ret; 194 } 195 #endif 196 197 static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops, 198 qcom_usb_extcon_suspend, qcom_usb_extcon_resume); 199 200 static const struct of_device_id qcom_usb_extcon_dt_match[] = { 201 { .compatible = "qcom,pm8941-misc", }, 202 { } 203 }; 204 MODULE_DEVICE_TABLE(of, qcom_usb_extcon_dt_match); 205 206 static struct platform_driver qcom_usb_extcon_driver = { 207 .probe = qcom_usb_extcon_probe, 208 .driver = { 209 .name = "extcon-pm8941-misc", 210 .pm = &qcom_usb_extcon_pm_ops, 211 .of_match_table = qcom_usb_extcon_dt_match, 212 }, 213 }; 214 module_platform_driver(qcom_usb_extcon_driver); 215 216 MODULE_DESCRIPTION("QCOM USB ID extcon driver"); 217 MODULE_AUTHOR("Stephen Boyd <stephen.boyd@linaro.org>"); 218 MODULE_LICENSE("GPL v2"); 219