blob: 1f8809bab002cd56cce290ae1ca0d54a85a9432f [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Allwinner sun4i USB phy driver
3 *
4 * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com>
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>
25#include <linux/delay.h>
26#include <linux/err.h>
27#include <linux/extcon-provider.h>
28#include <linux/io.h>
29#include <linux/interrupt.h>
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>
35#include <linux/of_device.h>
36#include <linux/of_gpio.h>
37#include <linux/phy/phy.h>
38#include <linux/phy/phy-sun4i-usb.h>
39#include <linux/platform_device.h>
40#include <linux/power_supply.h>
41#include <linux/regulator/consumer.h>
42#include <linux/reset.h>
43#include <linux/spinlock.h>
44#include <linux/usb/of.h>
45#include <linux/workqueue.h>
46
47#define REG_ISCR 0x00
48#define REG_PHYCTL_A10 0x04
49#define REG_PHYBIST 0x08
50#define REG_PHYTUNE 0x0c
51#define REG_PHYCTL_A33 0x10
52#define REG_PHY_OTGCTL 0x20
53
54#define REG_PMU_UNK1 0x10
55
56#define PHYCTL_DATA BIT(7)
57
58#define OTGCTL_ROUTE_MUSB BIT(0)
59
60#define SUNXI_AHB_ICHR8_EN BIT(10)
61#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
62#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
63#define SUNXI_ULPI_BYPASS_EN BIT(0)
64
65/* ISCR, Interface Status and Control bits */
66#define ISCR_ID_PULLUP_EN (1 << 17)
67#define ISCR_DPDM_PULLUP_EN (1 << 16)
68/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
69#define ISCR_FORCE_ID_MASK (3 << 14)
70#define ISCR_FORCE_ID_LOW (2 << 14)
71#define ISCR_FORCE_ID_HIGH (3 << 14)
72#define ISCR_FORCE_VBUS_MASK (3 << 12)
73#define ISCR_FORCE_VBUS_LOW (2 << 12)
74#define ISCR_FORCE_VBUS_HIGH (3 << 12)
75
76/* Common Control Bits for Both PHYs */
77#define PHY_PLL_BW 0x03
78#define PHY_RES45_CAL_EN 0x0c
79
80/* Private Control Bits for Each PHY */
81#define PHY_TX_AMPLITUDE_TUNE 0x20
82#define PHY_TX_SLEWRATE_TUNE 0x22
83#define PHY_VBUSVALID_TH_SEL 0x25
84#define PHY_PULLUP_RES_SEL 0x27
85#define PHY_OTG_FUNC_EN 0x28
86#define PHY_VBUS_DET_EN 0x29
87#define PHY_DISCON_TH_SEL 0x2a
88#define PHY_SQUELCH_DETECT 0x3c
89
90/* A83T specific control bits for PHY0 */
91#define PHY_CTL_VBUSVLDEXT BIT(5)
92#define PHY_CTL_SIDDQ BIT(3)
93
94/* A83T specific control bits for PHY2 HSIC */
95#define SUNXI_EHCI_HS_FORCE BIT(20)
96#define SUNXI_HSIC_CONNECT_DET BIT(17)
97#define SUNXI_HSIC_CONNECT_INT BIT(16)
98#define SUNXI_HSIC BIT(1)
99
100#define MAX_PHYS 4
101
102/*
103 * Note do not raise the debounce time, we must report Vusb high within 100ms
104 * otherwise we get Vbus errors
105 */
106#define DEBOUNCE_TIME msecs_to_jiffies(50)
107#define POLL_TIME msecs_to_jiffies(250)
108
109enum sun4i_usb_phy_type {
110 sun4i_a10_phy,
111 sun6i_a31_phy,
112 sun8i_a33_phy,
113 sun8i_a83t_phy,
114 sun8i_h3_phy,
115 sun8i_r40_phy,
116 sun8i_v3s_phy,
117 sun50i_a64_phy,
118};
119
120struct sun4i_usb_phy_cfg {
121 int num_phys;
122 int hsic_index;
123 enum sun4i_usb_phy_type type;
124 u32 disc_thresh;
125 u8 phyctl_offset;
126 bool dedicated_clocks;
127 bool enable_pmu_unk1;
128 bool phy0_dual_route;
129 int missing_phys;
130};
131
132struct sun4i_usb_phy_data {
133 void __iomem *base;
134 const struct sun4i_usb_phy_cfg *cfg;
135 enum usb_dr_mode dr_mode;
136 spinlock_t reg_lock; /* guard access to phyctl reg */
137 struct sun4i_usb_phy {
138 struct phy *phy;
139 void __iomem *pmu;
140 struct regulator *vbus;
141 struct reset_control *reset;
142 struct clk *clk;
143 struct clk *clk2;
144 bool regulator_on;
145 int index;
146 } phys[MAX_PHYS];
147 /* phy0 / otg related variables */
148 struct extcon_dev *extcon;
149 bool phy0_init;
150 struct gpio_desc *id_det_gpio;
151 struct gpio_desc *vbus_det_gpio;
152 struct power_supply *vbus_power_supply;
153 struct notifier_block vbus_power_nb;
154 bool vbus_power_nb_registered;
155 bool force_session_end;
156 int id_det_irq;
157 int vbus_det_irq;
158 int id_det;
159 int vbus_det;
160 struct delayed_work detect;
161};
162
163#define to_sun4i_usb_phy_data(phy) \
164 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
165
166static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
167{
168 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
169 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
170 u32 iscr;
171
172 iscr = readl(data->base + REG_ISCR);
173 iscr &= ~clr;
174 iscr |= set;
175 writel(iscr, data->base + REG_ISCR);
176}
177
178static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
179{
180 if (val)
181 val = ISCR_FORCE_ID_HIGH;
182 else
183 val = ISCR_FORCE_ID_LOW;
184
185 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
186}
187
188static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
189{
190 if (val)
191 val = ISCR_FORCE_VBUS_HIGH;
192 else
193 val = ISCR_FORCE_VBUS_LOW;
194
195 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
196}
197
198static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
199 int len)
200{
201 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
202 u32 temp, usbc_bit = BIT(phy->index * 2);
203 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
204 unsigned long flags;
205 int i;
206
207 spin_lock_irqsave(&phy_data->reg_lock, flags);
208
209 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
210 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
211 writel(0, phyctl);
212 }
213
214 for (i = 0; i < len; i++) {
215 temp = readl(phyctl);
216
217 /* clear the address portion */
218 temp &= ~(0xff << 8);
219
220 /* set the address */
221 temp |= ((addr + i) << 8);
222 writel(temp, phyctl);
223
224 /* set the data bit and clear usbc bit*/
225 temp = readb(phyctl);
226 if (data & 0x1)
227 temp |= PHYCTL_DATA;
228 else
229 temp &= ~PHYCTL_DATA;
230 temp &= ~usbc_bit;
231 writeb(temp, phyctl);
232
233 /* pulse usbc_bit */
234 temp = readb(phyctl);
235 temp |= usbc_bit;
236 writeb(temp, phyctl);
237
238 temp = readb(phyctl);
239 temp &= ~usbc_bit;
240 writeb(temp, phyctl);
241
242 data >>= 1;
243 }
244
245 spin_unlock_irqrestore(&phy_data->reg_lock, flags);
246}
247
248static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
249{
250 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
251 u32 bits, reg_value;
252
253 if (!phy->pmu)
254 return;
255
256 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
257 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
258
259 /* A83T USB2 is HSIC */
260 if (phy_data->cfg->type == sun8i_a83t_phy && phy->index == 2)
261 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
262 SUNXI_HSIC;
263
264 reg_value = readl(phy->pmu);
265
266 if (enable)
267 reg_value |= bits;
268 else
269 reg_value &= ~bits;
270
271 writel(reg_value, phy->pmu);
272}
273
274static int sun4i_usb_phy_init(struct phy *_phy)
275{
276 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
277 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
278 int ret;
279 u32 val;
280
281 ret = clk_prepare_enable(phy->clk);
282 if (ret)
283 return ret;
284
285 ret = clk_prepare_enable(phy->clk2);
286 if (ret) {
287 clk_disable_unprepare(phy->clk);
288 return ret;
289 }
290
291 ret = reset_control_deassert(phy->reset);
292 if (ret) {
293 clk_disable_unprepare(phy->clk2);
294 clk_disable_unprepare(phy->clk);
295 return ret;
296 }
297
298 if (data->cfg->type == sun8i_a83t_phy) {
299 if (phy->index == 0) {
300 val = readl(data->base + data->cfg->phyctl_offset);
301 val |= PHY_CTL_VBUSVLDEXT;
302 val &= ~PHY_CTL_SIDDQ;
303 writel(val, data->base + data->cfg->phyctl_offset);
304 }
305 } else {
306 if (phy->pmu && data->cfg->enable_pmu_unk1) {
307 val = readl(phy->pmu + REG_PMU_UNK1);
308 writel(val & ~2, phy->pmu + REG_PMU_UNK1);
309 }
310
311 /* Enable USB 45 Ohm resistor calibration */
312 if (phy->index == 0)
313 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
314
315 /* Adjust PHY's magnitude and rate */
316 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
317
318 /* Disconnect threshold adjustment */
319 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
320 data->cfg->disc_thresh, 2);
321 }
322
323 sun4i_usb_phy_passby(phy, 1);
324
325 if (phy->index == 0) {
326 data->phy0_init = true;
327
328 /* Enable pull-ups */
329 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
330 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
331
332 /* Force ISCR and cable state updates */
333 data->id_det = -1;
334 data->vbus_det = -1;
335 queue_delayed_work(system_wq, &data->detect, 0);
336 }
337
338 return 0;
339}
340
341static int sun4i_usb_phy_exit(struct phy *_phy)
342{
343 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
344 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
345
346 if (phy->index == 0) {
347 if (data->cfg->type == sun8i_a83t_phy) {
348 void __iomem *phyctl = data->base +
349 data->cfg->phyctl_offset;
350
351 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
352 }
353
354 /* Disable pull-ups */
355 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
356 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
357 data->phy0_init = false;
358 }
359
360 sun4i_usb_phy_passby(phy, 0);
361 reset_control_assert(phy->reset);
362 clk_disable_unprepare(phy->clk2);
363 clk_disable_unprepare(phy->clk);
364
365 return 0;
366}
367
368static int sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data *data)
369{
370 switch (data->dr_mode) {
371 case USB_DR_MODE_OTG:
372 if (data->id_det_gpio)
373 return gpiod_get_value_cansleep(data->id_det_gpio);
374 else
375 return 1; /* Fallback to peripheral mode */
376 case USB_DR_MODE_HOST:
377 return 0;
378 case USB_DR_MODE_PERIPHERAL:
379 default:
380 return 1;
381 }
382}
383
384static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
385{
386 if (data->vbus_det_gpio)
387 return gpiod_get_value_cansleep(data->vbus_det_gpio);
388
389 if (data->vbus_power_supply) {
390 union power_supply_propval val;
391 int r;
392
393 r = power_supply_get_property(data->vbus_power_supply,
394 POWER_SUPPLY_PROP_PRESENT, &val);
395 if (r == 0)
396 return val.intval;
397 }
398
399 /* Fallback: report vbus as high */
400 return 1;
401}
402
403static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
404{
405 return data->vbus_det_gpio || data->vbus_power_supply;
406}
407
408static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
409{
410 if ((data->id_det_gpio && data->id_det_irq <= 0) ||
411 (data->vbus_det_gpio && data->vbus_det_irq <= 0))
412 return true;
413
414 /*
415 * The A31/A23/A33 companion pmics (AXP221/AXP223) do not
416 * generate vbus change interrupts when the board is driving
417 * vbus using the N_VBUSEN pin on the pmic, so we must poll
418 * when using the pmic for vbus-det _and_ we're driving vbus.
419 */
420 if ((data->cfg->type == sun6i_a31_phy ||
421 data->cfg->type == sun8i_a33_phy) &&
422 data->vbus_power_supply && data->phys[0].regulator_on)
423 return true;
424
425 return false;
426}
427
428static int sun4i_usb_phy_power_on(struct phy *_phy)
429{
430 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
431 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
432 int ret;
433
434 if (!phy->vbus || phy->regulator_on)
435 return 0;
436
437 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
438 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
439 data->vbus_det) {
440 dev_warn(&_phy->dev, "External vbus detected, not enabling our own vbus\n");
441 return 0;
442 }
443
444 ret = regulator_enable(phy->vbus);
445 if (ret)
446 return ret;
447
448 phy->regulator_on = true;
449
450 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
451 if (phy->index == 0 && sun4i_usb_phy0_poll(data))
452 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
453
454 return 0;
455}
456
457static int sun4i_usb_phy_power_off(struct phy *_phy)
458{
459 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
460 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
461
462 if (!phy->vbus || !phy->regulator_on)
463 return 0;
464
465 regulator_disable(phy->vbus);
466 phy->regulator_on = false;
467
468 /*
469 * phy0 vbus typically slowly discharges, sometimes this causes the
470 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
471 */
472 if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
473 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
474
475 return 0;
476}
477
478static int sun4i_usb_phy_set_mode(struct phy *_phy, enum phy_mode mode)
479{
480 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
481 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
482 int new_mode;
483
484 if (phy->index != 0) {
485 if (mode == PHY_MODE_USB_HOST)
486 return 0;
487 return -EINVAL;
488 }
489
490 switch (mode) {
491 case PHY_MODE_USB_HOST:
492 new_mode = USB_DR_MODE_HOST;
493 break;
494 case PHY_MODE_USB_DEVICE:
495 new_mode = USB_DR_MODE_PERIPHERAL;
496 break;
497 case PHY_MODE_USB_OTG:
498 new_mode = USB_DR_MODE_OTG;
499 break;
500 default:
501 return -EINVAL;
502 }
503
504 if (new_mode != data->dr_mode) {
505 dev_info(&_phy->dev, "Changing dr_mode to %d\n", new_mode);
506 data->dr_mode = new_mode;
507 }
508
509 data->id_det = -1; /* Force reprocessing of id */
510 data->force_session_end = true;
511 queue_delayed_work(system_wq, &data->detect, 0);
512
513 return 0;
514}
515
516void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
517{
518 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
519
520 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
521}
522EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
523
524static const struct phy_ops sun4i_usb_phy_ops = {
525 .init = sun4i_usb_phy_init,
526 .exit = sun4i_usb_phy_exit,
527 .power_on = sun4i_usb_phy_power_on,
528 .power_off = sun4i_usb_phy_power_off,
529 .set_mode = sun4i_usb_phy_set_mode,
530 .owner = THIS_MODULE,
531};
532
533static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, int id_det)
534{
535 u32 regval;
536
537 regval = readl(data->base + REG_PHY_OTGCTL);
538 if (id_det == 0) {
539 /* Host mode. Route phy0 to EHCI/OHCI */
540 regval &= ~OTGCTL_ROUTE_MUSB;
541 } else {
542 /* Peripheral mode. Route phy0 to MUSB */
543 regval |= OTGCTL_ROUTE_MUSB;
544 }
545 writel(regval, data->base + REG_PHY_OTGCTL);
546}
547
548static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
549{
550 struct sun4i_usb_phy_data *data =
551 container_of(work, struct sun4i_usb_phy_data, detect.work);
552 struct phy *phy0 = data->phys[0].phy;
553 struct sun4i_usb_phy *phy = phy_get_drvdata(phy0);
554 bool force_session_end, id_notify = false, vbus_notify = false;
555 int id_det, vbus_det;
556
557 if (phy0 == NULL)
558 return;
559
560 id_det = sun4i_usb_phy0_get_id_det(data);
561 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
562
563 mutex_lock(&phy0->mutex);
564
565 if (!data->phy0_init) {
566 mutex_unlock(&phy0->mutex);
567 return;
568 }
569
570 force_session_end = data->force_session_end;
571 data->force_session_end = false;
572
573 if (id_det != data->id_det) {
574 /* id-change, force session end if we've no vbus detection */
575 if (data->dr_mode == USB_DR_MODE_OTG &&
576 !sun4i_usb_phy0_have_vbus_det(data))
577 force_session_end = true;
578
579 /* When entering host mode (id = 0) force end the session now */
580 if (force_session_end && id_det == 0) {
581 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
582 msleep(200);
583 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
584 }
585 sun4i_usb_phy0_set_id_detect(phy0, id_det);
586 data->id_det = id_det;
587 id_notify = true;
588 }
589
590 if (vbus_det != data->vbus_det) {
591 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
592 data->vbus_det = vbus_det;
593 vbus_notify = true;
594 }
595
596 mutex_unlock(&phy0->mutex);
597
598 if (id_notify) {
599 extcon_set_state_sync(data->extcon, EXTCON_USB_HOST,
600 !id_det);
601 /* When leaving host mode force end the session here */
602 if (force_session_end && id_det == 1) {
603 mutex_lock(&phy0->mutex);
604 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
605 msleep(1000);
606 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
607 mutex_unlock(&phy0->mutex);
608 }
609
610 /* Enable PHY0 passby for host mode only. */
611 sun4i_usb_phy_passby(phy, !id_det);
612
613 /* Re-route PHY0 if necessary */
614 if (data->cfg->phy0_dual_route)
615 sun4i_usb_phy0_reroute(data, id_det);
616 }
617
618 if (vbus_notify)
619 extcon_set_state_sync(data->extcon, EXTCON_USB, vbus_det);
620
621 if (sun4i_usb_phy0_poll(data))
622 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
623}
624
625static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
626{
627 struct sun4i_usb_phy_data *data = dev_id;
628
629 /* vbus or id changed, let the pins settle and then scan them */
630 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
631
632 return IRQ_HANDLED;
633}
634
635static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
636 unsigned long val, void *v)
637{
638 struct sun4i_usb_phy_data *data =
639 container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
640 struct power_supply *psy = v;
641
642 /* Properties on the vbus_power_supply changed, scan vbus_det */
643 if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
644 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
645
646 return NOTIFY_OK;
647}
648
649static struct phy *sun4i_usb_phy_xlate(struct device *dev,
650 struct of_phandle_args *args)
651{
652 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
653
654 if (args->args[0] >= data->cfg->num_phys)
655 return ERR_PTR(-ENODEV);
656
657 if (data->cfg->missing_phys & BIT(args->args[0]))
658 return ERR_PTR(-ENODEV);
659
660 return data->phys[args->args[0]].phy;
661}
662
663static int sun4i_usb_phy_remove(struct platform_device *pdev)
664{
665 struct device *dev = &pdev->dev;
666 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
667
668 if (data->vbus_power_nb_registered)
669 power_supply_unreg_notifier(&data->vbus_power_nb);
670 if (data->id_det_irq > 0)
671 devm_free_irq(dev, data->id_det_irq, data);
672 if (data->vbus_det_irq > 0)
673 devm_free_irq(dev, data->vbus_det_irq, data);
674
675 cancel_delayed_work_sync(&data->detect);
676
677 return 0;
678}
679
680static const unsigned int sun4i_usb_phy0_cable[] = {
681 EXTCON_USB,
682 EXTCON_USB_HOST,
683 EXTCON_NONE,
684};
685
686static int sun4i_usb_phy_probe(struct platform_device *pdev)
687{
688 struct sun4i_usb_phy_data *data;
689 struct device *dev = &pdev->dev;
690 struct device_node *np = dev->of_node;
691 struct phy_provider *phy_provider;
692 struct resource *res;
693 int i, ret;
694
695 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
696 if (!data)
697 return -ENOMEM;
698
699 spin_lock_init(&data->reg_lock);
700 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
701 dev_set_drvdata(dev, data);
702 data->cfg = of_device_get_match_data(dev);
703 if (!data->cfg)
704 return -EINVAL;
705
706 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
707 data->base = devm_ioremap_resource(dev, res);
708 if (IS_ERR(data->base))
709 return PTR_ERR(data->base);
710
711 data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
712 GPIOD_IN);
713 if (IS_ERR(data->id_det_gpio)) {
714 dev_err(dev, "Couldn't request ID GPIO\n");
715 return PTR_ERR(data->id_det_gpio);
716 }
717
718 data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
719 GPIOD_IN);
720 if (IS_ERR(data->vbus_det_gpio)) {
721 dev_err(dev, "Couldn't request VBUS detect GPIO\n");
722 return PTR_ERR(data->vbus_det_gpio);
723 }
724
725 if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
726 data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
727 "usb0_vbus_power-supply");
728 if (IS_ERR(data->vbus_power_supply)) {
729 dev_err(dev, "Couldn't get the VBUS power supply\n");
730 return PTR_ERR(data->vbus_power_supply);
731 }
732
733 if (!data->vbus_power_supply)
734 return -EPROBE_DEFER;
735 }
736
737 data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
738
739 data->extcon = devm_extcon_dev_allocate(dev, sun4i_usb_phy0_cable);
740 if (IS_ERR(data->extcon)) {
741 dev_err(dev, "Couldn't allocate our extcon device\n");
742 return PTR_ERR(data->extcon);
743 }
744
745 ret = devm_extcon_dev_register(dev, data->extcon);
746 if (ret) {
747 dev_err(dev, "failed to register extcon: %d\n", ret);
748 return ret;
749 }
750
751 for (i = 0; i < data->cfg->num_phys; i++) {
752 struct sun4i_usb_phy *phy = data->phys + i;
753 char name[16];
754
755 if (data->cfg->missing_phys & BIT(i))
756 continue;
757
758 snprintf(name, sizeof(name), "usb%d_vbus", i);
759 phy->vbus = devm_regulator_get_optional(dev, name);
760 if (IS_ERR(phy->vbus)) {
761 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER) {
762 dev_err(dev,
763 "Couldn't get regulator %s... Deferring probe\n",
764 name);
765 return -EPROBE_DEFER;
766 }
767
768 phy->vbus = NULL;
769 }
770
771 if (data->cfg->dedicated_clocks)
772 snprintf(name, sizeof(name), "usb%d_phy", i);
773 else
774 strlcpy(name, "usb_phy", sizeof(name));
775
776 phy->clk = devm_clk_get(dev, name);
777 if (IS_ERR(phy->clk)) {
778 dev_err(dev, "failed to get clock %s\n", name);
779 return PTR_ERR(phy->clk);
780 }
781
782 /* The first PHY is always tied to OTG, and never HSIC */
783 if (data->cfg->hsic_index && i == data->cfg->hsic_index) {
784 /* HSIC needs secondary clock */
785 snprintf(name, sizeof(name), "usb%d_hsic_12M", i);
786 phy->clk2 = devm_clk_get(dev, name);
787 if (IS_ERR(phy->clk2)) {
788 dev_err(dev, "failed to get clock %s\n", name);
789 return PTR_ERR(phy->clk2);
790 }
791 }
792
793 snprintf(name, sizeof(name), "usb%d_reset", i);
794 phy->reset = devm_reset_control_get(dev, name);
795 if (IS_ERR(phy->reset)) {
796 dev_err(dev, "failed to get reset %s\n", name);
797 return PTR_ERR(phy->reset);
798 }
799
800 if (i || data->cfg->phy0_dual_route) { /* No pmu for musb */
801 snprintf(name, sizeof(name), "pmu%d", i);
802 res = platform_get_resource_byname(pdev,
803 IORESOURCE_MEM, name);
804 phy->pmu = devm_ioremap_resource(dev, res);
805 if (IS_ERR(phy->pmu))
806 return PTR_ERR(phy->pmu);
807 }
808
809 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
810 if (IS_ERR(phy->phy)) {
811 dev_err(dev, "failed to create PHY %d\n", i);
812 return PTR_ERR(phy->phy);
813 }
814
815 phy->index = i;
816 phy_set_drvdata(phy->phy, &data->phys[i]);
817 }
818
819 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
820 if (data->id_det_irq > 0) {
821 ret = devm_request_irq(dev, data->id_det_irq,
822 sun4i_usb_phy0_id_vbus_det_irq,
823 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
824 "usb0-id-det", data);
825 if (ret) {
826 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
827 return ret;
828 }
829 }
830
831 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
832 if (data->vbus_det_irq > 0) {
833 ret = devm_request_irq(dev, data->vbus_det_irq,
834 sun4i_usb_phy0_id_vbus_det_irq,
835 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
836 "usb0-vbus-det", data);
837 if (ret) {
838 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
839 data->vbus_det_irq = -1;
840 sun4i_usb_phy_remove(pdev); /* Stop detect work */
841 return ret;
842 }
843 }
844
845 if (data->vbus_power_supply) {
846 data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
847 data->vbus_power_nb.priority = 0;
848 ret = power_supply_reg_notifier(&data->vbus_power_nb);
849 if (ret) {
850 sun4i_usb_phy_remove(pdev); /* Stop detect work */
851 return ret;
852 }
853 data->vbus_power_nb_registered = true;
854 }
855
856 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
857 if (IS_ERR(phy_provider)) {
858 sun4i_usb_phy_remove(pdev); /* Stop detect work */
859 return PTR_ERR(phy_provider);
860 }
861
862 dev_dbg(dev, "successfully loaded\n");
863
864 return 0;
865}
866
867static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
868 .num_phys = 3,
869 .type = sun4i_a10_phy,
870 .disc_thresh = 3,
871 .phyctl_offset = REG_PHYCTL_A10,
872 .dedicated_clocks = false,
873 .enable_pmu_unk1 = false,
874};
875
876static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
877 .num_phys = 2,
878 .type = sun4i_a10_phy,
879 .disc_thresh = 2,
880 .phyctl_offset = REG_PHYCTL_A10,
881 .dedicated_clocks = false,
882 .enable_pmu_unk1 = false,
883};
884
885static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
886 .num_phys = 3,
887 .type = sun6i_a31_phy,
888 .disc_thresh = 3,
889 .phyctl_offset = REG_PHYCTL_A10,
890 .dedicated_clocks = true,
891 .enable_pmu_unk1 = false,
892};
893
894static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
895 .num_phys = 3,
896 .type = sun4i_a10_phy,
897 .disc_thresh = 2,
898 .phyctl_offset = REG_PHYCTL_A10,
899 .dedicated_clocks = false,
900 .enable_pmu_unk1 = false,
901};
902
903static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
904 .num_phys = 2,
905 .type = sun6i_a31_phy,
906 .disc_thresh = 3,
907 .phyctl_offset = REG_PHYCTL_A10,
908 .dedicated_clocks = true,
909 .enable_pmu_unk1 = false,
910};
911
912static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
913 .num_phys = 2,
914 .type = sun8i_a33_phy,
915 .disc_thresh = 3,
916 .phyctl_offset = REG_PHYCTL_A33,
917 .dedicated_clocks = true,
918 .enable_pmu_unk1 = false,
919};
920
921static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
922 .num_phys = 3,
923 .hsic_index = 2,
924 .type = sun8i_a83t_phy,
925 .phyctl_offset = REG_PHYCTL_A33,
926 .dedicated_clocks = true,
927};
928
929static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
930 .num_phys = 4,
931 .type = sun8i_h3_phy,
932 .disc_thresh = 3,
933 .phyctl_offset = REG_PHYCTL_A33,
934 .dedicated_clocks = true,
935 .enable_pmu_unk1 = true,
936 .phy0_dual_route = true,
937};
938
939static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
940 .num_phys = 3,
941 .type = sun8i_r40_phy,
942 .disc_thresh = 3,
943 .phyctl_offset = REG_PHYCTL_A33,
944 .dedicated_clocks = true,
945 .enable_pmu_unk1 = true,
946 .phy0_dual_route = true,
947};
948
949static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
950 .num_phys = 1,
951 .type = sun8i_v3s_phy,
952 .disc_thresh = 3,
953 .phyctl_offset = REG_PHYCTL_A33,
954 .dedicated_clocks = true,
955 .enable_pmu_unk1 = true,
956 .phy0_dual_route = true,
957};
958
959static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
960 .num_phys = 2,
961 .type = sun50i_a64_phy,
962 .disc_thresh = 3,
963 .phyctl_offset = REG_PHYCTL_A33,
964 .dedicated_clocks = true,
965 .enable_pmu_unk1 = true,
966 .phy0_dual_route = true,
967};
968
969static const struct of_device_id sun4i_usb_phy_of_match[] = {
970 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
971 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
972 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
973 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
974 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
975 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
976 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = &sun8i_a83t_cfg },
977 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
978 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = &sun8i_r40_cfg },
979 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = &sun8i_v3s_cfg },
980 { .compatible = "allwinner,sun50i-a64-usb-phy",
981 .data = &sun50i_a64_cfg},
982 { },
983};
984MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
985
986static struct platform_driver sun4i_usb_phy_driver = {
987 .probe = sun4i_usb_phy_probe,
988 .remove = sun4i_usb_phy_remove,
989 .driver = {
990 .of_match_table = sun4i_usb_phy_of_match,
991 .name = "sun4i-usb-phy",
992 }
993};
994module_platform_driver(sun4i_usb_phy_driver);
995
996MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
997MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
998MODULE_LICENSE("GPL v2");