Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: samsung: exynos-clkout: convert to module driver

The Exynos clkout driver depends on board input clock (typically XXTI or
XUSBXTI), however on Exynos4 boards these clocks were modeled as part of
SoC clocks (Exynos4 clocks driver). Obviously this is not proper, but
correcting it would break DT backward compatibility.

Both drivers - clkout and Exynos4 clocks - register the clock providers
with CLK_OF_DECLARE/OF_DECLARE_1 so their order is fragile (in the
Makefile clkout is behind Exynos4 clock). It will work only if the
Exynos4 clock driver comes up before clkout.

A change in DTS adding input clock reference to Exynos4 clocks input
PLL, see reverted commit eaf2d2f6895d ("ARM: dts: exynos: add input
clock to CMU in Exynos4412 Odroid"), caused probe reorder: the clkout
appeared before Exynos4 clock provider. Since clkout depends on Exynos4
clocks and does not support deferred probe, this did not work and caused
later failure of usb3503 USB hub probe which needs clkout:

[ 5.007442] usb3503 0-0008: unable to request refclk (-517)

The Exynos clkout driver is not a critical/core clock so there is
actually no problem in instantiating it later, as a regular module.
This removes specific probe ordering and adds support for probe
deferral.

Signed-off-by: Krzysztof Kozlowski <krzk@kernel.org>
Tested-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
Reviewed-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
Link: https://lore.kernel.org/r/20201001165646.32279-3-krzk@kernel.org

+143 -59
+143 -59
drivers/clk/samsung/clk-exynos-clkout.c
··· 9 9 #include <linux/slab.h> 10 10 #include <linux/clk.h> 11 11 #include <linux/clk-provider.h> 12 + #include <linux/module.h> 12 13 #include <linux/io.h> 13 14 #include <linux/of.h> 14 15 #include <linux/of_address.h> 15 - #include <linux/syscore_ops.h> 16 + #include <linux/of_device.h> 17 + #include <linux/platform_device.h> 18 + #include <linux/pm.h> 16 19 17 20 #define EXYNOS_CLKOUT_NR_CLKS 1 18 21 #define EXYNOS_CLKOUT_PARENTS 32 ··· 31 28 struct clk_mux mux; 32 29 spinlock_t slock; 33 30 void __iomem *reg; 31 + struct device_node *np; 34 32 u32 pmu_debug_save; 35 33 struct clk_hw_onecell_data data; 36 34 }; 37 35 38 - static struct exynos_clkout *clkout; 36 + struct exynos_clkout_variant { 37 + u32 mux_mask; 38 + }; 39 39 40 - static int exynos_clkout_suspend(void) 40 + static const struct exynos_clkout_variant exynos_clkout_exynos4 = { 41 + .mux_mask = EXYNOS4_CLKOUT_MUX_MASK, 42 + }; 43 + 44 + static const struct exynos_clkout_variant exynos_clkout_exynos5 = { 45 + .mux_mask = EXYNOS5_CLKOUT_MUX_MASK, 46 + }; 47 + 48 + static const struct of_device_id exynos_clkout_ids[] = { 49 + { 50 + .compatible = "samsung,exynos3250-pmu", 51 + .data = &exynos_clkout_exynos4, 52 + }, { 53 + .compatible = "samsung,exynos4210-pmu", 54 + .data = &exynos_clkout_exynos4, 55 + }, { 56 + .compatible = "samsung,exynos4412-pmu", 57 + .data = &exynos_clkout_exynos4, 58 + }, { 59 + .compatible = "samsung,exynos5250-pmu", 60 + .data = &exynos_clkout_exynos5, 61 + }, { 62 + .compatible = "samsung,exynos5410-pmu", 63 + .data = &exynos_clkout_exynos5, 64 + }, { 65 + .compatible = "samsung,exynos5420-pmu", 66 + .data = &exynos_clkout_exynos5, 67 + }, { 68 + .compatible = "samsung,exynos5433-pmu", 69 + .data = &exynos_clkout_exynos5, 70 + }, { } 71 + }; 72 + 73 + /* 74 + * Device will be instantiated as child of PMU device without its own 75 + * device node. Therefore match compatibles against parent. 76 + */ 77 + static int exynos_clkout_match_parent_dev(struct device *dev, u32 *mux_mask) 41 78 { 42 - clkout->pmu_debug_save = readl(clkout->reg + EXYNOS_PMU_DEBUG_REG); 79 + const struct exynos_clkout_variant *variant; 80 + const struct of_device_id *match; 81 + 82 + if (!dev->parent) { 83 + dev_err(dev, "not instantiated from MFD\n"); 84 + return -EINVAL; 85 + } 86 + 87 + match = of_match_device(exynos_clkout_ids, dev->parent); 88 + if (!match) { 89 + dev_err(dev, "cannot match parent device\n"); 90 + return -EINVAL; 91 + } 92 + variant = match->data; 93 + 94 + *mux_mask = variant->mux_mask; 43 95 44 96 return 0; 45 97 } 46 98 47 - static void exynos_clkout_resume(void) 48 - { 49 - writel(clkout->pmu_debug_save, clkout->reg + EXYNOS_PMU_DEBUG_REG); 50 - } 51 - 52 - static struct syscore_ops exynos_clkout_syscore_ops = { 53 - .suspend = exynos_clkout_suspend, 54 - .resume = exynos_clkout_resume, 55 - }; 56 - 57 - static void __init exynos_clkout_init(struct device_node *node, u32 mux_mask) 99 + static int exynos_clkout_probe(struct platform_device *pdev) 58 100 { 59 101 const char *parent_names[EXYNOS_CLKOUT_PARENTS]; 60 102 struct clk *parents[EXYNOS_CLKOUT_PARENTS]; 61 - int parent_count; 62 - int ret; 63 - int i; 103 + struct exynos_clkout *clkout; 104 + int parent_count, ret, i; 105 + u32 mux_mask; 64 106 65 - clkout = kzalloc(struct_size(clkout, data.hws, EXYNOS_CLKOUT_NR_CLKS), 66 - GFP_KERNEL); 107 + clkout = devm_kzalloc(&pdev->dev, 108 + struct_size(clkout, data.hws, EXYNOS_CLKOUT_NR_CLKS), 109 + GFP_KERNEL); 67 110 if (!clkout) 68 - return; 111 + return -ENOMEM; 112 + 113 + ret = exynos_clkout_match_parent_dev(&pdev->dev, &mux_mask); 114 + if (ret) 115 + return ret; 116 + 117 + clkout->np = pdev->dev.of_node; 118 + if (!clkout->np) { 119 + /* 120 + * pdev->dev.parent was checked by exynos_clkout_match_parent_dev() 121 + * so it is not NULL. 122 + */ 123 + clkout->np = pdev->dev.parent->of_node; 124 + } 125 + 126 + platform_set_drvdata(pdev, clkout); 69 127 70 128 spin_lock_init(&clkout->slock); 71 129 ··· 135 71 char name[] = "clkoutXX"; 136 72 137 73 snprintf(name, sizeof(name), "clkout%d", i); 138 - parents[i] = of_clk_get_by_name(node, name); 74 + parents[i] = of_clk_get_by_name(clkout->np, name); 139 75 if (IS_ERR(parents[i])) { 140 76 parent_names[i] = "none"; 141 77 continue; ··· 146 82 } 147 83 148 84 if (!parent_count) 149 - goto free_clkout; 85 + return -EINVAL; 150 86 151 - clkout->reg = of_iomap(node, 0); 152 - if (!clkout->reg) 87 + clkout->reg = of_iomap(clkout->np, 0); 88 + if (!clkout->reg) { 89 + ret = -ENODEV; 153 90 goto clks_put; 91 + } 154 92 155 93 clkout->gate.reg = clkout->reg + EXYNOS_PMU_DEBUG_REG; 156 94 clkout->gate.bit_idx = EXYNOS_CLKOUT_DISABLE_SHIFT; ··· 169 103 &clk_mux_ops, NULL, NULL, &clkout->gate.hw, 170 104 &clk_gate_ops, CLK_SET_RATE_PARENT 171 105 | CLK_SET_RATE_NO_REPARENT); 172 - if (IS_ERR(clkout->data.hws[0])) 106 + if (IS_ERR(clkout->data.hws[0])) { 107 + ret = PTR_ERR(clkout->data.hws[0]); 173 108 goto err_unmap; 109 + } 174 110 175 111 clkout->data.num = EXYNOS_CLKOUT_NR_CLKS; 176 - ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, &clkout->data); 112 + ret = of_clk_add_hw_provider(clkout->np, of_clk_hw_onecell_get, &clkout->data); 177 113 if (ret) 178 114 goto err_clk_unreg; 179 115 180 - register_syscore_ops(&exynos_clkout_syscore_ops); 181 - 182 - return; 116 + return 0; 183 117 184 118 err_clk_unreg: 185 119 clk_hw_unregister(clkout->data.hws[0]); ··· 189 123 for (i = 0; i < EXYNOS_CLKOUT_PARENTS; ++i) 190 124 if (!IS_ERR(parents[i])) 191 125 clk_put(parents[i]); 192 - free_clkout: 193 - kfree(clkout); 194 126 195 - pr_err("%s: failed to register clkout clock\n", __func__); 127 + dev_err(&pdev->dev, "failed to register clkout clock\n"); 128 + 129 + return ret; 196 130 } 197 131 198 - /* 199 - * We use CLK_OF_DECLARE_DRIVER initialization method to avoid setting 200 - * the OF_POPULATED flag on the pmu device tree node, so later the 201 - * Exynos PMU platform device can be properly probed with PMU driver. 202 - */ 203 - 204 - static void __init exynos4_clkout_init(struct device_node *node) 132 + static int exynos_clkout_remove(struct platform_device *pdev) 205 133 { 206 - exynos_clkout_init(node, EXYNOS4_CLKOUT_MUX_MASK); 207 - } 208 - CLK_OF_DECLARE_DRIVER(exynos4210_clkout, "samsung,exynos4210-pmu", 209 - exynos4_clkout_init); 210 - CLK_OF_DECLARE_DRIVER(exynos4412_clkout, "samsung,exynos4412-pmu", 211 - exynos4_clkout_init); 212 - CLK_OF_DECLARE_DRIVER(exynos3250_clkout, "samsung,exynos3250-pmu", 213 - exynos4_clkout_init); 134 + struct exynos_clkout *clkout = platform_get_drvdata(pdev); 214 135 215 - static void __init exynos5_clkout_init(struct device_node *node) 216 - { 217 - exynos_clkout_init(node, EXYNOS5_CLKOUT_MUX_MASK); 136 + of_clk_del_provider(clkout->np); 137 + clk_hw_unregister(clkout->data.hws[0]); 138 + iounmap(clkout->reg); 139 + 140 + return 0; 218 141 } 219 - CLK_OF_DECLARE_DRIVER(exynos5250_clkout, "samsung,exynos5250-pmu", 220 - exynos5_clkout_init); 221 - CLK_OF_DECLARE_DRIVER(exynos5410_clkout, "samsung,exynos5410-pmu", 222 - exynos5_clkout_init); 223 - CLK_OF_DECLARE_DRIVER(exynos5420_clkout, "samsung,exynos5420-pmu", 224 - exynos5_clkout_init); 225 - CLK_OF_DECLARE_DRIVER(exynos5433_clkout, "samsung,exynos5433-pmu", 226 - exynos5_clkout_init); 142 + 143 + static int exynos_clkout_suspend(struct device *dev) 144 + { 145 + struct exynos_clkout *clkout = dev_get_drvdata(dev); 146 + 147 + clkout->pmu_debug_save = readl(clkout->reg + EXYNOS_PMU_DEBUG_REG); 148 + 149 + return 0; 150 + } 151 + 152 + static int exynos_clkout_resume(struct device *dev) 153 + { 154 + struct exynos_clkout *clkout = dev_get_drvdata(dev); 155 + 156 + writel(clkout->pmu_debug_save, clkout->reg + EXYNOS_PMU_DEBUG_REG); 157 + 158 + return 0; 159 + } 160 + 161 + static SIMPLE_DEV_PM_OPS(exynos_clkout_pm_ops, exynos_clkout_suspend, 162 + exynos_clkout_resume); 163 + 164 + static struct platform_driver exynos_clkout_driver = { 165 + .driver = { 166 + .name = "exynos-clkout", 167 + .of_match_table = exynos_clkout_ids, 168 + .pm = &exynos_clkout_pm_ops, 169 + }, 170 + .probe = exynos_clkout_probe, 171 + .remove = exynos_clkout_remove, 172 + }; 173 + module_platform_driver(exynos_clkout_driver); 174 + 175 + MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>"); 176 + MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>"); 177 + MODULE_DESCRIPTION("Samsung Exynos clock output driver"); 178 + MODULE_LICENSE("GPL");