2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/regmap.h>
17 #include <linux/export.h>
19 #include "clk-regmap-divider.h"
21 static inline struct clk_regmap_div
*to_clk_regmap_div(struct clk_hw
*hw
)
23 return container_of(to_clk_regmap(hw
), struct clk_regmap_div
, clkr
);
26 static long div_round_rate(struct clk_hw
*hw
, unsigned long rate
,
29 struct clk_regmap_div
*divider
= to_clk_regmap_div(hw
);
31 return divider_round_rate(hw
, rate
, prate
, NULL
, divider
->width
,
32 CLK_DIVIDER_ROUND_CLOSEST
);
35 static int div_set_rate(struct clk_hw
*hw
, unsigned long rate
,
36 unsigned long parent_rate
)
38 struct clk_regmap_div
*divider
= to_clk_regmap_div(hw
);
39 struct clk_regmap
*clkr
= ÷r
->clkr
;
42 div
= divider_get_val(rate
, parent_rate
, NULL
, divider
->width
,
43 CLK_DIVIDER_ROUND_CLOSEST
);
45 return regmap_update_bits(clkr
->regmap
, divider
->reg
,
46 (BIT(divider
->width
) - 1) << divider
->shift
,
47 div
<< divider
->shift
);
50 static unsigned long div_recalc_rate(struct clk_hw
*hw
,
51 unsigned long parent_rate
)
53 struct clk_regmap_div
*divider
= to_clk_regmap_div(hw
);
54 struct clk_regmap
*clkr
= ÷r
->clkr
;
57 regmap_read(clkr
->regmap
, divider
->reg
, &div
);
58 div
>>= divider
->shift
;
59 div
&= BIT(divider
->width
) - 1;
61 return divider_recalc_rate(hw
, parent_rate
, div
, NULL
,
62 CLK_DIVIDER_ROUND_CLOSEST
);
65 const struct clk_ops clk_regmap_div_ops
= {
66 .round_rate
= div_round_rate
,
67 .set_rate
= div_set_rate
,
68 .recalc_rate
= div_recalc_rate
,
70 EXPORT_SYMBOL_GPL(clk_regmap_div_ops
);