1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * DRA7 ATL (Audio Tracking Logic) clock driver
4 *
5 * Copyright (C) 2013 Texas Instruments, Inc.
6 *
7 * Peter Ujfalusi <peter.ujfalusi@ti.com>
8 */
9
10 #include <linux/init.h>
11 #include <linux/clk.h>
12 #include <linux/clk-provider.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/clk/ti.h>
20
21 #include "clock.h"
22
23 #define DRA7_ATL_INSTANCES 4
24
25 #define DRA7_ATL_PPMR_REG(id) (0x200 + (id * 0x80))
26 #define DRA7_ATL_BBSR_REG(id) (0x204 + (id * 0x80))
27 #define DRA7_ATL_ATLCR_REG(id) (0x208 + (id * 0x80))
28 #define DRA7_ATL_SWEN_REG(id) (0x210 + (id * 0x80))
29 #define DRA7_ATL_BWSMUX_REG(id) (0x214 + (id * 0x80))
30 #define DRA7_ATL_AWSMUX_REG(id) (0x218 + (id * 0x80))
31 #define DRA7_ATL_PCLKMUX_REG(id) (0x21c + (id * 0x80))
32
33 #define DRA7_ATL_SWEN BIT(0)
34 #define DRA7_ATL_DIVIDER_MASK (0x1f)
35 #define DRA7_ATL_PCLKMUX BIT(0)
36 struct dra7_atl_clock_info;
37
38 struct dra7_atl_desc {
39 struct clk *clk;
40 struct clk_hw hw;
41 struct dra7_atl_clock_info *cinfo;
42 int id;
43
44 bool probed; /* the driver for the IP has been loaded */
45 bool valid; /* configured */
46 bool enabled;
47 u32 bws; /* Baseband Word Select Mux */
48 u32 aws; /* Audio Word Select Mux */
49 u32 divider; /* Cached divider value */
50 };
51
52 struct dra7_atl_clock_info {
53 struct device *dev;
54 void __iomem *iobase;
55
56 struct dra7_atl_desc *cdesc;
57 };
58
59 #define to_atl_desc(_hw) container_of(_hw, struct dra7_atl_desc, hw)
60
atl_write(struct dra7_atl_clock_info * cinfo,u32 reg,u32 val)61 static inline void atl_write(struct dra7_atl_clock_info *cinfo, u32 reg,
62 u32 val)
63 {
64 __raw_writel(val, cinfo->iobase + reg);
65 }
66
atl_read(struct dra7_atl_clock_info * cinfo,u32 reg)67 static inline int atl_read(struct dra7_atl_clock_info *cinfo, u32 reg)
68 {
69 return __raw_readl(cinfo->iobase + reg);
70 }
71
atl_clk_enable(struct clk_hw * hw)72 static int atl_clk_enable(struct clk_hw *hw)
73 {
74 struct dra7_atl_desc *cdesc = to_atl_desc(hw);
75
76 if (!cdesc->probed)
77 goto out;
78
79 if (unlikely(!cdesc->valid))
80 dev_warn(cdesc->cinfo->dev, "atl%d has not been configured\n",
81 cdesc->id);
82 pm_runtime_get_sync(cdesc->cinfo->dev);
83
84 atl_write(cdesc->cinfo, DRA7_ATL_ATLCR_REG(cdesc->id),
85 cdesc->divider - 1);
86 atl_write(cdesc->cinfo, DRA7_ATL_SWEN_REG(cdesc->id), DRA7_ATL_SWEN);
87
88 out:
89 cdesc->enabled = true;
90
91 return 0;
92 }
93
atl_clk_disable(struct clk_hw * hw)94 static void atl_clk_disable(struct clk_hw *hw)
95 {
96 struct dra7_atl_desc *cdesc = to_atl_desc(hw);
97
98 if (!cdesc->probed)
99 goto out;
100
101 atl_write(cdesc->cinfo, DRA7_ATL_SWEN_REG(cdesc->id), 0);
102 pm_runtime_put_sync(cdesc->cinfo->dev);
103
104 out:
105 cdesc->enabled = false;
106 }
107
atl_clk_is_enabled(struct clk_hw * hw)108 static int atl_clk_is_enabled(struct clk_hw *hw)
109 {
110 struct dra7_atl_desc *cdesc = to_atl_desc(hw);
111
112 return cdesc->enabled;
113 }
114
atl_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)115 static unsigned long atl_clk_recalc_rate(struct clk_hw *hw,
116 unsigned long parent_rate)
117 {
118 struct dra7_atl_desc *cdesc = to_atl_desc(hw);
119
120 return parent_rate / cdesc->divider;
121 }
122
atl_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)123 static long atl_clk_round_rate(struct clk_hw *hw, unsigned long rate,
124 unsigned long *parent_rate)
125 {
126 unsigned divider;
127
128 divider = (*parent_rate + rate / 2) / rate;
129 if (divider > DRA7_ATL_DIVIDER_MASK + 1)
130 divider = DRA7_ATL_DIVIDER_MASK + 1;
131
132 return *parent_rate / divider;
133 }
134
atl_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)135 static int atl_clk_set_rate(struct clk_hw *hw, unsigned long rate,
136 unsigned long parent_rate)
137 {
138 struct dra7_atl_desc *cdesc;
139 u32 divider;
140
141 if (!hw || !rate)
142 return -EINVAL;
143
144 cdesc = to_atl_desc(hw);
145 divider = ((parent_rate + rate / 2) / rate) - 1;
146 if (divider > DRA7_ATL_DIVIDER_MASK)
147 divider = DRA7_ATL_DIVIDER_MASK;
148
149 cdesc->divider = divider + 1;
150
151 return 0;
152 }
153
154 static const struct clk_ops atl_clk_ops = {
155 .enable = atl_clk_enable,
156 .disable = atl_clk_disable,
157 .is_enabled = atl_clk_is_enabled,
158 .recalc_rate = atl_clk_recalc_rate,
159 .round_rate = atl_clk_round_rate,
160 .set_rate = atl_clk_set_rate,
161 };
162
of_dra7_atl_clock_setup(struct device_node * node)163 static void __init of_dra7_atl_clock_setup(struct device_node *node)
164 {
165 struct dra7_atl_desc *clk_hw = NULL;
166 struct clk_init_data init = { NULL };
167 const char **parent_names = NULL;
168 const char *name;
169 struct clk *clk;
170
171 clk_hw = kzalloc(sizeof(*clk_hw), GFP_KERNEL);
172 if (!clk_hw) {
173 pr_err("%s: could not allocate dra7_atl_desc\n", __func__);
174 return;
175 }
176
177 clk_hw->hw.init = &init;
178 clk_hw->divider = 1;
179 name = ti_dt_clk_name(node);
180 init.name = name;
181 init.ops = &atl_clk_ops;
182 init.flags = CLK_IGNORE_UNUSED;
183 init.num_parents = of_clk_get_parent_count(node);
184
185 if (init.num_parents != 1) {
186 pr_err("%s: atl clock %pOFn must have 1 parent\n", __func__,
187 node);
188 goto cleanup;
189 }
190
191 parent_names = kzalloc(sizeof(char *), GFP_KERNEL);
192
193 if (!parent_names)
194 goto cleanup;
195
196 parent_names[0] = of_clk_get_parent_name(node, 0);
197
198 init.parent_names = parent_names;
199
200 clk = of_ti_clk_register(node, &clk_hw->hw, name);
201
202 if (!IS_ERR(clk)) {
203 of_clk_add_provider(node, of_clk_src_simple_get, clk);
204 kfree(parent_names);
205 return;
206 }
207 cleanup:
208 kfree(parent_names);
209 kfree(clk_hw);
210 }
211 CLK_OF_DECLARE(dra7_atl_clock, "ti,dra7-atl-clock", of_dra7_atl_clock_setup);
212
of_dra7_atl_clk_probe(struct platform_device * pdev)213 static int of_dra7_atl_clk_probe(struct platform_device *pdev)
214 {
215 struct device_node *node = pdev->dev.of_node;
216 struct dra7_atl_clock_info *cinfo;
217 int i;
218 int ret = 0;
219
220 if (!node)
221 return -ENODEV;
222
223 cinfo = devm_kzalloc(&pdev->dev, sizeof(*cinfo), GFP_KERNEL);
224 if (!cinfo)
225 return -ENOMEM;
226
227 cinfo->iobase = of_iomap(node, 0);
228 cinfo->dev = &pdev->dev;
229 pm_runtime_enable(cinfo->dev);
230
231 pm_runtime_get_sync(cinfo->dev);
232 atl_write(cinfo, DRA7_ATL_PCLKMUX_REG(0), DRA7_ATL_PCLKMUX);
233
234 for (i = 0; i < DRA7_ATL_INSTANCES; i++) {
235 struct device_node *cfg_node;
236 char prop[5];
237 struct dra7_atl_desc *cdesc;
238 struct of_phandle_args clkspec;
239 struct clk *clk;
240 int rc;
241
242 rc = of_parse_phandle_with_args(node, "ti,provided-clocks",
243 NULL, i, &clkspec);
244
245 if (rc) {
246 pr_err("%s: failed to lookup atl clock %d\n", __func__,
247 i);
248 ret = -EINVAL;
249 goto pm_put;
250 }
251
252 clk = of_clk_get_from_provider(&clkspec);
253 if (IS_ERR(clk)) {
254 pr_err("%s: failed to get atl clock %d from provider\n",
255 __func__, i);
256 ret = PTR_ERR(clk);
257 goto pm_put;
258 }
259
260 cdesc = to_atl_desc(__clk_get_hw(clk));
261 cdesc->cinfo = cinfo;
262 cdesc->id = i;
263
264 /* Get configuration for the ATL instances */
265 snprintf(prop, sizeof(prop), "atl%u", i);
266 cfg_node = of_get_child_by_name(node, prop);
267 if (cfg_node) {
268 ret = of_property_read_u32(cfg_node, "bws",
269 &cdesc->bws);
270 ret |= of_property_read_u32(cfg_node, "aws",
271 &cdesc->aws);
272 if (!ret) {
273 cdesc->valid = true;
274 atl_write(cinfo, DRA7_ATL_BWSMUX_REG(i),
275 cdesc->bws);
276 atl_write(cinfo, DRA7_ATL_AWSMUX_REG(i),
277 cdesc->aws);
278 }
279 of_node_put(cfg_node);
280 }
281
282 cdesc->probed = true;
283 /*
284 * Enable the clock if it has been asked prior to loading the
285 * hw driver
286 */
287 if (cdesc->enabled)
288 atl_clk_enable(__clk_get_hw(clk));
289 }
290
291 pm_put:
292 pm_runtime_put_sync(cinfo->dev);
293 return ret;
294 }
295
296 static const struct of_device_id of_dra7_atl_clk_match_tbl[] = {
297 { .compatible = "ti,dra7-atl", },
298 {},
299 };
300
301 static struct platform_driver dra7_atl_clk_driver = {
302 .driver = {
303 .name = "dra7-atl",
304 .suppress_bind_attrs = true,
305 .of_match_table = of_dra7_atl_clk_match_tbl,
306 },
307 .probe = of_dra7_atl_clk_probe,
308 };
309 builtin_platform_driver(dra7_atl_clk_driver);
310