Skip to content

Commit 9966946

Browse files
committed
clk: spacemit: extract common ccu functions
Refactor the probe function of SpacemiT's clock, and extract a common ccu file, so new clock driver added in the future can share the same code, which would lower the burden of maintenance. Since this commit changes the module name from spacemit_ccu_k1 to spacemit_ccu where the auxiliary device registered, the auxiliary device id need to be adjusted. Idea of the patch comes from the review of K3 clock driver, please refer to this disucssion[1] for more detail. This change will introduce a runtime break to reset driver, and will be fixed in follow-up commit: ecff77f ("reset: spacemit: fix auxiliary device id") Link: https://lore.kernel.org/all/aTo8sCPpVM1o9PKX@pie/ [1] Link: https://lore.kernel.org/r/20260108-06-k1-clk-common-v4-2-badf635993d3@gentoo.org Suggested-by: Yao Zi <me@ziyao.cc> Reviewed-by: Alex Elder <elder@riscstar.com> Signed-off-by: Yixun Lan <dlan@gentoo.org>
1 parent b61571f commit 9966946

3 files changed

Lines changed: 186 additions & 174 deletions

File tree

drivers/clk/spacemit/ccu-k1.c

Lines changed: 5 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
*/
66

77
#include <linux/array_size.h>
8-
#include <linux/auxiliary_bus.h>
98
#include <linux/clk-provider.h>
10-
#include <linux/delay.h>
11-
#include <linux/idr.h>
12-
#include <linux/mfd/syscon.h>
139
#include <linux/minmax.h>
1410
#include <linux/module.h>
1511
#include <linux/platform_device.h>
16-
#include <linux/slab.h>
1712
#include <soc/spacemit/k1-syscon.h>
1813

1914
#include "ccu_common.h"
@@ -23,14 +18,6 @@
2318

2419
#include <dt-bindings/clock/spacemit,k1-syscon.h>
2520

26-
struct spacemit_ccu_data {
27-
const char *reset_name;
28-
struct clk_hw **hws;
29-
size_t num;
30-
};
31-
32-
static DEFINE_IDA(auxiliary_ids);
33-
3421
/* APBS clocks start, APBS region contains and only contains all PLL clocks */
3522

