1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Renesas R-Car USB2.0 clock selector 4 * 5 * Copyright (C) 2017 Renesas Electronics Corp. 6 * 7 * Based on renesas-cpg-mssr.c 8 * 9 * Copyright (C) 2015 Glider bvba 10 */ 11 12 #include <linux/clk.h> 13 #include <linux/clk-provider.h> 14 #include <linux/device.h> 15 #include <linux/init.h> 16 #include <linux/module.h> 17 #include <linux/of_device.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/slab.h> 22 23 #define USB20_CLKSET0 0x00 24 #define CLKSET0_INTCLK_EN BIT(11) 25 #define CLKSET0_PRIVATE BIT(0) 26 #define CLKSET0_EXTAL_ONLY (CLKSET0_INTCLK_EN | CLKSET0_PRIVATE) 27 28 struct usb2_clock_sel_priv { 29 void __iomem *base; 30 struct clk_hw hw; 31 bool extal; 32 bool xtal; 33 }; 34 #define to_priv(_hw) container_of(_hw, struct usb2_clock_sel_priv, hw) 35 36 static void usb2_clock_sel_enable_extal_only(struct usb2_clock_sel_priv *priv) 37 { 38 u16 val = readw(priv->base + USB20_CLKSET0); 39 40 pr_debug("%s: enter %d %d %x\n", __func__, 41 priv->extal, priv->xtal, val); 42 43 if (priv->extal && !priv->xtal && val != CLKSET0_EXTAL_ONLY) 44 writew(CLKSET0_EXTAL_ONLY, priv->base + USB20_CLKSET0); 45 } 46 47 static void usb2_clock_sel_disable_extal_only(struct usb2_clock_sel_priv *priv) 48 { 49 if (priv->extal && !priv->xtal) 50 writew(CLKSET0_PRIVATE, priv->base + USB20_CLKSET0); 51 } 52 53 static int usb2_clock_sel_enable(struct clk_hw *hw) 54 { 55 usb2_clock_sel_enable_extal_only(to_priv(hw)); 56 57 return 0; 58 } 59 60 static void usb2_clock_sel_disable(struct clk_hw *hw) 61 { 62 usb2_clock_sel_disable_extal_only(to_priv(hw)); 63 } 64 65 /* 66 * This module seems a mux, but this driver assumes a gate because 67 * ehci/ohci platform drivers don't support clk_set_parent() for now. 68 * If this driver acts as a gate, ehci/ohci-platform drivers don't need 69 * any modification. 70 */ 71 static const struct clk_ops usb2_clock_sel_clock_ops = { 72 .enable = usb2_clock_sel_enable, 73 .disable = usb2_clock_sel_disable, 74 }; 75 76 static const struct of_device_id rcar_usb2_clock_sel_match[] = { 77 { .compatible = "renesas,rcar-gen3-usb2-clock-sel" }, 78 { } 79 }; 80 81 static int rcar_usb2_clock_sel_suspend(struct device *dev) 82 { 83 struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev); 84 85 usb2_clock_sel_disable_extal_only(priv); 86 pm_runtime_put(dev); 87 88 return 0; 89 } 90 91 static int rcar_usb2_clock_sel_resume(struct device *dev) 92 { 93 struct usb2_clock_sel_priv *priv = dev_get_drvdata(dev); 94 95 pm_runtime_get_sync(dev); 96 usb2_clock_sel_enable_extal_only(priv); 97 98 return 0; 99 } 100 101 static int rcar_usb2_clock_sel_remove(struct platform_device *pdev) 102 { 103 struct device *dev = &pdev->dev; 104 struct usb2_clock_sel_priv *priv = platform_get_drvdata(pdev); 105 106 of_clk_del_provider(dev->of_node); 107 clk_hw_unregister(&priv->hw); 108 pm_runtime_put(dev); 109 pm_runtime_disable(dev); 110 111 return 0; 112 } 113 114 static int rcar_usb2_clock_sel_probe(struct platform_device *pdev) 115 { 116 struct device *dev = &pdev->dev; 117 struct device_node *np = dev->of_node; 118 struct usb2_clock_sel_priv *priv; 119 struct resource *res; 120 struct clk *clk; 121 struct clk_init_data init; 122 123 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 124 if (!priv) 125 return -ENOMEM; 126 127 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 128 priv->base = devm_ioremap_resource(dev, res); 129 if (IS_ERR(priv->base)) 130 return PTR_ERR(priv->base); 131 132 pm_runtime_enable(dev); 133 pm_runtime_get_sync(dev); 134 135 clk = devm_clk_get(dev, "usb_extal"); 136 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) { 137 priv->extal = !!clk_get_rate(clk); 138 clk_disable_unprepare(clk); 139 } 140 clk = devm_clk_get(dev, "usb_xtal"); 141 if (!IS_ERR(clk) && !clk_prepare_enable(clk)) { 142 priv->xtal = !!clk_get_rate(clk); 143 clk_disable_unprepare(clk); 144 } 145 146 if (!priv->extal && !priv->xtal) { 147 dev_err(dev, "This driver needs usb_extal or usb_xtal\n"); 148 return -ENOENT; 149 } 150 151 platform_set_drvdata(pdev, priv); 152 dev_set_drvdata(dev, priv); 153 154 init.name = "rcar_usb2_clock_sel"; 155 init.ops = &usb2_clock_sel_clock_ops; 156 init.flags = 0; 157 init.parent_names = NULL; 158 init.num_parents = 0; 159 priv->hw.init = &init; 160 161 clk = clk_register(NULL, &priv->hw); 162 if (IS_ERR(clk)) 163 return PTR_ERR(clk); 164 165 return of_clk_add_hw_provider(np, of_clk_hw_simple_get, &priv->hw); 166 } 167 168 static const struct dev_pm_ops rcar_usb2_clock_sel_pm_ops = { 169 .suspend = rcar_usb2_clock_sel_suspend, 170 .resume = rcar_usb2_clock_sel_resume, 171 }; 172 173 static struct platform_driver rcar_usb2_clock_sel_driver = { 174 .driver = { 175 .name = "rcar-usb2-clock-sel", 176 .of_match_table = rcar_usb2_clock_sel_match, 177 .pm = &rcar_usb2_clock_sel_pm_ops, 178 }, 179 .probe = rcar_usb2_clock_sel_probe, 180 .remove = rcar_usb2_clock_sel_remove, 181 }; 182 builtin_platform_driver(rcar_usb2_clock_sel_driver); 183 184 MODULE_DESCRIPTION("Renesas R-Car USB2 clock selector Driver"); 185 MODULE_LICENSE("GPL v2"); 186