Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Linaro Limited. |
| 4 | */ |
| 5 | |
Patrick Delaunay | 5dd84d4 | 2021-02-24 11:19:44 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SCMI_AGENT |
| 7 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 10 | #include <scmi_agent.h> |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 11 | #include <scmi_agent-uclass.h> |
| 12 | #include <scmi_protocols.h> |
Patrick Delaunay | d4c9f68 | 2021-02-24 11:19:45 +0100 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
| 15 | #include <linux/compat.h> |
| 16 | |
| 17 | /** |
| 18 | * struct error_code - Helper structure for SCMI error code conversion |
| 19 | * @scmi: SCMI error code |
| 20 | * @errno: Related standard error number |
| 21 | */ |
| 22 | struct error_code { |
| 23 | int scmi; |
| 24 | int errno; |
| 25 | }; |
| 26 | |
| 27 | static const struct error_code scmi_linux_errmap[] = { |
| 28 | { .scmi = SCMI_NOT_SUPPORTED, .errno = -EOPNOTSUPP, }, |
| 29 | { .scmi = SCMI_INVALID_PARAMETERS, .errno = -EINVAL, }, |
| 30 | { .scmi = SCMI_DENIED, .errno = -EACCES, }, |
| 31 | { .scmi = SCMI_NOT_FOUND, .errno = -ENOENT, }, |
| 32 | { .scmi = SCMI_OUT_OF_RANGE, .errno = -ERANGE, }, |
| 33 | { .scmi = SCMI_BUSY, .errno = -EBUSY, }, |
| 34 | { .scmi = SCMI_COMMS_ERROR, .errno = -ECOMM, }, |
| 35 | { .scmi = SCMI_GENERIC_ERROR, .errno = -EIO, }, |
| 36 | { .scmi = SCMI_HARDWARE_ERROR, .errno = -EREMOTEIO, }, |
| 37 | { .scmi = SCMI_PROTOCOL_ERROR, .errno = -EPROTO, }, |
| 38 | }; |
| 39 | |
AKASHI Takahiro | fa74400 | 2023-10-11 19:07:07 +0900 | [diff] [blame] | 40 | /** |
| 41 | * scmi_protocol_is_supported - check availability of protocol |
| 42 | * @dev: SCMI agent device |
| 43 | * @proto_id: Identifier of protocol |
| 44 | * |
| 45 | * check if the protocol, @proto_id, is provided by the SCMI agent, |
| 46 | * @dev. |
| 47 | * |
| 48 | * Return: 0 on success, error code otherwise |
| 49 | */ |
| 50 | static bool scmi_protocol_is_supported(struct udevice *dev, |
| 51 | enum scmi_std_protocol proto_id) |
| 52 | { |
| 53 | struct scmi_agent_priv *priv; |
| 54 | int i; |
| 55 | |
| 56 | if (proto_id == SCMI_PROTOCOL_ID_BASE) |
| 57 | return true; |
| 58 | |
| 59 | priv = dev_get_uclass_plat(dev); |
| 60 | if (!priv) { |
| 61 | dev_err(dev, "No priv data found\n"); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | for (i = 0; i < priv->num_protocols; i++) |
| 66 | if (priv->protocols[i] == proto_id) |
| 67 | return true; |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 72 | struct udevice *scmi_get_protocol(struct udevice *dev, |
| 73 | enum scmi_std_protocol id) |
| 74 | { |
| 75 | struct scmi_agent_priv *priv; |
| 76 | struct udevice *proto; |
| 77 | |
| 78 | priv = dev_get_uclass_plat(dev); |
| 79 | if (!priv) { |
| 80 | dev_err(dev, "No priv data found\n"); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | switch (id) { |
AKASHI Takahiro | 410be2c | 2023-10-11 19:07:04 +0900 | [diff] [blame] | 85 | case SCMI_PROTOCOL_ID_BASE: |
| 86 | proto = priv->base_dev; |
| 87 | break; |
AKASHI Takahiro | df234cf | 2023-10-16 14:39:44 +0900 | [diff] [blame] | 88 | case SCMI_PROTOCOL_ID_POWER_DOMAIN: |
| 89 | proto = priv->pwdom_dev; |
| 90 | break; |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 91 | case SCMI_PROTOCOL_ID_CLOCK: |
| 92 | proto = priv->clock_dev; |
| 93 | break; |
| 94 | case SCMI_PROTOCOL_ID_RESET_DOMAIN: |
| 95 | proto = priv->resetdom_dev; |
| 96 | break; |
| 97 | case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN: |
| 98 | proto = priv->voltagedom_dev; |
| 99 | break; |
| 100 | default: |
| 101 | dev_err(dev, "Protocol not supported\n"); |
| 102 | proto = NULL; |
| 103 | break; |
| 104 | } |
| 105 | if (proto && device_probe(proto)) |
| 106 | dev_err(dev, "Probe failed\n"); |
| 107 | |
| 108 | return proto; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * scmi_add_protocol - add protocol to agent |
| 113 | * @dev: SCMI agent device |
| 114 | * @proto_id: SCMI protocol ID |
| 115 | * @proto: SCMI protocol device |
| 116 | * |
| 117 | * Associate the protocol instance, @proto, to the agent, @dev, |
| 118 | * for later use. |
| 119 | * |
| 120 | * Return: 0 on success, error code on failure |
| 121 | */ |
| 122 | static int scmi_add_protocol(struct udevice *dev, |
| 123 | enum scmi_std_protocol proto_id, |
| 124 | struct udevice *proto) |
| 125 | { |
| 126 | struct scmi_agent_priv *priv; |
| 127 | |
| 128 | priv = dev_get_uclass_plat(dev); |
| 129 | if (!priv) { |
| 130 | dev_err(dev, "No priv data found\n"); |
| 131 | return -ENODEV; |
| 132 | } |
| 133 | |
| 134 | switch (proto_id) { |
AKASHI Takahiro | 410be2c | 2023-10-11 19:07:04 +0900 | [diff] [blame] | 135 | case SCMI_PROTOCOL_ID_BASE: |
| 136 | priv->base_dev = proto; |
| 137 | break; |
AKASHI Takahiro | df234cf | 2023-10-16 14:39:44 +0900 | [diff] [blame] | 138 | case SCMI_PROTOCOL_ID_POWER_DOMAIN: |
| 139 | priv->pwdom_dev = proto; |
| 140 | break; |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 141 | case SCMI_PROTOCOL_ID_CLOCK: |
| 142 | priv->clock_dev = proto; |
| 143 | break; |
| 144 | case SCMI_PROTOCOL_ID_RESET_DOMAIN: |
| 145 | priv->resetdom_dev = proto; |
| 146 | break; |
| 147 | case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN: |
| 148 | priv->voltagedom_dev = proto; |
| 149 | break; |
| 150 | default: |
| 151 | dev_err(dev, "Protocol not supported\n"); |
| 152 | return -EPROTO; |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 158 | int scmi_to_linux_errno(s32 scmi_code) |
| 159 | { |
| 160 | int n; |
| 161 | |
| 162 | if (!scmi_code) |
| 163 | return 0; |
| 164 | |
| 165 | for (n = 0; n < ARRAY_SIZE(scmi_linux_errmap); n++) |
| 166 | if (scmi_code == scmi_linux_errmap[n].scmi) |
AKASHI Takahiro | c937154 | 2023-06-13 10:30:45 +0900 | [diff] [blame] | 167 | return scmi_linux_errmap[n].errno; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 168 | |
| 169 | return -EPROTO; |
| 170 | } |
| 171 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 172 | static struct udevice *find_scmi_protocol_device(struct udevice *dev) |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 173 | { |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 174 | struct udevice *parent = NULL, *protocol; |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 175 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 176 | for (protocol = dev; protocol; protocol = parent) { |
| 177 | parent = dev_get_parent(protocol); |
| 178 | if (!parent || |
| 179 | device_get_uclass_id(parent) == UCLASS_SCMI_AGENT) |
| 180 | break; |
| 181 | } |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 182 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 183 | if (!parent) { |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 184 | dev_err(dev, "Invalid SCMI device, agent not found\n"); |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 185 | return NULL; |
| 186 | } |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 187 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 188 | return protocol; |
Etienne Carriere | 2ee6458 | 2022-05-31 18:09:20 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 191 | static const struct scmi_agent_ops *transport_dev_ops(struct udevice *dev) |
| 192 | { |
| 193 | return (const struct scmi_agent_ops *)dev->driver->ops; |
| 194 | } |
| 195 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 196 | /** |
| 197 | * scmi_of_get_channel() - Get SCMI channel handle |
| 198 | * |
| 199 | * @dev: SCMI agent device |
| 200 | * @channel: Output reference to the SCMI channel upon success |
| 201 | * |
| 202 | * On return, @channel will be set. |
| 203 | * Return 0 on success and a negative errno on failure |
| 204 | */ |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 205 | static int scmi_of_get_channel(struct udevice *dev, struct udevice *protocol, |
| 206 | struct scmi_channel **channel) |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 207 | { |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 208 | const struct scmi_agent_ops *ops; |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 209 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 210 | ops = transport_dev_ops(dev); |
| 211 | if (ops->of_get_channel) |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 212 | return ops->of_get_channel(dev, protocol, channel); |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 213 | else |
| 214 | return -EPROTONOSUPPORT; |
| 215 | } |
| 216 | |
| 217 | int devm_scmi_of_get_channel(struct udevice *dev) |
| 218 | { |
| 219 | struct udevice *protocol; |
| 220 | struct scmi_agent_proto_priv *priv; |
| 221 | int ret; |
| 222 | |
| 223 | protocol = find_scmi_protocol_device(dev); |
| 224 | if (!protocol) |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 225 | return -ENODEV; |
| 226 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 227 | priv = dev_get_parent_priv(protocol); |
AKASHI Takahiro | 589ec9a | 2023-10-11 19:06:55 +0900 | [diff] [blame] | 228 | ret = scmi_of_get_channel(protocol->parent, protocol, &priv->channel); |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 229 | if (ret == -EPROTONOSUPPORT) { |
| 230 | /* Drivers without a get_channel operator don't need a channel ref */ |
| 231 | priv->channel = NULL; |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 232 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 233 | return 0; |
| 234 | } |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 235 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 236 | return ret; |
Etienne Carriere | 187cea1 | 2022-05-31 18:09:21 +0200 | [diff] [blame] | 237 | } |
| 238 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 239 | /** |
| 240 | * scmi_process_msg() - Send and process an SCMI message |
| 241 | * |
| 242 | * Send a message to an SCMI server. |
| 243 | * Caller sets scmi_msg::out_msg_sz to the output message buffer size. |
| 244 | * |
| 245 | * @dev: SCMI agent device |
| 246 | * @channel: Communication channel for the device |
| 247 | * @msg: Message structure reference |
| 248 | * |
| 249 | * On return, scmi_msg::out_msg_sz stores the response payload size. |
| 250 | * Return: 0 on success and a negative errno on failure |
| 251 | */ |
| 252 | static int scmi_process_msg(struct udevice *dev, struct scmi_channel *channel, |
| 253 | struct scmi_msg *msg) |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 254 | { |
Etienne Carriere | 2f26c04 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 255 | const struct scmi_agent_ops *ops; |
Etienne Carriere | 2f26c04 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 256 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 257 | ops = transport_dev_ops(dev); |
| 258 | if (ops->process_msg) |
| 259 | return ops->process_msg(dev, channel, msg); |
| 260 | else |
| 261 | return -EPROTONOSUPPORT; |
| 262 | } |
Etienne Carriere | 2f26c04 | 2022-02-21 09:22:40 +0100 | [diff] [blame] | 263 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 264 | int devm_scmi_process_msg(struct udevice *dev, struct scmi_msg *msg) |
| 265 | { |
| 266 | struct udevice *protocol; |
| 267 | struct scmi_agent_proto_priv *priv; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 268 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 269 | protocol = find_scmi_protocol_device(dev); |
| 270 | if (!protocol) |
| 271 | return -ENODEV; |
| 272 | |
| 273 | priv = dev_get_parent_priv(protocol); |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 274 | |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 275 | return scmi_process_msg(protocol->parent, priv->channel, msg); |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 276 | } |
| 277 | |
AKASHI Takahiro | 410be2c | 2023-10-11 19:07:04 +0900 | [diff] [blame] | 278 | /** |
| 279 | * scmi_fill_base_info - get base information about SCMI server |
| 280 | * @agent: SCMI agent device |
| 281 | * @dev: SCMI protocol device |
| 282 | * |
| 283 | * By using Base protocol commands, collect the base information |
| 284 | * about SCMI server. |
| 285 | * |
| 286 | * Return: 0 on success, error code on failure |
| 287 | */ |
| 288 | static int scmi_fill_base_info(struct udevice *agent, struct udevice *dev) |
| 289 | { |
| 290 | struct scmi_agent_priv *priv = dev_get_uclass_plat(agent); |
| 291 | int ret; |
| 292 | |
| 293 | ret = scmi_base_protocol_version(dev, &priv->version); |
| 294 | if (ret) { |
| 295 | dev_err(dev, "protocol_version() failed (%d)\n", ret); |
| 296 | return ret; |
| 297 | } |
| 298 | /* check for required version */ |
| 299 | if (priv->version < SCMI_BASE_PROTOCOL_VERSION) { |
| 300 | dev_err(dev, "base protocol version (%d) lower than expected\n", |
| 301 | priv->version); |
| 302 | return -EPROTO; |
| 303 | } |
| 304 | |
| 305 | ret = scmi_base_protocol_attrs(dev, &priv->num_agents, |
| 306 | &priv->num_protocols); |
| 307 | if (ret) { |
| 308 | dev_err(dev, "protocol_attrs() failed (%d)\n", ret); |
| 309 | return ret; |
| 310 | } |
| 311 | ret = scmi_base_discover_vendor(dev, &priv->vendor); |
| 312 | if (ret) { |
| 313 | dev_err(dev, "base_discover_vendor() failed (%d)\n", ret); |
| 314 | return ret; |
| 315 | } |
| 316 | ret = scmi_base_discover_sub_vendor(dev, &priv->sub_vendor); |
| 317 | if (ret) { |
| 318 | if (ret != -EOPNOTSUPP) { |
| 319 | dev_err(dev, "base_discover_sub_vendor() failed (%d)\n", |
| 320 | ret); |
| 321 | return ret; |
| 322 | } |
| 323 | priv->sub_vendor = "NA"; |
| 324 | } |
| 325 | ret = scmi_base_discover_impl_version(dev, &priv->impl_version); |
| 326 | if (ret) { |
| 327 | dev_err(dev, "base_discover_impl_version() failed (%d)\n", |
| 328 | ret); |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | ret = scmi_base_discover_agent(dev, 0xffffffff, |
| 333 | &priv->agent_id, &priv->agent_name); |
| 334 | if (ret) { |
| 335 | if (ret != -EOPNOTSUPP) { |
| 336 | dev_err(dev, |
| 337 | "base_discover_agent() failed for myself (%d)\n", |
| 338 | ret); |
| 339 | return ret; |
| 340 | } |
| 341 | priv->agent_id = 0xffffffff; |
| 342 | priv->agent_name = "NA"; |
| 343 | } |
| 344 | |
| 345 | ret = scmi_base_discover_list_protocols(dev, &priv->protocols); |
| 346 | if (ret != priv->num_protocols) { |
| 347 | dev_err(dev, "base_discover_list_protocols() failed (%d)\n", |
| 348 | ret); |
| 349 | return -EPROTO; |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 355 | /* |
| 356 | * SCMI agent devices binds devices of various uclasses depending on |
| 357 | * the FDT description. scmi_bind_protocol() is a generic bind sequence |
| 358 | * called by the uclass at bind stage, that is uclass post_bind. |
| 359 | */ |
| 360 | static int scmi_bind_protocols(struct udevice *dev) |
| 361 | { |
| 362 | int ret = 0; |
| 363 | ofnode node; |
| 364 | const char *name; |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 365 | struct driver *drv; |
AKASHI Takahiro | 410be2c | 2023-10-11 19:07:04 +0900 | [diff] [blame] | 366 | struct udevice *agent, *proto; |
| 367 | |
| 368 | if (!uclass_get_device(UCLASS_SCMI_AGENT, 1, &agent)) { |
| 369 | /* This is a second SCMI agent */ |
| 370 | dev_err(dev, "Cannot have more than one SCMI agent\n"); |
| 371 | return -EEXIST; |
| 372 | } |
| 373 | |
| 374 | /* initialize the device from device tree */ |
| 375 | drv = DM_DRIVER_GET(scmi_base_drv); |
| 376 | name = "scmi-base.0"; |
| 377 | ret = device_bind(dev, drv, name, NULL, ofnode_null(), &proto); |
| 378 | if (ret) { |
| 379 | dev_err(dev, "failed to bind base protocol\n"); |
| 380 | return ret; |
| 381 | } |
| 382 | ret = scmi_add_protocol(dev, SCMI_PROTOCOL_ID_BASE, proto); |
| 383 | if (ret) { |
| 384 | dev_err(dev, "failed to add protocol: %s, ret: %d\n", |
| 385 | proto->name, ret); |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | ret = device_probe(proto); |
| 390 | if (ret) { |
| 391 | dev_err(dev, "failed to probe base protocol\n"); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | ret = scmi_fill_base_info(dev, proto); |
| 396 | if (ret) { |
| 397 | dev_err(dev, "failed to get base information\n"); |
| 398 | return ret; |
| 399 | } |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 400 | |
| 401 | dev_for_each_subnode(node, dev) { |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 402 | u32 protocol_id; |
| 403 | |
| 404 | if (!ofnode_is_enabled(node)) |
| 405 | continue; |
| 406 | |
| 407 | if (ofnode_read_u32(node, "reg", &protocol_id)) |
| 408 | continue; |
| 409 | |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 410 | drv = NULL; |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 411 | name = ofnode_get_name(node); |
| 412 | switch (protocol_id) { |
AKASHI Takahiro | df234cf | 2023-10-16 14:39:44 +0900 | [diff] [blame] | 413 | case SCMI_PROTOCOL_ID_POWER_DOMAIN: |
| 414 | if (CONFIG_IS_ENABLED(SCMI_POWER_DOMAIN) && |
| 415 | scmi_protocol_is_supported(dev, protocol_id)) |
| 416 | drv = DM_DRIVER_GET(scmi_power_domain); |
| 417 | break; |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 418 | case SCMI_PROTOCOL_ID_CLOCK: |
AKASHI Takahiro | fa74400 | 2023-10-11 19:07:07 +0900 | [diff] [blame] | 419 | if (CONFIG_IS_ENABLED(CLK_SCMI) && |
| 420 | scmi_protocol_is_supported(dev, protocol_id)) |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 421 | drv = DM_DRIVER_GET(scmi_clock); |
| 422 | break; |
| 423 | case SCMI_PROTOCOL_ID_RESET_DOMAIN: |
AKASHI Takahiro | fa74400 | 2023-10-11 19:07:07 +0900 | [diff] [blame] | 424 | if (IS_ENABLED(CONFIG_RESET_SCMI) && |
| 425 | scmi_protocol_is_supported(dev, protocol_id)) |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 426 | drv = DM_DRIVER_GET(scmi_reset_domain); |
| 427 | break; |
| 428 | case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN: |
AKASHI Takahiro | fa74400 | 2023-10-11 19:07:07 +0900 | [diff] [blame] | 429 | if (IS_ENABLED(CONFIG_DM_REGULATOR_SCMI) && |
| 430 | scmi_protocol_is_supported(dev, protocol_id)) { |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 431 | node = ofnode_find_subnode(node, "regulators"); |
| 432 | if (!ofnode_valid(node)) { |
| 433 | dev_err(dev, "no regulators node\n"); |
| 434 | return -ENXIO; |
| 435 | } |
| 436 | drv = DM_DRIVER_GET(scmi_voltage_domain); |
| 437 | } |
| 438 | break; |
| 439 | default: |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | if (!drv) { |
| 444 | dev_dbg(dev, "Ignore unsupported SCMI protocol %#x\n", |
| 445 | protocol_id); |
| 446 | continue; |
| 447 | } |
| 448 | |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 449 | ret = device_bind(dev, drv, name, NULL, node, &proto); |
| 450 | if (ret) { |
| 451 | dev_err(dev, "failed to bind %s protocol\n", drv->name); |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 452 | break; |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 453 | } |
| 454 | ret = scmi_add_protocol(dev, protocol_id, proto); |
| 455 | if (ret) { |
| 456 | dev_err(dev, "failed to add protocol: %s, ret: %d\n", |
| 457 | proto->name, ret); |
| 458 | break; |
| 459 | } |
AKASHI Takahiro | 6d37915 | 2023-10-11 19:06:57 +0900 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 465 | UCLASS_DRIVER(scmi_agent) = { |
| 466 | .id = UCLASS_SCMI_AGENT, |
| 467 | .name = "scmi_agent", |
| 468 | .post_bind = scmi_bind_protocols, |
AKASHI Takahiro | 6c90f82 | 2023-10-11 19:06:58 +0900 | [diff] [blame] | 469 | .per_device_plat_auto = sizeof(struct scmi_agent_priv), |
AKASHI Takahiro | 7b3aa37 | 2023-10-11 19:06:54 +0900 | [diff] [blame] | 470 | .per_child_auto = sizeof(struct scmi_agent_proto_priv), |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 471 | }; |