8c7eb335622ee1781a6b83d155740153b00d4717
[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/usb/of.h>
44 #include <linux/workqueue.h>
45
46 #define REG_ISCR 0x00
47 #define REG_PHYCTL_A10 0x04
48 #define REG_PHYBIST 0x08
49 #define REG_PHYTUNE 0x0c
50 #define REG_PHYCTL_A33 0x10
51 #define REG_PHY_UNK_H3 0x20
52
53 #define REG_PMU_UNK_H3 0x10
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
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
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
85 #define PHY_SQUELCH_DETECT 0x3c
86
87 #define MAX_PHYS 4
88
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
96 enum sun4i_usb_phy_type {
97 sun4i_a10_phy,
98 sun6i_a31_phy,
99 sun8i_a33_phy,
100 sun8i_h3_phy,
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
111 struct sun4i_usb_phy_data {
112 void __iomem *base;
113 const struct sun4i_usb_phy_cfg *cfg;
114 enum usb_dr_mode dr_mode;
115 struct mutex mutex;
116 struct sun4i_usb_phy {
117 struct phy *phy;
118 void __iomem *pmu;
119 struct regulator *vbus;
120 struct reset_control *reset;
121 struct clk *clk;
122 bool regulator_on;
123 int index;
124 } phys[MAX_PHYS];
125 int first_phy;
126 /* phy0 / otg related variables */
127 struct extcon_dev *extcon;
128 bool phy0_init;
129 struct gpio_desc *id_det_gpio;
130 struct gpio_desc *vbus_det_gpio;
131 struct power_supply *vbus_power_supply;
132 struct notifier_block vbus_power_nb;
133 bool vbus_power_nb_registered;
134 int id_det_irq;
135 int vbus_det_irq;
136 int id_det;
137 int vbus_det;
138 struct delayed_work detect;
139 };
140
141 #define to_sun4i_usb_phy_data(phy) \
142 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
143
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
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);
181 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
182 int i;
183
184 mutex_lock(&phy_data->mutex);
185
186 if (phy_data->cfg->type == sun8i_a33_phy) {
187 /* A33 needs us to set phyctl to 0 explicitly */
188 writel(0, phyctl);
189 }
190
191 for (i = 0; i < len; i++) {
192 temp = readl(phyctl);
193
194 /* clear the address portion */
195 temp &= ~(0xff << 8);
196
197 /* set the address */
198 temp |= ((addr + i) << 8);
199 writel(temp, phyctl);
200
201 /* set the data bit and clear usbc bit*/
202 temp = readb(phyctl);
203 if (data & 0x1)
204 temp |= PHYCTL_DATA;
205 else
206 temp &= ~PHYCTL_DATA;
207 temp &= ~usbc_bit;
208 writeb(temp, phyctl);
209
210 /* pulse usbc_bit */
211 temp = readb(phyctl);
212 temp |= usbc_bit;
213 writeb(temp, phyctl);
214
215 temp = readb(phyctl);
216 temp &= ~usbc_bit;
217 writeb(temp, phyctl);
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;
249 u32 val;
250
251 ret = clk_prepare_enable(phy->clk);
252 if (ret)
253 return ret;
254
255 ret = reset_control_deassert(phy->reset);
256 if (ret) {
257 clk_disable_unprepare(phy->clk);
258 return ret;
259 }
260
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);
273
274 /* Adjust PHY's magnitude and rate */
275 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
276
277 /* Disconnect threshold adjustment */
278 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
279 data->cfg->disc_thresh, 2);
280 }
281
282 sun4i_usb_phy_passby(phy, 1);
283
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
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);
295 }
296
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);
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 }
311
312 sun4i_usb_phy_passby(phy, 0);
313 reset_control_assert(phy->reset);
314 clk_disable_unprepare(phy->clk);
315
316 return 0;
317 }
318
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
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
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
374 static int sun4i_usb_phy_power_on(struct phy *_phy)
375 {
376 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
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 */
384 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
385 data->vbus_det)
386 return 0;
387
388 ret = regulator_enable(phy->vbus);
389 if (ret)
390 return ret;
391
392 phy->regulator_on = true;
393
394 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
395 if (phy->index == 0 && sun4i_usb_phy0_poll(data))
396 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
397
398 return 0;
399 }
400
401 static int sun4i_usb_phy_power_off(struct phy *_phy)
402 {
403 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
404 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
405
406 if (!phy->vbus || !phy->regulator_on)
407 return 0;
408
409 regulator_disable(phy->vbus);
410 phy->regulator_on = false;
411
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 */
416 if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
417 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
418
419 return 0;
420 }
421
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 }
428 EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
429
430 static const struct phy_ops sun4i_usb_phy_ops = {
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
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;
443 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
444
445 if (phy0 == NULL)
446 return;
447
448 id_det = sun4i_usb_phy0_get_id_det(data);
449 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
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) {
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 */
464 if (data->dr_mode == USB_DR_MODE_OTG &&
465 !sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) {
466 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
467 msleep(200);
468 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
469 }
470 sun4i_usb_phy0_set_id_detect(phy0, id_det);
471 data->id_det = id_det;
472 id_notify = 1;
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;
478 vbus_notify = 1;
479 }
480
481 mutex_unlock(&phy0->mutex);
482
483 if (id_notify) {
484 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
485 !id_det);
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 */
491 if (data->dr_mode == USB_DR_MODE_OTG &&
492 !sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) {
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 }
500
501 if (vbus_notify)
502 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
503
504 if (sun4i_usb_phy0_poll(data))
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
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
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
537 if (args->args[0] < data->first_phy ||
538 args->args[0] >= data->cfg->num_phys)
539 return ERR_PTR(-ENODEV);
540
541 return data->phys[args->args[0]].phy;
542 }
543
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
549 if (data->vbus_power_nb_registered)
550 power_supply_unreg_notifier(&data->vbus_power_nb);
551 if (data->id_det_irq > 0)
552 devm_free_irq(dev, data->id_det_irq, data);
553 if (data->vbus_det_irq > 0)
554 devm_free_irq(dev, data->vbus_det_irq, data);
555
556 cancel_delayed_work_sync(&data->detect);
557
558 return 0;
559 }
560
561 static const unsigned int sun4i_usb_phy0_cable[] = {
562 EXTCON_USB,
563 EXTCON_USB_HOST,
564 EXTCON_NONE,
565 };
566
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;
572 struct phy_provider *phy_provider;
573 struct resource *res;
574 int i, ret;
575
576 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
577 if (!data)
578 return -ENOMEM;
579
580 mutex_init(&data->mutex);
581 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
582 dev_set_drvdata(dev, data);
583 data->cfg = of_device_get_match_data(dev);
584 if (!data->cfg)
585 return -EINVAL;
586
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
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);
601
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
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:
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 }
633 break;
634 default:
635 dev_info(dev, "dr_mode unknown, not registering usb phy0\n");
636 data->first_phy = 1;
637 }
638
639 for (i = data->first_phy; i < data->cfg->num_phys; i++) {
640 struct sun4i_usb_phy *phy = data->phys + i;
641 char name[16];
642
643 snprintf(name, sizeof(name), "usb%d_vbus", i);
644 phy->vbus = devm_regulator_get_optional(dev, name);
645 if (IS_ERR(phy->vbus)) {
646 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
647 return -EPROBE_DEFER;
648 phy->vbus = NULL;
649 }
650
651 if (data->cfg->dedicated_clocks)
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
662 snprintf(name, sizeof(name), "usb%d_reset", i);
663 phy->reset = devm_reset_control_get(dev, name);
664 if (IS_ERR(phy->reset)) {
665 dev_err(dev, "failed to get reset %s\n", name);
666 return PTR_ERR(phy->reset);
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);
673 phy->pmu = devm_ioremap_resource(dev, res);
674 if (IS_ERR(phy->pmu))
675 return PTR_ERR(phy->pmu);
676 }
677
678 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
679 if (IS_ERR(phy->phy)) {
680 dev_err(dev, "failed to create PHY %d\n", i);
681 return PTR_ERR(phy->phy);
682 }
683
684 phy->index = i;
685 phy_set_drvdata(phy->phy, &data->phys[i]);
686 }
687
688 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
689 if (data->id_det_irq > 0) {
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
700 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
701 if (data->vbus_det_irq > 0) {
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
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
725 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
726 if (IS_ERR(phy_provider)) {
727 sun4i_usb_phy_remove(pdev); /* Stop detect work */
728 return PTR_ERR(phy_provider);
729 }
730
731 return 0;
732 }
733
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,
752 .type = sun6i_a31_phy,
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
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
789 static const struct of_device_id sun4i_usb_phy_of_match[] = {
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 },
796 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
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,
803 .remove = sun4i_usb_phy_remove,
804 .driver = {
805 .of_match_table = sun4i_usb_phy_of_match,
806 .name = "sun4i-usb-phy",
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");
This page took 0.046232 seconds and 4 git commands to generate.