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