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/mutex.h>
29 #include <linux/iio/iio.h>
31 #include "iio_core_trigger.h"
32 #include <linux/iio/sysfs.h>
33 #include <linux/iio/events.h>
34 #include <linux/iio/buffer.h>
36 /* IDA to assign each registered device a unique id */
37 static DEFINE_IDA(iio_ida
);
39 static dev_t iio_devt
;
41 #define IIO_DEV_MAX 256
42 struct bus_type iio_bus_type
= {
45 EXPORT_SYMBOL(iio_bus_type
);
47 static struct dentry
*iio_debugfs_dentry
;
49 static const char * const iio_direction
[] = {
54 static const char * const iio_chan_type_name_spec
[] = {
55 [IIO_VOLTAGE
] = "voltage",
56 [IIO_CURRENT
] = "current",
57 [IIO_POWER
] = "power",
58 [IIO_ACCEL
] = "accel",
59 [IIO_ANGL_VEL
] = "anglvel",
61 [IIO_LIGHT
] = "illuminance",
62 [IIO_INTENSITY
] = "intensity",
63 [IIO_PROXIMITY
] = "proximity",
65 [IIO_INCLI
] = "incli",
68 [IIO_TIMESTAMP
] = "timestamp",
69 [IIO_CAPACITANCE
] = "capacitance",
70 [IIO_ALTVOLTAGE
] = "altvoltage",
72 [IIO_PRESSURE
] = "pressure",
73 [IIO_HUMIDITYRELATIVE
] = "humidityrelative",
74 [IIO_ACTIVITY
] = "activity",
75 [IIO_STEPS
] = "steps",
76 [IIO_ENERGY
] = "energy",
77 [IIO_DISTANCE
] = "distance",
78 [IIO_VELOCITY
] = "velocity",
79 [IIO_CONCENTRATION
] = "concentration",
80 [IIO_RESISTANCE
] = "resistance",
84 static const char * const iio_modifier_names
[] = {
88 [IIO_MOD_X_AND_Y
] = "x&y",
89 [IIO_MOD_X_AND_Z
] = "x&z",
90 [IIO_MOD_Y_AND_Z
] = "y&z",
91 [IIO_MOD_X_AND_Y_AND_Z
] = "x&y&z",
92 [IIO_MOD_X_OR_Y
] = "x|y",
93 [IIO_MOD_X_OR_Z
] = "x|z",
94 [IIO_MOD_Y_OR_Z
] = "y|z",
95 [IIO_MOD_X_OR_Y_OR_Z
] = "x|y|z",
96 [IIO_MOD_ROOT_SUM_SQUARED_X_Y
] = "sqrt(x^2+y^2)",
97 [IIO_MOD_SUM_SQUARED_X_Y_Z
] = "x^2+y^2+z^2",
98 [IIO_MOD_LIGHT_BOTH
] = "both",
99 [IIO_MOD_LIGHT_IR
] = "ir",
100 [IIO_MOD_LIGHT_CLEAR
] = "clear",
101 [IIO_MOD_LIGHT_RED
] = "red",
102 [IIO_MOD_LIGHT_GREEN
] = "green",
103 [IIO_MOD_LIGHT_BLUE
] = "blue",
104 [IIO_MOD_QUATERNION
] = "quaternion",
105 [IIO_MOD_TEMP_AMBIENT
] = "ambient",
106 [IIO_MOD_TEMP_OBJECT
] = "object",
107 [IIO_MOD_NORTH_MAGN
] = "from_north_magnetic",
108 [IIO_MOD_NORTH_TRUE
] = "from_north_true",
109 [IIO_MOD_NORTH_MAGN_TILT_COMP
] = "from_north_magnetic_tilt_comp",
110 [IIO_MOD_NORTH_TRUE_TILT_COMP
] = "from_north_true_tilt_comp",
111 [IIO_MOD_RUNNING
] = "running",
112 [IIO_MOD_JOGGING
] = "jogging",
113 [IIO_MOD_WALKING
] = "walking",
114 [IIO_MOD_STILL
] = "still",
115 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
] = "sqrt(x^2+y^2+z^2)",
118 [IIO_MOD_CO2
] = "co2",
119 [IIO_MOD_VOC
] = "voc",
122 /* relies on pairs of these shared then separate */
123 static const char * const iio_chan_info_postfix
[] = {
124 [IIO_CHAN_INFO_RAW
] = "raw",
125 [IIO_CHAN_INFO_PROCESSED
] = "input",
126 [IIO_CHAN_INFO_SCALE
] = "scale",
127 [IIO_CHAN_INFO_OFFSET
] = "offset",
128 [IIO_CHAN_INFO_CALIBSCALE
] = "calibscale",
129 [IIO_CHAN_INFO_CALIBBIAS
] = "calibbias",
130 [IIO_CHAN_INFO_PEAK
] = "peak_raw",
131 [IIO_CHAN_INFO_PEAK_SCALE
] = "peak_scale",
132 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW
] = "quadrature_correction_raw",
133 [IIO_CHAN_INFO_AVERAGE_RAW
] = "mean_raw",
134 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
]
135 = "filter_low_pass_3db_frequency",
136 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
]
137 = "filter_high_pass_3db_frequency",
138 [IIO_CHAN_INFO_SAMP_FREQ
] = "sampling_frequency",
139 [IIO_CHAN_INFO_FREQUENCY
] = "frequency",
140 [IIO_CHAN_INFO_PHASE
] = "phase",
141 [IIO_CHAN_INFO_HARDWAREGAIN
] = "hardwaregain",
142 [IIO_CHAN_INFO_HYSTERESIS
] = "hysteresis",
143 [IIO_CHAN_INFO_INT_TIME
] = "integration_time",
144 [IIO_CHAN_INFO_ENABLE
] = "en",
145 [IIO_CHAN_INFO_CALIBHEIGHT
] = "calibheight",
146 [IIO_CHAN_INFO_CALIBWEIGHT
] = "calibweight",
147 [IIO_CHAN_INFO_DEBOUNCE_COUNT
] = "debounce_count",
148 [IIO_CHAN_INFO_DEBOUNCE_TIME
] = "debounce_time",
149 [IIO_CHAN_INFO_CALIBEMISSIVITY
] = "calibemissivity",
150 [IIO_CHAN_INFO_OVERSAMPLING_RATIO
] = "oversampling_ratio",
154 * iio_find_channel_from_si() - get channel from its scan index
156 * @si: scan index to match
158 const struct iio_chan_spec
159 *iio_find_channel_from_si(struct iio_dev
*indio_dev
, int si
)
163 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
164 if (indio_dev
->channels
[i
].scan_index
== si
)
165 return &indio_dev
->channels
[i
];
169 /* This turns up an awful lot */
170 ssize_t
iio_read_const_attr(struct device
*dev
,
171 struct device_attribute
*attr
,
174 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
176 EXPORT_SYMBOL(iio_read_const_attr
);
178 static int __init
iio_init(void)
182 /* Register sysfs bus */
183 ret
= bus_register(&iio_bus_type
);
185 pr_err("could not register bus type\n");
189 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
191 pr_err("failed to allocate char dev region\n");
192 goto error_unregister_bus_type
;
195 iio_debugfs_dentry
= debugfs_create_dir("iio", NULL
);
199 error_unregister_bus_type
:
200 bus_unregister(&iio_bus_type
);
205 static void __exit
iio_exit(void)
208 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
209 bus_unregister(&iio_bus_type
);
210 debugfs_remove(iio_debugfs_dentry
);
213 #if defined(CONFIG_DEBUG_FS)
214 static ssize_t
iio_debugfs_read_reg(struct file
*file
, char __user
*userbuf
,
215 size_t count
, loff_t
*ppos
)
217 struct iio_dev
*indio_dev
= file
->private_data
;
223 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
,
224 indio_dev
->cached_reg_addr
,
227 dev_err(indio_dev
->dev
.parent
, "%s: read failed\n", __func__
);
229 len
= snprintf(buf
, sizeof(buf
), "0x%X\n", val
);
231 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
234 static ssize_t
iio_debugfs_write_reg(struct file
*file
,
235 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
237 struct iio_dev
*indio_dev
= file
->private_data
;
242 count
= min_t(size_t, count
, (sizeof(buf
)-1));
243 if (copy_from_user(buf
, userbuf
, count
))
248 ret
= sscanf(buf
, "%i %i", ®
, &val
);
252 indio_dev
->cached_reg_addr
= reg
;
255 indio_dev
->cached_reg_addr
= reg
;
256 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
, reg
,
259 dev_err(indio_dev
->dev
.parent
, "%s: write failed\n",
271 static const struct file_operations iio_debugfs_reg_fops
= {
273 .read
= iio_debugfs_read_reg
,
274 .write
= iio_debugfs_write_reg
,
277 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
279 debugfs_remove_recursive(indio_dev
->debugfs_dentry
);
282 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
286 if (indio_dev
->info
->debugfs_reg_access
== NULL
)
289 if (!iio_debugfs_dentry
)
292 indio_dev
->debugfs_dentry
=
293 debugfs_create_dir(dev_name(&indio_dev
->dev
),
295 if (indio_dev
->debugfs_dentry
== NULL
) {
296 dev_warn(indio_dev
->dev
.parent
,
297 "Failed to create debugfs directory\n");
301 d
= debugfs_create_file("direct_reg_access", 0644,
302 indio_dev
->debugfs_dentry
,
303 indio_dev
, &iio_debugfs_reg_fops
);
305 iio_device_unregister_debugfs(indio_dev
);
312 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
317 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
320 #endif /* CONFIG_DEBUG_FS */
322 static ssize_t
iio_read_channel_ext_info(struct device
*dev
,
323 struct device_attribute
*attr
,
326 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
327 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
328 const struct iio_chan_spec_ext_info
*ext_info
;
330 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
332 return ext_info
->read(indio_dev
, ext_info
->private, this_attr
->c
, buf
);
335 static ssize_t
iio_write_channel_ext_info(struct device
*dev
,
336 struct device_attribute
*attr
,
340 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
341 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
342 const struct iio_chan_spec_ext_info
*ext_info
;
344 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
346 return ext_info
->write(indio_dev
, ext_info
->private,
347 this_attr
->c
, buf
, len
);
350 ssize_t
iio_enum_available_read(struct iio_dev
*indio_dev
,
351 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
353 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
360 for (i
= 0; i
< e
->num_items
; ++i
)
361 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%s ", e
->items
[i
]);
363 /* replace last space with a newline */
368 EXPORT_SYMBOL_GPL(iio_enum_available_read
);
370 ssize_t
iio_enum_read(struct iio_dev
*indio_dev
,
371 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
373 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
379 i
= e
->get(indio_dev
, chan
);
382 else if (i
>= e
->num_items
)
385 return snprintf(buf
, PAGE_SIZE
, "%s\n", e
->items
[i
]);
387 EXPORT_SYMBOL_GPL(iio_enum_read
);
389 ssize_t
iio_enum_write(struct iio_dev
*indio_dev
,
390 uintptr_t priv
, const struct iio_chan_spec
*chan
, const char *buf
,
393 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
400 for (i
= 0; i
< e
->num_items
; i
++) {
401 if (sysfs_streq(buf
, e
->items
[i
]))
405 if (i
== e
->num_items
)
408 ret
= e
->set(indio_dev
, chan
, i
);
409 return ret
? ret
: len
;
411 EXPORT_SYMBOL_GPL(iio_enum_write
);
414 * iio_format_value() - Formats a IIO value into its string representation
415 * @buf: The buffer to which the formatted value gets written
416 * @type: One of the IIO_VAL_... constants. This decides how the val
417 * and val2 parameters are formatted.
418 * @size: Number of IIO value entries contained in vals
419 * @vals: Pointer to the values, exact meaning depends on the
422 * Return: 0 by default, a negative number on failure or the
423 * total number of characters written for a type that belongs
424 * to the IIO_VAL_... constant.
426 ssize_t
iio_format_value(char *buf
, unsigned int type
, int size
, int *vals
)
428 unsigned long long tmp
;
429 bool scale_db
= false;
433 return sprintf(buf
, "%d\n", vals
[0]);
434 case IIO_VAL_INT_PLUS_MICRO_DB
:
436 case IIO_VAL_INT_PLUS_MICRO
:
438 return sprintf(buf
, "-%d.%06u%s\n", abs(vals
[0]),
439 -vals
[1], scale_db
? " dB" : "");
441 return sprintf(buf
, "%d.%06u%s\n", vals
[0], vals
[1],
442 scale_db
? " dB" : "");
443 case IIO_VAL_INT_PLUS_NANO
:
445 return sprintf(buf
, "-%d.%09u\n", abs(vals
[0]),
448 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
449 case IIO_VAL_FRACTIONAL
:
450 tmp
= div_s64((s64
)vals
[0] * 1000000000LL, vals
[1]);
451 vals
[1] = do_div(tmp
, 1000000000LL);
453 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
454 case IIO_VAL_FRACTIONAL_LOG2
:
455 tmp
= (s64
)vals
[0] * 1000000000LL >> vals
[1];
456 vals
[1] = do_div(tmp
, 1000000000LL);
458 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
459 case IIO_VAL_INT_MULTIPLE
:
464 for (i
= 0; i
< size
; ++i
)
465 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "%d ",
467 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "\n");
474 EXPORT_SYMBOL_GPL(iio_format_value
);
476 static ssize_t
iio_read_channel_info(struct device
*dev
,
477 struct device_attribute
*attr
,
480 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
481 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
482 int vals
[INDIO_MAX_RAW_ELEMENTS
];
486 if (indio_dev
->info
->read_raw_multi
)
487 ret
= indio_dev
->info
->read_raw_multi(indio_dev
, this_attr
->c
,
488 INDIO_MAX_RAW_ELEMENTS
,
492 ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
493 &vals
[0], &vals
[1], this_attr
->address
);
498 return iio_format_value(buf
, ret
, val_len
, vals
);
502 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
503 * @str: The string to parse
504 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
505 * @integer: The integer part of the number
506 * @fract: The fractional part of the number
508 * Returns 0 on success, or a negative error code if the string could not be
511 int iio_str_to_fixpoint(const char *str
, int fract_mult
,
512 int *integer
, int *fract
)
515 bool integer_part
= true, negative
= false;
517 if (fract_mult
== 0) {
520 return kstrtoint(str
, 0, integer
);
526 } else if (str
[0] == '+') {
531 if ('0' <= *str
&& *str
<= '9') {
533 i
= i
* 10 + *str
- '0';
535 f
+= fract_mult
* (*str
- '0');
538 } else if (*str
== '\n') {
539 if (*(str
+ 1) == '\0')
543 } else if (*str
== '.' && integer_part
) {
544 integer_part
= false;
563 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint
);
565 static ssize_t
iio_write_channel_info(struct device
*dev
,
566 struct device_attribute
*attr
,
570 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
571 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
572 int ret
, fract_mult
= 100000;
575 /* Assumes decimal - precision based on number of digits */
576 if (!indio_dev
->info
->write_raw
)
579 if (indio_dev
->info
->write_raw_get_fmt
)
580 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
581 this_attr
->c
, this_attr
->address
)) {
585 case IIO_VAL_INT_PLUS_MICRO
:
588 case IIO_VAL_INT_PLUS_NANO
:
589 fract_mult
= 100000000;
595 ret
= iio_str_to_fixpoint(buf
, fract_mult
, &integer
, &fract
);
599 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
600 integer
, fract
, this_attr
->address
);
608 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
610 struct iio_chan_spec
const *chan
,
611 ssize_t (*readfunc
)(struct device
*dev
,
612 struct device_attribute
*attr
,
614 ssize_t (*writefunc
)(struct device
*dev
,
615 struct device_attribute
*attr
,
618 enum iio_shared_by shared_by
)
623 sysfs_attr_init(&dev_attr
->attr
);
625 /* Build up postfix of <extend_name>_<modifier>_postfix */
626 if (chan
->modified
&& (shared_by
== IIO_SEPARATE
)) {
627 if (chan
->extend_name
)
628 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
629 iio_modifier_names
[chan
634 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s",
635 iio_modifier_names
[chan
639 if (chan
->extend_name
== NULL
|| shared_by
!= IIO_SEPARATE
)
640 full_postfix
= kstrdup(postfix
, GFP_KERNEL
);
642 full_postfix
= kasprintf(GFP_KERNEL
,
647 if (full_postfix
== NULL
)
650 if (chan
->differential
) { /* Differential can not have modifier */
652 case IIO_SHARED_BY_ALL
:
653 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
655 case IIO_SHARED_BY_DIR
:
656 name
= kasprintf(GFP_KERNEL
, "%s_%s",
657 iio_direction
[chan
->output
],
660 case IIO_SHARED_BY_TYPE
:
661 name
= kasprintf(GFP_KERNEL
, "%s_%s-%s_%s",
662 iio_direction
[chan
->output
],
663 iio_chan_type_name_spec
[chan
->type
],
664 iio_chan_type_name_spec
[chan
->type
],
668 if (!chan
->indexed
) {
669 WARN(1, "Differential channels must be indexed\n");
671 goto error_free_full_postfix
;
673 name
= kasprintf(GFP_KERNEL
,
675 iio_direction
[chan
->output
],
676 iio_chan_type_name_spec
[chan
->type
],
678 iio_chan_type_name_spec
[chan
->type
],
683 } else { /* Single ended */
685 case IIO_SHARED_BY_ALL
:
686 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
688 case IIO_SHARED_BY_DIR
:
689 name
= kasprintf(GFP_KERNEL
, "%s_%s",
690 iio_direction
[chan
->output
],
693 case IIO_SHARED_BY_TYPE
:
694 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
695 iio_direction
[chan
->output
],
696 iio_chan_type_name_spec
[chan
->type
],
702 name
= kasprintf(GFP_KERNEL
, "%s_%s%d_%s",
703 iio_direction
[chan
->output
],
704 iio_chan_type_name_spec
[chan
->type
],
708 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
709 iio_direction
[chan
->output
],
710 iio_chan_type_name_spec
[chan
->type
],
717 goto error_free_full_postfix
;
719 dev_attr
->attr
.name
= name
;
722 dev_attr
->attr
.mode
|= S_IRUGO
;
723 dev_attr
->show
= readfunc
;
727 dev_attr
->attr
.mode
|= S_IWUSR
;
728 dev_attr
->store
= writefunc
;
731 error_free_full_postfix
:
737 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
739 kfree(dev_attr
->attr
.name
);
742 int __iio_add_chan_devattr(const char *postfix
,
743 struct iio_chan_spec
const *chan
,
744 ssize_t (*readfunc
)(struct device
*dev
,
745 struct device_attribute
*attr
,
747 ssize_t (*writefunc
)(struct device
*dev
,
748 struct device_attribute
*attr
,
752 enum iio_shared_by shared_by
,
754 struct list_head
*attr_list
)
757 struct iio_dev_attr
*iio_attr
, *t
;
759 iio_attr
= kzalloc(sizeof(*iio_attr
), GFP_KERNEL
);
760 if (iio_attr
== NULL
)
762 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
764 readfunc
, writefunc
, shared_by
);
766 goto error_iio_dev_attr_free
;
768 iio_attr
->address
= mask
;
769 list_for_each_entry(t
, attr_list
, l
)
770 if (strcmp(t
->dev_attr
.attr
.name
,
771 iio_attr
->dev_attr
.attr
.name
) == 0) {
772 if (shared_by
== IIO_SEPARATE
)
773 dev_err(dev
, "tried to double register : %s\n",
774 t
->dev_attr
.attr
.name
);
776 goto error_device_attr_deinit
;
778 list_add(&iio_attr
->l
, attr_list
);
782 error_device_attr_deinit
:
783 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
784 error_iio_dev_attr_free
:
789 static int iio_device_add_info_mask_type(struct iio_dev
*indio_dev
,
790 struct iio_chan_spec
const *chan
,
791 enum iio_shared_by shared_by
,
792 const long *infomask
)
794 int i
, ret
, attrcount
= 0;
796 for_each_set_bit(i
, infomask
, sizeof(infomask
)*8) {
797 if (i
>= ARRAY_SIZE(iio_chan_info_postfix
))
799 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
],
801 &iio_read_channel_info
,
802 &iio_write_channel_info
,
806 &indio_dev
->channel_attr_list
);
807 if ((ret
== -EBUSY
) && (shared_by
!= IIO_SEPARATE
))
817 static int iio_device_add_channel_sysfs(struct iio_dev
*indio_dev
,
818 struct iio_chan_spec
const *chan
)
820 int ret
, attrcount
= 0;
821 const struct iio_chan_spec_ext_info
*ext_info
;
823 if (chan
->channel
< 0)
825 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
827 &chan
->info_mask_separate
);
832 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
834 &chan
->info_mask_shared_by_type
);
839 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
841 &chan
->info_mask_shared_by_dir
);
846 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
848 &chan
->info_mask_shared_by_all
);
853 if (chan
->ext_info
) {
855 for (ext_info
= chan
->ext_info
; ext_info
->name
; ext_info
++) {
856 ret
= __iio_add_chan_devattr(ext_info
->name
,
859 &iio_read_channel_ext_info
: NULL
,
861 &iio_write_channel_ext_info
: NULL
,
865 &indio_dev
->channel_attr_list
);
867 if (ret
== -EBUSY
&& ext_info
->shared
)
881 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
882 * @attr_list: List of IIO device attributes
884 * This function frees the memory allocated for each of the IIO device
885 * attributes in the list.
887 void iio_free_chan_devattr_list(struct list_head
*attr_list
)
889 struct iio_dev_attr
*p
, *n
;
891 list_for_each_entry_safe(p
, n
, attr_list
, l
) {
892 kfree(p
->dev_attr
.attr
.name
);
898 static ssize_t
iio_show_dev_name(struct device
*dev
,
899 struct device_attribute
*attr
,
902 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
903 return snprintf(buf
, PAGE_SIZE
, "%s\n", indio_dev
->name
);
906 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
908 static int iio_device_register_sysfs(struct iio_dev
*indio_dev
)
910 int i
, ret
= 0, attrcount
, attrn
, attrcount_orig
= 0;
911 struct iio_dev_attr
*p
;
912 struct attribute
**attr
;
914 /* First count elements in any existing group */
915 if (indio_dev
->info
->attrs
) {
916 attr
= indio_dev
->info
->attrs
->attrs
;
917 while (*attr
++ != NULL
)
920 attrcount
= attrcount_orig
;
922 * New channel registration method - relies on the fact a group does
923 * not need to be initialized if its name is NULL.
925 if (indio_dev
->channels
)
926 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
927 ret
= iio_device_add_channel_sysfs(indio_dev
,
931 goto error_clear_attrs
;
938 indio_dev
->chan_attr_group
.attrs
= kcalloc(attrcount
+ 1,
939 sizeof(indio_dev
->chan_attr_group
.attrs
[0]),
941 if (indio_dev
->chan_attr_group
.attrs
== NULL
) {
943 goto error_clear_attrs
;
945 /* Copy across original attributes */
946 if (indio_dev
->info
->attrs
)
947 memcpy(indio_dev
->chan_attr_group
.attrs
,
948 indio_dev
->info
->attrs
->attrs
,
949 sizeof(indio_dev
->chan_attr_group
.attrs
[0])
951 attrn
= attrcount_orig
;
952 /* Add all elements from the list. */
953 list_for_each_entry(p
, &indio_dev
->channel_attr_list
, l
)
954 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
956 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_name
.attr
;
958 indio_dev
->groups
[indio_dev
->groupcounter
++] =
959 &indio_dev
->chan_attr_group
;
964 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
969 static void iio_device_unregister_sysfs(struct iio_dev
*indio_dev
)
972 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
973 kfree(indio_dev
->chan_attr_group
.attrs
);
974 indio_dev
->chan_attr_group
.attrs
= NULL
;
977 static void iio_dev_release(struct device
*device
)
979 struct iio_dev
*indio_dev
= dev_to_iio_dev(device
);
980 if (indio_dev
->modes
& (INDIO_BUFFER_TRIGGERED
| INDIO_EVENT_TRIGGERED
))
981 iio_device_unregister_trigger_consumer(indio_dev
);
982 iio_device_unregister_eventset(indio_dev
);
983 iio_device_unregister_sysfs(indio_dev
);
985 iio_buffer_put(indio_dev
->buffer
);
987 ida_simple_remove(&iio_ida
, indio_dev
->id
);
991 struct device_type iio_device_type
= {
992 .name
= "iio_device",
993 .release
= iio_dev_release
,
997 * iio_device_alloc() - allocate an iio_dev from a driver
998 * @sizeof_priv: Space to allocate for private structure.
1000 struct iio_dev
*iio_device_alloc(int sizeof_priv
)
1002 struct iio_dev
*dev
;
1005 alloc_size
= sizeof(struct iio_dev
);
1007 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
1008 alloc_size
+= sizeof_priv
;
1010 /* ensure 32-byte alignment of whole construct ? */
1011 alloc_size
+= IIO_ALIGN
- 1;
1013 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
1016 dev
->dev
.groups
= dev
->groups
;
1017 dev
->dev
.type
= &iio_device_type
;
1018 dev
->dev
.bus
= &iio_bus_type
;
1019 device_initialize(&dev
->dev
);
1020 dev_set_drvdata(&dev
->dev
, (void *)dev
);
1021 mutex_init(&dev
->mlock
);
1022 mutex_init(&dev
->info_exist_lock
);
1023 INIT_LIST_HEAD(&dev
->channel_attr_list
);
1025 dev
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
1027 /* cannot use a dev_err as the name isn't available */
1028 pr_err("failed to get device id\n");
1032 dev_set_name(&dev
->dev
, "iio:device%d", dev
->id
);
1033 INIT_LIST_HEAD(&dev
->buffer_list
);
1038 EXPORT_SYMBOL(iio_device_alloc
);
1041 * iio_device_free() - free an iio_dev from a driver
1042 * @dev: the iio_dev associated with the device
1044 void iio_device_free(struct iio_dev
*dev
)
1047 put_device(&dev
->dev
);
1049 EXPORT_SYMBOL(iio_device_free
);
1051 static void devm_iio_device_release(struct device
*dev
, void *res
)
1053 iio_device_free(*(struct iio_dev
**)res
);
1056 static int devm_iio_device_match(struct device
*dev
, void *res
, void *data
)
1058 struct iio_dev
**r
= res
;
1067 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1068 * @dev: Device to allocate iio_dev for
1069 * @sizeof_priv: Space to allocate for private structure.
1071 * Managed iio_device_alloc. iio_dev allocated with this function is
1072 * automatically freed on driver detach.
1074 * If an iio_dev allocated with this function needs to be freed separately,
1075 * devm_iio_device_free() must be used.
1078 * Pointer to allocated iio_dev on success, NULL on failure.
1080 struct iio_dev
*devm_iio_device_alloc(struct device
*dev
, int sizeof_priv
)
1082 struct iio_dev
**ptr
, *iio_dev
;
1084 ptr
= devres_alloc(devm_iio_device_release
, sizeof(*ptr
),
1089 iio_dev
= iio_device_alloc(sizeof_priv
);
1092 devres_add(dev
, ptr
);
1099 EXPORT_SYMBOL_GPL(devm_iio_device_alloc
);
1102 * devm_iio_device_free - Resource-managed iio_device_free()
1103 * @dev: Device this iio_dev belongs to
1104 * @iio_dev: the iio_dev associated with the device
1106 * Free iio_dev allocated with devm_iio_device_alloc().
1108 void devm_iio_device_free(struct device
*dev
, struct iio_dev
*iio_dev
)
1112 rc
= devres_release(dev
, devm_iio_device_release
,
1113 devm_iio_device_match
, iio_dev
);
1116 EXPORT_SYMBOL_GPL(devm_iio_device_free
);
1119 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1120 * @inode: Inode structure for identifying the device in the file system
1121 * @filp: File structure for iio device used to keep and later access
1124 * Return: 0 on success or -EBUSY if the device is already opened
1126 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1128 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1129 struct iio_dev
, chrdev
);
1131 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
))
1134 iio_device_get(indio_dev
);
1136 filp
->private_data
= indio_dev
;
1142 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1143 * @inode: Inode structure pointer for the char device
1144 * @filp: File structure pointer for the char device
1146 * Return: 0 for successful release
1148 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1150 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1151 struct iio_dev
, chrdev
);
1152 clear_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
);
1153 iio_device_put(indio_dev
);
1158 /* Somewhat of a cross file organization violation - ioctls here are actually
1160 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1162 struct iio_dev
*indio_dev
= filp
->private_data
;
1163 int __user
*ip
= (int __user
*)arg
;
1166 if (!indio_dev
->info
)
1169 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1170 fd
= iio_event_getfd(indio_dev
);
1173 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1180 static const struct file_operations iio_buffer_fileops
= {
1181 .read
= iio_buffer_read_first_n_outer_addr
,
1182 .release
= iio_chrdev_release
,
1183 .open
= iio_chrdev_open
,
1184 .poll
= iio_buffer_poll_addr
,
1185 .owner
= THIS_MODULE
,
1186 .llseek
= noop_llseek
,
1187 .unlocked_ioctl
= iio_ioctl
,
1188 .compat_ioctl
= iio_ioctl
,
1191 static int iio_check_unique_scan_index(struct iio_dev
*indio_dev
)
1194 const struct iio_chan_spec
*channels
= indio_dev
->channels
;
1196 if (!(indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
))
1199 for (i
= 0; i
< indio_dev
->num_channels
- 1; i
++) {
1200 if (channels
[i
].scan_index
< 0)
1202 for (j
= i
+ 1; j
< indio_dev
->num_channels
; j
++)
1203 if (channels
[i
].scan_index
== channels
[j
].scan_index
) {
1204 dev_err(&indio_dev
->dev
,
1205 "Duplicate scan index %d\n",
1206 channels
[i
].scan_index
);
1214 static const struct iio_buffer_setup_ops noop_ring_setup_ops
;
1217 * iio_device_register() - register a device with the IIO subsystem
1218 * @indio_dev: Device structure filled by the device driver
1220 int iio_device_register(struct iio_dev
*indio_dev
)
1224 /* If the calling driver did not initialize of_node, do it here */
1225 if (!indio_dev
->dev
.of_node
&& indio_dev
->dev
.parent
)
1226 indio_dev
->dev
.of_node
= indio_dev
->dev
.parent
->of_node
;
1228 ret
= iio_check_unique_scan_index(indio_dev
);
1232 /* configure elements for the chrdev */
1233 indio_dev
->dev
.devt
= MKDEV(MAJOR(iio_devt
), indio_dev
->id
);
1235 ret
= iio_device_register_debugfs(indio_dev
);
1237 dev_err(indio_dev
->dev
.parent
,
1238 "Failed to register debugfs interfaces\n");
1242 ret
= iio_buffer_alloc_sysfs_and_mask(indio_dev
);
1244 dev_err(indio_dev
->dev
.parent
,
1245 "Failed to create buffer sysfs interfaces\n");
1246 goto error_unreg_debugfs
;
1249 ret
= iio_device_register_sysfs(indio_dev
);
1251 dev_err(indio_dev
->dev
.parent
,
1252 "Failed to register sysfs interfaces\n");
1253 goto error_buffer_free_sysfs
;
1255 ret
= iio_device_register_eventset(indio_dev
);
1257 dev_err(indio_dev
->dev
.parent
,
1258 "Failed to register event set\n");
1259 goto error_free_sysfs
;
1261 if (indio_dev
->modes
& (INDIO_BUFFER_TRIGGERED
| INDIO_EVENT_TRIGGERED
))
1262 iio_device_register_trigger_consumer(indio_dev
);
1264 if ((indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
) &&
1265 indio_dev
->setup_ops
== NULL
)
1266 indio_dev
->setup_ops
= &noop_ring_setup_ops
;
1268 cdev_init(&indio_dev
->chrdev
, &iio_buffer_fileops
);
1269 indio_dev
->chrdev
.owner
= indio_dev
->info
->driver_module
;
1270 indio_dev
->chrdev
.kobj
.parent
= &indio_dev
->dev
.kobj
;
1271 ret
= cdev_add(&indio_dev
->chrdev
, indio_dev
->dev
.devt
, 1);
1273 goto error_unreg_eventset
;
1275 ret
= device_add(&indio_dev
->dev
);
1277 goto error_cdev_del
;
1281 cdev_del(&indio_dev
->chrdev
);
1282 error_unreg_eventset
:
1283 iio_device_unregister_eventset(indio_dev
);
1285 iio_device_unregister_sysfs(indio_dev
);
1286 error_buffer_free_sysfs
:
1287 iio_buffer_free_sysfs_and_mask(indio_dev
);
1288 error_unreg_debugfs
:
1289 iio_device_unregister_debugfs(indio_dev
);
1292 EXPORT_SYMBOL(iio_device_register
);
1295 * iio_device_unregister() - unregister a device from the IIO subsystem
1296 * @indio_dev: Device structure representing the device.
1298 void iio_device_unregister(struct iio_dev
*indio_dev
)
1300 mutex_lock(&indio_dev
->info_exist_lock
);
1302 device_del(&indio_dev
->dev
);
1304 if (indio_dev
->chrdev
.dev
)
1305 cdev_del(&indio_dev
->chrdev
);
1306 iio_device_unregister_debugfs(indio_dev
);
1308 iio_disable_all_buffers(indio_dev
);
1310 indio_dev
->info
= NULL
;
1312 iio_device_wakeup_eventset(indio_dev
);
1313 iio_buffer_wakeup_poll(indio_dev
);
1315 mutex_unlock(&indio_dev
->info_exist_lock
);
1317 iio_buffer_free_sysfs_and_mask(indio_dev
);
1319 EXPORT_SYMBOL(iio_device_unregister
);
1321 static void devm_iio_device_unreg(struct device
*dev
, void *res
)
1323 iio_device_unregister(*(struct iio_dev
**)res
);
1327 * devm_iio_device_register - Resource-managed iio_device_register()
1328 * @dev: Device to allocate iio_dev for
1329 * @indio_dev: Device structure filled by the device driver
1331 * Managed iio_device_register. The IIO device registered with this
1332 * function is automatically unregistered on driver detach. This function
1333 * calls iio_device_register() internally. Refer to that function for more
1336 * If an iio_dev registered with this function needs to be unregistered
1337 * separately, devm_iio_device_unregister() must be used.
1340 * 0 on success, negative error number on failure.
1342 int devm_iio_device_register(struct device
*dev
, struct iio_dev
*indio_dev
)
1344 struct iio_dev
**ptr
;
1347 ptr
= devres_alloc(devm_iio_device_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1352 ret
= iio_device_register(indio_dev
);
1354 devres_add(dev
, ptr
);
1360 EXPORT_SYMBOL_GPL(devm_iio_device_register
);
1363 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1364 * @dev: Device this iio_dev belongs to
1365 * @indio_dev: the iio_dev associated with the device
1367 * Unregister iio_dev registered with devm_iio_device_register().
1369 void devm_iio_device_unregister(struct device
*dev
, struct iio_dev
*indio_dev
)
1373 rc
= devres_release(dev
, devm_iio_device_unreg
,
1374 devm_iio_device_match
, indio_dev
);
1377 EXPORT_SYMBOL_GPL(devm_iio_device_unregister
);
1380 * iio_device_claim_direct_mode - Keep device in direct mode
1381 * @indio_dev: the iio_dev associated with the device
1383 * If the device is in direct mode it is guaranteed to stay
1384 * that way until iio_device_release_direct_mode() is called.
1386 * Use with iio_device_release_direct_mode()
1388 * Returns: 0 on success, -EBUSY on failure
1390 int iio_device_claim_direct_mode(struct iio_dev
*indio_dev
)
1392 mutex_lock(&indio_dev
->mlock
);
1394 if (iio_buffer_enabled(indio_dev
)) {
1395 mutex_unlock(&indio_dev
->mlock
);
1400 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode
);
1403 * iio_device_release_direct_mode - releases claim on direct mode
1404 * @indio_dev: the iio_dev associated with the device
1406 * Release the claim. Device is no longer guaranteed to stay
1409 * Use with iio_device_claim_direct_mode()
1411 void iio_device_release_direct_mode(struct iio_dev
*indio_dev
)
1413 mutex_unlock(&indio_dev
->mlock
);
1415 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode
);
1417 subsys_initcall(iio_init
);
1418 module_exit(iio_exit
);
1420 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1421 MODULE_DESCRIPTION("Industrial I/O core");
1422 MODULE_LICENSE("GPL");