blob: 2dedceefd21d84464f4733bdfa36fda1f3489a2b [file] [log] [blame]
Stephen Boyd49fc8252014-03-21 17:59:37 -07001/*
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>
Stephen Boyd169f05e82015-07-24 11:55:42 -070015#include <linux/module.h>
Stephen Boyd49fc8252014-03-21 17:59:37 -070016#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"
Stephen Boyd50c6a502014-09-04 13:21:50 -070022#include "clk-rcg.h"
Stephen Boyd49fc8252014-03-21 17:59:37 -070023#include "clk-regmap.h"
24#include "reset.h"
25
26struct qcom_cc {
27 struct qcom_reset_controller reset;
28 struct clk_onecell_data data;
29 struct clk *clks[];
30};
31
Stephen Boyd50c6a502014-09-04 13:21:50 -070032const
33struct 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}
45EXPORT_SYMBOL_GPL(qcom_find_freq);
46
Georgi Djakov293d2e972015-03-20 18:30:26 +020047int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src)
48{
Stephen Boyd497295a2015-06-25 16:53:23 -070049 int i, num_parents = clk_hw_get_num_parents(hw);
Georgi Djakov293d2e972015-03-20 18:30:26 +020050
51 for (i = 0; i < num_parents; i++)
52 if (src == map[i].src)
53 return i;
54
55 return -ENOENT;
56}
57EXPORT_SYMBOL_GPL(qcom_find_src_index);
58
Stephen Boyd5b6b7492014-07-15 14:59:21 -070059struct regmap *
60qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc)
Stephen Boyd49fc8252014-03-21 17:59:37 -070061{
62 void __iomem *base;
63 struct resource *res;
Stephen Boyd5b6b7492014-07-15 14:59:21 -070064 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}
73EXPORT_SYMBOL_GPL(qcom_cc_map);
74
75int qcom_cc_really_probe(struct platform_device *pdev,
76 const struct qcom_cc_desc *desc, struct regmap *regmap)
77{
Stephen Boyd49fc8252014-03-21 17:59:37 -070078 int i, ret;
79 struct device *dev = &pdev->dev;
80 struct clk *clk;
81 struct clk_onecell_data *data;
82 struct clk **clks;
Stephen Boyd49fc8252014-03-21 17:59:37 -070083 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
Stephen Boyd49fc8252014-03-21 17:59:37 -070088 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++) {
Stephen Boyd9ec27492014-05-16 16:07:14 -070099 if (!rclks[i]) {
100 clks[i] = ERR_PTR(-ENOENT);
Stephen Boyd49fc8252014-03-21 17:59:37 -0700101 continue;
Stephen Boyd9ec27492014-05-16 16:07:14 -0700102 }
Stephen Boyd49fc8252014-03-21 17:59:37 -0700103 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}
Stephen Boyd5b6b7492014-07-15 14:59:21 -0700128EXPORT_SYMBOL_GPL(qcom_cc_really_probe);
129
130int 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}
Stephen Boyd49fc8252014-03-21 17:59:37 -0700140EXPORT_SYMBOL_GPL(qcom_cc_probe);
141
142void 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}
147EXPORT_SYMBOL_GPL(qcom_cc_remove);
Stephen Boyd169f05e82015-07-24 11:55:42 -0700148
149MODULE_LICENSE("GPL v2");