1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) Sensor Protocol 4 * 5 * Copyright (C) 2018-2022 ARM Ltd. 6 */ 7 8 #define pr_fmt(fmt) "SCMI Notifications SENSOR - " fmt 9 10 #include <linux/bitfield.h> 11 #include <linux/module.h> 12 #include <linux/scmi_protocol.h> 13 14 #include "protocols.h" 15 #include "notify.h" 16 17 #define SCMI_MAX_NUM_SENSOR_AXIS 63 18 #define SCMIv2_SENSOR_PROTOCOL 0x10000 19 20 enum scmi_sensor_protocol_cmd { 21 SENSOR_DESCRIPTION_GET = 0x3, 22 SENSOR_TRIP_POINT_NOTIFY = 0x4, 23 SENSOR_TRIP_POINT_CONFIG = 0x5, 24 SENSOR_READING_GET = 0x6, 25 SENSOR_AXIS_DESCRIPTION_GET = 0x7, 26 SENSOR_LIST_UPDATE_INTERVALS = 0x8, 27 SENSOR_CONFIG_GET = 0x9, 28 SENSOR_CONFIG_SET = 0xA, 29 SENSOR_CONTINUOUS_UPDATE_NOTIFY = 0xB, 30 SENSOR_NAME_GET = 0xC, 31 SENSOR_AXIS_NAME_GET = 0xD, 32 }; 33 34 struct scmi_msg_resp_sensor_attributes { 35 __le16 num_sensors; 36 u8 max_requests; 37 u8 reserved; 38 __le32 reg_addr_low; 39 __le32 reg_addr_high; 40 __le32 reg_size; 41 }; 42 43 /* v3 attributes_low macros */ 44 #define SUPPORTS_UPDATE_NOTIFY(x) FIELD_GET(BIT(30), (x)) 45 #define SENSOR_TSTAMP_EXP(x) FIELD_GET(GENMASK(14, 10), (x)) 46 #define SUPPORTS_TIMESTAMP(x) FIELD_GET(BIT(9), (x)) 47 #define SUPPORTS_EXTEND_ATTRS(x) FIELD_GET(BIT(8), (x)) 48 49 /* v2 attributes_high macros */ 50 #define SENSOR_UPDATE_BASE(x) FIELD_GET(GENMASK(31, 27), (x)) 51 #define SENSOR_UPDATE_SCALE(x) FIELD_GET(GENMASK(26, 22), (x)) 52 53 /* v3 attributes_high macros */ 54 #define SENSOR_AXIS_NUMBER(x) FIELD_GET(GENMASK(21, 16), (x)) 55 #define SUPPORTS_AXIS(x) FIELD_GET(BIT(8), (x)) 56 57 /* v3 resolution macros */ 58 #define SENSOR_RES(x) FIELD_GET(GENMASK(26, 0), (x)) 59 #define SENSOR_RES_EXP(x) FIELD_GET(GENMASK(31, 27), (x)) 60 61 struct scmi_msg_resp_attrs { 62 __le32 min_range_low; 63 __le32 min_range_high; 64 __le32 max_range_low; 65 __le32 max_range_high; 66 }; 67 68 struct scmi_msg_sensor_description { 69 __le32 desc_index; 70 }; 71 72 struct scmi_msg_resp_sensor_description { 73 __le16 num_returned; 74 __le16 num_remaining; 75 struct scmi_sensor_descriptor { 76 __le32 id; 77 __le32 attributes_low; 78 /* Common attributes_low macros */ 79 #define SUPPORTS_ASYNC_READ(x) FIELD_GET(BIT(31), (x)) 80 #define SUPPORTS_EXTENDED_NAMES(x) FIELD_GET(BIT(29), (x)) 81 #define NUM_TRIP_POINTS(x) FIELD_GET(GENMASK(7, 0), (x)) 82 __le32 attributes_high; 83 /* Common attributes_high macros */ 84 #define SENSOR_SCALE(x) FIELD_GET(GENMASK(15, 11), (x)) 85 #define SENSOR_SCALE_SIGN BIT(4) 86 #define SENSOR_SCALE_EXTEND GENMASK(31, 5) 87 #define SENSOR_TYPE(x) FIELD_GET(GENMASK(7, 0), (x)) 88 u8 name[SCMI_SHORT_NAME_MAX_SIZE]; 89 /* only for version > 2.0 */ 90 __le32 power; 91 __le32 resolution; 92 struct scmi_msg_resp_attrs scalar_attrs; 93 } desc[]; 94 }; 95 96 /* Base scmi_sensor_descriptor size excluding extended attrs after name */ 97 #define SCMI_MSG_RESP_SENS_DESCR_BASE_SZ 28 98 99 /* Sign extend to a full s32 */ 100 #define S32_EXT(v) \ 101 ({ \ 102 int __v = (v); \ 103 \ 104 if (__v & SENSOR_SCALE_SIGN) \ 105 __v |= SENSOR_SCALE_EXTEND; \ 106 __v; \ 107 }) 108 109 struct scmi_msg_sensor_axis_description_get { 110 __le32 id; 111 __le32 axis_desc_index; 112 }; 113 114 struct scmi_msg_resp_sensor_axis_description { 115 __le32 num_axis_flags; 116 #define NUM_AXIS_RETURNED(x) FIELD_GET(GENMASK(5, 0), (x)) 117 #define NUM_AXIS_REMAINING(x) FIELD_GET(GENMASK(31, 26), (x)) 118 struct scmi_axis_descriptor { 119 __le32 id; 120 __le32 attributes_low; 121 #define SUPPORTS_EXTENDED_AXIS_NAMES(x) FIELD_GET(BIT(9), (x)) 122 __le32 attributes_high; 123 u8 name[SCMI_SHORT_NAME_MAX_SIZE]; 124 __le32 resolution; 125 struct scmi_msg_resp_attrs attrs; 126 } desc[]; 127 }; 128 129 struct scmi_msg_resp_sensor_axis_names_description { 130 __le32 num_axis_flags; 131 struct scmi_sensor_axis_name_descriptor { 132 __le32 axis_id; 133 u8 name[SCMI_MAX_STR_SIZE]; 134 } desc[]; 135 }; 136 137 /* Base scmi_axis_descriptor size excluding extended attrs after name */ 138 #define SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ 28 139 140 struct scmi_msg_sensor_list_update_intervals { 141 __le32 id; 142 __le32 index; 143 }; 144 145 struct scmi_msg_resp_sensor_list_update_intervals { 146 __le32 num_intervals_flags; 147 #define NUM_INTERVALS_RETURNED(x) FIELD_GET(GENMASK(11, 0), (x)) 148 #define SEGMENTED_INTVL_FORMAT(x) FIELD_GET(BIT(12), (x)) 149 #define NUM_INTERVALS_REMAINING(x) FIELD_GET(GENMASK(31, 16), (x)) 150 __le32 intervals[]; 151 }; 152 153 struct scmi_msg_sensor_request_notify { 154 __le32 id; 155 __le32 event_control; 156 #define SENSOR_NOTIFY_ALL BIT(0) 157 }; 158 159 struct scmi_msg_set_sensor_trip_point { 160 __le32 id; 161 __le32 event_control; 162 #define SENSOR_TP_EVENT_MASK (0x3) 163 #define SENSOR_TP_DISABLED 0x0 164 #define SENSOR_TP_POSITIVE 0x1 165 #define SENSOR_TP_NEGATIVE 0x2 166 #define SENSOR_TP_BOTH 0x3 167 #define SENSOR_TP_ID(x) (((x) & 0xff) << 4) 168 __le32 value_low; 169 __le32 value_high; 170 }; 171 172 struct scmi_msg_sensor_config_set { 173 __le32 id; 174 __le32 sensor_config; 175 }; 176 177 struct scmi_msg_sensor_reading_get { 178 __le32 id; 179 __le32 flags; 180 #define SENSOR_READ_ASYNC BIT(0) 181 }; 182 183 struct scmi_resp_sensor_reading_complete { 184 __le32 id; 185 __le32 readings_low; 186 __le32 readings_high; 187 }; 188 189 struct scmi_sensor_reading_resp { 190 __le32 sensor_value_low; 191 __le32 sensor_value_high; 192 __le32 timestamp_low; 193 __le32 timestamp_high; 194 }; 195 196 struct scmi_resp_sensor_reading_complete_v3 { 197 __le32 id; 198 struct scmi_sensor_reading_resp readings[]; 199 }; 200 201 struct scmi_sensor_trip_notify_payld { 202 __le32 agent_id; 203 __le32 sensor_id; 204 __le32 trip_point_desc; 205 }; 206 207 struct scmi_sensor_update_notify_payld { 208 __le32 agent_id; 209 __le32 sensor_id; 210 struct scmi_sensor_reading_resp readings[]; 211 }; 212 213 struct sensors_info { 214 u32 version; 215 int num_sensors; 216 int max_requests; 217 u64 reg_addr; 218 u32 reg_size; 219 struct scmi_sensor_info *sensors; 220 }; 221 222 static int scmi_sensor_attributes_get(const struct scmi_protocol_handle *ph, 223 struct sensors_info *si) 224 { 225 int ret; 226 struct scmi_xfer *t; 227 struct scmi_msg_resp_sensor_attributes *attr; 228 229 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 230 0, sizeof(*attr), &t); 231 if (ret) 232 return ret; 233 234 attr = t->rx.buf; 235 236 ret = ph->xops->do_xfer(ph, t); 237 if (!ret) { 238 si->num_sensors = le16_to_cpu(attr->num_sensors); 239 si->max_requests = attr->max_requests; 240 si->reg_addr = le32_to_cpu(attr->reg_addr_low) | 241 (u64)le32_to_cpu(attr->reg_addr_high) << 32; 242 si->reg_size = le32_to_cpu(attr->reg_size); 243 } 244 245 ph->xops->xfer_put(ph, t); 246 return ret; 247 } 248 249 static inline void scmi_parse_range_attrs(struct scmi_range_attrs *out, 250 const struct scmi_msg_resp_attrs *in) 251 { 252 out->min_range = get_unaligned_le64((void *)&in->min_range_low); 253 out->max_range = get_unaligned_le64((void *)&in->max_range_low); 254 } 255 256 struct scmi_sens_ipriv { 257 void *priv; 258 struct device *dev; 259 }; 260 261 static void iter_intervals_prepare_message(void *message, 262 unsigned int desc_index, 263 const void *p) 264 { 265 struct scmi_msg_sensor_list_update_intervals *msg = message; 266 const struct scmi_sensor_info *s; 267 268 s = ((const struct scmi_sens_ipriv *)p)->priv; 269 /* Set the number of sensors to be skipped/already read */ 270 msg->id = cpu_to_le32(s->id); 271 msg->index = cpu_to_le32(desc_index); 272 } 273 274 static int iter_intervals_update_state(struct scmi_iterator_state *st, 275 const void *response, void *p) 276 { 277 u32 flags; 278 struct scmi_sensor_info *s = ((struct scmi_sens_ipriv *)p)->priv; 279 struct device *dev = ((struct scmi_sens_ipriv *)p)->dev; 280 const struct scmi_msg_resp_sensor_list_update_intervals *r = response; 281 282 flags = le32_to_cpu(r->num_intervals_flags); 283 st->num_returned = NUM_INTERVALS_RETURNED(flags); 284 st->num_remaining = NUM_INTERVALS_REMAINING(flags); 285 286 /* 287 * Max intervals is not declared previously anywhere so we 288 * assume it's returned+remaining on first call. 289 */ 290 if (!st->max_resources) { 291 s->intervals.segmented = SEGMENTED_INTVL_FORMAT(flags); 292 s->intervals.count = st->num_returned + st->num_remaining; 293 /* segmented intervals are reported in one triplet */ 294 if (s->intervals.segmented && 295 (st->num_remaining || st->num_returned != 3)) { 296 dev_err(dev, 297 "Sensor ID:%d advertises an invalid segmented interval (%d)\n", 298 s->id, s->intervals.count); 299 s->intervals.segmented = false; 300 s->intervals.count = 0; 301 return -EINVAL; 302 } 303 /* Direct allocation when exceeding pre-allocated */ 304 if (s->intervals.count >= SCMI_MAX_PREALLOC_POOL) { 305 s->intervals.desc = 306 devm_kcalloc(dev, 307 s->intervals.count, 308 sizeof(*s->intervals.desc), 309 GFP_KERNEL); 310 if (!s->intervals.desc) { 311 s->intervals.segmented = false; 312 s->intervals.count = 0; 313 return -ENOMEM; 314 } 315 } 316 317 st->max_resources = s->intervals.count; 318 } 319 320 return 0; 321 } 322 323 static int 324 iter_intervals_process_response(const struct scmi_protocol_handle *ph, 325 const void *response, 326 struct scmi_iterator_state *st, void *p) 327 { 328 const struct scmi_msg_resp_sensor_list_update_intervals *r = response; 329 struct scmi_sensor_info *s = ((struct scmi_sens_ipriv *)p)->priv; 330 331 s->intervals.desc[st->desc_index + st->loop_idx] = 332 le32_to_cpu(r->intervals[st->loop_idx]); 333 334 return 0; 335 } 336 337 static int scmi_sensor_update_intervals(const struct scmi_protocol_handle *ph, 338 struct scmi_sensor_info *s) 339 { 340 void *iter; 341 struct scmi_msg_sensor_list_update_intervals *msg; 342 struct scmi_iterator_ops ops = { 343 .prepare_message = iter_intervals_prepare_message, 344 .update_state = iter_intervals_update_state, 345 .process_response = iter_intervals_process_response, 346 }; 347 struct scmi_sens_ipriv upriv = { 348 .priv = s, 349 .dev = ph->dev, 350 }; 351 352 iter = ph->hops->iter_response_init(ph, &ops, s->intervals.count, 353 SENSOR_LIST_UPDATE_INTERVALS, 354 sizeof(*msg), &upriv); 355 if (IS_ERR(iter)) 356 return PTR_ERR(iter); 357 358 return ph->hops->iter_response_run(iter); 359 } 360 361 static void iter_axes_desc_prepare_message(void *message, 362 const unsigned int desc_index, 363 const void *priv) 364 { 365 struct scmi_msg_sensor_axis_description_get *msg = message; 366 const struct scmi_sensor_info *s = priv; 367 368 /* Set the number of sensors to be skipped/already read */ 369 msg->id = cpu_to_le32(s->id); 370 msg->axis_desc_index = cpu_to_le32(desc_index); 371 } 372 373 static int 374 iter_axes_desc_update_state(struct scmi_iterator_state *st, 375 const void *response, void *priv) 376 { 377 u32 flags; 378 const struct scmi_msg_resp_sensor_axis_description *r = response; 379 380 flags = le32_to_cpu(r->num_axis_flags); 381 st->num_returned = NUM_AXIS_RETURNED(flags); 382 st->num_remaining = NUM_AXIS_REMAINING(flags); 383 st->priv = (void *)&r->desc[0]; 384 385 return 0; 386 } 387 388 static int 389 iter_axes_desc_process_response(const struct scmi_protocol_handle *ph, 390 const void *response, 391 struct scmi_iterator_state *st, void *priv) 392 { 393 u32 attrh, attrl; 394 struct scmi_sensor_axis_info *a; 395 size_t dsize = SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ; 396 struct scmi_sensor_info *s = priv; 397 const struct scmi_axis_descriptor *adesc = st->priv; 398 399 attrl = le32_to_cpu(adesc->attributes_low); 400 401 a = &s->axis[st->desc_index + st->loop_idx]; 402 a->id = le32_to_cpu(adesc->id); 403 a->extended_attrs = SUPPORTS_EXTEND_ATTRS(attrl); 404 405 attrh = le32_to_cpu(adesc->attributes_high); 406 a->scale = S32_EXT(SENSOR_SCALE(attrh)); 407 a->type = SENSOR_TYPE(attrh); 408 strscpy(a->name, adesc->name, SCMI_MAX_STR_SIZE); 409 410 if (a->extended_attrs) { 411 unsigned int ares = le32_to_cpu(adesc->resolution); 412 413 a->resolution = SENSOR_RES(ares); 414 a->exponent = S32_EXT(SENSOR_RES_EXP(ares)); 415 dsize += sizeof(adesc->resolution); 416 417 scmi_parse_range_attrs(&a->attrs, &adesc->attrs); 418 dsize += sizeof(adesc->attrs); 419 } 420 st->priv = ((u8 *)adesc + dsize); 421 422 return 0; 423 } 424 425 static int 426 iter_axes_extended_name_update_state(struct scmi_iterator_state *st, 427 const void *response, void *priv) 428 { 429 u32 flags; 430 const struct scmi_msg_resp_sensor_axis_names_description *r = response; 431 432 flags = le32_to_cpu(r->num_axis_flags); 433 st->num_returned = NUM_AXIS_RETURNED(flags); 434 st->num_remaining = NUM_AXIS_REMAINING(flags); 435 st->priv = (void *)&r->desc[0]; 436 437 return 0; 438 } 439 440 static int 441 iter_axes_extended_name_process_response(const struct scmi_protocol_handle *ph, 442 const void *response, 443 struct scmi_iterator_state *st, 444 void *priv) 445 { 446 struct scmi_sensor_axis_info *a; 447 const struct scmi_sensor_info *s = priv; 448 struct scmi_sensor_axis_name_descriptor *adesc = st->priv; 449 450 a = &s->axis[st->desc_index + st->loop_idx]; 451 strscpy(a->name, adesc->name, SCMI_MAX_STR_SIZE); 452 st->priv = ++adesc; 453 454 return 0; 455 } 456 457 static int 458 scmi_sensor_axis_extended_names_get(const struct scmi_protocol_handle *ph, 459 struct scmi_sensor_info *s) 460 { 461 void *iter; 462 struct scmi_msg_sensor_axis_description_get *msg; 463 struct scmi_iterator_ops ops = { 464 .prepare_message = iter_axes_desc_prepare_message, 465 .update_state = iter_axes_extended_name_update_state, 466 .process_response = iter_axes_extended_name_process_response, 467 }; 468 469 iter = ph->hops->iter_response_init(ph, &ops, s->num_axis, 470 SENSOR_AXIS_NAME_GET, 471 sizeof(*msg), s); 472 if (IS_ERR(iter)) 473 return PTR_ERR(iter); 474 475 return ph->hops->iter_response_run(iter); 476 } 477 478 static int scmi_sensor_axis_description(const struct scmi_protocol_handle *ph, 479 struct scmi_sensor_info *s, 480 u32 version) 481 { 482 int ret; 483 void *iter; 484 struct scmi_msg_sensor_axis_description_get *msg; 485 struct scmi_iterator_ops ops = { 486 .prepare_message = iter_axes_desc_prepare_message, 487 .update_state = iter_axes_desc_update_state, 488 .process_response = iter_axes_desc_process_response, 489 }; 490 491 s->axis = devm_kcalloc(ph->dev, s->num_axis, 492 sizeof(*s->axis), GFP_KERNEL); 493 if (!s->axis) 494 return -ENOMEM; 495 496 iter = ph->hops->iter_response_init(ph, &ops, s->num_axis, 497 SENSOR_AXIS_DESCRIPTION_GET, 498 sizeof(*msg), s); 499 if (IS_ERR(iter)) 500 return PTR_ERR(iter); 501 502 ret = ph->hops->iter_response_run(iter); 503 if (ret) 504 return ret; 505 506 if (PROTOCOL_REV_MAJOR(version) >= 0x3) 507 ret = scmi_sensor_axis_extended_names_get(ph, s); 508 509 return ret; 510 } 511 512 static void iter_sens_descr_prepare_message(void *message, 513 unsigned int desc_index, 514 const void *priv) 515 { 516 struct scmi_msg_sensor_description *msg = message; 517 518 msg->desc_index = cpu_to_le32(desc_index); 519 } 520 521 static int iter_sens_descr_update_state(struct scmi_iterator_state *st, 522 const void *response, void *priv) 523 { 524 const struct scmi_msg_resp_sensor_description *r = response; 525 526 st->num_returned = le16_to_cpu(r->num_returned); 527 st->num_remaining = le16_to_cpu(r->num_remaining); 528 st->priv = (void *)&r->desc[0]; 529 530 return 0; 531 } 532 533 static int 534 iter_sens_descr_process_response(const struct scmi_protocol_handle *ph, 535 const void *response, 536 struct scmi_iterator_state *st, void *priv) 537 538 { 539 int ret = 0; 540 u32 attrh, attrl; 541 size_t dsize = SCMI_MSG_RESP_SENS_DESCR_BASE_SZ; 542 struct scmi_sensor_info *s; 543 struct sensors_info *si = priv; 544 const struct scmi_sensor_descriptor *sdesc = st->priv; 545 546 s = &si->sensors[st->desc_index + st->loop_idx]; 547 s->id = le32_to_cpu(sdesc->id); 548 549 attrl = le32_to_cpu(sdesc->attributes_low); 550 /* common bitfields parsing */ 551 s->async = SUPPORTS_ASYNC_READ(attrl); 552 s->num_trip_points = NUM_TRIP_POINTS(attrl); 553 /** 554 * only SCMIv3.0 specific bitfield below. 555 * Such bitfields are assumed to be zeroed on non 556 * relevant fw versions...assuming fw not buggy ! 557 */ 558 s->update = SUPPORTS_UPDATE_NOTIFY(attrl); 559 s->timestamped = SUPPORTS_TIMESTAMP(attrl); 560 if (s->timestamped) 561 s->tstamp_scale = S32_EXT(SENSOR_TSTAMP_EXP(attrl)); 562 s->extended_scalar_attrs = SUPPORTS_EXTEND_ATTRS(attrl); 563 564 attrh = le32_to_cpu(sdesc->attributes_high); 565 /* common bitfields parsing */ 566 s->scale = S32_EXT(SENSOR_SCALE(attrh)); 567 s->type = SENSOR_TYPE(attrh); 568 /* Use pre-allocated pool wherever possible */ 569 s->intervals.desc = s->intervals.prealloc_pool; 570 if (si->version == SCMIv2_SENSOR_PROTOCOL) { 571 s->intervals.segmented = false; 572 s->intervals.count = 1; 573 /* 574 * Convert SCMIv2.0 update interval format to 575 * SCMIv3.0 to be used as the common exposed 576 * descriptor, accessible via common macros. 577 */ 578 s->intervals.desc[0] = (SENSOR_UPDATE_BASE(attrh) << 5) | 579 SENSOR_UPDATE_SCALE(attrh); 580 } else { 581 /* 582 * From SCMIv3.0 update intervals are retrieved 583 * via a dedicated (optional) command. 584 * Since the command is optional, on error carry 585 * on without any update interval. 586 */ 587 if (scmi_sensor_update_intervals(ph, s)) 588 dev_dbg(ph->dev, 589 "Update Intervals not available for sensor ID:%d\n", 590 s->id); 591 } 592 /** 593 * only > SCMIv2.0 specific bitfield below. 594 * Such bitfields are assumed to be zeroed on non 595 * relevant fw versions...assuming fw not buggy ! 596 */ 597 s->num_axis = min_t(unsigned int, 598 SUPPORTS_AXIS(attrh) ? 599 SENSOR_AXIS_NUMBER(attrh) : 0, 600 SCMI_MAX_NUM_SENSOR_AXIS); 601 strscpy(s->name, sdesc->name, SCMI_MAX_STR_SIZE); 602 603 /* 604 * If supported overwrite short name with the extended 605 * one; on error just carry on and use already provided 606 * short name. 607 */ 608 if (PROTOCOL_REV_MAJOR(si->version) >= 0x3 && 609 SUPPORTS_EXTENDED_NAMES(attrl)) 610 ph->hops->extended_name_get(ph, SENSOR_NAME_GET, s->id, 611 s->name, SCMI_MAX_STR_SIZE); 612 613 if (s->extended_scalar_attrs) { 614 s->sensor_power = le32_to_cpu(sdesc->power); 615 dsize += sizeof(sdesc->power); 616 617 /* Only for sensors reporting scalar values */ 618 if (s->num_axis == 0) { 619 unsigned int sres = le32_to_cpu(sdesc->resolution); 620 621 s->resolution = SENSOR_RES(sres); 622 s->exponent = S32_EXT(SENSOR_RES_EXP(sres)); 623 dsize += sizeof(sdesc->resolution); 624 625 scmi_parse_range_attrs(&s->scalar_attrs, 626 &sdesc->scalar_attrs); 627 dsize += sizeof(sdesc->scalar_attrs); 628 } 629 } 630 631 if (s->num_axis > 0) 632 ret = scmi_sensor_axis_description(ph, s, si->version); 633 634 st->priv = ((u8 *)sdesc + dsize); 635 636 return ret; 637 } 638 639 static int scmi_sensor_description_get(const struct scmi_protocol_handle *ph, 640 struct sensors_info *si) 641 { 642 void *iter; 643 struct scmi_iterator_ops ops = { 644 .prepare_message = iter_sens_descr_prepare_message, 645 .update_state = iter_sens_descr_update_state, 646 .process_response = iter_sens_descr_process_response, 647 }; 648 649 iter = ph->hops->iter_response_init(ph, &ops, si->num_sensors, 650 SENSOR_DESCRIPTION_GET, 651 sizeof(__le32), si); 652 if (IS_ERR(iter)) 653 return PTR_ERR(iter); 654 655 return ph->hops->iter_response_run(iter); 656 } 657 658 static inline int 659 scmi_sensor_request_notify(const struct scmi_protocol_handle *ph, u32 sensor_id, 660 u8 message_id, bool enable) 661 { 662 int ret; 663 u32 evt_cntl = enable ? SENSOR_NOTIFY_ALL : 0; 664 struct scmi_xfer *t; 665 struct scmi_msg_sensor_request_notify *cfg; 666 667 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*cfg), 0, &t); 668 if (ret) 669 return ret; 670 671 cfg = t->tx.buf; 672 cfg->id = cpu_to_le32(sensor_id); 673 cfg->event_control = cpu_to_le32(evt_cntl); 674 675 ret = ph->xops->do_xfer(ph, t); 676 677 ph->xops->xfer_put(ph, t); 678 return ret; 679 } 680 681 static int scmi_sensor_trip_point_notify(const struct scmi_protocol_handle *ph, 682 u32 sensor_id, bool enable) 683 { 684 return scmi_sensor_request_notify(ph, sensor_id, 685 SENSOR_TRIP_POINT_NOTIFY, 686 enable); 687 } 688 689 static int 690 scmi_sensor_continuous_update_notify(const struct scmi_protocol_handle *ph, 691 u32 sensor_id, bool enable) 692 { 693 return scmi_sensor_request_notify(ph, sensor_id, 694 SENSOR_CONTINUOUS_UPDATE_NOTIFY, 695 enable); 696 } 697 698 static int 699 scmi_sensor_trip_point_config(const struct scmi_protocol_handle *ph, 700 u32 sensor_id, u8 trip_id, u64 trip_value) 701 { 702 int ret; 703 u32 evt_cntl = SENSOR_TP_BOTH; 704 struct scmi_xfer *t; 705 struct scmi_msg_set_sensor_trip_point *trip; 706 707 ret = ph->xops->xfer_get_init(ph, SENSOR_TRIP_POINT_CONFIG, 708 sizeof(*trip), 0, &t); 709 if (ret) 710 return ret; 711 712 trip = t->tx.buf; 713 trip->id = cpu_to_le32(sensor_id); 714 trip->event_control = cpu_to_le32(evt_cntl | SENSOR_TP_ID(trip_id)); 715 trip->value_low = cpu_to_le32(trip_value & 0xffffffff); 716 trip->value_high = cpu_to_le32(trip_value >> 32); 717 718 ret = ph->xops->do_xfer(ph, t); 719 720 ph->xops->xfer_put(ph, t); 721 return ret; 722 } 723 724 static int scmi_sensor_config_get(const struct scmi_protocol_handle *ph, 725 u32 sensor_id, u32 *sensor_config) 726 { 727 int ret; 728 struct scmi_xfer *t; 729 730 ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_GET, 731 sizeof(__le32), sizeof(__le32), &t); 732 if (ret) 733 return ret; 734 735 put_unaligned_le32(sensor_id, t->tx.buf); 736 ret = ph->xops->do_xfer(ph, t); 737 if (!ret) { 738 struct sensors_info *si = ph->get_priv(ph); 739 struct scmi_sensor_info *s = si->sensors + sensor_id; 740 741 *sensor_config = get_unaligned_le64(t->rx.buf); 742 s->sensor_config = *sensor_config; 743 } 744 745 ph->xops->xfer_put(ph, t); 746 return ret; 747 } 748 749 static int scmi_sensor_config_set(const struct scmi_protocol_handle *ph, 750 u32 sensor_id, u32 sensor_config) 751 { 752 int ret; 753 struct scmi_xfer *t; 754 struct scmi_msg_sensor_config_set *msg; 755 756 ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_SET, 757 sizeof(*msg), 0, &t); 758 if (ret) 759 return ret; 760 761 msg = t->tx.buf; 762 msg->id = cpu_to_le32(sensor_id); 763 msg->sensor_config = cpu_to_le32(sensor_config); 764 765 ret = ph->xops->do_xfer(ph, t); 766 if (!ret) { 767 struct sensors_info *si = ph->get_priv(ph); 768 struct scmi_sensor_info *s = si->sensors + sensor_id; 769 770 s->sensor_config = sensor_config; 771 } 772 773 ph->xops->xfer_put(ph, t); 774 return ret; 775 } 776 777 /** 778 * scmi_sensor_reading_get - Read scalar sensor value 779 * @ph: Protocol handle 780 * @sensor_id: Sensor ID 781 * @value: The 64bit value sensor reading 782 * 783 * This function returns a single 64 bit reading value representing the sensor 784 * value; if the platform SCMI Protocol implementation and the sensor support 785 * multiple axis and timestamped-reads, this just returns the first axis while 786 * dropping the timestamp value. 787 * Use instead the @scmi_sensor_reading_get_timestamped to retrieve the array of 788 * timestamped multi-axis values. 789 * 790 * Return: 0 on Success 791 */ 792 static int scmi_sensor_reading_get(const struct scmi_protocol_handle *ph, 793 u32 sensor_id, u64 *value) 794 { 795 int ret; 796 struct scmi_xfer *t; 797 struct scmi_msg_sensor_reading_get *sensor; 798 struct sensors_info *si = ph->get_priv(ph); 799 struct scmi_sensor_info *s = si->sensors + sensor_id; 800 801 ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET, 802 sizeof(*sensor), 0, &t); 803 if (ret) 804 return ret; 805 806 sensor = t->tx.buf; 807 sensor->id = cpu_to_le32(sensor_id); 808 if (s->async) { 809 sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC); 810 ret = ph->xops->do_xfer_with_response(ph, t); 811 if (!ret) { 812 struct scmi_resp_sensor_reading_complete *resp; 813 814 resp = t->rx.buf; 815 if (le32_to_cpu(resp->id) == sensor_id) 816 *value = 817 get_unaligned_le64(&resp->readings_low); 818 else 819 ret = -EPROTO; 820 } 821 } else { 822 sensor->flags = cpu_to_le32(0); 823 ret = ph->xops->do_xfer(ph, t); 824 if (!ret) 825 *value = get_unaligned_le64(t->rx.buf); 826 } 827 828 ph->xops->xfer_put(ph, t); 829 return ret; 830 } 831 832 static inline void 833 scmi_parse_sensor_readings(struct scmi_sensor_reading *out, 834 const struct scmi_sensor_reading_resp *in) 835 { 836 out->value = get_unaligned_le64((void *)&in->sensor_value_low); 837 out->timestamp = get_unaligned_le64((void *)&in->timestamp_low); 838 } 839 840 /** 841 * scmi_sensor_reading_get_timestamped - Read multiple-axis timestamped values 842 * @ph: Protocol handle 843 * @sensor_id: Sensor ID 844 * @count: The length of the provided @readings array 845 * @readings: An array of elements each representing a timestamped per-axis 846 * reading of type @struct scmi_sensor_reading. 847 * Returned readings are ordered as the @axis descriptors array 848 * included in @struct scmi_sensor_info and the max number of 849 * returned elements is min(@count, @num_axis); ideally the provided 850 * array should be of length @count equal to @num_axis. 851 * 852 * Return: 0 on Success 853 */ 854 static int 855 scmi_sensor_reading_get_timestamped(const struct scmi_protocol_handle *ph, 856 u32 sensor_id, u8 count, 857 struct scmi_sensor_reading *readings) 858 { 859 int ret; 860 struct scmi_xfer *t; 861 struct scmi_msg_sensor_reading_get *sensor; 862 struct sensors_info *si = ph->get_priv(ph); 863 struct scmi_sensor_info *s = si->sensors + sensor_id; 864 865 if (!count || !readings || 866 (!s->num_axis && count > 1) || (s->num_axis && count > s->num_axis)) 867 return -EINVAL; 868 869 ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET, 870 sizeof(*sensor), 0, &t); 871 if (ret) 872 return ret; 873 874 sensor = t->tx.buf; 875 sensor->id = cpu_to_le32(sensor_id); 876 if (s->async) { 877 sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC); 878 ret = ph->xops->do_xfer_with_response(ph, t); 879 if (!ret) { 880 int i; 881 struct scmi_resp_sensor_reading_complete_v3 *resp; 882 883 resp = t->rx.buf; 884 /* Retrieve only the number of requested axis anyway */ 885 if (le32_to_cpu(resp->id) == sensor_id) 886 for (i = 0; i < count; i++) 887 scmi_parse_sensor_readings(&readings[i], 888 &resp->readings[i]); 889 else 890 ret = -EPROTO; 891 } 892 } else { 893 sensor->flags = cpu_to_le32(0); 894 ret = ph->xops->do_xfer(ph, t); 895 if (!ret) { 896 int i; 897 struct scmi_sensor_reading_resp *resp_readings; 898 899 resp_readings = t->rx.buf; 900 for (i = 0; i < count; i++) 901 scmi_parse_sensor_readings(&readings[i], 902 &resp_readings[i]); 903 } 904 } 905 906 ph->xops->xfer_put(ph, t); 907 return ret; 908 } 909 910 static const struct scmi_sensor_info * 911 scmi_sensor_info_get(const struct scmi_protocol_handle *ph, u32 sensor_id) 912 { 913 struct sensors_info *si = ph->get_priv(ph); 914 915 return si->sensors + sensor_id; 916 } 917 918 static int scmi_sensor_count_get(const struct scmi_protocol_handle *ph) 919 { 920 struct sensors_info *si = ph->get_priv(ph); 921 922 return si->num_sensors; 923 } 924 925 static const struct scmi_sensor_proto_ops sensor_proto_ops = { 926 .count_get = scmi_sensor_count_get, 927 .info_get = scmi_sensor_info_get, 928 .trip_point_config = scmi_sensor_trip_point_config, 929 .reading_get = scmi_sensor_reading_get, 930 .reading_get_timestamped = scmi_sensor_reading_get_timestamped, 931 .config_get = scmi_sensor_config_get, 932 .config_set = scmi_sensor_config_set, 933 }; 934 935 static int scmi_sensor_set_notify_enabled(const struct scmi_protocol_handle *ph, 936 u8 evt_id, u32 src_id, bool enable) 937 { 938 int ret; 939 940 switch (evt_id) { 941 case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT: 942 ret = scmi_sensor_trip_point_notify(ph, src_id, enable); 943 break; 944 case SCMI_EVENT_SENSOR_UPDATE: 945 ret = scmi_sensor_continuous_update_notify(ph, src_id, enable); 946 break; 947 default: 948 ret = -EINVAL; 949 break; 950 } 951 952 if (ret) 953 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n", 954 evt_id, src_id, ret); 955 956 return ret; 957 } 958 959 static void * 960 scmi_sensor_fill_custom_report(const struct scmi_protocol_handle *ph, 961 u8 evt_id, ktime_t timestamp, 962 const void *payld, size_t payld_sz, 963 void *report, u32 *src_id) 964 { 965 void *rep = NULL; 966 967 switch (evt_id) { 968 case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT: 969 { 970 const struct scmi_sensor_trip_notify_payld *p = payld; 971 struct scmi_sensor_trip_point_report *r = report; 972 973 if (sizeof(*p) != payld_sz) 974 break; 975 976 r->timestamp = timestamp; 977 r->agent_id = le32_to_cpu(p->agent_id); 978 r->sensor_id = le32_to_cpu(p->sensor_id); 979 r->trip_point_desc = le32_to_cpu(p->trip_point_desc); 980 *src_id = r->sensor_id; 981 rep = r; 982 break; 983 } 984 case SCMI_EVENT_SENSOR_UPDATE: 985 { 986 int i; 987 struct scmi_sensor_info *s; 988 const struct scmi_sensor_update_notify_payld *p = payld; 989 struct scmi_sensor_update_report *r = report; 990 struct sensors_info *sinfo = ph->get_priv(ph); 991 992 /* payld_sz is variable for this event */ 993 r->sensor_id = le32_to_cpu(p->sensor_id); 994 if (r->sensor_id >= sinfo->num_sensors) 995 break; 996 r->timestamp = timestamp; 997 r->agent_id = le32_to_cpu(p->agent_id); 998 s = &sinfo->sensors[r->sensor_id]; 999 /* 1000 * The generated report r (@struct scmi_sensor_update_report) 1001 * was pre-allocated to contain up to SCMI_MAX_NUM_SENSOR_AXIS 1002 * readings: here it is filled with the effective @num_axis 1003 * readings defined for this sensor or 1 for scalar sensors. 1004 */ 1005 r->readings_count = s->num_axis ?: 1; 1006 for (i = 0; i < r->readings_count; i++) 1007 scmi_parse_sensor_readings(&r->readings[i], 1008 &p->readings[i]); 1009 *src_id = r->sensor_id; 1010 rep = r; 1011 break; 1012 } 1013 default: 1014 break; 1015 } 1016 1017 return rep; 1018 } 1019 1020 static int scmi_sensor_get_num_sources(const struct scmi_protocol_handle *ph) 1021 { 1022 struct sensors_info *si = ph->get_priv(ph); 1023 1024 return si->num_sensors; 1025 } 1026 1027 static const struct scmi_event sensor_events[] = { 1028 { 1029 .id = SCMI_EVENT_SENSOR_TRIP_POINT_EVENT, 1030 .max_payld_sz = sizeof(struct scmi_sensor_trip_notify_payld), 1031 .max_report_sz = sizeof(struct scmi_sensor_trip_point_report), 1032 }, 1033 { 1034 .id = SCMI_EVENT_SENSOR_UPDATE, 1035 .max_payld_sz = 1036 sizeof(struct scmi_sensor_update_notify_payld) + 1037 SCMI_MAX_NUM_SENSOR_AXIS * 1038 sizeof(struct scmi_sensor_reading_resp), 1039 .max_report_sz = sizeof(struct scmi_sensor_update_report) + 1040 SCMI_MAX_NUM_SENSOR_AXIS * 1041 sizeof(struct scmi_sensor_reading), 1042 }, 1043 }; 1044 1045 static const struct scmi_event_ops sensor_event_ops = { 1046 .get_num_sources = scmi_sensor_get_num_sources, 1047 .set_notify_enabled = scmi_sensor_set_notify_enabled, 1048 .fill_custom_report = scmi_sensor_fill_custom_report, 1049 }; 1050 1051 static const struct scmi_protocol_events sensor_protocol_events = { 1052 .queue_sz = SCMI_PROTO_QUEUE_SZ, 1053 .ops = &sensor_event_ops, 1054 .evts = sensor_events, 1055 .num_events = ARRAY_SIZE(sensor_events), 1056 }; 1057 1058 static int scmi_sensors_protocol_init(const struct scmi_protocol_handle *ph) 1059 { 1060 u32 version; 1061 int ret; 1062 struct sensors_info *sinfo; 1063 1064 ret = ph->xops->version_get(ph, &version); 1065 if (ret) 1066 return ret; 1067 1068 dev_dbg(ph->dev, "Sensor Version %d.%d\n", 1069 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version)); 1070 1071 sinfo = devm_kzalloc(ph->dev, sizeof(*sinfo), GFP_KERNEL); 1072 if (!sinfo) 1073 return -ENOMEM; 1074 sinfo->version = version; 1075 1076 ret = scmi_sensor_attributes_get(ph, sinfo); 1077 if (ret) 1078 return ret; 1079 sinfo->sensors = devm_kcalloc(ph->dev, sinfo->num_sensors, 1080 sizeof(*sinfo->sensors), GFP_KERNEL); 1081 if (!sinfo->sensors) 1082 return -ENOMEM; 1083 1084 ret = scmi_sensor_description_get(ph, sinfo); 1085 if (ret) 1086 return ret; 1087 1088 return ph->set_priv(ph, sinfo); 1089 } 1090 1091 static const struct scmi_protocol scmi_sensors = { 1092 .id = SCMI_PROTOCOL_SENSOR, 1093 .owner = THIS_MODULE, 1094 .instance_init = &scmi_sensors_protocol_init, 1095 .ops = &sensor_proto_ops, 1096 .events = &sensor_protocol_events, 1097 }; 1098 1099 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(sensors, scmi_sensors) 1100