1 /* 2 * drivers/usb/otg/ab8500_usb.c 3 * 4 * USB transceiver driver for AB8500 chip 5 * 6 * Copyright (C) 2010 ST-Ericsson AB 7 * Mian Yousaf Kaukab <mian.yousaf.kaukab@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/mfd/abx500.h> 33 #include <linux/mfd/abx500/ab8500.h> 34 #include <linux/usb/musb-ux500.h> 35 #include <linux/regulator/consumer.h> 36 #include <linux/pinctrl/consumer.h> 37 38 /* Bank AB8500_SYS_CTRL2_BLOCK */ 39 #define AB8500_MAIN_WD_CTRL_REG 0x01 40 41 /* Bank AB8500_USB */ 42 #define AB8500_USB_LINE_STAT_REG 0x80 43 #define AB8505_USB_LINE_STAT_REG 0x94 44 #define AB8500_USB_PHY_CTRL_REG 0x8A 45 46 /* Bank AB8500_DEVELOPMENT */ 47 #define AB8500_BANK12_ACCESS 0x00 48 49 /* Bank AB8500_DEBUG */ 50 #define AB8500_USB_PHY_TUNE1 0x05 51 #define AB8500_USB_PHY_TUNE2 0x06 52 #define AB8500_USB_PHY_TUNE3 0x07 53 54 #define AB8500_BIT_OTG_STAT_ID (1 << 0) 55 #define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0) 56 #define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1) 57 #define AB8500_BIT_WD_CTRL_ENABLE (1 << 0) 58 #define AB8500_BIT_WD_CTRL_KICK (1 << 1) 59 60 #define AB8500_WD_KICK_DELAY_US 100 /* usec */ 61 #define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */ 62 #define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */ 63 64 /* Usb line status register */ 65 enum ab8500_usb_link_status { 66 USB_LINK_NOT_CONFIGURED_8500 = 0, 67 USB_LINK_STD_HOST_NC_8500, 68 USB_LINK_STD_HOST_C_NS_8500, 69 USB_LINK_STD_HOST_C_S_8500, 70 USB_LINK_HOST_CHG_NM_8500, 71 USB_LINK_HOST_CHG_HS_8500, 72 USB_LINK_HOST_CHG_HS_CHIRP_8500, 73 USB_LINK_DEDICATED_CHG_8500, 74 USB_LINK_ACA_RID_A_8500, 75 USB_LINK_ACA_RID_B_8500, 76 USB_LINK_ACA_RID_C_NM_8500, 77 USB_LINK_ACA_RID_C_HS_8500, 78 USB_LINK_ACA_RID_C_HS_CHIRP_8500, 79 USB_LINK_HM_IDGND_8500, 80 USB_LINK_RESERVED_8500, 81 USB_LINK_NOT_VALID_LINK_8500, 82 }; 83 84 enum ab8505_usb_link_status { 85 USB_LINK_NOT_CONFIGURED_8505 = 0, 86 USB_LINK_STD_HOST_NC_8505, 87 USB_LINK_STD_HOST_C_NS_8505, 88 USB_LINK_STD_HOST_C_S_8505, 89 USB_LINK_CDP_8505, 90 USB_LINK_RESERVED0_8505, 91 USB_LINK_RESERVED1_8505, 92 USB_LINK_DEDICATED_CHG_8505, 93 USB_LINK_ACA_RID_A_8505, 94 USB_LINK_ACA_RID_B_8505, 95 USB_LINK_ACA_RID_C_NM_8505, 96 USB_LINK_RESERVED2_8505, 97 USB_LINK_RESERVED3_8505, 98 USB_LINK_HM_IDGND_8505, 99 USB_LINK_CHARGERPORT_NOT_OK_8505, 100 USB_LINK_CHARGER_DM_HIGH_8505, 101 USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505, 102 USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505, 103 USB_LINK_STD_UPSTREAM_8505, 104 USB_LINK_CHARGER_SE1_8505, 105 USB_LINK_CARKIT_CHGR_1_8505, 106 USB_LINK_CARKIT_CHGR_2_8505, 107 USB_LINK_ACA_DOCK_CHGR_8505, 108 USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505, 109 USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505, 110 USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505, 111 USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505, 112 USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505, 113 }; 114 115 enum ab8500_usb_mode { 116 USB_IDLE = 0, 117 USB_PERIPHERAL, 118 USB_HOST, 119 USB_DEDICATED_CHG 120 }; 121 122 struct ab8500_usb { 123 struct usb_phy phy; 124 struct device *dev; 125 struct ab8500 *ab8500; 126 unsigned vbus_draw; 127 struct work_struct phy_dis_work; 128 enum ab8500_usb_mode mode; 129 struct regulator *v_ape; 130 struct regulator *v_musb; 131 struct regulator *v_ulpi; 132 int saved_v_ulpi; 133 int previous_link_status_state; 134 struct pinctrl *pinctrl; 135 struct pinctrl_state *pins_sleep; 136 }; 137 138 static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x) 139 { 140 return container_of(x, struct ab8500_usb, phy); 141 } 142 143 static void ab8500_usb_wd_workaround(struct ab8500_usb *ab) 144 { 145 abx500_set_register_interruptible(ab->dev, 146 AB8500_SYS_CTRL2_BLOCK, 147 AB8500_MAIN_WD_CTRL_REG, 148 AB8500_BIT_WD_CTRL_ENABLE); 149 150 udelay(AB8500_WD_KICK_DELAY_US); 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 | AB8500_BIT_WD_CTRL_KICK)); 157 158 udelay(AB8500_WD_V11_DISABLE_DELAY_US); 159 160 abx500_set_register_interruptible(ab->dev, 161 AB8500_SYS_CTRL2_BLOCK, 162 AB8500_MAIN_WD_CTRL_REG, 163 0); 164 } 165 166 static void ab8500_usb_regulator_enable(struct ab8500_usb *ab) 167 { 168 int ret, volt; 169 170 regulator_enable(ab->v_ape); 171 172 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) { 173 ab->saved_v_ulpi = regulator_get_voltage(ab->v_ulpi); 174 if (ab->saved_v_ulpi < 0) 175 dev_err(ab->dev, "Failed to get v_ulpi voltage\n"); 176 177 ret = regulator_set_voltage(ab->v_ulpi, 1300000, 1350000); 178 if (ret < 0) 179 dev_err(ab->dev, "Failed to set the Vintcore to 1.3V, ret=%d\n", 180 ret); 181 182 ret = regulator_set_optimum_mode(ab->v_ulpi, 28000); 183 if (ret < 0) 184 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n", 185 ret); 186 } 187 188 regulator_enable(ab->v_ulpi); 189 190 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) { 191 volt = regulator_get_voltage(ab->v_ulpi); 192 if ((volt != 1300000) && (volt != 1350000)) 193 dev_err(ab->dev, "Vintcore is not set to 1.3V volt=%d\n", 194 volt); 195 } 196 197 regulator_enable(ab->v_musb); 198 } 199 200 static void ab8500_usb_regulator_disable(struct ab8500_usb *ab) 201 { 202 int ret; 203 204 regulator_disable(ab->v_musb); 205 206 regulator_disable(ab->v_ulpi); 207 208 /* USB is not the only consumer of Vintcore, restore old settings */ 209 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) { 210 if (ab->saved_v_ulpi > 0) { 211 ret = regulator_set_voltage(ab->v_ulpi, 212 ab->saved_v_ulpi, ab->saved_v_ulpi); 213 if (ret < 0) 214 dev_err(ab->dev, "Failed to set the Vintcore to %duV, ret=%d\n", 215 ab->saved_v_ulpi, ret); 216 } 217 218 ret = regulator_set_optimum_mode(ab->v_ulpi, 0); 219 if (ret < 0) 220 dev_err(ab->dev, "Failed to set optimum mode (ret=%d)\n", 221 ret); 222 } 223 224 regulator_disable(ab->v_ape); 225 } 226 227 static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab, u8 bit) 228 { 229 /* Workaround for v2.0 bug # 31952 */ 230 if (is_ab8500_2p0(ab->ab8500)) { 231 abx500_mask_and_set_register_interruptible(ab->dev, 232 AB8500_USB, AB8500_USB_PHY_CTRL_REG, 233 bit, bit); 234 udelay(AB8500_V20_31952_DISABLE_DELAY_US); 235 } 236 } 237 238 static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host) 239 { 240 u8 bit; 241 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN : 242 AB8500_BIT_PHY_CTRL_DEVICE_EN; 243 244 /* mux and configure USB pins to DEFAULT state */ 245 ab->pinctrl = pinctrl_get_select(ab->dev, PINCTRL_STATE_DEFAULT); 246 if (IS_ERR(ab->pinctrl)) 247 dev_err(ab->dev, "could not get/set default pinstate\n"); 248 249 ab8500_usb_regulator_enable(ab); 250 251 abx500_mask_and_set_register_interruptible(ab->dev, 252 AB8500_USB, AB8500_USB_PHY_CTRL_REG, 253 bit, bit); 254 } 255 256 static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host) 257 { 258 u8 bit; 259 bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN : 260 AB8500_BIT_PHY_CTRL_DEVICE_EN; 261 262 ab8500_usb_wd_linkstatus(ab, bit); 263 264 abx500_mask_and_set_register_interruptible(ab->dev, 265 AB8500_USB, AB8500_USB_PHY_CTRL_REG, 266 bit, 0); 267 268 /* Needed to disable the phy.*/ 269 ab8500_usb_wd_workaround(ab); 270 271 ab8500_usb_regulator_disable(ab); 272 273 if (!IS_ERR(ab->pinctrl)) { 274 /* configure USB pins to SLEEP state */ 275 ab->pins_sleep = pinctrl_lookup_state(ab->pinctrl, 276 PINCTRL_STATE_SLEEP); 277 278 if (IS_ERR(ab->pins_sleep)) 279 dev_dbg(ab->dev, "could not get sleep pinstate\n"); 280 else if (pinctrl_select_state(ab->pinctrl, ab->pins_sleep)) 281 dev_err(ab->dev, "could not set pins to sleep state\n"); 282 283 /* as USB pins are shared with idddet, release them to allow 284 * iddet to request them 285 */ 286 pinctrl_put(ab->pinctrl); 287 } 288 } 289 290 #define ab8500_usb_host_phy_en(ab) ab8500_usb_phy_enable(ab, true) 291 #define ab8500_usb_host_phy_dis(ab) ab8500_usb_phy_disable(ab, true) 292 #define ab8500_usb_peri_phy_en(ab) ab8500_usb_phy_enable(ab, false) 293 #define ab8500_usb_peri_phy_dis(ab) ab8500_usb_phy_disable(ab, false) 294 295 static int ab8505_usb_link_status_update(struct ab8500_usb *ab, 296 enum ab8505_usb_link_status lsts) 297 { 298 enum ux500_musb_vbus_id_status event = 0; 299 300 dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts); 301 302 /* 303 * Spurious link_status interrupts are seen at the time of 304 * disconnection of a device in RIDA state 305 */ 306 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8505 && 307 (lsts == USB_LINK_STD_HOST_NC_8505)) 308 return 0; 309 310 ab->previous_link_status_state = lsts; 311 312 switch (lsts) { 313 case USB_LINK_ACA_RID_B_8505: 314 event = UX500_MUSB_RIDB; 315 case USB_LINK_NOT_CONFIGURED_8505: 316 case USB_LINK_RESERVED0_8505: 317 case USB_LINK_RESERVED1_8505: 318 case USB_LINK_RESERVED2_8505: 319 case USB_LINK_RESERVED3_8505: 320 ab->mode = USB_IDLE; 321 ab->phy.otg->default_a = false; 322 ab->vbus_draw = 0; 323 if (event != UX500_MUSB_RIDB) 324 event = UX500_MUSB_NONE; 325 /* 326 * Fallback to default B_IDLE as nothing 327 * is connected 328 */ 329 ab->phy.state = OTG_STATE_B_IDLE; 330 break; 331 332 case USB_LINK_ACA_RID_C_NM_8505: 333 event = UX500_MUSB_RIDC; 334 case USB_LINK_STD_HOST_NC_8505: 335 case USB_LINK_STD_HOST_C_NS_8505: 336 case USB_LINK_STD_HOST_C_S_8505: 337 case USB_LINK_CDP_8505: 338 if (ab->mode == USB_IDLE) { 339 ab->mode = USB_PERIPHERAL; 340 ab8500_usb_peri_phy_en(ab); 341 atomic_notifier_call_chain(&ab->phy.notifier, 342 UX500_MUSB_PREPARE, &ab->vbus_draw); 343 } 344 if (event != UX500_MUSB_RIDC) 345 event = UX500_MUSB_VBUS; 346 break; 347 348 case USB_LINK_ACA_RID_A_8505: 349 case USB_LINK_ACA_DOCK_CHGR_8505: 350 event = UX500_MUSB_RIDA; 351 case USB_LINK_HM_IDGND_8505: 352 if (ab->mode == USB_IDLE) { 353 ab->mode = USB_HOST; 354 ab8500_usb_host_phy_en(ab); 355 atomic_notifier_call_chain(&ab->phy.notifier, 356 UX500_MUSB_PREPARE, &ab->vbus_draw); 357 } 358 ab->phy.otg->default_a = true; 359 if (event != UX500_MUSB_RIDA) 360 event = UX500_MUSB_ID; 361 atomic_notifier_call_chain(&ab->phy.notifier, 362 event, &ab->vbus_draw); 363 break; 364 365 case USB_LINK_DEDICATED_CHG_8505: 366 ab->mode = USB_DEDICATED_CHG; 367 event = UX500_MUSB_CHARGER; 368 atomic_notifier_call_chain(&ab->phy.notifier, 369 event, &ab->vbus_draw); 370 break; 371 372 default: 373 break; 374 } 375 376 return 0; 377 } 378 379 static int ab8500_usb_link_status_update(struct ab8500_usb *ab, 380 enum ab8500_usb_link_status lsts) 381 { 382 enum ux500_musb_vbus_id_status event = 0; 383 384 dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts); 385 386 /* 387 * Spurious link_status interrupts are seen in case of a 388 * disconnection of a device in IDGND and RIDA stage 389 */ 390 if (ab->previous_link_status_state == USB_LINK_HM_IDGND_8500 && 391 (lsts == USB_LINK_STD_HOST_C_NS_8500 || 392 lsts == USB_LINK_STD_HOST_NC_8500)) 393 return 0; 394 395 if (ab->previous_link_status_state == USB_LINK_ACA_RID_A_8500 && 396 lsts == USB_LINK_STD_HOST_NC_8500) 397 return 0; 398 399 ab->previous_link_status_state = lsts; 400 401 switch (lsts) { 402 case USB_LINK_ACA_RID_B_8500: 403 event = UX500_MUSB_RIDB; 404 case USB_LINK_NOT_CONFIGURED_8500: 405 case USB_LINK_NOT_VALID_LINK_8500: 406 ab->mode = USB_IDLE; 407 ab->phy.otg->default_a = false; 408 ab->vbus_draw = 0; 409 if (event != UX500_MUSB_RIDB) 410 event = UX500_MUSB_NONE; 411 /* Fallback to default B_IDLE as nothing is connected */ 412 ab->phy.state = OTG_STATE_B_IDLE; 413 break; 414 415 case USB_LINK_ACA_RID_C_NM_8500: 416 case USB_LINK_ACA_RID_C_HS_8500: 417 case USB_LINK_ACA_RID_C_HS_CHIRP_8500: 418 event = UX500_MUSB_RIDC; 419 case USB_LINK_STD_HOST_NC_8500: 420 case USB_LINK_STD_HOST_C_NS_8500: 421 case USB_LINK_STD_HOST_C_S_8500: 422 case USB_LINK_HOST_CHG_NM_8500: 423 case USB_LINK_HOST_CHG_HS_8500: 424 case USB_LINK_HOST_CHG_HS_CHIRP_8500: 425 if (ab->mode == USB_IDLE) { 426 ab->mode = USB_PERIPHERAL; 427 ab8500_usb_peri_phy_en(ab); 428 atomic_notifier_call_chain(&ab->phy.notifier, 429 UX500_MUSB_PREPARE, &ab->vbus_draw); 430 } 431 if (event != UX500_MUSB_RIDC) 432 event = UX500_MUSB_VBUS; 433 break; 434 435 case USB_LINK_ACA_RID_A_8500: 436 event = UX500_MUSB_RIDA; 437 case USB_LINK_HM_IDGND_8500: 438 if (ab->mode == USB_IDLE) { 439 ab->mode = USB_HOST; 440 ab8500_usb_host_phy_en(ab); 441 atomic_notifier_call_chain(&ab->phy.notifier, 442 UX500_MUSB_PREPARE, &ab->vbus_draw); 443 } 444 ab->phy.otg->default_a = true; 445 if (event != UX500_MUSB_RIDA) 446 event = UX500_MUSB_ID; 447 atomic_notifier_call_chain(&ab->phy.notifier, 448 event, &ab->vbus_draw); 449 break; 450 451 case USB_LINK_DEDICATED_CHG_8500: 452 ab->mode = USB_DEDICATED_CHG; 453 event = UX500_MUSB_CHARGER; 454 atomic_notifier_call_chain(&ab->phy.notifier, 455 event, &ab->vbus_draw); 456 break; 457 458 case USB_LINK_RESERVED_8500: 459 break; 460 } 461 462 return 0; 463 } 464 465 /* 466 * Connection Sequence: 467 * 1. Link Status Interrupt 468 * 2. Enable AB clock 469 * 3. Enable AB regulators 470 * 4. Enable USB phy 471 * 5. Reset the musb controller 472 * 6. Switch the ULPI GPIO pins to fucntion mode 473 * 7. Enable the musb Peripheral5 clock 474 * 8. Restore MUSB context 475 */ 476 static int abx500_usb_link_status_update(struct ab8500_usb *ab) 477 { 478 u8 reg; 479 int ret = 0; 480 481 if (is_ab8500(ab->ab8500)) { 482 enum ab8500_usb_link_status lsts; 483 484 abx500_get_register_interruptible(ab->dev, 485 AB8500_USB, AB8500_USB_LINE_STAT_REG, ®); 486 lsts = (reg >> 3) & 0x0F; 487 ret = ab8500_usb_link_status_update(ab, lsts); 488 } else if (is_ab8505(ab->ab8500)) { 489 enum ab8505_usb_link_status lsts; 490 491 abx500_get_register_interruptible(ab->dev, 492 AB8500_USB, AB8505_USB_LINE_STAT_REG, ®); 493 lsts = (reg >> 3) & 0x1F; 494 ret = ab8505_usb_link_status_update(ab, lsts); 495 } 496 497 return ret; 498 } 499 500 /* 501 * Disconnection Sequence: 502 * 1. Disconect Interrupt 503 * 2. Disable regulators 504 * 3. Disable AB clock 505 * 4. Disable the Phy 506 * 5. Link Status Interrupt 507 * 6. Disable Musb Clock 508 */ 509 static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data) 510 { 511 struct ab8500_usb *ab = (struct ab8500_usb *) data; 512 enum usb_phy_events event = UX500_MUSB_NONE; 513 514 /* Link status will not be updated till phy is disabled. */ 515 if (ab->mode == USB_HOST) { 516 ab->phy.otg->default_a = false; 517 ab->vbus_draw = 0; 518 atomic_notifier_call_chain(&ab->phy.notifier, 519 event, &ab->vbus_draw); 520 ab8500_usb_host_phy_dis(ab); 521 ab->mode = USB_IDLE; 522 } 523 524 if (ab->mode == USB_PERIPHERAL) { 525 atomic_notifier_call_chain(&ab->phy.notifier, 526 event, &ab->vbus_draw); 527 ab8500_usb_peri_phy_dis(ab); 528 atomic_notifier_call_chain(&ab->phy.notifier, 529 UX500_MUSB_CLEAN, &ab->vbus_draw); 530 ab->mode = USB_IDLE; 531 ab->phy.otg->default_a = false; 532 ab->vbus_draw = 0; 533 } 534 535 if (is_ab8500_2p0(ab->ab8500)) { 536 if (ab->mode == USB_DEDICATED_CHG) { 537 ab8500_usb_wd_linkstatus(ab, 538 AB8500_BIT_PHY_CTRL_DEVICE_EN); 539 abx500_mask_and_set_register_interruptible(ab->dev, 540 AB8500_USB, AB8500_USB_PHY_CTRL_REG, 541 AB8500_BIT_PHY_CTRL_DEVICE_EN, 0); 542 } 543 } 544 545 return IRQ_HANDLED; 546 } 547 548 static irqreturn_t ab8500_usb_link_status_irq(int irq, void *data) 549 { 550 struct ab8500_usb *ab = (struct ab8500_usb *) data; 551 552 abx500_usb_link_status_update(ab); 553 554 return IRQ_HANDLED; 555 } 556 557 static void ab8500_usb_phy_disable_work(struct work_struct *work) 558 { 559 struct ab8500_usb *ab = container_of(work, struct ab8500_usb, 560 phy_dis_work); 561 562 if (!ab->phy.otg->host) 563 ab8500_usb_host_phy_dis(ab); 564 565 if (!ab->phy.otg->gadget) 566 ab8500_usb_peri_phy_dis(ab); 567 } 568 569 static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA) 570 { 571 /* 572 * AB8500 V2 has eye diagram issues when drawing more than 100mA from 573 * VBUS. Set charging current to 100mA in case of standard host 574 */ 575 if (is_ab8500_2p0_or_earlier(ab->ab8500)) 576 if (mA > 100) 577 mA = 100; 578 579 return mA; 580 } 581 582 static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA) 583 { 584 struct ab8500_usb *ab; 585 586 if (!phy) 587 return -ENODEV; 588 589 ab = phy_to_ab(phy); 590 591 mA = ab8500_eyediagram_workaroud(ab, mA); 592 593 ab->vbus_draw = mA; 594 595 atomic_notifier_call_chain(&ab->phy.notifier, 596 UX500_MUSB_VBUS, &ab->vbus_draw); 597 598 return 0; 599 } 600 601 static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend) 602 { 603 /* TODO */ 604 return 0; 605 } 606 607 static int ab8500_usb_set_peripheral(struct usb_otg *otg, 608 struct usb_gadget *gadget) 609 { 610 struct ab8500_usb *ab; 611 612 if (!otg) 613 return -ENODEV; 614 615 ab = phy_to_ab(otg->phy); 616 617 /* Some drivers call this function in atomic context. 618 * Do not update ab8500 registers directly till this 619 * is fixed. 620 */ 621 622 if (!gadget) { 623 otg->gadget = NULL; 624 schedule_work(&ab->phy_dis_work); 625 } else { 626 otg->gadget = gadget; 627 otg->phy->state = OTG_STATE_B_IDLE; 628 } 629 630 return 0; 631 } 632 633 static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host) 634 { 635 struct ab8500_usb *ab; 636 637 if (!otg) 638 return -ENODEV; 639 640 ab = phy_to_ab(otg->phy); 641 642 /* Some drivers call this function in atomic context. 643 * Do not update ab8500 registers directly till this 644 * is fixed. 645 */ 646 647 if (!host) { 648 otg->host = NULL; 649 schedule_work(&ab->phy_dis_work); 650 } else { 651 otg->host = host; 652 } 653 654 return 0; 655 } 656 657 static int ab8500_usb_regulator_get(struct ab8500_usb *ab) 658 { 659 int err; 660 661 ab->v_ape = devm_regulator_get(ab->dev, "v-ape"); 662 if (IS_ERR(ab->v_ape)) { 663 dev_err(ab->dev, "Could not get v-ape supply\n"); 664 err = PTR_ERR(ab->v_ape); 665 return err; 666 } 667 668 ab->v_ulpi = devm_regulator_get(ab->dev, "vddulpivio18"); 669 if (IS_ERR(ab->v_ulpi)) { 670 dev_err(ab->dev, "Could not get vddulpivio18 supply\n"); 671 err = PTR_ERR(ab->v_ulpi); 672 return err; 673 } 674 675 ab->v_musb = devm_regulator_get(ab->dev, "musb_1v8"); 676 if (IS_ERR(ab->v_musb)) { 677 dev_err(ab->dev, "Could not get musb_1v8 supply\n"); 678 err = PTR_ERR(ab->v_musb); 679 return err; 680 } 681 682 return 0; 683 } 684 685 static int ab8500_usb_irq_setup(struct platform_device *pdev, 686 struct ab8500_usb *ab) 687 { 688 int err; 689 int irq; 690 691 irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS"); 692 if (irq < 0) { 693 dev_err(&pdev->dev, "Link status irq not found\n"); 694 return irq; 695 } 696 err = devm_request_threaded_irq(&pdev->dev, irq, NULL, 697 ab8500_usb_link_status_irq, 698 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-link-status", ab); 699 if (err < 0) { 700 dev_err(ab->dev, "request_irq failed for link status irq\n"); 701 return err; 702 } 703 704 irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F"); 705 if (irq < 0) { 706 dev_err(&pdev->dev, "ID fall irq not found\n"); 707 return irq; 708 } 709 err = devm_request_threaded_irq(&pdev->dev, irq, NULL, 710 ab8500_usb_disconnect_irq, 711 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-id-fall", ab); 712 if (err < 0) { 713 dev_err(ab->dev, "request_irq failed for ID fall irq\n"); 714 return err; 715 } 716 717 irq = platform_get_irq_byname(pdev, "VBUS_DET_F"); 718 if (irq < 0) { 719 dev_err(&pdev->dev, "VBUS fall irq not found\n"); 720 return irq; 721 } 722 err = devm_request_threaded_irq(&pdev->dev, irq, NULL, 723 ab8500_usb_disconnect_irq, 724 IRQF_NO_SUSPEND | IRQF_SHARED, "usb-vbus-fall", ab); 725 if (err < 0) { 726 dev_err(ab->dev, "request_irq failed for Vbus fall irq\n"); 727 return err; 728 } 729 730 return 0; 731 } 732 733 static int ab8500_usb_probe(struct platform_device *pdev) 734 { 735 struct ab8500_usb *ab; 736 struct ab8500 *ab8500; 737 struct usb_otg *otg; 738 int err; 739 int rev; 740 741 ab8500 = dev_get_drvdata(pdev->dev.parent); 742 rev = abx500_get_chip_id(&pdev->dev); 743 744 if (is_ab8500_1p1_or_earlier(ab8500)) { 745 dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev); 746 return -ENODEV; 747 } 748 749 ab = devm_kzalloc(&pdev->dev, sizeof(*ab), GFP_KERNEL); 750 if (!ab) 751 return -ENOMEM; 752 753 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); 754 if (!otg) 755 return -ENOMEM; 756 757 ab->dev = &pdev->dev; 758 ab->ab8500 = ab8500; 759 ab->phy.dev = ab->dev; 760 ab->phy.otg = otg; 761 ab->phy.label = "ab8500"; 762 ab->phy.set_suspend = ab8500_usb_set_suspend; 763 ab->phy.set_power = ab8500_usb_set_power; 764 ab->phy.state = OTG_STATE_UNDEFINED; 765 766 otg->phy = &ab->phy; 767 otg->set_host = ab8500_usb_set_host; 768 otg->set_peripheral = ab8500_usb_set_peripheral; 769 770 platform_set_drvdata(pdev, ab); 771 772 ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier); 773 774 /* all: Disable phy when called from set_host and set_peripheral */ 775 INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work); 776 777 err = ab8500_usb_regulator_get(ab); 778 if (err) 779 return err; 780 781 err = ab8500_usb_irq_setup(pdev, ab); 782 if (err < 0) 783 return err; 784 785 err = usb_add_phy(&ab->phy, USB_PHY_TYPE_USB2); 786 if (err) { 787 dev_err(&pdev->dev, "Can't register transceiver\n"); 788 return err; 789 } 790 791 /* Phy tuning values for AB8500 */ 792 if (!is_ab8500_2p0_or_earlier(ab->ab8500)) { 793 /* Enable the PBT/Bank 0x12 access */ 794 err = abx500_set_register_interruptible(ab->dev, 795 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x01); 796 if (err < 0) 797 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n", 798 err); 799 800 err = abx500_set_register_interruptible(ab->dev, 801 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 0xC8); 802 if (err < 0) 803 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n", 804 err); 805 806 err = abx500_set_register_interruptible(ab->dev, 807 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 0x00); 808 if (err < 0) 809 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n", 810 err); 811 812 err = abx500_set_register_interruptible(ab->dev, 813 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 0x78); 814 if (err < 0) 815 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n", 816 err); 817 818 /* Switch to normal mode/disable Bank 0x12 access */ 819 err = abx500_set_register_interruptible(ab->dev, 820 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 0x00); 821 if (err < 0) 822 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n", 823 err); 824 } 825 826 /* Phy tuning values for AB8505 */ 827 if (is_ab8505(ab->ab8500)) { 828 /* Enable the PBT/Bank 0x12 access */ 829 err = abx500_mask_and_set_register_interruptible(ab->dev, 830 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 831 0x01, 0x01); 832 if (err < 0) 833 dev_err(ab->dev, "Failed to enable bank12 access err=%d\n", 834 err); 835 836 err = abx500_mask_and_set_register_interruptible(ab->dev, 837 AB8500_DEBUG, AB8500_USB_PHY_TUNE1, 838 0xC8, 0xC8); 839 if (err < 0) 840 dev_err(ab->dev, "Failed to set PHY_TUNE1 register err=%d\n", 841 err); 842 843 err = abx500_mask_and_set_register_interruptible(ab->dev, 844 AB8500_DEBUG, AB8500_USB_PHY_TUNE2, 845 0x60, 0x60); 846 if (err < 0) 847 dev_err(ab->dev, "Failed to set PHY_TUNE2 register err=%d\n", 848 err); 849 850 err = abx500_mask_and_set_register_interruptible(ab->dev, 851 AB8500_DEBUG, AB8500_USB_PHY_TUNE3, 852 0xFC, 0x80); 853 854 if (err < 0) 855 dev_err(ab->dev, "Failed to set PHY_TUNE3 regester err=%d\n", 856 err); 857 858 /* Switch to normal mode/disable Bank 0x12 access */ 859 err = abx500_mask_and_set_register_interruptible(ab->dev, 860 AB8500_DEVELOPMENT, AB8500_BANK12_ACCESS, 861 0x00, 0x00); 862 if (err < 0) 863 dev_err(ab->dev, "Failed to switch bank12 access err=%d\n", 864 err); 865 } 866 867 /* Needed to enable ID detection. */ 868 ab8500_usb_wd_workaround(ab); 869 870 abx500_usb_link_status_update(ab); 871 872 dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev); 873 874 return 0; 875 } 876 877 static int ab8500_usb_remove(struct platform_device *pdev) 878 { 879 struct ab8500_usb *ab = platform_get_drvdata(pdev); 880 881 cancel_work_sync(&ab->phy_dis_work); 882 883 usb_remove_phy(&ab->phy); 884 885 if (ab->mode == USB_HOST) 886 ab8500_usb_host_phy_dis(ab); 887 else if (ab->mode == USB_PERIPHERAL) 888 ab8500_usb_peri_phy_dis(ab); 889 890 platform_set_drvdata(pdev, NULL); 891 892 return 0; 893 } 894 895 static struct platform_driver ab8500_usb_driver = { 896 .probe = ab8500_usb_probe, 897 .remove = ab8500_usb_remove, 898 .driver = { 899 .name = "ab8500-usb", 900 .owner = THIS_MODULE, 901 }, 902 }; 903 904 static int __init ab8500_usb_init(void) 905 { 906 return platform_driver_register(&ab8500_usb_driver); 907 } 908 subsys_initcall(ab8500_usb_init); 909 910 static void __exit ab8500_usb_exit(void) 911 { 912 platform_driver_unregister(&ab8500_usb_driver); 913 } 914 module_exit(ab8500_usb_exit); 915 916 MODULE_ALIAS("platform:ab8500_usb"); 917 MODULE_AUTHOR("ST-Ericsson AB"); 918 MODULE_DESCRIPTION("AB8500 usb transceiver driver"); 919 MODULE_LICENSE("GPL"); 920