This source file includes following definitions.
- at91sam9x5_clk_usb_recalc_rate
- at91sam9x5_clk_usb_determine_rate
- at91sam9x5_clk_usb_set_parent
- at91sam9x5_clk_usb_get_parent
- at91sam9x5_clk_usb_set_rate
- at91sam9n12_clk_usb_enable
- at91sam9n12_clk_usb_disable
- at91sam9n12_clk_usb_is_enabled
- _at91sam9x5_clk_register_usb
- at91sam9x5_clk_register_usb
- sam9x60_clk_register_usb
- at91sam9n12_clk_register_usb
- at91rm9200_clk_usb_recalc_rate
- at91rm9200_clk_usb_round_rate
- at91rm9200_clk_usb_set_rate
- at91rm9200_clk_register_usb
1
2
3
4
5
6 #include <linux/clk-provider.h>
7 #include <linux/clkdev.h>
8 #include <linux/clk/at91_pmc.h>
9 #include <linux/of.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/regmap.h>
12
13 #include "pmc.h"
14
15 #define SAM9X5_USB_DIV_SHIFT 8
16 #define SAM9X5_USB_MAX_DIV 0xf
17
18 #define RM9200_USB_DIV_SHIFT 28
19 #define RM9200_USB_DIV_TAB_SIZE 4
20
21 #define SAM9X5_USBS_MASK GENMASK(0, 0)
22 #define SAM9X60_USBS_MASK GENMASK(1, 0)
23
24 struct at91sam9x5_clk_usb {
25 struct clk_hw hw;
26 struct regmap *regmap;
27 u32 usbs_mask;
28 };
29
30 #define to_at91sam9x5_clk_usb(hw) \
31 container_of(hw, struct at91sam9x5_clk_usb, hw)
32
33 struct at91rm9200_clk_usb {
34 struct clk_hw hw;
35 struct regmap *regmap;
36 u32 divisors[4];
37 };
38
39 #define to_at91rm9200_clk_usb(hw) \
40 container_of(hw, struct at91rm9200_clk_usb, hw)
41
42 static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
43 unsigned long parent_rate)
44 {
45 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
46 unsigned int usbr;
47 u8 usbdiv;
48
49 regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
50 usbdiv = (usbr & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
51
52 return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
53 }
54
55 static int at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
56 struct clk_rate_request *req)
57 {
58 struct clk_hw *parent;
59 long best_rate = -EINVAL;
60 unsigned long tmp_rate;
61 int best_diff = -1;
62 int tmp_diff;
63 int i;
64
65 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
66 int div;
67
68 parent = clk_hw_get_parent_by_index(hw, i);
69 if (!parent)
70 continue;
71
72 for (div = 1; div < SAM9X5_USB_MAX_DIV + 2; div++) {
73 unsigned long tmp_parent_rate;
74
75 tmp_parent_rate = req->rate * div;
76 tmp_parent_rate = clk_hw_round_rate(parent,
77 tmp_parent_rate);
78 if (!tmp_parent_rate)
79 continue;
80
81 tmp_rate = DIV_ROUND_CLOSEST(tmp_parent_rate, div);
82 if (tmp_rate < req->rate)
83 tmp_diff = req->rate - tmp_rate;
84 else
85 tmp_diff = tmp_rate - req->rate;
86
87 if (best_diff < 0 || best_diff > tmp_diff) {
88 best_rate = tmp_rate;
89 best_diff = tmp_diff;
90 req->best_parent_rate = tmp_parent_rate;
91 req->best_parent_hw = parent;
92 }
93
94 if (!best_diff || tmp_rate < req->rate)
95 break;
96 }
97
98 if (!best_diff)
99 break;
100 }
101
102 if (best_rate < 0)
103 return best_rate;
104
105 req->rate = best_rate;
106 return 0;
107 }
108
109 static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
110 {
111 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
112
113 if (index > 1)
114 return -EINVAL;
115
116 regmap_update_bits(usb->regmap, AT91_PMC_USB, usb->usbs_mask, index);
117
118 return 0;
119 }
120
121 static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
122 {
123 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
124 unsigned int usbr;
125
126 regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
127
128 return usbr & usb->usbs_mask;
129 }
130
131 static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
132 unsigned long parent_rate)
133 {
134 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
135 unsigned long div;
136
137 if (!rate)
138 return -EINVAL;
139
140 div = DIV_ROUND_CLOSEST(parent_rate, rate);
141 if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
142 return -EINVAL;
143
144 regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_OHCIUSBDIV,
145 (div - 1) << SAM9X5_USB_DIV_SHIFT);
146
147 return 0;
148 }
149
150 static const struct clk_ops at91sam9x5_usb_ops = {
151 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
152 .determine_rate = at91sam9x5_clk_usb_determine_rate,
153 .get_parent = at91sam9x5_clk_usb_get_parent,
154 .set_parent = at91sam9x5_clk_usb_set_parent,
155 .set_rate = at91sam9x5_clk_usb_set_rate,
156 };
157
158 static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
159 {
160 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
161
162 regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS,
163 AT91_PMC_USBS);
164
165 return 0;
166 }
167
168 static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
169 {
170 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
171
172 regmap_update_bits(usb->regmap, AT91_PMC_USB, AT91_PMC_USBS, 0);
173 }
174
175 static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
176 {
177 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
178 unsigned int usbr;
179
180 regmap_read(usb->regmap, AT91_PMC_USB, &usbr);
181
182 return usbr & AT91_PMC_USBS;
183 }
184
185 static const struct clk_ops at91sam9n12_usb_ops = {
186 .enable = at91sam9n12_clk_usb_enable,
187 .disable = at91sam9n12_clk_usb_disable,
188 .is_enabled = at91sam9n12_clk_usb_is_enabled,
189 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
190 .determine_rate = at91sam9x5_clk_usb_determine_rate,
191 .set_rate = at91sam9x5_clk_usb_set_rate,
192 };
193
194 static struct clk_hw * __init
195 _at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
196 const char **parent_names, u8 num_parents,
197 u32 usbs_mask)
198 {
199 struct at91sam9x5_clk_usb *usb;
200 struct clk_hw *hw;
201 struct clk_init_data init;
202 int ret;
203
204 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
205 if (!usb)
206 return ERR_PTR(-ENOMEM);
207
208 init.name = name;
209 init.ops = &at91sam9x5_usb_ops;
210 init.parent_names = parent_names;
211 init.num_parents = num_parents;
212 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
213 CLK_SET_RATE_PARENT;
214
215 usb->hw.init = &init;
216 usb->regmap = regmap;
217 usb->usbs_mask = usbs_mask;
218
219 hw = &usb->hw;
220 ret = clk_hw_register(NULL, &usb->hw);
221 if (ret) {
222 kfree(usb);
223 hw = ERR_PTR(ret);
224 }
225
226 return hw;
227 }
228
229 struct clk_hw * __init
230 at91sam9x5_clk_register_usb(struct regmap *regmap, const char *name,
231 const char **parent_names, u8 num_parents)
232 {
233 return _at91sam9x5_clk_register_usb(regmap, name, parent_names,
234 num_parents, SAM9X5_USBS_MASK);
235 }
236
237 struct clk_hw * __init
238 sam9x60_clk_register_usb(struct regmap *regmap, const char *name,
239 const char **parent_names, u8 num_parents)
240 {
241 return _at91sam9x5_clk_register_usb(regmap, name, parent_names,
242 num_parents, SAM9X60_USBS_MASK);
243 }
244
245 struct clk_hw * __init
246 at91sam9n12_clk_register_usb(struct regmap *regmap, const char *name,
247 const char *parent_name)
248 {
249 struct at91sam9x5_clk_usb *usb;
250 struct clk_hw *hw;
251 struct clk_init_data init;
252 int ret;
253
254 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
255 if (!usb)
256 return ERR_PTR(-ENOMEM);
257
258 init.name = name;
259 init.ops = &at91sam9n12_usb_ops;
260 init.parent_names = &parent_name;
261 init.num_parents = 1;
262 init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
263
264 usb->hw.init = &init;
265 usb->regmap = regmap;
266
267 hw = &usb->hw;
268 ret = clk_hw_register(NULL, &usb->hw);
269 if (ret) {
270 kfree(usb);
271 hw = ERR_PTR(ret);
272 }
273
274 return hw;
275 }
276
277 static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
278 unsigned long parent_rate)
279 {
280 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
281 unsigned int pllbr;
282 u8 usbdiv;
283
284 regmap_read(usb->regmap, AT91_CKGR_PLLBR, &pllbr);
285
286 usbdiv = (pllbr & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
287 if (usb->divisors[usbdiv])
288 return parent_rate / usb->divisors[usbdiv];
289
290 return 0;
291 }
292
293 static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
294 unsigned long *parent_rate)
295 {
296 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
297 struct clk_hw *parent = clk_hw_get_parent(hw);
298 unsigned long bestrate = 0;
299 int bestdiff = -1;
300 unsigned long tmprate;
301 int tmpdiff;
302 int i = 0;
303
304 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
305 unsigned long tmp_parent_rate;
306
307 if (!usb->divisors[i])
308 continue;
309
310 tmp_parent_rate = rate * usb->divisors[i];
311 tmp_parent_rate = clk_hw_round_rate(parent, tmp_parent_rate);
312 tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
313 if (tmprate < rate)
314 tmpdiff = rate - tmprate;
315 else
316 tmpdiff = tmprate - rate;
317
318 if (bestdiff < 0 || bestdiff > tmpdiff) {
319 bestrate = tmprate;
320 bestdiff = tmpdiff;
321 *parent_rate = tmp_parent_rate;
322 }
323
324 if (!bestdiff)
325 break;
326 }
327
328 return bestrate;
329 }
330
331 static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
332 unsigned long parent_rate)
333 {
334 int i;
335 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
336 unsigned long div;
337
338 if (!rate)
339 return -EINVAL;
340
341 div = DIV_ROUND_CLOSEST(parent_rate, rate);
342
343 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
344 if (usb->divisors[i] == div) {
345 regmap_update_bits(usb->regmap, AT91_CKGR_PLLBR,
346 AT91_PMC_USBDIV,
347 i << RM9200_USB_DIV_SHIFT);
348
349 return 0;
350 }
351 }
352
353 return -EINVAL;
354 }
355
356 static const struct clk_ops at91rm9200_usb_ops = {
357 .recalc_rate = at91rm9200_clk_usb_recalc_rate,
358 .round_rate = at91rm9200_clk_usb_round_rate,
359 .set_rate = at91rm9200_clk_usb_set_rate,
360 };
361
362 struct clk_hw * __init
363 at91rm9200_clk_register_usb(struct regmap *regmap, const char *name,
364 const char *parent_name, const u32 *divisors)
365 {
366 struct at91rm9200_clk_usb *usb;
367 struct clk_hw *hw;
368 struct clk_init_data init;
369 int ret;
370
371 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
372 if (!usb)
373 return ERR_PTR(-ENOMEM);
374
375 init.name = name;
376 init.ops = &at91rm9200_usb_ops;
377 init.parent_names = &parent_name;
378 init.num_parents = 1;
379 init.flags = CLK_SET_RATE_PARENT;
380
381 usb->hw.init = &init;
382 usb->regmap = regmap;
383 memcpy(usb->divisors, divisors, sizeof(usb->divisors));
384
385 hw = &usb->hw;
386 ret = clk_hw_register(NULL, &usb->hw);
387 if (ret) {
388 kfree(usb);
389 hw = ERR_PTR(ret);
390 }
391
392 return hw;
393 }