Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 2 | /* |
| 3 | * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver. |
| 4 | * |
Alexander A. Klimov | 36774c5 | 2020-07-10 21:18:42 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Texas Instruments Incorporated - https://www.ti.com |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 6 | * |
| 7 | * Author: Hema HK <hemahk@ti.com> |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/io.h> |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 15 | #include <linux/usb/musb.h> |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 16 | #include <linux/usb/phy_companion.h> |
Kishon Vijay Abraham I | 6284be2 | 2014-03-03 17:08:14 +0530 | [diff] [blame] | 17 | #include <linux/phy/omap_usb.h> |
Wolfram Sang | a205425 | 2017-08-14 18:34:24 +0200 | [diff] [blame] | 18 | #include <linux/mfd/twl.h> |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 19 | #include <linux/regulator/consumer.h> |
| 20 | #include <linux/err.h> |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 21 | #include <linux/slab.h> |
Hema HK | 603ab52 | 2011-03-22 16:54:21 +0530 | [diff] [blame] | 22 | #include <linux/delay.h> |
Sachin Kamat | 80d7d8a | 2013-10-17 13:55:06 +0530 | [diff] [blame] | 23 | #include <linux/of.h> |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 24 | |
| 25 | /* usb register definitions */ |
| 26 | #define USB_VENDOR_ID_LSB 0x00 |
| 27 | #define USB_VENDOR_ID_MSB 0x01 |
| 28 | #define USB_PRODUCT_ID_LSB 0x02 |
| 29 | #define USB_PRODUCT_ID_MSB 0x03 |
| 30 | #define USB_VBUS_CTRL_SET 0x04 |
| 31 | #define USB_VBUS_CTRL_CLR 0x05 |
| 32 | #define USB_ID_CTRL_SET 0x06 |
| 33 | #define USB_ID_CTRL_CLR 0x07 |
| 34 | #define USB_VBUS_INT_SRC 0x08 |
| 35 | #define USB_VBUS_INT_LATCH_SET 0x09 |
| 36 | #define USB_VBUS_INT_LATCH_CLR 0x0A |
| 37 | #define USB_VBUS_INT_EN_LO_SET 0x0B |
| 38 | #define USB_VBUS_INT_EN_LO_CLR 0x0C |
| 39 | #define USB_VBUS_INT_EN_HI_SET 0x0D |
| 40 | #define USB_VBUS_INT_EN_HI_CLR 0x0E |
| 41 | #define USB_ID_INT_SRC 0x0F |
| 42 | #define USB_ID_INT_LATCH_SET 0x10 |
| 43 | #define USB_ID_INT_LATCH_CLR 0x11 |
| 44 | |
| 45 | #define USB_ID_INT_EN_LO_SET 0x12 |
| 46 | #define USB_ID_INT_EN_LO_CLR 0x13 |
| 47 | #define USB_ID_INT_EN_HI_SET 0x14 |
| 48 | #define USB_ID_INT_EN_HI_CLR 0x15 |
| 49 | #define USB_OTG_ADP_CTRL 0x16 |
| 50 | #define USB_OTG_ADP_HIGH 0x17 |
| 51 | #define USB_OTG_ADP_LOW 0x18 |
| 52 | #define USB_OTG_ADP_RISE 0x19 |
| 53 | #define USB_OTG_REVISION 0x1A |
| 54 | |
| 55 | /* to be moved to LDO */ |
| 56 | #define TWL6030_MISC2 0xE5 |
| 57 | #define TWL6030_CFG_LDO_PD2 0xF5 |
| 58 | #define TWL6030_BACKUP_REG 0xFA |
| 59 | |
| 60 | #define STS_HW_CONDITIONS 0x21 |
| 61 | |
| 62 | /* In module TWL6030_MODULE_PM_MASTER */ |
| 63 | #define STS_HW_CONDITIONS 0x21 |
| 64 | #define STS_USB_ID BIT(2) |
| 65 | |
| 66 | /* In module TWL6030_MODULE_PM_RECEIVER */ |
| 67 | #define VUSB_CFG_TRANS 0x71 |
| 68 | #define VUSB_CFG_STATE 0x72 |
| 69 | #define VUSB_CFG_VOLTAGE 0x73 |
| 70 | |
| 71 | /* in module TWL6030_MODULE_MAIN_CHARGE */ |
| 72 | |
| 73 | #define CHARGERUSB_CTRL1 0x8 |
| 74 | |
| 75 | #define CONTROLLER_STAT1 0x03 |
| 76 | #define VBUS_DET BIT(2) |
| 77 | |
| 78 | struct twl6030_usb { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 79 | struct phy_companion comparator; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 80 | struct device *dev; |
| 81 | |
| 82 | /* for vbus reporting with irqs disabled */ |
| 83 | spinlock_t lock; |
| 84 | |
| 85 | struct regulator *usb3v3; |
| 86 | |
Tony Lindgren | b6a619a | 2016-05-31 10:05:23 -0500 | [diff] [blame] | 87 | /* used to check initial cable status after probe */ |
| 88 | struct delayed_work get_status_work; |
| 89 | |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 90 | /* used to set vbus, in atomic path */ |
| 91 | struct work_struct set_vbus_work; |
| 92 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 93 | int irq1; |
| 94 | int irq2; |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 95 | enum musb_vbus_id_status linkstat; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 96 | u8 asleep; |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 97 | bool vbus_enable; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 98 | }; |
| 99 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 100 | #define comparator_to_twl(x) container_of((x), struct twl6030_usb, comparator) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 101 | |
| 102 | /*-------------------------------------------------------------------------*/ |
| 103 | |
| 104 | static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module, |
| 105 | u8 data, u8 address) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | |
| 109 | ret = twl_i2c_write_u8(module, data, address); |
| 110 | if (ret < 0) |
| 111 | dev_err(twl->dev, |
| 112 | "Write[0x%x] Error %d\n", address, ret); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address) |
| 117 | { |
Dan Carpenter | 0b55149 | 2013-11-08 01:43:17 -0800 | [diff] [blame] | 118 | u8 data; |
| 119 | int ret; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 120 | |
| 121 | ret = twl_i2c_read_u8(module, &data, address); |
| 122 | if (ret >= 0) |
| 123 | ret = data; |
| 124 | else |
| 125 | dev_err(twl->dev, |
| 126 | "readb[0x%x,0x%x] Error %d\n", |
| 127 | module, address, ret); |
| 128 | return ret; |
| 129 | } |
| 130 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 131 | static int twl6030_start_srp(struct phy_companion *comparator) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 132 | { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 133 | struct twl6030_usb *twl = comparator_to_twl(comparator); |
Hema HK | 603ab52 | 2011-03-22 16:54:21 +0530 | [diff] [blame] | 134 | |
| 135 | twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET); |
| 136 | twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET); |
| 137 | |
| 138 | mdelay(100); |
| 139 | twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 144 | static int twl6030_usb_ldo_init(struct twl6030_usb *twl) |
| 145 | { |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 146 | /* 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] | 147 | twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_BACKUP_REG); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 148 | |
| 149 | /* Program CFG_LDO_PD2 register and set VUSB bit */ |
Sandhya Bankar | 13069bd | 2016-05-04 08:00:54 +0530 | [diff] [blame] | 150 | twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x1, TWL6030_CFG_LDO_PD2); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 151 | |
| 152 | /* Program MISC2 register and set bit VUSB_IN_VBAT */ |
Sandhya Bankar | 13069bd | 2016-05-04 08:00:54 +0530 | [diff] [blame] | 153 | twl6030_writeb(twl, TWL6030_MODULE_ID0, 0x10, TWL6030_MISC2); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 154 | |
Nicolae Rosia | 1ad528d | 2016-11-12 12:47:43 +0200 | [diff] [blame] | 155 | twl->usb3v3 = regulator_get(twl->dev, "usb"); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 156 | if (IS_ERR(twl->usb3v3)) |
| 157 | return -ENODEV; |
| 158 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 159 | /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */ |
| 160 | twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET); |
| 161 | |
| 162 | /* |
| 163 | * Program the USB_ID_CTRL_SET register to enable GND drive |
| 164 | * and the ID comparators |
| 165 | */ |
| 166 | twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Greg Kroah-Hartman | 7f26ee4 | 2018-01-23 11:24:06 +0100 | [diff] [blame] | 171 | static ssize_t vbus_show(struct device *dev, |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 172 | struct device_attribute *attr, char *buf) |
| 173 | { |
| 174 | struct twl6030_usb *twl = dev_get_drvdata(dev); |
| 175 | unsigned long flags; |
| 176 | int ret = -EINVAL; |
| 177 | |
| 178 | spin_lock_irqsave(&twl->lock, flags); |
| 179 | |
| 180 | switch (twl->linkstat) { |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 181 | case MUSB_VBUS_VALID: |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 182 | ret = snprintf(buf, PAGE_SIZE, "vbus\n"); |
| 183 | break; |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 184 | case MUSB_ID_GROUND: |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 185 | ret = snprintf(buf, PAGE_SIZE, "id\n"); |
| 186 | break; |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 187 | case MUSB_VBUS_OFF: |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 188 | ret = snprintf(buf, PAGE_SIZE, "none\n"); |
| 189 | break; |
| 190 | default: |
| 191 | ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n"); |
| 192 | } |
| 193 | spin_unlock_irqrestore(&twl->lock, flags); |
| 194 | |
| 195 | return ret; |
| 196 | } |
Greg Kroah-Hartman | 7f26ee4 | 2018-01-23 11:24:06 +0100 | [diff] [blame] | 197 | static DEVICE_ATTR_RO(vbus); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 198 | |
Greg Kroah-Hartman | 891c1da | 2019-08-06 09:32:34 +0200 | [diff] [blame] | 199 | static struct attribute *twl6030_attrs[] = { |
| 200 | &dev_attr_vbus.attr, |
| 201 | NULL, |
| 202 | }; |
| 203 | ATTRIBUTE_GROUPS(twl6030); |
| 204 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 205 | static irqreturn_t twl6030_usb_irq(int irq, void *_twl) |
| 206 | { |
| 207 | struct twl6030_usb *twl = _twl; |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 208 | enum musb_vbus_id_status status = MUSB_UNKNOWN; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 209 | u8 vbus_state, hw_state; |
Fabio Baltieri | bb54542 | 2013-04-03 16:02:27 +0200 | [diff] [blame] | 210 | int ret; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 211 | |
| 212 | hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS); |
| 213 | |
| 214 | vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE, |
| 215 | CONTROLLER_STAT1); |
| 216 | if (!(hw_state & STS_USB_ID)) { |
| 217 | if (vbus_state & VBUS_DET) { |
Fabio Baltieri | bb54542 | 2013-04-03 16:02:27 +0200 | [diff] [blame] | 218 | ret = regulator_enable(twl->usb3v3); |
| 219 | if (ret) |
| 220 | dev_err(twl->dev, "Failed to enable usb3v3\n"); |
| 221 | |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 222 | twl->asleep = 1; |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 223 | status = MUSB_VBUS_VALID; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 224 | twl->linkstat = status; |
Tony Lindgren | 12b7db2 | 2016-05-31 10:05:19 -0500 | [diff] [blame] | 225 | ret = musb_mailbox(status); |
| 226 | if (ret) |
| 227 | twl->linkstat = MUSB_UNKNOWN; |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 228 | } else { |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 229 | if (twl->linkstat != MUSB_UNKNOWN) { |
| 230 | status = MUSB_VBUS_OFF; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +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; |
Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 235 | if (twl->asleep) { |
| 236 | regulator_disable(twl->usb3v3); |
| 237 | twl->asleep = 0; |
| 238 | } |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 239 | } |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | sysfs_notify(&twl->dev->kobj, NULL, "vbus"); |
| 243 | |
| 244 | return IRQ_HANDLED; |
| 245 | } |
| 246 | |
| 247 | static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl) |
| 248 | { |
| 249 | struct twl6030_usb *twl = _twl; |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 250 | enum musb_vbus_id_status status = MUSB_UNKNOWN; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 251 | u8 hw_state; |
Fabio Baltieri | bb54542 | 2013-04-03 16:02:27 +0200 | [diff] [blame] | 252 | int ret; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 253 | |
| 254 | hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS); |
| 255 | |
| 256 | if (hw_state & STS_USB_ID) { |
Fabio Baltieri | bb54542 | 2013-04-03 16:02:27 +0200 | [diff] [blame] | 257 | ret = regulator_enable(twl->usb3v3); |
| 258 | if (ret) |
| 259 | dev_err(twl->dev, "Failed to enable usb3v3\n"); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 260 | |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 261 | twl->asleep = 1; |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 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); |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 264 | status = MUSB_ID_GROUND; |
Hema HK | 31e9992 | 2011-02-17 12:06:05 +0530 | [diff] [blame] | 265 | twl->linkstat = status; |
Tony Lindgren | 12b7db2 | 2016-05-31 10:05:19 -0500 | [diff] [blame] | 266 | ret = musb_mailbox(status); |
| 267 | if (ret) |
| 268 | twl->linkstat = MUSB_UNKNOWN; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 269 | } else { |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 270 | twl6030_writeb(twl, TWL_MODULE_USB, 0x10, USB_ID_INT_EN_HI_CLR); |
| 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 | } |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 273 | twl6030_writeb(twl, TWL_MODULE_USB, status, USB_ID_INT_LATCH_CLR); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 274 | |
| 275 | return IRQ_HANDLED; |
| 276 | } |
| 277 | |
Tony Lindgren | b6a619a | 2016-05-31 10:05:23 -0500 | [diff] [blame] | 278 | static void twl6030_status_work(struct work_struct *work) |
| 279 | { |
| 280 | struct twl6030_usb *twl = container_of(work, struct twl6030_usb, |
| 281 | get_status_work.work); |
| 282 | |
| 283 | twl6030_usb_irq(twl->irq2, twl); |
| 284 | twl6030_usbotg_irq(twl->irq1, twl); |
| 285 | } |
| 286 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 287 | static int twl6030_enable_irq(struct twl6030_usb *twl) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 288 | { |
Moiz Sonasath | dc8738d | 2012-06-12 20:17:12 +0300 | [diff] [blame] | 289 | 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] | 290 | twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C); |
| 291 | twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C); |
| 292 | |
| 293 | twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK, |
| 294 | REG_INT_MSK_LINE_C); |
| 295 | twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK, |
| 296 | REG_INT_MSK_STS_C); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 301 | static void otg_set_vbus_work(struct work_struct *data) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 302 | { |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 303 | struct twl6030_usb *twl = container_of(data, struct twl6030_usb, |
| 304 | set_vbus_work); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1 |
| 308 | * register. This enables boost mode. |
| 309 | */ |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 310 | |
| 311 | if (twl->vbus_enable) |
Sandhya Bankar | 13069bd | 2016-05-04 08:00:54 +0530 | [diff] [blame] | 312 | twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x40, |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 313 | CHARGERUSB_CTRL1); |
| 314 | else |
Sandhya Bankar | 13069bd | 2016-05-04 08:00:54 +0530 | [diff] [blame] | 315 | twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE, 0x00, |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 316 | CHARGERUSB_CTRL1); |
| 317 | } |
| 318 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 319 | static int twl6030_set_vbus(struct phy_companion *comparator, bool enabled) |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 320 | { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 321 | struct twl6030_usb *twl = comparator_to_twl(comparator); |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 322 | |
| 323 | twl->vbus_enable = enabled; |
| 324 | schedule_work(&twl->set_vbus_work); |
| 325 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 329 | static int twl6030_usb_probe(struct platform_device *pdev) |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 330 | { |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 331 | u32 ret; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 332 | struct twl6030_usb *twl; |
| 333 | int status, err; |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 334 | struct device_node *np = pdev->dev.of_node; |
| 335 | struct device *dev = &pdev->dev; |
Nicolae Rosia | 1ad528d | 2016-11-12 12:47:43 +0200 | [diff] [blame] | 336 | |
| 337 | if (!np) { |
| 338 | dev_err(dev, "no DT info\n"); |
| 339 | return -EINVAL; |
| 340 | } |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 341 | |
Sachin Kamat | 2ee8ff3 | 2013-12-21 15:20:09 +0530 | [diff] [blame] | 342 | twl = devm_kzalloc(dev, sizeof(*twl), GFP_KERNEL); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 343 | if (!twl) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | twl->dev = &pdev->dev; |
| 347 | twl->irq1 = platform_get_irq(pdev, 0); |
| 348 | twl->irq2 = platform_get_irq(pdev, 1); |
Tony Lindgren | 8055555 | 2015-11-30 21:37:12 -0800 | [diff] [blame] | 349 | twl->linkstat = MUSB_UNKNOWN; |
Heikki Krogerus | 46b8f6b | 2012-02-13 13:24:12 +0200 | [diff] [blame] | 350 | |
Sergey Shtylyov | 0881e22 | 2021-08-09 23:53:16 +0300 | [diff] [blame] | 351 | if (twl->irq1 < 0) |
| 352 | return twl->irq1; |
| 353 | if (twl->irq2 < 0) |
| 354 | return twl->irq2; |
| 355 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 356 | twl->comparator.set_vbus = twl6030_set_vbus; |
| 357 | twl->comparator.start_srp = twl6030_start_srp; |
Heikki Krogerus | 46b8f6b | 2012-02-13 13:24:12 +0200 | [diff] [blame] | 358 | |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 359 | ret = omap_usb2_set_comparator(&twl->comparator); |
| 360 | if (ret == -ENODEV) { |
| 361 | dev_info(&pdev->dev, "phy not ready, deferring probe"); |
| 362 | return -EPROBE_DEFER; |
| 363 | } |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 364 | |
| 365 | /* init spinlock for workqueue */ |
| 366 | spin_lock_init(&twl->lock); |
| 367 | |
| 368 | err = twl6030_usb_ldo_init(twl); |
| 369 | if (err) { |
| 370 | dev_err(&pdev->dev, "ldo init failed\n"); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 371 | return err; |
| 372 | } |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 373 | |
| 374 | platform_set_drvdata(pdev, twl); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 375 | |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 376 | INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work); |
Tony Lindgren | b6a619a | 2016-05-31 10:05:23 -0500 | [diff] [blame] | 377 | INIT_DELAYED_WORK(&twl->get_status_work, twl6030_status_work); |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 378 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 379 | status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq, |
Ming Lei | 8f9d973 | 2012-05-17 11:38:24 +0800 | [diff] [blame] | 380 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 381 | "twl6030_usb", twl); |
| 382 | if (status < 0) { |
| 383 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
| 384 | twl->irq1, status); |
Christophe JAILLET | f058764 | 2020-04-27 22:21:16 +0200 | [diff] [blame] | 385 | goto err_put_regulator; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq, |
Ming Lei | 8f9d973 | 2012-05-17 11:38:24 +0800 | [diff] [blame] | 389 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 390 | "twl6030_usb", twl); |
| 391 | if (status < 0) { |
| 392 | dev_err(&pdev->dev, "can't get IRQ %d, err %d\n", |
| 393 | twl->irq2, status); |
Christophe JAILLET | f058764 | 2020-04-27 22:21:16 +0200 | [diff] [blame] | 394 | goto err_free_irq1; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 395 | } |
| 396 | |
Hema HK | 6dc2503 | 2011-02-17 12:06:04 +0530 | [diff] [blame] | 397 | twl->asleep = 0; |
Kishon Vijay Abraham I | 0e98de6 | 2012-09-06 20:27:07 +0530 | [diff] [blame] | 398 | twl6030_enable_irq(twl); |
Tony Lindgren | b6a619a | 2016-05-31 10:05:23 -0500 | [diff] [blame] | 399 | schedule_delayed_work(&twl->get_status_work, HZ); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 400 | dev_info(&pdev->dev, "Initialized TWL6030 USB module\n"); |
| 401 | |
| 402 | return 0; |
Christophe JAILLET | f058764 | 2020-04-27 22:21:16 +0200 | [diff] [blame] | 403 | |
| 404 | err_free_irq1: |
| 405 | free_irq(twl->irq1, twl); |
| 406 | err_put_regulator: |
| 407 | regulator_put(twl->usb3v3); |
| 408 | |
| 409 | return status; |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 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 | |
Sven Van Asbroeck | 5895d311 | 2019-02-11 10:04:26 -0500 | [diff] [blame] | 416 | cancel_delayed_work_sync(&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); |
Moiz Sonasath | 5bf5450 | 2011-06-27 10:01:01 -0500 | [diff] [blame] | 424 | cancel_work_sync(&twl->set_vbus_work); |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 429 | static const struct of_device_id twl6030_usb_id_table[] = { |
| 430 | { .compatible = "ti,twl6030-usb" }, |
| 431 | {} |
| 432 | }; |
| 433 | MODULE_DEVICE_TABLE(of, twl6030_usb_id_table); |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 434 | |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 435 | static struct platform_driver twl6030_usb_driver = { |
| 436 | .probe = twl6030_usb_probe, |
Dmitry Torokhov | 39d3568 | 2013-02-24 00:55:07 -0800 | [diff] [blame] | 437 | .remove = twl6030_usb_remove, |
Hema HK | c33fad0 | 2010-12-10 17:58:20 +0530 | [diff] [blame] | 438 | .driver = { |
| 439 | .name = "twl6030_usb", |
Kishon Vijay Abraham I | ff0a1f3 | 2012-09-06 20:27:08 +0530 | [diff] [blame] | 440 | .of_match_table = of_match_ptr(twl6030_usb_id_table), |
Greg Kroah-Hartman | 891c1da | 2019-08-06 09:32:34 +0200 | [diff] [blame] | 441 | .dev_groups = twl6030_groups, |
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"); |