]>
Commit | Line | Data |
---|---|---|
49fc825f SB |
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> | |
169f05e8 | 15 | #include <linux/module.h> |
49fc825f SB |
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" | |
50c6a503 | 22 | #include "clk-rcg.h" |
49fc825f SB |
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 | ||
50c6a503 SB |
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 | ||
293d2e97 GD |
47 | int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src) |
48 | { | |
497295af | 49 | int i, num_parents = clk_hw_get_num_parents(hw); |
293d2e97 GD |
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 | ||
5b6b7490 SB |
59 | struct regmap * |
60 | qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc) | |
49fc825f SB |
61 | { |
62 | void __iomem *base; | |
63 | struct resource *res; | |
5b6b7490 SB |
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 | { | |
49fc825f SB |
78 | int i, ret; |
79 | struct device *dev = &pdev->dev; | |
80 | struct clk *clk; | |
81 | struct clk_onecell_data *data; | |
82 | struct clk **clks; | |
49fc825f SB |
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 | ||
49fc825f SB |
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++) { | |
9ec2749b SB |
99 | if (!rclks[i]) { |
100 | clks[i] = ERR_PTR(-ENOENT); | |
49fc825f | 101 | continue; |
9ec2749b | 102 | } |
49fc825f SB |
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 | } | |
5b6b7490 SB |
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 | } | |
49fc825f SB |
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); | |
169f05e8 SB |
148 | |
149 | MODULE_LICENSE("GPL v2"); |