| Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 2 | /** |
| 3 | * tusb1210.c - TUSB1210 USB ULPI PHY driver |
| 4 | * |
| 5 | * Copyright (C) 2015 Intel Corporation |
| 6 | * |
| 7 | * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/ulpi/driver.h> |
| Felipe Balbi | 54fe308 | 2017-06-09 13:20:42 +0300 | [diff] [blame] | 11 | #include <linux/ulpi/regs.h> |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 12 | #include <linux/gpio/consumer.h> |
| Vivek Gautam | 858edde | 2017-05-11 12:17:41 +0530 | [diff] [blame] | 13 | #include <linux/phy/ulpi_phy.h> |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 14 | |
| 15 | #define TUSB1210_VENDOR_SPECIFIC2 0x80 |
| 16 | #define TUSB1210_VENDOR_SPECIFIC2_IHSTX_SHIFT 0 |
| 17 | #define TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_SHIFT 4 |
| 18 | #define TUSB1210_VENDOR_SPECIFIC2_DP_SHIFT 6 |
| 19 | |
| 20 | struct tusb1210 { |
| 21 | struct ulpi *ulpi; |
| 22 | struct phy *phy; |
| 23 | struct gpio_desc *gpio_reset; |
| 24 | struct gpio_desc *gpio_cs; |
| 25 | u8 vendor_specific2; |
| 26 | }; |
| 27 | |
| 28 | static int tusb1210_power_on(struct phy *phy) |
| 29 | { |
| 30 | struct tusb1210 *tusb = phy_get_drvdata(phy); |
| 31 | |
| 32 | gpiod_set_value_cansleep(tusb->gpio_reset, 1); |
| 33 | gpiod_set_value_cansleep(tusb->gpio_cs, 1); |
| 34 | |
| 35 | /* Restore the optional eye diagram optimization value */ |
| 36 | if (tusb->vendor_specific2) |
| 37 | ulpi_write(tusb->ulpi, TUSB1210_VENDOR_SPECIFIC2, |
| 38 | tusb->vendor_specific2); |
| 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int tusb1210_power_off(struct phy *phy) |
| 44 | { |
| 45 | struct tusb1210 *tusb = phy_get_drvdata(phy); |
| 46 | |
| 47 | gpiod_set_value_cansleep(tusb->gpio_reset, 0); |
| 48 | gpiod_set_value_cansleep(tusb->gpio_cs, 0); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| Grygorii Strashko | 79a5a18 | 2018-11-19 19:24:20 -0600 | [diff] [blame] | 53 | static int tusb1210_set_mode(struct phy *phy, enum phy_mode mode, int submode) |
| Felipe Balbi | 54fe308 | 2017-06-09 13:20:42 +0300 | [diff] [blame] | 54 | { |
| 55 | struct tusb1210 *tusb = phy_get_drvdata(phy); |
| 56 | int ret; |
| 57 | |
| 58 | ret = ulpi_read(tusb->ulpi, ULPI_OTG_CTRL); |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | |
| 62 | switch (mode) { |
| 63 | case PHY_MODE_USB_HOST: |
| 64 | ret |= (ULPI_OTG_CTRL_DRVVBUS_EXT |
| 65 | | ULPI_OTG_CTRL_ID_PULLUP |
| 66 | | ULPI_OTG_CTRL_DP_PULLDOWN |
| 67 | | ULPI_OTG_CTRL_DM_PULLDOWN); |
| 68 | ulpi_write(tusb->ulpi, ULPI_OTG_CTRL, ret); |
| 69 | ret |= ULPI_OTG_CTRL_DRVVBUS; |
| 70 | break; |
| 71 | case PHY_MODE_USB_DEVICE: |
| 72 | ret &= ~(ULPI_OTG_CTRL_DRVVBUS |
| 73 | | ULPI_OTG_CTRL_DP_PULLDOWN |
| 74 | | ULPI_OTG_CTRL_DM_PULLDOWN); |
| 75 | ulpi_write(tusb->ulpi, ULPI_OTG_CTRL, ret); |
| 76 | ret &= ~ULPI_OTG_CTRL_DRVVBUS_EXT; |
| 77 | break; |
| 78 | default: |
| 79 | /* nothing */ |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | return ulpi_write(tusb->ulpi, ULPI_OTG_CTRL, ret); |
| 84 | } |
| 85 | |
| Axel Lin | 4a9e5ca | 2015-07-15 15:33:51 +0800 | [diff] [blame] | 86 | static const struct phy_ops phy_ops = { |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 87 | .power_on = tusb1210_power_on, |
| 88 | .power_off = tusb1210_power_off, |
| Felipe Balbi | 54fe308 | 2017-06-09 13:20:42 +0300 | [diff] [blame] | 89 | .set_mode = tusb1210_set_mode, |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 90 | .owner = THIS_MODULE, |
| 91 | }; |
| 92 | |
| 93 | static int tusb1210_probe(struct ulpi *ulpi) |
| 94 | { |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 95 | struct tusb1210 *tusb; |
| 96 | u8 val, reg; |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 97 | |
| 98 | tusb = devm_kzalloc(&ulpi->dev, sizeof(*tusb), GFP_KERNEL); |
| 99 | if (!tusb) |
| 100 | return -ENOMEM; |
| 101 | |
| Uwe Kleine-König | 8e71c68 | 2015-06-12 08:55:30 +0200 | [diff] [blame] | 102 | tusb->gpio_reset = devm_gpiod_get_optional(&ulpi->dev, "reset", |
| 103 | GPIOD_OUT_LOW); |
| 104 | if (IS_ERR(tusb->gpio_reset)) |
| 105 | return PTR_ERR(tusb->gpio_reset); |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 106 | |
| Uwe Kleine-König | 8e71c68 | 2015-06-12 08:55:30 +0200 | [diff] [blame] | 107 | gpiod_set_value_cansleep(tusb->gpio_reset, 1); |
| 108 | |
| 109 | tusb->gpio_cs = devm_gpiod_get_optional(&ulpi->dev, "cs", |
| 110 | GPIOD_OUT_LOW); |
| 111 | if (IS_ERR(tusb->gpio_cs)) |
| 112 | return PTR_ERR(tusb->gpio_cs); |
| 113 | |
| 114 | gpiod_set_value_cansleep(tusb->gpio_cs, 1); |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * VENDOR_SPECIFIC2 register in TUSB1210 can be used for configuring eye |
| 118 | * diagram optimization and DP/DM swap. |
| 119 | */ |
| 120 | |
| 121 | /* High speed output drive strength configuration */ |
| 122 | device_property_read_u8(&ulpi->dev, "ihstx", &val); |
| 123 | reg = val << TUSB1210_VENDOR_SPECIFIC2_IHSTX_SHIFT; |
| 124 | |
| 125 | /* High speed output impedance configuration */ |
| 126 | device_property_read_u8(&ulpi->dev, "zhsdrv", &val); |
| 127 | reg |= val << TUSB1210_VENDOR_SPECIFIC2_ZHSDRV_SHIFT; |
| 128 | |
| 129 | /* DP/DM swap control */ |
| 130 | device_property_read_u8(&ulpi->dev, "datapolarity", &val); |
| 131 | reg |= val << TUSB1210_VENDOR_SPECIFIC2_DP_SHIFT; |
| 132 | |
| 133 | if (reg) { |
| 134 | ulpi_write(ulpi, TUSB1210_VENDOR_SPECIFIC2, reg); |
| 135 | tusb->vendor_specific2 = reg; |
| 136 | } |
| 137 | |
| 138 | tusb->phy = ulpi_phy_create(ulpi, &phy_ops); |
| 139 | if (IS_ERR(tusb->phy)) |
| 140 | return PTR_ERR(tusb->phy); |
| 141 | |
| 142 | tusb->ulpi = ulpi; |
| 143 | |
| 144 | phy_set_drvdata(tusb->phy, tusb); |
| 145 | ulpi_set_drvdata(ulpi, tusb); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static void tusb1210_remove(struct ulpi *ulpi) |
| 150 | { |
| 151 | struct tusb1210 *tusb = ulpi_get_drvdata(ulpi); |
| 152 | |
| 153 | ulpi_phy_destroy(ulpi, tusb->phy); |
| 154 | } |
| 155 | |
| 156 | #define TI_VENDOR_ID 0x0451 |
| 157 | |
| 158 | static const struct ulpi_device_id tusb1210_ulpi_id[] = { |
| Felipe Balbi | 82d9d5e | 2017-06-09 13:20:41 +0300 | [diff] [blame] | 159 | { TI_VENDOR_ID, 0x1507, }, /* TUSB1210 */ |
| 160 | { TI_VENDOR_ID, 0x1508, }, /* TUSB1211 */ |
| Heikki Krogerus | 1c14905 | 2015-05-13 15:26:53 +0300 | [diff] [blame] | 161 | { }, |
| 162 | }; |
| 163 | MODULE_DEVICE_TABLE(ulpi, tusb1210_ulpi_id); |
| 164 | |
| 165 | static struct ulpi_driver tusb1210_driver = { |
| 166 | .id_table = tusb1210_ulpi_id, |
| 167 | .probe = tusb1210_probe, |
| 168 | .remove = tusb1210_remove, |
| 169 | .driver = { |
| 170 | .name = "tusb1210", |
| 171 | .owner = THIS_MODULE, |
| 172 | }, |
| 173 | }; |
| 174 | |
| 175 | module_ulpi_driver(tusb1210_driver); |
| 176 | |
| 177 | MODULE_AUTHOR("Intel Corporation"); |
| 178 | MODULE_LICENSE("GPL v2"); |
| 179 | MODULE_DESCRIPTION("TUSB1210 ULPI PHY driver"); |