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> |
| 15 | #include <linux/regmap.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/clk-provider.h> |
| 18 | #include <linux/reset-controller.h> |
| 19 | |
| 20 | #include "common.h" |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 21 | #include "clk-rcg.h" |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 22 | #include "clk-regmap.h" |
| 23 | #include "reset.h" |
| 24 | |
| 25 | struct qcom_cc { |
| 26 | struct qcom_reset_controller reset; |
| 27 | struct clk_onecell_data data; |
| 28 | struct clk *clks[]; |
| 29 | }; |
| 30 | |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 31 | const |
| 32 | struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, unsigned long rate) |
| 33 | { |
| 34 | if (!f) |
| 35 | return NULL; |
| 36 | |
| 37 | for (; f->freq; f++) |
| 38 | if (rate <= f->freq) |
| 39 | return f; |
| 40 | |
| 41 | /* Default to our fastest rate */ |
| 42 | return f - 1; |
| 43 | } |
| 44 | EXPORT_SYMBOL_GPL(qcom_find_freq); |
| 45 | |
Stephen Boyd | 5b6b749 | 2014-07-15 14:59:21 -0700 | [diff] [blame] | 46 | struct regmap * |
| 47 | 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] | 48 | { |
| 49 | void __iomem *base; |
| 50 | struct resource *res; |
Stephen Boyd | 5b6b749 | 2014-07-15 14:59:21 -0700 | [diff] [blame] | 51 | struct device *dev = &pdev->dev; |
| 52 | |
| 53 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 54 | base = devm_ioremap_resource(dev, res); |
| 55 | if (IS_ERR(base)) |
| 56 | return ERR_CAST(base); |
| 57 | |
| 58 | return devm_regmap_init_mmio(dev, base, desc->config); |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(qcom_cc_map); |
| 61 | |
| 62 | int qcom_cc_really_probe(struct platform_device *pdev, |
| 63 | const struct qcom_cc_desc *desc, struct regmap *regmap) |
| 64 | { |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 65 | int i, ret; |
| 66 | struct device *dev = &pdev->dev; |
| 67 | struct clk *clk; |
| 68 | struct clk_onecell_data *data; |
| 69 | struct clk **clks; |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 70 | struct qcom_reset_controller *reset; |
| 71 | struct qcom_cc *cc; |
| 72 | size_t num_clks = desc->num_clks; |
| 73 | struct clk_regmap **rclks = desc->clks; |
| 74 | |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 75 | cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*clks) * num_clks, |
| 76 | GFP_KERNEL); |
| 77 | if (!cc) |
| 78 | return -ENOMEM; |
| 79 | |
| 80 | clks = cc->clks; |
| 81 | data = &cc->data; |
| 82 | data->clks = clks; |
| 83 | data->clk_num = num_clks; |
| 84 | |
| 85 | for (i = 0; i < num_clks; i++) { |
Stephen Boyd | 9ec2749 | 2014-05-16 16:07:14 -0700 | [diff] [blame] | 86 | if (!rclks[i]) { |
| 87 | clks[i] = ERR_PTR(-ENOENT); |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 88 | continue; |
Stephen Boyd | 9ec2749 | 2014-05-16 16:07:14 -0700 | [diff] [blame] | 89 | } |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 90 | clk = devm_clk_register_regmap(dev, rclks[i]); |
| 91 | if (IS_ERR(clk)) |
| 92 | return PTR_ERR(clk); |
| 93 | clks[i] = clk; |
| 94 | } |
| 95 | |
| 96 | ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get, data); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | |
| 100 | reset = &cc->reset; |
| 101 | reset->rcdev.of_node = dev->of_node; |
| 102 | reset->rcdev.ops = &qcom_reset_ops; |
| 103 | reset->rcdev.owner = dev->driver->owner; |
| 104 | reset->rcdev.nr_resets = desc->num_resets; |
| 105 | reset->regmap = regmap; |
| 106 | reset->reset_map = desc->resets; |
| 107 | platform_set_drvdata(pdev, &reset->rcdev); |
| 108 | |
| 109 | ret = reset_controller_register(&reset->rcdev); |
| 110 | if (ret) |
| 111 | of_clk_del_provider(dev->of_node); |
| 112 | |
| 113 | return ret; |
| 114 | } |
Stephen Boyd | 5b6b749 | 2014-07-15 14:59:21 -0700 | [diff] [blame] | 115 | EXPORT_SYMBOL_GPL(qcom_cc_really_probe); |
| 116 | |
| 117 | int qcom_cc_probe(struct platform_device *pdev, const struct qcom_cc_desc *desc) |
| 118 | { |
| 119 | struct regmap *regmap; |
| 120 | |
| 121 | regmap = qcom_cc_map(pdev, desc); |
| 122 | if (IS_ERR(regmap)) |
| 123 | return PTR_ERR(regmap); |
| 124 | |
| 125 | return qcom_cc_really_probe(pdev, desc, regmap); |
| 126 | } |
Stephen Boyd | 49fc825 | 2014-03-21 17:59:37 -0700 | [diff] [blame] | 127 | EXPORT_SYMBOL_GPL(qcom_cc_probe); |
| 128 | |
| 129 | void qcom_cc_remove(struct platform_device *pdev) |
| 130 | { |
| 131 | of_clk_del_provider(pdev->dev.of_node); |
| 132 | reset_controller_unregister(platform_get_drvdata(pdev)); |
| 133 | } |
| 134 | EXPORT_SYMBOL_GPL(qcom_cc_remove); |