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",
75 static const char * const iio_modifier_names
[] = {
79 [IIO_MOD_ROOT_SUM_SQUARED_X_Y
] = "sqrt(x^2+y^2)",
80 [IIO_MOD_SUM_SQUARED_X_Y_Z
] = "x^2+y^2+z^2",
81 [IIO_MOD_LIGHT_BOTH
] = "both",
82 [IIO_MOD_LIGHT_IR
] = "ir",
83 [IIO_MOD_LIGHT_CLEAR
] = "clear",
84 [IIO_MOD_LIGHT_RED
] = "red",
85 [IIO_MOD_LIGHT_GREEN
] = "green",
86 [IIO_MOD_LIGHT_BLUE
] = "blue",
89 /* relies on pairs of these shared then separate */
90 static const char * const iio_chan_info_postfix
[] = {
91 [IIO_CHAN_INFO_RAW
] = "raw",
92 [IIO_CHAN_INFO_PROCESSED
] = "input",
93 [IIO_CHAN_INFO_SCALE
] = "scale",
94 [IIO_CHAN_INFO_OFFSET
] = "offset",
95 [IIO_CHAN_INFO_CALIBSCALE
] = "calibscale",
96 [IIO_CHAN_INFO_CALIBBIAS
] = "calibbias",
97 [IIO_CHAN_INFO_PEAK
] = "peak_raw",
98 [IIO_CHAN_INFO_PEAK_SCALE
] = "peak_scale",
99 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW
] = "quadrature_correction_raw",
100 [IIO_CHAN_INFO_AVERAGE_RAW
] = "mean_raw",
101 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
]
102 = "filter_low_pass_3db_frequency",
103 [IIO_CHAN_INFO_SAMP_FREQ
] = "sampling_frequency",
104 [IIO_CHAN_INFO_FREQUENCY
] = "frequency",
105 [IIO_CHAN_INFO_PHASE
] = "phase",
106 [IIO_CHAN_INFO_HARDWAREGAIN
] = "hardwaregain",
107 [IIO_CHAN_INFO_HYSTERESIS
] = "hysteresis",
108 [IIO_CHAN_INFO_INT_TIME
] = "integration_time",
112 * iio_find_channel_from_si() - get channel from its scan index
114 * @si: scan index to match
116 const struct iio_chan_spec
117 *iio_find_channel_from_si(struct iio_dev
*indio_dev
, int si
)
121 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
122 if (indio_dev
->channels
[i
].scan_index
== si
)
123 return &indio_dev
->channels
[i
];
127 /* This turns up an awful lot */
128 ssize_t
iio_read_const_attr(struct device
*dev
,
129 struct device_attribute
*attr
,
132 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
134 EXPORT_SYMBOL(iio_read_const_attr
);
136 static int __init
iio_init(void)
140 /* Register sysfs bus */
141 ret
= bus_register(&iio_bus_type
);
143 pr_err("could not register bus type\n");
147 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
149 pr_err("failed to allocate char dev region\n");
150 goto error_unregister_bus_type
;
153 iio_debugfs_dentry
= debugfs_create_dir("iio", NULL
);
157 error_unregister_bus_type
:
158 bus_unregister(&iio_bus_type
);
163 static void __exit
iio_exit(void)
166 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
167 bus_unregister(&iio_bus_type
);
168 debugfs_remove(iio_debugfs_dentry
);
171 #if defined(CONFIG_DEBUG_FS)
172 static ssize_t
iio_debugfs_read_reg(struct file
*file
, char __user
*userbuf
,
173 size_t count
, loff_t
*ppos
)
175 struct iio_dev
*indio_dev
= file
->private_data
;
181 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
,
182 indio_dev
->cached_reg_addr
,
185 dev_err(indio_dev
->dev
.parent
, "%s: read failed\n", __func__
);
187 len
= snprintf(buf
, sizeof(buf
), "0x%X\n", val
);
189 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
192 static ssize_t
iio_debugfs_write_reg(struct file
*file
,
193 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
195 struct iio_dev
*indio_dev
= file
->private_data
;
200 count
= min_t(size_t, count
, (sizeof(buf
)-1));
201 if (copy_from_user(buf
, userbuf
, count
))
206 ret
= sscanf(buf
, "%i %i", ®
, &val
);
210 indio_dev
->cached_reg_addr
= reg
;
213 indio_dev
->cached_reg_addr
= reg
;
214 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
, reg
,
217 dev_err(indio_dev
->dev
.parent
, "%s: write failed\n",
229 static const struct file_operations iio_debugfs_reg_fops
= {
231 .read
= iio_debugfs_read_reg
,
232 .write
= iio_debugfs_write_reg
,
235 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
237 debugfs_remove_recursive(indio_dev
->debugfs_dentry
);
240 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
244 if (indio_dev
->info
->debugfs_reg_access
== NULL
)
247 if (!iio_debugfs_dentry
)
250 indio_dev
->debugfs_dentry
=
251 debugfs_create_dir(dev_name(&indio_dev
->dev
),
253 if (indio_dev
->debugfs_dentry
== NULL
) {
254 dev_warn(indio_dev
->dev
.parent
,
255 "Failed to create debugfs directory\n");
259 d
= debugfs_create_file("direct_reg_access", 0644,
260 indio_dev
->debugfs_dentry
,
261 indio_dev
, &iio_debugfs_reg_fops
);
263 iio_device_unregister_debugfs(indio_dev
);
270 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
275 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
278 #endif /* CONFIG_DEBUG_FS */
280 static ssize_t
iio_read_channel_ext_info(struct device
*dev
,
281 struct device_attribute
*attr
,
284 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
285 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
286 const struct iio_chan_spec_ext_info
*ext_info
;
288 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
290 return ext_info
->read(indio_dev
, ext_info
->private, this_attr
->c
, buf
);
293 static ssize_t
iio_write_channel_ext_info(struct device
*dev
,
294 struct device_attribute
*attr
,
298 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
299 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
300 const struct iio_chan_spec_ext_info
*ext_info
;
302 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
304 return ext_info
->write(indio_dev
, ext_info
->private,
305 this_attr
->c
, buf
, len
);
308 ssize_t
iio_enum_available_read(struct iio_dev
*indio_dev
,
309 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
311 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
318 for (i
= 0; i
< e
->num_items
; ++i
)
319 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%s ", e
->items
[i
]);
321 /* replace last space with a newline */
326 EXPORT_SYMBOL_GPL(iio_enum_available_read
);
328 ssize_t
iio_enum_read(struct iio_dev
*indio_dev
,
329 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
331 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
337 i
= e
->get(indio_dev
, chan
);
340 else if (i
>= e
->num_items
)
343 return sprintf(buf
, "%s\n", e
->items
[i
]);
345 EXPORT_SYMBOL_GPL(iio_enum_read
);
347 ssize_t
iio_enum_write(struct iio_dev
*indio_dev
,
348 uintptr_t priv
, const struct iio_chan_spec
*chan
, const char *buf
,
351 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
358 for (i
= 0; i
< e
->num_items
; i
++) {
359 if (sysfs_streq(buf
, e
->items
[i
]))
363 if (i
== e
->num_items
)
366 ret
= e
->set(indio_dev
, chan
, i
);
367 return ret
? ret
: len
;
369 EXPORT_SYMBOL_GPL(iio_enum_write
);
372 * iio_format_value() - Formats a IIO value into its string representation
373 * @buf: The buffer to which the formated value gets written
374 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
375 * parameters are formatted.
376 * @val: First part of the value, exact meaning depends on the type parameter.
377 * @val2: Second part of the value, exact meaning depends on the type parameter.
379 ssize_t
iio_format_value(char *buf
, unsigned int type
, int val
, int val2
)
381 unsigned long long tmp
;
382 bool scale_db
= false;
386 return sprintf(buf
, "%d\n", val
);
387 case IIO_VAL_INT_PLUS_MICRO_DB
:
389 case IIO_VAL_INT_PLUS_MICRO
:
391 return sprintf(buf
, "-%ld.%06u%s\n", abs(val
), -val2
,
392 scale_db
? " dB" : "");
394 return sprintf(buf
, "%d.%06u%s\n", val
, val2
,
395 scale_db
? " dB" : "");
396 case IIO_VAL_INT_PLUS_NANO
:
398 return sprintf(buf
, "-%ld.%09u\n", abs(val
), -val2
);
400 return sprintf(buf
, "%d.%09u\n", val
, val2
);
401 case IIO_VAL_FRACTIONAL
:
402 tmp
= div_s64((s64
)val
* 1000000000LL, val2
);
403 val2
= do_div(tmp
, 1000000000LL);
405 return sprintf(buf
, "%d.%09u\n", val
, val2
);
406 case IIO_VAL_FRACTIONAL_LOG2
:
407 tmp
= (s64
)val
* 1000000000LL >> val2
;
408 val2
= do_div(tmp
, 1000000000LL);
410 return sprintf(buf
, "%d.%09u\n", val
, val2
);
416 static ssize_t
iio_read_channel_info(struct device
*dev
,
417 struct device_attribute
*attr
,
420 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
421 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
423 int ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
424 &val
, &val2
, this_attr
->address
);
429 return iio_format_value(buf
, ret
, val
, val2
);
433 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
434 * @str: The string to parse
435 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
436 * @integer: The integer part of the number
437 * @fract: The fractional part of the number
439 * Returns 0 on success, or a negative error code if the string could not be
442 int iio_str_to_fixpoint(const char *str
, int fract_mult
,
443 int *integer
, int *fract
)
446 bool integer_part
= true, negative
= false;
451 } else if (str
[0] == '+') {
456 if ('0' <= *str
&& *str
<= '9') {
458 i
= i
* 10 + *str
- '0';
460 f
+= fract_mult
* (*str
- '0');
463 } else if (*str
== '\n') {
464 if (*(str
+ 1) == '\0')
468 } else if (*str
== '.' && integer_part
) {
469 integer_part
= false;
488 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint
);
490 static ssize_t
iio_write_channel_info(struct device
*dev
,
491 struct device_attribute
*attr
,
495 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
496 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
497 int ret
, fract_mult
= 100000;
500 /* Assumes decimal - precision based on number of digits */
501 if (!indio_dev
->info
->write_raw
)
504 if (indio_dev
->info
->write_raw_get_fmt
)
505 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
506 this_attr
->c
, this_attr
->address
)) {
507 case IIO_VAL_INT_PLUS_MICRO
:
510 case IIO_VAL_INT_PLUS_NANO
:
511 fract_mult
= 100000000;
517 ret
= iio_str_to_fixpoint(buf
, fract_mult
, &integer
, &fract
);
521 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
522 integer
, fract
, this_attr
->address
);
530 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
532 struct iio_chan_spec
const *chan
,
533 ssize_t (*readfunc
)(struct device
*dev
,
534 struct device_attribute
*attr
,
536 ssize_t (*writefunc
)(struct device
*dev
,
537 struct device_attribute
*attr
,
540 enum iio_shared_by shared_by
)
543 char *name_format
= NULL
;
545 sysfs_attr_init(&dev_attr
->attr
);
547 /* Build up postfix of <extend_name>_<modifier>_postfix */
548 if (chan
->modified
&& (shared_by
== IIO_SEPARATE
)) {
549 if (chan
->extend_name
)
550 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
551 iio_modifier_names
[chan
556 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s",
557 iio_modifier_names
[chan
561 if (chan
->extend_name
== NULL
)
562 full_postfix
= kstrdup(postfix
, GFP_KERNEL
);
564 full_postfix
= kasprintf(GFP_KERNEL
,
569 if (full_postfix
== NULL
)
572 if (chan
->differential
) { /* Differential can not have modifier */
574 case IIO_SHARED_BY_ALL
:
575 name_format
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
577 case IIO_SHARED_BY_DIR
:
578 name_format
= kasprintf(GFP_KERNEL
, "%s_%s",
579 iio_direction
[chan
->output
],
582 case IIO_SHARED_BY_TYPE
:
584 = kasprintf(GFP_KERNEL
, "%s_%s-%s_%s",
585 iio_direction
[chan
->output
],
586 iio_chan_type_name_spec
[chan
->type
],
587 iio_chan_type_name_spec
[chan
->type
],
591 if (!chan
->indexed
) {
592 WARN_ON("Differential channels must be indexed\n");
594 goto error_free_full_postfix
;
597 = kasprintf(GFP_KERNEL
,
599 iio_direction
[chan
->output
],
600 iio_chan_type_name_spec
[chan
->type
],
602 iio_chan_type_name_spec
[chan
->type
],
607 } else { /* Single ended */
609 case IIO_SHARED_BY_ALL
:
610 name_format
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
612 case IIO_SHARED_BY_DIR
:
613 name_format
= kasprintf(GFP_KERNEL
, "%s_%s",
614 iio_direction
[chan
->output
],
617 case IIO_SHARED_BY_TYPE
:
619 = kasprintf(GFP_KERNEL
, "%s_%s_%s",
620 iio_direction
[chan
->output
],
621 iio_chan_type_name_spec
[chan
->type
],
628 = kasprintf(GFP_KERNEL
, "%s_%s%d_%s",
629 iio_direction
[chan
->output
],
630 iio_chan_type_name_spec
[chan
->type
],
635 = kasprintf(GFP_KERNEL
, "%s_%s_%s",
636 iio_direction
[chan
->output
],
637 iio_chan_type_name_spec
[chan
->type
],
642 if (name_format
== NULL
) {
644 goto error_free_full_postfix
;
646 dev_attr
->attr
.name
= kasprintf(GFP_KERNEL
,
650 if (dev_attr
->attr
.name
== NULL
) {
652 goto error_free_name_format
;
656 dev_attr
->attr
.mode
|= S_IRUGO
;
657 dev_attr
->show
= readfunc
;
661 dev_attr
->attr
.mode
|= S_IWUSR
;
662 dev_attr
->store
= writefunc
;
664 error_free_name_format
:
666 error_free_full_postfix
:
672 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
674 kfree(dev_attr
->attr
.name
);
677 int __iio_add_chan_devattr(const char *postfix
,
678 struct iio_chan_spec
const *chan
,
679 ssize_t (*readfunc
)(struct device
*dev
,
680 struct device_attribute
*attr
,
682 ssize_t (*writefunc
)(struct device
*dev
,
683 struct device_attribute
*attr
,
687 enum iio_shared_by shared_by
,
689 struct list_head
*attr_list
)
692 struct iio_dev_attr
*iio_attr
, *t
;
694 iio_attr
= kzalloc(sizeof(*iio_attr
), GFP_KERNEL
);
695 if (iio_attr
== NULL
) {
699 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
701 readfunc
, writefunc
, shared_by
);
703 goto error_iio_dev_attr_free
;
705 iio_attr
->address
= mask
;
706 list_for_each_entry(t
, attr_list
, l
)
707 if (strcmp(t
->dev_attr
.attr
.name
,
708 iio_attr
->dev_attr
.attr
.name
) == 0) {
709 if (shared_by
== IIO_SEPARATE
)
710 dev_err(dev
, "tried to double register : %s\n",
711 t
->dev_attr
.attr
.name
);
713 goto error_device_attr_deinit
;
715 list_add(&iio_attr
->l
, attr_list
);
719 error_device_attr_deinit
:
720 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
721 error_iio_dev_attr_free
:
727 static int iio_device_add_info_mask_type(struct iio_dev
*indio_dev
,
728 struct iio_chan_spec
const *chan
,
729 enum iio_shared_by shared_by
,
730 const long *infomask
)
732 int i
, ret
, attrcount
= 0;
734 for_each_set_bit(i
, infomask
, sizeof(infomask
)*8) {
735 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
],
737 &iio_read_channel_info
,
738 &iio_write_channel_info
,
742 &indio_dev
->channel_attr_list
);
743 if ((ret
== -EBUSY
) && (shared_by
!= IIO_SEPARATE
))
753 static int iio_device_add_channel_sysfs(struct iio_dev
*indio_dev
,
754 struct iio_chan_spec
const *chan
)
756 int ret
, attrcount
= 0;
757 const struct iio_chan_spec_ext_info
*ext_info
;
759 if (chan
->channel
< 0)
761 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
763 &chan
->info_mask_separate
);
768 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
770 &chan
->info_mask_shared_by_type
);
775 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
777 &chan
->info_mask_shared_by_dir
);
782 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
784 &chan
->info_mask_shared_by_all
);
789 if (chan
->ext_info
) {
791 for (ext_info
= chan
->ext_info
; ext_info
->name
; ext_info
++) {
792 ret
= __iio_add_chan_devattr(ext_info
->name
,
795 &iio_read_channel_ext_info
: NULL
,
797 &iio_write_channel_ext_info
: NULL
,
801 &indio_dev
->channel_attr_list
);
803 if (ret
== -EBUSY
&& ext_info
->shared
)
817 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
818 * @attr_list: List of IIO device attributes
820 * This function frees the memory allocated for each of the IIO device
821 * attributes in the list. Note: if you want to reuse the list after calling
822 * this function you have to reinitialize it using INIT_LIST_HEAD().
824 void iio_free_chan_devattr_list(struct list_head
*attr_list
)
826 struct iio_dev_attr
*p
, *n
;
828 list_for_each_entry_safe(p
, n
, attr_list
, l
) {
829 kfree(p
->dev_attr
.attr
.name
);
834 static ssize_t
iio_show_dev_name(struct device
*dev
,
835 struct device_attribute
*attr
,
838 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
839 return sprintf(buf
, "%s\n", indio_dev
->name
);
842 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
844 static int iio_device_register_sysfs(struct iio_dev
*indio_dev
)
846 int i
, ret
= 0, attrcount
, attrn
, attrcount_orig
= 0;
847 struct iio_dev_attr
*p
;
848 struct attribute
**attr
;
850 /* First count elements in any existing group */
851 if (indio_dev
->info
->attrs
) {
852 attr
= indio_dev
->info
->attrs
->attrs
;
853 while (*attr
++ != NULL
)
856 attrcount
= attrcount_orig
;
858 * New channel registration method - relies on the fact a group does
859 * not need to be initialized if its name is NULL.
861 if (indio_dev
->channels
)
862 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
863 ret
= iio_device_add_channel_sysfs(indio_dev
,
867 goto error_clear_attrs
;
874 indio_dev
->chan_attr_group
.attrs
= kcalloc(attrcount
+ 1,
875 sizeof(indio_dev
->chan_attr_group
.attrs
[0]),
877 if (indio_dev
->chan_attr_group
.attrs
== NULL
) {
879 goto error_clear_attrs
;
881 /* Copy across original attributes */
882 if (indio_dev
->info
->attrs
)
883 memcpy(indio_dev
->chan_attr_group
.attrs
,
884 indio_dev
->info
->attrs
->attrs
,
885 sizeof(indio_dev
->chan_attr_group
.attrs
[0])
887 attrn
= attrcount_orig
;
888 /* Add all elements from the list. */
889 list_for_each_entry(p
, &indio_dev
->channel_attr_list
, l
)
890 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
892 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_name
.attr
;
894 indio_dev
->groups
[indio_dev
->groupcounter
++] =
895 &indio_dev
->chan_attr_group
;
900 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
905 static void iio_device_unregister_sysfs(struct iio_dev
*indio_dev
)
908 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
909 kfree(indio_dev
->chan_attr_group
.attrs
);
912 static void iio_dev_release(struct device
*device
)
914 struct iio_dev
*indio_dev
= dev_to_iio_dev(device
);
915 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
916 iio_device_unregister_trigger_consumer(indio_dev
);
917 iio_device_unregister_eventset(indio_dev
);
918 iio_device_unregister_sysfs(indio_dev
);
920 iio_buffer_put(indio_dev
->buffer
);
922 ida_simple_remove(&iio_ida
, indio_dev
->id
);
926 struct device_type iio_device_type
= {
927 .name
= "iio_device",
928 .release
= iio_dev_release
,
932 * iio_device_alloc() - allocate an iio_dev from a driver
933 * @sizeof_priv: Space to allocate for private structure.
935 struct iio_dev
*iio_device_alloc(int sizeof_priv
)
940 alloc_size
= sizeof(struct iio_dev
);
942 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
943 alloc_size
+= sizeof_priv
;
945 /* ensure 32-byte alignment of whole construct ? */
946 alloc_size
+= IIO_ALIGN
- 1;
948 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
951 dev
->dev
.groups
= dev
->groups
;
952 dev
->dev
.type
= &iio_device_type
;
953 dev
->dev
.bus
= &iio_bus_type
;
954 device_initialize(&dev
->dev
);
955 dev_set_drvdata(&dev
->dev
, (void *)dev
);
956 mutex_init(&dev
->mlock
);
957 mutex_init(&dev
->info_exist_lock
);
958 INIT_LIST_HEAD(&dev
->channel_attr_list
);
960 dev
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
962 /* cannot use a dev_err as the name isn't available */
963 pr_err("failed to get device id\n");
967 dev_set_name(&dev
->dev
, "iio:device%d", dev
->id
);
968 INIT_LIST_HEAD(&dev
->buffer_list
);
973 EXPORT_SYMBOL(iio_device_alloc
);
976 * iio_device_free() - free an iio_dev from a driver
977 * @dev: the iio_dev associated with the device
979 void iio_device_free(struct iio_dev
*dev
)
982 put_device(&dev
->dev
);
984 EXPORT_SYMBOL(iio_device_free
);
986 static void devm_iio_device_release(struct device
*dev
, void *res
)
988 iio_device_free(*(struct iio_dev
**)res
);
991 static int devm_iio_device_match(struct device
*dev
, void *res
, void *data
)
993 struct iio_dev
**r
= res
;
1002 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1003 * @dev: Device to allocate iio_dev for
1004 * @sizeof_priv: Space to allocate for private structure.
1006 * Managed iio_device_alloc. iio_dev allocated with this function is
1007 * automatically freed on driver detach.
1009 * If an iio_dev allocated with this function needs to be freed separately,
1010 * devm_iio_device_free() must be used.
1013 * Pointer to allocated iio_dev on success, NULL on failure.
1015 struct iio_dev
*devm_iio_device_alloc(struct device
*dev
, int sizeof_priv
)
1017 struct iio_dev
**ptr
, *iio_dev
;
1019 ptr
= devres_alloc(devm_iio_device_release
, sizeof(*ptr
),
1024 /* use raw alloc_dr for kmalloc caller tracing */
1025 iio_dev
= iio_device_alloc(sizeof_priv
);
1028 devres_add(dev
, ptr
);
1035 EXPORT_SYMBOL_GPL(devm_iio_device_alloc
);
1038 * devm_iio_device_free - Resource-managed iio_device_free()
1039 * @dev: Device this iio_dev belongs to
1040 * @iio_dev: the iio_dev associated with the device
1042 * Free iio_dev allocated with devm_iio_device_alloc().
1044 void devm_iio_device_free(struct device
*dev
, struct iio_dev
*iio_dev
)
1048 rc
= devres_release(dev
, devm_iio_device_release
,
1049 devm_iio_device_match
, iio_dev
);
1052 EXPORT_SYMBOL_GPL(devm_iio_device_free
);
1055 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1057 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1059 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1060 struct iio_dev
, chrdev
);
1062 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
))
1065 iio_device_get(indio_dev
);
1067 filp
->private_data
= indio_dev
;
1073 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1075 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1077 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1078 struct iio_dev
, chrdev
);
1079 clear_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
);
1080 iio_device_put(indio_dev
);
1085 /* Somewhat of a cross file organization violation - ioctls here are actually
1087 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1089 struct iio_dev
*indio_dev
= filp
->private_data
;
1090 int __user
*ip
= (int __user
*)arg
;
1093 if (!indio_dev
->info
)
1096 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1097 fd
= iio_event_getfd(indio_dev
);
1098 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1105 static const struct file_operations iio_buffer_fileops
= {
1106 .read
= iio_buffer_read_first_n_outer_addr
,
1107 .release
= iio_chrdev_release
,
1108 .open
= iio_chrdev_open
,
1109 .poll
= iio_buffer_poll_addr
,
1110 .owner
= THIS_MODULE
,
1111 .llseek
= noop_llseek
,
1112 .unlocked_ioctl
= iio_ioctl
,
1113 .compat_ioctl
= iio_ioctl
,
1116 static const struct iio_buffer_setup_ops noop_ring_setup_ops
;
1119 * iio_device_register() - register a device with the IIO subsystem
1120 * @indio_dev: Device structure filled by the device driver
1122 int iio_device_register(struct iio_dev
*indio_dev
)
1126 /* If the calling driver did not initialize of_node, do it here */
1127 if (!indio_dev
->dev
.of_node
&& indio_dev
->dev
.parent
)
1128 indio_dev
->dev
.of_node
= indio_dev
->dev
.parent
->of_node
;
1130 /* configure elements for the chrdev */
1131 indio_dev
->dev
.devt
= MKDEV(MAJOR(iio_devt
), indio_dev
->id
);
1133 ret
= iio_device_register_debugfs(indio_dev
);
1135 dev_err(indio_dev
->dev
.parent
,
1136 "Failed to register debugfs interfaces\n");
1139 ret
= iio_device_register_sysfs(indio_dev
);
1141 dev_err(indio_dev
->dev
.parent
,
1142 "Failed to register sysfs interfaces\n");
1143 goto error_unreg_debugfs
;
1145 ret
= iio_device_register_eventset(indio_dev
);
1147 dev_err(indio_dev
->dev
.parent
,
1148 "Failed to register event set\n");
1149 goto error_free_sysfs
;
1151 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
1152 iio_device_register_trigger_consumer(indio_dev
);
1154 if ((indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
) &&
1155 indio_dev
->setup_ops
== NULL
)
1156 indio_dev
->setup_ops
= &noop_ring_setup_ops
;
1158 cdev_init(&indio_dev
->chrdev
, &iio_buffer_fileops
);
1159 indio_dev
->chrdev
.owner
= indio_dev
->info
->driver_module
;
1160 indio_dev
->chrdev
.kobj
.parent
= &indio_dev
->dev
.kobj
;
1161 ret
= cdev_add(&indio_dev
->chrdev
, indio_dev
->dev
.devt
, 1);
1163 goto error_unreg_eventset
;
1165 ret
= device_add(&indio_dev
->dev
);
1167 goto error_cdev_del
;
1171 cdev_del(&indio_dev
->chrdev
);
1172 error_unreg_eventset
:
1173 iio_device_unregister_eventset(indio_dev
);
1175 iio_device_unregister_sysfs(indio_dev
);
1176 error_unreg_debugfs
:
1177 iio_device_unregister_debugfs(indio_dev
);
1181 EXPORT_SYMBOL(iio_device_register
);
1184 * iio_device_unregister() - unregister a device from the IIO subsystem
1185 * @indio_dev: Device structure representing the device.
1187 void iio_device_unregister(struct iio_dev
*indio_dev
)
1189 mutex_lock(&indio_dev
->info_exist_lock
);
1191 device_del(&indio_dev
->dev
);
1193 if (indio_dev
->chrdev
.dev
)
1194 cdev_del(&indio_dev
->chrdev
);
1195 iio_device_unregister_debugfs(indio_dev
);
1197 iio_disable_all_buffers(indio_dev
);
1199 indio_dev
->info
= NULL
;
1201 iio_device_wakeup_eventset(indio_dev
);
1202 iio_buffer_wakeup_poll(indio_dev
);
1204 mutex_unlock(&indio_dev
->info_exist_lock
);
1206 EXPORT_SYMBOL(iio_device_unregister
);
1208 static void devm_iio_device_unreg(struct device
*dev
, void *res
)
1210 iio_device_unregister(*(struct iio_dev
**)res
);
1214 * devm_iio_device_register - Resource-managed iio_device_register()
1215 * @dev: Device to allocate iio_dev for
1216 * @indio_dev: Device structure filled by the device driver
1218 * Managed iio_device_register. The IIO device registered with this
1219 * function is automatically unregistered on driver detach. This function
1220 * calls iio_device_register() internally. Refer to that function for more
1223 * If an iio_dev registered with this function needs to be unregistered
1224 * separately, devm_iio_device_unregister() must be used.
1227 * 0 on success, negative error number on failure.
1229 int devm_iio_device_register(struct device
*dev
, struct iio_dev
*indio_dev
)
1231 struct iio_dev
**ptr
;
1234 ptr
= devres_alloc(devm_iio_device_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1239 ret
= iio_device_register(indio_dev
);
1241 devres_add(dev
, ptr
);
1247 EXPORT_SYMBOL_GPL(devm_iio_device_register
);
1250 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1251 * @dev: Device this iio_dev belongs to
1252 * @indio_dev: the iio_dev associated with the device
1254 * Unregister iio_dev registered with devm_iio_device_register().
1256 void devm_iio_device_unregister(struct device
*dev
, struct iio_dev
*indio_dev
)
1260 rc
= devres_release(dev
, devm_iio_device_unreg
,
1261 devm_iio_device_match
, indio_dev
);
1264 EXPORT_SYMBOL_GPL(devm_iio_device_unregister
);
1266 subsys_initcall(iio_init
);
1267 module_exit(iio_exit
);
1269 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1270 MODULE_DESCRIPTION("Industrial I/O core");
1271 MODULE_LICENSE("GPL");