1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core
4 * Copyright (c) 2008 Jonathan Cameron
6 * Based on elements of hwmon and input subsystems.
9 #define pr_fmt(fmt) "iio-core: " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/idr.h>
14 #include <linux/kdev_t.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
18 #include <linux/poll.h>
19 #include <linux/property.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/debugfs.h>
26 #include <linux/mutex.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/iio-opaque.h>
30 #include "iio_core_trigger.h"
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/events.h>
33 #include <linux/iio/buffer.h>
34 #include <linux/iio/buffer_impl.h>
36 /* IDA to assign each registered device a unique id */
37 static DEFINE_IDA(iio_ida);
39 static dev_t iio_devt;
41 #define IIO_DEV_MAX 256
42 struct bus_type iio_bus_type = {
45 EXPORT_SYMBOL(iio_bus_type);
47 static struct dentry *iio_debugfs_dentry;
49 static const char * const iio_direction[] = {
54 static const char * const iio_chan_type_name_spec[] = {
55 [IIO_VOLTAGE] = "voltage",
56 [IIO_CURRENT] = "current",
57 [IIO_POWER] = "power",
58 [IIO_ACCEL] = "accel",
59 [IIO_ANGL_VEL] = "anglvel",
61 [IIO_LIGHT] = "illuminance",
62 [IIO_INTENSITY] = "intensity",
63 [IIO_PROXIMITY] = "proximity",
65 [IIO_INCLI] = "incli",
68 [IIO_TIMESTAMP] = "timestamp",
69 [IIO_CAPACITANCE] = "capacitance",
70 [IIO_ALTVOLTAGE] = "altvoltage",
72 [IIO_PRESSURE] = "pressure",
73 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
74 [IIO_ACTIVITY] = "activity",
75 [IIO_STEPS] = "steps",
76 [IIO_ENERGY] = "energy",
77 [IIO_DISTANCE] = "distance",
78 [IIO_VELOCITY] = "velocity",
79 [IIO_CONCENTRATION] = "concentration",
80 [IIO_RESISTANCE] = "resistance",
82 [IIO_UVINDEX] = "uvindex",
83 [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
84 [IIO_COUNT] = "count",
85 [IIO_INDEX] = "index",
86 [IIO_GRAVITY] = "gravity",
87 [IIO_POSITIONRELATIVE] = "positionrelative",
88 [IIO_PHASE] = "phase",
89 [IIO_MASSCONCENTRATION] = "massconcentration",
92 static const char * const iio_modifier_names[] = {
96 [IIO_MOD_X_AND_Y] = "x&y",
97 [IIO_MOD_X_AND_Z] = "x&z",
98 [IIO_MOD_Y_AND_Z] = "y&z",
99 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
100 [IIO_MOD_X_OR_Y] = "x|y",
101 [IIO_MOD_X_OR_Z] = "x|z",
102 [IIO_MOD_Y_OR_Z] = "y|z",
103 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
104 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
105 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
106 [IIO_MOD_LIGHT_BOTH] = "both",
107 [IIO_MOD_LIGHT_IR] = "ir",
108 [IIO_MOD_LIGHT_CLEAR] = "clear",
109 [IIO_MOD_LIGHT_RED] = "red",
110 [IIO_MOD_LIGHT_GREEN] = "green",
111 [IIO_MOD_LIGHT_BLUE] = "blue",
112 [IIO_MOD_LIGHT_UV] = "uv",
113 [IIO_MOD_LIGHT_DUV] = "duv",
114 [IIO_MOD_QUATERNION] = "quaternion",
115 [IIO_MOD_TEMP_AMBIENT] = "ambient",
116 [IIO_MOD_TEMP_OBJECT] = "object",
117 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
118 [IIO_MOD_NORTH_TRUE] = "from_north_true",
119 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
120 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
121 [IIO_MOD_RUNNING] = "running",
122 [IIO_MOD_JOGGING] = "jogging",
123 [IIO_MOD_WALKING] = "walking",
124 [IIO_MOD_STILL] = "still",
125 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
128 [IIO_MOD_CO2] = "co2",
129 [IIO_MOD_VOC] = "voc",
130 [IIO_MOD_PM1] = "pm1",
131 [IIO_MOD_PM2P5] = "pm2p5",
132 [IIO_MOD_PM4] = "pm4",
133 [IIO_MOD_PM10] = "pm10",
134 [IIO_MOD_ETHANOL] = "ethanol",
139 /* relies on pairs of these shared then separate */
140 static const char * const iio_chan_info_postfix[] = {
141 [IIO_CHAN_INFO_RAW] = "raw",
142 [IIO_CHAN_INFO_PROCESSED] = "input",
143 [IIO_CHAN_INFO_SCALE] = "scale",
144 [IIO_CHAN_INFO_OFFSET] = "offset",
145 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
146 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
147 [IIO_CHAN_INFO_PEAK] = "peak_raw",
148 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
149 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
150 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
151 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
152 = "filter_low_pass_3db_frequency",
153 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
154 = "filter_high_pass_3db_frequency",
155 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
156 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
157 [IIO_CHAN_INFO_PHASE] = "phase",
158 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
159 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
160 [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative",
161 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
162 [IIO_CHAN_INFO_ENABLE] = "en",
163 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
164 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
165 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
166 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
167 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
168 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
169 [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
170 [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
173 * iio_device_id() - query the unique ID for the device
174 * @indio_dev: Device structure whose ID is being queried
176 * The IIO device ID is a unique index used for example for the naming
177 * of the character device /dev/iio\:device[ID]
179 int iio_device_id(struct iio_dev *indio_dev)
181 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
183 return iio_dev_opaque->id;
185 EXPORT_SYMBOL_GPL(iio_device_id);
188 * iio_buffer_enabled() - helper function to test if the buffer is enabled
189 * @indio_dev: IIO device structure for device
191 bool iio_buffer_enabled(struct iio_dev *indio_dev)
193 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
195 return iio_dev_opaque->currentmode
196 & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE |
197 INDIO_BUFFER_SOFTWARE);
199 EXPORT_SYMBOL_GPL(iio_buffer_enabled);
202 * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps
203 * @array: array of strings
204 * @n: number of strings in the array
205 * @str: string to match with
207 * Returns index of @str in the @array or -EINVAL, similar to match_string().
208 * Uses sysfs_streq instead of strcmp for matching.
210 * This routine will look for a string in an array of strings.
211 * The search will continue until the element is found or the n-th element
212 * is reached, regardless of any NULL elements in the array.
214 static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n,
220 for (index = 0; index < n; index++) {
224 if (sysfs_streq(item, str))
231 #if defined(CONFIG_DEBUG_FS)
233 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
234 * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
236 struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
238 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
239 return iio_dev_opaque->debugfs_dentry;
241 EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
245 * iio_find_channel_from_si() - get channel from its scan index
247 * @si: scan index to match
249 const struct iio_chan_spec
250 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
254 for (i = 0; i < indio_dev->num_channels; i++)
255 if (indio_dev->channels[i].scan_index == si)
256 return &indio_dev->channels[i];
260 /* This turns up an awful lot */
261 ssize_t iio_read_const_attr(struct device *dev,
262 struct device_attribute *attr,
265 return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string);
267 EXPORT_SYMBOL(iio_read_const_attr);
270 * iio_device_set_clock() - Set current timestamping clock for the device
271 * @indio_dev: IIO device structure containing the device
272 * @clock_id: timestamping clock posix identifier to set.
274 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
277 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
278 const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
280 ret = mutex_lock_interruptible(&indio_dev->mlock);
283 if ((ev_int && iio_event_enabled(ev_int)) ||
284 iio_buffer_enabled(indio_dev)) {
285 mutex_unlock(&indio_dev->mlock);
288 iio_dev_opaque->clock_id = clock_id;
289 mutex_unlock(&indio_dev->mlock);
293 EXPORT_SYMBOL(iio_device_set_clock);
296 * iio_device_get_clock() - Retrieve current timestamping clock for the device
297 * @indio_dev: IIO device structure containing the device
299 clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
301 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
303 return iio_dev_opaque->clock_id;
305 EXPORT_SYMBOL(iio_device_get_clock);
308 * iio_get_time_ns() - utility function to get a time stamp for events etc
311 s64 iio_get_time_ns(const struct iio_dev *indio_dev)
313 struct timespec64 tp;
315 switch (iio_device_get_clock(indio_dev)) {
317 return ktime_get_real_ns();
318 case CLOCK_MONOTONIC:
319 return ktime_get_ns();
320 case CLOCK_MONOTONIC_RAW:
321 return ktime_get_raw_ns();
322 case CLOCK_REALTIME_COARSE:
323 return ktime_to_ns(ktime_get_coarse_real());
324 case CLOCK_MONOTONIC_COARSE:
325 ktime_get_coarse_ts64(&tp);
326 return timespec64_to_ns(&tp);
328 return ktime_get_boottime_ns();
330 return ktime_get_clocktai_ns();
335 EXPORT_SYMBOL(iio_get_time_ns);
338 * iio_get_time_res() - utility function to get time stamp clock resolution in
342 unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
344 switch (iio_device_get_clock(indio_dev)) {
346 case CLOCK_MONOTONIC:
347 case CLOCK_MONOTONIC_RAW:
350 return hrtimer_resolution;
351 case CLOCK_REALTIME_COARSE:
352 case CLOCK_MONOTONIC_COARSE:
358 EXPORT_SYMBOL(iio_get_time_res);
360 static int __init iio_init(void)
364 /* Register sysfs bus */
365 ret = bus_register(&iio_bus_type);
367 pr_err("could not register bus type\n");
371 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
373 pr_err("failed to allocate char dev region\n");
374 goto error_unregister_bus_type;
377 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
381 error_unregister_bus_type:
382 bus_unregister(&iio_bus_type);
387 static void __exit iio_exit(void)
390 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
391 bus_unregister(&iio_bus_type);
392 debugfs_remove(iio_debugfs_dentry);
395 #if defined(CONFIG_DEBUG_FS)
396 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
397 size_t count, loff_t *ppos)
399 struct iio_dev *indio_dev = file->private_data;
400 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
405 return simple_read_from_buffer(userbuf, count, ppos,
406 iio_dev_opaque->read_buf,
407 iio_dev_opaque->read_buf_len);
409 ret = indio_dev->info->debugfs_reg_access(indio_dev,
410 iio_dev_opaque->cached_reg_addr,
413 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
417 iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
418 sizeof(iio_dev_opaque->read_buf),
421 return simple_read_from_buffer(userbuf, count, ppos,
422 iio_dev_opaque->read_buf,
423 iio_dev_opaque->read_buf_len);
426 static ssize_t iio_debugfs_write_reg(struct file *file,
427 const char __user *userbuf, size_t count, loff_t *ppos)
429 struct iio_dev *indio_dev = file->private_data;
430 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
435 count = min_t(size_t, count, (sizeof(buf)-1));
436 if (copy_from_user(buf, userbuf, count))
441 ret = sscanf(buf, "%i %i", ®, &val);
445 iio_dev_opaque->cached_reg_addr = reg;
448 iio_dev_opaque->cached_reg_addr = reg;
449 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
452 dev_err(indio_dev->dev.parent, "%s: write failed\n",
464 static const struct file_operations iio_debugfs_reg_fops = {
466 .read = iio_debugfs_read_reg,
467 .write = iio_debugfs_write_reg,
470 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
472 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
473 debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
476 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
478 struct iio_dev_opaque *iio_dev_opaque;
480 if (indio_dev->info->debugfs_reg_access == NULL)
483 if (!iio_debugfs_dentry)
486 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
488 iio_dev_opaque->debugfs_dentry =
489 debugfs_create_dir(dev_name(&indio_dev->dev),
492 debugfs_create_file("direct_reg_access", 0644,
493 iio_dev_opaque->debugfs_dentry, indio_dev,
494 &iio_debugfs_reg_fops);
497 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
501 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
504 #endif /* CONFIG_DEBUG_FS */
506 static ssize_t iio_read_channel_ext_info(struct device *dev,
507 struct device_attribute *attr,
510 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
511 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
512 const struct iio_chan_spec_ext_info *ext_info;
514 ext_info = &this_attr->c->ext_info[this_attr->address];
516 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
519 static ssize_t iio_write_channel_ext_info(struct device *dev,
520 struct device_attribute *attr,
524 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
525 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
526 const struct iio_chan_spec_ext_info *ext_info;
528 ext_info = &this_attr->c->ext_info[this_attr->address];
530 return ext_info->write(indio_dev, ext_info->private,
531 this_attr->c, buf, len);
534 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
535 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
537 const struct iio_enum *e = (const struct iio_enum *)priv;
544 for (i = 0; i < e->num_items; ++i) {
547 len += sysfs_emit_at(buf, len, "%s ", e->items[i]);
550 /* replace last space with a newline */
555 EXPORT_SYMBOL_GPL(iio_enum_available_read);
557 ssize_t iio_enum_read(struct iio_dev *indio_dev,
558 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
560 const struct iio_enum *e = (const struct iio_enum *)priv;
566 i = e->get(indio_dev, chan);
569 else if (i >= e->num_items || !e->items[i])
572 return sysfs_emit(buf, "%s\n", e->items[i]);
574 EXPORT_SYMBOL_GPL(iio_enum_read);
576 ssize_t iio_enum_write(struct iio_dev *indio_dev,
577 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
580 const struct iio_enum *e = (const struct iio_enum *)priv;
586 ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf);
590 ret = e->set(indio_dev, chan, ret);
591 return ret ? ret : len;
593 EXPORT_SYMBOL_GPL(iio_enum_write);
595 static const struct iio_mount_matrix iio_mount_idmatrix = {
603 static int iio_setup_mount_idmatrix(const struct device *dev,
604 struct iio_mount_matrix *matrix)
606 *matrix = iio_mount_idmatrix;
607 dev_info(dev, "mounting matrix not found: using identity...\n");
611 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
612 const struct iio_chan_spec *chan, char *buf)
614 const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
615 priv)(indio_dev, chan);
621 mtx = &iio_mount_idmatrix;
623 return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
624 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
625 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
626 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
628 EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
631 * iio_read_mount_matrix() - retrieve iio device mounting matrix from
632 * device "mount-matrix" property
633 * @dev: device the mounting matrix property is assigned to
634 * @matrix: where to store retrieved matrix
636 * If device is assigned no mounting matrix property, a default 3x3 identity
637 * matrix will be filled in.
639 * Return: 0 if success, or a negative error code on failure.
641 int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix)
643 size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
646 err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len);
651 /* Invalid number of matrix entries. */
655 /* Invalid matrix declaration format. */
658 /* Matrix was not declared at all: fallback to identity. */
659 return iio_setup_mount_idmatrix(dev, matrix);
661 EXPORT_SYMBOL(iio_read_mount_matrix);
663 static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
664 int size, const int *vals)
668 bool scale_db = false;
672 return sysfs_emit_at(buf, offset, "%d", vals[0]);
673 case IIO_VAL_INT_PLUS_MICRO_DB:
676 case IIO_VAL_INT_PLUS_MICRO:
678 return sysfs_emit_at(buf, offset, "-%d.%06u%s",
679 abs(vals[0]), -vals[1],
680 scale_db ? " dB" : "");
682 return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0],
683 vals[1], scale_db ? " dB" : "");
684 case IIO_VAL_INT_PLUS_NANO:
686 return sysfs_emit_at(buf, offset, "-%d.%09u",
687 abs(vals[0]), -vals[1]);
689 return sysfs_emit_at(buf, offset, "%d.%09u", vals[0],
691 case IIO_VAL_FRACTIONAL:
692 tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
694 tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
695 if ((tmp2 < 0) && (tmp0 == 0))
696 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
698 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
700 case IIO_VAL_FRACTIONAL_LOG2:
701 tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
702 tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
703 if (tmp0 == 0 && tmp2 < 0)
704 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
706 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
708 case IIO_VAL_INT_MULTIPLE:
713 for (i = 0; i < size; ++i)
714 l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]);
718 return sysfs_emit_at(buf, offset, "%c", (char)vals[0]);
720 tmp2 = (s64)((((u64)vals[1]) << 32) | (u32)vals[0]);
721 return sysfs_emit_at(buf, offset, "%lld", tmp2);
728 * iio_format_value() - Formats a IIO value into its string representation
729 * @buf: The buffer to which the formatted value gets written
730 * which is assumed to be big enough (i.e. PAGE_SIZE).
731 * @type: One of the IIO_VAL_* constants. This decides how the val
732 * and val2 parameters are formatted.
733 * @size: Number of IIO value entries contained in vals
734 * @vals: Pointer to the values, exact meaning depends on the
737 * Return: 0 by default, a negative number on failure or the
738 * total number of characters written for a type that belongs
739 * to the IIO_VAL_* constant.
741 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
745 len = __iio_format_value(buf, 0, type, size, vals);
746 if (len >= PAGE_SIZE - 1)
749 return len + sysfs_emit_at(buf, len, "\n");
751 EXPORT_SYMBOL_GPL(iio_format_value);
753 static ssize_t iio_read_channel_label(struct device *dev,
754 struct device_attribute *attr,
757 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
758 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
760 if (indio_dev->info->read_label)
761 return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
763 if (this_attr->c->extend_name)
764 return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
769 static ssize_t iio_read_channel_info(struct device *dev,
770 struct device_attribute *attr,
773 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
774 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
775 int vals[INDIO_MAX_RAW_ELEMENTS];
779 if (indio_dev->info->read_raw_multi)
780 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
781 INDIO_MAX_RAW_ELEMENTS,
785 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
786 &vals[0], &vals[1], this_attr->address);
791 return iio_format_value(buf, ret, val_len, vals);
794 static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
795 const char *prefix, const char *suffix)
810 len = sysfs_emit(buf, prefix);
812 for (i = 0; i <= length - stride; i += stride) {
814 len += sysfs_emit_at(buf, len, " ");
815 if (len >= PAGE_SIZE)
819 len += __iio_format_value(buf, len, type, stride, &vals[i]);
820 if (len >= PAGE_SIZE)
824 len += sysfs_emit_at(buf, len, "%s\n", suffix);
829 static ssize_t iio_format_avail_list(char *buf, const int *vals,
830 int type, int length)
833 return iio_format_list(buf, vals, type, length, "", "");
836 static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
838 return iio_format_list(buf, vals, type, 3, "[", "]");
841 static ssize_t iio_read_channel_info_avail(struct device *dev,
842 struct device_attribute *attr,
845 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
846 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
852 ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
853 &vals, &type, &length,
860 return iio_format_avail_list(buf, vals, type, length);
861 case IIO_AVAIL_RANGE:
862 return iio_format_avail_range(buf, vals, type);
869 * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
870 * @str: The string to parse
871 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
872 * @integer: The integer part of the number
873 * @fract: The fractional part of the number
874 * @scale_db: True if this should parse as dB
876 * Returns 0 on success, or a negative error code if the string could not be
879 static int __iio_str_to_fixpoint(const char *str, int fract_mult,
880 int *integer, int *fract, bool scale_db)
883 bool integer_part = true, negative = false;
885 if (fract_mult == 0) {
888 return kstrtoint(str, 0, integer);
894 } else if (str[0] == '+') {
899 if ('0' <= *str && *str <= '9') {
901 i = i * 10 + *str - '0';
903 f += fract_mult * (*str - '0');
906 } else if (*str == '\n') {
907 if (*(str + 1) == '\0')
910 } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
911 /* Ignore the dB suffix */
912 str += sizeof(" dB") - 1;
914 } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
915 /* Ignore the dB suffix */
916 str += sizeof("dB") - 1;
918 } else if (*str == '.' && integer_part) {
919 integer_part = false;
940 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
941 * @str: The string to parse
942 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
943 * @integer: The integer part of the number
944 * @fract: The fractional part of the number
946 * Returns 0 on success, or a negative error code if the string could not be
949 int iio_str_to_fixpoint(const char *str, int fract_mult,
950 int *integer, int *fract)
952 return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
954 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
956 static ssize_t iio_write_channel_info(struct device *dev,
957 struct device_attribute *attr,
961 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
962 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
963 int ret, fract_mult = 100000;
964 int integer, fract = 0;
965 bool is_char = false;
966 bool scale_db = false;
968 /* Assumes decimal - precision based on number of digits */
969 if (!indio_dev->info->write_raw)
972 if (indio_dev->info->write_raw_get_fmt)
973 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
974 this_attr->c, this_attr->address)) {
978 case IIO_VAL_INT_PLUS_MICRO_DB:
981 case IIO_VAL_INT_PLUS_MICRO:
984 case IIO_VAL_INT_PLUS_NANO:
985 fract_mult = 100000000;
997 if (sscanf(buf, "%c", &ch) != 1)
1001 ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
1007 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
1008 integer, fract, this_attr->address);
1016 int __iio_device_attr_init(struct device_attribute *dev_attr,
1017 const char *postfix,
1018 struct iio_chan_spec const *chan,
1019 ssize_t (*readfunc)(struct device *dev,
1020 struct device_attribute *attr,
1022 ssize_t (*writefunc)(struct device *dev,
1023 struct device_attribute *attr,
1026 enum iio_shared_by shared_by)
1031 sysfs_attr_init(&dev_attr->attr);
1033 /* Build up postfix of <extend_name>_<modifier>_postfix */
1034 if (chan->modified && (shared_by == IIO_SEPARATE)) {
1035 if (chan->extend_name)
1036 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
1037 iio_modifier_names[chan
1042 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
1043 iio_modifier_names[chan
1047 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
1048 full_postfix = kstrdup(postfix, GFP_KERNEL);
1050 full_postfix = kasprintf(GFP_KERNEL,
1055 if (full_postfix == NULL)
1058 if (chan->differential) { /* Differential can not have modifier */
1059 switch (shared_by) {
1060 case IIO_SHARED_BY_ALL:
1061 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1063 case IIO_SHARED_BY_DIR:
1064 name = kasprintf(GFP_KERNEL, "%s_%s",
1065 iio_direction[chan->output],
1068 case IIO_SHARED_BY_TYPE:
1069 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
1070 iio_direction[chan->output],
1071 iio_chan_type_name_spec[chan->type],
1072 iio_chan_type_name_spec[chan->type],
1076 if (!chan->indexed) {
1077 WARN(1, "Differential channels must be indexed\n");
1079 goto error_free_full_postfix;
1081 name = kasprintf(GFP_KERNEL,
1083 iio_direction[chan->output],
1084 iio_chan_type_name_spec[chan->type],
1086 iio_chan_type_name_spec[chan->type],
1091 } else { /* Single ended */
1092 switch (shared_by) {
1093 case IIO_SHARED_BY_ALL:
1094 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1096 case IIO_SHARED_BY_DIR:
1097 name = kasprintf(GFP_KERNEL, "%s_%s",
1098 iio_direction[chan->output],
1101 case IIO_SHARED_BY_TYPE:
1102 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1103 iio_direction[chan->output],
1104 iio_chan_type_name_spec[chan->type],
1110 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
1111 iio_direction[chan->output],
1112 iio_chan_type_name_spec[chan->type],
1116 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1117 iio_direction[chan->output],
1118 iio_chan_type_name_spec[chan->type],
1125 goto error_free_full_postfix;
1127 dev_attr->attr.name = name;
1130 dev_attr->attr.mode |= S_IRUGO;
1131 dev_attr->show = readfunc;
1135 dev_attr->attr.mode |= S_IWUSR;
1136 dev_attr->store = writefunc;
1139 error_free_full_postfix:
1140 kfree(full_postfix);
1145 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1147 kfree(dev_attr->attr.name);
1150 int __iio_add_chan_devattr(const char *postfix,
1151 struct iio_chan_spec const *chan,
1152 ssize_t (*readfunc)(struct device *dev,
1153 struct device_attribute *attr,
1155 ssize_t (*writefunc)(struct device *dev,
1156 struct device_attribute *attr,
1160 enum iio_shared_by shared_by,
1162 struct iio_buffer *buffer,
1163 struct list_head *attr_list)
1166 struct iio_dev_attr *iio_attr, *t;
1168 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
1169 if (iio_attr == NULL)
1171 ret = __iio_device_attr_init(&iio_attr->dev_attr,
1173 readfunc, writefunc, shared_by);
1175 goto error_iio_dev_attr_free;
1177 iio_attr->address = mask;
1178 iio_attr->buffer = buffer;
1179 list_for_each_entry(t, attr_list, l)
1180 if (strcmp(t->dev_attr.attr.name,
1181 iio_attr->dev_attr.attr.name) == 0) {
1182 if (shared_by == IIO_SEPARATE)
1183 dev_err(dev, "tried to double register : %s\n",
1184 t->dev_attr.attr.name);
1186 goto error_device_attr_deinit;
1188 list_add(&iio_attr->l, attr_list);
1192 error_device_attr_deinit:
1193 __iio_device_attr_deinit(&iio_attr->dev_attr);
1194 error_iio_dev_attr_free:
1199 static int iio_device_add_channel_label(struct iio_dev *indio_dev,
1200 struct iio_chan_spec const *chan)
1202 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1205 if (!indio_dev->info->read_label && !chan->extend_name)
1208 ret = __iio_add_chan_devattr("label",
1210 &iio_read_channel_label,
1216 &iio_dev_opaque->channel_attr_list);
1223 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1224 struct iio_chan_spec const *chan,
1225 enum iio_shared_by shared_by,
1226 const long *infomask)
1228 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1229 int i, ret, attrcount = 0;
1231 for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1232 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1234 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1236 &iio_read_channel_info,
1237 &iio_write_channel_info,
1242 &iio_dev_opaque->channel_attr_list);
1243 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1253 static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1254 struct iio_chan_spec const *chan,
1255 enum iio_shared_by shared_by,
1256 const long *infomask)
1258 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1259 int i, ret, attrcount = 0;
1260 char *avail_postfix;
1262 for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1263 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1265 avail_postfix = kasprintf(GFP_KERNEL,
1267 iio_chan_info_postfix[i]);
1271 ret = __iio_add_chan_devattr(avail_postfix,
1273 &iio_read_channel_info_avail,
1279 &iio_dev_opaque->channel_attr_list);
1280 kfree(avail_postfix);
1281 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1291 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1292 struct iio_chan_spec const *chan)
1294 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1295 int ret, attrcount = 0;
1296 const struct iio_chan_spec_ext_info *ext_info;
1298 if (chan->channel < 0)
1300 ret = iio_device_add_info_mask_type(indio_dev, chan,
1302 &chan->info_mask_separate);
1307 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1310 info_mask_separate_available);
1315 ret = iio_device_add_info_mask_type(indio_dev, chan,
1317 &chan->info_mask_shared_by_type);
1322 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1325 info_mask_shared_by_type_available);
1330 ret = iio_device_add_info_mask_type(indio_dev, chan,
1332 &chan->info_mask_shared_by_dir);
1337 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1339 &chan->info_mask_shared_by_dir_available);
1344 ret = iio_device_add_info_mask_type(indio_dev, chan,
1346 &chan->info_mask_shared_by_all);
1351 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1353 &chan->info_mask_shared_by_all_available);
1358 ret = iio_device_add_channel_label(indio_dev, chan);
1363 if (chan->ext_info) {
1365 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1366 ret = __iio_add_chan_devattr(ext_info->name,
1369 &iio_read_channel_ext_info : NULL,
1371 &iio_write_channel_ext_info : NULL,
1376 &iio_dev_opaque->channel_attr_list);
1378 if (ret == -EBUSY && ext_info->shared)
1392 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1393 * @attr_list: List of IIO device attributes
1395 * This function frees the memory allocated for each of the IIO device
1396 * attributes in the list.
1398 void iio_free_chan_devattr_list(struct list_head *attr_list)
1400 struct iio_dev_attr *p, *n;
1402 list_for_each_entry_safe(p, n, attr_list, l) {
1403 kfree_const(p->dev_attr.attr.name);
1409 static ssize_t iio_show_dev_name(struct device *dev,
1410 struct device_attribute *attr,
1413 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1414 return sysfs_emit(buf, "%s\n", indio_dev->name);
1417 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1419 static ssize_t iio_show_dev_label(struct device *dev,
1420 struct device_attribute *attr,
1423 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1424 return sysfs_emit(buf, "%s\n", indio_dev->label);
1427 static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL);
1429 static ssize_t iio_show_timestamp_clock(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);
1439 case CLOCK_REALTIME:
1440 name = "realtime\n";
1441 sz = sizeof("realtime\n");
1443 case CLOCK_MONOTONIC:
1444 name = "monotonic\n";
1445 sz = sizeof("monotonic\n");
1447 case CLOCK_MONOTONIC_RAW:
1448 name = "monotonic_raw\n";
1449 sz = sizeof("monotonic_raw\n");
1451 case CLOCK_REALTIME_COARSE:
1452 name = "realtime_coarse\n";
1453 sz = sizeof("realtime_coarse\n");
1455 case CLOCK_MONOTONIC_COARSE:
1456 name = "monotonic_coarse\n";
1457 sz = sizeof("monotonic_coarse\n");
1459 case CLOCK_BOOTTIME:
1460 name = "boottime\n";
1461 sz = sizeof("boottime\n");
1465 sz = sizeof("tai\n");
1471 memcpy(buf, name, sz);
1475 static ssize_t iio_store_timestamp_clock(struct device *dev,
1476 struct device_attribute *attr,
1477 const char *buf, size_t len)
1482 if (sysfs_streq(buf, "realtime"))
1483 clk = CLOCK_REALTIME;
1484 else if (sysfs_streq(buf, "monotonic"))
1485 clk = CLOCK_MONOTONIC;
1486 else if (sysfs_streq(buf, "monotonic_raw"))
1487 clk = CLOCK_MONOTONIC_RAW;
1488 else if (sysfs_streq(buf, "realtime_coarse"))
1489 clk = CLOCK_REALTIME_COARSE;
1490 else if (sysfs_streq(buf, "monotonic_coarse"))
1491 clk = CLOCK_MONOTONIC_COARSE;
1492 else if (sysfs_streq(buf, "boottime"))
1493 clk = CLOCK_BOOTTIME;
1494 else if (sysfs_streq(buf, "tai"))
1499 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1506 int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
1507 const struct attribute_group *group)
1509 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1510 const struct attribute_group **new, **old = iio_dev_opaque->groups;
1511 unsigned int cnt = iio_dev_opaque->groupcounter;
1513 new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
1517 new[iio_dev_opaque->groupcounter++] = group;
1518 new[iio_dev_opaque->groupcounter] = NULL;
1520 iio_dev_opaque->groups = new;
1525 static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1526 iio_show_timestamp_clock, iio_store_timestamp_clock);
1528 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1530 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1531 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1532 struct iio_dev_attr *p;
1533 struct attribute **attr, *clk = NULL;
1535 /* First count elements in any existing group */
1536 if (indio_dev->info->attrs) {
1537 attr = indio_dev->info->attrs->attrs;
1538 while (*attr++ != NULL)
1541 attrcount = attrcount_orig;
1543 * New channel registration method - relies on the fact a group does
1544 * not need to be initialized if its name is NULL.
1546 if (indio_dev->channels)
1547 for (i = 0; i < indio_dev->num_channels; i++) {
1548 const struct iio_chan_spec *chan =
1549 &indio_dev->channels[i];
1551 if (chan->type == IIO_TIMESTAMP)
1552 clk = &dev_attr_current_timestamp_clock.attr;
1554 ret = iio_device_add_channel_sysfs(indio_dev, chan);
1556 goto error_clear_attrs;
1560 if (iio_dev_opaque->event_interface)
1561 clk = &dev_attr_current_timestamp_clock.attr;
1563 if (indio_dev->name)
1565 if (indio_dev->label)
1570 iio_dev_opaque->chan_attr_group.attrs =
1571 kcalloc(attrcount + 1,
1572 sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1574 if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
1576 goto error_clear_attrs;
1578 /* Copy across original attributes */
1579 if (indio_dev->info->attrs) {
1580 memcpy(iio_dev_opaque->chan_attr_group.attrs,
1581 indio_dev->info->attrs->attrs,
1582 sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
1584 iio_dev_opaque->chan_attr_group.is_visible =
1585 indio_dev->info->attrs->is_visible;
1587 attrn = attrcount_orig;
1588 /* Add all elements from the list. */
1589 list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1590 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1591 if (indio_dev->name)
1592 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1593 if (indio_dev->label)
1594 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1596 iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
1598 ret = iio_device_register_sysfs_group(indio_dev,
1599 &iio_dev_opaque->chan_attr_group);
1601 goto error_clear_attrs;
1606 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1611 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1613 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1615 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1616 kfree(iio_dev_opaque->chan_attr_group.attrs);
1617 iio_dev_opaque->chan_attr_group.attrs = NULL;
1618 kfree(iio_dev_opaque->groups);
1619 iio_dev_opaque->groups = NULL;
1622 static void iio_dev_release(struct device *device)
1624 struct iio_dev *indio_dev = dev_to_iio_dev(device);
1625 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1627 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1628 iio_device_unregister_trigger_consumer(indio_dev);
1629 iio_device_unregister_eventset(indio_dev);
1630 iio_device_unregister_sysfs(indio_dev);
1632 iio_device_detach_buffers(indio_dev);
1634 ida_simple_remove(&iio_ida, iio_dev_opaque->id);
1635 kfree(iio_dev_opaque);
1638 const struct device_type iio_device_type = {
1639 .name = "iio_device",
1640 .release = iio_dev_release,
1644 * iio_device_alloc() - allocate an iio_dev from a driver
1645 * @parent: Parent device.
1646 * @sizeof_priv: Space to allocate for private structure.
1648 struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
1650 struct iio_dev_opaque *iio_dev_opaque;
1651 struct iio_dev *indio_dev;
1654 alloc_size = sizeof(struct iio_dev_opaque);
1656 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1657 alloc_size += sizeof_priv;
1660 iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1661 if (!iio_dev_opaque)
1664 indio_dev = &iio_dev_opaque->indio_dev;
1665 indio_dev->priv = (char *)iio_dev_opaque +
1666 ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
1668 indio_dev->dev.parent = parent;
1669 indio_dev->dev.type = &iio_device_type;
1670 indio_dev->dev.bus = &iio_bus_type;
1671 device_initialize(&indio_dev->dev);
1672 mutex_init(&indio_dev->mlock);
1673 mutex_init(&iio_dev_opaque->info_exist_lock);
1674 INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
1676 iio_dev_opaque->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1677 if (iio_dev_opaque->id < 0) {
1678 /* cannot use a dev_err as the name isn't available */
1679 pr_err("failed to get device id\n");
1680 kfree(iio_dev_opaque);
1684 if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) {
1685 ida_simple_remove(&iio_ida, iio_dev_opaque->id);
1686 kfree(iio_dev_opaque);
1690 INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
1691 INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
1695 EXPORT_SYMBOL(iio_device_alloc);
1698 * iio_device_free() - free an iio_dev from a driver
1699 * @dev: the iio_dev associated with the device
1701 void iio_device_free(struct iio_dev *dev)
1704 put_device(&dev->dev);
1706 EXPORT_SYMBOL(iio_device_free);
1708 static void devm_iio_device_release(void *iio_dev)
1710 iio_device_free(iio_dev);
1714 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1715 * @parent: Device to allocate iio_dev for, and parent for this IIO device
1716 * @sizeof_priv: Space to allocate for private structure.
1718 * Managed iio_device_alloc. iio_dev allocated with this function is
1719 * automatically freed on driver detach.
1722 * Pointer to allocated iio_dev on success, NULL on failure.
1724 struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
1726 struct iio_dev *iio_dev;
1729 iio_dev = iio_device_alloc(parent, sizeof_priv);
1733 ret = devm_add_action_or_reset(parent, devm_iio_device_release,
1740 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1743 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1744 * @inode: Inode structure for identifying the device in the file system
1745 * @filp: File structure for iio device used to keep and later access
1748 * Return: 0 on success or -EBUSY if the device is already opened
1750 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1752 struct iio_dev_opaque *iio_dev_opaque =
1753 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1754 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1755 struct iio_dev_buffer_pair *ib;
1757 if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags))
1760 iio_device_get(indio_dev);
1762 ib = kmalloc(sizeof(*ib), GFP_KERNEL);
1764 iio_device_put(indio_dev);
1765 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1769 ib->indio_dev = indio_dev;
1770 ib->buffer = indio_dev->buffer;
1772 filp->private_data = ib;
1778 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1779 * @inode: Inode structure pointer for the char device
1780 * @filp: File structure pointer for the char device
1782 * Return: 0 for successful release
1784 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1786 struct iio_dev_buffer_pair *ib = filp->private_data;
1787 struct iio_dev_opaque *iio_dev_opaque =
1788 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1789 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1791 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1792 iio_device_put(indio_dev);
1797 void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
1798 struct iio_ioctl_handler *h)
1800 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1802 list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers);
1805 void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h)
1807 list_del(&h->entry);
1810 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1812 struct iio_dev_buffer_pair *ib = filp->private_data;
1813 struct iio_dev *indio_dev = ib->indio_dev;
1814 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1815 struct iio_ioctl_handler *h;
1818 mutex_lock(&iio_dev_opaque->info_exist_lock);
1821 * The NULL check here is required to prevent crashing when a device
1822 * is being removed while userspace would still have open file handles
1823 * to try to access this device.
1825 if (!indio_dev->info)
1828 list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
1829 ret = h->ioctl(indio_dev, filp, cmd, arg);
1830 if (ret != IIO_IOCTL_UNHANDLED)
1834 if (ret == IIO_IOCTL_UNHANDLED)
1838 mutex_unlock(&iio_dev_opaque->info_exist_lock);
1843 static const struct file_operations iio_buffer_fileops = {
1844 .owner = THIS_MODULE,
1845 .llseek = noop_llseek,
1846 .read = iio_buffer_read_outer_addr,
1847 .write = iio_buffer_write_outer_addr,
1848 .poll = iio_buffer_poll_addr,
1849 .unlocked_ioctl = iio_ioctl,
1850 .compat_ioctl = compat_ptr_ioctl,
1851 .open = iio_chrdev_open,
1852 .release = iio_chrdev_release,
1855 static const struct file_operations iio_event_fileops = {
1856 .owner = THIS_MODULE,
1857 .llseek = noop_llseek,
1858 .unlocked_ioctl = iio_ioctl,
1859 .compat_ioctl = compat_ptr_ioctl,
1860 .open = iio_chrdev_open,
1861 .release = iio_chrdev_release,
1864 static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1867 const struct iio_chan_spec *channels = indio_dev->channels;
1869 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1872 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1873 if (channels[i].scan_index < 0)
1875 for (j = i + 1; j < indio_dev->num_channels; j++)
1876 if (channels[i].scan_index == channels[j].scan_index) {
1877 dev_err(&indio_dev->dev,
1878 "Duplicate scan index %d\n",
1879 channels[i].scan_index);
1887 static int iio_check_extended_name(const struct iio_dev *indio_dev)
1891 if (!indio_dev->info->read_label)
1894 for (i = 0; i < indio_dev->num_channels; i++) {
1895 if (indio_dev->channels[i].extend_name) {
1896 dev_err(&indio_dev->dev,
1897 "Cannot use labels and extend_name at the same time\n");
1905 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1907 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1909 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1910 struct fwnode_handle *fwnode;
1913 if (!indio_dev->info)
1916 iio_dev_opaque->driver_module = this_mod;
1918 /* If the calling driver did not initialize firmware node, do it here */
1919 if (dev_fwnode(&indio_dev->dev))
1920 fwnode = dev_fwnode(&indio_dev->dev);
1922 fwnode = dev_fwnode(indio_dev->dev.parent);
1923 device_set_node(&indio_dev->dev, fwnode);
1925 fwnode_property_read_string(fwnode, "label", &indio_dev->label);
1927 ret = iio_check_unique_scan_index(indio_dev);
1931 ret = iio_check_extended_name(indio_dev);
1935 iio_device_register_debugfs(indio_dev);
1937 ret = iio_buffers_alloc_sysfs_and_mask(indio_dev);
1939 dev_err(indio_dev->dev.parent,
1940 "Failed to create buffer sysfs interfaces\n");
1941 goto error_unreg_debugfs;
1944 ret = iio_device_register_sysfs(indio_dev);
1946 dev_err(indio_dev->dev.parent,
1947 "Failed to register sysfs interfaces\n");
1948 goto error_buffer_free_sysfs;
1950 ret = iio_device_register_eventset(indio_dev);
1952 dev_err(indio_dev->dev.parent,
1953 "Failed to register event set\n");
1954 goto error_free_sysfs;
1956 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1957 iio_device_register_trigger_consumer(indio_dev);
1959 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1960 indio_dev->setup_ops == NULL)
1961 indio_dev->setup_ops = &noop_ring_setup_ops;
1963 if (iio_dev_opaque->attached_buffers_cnt)
1964 cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops);
1965 else if (iio_dev_opaque->event_interface)
1966 cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops);
1968 if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) {
1969 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id);
1970 iio_dev_opaque->chrdev.owner = this_mod;
1973 /* assign device groups now; they should be all registered now */
1974 indio_dev->dev.groups = iio_dev_opaque->groups;
1976 ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev);
1978 goto error_unreg_eventset;
1982 error_unreg_eventset:
1983 iio_device_unregister_eventset(indio_dev);
1985 iio_device_unregister_sysfs(indio_dev);
1986 error_buffer_free_sysfs:
1987 iio_buffers_free_sysfs_and_mask(indio_dev);
1988 error_unreg_debugfs:
1989 iio_device_unregister_debugfs(indio_dev);
1992 EXPORT_SYMBOL(__iio_device_register);
1995 * iio_device_unregister() - unregister a device from the IIO subsystem
1996 * @indio_dev: Device structure representing the device.
1998 void iio_device_unregister(struct iio_dev *indio_dev)
2000 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2002 cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev);
2004 mutex_lock(&iio_dev_opaque->info_exist_lock);
2006 iio_device_unregister_debugfs(indio_dev);
2008 iio_disable_all_buffers(indio_dev);
2010 indio_dev->info = NULL;
2012 iio_device_wakeup_eventset(indio_dev);
2013 iio_buffer_wakeup_poll(indio_dev);
2015 mutex_unlock(&iio_dev_opaque->info_exist_lock);
2017 iio_buffers_free_sysfs_and_mask(indio_dev);
2019 EXPORT_SYMBOL(iio_device_unregister);
2021 static void devm_iio_device_unreg(void *indio_dev)
2023 iio_device_unregister(indio_dev);
2026 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
2027 struct module *this_mod)
2031 ret = __iio_device_register(indio_dev, this_mod);
2035 return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev);
2037 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
2040 * iio_device_claim_direct_mode - Keep device in direct mode
2041 * @indio_dev: the iio_dev associated with the device
2043 * If the device is in direct mode it is guaranteed to stay
2044 * that way until iio_device_release_direct_mode() is called.
2046 * Use with iio_device_release_direct_mode()
2048 * Returns: 0 on success, -EBUSY on failure
2050 int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
2052 mutex_lock(&indio_dev->mlock);
2054 if (iio_buffer_enabled(indio_dev)) {
2055 mutex_unlock(&indio_dev->mlock);
2060 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
2063 * iio_device_release_direct_mode - releases claim on direct mode
2064 * @indio_dev: the iio_dev associated with the device
2066 * Release the claim. Device is no longer guaranteed to stay
2069 * Use with iio_device_claim_direct_mode()
2071 void iio_device_release_direct_mode(struct iio_dev *indio_dev)
2073 mutex_unlock(&indio_dev->mlock);
2075 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
2078 * iio_device_get_current_mode() - helper function providing read-only access to
2079 * the opaque @currentmode variable
2080 * @indio_dev: IIO device structure for device
2082 int iio_device_get_current_mode(struct iio_dev *indio_dev)
2084 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2086 return iio_dev_opaque->currentmode;
2088 EXPORT_SYMBOL_GPL(iio_device_get_current_mode);
2090 subsys_initcall(iio_init);
2091 module_exit(iio_exit);
2093 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
2094 MODULE_DESCRIPTION("Industrial I/O core");
2095 MODULE_LICENSE("GPL");