xref: /openbmc/linux/drivers/clk/sprd/common.c (revision 1372a51b)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Spreadtrum clock infrastructure
4 //
5 // Copyright (C) 2017 Spreadtrum, Inc.
6 // Author: Chunyan Zhang <chunyan.zhang@spreadtrum.com>
7 
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 #include <linux/regmap.h>
13 
14 #include "common.h"
15 
16 static const struct regmap_config sprdclk_regmap_config = {
17 	.reg_bits	= 32,
18 	.reg_stride	= 4,
19 	.val_bits	= 32,
20 	.max_register	= 0xffff,
21 	.fast_io	= true,
22 };
23 
24 static void sprd_clk_set_regmap(const struct sprd_clk_desc *desc,
25 			 struct regmap *regmap)
26 {
27 	int i;
28 	struct sprd_clk_common *cclk;
29 
30 	for (i = 0; i < desc->num_clk_clks; i++) {
31 		cclk = desc->clk_clks[i];
32 		if (!cclk)
33 			continue;
34 
35 		cclk->regmap = regmap;
36 	}
37 }
38 
39 int sprd_clk_regmap_init(struct platform_device *pdev,
40 			 const struct sprd_clk_desc *desc)
41 {
42 	void __iomem *base;
43 	struct device_node *node = pdev->dev.of_node;
44 	struct regmap *regmap;
45 
46 	if (of_find_property(node, "sprd,syscon", NULL)) {
47 		regmap = syscon_regmap_lookup_by_phandle(node, "sprd,syscon");
48 		if (IS_ERR(regmap)) {
49 			pr_err("%s: failed to get syscon regmap\n", __func__);
50 			return PTR_ERR(regmap);
51 		}
52 	} else {
53 		base = devm_platform_ioremap_resource(pdev, 0);
54 		if (IS_ERR(base))
55 			return PTR_ERR(base);
56 
57 		regmap = devm_regmap_init_mmio(&pdev->dev, base,
58 					       &sprdclk_regmap_config);
59 		if (IS_ERR(regmap)) {
60 			pr_err("failed to init regmap\n");
61 			return PTR_ERR(regmap);
62 		}
63 	}
64 
65 	sprd_clk_set_regmap(desc, regmap);
66 
67 	return 0;
68 }
69 EXPORT_SYMBOL_GPL(sprd_clk_regmap_init);
70 
71 int sprd_clk_probe(struct device *dev, struct clk_hw_onecell_data *clkhw)
72 {
73 	int i, ret;
74 	struct clk_hw *hw;
75 
76 	for (i = 0; i < clkhw->num; i++) {
77 		const char *name;
78 
79 		hw = clkhw->hws[i];
80 		if (!hw)
81 			continue;
82 
83 		name = hw->init->name;
84 		ret = devm_clk_hw_register(dev, hw);
85 		if (ret) {
86 			dev_err(dev, "Couldn't register clock %d - %s\n",
87 				i, name);
88 			return ret;
89 		}
90 	}
91 
92 	ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clkhw);
93 	if (ret)
94 		dev_err(dev, "Failed to add clock provider\n");
95 
96 	return ret;
97 }
98 EXPORT_SYMBOL_GPL(sprd_clk_probe);
99 
100 MODULE_LICENSE("GPL v2");
101