2 * Motorola CPCAP PMIC USB PHY driver
3 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
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.
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.
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.
19 #include <linux/atomic.h>
20 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/err.h>
24 #include <linux/module.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>
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>
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)
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)
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)
94 struct cpcap_usb_ints_state {
115 enum cpcap_gpio_mode {
118 CPCAP_UNKNOWN_DISABLED, /* Seems to disable USB lines */
122 struct cpcap_phy_ddata {
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;
139 static bool cpcap_usb_vbus_valid(struct cpcap_phy_ddata *ddata)
141 int error, value = 0;
143 error = iio_read_channel_processed(ddata->vbus, &value);
145 return value > 3900 ? true : false;
147 dev_err(ddata->dev, "error reading VBUS: %i\n", error);
152 static int cpcap_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
156 otg->state = OTG_STATE_UNDEFINED;
161 static int cpcap_usb_phy_set_peripheral(struct usb_otg *otg,
162 struct usb_gadget *gadget)
164 otg->gadget = gadget;
166 otg->state = OTG_STATE_UNDEFINED;
171 static const struct phy_ops ops = {
172 .owner = THIS_MODULE,
175 static int cpcap_phy_get_ints_state(struct cpcap_phy_ddata *ddata,
176 struct cpcap_usb_ints_state *s)
180 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
184 s->id_ground = val & BIT(15);
185 s->id_float = val & BIT(14);
186 s->vbusov = val & BIT(11);
188 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
192 s->vbusvld = val & BIT(3);
193 s->sessvld = val & BIT(2);
194 s->sessend = val & BIT(1);
195 s->se1 = val & BIT(0);
197 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
201 s->dm = val & BIT(1);
202 s->dp = val & BIT(0);
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);
210 static void cpcap_usb_try_musb_mailbox(struct cpcap_phy_ddata *ddata,
211 enum musb_vbus_id_status status)
215 error = musb_mailbox(status);
219 dev_dbg(ddata->dev, "%s: musb_mailbox failed: %i\n",
223 static void cpcap_usb_detect(struct work_struct *work)
225 struct cpcap_phy_ddata *ddata;
226 struct cpcap_usb_ints_state s;
230 ddata = container_of(work, struct cpcap_phy_ddata, detect_work.work);
232 error = cpcap_phy_get_ints_state(ddata, &s);
237 dev_dbg(ddata->dev, "id ground, USB host mode\n");
238 error = cpcap_usb_set_usb_mode(ddata);
242 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
244 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
245 CPCAP_BIT_VBUSSTBY_EN,
246 CPCAP_BIT_VBUSSTBY_EN);
253 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
254 CPCAP_BIT_VBUSSTBY_EN, 0);
258 vbus = cpcap_usb_vbus_valid(ddata);
261 /* Are we connected to a docking station with vbus? */
263 dev_dbg(ddata->dev, "connected to a dock\n");
265 /* No VBUS needed with docks */
266 error = cpcap_usb_set_usb_mode(ddata);
269 cpcap_usb_try_musb_mailbox(ddata, MUSB_ID_GROUND);
274 /* Otherwise assume we're connected to a USB host */
275 dev_dbg(ddata->dev, "connected to USB host\n");
276 error = cpcap_usb_set_usb_mode(ddata);
279 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_VALID);
284 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
286 /* Default to debug UART mode */
287 error = cpcap_usb_set_uart_mode(ddata);
291 dev_dbg(ddata->dev, "set UART mode\n");
296 dev_err(ddata->dev, "error setting cable state: %i\n", error);
299 static irqreturn_t cpcap_phy_irq_thread(int irq, void *data)
301 struct cpcap_phy_ddata *ddata = data;
303 if (!atomic_read(&ddata->active))
306 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
311 static int cpcap_usb_init_irq(struct platform_device *pdev,
312 struct cpcap_phy_ddata *ddata,
317 irq = platform_get_irq_byname(pdev, name);
321 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
322 cpcap_phy_irq_thread,
326 dev_err(ddata->dev, "could not get irq %s: %i\n",
335 static const char * const cpcap_phy_irqs[] = {
337 "id_ground", "id_float",
340 "se0conn", "vbusvld", "sessvld", "sessend", "se1",
346 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
347 struct cpcap_phy_ddata *ddata)
351 for (i = 0; i < ARRAY_SIZE(cpcap_phy_irqs); i++) {
352 error = cpcap_usb_init_irq(pdev, ddata, cpcap_phy_irqs[i]);
361 * Optional pins and modes. At least Motorola mapphone devices
362 * are using two GPIOs and dynamic pinctrl to multiplex PHY pins
363 * to UART, ULPI or UTMI mode.
366 static int cpcap_usb_gpio_set_mode(struct cpcap_phy_ddata *ddata,
367 enum cpcap_gpio_mode mode)
369 if (!ddata->gpio[0] || !ddata->gpio[1])
372 gpiod_set_value(ddata->gpio[0], mode & 1);
373 gpiod_set_value(ddata->gpio[1], mode >> 1);
378 static int cpcap_usb_set_uart_mode(struct cpcap_phy_ddata *ddata)
382 /* Disable lines to prevent glitches from waking up mdm6600 */
383 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
387 if (ddata->pins_uart) {
388 error = pinctrl_select_state(ddata->pins, ddata->pins_uart);
393 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
399 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
400 0xffff, CPCAP_BIT_UARTMUX0 |
405 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3, 0x7fff,
410 /* Enable UART mode */
411 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_DM_DP);
418 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
423 static int cpcap_usb_set_usb_mode(struct cpcap_phy_ddata *ddata)
427 /* Disable lines to prevent glitches from waking up mdm6600 */
428 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_UNKNOWN_DISABLED);
432 if (ddata->pins_utmi) {
433 error = pinctrl_select_state(ddata->pins, ddata->pins_utmi);
435 dev_err(ddata->dev, "could not set usb mode: %i\n",
442 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC1,
443 CPCAP_BIT_VBUSPD, 0);
447 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
449 CPCAP_BIT_USBXCVREN);
453 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC3,
457 CPCAP_BIT_SUSPEND_SPI |
458 CPCAP_BIT_ULPI_SPI_SEL, 0);
462 error = regmap_update_bits(ddata->reg, CPCAP_REG_USBC2,
464 CPCAP_BIT_USBXCVREN);
468 /* Enable USB mode */
469 error = cpcap_usb_gpio_set_mode(ddata, CPCAP_OTG_DM_DP);
476 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
481 static int cpcap_usb_init_optional_pins(struct cpcap_phy_ddata *ddata)
483 ddata->pins = devm_pinctrl_get(ddata->dev);
484 if (IS_ERR(ddata->pins)) {
485 dev_info(ddata->dev, "default pins not configured: %ld\n",
486 PTR_ERR(ddata->pins));
492 ddata->pins_ulpi = pinctrl_lookup_state(ddata->pins, "ulpi");
493 if (IS_ERR(ddata->pins_ulpi)) {
494 dev_info(ddata->dev, "ulpi pins not configured\n");
495 ddata->pins_ulpi = NULL;
498 ddata->pins_utmi = pinctrl_lookup_state(ddata->pins, "utmi");
499 if (IS_ERR(ddata->pins_utmi)) {
500 dev_info(ddata->dev, "utmi pins not configured\n");
501 ddata->pins_utmi = NULL;
504 ddata->pins_uart = pinctrl_lookup_state(ddata->pins, "uart");
505 if (IS_ERR(ddata->pins_uart)) {
506 dev_info(ddata->dev, "uart pins not configured\n");
507 ddata->pins_uart = NULL;
510 if (ddata->pins_uart)
511 return pinctrl_select_state(ddata->pins, ddata->pins_uart);
516 static void cpcap_usb_init_optional_gpios(struct cpcap_phy_ddata *ddata)
520 for (i = 0; i < 2; i++) {
521 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
523 if (IS_ERR(ddata->gpio[i])) {
524 dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
525 i, PTR_ERR(ddata->gpio[i]));
526 ddata->gpio[i] = NULL;
531 static int cpcap_usb_init_iio(struct cpcap_phy_ddata *ddata)
533 enum iio_chan_type type;
536 ddata->vbus = devm_iio_channel_get(ddata->dev, "vbus");
537 if (IS_ERR(ddata->vbus)) {
538 error = PTR_ERR(ddata->vbus);
542 if (!ddata->vbus->indio_dev) {
547 error = iio_get_channel_type(ddata->vbus, &type);
551 if (type != IIO_VOLTAGE) {
559 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
566 static const struct of_device_id cpcap_usb_phy_id_table[] = {
568 .compatible = "motorola,cpcap-usb-phy",
571 .compatible = "motorola,mapphone-cpcap-usb-phy",
575 MODULE_DEVICE_TABLE(of, cpcap_usb_phy_id_table);
578 static int cpcap_usb_phy_probe(struct platform_device *pdev)
580 struct cpcap_phy_ddata *ddata;
581 struct phy *generic_phy;
582 struct phy_provider *phy_provider;
584 const struct of_device_id *of_id;
587 of_id = of_match_device(of_match_ptr(cpcap_usb_phy_id_table),
592 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
596 ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
600 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
604 ddata->dev = &pdev->dev;
605 ddata->phy.dev = ddata->dev;
606 ddata->phy.label = "cpcap_usb_phy";
607 ddata->phy.otg = otg;
608 ddata->phy.type = USB_PHY_TYPE_USB2;
609 otg->set_host = cpcap_usb_phy_set_host;
610 otg->set_peripheral = cpcap_usb_phy_set_peripheral;
611 otg->usb_phy = &ddata->phy;
612 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
613 platform_set_drvdata(pdev, ddata);
615 ddata->vusb = devm_regulator_get(&pdev->dev, "vusb");
616 if (IS_ERR(ddata->vusb))
617 return PTR_ERR(ddata->vusb);
619 error = regulator_enable(ddata->vusb);
623 generic_phy = devm_phy_create(ddata->dev, NULL, &ops);
624 if (IS_ERR(generic_phy)) {
625 error = PTR_ERR(generic_phy);
626 goto out_reg_disable;
629 phy_set_drvdata(generic_phy, ddata);
631 phy_provider = devm_of_phy_provider_register(ddata->dev,
632 of_phy_simple_xlate);
633 if (IS_ERR(phy_provider)) {
634 error = PTR_ERR(phy_provider);
635 goto out_reg_disable;
638 error = cpcap_usb_init_optional_pins(ddata);
640 goto out_reg_disable;
642 cpcap_usb_init_optional_gpios(ddata);
644 error = cpcap_usb_init_iio(ddata);
646 goto out_reg_disable;
648 error = cpcap_usb_init_interrupts(pdev, ddata);
650 goto out_reg_disable;
652 usb_add_phy_dev(&ddata->phy);
653 atomic_set(&ddata->active, 1);
654 schedule_delayed_work(&ddata->detect_work, msecs_to_jiffies(1));
659 regulator_disable(ddata->vusb);
664 static int cpcap_usb_phy_remove(struct platform_device *pdev)
666 struct cpcap_phy_ddata *ddata = platform_get_drvdata(pdev);
669 atomic_set(&ddata->active, 0);
670 error = cpcap_usb_set_uart_mode(ddata);
672 dev_err(ddata->dev, "could not set UART mode\n");
674 cpcap_usb_try_musb_mailbox(ddata, MUSB_VBUS_OFF);
676 usb_remove_phy(&ddata->phy);
677 cancel_delayed_work_sync(&ddata->detect_work);
678 clk_unprepare(ddata->refclk);
679 regulator_disable(ddata->vusb);
684 static struct platform_driver cpcap_usb_phy_driver = {
685 .probe = cpcap_usb_phy_probe,
686 .remove = cpcap_usb_phy_remove,
688 .name = "cpcap-usb-phy",
689 .of_match_table = of_match_ptr(cpcap_usb_phy_id_table),
693 module_platform_driver(cpcap_usb_phy_driver);
695 MODULE_ALIAS("platform:cpcap_usb");
696 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
697 MODULE_DESCRIPTION("CPCAP usb phy driver");
698 MODULE_LICENSE("GPL v2");