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