1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
6 #include <linux/clk-provider.h>
7 #include <linux/clkdev.h>
8 #include <linux/clk/at91_pmc.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/regmap.h>
15 #define SAM9X5_USB_DIV_SHIFT 8
16 #define SAM9X5_USB_MAX_DIV 0xf
18 #define RM9200_USB_DIV_SHIFT 28
19 #define RM9200_USB_DIV_TAB_SIZE 4
21 #define SAM9X5_USBS_MASK GENMASK(0, 0)
22 #define SAM9X60_USBS_MASK GENMASK(1, 0)
24 struct at91sam9x5_clk_usb
{
26 struct regmap
*regmap
;
27 struct at91_clk_pms pms
;
32 #define to_at91sam9x5_clk_usb(hw) \
33 container_of(hw, struct at91sam9x5_clk_usb, hw)
35 struct at91rm9200_clk_usb
{
37 struct regmap
*regmap
;
41 #define to_at91rm9200_clk_usb(hw) \
42 container_of(hw, struct at91rm9200_clk_usb, hw)
44 static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw
*hw
,
45 unsigned long parent_rate
)
47 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
51 regmap_read(usb
->regmap
, AT91_PMC_USB
, &usbr
);
52 usbdiv
= (usbr
& AT91_PMC_OHCIUSBDIV
) >> SAM9X5_USB_DIV_SHIFT
;
54 return DIV_ROUND_CLOSEST(parent_rate
, (usbdiv
+ 1));
57 static int at91sam9x5_clk_usb_determine_rate(struct clk_hw
*hw
,
58 struct clk_rate_request
*req
)
60 struct clk_hw
*parent
;
61 long best_rate
= -EINVAL
;
62 unsigned long tmp_rate
;
67 for (i
= 0; i
< clk_hw_get_num_parents(hw
); i
++) {
70 parent
= clk_hw_get_parent_by_index(hw
, i
);
74 for (div
= 1; div
< SAM9X5_USB_MAX_DIV
+ 2; div
++) {
75 unsigned long tmp_parent_rate
;
77 tmp_parent_rate
= req
->rate
* div
;
78 tmp_parent_rate
= clk_hw_round_rate(parent
,
83 tmp_rate
= DIV_ROUND_CLOSEST(tmp_parent_rate
, div
);
84 if (tmp_rate
< req
->rate
)
85 tmp_diff
= req
->rate
- tmp_rate
;
87 tmp_diff
= tmp_rate
- req
->rate
;
89 if (best_diff
< 0 || best_diff
> tmp_diff
) {
92 req
->best_parent_rate
= tmp_parent_rate
;
93 req
->best_parent_hw
= parent
;
96 if (!best_diff
|| tmp_rate
< req
->rate
)
107 req
->rate
= best_rate
;
111 static int at91sam9x5_clk_usb_set_parent(struct clk_hw
*hw
, u8 index
)
113 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
115 if (index
>= usb
->num_parents
)
118 regmap_update_bits(usb
->regmap
, AT91_PMC_USB
, usb
->usbs_mask
, index
);
123 static u8
at91sam9x5_clk_usb_get_parent(struct clk_hw
*hw
)
125 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
128 regmap_read(usb
->regmap
, AT91_PMC_USB
, &usbr
);
130 return usbr
& usb
->usbs_mask
;
133 static int at91sam9x5_clk_usb_set_rate(struct clk_hw
*hw
, unsigned long rate
,
134 unsigned long parent_rate
)
136 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
142 div
= DIV_ROUND_CLOSEST(parent_rate
, rate
);
143 if (div
> SAM9X5_USB_MAX_DIV
+ 1 || !div
)
146 regmap_update_bits(usb
->regmap
, AT91_PMC_USB
, AT91_PMC_OHCIUSBDIV
,
147 (div
- 1) << SAM9X5_USB_DIV_SHIFT
);
152 static int at91sam9x5_usb_save_context(struct clk_hw
*hw
)
154 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
155 struct clk_hw
*parent_hw
= clk_hw_get_parent(hw
);
157 usb
->pms
.parent
= at91sam9x5_clk_usb_get_parent(hw
);
158 usb
->pms
.parent_rate
= clk_hw_get_rate(parent_hw
);
159 usb
->pms
.rate
= at91sam9x5_clk_usb_recalc_rate(hw
, usb
->pms
.parent_rate
);
164 static void at91sam9x5_usb_restore_context(struct clk_hw
*hw
)
166 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
169 ret
= at91sam9x5_clk_usb_set_parent(hw
, usb
->pms
.parent
);
173 at91sam9x5_clk_usb_set_rate(hw
, usb
->pms
.rate
, usb
->pms
.parent_rate
);
176 static const struct clk_ops at91sam9x5_usb_ops
= {
177 .recalc_rate
= at91sam9x5_clk_usb_recalc_rate
,
178 .determine_rate
= at91sam9x5_clk_usb_determine_rate
,
179 .get_parent
= at91sam9x5_clk_usb_get_parent
,
180 .set_parent
= at91sam9x5_clk_usb_set_parent
,
181 .set_rate
= at91sam9x5_clk_usb_set_rate
,
182 .save_context
= at91sam9x5_usb_save_context
,
183 .restore_context
= at91sam9x5_usb_restore_context
,
186 static int at91sam9n12_clk_usb_enable(struct clk_hw
*hw
)
188 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
190 regmap_update_bits(usb
->regmap
, AT91_PMC_USB
, AT91_PMC_USBS
,
196 static void at91sam9n12_clk_usb_disable(struct clk_hw
*hw
)
198 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
200 regmap_update_bits(usb
->regmap
, AT91_PMC_USB
, AT91_PMC_USBS
, 0);
203 static int at91sam9n12_clk_usb_is_enabled(struct clk_hw
*hw
)
205 struct at91sam9x5_clk_usb
*usb
= to_at91sam9x5_clk_usb(hw
);
208 regmap_read(usb
->regmap
, AT91_PMC_USB
, &usbr
);
210 return usbr
& AT91_PMC_USBS
;
213 static const struct clk_ops at91sam9n12_usb_ops
= {
214 .enable
= at91sam9n12_clk_usb_enable
,
215 .disable
= at91sam9n12_clk_usb_disable
,
216 .is_enabled
= at91sam9n12_clk_usb_is_enabled
,
217 .recalc_rate
= at91sam9x5_clk_usb_recalc_rate
,
218 .determine_rate
= at91sam9x5_clk_usb_determine_rate
,
219 .set_rate
= at91sam9x5_clk_usb_set_rate
,
222 static struct clk_hw
* __init
223 _at91sam9x5_clk_register_usb(struct regmap
*regmap
, const char *name
,
224 const char **parent_names
, u8 num_parents
,
227 struct at91sam9x5_clk_usb
*usb
;
229 struct clk_init_data init
;
232 usb
= kzalloc(sizeof(*usb
), GFP_KERNEL
);
234 return ERR_PTR(-ENOMEM
);
237 init
.ops
= &at91sam9x5_usb_ops
;
238 init
.parent_names
= parent_names
;
239 init
.num_parents
= num_parents
;
240 init
.flags
= CLK_SET_RATE_GATE
| CLK_SET_PARENT_GATE
|
243 usb
->hw
.init
= &init
;
244 usb
->regmap
= regmap
;
245 usb
->usbs_mask
= usbs_mask
;
246 usb
->num_parents
= num_parents
;
249 ret
= clk_hw_register(NULL
, &usb
->hw
);
258 struct clk_hw
* __init
259 at91sam9x5_clk_register_usb(struct regmap
*regmap
, const char *name
,
260 const char **parent_names
, u8 num_parents
)
262 return _at91sam9x5_clk_register_usb(regmap
, name
, parent_names
,
263 num_parents
, SAM9X5_USBS_MASK
);
266 struct clk_hw
* __init
267 sam9x60_clk_register_usb(struct regmap
*regmap
, const char *name
,
268 const char **parent_names
, u8 num_parents
)
270 return _at91sam9x5_clk_register_usb(regmap
, name
, parent_names
,
271 num_parents
, SAM9X60_USBS_MASK
);
274 struct clk_hw
* __init
275 at91sam9n12_clk_register_usb(struct regmap
*regmap
, const char *name
,
276 const char *parent_name
)
278 struct at91sam9x5_clk_usb
*usb
;
280 struct clk_init_data init
;
283 usb
= kzalloc(sizeof(*usb
), GFP_KERNEL
);
285 return ERR_PTR(-ENOMEM
);
288 init
.ops
= &at91sam9n12_usb_ops
;
289 init
.parent_names
= &parent_name
;
290 init
.num_parents
= 1;
291 init
.flags
= CLK_SET_RATE_GATE
| CLK_SET_RATE_PARENT
;
293 usb
->hw
.init
= &init
;
294 usb
->regmap
= regmap
;
297 ret
= clk_hw_register(NULL
, &usb
->hw
);
306 static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw
*hw
,
307 unsigned long parent_rate
)
309 struct at91rm9200_clk_usb
*usb
= to_at91rm9200_clk_usb(hw
);
313 regmap_read(usb
->regmap
, AT91_CKGR_PLLBR
, &pllbr
);
315 usbdiv
= (pllbr
& AT91_PMC_USBDIV
) >> RM9200_USB_DIV_SHIFT
;
316 if (usb
->divisors
[usbdiv
])
317 return parent_rate
/ usb
->divisors
[usbdiv
];
322 static long at91rm9200_clk_usb_round_rate(struct clk_hw
*hw
, unsigned long rate
,
323 unsigned long *parent_rate
)
325 struct at91rm9200_clk_usb
*usb
= to_at91rm9200_clk_usb(hw
);
326 struct clk_hw
*parent
= clk_hw_get_parent(hw
);
327 unsigned long bestrate
= 0;
329 unsigned long tmprate
;
333 for (i
= 0; i
< RM9200_USB_DIV_TAB_SIZE
; i
++) {
334 unsigned long tmp_parent_rate
;
336 if (!usb
->divisors
[i
])
339 tmp_parent_rate
= rate
* usb
->divisors
[i
];
340 tmp_parent_rate
= clk_hw_round_rate(parent
, tmp_parent_rate
);
341 tmprate
= DIV_ROUND_CLOSEST(tmp_parent_rate
, usb
->divisors
[i
]);
343 tmpdiff
= rate
- tmprate
;
345 tmpdiff
= tmprate
- rate
;
347 if (bestdiff
< 0 || bestdiff
> tmpdiff
) {
350 *parent_rate
= tmp_parent_rate
;
360 static int at91rm9200_clk_usb_set_rate(struct clk_hw
*hw
, unsigned long rate
,
361 unsigned long parent_rate
)
364 struct at91rm9200_clk_usb
*usb
= to_at91rm9200_clk_usb(hw
);
370 div
= DIV_ROUND_CLOSEST(parent_rate
, rate
);
372 for (i
= 0; i
< RM9200_USB_DIV_TAB_SIZE
; i
++) {
373 if (usb
->divisors
[i
] == div
) {
374 regmap_update_bits(usb
->regmap
, AT91_CKGR_PLLBR
,
376 i
<< RM9200_USB_DIV_SHIFT
);
385 static const struct clk_ops at91rm9200_usb_ops
= {
386 .recalc_rate
= at91rm9200_clk_usb_recalc_rate
,
387 .round_rate
= at91rm9200_clk_usb_round_rate
,
388 .set_rate
= at91rm9200_clk_usb_set_rate
,
391 struct clk_hw
* __init
392 at91rm9200_clk_register_usb(struct regmap
*regmap
, const char *name
,
393 const char *parent_name
, const u32
*divisors
)
395 struct at91rm9200_clk_usb
*usb
;
397 struct clk_init_data init
;
400 usb
= kzalloc(sizeof(*usb
), GFP_KERNEL
);
402 return ERR_PTR(-ENOMEM
);
405 init
.ops
= &at91rm9200_usb_ops
;
406 init
.parent_names
= &parent_name
;
407 init
.num_parents
= 1;
408 init
.flags
= CLK_SET_RATE_PARENT
;
410 usb
->hw
.init
= &init
;
411 usb
->regmap
= regmap
;
412 memcpy(usb
->divisors
, divisors
, sizeof(usb
->divisors
));
415 ret
= clk_hw_register(NULL
, &usb
->hw
);