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/musb.h>
29 #include <linux/usb/phy_companion.h>
30 #include <linux/phy/omap_usb.h>
31 #include <linux/mfd/twl.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/err.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h>
38 /* usb register definitions */
39 #define USB_VENDOR_ID_LSB 0x00
40 #define USB_VENDOR_ID_MSB 0x01
41 #define USB_PRODUCT_ID_LSB 0x02
42 #define USB_PRODUCT_ID_MSB 0x03
43 #define USB_VBUS_CTRL_SET 0x04
44 #define USB_VBUS_CTRL_CLR 0x05
45 #define USB_ID_CTRL_SET 0x06
46 #define USB_ID_CTRL_CLR 0x07
47 #define USB_VBUS_INT_SRC 0x08
48 #define USB_VBUS_INT_LATCH_SET 0x09
49 #define USB_VBUS_INT_LATCH_CLR 0x0A
50 #define USB_VBUS_INT_EN_LO_SET 0x0B
51 #define USB_VBUS_INT_EN_LO_CLR 0x0C
52 #define USB_VBUS_INT_EN_HI_SET 0x0D
53 #define USB_VBUS_INT_EN_HI_CLR 0x0E
54 #define USB_ID_INT_SRC 0x0F
55 #define USB_ID_INT_LATCH_SET 0x10
56 #define USB_ID_INT_LATCH_CLR 0x11
58 #define USB_ID_INT_EN_LO_SET 0x12
59 #define USB_ID_INT_EN_LO_CLR 0x13
60 #define USB_ID_INT_EN_HI_SET 0x14
61 #define USB_ID_INT_EN_HI_CLR 0x15
62 #define USB_OTG_ADP_CTRL 0x16
63 #define USB_OTG_ADP_HIGH 0x17
64 #define USB_OTG_ADP_LOW 0x18
65 #define USB_OTG_ADP_RISE 0x19
66 #define USB_OTG_REVISION 0x1A
68 /* to be moved to LDO */
69 #define TWL6030_MISC2 0xE5
70 #define TWL6030_CFG_LDO_PD2 0xF5
71 #define TWL6030_BACKUP_REG 0xFA
73 #define STS_HW_CONDITIONS 0x21
75 /* In module TWL6030_MODULE_PM_MASTER */
76 #define STS_HW_CONDITIONS 0x21
77 #define STS_USB_ID BIT(2)
79 /* In module TWL6030_MODULE_PM_RECEIVER */
80 #define VUSB_CFG_TRANS 0x71
81 #define VUSB_CFG_STATE 0x72
82 #define VUSB_CFG_VOLTAGE 0x73
84 /* in module TWL6030_MODULE_MAIN_CHARGE */
86 #define CHARGERUSB_CTRL1 0x8
88 #define CONTROLLER_STAT1 0x03
89 #define VBUS_DET BIT(2)
92 struct phy_companion comparator;
95 /* for vbus reporting with irqs disabled */
98 struct regulator *usb3v3;
100 /* used to check initial cable status after probe */
101 struct delayed_work get_status_work;
103 /* used to set vbus, in atomic path */
104 struct work_struct set_vbus_work;
108 enum musb_vbus_id_status linkstat;
113 #define comparator_to_twl(x) container_of((x), struct twl6030_usb, comparator)
115 /*-------------------------------------------------------------------------*/
117 static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
122 ret = twl_i2c_write_u8(module, data, address);
125 "Write[0x%x] Error %d\n", address, ret);
129 static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
134 ret = twl_i2c_read_u8(module, &data, address);
139 "readb[0x%x,0x%x] Error %d\n",
140 module, address, ret);
144 static int twl6030_start_srp(struct phy_companion *comparator)
146 struct twl6030_usb *twl = comparator_to_twl(comparator);
148 twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
149 twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
152 twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
157 static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
159 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
160 twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_BACKUP_REG);
162 /* Program CFG_LDO_PD2 register and set VUSB bit */
163 twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_CFG_LDO_PD2);
165 /* Program MISC2 register and set bit VUSB_IN_VBAT */
166 twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x10, TWL6030_MISC2);
168 twl->usb3v3 = regulator_get(twl->dev, "usb");
169 if (IS_ERR(twl->usb3v3))
172 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
173 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
176 * Program the USB_ID_CTRL_SET register to enable GND drive
177 * and the ID comparators
179 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
184 static ssize_t twl6030_usb_vbus_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
187 struct twl6030_usb *twl = dev_get_drvdata(dev);
191 spin_lock_irqsave(&twl->lock, flags);
193 switch (twl->linkstat) {
194 case MUSB_VBUS_VALID:
195 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
198 ret = snprintf(buf, PAGE_SIZE, "id\n");
201 ret = snprintf(buf, PAGE_SIZE, "none\n");
204 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
206 spin_unlock_irqrestore(&twl->lock, flags);
210 static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
212 static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
214 struct twl6030_usb *twl = _twl;
215 enum musb_vbus_id_status status = MUSB_UNKNOWN;
216 u8 vbus_state, hw_state;
219 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
221 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
223 if (!(hw_state & STS_USB_ID)) {
224 if (vbus_state & VBUS_DET) {
225 ret = regulator_enable(twl->usb3v3);
227 dev_err(twl->dev, "Failed to enable usb3v3\n");
230 status = MUSB_VBUS_VALID;
231 twl->linkstat = status;
232 ret = musb_mailbox(status);
234 twl->linkstat = MUSB_UNKNOWN;
236 if (twl->linkstat != MUSB_UNKNOWN) {
237 status = MUSB_VBUS_OFF;
238 twl->linkstat = status;
239 ret = musb_mailbox(status);
241 twl->linkstat = MUSB_UNKNOWN;
243 regulator_disable(twl->usb3v3);
249 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
254 static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
256 struct twl6030_usb *twl = _twl;
257 enum musb_vbus_id_status status = MUSB_UNKNOWN;
261 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
263 if (hw_state & STS_USB_ID) {
264 ret = regulator_enable(twl->usb3v3);
266 dev_err(twl->dev, "Failed to enable usb3v3\n");
269 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR);
270 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET);
271 status = MUSB_ID_GROUND;
272 twl->linkstat = status;
273 ret = musb_mailbox(status);
275 twl->linkstat = MUSB_UNKNOWN;
277 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR);
278 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
280 twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR);
285 static void twl6030_status_work(struct work_struct *work)
287 struct twl6030_usb *twl = container_of(work, struct twl6030_usb,
288 get_status_work.work);
290 twl6030_usb_irq(twl->irq2, twl);
291 twl6030_usbotg_irq(twl->irq1, twl);
294 static int twl6030_enable_irq(struct twl6030_usb *twl)
296 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
297 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
298 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
300 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
302 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
308 static void otg_set_vbus_work(struct work_struct *data)
310 struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
314 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
315 * register. This enables boost mode.
318 if (twl->vbus_enable)
319 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x40,
322 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x00,
326 static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled)
328 struct twl6030_usb *twl = comparator_to_twl(comparator);
330 twl->vbus_enable = enabled;
331 schedule_work(&twl->set_vbus_work);
336 static int twl6030_usb_probe(struct platform_device *pdev)
339 struct twl6030_usb *twl;
341 struct device_node *np = pdev->dev.of_node;
342 struct device *dev = &pdev->dev;
345 dev_err(dev, "no DT info\n");
349 twl = devm_kzalloc(dev, sizeof(*twl), GFP_KERNEL);
353 twl->dev = &pdev->dev;
354 twl->irq1 = platform_get_irq(pdev, 0);
355 twl->irq2 = platform_get_irq(pdev, 1);
356 twl->linkstat = MUSB_UNKNOWN;
363 twl->comparator.set_vbus = twl6030_set_vbus;
364 twl->comparator.start_srp = twl6030_start_srp;
366 ret = omap_usb2_set_comparator(&twl->comparator);
367 if (ret == -ENODEV) {
368 dev_info(&pdev->dev, "phy not ready, deferring probe");
369 return -EPROBE_DEFER;
372 /* init spinlock for workqueue */
373 spin_lock_init(&twl->lock);
375 err = twl6030_usb_ldo_init(twl);
377 dev_err(&pdev->dev, "ldo init failed\n");
381 platform_set_drvdata(pdev, twl);
382 if (device_create_file(&pdev->dev, &dev_attr_vbus))
383 dev_warn(&pdev->dev, "could not create sysfs file\n");
385 INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
386 INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work);
388 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
389 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
392 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
394 device_remove_file(twl->dev, &dev_attr_vbus);
398 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
399 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
402 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
404 free_irq(twl->irq1, twl);
405 device_remove_file(twl->dev, &dev_attr_vbus);
410 twl6030_enable_irq(twl);
411 schedule_delayed_work(&twl->get_status_work, HZ);
412 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
417 static int twl6030_usb_remove(struct platform_device *pdev)
419 struct twl6030_usb *twl = platform_get_drvdata(pdev);
421 cancel_delayed_work_sync(&twl->get_status_work);
422 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
424 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
426 free_irq(twl->irq1, twl);
427 free_irq(twl->irq2, twl);
428 regulator_put(twl->usb3v3);
429 device_remove_file(twl->dev, &dev_attr_vbus);
430 cancel_work_sync(&twl->set_vbus_work);
435 static const struct of_device_id twl6030_usb_id_table[] = {
436 { .compatible = "ti,twl6030-usb" },
439 MODULE_DEVICE_TABLE(of, twl6030_usb_id_table);
441 static struct platform_driver twl6030_usb_driver = {
442 .probe = twl6030_usb_probe,
443 .remove = twl6030_usb_remove,
445 .name = "twl6030_usb",
446 .of_match_table = of_match_ptr(twl6030_usb_id_table),
450 static int __init twl6030_usb_init(void)
452 return platform_driver_register(&twl6030_usb_driver);
454 subsys_initcall(twl6030_usb_init);
456 static void __exit twl6030_usb_exit(void)
458 platform_driver_unregister(&twl6030_usb_driver);
460 module_exit(twl6030_usb_exit);
462 MODULE_ALIAS("platform:twl6030_usb");
463 MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
464 MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
465 MODULE_LICENSE("GPL");