1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Sensor Protocol
5 * Copyright (C) 2018-2022 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>
14 #include "protocols.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,
30 SENSOR_NAME_GET = 0xC,
31 SENSOR_AXIS_NAME_GET = 0xD,
34 struct scmi_msg_resp_sensor_attributes {
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))
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))
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))
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))
61 struct scmi_msg_resp_attrs {
63 __le32 min_range_high;
65 __le32 max_range_high;
68 struct scmi_msg_sensor_description {
72 struct scmi_msg_resp_sensor_description {
75 struct scmi_sensor_descriptor {
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 */
92 struct scmi_msg_resp_attrs scalar_attrs;
96 /* Base scmi_sensor_descriptor size excluding extended attrs after name */
97 #define SCMI_MSG_RESP_SENS_DESCR_BASE_SZ 28
99 /* Sign extend to a full s32 */
104 if (__v & SENSOR_SCALE_SIGN) \
105 __v |= SENSOR_SCALE_EXTEND; \
109 struct scmi_msg_sensor_axis_description_get {
111 __le32 axis_desc_index;
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 {
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];
125 struct scmi_msg_resp_attrs attrs;
129 struct scmi_msg_resp_sensor_axis_names_description {
130 __le32 num_axis_flags;
131 struct scmi_sensor_axis_name_descriptor {
133 u8 name[SCMI_MAX_STR_SIZE];
137 /* Base scmi_axis_descriptor size excluding extended attrs after name */
138 #define SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ 28
140 struct scmi_msg_sensor_list_update_intervals {
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))
153 struct scmi_msg_sensor_request_notify {
155 __le32 event_control;
156 #define SENSOR_NOTIFY_ALL BIT(0)
159 struct scmi_msg_set_sensor_trip_point {
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)
172 struct scmi_msg_sensor_config_set {
174 __le32 sensor_config;
177 struct scmi_msg_sensor_reading_get {
180 #define SENSOR_READ_ASYNC BIT(0)
183 struct scmi_resp_sensor_reading_complete {
186 __le32 readings_high;
189 struct scmi_sensor_reading_resp {
190 __le32 sensor_value_low;
191 __le32 sensor_value_high;
192 __le32 timestamp_low;
193 __le32 timestamp_high;
196 struct scmi_resp_sensor_reading_complete_v3 {
198 struct scmi_sensor_reading_resp readings[];
201 struct scmi_sensor_trip_notify_payld {
204 __le32 trip_point_desc;
207 struct scmi_sensor_update_notify_payld {
210 struct scmi_sensor_reading_resp readings[];
213 struct sensors_info {
219 struct scmi_sensor_info *sensors;
222 static int scmi_sensor_attributes_get(const struct scmi_protocol_handle *ph,
223 struct sensors_info *si)
227 struct scmi_msg_resp_sensor_attributes *attr;
229 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
230 0, sizeof(*attr), &t);
236 ret = ph->xops->do_xfer(ph, t);
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);
245 ph->xops->xfer_put(ph, t);
249 static inline void scmi_parse_range_attrs(struct scmi_range_attrs *out,
250 const struct scmi_msg_resp_attrs *in)
252 out->min_range = get_unaligned_le64((void *)&in->min_range_low);
253 out->max_range = get_unaligned_le64((void *)&in->max_range_low);
256 struct scmi_sens_ipriv {
261 static void iter_intervals_prepare_message(void *message,
262 unsigned int desc_index,
265 struct scmi_msg_sensor_list_update_intervals *msg = message;
266 const struct scmi_sensor_info *s;
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);
274 static int iter_intervals_update_state(struct scmi_iterator_state *st,
275 const void *response, void *p)
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;
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);
287 * Max intervals is not declared previously anywhere so we
288 * assume it's returned+remaining on first call.
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)) {
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;
303 /* Direct allocation when exceeding pre-allocated */
304 if (s->intervals.count >= SCMI_MAX_PREALLOC_POOL) {
308 sizeof(*s->intervals.desc),
310 if (!s->intervals.desc) {
311 s->intervals.segmented = false;
312 s->intervals.count = 0;
317 st->max_resources = s->intervals.count;
324 iter_intervals_process_response(const struct scmi_protocol_handle *ph,
325 const void *response,
326 struct scmi_iterator_state *st, void *p)
328 const struct scmi_msg_resp_sensor_list_update_intervals *r = response;
329 struct scmi_sensor_info *s = ((struct scmi_sens_ipriv *)p)->priv;
331 s->intervals.desc[st->desc_index + st->loop_idx] =
332 le32_to_cpu(r->intervals[st->loop_idx]);
337 static int scmi_sensor_update_intervals(const struct scmi_protocol_handle *ph,
338 struct scmi_sensor_info *s)
341 struct scmi_iterator_ops ops = {
342 .prepare_message = iter_intervals_prepare_message,
343 .update_state = iter_intervals_update_state,
344 .process_response = iter_intervals_process_response,
346 struct scmi_sens_ipriv upriv = {
351 iter = ph->hops->iter_response_init(ph, &ops, s->intervals.count,
352 SENSOR_LIST_UPDATE_INTERVALS,
353 sizeof(struct scmi_msg_sensor_list_update_intervals),
356 return PTR_ERR(iter);
358 return ph->hops->iter_response_run(iter);
362 bool any_axes_support_extended_names;
363 struct scmi_sensor_info *s;
366 static void iter_axes_desc_prepare_message(void *message,
367 const unsigned int desc_index,
370 struct scmi_msg_sensor_axis_description_get *msg = message;
371 const struct scmi_apriv *apriv = priv;
373 /* Set the number of sensors to be skipped/already read */
374 msg->id = cpu_to_le32(apriv->s->id);
375 msg->axis_desc_index = cpu_to_le32(desc_index);
379 iter_axes_desc_update_state(struct scmi_iterator_state *st,
380 const void *response, void *priv)
383 const struct scmi_msg_resp_sensor_axis_description *r = response;
385 flags = le32_to_cpu(r->num_axis_flags);
386 st->num_returned = NUM_AXIS_RETURNED(flags);
387 st->num_remaining = NUM_AXIS_REMAINING(flags);
388 st->priv = (void *)&r->desc[0];
394 iter_axes_desc_process_response(const struct scmi_protocol_handle *ph,
395 const void *response,
396 struct scmi_iterator_state *st, void *priv)
399 struct scmi_sensor_axis_info *a;
400 size_t dsize = SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ;
401 struct scmi_apriv *apriv = priv;
402 const struct scmi_axis_descriptor *adesc = st->priv;
404 attrl = le32_to_cpu(adesc->attributes_low);
405 if (SUPPORTS_EXTENDED_AXIS_NAMES(attrl))
406 apriv->any_axes_support_extended_names = true;
408 a = &apriv->s->axis[st->desc_index + st->loop_idx];
409 a->id = le32_to_cpu(adesc->id);
410 a->extended_attrs = SUPPORTS_EXTEND_ATTRS(attrl);
412 attrh = le32_to_cpu(adesc->attributes_high);
413 a->scale = S32_EXT(SENSOR_SCALE(attrh));
414 a->type = SENSOR_TYPE(attrh);
415 strscpy(a->name, adesc->name, SCMI_SHORT_NAME_MAX_SIZE);
417 if (a->extended_attrs) {
418 unsigned int ares = le32_to_cpu(adesc->resolution);
420 a->resolution = SENSOR_RES(ares);
421 a->exponent = S32_EXT(SENSOR_RES_EXP(ares));
422 dsize += sizeof(adesc->resolution);
424 scmi_parse_range_attrs(&a->attrs, &adesc->attrs);
425 dsize += sizeof(adesc->attrs);
427 st->priv = ((u8 *)adesc + dsize);
433 iter_axes_extended_name_update_state(struct scmi_iterator_state *st,
434 const void *response, void *priv)
437 const struct scmi_msg_resp_sensor_axis_names_description *r = response;
439 flags = le32_to_cpu(r->num_axis_flags);
440 st->num_returned = NUM_AXIS_RETURNED(flags);
441 st->num_remaining = NUM_AXIS_REMAINING(flags);
442 st->priv = (void *)&r->desc[0];
448 iter_axes_extended_name_process_response(const struct scmi_protocol_handle *ph,
449 const void *response,
450 struct scmi_iterator_state *st,
453 struct scmi_sensor_axis_info *a;
454 const struct scmi_apriv *apriv = priv;
455 struct scmi_sensor_axis_name_descriptor *adesc = st->priv;
456 u32 axis_id = le32_to_cpu(adesc->axis_id);
458 if (axis_id >= st->max_resources)
462 * Pick the corresponding descriptor based on the axis_id embedded
463 * in the reply since the list of axes supporting extended names
464 * can be a subset of all the axes.
466 a = &apriv->s->axis[axis_id];
467 strscpy(a->name, adesc->name, SCMI_MAX_STR_SIZE);
474 scmi_sensor_axis_extended_names_get(const struct scmi_protocol_handle *ph,
475 struct scmi_sensor_info *s)
479 struct scmi_iterator_ops ops = {
480 .prepare_message = iter_axes_desc_prepare_message,
481 .update_state = iter_axes_extended_name_update_state,
482 .process_response = iter_axes_extended_name_process_response,
484 struct scmi_apriv apriv = {
485 .any_axes_support_extended_names = false,
489 iter = ph->hops->iter_response_init(ph, &ops, s->num_axis,
490 SENSOR_AXIS_NAME_GET,
491 sizeof(struct scmi_msg_sensor_axis_description_get),
494 return PTR_ERR(iter);
497 * Do not cause whole protocol initialization failure when failing to
498 * get extended names for axes.
500 ret = ph->hops->iter_response_run(iter);
503 "Failed to get axes extended names for %s (ret:%d).\n",
509 static int scmi_sensor_axis_description(const struct scmi_protocol_handle *ph,
510 struct scmi_sensor_info *s,
515 struct scmi_iterator_ops ops = {
516 .prepare_message = iter_axes_desc_prepare_message,
517 .update_state = iter_axes_desc_update_state,
518 .process_response = iter_axes_desc_process_response,
520 struct scmi_apriv apriv = {
521 .any_axes_support_extended_names = false,
525 s->axis = devm_kcalloc(ph->dev, s->num_axis,
526 sizeof(*s->axis), GFP_KERNEL);
530 iter = ph->hops->iter_response_init(ph, &ops, s->num_axis,
531 SENSOR_AXIS_DESCRIPTION_GET,
532 sizeof(struct scmi_msg_sensor_axis_description_get),
535 return PTR_ERR(iter);
537 ret = ph->hops->iter_response_run(iter);
541 if (PROTOCOL_REV_MAJOR(version) >= 0x3 &&
542 apriv.any_axes_support_extended_names)
543 ret = scmi_sensor_axis_extended_names_get(ph, s);
548 static void iter_sens_descr_prepare_message(void *message,
549 unsigned int desc_index,
552 struct scmi_msg_sensor_description *msg = message;
554 msg->desc_index = cpu_to_le32(desc_index);
557 static int iter_sens_descr_update_state(struct scmi_iterator_state *st,
558 const void *response, void *priv)
560 const struct scmi_msg_resp_sensor_description *r = response;
562 st->num_returned = le16_to_cpu(r->num_returned);
563 st->num_remaining = le16_to_cpu(r->num_remaining);
564 st->priv = (void *)&r->desc[0];
570 iter_sens_descr_process_response(const struct scmi_protocol_handle *ph,
571 const void *response,
572 struct scmi_iterator_state *st, void *priv)
577 size_t dsize = SCMI_MSG_RESP_SENS_DESCR_BASE_SZ;
578 struct scmi_sensor_info *s;
579 struct sensors_info *si = priv;
580 const struct scmi_sensor_descriptor *sdesc = st->priv;
582 s = &si->sensors[st->desc_index + st->loop_idx];
583 s->id = le32_to_cpu(sdesc->id);
585 attrl = le32_to_cpu(sdesc->attributes_low);
586 /* common bitfields parsing */
587 s->async = SUPPORTS_ASYNC_READ(attrl);
588 s->num_trip_points = NUM_TRIP_POINTS(attrl);
590 * only SCMIv3.0 specific bitfield below.
591 * Such bitfields are assumed to be zeroed on non
592 * relevant fw versions...assuming fw not buggy !
594 s->update = SUPPORTS_UPDATE_NOTIFY(attrl);
595 s->timestamped = SUPPORTS_TIMESTAMP(attrl);
597 s->tstamp_scale = S32_EXT(SENSOR_TSTAMP_EXP(attrl));
598 s->extended_scalar_attrs = SUPPORTS_EXTEND_ATTRS(attrl);
600 attrh = le32_to_cpu(sdesc->attributes_high);
601 /* common bitfields parsing */
602 s->scale = S32_EXT(SENSOR_SCALE(attrh));
603 s->type = SENSOR_TYPE(attrh);
604 /* Use pre-allocated pool wherever possible */
605 s->intervals.desc = s->intervals.prealloc_pool;
606 if (si->version == SCMIv2_SENSOR_PROTOCOL) {
607 s->intervals.segmented = false;
608 s->intervals.count = 1;
610 * Convert SCMIv2.0 update interval format to
611 * SCMIv3.0 to be used as the common exposed
612 * descriptor, accessible via common macros.
614 s->intervals.desc[0] = (SENSOR_UPDATE_BASE(attrh) << 5) |
615 SENSOR_UPDATE_SCALE(attrh);
618 * From SCMIv3.0 update intervals are retrieved
619 * via a dedicated (optional) command.
620 * Since the command is optional, on error carry
621 * on without any update interval.
623 if (scmi_sensor_update_intervals(ph, s))
625 "Update Intervals not available for sensor ID:%d\n",
629 * only > SCMIv2.0 specific bitfield below.
630 * Such bitfields are assumed to be zeroed on non
631 * relevant fw versions...assuming fw not buggy !
633 s->num_axis = min_t(unsigned int,
634 SUPPORTS_AXIS(attrh) ?
635 SENSOR_AXIS_NUMBER(attrh) : 0,
636 SCMI_MAX_NUM_SENSOR_AXIS);
637 strscpy(s->name, sdesc->name, SCMI_SHORT_NAME_MAX_SIZE);
640 * If supported overwrite short name with the extended
641 * one; on error just carry on and use already provided
644 if (PROTOCOL_REV_MAJOR(si->version) >= 0x3 &&
645 SUPPORTS_EXTENDED_NAMES(attrl))
646 ph->hops->extended_name_get(ph, SENSOR_NAME_GET, s->id,
647 s->name, SCMI_MAX_STR_SIZE);
649 if (s->extended_scalar_attrs) {
650 s->sensor_power = le32_to_cpu(sdesc->power);
651 dsize += sizeof(sdesc->power);
653 /* Only for sensors reporting scalar values */
654 if (s->num_axis == 0) {
655 unsigned int sres = le32_to_cpu(sdesc->resolution);
657 s->resolution = SENSOR_RES(sres);
658 s->exponent = S32_EXT(SENSOR_RES_EXP(sres));
659 dsize += sizeof(sdesc->resolution);
661 scmi_parse_range_attrs(&s->scalar_attrs,
662 &sdesc->scalar_attrs);
663 dsize += sizeof(sdesc->scalar_attrs);
668 ret = scmi_sensor_axis_description(ph, s, si->version);
670 st->priv = ((u8 *)sdesc + dsize);
675 static int scmi_sensor_description_get(const struct scmi_protocol_handle *ph,
676 struct sensors_info *si)
679 struct scmi_iterator_ops ops = {
680 .prepare_message = iter_sens_descr_prepare_message,
681 .update_state = iter_sens_descr_update_state,
682 .process_response = iter_sens_descr_process_response,
685 iter = ph->hops->iter_response_init(ph, &ops, si->num_sensors,
686 SENSOR_DESCRIPTION_GET,
689 return PTR_ERR(iter);
691 return ph->hops->iter_response_run(iter);
695 scmi_sensor_request_notify(const struct scmi_protocol_handle *ph, u32 sensor_id,
696 u8 message_id, bool enable)
699 u32 evt_cntl = enable ? SENSOR_NOTIFY_ALL : 0;
701 struct scmi_msg_sensor_request_notify *cfg;
703 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*cfg), 0, &t);
708 cfg->id = cpu_to_le32(sensor_id);
709 cfg->event_control = cpu_to_le32(evt_cntl);
711 ret = ph->xops->do_xfer(ph, t);
713 ph->xops->xfer_put(ph, t);
717 static int scmi_sensor_trip_point_notify(const struct scmi_protocol_handle *ph,
718 u32 sensor_id, bool enable)
720 return scmi_sensor_request_notify(ph, sensor_id,
721 SENSOR_TRIP_POINT_NOTIFY,
726 scmi_sensor_continuous_update_notify(const struct scmi_protocol_handle *ph,
727 u32 sensor_id, bool enable)
729 return scmi_sensor_request_notify(ph, sensor_id,
730 SENSOR_CONTINUOUS_UPDATE_NOTIFY,
735 scmi_sensor_trip_point_config(const struct scmi_protocol_handle *ph,
736 u32 sensor_id, u8 trip_id, u64 trip_value)
739 u32 evt_cntl = SENSOR_TP_BOTH;
741 struct scmi_msg_set_sensor_trip_point *trip;
743 ret = ph->xops->xfer_get_init(ph, SENSOR_TRIP_POINT_CONFIG,
744 sizeof(*trip), 0, &t);
749 trip->id = cpu_to_le32(sensor_id);
750 trip->event_control = cpu_to_le32(evt_cntl | SENSOR_TP_ID(trip_id));
751 trip->value_low = cpu_to_le32(trip_value & 0xffffffff);
752 trip->value_high = cpu_to_le32(trip_value >> 32);
754 ret = ph->xops->do_xfer(ph, t);
756 ph->xops->xfer_put(ph, t);
760 static int scmi_sensor_config_get(const struct scmi_protocol_handle *ph,
761 u32 sensor_id, u32 *sensor_config)
765 struct sensors_info *si = ph->get_priv(ph);
767 if (sensor_id >= si->num_sensors)
770 ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_GET,
771 sizeof(__le32), sizeof(__le32), &t);
775 put_unaligned_le32(sensor_id, t->tx.buf);
776 ret = ph->xops->do_xfer(ph, t);
778 struct scmi_sensor_info *s = si->sensors + sensor_id;
780 *sensor_config = get_unaligned_le64(t->rx.buf);
781 s->sensor_config = *sensor_config;
784 ph->xops->xfer_put(ph, t);
788 static int scmi_sensor_config_set(const struct scmi_protocol_handle *ph,
789 u32 sensor_id, u32 sensor_config)
793 struct scmi_msg_sensor_config_set *msg;
794 struct sensors_info *si = ph->get_priv(ph);
796 if (sensor_id >= si->num_sensors)
799 ret = ph->xops->xfer_get_init(ph, SENSOR_CONFIG_SET,
800 sizeof(*msg), 0, &t);
805 msg->id = cpu_to_le32(sensor_id);
806 msg->sensor_config = cpu_to_le32(sensor_config);
808 ret = ph->xops->do_xfer(ph, t);
810 struct scmi_sensor_info *s = si->sensors + sensor_id;
812 s->sensor_config = sensor_config;
815 ph->xops->xfer_put(ph, t);
820 * scmi_sensor_reading_get - Read scalar sensor value
821 * @ph: Protocol handle
822 * @sensor_id: Sensor ID
823 * @value: The 64bit value sensor reading
825 * This function returns a single 64 bit reading value representing the sensor
826 * value; if the platform SCMI Protocol implementation and the sensor support
827 * multiple axis and timestamped-reads, this just returns the first axis while
828 * dropping the timestamp value.
829 * Use instead the @scmi_sensor_reading_get_timestamped to retrieve the array of
830 * timestamped multi-axis values.
832 * Return: 0 on Success
834 static int scmi_sensor_reading_get(const struct scmi_protocol_handle *ph,
835 u32 sensor_id, u64 *value)
839 struct scmi_msg_sensor_reading_get *sensor;
840 struct scmi_sensor_info *s;
841 struct sensors_info *si = ph->get_priv(ph);
843 if (sensor_id >= si->num_sensors)
846 ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET,
847 sizeof(*sensor), 0, &t);
852 sensor->id = cpu_to_le32(sensor_id);
853 s = si->sensors + sensor_id;
855 sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC);
856 ret = ph->xops->do_xfer_with_response(ph, t);
858 struct scmi_resp_sensor_reading_complete *resp;
861 if (le32_to_cpu(resp->id) == sensor_id)
863 get_unaligned_le64(&resp->readings_low);
868 sensor->flags = cpu_to_le32(0);
869 ret = ph->xops->do_xfer(ph, t);
871 *value = get_unaligned_le64(t->rx.buf);
874 ph->xops->xfer_put(ph, t);
879 scmi_parse_sensor_readings(struct scmi_sensor_reading *out,
880 const struct scmi_sensor_reading_resp *in)
882 out->value = get_unaligned_le64((void *)&in->sensor_value_low);
883 out->timestamp = get_unaligned_le64((void *)&in->timestamp_low);
887 * scmi_sensor_reading_get_timestamped - Read multiple-axis timestamped values
888 * @ph: Protocol handle
889 * @sensor_id: Sensor ID
890 * @count: The length of the provided @readings array
891 * @readings: An array of elements each representing a timestamped per-axis
892 * reading of type @struct scmi_sensor_reading.
893 * Returned readings are ordered as the @axis descriptors array
894 * included in @struct scmi_sensor_info and the max number of
895 * returned elements is min(@count, @num_axis); ideally the provided
896 * array should be of length @count equal to @num_axis.
898 * Return: 0 on Success
901 scmi_sensor_reading_get_timestamped(const struct scmi_protocol_handle *ph,
902 u32 sensor_id, u8 count,
903 struct scmi_sensor_reading *readings)
907 struct scmi_msg_sensor_reading_get *sensor;
908 struct scmi_sensor_info *s;
909 struct sensors_info *si = ph->get_priv(ph);
911 if (sensor_id >= si->num_sensors)
914 s = si->sensors + sensor_id;
915 if (!count || !readings ||
916 (!s->num_axis && count > 1) || (s->num_axis && count > s->num_axis))
919 ret = ph->xops->xfer_get_init(ph, SENSOR_READING_GET,
920 sizeof(*sensor), 0, &t);
925 sensor->id = cpu_to_le32(sensor_id);
927 sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC);
928 ret = ph->xops->do_xfer_with_response(ph, t);
931 struct scmi_resp_sensor_reading_complete_v3 *resp;
934 /* Retrieve only the number of requested axis anyway */
935 if (le32_to_cpu(resp->id) == sensor_id)
936 for (i = 0; i < count; i++)
937 scmi_parse_sensor_readings(&readings[i],
943 sensor->flags = cpu_to_le32(0);
944 ret = ph->xops->do_xfer(ph, t);
947 struct scmi_sensor_reading_resp *resp_readings;
949 resp_readings = t->rx.buf;
950 for (i = 0; i < count; i++)
951 scmi_parse_sensor_readings(&readings[i],
956 ph->xops->xfer_put(ph, t);
960 static const struct scmi_sensor_info *
961 scmi_sensor_info_get(const struct scmi_protocol_handle *ph, u32 sensor_id)
963 struct sensors_info *si = ph->get_priv(ph);
965 if (sensor_id >= si->num_sensors)
968 return si->sensors + sensor_id;
971 static int scmi_sensor_count_get(const struct scmi_protocol_handle *ph)
973 struct sensors_info *si = ph->get_priv(ph);
975 return si->num_sensors;
978 static const struct scmi_sensor_proto_ops sensor_proto_ops = {
979 .count_get = scmi_sensor_count_get,
980 .info_get = scmi_sensor_info_get,
981 .trip_point_config = scmi_sensor_trip_point_config,
982 .reading_get = scmi_sensor_reading_get,
983 .reading_get_timestamped = scmi_sensor_reading_get_timestamped,
984 .config_get = scmi_sensor_config_get,
985 .config_set = scmi_sensor_config_set,
988 static int scmi_sensor_set_notify_enabled(const struct scmi_protocol_handle *ph,
989 u8 evt_id, u32 src_id, bool enable)
994 case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT:
995 ret = scmi_sensor_trip_point_notify(ph, src_id, enable);
997 case SCMI_EVENT_SENSOR_UPDATE:
998 ret = scmi_sensor_continuous_update_notify(ph, src_id, enable);
1006 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
1007 evt_id, src_id, ret);
1013 scmi_sensor_fill_custom_report(const struct scmi_protocol_handle *ph,
1014 u8 evt_id, ktime_t timestamp,
1015 const void *payld, size_t payld_sz,
1016 void *report, u32 *src_id)
1021 case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT:
1023 const struct scmi_sensor_trip_notify_payld *p = payld;
1024 struct scmi_sensor_trip_point_report *r = report;
1026 if (sizeof(*p) != payld_sz)
1029 r->timestamp = timestamp;
1030 r->agent_id = le32_to_cpu(p->agent_id);
1031 r->sensor_id = le32_to_cpu(p->sensor_id);
1032 r->trip_point_desc = le32_to_cpu(p->trip_point_desc);
1033 *src_id = r->sensor_id;
1037 case SCMI_EVENT_SENSOR_UPDATE:
1040 struct scmi_sensor_info *s;
1041 const struct scmi_sensor_update_notify_payld *p = payld;
1042 struct scmi_sensor_update_report *r = report;
1043 struct sensors_info *sinfo = ph->get_priv(ph);
1045 /* payld_sz is variable for this event */
1046 r->sensor_id = le32_to_cpu(p->sensor_id);
1047 if (r->sensor_id >= sinfo->num_sensors)
1049 r->timestamp = timestamp;
1050 r->agent_id = le32_to_cpu(p->agent_id);
1051 s = &sinfo->sensors[r->sensor_id];
1053 * The generated report r (@struct scmi_sensor_update_report)
1054 * was pre-allocated to contain up to SCMI_MAX_NUM_SENSOR_AXIS
1055 * readings: here it is filled with the effective @num_axis
1056 * readings defined for this sensor or 1 for scalar sensors.
1058 r->readings_count = s->num_axis ?: 1;
1059 for (i = 0; i < r->readings_count; i++)
1060 scmi_parse_sensor_readings(&r->readings[i],
1062 *src_id = r->sensor_id;
1073 static int scmi_sensor_get_num_sources(const struct scmi_protocol_handle *ph)
1075 struct sensors_info *si = ph->get_priv(ph);
1077 return si->num_sensors;
1080 static const struct scmi_event sensor_events[] = {
1082 .id = SCMI_EVENT_SENSOR_TRIP_POINT_EVENT,
1083 .max_payld_sz = sizeof(struct scmi_sensor_trip_notify_payld),
1084 .max_report_sz = sizeof(struct scmi_sensor_trip_point_report),
1087 .id = SCMI_EVENT_SENSOR_UPDATE,
1089 sizeof(struct scmi_sensor_update_notify_payld) +
1090 SCMI_MAX_NUM_SENSOR_AXIS *
1091 sizeof(struct scmi_sensor_reading_resp),
1092 .max_report_sz = sizeof(struct scmi_sensor_update_report) +
1093 SCMI_MAX_NUM_SENSOR_AXIS *
1094 sizeof(struct scmi_sensor_reading),
1098 static const struct scmi_event_ops sensor_event_ops = {
1099 .get_num_sources = scmi_sensor_get_num_sources,
1100 .set_notify_enabled = scmi_sensor_set_notify_enabled,
1101 .fill_custom_report = scmi_sensor_fill_custom_report,
1104 static const struct scmi_protocol_events sensor_protocol_events = {
1105 .queue_sz = SCMI_PROTO_QUEUE_SZ,
1106 .ops = &sensor_event_ops,
1107 .evts = sensor_events,
1108 .num_events = ARRAY_SIZE(sensor_events),
1111 static int scmi_sensors_protocol_init(const struct scmi_protocol_handle *ph)
1115 struct sensors_info *sinfo;
1117 ret = ph->xops->version_get(ph, &version);
1121 dev_dbg(ph->dev, "Sensor Version %d.%d\n",
1122 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
1124 sinfo = devm_kzalloc(ph->dev, sizeof(*sinfo), GFP_KERNEL);
1127 sinfo->version = version;
1129 ret = scmi_sensor_attributes_get(ph, sinfo);
1132 sinfo->sensors = devm_kcalloc(ph->dev, sinfo->num_sensors,
1133 sizeof(*sinfo->sensors), GFP_KERNEL);
1134 if (!sinfo->sensors)
1137 ret = scmi_sensor_description_get(ph, sinfo);
1141 return ph->set_priv(ph, sinfo);
1144 static const struct scmi_protocol scmi_sensors = {
1145 .id = SCMI_PROTOCOL_SENSOR,
1146 .owner = THIS_MODULE,
1147 .instance_init = &scmi_sensors_protocol_init,
1148 .ops = &sensor_proto_ops,
1149 .events = &sensor_protocol_events,
1152 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(sensors, scmi_sensors)