1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
6 #include <linux/kernel.h>
7 #include <linux/bitops.h>
8 #include <linux/regmap.h>
9 #include <linux/export.h>
11 #include "clk-regmap-divider.h"
13 static inline struct clk_regmap_div
*to_clk_regmap_div(struct clk_hw
*hw
)
15 return container_of(to_clk_regmap(hw
), struct clk_regmap_div
, clkr
);
18 static long div_round_ro_rate(struct clk_hw
*hw
, unsigned long rate
,
21 struct clk_regmap_div
*divider
= to_clk_regmap_div(hw
);
22 struct clk_regmap
*clkr
= ÷r
->clkr
;
25 regmap_read(clkr
->regmap
, divider
->reg
, &val
);
26 val
>>= divider
->shift
;
27 val
&= BIT(divider
->width
) - 1;
29 return divider_ro_round_rate(hw
, rate
, prate
, NULL
, divider
->width
,
30 CLK_DIVIDER_ROUND_CLOSEST
, val
);
33 static long div_round_rate(struct clk_hw
*hw
, unsigned long rate
,
36 struct clk_regmap_div
*divider
= to_clk_regmap_div(hw
);
38 return divider_round_rate(hw
, rate
, prate
, NULL
, divider
->width
,
39 CLK_DIVIDER_ROUND_CLOSEST
);
42 static int div_set_rate(struct clk_hw
*hw
, unsigned long rate
,
43 unsigned long parent_rate
)
45 struct clk_regmap_div
*divider
= to_clk_regmap_div(hw
);
46 struct clk_regmap
*clkr
= ÷r
->clkr
;
49 div
= divider_get_val(rate
, parent_rate
, NULL
, divider
->width
,
50 CLK_DIVIDER_ROUND_CLOSEST
);
52 return regmap_update_bits(clkr
->regmap
, divider
->reg
,
53 (BIT(divider
->width
) - 1) << divider
->shift
,
54 div
<< divider
->shift
);
57 static unsigned long div_recalc_rate(struct clk_hw
*hw
,
58 unsigned long parent_rate
)
60 struct clk_regmap_div
*divider
= to_clk_regmap_div(hw
);
61 struct clk_regmap
*clkr
= ÷r
->clkr
;
64 regmap_read(clkr
->regmap
, divider
->reg
, &div
);
65 div
>>= divider
->shift
;
66 div
&= BIT(divider
->width
) - 1;
68 return divider_recalc_rate(hw
, parent_rate
, div
, NULL
,
69 CLK_DIVIDER_ROUND_CLOSEST
, divider
->width
);
72 const struct clk_ops clk_regmap_div_ops
= {
73 .round_rate
= div_round_rate
,
74 .set_rate
= div_set_rate
,
75 .recalc_rate
= div_recalc_rate
,
77 EXPORT_SYMBOL_GPL(clk_regmap_div_ops
);
79 const struct clk_ops clk_regmap_div_ro_ops
= {
80 .round_rate
= div_round_ro_rate
,
81 .recalc_rate
= div_recalc_rate
,
83 EXPORT_SYMBOL_GPL(clk_regmap_div_ro_ops
);