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