1 // SPDX-License-Identifier: GPL-2.0-only
3 * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID
4 * and VBUS detection based on extcon-usb-gpio.c.
6 * Copyright (C) 2016 Linaro, Ltd.
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/mod_devicetable.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
21 #define USB_ID_DEBOUNCE_MS 5 /* ms */
23 struct qcom_usb_extcon_info {
24 struct extcon_dev *edev;
27 struct delayed_work wq_detcable;
28 unsigned long debounce_jiffies;
31 static const unsigned int qcom_usb_extcon_cable[] = {
37 static void qcom_usb_extcon_detect_cable(struct work_struct *work)
41 union extcon_property_value val;
42 struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),
43 struct qcom_usb_extcon_info,
46 if (info->id_irq > 0) {
47 /* check ID and update cable state */
48 ret = irq_get_irqchip_state(info->id_irq,
49 IRQCHIP_STATE_LINE_LEVEL, &state);
55 extcon_set_property(info->edev, EXTCON_USB_HOST,
56 EXTCON_PROP_USB_SS, val);
58 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !state);
61 if (info->vbus_irq > 0) {
62 /* check VBUS and update cable state */
63 ret = irq_get_irqchip_state(info->vbus_irq,
64 IRQCHIP_STATE_LINE_LEVEL, &state);
70 extcon_set_property(info->edev, EXTCON_USB,
71 EXTCON_PROP_USB_SS, val);
73 extcon_set_state_sync(info->edev, EXTCON_USB, state);
77 static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)
79 struct qcom_usb_extcon_info *info = dev_id;
81 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
82 info->debounce_jiffies);
87 static int qcom_usb_extcon_probe(struct platform_device *pdev)
89 struct device *dev = &pdev->dev;
90 struct qcom_usb_extcon_info *info;
93 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
97 info->edev = devm_extcon_dev_allocate(dev, qcom_usb_extcon_cable);
98 if (IS_ERR(info->edev)) {
99 dev_err(dev, "failed to allocate extcon device\n");
103 ret = devm_extcon_dev_register(dev, info->edev);
105 dev_err(dev, "failed to register extcon device\n");
109 ret = extcon_set_property_capability(info->edev,
110 EXTCON_USB, EXTCON_PROP_USB_SS);
111 ret |= extcon_set_property_capability(info->edev,
112 EXTCON_USB_HOST, EXTCON_PROP_USB_SS);
114 dev_err(dev, "failed to register extcon props rc=%d\n",
119 info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);
121 ret = devm_delayed_work_autocancel(dev, &info->wq_detcable,
122 qcom_usb_extcon_detect_cable);
126 info->id_irq = platform_get_irq_byname_optional(pdev, "usb_id");
127 if (info->id_irq > 0) {
128 ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
129 qcom_usb_irq_handler,
130 IRQF_TRIGGER_RISING |
131 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
134 dev_err(dev, "failed to request handler for ID IRQ\n");
139 info->vbus_irq = platform_get_irq_byname_optional(pdev, "usb_vbus");
140 if (info->vbus_irq > 0) {
141 ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
142 qcom_usb_irq_handler,
143 IRQF_TRIGGER_RISING |
144 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
147 dev_err(dev, "failed to request handler for VBUS IRQ\n");
152 if (info->id_irq < 0 && info->vbus_irq < 0) {
153 dev_err(dev, "ID and VBUS IRQ not found\n");
157 platform_set_drvdata(pdev, info);
158 device_init_wakeup(dev, 1);
160 /* Perform initial detection */
161 qcom_usb_extcon_detect_cable(&info->wq_detcable.work);
166 #ifdef CONFIG_PM_SLEEP
167 static int qcom_usb_extcon_suspend(struct device *dev)
169 struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
172 if (device_may_wakeup(dev)) {
173 if (info->id_irq > 0)
174 ret = enable_irq_wake(info->id_irq);
175 if (info->vbus_irq > 0)
176 ret = enable_irq_wake(info->vbus_irq);
182 static int qcom_usb_extcon_resume(struct device *dev)
184 struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
187 if (device_may_wakeup(dev)) {
188 if (info->id_irq > 0)
189 ret = disable_irq_wake(info->id_irq);
190 if (info->vbus_irq > 0)
191 ret = disable_irq_wake(info->vbus_irq);
198 static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops,
199 qcom_usb_extcon_suspend, qcom_usb_extcon_resume);
201 static const struct of_device_id qcom_usb_extcon_dt_match[] = {
202 { .compatible = "qcom,pm8941-misc", },
205 MODULE_DEVICE_TABLE(of, qcom_usb_extcon_dt_match);
207 static struct platform_driver qcom_usb_extcon_driver = {
208 .probe = qcom_usb_extcon_probe,
210 .name = "extcon-pm8941-misc",
211 .pm = &qcom_usb_extcon_pm_ops,
212 .of_match_table = qcom_usb_extcon_dt_match,
215 module_platform_driver(qcom_usb_extcon_driver);
217 MODULE_DESCRIPTION("QCOM USB ID extcon driver");
219 MODULE_LICENSE("GPL v2");