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