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",
74 static const char * const iio_modifier_names[] = {
78 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
79 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
80 [IIO_MOD_LIGHT_BOTH] = "both",
81 [IIO_MOD_LIGHT_IR] = "ir",
82 [IIO_MOD_LIGHT_CLEAR] = "clear",
83 [IIO_MOD_LIGHT_RED] = "red",
84 [IIO_MOD_LIGHT_GREEN] = "green",
85 [IIO_MOD_LIGHT_BLUE] = "blue",
88 /* relies on pairs of these shared then separate */
89 static const char * const iio_chan_info_postfix[] = {
90 [IIO_CHAN_INFO_RAW] = "raw",
91 [IIO_CHAN_INFO_PROCESSED] = "input",
92 [IIO_CHAN_INFO_SCALE] = "scale",
93 [IIO_CHAN_INFO_OFFSET] = "offset",
94 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
95 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
96 [IIO_CHAN_INFO_PEAK] = "peak_raw",
97 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
98 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
99 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
100 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
101 = "filter_low_pass_3db_frequency",
102 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
103 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
104 [IIO_CHAN_INFO_PHASE] = "phase",
105 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
106 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
107 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
110 const struct iio_chan_spec
111 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
115 for (i = 0; i < indio_dev->num_channels; i++)
116 if (indio_dev->channels[i].scan_index == si)
117 return &indio_dev->channels[i];
121 /* This turns up an awful lot */
122 ssize_t iio_read_const_attr(struct device *dev,
123 struct device_attribute *attr,
126 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
128 EXPORT_SYMBOL(iio_read_const_attr);
130 static int __init iio_init(void)
134 /* Register sysfs bus */
135 ret = bus_register(&iio_bus_type);
137 pr_err("could not register bus type\n");
141 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
143 pr_err("failed to allocate char dev region\n");
144 goto error_unregister_bus_type;
147 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
151 error_unregister_bus_type:
152 bus_unregister(&iio_bus_type);
157 static void __exit iio_exit(void)
160 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
161 bus_unregister(&iio_bus_type);
162 debugfs_remove(iio_debugfs_dentry);
165 #if defined(CONFIG_DEBUG_FS)
166 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
167 size_t count, loff_t *ppos)
169 struct iio_dev *indio_dev = file->private_data;
175 ret = indio_dev->info->debugfs_reg_access(indio_dev,
176 indio_dev->cached_reg_addr,
179 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
181 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
183 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
186 static ssize_t iio_debugfs_write_reg(struct file *file,
187 const char __user *userbuf, size_t count, loff_t *ppos)
189 struct iio_dev *indio_dev = file->private_data;
194 count = min_t(size_t, count, (sizeof(buf)-1));
195 if (copy_from_user(buf, userbuf, count))
200 ret = sscanf(buf, "%i %i", ®, &val);
204 indio_dev->cached_reg_addr = reg;
207 indio_dev->cached_reg_addr = reg;
208 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
211 dev_err(indio_dev->dev.parent, "%s: write failed\n",
223 static const struct file_operations iio_debugfs_reg_fops = {
225 .read = iio_debugfs_read_reg,
226 .write = iio_debugfs_write_reg,
229 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
231 debugfs_remove_recursive(indio_dev->debugfs_dentry);
234 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
238 if (indio_dev->info->debugfs_reg_access == NULL)
241 if (!iio_debugfs_dentry)
244 indio_dev->debugfs_dentry =
245 debugfs_create_dir(dev_name(&indio_dev->dev),
247 if (indio_dev->debugfs_dentry == NULL) {
248 dev_warn(indio_dev->dev.parent,
249 "Failed to create debugfs directory\n");
253 d = debugfs_create_file("direct_reg_access", 0644,
254 indio_dev->debugfs_dentry,
255 indio_dev, &iio_debugfs_reg_fops);
257 iio_device_unregister_debugfs(indio_dev);
264 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
269 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
272 #endif /* CONFIG_DEBUG_FS */
274 static ssize_t iio_read_channel_ext_info(struct device *dev,
275 struct device_attribute *attr,
278 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
279 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
280 const struct iio_chan_spec_ext_info *ext_info;
282 ext_info = &this_attr->c->ext_info[this_attr->address];
284 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
287 static ssize_t iio_write_channel_ext_info(struct device *dev,
288 struct device_attribute *attr,
292 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
293 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
294 const struct iio_chan_spec_ext_info *ext_info;
296 ext_info = &this_attr->c->ext_info[this_attr->address];
298 return ext_info->write(indio_dev, ext_info->private,
299 this_attr->c, buf, len);
302 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
303 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
305 const struct iio_enum *e = (const struct iio_enum *)priv;
312 for (i = 0; i < e->num_items; ++i)
313 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
315 /* replace last space with a newline */
320 EXPORT_SYMBOL_GPL(iio_enum_available_read);
322 ssize_t iio_enum_read(struct iio_dev *indio_dev,
323 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
325 const struct iio_enum *e = (const struct iio_enum *)priv;
331 i = e->get(indio_dev, chan);
334 else if (i >= e->num_items)
337 return sprintf(buf, "%s\n", e->items[i]);
339 EXPORT_SYMBOL_GPL(iio_enum_read);
341 ssize_t iio_enum_write(struct iio_dev *indio_dev,
342 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
345 const struct iio_enum *e = (const struct iio_enum *)priv;
352 for (i = 0; i < e->num_items; i++) {
353 if (sysfs_streq(buf, e->items[i]))
357 if (i == e->num_items)
360 ret = e->set(indio_dev, chan, i);
361 return ret ? ret : len;
363 EXPORT_SYMBOL_GPL(iio_enum_write);
366 * iio_format_value() - Formats a IIO value into its string representation
367 * @buf: The buffer to which the formated value gets written
368 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
369 * parameters are formatted.
370 * @val: First part of the value, exact meaning depends on the type parameter.
371 * @val2: Second part of the value, exact meaning depends on the type parameter.
373 ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2)
375 unsigned long long tmp;
376 bool scale_db = false;
380 return sprintf(buf, "%d\n", val);
381 case IIO_VAL_INT_PLUS_MICRO_DB:
383 case IIO_VAL_INT_PLUS_MICRO:
385 return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
386 scale_db ? " dB" : "");
388 return sprintf(buf, "%d.%06u%s\n", val, val2,
389 scale_db ? " dB" : "");
390 case IIO_VAL_INT_PLUS_NANO:
392 return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
394 return sprintf(buf, "%d.%09u\n", val, val2);
395 case IIO_VAL_FRACTIONAL:
396 tmp = div_s64((s64)val * 1000000000LL, val2);
397 val2 = do_div(tmp, 1000000000LL);
399 return sprintf(buf, "%d.%09u\n", val, val2);
400 case IIO_VAL_FRACTIONAL_LOG2:
401 tmp = (s64)val * 1000000000LL >> val2;
402 val2 = do_div(tmp, 1000000000LL);
404 return sprintf(buf, "%d.%09u\n", val, val2);
410 static ssize_t iio_read_channel_info(struct device *dev,
411 struct device_attribute *attr,
414 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
415 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
417 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
418 &val, &val2, this_attr->address);
423 return iio_format_value(buf, ret, val, val2);
427 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
428 * @str: The string to parse
429 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
430 * @integer: The integer part of the number
431 * @fract: The fractional part of the number
433 * Returns 0 on success, or a negative error code if the string could not be
436 int iio_str_to_fixpoint(const char *str, int fract_mult,
437 int *integer, int *fract)
440 bool integer_part = true, negative = false;
445 } else if (str[0] == '+') {
450 if ('0' <= *str && *str <= '9') {
452 i = i * 10 + *str - '0';
454 f += fract_mult * (*str - '0');
457 } else if (*str == '\n') {
458 if (*(str + 1) == '\0')
462 } else if (*str == '.' && integer_part) {
463 integer_part = false;
482 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
484 static ssize_t iio_write_channel_info(struct device *dev,
485 struct device_attribute *attr,
489 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
490 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
491 int ret, fract_mult = 100000;
494 /* Assumes decimal - precision based on number of digits */
495 if (!indio_dev->info->write_raw)
498 if (indio_dev->info->write_raw_get_fmt)
499 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
500 this_attr->c, this_attr->address)) {
501 case IIO_VAL_INT_PLUS_MICRO:
504 case IIO_VAL_INT_PLUS_NANO:
505 fract_mult = 100000000;
511 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
515 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
516 integer, fract, this_attr->address);
524 int __iio_device_attr_init(struct device_attribute *dev_attr,
526 struct iio_chan_spec const *chan,
527 ssize_t (*readfunc)(struct device *dev,
528 struct device_attribute *attr,
530 ssize_t (*writefunc)(struct device *dev,
531 struct device_attribute *attr,
534 enum iio_shared_by shared_by)
537 char *name_format = NULL;
539 sysfs_attr_init(&dev_attr->attr);
541 /* Build up postfix of <extend_name>_<modifier>_postfix */
542 if (chan->modified && (shared_by == IIO_SEPARATE)) {
543 if (chan->extend_name)
544 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
545 iio_modifier_names[chan
550 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
551 iio_modifier_names[chan
555 if (chan->extend_name == NULL)
556 full_postfix = kstrdup(postfix, GFP_KERNEL);
558 full_postfix = kasprintf(GFP_KERNEL,
563 if (full_postfix == NULL)
566 if (chan->differential) { /* Differential can not have modifier */
568 case IIO_SHARED_BY_ALL:
569 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
571 case IIO_SHARED_BY_DIR:
572 name_format = kasprintf(GFP_KERNEL, "%s_%s",
573 iio_direction[chan->output],
576 case IIO_SHARED_BY_TYPE:
578 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
579 iio_direction[chan->output],
580 iio_chan_type_name_spec[chan->type],
581 iio_chan_type_name_spec[chan->type],
585 if (!chan->indexed) {
586 WARN_ON("Differential channels must be indexed\n");
588 goto error_free_full_postfix;
591 = kasprintf(GFP_KERNEL,
593 iio_direction[chan->output],
594 iio_chan_type_name_spec[chan->type],
596 iio_chan_type_name_spec[chan->type],
601 } else { /* Single ended */
603 case IIO_SHARED_BY_ALL:
604 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
606 case IIO_SHARED_BY_DIR:
607 name_format = kasprintf(GFP_KERNEL, "%s_%s",
608 iio_direction[chan->output],
611 case IIO_SHARED_BY_TYPE:
613 = kasprintf(GFP_KERNEL, "%s_%s_%s",
614 iio_direction[chan->output],
615 iio_chan_type_name_spec[chan->type],
622 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
623 iio_direction[chan->output],
624 iio_chan_type_name_spec[chan->type],
629 = kasprintf(GFP_KERNEL, "%s_%s_%s",
630 iio_direction[chan->output],
631 iio_chan_type_name_spec[chan->type],
636 if (name_format == NULL) {
638 goto error_free_full_postfix;
640 dev_attr->attr.name = kasprintf(GFP_KERNEL,
644 if (dev_attr->attr.name == NULL) {
646 goto error_free_name_format;
650 dev_attr->attr.mode |= S_IRUGO;
651 dev_attr->show = readfunc;
655 dev_attr->attr.mode |= S_IWUSR;
656 dev_attr->store = writefunc;
658 error_free_name_format:
660 error_free_full_postfix:
666 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
668 kfree(dev_attr->attr.name);
671 int __iio_add_chan_devattr(const char *postfix,
672 struct iio_chan_spec const *chan,
673 ssize_t (*readfunc)(struct device *dev,
674 struct device_attribute *attr,
676 ssize_t (*writefunc)(struct device *dev,
677 struct device_attribute *attr,
681 enum iio_shared_by shared_by,
683 struct list_head *attr_list)
686 struct iio_dev_attr *iio_attr, *t;
688 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
689 if (iio_attr == NULL) {
693 ret = __iio_device_attr_init(&iio_attr->dev_attr,
695 readfunc, writefunc, shared_by);
697 goto error_iio_dev_attr_free;
699 iio_attr->address = mask;
700 list_for_each_entry(t, attr_list, l)
701 if (strcmp(t->dev_attr.attr.name,
702 iio_attr->dev_attr.attr.name) == 0) {
703 if (shared_by == IIO_SEPARATE)
704 dev_err(dev, "tried to double register : %s\n",
705 t->dev_attr.attr.name);
707 goto error_device_attr_deinit;
709 list_add(&iio_attr->l, attr_list);
713 error_device_attr_deinit:
714 __iio_device_attr_deinit(&iio_attr->dev_attr);
715 error_iio_dev_attr_free:
721 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
722 struct iio_chan_spec const *chan,
723 enum iio_shared_by shared_by,
724 const long *infomask)
726 int i, ret, attrcount = 0;
728 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
729 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
731 &iio_read_channel_info,
732 &iio_write_channel_info,
736 &indio_dev->channel_attr_list);
737 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
747 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
748 struct iio_chan_spec const *chan)
750 int ret, attrcount = 0;
751 const struct iio_chan_spec_ext_info *ext_info;
753 if (chan->channel < 0)
755 ret = iio_device_add_info_mask_type(indio_dev, chan,
757 &chan->info_mask_separate);
762 ret = iio_device_add_info_mask_type(indio_dev, chan,
764 &chan->info_mask_shared_by_type);
769 ret = iio_device_add_info_mask_type(indio_dev, chan,
771 &chan->info_mask_shared_by_dir);
776 ret = iio_device_add_info_mask_type(indio_dev, chan,
778 &chan->info_mask_shared_by_all);
783 if (chan->ext_info) {
785 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
786 ret = __iio_add_chan_devattr(ext_info->name,
789 &iio_read_channel_ext_info : NULL,
791 &iio_write_channel_ext_info : NULL,
795 &indio_dev->channel_attr_list);
797 if (ret == -EBUSY && ext_info->shared)
811 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
812 * @attr_list: List of IIO device attributes
814 * This function frees the memory allocated for each of the IIO device
815 * attributes in the list. Note: if you want to reuse the list after calling
816 * this function you have to reinitialize it using INIT_LIST_HEAD().
818 void iio_free_chan_devattr_list(struct list_head *attr_list)
820 struct iio_dev_attr *p, *n;
822 list_for_each_entry_safe(p, n, attr_list, l) {
823 kfree(p->dev_attr.attr.name);
828 static ssize_t iio_show_dev_name(struct device *dev,
829 struct device_attribute *attr,
832 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
833 return sprintf(buf, "%s\n", indio_dev->name);
836 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
838 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
840 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
841 struct iio_dev_attr *p;
842 struct attribute **attr;
844 /* First count elements in any existing group */
845 if (indio_dev->info->attrs) {
846 attr = indio_dev->info->attrs->attrs;
847 while (*attr++ != NULL)
850 attrcount = attrcount_orig;
852 * New channel registration method - relies on the fact a group does
853 * not need to be initialized if its name is NULL.
855 if (indio_dev->channels)
856 for (i = 0; i < indio_dev->num_channels; i++) {
857 ret = iio_device_add_channel_sysfs(indio_dev,
861 goto error_clear_attrs;
868 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
869 sizeof(indio_dev->chan_attr_group.attrs[0]),
871 if (indio_dev->chan_attr_group.attrs == NULL) {
873 goto error_clear_attrs;
875 /* Copy across original attributes */
876 if (indio_dev->info->attrs)
877 memcpy(indio_dev->chan_attr_group.attrs,
878 indio_dev->info->attrs->attrs,
879 sizeof(indio_dev->chan_attr_group.attrs[0])
881 attrn = attrcount_orig;
882 /* Add all elements from the list. */
883 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
884 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
886 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
888 indio_dev->groups[indio_dev->groupcounter++] =
889 &indio_dev->chan_attr_group;
894 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
899 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
902 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
903 kfree(indio_dev->chan_attr_group.attrs);
906 static void iio_dev_release(struct device *device)
908 struct iio_dev *indio_dev = dev_to_iio_dev(device);
909 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
910 iio_device_unregister_trigger_consumer(indio_dev);
911 iio_device_unregister_eventset(indio_dev);
912 iio_device_unregister_sysfs(indio_dev);
914 iio_buffer_put(indio_dev->buffer);
916 ida_simple_remove(&iio_ida, indio_dev->id);
920 struct device_type iio_device_type = {
921 .name = "iio_device",
922 .release = iio_dev_release,
925 struct iio_dev *iio_device_alloc(int sizeof_priv)
930 alloc_size = sizeof(struct iio_dev);
932 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
933 alloc_size += sizeof_priv;
935 /* ensure 32-byte alignment of whole construct ? */
936 alloc_size += IIO_ALIGN - 1;
938 dev = kzalloc(alloc_size, GFP_KERNEL);
941 dev->dev.groups = dev->groups;
942 dev->dev.type = &iio_device_type;
943 dev->dev.bus = &iio_bus_type;
944 device_initialize(&dev->dev);
945 dev_set_drvdata(&dev->dev, (void *)dev);
946 mutex_init(&dev->mlock);
947 mutex_init(&dev->info_exist_lock);
948 INIT_LIST_HEAD(&dev->channel_attr_list);
950 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
952 /* cannot use a dev_err as the name isn't available */
953 pr_err("failed to get device id\n");
957 dev_set_name(&dev->dev, "iio:device%d", dev->id);
958 INIT_LIST_HEAD(&dev->buffer_list);
963 EXPORT_SYMBOL(iio_device_alloc);
965 void iio_device_free(struct iio_dev *dev)
968 put_device(&dev->dev);
970 EXPORT_SYMBOL(iio_device_free);
972 static void devm_iio_device_release(struct device *dev, void *res)
974 iio_device_free(*(struct iio_dev **)res);
977 static int devm_iio_device_match(struct device *dev, void *res, void *data)
979 struct iio_dev **r = res;
987 struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
989 struct iio_dev **ptr, *iio_dev;
991 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
996 /* use raw alloc_dr for kmalloc caller tracing */
997 iio_dev = iio_device_alloc(sizeof_priv);
1000 devres_add(dev, ptr);
1007 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1009 void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1013 rc = devres_release(dev, devm_iio_device_release,
1014 devm_iio_device_match, iio_dev);
1017 EXPORT_SYMBOL_GPL(devm_iio_device_free);
1020 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1022 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1024 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1025 struct iio_dev, chrdev);
1027 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1030 iio_device_get(indio_dev);
1032 filp->private_data = indio_dev;
1038 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1040 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1042 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1043 struct iio_dev, chrdev);
1044 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1045 iio_device_put(indio_dev);
1050 /* Somewhat of a cross file organization violation - ioctls here are actually
1052 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1054 struct iio_dev *indio_dev = filp->private_data;
1055 int __user *ip = (int __user *)arg;
1058 if (!indio_dev->info)
1061 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1062 fd = iio_event_getfd(indio_dev);
1063 if (copy_to_user(ip, &fd, sizeof(fd)))
1070 static const struct file_operations iio_buffer_fileops = {
1071 .read = iio_buffer_read_first_n_outer_addr,
1072 .release = iio_chrdev_release,
1073 .open = iio_chrdev_open,
1074 .poll = iio_buffer_poll_addr,
1075 .owner = THIS_MODULE,
1076 .llseek = noop_llseek,
1077 .unlocked_ioctl = iio_ioctl,
1078 .compat_ioctl = iio_ioctl,
1081 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1083 int iio_device_register(struct iio_dev *indio_dev)
1087 /* If the calling driver did not initialize of_node, do it here */
1088 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1089 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1091 /* configure elements for the chrdev */
1092 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1094 ret = iio_device_register_debugfs(indio_dev);
1096 dev_err(indio_dev->dev.parent,
1097 "Failed to register debugfs interfaces\n");
1100 ret = iio_device_register_sysfs(indio_dev);
1102 dev_err(indio_dev->dev.parent,
1103 "Failed to register sysfs interfaces\n");
1104 goto error_unreg_debugfs;
1106 ret = iio_device_register_eventset(indio_dev);
1108 dev_err(indio_dev->dev.parent,
1109 "Failed to register event set\n");
1110 goto error_free_sysfs;
1112 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1113 iio_device_register_trigger_consumer(indio_dev);
1115 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1116 indio_dev->setup_ops == NULL)
1117 indio_dev->setup_ops = &noop_ring_setup_ops;
1119 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1120 indio_dev->chrdev.owner = indio_dev->info->driver_module;
1121 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
1122 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
1124 goto error_unreg_eventset;
1126 ret = device_add(&indio_dev->dev);
1128 goto error_cdev_del;
1132 cdev_del(&indio_dev->chrdev);
1133 error_unreg_eventset:
1134 iio_device_unregister_eventset(indio_dev);
1136 iio_device_unregister_sysfs(indio_dev);
1137 error_unreg_debugfs:
1138 iio_device_unregister_debugfs(indio_dev);
1142 EXPORT_SYMBOL(iio_device_register);
1144 void iio_device_unregister(struct iio_dev *indio_dev)
1146 mutex_lock(&indio_dev->info_exist_lock);
1148 device_del(&indio_dev->dev);
1150 if (indio_dev->chrdev.dev)
1151 cdev_del(&indio_dev->chrdev);
1152 iio_device_unregister_debugfs(indio_dev);
1154 iio_disable_all_buffers(indio_dev);
1156 indio_dev->info = NULL;
1158 iio_device_wakeup_eventset(indio_dev);
1159 iio_buffer_wakeup_poll(indio_dev);
1161 mutex_unlock(&indio_dev->info_exist_lock);
1163 EXPORT_SYMBOL(iio_device_unregister);
1164 subsys_initcall(iio_init);
1165 module_exit(iio_exit);
1167 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1168 MODULE_DESCRIPTION("Industrial I/O core");
1169 MODULE_LICENSE("GPL");