|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Socionext Inc. |
| 3 | + * Author: Masahiro Yamada <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <linux/clk-provider.h> |
| 17 | +#include <linux/init.h> |
| 18 | +#include <linux/mfd/syscon.h> |
| 19 | +#include <linux/of.h> |
| 20 | +#include <linux/of_device.h> |
| 21 | +#include <linux/platform_device.h> |
| 22 | + |
| 23 | +#include "clk-uniphier.h" |
| 24 | + |
| 25 | +static struct clk_hw *uniphier_clk_register(struct device *dev, |
| 26 | + struct regmap *regmap, |
| 27 | + const struct uniphier_clk_data *data) |
| 28 | +{ |
| 29 | + switch (data->type) { |
| 30 | + case UNIPHIER_CLK_TYPE_FIXED_FACTOR: |
| 31 | + return uniphier_clk_register_fixed_factor(dev, data->name, |
| 32 | + &data->data.factor); |
| 33 | + case UNIPHIER_CLK_TYPE_FIXED_RATE: |
| 34 | + return uniphier_clk_register_fixed_rate(dev, data->name, |
| 35 | + &data->data.rate); |
| 36 | + case UNIPHIER_CLK_TYPE_GATE: |
| 37 | + return uniphier_clk_register_gate(dev, regmap, data->name, |
| 38 | + &data->data.gate); |
| 39 | + case UNIPHIER_CLK_TYPE_MUX: |
| 40 | + return uniphier_clk_register_mux(dev, regmap, data->name, |
| 41 | + &data->data.mux); |
| 42 | + default: |
| 43 | + dev_err(dev, "unsupported clock type\n"); |
| 44 | + return ERR_PTR(-EINVAL); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +static int uniphier_clk_probe(struct platform_device *pdev) |
| 49 | +{ |
| 50 | + struct device *dev = &pdev->dev; |
| 51 | + struct clk_hw_onecell_data *hw_data; |
| 52 | + const struct uniphier_clk_data *p, *data; |
| 53 | + struct regmap *regmap; |
| 54 | + struct device_node *parent; |
| 55 | + int clk_num = 0; |
| 56 | + |
| 57 | + data = of_device_get_match_data(dev); |
| 58 | + if (WARN_ON(!data)) |
| 59 | + return -EINVAL; |
| 60 | + |
| 61 | + parent = of_get_parent(dev->of_node); /* parent should be syscon node */ |
| 62 | + regmap = syscon_node_to_regmap(parent); |
| 63 | + of_node_put(parent); |
| 64 | + if (IS_ERR(regmap)) { |
| 65 | + dev_err(dev, "failed to get regmap (error %ld)\n", |
| 66 | + PTR_ERR(regmap)); |
| 67 | + return PTR_ERR(regmap); |
| 68 | + } |
| 69 | + |
| 70 | + for (p = data; p->name; p++) |
| 71 | + clk_num = max(clk_num, p->idx + 1); |
| 72 | + |
| 73 | + hw_data = devm_kzalloc(dev, |
| 74 | + sizeof(*hw_data) + clk_num * sizeof(struct clk_hw *), |
| 75 | + GFP_KERNEL); |
| 76 | + if (!hw_data) |
| 77 | + return -ENOMEM; |
| 78 | + |
| 79 | + hw_data->num = clk_num; |
| 80 | + |
| 81 | + /* avoid returning NULL for unused idx */ |
| 82 | + for (; clk_num >= 0; clk_num--) |
| 83 | + hw_data->hws[clk_num] = ERR_PTR(-EINVAL); |
| 84 | + |
| 85 | + for (p = data; p->name; p++) { |
| 86 | + struct clk_hw *hw; |
| 87 | + |
| 88 | + dev_dbg(dev, "register %s (index=%d)\n", p->name, p->idx); |
| 89 | + hw = uniphier_clk_register(dev, regmap, p); |
| 90 | + if (IS_ERR(hw)) { |
| 91 | + dev_err(dev, "failed to register %s (error %ld)\n", |
| 92 | + p->name, PTR_ERR(hw)); |
| 93 | + return PTR_ERR(hw); |
| 94 | + } |
| 95 | + |
| 96 | + if (p->idx >= 0) |
| 97 | + hw_data->hws[p->idx] = hw; |
| 98 | + } |
| 99 | + |
| 100 | + return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get, |
| 101 | + hw_data); |
| 102 | +} |
| 103 | + |
| 104 | +static int uniphier_clk_remove(struct platform_device *pdev) |
| 105 | +{ |
| 106 | + of_clk_del_provider(pdev->dev.of_node); |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +static const struct of_device_id uniphier_clk_match[] = { |
| 112 | + { /* sentinel */ } |
| 113 | +}; |
| 114 | + |
| 115 | +static struct platform_driver uniphier_clk_driver = { |
| 116 | + .probe = uniphier_clk_probe, |
| 117 | + .remove = uniphier_clk_remove, |
| 118 | + .driver = { |
| 119 | + .name = "uniphier-clk", |
| 120 | + .of_match_table = uniphier_clk_match, |
| 121 | + }, |
| 122 | +}; |
| 123 | +builtin_platform_driver(uniphier_clk_driver); |
0 commit comments