blob: 85692738224873e0670ec8e6f51485d9d4203bc1 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans de Goedeba4bdc92014-03-01 18:09:26 +01002/*
3 * Allwinner sun4i USB phy driver
4 *
Hans de Goeded2332302015-06-13 14:37:45 +02005 * Copyright (C) 2014-2015 Hans de Goede <hdegoede@redhat.com>
Hans de Goedeba4bdc92014-03-01 18:09:26 +01006 *
7 * Based on code from
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 *
10 * Modelled after: Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
11 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
12 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010013 */
14
15#include <linux/clk.h>
Hans de Goede1aedf3a2015-06-13 14:37:50 +020016#include <linux/delay.h>
Sachin Kamat2d84aff92014-05-29 12:00:49 +053017#include <linux/err.h>
Chanwoo Choi176aa362017-09-21 12:11:24 +090018#include <linux/extcon-provider.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010019#include <linux/io.h>
Hans de Goeded2332302015-06-13 14:37:45 +020020#include <linux/interrupt.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010021#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24#include <linux/of.h>
25#include <linux/of_address.h>
Hans de Goede68dbc2c2015-12-11 16:32:17 +010026#include <linux/of_device.h>
Hans de Goeded2332302015-06-13 14:37:45 +020027#include <linux/of_gpio.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010028#include <linux/phy/phy.h>
Hans de Goede24fe86a2015-03-29 12:50:46 +020029#include <linux/phy/phy-sun4i-usb.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010030#include <linux/platform_device.h>
Hans de Goede8665c182015-06-13 14:37:51 +020031#include <linux/power_supply.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010032#include <linux/regulator/consumer.h>
33#include <linux/reset.h>
Chen-Yu Tsai919ab252016-09-09 11:58:18 +080034#include <linux/spinlock.h>
Hans de Goedeb33ecca2016-08-08 21:55:39 +020035#include <linux/usb/of.h>
Hans de Goeded2332302015-06-13 14:37:45 +020036#include <linux/workqueue.h>
Hans de Goedeba4bdc92014-03-01 18:09:26 +010037
38#define REG_ISCR 0x00
Hans de Goedefc1f45e2015-06-13 14:37:49 +020039#define REG_PHYCTL_A10 0x04
Hans de Goedeba4bdc92014-03-01 18:09:26 +010040#define REG_PHYBIST 0x08
41#define REG_PHYTUNE 0x0c
Hans de Goedefc1f45e2015-06-13 14:37:49 +020042#define REG_PHYCTL_A33 0x10
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +080043#define REG_PHY_OTGCTL 0x20
Reinder de Haan626a6302015-12-11 16:32:18 +010044
Icenowy Zhengb3e0d142016-08-12 11:06:21 +080045#define REG_PMU_UNK1 0x10
Hans de Goedeba4bdc92014-03-01 18:09:26 +010046
47#define PHYCTL_DATA BIT(7)
48
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +080049#define OTGCTL_ROUTE_MUSB BIT(0)
50
Hans de Goedeba4bdc92014-03-01 18:09:26 +010051#define SUNXI_AHB_ICHR8_EN BIT(10)
52#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
53#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
54#define SUNXI_ULPI_BYPASS_EN BIT(0)
55
Hans de Goeded2332302015-06-13 14:37:45 +020056/* ISCR, Interface Status and Control bits */
57#define ISCR_ID_PULLUP_EN (1 << 17)
58#define ISCR_DPDM_PULLUP_EN (1 << 16)
59/* sunxi has the phy id/vbus pins not connected, so we use the force bits */
60#define ISCR_FORCE_ID_MASK (3 << 14)
61#define ISCR_FORCE_ID_LOW (2 << 14)
62#define ISCR_FORCE_ID_HIGH (3 << 14)
63#define ISCR_FORCE_VBUS_MASK (3 << 12)
64#define ISCR_FORCE_VBUS_LOW (2 << 12)
65#define ISCR_FORCE_VBUS_HIGH (3 << 12)
66
Hans de Goedeba4bdc92014-03-01 18:09:26 +010067/* Common Control Bits for Both PHYs */
68#define PHY_PLL_BW 0x03
69#define PHY_RES45_CAL_EN 0x0c
70
71/* Private Control Bits for Each PHY */
72#define PHY_TX_AMPLITUDE_TUNE 0x20
73#define PHY_TX_SLEWRATE_TUNE 0x22
74#define PHY_VBUSVALID_TH_SEL 0x25
75#define PHY_PULLUP_RES_SEL 0x27
76#define PHY_OTG_FUNC_EN 0x28
77#define PHY_VBUS_DET_EN 0x29
78#define PHY_DISCON_TH_SEL 0x2a
Hans de Goede24fe86a2015-03-29 12:50:46 +020079#define PHY_SQUELCH_DETECT 0x3c
Hans de Goedeba4bdc92014-03-01 18:09:26 +010080
Chen-Yu Tsai4b637432017-08-03 16:14:07 +080081/* A83T specific control bits for PHY0 */
82#define PHY_CTL_VBUSVLDEXT BIT(5)
83#define PHY_CTL_SIDDQ BIT(3)
84
85/* A83T specific control bits for PHY2 HSIC */
86#define SUNXI_EHCI_HS_FORCE BIT(20)
87#define SUNXI_HSIC_CONNECT_DET BIT(17)
88#define SUNXI_HSIC_CONNECT_INT BIT(16)
89#define SUNXI_HSIC BIT(1)
90
Reinder de Haan626a6302015-12-11 16:32:18 +010091#define MAX_PHYS 4
Hans de Goedeba4bdc92014-03-01 18:09:26 +010092
Hans de Goeded2332302015-06-13 14:37:45 +020093/*
94 * Note do not raise the debounce time, we must report Vusb high within 100ms
95 * otherwise we get Vbus errors
96 */
97#define DEBOUNCE_TIME msecs_to_jiffies(50)
98#define POLL_TIME msecs_to_jiffies(250)
99
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100100enum sun4i_usb_phy_type {
101 sun4i_a10_phy,
Hans de Goede91d96f02016-06-29 20:14:10 +0200102 sun6i_a31_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100103 sun8i_a33_phy,
Chen-Yu Tsai4b637432017-08-03 16:14:07 +0800104 sun8i_a83t_phy,
Reinder de Haan626a6302015-12-11 16:32:18 +0100105 sun8i_h3_phy,
Icenowy Zhengf3d96f82018-01-03 16:49:44 +0800106 sun8i_r40_phy,
Icenowy Zheng16c40362017-01-03 23:25:31 +0800107 sun8i_v3s_phy,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800108 sun50i_a64_phy,
Icenowy Zhengae409cc2018-10-04 20:28:48 +0800109 sun50i_h6_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100110};
111
112struct sun4i_usb_phy_cfg {
113 int num_phys;
Chen-Yu Tsaif0152c52017-08-03 16:14:06 +0800114 int hsic_index;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100115 enum sun4i_usb_phy_type type;
116 u32 disc_thresh;
117 u8 phyctl_offset;
118 bool dedicated_clocks;
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800119 bool enable_pmu_unk1;
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +0800120 bool phy0_dual_route;
Icenowy Zheng2659392e2018-10-04 20:28:47 +0800121 int missing_phys;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100122};
123
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100124struct sun4i_usb_phy_data {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100125 void __iomem *base;
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100126 const struct sun4i_usb_phy_cfg *cfg;
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200127 enum usb_dr_mode dr_mode;
Chen-Yu Tsai919ab252016-09-09 11:58:18 +0800128 spinlock_t reg_lock; /* guard access to phyctl reg */
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100129 struct sun4i_usb_phy {
130 struct phy *phy;
131 void __iomem *pmu;
132 struct regulator *vbus;
133 struct reset_control *reset;
Maxime Ripardeadd4312014-05-13 17:44:18 +0200134 struct clk *clk;
Chen-Yu Tsaif0152c52017-08-03 16:14:06 +0800135 struct clk *clk2;
Hans de Goeded2332302015-06-13 14:37:45 +0200136 bool regulator_on;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100137 int index;
138 } phys[MAX_PHYS];
Hans de Goeded2332302015-06-13 14:37:45 +0200139 /* phy0 / otg related variables */
Hans de Goede1a52abe2015-06-13 14:37:46 +0200140 struct extcon_dev *extcon;
Hans de Goeded2332302015-06-13 14:37:45 +0200141 bool phy0_init;
Hans de Goeded2332302015-06-13 14:37:45 +0200142 struct gpio_desc *id_det_gpio;
143 struct gpio_desc *vbus_det_gpio;
Hans de Goede8665c182015-06-13 14:37:51 +0200144 struct power_supply *vbus_power_supply;
145 struct notifier_block vbus_power_nb;
146 bool vbus_power_nb_registered;
Hans de Goede36f91592016-09-07 22:20:38 +0200147 bool force_session_end;
Hans de Goeded2332302015-06-13 14:37:45 +0200148 int id_det_irq;
149 int vbus_det_irq;
150 int id_det;
151 int vbus_det;
152 struct delayed_work detect;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100153};
154
155#define to_sun4i_usb_phy_data(phy) \
156 container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
157
Hans de Goeded2332302015-06-13 14:37:45 +0200158static void sun4i_usb_phy0_update_iscr(struct phy *_phy, u32 clr, u32 set)
159{
160 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
161 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
162 u32 iscr;
163
164 iscr = readl(data->base + REG_ISCR);
165 iscr &= ~clr;
166 iscr |= set;
167 writel(iscr, data->base + REG_ISCR);
168}
169
170static void sun4i_usb_phy0_set_id_detect(struct phy *phy, u32 val)
171{
172 if (val)
173 val = ISCR_FORCE_ID_HIGH;
174 else
175 val = ISCR_FORCE_ID_LOW;
176
177 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_ID_MASK, val);
178}
179
180static void sun4i_usb_phy0_set_vbus_detect(struct phy *phy, u32 val)
181{
182 if (val)
183 val = ISCR_FORCE_VBUS_HIGH;
184 else
185 val = ISCR_FORCE_VBUS_LOW;
186
187 sun4i_usb_phy0_update_iscr(phy, ISCR_FORCE_VBUS_MASK, val);
188}
189
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100190static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
191 int len)
192{
193 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
194 u32 temp, usbc_bit = BIT(phy->index * 2);
Ben Dooksd99cb372016-06-07 18:14:56 +0100195 void __iomem *phyctl = phy_data->base + phy_data->cfg->phyctl_offset;
Chen-Yu Tsai919ab252016-09-09 11:58:18 +0800196 unsigned long flags;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100197 int i;
198
Chen-Yu Tsai919ab252016-09-09 11:58:18 +0800199 spin_lock_irqsave(&phy_data->reg_lock, flags);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100200
Icenowy Zhengd699c1d02017-03-25 22:50:09 +0800201 if (phy_data->cfg->phyctl_offset == REG_PHYCTL_A33) {
202 /* SoCs newer than A33 need us to set phyctl to 0 explicitly */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200203 writel(0, phyctl);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200204 }
205
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100206 for (i = 0; i < len; i++) {
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200207 temp = readl(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100208
209 /* clear the address portion */
210 temp &= ~(0xff << 8);
211
212 /* set the address */
213 temp |= ((addr + i) << 8);
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200214 writel(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100215
216 /* set the data bit and clear usbc bit*/
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200217 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100218 if (data & 0x1)
219 temp |= PHYCTL_DATA;
220 else
221 temp &= ~PHYCTL_DATA;
222 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200223 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100224
225 /* pulse usbc_bit */
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200226 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100227 temp |= usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200228 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100229
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200230 temp = readb(phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100231 temp &= ~usbc_bit;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200232 writeb(temp, phyctl);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100233
234 data >>= 1;
235 }
Chen-Yu Tsai919ab252016-09-09 11:58:18 +0800236
237 spin_unlock_irqrestore(&phy_data->reg_lock, flags);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100238}
239
240static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
241{
Chen-Yu Tsai4b637432017-08-03 16:14:07 +0800242 struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100243 u32 bits, reg_value;
244
245 if (!phy->pmu)
246 return;
247
248 bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
249 SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
250
Chen-Yu Tsai4b637432017-08-03 16:14:07 +0800251 /* A83T USB2 is HSIC */
252 if (phy_data->cfg->type == sun8i_a83t_phy && phy->index == 2)
253 bits |= SUNXI_EHCI_HS_FORCE | SUNXI_HSIC_CONNECT_INT |
254 SUNXI_HSIC;
255
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100256 reg_value = readl(phy->pmu);
257
258 if (enable)
259 reg_value |= bits;
260 else
261 reg_value &= ~bits;
262
263 writel(reg_value, phy->pmu);
264}
265
266static int sun4i_usb_phy_init(struct phy *_phy)
267{
268 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
269 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
270 int ret;
Reinder de Haan626a6302015-12-11 16:32:18 +0100271 u32 val;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100272
Maxime Ripardeadd4312014-05-13 17:44:18 +0200273 ret = clk_prepare_enable(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100274 if (ret)
275 return ret;
276
Chen-Yu Tsaif0152c52017-08-03 16:14:06 +0800277 ret = clk_prepare_enable(phy->clk2);
278 if (ret) {
279 clk_disable_unprepare(phy->clk);
280 return ret;
281 }
282
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100283 ret = reset_control_deassert(phy->reset);
284 if (ret) {
Chen-Yu Tsaif0152c52017-08-03 16:14:06 +0800285 clk_disable_unprepare(phy->clk2);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200286 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100287 return ret;
288 }
289
Icenowy Zhengae409cc2018-10-04 20:28:48 +0800290 if (data->cfg->type == sun8i_a83t_phy ||
291 data->cfg->type == sun50i_h6_phy) {
Chen-Yu Tsai4b637432017-08-03 16:14:07 +0800292 if (phy->index == 0) {
293 val = readl(data->base + data->cfg->phyctl_offset);
294 val |= PHY_CTL_VBUSVLDEXT;
295 val &= ~PHY_CTL_SIDDQ;
296 writel(val, data->base + data->cfg->phyctl_offset);
297 }
298 } else {
299 if (phy->pmu && data->cfg->enable_pmu_unk1) {
300 val = readl(phy->pmu + REG_PMU_UNK1);
301 writel(val & ~2, phy->pmu + REG_PMU_UNK1);
302 }
303
304 /* Enable USB 45 Ohm resistor calibration */
305 if (phy->index == 0)
306 sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
307
308 /* Adjust PHY's magnitude and rate */
309 sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
310
311 /* Disconnect threshold adjustment */
312 sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL,
313 data->cfg->disc_thresh, 2);
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800314 }
315
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100316 sun4i_usb_phy_passby(phy, 1);
317
Hans de Goeded2332302015-06-13 14:37:45 +0200318 if (phy->index == 0) {
319 data->phy0_init = true;
320
321 /* Enable pull-ups */
322 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_DPDM_PULLUP_EN);
323 sun4i_usb_phy0_update_iscr(_phy, 0, ISCR_ID_PULLUP_EN);
324
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200325 /* Force ISCR and cable state updates */
326 data->id_det = -1;
327 data->vbus_det = -1;
328 queue_delayed_work(system_wq, &data->detect, 0);
Hans de Goeded2332302015-06-13 14:37:45 +0200329 }
330
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100331 return 0;
332}
333
334static int sun4i_usb_phy_exit(struct phy *_phy)
335{
336 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200337 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
338
339 if (phy->index == 0) {
Icenowy Zhengae409cc2018-10-04 20:28:48 +0800340 if (data->cfg->type == sun8i_a83t_phy ||
341 data->cfg->type == sun50i_h6_phy) {
Chen-Yu Tsai4b637432017-08-03 16:14:07 +0800342 void __iomem *phyctl = data->base +
343 data->cfg->phyctl_offset;
344
345 writel(readl(phyctl) | PHY_CTL_SIDDQ, phyctl);
346 }
347
Hans de Goeded2332302015-06-13 14:37:45 +0200348 /* Disable pull-ups */
349 sun4i_usb_phy0_update_iscr(_phy, ISCR_DPDM_PULLUP_EN, 0);
350 sun4i_usb_phy0_update_iscr(_phy, ISCR_ID_PULLUP_EN, 0);
351 data->phy0_init = false;
352 }
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100353
354 sun4i_usb_phy_passby(phy, 0);
355 reset_control_assert(phy->reset);
Chen-Yu Tsaif0152c52017-08-03 16:14:06 +0800356 clk_disable_unprepare(phy->clk2);
Maxime Ripardeadd4312014-05-13 17:44:18 +0200357 clk_disable_unprepare(phy->clk);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100358
359 return 0;
360}
361
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200362static int sun4i_usb_phy0_get_id_det(struct sun4i_usb_phy_data *data)
363{
364 switch (data->dr_mode) {
365 case USB_DR_MODE_OTG:
Hans de Goede5f90d312016-09-07 22:20:39 +0200366 if (data->id_det_gpio)
367 return gpiod_get_value_cansleep(data->id_det_gpio);
368 else
369 return 1; /* Fallback to peripheral mode */
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200370 case USB_DR_MODE_HOST:
371 return 0;
372 case USB_DR_MODE_PERIPHERAL:
373 default:
374 return 1;
375 }
376}
377
Hans de Goede3d772c42015-07-31 10:01:41 +0200378static int sun4i_usb_phy0_get_vbus_det(struct sun4i_usb_phy_data *data)
379{
380 if (data->vbus_det_gpio)
381 return gpiod_get_value_cansleep(data->vbus_det_gpio);
382
383 if (data->vbus_power_supply) {
384 union power_supply_propval val;
385 int r;
386
387 r = power_supply_get_property(data->vbus_power_supply,
388 POWER_SUPPLY_PROP_PRESENT, &val);
389 if (r == 0)
390 return val.intval;
391 }
392
393 /* Fallback: report vbus as high */
394 return 1;
395}
396
397static bool sun4i_usb_phy0_have_vbus_det(struct sun4i_usb_phy_data *data)
398{
399 return data->vbus_det_gpio || data->vbus_power_supply;
400}
401
Hans de Goede91d96f02016-06-29 20:14:10 +0200402static bool sun4i_usb_phy0_poll(struct sun4i_usb_phy_data *data)
403{
404 if ((data->id_det_gpio && data->id_det_irq <= 0) ||
405 (data->vbus_det_gpio && data->vbus_det_irq <= 0))
406 return true;
407
408 /*
Chen-Yu Tsaid7119222018-01-19 17:25:41 +0800409 * The A31/A23/A33 companion pmics (AXP221/AXP223) do not
410 * generate vbus change interrupts when the board is driving
411 * vbus using the N_VBUSEN pin on the pmic, so we must poll
Hans de Goede91d96f02016-06-29 20:14:10 +0200412 * when using the pmic for vbus-det _and_ we're driving vbus.
413 */
Chen-Yu Tsaid7119222018-01-19 17:25:41 +0800414 if ((data->cfg->type == sun6i_a31_phy ||
415 data->cfg->type == sun8i_a33_phy) &&
Hans de Goede91d96f02016-06-29 20:14:10 +0200416 data->vbus_power_supply && data->phys[0].regulator_on)
417 return true;
418
419 return false;
420}
421
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100422static int sun4i_usb_phy_power_on(struct phy *_phy)
423{
424 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200425 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
426 int ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100427
Hans de Goeded2332302015-06-13 14:37:45 +0200428 if (!phy->vbus || phy->regulator_on)
429 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100430
Hans de Goeded2332302015-06-13 14:37:45 +0200431 /* For phy0 only turn on Vbus if we don't have an ext. Vbus */
Hans de Goede80835262015-07-31 10:01:42 +0200432 if (phy->index == 0 && sun4i_usb_phy0_have_vbus_det(data) &&
Hans de Goede91d6e3b2016-09-07 22:20:41 +0200433 data->vbus_det) {
434 dev_warn(&_phy->dev, "External vbus detected, not enabling our own vbus\n");
Hans de Goeded2332302015-06-13 14:37:45 +0200435 return 0;
Hans de Goede91d6e3b2016-09-07 22:20:41 +0200436 }
Hans de Goeded2332302015-06-13 14:37:45 +0200437
438 ret = regulator_enable(phy->vbus);
439 if (ret)
440 return ret;
441
442 phy->regulator_on = true;
443
444 /* We must report Vbus high within OTG_TIME_A_WAIT_VRISE msec. */
Hans de Goede91d96f02016-06-29 20:14:10 +0200445 if (phy->index == 0 && sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200446 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
447
448 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100449}
450
451static int sun4i_usb_phy_power_off(struct phy *_phy)
452{
453 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
Hans de Goeded2332302015-06-13 14:37:45 +0200454 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100455
Hans de Goeded2332302015-06-13 14:37:45 +0200456 if (!phy->vbus || !phy->regulator_on)
457 return 0;
458
459 regulator_disable(phy->vbus);
460 phy->regulator_on = false;
461
462 /*
463 * phy0 vbus typically slowly discharges, sometimes this causes the
464 * Vbus gpio to not trigger an edge irq on Vbus off, so force a rescan.
465 */
Hans de Goede91d96f02016-06-29 20:14:10 +0200466 if (phy->index == 0 && !sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200467 mod_delayed_work(system_wq, &data->detect, POLL_TIME);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100468
469 return 0;
470}
471
Grygorii Strashko79a5a182018-11-19 19:24:20 -0600472static int sun4i_usb_phy_set_mode(struct phy *_phy,
473 enum phy_mode mode, int submode)
Hans de Goede6ba43c22016-09-07 22:20:40 +0200474{
475 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
476 struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
Hans de Goede5d04c882016-09-23 16:40:56 +0300477 int new_mode;
Hans de Goede6ba43c22016-09-07 22:20:40 +0200478
Chen-Yu Tsai13969292019-03-22 16:51:07 +0800479 if (phy->index != 0) {
480 if (mode == PHY_MODE_USB_HOST)
481 return 0;
Hans de Goede6ba43c22016-09-07 22:20:40 +0200482 return -EINVAL;
Chen-Yu Tsai13969292019-03-22 16:51:07 +0800483 }
Hans de Goede6ba43c22016-09-07 22:20:40 +0200484
485 switch (mode) {
486 case PHY_MODE_USB_HOST:
Hans de Goede5d04c882016-09-23 16:40:56 +0300487 new_mode = USB_DR_MODE_HOST;
Hans de Goede6ba43c22016-09-07 22:20:40 +0200488 break;
489 case PHY_MODE_USB_DEVICE:
Hans de Goede5d04c882016-09-23 16:40:56 +0300490 new_mode = USB_DR_MODE_PERIPHERAL;
Hans de Goede6ba43c22016-09-07 22:20:40 +0200491 break;
492 case PHY_MODE_USB_OTG:
Hans de Goede5d04c882016-09-23 16:40:56 +0300493 new_mode = USB_DR_MODE_OTG;
Hans de Goede6ba43c22016-09-07 22:20:40 +0200494 break;
495 default:
496 return -EINVAL;
497 }
498
Hans de Goede5d04c882016-09-23 16:40:56 +0300499 if (new_mode != data->dr_mode) {
500 dev_info(&_phy->dev, "Changing dr_mode to %d\n", new_mode);
501 data->dr_mode = new_mode;
502 }
503
504 data->id_det = -1; /* Force reprocessing of id */
Hans de Goede6ba43c22016-09-07 22:20:40 +0200505 data->force_session_end = true;
506 queue_delayed_work(system_wq, &data->detect, 0);
507
508 return 0;
509}
510
Hans de Goede24fe86a2015-03-29 12:50:46 +0200511void sun4i_usb_phy_set_squelch_detect(struct phy *_phy, bool enabled)
512{
513 struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
514
515 sun4i_usb_phy_write(phy, PHY_SQUELCH_DETECT, enabled ? 0 : 2, 2);
516}
Hans de Goede7167bf82015-07-31 10:01:40 +0200517EXPORT_SYMBOL_GPL(sun4i_usb_phy_set_squelch_detect);
Hans de Goede24fe86a2015-03-29 12:50:46 +0200518
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800519static const struct phy_ops sun4i_usb_phy_ops = {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100520 .init = sun4i_usb_phy_init,
521 .exit = sun4i_usb_phy_exit,
522 .power_on = sun4i_usb_phy_power_on,
523 .power_off = sun4i_usb_phy_power_off,
Hans de Goede6ba43c22016-09-07 22:20:40 +0200524 .set_mode = sun4i_usb_phy_set_mode,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100525 .owner = THIS_MODULE,
526};
527
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +0800528static void sun4i_usb_phy0_reroute(struct sun4i_usb_phy_data *data, int id_det)
529{
530 u32 regval;
531
532 regval = readl(data->base + REG_PHY_OTGCTL);
533 if (id_det == 0) {
534 /* Host mode. Route phy0 to EHCI/OHCI */
535 regval &= ~OTGCTL_ROUTE_MUSB;
536 } else {
537 /* Peripheral mode. Route phy0 to MUSB */
538 regval |= OTGCTL_ROUTE_MUSB;
539 }
540 writel(regval, data->base + REG_PHY_OTGCTL);
541}
542
Hans de Goeded2332302015-06-13 14:37:45 +0200543static void sun4i_usb_phy0_id_vbus_det_scan(struct work_struct *work)
544{
545 struct sun4i_usb_phy_data *data =
546 container_of(work, struct sun4i_usb_phy_data, detect.work);
547 struct phy *phy0 = data->phys[0].phy;
Paul Kocialkowskie6f32ef2019-03-14 14:05:18 +0100548 struct sun4i_usb_phy *phy = phy_get_drvdata(phy0);
Hans de Goede36f91592016-09-07 22:20:38 +0200549 bool force_session_end, id_notify = false, vbus_notify = false;
Hans de Goede9745cee2016-09-07 22:20:37 +0200550 int id_det, vbus_det;
Hans de Goeded2332302015-06-13 14:37:45 +0200551
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200552 if (phy0 == NULL)
553 return;
554
555 id_det = sun4i_usb_phy0_get_id_det(data);
Hans de Goede8665c182015-06-13 14:37:51 +0200556 vbus_det = sun4i_usb_phy0_get_vbus_det(data);
Hans de Goeded2332302015-06-13 14:37:45 +0200557
558 mutex_lock(&phy0->mutex);
559
560 if (!data->phy0_init) {
561 mutex_unlock(&phy0->mutex);
562 return;
563 }
564
Hans de Goede36f91592016-09-07 22:20:38 +0200565 force_session_end = data->force_session_end;
566 data->force_session_end = false;
567
Hans de Goeded2332302015-06-13 14:37:45 +0200568 if (id_det != data->id_det) {
Hans de Goede36f91592016-09-07 22:20:38 +0200569 /* id-change, force session end if we've no vbus detection */
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200570 if (data->dr_mode == USB_DR_MODE_OTG &&
Hans de Goede36f91592016-09-07 22:20:38 +0200571 !sun4i_usb_phy0_have_vbus_det(data))
572 force_session_end = true;
573
574 /* When entering host mode (id = 0) force end the session now */
575 if (force_session_end && id_det == 0) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200576 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
577 msleep(200);
578 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
579 }
Hans de Goeded2332302015-06-13 14:37:45 +0200580 sun4i_usb_phy0_set_id_detect(phy0, id_det);
581 data->id_det = id_det;
Hans de Goede9745cee2016-09-07 22:20:37 +0200582 id_notify = true;
Hans de Goeded2332302015-06-13 14:37:45 +0200583 }
584
585 if (vbus_det != data->vbus_det) {
586 sun4i_usb_phy0_set_vbus_detect(phy0, vbus_det);
587 data->vbus_det = vbus_det;
Hans de Goede9745cee2016-09-07 22:20:37 +0200588 vbus_notify = true;
Hans de Goeded2332302015-06-13 14:37:45 +0200589 }
590
591 mutex_unlock(&phy0->mutex);
592
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200593 if (id_notify) {
Chanwoo Choi66adb882016-12-30 13:11:29 +0900594 extcon_set_state_sync(data->extcon, EXTCON_USB_HOST,
Hans de Goede1a52abe2015-06-13 14:37:46 +0200595 !id_det);
Hans de Goede36f91592016-09-07 22:20:38 +0200596 /* When leaving host mode force end the session here */
597 if (force_session_end && id_det == 1) {
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200598 mutex_lock(&phy0->mutex);
599 sun4i_usb_phy0_set_vbus_detect(phy0, 0);
600 msleep(1000);
601 sun4i_usb_phy0_set_vbus_detect(phy0, 1);
602 mutex_unlock(&phy0->mutex);
603 }
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +0800604
Paul Kocialkowskie6f32ef2019-03-14 14:05:18 +0100605 /* Enable PHY0 passby for host mode only. */
606 sun4i_usb_phy_passby(phy, !id_det);
607
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +0800608 /* Re-route PHY0 if necessary */
609 if (data->cfg->phy0_dual_route)
610 sun4i_usb_phy0_reroute(data, id_det);
Hans de Goede1aedf3a2015-06-13 14:37:50 +0200611 }
Hans de Goede1a52abe2015-06-13 14:37:46 +0200612
613 if (vbus_notify)
Chanwoo Choi66adb882016-12-30 13:11:29 +0900614 extcon_set_state_sync(data->extcon, EXTCON_USB, vbus_det);
Hans de Goede1a52abe2015-06-13 14:37:46 +0200615
Hans de Goede91d96f02016-06-29 20:14:10 +0200616 if (sun4i_usb_phy0_poll(data))
Hans de Goeded2332302015-06-13 14:37:45 +0200617 queue_delayed_work(system_wq, &data->detect, POLL_TIME);
618}
619
620static irqreturn_t sun4i_usb_phy0_id_vbus_det_irq(int irq, void *dev_id)
621{
622 struct sun4i_usb_phy_data *data = dev_id;
623
624 /* vbus or id changed, let the pins settle and then scan them */
625 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
626
627 return IRQ_HANDLED;
628}
629
Hans de Goede8665c182015-06-13 14:37:51 +0200630static int sun4i_usb_phy0_vbus_notify(struct notifier_block *nb,
631 unsigned long val, void *v)
632{
633 struct sun4i_usb_phy_data *data =
634 container_of(nb, struct sun4i_usb_phy_data, vbus_power_nb);
635 struct power_supply *psy = v;
636
637 /* Properties on the vbus_power_supply changed, scan vbus_det */
638 if (val == PSY_EVENT_PROP_CHANGED && psy == data->vbus_power_supply)
639 mod_delayed_work(system_wq, &data->detect, DEBOUNCE_TIME);
640
641 return NOTIFY_OK;
642}
643
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100644static struct phy *sun4i_usb_phy_xlate(struct device *dev,
645 struct of_phandle_args *args)
646{
647 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
648
Hans de Goede5f90d312016-09-07 22:20:39 +0200649 if (args->args[0] >= data->cfg->num_phys)
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100650 return ERR_PTR(-ENODEV);
651
Icenowy Zheng2659392e2018-10-04 20:28:47 +0800652 if (data->cfg->missing_phys & BIT(args->args[0]))
653 return ERR_PTR(-ENODEV);
654
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100655 return data->phys[args->args[0]].phy;
656}
657
Hans de Goeded2332302015-06-13 14:37:45 +0200658static int sun4i_usb_phy_remove(struct platform_device *pdev)
659{
660 struct device *dev = &pdev->dev;
661 struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
662
Hans de Goede8665c182015-06-13 14:37:51 +0200663 if (data->vbus_power_nb_registered)
664 power_supply_unreg_notifier(&data->vbus_power_nb);
Hans de Goede04e59a02016-06-18 11:31:33 +0200665 if (data->id_det_irq > 0)
Hans de Goeded2332302015-06-13 14:37:45 +0200666 devm_free_irq(dev, data->id_det_irq, data);
Hans de Goede04e59a02016-06-18 11:31:33 +0200667 if (data->vbus_det_irq > 0)
Hans de Goeded2332302015-06-13 14:37:45 +0200668 devm_free_irq(dev, data->vbus_det_irq, data);
669
670 cancel_delayed_work_sync(&data->detect);
671
672 return 0;
673}
674
Hans de Goede1a52abe2015-06-13 14:37:46 +0200675static const unsigned int sun4i_usb_phy0_cable[] = {
676 EXTCON_USB,
677 EXTCON_USB_HOST,
678 EXTCON_NONE,
679};
680
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100681static int sun4i_usb_phy_probe(struct platform_device *pdev)
682{
683 struct sun4i_usb_phy_data *data;
684 struct device *dev = &pdev->dev;
685 struct device_node *np = dev->of_node;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100686 struct phy_provider *phy_provider;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100687 struct resource *res;
Hans de Goeded2332302015-06-13 14:37:45 +0200688 int i, ret;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100689
690 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
691 if (!data)
692 return -ENOMEM;
693
Chen-Yu Tsai919ab252016-09-09 11:58:18 +0800694 spin_lock_init(&data->reg_lock);
Hans de Goeded2332302015-06-13 14:37:45 +0200695 INIT_DELAYED_WORK(&data->detect, sun4i_usb_phy0_id_vbus_det_scan);
696 dev_set_drvdata(dev, data);
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100697 data->cfg = of_device_get_match_data(dev);
698 if (!data->cfg)
699 return -EINVAL;
Hans de Goedefc1f45e2015-06-13 14:37:49 +0200700
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100701 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
702 data->base = devm_ioremap_resource(dev, res);
703 if (IS_ERR(data->base))
704 return PTR_ERR(data->base);
705
Axel Linb2dfc342015-09-25 08:47:29 +0800706 data->id_det_gpio = devm_gpiod_get_optional(dev, "usb0_id_det",
707 GPIOD_IN);
Quentin Schulze7cded22017-07-04 14:37:03 +0200708 if (IS_ERR(data->id_det_gpio)) {
709 dev_err(dev, "Couldn't request ID GPIO\n");
Axel Linb2dfc342015-09-25 08:47:29 +0800710 return PTR_ERR(data->id_det_gpio);
Quentin Schulze7cded22017-07-04 14:37:03 +0200711 }
Hans de Goeded2332302015-06-13 14:37:45 +0200712
Axel Linb2dfc342015-09-25 08:47:29 +0800713 data->vbus_det_gpio = devm_gpiod_get_optional(dev, "usb0_vbus_det",
714 GPIOD_IN);
Quentin Schulze7cded22017-07-04 14:37:03 +0200715 if (IS_ERR(data->vbus_det_gpio)) {
716 dev_err(dev, "Couldn't request VBUS detect GPIO\n");
Axel Linb2dfc342015-09-25 08:47:29 +0800717 return PTR_ERR(data->vbus_det_gpio);
Quentin Schulze7cded22017-07-04 14:37:03 +0200718 }
Hans de Goeded2332302015-06-13 14:37:45 +0200719
Hans de Goede8665c182015-06-13 14:37:51 +0200720 if (of_find_property(np, "usb0_vbus_power-supply", NULL)) {
721 data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
722 "usb0_vbus_power-supply");
Quentin Schulze7cded22017-07-04 14:37:03 +0200723 if (IS_ERR(data->vbus_power_supply)) {
724 dev_err(dev, "Couldn't get the VBUS power supply\n");
Hans de Goede8665c182015-06-13 14:37:51 +0200725 return PTR_ERR(data->vbus_power_supply);
Quentin Schulze7cded22017-07-04 14:37:03 +0200726 }
Hans de Goede8665c182015-06-13 14:37:51 +0200727
728 if (!data->vbus_power_supply)
729 return -EPROBE_DEFER;
730 }
731
Hans de Goedeb33ecca2016-08-08 21:55:39 +0200732 data->dr_mode = of_usb_get_dr_mode_by_phy(np, 0);
Hans de Goede1a52abe2015-06-13 14:37:46 +0200733
Hans de Goede5f90d312016-09-07 22:20:39 +0200734 data->extcon = devm_extcon_dev_allocate(dev, sun4i_usb_phy0_cable);
Quentin Schulze7cded22017-07-04 14:37:03 +0200735 if (IS_ERR(data->extcon)) {
736 dev_err(dev, "Couldn't allocate our extcon device\n");
Hans de Goede5f90d312016-09-07 22:20:39 +0200737 return PTR_ERR(data->extcon);
Quentin Schulze7cded22017-07-04 14:37:03 +0200738 }
Hans de Goede5f90d312016-09-07 22:20:39 +0200739
740 ret = devm_extcon_dev_register(dev, data->extcon);
741 if (ret) {
742 dev_err(dev, "failed to register extcon: %d\n", ret);
743 return ret;
Hans de Goede1a52abe2015-06-13 14:37:46 +0200744 }
745
Hans de Goede5f90d312016-09-07 22:20:39 +0200746 for (i = 0; i < data->cfg->num_phys; i++) {
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200747 struct sun4i_usb_phy *phy = data->phys + i;
748 char name[16];
749
Icenowy Zheng2659392e2018-10-04 20:28:47 +0800750 if (data->cfg->missing_phys & BIT(i))
751 continue;
752
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100753 snprintf(name, sizeof(name), "usb%d_vbus", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200754 phy->vbus = devm_regulator_get_optional(dev, name);
755 if (IS_ERR(phy->vbus)) {
Quentin Schulze7cded22017-07-04 14:37:03 +0200756 if (PTR_ERR(phy->vbus) == -EPROBE_DEFER) {
757 dev_err(dev,
758 "Couldn't get regulator %s... Deferring probe\n",
759 name);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100760 return -EPROBE_DEFER;
Quentin Schulze7cded22017-07-04 14:37:03 +0200761 }
762
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200763 phy->vbus = NULL;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100764 }
765
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100766 if (data->cfg->dedicated_clocks)
Maxime Ripardeadd4312014-05-13 17:44:18 +0200767 snprintf(name, sizeof(name), "usb%d_phy", i);
768 else
769 strlcpy(name, "usb_phy", sizeof(name));
770
771 phy->clk = devm_clk_get(dev, name);
772 if (IS_ERR(phy->clk)) {
773 dev_err(dev, "failed to get clock %s\n", name);
774 return PTR_ERR(phy->clk);
775 }
776
Chen-Yu Tsaif0152c52017-08-03 16:14:06 +0800777 /* The first PHY is always tied to OTG, and never HSIC */
778 if (data->cfg->hsic_index && i == data->cfg->hsic_index) {
779 /* HSIC needs secondary clock */
780 snprintf(name, sizeof(name), "usb%d_hsic_12M", i);
781 phy->clk2 = devm_clk_get(dev, name);
782 if (IS_ERR(phy->clk2)) {
783 dev_err(dev, "failed to get clock %s\n", name);
784 return PTR_ERR(phy->clk2);
785 }
786 }
787
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100788 snprintf(name, sizeof(name), "usb%d_reset", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200789 phy->reset = devm_reset_control_get(dev, name);
790 if (IS_ERR(phy->reset)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100791 dev_err(dev, "failed to get reset %s\n", name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200792 return PTR_ERR(phy->reset);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100793 }
794
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +0800795 if (i || data->cfg->phy0_dual_route) { /* No pmu for musb */
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100796 snprintf(name, sizeof(name), "pmu%d", i);
797 res = platform_get_resource_byname(pdev,
798 IORESOURCE_MEM, name);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200799 phy->pmu = devm_ioremap_resource(dev, res);
800 if (IS_ERR(phy->pmu))
801 return PTR_ERR(phy->pmu);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100802 }
803
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200804 phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200805 if (IS_ERR(phy->phy)) {
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100806 dev_err(dev, "failed to create PHY %d\n", i);
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200807 return PTR_ERR(phy->phy);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100808 }
809
Maxime Ripard2a7f9982014-05-13 17:44:17 +0200810 phy->index = i;
811 phy_set_drvdata(phy->phy, &data->phys[i]);
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100812 }
813
Hans de Goeded2332302015-06-13 14:37:45 +0200814 data->id_det_irq = gpiod_to_irq(data->id_det_gpio);
Quentin Schulz5cf700a2016-06-13 13:45:48 +0200815 if (data->id_det_irq > 0) {
Hans de Goeded2332302015-06-13 14:37:45 +0200816 ret = devm_request_irq(dev, data->id_det_irq,
817 sun4i_usb_phy0_id_vbus_det_irq,
818 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
819 "usb0-id-det", data);
820 if (ret) {
821 dev_err(dev, "Err requesting id-det-irq: %d\n", ret);
822 return ret;
823 }
824 }
825
Hans de Goede91d96f02016-06-29 20:14:10 +0200826 data->vbus_det_irq = gpiod_to_irq(data->vbus_det_gpio);
Quentin Schulz5cf700a2016-06-13 13:45:48 +0200827 if (data->vbus_det_irq > 0) {
Hans de Goeded2332302015-06-13 14:37:45 +0200828 ret = devm_request_irq(dev, data->vbus_det_irq,
829 sun4i_usb_phy0_id_vbus_det_irq,
830 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
831 "usb0-vbus-det", data);
832 if (ret) {
833 dev_err(dev, "Err requesting vbus-det-irq: %d\n", ret);
834 data->vbus_det_irq = -1;
835 sun4i_usb_phy_remove(pdev); /* Stop detect work */
836 return ret;
837 }
838 }
839
Hans de Goede8665c182015-06-13 14:37:51 +0200840 if (data->vbus_power_supply) {
841 data->vbus_power_nb.notifier_call = sun4i_usb_phy0_vbus_notify;
842 data->vbus_power_nb.priority = 0;
843 ret = power_supply_reg_notifier(&data->vbus_power_nb);
844 if (ret) {
845 sun4i_usb_phy_remove(pdev); /* Stop detect work */
846 return ret;
847 }
848 data->vbus_power_nb_registered = true;
849 }
850
Hans de Goeded2332302015-06-13 14:37:45 +0200851 phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
852 if (IS_ERR(phy_provider)) {
853 sun4i_usb_phy_remove(pdev); /* Stop detect work */
854 return PTR_ERR(phy_provider);
855 }
856
Quentin Schulze7cded22017-07-04 14:37:03 +0200857 dev_dbg(dev, "successfully loaded\n");
858
Hans de Goeded2332302015-06-13 14:37:45 +0200859 return 0;
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100860}
861
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100862static const struct sun4i_usb_phy_cfg sun4i_a10_cfg = {
863 .num_phys = 3,
864 .type = sun4i_a10_phy,
865 .disc_thresh = 3,
866 .phyctl_offset = REG_PHYCTL_A10,
867 .dedicated_clocks = false,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800868 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100869};
870
871static const struct sun4i_usb_phy_cfg sun5i_a13_cfg = {
872 .num_phys = 2,
873 .type = sun4i_a10_phy,
874 .disc_thresh = 2,
875 .phyctl_offset = REG_PHYCTL_A10,
876 .dedicated_clocks = false,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800877 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100878};
879
880static const struct sun4i_usb_phy_cfg sun6i_a31_cfg = {
881 .num_phys = 3,
Hans de Goede91d96f02016-06-29 20:14:10 +0200882 .type = sun6i_a31_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100883 .disc_thresh = 3,
884 .phyctl_offset = REG_PHYCTL_A10,
885 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800886 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100887};
888
889static const struct sun4i_usb_phy_cfg sun7i_a20_cfg = {
890 .num_phys = 3,
891 .type = sun4i_a10_phy,
892 .disc_thresh = 2,
893 .phyctl_offset = REG_PHYCTL_A10,
894 .dedicated_clocks = false,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800895 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100896};
897
898static const struct sun4i_usb_phy_cfg sun8i_a23_cfg = {
899 .num_phys = 2,
Chen-Yu Tsaid7119222018-01-19 17:25:41 +0800900 .type = sun6i_a31_phy,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100901 .disc_thresh = 3,
902 .phyctl_offset = REG_PHYCTL_A10,
903 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800904 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100905};
906
907static const struct sun4i_usb_phy_cfg sun8i_a33_cfg = {
908 .num_phys = 2,
909 .type = sun8i_a33_phy,
910 .disc_thresh = 3,
911 .phyctl_offset = REG_PHYCTL_A33,
912 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800913 .enable_pmu_unk1 = false,
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100914};
915
Chen-Yu Tsai4b637432017-08-03 16:14:07 +0800916static const struct sun4i_usb_phy_cfg sun8i_a83t_cfg = {
917 .num_phys = 3,
918 .hsic_index = 2,
919 .type = sun8i_a83t_phy,
920 .phyctl_offset = REG_PHYCTL_A33,
921 .dedicated_clocks = true,
922};
923
Reinder de Haan626a6302015-12-11 16:32:18 +0100924static const struct sun4i_usb_phy_cfg sun8i_h3_cfg = {
925 .num_phys = 4,
926 .type = sun8i_h3_phy,
927 .disc_thresh = 3,
Icenowy Zheng864ebdf2017-03-25 22:50:10 +0800928 .phyctl_offset = REG_PHYCTL_A33,
Reinder de Haan626a6302015-12-11 16:32:18 +0100929 .dedicated_clocks = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800930 .enable_pmu_unk1 = true,
Icenowy Zheng3ecc25e2017-03-25 22:50:11 +0800931 .phy0_dual_route = true,
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800932};
933
Icenowy Zhengf3d96f82018-01-03 16:49:44 +0800934static const struct sun4i_usb_phy_cfg sun8i_r40_cfg = {
935 .num_phys = 3,
936 .type = sun8i_r40_phy,
937 .disc_thresh = 3,
938 .phyctl_offset = REG_PHYCTL_A33,
939 .dedicated_clocks = true,
940 .enable_pmu_unk1 = true,
941 .phy0_dual_route = true,
942};
943
Icenowy Zheng16c40362017-01-03 23:25:31 +0800944static const struct sun4i_usb_phy_cfg sun8i_v3s_cfg = {
945 .num_phys = 1,
946 .type = sun8i_v3s_phy,
947 .disc_thresh = 3,
948 .phyctl_offset = REG_PHYCTL_A33,
949 .dedicated_clocks = true,
950 .enable_pmu_unk1 = true,
Icenowy Zhenga06173b2017-09-28 17:33:49 +0800951 .phy0_dual_route = true,
Icenowy Zheng16c40362017-01-03 23:25:31 +0800952};
953
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800954static const struct sun4i_usb_phy_cfg sun50i_a64_cfg = {
955 .num_phys = 2,
956 .type = sun50i_a64_phy,
957 .disc_thresh = 3,
958 .phyctl_offset = REG_PHYCTL_A33,
959 .dedicated_clocks = true,
960 .enable_pmu_unk1 = true,
Icenowy Zhengc957b7d2017-04-05 02:45:16 +0800961 .phy0_dual_route = true,
Reinder de Haan626a6302015-12-11 16:32:18 +0100962};
963
Icenowy Zhengae409cc2018-10-04 20:28:48 +0800964static const struct sun4i_usb_phy_cfg sun50i_h6_cfg = {
965 .num_phys = 4,
966 .type = sun50i_h6_phy,
967 .disc_thresh = 3,
968 .phyctl_offset = REG_PHYCTL_A33,
969 .dedicated_clocks = true,
970 .enable_pmu_unk1 = true,
971 .phy0_dual_route = true,
972 .missing_phys = BIT(1) | BIT(2),
973};
974
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100975static const struct of_device_id sun4i_usb_phy_of_match[] = {
Hans de Goede68dbc2c2015-12-11 16:32:17 +0100976 { .compatible = "allwinner,sun4i-a10-usb-phy", .data = &sun4i_a10_cfg },
977 { .compatible = "allwinner,sun5i-a13-usb-phy", .data = &sun5i_a13_cfg },
978 { .compatible = "allwinner,sun6i-a31-usb-phy", .data = &sun6i_a31_cfg },
979 { .compatible = "allwinner,sun7i-a20-usb-phy", .data = &sun7i_a20_cfg },
980 { .compatible = "allwinner,sun8i-a23-usb-phy", .data = &sun8i_a23_cfg },
981 { .compatible = "allwinner,sun8i-a33-usb-phy", .data = &sun8i_a33_cfg },
Chen-Yu Tsai4b637432017-08-03 16:14:07 +0800982 { .compatible = "allwinner,sun8i-a83t-usb-phy", .data = &sun8i_a83t_cfg },
Reinder de Haan626a6302015-12-11 16:32:18 +0100983 { .compatible = "allwinner,sun8i-h3-usb-phy", .data = &sun8i_h3_cfg },
Icenowy Zhengf3d96f82018-01-03 16:49:44 +0800984 { .compatible = "allwinner,sun8i-r40-usb-phy", .data = &sun8i_r40_cfg },
Icenowy Zheng16c40362017-01-03 23:25:31 +0800985 { .compatible = "allwinner,sun8i-v3s-usb-phy", .data = &sun8i_v3s_cfg },
Icenowy Zhengb3e0d142016-08-12 11:06:21 +0800986 { .compatible = "allwinner,sun50i-a64-usb-phy",
987 .data = &sun50i_a64_cfg},
Icenowy Zhengae409cc2018-10-04 20:28:48 +0800988 { .compatible = "allwinner,sun50i-h6-usb-phy", .data = &sun50i_h6_cfg },
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100989 { },
990};
991MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
992
993static struct platform_driver sun4i_usb_phy_driver = {
994 .probe = sun4i_usb_phy_probe,
Hans de Goeded2332302015-06-13 14:37:45 +0200995 .remove = sun4i_usb_phy_remove,
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100996 .driver = {
997 .of_match_table = sun4i_usb_phy_of_match,
998 .name = "sun4i-usb-phy",
Hans de Goedeba4bdc92014-03-01 18:09:26 +0100999 }
1000};
1001module_platform_driver(sun4i_usb_phy_driver);
1002
1003MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
1004MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
1005MODULE_LICENSE("GPL v2");