Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * USB transceiver driver for AB8500 family chips
   4 *
   5 * Copyright (C) 2010-2013 ST-Ericsson AB
   6 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
   7 * Avinash Kumar <avinash.kumar@stericsson.com>
   8 * Thirupathi Chippakurthy <thirupathi.chippakurthy@stericsson.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   9 */
  10
  11#include <linux/module.h>
  12#include <linux/platform_device.h>
  13#include <linux/usb/otg.h>
  14#include <linux/slab.h>
  15#include <linux/notifier.h>
  16#include <linux/interrupt.h>
  17#include <linux/delay.h>
  18#include <linux/clk.h>
  19#include <linux/err.h>
  20#include <linux/mfd/abx500.h>
  21#include <linux/mfd/abx500/ab8500.h>
  22#include <linux/usb/musb-ux500.h>
  23#include <linux/regulator/consumer.h>
  24#include <linux/pinctrl/consumer.h>
  25
  26/* Bank AB8500_SYS_CTRL2_BLOCK */
  27#define AB8500_MAIN_WD_CTRL_REG 0x01
  28
  29/* Bank AB8500_USB */
  30#define AB8500_USB_LINE_STAT_REG 0x80
  31#define AB8505_USB_LINE_STAT_REG 0x94
 
 
 
  32#define AB8500_USB_PHY_CTRL_REG 0x8A
 
  33
  34/* Bank AB8500_DEVELOPMENT */
  35#define AB8500_BANK12_ACCESS 0x00
  36
  37/* Bank AB8500_DEBUG */
 
  38#define AB8500_USB_PHY_TUNE1 0x05
  39#define AB8500_USB_PHY_TUNE2 0x06
  40#define AB8500_USB_PHY_TUNE3 0x07
  41
  42/* Bank AB8500_INTERRUPT */
  43#define AB8500_IT_SOURCE2_REG 0x01
  44
  45#define AB8500_BIT_OTG_STAT_ID (1 << 0)
  46#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
  47#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
  48#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
  49#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
  50#define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
 
 
 
 
  51
  52#define AB8500_WD_KICK_DELAY_US 100 /* usec */
  53#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
  54#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
  55
  56/* Usb line status register */
  57enum ab8500_usb_link_status {
  58	USB_LINK_NOT_CONFIGURED_8500 = 0,
  59	USB_LINK_STD_HOST_NC_8500,
  60	USB_LINK_STD_HOST_C_NS_8500,
  61	USB_LINK_STD_HOST_C_S_8500,
  62	USB_LINK_HOST_CHG_NM_8500,
  63	USB_LINK_HOST_CHG_HS_8500,
  64	USB_LINK_HOST_CHG_HS_CHIRP_8500,
  65	USB_LINK_DEDICATED_CHG_8500,
  66	USB_LINK_ACA_RID_A_8500,
  67	USB_LINK_ACA_RID_B_8500,
  68	USB_LINK_ACA_RID_C_NM_8500,
  69	USB_LINK_ACA_RID_C_HS_8500,
  70	USB_LINK_ACA_RID_C_HS_CHIRP_8500,
  71	USB_LINK_HM_IDGND_8500,
  72	USB_LINK_RESERVED_8500,
  73	USB_LINK_NOT_VALID_LINK_8500,
  74};
  75
  76enum ab8505_usb_link_status {
  77	USB_LINK_NOT_CONFIGURED_8505 = 0,
  78	USB_LINK_STD_HOST_NC_8505,
  79	USB_LINK_STD_HOST_C_NS_8505,
  80	USB_LINK_STD_HOST_C_S_8505,
  81	USB_LINK_CDP_8505,
  82	USB_LINK_RESERVED0_8505,
  83	USB_LINK_RESERVED1_8505,
  84	USB_LINK_DEDICATED_CHG_8505,
  85	USB_LINK_ACA_RID_A_8505,
  86	USB_LINK_ACA_RID_B_8505,
  87	USB_LINK_ACA_RID_C_NM_8505,
  88	USB_LINK_RESERVED2_8505,
  89	USB_LINK_RESERVED3_8505,
  90	USB_LINK_HM_IDGND_8505,
  91	USB_LINK_CHARGERPORT_NOT_OK_8505,
  92	USB_LINK_CHARGER_DM_HIGH_8505,
  93	USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
  94	USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
  95	USB_LINK_STD_UPSTREAM_8505,
  96	USB_LINK_CHARGER_SE1_8505,
  97	USB_LINK_CARKIT_CHGR_1_8505,
  98	USB_LINK_CARKIT_CHGR_2_8505,
  99	USB_LINK_ACA_DOCK_CHGR_8505,
 100	USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
 101	USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
 102	USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
 103	USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
 104	USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
 105};
 106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 107enum ab8500_usb_mode {
 108	USB_IDLE = 0,
 109	USB_PERIPHERAL,
 110	USB_HOST,
 111	USB_DEDICATED_CHG,
 112	USB_UART
 113};
 114
 115/* Register USB_LINK_STATUS interrupt */
 116#define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ	(1 << 0)
 117/* Register ID_WAKEUP_F interrupt */
 118#define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ	(1 << 1)
 119/* Register VBUS_DET_F interrupt */
 120#define AB8500_USB_FLAG_USE_VBUS_DET_IRQ	(1 << 2)
 121/* Driver is using the ab-iddet driver*/
 122#define AB8500_USB_FLAG_USE_AB_IDDET		(1 << 3)
 123/* Enable setting regulators voltage */
 124#define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE	(1 << 4)
 
 
 
 
 125
 126struct ab8500_usb {
 127	struct usb_phy phy;
 128	struct device *dev;
 129	struct ab8500 *ab8500;
 130	unsigned vbus_draw;
 131	struct work_struct phy_dis_work;
 
 132	enum ab8500_usb_mode mode;
 133	struct clk *sysclk;
 134	struct regulator *v_ape;
 135	struct regulator *v_musb;
 136	struct regulator *v_ulpi;
 137	int saved_v_ulpi;
 138	int previous_link_status_state;
 139	struct pinctrl *pinctrl;
 140	struct pinctrl_state *pins_sleep;
 141	bool enabled_charging_detection;
 142	unsigned int flags;
 143};
 144
 145static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
 146{
 147	return container_of(x, struct ab8500_usb, phy);
 148}
 149
 150static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
 151{
 152	abx500_set_register_interruptible(ab->dev,
 153		AB8500_SYS_CTRL2_BLOCK,
 154		AB8500_MAIN_WD_CTRL_REG,
 155		AB8500_BIT_WD_CTRL_ENABLE);
 156
 157	udelay(AB8500_WD_KICK_DELAY_US);
 158
 159	abx500_set_register_interruptible(ab->dev,
 160		AB8500_SYS_CTRL2_BLOCK,
 161		AB8500_MAIN_WD_CTRL_REG,
 162		(AB8500_BIT_WD_CTRL_ENABLE
 163		| AB8500_BIT_WD_CTRL_KICK));
 164
 165	udelay(AB8500_WD_V11_DISABLE_DELAY_US);
 166
 167	abx500_set_register_interruptible(ab->dev,
 168		AB8500_SYS_CTRL2_BLOCK,
 169		AB8500_MAIN_WD_CTRL_REG,
 170		0);
 171}
 172
 173static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
 174{
 175	int ret, volt;
 176
 177	ret = regulator_enable(ab->v_ape);
 178	if (ret)
 179		dev_err(ab->dev, "Failed to enable v-ape\n");
 180
 181	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
 182		ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
 183		if (ab->saved_v_ulpi < 0)
 184			dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
 185
 186		ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
 187		if (ret < 0)
 188			dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
 189					ret);
 190
 191		ret = regulator_set_load(ab->v_ulpi, 28000);
 192		if (ret < 0)
 193			dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
 194					ret);
 195	}
 196
 197	ret = regulator_enable(ab->v_ulpi);
 198	if (ret)
 199		dev_err(ab->dev, "Failed to enable vddulpivio18\n");
 200
 201	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
 202		volt = regulator_get_voltage(ab->v_ulpi);
 203		if ((volt != 1300000) && (volt != 1350000))
 204			dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
 205					volt);
 206	}
 207
 208	ret = regulator_enable(ab->v_musb);
 209	if (ret)
 210		dev_err(ab->dev, "Failed to enable musb_1v8\n");
 211}
 212
 213static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
 214{
 215	int ret;
 216
 217	regulator_disable(ab->v_musb);
 218
 219	regulator_disable(ab->v_ulpi);
 220
 221	/* USB is not the only consumer of Vintcore, restore old settings */
 222	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
 223		if (ab->saved_v_ulpi > 0) {
 224			ret = regulator_set_voltage(ab->v_ulpi,
 225					ab->saved_v_ulpi, ab->saved_v_ulpi);
 226			if (ret < 0)
 227				dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
 228						ab->saved_v_ulpi, ret);
 229		}
 230
 231		ret = regulator_set_load(ab->v_ulpi, 0);
 232		if (ret < 0)
 233			dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
 234					ret);
 235	}
 236
 237	regulator_disable(ab->v_ape);
 238}
 239
 240static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
 241{
 242	/* Workaround for v2.0 bug # 31952 */
 243	if (is_ab8500_2p0(ab->ab8500)) {
 244		abx500_mask_and_set_register_interruptible(ab->dev,
 245				AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 246				bit, bit);
 247		udelay(AB8500_V20_31952_DISABLE_DELAY_US);
 248	}
 249}
 250
 251static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
 252{
 253	u8 bit;
 254	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
 255		AB8500_BIT_PHY_CTRL_DEVICE_EN;
 256
 257	/* mux and configure USB pins to DEFAULT state */
 258	ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
 259	if (IS_ERR(ab->pinctrl))
 260		dev_err(ab->dev, "could not get/set default pinstate\n");
 261
 262	if (clk_prepare_enable(ab->sysclk))
 263		dev_err(ab->dev, "can't prepare/enable clock\n");
 264
 265	ab8500_usb_regulator_enable(ab);
 266
 267	abx500_mask_and_set_register_interruptible(ab->dev,
 268			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 269			bit, bit);
 
 
 
 
 
 
 
 
 
 270}
 271
 272static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
 273{
 274	u8 bit;
 275	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
 276		AB8500_BIT_PHY_CTRL_DEVICE_EN;
 277
 278	ab8500_usb_wd_linkstatus(ab, bit);
 279
 280	abx500_mask_and_set_register_interruptible(ab->dev,
 281			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 282			bit, 0);
 283
 284	/* Needed to disable the phy.*/
 285	ab8500_usb_wd_workaround(ab);
 286
 287	clk_disable_unprepare(ab->sysclk);
 288
 289	ab8500_usb_regulator_disable(ab);
 290
 291	if (!IS_ERR(ab->pinctrl)) {
 292		/* configure USB pins to SLEEP state */
 293		ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
 294				PINCTRL_STATE_SLEEP);
 295
 296		if (IS_ERR(ab->pins_sleep))
 297			dev_dbg(ab->dev, "could not get sleep pinstate\n");
 298		else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
 299			dev_err(ab->dev, "could not set pins to sleep state\n");
 300
 301		/*
 302		 * as USB pins are shared with iddet, release them to allow
 303		 * iddet to request them
 304		 */
 305		pinctrl_put(ab->pinctrl);
 306	}
 307}
 308
 309#define ab8500_usb_host_phy_en(ab)	ab8500_usb_phy_enable(ab, true)
 310#define ab8500_usb_host_phy_dis(ab)	ab8500_usb_phy_disable(ab, true)
 311#define ab8500_usb_peri_phy_en(ab)	ab8500_usb_phy_enable(ab, false)
 312#define ab8500_usb_peri_phy_dis(ab)	ab8500_usb_phy_disable(ab, false)
 313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 314static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
 315		enum ab8505_usb_link_status lsts)
 316{
 317	enum ux500_musb_vbus_id_status event = 0;
 318
 319	dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
 320
 321	/*
 322	 * Spurious link_status interrupts are seen at the time of
 323	 * disconnection of a device in RIDA state
 324	 */
 325	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
 326			(lsts == USB_LINK_STD_HOST_NC_8505))
 327		return 0;
 328
 329	ab->previous_link_status_state = lsts;
 330
 331	switch (lsts) {
 332	case USB_LINK_ACA_RID_B_8505:
 333		event = UX500_MUSB_RIDB;
 334		fallthrough;
 335	case USB_LINK_NOT_CONFIGURED_8505:
 336	case USB_LINK_RESERVED0_8505:
 337	case USB_LINK_RESERVED1_8505:
 338	case USB_LINK_RESERVED2_8505:
 339	case USB_LINK_RESERVED3_8505:
 340		ab->mode = USB_IDLE;
 341		ab->phy.otg->default_a = false;
 342		ab->vbus_draw = 0;
 343		if (event != UX500_MUSB_RIDB)
 344			event = UX500_MUSB_NONE;
 345		/*
 346		 * Fallback to default B_IDLE as nothing
 347		 * is connected
 348		 */
 349		ab->phy.otg->state = OTG_STATE_B_IDLE;
 350		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 351		break;
 352
 353	case USB_LINK_ACA_RID_C_NM_8505:
 354		event = UX500_MUSB_RIDC;
 355		fallthrough;
 356	case USB_LINK_STD_HOST_NC_8505:
 357	case USB_LINK_STD_HOST_C_NS_8505:
 358	case USB_LINK_STD_HOST_C_S_8505:
 359	case USB_LINK_CDP_8505:
 360		if (ab->mode == USB_IDLE) {
 361			ab->mode = USB_PERIPHERAL;
 362			ab8500_usb_peri_phy_en(ab);
 363			atomic_notifier_call_chain(&ab->phy.notifier,
 364					UX500_MUSB_PREPARE, &ab->vbus_draw);
 365			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
 366		}
 367		if (event != UX500_MUSB_RIDC)
 368			event = UX500_MUSB_VBUS;
 369		break;
 370
 371	case USB_LINK_ACA_RID_A_8505:
 372	case USB_LINK_ACA_DOCK_CHGR_8505:
 373		event = UX500_MUSB_RIDA;
 374		fallthrough;
 375	case USB_LINK_HM_IDGND_8505:
 376		if (ab->mode == USB_IDLE) {
 377			ab->mode = USB_HOST;
 378			ab8500_usb_host_phy_en(ab);
 379			atomic_notifier_call_chain(&ab->phy.notifier,
 380					UX500_MUSB_PREPARE, &ab->vbus_draw);
 381		}
 382		ab->phy.otg->default_a = true;
 383		if (event != UX500_MUSB_RIDA)
 384			event = UX500_MUSB_ID;
 385		atomic_notifier_call_chain(&ab->phy.notifier,
 386				event, &ab->vbus_draw);
 387		break;
 388
 389	case USB_LINK_DEDICATED_CHG_8505:
 390		ab->mode = USB_DEDICATED_CHG;
 391		event = UX500_MUSB_CHARGER;
 392		atomic_notifier_call_chain(&ab->phy.notifier,
 393				event, &ab->vbus_draw);
 394		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
 395		break;
 396
 397	/*
 398	 * FIXME: For now we rely on the boot firmware to set up the necessary
 399	 * PHY/pin configuration for UART mode.
 400	 *
 401	 * AB8505 does not seem to report any status change for UART cables,
 402	 * possibly because it cannot detect them autonomously.
 403	 * We may need to measure the ID resistance manually to reliably
 404	 * detect UART cables after bootup.
 405	 */
 406	case USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505:
 407	case USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505:
 408		if (ab->mode == USB_IDLE) {
 409			ab->mode = USB_UART;
 410			ab8500_usb_peri_phy_en(ab);
 411		}
 412
 413		break;
 414
 415	default:
 416		break;
 417	}
 418
 419	return 0;
 420}
 421
 422static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
 423		enum ab8500_usb_link_status lsts)
 424{
 425	enum ux500_musb_vbus_id_status event = 0;
 426
 427	dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
 428
 429	/*
 430	 * Spurious link_status interrupts are seen in case of a
 431	 * disconnection of a device in IDGND and RIDA stage
 432	 */
 433	if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
 434			(lsts == USB_LINK_STD_HOST_C_NS_8500 ||
 435			 lsts == USB_LINK_STD_HOST_NC_8500))
 436		return 0;
 437
 438	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
 439			lsts == USB_LINK_STD_HOST_NC_8500)
 440		return 0;
 441
 442	ab->previous_link_status_state = lsts;
 443
 444	switch (lsts) {
 445	case USB_LINK_ACA_RID_B_8500:
 446		event = UX500_MUSB_RIDB;
 447		fallthrough;
 448	case USB_LINK_NOT_CONFIGURED_8500:
 449	case USB_LINK_NOT_VALID_LINK_8500:
 450		ab->mode = USB_IDLE;
 451		ab->phy.otg->default_a = false;
 452		ab->vbus_draw = 0;
 453		if (event != UX500_MUSB_RIDB)
 454			event = UX500_MUSB_NONE;
 455		/* Fallback to default B_IDLE as nothing is connected */
 456		ab->phy.otg->state = OTG_STATE_B_IDLE;
 457		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 458		break;
 459
 460	case USB_LINK_ACA_RID_C_NM_8500:
 461	case USB_LINK_ACA_RID_C_HS_8500:
 462	case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
 463		event = UX500_MUSB_RIDC;
 464		fallthrough;
 465	case USB_LINK_STD_HOST_NC_8500:
 466	case USB_LINK_STD_HOST_C_NS_8500:
 467	case USB_LINK_STD_HOST_C_S_8500:
 468	case USB_LINK_HOST_CHG_NM_8500:
 469	case USB_LINK_HOST_CHG_HS_8500:
 470	case USB_LINK_HOST_CHG_HS_CHIRP_8500:
 471		if (ab->mode == USB_IDLE) {
 472			ab->mode = USB_PERIPHERAL;
 473			ab8500_usb_peri_phy_en(ab);
 474			atomic_notifier_call_chain(&ab->phy.notifier,
 475					UX500_MUSB_PREPARE, &ab->vbus_draw);
 476			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
 477		}
 478		if (event != UX500_MUSB_RIDC)
 479			event = UX500_MUSB_VBUS;
 480		break;
 481
 482	case USB_LINK_ACA_RID_A_8500:
 483		event = UX500_MUSB_RIDA;
 484		fallthrough;
 485	case USB_LINK_HM_IDGND_8500:
 486		if (ab->mode == USB_IDLE) {
 487			ab->mode = USB_HOST;
 488			ab8500_usb_host_phy_en(ab);
 489			atomic_notifier_call_chain(&ab->phy.notifier,
 490					UX500_MUSB_PREPARE, &ab->vbus_draw);
 491		}
 492		ab->phy.otg->default_a = true;
 493		if (event != UX500_MUSB_RIDA)
 494			event = UX500_MUSB_ID;
 495		atomic_notifier_call_chain(&ab->phy.notifier,
 496				event, &ab->vbus_draw);
 497		break;
 498
 499	case USB_LINK_DEDICATED_CHG_8500:
 500		ab->mode = USB_DEDICATED_CHG;
 501		event = UX500_MUSB_CHARGER;
 502		atomic_notifier_call_chain(&ab->phy.notifier,
 503				event, &ab->vbus_draw);
 504		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
 505		break;
 506
 507	case USB_LINK_RESERVED_8500:
 508		break;
 509	}
 510
 511	return 0;
 512}
 513
 514/*
 515 * Connection Sequence:
 516 *   1. Link Status Interrupt
 517 *   2. Enable AB clock
 518 *   3. Enable AB regulators
 519 *   4. Enable USB phy
 520 *   5. Reset the musb controller
 521 *   6. Switch the ULPI GPIO pins to function mode
 522 *   7. Enable the musb Peripheral5 clock
 523 *   8. Restore MUSB context
 524 */
 525static int abx500_usb_link_status_update(struct ab8500_usb *ab)
 526{
 527	u8 reg;
 528	int ret = 0;
 529
 530	if (is_ab8500(ab->ab8500)) {
 531		enum ab8500_usb_link_status lsts;
 532
 533		ret = abx500_get_register_interruptible(ab->dev,
 534				AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
 535		if (ret < 0)
 536			return ret;
 537		lsts = (reg >> 3) & 0x0F;
 538		ret = ab8500_usb_link_status_update(ab, lsts);
 539	} else if (is_ab8505(ab->ab8500)) {
 540		enum ab8505_usb_link_status lsts;
 541
 542		ret = abx500_get_register_interruptible(ab->dev,
 543				AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
 544		if (ret < 0)
 545			return ret;
 546		lsts = (reg >> 3) & 0x1F;
 547		ret = ab8505_usb_link_status_update(ab, lsts);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 548	}
 549
 550	return ret;
 551}
 552
 553/*
 554 * Disconnection Sequence:
 555 *   1. Disconnect Interrupt
 556 *   2. Disable regulators
 557 *   3. Disable AB clock
 558 *   4. Disable the Phy
 559 *   5. Link Status Interrupt
 560 *   6. Disable Musb Clock
 561 */
 562static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
 563{
 564	struct ab8500_usb *ab = (struct ab8500_usb *) data;
 565	enum usb_phy_events event = USB_EVENT_NONE;
 566
 567	/* Link status will not be updated till phy is disabled. */
 568	if (ab->mode == USB_HOST) {
 569		ab->phy.otg->default_a = false;
 570		ab->vbus_draw = 0;
 571		atomic_notifier_call_chain(&ab->phy.notifier,
 572				event, &ab->vbus_draw);
 573		ab8500_usb_host_phy_dis(ab);
 574		ab->mode = USB_IDLE;
 575	}
 576
 577	if (ab->mode == USB_PERIPHERAL) {
 578		atomic_notifier_call_chain(&ab->phy.notifier,
 579				event, &ab->vbus_draw);
 580		ab8500_usb_peri_phy_dis(ab);
 581		atomic_notifier_call_chain(&ab->phy.notifier,
 582				UX500_MUSB_CLEAN, &ab->vbus_draw);
 583		ab->mode = USB_IDLE;
 584		ab->phy.otg->default_a = false;
 585		ab->vbus_draw = 0;
 586	}
 587
 588	if (ab->mode == USB_UART) {
 589		ab8500_usb_peri_phy_dis(ab);
 590		ab->mode = USB_IDLE;
 591	}
 592
 593	if (is_ab8500_2p0(ab->ab8500)) {
 594		if (ab->mode == USB_DEDICATED_CHG) {
 595			ab8500_usb_wd_linkstatus(ab,
 596					AB8500_BIT_PHY_CTRL_DEVICE_EN);
 597			abx500_mask_and_set_register_interruptible(ab->dev,
 598					AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 599					AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
 600		}
 601	}
 602
 603	return IRQ_HANDLED;
 604}
 605
 606static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
 607{
 608	struct ab8500_usb *ab = (struct ab8500_usb *)data;
 609
 610	abx500_usb_link_status_update(ab);
 611
 612	return IRQ_HANDLED;
 613}
 614
 615static void ab8500_usb_phy_disable_work(struct work_struct *work)
 616{
 617	struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
 618						phy_dis_work);
 619
 620	if (!ab->phy.otg->host)
 621		ab8500_usb_host_phy_dis(ab);
 622
 623	if (!ab->phy.otg->gadget)
 624		ab8500_usb_peri_phy_dis(ab);
 625}
 626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 627static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
 628{
 629	/* TODO */
 630	return 0;
 631}
 632
 633static int ab8500_usb_set_peripheral(struct usb_otg *otg,
 634					struct usb_gadget *gadget)
 635{
 636	struct ab8500_usb *ab;
 637
 638	if (!otg)
 639		return -ENODEV;
 640
 641	ab = phy_to_ab(otg->usb_phy);
 642
 643	ab->phy.otg->gadget = gadget;
 644
 645	/* Some drivers call this function in atomic context.
 646	 * Do not update ab8500 registers directly till this
 647	 * is fixed.
 648	 */
 649
 650	if ((ab->mode != USB_IDLE) && !gadget) {
 651		ab->mode = USB_IDLE;
 652		schedule_work(&ab->phy_dis_work);
 653	}
 654
 655	return 0;
 656}
 657
 658static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
 659{
 660	struct ab8500_usb *ab;
 661
 662	if (!otg)
 663		return -ENODEV;
 664
 665	ab = phy_to_ab(otg->usb_phy);
 666
 667	ab->phy.otg->host = host;
 668
 669	/* Some drivers call this function in atomic context.
 670	 * Do not update ab8500 registers directly till this
 671	 * is fixed.
 672	 */
 673
 674	if ((ab->mode != USB_IDLE) && !host) {
 675		ab->mode = USB_IDLE;
 676		schedule_work(&ab->phy_dis_work);
 677	}
 678
 679	return 0;
 680}
 681
 682static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
 683{
 684	abx500_mask_and_set_register_interruptible(ab->dev,
 685			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 686			AB8500_BIT_PHY_CTRL_DEVICE_EN,
 687			AB8500_BIT_PHY_CTRL_DEVICE_EN);
 688
 689	udelay(100);
 690
 691	abx500_mask_and_set_register_interruptible(ab->dev,
 692			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 693			AB8500_BIT_PHY_CTRL_DEVICE_EN,
 694			0);
 695
 696	abx500_mask_and_set_register_interruptible(ab->dev,
 697			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 698			AB8500_BIT_PHY_CTRL_HOST_EN,
 699			AB8500_BIT_PHY_CTRL_HOST_EN);
 700
 701	udelay(100);
 702
 703	abx500_mask_and_set_register_interruptible(ab->dev,
 704			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 705			AB8500_BIT_PHY_CTRL_HOST_EN,
 706			0);
 707}
 708
 709static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
 710{
 711	int err;
 712
 713	ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
 714	if (IS_ERR(ab->v_ape)) {
 715		dev_err(ab->dev, "Could not get v-ape supply\n");
 716		err = PTR_ERR(ab->v_ape);
 717		return err;
 718	}
 719
 720	ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
 721	if (IS_ERR(ab->v_ulpi)) {
 722		dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
 723		err = PTR_ERR(ab->v_ulpi);
 724		return err;
 725	}
 726
 727	ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
 728	if (IS_ERR(ab->v_musb)) {
 729		dev_err(ab->dev, "Could not get musb_1v8 supply\n");
 730		err = PTR_ERR(ab->v_musb);
 731		return err;
 732	}
 733
 734	return 0;
 735}
 736
 737static int ab8500_usb_irq_setup(struct platform_device *pdev,
 738		struct ab8500_usb *ab)
 739{
 740	int err;
 741	int irq;
 742
 743	if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
 744		irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
 745		if (irq < 0)
 
 746			return irq;
 
 747		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 748				ab8500_usb_link_status_irq,
 749				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
 750				"usb-link-status", ab);
 751		if (err < 0) {
 752			dev_err(ab->dev, "request_irq failed for link status irq\n");
 753			return err;
 754		}
 755	}
 756
 757	if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
 758		irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
 759		if (irq < 0)
 
 760			return irq;
 
 761		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 762				ab8500_usb_disconnect_irq,
 763				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
 764				"usb-id-fall", ab);
 765		if (err < 0) {
 766			dev_err(ab->dev, "request_irq failed for ID fall irq\n");
 767			return err;
 768		}
 769	}
 770
 771	if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
 772		irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
 773		if (irq < 0)
 
 774			return irq;
 
 775		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
 776				ab8500_usb_disconnect_irq,
 777				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
 778				"usb-vbus-fall", ab);
 779		if (err < 0) {
 780			dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
 781			return err;
 782		}
 783	}
 784
 785	return 0;
 786}
 787
 788static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
 789{
 790	int err;
 791
 792	/* Enable the PBT/Bank 0x12 access */
 793	err = abx500_set_register_interruptible(ab->dev,
 794			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
 795	if (err < 0)
 796		dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
 797				err);
 798
 799	err = abx500_set_register_interruptible(ab->dev,
 800			AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
 801	if (err < 0)
 802		dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
 803				err);
 804
 805	err = abx500_set_register_interruptible(ab->dev,
 806			AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
 807	if (err < 0)
 808		dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
 809				err);
 810
 811	err = abx500_set_register_interruptible(ab->dev,
 812			AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
 813	if (err < 0)
 814		dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
 815				err);
 816
 817	/* Switch to normal mode/disable Bank 0x12 access */
 818	err = abx500_set_register_interruptible(ab->dev,
 819			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
 820	if (err < 0)
 821		dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
 822				err);
 823}
 824
 825static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
 826{
 827	int err;
 828
 829	/* Enable the PBT/Bank 0x12 access */
 830	err = abx500_mask_and_set_register_interruptible(ab->dev,
 831			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
 832			0x01, 0x01);
 833	if (err < 0)
 834		dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
 835				err);
 836
 837	err = abx500_mask_and_set_register_interruptible(ab->dev,
 838			AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
 839			0xC8, 0xC8);
 840	if (err < 0)
 841		dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
 842				err);
 843
 844	err = abx500_mask_and_set_register_interruptible(ab->dev,
 845			AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
 846			0x60, 0x60);
 847	if (err < 0)
 848		dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
 849				err);
 850
 851	err = abx500_mask_and_set_register_interruptible(ab->dev,
 852			AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
 853			0xFC, 0x80);
 854
 855	if (err < 0)
 856		dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
 857				err);
 858
 859	/* Switch to normal mode/disable Bank 0x12 access */
 860	err = abx500_mask_and_set_register_interruptible(ab->dev,
 861			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
 862			0x00, 0x00);
 863	if (err < 0)
 864		dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
 865				err);
 866}
 867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 868static int ab8500_usb_probe(struct platform_device *pdev)
 869{
 870	struct ab8500_usb	*ab;
 871	struct ab8500		*ab8500;
 872	struct usb_otg		*otg;
 873	int err;
 874	int rev;
 875
 876	ab8500 = dev_get_drvdata(pdev->dev.parent);
 877	rev = abx500_get_chip_id(&pdev->dev);
 878
 879	if (is_ab8500_1p1_or_earlier(ab8500)) {
 880		dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
 881		return -ENODEV;
 882	}
 883
 884	ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
 885	if (!ab)
 886		return -ENOMEM;
 887
 888	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
 889	if (!otg)
 890		return -ENOMEM;
 891
 892	ab->dev			= &pdev->dev;
 893	ab->ab8500		= ab8500;
 894	ab->phy.dev		= ab->dev;
 895	ab->phy.otg		= otg;
 896	ab->phy.label		= "ab8500";
 897	ab->phy.set_suspend	= ab8500_usb_set_suspend;
 
 898	ab->phy.otg->state	= OTG_STATE_UNDEFINED;
 899
 900	otg->usb_phy		= &ab->phy;
 901	otg->set_host		= ab8500_usb_set_host;
 902	otg->set_peripheral	= ab8500_usb_set_peripheral;
 903
 904	if (is_ab8500(ab->ab8500)) {
 905		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
 906			AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
 907			AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
 908			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
 909	} else if (is_ab8505(ab->ab8500)) {
 910		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
 911			AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
 912			AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
 913			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
 
 
 
 
 
 
 
 
 
 
 
 914	}
 915
 916	/* Disable regulator voltage setting for AB8500 <= v2.0 */
 917	if (is_ab8500_2p0_or_earlier(ab->ab8500))
 918		ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
 919
 920	platform_set_drvdata(pdev, ab);
 921
 922	/* all: Disable phy when called from set_host and set_peripheral */
 923	INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
 924
 
 
 925	err = ab8500_usb_regulator_get(ab);
 926	if (err)
 927		return err;
 928
 929	ab->sysclk = devm_clk_get(ab->dev, "sysclk");
 930	if (IS_ERR(ab->sysclk)) {
 931		dev_err(ab->dev, "Could not get sysclk.\n");
 932		return PTR_ERR(ab->sysclk);
 933	}
 934
 935	err = ab8500_usb_irq_setup(pdev, ab);
 936	if (err < 0)
 937		return err;
 938
 939	err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
 940	if (err) {
 941		dev_err(&pdev->dev, "Can't register transceiver\n");
 942		return err;
 943	}
 944
 945	if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
 946		/* Phy tuning values for AB8500 > v2.0 */
 947		ab8500_usb_set_ab8500_tuning_values(ab);
 948	else if (is_ab8505(ab->ab8500))
 949		/* Phy tuning values for AB8505 */
 950		ab8500_usb_set_ab8505_tuning_values(ab);
 
 
 
 
 
 
 951
 952	/* Needed to enable ID detection. */
 953	ab8500_usb_wd_workaround(ab);
 954
 955	/*
 956	 * This is required for usb-link-status to work properly when a
 957	 * cable is connected at boot time.
 958	 */
 959	ab8500_usb_restart_phy(ab);
 960
 
 
 
 
 
 961	abx500_usb_link_status_update(ab);
 962
 963	dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
 964
 965	return 0;
 966}
 967
 968static void ab8500_usb_remove(struct platform_device *pdev)
 969{
 970	struct ab8500_usb *ab = platform_get_drvdata(pdev);
 971
 972	cancel_work_sync(&ab->phy_dis_work);
 
 973
 974	usb_remove_phy(&ab->phy);
 975
 976	if (ab->mode == USB_HOST)
 977		ab8500_usb_host_phy_dis(ab);
 978	else if (ab->mode == USB_PERIPHERAL)
 979		ab8500_usb_peri_phy_dis(ab);
 
 
 980}
 981
 982static const struct platform_device_id ab8500_usb_devtype[] = {
 983	{ .name = "ab8500-usb", },
 
 
 984	{ /* sentinel */ }
 985};
 986MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
 987
 988static struct platform_driver ab8500_usb_driver = {
 989	.probe		= ab8500_usb_probe,
 990	.remove_new	= ab8500_usb_remove,
 991	.id_table	= ab8500_usb_devtype,
 992	.driver		= {
 993		.name	= "abx5x0-usb",
 994	},
 995};
 996
 997static int __init ab8500_usb_init(void)
 998{
 999	return platform_driver_register(&ab8500_usb_driver);
1000}
1001subsys_initcall(ab8500_usb_init);
1002
1003static void __exit ab8500_usb_exit(void)
1004{
1005	platform_driver_unregister(&ab8500_usb_driver);
1006}
1007module_exit(ab8500_usb_exit);
1008
1009MODULE_AUTHOR("ST-Ericsson AB");
1010MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
1011MODULE_LICENSE("GPL");
v4.10.11
 
   1/*
   2 * USB transceiver driver for AB8500 family chips
   3 *
   4 * Copyright (C) 2010-2013 ST-Ericsson AB
   5 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
   6 * Avinash Kumar <avinash.kumar@stericsson.com>
   7 * Thirupathi Chippakurthy <thirupathi.chippakurthy@stericsson.com>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, write to the Free Software
  21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22 *
  23 */
  24
  25#include <linux/module.h>
  26#include <linux/platform_device.h>
  27#include <linux/usb/otg.h>
  28#include <linux/slab.h>
  29#include <linux/notifier.h>
  30#include <linux/interrupt.h>
  31#include <linux/delay.h>
  32#include <linux/clk.h>
  33#include <linux/err.h>
  34#include <linux/mfd/abx500.h>
  35#include <linux/mfd/abx500/ab8500.h>
  36#include <linux/usb/musb-ux500.h>
  37#include <linux/regulator/consumer.h>
  38#include <linux/pinctrl/consumer.h>
  39
  40/* Bank AB8500_SYS_CTRL2_BLOCK */
  41#define AB8500_MAIN_WD_CTRL_REG 0x01
  42
  43/* Bank AB8500_USB */
  44#define AB8500_USB_LINE_STAT_REG 0x80
  45#define AB8505_USB_LINE_STAT_REG 0x94
  46#define AB8540_USB_LINK_STAT_REG 0x94
  47#define AB9540_USB_LINK_STAT_REG 0x94
  48#define AB8540_USB_OTG_CTL_REG 0x87
  49#define AB8500_USB_PHY_CTRL_REG 0x8A
  50#define AB8540_VBUS_CTRL_REG 0x82
  51
  52/* Bank AB8500_DEVELOPMENT */
  53#define AB8500_BANK12_ACCESS 0x00
  54
  55/* Bank AB8500_DEBUG */
  56#define AB8540_DEBUG 0x32
  57#define AB8500_USB_PHY_TUNE1 0x05
  58#define AB8500_USB_PHY_TUNE2 0x06
  59#define AB8500_USB_PHY_TUNE3 0x07
  60
  61/* Bank AB8500_INTERRUPT */
  62#define AB8500_IT_SOURCE2_REG 0x01
  63
  64#define AB8500_BIT_OTG_STAT_ID (1 << 0)
  65#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
  66#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
  67#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
  68#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
  69#define AB8500_BIT_SOURCE2_VBUSDET (1 << 7)
  70#define AB8540_BIT_OTG_CTL_VBUS_VALID_ENA (1 << 0)
  71#define AB8540_BIT_OTG_CTL_ID_HOST_ENA (1 << 1)
  72#define AB8540_BIT_OTG_CTL_ID_DEV_ENA (1 << 5)
  73#define AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA (1 << 0)
  74
  75#define AB8500_WD_KICK_DELAY_US 100 /* usec */
  76#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
  77#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
  78
  79/* Usb line status register */
  80enum ab8500_usb_link_status {
  81	USB_LINK_NOT_CONFIGURED_8500 = 0,
  82	USB_LINK_STD_HOST_NC_8500,
  83	USB_LINK_STD_HOST_C_NS_8500,
  84	USB_LINK_STD_HOST_C_S_8500,
  85	USB_LINK_HOST_CHG_NM_8500,
  86	USB_LINK_HOST_CHG_HS_8500,
  87	USB_LINK_HOST_CHG_HS_CHIRP_8500,
  88	USB_LINK_DEDICATED_CHG_8500,
  89	USB_LINK_ACA_RID_A_8500,
  90	USB_LINK_ACA_RID_B_8500,
  91	USB_LINK_ACA_RID_C_NM_8500,
  92	USB_LINK_ACA_RID_C_HS_8500,
  93	USB_LINK_ACA_RID_C_HS_CHIRP_8500,
  94	USB_LINK_HM_IDGND_8500,
  95	USB_LINK_RESERVED_8500,
  96	USB_LINK_NOT_VALID_LINK_8500,
  97};
  98
  99enum ab8505_usb_link_status {
 100	USB_LINK_NOT_CONFIGURED_8505 = 0,
 101	USB_LINK_STD_HOST_NC_8505,
 102	USB_LINK_STD_HOST_C_NS_8505,
 103	USB_LINK_STD_HOST_C_S_8505,
 104	USB_LINK_CDP_8505,
 105	USB_LINK_RESERVED0_8505,
 106	USB_LINK_RESERVED1_8505,
 107	USB_LINK_DEDICATED_CHG_8505,
 108	USB_LINK_ACA_RID_A_8505,
 109	USB_LINK_ACA_RID_B_8505,
 110	USB_LINK_ACA_RID_C_NM_8505,
 111	USB_LINK_RESERVED2_8505,
 112	USB_LINK_RESERVED3_8505,
 113	USB_LINK_HM_IDGND_8505,
 114	USB_LINK_CHARGERPORT_NOT_OK_8505,
 115	USB_LINK_CHARGER_DM_HIGH_8505,
 116	USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
 117	USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
 118	USB_LINK_STD_UPSTREAM_8505,
 119	USB_LINK_CHARGER_SE1_8505,
 120	USB_LINK_CARKIT_CHGR_1_8505,
 121	USB_LINK_CARKIT_CHGR_2_8505,
 122	USB_LINK_ACA_DOCK_CHGR_8505,
 123	USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
 124	USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
 125	USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
 126	USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
 127	USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
 128};
 129
 130enum ab8540_usb_link_status {
 131	USB_LINK_NOT_CONFIGURED_8540 = 0,
 132	USB_LINK_STD_HOST_NC_8540,
 133	USB_LINK_STD_HOST_C_NS_8540,
 134	USB_LINK_STD_HOST_C_S_8540,
 135	USB_LINK_CDP_8540,
 136	USB_LINK_RESERVED0_8540,
 137	USB_LINK_RESERVED1_8540,
 138	USB_LINK_DEDICATED_CHG_8540,
 139	USB_LINK_ACA_RID_A_8540,
 140	USB_LINK_ACA_RID_B_8540,
 141	USB_LINK_ACA_RID_C_NM_8540,
 142	USB_LINK_RESERVED2_8540,
 143	USB_LINK_RESERVED3_8540,
 144	USB_LINK_HM_IDGND_8540,
 145	USB_LINK_CHARGERPORT_NOT_OK_8540,
 146	USB_LINK_CHARGER_DM_HIGH_8540,
 147	USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540,
 148	USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540,
 149	USB_LINK_STD_UPSTREAM_8540,
 150	USB_LINK_CHARGER_SE1_8540,
 151	USB_LINK_CARKIT_CHGR_1_8540,
 152	USB_LINK_CARKIT_CHGR_2_8540,
 153	USB_LINK_ACA_DOCK_CHGR_8540,
 154	USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8540,
 155	USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8540,
 156	USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8540,
 157	USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8540,
 158	USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8540
 159};
 160
 161enum ab9540_usb_link_status {
 162	USB_LINK_NOT_CONFIGURED_9540 = 0,
 163	USB_LINK_STD_HOST_NC_9540,
 164	USB_LINK_STD_HOST_C_NS_9540,
 165	USB_LINK_STD_HOST_C_S_9540,
 166	USB_LINK_CDP_9540,
 167	USB_LINK_RESERVED0_9540,
 168	USB_LINK_RESERVED1_9540,
 169	USB_LINK_DEDICATED_CHG_9540,
 170	USB_LINK_ACA_RID_A_9540,
 171	USB_LINK_ACA_RID_B_9540,
 172	USB_LINK_ACA_RID_C_NM_9540,
 173	USB_LINK_RESERVED2_9540,
 174	USB_LINK_RESERVED3_9540,
 175	USB_LINK_HM_IDGND_9540,
 176	USB_LINK_CHARGERPORT_NOT_OK_9540,
 177	USB_LINK_CHARGER_DM_HIGH_9540,
 178	USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540,
 179	USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_9540,
 180	USB_LINK_STD_UPSTREAM_9540,
 181	USB_LINK_CHARGER_SE1_9540,
 182	USB_LINK_CARKIT_CHGR_1_9540,
 183	USB_LINK_CARKIT_CHGR_2_9540,
 184	USB_LINK_ACA_DOCK_CHGR_9540,
 185	USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_9540,
 186	USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_9540,
 187	USB_LINK_SAMSUNG_UART_CBL_PHY_EN_9540,
 188	USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_9540,
 189	USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_9540
 190};
 191
 192enum ab8500_usb_mode {
 193	USB_IDLE = 0,
 194	USB_PERIPHERAL,
 195	USB_HOST,
 196	USB_DEDICATED_CHG
 
 197};
 198
 199/* Register USB_LINK_STATUS interrupt */
 200#define AB8500_USB_FLAG_USE_LINK_STATUS_IRQ	(1 << 0)
 201/* Register ID_WAKEUP_F interrupt */
 202#define AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ	(1 << 1)
 203/* Register VBUS_DET_F interrupt */
 204#define AB8500_USB_FLAG_USE_VBUS_DET_IRQ	(1 << 2)
 205/* Driver is using the ab-iddet driver*/
 206#define AB8500_USB_FLAG_USE_AB_IDDET		(1 << 3)
 207/* Enable setting regulators voltage */
 208#define AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE	(1 << 4)
 209/* Enable the check_vbus_status workaround */
 210#define AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS	(1 << 5)
 211/* Enable the vbus host workaround */
 212#define AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK	(1 << 6)
 213
 214struct ab8500_usb {
 215	struct usb_phy phy;
 216	struct device *dev;
 217	struct ab8500 *ab8500;
 218	unsigned vbus_draw;
 219	struct work_struct phy_dis_work;
 220	struct work_struct vbus_event_work;
 221	enum ab8500_usb_mode mode;
 222	struct clk *sysclk;
 223	struct regulator *v_ape;
 224	struct regulator *v_musb;
 225	struct regulator *v_ulpi;
 226	int saved_v_ulpi;
 227	int previous_link_status_state;
 228	struct pinctrl *pinctrl;
 229	struct pinctrl_state *pins_sleep;
 230	bool enabled_charging_detection;
 231	unsigned int flags;
 232};
 233
 234static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
 235{
 236	return container_of(x, struct ab8500_usb, phy);
 237}
 238
 239static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
 240{
 241	abx500_set_register_interruptible(ab->dev,
 242		AB8500_SYS_CTRL2_BLOCK,
 243		AB8500_MAIN_WD_CTRL_REG,
 244		AB8500_BIT_WD_CTRL_ENABLE);
 245
 246	udelay(AB8500_WD_KICK_DELAY_US);
 247
 248	abx500_set_register_interruptible(ab->dev,
 249		AB8500_SYS_CTRL2_BLOCK,
 250		AB8500_MAIN_WD_CTRL_REG,
 251		(AB8500_BIT_WD_CTRL_ENABLE
 252		| AB8500_BIT_WD_CTRL_KICK));
 253
 254	udelay(AB8500_WD_V11_DISABLE_DELAY_US);
 255
 256	abx500_set_register_interruptible(ab->dev,
 257		AB8500_SYS_CTRL2_BLOCK,
 258		AB8500_MAIN_WD_CTRL_REG,
 259		0);
 260}
 261
 262static void ab8500_usb_regulator_enable(struct ab8500_usb *ab)
 263{
 264	int ret, volt;
 265
 266	ret = regulator_enable(ab->v_ape);
 267	if (ret)
 268		dev_err(ab->dev, "Failed to enable v-ape\n");
 269
 270	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
 271		ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi);
 272		if (ab->saved_v_ulpi < 0)
 273			dev_err(ab->dev, "Failed to get v_ulpi voltage\n");
 274
 275		ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000);
 276		if (ret < 0)
 277			dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n",
 278					ret);
 279
 280		ret = regulator_set_load(ab->v_ulpi, 28000);
 281		if (ret < 0)
 282			dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
 283					ret);
 284	}
 285
 286	ret = regulator_enable(ab->v_ulpi);
 287	if (ret)
 288		dev_err(ab->dev, "Failed to enable vddulpivio18\n");
 289
 290	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
 291		volt = regulator_get_voltage(ab->v_ulpi);
 292		if ((volt != 1300000) && (volt != 1350000))
 293			dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n",
 294					volt);
 295	}
 296
 297	ret = regulator_enable(ab->v_musb);
 298	if (ret)
 299		dev_err(ab->dev, "Failed to enable musb_1v8\n");
 300}
 301
 302static void ab8500_usb_regulator_disable(struct ab8500_usb *ab)
 303{
 304	int ret;
 305
 306	regulator_disable(ab->v_musb);
 307
 308	regulator_disable(ab->v_ulpi);
 309
 310	/* USB is not the only consumer of Vintcore, restore old settings */
 311	if (ab->flags & AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE) {
 312		if (ab->saved_v_ulpi > 0) {
 313			ret = regulator_set_voltage(ab->v_ulpi,
 314					ab->saved_v_ulpi, ab->saved_v_ulpi);
 315			if (ret < 0)
 316				dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n",
 317						ab->saved_v_ulpi, ret);
 318		}
 319
 320		ret = regulator_set_load(ab->v_ulpi, 0);
 321		if (ret < 0)
 322			dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n",
 323					ret);
 324	}
 325
 326	regulator_disable(ab->v_ape);
 327}
 328
 329static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit)
 330{
 331	/* Workaround for v2.0 bug # 31952 */
 332	if (is_ab8500_2p0(ab->ab8500)) {
 333		abx500_mask_and_set_register_interruptible(ab->dev,
 334				AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 335				bit, bit);
 336		udelay(AB8500_V20_31952_DISABLE_DELAY_US);
 337	}
 338}
 339
 340static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
 341{
 342	u8 bit;
 343	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
 344		AB8500_BIT_PHY_CTRL_DEVICE_EN;
 345
 346	/* mux and configure USB pins to DEFAULT state */
 347	ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT);
 348	if (IS_ERR(ab->pinctrl))
 349		dev_err(ab->dev, "could not get/set default pinstate\n");
 350
 351	if (clk_prepare_enable(ab->sysclk))
 352		dev_err(ab->dev, "can't prepare/enable clock\n");
 353
 354	ab8500_usb_regulator_enable(ab);
 355
 356	abx500_mask_and_set_register_interruptible(ab->dev,
 357			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 358			bit, bit);
 359
 360	if (ab->flags & AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK) {
 361		if (sel_host)
 362			abx500_set_register_interruptible(ab->dev,
 363					AB8500_USB, AB8540_USB_OTG_CTL_REG,
 364					AB8540_BIT_OTG_CTL_VBUS_VALID_ENA |
 365					AB8540_BIT_OTG_CTL_ID_HOST_ENA |
 366					AB8540_BIT_OTG_CTL_ID_DEV_ENA);
 367	}
 368}
 369
 370static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
 371{
 372	u8 bit;
 373	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
 374		AB8500_BIT_PHY_CTRL_DEVICE_EN;
 375
 376	ab8500_usb_wd_linkstatus(ab, bit);
 377
 378	abx500_mask_and_set_register_interruptible(ab->dev,
 379			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 380			bit, 0);
 381
 382	/* Needed to disable the phy.*/
 383	ab8500_usb_wd_workaround(ab);
 384
 385	clk_disable_unprepare(ab->sysclk);
 386
 387	ab8500_usb_regulator_disable(ab);
 388
 389	if (!IS_ERR(ab->pinctrl)) {
 390		/* configure USB pins to SLEEP state */
 391		ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl,
 392				PINCTRL_STATE_SLEEP);
 393
 394		if (IS_ERR(ab->pins_sleep))
 395			dev_dbg(ab->dev, "could not get sleep pinstate\n");
 396		else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep))
 397			dev_err(ab->dev, "could not set pins to sleep state\n");
 398
 399		/*
 400		 * as USB pins are shared with iddet, release them to allow
 401		 * iddet to request them
 402		 */
 403		pinctrl_put(ab->pinctrl);
 404	}
 405}
 406
 407#define ab8500_usb_host_phy_en(ab)	ab8500_usb_phy_enable(ab, true)
 408#define ab8500_usb_host_phy_dis(ab)	ab8500_usb_phy_disable(ab, true)
 409#define ab8500_usb_peri_phy_en(ab)	ab8500_usb_phy_enable(ab, false)
 410#define ab8500_usb_peri_phy_dis(ab)	ab8500_usb_phy_disable(ab, false)
 411
 412static int ab9540_usb_link_status_update(struct ab8500_usb *ab,
 413		enum ab9540_usb_link_status lsts)
 414{
 415	enum ux500_musb_vbus_id_status event = 0;
 416
 417	dev_dbg(ab->dev, "ab9540_usb_link_status_update %d\n", lsts);
 418
 419	if (ab->previous_link_status_state == USB_LINK_HM_IDGND_9540 &&
 420			(lsts == USB_LINK_STD_HOST_C_NS_9540 ||
 421			 lsts == USB_LINK_STD_HOST_NC_9540))
 422		return 0;
 423
 424	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_9540 &&
 425			(lsts == USB_LINK_STD_HOST_NC_9540))
 426		return 0;
 427
 428	ab->previous_link_status_state = lsts;
 429
 430	switch (lsts) {
 431	case USB_LINK_ACA_RID_B_9540:
 432		event = UX500_MUSB_RIDB;
 433	case USB_LINK_NOT_CONFIGURED_9540:
 434	case USB_LINK_RESERVED0_9540:
 435	case USB_LINK_RESERVED1_9540:
 436	case USB_LINK_RESERVED2_9540:
 437	case USB_LINK_RESERVED3_9540:
 438		if (ab->mode == USB_PERIPHERAL)
 439			atomic_notifier_call_chain(&ab->phy.notifier,
 440					UX500_MUSB_CLEAN, &ab->vbus_draw);
 441		ab->mode = USB_IDLE;
 442		ab->phy.otg->default_a = false;
 443		ab->vbus_draw = 0;
 444		if (event != UX500_MUSB_RIDB)
 445			event = UX500_MUSB_NONE;
 446		/* Fallback to default B_IDLE as nothing is connected. */
 447		ab->phy.otg->state = OTG_STATE_B_IDLE;
 448		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 449		break;
 450
 451	case USB_LINK_ACA_RID_C_NM_9540:
 452		event = UX500_MUSB_RIDC;
 453	case USB_LINK_STD_HOST_NC_9540:
 454	case USB_LINK_STD_HOST_C_NS_9540:
 455	case USB_LINK_STD_HOST_C_S_9540:
 456	case USB_LINK_CDP_9540:
 457		if (ab->mode == USB_HOST) {
 458			ab->mode = USB_PERIPHERAL;
 459			ab8500_usb_host_phy_dis(ab);
 460			ab8500_usb_peri_phy_en(ab);
 461			atomic_notifier_call_chain(&ab->phy.notifier,
 462					UX500_MUSB_PREPARE, &ab->vbus_draw);
 463			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
 464		}
 465		if (ab->mode == USB_IDLE) {
 466			ab->mode = USB_PERIPHERAL;
 467			ab8500_usb_peri_phy_en(ab);
 468			atomic_notifier_call_chain(&ab->phy.notifier,
 469					UX500_MUSB_PREPARE, &ab->vbus_draw);
 470			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
 471		}
 472		if (event != UX500_MUSB_RIDC)
 473			event = UX500_MUSB_VBUS;
 474		break;
 475
 476	case USB_LINK_ACA_RID_A_9540:
 477		event = UX500_MUSB_RIDA;
 478	case USB_LINK_HM_IDGND_9540:
 479	case USB_LINK_STD_UPSTREAM_9540:
 480		if (ab->mode == USB_PERIPHERAL) {
 481			ab->mode = USB_HOST;
 482			ab8500_usb_peri_phy_dis(ab);
 483			ab8500_usb_host_phy_en(ab);
 484			atomic_notifier_call_chain(&ab->phy.notifier,
 485					UX500_MUSB_PREPARE, &ab->vbus_draw);
 486		}
 487		if (ab->mode == USB_IDLE) {
 488			ab->mode = USB_HOST;
 489			ab8500_usb_host_phy_en(ab);
 490			atomic_notifier_call_chain(&ab->phy.notifier,
 491					UX500_MUSB_PREPARE, &ab->vbus_draw);
 492		}
 493		ab->phy.otg->default_a = true;
 494		if (event != UX500_MUSB_RIDA)
 495			event = UX500_MUSB_ID;
 496
 497		atomic_notifier_call_chain(&ab->phy.notifier,
 498				event, &ab->vbus_draw);
 499		break;
 500
 501	case USB_LINK_DEDICATED_CHG_9540:
 502		ab->mode = USB_DEDICATED_CHG;
 503		event = UX500_MUSB_CHARGER;
 504		atomic_notifier_call_chain(&ab->phy.notifier,
 505				event, &ab->vbus_draw);
 506		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
 507		break;
 508
 509	case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540:
 510	case USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_9540:
 511		if (!(is_ab9540_2p0_or_earlier(ab->ab8500))) {
 512			event = UX500_MUSB_NONE;
 513			if (ab->mode == USB_HOST) {
 514				ab->phy.otg->default_a = false;
 515				ab->vbus_draw = 0;
 516				atomic_notifier_call_chain(&ab->phy.notifier,
 517						event, &ab->vbus_draw);
 518				ab8500_usb_host_phy_dis(ab);
 519				ab->mode = USB_IDLE;
 520			}
 521			if (ab->mode == USB_PERIPHERAL) {
 522				atomic_notifier_call_chain(&ab->phy.notifier,
 523						event, &ab->vbus_draw);
 524				ab8500_usb_peri_phy_dis(ab);
 525				atomic_notifier_call_chain(&ab->phy.notifier,
 526						UX500_MUSB_CLEAN,
 527						&ab->vbus_draw);
 528				ab->mode = USB_IDLE;
 529				ab->phy.otg->default_a = false;
 530				ab->vbus_draw = 0;
 531				usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 532			}
 533		}
 534		break;
 535
 536	default:
 537		break;
 538	}
 539
 540	return 0;
 541}
 542
 543static int ab8540_usb_link_status_update(struct ab8500_usb *ab,
 544		enum ab8540_usb_link_status lsts)
 545{
 546	enum ux500_musb_vbus_id_status event = 0;
 547
 548	dev_dbg(ab->dev, "ab8540_usb_link_status_update %d\n", lsts);
 549
 550	if (ab->enabled_charging_detection) {
 551		/* Disable USB Charger detection */
 552		abx500_mask_and_set_register_interruptible(ab->dev,
 553				AB8500_USB, AB8540_VBUS_CTRL_REG,
 554				AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA, 0x00);
 555		ab->enabled_charging_detection = false;
 556	}
 557
 558	/*
 559	 * Spurious link_status interrupts are seen in case of a
 560	 * disconnection of a device in IDGND and RIDA stage
 561	 */
 562	if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8540 &&
 563			(lsts == USB_LINK_STD_HOST_C_NS_8540 ||
 564			 lsts == USB_LINK_STD_HOST_NC_8540))
 565		return 0;
 566
 567	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8540 &&
 568			(lsts == USB_LINK_STD_HOST_NC_8540))
 569		return 0;
 570
 571	ab->previous_link_status_state = lsts;
 572
 573	switch (lsts) {
 574	case USB_LINK_ACA_RID_B_8540:
 575		event = UX500_MUSB_RIDB;
 576	case USB_LINK_NOT_CONFIGURED_8540:
 577	case USB_LINK_RESERVED0_8540:
 578	case USB_LINK_RESERVED1_8540:
 579	case USB_LINK_RESERVED2_8540:
 580	case USB_LINK_RESERVED3_8540:
 581		ab->mode = USB_IDLE;
 582		ab->phy.otg->default_a = false;
 583		ab->vbus_draw = 0;
 584		if (event != UX500_MUSB_RIDB)
 585			event = UX500_MUSB_NONE;
 586		/*
 587		 * Fallback to default B_IDLE as nothing
 588		 * is connected
 589		 */
 590		ab->phy.otg->state = OTG_STATE_B_IDLE;
 591		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 592		break;
 593
 594	case USB_LINK_ACA_RID_C_NM_8540:
 595		event = UX500_MUSB_RIDC;
 596	case USB_LINK_STD_HOST_NC_8540:
 597	case USB_LINK_STD_HOST_C_NS_8540:
 598	case USB_LINK_STD_HOST_C_S_8540:
 599	case USB_LINK_CDP_8540:
 600		if (ab->mode == USB_IDLE) {
 601			ab->mode = USB_PERIPHERAL;
 602			ab8500_usb_peri_phy_en(ab);
 603			atomic_notifier_call_chain(&ab->phy.notifier,
 604					UX500_MUSB_PREPARE, &ab->vbus_draw);
 605			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
 606		}
 607		if (event != UX500_MUSB_RIDC)
 608			event = UX500_MUSB_VBUS;
 609		break;
 610
 611	case USB_LINK_ACA_RID_A_8540:
 612	case USB_LINK_ACA_DOCK_CHGR_8540:
 613		event = UX500_MUSB_RIDA;
 614	case USB_LINK_HM_IDGND_8540:
 615	case USB_LINK_STD_UPSTREAM_8540:
 616		if (ab->mode == USB_IDLE) {
 617			ab->mode = USB_HOST;
 618			ab8500_usb_host_phy_en(ab);
 619			atomic_notifier_call_chain(&ab->phy.notifier,
 620					UX500_MUSB_PREPARE, &ab->vbus_draw);
 621		}
 622		ab->phy.otg->default_a = true;
 623		if (event != UX500_MUSB_RIDA)
 624			event = UX500_MUSB_ID;
 625		atomic_notifier_call_chain(&ab->phy.notifier,
 626				event, &ab->vbus_draw);
 627		break;
 628
 629	case USB_LINK_DEDICATED_CHG_8540:
 630		ab->mode = USB_DEDICATED_CHG;
 631		event = UX500_MUSB_CHARGER;
 632		atomic_notifier_call_chain(&ab->phy.notifier,
 633				event, &ab->vbus_draw);
 634		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
 635		break;
 636
 637	case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540:
 638	case USB_LINK_STD_UPSTREAM_NO_IDGNG_VBUS_8540:
 639		event = UX500_MUSB_NONE;
 640		if (ab->mode == USB_HOST) {
 641			ab->phy.otg->default_a = false;
 642			ab->vbus_draw = 0;
 643			atomic_notifier_call_chain(&ab->phy.notifier,
 644					event, &ab->vbus_draw);
 645			ab8500_usb_host_phy_dis(ab);
 646			ab->mode = USB_IDLE;
 647		}
 648		if (ab->mode == USB_PERIPHERAL) {
 649			atomic_notifier_call_chain(&ab->phy.notifier,
 650					event, &ab->vbus_draw);
 651			ab8500_usb_peri_phy_dis(ab);
 652			atomic_notifier_call_chain(&ab->phy.notifier,
 653					UX500_MUSB_CLEAN, &ab->vbus_draw);
 654			ab->mode = USB_IDLE;
 655			ab->phy.otg->default_a = false;
 656			ab->vbus_draw = 0;
 657		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 658		}
 659		break;
 660
 661	default:
 662		event = UX500_MUSB_NONE;
 663		break;
 664	}
 665
 666	return 0;
 667}
 668
 669static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
 670		enum ab8505_usb_link_status lsts)
 671{
 672	enum ux500_musb_vbus_id_status event = 0;
 673
 674	dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);
 675
 676	/*
 677	 * Spurious link_status interrupts are seen at the time of
 678	 * disconnection of a device in RIDA state
 679	 */
 680	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 &&
 681			(lsts == USB_LINK_STD_HOST_NC_8505))
 682		return 0;
 683
 684	ab->previous_link_status_state = lsts;
 685
 686	switch (lsts) {
 687	case USB_LINK_ACA_RID_B_8505:
 688		event = UX500_MUSB_RIDB;
 
 689	case USB_LINK_NOT_CONFIGURED_8505:
 690	case USB_LINK_RESERVED0_8505:
 691	case USB_LINK_RESERVED1_8505:
 692	case USB_LINK_RESERVED2_8505:
 693	case USB_LINK_RESERVED3_8505:
 694		ab->mode = USB_IDLE;
 695		ab->phy.otg->default_a = false;
 696		ab->vbus_draw = 0;
 697		if (event != UX500_MUSB_RIDB)
 698			event = UX500_MUSB_NONE;
 699		/*
 700		 * Fallback to default B_IDLE as nothing
 701		 * is connected
 702		 */
 703		ab->phy.otg->state = OTG_STATE_B_IDLE;
 704		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 705		break;
 706
 707	case USB_LINK_ACA_RID_C_NM_8505:
 708		event = UX500_MUSB_RIDC;
 
 709	case USB_LINK_STD_HOST_NC_8505:
 710	case USB_LINK_STD_HOST_C_NS_8505:
 711	case USB_LINK_STD_HOST_C_S_8505:
 712	case USB_LINK_CDP_8505:
 713		if (ab->mode == USB_IDLE) {
 714			ab->mode = USB_PERIPHERAL;
 715			ab8500_usb_peri_phy_en(ab);
 716			atomic_notifier_call_chain(&ab->phy.notifier,
 717					UX500_MUSB_PREPARE, &ab->vbus_draw);
 718			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
 719		}
 720		if (event != UX500_MUSB_RIDC)
 721			event = UX500_MUSB_VBUS;
 722		break;
 723
 724	case USB_LINK_ACA_RID_A_8505:
 725	case USB_LINK_ACA_DOCK_CHGR_8505:
 726		event = UX500_MUSB_RIDA;
 
 727	case USB_LINK_HM_IDGND_8505:
 728		if (ab->mode == USB_IDLE) {
 729			ab->mode = USB_HOST;
 730			ab8500_usb_host_phy_en(ab);
 731			atomic_notifier_call_chain(&ab->phy.notifier,
 732					UX500_MUSB_PREPARE, &ab->vbus_draw);
 733		}
 734		ab->phy.otg->default_a = true;
 735		if (event != UX500_MUSB_RIDA)
 736			event = UX500_MUSB_ID;
 737		atomic_notifier_call_chain(&ab->phy.notifier,
 738				event, &ab->vbus_draw);
 739		break;
 740
 741	case USB_LINK_DEDICATED_CHG_8505:
 742		ab->mode = USB_DEDICATED_CHG;
 743		event = UX500_MUSB_CHARGER;
 744		atomic_notifier_call_chain(&ab->phy.notifier,
 745				event, &ab->vbus_draw);
 746		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
 747		break;
 748
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 749	default:
 750		break;
 751	}
 752
 753	return 0;
 754}
 755
 756static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
 757		enum ab8500_usb_link_status lsts)
 758{
 759	enum ux500_musb_vbus_id_status event = 0;
 760
 761	dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);
 762
 763	/*
 764	 * Spurious link_status interrupts are seen in case of a
 765	 * disconnection of a device in IDGND and RIDA stage
 766	 */
 767	if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 &&
 768			(lsts == USB_LINK_STD_HOST_C_NS_8500 ||
 769			 lsts == USB_LINK_STD_HOST_NC_8500))
 770		return 0;
 771
 772	if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 &&
 773			lsts == USB_LINK_STD_HOST_NC_8500)
 774		return 0;
 775
 776	ab->previous_link_status_state = lsts;
 777
 778	switch (lsts) {
 779	case USB_LINK_ACA_RID_B_8500:
 780		event = UX500_MUSB_RIDB;
 
 781	case USB_LINK_NOT_CONFIGURED_8500:
 782	case USB_LINK_NOT_VALID_LINK_8500:
 783		ab->mode = USB_IDLE;
 784		ab->phy.otg->default_a = false;
 785		ab->vbus_draw = 0;
 786		if (event != UX500_MUSB_RIDB)
 787			event = UX500_MUSB_NONE;
 788		/* Fallback to default B_IDLE as nothing is connected */
 789		ab->phy.otg->state = OTG_STATE_B_IDLE;
 790		usb_phy_set_event(&ab->phy, USB_EVENT_NONE);
 791		break;
 792
 793	case USB_LINK_ACA_RID_C_NM_8500:
 794	case USB_LINK_ACA_RID_C_HS_8500:
 795	case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
 796		event = UX500_MUSB_RIDC;
 
 797	case USB_LINK_STD_HOST_NC_8500:
 798	case USB_LINK_STD_HOST_C_NS_8500:
 799	case USB_LINK_STD_HOST_C_S_8500:
 800	case USB_LINK_HOST_CHG_NM_8500:
 801	case USB_LINK_HOST_CHG_HS_8500:
 802	case USB_LINK_HOST_CHG_HS_CHIRP_8500:
 803		if (ab->mode == USB_IDLE) {
 804			ab->mode = USB_PERIPHERAL;
 805			ab8500_usb_peri_phy_en(ab);
 806			atomic_notifier_call_chain(&ab->phy.notifier,
 807					UX500_MUSB_PREPARE, &ab->vbus_draw);
 808			usb_phy_set_event(&ab->phy, USB_EVENT_ENUMERATED);
 809		}
 810		if (event != UX500_MUSB_RIDC)
 811			event = UX500_MUSB_VBUS;
 812		break;
 813
 814	case USB_LINK_ACA_RID_A_8500:
 815		event = UX500_MUSB_RIDA;
 
 816	case USB_LINK_HM_IDGND_8500:
 817		if (ab->mode == USB_IDLE) {
 818			ab->mode = USB_HOST;
 819			ab8500_usb_host_phy_en(ab);
 820			atomic_notifier_call_chain(&ab->phy.notifier,
 821					UX500_MUSB_PREPARE, &ab->vbus_draw);
 822		}
 823		ab->phy.otg->default_a = true;
 824		if (event != UX500_MUSB_RIDA)
 825			event = UX500_MUSB_ID;
 826		atomic_notifier_call_chain(&ab->phy.notifier,
 827				event, &ab->vbus_draw);
 828		break;
 829
 830	case USB_LINK_DEDICATED_CHG_8500:
 831		ab->mode = USB_DEDICATED_CHG;
 832		event = UX500_MUSB_CHARGER;
 833		atomic_notifier_call_chain(&ab->phy.notifier,
 834				event, &ab->vbus_draw);
 835		usb_phy_set_event(&ab->phy, USB_EVENT_CHARGER);
 836		break;
 837
 838	case USB_LINK_RESERVED_8500:
 839		break;
 840	}
 841
 842	return 0;
 843}
 844
 845/*
 846 * Connection Sequence:
 847 *   1. Link Status Interrupt
 848 *   2. Enable AB clock
 849 *   3. Enable AB regulators
 850 *   4. Enable USB phy
 851 *   5. Reset the musb controller
 852 *   6. Switch the ULPI GPIO pins to fucntion mode
 853 *   7. Enable the musb Peripheral5 clock
 854 *   8. Restore MUSB context
 855 */
 856static int abx500_usb_link_status_update(struct ab8500_usb *ab)
 857{
 858	u8 reg;
 859	int ret = 0;
 860
 861	if (is_ab8500(ab->ab8500)) {
 862		enum ab8500_usb_link_status lsts;
 863
 864		abx500_get_register_interruptible(ab->dev,
 865				AB8500_USB, AB8500_USB_LINE_STAT_REG, &reg);
 
 
 866		lsts = (reg >> 3) & 0x0F;
 867		ret = ab8500_usb_link_status_update(ab, lsts);
 868	} else if (is_ab8505(ab->ab8500)) {
 869		enum ab8505_usb_link_status lsts;
 870
 871		abx500_get_register_interruptible(ab->dev,
 872				AB8500_USB, AB8505_USB_LINE_STAT_REG, &reg);
 
 
 873		lsts = (reg >> 3) & 0x1F;
 874		ret = ab8505_usb_link_status_update(ab, lsts);
 875	} else if (is_ab8540(ab->ab8500)) {
 876		enum ab8540_usb_link_status lsts;
 877
 878		abx500_get_register_interruptible(ab->dev,
 879				AB8500_USB, AB8540_USB_LINK_STAT_REG, &reg);
 880		lsts = (reg >> 3) & 0xFF;
 881		ret = ab8540_usb_link_status_update(ab, lsts);
 882	} else if (is_ab9540(ab->ab8500)) {
 883		enum ab9540_usb_link_status lsts;
 884
 885		abx500_get_register_interruptible(ab->dev,
 886				AB8500_USB, AB9540_USB_LINK_STAT_REG, &reg);
 887		lsts = (reg >> 3) & 0xFF;
 888		ret = ab9540_usb_link_status_update(ab, lsts);
 889	}
 890
 891	return ret;
 892}
 893
 894/*
 895 * Disconnection Sequence:
 896 *   1. Disconnect Interrupt
 897 *   2. Disable regulators
 898 *   3. Disable AB clock
 899 *   4. Disable the Phy
 900 *   5. Link Status Interrupt
 901 *   6. Disable Musb Clock
 902 */
 903static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
 904{
 905	struct ab8500_usb *ab = (struct ab8500_usb *) data;
 906	enum usb_phy_events event = UX500_MUSB_NONE;
 907
 908	/* Link status will not be updated till phy is disabled. */
 909	if (ab->mode == USB_HOST) {
 910		ab->phy.otg->default_a = false;
 911		ab->vbus_draw = 0;
 912		atomic_notifier_call_chain(&ab->phy.notifier,
 913				event, &ab->vbus_draw);
 914		ab8500_usb_host_phy_dis(ab);
 915		ab->mode = USB_IDLE;
 916	}
 917
 918	if (ab->mode == USB_PERIPHERAL) {
 919		atomic_notifier_call_chain(&ab->phy.notifier,
 920				event, &ab->vbus_draw);
 921		ab8500_usb_peri_phy_dis(ab);
 922		atomic_notifier_call_chain(&ab->phy.notifier,
 923				UX500_MUSB_CLEAN, &ab->vbus_draw);
 924		ab->mode = USB_IDLE;
 925		ab->phy.otg->default_a = false;
 926		ab->vbus_draw = 0;
 927	}
 928
 
 
 
 
 
 929	if (is_ab8500_2p0(ab->ab8500)) {
 930		if (ab->mode == USB_DEDICATED_CHG) {
 931			ab8500_usb_wd_linkstatus(ab,
 932					AB8500_BIT_PHY_CTRL_DEVICE_EN);
 933			abx500_mask_and_set_register_interruptible(ab->dev,
 934					AB8500_USB, AB8500_USB_PHY_CTRL_REG,
 935					AB8500_BIT_PHY_CTRL_DEVICE_EN, 0);
 936		}
 937	}
 938
 939	return IRQ_HANDLED;
 940}
 941
 942static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data)
 943{
 944	struct ab8500_usb *ab = (struct ab8500_usb *)data;
 945
 946	abx500_usb_link_status_update(ab);
 947
 948	return IRQ_HANDLED;
 949}
 950
 951static void ab8500_usb_phy_disable_work(struct work_struct *work)
 952{
 953	struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
 954						phy_dis_work);
 955
 956	if (!ab->phy.otg->host)
 957		ab8500_usb_host_phy_dis(ab);
 958
 959	if (!ab->phy.otg->gadget)
 960		ab8500_usb_peri_phy_dis(ab);
 961}
 962
 963/* Check if VBUS is set and linkstatus has not detected a cable. */
 964static bool ab8500_usb_check_vbus_status(struct ab8500_usb *ab)
 965{
 966	u8 isource2;
 967	u8 reg;
 968	enum ab8540_usb_link_status lsts;
 969
 970	abx500_get_register_interruptible(ab->dev,
 971			AB8500_INTERRUPT, AB8500_IT_SOURCE2_REG,
 972			&isource2);
 973
 974	/* If Vbus is below 3.6V abort */
 975	if (!(isource2 & AB8500_BIT_SOURCE2_VBUSDET))
 976		return false;
 977
 978	abx500_get_register_interruptible(ab->dev,
 979			AB8500_USB, AB8540_USB_LINK_STAT_REG,
 980			&reg);
 981
 982	lsts = (reg >> 3) & 0xFF;
 983
 984	/* Check if linkstatus has detected a cable */
 985	if (lsts)
 986		return false;
 987
 988	return true;
 989}
 990
 991/* re-trigger charger detection again with watchdog re-kick. */
 992static void ab8500_usb_vbus_turn_on_event_work(struct work_struct *work)
 993{
 994	struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
 995			vbus_event_work);
 996
 997	if (ab->mode != USB_IDLE)
 998		return;
 999
