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",
72 [IIO_HUMIDITYRELATIVE
] = "humidityrelative",
73 [IIO_ACTIVITY
] = "activity",
74 [IIO_STEPS
] = "steps",
75 [IIO_ENERGY
] = "energy",
76 [IIO_DISTANCE
] = "distance",
77 [IIO_VELOCITY
] = "velocity",
80 static const char * const iio_modifier_names
[] = {
84 [IIO_MOD_ROOT_SUM_SQUARED_X_Y
] = "sqrt(x^2+y^2)",
85 [IIO_MOD_SUM_SQUARED_X_Y_Z
] = "x^2+y^2+z^2",
86 [IIO_MOD_LIGHT_BOTH
] = "both",
87 [IIO_MOD_LIGHT_IR
] = "ir",
88 [IIO_MOD_LIGHT_CLEAR
] = "clear",
89 [IIO_MOD_LIGHT_RED
] = "red",
90 [IIO_MOD_LIGHT_GREEN
] = "green",
91 [IIO_MOD_LIGHT_BLUE
] = "blue",
92 [IIO_MOD_QUATERNION
] = "quaternion",
93 [IIO_MOD_TEMP_AMBIENT
] = "ambient",
94 [IIO_MOD_TEMP_OBJECT
] = "object",
95 [IIO_MOD_NORTH_MAGN
] = "from_north_magnetic",
96 [IIO_MOD_NORTH_TRUE
] = "from_north_true",
97 [IIO_MOD_NORTH_MAGN_TILT_COMP
] = "from_north_magnetic_tilt_comp",
98 [IIO_MOD_NORTH_TRUE_TILT_COMP
] = "from_north_true_tilt_comp",
99 [IIO_MOD_RUNNING
] = "running",
100 [IIO_MOD_JOGGING
] = "jogging",
101 [IIO_MOD_WALKING
] = "walking",
102 [IIO_MOD_STILL
] = "still",
103 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
] = "sqrt(x^2+y^2+z^2)",
108 /* relies on pairs of these shared then separate */
109 static const char * const iio_chan_info_postfix
[] = {
110 [IIO_CHAN_INFO_RAW
] = "raw",
111 [IIO_CHAN_INFO_PROCESSED
] = "input",
112 [IIO_CHAN_INFO_SCALE
] = "scale",
113 [IIO_CHAN_INFO_OFFSET
] = "offset",
114 [IIO_CHAN_INFO_CALIBSCALE
] = "calibscale",
115 [IIO_CHAN_INFO_CALIBBIAS
] = "calibbias",
116 [IIO_CHAN_INFO_PEAK
] = "peak_raw",
117 [IIO_CHAN_INFO_PEAK_SCALE
] = "peak_scale",
118 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW
] = "quadrature_correction_raw",
119 [IIO_CHAN_INFO_AVERAGE_RAW
] = "mean_raw",
120 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
]
121 = "filter_low_pass_3db_frequency",
122 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
]
123 = "filter_high_pass_3db_frequency",
124 [IIO_CHAN_INFO_SAMP_FREQ
] = "sampling_frequency",
125 [IIO_CHAN_INFO_FREQUENCY
] = "frequency",
126 [IIO_CHAN_INFO_PHASE
] = "phase",
127 [IIO_CHAN_INFO_HARDWAREGAIN
] = "hardwaregain",
128 [IIO_CHAN_INFO_HYSTERESIS
] = "hysteresis",
129 [IIO_CHAN_INFO_INT_TIME
] = "integration_time",
130 [IIO_CHAN_INFO_ENABLE
] = "en",
131 [IIO_CHAN_INFO_CALIBHEIGHT
] = "calibheight",
132 [IIO_CHAN_INFO_CALIBWEIGHT
] = "calibweight",
133 [IIO_CHAN_INFO_DEBOUNCE_COUNT
] = "debounce_count",
134 [IIO_CHAN_INFO_DEBOUNCE_TIME
] = "debounce_time",
135 [IIO_CHAN_INFO_CALIBEMISSIVITY
] = "calibemissivity",
136 [IIO_CHAN_INFO_OVERSAMPLING_RATIO
] = "oversampling_ratio",
140 * iio_find_channel_from_si() - get channel from its scan index
142 * @si: scan index to match
144 const struct iio_chan_spec
145 *iio_find_channel_from_si(struct iio_dev
*indio_dev
, int si
)
149 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
150 if (indio_dev
->channels
[i
].scan_index
== si
)
151 return &indio_dev
->channels
[i
];
155 /* This turns up an awful lot */
156 ssize_t
iio_read_const_attr(struct device
*dev
,
157 struct device_attribute
*attr
,
160 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
162 EXPORT_SYMBOL(iio_read_const_attr
);
164 static int __init
iio_init(void)
168 /* Register sysfs bus */
169 ret
= bus_register(&iio_bus_type
);
171 pr_err("could not register bus type\n");
175 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
177 pr_err("failed to allocate char dev region\n");
178 goto error_unregister_bus_type
;
181 iio_debugfs_dentry
= debugfs_create_dir("iio", NULL
);
185 error_unregister_bus_type
:
186 bus_unregister(&iio_bus_type
);
191 static void __exit
iio_exit(void)
194 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
195 bus_unregister(&iio_bus_type
);
196 debugfs_remove(iio_debugfs_dentry
);
199 #if defined(CONFIG_DEBUG_FS)
200 static ssize_t
iio_debugfs_read_reg(struct file
*file
, char __user
*userbuf
,
201 size_t count
, loff_t
*ppos
)
203 struct iio_dev
*indio_dev
= file
->private_data
;
209 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
,
210 indio_dev
->cached_reg_addr
,
213 dev_err(indio_dev
->dev
.parent
, "%s: read failed\n", __func__
);
215 len
= snprintf(buf
, sizeof(buf
), "0x%X\n", val
);
217 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
220 static ssize_t
iio_debugfs_write_reg(struct file
*file
,
221 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
223 struct iio_dev
*indio_dev
= file
->private_data
;
228 count
= min_t(size_t, count
, (sizeof(buf
)-1));
229 if (copy_from_user(buf
, userbuf
, count
))
234 ret
= sscanf(buf
, "%i %i", ®
, &val
);
238 indio_dev
->cached_reg_addr
= reg
;
241 indio_dev
->cached_reg_addr
= reg
;
242 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
, reg
,
245 dev_err(indio_dev
->dev
.parent
, "%s: write failed\n",
257 static const struct file_operations iio_debugfs_reg_fops
= {
259 .read
= iio_debugfs_read_reg
,
260 .write
= iio_debugfs_write_reg
,
263 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
265 debugfs_remove_recursive(indio_dev
->debugfs_dentry
);
268 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
272 if (indio_dev
->info
->debugfs_reg_access
== NULL
)
275 if (!iio_debugfs_dentry
)
278 indio_dev
->debugfs_dentry
=
279 debugfs_create_dir(dev_name(&indio_dev
->dev
),
281 if (indio_dev
->debugfs_dentry
== NULL
) {
282 dev_warn(indio_dev
->dev
.parent
,
283 "Failed to create debugfs directory\n");
287 d
= debugfs_create_file("direct_reg_access", 0644,
288 indio_dev
->debugfs_dentry
,
289 indio_dev
, &iio_debugfs_reg_fops
);
291 iio_device_unregister_debugfs(indio_dev
);
298 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
303 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
306 #endif /* CONFIG_DEBUG_FS */
308 static ssize_t
iio_read_channel_ext_info(struct device
*dev
,
309 struct device_attribute
*attr
,
312 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
313 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
314 const struct iio_chan_spec_ext_info
*ext_info
;
316 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
318 return ext_info
->read(indio_dev
, ext_info
->private, this_attr
->c
, buf
);
321 static ssize_t
iio_write_channel_ext_info(struct device
*dev
,
322 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
->write(indio_dev
, ext_info
->private,
333 this_attr
->c
, buf
, len
);
336 ssize_t
iio_enum_available_read(struct iio_dev
*indio_dev
,
337 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
339 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
346 for (i
= 0; i
< e
->num_items
; ++i
)
347 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%s ", e
->items
[i
]);
349 /* replace last space with a newline */
354 EXPORT_SYMBOL_GPL(iio_enum_available_read
);
356 ssize_t
iio_enum_read(struct iio_dev
*indio_dev
,
357 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
359 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
365 i
= e
->get(indio_dev
, chan
);
368 else if (i
>= e
->num_items
)
371 return snprintf(buf
, PAGE_SIZE
, "%s\n", e
->items
[i
]);
373 EXPORT_SYMBOL_GPL(iio_enum_read
);
375 ssize_t
iio_enum_write(struct iio_dev
*indio_dev
,
376 uintptr_t priv
, const struct iio_chan_spec
*chan
, const char *buf
,
379 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
386 for (i
= 0; i
< e
->num_items
; i
++) {
387 if (sysfs_streq(buf
, e
->items
[i
]))
391 if (i
== e
->num_items
)
394 ret
= e
->set(indio_dev
, chan
, i
);
395 return ret
? ret
: len
;
397 EXPORT_SYMBOL_GPL(iio_enum_write
);
400 * iio_format_value() - Formats a IIO value into its string representation
401 * @buf: The buffer to which the formated value gets written
402 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
403 * parameters are formatted.
404 * @vals: pointer to the values, exact meaning depends on the type parameter.
406 ssize_t
iio_format_value(char *buf
, unsigned int type
, int size
, int *vals
)
408 unsigned long long tmp
;
409 bool scale_db
= false;
413 return sprintf(buf
, "%d\n", vals
[0]);
414 case IIO_VAL_INT_PLUS_MICRO_DB
:
416 case IIO_VAL_INT_PLUS_MICRO
:
418 return sprintf(buf
, "-%ld.%06u%s\n", abs(vals
[0]),
420 scale_db
? " dB" : "");
422 return sprintf(buf
, "%d.%06u%s\n", vals
[0], vals
[1],
423 scale_db
? " dB" : "");
424 case IIO_VAL_INT_PLUS_NANO
:
426 return sprintf(buf
, "-%ld.%09u\n", abs(vals
[0]),
429 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
430 case IIO_VAL_FRACTIONAL
:
431 tmp
= div_s64((s64
)vals
[0] * 1000000000LL, vals
[1]);
432 vals
[1] = do_div(tmp
, 1000000000LL);
434 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
435 case IIO_VAL_FRACTIONAL_LOG2
:
436 tmp
= (s64
)vals
[0] * 1000000000LL >> vals
[1];
437 vals
[1] = do_div(tmp
, 1000000000LL);
439 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
440 case IIO_VAL_INT_MULTIPLE
:
445 for (i
= 0; i
< size
; ++i
)
446 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "%d ",
448 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "\n");
456 static ssize_t
iio_read_channel_info(struct device
*dev
,
457 struct device_attribute
*attr
,
460 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
461 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
462 int vals
[INDIO_MAX_RAW_ELEMENTS
];
466 if (indio_dev
->info
->read_raw_multi
)
467 ret
= indio_dev
->info
->read_raw_multi(indio_dev
, this_attr
->c
,
468 INDIO_MAX_RAW_ELEMENTS
,
472 ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
473 &vals
[0], &vals
[1], this_attr
->address
);
478 return iio_format_value(buf
, ret
, val_len
, vals
);
482 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
483 * @str: The string to parse
484 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
485 * @integer: The integer part of the number
486 * @fract: The fractional part of the number
488 * Returns 0 on success, or a negative error code if the string could not be
491 int iio_str_to_fixpoint(const char *str
, int fract_mult
,
492 int *integer
, int *fract
)
495 bool integer_part
= true, negative
= false;
500 } else if (str
[0] == '+') {
505 if ('0' <= *str
&& *str
<= '9') {
507 i
= i
* 10 + *str
- '0';
509 f
+= fract_mult
* (*str
- '0');
512 } else if (*str
== '\n') {
513 if (*(str
+ 1) == '\0')
517 } else if (*str
== '.' && integer_part
) {
518 integer_part
= false;
537 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint
);
539 static ssize_t
iio_write_channel_info(struct device
*dev
,
540 struct device_attribute
*attr
,
544 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
545 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
546 int ret
, fract_mult
= 100000;
549 /* Assumes decimal - precision based on number of digits */
550 if (!indio_dev
->info
->write_raw
)
553 if (indio_dev
->info
->write_raw_get_fmt
)
554 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
555 this_attr
->c
, this_attr
->address
)) {
556 case IIO_VAL_INT_PLUS_MICRO
:
559 case IIO_VAL_INT_PLUS_NANO
:
560 fract_mult
= 100000000;
566 ret
= iio_str_to_fixpoint(buf
, fract_mult
, &integer
, &fract
);
570 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
571 integer
, fract
, this_attr
->address
);
579 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
581 struct iio_chan_spec
const *chan
,
582 ssize_t (*readfunc
)(struct device
*dev
,
583 struct device_attribute
*attr
,
585 ssize_t (*writefunc
)(struct device
*dev
,
586 struct device_attribute
*attr
,
589 enum iio_shared_by shared_by
)
594 sysfs_attr_init(&dev_attr
->attr
);
596 /* Build up postfix of <extend_name>_<modifier>_postfix */
597 if (chan
->modified
&& (shared_by
== IIO_SEPARATE
)) {
598 if (chan
->extend_name
)
599 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
600 iio_modifier_names
[chan
605 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s",
606 iio_modifier_names
[chan
610 if (chan
->extend_name
== NULL
|| shared_by
!= IIO_SEPARATE
)
611 full_postfix
= kstrdup(postfix
, GFP_KERNEL
);
613 full_postfix
= kasprintf(GFP_KERNEL
,
618 if (full_postfix
== NULL
)
621 if (chan
->differential
) { /* Differential can not have modifier */
623 case IIO_SHARED_BY_ALL
:
624 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
626 case IIO_SHARED_BY_DIR
:
627 name
= kasprintf(GFP_KERNEL
, "%s_%s",
628 iio_direction
[chan
->output
],
631 case IIO_SHARED_BY_TYPE
:
632 name
= kasprintf(GFP_KERNEL
, "%s_%s-%s_%s",
633 iio_direction
[chan
->output
],
634 iio_chan_type_name_spec
[chan
->type
],
635 iio_chan_type_name_spec
[chan
->type
],
639 if (!chan
->indexed
) {
640 WARN_ON("Differential channels must be indexed\n");
642 goto error_free_full_postfix
;
644 name
= kasprintf(GFP_KERNEL
,
646 iio_direction
[chan
->output
],
647 iio_chan_type_name_spec
[chan
->type
],
649 iio_chan_type_name_spec
[chan
->type
],
654 } else { /* Single ended */
656 case IIO_SHARED_BY_ALL
:
657 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
659 case IIO_SHARED_BY_DIR
:
660 name
= kasprintf(GFP_KERNEL
, "%s_%s",
661 iio_direction
[chan
->output
],
664 case IIO_SHARED_BY_TYPE
:
665 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
666 iio_direction
[chan
->output
],
667 iio_chan_type_name_spec
[chan
->type
],
673 name
= kasprintf(GFP_KERNEL
, "%s_%s%d_%s",
674 iio_direction
[chan
->output
],
675 iio_chan_type_name_spec
[chan
->type
],
679 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
680 iio_direction
[chan
->output
],
681 iio_chan_type_name_spec
[chan
->type
],
688 goto error_free_full_postfix
;
690 dev_attr
->attr
.name
= name
;
693 dev_attr
->attr
.mode
|= S_IRUGO
;
694 dev_attr
->show
= readfunc
;
698 dev_attr
->attr
.mode
|= S_IWUSR
;
699 dev_attr
->store
= writefunc
;
702 error_free_full_postfix
:
708 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
710 kfree(dev_attr
->attr
.name
);
713 int __iio_add_chan_devattr(const char *postfix
,
714 struct iio_chan_spec
const *chan
,
715 ssize_t (*readfunc
)(struct device
*dev
,
716 struct device_attribute
*attr
,
718 ssize_t (*writefunc
)(struct device
*dev
,
719 struct device_attribute
*attr
,
723 enum iio_shared_by shared_by
,
725 struct list_head
*attr_list
)
728 struct iio_dev_attr
*iio_attr
, *t
;
730 iio_attr
= kzalloc(sizeof(*iio_attr
), GFP_KERNEL
);
731 if (iio_attr
== NULL
)
733 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
735 readfunc
, writefunc
, shared_by
);
737 goto error_iio_dev_attr_free
;
739 iio_attr
->address
= mask
;
740 list_for_each_entry(t
, attr_list
, l
)
741 if (strcmp(t
->dev_attr
.attr
.name
,
742 iio_attr
->dev_attr
.attr
.name
) == 0) {
743 if (shared_by
== IIO_SEPARATE
)
744 dev_err(dev
, "tried to double register : %s\n",
745 t
->dev_attr
.attr
.name
);
747 goto error_device_attr_deinit
;
749 list_add(&iio_attr
->l
, attr_list
);
753 error_device_attr_deinit
:
754 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
755 error_iio_dev_attr_free
:
760 static int iio_device_add_info_mask_type(struct iio_dev
*indio_dev
,
761 struct iio_chan_spec
const *chan
,
762 enum iio_shared_by shared_by
,
763 const long *infomask
)
765 int i
, ret
, attrcount
= 0;
767 for_each_set_bit(i
, infomask
, sizeof(infomask
)*8) {
768 if (i
>= ARRAY_SIZE(iio_chan_info_postfix
))
770 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
],
772 &iio_read_channel_info
,
773 &iio_write_channel_info
,
777 &indio_dev
->channel_attr_list
);
778 if ((ret
== -EBUSY
) && (shared_by
!= IIO_SEPARATE
))
788 static int iio_device_add_channel_sysfs(struct iio_dev
*indio_dev
,
789 struct iio_chan_spec
const *chan
)
791 int ret
, attrcount
= 0;
792 const struct iio_chan_spec_ext_info
*ext_info
;
794 if (chan
->channel
< 0)
796 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
798 &chan
->info_mask_separate
);
803 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
805 &chan
->info_mask_shared_by_type
);
810 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
812 &chan
->info_mask_shared_by_dir
);
817 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
819 &chan
->info_mask_shared_by_all
);
824 if (chan
->ext_info
) {
826 for (ext_info
= chan
->ext_info
; ext_info
->name
; ext_info
++) {
827 ret
= __iio_add_chan_devattr(ext_info
->name
,
830 &iio_read_channel_ext_info
: NULL
,
832 &iio_write_channel_ext_info
: NULL
,
836 &indio_dev
->channel_attr_list
);
838 if (ret
== -EBUSY
&& ext_info
->shared
)
852 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
853 * @attr_list: List of IIO device attributes
855 * This function frees the memory allocated for each of the IIO device
856 * attributes in the list.
858 void iio_free_chan_devattr_list(struct list_head
*attr_list
)
860 struct iio_dev_attr
*p
, *n
;
862 list_for_each_entry_safe(p
, n
, attr_list
, l
) {
863 kfree(p
->dev_attr
.attr
.name
);
869 static ssize_t
iio_show_dev_name(struct device
*dev
,
870 struct device_attribute
*attr
,
873 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
874 return snprintf(buf
, PAGE_SIZE
, "%s\n", indio_dev
->name
);
877 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
879 static int iio_device_register_sysfs(struct iio_dev
*indio_dev
)
881 int i
, ret
= 0, attrcount
, attrn
, attrcount_orig
= 0;
882 struct iio_dev_attr
*p
;
883 struct attribute
**attr
;
885 /* First count elements in any existing group */
886 if (indio_dev
->info
->attrs
) {
887 attr
= indio_dev
->info
->attrs
->attrs
;
888 while (*attr
++ != NULL
)
891 attrcount
= attrcount_orig
;
893 * New channel registration method - relies on the fact a group does
894 * not need to be initialized if its name is NULL.
896 if (indio_dev
->channels
)
897 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
898 ret
= iio_device_add_channel_sysfs(indio_dev
,
902 goto error_clear_attrs
;
909 indio_dev
->chan_attr_group
.attrs
= kcalloc(attrcount
+ 1,
910 sizeof(indio_dev
->chan_attr_group
.attrs
[0]),
912 if (indio_dev
->chan_attr_group
.attrs
== NULL
) {
914 goto error_clear_attrs
;
916 /* Copy across original attributes */
917 if (indio_dev
->info
->attrs
)
918 memcpy(indio_dev
->chan_attr_group
.attrs
,
919 indio_dev
->info
->attrs
->attrs
,
920 sizeof(indio_dev
->chan_attr_group
.attrs
[0])
922 attrn
= attrcount_orig
;
923 /* Add all elements from the list. */
924 list_for_each_entry(p
, &indio_dev
->channel_attr_list
, l
)
925 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
927 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_name
.attr
;
929 indio_dev
->groups
[indio_dev
->groupcounter
++] =
930 &indio_dev
->chan_attr_group
;
935 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
940 static void iio_device_unregister_sysfs(struct iio_dev
*indio_dev
)
943 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
944 kfree(indio_dev
->chan_attr_group
.attrs
);
945 indio_dev
->chan_attr_group
.attrs
= NULL
;
948 static void iio_dev_release(struct device
*device
)
950 struct iio_dev
*indio_dev
= dev_to_iio_dev(device
);
951 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
952 iio_device_unregister_trigger_consumer(indio_dev
);
953 iio_device_unregister_eventset(indio_dev
);
954 iio_device_unregister_sysfs(indio_dev
);
956 iio_buffer_put(indio_dev
->buffer
);
958 ida_simple_remove(&iio_ida
, indio_dev
->id
);
962 struct device_type iio_device_type
= {
963 .name
= "iio_device",
964 .release
= iio_dev_release
,
968 * iio_device_alloc() - allocate an iio_dev from a driver
969 * @sizeof_priv: Space to allocate for private structure.
971 struct iio_dev
*iio_device_alloc(int sizeof_priv
)
976 alloc_size
= sizeof(struct iio_dev
);
978 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
979 alloc_size
+= sizeof_priv
;
981 /* ensure 32-byte alignment of whole construct ? */
982 alloc_size
+= IIO_ALIGN
- 1;
984 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
987 dev
->dev
.groups
= dev
->groups
;
988 dev
->dev
.type
= &iio_device_type
;
989 dev
->dev
.bus
= &iio_bus_type
;
990 device_initialize(&dev
->dev
);
991 dev_set_drvdata(&dev
->dev
, (void *)dev
);
992 mutex_init(&dev
->mlock
);
993 mutex_init(&dev
->info_exist_lock
);
994 INIT_LIST_HEAD(&dev
->channel_attr_list
);
996 dev
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
998 /* cannot use a dev_err as the name isn't available */
999 pr_err("failed to get device id\n");
1003 dev_set_name(&dev
->dev
, "iio:device%d", dev
->id
);
1004 INIT_LIST_HEAD(&dev
->buffer_list
);
1009 EXPORT_SYMBOL(iio_device_alloc
);
1012 * iio_device_free() - free an iio_dev from a driver
1013 * @dev: the iio_dev associated with the device
1015 void iio_device_free(struct iio_dev
*dev
)
1018 put_device(&dev
->dev
);
1020 EXPORT_SYMBOL(iio_device_free
);
1022 static void devm_iio_device_release(struct device
*dev
, void *res
)
1024 iio_device_free(*(struct iio_dev
**)res
);
1027 static int devm_iio_device_match(struct device
*dev
, void *res
, void *data
)
1029 struct iio_dev
**r
= res
;
1038 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1039 * @dev: Device to allocate iio_dev for
1040 * @sizeof_priv: Space to allocate for private structure.
1042 * Managed iio_device_alloc. iio_dev allocated with this function is
1043 * automatically freed on driver detach.
1045 * If an iio_dev allocated with this function needs to be freed separately,
1046 * devm_iio_device_free() must be used.
1049 * Pointer to allocated iio_dev on success, NULL on failure.
1051 struct iio_dev
*devm_iio_device_alloc(struct device
*dev
, int sizeof_priv
)
1053 struct iio_dev
**ptr
, *iio_dev
;
1055 ptr
= devres_alloc(devm_iio_device_release
, sizeof(*ptr
),
1060 iio_dev
= iio_device_alloc(sizeof_priv
);
1063 devres_add(dev
, ptr
);
1070 EXPORT_SYMBOL_GPL(devm_iio_device_alloc
);
1073 * devm_iio_device_free - Resource-managed iio_device_free()
1074 * @dev: Device this iio_dev belongs to
1075 * @iio_dev: the iio_dev associated with the device
1077 * Free iio_dev allocated with devm_iio_device_alloc().
1079 void devm_iio_device_free(struct device
*dev
, struct iio_dev
*iio_dev
)
1083 rc
= devres_release(dev
, devm_iio_device_release
,
1084 devm_iio_device_match
, iio_dev
);
1087 EXPORT_SYMBOL_GPL(devm_iio_device_free
);
1090 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1092 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1094 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1095 struct iio_dev
, chrdev
);
1097 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
))
1100 iio_device_get(indio_dev
);
1102 filp
->private_data
= indio_dev
;
1108 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1110 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1112 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1113 struct iio_dev
, chrdev
);
1114 clear_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
);
1115 iio_device_put(indio_dev
);
1120 /* Somewhat of a cross file organization violation - ioctls here are actually
1122 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1124 struct iio_dev
*indio_dev
= filp
->private_data
;
1125 int __user
*ip
= (int __user
*)arg
;
1128 if (!indio_dev
->info
)
1131 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1132 fd
= iio_event_getfd(indio_dev
);
1133 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1140 static const struct file_operations iio_buffer_fileops
= {
1141 .read
= iio_buffer_read_first_n_outer_addr
,
1142 .release
= iio_chrdev_release
,
1143 .open
= iio_chrdev_open
,
1144 .poll
= iio_buffer_poll_addr
,
1145 .owner
= THIS_MODULE
,
1146 .llseek
= noop_llseek
,
1147 .unlocked_ioctl
= iio_ioctl
,
1148 .compat_ioctl
= iio_ioctl
,
1151 static int iio_check_unique_scan_index(struct iio_dev
*indio_dev
)
1154 const struct iio_chan_spec
*channels
= indio_dev
->channels
;
1156 if (!(indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
))
1159 for (i
= 0; i
< indio_dev
->num_channels
- 1; i
++) {
1160 if (channels
[i
].scan_index
< 0)
1162 for (j
= i
+ 1; j
< indio_dev
->num_channels
; j
++)
1163 if (channels
[i
].scan_index
== channels
[j
].scan_index
) {
1164 dev_err(&indio_dev
->dev
,
1165 "Duplicate scan index %d\n",
1166 channels
[i
].scan_index
);
1174 static const struct iio_buffer_setup_ops noop_ring_setup_ops
;
1177 * iio_device_register() - register a device with the IIO subsystem
1178 * @indio_dev: Device structure filled by the device driver
1180 int iio_device_register(struct iio_dev
*indio_dev
)
1184 /* If the calling driver did not initialize of_node, do it here */
1185 if (!indio_dev
->dev
.of_node
&& indio_dev
->dev
.parent
)
1186 indio_dev
->dev
.of_node
= indio_dev
->dev
.parent
->of_node
;
1188 ret
= iio_check_unique_scan_index(indio_dev
);
1192 /* configure elements for the chrdev */
1193 indio_dev
->dev
.devt
= MKDEV(MAJOR(iio_devt
), indio_dev
->id
);
1195 ret
= iio_device_register_debugfs(indio_dev
);
1197 dev_err(indio_dev
->dev
.parent
,
1198 "Failed to register debugfs interfaces\n");
1202 ret
= iio_buffer_alloc_sysfs_and_mask(indio_dev
);
1204 dev_err(indio_dev
->dev
.parent
,
1205 "Failed to create buffer sysfs interfaces\n");
1206 goto error_unreg_debugfs
;
1209 ret
= iio_device_register_sysfs(indio_dev
);
1211 dev_err(indio_dev
->dev
.parent
,
1212 "Failed to register sysfs interfaces\n");
1213 goto error_buffer_free_sysfs
;
1215 ret
= iio_device_register_eventset(indio_dev
);
1217 dev_err(indio_dev
->dev
.parent
,
1218 "Failed to register event set\n");
1219 goto error_free_sysfs
;
1221 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
1222 iio_device_register_trigger_consumer(indio_dev
);
1224 if ((indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
) &&
1225 indio_dev
->setup_ops
== NULL
)
1226 indio_dev
->setup_ops
= &noop_ring_setup_ops
;
1228 cdev_init(&indio_dev
->chrdev
, &iio_buffer_fileops
);
1229 indio_dev
->chrdev
.owner
= indio_dev
->info
->driver_module
;
1230 indio_dev
->chrdev
.kobj
.parent
= &indio_dev
->dev
.kobj
;
1231 ret
= cdev_add(&indio_dev
->chrdev
, indio_dev
->dev
.devt
, 1);
1233 goto error_unreg_eventset
;
1235 ret
= device_add(&indio_dev
->dev
);
1237 goto error_cdev_del
;
1241 cdev_del(&indio_dev
->chrdev
);
1242 error_unreg_eventset
:
1243 iio_device_unregister_eventset(indio_dev
);
1245 iio_device_unregister_sysfs(indio_dev
);
1246 error_buffer_free_sysfs
:
1247 iio_buffer_free_sysfs_and_mask(indio_dev
);
1248 error_unreg_debugfs
:
1249 iio_device_unregister_debugfs(indio_dev
);
1252 EXPORT_SYMBOL(iio_device_register
);
1255 * iio_device_unregister() - unregister a device from the IIO subsystem
1256 * @indio_dev: Device structure representing the device.
1258 void iio_device_unregister(struct iio_dev
*indio_dev
)
1260 mutex_lock(&indio_dev
->info_exist_lock
);
1262 device_del(&indio_dev
->dev
);
1264 if (indio_dev
->chrdev
.dev
)
1265 cdev_del(&indio_dev
->chrdev
);
1266 iio_device_unregister_debugfs(indio_dev
);
1268 iio_disable_all_buffers(indio_dev
);
1270 indio_dev
->info
= NULL
;
1272 iio_device_wakeup_eventset(indio_dev
);
1273 iio_buffer_wakeup_poll(indio_dev
);
1275 mutex_unlock(&indio_dev
->info_exist_lock
);
1277 iio_buffer_free_sysfs_and_mask(indio_dev
);
1279 EXPORT_SYMBOL(iio_device_unregister
);
1281 static void devm_iio_device_unreg(struct device
*dev
, void *res
)
1283 iio_device_unregister(*(struct iio_dev
**)res
);
1287 * devm_iio_device_register - Resource-managed iio_device_register()
1288 * @dev: Device to allocate iio_dev for
1289 * @indio_dev: Device structure filled by the device driver
1291 * Managed iio_device_register. The IIO device registered with this
1292 * function is automatically unregistered on driver detach. This function
1293 * calls iio_device_register() internally. Refer to that function for more
1296 * If an iio_dev registered with this function needs to be unregistered
1297 * separately, devm_iio_device_unregister() must be used.
1300 * 0 on success, negative error number on failure.
1302 int devm_iio_device_register(struct device
*dev
, struct iio_dev
*indio_dev
)
1304 struct iio_dev
**ptr
;
1307 ptr
= devres_alloc(devm_iio_device_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1312 ret
= iio_device_register(indio_dev
);
1314 devres_add(dev
, ptr
);
1320 EXPORT_SYMBOL_GPL(devm_iio_device_register
);
1323 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1324 * @dev: Device this iio_dev belongs to
1325 * @indio_dev: the iio_dev associated with the device
1327 * Unregister iio_dev registered with devm_iio_device_register().
1329 void devm_iio_device_unregister(struct device
*dev
, struct iio_dev
*indio_dev
)
1333 rc
= devres_release(dev
, devm_iio_device_unreg
,
1334 devm_iio_device_match
, indio_dev
);
1337 EXPORT_SYMBOL_GPL(devm_iio_device_unregister
);
1339 subsys_initcall(iio_init
);
1340 module_exit(iio_exit
);
1342 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1343 MODULE_DESCRIPTION("Industrial I/O core");
1344 MODULE_LICENSE("GPL");