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