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 21 #include "common.h" 22 #include "clk-rcg.h" 23 #include "clk-regmap.h" 24 #include "reset.h" 25 #include "gdsc.h" 26 27 struct qcom_cc { 28 struct qcom_reset_controller reset; 29 struct clk_onecell_data data; 30 struct clk *clks[]; 31 }; 32 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 48 int qcom_find_src_index(struct clk_hw *hw, const struct parent_map *map, u8 src) 49 { 50 int i, num_parents = clk_hw_get_num_parents(hw); 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 60 struct regmap * 61 qcom_cc_map(struct platform_device *pdev, const struct qcom_cc_desc *desc) 62 { 63 void __iomem *base; 64 struct resource *res; 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 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 91 int qcom_cc_really_probe(struct platform_device *pdev, 92 const struct qcom_cc_desc *desc, struct regmap *regmap) 93 { 94 int i, ret; 95 struct device *dev = &pdev->dev; 96 struct clk *clk; 97 struct clk_onecell_data *data; 98 struct clk **clks; 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 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++) { 115 if (!rclks[i]) { 116 clks[i] = ERR_PTR(-ENOENT); 117 continue; 118 } 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 129 devm_add_action(dev, qcom_cc_del_clk_provider, pdev->dev.of_node); 130 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; 138 139 ret = reset_controller_register(&reset->rcdev); 140 if (ret) 141 return ret; 142 143 devm_add_action(dev, qcom_cc_reset_unregister, &reset->rcdev); 144 145 if (desc->gdscs && desc->num_gdscs) { 146 ret = gdsc_register(dev, desc->gdscs, desc->num_gdscs, 147 &reset->rcdev, regmap); 148 if (ret) 149 return ret; 150 } 151 152 devm_add_action(dev, qcom_cc_gdsc_unregister, dev); 153 154 155 return 0; 156 } 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 } 169 EXPORT_SYMBOL_GPL(qcom_cc_probe); 170 171 MODULE_LICENSE("GPL v2"); 172