| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Renesas R-Car USB2.0 clock selector |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 6 | 7 | * Based on renesas-cpg-mssr.c |
|---|
| 7 | 8 | * |
|---|
| 8 | 9 | * Copyright (C) 2015 Glider bvba |
|---|
| 9 | | - * |
|---|
| 10 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 12 | | - * the Free Software Foundation; version 2 of the License. |
|---|
| 13 | 10 | */ |
|---|
| 14 | 11 | |
|---|
| 15 | 12 | #include <linux/clk.h> |
|---|
| 16 | 13 | #include <linux/clk-provider.h> |
|---|
| 17 | 14 | #include <linux/device.h> |
|---|
| 18 | 15 | #include <linux/init.h> |
|---|
| 16 | +#include <linux/io.h> |
|---|
| 19 | 17 | #include <linux/module.h> |
|---|
| 20 | 18 | #include <linux/of_device.h> |
|---|
| 21 | 19 | #include <linux/platform_device.h> |
|---|
| 22 | 20 | #include <linux/pm.h> |
|---|
| 23 | 21 | #include <linux/pm_runtime.h> |
|---|
| 22 | +#include <linux/reset.h> |
|---|
| 24 | 23 | #include <linux/slab.h> |
|---|
| 25 | 24 | |
|---|
| 26 | 25 | #define USB20_CLKSET0 0x00 |
|---|
| .. | .. |
|---|
| 28 | 27 | #define CLKSET0_PRIVATE BIT(0) |
|---|
| 29 | 28 | #define CLKSET0_EXTAL_ONLY (CLKSET0_INTCLK_EN | CLKSET0_PRIVATE) |
|---|
| 30 | 29 | |
|---|
| 30 | +static const struct clk_bulk_data rcar_usb2_clocks[] = { |
|---|
| 31 | + { .id = "ehci_ohci", }, |
|---|
| 32 | + { .id = "hs-usb-if", }, |
|---|
| 33 | +}; |
|---|
| 34 | + |
|---|
| 31 | 35 | struct usb2_clock_sel_priv { |
|---|
| 32 | 36 | void __iomem *base; |
|---|
| 33 | 37 | struct clk_hw hw; |
|---|
| 38 | + struct clk_bulk_data clks[ARRAY_SIZE(rcar_usb2_clocks)]; |
|---|
| 39 | + struct reset_control *rsts; |
|---|
| 34 | 40 | bool extal; |
|---|
| 35 | 41 | bool xtal; |
|---|
| 36 | 42 | }; |
|---|
| .. | .. |
|---|
| 55 | 61 | |
|---|
| 56 | 62 | static int usb2_clock_sel_enable(struct clk_hw *hw) |
|---|
| 57 | 63 | { |
|---|
| 58 | | - usb2_clock_sel_enable_extal_only(to_priv(hw)); |
|---|
| 64 | + struct usb2_clock_sel_priv *priv = to_priv(hw); |
|---|
| 65 | + int ret; |
|---|
| 66 | + |
|---|
| 67 | + ret = reset_control_deassert(priv->rsts); |
|---|
| 68 | + if (ret) |
|---|
| 69 | + return ret; |
|---|
| 70 | + |
|---|
| 71 | + ret = clk_bulk_prepare_enable(ARRAY_SIZE(priv->clks), priv->clks); |
|---|
| 72 | + if (ret) { |
|---|
| 73 | + reset_control_assert(priv->rsts); |
|---|
| 74 | + return ret; |
|---|
| 75 | + } |
|---|
| 76 | + |
|---|
| 77 | + usb2_clock_sel_enable_extal_only(priv); |
|---|
| 59 | 78 | |
|---|
| 60 | 79 | return 0; |
|---|
| 61 | 80 | } |
|---|
| 62 | 81 | |
|---|
| 63 | 82 | static void usb2_clock_sel_disable(struct clk_hw *hw) |
|---|
| 64 | 83 | { |
|---|
| 65 | | - usb2_clock_sel_disable_extal_only(to_priv(hw)); |
|---|
| 84 | + struct usb2_clock_sel_priv *priv = to_priv(hw); |
|---|
| 85 | + |
|---|
| 86 | + usb2_clock_sel_disable_extal_only(priv); |
|---|
| 87 | + |
|---|
| 88 | + clk_bulk_disable_unprepare(ARRAY_SIZE(priv->clks), priv->clks); |
|---|
| 89 | + reset_control_assert(priv->rsts); |
|---|
| 66 | 90 | } |
|---|
| 67 | 91 | |
|---|
| 68 | 92 | /* |
|---|
| .. | .. |
|---|
| 104 | 128 | static int rcar_usb2_clock_sel_remove(struct platform_device *pdev) |
|---|
| 105 | 129 | { |
|---|
| 106 | 130 | struct device *dev = &pdev->dev; |
|---|
| 107 | | - struct usb2_clock_sel_priv *priv = platform_get_drvdata(pdev); |
|---|
| 108 | 131 | |
|---|
| 109 | 132 | of_clk_del_provider(dev->of_node); |
|---|
| 110 | | - clk_hw_unregister(&priv->hw); |
|---|
| 111 | 133 | pm_runtime_put(dev); |
|---|
| 112 | 134 | pm_runtime_disable(dev); |
|---|
| 113 | 135 | |
|---|
| .. | .. |
|---|
| 119 | 141 | struct device *dev = &pdev->dev; |
|---|
| 120 | 142 | struct device_node *np = dev->of_node; |
|---|
| 121 | 143 | struct usb2_clock_sel_priv *priv; |
|---|
| 122 | | - struct resource *res; |
|---|
| 123 | 144 | struct clk *clk; |
|---|
| 124 | | - struct clk_init_data init = {}; |
|---|
| 145 | + struct clk_init_data init; |
|---|
| 146 | + int ret; |
|---|
| 125 | 147 | |
|---|
| 126 | 148 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
|---|
| 127 | 149 | if (!priv) |
|---|
| 128 | 150 | return -ENOMEM; |
|---|
| 129 | 151 | |
|---|
| 130 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 131 | | - priv->base = devm_ioremap_resource(dev, res); |
|---|
| 152 | + priv->base = devm_platform_ioremap_resource(pdev, 0); |
|---|
| 132 | 153 | if (IS_ERR(priv->base)) |
|---|
| 133 | 154 | return PTR_ERR(priv->base); |
|---|
| 134 | 155 | |
|---|
| 135 | | - pm_runtime_enable(dev); |
|---|
| 136 | | - pm_runtime_get_sync(dev); |
|---|
| 156 | + memcpy(priv->clks, rcar_usb2_clocks, sizeof(priv->clks)); |
|---|
| 157 | + ret = devm_clk_bulk_get(dev, ARRAY_SIZE(priv->clks), priv->clks); |
|---|
| 158 | + if (ret < 0) |
|---|
| 159 | + return ret; |
|---|
| 160 | + |
|---|
| 161 | + priv->rsts = devm_reset_control_array_get(dev, true, false); |
|---|
| 162 | + if (IS_ERR(priv->rsts)) |
|---|
| 163 | + return PTR_ERR(priv->rsts); |
|---|
| 137 | 164 | |
|---|
| 138 | 165 | clk = devm_clk_get(dev, "usb_extal"); |
|---|
| 139 | 166 | if (!IS_ERR(clk) && !clk_prepare_enable(clk)) { |
|---|
| .. | .. |
|---|
| 151 | 178 | return -ENOENT; |
|---|
| 152 | 179 | } |
|---|
| 153 | 180 | |
|---|
| 181 | + pm_runtime_enable(dev); |
|---|
| 182 | + pm_runtime_get_sync(dev); |
|---|
| 154 | 183 | platform_set_drvdata(pdev, priv); |
|---|
| 155 | 184 | dev_set_drvdata(dev, priv); |
|---|
| 156 | 185 | |
|---|
| .. | .. |
|---|
| 161 | 190 | init.num_parents = 0; |
|---|
| 162 | 191 | priv->hw.init = &init; |
|---|
| 163 | 192 | |
|---|
| 164 | | - clk = clk_register(NULL, &priv->hw); |
|---|
| 165 | | - if (IS_ERR(clk)) |
|---|
| 166 | | - return PTR_ERR(clk); |
|---|
| 193 | + ret = devm_clk_hw_register(dev, &priv->hw); |
|---|
| 194 | + if (ret) |
|---|
| 195 | + goto pm_put; |
|---|
| 167 | 196 | |
|---|
| 168 | | - return of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw); |
|---|
| 197 | + ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw); |
|---|
| 198 | + if (ret) |
|---|
| 199 | + goto pm_put; |
|---|
| 200 | + |
|---|
| 201 | + return 0; |
|---|
| 202 | + |
|---|
| 203 | +pm_put: |
|---|
| 204 | + pm_runtime_put(dev); |
|---|
| 205 | + pm_runtime_disable(dev); |
|---|
| 206 | + return ret; |
|---|
| 169 | 207 | } |
|---|
| 170 | 208 | |
|---|
| 171 | 209 | static const struct dev_pm_ops rcar_usb2_clock_sel_pm_ops = { |
|---|