USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / phy / phy-twl6030-usb.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
c33fad0c
HH
2/*
3 * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver.
4 *
5 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Author: Hema HK <hemahk@ti.com>
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/platform_device.h>
28#include <linux/io.h>
8055555f 29#include <linux/usb/musb.h>
0e98de67 30#include <linux/usb/phy_companion.h>
6284be23 31#include <linux/phy/omap_usb.h>
a2054256 32#include <linux/mfd/twl.h>
c33fad0c
HH
33#include <linux/regulator/consumer.h>
34#include <linux/err.h>
c33fad0c 35#include <linux/slab.h>
603ab524 36#include <linux/delay.h>
80d7d8a7 37#include <linux/of.h>
c33fad0c
HH
38
39/* usb register definitions */
40#define USB_VENDOR_ID_LSB 0x00
41#define USB_VENDOR_ID_MSB 0x01
42#define USB_PRODUCT_ID_LSB 0x02
43#define USB_PRODUCT_ID_MSB 0x03
44#define USB_VBUS_CTRL_SET 0x04
45#define USB_VBUS_CTRL_CLR 0x05
46#define USB_ID_CTRL_SET 0x06
47#define USB_ID_CTRL_CLR 0x07
48#define USB_VBUS_INT_SRC 0x08
49#define USB_VBUS_INT_LATCH_SET 0x09
50#define USB_VBUS_INT_LATCH_CLR 0x0A
51#define USB_VBUS_INT_EN_LO_SET 0x0B
52#define USB_VBUS_INT_EN_LO_CLR 0x0C
53#define USB_VBUS_INT_EN_HI_SET 0x0D
54#define USB_VBUS_INT_EN_HI_CLR 0x0E
55#define USB_ID_INT_SRC 0x0F
56#define USB_ID_INT_LATCH_SET 0x10
57#define USB_ID_INT_LATCH_CLR 0x11
58
59#define USB_ID_INT_EN_LO_SET 0x12
60#define USB_ID_INT_EN_LO_CLR 0x13
61#define USB_ID_INT_EN_HI_SET 0x14
62#define USB_ID_INT_EN_HI_CLR 0x15
63#define USB_OTG_ADP_CTRL 0x16
64#define USB_OTG_ADP_HIGH 0x17
65#define USB_OTG_ADP_LOW 0x18
66#define USB_OTG_ADP_RISE 0x19
67#define USB_OTG_REVISION 0x1A
68
69/* to be moved to LDO */
70#define TWL6030_MISC2 0xE5
71#define TWL6030_CFG_LDO_PD2 0xF5
72#define TWL6030_BACKUP_REG 0xFA
73
74#define STS_HW_CONDITIONS 0x21
75
76/* In module TWL6030_MODULE_PM_MASTER */
77#define STS_HW_CONDITIONS 0x21
78#define STS_USB_ID BIT(2)
79
80/* In module TWL6030_MODULE_PM_RECEIVER */
81#define VUSB_CFG_TRANS 0x71
82#define VUSB_CFG_STATE 0x72
83#define VUSB_CFG_VOLTAGE 0x73
84
85/* in module TWL6030_MODULE_MAIN_CHARGE */
86
87#define CHARGERUSB_CTRL1 0x8
88
89#define CONTROLLER_STAT1 0x03
90#define VBUS_DET BIT(2)
91
92struct twl6030_usb {
0e98de67 93 struct phy_companion comparator;
c33fad0c
HH
94 struct device *dev;
95
96 /* for vbus reporting with irqs disabled */
97 spinlock_t lock;
98
99 struct regulator *usb3v3;
100
b6a619a8
TL
101 /* used to check initial cable status after probe */
102 struct delayed_work get_status_work;
103
5bf54506
MS
104 /* used to set vbus, in atomic path */
105 struct work_struct set_vbus_work;
106
c33fad0c
HH
107 int irq1;
108 int irq2;
8055555f 109 enum musb_vbus_id_status linkstat;
c33fad0c 110 u8 asleep;
5bf54506 111 bool vbus_enable;
c33fad0c
HH
112};
113
0e98de67 114#define comparator_to_twl(x) container_of((x), struct twl6030_usb, comparator)
c33fad0c
HH
115
116/*-------------------------------------------------------------------------*/
117
118static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
119 u8 data, u8 address)
120{
121 int ret = 0;
122
123 ret = twl_i2c_write_u8(module, data, address);
124 if (ret < 0)
125 dev_err(twl->dev,
126 "Write[0x%x] Error %d\n", address, ret);
127 return ret;
128}
129
130static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
131{
0b551490
DC
132 u8 data;
133 int ret;
c33fad0c
HH
134
135 ret = twl_i2c_read_u8(module, &data, address);
136 if (ret >= 0)
137 ret = data;
138 else
139 dev_err(twl->dev,
140 "readb[0x%x,0x%x] Error %d\n",
141 module, address, ret);
142 return ret;
143}
144
0e98de67 145static int twl6030_start_srp(struct phy_companion *comparator)
c33fad0c 146{
0e98de67 147 struct twl6030_usb *twl = comparator_to_twl(comparator);
603ab524
HH
148
149 twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
150 twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
151
152 mdelay(100);
153 twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
154
155 return 0;
156}
157
c33fad0c
HH
158static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
159{
c33fad0c 160 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
13069bd9 161 twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_BACKUP_REG);
c33fad0c
HH
162
163 /* Program CFG_LDO_PD2 register and set VUSB bit */
13069bd9 164 twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_CFG_LDO_PD2);
c33fad0c
HH
165
166 /* Program MISC2 register and set bit VUSB_IN_VBAT */
13069bd9 167 twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x10, TWL6030_MISC2);
c33fad0c 168
1ad528db 169 twl->usb3v3 = regulator_get(twl->dev, "usb");
c33fad0c
HH
170 if (IS_ERR(twl->usb3v3))
171 return -ENODEV;
172
c33fad0c
HH
173 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
174 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
175
176 /*
177 * Program the USB_ID_CTRL_SET register to enable GND drive
178 * and the ID comparators
179 */
180 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
181
182 return 0;
183}
184
185static ssize_t twl6030_usb_vbus_show(struct device *dev,
186 struct device_attribute *attr, char *buf)
187{
188 struct twl6030_usb *twl = dev_get_drvdata(dev);
189 unsigned long flags;
190 int ret = -EINVAL;
191
192 spin_lock_irqsave(&twl->lock, flags);
193
194 switch (twl->linkstat) {
8055555f 195 case MUSB_VBUS_VALID:
c33fad0c
HH
196 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
197 break;
8055555f 198 case MUSB_ID_GROUND:
c33fad0c
HH
199 ret = snprintf(buf, PAGE_SIZE, "id\n");
200 break;
8055555f 201 case MUSB_VBUS_OFF:
c33fad0c
HH
202 ret = snprintf(buf, PAGE_SIZE, "none\n");
203 break;
204 default:
205 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
206 }
207 spin_unlock_irqrestore(&twl->lock, flags);
208
209 return ret;
210}
211static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
212
213static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
214{
215 struct twl6030_usb *twl = _twl;
8055555f 216 enum musb_vbus_id_status status = MUSB_UNKNOWN;
c33fad0c 217 u8 vbus_state, hw_state;
bb54542c 218 int ret;
c33fad0c
HH
219
220 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
221
222 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
223 CONTROLLER_STAT1);
224 if (!(hw_state & STS_USB_ID)) {
225 if (vbus_state & VBUS_DET) {
bb54542c
FB
226 ret = regulator_enable(twl->usb3v3);
227 if (ret)
228 dev_err(twl->dev, "Failed to enable usb3v3\n");
229
6dc2503b 230 twl->asleep = 1;
8055555f 231 status = MUSB_VBUS_VALID;
6dc2503b 232 twl->linkstat = status;
12b7db2b
TL
233 ret = musb_mailbox(status);
234 if (ret)
235 twl->linkstat = MUSB_UNKNOWN;
c33fad0c 236 } else {
8055555f
TL
237 if (twl->linkstat != MUSB_UNKNOWN) {
238 status = MUSB_VBUS_OFF;
c9721438 239 twl->linkstat = status;
12b7db2b
TL
240 ret = musb_mailbox(status);
241 if (ret)
242 twl->linkstat = MUSB_UNKNOWN;
c9721438
KVA
243 if (twl->asleep) {
244 regulator_disable(twl->usb3v3);
245 twl->asleep = 0;
246 }
6dc2503b 247 }
c33fad0c
HH
248 }
249 }
250 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
251
252 return IRQ_HANDLED;
253}
254
255static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
256{
257 struct twl6030_usb *twl = _twl;
8055555f 258 enum musb_vbus_id_status status = MUSB_UNKNOWN;
c33fad0c 259 u8 hw_state;
bb54542c 260 int ret;
c33fad0c
HH
261
262 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
263
264 if (hw_state & STS_USB_ID) {
bb54542c
FB
265 ret = regulator_enable(twl->usb3v3);
266 if (ret)
267 dev_err(twl->dev, "Failed to enable usb3v3\n");
c33fad0c 268
6dc2503b 269 twl->asleep = 1;
dc8738d9
MS
270 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR);
271 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET);
8055555f 272 status = MUSB_ID_GROUND;
31e9992a 273 twl->linkstat = status;
12b7db2b
TL
274 ret = musb_mailbox(status);
275 if (ret)
276 twl->linkstat = MUSB_UNKNOWN;
c33fad0c 277 } else {
dc8738d9
MS
278 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR);
279 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
c33fad0c 280 }
dc8738d9 281 twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR);
c33fad0c
HH
282
283 return IRQ_HANDLED;
284}
285
b6a619a8
TL
286static void twl6030_status_work(struct work_struct *work)
287{
288 struct twl6030_usb *twl = container_of(work, struct twl6030_usb,
289 get_status_work.work);
290
291 twl6030_usb_irq(twl->irq2, twl);
292 twl6030_usbotg_irq(twl->irq1, twl);
293}
294
0e98de67 295static int twl6030_enable_irq(struct twl6030_usb *twl)
c33fad0c 296{
dc8738d9 297 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
c33fad0c
HH
298 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
299 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
300
301 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
302 REG_INT_MSK_LINE_C);
303 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
304 REG_INT_MSK_STS_C);
c33fad0c
HH
305
306 return 0;
307}
308
5bf54506 309static void otg_set_vbus_work(struct work_struct *data)
c33fad0c 310{
5bf54506
MS
311 struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
312 set_vbus_work);
c33fad0c
HH
313
314 /*
315 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
316 * register. This enables boost mode.
317 */
5bf54506
MS
318
319 if (twl->vbus_enable)
13069bd9 320 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x40,
5bf54506
MS
321 CHARGERUSB_CTRL1);
322 else
13069bd9 323 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x00,
5bf54506
MS
324 CHARGERUSB_CTRL1);
325}
326
0e98de67 327static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled)
5bf54506 328{
0e98de67 329 struct twl6030_usb *twl = comparator_to_twl(comparator);
5bf54506
MS
330
331 twl->vbus_enable = enabled;
332 schedule_work(&twl->set_vbus_work);
333
c33fad0c
HH
334 return 0;
335}
336
41ac7b3a 337static int twl6030_usb_probe(struct platform_device *pdev)
c33fad0c 338{
0e98de67 339 u32 ret;
c33fad0c
HH
340 struct twl6030_usb *twl;
341 int status, err;
ff0a1f39
KVA
342 struct device_node *np = pdev->dev.of_node;
343 struct device *dev = &pdev->dev;
1ad528db
NR
344
345 if (!np) {
346 dev_err(dev, "no DT info\n");
347 return -EINVAL;
348 }
c33fad0c 349
2ee8ff30 350 twl = devm_kzalloc(dev, sizeof(*twl), GFP_KERNEL);
c33fad0c
HH
351 if (!twl)
352 return -ENOMEM;
353
354 twl->dev = &pdev->dev;
355 twl->irq1 = platform_get_irq(pdev, 0);
356 twl->irq2 = platform_get_irq(pdev, 1);
8055555f 357 twl->linkstat = MUSB_UNKNOWN;
46b8f6b0 358
0e98de67
KVA
359 twl->comparator.set_vbus = twl6030_set_vbus;
360 twl->comparator.start_srp = twl6030_start_srp;
46b8f6b0 361
0e98de67
KVA
362 ret = omap_usb2_set_comparator(&twl->comparator);
363 if (ret == -ENODEV) {
364 dev_info(&pdev->dev, "phy not ready, deferring probe");
365 return -EPROBE_DEFER;
366 }
c33fad0c
HH
367
368 /* init spinlock for workqueue */
369 spin_lock_init(&twl->lock);
370
371 err = twl6030_usb_ldo_init(twl);
372 if (err) {
373 dev_err(&pdev->dev, "ldo init failed\n");
c33fad0c
HH
374 return err;
375 }
c33fad0c
HH
376
377 platform_set_drvdata(pdev, twl);
378 if (device_create_file(&pdev->dev, &dev_attr_vbus))
379 dev_warn(&pdev->dev, "could not create sysfs file\n");
380
5bf54506 381 INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
b6a619a8 382 INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
5bf54506 383
c33fad0c 384 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
8f9d973a 385 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
c33fad0c
HH
386 "twl6030_usb", twl);
387 if (status < 0) {
388 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
389 twl->irq1, status);
390 device_remove_file(twl->dev, &dev_attr_vbus);
c33fad0c
HH
391 return status;
392 }
393
394 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
8f9d973a 395 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
c33fad0c
HH
396 "twl6030_usb", twl);
397 if (status < 0) {
398 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
399 twl->irq2, status);
400 free_irq(twl->irq1, twl);
401 device_remove_file(twl->dev, &dev_attr_vbus);
c33fad0c
HH
402 return status;
403 }
404
6dc2503b 405 twl->asleep = 0;
0e98de67 406 twl6030_enable_irq(twl);
b6a619a8 407 schedule_delayed_work(&twl->get_status_work, HZ);
c33fad0c
HH
408 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
409
410 return 0;
411}
412
39d35681 413static int twl6030_usb_remove(struct platform_device *pdev)
c33fad0c
HH
414{
415 struct twl6030_usb *twl = platform_get_drvdata(pdev);
416
b6a619a8 417 cancel_delayed_work(&twl->get_status_work);
c33fad0c
HH
418 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
419 REG_INT_MSK_LINE_C);
420 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
421 REG_INT_MSK_STS_C);
422 free_irq(twl->irq1, twl);
423 free_irq(twl->irq2, twl);
424 regulator_put(twl->usb3v3);
c33fad0c 425 device_remove_file(twl->dev, &dev_attr_vbus);
5bf54506 426 cancel_work_sync(&twl->set_vbus_work);
c33fad0c
HH
427
428 return 0;
429}
430
ff0a1f39
KVA
431static const struct of_device_id twl6030_usb_id_table[] = {
432 { .compatible = "ti,twl6030-usb" },
433 {}
434};
435MODULE_DEVICE_TABLE(of, twl6030_usb_id_table);
ff0a1f39 436
c33fad0c
HH
437static struct platform_driver twl6030_usb_driver = {
438 .probe = twl6030_usb_probe,
39d35681 439 .remove = twl6030_usb_remove,
c33fad0c
HH
440 .driver = {
441 .name = "twl6030_usb",
ff0a1f39 442 .of_match_table = of_match_ptr(twl6030_usb_id_table),
c33fad0c
HH
443 },
444};
445
446static int __init twl6030_usb_init(void)
447{
448 return platform_driver_register(&twl6030_usb_driver);
449}
450subsys_initcall(twl6030_usb_init);
451
452static void __exit twl6030_usb_exit(void)
453{
454 platform_driver_unregister(&twl6030_usb_driver);
455}
456module_exit(twl6030_usb_exit);
457
458MODULE_ALIAS("platform:twl6030_usb");
459MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
460MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
461MODULE_LICENSE("GPL");