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