twl6030-usb.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * twl6030_usb - TWL6030 USB transceiver, talking to OMAP OTG driver.
 *
 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Author: Hema HK <hemahk@ti.com>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/usb/otg.h>
29
#include <linux/usb/musb-omap.h>
30 31 32 33
#include <linux/i2c/twl.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/slab.h>
34
#include <linux/delay.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

/* usb register definitions */
#define USB_VENDOR_ID_LSB		0x00
#define USB_VENDOR_ID_MSB		0x01
#define USB_PRODUCT_ID_LSB		0x02
#define USB_PRODUCT_ID_MSB		0x03
#define USB_VBUS_CTRL_SET		0x04
#define USB_VBUS_CTRL_CLR		0x05
#define USB_ID_CTRL_SET			0x06
#define USB_ID_CTRL_CLR			0x07
#define USB_VBUS_INT_SRC		0x08
#define USB_VBUS_INT_LATCH_SET		0x09
#define USB_VBUS_INT_LATCH_CLR		0x0A
#define USB_VBUS_INT_EN_LO_SET		0x0B
#define USB_VBUS_INT_EN_LO_CLR		0x0C
#define USB_VBUS_INT_EN_HI_SET		0x0D
#define USB_VBUS_INT_EN_HI_CLR		0x0E
#define USB_ID_INT_SRC			0x0F
#define USB_ID_INT_LATCH_SET		0x10
#define USB_ID_INT_LATCH_CLR		0x11

#define USB_ID_INT_EN_LO_SET		0x12
#define USB_ID_INT_EN_LO_CLR		0x13
#define USB_ID_INT_EN_HI_SET		0x14
#define USB_ID_INT_EN_HI_CLR		0x15
#define USB_OTG_ADP_CTRL		0x16
#define USB_OTG_ADP_HIGH		0x17
#define USB_OTG_ADP_LOW			0x18
#define USB_OTG_ADP_RISE		0x19
#define USB_OTG_REVISION		0x1A

/* to be moved to LDO */
#define TWL6030_MISC2			0xE5
#define TWL6030_CFG_LDO_PD2		0xF5
#define TWL6030_BACKUP_REG		0xFA

#define STS_HW_CONDITIONS		0x21

/* In module TWL6030_MODULE_PM_MASTER */
#define STS_HW_CONDITIONS		0x21
#define STS_USB_ID			BIT(2)

/* In module TWL6030_MODULE_PM_RECEIVER */
#define VUSB_CFG_TRANS			0x71
#define VUSB_CFG_STATE			0x72
#define VUSB_CFG_VOLTAGE		0x73

/* in module TWL6030_MODULE_MAIN_CHARGE */

#define CHARGERUSB_CTRL1		0x8

#define CONTROLLER_STAT1		0x03
#define	VBUS_DET			BIT(2)

struct twl6030_usb {
90
	struct usb_phy		phy;
91 92 93 94 95 96 97
	struct device		*dev;

	/* for vbus reporting with irqs disabled */
	spinlock_t		lock;

	struct regulator		*usb3v3;

98 99 100
	/* used to set vbus, in atomic path */
	struct work_struct	set_vbus_work;

101 102
	int			irq1;
	int			irq2;
103
	enum omap_musb_vbus_id_status linkstat;
104 105
	u8			asleep;
	bool			irq_enabled;
106
	bool			vbus_enable;
107
	unsigned long		features;
108 109
};

110
#define phy_to_twl(x)		container_of((x), struct twl6030_usb, phy)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

/*-------------------------------------------------------------------------*/

static inline int twl6030_writeb(struct twl6030_usb *twl, u8 module,
						u8 data, u8 address)
{
	int ret = 0;

	ret = twl_i2c_write_u8(module, data, address);
	if (ret < 0)
		dev_err(twl->dev,
			"Write[0x%x] Error %d\n", address, ret);
	return ret;
}

static inline u8 twl6030_readb(struct twl6030_usb *twl, u8 module, u8 address)
{
	u8 data, ret = 0;

	ret = twl_i2c_read_u8(module, &data, address);
	if (ret >= 0)
		ret = data;
	else
		dev_err(twl->dev,
			"readb[0x%x,0x%x] Error %d\n",
					module, address, ret);
	return ret;
}

140
static int twl6030_phy_init(struct usb_phy *x)
141 142 143 144 145
{
	struct twl6030_usb *twl;
	struct device *dev;
	struct twl4030_usb_data *pdata;

146
	twl = phy_to_twl(x);
147 148 149
	dev  = twl->dev;
	pdata = dev->platform_data;

150
	if (twl->linkstat == OMAP_MUSB_ID_GROUND)
151 152 153 154 155 156 157
		pdata->phy_power(twl->dev, 1, 1);
	else
		pdata->phy_power(twl->dev, 0, 1);

	return 0;
}

