xref: /linux/drivers/clk/clk-scmi.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Power Interface (SCMI) Protocol based clock driver
4  *
5  * Copyright (C) 2018-2024 ARM Ltd.
6  */
7 
8 #include <linux/bits.h>
9 #include <linux/clk-provider.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/of.h>
13 #include <linux/module.h>
14 #include <linux/scmi_protocol.h>
15 
16 #define NOT_ATOMIC	false
17 #define ATOMIC		true
18 
19 enum scmi_clk_feats {
20 	SCMI_CLK_ATOMIC_SUPPORTED,
21 	SCMI_CLK_STATE_CTRL_SUPPORTED,
22 	SCMI_CLK_RATE_CTRL_SUPPORTED,
23 	SCMI_CLK_PARENT_CTRL_SUPPORTED,
24 	SCMI_CLK_DUTY_CYCLE_SUPPORTED,
25 	SCMI_CLK_FEATS_COUNT
26 };
27 
28 #define SCMI_MAX_CLK_OPS	BIT(SCMI_CLK_FEATS_COUNT)
29 
30 static const struct scmi_clk_proto_ops *scmi_proto_clk_ops;
31 
32 struct scmi_clk {
33 	u32 id;
34 	struct device *dev;
35 	struct clk_hw hw;
36 	const struct scmi_clock_info *info;
37 	const struct scmi_protocol_handle *ph;
38 	struct clk_parent_data *parent_data;
39 };
40 
41 #define to_scmi_clk(clk) container_of(clk, struct scmi_clk, hw)
42 
43 static unsigned long scmi_clk_recalc_rate(struct clk_hw *hw,
44 					  unsigned long parent_rate)
45 {
46 	int ret;
47 	u64 rate;
48 	struct scmi_clk *clk = to_scmi_clk(hw);
49 
50 	ret = scmi_proto_clk_ops->rate_get(clk->ph, clk->id, &rate);
51 	if (ret)
52 		return 0;
53 	return rate;
54 }
55 
56 static int scmi_clk_determine_rate(struct clk_hw *hw,
57 				   struct clk_rate_request *req)
58 {
59 	int ret;
60 	struct scmi_clk *clk = to_scmi_clk(hw);
61 
62 	/*
63 	 * If we could not get a better rate scmi_clk_recalc_rate() will be
64 	 * called after the rate is set and we'll know what rate the clock is
65 	 * running at then.
66 	 */
67 	ret = scmi_proto_clk_ops->determine_rate(clk->ph, clk->id, &req->rate);
68 	if (ret)
69 		return ret;
70 
71 	return 0;
72 }
73 
74 static int scmi_clk_set_rate(struct clk_hw *hw, unsigned long rate,
75 			     unsigned long parent_rate)
76 {
77 	struct scmi_clk *clk = to_scmi_clk(hw);
78 
79 	return scmi_proto_clk_ops->rate_set(clk->ph, clk->id, rate);
80 }
81 
82 static int scmi_clk_set_parent(struct clk_hw *hw, u8 parent_index)
83 {
84 	struct scmi_clk *clk = to_scmi_clk(hw);
85 
86 	return scmi_proto_clk_ops->parent_set(clk->ph, clk->id, parent_index);
87 }
88 
89 static u8 scmi_clk_get_parent(struct clk_hw *hw)
90 {
91 	struct scmi_clk *clk = to_scmi_clk(hw);
92 	u32 parent_id, p_idx;
93 	int ret;
94 
95 	ret = scmi_proto_clk_ops->parent_get(clk->ph, clk->id, &parent_id);
96 	if (ret)
97 		return 0;
98 
99 	for (p_idx = 0; p_idx < clk->info->num_parents; p_idx++) {
100 		if (clk->parent_data[p_idx].index == parent_id)
101 			break;
102 	}
103 
104 	if (p_idx == clk->info->num_parents)
105 		return 0;
106 
107 	return p_idx;
108 }
109 
110 static int scmi_clk_enable(struct clk_hw *hw)
111 {
112 	struct scmi_clk *clk = to_scmi_clk(hw);
113 
114 	return scmi_proto_clk_ops->enable(clk->ph, clk->id, NOT_ATOMIC);
115 }
116 
117 static void scmi_clk_disable(struct clk_hw *hw)
118 {
119 	struct scmi_clk *clk = to_scmi_clk(hw);
120 
121 	scmi_proto_clk_ops->disable(clk->ph, clk->id, NOT_ATOMIC);
122 }
123 
124 static int scmi_clk_atomic_enable(struct clk_hw *hw)
125 {
126 	struct scmi_clk *clk = to_scmi_clk(hw);
127 
128 	return scmi_proto_clk_ops->enable(clk->ph, clk->id, ATOMIC);
129 }
130 
131 static void scmi_clk_atomic_disable(struct clk_hw *hw)
132 {
133 	struct scmi_clk *clk = to_scmi_clk(hw);
134 
135 	scmi_proto_clk_ops->disable(clk->ph, clk->id, ATOMIC);
136 }
137 
138 static int __scmi_clk_is_enabled(struct clk_hw *hw, bool atomic)
139 {
140 	int ret;
141 	bool enabled = false;
142 	struct scmi_clk *clk = to_scmi_clk(hw);
143 
144 	ret = scmi_proto_clk_ops->state_get(clk->ph, clk->id, &enabled, atomic);
145 	if (ret)
146 		dev_warn(clk->dev,
147 			 "Failed to get state for clock ID %d\n", clk->id);
148 
149 	return !!enabled;
150 }
151 
152 static int scmi_clk_atomic_is_enabled(struct clk_hw *hw)
153 {
154 	return __scmi_clk_is_enabled(hw, ATOMIC);
155 }
156 
157 static int scmi_clk_is_enabled(struct clk_hw *hw)
158 {
159 	return __scmi_clk_is_enabled(hw, NOT_ATOMIC);
160 }
161 
162 static int scmi_clk_get_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
163 {
164 	int ret;
165 	u32 val;
166 	struct scmi_clk *clk = to_scmi_clk(hw);
167 
168 	ret = scmi_proto_clk_ops->config_oem_get(clk->ph, clk->id,
169 						 SCMI_CLOCK_CFG_DUTY_CYCLE,
170 						 &val, NULL, false);
171 	if (!ret) {
172 		duty->num = val;
173 		duty->den = 100;
174 	} else {
175 		dev_warn(clk->dev,
176 			 "Failed to get duty cycle for clock ID %d\n", clk->id);
177 	}
178 
179 	return ret;
180 }
181 
182 static int scmi_clk_set_duty_cycle(struct clk_hw *hw, struct clk_duty *duty)
183 {
184 	int ret;
185 	u32 val;
186 	struct scmi_clk *clk = to_scmi_clk(hw);
187 
188 	/* SCMI OEM Duty Cycle is expressed as a percentage */
189 	val = (duty->num * 100) / duty->den;
190 	ret = scmi_proto_clk_ops->config_oem_set(clk->ph, clk->id,
191 						 SCMI_CLOCK_CFG_DUTY_CYCLE,
192 						 val, false);
193 	if (ret)
194 		dev_warn(clk->dev,
195 			 "Failed to set duty cycle(%u/%u) for clock ID %d\n",
196 			 duty->num, duty->den, clk->id);
197 
198 	return ret;
199 }
200 
201 static int scmi_clk_ops_init(struct device *dev, struct scmi_clk *sclk,
202 			     const struct clk_ops *scmi_ops)
203 {
204 	int ret;
205 
206 	struct clk_init_data init = {
207 		.flags = CLK_GET_RATE_NOCACHE,
208 		.num_parents = sclk->info->num_parents,
209 		.ops = scmi_ops,
210 		.name = sclk->info->name,
211 		.parent_data = sclk->parent_data,
212 	};
213 
214 	sclk->hw.init = &init;
215 	ret = devm_clk_hw_register(dev, &sclk->hw);
216 	if (ret)
217 		return ret;
218 
219 	clk_hw_set_rate_range(&sclk->hw, sclk->info->min_rate,
220 			      sclk->info->max_rate);
221 	return ret;
222 }
223 
224 /**
225  * scmi_clk_ops_alloc() - Alloc and configure clock operations
226  * @dev: A device reference for devres
227  * @feats_key: A bitmap representing the desired clk_ops capabilities
228  *
229  * Allocate and configure a proper set of clock operations depending on the
230  * specifically required SCMI clock features.
231  *
232  * Return: A pointer to the allocated and configured clk_ops on success,
233  *	   or NULL on allocation failure.
234  */
235 static const struct clk_ops *
236 scmi_clk_ops_alloc(struct device *dev, unsigned long feats_key)
237 {
238 	struct clk_ops *ops;
239 
240 	ops = devm_kzalloc(dev, sizeof(*ops), GFP_KERNEL);
241 	if (!ops)
242 		return NULL;
243 	/*
244 	 * We can provide enable/disable/is_enabled atomic callbacks only if the
245 	 * underlying SCMI transport for an SCMI instance is configured to
246 	 * handle SCMI commands in an atomic manner.
247 	 *
248 	 * When no SCMI atomic transport support is available we instead provide
249 	 * only the prepare/unprepare API, as allowed by the clock framework
250 	 * when atomic calls are not available.
251 	 */
252 	if (feats_key & BIT(SCMI_CLK_STATE_CTRL_SUPPORTED)) {
253 		if (feats_key & BIT(SCMI_CLK_ATOMIC_SUPPORTED)) {
254 			ops->enable = scmi_clk_atomic_enable;
255 			ops->disable = scmi_clk_atomic_disable;
256 		} else {
257 			ops->prepare = scmi_clk_enable;
258 			ops->unprepare = scmi_clk_disable;
259 		}
260 	}
261 
262 	if (feats_key & BIT(SCMI_CLK_ATOMIC_SUPPORTED))
263 		ops->is_enabled = scmi_clk_atomic_is_enabled;
264 	else
265 		ops->is_prepared = scmi_clk_is_enabled;
266 
267 	/* Rate ops */
268 	ops->recalc_rate = scmi_clk_recalc_rate;
269 	ops->determine_rate = scmi_clk_determine_rate;
270 	if (feats_key & BIT(SCMI_CLK_RATE_CTRL_SUPPORTED))
271 		ops->set_rate = scmi_clk_set_rate;
272 
273 	/* Parent ops */
274 	ops->get_parent = scmi_clk_get_parent;
275 	if (feats_key & BIT(SCMI_CLK_PARENT_CTRL_SUPPORTED))
276 		ops->set_parent = scmi_clk_set_parent;
277 
278 	/* Duty cycle */
279 	if (feats_key & BIT(SCMI_CLK_DUTY_CYCLE_SUPPORTED)) {
280 		ops->get_duty_cycle = scmi_clk_get_duty_cycle;
281 		ops->set_duty_cycle = scmi_clk_set_duty_cycle;
282 	}
283 
284 	return ops;
285 }
286 
287 /**
288  * scmi_clk_ops_select() - Select a proper set of clock operations
289  * @sclk: A reference to an SCMI clock descriptor
290  * @atomic_capable: A flag to indicate if atomic mode is supported by the
291  *		    transport
292  * @atomic_threshold_us: Platform atomic threshold value in microseconds:
293  *			 clk_ops are atomic when clock enable latency is less
294  *			 than this threshold
295  * @clk_ops_db: A reference to the array used as a database to store all the
296  *		created clock operations combinations.
297  * @db_size: Maximum number of entries held by @clk_ops_db
298  *
299  * After having built a bitmap descriptor to represent the set of features
300  * needed by this SCMI clock, at first use it to lookup into the set of
301  * previously allocated clk_ops to check if a suitable combination of clock
302  * operations was already created; when no match is found allocate a brand new
303  * set of clk_ops satisfying the required combination of features and save it
304  * for future references.
305  *
306  * In this way only one set of clk_ops is ever created for each different
307  * combination that is effectively needed by a driver instance.
308  *
309  * Return: A pointer to the allocated and configured clk_ops on success, or
310  *	   NULL otherwise.
311  */
312 static const struct clk_ops *
313 scmi_clk_ops_select(struct scmi_clk *sclk, bool atomic_capable,
314 		    unsigned int atomic_threshold_us,
315 		    const struct clk_ops **clk_ops_db, size_t db_size)
316 {
317 	int ret;
318 	u32 val;
319 	const struct scmi_clock_info *ci = sclk->info;
320 	unsigned int feats_key = 0;
321 	const struct clk_ops *ops;
322 
323 	/*
324 	 * Note that when transport is atomic but SCMI protocol did not
325 	 * specify (or support) an enable_latency associated with a
326 	 * clock, we default to use atomic operations mode.
327 	 */
328 	if (atomic_capable && ci->enable_latency <= atomic_threshold_us)
329 		feats_key |= BIT(SCMI_CLK_ATOMIC_SUPPORTED);
330 
331 	if (!ci->state_ctrl_forbidden)
332 		feats_key |= BIT(SCMI_CLK_STATE_CTRL_SUPPORTED);
333 
334 	if (!ci->rate_ctrl_forbidden)
335 		feats_key |= BIT(SCMI_CLK_RATE_CTRL_SUPPORTED);
336 
337 	if (!ci->parent_ctrl_forbidden)
338 		feats_key |= BIT(SCMI_CLK_PARENT_CTRL_SUPPORTED);
339 
340 	if (ci->extended_config) {
341 		ret = scmi_proto_clk_ops->config_oem_get(sclk->ph, sclk->id,
342 						 SCMI_CLOCK_CFG_DUTY_CYCLE,
343 						 &val, NULL, false);
344 		if (!ret)
345 			feats_key |= BIT(SCMI_CLK_DUTY_CYCLE_SUPPORTED);
346 	}
347 
348 	if (WARN_ON(feats_key >= db_size))
349 		return NULL;
350 
351 	/* Lookup previously allocated ops */
352 	ops = clk_ops_db[feats_key];
353 	if (ops)
354 		return ops;
355 
356 	/* Did not find a pre-allocated clock_ops */
357 	ops = scmi_clk_ops_alloc(sclk->dev, feats_key);
358 	if (!ops)
359 		return NULL;
360 
361 	/* Store new ops combinations */
362 	clk_ops_db[feats_key] = ops;
363 
364 	return ops;
365 }
366 
367 static int scmi_clocks_probe(struct scmi_device *sdev)
368 {
369 	int idx, count, err;
370 	unsigned int atomic_threshold_us;
371 	bool transport_is_atomic;
372 	struct clk_hw **hws;
373 	struct clk_hw_onecell_data *clk_data;
374 	struct device *dev = &sdev->dev;
375 	struct device_node *np = dev->of_node;
376 	const struct scmi_handle *handle = sdev->handle;
377 	struct scmi_protocol_handle *ph;
378 	const struct clk_ops *scmi_clk_ops_db[SCMI_MAX_CLK_OPS] = {};
379 	struct scmi_clk *sclks;
380 
381 	if (!handle)
382 		return -ENODEV;
383 
384 	scmi_proto_clk_ops =
385 		handle->devm_protocol_get(sdev, SCMI_PROTOCOL_CLOCK, &ph);
386 	if (IS_ERR(scmi_proto_clk_ops))
387 		return PTR_ERR(scmi_proto_clk_ops);
388 
389 	count = scmi_proto_clk_ops->count_get(ph);
390 	if (count < 0) {
391 		dev_err(dev, "%pOFn: invalid clock output count\n", np);
392 		return -EINVAL;
393 	}
394 
395 	clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
396 				GFP_KERNEL);
397 	if (!clk_data)
398 		return -ENOMEM;
399 
400 	clk_data->num = count;
401 	hws = clk_data->hws;
402 
403 	transport_is_atomic = handle->is_transport_atomic(handle,
404 							  &atomic_threshold_us);
405 
406 	sclks = devm_kcalloc(dev, count, sizeof(*sclks), GFP_KERNEL);
407 	if (!sclks)
408 		return -ENOMEM;
409 
410 	for (idx = 0; idx < count; idx++)
411 		hws[idx] = &sclks[idx].hw;
412 
413 	for (idx = 0; idx < count; idx++) {
414 		struct scmi_clk *sclk = &sclks[idx];
415 		const struct clk_ops *scmi_ops;
416 
417 		sclk->info = scmi_proto_clk_ops->info_get(ph, idx);
418 		if (!sclk->info) {
419 			dev_dbg(dev, "invalid clock info for idx %d\n", idx);
420 			hws[idx] = NULL;
421 			continue;
422 		}
423 
424 		sclk->id = idx;
425 		sclk->ph = ph;
426 		sclk->dev = dev;
427 
428 		/*
429 		 * Note that the scmi_clk_ops_db is on the stack, not global,
430 		 * because it cannot be shared between multiple probe-sequences
431 		 * to avoid sharing the devm_ allocated clk_ops between multiple
432 		 * SCMI clk driver instances.
433 		 */
434 		scmi_ops = scmi_clk_ops_select(sclk, transport_is_atomic,
435 					       atomic_threshold_us,
436 					       scmi_clk_ops_db,
437 					       ARRAY_SIZE(scmi_clk_ops_db));
438 		if (!scmi_ops)
439 			return -ENOMEM;
440 
441 		/* Initialize clock parent data. */
442 		if (sclk->info->num_parents > 0) {
443 			sclk->parent_data = devm_kcalloc(dev, sclk->info->num_parents,
444 							 sizeof(*sclk->parent_data), GFP_KERNEL);
445 			if (!sclk->parent_data)
446 				return -ENOMEM;
447 
448 			for (int i = 0; i < sclk->info->num_parents; i++) {
449 				sclk->parent_data[i].index = sclk->info->parents[i];
450 				sclk->parent_data[i].hw = hws[sclk->info->parents[i]];
451 			}
452 		}
453 
454 		err = scmi_clk_ops_init(dev, sclk, scmi_ops);
455 		if (err) {
456 			dev_err(dev, "failed to register clock %d\n", idx);
457 			devm_kfree(dev, sclk->parent_data);
458 			hws[idx] = NULL;
459 		} else {
460 			dev_dbg(dev, "Registered clock:%s%s\n",
461 				sclk->info->name,
462 				scmi_ops->enable ? " (atomic ops)" : "");
463 		}
464 	}
465 
466 	return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
467 					   clk_data);
468 }
469 
470 static const struct scmi_device_id scmi_id_table[] = {
471 	{ SCMI_PROTOCOL_CLOCK, "clocks" },
472 	{ },
473 };
474 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
475 
476 static struct scmi_driver scmi_clocks_driver = {
477 	.name = "scmi-clocks",
478 	.probe = scmi_clocks_probe,
479 	.id_table = scmi_id_table,
480 };
481 module_scmi_driver(scmi_clocks_driver);
482 
483 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
484 MODULE_DESCRIPTION("ARM SCMI clock driver");
485 MODULE_LICENSE("GPL v2");
486