1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sysctrl clock implementation for ux500 platform.
5 * Copyright (C) 2013 ST-Ericsson SA
6 * Author: Ulf Hansson <ulf.hansson@linaro.org>
9 #include <linux/clk-provider.h>
10 #include <linux/mfd/abx500/ab8500-sysctrl.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
15 #include <linux/err.h>
18 #define SYSCTRL_MAX_NUM_PARENTS 4
20 #define to_clk_sysctrl(_hw) container_of(_hw, struct clk_sysctrl, hw)
26 u16 reg_sel
[SYSCTRL_MAX_NUM_PARENTS
];
27 u8 reg_mask
[SYSCTRL_MAX_NUM_PARENTS
];
28 u8 reg_bits
[SYSCTRL_MAX_NUM_PARENTS
];
30 unsigned long enable_delay_us
;
33 /* Sysctrl clock operations. */
35 static int clk_sysctrl_prepare(struct clk_hw
*hw
)
38 struct clk_sysctrl
*clk
= to_clk_sysctrl(hw
);
40 ret
= ab8500_sysctrl_write(clk
->reg_sel
[0], clk
->reg_mask
[0],
43 if (!ret
&& clk
->enable_delay_us
)
44 usleep_range(clk
->enable_delay_us
, clk
->enable_delay_us
+
45 (clk
->enable_delay_us
>> 2));
50 static void clk_sysctrl_unprepare(struct clk_hw
*hw
)
52 struct clk_sysctrl
*clk
= to_clk_sysctrl(hw
);
53 if (ab8500_sysctrl_clear(clk
->reg_sel
[0], clk
->reg_mask
[0]))
54 dev_err(clk
->dev
, "clk_sysctrl: %s fail to clear %s.\n",
55 __func__
, clk_hw_get_name(hw
));
58 static unsigned long clk_sysctrl_recalc_rate(struct clk_hw
*hw
,
59 unsigned long parent_rate
)
61 struct clk_sysctrl
*clk
= to_clk_sysctrl(hw
);
65 static int clk_sysctrl_set_parent(struct clk_hw
*hw
, u8 index
)
67 struct clk_sysctrl
*clk
= to_clk_sysctrl(hw
);
68 u8 old_index
= clk
->parent_index
;
71 if (clk
->reg_sel
[old_index
]) {
72 ret
= ab8500_sysctrl_clear(clk
->reg_sel
[old_index
],
73 clk
->reg_mask
[old_index
]);
78 if (clk
->reg_sel
[index
]) {
79 ret
= ab8500_sysctrl_write(clk
->reg_sel
[index
],
81 clk
->reg_bits
[index
]);
83 if (clk
->reg_sel
[old_index
])
84 ab8500_sysctrl_write(clk
->reg_sel
[old_index
],
85 clk
->reg_mask
[old_index
],
86 clk
->reg_bits
[old_index
]);
90 clk
->parent_index
= index
;
95 static u8
clk_sysctrl_get_parent(struct clk_hw
*hw
)
97 struct clk_sysctrl
*clk
= to_clk_sysctrl(hw
);
98 return clk
->parent_index
;
101 static const struct clk_ops clk_sysctrl_gate_ops
= {
102 .prepare
= clk_sysctrl_prepare
,
103 .unprepare
= clk_sysctrl_unprepare
,
106 static const struct clk_ops clk_sysctrl_gate_fixed_rate_ops
= {
107 .prepare
= clk_sysctrl_prepare
,
108 .unprepare
= clk_sysctrl_unprepare
,
109 .recalc_rate
= clk_sysctrl_recalc_rate
,
112 static const struct clk_ops clk_sysctrl_set_parent_ops
= {
113 .determine_rate
= clk_hw_determine_rate_no_reparent
,
114 .set_parent
= clk_sysctrl_set_parent
,
115 .get_parent
= clk_sysctrl_get_parent
,
118 static struct clk
*clk_reg_sysctrl(struct device
*dev
,
120 const char **parent_names
,
126 unsigned long enable_delay_us
,
128 const struct clk_ops
*clk_sysctrl_ops
)
130 struct clk_sysctrl
*clk
;
131 struct clk_init_data clk_sysctrl_init
;
136 return ERR_PTR(-EINVAL
);
138 if (!name
|| (num_parents
> SYSCTRL_MAX_NUM_PARENTS
)) {
139 dev_err(dev
, "clk_sysctrl: invalid arguments passed\n");
140 return ERR_PTR(-EINVAL
);
143 clk
= devm_kzalloc(dev
, sizeof(*clk
), GFP_KERNEL
);
145 return ERR_PTR(-ENOMEM
);
147 /* set main clock registers */
148 clk
->reg_sel
[0] = reg_sel
[0];
149 clk
->reg_bits
[0] = reg_bits
[0];
150 clk
->reg_mask
[0] = reg_mask
[0];
152 /* handle clocks with more than one parent */
153 for (i
= 1; i
< num_parents
; i
++) {
154 clk
->reg_sel
[i
] = reg_sel
[i
];
155 clk
->reg_bits
[i
] = reg_bits
[i
];
156 clk
->reg_mask
[i
] = reg_mask
[i
];
159 clk
->parent_index
= 0;
161 clk
->enable_delay_us
= enable_delay_us
;
164 clk_sysctrl_init
.name
= name
;
165 clk_sysctrl_init
.ops
= clk_sysctrl_ops
;
166 clk_sysctrl_init
.flags
= flags
;
167 clk_sysctrl_init
.parent_names
= parent_names
;
168 clk_sysctrl_init
.num_parents
= num_parents
;
169 clk
->hw
.init
= &clk_sysctrl_init
;
171 clk_reg
= devm_clk_register(clk
->dev
, &clk
->hw
);
173 dev_err(dev
, "clk_sysctrl: clk_register failed\n");
178 struct clk
*clk_reg_sysctrl_gate(struct device
*dev
,
180 const char *parent_name
,
184 unsigned long enable_delay_us
,
187 const char **parent_names
= (parent_name
? &parent_name
: NULL
);
188 u8 num_parents
= (parent_name
? 1 : 0);
190 return clk_reg_sysctrl(dev
, name
, parent_names
, num_parents
,
191 ®_sel
, ®_mask
, ®_bits
, 0, enable_delay_us
,
192 flags
, &clk_sysctrl_gate_ops
);
195 struct clk
*clk_reg_sysctrl_gate_fixed_rate(struct device
*dev
,
197 const char *parent_name
,
202 unsigned long enable_delay_us
,
205 const char **parent_names
= (parent_name
? &parent_name
: NULL
);
206 u8 num_parents
= (parent_name
? 1 : 0);
208 return clk_reg_sysctrl(dev
, name
, parent_names
, num_parents
,
209 ®_sel
, ®_mask
, ®_bits
,
210 rate
, enable_delay_us
, flags
,
211 &clk_sysctrl_gate_fixed_rate_ops
);
214 struct clk
*clk_reg_sysctrl_set_parent(struct device
*dev
,
216 const char **parent_names
,
223 return clk_reg_sysctrl(dev
, name
, parent_names
, num_parents
,
224 reg_sel
, reg_mask
, reg_bits
, 0, 0, flags
,
225 &clk_sysctrl_set_parent_ops
);