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",
87 [IIO_MOD_QUATERNION
] = "quaternion",
88 [IIO_MOD_TEMP_AMBIENT
] = "ambient",
89 [IIO_MOD_TEMP_OBJECT
] = "object",
92 /* relies on pairs of these shared then separate */
93 static const char * const iio_chan_info_postfix
[] = {
94 [IIO_CHAN_INFO_RAW
] = "raw",
95 [IIO_CHAN_INFO_PROCESSED
] = "input",
96 [IIO_CHAN_INFO_SCALE
] = "scale",
97 [IIO_CHAN_INFO_OFFSET
] = "offset",
98 [IIO_CHAN_INFO_CALIBSCALE
] = "calibscale",
99 [IIO_CHAN_INFO_CALIBBIAS
] = "calibbias",
100 [IIO_CHAN_INFO_PEAK
] = "peak_raw",
101 [IIO_CHAN_INFO_PEAK_SCALE
] = "peak_scale",
102 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW
] = "quadrature_correction_raw",
103 [IIO_CHAN_INFO_AVERAGE_RAW
] = "mean_raw",
104 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
]
105 = "filter_low_pass_3db_frequency",
106 [IIO_CHAN_INFO_SAMP_FREQ
] = "sampling_frequency",
107 [IIO_CHAN_INFO_FREQUENCY
] = "frequency",
108 [IIO_CHAN_INFO_PHASE
] = "phase",
109 [IIO_CHAN_INFO_HARDWAREGAIN
] = "hardwaregain",
110 [IIO_CHAN_INFO_HYSTERESIS
] = "hysteresis",
111 [IIO_CHAN_INFO_INT_TIME
] = "integration_time",
115 * iio_find_channel_from_si() - get channel from its scan index
117 * @si: scan index to match
119 const struct iio_chan_spec
120 *iio_find_channel_from_si(struct iio_dev
*indio_dev
, int si
)
124 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
125 if (indio_dev
->channels
[i
].scan_index
== si
)
126 return &indio_dev
->channels
[i
];
130 /* This turns up an awful lot */
131 ssize_t
iio_read_const_attr(struct device
*dev
,
132 struct device_attribute
*attr
,
135 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
137 EXPORT_SYMBOL(iio_read_const_attr
);
139 static int __init
iio_init(void)
143 /* Register sysfs bus */
144 ret
= bus_register(&iio_bus_type
);
146 pr_err("could not register bus type\n");
150 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
152 pr_err("failed to allocate char dev region\n");
153 goto error_unregister_bus_type
;
156 iio_debugfs_dentry
= debugfs_create_dir("iio", NULL
);
160 error_unregister_bus_type
:
161 bus_unregister(&iio_bus_type
);
166 static void __exit
iio_exit(void)
169 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
170 bus_unregister(&iio_bus_type
);
171 debugfs_remove(iio_debugfs_dentry
);
174 #if defined(CONFIG_DEBUG_FS)
175 static ssize_t
iio_debugfs_read_reg(struct file
*file
, char __user
*userbuf
,
176 size_t count
, loff_t
*ppos
)
178 struct iio_dev
*indio_dev
= file
->private_data
;
184 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
,
185 indio_dev
->cached_reg_addr
,
188 dev_err(indio_dev
->dev
.parent
, "%s: read failed\n", __func__
);
190 len
= snprintf(buf
, sizeof(buf
), "0x%X\n", val
);
192 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
195 static ssize_t
iio_debugfs_write_reg(struct file
*file
,
196 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
198 struct iio_dev
*indio_dev
= file
->private_data
;
203 count
= min_t(size_t, count
, (sizeof(buf
)-1));
204 if (copy_from_user(buf
, userbuf
, count
))
209 ret
= sscanf(buf
, "%i %i", ®
, &val
);
213 indio_dev
->cached_reg_addr
= reg
;
216 indio_dev
->cached_reg_addr
= reg
;
217 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
, reg
,
220 dev_err(indio_dev
->dev
.parent
, "%s: write failed\n",
232 static const struct file_operations iio_debugfs_reg_fops
= {
234 .read
= iio_debugfs_read_reg
,
235 .write
= iio_debugfs_write_reg
,
238 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
240 debugfs_remove_recursive(indio_dev
->debugfs_dentry
);
243 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
247 if (indio_dev
->info
->debugfs_reg_access
== NULL
)
250 if (!iio_debugfs_dentry
)
253 indio_dev
->debugfs_dentry
=
254 debugfs_create_dir(dev_name(&indio_dev
->dev
),
256 if (indio_dev
->debugfs_dentry
== NULL
) {
257 dev_warn(indio_dev
->dev
.parent
,
258 "Failed to create debugfs directory\n");
262 d
= debugfs_create_file("direct_reg_access", 0644,
263 indio_dev
->debugfs_dentry
,
264 indio_dev
, &iio_debugfs_reg_fops
);
266 iio_device_unregister_debugfs(indio_dev
);
273 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
278 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
281 #endif /* CONFIG_DEBUG_FS */
283 static ssize_t
iio_read_channel_ext_info(struct device
*dev
,
284 struct device_attribute
*attr
,
287 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
288 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
289 const struct iio_chan_spec_ext_info
*ext_info
;
291 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
293 return ext_info
->read(indio_dev
, ext_info
->private, this_attr
->c
, buf
);
296 static ssize_t
iio_write_channel_ext_info(struct device
*dev
,
297 struct device_attribute
*attr
,
301 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
302 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
303 const struct iio_chan_spec_ext_info
*ext_info
;
305 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
307 return ext_info
->write(indio_dev
, ext_info
->private,
308 this_attr
->c
, buf
, len
);
311 ssize_t
iio_enum_available_read(struct iio_dev
*indio_dev
,
312 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
314 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
321 for (i
= 0; i
< e
->num_items
; ++i
)
322 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%s ", e
->items
[i
]);
324 /* replace last space with a newline */
329 EXPORT_SYMBOL_GPL(iio_enum_available_read
);
331 ssize_t
iio_enum_read(struct iio_dev
*indio_dev
,
332 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
334 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
340 i
= e
->get(indio_dev
, chan
);
343 else if (i
>= e
->num_items
)
346 return snprintf(buf
, PAGE_SIZE
, "%s\n", e
->items
[i
]);
348 EXPORT_SYMBOL_GPL(iio_enum_read
);
350 ssize_t
iio_enum_write(struct iio_dev
*indio_dev
,
351 uintptr_t priv
, const struct iio_chan_spec
*chan
, const char *buf
,
354 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
361 for (i
= 0; i
< e
->num_items
; i
++) {
362 if (sysfs_streq(buf
, e
->items
[i
]))
366 if (i
== e
->num_items
)
369 ret
= e
->set(indio_dev
, chan
, i
);
370 return ret
? ret
: len
;
372 EXPORT_SYMBOL_GPL(iio_enum_write
);
375 * iio_format_value() - Formats a IIO value into its string representation
376 * @buf: The buffer to which the formated value gets written
377 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
378 * parameters are formatted.
379 * @vals: pointer to the values, exact meaning depends on the type parameter.
381 ssize_t
iio_format_value(char *buf
, unsigned int type
, int size
, int *vals
)
383 unsigned long long tmp
;
384 bool scale_db
= false;
388 return sprintf(buf
, "%d\n", vals
[0]);
389 case IIO_VAL_INT_PLUS_MICRO_DB
:
391 case IIO_VAL_INT_PLUS_MICRO
:
393 return sprintf(buf
, "-%ld.%06u%s\n", abs(vals
[0]),
395 scale_db
? " dB" : "");
397 return sprintf(buf
, "%d.%06u%s\n", vals
[0], vals
[1],
398 scale_db
? " dB" : "");
399 case IIO_VAL_INT_PLUS_NANO
:
401 return sprintf(buf
, "-%ld.%09u\n", abs(vals
[0]),
404 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
405 case IIO_VAL_FRACTIONAL
:
406 tmp
= div_s64((s64
)vals
[0] * 1000000000LL, vals
[1]);
407 vals
[1] = do_div(tmp
, 1000000000LL);
409 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
410 case IIO_VAL_FRACTIONAL_LOG2
:
411 tmp
= (s64
)vals
[0] * 1000000000LL >> vals
[1];
412 vals
[1] = do_div(tmp
, 1000000000LL);
414 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
415 case IIO_VAL_INT_MULTIPLE
:
420 for (i
= 0; i
< size
; ++i
)
421 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "%d ",
423 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "\n");
431 static ssize_t
iio_read_channel_info(struct device
*dev
,
432 struct device_attribute
*attr
,
435 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
436 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
437 int vals
[INDIO_MAX_RAW_ELEMENTS
];
441 if (indio_dev
->info
->read_raw_multi
)
442 ret
= indio_dev
->info
->read_raw_multi(indio_dev
, this_attr
->c
,
443 INDIO_MAX_RAW_ELEMENTS
,
447 ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
448 &vals
[0], &vals
[1], this_attr
->address
);
453 return iio_format_value(buf
, ret
, val_len
, vals
);
457 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
458 * @str: The string to parse
459 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
460 * @integer: The integer part of the number
461 * @fract: The fractional part of the number
463 * Returns 0 on success, or a negative error code if the string could not be
466 int iio_str_to_fixpoint(const char *str
, int fract_mult
,
467 int *integer
, int *fract
)
470 bool integer_part
= true, negative
= false;
475 } else if (str
[0] == '+') {
480 if ('0' <= *str
&& *str
<= '9') {
482 i
= i
* 10 + *str
- '0';
484 f
+= fract_mult
* (*str
- '0');
487 } else if (*str
== '\n') {
488 if (*(str
+ 1) == '\0')
492 } else if (*str
== '.' && integer_part
) {
493 integer_part
= false;
512 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint
);
514 static ssize_t
iio_write_channel_info(struct device
*dev
,
515 struct device_attribute
*attr
,
519 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
520 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
521 int ret
, fract_mult
= 100000;
524 /* Assumes decimal - precision based on number of digits */
525 if (!indio_dev
->info
->write_raw
)
528 if (indio_dev
->info
->write_raw_get_fmt
)
529 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
530 this_attr
->c
, this_attr
->address
)) {
531 case IIO_VAL_INT_PLUS_MICRO
:
534 case IIO_VAL_INT_PLUS_NANO
:
535 fract_mult
= 100000000;
541 ret
= iio_str_to_fixpoint(buf
, fract_mult
, &integer
, &fract
);
545 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
546 integer
, fract
, this_attr
->address
);
554 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
556 struct iio_chan_spec
const *chan
,
557 ssize_t (*readfunc
)(struct device
*dev
,
558 struct device_attribute
*attr
,
560 ssize_t (*writefunc
)(struct device
*dev
,
561 struct device_attribute
*attr
,
564 enum iio_shared_by shared_by
)
569 sysfs_attr_init(&dev_attr
->attr
);
571 /* Build up postfix of <extend_name>_<modifier>_postfix */
572 if (chan
->modified
&& (shared_by
== IIO_SEPARATE
)) {
573 if (chan
->extend_name
)
574 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
575 iio_modifier_names
[chan
580 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s",
581 iio_modifier_names
[chan
585 if (chan
->extend_name
== NULL
|| shared_by
!= IIO_SEPARATE
)
586 full_postfix
= kstrdup(postfix
, GFP_KERNEL
);
588 full_postfix
= kasprintf(GFP_KERNEL
,
593 if (full_postfix
== NULL
)
596 if (chan
->differential
) { /* Differential can not have modifier */
598 case IIO_SHARED_BY_ALL
:
599 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
601 case IIO_SHARED_BY_DIR
:
602 name
= kasprintf(GFP_KERNEL
, "%s_%s",
603 iio_direction
[chan
->output
],
606 case IIO_SHARED_BY_TYPE
:
607 name
= kasprintf(GFP_KERNEL
, "%s_%s-%s_%s",
608 iio_direction
[chan
->output
],
609 iio_chan_type_name_spec
[chan
->type
],
610 iio_chan_type_name_spec
[chan
->type
],
614 if (!chan
->indexed
) {
615 WARN_ON("Differential channels must be indexed\n");
617 goto error_free_full_postfix
;
619 name
= kasprintf(GFP_KERNEL
,
621 iio_direction
[chan
->output
],
622 iio_chan_type_name_spec
[chan
->type
],
624 iio_chan_type_name_spec
[chan
->type
],
629 } else { /* Single ended */
631 case IIO_SHARED_BY_ALL
:
632 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
634 case IIO_SHARED_BY_DIR
:
635 name
= kasprintf(GFP_KERNEL
, "%s_%s",
636 iio_direction
[chan
->output
],
639 case IIO_SHARED_BY_TYPE
:
640 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
641 iio_direction
[chan
->output
],
642 iio_chan_type_name_spec
[chan
->type
],
648 name
= kasprintf(GFP_KERNEL
, "%s_%s%d_%s",
649 iio_direction
[chan
->output
],
650 iio_chan_type_name_spec
[chan
->type
],
654 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
655 iio_direction
[chan
->output
],
656 iio_chan_type_name_spec
[chan
->type
],
663 goto error_free_full_postfix
;
665 dev_attr
->attr
.name
= name
;
668 dev_attr
->attr
.mode
|= S_IRUGO
;
669 dev_attr
->show
= readfunc
;
673 dev_attr
->attr
.mode
|= S_IWUSR
;
674 dev_attr
->store
= writefunc
;
677 error_free_full_postfix
:
683 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
685 kfree(dev_attr
->attr
.name
);
688 int __iio_add_chan_devattr(const char *postfix
,
689 struct iio_chan_spec
const *chan
,
690 ssize_t (*readfunc
)(struct device
*dev
,
691 struct device_attribute
*attr
,
693 ssize_t (*writefunc
)(struct device
*dev
,
694 struct device_attribute
*attr
,
698 enum iio_shared_by shared_by
,
700 struct list_head
*attr_list
)
703 struct iio_dev_attr
*iio_attr
, *t
;
705 iio_attr
= kzalloc(sizeof(*iio_attr
), GFP_KERNEL
);
706 if (iio_attr
== NULL
)
708 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
710 readfunc
, writefunc
, shared_by
);
712 goto error_iio_dev_attr_free
;
714 iio_attr
->address
= mask
;
715 list_for_each_entry(t
, attr_list
, l
)
716 if (strcmp(t
->dev_attr
.attr
.name
,
717 iio_attr
->dev_attr
.attr
.name
) == 0) {
718 if (shared_by
== IIO_SEPARATE
)
719 dev_err(dev
, "tried to double register : %s\n",
720 t
->dev_attr
.attr
.name
);
722 goto error_device_attr_deinit
;
724 list_add(&iio_attr
->l
, attr_list
);
728 error_device_attr_deinit
:
729 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
730 error_iio_dev_attr_free
:
735 static int iio_device_add_info_mask_type(struct iio_dev
*indio_dev
,
736 struct iio_chan_spec
const *chan
,
737 enum iio_shared_by shared_by
,
738 const long *infomask
)
740 int i
, ret
, attrcount
= 0;
742 for_each_set_bit(i
, infomask
, sizeof(infomask
)*8) {
743 if (i
>= ARRAY_SIZE(iio_chan_info_postfix
))
745 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
],
747 &iio_read_channel_info
,
748 &iio_write_channel_info
,
752 &indio_dev
->channel_attr_list
);
753 if ((ret
== -EBUSY
) && (shared_by
!= IIO_SEPARATE
))
763 static int iio_device_add_channel_sysfs(struct iio_dev
*indio_dev
,
764 struct iio_chan_spec
const *chan
)
766 int ret
, attrcount
= 0;
767 const struct iio_chan_spec_ext_info
*ext_info
;
769 if (chan
->channel
< 0)
771 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
773 &chan
->info_mask_separate
);
778 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
780 &chan
->info_mask_shared_by_type
);
785 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
787 &chan
->info_mask_shared_by_dir
);
792 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
794 &chan
->info_mask_shared_by_all
);
799 if (chan
->ext_info
) {
801 for (ext_info
= chan
->ext_info
; ext_info
->name
; ext_info
++) {
802 ret
= __iio_add_chan_devattr(ext_info
->name
,
805 &iio_read_channel_ext_info
: NULL
,
807 &iio_write_channel_ext_info
: NULL
,
811 &indio_dev
->channel_attr_list
);
813 if (ret
== -EBUSY
&& ext_info
->shared
)
827 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
828 * @attr_list: List of IIO device attributes
830 * This function frees the memory allocated for each of the IIO device
831 * attributes in the list. Note: if you want to reuse the list after calling
832 * this function you have to reinitialize it using INIT_LIST_HEAD().
834 void iio_free_chan_devattr_list(struct list_head
*attr_list
)
836 struct iio_dev_attr
*p
, *n
;
838 list_for_each_entry_safe(p
, n
, attr_list
, l
) {
839 kfree(p
->dev_attr
.attr
.name
);
844 static ssize_t
iio_show_dev_name(struct device
*dev
,
845 struct device_attribute
*attr
,
848 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
849 return snprintf(buf
, PAGE_SIZE
, "%s\n", indio_dev
->name
);
852 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
854 static int iio_device_register_sysfs(struct iio_dev
*indio_dev
)
856 int i
, ret
= 0, attrcount
, attrn
, attrcount_orig
= 0;
857 struct iio_dev_attr
*p
;
858 struct attribute
**attr
;
860 /* First count elements in any existing group */
861 if (indio_dev
->info
->attrs
) {
862 attr
= indio_dev
->info
->attrs
->attrs
;
863 while (*attr
++ != NULL
)
866 attrcount
= attrcount_orig
;
868 * New channel registration method - relies on the fact a group does
869 * not need to be initialized if its name is NULL.
871 if (indio_dev
->channels
)
872 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
873 ret
= iio_device_add_channel_sysfs(indio_dev
,
877 goto error_clear_attrs
;
884 indio_dev
->chan_attr_group
.attrs
= kcalloc(attrcount
+ 1,
885 sizeof(indio_dev
->chan_attr_group
.attrs
[0]),
887 if (indio_dev
->chan_attr_group
.attrs
== NULL
) {
889 goto error_clear_attrs
;
891 /* Copy across original attributes */
892 if (indio_dev
->info
->attrs
)
893 memcpy(indio_dev
->chan_attr_group
.attrs
,
894 indio_dev
->info
->attrs
->attrs
,
895 sizeof(indio_dev
->chan_attr_group
.attrs
[0])
897 attrn
= attrcount_orig
;
898 /* Add all elements from the list. */
899 list_for_each_entry(p
, &indio_dev
->channel_attr_list
, l
)
900 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
902 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_name
.attr
;
904 indio_dev
->groups
[indio_dev
->groupcounter
++] =
905 &indio_dev
->chan_attr_group
;
910 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
915 static void iio_device_unregister_sysfs(struct iio_dev
*indio_dev
)
918 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
919 kfree(indio_dev
->chan_attr_group
.attrs
);
922 static void iio_dev_release(struct device
*device
)
924 struct iio_dev
*indio_dev
= dev_to_iio_dev(device
);
925 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
926 iio_device_unregister_trigger_consumer(indio_dev
);
927 iio_device_unregister_eventset(indio_dev
);
928 iio_device_unregister_sysfs(indio_dev
);
930 iio_buffer_put(indio_dev
->buffer
);
932 ida_simple_remove(&iio_ida
, indio_dev
->id
);
936 struct device_type iio_device_type
= {
937 .name
= "iio_device",
938 .release
= iio_dev_release
,
942 * iio_device_alloc() - allocate an iio_dev from a driver
943 * @sizeof_priv: Space to allocate for private structure.
945 struct iio_dev
*iio_device_alloc(int sizeof_priv
)
950 alloc_size
= sizeof(struct iio_dev
);
952 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
953 alloc_size
+= sizeof_priv
;
955 /* ensure 32-byte alignment of whole construct ? */
956 alloc_size
+= IIO_ALIGN
- 1;
958 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
961 dev
->dev
.groups
= dev
->groups
;
962 dev
->dev
.type
= &iio_device_type
;
963 dev
->dev
.bus
= &iio_bus_type
;
964 device_initialize(&dev
->dev
);
965 dev_set_drvdata(&dev
->dev
, (void *)dev
);
966 mutex_init(&dev
->mlock
);
967 mutex_init(&dev
->info_exist_lock
);
968 INIT_LIST_HEAD(&dev
->channel_attr_list
);
970 dev
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
972 /* cannot use a dev_err as the name isn't available */
973 pr_err("failed to get device id\n");
977 dev_set_name(&dev
->dev
, "iio:device%d", dev
->id
);
978 INIT_LIST_HEAD(&dev
->buffer_list
);
983 EXPORT_SYMBOL(iio_device_alloc
);
986 * iio_device_free() - free an iio_dev from a driver
987 * @dev: the iio_dev associated with the device
989 void iio_device_free(struct iio_dev
*dev
)
992 put_device(&dev
->dev
);
994 EXPORT_SYMBOL(iio_device_free
);
996 static void devm_iio_device_release(struct device
*dev
, void *res
)
998 iio_device_free(*(struct iio_dev
**)res
);
1001 static int devm_iio_device_match(struct device
*dev
, void *res
, void *data
)
1003 struct iio_dev
**r
= res
;
1012 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1013 * @dev: Device to allocate iio_dev for
1014 * @sizeof_priv: Space to allocate for private structure.
1016 * Managed iio_device_alloc. iio_dev allocated with this function is
1017 * automatically freed on driver detach.
1019 * If an iio_dev allocated with this function needs to be freed separately,
1020 * devm_iio_device_free() must be used.
1023 * Pointer to allocated iio_dev on success, NULL on failure.
1025 struct iio_dev
*devm_iio_device_alloc(struct device
*dev
, int sizeof_priv
)
1027 struct iio_dev
**ptr
, *iio_dev
;
1029 ptr
= devres_alloc(devm_iio_device_release
, sizeof(*ptr
),
1034 /* use raw alloc_dr for kmalloc caller tracing */
1035 iio_dev
= iio_device_alloc(sizeof_priv
);
1038 devres_add(dev
, ptr
);
1045 EXPORT_SYMBOL_GPL(devm_iio_device_alloc
);
1048 * devm_iio_device_free - Resource-managed iio_device_free()
1049 * @dev: Device this iio_dev belongs to
1050 * @iio_dev: the iio_dev associated with the device
1052 * Free iio_dev allocated with devm_iio_device_alloc().
1054 void devm_iio_device_free(struct device
*dev
, struct iio_dev
*iio_dev
)
1058 rc
= devres_release(dev
, devm_iio_device_release
,
1059 devm_iio_device_match
, iio_dev
);
1062 EXPORT_SYMBOL_GPL(devm_iio_device_free
);
1065 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1067 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1069 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1070 struct iio_dev
, chrdev
);
1072 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
))
1075 iio_device_get(indio_dev
);
1077 filp
->private_data
= indio_dev
;
1083 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1085 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1087 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1088 struct iio_dev
, chrdev
);
1089 clear_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
);
1090 iio_device_put(indio_dev
);
1095 /* Somewhat of a cross file organization violation - ioctls here are actually
1097 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1099 struct iio_dev
*indio_dev
= filp
->private_data
;
1100 int __user
*ip
= (int __user
*)arg
;
1103 if (!indio_dev
->info
)
1106 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1107 fd
= iio_event_getfd(indio_dev
);
1108 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1115 static const struct file_operations iio_buffer_fileops
= {
1116 .read
= iio_buffer_read_first_n_outer_addr
,
1117 .release
= iio_chrdev_release
,
1118 .open
= iio_chrdev_open
,
1119 .poll
= iio_buffer_poll_addr
,
1120 .owner
= THIS_MODULE
,
1121 .llseek
= noop_llseek
,
1122 .unlocked_ioctl
= iio_ioctl
,
1123 .compat_ioctl
= iio_ioctl
,
1126 static const struct iio_buffer_setup_ops noop_ring_setup_ops
;
1129 * iio_device_register() - register a device with the IIO subsystem
1130 * @indio_dev: Device structure filled by the device driver
1132 int iio_device_register(struct iio_dev
*indio_dev
)
1136 /* If the calling driver did not initialize of_node, do it here */
1137 if (!indio_dev
->dev
.of_node
&& indio_dev
->dev
.parent
)
1138 indio_dev
->dev
.of_node
= indio_dev
->dev
.parent
->of_node
;
1140 /* configure elements for the chrdev */
1141 indio_dev
->dev
.devt
= MKDEV(MAJOR(iio_devt
), indio_dev
->id
);
1143 ret
= iio_device_register_debugfs(indio_dev
);
1145 dev_err(indio_dev
->dev
.parent
,
1146 "Failed to register debugfs interfaces\n");
1149 ret
= iio_device_register_sysfs(indio_dev
);
1151 dev_err(indio_dev
->dev
.parent
,
1152 "Failed to register sysfs interfaces\n");
1153 goto error_unreg_debugfs
;
1155 ret
= iio_device_register_eventset(indio_dev
);
1157 dev_err(indio_dev
->dev
.parent
,
1158 "Failed to register event set\n");
1159 goto error_free_sysfs
;
1161 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
1162 iio_device_register_trigger_consumer(indio_dev
);
1164 if ((indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
) &&
1165 indio_dev
->setup_ops
== NULL
)
1166 indio_dev
->setup_ops
= &noop_ring_setup_ops
;
1168 cdev_init(&indio_dev
->chrdev
, &iio_buffer_fileops
);
1169 indio_dev
->chrdev
.owner
= indio_dev
->info
->driver_module
;
1170 indio_dev
->chrdev
.kobj
.parent
= &indio_dev
->dev
.kobj
;
1171 ret
= cdev_add(&indio_dev
->chrdev
, indio_dev
->dev
.devt
, 1);
1173 goto error_unreg_eventset
;
1175 ret
= device_add(&indio_dev
->dev
);
1177 goto error_cdev_del
;
1181 cdev_del(&indio_dev
->chrdev
);
1182 error_unreg_eventset
:
1183 iio_device_unregister_eventset(indio_dev
);
1185 iio_device_unregister_sysfs(indio_dev
);
1186 error_unreg_debugfs
:
1187 iio_device_unregister_debugfs(indio_dev
);
1190 EXPORT_SYMBOL(iio_device_register
);
1193 * iio_device_unregister() - unregister a device from the IIO subsystem
1194 * @indio_dev: Device structure representing the device.
1196 void iio_device_unregister(struct iio_dev
*indio_dev
)
1198 mutex_lock(&indio_dev
->info_exist_lock
);
1200 device_del(&indio_dev
->dev
);
1202 if (indio_dev
->chrdev
.dev
)
1203 cdev_del(&indio_dev
->chrdev
);
1204 iio_device_unregister_debugfs(indio_dev
);
1206 iio_disable_all_buffers(indio_dev
);
1208 indio_dev
->info
= NULL
;
1210 iio_device_wakeup_eventset(indio_dev
);
1211 iio_buffer_wakeup_poll(indio_dev
);
1213 mutex_unlock(&indio_dev
->info_exist_lock
);
1215 EXPORT_SYMBOL(iio_device_unregister
);
1217 static void devm_iio_device_unreg(struct device
*dev
, void *res
)
1219 iio_device_unregister(*(struct iio_dev
**)res
);
1223 * devm_iio_device_register - Resource-managed iio_device_register()
1224 * @dev: Device to allocate iio_dev for
1225 * @indio_dev: Device structure filled by the device driver
1227 * Managed iio_device_register. The IIO device registered with this
1228 * function is automatically unregistered on driver detach. This function
1229 * calls iio_device_register() internally. Refer to that function for more
1232 * If an iio_dev registered with this function needs to be unregistered
1233 * separately, devm_iio_device_unregister() must be used.
1236 * 0 on success, negative error number on failure.
1238 int devm_iio_device_register(struct device
*dev
, struct iio_dev
*indio_dev
)
1240 struct iio_dev
**ptr
;
1243 ptr
= devres_alloc(devm_iio_device_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1248 ret
= iio_device_register(indio_dev
);
1250 devres_add(dev
, ptr
);
1256 EXPORT_SYMBOL_GPL(devm_iio_device_register
);
1259 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1260 * @dev: Device this iio_dev belongs to
1261 * @indio_dev: the iio_dev associated with the device
1263 * Unregister iio_dev registered with devm_iio_device_register().
1265 void devm_iio_device_unregister(struct device
*dev
, struct iio_dev
*indio_dev
)
1269 rc
= devres_release(dev
, devm_iio_device_unreg
,
1270 devm_iio_device_match
, indio_dev
);
1273 EXPORT_SYMBOL_GPL(devm_iio_device_unregister
);
1275 subsys_initcall(iio_init
);
1276 module_exit(iio_exit
);
1278 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1279 MODULE_DESCRIPTION("Industrial I/O core");
1280 MODULE_LICENSE("GPL");