1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
// SPDX-License-Identifier: GPL-2.0+
/*
* TI multiplexer clock support
*
* Copyright (C) 2020 Dario Binacchi <dariobin@libero.it>
*
* Based on Linux kernel drivers/clk/ti/mux.c
*/
#include <common.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <clk-uclass.h>
#include <asm/io.h>
#include <linux/clk-provider.h>
#include "clk.h"
struct clk_ti_mux_priv {
struct clk_bulk parents;
struct clk_ti_reg reg;
u32 flags;
u32 mux_flags;
u32 mask;
u32 shift;
s32 latch;
};
static struct clk *clk_ti_mux_get_parent_by_index(struct clk_bulk *parents,
int index)
{
if (index < 0 || !parents)
return ERR_PTR(-EINVAL);
if (index >= parents->count)
return ERR_PTR(-ENODEV);
return &parents->clks[index];
}
static int clk_ti_mux_get_parent_index(struct clk_bulk *parents,
struct clk *parent)
{
int i;
if (!parents || !parent)
return -EINVAL;
for (i = 0; i < parents->count; i++) {
if (parents->clks[i].dev == parent->dev)
return i;
}
return -ENODEV;
}
static int clk_ti_mux_get_index(struct clk *clk)
{
struct clk_ti_mux_priv *priv = dev_get_priv(clk->dev);
u32 val;
val = clk_ti_readl(&priv->reg);
val >>= priv->shift;
val &= priv->mask;
if (val && (priv->flags & CLK_MUX_INDEX_BIT))
val = ffs(val) - 1;
if (val && (priv->flags & CLK_MUX_INDEX_ONE))
val--;
if (val >= priv->parents.count)
return -EINVAL;
return val;
}
static int clk_ti_mux_set_parent(struct clk *clk, struct clk *parent)
{
struct clk_ti_mux_priv *priv = dev_get_priv(clk->dev);
int index;
u32 val;
index = clk_ti_mux_get_parent_index(&priv->parents, parent);
if (index < 0) {
dev_err(clk->dev, "failed to get parent clock\n");
return index;
}
index = clk_mux_index_to_val(NULL, priv->flags, index);
if (priv->flags & CLK_MUX_HIWORD_MASK) {
val = priv->mask << (priv->shift + 16);
} else {
val = clk_ti_readl(&priv->reg);
val &= ~(priv->mask << priv->shift);
}
val |= index << priv->shift;
clk_ti_writel(val, &priv->reg);
clk_ti_latch(&priv->reg, priv->latch);
return 0;
}
static ulong clk_ti_mux_set_rate(struct clk *clk, ulong rate)
{
struct clk_ti_mux_priv *priv = dev_get_priv(clk->dev);
struct clk *parent;
int index;
if ((clk->flags & CLK_SET_RATE_PARENT) == 0)
return -ENOSYS;
index = clk_ti_mux_get_index(clk);
parent = clk_ti_mux_get_parent_by_index(&priv->parents, index);
if (IS_ERR(parent))
return PTR_ERR(parent);
rate = clk_set_rate(parent, rate);
dev_dbg(clk->dev, "rate=%ld\n", rate);
return rate;
}
static ulong clk_ti_mux_get_rate(struct clk *clk)
{
struct clk_ti_mux_priv *priv = dev_get_priv(clk->dev);
int index;
struct clk *parent;
ulong rate;
index = clk_ti_mux_get_index(clk);
parent = clk_ti_mux_get_parent_by_index(&priv->parents, index);
if (IS_ERR(parent))
return PTR_ERR(parent);
rate = clk_get_rate(parent);
dev_dbg(clk->dev, "rate=%ld\n", rate);
return rate;
}
static ulong clk_ti_mux_round_rate(struct clk *clk, ulong rate)
{
struct clk_ti_mux_priv *priv = dev_get_priv(clk->dev);
struct clk *parent;
int index;
if ((clk->flags & CLK_SET_RATE_PARENT) == 0)
return -ENOSYS;
index = clk_ti_mux_get_index(clk);
parent = clk_ti_mux_get_parent_by_index(&priv->parents, index);
if (IS_ERR(parent))
return PTR_ERR(parent);
rate = clk_round_rate(parent, rate);
dev_dbg(clk->dev, "rate=%ld\n", rate);
return rate;
}
static int clk_ti_mux_request(struct clk *clk)
{
struct clk_ti_mux_priv *priv = dev_get_priv(clk->dev);
struct clk *parent;
int index;
clk->flags = priv->flags;
index = clk_ti_mux_get_index(clk);
parent = clk_ti_mux_get_parent_by_index(&priv->parents, index);
if (IS_ERR(parent))
return PTR_ERR(parent);
return clk_ti_mux_set_parent(clk, parent);
}
static struct clk_ops clk_ti_mux_ops = {
.request = clk_ti_mux_request,
.round_rate = clk_ti_mux_round_rate,
.get_rate = clk_ti_mux_get_rate,
.set_rate = clk_ti_mux_set_rate,
.set_parent = clk_ti_mux_set_parent,
};
static int clk_ti_mux_remove(struct udevice *dev)
{
struct clk_ti_mux_priv *priv = dev_get_priv(dev);
int err;
err = clk_release_all(priv->parents.clks, priv->parents.count);
if (err)
dev_dbg(dev, "could not release all parents' clocks\n");
return err;
}
static int clk_ti_mux_probe(struct udevice *dev)
{
struct clk_ti_mux_priv *priv = dev_get_priv(dev);
int err;
err = clk_get_bulk(dev, &priv->parents);
if (err || priv->parents.count < 2) {
dev_err(dev, "mux-clock must have parents\n");
return err ? err : -EFAULT;
}
/* Generate bit-mask based on parents info */
priv->mask = priv->parents.count;
if (!(priv->mux_flags & CLK_MUX_INDEX_ONE))
priv->mask--;
priv->mask = (1 << fls(priv->mask)) - 1;
return 0;
}
static int clk_ti_mux_of_to_plat(struct udevice *dev)
{
struct clk_ti_mux_priv *priv = dev_get_priv(dev);
int err;
err = clk_ti_get_reg_addr(dev, 0, &priv->reg);
if (err) {
dev_err(dev, "failed to get register address\n");
return err;
}
priv->shift = dev_read_u32_default(dev, "ti,bit-shift", 0);
priv->latch = dev_read_s32_default(dev, "ti,latch-bit", -EINVAL);
priv->flags = CLK_SET_RATE_NO_REPARENT;
if (dev_read_bool(dev, "ti,set-rate-parent"))
priv->flags |= CLK_SET_RATE_PARENT;
if (dev_read_bool(dev, "ti,index-starts-at-one"))
priv->mux_flags |= CLK_MUX_INDEX_ONE;
return 0;
}
static const struct udevice_id clk_ti_mux_of_match[] = {
{.compatible = "ti,mux-clock"},
{},
};
U_BOOT_DRIVER(clk_ti_mux) = {
.name = "ti_mux_clock",
.id = UCLASS_CLK,
.of_match = clk_ti_mux_of_match,
.of_to_plat = clk_ti_mux_of_to_plat,
.probe = clk_ti_mux_probe,
.remove = clk_ti_mux_remove,
.priv_auto = sizeof(struct clk_ti_mux_priv),
.ops = &clk_ti_mux_ops,
};
|