1 // SPDX-License-Identifier: GPL-2.0-only
3 * Motorola CPCAP PMIC USB PHY driver
4 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
6 * Some parts based on earlier Motorola Linux kernel tree code in
7 * board-mapphone-usb.c and cpcap-usb-det.c:
8 * Copyright (C) 2007 - 2011 Motorola, Inc.
11 #include <linux/atomic.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
16 #include <linux/module.h>
18 #include <linux/of_platform.h>
19 #include <linux/iio/consumer.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/regmap.h>
23 #include <linux/slab.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/mfd/motorola-cpcap.h>
27 #include <linux/phy/omap_usb.h>
28 #include <linux/phy/phy.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/usb/musb.h>
32 /* CPCAP_REG_USBC1 register bits */
33 #define CPCAP_BIT_IDPULSE BIT(15)
34 #define CPCAP_BIT_ID100KPU BIT(14)
35 #define CPCAP_BIT_IDPUCNTRL BIT(13)
36 #define CPCAP_BIT_IDPU BIT(12)
37 #define CPCAP_BIT_IDPD BIT(11)
38 #define CPCAP_BIT_VBUSCHRGTMR3 BIT(10)
39 #define CPCAP_BIT_VBUSCHRGTMR2 BIT(9)
40 #define CPCAP_BIT_VBUSCHRGTMR1 BIT(8)
41 #define CPCAP_BIT_VBUSCHRGTMR0 BIT(7)
42 #define CPCAP_BIT_VBUSPU BIT(6)
43 #define CPCAP_BIT_VBUSPD BIT(5)
44 #define CPCAP_BIT_DMPD BIT(4)
45 #define CPCAP_BIT_DPPD BIT(3)
46 #define CPCAP_BIT_DM1K5PU BIT(2)
47 #define CPCAP_BIT_DP1K5PU BIT(1)
48 #define CPCAP_BIT_DP150KPU BIT(0)
50 /* CPCAP_REG_USBC2 register bits */
51 #define CPCAP_BIT_ZHSDRV1 BIT(15)
52 #define CPCAP_BIT_ZHSDRV0 BIT(14)
53 #define CPCAP_BIT_DPLLCLKREQ BIT(13)
54 #define CPCAP_BIT_SE0CONN BIT(12)
55 #define CPCAP_BIT_UARTTXTRI BIT(11)
56 #define CPCAP_BIT_UARTSWAP BIT(10)
57 #define CPCAP_BIT_UARTMUX1 BIT(9)
58 #define CPCAP_BIT_UARTMUX0 BIT(8)
59 #define CPCAP_BIT_ULPISTPLOW BIT(7)
60 #define CPCAP_BIT_TXENPOL BIT(6)
61 #define CPCAP_BIT_USBXCVREN BIT(5)
62 #define CPCAP_BIT_USBCNTRL BIT(4)
63 #define CPCAP_BIT_USBSUSPEND BIT(3)
64 #define CPCAP_BIT_EMUMODE2 BIT(2)
65 #define CPCAP_BIT_EMUMODE1 BIT(1)
66 #define CPCAP_BIT_EMUMODE0 BIT(0)
68 /* CPCAP_REG_USBC3 register bits */
69 #define CPCAP_BIT_SPARE_898_15 BIT(15)
70 #define CPCAP_BIT_IHSTX03 BIT(14)
71 #define CPCAP_BIT_IHSTX02 BIT(13)
72 #define CPCAP_BIT_IHSTX01 BIT(12)
73 #define CPCAP_BIT_IHSTX0 BIT(11)
74 #define CPCAP_BIT_IDPU_SPI BIT(10)
75 #define CPCAP_BIT_UNUSED_898_9 BIT(9)
76 #define CPCAP_BIT_VBUSSTBY_EN BIT(8)
77 #define CPCAP_BIT_VBUSEN_SPI BIT(7)
78 #define CPCAP_BIT_VBUSPU_SPI BIT(6)
79 #define CPCAP_BIT_VBUSPD_SPI BIT(5)
80 #define CPCAP_BIT_DMPD_SPI BIT(4)
81 #define CPCAP_BIT_DPPD_SPI BIT(3)
82 #define CPCAP_BIT_SUSPEND_SPI BIT(2)
83 #define CPCAP_BIT_PU_SPI BIT(1)
84 #define CPCAP_BIT_ULPI_SPI_SEL BIT(0)
86 struct cpcap_usb_ints_state {
107 enum cpcap_gpio_mode {
110 CPCAP_UNKNOWN_DISABLED, /* Seems to disable USB lines */
114 struct cpcap_phy_ddata {
118 struct delayed_work detect_work;
119 struct pinctrl *pins;
120 struct pinctrl_state *pins_ulpi;
121 struct pinctrl_state *pins_utmi;
122 struct pinctrl_state *pins_uart;
123 struct gpio_desc *gpio[2];
124 struct iio_channel *vbus;
125 struct iio_channel *id;
126 struct regulator *vusb;
128 unsigned int vbus_provider:1;
129 unsigned int docked:1;
132 static bool cpcap_usb_vbus_valid(struct cpcap_phy_ddata *ddata)
134 int error, value = 0;
136 error = iio_read_channel_processed(ddata->vbus, &value);
140 dev_err(ddata->dev, "error reading VBUS: %i\n", error);
145 static int cpcap_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
149 otg->state = OTG_STATE_UNDEFINED;
154 static int cpcap_usb_phy_set_peripheral(struct usb_otg *otg,
155 struct usb_gadget *gadget)
157 otg->gadget = gadget;
159 otg->state = OTG_STATE_UNDEFINED;
164 static const struct phy_ops ops = {
165 .owner = THIS_MODULE,
168 static int cpcap_phy_get_ints_state(struct cpcap_phy_ddata *ddata,
169 struct cpcap_usb_ints_state *s)
173 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
177 s->id_ground = val & BIT(15);
178 s->id_float = val & BIT(14);
179 s->vbusov = val & BIT(11);
181 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
185 s->vbusvld = val & BIT(3);
186 s->sessvld = val & BIT(2);
187 s->sessend = val & BIT(1);
188 s->se1 = val & BIT(0);
190 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
194 s->dm = val & BIT(1);
195 s->dp = val & BIT(0);
200 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata);
201 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata);
203 static void cpcap_usb_try_musb_mailbox(struct cpcap_phy_ddata *ddata,
204 enum musb_vbus_id_status status)
208 error = musb_mailbox(status);
212 dev_dbg(ddata->dev, "%s: musb_mailbox failed: %i\n",
216 static void cpcap_usb_detect(struct work_struct *work)
218 struct cpcap_phy_ddata *ddata;
219 struct cpcap_usb_ints_state s;
223 ddata = container_of(work, struct cpcap_phy_ddata, detect_work.work);
225 error = cpcap_phy_get_ints_state(ddata, &s);
229 vbus = cpcap_usb_vbus_valid(ddata);
231 /* We need to kick the VBUS as USB A-host */
232 if (s.id_ground && ddata->vbus_provider) {
233 dev_dbg(ddata->dev, "still in USB A-host mode, kicking VBUS\n");
235 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
237 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
238 CPCAP_BIT_VBUSSTBY_EN |
239 CPCAP_BIT_VBUSEN_SPI,
240 CPCAP_BIT_VBUSEN_SPI);
247 if (vbus && s.id_ground && ddata->docked) {
248 dev_dbg(ddata->dev, "still docked as A-host, signal ID down\n");
250 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
255 /* No VBUS needed with docks */
256 if (vbus && s.id_ground && !ddata->vbus_provider) {
257 dev_dbg(ddata->dev, "connected to a dock\n");
259 ddata->docked = true;
261 error = cpcap_usb_set_usb_mode(ddata);
265 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
268 * Force check state again after musb has reoriented,
269 * otherwise devices won't enumerate after loading PHY
272 schedule_delayed_work(&ddata->detect_work,
273 msecs_to_jiffies(1000));
278 if (s.id_ground && !ddata->docked) {
279 dev_dbg(ddata->dev, "id ground, USB host mode\n");
281 ddata->vbus_provider = true;
283 error = cpcap_usb_set_usb_mode(ddata);
287 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
289 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
290 CPCAP_BIT_VBUSSTBY_EN |
291 CPCAP_BIT_VBUSEN_SPI,
292 CPCAP_BIT_VBUSEN_SPI);
299 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
300 CPCAP_BIT_VBUSSTBY_EN |
301 CPCAP_BIT_VBUSEN_SPI, 0);
305 vbus = cpcap_usb_vbus_valid(ddata);
307 /* Otherwise assume we're connected to a USB host */
309 dev_dbg(ddata->dev, "connected to USB host\n");
310 error = cpcap_usb_set_usb_mode(ddata);
313 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_VALID);
318 ddata->vbus_provider = false;
319 ddata->docked = false;
320 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
322 /* Default to debug UART mode */
323 error = cpcap_usb_set_uart_mode(ddata);
327 dev_dbg(ddata->dev, "set UART mode\n");
332 dev_err(ddata->dev, "error setting cable state: %i\n", error);
335 static irqreturn_t cpcap_phy_irq_thread(int irq, void *data)
337 struct cpcap_phy_ddata *ddata = data;
339 if (!atomic_read(&ddata->active))
342 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
347 static int cpcap_usb_init_irq(struct platform_device *pdev,
348 struct cpcap_phy_ddata *ddata,
353 irq = platform_get_irq_byname(pdev, name);
357 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
358 cpcap_phy_irq_thread,
363 dev_err(ddata->dev, "could not get irq %s: %i\n",
372 static const char * const cpcap_phy_irqs[] = {
374 "id_ground", "id_float",
377 "se0conn", "vbusvld", "sessvld", "sessend", "se1",
383 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
384 struct cpcap_phy_ddata *ddata)
388 for (i = 0; i < ARRAY_SIZE(cpcap_phy_irqs); i++) {
389 error = cpcap_usb_init_irq(pdev, ddata, cpcap_phy_irqs[i]);
398 * Optional pins and modes. At least Motorola mapphone devices
399 * are using two GPIOs and dynamic pinctrl to multiplex PHY pins
400 * to UART, ULPI or UTMI mode.
403 static int cpcap_usb_gpio_set_mode(struct cpcap_phy_ddata *ddata,
404 enum cpcap_gpio_mode mode)
406 if (!ddata->gpio[0] || !ddata->gpio[1])
409 gpiod_set_value(ddata->gpio[0], mode & 1);
410 gpiod_set_value(ddata->gpio[1], mode >> 1);
415 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata)
419 /* Disable lines to prevent glitches from waking up mdm6600 */
420 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
424 if (ddata->pins_uart) {
425 error = pinctrl_select_state(ddata->pins, ddata->pins_uart);
430 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
436 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
437 0xffff, CPCAP_BIT_UARTMUX0 |
442 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff,
447 /* Enable UART mode */
448 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_DM_DP);
455 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
460 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata)
464 /* Disable lines to prevent glitches from waking up mdm6600 */
465 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
469 if (ddata->pins_utmi) {
470 error = pinctrl_select_state(ddata->pins, ddata->pins_utmi);
472 dev_err(ddata->dev, "could not set usb mode: %i\n",
479 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
480 CPCAP_BIT_VBUSPD, 0);
484 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
488 CPCAP_BIT_SUSPEND_SPI |
489 CPCAP_BIT_ULPI_SPI_SEL, 0);
493 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
495 CPCAP_BIT_USBXCVREN);
499 /* Enable USB mode */
500 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_OTG_DM_DP);
507 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
512 static int cpcap_usb_init_optional_pins(struct cpcap_phy_ddata *ddata)
514 ddata->pins = devm_pinctrl_get(ddata->dev);
515 if (IS_ERR(ddata->pins)) {
516 dev_info(ddata->dev, "default pins not configured: %ld\n",
517 PTR_ERR(ddata->pins));
523 ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi");
524 if (IS_ERR(ddata->pins_ulpi)) {
525 dev_info(ddata->dev, "ulpi pins not configured\n");
526 ddata->pins_ulpi = NULL;
529 ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi");
530 if (IS_ERR(ddata->pins_utmi)) {
531 dev_info(ddata->dev, "utmi pins not configured\n");
532 ddata->pins_utmi = NULL;
535 ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart");
536 if (IS_ERR(ddata->pins_uart)) {
537 dev_info(ddata->dev, "uart pins not configured\n");
538 ddata->pins_uart = NULL;
541 if (ddata->pins_uart)
542 return pinctrl_select_state(ddata->pins, ddata->pins_uart);
547 static void cpcap_usb_init_optional_gpios(struct cpcap_phy_ddata *ddata)
551 for (i = 0; i < 2; i++) {
552 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
554 if (IS_ERR(ddata->gpio[i])) {
555 dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
556 i, PTR_ERR(ddata->gpio[i]));
557 ddata->gpio[i] = NULL;
562 static int cpcap_usb_init_iio(struct cpcap_phy_ddata *ddata)
564 enum iio_chan_type type;
567 ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus");
568 if (IS_ERR(ddata->vbus)) {
569 error = PTR_ERR(ddata->vbus);
573 if (!ddata->vbus->indio_dev) {
578 error = iio_get_channel_type(ddata->vbus, &type);
582 if (type != IIO_VOLTAGE) {
590 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
597 static const struct of_device_id cpcap_usb_phy_id_table[] = {
599 .compatible = "motorola,cpcap-usb-phy",
602 .compatible = "motorola,mapphone-cpcap-usb-phy",
606 MODULE_DEVICE_TABLE(of, cpcap_usb_phy_id_table);
609 static int cpcap_usb_phy_probe(struct platform_device *pdev)
611 struct cpcap_phy_ddata *ddata;
612 struct phy *generic_phy;
613 struct phy_provider *phy_provider;
615 const struct of_device_id *of_id;
618 of_id = of_match_device(of_match_ptr(cpcap_usb_phy_id_table),
623 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
627 ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
631 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
635 ddata->dev = &pdev->dev;
636 ddata->phy.dev = ddata->dev;
637 ddata->phy.label = "cpcap_usb_phy";
638 ddata->phy.otg = otg;
639 ddata->phy.type = USB_PHY_TYPE_USB2;
640 otg->set_host = cpcap_usb_phy_set_host;
641 otg->set_peripheral = cpcap_usb_phy_set_peripheral;
642 otg->usb_phy = &ddata->phy;
643 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
644 platform_set_drvdata(pdev, ddata);
646 ddata->vusb = devm_regulator_get(&pdev->dev, "vusb");
647 if (IS_ERR(ddata->vusb))
648 return PTR_ERR(ddata->vusb);
650 error = regulator_enable(ddata->vusb);
654 generic_phy = devm_phy_create(ddata->dev, NULL, &ops);
655 if (IS_ERR(generic_phy)) {
656 error = PTR_ERR(generic_phy);
657 goto out_reg_disable;
660 phy_set_drvdata(generic_phy, ddata);
662 phy_provider = devm_of_phy_provider_register(ddata->dev,
663 of_phy_simple_xlate);
664 if (IS_ERR(phy_provider)) {
665 error = PTR_ERR(phy_provider);
666 goto out_reg_disable;
669 error = cpcap_usb_init_optional_pins(ddata);
671 goto out_reg_disable;
673 cpcap_usb_init_optional_gpios(ddata);
675 error = cpcap_usb_init_iio(ddata);
677 goto out_reg_disable;
679 error = cpcap_usb_init_interrupts(pdev, ddata);
681 goto out_reg_disable;
683 usb_add_phy_dev(&ddata->phy);
684 atomic_set(&ddata->active, 1);
685 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
690 regulator_disable(ddata->vusb);
695 static void cpcap_usb_phy_remove(struct platform_device *pdev)
697 struct cpcap_phy_ddata *ddata = platform_get_drvdata(pdev);
700 atomic_set(&ddata->active, 0);
701 error = cpcap_usb_set_uart_mode(ddata);
703 dev_err(ddata->dev, "could not set UART mode\n");
705 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
707 usb_remove_phy(&ddata->phy);
708 cancel_delayed_work_sync(&ddata->detect_work);
709 regulator_disable(ddata->vusb);
712 static struct platform_driver cpcap_usb_phy_driver = {
713 .probe = cpcap_usb_phy_probe,
714 .remove_new = cpcap_usb_phy_remove,
716 .name = "cpcap-usb-phy",
717 .of_match_table = of_match_ptr(cpcap_usb_phy_id_table),
721 module_platform_driver(cpcap_usb_phy_driver);
723 MODULE_ALIAS("platform:cpcap_usb");
724 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
725 MODULE_DESCRIPTION("CPCAP usb phy driver");
726 MODULE_LICENSE("GPL v2");