blob: 05f17b77d54c1e667aaa09667bec247f8fdbe2a6 [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;
Hema HK647b2d92011-02-17 12:06:09 +0530265 twl->otg.last_event = status;
Hema HKc33fad02010-12-10 17:58:20 +0530266 blocking_notifier_call_chain(&twl->otg.notifier,
267 status, twl->otg.gadget);
Hema HK6dc25032011-02-17 12:06:04 +0530268 } else {
269 status = USB_EVENT_NONE;
270 twl->linkstat = status;
Hema HK647b2d92011-02-17 12:06:09 +0530271 twl->otg.last_event = status;
Hema HK6dc25032011-02-17 12:06:04 +0530272 blocking_notifier_call_chain(&twl->otg.notifier,
273 status, twl->otg.gadget);
274 if (twl->asleep) {
275 regulator_disable(twl->usb3v3);
276 twl->asleep = 0;
277 }
Hema HKc33fad02010-12-10 17:58:20 +0530278 }
279 }
280 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
281
282 return IRQ_HANDLED;
283}
284
285static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
286{
287 struct twl6030_usb *twl = _twl;
288 int status = USB_EVENT_NONE;
289 u8 hw_state;
290
291 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
292
293 if (hw_state & STS_USB_ID) {
294
Hema HK6dc25032011-02-17 12:06:04 +0530295 regulator_enable(twl->usb3v3);
296 twl->asleep = 1;
Hema HKc33fad02010-12-10 17:58:20 +0530297 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
298 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
299 0x10);
300 status = USB_EVENT_ID;
301 twl->otg.default_a = true;
302 twl->otg.state = OTG_STATE_A_IDLE;
Hema HK31e99922011-02-17 12:06:05 +0530303 twl->linkstat = status;
Hema HK647b2d92011-02-17 12:06:09 +0530304 twl->otg.last_event = status;
Hema HKc33fad02010-12-10 17:58:20 +0530305 blocking_notifier_call_chain(&twl->otg.notifier, status,
306 twl->otg.gadget);
307 } else {
308 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
309 0x10);
310 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
311 0x1);
312 }
313 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
Hema HKc33fad02010-12-10 17:58:20 +0530314
315 return IRQ_HANDLED;
316}
317
318static int twl6030_set_peripheral(struct otg_transceiver *x,
319 struct usb_gadget *gadget)
320{
321 struct twl6030_usb *twl;
322
323 if (!x)
324 return -ENODEV;
325
326 twl = xceiv_to_twl(x);
327 twl->otg.gadget = gadget;
328 if (!gadget)
329 twl->otg.state = OTG_STATE_UNDEFINED;
330
331 return 0;
332}
333
334static int twl6030_enable_irq(struct otg_transceiver *x)
335{
336 struct twl6030_usb *twl = xceiv_to_twl(x);
337
338 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
339 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
340 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
341
342 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
343 REG_INT_MSK_LINE_C);
344 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
345 REG_INT_MSK_STS_C);
346 twl6030_usb_irq(twl->irq2, twl);
347 twl6030_usbotg_irq(twl->irq1, twl);
348
349 return 0;
350}
351
352static int twl6030_set_vbus(struct otg_transceiver *x, bool enabled)
353{
354 struct twl6030_usb *twl = xceiv_to_twl(x);
355
356 /*
357 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
358 * register. This enables boost mode.
359 */
360 if (enabled)
361 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
362 CHARGERUSB_CTRL1);
363 else
364 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
365 CHARGERUSB_CTRL1);
366 return 0;
367}
368
369static int twl6030_set_host(struct otg_transceiver *x, struct usb_bus *host)
370{
371 struct twl6030_usb *twl;
372
373 if (!x)
374 return -ENODEV;
375
376 twl = xceiv_to_twl(x);
377 twl->otg.host = host;
378 if (!host)
379 twl->otg.state = OTG_STATE_UNDEFINED;
380 return 0;
381}
382
383static int __devinit twl6030_usb_probe(struct platform_device *pdev)
384{
385 struct twl6030_usb *twl;
386 int status, err;
387 struct twl4030_usb_data *pdata;
388 struct device *dev = &pdev->dev;
389 pdata = dev->platform_data;
390
391 twl = kzalloc(sizeof *twl, GFP_KERNEL);
392 if (!twl)
393 return -ENOMEM;
394
395 twl->dev = &pdev->dev;
396 twl->irq1 = platform_get_irq(pdev, 0);
397 twl->irq2 = platform_get_irq(pdev, 1);
398 twl->otg.dev = twl->dev;
399 twl->otg.label = "twl6030";
400 twl->otg.set_host = twl6030_set_host;
401 twl->otg.set_peripheral = twl6030_set_peripheral;
402 twl->otg.set_vbus = twl6030_set_vbus;
403 twl->otg.init = twl6030_phy_init;
404 twl->otg.shutdown = twl6030_phy_shutdown;
Hema HK070b8ed2011-02-17 12:06:08 +0530405 twl->otg.set_suspend = twl6030_phy_suspend;
Hema HKc33fad02010-12-10 17:58:20 +0530406
407 /* init spinlock for workqueue */
408 spin_lock_init(&twl->lock);
409
410 err = twl6030_usb_ldo_init(twl);
411 if (err) {
412 dev_err(&pdev->dev, "ldo init failed\n");
413 kfree(twl);
414 return err;
415 }
416 otg_set_transceiver(&twl->otg);
417
418 platform_set_drvdata(pdev, twl);
419 if (device_create_file(&pdev->dev, &dev_attr_vbus))
420 dev_warn(&pdev->dev, "could not create sysfs file\n");
421
422 BLOCKING_INIT_NOTIFIER_HEAD(&twl->otg.notifier);
423
424 twl->irq_enabled = true;
425 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
426 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
427 "twl6030_usb", twl);
428 if (status < 0) {
429 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
430 twl->irq1, status);
431 device_remove_file(twl->dev, &dev_attr_vbus);
432 kfree(twl);
433 return status;
434 }
435
436 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
437 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
438 "twl6030_usb", twl);
439 if (status < 0) {
440 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
441 twl->irq2, status);
442 free_irq(twl->irq1, twl);
443 device_remove_file(twl->dev, &dev_attr_vbus);
444 kfree(twl);
445 return status;
446 }
447
Hema HK6dc25032011-02-17 12:06:04 +0530448 twl->asleep = 0;
Hema HKc33fad02010-12-10 17:58:20 +0530449 pdata->phy_init(dev);
Hema HK070b8ed2011-02-17 12:06:08 +0530450 twl6030_phy_suspend(&twl->otg, 0);
Hema HKc33fad02010-12-10 17:58:20 +0530451 twl6030_enable_irq(&twl->otg);
452 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
453
454 return 0;
455}
456
457static int __exit twl6030_usb_remove(struct platform_device *pdev)
458{
459 struct twl6030_usb *twl = platform_get_drvdata(pdev);
460
461 struct twl4030_usb_data *pdata;
462 struct device *dev = &pdev->dev;
463 pdata = dev->platform_data;
464
465 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
466 REG_INT_MSK_LINE_C);
467 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
468 REG_INT_MSK_STS_C);
469 free_irq(twl->irq1, twl);
470 free_irq(twl->irq2, twl);
471 regulator_put(twl->usb3v3);
472 pdata->phy_exit(twl->dev);
473 device_remove_file(twl->dev, &dev_attr_vbus);
474 kfree(twl);
475
476 return 0;
477}
478
479static struct platform_driver twl6030_usb_driver = {
480 .probe = twl6030_usb_probe,
481 .remove = __exit_p(twl6030_usb_remove),
482 .driver = {
483 .name = "twl6030_usb",
484 .owner = THIS_MODULE,
485 },
486};
487
488static int __init twl6030_usb_init(void)
489{
490 return platform_driver_register(&twl6030_usb_driver);
491}
492subsys_initcall(twl6030_usb_init);
493
494static void __exit twl6030_usb_exit(void)
495{
496 platform_driver_unregister(&twl6030_usb_driver);
497}
498module_exit(twl6030_usb_exit);
499
500MODULE_ALIAS("platform:twl6030_usb");
501MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
502MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
503MODULE_LICENSE("GPL");