12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/kdev_t.h>
17 #include <linux/device.h>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
23 #include <linux/slab.h>
35 static dev_t iio_devt;
37 #define IIO_DEV_MAX 256
43 static struct dentry *iio_debugfs_dentry;
45 static const char *
const iio_direction[] = {
50 static const char *
const iio_chan_type_name_spec[] = {
70 static const char *
const iio_modifier_names[] = {
85 static const char *
const iio_chan_info_postfix[] = {
97 =
"filter_low_pass_3db_frequency",
111 if (indio_dev->
channels[i].scan_index == si)
125 static int __init iio_init(
void)
133 "%s could not register bus type\n",
142 goto error_unregister_bus_type;
149 error_unregister_bus_type:
155 static void __exit iio_exit(
void)
163 #if defined(CONFIG_DEBUG_FS)
164 static ssize_t iio_debugfs_read_reg(
struct file *
file,
char __user *userbuf,
165 size_t count, loff_t *ppos)
173 ret = indio_dev->
info->debugfs_reg_access(indio_dev,
174 indio_dev->cached_reg_addr,
177 dev_err(indio_dev->
dev.parent,
"%s: read failed\n", __func__);
179 len =
snprintf(buf,
sizeof(buf),
"0x%X\n", val);
184 static ssize_t iio_debugfs_write_reg(
struct file *file,
185 const char __user *userbuf,
size_t count, loff_t *ppos)
192 count =
min_t(
size_t, count, (
sizeof(buf)-1));
198 ret =
sscanf(buf,
"%i %i", ®, &val);
202 indio_dev->cached_reg_addr =
reg;
205 indio_dev->cached_reg_addr =
reg;
206 ret = indio_dev->
info->debugfs_reg_access(indio_dev, reg,
209 dev_err(indio_dev->
dev.parent,
"%s: write failed\n",
223 .read = iio_debugfs_read_reg,
224 .write = iio_debugfs_write_reg,
227 static void iio_device_unregister_debugfs(
struct iio_dev *indio_dev)
232 static int iio_device_register_debugfs(
struct iio_dev *indio_dev)
236 if (indio_dev->
info->debugfs_reg_access ==
NULL)
239 if (!iio_debugfs_dentry)
242 indio_dev->debugfs_dentry =
245 if (indio_dev->debugfs_dentry ==
NULL) {
247 "Failed to create debugfs directory\n");
252 indio_dev->debugfs_dentry,
253 indio_dev, &iio_debugfs_reg_fops);
255 iio_device_unregister_debugfs(indio_dev);
262 static int iio_device_register_debugfs(
struct iio_dev *indio_dev)
267 static void iio_device_unregister_debugfs(
struct iio_dev *indio_dev)
276 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
280 ext_info = &this_attr->
c->ext_info[this_attr->
address];
282 return ext_info->
read(indio_dev, ext_info->
private, this_attr->
c, buf);
285 static ssize_t iio_write_channel_ext_info(
struct device *dev,
290 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
294 ext_info = &this_attr->
c->ext_info[this_attr->
address];
297 this_attr->
c, buf, len);
329 i = e->
get(indio_dev, chan);
358 ret = e->
set(indio_dev, chan, i);
359 return ret ? ret : len;
367 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
369 unsigned long long tmp;
371 bool scale_db =
false;
372 int ret = indio_dev->
info->read_raw(indio_dev, this_attr->
c,
373 &val, &val2, this_attr->
address);
380 return sprintf(buf,
"%d\n", val);
385 return sprintf(buf,
"-%d.%06u%s\n", val, -val2,
386 scale_db ?
" dB" :
"");
388 return sprintf(buf,
"%d.%06u%s\n", val, val2,
389 scale_db ?
" dB" :
"");
392 return sprintf(buf,
"-%d.%09u\n", val, -val2);
394 return sprintf(buf,
"%d.%09u\n", val, val2);
396 tmp = div_s64((
s64)val * 1000000000
LL, val2);
399 return sprintf(buf,
"%d.%09u\n", val, val2);
410 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
412 int ret,
integer = 0, fract = 0, fract_mult = 100000;
413 bool integer_part =
true, negative =
false;
416 if (!indio_dev->
info->write_raw)
419 if (indio_dev->
info->write_raw_get_fmt)
420 switch (indio_dev->
info->write_raw_get_fmt(indio_dev,
421 this_attr->
c, this_attr->
address)) {
426 fract_mult = 100000000;
438 if (
'0' <= *buf && *buf <=
'9') {
440 integer = integer*10 + *buf -
'0';
442 fract += fract_mult*(*buf -
'0');
447 }
else if (*buf ==
'\n') {
448 if (*(buf + 1) ==
'\0')
452 }
else if (*buf ==
'.') {
453 integer_part =
false;
466 ret = indio_dev->
info->write_raw(indio_dev, this_attr->
c,
467 integer, fract, this_attr->
address);
488 char *name_format, *full_postfix;
495 iio_modifier_names[chan
501 iio_modifier_names[chan
513 if (full_postfix ==
NULL) {
522 iio_direction[chan->
output],
523 iio_chan_type_name_spec[chan->
type],
524 iio_chan_type_name_spec[chan->
type],
529 iio_direction[chan->
output],
530 iio_chan_type_name_spec[chan->
type],
532 iio_chan_type_name_spec[chan->
type],
536 WARN_ON(
"Differential channels must be indexed\n");
538 goto error_free_full_postfix;
544 iio_direction[chan->
output],
545 iio_chan_type_name_spec[chan->
type],
550 iio_direction[chan->
output],
551 iio_chan_type_name_spec[chan->
type],
557 iio_direction[chan->
output],
558 iio_chan_type_name_spec[chan->
type],
561 if (name_format ==
NULL) {
563 goto error_free_full_postfix;
571 goto error_free_name_format;
576 dev_attr->
show = readfunc;
581 dev_attr->
store = writefunc;
588 error_free_name_format:
590 error_free_full_postfix:
618 iio_attr = kzalloc(
sizeof *iio_attr,
GFP_KERNEL);
619 if (iio_attr ==
NULL) {
623 ret = __iio_device_attr_init(&iio_attr->
dev_attr,
625 readfunc, writefunc,
generic);
627 goto error_iio_dev_attr_free;
632 iio_attr->
dev_attr.attr.name) == 0) {
634 dev_err(dev,
"tried to double register : %s\n",
637 goto error_device_attr_deinit;
639 list_add(&iio_attr->
l, attr_list);
643 error_device_attr_deinit:
644 __iio_device_attr_deinit(&iio_attr->
dev_attr);
645 error_iio_dev_attr_free:
651 static int iio_device_add_channel_sysfs(
struct iio_dev *indio_dev,
654 int ret, attrcount = 0;
663 &iio_read_channel_info,
664 &iio_write_channel_info,
669 if (ret == -
EBUSY && (i%2 == 0)) {
680 for (ext_info = chan->
ext_info; ext_info->
name; ext_info++) {
684 &iio_read_channel_ext_info :
NULL,
686 &iio_write_channel_ext_info : NULL,
707 static void iio_device_remove_and_free_read_attr(
struct iio_dev *indio_dev,
718 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
724 static int iio_device_register_sysfs(
struct iio_dev *indio_dev)
726 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
731 if (indio_dev->
info->attrs) {
732 attr = indio_dev->
info->attrs->attrs;
733 while (*attr++ !=
NULL)
736 attrcount = attrcount_orig;
743 ret = iio_device_add_channel_sysfs(indio_dev,
747 goto error_clear_attrs;
759 goto error_clear_attrs;
762 if (indio_dev->
info->attrs)
764 indio_dev->
info->attrs->attrs,
767 attrn = attrcount_orig;
770 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
772 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
774 indio_dev->groups[indio_dev->groupcounter++] =
775 &indio_dev->chan_attr_group;
781 &indio_dev->channel_attr_list,
l) {
783 iio_device_remove_and_free_read_attr(indio_dev, p);
789 static void iio_device_unregister_sysfs(
struct iio_dev *indio_dev)
796 iio_device_remove_and_free_read_attr(indio_dev, p);
803 struct iio_dev *indio_dev = dev_to_iio_dev(device);
804 if (indio_dev->
chrdev.dev)
809 iio_device_unregister_sysfs(indio_dev);
810 iio_device_unregister_debugfs(indio_dev);
817 .name =
"iio_device",
818 .release = iio_dev_release,
826 alloc_size =
sizeof(
struct iio_dev);
829 alloc_size += sizeof_priv;
838 dev->
dev.type = &iio_dev_type;
870 static int iio_chrdev_open(
struct inode *
inode,
struct file *filp)
886 static int iio_chrdev_release(
struct inode *inode,
struct file *filp)
896 static long iio_ioctl(
struct file *filp,
unsigned int cmd,
unsigned long arg)
913 .release = iio_chrdev_release,
914 .open = iio_chrdev_open,
918 .unlocked_ioctl = iio_ioctl,
919 .compat_ioctl = iio_ioctl,
931 ret = iio_device_register_debugfs(indio_dev);
934 "Failed to register debugfs interfaces\n");
937 ret = iio_device_register_sysfs(indio_dev);
940 "Failed to register sysfs interfaces\n");
941 goto error_unreg_debugfs;
946 "Failed to register event set\n");
947 goto error_free_sysfs;
954 indio_dev->
setup_ops = &noop_ring_setup_ops;
958 goto error_unreg_eventset;
960 indio_dev->
chrdev.owner = indio_dev->
info->driver_module;
963 goto error_del_device;
968 error_unreg_eventset:
971 iio_device_unregister_sysfs(indio_dev);
973 iio_device_unregister_debugfs(indio_dev);