158
static void twl6030_phy_shutdown(struct usb_phy *x)
159 160 161 162 163
{
	struct twl6030_usb *twl;
	struct device *dev;
	struct twl4030_usb_data *pdata;

164
	twl = phy_to_twl(x);
165 166 167 168 169
	dev  = twl->dev;
	pdata = dev->platform_data;
	pdata->phy_power(twl->dev, 0, 0);
}

170
static int twl6030_phy_suspend(struct usb_phy *x, int suspend)
171
{
172
	struct twl6030_usb *twl = phy_to_twl(x);
173 174 175 176 177 178 179 180
	struct device *dev = twl->dev;
	struct twl4030_usb_data *pdata = dev->platform_data;

	pdata->phy_suspend(dev, suspend);

	return 0;
}

181
static int twl6030_start_srp(struct usb_otg *otg)
182
{
183
	struct twl6030_usb *twl = phy_to_twl(otg->phy);
184 185 186 187 188 189 190 191 192 193

	twl6030_writeb(twl, TWL_MODULE_USB, 0x24, USB_VBUS_CTRL_SET);
	twl6030_writeb(twl, TWL_MODULE_USB, 0x84, USB_VBUS_CTRL_SET);

	mdelay(100);
	twl6030_writeb(twl, TWL_MODULE_USB, 0xa0, USB_VBUS_CTRL_CLR);

	return 0;
}

194 195
static int twl6030_usb_ldo_init(struct twl6030_usb *twl)
{
196 197 198 199 200 201
	char *regulator_name;

	if (twl->features & TWL6025_SUBCLASS)
		regulator_name = "ldousb";
	else
		regulator_name = "vusb";
202 203 204 205 206 207 208 209 210 211

	/* Set to OTG_REV 1.3 and turn on the ID_WAKEUP_COMP */
	twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_BACKUP_REG);

	/* Program CFG_LDO_PD2 register and set VUSB bit */
	twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x1, TWL6030_CFG_LDO_PD2);

	/* Program MISC2 register and set bit VUSB_IN_VBAT */
	twl6030_writeb(twl, TWL6030_MODULE_ID0 , 0x10, TWL6030_MISC2);

212
	twl->usb3v3 = regulator_get(twl->dev, regulator_name);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	if (IS_ERR(twl->usb3v3))
		return -ENODEV;

	/* Program the USB_VBUS_CTRL_SET and set VBUS_ACT_COMP bit */
	twl6030_writeb(twl, TWL_MODULE_USB, 0x4, USB_VBUS_CTRL_SET);

	/*
	 * Program the USB_ID_CTRL_SET register to enable GND drive
	 * and the ID comparators
	 */
	twl6030_writeb(twl, TWL_MODULE_USB, 0x14, USB_ID_CTRL_SET);

	return 0;
}

static ssize_t twl6030_usb_vbus_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct twl6030_usb *twl = dev_get_drvdata(dev);
	unsigned long flags;
	int ret = -EINVAL;

	spin_lock_irqsave(&twl->lock, flags);

	switch (twl->linkstat) {
238
	case OMAP_MUSB_VBUS_VALID:
239 240
	       ret = snprintf(buf, PAGE_SIZE, "vbus\n");
	       break;
241
	case OMAP_MUSB_ID_GROUND:
242 243
	       ret = snprintf(buf, PAGE_SIZE, "id\n");
	       break;
244
	case OMAP_MUSB_VBUS_OFF:
245 246 247 248 249 250 251 252 253 254 255 256 257 258
	       ret = snprintf(buf, PAGE_SIZE, "none\n");
	       break;
	default:
	       ret = snprintf(buf, PAGE_SIZE, "UNKNOWN\n");
	}
	spin_unlock_irqrestore(&twl->lock, flags);

	return ret;
}
static DEVICE_ATTR(vbus, 0444, twl6030_usb_vbus_show, NULL);

static irqreturn_t twl6030_usb_irq(int irq, void *_twl)
{
	struct twl6030_usb *twl = _twl;
259
	struct usb_otg *otg = twl->phy.otg;
260
	enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN;
261 262 263 264 265 266 267 268
	u8 vbus_state, hw_state;

	hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);

	vbus_state = twl6030_readb(twl, TWL_MODULE_MAIN_CHARGE,
						CONTROLLER_STAT1);
	if (!(hw_state & STS_USB_ID)) {
		if (vbus_state & VBUS_DET) {
269 270
			regulator_enable(twl->usb3v3);
			twl->asleep = 1;
271
			status = OMAP_MUSB_VBUS_VALID;
272 273
			otg->default_a = false;
			twl->phy.state = OTG_STATE_B_IDLE;
274
			twl->linkstat = status;
275
			omap_musb_mailbox(status);
276
		} else {
277 278 279 280 281 282 283 284
			if (twl->linkstat != OMAP_MUSB_UNKNOWN) {
				status = OMAP_MUSB_VBUS_OFF;
				twl->linkstat = status;
				omap_musb_mailbox(status);
				if (twl->asleep) {
					regulator_disable(twl->usb3v3);
					twl->asleep = 0;
				}
285
			}
286 287 288 289 290 291 292 293 294 295
		}
	}
	sysfs_notify(&twl->dev->kobj, NULL, "vbus");

	return IRQ_HANDLED;
}

