blob: 11c6757b6c4051b8a4198ecf6b2631591f264e9d [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>
31
32static const char *palmas_extcon_cable[] = {
33 [0] = "USB",
34 [1] = "USB-HOST",
35 NULL,
36};
37
38static const int mutually_exclusive[] = {0x3, 0x0};
39
40static void palmas_usb_wakeup(struct palmas *palmas, int enable)
41{
42 if (enable)
43 palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP,
44 PALMAS_USB_WAKEUP_ID_WK_UP_COMP);
45 else
46 palmas_write(palmas, PALMAS_USB_OTG_BASE, PALMAS_USB_WAKEUP, 0);
47}
48
49static irqreturn_t palmas_vbus_irq_handler(int irq, void *_palmas_usb)
50{
51 struct palmas_usb *palmas_usb = _palmas_usb;
52 unsigned int vbus_line_state;
53
54 palmas_read(palmas_usb->palmas, PALMAS_INTERRUPT_BASE,
55 PALMAS_INT3_LINE_STATE, &vbus_line_state);
56
57 if (vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS) {
58 if (palmas_usb->linkstat != PALMAS_USB_STATE_VBUS) {
59 palmas_usb->linkstat = PALMAS_USB_STATE_VBUS;
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +090060 extcon_set_cable_state(palmas_usb->edev, "USB", true);
Laxman Dewangan002945f2013-07-09 18:34:22 +053061 dev_info(palmas_usb->dev, "USB cable is attached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090062 } else {
63 dev_dbg(palmas_usb->dev,
64 "Spurious connect event detected\n");
65 }
66 } else if (!(vbus_line_state & PALMAS_INT3_LINE_STATE_VBUS)) {
67 if (palmas_usb->linkstat == PALMAS_USB_STATE_VBUS) {
68 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +090069 extcon_set_cable_state(palmas_usb->edev, "USB", false);
Laxman Dewangan002945f2013-07-09 18:34:22 +053070 dev_info(palmas_usb->dev, "USB cable is detached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090071 } else {
72 dev_dbg(palmas_usb->dev,
73 "Spurious disconnect event detected\n");
74 }
75 }
76
77 return IRQ_HANDLED;
78}
79
80static irqreturn_t palmas_id_irq_handler(int irq, void *_palmas_usb)
81{
George Cherianb7d2d5b2013-10-11 00:18:39 +053082 unsigned int set, id_src;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090083 struct palmas_usb *palmas_usb = _palmas_usb;
84
85 palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
86 PALMAS_USB_ID_INT_LATCH_SET, &set);
George Cherianb7d2d5b2013-10-11 00:18:39 +053087 palmas_read(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
88 PALMAS_USB_ID_INT_SRC, &id_src);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090089
George Cherianb7d2d5b2013-10-11 00:18:39 +053090 if ((set & PALMAS_USB_ID_INT_SRC_ID_GND) &&
91 (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090092 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +090093 PALMAS_USB_ID_INT_LATCH_CLR,
94 PALMAS_USB_ID_INT_EN_HI_CLR_ID_GND);
95 palmas_usb->linkstat = PALMAS_USB_STATE_ID;
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +090096 extcon_set_cable_state(palmas_usb->edev, "USB-HOST", true);
Laxman Dewangan002945f2013-07-09 18:34:22 +053097 dev_info(palmas_usb->dev, "USB-HOST cable is attached\n");
George Cherianb7d2d5b2013-10-11 00:18:39 +053098 } else if ((set & PALMAS_USB_ID_INT_SRC_ID_FLOAT) &&
99 (id_src & PALMAS_USB_ID_INT_SRC_ID_FLOAT)) {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900100 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900101 PALMAS_USB_ID_INT_LATCH_CLR,
102 PALMAS_USB_ID_INT_EN_HI_CLR_ID_FLOAT);
103 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900104 extcon_set_cable_state(palmas_usb->edev, "USB-HOST", false);
Laxman Dewangan002945f2013-07-09 18:34:22 +0530105 dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
106 } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_ID) &&
107 (!(set & PALMAS_USB_ID_INT_SRC_ID_GND))) {
108 palmas_usb->linkstat = PALMAS_USB_STATE_DISCONNECT;
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900109 extcon_set_cable_state(palmas_usb->edev, "USB-HOST", false);
Laxman Dewangan002945f2013-07-09 18:34:22 +0530110 dev_info(palmas_usb->dev, "USB-HOST cable is detached\n");
George Cherianb7d2d5b2013-10-11 00:18:39 +0530111 } else if ((palmas_usb->linkstat == PALMAS_USB_STATE_DISCONNECT) &&
112 (id_src & PALMAS_USB_ID_INT_SRC_ID_GND)) {
113 palmas_usb->linkstat = PALMAS_USB_STATE_ID;
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900114 extcon_set_cable_state(palmas_usb->edev, "USB-HOST", true);
George Cherianb7d2d5b2013-10-11 00:18:39 +0530115 dev_info(palmas_usb->dev, " USB-HOST cable is attached\n");
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900116 }
117
118 return IRQ_HANDLED;
119}
120
121static void palmas_enable_irq(struct palmas_usb *palmas_usb)
122{
123 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
124 PALMAS_USB_VBUS_CTRL_SET,
125 PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP);
126
127 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
128 PALMAS_USB_ID_CTRL_SET, PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP);
129
130 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE,
131 PALMAS_USB_ID_INT_EN_HI_SET,
Laxman Dewangan002945f2013-07-09 18:34:22 +0530132 PALMAS_USB_ID_INT_EN_HI_SET_ID_GND |
133 PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900134
Laxman Dewangan7281e052013-07-10 14:59:06 +0530135 if (palmas_usb->enable_vbus_detection)
136 palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900137
138 /* cold plug for host mode needs this delay */
Laxman Dewangan7281e052013-07-10 14:59:06 +0530139 if (palmas_usb->enable_id_detection) {
140 msleep(30);
141 palmas_id_irq_handler(palmas_usb->id_irq, palmas_usb);
142 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900143}
144
145static int palmas_usb_probe(struct platform_device *pdev)
146{
147 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
Jingoo Han7c0f65582013-09-11 13:22:18 +0900148 struct palmas_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900149 struct device_node *node = pdev->dev.of_node;
150 struct palmas_usb *palmas_usb;
151 int status;
152
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900153 palmas_usb = devm_kzalloc(&pdev->dev, sizeof(*palmas_usb), GFP_KERNEL);
154 if (!palmas_usb)
155 return -ENOMEM;
156
Laxman Dewangan7281e052013-07-10 14:59:06 +0530157 if (node && !pdata) {
158 palmas_usb->wakeup = of_property_read_bool(node, "ti,wakeup");
159 palmas_usb->enable_id_detection = of_property_read_bool(node,
160 "ti,enable-id-detection");
161 palmas_usb->enable_vbus_detection = of_property_read_bool(node,
162 "ti,enable-vbus-detection");
163 } else {
164 palmas_usb->wakeup = true;
165 palmas_usb->enable_id_detection = true;
166 palmas_usb->enable_vbus_detection = true;
167
168 if (pdata)
169 palmas_usb->wakeup = pdata->wakeup;
170 }
171
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900172 palmas->usb = palmas_usb;
173 palmas_usb->palmas = palmas;
174
175 palmas_usb->dev = &pdev->dev;
176
177 palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data,
178 PALMAS_ID_OTG_IRQ);
179 palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data,
180 PALMAS_ID_IRQ);
181 palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data,
182 PALMAS_VBUS_OTG_IRQ);
183 palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data,
184 PALMAS_VBUS_IRQ);
185
Laxman Dewangan7281e052013-07-10 14:59:06 +0530186 palmas_usb_wakeup(palmas, palmas_usb->wakeup);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900187
188 platform_set_drvdata(pdev, palmas_usb);
189
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900190 palmas_usb->edev = devm_extcon_dev_allocate(&pdev->dev,
191 palmas_extcon_cable);
192 if (IS_ERR(palmas_usb->edev)) {
193 dev_err(&pdev->dev, "failed to allocate extcon device\n");
194 return -ENOMEM;
195 }
196 palmas_usb->edev->name = kstrdup(node->name, GFP_KERNEL);
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900197 palmas_usb->edev->mutually_exclusive = mutually_exclusive;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900198
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900199 status = devm_extcon_dev_register(&pdev->dev, palmas_usb->edev);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900200 if (status) {
201 dev_err(&pdev->dev, "failed to register extcon device\n");
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900202 kfree(palmas_usb->edev->name);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900203 return status;
204 }
205
Laxman Dewangan7281e052013-07-10 14:59:06 +0530206 if (palmas_usb->enable_id_detection) {
207 status = devm_request_threaded_irq(palmas_usb->dev,
208 palmas_usb->id_irq,
209 NULL, palmas_id_irq_handler,
210 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
211 IRQF_ONESHOT | IRQF_EARLY_RESUME,
212 "palmas_usb_id", palmas_usb);
213 if (status < 0) {
214 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900215 palmas_usb->id_irq, status);
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900216 kfree(palmas_usb->edev->name);
Sangjung Woob4dad552014-04-21 19:10:14 +0900217 return status;
Laxman Dewangan7281e052013-07-10 14:59:06 +0530218 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900219 }
220
Laxman Dewangan7281e052013-07-10 14:59:06 +0530221 if (palmas_usb->enable_vbus_detection) {
222 status = devm_request_threaded_irq(palmas_usb->dev,
223 palmas_usb->vbus_irq, NULL,
224 palmas_vbus_irq_handler,
225 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
226 IRQF_ONESHOT | IRQF_EARLY_RESUME,
227 "palmas_usb_vbus", palmas_usb);
228 if (status < 0) {
229 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900230 palmas_usb->vbus_irq, status);
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900231 kfree(palmas_usb->edev->name);
Sangjung Woob4dad552014-04-21 19:10:14 +0900232 return status;
Laxman Dewangan7281e052013-07-10 14:59:06 +0530233 }
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900234 }
235
236 palmas_enable_irq(palmas_usb);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530237 device_set_wakeup_capable(&pdev->dev, true);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900238 return 0;
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900239}
240
241static int palmas_usb_remove(struct platform_device *pdev)
242{
243 struct palmas_usb *palmas_usb = platform_get_drvdata(pdev);
244
Chanwoo Choi3f79a3f2014-04-21 20:44:53 +0900245 kfree(palmas_usb->edev->name);
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900246
247 return 0;
248}
249
Laxman Dewangan024783e2013-07-09 18:34:23 +0530250#ifdef CONFIG_PM_SLEEP
251static int palmas_usb_suspend(struct device *dev)
252{
253 struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
254
255 if (device_may_wakeup(dev)) {
Laxman Dewangan7281e052013-07-10 14:59:06 +0530256 if (palmas_usb->enable_vbus_detection)
257 enable_irq_wake(palmas_usb->vbus_irq);
258 if (palmas_usb->enable_id_detection)
259 enable_irq_wake(palmas_usb->id_irq);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530260 }
261 return 0;
262}
263
264static int palmas_usb_resume(struct device *dev)
265{
266 struct palmas_usb *palmas_usb = dev_get_drvdata(dev);
267
268 if (device_may_wakeup(dev)) {
Laxman Dewangan7281e052013-07-10 14:59:06 +0530269 if (palmas_usb->enable_vbus_detection)
270 disable_irq_wake(palmas_usb->vbus_irq);
271 if (palmas_usb->enable_id_detection)
272 disable_irq_wake(palmas_usb->id_irq);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530273 }
274 return 0;
275};
276#endif
277
Jingoo Han8d4288f2014-02-27 20:36:15 +0900278static SIMPLE_DEV_PM_OPS(palmas_pm_ops, palmas_usb_suspend, palmas_usb_resume);
Laxman Dewangan024783e2013-07-09 18:34:23 +0530279
Krzysztof Kozlowski3bc2ac72014-05-23 11:03:05 +0200280static const struct of_device_id of_palmas_match_tbl[] = {
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900281 { .compatible = "ti,palmas-usb", },
Kishon Vijay Abraham I5fbc77d2013-10-10 18:07:27 +0530282 { .compatible = "ti,palmas-usb-vid", },
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900283 { .compatible = "ti,twl6035-usb", },
Kishon Vijay Abraham I5fbc77d2013-10-10 18:07:27 +0530284 { .compatible = "ti,twl6035-usb-vid", },
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900285 { /* end */ }
286};
287
288static struct platform_driver palmas_usb_driver = {
289 .probe = palmas_usb_probe,
290 .remove = palmas_usb_remove,
291 .driver = {
292 .name = "palmas-usb",
293 .of_match_table = of_palmas_match_tbl,
Laxman Dewangan024783e2013-07-09 18:34:23 +0530294 .pm = &palmas_pm_ops,
Graeme Gregoryb1f254e2013-05-28 10:50:11 +0900295 },
296};
297
298module_platform_driver(palmas_usb_driver);
299
300MODULE_ALIAS("platform:palmas-usb");
301MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
302MODULE_DESCRIPTION("Palmas USB transceiver driver");
303MODULE_LICENSE("GPL");
304MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);