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