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