blob: 00424dde79c120d8c9b01f1e9553d35319198680 [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
Hans de Goede123dfdb2015-06-13 14:37:48 +0200445 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
446 of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100447 data->num_phys = 2;
448 else
449 data->num_phys = 3;
450
Hans de Goede28464692015-06-13 14:37:47 +0200451 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
452 of_device_is_compatible(np, "allwinner,sun7i-a20-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100453 data->disc_thresh = 2;
Hans de Goede28464692015-06-13 14:37:47 +0200454 else
455 data->disc_thresh = 3;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100456
Hans de Goede123dfdb2015-06-13 14:37:48 +0200457 if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy") ||
458 of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy"))
Maxime Ripardeadd4312014-05-13 17:44:18 +0200459 dedicated_clocks = true;
460 else
461 dedicated_clocks = false;
462
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100463 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
464 data->base = devm_ioremap_resource(dev, res);
465 if (IS_ERR(data->base))
466 return PTR_ERR(data->base);
467
Hans de Goeded2332302015-06-13 14:37:45 +0200468 data->id_det_gpio = devm_gpiod_get(dev, "usb0_id_det", GPIOD_IN);
469 if (IS_ERR(data->id_det_gpio)) {
470 if (PTR_ERR(data->id_det_gpio) == -EPROBE_DEFER)
471 return -EPROBE_DEFER;
472 data->id_det_gpio = NULL;
473 }
474
475 data->vbus_det_gpio = devm_gpiod_get(dev, "usb0_vbus_det", GPIOD_IN);
476 if (IS_ERR(data->vbus_det_gpio)) {
477 if (PTR_ERR(data->vbus_det_gpio) == -EPROBE_DEFER)
478 return -EPROBE_DEFER;
479 data->vbus_det_gpio = NULL;
480 }
481
482 /* We either want both gpio pins or neither (when in host mode) */
483 if (!data->id_det_gpio != !data->vbus_det_gpio) {
484 dev_err(dev, "failed to get id or vbus detect pin\n");
485 return -ENODEV;
486 }
487
Hans de Goede1a52abe2015-06-13 14:37:46 +0200488 if (data->id_det_gpio) {
489 data->extcon = devm_extcon_dev_allocate(dev,
490 sun4i_usb_phy0_cable);
491 if (IS_ERR(data->extcon))
492 return PTR_ERR(data->extcon);
493
494 ret = devm_extcon_dev_register(dev, data->extcon);
495 if (ret) {
496 dev_err(dev, "failed to register extcon: %d\n", ret);
497 return ret;
498 }
499 }
500
Roman Byshko6827a46f2014-11-10 19:55:06 +0100501 for (i = 0; i < data->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200502 struct sun4i_usb_phy *phy = data->phys + i;
503 char name[16];
504
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100505 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200506 phy->vbus = devm_regulator_get_optional(dev, name);
507 if (IS_ERR(phy->vbus)) {
508 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100509 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200510 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100511 }
512
Maxime Ripardeadd4312014-05-13 17:44:18 +0200513 if (dedicated_clocks)
514 snprintf(name, sizeof(name), "usb%d_phy", i);
515 else
516 strlcpy(name, "usb_phy", sizeof(name));
517
518 phy->clk = devm_clk_get(dev, name);
519 if (IS_ERR(phy->clk)) {
520 dev_err(dev, "failed to get clock %s\n", name);
521 return PTR_ERR(phy->clk);
522 }
523
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100524 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200525 phy->reset = devm_reset_control_get(dev, name);
526 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100527 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200528 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100529 }
530
531 if (i) { /* No pmu for usbc0 */
532 snprintf(name, sizeof(name), "pmu%d", i);
533 res = platform_get_resource_byname(pdev,
534 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200535 phy->pmu = devm_ioremap_resource(dev, res);
536 if (IS_ERR(phy->pmu))
537 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100538 }
539
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200540 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200541 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100542 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200543 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100544 }
545
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200546 phy->index = i;
547 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100548 }
549
Hans de Goeded2332302015-06-13 14:37:45 +0200550 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
551 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
552 if (data->id_det_irq < 0 || data->vbus_det_irq < 0)
553 data->phy0_poll = true;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100554
Hans de Goeded2332302015-06-13 14:37:45 +0200555 if (data->id_det_irq >= 0) {
556 ret = devm_request_irq(dev, data->id_det_irq,
557 sun4i_usb_phy0_id_vbus_det_irq,
558 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
559 "usb0-id-det", data);
560 if (ret) {
561 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
562 return ret;
563 }
564 }
565
566 if (data->vbus_det_irq >= 0) {
567 ret = devm_request_irq(dev, data->vbus_det_irq,
568 sun4i_usb_phy0_id_vbus_det_irq,
569 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
570 "usb0-vbus-det", data);
571 if (ret) {
572 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
573 data->vbus_det_irq = -1;
574 sun4i_usb_phy_remove(pdev); /* Stop detect work */
575 return ret;
576 }
577 }
578
579 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
580 if (IS_ERR(phy_provider)) {
581 sun4i_usb_phy_remove(pdev); /* Stop detect work */
582 return PTR_ERR(phy_provider);
583 }
584
585 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100586}
587
588static const struct of_device_id sun4i_usb_phy_of_match[] = {
589 { .compatible = "allwinner,sun4i-a10-usb-phy" },
590 { .compatible = "allwinner,sun5i-a13-usb-phy" },
Maxime Ripardeadd4312014-05-13 17:44:18 +0200591 { .compatible = "allwinner,sun6i-a31-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100592 { .compatible = "allwinner,sun7i-a20-usb-phy" },
Hans de Goede123dfdb2015-06-13 14:37:48 +0200593 { .compatible = "allwinner,sun8i-a23-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100594 { },
595};
596MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
597
598static struct platform_driver sun4i_usb_phy_driver = {
599 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200600 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100601 .driver = {
602 .of_match_table = sun4i_usb_phy_of_match,
603 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100604 }
605};
606module_platform_driver(sun4i_usb_phy_driver);
607
608MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
609MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
610MODULE_LICENSE("GPL v2");