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