blob: a6ea2e6b1d5c7d79c6c09b4a20ee707e4555a6f5 [file] [log] [blame]
Hans de Goedeba4bdc92014-03-01 18:09:26 +01001/*
2 * Allwinner sun4i USB phy driver
3 *
Hans de Goeded2332302015-06-13 14:37:45 +02004 * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com>
Hans de Goedeba4bdc92014-03-01 18:09:26 +01005 *
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>
Sachin Kamat2d84aff2014-05-29 12:00:49 +053025#include <linux/err.h>
Hans de Goede1a52abe2015-06-13 14:37:46 +020026#include <linux/extcon.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010027#include <linux/io.h>
Hans de Goeded2332302015-06-13 14:37:45 +020028#include <linux/interrupt.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/mutex.h>
32#include <linux/of.h>
33#include <linux/of_address.h>
Hans de Goeded2332302015-06-13 14:37:45 +020034#include <linux/of_gpio.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010035#include <linux/phy/phy.h>
Hans de Goede24fe86a2015-03-29 12:50:46 +020036#include <linux/phy/phy-sun4i-usb.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010037#include <linux/platform_device.h>
38#include <linux/regulator/consumer.h>
39#include <linux/reset.h>
Hans de Goeded2332302015-06-13 14:37:45 +020040#include <linux/workqueue.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010041
42#define REG_ISCR 0x00
43#define REG_PHYCTL 0x04
44#define REG_PHYBIST 0x08
45#define REG_PHYTUNE 0x0c
46
47#define PHYCTL_DATA BIT(7)
48
49#define SUNXI_AHB_ICHR8_EN BIT(10)
50#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
51#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
52#define SUNXI_ULPI_BYPASS_EN BIT(0)
53
Hans de Goeded2332302015-06-13 14:37:45 +020054/* ISCR, Interface Status and Control bits */
55#define ISCR_ID_PULLUP_EN (1 << 17)
56#define ISCR_DPDM_PULLUP_EN (1 << 16)
57/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
58#define ISCR_FORCE_ID_MASK (3 << 14)
59#define ISCR_FORCE_ID_LOW (2 << 14)
60#define ISCR_FORCE_ID_HIGH (3 << 14)
61#define ISCR_FORCE_VBUS_MASK (3 << 12)
62#define ISCR_FORCE_VBUS_LOW (2 << 12)
63#define ISCR_FORCE_VBUS_HIGH (3 << 12)
64
Hans de Goedeba4bdc92014-03-01 18:09:26 +010065/* Common Control Bits for Both PHYs */
66#define PHY_PLL_BW 0x03
67#define PHY_RES45_CAL_EN 0x0c
68
69/* Private Control Bits for Each PHY */
70#define PHY_TX_AMPLITUDE_TUNE 0x20
71#define PHY_TX_SLEWRATE_TUNE 0x22
72#define PHY_VBUSVALID_TH_SEL 0x25
73#define PHY_PULLUP_RES_SEL 0x27
74#define PHY_OTG_FUNC_EN 0x28
75#define PHY_VBUS_DET_EN 0x29
76#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020077#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010078
79#define MAX_PHYS 3
80
Hans de Goeded2332302015-06-13 14:37:45 +020081/*
82 * Note do not raise the debounce time, we must report Vusb high within 100ms
83 * otherwise we get Vbus errors
84 */
85#define DEBOUNCE_TIME msecs_to_jiffies(50)
86#define POLL_TIME msecs_to_jiffies(250)
87
Hans de Goedeba4bdc92014-03-01 18:09:26 +010088struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +010089 void __iomem *base;
90 struct mutex mutex;
91 int num_phys;
92 u32 disc_thresh;
93 struct sun4i_usb_phy {
94 struct phy *phy;
95 void __iomem *pmu;
96 struct regulator *vbus;
97 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +020098 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +020099 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100100 int index;
101 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200102 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200103 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200104 bool phy0_init;
105 bool phy0_poll;
106 struct gpio_desc *id_det_gpio;
107 struct gpio_desc *vbus_det_gpio;
108 int id_det_irq;
109 int vbus_det_irq;
110 int id_det;
111 int vbus_det;
112 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100113};
114
115#define to_sun4i_usb_phy_data(phy) \
116 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
117
Hans de Goeded2332302015-06-13 14:37:45 +0200118static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
119{
120 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
121 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
122 u32 iscr;
123
124 iscr = readl(data->base + REG_ISCR);
125 iscr &= ~clr;
126 iscr |= set;
127 writel(iscr, data->base + REG_ISCR);
128}
129
130static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
131{
132 if (val)
133 val = ISCR_FORCE_ID_HIGH;
134 else
135 val = ISCR_FORCE_ID_LOW;
136
137 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
138}
139
140static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
141{
142 if (val)
143 val = ISCR_FORCE_VBUS_HIGH;
144 else
145 val = ISCR_FORCE_VBUS_LOW;
146
147 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
148}
149
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100150static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
151 int len)
152{
153 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
154 u32 temp, usbc_bit = BIT(phy->index * 2);
155 int i;
156
157 mutex_lock(&phy_data->mutex);
158
159 for (i = 0; i < len; i++) {
160 temp = readl(phy_data->base + REG_PHYCTL);
161
162 /* clear the address portion */
163 temp &= ~(0xff << 8);
164
165 /* set the address */
166 temp |= ((addr + i) << 8);
167 writel(temp, phy_data->base + REG_PHYCTL);
168
169 /* set the data bit and clear usbc bit*/
170 temp = readb(phy_data->base + REG_PHYCTL);
171 if (data & 0x1)
172 temp |= PHYCTL_DATA;
173 else
174 temp &= ~PHYCTL_DATA;
175 temp &= ~usbc_bit;
176 writeb(temp, phy_data->base + REG_PHYCTL);
177
178 /* pulse usbc_bit */
179 temp = readb(phy_data->base + REG_PHYCTL);
180 temp |= usbc_bit;
181 writeb(temp, phy_data->base + REG_PHYCTL);
182
183 temp = readb(phy_data->base + REG_PHYCTL);
184 temp &= ~usbc_bit;
185 writeb(temp, phy_data->base + REG_PHYCTL);
186
187 data >>= 1;
188 }
189 mutex_unlock(&phy_data->mutex);
190}
191
192static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
193{
194 u32 bits, reg_value;
195
196 if (!phy->pmu)
197 return;
198
199 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
200 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
201
202 reg_value = readl(phy->pmu);
203
204 if (enable)
205 reg_value |= bits;
206 else
207 reg_value &= ~bits;
208
209 writel(reg_value, phy->pmu);
210}
211
212static int sun4i_usb_phy_init(struct phy *_phy)
213{
214 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
215 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
216 int ret;
217
Maxime Ripardeadd4312014-05-13 17:44:18 +0200218 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100219 if (ret)
220 return ret;
221
222 ret = reset_control_deassert(phy->reset);
223 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200224 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100225 return ret;
226 }
227
Roman Byshko6827a46f2014-11-10 19:55:06 +0100228 /* Enable USB 45 Ohm resistor calibration */
229 if (phy->index == 0)
230 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
231
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100232 /* Adjust PHY's magnitude and rate */
233 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
234
235 /* Disconnect threshold adjustment */
236 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
237
238 sun4i_usb_phy_passby(phy, 1);
239
Hans de Goeded2332302015-06-13 14:37:45 +0200240 if (phy->index == 0) {
241 data->phy0_init = true;
242
243 /* Enable pull-ups */
244 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
245 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
246
247 if (data->id_det_gpio) {
248 /* OTG mode, force ISCR and cable state updates */
249 data->id_det = -1;
250 data->vbus_det = -1;
251 queue_delayed_work(system_wq, &data->detect, 0);
252 } else {
253 /* Host only mode */
254 sun4i_usb_phy0_set_id_detect(_phy, 0);
255 sun4i_usb_phy0_set_vbus_detect(_phy, 1);
256 }
257 }
258
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100259 return 0;
260}
261
262static int sun4i_usb_phy_exit(struct phy *_phy)
263{
264 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200265 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
266
267 if (phy->index == 0) {
268 /* Disable pull-ups */
269 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
270 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
271 data->phy0_init = false;
272 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100273
274 sun4i_usb_phy_passby(phy, 0);
275 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200276 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100277
278 return 0;
279}
280
281static int sun4i_usb_phy_power_on(struct phy *_phy)
282{
283 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200284 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
285 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100286
Hans de Goeded2332302015-06-13 14:37:45 +0200287 if (!phy->vbus || phy->regulator_on)
288 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100289
Hans de Goeded2332302015-06-13 14:37:45 +0200290 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
291 if (phy->index == 0 && data->vbus_det)
292 return 0;
293
294 ret = regulator_enable(phy->vbus);
295 if (ret)
296 return ret;
297
298 phy->regulator_on = true;
299
300 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
301 if (phy->index == 0 && data->phy0_poll)
302 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
303
304 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100305}
306
307static int sun4i_usb_phy_power_off(struct phy *_phy)
308{
309 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200310 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100311
Hans de Goeded2332302015-06-13 14:37:45 +0200312 if (!phy->vbus || !phy->regulator_on)
313 return 0;
314
315 regulator_disable(phy->vbus);
316 phy->regulator_on = false;
317
318 /*
319 * phy0 vbus typically slowly discharges, sometimes this causes the
320 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
321 */
322 if (phy->index == 0 && !data->phy0_poll)
323 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100324
325 return 0;
326}
327
Hans de Goede24fe86a2015-03-29 12:50:46 +0200328void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
329{
330 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
331
332 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
333}
334
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100335static struct phy_ops sun4i_usb_phy_ops = {
336 .init = sun4i_usb_phy_init,
337 .exit = sun4i_usb_phy_exit,
338 .power_on = sun4i_usb_phy_power_on,
339 .power_off = sun4i_usb_phy_power_off,
340 .owner = THIS_MODULE,
341};
342
Hans de Goeded2332302015-06-13 14:37:45 +0200343static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
344{
345 struct sun4i_usb_phy_data *data =
346 container_of(work, struct sun4i_usb_phy_data, detect.work);
347 struct phy *phy0 = data->phys[0].phy;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200348 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
Hans de Goeded2332302015-06-13 14:37:45 +0200349
350 id_det = gpiod_get_value_cansleep(data->id_det_gpio);
351 vbus_det = gpiod_get_value_cansleep(data->vbus_det_gpio);
352
353 mutex_lock(&phy0->mutex);
354
355 if (!data->phy0_init) {
356 mutex_unlock(&phy0->mutex);
357 return;
358 }
359
360 if (id_det != data->id_det) {
361 sun4i_usb_phy0_set_id_detect(phy0, id_det);
362 data->id_det = id_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200363 id_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200364 }
365
366 if (vbus_det != data->vbus_det) {
367 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
368 data->vbus_det = vbus_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200369 vbus_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200370 }
371
372 mutex_unlock(&phy0->mutex);
373
Hans de Goede1a52abe2015-06-13 14:37:46 +0200374 if (id_notify)
375 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
376 !id_det);
377
378 if (vbus_notify)
379 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
380
Hans de Goeded2332302015-06-13 14:37:45 +0200381 if (data->phy0_poll)
382 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
383}
384
385static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
386{
387 struct sun4i_usb_phy_data *data = dev_id;
388
389 /* vbus or id changed, let the pins settle and then scan them */
390 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
391
392 return IRQ_HANDLED;
393}
394
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100395static struct phy *sun4i_usb_phy_xlate(struct device *dev,
396 struct of_phandle_args *args)
397{
398 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
399
Roman Byshko6827a46f2014-11-10 19:55:06 +0100400 if (args->args[0] >= data->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100401 return ERR_PTR(-ENODEV);
402
403 return data->phys[args->args[0]].phy;
404}
405
Hans de Goeded2332302015-06-13 14:37:45 +0200406static int sun4i_usb_phy_remove(struct platform_device *pdev)
407{
408 struct device *dev = &pdev->dev;
409 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
410
411 if (data->id_det_irq >= 0)
412 devm_free_irq(dev, data->id_det_irq, data);
413 if (data->vbus_det_irq >= 0)
414 devm_free_irq(dev, data->vbus_det_irq, data);
415
416 cancel_delayed_work_sync(&data->detect);
417
418 return 0;
419}
420
Hans de Goede1a52abe2015-06-13 14:37:46 +0200421static const unsigned int sun4i_usb_phy0_cable[] = {
422 EXTCON_USB,
423 EXTCON_USB_HOST,
424 EXTCON_NONE,
425};
426
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100427static int sun4i_usb_phy_probe(struct platform_device *pdev)
428{
429 struct sun4i_usb_phy_data *data;
430 struct device *dev = &pdev->dev;
431 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100432 struct phy_provider *phy_provider;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200433 bool dedicated_clocks;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100434 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200435 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100436
437 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
438 if (!data)
439 return -ENOMEM;
440
441 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200442 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
443 dev_set_drvdata(dev, data);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100444
445 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy"))
446 data->num_phys = 2;
447 else
448 data->num_phys = 3;
449
Hans de Goede37240032014-12-17 15:39:37 +0100450 if (of_device_is_compatible(np, "allwinner,sun4i-a10-usb-phy") ||
451 of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100452 data->disc_thresh = 3;
453 else
454 data->disc_thresh = 2;
455
Maxime Ripardeadd4312014-05-13 17:44:18 +0200456 if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy"))
457 dedicated_clocks = true;
458 else
459 dedicated_clocks = false;
460
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100461 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
462 data->base = devm_ioremap_resource(dev, res);
463 if (IS_ERR(data->base))
464 return PTR_ERR(data->base);
465
Hans de Goeded2332302015-06-13 14:37:45 +0200466 data->id_det_gpio = devm_gpiod_get(dev, "usb0_id_det", GPIOD_IN);
467 if (IS_ERR(data->id_det_gpio)) {
468 if (PTR_ERR(data->id_det_gpio) == -EPROBE_DEFER)
469 return -EPROBE_DEFER;
470 data->id_det_gpio = NULL;
471 }
472
473 data->vbus_det_gpio = devm_gpiod_get(dev, "usb0_vbus_det", GPIOD_IN);
474 if (IS_ERR(data->vbus_det_gpio)) {
475 if (PTR_ERR(data->vbus_det_gpio) == -EPROBE_DEFER)
476 return -EPROBE_DEFER;
477 data->vbus_det_gpio = NULL;
478 }
479
480 /* We either want both gpio pins or neither (when in host mode) */
481 if (!data->id_det_gpio != !data->vbus_det_gpio) {
482 dev_err(dev, "failed to get id or vbus detect pin\n");
483 return -ENODEV;
484 }
485
Hans de Goede1a52abe2015-06-13 14:37:46 +0200486 if (data->id_det_gpio) {
487 data->extcon = devm_extcon_dev_allocate(dev,
488 sun4i_usb_phy0_cable);
489 if (IS_ERR(data->extcon))
490 return PTR_ERR(data->extcon);
491
492 ret = devm_extcon_dev_register(dev, data->extcon);
493 if (ret) {
494 dev_err(dev, "failed to register extcon: %d\n", ret);
495 return ret;
496 }
497 }
498
Roman Byshko6827a46f2014-11-10 19:55:06 +0100499 for (i = 0; i < data->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200500 struct sun4i_usb_phy *phy = data->phys + i;
501 char name[16];
502
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100503 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200504 phy->vbus = devm_regulator_get_optional(dev, name);
505 if (IS_ERR(phy->vbus)) {
506 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100507 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200508 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100509 }
510
Maxime Ripardeadd4312014-05-13 17:44:18 +0200511 if (dedicated_clocks)
512 snprintf(name, sizeof(name), "usb%d_phy", i);
513 else
514 strlcpy(name, "usb_phy", sizeof(name));
515
516 phy->clk = devm_clk_get(dev, name);
517 if (IS_ERR(phy->clk)) {
518 dev_err(dev, "failed to get clock %s\n", name);
519 return PTR_ERR(phy->clk);
520 }
521
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100522 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200523 phy->reset = devm_reset_control_get(dev, name);
524 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100525 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200526 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100527 }
528
529 if (i) { /* No pmu for usbc0 */
530 snprintf(name, sizeof(name), "pmu%d", i);
531 res = platform_get_resource_byname(pdev,
532 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200533 phy->pmu = devm_ioremap_resource(dev, res);
534 if (IS_ERR(phy->pmu))
535 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100536 }
537
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200538 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200539 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100540 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200541 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100542 }
543
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200544 phy->index = i;
545 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100546 }
547
Hans de Goeded2332302015-06-13 14:37:45 +0200548 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
549 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
550 if (data->id_det_irq < 0 || data->vbus_det_irq < 0)
551 data->phy0_poll = true;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100552
Hans de Goeded2332302015-06-13 14:37:45 +0200553 if (data->id_det_irq >= 0) {
554 ret = devm_request_irq(dev, data->id_det_irq,
555 sun4i_usb_phy0_id_vbus_det_irq,
556 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
557 "usb0-id-det", data);
558 if (ret) {
559 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
560 return ret;
561 }
562 }
563
564 if (data->vbus_det_irq >= 0) {
565 ret = devm_request_irq(dev, data->vbus_det_irq,
566 sun4i_usb_phy0_id_vbus_det_irq,
567 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
568 "usb0-vbus-det", data);
569 if (ret) {
570 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
571 data->vbus_det_irq = -1;
572 sun4i_usb_phy_remove(pdev); /* Stop detect work */
573 return ret;
574 }
575 }
576
577 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
578 if (IS_ERR(phy_provider)) {
579 sun4i_usb_phy_remove(pdev); /* Stop detect work */
580 return PTR_ERR(phy_provider);
581 }
582
583 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100584}
585
586static const struct of_device_id sun4i_usb_phy_of_match[] = {
587 { .compatible = "allwinner,sun4i-a10-usb-phy" },
588 { .compatible = "allwinner,sun5i-a13-usb-phy" },
Maxime Ripardeadd4312014-05-13 17:44:18 +0200589 { .compatible = "allwinner,sun6i-a31-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100590 { .compatible = "allwinner,sun7i-a20-usb-phy" },
591 { },
592};
593MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
594
595static struct platform_driver sun4i_usb_phy_driver = {
596 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200597 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100598 .driver = {
599 .of_match_table = sun4i_usb_phy_of_match,
600 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100601 }
602};
603module_platform_driver(sun4i_usb_phy_driver);
604
605MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
606MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
607MODULE_LICENSE("GPL v2");