xref: /linux/drivers/clk/meson/meson-aoclk.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Amlogic Meson-AXG Clock Controller Driver
4  *
5  * Copyright (c) 2016 BayLibre, SAS.
6  * Author: Neil Armstrong <narmstrong@baylibre.com>
7  *
8  * Copyright (c) 2018 Amlogic, inc.
9  * Author: Qiufang Dai <qiufang.dai@amlogic.com>
10  * Author: Yixun Lan <yixun.lan@amlogic.com>
11  */
12 
13 #include <linux/platform_device.h>
14 #include <linux/reset-controller.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/of.h>
17 #include <linux/module.h>
18 
19 #include <linux/slab.h>
20 #include "meson-aoclk.h"
21 #include "clk-regmap.h"
22 
23 static int meson_aoclk_do_reset(struct reset_controller_dev *rcdev,
24 			       unsigned long id)
25 {
26 	struct meson_aoclk_reset_controller *rstc =
27 		container_of(rcdev, struct meson_aoclk_reset_controller, reset);
28 
29 	return regmap_write(rstc->regmap, rstc->data->reset_reg,
30 			    BIT(rstc->data->reset[id]));
31 }
32 
33 static const struct reset_control_ops meson_aoclk_reset_ops = {
34 	.reset = meson_aoclk_do_reset,
35 };
36 
37 int meson_aoclkc_probe(struct platform_device *pdev)
38 {
39 	struct meson_aoclk_reset_controller *rstc;
40 	const struct meson_clkc_data *clkc_data;
41 	const struct meson_aoclk_data *data;
42 	struct device *dev = &pdev->dev;
43 	struct device_node *np;
44 	struct regmap *regmap;
45 	int ret;
46 
47 	clkc_data = of_device_get_match_data(dev);
48 	if (!clkc_data)
49 		return -EINVAL;
50 
51 	ret = meson_clkc_syscon_probe(pdev);
52 	if (ret)
53 		return ret;
54 
55 	data = container_of(clkc_data, struct meson_aoclk_data,
56 			    clkc_data);
57 
58 	rstc = devm_kzalloc(dev, sizeof(*rstc), GFP_KERNEL);
59 	if (!rstc)
60 		return -ENOMEM;
61 
62 	np = of_get_parent(dev->of_node);
63 	regmap = syscon_node_to_regmap(np);
64 	of_node_put(np);
65 	if (IS_ERR(regmap)) {
66 		dev_err(dev, "failed to get regmap\n");
67 		return PTR_ERR(regmap);
68 	}
69 
70 	/* Reset Controller */
71 	rstc->data = data;
72 	rstc->regmap = regmap;
73 	rstc->reset.ops = &meson_aoclk_reset_ops;
74 	rstc->reset.nr_resets = data->num_reset;
75 	rstc->reset.of_node = dev->of_node;
76 	ret = devm_reset_controller_register(dev, &rstc->reset);
77 	if (ret) {
78 		dev_err(dev, "failed to register reset controller\n");
79 		return ret;
80 	}
81 
82 	return 0;
83 }
84 EXPORT_SYMBOL_NS_GPL(meson_aoclkc_probe, "CLK_MESON");
85 
86 MODULE_DESCRIPTION("Amlogic Always-ON Clock Controller helpers");
87 MODULE_LICENSE("GPL");
88 MODULE_IMPORT_NS("CLK_MESON");
89