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.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
27 #define USB_ID_DEBOUNCE_MS 5 /* ms */
29 struct qcom_usb_extcon_info
{
30 struct extcon_dev
*edev
;
32 struct delayed_work wq_detcable
;
33 unsigned long debounce_jiffies
;
36 static const unsigned int qcom_usb_extcon_cable
[] = {
41 static void qcom_usb_extcon_detect_cable(struct work_struct
*work
)
45 struct qcom_usb_extcon_info
*info
= container_of(to_delayed_work(work
),
46 struct qcom_usb_extcon_info
,
49 /* check ID and update cable state */
50 ret
= irq_get_irqchip_state(info
->irq
, IRQCHIP_STATE_LINE_LEVEL
, &id
);
54 extcon_set_state_sync(info
->edev
, EXTCON_USB_HOST
, !id
);
57 static irqreturn_t
qcom_usb_irq_handler(int irq
, void *dev_id
)
59 struct qcom_usb_extcon_info
*info
= dev_id
;
61 queue_delayed_work(system_power_efficient_wq
, &info
->wq_detcable
,
62 info
->debounce_jiffies
);
67 static int qcom_usb_extcon_probe(struct platform_device
*pdev
)
69 struct device
*dev
= &pdev
->dev
;
70 struct qcom_usb_extcon_info
*info
;
73 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
77 info
->edev
= devm_extcon_dev_allocate(dev
, qcom_usb_extcon_cable
);
78 if (IS_ERR(info
->edev
)) {
79 dev_err(dev
, "failed to allocate extcon device\n");
83 ret
= devm_extcon_dev_register(dev
, info
->edev
);
85 dev_err(dev
, "failed to register extcon device\n");
89 info
->debounce_jiffies
= msecs_to_jiffies(USB_ID_DEBOUNCE_MS
);
90 INIT_DELAYED_WORK(&info
->wq_detcable
, qcom_usb_extcon_detect_cable
);
92 info
->irq
= platform_get_irq_byname(pdev
, "usb_id");
96 ret
= devm_request_threaded_irq(dev
, info
->irq
, NULL
,
99 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
102 dev_err(dev
, "failed to request handler for ID IRQ\n");
106 platform_set_drvdata(pdev
, info
);
107 device_init_wakeup(dev
, 1);
109 /* Perform initial detection */
110 qcom_usb_extcon_detect_cable(&info
->wq_detcable
.work
);
115 static int qcom_usb_extcon_remove(struct platform_device
*pdev
)
117 struct qcom_usb_extcon_info
*info
= platform_get_drvdata(pdev
);
119 cancel_delayed_work_sync(&info
->wq_detcable
);
124 #ifdef CONFIG_PM_SLEEP
125 static int qcom_usb_extcon_suspend(struct device
*dev
)
127 struct qcom_usb_extcon_info
*info
= dev_get_drvdata(dev
);
130 if (device_may_wakeup(dev
))
131 ret
= enable_irq_wake(info
->irq
);
136 static int qcom_usb_extcon_resume(struct device
*dev
)
138 struct qcom_usb_extcon_info
*info
= dev_get_drvdata(dev
);
141 if (device_may_wakeup(dev
))
142 ret
= disable_irq_wake(info
->irq
);
148 static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops
,
149 qcom_usb_extcon_suspend
, qcom_usb_extcon_resume
);
151 static const struct of_device_id qcom_usb_extcon_dt_match
[] = {
152 { .compatible
= "qcom,pm8941-misc", },
155 MODULE_DEVICE_TABLE(of
, qcom_usb_extcon_dt_match
);
157 static struct platform_driver qcom_usb_extcon_driver
= {
158 .probe
= qcom_usb_extcon_probe
,
159 .remove
= qcom_usb_extcon_remove
,
161 .name
= "extcon-pm8941-misc",
162 .pm
= &qcom_usb_extcon_pm_ops
,
163 .of_match_table
= qcom_usb_extcon_dt_match
,
166 module_platform_driver(qcom_usb_extcon_driver
);
168 MODULE_DESCRIPTION("QCOM USB ID extcon driver");
169 MODULE_AUTHOR("Stephen Boyd <stephen.boyd@linaro.org>");
170 MODULE_LICENSE("GPL v2");