blob: fbea72091b190c43fdf411a0d96f721735264faf [file] [log] [blame]
Simon Glass36ad2342015-06-23 15:39:15 -06001/*
2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
Stephen Warrena9622432016-06-17 09:44:00 -06004 * Copyright (c) 2016, NVIDIA CORPORATION.
Simon Glass36ad2342015-06-23 15:39:15 -06005 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
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>
Simon Glass589d9152016-07-04 11:58:03 -060013#include <dt-structs.h>
Simon Glass36ad2342015-06-23 15:39:15 -060014#include <errno.h>
Simon Glass36ad2342015-06-23 15:39:15 -060015
Mario Six799fe562018-01-15 11:06:51 +010016static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glass36ad2342015-06-23 15:39:15 -060017{
Mario Six799fe562018-01-15 11:06:51 +010018 return (const struct clk_ops *)dev->driver->ops;
Simon Glass36ad2342015-06-23 15:39:15 -060019}
20
Stephen Warrena9622432016-06-17 09:44:00 -060021#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass589d9152016-07-04 11:58:03 -060022# if CONFIG_IS_ENABLED(OF_PLATDATA)
23int clk_get_by_index_platdata(struct udevice *dev, int index,
Simon Glasse94414b2017-08-29 14:15:56 -060024 struct phandle_1_arg *cells, struct clk *clk)
Simon Glass589d9152016-07-04 11:58:03 -060025{
26 int ret;
27
28 if (index != 0)
29 return -ENOSYS;
30 ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
31 if (ret)
32 return ret;
Simon Glassfdec5802017-08-29 14:15:58 -060033 clk->id = cells[0].arg[0];
Simon Glass589d9152016-07-04 11:58:03 -060034
35 return 0;
36}
37# else
Stephen Warrena9622432016-06-17 09:44:00 -060038static int clk_of_xlate_default(struct clk *clk,
Simon Glassb7ae2772017-05-18 20:09:40 -060039 struct ofnode_phandle_args *args)
Simon Glass36ad2342015-06-23 15:39:15 -060040{
Stephen Warrena9622432016-06-17 09:44:00 -060041 debug("%s(clk=%p)\n", __func__, clk);
Simon Glass36ad2342015-06-23 15:39:15 -060042
Stephen Warrena9622432016-06-17 09:44:00 -060043 if (args->args_count > 1) {
44 debug("Invaild args_count: %d\n", args->args_count);
45 return -EINVAL;
46 }
Simon Glass36ad2342015-06-23 15:39:15 -060047
Stephen Warrena9622432016-06-17 09:44:00 -060048 if (args->args_count)
49 clk->id = args->args[0];
50 else
51 clk->id = 0;
Simon Glass36ad2342015-06-23 15:39:15 -060052
Stephen Warrena9622432016-06-17 09:44:00 -060053 return 0;
Simon Glass36ad2342015-06-23 15:39:15 -060054}
Simon Glass0342bd22016-01-20 19:43:02 -070055
Stephen Warrena9622432016-06-17 09:44:00 -060056int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
Simon Glass0342bd22016-01-20 19:43:02 -070057{
Simon Glass0342bd22016-01-20 19:43:02 -070058 int ret;
Simon Glass2558bff2017-05-30 21:47:29 -060059 struct ofnode_phandle_args args;
Stephen Warrena9622432016-06-17 09:44:00 -060060 struct udevice *dev_clk;
Mario Six799fe562018-01-15 11:06:51 +010061 const struct clk_ops *ops;
Simon Glass0342bd22016-01-20 19:43:02 -070062
Stephen Warrena9622432016-06-17 09:44:00 -060063 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
64
65 assert(clk);
Patrice Chotard96fc03d2017-07-18 11:57:07 +020066 clk->dev = NULL;
67
Simon Glass2558bff2017-05-30 21:47:29 -060068 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
Mario Six799fe562018-01-15 11:06:51 +010069 index, &args);
Simon Glass0342bd22016-01-20 19:43:02 -070070 if (ret) {
71 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
72 __func__, ret);
73 return ret;
74 }
75
Simon Glass2558bff2017-05-30 21:47:29 -060076 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args.node, &dev_clk);
Simon Glass0342bd22016-01-20 19:43:02 -070077 if (ret) {
78 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
79 __func__, ret);
80 return ret;
81 }
Wenyou Yang33cdf492016-09-27 11:00:28 +080082
83 clk->dev = dev_clk;
84
Stephen Warrena9622432016-06-17 09:44:00 -060085 ops = clk_dev_ops(dev_clk);
86
87 if (ops->of_xlate)
Simon Glass2558bff2017-05-30 21:47:29 -060088 ret = ops->of_xlate(clk, &args);
Stephen Warrena9622432016-06-17 09:44:00 -060089 else
Simon Glass2558bff2017-05-30 21:47:29 -060090 ret = clk_of_xlate_default(clk, &args);
Stephen Warrena9622432016-06-17 09:44:00 -060091 if (ret) {
92 debug("of_xlate() failed: %d\n", ret);
93 return ret;
94 }
95
96 return clk_request(dev_clk, clk);
97}
Michal Simek30d40b32016-07-14 13:11:37 +020098# endif /* OF_PLATDATA */
Stephen Warrena9622432016-06-17 09:44:00 -060099
100int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
101{
102 int index;
103
104 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
Patrice Chotard96fc03d2017-07-18 11:57:07 +0200105 clk->dev = NULL;
Stephen Warrena9622432016-06-17 09:44:00 -0600106
Simon Glass2558bff2017-05-30 21:47:29 -0600107 index = dev_read_stringlist_search(dev, "clock-names", name);
Stephen Warrena9622432016-06-17 09:44:00 -0600108 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600109 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warrena9622432016-06-17 09:44:00 -0600110 return index;
111 }
112
113 return clk_get_by_index(dev, index, clk);
Simon Glass0342bd22016-01-20 19:43:02 -0700114}
Patrice Chotardcafc3412017-07-25 13:24:45 +0200115
116int clk_release_all(struct clk *clk, int count)
117{
118 int i, ret;
119
120 for (i = 0; i < count; i++) {
121 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
122
123 /* check if clock has been previously requested */
124 if (!clk[i].dev)
125 continue;
126
127 ret = clk_disable(&clk[i]);
128 if (ret && ret != -ENOSYS)
129 return ret;
130
131 ret = clk_free(&clk[i]);
132 if (ret && ret != -ENOSYS)
133 return ret;
134 }
135
136 return 0;
137}
138
Simon Glass589d9152016-07-04 11:58:03 -0600139#endif /* OF_CONTROL */
Stephen Warrena9622432016-06-17 09:44:00 -0600140
141int clk_request(struct udevice *dev, struct clk *clk)
142{
Mario Six799fe562018-01-15 11:06:51 +0100143 const struct clk_ops *ops = clk_dev_ops(dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600144
145 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
146
147 clk->dev = dev;
148
149 if (!ops->request)
150 return 0;
151
152 return ops->request(clk);
153}
154
155int clk_free(struct clk *clk)
156{
Mario Six799fe562018-01-15 11:06:51 +0100157 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600158
159 debug("%s(clk=%p)\n", __func__, clk);
160
161 if (!ops->free)
162 return 0;
163
164 return ops->free(clk);
165}
166
167ulong clk_get_rate(struct clk *clk)
168{
Mario Six799fe562018-01-15 11:06:51 +0100169 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600170
171 debug("%s(clk=%p)\n", __func__, clk);
172
173 if (!ops->get_rate)
174 return -ENOSYS;
175
176 return ops->get_rate(clk);
177}
178
179ulong clk_set_rate(struct clk *clk, ulong rate)
180{
Mario Six799fe562018-01-15 11:06:51 +0100181 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600182
183 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
184
185 if (!ops->set_rate)
186 return -ENOSYS;
187
188 return ops->set_rate(clk, rate);
189}
190
191int clk_enable(struct clk *clk)
192{
Mario Six799fe562018-01-15 11:06:51 +0100193 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600194
195 debug("%s(clk=%p)\n", __func__, clk);
196
197 if (!ops->enable)
198 return -ENOSYS;
199
200 return ops->enable(clk);
201}
202
203int clk_disable(struct clk *clk)
204{
Mario Six799fe562018-01-15 11:06:51 +0100205 const struct clk_ops *ops = clk_dev_ops(clk->dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600206
207 debug("%s(clk=%p)\n", __func__, clk);
208
209 if (!ops->disable)
210 return -ENOSYS;
211
212 return ops->disable(clk);
213}
Simon Glass36ad2342015-06-23 15:39:15 -0600214
Simon Glass36ad2342015-06-23 15:39:15 -0600215UCLASS_DRIVER(clk) = {
216 .id = UCLASS_CLK,
217 .name = "clk",
218};