Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / sprd / div.c
blob7621a1d1ab9c1a366829142ca9f0ac71eb216f89
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Spreadtrum divider clock driver
4 //
5 // Copyright (C) 2017 Spreadtrum, Inc.
6 // Author: Chunyan Zhang <chunyan.zhang@spreadtrum.com>
8 #include <linux/clk-provider.h>
10 #include "div.h"
12 long sprd_div_helper_round_rate(struct sprd_clk_common *common,
13 const struct sprd_div_internal *div,
14 unsigned long rate,
15 unsigned long *parent_rate)
17 return divider_round_rate(&common->hw, rate, parent_rate,
18 NULL, div->width, 0);
20 EXPORT_SYMBOL_GPL(sprd_div_helper_round_rate);
22 static long sprd_div_round_rate(struct clk_hw *hw, unsigned long rate,
23 unsigned long *parent_rate)
25 struct sprd_div *cd = hw_to_sprd_div(hw);
27 return sprd_div_helper_round_rate(&cd->common, &cd->div,
28 rate, parent_rate);
31 unsigned long sprd_div_helper_recalc_rate(struct sprd_clk_common *common,
32 const struct sprd_div_internal *div,
33 unsigned long parent_rate)
35 unsigned long val;
36 unsigned int reg;
38 regmap_read(common->regmap, common->reg, &reg);
39 val = reg >> div->shift;
40 val &= (1 << div->width) - 1;
42 return divider_recalc_rate(&common->hw, parent_rate, val, NULL, 0,
43 div->width);
45 EXPORT_SYMBOL_GPL(sprd_div_helper_recalc_rate);
47 static unsigned long sprd_div_recalc_rate(struct clk_hw *hw,
48 unsigned long parent_rate)
50 struct sprd_div *cd = hw_to_sprd_div(hw);
52 return sprd_div_helper_recalc_rate(&cd->common, &cd->div, parent_rate);
55 int sprd_div_helper_set_rate(const struct sprd_clk_common *common,
56 const struct sprd_div_internal *div,
57 unsigned long rate,
58 unsigned long parent_rate)
60 unsigned long val;
61 unsigned int reg;
63 val = divider_get_val(rate, parent_rate, NULL,
64 div->width, 0);
66 regmap_read(common->regmap, common->reg, &reg);
67 reg &= ~GENMASK(div->width + div->shift - 1, div->shift);
69 regmap_write(common->regmap, common->reg,
70 reg | (val << div->shift));
72 return 0;
75 EXPORT_SYMBOL_GPL(sprd_div_helper_set_rate);
77 static int sprd_div_set_rate(struct clk_hw *hw, unsigned long rate,
78 unsigned long parent_rate)
80 struct sprd_div *cd = hw_to_sprd_div(hw);
82 return sprd_div_helper_set_rate(&cd->common, &cd->div,
83 rate, parent_rate);
86 const struct clk_ops sprd_div_ops = {
87 .recalc_rate = sprd_div_recalc_rate,
88 .round_rate = sprd_div_round_rate,
89 .set_rate = sprd_div_set_rate,
91 EXPORT_SYMBOL_GPL(sprd_div_ops);