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 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/idr.h>
15 #include <linux/kdev_t.h>
16 #include <linux/err.h>
17 #include <linux/device.h>
19 #include <linux/poll.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/iio/iio.h>
28 #include "iio_core_trigger.h"
29 #include <linux/iio/sysfs.h>
30 #include <linux/iio/events.h>
32 /* IDA to assign each registered device a unique id*/
33 static DEFINE_IDA(iio_ida);
35 static dev_t iio_devt;
37 #define IIO_DEV_MAX 256
38 struct bus_type iio_bus_type = {
41 EXPORT_SYMBOL(iio_bus_type);
43 static struct dentry *iio_debugfs_dentry;
45 static const char * const iio_direction[] = {
50 static const char * const iio_chan_type_name_spec[] = {
51 [IIO_VOLTAGE] = "voltage",
52 [IIO_CURRENT] = "current",
53 [IIO_POWER] = "power",
54 [IIO_ACCEL] = "accel",
55 [IIO_ANGL_VEL] = "anglvel",
57 [IIO_LIGHT] = "illuminance",
58 [IIO_INTENSITY] = "intensity",
59 [IIO_PROXIMITY] = "proximity",
61 [IIO_INCLI] = "incli",
64 [IIO_TIMESTAMP] = "timestamp",
65 [IIO_CAPACITANCE] = "capacitance",
68 static const char * const iio_modifier_names[] = {
72 [IIO_MOD_LIGHT_BOTH] = "both",
73 [IIO_MOD_LIGHT_IR] = "ir",
76 /* relies on pairs of these shared then separate */
77 static const char * const iio_chan_info_postfix[] = {
78 [IIO_CHAN_INFO_RAW] = "raw",
79 [IIO_CHAN_INFO_PROCESSED] = "input",
80 [IIO_CHAN_INFO_SCALE] = "scale",
81 [IIO_CHAN_INFO_OFFSET] = "offset",
82 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
83 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
84 [IIO_CHAN_INFO_PEAK] = "peak_raw",
85 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
86 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
87 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
88 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
89 = "filter_low_pass_3db_frequency",
90 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
93 const struct iio_chan_spec
94 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
98 for (i = 0; i < indio_dev->num_channels; i++)
99 if (indio_dev->channels[i].scan_index == si)
100 return &indio_dev->channels[i];
104 /* This turns up an awful lot */
105 ssize_t iio_read_const_attr(struct device *dev,
106 struct device_attribute *attr,
109 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
111 EXPORT_SYMBOL(iio_read_const_attr);
113 static int __init iio_init(void)
117 /* Register sysfs bus */
118 ret = bus_register(&iio_bus_type);
121 "%s could not register bus type\n",
126 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
128 printk(KERN_ERR "%s: failed to allocate char dev region\n",
130 goto error_unregister_bus_type;
133 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
137 error_unregister_bus_type:
138 bus_unregister(&iio_bus_type);
143 static void __exit iio_exit(void)
146 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
147 bus_unregister(&iio_bus_type);
148 debugfs_remove(iio_debugfs_dentry);
151 #if defined(CONFIG_DEBUG_FS)
152 static int iio_debugfs_open(struct inode *inode, struct file *file)
154 if (inode->i_private)
155 file->private_data = inode->i_private;
160 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
161 size_t count, loff_t *ppos)
163 struct iio_dev *indio_dev = file->private_data;
169 ret = indio_dev->info->debugfs_reg_access(indio_dev,
170 indio_dev->cached_reg_addr,
173 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
175 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
177 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
180 static ssize_t iio_debugfs_write_reg(struct file *file,
181 const char __user *userbuf, size_t count, loff_t *ppos)
183 struct iio_dev *indio_dev = file->private_data;
188 count = min_t(size_t, count, (sizeof(buf)-1));
189 if (copy_from_user(buf, userbuf, count))
194 ret = sscanf(buf, "%i %i", ®, &val);
198 indio_dev->cached_reg_addr = reg;
201 indio_dev->cached_reg_addr = reg;
202 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
205 dev_err(indio_dev->dev.parent, "%s: write failed\n",
217 static const struct file_operations iio_debugfs_reg_fops = {
218 .open = iio_debugfs_open,
219 .read = iio_debugfs_read_reg,
220 .write = iio_debugfs_write_reg,
223 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
225 debugfs_remove_recursive(indio_dev->debugfs_dentry);
228 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
232 if (indio_dev->info->debugfs_reg_access == NULL)
235 if (IS_ERR(iio_debugfs_dentry))
238 indio_dev->debugfs_dentry =
239 debugfs_create_dir(dev_name(&indio_dev->dev),
241 if (IS_ERR(indio_dev->debugfs_dentry))
242 return PTR_ERR(indio_dev->debugfs_dentry);
244 if (indio_dev->debugfs_dentry == NULL) {
245 dev_warn(indio_dev->dev.parent,
246 "Failed to create debugfs directory\n");
250 d = debugfs_create_file("direct_reg_access", 0644,
251 indio_dev->debugfs_dentry,
252 indio_dev, &iio_debugfs_reg_fops);
254 iio_device_unregister_debugfs(indio_dev);
261 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
266 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
269 #endif /* CONFIG_DEBUG_FS */
271 static ssize_t iio_read_channel_ext_info(struct device *dev,
272 struct device_attribute *attr,
275 struct iio_dev *indio_dev = dev_get_drvdata(dev);
276 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
277 const struct iio_chan_spec_ext_info *ext_info;
279 ext_info = &this_attr->c->ext_info[this_attr->address];
281 return ext_info->read(indio_dev, this_attr->c, buf);
284 static ssize_t iio_write_channel_ext_info(struct device *dev,
285 struct device_attribute *attr,
289 struct iio_dev *indio_dev = dev_get_drvdata(dev);
290 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
291 const struct iio_chan_spec_ext_info *ext_info;
293 ext_info = &this_attr->c->ext_info[this_attr->address];
295 return ext_info->write(indio_dev, this_attr->c, buf, len);
298 static ssize_t iio_read_channel_info(struct device *dev,
299 struct device_attribute *attr,
302 struct iio_dev *indio_dev = dev_get_drvdata(dev);
303 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
305 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
306 &val, &val2, this_attr->address);
311 if (ret == IIO_VAL_INT)
312 return sprintf(buf, "%d\n", val);
313 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
315 return sprintf(buf, "-%d.%06u\n", val, -val2);
317 return sprintf(buf, "%d.%06u\n", val, val2);
318 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
320 return sprintf(buf, "-%d.%09u\n", val, -val2);
322 return sprintf(buf, "%d.%09u\n", val, val2);
327 static ssize_t iio_write_channel_info(struct device *dev,
328 struct device_attribute *attr,
332 struct iio_dev *indio_dev = dev_get_drvdata(dev);
333 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
334 int ret, integer = 0, fract = 0, fract_mult = 100000;
335 bool integer_part = true, negative = false;
337 /* Assumes decimal - precision based on number of digits */
338 if (!indio_dev->info->write_raw)
341 if (indio_dev->info->write_raw_get_fmt)
342 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
343 this_attr->c, this_attr->address)) {
344 case IIO_VAL_INT_PLUS_MICRO:
347 case IIO_VAL_INT_PLUS_NANO:
348 fract_mult = 100000000;
360 if ('0' <= *buf && *buf <= '9') {
362 integer = integer*10 + *buf - '0';
364 fract += fract_mult*(*buf - '0');
369 } else if (*buf == '\n') {
370 if (*(buf + 1) == '\0')
374 } else if (*buf == '.') {
375 integer_part = false;
388 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
389 integer, fract, this_attr->address);
397 int __iio_device_attr_init(struct device_attribute *dev_attr,
399 struct iio_chan_spec const *chan,
400 ssize_t (*readfunc)(struct device *dev,
401 struct device_attribute *attr,
403 ssize_t (*writefunc)(struct device *dev,
404 struct device_attribute *attr,
410 char *name_format, *full_postfix;
411 sysfs_attr_init(&dev_attr->attr);
413 /* Build up postfix of <extend_name>_<modifier>_postfix */
414 if (chan->modified && !generic) {
415 if (chan->extend_name)
416 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
417 iio_modifier_names[chan
422 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
423 iio_modifier_names[chan
427 if (chan->extend_name == NULL)
428 full_postfix = kstrdup(postfix, GFP_KERNEL);
430 full_postfix = kasprintf(GFP_KERNEL,
435 if (full_postfix == NULL) {
440 if (chan->differential) { /* Differential can not have modifier */
443 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
444 iio_direction[chan->output],
445 iio_chan_type_name_spec[chan->type],
446 iio_chan_type_name_spec[chan->type],
448 else if (chan->indexed)
450 = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
451 iio_direction[chan->output],
452 iio_chan_type_name_spec[chan->type],
454 iio_chan_type_name_spec[chan->type],
458 WARN_ON("Differential channels must be indexed\n");
460 goto error_free_full_postfix;
462 } else { /* Single ended */
465 = kasprintf(GFP_KERNEL, "%s_%s_%s",
466 iio_direction[chan->output],
467 iio_chan_type_name_spec[chan->type],
469 else if (chan->indexed)
471 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
472 iio_direction[chan->output],
473 iio_chan_type_name_spec[chan->type],
478 = kasprintf(GFP_KERNEL, "%s_%s_%s",
479 iio_direction[chan->output],
480 iio_chan_type_name_spec[chan->type],
483 if (name_format == NULL) {
485 goto error_free_full_postfix;
487 dev_attr->attr.name = kasprintf(GFP_KERNEL,
491 if (dev_attr->attr.name == NULL) {
493 goto error_free_name_format;
497 dev_attr->attr.mode |= S_IRUGO;
498 dev_attr->show = readfunc;
502 dev_attr->attr.mode |= S_IWUSR;
503 dev_attr->store = writefunc;
510 error_free_name_format:
512 error_free_full_postfix:
518 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
520 kfree(dev_attr->attr.name);
523 int __iio_add_chan_devattr(const char *postfix,
524 struct iio_chan_spec const *chan,
525 ssize_t (*readfunc)(struct device *dev,
526 struct device_attribute *attr,
528 ssize_t (*writefunc)(struct device *dev,
529 struct device_attribute *attr,
535 struct list_head *attr_list)
538 struct iio_dev_attr *iio_attr, *t;
540 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
541 if (iio_attr == NULL) {
545 ret = __iio_device_attr_init(&iio_attr->dev_attr,
547 readfunc, writefunc, generic);
549 goto error_iio_dev_attr_free;
551 iio_attr->address = mask;
552 list_for_each_entry(t, attr_list, l)
553 if (strcmp(t->dev_attr.attr.name,
554 iio_attr->dev_attr.attr.name) == 0) {
556 dev_err(dev, "tried to double register : %s\n",
557 t->dev_attr.attr.name);
559 goto error_device_attr_deinit;
561 list_add(&iio_attr->l, attr_list);
565 error_device_attr_deinit:
566 __iio_device_attr_deinit(&iio_attr->dev_attr);
567 error_iio_dev_attr_free:
573 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
574 struct iio_chan_spec const *chan)
576 int ret, attrcount = 0;
578 const struct iio_chan_spec_ext_info *ext_info;
580 if (chan->channel < 0)
582 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
583 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
585 &iio_read_channel_info,
586 &iio_write_channel_info,
590 &indio_dev->channel_attr_list);
591 if (ret == -EBUSY && (i%2 == 0)) {
600 if (chan->ext_info) {
602 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
603 ret = __iio_add_chan_devattr(ext_info->name,
606 &iio_read_channel_ext_info : NULL,
608 &iio_write_channel_ext_info : NULL,
612 &indio_dev->channel_attr_list);
614 if (ret == -EBUSY && ext_info->shared)
629 static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
630 struct iio_dev_attr *p)
632 kfree(p->dev_attr.attr.name);
636 static ssize_t iio_show_dev_name(struct device *dev,
637 struct device_attribute *attr,
640 struct iio_dev *indio_dev = dev_get_drvdata(dev);
641 return sprintf(buf, "%s\n", indio_dev->name);
644 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
646 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
648 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
649 struct iio_dev_attr *p, *n;
650 struct attribute **attr;
652 /* First count elements in any existing group */
653 if (indio_dev->info->attrs) {
654 attr = indio_dev->info->attrs->attrs;
655 while (*attr++ != NULL)
658 attrcount = attrcount_orig;
660 * New channel registration method - relies on the fact a group does
661 * not need to be initialized if it is name is NULL.
663 INIT_LIST_HEAD(&indio_dev->channel_attr_list);
664 if (indio_dev->channels)
665 for (i = 0; i < indio_dev->num_channels; i++) {
666 ret = iio_device_add_channel_sysfs(indio_dev,
670 goto error_clear_attrs;
677 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
678 sizeof(indio_dev->chan_attr_group.attrs[0]),
680 if (indio_dev->chan_attr_group.attrs == NULL) {
682 goto error_clear_attrs;
684 /* Copy across original attributes */
685 if (indio_dev->info->attrs)
686 memcpy(indio_dev->chan_attr_group.attrs,
687 indio_dev->info->attrs->attrs,
688 sizeof(indio_dev->chan_attr_group.attrs[0])
690 attrn = attrcount_orig;
691 /* Add all elements from the list. */
692 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
693 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
695 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
697 indio_dev->groups[indio_dev->groupcounter++] =
698 &indio_dev->chan_attr_group;
703 list_for_each_entry_safe(p, n,
704 &indio_dev->channel_attr_list, l) {
706 iio_device_remove_and_free_read_attr(indio_dev, p);
712 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
715 struct iio_dev_attr *p, *n;
717 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
719 iio_device_remove_and_free_read_attr(indio_dev, p);
721 kfree(indio_dev->chan_attr_group.attrs);
724 static void iio_dev_release(struct device *device)
726 struct iio_dev *indio_dev = container_of(device, struct iio_dev, dev);
727 cdev_del(&indio_dev->chrdev);
728 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
729 iio_device_unregister_trigger_consumer(indio_dev);
730 iio_device_unregister_eventset(indio_dev);
731 iio_device_unregister_sysfs(indio_dev);
732 iio_device_unregister_debugfs(indio_dev);
735 static struct device_type iio_dev_type = {
736 .name = "iio_device",
737 .release = iio_dev_release,
740 struct iio_dev *iio_allocate_device(int sizeof_priv)
745 alloc_size = sizeof(struct iio_dev);
747 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
748 alloc_size += sizeof_priv;
750 /* ensure 32-byte alignment of whole construct ? */
751 alloc_size += IIO_ALIGN - 1;
753 dev = kzalloc(alloc_size, GFP_KERNEL);
756 dev->dev.groups = dev->groups;
757 dev->dev.type = &iio_dev_type;
758 dev->dev.bus = &iio_bus_type;
759 device_initialize(&dev->dev);
760 dev_set_drvdata(&dev->dev, (void *)dev);
761 mutex_init(&dev->mlock);
762 mutex_init(&dev->info_exist_lock);
764 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
766 /* cannot use a dev_err as the name isn't available */
767 printk(KERN_ERR "Failed to get id\n");
771 dev_set_name(&dev->dev, "iio:device%d", dev->id);
776 EXPORT_SYMBOL(iio_allocate_device);
778 void iio_free_device(struct iio_dev *dev)
781 ida_simple_remove(&iio_ida, dev->id);
785 EXPORT_SYMBOL(iio_free_device);
788 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
790 static int iio_chrdev_open(struct inode *inode, struct file *filp)
792 struct iio_dev *indio_dev = container_of(inode->i_cdev,
793 struct iio_dev, chrdev);
795 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
798 filp->private_data = indio_dev;
804 * iio_chrdev_release() - chrdev file close buffer access and ioctls
806 static int iio_chrdev_release(struct inode *inode, struct file *filp)
808 struct iio_dev *indio_dev = container_of(inode->i_cdev,
809 struct iio_dev, chrdev);
810 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
814 /* Somewhat of a cross file organization violation - ioctls here are actually
816 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
818 struct iio_dev *indio_dev = filp->private_data;
819 int __user *ip = (int __user *)arg;
822 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
823 fd = iio_event_getfd(indio_dev);
824 if (copy_to_user(ip, &fd, sizeof(fd)))
831 static const struct file_operations iio_buffer_fileops = {
832 .read = iio_buffer_read_first_n_outer_addr,
833 .release = iio_chrdev_release,
834 .open = iio_chrdev_open,
835 .poll = iio_buffer_poll_addr,
836 .owner = THIS_MODULE,
837 .llseek = noop_llseek,
838 .unlocked_ioctl = iio_ioctl,
839 .compat_ioctl = iio_ioctl,
842 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
844 int iio_device_register(struct iio_dev *indio_dev)
848 /* configure elements for the chrdev */
849 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
851 ret = iio_device_register_debugfs(indio_dev);
853 dev_err(indio_dev->dev.parent,
854 "Failed to register debugfs interfaces\n");
857 ret = iio_device_register_sysfs(indio_dev);
859 dev_err(indio_dev->dev.parent,
860 "Failed to register sysfs interfaces\n");
861 goto error_unreg_debugfs;
863 ret = iio_device_register_eventset(indio_dev);
865 dev_err(indio_dev->dev.parent,
866 "Failed to register event set\n");
867 goto error_free_sysfs;
869 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
870 iio_device_register_trigger_consumer(indio_dev);
872 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
873 indio_dev->setup_ops == NULL)
874 indio_dev->setup_ops = &noop_ring_setup_ops;
876 ret = device_add(&indio_dev->dev);
878 goto error_unreg_eventset;
879 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
880 indio_dev->chrdev.owner = indio_dev->info->driver_module;
881 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
883 goto error_del_device;
887 device_del(&indio_dev->dev);
888 error_unreg_eventset:
889 iio_device_unregister_eventset(indio_dev);
891 iio_device_unregister_sysfs(indio_dev);
893 iio_device_unregister_debugfs(indio_dev);
897 EXPORT_SYMBOL(iio_device_register);
899 void iio_device_unregister(struct iio_dev *indio_dev)
901 mutex_lock(&indio_dev->info_exist_lock);
902 indio_dev->info = NULL;
903 mutex_unlock(&indio_dev->info_exist_lock);
904 device_unregister(&indio_dev->dev);
906 EXPORT_SYMBOL(iio_device_unregister);
907 subsys_initcall(iio_init);
908 module_exit(iio_exit);
910 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
911 MODULE_DESCRIPTION("Industrial I/O core");
912 MODULE_LICENSE("GPL");