Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [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> |
Stephen Boyd | 169f05e8 | 2015-07-24 11:55:42 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 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" |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 22 | #include "clk-rcg.h" |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 23 | #include "clk-regmap.h" |
| 24 | #include "reset.h" |
Rajendra Nayak | 5e5cc24 | 2015-08-06 16:07:43 +0530 | [diff] [blame] | 25 | #include "gdsc.h" |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 26 | |
| 27 | struct qcom_cc { |
| 28 | struct qcom_reset_controller reset; |
| 29 | struct clk_onecell_data data; |
| 30 | struct clk *clks[]; |
| 31 | }; |
| 32 | |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 33 | const |
| 34 | struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate) |
| 35 | { |
| 36 | if (!f) |
| 37 | return NULL; |
| 38 | |
| 39 | for (; f->freq; f++) |
| 40 | if (rate <= f->freq) |
| 41 | return f; |
| 42 | |
| 43 | /* Default to our fastest rate */ |
| 44 | return f - 1; |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(qcom_find_freq); |
| 47 | |
Georgi Djakov | 293d2e97 | 2015-03-20 18:30:26 +0200 | [diff] [blame] | 48 | int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src) |
| 49 | { |
Stephen Boyd | 497295a | 2015-06-25 16:53:23 -0700 | [diff] [blame] | 50 | int i, num_parents = clk_hw_get_num_parents(hw); |
Georgi Djakov | 293d2e97 | 2015-03-20 18:30:26 +0200 | [diff] [blame] | 51 | |
| 52 | for (i = 0; i < num_parents; i++) |
| 53 | if (src == map[i].src) |
| 54 | return i; |
| 55 | |
| 56 | return -ENOENT; |
| 57 | } |
| 58 | EXPORT_SYMBOL_GPL(qcom_find_src_index); |
| 59 | |
Stephen Boyd | 5b6b749 | 2014-07-15 14:59:21 -0700 | [diff] [blame] | 60 | struct regmap * |
| 61 | 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] | 62 | { |
| 63 | void __iomem *base; |
| 64 | struct resource *res; |
Stephen Boyd | 5b6b749 | 2014-07-15 14:59:21 -0700 | [diff] [blame] | 65 | struct device *dev = &pdev->dev; |
| 66 | |
| 67 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 68 | base = devm_ioremap_resource(dev, res); |
| 69 | if (IS_ERR(base)) |
| 70 | return ERR_CAST(base); |
| 71 | |
| 72 | return devm_regmap_init_mmio(dev, base, desc->config); |
| 73 | } |
| 74 | EXPORT_SYMBOL_GPL(qcom_cc_map); |
| 75 | |
Stephen Boyd | 94c51f4 | 2015-10-07 23:59:57 -0700 | [diff] [blame^] | 76 | static void qcom_cc_del_clk_provider(void *data) |
| 77 | { |
| 78 | of_clk_del_provider(data); |
| 79 | } |
| 80 | |
| 81 | static void qcom_cc_reset_unregister(void *data) |
| 82 | { |
| 83 | reset_controller_unregister(data); |
| 84 | } |
| 85 | |
| 86 | static void qcom_cc_gdsc_unregister(void *data) |
| 87 | { |
| 88 | gdsc_unregister(data); |
| 89 | } |
| 90 | |
Stephen Boyd | 5b6b749 | 2014-07-15 14:59:21 -0700 | [diff] [blame] | 91 | int qcom_cc_really_probe(struct platform_device *pdev, |
| 92 | const struct qcom_cc_desc *desc, struct regmap *regmap) |
| 93 | { |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 94 | int i, ret; |
| 95 | struct device *dev = &pdev->dev; |
| 96 | struct clk *clk; |
| 97 | struct clk_onecell_data *data; |
| 98 | struct clk **clks; |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 99 | struct qcom_reset_controller *reset; |
| 100 | struct qcom_cc *cc; |
| 101 | size_t num_clks = desc->num_clks; |
| 102 | struct clk_regmap **rclks = desc->clks; |
| 103 | |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 104 | cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks, |
| 105 | GFP_KERNEL); |
| 106 | if (!cc) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | clks = cc->clks; |
| 110 | data = &cc->data; |
| 111 | data->clks = clks; |
| 112 | data->clk_num = num_clks; |
| 113 | |
| 114 | for (i = 0; i < num_clks; i++) { |
Stephen Boyd | 9ec2749 | 2014-05-16 16:07:14 -0700 | [diff] [blame] | 115 | if (!rclks[i]) { |
| 116 | clks[i] = ERR_PTR(-ENOENT); |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 117 | continue; |
Stephen Boyd | 9ec2749 | 2014-05-16 16:07:14 -0700 | [diff] [blame] | 118 | } |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 119 | clk = devm_clk_register_regmap(dev, rclks[i]); |
| 120 | if (IS_ERR(clk)) |
| 121 | return PTR_ERR(clk); |
| 122 | clks[i] = clk; |
| 123 | } |
| 124 | |
| 125 | ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data); |
| 126 | if (ret) |
| 127 | return ret; |
| 128 | |
Stephen Boyd | 94c51f4 | 2015-10-07 23:59:57 -0700 | [diff] [blame^] | 129 | devm_add_action(dev, qcom_cc_del_clk_provider, pdev->dev.of_node); |
| 130 | |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 131 | reset = &cc->reset; |
| 132 | reset->rcdev.of_node = dev->of_node; |
| 133 | reset->rcdev.ops = &qcom_reset_ops; |
| 134 | reset->rcdev.owner = dev->driver->owner; |
| 135 | reset->rcdev.nr_resets = desc->num_resets; |
| 136 | reset->regmap = regmap; |
| 137 | reset->reset_map = desc->resets; |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 138 | |
| 139 | ret = reset_controller_register(&reset->rcdev); |
| 140 | if (ret) |
Stephen Boyd | 94c51f4 | 2015-10-07 23:59:57 -0700 | [diff] [blame^] | 141 | return ret; |
| 142 | |
| 143 | devm_add_action(dev, qcom_cc_reset_unregister, &reset->rcdev); |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 144 | |
Rajendra Nayak | 5e5cc24 | 2015-08-06 16:07:43 +0530 | [diff] [blame] | 145 | if (desc->gdscs && desc->num_gdscs) { |
Rajendra Nayak | 3c53f5e | 2015-08-06 16:07:45 +0530 | [diff] [blame] | 146 | ret = gdsc_register(dev, desc->gdscs, desc->num_gdscs, |
| 147 | &reset->rcdev, regmap); |
Rajendra Nayak | 5e5cc24 | 2015-08-06 16:07:43 +0530 | [diff] [blame] | 148 | if (ret) |
Stephen Boyd | 94c51f4 | 2015-10-07 23:59:57 -0700 | [diff] [blame^] | 149 | return ret; |
Rajendra Nayak | 5e5cc24 | 2015-08-06 16:07:43 +0530 | [diff] [blame] | 150 | } |
| 151 | |
Stephen Boyd | 94c51f4 | 2015-10-07 23:59:57 -0700 | [diff] [blame^] | 152 | devm_add_action(dev, qcom_cc_gdsc_unregister, dev); |
| 153 | |
| 154 | |
Rajendra Nayak | 5e5cc24 | 2015-08-06 16:07:43 +0530 | [diff] [blame] | 155 | return 0; |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 156 | } |
Stephen Boyd | 5b6b749 | 2014-07-15 14:59:21 -0700 | [diff] [blame] | 157 | EXPORT_SYMBOL_GPL(qcom_cc_really_probe); |
| 158 | |
| 159 | int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc) |
| 160 | { |
| 161 | struct regmap *regmap; |
| 162 | |
| 163 | regmap = qcom_cc_map(pdev, desc); |
| 164 | if (IS_ERR(regmap)) |
| 165 | return PTR_ERR(regmap); |
| 166 | |
| 167 | return qcom_cc_really_probe(pdev, desc, regmap); |
| 168 | } |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 169 | EXPORT_SYMBOL_GPL(qcom_cc_probe); |
| 170 | |
| 171 | void qcom_cc_remove(struct platform_device *pdev) |
| 172 | { |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 173 | } |
| 174 | EXPORT_SYMBOL_GPL(qcom_cc_remove); |
Stephen Boyd | 169f05e8 | 2015-07-24 11:55:42 -0700 | [diff] [blame] | 175 | |
| 176 | MODULE_LICENSE("GPL v2"); |