1 // SPDX-License-Identifier: GPL-2.0
4 * System Control and Management Interface(SCMI) based IIO sensor driver
6 * Copyright (C) 2021 Google LLC
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>
22 #define SCMI_IIO_NUM_OF_AXIS 3
24 struct scmi_iio_priv {
25 const struct scmi_sensor_proto_ops *sensor_ops;
26 struct scmi_protocol_handle *ph;
27 const struct scmi_sensor_info *sensor_info;
28 struct iio_dev *indio_dev;
29 /* adding one additional channel for timestamp */
30 s64 iio_buf[SCMI_IIO_NUM_OF_AXIS + 1];
31 struct notifier_block sensor_update_nb;
35 static int scmi_iio_sensor_update_cb(struct notifier_block *nb,
36 unsigned long event, void *data)
38 struct scmi_sensor_update_report *sensor_update = data;
39 struct iio_dev *scmi_iio_dev;
40 struct scmi_iio_priv *sensor;
45 if (sensor_update->readings_count == 0)
48 sensor = container_of(nb, struct scmi_iio_priv, sensor_update_nb);
50 for (i = 0; i < sensor_update->readings_count; i++)
51 sensor->iio_buf[i] = sensor_update->readings[i].value;
53 if (!sensor->sensor_info->timestamped) {
54 time_ns = ktime_to_ns(sensor_update->timestamp);
57 * All the axes are supposed to have the same value for timestamp.
58 * We are just using the values from the Axis 0 here.
60 time = sensor_update->readings[0].timestamp;
63 * Timestamp returned by SCMI is in seconds and is equal to
64 * time * power-of-10 multiplier(tstamp_scale) seconds.
65 * Converting the timestamp to nanoseconds below.
67 tstamp_scale = sensor->sensor_info->tstamp_scale +
68 const_ilog2(NSEC_PER_SEC) / const_ilog2(10);
69 if (tstamp_scale < 0) {
70 do_div(time, int_pow(10, abs(tstamp_scale)));
73 time_ns = time * int_pow(10, tstamp_scale);
77 scmi_iio_dev = sensor->indio_dev;
78 iio_push_to_buffers_with_timestamp(scmi_iio_dev, sensor->iio_buf,
83 static int scmi_iio_buffer_preenable(struct iio_dev *iio_dev)
85 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
86 u32 sensor_config = 0;
89 if (sensor->sensor_info->timestamped)
90 sensor_config |= FIELD_PREP(SCMI_SENS_CFG_TSTAMP_ENABLED_MASK,
91 SCMI_SENS_CFG_TSTAMP_ENABLE);
93 sensor_config |= FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
94 SCMI_SENS_CFG_SENSOR_ENABLE);
95 err = sensor->sensor_ops->config_set(sensor->ph,
96 sensor->sensor_info->id,
99 dev_err(&iio_dev->dev, "Error in enabling sensor %s err %d",
100 sensor->sensor_info->name, err);
105 static int scmi_iio_buffer_postdisable(struct iio_dev *iio_dev)
107 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
108 u32 sensor_config = 0;
111 sensor_config |= FIELD_PREP(SCMI_SENS_CFG_SENSOR_ENABLED_MASK,
112 SCMI_SENS_CFG_SENSOR_DISABLE);
113 err = sensor->sensor_ops->config_set(sensor->ph,
114 sensor->sensor_info->id,
117 dev_err(&iio_dev->dev,
118 "Error in disabling sensor %s with err %d",
119 sensor->sensor_info->name, err);
125 static const struct iio_buffer_setup_ops scmi_iio_buffer_ops = {
126 .preenable = scmi_iio_buffer_preenable,
127 .postdisable = scmi_iio_buffer_postdisable,
130 static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
132 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
133 const unsigned long UHZ_PER_HZ = 1000000UL;
134 u64 sec, mult, uHz, sf;
138 int err = sensor->sensor_ops->config_get(sensor->ph,
139 sensor->sensor_info->id,
142 dev_err(&iio_dev->dev,
143 "Error in getting sensor config for sensor %s err %d",
144 sensor->sensor_info->name, err);
148 uHz = val * UHZ_PER_HZ + val2;
151 * The seconds field in the sensor interval in SCMI is 16 bits long
152 * Therefore seconds = 1/Hz <= 0xFFFF. As floating point calculations are
153 * discouraged in the kernel driver code, to calculate the scale factor (sf)
154 * (1* 1000000 * sf)/uHz <= 0xFFFF. Therefore, sf <= (uHz * 0xFFFF)/1000000
155 * To calculate the multiplier,we convert the sf into char string and
156 * count the number of characters
158 sf = (u64)uHz * 0xFFFF;
159 do_div(sf, UHZ_PER_HZ);
160 mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
162 sec = int_pow(10, mult) * UHZ_PER_HZ;
165 dev_err(&iio_dev->dev,
166 "Trying to set invalid sensor update value for sensor %s",
167 sensor->sensor_info->name);
171 sensor_config &= ~SCMI_SENS_CFG_UPDATE_SECS_MASK;
172 sensor_config |= FIELD_PREP(SCMI_SENS_CFG_UPDATE_SECS_MASK, sec);
173 sensor_config &= ~SCMI_SENS_CFG_UPDATE_EXP_MASK;
174 sensor_config |= FIELD_PREP(SCMI_SENS_CFG_UPDATE_EXP_MASK, -mult);
176 if (sensor->sensor_info->timestamped) {
177 sensor_config &= ~SCMI_SENS_CFG_TSTAMP_ENABLED_MASK;
178 sensor_config |= FIELD_PREP(SCMI_SENS_CFG_TSTAMP_ENABLED_MASK,
179 SCMI_SENS_CFG_TSTAMP_ENABLE);
182 sensor_config &= ~SCMI_SENS_CFG_ROUND_MASK;
184 FIELD_PREP(SCMI_SENS_CFG_ROUND_MASK, SCMI_SENS_CFG_ROUND_AUTO);
186 err = sensor->sensor_ops->config_set(sensor->ph,
187 sensor->sensor_info->id,
190 dev_err(&iio_dev->dev,
191 "Error in setting sensor update interval for sensor %s value %u err %d",
192 sensor->sensor_info->name, sensor_config, err);
197 static int scmi_iio_write_raw(struct iio_dev *iio_dev,
198 struct iio_chan_spec const *chan, int val,
204 case IIO_CHAN_INFO_SAMP_FREQ:
205 mutex_lock(&iio_dev->mlock);
206 err = scmi_iio_set_odr_val(iio_dev, val, val2);
207 mutex_unlock(&iio_dev->mlock);
214 static int scmi_iio_read_avail(struct iio_dev *iio_dev,
215 struct iio_chan_spec const *chan,
216 const int **vals, int *type, int *length,
219 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
222 case IIO_CHAN_INFO_SAMP_FREQ:
223 *vals = sensor->freq_avail;
224 *type = IIO_VAL_INT_PLUS_MICRO;
225 *length = sensor->sensor_info->intervals.count * 2;
226 if (sensor->sensor_info->intervals.segmented)
227 return IIO_AVAIL_RANGE;
229 return IIO_AVAIL_LIST;
235 static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
240 rem = do_div(freq, interval_ns);
242 *uhz = rem * 1000000UL;
243 do_div(*uhz, interval_ns);
246 static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
248 u64 sensor_update_interval, sensor_interval_mult, hz, uhz;
249 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
253 int err = sensor->sensor_ops->config_get(sensor->ph,
254 sensor->sensor_info->id,
257 dev_err(&iio_dev->dev,
258 "Error in getting sensor config for sensor %s err %d",
259 sensor->sensor_info->name, err);
263 sensor_update_interval =
264 SCMI_SENS_CFG_GET_UPDATE_SECS(sensor_config) * NSEC_PER_SEC;
266 mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
268 sensor_interval_mult = int_pow(10, abs(mult));
269 do_div(sensor_update_interval, sensor_interval_mult);
271 sensor_interval_mult = int_pow(10, mult);
272 sensor_update_interval =
273 sensor_update_interval * sensor_interval_mult;
276 convert_ns_to_freq(sensor_update_interval, &hz, &uhz);
282 static int scmi_iio_read_raw(struct iio_dev *iio_dev,
283 struct iio_chan_spec const *ch, int *val,
284 int *val2, long mask)
286 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
291 case IIO_CHAN_INFO_SCALE:
292 scale = sensor->sensor_info->axis[ch->scan_index].scale;
295 *val2 = int_pow(10, abs(scale));
296 return IIO_VAL_FRACTIONAL;
298 *val = int_pow(10, scale);
300 case IIO_CHAN_INFO_SAMP_FREQ:
301 ret = scmi_iio_get_odr_val(iio_dev, val, val2);
302 return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
308 static const struct iio_info scmi_iio_info = {
309 .read_raw = scmi_iio_read_raw,
310 .read_avail = scmi_iio_read_avail,
311 .write_raw = scmi_iio_write_raw,
314 static ssize_t scmi_iio_get_raw_available(struct iio_dev *iio_dev,
316 const struct iio_chan_spec *chan,
319 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
321 s64 min_range, max_range;
326 * All the axes are supposed to have the same value for range and resolution.
327 * We are just using the values from the Axis 0 here.
329 if (sensor->sensor_info->axis[0].extended_attrs) {
330 min_range = sensor->sensor_info->axis[0].attrs.min_range;
331 max_range = sensor->sensor_info->axis[0].attrs.max_range;
332 resolution = sensor->sensor_info->axis[0].resolution;
333 exponent = sensor->sensor_info->axis[0].exponent;
334 scale = sensor->sensor_info->axis[0].scale;
337 * To provide the raw value for the resolution to the userspace,
338 * need to divide the resolution exponent by the sensor scale
340 exponent = exponent - scale;
342 rem = do_div(resolution,
343 int_pow(10, abs(exponent))
345 len = scnprintf(buf, PAGE_SIZE,
346 "[%lld %llu.%llu %lld]\n", min_range,
347 resolution, rem, max_range);
349 resolution = resolution * int_pow(10, exponent);
350 len = scnprintf(buf, PAGE_SIZE, "[%lld %llu %lld]\n",
351 min_range, resolution, max_range);
357 static const struct iio_chan_spec_ext_info scmi_iio_ext_info[] = {
359 .name = "raw_available",
360 .read = scmi_iio_get_raw_available,
361 .shared = IIO_SHARED_BY_TYPE,
366 static void scmi_iio_set_timestamp_channel(struct iio_chan_spec *iio_chan,
369 iio_chan->type = IIO_TIMESTAMP;
370 iio_chan->channel = -1;
371 iio_chan->scan_index = scan_index;
372 iio_chan->scan_type.sign = 'u';
373 iio_chan->scan_type.realbits = 64;
374 iio_chan->scan_type.storagebits = 64;
377 static void scmi_iio_set_data_channel(struct iio_chan_spec *iio_chan,
378 enum iio_chan_type type,
379 enum iio_modifier mod, int scan_index)
381 iio_chan->type = type;
382 iio_chan->modified = 1;
383 iio_chan->channel2 = mod;
384 iio_chan->info_mask_separate = BIT(IIO_CHAN_INFO_SCALE);
385 iio_chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ);
386 iio_chan->info_mask_shared_by_type_available =
387 BIT(IIO_CHAN_INFO_SAMP_FREQ);
388 iio_chan->scan_index = scan_index;
389 iio_chan->scan_type.sign = 's';
390 iio_chan->scan_type.realbits = 64;
391 iio_chan->scan_type.storagebits = 64;
392 iio_chan->scan_type.endianness = IIO_LE;
393 iio_chan->ext_info = scmi_iio_ext_info;
396 static int scmi_iio_get_chan_modifier(const char *name,
397 enum iio_modifier *modifier)
404 pch = strrchr(name, '_');
411 *modifier = IIO_MOD_X;
414 *modifier = IIO_MOD_Y;
417 *modifier = IIO_MOD_Z;
424 static int scmi_iio_get_chan_type(u8 scmi_type, enum iio_chan_type *iio_type)
427 case METERS_SEC_SQUARED:
428 *iio_type = IIO_ACCEL;
431 *iio_type = IIO_ANGL_VEL;
438 static u64 scmi_iio_convert_interval_to_ns(u32 val)
440 u64 sensor_update_interval =
441 SCMI_SENS_INTVL_GET_SECS(val) * NSEC_PER_SEC;
442 u64 sensor_interval_mult;
445 mult = SCMI_SENS_INTVL_GET_EXP(val);
447 sensor_interval_mult = int_pow(10, abs(mult));
448 do_div(sensor_update_interval, sensor_interval_mult);
450 sensor_interval_mult = int_pow(10, mult);
451 sensor_update_interval =
452 sensor_update_interval * sensor_interval_mult;
454 return sensor_update_interval;
457 static int scmi_iio_set_sampling_freq_avail(struct iio_dev *iio_dev)
459 u64 cur_interval_ns, low_interval_ns, high_interval_ns, step_size_ns,
461 unsigned int cur_interval, low_interval, high_interval, step_size;
462 struct scmi_iio_priv *sensor = iio_priv(iio_dev);
466 devm_kzalloc(&iio_dev->dev,
467 sizeof(*sensor->freq_avail) *
468 (sensor->sensor_info->intervals.count * 2),
470 if (!sensor->freq_avail)
473 if (sensor->sensor_info->intervals.segmented) {
474 low_interval = sensor->sensor_info->intervals
475 .desc[SCMI_SENS_INTVL_SEGMENT_LOW];
476 low_interval_ns = scmi_iio_convert_interval_to_ns(low_interval);
477 convert_ns_to_freq(low_interval_ns, &hz, &uhz);
478 sensor->freq_avail[0] = hz;
479 sensor->freq_avail[1] = uhz;
481 step_size = sensor->sensor_info->intervals
482 .desc[SCMI_SENS_INTVL_SEGMENT_STEP];
483 step_size_ns = scmi_iio_convert_interval_to_ns(step_size);
484 convert_ns_to_freq(step_size_ns, &hz, &uhz);
485 sensor->freq_avail[2] = hz;
486 sensor->freq_avail[3] = uhz;
488 high_interval = sensor->sensor_info->intervals
489 .desc[SCMI_SENS_INTVL_SEGMENT_HIGH];
491 scmi_iio_convert_interval_to_ns(high_interval);
492 convert_ns_to_freq(high_interval_ns, &hz, &uhz);
493 sensor->freq_avail[4] = hz;
494 sensor->freq_avail[5] = uhz;
496 for (i = 0; i < sensor->sensor_info->intervals.count; i++) {
497 cur_interval = sensor->sensor_info->intervals.desc[i];
499 scmi_iio_convert_interval_to_ns(cur_interval);
500 convert_ns_to_freq(cur_interval_ns, &hz, &uhz);
501 sensor->freq_avail[i * 2] = hz;
502 sensor->freq_avail[i * 2 + 1] = uhz;
508 static struct iio_dev *
509 scmi_alloc_iiodev(struct scmi_device *sdev,
510 const struct scmi_sensor_proto_ops *ops,
511 struct scmi_protocol_handle *ph,
512 const struct scmi_sensor_info *sensor_info)
514 struct iio_chan_spec *iio_channels;
515 struct scmi_iio_priv *sensor;
516 enum iio_modifier modifier;
517 enum iio_chan_type type;
518 struct iio_dev *iiodev;
519 struct device *dev = &sdev->dev;
520 const struct scmi_handle *handle = sdev->handle;
523 iiodev = devm_iio_device_alloc(dev, sizeof(*sensor));
525 return ERR_PTR(-ENOMEM);
527 iiodev->modes = INDIO_DIRECT_MODE;
528 sensor = iio_priv(iiodev);
529 sensor->sensor_ops = ops;
531 sensor->sensor_info = sensor_info;
532 sensor->sensor_update_nb.notifier_call = scmi_iio_sensor_update_cb;
533 sensor->indio_dev = iiodev;
535 /* adding one additional channel for timestamp */
536 iiodev->num_channels = sensor_info->num_axis + 1;
537 iiodev->name = sensor_info->name;
538 iiodev->info = &scmi_iio_info;
542 sizeof(*iio_channels) * (iiodev->num_channels),
545 return ERR_PTR(-ENOMEM);
547 ret = scmi_iio_set_sampling_freq_avail(iiodev);
551 for (i = 0; i < sensor_info->num_axis; i++) {
552 ret = scmi_iio_get_chan_type(sensor_info->axis[i].type, &type);
556 ret = scmi_iio_get_chan_modifier(sensor_info->axis[i].name,
561 scmi_iio_set_data_channel(&iio_channels[i], type, modifier,
562 sensor_info->axis[i].id);
565 ret = handle->notify_ops->devm_event_notifier_register(sdev,
566 SCMI_PROTOCOL_SENSOR, SCMI_EVENT_SENSOR_UPDATE,
567 &sensor->sensor_info->id,
568 &sensor->sensor_update_nb);
570 dev_err(&iiodev->dev,
571 "Error in registering sensor update notifier for sensor %s err %d",
572 sensor->sensor_info->name, ret);
576 scmi_iio_set_timestamp_channel(&iio_channels[i], i);
577 iiodev->channels = iio_channels;
581 static int scmi_iio_dev_probe(struct scmi_device *sdev)
583 const struct scmi_sensor_info *sensor_info;
584 struct scmi_handle *handle = sdev->handle;
585 const struct scmi_sensor_proto_ops *sensor_ops;
586 struct scmi_protocol_handle *ph;
587 struct device *dev = &sdev->dev;
588 struct iio_dev *scmi_iio_dev;
590 int err = -ENODEV, i;
595 sensor_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_SENSOR, &ph);
596 if (IS_ERR(sensor_ops)) {
597 dev_err(dev, "SCMI device has no sensor interface\n");
598 return PTR_ERR(sensor_ops);
601 nr_sensors = sensor_ops->count_get(ph);
603 dev_dbg(dev, "0 sensors found via SCMI bus\n");
607 for (i = 0; i < nr_sensors; i++) {
608 sensor_info = sensor_ops->info_get(ph, i);
610 dev_err(dev, "SCMI sensor %d has missing info\n", i);
614 /* This driver only supports 3-axis accel and gyro, skipping other sensors */
615 if (sensor_info->num_axis != SCMI_IIO_NUM_OF_AXIS)
618 /* This driver only supports 3-axis accel and gyro, skipping other sensors */
619 if (sensor_info->axis[0].type != METERS_SEC_SQUARED &&
620 sensor_info->axis[0].type != RADIANS_SEC)
623 scmi_iio_dev = scmi_alloc_iiodev(sdev, sensor_ops, ph,
625 if (IS_ERR(scmi_iio_dev)) {
627 "failed to allocate IIO device for sensor %s: %ld\n",
628 sensor_info->name, PTR_ERR(scmi_iio_dev));
629 return PTR_ERR(scmi_iio_dev);
632 err = devm_iio_kfifo_buffer_setup(&scmi_iio_dev->dev,
634 INDIO_BUFFER_SOFTWARE,
635 &scmi_iio_buffer_ops);
638 "IIO buffer setup error at sensor %s: %d\n",
639 sensor_info->name, err);
643 err = devm_iio_device_register(dev, scmi_iio_dev);
646 "IIO device registration failed at sensor %s: %d\n",
647 sensor_info->name, err);
654 static const struct scmi_device_id scmi_id_table[] = {
655 { SCMI_PROTOCOL_SENSOR, "iiodev" },
659 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
661 static struct scmi_driver scmi_iiodev_driver = {
662 .name = "scmi-sensor-iiodev",
663 .probe = scmi_iio_dev_probe,
664 .id_table = scmi_id_table,
667 module_scmi_driver(scmi_iiodev_driver);
669 MODULE_AUTHOR("Jyoti Bhayana <jbhayana@google.com>");
670 MODULE_DESCRIPTION("SCMI IIO Driver");
671 MODULE_LICENSE("GPL v2");