1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * Based on elements of hwmon and input subsystems.
12 #define pr_fmt(fmt) "iio-core: " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/idr.h>
17 #include <linux/kdev_t.h>
18 #include <linux/err.h>
19 #include <linux/device.h>
21 #include <linux/poll.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
24 #include <linux/cdev.h>
25 #include <linux/slab.h>
26 #include <linux/anon_inodes.h>
27 #include <linux/debugfs.h>
28 #include <linux/iio/iio.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>
35 /* IDA to assign each registered device a unique id */
36 static DEFINE_IDA(iio_ida);
38 static dev_t iio_devt;
40 #define IIO_DEV_MAX 256
41 struct bus_type iio_bus_type = {
44 EXPORT_SYMBOL(iio_bus_type);
46 static struct dentry *iio_debugfs_dentry;
48 static const char * const iio_direction[] = {
53 static const char * const iio_chan_type_name_spec[] = {
54 [IIO_VOLTAGE] = "voltage",
55 [IIO_CURRENT] = "current",
56 [IIO_POWER] = "power",
57 [IIO_ACCEL] = "accel",
58 [IIO_ANGL_VEL] = "anglvel",
60 [IIO_LIGHT] = "illuminance",
61 [IIO_INTENSITY] = "intensity",
62 [IIO_PROXIMITY] = "proximity",
64 [IIO_INCLI] = "incli",
67 [IIO_TIMESTAMP] = "timestamp",
68 [IIO_CAPACITANCE] = "capacitance",
69 [IIO_ALTVOLTAGE] = "altvoltage",
71 [IIO_PRESSURE] = "pressure",
72 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
75 static const char * const iio_modifier_names[] = {
79 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
80 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
81 [IIO_MOD_LIGHT_BOTH] = "both",
82 [IIO_MOD_LIGHT_IR] = "ir",
83 [IIO_MOD_LIGHT_CLEAR] = "clear",
84 [IIO_MOD_LIGHT_RED] = "red",
85 [IIO_MOD_LIGHT_GREEN] = "green",
86 [IIO_MOD_LIGHT_BLUE] = "blue",
87 [IIO_MOD_QUATERNION] = "quaternion",
88 [IIO_MOD_TEMP_AMBIENT] = "ambient",
89 [IIO_MOD_TEMP_OBJECT] = "object",
90 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
91 [IIO_MOD_NORTH_TRUE] = "from_north_true",
92 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
93 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
96 /* relies on pairs of these shared then separate */
97 static const char * const iio_chan_info_postfix[] = {
98 [IIO_CHAN_INFO_RAW] = "raw",
99 [IIO_CHAN_INFO_PROCESSED] = "input",
100 [IIO_CHAN_INFO_SCALE] = "scale",
101 [IIO_CHAN_INFO_OFFSET] = "offset",
102 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
103 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
104 [IIO_CHAN_INFO_PEAK] = "peak_raw",
105 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
106 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
107 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
108 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
109 = "filter_low_pass_3db_frequency",
110 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
111 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
112 [IIO_CHAN_INFO_PHASE] = "phase",
113 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
114 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
115 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
119 * iio_find_channel_from_si() - get channel from its scan index
121 * @si: scan index to match
123 const struct iio_chan_spec
124 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
128 for (i = 0; i < indio_dev->num_channels; i++)
129 if (indio_dev->channels[i].scan_index == si)
130 return &indio_dev->channels[i];
134 /* This turns up an awful lot */
135 ssize_t iio_read_const_attr(struct device *dev,
136 struct device_attribute *attr,
139 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
141 EXPORT_SYMBOL(iio_read_const_attr);
143 static int __init iio_init(void)
147 /* Register sysfs bus */
148 ret = bus_register(&iio_bus_type);
150 pr_err("could not register bus type\n");
154 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
156 pr_err("failed to allocate char dev region\n");
157 goto error_unregister_bus_type;
160 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
164 error_unregister_bus_type:
165 bus_unregister(&iio_bus_type);
170 static void __exit iio_exit(void)
173 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
174 bus_unregister(&iio_bus_type);
175 debugfs_remove(iio_debugfs_dentry);
178 #if defined(CONFIG_DEBUG_FS)
179 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
180 size_t count, loff_t *ppos)
182 struct iio_dev *indio_dev = file->private_data;
188 ret = indio_dev->info->debugfs_reg_access(indio_dev,
189 indio_dev->cached_reg_addr,
192 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
194 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
196 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
199 static ssize_t iio_debugfs_write_reg(struct file *file,
200 const char __user *userbuf, size_t count, loff_t *ppos)
202 struct iio_dev *indio_dev = file->private_data;
207 count = min_t(size_t, count, (sizeof(buf)-1));
208 if (copy_from_user(buf, userbuf, count))
213 ret = sscanf(buf, "%i %i", ®, &val);
217 indio_dev->cached_reg_addr = reg;
220 indio_dev->cached_reg_addr = reg;
221 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
224 dev_err(indio_dev->dev.parent, "%s: write failed\n",
236 static const struct file_operations iio_debugfs_reg_fops = {
238 .read = iio_debugfs_read_reg,
239 .write = iio_debugfs_write_reg,
242 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
244 debugfs_remove_recursive(indio_dev->debugfs_dentry);
247 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
251 if (indio_dev->info->debugfs_reg_access == NULL)
254 if (!iio_debugfs_dentry)
257 indio_dev->debugfs_dentry =
258 debugfs_create_dir(dev_name(&indio_dev->dev),
260 if (indio_dev->debugfs_dentry == NULL) {
261 dev_warn(indio_dev->dev.parent,
262 "Failed to create debugfs directory\n");
266 d = debugfs_create_file("direct_reg_access", 0644,
267 indio_dev->debugfs_dentry,
268 indio_dev, &iio_debugfs_reg_fops);
270 iio_device_unregister_debugfs(indio_dev);
277 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
282 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
285 #endif /* CONFIG_DEBUG_FS */
287 static ssize_t iio_read_channel_ext_info(struct device *dev,
288 struct device_attribute *attr,
291 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
292 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
293 const struct iio_chan_spec_ext_info *ext_info;
295 ext_info = &this_attr->c->ext_info[this_attr->address];
297 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
300 static ssize_t iio_write_channel_ext_info(struct device *dev,
301 struct device_attribute *attr,
305 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
306 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
307 const struct iio_chan_spec_ext_info *ext_info;
309 ext_info = &this_attr->c->ext_info[this_attr->address];
311 return ext_info->write(indio_dev, ext_info->private,
312 this_attr->c, buf, len);
315 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
316 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
318 const struct iio_enum *e = (const struct iio_enum *)priv;
325 for (i = 0; i < e->num_items; ++i)
326 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
328 /* replace last space with a newline */
333 EXPORT_SYMBOL_GPL(iio_enum_available_read);
335 ssize_t iio_enum_read(struct iio_dev *indio_dev,
336 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
338 const struct iio_enum *e = (const struct iio_enum *)priv;
344 i = e->get(indio_dev, chan);
347 else if (i >= e->num_items)
350 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
352 EXPORT_SYMBOL_GPL(iio_enum_read);
354 ssize_t iio_enum_write(struct iio_dev *indio_dev,
355 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
358 const struct iio_enum *e = (const struct iio_enum *)priv;
365 for (i = 0; i < e->num_items; i++) {
366 if (sysfs_streq(buf, e->items[i]))
370 if (i == e->num_items)
373 ret = e->set(indio_dev, chan, i);
374 return ret ? ret : len;
376 EXPORT_SYMBOL_GPL(iio_enum_write);
379 * iio_format_value() - Formats a IIO value into its string representation
380 * @buf: The buffer to which the formated value gets written
381 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
382 * parameters are formatted.
383 * @vals: pointer to the values, exact meaning depends on the type parameter.
385 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
387 unsigned long long tmp;
388 bool scale_db = false;
392 return sprintf(buf, "%d\n", vals[0]);
393 case IIO_VAL_INT_PLUS_MICRO_DB:
395 case IIO_VAL_INT_PLUS_MICRO:
397 return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
399 scale_db ? " dB" : "");
401 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
402 scale_db ? " dB" : "");
403 case IIO_VAL_INT_PLUS_NANO:
405 return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
408 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
409 case IIO_VAL_FRACTIONAL:
410 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
411 vals[1] = do_div(tmp, 1000000000LL);
413 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
414 case IIO_VAL_FRACTIONAL_LOG2:
415 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
416 vals[1] = do_div(tmp, 1000000000LL);
418 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
419 case IIO_VAL_INT_MULTIPLE:
424 for (i = 0; i < size; ++i)
425 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
427 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
435 static ssize_t iio_read_channel_info(struct device *dev,
436 struct device_attribute *attr,
439 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
440 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
441 int vals[INDIO_MAX_RAW_ELEMENTS];
445 if (indio_dev->info->read_raw_multi)
446 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
447 INDIO_MAX_RAW_ELEMENTS,
451 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
452 &vals[0], &vals[1], this_attr->address);
457 return iio_format_value(buf, ret, val_len, vals);
461 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
462 * @str: The string to parse
463 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
464 * @integer: The integer part of the number
465 * @fract: The fractional part of the number
467 * Returns 0 on success, or a negative error code if the string could not be
470 int iio_str_to_fixpoint(const char *str, int fract_mult,
471 int *integer, int *fract)
474 bool integer_part = true, negative = false;
479 } else if (str[0] == '+') {
484 if ('0' <= *str && *str <= '9') {
486 i = i * 10 + *str - '0';
488 f += fract_mult * (*str - '0');
491 } else if (*str == '\n') {
492 if (*(str + 1) == '\0')
496 } else if (*str == '.' && integer_part) {
497 integer_part = false;
516 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
518 static ssize_t iio_write_channel_info(struct device *dev,
519 struct device_attribute *attr,
523 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
524 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
525 int ret, fract_mult = 100000;
528 /* Assumes decimal - precision based on number of digits */
529 if (!indio_dev->info->write_raw)
532 if (indio_dev->info->write_raw_get_fmt)
533 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
534 this_attr->c, this_attr->address)) {
535 case IIO_VAL_INT_PLUS_MICRO:
538 case IIO_VAL_INT_PLUS_NANO:
539 fract_mult = 100000000;
545 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
549 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
550 integer, fract, this_attr->address);
558 int __iio_device_attr_init(struct device_attribute *dev_attr,
560 struct iio_chan_spec const *chan,
561 ssize_t (*readfunc)(struct device *dev,
562 struct device_attribute *attr,
564 ssize_t (*writefunc)(struct device *dev,
565 struct device_attribute *attr,
568 enum iio_shared_by shared_by)
573 sysfs_attr_init(&dev_attr->attr);
575 /* Build up postfix of <extend_name>_<modifier>_postfix */
576 if (chan->modified && (shared_by == IIO_SEPARATE)) {
577 if (chan->extend_name)
578 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
579 iio_modifier_names[chan
584 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
585 iio_modifier_names[chan
589 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
590 full_postfix = kstrdup(postfix, GFP_KERNEL);
592 full_postfix = kasprintf(GFP_KERNEL,
597 if (full_postfix == NULL)
600 if (chan->differential) { /* Differential can not have modifier */
602 case IIO_SHARED_BY_ALL:
603 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
605 case IIO_SHARED_BY_DIR:
606 name = kasprintf(GFP_KERNEL, "%s_%s",
607 iio_direction[chan->output],
610 case IIO_SHARED_BY_TYPE:
611 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
612 iio_direction[chan->output],
613 iio_chan_type_name_spec[chan->type],
614 iio_chan_type_name_spec[chan->type],
618 if (!chan->indexed) {
619 WARN_ON("Differential channels must be indexed\n");
621 goto error_free_full_postfix;
623 name = kasprintf(GFP_KERNEL,
625 iio_direction[chan->output],
626 iio_chan_type_name_spec[chan->type],
628 iio_chan_type_name_spec[chan->type],
633 } else { /* Single ended */
635 case IIO_SHARED_BY_ALL:
636 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
638 case IIO_SHARED_BY_DIR:
639 name = kasprintf(GFP_KERNEL, "%s_%s",
640 iio_direction[chan->output],
643 case IIO_SHARED_BY_TYPE:
644 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
645 iio_direction[chan->output],
646 iio_chan_type_name_spec[chan->type],
652 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
653 iio_direction[chan->output],
654 iio_chan_type_name_spec[chan->type],
658 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
659 iio_direction[chan->output],
660 iio_chan_type_name_spec[chan->type],
667 goto error_free_full_postfix;
669 dev_attr->attr.name = name;
672 dev_attr->attr.mode |= S_IRUGO;
673 dev_attr->show = readfunc;
677 dev_attr->attr.mode |= S_IWUSR;
678 dev_attr->store = writefunc;
681 error_free_full_postfix:
687 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
689 kfree(dev_attr->attr.name);
692 int __iio_add_chan_devattr(const char *postfix,
693 struct iio_chan_spec const *chan,
694 ssize_t (*readfunc)(struct device *dev,
695 struct device_attribute *attr,
697 ssize_t (*writefunc)(struct device *dev,
698 struct device_attribute *attr,
702 enum iio_shared_by shared_by,
704 struct list_head *attr_list)
707 struct iio_dev_attr *iio_attr, *t;
709 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
710 if (iio_attr == NULL)
712 ret = __iio_device_attr_init(&iio_attr->dev_attr,
714 readfunc, writefunc, shared_by);
716 goto error_iio_dev_attr_free;
718 iio_attr->address = mask;
719 list_for_each_entry(t, attr_list, l)
720 if (strcmp(t->dev_attr.attr.name,
721 iio_attr->dev_attr.attr.name) == 0) {
722 if (shared_by == IIO_SEPARATE)
723 dev_err(dev, "tried to double register : %s\n",
724 t->dev_attr.attr.name);
726 goto error_device_attr_deinit;
728 list_add(&iio_attr->l, attr_list);
732 error_device_attr_deinit:
733 __iio_device_attr_deinit(&iio_attr->dev_attr);
734 error_iio_dev_attr_free:
739 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
740 struct iio_chan_spec const *chan,
741 enum iio_shared_by shared_by,
742 const long *infomask)
744 int i, ret, attrcount = 0;
746 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
747 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
749 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
751 &iio_read_channel_info,
752 &iio_write_channel_info,
756 &indio_dev->channel_attr_list);
757 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
767 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
768 struct iio_chan_spec const *chan)
770 int ret, attrcount = 0;
771 const struct iio_chan_spec_ext_info *ext_info;
773 if (chan->channel < 0)
775 ret = iio_device_add_info_mask_type(indio_dev, chan,
777 &chan->info_mask_separate);
782 ret = iio_device_add_info_mask_type(indio_dev, chan,
784 &chan->info_mask_shared_by_type);
789 ret = iio_device_add_info_mask_type(indio_dev, chan,
791 &chan->info_mask_shared_by_dir);
796 ret = iio_device_add_info_mask_type(indio_dev, chan,
798 &chan->info_mask_shared_by_all);
803 if (chan->ext_info) {
805 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
806 ret = __iio_add_chan_devattr(ext_info->name,
809 &iio_read_channel_ext_info : NULL,
811 &iio_write_channel_ext_info : NULL,
815 &indio_dev->channel_attr_list);
817 if (ret == -EBUSY && ext_info->shared)
831 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
832 * @attr_list: List of IIO device attributes
834 * This function frees the memory allocated for each of the IIO device
835 * attributes in the list. Note: if you want to reuse the list after calling
836 * this function you have to reinitialize it using INIT_LIST_HEAD().
838 void iio_free_chan_devattr_list(struct list_head *attr_list)
840 struct iio_dev_attr *p, *n;
842 list_for_each_entry_safe(p, n, attr_list, l) {
843 kfree(p->dev_attr.attr.name);
848 static ssize_t iio_show_dev_name(struct device *dev,
849 struct device_attribute *attr,
852 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
853 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
856 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
858 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
860 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
861 struct iio_dev_attr *p;
862 struct attribute **attr;
864 /* First count elements in any existing group */
865 if (indio_dev->info->attrs) {
866 attr = indio_dev->info->attrs->attrs;
867 while (*attr++ != NULL)
870 attrcount = attrcount_orig;
872 * New channel registration method - relies on the fact a group does
873 * not need to be initialized if its name is NULL.
875 if (indio_dev->channels)
876 for (i = 0; i < indio_dev->num_channels; i++) {
877 ret = iio_device_add_channel_sysfs(indio_dev,
881 goto error_clear_attrs;
888 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
889 sizeof(indio_dev->chan_attr_group.attrs[0]),
891 if (indio_dev->chan_attr_group.attrs == NULL) {
893 goto error_clear_attrs;
895 /* Copy across original attributes */
896 if (indio_dev->info->attrs)
897 memcpy(indio_dev->chan_attr_group.attrs,
898 indio_dev->info->attrs->attrs,
899 sizeof(indio_dev->chan_attr_group.attrs[0])
901 attrn = attrcount_orig;
902 /* Add all elements from the list. */
903 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
904 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
906 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
908 indio_dev->groups[indio_dev->groupcounter++] =
909 &indio_dev->chan_attr_group;
914 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
919 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
922 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
923 kfree(indio_dev->chan_attr_group.attrs);
926 static void iio_dev_release(struct device *device)
928 struct iio_dev *indio_dev = dev_to_iio_dev(device);
929 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
930 iio_device_unregister_trigger_consumer(indio_dev);
931 iio_device_unregister_eventset(indio_dev);
932 iio_device_unregister_sysfs(indio_dev);
934 iio_buffer_put(indio_dev->buffer);
936 ida_simple_remove(&iio_ida, indio_dev->id);
940 struct device_type iio_device_type = {
941 .name = "iio_device",
942 .release = iio_dev_release,
946 * iio_device_alloc() - allocate an iio_dev from a driver
947 * @sizeof_priv: Space to allocate for private structure.
949 struct iio_dev *iio_device_alloc(int sizeof_priv)
954 alloc_size = sizeof(struct iio_dev);
956 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
957 alloc_size += sizeof_priv;
959 /* ensure 32-byte alignment of whole construct ? */
960 alloc_size += IIO_ALIGN - 1;
962 dev = kzalloc(alloc_size, GFP_KERNEL);
965 dev->dev.groups = dev->groups;
966 dev->dev.type = &iio_device_type;
967 dev->dev.bus = &iio_bus_type;
968 device_initialize(&dev->dev);
969 dev_set_drvdata(&dev->dev, (void *)dev);
970 mutex_init(&dev->mlock);
971 mutex_init(&dev->info_exist_lock);
972 INIT_LIST_HEAD(&dev->channel_attr_list);
974 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
976 /* cannot use a dev_err as the name isn't available */
977 pr_err("failed to get device id\n");
981 dev_set_name(&dev->dev, "iio:device%d", dev->id);
982 INIT_LIST_HEAD(&dev->buffer_list);
987 EXPORT_SYMBOL(iio_device_alloc);
990 * iio_device_free() - free an iio_dev from a driver
991 * @dev: the iio_dev associated with the device
993 void iio_device_free(struct iio_dev *dev)
996 put_device(&dev->dev);
998 EXPORT_SYMBOL(iio_device_free);
1000 static void devm_iio_device_release(struct device *dev, void *res)
1002 iio_device_free(*(struct iio_dev **)res);
1005 static int devm_iio_device_match(struct device *dev, void *res, void *data)
1007 struct iio_dev **r = res;
1016 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1017 * @dev: Device to allocate iio_dev for
1018 * @sizeof_priv: Space to allocate for private structure.
1020 * Managed iio_device_alloc. iio_dev allocated with this function is
1021 * automatically freed on driver detach.
1023 * If an iio_dev allocated with this function needs to be freed separately,
1024 * devm_iio_device_free() must be used.
1027 * Pointer to allocated iio_dev on success, NULL on failure.
1029 struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1031 struct iio_dev **ptr, *iio_dev;
1033 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1038 /* use raw alloc_dr for kmalloc caller tracing */
1039 iio_dev = iio_device_alloc(sizeof_priv);
1042 devres_add(dev, ptr);
1049 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1052 * devm_iio_device_free - Resource-managed iio_device_free()
1053 * @dev: Device this iio_dev belongs to
1054 * @iio_dev: the iio_dev associated with the device
1056 * Free iio_dev allocated with devm_iio_device_alloc().
1058 void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1062 rc = devres_release(dev, devm_iio_device_release,
1063 devm_iio_device_match, iio_dev);
1066 EXPORT_SYMBOL_GPL(devm_iio_device_free);
1069 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1071 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1073 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1074 struct iio_dev, chrdev);
1076 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1079 iio_device_get(indio_dev);
1081 filp->private_data = indio_dev;
1087 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1089 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1091 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1092 struct iio_dev, chrdev);
1093 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1094 iio_device_put(indio_dev);
1099 /* Somewhat of a cross file organization violation - ioctls here are actually
1101 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1103 struct iio_dev *indio_dev = filp->private_data;
1104 int __user *ip = (int __user *)arg;
1107 if (!indio_dev->info)
1110 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1111 fd = iio_event_getfd(indio_dev);
1112 if (copy_to_user(ip, &fd, sizeof(fd)))
1119 static const struct file_operations iio_buffer_fileops = {
1120 .read = iio_buffer_read_first_n_outer_addr,
1121 .release = iio_chrdev_release,
1122 .open = iio_chrdev_open,
1123 .poll = iio_buffer_poll_addr,
1124 .owner = THIS_MODULE,
1125 .llseek = noop_llseek,
1126 .unlocked_ioctl = iio_ioctl,
1127 .compat_ioctl = iio_ioctl,
1130 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1133 * iio_device_register() - register a device with the IIO subsystem
1134 * @indio_dev: Device structure filled by the device driver
1136 int iio_device_register(struct iio_dev *indio_dev)
1140 /* If the calling driver did not initialize of_node, do it here */
1141 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1142 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1144 /* configure elements for the chrdev */
1145 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1147 ret = iio_device_register_debugfs(indio_dev);
1149 dev_err(indio_dev->dev.parent,
1150 "Failed to register debugfs interfaces\n");
1153 ret = iio_device_register_sysfs(indio_dev);
1155 dev_err(indio_dev->dev.parent,
1156 "Failed to register sysfs interfaces\n");
1157 goto error_unreg_debugfs;
1159 ret = iio_device_register_eventset(indio_dev);
1161 dev_err(indio_dev->dev.parent,
1162 "Failed to register event set\n");
1163 goto error_free_sysfs;
1165 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1166 iio_device_register_trigger_consumer(indio_dev);
1168 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1169 indio_dev->setup_ops == NULL)
1170 indio_dev->setup_ops = &noop_ring_setup_ops;
1172 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1173 indio_dev->chrdev.owner = indio_dev->info->driver_module;
1174 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
1175 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
1177 goto error_unreg_eventset;
1179 ret = device_add(&indio_dev->dev);
1181 goto error_cdev_del;
1185 cdev_del(&indio_dev->chrdev);
1186 error_unreg_eventset:
1187 iio_device_unregister_eventset(indio_dev);
1189 iio_device_unregister_sysfs(indio_dev);
1190 error_unreg_debugfs:
1191 iio_device_unregister_debugfs(indio_dev);
1194 EXPORT_SYMBOL(iio_device_register);
1197 * iio_device_unregister() - unregister a device from the IIO subsystem
1198 * @indio_dev: Device structure representing the device.
1200 void iio_device_unregister(struct iio_dev *indio_dev)
1202 mutex_lock(&indio_dev->info_exist_lock);
1204 device_del(&indio_dev->dev);
1206 if (indio_dev->chrdev.dev)
1207 cdev_del(&indio_dev->chrdev);
1208 iio_device_unregister_debugfs(indio_dev);
1210 iio_disable_all_buffers(indio_dev);
1212 indio_dev->info = NULL;
1214 iio_device_wakeup_eventset(indio_dev);
1215 iio_buffer_wakeup_poll(indio_dev);
1217 mutex_unlock(&indio_dev->info_exist_lock);
1219 EXPORT_SYMBOL(iio_device_unregister);
1221 static void devm_iio_device_unreg(struct device *dev, void *res)
1223 iio_device_unregister(*(struct iio_dev **)res);
1227 * devm_iio_device_register - Resource-managed iio_device_register()
1228 * @dev: Device to allocate iio_dev for
1229 * @indio_dev: Device structure filled by the device driver
1231 * Managed iio_device_register. The IIO device registered with this
1232 * function is automatically unregistered on driver detach. This function
1233 * calls iio_device_register() internally. Refer to that function for more
1236 * If an iio_dev registered with this function needs to be unregistered
1237 * separately, devm_iio_device_unregister() must be used.
1240 * 0 on success, negative error number on failure.
1242 int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1244 struct iio_dev **ptr;
1247 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1252 ret = iio_device_register(indio_dev);
1254 devres_add(dev, ptr);
1260 EXPORT_SYMBOL_GPL(devm_iio_device_register);
1263 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1264 * @dev: Device this iio_dev belongs to
1265 * @indio_dev: the iio_dev associated with the device
1267 * Unregister iio_dev registered with devm_iio_device_register().
1269 void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1273 rc = devres_release(dev, devm_iio_device_unreg,
1274 devm_iio_device_match, indio_dev);
1277 EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1279 subsys_initcall(iio_init);
1280 module_exit(iio_exit);
1282 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1283 MODULE_DESCRIPTION("Industrial I/O core");
1284 MODULE_LICENSE("GPL");