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