1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Sensor Protocol
5 * Copyright (C) 2018-2020 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications SENSOR - " fmt
10 #include <linux/bitfield.h>
11 #include <linux/scmi_protocol.h>
16 #define SCMI_MAX_NUM_SENSOR_AXIS 63
17 #define SCMIv2_SENSOR_PROTOCOL 0x10000
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,
31 struct scmi_msg_resp_sensor_attributes
{
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))
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))
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))
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))
58 struct scmi_msg_resp_attrs
{
60 __le32 min_range_high
;
62 __le32 max_range_high
;
65 struct scmi_msg_resp_sensor_description
{
68 struct scmi_sensor_descriptor
{
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 */
84 struct scmi_msg_resp_attrs scalar_attrs
;
88 /* Base scmi_sensor_descriptor size excluding extended attrs after name */
89 #define SCMI_MSG_RESP_SENS_DESCR_BASE_SZ 28
91 /* Sign extend to a full s32 */
96 if (__v & SENSOR_SCALE_SIGN) \
97 __v |= SENSOR_SCALE_EXTEND; \
101 struct scmi_msg_sensor_axis_description_get
{
103 __le32 axis_desc_index
;
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
{
112 __le32 attributes_low
;
113 __le32 attributes_high
;
114 u8 name
[SCMI_MAX_STR_SIZE
];
116 struct scmi_msg_resp_attrs attrs
;
120 /* Base scmi_axis_descriptor size excluding extended attrs after name */
121 #define SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ 28
123 struct scmi_msg_sensor_list_update_intervals
{
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))
136 struct scmi_msg_sensor_request_notify
{
138 __le32 event_control
;
139 #define SENSOR_NOTIFY_ALL BIT(0)
142 struct scmi_msg_set_sensor_trip_point
{
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)
155 struct scmi_msg_sensor_config_set
{
157 __le32 sensor_config
;
160 struct scmi_msg_sensor_reading_get
{
163 #define SENSOR_READ_ASYNC BIT(0)
166 struct scmi_resp_sensor_reading_complete
{
171 struct scmi_sensor_reading_resp
{
172 __le32 sensor_value_low
;
173 __le32 sensor_value_high
;
174 __le32 timestamp_low
;
175 __le32 timestamp_high
;
178 struct scmi_resp_sensor_reading_complete_v3
{
180 struct scmi_sensor_reading_resp readings
[];
183 struct scmi_sensor_trip_notify_payld
{
186 __le32 trip_point_desc
;
189 struct scmi_sensor_update_notify_payld
{
192 struct scmi_sensor_reading_resp readings
[];
195 struct sensors_info
{
201 struct scmi_sensor_info
*sensors
;
204 static int scmi_sensor_attributes_get(const struct scmi_handle
*handle
,
205 struct sensors_info
*si
)
209 struct scmi_msg_resp_sensor_attributes
*attr
;
211 ret
= scmi_xfer_get_init(handle
, PROTOCOL_ATTRIBUTES
,
212 SCMI_PROTOCOL_SENSOR
, 0, sizeof(*attr
), &t
);
218 ret
= scmi_do_xfer(handle
, t
);
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
);
227 scmi_xfer_put(handle
, t
);
231 static inline void scmi_parse_range_attrs(struct scmi_range_attrs
*out
,
232 struct scmi_msg_resp_attrs
*in
)
234 out
->min_range
= get_unaligned_le64((void *)&in
->min_range_low
);
235 out
->max_range
= get_unaligned_le64((void *)&in
->max_range_low
);
238 static int scmi_sensor_update_intervals(const struct scmi_handle
*handle
,
239 struct scmi_sensor_info
*s
)
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
;
248 ret
= scmi_xfer_get_init(handle
, SENSOR_LIST_UPDATE_INTERVALS
,
249 SCMI_PROTOCOL_SENSOR
, sizeof(*msg
), 0, &ti
);
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
);
262 ret
= scmi_do_xfer(handle
, ti
);
266 flags
= le32_to_cpu(buf
->num_intervals_flags
);
267 num_returned
= NUM_INTERVALS_RETURNED(flags
);
268 num_remaining
= NUM_INTERVALS_REMAINING(flags
);
271 * Max intervals is not declared previously anywhere so we
272 * assume it's returned+remaining.
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)) {
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;
288 /* Direct allocation when exceeding pre-allocated */
289 if (s
->intervals
.count
>= SCMI_MAX_PREALLOC_POOL
) {
291 devm_kcalloc(handle
->dev
,
293 sizeof(*s
->intervals
.desc
),
295 if (!s
->intervals
.desc
) {
296 s
->intervals
.segmented
= false;
297 s
->intervals
.count
= 0;
302 } else if (desc_index
+ num_returned
> s
->intervals
.count
) {
304 "No. of update intervals can't exceed %d\n",
310 for (cnt
= 0; cnt
< num_returned
; cnt
++)
311 s
->intervals
.desc
[desc_index
+ cnt
] =
312 le32_to_cpu(buf
->intervals
[cnt
]);
314 desc_index
+= num_returned
;
316 scmi_reset_rx_to_maxsz(handle
, ti
);
318 * check for both returned and remaining to avoid infinite
319 * loop due to buggy firmware
321 } while (num_returned
&& num_remaining
);
323 scmi_xfer_put(handle
, ti
);
327 static int scmi_sensor_axis_description(const struct scmi_handle
*handle
,
328 struct scmi_sensor_info
*s
)
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
;
337 s
->axis
= devm_kcalloc(handle
->dev
, s
->num_axis
,
338 sizeof(*s
->axis
), GFP_KERNEL
);
342 ret
= scmi_xfer_get_init(handle
, SENSOR_AXIS_DESCRIPTION_GET
,
343 SCMI_PROTOCOL_SENSOR
, sizeof(*msg
), 0, &te
);
350 struct scmi_axis_descriptor
*adesc
;
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
);
357 ret
= scmi_do_xfer(handle
, te
);
361 flags
= le32_to_cpu(buf
->num_axis_flags
);
362 num_returned
= NUM_AXIS_RETURNED(flags
);
363 num_remaining
= NUM_AXIS_REMAINING(flags
);
365 if (desc_index
+ num_returned
> s
->num_axis
) {
366 dev_err(handle
->dev
, "No. of axis can't exceed %d\n",
371 adesc
= &buf
->desc
[0];
372 for (cnt
= 0; cnt
< num_returned
; cnt
++) {
374 struct scmi_sensor_axis_info
*a
;
375 size_t dsize
= SCMI_MSG_RESP_AXIS_DESCR_BASE_SZ
;
377 attrl
= le32_to_cpu(adesc
->attributes_low
);
379 a
= &s
->axis
[desc_index
+ cnt
];
381 a
->id
= le32_to_cpu(adesc
->id
);
382 a
->extended_attrs
= SUPPORTS_EXTEND_ATTRS(attrl
);
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
);
389 if (a
->extended_attrs
) {
391 le32_to_cpu(adesc
->resolution
);
393 a
->resolution
= SENSOR_RES(ares
);
395 S32_EXT(SENSOR_RES_EXP(ares
));
396 dsize
+= sizeof(adesc
->resolution
);
398 scmi_parse_range_attrs(&a
->attrs
,
400 dsize
+= sizeof(adesc
->attrs
);
403 adesc
= (typeof(adesc
))((u8
*)adesc
+ dsize
);
406 desc_index
+= num_returned
;
408 scmi_reset_rx_to_maxsz(handle
, te
);
410 * check for both returned and remaining to avoid infinite
411 * loop due to buggy firmware
413 } while (num_returned
&& num_remaining
);
415 scmi_xfer_put(handle
, te
);
419 static int scmi_sensor_description_get(const struct scmi_handle
*handle
,
420 struct sensors_info
*si
)
424 u16 num_returned
, num_remaining
;
426 struct scmi_msg_resp_sensor_description
*buf
;
428 ret
= scmi_xfer_get_init(handle
, SENSOR_DESCRIPTION_GET
,
429 SCMI_PROTOCOL_SENSOR
, sizeof(__le32
), 0, &t
);
436 struct scmi_sensor_descriptor
*sdesc
;
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
);
444 num_returned
= le16_to_cpu(buf
->num_returned
);
445 num_remaining
= le16_to_cpu(buf
->num_remaining
);
447 if (desc_index
+ num_returned
> si
->num_sensors
) {
448 dev_err(handle
->dev
, "No. of sensors can't exceed %d",
453 sdesc
= &buf
->desc
[0];
454 for (cnt
= 0; cnt
< num_returned
; cnt
++) {
456 struct scmi_sensor_info
*s
;
457 size_t dsize
= SCMI_MSG_RESP_SENS_DESCR_BASE_SZ
;
459 s
= &si
->sensors
[desc_index
+ cnt
];
460 s
->id
= le32_to_cpu(sdesc
->id
);
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
);
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 !
471 s
->update
= SUPPORTS_UPDATE_NOTIFY(attrl
);
472 s
->timestamped
= SUPPORTS_TIMESTAMP(attrl
);
475 S32_EXT(SENSOR_TSTAMP_EXP(attrl
));
476 s
->extended_scalar_attrs
=
477 SUPPORTS_EXTEND_ATTRS(attrl
);
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;
489 * Convert SCMIv2.0 update interval format to
490 * SCMIv3.0 to be used as the common exposed
491 * descriptor, accessible via common macros.
493 s
->intervals
.desc
[0] =
494 (SENSOR_UPDATE_BASE(attrh
) << 5) |
495 SENSOR_UPDATE_SCALE(attrh
);
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.
503 if (scmi_sensor_update_intervals(handle
, s
))
505 "Update Intervals not available for sensor ID:%d\n",
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 !
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
);
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) {
525 le32_to_cpu(sdesc
->resolution
);
527 s
->resolution
= SENSOR_RES(sres
);
529 S32_EXT(SENSOR_RES_EXP(sres
));
530 dsize
+= sizeof(sdesc
->resolution
);
532 scmi_parse_range_attrs(&s
->scalar_attrs
,
533 &sdesc
->scalar_attrs
);
534 dsize
+= sizeof(sdesc
->scalar_attrs
);
537 if (s
->num_axis
> 0) {
538 ret
= scmi_sensor_axis_description(handle
, s
);
543 sdesc
= (typeof(sdesc
))((u8
*)sdesc
+ dsize
);
546 desc_index
+= num_returned
;
548 scmi_reset_rx_to_maxsz(handle
, t
);
550 * check for both returned and remaining to avoid infinite
551 * loop due to buggy firmware
553 } while (num_returned
&& num_remaining
);
556 scmi_xfer_put(handle
, t
);
561 scmi_sensor_request_notify(const struct scmi_handle
*handle
, u32 sensor_id
,
562 u8 message_id
, bool enable
)
565 u32 evt_cntl
= enable
? SENSOR_NOTIFY_ALL
: 0;
567 struct scmi_msg_sensor_request_notify
*cfg
;
569 ret
= scmi_xfer_get_init(handle
, message_id
,
570 SCMI_PROTOCOL_SENSOR
, sizeof(*cfg
), 0, &t
);
575 cfg
->id
= cpu_to_le32(sensor_id
);
576 cfg
->event_control
= cpu_to_le32(evt_cntl
);
578 ret
= scmi_do_xfer(handle
, t
);
580 scmi_xfer_put(handle
, t
);
584 static int scmi_sensor_trip_point_notify(const struct scmi_handle
*handle
,
585 u32 sensor_id
, bool enable
)
587 return scmi_sensor_request_notify(handle
, sensor_id
,
588 SENSOR_TRIP_POINT_NOTIFY
,
593 scmi_sensor_continuous_update_notify(const struct scmi_handle
*handle
,
594 u32 sensor_id
, bool enable
)
596 return scmi_sensor_request_notify(handle
, sensor_id
,
597 SENSOR_CONTINUOUS_UPDATE_NOTIFY
,
602 scmi_sensor_trip_point_config(const struct scmi_handle
*handle
, u32 sensor_id
,
603 u8 trip_id
, u64 trip_value
)
606 u32 evt_cntl
= SENSOR_TP_BOTH
;
608 struct scmi_msg_set_sensor_trip_point
*trip
;
610 ret
= scmi_xfer_get_init(handle
, SENSOR_TRIP_POINT_CONFIG
,
611 SCMI_PROTOCOL_SENSOR
, sizeof(*trip
), 0, &t
);
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);
621 ret
= scmi_do_xfer(handle
, t
);
623 scmi_xfer_put(handle
, t
);
627 static int scmi_sensor_config_get(const struct scmi_handle
*handle
,
628 u32 sensor_id
, u32
*sensor_config
)
633 ret
= scmi_xfer_get_init(handle
, SENSOR_CONFIG_GET
,
634 SCMI_PROTOCOL_SENSOR
, sizeof(__le32
),
639 put_unaligned_le32(cpu_to_le32(sensor_id
), t
->tx
.buf
);
640 ret
= scmi_do_xfer(handle
, t
);
642 struct sensors_info
*si
= handle
->sensor_priv
;
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 scmi_xfer_put(handle
, t
);
653 static int scmi_sensor_config_set(const struct scmi_handle
*handle
,
654 u32 sensor_id
, u32 sensor_config
)
658 struct scmi_msg_sensor_config_set
*msg
;
660 ret
= scmi_xfer_get_init(handle
, SENSOR_CONFIG_SET
,
661 SCMI_PROTOCOL_SENSOR
, sizeof(*msg
), 0, &t
);
666 msg
->id
= cpu_to_le32(sensor_id
);
667 msg
->sensor_config
= cpu_to_le32(sensor_config
);
669 ret
= scmi_do_xfer(handle
, t
);
671 struct sensors_info
*si
= handle
->sensor_priv
;
672 struct scmi_sensor_info
*s
= si
->sensors
+ sensor_id
;
674 s
->sensor_config
= sensor_config
;
677 scmi_xfer_put(handle
, t
);
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
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_handle
*handle
,
697 u32 sensor_id
, u64
*value
)
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
;
705 ret
= scmi_xfer_get_init(handle
, SENSOR_READING_GET
,
706 SCMI_PROTOCOL_SENSOR
, sizeof(*sensor
), 0, &t
);
711 sensor
->id
= cpu_to_le32(sensor_id
);
713 sensor
->flags
= cpu_to_le32(SENSOR_READ_ASYNC
);
714 ret
= scmi_do_xfer_with_response(handle
, 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
= scmi_do_xfer(handle
, t
);
728 *value
= get_unaligned_le64(t
->rx
.buf
);
731 scmi_xfer_put(handle
, 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 * @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.
755 * Return: 0 on Success
758 scmi_sensor_reading_get_timestamped(const struct scmi_handle
*handle
,
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
= handle
->sensor_priv
;
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
= scmi_xfer_get_init(handle
, SENSOR_READING_GET
,
773 SCMI_PROTOCOL_SENSOR
, sizeof(*sensor
), 0, &t
);
778 sensor
->id
= cpu_to_le32(sensor_id
);
780 sensor
->flags
= cpu_to_le32(SENSOR_READ_ASYNC
);
781 ret
= scmi_do_xfer_with_response(handle
, 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
= scmi_do_xfer(handle
, 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 scmi_xfer_put(handle
, t
);
813 static const struct scmi_sensor_info
*
814 scmi_sensor_info_get(const struct scmi_handle
*handle
, u32 sensor_id
)
816 struct sensors_info
*si
= handle
->sensor_priv
;
818 return si
->sensors
+ sensor_id
;
821 static int scmi_sensor_count_get(const struct scmi_handle
*handle
)
823 struct sensors_info
*si
= handle
->sensor_priv
;
825 return si
->num_sensors
;
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
,
838 static int scmi_sensor_set_notify_enabled(const struct scmi_handle
*handle
,
839 u8 evt_id
, u32 src_id
, bool enable
)
844 case SCMI_EVENT_SENSOR_TRIP_POINT_EVENT
:
845 ret
= scmi_sensor_trip_point_notify(handle
, src_id
, enable
);
847 case SCMI_EVENT_SENSOR_UPDATE
:
848 ret
= scmi_sensor_continuous_update_notify(handle
, src_id
,
857 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
858 evt_id
, src_id
, ret
);
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
)
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
= handle
->sensor_priv
;
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 const struct scmi_event sensor_events
[] = {
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
),
930 .id
= SCMI_EVENT_SENSOR_UPDATE
,
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
),
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
,
946 static int scmi_sensors_protocol_init(struct scmi_handle
*handle
)
950 struct sensors_info
*sinfo
;
952 scmi_version_get(handle
, SCMI_PROTOCOL_SENSOR
, &version
);
954 dev_dbg(handle
->dev
, "Sensor Version %d.%d\n",
955 PROTOCOL_REV_MAJOR(version
), PROTOCOL_REV_MINOR(version
));
957 sinfo
= devm_kzalloc(handle
->dev
, sizeof(*sinfo
), GFP_KERNEL
);
960 sinfo
->version
= version
;
962 ret
= scmi_sensor_attributes_get(handle
, sinfo
);
965 sinfo
->sensors
= devm_kcalloc(handle
->dev
, sinfo
->num_sensors
,
966 sizeof(*sinfo
->sensors
), GFP_KERNEL
);
970 ret
= scmi_sensor_description_get(handle
, sinfo
);
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
),
980 handle
->sensor_priv
= sinfo
;
981 handle
->sensor_ops
= &sensor_ops
;
986 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(SCMI_PROTOCOL_SENSOR
, sensors
)