blob: 88989e61430ca2bc26dea2f58a7fcb7bd37ff328 [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
180static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
181{
182
183 /* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
184 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);
185
186 /* Program CFG_LDO_PD2 register and set VUSB bit */
187 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);
188
189 /* Program MISC2 register and set bit VUSB_IN_VBAT */
190 twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);
191
192 twl->usb3v3 = regulator_get(twl->dev, "vusb");
193 if (IS_ERR(twl->usb3v3))
194 return -ENODEV;
195
Hema HKc33fad02010-12-10 17:58:20 +0530196 /* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
197 twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);
198
199 /*
200 * Program the USB_ID_CTRL_SET register to enable GND drive
201 * and the ID comparators
202 */
203 twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);
204
205 return 0;
206}
207
208static ssize_t twl6030_usb_vbus_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
210{
211 struct twl6030_usb *twl = dev_get_drvdata(dev);
212 unsigned long flags;
213 int ret = -EINVAL;
214
215 spin_lock_irqsave(&twl->lock, flags);
216
217 switch (twl->linkstat) {
218 case USB_EVENT_VBUS:
219 ret = snprintf(buf, PAGE_SIZE, "vbus\n");
220 break;
221 case USB_EVENT_ID:
222 ret = snprintf(buf, PAGE_SIZE, "id\n");
223 break;
224 case USB_EVENT_NONE:
225 ret = snprintf(buf, PAGE_SIZE, "none\n");
226 break;
227 default:
228 ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
229 }
230 spin_unlock_irqrestore(&twl->lock, flags);
231
232 return ret;
233}
234static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);
235
236static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
237{
238 struct twl6030_usb *twl = _twl;
239 int status;
240 u8 vbus_state, hw_state;
241
242 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
243
244 vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
245 CONTROLLER_STAT1);
246 if (!(hw_state & STS_USB_ID)) {
247 if (vbus_state & VBUS_DET) {
Hema HK6dc25032011-02-17 12:06:04 +0530248 regulator_enable(twl->usb3v3);
249 twl->asleep = 1;
Hema HKc33fad02010-12-10 17:58:20 +0530250 status = USB_EVENT_VBUS;
251 twl->otg.default_a = false;
252 twl->otg.state = OTG_STATE_B_IDLE;
Hema HKc33fad02010-12-10 17:58:20 +0530253 twl->linkstat = status;
254 blocking_notifier_call_chain(&twl->otg.notifier,
255 status, twl->otg.gadget);
Hema HK6dc25032011-02-17 12:06:04 +0530256 } else {
257 status = USB_EVENT_NONE;
258 twl->linkstat = status;
259 blocking_notifier_call_chain(&twl->otg.notifier,
260 status, twl->otg.gadget);
261 if (twl->asleep) {
262 regulator_disable(twl->usb3v3);
263 twl->asleep = 0;
264 }
Hema HKc33fad02010-12-10 17:58:20 +0530265 }
266 }
267 sysfs_notify(&twl->dev->kobj, NULL, "vbus");
268
269 return IRQ_HANDLED;
270}
271
272static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
273{
274 struct twl6030_usb *twl = _twl;
275 int status = USB_EVENT_NONE;
276 u8 hw_state;
277
278 hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);
279
280 if (hw_state & STS_USB_ID) {
281
Hema HK6dc25032011-02-17 12:06:04 +0530282 regulator_enable(twl->usb3v3);
283 twl->asleep = 1;
Hema HKc33fad02010-12-10 17:58:20 +0530284 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
285 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
286 0x10);
287 status = USB_EVENT_ID;
288 twl->otg.default_a = true;
289 twl->otg.state = OTG_STATE_A_IDLE;
Hema HK31e99922011-02-17 12:06:05 +0530290 twl->linkstat = status;
Hema HKc33fad02010-12-10 17:58:20 +0530291 blocking_notifier_call_chain(&twl->otg.notifier, status,
292 twl->otg.gadget);
293 } else {
294 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
295 0x10);
296 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
297 0x1);
298 }
299 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);
Hema HKc33fad02010-12-10 17:58:20 +0530300
301 return IRQ_HANDLED;
302}
303
304static int twl6030_set_peripheral(struct otg_transceiver *x,
305 struct usb_gadget *gadget)
306{
307 struct twl6030_usb *twl;
308
309 if (!x)
310 return -ENODEV;
311
312 twl = xceiv_to_twl(x);
313 twl->otg.gadget = gadget;
314 if (!gadget)
315 twl->otg.state = OTG_STATE_UNDEFINED;
316
317 return 0;
318}
319
320static int twl6030_enable_irq(struct otg_transceiver *x)
321{
322 struct twl6030_usb *twl = xceiv_to_twl(x);
323
324 twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
325 twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
326 twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);
327
328 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
329 REG_INT_MSK_LINE_C);
330 twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
331 REG_INT_MSK_STS_C);
332 twl6030_usb_irq(twl->irq2, twl);
333 twl6030_usbotg_irq(twl->irq1, twl);
334
335 return 0;
336}
337
338static int twl6030_set_vbus(struct otg_transceiver *x, bool enabled)
339{
340 struct twl6030_usb *twl = xceiv_to_twl(x);
341
342 /*
343 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
344 * register. This enables boost mode.
345 */
346 if (enabled)
347 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
348 CHARGERUSB_CTRL1);
349 else
350 twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
351 CHARGERUSB_CTRL1);
352 return 0;
353}
354
355static int twl6030_set_host(struct otg_transceiver *x, struct usb_bus *host)
356{
357 struct twl6030_usb *twl;
358
359 if (!x)
360 return -ENODEV;
361
362 twl = xceiv_to_twl(x);
363 twl->otg.host = host;
364 if (!host)
365 twl->otg.state = OTG_STATE_UNDEFINED;
366 return 0;
367}
368
369static int __devinit twl6030_usb_probe(struct platform_device *pdev)
370{
371 struct twl6030_usb *twl;
372 int status, err;
373 struct twl4030_usb_data *pdata;
374 struct device *dev = &pdev->dev;
375 pdata = dev->platform_data;
376
377 twl = kzalloc(sizeof *twl, GFP_KERNEL);
378 if (!twl)
379 return -ENOMEM;
380
381 twl->dev = &pdev->dev;
382 twl->irq1 = platform_get_irq(pdev, 0);
383 twl->irq2 = platform_get_irq(pdev, 1);
384 twl->otg.dev = twl->dev;
385 twl->otg.label = "twl6030";
386 twl->otg.set_host = twl6030_set_host;
387 twl->otg.set_peripheral = twl6030_set_peripheral;
388 twl->otg.set_vbus = twl6030_set_vbus;
389 twl->otg.init = twl6030_phy_init;
390 twl->otg.shutdown = twl6030_phy_shutdown;
391
392 /* init spinlock for workqueue */
393 spin_lock_init(&twl->lock);
394
395 err = twl6030_usb_ldo_init(twl);
396 if (err) {
397 dev_err(&pdev->dev, "ldo init failed\n");
398 kfree(twl);
399 return err;
400 }
401 otg_set_transceiver(&twl->otg);
402
403 platform_set_drvdata(pdev, twl);
404 if (device_create_file(&pdev->dev, &dev_attr_vbus))
405 dev_warn(&pdev->dev, "could not create sysfs file\n");
406
407 BLOCKING_INIT_NOTIFIER_HEAD(&twl->otg.notifier);
408
409 twl->irq_enabled = true;
410 status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
411 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
412 "twl6030_usb", twl);
413 if (status < 0) {
414 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
415 twl->irq1, status);
416 device_remove_file(twl->dev, &dev_attr_vbus);
417 kfree(twl);
418 return status;
419 }
420
421 status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
422 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
423 "twl6030_usb", twl);
424 if (status < 0) {
425 dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
426 twl->irq2, status);
427 free_irq(twl->irq1, twl);
428 device_remove_file(twl->dev, &dev_attr_vbus);
429 kfree(twl);
430 return status;
431 }
432
Hema HK6dc25032011-02-17 12:06:04 +0530433 twl->asleep = 0;
Hema HKc33fad02010-12-10 17:58:20 +0530434 pdata->phy_init(dev);
435 twl6030_enable_irq(&twl->otg);
436 dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");
437
438 return 0;
439}
440
441static int __exit twl6030_usb_remove(struct platform_device *pdev)
442{
443 struct twl6030_usb *twl = platform_get_drvdata(pdev);
444
445 struct twl4030_usb_data *pdata;
446 struct device *dev = &pdev->dev;
447 pdata = dev->platform_data;
448
449 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
450 REG_INT_MSK_LINE_C);
451 twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
452 REG_INT_MSK_STS_C);
453 free_irq(twl->irq1, twl);
454 free_irq(twl->irq2, twl);
455 regulator_put(twl->usb3v3);
456 pdata->phy_exit(twl->dev);
457 device_remove_file(twl->dev, &dev_attr_vbus);
458 kfree(twl);
459
460 return 0;
461}
462
463static struct platform_driver twl6030_usb_driver = {
464 .probe = twl6030_usb_probe,
465 .remove = __exit_p(twl6030_usb_remove),
466 .driver = {
467 .name = "twl6030_usb",
468 .owner = THIS_MODULE,
469 },
470};
471
472static int __init twl6030_usb_init(void)
473{
474 return platform_driver_register(&twl6030_usb_driver);
475}
476subsys_initcall(twl6030_usb_init);
477
478static void __exit twl6030_usb_exit(void)
479{
480 platform_driver_unregister(&twl6030_usb_driver);
481}
482module_exit(twl6030_usb_exit);
483
484MODULE_ALIAS("platform:twl6030_usb");
485MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
486MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
487MODULE_LICENSE("GPL");