static irqreturn_t twl6030_usbotg_irq(int irq, void *_twl)
{
	struct twl6030_usb *twl = _twl;
296
	struct usb_otg *otg = twl->phy.otg;
297
	enum omap_musb_vbus_id_status status = OMAP_MUSB_UNKNOWN;
298 299 300 301 302 303
	u8 hw_state;

	hw_state = twl6030_readb(twl, TWL6030_MODULE_ID0, STS_HW_CONDITIONS);

	if (hw_state & STS_USB_ID) {

304 305
		regulator_enable(twl->usb3v3);
		twl->asleep = 1;
306 307 308
		twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR, 0x1);
		twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
								0x10);
309
		status = OMAP_MUSB_ID_GROUND;
310 311
		otg->default_a = true;
		twl->phy.state = OTG_STATE_A_IDLE;
312
		twl->linkstat = status;
313
		omap_musb_mailbox(status);
314 315 316 317 318 319 320 321 322 323 324
	} else  {
		twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_CLR,
								0x10);
		twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET,
								0x1);
	}
	twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_LATCH_CLR, status);

	return IRQ_HANDLED;
}

325
static int twl6030_set_peripheral(struct usb_otg *otg,
326 327
		struct usb_gadget *gadget)
{
328
	if (!otg)
329 330
		return -ENODEV;

331
	otg->gadget = gadget;
332
	if (!gadget)
333
		otg->phy->state = OTG_STATE_UNDEFINED;
334 335 336 337

	return 0;
}

338
static int twl6030_enable_irq(struct usb_phy *x)
339
{
340
	struct twl6030_usb *twl = phy_to_twl(x);
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

	twl6030_writeb(twl, TWL_MODULE_USB, USB_ID_INT_EN_HI_SET, 0x1);
	twl6030_interrupt_unmask(0x05, REG_INT_MSK_LINE_C);
	twl6030_interrupt_unmask(0x05, REG_INT_MSK_STS_C);

	twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
				REG_INT_MSK_LINE_C);
	twl6030_interrupt_unmask(TWL6030_CHARGER_CTRL_INT_MASK,
				REG_INT_MSK_STS_C);
	twl6030_usb_irq(twl->irq2, twl);
	twl6030_usbotg_irq(twl->irq1, twl);

	return 0;
}

356
static void otg_set_vbus_work(struct work_struct *data)
357
{
358 359
	struct twl6030_usb *twl = container_of(data, struct twl6030_usb,
								set_vbus_work);
360 361 362 363 364

	/*
	 * Start driving VBUS. Set OPA_MODE bit in CHARGERUSB_CTRL1
	 * register. This enables boost mode.
	 */
365 366

	if (twl->vbus_enable)
367
		twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x40,
368 369
							CHARGERUSB_CTRL1);
	else
370
		twl6030_writeb(twl, TWL_MODULE_MAIN_CHARGE , 0x00,
371 372 373
							CHARGERUSB_CTRL1);
}

374
static int twl6030_set_vbus(struct usb_otg *otg, bool enabled)
375
{
376
	struct twl6030_usb *twl = phy_to_twl(otg->phy);
377 378 379 380

	twl->vbus_enable = enabled;
	schedule_work(&twl->set_vbus_work);

381 382 383
	return 0;
}

384
static int twl6030_set_host(struct usb_otg *otg, struct usb_bus *host)
385
{
386
	if (!otg)
387 388
		return -ENODEV;

389
	otg->host = host;
390
	if (!host)
391
		otg->phy->state = OTG_STATE_UNDEFINED;
392 393 394 395 396 397 398 399
	return 0;
}

