]>
Commit | Line | Data |
---|---|---|
ba4bdc9e HG |
1 | /* |
2 | * Allwinner sun4i USB phy driver | |
3 | * | |
d2332303 | 4 | * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com> |
ba4bdc9e HG |
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> | |
1aedf3a7 | 25 | #include <linux/delay.h> |
2d84aff9 | 26 | #include <linux/err.h> |
1a52abe6 | 27 | #include <linux/extcon.h> |
ba4bdc9e | 28 | #include <linux/io.h> |
d2332303 | 29 | #include <linux/interrupt.h> |
ba4bdc9e HG |
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> | |
68dbc2ce | 35 | #include <linux/of_device.h> |
d2332303 | 36 | #include <linux/of_gpio.h> |
ba4bdc9e | 37 | #include <linux/phy/phy.h> |
24fe86a6 | 38 | #include <linux/phy/phy-sun4i-usb.h> |
ba4bdc9e | 39 | #include <linux/platform_device.h> |
8665c18b | 40 | #include <linux/power_supply.h> |
ba4bdc9e HG |
41 | #include <linux/regulator/consumer.h> |
42 | #include <linux/reset.h> | |
919ab252 | 43 | #include <linux/spinlock.h> |
b33ecca8 | 44 | #include <linux/usb/of.h> |
d2332303 | 45 | #include <linux/workqueue.h> |
ba4bdc9e HG |
46 | |
47 | #define REG_ISCR 0x00 | |
fc1f45ed | 48 | #define REG_PHYCTL_A10 0x04 |
ba4bdc9e HG |
49 | #define REG_PHYBIST 0x08 |
50 | #define REG_PHYTUNE 0x0c | |
fc1f45ed | 51 | #define REG_PHYCTL_A33 0x10 |
626a630e RH |
52 | #define REG_PHY_UNK_H3 0x20 |
53 | ||
b3e0d141 | 54 | #define REG_PMU_UNK1 0x10 |
ba4bdc9e HG |
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 | ||
d2332303 HG |
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 | ||
ba4bdc9e HG |
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 | |
24fe86a6 | 86 | #define PHY_SQUELCH_DETECT 0x3c |
ba4bdc9e | 87 | |
626a630e | 88 | #define MAX_PHYS 4 |
ba4bdc9e | 89 | |
d2332303 HG |
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 | ||
68dbc2ce HG |
97 | enum sun4i_usb_phy_type { |
98 | sun4i_a10_phy, | |
91d96f06 | 99 | sun6i_a31_phy, |
68dbc2ce | 100 | sun8i_a33_phy, |
626a630e | 101 | sun8i_h3_phy, |
16c40361 | 102 | sun8i_v3s_phy, |
b3e0d141 | 103 | sun50i_a64_phy, |
68dbc2ce HG |
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; | |
b3e0d141 | 112 | bool enable_pmu_unk1; |
68dbc2ce HG |
113 | }; |
114 | ||
ba4bdc9e | 115 | struct sun4i_usb_phy_data { |
ba4bdc9e | 116 | void __iomem *base; |
68dbc2ce | 117 | const struct sun4i_usb_phy_cfg *cfg; |
b33ecca8 | 118 | enum usb_dr_mode dr_mode; |
919ab252 | 119 | spinlock_t reg_lock; /* guard access to phyctl reg */ |
ba4bdc9e HG |
120 | struct sun4i_usb_phy { |
121 | struct phy *phy; | |
122 | void __iomem *pmu; | |
123 | struct regulator *vbus; | |
124 | struct reset_control *reset; | |
eadd4312 | 125 | struct clk *clk; |
d2332303 | 126 | bool regulator_on; |
ba4bdc9e HG |
127 | int index; |
128 | } phys[MAX_PHYS]; | |
d2332303 | 129 | /* phy0 / otg related variables */ |
1a52abe6 | 130 | struct extcon_dev *extcon; |
d2332303 | 131 | bool phy0_init; |
d2332303 HG |
132 | struct gpio_desc *id_det_gpio; |
133 | struct gpio_desc *vbus_det_gpio; | |
8665c18b HG |
134 | struct power_supply *vbus_power_supply; |
135 | struct notifier_block vbus_power_nb; | |
136 | bool vbus_power_nb_registered; | |
36f9159b | 137 | bool force_session_end; |
d2332303 HG |
138 | int id_det_irq; |
139 | int vbus_det_irq; | |
140 | int id_det; | |
141 | int vbus_det; | |
142 | struct delayed_work detect; | |
ba4bdc9e HG |
143 | }; |
144 | ||
145 | #define to_sun4i_usb_phy_data(phy) \ | |
146 | container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index]) | |
147 | ||
d2332303 HG |
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 | ||
ba4bdc9e HG |
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); | |
d99cb378 | 185 | void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset; |
919ab252 | 186 | unsigned long flags; |
ba4bdc9e HG |
187 | int i; |
188 | ||
919ab252 | 189 | spin_lock_irqsave(&phy_data->reg_lock, flags); |
ba4bdc9e | 190 | |
b3e0d141 | 191 | if (phy_data->cfg->type == sun8i_a33_phy || |
16c40361 IZ |
192 | phy_data->cfg->type == sun50i_a64_phy || |
193 | phy_data->cfg->type == sun8i_v3s_phy) { | |
b3e0d141 | 194 | /* A33 or A64 needs us to set phyctl to 0 explicitly */ |
fc1f45ed | 195 | writel(0, phyctl); |
fc1f45ed HG |
196 | } |
197 | ||
ba4bdc9e | 198 | for (i = 0; i < len; i++) { |
fc1f45ed | 199 | temp = readl(phyctl); |
ba4bdc9e HG |
200 | |
201 | /* clear the address portion */ | |
202 | temp &= ~(0xff << 8); | |
203 | ||
204 | /* set the address */ | |
205 | temp |= ((addr + i) << 8); | |
fc1f45ed | 206 | writel(temp, phyctl); |
ba4bdc9e HG |
207 | |
208 | /* set the data bit and clear usbc bit*/ | |
fc1f45ed | 209 | temp = readb(phyctl); |
ba4bdc9e HG |
210 | if (data & 0x1) |
211 | temp |= PHYCTL_DATA; | |
212 | else | |
213 | temp &= ~PHYCTL_DATA; | |
214 | temp &= ~usbc_bit; | |
fc1f45ed | 215 | writeb(temp, phyctl); |
ba4bdc9e HG |
216 | |
217 | /* pulse usbc_bit */ | |
fc1f45ed | 218 | temp = readb(phyctl); |
ba4bdc9e | 219 | temp |= usbc_bit; |
fc1f45ed | 220 | writeb(temp, phyctl); |
ba4bdc9e | 221 | |
fc1f45ed | 222 | temp = readb(phyctl); |
ba4bdc9e | 223 | temp &= ~usbc_bit; |
fc1f45ed | 224 | writeb(temp, phyctl); |
ba4bdc9e HG |
225 | |
226 | data >>= 1; | |
227 | } | |
919ab252 CYT |
228 | |
229 | spin_unlock_irqrestore(&phy_data->reg_lock, flags); | |
ba4bdc9e HG |
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; | |
626a630e | 257 | u32 val; |
ba4bdc9e | 258 | |
eadd4312 | 259 | ret = clk_prepare_enable(phy->clk); |
ba4bdc9e HG |
260 | if (ret) |
261 | return ret; | |
262 | ||
263 | ret = reset_control_deassert(phy->reset); | |
264 | if (ret) { | |
eadd4312 | 265 | clk_disable_unprepare(phy->clk); |
ba4bdc9e HG |
266 | return ret; |
267 | } | |
268 | ||
4320f9d4 | 269 | if (phy->pmu && data->cfg->enable_pmu_unk1) { |
b3e0d141 IZ |
270 | val = readl(phy->pmu + REG_PMU_UNK1); |
271 | writel(val & ~2, phy->pmu + REG_PMU_UNK1); | |
272 | } | |
273 | ||
626a630e RH |
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 | } | |
626a630e RH |
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); | |
6827a46f | 283 | |
626a630e RH |
284 | /* Adjust PHY's magnitude and rate */ |
285 | sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5); | |
ba4bdc9e | 286 | |
626a630e RH |
287 | /* Disconnect threshold adjustment */ |
288 | sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, | |
289 | data->cfg->disc_thresh, 2); | |
290 | } | |
ba4bdc9e HG |
291 | |
292 | sun4i_usb_phy_passby(phy, 1); | |
293 | ||
d2332303 HG |
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 | ||
b33ecca8 HG |
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); | |
d2332303 HG |
305 | } |
306 | ||
ba4bdc9e HG |
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); | |
d2332303 HG |
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 | } | |
ba4bdc9e HG |
321 | |
322 | sun4i_usb_phy_passby(phy, 0); | |
323 | reset_control_assert(phy->reset); | |
eadd4312 | 324 | clk_disable_unprepare(phy->clk); |
ba4bdc9e HG |
325 | |
326 | return 0; | |
327 | } | |
328 | ||
b33ecca8 HG |
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: | |
5f90d31c HG |
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 */ | |
b33ecca8 HG |
337 | case USB_DR_MODE_HOST: |
338 | return 0; | |
339 | case USB_DR_MODE_PERIPHERAL: | |
340 | default: | |
341 | return 1; | |
342 | } | |
343 | } | |
344 | ||
3d772c4a HG |
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 | ||
91d96f06 HG |
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 | ||
ba4bdc9e HG |
387 | static int sun4i_usb_phy_power_on(struct phy *_phy) |
388 | { | |
389 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); | |
d2332303 HG |
390 | struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy); |
391 | int ret; | |
392 | ||
393 | if (!phy->vbus || phy->regulator_on) | |
394 | return 0; | |
395 | ||
396 | /* For phy0 only turn on Vbus if we don't have an ext. Vbus */ | |
8083526e | 397 | if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) && |
91d6e3b6 HG |
398 | data->vbus_det) { |
399 | dev_warn(&_phy->dev, "External vbus detected, not enabling our own vbus\n"); | |
d2332303 | 400 | return 0; |
91d6e3b6 | 401 | } |
ba4bdc9e | 402 | |
d2332303 HG |
403 | ret = regulator_enable(phy->vbus); |
404 | if (ret) | |
405 | return ret; | |
ba4bdc9e | 406 | |
d2332303 | 407 | phy->regulator_on = true; |
ba4bdc9e | 408 | |
d2332303 | 409 | /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */ |
91d96f06 | 410 | if (phy->index == 0 && sun4i_usb_phy0_poll(data)) |
d2332303 | 411 | mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME); |
ba4bdc9e | 412 | |
d2332303 | 413 | return 0; |
ba4bdc9e HG |
414 | } |
415 | ||
416 | static int sun4i_usb_phy_power_off(struct phy *_phy) | |
417 | { | |
418 | struct sun4i_usb_phy *phy = phy_get_drvdata(_phy); | |
d2332303 HG |
419 | struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy); |
420 | ||
421 | if (!phy->vbus || !phy->regulator_on) | |
422 | return 0; | |
ba4bdc9e | 423 | |
d2332303 HG |
424 | regulator_disable(phy->vbus); |
425 | phy->regulator_on = false; | |
ba4bdc9e | 426 | |
d2332303 HG |
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 | */ | |
91d96f06 | 431 | if (phy->index == 0 && !sun4i_usb_phy0_poll(data)) |
d2332303 | 432 | mod_delayed_work(system_wq, &data->detect, POLL_TIME); |
ba4bdc9e HG |
433 | |
434 | return 0; | |
435 | } | |
436 | ||
6ba43c29 HG |
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); | |
5d04c883 | 441 | int new_mode; |
6ba43c29 HG |
442 | |
443 | if (phy->index != 0) | |
444 | return -EINVAL; | |
445 | ||
446 | switch (mode) { | |
447 | case PHY_MODE_USB_HOST: | |
5d04c883 | 448 | new_mode = USB_DR_MODE_HOST; |
6ba43c29 HG |
449 | break; |
450 | case PHY_MODE_USB_DEVICE: | |
5d04c883 | 451 | new_mode = USB_DR_MODE_PERIPHERAL; |
6ba43c29 HG |
452 | break; |
453 | case PHY_MODE_USB_OTG: | |
5d04c883 | 454 | new_mode = USB_DR_MODE_OTG; |
6ba43c29 HG |
455 | break; |
456 | default: | |
457 | return -EINVAL; | |
458 | } | |
459 | ||
5d04c883 HG |
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 */ | |
6ba43c29 HG |
466 | data->force_session_end = true; |
467 | queue_delayed_work(system_wq, &data->detect, 0); | |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
24fe86a6 HG |
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 | } | |
7167bf8b | 478 | EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect); |
24fe86a6 | 479 | |
4a9e5ca1 | 480 | static const struct phy_ops sun4i_usb_phy_ops = { |
ba4bdc9e HG |
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, | |
6ba43c29 | 485 | .set_mode = sun4i_usb_phy_set_mode, |
ba4bdc9e HG |
486 | .owner = THIS_MODULE, |
487 | }; | |
488 | ||
d2332303 HG |
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; | |
36f9159b | 494 | bool force_session_end, id_notify = false, vbus_notify = false; |
9745ceeb | 495 | int id_det, vbus_det; |
d2332303 | 496 | |
b33ecca8 HG |
497 | if (phy0 == NULL) |
498 | return; | |
499 | ||
500 | id_det = sun4i_usb_phy0_get_id_det(data); | |
8665c18b | 501 | vbus_det = sun4i_usb_phy0_get_vbus_det(data); |
d2332303 HG |
502 | |
503 | mutex_lock(&phy0->mutex); | |
504 | ||
505 | if (!data->phy0_init) { | |
506 | mutex_unlock(&phy0->mutex); | |
507 | return; | |
508 | } | |
509 | ||
36f9159b HG |
510 | force_session_end = data->force_session_end; |
511 | data->force_session_end = false; | |
512 | ||
d2332303 | 513 | if (id_det != data->id_det) { |
36f9159b | 514 | /* id-change, force session end if we've no vbus detection */ |
b33ecca8 | 515 | if (data->dr_mode == USB_DR_MODE_OTG && |
36f9159b HG |
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) { | |
1aedf3a7 HG |
521 | sun4i_usb_phy0_set_vbus_detect(phy0, 0); |
522 | msleep(200); | |
523 | sun4i_usb_phy0_set_vbus_detect(phy0, 1); | |
524 | } | |
d2332303 HG |
525 | sun4i_usb_phy0_set_id_detect(phy0, id_det); |
526 | data->id_det = id_det; | |
9745ceeb | 527 | id_notify = true; |
d2332303 HG |
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; | |
9745ceeb | 533 | vbus_notify = true; |
d2332303 HG |
534 | } |
535 | ||
536 | mutex_unlock(&phy0->mutex); | |
537 | ||
1aedf3a7 | 538 | if (id_notify) { |
66adb889 | 539 | extcon_set_state_sync(data->extcon, EXTCON_USB_HOST, |
1a52abe6 | 540 | !id_det); |
36f9159b HG |
541 | /* When leaving host mode force end the session here */ |
542 | if (force_session_end && id_det == 1) { | |
1aedf3a7 HG |
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 | } | |
1a52abe6 HG |
550 | |
551 | if (vbus_notify) | |
66adb889 | 552 | extcon_set_state_sync(data->extcon, EXTCON_USB, vbus_det); |
1a52abe6 | 553 | |
91d96f06 | 554 | if (sun4i_usb_phy0_poll(data)) |
d2332303 HG |
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 | ||
8665c18b HG |
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 | ||
ba4bdc9e HG |
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 | ||
5f90d31c | 587 | if (args->args[0] >= data->cfg->num_phys) |
ba4bdc9e HG |
588 | return ERR_PTR(-ENODEV); |
589 | ||
590 | return data->phys[args->args[0]].phy; | |
591 | } | |
592 | ||
d2332303 HG |
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 | ||
8665c18b HG |
598 | if (data->vbus_power_nb_registered) |
599 | power_supply_unreg_notifier(&data->vbus_power_nb); | |
04e59a02 | 600 | if (data->id_det_irq > 0) |
d2332303 | 601 | devm_free_irq(dev, data->id_det_irq, data); |
04e59a02 | 602 | if (data->vbus_det_irq > 0) |
d2332303 HG |
603 | devm_free_irq(dev, data->vbus_det_irq, data); |
604 | ||
605 | cancel_delayed_work_sync(&data->detect); | |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
1a52abe6 HG |
610 | static const unsigned int sun4i_usb_phy0_cable[] = { |
611 | EXTCON_USB, | |
612 | EXTCON_USB_HOST, | |
613 | EXTCON_NONE, | |
614 | }; | |
615 | ||
ba4bdc9e HG |
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; | |
ba4bdc9e | 621 | struct phy_provider *phy_provider; |
ba4bdc9e | 622 | struct resource *res; |
d2332303 | 623 | int i, ret; |
ba4bdc9e HG |
624 | |
625 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); | |
626 | if (!data) | |
627 | return -ENOMEM; | |
628 | ||
919ab252 | 629 | spin_lock_init(&data->reg_lock); |
d2332303 HG |
630 | INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan); |
631 | dev_set_drvdata(dev, data); | |
68dbc2ce HG |
632 | data->cfg = of_device_get_match_data(dev); |
633 | if (!data->cfg) | |
634 | return -EINVAL; | |
fc1f45ed | 635 | |
ba4bdc9e HG |
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 | ||
b2dfc34c AL |
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); | |
645 | ||
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); | |
d2332303 | 650 | |
8665c18b HG |
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 | ||
b33ecca8 | 661 | data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0); |
5f90d31c HG |
662 | |
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; | |
1a52abe6 HG |
671 | } |
672 | ||
5f90d31c | 673 | for (i = 0; i < data->cfg->num_phys; i++) { |
2a7f9982 MR |
674 | struct sun4i_usb_phy *phy = data->phys + i; |
675 | char name[16]; | |
676 | ||
ba4bdc9e | 677 | snprintf(name, sizeof(name), "usb%d_vbus", i); |
2a7f9982 MR |
678 | phy->vbus = devm_regulator_get_optional(dev, name); |
679 | if (IS_ERR(phy->vbus)) { | |
680 | if (PTR_ERR(phy->vbus) == -EPROBE_DEFER) | |
ba4bdc9e | 681 | return -EPROBE_DEFER; |
2a7f9982 | 682 | phy->vbus = NULL; |
ba4bdc9e HG |
683 | } |
684 | ||
68dbc2ce | 685 | if (data->cfg->dedicated_clocks) |
eadd4312 MR |
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 | ||
ba4bdc9e | 696 | snprintf(name, sizeof(name), "usb%d_reset", i); |
2a7f9982 MR |
697 | phy->reset = devm_reset_control_get(dev, name); |
698 | if (IS_ERR(phy->reset)) { | |
ba4bdc9e | 699 | dev_err(dev, "failed to get reset %s\n", name); |
2a7f9982 | 700 | return PTR_ERR(phy->reset); |
ba4bdc9e HG |
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); | |
2a7f9982 MR |
707 | phy->pmu = devm_ioremap_resource(dev, res); |
708 | if (IS_ERR(phy->pmu)) | |
709 | return PTR_ERR(phy->pmu); | |
ba4bdc9e HG |
710 | } |
711 | ||
dbc98635 | 712 | phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops); |
2a7f9982 | 713 | if (IS_ERR(phy->phy)) { |
ba4bdc9e | 714 | dev_err(dev, "failed to create PHY %d\n", i); |
2a7f9982 | 715 | return PTR_ERR(phy->phy); |
ba4bdc9e HG |
716 | } |
717 | ||
2a7f9982 MR |
718 | phy->index = i; |
719 | phy_set_drvdata(phy->phy, &data->phys[i]); | |
ba4bdc9e HG |
720 | } |
721 | ||
d2332303 | 722 | data->id_det_irq = gpiod_to_irq(data->id_det_gpio); |
5cf700ac | 723 | if (data->id_det_irq > 0) { |
d2332303 HG |
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 | ||
91d96f06 | 734 | data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio); |
5cf700ac | 735 | if (data->vbus_det_irq > 0) { |
d2332303 HG |
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 | ||
8665c18b HG |
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 | ||
ba4bdc9e | 759 | phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate); |
d2332303 HG |
760 | if (IS_ERR(phy_provider)) { |
761 | sun4i_usb_phy_remove(pdev); /* Stop detect work */ | |
762 | return PTR_ERR(phy_provider); | |
763 | } | |
ba4bdc9e | 764 | |
d2332303 | 765 | return 0; |
ba4bdc9e HG |
766 | } |
767 | ||
68dbc2ce HG |
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, | |
b3e0d141 | 774 | .enable_pmu_unk1 = false, |
68dbc2ce HG |
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, | |
b3e0d141 | 783 | .enable_pmu_unk1 = false, |
68dbc2ce HG |
784 | }; |
785 | ||
786 | static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = { | |
787 | .num_phys = 3, | |
91d96f06 | 788 | .type = sun6i_a31_phy, |
68dbc2ce HG |
789 | .disc_thresh = 3, |
790 | .phyctl_offset = REG_PHYCTL_A10, | |
791 | .dedicated_clocks = true, | |
b3e0d141 | 792 | .enable_pmu_unk1 = false, |
68dbc2ce HG |
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, | |
b3e0d141 | 801 | .enable_pmu_unk1 = false, |
68dbc2ce HG |
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, | |
b3e0d141 | 810 | .enable_pmu_unk1 = false, |
68dbc2ce HG |
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, | |
b3e0d141 | 819 | .enable_pmu_unk1 = false, |
68dbc2ce HG |
820 | }; |
821 | ||
626a630e RH |
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, | |
b3e0d141 IZ |
827 | .enable_pmu_unk1 = true, |
828 | }; | |
829 | ||
16c40361 IZ |
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 | ||
b3e0d141 IZ |
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, | |
626a630e RH |
846 | }; |
847 | ||
ba4bdc9e | 848 | static const struct of_device_id sun4i_usb_phy_of_match[] = { |
68dbc2ce HG |
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 }, | |
626a630e | 855 | { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg }, |
16c40361 | 856 | { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = &sun8i_v3s_cfg }, |
b3e0d141 IZ |
857 | { .compatible = "allwinner,sun50i-a64-usb-phy", |
858 | .data = &sun50i_a64_cfg}, | |
ba4bdc9e HG |
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, | |
d2332303 | 865 | .remove = sun4i_usb_phy_remove, |
ba4bdc9e HG |
866 | .driver = { |
867 | .of_match_table = sun4i_usb_phy_of_match, | |
868 | .name = "sun4i-usb-phy", | |
ba4bdc9e HG |
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"); |