xref: /openbmc/u-boot/drivers/clk/clk-uclass.c (revision ce9844ce)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  * Copyright (c) 2016, NVIDIA CORPORATION.
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <clk.h>
11 #include <clk-uclass.h>
12 #include <dm.h>
13 #include <errno.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 static inline struct clk_ops *clk_dev_ops(struct udevice *dev)
18 {
19 	return (struct clk_ops *)dev->driver->ops;
20 }
21 
22 #if CONFIG_IS_ENABLED(OF_CONTROL)
23 #ifdef CONFIG_SPL_BUILD
24 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
25 {
26 	int ret;
27 	u32 cell[2];
28 
29 	if (index != 0)
30 		return -ENOSYS;
31 	assert(clk);
32 	ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
33 	if (ret)
34 		return ret;
35 	ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset, "clocks",
36 				   cell, 2);
37 	if (ret)
38 		return ret;
39 	clk->id = cell[1];
40 	return 0;
41 }
42 
43 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
44 {
45 	return -ENOSYS;
46 }
47 #else
48 static int clk_of_xlate_default(struct clk *clk,
49 				struct fdtdec_phandle_args *args)
50 {
51 	debug("%s(clk=%p)\n", __func__, clk);
52 
53 	if (args->args_count > 1) {
54 		debug("Invaild args_count: %d\n", args->args_count);
55 		return -EINVAL;
56 	}
57 
58 	if (args->args_count)
59 		clk->id = args->args[0];
60 	else
61 		clk->id = 0;
62 
63 	return 0;
64 }
65 
66 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
67 {
68 	int ret;
69 	struct fdtdec_phandle_args args;
70 	struct udevice *dev_clk;
71 	struct clk_ops *ops;
72 
73 	debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
74 
75 	assert(clk);
76 	ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
77 					     "clocks", "#clock-cells", 0, index,
78 					     &args);
79 	if (ret) {
80 		debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
81 		      __func__, ret);
82 		return ret;
83 	}
84 
85 	ret = uclass_get_device_by_of_offset(UCLASS_CLK, args.node, &dev_clk);
86 	if (ret) {
87 		debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
88 		      __func__, ret);
89 		return ret;
90 	}
91 	ops = clk_dev_ops(dev_clk);
92 
93 	if (ops->of_xlate)
94 		ret = ops->of_xlate(clk, &args);
95 	else
96 		ret = clk_of_xlate_default(clk, &args);
97 	if (ret) {
98 		debug("of_xlate() failed: %d\n", ret);
99 		return ret;
100 	}
101 
102 	return clk_request(dev_clk, clk);
103 }
104 
105 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
106 {
107 	int index;
108 
109 	debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
110 
111 	index = fdt_find_string(gd->fdt_blob, dev->of_offset, "clock-names",
112 				name);
113 	if (index < 0) {
114 		debug("fdt_find_string() failed: %d\n", index);
115 		return index;
116 	}
117 
118 	return clk_get_by_index(dev, index, clk);
119 }
120 #endif
121 #endif
122 
123 int clk_request(struct udevice *dev, struct clk *clk)
124 {
125 	struct clk_ops *ops = clk_dev_ops(dev);
126 
127 	debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
128 
129 	clk->dev = dev;
130 
131 	if (!ops->request)
132 		return 0;
133 
134 	return ops->request(clk);
135 }
136 
137 int clk_free(struct clk *clk)
138 {
139 	struct clk_ops *ops = clk_dev_ops(clk->dev);
140 
141 	debug("%s(clk=%p)\n", __func__, clk);
142 
143 	if (!ops->free)
144 		return 0;
145 
146 	return ops->free(clk);
147 }
148 
149 ulong clk_get_rate(struct clk *clk)
150 {
151 	struct clk_ops *ops = clk_dev_ops(clk->dev);
152 
153 	debug("%s(clk=%p)\n", __func__, clk);
154 
155 	if (!ops->get_rate)
156 		return -ENOSYS;
157 
158 	return ops->get_rate(clk);
159 }
160 
161 ulong clk_set_rate(struct clk *clk, ulong rate)
162 {
163 	struct clk_ops *ops = clk_dev_ops(clk->dev);
164 
165 	debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
166 
167 	if (!ops->set_rate)
168 		return -ENOSYS;
169 
170 	return ops->set_rate(clk, rate);
171 }
172 
173 int clk_enable(struct clk *clk)
174 {
175 	struct clk_ops *ops = clk_dev_ops(clk->dev);
176 
177 	debug("%s(clk=%p)\n", __func__, clk);
178 
179 	if (!ops->enable)
180 		return -ENOSYS;
181 
182 	return ops->enable(clk);
183 }
184 
185 int clk_disable(struct clk *clk)
186 {
187 	struct clk_ops *ops = clk_dev_ops(clk->dev);
188 
189 	debug("%s(clk=%p)\n", __func__, clk);
190 
191 	if (!ops->disable)
192 		return -ENOSYS;
193 
194 	return ops->disable(clk);
195 }
196 
197 UCLASS_DRIVER(clk) = {
198 	.id		= UCLASS_CLK,
199 	.name		= "clk",
200 };
201