3623
/*
@@ -1001,167 +988,6 @@ static const struct spacemit_ccu_data k1_ccu_apbc2_data = {
1001988
.reset_name = "apbc2-reset",
1002989
};
1003990

1004-
static int spacemit_ccu_register(struct device *dev,
1005-
struct regmap *regmap,
1006-
struct regmap *lock_regmap,
1007-
const struct spacemit_ccu_data *data)
1008-
{
1009-
struct clk_hw_onecell_data *clk_data;
1010-
int i, ret;
1011-
1012-
/* Nothing to do if the CCU does not implement any clocks */
1013-
if (!data->hws)
1014-
return 0;
1015-
1016-
clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, data->num),
1017-
GFP_KERNEL);
1018-
if (!clk_data)
1019-
return -ENOMEM;
1020-
1021-
clk_data->num = data->num;
1022-
1023-
for (i = 0; i < data->num; i++) {
1024-
struct clk_hw *hw = data->hws[i];
1025-
struct ccu_common *common;
1026-
const char *name;
1027-
1028-
if (!hw) {
1029-
clk_data->hws[i] = ERR_PTR(-ENOENT);
1030-
continue;
1031-
}
1032-
1033-
name = hw->init->name;
1034-
1035-
common = hw_to_ccu_common(hw);
1036-
common->regmap = regmap;
1037-
common->lock_regmap = lock_regmap;
1038-
1039-
ret = devm_clk_hw_register(dev, hw);
1040-
if (ret) {
1041-
dev_err(dev, "Cannot register clock %d - %s\n",
1042-
i, name);
1043-
return ret;
1044-
}
1045-
1046-
clk_data->hws[i] = hw;
1047-
}
1048-
1049-
ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
1050-
if (ret)
1051-
dev_err(dev, "failed to add clock hardware provider (%d)\n", ret);
1052-
1053-
return ret;
1054-
}
1055-
1056-
static void spacemit_cadev_release(struct device *dev)
1057-
{
1058-
struct auxiliary_device *adev = to_auxiliary_dev(dev);
1059-
1060-
ida_free(&auxiliary_ids, adev->id);
1061-
kfree(to_spacemit_ccu_adev(adev));
1062-
}
1063-
1064-
static void spacemit_adev_unregister(void *data)
1065-
{
1066-
struct auxiliary_device *adev = data;
1067-
1068-
auxiliary_device_delete(adev);
1069-
auxiliary_device_uninit(adev);
1070-
}
1071-
1072-
static int spacemit_ccu_reset_register(struct device *dev,
1073-
struct regmap *regmap,
1074-
const char *reset_name)
1075-
{
1076-
struct spacemit_ccu_adev *cadev;
1077-
struct auxiliary_device *adev;
1078-
int ret;
1079-
1080-
/* Nothing to do if the CCU does not implement a reset controller */
1081-
if (!reset_name)
1082-
return 0;
1083-
1084-
cadev = kzalloc(sizeof(*cadev), GFP_KERNEL);
1085-
if (!cadev)
1086-
return -ENOMEM;
1087-
1088-
cadev->regmap = regmap;
1089-
1090-
adev = &cadev->adev;
1091-
adev->name = reset_name;
1092-
adev->dev.parent = dev;
1093-
adev->dev.release = spacemit_cadev_release;
1094-
adev->dev.of_node = dev->of_node;
1095-
ret = ida_alloc(&auxiliary_ids, GFP_KERNEL);
1096-
if (ret < 0)
1097-
goto err_free_cadev;
1098-
adev->id = ret;
1099-
1100-
ret = auxiliary_device_init(adev);
1101-
if (ret)
1102-
goto err_free_aux_id;
1103-
1104-
ret = auxiliary_device_add(adev);
1105-
if (ret) {
1106-
auxiliary_device_uninit(adev);
1107-
return ret;
1108-
}
1109-
1110-
return devm_add_action_or_reset(dev, spacemit_adev_unregister, adev);
1111-
1112-
err_free_aux_id:
1113-
ida_free(&auxiliary_ids, adev->id);
1114-
err_free_cadev:
1115-
kfree(cadev);
1116-
1117-
return ret;
1118-
}
1119-
1120-
static int k1_ccu_probe(struct platform_device *pdev)
1121-
{
1122-
struct regmap *base_regmap, *lock_regmap = NULL;
1123-
const struct spacemit_ccu_data *data;
1124-
struct device *dev = &pdev->dev;
1125-
int ret;
1126-
1127-
base_regmap = device_node_to_regmap(dev->of_node);
1128-
if (IS_ERR(base_regmap))
1129-
return dev_err_probe(dev, PTR_ERR(base_regmap),
1130-
"failed to get regmap\n");
1131-
1132-
/*
1133-
* The lock status of PLLs locate in MPMU region, while PLLs themselves
1134-
* are in APBS region. Reference to MPMU syscon is required to check PLL
1135-
* status.
1136-
*/
1137-
if (of_device_is_compatible(dev->of_node, "spacemit,k1-pll")) {
1138-
struct device_node *mpmu = of_parse_phandle(dev->of_node,
1139-
"spacemit,mpmu", 0);
1140-
if (!mpmu)
1141-
return dev_err_probe(dev, -ENODEV,
1142-
"Cannot parse MPMU region\n");
1143-
1144-
lock_regmap = device_node_to_regmap(mpmu);
1145-
of_node_put(mpmu);
1146-
1147-
if (IS_ERR(lock_regmap))
1148-
return dev_err_probe(dev, PTR_ERR(lock_regmap),
1149-
"failed to get lock regmap\n");
1150-
}
1151-
1152-
data = of_device_get_match_data(dev);
1153-
1154-
ret = spacemit_ccu_register(dev, base_regmap, lock_regmap, data);
1155-
if (ret)
1156-
return dev_err_probe(dev, ret, "failed to register clocks\n");
1157-
1158-
ret = spacemit_ccu_reset_register(dev, base_regmap, data->reset_name);
1159-
if (ret)
1160-
return dev_err_probe(dev, ret, "failed to register resets\n");
1161-
1162-
return 0;
1163-
}
1164-
1165991
static const struct of_device_id of_k1_ccu_match[] = {
1166992
{
1167993
.compatible = "spacemit,k1-pll",
@@ -1195,6 +1021,11 @@ static const struct of_device_id of_k1_ccu_match[] = {
11951021
};
11961022
MODULE_DEVICE_TABLE(of, of_k1_ccu_match);
11971023

1024+
static int k1_ccu_probe(struct platform_device *pdev)
1025+
{
1026+
return spacemit_ccu_probe(pdev, "spacemit,k1-pll");
1027+
}
1028+
11981029
static struct platform_driver k1_ccu_driver = {
11991030
.driver = {
12001031
.name = "spacemit,k1-ccu",

drivers/clk/spacemit/ccu_common.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,177 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

3+
#include <linux/clk-provider.h>
4+
#include <linux/device/devres.h>
5+
#include <linux/mfd/syscon.h>
36
#include <linux/module.h>
7+
#include <linux/of.h>
8+
#include <linux/slab.h>
9+
#include <soc/spacemit/ccu.h>
10+
11+
#include "ccu_common.h"
12+
13+
static DEFINE_IDA(auxiliary_ids);
14+
static int spacemit_ccu_register(struct device *dev,
15+
struct regmap *regmap,
16+
struct regmap *lock_regmap,
17+
const struct spacemit_ccu_data *data)
18+
{
19+
struct clk_hw_onecell_data *clk_data;
20+
int i, ret;
21+
22+
/* Nothing to do if the CCU does not implement any clocks */
23+
if (!data->hws)
24+
return 0;
25+
26+
clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, data->num),
27+
GFP_KERNEL);
28+
if (!clk_data)
29+
return -ENOMEM;
30+
31+
clk_data->num = data->num;
32+
33+
for (i = 0; i < data->num; i++) {
34+
struct clk_hw *hw = data->hws[i];
35+
struct ccu_common *common;
36+
const char *name;
37+
38+
if (!hw) {
39+
clk_data->hws[i] = ERR_PTR(-ENOENT);
40+
continue;
41+
}
42+
43+
name = hw->init->name;
44+
45+
common = hw_to_ccu_common(hw);
46+
common->regmap = regmap;
47+
common->lock_regmap = lock_regmap;
48+
49+
ret = devm_clk_hw_register(dev, hw);
50+
if (ret) {
51+
dev_err(dev, "Cannot register clock %d - %s\n",
52+
i, name);
53+
return ret;
54+
}
55+
56+
clk_data->hws[i] = hw;
57+
}
58+
59+
ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
60+
if (ret)
61+
dev_err(dev, "failed to add clock hardware provider (%d)\n", ret);
62+
63+
return ret;
64+
}
65+
66+
static void spacemit_cadev_release(struct device *dev)
67+
{
68+
struct auxiliary_device *adev = to_auxiliary_dev(dev);
69+
70+
ida_free(&auxiliary_ids, adev->id);
71+
kfree(to_spacemit_ccu_adev(adev));
72+
}
73+
74+
static void spacemit_adev_unregister(void *data)
75+
{
76+
struct auxiliary_device *adev = data;
77+
78+
auxiliary_device_delete(adev);
79+
auxiliary_device_uninit(adev);
80+
}
81+
82+
static int spacemit_ccu_reset_register(struct device *dev,
83+
struct regmap *regmap,
84+
const char *reset_name)
85+
{
86+
struct spacemit_ccu_adev *cadev;
87+
struct auxiliary_device *adev;
88+
int ret;
89+
90+
/* Nothing to do if the CCU does not implement a reset controller */
91+
if (!reset_name)
92+
return 0;
93+
94+
cadev = kzalloc(sizeof(*cadev), GFP_KERNEL);
95+
if (!cadev)
96+
return -ENOMEM;
97+
98+
cadev->regmap = regmap;
99+
100+
adev = &cadev->adev;
101+
adev->name = reset_name;
102+
adev->dev.parent = dev;
103+
adev->dev.release = spacemit_cadev_release;
104+
adev->dev.of_node = dev->of_node;
105+
ret = ida_alloc(&auxiliary_ids, GFP_KERNEL);
106+
if (ret < 0)
107+
goto err_free_cadev;
108+
adev->id = ret;
109+
110+
ret = auxiliary_device_init(adev);
111+
if (ret)
112+
goto err_free_aux_id;
113+
114+
ret = auxiliary_device_add(adev);
115+
if (ret) {
116+
auxiliary_device_uninit(adev);
117+
return ret;
118+
}
119+
120+
return devm_add_action_or_reset(dev, spacemit_adev_unregister, adev);
121+
122+
err_free_aux_id:
123+
ida_free(&auxiliary_ids, adev->id);
124+
err_free_cadev:
125+
kfree(cadev);
126+
127+
return ret;
128+
}
129+
130+
int spacemit_ccu_probe(struct platform_device *pdev, const char *compat)
131+
{
132+
struct regmap *base_regmap, *lock_regmap = NULL;
133+
const struct spacemit_ccu_data *data;
134+
struct device *dev = &pdev->dev;
135+
int ret;
136+
137+
base_regmap = device_node_to_regmap(dev->of_node);
138+
if (IS_ERR(base_regmap))
139+
return dev_err_probe(dev, PTR_ERR(base_regmap),
140+
"failed to get regmap\n");
141+
142+
/*
143+
* The lock status of PLLs locate in MPMU region, while PLLs themselves
144+
* are in APBS region. Reference to MPMU syscon is required to check PLL
145+
* status.
146+
*/
147+
if (compat && of_device_is_compatible(dev->of_node, compat)) {
148+
struct device_node *mpmu = of_parse_phandle(dev->of_node,
149+
"spacemit,mpmu", 0);
150+
if (!mpmu)
151+
return dev_err_probe(dev, -ENODEV,
152+
"Cannot parse MPMU region\n");
153+
154+
lock_regmap = device_node_to_regmap(mpmu);
155+
of_node_put(mpmu);
156+
157+
if (IS_ERR(lock_regmap))
158+
return dev_err_probe(dev, PTR_ERR(lock_regmap),
159+
"failed to get lock regmap\n");
160+
}
161+
162+
data = of_device_get_match_data(dev);
163+
164+
ret = spacemit_ccu_register(dev, base_regmap, lock_regmap, data);
165+
if (ret)
166+
return dev_err_probe(dev, ret, "failed to register clocks\n");
167+
168+
ret = spacemit_ccu_reset_register(dev, base_regmap, data->reset_name);
169+
if (ret)
170+
return dev_err_probe(dev, ret, "failed to register resets\n");
171+
172+
return 0;
173+
}
174+
EXPORT_SYMBOL_NS_GPL(spacemit_ccu_probe, "CLK_SPACEMIT");
4175

5176
MODULE_DESCRIPTION("SpacemiT CCU common clock driver");
6177
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)