1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4  * Author: Tomasz Figa <t.figa@samsung.com>
5  *
6  * Clock driver for Exynos clock output
7  */
8 
9 #include <linux/slab.h>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/module.h>
13 #include <linux/io.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm.h>
19 
20 #define EXYNOS_CLKOUT_NR_CLKS		1
21 #define EXYNOS_CLKOUT_PARENTS		32
22 
23 #define EXYNOS_PMU_DEBUG_REG		0xa00
24 #define EXYNOS_CLKOUT_DISABLE_SHIFT	0
25 #define EXYNOS_CLKOUT_MUX_SHIFT		8
26 #define EXYNOS4_CLKOUT_MUX_MASK		0xf
27 #define EXYNOS5_CLKOUT_MUX_MASK		0x1f
28 
29 struct exynos_clkout {
30 	struct clk_gate gate;
31 	struct clk_mux mux;
32 	spinlock_t slock;
33 	void __iomem *reg;
34 	struct device_node *np;
35 	u32 pmu_debug_save;
36 	struct clk_hw_onecell_data data;
37 };
38 
39 struct exynos_clkout_variant {
40 	u32 mux_mask;
41 };
42 
43 static const struct exynos_clkout_variant exynos_clkout_exynos4 = {
44 	.mux_mask	= EXYNOS4_CLKOUT_MUX_MASK,
45 };
46 
47 static const struct exynos_clkout_variant exynos_clkout_exynos5 = {
48 	.mux_mask	= EXYNOS5_CLKOUT_MUX_MASK,
49 };
50 
51 static const struct of_device_id exynos_clkout_ids[] = {
52 	{
53 		.compatible = "samsung,exynos3250-pmu",
54 		.data = &exynos_clkout_exynos4,
55 	}, {
56 		.compatible = "samsung,exynos4210-pmu",
57 		.data = &exynos_clkout_exynos4,
58 	}, {
59 		.compatible = "samsung,exynos4412-pmu",
60 		.data = &exynos_clkout_exynos4,
61 	}, {
62 		.compatible = "samsung,exynos5250-pmu",
63 		.data = &exynos_clkout_exynos5,
64 	}, {
65 		.compatible = "samsung,exynos5410-pmu",
66 		.data = &exynos_clkout_exynos5,
67 	}, {
68 		.compatible = "samsung,exynos5420-pmu",
69 		.data = &exynos_clkout_exynos5,
70 	}, {
71 		.compatible = "samsung,exynos5433-pmu",
72 		.data = &exynos_clkout_exynos5,
73 	}, { }
74 };
75 MODULE_DEVICE_TABLE(of, exynos_clkout_ids);
76 
77 /*
78  * Device will be instantiated as child of PMU device without its own
79  * device node.  Therefore match compatibles against parent.
80  */
81 static int exynos_clkout_match_parent_dev(struct device *dev, u32 *mux_mask)
82 {
83 	const struct exynos_clkout_variant *variant;
84 
85 	if (!dev->parent) {
86 		dev_err(dev, "not instantiated from MFD\n");
87 		return -EINVAL;
88 	}
89 
90 	variant = of_device_get_match_data(dev->parent);
91 	if (!variant) {
92 		dev_err(dev, "cannot match parent device\n");
93 		return -EINVAL;
94 	}
95 
96 	*mux_mask = variant->mux_mask;
97 
98 	return 0;
99 }
100 
101 static int exynos_clkout_probe(struct platform_device *pdev)
102 {
103 	const char *parent_names[EXYNOS_CLKOUT_PARENTS];
104 	struct clk *parents[EXYNOS_CLKOUT_PARENTS];
105 	struct exynos_clkout *clkout;
106 	int parent_count, ret, i;
107 	u32 mux_mask;
108 
109 	clkout = devm_kzalloc(&pdev->dev,
110 			      struct_size(clkout, data.hws, EXYNOS_CLKOUT_NR_CLKS),
111 			      GFP_KERNEL);
112 	if (!clkout)
113 		return -ENOMEM;
114 
115 	ret = exynos_clkout_match_parent_dev(&pdev->dev, &mux_mask);
116 	if (ret)
117 		return ret;
118 
119 	clkout->np = pdev->dev.of_node;
120 	if (!clkout->np) {
121 		/*
122 		 * pdev->dev.parent was checked by exynos_clkout_match_parent_dev()
123 		 * so it is not NULL.
124 		 */
125 		clkout->np = pdev->dev.parent->of_node;
126 	}
127 
128 	platform_set_drvdata(pdev, clkout);
129 
130 	spin_lock_init(&clkout->slock);
131 
132 	parent_count = 0;
133 	for (i = 0; i < EXYNOS_CLKOUT_PARENTS; ++i) {
134 		char name[] = "clkoutXX";
135 
136 		snprintf(name, sizeof(name), "clkout%d", i);
137 		parents[i] = of_clk_get_by_name(clkout->np, name);
138 		if (IS_ERR(parents[i])) {
139 			parent_names[i] = "none";
140 			continue;
141 		}
142 
143 		parent_names[i] = __clk_get_name(parents[i]);
144 		parent_count = i + 1;
145 	}
146 
147 	if (!parent_count)
148 		return -EINVAL;
149 
150 	clkout->reg = of_iomap(clkout->np, 0);
151 	if (!clkout->reg) {
152 		ret = -ENODEV;
153 		goto clks_put;
154 	}
155 
156 	clkout->gate.reg = clkout->reg + EXYNOS_PMU_DEBUG_REG;
157 	clkout->gate.bit_idx = EXYNOS_CLKOUT_DISABLE_SHIFT;
158 	clkout->gate.flags = CLK_GATE_SET_TO_DISABLE;
159 	clkout->gate.lock = &clkout->slock;
160 
161 	clkout->mux.reg = clkout->reg + EXYNOS_PMU_DEBUG_REG;
162 	clkout->mux.mask = mux_mask;
163 	clkout->mux.shift = EXYNOS_CLKOUT_MUX_SHIFT;
164 	clkout->mux.lock = &clkout->slock;
165 
166 	clkout->data.hws[0] = clk_hw_register_composite(NULL, "clkout",
167 				parent_names, parent_count, &clkout->mux.hw,
168 				&clk_mux_ops, NULL, NULL, &clkout->gate.hw,
169 				&clk_gate_ops, CLK_SET_RATE_PARENT
170 				| CLK_SET_RATE_NO_REPARENT);
171 	if (IS_ERR(clkout->data.hws[0])) {
172 		ret = PTR_ERR(clkout->data.hws[0]);
173 		goto err_unmap;
174 	}
175 
176 	clkout->data.num = EXYNOS_CLKOUT_NR_CLKS;
177 	ret = of_clk_add_hw_provider(clkout->np, of_clk_hw_onecell_get, &clkout->data);
178 	if (ret)
179 		goto err_clk_unreg;
180 
181 	return 0;
182 
183 err_clk_unreg:
184 	clk_hw_unregister(clkout->data.hws[0]);
185 err_unmap:
186 	iounmap(clkout->reg);
187 clks_put:
188 	for (i = 0; i < EXYNOS_CLKOUT_PARENTS; ++i)
189 		if (!IS_ERR(parents[i]))
190 			clk_put(parents[i]);
191 
192 	dev_err(&pdev->dev, "failed to register clkout clock\n");
193 
194 	return ret;
195 }
196 
197 static int exynos_clkout_remove(struct platform_device *pdev)
198 {
199 	struct exynos_clkout *clkout = platform_get_drvdata(pdev);
200 
201 	of_clk_del_provider(clkout->np);
202 	clk_hw_unregister(clkout->data.hws[0]);
203 	iounmap(clkout->reg);
204 
205 	return 0;
206 }
207 
208 static int __maybe_unused exynos_clkout_suspend(struct device *dev)
209 {
210 	struct exynos_clkout *clkout = dev_get_drvdata(dev);
211 
212 	clkout->pmu_debug_save = readl(clkout->reg + EXYNOS_PMU_DEBUG_REG);
213 
214 	return 0;
215 }
216 
217 static int __maybe_unused exynos_clkout_resume(struct device *dev)
218 {
219 	struct exynos_clkout *clkout = dev_get_drvdata(dev);
220 
221 	writel(clkout->pmu_debug_save, clkout->reg + EXYNOS_PMU_DEBUG_REG);
222 
223 	return 0;
224 }
225 
226 static SIMPLE_DEV_PM_OPS(exynos_clkout_pm_ops, exynos_clkout_suspend,
227 			 exynos_clkout_resume);
228 
229 static struct platform_driver exynos_clkout_driver = {
230 	.driver = {
231 		.name = "exynos-clkout",
232 		.of_match_table = exynos_clkout_ids,
233 		.pm = &exynos_clkout_pm_ops,
234 	},
235 	.probe = exynos_clkout_probe,
236 	.remove = exynos_clkout_remove,
237 };
238 module_platform_driver(exynos_clkout_driver);
239 
240 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
241 MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
242 MODULE_DESCRIPTION("Samsung Exynos clock output driver");
243 MODULE_LICENSE("GPL");
244