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