Prepare v2022.04-rc5
[platform/kernel/u-boot.git] / drivers / clk / clk_scmi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019-2020 Linaro Limited
4  */
5
6 #define LOG_CATEGORY UCLASS_CLK
7
8 #include <common.h>
9 #include <clk-uclass.h>
10 #include <dm.h>
11 #include <scmi_agent.h>
12 #include <scmi_protocols.h>
13 #include <asm/types.h>
14
15 static int scmi_clk_gate(struct clk *clk, int enable)
16 {
17         struct scmi_clk_state_in in = {
18                 .clock_id = clk->id,
19                 .attributes = enable,
20         };
21         struct scmi_clk_state_out out;
22         struct scmi_msg msg = SCMI_MSG_IN(SCMI_PROTOCOL_ID_CLOCK,
23                                           SCMI_CLOCK_CONFIG_SET,
24                                           in, out);
25         int ret;
26
27         ret = devm_scmi_process_msg(clk->dev->parent, &msg);
28         if (ret)
29                 return ret;
30
31         return scmi_to_linux_errno(out.status);
32 }
33
34 static int scmi_clk_enable(struct clk *clk)
35 {
36         return scmi_clk_gate(clk, 1);
37 }
38
39 static int scmi_clk_disable(struct clk *clk)
40 {
41         return scmi_clk_gate(clk, 0);
42 }
43
44 static ulong scmi_clk_get_rate(struct clk *clk)
45 {
46         struct scmi_clk_rate_get_in in = {
47                 .clock_id = clk->id,
48         };
49         struct scmi_clk_rate_get_out out;
50         struct scmi_msg msg = SCMI_MSG_IN(SCMI_PROTOCOL_ID_CLOCK,
51                                           SCMI_CLOCK_RATE_GET,
52                                           in, out);
53         int ret;
54
55         ret = devm_scmi_process_msg(clk->dev->parent, &msg);
56         if (ret < 0)
57                 return ret;
58
59         ret = scmi_to_linux_errno(out.status);
60         if (ret < 0)
61                 return ret;
62
63         return (ulong)(((u64)out.rate_msb << 32) | out.rate_lsb);
64 }
65
66 static ulong scmi_clk_set_rate(struct clk *clk, ulong rate)
67 {
68         struct scmi_clk_rate_set_in in = {
69                 .clock_id = clk->id,
70                 .flags = SCMI_CLK_RATE_ROUND_CLOSEST,
71                 .rate_lsb = (u32)rate,
72                 .rate_msb = (u32)((u64)rate >> 32),
73         };
74         struct scmi_clk_rate_set_out out;
75         struct scmi_msg msg = SCMI_MSG_IN(SCMI_PROTOCOL_ID_CLOCK,
76                                           SCMI_CLOCK_RATE_SET,
77                                           in, out);
78         int ret;
79
80         ret = devm_scmi_process_msg(clk->dev->parent, &msg);
81         if (ret < 0)
82                 return ret;
83
84         ret = scmi_to_linux_errno(out.status);
85         if (ret < 0)
86                 return ret;
87
88         return scmi_clk_get_rate(clk);
89 }
90
91 static const struct clk_ops scmi_clk_ops = {
92         .enable = scmi_clk_enable,
93         .disable = scmi_clk_disable,
94         .get_rate = scmi_clk_get_rate,
95         .set_rate = scmi_clk_set_rate,
96 };
97
98 U_BOOT_DRIVER(scmi_clock) = {
99         .name = "scmi_clk",
100         .id = UCLASS_CLK,
101         .ops = &scmi_clk_ops,
102 };