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>
28 #include "iio_core_trigger.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_data_type_name[] = {
47 [IIO_PROCESSED] = "input",
50 static const char * const iio_direction[] = {
55 static const char * const iio_chan_type_name_spec[] = {
56 [IIO_VOLTAGE] = "voltage",
57 [IIO_CURRENT] = "current",
58 [IIO_POWER] = "power",
59 [IIO_ACCEL] = "accel",
60 [IIO_ANGL_VEL] = "anglvel",
62 [IIO_LIGHT] = "illuminance",
63 [IIO_INTENSITY] = "intensity",
64 [IIO_PROXIMITY] = "proximity",
66 [IIO_INCLI] = "incli",
69 [IIO_TIMESTAMP] = "timestamp",
70 [IIO_CAPACITANCE] = "capacitance",
73 static const char * const iio_modifier_names[] = {
77 [IIO_MOD_LIGHT_BOTH] = "both",
78 [IIO_MOD_LIGHT_IR] = "ir",
81 /* relies on pairs of these shared then separate */
82 static const char * const iio_chan_info_postfix[] = {
83 [IIO_CHAN_INFO_SCALE] = "scale",
84 [IIO_CHAN_INFO_OFFSET] = "offset",
85 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
86 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
87 [IIO_CHAN_INFO_PEAK] = "peak_raw",
88 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
89 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
90 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
91 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
92 = "filter_low_pass_3db_frequency",
95 const struct iio_chan_spec
96 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
100 for (i = 0; i < indio_dev->num_channels; i++)
101 if (indio_dev->channels[i].scan_index == si)
102 return &indio_dev->channels[i];
106 /* This turns up an awful lot */
107 ssize_t iio_read_const_attr(struct device *dev,
108 struct device_attribute *attr,
111 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
113 EXPORT_SYMBOL(iio_read_const_attr);
115 static int __init iio_init(void)
119 /* Register sysfs bus */
120 ret = bus_register(&iio_bus_type);
123 "%s could not register bus type\n",
128 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
130 printk(KERN_ERR "%s: failed to allocate char dev region\n",
132 goto error_unregister_bus_type;
135 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
139 error_unregister_bus_type:
140 bus_unregister(&iio_bus_type);
145 static void __exit iio_exit(void)
148 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
149 bus_unregister(&iio_bus_type);
150 debugfs_remove(iio_debugfs_dentry);
153 #if defined(CONFIG_DEBUG_FS)
154 static int iio_debugfs_open(struct inode *inode, struct file *file)
156 if (inode->i_private)
157 file->private_data = inode->i_private;
162 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
163 size_t count, loff_t *ppos)
165 struct iio_dev *indio_dev = file->private_data;
171 ret = indio_dev->info->debugfs_reg_access(indio_dev,
172 indio_dev->cached_reg_addr,
175 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
177 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
179 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
182 static ssize_t iio_debugfs_write_reg(struct file *file,
183 const char __user *userbuf, size_t count, loff_t *ppos)
185 struct iio_dev *indio_dev = file->private_data;
190 count = min_t(size_t, count, (sizeof(buf)-1));
191 if (copy_from_user(buf, userbuf, count))
196 ret = sscanf(buf, "%i %i", ®, &val);
200 indio_dev->cached_reg_addr = reg;
203 indio_dev->cached_reg_addr = reg;
204 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
207 dev_err(indio_dev->dev.parent, "%s: write failed\n",
219 static const struct file_operations iio_debugfs_reg_fops = {
220 .open = iio_debugfs_open,
221 .read = iio_debugfs_read_reg,
222 .write = iio_debugfs_write_reg,
225 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
227 debugfs_remove_recursive(indio_dev->debugfs_dentry);
230 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
234 if (indio_dev->info->debugfs_reg_access == NULL)
237 if (IS_ERR(iio_debugfs_dentry))
240 indio_dev->debugfs_dentry =
241 debugfs_create_dir(dev_name(&indio_dev->dev),
243 if (IS_ERR(indio_dev->debugfs_dentry))
244 return PTR_ERR(indio_dev->debugfs_dentry);
246 if (indio_dev->debugfs_dentry == NULL) {
247 dev_warn(indio_dev->dev.parent,
248 "Failed to create debugfs directory\n");
252 d = debugfs_create_file("direct_reg_access", 0644,
253 indio_dev->debugfs_dentry,
254 indio_dev, &iio_debugfs_reg_fops);
256 iio_device_unregister_debugfs(indio_dev);
263 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
268 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
271 #endif /* CONFIG_DEBUG_FS */
273 static ssize_t iio_read_channel_ext_info(struct device *dev,
274 struct device_attribute *attr,
277 struct iio_dev *indio_dev = dev_get_drvdata(dev);
278 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
279 const struct iio_chan_spec_ext_info *ext_info;
281 ext_info = &this_attr->c->ext_info[this_attr->address];
283 return ext_info->read(indio_dev, this_attr->c, buf);
286 static ssize_t iio_write_channel_ext_info(struct device *dev,
287 struct device_attribute *attr,
291 struct iio_dev *indio_dev = dev_get_drvdata(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->write(indio_dev, this_attr->c, buf, len);
300 static ssize_t iio_read_channel_info(struct device *dev,
301 struct device_attribute *attr,
304 struct iio_dev *indio_dev = dev_get_drvdata(dev);
305 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
307 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
308 &val, &val2, this_attr->address);
313 if (ret == IIO_VAL_INT)
314 return sprintf(buf, "%d\n", val);
315 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
317 return sprintf(buf, "-%d.%06u\n", val, -val2);
319 return sprintf(buf, "%d.%06u\n", val, val2);
320 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
322 return sprintf(buf, "-%d.%09u\n", val, -val2);
324 return sprintf(buf, "%d.%09u\n", val, val2);
329 static ssize_t iio_write_channel_info(struct device *dev,
330 struct device_attribute *attr,
334 struct iio_dev *indio_dev = dev_get_drvdata(dev);
335 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
336 int ret, integer = 0, fract = 0, fract_mult = 100000;
337 bool integer_part = true, negative = false;
339 /* Assumes decimal - precision based on number of digits */
340 if (!indio_dev->info->write_raw)
343 if (indio_dev->info->write_raw_get_fmt)
344 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
345 this_attr->c, this_attr->address)) {
346 case IIO_VAL_INT_PLUS_MICRO:
349 case IIO_VAL_INT_PLUS_NANO:
350 fract_mult = 100000000;
362 if ('0' <= *buf && *buf <= '9') {
364 integer = integer*10 + *buf - '0';
366 fract += fract_mult*(*buf - '0');
371 } else if (*buf == '\n') {
372 if (*(buf + 1) == '\0')
376 } else if (*buf == '.') {
377 integer_part = false;
390 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
391 integer, fract, this_attr->address);
399 int __iio_device_attr_init(struct device_attribute *dev_attr,
401 struct iio_chan_spec const *chan,
402 ssize_t (*readfunc)(struct device *dev,
403 struct device_attribute *attr,
405 ssize_t (*writefunc)(struct device *dev,
406 struct device_attribute *attr,
412 char *name_format, *full_postfix;
413 sysfs_attr_init(&dev_attr->attr);
415 /* Build up postfix of <extend_name>_<modifier>_postfix */
416 if (chan->modified && !generic) {
417 if (chan->extend_name)
418 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
419 iio_modifier_names[chan
424 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
425 iio_modifier_names[chan
429 if (chan->extend_name == NULL)
430 full_postfix = kstrdup(postfix, GFP_KERNEL);
432 full_postfix = kasprintf(GFP_KERNEL,
437 if (full_postfix == NULL) {
442 if (chan->differential) { /* Differential can not have modifier */
445 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
446 iio_direction[chan->output],
447 iio_chan_type_name_spec[chan->type],
448 iio_chan_type_name_spec[chan->type],
450 else if (chan->indexed)
452 = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
453 iio_direction[chan->output],
454 iio_chan_type_name_spec[chan->type],
456 iio_chan_type_name_spec[chan->type],
460 WARN_ON("Differential channels must be indexed\n");
462 goto error_free_full_postfix;
464 } else { /* Single ended */
467 = kasprintf(GFP_KERNEL, "%s_%s_%s",
468 iio_direction[chan->output],
469 iio_chan_type_name_spec[chan->type],
471 else if (chan->indexed)
473 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
474 iio_direction[chan->output],
475 iio_chan_type_name_spec[chan->type],
480 = kasprintf(GFP_KERNEL, "%s_%s_%s",
481 iio_direction[chan->output],
482 iio_chan_type_name_spec[chan->type],
485 if (name_format == NULL) {
487 goto error_free_full_postfix;
489 dev_attr->attr.name = kasprintf(GFP_KERNEL,
493 if (dev_attr->attr.name == NULL) {
495 goto error_free_name_format;
499 dev_attr->attr.mode |= S_IRUGO;
500 dev_attr->show = readfunc;
504 dev_attr->attr.mode |= S_IWUSR;
505 dev_attr->store = writefunc;
512 error_free_name_format:
514 error_free_full_postfix:
520 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
522 kfree(dev_attr->attr.name);
525 int __iio_add_chan_devattr(const char *postfix,
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,
537 struct list_head *attr_list)
540 struct iio_dev_attr *iio_attr, *t;
542 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
543 if (iio_attr == NULL) {
547 ret = __iio_device_attr_init(&iio_attr->dev_attr,
549 readfunc, writefunc, generic);
551 goto error_iio_dev_attr_free;
553 iio_attr->address = mask;
554 list_for_each_entry(t, attr_list, l)
555 if (strcmp(t->dev_attr.attr.name,
556 iio_attr->dev_attr.attr.name) == 0) {
558 dev_err(dev, "tried to double register : %s\n",
559 t->dev_attr.attr.name);
561 goto error_device_attr_deinit;
563 list_add(&iio_attr->l, attr_list);
567 error_device_attr_deinit:
568 __iio_device_attr_deinit(&iio_attr->dev_attr);
569 error_iio_dev_attr_free:
575 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
576 struct iio_chan_spec const *chan)
578 int ret, i, attrcount = 0;
579 const struct iio_chan_spec_ext_info *ext_info;
581 if (chan->channel < 0)
584 ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
586 &iio_read_channel_info,
588 &iio_write_channel_info : NULL),
592 &indio_dev->channel_attr_list);
597 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
598 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
600 &iio_read_channel_info,
601 &iio_write_channel_info,
605 &indio_dev->channel_attr_list);
606 if (ret == -EBUSY && (i%2 == 0)) {
615 if (chan->ext_info) {
617 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
618 ret = __iio_add_chan_devattr(ext_info->name,
621 &iio_read_channel_ext_info : NULL,
623 &iio_write_channel_ext_info : NULL,
627 &indio_dev->channel_attr_list);
629 if (ret == -EBUSY && ext_info->shared)
644 static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
645 struct iio_dev_attr *p)
647 kfree(p->dev_attr.attr.name);
651 static ssize_t iio_show_dev_name(struct device *dev,
652 struct device_attribute *attr,
655 struct iio_dev *indio_dev = dev_get_drvdata(dev);
656 return sprintf(buf, "%s\n", indio_dev->name);
659 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
661 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
663 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
664 struct iio_dev_attr *p, *n;
665 struct attribute **attr;
667 /* First count elements in any existing group */
668 if (indio_dev->info->attrs) {
669 attr = indio_dev->info->attrs->attrs;
670 while (*attr++ != NULL)
673 attrcount = attrcount_orig;
675 * New channel registration method - relies on the fact a group does
676 * not need to be initialized if it is name is NULL.
678 INIT_LIST_HEAD(&indio_dev->channel_attr_list);
679 if (indio_dev->channels)
680 for (i = 0; i < indio_dev->num_channels; i++) {
681 ret = iio_device_add_channel_sysfs(indio_dev,
685 goto error_clear_attrs;
692 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
693 sizeof(indio_dev->chan_attr_group.attrs[0]),
695 if (indio_dev->chan_attr_group.attrs == NULL) {
697 goto error_clear_attrs;
699 /* Copy across original attributes */
700 if (indio_dev->info->attrs)
701 memcpy(indio_dev->chan_attr_group.attrs,
702 indio_dev->info->attrs->attrs,
703 sizeof(indio_dev->chan_attr_group.attrs[0])
705 attrn = attrcount_orig;
706 /* Add all elements from the list. */
707 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
708 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
710 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
712 indio_dev->groups[indio_dev->groupcounter++] =
713 &indio_dev->chan_attr_group;
718 list_for_each_entry_safe(p, n,
719 &indio_dev->channel_attr_list, l) {
721 iio_device_remove_and_free_read_attr(indio_dev, p);
727 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
730 struct iio_dev_attr *p, *n;
732 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
734 iio_device_remove_and_free_read_attr(indio_dev, p);
736 kfree(indio_dev->chan_attr_group.attrs);
739 static void iio_dev_release(struct device *device)
741 struct iio_dev *indio_dev = container_of(device, struct iio_dev, dev);
742 cdev_del(&indio_dev->chrdev);
743 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
744 iio_device_unregister_trigger_consumer(indio_dev);
745 iio_device_unregister_eventset(indio_dev);
746 iio_device_unregister_sysfs(indio_dev);
747 iio_device_unregister_debugfs(indio_dev);
750 static struct device_type iio_dev_type = {
751 .name = "iio_device",
752 .release = iio_dev_release,
755 struct iio_dev *iio_allocate_device(int sizeof_priv)
760 alloc_size = sizeof(struct iio_dev);
762 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
763 alloc_size += sizeof_priv;
765 /* ensure 32-byte alignment of whole construct ? */
766 alloc_size += IIO_ALIGN - 1;
768 dev = kzalloc(alloc_size, GFP_KERNEL);
771 dev->dev.groups = dev->groups;
772 dev->dev.type = &iio_dev_type;
773 dev->dev.bus = &iio_bus_type;
774 device_initialize(&dev->dev);
775 dev_set_drvdata(&dev->dev, (void *)dev);
776 mutex_init(&dev->mlock);
777 mutex_init(&dev->info_exist_lock);
779 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
781 /* cannot use a dev_err as the name isn't available */
782 printk(KERN_ERR "Failed to get id\n");
786 dev_set_name(&dev->dev, "iio:device%d", dev->id);
791 EXPORT_SYMBOL(iio_allocate_device);
793 void iio_free_device(struct iio_dev *dev)
796 ida_simple_remove(&iio_ida, dev->id);
800 EXPORT_SYMBOL(iio_free_device);
803 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
805 static int iio_chrdev_open(struct inode *inode, struct file *filp)
807 struct iio_dev *indio_dev = container_of(inode->i_cdev,
808 struct iio_dev, chrdev);
810 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
813 filp->private_data = indio_dev;
819 * iio_chrdev_release() - chrdev file close buffer access and ioctls
821 static int iio_chrdev_release(struct inode *inode, struct file *filp)
823 struct iio_dev *indio_dev = container_of(inode->i_cdev,
824 struct iio_dev, chrdev);
825 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
829 /* Somewhat of a cross file organization violation - ioctls here are actually
831 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
833 struct iio_dev *indio_dev = filp->private_data;
834 int __user *ip = (int __user *)arg;
837 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
838 fd = iio_event_getfd(indio_dev);
839 if (copy_to_user(ip, &fd, sizeof(fd)))
846 static const struct file_operations iio_buffer_fileops = {
847 .read = iio_buffer_read_first_n_outer_addr,
848 .release = iio_chrdev_release,
849 .open = iio_chrdev_open,
850 .poll = iio_buffer_poll_addr,
851 .owner = THIS_MODULE,
852 .llseek = noop_llseek,
853 .unlocked_ioctl = iio_ioctl,
854 .compat_ioctl = iio_ioctl,
857 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
859 int iio_device_register(struct iio_dev *indio_dev)
863 /* configure elements for the chrdev */
864 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
866 ret = iio_device_register_debugfs(indio_dev);
868 dev_err(indio_dev->dev.parent,
869 "Failed to register debugfs interfaces\n");
872 ret = iio_device_register_sysfs(indio_dev);
874 dev_err(indio_dev->dev.parent,
875 "Failed to register sysfs interfaces\n");
876 goto error_unreg_debugfs;
878 ret = iio_device_register_eventset(indio_dev);
880 dev_err(indio_dev->dev.parent,
881 "Failed to register event set\n");
882 goto error_free_sysfs;
884 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
885 iio_device_register_trigger_consumer(indio_dev);
887 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
888 indio_dev->setup_ops == NULL)
889 indio_dev->setup_ops = &noop_ring_setup_ops;
891 ret = device_add(&indio_dev->dev);
893 goto error_unreg_eventset;
894 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
895 indio_dev->chrdev.owner = indio_dev->info->driver_module;
896 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
898 goto error_del_device;
902 device_del(&indio_dev->dev);
903 error_unreg_eventset:
904 iio_device_unregister_eventset(indio_dev);
906 iio_device_unregister_sysfs(indio_dev);
908 iio_device_unregister_debugfs(indio_dev);
912 EXPORT_SYMBOL(iio_device_register);
914 void iio_device_unregister(struct iio_dev *indio_dev)
916 mutex_lock(&indio_dev->info_exist_lock);
917 indio_dev->info = NULL;
918 mutex_unlock(&indio_dev->info_exist_lock);
919 device_unregister(&indio_dev->dev);
921 EXPORT_SYMBOL(iio_device_unregister);
922 subsys_initcall(iio_init);
923 module_exit(iio_exit);
925 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
926 MODULE_DESCRIPTION("Industrial I/O core");
927 MODULE_LICENSE("GPL");