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",
82 [IIO_UVINDEX
] = "uvindex",
85 static const char * const iio_modifier_names
[] = {
89 [IIO_MOD_X_AND_Y
] = "x&y",
90 [IIO_MOD_X_AND_Z
] = "x&z",
91 [IIO_MOD_Y_AND_Z
] = "y&z",
92 [IIO_MOD_X_AND_Y_AND_Z
] = "x&y&z",
93 [IIO_MOD_X_OR_Y
] = "x|y",
94 [IIO_MOD_X_OR_Z
] = "x|z",
95 [IIO_MOD_Y_OR_Z
] = "y|z",
96 [IIO_MOD_X_OR_Y_OR_Z
] = "x|y|z",
97 [IIO_MOD_ROOT_SUM_SQUARED_X_Y
] = "sqrt(x^2+y^2)",
98 [IIO_MOD_SUM_SQUARED_X_Y_Z
] = "x^2+y^2+z^2",
99 [IIO_MOD_LIGHT_BOTH
] = "both",
100 [IIO_MOD_LIGHT_IR
] = "ir",
101 [IIO_MOD_LIGHT_CLEAR
] = "clear",
102 [IIO_MOD_LIGHT_RED
] = "red",
103 [IIO_MOD_LIGHT_GREEN
] = "green",
104 [IIO_MOD_LIGHT_BLUE
] = "blue",
105 [IIO_MOD_LIGHT_UV
] = "uv",
106 [IIO_MOD_QUATERNION
] = "quaternion",
107 [IIO_MOD_TEMP_AMBIENT
] = "ambient",
108 [IIO_MOD_TEMP_OBJECT
] = "object",
109 [IIO_MOD_NORTH_MAGN
] = "from_north_magnetic",
110 [IIO_MOD_NORTH_TRUE
] = "from_north_true",
111 [IIO_MOD_NORTH_MAGN_TILT_COMP
] = "from_north_magnetic_tilt_comp",
112 [IIO_MOD_NORTH_TRUE_TILT_COMP
] = "from_north_true_tilt_comp",
113 [IIO_MOD_RUNNING
] = "running",
114 [IIO_MOD_JOGGING
] = "jogging",
115 [IIO_MOD_WALKING
] = "walking",
116 [IIO_MOD_STILL
] = "still",
117 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
] = "sqrt(x^2+y^2+z^2)",
120 [IIO_MOD_CO2
] = "co2",
121 [IIO_MOD_VOC
] = "voc",
124 /* relies on pairs of these shared then separate */
125 static const char * const iio_chan_info_postfix
[] = {
126 [IIO_CHAN_INFO_RAW
] = "raw",
127 [IIO_CHAN_INFO_PROCESSED
] = "input",
128 [IIO_CHAN_INFO_SCALE
] = "scale",
129 [IIO_CHAN_INFO_OFFSET
] = "offset",
130 [IIO_CHAN_INFO_CALIBSCALE
] = "calibscale",
131 [IIO_CHAN_INFO_CALIBBIAS
] = "calibbias",
132 [IIO_CHAN_INFO_PEAK
] = "peak_raw",
133 [IIO_CHAN_INFO_PEAK_SCALE
] = "peak_scale",
134 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW
] = "quadrature_correction_raw",
135 [IIO_CHAN_INFO_AVERAGE_RAW
] = "mean_raw",
136 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
]
137 = "filter_low_pass_3db_frequency",
138 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
]
139 = "filter_high_pass_3db_frequency",
140 [IIO_CHAN_INFO_SAMP_FREQ
] = "sampling_frequency",
141 [IIO_CHAN_INFO_FREQUENCY
] = "frequency",
142 [IIO_CHAN_INFO_PHASE
] = "phase",
143 [IIO_CHAN_INFO_HARDWAREGAIN
] = "hardwaregain",
144 [IIO_CHAN_INFO_HYSTERESIS
] = "hysteresis",
145 [IIO_CHAN_INFO_INT_TIME
] = "integration_time",
146 [IIO_CHAN_INFO_ENABLE
] = "en",
147 [IIO_CHAN_INFO_CALIBHEIGHT
] = "calibheight",
148 [IIO_CHAN_INFO_CALIBWEIGHT
] = "calibweight",
149 [IIO_CHAN_INFO_DEBOUNCE_COUNT
] = "debounce_count",
150 [IIO_CHAN_INFO_DEBOUNCE_TIME
] = "debounce_time",
151 [IIO_CHAN_INFO_CALIBEMISSIVITY
] = "calibemissivity",
152 [IIO_CHAN_INFO_OVERSAMPLING_RATIO
] = "oversampling_ratio",
156 * iio_find_channel_from_si() - get channel from its scan index
158 * @si: scan index to match
160 const struct iio_chan_spec
161 *iio_find_channel_from_si(struct iio_dev
*indio_dev
, int si
)
165 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
166 if (indio_dev
->channels
[i
].scan_index
== si
)
167 return &indio_dev
->channels
[i
];
171 /* This turns up an awful lot */
172 ssize_t
iio_read_const_attr(struct device
*dev
,
173 struct device_attribute
*attr
,
176 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
178 EXPORT_SYMBOL(iio_read_const_attr
);
180 static int __init
iio_init(void)
184 /* Register sysfs bus */
185 ret
= bus_register(&iio_bus_type
);
187 pr_err("could not register bus type\n");
191 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
193 pr_err("failed to allocate char dev region\n");
194 goto error_unregister_bus_type
;
197 iio_debugfs_dentry
= debugfs_create_dir("iio", NULL
);
201 error_unregister_bus_type
:
202 bus_unregister(&iio_bus_type
);
207 static void __exit
iio_exit(void)
210 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
211 bus_unregister(&iio_bus_type
);
212 debugfs_remove(iio_debugfs_dentry
);
215 #if defined(CONFIG_DEBUG_FS)
216 static ssize_t
iio_debugfs_read_reg(struct file
*file
, char __user
*userbuf
,
217 size_t count
, loff_t
*ppos
)
219 struct iio_dev
*indio_dev
= file
->private_data
;
225 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
,
226 indio_dev
->cached_reg_addr
,
229 dev_err(indio_dev
->dev
.parent
, "%s: read failed\n", __func__
);
231 len
= snprintf(buf
, sizeof(buf
), "0x%X\n", val
);
233 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
236 static ssize_t
iio_debugfs_write_reg(struct file
*file
,
237 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
239 struct iio_dev
*indio_dev
= file
->private_data
;
244 count
= min_t(size_t, count
, (sizeof(buf
)-1));
245 if (copy_from_user(buf
, userbuf
, count
))
250 ret
= sscanf(buf
, "%i %i", ®
, &val
);
254 indio_dev
->cached_reg_addr
= reg
;
257 indio_dev
->cached_reg_addr
= reg
;
258 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
, reg
,
261 dev_err(indio_dev
->dev
.parent
, "%s: write failed\n",
273 static const struct file_operations iio_debugfs_reg_fops
= {
275 .read
= iio_debugfs_read_reg
,
276 .write
= iio_debugfs_write_reg
,
279 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
281 debugfs_remove_recursive(indio_dev
->debugfs_dentry
);
284 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
288 if (indio_dev
->info
->debugfs_reg_access
== NULL
)
291 if (!iio_debugfs_dentry
)
294 indio_dev
->debugfs_dentry
=
295 debugfs_create_dir(dev_name(&indio_dev
->dev
),
297 if (indio_dev
->debugfs_dentry
== NULL
) {
298 dev_warn(indio_dev
->dev
.parent
,
299 "Failed to create debugfs directory\n");
303 d
= debugfs_create_file("direct_reg_access", 0644,
304 indio_dev
->debugfs_dentry
,
305 indio_dev
, &iio_debugfs_reg_fops
);
307 iio_device_unregister_debugfs(indio_dev
);
314 static int iio_device_register_debugfs(struct iio_dev
*indio_dev
)
319 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
322 #endif /* CONFIG_DEBUG_FS */
324 static ssize_t
iio_read_channel_ext_info(struct device
*dev
,
325 struct device_attribute
*attr
,
328 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
329 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
330 const struct iio_chan_spec_ext_info
*ext_info
;
332 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
334 return ext_info
->read(indio_dev
, ext_info
->private, this_attr
->c
, buf
);
337 static ssize_t
iio_write_channel_ext_info(struct device
*dev
,
338 struct device_attribute
*attr
,
342 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
343 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
344 const struct iio_chan_spec_ext_info
*ext_info
;
346 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
348 return ext_info
->write(indio_dev
, ext_info
->private,
349 this_attr
->c
, buf
, len
);
352 ssize_t
iio_enum_available_read(struct iio_dev
*indio_dev
,
353 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
355 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
362 for (i
= 0; i
< e
->num_items
; ++i
)
363 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%s ", e
->items
[i
]);
365 /* replace last space with a newline */
370 EXPORT_SYMBOL_GPL(iio_enum_available_read
);
372 ssize_t
iio_enum_read(struct iio_dev
*indio_dev
,
373 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
375 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
381 i
= e
->get(indio_dev
, chan
);
384 else if (i
>= e
->num_items
)
387 return snprintf(buf
, PAGE_SIZE
, "%s\n", e
->items
[i
]);
389 EXPORT_SYMBOL_GPL(iio_enum_read
);
391 ssize_t
iio_enum_write(struct iio_dev
*indio_dev
,
392 uintptr_t priv
, const struct iio_chan_spec
*chan
, const char *buf
,
395 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
402 for (i
= 0; i
< e
->num_items
; i
++) {
403 if (sysfs_streq(buf
, e
->items
[i
]))
407 if (i
== e
->num_items
)
410 ret
= e
->set(indio_dev
, chan
, i
);
411 return ret
? ret
: len
;
413 EXPORT_SYMBOL_GPL(iio_enum_write
);
415 static const struct iio_mount_matrix iio_mount_idmatrix
= {
423 static int iio_setup_mount_idmatrix(const struct device
*dev
,
424 struct iio_mount_matrix
*matrix
)
426 *matrix
= iio_mount_idmatrix
;
427 dev_info(dev
, "mounting matrix not found: using identity...\n");
431 ssize_t
iio_show_mount_matrix(struct iio_dev
*indio_dev
, uintptr_t priv
,
432 const struct iio_chan_spec
*chan
, char *buf
)
434 const struct iio_mount_matrix
*mtx
= ((iio_get_mount_matrix_t
*)
435 priv
)(indio_dev
, chan
);
441 mtx
= &iio_mount_idmatrix
;
443 return snprintf(buf
, PAGE_SIZE
, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
444 mtx
->rotation
[0], mtx
->rotation
[1], mtx
->rotation
[2],
445 mtx
->rotation
[3], mtx
->rotation
[4], mtx
->rotation
[5],
446 mtx
->rotation
[6], mtx
->rotation
[7], mtx
->rotation
[8]);
448 EXPORT_SYMBOL_GPL(iio_show_mount_matrix
);
451 * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from
452 * device-tree "mount-matrix" property
453 * @dev: device the mounting matrix property is assigned to
454 * @propname: device specific mounting matrix property name
455 * @matrix: where to store retrieved matrix
457 * If device is assigned no mounting matrix property, a default 3x3 identity
458 * matrix will be filled in.
460 * Return: 0 if success, or a negative error code on failure.
463 int of_iio_read_mount_matrix(const struct device
*dev
,
464 const char *propname
,
465 struct iio_mount_matrix
*matrix
)
468 int err
= of_property_read_string_array(dev
->of_node
,
469 propname
, matrix
->rotation
,
470 ARRAY_SIZE(iio_mount_idmatrix
.rotation
));
472 if (err
== ARRAY_SIZE(iio_mount_idmatrix
.rotation
))
476 /* Invalid number of matrix entries. */
480 /* Invalid matrix declaration format. */
484 /* Matrix was not declared at all: fallback to identity. */
485 return iio_setup_mount_idmatrix(dev
, matrix
);
488 int of_iio_read_mount_matrix(const struct device
*dev
,
489 const char *propname
,
490 struct iio_mount_matrix
*matrix
)
492 return iio_setup_mount_idmatrix(dev
, matrix
);
495 EXPORT_SYMBOL(of_iio_read_mount_matrix
);
498 * iio_format_value() - Formats a IIO value into its string representation
499 * @buf: The buffer to which the formatted value gets written
500 * @type: One of the IIO_VAL_... constants. This decides how the val
501 * and val2 parameters are formatted.
502 * @size: Number of IIO value entries contained in vals
503 * @vals: Pointer to the values, exact meaning depends on the
506 * Return: 0 by default, a negative number on failure or the
507 * total number of characters written for a type that belongs
508 * to the IIO_VAL_... constant.
510 ssize_t
iio_format_value(char *buf
, unsigned int type
, int size
, int *vals
)
512 unsigned long long tmp
;
513 bool scale_db
= false;
517 return sprintf(buf
, "%d\n", vals
[0]);
518 case IIO_VAL_INT_PLUS_MICRO_DB
:
520 case IIO_VAL_INT_PLUS_MICRO
:
522 return sprintf(buf
, "-%d.%06u%s\n", abs(vals
[0]),
523 -vals
[1], scale_db
? " dB" : "");
525 return sprintf(buf
, "%d.%06u%s\n", vals
[0], vals
[1],
526 scale_db
? " dB" : "");
527 case IIO_VAL_INT_PLUS_NANO
:
529 return sprintf(buf
, "-%d.%09u\n", abs(vals
[0]),
532 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
533 case IIO_VAL_FRACTIONAL
:
534 tmp
= div_s64((s64
)vals
[0] * 1000000000LL, vals
[1]);
535 vals
[1] = do_div(tmp
, 1000000000LL);
537 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
538 case IIO_VAL_FRACTIONAL_LOG2
:
539 tmp
= (s64
)vals
[0] * 1000000000LL >> vals
[1];
540 vals
[1] = do_div(tmp
, 1000000000LL);
542 return sprintf(buf
, "%d.%09u\n", vals
[0], vals
[1]);
543 case IIO_VAL_INT_MULTIPLE
:
548 for (i
= 0; i
< size
; ++i
)
549 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "%d ",
551 len
+= snprintf(&buf
[len
], PAGE_SIZE
- len
, "\n");
558 EXPORT_SYMBOL_GPL(iio_format_value
);
560 static ssize_t
iio_read_channel_info(struct device
*dev
,
561 struct device_attribute
*attr
,
564 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
565 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
566 int vals
[INDIO_MAX_RAW_ELEMENTS
];
570 if (indio_dev
->info
->read_raw_multi
)
571 ret
= indio_dev
->info
->read_raw_multi(indio_dev
, this_attr
->c
,
572 INDIO_MAX_RAW_ELEMENTS
,
576 ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
577 &vals
[0], &vals
[1], this_attr
->address
);
582 return iio_format_value(buf
, ret
, val_len
, vals
);
586 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
587 * @str: The string to parse
588 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
589 * @integer: The integer part of the number
590 * @fract: The fractional part of the number
592 * Returns 0 on success, or a negative error code if the string could not be
595 int iio_str_to_fixpoint(const char *str
, int fract_mult
,
596 int *integer
, int *fract
)
599 bool integer_part
= true, negative
= false;
601 if (fract_mult
== 0) {
604 return kstrtoint(str
, 0, integer
);
610 } else if (str
[0] == '+') {
615 if ('0' <= *str
&& *str
<= '9') {
617 i
= i
* 10 + *str
- '0';
619 f
+= fract_mult
* (*str
- '0');
622 } else if (*str
== '\n') {
623 if (*(str
+ 1) == '\0')
627 } else if (*str
== '.' && integer_part
) {
628 integer_part
= false;
647 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint
);
649 static ssize_t
iio_write_channel_info(struct device
*dev
,
650 struct device_attribute
*attr
,
654 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
655 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
656 int ret
, fract_mult
= 100000;
659 /* Assumes decimal - precision based on number of digits */
660 if (!indio_dev
->info
->write_raw
)
663 if (indio_dev
->info
->write_raw_get_fmt
)
664 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
665 this_attr
->c
, this_attr
->address
)) {
669 case IIO_VAL_INT_PLUS_MICRO
:
672 case IIO_VAL_INT_PLUS_NANO
:
673 fract_mult
= 100000000;
679 ret
= iio_str_to_fixpoint(buf
, fract_mult
, &integer
, &fract
);
683 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
684 integer
, fract
, this_attr
->address
);
692 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
694 struct iio_chan_spec
const *chan
,
695 ssize_t (*readfunc
)(struct device
*dev
,
696 struct device_attribute
*attr
,
698 ssize_t (*writefunc
)(struct device
*dev
,
699 struct device_attribute
*attr
,
702 enum iio_shared_by shared_by
)
707 sysfs_attr_init(&dev_attr
->attr
);
709 /* Build up postfix of <extend_name>_<modifier>_postfix */
710 if (chan
->modified
&& (shared_by
== IIO_SEPARATE
)) {
711 if (chan
->extend_name
)
712 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
713 iio_modifier_names
[chan
718 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s",
719 iio_modifier_names
[chan
723 if (chan
->extend_name
== NULL
|| shared_by
!= IIO_SEPARATE
)
724 full_postfix
= kstrdup(postfix
, GFP_KERNEL
);
726 full_postfix
= kasprintf(GFP_KERNEL
,
731 if (full_postfix
== NULL
)
734 if (chan
->differential
) { /* Differential can not have modifier */
736 case IIO_SHARED_BY_ALL
:
737 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
739 case IIO_SHARED_BY_DIR
:
740 name
= kasprintf(GFP_KERNEL
, "%s_%s",
741 iio_direction
[chan
->output
],
744 case IIO_SHARED_BY_TYPE
:
745 name
= kasprintf(GFP_KERNEL
, "%s_%s-%s_%s",
746 iio_direction
[chan
->output
],
747 iio_chan_type_name_spec
[chan
->type
],
748 iio_chan_type_name_spec
[chan
->type
],
752 if (!chan
->indexed
) {
753 WARN(1, "Differential channels must be indexed\n");
755 goto error_free_full_postfix
;
757 name
= kasprintf(GFP_KERNEL
,
759 iio_direction
[chan
->output
],
760 iio_chan_type_name_spec
[chan
->type
],
762 iio_chan_type_name_spec
[chan
->type
],
767 } else { /* Single ended */
769 case IIO_SHARED_BY_ALL
:
770 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
772 case IIO_SHARED_BY_DIR
:
773 name
= kasprintf(GFP_KERNEL
, "%s_%s",
774 iio_direction
[chan
->output
],
777 case IIO_SHARED_BY_TYPE
:
778 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
779 iio_direction
[chan
->output
],
780 iio_chan_type_name_spec
[chan
->type
],
786 name
= kasprintf(GFP_KERNEL
, "%s_%s%d_%s",
787 iio_direction
[chan
->output
],
788 iio_chan_type_name_spec
[chan
->type
],
792 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
793 iio_direction
[chan
->output
],
794 iio_chan_type_name_spec
[chan
->type
],
801 goto error_free_full_postfix
;
803 dev_attr
->attr
.name
= name
;
806 dev_attr
->attr
.mode
|= S_IRUGO
;
807 dev_attr
->show
= readfunc
;
811 dev_attr
->attr
.mode
|= S_IWUSR
;
812 dev_attr
->store
= writefunc
;
815 error_free_full_postfix
:
821 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
823 kfree(dev_attr
->attr
.name
);
826 int __iio_add_chan_devattr(const char *postfix
,
827 struct iio_chan_spec
const *chan
,
828 ssize_t (*readfunc
)(struct device
*dev
,
829 struct device_attribute
*attr
,
831 ssize_t (*writefunc
)(struct device
*dev
,
832 struct device_attribute
*attr
,
836 enum iio_shared_by shared_by
,
838 struct list_head
*attr_list
)
841 struct iio_dev_attr
*iio_attr
, *t
;
843 iio_attr
= kzalloc(sizeof(*iio_attr
), GFP_KERNEL
);
844 if (iio_attr
== NULL
)
846 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
848 readfunc
, writefunc
, shared_by
);
850 goto error_iio_dev_attr_free
;
852 iio_attr
->address
= mask
;
853 list_for_each_entry(t
, attr_list
, l
)
854 if (strcmp(t
->dev_attr
.attr
.name
,
855 iio_attr
->dev_attr
.attr
.name
) == 0) {
856 if (shared_by
== IIO_SEPARATE
)
857 dev_err(dev
, "tried to double register : %s\n",
858 t
->dev_attr
.attr
.name
);
860 goto error_device_attr_deinit
;
862 list_add(&iio_attr
->l
, attr_list
);
866 error_device_attr_deinit
:
867 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
868 error_iio_dev_attr_free
:
873 static int iio_device_add_info_mask_type(struct iio_dev
*indio_dev
,
874 struct iio_chan_spec
const *chan
,
875 enum iio_shared_by shared_by
,
876 const long *infomask
)
878 int i
, ret
, attrcount
= 0;
880 for_each_set_bit(i
, infomask
, sizeof(infomask
)*8) {
881 if (i
>= ARRAY_SIZE(iio_chan_info_postfix
))
883 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
],
885 &iio_read_channel_info
,
886 &iio_write_channel_info
,
890 &indio_dev
->channel_attr_list
);
891 if ((ret
== -EBUSY
) && (shared_by
!= IIO_SEPARATE
))
901 static int iio_device_add_channel_sysfs(struct iio_dev
*indio_dev
,
902 struct iio_chan_spec
const *chan
)
904 int ret
, attrcount
= 0;
905 const struct iio_chan_spec_ext_info
*ext_info
;
907 if (chan
->channel
< 0)
909 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
911 &chan
->info_mask_separate
);
916 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
918 &chan
->info_mask_shared_by_type
);
923 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
925 &chan
->info_mask_shared_by_dir
);
930 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
932 &chan
->info_mask_shared_by_all
);
937 if (chan
->ext_info
) {
939 for (ext_info
= chan
->ext_info
; ext_info
->name
; ext_info
++) {
940 ret
= __iio_add_chan_devattr(ext_info
->name
,
943 &iio_read_channel_ext_info
: NULL
,
945 &iio_write_channel_ext_info
: NULL
,
949 &indio_dev
->channel_attr_list
);
951 if (ret
== -EBUSY
&& ext_info
->shared
)
965 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
966 * @attr_list: List of IIO device attributes
968 * This function frees the memory allocated for each of the IIO device
969 * attributes in the list.
971 void iio_free_chan_devattr_list(struct list_head
*attr_list
)
973 struct iio_dev_attr
*p
, *n
;
975 list_for_each_entry_safe(p
, n
, attr_list
, l
) {
976 kfree(p
->dev_attr
.attr
.name
);
982 static ssize_t
iio_show_dev_name(struct device
*dev
,
983 struct device_attribute
*attr
,
986 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
987 return snprintf(buf
, PAGE_SIZE
, "%s\n", indio_dev
->name
);
990 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
992 static int iio_device_register_sysfs(struct iio_dev
*indio_dev
)
994 int i
, ret
= 0, attrcount
, attrn
, attrcount_orig
= 0;
995 struct iio_dev_attr
*p
;
996 struct attribute
**attr
;
998 /* First count elements in any existing group */
999 if (indio_dev
->info
->attrs
) {
1000 attr
= indio_dev
->info
->attrs
->attrs
;
1001 while (*attr
++ != NULL
)
1004 attrcount
= attrcount_orig
;
1006 * New channel registration method - relies on the fact a group does
1007 * not need to be initialized if its name is NULL.
1009 if (indio_dev
->channels
)
1010 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1011 ret
= iio_device_add_channel_sysfs(indio_dev
,
1015 goto error_clear_attrs
;
1019 if (indio_dev
->name
)
1022 indio_dev
->chan_attr_group
.attrs
= kcalloc(attrcount
+ 1,
1023 sizeof(indio_dev
->chan_attr_group
.attrs
[0]),
1025 if (indio_dev
->chan_attr_group
.attrs
== NULL
) {
1027 goto error_clear_attrs
;
1029 /* Copy across original attributes */
1030 if (indio_dev
->info
->attrs
)
1031 memcpy(indio_dev
->chan_attr_group
.attrs
,
1032 indio_dev
->info
->attrs
->attrs
,
1033 sizeof(indio_dev
->chan_attr_group
.attrs
[0])
1035 attrn
= attrcount_orig
;
1036 /* Add all elements from the list. */
1037 list_for_each_entry(p
, &indio_dev
->channel_attr_list
, l
)
1038 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
1039 if (indio_dev
->name
)
1040 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_name
.attr
;
1042 indio_dev
->groups
[indio_dev
->groupcounter
++] =
1043 &indio_dev
->chan_attr_group
;
1048 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
1053 static void iio_device_unregister_sysfs(struct iio_dev
*indio_dev
)
1056 iio_free_chan_devattr_list(&indio_dev
->channel_attr_list
);
1057 kfree(indio_dev
->chan_attr_group
.attrs
);
1058 indio_dev
->chan_attr_group
.attrs
= NULL
;
1061 static void iio_dev_release(struct device
*device
)
1063 struct iio_dev
*indio_dev
= dev_to_iio_dev(device
);
1064 if (indio_dev
->modes
& (INDIO_BUFFER_TRIGGERED
| INDIO_EVENT_TRIGGERED
))
1065 iio_device_unregister_trigger_consumer(indio_dev
);
1066 iio_device_unregister_eventset(indio_dev
);
1067 iio_device_unregister_sysfs(indio_dev
);
1069 iio_buffer_put(indio_dev
->buffer
);
1071 ida_simple_remove(&iio_ida
, indio_dev
->id
);
1075 struct device_type iio_device_type
= {
1076 .name
= "iio_device",
1077 .release
= iio_dev_release
,
1081 * iio_device_alloc() - allocate an iio_dev from a driver
1082 * @sizeof_priv: Space to allocate for private structure.
1084 struct iio_dev
*iio_device_alloc(int sizeof_priv
)
1086 struct iio_dev
*dev
;
1089 alloc_size
= sizeof(struct iio_dev
);
1091 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
1092 alloc_size
+= sizeof_priv
;
1094 /* ensure 32-byte alignment of whole construct ? */
1095 alloc_size
+= IIO_ALIGN
- 1;
1097 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
1100 dev
->dev
.groups
= dev
->groups
;
1101 dev
->dev
.type
= &iio_device_type
;
1102 dev
->dev
.bus
= &iio_bus_type
;
1103 device_initialize(&dev
->dev
);
1104 dev_set_drvdata(&dev
->dev
, (void *)dev
);
1105 mutex_init(&dev
->mlock
);
1106 mutex_init(&dev
->info_exist_lock
);
1107 INIT_LIST_HEAD(&dev
->channel_attr_list
);
1109 dev
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
1111 /* cannot use a dev_err as the name isn't available */
1112 pr_err("failed to get device id\n");
1116 dev_set_name(&dev
->dev
, "iio:device%d", dev
->id
);
1117 INIT_LIST_HEAD(&dev
->buffer_list
);
1122 EXPORT_SYMBOL(iio_device_alloc
);
1125 * iio_device_free() - free an iio_dev from a driver
1126 * @dev: the iio_dev associated with the device
1128 void iio_device_free(struct iio_dev
*dev
)
1131 put_device(&dev
->dev
);
1133 EXPORT_SYMBOL(iio_device_free
);
1135 static void devm_iio_device_release(struct device
*dev
, void *res
)
1137 iio_device_free(*(struct iio_dev
**)res
);
1140 static int devm_iio_device_match(struct device
*dev
, void *res
, void *data
)
1142 struct iio_dev
**r
= res
;
1151 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1152 * @dev: Device to allocate iio_dev for
1153 * @sizeof_priv: Space to allocate for private structure.
1155 * Managed iio_device_alloc. iio_dev allocated with this function is
1156 * automatically freed on driver detach.
1158 * If an iio_dev allocated with this function needs to be freed separately,
1159 * devm_iio_device_free() must be used.
1162 * Pointer to allocated iio_dev on success, NULL on failure.
1164 struct iio_dev
*devm_iio_device_alloc(struct device
*dev
, int sizeof_priv
)
1166 struct iio_dev
**ptr
, *iio_dev
;
1168 ptr
= devres_alloc(devm_iio_device_release
, sizeof(*ptr
),
1173 iio_dev
= iio_device_alloc(sizeof_priv
);
1176 devres_add(dev
, ptr
);
1183 EXPORT_SYMBOL_GPL(devm_iio_device_alloc
);
1186 * devm_iio_device_free - Resource-managed iio_device_free()
1187 * @dev: Device this iio_dev belongs to
1188 * @iio_dev: the iio_dev associated with the device
1190 * Free iio_dev allocated with devm_iio_device_alloc().
1192 void devm_iio_device_free(struct device
*dev
, struct iio_dev
*iio_dev
)
1196 rc
= devres_release(dev
, devm_iio_device_release
,
1197 devm_iio_device_match
, iio_dev
);
1200 EXPORT_SYMBOL_GPL(devm_iio_device_free
);
1203 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1204 * @inode: Inode structure for identifying the device in the file system
1205 * @filp: File structure for iio device used to keep and later access
1208 * Return: 0 on success or -EBUSY if the device is already opened
1210 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1212 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1213 struct iio_dev
, chrdev
);
1215 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
))
1218 iio_device_get(indio_dev
);
1220 filp
->private_data
= indio_dev
;
1226 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1227 * @inode: Inode structure pointer for the char device
1228 * @filp: File structure pointer for the char device
1230 * Return: 0 for successful release
1232 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1234 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1235 struct iio_dev
, chrdev
);
1236 clear_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
);
1237 iio_device_put(indio_dev
);
1242 /* Somewhat of a cross file organization violation - ioctls here are actually
1244 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1246 struct iio_dev
*indio_dev
= filp
->private_data
;
1247 int __user
*ip
= (int __user
*)arg
;
1250 if (!indio_dev
->info
)
1253 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1254 fd
= iio_event_getfd(indio_dev
);
1257 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1264 static const struct file_operations iio_buffer_fileops
= {
1265 .read
= iio_buffer_read_first_n_outer_addr
,
1266 .release
= iio_chrdev_release
,
1267 .open
= iio_chrdev_open
,
1268 .poll
= iio_buffer_poll_addr
,
1269 .owner
= THIS_MODULE
,
1270 .llseek
= noop_llseek
,
1271 .unlocked_ioctl
= iio_ioctl
,
1272 .compat_ioctl
= iio_ioctl
,
1275 static int iio_check_unique_scan_index(struct iio_dev
*indio_dev
)
1278 const struct iio_chan_spec
*channels
= indio_dev
->channels
;
1280 if (!(indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
))
1283 for (i
= 0; i
< indio_dev
->num_channels
- 1; i
++) {
1284 if (channels
[i
].scan_index
< 0)
1286 for (j
= i
+ 1; j
< indio_dev
->num_channels
; j
++)
1287 if (channels
[i
].scan_index
== channels
[j
].scan_index
) {
1288 dev_err(&indio_dev
->dev
,
1289 "Duplicate scan index %d\n",
1290 channels
[i
].scan_index
);
1298 static const struct iio_buffer_setup_ops noop_ring_setup_ops
;
1301 * iio_device_register() - register a device with the IIO subsystem
1302 * @indio_dev: Device structure filled by the device driver
1304 int iio_device_register(struct iio_dev
*indio_dev
)
1308 /* If the calling driver did not initialize of_node, do it here */
1309 if (!indio_dev
->dev
.of_node
&& indio_dev
->dev
.parent
)
1310 indio_dev
->dev
.of_node
= indio_dev
->dev
.parent
->of_node
;
1312 ret
= iio_check_unique_scan_index(indio_dev
);
1316 /* configure elements for the chrdev */
1317 indio_dev
->dev
.devt
= MKDEV(MAJOR(iio_devt
), indio_dev
->id
);
1319 ret
= iio_device_register_debugfs(indio_dev
);
1321 dev_err(indio_dev
->dev
.parent
,
1322 "Failed to register debugfs interfaces\n");
1326 ret
= iio_buffer_alloc_sysfs_and_mask(indio_dev
);
1328 dev_err(indio_dev
->dev
.parent
,
1329 "Failed to create buffer sysfs interfaces\n");
1330 goto error_unreg_debugfs
;
1333 ret
= iio_device_register_sysfs(indio_dev
);
1335 dev_err(indio_dev
->dev
.parent
,
1336 "Failed to register sysfs interfaces\n");
1337 goto error_buffer_free_sysfs
;
1339 ret
= iio_device_register_eventset(indio_dev
);
1341 dev_err(indio_dev
->dev
.parent
,
1342 "Failed to register event set\n");
1343 goto error_free_sysfs
;
1345 if (indio_dev
->modes
& (INDIO_BUFFER_TRIGGERED
| INDIO_EVENT_TRIGGERED
))
1346 iio_device_register_trigger_consumer(indio_dev
);
1348 if ((indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
) &&
1349 indio_dev
->setup_ops
== NULL
)
1350 indio_dev
->setup_ops
= &noop_ring_setup_ops
;
1352 cdev_init(&indio_dev
->chrdev
, &iio_buffer_fileops
);
1353 indio_dev
->chrdev
.owner
= indio_dev
->info
->driver_module
;
1354 indio_dev
->chrdev
.kobj
.parent
= &indio_dev
->dev
.kobj
;
1355 ret
= cdev_add(&indio_dev
->chrdev
, indio_dev
->dev
.devt
, 1);
1357 goto error_unreg_eventset
;
1359 ret
= device_add(&indio_dev
->dev
);
1361 goto error_cdev_del
;
1365 cdev_del(&indio_dev
->chrdev
);
1366 error_unreg_eventset
:
1367 iio_device_unregister_eventset(indio_dev
);
1369 iio_device_unregister_sysfs(indio_dev
);
1370 error_buffer_free_sysfs
:
1371 iio_buffer_free_sysfs_and_mask(indio_dev
);
1372 error_unreg_debugfs
:
1373 iio_device_unregister_debugfs(indio_dev
);
1376 EXPORT_SYMBOL(iio_device_register
);
1379 * iio_device_unregister() - unregister a device from the IIO subsystem
1380 * @indio_dev: Device structure representing the device.
1382 void iio_device_unregister(struct iio_dev
*indio_dev
)
1384 mutex_lock(&indio_dev
->info_exist_lock
);
1386 device_del(&indio_dev
->dev
);
1388 if (indio_dev
->chrdev
.dev
)
1389 cdev_del(&indio_dev
->chrdev
);
1390 iio_device_unregister_debugfs(indio_dev
);
1392 iio_disable_all_buffers(indio_dev
);
1394 indio_dev
->info
= NULL
;
1396 iio_device_wakeup_eventset(indio_dev
);
1397 iio_buffer_wakeup_poll(indio_dev
);
1399 mutex_unlock(&indio_dev
->info_exist_lock
);
1401 iio_buffer_free_sysfs_and_mask(indio_dev
);
1403 EXPORT_SYMBOL(iio_device_unregister
);
1405 static void devm_iio_device_unreg(struct device
*dev
, void *res
)
1407 iio_device_unregister(*(struct iio_dev
**)res
);
1411 * devm_iio_device_register - Resource-managed iio_device_register()
1412 * @dev: Device to allocate iio_dev for
1413 * @indio_dev: Device structure filled by the device driver
1415 * Managed iio_device_register. The IIO device registered with this
1416 * function is automatically unregistered on driver detach. This function
1417 * calls iio_device_register() internally. Refer to that function for more
1420 * If an iio_dev registered with this function needs to be unregistered
1421 * separately, devm_iio_device_unregister() must be used.
1424 * 0 on success, negative error number on failure.
1426 int devm_iio_device_register(struct device
*dev
, struct iio_dev
*indio_dev
)
1428 struct iio_dev
**ptr
;
1431 ptr
= devres_alloc(devm_iio_device_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1436 ret
= iio_device_register(indio_dev
);
1438 devres_add(dev
, ptr
);
1444 EXPORT_SYMBOL_GPL(devm_iio_device_register
);
1447 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1448 * @dev: Device this iio_dev belongs to
1449 * @indio_dev: the iio_dev associated with the device
1451 * Unregister iio_dev registered with devm_iio_device_register().
1453 void devm_iio_device_unregister(struct device
*dev
, struct iio_dev
*indio_dev
)
1457 rc
= devres_release(dev
, devm_iio_device_unreg
,
1458 devm_iio_device_match
, indio_dev
);
1461 EXPORT_SYMBOL_GPL(devm_iio_device_unregister
);
1464 * iio_device_claim_direct_mode - Keep device in direct mode
1465 * @indio_dev: the iio_dev associated with the device
1467 * If the device is in direct mode it is guaranteed to stay
1468 * that way until iio_device_release_direct_mode() is called.
1470 * Use with iio_device_release_direct_mode()
1472 * Returns: 0 on success, -EBUSY on failure
1474 int iio_device_claim_direct_mode(struct iio_dev
*indio_dev
)
1476 mutex_lock(&indio_dev
->mlock
);
1478 if (iio_buffer_enabled(indio_dev
)) {
1479 mutex_unlock(&indio_dev
->mlock
);
1484 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode
);
1487 * iio_device_release_direct_mode - releases claim on direct mode
1488 * @indio_dev: the iio_dev associated with the device
1490 * Release the claim. Device is no longer guaranteed to stay
1493 * Use with iio_device_claim_direct_mode()
1495 void iio_device_release_direct_mode(struct iio_dev
*indio_dev
)
1497 mutex_unlock(&indio_dev
->mlock
);
1499 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode
);
1501 subsys_initcall(iio_init
);
1502 module_exit(iio_exit
);
1504 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1505 MODULE_DESCRIPTION("Industrial I/O core");
1506 MODULE_LICENSE("GPL");