blob: 419d4515acc6c240299d75572ffdab8af3cf0656 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass36ad2342015-06-23 15:39:15 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Stephen Warrena9622432016-06-17 09:44:00 -06005 * Copyright (c) 2016, NVIDIA CORPORATION.
Philipp Tomsich9cf03b02018-01-08 13:59:18 +01006 * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH
Simon Glass36ad2342015-06-23 15:39:15 -06007 */
8
9#include <common.h>
10#include <clk.h>
Stephen Warrena9622432016-06-17 09:44:00 -060011#include <clk-uclass.h>
Simon Glass36ad2342015-06-23 15:39:15 -060012#include <dm.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010013#include <dm/read.h>
Simon Glass589d9152016-07-04 11:58:03 -060014#include <dt-structs.h>
Simon Glass36ad2342015-06-23 15:39:15 -060015#include <errno.h>
Simon Glass36ad2342015-06-23 15:39:15 -060016
Mario Six799fe562018-01-15 11:06:51 +010017static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glass36ad2342015-06-23 15:39:15 -060018{
Mario Six799fe562018-01-15 11:06:51 +010019 return (const struct clk_ops *)dev->driver->ops;
Simon Glass36ad2342015-06-23 15:39:15 -060020}
21
Stephen Warrena9622432016-06-17 09:44:00 -060022#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass589d9152016-07-04 11:58:03 -060023# if CONFIG_IS_ENABLED(OF_PLATDATA)
24int clk_get_by_index_platdata(struct udevice *dev, int index,
Simon Glasse94414b2017-08-29 14:15:56 -060025 struct phandle_1_arg *cells, struct clk *clk)
Simon Glass589d9152016-07-04 11:58:03 -060026{
27 int ret;
28
29 if (index != 0)
30 return -ENOSYS;
31 ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
32 if (ret)
33 return ret;
Simon Glassfdec5802017-08-29 14:15:58 -060034 clk->id = cells[0].arg[0];
Simon Glass589d9152016-07-04 11:58:03 -060035
36 return 0;
37}
38# else
Stephen Warrena9622432016-06-17 09:44:00 -060039static int clk_of_xlate_default(struct clk *clk,
Simon Glassb7ae2772017-05-18 20:09:40 -060040 struct ofnode_phandle_args *args)
Simon Glass36ad2342015-06-23 15:39:15 -060041{
Stephen Warrena9622432016-06-17 09:44:00 -060042 debug("%s(clk=%p)\n", __func__, clk);
Simon Glass36ad2342015-06-23 15:39:15 -060043
Stephen Warrena9622432016-06-17 09:44:00 -060044 if (args->args_count > 1) {
45 debug("Invaild args_count: %d\n", args->args_count);
46 return -EINVAL;
47 }
Simon Glass36ad2342015-06-23 15:39:15 -060048
Stephen Warrena9622432016-06-17 09:44:00 -060049 if (args->args_count)
50 clk->id = args->args[0];
51 else
52 clk->id = 0;
Simon Glass36ad2342015-06-23 15:39:15 -060053
Stephen Warrena9622432016-06-17 09:44:00 -060054 return 0;
Simon Glass36ad2342015-06-23 15:39:15 -060055}
Simon Glass0342bd22016-01-20 19:43:02 -070056
Philipp Tomsichf7604342018-01-08 11:18:18 +010057static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
58 int index, struct clk *clk)
Simon Glass0342bd22016-01-20 19:43:02 -070059{
Simon Glass0342bd22016-01-20 19:43:02 -070060 int ret;
Simon Glass2558bff2017-05-30 21:47:29 -060061 struct ofnode_phandle_args args;
Stephen Warrena9622432016-06-17 09:44:00 -060062 struct udevice *dev_clk;
Mario Six799fe562018-01-15 11:06:51 +010063 const struct clk_ops *ops;
Simon Glass0342bd22016-01-20 19:43:02 -070064
Stephen Warrena9622432016-06-17 09:44:00 -060065 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
66
67 assert(clk);
Patrice Chotard96fc03d2017-07-18 11:57:07 +020068 clk->dev = NULL;
69
Philipp Tomsichf7604342018-01-08 11:18:18 +010070 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
Mario Six799fe562018-01-15 11:06:51 +010071 index, &args);
Simon Glass0342bd22016-01-20 19:43:02 -070072 if (ret) {
73 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
74 __func__, ret);
75 return ret;
76 }
77
Simon Glass2558bff2017-05-30 21:47:29 -060078 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &dev_clk);
Simon Glass0342bd22016-01-20 19:43:02 -070079 if (ret) {
80 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
81 __func__, ret);
82 return ret;
83 }
Wenyou Yang33cdf492016-09-27 11:00:28 +080084
85 clk->dev = dev_clk;
86
Stephen Warrena9622432016-06-17 09:44:00 -060087 ops = clk_dev_ops(dev_clk);
88
89 if (ops->of_xlate)
Simon Glass2558bff2017-05-30 21:47:29 -060090 ret = ops->of_xlate(clk, &args);
Stephen Warrena9622432016-06-17 09:44:00 -060091 else
Simon Glass2558bff2017-05-30 21:47:29 -060092 ret = clk_of_xlate_default(clk, &args);
Stephen Warrena9622432016-06-17 09:44:00 -060093 if (ret) {
94 debug("of_xlate() failed: %d\n", ret);
95 return ret;
96 }
97
98 return clk_request(dev_clk, clk);
99}
Philipp Tomsichf7604342018-01-08 11:18:18 +0100100
101int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
102{
103 return clk_get_by_indexed_prop(dev, "clocks", index, clk);
104}
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100105
Neil Armstrong8a275a02018-04-03 11:44:18 +0200106int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
107{
108 int i, ret, err, count;
109
110 bulk->count = 0;
111
112 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
Neil Armstrong52b26d92018-04-17 11:30:31 +0200113 if (count < 1)
114 return count;
Neil Armstrong8a275a02018-04-03 11:44:18 +0200115
116 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
117 if (!bulk->clks)
118 return -ENOMEM;
119
120 for (i = 0; i < count; i++) {
121 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
122 if (ret < 0)
123 goto bulk_get_err;
124
125 ++bulk->count;
126 }
127
128 return 0;
129
130bulk_get_err:
131 err = clk_release_all(bulk->clks, bulk->count);
132 if (err)
133 debug("%s: could release all clocks for %p\n",
134 __func__, dev);
135
136 return ret;
137}
138
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100139static int clk_set_default_parents(struct udevice *dev)
140{
141 struct clk clk, parent_clk;
142 int index;
143 int num_parents;
144 int ret;
145
146 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
147 "#clock-cells");
148 if (num_parents < 0) {
149 debug("%s: could not read assigned-clock-parents for %p\n",
150 __func__, dev);
151 return 0;
152 }
153
154 for (index = 0; index < num_parents; index++) {
155 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
156 index, &parent_clk);
157 if (ret) {
158 debug("%s: could not get parent clock %d for %s\n",
159 __func__, index, dev_read_name(dev));
160 return ret;
161 }
162
163 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
164 index, &clk);
165 if (ret) {
166 debug("%s: could not get assigned clock %d for %s\n",
167 __func__, index, dev_read_name(dev));
168 return ret;
169 }
170
171 ret = clk_set_parent(&clk, &parent_clk);
172
173 /*
174 * Not all drivers may support clock-reparenting (as of now).
175 * Ignore errors due to this.
176 */
177 if (ret == -ENOSYS)
178 continue;
179
180 if (ret) {
181 debug("%s: failed to reparent clock %d for %s\n",
182 __func__, index, dev_read_name(dev));
183 return ret;
184 }
185 }
186
187 return 0;
188}
189
190static int clk_set_default_rates(struct udevice *dev)
191{
192 struct clk clk;
193 int index;
194 int num_rates;
195 int size;
196 int ret = 0;
197 u32 *rates = NULL;
198
199 size = dev_read_size(dev, "assigned-clock-rates");
200 if (size < 0)
201 return 0;
202
203 num_rates = size / sizeof(u32);
204 rates = calloc(num_rates, sizeof(u32));
205 if (!rates)
206 return -ENOMEM;
207
208 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
209 if (ret)
210 goto fail;
211
212 for (index = 0; index < num_rates; index++) {
213 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
214 index, &clk);
215 if (ret) {
216 debug("%s: could not get assigned clock %d for %s\n",
217 __func__, index, dev_read_name(dev));
218 continue;
219 }
220
221 ret = clk_set_rate(&clk, rates[index]);
222 if (ret < 0) {
223 debug("%s: failed to set rate on clock %d for %s\n",
224 __func__, index, dev_read_name(dev));
225 break;
226 }
227 }
228
229fail:
230 free(rates);
231 return ret;
232}
233
234int clk_set_defaults(struct udevice *dev)
235{
236 int ret;
237
238 /* If this is running pre-reloc state, don't take any action. */
239 if (!(gd->flags & GD_FLG_RELOC))
240 return 0;
241
242 debug("%s(%s)\n", __func__, dev_read_name(dev));
243
244 ret = clk_set_default_parents(dev);
245 if (ret)
246 return ret;
247
248 ret = clk_set_default_rates(dev);
249 if (ret < 0)
250 return ret;
251
252 return 0;
253}
Michal Simek30d40b32016-07-14 13:11:37 +0200254# endif /* OF_PLATDATA */
Stephen Warrena9622432016-06-17 09:44:00 -0600255
256int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
257{
258 int index;
259
260 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
Patrice Chotard96fc03d2017-07-18 11:57:07 +0200261 clk->dev = NULL;
Stephen Warrena9622432016-06-17 09:44:00 -0600262
Simon Glass2558bff2017-05-30 21:47:29 -0600263 index = dev_read_stringlist_search(dev, "clock-names", name);
Stephen Warrena9622432016-06-17 09:44:00 -0600264 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600265 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warrena9622432016-06-17 09:44:00 -0600266 return index;
267 }
268
269 return clk_get_by_index(dev, index, clk);
Simon Glass0342bd22016-01-20 19:43:02 -0700270}
Patrice Chotardcafc3412017-07-25 13:24:45 +0200271
272int clk_release_all(struct clk *clk, int count)
273{
274 int i, ret;
275
276 for (i = 0; i < count; i++) {
277 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
278
279 /* check if clock has been previously requested */
280 if (!clk[i].dev)
281 continue;
282
283 ret = clk_disable(&clk[i]);
284 if (ret && ret != -ENOSYS)
285 return ret;
286
287 ret = clk_free(&clk[i]);
288 if (ret && ret != -ENOSYS)
289 return ret;
290 }
291
292 return 0;
293}
294
Simon Glass589d9152016-07-04 11:58:03 -0600295#endif /* OF_CONTROL */
Stephen Warrena9622432016-06-17 09:44:00 -0600296
297int clk_request(struct udevice *dev, struct clk *clk)
298{
Mario Six799fe562018-01-15 11:06:51 +0100299 const struct clk_ops *ops = clk_dev_ops(dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600300
301 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
302
303 clk->dev = dev;
304
305 if (!ops->request)
306 return 0;
307
308 return ops->request(clk);
309}
310
311int clk_free(struct clk *clk)
312{
Mario Six799fe562018-01-15 11:06:51 +0100313 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600314
315 debug("%s(clk=%p)\n", __func__, clk);
316
317 if (!ops->free)
318 return 0;
319
320 return ops->free(clk);
321}
322
323ulong clk_get_rate(struct clk *clk)
324{
Mario Six799fe562018-01-15 11:06:51 +0100325 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600326
327 debug("%s(clk=%p)\n", __func__, clk);
328
329 if (!ops->get_rate)
330 return -ENOSYS;
331
332 return ops->get_rate(clk);
333}
334
335ulong clk_set_rate(struct clk *clk, ulong rate)
336{
Mario Six799fe562018-01-15 11:06:51 +0100337 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600338
339 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
340
341 if (!ops->set_rate)
342 return -ENOSYS;
343
344 return ops->set_rate(clk, rate);
345}
346
Philipp Tomsichf8e02b22018-01-08 11:15:08 +0100347int clk_set_parent(struct clk *clk, struct clk *parent)
348{
349 const struct clk_ops *ops = clk_dev_ops(clk->dev);
350
351 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
352
353 if (!ops->set_parent)
354 return -ENOSYS;
355
356 return ops->set_parent(clk, parent);
357}
358
Stephen Warrena9622432016-06-17 09:44:00 -0600359int clk_enable(struct clk *clk)
360{
Mario Six799fe562018-01-15 11:06:51 +0100361 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600362
363 debug("%s(clk=%p)\n", __func__, clk);
364
365 if (!ops->enable)
366 return -ENOSYS;
367
368 return ops->enable(clk);
369}
370
Neil Armstrong8a275a02018-04-03 11:44:18 +0200371int clk_enable_bulk(struct clk_bulk *bulk)
372{
373 int i, ret;
374
375 for (i = 0; i < bulk->count; i++) {
376 ret = clk_enable(&bulk->clks[i]);
377 if (ret < 0 && ret != -ENOSYS)
378 return ret;
379 }
380
381 return 0;
382}
383
Stephen Warrena9622432016-06-17 09:44:00 -0600384int clk_disable(struct clk *clk)
385{
Mario Six799fe562018-01-15 11:06:51 +0100386 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600387
388 debug("%s(clk=%p)\n", __func__, clk);
389
390 if (!ops->disable)
391 return -ENOSYS;
392
393 return ops->disable(clk);
394}
Simon Glass36ad2342015-06-23 15:39:15 -0600395
Neil Armstrong8a275a02018-04-03 11:44:18 +0200396int clk_disable_bulk(struct clk_bulk *bulk)
397{
398 int i, ret;
399
400 for (i = 0; i < bulk->count; i++) {
401 ret = clk_disable(&bulk->clks[i]);
402 if (ret < 0 && ret != -ENOSYS)
403 return ret;
404 }
405
406 return 0;
407}
408
Simon Glass36ad2342015-06-23 15:39:15 -0600409UCLASS_DRIVER(clk) = {
410 .id = UCLASS_CLK,
411 .name = "clk",
412};