1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Sensor Protocol
5 * Copyright (C) 2018-2021 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications SENSOR - " fmt
10 #include <linux/bitfield.h>
11 #include <linux/module.h>
12 #include <linux/scmi_protocol.h>
17 #define SCMI_MAX_NUM_SENSOR_AXIS 63
18 #define SCMIv2_SENSOR_PROTOCOL 0x10000
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,
32 struct scmi_msg_resp_sensor_attributes {
41 /* v3 attributes_low macros */
42 #define SUPPORTS_UPDATE_NOTIFY(x) FIELD_GET(BIT(30), (x))
43 #define SENSOR_TSTAMP_EXP(x) FIELD_GET(GENMASK(14, 10), (x))
44 #define SUPPORTS_TIMESTAMP(x) FIELD_GET(BIT(9), (x))
45 #define SUPPORTS_EXTEND_ATTRS(x) FIELD_GET(BIT(8), (x))
47 /* v2 attributes_high macros */
48 #define SENSOR_UPDATE_BASE(x) FIELD_GET(GENMASK(31, 27), (x))
49 #define SENSOR_UPDATE_SCALE(x) FIELD_GET(GENMASK(26, 22), (x))
51 /* v3 attributes_high macros */
52 #define SENSOR_AXIS_NUMBER(x) FIELD_GET(GENMASK(21, 16), (x))
53 #define SUPPORTS_AXIS(x) FIELD_GET(BIT(8), (x))
55 /* v3 resolution macros */
56 #define SENSOR_RES(x) FIELD_GET(GENMASK(26, 0), (x))
57 #define SENSOR_RES_EXP(x) FIELD_GET(GENMASK(31, 27), (x))
59 struct scmi_msg_resp_attrs {
61 __le32 min_range_high;
63 __le32 max_range_high;
66 struct scmi_msg_resp_sensor_description {
69 struct scmi_sensor_descriptor {
71 __le32 attributes_low;
72 /* Common attributes_low macros */
73 #define SUPPORTS_ASYNC_READ(x) FIELD_GET(BIT(31), (x))
74 #define NUM_TRIP_POINTS(x) FIELD_GET(GENMASK(7, 0), (x))
75 __le32 attributes_high;
76 /* Common attributes_high macros */
77 #define SENSOR_SCALE(x) FIELD_GET(GENMASK(15, 11), (x))
78 #define SENSOR_SCALE_SIGN BIT(4)
79 #define SENSOR_SCALE_EXTEND GENMASK(31, 5)
80 #define SENSOR_TYPE(x) FIELD_GET(GENMASK(7, 0), (x))
81 u8 name[SCMI_MAX_STR_SIZE];
82 /* only for version > 2.0 */
85 struct scmi_msg_resp_attrs scalar_attrs;
89 /* Base scmi_sensor_descriptor size excluding extended attrs after name */
90 #define SCMI_MSG_RESP_SENS_DESCR_BASE_SZ 28
92 /* Sign extend to a full s32 */
97 if (__v & SENSOR_SCALE_SIGN) \
98 __v |= SENSOR_SCALE_EXTEND; \
102 struct scmi_msg_sensor_axis_description_get {
104 __le32 axis_desc_index;
107 struct scmi_msg_resp_sensor_axis_description {
108 __le32 num_axis_flags;
109 #define NUM_AXIS_RETURNED(x) FIELD_GET(GENMASK(5, 0), (x))
110 #define NUM_AXIS_REMAINING(x) FIELD_GET(GENMASK(31, 26), (x))
111 struct scmi_axis_descriptor {
113 __le32 attributes_low;
114 __le32 attributes_high;
115 u8 name[SCMI_MAX_STR_SIZE];
117 struct scmi_msg_resp_attrs attrs;
121 /* Base scmi_axis_descriptor size excluding extended attrs after name */
122 #define SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ 28
124 struct scmi_msg_sensor_list_update_intervals {
129 struct scmi_msg_resp_sensor_list_update_intervals {
130 __le32 num_intervals_flags;
131 #define NUM_INTERVALS_RETURNED(x) FIELD_GET(GENMASK(11, 0), (x))
132 #define SEGMENTED_INTVL_FORMAT(x) FIELD_GET(BIT(12), (x))
133 #define NUM_INTERVALS_REMAINING(x) FIELD_GET(GENMASK(31, 16), (x))
137 struct scmi_msg_sensor_request_notify {
139 __le32 event_control;
140 #define SENSOR_NOTIFY_ALL BIT(0)
143 struct scmi_msg_set_sensor_trip_point {
145 __le32 event_control;
146 #define SENSOR_TP_EVENT_MASK (0x3)
147 #define SENSOR_TP_DISABLED 0x0
148 #define SENSOR_TP_POSITIVE 0x1
149 #define SENSOR_TP_NEGATIVE 0x2
150 #define SENSOR_TP_BOTH 0x3
151 #define SENSOR_TP_ID(x) (((x) & 0xff) << 4)
156 struct scmi_msg_sensor_config_set {
158 __le32 sensor_config;
161 struct scmi_msg_sensor_reading_get {
164 #define SENSOR_READ_ASYNC BIT(0)
167 struct scmi_resp_sensor_reading_complete {
172 struct scmi_sensor_reading_resp {
173 __le32 sensor_value_low;
174 __le32 sensor_value_high;
175 __le32 timestamp_low;
176 __le32 timestamp_high;
179 struct scmi_resp_sensor_reading_complete_v3 {
181 struct scmi_sensor_reading_resp readings[];
184 struct scmi_sensor_trip_notify_payld {
187 __le32 trip_point_desc;
190 struct scmi_sensor_update_notify_payld {
193 struct scmi_sensor_reading_resp readings[];
196 struct sensors_info {
202 struct scmi_sensor_info *sensors;
205 static int scmi_sensor_attributes_get(const struct scmi_protocol_handle *ph,
206 struct sensors_info *si)
210 struct scmi_msg_resp_sensor_attributes *attr;
212 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
213 0, sizeof(*attr), &t);
219 ret = ph->xops->do_xfer(ph, t);
221 si->num_sensors = le16_to_cpu(attr->num_sensors);
222 si->max_requests = attr->max_requests;
223 si->reg_addr = le32_to_cpu(attr->reg_addr_low) |
224 (u64)le32_to_cpu(attr->reg_addr_high) << 32;
225 si->reg_size = le32_to_cpu(attr->reg_size);
228 ph->xops->xfer_put(ph, t);
232 static inline void scmi_parse_range_attrs(struct scmi_range_attrs *out,
233 struct scmi_msg_resp_attrs *in)
235 out->min_range = get_unaligned_le64((void *)&in->min_range_low);
236 out->max_range = get_unaligned_le64((void *)&in->max_range_low);
239 static int scmi_sensor_update_intervals(const struct scmi_protocol_handle *ph,
240 struct scmi_sensor_info *s)
244 u16 num_returned, num_remaining;
245 struct scmi_xfer *ti;
246 struct scmi_msg_resp_sensor_list_update_intervals *buf;
247 struct scmi_msg_sensor_list_update_intervals *msg;
249 ret = ph->xops->xfer_get_init(ph, SENSOR_LIST_UPDATE_INTERVALS,
250 sizeof(*msg), 0, &ti);
259 /* Set the number of sensors to be skipped/already read */
260 msg->id = cpu_to_le32(s->id);
261 msg->index = cpu_to_le32(desc_index);
263 ret = ph->xops->do_xfer(ph, ti);
267 flags = le32_to_cpu(buf->num_intervals_flags);
268 num_returned = NUM_INTERVALS_RETURNED(flags);
269 num_remaining = NUM_INTERVALS_REMAINING(flags);
272 * Max intervals is not declared previously anywhere so we
273 * assume it's returned+remaining.
275 if (!s->intervals.count) {
276 s->intervals.segmented = SEGMENTED_INTVL_FORMAT(flags);
277 s->intervals.count = num_returned + num_remaining;
278 /* segmented intervals are reported in one triplet */
279 if (s->intervals.segmented &&
280 (num_remaining || num_returned != 3)) {
282 "Sensor ID:%d advertises an invalid segmented interval (%d)\n",
283 s->id, s->intervals.count);
284 s->intervals.segmented = false;
285 s->intervals.count = 0;
289 /* Direct allocation when exceeding pre-allocated */
290 if (s->intervals.count >= SCMI_MAX_PREALLOC_POOL) {
292 devm_kcalloc(ph->dev,
294 sizeof(*s->intervals.desc),
296 if (!s->intervals.desc) {
297 s->intervals.segmented = false;
298 s->intervals.count = 0;
303 } else if (desc_index + num_returned > s->intervals.count) {
305 "No. of update intervals can't exceed %d\n",
311 for (cnt = 0; cnt < num_returned; cnt++)
312 s->intervals.desc[desc_index + cnt] =
313 le32_to_cpu(buf->intervals[cnt]);
315 desc_index += num_returned;
317 ph->xops->reset_rx_to_maxsz(ph, ti);
319 * check for both returned and remaining to avoid infinite
320 * loop due to buggy firmware
322 } while (num_returned && num_remaining);
324 ph->xops->xfer_put(ph, ti);
328 static int scmi_sensor_axis_description(const struct scmi_protocol_handle *ph,
329 struct scmi_sensor_info *s)
333 u16 num_returned, num_remaining;
334 struct scmi_xfer *te;
335 struct scmi_msg_resp_sensor_axis_description *buf;
336 struct scmi_msg_sensor_axis_description_get *msg;
338 s->axis = devm_kcalloc(ph->dev, s->num_axis,
339 sizeof(*s->axis), GFP_KERNEL);
343 ret = ph->xops->xfer_get_init(ph, SENSOR_AXIS_DESCRIPTION_GET,
344 sizeof(*msg), 0, &te);
351 struct scmi_axis_descriptor *adesc;
354 /* Set the number of sensors to be skipped/already read */
355 msg->id = cpu_to_le32(s->id);
356 msg->axis_desc_index = cpu_to_le32(desc_index);
358 ret = ph->xops->do_xfer(ph, te);
362 flags = le32_to_cpu(buf->num_axis_flags);
363 num_returned = NUM_AXIS_RETURNED(flags);
364 num_remaining = NUM_AXIS_REMAINING(flags);
366 if (desc_index + num_returned > s->num_axis) {
367 dev_err(ph->dev, "No. of axis can't exceed %d\n",
372 adesc = &buf->desc[0];
373 for (cnt = 0; cnt < num_returned; cnt++) {
375 struct scmi_sensor_axis_info *a;
376 size_t dsize = SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ;
378 attrl = le32_to_cpu(adesc->attributes_low);
380 a = &s->axis[desc_index + cnt];
382 a->id = le32_to_cpu(adesc->id);
383 a->extended_attrs = SUPPORTS_EXTEND_ATTRS(attrl);
385 attrh = le32_to_cpu(adesc->attributes_high);
386 a->scale = S32_EXT(SENSOR_SCALE(attrh));
387 a->type = SENSOR_TYPE(attrh);
388 strlcpy(a->name, adesc->name, SCMI_MAX_STR_SIZE);
390 if (a->extended_attrs) {
392 le32_to_cpu(adesc->resolution);
394 a->resolution = SENSOR_RES(ares);
396 S32_EXT(SENSOR_RES_EXP(ares));
397 dsize += sizeof(adesc->resolution);
399 scmi_parse_range_attrs(&a->attrs,
401 dsize += sizeof(adesc->attrs);
404 adesc = (typeof(adesc))((u8 *)adesc + dsize);
407 desc_index += num_returned;
409 ph->xops->reset_rx_to_maxsz(ph, te);
411 * check for both returned and remaining to avoid infinite
412 * loop due to buggy firmware
414 } while (num_returned && num_remaining);
416 ph->xops->xfer_put(ph, te);
420 static int scmi_sensor_description_get(const struct scmi_protocol_handle *ph,
421 struct sensors_info *si)
425 u16 num_returned, num_remaining;
427 struct scmi_msg_resp_sensor_description *buf;
429 ret = ph->xops->xfer_get_init(ph, SENSOR_DESCRIPTION_GET,
430 sizeof(__le32), 0, &t);
437 struct scmi_sensor_descriptor *sdesc;
439 /* Set the number of sensors to be skipped/already read */
440 put_unaligned_le32(desc_index, t->tx.buf);
442 ret = ph->xops->do_xfer(ph, t);
446 num_returned = le16_to_cpu(buf->num_returned);
447 num_remaining = le16_to_cpu(buf->num_remaining);
449 if (desc_index + num_returned > si->num_sensors) {
450 dev_err(ph->dev, "No. of sensors can't exceed %d",
455 sdesc = &buf->desc[0];
456 for (cnt = 0; cnt < num_returned; cnt++) {
458 struct scmi_sensor_info *s;
459 size_t dsize = SCMI_MSG_RESP_SENS_DESCR_BASE_SZ;
461 s = &si->sensors[desc_index + cnt];
462 s->id = le32_to_cpu(sdesc->id);
464 attrl = le32_to_cpu(sdesc->attributes_low);
465 /* common bitfields parsing */
466 s->async = SUPPORTS_ASYNC_READ(attrl);
467 s->num_trip_points = NUM_TRIP_POINTS(attrl);
469 * only SCMIv3.0 specific bitfield below.
470 * Such bitfields are assumed to be zeroed on non
471 * relevant fw versions...assuming fw not buggy !
473 s->update = SUPPORTS_UPDATE_NOTIFY(attrl);
474 s->timestamped = SUPPORTS_TIMESTAMP(attrl);
477 S32_EXT(SENSOR_TSTAMP_EXP(attrl));
478 s->extended_scalar_attrs =
479 SUPPORTS_EXTEND_ATTRS(attrl);
481 attrh = le32_to_cpu(sdesc->attributes_high);
482 /* common bitfields parsing */
483 s->scale = S32_EXT(SENSOR_SCALE(attrh));
484 s->type = SENSOR_TYPE(attrh);
485 /* Use pre-allocated pool wherever possible */
486 s->intervals.desc = s->intervals.prealloc_pool;
487 if (si->version == SCMIv2_SENSOR_PROTOCOL) {
488 s->intervals.segmented = false;
489 s->intervals.count = 1;
491 * Convert SCMIv2.0 update interval format to
492 * SCMIv3.0 to be used as the common exposed
493 * descriptor, accessible via common macros.
495 s->intervals.desc[0] =
496 (SENSOR_UPDATE_BASE(attrh) << 5) |
497 SENSOR_UPDATE_SCALE(attrh);
500 * From SCMIv3.0 update intervals are retrieved
501 * via a dedicated (optional) command.
502 * Since the command is optional, on error carry
503 * on without any update interval.
505 if (scmi_sensor_update_intervals(ph, s))
507 "Update Intervals not available for sensor ID:%d\n",
511 * only > SCMIv2.0 specific bitfield below.
512 * Such bitfields are assumed to be zeroed on non
513 * relevant fw versions...assuming fw not buggy !
515 s->num_axis = min_t(unsigned int,
516 SUPPORTS_AXIS(attrh) ?
517 SENSOR_AXIS_NUMBER(attrh) : 0,
518 SCMI_MAX_NUM_SENSOR_AXIS);
519 strlcpy(s->name, sdesc->name, SCMI_MAX_STR_SIZE);
521 if (s->extended_scalar_attrs) {
522 s->sensor_power = le32_to_cpu(sdesc->power);
523 dsize += sizeof(sdesc->power);
524 /* Only for sensors reporting scalar values */
525 if (s->num_axis == 0) {
527 le32_to_cpu(sdesc->resolution);
529 s->resolution = SENSOR_RES(sres);
531 S32_EXT(SENSOR_RES_EXP(sres));
532 dsize += sizeof(sdesc->resolution);
534 scmi_parse_range_attrs(&s->scalar_attrs,
535 &sdesc->scalar_attrs);
536 dsize += sizeof(sdesc->scalar_attrs);
539 if (s->num_axis > 0) {
540 ret = scmi_sensor_axis_description(ph, s);
545 sdesc = (typeof(sdesc))((u8 *)sdesc + dsize);
548 desc_index += num_returned;
550 ph->xops->reset_rx_to_maxsz(ph, t);
552 * check for both returned and remaining to avoid infinite
553 * loop due to buggy firmware
555 } while (num_returned && num_remaining);
558 ph->xops->xfer_put(ph, t);
563 scmi_sensor_request_notify(const struct scmi_protocol_handle *ph, u32 sensor_id,
564 u8 message_id, bool enable)
567 u32 evt_cntl = enable ? SENSOR_NOTIFY_ALL : 0;
569 struct scmi_msg_sensor_request_notify *cfg;
571 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*cfg), 0, &t);
576 cfg->id = cpu_to_le32(sensor_id);
577 cfg->event_control = cpu_to_le32(evt_cntl);
579 ret = ph->xops->do_xfer(ph, t);
581 ph->xops->xfer_put(ph, t);
585 static int scmi_sensor_trip_point_notify(const struct scmi_protocol_handle *ph,
586 u32 sensor_id, bool enable)
588 return scmi_sensor_request_notify(ph, sensor_id,
589 SENSOR_TRIP_POINT_NOTIFY,
594 scmi_sensor_continuous_update_notify(const struct scmi_protocol_handle *ph,
595 u32 sensor_id, bool enable)
597 return scmi_sensor_request_notify(ph, sensor_id,
598 SENSOR_CONTINUOUS_UPDATE_NOTIFY,
603 scmi_sensor_trip_point_config(const struct scmi_protocol_handle *ph,
604 u32 sensor_id, u8 trip_id, u64 trip_value)
607 u32 evt_cntl = SENSOR_TP_BOTH;
609 struct scmi_msg_set_sensor_trip_point *trip;
611 ret = ph->xops->xfer_get_init(ph, SENSOR_TRIP_POINT_CONFIG,
612 sizeof(*trip), 0, &t);
617 trip->id = cpu_to_le32(sensor_id);
618 trip->event_control = cpu_to_le32(evt_cntl | SENSOR_TP_ID(trip_id));
619 trip->value_low = cpu_to_le32(trip_value & 0xffffffff);
620 trip->value_high = cpu_to_le32(trip_value >> 32);
622 ret = ph->xops->do_xfer(ph, t);
624 ph->xops->xfer_put(ph, t);
628 static int scmi_sensor_config_get(const struct scmi_protocol_handle *ph,
629 u32 sensor_id, u32 *sensor_config)
634 ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_GET,
635 sizeof(__le32), sizeof(__le32), &t);
639 put_unaligned_le32(cpu_to_le32(sensor_id), t->tx.buf);
640 ret = ph->xops->do_xfer(ph, t);
642 struct sensors_info *si = ph->get_priv(ph);
643 struct scmi_sensor_info *s = si->sensors + sensor_id;
645 *sensor_config = get_unaligned_le64(t->rx.buf);
646 s->sensor_config = *sensor_config;
649 ph->xops->xfer_put(ph, t);
653 static int scmi_sensor_config_set(const struct scmi_protocol_handle *ph,
654 u32 sensor_id, u32 sensor_config)
658 struct scmi_msg_sensor_config_set *msg;
660 ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_SET,
661 sizeof(*msg), 0, &t);
666 msg->id = cpu_to_le32(sensor_id);
667 msg->sensor_config = cpu_to_le32(sensor_config);
669 ret = ph->xops->do_xfer(ph, t);
671 struct sensors_info *si = ph->get_priv(ph);
672 struct scmi_sensor_info *s = si->sensors + sensor_id;
674 s->sensor_config = sensor_config;
677 ph->xops->xfer_put(ph, t);
682 * scmi_sensor_reading_get - Read scalar sensor value
683 * @ph: Protocol handle
684 * @sensor_id: Sensor ID
685 * @value: The 64bit value sensor reading
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.
694 * Return: 0 on Success
696 static int scmi_sensor_reading_get(const struct scmi_protocol_handle *ph,
697 u32 sensor_id, u64 *value)
701 struct scmi_msg_sensor_reading_get *sensor;
702 struct sensors_info *si = ph->get_priv(ph);
703 struct scmi_sensor_info *s = si->sensors + sensor_id;
705 ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET,
706 sizeof(*sensor), 0, &t);
711 sensor->id = cpu_to_le32(sensor_id);
713 sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC);
714 ret = ph->xops->do_xfer_with_response(ph, t);
716 struct scmi_resp_sensor_reading_complete *resp;
719 if (le32_to_cpu(resp->id) == sensor_id)
720 *value = get_unaligned_le64(&resp->readings);
725 sensor->flags = cpu_to_le32(0);
726 ret = ph->xops->do_xfer(ph, t);
728 *value = get_unaligned_le64(t->rx.buf);
731 ph->xops->xfer_put(ph, t);
736 scmi_parse_sensor_readings(struct scmi_sensor_reading *out,
737 const struct scmi_sensor_reading_resp *in)
739 out->value = get_unaligned_le64((void *)&in->sensor_value_low);
740 out->timestamp = get_unaligned_le64((void *)&in->timestamp_low);
744 * scmi_sensor_reading_get_timestamped - Read multiple-axis timestamped values
745 * @ph: Protocol 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.
755 * Return: 0 on Success
758 scmi_sensor_reading_get_timestamped(const struct scmi_protocol_handle *ph,
759 u32 sensor_id, u8 count,
760 struct scmi_sensor_reading *readings)
764 struct scmi_msg_sensor_reading_get *sensor;
765 struct sensors_info *si = ph->get_priv(ph);
766 struct scmi_sensor_info *s = si->sensors + sensor_id;
768 if (!count || !readings ||
769 (!s->num_axis && count > 1) || (s->num_axis && count > s->num_axis))
772 ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET,
773 sizeof(*sensor), 0, &t);
778 sensor->id = cpu_to_le32(sensor_id);
780 sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC);
781 ret = ph->xops->do_xfer_with_response(ph, t);
784 struct scmi_resp_sensor_reading_complete_v3 *resp;
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],
796 sensor->flags = cpu_to_le32(0);
797 ret = ph->xops->do_xfer(ph, t);
800 struct scmi_sensor_reading_resp *resp_readings;
802 resp_readings = t->rx.buf;
803 for (i = 0; i < count; i++)
804 scmi_parse_sensor_readings(&readings[i],
809 ph->xops->xfer_put(ph, t);
813 static const struct scmi_sensor_info *
814 scmi_sensor_info_get(const struct scmi_protocol_handle *ph, u32 sensor_id)
816 struct sensors_info *si = ph->get_priv(ph);
818 return si->sensors + sensor_id;
821 static int scmi_sensor_count_get(const struct scmi_protocol_handle *ph)
823 struct sensors_info *si = ph->get_priv(ph);
825 return si->num_sensors;
828 static const struct scmi_sensor_proto_ops sensor_proto_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,
838 static int scmi_sensor_set_notify_enabled(const struct scmi_protocol_handle *ph,
839 u8 evt_id, u32 src_id, bool enable)
844 case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT:
845 ret = scmi_sensor_trip_point_notify(ph, src_id, enable);
847 case SCMI_EVENT_SENSOR_UPDATE:
848 ret = scmi_sensor_continuous_update_notify(ph, src_id, enable);
856 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
857 evt_id, src_id, ret);
863 scmi_sensor_fill_custom_report(const struct scmi_protocol_handle *ph,
864 u8 evt_id, ktime_t timestamp,
865 const void *payld, size_t payld_sz,
866 void *report, u32 *src_id)
871 case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT:
873 const struct scmi_sensor_trip_notify_payld *p = payld;
874 struct scmi_sensor_trip_point_report *r = report;
876 if (sizeof(*p) != payld_sz)
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;
887 case SCMI_EVENT_SENSOR_UPDATE:
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 = ph->get_priv(ph);
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)
899 r->timestamp = timestamp;
900 r->agent_id = le32_to_cpu(p->agent_id);
901 s = &sinfo->sensors[r->sensor_id];
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.
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],
912 *src_id = r->sensor_id;
923 static int scmi_sensor_get_num_sources(const struct scmi_protocol_handle *ph)
925 struct sensors_info *si = ph->get_priv(ph);
927 return si->num_sensors;
930 static const struct scmi_event sensor_events[] = {
932 .id = SCMI_EVENT_SENSOR_TRIP_POINT_EVENT,
933 .max_payld_sz = sizeof(struct scmi_sensor_trip_notify_payld),
934 .max_report_sz = sizeof(struct scmi_sensor_trip_point_report),
937 .id = SCMI_EVENT_SENSOR_UPDATE,
939 sizeof(struct scmi_sensor_update_notify_payld) +
940 SCMI_MAX_NUM_SENSOR_AXIS *
941 sizeof(struct scmi_sensor_reading_resp),
942 .max_report_sz = sizeof(struct scmi_sensor_update_report) +
943 SCMI_MAX_NUM_SENSOR_AXIS *
944 sizeof(struct scmi_sensor_reading),
948 static const struct scmi_event_ops sensor_event_ops = {
949 .get_num_sources = scmi_sensor_get_num_sources,
950 .set_notify_enabled = scmi_sensor_set_notify_enabled,
951 .fill_custom_report = scmi_sensor_fill_custom_report,
954 static const struct scmi_protocol_events sensor_protocol_events = {
955 .queue_sz = SCMI_PROTO_QUEUE_SZ,
956 .ops = &sensor_event_ops,
957 .evts = sensor_events,
958 .num_events = ARRAY_SIZE(sensor_events),
961 static int scmi_sensors_protocol_init(const struct scmi_protocol_handle *ph)
965 struct sensors_info *sinfo;
967 ph->xops->version_get(ph, &version);
969 dev_dbg(ph->dev, "Sensor Version %d.%d\n",
970 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
972 sinfo = devm_kzalloc(ph->dev, sizeof(*sinfo), GFP_KERNEL);
975 sinfo->version = version;
977 ret = scmi_sensor_attributes_get(ph, sinfo);
980 sinfo->sensors = devm_kcalloc(ph->dev, sinfo->num_sensors,
981 sizeof(*sinfo->sensors), GFP_KERNEL);
985 ret = scmi_sensor_description_get(ph, sinfo);
989 return ph->set_priv(ph, sinfo);
992 static const struct scmi_protocol scmi_sensors = {
993 .id = SCMI_PROTOCOL_SENSOR,
994 .owner = THIS_MODULE,
995 .instance_init = &scmi_sensors_protocol_init,
996 .ops = &sensor_proto_ops,
997 .events = &sensor_protocol_events,
1000 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(sensors, scmi_sensors)