Lines Matching +full:usb +full:- +full:vbus

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Motorola CPCAP PMIC USB PHY driver
7 * board-mapphone-usb.c and cpcap-usb-det.c:
8 * Copyright (C) 2007 - 2011 Motorola, Inc.
26 #include <linux/mfd/motorola-cpcap.h>
30 #include <linux/usb/musb.h>
110 CPCAP_UNKNOWN_DISABLED, /* Seems to disable USB lines */
124 struct iio_channel *vbus; member
136 error = iio_read_channel_processed(ddata->vbus, &value); in cpcap_usb_vbus_valid()
140 dev_err(ddata->dev, "error reading VBUS: %i\n", error); in cpcap_usb_vbus_valid()
147 otg->host = host; in cpcap_usb_phy_set_host()
149 otg->state = OTG_STATE_UNDEFINED; in cpcap_usb_phy_set_host()
157 otg->gadget = gadget; in cpcap_usb_phy_set_peripheral()
159 otg->state = OTG_STATE_UNDEFINED; in cpcap_usb_phy_set_peripheral()
173 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val); in cpcap_phy_get_ints_state()
177 s->id_ground = val & BIT(15); in cpcap_phy_get_ints_state()
178 s->id_float = val & BIT(14); in cpcap_phy_get_ints_state()
179 s->vbusov = val & BIT(11); in cpcap_phy_get_ints_state()
181 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val); in cpcap_phy_get_ints_state()
185 s->vbusvld = val & BIT(3); in cpcap_phy_get_ints_state()
186 s->sessvld = val & BIT(2); in cpcap_phy_get_ints_state()
187 s->sessend = val & BIT(1); in cpcap_phy_get_ints_state()
188 s->se1 = val & BIT(0); in cpcap_phy_get_ints_state()
190 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val); in cpcap_phy_get_ints_state()
194 s->dm = val & BIT(1); in cpcap_phy_get_ints_state()
195 s->dp = val & BIT(0); in cpcap_phy_get_ints_state()
212 dev_dbg(ddata->dev, "%s: musb_mailbox failed: %i\n", in cpcap_usb_try_musb_mailbox()
220 bool vbus = false; in cpcap_usb_detect() local
229 vbus = cpcap_usb_vbus_valid(ddata); in cpcap_usb_detect()
231 /* We need to kick the VBUS as USB A-host */ in cpcap_usb_detect()
232 if (s.id_ground && ddata->vbus_provider) { in cpcap_usb_detect()
233 dev_dbg(ddata->dev, "still in USB A-host mode, kicking VBUS\n"); in cpcap_usb_detect()
237 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, in cpcap_usb_detect()
247 if (vbus && s.id_ground && ddata->docked) { in cpcap_usb_detect()
248 dev_dbg(ddata->dev, "still docked as A-host, signal ID down\n"); in cpcap_usb_detect()
255 /* No VBUS needed with docks */ in cpcap_usb_detect()
256 if (vbus && s.id_ground && !ddata->vbus_provider) { in cpcap_usb_detect()
257 dev_dbg(ddata->dev, "connected to a dock\n"); in cpcap_usb_detect()
259 ddata->docked = true; in cpcap_usb_detect()
272 schedule_delayed_work(&ddata->detect_work, in cpcap_usb_detect()
278 if (s.id_ground && !ddata->docked) { in cpcap_usb_detect()
279 dev_dbg(ddata->dev, "id ground, USB host mode\n"); in cpcap_usb_detect()
281 ddata->vbus_provider = true; in cpcap_usb_detect()
289 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, in cpcap_usb_detect()
299 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, in cpcap_usb_detect()
305 vbus = cpcap_usb_vbus_valid(ddata); in cpcap_usb_detect()
307 /* Otherwise assume we're connected to a USB host */ in cpcap_usb_detect()
308 if (vbus) { in cpcap_usb_detect()
309 dev_dbg(ddata->dev, "connected to USB host\n"); in cpcap_usb_detect()
318 ddata->vbus_provider = false; in cpcap_usb_detect()
319 ddata->docked = false; in cpcap_usb_detect()
327 dev_dbg(ddata->dev, "set UART mode\n"); in cpcap_usb_detect()
332 dev_err(ddata->dev, "error setting cable state: %i\n", error); in cpcap_usb_detect()
339 if (!atomic_read(&ddata->active)) in cpcap_phy_irq_thread()
342 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1)); in cpcap_phy_irq_thread()
355 return -ENODEV; in cpcap_usb_init_irq()
357 error = devm_request_threaded_irq(ddata->dev, irq, NULL, in cpcap_usb_init_irq()
363 dev_err(ddata->dev, "could not get irq %s: %i\n", in cpcap_usb_init_irq()
406 if (!ddata->gpio[0] || !ddata->gpio[1]) in cpcap_usb_gpio_set_mode()
409 gpiod_set_value(ddata->gpio[0], mode & 1); in cpcap_usb_gpio_set_mode()
410 gpiod_set_value(ddata->gpio[1], mode >> 1); in cpcap_usb_gpio_set_mode()
424 if (ddata->pins_uart) { in cpcap_usb_set_uart_mode()
425 error = pinctrl_select_state(ddata->pins, ddata->pins_uart); in cpcap_usb_set_uart_mode()
430 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1, in cpcap_usb_set_uart_mode()
436 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, in cpcap_usb_set_uart_mode()
442 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff, in cpcap_usb_set_uart_mode()
455 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); in cpcap_usb_set_uart_mode()
469 if (ddata->pins_utmi) { in cpcap_usb_set_usb_mode()
470 error = pinctrl_select_state(ddata->pins, ddata->pins_utmi); in cpcap_usb_set_usb_mode()
472 dev_err(ddata->dev, "could not set usb mode: %i\n", in cpcap_usb_set_usb_mode()
479 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1, in cpcap_usb_set_usb_mode()
484 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, in cpcap_usb_set_usb_mode()
493 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2, in cpcap_usb_set_usb_mode()
499 /* Enable USB mode */ in cpcap_usb_set_usb_mode()
507 dev_err(ddata->dev, "%s failed with %i\n", __func__, error); in cpcap_usb_set_usb_mode()
514 ddata->pins = devm_pinctrl_get(ddata->dev); in cpcap_usb_init_optional_pins()
515 if (IS_ERR(ddata->pins)) { in cpcap_usb_init_optional_pins()
516 dev_info(ddata->dev, "default pins not configured: %ld\n", in cpcap_usb_init_optional_pins()
517 PTR_ERR(ddata->pins)); in cpcap_usb_init_optional_pins()
518 ddata->pins = NULL; in cpcap_usb_init_optional_pins()
523 ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi"); in cpcap_usb_init_optional_pins()
524 if (IS_ERR(ddata->pins_ulpi)) { in cpcap_usb_init_optional_pins()
525 dev_info(ddata->dev, "ulpi pins not configured\n"); in cpcap_usb_init_optional_pins()
526 ddata->pins_ulpi = NULL; in cpcap_usb_init_optional_pins()
529 ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi"); in cpcap_usb_init_optional_pins()
530 if (IS_ERR(ddata->pins_utmi)) { in cpcap_usb_init_optional_pins()
531 dev_info(ddata->dev, "utmi pins not configured\n"); in cpcap_usb_init_optional_pins()
532 ddata->pins_utmi = NULL; in cpcap_usb_init_optional_pins()
535 ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart"); in cpcap_usb_init_optional_pins()
536 if (IS_ERR(ddata->pins_uart)) { in cpcap_usb_init_optional_pins()
537 dev_info(ddata->dev, "uart pins not configured\n"); in cpcap_usb_init_optional_pins()
538 ddata->pins_uart = NULL; in cpcap_usb_init_optional_pins()
541 if (ddata->pins_uart) in cpcap_usb_init_optional_pins()
542 return pinctrl_select_state(ddata->pins, ddata->pins_uart); in cpcap_usb_init_optional_pins()
552 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode", in cpcap_usb_init_optional_gpios()
554 if (IS_ERR(ddata->gpio[i])) { in cpcap_usb_init_optional_gpios()
555 dev_info(ddata->dev, "no mode change GPIO%i: %li\n", in cpcap_usb_init_optional_gpios()
556 i, PTR_ERR(ddata->gpio[i])); in cpcap_usb_init_optional_gpios()
557 ddata->gpio[i] = NULL; in cpcap_usb_init_optional_gpios()
567 ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus"); in cpcap_usb_init_iio()
568 if (IS_ERR(ddata->vbus)) { in cpcap_usb_init_iio()
569 error = PTR_ERR(ddata->vbus); in cpcap_usb_init_iio()
573 if (!ddata->vbus->indio_dev) { in cpcap_usb_init_iio()
574 error = -ENXIO; in cpcap_usb_init_iio()
578 error = iio_get_channel_type(ddata->vbus, &type); in cpcap_usb_init_iio()
583 error = -EINVAL; in cpcap_usb_init_iio()
590 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n", in cpcap_usb_init_iio()
599 .compatible = "motorola,cpcap-usb-phy",
602 .compatible = "motorola,mapphone-cpcap-usb-phy",
619 &pdev->dev); in cpcap_usb_phy_probe()
621 return -EINVAL; in cpcap_usb_phy_probe()
623 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); in cpcap_usb_phy_probe()
625 return -ENOMEM; in cpcap_usb_phy_probe()
627 ddata->reg = dev_get_regmap(pdev->dev.parent, NULL); in cpcap_usb_phy_probe()
628 if (!ddata->reg) in cpcap_usb_phy_probe()
629 return -ENODEV; in cpcap_usb_phy_probe()
631 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL); in cpcap_usb_phy_probe()
633 return -ENOMEM; in cpcap_usb_phy_probe()
635 ddata->dev = &pdev->dev; in cpcap_usb_phy_probe()
636 ddata->phy.dev = ddata->dev; in cpcap_usb_phy_probe()
637 ddata->phy.label = "cpcap_usb_phy"; in cpcap_usb_phy_probe()
638 ddata->phy.otg = otg; in cpcap_usb_phy_probe()
639 ddata->phy.type = USB_PHY_TYPE_USB2; in cpcap_usb_phy_probe()
640 otg->set_host = cpcap_usb_phy_set_host; in cpcap_usb_phy_probe()
641 otg->set_peripheral = cpcap_usb_phy_set_peripheral; in cpcap_usb_phy_probe()
642 otg->usb_phy = &ddata->phy; in cpcap_usb_phy_probe()
643 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect); in cpcap_usb_phy_probe()
646 ddata->vusb = devm_regulator_get(&pdev->dev, "vusb"); in cpcap_usb_phy_probe()
647 if (IS_ERR(ddata->vusb)) in cpcap_usb_phy_probe()
648 return PTR_ERR(ddata->vusb); in cpcap_usb_phy_probe()
650 error = regulator_enable(ddata->vusb); in cpcap_usb_phy_probe()
654 generic_phy = devm_phy_create(ddata->dev, NULL, &ops); in cpcap_usb_phy_probe()
662 phy_provider = devm_of_phy_provider_register(ddata->dev, in cpcap_usb_phy_probe()
683 usb_add_phy_dev(&ddata->phy); in cpcap_usb_phy_probe()
684 atomic_set(&ddata->active, 1); in cpcap_usb_phy_probe()
685 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1)); in cpcap_usb_phy_probe()
690 regulator_disable(ddata->vusb); in cpcap_usb_phy_probe()
700 atomic_set(&ddata->active, 0); in cpcap_usb_phy_remove()
703 dev_err(ddata->dev, "could not set UART mode\n"); in cpcap_usb_phy_remove()
707 usb_remove_phy(&ddata->phy); in cpcap_usb_phy_remove()
708 cancel_delayed_work_sync(&ddata->detect_work); in cpcap_usb_phy_remove()
709 regulator_disable(ddata->vusb); in cpcap_usb_phy_remove()
716 .name = "cpcap-usb-phy",
725 MODULE_DESCRIPTION("CPCAP usb phy driver");