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