1 /*
2  * Copyright (c) 2023 Google LLC.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 
9 #include <zephyr/drivers/sensor.h>
10 #include <zephyr/dsp/types.h>
11 #include <zephyr/logging/log.h>
12 
13 LOG_MODULE_REGISTER(sensor_compat, CONFIG_SENSOR_LOG_LEVEL);
14 
15 /*
16  * Ensure that the size of the generic header aligns with the sensor channel enum. If it doesn't,
17  * then cores that require aligned memory access will fail to read channel[0].
18  */
19 BUILD_ASSERT((sizeof(struct sensor_data_generic_header) % sizeof(enum sensor_channel)) == 0);
20 
21 static void sensor_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe);
22 
sensor_iodev_submit(struct rtio_iodev_sqe * iodev_sqe)23 static void sensor_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
24 {
25 	const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data;
26 	const struct device *dev = cfg->sensor;
27 	const struct sensor_driver_api *api = dev->api;
28 
29 	if (api->submit != NULL) {
30 		api->submit(dev, iodev_sqe);
31 	} else {
32 		sensor_submit_fallback(dev, iodev_sqe);
33 	}
34 }
35 
36 const struct rtio_iodev_api __sensor_iodev_api = {
37 	.submit = sensor_iodev_submit,
38 };
39 
40 /**
41  * @brief Compute the number of samples needed for the given channels
42  *
43  * @param[in] channels Array of channels requested
44  * @param[in] num_channels Number of channels on the @p channels array
45  * @return The number of samples required to read the given channels
46  */
compute_num_samples(const enum sensor_channel * channels,size_t num_channels)47 static inline int compute_num_samples(const enum sensor_channel *channels, size_t num_channels)
48 {
49 	int num_samples = 0;
50 
51 	for (size_t i = 0; i < num_channels; ++i) {
52 		num_samples += SENSOR_CHANNEL_3_AXIS(channels[i]) ? 3 : 1;
53 	}
54 
55 	return num_samples;
56 }
57 
58 /**
59  * @brief Compute the required header size
60  *
61  * This function takes into account alignment of the q31 values that will follow the header.
62  *
63  * @param[in] num_output_samples The number of samples to represent
64  * @return The number of bytes needed for this sample frame's header
65  */
compute_header_size(int num_output_samples)66 static inline uint32_t compute_header_size(int num_output_samples)
67 {
68 	uint32_t size = sizeof(struct sensor_data_generic_header) +
69 			(num_output_samples * sizeof(enum sensor_channel));
70 	return (size + 3) & ~0x3;
71 }
72 
73 /**
74  * @brief Compute the minimum number of bytes needed
75  *
76  * @param[in] num_output_samples The number of samples to represent
77  * @return The number of bytes needed for this sample frame
78  */
compute_min_buf_len(int num_output_samples)79 static inline uint32_t compute_min_buf_len(int num_output_samples)
80 {
81 	return compute_header_size(num_output_samples) + (num_output_samples * sizeof(q31_t));
82 }
83 
84 /**
85  * @brief Checks if the header already contains a given channel
86  *
87  * @param[in] header The header to scan
88  * @param[in] channel The channel to search for
89  * @param[in] num_channels The number of valid channels in the header so far
90  * @return Index of the @p channel if found or negative if not found
91  */
check_header_contains_channel(const struct sensor_data_generic_header * header,enum sensor_channel channel,int num_channels)92 static inline int check_header_contains_channel(const struct sensor_data_generic_header *header,
93 						enum sensor_channel channel, int num_channels)
94 {
95 	__ASSERT_NO_MSG(!SENSOR_CHANNEL_3_AXIS(channel));
96 
97 	for (int i = 0; i < num_channels; ++i) {
98 		if (header->channels[i] == channel) {
99 			return i;
100 		}
101 	}
102 	return -1;
103 }
104 
105 /**
106  * @brief Fallback function for retrofiting old drivers to rtio
107  *
108  * @param[in] dev The sensor device to read
109  * @param[in] iodev_sqe The read submission queue event
110  */
sensor_submit_fallback(const struct device * dev,struct rtio_iodev_sqe * iodev_sqe)111 static void sensor_submit_fallback(const struct device *dev, struct rtio_iodev_sqe *iodev_sqe)
112 {
113 	const struct sensor_read_config *cfg = iodev_sqe->sqe.iodev->data;
114 	const enum sensor_channel *const channels = cfg->channels;
115 	const int num_output_samples = compute_num_samples(channels, cfg->count);
116 	uint32_t min_buf_len = compute_min_buf_len(num_output_samples);
117 	uint64_t timestamp_ns = k_ticks_to_ns_floor64(k_uptime_ticks());
118 	int rc = sensor_sample_fetch(dev);
119 	uint8_t *buf;
120 	uint32_t buf_len;
121 
122 	/* Check that the fetch succeeded */
123 	if (rc != 0) {
124 		LOG_WRN("Failed to fetch samples");
125 		rtio_iodev_sqe_err(iodev_sqe, rc);
126 		return;
127 	}
128 
129 	/* Get the buffer for the frame, it may be allocated dynamically by the rtio context */
130 	rc = rtio_sqe_rx_buf(iodev_sqe, min_buf_len, min_buf_len, &buf, &buf_len);
131 	if (rc != 0) {
132 		LOG_WRN("Failed to get a read buffer of size %u bytes", min_buf_len);
133 		rtio_iodev_sqe_err(iodev_sqe, rc);
134 		return;
135 	}
136 
137 	/* Set the timestamp and num_channels */
138 	struct sensor_data_generic_header *header = (struct sensor_data_generic_header *)buf;
139 
140 	header->timestamp_ns = timestamp_ns;
141 	header->num_channels = num_output_samples;
142 	header->shift = 0;
143 
144 	q31_t *q = (q31_t *)(buf + compute_header_size(num_output_samples));
145 
146 	/* Populate values, update shift, and set channels */
147 	for (size_t i = 0, sample_idx = 0; i < cfg->count; ++i) {
148 		struct sensor_value value[3];
149 		const int num_samples = SENSOR_CHANNEL_3_AXIS(channels[i]) ? 3 : 1;
150 
151 		/* Get the current channel requested by the user */
152 		rc = sensor_channel_get(dev, channels[i], value);
153 
154 		if (num_samples == 3) {
155 			header->channels[sample_idx++] =
156 				rc == 0 ? channels[i] - 3 : SENSOR_CHAN_MAX;
157 			header->channels[sample_idx++] =
158 				rc == 0 ? channels[i] - 2 : SENSOR_CHAN_MAX;
159 			header->channels[sample_idx++] =
160 				rc == 0 ? channels[i] - 1 : SENSOR_CHAN_MAX;
161 		} else {
162 			header->channels[sample_idx++] = rc == 0 ? channels[i] : SENSOR_CHAN_MAX;
163 		}
164 
165 		if (rc != 0) {
166 			LOG_DBG("Failed to get channel %d, skipping", channels[i]);
167 			continue;
168 		}
169 
170 		/* Get the largest absolute value reading to set the scale for the channel */
171 		uint32_t header_scale = 0;
172 
173 		for (int sample = 0; sample < num_samples; ++sample) {
174 			/*
175 			 * The scale is the ceil(abs(sample)).
176 			 * Since we are using fractional values, it's easier to assume that .val2
177 			 *   is non 0 and convert this to abs(sample.val1) + 1 (removing a branch).
178 			 * Since it's possible that val1 (int32_t) is saturated (INT32_MAX) we need
179 			 *   to upcast it to 64 bit int first, then take the abs() of that 64 bit
180 			 *   int before we '+ 1'. Once that's done, we can safely cast back down
181 			 *   to uint32_t because the min value is 0 and max is INT32_MAX + 1 which
182 			 *   is less than UINT32_MAX.
183 			 */
184 			uint32_t scale = (uint32_t)llabs((int64_t)value[sample].val1) + 1;
185 
186 			header_scale = MAX(header_scale, scale);
187 		}
188 
189 		int8_t new_shift = ilog2(header_scale - 1) + 1;
190 
191 		/* Reset sample_idx */
192 		sample_idx -= num_samples;
193 		if (header->shift < new_shift) {
194 			/*
195 			 * Shift was updated, need to convert all the existing q values. This could
196 			 * be optimized by calling zdsp_scale_q31() but that would force a
197 			 * dependency between sensors and the zDSP subsystem.
198 			 */
199 			for (int q_idx = 0; q_idx < sample_idx; ++q_idx) {
200 				q[q_idx] = q[q_idx] >> (new_shift - header->shift);
201 			}
202 			header->shift = new_shift;
203 		}
204 
205 		/*
206 		 * Spread the q31 values. This is needed because some channels are 3D. If
207 		 * the user specified one of those then num_samples will be 3; and we need to
208 		 * produce 3 separate readings.
209 		 */
210 		for (int sample = 0; sample < num_samples; ++sample) {
211 			/* Check if the channel is already in the buffer */
212 			int prev_computed_value_idx = check_header_contains_channel(
213 				header, header->channels[sample_idx + sample], sample_idx + sample);
214 
215 			if (prev_computed_value_idx >= 0 &&
216 			    prev_computed_value_idx != sample_idx + sample) {
217 				LOG_DBG("value[%d] previously computed at q[%d]@%p", sample,
218 					prev_computed_value_idx,
219 					(void *)&q[prev_computed_value_idx]);
220 				q[sample_idx + sample] = q[prev_computed_value_idx];
221 				continue;
222 			}
223 
224 			/* Convert the value to micro-units */
225 			int64_t value_u = sensor_value_to_micro(&value[sample]);
226 
227 			/* Convert to q31 using the shift */
228 			q[sample_idx + sample] =
229 				((value_u * ((INT64_C(1) << 31) - 1)) / 1000000) >> header->shift;
230 
231 			LOG_DBG("value[%d]=%s%d.%06d, q[%d]@%p=%d", sample, value_u < 0 ? "-" : "",
232 				abs((int)value[sample].val1), abs((int)value[sample].val2),
233 				(int)(sample_idx + sample), (void *)&q[sample_idx + sample],
234 				q[sample_idx + sample]);
235 		}
236 		sample_idx += num_samples;
237 	}
238 	LOG_DBG("Total channels in header: %u", header->num_channels);
239 	rtio_iodev_sqe_ok(iodev_sqe, 0);
240 }
241 
sensor_processing_with_callback(struct rtio * ctx,sensor_processing_callback_t cb)242 void sensor_processing_with_callback(struct rtio *ctx, sensor_processing_callback_t cb)
243 {
244 	void *userdata = NULL;
245 	uint8_t *buf = NULL;
246 	uint32_t buf_len = 0;
247 	int rc;
248 
249 	/* Wait for a CQE */
250 	struct rtio_cqe *cqe = rtio_cqe_consume_block(ctx);
251 
252 	/* Cache the data from the CQE */
253 	rc = cqe->result;
254 	userdata = cqe->userdata;
255 	rtio_cqe_get_mempool_buffer(ctx, cqe, &buf, &buf_len);
256 
257 	/* Release the CQE */
258 	rtio_cqe_release(ctx, cqe);
259 
260 	/* Call the callback */
261 	cb(rc, buf, buf_len, userdata);
262 
263 	/* Release the memory */
264 	rtio_release_buffer(ctx, buf, buf_len);
265 }
266 
267 /**
268  * @brief Default decoder get frame count
269  *
270  * Default reader can only ever service a single frame at a time.
271  *
272  * @param[in]  buffer The data buffer to parse
273  * @param[in]  channel The channel to get the count for
274  * @param[in]  channel_idx The index of the channel
275  * @param[out] frame_count The number of frames in the buffer (always 1)
276  * @return 0 in all cases
277  */
get_frame_count(const uint8_t * buffer,enum sensor_channel channel,size_t channel_idx,uint16_t * frame_count)278 static int get_frame_count(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx,
279 			   uint16_t *frame_count)
280 {
281 	struct sensor_data_generic_header *header = (struct sensor_data_generic_header *)buffer;
282 	size_t count = 0;
283 
284 	switch (channel) {
285 	case SENSOR_CHAN_ACCEL_XYZ:
286 		channel = SENSOR_CHAN_ACCEL_X;
287 		break;
288 	case SENSOR_CHAN_GYRO_XYZ:
289 		channel = SENSOR_CHAN_GYRO_X;
290 		break;
291 	case SENSOR_CHAN_MAGN_XYZ:
292 		channel = SENSOR_CHAN_MAGN_X;
293 		break;
294 	default:
295 		break;
296 	}
297 	for (size_t i = 0; i < header->num_channels; ++i) {
298 		if (header->channels[i] == channel) {
299 			if (channel_idx == count) {
300 				*frame_count = 1;
301 				return 0;
302 			}
303 			++count;
304 		}
305 	}
306 
307 	return -ENOTSUP;
308 }
309 
sensor_natively_supported_channel_size_info(enum sensor_channel channel,size_t * base_size,size_t * frame_size)310 int sensor_natively_supported_channel_size_info(enum sensor_channel channel, size_t *base_size,
311 						size_t *frame_size)
312 {
313 	__ASSERT_NO_MSG(base_size != NULL);
314 	__ASSERT_NO_MSG(frame_size != NULL);
315 
316 	switch (channel) {
317 	case SENSOR_CHAN_ACCEL_X:
318 	case SENSOR_CHAN_ACCEL_Y:
319 	case SENSOR_CHAN_ACCEL_Z:
320 	case SENSOR_CHAN_ACCEL_XYZ:
321 	case SENSOR_CHAN_GYRO_X:
322 	case SENSOR_CHAN_GYRO_Y:
323 	case SENSOR_CHAN_GYRO_Z:
324 	case SENSOR_CHAN_GYRO_XYZ:
325 	case SENSOR_CHAN_MAGN_X:
326 	case SENSOR_CHAN_MAGN_Y:
327 	case SENSOR_CHAN_MAGN_Z:
328 	case SENSOR_CHAN_MAGN_XYZ:
329 	case SENSOR_CHAN_POS_DX:
330 	case SENSOR_CHAN_POS_DY:
331 	case SENSOR_CHAN_POS_DZ:
332 		*base_size = sizeof(struct sensor_three_axis_data);
333 		*frame_size = sizeof(struct sensor_three_axis_sample_data);
334 		return 0;
335 	case SENSOR_CHAN_DIE_TEMP:
336 	case SENSOR_CHAN_AMBIENT_TEMP:
337 	case SENSOR_CHAN_PRESS:
338 	case SENSOR_CHAN_HUMIDITY:
339 	case SENSOR_CHAN_LIGHT:
340 	case SENSOR_CHAN_IR:
341 	case SENSOR_CHAN_RED:
342 	case SENSOR_CHAN_GREEN:
343 	case SENSOR_CHAN_BLUE:
344 	case SENSOR_CHAN_ALTITUDE:
345 	case SENSOR_CHAN_PM_1_0:
346 	case SENSOR_CHAN_PM_2_5:
347 	case SENSOR_CHAN_PM_10:
348 	case SENSOR_CHAN_DISTANCE:
349 	case SENSOR_CHAN_CO2:
350 	case SENSOR_CHAN_VOC:
351 	case SENSOR_CHAN_GAS_RES:
352 	case SENSOR_CHAN_VOLTAGE:
353 	case SENSOR_CHAN_CURRENT:
354 	case SENSOR_CHAN_POWER:
355 	case SENSOR_CHAN_RESISTANCE:
356 	case SENSOR_CHAN_ROTATION:
357 	case SENSOR_CHAN_RPM:
358 	case SENSOR_CHAN_GAUGE_VOLTAGE:
359 	case SENSOR_CHAN_GAUGE_AVG_CURRENT:
360 	case SENSOR_CHAN_GAUGE_STDBY_CURRENT:
361 	case SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT:
362 	case SENSOR_CHAN_GAUGE_TEMP:
363 	case SENSOR_CHAN_GAUGE_STATE_OF_CHARGE:
364 	case SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY:
365 	case SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY:
366 	case SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY:
367 	case SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY:
368 	case SENSOR_CHAN_GAUGE_AVG_POWER:
369 	case SENSOR_CHAN_GAUGE_STATE_OF_HEALTH:
370 	case SENSOR_CHAN_GAUGE_TIME_TO_EMPTY:
371 	case SENSOR_CHAN_GAUGE_TIME_TO_FULL:
372 	case SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE:
373 	case SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE:
374 	case SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT:
375 		*base_size = sizeof(struct sensor_q31_data);
376 		*frame_size = sizeof(struct sensor_q31_sample_data);
377 		return 0;
378 	case SENSOR_CHAN_PROX:
379 		*base_size = sizeof(struct sensor_byte_data);
380 		*frame_size = sizeof(struct sensor_byte_sample_data);
381 		return 0;
382 	case SENSOR_CHAN_GAUGE_CYCLE_COUNT:
383 		*base_size = sizeof(struct sensor_uint64_data);
384 		*frame_size = sizeof(struct sensor_uint64_sample_data);
385 		return 0;
386 	default:
387 		return -ENOTSUP;
388 	}
389 }
390 
get_q31_value(const struct sensor_data_generic_header * header,const q31_t * values,enum sensor_channel channel,size_t channel_idx,q31_t * out)391 static int get_q31_value(const struct sensor_data_generic_header *header, const q31_t *values,
392 			 enum sensor_channel channel, size_t channel_idx, q31_t *out)
393 {
394 	size_t count = 0;
395 
396 	for (size_t i = 0; i < header->num_channels; ++i) {
397 		if (channel != header->channels[i]) {
398 			continue;
399 		}
400 		if (count == channel_idx) {
401 			*out = values[i];
402 			return 0;
403 		}
404 		++count;
405 	}
406 	return -EINVAL;
407 }
408 
decode_three_axis(const struct sensor_data_generic_header * header,const q31_t * values,struct sensor_three_axis_data * data_out,enum sensor_channel x,enum sensor_channel y,enum sensor_channel z,size_t channel_idx)409 static int decode_three_axis(const struct sensor_data_generic_header *header, const q31_t *values,
410 			     struct sensor_three_axis_data *data_out, enum sensor_channel x,
411 			     enum sensor_channel y, enum sensor_channel z, size_t channel_idx)
412 {
413 	int rc;
414 
415 	data_out->header.base_timestamp_ns = header->timestamp_ns;
416 	data_out->header.reading_count = 1;
417 	data_out->shift = header->shift;
418 	data_out->readings[0].timestamp_delta = 0;
419 
420 	rc = get_q31_value(header, values, x, channel_idx, &data_out->readings[0].values[0]);
421 	if (rc < 0) {
422 		return rc;
423 	}
424 	rc = get_q31_value(header, values, y, channel_idx, &data_out->readings[0].values[1]);
425 	if (rc < 0) {
426 		return rc;
427 	}
428 	rc = get_q31_value(header, values, z, channel_idx, &data_out->readings[0].values[2]);
429 	if (rc < 0) {
430 		return rc;
431 	}
432 	return 1;
433 }
434 
decode_q31(const struct sensor_data_generic_header * header,const q31_t * values,struct sensor_q31_data * data_out,enum sensor_channel channel,size_t channel_idx)435 static int decode_q31(const struct sensor_data_generic_header *header, const q31_t *values,
436 		      struct sensor_q31_data *data_out, enum sensor_channel channel,
437 		      size_t channel_idx)
438 {
439 	int rc;
440 
441 	data_out->header.base_timestamp_ns = header->timestamp_ns;
442 	data_out->header.reading_count = 1;
443 	data_out->shift = header->shift;
444 	data_out->readings[0].timestamp_delta = 0;
445 
446 	rc = get_q31_value(header, values, channel, channel_idx, &data_out->readings[0].value);
447 	if (rc < 0) {
448 		return rc;
449 	}
450 	return 1;
451 }
452 
453 /**
454  * @brief Decode up to N samples from the buffer
455  *
456  * This function will never wrap frames. If 1 channel is available in the current frame and
457  * @p max_count is 2, only 1 channel will be decoded and the frame iterator will be modified
458  * so that the next call to decode will begin at the next frame.
459  *
460  * @param[in]     buffer The buffer provided on the :c:struct:`rtio` context
461  * @param[in]     channel The channel to decode
462  * @param[in]     channel_idx The index of the channel
463  * @param[in,out] fit The current frame iterator
464  * @param[in]     max_count The maximum number of channels to decode.
465  * @param[out]    data_out The decoded data
466  * @return 0 no more samples to decode
467  * @return >0 the number of decoded frames
468  * @return <0 on error
469  */
decode(const uint8_t * buffer,enum sensor_channel channel,size_t channel_idx,uint32_t * fit,uint16_t max_count,void * data_out)470 static int decode(const uint8_t *buffer, enum sensor_channel channel, size_t channel_idx,
471 		  uint32_t *fit, uint16_t max_count, void *data_out)
472 {
473 	const struct sensor_data_generic_header *header =
474 		(const struct sensor_data_generic_header *)buffer;
475 	const q31_t *q =
476 		(const q31_t *)(buffer + sizeof(struct sensor_data_generic_header) +
477 				header->num_channels * sizeof(enum sensor_channel));
478 	int count = 0;
479 
480 	if (*fit != 0 || max_count < 1) {
481 		return -EINVAL;
482 	}
483 
484 	/* Check for 3d channel mappings */
485 	switch (channel) {
486 	case SENSOR_CHAN_ACCEL_X:
487 	case SENSOR_CHAN_ACCEL_Y:
488 	case SENSOR_CHAN_ACCEL_Z:
489 	case SENSOR_CHAN_ACCEL_XYZ:
490 		count = decode_three_axis(header, q, data_out, SENSOR_CHAN_ACCEL_X,
491 					  SENSOR_CHAN_ACCEL_Y, SENSOR_CHAN_ACCEL_Z, channel_idx);
492 		break;
493 	case SENSOR_CHAN_GYRO_X:
494 	case SENSOR_CHAN_GYRO_Y:
495 	case SENSOR_CHAN_GYRO_Z:
496 	case SENSOR_CHAN_GYRO_XYZ:
497 		count = decode_three_axis(header, q, data_out, SENSOR_CHAN_GYRO_X,
498 					  SENSOR_CHAN_GYRO_Y, SENSOR_CHAN_GYRO_Z, channel_idx);
499 		break;
500 	case SENSOR_CHAN_MAGN_X:
501 	case SENSOR_CHAN_MAGN_Y:
502 	case SENSOR_CHAN_MAGN_Z:
503 	case SENSOR_CHAN_MAGN_XYZ:
504 		count = decode_three_axis(header, q, data_out, SENSOR_CHAN_MAGN_X,
505 					  SENSOR_CHAN_MAGN_Y, SENSOR_CHAN_MAGN_Z, channel_idx);
506 		break;
507 	case SENSOR_CHAN_POS_DX:
508 	case SENSOR_CHAN_POS_DY:
509 	case SENSOR_CHAN_POS_DZ:
510 		count = decode_three_axis(header, q, data_out, SENSOR_CHAN_POS_DX,
511 					  SENSOR_CHAN_POS_DY, SENSOR_CHAN_POS_DZ, channel_idx);
512 		break;
513 	case SENSOR_CHAN_DIE_TEMP:
514 	case SENSOR_CHAN_AMBIENT_TEMP:
515 	case SENSOR_CHAN_PRESS:
516 	case SENSOR_CHAN_HUMIDITY:
517 	case SENSOR_CHAN_LIGHT:
518 	case SENSOR_CHAN_IR:
519 	case SENSOR_CHAN_RED:
520 	case SENSOR_CHAN_GREEN:
521 	case SENSOR_CHAN_BLUE:
522 	case SENSOR_CHAN_ALTITUDE:
523 	case SENSOR_CHAN_PM_1_0:
524 	case SENSOR_CHAN_PM_2_5:
525 	case SENSOR_CHAN_PM_10:
526 	case SENSOR_CHAN_DISTANCE:
527 	case SENSOR_CHAN_CO2:
528 	case SENSOR_CHAN_VOC:
529 	case SENSOR_CHAN_GAS_RES:
530 	case SENSOR_CHAN_VOLTAGE:
531 	case SENSOR_CHAN_CURRENT:
532 	case SENSOR_CHAN_POWER:
533 	case SENSOR_CHAN_RESISTANCE:
534 	case SENSOR_CHAN_ROTATION:
535 	case SENSOR_CHAN_RPM:
536 	case SENSOR_CHAN_GAUGE_VOLTAGE:
537 	case SENSOR_CHAN_GAUGE_AVG_CURRENT:
538 	case SENSOR_CHAN_GAUGE_STDBY_CURRENT:
539 	case SENSOR_CHAN_GAUGE_MAX_LOAD_CURRENT:
540 	case SENSOR_CHAN_GAUGE_TEMP:
541 	case SENSOR_CHAN_GAUGE_STATE_OF_CHARGE:
542 	case SENSOR_CHAN_GAUGE_FULL_CHARGE_CAPACITY:
543 	case SENSOR_CHAN_GAUGE_REMAINING_CHARGE_CAPACITY:
544 	case SENSOR_CHAN_GAUGE_NOM_AVAIL_CAPACITY:
545 	case SENSOR_CHAN_GAUGE_FULL_AVAIL_CAPACITY:
546 	case SENSOR_CHAN_GAUGE_AVG_POWER:
547 	case SENSOR_CHAN_GAUGE_STATE_OF_HEALTH:
548 	case SENSOR_CHAN_GAUGE_TIME_TO_EMPTY:
549 	case SENSOR_CHAN_GAUGE_TIME_TO_FULL:
550 	case SENSOR_CHAN_GAUGE_DESIGN_VOLTAGE:
551 	case SENSOR_CHAN_GAUGE_DESIRED_VOLTAGE:
552 	case SENSOR_CHAN_GAUGE_DESIRED_CHARGING_CURRENT:
553 		count = decode_q31(header, q, data_out, channel, channel_idx);
554 		break;
555 	default:
556 		break;
557 	}
558 	if (count > 0) {
559 		*fit = 1;
560 	}
561 	return count;
562 }
563 
564 const struct sensor_decoder_api __sensor_default_decoder = {
565 	.get_frame_count = get_frame_count,
566 	.get_size_info = sensor_natively_supported_channel_size_info,
567 	.decode = decode,
568 };
569