ASoC: Intel: bytcht_es8316: Fix Irbis NB41 netbook quirk
[linux/fpc-iii.git] / drivers / usb / dwc3 / dwc3-meson-g12a.c
blob8a3ec1a951feb3173e9a6b73f81d3cc8db79a464
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Glue for Amlogic G12A SoCs
5 * Copyright (c) 2019 BayLibre, SAS
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 */
9 /*
10 * The USB is organized with a glue around the DWC3 Controller IP as :
11 * - Control registers for each USB2 Ports
12 * - Control registers for the USB PHY layer
13 * - SuperSpeed PHY can be enabled only if port is used
14 * - Dynamic OTG switching with ID change interrupt
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/clk.h>
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/bitfield.h>
26 #include <linux/bitops.h>
27 #include <linux/reset.h>
28 #include <linux/phy/phy.h>
29 #include <linux/usb/otg.h>
30 #include <linux/usb/role.h>
31 #include <linux/regulator/consumer.h>
33 /* USB2 Ports Control Registers */
35 #define U2P_REG_SIZE 0x20
37 #define U2P_R0 0x0
38 #define U2P_R0_HOST_DEVICE BIT(0)
39 #define U2P_R0_POWER_OK BIT(1)
40 #define U2P_R0_HAST_MODE BIT(2)
41 #define U2P_R0_POWER_ON_RESET BIT(3)
42 #define U2P_R0_ID_PULLUP BIT(4)
43 #define U2P_R0_DRV_VBUS BIT(5)
45 #define U2P_R1 0x4
46 #define U2P_R1_PHY_READY BIT(0)
47 #define U2P_R1_ID_DIG BIT(1)
48 #define U2P_R1_OTG_SESSION_VALID BIT(2)
49 #define U2P_R1_VBUS_VALID BIT(3)
51 /* USB Glue Control Registers */
53 #define USB_R0 0x80
54 #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
55 #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
56 #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
57 #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
58 #define USB_R0_U2D_ACT BIT(31)
60 #define USB_R1 0x84
61 #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
62 #define USB_R1_U3H_PME_ENABLE BIT(1)
63 #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(4, 2)
64 #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(9, 7)
65 #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(13, 12)
66 #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
67 #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
68 #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
69 #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
70 #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
72 #define USB_R2 0x88
73 #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
74 #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
76 #define USB_R3 0x8c
77 #define USB_R3_P30_SSC_ENABLE BIT(0)
78 #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
79 #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
80 #define USB_R3_P30_REF_SSP_EN BIT(13)
82 #define USB_R4 0x90
83 #define USB_R4_P21_PORT_RESET_0 BIT(0)
84 #define USB_R4_P21_SLEEP_M0 BIT(1)
85 #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
86 #define USB_R4_P21_ONLY BIT(4)
88 #define USB_R5 0x94
89 #define USB_R5_ID_DIG_SYNC BIT(0)
90 #define USB_R5_ID_DIG_REG BIT(1)
91 #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
92 #define USB_R5_ID_DIG_EN_0 BIT(4)
93 #define USB_R5_ID_DIG_EN_1 BIT(5)
94 #define USB_R5_ID_DIG_CURR BIT(6)
95 #define USB_R5_ID_DIG_IRQ BIT(7)
96 #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
97 #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
99 enum {
100 USB2_HOST_PHY = 0,
101 USB2_OTG_PHY,
102 USB3_HOST_PHY,
103 PHY_COUNT,
106 static const char *phy_names[PHY_COUNT] = {
107 "usb2-phy0", "usb2-phy1", "usb3-phy0",
110 struct dwc3_meson_g12a {
111 struct device *dev;
112 struct regmap *regmap;
113 struct clk *clk;
114 struct reset_control *reset;
115 struct phy *phys[PHY_COUNT];
116 enum usb_dr_mode otg_mode;
117 enum phy_mode otg_phy_mode;
118 unsigned int usb2_ports;
119 unsigned int usb3_ports;
120 struct regulator *vbus;
121 struct usb_role_switch_desc switch_desc;
122 struct usb_role_switch *role_switch;
125 static void dwc3_meson_g12a_usb2_set_mode(struct dwc3_meson_g12a *priv,
126 int i, enum phy_mode mode)
128 if (mode == PHY_MODE_USB_HOST)
129 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
130 U2P_R0_HOST_DEVICE,
131 U2P_R0_HOST_DEVICE);
132 else
133 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
134 U2P_R0_HOST_DEVICE, 0);
137 static int dwc3_meson_g12a_usb2_init(struct dwc3_meson_g12a *priv)
139 int i;
141 if (priv->otg_mode == USB_DR_MODE_PERIPHERAL)
142 priv->otg_phy_mode = PHY_MODE_USB_DEVICE;
143 else
144 priv->otg_phy_mode = PHY_MODE_USB_HOST;
146 for (i = 0 ; i < USB3_HOST_PHY ; ++i) {
147 if (!priv->phys[i])
148 continue;
150 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
151 U2P_R0_POWER_ON_RESET,
152 U2P_R0_POWER_ON_RESET);
154 if (i == USB2_OTG_PHY) {
155 regmap_update_bits(priv->regmap,
156 U2P_R0 + (U2P_REG_SIZE * i),
157 U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS,
158 U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS);
160 dwc3_meson_g12a_usb2_set_mode(priv, i,
161 priv->otg_phy_mode);
162 } else
163 dwc3_meson_g12a_usb2_set_mode(priv, i,
164 PHY_MODE_USB_HOST);
166 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
167 U2P_R0_POWER_ON_RESET, 0);
170 return 0;
173 static void dwc3_meson_g12a_usb3_init(struct dwc3_meson_g12a *priv)
175 regmap_update_bits(priv->regmap, USB_R3,
176 USB_R3_P30_SSC_RANGE_MASK |
177 USB_R3_P30_REF_SSP_EN,
178 USB_R3_P30_SSC_ENABLE |
179 FIELD_PREP(USB_R3_P30_SSC_RANGE_MASK, 2) |
180 USB_R3_P30_REF_SSP_EN);
181 udelay(2);
183 regmap_update_bits(priv->regmap, USB_R2,
184 USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK,
185 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK, 0x15));
187 regmap_update_bits(priv->regmap, USB_R2,
188 USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK,
189 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK, 0x20));
191 udelay(2);
193 regmap_update_bits(priv->regmap, USB_R1,
194 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT,
195 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT);
197 regmap_update_bits(priv->regmap, USB_R1,
198 USB_R1_P30_PCS_TX_SWING_FULL_MASK,
199 FIELD_PREP(USB_R1_P30_PCS_TX_SWING_FULL_MASK, 127));
202 static void dwc3_meson_g12a_usb_otg_apply_mode(struct dwc3_meson_g12a *priv)
204 if (priv->otg_phy_mode == PHY_MODE_USB_DEVICE) {
205 regmap_update_bits(priv->regmap, USB_R0,
206 USB_R0_U2D_ACT, USB_R0_U2D_ACT);
207 regmap_update_bits(priv->regmap, USB_R0,
208 USB_R0_U2D_SS_SCALEDOWN_MODE_MASK, 0);
209 regmap_update_bits(priv->regmap, USB_R4,
210 USB_R4_P21_SLEEP_M0, USB_R4_P21_SLEEP_M0);
211 } else {
212 regmap_update_bits(priv->regmap, USB_R0,
213 USB_R0_U2D_ACT, 0);
214 regmap_update_bits(priv->regmap, USB_R4,
215 USB_R4_P21_SLEEP_M0, 0);
219 static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a *priv)
221 int ret;
223 ret = dwc3_meson_g12a_usb2_init(priv);
224 if (ret)
225 return ret;
227 regmap_update_bits(priv->regmap, USB_R1,
228 USB_R1_U3H_FLADJ_30MHZ_REG_MASK,
229 FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20));
231 regmap_update_bits(priv->regmap, USB_R5,
232 USB_R5_ID_DIG_EN_0,
233 USB_R5_ID_DIG_EN_0);
234 regmap_update_bits(priv->regmap, USB_R5,
235 USB_R5_ID_DIG_EN_1,
236 USB_R5_ID_DIG_EN_1);
237 regmap_update_bits(priv->regmap, USB_R5,
238 USB_R5_ID_DIG_TH_MASK,
239 FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff));
241 /* If we have an actual SuperSpeed port, initialize it */
242 if (priv->usb3_ports)
243 dwc3_meson_g12a_usb3_init(priv);
245 dwc3_meson_g12a_usb_otg_apply_mode(priv);
247 return 0;
250 static const struct regmap_config phy_meson_g12a_usb3_regmap_conf = {
251 .reg_bits = 8,
252 .val_bits = 32,
253 .reg_stride = 4,
254 .max_register = USB_R5,
257 static int dwc3_meson_g12a_get_phys(struct dwc3_meson_g12a *priv)
259 int i;
261 for (i = 0 ; i < PHY_COUNT ; ++i) {
262 priv->phys[i] = devm_phy_optional_get(priv->dev, phy_names[i]);
263 if (!priv->phys[i])
264 continue;
266 if (IS_ERR(priv->phys[i]))
267 return PTR_ERR(priv->phys[i]);
269 if (i == USB3_HOST_PHY)
270 priv->usb3_ports++;
271 else
272 priv->usb2_ports++;
275 dev_info(priv->dev, "USB2 ports: %d\n", priv->usb2_ports);
276 dev_info(priv->dev, "USB3 ports: %d\n", priv->usb3_ports);
278 return 0;
281 static enum phy_mode dwc3_meson_g12a_get_id(struct dwc3_meson_g12a *priv)
283 u32 reg;
285 regmap_read(priv->regmap, USB_R5, &reg);
287 if (reg & (USB_R5_ID_DIG_SYNC | USB_R5_ID_DIG_REG))
288 return PHY_MODE_USB_DEVICE;
290 return PHY_MODE_USB_HOST;
293 static int dwc3_meson_g12a_otg_mode_set(struct dwc3_meson_g12a *priv,
294 enum phy_mode mode)
296 int ret;
298 if (!priv->phys[USB2_OTG_PHY])
299 return -EINVAL;
301 if (mode == PHY_MODE_USB_HOST)
302 dev_info(priv->dev, "switching to Host Mode\n");
303 else
304 dev_info(priv->dev, "switching to Device Mode\n");
306 if (priv->vbus) {
307 if (mode == PHY_MODE_USB_DEVICE)
308 ret = regulator_disable(priv->vbus);
309 else
310 ret = regulator_enable(priv->vbus);
311 if (ret)
312 return ret;
315 priv->otg_phy_mode = mode;
317 dwc3_meson_g12a_usb2_set_mode(priv, USB2_OTG_PHY, mode);
319 dwc3_meson_g12a_usb_otg_apply_mode(priv);
321 return 0;
324 static int dwc3_meson_g12a_role_set(struct device *dev, enum usb_role role)
326 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
327 enum phy_mode mode;
329 if (role == USB_ROLE_NONE)
330 return 0;
332 mode = (role == USB_ROLE_HOST) ? PHY_MODE_USB_HOST
333 : PHY_MODE_USB_DEVICE;
335 if (mode == priv->otg_phy_mode)
336 return 0;
338 return dwc3_meson_g12a_otg_mode_set(priv, mode);
341 static enum usb_role dwc3_meson_g12a_role_get(struct device *dev)
343 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
345 return priv->otg_phy_mode == PHY_MODE_USB_HOST ?
346 USB_ROLE_HOST : USB_ROLE_DEVICE;
349 static irqreturn_t dwc3_meson_g12a_irq_thread(int irq, void *data)
351 struct dwc3_meson_g12a *priv = data;
352 enum phy_mode otg_id;
354 otg_id = dwc3_meson_g12a_get_id(priv);
355 if (otg_id != priv->otg_phy_mode) {
356 if (dwc3_meson_g12a_otg_mode_set(priv, otg_id))
357 dev_warn(priv->dev, "Failed to switch OTG mode\n");
360 regmap_update_bits(priv->regmap, USB_R5, USB_R5_ID_DIG_IRQ, 0);
362 return IRQ_HANDLED;
365 static struct device *dwc3_meson_g12_find_child(struct device *dev,
366 const char *compatible)
368 struct platform_device *pdev;
369 struct device_node *np;
371 np = of_get_compatible_child(dev->of_node, compatible);
372 if (!np)
373 return NULL;
375 pdev = of_find_device_by_node(np);
376 of_node_put(np);
377 if (!pdev)
378 return NULL;
380 return &pdev->dev;
383 static int dwc3_meson_g12a_probe(struct platform_device *pdev)
385 struct dwc3_meson_g12a *priv;
386 struct device *dev = &pdev->dev;
387 struct device_node *np = dev->of_node;
388 void __iomem *base;
389 enum phy_mode otg_id;
390 int ret, i, irq;
392 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
393 if (!priv)
394 return -ENOMEM;
396 base = devm_platform_ioremap_resource(pdev, 0);
397 if (IS_ERR(base))
398 return PTR_ERR(base);
400 priv->regmap = devm_regmap_init_mmio(dev, base,
401 &phy_meson_g12a_usb3_regmap_conf);
402 if (IS_ERR(priv->regmap))
403 return PTR_ERR(priv->regmap);
405 priv->vbus = devm_regulator_get_optional(dev, "vbus");
406 if (IS_ERR(priv->vbus)) {
407 if (PTR_ERR(priv->vbus) == -EPROBE_DEFER)
408 return PTR_ERR(priv->vbus);
409 priv->vbus = NULL;
412 priv->clk = devm_clk_get(dev, NULL);
413 if (IS_ERR(priv->clk))
414 return PTR_ERR(priv->clk);
416 ret = clk_prepare_enable(priv->clk);
417 if (ret)
418 return ret;
420 devm_add_action_or_reset(dev,
421 (void(*)(void *))clk_disable_unprepare,
422 priv->clk);
424 platform_set_drvdata(pdev, priv);
425 priv->dev = dev;
427 priv->reset = devm_reset_control_get(dev, NULL);
428 if (IS_ERR(priv->reset)) {
429 ret = PTR_ERR(priv->reset);
430 dev_err(dev, "failed to get device reset, err=%d\n", ret);
431 return ret;
434 ret = reset_control_reset(priv->reset);
435 if (ret)
436 return ret;
438 ret = dwc3_meson_g12a_get_phys(priv);
439 if (ret)
440 return ret;
442 if (priv->vbus) {
443 ret = regulator_enable(priv->vbus);
444 if (ret)
445 return ret;
448 /* Get dr_mode */
449 priv->otg_mode = usb_get_dr_mode(dev);
451 if (priv->otg_mode == USB_DR_MODE_OTG) {
452 /* Ack irq before registering */
453 regmap_update_bits(priv->regmap, USB_R5,
454 USB_R5_ID_DIG_IRQ, 0);
456 irq = platform_get_irq(pdev, 0);
457 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
458 dwc3_meson_g12a_irq_thread,
459 IRQF_ONESHOT, pdev->name, priv);
460 if (ret)
461 return ret;
464 dwc3_meson_g12a_usb_init(priv);
466 /* Init PHYs */
467 for (i = 0 ; i < PHY_COUNT ; ++i) {
468 ret = phy_init(priv->phys[i]);
469 if (ret)
470 return ret;
473 /* Set PHY Power */
474 for (i = 0 ; i < PHY_COUNT ; ++i) {
475 ret = phy_power_on(priv->phys[i]);
476 if (ret)
477 goto err_phys_exit;
480 ret = of_platform_populate(np, NULL, NULL, dev);
481 if (ret) {
482 clk_disable_unprepare(priv->clk);
483 goto err_phys_power;
486 /* Setup OTG mode corresponding to the ID pin */
487 if (priv->otg_mode == USB_DR_MODE_OTG) {
488 otg_id = dwc3_meson_g12a_get_id(priv);
489 if (otg_id != priv->otg_phy_mode) {
490 if (dwc3_meson_g12a_otg_mode_set(priv, otg_id))
491 dev_warn(dev, "Failed to switch OTG mode\n");
495 /* Setup role switcher */
496 priv->switch_desc.usb2_port = dwc3_meson_g12_find_child(dev,
497 "snps,dwc3");
498 priv->switch_desc.udc = dwc3_meson_g12_find_child(dev, "snps,dwc2");
499 priv->switch_desc.allow_userspace_control = true;
500 priv->switch_desc.set = dwc3_meson_g12a_role_set;
501 priv->switch_desc.get = dwc3_meson_g12a_role_get;
503 priv->role_switch = usb_role_switch_register(dev, &priv->switch_desc);
504 if (IS_ERR(priv->role_switch))
505 dev_warn(dev, "Unable to register Role Switch\n");
507 pm_runtime_set_active(dev);
508 pm_runtime_enable(dev);
509 pm_runtime_get_sync(dev);
511 return 0;
513 err_phys_power:
514 for (i = 0 ; i < PHY_COUNT ; ++i)
515 phy_power_off(priv->phys[i]);
517 err_phys_exit:
518 for (i = 0 ; i < PHY_COUNT ; ++i)
519 phy_exit(priv->phys[i]);
521 return ret;
524 static int dwc3_meson_g12a_remove(struct platform_device *pdev)
526 struct dwc3_meson_g12a *priv = platform_get_drvdata(pdev);
527 struct device *dev = &pdev->dev;
528 int i;
530 usb_role_switch_unregister(priv->role_switch);
532 of_platform_depopulate(dev);
534 for (i = 0 ; i < PHY_COUNT ; ++i) {
535 phy_power_off(priv->phys[i]);
536 phy_exit(priv->phys[i]);
539 pm_runtime_disable(dev);
540 pm_runtime_put_noidle(dev);
541 pm_runtime_set_suspended(dev);
543 return 0;
546 static int __maybe_unused dwc3_meson_g12a_runtime_suspend(struct device *dev)
548 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
550 clk_disable(priv->clk);
552 return 0;
555 static int __maybe_unused dwc3_meson_g12a_runtime_resume(struct device *dev)
557 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
559 return clk_enable(priv->clk);
562 static int __maybe_unused dwc3_meson_g12a_suspend(struct device *dev)
564 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
565 int i, ret;
567 if (priv->vbus && priv->otg_phy_mode == PHY_MODE_USB_HOST) {
568 ret = regulator_disable(priv->vbus);
569 if (ret)
570 return ret;
573 for (i = 0 ; i < PHY_COUNT ; ++i) {
574 phy_power_off(priv->phys[i]);
575 phy_exit(priv->phys[i]);
578 reset_control_assert(priv->reset);
580 return 0;
583 static int __maybe_unused dwc3_meson_g12a_resume(struct device *dev)
585 struct dwc3_meson_g12a *priv = dev_get_drvdata(dev);
586 int i, ret;
588 reset_control_deassert(priv->reset);
590 dwc3_meson_g12a_usb_init(priv);
592 /* Init PHYs */
593 for (i = 0 ; i < PHY_COUNT ; ++i) {
594 ret = phy_init(priv->phys[i]);
595 if (ret)
596 return ret;
599 /* Set PHY Power */
600 for (i = 0 ; i < PHY_COUNT ; ++i) {
601 ret = phy_power_on(priv->phys[i]);
602 if (ret)
603 return ret;
606 if (priv->vbus && priv->otg_phy_mode == PHY_MODE_USB_HOST) {
607 ret = regulator_enable(priv->vbus);
608 if (ret)
609 return ret;
612 return 0;
615 static const struct dev_pm_ops dwc3_meson_g12a_dev_pm_ops = {
616 SET_SYSTEM_SLEEP_PM_OPS(dwc3_meson_g12a_suspend, dwc3_meson_g12a_resume)
617 SET_RUNTIME_PM_OPS(dwc3_meson_g12a_runtime_suspend,
618 dwc3_meson_g12a_runtime_resume, NULL)
621 static const struct of_device_id dwc3_meson_g12a_match[] = {
622 { .compatible = "amlogic,meson-g12a-usb-ctrl" },
623 { /* Sentinel */ }
625 MODULE_DEVICE_TABLE(of, dwc3_meson_g12a_match);
627 static struct platform_driver dwc3_meson_g12a_driver = {
628 .probe = dwc3_meson_g12a_probe,
629 .remove = dwc3_meson_g12a_remove,
630 .driver = {
631 .name = "dwc3-meson-g12a",
632 .of_match_table = dwc3_meson_g12a_match,
633 .pm = &dwc3_meson_g12a_dev_pm_ops,
637 module_platform_driver(dwc3_meson_g12a_driver);
638 MODULE_LICENSE("GPL v2");
639 MODULE_DESCRIPTION("Amlogic Meson G12A USB Glue Layer");
640 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");