Merge https://source.denx.de/u-boot/custodians/u-boot-usb
[platform/kernel/u-boot.git] / drivers / clk / clk_scmi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019-2022 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 #include <linux/clk-provider.h>
15
16 /**
17  * struct scmi_clk_priv - Private data for SCMI clocks
18  * @channel: Reference to the SCMI channel to use
19  */
20 struct scmi_clk_priv {
21         struct scmi_channel *channel;
22 };
23
24 static int scmi_clk_get_num_clock(struct udevice *dev, size_t *num_clocks)
25 {
26         struct scmi_clk_priv *priv = dev_get_priv(dev);
27         struct scmi_clk_protocol_attr_out out;
28         struct scmi_msg msg = {
29                 .protocol_id = SCMI_PROTOCOL_ID_CLOCK,
30                 .message_id = SCMI_PROTOCOL_ATTRIBUTES,
31                 .out_msg = (u8 *)&out,
32                 .out_msg_sz = sizeof(out),
33         };
34         int ret;
35
36         ret = devm_scmi_process_msg(dev, priv->channel, &msg);
37         if (ret)
38                 return ret;
39
40         *num_clocks = out.attributes & SCMI_CLK_PROTO_ATTR_COUNT_MASK;
41
42         return 0;
43 }
44
45 static int scmi_clk_get_attibute(struct udevice *dev, int clkid, char **name)
46 {
47         struct scmi_clk_priv *priv = dev_get_priv(dev);
48         struct scmi_clk_attribute_in in = {
49                 .clock_id = clkid,
50         };
51         struct scmi_clk_attribute_out out;
52         struct scmi_msg msg = {
53                 .protocol_id = SCMI_PROTOCOL_ID_CLOCK,
54                 .message_id = SCMI_CLOCK_ATTRIBUTES,
55                 .in_msg = (u8 *)&in,
56                 .in_msg_sz = sizeof(in),
57                 .out_msg = (u8 *)&out,
58                 .out_msg_sz = sizeof(out),
59         };
60         int ret;
61
62         ret = devm_scmi_process_msg(dev, priv->channel, &msg);
63         if (ret)
64                 return ret;
65
66         *name = strdup(out.clock_name);
67
68         return 0;
69 }
70
71 static int scmi_clk_gate(struct clk *clk, int enable)
72 {
73         struct scmi_clk_priv *priv = dev_get_priv(clk->dev);
74         struct scmi_clk_state_in in = {
75                 .clock_id = clk->id,
76                 .attributes = enable,
77         };
78         struct scmi_clk_state_out out;
79         struct scmi_msg msg = SCMI_MSG_IN(SCMI_PROTOCOL_ID_CLOCK,
80                                           SCMI_CLOCK_CONFIG_SET,
81                                           in, out);
82         int ret;
83
84         ret = devm_scmi_process_msg(clk->dev, priv->channel, &msg);
85         if (ret)
86                 return ret;
87
88         return scmi_to_linux_errno(out.status);
89 }
90
91 static int scmi_clk_enable(struct clk *clk)
92 {
93         return scmi_clk_gate(clk, 1);
94 }
95
96 static int scmi_clk_disable(struct clk *clk)
97 {
98         return scmi_clk_gate(clk, 0);
99 }
100
101 static ulong scmi_clk_get_rate(struct clk *clk)
102 {
103         struct scmi_clk_priv *priv = dev_get_priv(clk->dev);
104         struct scmi_clk_rate_get_in in = {
105                 .clock_id = clk->id,
106         };
107         struct scmi_clk_rate_get_out out;
108         struct scmi_msg msg = SCMI_MSG_IN(SCMI_PROTOCOL_ID_CLOCK,
109                                           SCMI_CLOCK_RATE_GET,
110                                           in, out);
111         int ret;
112
113         ret = devm_scmi_process_msg(clk->dev, priv->channel, &msg);
114         if (ret < 0)
115                 return ret;
116
117         ret = scmi_to_linux_errno(out.status);
118         if (ret < 0)
119                 return ret;
120
121         return (ulong)(((u64)out.rate_msb << 32) | out.rate_lsb);
122 }
123
124 static ulong scmi_clk_set_rate(struct clk *clk, ulong rate)
125 {
126         struct scmi_clk_priv *priv = dev_get_priv(clk->dev);
127         struct scmi_clk_rate_set_in in = {
128                 .clock_id = clk->id,
129                 .flags = SCMI_CLK_RATE_ROUND_CLOSEST,
130                 .rate_lsb = (u32)rate,
131                 .rate_msb = (u32)((u64)rate >> 32),
132         };
133         struct scmi_clk_rate_set_out out;
134         struct scmi_msg msg = SCMI_MSG_IN(SCMI_PROTOCOL_ID_CLOCK,
135                                           SCMI_CLOCK_RATE_SET,
136                                           in, out);
137         int ret;
138
139         ret = devm_scmi_process_msg(clk->dev, priv->channel, &msg);
140         if (ret < 0)
141                 return ret;
142
143         ret = scmi_to_linux_errno(out.status);
144         if (ret < 0)
145                 return ret;
146
147         return scmi_clk_get_rate(clk);
148 }
149
150 static int scmi_clk_probe(struct udevice *dev)
151 {
152         struct scmi_clk_priv *priv = dev_get_priv(dev);
153         struct clk *clk;
154         size_t num_clocks, i;
155         int ret;
156
157         ret = devm_scmi_of_get_channel(dev, &priv->channel);
158         if (ret)
159                 return ret;
160
161         if (!CONFIG_IS_ENABLED(CLK_CCF))
162                 return 0;
163
164         /* register CCF children: CLK UCLASS, no probed again */
165         if (device_get_uclass_id(dev->parent) == UCLASS_CLK)
166                 return 0;
167
168         ret = scmi_clk_get_num_clock(dev, &num_clocks);
169         if (ret)
170                 return ret;
171
172         for (i = 0; i < num_clocks; i++) {
173                 char *clock_name;
174
175                 if (!scmi_clk_get_attibute(dev, i, &clock_name)) {
176                         clk = kzalloc(sizeof(*clk), GFP_KERNEL);
177                         if (!clk || !clock_name)
178                                 ret = -ENOMEM;
179                         else
180                                 ret = clk_register(clk, dev->driver->name,
181                                                    clock_name, dev->name);
182
183                         if (ret) {
184                                 free(clk);
185                                 free(clock_name);
186                                 return ret;
187                         }
188
189                         clk_dm(i, clk);
190                 }
191         }
192
193         return 0;
194 }
195
196 static const struct clk_ops scmi_clk_ops = {
197         .enable = scmi_clk_enable,
198         .disable = scmi_clk_disable,
199         .get_rate = scmi_clk_get_rate,
200         .set_rate = scmi_clk_set_rate,
201 };
202
203 U_BOOT_DRIVER(scmi_clock) = {
204         .name = "scmi_clk",
205         .id = UCLASS_CLK,
206         .ops = &scmi_clk_ops,
207         .probe = scmi_clk_probe,
208         .priv_auto = sizeof(struct scmi_clk_priv *),
209 };