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