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