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 int qcom_cc_really_probe(struct platform_device *pdev, 77 const struct qcom_cc_desc *desc, struct regmap *regmap) 78 { 79 int i, ret; 80 struct device *dev = &pdev->dev; 81 struct clk *clk; 82 struct clk_onecell_data *data; 83 struct clk **clks; 84 struct qcom_reset_controller *reset; 85 struct qcom_cc *cc; 86 size_t num_clks = desc->num_clks; 87 struct clk_regmap **rclks = desc->clks; 88 89 cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks, 90 GFP_KERNEL); 91 if (!cc) 92 return -ENOMEM; 93 94 clks = cc->clks; 95 data = &cc->data; 96 data->clks = clks; 97 data->clk_num = num_clks; 98 99 for (i = 0; i < num_clks; i++) { 100 if (!rclks[i]) { 101 clks[i] = ERR_PTR(-ENOENT); 102 continue; 103 } 104 clk = devm_clk_register_regmap(dev, rclks[i]); 105 if (IS_ERR(clk)) 106 return PTR_ERR(clk); 107 clks[i] = clk; 108 } 109 110 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data); 111 if (ret) 112 return ret; 113 114 reset = &cc->reset; 115 reset->rcdev.of_node = dev->of_node; 116 reset->rcdev.ops = &qcom_reset_ops; 117 reset->rcdev.owner = dev->driver->owner; 118 reset->rcdev.nr_resets = desc->num_resets; 119 reset->regmap = regmap; 120 reset->reset_map = desc->resets; 121 platform_set_drvdata(pdev, &reset->rcdev); 122 123 ret = reset_controller_register(&reset->rcdev); 124 if (ret) 125 goto err_reset; 126 127 if (desc->gdscs && desc->num_gdscs) { 128 ret = gdsc_register(dev, desc->gdscs, desc->num_gdscs, 129 &reset->rcdev, regmap); 130 if (ret) 131 goto err_pd; 132 } 133 134 return 0; 135 err_pd: 136 reset_controller_unregister(&reset->rcdev); 137 err_reset: 138 of_clk_del_provider(dev->of_node); 139 return ret; 140 } 141 EXPORT_SYMBOL_GPL(qcom_cc_really_probe); 142 143 int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc) 144 { 145 struct regmap *regmap; 146 147 regmap = qcom_cc_map(pdev, desc); 148 if (IS_ERR(regmap)) 149 return PTR_ERR(regmap); 150 151 return qcom_cc_really_probe(pdev, desc, regmap); 152 } 153 EXPORT_SYMBOL_GPL(qcom_cc_probe); 154 155 void qcom_cc_remove(struct platform_device *pdev) 156 { 157 gdsc_unregister(&pdev->dev); 158 of_clk_del_provider(pdev->dev.of_node); 159 reset_controller_unregister(platform_get_drvdata(pdev)); 160 } 161 EXPORT_SYMBOL_GPL(qcom_cc_remove); 162 163 MODULE_LICENSE("GPL v2"); 164