| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* System Control and Management Interface (SCMI) Voltage Protocol |
| 4 |
* |
| 5 |
* Copyright (C) 2020-2022 ARM Ltd. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/scmi_protocol.h> |
| 10 |
|
| 11 |
#include "protocols.h" |
| 12 |
|
| 13 |
/* Updated only after ALL the mandatory features for that version are merged */ |
| 14 |
#define SCMI_PROTOCOL_SUPPORTED_VERSION 0x20001 |
| 15 |
|
| 16 |
#define VOLTAGE_DOMS_NUM_MASK GENMASK(15, 0) |
| 17 |
#define REMAINING_LEVELS_MASK GENMASK(31, 16) |
| 18 |
#define RETURNED_LEVELS_MASK GENMASK(11, 0) |
| 19 |
|
| 20 |
enum scmi_voltage_protocol_cmd { |
| 21 |
VOLTAGE_DOMAIN_ATTRIBUTES = 0x3, |
| 22 |
VOLTAGE_DESCRIBE_LEVELS = 0x4, |
| 23 |
VOLTAGE_CONFIG_SET = 0x5, |
| 24 |
VOLTAGE_CONFIG_GET = 0x6, |
| 25 |
VOLTAGE_LEVEL_SET = 0x7, |
| 26 |
VOLTAGE_LEVEL_GET = 0x8, |
| 27 |
VOLTAGE_DOMAIN_NAME_GET = 0x09, |
| 28 |
}; |
| 29 |
|
| 30 |
#define NUM_VOLTAGE_DOMAINS(x) ((u16)(FIELD_GET(VOLTAGE_DOMS_NUM_MASK, (x)))) |
| 31 |
|
| 32 |
struct scmi_msg_resp_domain_attributes { |
| 33 |
__le32 attr; |
| 34 |
#define SUPPORTS_ASYNC_LEVEL_SET(x) ((x) & BIT(31)) |
| 35 |
#define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(30)) |
| 36 |
u8 name[SCMI_SHORT_NAME_MAX_SIZE]; |
| 37 |
}; |
| 38 |
|
| 39 |
struct scmi_msg_cmd_describe_levels { |
| 40 |
__le32 domain_id; |
| 41 |
__le32 level_index; |
| 42 |
}; |
| 43 |
|
| 44 |
struct scmi_msg_resp_describe_levels { |
| 45 |
__le32 flags; |
| 46 |
#define NUM_REMAINING_LEVELS(f) ((u16)(FIELD_GET(REMAINING_LEVELS_MASK, (f)))) |
| 47 |
#define NUM_RETURNED_LEVELS(f) ((u16)(FIELD_GET(RETURNED_LEVELS_MASK, (f)))) |
| 48 |
#define SUPPORTS_SEGMENTED_LEVELS(f) ((f) & BIT(12)) |
| 49 |
__le32 voltage[]; |
| 50 |
}; |
| 51 |
|
| 52 |
struct scmi_msg_cmd_config_set { |
| 53 |
__le32 domain_id; |
| 54 |
__le32 config; |
| 55 |
}; |
| 56 |
|
| 57 |
struct scmi_msg_cmd_level_set { |
| 58 |
__le32 domain_id; |
| 59 |
__le32 flags; |
| 60 |
__le32 voltage_level; |
| 61 |
}; |
| 62 |
|
| 63 |
struct scmi_resp_voltage_level_set_complete { |
| 64 |
__le32 domain_id; |
| 65 |
__le32 voltage_level; |
| 66 |
}; |
| 67 |
|
| 68 |
struct voltage_info { |
| 69 |
unsigned int num_domains; |
| 70 |
struct scmi_voltage_info *domains; |
| 71 |
}; |
| 72 |
|
| 73 |
static int scmi_protocol_attributes_get(const struct scmi_protocol_handle *ph, |
| 74 |
struct voltage_info *vinfo) |
| 75 |
{ |
| 76 |
int ret; |
| 77 |
struct scmi_xfer *t; |
| 78 |
|
| 79 |
ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0, |
| 80 |
sizeof(__le32), &t); |
| 81 |
if (ret) |
| 82 |
return ret; |
| 83 |
|
| 84 |
ret = ph->xops->do_xfer(ph, t); |
| 85 |
if (!ret) |
| 86 |
vinfo->num_domains = |
| 87 |
NUM_VOLTAGE_DOMAINS(get_unaligned_le32(t->rx.buf)); |
| 88 |
|
| 89 |
ph->xops->xfer_put(ph, t); |
| 90 |
return ret; |
| 91 |
} |
| 92 |
|
| 93 |
static int scmi_init_voltage_levels(struct device *dev, |
| 94 |
struct scmi_voltage_info *v, |
| 95 |
u32 num_returned, u32 num_remaining, |
| 96 |
bool segmented) |
| 97 |
{ |
| 98 |
u32 num_levels; |
| 99 |
|
| 100 |
num_levels = num_returned + num_remaining; |
| 101 |
/* |
| 102 |
* segmented levels entries are represented by a single triplet |
| 103 |
* returned all in one go. |
| 104 |
*/ |
| 105 |
if (!num_levels || |
| 106 |
(segmented && (num_remaining || num_returned != 3))) { |
| 107 |
dev_err(dev, |
| 108 |
"Invalid level descriptor(%d/%d/%d) for voltage dom %d\n", |
| 109 |
num_levels, num_returned, num_remaining, v->id); |
| 110 |
return -EINVAL; |
| 111 |
} |
| 112 |
|
| 113 |
v->levels_uv = devm_kcalloc(dev, num_levels, sizeof(u32), GFP_KERNEL); |
| 114 |
if (!v->levels_uv) |
| 115 |
return -ENOMEM; |
| 116 |
|
| 117 |
v->num_levels = num_levels; |
| 118 |
v->segmented = segmented; |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
struct scmi_volt_ipriv { |
| 124 |
struct device *dev; |
| 125 |
struct scmi_voltage_info *v; |
| 126 |
}; |
| 127 |
|
| 128 |
static void iter_volt_levels_prepare_message(void *message, |
| 129 |
unsigned int desc_index, |
| 130 |
const void *priv) |
| 131 |
{ |
| 132 |
struct scmi_msg_cmd_describe_levels *msg = message; |
| 133 |
const struct scmi_volt_ipriv *p = priv; |
| 134 |
|
| 135 |
msg->domain_id = cpu_to_le32(p->v->id); |
| 136 |
msg->level_index = cpu_to_le32(desc_index); |
| 137 |
} |
| 138 |
|
| 139 |
static int iter_volt_levels_update_state(struct scmi_iterator_state *st, |
| 140 |
const void *response, void *priv) |
| 141 |
{ |
| 142 |
int ret = 0; |
| 143 |
u32 flags; |
| 144 |
const struct scmi_msg_resp_describe_levels *r = response; |
| 145 |
struct scmi_volt_ipriv *p = priv; |
| 146 |
|
| 147 |
flags = le32_to_cpu(r->flags); |
| 148 |
st->num_returned = NUM_RETURNED_LEVELS(flags); |
| 149 |
st->num_remaining = NUM_REMAINING_LEVELS(flags); |
| 150 |
|
| 151 |
/* Allocate space for num_levels if not already done */ |
| 152 |
if (!p->v->num_levels) { |
| 153 |
ret = scmi_init_voltage_levels(p->dev, p->v, st->num_returned, |
| 154 |
st->num_remaining, |
| 155 |
SUPPORTS_SEGMENTED_LEVELS(flags)); |
| 156 |
if (!ret) |
| 157 |
st->max_resources = p->v->num_levels; |
| 158 |
} |
| 159 |
|
| 160 |
return ret; |
| 161 |
} |
| 162 |
|
| 163 |
static int |
| 164 |
iter_volt_levels_process_response(const struct scmi_protocol_handle *ph, |
| 165 |
const void *response, |
| 166 |
struct scmi_iterator_state *st, void *priv) |
| 167 |
{ |
| 168 |
s32 val; |
| 169 |
const struct scmi_msg_resp_describe_levels *r = response; |
| 170 |
struct scmi_volt_ipriv *p = priv; |
| 171 |
|
| 172 |
val = (s32)le32_to_cpu(r->voltage[st->loop_idx]); |
| 173 |
p->v->levels_uv[st->desc_index + st->loop_idx] = val; |
| 174 |
if (val < 0) |
| 175 |
p->v->negative_volts_allowed = true; |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
static int scmi_voltage_levels_get(const struct scmi_protocol_handle *ph, |
| 181 |
struct scmi_voltage_info *v) |
| 182 |
{ |
| 183 |
int ret; |
| 184 |
void *iter; |
| 185 |
struct scmi_iterator_ops ops = { |
| 186 |
.prepare_message = iter_volt_levels_prepare_message, |
| 187 |
.update_state = iter_volt_levels_update_state, |
| 188 |
.process_response = iter_volt_levels_process_response, |
| 189 |
}; |
| 190 |
struct scmi_volt_ipriv vpriv = { |
| 191 |
.dev = ph->dev, |
| 192 |
.v = v, |
| 193 |
}; |
| 194 |
|
| 195 |
iter = ph->hops->iter_response_init(ph, &ops, v->num_levels, |
| 196 |
VOLTAGE_DESCRIBE_LEVELS, |
| 197 |
sizeof(struct scmi_msg_cmd_describe_levels), |
| 198 |
&vpriv); |
| 199 |
if (IS_ERR(iter)) |
| 200 |
return PTR_ERR(iter); |
| 201 |
|
| 202 |
ret = ph->hops->iter_response_run(iter); |
| 203 |
if (ret) { |
| 204 |
v->num_levels = 0; |
| 205 |
devm_kfree(ph->dev, v->levels_uv); |
| 206 |
} |
| 207 |
|
| 208 |
return ret; |
| 209 |
} |
| 210 |
|
| 211 |
static int scmi_voltage_descriptors_get(const struct scmi_protocol_handle *ph, |
| 212 |
struct voltage_info *vinfo) |
| 213 |
{ |
| 214 |
int ret, dom; |
| 215 |
struct scmi_xfer *td; |
| 216 |
struct scmi_msg_resp_domain_attributes *resp_dom; |
| 217 |
|
| 218 |
ret = ph->xops->xfer_get_init(ph, VOLTAGE_DOMAIN_ATTRIBUTES, |
| 219 |
sizeof(__le32), sizeof(*resp_dom), &td); |
| 220 |
if (ret) |
| 221 |
return ret; |
| 222 |
resp_dom = td->rx.buf; |
| 223 |
|
| 224 |
for (dom = 0; dom < vinfo->num_domains; dom++) { |
| 225 |
u32 attributes; |
| 226 |
struct scmi_voltage_info *v; |
| 227 |
|
| 228 |
/* Retrieve domain attributes at first ... */ |
| 229 |
put_unaligned_le32(dom, td->tx.buf); |
| 230 |
/* Skip domain on comms error */ |
| 231 |
if (ph->xops->do_xfer(ph, td)) { |
| 232 |
ph->xops->reset_rx_to_maxsz(ph, td); |
| 233 |
continue; |
| 234 |
} |
| 235 |
|
| 236 |
v = vinfo->domains + dom; |
| 237 |
v->id = dom; |
| 238 |
attributes = le32_to_cpu(resp_dom->attr); |
| 239 |
strscpy(v->name, resp_dom->name, SCMI_SHORT_NAME_MAX_SIZE); |
| 240 |
|
| 241 |
/* |
| 242 |
* If supported overwrite short name with the extended one; |
| 243 |
* on error just carry on and use already provided short name. |
| 244 |
*/ |
| 245 |
if (PROTOCOL_REV_MAJOR(ph->version) >= 0x2) { |
| 246 |
if (SUPPORTS_EXTENDED_NAMES(attributes)) |
| 247 |
ph->hops->extended_name_get(ph, |
| 248 |
VOLTAGE_DOMAIN_NAME_GET, |
| 249 |
v->id, NULL, v->name, |
| 250 |
SCMI_MAX_STR_SIZE); |
| 251 |
if (SUPPORTS_ASYNC_LEVEL_SET(attributes)) |
| 252 |
v->async_level_set = true; |
| 253 |
} |
| 254 |
|
| 255 |
/* Skip invalid voltage descriptors */ |
| 256 |
scmi_voltage_levels_get(ph, v); |
| 257 |
} |
| 258 |
|
| 259 |
ph->xops->xfer_put(ph, td); |
| 260 |
|
| 261 |
return ret; |
| 262 |
} |
| 263 |
|
| 264 |
static int __scmi_voltage_get_u32(const struct scmi_protocol_handle *ph, |
| 265 |
u8 cmd_id, u32 domain_id, u32 *value) |
| 266 |
{ |
| 267 |
int ret; |
| 268 |
struct scmi_xfer *t; |
| 269 |
struct voltage_info *vinfo = ph->get_priv(ph); |
| 270 |
|
| 271 |
if (domain_id >= vinfo->num_domains) |
| 272 |
return -EINVAL; |
| 273 |
|
| 274 |
ret = ph->xops->xfer_get_init(ph, cmd_id, sizeof(__le32), 0, &t); |
| 275 |
if (ret) |
| 276 |
return ret; |
| 277 |
|
| 278 |
put_unaligned_le32(domain_id, t->tx.buf); |
| 279 |
ret = ph->xops->do_xfer(ph, t); |
| 280 |
if (!ret) |
| 281 |
*value = get_unaligned_le32(t->rx.buf); |
| 282 |
|
| 283 |
ph->xops->xfer_put(ph, t); |
| 284 |
return ret; |
| 285 |
} |
| 286 |
|
| 287 |
static int scmi_voltage_config_set(const struct scmi_protocol_handle *ph, |
| 288 |
u32 domain_id, u32 config) |
| 289 |
{ |
| 290 |
int ret; |
| 291 |
struct scmi_xfer *t; |
| 292 |
struct voltage_info *vinfo = ph->get_priv(ph); |
| 293 |
struct scmi_msg_cmd_config_set *cmd; |
| 294 |
|
| 295 |
if (domain_id >= vinfo->num_domains) |
| 296 |
return -EINVAL; |
| 297 |
|
| 298 |
ret = ph->xops->xfer_get_init(ph, VOLTAGE_CONFIG_SET, |
| 299 |
sizeof(*cmd), 0, &t); |
| 300 |
if (ret) |
| 301 |
return ret; |
| 302 |
|
| 303 |
cmd = t->tx.buf; |
| 304 |
cmd->domain_id = cpu_to_le32(domain_id); |
| 305 |
cmd->config = cpu_to_le32(config & GENMASK(3, 0)); |
| 306 |
|
| 307 |
ret = ph->xops->do_xfer(ph, t); |
| 308 |
|
| 309 |
ph->xops->xfer_put(ph, t); |
| 310 |
return ret; |
| 311 |
} |
| 312 |
|
| 313 |
static int scmi_voltage_config_get(const struct scmi_protocol_handle *ph, |
| 314 |
u32 domain_id, u32 *config) |
| 315 |
{ |
| 316 |
return __scmi_voltage_get_u32(ph, VOLTAGE_CONFIG_GET, |
| 317 |
domain_id, config); |
| 318 |
} |
| 319 |
|
| 320 |
static int scmi_voltage_level_set(const struct scmi_protocol_handle *ph, |
| 321 |
u32 domain_id, |
| 322 |
enum scmi_voltage_level_mode mode, |
| 323 |
s32 volt_uV) |
| 324 |
{ |
| 325 |
int ret; |
| 326 |
struct scmi_xfer *t; |
| 327 |
struct voltage_info *vinfo = ph->get_priv(ph); |
| 328 |
struct scmi_msg_cmd_level_set *cmd; |
| 329 |
struct scmi_voltage_info *v; |
| 330 |
|
| 331 |
if (domain_id >= vinfo->num_domains) |
| 332 |
return -EINVAL; |
| 333 |
|
| 334 |
ret = ph->xops->xfer_get_init(ph, VOLTAGE_LEVEL_SET, |
| 335 |
sizeof(*cmd), 0, &t); |
| 336 |
if (ret) |
| 337 |
return ret; |
| 338 |
|
| 339 |
v = vinfo->domains + domain_id; |
| 340 |
|
| 341 |
cmd = t->tx.buf; |
| 342 |
cmd->domain_id = cpu_to_le32(domain_id); |
| 343 |
cmd->voltage_level = cpu_to_le32(volt_uV); |
| 344 |
|
| 345 |
if (!v->async_level_set || mode != SCMI_VOLTAGE_LEVEL_SET_AUTO) { |
| 346 |
cmd->flags = cpu_to_le32(0x0); |
| 347 |
ret = ph->xops->do_xfer(ph, t); |
| 348 |
} else { |
| 349 |
cmd->flags = cpu_to_le32(0x1); |
| 350 |
ret = ph->xops->do_xfer_with_response(ph, t); |
| 351 |
if (!ret) { |
| 352 |
struct scmi_resp_voltage_level_set_complete *resp; |
| 353 |
|
| 354 |
resp = t->rx.buf; |
| 355 |
if (le32_to_cpu(resp->domain_id) == domain_id) |
| 356 |
dev_dbg(ph->dev, |
| 357 |
"Voltage domain %d set async to %d\n", |
| 358 |
v->id, |
| 359 |
le32_to_cpu(resp->voltage_level)); |
| 360 |
else |
| 361 |
ret = -EPROTO; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
ph->xops->xfer_put(ph, t); |
| 366 |
return ret; |
| 367 |
} |
| 368 |
|
| 369 |
static int scmi_voltage_level_get(const struct scmi_protocol_handle *ph, |
| 370 |
u32 domain_id, s32 *volt_uV) |
| 371 |
{ |
| 372 |
return __scmi_voltage_get_u32(ph, VOLTAGE_LEVEL_GET, |
| 373 |
domain_id, (u32 *)volt_uV); |
| 374 |
} |
| 375 |
|
| 376 |
static const struct scmi_voltage_info * __must_check |
| 377 |
scmi_voltage_info_get(const struct scmi_protocol_handle *ph, u32 domain_id) |
| 378 |
{ |
| 379 |
struct voltage_info *vinfo = ph->get_priv(ph); |
| 380 |
|
| 381 |
if (domain_id >= vinfo->num_domains || |
| 382 |
!vinfo->domains[domain_id].num_levels) |
| 383 |
return NULL; |
| 384 |
|
| 385 |
return vinfo->domains + domain_id; |
| 386 |
} |
| 387 |
|
| 388 |
static int scmi_voltage_domains_num_get(const struct scmi_protocol_handle *ph) |
| 389 |
{ |
| 390 |
struct voltage_info *vinfo = ph->get_priv(ph); |
| 391 |
|
| 392 |
return vinfo->num_domains; |
| 393 |
} |
| 394 |
|
| 395 |
static const struct scmi_voltage_proto_ops voltage_proto_ops = { |
| 396 |
.num_domains_get = scmi_voltage_domains_num_get, |
| 397 |
.info_get = scmi_voltage_info_get, |
| 398 |
.config_set = scmi_voltage_config_set, |
| 399 |
.config_get = scmi_voltage_config_get, |
| 400 |
.level_set = scmi_voltage_level_set, |
| 401 |
.level_get = scmi_voltage_level_get, |
| 402 |
}; |
| 403 |
|
| 404 |
static int scmi_voltage_protocol_init(const struct scmi_protocol_handle *ph) |
| 405 |
{ |
| 406 |
int ret; |
| 407 |
struct voltage_info *vinfo; |
| 408 |
|
| 409 |
dev_dbg(ph->dev, "Voltage Version %d.%d\n", |
| 410 |
PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version)); |
| 411 |
|
| 412 |
vinfo = devm_kzalloc(ph->dev, sizeof(*vinfo), GFP_KERNEL); |
| 413 |
if (!vinfo) |
| 414 |
return -ENOMEM; |
| 415 |
|
| 416 |
ret = scmi_protocol_attributes_get(ph, vinfo); |
| 417 |
if (ret) |
| 418 |
return ret; |
| 419 |
|
| 420 |
if (vinfo->num_domains) { |
| 421 |
vinfo->domains = devm_kcalloc(ph->dev, vinfo->num_domains, |
| 422 |
sizeof(*vinfo->domains), |
| 423 |
GFP_KERNEL); |
| 424 |
if (!vinfo->domains) |
| 425 |
return -ENOMEM; |
| 426 |
ret = scmi_voltage_descriptors_get(ph, vinfo); |
| 427 |
if (ret) |
| 428 |
return ret; |
| 429 |
} else { |
| 430 |
dev_warn(ph->dev, "No Voltage domains found.\n"); |
| 431 |
} |
| 432 |
|
| 433 |
return ph->set_priv(ph, vinfo); |
| 434 |
} |
| 435 |
|
| 436 |
static const struct scmi_protocol scmi_voltage = { |
| 437 |
.id = SCMI_PROTOCOL_VOLTAGE, |
| 438 |
.owner = THIS_MODULE, |
| 439 |
.instance_init = &scmi_voltage_protocol_init, |
| 440 |
.ops = &voltage_proto_ops, |
| 441 |
.supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION, |
| 442 |
}; |
| 443 |
|
| 444 |
DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(voltage, scmi_voltage) |