blob: 37f52f73fdba4bc6217b873eb9c14344217870e9 [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 Goedeba4bdc92014-03-01 18:09:26 +010026#include <linux/io.h>
Hans de Goeded2332302015-06-13 14:37:45 +020027#include <linux/interrupt.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/mutex.h>
31#include <linux/of.h>
32#include <linux/of_address.h>
Hans de Goeded2332302015-06-13 14:37:45 +020033#include <linux/of_gpio.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010034#include <linux/phy/phy.h>
Hans de Goede24fe86a2015-03-29 12:50:46 +020035#include <linux/phy/phy-sun4i-usb.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010036#include <linux/platform_device.h>
37#include <linux/regulator/consumer.h>
38#include <linux/reset.h>
Hans de Goeded2332302015-06-13 14:37:45 +020039#include <linux/workqueue.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010040
41#define REG_ISCR 0x00
42#define REG_PHYCTL 0x04
43#define REG_PHYBIST 0x08
44#define REG_PHYTUNE 0x0c
45
46#define PHYCTL_DATA BIT(7)
47
48#define SUNXI_AHB_ICHR8_EN BIT(10)
49#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
50#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
51#define SUNXI_ULPI_BYPASS_EN BIT(0)
52
Hans de Goeded2332302015-06-13 14:37:45 +020053/* ISCR, Interface Status and Control bits */
54#define ISCR_ID_PULLUP_EN (1 << 17)
55#define ISCR_DPDM_PULLUP_EN (1 << 16)
56/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
57#define ISCR_FORCE_ID_MASK (3 << 14)
58#define ISCR_FORCE_ID_LOW (2 << 14)
59#define ISCR_FORCE_ID_HIGH (3 << 14)
60#define ISCR_FORCE_VBUS_MASK (3 << 12)
61#define ISCR_FORCE_VBUS_LOW (2 << 12)
62#define ISCR_FORCE_VBUS_HIGH (3 << 12)
63
Hans de Goedeba4bdc92014-03-01 18:09:26 +010064/* Common Control Bits for Both PHYs */
65#define PHY_PLL_BW 0x03
66#define PHY_RES45_CAL_EN 0x0c
67
68/* Private Control Bits for Each PHY */
69#define PHY_TX_AMPLITUDE_TUNE 0x20
70#define PHY_TX_SLEWRATE_TUNE 0x22
71#define PHY_VBUSVALID_TH_SEL 0x25
72#define PHY_PULLUP_RES_SEL 0x27
73#define PHY_OTG_FUNC_EN 0x28
74#define PHY_VBUS_DET_EN 0x29
75#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020076#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010077
78#define MAX_PHYS 3
79
Hans de Goeded2332302015-06-13 14:37:45 +020080/*
81 * Note do not raise the debounce time, we must report Vusb high within 100ms
82 * otherwise we get Vbus errors
83 */
84#define DEBOUNCE_TIME msecs_to_jiffies(50)
85#define POLL_TIME msecs_to_jiffies(250)
86
Hans de Goedeba4bdc92014-03-01 18:09:26 +010087struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +010088 void __iomem *base;
89 struct mutex mutex;
90 int num_phys;
91 u32 disc_thresh;
92 struct sun4i_usb_phy {
93 struct phy *phy;
94 void __iomem *pmu;
95 struct regulator *vbus;
96 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +020097 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +020098 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +010099 int index;
100 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200101 /* phy0 / otg related variables */
102 bool phy0_init;
103 bool phy0_poll;
104 struct gpio_desc *id_det_gpio;
105 struct gpio_desc *vbus_det_gpio;
106 int id_det_irq;
107 int vbus_det_irq;
108 int id_det;
109 int vbus_det;
110 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100111};
112
113#define to_sun4i_usb_phy_data(phy) \
114 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
115
Hans de Goeded2332302015-06-13 14:37:45 +0200116static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
117{
118 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
119 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
120 u32 iscr;
121
122 iscr = readl(data->base + REG_ISCR);
123 iscr &= ~clr;
124 iscr |= set;
125 writel(iscr, data->base + REG_ISCR);
126}
127
128static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
129{
130 if (val)
131 val = ISCR_FORCE_ID_HIGH;
132 else
133 val = ISCR_FORCE_ID_LOW;
134
135 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
136}
137
138static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
139{
140 if (val)
141 val = ISCR_FORCE_VBUS_HIGH;
142 else
143 val = ISCR_FORCE_VBUS_LOW;
144
145 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
146}
147
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100148static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
149 int len)
150{
151 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
152 u32 temp, usbc_bit = BIT(phy->index * 2);
153 int i;
154
155 mutex_lock(&phy_data->mutex);
156
157 for (i = 0; i < len; i++) {
158 temp = readl(phy_data->base + REG_PHYCTL);
159
160 /* clear the address portion */
161 temp &= ~(0xff << 8);
162
163 /* set the address */
164 temp |= ((addr + i) << 8);
165 writel(temp, phy_data->base + REG_PHYCTL);
166
167 /* set the data bit and clear usbc bit*/
168 temp = readb(phy_data->base + REG_PHYCTL);
169 if (data & 0x1)
170 temp |= PHYCTL_DATA;
171 else
172 temp &= ~PHYCTL_DATA;
173 temp &= ~usbc_bit;
174 writeb(temp, phy_data->base + REG_PHYCTL);
175
176 /* pulse usbc_bit */
177 temp = readb(phy_data->base + REG_PHYCTL);
178 temp |= usbc_bit;
179 writeb(temp, phy_data->base + REG_PHYCTL);
180
181 temp = readb(phy_data->base + REG_PHYCTL);
182 temp &= ~usbc_bit;
183 writeb(temp, phy_data->base + REG_PHYCTL);
184
185 data >>= 1;
186 }
187 mutex_unlock(&phy_data->mutex);
188}
189
190static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
191{
192 u32 bits, reg_value;
193
194 if (!phy->pmu)
195 return;
196
197 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
198 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
199
200 reg_value = readl(phy->pmu);
201
202 if (enable)
203 reg_value |= bits;
204 else
205 reg_value &= ~bits;
206
207 writel(reg_value, phy->pmu);
208}
209
210static int sun4i_usb_phy_init(struct phy *_phy)
211{
212 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
213 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
214 int ret;
215
Maxime Ripardeadd4312014-05-13 17:44:18 +0200216 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100217 if (ret)
218 return ret;
219
220 ret = reset_control_deassert(phy->reset);
221 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200222 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100223 return ret;
224 }
225
Roman Byshko6827a46f2014-11-10 19:55:06 +0100226 /* Enable USB 45 Ohm resistor calibration */
227 if (phy->index == 0)
228 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
229
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100230 /* Adjust PHY's magnitude and rate */
231 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
232
233 /* Disconnect threshold adjustment */
234 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
235
236 sun4i_usb_phy_passby(phy, 1);
237
Hans de Goeded2332302015-06-13 14:37:45 +0200238 if (phy->index == 0) {
239 data->phy0_init = true;
240
241 /* Enable pull-ups */
242 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
243 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
244
245 if (data->id_det_gpio) {
246 /* OTG mode, force ISCR and cable state updates */
247 data->id_det = -1;
248 data->vbus_det = -1;
249 queue_delayed_work(system_wq, &data->detect, 0);
250 } else {
251 /* Host only mode */
252 sun4i_usb_phy0_set_id_detect(_phy, 0);
253 sun4i_usb_phy0_set_vbus_detect(_phy, 1);
254 }
255 }
256
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100257 return 0;
258}
259
260static int sun4i_usb_phy_exit(struct phy *_phy)
261{
262 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200263 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
264
265 if (phy->index == 0) {
266 /* Disable pull-ups */
267 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
268 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
269 data->phy0_init = false;
270 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100271
272 sun4i_usb_phy_passby(phy, 0);
273 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200274 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100275
276 return 0;
277}
278
279static int sun4i_usb_phy_power_on(struct phy *_phy)
280{
281 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200282 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
283 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100284
Hans de Goeded2332302015-06-13 14:37:45 +0200285 if (!phy->vbus || phy->regulator_on)
286 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100287
Hans de Goeded2332302015-06-13 14:37:45 +0200288 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
289 if (phy->index == 0 && data->vbus_det)
290 return 0;
291
292 ret = regulator_enable(phy->vbus);
293 if (ret)
294 return ret;
295
296 phy->regulator_on = true;
297
298 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
299 if (phy->index == 0 && data->phy0_poll)
300 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
301
302 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100303}
304
305static int sun4i_usb_phy_power_off(struct phy *_phy)
306{
307 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200308 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100309
Hans de Goeded2332302015-06-13 14:37:45 +0200310 if (!phy->vbus || !phy->regulator_on)
311 return 0;
312
313 regulator_disable(phy->vbus);
314 phy->regulator_on = false;
315
316 /*
317 * phy0 vbus typically slowly discharges, sometimes this causes the
318 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
319 */
320 if (phy->index == 0 && !data->phy0_poll)
321 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100322
323 return 0;
324}
325
Hans de Goede24fe86a2015-03-29 12:50:46 +0200326void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
327{
328 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
329
330 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
331}
332
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100333static struct phy_ops sun4i_usb_phy_ops = {
334 .init = sun4i_usb_phy_init,
335 .exit = sun4i_usb_phy_exit,
336 .power_on = sun4i_usb_phy_power_on,
337 .power_off = sun4i_usb_phy_power_off,
338 .owner = THIS_MODULE,
339};
340
Hans de Goeded2332302015-06-13 14:37:45 +0200341static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
342{
343 struct sun4i_usb_phy_data *data =
344 container_of(work, struct sun4i_usb_phy_data, detect.work);
345 struct phy *phy0 = data->phys[0].phy;
346 int id_det, vbus_det;
347
348 id_det = gpiod_get_value_cansleep(data->id_det_gpio);
349 vbus_det = gpiod_get_value_cansleep(data->vbus_det_gpio);
350
351 mutex_lock(&phy0->mutex);
352
353 if (!data->phy0_init) {
354 mutex_unlock(&phy0->mutex);
355 return;
356 }
357
358 if (id_det != data->id_det) {
359 sun4i_usb_phy0_set_id_detect(phy0, id_det);
360 data->id_det = id_det;
361 }
362
363 if (vbus_det != data->vbus_det) {
364 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
365 data->vbus_det = vbus_det;
366 }
367
368 mutex_unlock(&phy0->mutex);
369
370 if (data->phy0_poll)
371 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
372}
373
374static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
375{
376 struct sun4i_usb_phy_data *data = dev_id;
377
378 /* vbus or id changed, let the pins settle and then scan them */
379 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
380
381 return IRQ_HANDLED;
382}
383
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100384static struct phy *sun4i_usb_phy_xlate(struct device *dev,
385 struct of_phandle_args *args)
386{
387 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
388
Roman Byshko6827a46f2014-11-10 19:55:06 +0100389 if (args->args[0] >= data->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100390 return ERR_PTR(-ENODEV);
391
392 return data->phys[args->args[0]].phy;
393}
394
Hans de Goeded2332302015-06-13 14:37:45 +0200395static int sun4i_usb_phy_remove(struct platform_device *pdev)
396{
397 struct device *dev = &pdev->dev;
398 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
399
400 if (data->id_det_irq >= 0)
401 devm_free_irq(dev, data->id_det_irq, data);
402 if (data->vbus_det_irq >= 0)
403 devm_free_irq(dev, data->vbus_det_irq, data);
404
405 cancel_delayed_work_sync(&data->detect);
406
407 return 0;
408}
409
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100410static int sun4i_usb_phy_probe(struct platform_device *pdev)
411{
412 struct sun4i_usb_phy_data *data;
413 struct device *dev = &pdev->dev;
414 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100415 struct phy_provider *phy_provider;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200416 bool dedicated_clocks;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100417 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200418 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100419
420 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
421 if (!data)
422 return -ENOMEM;
423
424 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200425 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
426 dev_set_drvdata(dev, data);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100427
428 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy"))
429 data->num_phys = 2;
430 else
431 data->num_phys = 3;
432
Hans de Goede37240032014-12-17 15:39:37 +0100433 if (of_device_is_compatible(np, "allwinner,sun4i-a10-usb-phy") ||
434 of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100435 data->disc_thresh = 3;
436 else
437 data->disc_thresh = 2;
438
Maxime Ripardeadd4312014-05-13 17:44:18 +0200439 if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy"))
440 dedicated_clocks = true;
441 else
442 dedicated_clocks = false;
443
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100444 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
445 data->base = devm_ioremap_resource(dev, res);
446 if (IS_ERR(data->base))
447 return PTR_ERR(data->base);
448
Hans de Goeded2332302015-06-13 14:37:45 +0200449 data->id_det_gpio = devm_gpiod_get(dev, "usb0_id_det", GPIOD_IN);
450 if (IS_ERR(data->id_det_gpio)) {
451 if (PTR_ERR(data->id_det_gpio) == -EPROBE_DEFER)
452 return -EPROBE_DEFER;
453 data->id_det_gpio = NULL;
454 }
455
456 data->vbus_det_gpio = devm_gpiod_get(dev, "usb0_vbus_det", GPIOD_IN);
457 if (IS_ERR(data->vbus_det_gpio)) {
458 if (PTR_ERR(data->vbus_det_gpio) == -EPROBE_DEFER)
459 return -EPROBE_DEFER;
460 data->vbus_det_gpio = NULL;
461 }
462
463 /* We either want both gpio pins or neither (when in host mode) */
464 if (!data->id_det_gpio != !data->vbus_det_gpio) {
465 dev_err(dev, "failed to get id or vbus detect pin\n");
466 return -ENODEV;
467 }
468
Roman Byshko6827a46f2014-11-10 19:55:06 +0100469 for (i = 0; i < data->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200470 struct sun4i_usb_phy *phy = data->phys + i;
471 char name[16];
472
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100473 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200474 phy->vbus = devm_regulator_get_optional(dev, name);
475 if (IS_ERR(phy->vbus)) {
476 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100477 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200478 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100479 }
480
Maxime Ripardeadd4312014-05-13 17:44:18 +0200481 if (dedicated_clocks)
482 snprintf(name, sizeof(name), "usb%d_phy", i);
483 else
484 strlcpy(name, "usb_phy", sizeof(name));
485
486 phy->clk = devm_clk_get(dev, name);
487 if (IS_ERR(phy->clk)) {
488 dev_err(dev, "failed to get clock %s\n", name);
489 return PTR_ERR(phy->clk);
490 }
491
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100492 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200493 phy->reset = devm_reset_control_get(dev, name);
494 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100495 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200496 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100497 }
498
499 if (i) { /* No pmu for usbc0 */
500 snprintf(name, sizeof(name), "pmu%d", i);
501 res = platform_get_resource_byname(pdev,
502 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200503 phy->pmu = devm_ioremap_resource(dev, res);
504 if (IS_ERR(phy->pmu))
505 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100506 }
507
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200508 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200509 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100510 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200511 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100512 }
513
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200514 phy->index = i;
515 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100516 }
517
Hans de Goeded2332302015-06-13 14:37:45 +0200518 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
519 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
520 if (data->id_det_irq < 0 || data->vbus_det_irq < 0)
521 data->phy0_poll = true;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100522
Hans de Goeded2332302015-06-13 14:37:45 +0200523 if (data->id_det_irq >= 0) {
524 ret = devm_request_irq(dev, data->id_det_irq,
525 sun4i_usb_phy0_id_vbus_det_irq,
526 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
527 "usb0-id-det", data);
528 if (ret) {
529 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
530 return ret;
531 }
532 }
533
534 if (data->vbus_det_irq >= 0) {
535 ret = devm_request_irq(dev, data->vbus_det_irq,
536 sun4i_usb_phy0_id_vbus_det_irq,
537 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
538 "usb0-vbus-det", data);
539 if (ret) {
540 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
541 data->vbus_det_irq = -1;
542 sun4i_usb_phy_remove(pdev); /* Stop detect work */
543 return ret;
544 }
545 }
546
547 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
548 if (IS_ERR(phy_provider)) {
549 sun4i_usb_phy_remove(pdev); /* Stop detect work */
550 return PTR_ERR(phy_provider);
551 }
552
553 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100554}
555
556static const struct of_device_id sun4i_usb_phy_of_match[] = {
557 { .compatible = "allwinner,sun4i-a10-usb-phy" },
558 { .compatible = "allwinner,sun5i-a13-usb-phy" },
Maxime Ripardeadd4312014-05-13 17:44:18 +0200559 { .compatible = "allwinner,sun6i-a31-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100560 { .compatible = "allwinner,sun7i-a20-usb-phy" },
561 { },
562};
563MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
564
565static struct platform_driver sun4i_usb_phy_driver = {
566 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200567 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100568 .driver = {
569 .of_match_table = sun4i_usb_phy_of_match,
570 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100571 }
572};
573module_platform_driver(sun4i_usb_phy_driver);
574
575MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
576MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
577MODULE_LICENSE("GPL v2");