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