blob: 93c30a885740e9bebb08c8f80162118c7e3f2359 [file] [log] [blame]
Graeme Gregoryb1f254e2013-05-28 10:50:11 +09001/*
2 * Palmas USB transceiver driver
3 *
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Author: Graeme Gregory <gg@slimlogic.co.uk>
11 * Author: Kishon Vijay Abraham I <kishon@ti.com>
12 *
13 * Based on twl6030_usb.c
14 *
15 * Author: Hema HK <hemahk@ti.com>
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 */
22
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
Kishon Vijay Abraham If63d32d2014-03-28 12:21:02 +053026#include <linux/slab.h>
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090027#include <linux/err.h>
28#include <linux/mfd/palmas.h>
29#include <linux/of.h>
30#include <linux/of_platform.h>
Roger Quadros92b7cb52015-08-03 17:40:51 +030031#include <linux/of_gpio.h>
Chanwoo Choiac22a1d2015-08-13 09:19:43 +090032#include <linux/gpio/consumer.h>
Roger Quadros92b7cb52015-08-03 17:40:51 +030033#include <linux/workqueue.h>
34
35#define USB_GPIO_DEBOUNCE_MS 20 /* ms */
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090036
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090037static const unsigned int palmas_extcon_cable[] = {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090038 EXTCON_USB,
39 EXTCON_USB_HOST,
40 EXTCON_NONE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090041};
42
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090043static void palmas_usb_wakeup(struct palmas *palmas, int enable)
44{
45 if (enable)
46 palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP,
47 PALMAS_USB_WAKEUP_ID_WK_UP_COMP);
48 else
49 palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, 0);
50}
51
52static irqreturn_t palmas_vbus_irq_handler(int irq, void *_palmas_usb)
53{
54 struct palmas_usb *palmas_usb = _palmas_usb;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090055 struct extcon_dev *edev = palmas_usb->edev;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090056 unsigned int vbus_line_state;
57
58 palmas_read(palmas_usb->palmas, PALMAS_INTERRUPT_BASE,
59 PALMAS_INT3_LINE_STATE, &vbus_line_state);
60
61 if (vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS) {
62 if (palmas_usb->linkstat != PALMAS_USB_STATE_VBUS) {
63 palmas_usb->linkstat = PALMAS_USB_STATE_VBUS;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090064 extcon_set_cable_state_(edev, EXTCON_USB, true);
Laxman Dewangan002945f2013-07-09 18:34:22 +053065 dev_info(palmas_usb->dev, "USB cable is attached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090066 } else {
67 dev_dbg(palmas_usb->dev,
68 "Spurious connect event detected\n");
69 }
70 } else if (!(vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS)) {
71 if (palmas_usb->linkstat == PALMAS_USB_STATE_VBUS) {
72 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090073 extcon_set_cable_state_(edev, EXTCON_USB, false);
Laxman Dewangan002945f2013-07-09 18:34:22 +053074 dev_info(palmas_usb->dev, "USB cable is detached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090075 } else {
76 dev_dbg(palmas_usb->dev,
77 "Spurious disconnect event detected\n");
78 }
79 }
80
81 return IRQ_HANDLED;
82}
83
84static irqreturn_t palmas_id_irq_handler(int irq, void *_palmas_usb)
85{
George Cherianb7d2d5b2013-10-11 00:18:39 +053086 unsigned int set, id_src;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090087 struct palmas_usb *palmas_usb = _palmas_usb;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090088 struct extcon_dev *edev = palmas_usb->edev;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090089
90 palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
91 PALMAS_USB_ID_INT_LATCH_SET, &set);
George Cherianb7d2d5b2013-10-11 00:18:39 +053092 palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
93 PALMAS_USB_ID_INT_SRC, &id_src);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090094
George Cherianb7d2d5b2013-10-11 00:18:39 +053095 if ((set & PALMAS_USB_ID_INT_SRC_ID_GND) &&
96 (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090097 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090098 PALMAS_USB_ID_INT_LATCH_CLR,
99 PALMAS_USB_ID_INT_EN_HI_CLR_ID_GND);
100 palmas_usb->linkstat = PALMAS_USB_STATE_ID;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900101 extcon_set_cable_state_(edev, EXTCON_USB_HOST, true);
Laxman Dewangan002945f2013-07-09 18:34:22 +0530102 dev_info(palmas_usb->dev, "USB-HOST cable is attached\n");
George Cherianb7d2d5b2013-10-11 00:18:39 +0530103 } else if ((set & PALMAS_USB_ID_INT_SRC_ID_FLOAT) &&
104 (id_src & PALMAS_USB_ID_INT_SRC_ID_FLOAT)) {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900105 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900106 PALMAS_USB_ID_INT_LATCH_CLR,
107 PALMAS_USB_ID_INT_EN_HI_CLR_ID_FLOAT);
108 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900109 extcon_set_cable_state_(edev, EXTCON_USB_HOST, false);
Laxman Dewangan002945f2013-07-09 18:34:22 +0530110 dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
111 } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_ID) &&
112 (!(set & PALMAS_USB_ID_INT_SRC_ID_GND))) {
113 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900114 extcon_set_cable_state_(edev, EXTCON_USB_HOST, false);
Laxman Dewangan002945f2013-07-09 18:34:22 +0530115 dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
George Cherianb7d2d5b2013-10-11 00:18:39 +0530116 } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_DISCONNECT) &&
117 (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
118 palmas_usb->linkstat = PALMAS_USB_STATE_ID;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900119 extcon_set_cable_state_(edev, EXTCON_USB_HOST, true);
George Cherianb7d2d5b2013-10-11 00:18:39 +0530120 dev_info(palmas_usb->dev, " USB-HOST cable is attached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900121 }
122
123 return IRQ_HANDLED;
124}
125
Roger Quadros92b7cb52015-08-03 17:40:51 +0300126static void palmas_gpio_id_detect(struct work_struct *work)
127{
128 int id;
129 struct palmas_usb *palmas_usb = container_of(to_delayed_work(work),
130 struct palmas_usb,
131 wq_detectid);
132 struct extcon_dev *edev = palmas_usb->edev;
133
134 if (!palmas_usb->id_gpiod)
135 return;
136
137 id = gpiod_get_value_cansleep(palmas_usb->id_gpiod);
138
139 if (id) {
140 extcon_set_cable_state_(edev, EXTCON_USB_HOST, false);
141 dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
142 } else {
143 extcon_set_cable_state_(edev, EXTCON_USB_HOST, true);
144 dev_info(palmas_usb->dev, "USB-HOST cable is attached\n");
145 }
146}
147
148static irqreturn_t palmas_gpio_id_irq_handler(int irq, void *_palmas_usb)
149{
150 struct palmas_usb *palmas_usb = _palmas_usb;
151
152 queue_delayed_work(system_power_efficient_wq, &palmas_usb->wq_detectid,
153 palmas_usb->sw_debounce_jiffies);
154
155 return IRQ_HANDLED;
156}
157
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900158static void palmas_enable_irq(struct palmas_usb *palmas_usb)
159{
160 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
161 PALMAS_USB_VBUS_CTRL_SET,
162 PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP);
163
Roger Quadros92b7cb52015-08-03 17:40:51 +0300164 if (palmas_usb->enable_id_detection) {
165 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
166 PALMAS_USB_ID_CTRL_SET,
167 PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900168
Roger Quadros92b7cb52015-08-03 17:40:51 +0300169 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
170 PALMAS_USB_ID_INT_EN_HI_SET,
171 PALMAS_USB_ID_INT_EN_HI_SET_ID_GND |
172 PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT);
173 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900174
Laxman Dewangan7281e052013-07-10 14:59:06 +0530175 if (palmas_usb->enable_vbus_detection)
176 palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900177
178 /* cold plug for host mode needs this delay */
Laxman Dewangan7281e052013-07-10 14:59:06 +0530179 if (palmas_usb->enable_id_detection) {
180 msleep(30);
181 palmas_id_irq_handler(palmas_usb->id_irq, palmas_usb);
182 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900183}
184
185static int palmas_usb_probe(struct platform_device *pdev)
186{
187 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
Jingoo Han7c0f65582013-09-11 13:22:18 +0900188 struct palmas_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900189 struct device_node *node = pdev->dev.of_node;
190 struct palmas_usb *palmas_usb;
191 int status;
192
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900193 palmas_usb = devm_kzalloc(&pdev->dev, sizeof(*palmas_usb), GFP_KERNEL);
194 if (!palmas_usb)
195 return -ENOMEM;
196
Laxman Dewangan7281e052013-07-10 14:59:06 +0530197 if (node && !pdata) {
198 palmas_usb->wakeup = of_property_read_bool(node, "ti,wakeup");
199 palmas_usb->enable_id_detection = of_property_read_bool(node,
200 "ti,enable-id-detection");
201 palmas_usb->enable_vbus_detection = of_property_read_bool(node,
202 "ti,enable-vbus-detection");
203 } else {
204 palmas_usb->wakeup = true;
205 palmas_usb->enable_id_detection = true;
206 palmas_usb->enable_vbus_detection = true;
207
208 if (pdata)
209 palmas_usb->wakeup = pdata->wakeup;
210 }
211
Chanwoo Choiac22a1d2015-08-13 09:19:43 +0900212 palmas_usb->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id",
213 GPIOD_IN);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300214 if (IS_ERR(palmas_usb->id_gpiod)) {
215 dev_err(&pdev->dev, "failed to get id gpio\n");
216 return PTR_ERR(palmas_usb->id_gpiod);
217 }
218
219 if (palmas_usb->enable_id_detection && palmas_usb->id_gpiod) {
220 palmas_usb->enable_id_detection = false;
221 palmas_usb->enable_gpio_id_detection = true;
222 }
223
224 if (palmas_usb->enable_gpio_id_detection) {
225 u32 debounce;
226
227 if (of_property_read_u32(node, "debounce-delay-ms", &debounce))
228 debounce = USB_GPIO_DEBOUNCE_MS;
229
230 status = gpiod_set_debounce(palmas_usb->id_gpiod,
231 debounce * 1000);
232 if (status < 0)
233 palmas_usb->sw_debounce_jiffies = msecs_to_jiffies(debounce);
234 }
235
236 INIT_DELAYED_WORK(&palmas_usb->wq_detectid, palmas_gpio_id_detect);
237
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900238 palmas->usb = palmas_usb;
239 palmas_usb->palmas = palmas;
240
241 palmas_usb->dev = &pdev->dev;
242
Laxman Dewangan7281e052013-07-10 14:59:06 +0530243 palmas_usb_wakeup(palmas, palmas_usb->wakeup);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900244
245 platform_set_drvdata(pdev, palmas_usb);
246
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900247 palmas_usb->edev = devm_extcon_dev_allocate(&pdev->dev,
248 palmas_extcon_cable);
249 if (IS_ERR(palmas_usb->edev)) {
250 dev_err(&pdev->dev, "failed to allocate extcon device\n");
251 return -ENOMEM;
252 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900253
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900254 status = devm_extcon_dev_register(&pdev->dev, palmas_usb->edev);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900255 if (status) {
256 dev_err(&pdev->dev, "failed to register extcon device\n");
257 return status;
258 }
259
Laxman Dewangan7281e052013-07-10 14:59:06 +0530260 if (palmas_usb->enable_id_detection) {
Roger Quadros92b7cb52015-08-03 17:40:51 +0300261 palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data,
262 PALMAS_ID_OTG_IRQ);
263 palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data,
264 PALMAS_ID_IRQ);
Laxman Dewangan7281e052013-07-10 14:59:06 +0530265 status = devm_request_threaded_irq(palmas_usb->dev,
266 palmas_usb->id_irq,
267 NULL, palmas_id_irq_handler,
268 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
269 IRQF_ONESHOT | IRQF_EARLY_RESUME,
270 "palmas_usb_id", palmas_usb);
271 if (status < 0) {
272 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900273 palmas_usb->id_irq, status);
Sangjung Woob4dad552014-04-21 19:10:14 +0900274 return status;
Laxman Dewangan7281e052013-07-10 14:59:06 +0530275 }
Roger Quadros92b7cb52015-08-03 17:40:51 +0300276 } else if (palmas_usb->enable_gpio_id_detection) {
277 palmas_usb->gpio_id_irq = gpiod_to_irq(palmas_usb->id_gpiod);
278 if (palmas_usb->gpio_id_irq < 0) {
279 dev_err(&pdev->dev, "failed to get id irq\n");
280 return palmas_usb->gpio_id_irq;
281 }
282 status = devm_request_threaded_irq(&pdev->dev,
283 palmas_usb->gpio_id_irq,
284 NULL,
285 palmas_gpio_id_irq_handler,
286 IRQF_TRIGGER_RISING |
287 IRQF_TRIGGER_FALLING |
288 IRQF_ONESHOT,
289 "palmas_usb_id",
290 palmas_usb);
291 if (status < 0) {
292 dev_err(&pdev->dev,
293 "failed to request handler for id irq\n");
294 return status;
295 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900296 }
297
Laxman Dewangan7281e052013-07-10 14:59:06 +0530298 if (palmas_usb->enable_vbus_detection) {
Roger Quadros92b7cb52015-08-03 17:40:51 +0300299 palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data,
300 PALMAS_VBUS_OTG_IRQ);
301 palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data,
302 PALMAS_VBUS_IRQ);
Laxman Dewangan7281e052013-07-10 14:59:06 +0530303 status = devm_request_threaded_irq(palmas_usb->dev,
304 palmas_usb->vbus_irq, NULL,
305 palmas_vbus_irq_handler,
306 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
307 IRQF_ONESHOT | IRQF_EARLY_RESUME,
308 "palmas_usb_vbus", palmas_usb);
309 if (status < 0) {
310 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900311 palmas_usb->vbus_irq, status);
Sangjung Woob4dad552014-04-21 19:10:14 +0900312 return status;
Laxman Dewangan7281e052013-07-10 14:59:06 +0530313 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900314 }
315
316 palmas_enable_irq(palmas_usb);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300317 /* perform initial detection */
318 palmas_gpio_id_detect(&palmas_usb->wq_detectid.work);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530319 device_set_wakeup_capable(&pdev->dev, true);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900320 return 0;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900321}
322
Roger Quadros92b7cb52015-08-03 17:40:51 +0300323static int palmas_usb_remove(struct platform_device *pdev)
324{
325 struct palmas_usb *palmas_usb = platform_get_drvdata(pdev);
326
327 cancel_delayed_work_sync(&palmas_usb->wq_detectid);
328
329 return 0;
330}
331
Laxman Dewangan024783e2013-07-09 18:34:23 +0530332#ifdef CONFIG_PM_SLEEP
333static int palmas_usb_suspend(struct device *dev)
334{
335 struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
336
337 if (device_may_wakeup(dev)) {
Laxman Dewangan7281e052013-07-10 14:59:06 +0530338 if (palmas_usb->enable_vbus_detection)
339 enable_irq_wake(palmas_usb->vbus_irq);
340 if (palmas_usb->enable_id_detection)
341 enable_irq_wake(palmas_usb->id_irq);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300342 if (palmas_usb->enable_gpio_id_detection)
343 enable_irq_wake(palmas_usb->gpio_id_irq);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530344 }
345 return 0;
346}
347
348static int palmas_usb_resume(struct device *dev)
349{
350 struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
351
352 if (device_may_wakeup(dev)) {
Laxman Dewangan7281e052013-07-10 14:59:06 +0530353 if (palmas_usb->enable_vbus_detection)
354 disable_irq_wake(palmas_usb->vbus_irq);
355 if (palmas_usb->enable_id_detection)
356 disable_irq_wake(palmas_usb->id_irq);
Roger Quadros92b7cb52015-08-03 17:40:51 +0300357 if (palmas_usb->enable_gpio_id_detection)
358 disable_irq_wake(palmas_usb->gpio_id_irq);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530359 }
360 return 0;
361};
362#endif
363
Jingoo Han8d4288f2014-02-27 20:36:15 +0900364static SIMPLE_DEV_PM_OPS(palmas_pm_ops, palmas_usb_suspend, palmas_usb_resume);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530365
Krzysztof Kozlowski3bc2ac72014-05-23 11:03:05 +0200366static const struct of_device_id of_palmas_match_tbl[] = {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900367 { .compatible = "ti,palmas-usb", },
Kishon Vijay Abraham I5fbc77d2013-10-10 18:07:27 +0530368 { .compatible = "ti,palmas-usb-vid", },
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900369 { .compatible = "ti,twl6035-usb", },
Kishon Vijay Abraham I5fbc77d2013-10-10 18:07:27 +0530370 { .compatible = "ti,twl6035-usb-vid", },
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900371 { /* end */ }
372};
373
374static struct platform_driver palmas_usb_driver = {
375 .probe = palmas_usb_probe,
Roger Quadros92b7cb52015-08-03 17:40:51 +0300376 .remove = palmas_usb_remove,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900377 .driver = {
378 .name = "palmas-usb",
379 .of_match_table = of_palmas_match_tbl,
Laxman Dewangan024783e2013-07-09 18:34:23 +0530380 .pm = &palmas_pm_ops,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900381 },
382};
383
384module_platform_driver(palmas_usb_driver);
385
386MODULE_ALIAS("platform:palmas-usb");
387MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
388MODULE_DESCRIPTION("Palmas USB transceiver driver");
389MODULE_LICENSE("GPL");
390MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);