Merge tag 'v3.5-rc6' into irqdomain/next
[deliverable/linux.git] / drivers / usb / otg / twl6030-usb.c
CommitLineData
c33fad0c
HH
1/*
2 * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver.
3 *
4 * Copyright (C) 2010 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: Hema HK <hemahk@ti.com>
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/usb/otg.h>
29#include <linux/i2c/twl.h>
30#include <linux/regulator/consumer.h>
31#include <linux/err.h>
32#include <linux/notifier.h>
33#include <linux/slab.h>
603ab524 34#include <linux/delay.h>
c33fad0c
HH
35
36/* usb register definitions */
37#define USB_VENDOR_ID_LSB 0x00
38#define USB_VENDOR_ID_MSB 0x01
39#define USB_PRODUCT_ID_LSB 0x02
40#define USB_PRODUCT_ID_MSB 0x03
41#define USB_VBUS_CTRL_SET 0x04
42#define USB_VBUS_CTRL_CLR 0x05
43#define USB_ID_CTRL_SET 0x06
44#define USB_ID_CTRL_CLR 0x07
45#define USB_VBUS_INT_SRC 0x08
46#define USB_VBUS_INT_LATCH_SET 0x09
47#define USB_VBUS_INT_LATCH_CLR 0x0A
48#define USB_VBUS_INT_EN_LO_SET 0x0B
49#define USB_VBUS_INT_EN_LO_CLR 0x0C
50#define USB_VBUS_INT_EN_HI_SET 0x0D
51#define USB_VBUS_INT_EN_HI_CLR 0x0E
52#define USB_ID_INT_SRC 0x0F
53#define USB_ID_INT_LATCH_SET 0x10
54#define USB_ID_INT_LATCH_CLR 0x11
55
56#define USB_ID_INT_EN_LO_SET 0x12
57#define USB_ID_INT_EN_LO_CLR 0x13
58#define USB_ID_INT_EN_HI_SET 0x14
59#define USB_ID_INT_EN_HI_CLR 0x15
60#define USB_OTG_ADP_CTRL 0x16
61#define USB_OTG_ADP_HIGH 0x17
62#define USB_OTG_ADP_LOW 0x18
63#define USB_OTG_ADP_RISE 0x19
64#define USB_OTG_REVISION 0x1A
65
66/* to be moved to LDO */
67#define TWL6030_MISC2 0xE5
68#define TWL6030_CFG_LDO_PD2 0xF5
69#define TWL6030_BACKUP_REG 0xFA
70
71#define STS_HW_CONDITIONS 0x21
72
73/* In module TWL6030_MODULE_PM_MASTER */
74#define STS_HW_CONDITIONS 0x21
75#define STS_USB_ID BIT(2)
76
77/* In module TWL6030_MODULE_PM_RECEIVER */
78#define VUSB_CFG_TRANS 0x71
79#define VUSB_CFG_STATE 0x72
80#define VUSB_CFG_VOLTAGE 0x73
81
82/* in module TWL6030_MODULE_MAIN_CHARGE */
83
84#define CHARGERUSB_CTRL1 0x8
85
86#define CONTROLLER_STAT1 0x03
87#define VBUS_DET BIT(2)
88
89struct twl6030_usb {
46b8f6b0 90 struct usb_phy phy;
c33fad0c
HH
91 struct device *dev;
92
93 /* for vbus reporting with irqs disabled */
94 spinlock_t lock;
95
96 struct regulator *usb3v3;
97
5bf54506
MS
98 /* used to set vbus, in atomic path */
99 struct work_struct set_vbus_work;
100
c33fad0c
HH
101 int irq1;
102 int irq2;
103 u8 linkstat;
104 u8 asleep;
105 bool irq_enabled;
5bf54506 106 bool vbus_enable;
5ccee4ae 107 unsigned long features;
c33fad0c
HH
108};
109
46b8f6b0 110#define phy_to_twl(x) container_of((x), struct twl6030_usb, phy)
c33fad0c
HH
111
112/*-------------------------------------------------------------------------*/
113
114static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
115 u8 data, u8 address)
116{
117 int ret = 0;
118
119 ret = twl_i2c_write_u8(module, data, address);
120 if (ret < 0)
121 dev_err(twl->dev,
122 "Write[0x%x] Error %d\n", address, ret);
123 return ret;
124}
125
126static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
127{
128 u8 data, ret = 0;
129
130 ret = twl_i2c_read_u8(module, &data, address);
131 if (ret >= 0)
132 ret = data;
133 else
134 dev_err(twl->dev,
135 "readb[0x%x,0x%x] Error %d\n",
136 module, address, ret);
137 return ret;
138}
139
86753811 140static int twl6030_phy_init(struct usb_phy *x)
c33fad0c 141{
c33fad0c
HH
142 struct twl6030_usb *twl;
143 struct device *dev;
144 struct twl4030_usb_data *pdata;
145
46b8f6b0 146 twl = phy_to_twl(x);
c33fad0c
HH
147 dev = twl->dev;
148 pdata = dev->platform_data;
149
31e9992a 150 if (twl->linkstat == USB_EVENT_ID)
c33fad0c
HH
151 pdata->phy_power(twl->dev, 1, 1);
152 else
153 pdata->phy_power(twl->dev, 0, 1);
154
155 return 0;
156}
157
86753811 158static void twl6030_phy_shutdown(struct usb_phy *x)
c33fad0c
HH
159{
160 struct twl6030_usb *twl;
161 struct device *dev;
162 struct twl4030_usb_data *pdata;
163
46b8f6b0 164 twl = phy_to_twl(x);
c33fad0c
HH
165 dev = twl->dev;
166 pdata = dev->platform_data;
167 pdata->phy_power(twl->dev, 0, 0);
c33fad0c
HH
168}
169
86753811 170static int twl6030_phy_suspend(struct usb_phy *x, int suspend)
070b8ed9 171{
46b8f6b0 172 struct twl6030_usb *twl = phy_to_twl(x);
070b8ed9
HH
173 struct device *dev = twl->dev;
174 struct twl4030_usb_data *pdata = dev->platform_data;
175
176 pdata->phy_suspend(dev, suspend);
177
178 return 0;
179}
180
46b8f6b0 181static int twl6030_start_srp(struct usb_otg *otg)
603ab524 182{
46b8f6b0 183 struct twl6030_usb *twl = phy_to_twl(otg->phy);
603ab524
HH
184
185 twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
186 twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
187
188 mdelay(100);
189 twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
190
191 return 0;
192}
193
c33fad0c
HH
194static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
195{
5ccee4ae
GG
196 char *regulator_name;
197
198 if (twl->features & TWL6025_SUBCLASS)
199 regulator_name = "ldousb";
200 else
201 regulator_name = "vusb";
c33fad0c
HH
202
203 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
204 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
205
206 /* Program CFG_LDO_PD2 register and set VUSB bit */
207 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
208
209 /* Program MISC2 register and set bit VUSB_IN_VBAT */
210 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
211
5ccee4ae 212 twl->usb3v3 = regulator_get(twl->dev, regulator_name);
c33fad0c
HH
213 if (IS_ERR(twl->usb3v3))
214 return -ENODEV;
215
c33fad0c
HH
216 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
217 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
218
219 /*
220 * Program the USB_ID_CTRL_SET register to enable GND drive
221 * and the ID comparators
222 */
223 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
224
225 return 0;
226}
227
228static ssize_t twl6030_usb_vbus_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
230{
231 struct twl6030_usb *twl = dev_get_drvdata(dev);
232 unsigned long flags;
233 int ret = -EINVAL;
234
235 spin_lock_irqsave(&twl->lock, flags);
236
237 switch (twl->linkstat) {
238 case USB_EVENT_VBUS:
239 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
240 break;
241 case USB_EVENT_ID:
242 ret = snprintf(buf, PAGE_SIZE, "id\n");
243 break;
244 case USB_EVENT_NONE:
245 ret = snprintf(buf, PAGE_SIZE, "none\n");
246 break;
247 default:
248 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
249 }
250 spin_unlock_irqrestore(&twl->lock, flags);
251
252 return ret;
253}
254static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
255
256static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
257{
258 struct twl6030_usb *twl = _twl;
46b8f6b0 259 struct usb_otg *otg = twl->phy.otg;
c33fad0c
HH
260 int status;
261 u8 vbus_state, hw_state;
262
263 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
264
265 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
266 CONTROLLER_STAT1);
267 if (!(hw_state & STS_USB_ID)) {
268 if (vbus_state & VBUS_DET) {
6dc2503b
HH
269 regulator_enable(twl->usb3v3);
270 twl->asleep = 1;
c33fad0c 271 status = USB_EVENT_VBUS;
46b8f6b0
HK
272 otg->default_a = false;
273 twl->phy.state = OTG_STATE_B_IDLE;
6dc2503b 274 twl->linkstat = status;
46b8f6b0
HK
275 twl->phy.last_event = status;
276 atomic_notifier_call_chain(&twl->phy.notifier,
277 status, otg->gadget);
c33fad0c
HH
278 } else {
279 status = USB_EVENT_NONE;
c33fad0c 280 twl->linkstat = status;
46b8f6b0
HK
281 twl->phy.last_event = status;
282 atomic_notifier_call_chain(&twl->phy.notifier,
283 status, otg->gadget);
6dc2503b
HH
284 if (twl->asleep) {
285 regulator_disable(twl->usb3v3);
286 twl->asleep = 0;
287 }
c33fad0c
HH
288 }
289 }
290 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
291
292 return IRQ_HANDLED;
293}
294
295static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
296{
297 struct twl6030_usb *twl = _twl;
46b8f6b0 298 struct usb_otg *otg = twl->phy.otg;
c33fad0c
HH
299 int status = USB_EVENT_NONE;
300 u8 hw_state;
301
302 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
303
304 if (hw_state & STS_USB_ID) {
305
6dc2503b
HH
306 regulator_enable(twl->usb3v3);
307 twl->asleep = 1;
dc8738d9
MS
308 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR);
309 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET);
c33fad0c 310 status = USB_EVENT_ID;
46b8f6b0
HK
311 otg->default_a = true;
312 twl->phy.state = OTG_STATE_A_IDLE;
31e9992a 313 twl->linkstat = status;
46b8f6b0
HK
314 twl->phy.last_event = status;
315 atomic_notifier_call_chain(&twl->phy.notifier, status,
316 otg->gadget);
c33fad0c 317 } else {
dc8738d9
MS
318 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR);
319 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
c33fad0c 320 }
dc8738d9 321 twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR);
c33fad0c
HH
322
323 return IRQ_HANDLED;
324}
325
46b8f6b0 326static int twl6030_set_peripheral(struct usb_otg *otg,
c33fad0c
HH
327 struct usb_gadget *gadget)
328{
46b8f6b0 329 if (!otg)
c33fad0c
HH
330 return -ENODEV;
331
46b8f6b0 332 otg->gadget = gadget;
c33fad0c 333 if (!gadget)
46b8f6b0 334 otg->phy->state = OTG_STATE_UNDEFINED;
c33fad0c
HH
335
336 return 0;
337}
338
86753811 339static int twl6030_enable_irq(struct usb_phy *x)
c33fad0c 340{
46b8f6b0 341 struct twl6030_usb *twl = phy_to_twl(x);
c33fad0c 342
dc8738d9 343 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
c33fad0c
HH
344 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
345 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
346
347 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
348 REG_INT_MSK_LINE_C);
349 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
350 REG_INT_MSK_STS_C);
351 twl6030_usb_irq(twl->irq2, twl);
352 twl6030_usbotg_irq(twl->irq1, twl);
353
354 return 0;
355}
356
5bf54506 357static void otg_set_vbus_work(struct work_struct *data)
c33fad0c 358{
5bf54506
MS
359 struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
360 set_vbus_work);
c33fad0c
HH
361
362 /*
363 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
364 * register. This enables boost mode.
365 */
5bf54506
MS
366
367 if (twl->vbus_enable)
c33fad0c 368 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
5bf54506
MS
369 CHARGERUSB_CTRL1);
370 else
c33fad0c 371 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
5bf54506
MS
372 CHARGERUSB_CTRL1);
373}
374
46b8f6b0 375static int twl6030_set_vbus(struct usb_otg *otg, bool enabled)
5bf54506 376{
46b8f6b0 377 struct twl6030_usb *twl = phy_to_twl(otg->phy);
5bf54506
MS
378
379 twl->vbus_enable = enabled;
380 schedule_work(&twl->set_vbus_work);
381
c33fad0c
HH
382 return 0;
383}
384
46b8f6b0 385static int twl6030_set_host(struct usb_otg *otg, struct usb_bus *host)
c33fad0c 386{
46b8f6b0 387 if (!otg)
c33fad0c
HH
388 return -ENODEV;
389
46b8f6b0 390 otg->host = host;
c33fad0c 391 if (!host)
46b8f6b0 392 otg->phy->state = OTG_STATE_UNDEFINED;
c33fad0c
HH
393 return 0;
394}
395
396static int __devinit twl6030_usb_probe(struct platform_device *pdev)
397{
398 struct twl6030_usb *twl;
399 int status, err;
400 struct twl4030_usb_data *pdata;
46b8f6b0 401 struct usb_otg *otg;
c33fad0c
HH
402 struct device *dev = &pdev->dev;
403 pdata = dev->platform_data;
404
405 twl = kzalloc(sizeof *twl, GFP_KERNEL);
406 if (!twl)
407 return -ENOMEM;
408
46b8f6b0
HK
409 otg = kzalloc(sizeof *otg, GFP_KERNEL);
410 if (!otg) {
411 kfree(twl);
412 return -ENOMEM;
413 }
414
c33fad0c
HH
415 twl->dev = &pdev->dev;
416 twl->irq1 = platform_get_irq(pdev, 0);
417 twl->irq2 = platform_get_irq(pdev, 1);
5ccee4ae 418 twl->features = pdata->features;
46b8f6b0
HK
419
420 twl->phy.dev = twl->dev;
421 twl->phy.label = "twl6030";
422 twl->phy.otg = otg;
423 twl->phy.init = twl6030_phy_init;
424 twl->phy.shutdown = twl6030_phy_shutdown;
425 twl->phy.set_suspend = twl6030_phy_suspend;
426
427 otg->phy = &twl->phy;
428 otg->set_host = twl6030_set_host;
429 otg->set_peripheral = twl6030_set_peripheral;
430 otg->set_vbus = twl6030_set_vbus;
431 otg->start_srp = twl6030_start_srp;
c33fad0c
HH
432
433 /* init spinlock for workqueue */
434 spin_lock_init(&twl->lock);
435
436 err = twl6030_usb_ldo_init(twl);
437 if (err) {
438 dev_err(&pdev->dev, "ldo init failed\n");
46b8f6b0 439 kfree(otg);
c33fad0c
HH
440 kfree(twl);
441 return err;
442 }
46b8f6b0 443 usb_set_transceiver(&twl->phy);
c33fad0c
HH
444
445 platform_set_drvdata(pdev, twl);
446 if (device_create_file(&pdev->dev, &dev_attr_vbus))
447 dev_warn(&pdev->dev, "could not create sysfs file\n");
448
46b8f6b0 449 ATOMIC_INIT_NOTIFIER_HEAD(&twl->phy.notifier);
c33fad0c 450
5bf54506
MS
451 INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
452
c33fad0c
HH
453 twl->irq_enabled = true;
454 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
8f9d973a 455 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
c33fad0c
HH
456 "twl6030_usb", twl);
457 if (status < 0) {
458 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
459 twl->irq1, status);
460 device_remove_file(twl->dev, &dev_attr_vbus);
46b8f6b0 461 kfree(otg);
c33fad0c
HH
462 kfree(twl);
463 return status;
464 }
465
466 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
8f9d973a 467 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
c33fad0c
HH
468 "twl6030_usb", twl);
469 if (status < 0) {
470 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
471 twl->irq2, status);
472 free_irq(twl->irq1, twl);
473 device_remove_file(twl->dev, &dev_attr_vbus);
46b8f6b0 474 kfree(otg);
c33fad0c
HH
475 kfree(twl);
476 return status;
477 }
478
6dc2503b 479 twl->asleep = 0;
c33fad0c 480 pdata->phy_init(dev);
46b8f6b0
HK
481 twl6030_phy_suspend(&twl->phy, 0);
482 twl6030_enable_irq(&twl->phy);
c33fad0c
HH
483 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
484
485 return 0;
486}
487
488static int __exit twl6030_usb_remove(struct platform_device *pdev)
489{
490 struct twl6030_usb *twl = platform_get_drvdata(pdev);
491
492 struct twl4030_usb_data *pdata;
493 struct device *dev = &pdev->dev;
494 pdata = dev->platform_data;
495
496 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
497 REG_INT_MSK_LINE_C);
498 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
499 REG_INT_MSK_STS_C);
500 free_irq(twl->irq1, twl);
501 free_irq(twl->irq2, twl);
502 regulator_put(twl->usb3v3);
503 pdata->phy_exit(twl->dev);
504 device_remove_file(twl->dev, &dev_attr_vbus);
5bf54506 505 cancel_work_sync(&twl->set_vbus_work);
46b8f6b0 506 kfree(twl->phy.otg);
c33fad0c
HH
507 kfree(twl);
508
509 return 0;
510}
511
512static struct platform_driver twl6030_usb_driver = {
513 .probe = twl6030_usb_probe,
514 .remove = __exit_p(twl6030_usb_remove),
515 .driver = {
516 .name = "twl6030_usb",
517 .owner = THIS_MODULE,
518 },
519};
520
521static int __init twl6030_usb_init(void)
522{
523 return platform_driver_register(&twl6030_usb_driver);
524}
525subsys_initcall(twl6030_usb_init);
526
527static void __exit twl6030_usb_exit(void)
528{
529 platform_driver_unregister(&twl6030_usb_driver);
530}
531module_exit(twl6030_usb_exit);
532
533MODULE_ALIAS("platform:twl6030_usb");
534MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
535MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
536MODULE_LICENSE("GPL");
This page took 0.132244 seconds and 5 git commands to generate.