1000	abx500_set_register_interruptible(ab->dev,
1001			AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
1002			AB8500_BIT_WD_CTRL_ENABLE);
1003
1004	udelay(100);
1005
1006	abx500_set_register_interruptible(ab->dev,
1007			AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
1008			AB8500_BIT_WD_CTRL_ENABLE | AB8500_BIT_WD_CTRL_KICK);
1009
1010	udelay(100);
1011
1012	/* Disable Main watchdog */
1013	abx500_set_register_interruptible(ab->dev,
1014			AB8500_SYS_CTRL2_BLOCK, AB8500_MAIN_WD_CTRL_REG,
1015			0x0);
1016
1017	/* Enable USB Charger detection */
1018	abx500_mask_and_set_register_interruptible(ab->dev,
1019			AB8500_USB, AB8540_VBUS_CTRL_REG,
1020			AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA,
1021			AB8540_BIT_VBUS_CTRL_CHARG_DET_ENA);
1022
1023	ab->enabled_charging_detection = true;
1024}
1025
1026static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
1027{
1028	/*
1029	 * AB8500 V2 has eye diagram issues when drawing more than 100mA from
1030	 * VBUS.  Set charging current to 100mA in case of standard host
1031	 */
1032	if (is_ab8500_2p0_or_earlier(ab->ab8500))
1033		if (mA > 100)
1034			mA = 100;
1035
1036	return mA;
1037}
1038
1039static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
1040{
1041	struct ab8500_usb *ab;
1042
1043	if (!phy)
1044		return -ENODEV;
1045
1046	ab = phy_to_ab(phy);
1047
1048	mA = ab8500_eyediagram_workaroud(ab, mA);
1049
1050	ab->vbus_draw = mA;
1051
1052	atomic_notifier_call_chain(&ab->phy.notifier,
1053			UX500_MUSB_VBUS, &ab->vbus_draw);
1054
1055	return 0;
1056}
1057
1058static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
1059{
1060	/* TODO */
1061	return 0;
1062}
1063
1064static int ab8500_usb_set_peripheral(struct usb_otg *otg,
1065					struct usb_gadget *gadget)
1066{
1067	struct ab8500_usb *ab;
1068
1069	if (!otg)
1070		return -ENODEV;
1071
1072	ab = phy_to_ab(otg->usb_phy);
1073
1074	ab->phy.otg->gadget = gadget;
1075
1076	/* Some drivers call this function in atomic context.
1077	 * Do not update ab8500 registers directly till this
1078	 * is fixed.
1079	 */
1080
1081	if ((ab->mode != USB_IDLE) && !gadget) {
1082		ab->mode = USB_IDLE;
1083		schedule_work(&ab->phy_dis_work);
1084	}
1085
1086	return 0;
1087}
1088
1089static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
1090{
1091	struct ab8500_usb *ab;
1092
1093	if (!otg)
1094		return -ENODEV;
1095
1096	ab = phy_to_ab(otg->usb_phy);
1097
1098	ab->phy.otg->host = host;
1099
1100	/* Some drivers call this function in atomic context.
1101	 * Do not update ab8500 registers directly till this
1102	 * is fixed.
1103	 */
1104
1105	if ((ab->mode != USB_IDLE) && !host) {
1106		ab->mode = USB_IDLE;
1107		schedule_work(&ab->phy_dis_work);
1108	}
1109
1110	return 0;
1111}
1112
1113static void ab8500_usb_restart_phy(struct ab8500_usb *ab)
1114{
1115	abx500_mask_and_set_register_interruptible(ab->dev,
1116			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1117			AB8500_BIT_PHY_CTRL_DEVICE_EN,
1118			AB8500_BIT_PHY_CTRL_DEVICE_EN);
1119
1120	udelay(100);
1121
1122	abx500_mask_and_set_register_interruptible(ab->dev,
1123			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1124			AB8500_BIT_PHY_CTRL_DEVICE_EN,
1125			0);
1126
1127	abx500_mask_and_set_register_interruptible(ab->dev,
1128			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1129			AB8500_BIT_PHY_CTRL_HOST_EN,
1130			AB8500_BIT_PHY_CTRL_HOST_EN);
1131
1132	udelay(100);
1133
1134	abx500_mask_and_set_register_interruptible(ab->dev,
1135			AB8500_USB, AB8500_USB_PHY_CTRL_REG,
1136			AB8500_BIT_PHY_CTRL_HOST_EN,
1137			0);
1138}
1139
1140static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
1141{
1142	int err;
1143
1144	ab->v_ape = devm_regulator_get(ab->dev, "v-ape");
1145	if (IS_ERR(ab->v_ape)) {
1146		dev_err(ab->dev, "Could not get v-ape supply\n");
1147		err = PTR_ERR(ab->v_ape);
1148		return err;
1149	}
1150
1151	ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18");
1152	if (IS_ERR(ab->v_ulpi)) {
1153		dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
1154		err = PTR_ERR(ab->v_ulpi);
1155		return err;
1156	}
1157
1158	ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8");
1159	if (IS_ERR(ab->v_musb)) {
1160		dev_err(ab->dev, "Could not get musb_1v8 supply\n");
1161		err = PTR_ERR(ab->v_musb);
1162		return err;
1163	}
1164
1165	return 0;
1166}
1167
1168static int ab8500_usb_irq_setup(struct platform_device *pdev,
1169		struct ab8500_usb *ab)
1170{
1171	int err;
1172	int irq;
1173
1174	if (ab->flags & AB8500_USB_FLAG_USE_LINK_STATUS_IRQ) {
1175		irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
1176		if (irq < 0) {
1177			dev_err(&pdev->dev, "Link status irq not found\n");
1178			return irq;
1179		}
1180		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1181				ab8500_usb_link_status_irq,
1182				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
1183				"usb-link-status", ab);
1184		if (err < 0) {
1185			dev_err(ab->dev, "request_irq failed for link status irq\n");
1186			return err;
1187		}
1188	}
1189
1190	if (ab->flags & AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ) {
1191		irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
1192		if (irq < 0) {
1193			dev_err(&pdev->dev, "ID fall irq not found\n");
1194			return irq;
1195		}
1196		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1197				ab8500_usb_disconnect_irq,
1198				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
1199				"usb-id-fall", ab);
1200		if (err < 0) {
1201			dev_err(ab->dev, "request_irq failed for ID fall irq\n");
1202			return err;
1203		}
1204	}
1205
1206	if (ab->flags & AB8500_USB_FLAG_USE_VBUS_DET_IRQ) {
1207		irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
1208		if (irq < 0) {
1209			dev_err(&pdev->dev, "VBUS fall irq not found\n");
1210			return irq;
1211		}
1212		err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1213				ab8500_usb_disconnect_irq,
1214				IRQF_NO_SUSPEND | IRQF_SHARED | IRQF_ONESHOT,
1215				"usb-vbus-fall", ab);
1216		if (err < 0) {
1217			dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
1218			return err;
1219		}
1220	}
1221
1222	return 0;
1223}
1224
1225static void ab8500_usb_set_ab8500_tuning_values(struct ab8500_usb *ab)
1226{
1227	int err;
1228
1229	/* Enable the PBT/Bank 0x12 access */
1230	err = abx500_set_register_interruptible(ab->dev,
1231			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
1232	if (err < 0)
1233		dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1234				err);
1235
1236	err = abx500_set_register_interruptible(ab->dev,
1237			AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
1238	if (err < 0)
1239		dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1240				err);
1241
1242	err = abx500_set_register_interruptible(ab->dev,
1243			AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00);
1244	if (err < 0)
1245		dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1246				err);
1247
1248	err = abx500_set_register_interruptible(ab->dev,
1249			AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78);
1250	if (err < 0)
1251		dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
1252				err);
1253
1254	/* Switch to normal mode/disable Bank 0x12 access */
1255	err = abx500_set_register_interruptible(ab->dev,
1256			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
1257	if (err < 0)
1258		dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1259				err);
1260}
1261
1262static void ab8500_usb_set_ab8505_tuning_values(struct ab8500_usb *ab)
1263{
1264	int err;
1265
1266	/* Enable the PBT/Bank 0x12 access */
1267	err = abx500_mask_and_set_register_interruptible(ab->dev,
1268			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
1269			0x01, 0x01);
1270	if (err < 0)
1271		dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1272				err);
1273
1274	err = abx500_mask_and_set_register_interruptible(ab->dev,
1275			AB8500_DEBUG, AB8500_USB_PHY_TUNE1,
1276			0xC8, 0xC8);
1277	if (err < 0)
1278		dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1279				err);
1280
1281	err = abx500_mask_and_set_register_interruptible(ab->dev,
1282			AB8500_DEBUG, AB8500_USB_PHY_TUNE2,
1283			0x60, 0x60);
1284	if (err < 0)
1285		dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1286				err);
1287
1288	err = abx500_mask_and_set_register_interruptible(ab->dev,
1289			AB8500_DEBUG, AB8500_USB_PHY_TUNE3,
1290			0xFC, 0x80);
1291
1292	if (err < 0)
1293		dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
1294				err);
1295
1296	/* Switch to normal mode/disable Bank 0x12 access */
1297	err = abx500_mask_and_set_register_interruptible(ab->dev,
1298			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS,
1299			0x00, 0x00);
1300	if (err < 0)
1301		dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1302				err);
1303}
1304
1305static void ab8500_usb_set_ab8540_tuning_values(struct ab8500_usb *ab)
1306{
1307	int err;
1308
1309	err = abx500_set_register_interruptible(ab->dev,
1310			AB8540_DEBUG, AB8500_USB_PHY_TUNE1, 0xCC);
1311	if (err < 0)
1312		dev_err(ab->dev, "Failed to set PHY_TUNE1 register ret=%d\n",
1313				err);
1314
1315	err = abx500_set_register_interruptible(ab->dev,
1316			AB8540_DEBUG, AB8500_USB_PHY_TUNE2, 0x60);
1317	if (err < 0)
1318		dev_err(ab->dev, "Failed to set PHY_TUNE2 register ret=%d\n",
1319				err);
1320
1321	err = abx500_set_register_interruptible(ab->dev,
1322			AB8540_DEBUG, AB8500_USB_PHY_TUNE3, 0x90);
1323	if (err < 0)
1324		dev_err(ab->dev, "Failed to set PHY_TUNE3 register ret=%d\n",
1325				err);
1326}
1327
1328static void ab8500_usb_set_ab9540_tuning_values(struct ab8500_usb *ab)
1329{
1330	int err;
1331
1332	/* Enable the PBT/Bank 0x12 access */
1333	err = abx500_set_register_interruptible(ab->dev,
1334			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01);
1335	if (err < 0)
1336		dev_err(ab->dev, "Failed to enable bank12 access err=%d\n",
1337				err);
1338
1339	err = abx500_set_register_interruptible(ab->dev,
1340			AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8);
1341	if (err < 0)
1342		dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n",
1343				err);
1344
1345	err = abx500_set_register_interruptible(ab->dev,
1346			AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x60);
1347	if (err < 0)
1348		dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n",
1349				err);
1350
1351	err = abx500_set_register_interruptible(ab->dev,
1352			AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x80);
1353	if (err < 0)
1354		dev_err(ab->dev, "Failed to set PHY_TUNE3 register err=%d\n",
1355				err);
1356
1357	/* Switch to normal mode/disable Bank 0x12 access */
1358	err = abx500_set_register_interruptible(ab->dev,
1359			AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00);
1360	if (err < 0)
1361		dev_err(ab->dev, "Failed to switch bank12 access err=%d\n",
1362				err);
1363}
1364
1365static int ab8500_usb_probe(struct platform_device *pdev)
1366{
1367	struct ab8500_usb	*ab;
1368	struct ab8500		*ab8500;
1369	struct usb_otg		*otg;
1370	int err;
1371	int rev;
1372
1373	ab8500 = dev_get_drvdata(pdev->dev.parent);
1374	rev = abx500_get_chip_id(&pdev->dev);
1375
1376	if (is_ab8500_1p1_or_earlier(ab8500)) {
1377		dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
1378		return -ENODEV;
1379	}
1380
1381	ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL);
1382	if (!ab)
1383		return -ENOMEM;
1384
1385	otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
1386	if (!otg)
1387		return -ENOMEM;
1388
1389	ab->dev			= &pdev->dev;
1390	ab->ab8500		= ab8500;
1391	ab->phy.dev		= ab->dev;
1392	ab->phy.otg		= otg;
1393	ab->phy.label		= "ab8500";
1394	ab->phy.set_suspend	= ab8500_usb_set_suspend;
1395	ab->phy.set_power	= ab8500_usb_set_power;
1396	ab->phy.otg->state	= OTG_STATE_UNDEFINED;
1397
1398	otg->usb_phy		= &ab->phy;
1399	otg->set_host		= ab8500_usb_set_host;
1400	otg->set_peripheral	= ab8500_usb_set_peripheral;
1401
1402	if (is_ab8500(ab->ab8500)) {
1403		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1404			AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1405			AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
1406			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1407	} else if (is_ab8505(ab->ab8500)) {
1408		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1409			AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1410			AB8500_USB_FLAG_USE_VBUS_DET_IRQ |
1411			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1412	} else if (is_ab8540(ab->ab8500)) {
1413		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1414			AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS |
1415			AB8500_USB_FLAG_USE_VBUS_HOST_QUIRK |
1416			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1417	} else if (is_ab9540(ab->ab8500)) {
1418		ab->flags |= AB8500_USB_FLAG_USE_LINK_STATUS_IRQ |
1419			AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1420		if (is_ab9540_2p0_or_earlier(ab->ab8500))
1421			ab->flags |= AB8500_USB_FLAG_USE_ID_WAKEUP_IRQ |
1422				AB8500_USB_FLAG_USE_VBUS_DET_IRQ;
1423	}
1424
1425	/* Disable regulator voltage setting for AB8500 <= v2.0 */
1426	if (is_ab8500_2p0_or_earlier(ab->ab8500))
1427		ab->flags &= ~AB8500_USB_FLAG_REGULATOR_SET_VOLTAGE;
1428
1429	platform_set_drvdata(pdev, ab);
1430
1431	/* all: Disable phy when called from set_host and set_peripheral */
1432	INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);
1433
1434	INIT_WORK(&ab->vbus_event_work, ab8500_usb_vbus_turn_on_event_work);
1435
1436	err = ab8500_usb_regulator_get(ab);
1437	if (err)
1438		return err;
1439
1440	ab->sysclk = devm_clk_get(ab->dev, "sysclk");
1441	if (IS_ERR(ab->sysclk)) {
1442		dev_err(ab->dev, "Could not get sysclk.\n");
1443		return PTR_ERR(ab->sysclk);
1444	}
1445
1446	err = ab8500_usb_irq_setup(pdev, ab);
1447	if (err < 0)
1448		return err;
1449
1450	err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2);
1451	if (err) {
1452		dev_err(&pdev->dev, "Can't register transceiver\n");
1453		return err;
1454	}
1455
1456	if (is_ab8500(ab->ab8500) && !is_ab8500_2p0_or_earlier(ab->ab8500))
1457		/* Phy tuning values for AB8500 > v2.0 */
1458		ab8500_usb_set_ab8500_tuning_values(ab);
1459	else if (is_ab8505(ab->ab8500))
1460		/* Phy tuning values for AB8505 */
1461		ab8500_usb_set_ab8505_tuning_values(ab);
1462	else if (is_ab8540(ab->ab8500))
1463		/* Phy tuning values for AB8540 */
1464		ab8500_usb_set_ab8540_tuning_values(ab);
1465	else if (is_ab9540(ab->ab8500))
1466		/* Phy tuning values for AB9540 */
1467		ab8500_usb_set_ab9540_tuning_values(ab);
1468
1469	/* Needed to enable ID detection. */
1470	ab8500_usb_wd_workaround(ab);
1471
1472	/*
1473	 * This is required for usb-link-status to work properly when a
1474	 * cable is connected at boot time.
1475	 */
1476	ab8500_usb_restart_phy(ab);
1477
1478	if (ab->flags & AB8500_USB_FLAG_USE_CHECK_VBUS_STATUS) {
1479		if (ab8500_usb_check_vbus_status(ab))
1480			schedule_work(&ab->vbus_event_work);
1481	}
1482
1483	abx500_usb_link_status_update(ab);
1484
1485	dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);
1486
1487	return 0;
1488}
1489
1490static int ab8500_usb_remove(struct platform_device *pdev)
1491{
1492	struct ab8500_usb *ab = platform_get_drvdata(pdev);
1493
1494	cancel_work_sync(&ab->phy_dis_work);
1495	cancel_work_sync(&ab->vbus_event_work);
1496
1497	usb_remove_phy(&ab->phy);
1498
1499	if (ab->mode == USB_HOST)
1500		ab8500_usb_host_phy_dis(ab);
1501	else if (ab->mode == USB_PERIPHERAL)
1502		ab8500_usb_peri_phy_dis(ab);
1503
1504	return 0;
1505}
1506
1507static const struct platform_device_id ab8500_usb_devtype[] = {
1508	{ .name = "ab8500-usb", },
1509	{ .name = "ab8540-usb", },
1510	{ .name = "ab9540-usb", },
1511	{ /* sentinel */ }
1512};
1513MODULE_DEVICE_TABLE(platform, ab8500_usb_devtype);
1514
1515static struct platform_driver ab8500_usb_driver = {
1516	.probe		= ab8500_usb_probe,
1517	.remove		= ab8500_usb_remove,
1518	.id_table	= ab8500_usb_devtype,
1519	.driver		= {
1520		.name	= "abx5x0-usb",
1521	},
1522};
1523
1524static int __init ab8500_usb_init(void)
1525{
1526	return platform_driver_register(&ab8500_usb_driver);
1527}
1528subsys_initcall(ab8500_usb_init);
1529
1530static void __exit ab8500_usb_exit(void)
1531{
1532	platform_driver_unregister(&ab8500_usb_driver);
1533}
1534module_exit(ab8500_usb_exit);
1535
1536MODULE_AUTHOR("ST-Ericsson AB");
1537MODULE_DESCRIPTION("AB8500 family usb transceiver driver");
1538MODULE_LICENSE("GPL");