1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas RZ/G2L USBPHY control driver
5 * Copyright (C) 2021 Renesas Electronics Corporation
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/regmap.h>
14 #include <linux/reset.h>
15 #include <linux/reset-controller.h>
20 #define RESET_SEL_PLLRESET BIT(12)
21 #define RESET_PLLRESET BIT(8)
23 #define RESET_SEL_P2RESET BIT(5)
24 #define RESET_SEL_P1RESET BIT(4)
25 #define RESET_PHYRST_2 BIT(1)
26 #define RESET_PHYRST_1 BIT(0)
28 #define PHY_RESET_PORT2 (RESET_SEL_P2RESET | RESET_PHYRST_2)
29 #define PHY_RESET_PORT1 (RESET_SEL_P1RESET | RESET_PHYRST_1)
33 struct rzg2l_usbphy_ctrl_priv {
34 struct reset_controller_dev rcdev;
35 struct reset_control *rstc;
37 struct platform_device *vdev;
42 #define rcdev_to_priv(x) container_of(x, struct rzg2l_usbphy_ctrl_priv, rcdev)
44 static int rzg2l_usbphy_ctrl_assert(struct reset_controller_dev *rcdev,
47 struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
48 u32 port_mask = PHY_RESET_PORT1 | PHY_RESET_PORT2;
49 void __iomem *base = priv->base;
53 spin_lock_irqsave(&priv->lock, flags);
54 val = readl(base + RESET);
55 val |= id ? PHY_RESET_PORT2 : PHY_RESET_PORT1;
56 if (port_mask == (val & port_mask))
57 val |= RESET_PLLRESET;
58 writel(val, base + RESET);
59 spin_unlock_irqrestore(&priv->lock, flags);
64 static int rzg2l_usbphy_ctrl_deassert(struct reset_controller_dev *rcdev,
67 struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
68 void __iomem *base = priv->base;
72 spin_lock_irqsave(&priv->lock, flags);
73 val = readl(base + RESET);
75 val |= RESET_SEL_PLLRESET;
76 val &= ~(RESET_PLLRESET | (id ? PHY_RESET_PORT2 : PHY_RESET_PORT1));
77 writel(val, base + RESET);
78 spin_unlock_irqrestore(&priv->lock, flags);
83 static int rzg2l_usbphy_ctrl_status(struct reset_controller_dev *rcdev,
86 struct rzg2l_usbphy_ctrl_priv *priv = rcdev_to_priv(rcdev);
89 port_mask = id ? PHY_RESET_PORT2 : PHY_RESET_PORT1;
91 return !!(readl(priv->base + RESET) & port_mask);
94 static const struct of_device_id rzg2l_usbphy_ctrl_match_table[] = {
95 { .compatible = "renesas,rzg2l-usbphy-ctrl" },
98 MODULE_DEVICE_TABLE(of, rzg2l_usbphy_ctrl_match_table);
100 static const struct reset_control_ops rzg2l_usbphy_ctrl_reset_ops = {
101 .assert = rzg2l_usbphy_ctrl_assert,
102 .deassert = rzg2l_usbphy_ctrl_deassert,
103 .status = rzg2l_usbphy_ctrl_status,
106 static const struct regmap_config rzg2l_usb_regconf = {
113 static int rzg2l_usbphy_ctrl_probe(struct platform_device *pdev)
115 struct device *dev = &pdev->dev;
116 struct rzg2l_usbphy_ctrl_priv *priv;
117 struct platform_device *vdev;
118 struct regmap *regmap;
123 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
127 priv->base = devm_platform_ioremap_resource(pdev, 0);
128 if (IS_ERR(priv->base))
129 return PTR_ERR(priv->base);
131 regmap = devm_regmap_init_mmio(dev, priv->base + VBENCTL, &rzg2l_usb_regconf);
133 return PTR_ERR(regmap);
135 priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
136 if (IS_ERR(priv->rstc))
137 return dev_err_probe(dev, PTR_ERR(priv->rstc),
138 "failed to get reset\n");
140 error = reset_control_deassert(priv->rstc);
144 spin_lock_init(&priv->lock);
145 dev_set_drvdata(dev, priv);
147 pm_runtime_enable(&pdev->dev);
148 error = pm_runtime_resume_and_get(&pdev->dev);
150 dev_err_probe(&pdev->dev, error, "pm_runtime_resume_and_get failed");
151 goto err_pm_disable_reset_deassert;
154 /* put pll and phy into reset state */
155 spin_lock_irqsave(&priv->lock, flags);
156 val = readl(priv->base + RESET);
157 val |= RESET_SEL_PLLRESET | RESET_PLLRESET | PHY_RESET_PORT2 | PHY_RESET_PORT1;
158 writel(val, priv->base + RESET);
159 spin_unlock_irqrestore(&priv->lock, flags);
161 priv->rcdev.ops = &rzg2l_usbphy_ctrl_reset_ops;
162 priv->rcdev.of_reset_n_cells = 1;
163 priv->rcdev.nr_resets = NUM_PORTS;
164 priv->rcdev.of_node = dev->of_node;
165 priv->rcdev.dev = dev;
167 error = devm_reset_controller_register(dev, &priv->rcdev);
169 goto err_pm_runtime_put;
171 vdev = platform_device_alloc("rzg2l-usb-vbus-regulator", pdev->id);
174 goto err_pm_runtime_put;
176 vdev->dev.parent = dev;
179 error = platform_device_add(vdev);
186 platform_device_put(vdev);
188 pm_runtime_put(&pdev->dev);
189 err_pm_disable_reset_deassert:
190 pm_runtime_disable(&pdev->dev);
191 reset_control_assert(priv->rstc);
195 static void rzg2l_usbphy_ctrl_remove(struct platform_device *pdev)
197 struct rzg2l_usbphy_ctrl_priv *priv = dev_get_drvdata(&pdev->dev);
199 platform_device_unregister(priv->vdev);
200 pm_runtime_put(&pdev->dev);
201 pm_runtime_disable(&pdev->dev);
202 reset_control_assert(priv->rstc);
205 static struct platform_driver rzg2l_usbphy_ctrl_driver = {
207 .name = "rzg2l_usbphy_ctrl",
208 .of_match_table = rzg2l_usbphy_ctrl_match_table,
210 .probe = rzg2l_usbphy_ctrl_probe,
211 .remove = rzg2l_usbphy_ctrl_remove,
213 module_platform_driver(rzg2l_usbphy_ctrl_driver);
215 MODULE_LICENSE("GPL v2");
216 MODULE_DESCRIPTION("Renesas RZ/G2L USBPHY Control");