Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Allwinner sun4i USB phy driver |
| 3 | * |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 4 | * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com> |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 5 | * |
| 6 | * Based on code from |
| 7 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 8 | * |
| 9 | * Modelled after: Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver |
| 10 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. |
| 11 | * Author: Sylwester Nawrocki <s.nawrocki@samsung.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/clk.h> |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 25 | #include <linux/delay.h> |
Sachin Kamat | 2d84aff | 2014-05-29 12:00:49 +0530 | [diff] [blame] | 26 | #include <linux/err.h> |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 27 | #include <linux/extcon.h> |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 28 | #include <linux/io.h> |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 29 | #include <linux/interrupt.h> |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 30 | #include <linux/kernel.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/mutex.h> |
| 33 | #include <linux/of.h> |
| 34 | #include <linux/of_address.h> |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 35 | #include <linux/of_device.h> |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 36 | #include <linux/of_gpio.h> |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 37 | #include <linux/phy/phy.h> |
Hans de Goede | 24fe86a | 2015-03-29 12:50:46 +0200 | [diff] [blame] | 38 | #include <linux/phy/phy-sun4i-usb.h> |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 39 | #include <linux/platform_device.h> |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 40 | #include <linux/power_supply.h> |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 41 | #include <linux/regulator/consumer.h> |
| 42 | #include <linux/reset.h> |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 43 | #include <linux/workqueue.h> |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 44 | |
| 45 | #define REG_ISCR 0x00 |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 46 | #define REG_PHYCTL_A10 0x04 |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 47 | #define REG_PHYBIST 0x08 |
| 48 | #define REG_PHYTUNE 0x0c |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 49 | #define REG_PHYCTL_A33 0x10 |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 50 | #define REG_PHY_UNK_H3 0x20 |
| 51 | |
| 52 | #define REG_PMU_UNK_H3 0x10 |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 53 | |
| 54 | #define PHYCTL_DATA BIT(7) |
| 55 | |
| 56 | #define SUNXI_AHB_ICHR8_EN BIT(10) |
| 57 | #define SUNXI_AHB_INCR4_BURST_EN BIT(9) |
| 58 | #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8) |
| 59 | #define SUNXI_ULPI_BYPASS_EN BIT(0) |
| 60 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 61 | /* ISCR, Interface Status and Control bits */ |
| 62 | #define ISCR_ID_PULLUP_EN (1 << 17) |
| 63 | #define ISCR_DPDM_PULLUP_EN (1 << 16) |
| 64 | /* sunxi has the phy id/vbus pins not connected, so we use the force bits */ |
| 65 | #define ISCR_FORCE_ID_MASK (3 << 14) |
| 66 | #define ISCR_FORCE_ID_LOW (2 << 14) |
| 67 | #define ISCR_FORCE_ID_HIGH (3 << 14) |
| 68 | #define ISCR_FORCE_VBUS_MASK (3 << 12) |
| 69 | #define ISCR_FORCE_VBUS_LOW (2 << 12) |
| 70 | #define ISCR_FORCE_VBUS_HIGH (3 << 12) |
| 71 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 72 | /* Common Control Bits for Both PHYs */ |
| 73 | #define PHY_PLL_BW 0x03 |
| 74 | #define PHY_RES45_CAL_EN 0x0c |
| 75 | |
| 76 | /* Private Control Bits for Each PHY */ |
| 77 | #define PHY_TX_AMPLITUDE_TUNE 0x20 |
| 78 | #define PHY_TX_SLEWRATE_TUNE 0x22 |
| 79 | #define PHY_VBUSVALID_TH_SEL 0x25 |
| 80 | #define PHY_PULLUP_RES_SEL 0x27 |
| 81 | #define PHY_OTG_FUNC_EN 0x28 |
| 82 | #define PHY_VBUS_DET_EN 0x29 |
| 83 | #define PHY_DISCON_TH_SEL 0x2a |
Hans de Goede | 24fe86a | 2015-03-29 12:50:46 +0200 | [diff] [blame] | 84 | #define PHY_SQUELCH_DETECT 0x3c |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 85 | |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 86 | #define MAX_PHYS 4 |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 87 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Note do not raise the debounce time, we must report Vusb high within 100ms |
| 90 | * otherwise we get Vbus errors |
| 91 | */ |
| 92 | #define DEBOUNCE_TIME msecs_to_jiffies(50) |
| 93 | #define POLL_TIME msecs_to_jiffies(250) |
| 94 | |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 95 | enum sun4i_usb_phy_type { |
| 96 | sun4i_a10_phy, |
Hans de Goede | 91d96f0 | 2016-06-29 20:14:10 +0200 | [diff] [blame] | 97 | sun6i_a31_phy, |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 98 | sun8i_a33_phy, |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 99 | sun8i_h3_phy, |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct sun4i_usb_phy_cfg { |
| 103 | int num_phys; |
| 104 | enum sun4i_usb_phy_type type; |
| 105 | u32 disc_thresh; |
| 106 | u8 phyctl_offset; |
| 107 | bool dedicated_clocks; |
| 108 | }; |
| 109 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 110 | struct sun4i_usb_phy_data { |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 111 | void __iomem *base; |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 112 | const struct sun4i_usb_phy_cfg *cfg; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 113 | struct mutex mutex; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 114 | struct sun4i_usb_phy { |
| 115 | struct phy *phy; |
| 116 | void __iomem *pmu; |
| 117 | struct regulator *vbus; |
| 118 | struct reset_control *reset; |
Maxime Ripard | eadd431 | 2014-05-13 17:44:18 +0200 | [diff] [blame] | 119 | struct clk *clk; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 120 | bool regulator_on; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 121 | int index; |
| 122 | } phys[MAX_PHYS]; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 123 | /* phy0 / otg related variables */ |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 124 | struct extcon_dev *extcon; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 125 | bool phy0_init; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 126 | struct gpio_desc *id_det_gpio; |
| 127 | struct gpio_desc *vbus_det_gpio; |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 128 | struct power_supply *vbus_power_supply; |
| 129 | struct notifier_block vbus_power_nb; |
| 130 | bool vbus_power_nb_registered; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 131 | int id_det_irq; |
| 132 | int vbus_det_irq; |
| 133 | int id_det; |
| 134 | int vbus_det; |
| 135 | struct delayed_work detect; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | #define to_sun4i_usb_phy_data(phy) \ |
| 139 | container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index]) |
| 140 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 141 | static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set) |
| 142 | { |
| 143 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); |
| 144 | struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy); |
| 145 | u32 iscr; |
| 146 | |
| 147 | iscr = readl(data->base + REG_ISCR); |
| 148 | iscr &= ~clr; |
| 149 | iscr |= set; |
| 150 | writel(iscr, data->base + REG_ISCR); |
| 151 | } |
| 152 | |
| 153 | static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val) |
| 154 | { |
| 155 | if (val) |
| 156 | val = ISCR_FORCE_ID_HIGH; |
| 157 | else |
| 158 | val = ISCR_FORCE_ID_LOW; |
| 159 | |
| 160 | sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val); |
| 161 | } |
| 162 | |
| 163 | static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val) |
| 164 | { |
| 165 | if (val) |
| 166 | val = ISCR_FORCE_VBUS_HIGH; |
| 167 | else |
| 168 | val = ISCR_FORCE_VBUS_LOW; |
| 169 | |
| 170 | sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val); |
| 171 | } |
| 172 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 173 | static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data, |
| 174 | int len) |
| 175 | { |
| 176 | struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy); |
| 177 | u32 temp, usbc_bit = BIT(phy->index * 2); |
Ben Dooks | d99cb37 | 2016-06-07 18:14:56 +0100 | [diff] [blame] | 178 | void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 179 | int i; |
| 180 | |
| 181 | mutex_lock(&phy_data->mutex); |
| 182 | |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 183 | if (phy_data->cfg->type == sun8i_a33_phy) { |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 184 | /* A33 needs us to set phyctl to 0 explicitly */ |
| 185 | writel(0, phyctl); |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 188 | for (i = 0; i < len; i++) { |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 189 | temp = readl(phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 190 | |
| 191 | /* clear the address portion */ |
| 192 | temp &= ~(0xff << 8); |
| 193 | |
| 194 | /* set the address */ |
| 195 | temp |= ((addr + i) << 8); |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 196 | writel(temp, phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 197 | |
| 198 | /* set the data bit and clear usbc bit*/ |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 199 | temp = readb(phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 200 | if (data & 0x1) |
| 201 | temp |= PHYCTL_DATA; |
| 202 | else |
| 203 | temp &= ~PHYCTL_DATA; |
| 204 | temp &= ~usbc_bit; |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 205 | writeb(temp, phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 206 | |
| 207 | /* pulse usbc_bit */ |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 208 | temp = readb(phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 209 | temp |= usbc_bit; |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 210 | writeb(temp, phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 211 | |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 212 | temp = readb(phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 213 | temp &= ~usbc_bit; |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 214 | writeb(temp, phyctl); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 215 | |
| 216 | data >>= 1; |
| 217 | } |
| 218 | mutex_unlock(&phy_data->mutex); |
| 219 | } |
| 220 | |
| 221 | static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable) |
| 222 | { |
| 223 | u32 bits, reg_value; |
| 224 | |
| 225 | if (!phy->pmu) |
| 226 | return; |
| 227 | |
| 228 | bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN | |
| 229 | SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN; |
| 230 | |
| 231 | reg_value = readl(phy->pmu); |
| 232 | |
| 233 | if (enable) |
| 234 | reg_value |= bits; |
| 235 | else |
| 236 | reg_value &= ~bits; |
| 237 | |
| 238 | writel(reg_value, phy->pmu); |
| 239 | } |
| 240 | |
| 241 | static int sun4i_usb_phy_init(struct phy *_phy) |
| 242 | { |
| 243 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); |
| 244 | struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy); |
| 245 | int ret; |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 246 | u32 val; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 247 | |
Maxime Ripard | eadd431 | 2014-05-13 17:44:18 +0200 | [diff] [blame] | 248 | ret = clk_prepare_enable(phy->clk); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 249 | if (ret) |
| 250 | return ret; |
| 251 | |
| 252 | ret = reset_control_deassert(phy->reset); |
| 253 | if (ret) { |
Maxime Ripard | eadd431 | 2014-05-13 17:44:18 +0200 | [diff] [blame] | 254 | clk_disable_unprepare(phy->clk); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 258 | if (data->cfg->type == sun8i_h3_phy) { |
| 259 | if (phy->index == 0) { |
| 260 | val = readl(data->base + REG_PHY_UNK_H3); |
| 261 | writel(val & ~1, data->base + REG_PHY_UNK_H3); |
| 262 | } |
Roman Byshko | 6827a46f | 2014-11-10 19:55:06 +0100 | [diff] [blame] | 263 | |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 264 | val = readl(phy->pmu + REG_PMU_UNK_H3); |
| 265 | writel(val & ~2, phy->pmu + REG_PMU_UNK_H3); |
| 266 | } else { |
| 267 | /* Enable USB 45 Ohm resistor calibration */ |
| 268 | if (phy->index == 0) |
| 269 | sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 270 | |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 271 | /* Adjust PHY's magnitude and rate */ |
| 272 | sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5); |
| 273 | |
| 274 | /* Disconnect threshold adjustment */ |
| 275 | sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, |
| 276 | data->cfg->disc_thresh, 2); |
| 277 | } |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 278 | |
| 279 | sun4i_usb_phy_passby(phy, 1); |
| 280 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 281 | if (phy->index == 0) { |
| 282 | data->phy0_init = true; |
| 283 | |
| 284 | /* Enable pull-ups */ |
| 285 | sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN); |
| 286 | sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN); |
| 287 | |
| 288 | if (data->id_det_gpio) { |
| 289 | /* OTG mode, force ISCR and cable state updates */ |
| 290 | data->id_det = -1; |
| 291 | data->vbus_det = -1; |
| 292 | queue_delayed_work(system_wq, &data->detect, 0); |
| 293 | } else { |
| 294 | /* Host only mode */ |
| 295 | sun4i_usb_phy0_set_id_detect(_phy, 0); |
| 296 | sun4i_usb_phy0_set_vbus_detect(_phy, 1); |
| 297 | } |
| 298 | } |
| 299 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int sun4i_usb_phy_exit(struct phy *_phy) |
| 304 | { |
| 305 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 306 | struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy); |
| 307 | |
| 308 | if (phy->index == 0) { |
| 309 | /* Disable pull-ups */ |
| 310 | sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0); |
| 311 | sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0); |
| 312 | data->phy0_init = false; |
| 313 | } |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 314 | |
| 315 | sun4i_usb_phy_passby(phy, 0); |
| 316 | reset_control_assert(phy->reset); |
Maxime Ripard | eadd431 | 2014-05-13 17:44:18 +0200 | [diff] [blame] | 317 | clk_disable_unprepare(phy->clk); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Hans de Goede | 3d772c4 | 2015-07-31 10:01:41 +0200 | [diff] [blame] | 322 | static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data) |
| 323 | { |
| 324 | if (data->vbus_det_gpio) |
| 325 | return gpiod_get_value_cansleep(data->vbus_det_gpio); |
| 326 | |
| 327 | if (data->vbus_power_supply) { |
| 328 | union power_supply_propval val; |
| 329 | int r; |
| 330 | |
| 331 | r = power_supply_get_property(data->vbus_power_supply, |
| 332 | POWER_SUPPLY_PROP_PRESENT, &val); |
| 333 | if (r == 0) |
| 334 | return val.intval; |
| 335 | } |
| 336 | |
| 337 | /* Fallback: report vbus as high */ |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data) |
| 342 | { |
| 343 | return data->vbus_det_gpio || data->vbus_power_supply; |
| 344 | } |
| 345 | |
Hans de Goede | 91d96f0 | 2016-06-29 20:14:10 +0200 | [diff] [blame] | 346 | static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data) |
| 347 | { |
| 348 | if ((data->id_det_gpio && data->id_det_irq <= 0) || |
| 349 | (data->vbus_det_gpio && data->vbus_det_irq <= 0)) |
| 350 | return true; |
| 351 | |
| 352 | /* |
| 353 | * The A31 companion pmic (axp221) does not generate vbus change |
| 354 | * interrupts when the board is driving vbus, so we must poll |
| 355 | * when using the pmic for vbus-det _and_ we're driving vbus. |
| 356 | */ |
| 357 | if (data->cfg->type == sun6i_a31_phy && |
| 358 | data->vbus_power_supply && data->phys[0].regulator_on) |
| 359 | return true; |
| 360 | |
| 361 | return false; |
| 362 | } |
| 363 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 364 | static int sun4i_usb_phy_power_on(struct phy *_phy) |
| 365 | { |
| 366 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 367 | struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy); |
| 368 | int ret; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 369 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 370 | if (!phy->vbus || phy->regulator_on) |
| 371 | return 0; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 372 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 373 | /* For phy0 only turn on Vbus if we don't have an ext. Vbus */ |
Hans de Goede | 8083526 | 2015-07-31 10:01:42 +0200 | [diff] [blame] | 374 | if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) && |
| 375 | data->vbus_det) |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 376 | return 0; |
| 377 | |
| 378 | ret = regulator_enable(phy->vbus); |
| 379 | if (ret) |
| 380 | return ret; |
| 381 | |
| 382 | phy->regulator_on = true; |
| 383 | |
| 384 | /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */ |
Hans de Goede | 91d96f0 | 2016-06-29 20:14:10 +0200 | [diff] [blame] | 385 | if (phy->index == 0 && sun4i_usb_phy0_poll(data)) |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 386 | mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME); |
| 387 | |
| 388 | return 0; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static int sun4i_usb_phy_power_off(struct phy *_phy) |
| 392 | { |
| 393 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 394 | struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 395 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 396 | if (!phy->vbus || !phy->regulator_on) |
| 397 | return 0; |
| 398 | |
| 399 | regulator_disable(phy->vbus); |
| 400 | phy->regulator_on = false; |
| 401 | |
| 402 | /* |
| 403 | * phy0 vbus typically slowly discharges, sometimes this causes the |
| 404 | * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan. |
| 405 | */ |
Hans de Goede | 91d96f0 | 2016-06-29 20:14:10 +0200 | [diff] [blame] | 406 | if (phy->index == 0 && !sun4i_usb_phy0_poll(data)) |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 407 | mod_delayed_work(system_wq, &data->detect, POLL_TIME); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
Hans de Goede | 24fe86a | 2015-03-29 12:50:46 +0200 | [diff] [blame] | 412 | void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled) |
| 413 | { |
| 414 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); |
| 415 | |
| 416 | sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2); |
| 417 | } |
Hans de Goede | 7167bf8 | 2015-07-31 10:01:40 +0200 | [diff] [blame] | 418 | EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect); |
Hans de Goede | 24fe86a | 2015-03-29 12:50:46 +0200 | [diff] [blame] | 419 | |
Axel Lin | 4a9e5ca | 2015-07-15 15:33:51 +0800 | [diff] [blame] | 420 | static const struct phy_ops sun4i_usb_phy_ops = { |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 421 | .init = sun4i_usb_phy_init, |
| 422 | .exit = sun4i_usb_phy_exit, |
| 423 | .power_on = sun4i_usb_phy_power_on, |
| 424 | .power_off = sun4i_usb_phy_power_off, |
| 425 | .owner = THIS_MODULE, |
| 426 | }; |
| 427 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 428 | static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work) |
| 429 | { |
| 430 | struct sun4i_usb_phy_data *data = |
| 431 | container_of(work, struct sun4i_usb_phy_data, detect.work); |
| 432 | struct phy *phy0 = data->phys[0].phy; |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 433 | int id_det, vbus_det, id_notify = 0, vbus_notify = 0; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 434 | |
| 435 | id_det = gpiod_get_value_cansleep(data->id_det_gpio); |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 436 | vbus_det = sun4i_usb_phy0_get_vbus_det(data); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 437 | |
| 438 | mutex_lock(&phy0->mutex); |
| 439 | |
| 440 | if (!data->phy0_init) { |
| 441 | mutex_unlock(&phy0->mutex); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | if (id_det != data->id_det) { |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 446 | /* |
| 447 | * When a host cable (id == 0) gets plugged in on systems |
| 448 | * without vbus detection report vbus low for long enough for |
| 449 | * the musb-ip to end the current device session. |
| 450 | */ |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 451 | if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) { |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 452 | sun4i_usb_phy0_set_vbus_detect(phy0, 0); |
| 453 | msleep(200); |
| 454 | sun4i_usb_phy0_set_vbus_detect(phy0, 1); |
| 455 | } |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 456 | sun4i_usb_phy0_set_id_detect(phy0, id_det); |
| 457 | data->id_det = id_det; |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 458 | id_notify = 1; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | if (vbus_det != data->vbus_det) { |
| 462 | sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det); |
| 463 | data->vbus_det = vbus_det; |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 464 | vbus_notify = 1; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | mutex_unlock(&phy0->mutex); |
| 468 | |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 469 | if (id_notify) { |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 470 | extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST, |
| 471 | !id_det); |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 472 | /* |
| 473 | * When a host cable gets unplugged (id == 1) on systems |
| 474 | * without vbus detection report vbus low for long enough to |
| 475 | * the musb-ip to end the current host session. |
| 476 | */ |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 477 | if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) { |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 478 | mutex_lock(&phy0->mutex); |
| 479 | sun4i_usb_phy0_set_vbus_detect(phy0, 0); |
| 480 | msleep(1000); |
| 481 | sun4i_usb_phy0_set_vbus_detect(phy0, 1); |
| 482 | mutex_unlock(&phy0->mutex); |
| 483 | } |
| 484 | } |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 485 | |
| 486 | if (vbus_notify) |
| 487 | extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det); |
| 488 | |
Hans de Goede | 91d96f0 | 2016-06-29 20:14:10 +0200 | [diff] [blame] | 489 | if (sun4i_usb_phy0_poll(data)) |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 490 | queue_delayed_work(system_wq, &data->detect, POLL_TIME); |
| 491 | } |
| 492 | |
| 493 | static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id) |
| 494 | { |
| 495 | struct sun4i_usb_phy_data *data = dev_id; |
| 496 | |
| 497 | /* vbus or id changed, let the pins settle and then scan them */ |
| 498 | mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME); |
| 499 | |
| 500 | return IRQ_HANDLED; |
| 501 | } |
| 502 | |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 503 | static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb, |
| 504 | unsigned long val, void *v) |
| 505 | { |
| 506 | struct sun4i_usb_phy_data *data = |
| 507 | container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb); |
| 508 | struct power_supply *psy = v; |
| 509 | |
| 510 | /* Properties on the vbus_power_supply changed, scan vbus_det */ |
| 511 | if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply) |
| 512 | mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME); |
| 513 | |
| 514 | return NOTIFY_OK; |
| 515 | } |
| 516 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 517 | static struct phy *sun4i_usb_phy_xlate(struct device *dev, |
| 518 | struct of_phandle_args *args) |
| 519 | { |
| 520 | struct sun4i_usb_phy_data *data = dev_get_drvdata(dev); |
| 521 | |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 522 | if (args->args[0] >= data->cfg->num_phys) |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 523 | return ERR_PTR(-ENODEV); |
| 524 | |
| 525 | return data->phys[args->args[0]].phy; |
| 526 | } |
| 527 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 528 | static int sun4i_usb_phy_remove(struct platform_device *pdev) |
| 529 | { |
| 530 | struct device *dev = &pdev->dev; |
| 531 | struct sun4i_usb_phy_data *data = dev_get_drvdata(dev); |
| 532 | |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 533 | if (data->vbus_power_nb_registered) |
| 534 | power_supply_unreg_notifier(&data->vbus_power_nb); |
Hans de Goede | 04e59a0 | 2016-06-18 11:31:33 +0200 | [diff] [blame] | 535 | if (data->id_det_irq > 0) |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 536 | devm_free_irq(dev, data->id_det_irq, data); |
Hans de Goede | 04e59a0 | 2016-06-18 11:31:33 +0200 | [diff] [blame] | 537 | if (data->vbus_det_irq > 0) |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 538 | devm_free_irq(dev, data->vbus_det_irq, data); |
| 539 | |
| 540 | cancel_delayed_work_sync(&data->detect); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 545 | static const unsigned int sun4i_usb_phy0_cable[] = { |
| 546 | EXTCON_USB, |
| 547 | EXTCON_USB_HOST, |
| 548 | EXTCON_NONE, |
| 549 | }; |
| 550 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 551 | static int sun4i_usb_phy_probe(struct platform_device *pdev) |
| 552 | { |
| 553 | struct sun4i_usb_phy_data *data; |
| 554 | struct device *dev = &pdev->dev; |
| 555 | struct device_node *np = dev->of_node; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 556 | struct phy_provider *phy_provider; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 557 | struct resource *res; |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 558 | int i, ret; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 559 | |
| 560 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 561 | if (!data) |
| 562 | return -ENOMEM; |
| 563 | |
| 564 | mutex_init(&data->mutex); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 565 | INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan); |
| 566 | dev_set_drvdata(dev, data); |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 567 | data->cfg = of_device_get_match_data(dev); |
| 568 | if (!data->cfg) |
| 569 | return -EINVAL; |
Hans de Goede | fc1f45e | 2015-06-13 14:37:49 +0200 | [diff] [blame] | 570 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 571 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl"); |
| 572 | data->base = devm_ioremap_resource(dev, res); |
| 573 | if (IS_ERR(data->base)) |
| 574 | return PTR_ERR(data->base); |
| 575 | |
Axel Lin | b2dfc34 | 2015-09-25 08:47:29 +0800 | [diff] [blame] | 576 | data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det", |
| 577 | GPIOD_IN); |
| 578 | if (IS_ERR(data->id_det_gpio)) |
| 579 | return PTR_ERR(data->id_det_gpio); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 580 | |
Axel Lin | b2dfc34 | 2015-09-25 08:47:29 +0800 | [diff] [blame] | 581 | data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det", |
| 582 | GPIOD_IN); |
| 583 | if (IS_ERR(data->vbus_det_gpio)) |
| 584 | return PTR_ERR(data->vbus_det_gpio); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 585 | |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 586 | if (of_find_property(np, "usb0_vbus_power-supply", NULL)) { |
| 587 | data->vbus_power_supply = devm_power_supply_get_by_phandle(dev, |
| 588 | "usb0_vbus_power-supply"); |
| 589 | if (IS_ERR(data->vbus_power_supply)) |
| 590 | return PTR_ERR(data->vbus_power_supply); |
| 591 | |
| 592 | if (!data->vbus_power_supply) |
| 593 | return -EPROBE_DEFER; |
| 594 | } |
| 595 | |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 596 | /* vbus_det without id_det makes no sense, and is not supported */ |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 597 | if (sun4i_usb_phy0_have_vbus_det(data) && !data->id_det_gpio) { |
Hans de Goede | 1aedf3a | 2015-06-13 14:37:50 +0200 | [diff] [blame] | 598 | dev_err(dev, "usb0_id_det missing or invalid\n"); |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 599 | return -ENODEV; |
| 600 | } |
| 601 | |
Hans de Goede | 1a52abe | 2015-06-13 14:37:46 +0200 | [diff] [blame] | 602 | if (data->id_det_gpio) { |
| 603 | data->extcon = devm_extcon_dev_allocate(dev, |
| 604 | sun4i_usb_phy0_cable); |
| 605 | if (IS_ERR(data->extcon)) |
| 606 | return PTR_ERR(data->extcon); |
| 607 | |
| 608 | ret = devm_extcon_dev_register(dev, data->extcon); |
| 609 | if (ret) { |
| 610 | dev_err(dev, "failed to register extcon: %d\n", ret); |
| 611 | return ret; |
| 612 | } |
| 613 | } |
| 614 | |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 615 | for (i = 0; i < data->cfg->num_phys; i++) { |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 616 | struct sun4i_usb_phy *phy = data->phys + i; |
| 617 | char name[16]; |
| 618 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 619 | snprintf(name, sizeof(name), "usb%d_vbus", i); |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 620 | phy->vbus = devm_regulator_get_optional(dev, name); |
| 621 | if (IS_ERR(phy->vbus)) { |
| 622 | if (PTR_ERR(phy->vbus) == -EPROBE_DEFER) |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 623 | return -EPROBE_DEFER; |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 624 | phy->vbus = NULL; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 627 | if (data->cfg->dedicated_clocks) |
Maxime Ripard | eadd431 | 2014-05-13 17:44:18 +0200 | [diff] [blame] | 628 | snprintf(name, sizeof(name), "usb%d_phy", i); |
| 629 | else |
| 630 | strlcpy(name, "usb_phy", sizeof(name)); |
| 631 | |
| 632 | phy->clk = devm_clk_get(dev, name); |
| 633 | if (IS_ERR(phy->clk)) { |
| 634 | dev_err(dev, "failed to get clock %s\n", name); |
| 635 | return PTR_ERR(phy->clk); |
| 636 | } |
| 637 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 638 | snprintf(name, sizeof(name), "usb%d_reset", i); |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 639 | phy->reset = devm_reset_control_get(dev, name); |
| 640 | if (IS_ERR(phy->reset)) { |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 641 | dev_err(dev, "failed to get reset %s\n", name); |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 642 | return PTR_ERR(phy->reset); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | if (i) { /* No pmu for usbc0 */ |
| 646 | snprintf(name, sizeof(name), "pmu%d", i); |
| 647 | res = platform_get_resource_byname(pdev, |
| 648 | IORESOURCE_MEM, name); |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 649 | phy->pmu = devm_ioremap_resource(dev, res); |
| 650 | if (IS_ERR(phy->pmu)) |
| 651 | return PTR_ERR(phy->pmu); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Heikki Krogerus | dbc9863 | 2014-11-19 17:28:21 +0200 | [diff] [blame] | 654 | phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops); |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 655 | if (IS_ERR(phy->phy)) { |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 656 | dev_err(dev, "failed to create PHY %d\n", i); |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 657 | return PTR_ERR(phy->phy); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Maxime Ripard | 2a7f998 | 2014-05-13 17:44:17 +0200 | [diff] [blame] | 660 | phy->index = i; |
| 661 | phy_set_drvdata(phy->phy, &data->phys[i]); |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 662 | } |
| 663 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 664 | data->id_det_irq = gpiod_to_irq(data->id_det_gpio); |
Quentin Schulz | 5cf700a | 2016-06-13 13:45:48 +0200 | [diff] [blame] | 665 | if (data->id_det_irq > 0) { |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 666 | ret = devm_request_irq(dev, data->id_det_irq, |
| 667 | sun4i_usb_phy0_id_vbus_det_irq, |
| 668 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 669 | "usb0-id-det", data); |
| 670 | if (ret) { |
| 671 | dev_err(dev, "Err requesting id-det-irq: %d\n", ret); |
| 672 | return ret; |
| 673 | } |
| 674 | } |
| 675 | |
Hans de Goede | 91d96f0 | 2016-06-29 20:14:10 +0200 | [diff] [blame] | 676 | data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio); |
Quentin Schulz | 5cf700a | 2016-06-13 13:45:48 +0200 | [diff] [blame] | 677 | if (data->vbus_det_irq > 0) { |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 678 | ret = devm_request_irq(dev, data->vbus_det_irq, |
| 679 | sun4i_usb_phy0_id_vbus_det_irq, |
| 680 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 681 | "usb0-vbus-det", data); |
| 682 | if (ret) { |
| 683 | dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret); |
| 684 | data->vbus_det_irq = -1; |
| 685 | sun4i_usb_phy_remove(pdev); /* Stop detect work */ |
| 686 | return ret; |
| 687 | } |
| 688 | } |
| 689 | |
Hans de Goede | 8665c18 | 2015-06-13 14:37:51 +0200 | [diff] [blame] | 690 | if (data->vbus_power_supply) { |
| 691 | data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify; |
| 692 | data->vbus_power_nb.priority = 0; |
| 693 | ret = power_supply_reg_notifier(&data->vbus_power_nb); |
| 694 | if (ret) { |
| 695 | sun4i_usb_phy_remove(pdev); /* Stop detect work */ |
| 696 | return ret; |
| 697 | } |
| 698 | data->vbus_power_nb_registered = true; |
| 699 | } |
| 700 | |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 701 | phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate); |
| 702 | if (IS_ERR(phy_provider)) { |
| 703 | sun4i_usb_phy_remove(pdev); /* Stop detect work */ |
| 704 | return PTR_ERR(phy_provider); |
| 705 | } |
| 706 | |
| 707 | return 0; |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 710 | static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = { |
| 711 | .num_phys = 3, |
| 712 | .type = sun4i_a10_phy, |
| 713 | .disc_thresh = 3, |
| 714 | .phyctl_offset = REG_PHYCTL_A10, |
| 715 | .dedicated_clocks = false, |
| 716 | }; |
| 717 | |
| 718 | static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = { |
| 719 | .num_phys = 2, |
| 720 | .type = sun4i_a10_phy, |
| 721 | .disc_thresh = 2, |
| 722 | .phyctl_offset = REG_PHYCTL_A10, |
| 723 | .dedicated_clocks = false, |
| 724 | }; |
| 725 | |
| 726 | static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = { |
| 727 | .num_phys = 3, |
Hans de Goede | 91d96f0 | 2016-06-29 20:14:10 +0200 | [diff] [blame] | 728 | .type = sun6i_a31_phy, |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 729 | .disc_thresh = 3, |
| 730 | .phyctl_offset = REG_PHYCTL_A10, |
| 731 | .dedicated_clocks = true, |
| 732 | }; |
| 733 | |
| 734 | static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = { |
| 735 | .num_phys = 3, |
| 736 | .type = sun4i_a10_phy, |
| 737 | .disc_thresh = 2, |
| 738 | .phyctl_offset = REG_PHYCTL_A10, |
| 739 | .dedicated_clocks = false, |
| 740 | }; |
| 741 | |
| 742 | static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = { |
| 743 | .num_phys = 2, |
| 744 | .type = sun4i_a10_phy, |
| 745 | .disc_thresh = 3, |
| 746 | .phyctl_offset = REG_PHYCTL_A10, |
| 747 | .dedicated_clocks = true, |
| 748 | }; |
| 749 | |
| 750 | static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = { |
| 751 | .num_phys = 2, |
| 752 | .type = sun8i_a33_phy, |
| 753 | .disc_thresh = 3, |
| 754 | .phyctl_offset = REG_PHYCTL_A33, |
| 755 | .dedicated_clocks = true, |
| 756 | }; |
| 757 | |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 758 | static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = { |
| 759 | .num_phys = 4, |
| 760 | .type = sun8i_h3_phy, |
| 761 | .disc_thresh = 3, |
| 762 | .dedicated_clocks = true, |
| 763 | }; |
| 764 | |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 765 | static const struct of_device_id sun4i_usb_phy_of_match[] = { |
Hans de Goede | 68dbc2c | 2015-12-11 16:32:17 +0100 | [diff] [blame] | 766 | { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg }, |
| 767 | { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg }, |
| 768 | { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg }, |
| 769 | { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg }, |
| 770 | { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg }, |
| 771 | { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg }, |
Reinder de Haan | 626a630 | 2015-12-11 16:32:18 +0100 | [diff] [blame] | 772 | { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg }, |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 773 | { }, |
| 774 | }; |
| 775 | MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match); |
| 776 | |
| 777 | static struct platform_driver sun4i_usb_phy_driver = { |
| 778 | .probe = sun4i_usb_phy_probe, |
Hans de Goede | d233230 | 2015-06-13 14:37:45 +0200 | [diff] [blame] | 779 | .remove = sun4i_usb_phy_remove, |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 780 | .driver = { |
| 781 | .of_match_table = sun4i_usb_phy_of_match, |
| 782 | .name = "sun4i-usb-phy", |
Hans de Goede | ba4bdc9 | 2014-03-01 18:09:26 +0100 | [diff] [blame] | 783 | } |
| 784 | }; |
| 785 | module_platform_driver(sun4i_usb_phy_driver); |
| 786 | |
| 787 | MODULE_DESCRIPTION("Allwinner sun4i USB phy driver"); |
| 788 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 789 | MODULE_LICENSE("GPL v2"); |