rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 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 | #include <linux/of.h> |
| 21 | |
| 22 | #include "common.h" |
| 23 | #include "clk-rcg.h" |
| 24 | #include "clk-regmap.h" |
| 25 | #include "reset.h" |
| 26 | #include "gdsc.h" |
| 27 | |
| 28 | struct qcom_cc { |
| 29 | struct qcom_reset_controller reset; |
| 30 | struct clk_regmap **rclks; |
| 31 | size_t num_rclks; |
| 32 | }; |
| 33 | |
| 34 | const |
| 35 | struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate) |
| 36 | { |
| 37 | if (!f) |
| 38 | return NULL; |
| 39 | |
| 40 | if (!f->freq) |
| 41 | return f; |
| 42 | |
| 43 | for (; f->freq; f++) |
| 44 | if (rate <= f->freq) |
| 45 | return f; |
| 46 | |
| 47 | /* Default to our fastest rate */ |
| 48 | return f - 1; |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(qcom_find_freq); |
| 51 | |
| 52 | const struct freq_tbl *qcom_find_freq_floor(const struct freq_tbl *f, |
| 53 | unsigned long rate) |
| 54 | { |
| 55 | const struct freq_tbl *best = NULL; |
| 56 | |
| 57 | for ( ; f->freq; f++) { |
| 58 | if (rate >= f->freq) |
| 59 | best = f; |
| 60 | else |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | return best; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(qcom_find_freq_floor); |
| 67 | |
| 68 | int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src) |
| 69 | { |
| 70 | int i, num_parents = clk_hw_get_num_parents(hw); |
| 71 | |
| 72 | for (i = 0; i < num_parents; i++) |
| 73 | if (src == map[i].src) |
| 74 | return i; |
| 75 | |
| 76 | return -ENOENT; |
| 77 | } |
| 78 | EXPORT_SYMBOL_GPL(qcom_find_src_index); |
| 79 | |
| 80 | struct regmap * |
| 81 | qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc) |
| 82 | { |
| 83 | void __iomem *base; |
| 84 | struct resource *res; |
| 85 | struct device *dev = &pdev->dev; |
| 86 | |
| 87 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 88 | base = devm_ioremap_resource(dev, res); |
| 89 | if (IS_ERR(base)) |
| 90 | return ERR_CAST(base); |
| 91 | |
| 92 | return devm_regmap_init_mmio(dev, base, desc->config); |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(qcom_cc_map); |
| 95 | |
| 96 | void |
| 97 | qcom_pll_set_fsm_mode(struct regmap *map, u32 reg, u8 bias_count, u8 lock_count) |
| 98 | { |
| 99 | u32 val; |
| 100 | u32 mask; |
| 101 | |
| 102 | /* De-assert reset to FSM */ |
| 103 | regmap_update_bits(map, reg, PLL_VOTE_FSM_RESET, 0); |
| 104 | |
| 105 | /* Program bias count and lock count */ |
| 106 | val = bias_count << PLL_BIAS_COUNT_SHIFT | |
| 107 | lock_count << PLL_LOCK_COUNT_SHIFT; |
| 108 | mask = PLL_BIAS_COUNT_MASK << PLL_BIAS_COUNT_SHIFT; |
| 109 | mask |= PLL_LOCK_COUNT_MASK << PLL_LOCK_COUNT_SHIFT; |
| 110 | regmap_update_bits(map, reg, mask, val); |
| 111 | |
| 112 | /* Enable PLL FSM voting */ |
| 113 | regmap_update_bits(map, reg, PLL_VOTE_FSM_ENA, PLL_VOTE_FSM_ENA); |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(qcom_pll_set_fsm_mode); |
| 116 | |
| 117 | static void qcom_cc_del_clk_provider(void *data) |
| 118 | { |
| 119 | of_clk_del_provider(data); |
| 120 | } |
| 121 | |
| 122 | static void qcom_cc_reset_unregister(void *data) |
| 123 | { |
| 124 | reset_controller_unregister(data); |
| 125 | } |
| 126 | |
| 127 | static void qcom_cc_gdsc_unregister(void *data) |
| 128 | { |
| 129 | gdsc_unregister(data); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Backwards compatibility with old DTs. Register a pass-through factor 1/1 |
| 134 | * clock to translate 'path' clk into 'name' clk and register the 'path' |
| 135 | * clk as a fixed rate clock if it isn't present. |
| 136 | */ |
| 137 | static int _qcom_cc_register_board_clk(struct device *dev, const char *path, |
| 138 | const char *name, unsigned long rate, |
| 139 | bool add_factor) |
| 140 | { |
| 141 | struct device_node *node = NULL; |
| 142 | struct device_node *clocks_node; |
| 143 | struct clk_fixed_factor *factor; |
| 144 | struct clk_fixed_rate *fixed; |
| 145 | struct clk_init_data init_data = { }; |
| 146 | int ret; |
| 147 | |
| 148 | clocks_node = of_find_node_by_path("/clocks"); |
| 149 | if (clocks_node) { |
| 150 | node = of_get_child_by_name(clocks_node, path); |
| 151 | of_node_put(clocks_node); |
| 152 | } |
| 153 | |
| 154 | if (!node) { |
| 155 | fixed = devm_kzalloc(dev, sizeof(*fixed), GFP_KERNEL); |
| 156 | if (!fixed) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | fixed->fixed_rate = rate; |
| 160 | fixed->hw.init = &init_data; |
| 161 | |
| 162 | init_data.name = path; |
| 163 | init_data.ops = &clk_fixed_rate_ops; |
| 164 | |
| 165 | ret = devm_clk_hw_register(dev, &fixed->hw); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | } |
| 169 | of_node_put(node); |
| 170 | |
| 171 | if (add_factor) { |
| 172 | factor = devm_kzalloc(dev, sizeof(*factor), GFP_KERNEL); |
| 173 | if (!factor) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | factor->mult = factor->div = 1; |
| 177 | factor->hw.init = &init_data; |
| 178 | |
| 179 | init_data.name = name; |
| 180 | init_data.parent_names = &path; |
| 181 | init_data.num_parents = 1; |
| 182 | init_data.flags = 0; |
| 183 | init_data.ops = &clk_fixed_factor_ops; |
| 184 | |
| 185 | ret = devm_clk_hw_register(dev, &factor->hw); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int qcom_cc_register_board_clk(struct device *dev, const char *path, |
| 194 | const char *name, unsigned long rate) |
| 195 | { |
| 196 | bool add_factor = true; |
| 197 | |
| 198 | /* |
| 199 | * TODO: The RPM clock driver currently does not support the xo clock. |
| 200 | * When xo is added to the RPM clock driver, we should change this |
| 201 | * function to skip registration of xo factor clocks. |
| 202 | */ |
| 203 | |
| 204 | return _qcom_cc_register_board_clk(dev, path, name, rate, add_factor); |
| 205 | } |
| 206 | EXPORT_SYMBOL_GPL(qcom_cc_register_board_clk); |
| 207 | |
| 208 | int qcom_cc_register_sleep_clk(struct device *dev) |
| 209 | { |
| 210 | return _qcom_cc_register_board_clk(dev, "sleep_clk", "sleep_clk_src", |
| 211 | 32768, true); |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(qcom_cc_register_sleep_clk); |
| 214 | |
| 215 | static struct clk_hw *qcom_cc_clk_hw_get(struct of_phandle_args *clkspec, |
| 216 | void *data) |
| 217 | { |
| 218 | struct qcom_cc *cc = data; |
| 219 | unsigned int idx = clkspec->args[0]; |
| 220 | |
| 221 | if (idx >= cc->num_rclks) { |
| 222 | pr_err("%s: invalid index %u\n", __func__, idx); |
| 223 | return ERR_PTR(-EINVAL); |
| 224 | } |
| 225 | |
| 226 | return cc->rclks[idx] ? &cc->rclks[idx]->hw : ERR_PTR(-ENOENT); |
| 227 | } |
| 228 | |
| 229 | int qcom_cc_really_probe(struct platform_device *pdev, |
| 230 | const struct qcom_cc_desc *desc, struct regmap *regmap) |
| 231 | { |
| 232 | int i, ret; |
| 233 | struct device *dev = &pdev->dev; |
| 234 | struct qcom_reset_controller *reset; |
| 235 | struct qcom_cc *cc; |
| 236 | struct gdsc_desc *scd; |
| 237 | size_t num_clks = desc->num_clks; |
| 238 | struct clk_regmap **rclks = desc->clks; |
| 239 | |
| 240 | cc = devm_kzalloc(dev, sizeof(*cc), GFP_KERNEL); |
| 241 | if (!cc) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | cc->rclks = rclks; |
| 245 | cc->num_rclks = num_clks; |
| 246 | |
| 247 | for (i = 0; i < num_clks; i++) { |
| 248 | if (!rclks[i]) |
| 249 | continue; |
| 250 | |
| 251 | ret = devm_clk_register_regmap(dev, rclks[i]); |
| 252 | if (ret) |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | ret = of_clk_add_hw_provider(dev->of_node, qcom_cc_clk_hw_get, cc); |
| 257 | if (ret) |
| 258 | return ret; |
| 259 | |
| 260 | ret = devm_add_action_or_reset(dev, qcom_cc_del_clk_provider, |
| 261 | pdev->dev.of_node); |
| 262 | |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | |
| 266 | reset = &cc->reset; |
| 267 | reset->rcdev.of_node = dev->of_node; |
| 268 | reset->rcdev.ops = &qcom_reset_ops; |
| 269 | reset->rcdev.owner = dev->driver->owner; |
| 270 | reset->rcdev.nr_resets = desc->num_resets; |
| 271 | reset->regmap = regmap; |
| 272 | reset->reset_map = desc->resets; |
| 273 | |
| 274 | ret = reset_controller_register(&reset->rcdev); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
| 278 | ret = devm_add_action_or_reset(dev, qcom_cc_reset_unregister, |
| 279 | &reset->rcdev); |
| 280 | |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | |
| 284 | if (desc->gdscs && desc->num_gdscs) { |
| 285 | scd = devm_kzalloc(dev, sizeof(*scd), GFP_KERNEL); |
| 286 | if (!scd) |
| 287 | return -ENOMEM; |
| 288 | scd->dev = dev; |
| 289 | scd->scs = desc->gdscs; |
| 290 | scd->num = desc->num_gdscs; |
| 291 | ret = gdsc_register(scd, &reset->rcdev, regmap); |
| 292 | if (ret) |
| 293 | return ret; |
| 294 | ret = devm_add_action_or_reset(dev, qcom_cc_gdsc_unregister, |
| 295 | scd); |
| 296 | if (ret) |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | EXPORT_SYMBOL_GPL(qcom_cc_really_probe); |
| 303 | |
| 304 | int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc) |
| 305 | { |
| 306 | struct regmap *regmap; |
| 307 | |
| 308 | regmap = qcom_cc_map(pdev, desc); |
| 309 | if (IS_ERR(regmap)) |
| 310 | return PTR_ERR(regmap); |
| 311 | |
| 312 | return qcom_cc_really_probe(pdev, desc, regmap); |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(qcom_cc_probe); |
| 315 | |
| 316 | MODULE_LICENSE("GPL v2"); |