blob: 4ba9856ac71a359a658edcece36f96d42c5088ca [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>
Hans de Goede1aedf3a2015-06-13 14:37:50 +020025#include <linux/delay.h>
Sachin Kamat2d84aff2014-05-29 12:00:49 +053026#include <linux/err.h>
Hans de Goede1a52abe2015-06-13 14:37:46 +020027#include <linux/extcon.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010028#include <linux/io.h>
Hans de Goeded2332302015-06-13 14:37:45 +020029#include <linux/interrupt.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010030#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>
Hans de Goeded2332302015-06-13 14:37:45 +020035#include <linux/of_gpio.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010036#include <linux/phy/phy.h>
Hans de Goede24fe86a2015-03-29 12:50:46 +020037#include <linux/phy/phy-sun4i-usb.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010038#include <linux/platform_device.h>
39#include <linux/regulator/consumer.h>
40#include <linux/reset.h>
Hans de Goeded2332302015-06-13 14:37:45 +020041#include <linux/workqueue.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010042
43#define REG_ISCR 0x00
Hans de Goedefc1f45e2015-06-13 14:37:49 +020044#define REG_PHYCTL_A10 0x04
Hans de Goedeba4bdc92014-03-01 18:09:26 +010045#define REG_PHYBIST 0x08
46#define REG_PHYTUNE 0x0c
Hans de Goedefc1f45e2015-06-13 14:37:49 +020047#define REG_PHYCTL_A33 0x10
Hans de Goedeba4bdc92014-03-01 18:09:26 +010048
49#define PHYCTL_DATA BIT(7)
50
51#define SUNXI_AHB_ICHR8_EN BIT(10)
52#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
53#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
54#define SUNXI_ULPI_BYPASS_EN BIT(0)
55
Hans de Goeded2332302015-06-13 14:37:45 +020056/* ISCR, Interface Status and Control bits */
57#define ISCR_ID_PULLUP_EN (1 << 17)
58#define ISCR_DPDM_PULLUP_EN (1 << 16)
59/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
60#define ISCR_FORCE_ID_MASK (3 << 14)
61#define ISCR_FORCE_ID_LOW (2 << 14)
62#define ISCR_FORCE_ID_HIGH (3 << 14)
63#define ISCR_FORCE_VBUS_MASK (3 << 12)
64#define ISCR_FORCE_VBUS_LOW (2 << 12)
65#define ISCR_FORCE_VBUS_HIGH (3 << 12)
66
Hans de Goedeba4bdc92014-03-01 18:09:26 +010067/* Common Control Bits for Both PHYs */
68#define PHY_PLL_BW 0x03
69#define PHY_RES45_CAL_EN 0x0c
70
71/* Private Control Bits for Each PHY */
72#define PHY_TX_AMPLITUDE_TUNE 0x20
73#define PHY_TX_SLEWRATE_TUNE 0x22
74#define PHY_VBUSVALID_TH_SEL 0x25
75#define PHY_PULLUP_RES_SEL 0x27
76#define PHY_OTG_FUNC_EN 0x28
77#define PHY_VBUS_DET_EN 0x29
78#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020079#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010080
81#define MAX_PHYS 3
82
Hans de Goeded2332302015-06-13 14:37:45 +020083/*
84 * Note do not raise the debounce time, we must report Vusb high within 100ms
85 * otherwise we get Vbus errors
86 */
87#define DEBOUNCE_TIME msecs_to_jiffies(50)
88#define POLL_TIME msecs_to_jiffies(250)
89
Hans de Goedeba4bdc92014-03-01 18:09:26 +010090struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +010091 void __iomem *base;
92 struct mutex mutex;
93 int num_phys;
94 u32 disc_thresh;
Hans de Goedefc1f45e2015-06-13 14:37:49 +020095 bool has_a33_phyctl;
Hans de Goedeba4bdc92014-03-01 18:09:26 +010096 struct sun4i_usb_phy {
97 struct phy *phy;
98 void __iomem *pmu;
99 struct regulator *vbus;
100 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200101 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +0200102 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100103 int index;
104 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200105 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200106 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200107 bool phy0_init;
108 bool phy0_poll;
109 struct gpio_desc *id_det_gpio;
110 struct gpio_desc *vbus_det_gpio;
111 int id_det_irq;
112 int vbus_det_irq;
113 int id_det;
114 int vbus_det;
115 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100116};
117
118#define to_sun4i_usb_phy_data(phy) \
119 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
120
Hans de Goeded2332302015-06-13 14:37:45 +0200121static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
122{
123 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
124 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
125 u32 iscr;
126
127 iscr = readl(data->base + REG_ISCR);
128 iscr &= ~clr;
129 iscr |= set;
130 writel(iscr, data->base + REG_ISCR);
131}
132
133static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
134{
135 if (val)
136 val = ISCR_FORCE_ID_HIGH;
137 else
138 val = ISCR_FORCE_ID_LOW;
139
140 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
141}
142
143static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
144{
145 if (val)
146 val = ISCR_FORCE_VBUS_HIGH;
147 else
148 val = ISCR_FORCE_VBUS_LOW;
149
150 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
151}
152
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100153static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
154 int len)
155{
156 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
157 u32 temp, usbc_bit = BIT(phy->index * 2);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200158 void *phyctl;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100159 int i;
160
161 mutex_lock(&phy_data->mutex);
162
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200163 if (phy_data->has_a33_phyctl) {
164 phyctl = phy_data->base + REG_PHYCTL_A33;
165 /* A33 needs us to set phyctl to 0 explicitly */
166 writel(0, phyctl);
167 } else {
168 phyctl = phy_data->base + REG_PHYCTL_A10;
169 }
170
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100171 for (i = 0; i < len; i++) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200172 temp = readl(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100173
174 /* clear the address portion */
175 temp &= ~(0xff << 8);
176
177 /* set the address */
178 temp |= ((addr + i) << 8);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200179 writel(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100180
181 /* set the data bit and clear usbc bit*/
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200182 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100183 if (data & 0x1)
184 temp |= PHYCTL_DATA;
185 else
186 temp &= ~PHYCTL_DATA;
187 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200188 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100189
190 /* pulse usbc_bit */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200191 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100192 temp |= usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200193 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100194
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200195 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100196 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200197 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100198
199 data >>= 1;
200 }
201 mutex_unlock(&phy_data->mutex);
202}
203
204static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
205{
206 u32 bits, reg_value;
207
208 if (!phy->pmu)
209 return;
210
211 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
212 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
213
214 reg_value = readl(phy->pmu);
215
216 if (enable)
217 reg_value |= bits;
218 else
219 reg_value &= ~bits;
220
221 writel(reg_value, phy->pmu);
222}
223
224static int sun4i_usb_phy_init(struct phy *_phy)
225{
226 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
227 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
228 int ret;
229
Maxime Ripardeadd4312014-05-13 17:44:18 +0200230 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100231 if (ret)
232 return ret;
233
234 ret = reset_control_deassert(phy->reset);
235 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200236 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100237 return ret;
238 }
239
Roman Byshko6827a46f2014-11-10 19:55:06 +0100240 /* Enable USB 45 Ohm resistor calibration */
241 if (phy->index == 0)
242 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
243
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100244 /* Adjust PHY's magnitude and rate */
245 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
246
247 /* Disconnect threshold adjustment */
248 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
249
250 sun4i_usb_phy_passby(phy, 1);
251
Hans de Goeded2332302015-06-13 14:37:45 +0200252 if (phy->index == 0) {
253 data->phy0_init = true;
254
255 /* Enable pull-ups */
256 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
257 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
258
259 if (data->id_det_gpio) {
260 /* OTG mode, force ISCR and cable state updates */
261 data->id_det = -1;
262 data->vbus_det = -1;
263 queue_delayed_work(system_wq, &data->detect, 0);
264 } else {
265 /* Host only mode */
266 sun4i_usb_phy0_set_id_detect(_phy, 0);
267 sun4i_usb_phy0_set_vbus_detect(_phy, 1);
268 }
269 }
270
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100271 return 0;
272}
273
274static int sun4i_usb_phy_exit(struct phy *_phy)
275{
276 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200277 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
278
279 if (phy->index == 0) {
280 /* Disable pull-ups */
281 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
282 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
283 data->phy0_init = false;
284 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100285
286 sun4i_usb_phy_passby(phy, 0);
287 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200288 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100289
290 return 0;
291}
292
293static int sun4i_usb_phy_power_on(struct phy *_phy)
294{
295 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200296 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
297 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100298
Hans de Goeded2332302015-06-13 14:37:45 +0200299 if (!phy->vbus || phy->regulator_on)
300 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100301
Hans de Goeded2332302015-06-13 14:37:45 +0200302 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
303 if (phy->index == 0 && data->vbus_det)
304 return 0;
305
306 ret = regulator_enable(phy->vbus);
307 if (ret)
308 return ret;
309
310 phy->regulator_on = true;
311
312 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200313 if (phy->index == 0 && data->vbus_det_gpio && data->phy0_poll)
Hans de Goeded2332302015-06-13 14:37:45 +0200314 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
315
316 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100317}
318
319static int sun4i_usb_phy_power_off(struct phy *_phy)
320{
321 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200322 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100323
Hans de Goeded2332302015-06-13 14:37:45 +0200324 if (!phy->vbus || !phy->regulator_on)
325 return 0;
326
327 regulator_disable(phy->vbus);
328 phy->regulator_on = false;
329
330 /*
331 * phy0 vbus typically slowly discharges, sometimes this causes the
332 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
333 */
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200334 if (phy->index == 0 && data->vbus_det_gpio && !data->phy0_poll)
Hans de Goeded2332302015-06-13 14:37:45 +0200335 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100336
337 return 0;
338}
339
Hans de Goede24fe86a2015-03-29 12:50:46 +0200340void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
341{
342 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
343
344 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
345}
346
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100347static struct phy_ops sun4i_usb_phy_ops = {
348 .init = sun4i_usb_phy_init,
349 .exit = sun4i_usb_phy_exit,
350 .power_on = sun4i_usb_phy_power_on,
351 .power_off = sun4i_usb_phy_power_off,
352 .owner = THIS_MODULE,
353};
354
Hans de Goeded2332302015-06-13 14:37:45 +0200355static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
356{
357 struct sun4i_usb_phy_data *data =
358 container_of(work, struct sun4i_usb_phy_data, detect.work);
359 struct phy *phy0 = data->phys[0].phy;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200360 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
Hans de Goeded2332302015-06-13 14:37:45 +0200361
362 id_det = gpiod_get_value_cansleep(data->id_det_gpio);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200363 if (data->vbus_det_gpio)
364 vbus_det = gpiod_get_value_cansleep(data->vbus_det_gpio);
365 else
366 vbus_det = 1; /* Report vbus as high */
Hans de Goeded2332302015-06-13 14:37:45 +0200367
368 mutex_lock(&phy0->mutex);
369
370 if (!data->phy0_init) {
371 mutex_unlock(&phy0->mutex);
372 return;
373 }
374
375 if (id_det != data->id_det) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200376 /*
377 * When a host cable (id == 0) gets plugged in on systems
378 * without vbus detection report vbus low for long enough for
379 * the musb-ip to end the current device session.
380 */
381 if (!data->vbus_det_gpio && id_det == 0) {
382 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
383 msleep(200);
384 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
385 }
Hans de Goeded2332302015-06-13 14:37:45 +0200386 sun4i_usb_phy0_set_id_detect(phy0, id_det);
387 data->id_det = id_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200388 id_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200389 }
390
391 if (vbus_det != data->vbus_det) {
392 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
393 data->vbus_det = vbus_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200394 vbus_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200395 }
396
397 mutex_unlock(&phy0->mutex);
398
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200399 if (id_notify) {
Hans de Goede1a52abe2015-06-13 14:37:46 +0200400 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
401 !id_det);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200402 /*
403 * When a host cable gets unplugged (id == 1) on systems
404 * without vbus detection report vbus low for long enough to
405 * the musb-ip to end the current host session.
406 */
407 if (!data->vbus_det_gpio && id_det == 1) {
408 mutex_lock(&phy0->mutex);
409 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
410 msleep(1000);
411 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
412 mutex_unlock(&phy0->mutex);
413 }
414 }
Hans de Goede1a52abe2015-06-13 14:37:46 +0200415
416 if (vbus_notify)
417 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
418
Hans de Goeded2332302015-06-13 14:37:45 +0200419 if (data->phy0_poll)
420 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
421}
422
423static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
424{
425 struct sun4i_usb_phy_data *data = dev_id;
426
427 /* vbus or id changed, let the pins settle and then scan them */
428 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
429
430 return IRQ_HANDLED;
431}
432
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100433static struct phy *sun4i_usb_phy_xlate(struct device *dev,
434 struct of_phandle_args *args)
435{
436 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
437
Roman Byshko6827a46f2014-11-10 19:55:06 +0100438 if (args->args[0] >= data->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100439 return ERR_PTR(-ENODEV);
440
441 return data->phys[args->args[0]].phy;
442}
443
Hans de Goeded2332302015-06-13 14:37:45 +0200444static int sun4i_usb_phy_remove(struct platform_device *pdev)
445{
446 struct device *dev = &pdev->dev;
447 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
448
449 if (data->id_det_irq >= 0)
450 devm_free_irq(dev, data->id_det_irq, data);
451 if (data->vbus_det_irq >= 0)
452 devm_free_irq(dev, data->vbus_det_irq, data);
453
454 cancel_delayed_work_sync(&data->detect);
455
456 return 0;
457}
458
Hans de Goede1a52abe2015-06-13 14:37:46 +0200459static const unsigned int sun4i_usb_phy0_cable[] = {
460 EXTCON_USB,
461 EXTCON_USB_HOST,
462 EXTCON_NONE,
463};
464
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100465static int sun4i_usb_phy_probe(struct platform_device *pdev)
466{
467 struct sun4i_usb_phy_data *data;
468 struct device *dev = &pdev->dev;
469 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100470 struct phy_provider *phy_provider;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200471 bool dedicated_clocks;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100472 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200473 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100474
475 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
476 if (!data)
477 return -ENOMEM;
478
479 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200480 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
481 dev_set_drvdata(dev, data);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100482
Hans de Goede123dfdb2015-06-13 14:37:48 +0200483 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200484 of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy") ||
485 of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100486 data->num_phys = 2;
487 else
488 data->num_phys = 3;
489
Hans de Goede28464692015-06-13 14:37:47 +0200490 if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy") ||
491 of_device_is_compatible(np, "allwinner,sun7i-a20-usb-phy"))
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100492 data->disc_thresh = 2;
Hans de Goede28464692015-06-13 14:37:47 +0200493 else
494 data->disc_thresh = 3;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100495
Hans de Goede123dfdb2015-06-13 14:37:48 +0200496 if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy") ||
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200497 of_device_is_compatible(np, "allwinner,sun8i-a23-usb-phy") ||
498 of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
Maxime Ripardeadd4312014-05-13 17:44:18 +0200499 dedicated_clocks = true;
500 else
501 dedicated_clocks = false;
502
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200503 if (of_device_is_compatible(np, "allwinner,sun8i-a33-usb-phy"))
504 data->has_a33_phyctl = true;
505
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100506 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
507 data->base = devm_ioremap_resource(dev, res);
508 if (IS_ERR(data->base))
509 return PTR_ERR(data->base);
510
Hans de Goeded2332302015-06-13 14:37:45 +0200511 data->id_det_gpio = devm_gpiod_get(dev, "usb0_id_det", GPIOD_IN);
512 if (IS_ERR(data->id_det_gpio)) {
513 if (PTR_ERR(data->id_det_gpio) == -EPROBE_DEFER)
514 return -EPROBE_DEFER;
515 data->id_det_gpio = NULL;
516 }
517
518 data->vbus_det_gpio = devm_gpiod_get(dev, "usb0_vbus_det", GPIOD_IN);
519 if (IS_ERR(data->vbus_det_gpio)) {
520 if (PTR_ERR(data->vbus_det_gpio) == -EPROBE_DEFER)
521 return -EPROBE_DEFER;
522 data->vbus_det_gpio = NULL;
523 }
524
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200525 /* vbus_det without id_det makes no sense, and is not supported */
526 if (data->vbus_det_gpio && !data->id_det_gpio) {
527 dev_err(dev, "usb0_id_det missing or invalid\n");
Hans de Goeded2332302015-06-13 14:37:45 +0200528 return -ENODEV;
529 }
530
Hans de Goede1a52abe2015-06-13 14:37:46 +0200531 if (data->id_det_gpio) {
532 data->extcon = devm_extcon_dev_allocate(dev,
533 sun4i_usb_phy0_cable);
534 if (IS_ERR(data->extcon))
535 return PTR_ERR(data->extcon);
536
537 ret = devm_extcon_dev_register(dev, data->extcon);
538 if (ret) {
539 dev_err(dev, "failed to register extcon: %d\n", ret);
540 return ret;
541 }
542 }
543
Roman Byshko6827a46f2014-11-10 19:55:06 +0100544 for (i = 0; i < data->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200545 struct sun4i_usb_phy *phy = data->phys + i;
546 char name[16];
547
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100548 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200549 phy->vbus = devm_regulator_get_optional(dev, name);
550 if (IS_ERR(phy->vbus)) {
551 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100552 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200553 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100554 }
555
Maxime Ripardeadd4312014-05-13 17:44:18 +0200556 if (dedicated_clocks)
557 snprintf(name, sizeof(name), "usb%d_phy", i);
558 else
559 strlcpy(name, "usb_phy", sizeof(name));
560
561 phy->clk = devm_clk_get(dev, name);
562 if (IS_ERR(phy->clk)) {
563 dev_err(dev, "failed to get clock %s\n", name);
564 return PTR_ERR(phy->clk);
565 }
566
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100567 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200568 phy->reset = devm_reset_control_get(dev, name);
569 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100570 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200571 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100572 }
573
574 if (i) { /* No pmu for usbc0 */
575 snprintf(name, sizeof(name), "pmu%d", i);
576 res = platform_get_resource_byname(pdev,
577 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200578 phy->pmu = devm_ioremap_resource(dev, res);
579 if (IS_ERR(phy->pmu))
580 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100581 }
582
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200583 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200584 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100585 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200586 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100587 }
588
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200589 phy->index = i;
590 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100591 }
592
Hans de Goeded2332302015-06-13 14:37:45 +0200593 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
594 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200595 if ((data->id_det_gpio && data->id_det_irq < 0) ||
596 (data->vbus_det_gpio && data->vbus_det_irq < 0))
Hans de Goeded2332302015-06-13 14:37:45 +0200597 data->phy0_poll = true;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100598
Hans de Goeded2332302015-06-13 14:37:45 +0200599 if (data->id_det_irq >= 0) {
600 ret = devm_request_irq(dev, data->id_det_irq,
601 sun4i_usb_phy0_id_vbus_det_irq,
602 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
603 "usb0-id-det", data);
604 if (ret) {
605 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
606 return ret;
607 }
608 }
609
610 if (data->vbus_det_irq >= 0) {
611 ret = devm_request_irq(dev, data->vbus_det_irq,
612 sun4i_usb_phy0_id_vbus_det_irq,
613 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
614 "usb0-vbus-det", data);
615 if (ret) {
616 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
617 data->vbus_det_irq = -1;
618 sun4i_usb_phy_remove(pdev); /* Stop detect work */
619 return ret;
620 }
621 }
622
623 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
624 if (IS_ERR(phy_provider)) {
625 sun4i_usb_phy_remove(pdev); /* Stop detect work */
626 return PTR_ERR(phy_provider);
627 }
628
629 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100630}
631
632static const struct of_device_id sun4i_usb_phy_of_match[] = {
633 { .compatible = "allwinner,sun4i-a10-usb-phy" },
634 { .compatible = "allwinner,sun5i-a13-usb-phy" },
Maxime Ripardeadd4312014-05-13 17:44:18 +0200635 { .compatible = "allwinner,sun6i-a31-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100636 { .compatible = "allwinner,sun7i-a20-usb-phy" },
Hans de Goede123dfdb2015-06-13 14:37:48 +0200637 { .compatible = "allwinner,sun8i-a23-usb-phy" },
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200638 { .compatible = "allwinner,sun8i-a33-usb-phy" },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100639 { },
640};
641MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
642
643static struct platform_driver sun4i_usb_phy_driver = {
644 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200645 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100646 .driver = {
647 .of_match_table = sun4i_usb_phy_of_match,
648 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100649 }
650};
651module_platform_driver(sun4i_usb_phy_driver);
652
653MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
654MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
655MODULE_LICENSE("GPL v2");