blob: 8a4c5680df6619d9101579fc52a635c2693b3cd4 [file] [log] [blame]
Simon Glassf26c8a82015-06-23 15:39:15 -06001/*
2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <errno.h>
12#include <dm/lists.h>
13#include <dm/root.h>
14
Simon Glasse70cc432016-01-20 19:43:02 -070015DECLARE_GLOBAL_DATA_PTR;
16
Simon Glassf26c8a82015-06-23 15:39:15 -060017ulong clk_get_rate(struct udevice *dev)
18{
19 struct clk_ops *ops = clk_get_ops(dev);
20
21 if (!ops->get_rate)
22 return -ENOSYS;
23
24 return ops->get_rate(dev);
25}
26
27ulong clk_set_rate(struct udevice *dev, ulong rate)
28{
29 struct clk_ops *ops = clk_get_ops(dev);
30
31 if (!ops->set_rate)
32 return -ENOSYS;
33
34 return ops->set_rate(dev, rate);
35}
36
Masahiro Yamadaf0e07512016-01-13 13:16:12 +090037int clk_enable(struct udevice *dev, int periph)
38{
39 struct clk_ops *ops = clk_get_ops(dev);
40
41 if (!ops->enable)
42 return -ENOSYS;
43
44 return ops->enable(dev, periph);
45}
46
Simon Glassf26c8a82015-06-23 15:39:15 -060047ulong clk_get_periph_rate(struct udevice *dev, int periph)
48{
49 struct clk_ops *ops = clk_get_ops(dev);
50
51 if (!ops->get_periph_rate)
52 return -ENOSYS;
53
54 return ops->get_periph_rate(dev, periph);
55}
56
57ulong clk_set_periph_rate(struct udevice *dev, int periph, ulong rate)
58{
59 struct clk_ops *ops = clk_get_ops(dev);
60
61 if (!ops->set_periph_rate)
62 return -ENOSYS;
63
64 return ops->set_periph_rate(dev, periph, rate);
65}
66
Simon Glasse70cc432016-01-20 19:43:02 -070067#if CONFIG_IS_ENABLED(OF_CONTROL)
68int clk_get_by_index(struct udevice *dev, int index, struct udevice **clk_devp)
69{
70 struct fdtdec_phandle_args args;
71 int ret;
72
73 assert(*clk_devp);
74 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
75 "clocks", "#clock-cells", 0, index,
76 &args);
77 if (ret) {
78 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
79 __func__, ret);
80 return ret;
81 }
82
83 ret = uclass_get_device_by_of_offset(UCLASS_CLK, args.node, clk_devp);
84 if (ret) {
85 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
86 __func__, ret);
87 return ret;
88 }
89 return args.args_count > 0 ? args.args[0] : 0;
90}
91#endif
92
Simon Glassf26c8a82015-06-23 15:39:15 -060093UCLASS_DRIVER(clk) = {
94 .id = UCLASS_CLK,
95 .name = "clk",
96};