xref: /openbmc/linux/drivers/clk/actions/owl-common.c (revision 9a29ad52)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // OWL common clock driver
4 //
5 // Copyright (c) 2014 Actions Semi Inc.
6 // Author: David Liu <liuwei@actions-semi.com>
7 //
8 // Copyright (c) 2018 Linaro Ltd.
9 // Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
10 
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 
16 #include "owl-common.h"
17 
18 static const struct regmap_config owl_regmap_config = {
19 	.reg_bits	= 32,
20 	.reg_stride	= 4,
21 	.val_bits	= 32,
22 	.max_register	= 0x00cc,
23 	.fast_io	= true,
24 };
25 
26 static void owl_clk_set_regmap(const struct owl_clk_desc *desc,
27 			 struct regmap *regmap)
28 {
29 	int i;
30 	struct owl_clk_common *clks;
31 
32 	for (i = 0; i < desc->num_clks; i++) {
33 		clks = desc->clks[i];
34 		if (!clks)
35 			continue;
36 
37 		clks->regmap = regmap;
38 	}
39 }
40 
41 int owl_clk_regmap_init(struct platform_device *pdev,
42 			 const struct owl_clk_desc *desc)
43 {
44 	void __iomem *base;
45 	struct regmap *regmap;
46 	struct resource *res;
47 
48 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
49 	base = devm_ioremap_resource(&pdev->dev, res);
50 	if (IS_ERR(base))
51 		return PTR_ERR(base);
52 
53 	regmap = devm_regmap_init_mmio(&pdev->dev, base, &owl_regmap_config);
54 	if (IS_ERR(regmap)) {
55 		pr_err("failed to init regmap\n");
56 		return PTR_ERR(regmap);
57 	}
58 
59 	owl_clk_set_regmap(desc, regmap);
60 
61 	return 0;
62 }
63 
64 int owl_clk_probe(struct device *dev, struct clk_hw_onecell_data *hw_clks)
65 {
66 	int i, ret;
67 	struct clk_hw *hw;
68 
69 	for (i = 0; i < hw_clks->num; i++) {
70 
71 		hw = hw_clks->hws[i];
72 
73 		if (IS_ERR_OR_NULL(hw))
74 			continue;
75 
76 		ret = devm_clk_hw_register(dev, hw);
77 		if (ret) {
78 			dev_err(dev, "Couldn't register clock %d - %s\n",
79 				i, hw->init->name);
80 			return ret;
81 		}
82 	}
83 
84 	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, hw_clks);
85 	if (ret)
86 		dev_err(dev, "Failed to add clock provider\n");
87 
88 	return ret;
89 }
90