xref: /linux/drivers/clk/qcom/common.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
3  *
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.
7  *
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.
12  */
13 
14 #include <linux/export.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk-provider.h>
19 #include <linux/reset-controller.h>
20 
21 #include "common.h"
22 #include "clk-rcg.h"
23 #include "clk-regmap.h"
24 #include "reset.h"
25 
26 struct qcom_cc {
27 	struct qcom_reset_controller reset;
28 	struct clk_onecell_data data;
29 	struct clk *clks[];
30 };
31 
32 const
33 struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate)
34 {
35 	if (!f)
36 		return NULL;
37 
38 	for (; f->freq; f++)
39 		if (rate <= f->freq)
40 			return f;
41 
42 	/* Default to our fastest rate */
43 	return f - 1;
44 }
45 EXPORT_SYMBOL_GPL(qcom_find_freq);
46 
47 int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src)
48 {
49 	int i, num_parents = clk_hw_get_num_parents(hw);
50 
51 	for (i = 0; i < num_parents; i++)
52 		if (src == map[i].src)
53 			return i;
54 
55 	return -ENOENT;
56 }
57 EXPORT_SYMBOL_GPL(qcom_find_src_index);
58 
59 struct regmap *
60 qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
61 {
62 	void __iomem *base;
63 	struct resource *res;
64 	struct device *dev = &pdev->dev;
65 
66 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
67 	base = devm_ioremap_resource(dev, res);
68 	if (IS_ERR(base))
69 		return ERR_CAST(base);
70 
71 	return devm_regmap_init_mmio(dev, base, desc->config);
72 }
73 EXPORT_SYMBOL_GPL(qcom_cc_map);
74 
75 int qcom_cc_really_probe(struct platform_device *pdev,
76 			 const struct qcom_cc_desc *desc, struct regmap *regmap)
77 {
78 	int i, ret;
79 	struct device *dev = &pdev->dev;
80 	struct clk *clk;
81 	struct clk_onecell_data *data;
82 	struct clk **clks;
83 	struct qcom_reset_controller *reset;
84 	struct qcom_cc *cc;
85 	size_t num_clks = desc->num_clks;
86 	struct clk_regmap **rclks = desc->clks;
87 
88 	cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks,
89 			  GFP_KERNEL);
90 	if (!cc)
91 		return -ENOMEM;
92 
93 	clks = cc->clks;
94 	data = &cc->data;
95 	data->clks = clks;
96 	data->clk_num = num_clks;
97 
98 	for (i = 0; i < num_clks; i++) {
99 		if (!rclks[i]) {
100 			clks[i] = ERR_PTR(-ENOENT);
101 			continue;
102 		}
103 		clk = devm_clk_register_regmap(dev, rclks[i]);
104 		if (IS_ERR(clk))
105 			return PTR_ERR(clk);
106 		clks[i] = clk;
107 	}
108 
109 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data);
110 	if (ret)
111 		return ret;
112 
113 	reset = &cc->reset;
114 	reset->rcdev.of_node = dev->of_node;
115 	reset->rcdev.ops = &qcom_reset_ops;
116 	reset->rcdev.owner = dev->driver->owner;
117 	reset->rcdev.nr_resets = desc->num_resets;
118 	reset->regmap = regmap;
119 	reset->reset_map = desc->resets;
120 	platform_set_drvdata(pdev, &reset->rcdev);
121 
122 	ret = reset_controller_register(&reset->rcdev);
123 	if (ret)
124 		of_clk_del_provider(dev->of_node);
125 
126 	return ret;
127 }
128 EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
129 
130 int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc)
131 {
132 	struct regmap *regmap;
133 
134 	regmap = qcom_cc_map(pdev, desc);
135 	if (IS_ERR(regmap))
136 		return PTR_ERR(regmap);
137 
138 	return qcom_cc_really_probe(pdev, desc, regmap);
139 }
140 EXPORT_SYMBOL_GPL(qcom_cc_probe);
141 
142 void qcom_cc_remove(struct platform_device *pdev)
143 {
144 	of_clk_del_provider(pdev->dev.of_node);
145 	reset_controller_unregister(platform_get_drvdata(pdev));
146 }
147 EXPORT_SYMBOL_GPL(qcom_cc_remove);
148 
149 MODULE_LICENSE("GPL v2");
150