blob: 8c7eb335622ee1781a6b83d155740153b00d4717 [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
53#define REG_PMU_UNK_H3 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,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100101};
102
103struct sun4i_usb_phy_cfg {
104 int num_phys;
105 enum sun4i_usb_phy_type type;
106 u32 disc_thresh;
107 u8 phyctl_offset;
108 bool dedicated_clocks;
109};
110
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100111struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100112 void __iomem *base;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100113 const struct sun4i_usb_phy_cfg *cfg;
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200114 enum usb_dr_mode dr_mode;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100115 struct mutex mutex;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100116 struct sun4i_usb_phy {
117 struct phy *phy;
118 void __iomem *pmu;
119 struct regulator *vbus;
120 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200121 struct clk *clk;
Hans de Goeded2332302015-06-13 14:37:45 +0200122 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100123 int index;
124 } phys[MAX_PHYS];
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200125 int first_phy;
Hans de Goeded2332302015-06-13 14:37:45 +0200126 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200127 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200128 bool phy0_init;
Hans de Goeded2332302015-06-13 14:37:45 +0200129 struct gpio_desc *id_det_gpio;
130 struct gpio_desc *vbus_det_gpio;
Hans de Goede8665c182015-06-13 14:37:51 +0200131 struct power_supply *vbus_power_supply;
132 struct notifier_block vbus_power_nb;
133 bool vbus_power_nb_registered;
Hans de Goeded2332302015-06-13 14:37:45 +0200134 int id_det_irq;
135 int vbus_det_irq;
136 int id_det;
137 int vbus_det;
138 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100139};
140
141#define to_sun4i_usb_phy_data(phy) \
142 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
143
Hans de Goeded2332302015-06-13 14:37:45 +0200144static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
145{
146 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
147 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
148 u32 iscr;
149
150 iscr = readl(data->base + REG_ISCR);
151 iscr &= ~clr;
152 iscr |= set;
153 writel(iscr, data->base + REG_ISCR);
154}
155
156static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
157{
158 if (val)
159 val = ISCR_FORCE_ID_HIGH;
160 else
161 val = ISCR_FORCE_ID_LOW;
162
163 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
164}
165
166static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
167{
168 if (val)
169 val = ISCR_FORCE_VBUS_HIGH;
170 else
171 val = ISCR_FORCE_VBUS_LOW;
172
173 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
174}
175
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100176static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
177 int len)
178{
179 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
180 u32 temp, usbc_bit = BIT(phy->index * 2);
Ben Dooksd99cb372016-06-07 18:14:56 +0100181 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100182 int i;
183
184 mutex_lock(&phy_data->mutex);
185
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100186 if (phy_data->cfg->type == sun8i_a33_phy) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200187 /* A33 needs us to set phyctl to 0 explicitly */
188 writel(0, phyctl);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200189 }
190
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100191 for (i = 0; i < len; i++) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200192 temp = readl(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100193
194 /* clear the address portion */
195 temp &= ~(0xff << 8);
196
197 /* set the address */
198 temp |= ((addr + i) << 8);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200199 writel(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100200
201 /* set the data bit and clear usbc bit*/
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200202 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100203 if (data & 0x1)
204 temp |= PHYCTL_DATA;
205 else
206 temp &= ~PHYCTL_DATA;
207 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200208 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100209
210 /* pulse usbc_bit */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200211 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100212 temp |= usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200213 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100214
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200215 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100216 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200217 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100218
219 data >>= 1;
220 }
221 mutex_unlock(&phy_data->mutex);
222}
223
224static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
225{
226 u32 bits, reg_value;
227
228 if (!phy->pmu)
229 return;
230
231 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
232 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
233
234 reg_value = readl(phy->pmu);
235
236 if (enable)
237 reg_value |= bits;
238 else
239 reg_value &= ~bits;
240
241 writel(reg_value, phy->pmu);
242}
243
244static int sun4i_usb_phy_init(struct phy *_phy)
245{
246 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
247 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
248 int ret;
Reinder de Haan626a6302015-12-11 16:32:18 +0100249 u32 val;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100250
Maxime Ripardeadd4312014-05-13 17:44:18 +0200251 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100252 if (ret)
253 return ret;
254
255 ret = reset_control_deassert(phy->reset);
256 if (ret) {
Maxime Ripardeadd4312014-05-13 17:44:18 +0200257 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100258 return ret;
259 }
260
Reinder de Haan626a6302015-12-11 16:32:18 +0100261 if (data->cfg->type == sun8i_h3_phy) {
262 if (phy->index == 0) {
263 val = readl(data->base + REG_PHY_UNK_H3);
264 writel(val & ~1, data->base + REG_PHY_UNK_H3);
265 }
Roman Byshko6827a46f2014-11-10 19:55:06 +0100266
Reinder de Haan626a6302015-12-11 16:32:18 +0100267 val = readl(phy->pmu + REG_PMU_UNK_H3);
268 writel(val & ~2, phy->pmu + REG_PMU_UNK_H3);
269 } else {
270 /* Enable USB 45 Ohm resistor calibration */
271 if (phy->index == 0)
272 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100273
Reinder de Haan626a6302015-12-11 16:32:18 +0100274 /* Adjust PHY's magnitude and rate */
275 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
276
277 /* Disconnect threshold adjustment */
278 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
279 data->cfg->disc_thresh, 2);
280 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100281
282 sun4i_usb_phy_passby(phy, 1);
283
Hans de Goeded2332302015-06-13 14:37:45 +0200284 if (phy->index == 0) {
285 data->phy0_init = true;
286
287 /* Enable pull-ups */
288 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
289 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
290
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200291 /* Force ISCR and cable state updates */
292 data->id_det = -1;
293 data->vbus_det = -1;
294 queue_delayed_work(system_wq, &data->detect, 0);
Hans de Goeded2332302015-06-13 14:37:45 +0200295 }
296
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100297 return 0;
298}
299
300static int sun4i_usb_phy_exit(struct phy *_phy)
301{
302 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200303 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
304
305 if (phy->index == 0) {
306 /* Disable pull-ups */
307 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
308 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
309 data->phy0_init = false;
310 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100311
312 sun4i_usb_phy_passby(phy, 0);
313 reset_control_assert(phy->reset);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200314 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100315
316 return 0;
317}
318
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200319static int sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data *data)
320{
321 switch (data->dr_mode) {
322 case USB_DR_MODE_OTG:
323 return gpiod_get_value_cansleep(data->id_det_gpio);
324 case USB_DR_MODE_HOST:
325 return 0;
326 case USB_DR_MODE_PERIPHERAL:
327 default:
328 return 1;
329 }
330}
331
Hans de Goede3d772c42015-07-31 10:01:41 +0200332static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
333{
334 if (data->vbus_det_gpio)
335 return gpiod_get_value_cansleep(data->vbus_det_gpio);
336
337 if (data->vbus_power_supply) {
338 union power_supply_propval val;
339 int r;
340
341 r = power_supply_get_property(data->vbus_power_supply,
342 POWER_SUPPLY_PROP_PRESENT, &val);
343 if (r == 0)
344 return val.intval;
345 }
346
347 /* Fallback: report vbus as high */
348 return 1;
349}
350
351static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
352{
353 return data->vbus_det_gpio || data->vbus_power_supply;
354}
355
Hans de Goede91d96f02016-06-29 20:14:10 +0200356static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
357{
358 if ((data->id_det_gpio && data->id_det_irq <= 0) ||
359 (data->vbus_det_gpio && data->vbus_det_irq <= 0))
360 return true;
361
362 /*
363 * The A31 companion pmic (axp221) does not generate vbus change
364 * interrupts when the board is driving vbus, so we must poll
365 * when using the pmic for vbus-det _and_ we're driving vbus.
366 */
367 if (data->cfg->type == sun6i_a31_phy &&
368 data->vbus_power_supply && data->phys[0].regulator_on)
369 return true;
370
371 return false;
372}
373
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100374static int sun4i_usb_phy_power_on(struct phy *_phy)
375{
376 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200377 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
378 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100379
Hans de Goeded2332302015-06-13 14:37:45 +0200380 if (!phy->vbus || phy->regulator_on)
381 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100382
Hans de Goeded2332302015-06-13 14:37:45 +0200383 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
Hans de Goede80835262015-07-31 10:01:42 +0200384 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
385 data->vbus_det)
Hans de Goeded2332302015-06-13 14:37:45 +0200386 return 0;
387
388 ret = regulator_enable(phy->vbus);
389 if (ret)
390 return ret;
391
392 phy->regulator_on = true;
393
394 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
Hans de Goede91d96f02016-06-29 20:14:10 +0200395 if (phy->index == 0 && sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200396 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
397
398 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100399}
400
401static int sun4i_usb_phy_power_off(struct phy *_phy)
402{
403 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200404 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100405
Hans de Goeded2332302015-06-13 14:37:45 +0200406 if (!phy->vbus || !phy->regulator_on)
407 return 0;
408
409 regulator_disable(phy->vbus);
410 phy->regulator_on = false;
411
412 /*
413 * phy0 vbus typically slowly discharges, sometimes this causes the
414 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
415 */
Hans de Goede91d96f02016-06-29 20:14:10 +0200416 if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200417 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100418
419 return 0;
420}
421
Hans de Goede24fe86a2015-03-29 12:50:46 +0200422void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
423{
424 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
425
426 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
427}
Hans de Goede7167bf82015-07-31 10:01:40 +0200428EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
Hans de Goede24fe86a2015-03-29 12:50:46 +0200429
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800430static const struct phy_ops sun4i_usb_phy_ops = {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100431 .init = sun4i_usb_phy_init,
432 .exit = sun4i_usb_phy_exit,
433 .power_on = sun4i_usb_phy_power_on,
434 .power_off = sun4i_usb_phy_power_off,
435 .owner = THIS_MODULE,
436};
437
Hans de Goeded2332302015-06-13 14:37:45 +0200438static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
439{
440 struct sun4i_usb_phy_data *data =
441 container_of(work, struct sun4i_usb_phy_data, detect.work);
442 struct phy *phy0 = data->phys[0].phy;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200443 int id_det, vbus_det, id_notify = 0, vbus_notify = 0;
Hans de Goeded2332302015-06-13 14:37:45 +0200444
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200445 if (phy0 == NULL)
446 return;
447
448 id_det = sun4i_usb_phy0_get_id_det(data);
Hans de Goede8665c182015-06-13 14:37:51 +0200449 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
Hans de Goeded2332302015-06-13 14:37:45 +0200450
451 mutex_lock(&phy0->mutex);
452
453 if (!data->phy0_init) {
454 mutex_unlock(&phy0->mutex);
455 return;
456 }
457
458 if (id_det != data->id_det) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200459 /*
460 * When a host cable (id == 0) gets plugged in on systems
461 * without vbus detection report vbus low for long enough for
462 * the musb-ip to end the current device session.
463 */
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200464 if (data->dr_mode == USB_DR_MODE_OTG &&
465 !sun4i_usb_phy0_have_vbus_det(data) && id_det == 0) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200466 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
467 msleep(200);
468 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
469 }
Hans de Goeded2332302015-06-13 14:37:45 +0200470 sun4i_usb_phy0_set_id_detect(phy0, id_det);
471 data->id_det = id_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200472 id_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200473 }
474
475 if (vbus_det != data->vbus_det) {
476 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
477 data->vbus_det = vbus_det;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200478 vbus_notify = 1;
Hans de Goeded2332302015-06-13 14:37:45 +0200479 }
480
481 mutex_unlock(&phy0->mutex);
482
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200483 if (id_notify) {
Hans de Goede1a52abe2015-06-13 14:37:46 +0200484 extcon_set_cable_state_(data->extcon, EXTCON_USB_HOST,
485 !id_det);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200486 /*
487 * When a host cable gets unplugged (id == 1) on systems
488 * without vbus detection report vbus low for long enough to
489 * the musb-ip to end the current host session.
490 */
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200491 if (data->dr_mode == USB_DR_MODE_OTG &&
492 !sun4i_usb_phy0_have_vbus_det(data) && id_det == 1) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200493 mutex_lock(&phy0->mutex);
494 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
495 msleep(1000);
496 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
497 mutex_unlock(&phy0->mutex);
498 }
499 }
Hans de Goede1a52abe2015-06-13 14:37:46 +0200500
501 if (vbus_notify)
502 extcon_set_cable_state_(data->extcon, EXTCON_USB, vbus_det);
503
Hans de Goede91d96f02016-06-29 20:14:10 +0200504 if (sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200505 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
506}
507
508static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
509{
510 struct sun4i_usb_phy_data *data = dev_id;
511
512 /* vbus or id changed, let the pins settle and then scan them */
513 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
514
515 return IRQ_HANDLED;
516}
517
Hans de Goede8665c182015-06-13 14:37:51 +0200518static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
519 unsigned long val, void *v)
520{
521 struct sun4i_usb_phy_data *data =
522 container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
523 struct power_supply *psy = v;
524
525 /* Properties on the vbus_power_supply changed, scan vbus_det */
526 if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
527 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
528
529 return NOTIFY_OK;
530}
531
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100532static struct phy *sun4i_usb_phy_xlate(struct device *dev,
533 struct of_phandle_args *args)
534{
535 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
536
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200537 if (args->args[0] < data->first_phy ||
538 args->args[0] >= data->cfg->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100539 return ERR_PTR(-ENODEV);
540
541 return data->phys[args->args[0]].phy;
542}
543
Hans de Goeded2332302015-06-13 14:37:45 +0200544static int sun4i_usb_phy_remove(struct platform_device *pdev)
545{
546 struct device *dev = &pdev->dev;
547 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
548
Hans de Goede8665c182015-06-13 14:37:51 +0200549 if (data->vbus_power_nb_registered)
550 power_supply_unreg_notifier(&data->vbus_power_nb);
Hans de Goede04e59a02016-06-18 11:31:33 +0200551 if (data->id_det_irq > 0)
Hans de Goeded2332302015-06-13 14:37:45 +0200552 devm_free_irq(dev, data->id_det_irq, data);
Hans de Goede04e59a02016-06-18 11:31:33 +0200553 if (data->vbus_det_irq > 0)
Hans de Goeded2332302015-06-13 14:37:45 +0200554 devm_free_irq(dev, data->vbus_det_irq, data);
555
556 cancel_delayed_work_sync(&data->detect);
557
558 return 0;
559}
560
Hans de Goede1a52abe2015-06-13 14:37:46 +0200561static const unsigned int sun4i_usb_phy0_cable[] = {
562 EXTCON_USB,
563 EXTCON_USB_HOST,
564 EXTCON_NONE,
565};
566
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100567static int sun4i_usb_phy_probe(struct platform_device *pdev)
568{
569 struct sun4i_usb_phy_data *data;
570 struct device *dev = &pdev->dev;
571 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100572 struct phy_provider *phy_provider;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100573 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200574 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100575
576 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
577 if (!data)
578 return -ENOMEM;
579
580 mutex_init(&data->mutex);
Hans de Goeded2332302015-06-13 14:37:45 +0200581 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
582 dev_set_drvdata(dev, data);
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100583 data->cfg = of_device_get_match_data(dev);
584 if (!data->cfg)
585 return -EINVAL;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200586
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100587 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
588 data->base = devm_ioremap_resource(dev, res);
589 if (IS_ERR(data->base))
590 return PTR_ERR(data->base);
591
Axel Linb2dfc342015-09-25 08:47:29 +0800592 data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
593 GPIOD_IN);
594 if (IS_ERR(data->id_det_gpio))
595 return PTR_ERR(data->id_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200596
Axel Linb2dfc342015-09-25 08:47:29 +0800597 data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
598 GPIOD_IN);
599 if (IS_ERR(data->vbus_det_gpio))
600 return PTR_ERR(data->vbus_det_gpio);
Hans de Goeded2332302015-06-13 14:37:45 +0200601
Hans de Goede8665c182015-06-13 14:37:51 +0200602 if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
603 data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
604 "usb0_vbus_power-supply");
605 if (IS_ERR(data->vbus_power_supply))
606 return PTR_ERR(data->vbus_power_supply);
607
608 if (!data->vbus_power_supply)
609 return -EPROBE_DEFER;
610 }
611
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200612 data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
613 switch (data->dr_mode) {
614 case USB_DR_MODE_OTG:
615 /* otg without id_det makes no sense, and is not supported */
616 if (!data->id_det_gpio) {
617 dev_err(dev, "usb0_id_det missing or invalid\n");
618 return -ENODEV;
619 }
620 /* fall through */
621 case USB_DR_MODE_HOST:
622 case USB_DR_MODE_PERIPHERAL:
Hans de Goede1a52abe2015-06-13 14:37:46 +0200623 data->extcon = devm_extcon_dev_allocate(dev,
624 sun4i_usb_phy0_cable);
625 if (IS_ERR(data->extcon))
626 return PTR_ERR(data->extcon);
627
628 ret = devm_extcon_dev_register(dev, data->extcon);
629 if (ret) {
630 dev_err(dev, "failed to register extcon: %d\n", ret);
631 return ret;
632 }
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200633 break;
634 default:
635 dev_info(dev, "dr_mode unknown, not registering usb phy0\n");
636 data->first_phy = 1;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200637 }
638
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200639 for (i = data->first_phy; i < data->cfg->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200640 struct sun4i_usb_phy *phy = data->phys + i;
641 char name[16];
642
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100643 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200644 phy->vbus = devm_regulator_get_optional(dev, name);
645 if (IS_ERR(phy->vbus)) {
646 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100647 return -EPROBE_DEFER;
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200648 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100649 }
650
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100651 if (data->cfg->dedicated_clocks)
Maxime Ripardeadd4312014-05-13 17:44:18 +0200652 snprintf(name, sizeof(name), "usb%d_phy", i);
653 else
654 strlcpy(name, "usb_phy", sizeof(name));
655
656 phy->clk = devm_clk_get(dev, name);
657 if (IS_ERR(phy->clk)) {
658 dev_err(dev, "failed to get clock %s\n", name);
659 return PTR_ERR(phy->clk);
660 }
661
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100662 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200663 phy->reset = devm_reset_control_get(dev, name);
664 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100665 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200666 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100667 }
668
669 if (i) { /* No pmu for usbc0 */
670 snprintf(name, sizeof(name), "pmu%d", i);
671 res = platform_get_resource_byname(pdev,
672 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200673 phy->pmu = devm_ioremap_resource(dev, res);
674 if (IS_ERR(phy->pmu))
675 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100676 }
677
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200678 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200679 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100680 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200681 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100682 }
683
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200684 phy->index = i;
685 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100686 }
687
Hans de Goeded2332302015-06-13 14:37:45 +0200688 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
Quentin Schulz5cf700a2016-06-13 13:45:48 +0200689 if (data->id_det_irq > 0) {
Hans de Goeded2332302015-06-13 14:37:45 +0200690 ret = devm_request_irq(dev, data->id_det_irq,
691 sun4i_usb_phy0_id_vbus_det_irq,
692 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
693 "usb0-id-det", data);
694 if (ret) {
695 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
696 return ret;
697 }
698 }
699
Hans de Goede91d96f02016-06-29 20:14:10 +0200700 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
Quentin Schulz5cf700a2016-06-13 13:45:48 +0200701 if (data->vbus_det_irq > 0) {
Hans de Goeded2332302015-06-13 14:37:45 +0200702 ret = devm_request_irq(dev, data->vbus_det_irq,
703 sun4i_usb_phy0_id_vbus_det_irq,
704 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
705 "usb0-vbus-det", data);
706 if (ret) {
707 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
708 data->vbus_det_irq = -1;
709 sun4i_usb_phy_remove(pdev); /* Stop detect work */
710 return ret;
711 }
712 }
713
Hans de Goede8665c182015-06-13 14:37:51 +0200714 if (data->vbus_power_supply) {
715 data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
716 data->vbus_power_nb.priority = 0;
717 ret = power_supply_reg_notifier(&data->vbus_power_nb);
718 if (ret) {
719 sun4i_usb_phy_remove(pdev); /* Stop detect work */
720 return ret;
721 }
722 data->vbus_power_nb_registered = true;
723 }
724
Hans de Goeded2332302015-06-13 14:37:45 +0200725 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
726 if (IS_ERR(phy_provider)) {
727 sun4i_usb_phy_remove(pdev); /* Stop detect work */
728 return PTR_ERR(phy_provider);
729 }
730
731 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100732}
733
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100734static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
735 .num_phys = 3,
736 .type = sun4i_a10_phy,
737 .disc_thresh = 3,
738 .phyctl_offset = REG_PHYCTL_A10,
739 .dedicated_clocks = false,
740};
741
742static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
743 .num_phys = 2,
744 .type = sun4i_a10_phy,
745 .disc_thresh = 2,
746 .phyctl_offset = REG_PHYCTL_A10,
747 .dedicated_clocks = false,
748};
749
750static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
751 .num_phys = 3,
Hans de Goede91d96f02016-06-29 20:14:10 +0200752 .type = sun6i_a31_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100753 .disc_thresh = 3,
754 .phyctl_offset = REG_PHYCTL_A10,
755 .dedicated_clocks = true,
756};
757
758static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
759 .num_phys = 3,
760 .type = sun4i_a10_phy,
761 .disc_thresh = 2,
762 .phyctl_offset = REG_PHYCTL_A10,
763 .dedicated_clocks = false,
764};
765
766static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
767 .num_phys = 2,
768 .type = sun4i_a10_phy,
769 .disc_thresh = 3,
770 .phyctl_offset = REG_PHYCTL_A10,
771 .dedicated_clocks = true,
772};
773
774static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
775 .num_phys = 2,
776 .type = sun8i_a33_phy,
777 .disc_thresh = 3,
778 .phyctl_offset = REG_PHYCTL_A33,
779 .dedicated_clocks = true,
780};
781
Reinder de Haan626a6302015-12-11 16:32:18 +0100782static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
783 .num_phys = 4,
784 .type = sun8i_h3_phy,
785 .disc_thresh = 3,
786 .dedicated_clocks = true,
787};
788
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100789static const struct of_device_id sun4i_usb_phy_of_match[] = {
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100790 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
791 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
792 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
793 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
794 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
795 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
Reinder de Haan626a6302015-12-11 16:32:18 +0100796 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100797 { },
798};
799MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
800
801static struct platform_driver sun4i_usb_phy_driver = {
802 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200803 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100804 .driver = {
805 .of_match_table = sun4i_usb_phy_of_match,
806 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100807 }
808};
809module_platform_driver(sun4i_usb_phy_driver);
810
811MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
812MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
813MODULE_LICENSE("GPL v2");