blob: ddc399536c5c2bc0d5ca24c0506f0238478146f8 [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
Hans de Goedefc1f45e2015-06-13 14:37:49 +020043#define REG_PHYCTL_A10 0x04
Hans de Goedeba4bdc92014-03-01 18:09:26 +010044#define REG_PHYBIST 0x08
45#define REG_PHYTUNE 0x0c
Hans de Goedefc1f45e2015-06-13 14:37:49 +020046#define REG_PHYCTL_A33 0x10
Hans de Goedeba4bdc92014-03-01 18:09:26 +010047
48#define PHYCTL_DATA BIT(7)
49
50#define SUNXI_AHB_ICHR8_EN BIT(10)
51#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
52#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
53#define SUNXI_ULPI_BYPASS_EN BIT(0)
54
Hans de Goeded2332302015-06-13 14:37:45 +020055/* ISCR, Interface Status and Control bits */
56#define ISCR_ID_PULLUP_EN (1 << 17)
57#define ISCR_DPDM_PULLUP_EN (1 << 16)
58/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
59#define ISCR_FORCE_ID_MASK (3 << 14)
60#define ISCR_FORCE_ID_LOW (2 << 14)
61#define ISCR_FORCE_ID_HIGH (3 << 14)
62#define ISCR_FORCE_VBUS_MASK (3 << 12)
63#define ISCR_FORCE_VBUS_LOW (2 << 12)
64#define ISCR_FORCE_VBUS_HIGH (3 << 12)
65
Hans de Goedeba4bdc92014-03-01 18:09:26 +010066/* Common Control Bits for Both PHYs */
67#define PHY_PLL_BW 0x03
68#define PHY_RES45_CAL_EN 0x0c
69
70/* Private Control Bits for Each PHY */
71#define PHY_TX_AMPLITUDE_TUNE 0x20
72#define PHY_TX_SLEWRATE_TUNE 0x22
73#define PHY_VBUSVALID_TH_SEL 0x25
74#define PHY_PULLUP_RES_SEL 0x27
75#define PHY_OTG_FUNC_EN 0x28
76#define PHY_VBUS_DET_EN 0x29
77#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020078#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010079
80#define MAX_PHYS 3
81
Hans de Goeded2332302015-06-13 14:37:45 +020082/*
83 * Note do not raise the debounce time, we must report Vusb high within 100ms
84 * otherwise we get Vbus errors
85 */
86#define DEBOUNCE_TIME msecs_to_jiffies(50)
87#define POLL_TIME msecs_to_jiffies(250)
88
Hans de Goedeba4bdc92014-03-01 18:09:26 +010089struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +010090 void __iomem *base;
91 struct mutex mutex;
92 int num_phys;
93 u32 disc_thresh;
Hans de Goedefc1f45e2015-06-13 14:37:49 +020094 bool has_a33_phyctl;
Hans de Goedeba4bdc92014-03-01 18:09:26 +010095 struct sun4i_usb_phy {
96 struct phy *phy;
97 void __iomem *pmu;
98 struct regulator *vbus;
99 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200100 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +0200101 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100102 int index;
103 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200104 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200105 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200106 bool phy0_init;
107 bool phy0_poll;
108 struct gpio_desc *id_det_gpio;
109 struct gpio_desc *vbus_det_gpio;
110 int id_det_irq;
111 int vbus_det_irq;
112 int id_det;
113 int vbus_det;
114 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100115};
116
117#define to_sun4i_usb_phy_data(phy) \
118 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
119
Hans de Goeded2332302015-06-13 14:37:45 +0200120static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
121{
122 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
123 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
124 u32 iscr;
125
126 iscr = readl(data->base + REG_ISCR);
127 iscr &= ~clr;
128 iscr |= set;
129 writel(iscr, data->base + REG_ISCR);
130}
131
132static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
133{
134 if (val)
135 val = ISCR_FORCE_ID_HIGH;
136 else
137 val = ISCR_FORCE_ID_LOW;
138
139 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
140}
141
142static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
143{
144 if (val)
145 val = ISCR_FORCE_VBUS_HIGH;
146 else
147 val = ISCR_FORCE_VBUS_LOW;
148
149 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
150}
151
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100152static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
153 int len)
154{
155 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
156 u32 temp, usbc_bit = BIT(phy->index * 2);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200157 void *phyctl;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100158 int i;
159
160 mutex_lock(&phy_data->mutex);
161
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200162 if (phy_data->has_a33_phyctl) {
163 phyctl = phy_data->base + REG_PHYCTL_A33;
164 /* A33 needs us to set phyctl to 0 explicitly */
165 writel(0, phyctl);
166 } else {
167 phyctl = phy_data->base + REG_PHYCTL_A10;
168 }
169
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100170 for (i = 0; i < len; i++) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200171 temp = readl(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100172
173 /* clear the address portion */
174 temp &= ~(0xff << 8);
175
176 /* set the address */
177 temp |= ((addr + i) << 8);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200178 writel(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100179
180 /* set the data bit and clear usbc bit*/
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200181 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100182 if (data & 0x1)
183 temp |= PHYCTL_DATA;
184 else
185 temp &= ~PHYCTL_DATA;
186 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200187 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100188
189 /* pulse usbc_bit */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200190 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100191 temp |= usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200192 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100193
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200194 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100195 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200196 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100197
198 data >>= 1;
199 }
200 mutex_unlock(&phy_data->mutex);
201}
202
203static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
204{
205 u32 bits, reg_value;
206
207 if (!phy->pmu)
208 return;
209
210 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
211 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
212
213 reg_value = readl(phy->pmu);
214
215 if (enable)
216 reg_value |= bits;
217 else
218 reg_value &= ~bits;
219
220 writel(reg_value, phy->pmu);
221}
222
223static int sun4i_usb_phy_init(struct phy *_phy)
224{
225 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
226 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
227 int ret;
228
Maxime Ripardeadd4312014-05-13 17:44:18 +0200229 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100230 if (ret)
231 return ret;
232
233 ret = reset_control_deassert(phy->reset);
234 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200235 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100236 return ret;
237 }
238
Roman Byshko6827a46f2014-11-10 19:55:06 +0100239 /* Enable USB 45 Ohm resistor calibration */
240 if (phy->index == 0)
241 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
242
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100243 /* Adjust PHY's magnitude and rate */
244 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
245
246 /* Disconnect threshold adjustment */
247 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
248
249 sun4i_usb_phy_passby(phy, 1);
250
Hans de Goeded2332302015-06-13 14:37:45 +0200251 if (phy->index == 0) {
252 data->phy0_init = true;
253
254 /* Enable pull-ups */
255 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
256 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
257
258 if (data->id_det_gpio) {
259 /* OTG mode, force ISCR and cable state updates */
260 data->id_det = -1;
261 data->vbus_det = -1;
262 queue_delayed_work(system_wq, &data->detect, 0);
263 } else {
264 /* Host only mode */
265 sun4i_usb_phy0_set_id_detect(_phy, 0);
266 sun4i_usb_phy0_set_vbus_detect(_phy, 1);
267 }
268 }
269
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100270 return 0;
271}
272
273static int sun4i_usb_phy_exit(struct phy *_phy)
274{
275 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200276 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
277
278 if (phy->index == 0) {
279 /* Disable pull-ups */
280 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
281 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
282 data->phy0_init = false;
283 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100284
285 sun4i_usb_phy_passby(phy, 0);
286 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200287 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100288
289 return 0;
290}
291
292static int sun4i_usb_phy_power_on(struct phy *_phy)
293{
294 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200295 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
296 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100297
Hans de Goeded2332302015-06-13 14:37:45 +0200298 if (!phy->vbus || phy->regulator_on)
299 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100300
Hans de Goeded2332302015-06-13 14:37:45 +0200301 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
302 if (phy->index == 0 && data->vbus_det)
303 return 0;
304
305 ret = regulator_enable(phy->vbus);
306 if (ret)
307 return ret;
308
309 phy->regulator_on = true;
310
311 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
312 if (phy->index == 0 && data->phy0_poll)
313 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
314
315 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100316}
317
318static int sun4i_usb_phy_power_off(struct phy *_phy)
319{
320 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200321 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100322
Hans de Goeded2332302015-06-13 14:37:45 +0200323 if (!phy->vbus || !phy->regulator_on)
324 return 0;
325
326 regulator_disable(phy->vbus);
327 phy->regulator_on = false;
328
329 /*
330 * phy0 vbus typically slowly discharges, sometimes this causes the
331 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
332 */
333 if (phy->index == 0 && !data->phy0_poll)
334 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100335
336 return 0;
337}
338
Hans de Goede24fe86a2015-03-29 12:50:46 +0200339void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
340{
341 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
342
343 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
344}
345
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100346static struct phy_ops sun4i_usb_phy_ops = {
347 .init = sun4i_usb_phy_init,
348 .exit = sun4i_usb_phy_exit,
349 .power_on = sun4i_usb_phy_power_on,
350 .power_off = sun4i_usb_phy_power_off,
351 .owner = THIS_MODULE,
352};
353
Hans de Goeded2332302015-06-13 14:37:45 +0200354static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
355{
356 struct sun4i_usb_phy_data *data =
357 container_of(work, struct sun4i_usb_phy_data, detect.work);
358 struct phy *phy0 = data->phys[0].phy;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200359 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
Hans de Goeded2332302015-06-13 14:37:45 +0200360
361 id_det = gpiod_get_value_cansleep(data->id_det_gpio);
362 vbus_det = gpiod_get_value_cansleep(data->vbus_det_gpio);
363
364 mutex_lock(&phy0->mutex);
365
366 if (!data->phy0_init) {
367 mutex_unlock(&phy0->mutex);
368 return;
369 }
370
371 if (id_det != data->id_det) {
372 sun4i_usb_phy0_set_id_detect(phy0, id_det);
373 data->id_det = id_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200374 id_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200375 }
376
377 if (vbus_det != data->vbus_det) {
378 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
379 data->vbus_det = vbus_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200380 vbus_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200381 }
382
383 mutex_unlock(&phy0->mutex);
384
Hans de Goede1a52abe2015-06-13 14:37:46 +0200385 if (id_notify)
386 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
387 !id_det);
388
389 if (vbus_notify)
390 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
391
Hans de Goeded2332302015-06-13 14:37:45 +0200392 if (data->phy0_poll)
393 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
394}
395
396static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
397{
398 struct sun4i_usb_phy_data *data = dev_id;
399
400 /* vbus or id changed, let the pins settle and then scan them */
401 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
402
403 return IRQ_HANDLED;
404}
405
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100406static struct phy *sun4i_usb_phy_xlate(struct device *dev,
407 struct of_phandle_args *args)
408{
409 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
410
Roman Byshko6827a46f2014-11-10 19:55:06 +0100411 if (args->args[0] >= data->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100412 return ERR_PTR(-ENODEV);
413
414 return data->phys[args->args[0]].phy;
415}
416
Hans de Goeded2332302015-06-13 14:37:45 +0200417static int sun4i_usb_phy_remove(struct platform_device *pdev)
418{
419 struct device *dev = &pdev->dev;
420 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
421
422 if (data->id_det_irq >= 0)
423 devm_free_irq(dev, data->id_det_irq, data);
424 if (data->vbus_det_irq >= 0)
425 devm_free_irq(dev, data->vbus_det_irq, data);
426
427 cancel_delayed_work_sync(&data->detect);
428
429 return 0;
430}
431
Hans de Goede1a52abe2015-06-13 14:37:46 +0200432static const unsigned int sun4i_usb_phy0_cable[] = {
433 EXTCON_USB,
434 EXTCON_USB_HOST,
435 EXTCON_NONE,
436};
437
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100438static int sun4i_usb_phy_probe(struct platform_device *pdev)
439{
440 struct sun4i_usb_phy_data *data;
441 struct device *dev = &pdev->dev;
442 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100443 struct phy_provider *phy_provider;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200444 bool dedicated_clocks;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100445 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200446 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100447
448 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
449 if (!data)
450 return -ENOMEM;
451
452 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200453 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
454 dev_set_drvdata(dev, data);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100455
Hans de Goede123dfdb2015-06-13 14:37:48 +0200456 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200457 of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy") ||
458 of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100459 data->num_phys = 2;
460 else
461 data->num_phys = 3;
462
Hans de Goede28464692015-06-13 14:37:47 +0200463 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
464 of_device_is_compatible(np, "allwinner,sun7i-a20-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100465 data->disc_thresh = 2;
Hans de Goede28464692015-06-13 14:37:47 +0200466 else
467 data->disc_thresh = 3;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100468
Hans de Goede123dfdb2015-06-13 14:37:48 +0200469 if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy") ||
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200470 of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy") ||
471 of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
Maxime Ripardeadd4312014-05-13 17:44:18 +0200472 dedicated_clocks = true;
473 else
474 dedicated_clocks = false;
475
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200476 if (of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
477 data->has_a33_phyctl = true;
478
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100479 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
480 data->base = devm_ioremap_resource(dev, res);
481 if (IS_ERR(data->base))
482 return PTR_ERR(data->base);
483
Hans de Goeded2332302015-06-13 14:37:45 +0200484 data->id_det_gpio = devm_gpiod_get(dev, "usb0_id_det", GPIOD_IN);
485 if (IS_ERR(data->id_det_gpio)) {
486 if (PTR_ERR(data->id_det_gpio) == -EPROBE_DEFER)
487 return -EPROBE_DEFER;
488 data->id_det_gpio = NULL;
489 }
490
491 data->vbus_det_gpio = devm_gpiod_get(dev, "usb0_vbus_det", GPIOD_IN);
492 if (IS_ERR(data->vbus_det_gpio)) {
493 if (PTR_ERR(data->vbus_det_gpio) == -EPROBE_DEFER)
494 return -EPROBE_DEFER;
495 data->vbus_det_gpio = NULL;
496 }
497
498 /* We either want both gpio pins or neither (when in host mode) */
499 if (!data->id_det_gpio != !data->vbus_det_gpio) {
500 dev_err(dev, "failed to get id or vbus detect pin\n");
501 return -ENODEV;
502 }
503
Hans de Goede1a52abe2015-06-13 14:37:46 +0200504 if (data->id_det_gpio) {
505 data->extcon = devm_extcon_dev_allocate(dev,
506 sun4i_usb_phy0_cable);
507 if (IS_ERR(data->extcon))
508 return PTR_ERR(data->extcon);
509
510 ret = devm_extcon_dev_register(dev, data->extcon);
511 if (ret) {
512 dev_err(dev, "failed to register extcon: %d\n", ret);
513 return ret;
514 }
515 }
516
Roman Byshko6827a46f2014-11-10 19:55:06 +0100517 for (i = 0; i < data->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200518 struct sun4i_usb_phy *phy = data->phys + i;
519 char name[16];
520
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100521 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200522 phy->vbus = devm_regulator_get_optional(dev, name);
523 if (IS_ERR(phy->vbus)) {
524 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100525 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200526 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100527 }
528
Maxime Ripardeadd4312014-05-13 17:44:18 +0200529 if (dedicated_clocks)
530 snprintf(name, sizeof(name), "usb%d_phy", i);
531 else
532 strlcpy(name, "usb_phy", sizeof(name));
533
534 phy->clk = devm_clk_get(dev, name);
535 if (IS_ERR(phy->clk)) {
536 dev_err(dev, "failed to get clock %s\n", name);
537 return PTR_ERR(phy->clk);
538 }
539
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100540 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200541 phy->reset = devm_reset_control_get(dev, name);
542 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100543 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200544 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100545 }
546
547 if (i) { /* No pmu for usbc0 */
548 snprintf(name, sizeof(name), "pmu%d", i);
549 res = platform_get_resource_byname(pdev,
550 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200551 phy->pmu = devm_ioremap_resource(dev, res);
552 if (IS_ERR(phy->pmu))
553 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100554 }
555
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200556 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200557 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100558 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200559 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100560 }
561
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200562 phy->index = i;
563 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100564 }
565
Hans de Goeded2332302015-06-13 14:37:45 +0200566 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
567 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
568 if (data->id_det_irq < 0 || data->vbus_det_irq < 0)
569 data->phy0_poll = true;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100570
Hans de Goeded2332302015-06-13 14:37:45 +0200571 if (data->id_det_irq >= 0) {
572 ret = devm_request_irq(dev, data->id_det_irq,
573 sun4i_usb_phy0_id_vbus_det_irq,
574 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
575 "usb0-id-det", data);
576 if (ret) {
577 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
578 return ret;
579 }
580 }
581
582 if (data->vbus_det_irq >= 0) {
583 ret = devm_request_irq(dev, data->vbus_det_irq,
584 sun4i_usb_phy0_id_vbus_det_irq,
585 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
586 "usb0-vbus-det", data);
587 if (ret) {
588 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
589 data->vbus_det_irq = -1;
590 sun4i_usb_phy_remove(pdev); /* Stop detect work */
591 return ret;
592 }
593 }
594
595 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
596 if (IS_ERR(phy_provider)) {
597 sun4i_usb_phy_remove(pdev); /* Stop detect work */
598 return PTR_ERR(phy_provider);
599 }
600
601 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100602}
603
604static const struct of_device_id sun4i_usb_phy_of_match[] = {
605 { .compatible = "allwinner,sun4i-a10-usb-phy" },
606 { .compatible = "allwinner,sun5i-a13-usb-phy" },
Maxime Ripardeadd4312014-05-13 17:44:18 +0200607 { .compatible = "allwinner,sun6i-a31-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100608 { .compatible = "allwinner,sun7i-a20-usb-phy" },
Hans de Goede123dfdb2015-06-13 14:37:48 +0200609 { .compatible = "allwinner,sun8i-a23-usb-phy" },
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200610 { .compatible = "allwinner,sun8i-a33-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100611 { },
612};
613MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
614
615static struct platform_driver sun4i_usb_phy_driver = {
616 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200617 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100618 .driver = {
619 .of_match_table = sun4i_usb_phy_of_match,
620 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100621 }
622};
623module_platform_driver(sun4i_usb_phy_driver);
624
625MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
626MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
627MODULE_LICENSE("GPL v2");