Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 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> |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 28 | #include <linux/usb/musb-omap.h> |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 29 | #include <linux/usb/phy_companion.h> |
| 30 | #include <linux/usb/omap_usb.h> |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 31 | #include <linux/i2c/twl.h> |
| 32 | #include <linux/regulator/consumer.h> |
| 33 | #include <linux/err.h> |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 34 | #include <linux/slab.h> |
Hema HK | 603ab52 | 2011-03-22 16:54:21 +0530 | [diff] [blame] | 35 | #include <linux/delay.h> |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 36 | |
| 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 |
| 56 | |
| 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 |
| 66 | |
| 67 | /* to be moved to LDO */ |
| 68 | #define TWL6030_MISC2 0xE5 |
| 69 | #define TWL6030_CFG_LDO_PD2 0xF5 |
| 70 | #define TWL6030_BACKUP_REG 0xFA |
| 71 | |
| 72 | #define STS_HW_CONDITIONS 0x21 |
| 73 | |
| 74 | /* In module TWL6030_MODULE_PM_MASTER */ |
| 75 | #define STS_HW_CONDITIONS 0x21 |
| 76 | #define STS_USB_ID BIT(2) |
| 77 | |
| 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 |
| 82 | |
| 83 | /* in module TWL6030_MODULE_MAIN_CHARGE */ |
| 84 | |
| 85 | #define CHARGERUSB_CTRL1 0x8 |
| 86 | |
| 87 | #define CONTROLLER_STAT1 0x03 |
| 88 | #define VBUS_DET BIT(2) |
| 89 | |
| 90 | struct twl6030_usb { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 91 | struct phy_companion comparator; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 92 | struct device *dev; |
| 93 | |
| 94 | /* for vbus reporting with irqs disabled */ |
| 95 | spinlock_t lock; |
| 96 | |
| 97 | struct regulator *usb3v3; |
| 98 | |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 99 | /* used to set vbus, in atomic path */ |
| 100 | struct work_struct set_vbus_work; |
| 101 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 102 | int irq1; |
| 103 | int irq2; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 104 | enum omap_musb_vbus_id_status linkstat; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 105 | u8 asleep; |
| 106 | bool irq_enabled; |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 107 | bool vbus_enable; |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 108 | const char *regulator; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 109 | }; |
| 110 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 111 | #define comparator_to_twl(x) container_of((x), struct twl6030_usb, comparator) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 112 | |
| 113 | /*-------------------------------------------------------------------------*/ |
| 114 | |
| 115 | static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module, |
| 116 | u8 data, u8 address) |
| 117 | { |
| 118 | int ret = 0; |
| 119 | |
| 120 | ret = twl_i2c_write_u8(module, data, address); |
| 121 | if (ret < 0) |
| 122 | dev_err(twl->dev, |
| 123 | "Write[0x%x] Error %d\n", address, ret); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address) |
| 128 | { |
| 129 | u8 data, ret = 0; |
| 130 | |
| 131 | ret = twl_i2c_read_u8(module, &data, address); |
| 132 | if (ret >= 0) |
| 133 | ret = data; |
| 134 | else |
| 135 | dev_err(twl->dev, |
| 136 | "readb[0x%x,0x%x] Error %d\n", |
| 137 | module, address, ret); |
| 138 | return ret; |
| 139 | } |
| 140 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 141 | static int twl6030_start_srp(struct phy_companion *comparator) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 142 | { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 143 | struct twl6030_usb *twl = comparator_to_twl(comparator); |
Hema HK | 603ab52 | 2011-03-22 16:54:21 +0530 | [diff] [blame] | 144 | |
| 145 | twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET); |
| 146 | twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET); |
| 147 | |
| 148 | mdelay(100); |
| 149 | twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 154 | static int twl6030_usb_ldo_init(struct twl6030_usb *twl) |
| 155 | { |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 156 | /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */ |
| 157 | twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG); |
| 158 | |
| 159 | /* Program CFG_LDO_PD2 register and set VUSB bit */ |
| 160 | twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2); |
| 161 | |
| 162 | /* Program MISC2 register and set bit VUSB_IN_VBAT */ |
| 163 | twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2); |
| 164 | |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 165 | twl->usb3v3 = regulator_get(twl->dev, twl->regulator); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 166 | if (IS_ERR(twl->usb3v3)) |
| 167 | return -ENODEV; |
| 168 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 169 | /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */ |
| 170 | twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET); |
| 171 | |
| 172 | /* |
| 173 | * Program the USB_ID_CTRL_SET register to enable GND drive |
| 174 | * and the ID comparators |
| 175 | */ |
| 176 | twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static ssize_t twl6030_usb_vbus_show(struct device *dev, |
| 182 | struct device_attribute *attr, char *buf) |
| 183 | { |
| 184 | struct twl6030_usb *twl = dev_get_drvdata(dev); |
| 185 | unsigned long flags; |
| 186 | int ret = -EINVAL; |
| 187 | |
| 188 | spin_lock_irqsave(&twl->lock, flags); |
| 189 | |
| 190 | switch (twl->linkstat) { |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 191 | case OMAP_MUSB_VBUS_VALID: |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 192 | ret = snprintf(buf, PAGE_SIZE, "vbus\n"); |
| 193 | break; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 194 | case OMAP_MUSB_ID_GROUND: |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 195 | ret = snprintf(buf, PAGE_SIZE, "id\n"); |
| 196 | break; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 197 | case OMAP_MUSB_VBUS_OFF: |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 198 | ret = snprintf(buf, PAGE_SIZE, "none\n"); |
| 199 | break; |
| 200 | default: |
| 201 | ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n"); |
| 202 | } |
| 203 | spin_unlock_irqrestore(&twl->lock, flags); |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL); |
| 208 | |
| 209 | static irqreturn_t twl6030_usb_irq(int irq, void *_twl) |
| 210 | { |
| 211 | struct twl6030_usb *twl = _twl; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 212 | enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 213 | u8 vbus_state, hw_state; |
| 214 | |
| 215 | hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS); |
| 216 | |
| 217 | vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE, |
| 218 | CONTROLLER_STAT1); |
| 219 | if (!(hw_state & STS_USB_ID)) { |
| 220 | if (vbus_state & VBUS_DET) { |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 221 | regulator_enable(twl->usb3v3); |
| 222 | twl->asleep = 1; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 223 | status = OMAP_MUSB_VBUS_VALID; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 224 | twl->linkstat = status; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 225 | omap_musb_mailbox(status); |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 226 | } else { |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 227 | if (twl->linkstat != OMAP_MUSB_UNKNOWN) { |
| 228 | status = OMAP_MUSB_VBUS_OFF; |
| 229 | twl->linkstat = status; |
| 230 | omap_musb_mailbox(status); |
| 231 | if (twl->asleep) { |
| 232 | regulator_disable(twl->usb3v3); |
| 233 | twl->asleep = 0; |
| 234 | } |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 235 | } |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | sysfs_notify(&twl->dev->kobj, NULL, "vbus"); |
| 239 | |
| 240 | return IRQ_HANDLED; |
| 241 | } |
| 242 | |
| 243 | static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl) |
| 244 | { |
| 245 | struct twl6030_usb *twl = _twl; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 246 | enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 247 | u8 hw_state; |
| 248 | |
| 249 | hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS); |
| 250 | |
| 251 | if (hw_state & STS_USB_ID) { |
| 252 | |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 253 | regulator_enable(twl->usb3v3); |
| 254 | twl->asleep = 1; |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 255 | twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_CLR); |
| 256 | twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_SET); |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 257 | status = OMAP_MUSB_ID_GROUND; |
Hema HK | 31e9992 | 2011-02-17 12:06:05 +0530 | [diff] [blame] | 258 | twl->linkstat = status; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 259 | omap_musb_mailbox(status); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 260 | } else { |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 261 | twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR); |
| 262 | twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 263 | } |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 264 | twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 265 | |
| 266 | return IRQ_HANDLED; |
| 267 | } |
| 268 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 269 | static int twl6030_enable_irq(struct twl6030_usb *twl) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 270 | { |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 271 | twl6030_writeb(twl, TWL_MODULE_USB, 0x1, USB_ID_INT_EN_HI_SET); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 272 | twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C); |
| 273 | twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C); |
| 274 | |
| 275 | twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK, |
| 276 | REG_INT_MSK_LINE_C); |
| 277 | twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK, |
| 278 | REG_INT_MSK_STS_C); |
| 279 | twl6030_usb_irq(twl->irq2, twl); |
| 280 | twl6030_usbotg_irq(twl->irq1, twl); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 285 | static void otg_set_vbus_work(struct work_struct *data) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 286 | { |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 287 | struct twl6030_usb *twl = container_of(data, struct twl6030_usb, |
| 288 | set_vbus_work); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1 |
| 292 | * register. This enables boost mode. |
| 293 | */ |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 294 | |
| 295 | if (twl->vbus_enable) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 296 | twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40, |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 297 | CHARGERUSB_CTRL1); |
| 298 | else |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 299 | twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00, |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 300 | CHARGERUSB_CTRL1); |
| 301 | } |
| 302 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 303 | static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled) |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 304 | { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 305 | struct twl6030_usb *twl = comparator_to_twl(comparator); |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 306 | |
| 307 | twl->vbus_enable = enabled; |
| 308 | schedule_work(&twl->set_vbus_work); |
| 309 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 313 | static int twl6030_usb_probe(struct platform_device *pdev) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 314 | { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 315 | u32 ret; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 316 | struct twl6030_usb *twl; |
| 317 | int status, err; |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 318 | struct device_node *np = pdev->dev.of_node; |
| 319 | struct device *dev = &pdev->dev; |
| 320 | struct twl4030_usb_data *pdata = dev->platform_data; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 321 | |
Kishon Vijay Abraham I | b8a3efa | 2012-06-22 17:40:55 +0530 | [diff] [blame] | 322 | twl = devm_kzalloc(dev, sizeof *twl, GFP_KERNEL); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 323 | if (!twl) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | twl->dev = &pdev->dev; |
| 327 | twl->irq1 = platform_get_irq(pdev, 0); |
| 328 | twl->irq2 = platform_get_irq(pdev, 1); |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 329 | twl->linkstat = OMAP_MUSB_UNKNOWN; |
Heikki Krogerus | 46b8f6b | 2012-02-13 13:24:12 +0200 | [diff] [blame] | 330 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 331 | twl->comparator.set_vbus = twl6030_set_vbus; |
| 332 | twl->comparator.start_srp = twl6030_start_srp; |
Heikki Krogerus | 46b8f6b | 2012-02-13 13:24:12 +0200 | [diff] [blame] | 333 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 334 | ret = omap_usb2_set_comparator(&twl->comparator); |
| 335 | if (ret == -ENODEV) { |
| 336 | dev_info(&pdev->dev, "phy not ready, deferring probe"); |
| 337 | return -EPROBE_DEFER; |
| 338 | } |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 339 | |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 340 | if (np) { |
| 341 | twl->regulator = "usb"; |
| 342 | } else if (pdata) { |
| 343 | if (pdata->features & TWL6025_SUBCLASS) |
| 344 | twl->regulator = "ldousb"; |
| 345 | else |
| 346 | twl->regulator = "vusb"; |
| 347 | } else { |
| 348 | dev_err(&pdev->dev, "twl6030 initialized without pdata\n"); |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 352 | /* init spinlock for workqueue */ |
| 353 | spin_lock_init(&twl->lock); |
| 354 | |
| 355 | err = twl6030_usb_ldo_init(twl); |
| 356 | if (err) { |
| 357 | dev_err(&pdev->dev, "ldo init failed\n"); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 358 | return err; |
| 359 | } |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 360 | |
| 361 | platform_set_drvdata(pdev, twl); |
| 362 | if (device_create_file(&pdev->dev, &dev_attr_vbus)) |
| 363 | dev_warn(&pdev->dev, "could not create sysfs file\n"); |
| 364 | |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 365 | INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work); |
| 366 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 367 | twl->irq_enabled = true; |
| 368 | status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq, |
Ming Lei | 8f9d973 | 2012-05-17 11:38:24 +0800 | [diff] [blame] | 369 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 370 | "twl6030_usb", twl); |
| 371 | if (status < 0) { |
| 372 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
| 373 | twl->irq1, status); |
| 374 | device_remove_file(twl->dev, &dev_attr_vbus); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 375 | return status; |
| 376 | } |
| 377 | |
| 378 | status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq, |
Ming Lei | 8f9d973 | 2012-05-17 11:38:24 +0800 | [diff] [blame] | 379 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 380 | "twl6030_usb", twl); |
| 381 | if (status < 0) { |
| 382 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
| 383 | twl->irq2, status); |
| 384 | free_irq(twl->irq1, twl); |
| 385 | device_remove_file(twl->dev, &dev_attr_vbus); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 386 | return status; |
| 387 | } |
| 388 | |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 389 | twl->asleep = 0; |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 390 | twl6030_enable_irq(twl); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 391 | dev_info(&pdev->dev, "Initialized TWL6030 USB module\n"); |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int __exit twl6030_usb_remove(struct platform_device *pdev) |
| 397 | { |
| 398 | struct twl6030_usb *twl = platform_get_drvdata(pdev); |
| 399 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 400 | twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK, |
| 401 | REG_INT_MSK_LINE_C); |
| 402 | twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK, |
| 403 | REG_INT_MSK_STS_C); |
| 404 | free_irq(twl->irq1, twl); |
| 405 | free_irq(twl->irq2, twl); |
| 406 | regulator_put(twl->usb3v3); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 407 | device_remove_file(twl->dev, &dev_attr_vbus); |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 408 | cancel_work_sync(&twl->set_vbus_work); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 413 | #ifdef CONFIG_OF |
| 414 | static const struct of_device_id twl6030_usb_id_table[] = { |
| 415 | { .compatible = "ti,twl6030-usb" }, |
| 416 | {} |
| 417 | }; |
| 418 | MODULE_DEVICE_TABLE(of, twl6030_usb_id_table); |
| 419 | #endif |
| 420 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 421 | static struct platform_driver twl6030_usb_driver = { |
| 422 | .probe = twl6030_usb_probe, |
| 423 | .remove = __exit_p(twl6030_usb_remove), |
| 424 | .driver = { |
| 425 | .name = "twl6030_usb", |
| 426 | .owner = THIS_MODULE, |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 427 | .of_match_table = of_match_ptr(twl6030_usb_id_table), |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 428 | }, |
| 429 | }; |
| 430 | |
| 431 | static int __init twl6030_usb_init(void) |
| 432 | { |
| 433 | return platform_driver_register(&twl6030_usb_driver); |
| 434 | } |
| 435 | subsys_initcall(twl6030_usb_init); |
| 436 | |
| 437 | static void __exit twl6030_usb_exit(void) |
| 438 | { |
| 439 | platform_driver_unregister(&twl6030_usb_driver); |
| 440 | } |
| 441 | module_exit(twl6030_usb_exit); |
| 442 | |
| 443 | MODULE_ALIAS("platform:twl6030_usb"); |
| 444 | MODULE_AUTHOR("Hema HK <hemahk@ti.com>"); |
| 445 | MODULE_DESCRIPTION("TWL6030 USB transceiver driver"); |
| 446 | MODULE_LICENSE("GPL"); |