Linux 4.19.133
[linux/fpc-iii.git] / drivers / clk / qcom / common.c
blobbfb6d6065a90c16bd9ac43f74365047562e0c2c9
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
4 */
6 #include <linux/export.h>
7 #include <linux/module.h>
8 #include <linux/regmap.h>
9 #include <linux/platform_device.h>
10 #include <linux/clk-provider.h>
11 #include <linux/reset-controller.h>
12 #include <linux/of.h>
14 #include "common.h"
15 #include "clk-rcg.h"
16 #include "clk-regmap.h"
17 #include "reset.h"
18 #include "gdsc.h"
20 struct qcom_cc {
21 struct qcom_reset_controller reset;
22 struct clk_regmap **rclks;
23 size_t num_rclks;
26 const
27 struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate)
29 if (!f)
30 return NULL;
32 if (!f->freq)
33 return f;
35 for (; f->freq; f++)
36 if (rate <= f->freq)
37 return f;
39 /* Default to our fastest rate */
40 return f - 1;
42 EXPORT_SYMBOL_GPL(qcom_find_freq);
44 const struct freq_tbl *qcom_find_freq_floor(const struct freq_tbl *f,
45 unsigned long rate)
47 const struct freq_tbl *best = NULL;
49 for ( ; f->freq; f++) {
50 if (rate >= f->freq)
51 best = f;
52 else
53 break;
56 return best;
58 EXPORT_SYMBOL_GPL(qcom_find_freq_floor);
60 int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src)
62 int i, num_parents = clk_hw_get_num_parents(hw);
64 for (i = 0; i < num_parents; i++)
65 if (src == map[i].src)
66 return i;
68 return -ENOENT;
70 EXPORT_SYMBOL_GPL(qcom_find_src_index);
72 struct regmap *
73 qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
75 void __iomem *base;
76 struct resource *res;
77 struct device *dev = &pdev->dev;
79 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
80 base = devm_ioremap_resource(dev, res);
81 if (IS_ERR(base))
82 return ERR_CAST(base);
84 return devm_regmap_init_mmio(dev, base, desc->config);
86 EXPORT_SYMBOL_GPL(qcom_cc_map);
88 void
89 qcom_pll_set_fsm_mode(struct regmap *map, u32 reg, u8 bias_count, u8 lock_count)
91 u32 val;
92 u32 mask;
94 /* De-assert reset to FSM */
95 regmap_update_bits(map, reg, PLL_VOTE_FSM_RESET, 0);
97 /* Program bias count and lock count */
98 val = bias_count << PLL_BIAS_COUNT_SHIFT |
99 lock_count << PLL_LOCK_COUNT_SHIFT;
100 mask = PLL_BIAS_COUNT_MASK << PLL_BIAS_COUNT_SHIFT;
101 mask |= PLL_LOCK_COUNT_MASK << PLL_LOCK_COUNT_SHIFT;
102 regmap_update_bits(map, reg, mask, val);
104 /* Enable PLL FSM voting */
105 regmap_update_bits(map, reg, PLL_VOTE_FSM_ENA, PLL_VOTE_FSM_ENA);
107 EXPORT_SYMBOL_GPL(qcom_pll_set_fsm_mode);
109 static void qcom_cc_gdsc_unregister(void *data)
111 gdsc_unregister(data);
115 * Backwards compatibility with old DTs. Register a pass-through factor 1/1
116 * clock to translate 'path' clk into 'name' clk and register the 'path'
117 * clk as a fixed rate clock if it isn't present.
119 static int _qcom_cc_register_board_clk(struct device *dev, const char *path,
120 const char *name, unsigned long rate,
121 bool add_factor)
123 struct device_node *node = NULL;
124 struct device_node *clocks_node;
125 struct clk_fixed_factor *factor;
126 struct clk_fixed_rate *fixed;
127 struct clk_init_data init_data = { };
128 int ret;
130 clocks_node = of_find_node_by_path("/clocks");
131 if (clocks_node) {
132 node = of_get_child_by_name(clocks_node, path);
133 of_node_put(clocks_node);
136 if (!node) {
137 fixed = devm_kzalloc(dev, sizeof(*fixed), GFP_KERNEL);
138 if (!fixed)
139 return -EINVAL;
141 fixed->fixed_rate = rate;
142 fixed->hw.init = &init_data;
144 init_data.name = path;
145 init_data.ops = &clk_fixed_rate_ops;
147 ret = devm_clk_hw_register(dev, &fixed->hw);
148 if (ret)
149 return ret;
151 of_node_put(node);
153 if (add_factor) {
154 factor = devm_kzalloc(dev, sizeof(*factor), GFP_KERNEL);
155 if (!factor)
156 return -EINVAL;
158 factor->mult = factor->div = 1;
159 factor->hw.init = &init_data;
161 init_data.name = name;
162 init_data.parent_names = &path;
163 init_data.num_parents = 1;
164 init_data.flags = 0;
165 init_data.ops = &clk_fixed_factor_ops;
167 ret = devm_clk_hw_register(dev, &factor->hw);
168 if (ret)
169 return ret;
172 return 0;
175 int qcom_cc_register_board_clk(struct device *dev, const char *path,
176 const char *name, unsigned long rate)
178 bool add_factor = true;
181 * TODO: The RPM clock driver currently does not support the xo clock.
182 * When xo is added to the RPM clock driver, we should change this
183 * function to skip registration of xo factor clocks.
186 return _qcom_cc_register_board_clk(dev, path, name, rate, add_factor);
188 EXPORT_SYMBOL_GPL(qcom_cc_register_board_clk);
190 int qcom_cc_register_sleep_clk(struct device *dev)
192 return _qcom_cc_register_board_clk(dev, "sleep_clk", "sleep_clk_src",
193 32768, true);
195 EXPORT_SYMBOL_GPL(qcom_cc_register_sleep_clk);
197 static struct clk_hw *qcom_cc_clk_hw_get(struct of_phandle_args *clkspec,
198 void *data)
200 struct qcom_cc *cc = data;
201 unsigned int idx = clkspec->args[0];
203 if (idx >= cc->num_rclks) {
204 pr_err("%s: invalid index %u\n", __func__, idx);
205 return ERR_PTR(-EINVAL);
208 return cc->rclks[idx] ? &cc->rclks[idx]->hw : ERR_PTR(-ENOENT);
211 int qcom_cc_really_probe(struct platform_device *pdev,
212 const struct qcom_cc_desc *desc, struct regmap *regmap)
214 int i, ret;
215 struct device *dev = &pdev->dev;
216 struct qcom_reset_controller *reset;
217 struct qcom_cc *cc;
218 struct gdsc_desc *scd;
219 size_t num_clks = desc->num_clks;
220 struct clk_regmap **rclks = desc->clks;
222 cc = devm_kzalloc(dev, sizeof(*cc), GFP_KERNEL);
223 if (!cc)
224 return -ENOMEM;
226 reset = &cc->reset;
227 reset->rcdev.of_node = dev->of_node;
228 reset->rcdev.ops = &qcom_reset_ops;
229 reset->rcdev.owner = dev->driver->owner;
230 reset->rcdev.nr_resets = desc->num_resets;
231 reset->regmap = regmap;
232 reset->reset_map = desc->resets;
234 ret = devm_reset_controller_register(dev, &reset->rcdev);
235 if (ret)
236 return ret;
238 if (desc->gdscs && desc->num_gdscs) {
239 scd = devm_kzalloc(dev, sizeof(*scd), GFP_KERNEL);
240 if (!scd)
241 return -ENOMEM;
242 scd->dev = dev;
243 scd->scs = desc->gdscs;
244 scd->num = desc->num_gdscs;
245 ret = gdsc_register(scd, &reset->rcdev, regmap);
246 if (ret)
247 return ret;
248 ret = devm_add_action_or_reset(dev, qcom_cc_gdsc_unregister,
249 scd);
250 if (ret)
251 return ret;
254 cc->rclks = rclks;
255 cc->num_rclks = num_clks;
257 for (i = 0; i < num_clks; i++) {
258 if (!rclks[i])
259 continue;
261 ret = devm_clk_register_regmap(dev, rclks[i]);
262 if (ret)
263 return ret;
266 ret = devm_of_clk_add_hw_provider(dev, qcom_cc_clk_hw_get, cc);
267 if (ret)
268 return ret;
270 return 0;
272 EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
274 int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc)
276 struct regmap *regmap;
278 regmap = qcom_cc_map(pdev, desc);
279 if (IS_ERR(regmap))
280 return PTR_ERR(regmap);
282 return qcom_cc_really_probe(pdev, desc, regmap);
284 EXPORT_SYMBOL_GPL(qcom_cc_probe);
286 MODULE_LICENSE("GPL v2");