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>
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)
89 struct otg_transceiver otg;
92 /* for vbus reporting with irqs disabled */
95 struct regulator *usb3v3;
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,
113 ret = twl_i2c_write_u8(module, data, address);
116 "Write[0x%x] Error %d\n", address, ret);
120 static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
124 ret = twl_i2c_read_u8(module, &data, address);
129 "readb[0x%x,0x%x] Error %d\n",
130 module, address, ret);
134 /*-------------------------------------------------------------------------*/
135 static int twl6030_set_phy_clk(struct otg_transceiver *x, int on)
137 struct twl6030_usb *twl;
139 struct twl4030_usb_data *pdata;
141 twl = xceiv_to_twl(x);
143 pdata = dev->platform_data;
145 pdata->phy_set_clock(twl->dev, on);
150 static int twl6030_phy_init(struct otg_transceiver *x)
153 struct twl6030_usb *twl;
155 struct twl4030_usb_data *pdata;
157 twl = xceiv_to_twl(x);
159 pdata = dev->platform_data;
161 regulator_enable(twl->usb3v3);
163 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
165 if (hw_state & STS_USB_ID)
166 pdata->phy_power(twl->dev, 1, 1);
168 pdata->phy_power(twl->dev, 0, 1);
173 static void twl6030_phy_shutdown(struct otg_transceiver *x)
175 struct twl6030_usb *twl;
177 struct twl4030_usb_data *pdata;
179 twl = xceiv_to_twl(x);
181 pdata = dev->platform_data;
182 pdata->phy_power(twl->dev, 0, 0);
183 regulator_disable(twl->usb3v3);
186 static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
189 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
190 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
192 /* Program CFG_LDO_PD2 register and set VUSB bit */
193 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
195 /* Program MISC2 register and set bit VUSB_IN_VBAT */
196 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
198 twl->usb3v3 = regulator_get(twl->dev, "vusb");
199 if (IS_ERR(twl->usb3v3))
202 regulator_enable(twl->usb3v3);
204 /* Program the VUSB_CFG_TRANS for ACTIVE state. */
205 twl6030_writeb(twl, TWL_MODULE_PM_RECEIVER, 0x3F,
208 /* Program the VUSB_CFG_STATE register to ON on all groups. */
209 twl6030_writeb(twl, TWL_MODULE_PM_RECEIVER, 0xE1,
212 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
213 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
216 * Program the USB_ID_CTRL_SET register to enable GND drive
217 * and the ID comparators
219 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
224 static ssize_t twl6030_usb_vbus_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
227 struct twl6030_usb *twl = dev_get_drvdata(dev);
231 spin_lock_irqsave(&twl->lock, flags);
233 switch (twl->linkstat) {
235 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
238 ret = snprintf(buf, PAGE_SIZE, "id\n");
241 ret = snprintf(buf, PAGE_SIZE, "none\n");
244 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
246 spin_unlock_irqrestore(&twl->lock, flags);
250 static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
252 static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
254 struct twl6030_usb *twl = _twl;
256 u8 vbus_state, hw_state;
258 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
260 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
262 if (!(hw_state & STS_USB_ID)) {
263 if (vbus_state & VBUS_DET) {
264 status = USB_EVENT_VBUS;
265 twl->otg.default_a = false;
266 twl->otg.state = OTG_STATE_B_IDLE;
268 status = USB_EVENT_NONE;
271 twl->linkstat = status;
272 blocking_notifier_call_chain(&twl->otg.notifier,
273 status, twl->otg.gadget);
276 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
281 static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
283 struct twl6030_usb *twl = _twl;
284 int status = USB_EVENT_NONE;
287 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
289 if (hw_state & STS_USB_ID) {
291 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
292 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
294 status = USB_EVENT_ID;
295 twl->otg.default_a = true;
296 twl->otg.state = OTG_STATE_A_IDLE;
297 blocking_notifier_call_chain(&twl->otg.notifier, status,
300 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
302 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
305 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
306 twl->linkstat = status;
311 static int twl6030_set_peripheral(struct otg_transceiver *x,
312 struct usb_gadget *gadget)
314 struct twl6030_usb *twl;
319 twl = xceiv_to_twl(x);
320 twl->otg.gadget = gadget;
322 twl->otg.state = OTG_STATE_UNDEFINED;
327 static int twl6030_enable_irq(struct otg_transceiver *x)
329 struct twl6030_usb *twl = xceiv_to_twl(x);
331 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
332 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
333 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
335 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
337 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
339 twl6030_usb_irq(twl->irq2, twl);
340 twl6030_usbotg_irq(twl->irq1, twl);
345 static int twl6030_set_vbus(struct otg_transceiver *x, bool enabled)
347 struct twl6030_usb *twl = xceiv_to_twl(x);
350 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
351 * register. This enables boost mode.
354 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
357 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
362 static int twl6030_set_host(struct otg_transceiver *x, struct usb_bus *host)
364 struct twl6030_usb *twl;
369 twl = xceiv_to_twl(x);
370 twl->otg.host = host;
372 twl->otg.state = OTG_STATE_UNDEFINED;
376 static int __devinit twl6030_usb_probe(struct platform_device *pdev)
378 struct twl6030_usb *twl;
380 struct twl4030_usb_data *pdata;
381 struct device *dev = &pdev->dev;
382 pdata = dev->platform_data;
384 twl = kzalloc(sizeof *twl, GFP_KERNEL);
388 twl->dev = &pdev->dev;
389 twl->irq1 = platform_get_irq(pdev, 0);
390 twl->irq2 = platform_get_irq(pdev, 1);
391 twl->otg.dev = twl->dev;
392 twl->otg.label = "twl6030";
393 twl->otg.set_host = twl6030_set_host;
394 twl->otg.set_peripheral = twl6030_set_peripheral;
395 twl->otg.set_vbus = twl6030_set_vbus;
396 twl->otg.init = twl6030_phy_init;
397 twl->otg.shutdown = twl6030_phy_shutdown;
399 /* init spinlock for workqueue */
400 spin_lock_init(&twl->lock);
402 err = twl6030_usb_ldo_init(twl);
404 dev_err(&pdev->dev, "ldo init failed\n");
408 otg_set_transceiver(&twl->otg);
410 platform_set_drvdata(pdev, twl);
411 if (device_create_file(&pdev->dev, &dev_attr_vbus))
412 dev_warn(&pdev->dev, "could not create sysfs file\n");
414 BLOCKING_INIT_NOTIFIER_HEAD(&twl->otg.notifier);
416 twl->irq_enabled = true;
417 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
418 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
421 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
423 device_remove_file(twl->dev, &dev_attr_vbus);
428 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
429 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
432 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
434 free_irq(twl->irq1, twl);
435 device_remove_file(twl->dev, &dev_attr_vbus);
440 pdata->phy_init(dev);
441 twl6030_enable_irq(&twl->otg);
442 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
447 static int __exit twl6030_usb_remove(struct platform_device *pdev)
449 struct twl6030_usb *twl = platform_get_drvdata(pdev);
451 struct twl4030_usb_data *pdata;
452 struct device *dev = &pdev->dev;
453 pdata = dev->platform_data;
455 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
457 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
459 free_irq(twl->irq1, twl);
460 free_irq(twl->irq2, twl);
461 regulator_put(twl->usb3v3);
462 pdata->phy_exit(twl->dev);
463 device_remove_file(twl->dev, &dev_attr_vbus);
469 static struct platform_driver twl6030_usb_driver = {
470 .probe = twl6030_usb_probe,
471 .remove = __exit_p(twl6030_usb_remove),
473 .name = "twl6030_usb",
474 .owner = THIS_MODULE,
478 static int __init twl6030_usb_init(void)
480 return platform_driver_register(&twl6030_usb_driver);
482 subsys_initcall(twl6030_usb_init);
484 static void __exit twl6030_usb_exit(void)
486 platform_driver_unregister(&twl6030_usb_driver);
488 module_exit(twl6030_usb_exit);
490 MODULE_ALIAS("platform:twl6030_usb");
491 MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
492 MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
493 MODULE_LICENSE("GPL");