blob: c42fff6ec2908ba801d8420011b7e55f4346da9a [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
Simon Glass0342bd22016-01-20 19:43:02 -070016DECLARE_GLOBAL_DATA_PTR;
17
Stephen Warrena9622432016-06-17 09:44:00 -060018static inline struct clk_ops *clk_dev_ops(struct udevice *dev)
Simon Glass36ad2342015-06-23 15:39:15 -060019{
Stephen Warrena9622432016-06-17 09:44:00 -060020 return (struct clk_ops *)dev->driver->ops;
Simon Glass36ad2342015-06-23 15:39:15 -060021}
22
Stephen Warrena9622432016-06-17 09:44:00 -060023#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass589d9152016-07-04 11:58:03 -060024# if CONFIG_IS_ENABLED(OF_PLATDATA)
25int clk_get_by_index_platdata(struct udevice *dev, int index,
26 struct phandle_2_cell *cells, struct clk *clk)
27{
28 int ret;
29
30 if (index != 0)
31 return -ENOSYS;
32 ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
33 if (ret)
34 return ret;
35 clk->id = cells[0].id;
36
37 return 0;
38}
39# else
Stephen Warrena9622432016-06-17 09:44:00 -060040static int clk_of_xlate_default(struct clk *clk,
41 struct fdtdec_phandle_args *args)
Simon Glass36ad2342015-06-23 15:39:15 -060042{
Stephen Warrena9622432016-06-17 09:44:00 -060043 debug("%s(clk=%p)\n", __func__, clk);
Simon Glass36ad2342015-06-23 15:39:15 -060044
Stephen Warrena9622432016-06-17 09:44:00 -060045 if (args->args_count > 1) {
46 debug("Invaild args_count: %d\n", args->args_count);
47 return -EINVAL;
48 }
Simon Glass36ad2342015-06-23 15:39:15 -060049
Stephen Warrena9622432016-06-17 09:44:00 -060050 if (args->args_count)
51 clk->id = args->args[0];
52 else
53 clk->id = 0;
Simon Glass36ad2342015-06-23 15:39:15 -060054
Stephen Warrena9622432016-06-17 09:44:00 -060055 return 0;
Simon Glass36ad2342015-06-23 15:39:15 -060056}
Simon Glass0342bd22016-01-20 19:43:02 -070057
Stephen Warrena9622432016-06-17 09:44:00 -060058int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
Simon Glass0342bd22016-01-20 19:43:02 -070059{
Simon Glass0342bd22016-01-20 19:43:02 -070060 int ret;
Simon Glass508c0772016-01-21 19:44:00 -070061 struct fdtdec_phandle_args args;
Stephen Warrena9622432016-06-17 09:44:00 -060062 struct udevice *dev_clk;
63 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);
Simon Glass0342bd22016-01-20 19:43:02 -070068 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
69 "clocks", "#clock-cells", 0, index,
70 &args);
71 if (ret) {
72 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
73 __func__, ret);
74 return ret;
75 }
76
Stephen Warrena9622432016-06-17 09:44:00 -060077 ret = uclass_get_device_by_of_offset(UCLASS_CLK, args.node, &dev_clk);
Simon Glass0342bd22016-01-20 19:43:02 -070078 if (ret) {
79 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
80 __func__, ret);
81 return ret;
82 }
Stephen Warrena9622432016-06-17 09:44:00 -060083 ops = clk_dev_ops(dev_clk);
84
85 if (ops->of_xlate)
86 ret = ops->of_xlate(clk, &args);
87 else
88 ret = clk_of_xlate_default(clk, &args);
89 if (ret) {
90 debug("of_xlate() failed: %d\n", ret);
91 return ret;
92 }
93
94 return clk_request(dev_clk, clk);
95}
Michal Simek30d40b32016-07-14 13:11:37 +020096# endif /* OF_PLATDATA */
Stephen Warrena9622432016-06-17 09:44:00 -060097
98int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
99{
100 int index;
101
102 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
103
Simon Glassb0ea7402016-10-02 17:59:28 -0600104 index = fdt_stringlist_search(gd->fdt_blob, dev->of_offset,
105 "clock-names", name);
Stephen Warrena9622432016-06-17 09:44:00 -0600106 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600107 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warrena9622432016-06-17 09:44:00 -0600108 return index;
109 }
110
111 return clk_get_by_index(dev, index, clk);
Simon Glass0342bd22016-01-20 19:43:02 -0700112}
Simon Glass589d9152016-07-04 11:58:03 -0600113#endif /* OF_CONTROL */
Stephen Warrena9622432016-06-17 09:44:00 -0600114
115int clk_request(struct udevice *dev, struct clk *clk)
116{
117 struct clk_ops *ops = clk_dev_ops(dev);
118
119 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
120
121 clk->dev = dev;
122
123 if (!ops->request)
124 return 0;
125
126 return ops->request(clk);
127}
128
129int clk_free(struct clk *clk)
130{
131 struct clk_ops *ops = clk_dev_ops(clk->dev);
132
133 debug("%s(clk=%p)\n", __func__, clk);
134
135 if (!ops->free)
136 return 0;
137
138 return ops->free(clk);
139}
140
141ulong clk_get_rate(struct clk *clk)
142{
143 struct clk_ops *ops = clk_dev_ops(clk->dev);
144
145 debug("%s(clk=%p)\n", __func__, clk);
146
147 if (!ops->get_rate)
148 return -ENOSYS;
149
150 return ops->get_rate(clk);
151}
152
153ulong clk_set_rate(struct clk *clk, ulong rate)
154{
155 struct clk_ops *ops = clk_dev_ops(clk->dev);
156
157 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
158
159 if (!ops->set_rate)
160 return -ENOSYS;
161
162 return ops->set_rate(clk, rate);
163}
164
165int clk_enable(struct clk *clk)
166{
167 struct clk_ops *ops = clk_dev_ops(clk->dev);
168
169 debug("%s(clk=%p)\n", __func__, clk);
170
171 if (!ops->enable)
172 return -ENOSYS;
173
174 return ops->enable(clk);
175}
176
177int clk_disable(struct clk *clk)
178{
179 struct clk_ops *ops = clk_dev_ops(clk->dev);
180
181 debug("%s(clk=%p)\n", __func__, clk);
182
183 if (!ops->disable)
184 return -ENOSYS;
185
186 return ops->disable(clk);
187}
Simon Glass36ad2342015-06-23 15:39:15 -0600188
189UCLASS_DRIVER(clk) = {
190 .id = UCLASS_CLK,
191 .name = "clk",
192};