Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / drivers / extcon / extcon-qcom-spmi-misc.c
blob72bc0f2478e28336764916f8ad8fce0c7a4cd032
1 /**
2 * extcon-qcom-spmi-misc.c - Qualcomm USB extcon driver to support USB ID
3 * detection based on extcon-usb-gpio.c.
5 * Copyright (C) 2016 Linaro, Ltd.
6 * Stephen Boyd <stephen.boyd@linaro.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/extcon-provider.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/workqueue.h>
28 #define USB_ID_DEBOUNCE_MS 5 /* ms */
30 struct qcom_usb_extcon_info {
31 struct extcon_dev *edev;
32 int irq;
33 struct delayed_work wq_detcable;
34 unsigned long debounce_jiffies;
37 static const unsigned int qcom_usb_extcon_cable[] = {
38 EXTCON_USB_HOST,
39 EXTCON_NONE,
42 static void qcom_usb_extcon_detect_cable(struct work_struct *work)
44 bool id;
45 int ret;
46 struct qcom_usb_extcon_info *info = container_of(to_delayed_work(work),
47 struct qcom_usb_extcon_info,
48 wq_detcable);
50 /* check ID and update cable state */
51 ret = irq_get_irqchip_state(info->irq, IRQCHIP_STATE_LINE_LEVEL, &id);
52 if (ret)
53 return;
55 extcon_set_state_sync(info->edev, EXTCON_USB_HOST, !id);
58 static irqreturn_t qcom_usb_irq_handler(int irq, void *dev_id)
60 struct qcom_usb_extcon_info *info = dev_id;
62 queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
63 info->debounce_jiffies);
65 return IRQ_HANDLED;
68 static int qcom_usb_extcon_probe(struct platform_device *pdev)
70 struct device *dev = &pdev->dev;
71 struct qcom_usb_extcon_info *info;
72 int ret;
74 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
75 if (!info)
76 return -ENOMEM;
78 info->edev = devm_extcon_dev_allocate(dev, qcom_usb_extcon_cable);
79 if (IS_ERR(info->edev)) {
80 dev_err(dev, "failed to allocate extcon device\n");
81 return -ENOMEM;
84 ret = devm_extcon_dev_register(dev, info->edev);
85 if (ret < 0) {
86 dev_err(dev, "failed to register extcon device\n");
87 return ret;
90 info->debounce_jiffies = msecs_to_jiffies(USB_ID_DEBOUNCE_MS);
91 INIT_DELAYED_WORK(&info->wq_detcable, qcom_usb_extcon_detect_cable);
93 info->irq = platform_get_irq_byname(pdev, "usb_id");
94 if (info->irq < 0)
95 return info->irq;
97 ret = devm_request_threaded_irq(dev, info->irq, NULL,
98 qcom_usb_irq_handler,
99 IRQF_TRIGGER_RISING |
100 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
101 pdev->name, info);
102 if (ret < 0) {
103 dev_err(dev, "failed to request handler for ID IRQ\n");
104 return ret;
107 platform_set_drvdata(pdev, info);
108 device_init_wakeup(dev, 1);
110 /* Perform initial detection */
111 qcom_usb_extcon_detect_cable(&info->wq_detcable.work);
113 return 0;
116 static int qcom_usb_extcon_remove(struct platform_device *pdev)
118 struct qcom_usb_extcon_info *info = platform_get_drvdata(pdev);
120 cancel_delayed_work_sync(&info->wq_detcable);
122 return 0;
125 #ifdef CONFIG_PM_SLEEP
126 static int qcom_usb_extcon_suspend(struct device *dev)
128 struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
129 int ret = 0;
131 if (device_may_wakeup(dev))
132 ret = enable_irq_wake(info->irq);
134 return ret;
137 static int qcom_usb_extcon_resume(struct device *dev)
139 struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
140 int ret = 0;
142 if (device_may_wakeup(dev))
143 ret = disable_irq_wake(info->irq);
145 return ret;
147 #endif
149 static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops,
150 qcom_usb_extcon_suspend, qcom_usb_extcon_resume);
152 static const struct of_device_id qcom_usb_extcon_dt_match[] = {
153 { .compatible = "qcom,pm8941-misc", },
156 MODULE_DEVICE_TABLE(of, qcom_usb_extcon_dt_match);
158 static struct platform_driver qcom_usb_extcon_driver = {
159 .probe = qcom_usb_extcon_probe,
160 .remove = qcom_usb_extcon_remove,
161 .driver = {
162 .name = "extcon-pm8941-misc",
163 .pm = &qcom_usb_extcon_pm_ops,
164 .of_match_table = qcom_usb_extcon_dt_match,
167 module_platform_driver(qcom_usb_extcon_driver);
169 MODULE_DESCRIPTION("QCOM USB ID extcon driver");
170 MODULE_AUTHOR("Stephen Boyd <stephen.boyd@linaro.org>");
171 MODULE_LICENSE("GPL v2");