blob: b4eda02c97f71f599afd973e2ec4db96faf964d5 [file] [log] [blame]
Hema HKc33fad02010-12-10 17:58:20 +05301/*
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>
28#include <linux/usb/otg.h>
29#include <linux/i2c/twl.h>
30#include <linux/regulator/consumer.h>
31#include <linux/err.h>
32#include <linux/notifier.h>
33#include <linux/slab.h>
34
35/* usb register definitions */
36#define USB_VENDOR_ID_LSB 0x00
37#define USB_VENDOR_ID_MSB 0x01
38#define USB_PRODUCT_ID_LSB 0x02
39#define USB_PRODUCT_ID_MSB 0x03
40#define USB_VBUS_CTRL_SET 0x04
41#define USB_VBUS_CTRL_CLR 0x05
42#define USB_ID_CTRL_SET 0x06
43#define USB_ID_CTRL_CLR 0x07
44#define USB_VBUS_INT_SRC 0x08
45#define USB_VBUS_INT_LATCH_SET 0x09
46#define USB_VBUS_INT_LATCH_CLR 0x0A
47#define USB_VBUS_INT_EN_LO_SET 0x0B
48#define USB_VBUS_INT_EN_LO_CLR 0x0C
49#define USB_VBUS_INT_EN_HI_SET 0x0D
50#define USB_VBUS_INT_EN_HI_CLR 0x0E
51#define USB_ID_INT_SRC 0x0F
52#define USB_ID_INT_LATCH_SET 0x10
53#define USB_ID_INT_LATCH_CLR 0x11
54
55#define USB_ID_INT_EN_LO_SET 0x12
56#define USB_ID_INT_EN_LO_CLR 0x13
57#define USB_ID_INT_EN_HI_SET 0x14
58#define USB_ID_INT_EN_HI_CLR 0x15
59#define USB_OTG_ADP_CTRL 0x16
60#define USB_OTG_ADP_HIGH 0x17
61#define USB_OTG_ADP_LOW 0x18
62#define USB_OTG_ADP_RISE 0x19
63#define USB_OTG_REVISION 0x1A
64
65/* to be moved to LDO */
66#define TWL6030_MISC2 0xE5
67#define TWL6030_CFG_LDO_PD2 0xF5
68#define TWL6030_BACKUP_REG 0xFA
69
70#define STS_HW_CONDITIONS 0x21
71
72/* In module TWL6030_MODULE_PM_MASTER */
73#define STS_HW_CONDITIONS 0x21
74#define STS_USB_ID BIT(2)
75
76/* In module TWL6030_MODULE_PM_RECEIVER */
77#define VUSB_CFG_TRANS 0x71
78#define VUSB_CFG_STATE 0x72
79#define VUSB_CFG_VOLTAGE 0x73
80
81/* in module TWL6030_MODULE_MAIN_CHARGE */
82
83#define CHARGERUSB_CTRL1 0x8
84
85#define CONTROLLER_STAT1 0x03
86#define VBUS_DET BIT(2)
87
88struct twl6030_usb {
89 struct otg_transceiver otg;
90 struct device *dev;
91
92 /* for vbus reporting with irqs disabled */
93 spinlock_t lock;
94
95 struct regulator *usb3v3;
96
97 int irq1;
98 int irq2;
99 u8 linkstat;
100 u8 asleep;
101 bool irq_enabled;
102};
103
104#define xceiv_to_twl(x) container_of((x), struct twl6030_usb, otg);
105
106/*-------------------------------------------------------------------------*/
107
108static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
109 u8 data, u8 address)
110{
111 int ret = 0;
112
113 ret = twl_i2c_write_u8(module, data, address);
114 if (ret < 0)
115 dev_err(twl->dev,
116 "Write[0x%x] Error %d\n", address, ret);
117 return ret;
118}
119
120static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
121{
122 u8 data, ret = 0;
123
124 ret = twl_i2c_read_u8(module, &data, address);
125 if (ret >= 0)
126 ret = data;
127 else
128 dev_err(twl->dev,
129 "readb[0x%x,0x%x] Error %d\n",
130 module, address, ret);
131 return ret;
132}
133
134/*-------------------------------------------------------------------------*/
135static int twl6030_set_phy_clk(struct otg_transceiver *x, int on)
136{
137 struct twl6030_usb *twl;
138 struct device *dev;
139 struct twl4030_usb_data *pdata;
140
141 twl = xceiv_to_twl(x);
142 dev = twl->dev;
143 pdata = dev->platform_data;
144
145 pdata->phy_set_clock(twl->dev, on);
146
147 return 0;
148}
149
150static int twl6030_phy_init(struct otg_transceiver *x)
151{
Hema HKc33fad02010-12-10 17:58:20 +0530152 struct twl6030_usb *twl;
153 struct device *dev;
154 struct twl4030_usb_data *pdata;
155
156 twl = xceiv_to_twl(x);
157 dev = twl->dev;
158 pdata = dev->platform_data;
159
Hema HK31e99922011-02-17 12:06:05 +0530160 if (twl->linkstat == USB_EVENT_ID)
Hema HKc33fad02010-12-10 17:58:20 +0530161 pdata->phy_power(twl->dev, 1, 1);
162 else
163 pdata->phy_power(twl->dev, 0, 1);
164
165 return 0;
166}
167
168static void twl6030_phy_shutdown(struct otg_transceiver *x)
169{
170 struct twl6030_usb *twl;
171 struct device *dev;
172 struct twl4030_usb_data *pdata;
173
174 twl = xceiv_to_twl(x);
175 dev = twl->dev;
176 pdata = dev->platform_data;
177 pdata->phy_power(twl->dev, 0, 0);
Hema HKc33fad02010-12-10 17:58:20 +0530178}
179
Hema HK070b8ed2011-02-17 12:06:08 +0530180static int twl6030_phy_suspend(struct otg_transceiver *x, int suspend)
181{
182 struct twl6030_usb *twl = xceiv_to_twl(x);
183 struct device *dev = twl->dev;
184 struct twl4030_usb_data *pdata = dev->platform_data;
185
186 pdata->phy_suspend(dev, suspend);
187
188 return 0;
189}
190
Hema HKc33fad02010-12-10 17:58:20 +0530191static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
192{
193
194 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
195 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
196
197 /* Program CFG_LDO_PD2 register and set VUSB bit */
198 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
199
200 /* Program MISC2 register and set bit VUSB_IN_VBAT */
201 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
202
203 twl->usb3v3 = regulator_get(twl->dev, "vusb");
204 if (IS_ERR(twl->usb3v3))
205 return -ENODEV;
206
Hema HKc33fad02010-12-10 17:58:20 +0530207 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
208 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
209
210 /*
211 * Program the USB_ID_CTRL_SET register to enable GND drive
212 * and the ID comparators
213 */
214 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
215
216 return 0;
217}
218
219static ssize_t twl6030_usb_vbus_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 struct twl6030_usb *twl = dev_get_drvdata(dev);
223 unsigned long flags;
224 int ret = -EINVAL;
225
226 spin_lock_irqsave(&twl->lock, flags);
227
228 switch (twl->linkstat) {
229 case USB_EVENT_VBUS:
230 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
231 break;
232 case USB_EVENT_ID:
233 ret = snprintf(buf, PAGE_SIZE, "id\n");
234 break;
235 case USB_EVENT_NONE:
236 ret = snprintf(buf, PAGE_SIZE, "none\n");
237 break;
238 default:
239 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
240 }
241 spin_unlock_irqrestore(&twl->lock, flags);
242
243 return ret;
244}
245static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
246
247static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
248{
249 struct twl6030_usb *twl = _twl;
250 int status;
251 u8 vbus_state, hw_state;
252
253 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
254
255 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
256 CONTROLLER_STAT1);
257 if (!(hw_state & STS_USB_ID)) {
258 if (vbus_state & VBUS_DET) {
Hema HK6dc25032011-02-17 12:06:04 +0530259 regulator_enable(twl->usb3v3);
260 twl->asleep = 1;
Hema HKc33fad02010-12-10 17:58:20 +0530261 status = USB_EVENT_VBUS;
262 twl->otg.default_a = false;
263 twl->otg.state = OTG_STATE_B_IDLE;
Hema HKc33fad02010-12-10 17:58:20 +0530264 twl->linkstat = status;
265 blocking_notifier_call_chain(&twl->otg.notifier,
266 status, twl->otg.gadget);
Hema HK6dc25032011-02-17 12:06:04 +0530267 } else {
268 status = USB_EVENT_NONE;
269 twl->linkstat = status;
270 blocking_notifier_call_chain(&twl->otg.notifier,
271 status, twl->otg.gadget);
272 if (twl->asleep) {
273 regulator_disable(twl->usb3v3);
274 twl->asleep = 0;
275 }
Hema HKc33fad02010-12-10 17:58:20 +0530276 }
277 }
278 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
279
280 return IRQ_HANDLED;
281}
282
283static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
284{
285 struct twl6030_usb *twl = _twl;
286 int status = USB_EVENT_NONE;
287 u8 hw_state;
288
289 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
290
291 if (hw_state & STS_USB_ID) {
292
Hema HK6dc25032011-02-17 12:06:04 +0530293 regulator_enable(twl->usb3v3);
294 twl->asleep = 1;
Hema HKc33fad02010-12-10 17:58:20 +0530295 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
296 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
297 0x10);
298 status = USB_EVENT_ID;
299 twl->otg.default_a = true;
300 twl->otg.state = OTG_STATE_A_IDLE;
Hema HK31e99922011-02-17 12:06:05 +0530301 twl->linkstat = status;
Hema HKc33fad02010-12-10 17:58:20 +0530302 blocking_notifier_call_chain(&twl->otg.notifier, status,
303 twl->otg.gadget);
304 } else {
305 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
306 0x10);
307 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
308 0x1);
309 }
310 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
Hema HKc33fad02010-12-10 17:58:20 +0530311
312 return IRQ_HANDLED;
313}
314
315static int twl6030_set_peripheral(struct otg_transceiver *x,
316 struct usb_gadget *gadget)
317{
318 struct twl6030_usb *twl;
319
320 if (!x)
321 return -ENODEV;
322
323 twl = xceiv_to_twl(x);
324 twl->otg.gadget = gadget;
325 if (!gadget)
326 twl->otg.state = OTG_STATE_UNDEFINED;
327
328 return 0;
329}
330
331static int twl6030_enable_irq(struct otg_transceiver *x)
332{
333 struct twl6030_usb *twl = xceiv_to_twl(x);
334
335 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
336 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
337 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
338
339 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
340 REG_INT_MSK_LINE_C);
341 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
342 REG_INT_MSK_STS_C);
343 twl6030_usb_irq(twl->irq2, twl);
344 twl6030_usbotg_irq(twl->irq1, twl);
345
346 return 0;
347}
348
349static int twl6030_set_vbus(struct otg_transceiver *x, bool enabled)
350{
351 struct twl6030_usb *twl = xceiv_to_twl(x);
352
353 /*
354 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
355 * register. This enables boost mode.
356 */
357 if (enabled)
358 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
359 CHARGERUSB_CTRL1);
360 else
361 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
362 CHARGERUSB_CTRL1);
363 return 0;
364}
365
366static int twl6030_set_host(struct otg_transceiver *x, struct usb_bus *host)
367{
368 struct twl6030_usb *twl;
369
370 if (!x)
371 return -ENODEV;
372
373 twl = xceiv_to_twl(x);
374 twl->otg.host = host;
375 if (!host)
376 twl->otg.state = OTG_STATE_UNDEFINED;
377 return 0;
378}
379
380static int __devinit twl6030_usb_probe(struct platform_device *pdev)
381{
382 struct twl6030_usb *twl;
383 int status, err;
384 struct twl4030_usb_data *pdata;
385 struct device *dev = &pdev->dev;
386 pdata = dev->platform_data;
387
388 twl = kzalloc(sizeof *twl, GFP_KERNEL);
389 if (!twl)
390 return -ENOMEM;
391
392 twl->dev = &pdev->dev;
393 twl->irq1 = platform_get_irq(pdev, 0);
394 twl->irq2 = platform_get_irq(pdev, 1);
395 twl->otg.dev = twl->dev;
396 twl->otg.label = "twl6030";
397 twl->otg.set_host = twl6030_set_host;
398 twl->otg.set_peripheral = twl6030_set_peripheral;
399 twl->otg.set_vbus = twl6030_set_vbus;
400 twl->otg.init = twl6030_phy_init;
401 twl->otg.shutdown = twl6030_phy_shutdown;
Hema HK070b8ed2011-02-17 12:06:08 +0530402 twl->otg.set_suspend = twl6030_phy_suspend;
Hema HKc33fad02010-12-10 17:58:20 +0530403
404 /* init spinlock for workqueue */
405 spin_lock_init(&twl->lock);
406
407 err = twl6030_usb_ldo_init(twl);
408 if (err) {
409 dev_err(&pdev->dev, "ldo init failed\n");
410 kfree(twl);
411 return err;
412 }
413 otg_set_transceiver(&twl->otg);
414
415 platform_set_drvdata(pdev, twl);
416 if (device_create_file(&pdev->dev, &dev_attr_vbus))
417 dev_warn(&pdev->dev, "could not create sysfs file\n");
418
419 BLOCKING_INIT_NOTIFIER_HEAD(&twl->otg.notifier);
420
421 twl->irq_enabled = true;
422 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
423 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
424 "twl6030_usb", twl);
425 if (status < 0) {
426 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
427 twl->irq1, status);
428 device_remove_file(twl->dev, &dev_attr_vbus);
429 kfree(twl);
430 return status;
431 }
432
433 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
434 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
435 "twl6030_usb", twl);
436 if (status < 0) {
437 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
438 twl->irq2, status);
439 free_irq(twl->irq1, twl);
440 device_remove_file(twl->dev, &dev_attr_vbus);
441 kfree(twl);
442 return status;
443 }
444
Hema HK6dc25032011-02-17 12:06:04 +0530445 twl->asleep = 0;
Hema HKc33fad02010-12-10 17:58:20 +0530446 pdata->phy_init(dev);
Hema HK070b8ed2011-02-17 12:06:08 +0530447 twl6030_phy_suspend(&twl->otg, 0);
Hema HKc33fad02010-12-10 17:58:20 +0530448 twl6030_enable_irq(&twl->otg);
449 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
450
451 return 0;
452}
453
454static int __exit twl6030_usb_remove(struct platform_device *pdev)
455{
456 struct twl6030_usb *twl = platform_get_drvdata(pdev);
457
458 struct twl4030_usb_data *pdata;
459 struct device *dev = &pdev->dev;
460 pdata = dev->platform_data;
461
462 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
463 REG_INT_MSK_LINE_C);
464 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
465 REG_INT_MSK_STS_C);
466 free_irq(twl->irq1, twl);
467 free_irq(twl->irq2, twl);
468 regulator_put(twl->usb3v3);
469 pdata->phy_exit(twl->dev);
470 device_remove_file(twl->dev, &dev_attr_vbus);
471 kfree(twl);
472
473 return 0;
474}
475
476static struct platform_driver twl6030_usb_driver = {
477 .probe = twl6030_usb_probe,
478 .remove = __exit_p(twl6030_usb_remove),
479 .driver = {
480 .name = "twl6030_usb",
481 .owner = THIS_MODULE,
482 },
483};
484
485static int __init twl6030_usb_init(void)
486{
487 return platform_driver_register(&twl6030_usb_driver);
488}
489subsys_initcall(twl6030_usb_init);
490
491static void __exit twl6030_usb_exit(void)
492{
493 platform_driver_unregister(&twl6030_usb_driver);
494}
495module_exit(twl6030_usb_exit);
496
497MODULE_ALIAS("platform:twl6030_usb");
498MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
499MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
500MODULE_LICENSE("GPL");