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