1 // SPDX-License-Identifier: GPL-2.0-only
3 * The industrial I/O core
5 * Copyright (c) 2008 Jonathan Cameron
7 * Based on elements of hwmon and input subsystems.
10 #define pr_fmt(fmt) "iio-core: " fmt
12 #include <linux/anon_inodes.h>
13 #include <linux/cdev.h>
14 #include <linux/debugfs.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
18 #include <linux/idr.h>
19 #include <linux/kdev_t.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/poll.h>
24 #include <linux/property.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/wait.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/buffer_impl.h>
31 #include <linux/iio/events.h>
32 #include <linux/iio/iio-opaque.h>
33 #include <linux/iio/iio.h>
34 #include <linux/iio/sysfs.h>
37 #include "iio_core_trigger.h"
39 /* IDA to assign each registered device a unique id */
40 static DEFINE_IDA(iio_ida);
42 static dev_t iio_devt;
44 #define IIO_DEV_MAX 256
45 struct bus_type iio_bus_type = {
48 EXPORT_SYMBOL(iio_bus_type);
50 static struct dentry *iio_debugfs_dentry;
52 static const char * const iio_direction[] = {
57 static const char * const iio_chan_type_name_spec[] = {
58 [IIO_VOLTAGE] = "voltage",
59 [IIO_CURRENT] = "current",
60 [IIO_POWER] = "power",
61 [IIO_ACCEL] = "accel",
62 [IIO_ANGL_VEL] = "anglvel",
64 [IIO_LIGHT] = "illuminance",
65 [IIO_INTENSITY] = "intensity",
66 [IIO_PROXIMITY] = "proximity",
68 [IIO_INCLI] = "incli",
71 [IIO_TIMESTAMP] = "timestamp",
72 [IIO_CAPACITANCE] = "capacitance",
73 [IIO_ALTVOLTAGE] = "altvoltage",
75 [IIO_PRESSURE] = "pressure",
76 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
77 [IIO_ACTIVITY] = "activity",
78 [IIO_STEPS] = "steps",
79 [IIO_ENERGY] = "energy",
80 [IIO_DISTANCE] = "distance",
81 [IIO_VELOCITY] = "velocity",
82 [IIO_CONCENTRATION] = "concentration",
83 [IIO_RESISTANCE] = "resistance",
85 [IIO_UVINDEX] = "uvindex",
86 [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
87 [IIO_COUNT] = "count",
88 [IIO_INDEX] = "index",
89 [IIO_GRAVITY] = "gravity",
90 [IIO_POSITIONRELATIVE] = "positionrelative",
91 [IIO_PHASE] = "phase",
92 [IIO_MASSCONCENTRATION] = "massconcentration",
93 [IIO_DELTA_ANGL] = "deltaangl",
94 [IIO_DELTA_VELOCITY] = "deltavelocity",
95 [IIO_COLORTEMP] = "colortemp",
96 [IIO_CHROMATICITY] = "chromaticity",
99 static const char * const iio_modifier_names[] = {
103 [IIO_MOD_X_AND_Y] = "x&y",
104 [IIO_MOD_X_AND_Z] = "x&z",
105 [IIO_MOD_Y_AND_Z] = "y&z",
106 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
107 [IIO_MOD_X_OR_Y] = "x|y",
108 [IIO_MOD_X_OR_Z] = "x|z",
109 [IIO_MOD_Y_OR_Z] = "y|z",
110 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
111 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
112 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
113 [IIO_MOD_LIGHT_BOTH] = "both",
114 [IIO_MOD_LIGHT_IR] = "ir",
115 [IIO_MOD_LIGHT_CLEAR] = "clear",
116 [IIO_MOD_LIGHT_RED] = "red",
117 [IIO_MOD_LIGHT_GREEN] = "green",
118 [IIO_MOD_LIGHT_BLUE] = "blue",
119 [IIO_MOD_LIGHT_UV] = "uv",
120 [IIO_MOD_LIGHT_UVA] = "uva",
121 [IIO_MOD_LIGHT_UVB] = "uvb",
122 [IIO_MOD_LIGHT_DUV] = "duv",
123 [IIO_MOD_QUATERNION] = "quaternion",
124 [IIO_MOD_TEMP_AMBIENT] = "ambient",
125 [IIO_MOD_TEMP_OBJECT] = "object",
126 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
127 [IIO_MOD_NORTH_TRUE] = "from_north_true",
128 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
129 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
130 [IIO_MOD_RUNNING] = "running",
131 [IIO_MOD_JOGGING] = "jogging",
132 [IIO_MOD_WALKING] = "walking",
133 [IIO_MOD_STILL] = "still",
134 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
137 [IIO_MOD_CO2] = "co2",
138 [IIO_MOD_VOC] = "voc",
139 [IIO_MOD_PM1] = "pm1",
140 [IIO_MOD_PM2P5] = "pm2p5",
141 [IIO_MOD_PM4] = "pm4",
142 [IIO_MOD_PM10] = "pm10",
143 [IIO_MOD_ETHANOL] = "ethanol",
146 [IIO_MOD_LINEAR_X] = "linear_x",
147 [IIO_MOD_LINEAR_Y] = "linear_y",
148 [IIO_MOD_LINEAR_Z] = "linear_z",
149 [IIO_MOD_PITCH] = "pitch",
150 [IIO_MOD_YAW] = "yaw",
151 [IIO_MOD_ROLL] = "roll",
154 /* relies on pairs of these shared then separate */
155 static const char * const iio_chan_info_postfix[] = {
156 [IIO_CHAN_INFO_RAW] = "raw",
157 [IIO_CHAN_INFO_PROCESSED] = "input",
158 [IIO_CHAN_INFO_SCALE] = "scale",
159 [IIO_CHAN_INFO_OFFSET] = "offset",
160 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
161 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
162 [IIO_CHAN_INFO_PEAK] = "peak_raw",
163 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
164 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
165 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
166 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
167 = "filter_low_pass_3db_frequency",
168 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
169 = "filter_high_pass_3db_frequency",
170 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
171 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
172 [IIO_CHAN_INFO_PHASE] = "phase",
173 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
174 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
175 [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative",
176 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
177 [IIO_CHAN_INFO_ENABLE] = "en",
178 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
179 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
180 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
181 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
182 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
183 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
184 [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
185 [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
186 [IIO_CHAN_INFO_ZEROPOINT] = "zeropoint",
187 [IIO_CHAN_INFO_TROUGH] = "trough_raw",
190 * iio_device_id() - query the unique ID for the device
191 * @indio_dev: Device structure whose ID is being queried
193 * The IIO device ID is a unique index used for example for the naming
194 * of the character device /dev/iio\:device[ID].
196 * Returns: Unique ID for the device.
198 int iio_device_id(struct iio_dev *indio_dev)
200 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
202 return iio_dev_opaque->id;
204 EXPORT_SYMBOL_GPL(iio_device_id);
207 * iio_buffer_enabled() - helper function to test if the buffer is enabled
208 * @indio_dev: IIO device structure for device
210 * Returns: True, if the buffer is enabled.
212 bool iio_buffer_enabled(struct iio_dev *indio_dev)
214 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
216 return iio_dev_opaque->currentmode &
217 (INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE |
218 INDIO_BUFFER_TRIGGERED);
220 EXPORT_SYMBOL_GPL(iio_buffer_enabled);
222 #if defined(CONFIG_DEBUG_FS)
224 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
225 * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
227 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
229 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
231 return iio_dev_opaque->debugfs_dentry;
233 EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
237 * iio_find_channel_from_si() - get channel from its scan index
239 * @si: scan index to match
242 * Constant pointer to iio_chan_spec, if scan index matches, NULL on failure.
244 const struct iio_chan_spec
245 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
249 for (i = 0; i < indio_dev->num_channels; i++)
250 if (indio_dev->channels[i].scan_index == si)
251 return &indio_dev->channels[i];
255 /* This turns up an awful lot */
256 ssize_t iio_read_const_attr(struct device *dev,
257 struct device_attribute *attr,
260 return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string);
262 EXPORT_SYMBOL(iio_read_const_attr);
265 * iio_device_set_clock() - Set current timestamping clock for the device
266 * @indio_dev: IIO device structure containing the device
267 * @clock_id: timestamping clock POSIX identifier to set.
269 * Returns: 0 on success, or a negative error code.
271 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
274 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
275 const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
277 ret = mutex_lock_interruptible(&iio_dev_opaque->mlock);
280 if ((ev_int && iio_event_enabled(ev_int)) ||
281 iio_buffer_enabled(indio_dev)) {
282 mutex_unlock(&iio_dev_opaque->mlock);
285 iio_dev_opaque->clock_id = clock_id;
286 mutex_unlock(&iio_dev_opaque->mlock);
290 EXPORT_SYMBOL(iio_device_set_clock);
293 * iio_device_get_clock() - Retrieve current timestamping clock for the device
294 * @indio_dev: IIO device structure containing the device
296 * Returns: Clock ID of the current timestamping clock for the device.
298 clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
300 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
302 return iio_dev_opaque->clock_id;
304 EXPORT_SYMBOL(iio_device_get_clock);
307 * iio_get_time_ns() - utility function to get a time stamp for events etc
310 * Returns: Timestamp of the event in nanoseconds.
312 s64 iio_get_time_ns(const struct iio_dev *indio_dev)
314 struct timespec64 tp;
316 switch (iio_device_get_clock(indio_dev)) {
318 return ktime_get_real_ns();
319 case CLOCK_MONOTONIC:
320 return ktime_get_ns();
321 case CLOCK_MONOTONIC_RAW:
322 return ktime_get_raw_ns();
323 case CLOCK_REALTIME_COARSE:
324 return ktime_to_ns(ktime_get_coarse_real());
325 case CLOCK_MONOTONIC_COARSE:
326 ktime_get_coarse_ts64(&tp);
327 return timespec64_to_ns(&tp);
329 return ktime_get_boottime_ns();
331 return ktime_get_clocktai_ns();
336 EXPORT_SYMBOL(iio_get_time_ns);
338 static int __init iio_init(void)
342 /* Register sysfs bus */
343 ret = bus_register(&iio_bus_type);
345 pr_err("could not register bus type\n");
349 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
351 pr_err("failed to allocate char dev region\n");
352 goto error_unregister_bus_type;
355 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
359 error_unregister_bus_type:
360 bus_unregister(&iio_bus_type);
365 static void __exit iio_exit(void)
368 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
369 bus_unregister(&iio_bus_type);
370 debugfs_remove(iio_debugfs_dentry);
373 #if defined(CONFIG_DEBUG_FS)
374 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
375 size_t count, loff_t *ppos)
377 struct iio_dev *indio_dev = file->private_data;
378 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
379 unsigned int val = 0;
383 return simple_read_from_buffer(userbuf, count, ppos,
384 iio_dev_opaque->read_buf,
385 iio_dev_opaque->read_buf_len);
387 ret = indio_dev->info->debugfs_reg_access(indio_dev,
388 iio_dev_opaque->cached_reg_addr,
391 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
395 iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
396 sizeof(iio_dev_opaque->read_buf),
399 return simple_read_from_buffer(userbuf, count, ppos,
400 iio_dev_opaque->read_buf,
401 iio_dev_opaque->read_buf_len);
404 static ssize_t iio_debugfs_write_reg(struct file *file,
405 const char __user *userbuf, size_t count, loff_t *ppos)
407 struct iio_dev *indio_dev = file->private_data;
408 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
409 unsigned int reg, val;
413 count = min(count, sizeof(buf) - 1);
414 if (copy_from_user(buf, userbuf, count))
419 ret = sscanf(buf, "%i %i", ®, &val);
423 iio_dev_opaque->cached_reg_addr = reg;
426 iio_dev_opaque->cached_reg_addr = reg;
427 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
430 dev_err(indio_dev->dev.parent, "%s: write failed\n",
442 static const struct file_operations iio_debugfs_reg_fops = {
444 .read = iio_debugfs_read_reg,
445 .write = iio_debugfs_write_reg,
448 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
450 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
452 debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
455 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
457 struct iio_dev_opaque *iio_dev_opaque;
459 if (indio_dev->info->debugfs_reg_access == NULL)
462 if (!iio_debugfs_dentry)
465 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
467 iio_dev_opaque->debugfs_dentry =
468 debugfs_create_dir(dev_name(&indio_dev->dev),
471 debugfs_create_file("direct_reg_access", 0644,
472 iio_dev_opaque->debugfs_dentry, indio_dev,
473 &iio_debugfs_reg_fops);
476 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
480 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
483 #endif /* CONFIG_DEBUG_FS */
485 static ssize_t iio_read_channel_ext_info(struct device *dev,
486 struct device_attribute *attr,
489 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
490 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
491 const struct iio_chan_spec_ext_info *ext_info;
493 ext_info = &this_attr->c->ext_info[this_attr->address];
495 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
498 static ssize_t iio_write_channel_ext_info(struct device *dev,
499 struct device_attribute *attr,
500 const char *buf, size_t len)
502 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
503 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
504 const struct iio_chan_spec_ext_info *ext_info;
506 ext_info = &this_attr->c->ext_info[this_attr->address];
508 return ext_info->write(indio_dev, ext_info->private,
509 this_attr->c, buf, len);
512 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
513 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
515 const struct iio_enum *e = (const struct iio_enum *)priv;
522 for (i = 0; i < e->num_items; ++i) {
525 len += sysfs_emit_at(buf, len, "%s ", e->items[i]);
528 /* replace last space with a newline */
533 EXPORT_SYMBOL_GPL(iio_enum_available_read);
535 ssize_t iio_enum_read(struct iio_dev *indio_dev,
536 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
538 const struct iio_enum *e = (const struct iio_enum *)priv;
544 i = e->get(indio_dev, chan);
547 if (i >= e->num_items || !e->items[i])
550 return sysfs_emit(buf, "%s\n", e->items[i]);
552 EXPORT_SYMBOL_GPL(iio_enum_read);
554 ssize_t iio_enum_write(struct iio_dev *indio_dev,
555 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
558 const struct iio_enum *e = (const struct iio_enum *)priv;
564 ret = __sysfs_match_string(e->items, e->num_items, buf);
568 ret = e->set(indio_dev, chan, ret);
569 return ret ? ret : len;
571 EXPORT_SYMBOL_GPL(iio_enum_write);
573 static const struct iio_mount_matrix iio_mount_idmatrix = {
581 static int iio_setup_mount_idmatrix(const struct device *dev,
582 struct iio_mount_matrix *matrix)
584 *matrix = iio_mount_idmatrix;
585 dev_info(dev, "mounting matrix not found: using identity...\n");
589 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
590 const struct iio_chan_spec *chan, char *buf)
592 const struct iio_mount_matrix *mtx;
594 mtx = ((iio_get_mount_matrix_t *)priv)(indio_dev, chan);
599 mtx = &iio_mount_idmatrix;
601 return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
602 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
603 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
604 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
606 EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
609 * iio_read_mount_matrix() - retrieve iio device mounting matrix from
610 * device "mount-matrix" property
611 * @dev: device the mounting matrix property is assigned to
612 * @matrix: where to store retrieved matrix
614 * If device is assigned no mounting matrix property, a default 3x3 identity
615 * matrix will be filled in.
617 * Returns: 0 if success, or a negative error code on failure.
619 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix)
621 size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
624 err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len);
629 /* Invalid number of matrix entries. */
633 /* Invalid matrix declaration format. */
636 /* Matrix was not declared at all: fallback to identity. */
637 return iio_setup_mount_idmatrix(dev, matrix);
639 EXPORT_SYMBOL(iio_read_mount_matrix);
641 static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
642 int size, const int *vals)
646 bool scale_db = false;
650 return sysfs_emit_at(buf, offset, "%d", vals[0]);
651 case IIO_VAL_INT_PLUS_MICRO_DB:
654 case IIO_VAL_INT_PLUS_MICRO:
656 return sysfs_emit_at(buf, offset, "-%d.%06u%s",
657 abs(vals[0]), -vals[1],
658 scale_db ? " dB" : "");
660 return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0],
661 vals[1], scale_db ? " dB" : "");
662 case IIO_VAL_INT_PLUS_NANO:
664 return sysfs_emit_at(buf, offset, "-%d.%09u",
665 abs(vals[0]), -vals[1]);
667 return sysfs_emit_at(buf, offset, "%d.%09u", vals[0],
669 case IIO_VAL_FRACTIONAL:
670 tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
672 tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
673 if ((tmp2 < 0) && (tmp0 == 0))
674 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
676 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
678 case IIO_VAL_FRACTIONAL_LOG2:
679 tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
680 tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
681 if (tmp0 == 0 && tmp2 < 0)
682 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
684 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
686 case IIO_VAL_INT_MULTIPLE:
691 for (i = 0; i < size; ++i)
692 l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]);
696 return sysfs_emit_at(buf, offset, "%c", (char)vals[0]);
698 tmp2 = (s64)((((u64)vals[1]) << 32) | (u32)vals[0]);
699 return sysfs_emit_at(buf, offset, "%lld", tmp2);
706 * iio_format_value() - Formats a IIO value into its string representation
707 * @buf: The buffer to which the formatted value gets written
708 * which is assumed to be big enough (i.e. PAGE_SIZE).
709 * @type: One of the IIO_VAL_* constants. This decides how the val
710 * and val2 parameters are formatted.
711 * @size: Number of IIO value entries contained in vals
712 * @vals: Pointer to the values, exact meaning depends on the
716 * 0 by default, a negative number on failure or the total number of characters
717 * written for a type that belongs to the IIO_VAL_* constant.
719 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
723 len = __iio_format_value(buf, 0, type, size, vals);
724 if (len >= PAGE_SIZE - 1)
727 return len + sysfs_emit_at(buf, len, "\n");
729 EXPORT_SYMBOL_GPL(iio_format_value);
731 static ssize_t iio_read_channel_label(struct device *dev,
732 struct device_attribute *attr,
735 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
736 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
738 if (indio_dev->info->read_label)
739 return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
741 if (this_attr->c->extend_name)
742 return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
747 static ssize_t iio_read_channel_info(struct device *dev,
748 struct device_attribute *attr,
751 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
752 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
753 int vals[INDIO_MAX_RAW_ELEMENTS];
757 if (indio_dev->info->read_raw_multi)
758 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
759 INDIO_MAX_RAW_ELEMENTS,
763 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
764 &vals[0], &vals[1], this_attr->address);
769 return iio_format_value(buf, ret, val_len, vals);
772 static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
773 const char *prefix, const char *suffix)
788 len = sysfs_emit(buf, prefix);
790 for (i = 0; i <= length - stride; i += stride) {
792 len += sysfs_emit_at(buf, len, " ");
793 if (len >= PAGE_SIZE)
797 len += __iio_format_value(buf, len, type, stride, &vals[i]);
798 if (len >= PAGE_SIZE)
802 len += sysfs_emit_at(buf, len, "%s\n", suffix);
807 static ssize_t iio_format_avail_list(char *buf, const int *vals,
808 int type, int length)
811 return iio_format_list(buf, vals, type, length, "", "");
814 static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
819 * length refers to the array size , not the number of elements.
820 * The purpose is to print the range [min , step ,max] so length should
821 * be 3 in case of int, and 6 for other types.
832 return iio_format_list(buf, vals, type, length, "[", "]");
835 static ssize_t iio_read_channel_info_avail(struct device *dev,
836 struct device_attribute *attr,
839 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
840 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
846 ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
847 &vals, &type, &length,
854 return iio_format_avail_list(buf, vals, type, length);
855 case IIO_AVAIL_RANGE:
856 return iio_format_avail_range(buf, vals, type);
863 * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
864 * @str: The string to parse
865 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
866 * @integer: The integer part of the number
867 * @fract: The fractional part of the number
868 * @scale_db: True if this should parse as dB
871 * 0 on success, or a negative error code if the string could not be parsed.
873 static int __iio_str_to_fixpoint(const char *str, int fract_mult,
874 int *integer, int *fract, bool scale_db)
877 bool integer_part = true, negative = false;
879 if (fract_mult == 0) {
882 return kstrtoint(str, 0, integer);
888 } else if (str[0] == '+') {
893 if ('0' <= *str && *str <= '9') {
895 i = i * 10 + *str - '0';
897 f += fract_mult * (*str - '0');
900 } else if (*str == '\n') {
901 if (*(str + 1) == '\0')
904 } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
905 /* Ignore the dB suffix */
906 str += sizeof(" dB") - 1;
908 } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
909 /* Ignore the dB suffix */
910 str += sizeof("dB") - 1;
912 } else if (*str == '.' && integer_part) {
913 integer_part = false;
934 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
935 * @str: The string to parse
936 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
937 * @integer: The integer part of the number
938 * @fract: The fractional part of the number
941 * 0 on success, or a negative error code if the string could not be parsed.
943 int iio_str_to_fixpoint(const char *str, int fract_mult,
944 int *integer, int *fract)
946 return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
948 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
950 static ssize_t iio_write_channel_info(struct device *dev,
951 struct device_attribute *attr,
955 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
956 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
957 int ret, fract_mult = 100000;
958 int integer, fract = 0;
959 bool is_char = false;
960 bool scale_db = false;
962 /* Assumes decimal - precision based on number of digits */
963 if (!indio_dev->info->write_raw)
966 if (indio_dev->info->write_raw_get_fmt)
967 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
968 this_attr->c, this_attr->address)) {
972 case IIO_VAL_INT_PLUS_MICRO_DB:
975 case IIO_VAL_INT_PLUS_MICRO:
978 case IIO_VAL_INT_PLUS_NANO:
979 fract_mult = 100000000;
991 if (sscanf(buf, "%c", &ch) != 1)
995 ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
1001 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
1002 integer, fract, this_attr->address);
1010 int __iio_device_attr_init(struct device_attribute *dev_attr,
1011 const char *postfix,
1012 struct iio_chan_spec const *chan,
1013 ssize_t (*readfunc)(struct device *dev,
1014 struct device_attribute *attr,
1016 ssize_t (*writefunc)(struct device *dev,
1017 struct device_attribute *attr,
1020 enum iio_shared_by shared_by)
1026 sysfs_attr_init(&dev_attr->attr);
1028 /* Build up postfix of <extend_name>_<modifier>_postfix */
1029 if (chan->modified && (shared_by == IIO_SEPARATE)) {
1030 if (chan->extend_name)
1031 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
1032 iio_modifier_names[chan->channel2],
1036 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
1037 iio_modifier_names[chan->channel2],
1040 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
1041 full_postfix = kstrdup(postfix, GFP_KERNEL);
1043 full_postfix = kasprintf(GFP_KERNEL,
1048 if (full_postfix == NULL)
1051 if (chan->differential) { /* Differential can not have modifier */
1052 switch (shared_by) {
1053 case IIO_SHARED_BY_ALL:
1054 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1056 case IIO_SHARED_BY_DIR:
1057 name = kasprintf(GFP_KERNEL, "%s_%s",
1058 iio_direction[chan->output],
1061 case IIO_SHARED_BY_TYPE:
1062 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
1063 iio_direction[chan->output],
1064 iio_chan_type_name_spec[chan->type],
1065 iio_chan_type_name_spec[chan->type],
1069 if (!chan->indexed) {
1070 WARN(1, "Differential channels must be indexed\n");
1072 goto error_free_full_postfix;
1074 name = kasprintf(GFP_KERNEL,
1076 iio_direction[chan->output],
1077 iio_chan_type_name_spec[chan->type],
1079 iio_chan_type_name_spec[chan->type],
1084 } else { /* Single ended */
1085 switch (shared_by) {
1086 case IIO_SHARED_BY_ALL:
1087 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1089 case IIO_SHARED_BY_DIR:
1090 name = kasprintf(GFP_KERNEL, "%s_%s",
1091 iio_direction[chan->output],
1094 case IIO_SHARED_BY_TYPE:
1095 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1096 iio_direction[chan->output],
1097 iio_chan_type_name_spec[chan->type],
1103 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
1104 iio_direction[chan->output],
1105 iio_chan_type_name_spec[chan->type],
1109 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1110 iio_direction[chan->output],
1111 iio_chan_type_name_spec[chan->type],
1118 goto error_free_full_postfix;
1120 dev_attr->attr.name = name;
1123 dev_attr->attr.mode |= 0444;
1124 dev_attr->show = readfunc;
1128 dev_attr->attr.mode |= 0200;
1129 dev_attr->store = writefunc;
1132 error_free_full_postfix:
1133 kfree(full_postfix);
1138 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1140 kfree(dev_attr->attr.name);
1143 int __iio_add_chan_devattr(const char *postfix,
1144 struct iio_chan_spec const *chan,
1145 ssize_t (*readfunc)(struct device *dev,
1146 struct device_attribute *attr,
1148 ssize_t (*writefunc)(struct device *dev,
1149 struct device_attribute *attr,
1153 enum iio_shared_by shared_by,
1155 struct iio_buffer *buffer,
1156 struct list_head *attr_list)
1159 struct iio_dev_attr *iio_attr, *t;
1161 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
1162 if (iio_attr == NULL)
1164 ret = __iio_device_attr_init(&iio_attr->dev_attr,
1166 readfunc, writefunc, shared_by);
1168 goto error_iio_dev_attr_free;
1170 iio_attr->address = mask;
1171 iio_attr->buffer = buffer;
1172 list_for_each_entry(t, attr_list, l)
1173 if (strcmp(t->dev_attr.attr.name,
1174 iio_attr->dev_attr.attr.name) == 0) {
1175 if (shared_by == IIO_SEPARATE)
1176 dev_err(dev, "tried to double register : %s\n",
1177 t->dev_attr.attr.name);
1179 goto error_device_attr_deinit;
1181 list_add(&iio_attr->l, attr_list);
1185 error_device_attr_deinit:
1186 __iio_device_attr_deinit(&iio_attr->dev_attr);
1187 error_iio_dev_attr_free:
1192 static int iio_device_add_channel_label(struct iio_dev *indio_dev,
1193 struct iio_chan_spec const *chan)
1195 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1198 if (!indio_dev->info->read_label && !chan->extend_name)
1201 ret = __iio_add_chan_devattr("label",
1203 &iio_read_channel_label,
1209 &iio_dev_opaque->channel_attr_list);
1216 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1217 struct iio_chan_spec const *chan,
1218 enum iio_shared_by shared_by,
1219 const long *infomask)
1221 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1222 int i, ret, attrcount = 0;
1224 for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1225 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1227 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1229 &iio_read_channel_info,
1230 &iio_write_channel_info,
1235 &iio_dev_opaque->channel_attr_list);
1236 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1246 static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1247 struct iio_chan_spec const *chan,
1248 enum iio_shared_by shared_by,
1249 const long *infomask)
1251 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1252 int i, ret, attrcount = 0;
1253 char *avail_postfix;
1255 for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1256 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1258 avail_postfix = kasprintf(GFP_KERNEL,
1260 iio_chan_info_postfix[i]);
1264 ret = __iio_add_chan_devattr(avail_postfix,
1266 &iio_read_channel_info_avail,
1272 &iio_dev_opaque->channel_attr_list);
1273 kfree(avail_postfix);
1274 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1284 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1285 struct iio_chan_spec const *chan)
1287 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1288 int ret, attrcount = 0;
1289 const struct iio_chan_spec_ext_info *ext_info;
1291 if (chan->channel < 0)
1293 ret = iio_device_add_info_mask_type(indio_dev, chan,
1295 &chan->info_mask_separate);
1300 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1302 &chan->info_mask_separate_available);
1307 ret = iio_device_add_info_mask_type(indio_dev, chan,
1309 &chan->info_mask_shared_by_type);
1314 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1316 &chan->info_mask_shared_by_type_available);
1321 ret = iio_device_add_info_mask_type(indio_dev, chan,
1323 &chan->info_mask_shared_by_dir);
1328 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1330 &chan->info_mask_shared_by_dir_available);
1335 ret = iio_device_add_info_mask_type(indio_dev, chan,
1337 &chan->info_mask_shared_by_all);
1342 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1344 &chan->info_mask_shared_by_all_available);
1349 ret = iio_device_add_channel_label(indio_dev, chan);
1354 if (chan->ext_info) {
1357 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1358 ret = __iio_add_chan_devattr(ext_info->name,
1361 &iio_read_channel_ext_info : NULL,
1363 &iio_write_channel_ext_info : NULL,
1368 &iio_dev_opaque->channel_attr_list);
1370 if (ret == -EBUSY && ext_info->shared)
1384 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1385 * @attr_list: List of IIO device attributes
1387 * This function frees the memory allocated for each of the IIO device
1388 * attributes in the list.
1390 void iio_free_chan_devattr_list(struct list_head *attr_list)
1392 struct iio_dev_attr *p, *n;
1394 list_for_each_entry_safe(p, n, attr_list, l) {
1395 kfree_const(p->dev_attr.attr.name);
1401 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
1404 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1406 return sysfs_emit(buf, "%s\n", indio_dev->name);
1409 static DEVICE_ATTR_RO(name);
1411 static ssize_t label_show(struct device *dev, struct device_attribute *attr,
1414 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1416 return sysfs_emit(buf, "%s\n", indio_dev->label);
1419 static DEVICE_ATTR_RO(label);
1421 static const char * const clock_names[] = {
1422 [CLOCK_REALTIME] = "realtime",
1423 [CLOCK_MONOTONIC] = "monotonic",
1424 [CLOCK_PROCESS_CPUTIME_ID] = "process_cputime_id",
1425 [CLOCK_THREAD_CPUTIME_ID] = "thread_cputime_id",
1426 [CLOCK_MONOTONIC_RAW] = "monotonic_raw",
1427 [CLOCK_REALTIME_COARSE] = "realtime_coarse",
1428 [CLOCK_MONOTONIC_COARSE] = "monotonic_coarse",
1429 [CLOCK_BOOTTIME] = "boottime",
1430 [CLOCK_REALTIME_ALARM] = "realtime_alarm",
1431 [CLOCK_BOOTTIME_ALARM] = "boottime_alarm",
1432 [CLOCK_SGI_CYCLE] = "sgi_cycle",
1433 [CLOCK_TAI] = "tai",
1436 static ssize_t current_timestamp_clock_show(struct device *dev,
1437 struct device_attribute *attr,
1440 const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1441 const clockid_t clk = iio_device_get_clock(indio_dev);
1444 case CLOCK_REALTIME:
1445 case CLOCK_MONOTONIC:
1446 case CLOCK_MONOTONIC_RAW:
1447 case CLOCK_REALTIME_COARSE:
1448 case CLOCK_MONOTONIC_COARSE:
1449 case CLOCK_BOOTTIME:
1456 return sysfs_emit(buf, "%s\n", clock_names[clk]);
1459 static ssize_t current_timestamp_clock_store(struct device *dev,
1460 struct device_attribute *attr,
1461 const char *buf, size_t len)
1466 ret = sysfs_match_string(clock_names, buf);
1472 case CLOCK_REALTIME:
1473 case CLOCK_MONOTONIC:
1474 case CLOCK_MONOTONIC_RAW:
1475 case CLOCK_REALTIME_COARSE:
1476 case CLOCK_MONOTONIC_COARSE:
1477 case CLOCK_BOOTTIME:
1484 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1491 int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
1492 const struct attribute_group *group)
1494 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1495 const struct attribute_group **new, **old = iio_dev_opaque->groups;
1496 unsigned int cnt = iio_dev_opaque->groupcounter;
1498 new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL);
1502 new[iio_dev_opaque->groupcounter++] = group;
1503 new[iio_dev_opaque->groupcounter] = NULL;
1505 iio_dev_opaque->groups = new;
1510 static DEVICE_ATTR_RW(current_timestamp_clock);
1512 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1514 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1515 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1516 struct iio_dev_attr *p;
1517 struct attribute **attr, *clk = NULL;
1519 /* First count elements in any existing group */
1520 if (indio_dev->info->attrs) {
1521 attr = indio_dev->info->attrs->attrs;
1522 while (*attr++ != NULL)
1525 attrcount = attrcount_orig;
1527 * New channel registration method - relies on the fact a group does
1528 * not need to be initialized if its name is NULL.
1530 if (indio_dev->channels)
1531 for (i = 0; i < indio_dev->num_channels; i++) {
1532 const struct iio_chan_spec *chan =
1533 &indio_dev->channels[i];
1535 if (chan->type == IIO_TIMESTAMP)
1536 clk = &dev_attr_current_timestamp_clock.attr;
1538 ret = iio_device_add_channel_sysfs(indio_dev, chan);
1540 goto error_clear_attrs;
1544 if (iio_dev_opaque->event_interface)
1545 clk = &dev_attr_current_timestamp_clock.attr;
1547 if (indio_dev->name)
1549 if (indio_dev->label)
1554 iio_dev_opaque->chan_attr_group.attrs =
1555 kcalloc(attrcount + 1,
1556 sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1558 if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
1560 goto error_clear_attrs;
1562 /* Copy across original attributes, and point to original binary attributes */
1563 if (indio_dev->info->attrs) {
1564 memcpy(iio_dev_opaque->chan_attr_group.attrs,
1565 indio_dev->info->attrs->attrs,
1566 sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
1568 iio_dev_opaque->chan_attr_group.is_visible =
1569 indio_dev->info->attrs->is_visible;
1570 iio_dev_opaque->chan_attr_group.bin_attrs =
1571 indio_dev->info->attrs->bin_attrs;
1573 attrn = attrcount_orig;
1574 /* Add all elements from the list. */
1575 list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1576 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1577 if (indio_dev->name)
1578 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1579 if (indio_dev->label)
1580 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1582 iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
1584 ret = iio_device_register_sysfs_group(indio_dev,
1585 &iio_dev_opaque->chan_attr_group);
1587 goto error_free_chan_attrs;
1591 error_free_chan_attrs:
1592 kfree(iio_dev_opaque->chan_attr_group.attrs);
1593 iio_dev_opaque->chan_attr_group.attrs = NULL;
1595 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1600 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1602 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1604 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1605 kfree(iio_dev_opaque->chan_attr_group.attrs);
1606 iio_dev_opaque->chan_attr_group.attrs = NULL;
1607 kfree(iio_dev_opaque->groups);
1608 iio_dev_opaque->groups = NULL;
1611 static void iio_dev_release(struct device *device)
1613 struct iio_dev *indio_dev = dev_to_iio_dev(device);
1614 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1616 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1617 iio_device_unregister_trigger_consumer(indio_dev);
1618 iio_device_unregister_eventset(indio_dev);
1619 iio_device_unregister_sysfs(indio_dev);
1621 iio_device_detach_buffers(indio_dev);
1623 lockdep_unregister_key(&iio_dev_opaque->mlock_key);
1625 ida_free(&iio_ida, iio_dev_opaque->id);
1626 kfree(iio_dev_opaque);
1629 const struct device_type iio_device_type = {
1630 .name = "iio_device",
1631 .release = iio_dev_release,
1635 * iio_device_alloc() - allocate an iio_dev from a driver
1636 * @parent: Parent device.
1637 * @sizeof_priv: Space to allocate for private structure.
1640 * Pointer to allocated iio_dev on success, NULL on failure.
1642 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
1644 struct iio_dev_opaque *iio_dev_opaque;
1645 struct iio_dev *indio_dev;
1648 alloc_size = sizeof(struct iio_dev_opaque);
1650 alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
1651 alloc_size += sizeof_priv;
1654 iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1655 if (!iio_dev_opaque)
1658 indio_dev = &iio_dev_opaque->indio_dev;
1659 indio_dev->priv = (char *)iio_dev_opaque +
1660 ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN);
1662 indio_dev->dev.parent = parent;
1663 indio_dev->dev.type = &iio_device_type;
1664 indio_dev->dev.bus = &iio_bus_type;
1665 device_initialize(&indio_dev->dev);
1666 mutex_init(&iio_dev_opaque->mlock);
1667 mutex_init(&iio_dev_opaque->info_exist_lock);
1668 INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
1670 iio_dev_opaque->id = ida_alloc(&iio_ida, GFP_KERNEL);
1671 if (iio_dev_opaque->id < 0) {
1672 /* cannot use a dev_err as the name isn't available */
1673 pr_err("failed to get device id\n");
1674 kfree(iio_dev_opaque);
1678 if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) {
1679 ida_free(&iio_ida, iio_dev_opaque->id);
1680 kfree(iio_dev_opaque);
1684 INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
1685 INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
1687 lockdep_register_key(&iio_dev_opaque->mlock_key);
1688 lockdep_set_class(&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key);
1692 EXPORT_SYMBOL(iio_device_alloc);
1695 * iio_device_free() - free an iio_dev from a driver
1696 * @dev: the iio_dev associated with the device
1698 void iio_device_free(struct iio_dev *dev)
1701 put_device(&dev->dev);
1703 EXPORT_SYMBOL(iio_device_free);
1705 static void devm_iio_device_release(void *iio_dev)
1707 iio_device_free(iio_dev);
1711 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1712 * @parent: Device to allocate iio_dev for, and parent for this IIO device
1713 * @sizeof_priv: Space to allocate for private structure.
1715 * Managed iio_device_alloc. iio_dev allocated with this function is
1716 * automatically freed on driver detach.
1719 * Pointer to allocated iio_dev on success, NULL on failure.
1721 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
1723 struct iio_dev *iio_dev;
1726 iio_dev = iio_device_alloc(parent, sizeof_priv);
1730 ret = devm_add_action_or_reset(parent, devm_iio_device_release,
1737 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1740 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1741 * @inode: Inode structure for identifying the device in the file system
1742 * @filp: File structure for iio device used to keep and later access
1745 * Returns: 0 on success or -EBUSY if the device is already opened
1747 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1749 struct iio_dev_opaque *iio_dev_opaque =
1750 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1751 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1752 struct iio_dev_buffer_pair *ib;
1754 if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags))
1757 iio_device_get(indio_dev);
1759 ib = kmalloc(sizeof(*ib), GFP_KERNEL);
1761 iio_device_put(indio_dev);
1762 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1766 ib->indio_dev = indio_dev;
1767 ib->buffer = indio_dev->buffer;
1769 filp->private_data = ib;
1775 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1776 * @inode: Inode structure pointer for the char device
1777 * @filp: File structure pointer for the char device
1779 * Returns: 0 for successful release.
1781 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1783 struct iio_dev_buffer_pair *ib = filp->private_data;
1784 struct iio_dev_opaque *iio_dev_opaque =
1785 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1786 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1789 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1790 iio_device_put(indio_dev);
1795 void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
1796 struct iio_ioctl_handler *h)
1798 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1800 list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers);
1803 void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h)
1805 list_del(&h->entry);
1808 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1810 struct iio_dev_buffer_pair *ib = filp->private_data;
1811 struct iio_dev *indio_dev = ib->indio_dev;
1812 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1813 struct iio_ioctl_handler *h;
1816 mutex_lock(&iio_dev_opaque->info_exist_lock);
1819 * The NULL check here is required to prevent crashing when a device
1820 * is being removed while userspace would still have open file handles
1821 * to try to access this device.
1823 if (!indio_dev->info)
1826 list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
1827 ret = h->ioctl(indio_dev, filp, cmd, arg);
1828 if (ret != IIO_IOCTL_UNHANDLED)
1832 if (ret == IIO_IOCTL_UNHANDLED)
1836 mutex_unlock(&iio_dev_opaque->info_exist_lock);
1841 static const struct file_operations iio_buffer_fileops = {
1842 .owner = THIS_MODULE,
1843 .llseek = noop_llseek,
1844 .read = iio_buffer_read_outer_addr,
1845 .write = iio_buffer_write_outer_addr,
1846 .poll = iio_buffer_poll_addr,
1847 .unlocked_ioctl = iio_ioctl,
1848 .compat_ioctl = compat_ptr_ioctl,
1849 .open = iio_chrdev_open,
1850 .release = iio_chrdev_release,
1853 static const struct file_operations iio_event_fileops = {
1854 .owner = THIS_MODULE,
1855 .llseek = noop_llseek,
1856 .unlocked_ioctl = iio_ioctl,
1857 .compat_ioctl = compat_ptr_ioctl,
1858 .open = iio_chrdev_open,
1859 .release = iio_chrdev_release,
1862 static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1865 const struct iio_chan_spec *channels = indio_dev->channels;
1867 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1870 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1871 if (channels[i].scan_index < 0)
1873 for (j = i + 1; j < indio_dev->num_channels; j++)
1874 if (channels[i].scan_index == channels[j].scan_index) {
1875 dev_err(&indio_dev->dev,
1876 "Duplicate scan index %d\n",
1877 channels[i].scan_index);
1885 static int iio_check_extended_name(const struct iio_dev *indio_dev)
1889 if (!indio_dev->info->read_label)
1892 for (i = 0; i < indio_dev->num_channels; i++) {
1893 if (indio_dev->channels[i].extend_name) {
1894 dev_err(&indio_dev->dev,
1895 "Cannot use labels and extend_name at the same time\n");
1903 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1905 static void iio_sanity_check_avail_scan_masks(struct iio_dev *indio_dev)
1907 unsigned int num_masks, masklength, longs_per_mask;
1908 const unsigned long *av_masks;
1911 av_masks = indio_dev->available_scan_masks;
1912 masklength = indio_dev->masklength;
1913 longs_per_mask = BITS_TO_LONGS(masklength);
1916 * The code determining how many available_scan_masks is in the array
1917 * will be assuming the end of masks when first long with all bits
1918 * zeroed is encountered. This is incorrect for masks where mask
1919 * consists of more than one long, and where some of the available masks
1920 * has long worth of bits zeroed (but has subsequent bit(s) set). This
1921 * is a safety measure against bug where array of masks is terminated by
1922 * a single zero while mask width is greater than width of a long.
1924 if (longs_per_mask > 1)
1925 dev_warn(indio_dev->dev.parent,
1926 "multi long available scan masks not fully supported\n");
1928 if (bitmap_empty(av_masks, masklength))
1929 dev_warn(indio_dev->dev.parent, "empty scan mask\n");
1931 for (num_masks = 0; *av_masks; num_masks++)
1932 av_masks += longs_per_mask;
1937 av_masks = indio_dev->available_scan_masks;
1940 * Go through all the masks from first to one before the last, and see
1941 * that no mask found later from the available_scan_masks array is a
1942 * subset of mask found earlier. If this happens, then the mask found
1943 * later will never get used because scanning the array is stopped when
1944 * the first suitable mask is found. Drivers should order the array of
1945 * available masks in the order of preference (presumably the least
1946 * costy to access masks first).
1948 for (i = 0; i < num_masks - 1; i++) {
1949 const unsigned long *mask1;
1952 mask1 = av_masks + i * longs_per_mask;
1953 for (j = i + 1; j < num_masks; j++) {
1954 const unsigned long *mask2;
1956 mask2 = av_masks + j * longs_per_mask;
1957 if (bitmap_subset(mask2, mask1, masklength))
1958 dev_warn(indio_dev->dev.parent,
1959 "available_scan_mask %d subset of %d. Never used\n",
1965 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1967 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1968 struct fwnode_handle *fwnode = NULL;
1971 if (!indio_dev->info)
1974 iio_dev_opaque->driver_module = this_mod;
1976 /* If the calling driver did not initialize firmware node, do it here */
1977 if (dev_fwnode(&indio_dev->dev))
1978 fwnode = dev_fwnode(&indio_dev->dev);
1979 /* The default dummy IIO device has no parent */
1980 else if (indio_dev->dev.parent)
1981 fwnode = dev_fwnode(indio_dev->dev.parent);
1982 device_set_node(&indio_dev->dev, fwnode);
1984 fwnode_property_read_string(fwnode, "label", &indio_dev->label);
1986 ret = iio_check_unique_scan_index(indio_dev);
1990 ret = iio_check_extended_name(indio_dev);
1994 iio_device_register_debugfs(indio_dev);
1996 ret = iio_buffers_alloc_sysfs_and_mask(indio_dev);
1998 dev_err(indio_dev->dev.parent,
1999 "Failed to create buffer sysfs interfaces\n");
2000 goto error_unreg_debugfs;
2003 if (indio_dev->available_scan_masks)
2004 iio_sanity_check_avail_scan_masks(indio_dev);
2006 ret = iio_device_register_sysfs(indio_dev);
2008 dev_err(indio_dev->dev.parent,
2009 "Failed to register sysfs interfaces\n");
2010 goto error_buffer_free_sysfs;
2012 ret = iio_device_register_eventset(indio_dev);
2014 dev_err(indio_dev->dev.parent,
2015 "Failed to register event set\n");
2016 goto error_free_sysfs;
2018 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
2019 iio_device_register_trigger_consumer(indio_dev);
2021 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
2022 indio_dev->setup_ops == NULL)
2023 indio_dev->setup_ops = &noop_ring_setup_ops;
2025 if (iio_dev_opaque->attached_buffers_cnt)
2026 cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops);
2027 else if (iio_dev_opaque->event_interface)
2028 cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops);
2030 if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) {
2031 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id);
2032 iio_dev_opaque->chrdev.owner = this_mod;
2035 /* assign device groups now; they should be all registered now */
2036 indio_dev->dev.groups = iio_dev_opaque->groups;
2038 ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev);
2040 goto error_unreg_eventset;
2044 error_unreg_eventset:
2045 iio_device_unregister_eventset(indio_dev);
2047 iio_device_unregister_sysfs(indio_dev);
2048 error_buffer_free_sysfs:
2049 iio_buffers_free_sysfs_and_mask(indio_dev);
2050 error_unreg_debugfs:
2051 iio_device_unregister_debugfs(indio_dev);
2054 EXPORT_SYMBOL(__iio_device_register);
2057 * iio_device_unregister() - unregister a device from the IIO subsystem
2058 * @indio_dev: Device structure representing the device.
2060 void iio_device_unregister(struct iio_dev *indio_dev)
2062 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2064 cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev);
2066 mutex_lock(&iio_dev_opaque->info_exist_lock);
2068 iio_device_unregister_debugfs(indio_dev);
2070 iio_disable_all_buffers(indio_dev);
2072 indio_dev->info = NULL;
2074 iio_device_wakeup_eventset(indio_dev);
2075 iio_buffer_wakeup_poll(indio_dev);
2077 mutex_unlock(&iio_dev_opaque->info_exist_lock);
2079 iio_buffers_free_sysfs_and_mask(indio_dev);
2081 EXPORT_SYMBOL(iio_device_unregister);
2083 static void devm_iio_device_unreg(void *indio_dev)
2085 iio_device_unregister(indio_dev);
2088 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
2089 struct module *this_mod)
2093 ret = __iio_device_register(indio_dev, this_mod);
2097 return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev);
2099 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
2102 * iio_device_claim_direct_mode - Keep device in direct mode
2103 * @indio_dev: the iio_dev associated with the device
2105 * If the device is in direct mode it is guaranteed to stay
2106 * that way until iio_device_release_direct_mode() is called.
2108 * Use with iio_device_release_direct_mode()
2110 * Returns: 0 on success, -EBUSY on failure.
2112 int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
2114 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2116 mutex_lock(&iio_dev_opaque->mlock);
2118 if (iio_buffer_enabled(indio_dev)) {
2119 mutex_unlock(&iio_dev_opaque->mlock);
2124 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
2127 * iio_device_release_direct_mode - releases claim on direct mode
2128 * @indio_dev: the iio_dev associated with the device
2130 * Release the claim. Device is no longer guaranteed to stay
2133 * Use with iio_device_claim_direct_mode()
2135 void iio_device_release_direct_mode(struct iio_dev *indio_dev)
2137 mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
2139 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
2142 * iio_device_claim_buffer_mode - Keep device in buffer mode
2143 * @indio_dev: the iio_dev associated with the device
2145 * If the device is in buffer mode it is guaranteed to stay
2146 * that way until iio_device_release_buffer_mode() is called.
2148 * Use with iio_device_release_buffer_mode().
2150 * Returns: 0 on success, -EBUSY on failure.
2152 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev)
2154 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2156 mutex_lock(&iio_dev_opaque->mlock);
2158 if (iio_buffer_enabled(indio_dev))
2161 mutex_unlock(&iio_dev_opaque->mlock);
2164 EXPORT_SYMBOL_GPL(iio_device_claim_buffer_mode);
2167 * iio_device_release_buffer_mode - releases claim on buffer mode
2168 * @indio_dev: the iio_dev associated with the device
2170 * Release the claim. Device is no longer guaranteed to stay
2173 * Use with iio_device_claim_buffer_mode().
2175 void iio_device_release_buffer_mode(struct iio_dev *indio_dev)
2177 mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
2179 EXPORT_SYMBOL_GPL(iio_device_release_buffer_mode);
2182 * iio_device_get_current_mode() - helper function providing read-only access to
2183 * the opaque @currentmode variable
2184 * @indio_dev: IIO device structure for device
2186 int iio_device_get_current_mode(struct iio_dev *indio_dev)
2188 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2190 return iio_dev_opaque->currentmode;
2192 EXPORT_SYMBOL_GPL(iio_device_get_current_mode);
2194 subsys_initcall(iio_init);
2195 module_exit(iio_exit);
2197 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
2198 MODULE_DESCRIPTION("Industrial I/O core");
2199 MODULE_LICENSE("GPL");