blob: 931208bc48f122623209edf16c1230e4853b74e4 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * System Control and Management Interface (SCMI) Sensor Protocol
4 *
5 * Copyright (C) 2018 ARM Ltd.
6 */
7
8#include "common.h"
9
10enum scmi_sensor_protocol_cmd {
11 SENSOR_DESCRIPTION_GET = 0x3,
12 SENSOR_TRIP_POINT_NOTIFY = 0x4,
13 SENSOR_TRIP_POINT_CONFIG = 0x5,
14 SENSOR_READING_GET = 0x6,
15};
16
17struct scmi_msg_resp_sensor_attributes {
18 __le16 num_sensors;
19 u8 max_requests;
20 u8 reserved;
21 __le32 reg_addr_low;
22 __le32 reg_addr_high;
23 __le32 reg_size;
24};
25
26struct scmi_msg_resp_sensor_description {
27 __le16 num_returned;
28 __le16 num_remaining;
29 struct {
30 __le32 id;
31 __le32 attributes_low;
32#define SUPPORTS_ASYNC_READ(x) ((x) & BIT(31))
33#define NUM_TRIP_POINTS(x) ((x) & 0xff)
34 __le32 attributes_high;
35#define SENSOR_TYPE(x) ((x) & 0xff)
36#define SENSOR_SCALE(x) (((x) >> 11) & 0x1f)
37#define SENSOR_SCALE_SIGN BIT(4)
38#define SENSOR_SCALE_EXTEND GENMASK(7, 5)
39#define SENSOR_UPDATE_SCALE(x) (((x) >> 22) & 0x1f)
40#define SENSOR_UPDATE_BASE(x) (((x) >> 27) & 0x1f)
41 u8 name[SCMI_MAX_STR_SIZE];
42 } desc[0];
43};
44
45struct scmi_msg_sensor_trip_point_notify {
46 __le32 id;
47 __le32 event_control;
48#define SENSOR_TP_NOTIFY_ALL BIT(0)
49};
50
51struct scmi_msg_set_sensor_trip_point {
52 __le32 id;
53 __le32 event_control;
54#define SENSOR_TP_EVENT_MASK (0x3)
55#define SENSOR_TP_DISABLED 0x0
56#define SENSOR_TP_POSITIVE 0x1
57#define SENSOR_TP_NEGATIVE 0x2
58#define SENSOR_TP_BOTH 0x3
59#define SENSOR_TP_ID(x) (((x) & 0xff) << 4)
60 __le32 value_low;
61 __le32 value_high;
62};
63
64struct scmi_msg_sensor_reading_get {
65 __le32 id;
66 __le32 flags;
67#define SENSOR_READ_ASYNC BIT(0)
68};
69
70struct sensors_info {
71 int num_sensors;
72 int max_requests;
73 u64 reg_addr;
74 u32 reg_size;
75 struct scmi_sensor_info *sensors;
76};
77
78static int scmi_sensor_attributes_get(const struct scmi_handle *handle,
79 struct sensors_info *si)
80{
81 int ret;
82 struct scmi_xfer *t;
83 struct scmi_msg_resp_sensor_attributes *attr;
84
85 ret = scmi_xfer_get_init(handle, PROTOCOL_ATTRIBUTES,
86 SCMI_PROTOCOL_SENSOR, 0, sizeof(*attr), &t);
87 if (ret)
88 return ret;
89
90 attr = t->rx.buf;
91
92 ret = scmi_do_xfer(handle, t);
93 if (!ret) {
94 si->num_sensors = le16_to_cpu(attr->num_sensors);
95 si->max_requests = attr->max_requests;
96 si->reg_addr = le32_to_cpu(attr->reg_addr_low) |
97 (u64)le32_to_cpu(attr->reg_addr_high) << 32;
98 si->reg_size = le32_to_cpu(attr->reg_size);
99 }
100
101 scmi_xfer_put(handle, t);
102 return ret;
103}
104
105static int scmi_sensor_description_get(const struct scmi_handle *handle,
106 struct sensors_info *si)
107{
108 int ret, cnt;
109 u32 desc_index = 0;
110 u16 num_returned, num_remaining;
111 struct scmi_xfer *t;
112 struct scmi_msg_resp_sensor_description *buf;
113
114 ret = scmi_xfer_get_init(handle, SENSOR_DESCRIPTION_GET,
115 SCMI_PROTOCOL_SENSOR, sizeof(__le32), 0, &t);
116 if (ret)
117 return ret;
118
119 buf = t->rx.buf;
120
121 do {
122 /* Set the number of sensors to be skipped/already read */
123 put_unaligned_le32(desc_index, t->tx.buf);
124
125 ret = scmi_do_xfer(handle, t);
126 if (ret)
127 break;
128
129 num_returned = le16_to_cpu(buf->num_returned);
130 num_remaining = le16_to_cpu(buf->num_remaining);
131
132 if (desc_index + num_returned > si->num_sensors) {
133 dev_err(handle->dev, "No. of sensors can't exceed %d",
134 si->num_sensors);
135 break;
136 }
137
138 for (cnt = 0; cnt < num_returned; cnt++) {
139 u32 attrh, attrl;
140 struct scmi_sensor_info *s;
141
142 attrl = le32_to_cpu(buf->desc[cnt].attributes_low);
143 attrh = le32_to_cpu(buf->desc[cnt].attributes_high);
144 s = &si->sensors[desc_index + cnt];
145 s->id = le32_to_cpu(buf->desc[cnt].id);
146 s->type = SENSOR_TYPE(attrh);
147 s->scale = SENSOR_SCALE(attrh);
148 /* Sign extend to a full s8 */
149 if (s->scale & SENSOR_SCALE_SIGN)
150 s->scale |= SENSOR_SCALE_EXTEND;
151 s->async = SUPPORTS_ASYNC_READ(attrl);
152 s->num_trip_points = NUM_TRIP_POINTS(attrl);
153 strlcpy(s->name, buf->desc[cnt].name, SCMI_MAX_STR_SIZE);
154 }
155
156 desc_index += num_returned;
157
158 scmi_reset_rx_to_maxsz(handle, t);
159 /*
160 * check for both returned and remaining to avoid infinite
161 * loop due to buggy firmware
162 */
163 } while (num_returned && num_remaining);
164
165 scmi_xfer_put(handle, t);
166 return ret;
167}
168
169static int scmi_sensor_trip_point_notify(const struct scmi_handle *handle,
170 u32 sensor_id, bool enable)
171{
172 int ret;
173 u32 evt_cntl = enable ? SENSOR_TP_NOTIFY_ALL : 0;
174 struct scmi_xfer *t;
175 struct scmi_msg_sensor_trip_point_notify *cfg;
176
177 ret = scmi_xfer_get_init(handle, SENSOR_TRIP_POINT_NOTIFY,
178 SCMI_PROTOCOL_SENSOR, sizeof(*cfg), 0, &t);
179 if (ret)
180 return ret;
181
182 cfg = t->tx.buf;
183 cfg->id = cpu_to_le32(sensor_id);
184 cfg->event_control = cpu_to_le32(evt_cntl);
185
186 ret = scmi_do_xfer(handle, t);
187
188 scmi_xfer_put(handle, t);
189 return ret;
190}
191
192static int
193scmi_sensor_trip_point_config(const struct scmi_handle *handle, u32 sensor_id,
194 u8 trip_id, u64 trip_value)
195{
196 int ret;
197 u32 evt_cntl = SENSOR_TP_BOTH;
198 struct scmi_xfer *t;
199 struct scmi_msg_set_sensor_trip_point *trip;
200
201 ret = scmi_xfer_get_init(handle, SENSOR_TRIP_POINT_CONFIG,
202 SCMI_PROTOCOL_SENSOR, sizeof(*trip), 0, &t);
203 if (ret)
204 return ret;
205
206 trip = t->tx.buf;
207 trip->id = cpu_to_le32(sensor_id);
208 trip->event_control = cpu_to_le32(evt_cntl | SENSOR_TP_ID(trip_id));
209 trip->value_low = cpu_to_le32(trip_value & 0xffffffff);
210 trip->value_high = cpu_to_le32(trip_value >> 32);
211
212 ret = scmi_do_xfer(handle, t);
213
214 scmi_xfer_put(handle, t);
215 return ret;
216}
217
218static int scmi_sensor_reading_get(const struct scmi_handle *handle,
219 u32 sensor_id, u64 *value)
220{
221 int ret;
222 struct scmi_xfer *t;
223 struct scmi_msg_sensor_reading_get *sensor;
224 struct sensors_info *si = handle->sensor_priv;
225 struct scmi_sensor_info *s = si->sensors + sensor_id;
226
227 ret = scmi_xfer_get_init(handle, SENSOR_READING_GET,
228 SCMI_PROTOCOL_SENSOR, sizeof(*sensor),
229 sizeof(u64), &t);
230 if (ret)
231 return ret;
232
233 sensor = t->tx.buf;
234 sensor->id = cpu_to_le32(sensor_id);
235
236 if (s->async) {
237 sensor->flags = cpu_to_le32(SENSOR_READ_ASYNC);
238 ret = scmi_do_xfer_with_response(handle, t);
239 if (!ret)
240 *value = get_unaligned_le64((void *)
241 ((__le32 *)t->rx.buf + 1));
242 } else {
243 sensor->flags = cpu_to_le32(0);
244 ret = scmi_do_xfer(handle, t);
245 if (!ret)
246 *value = get_unaligned_le64(t->rx.buf);
247 }
248
249 scmi_xfer_put(handle, t);
250 return ret;
251}
252
253static const struct scmi_sensor_info *
254scmi_sensor_info_get(const struct scmi_handle *handle, u32 sensor_id)
255{
256 struct sensors_info *si = handle->sensor_priv;
257
258 return si->sensors + sensor_id;
259}
260
261static int scmi_sensor_count_get(const struct scmi_handle *handle)
262{
263 struct sensors_info *si = handle->sensor_priv;
264
265 return si->num_sensors;
266}
267
268static struct scmi_sensor_ops sensor_ops = {
269 .count_get = scmi_sensor_count_get,
270 .info_get = scmi_sensor_info_get,
271 .trip_point_notify = scmi_sensor_trip_point_notify,
272 .trip_point_config = scmi_sensor_trip_point_config,
273 .reading_get = scmi_sensor_reading_get,
274};
275
276static int scmi_sensors_protocol_init(struct scmi_handle *handle)
277{
278 u32 version;
279 struct sensors_info *sinfo;
280
281 scmi_version_get(handle, SCMI_PROTOCOL_SENSOR, &version);
282
283 dev_dbg(handle->dev, "Sensor Version %d.%d\n",
284 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
285
286 sinfo = devm_kzalloc(handle->dev, sizeof(*sinfo), GFP_KERNEL);
287 if (!sinfo)
288 return -ENOMEM;
289
290 scmi_sensor_attributes_get(handle, sinfo);
291
292 sinfo->sensors = devm_kcalloc(handle->dev, sinfo->num_sensors,
293 sizeof(*sinfo->sensors), GFP_KERNEL);
294 if (!sinfo->sensors)
295 return -ENOMEM;
296
297 scmi_sensor_description_get(handle, sinfo);
298
299 handle->sensor_ops = &sensor_ops;
300 handle->sensor_priv = sinfo;
301
302 return 0;
303}
304
305static int __init scmi_sensors_init(void)
306{
307 return scmi_protocol_register(SCMI_PROTOCOL_SENSOR,
308 &scmi_sensors_protocol_init);
309}
310subsys_initcall(scmi_sensors_init);