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