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",
95 static const char * const iio_modifier_names[] = {
99 [IIO_MOD_X_AND_Y] = "x&y",
100 [IIO_MOD_X_AND_Z] = "x&z",
101 [IIO_MOD_Y_AND_Z] = "y&z",
102 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
103 [IIO_MOD_X_OR_Y] = "x|y",
104 [IIO_MOD_X_OR_Z] = "x|z",
105 [IIO_MOD_Y_OR_Z] = "y|z",
106 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
107 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
108 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
109 [IIO_MOD_LIGHT_BOTH] = "both",
110 [IIO_MOD_LIGHT_IR] = "ir",
111 [IIO_MOD_LIGHT_CLEAR] = "clear",
112 [IIO_MOD_LIGHT_RED] = "red",
113 [IIO_MOD_LIGHT_GREEN] = "green",
114 [IIO_MOD_LIGHT_BLUE] = "blue",
115 [IIO_MOD_LIGHT_UV] = "uv",
116 [IIO_MOD_LIGHT_DUV] = "duv",
117 [IIO_MOD_QUATERNION] = "quaternion",
118 [IIO_MOD_TEMP_AMBIENT] = "ambient",
119 [IIO_MOD_TEMP_OBJECT] = "object",
120 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
121 [IIO_MOD_NORTH_TRUE] = "from_north_true",
122 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
123 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
124 [IIO_MOD_RUNNING] = "running",
125 [IIO_MOD_JOGGING] = "jogging",
126 [IIO_MOD_WALKING] = "walking",
127 [IIO_MOD_STILL] = "still",
128 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
131 [IIO_MOD_CO2] = "co2",
132 [IIO_MOD_VOC] = "voc",
133 [IIO_MOD_PM1] = "pm1",
134 [IIO_MOD_PM2P5] = "pm2p5",
135 [IIO_MOD_PM4] = "pm4",
136 [IIO_MOD_PM10] = "pm10",
137 [IIO_MOD_ETHANOL] = "ethanol",
140 [IIO_MOD_LINEAR_X] = "linear_x",
141 [IIO_MOD_LINEAR_Y] = "linear_y",
142 [IIO_MOD_LINEAR_Z] = "linear_z",
143 [IIO_MOD_PITCH] = "pitch",
144 [IIO_MOD_YAW] = "yaw",
145 [IIO_MOD_ROLL] = "roll",
148 /* relies on pairs of these shared then separate */
149 static const char * const iio_chan_info_postfix[] = {
150 [IIO_CHAN_INFO_RAW] = "raw",
151 [IIO_CHAN_INFO_PROCESSED] = "input",
152 [IIO_CHAN_INFO_SCALE] = "scale",
153 [IIO_CHAN_INFO_OFFSET] = "offset",
154 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
155 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
156 [IIO_CHAN_INFO_PEAK] = "peak_raw",
157 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
158 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
159 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
160 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
161 = "filter_low_pass_3db_frequency",
162 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
163 = "filter_high_pass_3db_frequency",
164 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
165 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
166 [IIO_CHAN_INFO_PHASE] = "phase",
167 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
168 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
169 [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative",
170 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
171 [IIO_CHAN_INFO_ENABLE] = "en",
172 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
173 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
174 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
175 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
176 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
177 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
178 [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
179 [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
180 [IIO_CHAN_INFO_ZEROPOINT] = "zeropoint",
183 * iio_device_id() - query the unique ID for the device
184 * @indio_dev: Device structure whose ID is being queried
186 * The IIO device ID is a unique index used for example for the naming
187 * of the character device /dev/iio\:device[ID].
189 * Returns: Unique ID for the device.
191 int iio_device_id(struct iio_dev *indio_dev)
193 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
195 return iio_dev_opaque->id;
197 EXPORT_SYMBOL_GPL(iio_device_id);
200 * iio_buffer_enabled() - helper function to test if the buffer is enabled
201 * @indio_dev: IIO device structure for device
203 * Returns: True, if the buffer is enabled.
205 bool iio_buffer_enabled(struct iio_dev *indio_dev)
207 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
209 return iio_dev_opaque->currentmode &
210 (INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE |
211 INDIO_BUFFER_TRIGGERED);
213 EXPORT_SYMBOL_GPL(iio_buffer_enabled);
215 #if defined(CONFIG_DEBUG_FS)
217 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
218 * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
220 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
222 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
224 return iio_dev_opaque->debugfs_dentry;
226 EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
230 * iio_find_channel_from_si() - get channel from its scan index
232 * @si: scan index to match
235 * Constant pointer to iio_chan_spec, if scan index matches, NULL on failure.
237 const struct iio_chan_spec
238 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
242 for (i = 0; i < indio_dev->num_channels; i++)
243 if (indio_dev->channels[i].scan_index == si)
244 return &indio_dev->channels[i];
248 /* This turns up an awful lot */
249 ssize_t iio_read_const_attr(struct device *dev,
250 struct device_attribute *attr,
253 return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string);
255 EXPORT_SYMBOL(iio_read_const_attr);
258 * iio_device_set_clock() - Set current timestamping clock for the device
259 * @indio_dev: IIO device structure containing the device
260 * @clock_id: timestamping clock POSIX identifier to set.
262 * Returns: 0 on success, or a negative error code.
264 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
267 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
268 const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
270 ret = mutex_lock_interruptible(&iio_dev_opaque->mlock);
273 if ((ev_int && iio_event_enabled(ev_int)) ||
274 iio_buffer_enabled(indio_dev)) {
275 mutex_unlock(&iio_dev_opaque->mlock);
278 iio_dev_opaque->clock_id = clock_id;
279 mutex_unlock(&iio_dev_opaque->mlock);
283 EXPORT_SYMBOL(iio_device_set_clock);
286 * iio_device_get_clock() - Retrieve current timestamping clock for the device
287 * @indio_dev: IIO device structure containing the device
289 * Returns: Clock ID of the current timestamping clock for the device.
291 clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
293 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
295 return iio_dev_opaque->clock_id;
297 EXPORT_SYMBOL(iio_device_get_clock);
300 * iio_get_time_ns() - utility function to get a time stamp for events etc
303 * Returns: Timestamp of the event in nanoseconds.
305 s64 iio_get_time_ns(const struct iio_dev *indio_dev)
307 struct timespec64 tp;
309 switch (iio_device_get_clock(indio_dev)) {
311 return ktime_get_real_ns();
312 case CLOCK_MONOTONIC:
313 return ktime_get_ns();
314 case CLOCK_MONOTONIC_RAW:
315 return ktime_get_raw_ns();
316 case CLOCK_REALTIME_COARSE:
317 return ktime_to_ns(ktime_get_coarse_real());
318 case CLOCK_MONOTONIC_COARSE:
319 ktime_get_coarse_ts64(&tp);
320 return timespec64_to_ns(&tp);
322 return ktime_get_boottime_ns();
324 return ktime_get_clocktai_ns();
329 EXPORT_SYMBOL(iio_get_time_ns);
331 static int __init iio_init(void)
335 /* Register sysfs bus */
336 ret = bus_register(&iio_bus_type);
338 pr_err("could not register bus type\n");
342 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
344 pr_err("failed to allocate char dev region\n");
345 goto error_unregister_bus_type;
348 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
352 error_unregister_bus_type:
353 bus_unregister(&iio_bus_type);
358 static void __exit iio_exit(void)
361 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
362 bus_unregister(&iio_bus_type);
363 debugfs_remove(iio_debugfs_dentry);
366 #if defined(CONFIG_DEBUG_FS)
367 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
368 size_t count, loff_t *ppos)
370 struct iio_dev *indio_dev = file->private_data;
371 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
372 unsigned int val = 0;
376 return simple_read_from_buffer(userbuf, count, ppos,
377 iio_dev_opaque->read_buf,
378 iio_dev_opaque->read_buf_len);
380 ret = indio_dev->info->debugfs_reg_access(indio_dev,
381 iio_dev_opaque->cached_reg_addr,
384 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
388 iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
389 sizeof(iio_dev_opaque->read_buf),
392 return simple_read_from_buffer(userbuf, count, ppos,
393 iio_dev_opaque->read_buf,
394 iio_dev_opaque->read_buf_len);
397 static ssize_t iio_debugfs_write_reg(struct file *file,
398 const char __user *userbuf, size_t count, loff_t *ppos)
400 struct iio_dev *indio_dev = file->private_data;
401 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
402 unsigned int reg, val;
406 count = min(count, sizeof(buf) - 1);
407 if (copy_from_user(buf, userbuf, count))
412 ret = sscanf(buf, "%i %i", ®, &val);
416 iio_dev_opaque->cached_reg_addr = reg;
419 iio_dev_opaque->cached_reg_addr = reg;
420 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
423 dev_err(indio_dev->dev.parent, "%s: write failed\n",
435 static const struct file_operations iio_debugfs_reg_fops = {
437 .read = iio_debugfs_read_reg,
438 .write = iio_debugfs_write_reg,
441 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
443 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
445 debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
448 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
450 struct iio_dev_opaque *iio_dev_opaque;
452 if (indio_dev->info->debugfs_reg_access == NULL)
455 if (!iio_debugfs_dentry)
458 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
460 iio_dev_opaque->debugfs_dentry =
461 debugfs_create_dir(dev_name(&indio_dev->dev),
464 debugfs_create_file("direct_reg_access", 0644,
465 iio_dev_opaque->debugfs_dentry, indio_dev,
466 &iio_debugfs_reg_fops);
469 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
473 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
476 #endif /* CONFIG_DEBUG_FS */
478 static ssize_t iio_read_channel_ext_info(struct device *dev,
479 struct device_attribute *attr,
482 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
483 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
484 const struct iio_chan_spec_ext_info *ext_info;
486 ext_info = &this_attr->c->ext_info[this_attr->address];
488 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
491 static ssize_t iio_write_channel_ext_info(struct device *dev,
492 struct device_attribute *attr,
493 const char *buf, size_t len)
495 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
496 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
497 const struct iio_chan_spec_ext_info *ext_info;
499 ext_info = &this_attr->c->ext_info[this_attr->address];
501 return ext_info->write(indio_dev, ext_info->private,
502 this_attr->c, buf, len);
505 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
506 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
508 const struct iio_enum *e = (const struct iio_enum *)priv;
515 for (i = 0; i < e->num_items; ++i) {
518 len += sysfs_emit_at(buf, len, "%s ", e->items[i]);
521 /* replace last space with a newline */
526 EXPORT_SYMBOL_GPL(iio_enum_available_read);
528 ssize_t iio_enum_read(struct iio_dev *indio_dev,
529 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
531 const struct iio_enum *e = (const struct iio_enum *)priv;
537 i = e->get(indio_dev, chan);
540 if (i >= e->num_items || !e->items[i])
543 return sysfs_emit(buf, "%s\n", e->items[i]);
545 EXPORT_SYMBOL_GPL(iio_enum_read);
547 ssize_t iio_enum_write(struct iio_dev *indio_dev,
548 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
551 const struct iio_enum *e = (const struct iio_enum *)priv;
557 ret = __sysfs_match_string(e->items, e->num_items, buf);
561 ret = e->set(indio_dev, chan, ret);
562 return ret ? ret : len;
564 EXPORT_SYMBOL_GPL(iio_enum_write);
566 static const struct iio_mount_matrix iio_mount_idmatrix = {
574 static int iio_setup_mount_idmatrix(const struct device *dev,
575 struct iio_mount_matrix *matrix)
577 *matrix = iio_mount_idmatrix;
578 dev_info(dev, "mounting matrix not found: using identity...\n");
582 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
583 const struct iio_chan_spec *chan, char *buf)
585 const struct iio_mount_matrix *mtx;
587 mtx = ((iio_get_mount_matrix_t *)priv)(indio_dev, chan);
592 mtx = &iio_mount_idmatrix;
594 return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
595 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
596 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
597 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
599 EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
602 * iio_read_mount_matrix() - retrieve iio device mounting matrix from
603 * device "mount-matrix" property
604 * @dev: device the mounting matrix property is assigned to
605 * @matrix: where to store retrieved matrix
607 * If device is assigned no mounting matrix property, a default 3x3 identity
608 * matrix will be filled in.
610 * Returns: 0 if success, or a negative error code on failure.
612 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix)
614 size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
617 err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len);
622 /* Invalid number of matrix entries. */
626 /* Invalid matrix declaration format. */
629 /* Matrix was not declared at all: fallback to identity. */
630 return iio_setup_mount_idmatrix(dev, matrix);
632 EXPORT_SYMBOL(iio_read_mount_matrix);
634 static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
635 int size, const int *vals)
639 bool scale_db = false;
643 return sysfs_emit_at(buf, offset, "%d", vals[0]);
644 case IIO_VAL_INT_PLUS_MICRO_DB:
647 case IIO_VAL_INT_PLUS_MICRO:
649 return sysfs_emit_at(buf, offset, "-%d.%06u%s",
650 abs(vals[0]), -vals[1],
651 scale_db ? " dB" : "");
653 return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0],
654 vals[1], scale_db ? " dB" : "");
655 case IIO_VAL_INT_PLUS_NANO:
657 return sysfs_emit_at(buf, offset, "-%d.%09u",
658 abs(vals[0]), -vals[1]);
660 return sysfs_emit_at(buf, offset, "%d.%09u", vals[0],
662 case IIO_VAL_FRACTIONAL:
663 tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
665 tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
666 if ((tmp2 < 0) && (tmp0 == 0))
667 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
669 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
671 case IIO_VAL_FRACTIONAL_LOG2:
672 tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
673 tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
674 if (tmp0 == 0 && tmp2 < 0)
675 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
677 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
679 case IIO_VAL_INT_MULTIPLE:
684 for (i = 0; i < size; ++i)
685 l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]);
689 return sysfs_emit_at(buf, offset, "%c", (char)vals[0]);
691 tmp2 = (s64)((((u64)vals[1]) << 32) | (u32)vals[0]);
692 return sysfs_emit_at(buf, offset, "%lld", tmp2);
699 * iio_format_value() - Formats a IIO value into its string representation
700 * @buf: The buffer to which the formatted value gets written
701 * which is assumed to be big enough (i.e. PAGE_SIZE).
702 * @type: One of the IIO_VAL_* constants. This decides how the val
703 * and val2 parameters are formatted.
704 * @size: Number of IIO value entries contained in vals
705 * @vals: Pointer to the values, exact meaning depends on the
709 * 0 by default, a negative number on failure or the total number of characters
710 * written for a type that belongs to the IIO_VAL_* constant.
712 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
716 len = __iio_format_value(buf, 0, type, size, vals);
717 if (len >= PAGE_SIZE - 1)
720 return len + sysfs_emit_at(buf, len, "\n");
722 EXPORT_SYMBOL_GPL(iio_format_value);
724 static ssize_t iio_read_channel_label(struct device *dev,
725 struct device_attribute *attr,
728 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
729 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
731 if (indio_dev->info->read_label)
732 return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
734 if (this_attr->c->extend_name)
735 return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
740 static ssize_t iio_read_channel_info(struct device *dev,
741 struct device_attribute *attr,
744 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
745 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
746 int vals[INDIO_MAX_RAW_ELEMENTS];
750 if (indio_dev->info->read_raw_multi)
751 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
752 INDIO_MAX_RAW_ELEMENTS,
756 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
757 &vals[0], &vals[1], this_attr->address);
762 return iio_format_value(buf, ret, val_len, vals);
765 static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
766 const char *prefix, const char *suffix)
781 len = sysfs_emit(buf, prefix);
783 for (i = 0; i <= length - stride; i += stride) {
785 len += sysfs_emit_at(buf, len, " ");
786 if (len >= PAGE_SIZE)
790 len += __iio_format_value(buf, len, type, stride, &vals[i]);
791 if (len >= PAGE_SIZE)
795 len += sysfs_emit_at(buf, len, "%s\n", suffix);
800 static ssize_t iio_format_avail_list(char *buf, const int *vals,
801 int type, int length)
804 return iio_format_list(buf, vals, type, length, "", "");
807 static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
812 * length refers to the array size , not the number of elements.
813 * The purpose is to print the range [min , step ,max] so length should
814 * be 3 in case of int, and 6 for other types.
825 return iio_format_list(buf, vals, type, length, "[", "]");
828 static ssize_t iio_read_channel_info_avail(struct device *dev,
829 struct device_attribute *attr,
832 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
833 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
839 ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
840 &vals, &type, &length,
847 return iio_format_avail_list(buf, vals, type, length);
848 case IIO_AVAIL_RANGE:
849 return iio_format_avail_range(buf, vals, type);
856 * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
857 * @str: The string to parse
858 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
859 * @integer: The integer part of the number
860 * @fract: The fractional part of the number
861 * @scale_db: True if this should parse as dB
864 * 0 on success, or a negative error code if the string could not be parsed.
866 static int __iio_str_to_fixpoint(const char *str, int fract_mult,
867 int *integer, int *fract, bool scale_db)
870 bool integer_part = true, negative = false;
872 if (fract_mult == 0) {
875 return kstrtoint(str, 0, integer);
881 } else if (str[0] == '+') {
886 if ('0' <= *str && *str <= '9') {
888 i = i * 10 + *str - '0';
890 f += fract_mult * (*str - '0');
893 } else if (*str == '\n') {
894 if (*(str + 1) == '\0')
897 } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
898 /* Ignore the dB suffix */
899 str += sizeof(" dB") - 1;
901 } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
902 /* Ignore the dB suffix */
903 str += sizeof("dB") - 1;
905 } else if (*str == '.' && integer_part) {
906 integer_part = false;
927 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
928 * @str: The string to parse
929 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
930 * @integer: The integer part of the number
931 * @fract: The fractional part of the number
934 * 0 on success, or a negative error code if the string could not be parsed.
936 int iio_str_to_fixpoint(const char *str, int fract_mult,
937 int *integer, int *fract)
939 return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
941 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
943 static ssize_t iio_write_channel_info(struct device *dev,
944 struct device_attribute *attr,
948 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
949 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
950 int ret, fract_mult = 100000;
951 int integer, fract = 0;
952 bool is_char = false;
953 bool scale_db = false;
955 /* Assumes decimal - precision based on number of digits */
956 if (!indio_dev->info->write_raw)
959 if (indio_dev->info->write_raw_get_fmt)
960 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
961 this_attr->c, this_attr->address)) {
965 case IIO_VAL_INT_PLUS_MICRO_DB:
968 case IIO_VAL_INT_PLUS_MICRO:
971 case IIO_VAL_INT_PLUS_NANO:
972 fract_mult = 100000000;
984 if (sscanf(buf, "%c", &ch) != 1)
988 ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
994 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
995 integer, fract, this_attr->address);
1003 int __iio_device_attr_init(struct device_attribute *dev_attr,
1004 const char *postfix,
1005 struct iio_chan_spec const *chan,
1006 ssize_t (*readfunc)(struct device *dev,
1007 struct device_attribute *attr,
1009 ssize_t (*writefunc)(struct device *dev,
1010 struct device_attribute *attr,
1013 enum iio_shared_by shared_by)
1019 sysfs_attr_init(&dev_attr->attr);
1021 /* Build up postfix of <extend_name>_<modifier>_postfix */
1022 if (chan->modified && (shared_by == IIO_SEPARATE)) {
1023 if (chan->extend_name)
1024 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
1025 iio_modifier_names[chan->channel2],
1029 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
1030 iio_modifier_names[chan->channel2],
1033 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
1034 full_postfix = kstrdup(postfix, GFP_KERNEL);
1036 full_postfix = kasprintf(GFP_KERNEL,
1041 if (full_postfix == NULL)
1044 if (chan->differential) { /* Differential can not have modifier */
1045 switch (shared_by) {
1046 case IIO_SHARED_BY_ALL:
1047 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1049 case IIO_SHARED_BY_DIR:
1050 name = kasprintf(GFP_KERNEL, "%s_%s",
1051 iio_direction[chan->output],
1054 case IIO_SHARED_BY_TYPE:
1055 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
1056 iio_direction[chan->output],
1057 iio_chan_type_name_spec[chan->type],
1058 iio_chan_type_name_spec[chan->type],
1062 if (!chan->indexed) {
1063 WARN(1, "Differential channels must be indexed\n");
1065 goto error_free_full_postfix;
1067 name = kasprintf(GFP_KERNEL,
1069 iio_direction[chan->output],
1070 iio_chan_type_name_spec[chan->type],
1072 iio_chan_type_name_spec[chan->type],
1077 } else { /* Single ended */
1078 switch (shared_by) {
1079 case IIO_SHARED_BY_ALL:
1080 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1082 case IIO_SHARED_BY_DIR:
1083 name = kasprintf(GFP_KERNEL, "%s_%s",
1084 iio_direction[chan->output],
1087 case IIO_SHARED_BY_TYPE:
1088 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1089 iio_direction[chan->output],
1090 iio_chan_type_name_spec[chan->type],
1096 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
1097 iio_direction[chan->output],
1098 iio_chan_type_name_spec[chan->type],
1102 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1103 iio_direction[chan->output],
1104 iio_chan_type_name_spec[chan->type],
1111 goto error_free_full_postfix;
1113 dev_attr->attr.name = name;
1116 dev_attr->attr.mode |= 0444;
1117 dev_attr->show = readfunc;
1121 dev_attr->attr.mode |= 0200;
1122 dev_attr->store = writefunc;
1125 error_free_full_postfix:
1126 kfree(full_postfix);
1131 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1133 kfree(dev_attr->attr.name);
1136 int __iio_add_chan_devattr(const char *postfix,
1137 struct iio_chan_spec const *chan,
1138 ssize_t (*readfunc)(struct device *dev,
1139 struct device_attribute *attr,
1141 ssize_t (*writefunc)(struct device *dev,
1142 struct device_attribute *attr,
1146 enum iio_shared_by shared_by,
1148 struct iio_buffer *buffer,
1149 struct list_head *attr_list)
1152 struct iio_dev_attr *iio_attr, *t;
1154 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
1155 if (iio_attr == NULL)
1157 ret = __iio_device_attr_init(&iio_attr->dev_attr,
1159 readfunc, writefunc, shared_by);
1161 goto error_iio_dev_attr_free;
1163 iio_attr->address = mask;
1164 iio_attr->buffer = buffer;
1165 list_for_each_entry(t, attr_list, l)
1166 if (strcmp(t->dev_attr.attr.name,
1167 iio_attr->dev_attr.attr.name) == 0) {
1168 if (shared_by == IIO_SEPARATE)
1169 dev_err(dev, "tried to double register : %s\n",
1170 t->dev_attr.attr.name);
1172 goto error_device_attr_deinit;
1174 list_add(&iio_attr->l, attr_list);
1178 error_device_attr_deinit:
1179 __iio_device_attr_deinit(&iio_attr->dev_attr);
1180 error_iio_dev_attr_free:
1185 static int iio_device_add_channel_label(struct iio_dev *indio_dev,
1186 struct iio_chan_spec const *chan)
1188 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1191 if (!indio_dev->info->read_label && !chan->extend_name)
1194 ret = __iio_add_chan_devattr("label",
1196 &iio_read_channel_label,
1202 &iio_dev_opaque->channel_attr_list);
1209 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1210 struct iio_chan_spec const *chan,
1211 enum iio_shared_by shared_by,
1212 const long *infomask)
1214 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1215 int i, ret, attrcount = 0;
1217 for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1218 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1220 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1222 &iio_read_channel_info,
1223 &iio_write_channel_info,
1228 &iio_dev_opaque->channel_attr_list);
1229 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1239 static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1240 struct iio_chan_spec const *chan,
1241 enum iio_shared_by shared_by,
1242 const long *infomask)
1244 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1245 int i, ret, attrcount = 0;
1246 char *avail_postfix;
1248 for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1249 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1251 avail_postfix = kasprintf(GFP_KERNEL,
1253 iio_chan_info_postfix[i]);
1257 ret = __iio_add_chan_devattr(avail_postfix,
1259 &iio_read_channel_info_avail,
1265 &iio_dev_opaque->channel_attr_list);
1266 kfree(avail_postfix);
1267 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1277 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1278 struct iio_chan_spec const *chan)
1280 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1281 int ret, attrcount = 0;
1282 const struct iio_chan_spec_ext_info *ext_info;
1284 if (chan->channel < 0)
1286 ret = iio_device_add_info_mask_type(indio_dev, chan,
1288 &chan->info_mask_separate);
1293 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1295 &chan->info_mask_separate_available);
1300 ret = iio_device_add_info_mask_type(indio_dev, chan,
1302 &chan->info_mask_shared_by_type);
1307 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1309 &chan->info_mask_shared_by_type_available);
1314 ret = iio_device_add_info_mask_type(indio_dev, chan,
1316 &chan->info_mask_shared_by_dir);
1321 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1323 &chan->info_mask_shared_by_dir_available);
1328 ret = iio_device_add_info_mask_type(indio_dev, chan,
1330 &chan->info_mask_shared_by_all);
1335 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1337 &chan->info_mask_shared_by_all_available);
1342 ret = iio_device_add_channel_label(indio_dev, chan);
1347 if (chan->ext_info) {
1350 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1351 ret = __iio_add_chan_devattr(ext_info->name,
1354 &iio_read_channel_ext_info : NULL,
1356 &iio_write_channel_ext_info : NULL,
1361 &iio_dev_opaque->channel_attr_list);
1363 if (ret == -EBUSY && ext_info->shared)
1377 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1378 * @attr_list: List of IIO device attributes
1380 * This function frees the memory allocated for each of the IIO device
1381 * attributes in the list.
1383 void iio_free_chan_devattr_list(struct list_head *attr_list)
1385 struct iio_dev_attr *p, *n;
1387 list_for_each_entry_safe(p, n, attr_list, l) {
1388 kfree_const(p->dev_attr.attr.name);
1394 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
1397 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1399 return sysfs_emit(buf, "%s\n", indio_dev->name);
1402 static DEVICE_ATTR_RO(name);
1404 static ssize_t label_show(struct device *dev, struct device_attribute *attr,
1407 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1409 return sysfs_emit(buf, "%s\n", indio_dev->label);
1412 static DEVICE_ATTR_RO(label);
1414 static const char * const clock_names[] = {
1415 [CLOCK_REALTIME] = "realtime",
1416 [CLOCK_MONOTONIC] = "monotonic",
1417 [CLOCK_PROCESS_CPUTIME_ID] = "process_cputime_id",
1418 [CLOCK_THREAD_CPUTIME_ID] = "thread_cputime_id",
1419 [CLOCK_MONOTONIC_RAW] = "monotonic_raw",
1420 [CLOCK_REALTIME_COARSE] = "realtime_coarse",
1421 [CLOCK_MONOTONIC_COARSE] = "monotonic_coarse",
1422 [CLOCK_BOOTTIME] = "boottime",
1423 [CLOCK_REALTIME_ALARM] = "realtime_alarm",
1424 [CLOCK_BOOTTIME_ALARM] = "boottime_alarm",
1425 [CLOCK_SGI_CYCLE] = "sgi_cycle",
1426 [CLOCK_TAI] = "tai",
1429 static ssize_t current_timestamp_clock_show(struct device *dev,
1430 struct device_attribute *attr,
1433 const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1434 const clockid_t clk = iio_device_get_clock(indio_dev);
1437 case CLOCK_REALTIME:
1438 case CLOCK_MONOTONIC:
1439 case CLOCK_MONOTONIC_RAW:
1440 case CLOCK_REALTIME_COARSE:
1441 case CLOCK_MONOTONIC_COARSE:
1442 case CLOCK_BOOTTIME:
1449 return sysfs_emit(buf, "%s\n", clock_names[clk]);
1452 static ssize_t current_timestamp_clock_store(struct device *dev,
1453 struct device_attribute *attr,
1454 const char *buf, size_t len)
1459 ret = sysfs_match_string(clock_names, buf);
1465 case CLOCK_REALTIME:
1466 case CLOCK_MONOTONIC:
1467 case CLOCK_MONOTONIC_RAW:
1468 case CLOCK_REALTIME_COARSE:
1469 case CLOCK_MONOTONIC_COARSE:
1470 case CLOCK_BOOTTIME:
1477 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1484 int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
1485 const struct attribute_group *group)
1487 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1488 const struct attribute_group **new, **old = iio_dev_opaque->groups;
1489 unsigned int cnt = iio_dev_opaque->groupcounter;
1491 new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL);
1495 new[iio_dev_opaque->groupcounter++] = group;
1496 new[iio_dev_opaque->groupcounter] = NULL;
1498 iio_dev_opaque->groups = new;
1503 static DEVICE_ATTR_RW(current_timestamp_clock);
1505 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1507 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1508 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1509 struct iio_dev_attr *p;
1510 struct attribute **attr, *clk = NULL;
1512 /* First count elements in any existing group */
1513 if (indio_dev->info->attrs) {
1514 attr = indio_dev->info->attrs->attrs;
1515 while (*attr++ != NULL)
1518 attrcount = attrcount_orig;
1520 * New channel registration method - relies on the fact a group does
1521 * not need to be initialized if its name is NULL.
1523 if (indio_dev->channels)
1524 for (i = 0; i < indio_dev->num_channels; i++) {
1525 const struct iio_chan_spec *chan =
1526 &indio_dev->channels[i];
1528 if (chan->type == IIO_TIMESTAMP)
1529 clk = &dev_attr_current_timestamp_clock.attr;
1531 ret = iio_device_add_channel_sysfs(indio_dev, chan);
1533 goto error_clear_attrs;
1537 if (iio_dev_opaque->event_interface)
1538 clk = &dev_attr_current_timestamp_clock.attr;
1540 if (indio_dev->name)
1542 if (indio_dev->label)
1547 iio_dev_opaque->chan_attr_group.attrs =
1548 kcalloc(attrcount + 1,
1549 sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1551 if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
1553 goto error_clear_attrs;
1555 /* Copy across original attributes, and point to original binary attributes */
1556 if (indio_dev->info->attrs) {
1557 memcpy(iio_dev_opaque->chan_attr_group.attrs,
1558 indio_dev->info->attrs->attrs,
1559 sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
1561 iio_dev_opaque->chan_attr_group.is_visible =
1562 indio_dev->info->attrs->is_visible;
1563 iio_dev_opaque->chan_attr_group.bin_attrs =
1564 indio_dev->info->attrs->bin_attrs;
1566 attrn = attrcount_orig;
1567 /* Add all elements from the list. */
1568 list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1569 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1570 if (indio_dev->name)
1571 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1572 if (indio_dev->label)
1573 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1575 iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
1577 ret = iio_device_register_sysfs_group(indio_dev,
1578 &iio_dev_opaque->chan_attr_group);
1580 goto error_clear_attrs;
1585 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1590 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1592 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1594 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1595 kfree(iio_dev_opaque->chan_attr_group.attrs);
1596 iio_dev_opaque->chan_attr_group.attrs = NULL;
1597 kfree(iio_dev_opaque->groups);
1598 iio_dev_opaque->groups = NULL;
1601 static void iio_dev_release(struct device *device)
1603 struct iio_dev *indio_dev = dev_to_iio_dev(device);
1604 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1606 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1607 iio_device_unregister_trigger_consumer(indio_dev);
1608 iio_device_unregister_eventset(indio_dev);
1609 iio_device_unregister_sysfs(indio_dev);
1611 iio_device_detach_buffers(indio_dev);
1613 lockdep_unregister_key(&iio_dev_opaque->mlock_key);
1615 ida_free(&iio_ida, iio_dev_opaque->id);
1616 kfree(iio_dev_opaque);
1619 const struct device_type iio_device_type = {
1620 .name = "iio_device",
1621 .release = iio_dev_release,
1625 * iio_device_alloc() - allocate an iio_dev from a driver
1626 * @parent: Parent device.
1627 * @sizeof_priv: Space to allocate for private structure.
1630 * Pointer to allocated iio_dev on success, NULL on failure.
1632 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
1634 struct iio_dev_opaque *iio_dev_opaque;
1635 struct iio_dev *indio_dev;
1638 alloc_size = sizeof(struct iio_dev_opaque);
1640 alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
1641 alloc_size += sizeof_priv;
1644 iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1645 if (!iio_dev_opaque)
1648 indio_dev = &iio_dev_opaque->indio_dev;
1649 indio_dev->priv = (char *)iio_dev_opaque +
1650 ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN);
1652 indio_dev->dev.parent = parent;
1653 indio_dev->dev.type = &iio_device_type;
1654 indio_dev->dev.bus = &iio_bus_type;
1655 device_initialize(&indio_dev->dev);
1656 mutex_init(&iio_dev_opaque->mlock);
1657 mutex_init(&iio_dev_opaque->info_exist_lock);
1658 INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
1660 iio_dev_opaque->id = ida_alloc(&iio_ida, GFP_KERNEL);
1661 if (iio_dev_opaque->id < 0) {
1662 /* cannot use a dev_err as the name isn't available */
1663 pr_err("failed to get device id\n");
1664 kfree(iio_dev_opaque);
1668 if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) {
1669 ida_free(&iio_ida, iio_dev_opaque->id);
1670 kfree(iio_dev_opaque);
1674 INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
1675 INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
1677 lockdep_register_key(&iio_dev_opaque->mlock_key);
1678 lockdep_set_class(&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key);
1682 EXPORT_SYMBOL(iio_device_alloc);
1685 * iio_device_free() - free an iio_dev from a driver
1686 * @dev: the iio_dev associated with the device
1688 void iio_device_free(struct iio_dev *dev)
1691 put_device(&dev->dev);
1693 EXPORT_SYMBOL(iio_device_free);
1695 static void devm_iio_device_release(void *iio_dev)
1697 iio_device_free(iio_dev);
1701 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1702 * @parent: Device to allocate iio_dev for, and parent for this IIO device
1703 * @sizeof_priv: Space to allocate for private structure.
1705 * Managed iio_device_alloc. iio_dev allocated with this function is
1706 * automatically freed on driver detach.
1709 * Pointer to allocated iio_dev on success, NULL on failure.
1711 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
1713 struct iio_dev *iio_dev;
1716 iio_dev = iio_device_alloc(parent, sizeof_priv);
1720 ret = devm_add_action_or_reset(parent, devm_iio_device_release,
1727 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1730 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1731 * @inode: Inode structure for identifying the device in the file system
1732 * @filp: File structure for iio device used to keep and later access
1735 * Returns: 0 on success or -EBUSY if the device is already opened
1737 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1739 struct iio_dev_opaque *iio_dev_opaque =
1740 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1741 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1742 struct iio_dev_buffer_pair *ib;
1744 if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags))
1747 iio_device_get(indio_dev);
1749 ib = kmalloc(sizeof(*ib), GFP_KERNEL);
1751 iio_device_put(indio_dev);
1752 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1756 ib->indio_dev = indio_dev;
1757 ib->buffer = indio_dev->buffer;
1759 filp->private_data = ib;
1765 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1766 * @inode: Inode structure pointer for the char device
1767 * @filp: File structure pointer for the char device
1769 * Returns: 0 for successful release.
1771 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1773 struct iio_dev_buffer_pair *ib = filp->private_data;
1774 struct iio_dev_opaque *iio_dev_opaque =
1775 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1776 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1779 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1780 iio_device_put(indio_dev);
1785 void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
1786 struct iio_ioctl_handler *h)
1788 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1790 list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers);
1793 void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h)
1795 list_del(&h->entry);
1798 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1800 struct iio_dev_buffer_pair *ib = filp->private_data;
1801 struct iio_dev *indio_dev = ib->indio_dev;
1802 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1803 struct iio_ioctl_handler *h;
1806 mutex_lock(&iio_dev_opaque->info_exist_lock);
1809 * The NULL check here is required to prevent crashing when a device
1810 * is being removed while userspace would still have open file handles
1811 * to try to access this device.
1813 if (!indio_dev->info)
1816 list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
1817 ret = h->ioctl(indio_dev, filp, cmd, arg);
1818 if (ret != IIO_IOCTL_UNHANDLED)
1822 if (ret == IIO_IOCTL_UNHANDLED)
1826 mutex_unlock(&iio_dev_opaque->info_exist_lock);
1831 static const struct file_operations iio_buffer_fileops = {
1832 .owner = THIS_MODULE,
1833 .llseek = noop_llseek,
1834 .read = iio_buffer_read_outer_addr,
1835 .write = iio_buffer_write_outer_addr,
1836 .poll = iio_buffer_poll_addr,
1837 .unlocked_ioctl = iio_ioctl,
1838 .compat_ioctl = compat_ptr_ioctl,
1839 .open = iio_chrdev_open,
1840 .release = iio_chrdev_release,
1843 static const struct file_operations iio_event_fileops = {
1844 .owner = THIS_MODULE,
1845 .llseek = noop_llseek,
1846 .unlocked_ioctl = iio_ioctl,
1847 .compat_ioctl = compat_ptr_ioctl,
1848 .open = iio_chrdev_open,
1849 .release = iio_chrdev_release,
1852 static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1855 const struct iio_chan_spec *channels = indio_dev->channels;
1857 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1860 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1861 if (channels[i].scan_index < 0)
1863 for (j = i + 1; j < indio_dev->num_channels; j++)
1864 if (channels[i].scan_index == channels[j].scan_index) {
1865 dev_err(&indio_dev->dev,
1866 "Duplicate scan index %d\n",
1867 channels[i].scan_index);
1875 static int iio_check_extended_name(const struct iio_dev *indio_dev)
1879 if (!indio_dev->info->read_label)
1882 for (i = 0; i < indio_dev->num_channels; i++) {
1883 if (indio_dev->channels[i].extend_name) {
1884 dev_err(&indio_dev->dev,
1885 "Cannot use labels and extend_name at the same time\n");
1893 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1895 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1897 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1898 struct fwnode_handle *fwnode = NULL;
1901 if (!indio_dev->info)
1904 iio_dev_opaque->driver_module = this_mod;
1906 /* If the calling driver did not initialize firmware node, do it here */
1907 if (dev_fwnode(&indio_dev->dev))
1908 fwnode = dev_fwnode(&indio_dev->dev);
1909 /* The default dummy IIO device has no parent */
1910 else if (indio_dev->dev.parent)
1911 fwnode = dev_fwnode(indio_dev->dev.parent);
1912 device_set_node(&indio_dev->dev, fwnode);
1914 fwnode_property_read_string(fwnode, "label", &indio_dev->label);
1916 ret = iio_check_unique_scan_index(indio_dev);
1920 ret = iio_check_extended_name(indio_dev);
1924 iio_device_register_debugfs(indio_dev);
1926 ret = iio_buffers_alloc_sysfs_and_mask(indio_dev);
1928 dev_err(indio_dev->dev.parent,
1929 "Failed to create buffer sysfs interfaces\n");
1930 goto error_unreg_debugfs;
1933 ret = iio_device_register_sysfs(indio_dev);
1935 dev_err(indio_dev->dev.parent,
1936 "Failed to register sysfs interfaces\n");
1937 goto error_buffer_free_sysfs;
1939 ret = iio_device_register_eventset(indio_dev);
1941 dev_err(indio_dev->dev.parent,
1942 "Failed to register event set\n");
1943 goto error_free_sysfs;
1945 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1946 iio_device_register_trigger_consumer(indio_dev);
1948 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1949 indio_dev->setup_ops == NULL)
1950 indio_dev->setup_ops = &noop_ring_setup_ops;
1952 if (iio_dev_opaque->attached_buffers_cnt)
1953 cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops);
1954 else if (iio_dev_opaque->event_interface)
1955 cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops);
1957 if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) {
1958 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id);
1959 iio_dev_opaque->chrdev.owner = this_mod;
1962 /* assign device groups now; they should be all registered now */
1963 indio_dev->dev.groups = iio_dev_opaque->groups;
1965 ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev);
1967 goto error_unreg_eventset;
1971 error_unreg_eventset:
1972 iio_device_unregister_eventset(indio_dev);
1974 iio_device_unregister_sysfs(indio_dev);
1975 error_buffer_free_sysfs:
1976 iio_buffers_free_sysfs_and_mask(indio_dev);
1977 error_unreg_debugfs:
1978 iio_device_unregister_debugfs(indio_dev);
1981 EXPORT_SYMBOL(__iio_device_register);
1984 * iio_device_unregister() - unregister a device from the IIO subsystem
1985 * @indio_dev: Device structure representing the device.
1987 void iio_device_unregister(struct iio_dev *indio_dev)
1989 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1991 cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev);
1993 mutex_lock(&iio_dev_opaque->info_exist_lock);
1995 iio_device_unregister_debugfs(indio_dev);
1997 iio_disable_all_buffers(indio_dev);
1999 indio_dev->info = NULL;
2001 iio_device_wakeup_eventset(indio_dev);
2002 iio_buffer_wakeup_poll(indio_dev);
2004 mutex_unlock(&iio_dev_opaque->info_exist_lock);
2006 iio_buffers_free_sysfs_and_mask(indio_dev);
2008 EXPORT_SYMBOL(iio_device_unregister);
2010 static void devm_iio_device_unreg(void *indio_dev)
2012 iio_device_unregister(indio_dev);
2015 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
2016 struct module *this_mod)
2020 ret = __iio_device_register(indio_dev, this_mod);
2024 return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev);
2026 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
2029 * iio_device_claim_direct_mode - Keep device in direct mode
2030 * @indio_dev: the iio_dev associated with the device
2032 * If the device is in direct mode it is guaranteed to stay
2033 * that way until iio_device_release_direct_mode() is called.
2035 * Use with iio_device_release_direct_mode()
2037 * Returns: 0 on success, -EBUSY on failure.
2039 int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
2041 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2043 mutex_lock(&iio_dev_opaque->mlock);
2045 if (iio_buffer_enabled(indio_dev)) {
2046 mutex_unlock(&iio_dev_opaque->mlock);
2051 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
2054 * iio_device_release_direct_mode - releases claim on direct mode
2055 * @indio_dev: the iio_dev associated with the device
2057 * Release the claim. Device is no longer guaranteed to stay
2060 * Use with iio_device_claim_direct_mode()
2062 void iio_device_release_direct_mode(struct iio_dev *indio_dev)
2064 mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
2066 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
2069 * iio_device_claim_buffer_mode - Keep device in buffer mode
2070 * @indio_dev: the iio_dev associated with the device
2072 * If the device is in buffer mode it is guaranteed to stay
2073 * that way until iio_device_release_buffer_mode() is called.
2075 * Use with iio_device_release_buffer_mode().
2077 * Returns: 0 on success, -EBUSY on failure.
2079 int iio_device_claim_buffer_mode(struct iio_dev *indio_dev)
2081 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2083 mutex_lock(&iio_dev_opaque->mlock);
2085 if (iio_buffer_enabled(indio_dev))
2088 mutex_unlock(&iio_dev_opaque->mlock);
2091 EXPORT_SYMBOL_GPL(iio_device_claim_buffer_mode);
2094 * iio_device_release_buffer_mode - releases claim on buffer mode
2095 * @indio_dev: the iio_dev associated with the device
2097 * Release the claim. Device is no longer guaranteed to stay
2100 * Use with iio_device_claim_buffer_mode().
2102 void iio_device_release_buffer_mode(struct iio_dev *indio_dev)
2104 mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock);
2106 EXPORT_SYMBOL_GPL(iio_device_release_buffer_mode);
2109 * iio_device_get_current_mode() - helper function providing read-only access to
2110 * the opaque @currentmode variable
2111 * @indio_dev: IIO device structure for device
2113 int iio_device_get_current_mode(struct iio_dev *indio_dev)
2115 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2117 return iio_dev_opaque->currentmode;
2119 EXPORT_SYMBOL_GPL(iio_device_get_current_mode);
2121 subsys_initcall(iio_init);
2122 module_exit(iio_exit);
2125 MODULE_DESCRIPTION("Industrial I/O core");
2126 MODULE_LICENSE("GPL");