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_device.h>
17 #include <linux/slab.h>
18 #include "meson-aoclk.h"
19 
meson_aoclk_do_reset(struct reset_controller_dev * rcdev,unsigned long id)20 static int meson_aoclk_do_reset(struct reset_controller_dev *rcdev,
21 			       unsigned long id)
22 {
23 	struct meson_aoclk_reset_controller *rstc =
24 		container_of(rcdev, struct meson_aoclk_reset_controller, reset);
25 
26 	return regmap_write(rstc->regmap, rstc->data->reset_reg,
27 			    BIT(rstc->data->reset[id]));
28 }
29 
30 static const struct reset_control_ops meson_aoclk_reset_ops = {
31 	.reset = meson_aoclk_do_reset,
32 };
33 
meson_aoclkc_probe(struct platform_device * pdev)34 int meson_aoclkc_probe(struct platform_device *pdev)
35 {
36 	struct meson_aoclk_reset_controller *rstc;
37 	struct meson_aoclk_data *data;
38 	struct device *dev = &pdev->dev;
39 	struct regmap *regmap;
40 	int ret, clkid;
41 
42 	data = (struct meson_aoclk_data *) of_device_get_match_data(dev);
43 	if (!data)
44 		return -ENODEV;
45 
46 	rstc = devm_kzalloc(dev, sizeof(*rstc), GFP_KERNEL);
47 	if (!rstc)
48 		return -ENOMEM;
49 
50 	regmap = syscon_node_to_regmap(of_get_parent(dev->of_node));
51 	if (IS_ERR(regmap)) {
52 		dev_err(dev, "failed to get regmap\n");
53 		return PTR_ERR(regmap);
54 	}
55 
56 	/* Reset Controller */
57 	rstc->data = data;
58 	rstc->regmap = regmap;
59 	rstc->reset.ops = &meson_aoclk_reset_ops;
60 	rstc->reset.nr_resets = data->num_reset,
61 	rstc->reset.of_node = dev->of_node;
62 	ret = devm_reset_controller_register(dev, &rstc->reset);
63 	if (ret) {
64 		dev_err(dev, "failed to register reset controller\n");
65 		return ret;
66 	}
67 
68 	/* Populate regmap */
69 	for (clkid = 0; clkid < data->num_clks; clkid++)
70 		data->clks[clkid]->map = regmap;
71 
72 	/* Register all clks */
73 	for (clkid = 0; clkid < data->hw_data->num; clkid++) {
74 		if (!data->hw_data->hws[clkid])
75 			continue;
76 
77 		ret = devm_clk_hw_register(dev, data->hw_data->hws[clkid]);
78 		if (ret) {
79 			dev_err(dev, "Clock registration failed\n");
80 			return ret;
81 		}
82 	}
83 
84 	return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
85 		(void *) data->hw_data);
86 }
87