xref: /openbmc/linux/drivers/clk/qcom/common.c (revision 3932b9ca)
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-regmap.h"
22 #include "reset.h"
23 
24 struct qcom_cc {
25 	struct qcom_reset_controller reset;
26 	struct clk_onecell_data data;
27 	struct clk *clks[];
28 };
29 
30 struct regmap *
31 qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
32 {
33 	void __iomem *base;
34 	struct resource *res;
35 	struct device *dev = &pdev->dev;
36 
37 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
38 	base = devm_ioremap_resource(dev, res);
39 	if (IS_ERR(base))
40 		return ERR_CAST(base);
41 
42 	return devm_regmap_init_mmio(dev, base, desc->config);
43 }
44 EXPORT_SYMBOL_GPL(qcom_cc_map);
45 
46 int qcom_cc_really_probe(struct platform_device *pdev,
47 			 const struct qcom_cc_desc *desc, struct regmap *regmap)
48 {
49 	int i, ret;
50 	struct device *dev = &pdev->dev;
51 	struct clk *clk;
52 	struct clk_onecell_data *data;
53 	struct clk **clks;
54 	struct qcom_reset_controller *reset;
55 	struct qcom_cc *cc;
56 	size_t num_clks = desc->num_clks;
57 	struct clk_regmap **rclks = desc->clks;
58 
59 	cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks,
60 			  GFP_KERNEL);
61 	if (!cc)
62 		return -ENOMEM;
63 
64 	clks = cc->clks;
65 	data = &cc->data;
66 	data->clks = clks;
67 	data->clk_num = num_clks;
68 
69 	for (i = 0; i < num_clks; i++) {
70 		if (!rclks[i]) {
71 			clks[i] = ERR_PTR(-ENOENT);
72 			continue;
73 		}
74 		clk = devm_clk_register_regmap(dev, rclks[i]);
75 		if (IS_ERR(clk))
76 			return PTR_ERR(clk);
77 		clks[i] = clk;
78 	}
79 
80 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data);
81 	if (ret)
82 		return ret;
83 
84 	reset = &cc->reset;
85 	reset->rcdev.of_node = dev->of_node;
86 	reset->rcdev.ops = &qcom_reset_ops;
87 	reset->rcdev.owner = dev->driver->owner;
88 	reset->rcdev.nr_resets = desc->num_resets;
89 	reset->regmap = regmap;
90 	reset->reset_map = desc->resets;
91 	platform_set_drvdata(pdev, &reset->rcdev);
92 
93 	ret = reset_controller_register(&reset->rcdev);
94 	if (ret)
95 		of_clk_del_provider(dev->of_node);
96 
97 	return ret;
98 }
99 EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
100 
101 int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc)
102 {
103 	struct regmap *regmap;
104 
105 	regmap = qcom_cc_map(pdev, desc);
106 	if (IS_ERR(regmap))
107 		return PTR_ERR(regmap);
108 
109 	return qcom_cc_really_probe(pdev, desc, regmap);
110 }
111 EXPORT_SYMBOL_GPL(qcom_cc_probe);
112 
113 void qcom_cc_remove(struct platform_device *pdev)
114 {
115 	of_clk_del_provider(pdev->dev.of_node);
116 	reset_controller_unregister(platform_get_drvdata(pdev));
117 }
118 EXPORT_SYMBOL_GPL(qcom_cc_remove);
119