blob: 35b1fa3b71fec85ebbb641230f880319b79afa43 [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 Goede68dbc2c2015-12-11 16:32:17 +010035#include <linux/of_device.h>
Hans de Goeded2332302015-06-13 14:37:45 +020036#include <linux/of_gpio.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010037#include <linux/phy/phy.h>
Hans de Goede24fe86a2015-03-29 12:50:46 +020038#include <linux/phy/phy-sun4i-usb.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010039#include <linux/platform_device.h>
Hans de Goede8665c182015-06-13 14:37:51 +020040#include <linux/power_supply.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010041#include <linux/regulator/consumer.h>
42#include <linux/reset.h>
Hans de Goeded2332302015-06-13 14:37:45 +020043#include <linux/workqueue.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010044
45#define REG_ISCR 0x00
Hans de Goedefc1f45e2015-06-13 14:37:49 +020046#define REG_PHYCTL_A10 0x04
Hans de Goedeba4bdc92014-03-01 18:09:26 +010047#define REG_PHYBIST 0x08
48#define REG_PHYTUNE 0x0c
Hans de Goedefc1f45e2015-06-13 14:37:49 +020049#define REG_PHYCTL_A33 0x10
Hans de Goedeba4bdc92014-03-01 18:09:26 +010050
51#define PHYCTL_DATA BIT(7)
52
53#define SUNXI_AHB_ICHR8_EN BIT(10)
54#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
55#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
56#define SUNXI_ULPI_BYPASS_EN BIT(0)
57
Hans de Goeded2332302015-06-13 14:37:45 +020058/* ISCR, Interface Status and Control bits */
59#define ISCR_ID_PULLUP_EN (1 << 17)
60#define ISCR_DPDM_PULLUP_EN (1 << 16)
61/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
62#define ISCR_FORCE_ID_MASK (3 << 14)
63#define ISCR_FORCE_ID_LOW (2 << 14)
64#define ISCR_FORCE_ID_HIGH (3 << 14)
65#define ISCR_FORCE_VBUS_MASK (3 << 12)
66#define ISCR_FORCE_VBUS_LOW (2 << 12)
67#define ISCR_FORCE_VBUS_HIGH (3 << 12)
68
Hans de Goedeba4bdc92014-03-01 18:09:26 +010069/* Common Control Bits for Both PHYs */
70#define PHY_PLL_BW 0x03
71#define PHY_RES45_CAL_EN 0x0c
72
73/* Private Control Bits for Each PHY */
74#define PHY_TX_AMPLITUDE_TUNE 0x20
75#define PHY_TX_SLEWRATE_TUNE 0x22
76#define PHY_VBUSVALID_TH_SEL 0x25
77#define PHY_PULLUP_RES_SEL 0x27
78#define PHY_OTG_FUNC_EN 0x28
79#define PHY_VBUS_DET_EN 0x29
80#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020081#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010082
83#define MAX_PHYS 3
84
Hans de Goeded2332302015-06-13 14:37:45 +020085/*
86 * Note do not raise the debounce time, we must report Vusb high within 100ms
87 * otherwise we get Vbus errors
88 */
89#define DEBOUNCE_TIME msecs_to_jiffies(50)
90#define POLL_TIME msecs_to_jiffies(250)
91
Hans de Goede68dbc2c2015-12-11 16:32:17 +010092enum sun4i_usb_phy_type {
93 sun4i_a10_phy,
94 sun8i_a33_phy,
95};
96
97struct sun4i_usb_phy_cfg {
98 int num_phys;
99 enum sun4i_usb_phy_type type;
100 u32 disc_thresh;
101 u8 phyctl_offset;
102 bool dedicated_clocks;
103};
104
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100105struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100106 void __iomem *base;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100107 const struct sun4i_usb_phy_cfg *cfg;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100108 struct mutex mutex;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100109 struct sun4i_usb_phy {
110 struct phy *phy;
111 void __iomem *pmu;
112 struct regulator *vbus;
113 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200114 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +0200115 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100116 int index;
117 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200118 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200119 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200120 bool phy0_init;
121 bool phy0_poll;
122 struct gpio_desc *id_det_gpio;
123 struct gpio_desc *vbus_det_gpio;
Hans de Goede8665c182015-06-13 14:37:51 +0200124 struct power_supply *vbus_power_supply;
125 struct notifier_block vbus_power_nb;
126 bool vbus_power_nb_registered;
Hans de Goeded2332302015-06-13 14:37:45 +0200127 int id_det_irq;
128 int vbus_det_irq;
129 int id_det;
130 int vbus_det;
131 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100132};
133
134#define to_sun4i_usb_phy_data(phy) \
135 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
136
Hans de Goeded2332302015-06-13 14:37:45 +0200137static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
138{
139 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
140 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
141 u32 iscr;
142
143 iscr = readl(data->base + REG_ISCR);
144 iscr &= ~clr;
145 iscr |= set;
146 writel(iscr, data->base + REG_ISCR);
147}
148
149static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
150{
151 if (val)
152 val = ISCR_FORCE_ID_HIGH;
153 else
154 val = ISCR_FORCE_ID_LOW;
155
156 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
157}
158
159static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
160{
161 if (val)
162 val = ISCR_FORCE_VBUS_HIGH;
163 else
164 val = ISCR_FORCE_VBUS_LOW;
165
166 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
167}
168
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100169static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
170 int len)
171{
172 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
173 u32 temp, usbc_bit = BIT(phy->index * 2);
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100174 void *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100175 int i;
176
177 mutex_lock(&phy_data->mutex);
178
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100179 if (phy_data->cfg->type == sun8i_a33_phy) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200180 /* A33 needs us to set phyctl to 0 explicitly */
181 writel(0, phyctl);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200182 }
183
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100184 for (i = 0; i < len; i++) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200185 temp = readl(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100186
187 /* clear the address portion */
188 temp &= ~(0xff << 8);
189
190 /* set the address */
191 temp |= ((addr + i) << 8);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200192 writel(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100193
194 /* set the data bit and clear usbc bit*/
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200195 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100196 if (data & 0x1)
197 temp |= PHYCTL_DATA;
198 else
199 temp &= ~PHYCTL_DATA;
200 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200201 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100202
203 /* pulse usbc_bit */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200204 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100205 temp |= usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200206 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100207
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200208 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100209 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200210 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100211
212 data >>= 1;
213 }
214 mutex_unlock(&phy_data->mutex);
215}
216
217static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
218{
219 u32 bits, reg_value;
220
221 if (!phy->pmu)
222 return;
223
224 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
225 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
226
227 reg_value = readl(phy->pmu);
228
229 if (enable)
230 reg_value |= bits;
231 else
232 reg_value &= ~bits;
233
234 writel(reg_value, phy->pmu);
235}
236
237static int sun4i_usb_phy_init(struct phy *_phy)
238{
239 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
240 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
241 int ret;
242
Maxime Ripardeadd4312014-05-13 17:44:18 +0200243 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100244 if (ret)
245 return ret;
246
247 ret = reset_control_deassert(phy->reset);
248 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200249 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100250 return ret;
251 }
252
Roman Byshko6827a46f2014-11-10 19:55:06 +0100253 /* Enable USB 45 Ohm resistor calibration */
254 if (phy->index == 0)
255 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
256
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100257 /* Adjust PHY's magnitude and rate */
258 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
259
260 /* Disconnect threshold adjustment */
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100261 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
262 data->cfg->disc_thresh, 2);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100263
264 sun4i_usb_phy_passby(phy, 1);
265
Hans de Goeded2332302015-06-13 14:37:45 +0200266 if (phy->index == 0) {
267 data->phy0_init = true;
268
269 /* Enable pull-ups */
270 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
271 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
272
273 if (data->id_det_gpio) {
274 /* OTG mode, force ISCR and cable state updates */
275 data->id_det = -1;
276 data->vbus_det = -1;
277 queue_delayed_work(system_wq, &data->detect, 0);
278 } else {
279 /* Host only mode */
280 sun4i_usb_phy0_set_id_detect(_phy, 0);
281 sun4i_usb_phy0_set_vbus_detect(_phy, 1);
282 }
283 }
284
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100285 return 0;
286}
287
288static int sun4i_usb_phy_exit(struct phy *_phy)
289{
290 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200291 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
292
293 if (phy->index == 0) {
294 /* Disable pull-ups */
295 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
296 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
297 data->phy0_init = false;
298 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100299
300 sun4i_usb_phy_passby(phy, 0);
301 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200302 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100303
304 return 0;
305}
306
Hans de Goede3d772c42015-07-31 10:01:41 +0200307static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
308{
309 if (data->vbus_det_gpio)
310 return gpiod_get_value_cansleep(data->vbus_det_gpio);
311
312 if (data->vbus_power_supply) {
313 union power_supply_propval val;
314 int r;
315
316 r = power_supply_get_property(data->vbus_power_supply,
317 POWER_SUPPLY_PROP_PRESENT, &val);
318 if (r == 0)
319 return val.intval;
320 }
321
322 /* Fallback: report vbus as high */
323 return 1;
324}
325
326static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
327{
328 return data->vbus_det_gpio || data->vbus_power_supply;
329}
330
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100331static int sun4i_usb_phy_power_on(struct phy *_phy)
332{
333 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200334 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
335 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100336
Hans de Goeded2332302015-06-13 14:37:45 +0200337 if (!phy->vbus || phy->regulator_on)
338 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100339
Hans de Goeded2332302015-06-13 14:37:45 +0200340 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
Hans de Goede80835262015-07-31 10:01:42 +0200341 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
342 data->vbus_det)
Hans de Goeded2332302015-06-13 14:37:45 +0200343 return 0;
344
345 ret = regulator_enable(phy->vbus);
346 if (ret)
347 return ret;
348
349 phy->regulator_on = true;
350
351 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200352 if (phy->index == 0 && data->vbus_det_gpio && data->phy0_poll)
Hans de Goeded2332302015-06-13 14:37:45 +0200353 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
354
355 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100356}
357
358static int sun4i_usb_phy_power_off(struct phy *_phy)
359{
360 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200361 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100362
Hans de Goeded2332302015-06-13 14:37:45 +0200363 if (!phy->vbus || !phy->regulator_on)
364 return 0;
365
366 regulator_disable(phy->vbus);
367 phy->regulator_on = false;
368
369 /*
370 * phy0 vbus typically slowly discharges, sometimes this causes the
371 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
372 */
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200373 if (phy->index == 0 && data->vbus_det_gpio && !data->phy0_poll)
Hans de Goeded2332302015-06-13 14:37:45 +0200374 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100375
376 return 0;
377}
378
Hans de Goede24fe86a2015-03-29 12:50:46 +0200379void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
380{
381 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
382
383 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
384}
Hans de Goede7167bf82015-07-31 10:01:40 +0200385EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
Hans de Goede24fe86a2015-03-29 12:50:46 +0200386
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800387static const struct phy_ops sun4i_usb_phy_ops = {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100388 .init = sun4i_usb_phy_init,
389 .exit = sun4i_usb_phy_exit,
390 .power_on = sun4i_usb_phy_power_on,
391 .power_off = sun4i_usb_phy_power_off,
392 .owner = THIS_MODULE,
393};
394
Hans de Goeded2332302015-06-13 14:37:45 +0200395static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
396{
397 struct sun4i_usb_phy_data *data =
398 container_of(work, struct sun4i_usb_phy_data, detect.work);
399 struct phy *phy0 = data->phys[0].phy;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200400 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
Hans de Goeded2332302015-06-13 14:37:45 +0200401
402 id_det = gpiod_get_value_cansleep(data->id_det_gpio);
Hans de Goede8665c182015-06-13 14:37:51 +0200403 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
Hans de Goeded2332302015-06-13 14:37:45 +0200404
405 mutex_lock(&phy0->mutex);
406
407 if (!data->phy0_init) {
408 mutex_unlock(&phy0->mutex);
409 return;
410 }
411
412 if (id_det != data->id_det) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200413 /*
414 * When a host cable (id == 0) gets plugged in on systems
415 * without vbus detection report vbus low for long enough for
416 * the musb-ip to end the current device session.
417 */
Hans de Goede8665c182015-06-13 14:37:51 +0200418 if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200419 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
420 msleep(200);
421 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
422 }
Hans de Goeded2332302015-06-13 14:37:45 +0200423 sun4i_usb_phy0_set_id_detect(phy0, id_det);
424 data->id_det = id_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200425 id_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200426 }
427
428 if (vbus_det != data->vbus_det) {
429 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
430 data->vbus_det = vbus_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200431 vbus_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200432 }
433
434 mutex_unlock(&phy0->mutex);
435
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200436 if (id_notify) {
Hans de Goede1a52abe2015-06-13 14:37:46 +0200437 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
438 !id_det);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200439 /*
440 * When a host cable gets unplugged (id == 1) on systems
441 * without vbus detection report vbus low for long enough to
442 * the musb-ip to end the current host session.
443 */
Hans de Goede8665c182015-06-13 14:37:51 +0200444 if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200445 mutex_lock(&phy0->mutex);
446 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
447 msleep(1000);
448 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
449 mutex_unlock(&phy0->mutex);
450 }
451 }
Hans de Goede1a52abe2015-06-13 14:37:46 +0200452
453 if (vbus_notify)
454 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
455
Hans de Goeded2332302015-06-13 14:37:45 +0200456 if (data->phy0_poll)
457 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
458}
459
460static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
461{
462 struct sun4i_usb_phy_data *data = dev_id;
463
464 /* vbus or id changed, let the pins settle and then scan them */
465 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
466
467 return IRQ_HANDLED;
468}
469
Hans de Goede8665c182015-06-13 14:37:51 +0200470static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
471 unsigned long val, void *v)
472{
473 struct sun4i_usb_phy_data *data =
474 container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
475 struct power_supply *psy = v;
476
477 /* Properties on the vbus_power_supply changed, scan vbus_det */
478 if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
479 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
480
481 return NOTIFY_OK;
482}
483
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100484static struct phy *sun4i_usb_phy_xlate(struct device *dev,
485 struct of_phandle_args *args)
486{
487 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
488
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100489 if (args->args[0] >= data->cfg->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100490 return ERR_PTR(-ENODEV);
491
492 return data->phys[args->args[0]].phy;
493}
494
Hans de Goeded2332302015-06-13 14:37:45 +0200495static int sun4i_usb_phy_remove(struct platform_device *pdev)
496{
497 struct device *dev = &pdev->dev;
498 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
499
Hans de Goede8665c182015-06-13 14:37:51 +0200500 if (data->vbus_power_nb_registered)
501 power_supply_unreg_notifier(&data->vbus_power_nb);
Hans de Goeded2332302015-06-13 14:37:45 +0200502 if (data->id_det_irq >= 0)
503 devm_free_irq(dev, data->id_det_irq, data);
504 if (data->vbus_det_irq >= 0)
505 devm_free_irq(dev, data->vbus_det_irq, data);
506
507 cancel_delayed_work_sync(&data->detect);
508
509 return 0;
510}
511
Hans de Goede1a52abe2015-06-13 14:37:46 +0200512static const unsigned int sun4i_usb_phy0_cable[] = {
513 EXTCON_USB,
514 EXTCON_USB_HOST,
515 EXTCON_NONE,
516};
517
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100518static int sun4i_usb_phy_probe(struct platform_device *pdev)
519{
520 struct sun4i_usb_phy_data *data;
521 struct device *dev = &pdev->dev;
522 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100523 struct phy_provider *phy_provider;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100524 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200525 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100526
527 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
528 if (!data)
529 return -ENOMEM;
530
531 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200532 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
533 dev_set_drvdata(dev, data);
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100534 data->cfg = of_device_get_match_data(dev);
535 if (!data->cfg)
536 return -EINVAL;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200537
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100538 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
539 data->base = devm_ioremap_resource(dev, res);
540 if (IS_ERR(data->base))
541 return PTR_ERR(data->base);
542
Axel Linb2dfc342015-09-25 08:47:29 +0800543 data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
544 GPIOD_IN);
545 if (IS_ERR(data->id_det_gpio))
546 return PTR_ERR(data->id_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200547
Axel Linb2dfc342015-09-25 08:47:29 +0800548 data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
549 GPIOD_IN);
550 if (IS_ERR(data->vbus_det_gpio))
551 return PTR_ERR(data->vbus_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200552
Hans de Goede8665c182015-06-13 14:37:51 +0200553 if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
554 data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
555 "usb0_vbus_power-supply");
556 if (IS_ERR(data->vbus_power_supply))
557 return PTR_ERR(data->vbus_power_supply);
558
559 if (!data->vbus_power_supply)
560 return -EPROBE_DEFER;
561 }
562
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200563 /* vbus_det without id_det makes no sense, and is not supported */
Hans de Goede8665c182015-06-13 14:37:51 +0200564 if (sun4i_usb_phy0_have_vbus_det(data) && !data->id_det_gpio) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200565 dev_err(dev, "usb0_id_det missing or invalid\n");
Hans de Goeded2332302015-06-13 14:37:45 +0200566 return -ENODEV;
567 }
568
Hans de Goede1a52abe2015-06-13 14:37:46 +0200569 if (data->id_det_gpio) {
570 data->extcon = devm_extcon_dev_allocate(dev,
571 sun4i_usb_phy0_cable);
572 if (IS_ERR(data->extcon))
573 return PTR_ERR(data->extcon);
574
575 ret = devm_extcon_dev_register(dev, data->extcon);
576 if (ret) {
577 dev_err(dev, "failed to register extcon: %d\n", ret);
578 return ret;
579 }
580 }
581
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100582 for (i = 0; i < data->cfg->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200583 struct sun4i_usb_phy *phy = data->phys + i;
584 char name[16];
585
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100586 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200587 phy->vbus = devm_regulator_get_optional(dev, name);
588 if (IS_ERR(phy->vbus)) {
589 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100590 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200591 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100592 }
593
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100594 if (data->cfg->dedicated_clocks)
Maxime Ripardeadd4312014-05-13 17:44:18 +0200595 snprintf(name, sizeof(name), "usb%d_phy", i);
596 else
597 strlcpy(name, "usb_phy", sizeof(name));
598
599 phy->clk = devm_clk_get(dev, name);
600 if (IS_ERR(phy->clk)) {
601 dev_err(dev, "failed to get clock %s\n", name);
602 return PTR_ERR(phy->clk);
603 }
604
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100605 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200606 phy->reset = devm_reset_control_get(dev, name);
607 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100608 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200609 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100610 }
611
612 if (i) { /* No pmu for usbc0 */
613 snprintf(name, sizeof(name), "pmu%d", i);
614 res = platform_get_resource_byname(pdev,
615 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200616 phy->pmu = devm_ioremap_resource(dev, res);
617 if (IS_ERR(phy->pmu))
618 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100619 }
620
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200621 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200622 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100623 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200624 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100625 }
626
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200627 phy->index = i;
628 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100629 }
630
Hans de Goeded2332302015-06-13 14:37:45 +0200631 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
632 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200633 if ((data->id_det_gpio && data->id_det_irq < 0) ||
634 (data->vbus_det_gpio && data->vbus_det_irq < 0))
Hans de Goeded2332302015-06-13 14:37:45 +0200635 data->phy0_poll = true;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100636
Hans de Goeded2332302015-06-13 14:37:45 +0200637 if (data->id_det_irq >= 0) {
638 ret = devm_request_irq(dev, data->id_det_irq,
639 sun4i_usb_phy0_id_vbus_det_irq,
640 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
641 "usb0-id-det", data);
642 if (ret) {
643 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
644 return ret;
645 }
646 }
647
648 if (data->vbus_det_irq >= 0) {
649 ret = devm_request_irq(dev, data->vbus_det_irq,
650 sun4i_usb_phy0_id_vbus_det_irq,
651 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
652 "usb0-vbus-det", data);
653 if (ret) {
654 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
655 data->vbus_det_irq = -1;
656 sun4i_usb_phy_remove(pdev); /* Stop detect work */
657 return ret;
658 }
659 }
660
Hans de Goede8665c182015-06-13 14:37:51 +0200661 if (data->vbus_power_supply) {
662 data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
663 data->vbus_power_nb.priority = 0;
664 ret = power_supply_reg_notifier(&data->vbus_power_nb);
665 if (ret) {
666 sun4i_usb_phy_remove(pdev); /* Stop detect work */
667 return ret;
668 }
669 data->vbus_power_nb_registered = true;
670 }
671
Hans de Goeded2332302015-06-13 14:37:45 +0200672 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
673 if (IS_ERR(phy_provider)) {
674 sun4i_usb_phy_remove(pdev); /* Stop detect work */
675 return PTR_ERR(phy_provider);
676 }
677
678 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100679}
680
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100681static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
682 .num_phys = 3,
683 .type = sun4i_a10_phy,
684 .disc_thresh = 3,
685 .phyctl_offset = REG_PHYCTL_A10,
686 .dedicated_clocks = false,
687};
688
689static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
690 .num_phys = 2,
691 .type = sun4i_a10_phy,
692 .disc_thresh = 2,
693 .phyctl_offset = REG_PHYCTL_A10,
694 .dedicated_clocks = false,
695};
696
697static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
698 .num_phys = 3,
699 .type = sun4i_a10_phy,
700 .disc_thresh = 3,
701 .phyctl_offset = REG_PHYCTL_A10,
702 .dedicated_clocks = true,
703};
704
705static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
706 .num_phys = 3,
707 .type = sun4i_a10_phy,
708 .disc_thresh = 2,
709 .phyctl_offset = REG_PHYCTL_A10,
710 .dedicated_clocks = false,
711};
712
713static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
714 .num_phys = 2,
715 .type = sun4i_a10_phy,
716 .disc_thresh = 3,
717 .phyctl_offset = REG_PHYCTL_A10,
718 .dedicated_clocks = true,
719};
720
721static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
722 .num_phys = 2,
723 .type = sun8i_a33_phy,
724 .disc_thresh = 3,
725 .phyctl_offset = REG_PHYCTL_A33,
726 .dedicated_clocks = true,
727};
728
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100729static const struct of_device_id sun4i_usb_phy_of_match[] = {
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100730 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
731 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
732 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
733 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
734 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
735 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100736 { },
737};
738MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
739
740static struct platform_driver sun4i_usb_phy_driver = {
741 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200742 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100743 .driver = {
744 .of_match_table = sun4i_usb_phy_of_match,
745 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100746 }
747};
748module_platform_driver(sun4i_usb_phy_driver);
749
750MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
751MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
752MODULE_LICENSE("GPL v2");