static int __devinit twl6030_usb_probe(struct platform_device *pdev)
{
	struct twl6030_usb	*twl;
	int			status, err;
	struct twl4030_usb_data *pdata;
400
	struct usb_otg		*otg;
401 402 403 404 405 406 407
	struct device *dev = &pdev->dev;
	pdata = dev->platform_data;

	twl = kzalloc(sizeof *twl, GFP_KERNEL);
	if (!twl)
		return -ENOMEM;

408 409 410 411 412 413
	otg = kzalloc(sizeof *otg, GFP_KERNEL);
	if (!otg) {
		kfree(twl);
		return -ENOMEM;
	}

414 415 416
	twl->dev		= &pdev->dev;
	twl->irq1		= platform_get_irq(pdev, 0);
	twl->irq2		= platform_get_irq(pdev, 1);
417
	twl->features		= pdata->features;
418
	twl->linkstat		= OMAP_MUSB_UNKNOWN;
419 420 421 422 423 424 425 426 427 428 429 430 431

	twl->phy.dev		= twl->dev;
	twl->phy.label		= "twl6030";
	twl->phy.otg		= otg;
	twl->phy.init		= twl6030_phy_init;
	twl->phy.shutdown	= twl6030_phy_shutdown;
	twl->phy.set_suspend	= twl6030_phy_suspend;

	otg->phy		= &twl->phy;
	otg->set_host		= twl6030_set_host;
	otg->set_peripheral	= twl6030_set_peripheral;
	otg->set_vbus		= twl6030_set_vbus;
	otg->start_srp		= twl6030_start_srp;
432 433 434 435 436 437 438

	/* init spinlock for workqueue */
	spin_lock_init(&twl->lock);

	err = twl6030_usb_ldo_init(twl);
	if (err) {
		dev_err(&pdev->dev, "ldo init failed\n");
439
		kfree(otg);
440 441 442
		kfree(twl);
		return err;
	}
443
	usb_add_phy(&twl->phy, USB_PHY_TYPE_USB2);
444 445 446 447 448

	platform_set_drvdata(pdev, twl);
	if (device_create_file(&pdev->dev, &dev_attr_vbus))
		dev_warn(&pdev->dev, "could not create sysfs file\n");

449 450
	INIT_WORK(&twl->set_vbus_work, otg_set_vbus_work);

451 452
	twl->irq_enabled = true;
	status = request_threaded_irq(twl->irq1, NULL, twl6030_usbotg_irq,
453
			IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
454 455 456 457 458
			"twl6030_usb", twl);
	if (status < 0) {
		dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
			twl->irq1, status);
		device_remove_file(twl->dev, &dev_attr_vbus);
459
		kfree(otg);
460 461 462 463 464
		kfree(twl);
		return status;
	}

	status = request_threaded_irq(twl->irq2, NULL, twl6030_usb_irq,
465
			IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT,
466 467 468 469 470 471
			"twl6030_usb", twl);
	if (status < 0) {
		dev_err(&pdev->dev, "can't get IRQ %d, err %d\n",
			twl->irq2, status);
		free_irq(twl->irq1, twl);
		device_remove_file(twl->dev, &dev_attr_vbus);
472
		kfree(otg);
473 474 475 476
		kfree(twl);
		return status;
	}

477
	twl->asleep = 0;
478
	pdata->phy_init(dev);
479 480
	twl6030_phy_suspend(&twl->phy, 0);
	twl6030_enable_irq(&twl->phy);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	dev_info(&pdev->dev, "Initialized TWL6030 USB module\n");

	return 0;
}

static int __exit twl6030_usb_remove(struct platform_device *pdev)
{
	struct twl6030_usb *twl = platform_get_drvdata(pdev);

	struct twl4030_usb_data *pdata;
	struct device *dev = &pdev->dev;
	pdata = dev->platform_data;

	twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
		REG_INT_MSK_LINE_C);
	twl6030_interrupt_mask(TWL6030_USBOTG_INT_MASK,
			REG_INT_MSK_STS_C);
	free_irq(twl->irq1, twl);
	free_irq(twl->irq2, twl);
	regulator_put(twl->usb3v3);
	pdata->phy_exit(twl->dev);
	device_remove_file(twl->dev, &dev_attr_vbus);
503
	cancel_work_sync(&twl->set_vbus_work);
504
	kfree(twl->phy.otg);
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	kfree(twl);

	return 0;
}

static struct platform_driver twl6030_usb_driver = {
	.probe		= twl6030_usb_probe,
	.remove		= __exit_p(twl6030_usb_remove),
	.driver		= {
		.name	= "twl6030_usb",
		.owner	= THIS_MODULE,
	},
};

static int __init twl6030_usb_init(void)
{
	return platform_driver_register(&twl6030_usb_driver);
}
subsys_initcall(twl6030_usb_init);

static void __exit twl6030_usb_exit(void)
{
	platform_driver_unregister(&twl6030_usb_driver);
}
module_exit(twl6030_usb_exit);

MODULE_ALIAS("platform:twl6030_usb");
MODULE_AUTHOR("Hema HK <hemahk@ti.com>");
MODULE_DESCRIPTION("TWL6030 USB transceiver driver");
MODULE_LICENSE("GPL");