1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * System Control and Management Interface (SCMI) Clock Protocol
4 *
5 * Copyright (C) 2018-2022 ARM Ltd.
6 */
7
8 #include <linux/math64.h>
9 #include <linux/module.h>
10 #include <linux/limits.h>
11 #include <linux/sort.h>
12
13 #include "protocols.h"
14 #include "notify.h"
15 #include "quirks.h"
16
17 /* Updated only after ALL the mandatory features for that version are merged */
18 #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x30000
19
20 enum scmi_clock_protocol_cmd {
21 CLOCK_ATTRIBUTES = 0x3,
22 CLOCK_DESCRIBE_RATES = 0x4,
23 CLOCK_RATE_SET = 0x5,
24 CLOCK_RATE_GET = 0x6,
25 CLOCK_CONFIG_SET = 0x7,
26 CLOCK_NAME_GET = 0x8,
27 CLOCK_RATE_NOTIFY = 0x9,
28 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY = 0xA,
29 CLOCK_CONFIG_GET = 0xB,
30 CLOCK_POSSIBLE_PARENTS_GET = 0xC,
31 CLOCK_PARENT_SET = 0xD,
32 CLOCK_PARENT_GET = 0xE,
33 CLOCK_GET_PERMISSIONS = 0xF,
34 };
35
36 #define CLOCK_STATE_CONTROL_ALLOWED BIT(31)
37 #define CLOCK_PARENT_CONTROL_ALLOWED BIT(30)
38 #define CLOCK_RATE_CONTROL_ALLOWED BIT(29)
39
40 enum clk_state {
41 CLK_STATE_DISABLE,
42 CLK_STATE_ENABLE,
43 CLK_STATE_RESERVED,
44 CLK_STATE_UNCHANGED,
45 };
46
47 struct scmi_msg_resp_clock_protocol_attributes {
48 __le16 num_clocks;
49 u8 max_async_req;
50 u8 reserved;
51 };
52
53 struct scmi_msg_resp_clock_attributes {
54 __le32 attributes;
55 #define SUPPORTS_RATE_CHANGED_NOTIF(x) ((x) & BIT(31))
56 #define SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(x) ((x) & BIT(30))
57 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(29))
58 #define SUPPORTS_PARENT_CLOCK(x) ((x) & BIT(28))
59 #define SUPPORTS_EXTENDED_CONFIG(x) ((x) & BIT(27))
60 #define SUPPORTS_GET_PERMISSIONS(x) ((x) & BIT(1))
61 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
62 __le32 clock_enable_latency;
63 };
64
65 struct scmi_msg_clock_possible_parents {
66 __le32 id;
67 __le32 skip_parents;
68 };
69
70 struct scmi_msg_resp_clock_possible_parents {
71 __le32 num_parent_flags;
72 #define NUM_PARENTS_RETURNED(x) ((x) & 0xff)
73 #define NUM_PARENTS_REMAINING(x) ((x) >> 24)
74 __le32 possible_parents[];
75 };
76
77 struct scmi_msg_clock_set_parent {
78 __le32 id;
79 __le32 parent_id;
80 };
81
82 struct scmi_msg_clock_config_set {
83 __le32 id;
84 __le32 attributes;
85 };
86
87 /* Valid only from SCMI clock v2.1 */
88 struct scmi_msg_clock_config_set_v2 {
89 __le32 id;
90 __le32 attributes;
91 #define NULL_OEM_TYPE 0
92 #define REGMASK_OEM_TYPE_SET GENMASK(23, 16)
93 #define REGMASK_CLK_STATE GENMASK(1, 0)
94 __le32 oem_config_val;
95 };
96
97 struct scmi_msg_clock_config_get {
98 __le32 id;
99 __le32 flags;
100 #define REGMASK_OEM_TYPE_GET GENMASK(7, 0)
101 };
102
103 struct scmi_msg_resp_clock_config_get {
104 __le32 attributes;
105 __le32 config;
106 #define IS_CLK_ENABLED(x) le32_get_bits((x), BIT(0))
107 __le32 oem_config_val;
108 };
109
110 struct scmi_msg_clock_describe_rates {
111 __le32 id;
112 __le32 rate_index;
113 };
114
115 struct scmi_msg_resp_clock_describe_rates {
116 __le32 num_rates_flags;
117 #define NUM_RETURNED(x) ((x) & 0xfff)
118 #define RATE_DISCRETE(x) !((x) & BIT(12))
119 #define NUM_REMAINING(x) ((x) >> 16)
120 struct {
121 __le32 value_low;
122 __le32 value_high;
123 } rate[];
124 #define RATE_TO_U64(X) \
125 ({ \
126 typeof(X) x = (X); \
127 le32_to_cpu((x).value_low) | (u64)le32_to_cpu((x).value_high) << 32; \
128 })
129 };
130
131 struct scmi_clock_set_rate {
132 __le32 flags;
133 #define CLOCK_SET_ASYNC BIT(0)
134 #define CLOCK_SET_IGNORE_RESP BIT(1)
135 #define CLOCK_SET_ROUND_UP BIT(2)
136 #define CLOCK_SET_ROUND_AUTO BIT(3)
137 __le32 id;
138 __le32 value_low;
139 __le32 value_high;
140 };
141
142 struct scmi_msg_resp_set_rate_complete {
143 __le32 id;
144 __le32 rate_low;
145 __le32 rate_high;
146 };
147
148 struct scmi_msg_clock_rate_notify {
149 __le32 clk_id;
150 __le32 notify_enable;
151 };
152
153 struct scmi_clock_rate_notify_payld {
154 __le32 agent_id;
155 __le32 clock_id;
156 __le32 rate_low;
157 __le32 rate_high;
158 };
159
160 struct scmi_clock_desc {
161 u32 id;
162 unsigned int tot_rates;
163 struct scmi_clock_rates r;
164 #define RATE_MIN 0
165 #define RATE_MAX 1
166 #define RATE_STEP 2
167 struct scmi_clock_info info;
168 };
169
170 #define to_desc(p) (container_of(p, struct scmi_clock_desc, info))
171
172 struct clock_info {
173 int num_clocks;
174 int max_async_req;
175 bool notify_rate_changed_cmd;
176 bool notify_rate_change_requested_cmd;
177 atomic_t cur_async_req;
178 struct scmi_clock_desc *clkds;
179 #define CLOCK_INFO(c, i) (&(((c)->clkds + (i))->info))
180 int (*clock_config_set)(const struct scmi_protocol_handle *ph,
181 u32 clk_id, enum clk_state state,
182 enum scmi_clock_oem_config oem_type,
183 u32 oem_val, bool atomic);
184 int (*clock_config_get)(const struct scmi_protocol_handle *ph,
185 u32 clk_id, enum scmi_clock_oem_config oem_type,
186 u32 *attributes, bool *enabled, u32 *oem_val,
187 bool atomic);
188 };
189
190 static enum scmi_clock_protocol_cmd evt_2_cmd[] = {
191 CLOCK_RATE_NOTIFY,
192 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY,
193 };
194
195 static inline struct scmi_clock_info *
scmi_clock_domain_lookup(struct clock_info * ci,u32 clk_id)196 scmi_clock_domain_lookup(struct clock_info *ci, u32 clk_id)
197 {
198 if (clk_id >= ci->num_clocks)
199 return ERR_PTR(-EINVAL);
200
201 return CLOCK_INFO(ci, clk_id);
202 }
203
204 static int
scmi_clock_protocol_attributes_get(const struct scmi_protocol_handle * ph,struct clock_info * ci)205 scmi_clock_protocol_attributes_get(const struct scmi_protocol_handle *ph,
206 struct clock_info *ci)
207 {
208 int ret;
209 struct scmi_xfer *t;
210 struct scmi_msg_resp_clock_protocol_attributes *attr;
211
212 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
213 0, sizeof(*attr), &t);
214 if (ret)
215 return ret;
216
217 attr = t->rx.buf;
218
219 ret = ph->xops->do_xfer(ph, t);
220 if (!ret) {
221 ci->num_clocks = le16_to_cpu(attr->num_clocks);
222 ci->max_async_req = attr->max_async_req;
223 }
224
225 ph->xops->xfer_put(ph, t);
226
227 if (!ret) {
228 if (!ph->hops->protocol_msg_check(ph, CLOCK_RATE_NOTIFY, NULL))
229 ci->notify_rate_changed_cmd = true;
230
231 if (!ph->hops->protocol_msg_check(ph,
232 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY,
233 NULL))
234 ci->notify_rate_change_requested_cmd = true;
235 }
236
237 return ret;
238 }
239
240 struct scmi_clk_ipriv {
241 struct device *dev;
242 struct scmi_clock_desc *clkd;
243 };
244
iter_clk_possible_parents_prepare_message(void * message,unsigned int desc_index,const void * priv)245 static void iter_clk_possible_parents_prepare_message(void *message, unsigned int desc_index,
246 const void *priv)
247 {
248 struct scmi_msg_clock_possible_parents *msg = message;
249 const struct scmi_clk_ipriv *p = priv;
250
251 msg->id = cpu_to_le32(p->clkd->id);
252 /* Set the number of OPPs to be skipped/already read */
253 msg->skip_parents = cpu_to_le32(desc_index);
254 }
255
iter_clk_possible_parents_update_state(struct scmi_iterator_state * st,const void * response,void * priv)256 static int iter_clk_possible_parents_update_state(struct scmi_iterator_state *st,
257 const void *response, void *priv)
258 {
259 const struct scmi_msg_resp_clock_possible_parents *r = response;
260 struct scmi_clk_ipriv *p = priv;
261 u32 flags;
262
263 flags = le32_to_cpu(r->num_parent_flags);
264 st->num_returned = NUM_PARENTS_RETURNED(flags);
265 st->num_remaining = NUM_PARENTS_REMAINING(flags);
266
267 /*
268 * num parents is not declared previously anywhere so we
269 * assume it's returned+remaining on first call.
270 */
271 if (!st->max_resources) {
272 int num_parents = st->num_returned + st->num_remaining;
273
274 p->clkd->info.parents = devm_kcalloc(p->dev, num_parents,
275 sizeof(*p->clkd->info.parents),
276 GFP_KERNEL);
277 if (!p->clkd->info.parents)
278 return -ENOMEM;
279
280 /* max_resources is used by the iterators to control bounds */
281 st->max_resources = st->num_returned + st->num_remaining;
282 }
283
284 return 0;
285 }
286
iter_clk_possible_parents_process_response(const struct scmi_protocol_handle * ph,const void * response,struct scmi_iterator_state * st,void * priv)287 static int iter_clk_possible_parents_process_response(const struct scmi_protocol_handle *ph,
288 const void *response,
289 struct scmi_iterator_state *st,
290 void *priv)
291 {
292 const struct scmi_msg_resp_clock_possible_parents *r = response;
293 struct scmi_clk_ipriv *p = priv;
294
295 p->clkd->info.parents[st->desc_index + st->loop_idx] =
296 le32_to_cpu(r->possible_parents[st->loop_idx]);
297
298 /* Count only effectively discovered parents */
299 p->clkd->info.num_parents++;
300
301 return 0;
302 }
303
scmi_clock_possible_parents(const struct scmi_protocol_handle * ph,u32 clk_id,struct clock_info * cinfo)304 static int scmi_clock_possible_parents(const struct scmi_protocol_handle *ph,
305 u32 clk_id, struct clock_info *cinfo)
306 {
307 struct scmi_iterator_ops ops = {
308 .prepare_message = iter_clk_possible_parents_prepare_message,
309 .update_state = iter_clk_possible_parents_update_state,
310 .process_response = iter_clk_possible_parents_process_response,
311 };
312 struct scmi_clock_desc *clkd = &cinfo->clkds[clk_id];
313 struct scmi_clk_ipriv ppriv = {
314 .clkd = clkd,
315 .dev = ph->dev,
316 };
317 void *iter;
318
319 iter = ph->hops->iter_response_init(ph, &ops, 0,
320 CLOCK_POSSIBLE_PARENTS_GET,
321 sizeof(struct scmi_msg_clock_possible_parents),
322 &ppriv);
323 if (IS_ERR(iter))
324 return PTR_ERR(iter);
325
326 return ph->hops->iter_response_run(iter);
327 }
328
329 static int
scmi_clock_get_permissions(const struct scmi_protocol_handle * ph,u32 clk_id,struct scmi_clock_info * clk)330 scmi_clock_get_permissions(const struct scmi_protocol_handle *ph, u32 clk_id,
331 struct scmi_clock_info *clk)
332 {
333 struct scmi_xfer *t;
334 u32 perm;
335 int ret;
336
337 ret = ph->xops->xfer_get_init(ph, CLOCK_GET_PERMISSIONS,
338 sizeof(clk_id), sizeof(perm), &t);
339 if (ret)
340 return ret;
341
342 put_unaligned_le32(clk_id, t->tx.buf);
343
344 ret = ph->xops->do_xfer(ph, t);
345 if (!ret) {
346 perm = get_unaligned_le32(t->rx.buf);
347
348 clk->state_ctrl_forbidden = !(perm & CLOCK_STATE_CONTROL_ALLOWED);
349 clk->rate_ctrl_forbidden = !(perm & CLOCK_RATE_CONTROL_ALLOWED);
350 clk->parent_ctrl_forbidden = !(perm & CLOCK_PARENT_CONTROL_ALLOWED);
351 }
352
353 ph->xops->xfer_put(ph, t);
354
355 return ret;
356 }
357
scmi_clock_attributes_get(const struct scmi_protocol_handle * ph,u32 clk_id,struct clock_info * cinfo)358 static int scmi_clock_attributes_get(const struct scmi_protocol_handle *ph,
359 u32 clk_id, struct clock_info *cinfo)
360 {
361 int ret;
362 u32 attributes;
363 struct scmi_xfer *t;
364 struct scmi_msg_resp_clock_attributes *attr;
365 struct scmi_clock_info *clk = CLOCK_INFO(cinfo, clk_id);
366
367 ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
368 sizeof(clk_id), sizeof(*attr), &t);
369 if (ret)
370 return ret;
371
372 put_unaligned_le32(clk_id, t->tx.buf);
373 attr = t->rx.buf;
374
375 ret = ph->xops->do_xfer(ph, t);
376 if (!ret) {
377 u32 latency = 0;
378
379 attributes = le32_to_cpu(attr->attributes);
380 strscpy(clk->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
381 /* clock_enable_latency field is present only since SCMI v3.1 */
382 if (PROTOCOL_REV_MAJOR(ph->version) >= 0x2)
383 latency = le32_to_cpu(attr->clock_enable_latency);
384 clk->enable_latency = latency ? : U32_MAX;
385 }
386
387 ph->xops->xfer_put(ph, t);
388
389 /*
390 * If supported overwrite short name with the extended one;
391 * on error just carry on and use already provided short name.
392 */
393 if (!ret && PROTOCOL_REV_MAJOR(ph->version) >= 0x2) {
394 if (SUPPORTS_EXTENDED_NAMES(attributes))
395 ph->hops->extended_name_get(ph, CLOCK_NAME_GET, clk_id,
396 NULL, clk->name,
397 SCMI_MAX_STR_SIZE);
398
399 if (cinfo->notify_rate_changed_cmd &&
400 SUPPORTS_RATE_CHANGED_NOTIF(attributes))
401 clk->rate_changed_notifications = true;
402 if (cinfo->notify_rate_change_requested_cmd &&
403 SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(attributes))
404 clk->rate_change_requested_notifications = true;
405 if (PROTOCOL_REV_MAJOR(ph->version) >= 0x3) {
406 if (SUPPORTS_PARENT_CLOCK(attributes))
407 scmi_clock_possible_parents(ph, clk_id, cinfo);
408 if (SUPPORTS_GET_PERMISSIONS(attributes))
409 scmi_clock_get_permissions(ph, clk_id, clk);
410 if (SUPPORTS_EXTENDED_CONFIG(attributes))
411 clk->extended_config = true;
412 }
413 }
414
415 return ret;
416 }
417
rate_cmp_func(const void * _r1,const void * _r2)418 static int rate_cmp_func(const void *_r1, const void *_r2)
419 {
420 const u64 *r1 = _r1, *r2 = _r2;
421
422 if (*r1 < *r2)
423 return -1;
424 else if (*r1 == *r2)
425 return 0;
426 else
427 return 1;
428 }
429
iter_clk_describe_prepare_message(void * message,const unsigned int desc_index,const void * priv)430 static void iter_clk_describe_prepare_message(void *message,
431 const unsigned int desc_index,
432 const void *priv)
433 {
434 struct scmi_msg_clock_describe_rates *msg = message;
435 const struct scmi_clk_ipriv *p = priv;
436
437 msg->id = cpu_to_le32(p->clkd->id);
438 /* Set the number of rates to be skipped/already read */
439 msg->rate_index = cpu_to_le32(desc_index);
440 }
441
442 #define QUIRK_OUT_OF_SPEC_TRIPLET \
443 ({ \
444 /* \
445 * A known quirk: a triplet is returned but num_returned != 3 \
446 * Check for a safe payload size and fix. \
447 */ \
448 if (st->num_returned != 3 && st->num_remaining == 0 && \
449 st->rx_len == sizeof(*r) + sizeof(__le32) * 2 * 3) { \
450 st->num_returned = 3; \
451 st->num_remaining = 0; \
452 } else { \
453 dev_err(p->dev, \
454 "Cannot fix out-of-spec reply !\n"); \
455 return -EPROTO; \
456 } \
457 })
458
459 static int
iter_clk_describe_update_state(struct scmi_iterator_state * st,const void * response,void * priv)460 iter_clk_describe_update_state(struct scmi_iterator_state *st,
461 const void *response, void *priv)
462 {
463 u32 flags;
464 struct scmi_clk_ipriv *p = priv;
465 const struct scmi_msg_resp_clock_describe_rates *r = response;
466
467 flags = le32_to_cpu(r->num_rates_flags);
468 st->num_remaining = NUM_REMAINING(flags);
469 st->num_returned = NUM_RETURNED(flags);
470 p->clkd->r.rate_discrete = RATE_DISCRETE(flags);
471
472 /* Warn about out of spec replies ... */
473 if (!p->clkd->r.rate_discrete &&
474 (st->num_returned != 3 || st->num_remaining != 0)) {
475 dev_warn(p->dev,
476 "Out-of-spec CLOCK_DESCRIBE_RATES reply for %s - returned:%d remaining:%d rx_len:%zd\n",
477 p->clkd->info.name, st->num_returned, st->num_remaining,
478 st->rx_len);
479
480 SCMI_QUIRK(clock_rates_triplet_out_of_spec,
481 QUIRK_OUT_OF_SPEC_TRIPLET);
482 }
483
484 if (!st->max_resources) {
485 unsigned int tot_rates = st->num_returned + st->num_remaining;
486
487 p->clkd->r.rates = devm_kcalloc(p->dev, tot_rates,
488 sizeof(*p->clkd->r.rates), GFP_KERNEL);
489 if (!p->clkd->r.rates)
490 return -ENOMEM;
491
492 /* max_resources is used by the iterators to control bounds */
493 p->clkd->tot_rates = tot_rates;
494 st->max_resources = tot_rates;
495 }
496
497 return 0;
498 }
499
500 static int
iter_clk_describe_process_response(const struct scmi_protocol_handle * ph,const void * response,struct scmi_iterator_state * st,void * priv)501 iter_clk_describe_process_response(const struct scmi_protocol_handle *ph,
502 const void *response,
503 struct scmi_iterator_state *st, void *priv)
504 {
505 struct scmi_clk_ipriv *p = priv;
506 const struct scmi_msg_resp_clock_describe_rates *r = response;
507
508 p->clkd->r.rates[p->clkd->r.num_rates] = RATE_TO_U64(r->rate[st->loop_idx]);
509
510 /* Count only effectively discovered rates */
511 p->clkd->r.num_rates++;
512
513 return 0;
514 }
515
516 static int
scmi_clock_describe_rates_get_full(const struct scmi_protocol_handle * ph,struct scmi_clock_desc * clkd)517 scmi_clock_describe_rates_get_full(const struct scmi_protocol_handle *ph,
518 struct scmi_clock_desc *clkd)
519 {
520 int ret;
521 void *iter;
522 struct scmi_iterator_ops ops = {
523 .prepare_message = iter_clk_describe_prepare_message,
524 .update_state = iter_clk_describe_update_state,
525 .process_response = iter_clk_describe_process_response,
526 };
527 struct scmi_clk_ipriv cpriv = {
528 .clkd = clkd,
529 .dev = ph->dev,
530 };
531
532 /*
533 * Using tot_rates as max_resources parameter here so as to trigger
534 * the dynamic allocation only when strictly needed: when trying a
535 * full enumeration after a lazy one tot_rates will be non-zero.
536 */
537 iter = ph->hops->iter_response_init(ph, &ops, clkd->tot_rates,
538 CLOCK_DESCRIBE_RATES,
539 sizeof(struct scmi_msg_clock_describe_rates),
540 &cpriv);
541 if (IS_ERR(iter))
542 return PTR_ERR(iter);
543
544 ret = ph->hops->iter_response_run(iter);
545 if (ret)
546 return ret;
547
548 /* empty set ? */
549 if (!clkd->r.num_rates)
550 return 0;
551
552 if (clkd->r.rate_discrete && PROTOCOL_REV_MAJOR(ph->version) == 0x1)
553 sort(clkd->r.rates, clkd->r.num_rates,
554 sizeof(clkd->r.rates[0]), rate_cmp_func, NULL);
555
556 return 0;
557 }
558
559 static int
scmi_clock_describe_rates_get_lazy(const struct scmi_protocol_handle * ph,struct scmi_clock_desc * clkd)560 scmi_clock_describe_rates_get_lazy(const struct scmi_protocol_handle *ph,
561 struct scmi_clock_desc *clkd)
562 {
563 struct scmi_iterator_ops ops = {
564 .prepare_message = iter_clk_describe_prepare_message,
565 .update_state = iter_clk_describe_update_state,
566 .process_response = iter_clk_describe_process_response,
567 };
568 struct scmi_clk_ipriv cpriv = {
569 .clkd = clkd,
570 .dev = ph->dev,
571 };
572 unsigned int first, last;
573 void *iter;
574 int ret;
575
576 iter = ph->hops->iter_response_init(ph, &ops, 0, CLOCK_DESCRIBE_RATES,
577 sizeof(struct scmi_msg_clock_describe_rates),
578 &cpriv);
579 if (IS_ERR(iter))
580 return PTR_ERR(iter);
581
582 /* Try to grab a triplet, so that in case is NON-discrete we are done */
583 first = 0;
584 last = 2;
585 ret = ph->hops->iter_response_run_bound(iter, &first, &last);
586 if (ret)
587 goto out;
588
589 /*
590 * If discrete and we don't already have it, grab the last value, which
591 * should be the max
592 */
593 if (clkd->r.rate_discrete && clkd->tot_rates > clkd->r.num_rates) {
594 first = clkd->tot_rates - 1;
595 last = clkd->tot_rates - 1;
596 ret = ph->hops->iter_response_run_bound(iter, &first, &last);
597 }
598
599 out:
600 ph->hops->iter_response_bound_cleanup(iter);
601
602 return ret;
603 }
604
605 static int
scmi_clock_describe_rates_get(const struct scmi_protocol_handle * ph,u32 clk_id,struct clock_info * cinfo)606 scmi_clock_describe_rates_get(const struct scmi_protocol_handle *ph,
607 u32 clk_id, struct clock_info *cinfo)
608 {
609 struct scmi_clock_desc *clkd = &cinfo->clkds[clk_id];
610 int ret;
611
612 /*
613 * Since only after SCMI Clock v1.0 the returned rates are guaranteed to
614 * be discovered in ascending order, lazy enumeration cannot be use for
615 * SCMI Clock v1.0 protocol.
616 */
617 if (PROTOCOL_REV_MAJOR(ph->version) > 0x1)
618 ret = scmi_clock_describe_rates_get_lazy(ph, clkd);
619 else
620 ret = scmi_clock_describe_rates_get_full(ph, clkd);
621
622 if (ret)
623 return ret;
624
625 clkd->info.min_rate = clkd->r.rates[RATE_MIN];
626 if (!clkd->r.rate_discrete) {
627 clkd->info.max_rate = clkd->r.rates[RATE_MAX];
628 dev_dbg(ph->dev, "Min %llu Max %llu Step %llu Hz\n",
629 clkd->r.rates[RATE_MIN], clkd->r.rates[RATE_MAX],
630 clkd->r.rates[RATE_STEP]);
631 } else {
632 clkd->info.max_rate = clkd->r.rates[clkd->r.num_rates - 1];
633 dev_dbg(ph->dev, "Clock:%s Num_Rates:%u -> Min %llu Max %llu\n",
634 clkd->info.name, clkd->tot_rates,
635 clkd->info.min_rate, clkd->info.max_rate);
636 }
637
638 return 0;
639 }
640
641 static int
scmi_clock_rate_get(const struct scmi_protocol_handle * ph,u32 clk_id,u64 * value)642 scmi_clock_rate_get(const struct scmi_protocol_handle *ph,
643 u32 clk_id, u64 *value)
644 {
645 int ret;
646 struct scmi_xfer *t;
647
648 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_GET,
649 sizeof(__le32), sizeof(u64), &t);
650 if (ret)
651 return ret;
652
653 put_unaligned_le32(clk_id, t->tx.buf);
654
655 ret = ph->xops->do_xfer(ph, t);
656 if (!ret)
657 *value = get_unaligned_le64(t->rx.buf);
658
659 ph->xops->xfer_put(ph, t);
660 return ret;
661 }
662
scmi_clock_rate_set(const struct scmi_protocol_handle * ph,u32 clk_id,u64 rate)663 static int scmi_clock_rate_set(const struct scmi_protocol_handle *ph,
664 u32 clk_id, u64 rate)
665 {
666 int ret;
667 u32 flags = 0;
668 struct scmi_xfer *t;
669 struct scmi_clock_set_rate *cfg;
670 struct clock_info *ci = ph->get_priv(ph);
671 struct scmi_clock_info *clk;
672
673 clk = scmi_clock_domain_lookup(ci, clk_id);
674 if (IS_ERR(clk))
675 return PTR_ERR(clk);
676
677 if (clk->rate_ctrl_forbidden)
678 return -EACCES;
679
680 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_SET, sizeof(*cfg), 0, &t);
681 if (ret)
682 return ret;
683
684 if (ci->max_async_req &&
685 atomic_inc_return(&ci->cur_async_req) < ci->max_async_req)
686 flags |= CLOCK_SET_ASYNC;
687
688 cfg = t->tx.buf;
689 cfg->flags = cpu_to_le32(flags);
690 cfg->id = cpu_to_le32(clk_id);
691 cfg->value_low = cpu_to_le32(rate & 0xffffffff);
692 cfg->value_high = cpu_to_le32(rate >> 32);
693
694 if (flags & CLOCK_SET_ASYNC) {
695 ret = ph->xops->do_xfer_with_response(ph, t);
696 if (!ret) {
697 struct scmi_msg_resp_set_rate_complete *resp;
698
699 resp = t->rx.buf;
700 if (le32_to_cpu(resp->id) == clk_id)
701 dev_dbg(ph->dev,
702 "Clk ID %d set async to %llu\n", clk_id,
703 get_unaligned_le64(&resp->rate_low));
704 else
705 ret = -EPROTO;
706 }
707 } else {
708 ret = ph->xops->do_xfer(ph, t);
709 }
710
711 if (ci->max_async_req)
712 atomic_dec(&ci->cur_async_req);
713
714 ph->xops->xfer_put(ph, t);
715 return ret;
716 }
717
scmi_clock_determine_rate(const struct scmi_protocol_handle * ph,u32 clk_id,unsigned long * rate)718 static int scmi_clock_determine_rate(const struct scmi_protocol_handle *ph,
719 u32 clk_id, unsigned long *rate)
720 {
721 u64 fmin, fmax, ftmp, step;
722 struct scmi_clock_info *clk;
723 struct scmi_clock_desc *clkd;
724 struct clock_info *ci = ph->get_priv(ph);
725
726 if (!rate)
727 return -EINVAL;
728
729 clk = scmi_clock_domain_lookup(ci, clk_id);
730 if (IS_ERR(clk))
731 return PTR_ERR(clk);
732
733 clkd = to_desc(clk);
734
735 /*
736 * If we can't figure out what rate it will be, so just return the
737 * rate back to the caller.
738 */
739 if (clkd->r.rate_discrete)
740 return 0;
741
742 fmin = clk->min_rate;
743 fmax = clk->max_rate;
744 if (*rate <= fmin) {
745 *rate = fmin;
746 return 0;
747 } else if (*rate >= fmax) {
748 *rate = fmax;
749 return 0;
750 }
751
752 step = clkd->r.rates[RATE_STEP];
753 if (!step)
754 return -EINVAL;
755
756 ftmp = *rate - fmin;
757 ftmp = DIV64_U64_ROUND_UP(ftmp, step);
758
759 *rate = ftmp * step + fmin;
760
761 return 0;
762 }
763
764 static const struct scmi_clock_rates *
scmi_clock_all_rates_get(const struct scmi_protocol_handle * ph,u32 clk_id)765 scmi_clock_all_rates_get(const struct scmi_protocol_handle *ph, u32 clk_id)
766 {
767 struct clock_info *ci = ph->get_priv(ph);
768 struct scmi_clock_desc *clkd;
769 struct scmi_clock_info *clk;
770
771 clk = scmi_clock_domain_lookup(ci, clk_id);
772 if (IS_ERR(clk) || !clk->name[0])
773 return NULL;
774
775 clkd = to_desc(clk);
776 /* Needs full enumeration ? */
777 if (clkd->r.rate_discrete && clkd->tot_rates != clkd->r.num_rates) {
778 int ret;
779
780 /* rates[] is already allocated BUT we need to re-enumerate */
781 clkd->r.num_rates = 0;
782 ret = scmi_clock_describe_rates_get_full(ph, clkd);
783 if (ret)
784 return NULL;
785 }
786
787 return &clkd->r;
788 }
789
790 static int
scmi_clock_config_set(const struct scmi_protocol_handle * ph,u32 clk_id,enum clk_state state,enum scmi_clock_oem_config __unused0,u32 __unused1,bool atomic)791 scmi_clock_config_set(const struct scmi_protocol_handle *ph, u32 clk_id,
792 enum clk_state state,
793 enum scmi_clock_oem_config __unused0, u32 __unused1,
794 bool atomic)
795 {
796 int ret;
797 struct scmi_xfer *t;
798 struct scmi_msg_clock_config_set *cfg;
799
800 if (state >= CLK_STATE_RESERVED)
801 return -EINVAL;
802
803 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET,
804 sizeof(*cfg), 0, &t);
805 if (ret)
806 return ret;
807
808 t->hdr.poll_completion = atomic;
809
810 cfg = t->tx.buf;
811 cfg->id = cpu_to_le32(clk_id);
812 cfg->attributes = cpu_to_le32(state);
813
814 ret = ph->xops->do_xfer(ph, t);
815
816 ph->xops->xfer_put(ph, t);
817 return ret;
818 }
819
820 static int
scmi_clock_set_parent(const struct scmi_protocol_handle * ph,u32 clk_id,u32 parent_id)821 scmi_clock_set_parent(const struct scmi_protocol_handle *ph, u32 clk_id,
822 u32 parent_id)
823 {
824 int ret;
825 struct scmi_xfer *t;
826 struct scmi_msg_clock_set_parent *cfg;
827 struct clock_info *ci = ph->get_priv(ph);
828 struct scmi_clock_info *clk;
829
830 clk = scmi_clock_domain_lookup(ci, clk_id);
831 if (IS_ERR(clk))
832 return PTR_ERR(clk);
833
834 if (parent_id >= clk->num_parents)
835 return -EINVAL;
836
837 if (clk->parent_ctrl_forbidden)
838 return -EACCES;
839
840 ret = ph->xops->xfer_get_init(ph, CLOCK_PARENT_SET,
841 sizeof(*cfg), 0, &t);
842 if (ret)
843 return ret;
844
845 t->hdr.poll_completion = false;
846
847 cfg = t->tx.buf;
848 cfg->id = cpu_to_le32(clk_id);
849 cfg->parent_id = cpu_to_le32(clk->parents[parent_id]);
850
851 ret = ph->xops->do_xfer(ph, t);
852
853 ph->xops->xfer_put(ph, t);
854
855 return ret;
856 }
857
858 static int
scmi_clock_get_parent(const struct scmi_protocol_handle * ph,u32 clk_id,u32 * parent_id)859 scmi_clock_get_parent(const struct scmi_protocol_handle *ph, u32 clk_id,
860 u32 *parent_id)
861 {
862 int ret;
863 struct scmi_xfer *t;
864
865 ret = ph->xops->xfer_get_init(ph, CLOCK_PARENT_GET,
866 sizeof(__le32), sizeof(u32), &t);
867 if (ret)
868 return ret;
869
870 put_unaligned_le32(clk_id, t->tx.buf);
871
872 ret = ph->xops->do_xfer(ph, t);
873 if (!ret)
874 *parent_id = get_unaligned_le32(t->rx.buf);
875
876 ph->xops->xfer_put(ph, t);
877 return ret;
878 }
879
880 /* For SCMI clock v3.0 and onwards */
881 static int
scmi_clock_config_set_v2(const struct scmi_protocol_handle * ph,u32 clk_id,enum clk_state state,enum scmi_clock_oem_config oem_type,u32 oem_val,bool atomic)882 scmi_clock_config_set_v2(const struct scmi_protocol_handle *ph, u32 clk_id,
883 enum clk_state state,
884 enum scmi_clock_oem_config oem_type, u32 oem_val,
885 bool atomic)
886 {
887 int ret;
888 u32 attrs;
889 struct scmi_xfer *t;
890 struct scmi_msg_clock_config_set_v2 *cfg;
891
892 if (state == CLK_STATE_RESERVED ||
893 (!oem_type && state == CLK_STATE_UNCHANGED))
894 return -EINVAL;
895
896 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET,
897 sizeof(*cfg), 0, &t);
898 if (ret)
899 return ret;
900
901 t->hdr.poll_completion = atomic;
902
903 attrs = FIELD_PREP(REGMASK_OEM_TYPE_SET, oem_type) |
904 FIELD_PREP(REGMASK_CLK_STATE, state);
905
906 cfg = t->tx.buf;
907 cfg->id = cpu_to_le32(clk_id);
908 cfg->attributes = cpu_to_le32(attrs);
909 /* Clear in any case */
910 cfg->oem_config_val = cpu_to_le32(0);
911 if (oem_type)
912 cfg->oem_config_val = cpu_to_le32(oem_val);
913
914 ret = ph->xops->do_xfer(ph, t);
915
916 ph->xops->xfer_put(ph, t);
917 return ret;
918 }
919
scmi_clock_enable(const struct scmi_protocol_handle * ph,u32 clk_id,bool atomic)920 static int scmi_clock_enable(const struct scmi_protocol_handle *ph, u32 clk_id,
921 bool atomic)
922 {
923 struct clock_info *ci = ph->get_priv(ph);
924 struct scmi_clock_info *clk;
925
926 clk = scmi_clock_domain_lookup(ci, clk_id);
927 if (IS_ERR(clk))
928 return PTR_ERR(clk);
929
930 if (clk->state_ctrl_forbidden)
931 return -EACCES;
932
933 return ci->clock_config_set(ph, clk_id, CLK_STATE_ENABLE,
934 NULL_OEM_TYPE, 0, atomic);
935 }
936
scmi_clock_disable(const struct scmi_protocol_handle * ph,u32 clk_id,bool atomic)937 static int scmi_clock_disable(const struct scmi_protocol_handle *ph, u32 clk_id,
938 bool atomic)
939 {
940 struct clock_info *ci = ph->get_priv(ph);
941 struct scmi_clock_info *clk;
942
943 clk = scmi_clock_domain_lookup(ci, clk_id);
944 if (IS_ERR(clk))
945 return PTR_ERR(clk);
946
947 if (clk->state_ctrl_forbidden)
948 return -EACCES;
949
950 return ci->clock_config_set(ph, clk_id, CLK_STATE_DISABLE,
951 NULL_OEM_TYPE, 0, atomic);
952 }
953
954 /* For SCMI clock v3.0 and onwards */
955 static int
scmi_clock_config_get_v2(const struct scmi_protocol_handle * ph,u32 clk_id,enum scmi_clock_oem_config oem_type,u32 * attributes,bool * enabled,u32 * oem_val,bool atomic)956 scmi_clock_config_get_v2(const struct scmi_protocol_handle *ph, u32 clk_id,
957 enum scmi_clock_oem_config oem_type, u32 *attributes,
958 bool *enabled, u32 *oem_val, bool atomic)
959 {
960 int ret;
961 u32 flags;
962 struct scmi_xfer *t;
963 struct scmi_msg_clock_config_get *cfg;
964
965 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_GET,
966 sizeof(*cfg), 0, &t);
967 if (ret)
968 return ret;
969
970 t->hdr.poll_completion = atomic;
971
972 flags = FIELD_PREP(REGMASK_OEM_TYPE_GET, oem_type);
973
974 cfg = t->tx.buf;
975 cfg->id = cpu_to_le32(clk_id);
976 cfg->flags = cpu_to_le32(flags);
977
978 ret = ph->xops->do_xfer(ph, t);
979 if (!ret) {
980 struct scmi_msg_resp_clock_config_get *resp = t->rx.buf;
981
982 if (attributes)
983 *attributes = le32_to_cpu(resp->attributes);
984
985 if (enabled)
986 *enabled = IS_CLK_ENABLED(resp->config);
987
988 if (oem_val && oem_type)
989 *oem_val = le32_to_cpu(resp->oem_config_val);
990 }
991
992 ph->xops->xfer_put(ph, t);
993
994 return ret;
995 }
996
997 static int
scmi_clock_config_get(const struct scmi_protocol_handle * ph,u32 clk_id,enum scmi_clock_oem_config oem_type,u32 * attributes,bool * enabled,u32 * oem_val,bool atomic)998 scmi_clock_config_get(const struct scmi_protocol_handle *ph, u32 clk_id,
999 enum scmi_clock_oem_config oem_type, u32 *attributes,
1000 bool *enabled, u32 *oem_val, bool atomic)
1001 {
1002 int ret;
1003 struct scmi_xfer *t;
1004 struct scmi_msg_resp_clock_attributes *resp;
1005
1006 if (!enabled)
1007 return -EINVAL;
1008
1009 ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
1010 sizeof(clk_id), sizeof(*resp), &t);
1011 if (ret)
1012 return ret;
1013
1014 t->hdr.poll_completion = atomic;
1015 put_unaligned_le32(clk_id, t->tx.buf);
1016 resp = t->rx.buf;
1017
1018 ret = ph->xops->do_xfer(ph, t);
1019 if (!ret)
1020 *enabled = IS_CLK_ENABLED(resp->attributes);
1021
1022 ph->xops->xfer_put(ph, t);
1023
1024 return ret;
1025 }
1026
scmi_clock_state_get(const struct scmi_protocol_handle * ph,u32 clk_id,bool * enabled,bool atomic)1027 static int scmi_clock_state_get(const struct scmi_protocol_handle *ph,
1028 u32 clk_id, bool *enabled, bool atomic)
1029 {
1030 struct clock_info *ci = ph->get_priv(ph);
1031
1032 return ci->clock_config_get(ph, clk_id, NULL_OEM_TYPE, NULL,
1033 enabled, NULL, atomic);
1034 }
1035
scmi_clock_config_oem_set(const struct scmi_protocol_handle * ph,u32 clk_id,enum scmi_clock_oem_config oem_type,u32 oem_val,bool atomic)1036 static int scmi_clock_config_oem_set(const struct scmi_protocol_handle *ph,
1037 u32 clk_id,
1038 enum scmi_clock_oem_config oem_type,
1039 u32 oem_val, bool atomic)
1040 {
1041 struct clock_info *ci = ph->get_priv(ph);
1042 struct scmi_clock_info *clk;
1043
1044 clk = scmi_clock_domain_lookup(ci, clk_id);
1045 if (IS_ERR(clk))
1046 return PTR_ERR(clk);
1047
1048 if (!clk->extended_config)
1049 return -EOPNOTSUPP;
1050
1051 return ci->clock_config_set(ph, clk_id, CLK_STATE_UNCHANGED,
1052 oem_type, oem_val, atomic);
1053 }
1054
scmi_clock_config_oem_get(const struct scmi_protocol_handle * ph,u32 clk_id,enum scmi_clock_oem_config oem_type,u32 * oem_val,u32 * attributes,bool atomic)1055 static int scmi_clock_config_oem_get(const struct scmi_protocol_handle *ph,
1056 u32 clk_id,
1057 enum scmi_clock_oem_config oem_type,
1058 u32 *oem_val, u32 *attributes, bool atomic)
1059 {
1060 struct clock_info *ci = ph->get_priv(ph);
1061 struct scmi_clock_info *clk;
1062
1063 clk = scmi_clock_domain_lookup(ci, clk_id);
1064 if (IS_ERR(clk))
1065 return PTR_ERR(clk);
1066
1067 if (!clk->extended_config)
1068 return -EOPNOTSUPP;
1069
1070 return ci->clock_config_get(ph, clk_id, oem_type, attributes,
1071 NULL, oem_val, atomic);
1072 }
1073
scmi_clock_count_get(const struct scmi_protocol_handle * ph)1074 static int scmi_clock_count_get(const struct scmi_protocol_handle *ph)
1075 {
1076 struct clock_info *ci = ph->get_priv(ph);
1077
1078 return ci->num_clocks;
1079 }
1080
1081 static const struct scmi_clock_info *
scmi_clock_info_get(const struct scmi_protocol_handle * ph,u32 clk_id)1082 scmi_clock_info_get(const struct scmi_protocol_handle *ph, u32 clk_id)
1083 {
1084 struct scmi_clock_info *clk;
1085 struct clock_info *ci = ph->get_priv(ph);
1086
1087 clk = scmi_clock_domain_lookup(ci, clk_id);
1088 if (IS_ERR(clk))
1089 return NULL;
1090
1091 if (!clk->name[0])
1092 return NULL;
1093
1094 return clk;
1095 }
1096
1097 static const struct scmi_clk_proto_ops clk_proto_ops = {
1098 .count_get = scmi_clock_count_get,
1099 .info_get = scmi_clock_info_get,
1100 .rate_get = scmi_clock_rate_get,
1101 .rate_set = scmi_clock_rate_set,
1102 .determine_rate = scmi_clock_determine_rate,
1103 .all_rates_get = scmi_clock_all_rates_get,
1104 .enable = scmi_clock_enable,
1105 .disable = scmi_clock_disable,
1106 .state_get = scmi_clock_state_get,
1107 .config_oem_get = scmi_clock_config_oem_get,
1108 .config_oem_set = scmi_clock_config_oem_set,
1109 .parent_set = scmi_clock_set_parent,
1110 .parent_get = scmi_clock_get_parent,
1111 };
1112
scmi_clk_notify_supported(const struct scmi_protocol_handle * ph,u8 evt_id,u32 src_id)1113 static bool scmi_clk_notify_supported(const struct scmi_protocol_handle *ph,
1114 u8 evt_id, u32 src_id)
1115 {
1116 bool supported;
1117 struct scmi_clock_info *clk;
1118 struct clock_info *ci = ph->get_priv(ph);
1119
1120 if (evt_id >= ARRAY_SIZE(evt_2_cmd))
1121 return false;
1122
1123 clk = scmi_clock_domain_lookup(ci, src_id);
1124 if (IS_ERR(clk))
1125 return false;
1126
1127 if (evt_id == SCMI_EVENT_CLOCK_RATE_CHANGED)
1128 supported = clk->rate_changed_notifications;
1129 else
1130 supported = clk->rate_change_requested_notifications;
1131
1132 return supported;
1133 }
1134
scmi_clk_rate_notify(const struct scmi_protocol_handle * ph,u32 clk_id,int message_id,bool enable)1135 static int scmi_clk_rate_notify(const struct scmi_protocol_handle *ph,
1136 u32 clk_id, int message_id, bool enable)
1137 {
1138 int ret;
1139 struct scmi_xfer *t;
1140 struct scmi_msg_clock_rate_notify *notify;
1141
1142 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
1143 if (ret)
1144 return ret;
1145
1146 notify = t->tx.buf;
1147 notify->clk_id = cpu_to_le32(clk_id);
1148 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
1149
1150 ret = ph->xops->do_xfer(ph, t);
1151
1152 ph->xops->xfer_put(ph, t);
1153 return ret;
1154 }
1155
scmi_clk_set_notify_enabled(const struct scmi_protocol_handle * ph,u8 evt_id,u32 src_id,bool enable)1156 static int scmi_clk_set_notify_enabled(const struct scmi_protocol_handle *ph,
1157 u8 evt_id, u32 src_id, bool enable)
1158 {
1159 int ret, cmd_id;
1160
1161 if (evt_id >= ARRAY_SIZE(evt_2_cmd))
1162 return -EINVAL;
1163
1164 cmd_id = evt_2_cmd[evt_id];
1165 ret = scmi_clk_rate_notify(ph, src_id, cmd_id, enable);
1166 if (ret)
1167 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
1168 evt_id, src_id, ret);
1169
1170 return ret;
1171 }
1172
scmi_clk_fill_custom_report(const struct scmi_protocol_handle * ph,u8 evt_id,ktime_t timestamp,const void * payld,size_t payld_sz,void * report,u32 * src_id)1173 static void *scmi_clk_fill_custom_report(const struct scmi_protocol_handle *ph,
1174 u8 evt_id, ktime_t timestamp,
1175 const void *payld, size_t payld_sz,
1176 void *report, u32 *src_id)
1177 {
1178 const struct scmi_clock_rate_notify_payld *p = payld;
1179 struct scmi_clock_rate_notif_report *r = report;
1180
1181 if (sizeof(*p) != payld_sz ||
1182 (evt_id != SCMI_EVENT_CLOCK_RATE_CHANGED &&
1183 evt_id != SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED))
1184 return NULL;
1185
1186 r->timestamp = timestamp;
1187 r->agent_id = le32_to_cpu(p->agent_id);
1188 r->clock_id = le32_to_cpu(p->clock_id);
1189 r->rate = get_unaligned_le64(&p->rate_low);
1190 *src_id = r->clock_id;
1191
1192 return r;
1193 }
1194
scmi_clk_get_num_sources(const struct scmi_protocol_handle * ph)1195 static int scmi_clk_get_num_sources(const struct scmi_protocol_handle *ph)
1196 {
1197 struct clock_info *ci = ph->get_priv(ph);
1198
1199 if (!ci)
1200 return -EINVAL;
1201
1202 return ci->num_clocks;
1203 }
1204
1205 static const struct scmi_event clk_events[] = {
1206 {
1207 .id = SCMI_EVENT_CLOCK_RATE_CHANGED,
1208 .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld),
1209 .max_report_sz = sizeof(struct scmi_clock_rate_notif_report),
1210 },
1211 {
1212 .id = SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED,
1213 .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld),
1214 .max_report_sz = sizeof(struct scmi_clock_rate_notif_report),
1215 },
1216 };
1217
1218 static const struct scmi_event_ops clk_event_ops = {
1219 .is_notify_supported = scmi_clk_notify_supported,
1220 .get_num_sources = scmi_clk_get_num_sources,
1221 .set_notify_enabled = scmi_clk_set_notify_enabled,
1222 .fill_custom_report = scmi_clk_fill_custom_report,
1223 };
1224
1225 static const struct scmi_protocol_events clk_protocol_events = {
1226 .queue_sz = SCMI_PROTO_QUEUE_SZ,
1227 .ops = &clk_event_ops,
1228 .evts = clk_events,
1229 .num_events = ARRAY_SIZE(clk_events),
1230 };
1231
scmi_clock_protocol_init(const struct scmi_protocol_handle * ph)1232 static int scmi_clock_protocol_init(const struct scmi_protocol_handle *ph)
1233 {
1234 int clkid, ret;
1235 struct clock_info *cinfo;
1236
1237 dev_dbg(ph->dev, "Clock Version %d.%d\n",
1238 PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version));
1239
1240 cinfo = devm_kzalloc(ph->dev, sizeof(*cinfo), GFP_KERNEL);
1241 if (!cinfo)
1242 return -ENOMEM;
1243
1244 ret = scmi_clock_protocol_attributes_get(ph, cinfo);
1245 if (ret)
1246 return ret;
1247
1248 cinfo->clkds = devm_kcalloc(ph->dev, cinfo->num_clocks,
1249 sizeof(*cinfo->clkds), GFP_KERNEL);
1250 if (!cinfo->clkds)
1251 return -ENOMEM;
1252
1253 for (clkid = 0; clkid < cinfo->num_clocks; clkid++) {
1254 cinfo->clkds[clkid].id = clkid;
1255 ret = scmi_clock_attributes_get(ph, clkid, cinfo);
1256 if (!ret)
1257 scmi_clock_describe_rates_get(ph, clkid, cinfo);
1258 }
1259
1260 if (PROTOCOL_REV_MAJOR(ph->version) >= 0x3) {
1261 cinfo->clock_config_set = scmi_clock_config_set_v2;
1262 cinfo->clock_config_get = scmi_clock_config_get_v2;
1263 } else {
1264 cinfo->clock_config_set = scmi_clock_config_set;
1265 cinfo->clock_config_get = scmi_clock_config_get;
1266 }
1267
1268 return ph->set_priv(ph, cinfo);
1269 }
1270
1271 static const struct scmi_protocol scmi_clock = {
1272 .id = SCMI_PROTOCOL_CLOCK,
1273 .owner = THIS_MODULE,
1274 .instance_init = &scmi_clock_protocol_init,
1275 .ops = &clk_proto_ops,
1276 .events = &clk_protocol_events,
1277 .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
1278 };
1279
1280 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(clock, scmi_clock)
1281