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