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