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-omap.h>
29 #include <linux/usb/phy_companion.h>
30 #include <linux/usb/omap_usb.h>
31 #include <linux/i2c/twl.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/err.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h>
37 /* usb register definitions */
38 #define USB_VENDOR_ID_LSB 0x00
39 #define USB_VENDOR_ID_MSB 0x01
40 #define USB_PRODUCT_ID_LSB 0x02
41 #define USB_PRODUCT_ID_MSB 0x03
42 #define USB_VBUS_CTRL_SET 0x04
43 #define USB_VBUS_CTRL_CLR 0x05
44 #define USB_ID_CTRL_SET 0x06
45 #define USB_ID_CTRL_CLR 0x07
46 #define USB_VBUS_INT_SRC 0x08
47 #define USB_VBUS_INT_LATCH_SET 0x09
48 #define USB_VBUS_INT_LATCH_CLR 0x0A
49 #define USB_VBUS_INT_EN_LO_SET 0x0B
50 #define USB_VBUS_INT_EN_LO_CLR 0x0C
51 #define USB_VBUS_INT_EN_HI_SET 0x0D
52 #define USB_VBUS_INT_EN_HI_CLR 0x0E
53 #define USB_ID_INT_SRC 0x0F
54 #define USB_ID_INT_LATCH_SET 0x10
55 #define USB_ID_INT_LATCH_CLR 0x11
57 #define USB_ID_INT_EN_LO_SET 0x12
58 #define USB_ID_INT_EN_LO_CLR 0x13
59 #define USB_ID_INT_EN_HI_SET 0x14
60 #define USB_ID_INT_EN_HI_CLR 0x15
61 #define USB_OTG_ADP_CTRL 0x16
62 #define USB_OTG_ADP_HIGH 0x17
63 #define USB_OTG_ADP_LOW 0x18
64 #define USB_OTG_ADP_RISE 0x19
65 #define USB_OTG_REVISION 0x1A
67 /* to be moved to LDO */
68 #define TWL6030_MISC2 0xE5
69 #define TWL6030_CFG_LDO_PD2 0xF5
70 #define TWL6030_BACKUP_REG 0xFA
72 #define STS_HW_CONDITIONS 0x21
74 /* In module TWL6030_MODULE_PM_MASTER */
75 #define STS_HW_CONDITIONS 0x21
76 #define STS_USB_ID BIT(2)
78 /* In module TWL6030_MODULE_PM_RECEIVER */
79 #define VUSB_CFG_TRANS 0x71
80 #define VUSB_CFG_STATE 0x72
81 #define VUSB_CFG_VOLTAGE 0x73
83 /* in module TWL6030_MODULE_MAIN_CHARGE */
85 #define CHARGERUSB_CTRL1 0x8
87 #define CONTROLLER_STAT1 0x03
88 #define VBUS_DET BIT(2)
91 struct phy_companion comparator;
94 /* for vbus reporting with irqs disabled */
97 struct regulator *usb3v3;
99 /* used to set vbus, in atomic path */
100 struct work_struct set_vbus_work;
104 enum omap_musb_vbus_id_status linkstat;
108 unsigned long features;
111 #define comparator_to_twl(x) container_of((x), struct twl6030_usb, comparator)
113 /*-------------------------------------------------------------------------*/
115 static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
120 ret = twl_i2c_write_u8(module, data, address);
123 "Write[0x%x] Error %d\n", address, ret);
127 static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
131 ret = twl_i2c_read_u8(module, &data, address);
136 "readb[0x%x,0x%x] Error %d\n",
137 module, address, ret);
141 static int twl6030_start_srp(struct phy_companion *comparator)
143 struct twl6030_usb *twl = comparator_to_twl(comparator);
145 twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
146 twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);
149 twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);
154 static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
156 char *regulator_name;
158 if (twl->features & TWL6025_SUBCLASS)
159 regulator_name = "ldousb";
161 regulator_name = "vusb";
163 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
164 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
166 /* Program CFG_LDO_PD2 register and set VUSB bit */
167 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
169 /* Program MISC2 register and set bit VUSB_IN_VBAT */
170 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
172 twl->usb3v3 = regulator_get(twl->dev, regulator_name);
173 if (IS_ERR(twl->usb3v3))
176 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
177 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
180 * Program the USB_ID_CTRL_SET register to enable GND drive
181 * and the ID comparators
183 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
188 static ssize_t twl6030_usb_vbus_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
191 struct twl6030_usb *twl = dev_get_drvdata(dev);
195 spin_lock_irqsave(&twl->lock, flags);
197 switch (twl->linkstat) {
198 case OMAP_MUSB_VBUS_VALID:
199 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
201 case OMAP_MUSB_ID_GROUND:
202 ret = snprintf(buf, PAGE_SIZE, "id\n");
204 case OMAP_MUSB_VBUS_OFF:
205 ret = snprintf(buf, PAGE_SIZE, "none\n");
208 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
210 spin_unlock_irqrestore(&twl->lock, flags);
214 static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
216 static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
218 struct twl6030_usb *twl = _twl;
219 enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN;
220 u8 vbus_state, hw_state;
222 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
224 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
226 if (!(hw_state & STS_USB_ID)) {
227 if (vbus_state & VBUS_DET) {
228 regulator_enable(twl->usb3v3);
230 status = OMAP_MUSB_VBUS_VALID;
231 twl->linkstat = status;
232 omap_musb_mailbox(status);
234 if (twl->linkstat != OMAP_MUSB_UNKNOWN) {
235 status = OMAP_MUSB_VBUS_OFF;
236 twl->linkstat = status;
237 omap_musb_mailbox(status);
239 regulator_disable(twl->usb3v3);
245 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
250 static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
252 struct twl6030_usb *twl = _twl;
253 enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN;
256 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
258 if (hw_state & STS_USB_ID) {
260 regulator_enable(twl->usb3v3);
262 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR);
263 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET);
264 status = OMAP_MUSB_ID_GROUND;
265 twl->linkstat = status;
266 omap_musb_mailbox(status);
268 twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR);
269 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
271 twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR);
276 static int twl6030_enable_irq(struct twl6030_usb *twl)
278 twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET);
279 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
280 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
282 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
284 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
286 twl6030_usb_irq(twl->irq2, twl);
287 twl6030_usbotg_irq(twl->irq1, twl);
292 static void otg_set_vbus_work(struct work_struct *data)
294 struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
298 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
299 * register. This enables boost mode.
302 if (twl->vbus_enable)
303 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
306 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
310 static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled)
312 struct twl6030_usb *twl = comparator_to_twl(comparator);
314 twl->vbus_enable = enabled;
315 schedule_work(&twl->set_vbus_work);
320 static int __devinit twl6030_usb_probe(struct platform_device *pdev)
323 struct twl6030_usb *twl;
325 struct twl4030_usb_data *pdata;
326 struct device *dev = &pdev->dev;
327 pdata = dev->platform_data;
329 twl = devm_kzalloc(dev, sizeof *twl, GFP_KERNEL);
333 twl->dev = &pdev->dev;
334 twl->irq1 = platform_get_irq(pdev, 0);
335 twl->irq2 = platform_get_irq(pdev, 1);
336 twl->features = pdata->features;
337 twl->linkstat = OMAP_MUSB_UNKNOWN;
339 twl->comparator.set_vbus = twl6030_set_vbus;
340 twl->comparator.start_srp = twl6030_start_srp;
342 ret = omap_usb2_set_comparator(&twl->comparator);
343 if (ret == -ENODEV) {
344 dev_info(&pdev->dev, "phy not ready, deferring probe");
345 return -EPROBE_DEFER;
348 /* init spinlock for workqueue */
349 spin_lock_init(&twl->lock);
351 err = twl6030_usb_ldo_init(twl);
353 dev_err(&pdev->dev, "ldo init failed\n");
357 platform_set_drvdata(pdev, twl);
358 if (device_create_file(&pdev->dev, &dev_attr_vbus))
359 dev_warn(&pdev->dev, "could not create sysfs file\n");
361 INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);
363 twl->irq_enabled = true;
364 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
365 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
368 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
370 device_remove_file(twl->dev, &dev_attr_vbus);
374 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
375 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
378 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
380 free_irq(twl->irq1, twl);
381 device_remove_file(twl->dev, &dev_attr_vbus);
386 twl6030_enable_irq(twl);
387 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
392 static int __exit twl6030_usb_remove(struct platform_device *pdev)
394 struct twl6030_usb *twl = platform_get_drvdata(pdev);
396 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
398 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
400 free_irq(twl->irq1, twl);
401 free_irq(twl->irq2, twl);
402 regulator_put(twl->usb3v3);
403 device_remove_file(twl->dev, &dev_attr_vbus);
404 cancel_work_sync(&twl->set_vbus_work);
409 static struct platform_driver twl6030_usb_driver = {
410 .probe = twl6030_usb_probe,
411 .remove = __exit_p(twl6030_usb_remove),
413 .name = "twl6030_usb",
414 .owner = THIS_MODULE,
418 static int __init twl6030_usb_init(void)
420 return platform_driver_register(&twl6030_usb_driver);
422 subsys_initcall(twl6030_usb_init);
424 static void __exit twl6030_usb_exit(void)
426 platform_driver_unregister(&twl6030_usb_driver);
428 module_exit(twl6030_usb_exit);
430 MODULE_ALIAS("platform:twl6030_usb");
431 MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
432 MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
433 MODULE_LICENSE("GPL");