usb: otg: TWL6030 Save the last event in otg_transceiver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / otg / twl6030-usb.c
blob05f17b77d54c1e667aaa09667bec247f8fdbe2a6
1 /*
2 * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver.
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.
10 * Author: Hema HK <hemahk@ti.com>
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.
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.
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>
35 /* usb register definitions */
36 #define USB_VENDOR_ID_LSB 0x00
37 #define USB_VENDOR_ID_MSB 0x01
38 #define USB_PRODUCT_ID_LSB 0x02
39 #define USB_PRODUCT_ID_MSB 0x03
40 #define USB_VBUS_CTRL_SET 0x04
41 #define USB_VBUS_CTRL_CLR 0x05
42 #define USB_ID_CTRL_SET 0x06
43 #define USB_ID_CTRL_CLR 0x07
44 #define USB_VBUS_INT_SRC 0x08
45 #define USB_VBUS_INT_LATCH_SET 0x09
46 #define USB_VBUS_INT_LATCH_CLR 0x0A
47 #define USB_VBUS_INT_EN_LO_SET 0x0B
48 #define USB_VBUS_INT_EN_LO_CLR 0x0C
49 #define USB_VBUS_INT_EN_HI_SET 0x0D
50 #define USB_VBUS_INT_EN_HI_CLR 0x0E
51 #define USB_ID_INT_SRC 0x0F
52 #define USB_ID_INT_LATCH_SET 0x10
53 #define USB_ID_INT_LATCH_CLR 0x11
55 #define USB_ID_INT_EN_LO_SET 0x12
56 #define USB_ID_INT_EN_LO_CLR 0x13
57 #define USB_ID_INT_EN_HI_SET 0x14
58 #define USB_ID_INT_EN_HI_CLR 0x15
59 #define USB_OTG_ADP_CTRL 0x16
60 #define USB_OTG_ADP_HIGH 0x17
61 #define USB_OTG_ADP_LOW 0x18
62 #define USB_OTG_ADP_RISE 0x19
63 #define USB_OTG_REVISION 0x1A
65 /* to be moved to LDO */
66 #define TWL6030_MISC2 0xE5
67 #define TWL6030_CFG_LDO_PD2 0xF5
68 #define TWL6030_BACKUP_REG 0xFA
70 #define STS_HW_CONDITIONS 0x21
72 /* In module TWL6030_MODULE_PM_MASTER */
73 #define STS_HW_CONDITIONS 0x21
74 #define STS_USB_ID BIT(2)
76 /* In module TWL6030_MODULE_PM_RECEIVER */
77 #define VUSB_CFG_TRANS 0x71
78 #define VUSB_CFG_STATE 0x72
79 #define VUSB_CFG_VOLTAGE 0x73
81 /* in module TWL6030_MODULE_MAIN_CHARGE */
83 #define CHARGERUSB_CTRL1 0x8
85 #define CONTROLLER_STAT1 0x03
86 #define VBUS_DET BIT(2)
88 struct twl6030_usb {
89 struct otg_transceiver otg;
90 struct device *dev;
92 /* for vbus reporting with irqs disabled */
93 spinlock_t lock;
95 struct regulator *usb3v3;
97 int irq1;
98 int irq2;
99 u8 linkstat;
100 u8 asleep;
101 bool irq_enabled;
104 #define xceiv_to_twl(x) container_of((x), struct twl6030_usb, otg);
106 /*-------------------------------------------------------------------------*/
108 static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
109 u8 data, u8 address)
111 int ret = 0;
113 ret = twl_i2c_write_u8(module, data, address);
114 if (ret < 0)
115 dev_err(twl->dev,
116 "Write[0x%x] Error %d\n", address, ret);
117 return ret;
120 static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
122 u8 data, ret = 0;
124 ret = twl_i2c_read_u8(module, &data, address);
125 if (ret >= 0)
126 ret = data;
127 else
128 dev_err(twl->dev,
129 "readb[0x%x,0x%x] Error %d\n",
130 module, address, ret);
131 return ret;
134 /*-------------------------------------------------------------------------*/
135 static int twl6030_set_phy_clk(struct otg_transceiver *x, int on)
137 struct twl6030_usb *twl;
138 struct device *dev;
139 struct twl4030_usb_data *pdata;
141 twl = xceiv_to_twl(x);
142 dev = twl->dev;
143 pdata = dev->platform_data;
145 pdata->phy_set_clock(twl->dev, on);
147 return 0;
150 static int twl6030_phy_init(struct otg_transceiver *x)
152 struct twl6030_usb *twl;
153 struct device *dev;
154 struct twl4030_usb_data *pdata;
156 twl = xceiv_to_twl(x);
157 dev = twl->dev;
158 pdata = dev->platform_data;
160 if (twl->linkstat == USB_EVENT_ID)
161 pdata->phy_power(twl->dev, 1, 1);
162 else
163 pdata->phy_power(twl->dev, 0, 1);
165 return 0;
168 static void twl6030_phy_shutdown(struct otg_transceiver *x)
170 struct twl6030_usb *twl;
171 struct device *dev;
172 struct twl4030_usb_data *pdata;
174 twl = xceiv_to_twl(x);
175 dev = twl->dev;
176 pdata = dev->platform_data;
177 pdata->phy_power(twl->dev, 0, 0);
180 static int twl6030_phy_suspend(struct otg_transceiver *x, int suspend)
182 struct twl6030_usb *twl = xceiv_to_twl(x);
183 struct device *dev = twl->dev;
184 struct twl4030_usb_data *pdata = dev->platform_data;
186 pdata->phy_suspend(dev, suspend);
188 return 0;
191 static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
194 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
195 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
197 /* Program CFG_LDO_PD2 register and set VUSB bit */
198 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
200 /* Program MISC2 register and set bit VUSB_IN_VBAT */
201 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
203 twl->usb3v3 = regulator_get(twl->dev, "vusb");
204 if (IS_ERR(twl->usb3v3))
205 return -ENODEV;
207 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
208 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
211 * Program the USB_ID_CTRL_SET register to enable GND drive
212 * and the ID comparators
214 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
216 return 0;
219 static ssize_t twl6030_usb_vbus_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 struct twl6030_usb *twl = dev_get_drvdata(dev);
223 unsigned long flags;
224 int ret = -EINVAL;
226 spin_lock_irqsave(&twl->lock, flags);
228 switch (twl->linkstat) {
229 case USB_EVENT_VBUS:
230 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
231 break;
232 case USB_EVENT_ID:
233 ret = snprintf(buf, PAGE_SIZE, "id\n");
234 break;
235 case USB_EVENT_NONE:
236 ret = snprintf(buf, PAGE_SIZE, "none\n");
237 break;
238 default:
239 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
241 spin_unlock_irqrestore(&twl->lock, flags);
243 return ret;
245 static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
247 static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
249 struct twl6030_usb *twl = _twl;
250 int status;
251 u8 vbus_state, hw_state;
253 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
255 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
256 CONTROLLER_STAT1);
257 if (!(hw_state & STS_USB_ID)) {
258 if (vbus_state & VBUS_DET) {
259 regulator_enable(twl->usb3v3);
260 twl->asleep = 1;
261 status = USB_EVENT_VBUS;
262 twl->otg.default_a = false;
263 twl->otg.state = OTG_STATE_B_IDLE;
264 twl->linkstat = status;
265 twl->otg.last_event = status;
266 blocking_notifier_call_chain(&twl->otg.notifier,
267 status, twl->otg.gadget);
268 } else {
269 status = USB_EVENT_NONE;
270 twl->linkstat = status;
271 twl->otg.last_event = status;
272 blocking_notifier_call_chain(&twl->otg.notifier,
273 status, twl->otg.gadget);
274 if (twl->asleep) {
275 regulator_disable(twl->usb3v3);
276 twl->asleep = 0;
280 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
282 return IRQ_HANDLED;
285 static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
287 struct twl6030_usb *twl = _twl;
288 int status = USB_EVENT_NONE;
289 u8 hw_state;
291 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
293 if (hw_state & STS_USB_ID) {
295 regulator_enable(twl->usb3v3);
296 twl->asleep = 1;
297 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
298 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
299 0x10);
300 status = USB_EVENT_ID;
301 twl->otg.default_a = true;
302 twl->otg.state = OTG_STATE_A_IDLE;
303 twl->linkstat = status;
304 twl->otg.last_event = status;
305 blocking_notifier_call_chain(&twl->otg.notifier, status,
306 twl->otg.gadget);
307 } else {
308 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
309 0x10);
310 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
311 0x1);
313 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
315 return IRQ_HANDLED;
318 static int twl6030_set_peripheral(struct otg_transceiver *x,
319 struct usb_gadget *gadget)
321 struct twl6030_usb *twl;
323 if (!x)
324 return -ENODEV;
326 twl = xceiv_to_twl(x);
327 twl->otg.gadget = gadget;
328 if (!gadget)
329 twl->otg.state = OTG_STATE_UNDEFINED;
331 return 0;
334 static int twl6030_enable_irq(struct otg_transceiver *x)
336 struct twl6030_usb *twl = xceiv_to_twl(x);
338 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
339 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
340 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
342 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
343 REG_INT_MSK_LINE_C);
344 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
345 REG_INT_MSK_STS_C);
346 twl6030_usb_irq(twl->irq2, twl);
347 twl6030_usbotg_irq(twl->irq1, twl);
349 return 0;
352 static int twl6030_set_vbus(struct otg_transceiver *x, bool enabled)
354 struct twl6030_usb *twl = xceiv_to_twl(x);
357 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
358 * register. This enables boost mode.
360 if (enabled)
361 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
362 CHARGERUSB_CTRL1);
363 else
364 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
365 CHARGERUSB_CTRL1);
366 return 0;
369 static int twl6030_set_host(struct otg_transceiver *x, struct usb_bus *host)
371 struct twl6030_usb *twl;
373 if (!x)
374 return -ENODEV;
376 twl = xceiv_to_twl(x);
377 twl->otg.host = host;
378 if (!host)
379 twl->otg.state = OTG_STATE_UNDEFINED;
380 return 0;
383 static int __devinit twl6030_usb_probe(struct platform_device *pdev)
385 struct twl6030_usb *twl;
386 int status, err;
387 struct twl4030_usb_data *pdata;
388 struct device *dev = &pdev->dev;
389 pdata = dev->platform_data;
391 twl = kzalloc(sizeof *twl, GFP_KERNEL);
392 if (!twl)
393 return -ENOMEM;
395 twl->dev = &pdev->dev;
396 twl->irq1 = platform_get_irq(pdev, 0);
397 twl->irq2 = platform_get_irq(pdev, 1);
398 twl->otg.dev = twl->dev;
399 twl->otg.label = "twl6030";
400 twl->otg.set_host = twl6030_set_host;
401 twl->otg.set_peripheral = twl6030_set_peripheral;
402 twl->otg.set_vbus = twl6030_set_vbus;
403 twl->otg.init = twl6030_phy_init;
404 twl->otg.shutdown = twl6030_phy_shutdown;
405 twl->otg.set_suspend = twl6030_phy_suspend;
407 /* init spinlock for workqueue */
408 spin_lock_init(&twl->lock);
410 err = twl6030_usb_ldo_init(twl);
411 if (err) {
412 dev_err(&pdev->dev, "ldo init failed\n");
413 kfree(twl);
414 return err;
416 otg_set_transceiver(&twl->otg);
418 platform_set_drvdata(pdev, twl);
419 if (device_create_file(&pdev->dev, &dev_attr_vbus))
420 dev_warn(&pdev->dev, "could not create sysfs file\n");
422 BLOCKING_INIT_NOTIFIER_HEAD(&twl->otg.notifier);
424 twl->irq_enabled = true;
425 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
426 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
427 "twl6030_usb", twl);
428 if (status < 0) {
429 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
430 twl->irq1, status);
431 device_remove_file(twl->dev, &dev_attr_vbus);
432 kfree(twl);
433 return status;
436 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
437 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
438 "twl6030_usb", twl);
439 if (status < 0) {
440 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
441 twl->irq2, status);
442 free_irq(twl->irq1, twl);
443 device_remove_file(twl->dev, &dev_attr_vbus);
444 kfree(twl);
445 return status;
448 twl->asleep = 0;
449 pdata->phy_init(dev);
450 twl6030_phy_suspend(&twl->otg, 0);
451 twl6030_enable_irq(&twl->otg);
452 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
454 return 0;
457 static int __exit twl6030_usb_remove(struct platform_device *pdev)
459 struct twl6030_usb *twl = platform_get_drvdata(pdev);
461 struct twl4030_usb_data *pdata;
462 struct device *dev = &pdev->dev;
463 pdata = dev->platform_data;
465 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
466 REG_INT_MSK_LINE_C);
467 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
468 REG_INT_MSK_STS_C);
469 free_irq(twl->irq1, twl);
470 free_irq(twl->irq2, twl);
471 regulator_put(twl->usb3v3);
472 pdata->phy_exit(twl->dev);
473 device_remove_file(twl->dev, &dev_attr_vbus);
474 kfree(twl);
476 return 0;
479 static struct platform_driver twl6030_usb_driver = {
480 .probe = twl6030_usb_probe,
481 .remove = __exit_p(twl6030_usb_remove),
482 .driver = {
483 .name = "twl6030_usb",
484 .owner = THIS_MODULE,
488 static int __init twl6030_usb_init(void)
490 return platform_driver_register(&twl6030_usb_driver);
492 subsys_initcall(twl6030_usb_init);
494 static void __exit twl6030_usb_exit(void)
496 platform_driver_unregister(&twl6030_usb_driver);
498 module_exit(twl6030_usb_exit);
500 MODULE_ALIAS("platform:twl6030_usb");
501 MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
502 MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
503 MODULE_LICENSE("GPL");