1 /* 2 * Motorola CPCAP PMIC USB PHY driver 3 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com> 4 * 5 * Some parts based on earlier Motorola Linux kernel tree code in 6 * board-mapphone-usb.c and cpcap-usb-det.c: 7 * Copyright (C) 2007 - 2011 Motorola, Inc. 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License as 11 * published by the Free Software Foundation version 2. 12 * 13 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 14 * kind, whether express or implied; without even the implied warranty 15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 */ 18 19 #include <linux/atomic.h> 20 #include <linux/clk.h> 21 #include <linux/delay.h> 22 #include <linux/err.h> 23 #include <linux/io.h> 24 #include <linux/module.h> 25 #include <linux/of.h> 26 #include <linux/of_platform.h> 27 #include <linux/iio/consumer.h> 28 #include <linux/pinctrl/consumer.h> 29 #include <linux/platform_device.h> 30 #include <linux/regmap.h> 31 #include <linux/slab.h> 32 33 #include <linux/gpio/consumer.h> 34 #include <linux/mfd/motorola-cpcap.h> 35 #include <linux/phy/omap_usb.h> 36 #include <linux/phy/phy.h> 37 #include <linux/regulator/consumer.h> 38 #include <linux/usb/musb.h> 39 40 /* CPCAP_REG_USBC1 register bits */ 41 #define CPCAP_BIT_IDPULSE BIT(15) 42 #define CPCAP_BIT_ID100KPU BIT(14) 43 #define CPCAP_BIT_IDPUCNTRL BIT(13) 44 #define CPCAP_BIT_IDPU BIT(12) 45 #define CPCAP_BIT_IDPD BIT(11) 46 #define CPCAP_BIT_VBUSCHRGTMR3 BIT(10) 47 #define CPCAP_BIT_VBUSCHRGTMR2 BIT(9) 48 #define CPCAP_BIT_VBUSCHRGTMR1 BIT(8) 49 #define CPCAP_BIT_VBUSCHRGTMR0 BIT(7) 50 #define CPCAP_BIT_VBUSPU BIT(6) 51 #define CPCAP_BIT_VBUSPD BIT(5) 52 #define CPCAP_BIT_DMPD BIT(4) 53 #define CPCAP_BIT_DPPD BIT(3) 54 #define CPCAP_BIT_DM1K5PU BIT(2) 55 #define CPCAP_BIT_DP1K5PU BIT(1) 56 #define CPCAP_BIT_DP150KPU BIT(0) 57 58 /* CPCAP_REG_USBC2 register bits */ 59 #define CPCAP_BIT_ZHSDRV1 BIT(15) 60 #define CPCAP_BIT_ZHSDRV0 BIT(14) 61 #define CPCAP_BIT_DPLLCLKREQ BIT(13) 62 #define CPCAP_BIT_SE0CONN BIT(12) 63 #define CPCAP_BIT_UARTTXTRI BIT(11) 64 #define CPCAP_BIT_UARTSWAP BIT(10) 65 #define CPCAP_BIT_UARTMUX1 BIT(9) 66 #define CPCAP_BIT_UARTMUX0 BIT(8) 67 #define CPCAP_BIT_ULPISTPLOW BIT(7) 68 #define CPCAP_BIT_TXENPOL BIT(6) 69 #define CPCAP_BIT_USBXCVREN BIT(5) 70 #define CPCAP_BIT_USBCNTRL BIT(4) 71 #define CPCAP_BIT_USBSUSPEND BIT(3) 72 #define CPCAP_BIT_EMUMODE2 BIT(2) 73 #define CPCAP_BIT_EMUMODE1 BIT(1) 74 #define CPCAP_BIT_EMUMODE0 BIT(0) 75 76 /* CPCAP_REG_USBC3 register bits */ 77 #define CPCAP_BIT_SPARE_898_15 BIT(15) 78 #define CPCAP_BIT_IHSTX03 BIT(14) 79 #define CPCAP_BIT_IHSTX02 BIT(13) 80 #define CPCAP_BIT_IHSTX01 BIT(12) 81 #define CPCAP_BIT_IHSTX0 BIT(11) 82 #define CPCAP_BIT_IDPU_SPI BIT(10) 83 #define CPCAP_BIT_UNUSED_898_9 BIT(9) 84 #define CPCAP_BIT_VBUSSTBY_EN BIT(8) 85 #define CPCAP_BIT_VBUSEN_SPI BIT(7) 86 #define CPCAP_BIT_VBUSPU_SPI BIT(6) 87 #define CPCAP_BIT_VBUSPD_SPI BIT(5) 88 #define CPCAP_BIT_DMPD_SPI BIT(4) 89 #define CPCAP_BIT_DPPD_SPI BIT(3) 90 #define CPCAP_BIT_SUSPEND_SPI BIT(2) 91 #define CPCAP_BIT_PU_SPI BIT(1) 92 #define CPCAP_BIT_ULPI_SPI_SEL BIT(0) 93 94 struct cpcap_usb_ints_state { 95 bool id_ground; 96 bool id_float; 97 bool chrg_det; 98 bool rvrs_chrg; 99 bool vbusov; 100 101 bool chrg_se1b; 102 bool se0conn; 103 bool rvrs_mode; 104 bool chrgcurr1; 105 bool vbusvld; 106 bool sessvld; 107 bool sessend; 108 bool se1; 109 110 bool battdetb; 111 bool dm; 112 bool dp; 113 }; 114 115 enum cpcap_gpio_mode { 116 CPCAP_DM_DP, 117 CPCAP_MDM_RX_TX, 118 CPCAP_UNKNOWN, 119 CPCAP_OTG_DM_DP, 120 }; 121 122 struct cpcap_phy_ddata { 123 struct regmap *reg; 124 struct device *dev; 125 struct clk *refclk; 126 struct usb_phy phy; 127 struct delayed_work detect_work; 128 struct pinctrl *pins; 129 struct pinctrl_state *pins_ulpi; 130 struct pinctrl_state *pins_utmi; 131 struct pinctrl_state *pins_uart; 132 struct gpio_desc *gpio[2]; 133 struct iio_channel *vbus; 134 struct iio_channel *id; 135 struct regulator *vusb; 136 atomic_t active; 137 }; 138 139 static bool cpcap_usb_vbus_valid(struct cpcap_phy_ddata *ddata) 140 { 141 int error, value = 0; 142 143 error = iio_read_channel_processed(ddata->vbus, &value); 144 if (error >= 0) 145 return value > 3900 ? true : false; 146 147 dev_err(ddata->dev, "error reading VBUS: %i\n", error); 148 149 return false; 150 } 151 152 static int cpcap_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host) 153 { 154 otg->host = host; 155 if (!host) 156 otg->state = OTG_STATE_UNDEFINED; 157 158 return 0; 159 } 160 161 static int cpcap_usb_phy_set_peripheral(struct usb_otg *otg, 162 struct usb_gadget *gadget) 163 { 164 otg->gadget = gadget; 165 if (!gadget) 166 otg->state = OTG_STATE_UNDEFINED; 167 168 return 0; 169 } 170 171 static const struct phy_ops ops = { 172 .owner = THIS_MODULE, 173 }; 174 175 static int cpcap_phy_get_ints_state(struct cpcap_phy_ddata *ddata, 176 struct cpcap_usb_ints_state *s) 177 { 178 int val, error; 179 180 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val); 181 if (error) 182 return error; 183 184 s->id_ground = val & BIT(15); 185 s->id_float = val & BIT(14); 186 s->vbusov = val & BIT(11); 187 188 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val); 189 if (error) 190 return error; 191 192 s->vbusvld = val & BIT(3); 193 s->sessvld = val & BIT(2); 194 s->sessend = val & BIT(1); 195 s->se1 = val & BIT(0); 196 197 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val); 198 if (error) 199 return error; 200 201 s->dm = val & BIT(1); 202 s->dp = val & BIT(0); 203 204 return 0; 205 } 206 207 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata); 208 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata); 209 210 static void cpcap_usb_detect(struct work_struct *work) 211 { 212 struct cpcap_phy_ddata *ddata; 213 struct cpcap_usb_ints_state s; 214 bool vbus = false; 215 int error; 216 217 ddata = container_of(work, struct cpcap_phy_ddata, detect_work.work); 218 219 error = cpcap_phy_get_ints_state(ddata, &s); 220 if (error) 221 return; 222 223 if (s.id_ground) { 224 dev_dbg(ddata->dev, "id ground, USB host mode\n"); 225 error = cpcap_usb_set_usb_mode(ddata); 226 if (error) 227 goto out_err; 228 229 error = musb_mailbox(MUSB_ID_GROUND); 230 if (error) 231 goto out_err; 232 233 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 234 CPCAP_BIT_VBUSSTBY_EN | 235 CPCAP_BIT_VBUSEN_SPI, 236 CPCAP_BIT_VBUSEN_SPI); 237 if (error) 238 goto out_err; 239 240 return; 241 } 242 243 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 244 CPCAP_BIT_VBUSSTBY_EN | 245 CPCAP_BIT_VBUSEN_SPI, 0); 246 if (error) 247 goto out_err; 248 249 vbus = cpcap_usb_vbus_valid(ddata); 250 251 if (vbus) { 252 /* Are we connected to a docking station with vbus? */ 253 if (s.id_ground) { 254 dev_dbg(ddata->dev, "connected to a dock\n"); 255 256 /* No VBUS needed with docks */ 257 error = cpcap_usb_set_usb_mode(ddata); 258 if (error) 259 goto out_err; 260 error = musb_mailbox(MUSB_ID_GROUND); 261 if (error) 262 goto out_err; 263 264 return; 265 } 266 267 /* Otherwise assume we're connected to a USB host */ 268 dev_dbg(ddata->dev, "connected to USB host\n"); 269 error = cpcap_usb_set_usb_mode(ddata); 270 if (error) 271 goto out_err; 272 error = musb_mailbox(MUSB_VBUS_VALID); 273 if (error) 274 goto out_err; 275 276 return; 277 } 278 279 /* Default to debug UART mode */ 280 error = cpcap_usb_set_uart_mode(ddata); 281 if (error) 282 goto out_err; 283 284 error = musb_mailbox(MUSB_VBUS_OFF); 285 if (error) 286 goto out_err; 287 288 dev_dbg(ddata->dev, "set UART mode\n"); 289 290 return; 291 292 out_err: 293 dev_err(ddata->dev, "error setting cable state: %i\n", error); 294 } 295 296 static irqreturn_t cpcap_phy_irq_thread(int irq, void *data) 297 { 298 struct cpcap_phy_ddata *ddata = data; 299 300 if (!atomic_read(&ddata->active)) 301 return IRQ_NONE; 302 303 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1)); 304 305 return IRQ_HANDLED; 306 } 307 308 static int cpcap_usb_init_irq(struct platform_device *pdev, 309 struct cpcap_phy_ddata *ddata, 310 const char *name) 311 { 312 int irq, error; 313 314 irq = platform_get_irq_byname(pdev, name); 315 if (irq < 0) 316 return -ENODEV; 317 318 error = devm_request_threaded_irq(ddata->dev, irq, NULL, 319 cpcap_phy_irq_thread, 320 IRQF_SHARED, 321 name, ddata); 322 if (error) { 323 dev_err(ddata->dev, "could not get irq %s: %i\n", 324 name, error); 325 326 return error; 327 } 328 329 return 0; 330 } 331 332 static const char * const cpcap_phy_irqs[] = { 333 /* REG_INT_0 */ 334 "id_ground", "id_float", 335 336 /* REG_INT1 */ 337 "se0conn", "vbusvld", "sessvld", "sessend", "se1", 338 339 /* REG_INT_3 */ 340 "dm", "dp", 341 }; 342 343 static int cpcap_usb_init_interrupts(struct platform_device *pdev, 344 struct cpcap_phy_ddata *ddata) 345 { 346 int i, error; 347 348 for (i = 0; i < ARRAY_SIZE(cpcap_phy_irqs); i++) { 349 error = cpcap_usb_init_irq(pdev, ddata, cpcap_phy_irqs[i]); 350 if (error) 351 return error; 352 } 353 354 return 0; 355 } 356 357 /* 358 * Optional pins and modes. At least Motorola mapphone devices 359 * are using two GPIOs and dynamic pinctrl to multiplex PHY pins 360 * to UART, ULPI or UTMI mode. 361 */ 362 363 static int cpcap_usb_gpio_set_mode(struct cpcap_phy_ddata *ddata, 364 enum cpcap_gpio_mode mode) 365 { 366 if (!ddata->gpio[0] || !ddata->gpio[1]) 367 return 0; 368 369 gpiod_set_value(ddata->gpio[0], mode & 1); 370 gpiod_set_value(ddata->gpio[1], mode >> 1); 371 372 return 0; 373 } 374 375 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata) 376 { 377 int error; 378 379 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_DM_DP); 380 if (error) 381 goto out_err; 382 383 if (ddata->pins_uart) { 384 error = pinctrl_select_state(ddata->pins, ddata->pins_uart); 385 if (error) 386 goto out_err; 387 } 388 389 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1, 390 CPCAP_BIT_VBUSPD, 391 CPCAP_BIT_VBUSPD); 392 if (error) 393 goto out_err; 394 395 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, 396 0xffff, CPCAP_BIT_UARTMUX0 | 397 CPCAP_BIT_EMUMODE0); 398 if (error) 399 goto out_err; 400 401 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff, 402 CPCAP_BIT_IDPU_SPI); 403 if (error) 404 goto out_err; 405 406 return 0; 407 408 out_err: 409 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 410 411 return error; 412 } 413 414 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata) 415 { 416 int error; 417 418 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_OTG_DM_DP); 419 if (error) 420 return error; 421 422 if (ddata->pins_utmi) { 423 error = pinctrl_select_state(ddata->pins, ddata->pins_utmi); 424 if (error) { 425 dev_err(ddata->dev, "could not set usb mode: %i\n", 426 error); 427 428 return error; 429 } 430 } 431 432 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1, 433 CPCAP_BIT_VBUSPD, 0); 434 if (error) 435 goto out_err; 436 437 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, 438 CPCAP_BIT_USBXCVREN, 439 CPCAP_BIT_USBXCVREN); 440 if (error) 441 goto out_err; 442 443 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 444 CPCAP_BIT_PU_SPI | 445 CPCAP_BIT_DMPD_SPI | 446 CPCAP_BIT_DPPD_SPI | 447 CPCAP_BIT_SUSPEND_SPI | 448 CPCAP_BIT_ULPI_SPI_SEL, 0); 449 if (error) 450 goto out_err; 451 452 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, 453 CPCAP_BIT_USBXCVREN, 454 CPCAP_BIT_USBXCVREN); 455 if (error) 456 goto out_err; 457 458 return 0; 459 460 out_err: 461 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); 462 463 return error; 464 } 465 466 static int cpcap_usb_init_optional_pins(struct cpcap_phy_ddata *ddata) 467 { 468 ddata->pins = devm_pinctrl_get(ddata->dev); 469 if (IS_ERR(ddata->pins)) { 470 dev_info(ddata->dev, "default pins not configured: %ld\n", 471 PTR_ERR(ddata->pins)); 472 ddata->pins = NULL; 473 474 return 0; 475 } 476 477 ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi"); 478 if (IS_ERR(ddata->pins_ulpi)) { 479 dev_info(ddata->dev, "ulpi pins not configured\n"); 480 ddata->pins_ulpi = NULL; 481 } 482 483 ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi"); 484 if (IS_ERR(ddata->pins_utmi)) { 485 dev_info(ddata->dev, "utmi pins not configured\n"); 486 ddata->pins_utmi = NULL; 487 } 488 489 ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart"); 490 if (IS_ERR(ddata->pins_uart)) { 491 dev_info(ddata->dev, "uart pins not configured\n"); 492 ddata->pins_uart = NULL; 493 } 494 495 if (ddata->pins_uart) 496 return pinctrl_select_state(ddata->pins, ddata->pins_uart); 497 498 return 0; 499 } 500 501 static void cpcap_usb_init_optional_gpios(struct cpcap_phy_ddata *ddata) 502 { 503 int i; 504 505 for (i = 0; i < 2; i++) { 506 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", 507 i, GPIOD_OUT_HIGH); 508 if (IS_ERR(ddata->gpio[i])) { 509 dev_info(ddata->dev, "no mode change GPIO%i: %li\n", 510 i, PTR_ERR(ddata->gpio[i])); 511 ddata->gpio[i] = NULL; 512 } 513 } 514 } 515 516 static int cpcap_usb_init_iio(struct cpcap_phy_ddata *ddata) 517 { 518 enum iio_chan_type type; 519 int error; 520 521 ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus"); 522 if (IS_ERR(ddata->vbus)) { 523 error = PTR_ERR(ddata->vbus); 524 goto out_err; 525 } 526 527 if (!ddata->vbus->indio_dev) { 528 error = -ENXIO; 529 goto out_err; 530 } 531 532 error = iio_get_channel_type(ddata->vbus, &type); 533 if (error < 0) 534 goto out_err; 535 536 if (type != IIO_VOLTAGE) { 537 error = -EINVAL; 538 goto out_err; 539 } 540 541 return 0; 542 543 out_err: 544 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", 545 error); 546 547 return error; 548 } 549 550 #ifdef CONFIG_OF 551 static const struct of_device_id cpcap_usb_phy_id_table[] = { 552 { 553 .compatible = "motorola,cpcap-usb-phy", 554 }, 555 { 556 .compatible = "motorola,mapphone-cpcap-usb-phy", 557 }, 558 {}, 559 }; 560 MODULE_DEVICE_TABLE(of, cpcap_usb_phy_id_table); 561 #endif 562 563 static int cpcap_usb_phy_probe(struct platform_device *pdev) 564 { 565 struct cpcap_phy_ddata *ddata; 566 struct phy *generic_phy; 567 struct phy_provider *phy_provider; 568 struct usb_otg *otg; 569 const struct of_device_id *of_id; 570 int error; 571 572 of_id = of_match_device(of_match_ptr(cpcap_usb_phy_id_table), 573 &pdev->dev); 574 if (!of_id) 575 return -EINVAL; 576 577 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 578 if (!ddata) 579 return -ENOMEM; 580 581 ddata->reg = dev_get_regmap(pdev->dev.parent, NULL); 582 if (!ddata->reg) 583 return -ENODEV; 584 585 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); 586 if (!otg) 587 return -ENOMEM; 588 589 ddata->dev = &pdev->dev; 590 ddata->phy.dev = ddata->dev; 591 ddata->phy.label = "cpcap_usb_phy"; 592 ddata->phy.otg = otg; 593 ddata->phy.type = USB_PHY_TYPE_USB2; 594 otg->set_host = cpcap_usb_phy_set_host; 595 otg->set_peripheral = cpcap_usb_phy_set_peripheral; 596 otg->usb_phy = &ddata->phy; 597 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); 598 platform_set_drvdata(pdev, ddata); 599 600 ddata->vusb = devm_regulator_get(&pdev->dev, "vusb"); 601 if (IS_ERR(ddata->vusb)) 602 return PTR_ERR(ddata->vusb); 603 604 error = regulator_enable(ddata->vusb); 605 if (error) 606 return error; 607 608 generic_phy = devm_phy_create(ddata->dev, NULL, &ops); 609 if (IS_ERR(generic_phy)) { 610 error = PTR_ERR(generic_phy); 611 return PTR_ERR(generic_phy); 612 } 613 614 phy_set_drvdata(generic_phy, ddata); 615 616 phy_provider = devm_of_phy_provider_register(ddata->dev, 617 of_phy_simple_xlate); 618 if (IS_ERR(phy_provider)) 619 return PTR_ERR(phy_provider); 620 621 error = cpcap_usb_init_optional_pins(ddata); 622 if (error) 623 return error; 624 625 cpcap_usb_init_optional_gpios(ddata); 626 627 error = cpcap_usb_init_iio(ddata); 628 if (error) 629 return error; 630 631 error = cpcap_usb_init_interrupts(pdev, ddata); 632 if (error) 633 return error; 634 635 usb_add_phy_dev(&ddata->phy); 636 atomic_set(&ddata->active, 1); 637 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1)); 638 639 return 0; 640 } 641 642 static int cpcap_usb_phy_remove(struct platform_device *pdev) 643 { 644 struct cpcap_phy_ddata *ddata = platform_get_drvdata(pdev); 645 int error; 646 647 atomic_set(&ddata->active, 0); 648 error = cpcap_usb_set_uart_mode(ddata); 649 if (error) 650 dev_err(ddata->dev, "could not set UART mode\n"); 651 652 error = musb_mailbox(MUSB_VBUS_OFF); 653 if (error) 654 dev_err(ddata->dev, "could not set mailbox\n"); 655 656 usb_remove_phy(&ddata->phy); 657 cancel_delayed_work_sync(&ddata->detect_work); 658 clk_unprepare(ddata->refclk); 659 regulator_disable(ddata->vusb); 660 661 return 0; 662 } 663 664 static struct platform_driver cpcap_usb_phy_driver = { 665 .probe = cpcap_usb_phy_probe, 666 .remove = cpcap_usb_phy_remove, 667 .driver = { 668 .name = "cpcap-usb-phy", 669 .of_match_table = of_match_ptr(cpcap_usb_phy_id_table), 670 }, 671 }; 672 673 module_platform_driver(cpcap_usb_phy_driver); 674 675 MODULE_ALIAS("platform:cpcap_usb"); 676 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); 677 MODULE_DESCRIPTION("CPCAP usb phy driver"); 678 MODULE_LICENSE("GPL v2"); 679