blob: cea58d0cb45741bd056e36fc079199df9cc4e645 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Graeme Gregoryb1f254e2013-05-28 10:50:11 +09002/*
3 * Palmas USB transceiver driver
4 *
5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
Graeme Gregoryb1f254e2013-05-28 10:50:11 +09006 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Kishon Vijay Abraham I <kishon@ti.com>
Graeme Gregoryb1f254e2013-05-28 10:50:11 +09008 * Based on twl6030_usb.c
Graeme Gregoryb1f254e2013-05-28 10:50:11 +09009 * Author: Hema HK <hemahk@ti.com>
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090010 */
11
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/platform_device.h>
Kishon Vijay Abraham If63d32d2014-03-28 12:21:02 +053015#include <linux/slab.h>
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090016#include <linux/err.h>
17#include <linux/mfd/palmas.h>
18#include <linux/of.h>
19#include <linux/of_platform.h>
Roger Quadros92b7cb52015-08-03 17:40:51 +030020#include <linux/of_gpio.h>
Chanwoo Choiac22a1d2015-08-13 09:19:43 +090021#include <linux/gpio/consumer.h>
Roger Quadros92b7cb52015-08-03 17:40:51 +030022#include <linux/workqueue.h>
23
24#define USB_GPIO_DEBOUNCE_MS 20 /* ms */
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090025
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090026static const unsigned int palmas_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090027 EXTCON_USB,
28 EXTCON_USB_HOST,
29 EXTCON_NONE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090030};
31
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090032static void palmas_usb_wakeup(struct palmas *palmas, int enable)
33{
34 if (enable)
35 palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP,
36 PALMAS_USB_WAKEUP_ID_WK_UP_COMP);
37 else
38 palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, 0);
39}
40
41static irqreturn_t palmas_vbus_irq_handler(int irq, void *_palmas_usb)
42{
43 struct palmas_usb *palmas_usb = _palmas_usb;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090044 struct extcon_dev *edev = palmas_usb->edev;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090045 unsigned int vbus_line_state;
46
47 palmas_read(palmas_usb->palmas, PALMAS_INTERRUPT_BASE,
48 PALMAS_INT3_LINE_STATE, &vbus_line_state);
49
50 if (vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS) {
51 if (palmas_usb->linkstat != PALMAS_USB_STATE_VBUS) {
52 palmas_usb->linkstat = PALMAS_USB_STATE_VBUS;
Chanwoo Choi8670b452016-08-16 15:55:34 +090053 extcon_set_state_sync(edev, EXTCON_USB, true);
Roger Quadros567ab5a2017-01-24 16:54:07 +020054 dev_dbg(palmas_usb->dev, "USB cable is attached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090055 } else {
56 dev_dbg(palmas_usb->dev,
57 "Spurious connect event detected\n");
58 }
59 } else if (!(vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS)) {
60 if (palmas_usb->linkstat == PALMAS_USB_STATE_VBUS) {
61 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi8670b452016-08-16 15:55:34 +090062 extcon_set_state_sync(edev, EXTCON_USB, false);
Roger Quadros567ab5a2017-01-24 16:54:07 +020063 dev_dbg(palmas_usb->dev, "USB cable is detached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090064 } else {
65 dev_dbg(palmas_usb->dev,
66 "Spurious disconnect event detected\n");
67 }
68 }
69
70 return IRQ_HANDLED;
71}
72
73static irqreturn_t palmas_id_irq_handler(int irq, void *_palmas_usb)
74{
George Cherianb7d2d5b2013-10-11 00:18:39 +053075 unsigned int set, id_src;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090076 struct palmas_usb *palmas_usb = _palmas_usb;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090077 struct extcon_dev *edev = palmas_usb->edev;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090078
79 palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
80 PALMAS_USB_ID_INT_LATCH_SET, &set);
George Cherianb7d2d5b2013-10-11 00:18:39 +053081 palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
82 PALMAS_USB_ID_INT_SRC, &id_src);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090083
George Cherianb7d2d5b2013-10-11 00:18:39 +053084 if ((set & PALMAS_USB_ID_INT_SRC_ID_GND) &&
85 (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090086 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090087 PALMAS_USB_ID_INT_LATCH_CLR,
88 PALMAS_USB_ID_INT_EN_HI_CLR_ID_GND);
89 palmas_usb->linkstat = PALMAS_USB_STATE_ID;
Chanwoo Choi8670b452016-08-16 15:55:34 +090090 extcon_set_state_sync(edev, EXTCON_USB_HOST, true);
Roger Quadros567ab5a2017-01-24 16:54:07 +020091 dev_dbg(palmas_usb->dev, "USB-HOST cable is attached\n");
George Cherianb7d2d5b2013-10-11 00:18:39 +053092 } else if ((set & PALMAS_USB_ID_INT_SRC_ID_FLOAT) &&
93 (id_src & PALMAS_USB_ID_INT_SRC_ID_FLOAT)) {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090094 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090095 PALMAS_USB_ID_INT_LATCH_CLR,
96 PALMAS_USB_ID_INT_EN_HI_CLR_ID_FLOAT);
97 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi8670b452016-08-16 15:55:34 +090098 extcon_set_state_sync(edev, EXTCON_USB_HOST, false);
Roger Quadros567ab5a2017-01-24 16:54:07 +020099 dev_dbg(palmas_usb->dev, "USB-HOST cable is detached\n");
Laxman Dewangan002945f2013-07-09 18:34:22 +0530100 } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_ID) &&
101 (!(set & PALMAS_USB_ID_INT_SRC_ID_GND))) {
102 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi8670b452016-08-16 15:55:34 +0900103 extcon_set_state_sync(edev, EXTCON_USB_HOST, false);
Roger Quadros567ab5a2017-01-24 16:54:07 +0200104 dev_dbg(palmas_usb->dev, "USB-HOST cable is detached\n");
George Cherianb7d2d5b2013-10-11 00:18:39 +0530105 } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_DISCONNECT) &&
106 (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
107 palmas_usb->linkstat = PALMAS_USB_STATE_ID;
Chanwoo Choi8670b452016-08-16 15:55:34 +0900108 extcon_set_state_sync(edev, EXTCON_USB_HOST, true);
Roger Quadros567ab5a2017-01-24 16:54:07 +0200109 dev_dbg(palmas_usb->dev, " USB-HOST cable is attached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900110 }
111
112 return IRQ_HANDLED;
113}
114
Roger Quadros92b7cb52015-08-03 17:40:51 +0300115static void palmas_gpio_id_detect(struct work_struct *work)
116{
117 int id;
118 struct palmas_usb *palmas_usb = container_of(to_delayed_work(work),
119 struct palmas_usb,
120 wq_detectid);
121 struct extcon_dev *edev = palmas_usb->edev;
122
123 if (!palmas_usb->id_gpiod)
124 return;
125
126 id = gpiod_get_value_cansleep(palmas_usb->id_gpiod);
127
128 if (id) {
Chanwoo Choi8670b452016-08-16 15:55:34 +0900129 extcon_set_state_sync(edev, EXTCON_USB_HOST, false);
Roger Quadros567ab5a2017-01-24 16:54:07 +0200130 dev_dbg(palmas_usb->dev, "USB-HOST cable is detached\n");
Roger Quadros92b7cb52015-08-03 17:40:51 +0300131 } else {
Chanwoo Choi8670b452016-08-16 15:55:34 +0900132 extcon_set_state_sync(edev, EXTCON_USB_HOST, true);
Roger Quadros567ab5a2017-01-24 16:54:07 +0200133 dev_dbg(palmas_usb->dev, "USB-HOST cable is attached\n");
Roger Quadros92b7cb52015-08-03 17:40:51 +0300134 }
135}
136
137static irqreturn_t palmas_gpio_id_irq_handler(int irq, void *_palmas_usb)
138{
139 struct palmas_usb *palmas_usb = _palmas_usb;
140
141 queue_delayed_work(system_power_efficient_wq, &palmas_usb->wq_detectid,
142 palmas_usb->sw_debounce_jiffies);
143
144 return IRQ_HANDLED;
145}
146
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900147static void palmas_enable_irq(struct palmas_usb *palmas_usb)
148{
149 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
150 PALMAS_USB_VBUS_CTRL_SET,
151 PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP);
152
Roger Quadros92b7cb52015-08-03 17:40:51 +0300153 if (palmas_usb->enable_id_detection) {
154 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
155 PALMAS_USB_ID_CTRL_SET,
156 PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900157
Roger Quadros92b7cb52015-08-03 17:40:51 +0300158 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
159 PALMAS_USB_ID_INT_EN_HI_SET,
160 PALMAS_USB_ID_INT_EN_HI_SET_ID_GND |
161 PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT);
162 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900163
Laxman Dewangan7281e052013-07-10 14:59:06 +0530164 if (palmas_usb->enable_vbus_detection)
165 palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900166
167 /* cold plug for host mode needs this delay */
Laxman Dewangan7281e052013-07-10 14:59:06 +0530168 if (palmas_usb->enable_id_detection) {
169 msleep(30);
170 palmas_id_irq_handler(palmas_usb->id_irq, palmas_usb);
171 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900172}
173
174static int palmas_usb_probe(struct platform_device *pdev)
175{
176 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
Jingoo Han7c0f65582013-09-11 13:22:18 +0900177 struct palmas_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900178 struct device_node *node = pdev->dev.of_node;
179 struct palmas_usb *palmas_usb;
180 int status;
181
Roger Quadros9fe172b2016-12-08 10:45:31 +0200182 if (!palmas) {
183 dev_err(&pdev->dev, "failed to get valid parent\n");
184 return -EINVAL;
185 }
186
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900187 palmas_usb = devm_kzalloc(&pdev->dev, sizeof(*palmas_usb), GFP_KERNEL);
188 if (!palmas_usb)
189 return -ENOMEM;
190
Laxman Dewangan7281e052013-07-10 14:59:06 +0530191 if (node && !pdata) {
192 palmas_usb->wakeup = of_property_read_bool(node, "ti,wakeup");
193 palmas_usb->enable_id_detection = of_property_read_bool(node,
194 "ti,enable-id-detection");
195 palmas_usb->enable_vbus_detection = of_property_read_bool(node,
196 "ti,enable-vbus-detection");
197 } else {
198 palmas_usb->wakeup = true;
199 palmas_usb->enable_id_detection = true;
200 palmas_usb->enable_vbus_detection = true;
201
202 if (pdata)
203 palmas_usb->wakeup = pdata->wakeup;
204 }
205
Chanwoo Choiac22a1d2015-08-13 09:19:43 +0900206 palmas_usb->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id",
207 GPIOD_IN);
H. Nikolaus Schaller3426ad62020-02-17 14:38:15 +0100208 if (PTR_ERR(palmas_usb->id_gpiod) == -EPROBE_DEFER) {
209 return -EPROBE_DEFER;
210 } else if (IS_ERR(palmas_usb->id_gpiod)) {
Roger Quadros92b7cb52015-08-03 17:40:51 +0300211 dev_err(&pdev->dev, "failed to get id gpio\n");
212 return PTR_ERR(palmas_usb->id_gpiod);
213 }
214
Felipe Balbib7aad8e2015-12-30 09:55:45 +0900215 palmas_usb->vbus_gpiod = devm_gpiod_get_optional(&pdev->dev, "vbus",
216 GPIOD_IN);
H. Nikolaus Schaller3426ad62020-02-17 14:38:15 +0100217 if (PTR_ERR(palmas_usb->vbus_gpiod) == -EPROBE_DEFER) {
218 return -EPROBE_DEFER;
219 } else if (IS_ERR(palmas_usb->vbus_gpiod)) {
Felipe Balbib7aad8e2015-12-30 09:55:45 +0900220 dev_err(&pdev->dev, "failed to get vbus gpio\n");
221 return PTR_ERR(palmas_usb->vbus_gpiod);
222 }
223
Roger Quadros92b7cb52015-08-03 17:40:51 +0300224 if (palmas_usb->enable_id_detection && palmas_usb->id_gpiod) {
225 palmas_usb->enable_id_detection = false;
226 palmas_usb->enable_gpio_id_detection = true;
227 }
228
Felipe Balbib7aad8e2015-12-30 09:55:45 +0900229 if (palmas_usb->enable_vbus_detection && palmas_usb->vbus_gpiod) {
230 palmas_usb->enable_vbus_detection = false;
231 palmas_usb->enable_gpio_vbus_detection = true;
232 }
233
Roger Quadros92b7cb52015-08-03 17:40:51 +0300234 if (palmas_usb->enable_gpio_id_detection) {
235 u32 debounce;
236
237 if (of_property_read_u32(node, "debounce-delay-ms", &debounce))
238 debounce = USB_GPIO_DEBOUNCE_MS;
239
240 status = gpiod_set_debounce(palmas_usb->id_gpiod,
241 debounce * 1000);
242 if (status < 0)
243 palmas_usb->sw_debounce_jiffies = msecs_to_jiffies(debounce);
244 }
245
246 INIT_DELAYED_WORK(&palmas_usb->wq_detectid, palmas_gpio_id_detect);
247
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900248 palmas->usb = palmas_usb;
249 palmas_usb->palmas = palmas;
250
251 palmas_usb->dev = &pdev->dev;
252
Laxman Dewangan7281e052013-07-10 14:59:06 +0530253 palmas_usb_wakeup(palmas, palmas_usb->wakeup);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900254
255 platform_set_drvdata(pdev, palmas_usb);
256
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900257 palmas_usb->edev = devm_extcon_dev_allocate(&pdev->dev,
258 palmas_extcon_cable);
259 if (IS_ERR(palmas_usb->edev)) {
260 dev_err(&pdev->dev, "failed to allocate extcon device\n");
261 return -ENOMEM;
262 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900263
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900264 status = devm_extcon_dev_register(&pdev->dev, palmas_usb->edev);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900265 if (status) {
266 dev_err(&pdev->dev, "failed to register extcon device\n");
267 return status;
268 }
269
Laxman Dewangan7281e052013-07-10 14:59:06 +0530270 if (palmas_usb->enable_id_detection) {
Roger Quadros92b7cb52015-08-03 17:40:51 +0300271 palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data,
272 PALMAS_ID_OTG_IRQ);
273 palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data,
274 PALMAS_ID_IRQ);
Laxman Dewangan7281e052013-07-10 14:59:06 +0530275 status = devm_request_threaded_irq(palmas_usb->dev,
276 palmas_usb->id_irq,
277 NULL, palmas_id_irq_handler,
278 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
Grygorii Strashkoae64e422016-02-26 17:42:51 +0200279 IRQF_ONESHOT,
Laxman Dewangan7281e052013-07-10 14:59:06 +0530280 "palmas_usb_id", palmas_usb);
281 if (status < 0) {
282 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900283 palmas_usb->id_irq, status);
Sangjung Woob4dad552014-04-21 19:10:14 +0900284 return status;
Laxman Dewangan7281e052013-07-10 14:59:06 +0530285 }
Roger Quadros92b7cb52015-08-03 17:40:51 +0300286 } else if (palmas_usb->enable_gpio_id_detection) {
287 palmas_usb->gpio_id_irq = gpiod_to_irq(palmas_usb->id_gpiod);
288 if (palmas_usb->gpio_id_irq < 0) {
289 dev_err(&pdev->dev, "failed to get id irq\n");
290 return palmas_usb->gpio_id_irq;
291 }
292 status = devm_request_threaded_irq(&pdev->dev,
293 palmas_usb->gpio_id_irq,
294 NULL,
295 palmas_gpio_id_irq_handler,
296 IRQF_TRIGGER_RISING |
297 IRQF_TRIGGER_FALLING |
298 IRQF_ONESHOT,
299 "palmas_usb_id",
300 palmas_usb);
301 if (status < 0) {
302 dev_err(&pdev->dev,
303 "failed to request handler for id irq\n");
304 return status;
305 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900306 }
307
Laxman Dewangan7281e052013-07-10 14:59:06 +0530308 if (palmas_usb->enable_vbus_detection) {
Roger Quadros92b7cb52015-08-03 17:40:51 +0300309 palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data,
310 PALMAS_VBUS_OTG_IRQ);
311 palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data,
312 PALMAS_VBUS_IRQ);
Laxman Dewangan7281e052013-07-10 14:59:06 +0530313 status = devm_request_threaded_irq(palmas_usb->dev,
314 palmas_usb->vbus_irq, NULL,
315 palmas_vbus_irq_handler,
316 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
Grygorii Strashkoae64e422016-02-26 17:42:51 +0200317 IRQF_ONESHOT,
Laxman Dewangan7281e052013-07-10 14:59:06 +0530318 "palmas_usb_vbus", palmas_usb);
319 if (status < 0) {
320 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900321 palmas_usb->vbus_irq, status);
Sangjung Woob4dad552014-04-21 19:10:14 +0900322 return status;
Laxman Dewangan7281e052013-07-10 14:59:06 +0530323 }
Felipe Balbib7aad8e2015-12-30 09:55:45 +0900324 } else if (palmas_usb->enable_gpio_vbus_detection) {
325 /* remux GPIO_1 as VBUSDET */
326 status = palmas_update_bits(palmas,
327 PALMAS_PU_PD_OD_BASE,
328 PALMAS_PRIMARY_SECONDARY_PAD1,
329 PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK,
330 (1 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT));
331 if (status < 0) {
332 dev_err(&pdev->dev, "can't remux GPIO1\n");
333 return status;
334 }
335
336 palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data,
337 PALMAS_VBUS_OTG_IRQ);
338 palmas_usb->gpio_vbus_irq = gpiod_to_irq(palmas_usb->vbus_gpiod);
339 if (palmas_usb->gpio_vbus_irq < 0) {
340 dev_err(&pdev->dev, "failed to get vbus irq\n");
341 return palmas_usb->gpio_vbus_irq;
342 }
343 status = devm_request_threaded_irq(&pdev->dev,
344 palmas_usb->gpio_vbus_irq,
345 NULL,
346 palmas_vbus_irq_handler,
347 IRQF_TRIGGER_FALLING |
348 IRQF_TRIGGER_RISING |
Nishanth Menonf75587b2016-04-01 08:52:47 -0500349 IRQF_ONESHOT,
Felipe Balbib7aad8e2015-12-30 09:55:45 +0900350 "palmas_usb_vbus",
351 palmas_usb);
352 if (status < 0) {
353 dev_err(&pdev->dev,
354 "failed to request handler for vbus irq\n");
355 return status;
356 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900357 }
358
359 palmas_enable_irq(palmas_usb);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300360 /* perform initial detection */
Roger Quadros62e6d1e2016-06-15 11:12:05 +0300361 if (palmas_usb->enable_gpio_vbus_detection)
362 palmas_vbus_irq_handler(palmas_usb->gpio_vbus_irq, palmas_usb);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300363 palmas_gpio_id_detect(&palmas_usb->wq_detectid.work);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530364 device_set_wakeup_capable(&pdev->dev, true);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900365 return 0;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900366}
367
Roger Quadros92b7cb52015-08-03 17:40:51 +0300368static int palmas_usb_remove(struct platform_device *pdev)
369{
370 struct palmas_usb *palmas_usb = platform_get_drvdata(pdev);
371
372 cancel_delayed_work_sync(&palmas_usb->wq_detectid);
373
374 return 0;
375}
376
Laxman Dewangan024783e2013-07-09 18:34:23 +0530377#ifdef CONFIG_PM_SLEEP
378static int palmas_usb_suspend(struct device *dev)
379{
380 struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
381
382 if (device_may_wakeup(dev)) {
Laxman Dewangan7281e052013-07-10 14:59:06 +0530383 if (palmas_usb->enable_vbus_detection)
384 enable_irq_wake(palmas_usb->vbus_irq);
Felipe Balbib7aad8e2015-12-30 09:55:45 +0900385 if (palmas_usb->enable_gpio_vbus_detection)
386 enable_irq_wake(palmas_usb->gpio_vbus_irq);
Laxman Dewangan7281e052013-07-10 14:59:06 +0530387 if (palmas_usb->enable_id_detection)
388 enable_irq_wake(palmas_usb->id_irq);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300389 if (palmas_usb->enable_gpio_id_detection)
390 enable_irq_wake(palmas_usb->gpio_id_irq);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530391 }
392 return 0;
393}
394
395static int palmas_usb_resume(struct device *dev)
396{
397 struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
398
399 if (device_may_wakeup(dev)) {
Laxman Dewangan7281e052013-07-10 14:59:06 +0530400 if (palmas_usb->enable_vbus_detection)
401 disable_irq_wake(palmas_usb->vbus_irq);
Felipe Balbib7aad8e2015-12-30 09:55:45 +0900402 if (palmas_usb->enable_gpio_vbus_detection)
403 disable_irq_wake(palmas_usb->gpio_vbus_irq);
Laxman Dewangan7281e052013-07-10 14:59:06 +0530404 if (palmas_usb->enable_id_detection)
405 disable_irq_wake(palmas_usb->id_irq);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300406 if (palmas_usb->enable_gpio_id_detection)
407 disable_irq_wake(palmas_usb->gpio_id_irq);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530408 }
Roger Quadros01944322017-02-15 14:31:29 +0200409
410 /* check if GPIO states changed while suspend/resume */
411 if (palmas_usb->enable_gpio_vbus_detection)
412 palmas_vbus_irq_handler(palmas_usb->gpio_vbus_irq, palmas_usb);
413 palmas_gpio_id_detect(&palmas_usb->wq_detectid.work);
414
Laxman Dewangan024783e2013-07-09 18:34:23 +0530415 return 0;
416};
417#endif
418
Jingoo Han8d4288f2014-02-27 20:36:15 +0900419static SIMPLE_DEV_PM_OPS(palmas_pm_ops, palmas_usb_suspend, palmas_usb_resume);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530420
Krzysztof Kozlowski3bc2ac72014-05-23 11:03:05 +0200421static const struct of_device_id of_palmas_match_tbl[] = {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900422 { .compatible = "ti,palmas-usb", },
Kishon Vijay Abraham I5fbc77d2013-10-10 18:07:27 +0530423 { .compatible = "ti,palmas-usb-vid", },
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900424 { .compatible = "ti,twl6035-usb", },
Kishon Vijay Abraham I5fbc77d2013-10-10 18:07:27 +0530425 { .compatible = "ti,twl6035-usb-vid", },
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900426 { /* end */ }
427};
428
429static struct platform_driver palmas_usb_driver = {
430 .probe = palmas_usb_probe,
Roger Quadros92b7cb52015-08-03 17:40:51 +0300431 .remove = palmas_usb_remove,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900432 .driver = {
433 .name = "palmas-usb",
434 .of_match_table = of_palmas_match_tbl,
Laxman Dewangan024783e2013-07-09 18:34:23 +0530435 .pm = &palmas_pm_ops,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900436 },
437};
438
439module_platform_driver(palmas_usb_driver);
440
441MODULE_ALIAS("platform:palmas-usb");
442MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
443MODULE_DESCRIPTION("Palmas USB transceiver driver");
444MODULE_LICENSE("GPL");
445MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);