blob: bae54f7a1f4877b88297858783b0b864783ec1fe [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
Reinder de Haan626a6302015-12-11 16:32:18 +010050#define REG_PHY_UNK_H3 0x20
51
52#define REG_PMU_UNK_H3 0x10
Hans de Goedeba4bdc92014-03-01 18:09:26 +010053
54#define PHYCTL_DATA BIT(7)
55
56#define SUNXI_AHB_ICHR8_EN BIT(10)
57#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
58#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
59#define SUNXI_ULPI_BYPASS_EN BIT(0)
60
Hans de Goeded2332302015-06-13 14:37:45 +020061/* ISCR, Interface Status and Control bits */
62#define ISCR_ID_PULLUP_EN (1 << 17)
63#define ISCR_DPDM_PULLUP_EN (1 << 16)
64/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
65#define ISCR_FORCE_ID_MASK (3 << 14)
66#define ISCR_FORCE_ID_LOW (2 << 14)
67#define ISCR_FORCE_ID_HIGH (3 << 14)
68#define ISCR_FORCE_VBUS_MASK (3 << 12)
69#define ISCR_FORCE_VBUS_LOW (2 << 12)
70#define ISCR_FORCE_VBUS_HIGH (3 << 12)
71
Hans de Goedeba4bdc92014-03-01 18:09:26 +010072/* Common Control Bits for Both PHYs */
73#define PHY_PLL_BW 0x03
74#define PHY_RES45_CAL_EN 0x0c
75
76/* Private Control Bits for Each PHY */
77#define PHY_TX_AMPLITUDE_TUNE 0x20
78#define PHY_TX_SLEWRATE_TUNE 0x22
79#define PHY_VBUSVALID_TH_SEL 0x25
80#define PHY_PULLUP_RES_SEL 0x27
81#define PHY_OTG_FUNC_EN 0x28
82#define PHY_VBUS_DET_EN 0x29
83#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020084#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010085
Reinder de Haan626a6302015-12-11 16:32:18 +010086#define MAX_PHYS 4
Hans de Goedeba4bdc92014-03-01 18:09:26 +010087
Hans de Goeded2332302015-06-13 14:37:45 +020088/*
89 * Note do not raise the debounce time, we must report Vusb high within 100ms
90 * otherwise we get Vbus errors
91 */
92#define DEBOUNCE_TIME msecs_to_jiffies(50)
93#define POLL_TIME msecs_to_jiffies(250)
94
Hans de Goede68dbc2c2015-12-11 16:32:17 +010095enum sun4i_usb_phy_type {
96 sun4i_a10_phy,
97 sun8i_a33_phy,
Reinder de Haan626a6302015-12-11 16:32:18 +010098 sun8i_h3_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +010099};
100
101struct sun4i_usb_phy_cfg {
102 int num_phys;
103 enum sun4i_usb_phy_type type;
104 u32 disc_thresh;
105 u8 phyctl_offset;
106 bool dedicated_clocks;
107};
108
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100109struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100110 void __iomem *base;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100111 const struct sun4i_usb_phy_cfg *cfg;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100112 struct mutex mutex;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100113 struct sun4i_usb_phy {
114 struct phy *phy;
115 void __iomem *pmu;
116 struct regulator *vbus;
117 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200118 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +0200119 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100120 int index;
121 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200122 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200123 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200124 bool phy0_init;
125 bool phy0_poll;
126 struct gpio_desc *id_det_gpio;
127 struct gpio_desc *vbus_det_gpio;
Hans de Goede8665c182015-06-13 14:37:51 +0200128 struct power_supply *vbus_power_supply;
129 struct notifier_block vbus_power_nb;
130 bool vbus_power_nb_registered;
Hans de Goeded2332302015-06-13 14:37:45 +0200131 int id_det_irq;
132 int vbus_det_irq;
133 int id_det;
134 int vbus_det;
135 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100136};
137
138#define to_sun4i_usb_phy_data(phy) \
139 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
140
Hans de Goeded2332302015-06-13 14:37:45 +0200141static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
142{
143 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
144 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
145 u32 iscr;
146
147 iscr = readl(data->base + REG_ISCR);
148 iscr &= ~clr;
149 iscr |= set;
150 writel(iscr, data->base + REG_ISCR);
151}
152
153static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
154{
155 if (val)
156 val = ISCR_FORCE_ID_HIGH;
157 else
158 val = ISCR_FORCE_ID_LOW;
159
160 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
161}
162
163static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
164{
165 if (val)
166 val = ISCR_FORCE_VBUS_HIGH;
167 else
168 val = ISCR_FORCE_VBUS_LOW;
169
170 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
171}
172
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100173static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
174 int len)
175{
176 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
177 u32 temp, usbc_bit = BIT(phy->index * 2);
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100178 void *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100179 int i;
180
181 mutex_lock(&phy_data->mutex);
182
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100183 if (phy_data->cfg->type == sun8i_a33_phy) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200184 /* A33 needs us to set phyctl to 0 explicitly */
185 writel(0, phyctl);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200186 }
187
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100188 for (i = 0; i < len; i++) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200189 temp = readl(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100190
191 /* clear the address portion */
192 temp &= ~(0xff << 8);
193
194 /* set the address */
195 temp |= ((addr + i) << 8);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200196 writel(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100197
198 /* set the data bit and clear usbc bit*/
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200199 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100200 if (data & 0x1)
201 temp |= PHYCTL_DATA;
202 else
203 temp &= ~PHYCTL_DATA;
204 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200205 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100206
207 /* pulse usbc_bit */
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
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200212 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100213 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200214 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100215
216 data >>= 1;
217 }
218 mutex_unlock(&phy_data->mutex);
219}
220
221static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
222{
223 u32 bits, reg_value;
224
225 if (!phy->pmu)
226 return;
227
228 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
229 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
230
231 reg_value = readl(phy->pmu);
232
233 if (enable)
234 reg_value |= bits;
235 else
236 reg_value &= ~bits;
237
238 writel(reg_value, phy->pmu);
239}
240
241static int sun4i_usb_phy_init(struct phy *_phy)
242{
243 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
244 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
245 int ret;
Reinder de Haan626a6302015-12-11 16:32:18 +0100246 u32 val;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100247
Maxime Ripardeadd4312014-05-13 17:44:18 +0200248 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100249 if (ret)
250 return ret;
251
252 ret = reset_control_deassert(phy->reset);
253 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200254 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100255 return ret;
256 }
257
Reinder de Haan626a6302015-12-11 16:32:18 +0100258 if (data->cfg->type == sun8i_h3_phy) {
259 if (phy->index == 0) {
260 val = readl(data->base + REG_PHY_UNK_H3);
261 writel(val & ~1, data->base + REG_PHY_UNK_H3);
262 }
Roman Byshko6827a46f2014-11-10 19:55:06 +0100263
Reinder de Haan626a6302015-12-11 16:32:18 +0100264 val = readl(phy->pmu + REG_PMU_UNK_H3);
265 writel(val & ~2, phy->pmu + REG_PMU_UNK_H3);
266 } else {
267 /* Enable USB 45 Ohm resistor calibration */
268 if (phy->index == 0)
269 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100270
Reinder de Haan626a6302015-12-11 16:32:18 +0100271 /* Adjust PHY's magnitude and rate */
272 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
273
274 /* Disconnect threshold adjustment */
275 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
276 data->cfg->disc_thresh, 2);
277 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100278
279 sun4i_usb_phy_passby(phy, 1);
280
Hans de Goeded2332302015-06-13 14:37:45 +0200281 if (phy->index == 0) {
282 data->phy0_init = true;
283
284 /* Enable pull-ups */
285 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
286 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
287
288 if (data->id_det_gpio) {
289 /* OTG mode, force ISCR and cable state updates */
290 data->id_det = -1;
291 data->vbus_det = -1;
292 queue_delayed_work(system_wq, &data->detect, 0);
293 } else {
294 /* Host only mode */
295 sun4i_usb_phy0_set_id_detect(_phy, 0);
296 sun4i_usb_phy0_set_vbus_detect(_phy, 1);
297 }
298 }
299
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100300 return 0;
301}
302
303static int sun4i_usb_phy_exit(struct phy *_phy)
304{
305 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200306 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
307
308 if (phy->index == 0) {
309 /* Disable pull-ups */
310 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
311 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
312 data->phy0_init = false;
313 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100314
315 sun4i_usb_phy_passby(phy, 0);
316 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200317 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100318
319 return 0;
320}
321
Hans de Goede3d772c42015-07-31 10:01:41 +0200322static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
323{
324 if (data->vbus_det_gpio)
325 return gpiod_get_value_cansleep(data->vbus_det_gpio);
326
327 if (data->vbus_power_supply) {
328 union power_supply_propval val;
329 int r;
330
331 r = power_supply_get_property(data->vbus_power_supply,
332 POWER_SUPPLY_PROP_PRESENT, &val);
333 if (r == 0)
334 return val.intval;
335 }
336
337 /* Fallback: report vbus as high */
338 return 1;
339}
340
341static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
342{
343 return data->vbus_det_gpio || data->vbus_power_supply;
344}
345
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100346static int sun4i_usb_phy_power_on(struct phy *_phy)
347{
348 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200349 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
350 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100351
Hans de Goeded2332302015-06-13 14:37:45 +0200352 if (!phy->vbus || phy->regulator_on)
353 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100354
Hans de Goeded2332302015-06-13 14:37:45 +0200355 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
Hans de Goede80835262015-07-31 10:01:42 +0200356 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
357 data->vbus_det)
Hans de Goeded2332302015-06-13 14:37:45 +0200358 return 0;
359
360 ret = regulator_enable(phy->vbus);
361 if (ret)
362 return ret;
363
364 phy->regulator_on = true;
365
366 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200367 if (phy->index == 0 && data->vbus_det_gpio && data->phy0_poll)
Hans de Goeded2332302015-06-13 14:37:45 +0200368 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
369
370 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100371}
372
373static int sun4i_usb_phy_power_off(struct phy *_phy)
374{
375 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200376 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100377
Hans de Goeded2332302015-06-13 14:37:45 +0200378 if (!phy->vbus || !phy->regulator_on)
379 return 0;
380
381 regulator_disable(phy->vbus);
382 phy->regulator_on = false;
383
384 /*
385 * phy0 vbus typically slowly discharges, sometimes this causes the
386 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
387 */
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200388 if (phy->index == 0 && data->vbus_det_gpio && !data->phy0_poll)
Hans de Goeded2332302015-06-13 14:37:45 +0200389 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100390
391 return 0;
392}
393
Hans de Goede24fe86a2015-03-29 12:50:46 +0200394void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
395{
396 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
397
398 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
399}
Hans de Goede7167bf82015-07-31 10:01:40 +0200400EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
Hans de Goede24fe86a2015-03-29 12:50:46 +0200401
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800402static const struct phy_ops sun4i_usb_phy_ops = {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100403 .init = sun4i_usb_phy_init,
404 .exit = sun4i_usb_phy_exit,
405 .power_on = sun4i_usb_phy_power_on,
406 .power_off = sun4i_usb_phy_power_off,
407 .owner = THIS_MODULE,
408};
409
Hans de Goeded2332302015-06-13 14:37:45 +0200410static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
411{
412 struct sun4i_usb_phy_data *data =
413 container_of(work, struct sun4i_usb_phy_data, detect.work);
414 struct phy *phy0 = data->phys[0].phy;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200415 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
Hans de Goeded2332302015-06-13 14:37:45 +0200416
417 id_det = gpiod_get_value_cansleep(data->id_det_gpio);
Hans de Goede8665c182015-06-13 14:37:51 +0200418 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
Hans de Goeded2332302015-06-13 14:37:45 +0200419
420 mutex_lock(&phy0->mutex);
421
422 if (!data->phy0_init) {
423 mutex_unlock(&phy0->mutex);
424 return;
425 }
426
427 if (id_det != data->id_det) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200428 /*
429 * When a host cable (id == 0) gets plugged in on systems
430 * without vbus detection report vbus low for long enough for
431 * the musb-ip to end the current device session.
432 */
Hans de Goede8665c182015-06-13 14:37:51 +0200433 if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200434 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
435 msleep(200);
436 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
437 }
Hans de Goeded2332302015-06-13 14:37:45 +0200438 sun4i_usb_phy0_set_id_detect(phy0, id_det);
439 data->id_det = id_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200440 id_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200441 }
442
443 if (vbus_det != data->vbus_det) {
444 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
445 data->vbus_det = vbus_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200446 vbus_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200447 }
448
449 mutex_unlock(&phy0->mutex);
450
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200451 if (id_notify) {
Hans de Goede1a52abe2015-06-13 14:37:46 +0200452 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
453 !id_det);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200454 /*
455 * When a host cable gets unplugged (id == 1) on systems
456 * without vbus detection report vbus low for long enough to
457 * the musb-ip to end the current host session.
458 */
Hans de Goede8665c182015-06-13 14:37:51 +0200459 if (!sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200460 mutex_lock(&phy0->mutex);
461 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
462 msleep(1000);
463 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
464 mutex_unlock(&phy0->mutex);
465 }
466 }
Hans de Goede1a52abe2015-06-13 14:37:46 +0200467
468 if (vbus_notify)
469 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
470
Hans de Goeded2332302015-06-13 14:37:45 +0200471 if (data->phy0_poll)
472 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
473}
474
475static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
476{
477 struct sun4i_usb_phy_data *data = dev_id;
478
479 /* vbus or id changed, let the pins settle and then scan them */
480 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
481
482 return IRQ_HANDLED;
483}
484
Hans de Goede8665c182015-06-13 14:37:51 +0200485static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
486 unsigned long val, void *v)
487{
488 struct sun4i_usb_phy_data *data =
489 container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
490 struct power_supply *psy = v;
491
492 /* Properties on the vbus_power_supply changed, scan vbus_det */
493 if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
494 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
495
496 return NOTIFY_OK;
497}
498
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100499static struct phy *sun4i_usb_phy_xlate(struct device *dev,
500 struct of_phandle_args *args)
501{
502 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
503
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100504 if (args->args[0] >= data->cfg->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100505 return ERR_PTR(-ENODEV);
506
507 return data->phys[args->args[0]].phy;
508}
509
Hans de Goeded2332302015-06-13 14:37:45 +0200510static int sun4i_usb_phy_remove(struct platform_device *pdev)
511{
512 struct device *dev = &pdev->dev;
513 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
514
Hans de Goede8665c182015-06-13 14:37:51 +0200515 if (data->vbus_power_nb_registered)
516 power_supply_unreg_notifier(&data->vbus_power_nb);
Hans de Goeded2332302015-06-13 14:37:45 +0200517 if (data->id_det_irq >= 0)
518 devm_free_irq(dev, data->id_det_irq, data);
519 if (data->vbus_det_irq >= 0)
520 devm_free_irq(dev, data->vbus_det_irq, data);
521
522 cancel_delayed_work_sync(&data->detect);
523
524 return 0;
525}
526
Hans de Goede1a52abe2015-06-13 14:37:46 +0200527static const unsigned int sun4i_usb_phy0_cable[] = {
528 EXTCON_USB,
529 EXTCON_USB_HOST,
530 EXTCON_NONE,
531};
532
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100533static int sun4i_usb_phy_probe(struct platform_device *pdev)
534{
535 struct sun4i_usb_phy_data *data;
536 struct device *dev = &pdev->dev;
537 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100538 struct phy_provider *phy_provider;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100539 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200540 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100541
542 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
543 if (!data)
544 return -ENOMEM;
545
546 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200547 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
548 dev_set_drvdata(dev, data);
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100549 data->cfg = of_device_get_match_data(dev);
550 if (!data->cfg)
551 return -EINVAL;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200552
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100553 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
554 data->base = devm_ioremap_resource(dev, res);
555 if (IS_ERR(data->base))
556 return PTR_ERR(data->base);
557
Axel Linb2dfc342015-09-25 08:47:29 +0800558 data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
559 GPIOD_IN);
560 if (IS_ERR(data->id_det_gpio))
561 return PTR_ERR(data->id_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200562
Axel Linb2dfc342015-09-25 08:47:29 +0800563 data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
564 GPIOD_IN);
565 if (IS_ERR(data->vbus_det_gpio))
566 return PTR_ERR(data->vbus_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200567
Hans de Goede8665c182015-06-13 14:37:51 +0200568 if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
569 data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
570 "usb0_vbus_power-supply");
571 if (IS_ERR(data->vbus_power_supply))
572 return PTR_ERR(data->vbus_power_supply);
573
574 if (!data->vbus_power_supply)
575 return -EPROBE_DEFER;
576 }
577
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200578 /* vbus_det without id_det makes no sense, and is not supported */
Hans de Goede8665c182015-06-13 14:37:51 +0200579 if (sun4i_usb_phy0_have_vbus_det(data) && !data->id_det_gpio) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200580 dev_err(dev, "usb0_id_det missing or invalid\n");
Hans de Goeded2332302015-06-13 14:37:45 +0200581 return -ENODEV;
582 }
583
Hans de Goede1a52abe2015-06-13 14:37:46 +0200584 if (data->id_det_gpio) {
585 data->extcon = devm_extcon_dev_allocate(dev,
586 sun4i_usb_phy0_cable);
587 if (IS_ERR(data->extcon))
588 return PTR_ERR(data->extcon);
589
590 ret = devm_extcon_dev_register(dev, data->extcon);
591 if (ret) {
592 dev_err(dev, "failed to register extcon: %d\n", ret);
593 return ret;
594 }
595 }
596
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100597 for (i = 0; i < data->cfg->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200598 struct sun4i_usb_phy *phy = data->phys + i;
599 char name[16];
600
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100601 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200602 phy->vbus = devm_regulator_get_optional(dev, name);
603 if (IS_ERR(phy->vbus)) {
604 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100605 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200606 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100607 }
608
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100609 if (data->cfg->dedicated_clocks)
Maxime Ripardeadd4312014-05-13 17:44:18 +0200610 snprintf(name, sizeof(name), "usb%d_phy", i);
611 else
612 strlcpy(name, "usb_phy", sizeof(name));
613
614 phy->clk = devm_clk_get(dev, name);
615 if (IS_ERR(phy->clk)) {
616 dev_err(dev, "failed to get clock %s\n", name);
617 return PTR_ERR(phy->clk);
618 }
619
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100620 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200621 phy->reset = devm_reset_control_get(dev, name);
622 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100623 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200624 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100625 }
626
627 if (i) { /* No pmu for usbc0 */
628 snprintf(name, sizeof(name), "pmu%d", i);
629 res = platform_get_resource_byname(pdev,
630 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200631 phy->pmu = devm_ioremap_resource(dev, res);
632 if (IS_ERR(phy->pmu))
633 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100634 }
635
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200636 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200637 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100638 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200639 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100640 }
641
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200642 phy->index = i;
643 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100644 }
645
Hans de Goeded2332302015-06-13 14:37:45 +0200646 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
647 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200648 if ((data->id_det_gpio && data->id_det_irq < 0) ||
649 (data->vbus_det_gpio && data->vbus_det_irq < 0))
Hans de Goeded2332302015-06-13 14:37:45 +0200650 data->phy0_poll = true;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100651
Hans de Goeded2332302015-06-13 14:37:45 +0200652 if (data->id_det_irq >= 0) {
653 ret = devm_request_irq(dev, data->id_det_irq,
654 sun4i_usb_phy0_id_vbus_det_irq,
655 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
656 "usb0-id-det", data);
657 if (ret) {
658 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
659 return ret;
660 }
661 }
662
663 if (data->vbus_det_irq >= 0) {
664 ret = devm_request_irq(dev, data->vbus_det_irq,
665 sun4i_usb_phy0_id_vbus_det_irq,
666 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
667 "usb0-vbus-det", data);
668 if (ret) {
669 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
670 data->vbus_det_irq = -1;
671 sun4i_usb_phy_remove(pdev); /* Stop detect work */
672 return ret;
673 }
674 }
675
Hans de Goede8665c182015-06-13 14:37:51 +0200676 if (data->vbus_power_supply) {
677 data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
678 data->vbus_power_nb.priority = 0;
679 ret = power_supply_reg_notifier(&data->vbus_power_nb);
680 if (ret) {
681 sun4i_usb_phy_remove(pdev); /* Stop detect work */
682 return ret;
683 }
684 data->vbus_power_nb_registered = true;
685 }
686
Hans de Goeded2332302015-06-13 14:37:45 +0200687 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
688 if (IS_ERR(phy_provider)) {
689 sun4i_usb_phy_remove(pdev); /* Stop detect work */
690 return PTR_ERR(phy_provider);
691 }
692
693 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100694}
695
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100696static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
697 .num_phys = 3,
698 .type = sun4i_a10_phy,
699 .disc_thresh = 3,
700 .phyctl_offset = REG_PHYCTL_A10,
701 .dedicated_clocks = false,
702};
703
704static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
705 .num_phys = 2,
706 .type = sun4i_a10_phy,
707 .disc_thresh = 2,
708 .phyctl_offset = REG_PHYCTL_A10,
709 .dedicated_clocks = false,
710};
711
712static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
713 .num_phys = 3,
714 .type = sun4i_a10_phy,
715 .disc_thresh = 3,
716 .phyctl_offset = REG_PHYCTL_A10,
717 .dedicated_clocks = true,
718};
719
720static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
721 .num_phys = 3,
722 .type = sun4i_a10_phy,
723 .disc_thresh = 2,
724 .phyctl_offset = REG_PHYCTL_A10,
725 .dedicated_clocks = false,
726};
727
728static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
729 .num_phys = 2,
730 .type = sun4i_a10_phy,
731 .disc_thresh = 3,
732 .phyctl_offset = REG_PHYCTL_A10,
733 .dedicated_clocks = true,
734};
735
736static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
737 .num_phys = 2,
738 .type = sun8i_a33_phy,
739 .disc_thresh = 3,
740 .phyctl_offset = REG_PHYCTL_A33,
741 .dedicated_clocks = true,
742};
743
Reinder de Haan626a6302015-12-11 16:32:18 +0100744static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
745 .num_phys = 4,
746 .type = sun8i_h3_phy,
747 .disc_thresh = 3,
748 .dedicated_clocks = true,
749};
750
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100751static const struct of_device_id sun4i_usb_phy_of_match[] = {
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100752 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
753 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
754 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
755 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
756 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
757 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
Reinder de Haan626a6302015-12-11 16:32:18 +0100758 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100759 { },
760};
761MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
762
763static struct platform_driver sun4i_usb_phy_driver = {
764 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200765 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100766 .driver = {
767 .of_match_table = sun4i_usb_phy_of_match,
768 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100769 }
770};
771module_platform_driver(sun4i_usb_phy_driver);
772
773MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
774MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
775MODULE_LICENSE("GPL v2");