1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * System Control and Management Interface(SCMI) based IIO sensor driver
5  *
6  * Copyright (C) 2021 Google LLC
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/iio/buffer.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/kfifo_buf.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/kernel.h>
16 #include <linux/kthread.h>
17 #include <linux/module.h>
18 #include <linux/scmi_protocol.h>
19 #include <linux/time.h>
20 #include <linux/types.h>
21 
22 #define SCMI_IIO_NUM_OF_AXIS 3
23 
24 struct scmi_iio_priv {
25 	struct scmi_handle *handle;
26 	const struct scmi_sensor_info *sensor_info;
27 	struct iio_dev *indio_dev;
28 	/* adding one additional channel for timestamp */
29 	s64 iio_buf[SCMI_IIO_NUM_OF_AXIS + 1];
30 	struct notifier_block sensor_update_nb;
31 	u32 *freq_avail;
32 };
33 
34 static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
35 				     unsigned long event, void *data)
36 {
37 	struct scmi_sensor_update_report *sensor_update = data;
38 	struct iio_dev *scmi_iio_dev;
39 	struct scmi_iio_priv *sensor;
40 	s8 tstamp_scale;
41 	u64 time, time_ns;
42 	int i;
43 
44 	if (sensor_update->readings_count == 0)
45 		return NOTIFY_DONE;
46 
47 	sensor = container_of(nb, struct scmi_iio_priv, sensor_update_nb);
48 
49 	for (i = 0; i < sensor_update->readings_count; i++)
50 		sensor->iio_buf[i] = sensor_update->readings[i].value;
51 
52 	if (!sensor->sensor_info->timestamped) {
53 		time_ns = ktime_to_ns(sensor_update->timestamp);
54 	} else {
55 		/*
56 		 *  All the axes are supposed to have the same value for timestamp.
57 		 *  We are just using the values from the Axis 0 here.
58 		 */
59 		time = sensor_update->readings[0].timestamp;
60 
61 		/*
62 		 *  Timestamp returned by SCMI is in seconds and is equal to
63 		 *  time * power-of-10 multiplier(tstamp_scale) seconds.
64 		 *  Converting the timestamp to nanoseconds below.
65 		 */
66 		tstamp_scale = sensor->sensor_info->tstamp_scale +
67 			       const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
68 		if (tstamp_scale < 0) {
69 			do_div(time, int_pow(10, abs(tstamp_scale)));
70 			time_ns = time;
71 		} else {
72 			time_ns = time * int_pow(10, tstamp_scale);
73 		}
74 	}
75 
76 	scmi_iio_dev = sensor->indio_dev;
77 	iio_push_to_buffers_with_timestamp(scmi_iio_dev, sensor->iio_buf,
78 					   time_ns);
79 	return NOTIFY_OK;
80 }
81 
82 static int scmi_iio_buffer_preenable(struct iio_dev *iio_dev)
83 {
84 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
85 	u32 sensor_id = sensor->sensor_info->id;
86 	u32 sensor_config = 0;
87 	int err;
88 
89 	if (sensor->sensor_info->timestamped)
90 		sensor_config |= FIELD_PREP(SCMI_SENS_CFG_TSTAMP_ENABLED_MASK,
91 					    SCMI_SENS_CFG_TSTAMP_ENABLE);
92 
93 	sensor_config |= FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
94 				    SCMI_SENS_CFG_SENSOR_ENABLE);
95 
96 	err = sensor->handle->notify_ops->register_event_notifier(sensor->handle,
97 			SCMI_PROTOCOL_SENSOR, SCMI_EVENT_SENSOR_UPDATE,
98 			&sensor_id, &sensor->sensor_update_nb);
99 	if (err) {
100 		dev_err(&iio_dev->dev,
101 			"Error in registering sensor update notifier for sensor %s err %d",
102 			sensor->sensor_info->name, err);
103 		return err;
104 	}
105 
106 	err = sensor->handle->sensor_ops->config_set(sensor->handle,
107 			sensor->sensor_info->id, sensor_config);
108 	if (err) {
109 		sensor->handle->notify_ops->unregister_event_notifier(sensor->handle,
110 				SCMI_PROTOCOL_SENSOR,
111 				SCMI_EVENT_SENSOR_UPDATE, &sensor_id,
112 				&sensor->sensor_update_nb);
113 		dev_err(&iio_dev->dev, "Error in enabling sensor %s err %d",
114 			sensor->sensor_info->name, err);
115 	}
116 
117 	return err;
118 }
119 
120 static int scmi_iio_buffer_postdisable(struct iio_dev *iio_dev)
121 {
122 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
123 	u32 sensor_id = sensor->sensor_info->id;
124 	u32 sensor_config = 0;
125 	int err;
126 
127 	sensor_config |= FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
128 				    SCMI_SENS_CFG_SENSOR_DISABLE);
129 
130 	err = sensor->handle->notify_ops->unregister_event_notifier(sensor->handle,
131 			SCMI_PROTOCOL_SENSOR, SCMI_EVENT_SENSOR_UPDATE,
132 			&sensor_id, &sensor->sensor_update_nb);
133 	if (err) {
134 		dev_err(&iio_dev->dev,
135 			"Error in unregistering sensor update notifier for sensor %s err %d",
136 			sensor->sensor_info->name, err);
137 		return err;
138 	}
139 
140 	err = sensor->handle->sensor_ops->config_set(sensor->handle, sensor_id,
141 						     sensor_config);
142 	if (err) {
143 		dev_err(&iio_dev->dev,
144 			"Error in disabling sensor %s with err %d",
145 			sensor->sensor_info->name, err);
146 	}
147 
148 	return err;
149 }
150 
151 static const struct iio_buffer_setup_ops scmi_iio_buffer_ops = {
152 	.preenable = scmi_iio_buffer_preenable,
153 	.postdisable = scmi_iio_buffer_postdisable,
154 };
155 
156 static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
157 {
158 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
159 	const unsigned long UHZ_PER_HZ = 1000000UL;
160 	u64 sec, mult, uHz, sf;
161 	u32 sensor_config;
162 	char buf[32];
163 
164 	int err = sensor->handle->sensor_ops->config_get(sensor->handle,
165 			sensor->sensor_info->id, &sensor_config);
166 	if (err) {
167 		dev_err(&iio_dev->dev,
168 			"Error in getting sensor config for sensor %s err %d",
169 			sensor->sensor_info->name, err);
170 		return err;
171 	}
172 
173 	uHz = val * UHZ_PER_HZ + val2;
174 
175 	/*
176 	 * The seconds field in the sensor interval in SCMI is 16 bits long
177 	 * Therefore seconds  = 1/Hz <= 0xFFFF. As floating point calculations are
178 	 * discouraged in the kernel driver code, to calculate the scale factor (sf)
179 	 * (1* 1000000 * sf)/uHz <= 0xFFFF. Therefore, sf <= (uHz * 0xFFFF)/1000000
180 	 * To calculate the multiplier,we convert the sf into char string  and
181 	 * count the number of characters
182 	 */
183 	sf = (u64)uHz * 0xFFFF;
184 	do_div(sf,  UHZ_PER_HZ);
185 	mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
186 
187 	sec = int_pow(10, mult) * UHZ_PER_HZ;
188 	do_div(sec, uHz);
189 	if (sec == 0) {
190 		dev_err(&iio_dev->dev,
191 			"Trying to set invalid sensor update value for sensor %s",
192 			sensor->sensor_info->name);
193 		return -EINVAL;
194 	}
195 
196 	sensor_config &= ~SCMI_SENS_CFG_UPDATE_SECS_MASK;
197 	sensor_config |= FIELD_PREP(SCMI_SENS_CFG_UPDATE_SECS_MASK, sec);
198 	sensor_config &= ~SCMI_SENS_CFG_UPDATE_EXP_MASK;
199 	sensor_config |= FIELD_PREP(SCMI_SENS_CFG_UPDATE_EXP_MASK, -mult);
200 
201 	if (sensor->sensor_info->timestamped) {
202 		sensor_config &= ~SCMI_SENS_CFG_TSTAMP_ENABLED_MASK;
203 		sensor_config |= FIELD_PREP(SCMI_SENS_CFG_TSTAMP_ENABLED_MASK,
204 					    SCMI_SENS_CFG_TSTAMP_ENABLE);
205 	}
206 
207 	sensor_config &= ~SCMI_SENS_CFG_ROUND_MASK;
208 	sensor_config |=
209 		FIELD_PREP(SCMI_SENS_CFG_ROUND_MASK, SCMI_SENS_CFG_ROUND_AUTO);
210 
211 	err = sensor->handle->sensor_ops->config_set(sensor->handle,
212 			sensor->sensor_info->id, sensor_config);
213 	if (err)
214 		dev_err(&iio_dev->dev,
215 			"Error in setting sensor update interval for sensor %s value %u err %d",
216 			sensor->sensor_info->name, sensor_config, err);
217 
218 	return err;
219 }
220 
221 static int scmi_iio_write_raw(struct iio_dev *iio_dev,
222 			      struct iio_chan_spec const *chan, int val,
223 			      int val2, long mask)
224 {
225 	int err;
226 
227 	switch (mask) {
228 	case IIO_CHAN_INFO_SAMP_FREQ:
229 		mutex_lock(&iio_dev->mlock);
230 		err = scmi_iio_set_odr_val(iio_dev, val, val2);
231 		mutex_unlock(&iio_dev->mlock);
232 		return err;
233 	default:
234 		return -EINVAL;
235 	}
236 }
237 
238 static int scmi_iio_read_avail(struct iio_dev *iio_dev,
239 			       struct iio_chan_spec const *chan,
240 			       const int **vals, int *type, int *length,
241 			       long mask)
242 {
243 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
244 
245 	switch (mask) {
246 	case IIO_CHAN_INFO_SAMP_FREQ:
247 		*vals = sensor->freq_avail;
248 		*type = IIO_VAL_INT_PLUS_MICRO;
249 		*length = sensor->sensor_info->intervals.count * 2;
250 		if (sensor->sensor_info->intervals.segmented)
251 			return IIO_AVAIL_RANGE;
252 		else
253 			return IIO_AVAIL_LIST;
254 	default:
255 		return -EINVAL;
256 	}
257 }
258 
259 static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
260 {
261 	u64 rem, freq;
262 
263 	freq = NSEC_PER_SEC;
264 	rem = do_div(freq, interval_ns);
265 	*hz = freq;
266 	*uhz = rem * 1000000UL;
267 	do_div(*uhz, interval_ns);
268 }
269 
270 static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
271 {
272 	u64 sensor_update_interval, sensor_interval_mult, hz, uhz;
273 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
274 	u32 sensor_config;
275 	int mult;
276 
277 	int err = sensor->handle->sensor_ops->config_get(sensor->handle,
278 			sensor->sensor_info->id, &sensor_config);
279 	if (err) {
280 		dev_err(&iio_dev->dev,
281 			"Error in getting sensor config for sensor %s err %d",
282 			sensor->sensor_info->name, err);
283 		return err;
284 	}
285 
286 	sensor_update_interval =
287 		SCMI_SENS_CFG_GET_UPDATE_SECS(sensor_config) * NSEC_PER_SEC;
288 
289 	mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
290 	if (mult < 0) {
291 		sensor_interval_mult = int_pow(10, abs(mult));
292 		do_div(sensor_update_interval, sensor_interval_mult);
293 	} else {
294 		sensor_interval_mult = int_pow(10, mult);
295 		sensor_update_interval =
296 			sensor_update_interval * sensor_interval_mult;
297 	}
298 
299 	convert_ns_to_freq(sensor_update_interval, &hz, &uhz);
300 	*val = hz;
301 	*val2 = uhz;
302 	return 0;
303 }
304 
305 static int scmi_iio_read_raw(struct iio_dev *iio_dev,
306 			     struct iio_chan_spec const *ch, int *val,
307 			     int *val2, long mask)
308 {
309 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
310 	s8 scale;
311 	int ret;
312 
313 	switch (mask) {
314 	case IIO_CHAN_INFO_SCALE:
315 		scale = sensor->sensor_info->axis[ch->scan_index].scale;
316 		if (scale < 0) {
317 			*val = 1;
318 			*val2 = int_pow(10, abs(scale));
319 			return IIO_VAL_FRACTIONAL;
320 		}
321 		*val = int_pow(10, scale);
322 		return IIO_VAL_INT;
323 	case IIO_CHAN_INFO_SAMP_FREQ:
324 		ret = scmi_iio_get_odr_val(iio_dev, val, val2);
325 		return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
326 	default:
327 		return -EINVAL;
328 	}
329 }
330 
331 static const struct iio_info scmi_iio_info = {
332 	.read_raw = scmi_iio_read_raw,
333 	.read_avail = scmi_iio_read_avail,
334 	.write_raw = scmi_iio_write_raw,
335 };
336 
337 static ssize_t scmi_iio_get_raw_available(struct iio_dev *iio_dev,
338 					  uintptr_t private,
339 					  const struct iio_chan_spec *chan,
340 					  char *buf)
341 {
342 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
343 	u64 resolution, rem;
344 	s64 min_range, max_range;
345 	s8 exponent, scale;
346 	int len = 0;
347 
348 	/*
349 	 * All the axes are supposed to have the same value for range and resolution.
350 	 * We are just using the values from the Axis 0 here.
351 	 */
352 	if (sensor->sensor_info->axis[0].extended_attrs) {
353 		min_range = sensor->sensor_info->axis[0].attrs.min_range;
354 		max_range = sensor->sensor_info->axis[0].attrs.max_range;
355 		resolution = sensor->sensor_info->axis[0].resolution;
356 		exponent = sensor->sensor_info->axis[0].exponent;
357 		scale = sensor->sensor_info->axis[0].scale;
358 
359 		/*
360 		 * To provide the raw value for the resolution to the userspace,
361 		 * need to divide the resolution exponent by the sensor scale
362 		 */
363 		exponent = exponent - scale;
364 		if (exponent < 0) {
365 			rem = do_div(resolution,
366 				     int_pow(10, abs(exponent))
367 				     );
368 			len = scnprintf(buf, PAGE_SIZE,
369 					"[%lld %llu.%llu %lld]\n", min_range,
370 					resolution, rem, max_range);
371 		} else {
372 			resolution = resolution * int_pow(10, exponent);
373 			len = scnprintf(buf, PAGE_SIZE, "[%lld %llu %lld]\n",
374 					min_range, resolution, max_range);
375 		}
376 	}
377 	return len;
378 }
379 
380 static const struct iio_chan_spec_ext_info scmi_iio_ext_info[] = {
381 	{
382 		.name = "raw_available",
383 		.read = scmi_iio_get_raw_available,
384 		.shared = IIO_SHARED_BY_TYPE,
385 	},
386 	{},
387 };
388 
389 static void scmi_iio_set_timestamp_channel(struct iio_chan_spec *iio_chan,
390 					   int scan_index)
391 {
392 	iio_chan->type = IIO_TIMESTAMP;
393 	iio_chan->channel = -1;
394 	iio_chan->scan_index = scan_index;
395 	iio_chan->scan_type.sign = 'u';
396 	iio_chan->scan_type.realbits = 64;
397 	iio_chan->scan_type.storagebits = 64;
398 }
399 
400 static void scmi_iio_set_data_channel(struct iio_chan_spec *iio_chan,
401 				      enum iio_chan_type type,
402 				      enum iio_modifier mod, int scan_index)
403 {
404 	iio_chan->type = type;
405 	iio_chan->modified = 1;
406 	iio_chan->channel2 = mod;
407 	iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_SCALE);
408 	iio_chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ);
409 	iio_chan->info_mask_shared_by_type_available =
410 		BIT(IIO_CHAN_INFO_SAMP_FREQ);
411 	iio_chan->scan_index = scan_index;
412 	iio_chan->scan_type.sign = 's';
413 	iio_chan->scan_type.realbits = 64;
414 	iio_chan->scan_type.storagebits = 64;
415 	iio_chan->scan_type.endianness = IIO_LE;
416 	iio_chan->ext_info = scmi_iio_ext_info;
417 }
418 
419 static int scmi_iio_get_chan_modifier(const char *name,
420 				      enum iio_modifier *modifier)
421 {
422 	char *pch, mod;
423 
424 	if (!name)
425 		return -EINVAL;
426 
427 	pch = strrchr(name, '_');
428 	if (!pch)
429 		return -EINVAL;
430 
431 	mod = *(pch + 1);
432 	switch (mod) {
433 	case 'X':
434 		*modifier = IIO_MOD_X;
435 		return 0;
436 	case 'Y':
437 		*modifier = IIO_MOD_Y;
438 		return 0;
439 	case 'Z':
440 		*modifier = IIO_MOD_Z;
441 		return 0;
442 	default:
443 		return -EINVAL;
444 	}
445 }
446 
447 static int scmi_iio_get_chan_type(u8 scmi_type, enum iio_chan_type *iio_type)
448 {
449 	switch (scmi_type) {
450 	case METERS_SEC_SQUARED:
451 		*iio_type = IIO_ACCEL;
452 		return 0;
453 	case RADIANS_SEC:
454 		*iio_type = IIO_ANGL_VEL;
455 		return 0;
456 	default:
457 		return -EINVAL;
458 	}
459 }
460 
461 static u64 scmi_iio_convert_interval_to_ns(u32 val)
462 {
463 	u64 sensor_update_interval =
464 		SCMI_SENS_INTVL_GET_SECS(val) * NSEC_PER_SEC;
465 	u64 sensor_interval_mult;
466 	int mult;
467 
468 	mult = SCMI_SENS_INTVL_GET_EXP(val);
469 	if (mult < 0) {
470 		sensor_interval_mult = int_pow(10, abs(mult));
471 		do_div(sensor_update_interval, sensor_interval_mult);
472 	} else {
473 		sensor_interval_mult = int_pow(10, mult);
474 		sensor_update_interval =
475 			sensor_update_interval * sensor_interval_mult;
476 	}
477 	return sensor_update_interval;
478 }
479 
480 static int scmi_iio_set_sampling_freq_avail(struct iio_dev *iio_dev)
481 {
482 	u64 cur_interval_ns, low_interval_ns, high_interval_ns, step_size_ns,
483 		hz, uhz;
484 	unsigned int cur_interval, low_interval, high_interval, step_size;
485 	struct scmi_iio_priv *sensor = iio_priv(iio_dev);
486 	int i;
487 
488 	sensor->freq_avail =
489 		devm_kzalloc(&iio_dev->dev,
490 			     sizeof(*sensor->freq_avail) *
491 				     (sensor->sensor_info->intervals.count * 2),
492 			     GFP_KERNEL);
493 	if (!sensor->freq_avail)
494 		return -ENOMEM;
495 
496 	if (sensor->sensor_info->intervals.segmented) {
497 		low_interval = sensor->sensor_info->intervals
498 				       .desc[SCMI_SENS_INTVL_SEGMENT_LOW];
499 		low_interval_ns = scmi_iio_convert_interval_to_ns(low_interval);
500 		convert_ns_to_freq(low_interval_ns, &hz, &uhz);
501 		sensor->freq_avail[0] = hz;
502 		sensor->freq_avail[1] = uhz;
503 
504 		step_size = sensor->sensor_info->intervals
505 				    .desc[SCMI_SENS_INTVL_SEGMENT_STEP];
506 		step_size_ns = scmi_iio_convert_interval_to_ns(step_size);
507 		convert_ns_to_freq(step_size_ns, &hz, &uhz);
508 		sensor->freq_avail[2] = hz;
509 		sensor->freq_avail[3] = uhz;
510 
511 		high_interval = sensor->sensor_info->intervals
512 					.desc[SCMI_SENS_INTVL_SEGMENT_HIGH];
513 		high_interval_ns =
514 			scmi_iio_convert_interval_to_ns(high_interval);
515 		convert_ns_to_freq(high_interval_ns, &hz, &uhz);
516 		sensor->freq_avail[4] = hz;
517 		sensor->freq_avail[5] = uhz;
518 	} else {
519 		for (i = 0; i < sensor->sensor_info->intervals.count; i++) {
520 			cur_interval = sensor->sensor_info->intervals.desc[i];
521 			cur_interval_ns =
522 				scmi_iio_convert_interval_to_ns(cur_interval);
523 			convert_ns_to_freq(cur_interval_ns, &hz, &uhz);
524 			sensor->freq_avail[i * 2] = hz;
525 			sensor->freq_avail[i * 2 + 1] = uhz;
526 		}
527 	}
528 	return 0;
529 }
530 
531 static struct iio_dev *scmi_alloc_iiodev(struct device *dev,
532 					 struct scmi_handle *handle,
533 					 const struct scmi_sensor_info *sensor_info)
534 {
535 	struct iio_chan_spec *iio_channels;
536 	struct scmi_iio_priv *sensor;
537 	enum iio_modifier modifier;
538 	enum iio_chan_type type;
539 	struct iio_dev *iiodev;
540 	int i, ret;
541 
542 	iiodev = devm_iio_device_alloc(dev, sizeof(*sensor));
543 	if (!iiodev)
544 		return ERR_PTR(-ENOMEM);
545 
546 	iiodev->modes = INDIO_DIRECT_MODE;
547 	iiodev->dev.parent = dev;
548 	sensor = iio_priv(iiodev);
549 	sensor->handle = handle;
550 	sensor->sensor_info = sensor_info;
551 	sensor->sensor_update_nb.notifier_call = scmi_iio_sensor_update_cb;
552 	sensor->indio_dev = iiodev;
553 
554 	/* adding one additional channel for timestamp */
555 	iiodev->num_channels = sensor_info->num_axis + 1;
556 	iiodev->name = sensor_info->name;
557 	iiodev->info = &scmi_iio_info;
558 
559 	iio_channels =
560 		devm_kzalloc(dev,
561 			     sizeof(*iio_channels) * (iiodev->num_channels),
562 			     GFP_KERNEL);
563 	if (!iio_channels)
564 		return ERR_PTR(-ENOMEM);
565 
566 	ret = scmi_iio_set_sampling_freq_avail(iiodev);
567 	if (ret < 0)
568 		return ERR_PTR(ret);
569 
570 	for (i = 0; i < sensor_info->num_axis; i++) {
571 		ret = scmi_iio_get_chan_type(sensor_info->axis[i].type, &type);
572 		if (ret < 0)
573 			return ERR_PTR(ret);
574 
575 		ret = scmi_iio_get_chan_modifier(sensor_info->axis[i].name,
576 						 &modifier);
577 		if (ret < 0)
578 			return ERR_PTR(ret);
579 
580 		scmi_iio_set_data_channel(&iio_channels[i], type, modifier,
581 					  sensor_info->axis[i].id);
582 	}
583 
584 	scmi_iio_set_timestamp_channel(&iio_channels[i], i);
585 	iiodev->channels = iio_channels;
586 	return iiodev;
587 }
588 
589 static int scmi_iio_dev_probe(struct scmi_device *sdev)
590 {
591 	const struct scmi_sensor_info *sensor_info;
592 	struct scmi_handle *handle = sdev->handle;
593 	struct device *dev = &sdev->dev;
594 	struct iio_dev *scmi_iio_dev;
595 	u16 nr_sensors;
596 	int err = -ENODEV, i;
597 
598 	if (!handle || !handle->sensor_ops) {
599 		dev_err(dev, "SCMI device has no sensor interface\n");
600 		return -EINVAL;
601 	}
602 
603 	nr_sensors = handle->sensor_ops->count_get(handle);
604 	if (!nr_sensors) {
605 		dev_dbg(dev, "0 sensors found via SCMI bus\n");
606 		return -ENODEV;
607 	}
608 
609 	for (i = 0; i < nr_sensors; i++) {
610 		sensor_info = handle->sensor_ops->info_get(handle, i);
611 		if (!sensor_info) {
612 			dev_err(dev, "SCMI sensor %d has missing info\n", i);
613 			return -EINVAL;
614 		}
615 
616 		/* This driver only supports 3-axis accel and gyro, skipping other sensors */
617 		if (sensor_info->num_axis != SCMI_IIO_NUM_OF_AXIS)
618 			continue;
619 
620 		/* This driver only supports 3-axis accel and gyro, skipping other sensors */
621 		if (sensor_info->axis[0].type != METERS_SEC_SQUARED &&
622 		    sensor_info->axis[0].type != RADIANS_SEC)
623 			continue;
624 
625 		scmi_iio_dev = scmi_alloc_iiodev(dev, handle, sensor_info);
626 		if (IS_ERR(scmi_iio_dev)) {
627 			dev_err(dev,
628 				"failed to allocate IIO device for sensor %s: %ld\n",
629 				sensor_info->name, PTR_ERR(scmi_iio_dev));
630 			return PTR_ERR(scmi_iio_dev);
631 		}
632 
633 		err = devm_iio_kfifo_buffer_setup(&scmi_iio_dev->dev,
634 						  scmi_iio_dev,
635 						  INDIO_BUFFER_SOFTWARE,
636 						  &scmi_iio_buffer_ops);
637 		if (err < 0) {
638 			dev_err(dev,
639 				"IIO buffer setup error at sensor %s: %d\n",
640 				sensor_info->name, err);
641 			return err;
642 		}
643 
644 		err = devm_iio_device_register(dev, scmi_iio_dev);
645 		if (err) {
646 			dev_err(dev,
647 				"IIO device registration failed at sensor %s: %d\n",
648 				sensor_info->name, err);
649 			return err;
650 		}
651 	}
652 	return err;
653 }
654 
655 static const struct scmi_device_id scmi_id_table[] = {
656 	{ SCMI_PROTOCOL_SENSOR, "iiodev" },
657 	{},
658 };
659 
660 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
661 
662 static struct scmi_driver scmi_iiodev_driver = {
663 	.name = "scmi-sensor-iiodev",
664 	.probe = scmi_iio_dev_probe,
665 	.id_table = scmi_id_table,
666 };
667 
668 module_scmi_driver(scmi_iiodev_driver);
669 
670 MODULE_AUTHOR("Jyoti Bhayana <jbhayana@google.com>");
671 MODULE_DESCRIPTION("SCMI IIO Driver");
672 MODULE_LICENSE("GPL v2");
673