blob: fb2d4f3666a5c2d8ec801ebf3a1c9d1901e1aa29 [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 Goedeb33ecca2016-08-08 21:55:39 +020043#include <linux/usb/of.h>
Hans de Goeded2332302015-06-13 14:37:45 +020044#include <linux/workqueue.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010045
46#define REG_ISCR 0x00
Hans de Goedefc1f45e2015-06-13 14:37:49 +020047#define REG_PHYCTL_A10 0x04
Hans de Goedeba4bdc92014-03-01 18:09:26 +010048#define REG_PHYBIST 0x08
49#define REG_PHYTUNE 0x0c
Hans de Goedefc1f45e2015-06-13 14:37:49 +020050#define REG_PHYCTL_A33 0x10
Reinder de Haan626a6302015-12-11 16:32:18 +010051#define REG_PHY_UNK_H3 0x20
52
Icenowy Zhengb3e0d142016-08-12 11:06:21 +080053#define REG_PMU_UNK1 0x10
Hans de Goedeba4bdc92014-03-01 18:09:26 +010054
55#define PHYCTL_DATA BIT(7)
56
57#define SUNXI_AHB_ICHR8_EN BIT(10)
58#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
59#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
60#define SUNXI_ULPI_BYPASS_EN BIT(0)
61
Hans de Goeded2332302015-06-13 14:37:45 +020062/* ISCR, Interface Status and Control bits */
63#define ISCR_ID_PULLUP_EN (1 << 17)
64#define ISCR_DPDM_PULLUP_EN (1 << 16)
65/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
66#define ISCR_FORCE_ID_MASK (3 << 14)
67#define ISCR_FORCE_ID_LOW (2 << 14)
68#define ISCR_FORCE_ID_HIGH (3 << 14)
69#define ISCR_FORCE_VBUS_MASK (3 << 12)
70#define ISCR_FORCE_VBUS_LOW (2 << 12)
71#define ISCR_FORCE_VBUS_HIGH (3 << 12)
72
Hans de Goedeba4bdc92014-03-01 18:09:26 +010073/* Common Control Bits for Both PHYs */
74#define PHY_PLL_BW 0x03
75#define PHY_RES45_CAL_EN 0x0c
76
77/* Private Control Bits for Each PHY */
78#define PHY_TX_AMPLITUDE_TUNE 0x20
79#define PHY_TX_SLEWRATE_TUNE 0x22
80#define PHY_VBUSVALID_TH_SEL 0x25
81#define PHY_PULLUP_RES_SEL 0x27
82#define PHY_OTG_FUNC_EN 0x28
83#define PHY_VBUS_DET_EN 0x29
84#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020085#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010086
Reinder de Haan626a6302015-12-11 16:32:18 +010087#define MAX_PHYS 4
Hans de Goedeba4bdc92014-03-01 18:09:26 +010088
Hans de Goeded2332302015-06-13 14:37:45 +020089/*
90 * Note do not raise the debounce time, we must report Vusb high within 100ms
91 * otherwise we get Vbus errors
92 */
93#define DEBOUNCE_TIME msecs_to_jiffies(50)
94#define POLL_TIME msecs_to_jiffies(250)
95
Hans de Goede68dbc2c2015-12-11 16:32:17 +010096enum sun4i_usb_phy_type {
97 sun4i_a10_phy,
Hans de Goede91d96f02016-06-29 20:14:10 +020098 sun6i_a31_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +010099 sun8i_a33_phy,
Reinder de Haan626a6302015-12-11 16:32:18 +0100100 sun8i_h3_phy,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800101 sun50i_a64_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100102};
103
104struct sun4i_usb_phy_cfg {
105 int num_phys;
106 enum sun4i_usb_phy_type type;
107 u32 disc_thresh;
108 u8 phyctl_offset;
109 bool dedicated_clocks;
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800110 bool enable_pmu_unk1;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100111};
112
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100113struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100114 void __iomem *base;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100115 const struct sun4i_usb_phy_cfg *cfg;
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200116 enum usb_dr_mode dr_mode;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100117 struct mutex mutex;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100118 struct sun4i_usb_phy {
119 struct phy *phy;
120 void __iomem *pmu;
121 struct regulator *vbus;
122 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200123 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +0200124 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100125 int index;
126 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200127 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200128 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200129 bool phy0_init;
Hans de Goeded2332302015-06-13 14:37:45 +0200130 struct gpio_desc *id_det_gpio;
131 struct gpio_desc *vbus_det_gpio;
Hans de Goede8665c182015-06-13 14:37:51 +0200132 struct power_supply *vbus_power_supply;
133 struct notifier_block vbus_power_nb;
134 bool vbus_power_nb_registered;
Hans de Goede36f91592016-09-07 22:20:38 +0200135 bool force_session_end;
Hans de Goeded2332302015-06-13 14:37:45 +0200136 int id_det_irq;
137 int vbus_det_irq;
138 int id_det;
139 int vbus_det;
140 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100141};
142
143#define to_sun4i_usb_phy_data(phy) \
144 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
145
Hans de Goeded2332302015-06-13 14:37:45 +0200146static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
147{
148 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
149 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
150 u32 iscr;
151
152 iscr = readl(data->base + REG_ISCR);
153 iscr &= ~clr;
154 iscr |= set;
155 writel(iscr, data->base + REG_ISCR);
156}
157
158static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
159{
160 if (val)
161 val = ISCR_FORCE_ID_HIGH;
162 else
163 val = ISCR_FORCE_ID_LOW;
164
165 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
166}
167
168static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
169{
170 if (val)
171 val = ISCR_FORCE_VBUS_HIGH;
172 else
173 val = ISCR_FORCE_VBUS_LOW;
174
175 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
176}
177
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100178static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
179 int len)
180{
181 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
182 u32 temp, usbc_bit = BIT(phy->index * 2);
Ben Dooksd99cb372016-06-07 18:14:56 +0100183 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100184 int i;
185
186 mutex_lock(&phy_data->mutex);
187
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800188 if (phy_data->cfg->type == sun8i_a33_phy ||
189 phy_data->cfg->type == sun50i_a64_phy) {
190 /* A33 or A64 needs us to set phyctl to 0 explicitly */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200191 writel(0, phyctl);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200192 }
193
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100194 for (i = 0; i < len; i++) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200195 temp = readl(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100196
197 /* clear the address portion */
198 temp &= ~(0xff << 8);
199
200 /* set the address */
201 temp |= ((addr + i) << 8);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200202 writel(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100203
204 /* set the data bit and clear usbc bit*/
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200205 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100206 if (data & 0x1)
207 temp |= PHYCTL_DATA;
208 else
209 temp &= ~PHYCTL_DATA;
210 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200211 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100212
213 /* pulse usbc_bit */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200214 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100215 temp |= usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200216 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100217
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200218 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100219 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200220 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100221
222 data >>= 1;
223 }
224 mutex_unlock(&phy_data->mutex);
225}
226
227static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
228{
229 u32 bits, reg_value;
230
231 if (!phy->pmu)
232 return;
233
234 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
235 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
236
237 reg_value = readl(phy->pmu);
238
239 if (enable)
240 reg_value |= bits;
241 else
242 reg_value &= ~bits;
243
244 writel(reg_value, phy->pmu);
245}
246
247static int sun4i_usb_phy_init(struct phy *_phy)
248{
249 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
250 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
251 int ret;
Reinder de Haan626a6302015-12-11 16:32:18 +0100252 u32 val;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100253
Maxime Ripardeadd4312014-05-13 17:44:18 +0200254 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100255 if (ret)
256 return ret;
257
258 ret = reset_control_deassert(phy->reset);
259 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200260 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100261 return ret;
262 }
263
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800264 if (data->cfg->enable_pmu_unk1) {
265 val = readl(phy->pmu + REG_PMU_UNK1);
266 writel(val & ~2, phy->pmu + REG_PMU_UNK1);
267 }
268
Reinder de Haan626a6302015-12-11 16:32:18 +0100269 if (data->cfg->type == sun8i_h3_phy) {
270 if (phy->index == 0) {
271 val = readl(data->base + REG_PHY_UNK_H3);
272 writel(val & ~1, data->base + REG_PHY_UNK_H3);
273 }
Reinder de Haan626a6302015-12-11 16:32:18 +0100274 } else {
275 /* Enable USB 45 Ohm resistor calibration */
276 if (phy->index == 0)
277 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100278
Reinder de Haan626a6302015-12-11 16:32:18 +0100279 /* Adjust PHY's magnitude and rate */
280 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
281
282 /* Disconnect threshold adjustment */
283 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
284 data->cfg->disc_thresh, 2);
285 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100286
287 sun4i_usb_phy_passby(phy, 1);
288
Hans de Goeded2332302015-06-13 14:37:45 +0200289 if (phy->index == 0) {
290 data->phy0_init = true;
291
292 /* Enable pull-ups */
293 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
294 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
295
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200296 /* Force ISCR and cable state updates */
297 data->id_det = -1;
298 data->vbus_det = -1;
299 queue_delayed_work(system_wq, &data->detect, 0);
Hans de Goeded2332302015-06-13 14:37:45 +0200300 }
301
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100302 return 0;
303}
304
305static int sun4i_usb_phy_exit(struct phy *_phy)
306{
307 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200308 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
309
310 if (phy->index == 0) {
311 /* Disable pull-ups */
312 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
313 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
314 data->phy0_init = false;
315 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100316
317 sun4i_usb_phy_passby(phy, 0);
318 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200319 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100320
321 return 0;
322}
323
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200324static int sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data *data)
325{
326 switch (data->dr_mode) {
327 case USB_DR_MODE_OTG:
Hans de Goede5f90d312016-09-07 22:20:39 +0200328 if (data->id_det_gpio)
329 return gpiod_get_value_cansleep(data->id_det_gpio);
330 else
331 return 1; /* Fallback to peripheral mode */
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200332 case USB_DR_MODE_HOST:
333 return 0;
334 case USB_DR_MODE_PERIPHERAL:
335 default:
336 return 1;
337 }
338}
339
Hans de Goede3d772c42015-07-31 10:01:41 +0200340static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
341{
342 if (data->vbus_det_gpio)
343 return gpiod_get_value_cansleep(data->vbus_det_gpio);
344
345 if (data->vbus_power_supply) {
346 union power_supply_propval val;
347 int r;
348
349 r = power_supply_get_property(data->vbus_power_supply,
350 POWER_SUPPLY_PROP_PRESENT, &val);
351 if (r == 0)
352 return val.intval;
353 }
354
355 /* Fallback: report vbus as high */
356 return 1;
357}
358
359static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
360{
361 return data->vbus_det_gpio || data->vbus_power_supply;
362}
363
Hans de Goede91d96f02016-06-29 20:14:10 +0200364static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
365{
366 if ((data->id_det_gpio && data->id_det_irq <= 0) ||
367 (data->vbus_det_gpio && data->vbus_det_irq <= 0))
368 return true;
369
370 /*
371 * The A31 companion pmic (axp221) does not generate vbus change
372 * interrupts when the board is driving vbus, so we must poll
373 * when using the pmic for vbus-det _and_ we're driving vbus.
374 */
375 if (data->cfg->type == sun6i_a31_phy &&
376 data->vbus_power_supply && data->phys[0].regulator_on)
377 return true;
378
379 return false;
380}
381
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100382static int sun4i_usb_phy_power_on(struct phy *_phy)
383{
384 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200385 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
386 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100387
Hans de Goeded2332302015-06-13 14:37:45 +0200388 if (!phy->vbus || phy->regulator_on)
389 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100390
Hans de Goeded2332302015-06-13 14:37:45 +0200391 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
Hans de Goede80835262015-07-31 10:01:42 +0200392 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
393 data->vbus_det)
Hans de Goeded2332302015-06-13 14:37:45 +0200394 return 0;
395
396 ret = regulator_enable(phy->vbus);
397 if (ret)
398 return ret;
399
400 phy->regulator_on = true;
401
402 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
Hans de Goede91d96f02016-06-29 20:14:10 +0200403 if (phy->index == 0 && sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200404 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
405
406 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100407}
408
409static int sun4i_usb_phy_power_off(struct phy *_phy)
410{
411 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200412 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100413
Hans de Goeded2332302015-06-13 14:37:45 +0200414 if (!phy->vbus || !phy->regulator_on)
415 return 0;
416
417 regulator_disable(phy->vbus);
418 phy->regulator_on = false;
419
420 /*
421 * phy0 vbus typically slowly discharges, sometimes this causes the
422 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
423 */
Hans de Goede91d96f02016-06-29 20:14:10 +0200424 if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200425 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100426
427 return 0;
428}
429
Hans de Goede24fe86a2015-03-29 12:50:46 +0200430void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
431{
432 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
433
434 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
435}
Hans de Goede7167bf82015-07-31 10:01:40 +0200436EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
Hans de Goede24fe86a2015-03-29 12:50:46 +0200437
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800438static const struct phy_ops sun4i_usb_phy_ops = {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100439 .init = sun4i_usb_phy_init,
440 .exit = sun4i_usb_phy_exit,
441 .power_on = sun4i_usb_phy_power_on,
442 .power_off = sun4i_usb_phy_power_off,
443 .owner = THIS_MODULE,
444};
445
Hans de Goeded2332302015-06-13 14:37:45 +0200446static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
447{
448 struct sun4i_usb_phy_data *data =
449 container_of(work, struct sun4i_usb_phy_data, detect.work);
450 struct phy *phy0 = data->phys[0].phy;
Hans de Goede36f91592016-09-07 22:20:38 +0200451 bool force_session_end, id_notify = false, vbus_notify = false;
Hans de Goede9745cee2016-09-07 22:20:37 +0200452 int id_det, vbus_det;
Hans de Goeded2332302015-06-13 14:37:45 +0200453
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200454 if (phy0 == NULL)
455 return;
456
457 id_det = sun4i_usb_phy0_get_id_det(data);
Hans de Goede8665c182015-06-13 14:37:51 +0200458 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
Hans de Goeded2332302015-06-13 14:37:45 +0200459
460 mutex_lock(&phy0->mutex);
461
462 if (!data->phy0_init) {
463 mutex_unlock(&phy0->mutex);
464 return;
465 }
466
Hans de Goede36f91592016-09-07 22:20:38 +0200467 force_session_end = data->force_session_end;
468 data->force_session_end = false;
469
Hans de Goeded2332302015-06-13 14:37:45 +0200470 if (id_det != data->id_det) {
Hans de Goede36f91592016-09-07 22:20:38 +0200471 /* id-change, force session end if we've no vbus detection */
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200472 if (data->dr_mode == USB_DR_MODE_OTG &&
Hans de Goede36f91592016-09-07 22:20:38 +0200473 !sun4i_usb_phy0_have_vbus_det(data))
474 force_session_end = true;
475
476 /* When entering host mode (id = 0) force end the session now */
477 if (force_session_end && id_det == 0) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200478 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
479 msleep(200);
480 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
481 }
Hans de Goeded2332302015-06-13 14:37:45 +0200482 sun4i_usb_phy0_set_id_detect(phy0, id_det);
483 data->id_det = id_det;
Hans de Goede9745cee2016-09-07 22:20:37 +0200484 id_notify = true;
Hans de Goeded2332302015-06-13 14:37:45 +0200485 }
486
487 if (vbus_det != data->vbus_det) {
488 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
489 data->vbus_det = vbus_det;
Hans de Goede9745cee2016-09-07 22:20:37 +0200490 vbus_notify = true;
Hans de Goeded2332302015-06-13 14:37:45 +0200491 }
492
493 mutex_unlock(&phy0->mutex);
494
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200495 if (id_notify) {
Hans de Goede1a52abe2015-06-13 14:37:46 +0200496 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
497 !id_det);
Hans de Goede36f91592016-09-07 22:20:38 +0200498 /* When leaving host mode force end the session here */
499 if (force_session_end && id_det == 1) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200500 mutex_lock(&phy0->mutex);
501 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
502 msleep(1000);
503 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
504 mutex_unlock(&phy0->mutex);
505 }
506 }
Hans de Goede1a52abe2015-06-13 14:37:46 +0200507
508 if (vbus_notify)
509 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
510
Hans de Goede91d96f02016-06-29 20:14:10 +0200511 if (sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200512 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
513}
514
515static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
516{
517 struct sun4i_usb_phy_data *data = dev_id;
518
519 /* vbus or id changed, let the pins settle and then scan them */
520 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
521
522 return IRQ_HANDLED;
523}
524
Hans de Goede8665c182015-06-13 14:37:51 +0200525static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
526 unsigned long val, void *v)
527{
528 struct sun4i_usb_phy_data *data =
529 container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
530 struct power_supply *psy = v;
531
532 /* Properties on the vbus_power_supply changed, scan vbus_det */
533 if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
534 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
535
536 return NOTIFY_OK;
537}
538
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100539static struct phy *sun4i_usb_phy_xlate(struct device *dev,
540 struct of_phandle_args *args)
541{
542 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
543
Hans de Goede5f90d312016-09-07 22:20:39 +0200544 if (args->args[0] >= data->cfg->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100545 return ERR_PTR(-ENODEV);
546
547 return data->phys[args->args[0]].phy;
548}
549
Hans de Goeded2332302015-06-13 14:37:45 +0200550static int sun4i_usb_phy_remove(struct platform_device *pdev)
551{
552 struct device *dev = &pdev->dev;
553 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
554
Hans de Goede8665c182015-06-13 14:37:51 +0200555 if (data->vbus_power_nb_registered)
556 power_supply_unreg_notifier(&data->vbus_power_nb);
Hans de Goede04e59a02016-06-18 11:31:33 +0200557 if (data->id_det_irq > 0)
Hans de Goeded2332302015-06-13 14:37:45 +0200558 devm_free_irq(dev, data->id_det_irq, data);
Hans de Goede04e59a02016-06-18 11:31:33 +0200559 if (data->vbus_det_irq > 0)
Hans de Goeded2332302015-06-13 14:37:45 +0200560 devm_free_irq(dev, data->vbus_det_irq, data);
561
562 cancel_delayed_work_sync(&data->detect);
563
564 return 0;
565}
566
Hans de Goede1a52abe2015-06-13 14:37:46 +0200567static const unsigned int sun4i_usb_phy0_cable[] = {
568 EXTCON_USB,
569 EXTCON_USB_HOST,
570 EXTCON_NONE,
571};
572
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100573static int sun4i_usb_phy_probe(struct platform_device *pdev)
574{
575 struct sun4i_usb_phy_data *data;
576 struct device *dev = &pdev->dev;
577 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100578 struct phy_provider *phy_provider;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100579 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200580 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100581
582 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
583 if (!data)
584 return -ENOMEM;
585
586 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200587 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
588 dev_set_drvdata(dev, data);
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100589 data->cfg = of_device_get_match_data(dev);
590 if (!data->cfg)
591 return -EINVAL;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200592
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100593 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
594 data->base = devm_ioremap_resource(dev, res);
595 if (IS_ERR(data->base))
596 return PTR_ERR(data->base);
597
Axel Linb2dfc342015-09-25 08:47:29 +0800598 data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
599 GPIOD_IN);
600 if (IS_ERR(data->id_det_gpio))
601 return PTR_ERR(data->id_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200602
Axel Linb2dfc342015-09-25 08:47:29 +0800603 data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
604 GPIOD_IN);
605 if (IS_ERR(data->vbus_det_gpio))
606 return PTR_ERR(data->vbus_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200607
Hans de Goede8665c182015-06-13 14:37:51 +0200608 if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
609 data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
610 "usb0_vbus_power-supply");
611 if (IS_ERR(data->vbus_power_supply))
612 return PTR_ERR(data->vbus_power_supply);
613
614 if (!data->vbus_power_supply)
615 return -EPROBE_DEFER;
616 }
617
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200618 data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
Hans de Goede1a52abe2015-06-13 14:37:46 +0200619
Hans de Goede5f90d312016-09-07 22:20:39 +0200620 data->extcon = devm_extcon_dev_allocate(dev, sun4i_usb_phy0_cable);
621 if (IS_ERR(data->extcon))
622 return PTR_ERR(data->extcon);
623
624 ret = devm_extcon_dev_register(dev, data->extcon);
625 if (ret) {
626 dev_err(dev, "failed to register extcon: %d\n", ret);
627 return ret;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200628 }
629
Hans de Goede5f90d312016-09-07 22:20:39 +0200630 for (i = 0; i < data->cfg->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200631 struct sun4i_usb_phy *phy = data->phys + i;
632 char name[16];
633
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100634 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200635 phy->vbus = devm_regulator_get_optional(dev, name);
636 if (IS_ERR(phy->vbus)) {
637 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100638 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200639 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100640 }
641
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100642 if (data->cfg->dedicated_clocks)
Maxime Ripardeadd4312014-05-13 17:44:18 +0200643 snprintf(name, sizeof(name), "usb%d_phy", i);
644 else
645 strlcpy(name, "usb_phy", sizeof(name));
646
647 phy->clk = devm_clk_get(dev, name);
648 if (IS_ERR(phy->clk)) {
649 dev_err(dev, "failed to get clock %s\n", name);
650 return PTR_ERR(phy->clk);
651 }
652
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100653 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200654 phy->reset = devm_reset_control_get(dev, name);
655 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100656 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200657 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100658 }
659
660 if (i) { /* No pmu for usbc0 */
661 snprintf(name, sizeof(name), "pmu%d", i);
662 res = platform_get_resource_byname(pdev,
663 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200664 phy->pmu = devm_ioremap_resource(dev, res);
665 if (IS_ERR(phy->pmu))
666 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100667 }
668
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200669 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200670 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100671 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200672 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100673 }
674
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200675 phy->index = i;
676 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100677 }
678
Hans de Goeded2332302015-06-13 14:37:45 +0200679 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
Quentin Schulz5cf700a2016-06-13 13:45:48 +0200680 if (data->id_det_irq > 0) {
Hans de Goeded2332302015-06-13 14:37:45 +0200681 ret = devm_request_irq(dev, data->id_det_irq,
682 sun4i_usb_phy0_id_vbus_det_irq,
683 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
684 "usb0-id-det", data);
685 if (ret) {
686 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
687 return ret;
688 }
689 }
690
Hans de Goede91d96f02016-06-29 20:14:10 +0200691 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
Quentin Schulz5cf700a2016-06-13 13:45:48 +0200692 if (data->vbus_det_irq > 0) {
Hans de Goeded2332302015-06-13 14:37:45 +0200693 ret = devm_request_irq(dev, data->vbus_det_irq,
694 sun4i_usb_phy0_id_vbus_det_irq,
695 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
696 "usb0-vbus-det", data);
697 if (ret) {
698 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
699 data->vbus_det_irq = -1;
700 sun4i_usb_phy_remove(pdev); /* Stop detect work */
701 return ret;
702 }
703 }
704
Hans de Goede8665c182015-06-13 14:37:51 +0200705 if (data->vbus_power_supply) {
706 data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
707 data->vbus_power_nb.priority = 0;
708 ret = power_supply_reg_notifier(&data->vbus_power_nb);
709 if (ret) {
710 sun4i_usb_phy_remove(pdev); /* Stop detect work */
711 return ret;
712 }
713 data->vbus_power_nb_registered = true;
714 }
715
Hans de Goeded2332302015-06-13 14:37:45 +0200716 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
717 if (IS_ERR(phy_provider)) {
718 sun4i_usb_phy_remove(pdev); /* Stop detect work */
719 return PTR_ERR(phy_provider);
720 }
721
722 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100723}
724
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100725static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
726 .num_phys = 3,
727 .type = sun4i_a10_phy,
728 .disc_thresh = 3,
729 .phyctl_offset = REG_PHYCTL_A10,
730 .dedicated_clocks = false,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800731 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100732};
733
734static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
735 .num_phys = 2,
736 .type = sun4i_a10_phy,
737 .disc_thresh = 2,
738 .phyctl_offset = REG_PHYCTL_A10,
739 .dedicated_clocks = false,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800740 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100741};
742
743static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
744 .num_phys = 3,
Hans de Goede91d96f02016-06-29 20:14:10 +0200745 .type = sun6i_a31_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100746 .disc_thresh = 3,
747 .phyctl_offset = REG_PHYCTL_A10,
748 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800749 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100750};
751
752static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
753 .num_phys = 3,
754 .type = sun4i_a10_phy,
755 .disc_thresh = 2,
756 .phyctl_offset = REG_PHYCTL_A10,
757 .dedicated_clocks = false,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800758 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100759};
760
761static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
762 .num_phys = 2,
763 .type = sun4i_a10_phy,
764 .disc_thresh = 3,
765 .phyctl_offset = REG_PHYCTL_A10,
766 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800767 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100768};
769
770static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
771 .num_phys = 2,
772 .type = sun8i_a33_phy,
773 .disc_thresh = 3,
774 .phyctl_offset = REG_PHYCTL_A33,
775 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800776 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100777};
778
Reinder de Haan626a6302015-12-11 16:32:18 +0100779static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
780 .num_phys = 4,
781 .type = sun8i_h3_phy,
782 .disc_thresh = 3,
783 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800784 .enable_pmu_unk1 = true,
785};
786
787static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
788 .num_phys = 2,
789 .type = sun50i_a64_phy,
790 .disc_thresh = 3,
791 .phyctl_offset = REG_PHYCTL_A33,
792 .dedicated_clocks = true,
793 .enable_pmu_unk1 = true,
Reinder de Haan626a6302015-12-11 16:32:18 +0100794};
795
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100796static const struct of_device_id sun4i_usb_phy_of_match[] = {
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100797 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
798 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
799 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
800 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
801 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
802 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
Reinder de Haan626a6302015-12-11 16:32:18 +0100803 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800804 { .compatible = "allwinner,sun50i-a64-usb-phy",
805 .data = &sun50i_a64_cfg},
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100806 { },
807};
808MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
809
810static struct platform_driver sun4i_usb_phy_driver = {
811 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200812 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100813 .driver = {
814 .of_match_table = sun4i_usb_phy_of_match,
815 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100816 }
817};
818module_platform_driver(sun4i_usb_phy_driver);
819
820MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
821MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
822MODULE_LICENSE("GPL v2");