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/extcon-provider.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.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 	INIT_DELAYED_WORK(&info->wq_detcable, qcom_usb_extcon_detect_cable);
120 
121 	info->id_irq = platform_get_irq_byname(pdev, "usb_id");
122 	if (info->id_irq > 0) {
123 		ret = devm_request_threaded_irq(dev, info->id_irq, NULL,
124 					qcom_usb_irq_handler,
125 					IRQF_TRIGGER_RISING |
126 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
127 					pdev->name, info);
128 		if (ret < 0) {
129 			dev_err(dev, "failed to request handler for ID IRQ\n");
130 			return ret;
131 		}
132 	}
133 
134 	info->vbus_irq = platform_get_irq_byname(pdev, "usb_vbus");
135 	if (info->vbus_irq > 0) {
136 		ret = devm_request_threaded_irq(dev, info->vbus_irq, NULL,
137 					qcom_usb_irq_handler,
138 					IRQF_TRIGGER_RISING |
139 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
140 					pdev->name, info);
141 		if (ret < 0) {
142 			dev_err(dev, "failed to request handler for VBUS IRQ\n");
143 			return ret;
144 		}
145 	}
146 
147 	if (info->id_irq < 0 && info->vbus_irq < 0) {
148 		dev_err(dev, "ID and VBUS IRQ not found\n");
149 		return -EINVAL;
150 	}
151 
152 	platform_set_drvdata(pdev, info);
153 	device_init_wakeup(dev, 1);
154 
155 	/* Perform initial detection */
156 	qcom_usb_extcon_detect_cable(&info->wq_detcable.work);
157 
158 	return 0;
159 }
160 
161 static int qcom_usb_extcon_remove(struct platform_device *pdev)
162 {
163 	struct qcom_usb_extcon_info *info = platform_get_drvdata(pdev);
164 
165 	cancel_delayed_work_sync(&info->wq_detcable);
166 
167 	return 0;
168 }
169 
170 #ifdef CONFIG_PM_SLEEP
171 static int qcom_usb_extcon_suspend(struct device *dev)
172 {
173 	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
174 	int ret = 0;
175 
176 	if (device_may_wakeup(dev)) {
177 		if (info->id_irq > 0)
178 			ret = enable_irq_wake(info->id_irq);
179 		if (info->vbus_irq > 0)
180 			ret = enable_irq_wake(info->vbus_irq);
181 	}
182 
183 	return ret;
184 }
185 
186 static int qcom_usb_extcon_resume(struct device *dev)
187 {
188 	struct qcom_usb_extcon_info *info = dev_get_drvdata(dev);
189 	int ret = 0;
190 
191 	if (device_may_wakeup(dev)) {
192 		if (info->id_irq > 0)
193 			ret = disable_irq_wake(info->id_irq);
194 		if (info->vbus_irq > 0)
195 			ret = disable_irq_wake(info->vbus_irq);
196 	}
197 
198 	return ret;
199 }
200 #endif
201 
202 static SIMPLE_DEV_PM_OPS(qcom_usb_extcon_pm_ops,
203 			 qcom_usb_extcon_suspend, qcom_usb_extcon_resume);
204 
205 static const struct of_device_id qcom_usb_extcon_dt_match[] = {
206 	{ .compatible = "qcom,pm8941-misc", },
207 	{ }
208 };
209 MODULE_DEVICE_TABLE(of, qcom_usb_extcon_dt_match);
210 
211 static struct platform_driver qcom_usb_extcon_driver = {
212 	.probe		= qcom_usb_extcon_probe,
213 	.remove		= qcom_usb_extcon_remove,
214 	.driver		= {
215 		.name	= "extcon-pm8941-misc",
216 		.pm	= &qcom_usb_extcon_pm_ops,
217 		.of_match_table = qcom_usb_extcon_dt_match,
218 	},
219 };
220 module_platform_driver(qcom_usb_extcon_driver);
221 
222 MODULE_DESCRIPTION("QCOM USB ID extcon driver");
223 MODULE_AUTHOR("Stephen Boyd <stephen.boyd@linaro.org>");
224 MODULE_LICENSE("GPL v2");
225