phy: sun4i-usb: Use spinlock to guard phyctl register access
[deliverable/linux.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>
72c6187f 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
54#define REG_PMU_UNK_H3 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,
68dbc2ce
HG
102};
103
104struct 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
ba4bdc9e 112struct sun4i_usb_phy_data {
ba4bdc9e 113 void __iomem *base;
68dbc2ce 114 const struct sun4i_usb_phy_cfg *cfg;
b33ecca8 115 enum usb_dr_mode dr_mode;
72c6187f 116 spinlock_t reg_lock; /* guard access to phyctl reg */
ba4bdc9e
HG
117 struct sun4i_usb_phy {
118 struct phy *phy;
119 void __iomem *pmu;
120 struct regulator *vbus;
121 struct reset_control *reset;
eadd4312 122 struct clk *clk;
d2332303 123 bool regulator_on;
ba4bdc9e
HG
124 int index;
125 } phys[MAX_PHYS];
b33ecca8 126 int first_phy;
d2332303 127 /* phy0 / otg related variables */
1a52abe6 128 struct extcon_dev *extcon;
d2332303 129 bool phy0_init;
d2332303
HG
130 struct gpio_desc *id_det_gpio;
131 struct gpio_desc *vbus_det_gpio;
8665c18b
HG
132 struct power_supply *vbus_power_supply;
133 struct notifier_block vbus_power_nb;
134 bool vbus_power_nb_registered;
d2332303
HG
135 int id_det_irq;
136 int vbus_det_irq;
137 int id_det;
138 int vbus_det;
139 struct delayed_work detect;
ba4bdc9e
HG
140};
141
142#define to_sun4i_usb_phy_data(phy) \
143 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
144
d2332303
HG
145static 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
157static 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
167static 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
ba4bdc9e
HG
177static 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);
d99cb378 182 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
72c6187f 183 unsigned long flags;
ba4bdc9e
HG
184 int i;
185
72c6187f 186 spin_lock_irqsave(&phy_data->reg_lock, flags);
ba4bdc9e 187
68dbc2ce 188 if (phy_data->cfg->type == sun8i_a33_phy) {
fc1f45ed
HG
189 /* A33 needs us to set phyctl to 0 explicitly */
190 writel(0, phyctl);
fc1f45ed
HG
191 }
192
ba4bdc9e 193 for (i = 0; i < len; i++) {
fc1f45ed 194 temp = readl(phyctl);
ba4bdc9e
HG
195
196 /* clear the address portion */
197 temp &= ~(0xff << 8);
198
199 /* set the address */
200 temp |= ((addr + i) << 8);
fc1f45ed 201 writel(temp, phyctl);
ba4bdc9e
HG
202
203 /* set the data bit and clear usbc bit*/
fc1f45ed 204 temp = readb(phyctl);
ba4bdc9e
HG
205 if (data & 0x1)
206 temp |= PHYCTL_DATA;
207 else
208 temp &= ~PHYCTL_DATA;
209 temp &= ~usbc_bit;
fc1f45ed 210 writeb(temp, phyctl);
ba4bdc9e
HG
211
212 /* pulse usbc_bit */
fc1f45ed 213 temp = readb(phyctl);
ba4bdc9e 214 temp |= usbc_bit;
fc1f45ed 215 writeb(temp, phyctl);
ba4bdc9e 216
fc1f45ed 217 temp = readb(phyctl);
ba4bdc9e 218 temp &= ~usbc_bit;
fc1f45ed 219 writeb(temp, phyctl);
ba4bdc9e
HG
220
221 data >>= 1;
222 }
72c6187f
CYT
223
224 spin_unlock_irqrestore(&phy_data->reg_lock, flags);
ba4bdc9e
HG
225}
226
227static 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
247static 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;
626a630e 252 u32 val;
ba4bdc9e 253
eadd4312 254 ret = clk_prepare_enable(phy->clk);
ba4bdc9e
HG
255 if (ret)
256 return ret;
257
258 ret = reset_control_deassert(phy->reset);
259 if (ret) {
eadd4312 260 clk_disable_unprepare(phy->clk);
ba4bdc9e
HG
261 return ret;
262 }
263
626a630e
RH
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);
6827a46f 276
626a630e
RH
277 /* Adjust PHY's magnitude and rate */
278 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
ba4bdc9e 279
626a630e
RH
280 /* Disconnect threshold adjustment */
281 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
282 data->cfg->disc_thresh, 2);
283 }
ba4bdc9e
HG
284
285 sun4i_usb_phy_passby(phy, 1);
286
d2332303
HG
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
b33ecca8
HG
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);
d2332303
HG
298 }
299
ba4bdc9e
HG
300 return 0;
301}
302
303static int sun4i_usb_phy_exit(struct phy *_phy)
304{
305 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
d2332303
HG
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 }
ba4bdc9e
HG
314
315 sun4i_usb_phy_passby(phy, 0);
316 reset_control_assert(phy->reset);
eadd4312 317 clk_disable_unprepare(phy->clk);
ba4bdc9e
HG
318
319 return 0;
320}
321
b33ecca8
HG
322static 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
3d772c4a
HG
335static 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
354static 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
91d96f06
HG
359static 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
ba4bdc9e
HG
377static int sun4i_usb_phy_power_on(struct phy *_phy)
378{
379 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
d2332303
HG
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 */
8083526e
HG
387 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
388 data->vbus_det)
d2332303 389 return 0;
ba4bdc9e 390
d2332303
HG
391 ret = regulator_enable(phy->vbus);
392 if (ret)
393 return ret;
ba4bdc9e 394
d2332303 395 phy->regulator_on = true;
ba4bdc9e 396
d2332303 397 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
91d96f06 398 if (phy->index == 0 && sun4i_usb_phy0_poll(data))
d2332303 399 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
ba4bdc9e 400
d2332303 401 return 0;
ba4bdc9e
HG
402}
403
404static int sun4i_usb_phy_power_off(struct phy *_phy)
405{
406 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
d2332303
HG
407 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
408
409 if (!phy->vbus || !phy->regulator_on)
410 return 0;
ba4bdc9e 411
d2332303
HG
412 regulator_disable(phy->vbus);
413 phy->regulator_on = false;
ba4bdc9e 414
d2332303
HG
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 */
91d96f06 419 if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
d2332303 420 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
ba4bdc9e
HG
421
422 return 0;
423}
424
24fe86a6
HG
425void 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}
7167bf8b 431EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
24fe86a6 432
4a9e5ca1 433static const struct phy_ops sun4i_usb_phy_ops = {
ba4bdc9e
HG
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
d2332303
HG
441static 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;
1a52abe6 446 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
d2332303 447
b33ecca8
HG
448 if (phy0 == NULL)
449 return;
450
451 id_det = sun4i_usb_phy0_get_id_det(data);
8665c18b 452 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
d2332303
HG
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) {
1aedf3a7
HG
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 */
b33ecca8
HG
467 if (data->dr_mode == USB_DR_MODE_OTG &&
468 !sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) {
1aedf3a7
HG
469 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
470 msleep(200);
471 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
472 }
d2332303
HG
473 sun4i_usb_phy0_set_id_detect(phy0, id_det);
474 data->id_det = id_det;
1a52abe6 475 id_notify = 1;
d2332303
HG
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;
1a52abe6 481 vbus_notify = 1;
d2332303
HG
482 }
483
484 mutex_unlock(&phy0->mutex);
485
1aedf3a7 486 if (id_notify) {
1a52abe6
HG
487 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
488 !id_det);
1aedf3a7
HG
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 */
b33ecca8
HG
494 if (data->dr_mode == USB_DR_MODE_OTG &&
495 !sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) {
1aedf3a7
HG
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 }
1a52abe6
HG
503
504 if (vbus_notify)
505 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
506
91d96f06 507 if (sun4i_usb_phy0_poll(data))
d2332303
HG
508 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
509}
510
511static 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
8665c18b
HG
521static 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
ba4bdc9e
HG
535static 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
b33ecca8
HG
540 if (args->args[0] < data->first_phy ||
541 args->args[0] >= data->cfg->num_phys)
ba4bdc9e
HG
542 return ERR_PTR(-ENODEV);
543
544 return data->phys[args->args[0]].phy;
545}
546
d2332303
HG
547static 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
8665c18b
HG
552 if (data->vbus_power_nb_registered)
553 power_supply_unreg_notifier(&data->vbus_power_nb);
04e59a02 554 if (data->id_det_irq > 0)
d2332303 555 devm_free_irq(dev, data->id_det_irq, data);
04e59a02 556 if (data->vbus_det_irq > 0)
d2332303
HG
557 devm_free_irq(dev, data->vbus_det_irq, data);
558
559 cancel_delayed_work_sync(&data->detect);
560
561 return 0;
562}
563
1a52abe6
HG
564static const unsigned int sun4i_usb_phy0_cable[] = {
565 EXTCON_USB,
566 EXTCON_USB_HOST,
567 EXTCON_NONE,
568};
569
ba4bdc9e
HG
570static 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;
ba4bdc9e 575 struct phy_provider *phy_provider;
ba4bdc9e 576 struct resource *res;
d2332303 577 int i, ret;
ba4bdc9e
HG
578
579 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
580 if (!data)
581 return -ENOMEM;
582
72c6187f 583 spin_lock_init(&data->reg_lock);
d2332303
HG
584 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
585 dev_set_drvdata(dev, data);
68dbc2ce
HG
586 data->cfg = of_device_get_match_data(dev);
587 if (!data->cfg)
588 return -EINVAL;
fc1f45ed 589
ba4bdc9e
HG
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
b2dfc34c
AL
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);
d2332303 604
8665c18b
HG
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
b33ecca8
HG
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:
1a52abe6
HG
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 }
b33ecca8
HG
636 break;
637 default:
638 dev_info(dev, "dr_mode unknown, not registering usb phy0\n");
639 data->first_phy = 1;
1a52abe6
HG
640 }
641
b33ecca8 642 for (i = data->first_phy; i < data->cfg->num_phys; i++) {
2a7f9982
MR
643 struct sun4i_usb_phy *phy = data->phys + i;
644 char name[16];
645
ba4bdc9e 646 snprintf(name, sizeof(name), "usb%d_vbus", i);
2a7f9982
MR
647 phy->vbus = devm_regulator_get_optional(dev, name);
648 if (IS_ERR(phy->vbus)) {
649 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
ba4bdc9e 650 return -EPROBE_DEFER;
2a7f9982 651 phy->vbus = NULL;
ba4bdc9e
HG
652 }
653
68dbc2ce 654 if (data->cfg->dedicated_clocks)
eadd4312
MR
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
ba4bdc9e 665 snprintf(name, sizeof(name), "usb%d_reset", i);
2a7f9982
MR
666 phy->reset = devm_reset_control_get(dev, name);
667 if (IS_ERR(phy->reset)) {
ba4bdc9e 668 dev_err(dev, "failed to get reset %s\n", name);
2a7f9982 669 return PTR_ERR(phy->reset);
ba4bdc9e
HG
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);
2a7f9982
MR
676 phy->pmu = devm_ioremap_resource(dev, res);
677 if (IS_ERR(phy->pmu))
678 return PTR_ERR(phy->pmu);
ba4bdc9e
HG
679 }
680
dbc98635 681 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
2a7f9982 682 if (IS_ERR(phy->phy)) {
ba4bdc9e 683 dev_err(dev, "failed to create PHY %d\n", i);
2a7f9982 684 return PTR_ERR(phy->phy);
ba4bdc9e
HG
685 }
686
2a7f9982
MR
687 phy->index = i;
688 phy_set_drvdata(phy->phy, &data->phys[i]);
ba4bdc9e
HG
689 }
690
d2332303 691 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
5cf700ac 692 if (data->id_det_irq > 0) {
d2332303
HG
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
91d96f06 703 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
5cf700ac 704 if (data->vbus_det_irq > 0) {
d2332303
HG
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
8665c18b
HG
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
ba4bdc9e 728 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
d2332303
HG
729 if (IS_ERR(phy_provider)) {
730 sun4i_usb_phy_remove(pdev); /* Stop detect work */
731 return PTR_ERR(phy_provider);
732 }
ba4bdc9e 733
d2332303 734 return 0;
ba4bdc9e
HG
735}
736
68dbc2ce
HG
737static 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
745static 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
753static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
754 .num_phys = 3,
91d96f06 755 .type = sun6i_a31_phy,
68dbc2ce
HG
756 .disc_thresh = 3,
757 .phyctl_offset = REG_PHYCTL_A10,
758 .dedicated_clocks = true,
759};
760
761static 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
769static 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
777static 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
626a630e
RH
785static 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
ba4bdc9e 792static const struct of_device_id sun4i_usb_phy_of_match[] = {
68dbc2ce
HG
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 },
626a630e 799 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
ba4bdc9e
HG
800 { },
801};
802MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
803
804static struct platform_driver sun4i_usb_phy_driver = {
805 .probe = sun4i_usb_phy_probe,
d2332303 806 .remove = sun4i_usb_phy_remove,
ba4bdc9e
HG
807 .driver = {
808 .of_match_table = sun4i_usb_phy_of_match,
809 .name = "sun4i-usb-phy",
ba4bdc9e
HG
810 }
811};
812module_platform_driver(sun4i_usb_phy_driver);
813
814MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
815MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
816MODULE_LICENSE("GPL v2");
This page took 0.168206 seconds and 5 git commands to generate.