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