1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core
4 * Copyright (c) 2008 Jonathan Cameron
6 * Based on elements of hwmon and input subsystems.
9 #define pr_fmt(fmt) "iio-core: " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/idr.h>
14 #include <linux/kdev_t.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
18 #include <linux/poll.h>
19 #include <linux/property.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/debugfs.h>
26 #include <linux/mutex.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/iio-opaque.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>
34 #include <linux/iio/buffer_impl.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",
83 [IIO_ELECTRICALCONDUCTIVITY
] = "electricalconductivity",
84 [IIO_COUNT
] = "count",
85 [IIO_INDEX
] = "index",
86 [IIO_GRAVITY
] = "gravity",
87 [IIO_POSITIONRELATIVE
] = "positionrelative",
88 [IIO_PHASE
] = "phase",
89 [IIO_MASSCONCENTRATION
] = "massconcentration",
92 static const char * const iio_modifier_names
[] = {
96 [IIO_MOD_X_AND_Y
] = "x&y",
97 [IIO_MOD_X_AND_Z
] = "x&z",
98 [IIO_MOD_Y_AND_Z
] = "y&z",
99 [IIO_MOD_X_AND_Y_AND_Z
] = "x&y&z",
100 [IIO_MOD_X_OR_Y
] = "x|y",
101 [IIO_MOD_X_OR_Z
] = "x|z",
102 [IIO_MOD_Y_OR_Z
] = "y|z",
103 [IIO_MOD_X_OR_Y_OR_Z
] = "x|y|z",
104 [IIO_MOD_ROOT_SUM_SQUARED_X_Y
] = "sqrt(x^2+y^2)",
105 [IIO_MOD_SUM_SQUARED_X_Y_Z
] = "x^2+y^2+z^2",
106 [IIO_MOD_LIGHT_BOTH
] = "both",
107 [IIO_MOD_LIGHT_IR
] = "ir",
108 [IIO_MOD_LIGHT_CLEAR
] = "clear",
109 [IIO_MOD_LIGHT_RED
] = "red",
110 [IIO_MOD_LIGHT_GREEN
] = "green",
111 [IIO_MOD_LIGHT_BLUE
] = "blue",
112 [IIO_MOD_LIGHT_UV
] = "uv",
113 [IIO_MOD_LIGHT_DUV
] = "duv",
114 [IIO_MOD_QUATERNION
] = "quaternion",
115 [IIO_MOD_TEMP_AMBIENT
] = "ambient",
116 [IIO_MOD_TEMP_OBJECT
] = "object",
117 [IIO_MOD_NORTH_MAGN
] = "from_north_magnetic",
118 [IIO_MOD_NORTH_TRUE
] = "from_north_true",
119 [IIO_MOD_NORTH_MAGN_TILT_COMP
] = "from_north_magnetic_tilt_comp",
120 [IIO_MOD_NORTH_TRUE_TILT_COMP
] = "from_north_true_tilt_comp",
121 [IIO_MOD_RUNNING
] = "running",
122 [IIO_MOD_JOGGING
] = "jogging",
123 [IIO_MOD_WALKING
] = "walking",
124 [IIO_MOD_STILL
] = "still",
125 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
] = "sqrt(x^2+y^2+z^2)",
128 [IIO_MOD_CO2
] = "co2",
129 [IIO_MOD_VOC
] = "voc",
130 [IIO_MOD_PM1
] = "pm1",
131 [IIO_MOD_PM2P5
] = "pm2p5",
132 [IIO_MOD_PM4
] = "pm4",
133 [IIO_MOD_PM10
] = "pm10",
134 [IIO_MOD_ETHANOL
] = "ethanol",
139 /* relies on pairs of these shared then separate */
140 static const char * const iio_chan_info_postfix
[] = {
141 [IIO_CHAN_INFO_RAW
] = "raw",
142 [IIO_CHAN_INFO_PROCESSED
] = "input",
143 [IIO_CHAN_INFO_SCALE
] = "scale",
144 [IIO_CHAN_INFO_OFFSET
] = "offset",
145 [IIO_CHAN_INFO_CALIBSCALE
] = "calibscale",
146 [IIO_CHAN_INFO_CALIBBIAS
] = "calibbias",
147 [IIO_CHAN_INFO_PEAK
] = "peak_raw",
148 [IIO_CHAN_INFO_PEAK_SCALE
] = "peak_scale",
149 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW
] = "quadrature_correction_raw",
150 [IIO_CHAN_INFO_AVERAGE_RAW
] = "mean_raw",
151 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
]
152 = "filter_low_pass_3db_frequency",
153 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
]
154 = "filter_high_pass_3db_frequency",
155 [IIO_CHAN_INFO_SAMP_FREQ
] = "sampling_frequency",
156 [IIO_CHAN_INFO_FREQUENCY
] = "frequency",
157 [IIO_CHAN_INFO_PHASE
] = "phase",
158 [IIO_CHAN_INFO_HARDWAREGAIN
] = "hardwaregain",
159 [IIO_CHAN_INFO_HYSTERESIS
] = "hysteresis",
160 [IIO_CHAN_INFO_INT_TIME
] = "integration_time",
161 [IIO_CHAN_INFO_ENABLE
] = "en",
162 [IIO_CHAN_INFO_CALIBHEIGHT
] = "calibheight",
163 [IIO_CHAN_INFO_CALIBWEIGHT
] = "calibweight",
164 [IIO_CHAN_INFO_DEBOUNCE_COUNT
] = "debounce_count",
165 [IIO_CHAN_INFO_DEBOUNCE_TIME
] = "debounce_time",
166 [IIO_CHAN_INFO_CALIBEMISSIVITY
] = "calibemissivity",
167 [IIO_CHAN_INFO_OVERSAMPLING_RATIO
] = "oversampling_ratio",
168 [IIO_CHAN_INFO_THERMOCOUPLE_TYPE
] = "thermocouple_type",
169 [IIO_CHAN_INFO_CALIBAMBIENT
] = "calibambient",
172 #if defined(CONFIG_DEBUG_FS)
174 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
175 * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
177 struct dentry
*iio_get_debugfs_dentry(struct iio_dev
*indio_dev
)
179 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
180 return iio_dev_opaque
->debugfs_dentry
;
182 EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry
);
186 * iio_find_channel_from_si() - get channel from its scan index
188 * @si: scan index to match
190 const struct iio_chan_spec
191 *iio_find_channel_from_si(struct iio_dev
*indio_dev
, int si
)
195 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
196 if (indio_dev
->channels
[i
].scan_index
== si
)
197 return &indio_dev
->channels
[i
];
201 /* This turns up an awful lot */
202 ssize_t
iio_read_const_attr(struct device
*dev
,
203 struct device_attribute
*attr
,
206 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
208 EXPORT_SYMBOL(iio_read_const_attr
);
211 * iio_device_set_clock() - Set current timestamping clock for the device
212 * @indio_dev: IIO device structure containing the device
213 * @clock_id: timestamping clock posix identifier to set.
215 int iio_device_set_clock(struct iio_dev
*indio_dev
, clockid_t clock_id
)
218 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
219 const struct iio_event_interface
*ev_int
= iio_dev_opaque
->event_interface
;
221 ret
= mutex_lock_interruptible(&indio_dev
->mlock
);
224 if ((ev_int
&& iio_event_enabled(ev_int
)) ||
225 iio_buffer_enabled(indio_dev
)) {
226 mutex_unlock(&indio_dev
->mlock
);
229 indio_dev
->clock_id
= clock_id
;
230 mutex_unlock(&indio_dev
->mlock
);
234 EXPORT_SYMBOL(iio_device_set_clock
);
237 * iio_get_time_ns() - utility function to get a time stamp for events etc
240 s64
iio_get_time_ns(const struct iio_dev
*indio_dev
)
242 struct timespec64 tp
;
244 switch (iio_device_get_clock(indio_dev
)) {
246 return ktime_get_real_ns();
247 case CLOCK_MONOTONIC
:
248 return ktime_get_ns();
249 case CLOCK_MONOTONIC_RAW
:
250 return ktime_get_raw_ns();
251 case CLOCK_REALTIME_COARSE
:
252 return ktime_to_ns(ktime_get_coarse_real());
253 case CLOCK_MONOTONIC_COARSE
:
254 ktime_get_coarse_ts64(&tp
);
255 return timespec64_to_ns(&tp
);
257 return ktime_get_boottime_ns();
259 return ktime_get_clocktai_ns();
264 EXPORT_SYMBOL(iio_get_time_ns
);
267 * iio_get_time_res() - utility function to get time stamp clock resolution in
271 unsigned int iio_get_time_res(const struct iio_dev
*indio_dev
)
273 switch (iio_device_get_clock(indio_dev
)) {
275 case CLOCK_MONOTONIC
:
276 case CLOCK_MONOTONIC_RAW
:
279 return hrtimer_resolution
;
280 case CLOCK_REALTIME_COARSE
:
281 case CLOCK_MONOTONIC_COARSE
:
287 EXPORT_SYMBOL(iio_get_time_res
);
289 static int __init
iio_init(void)
293 /* Register sysfs bus */
294 ret
= bus_register(&iio_bus_type
);
296 pr_err("could not register bus type\n");
300 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
302 pr_err("failed to allocate char dev region\n");
303 goto error_unregister_bus_type
;
306 iio_debugfs_dentry
= debugfs_create_dir("iio", NULL
);
310 error_unregister_bus_type
:
311 bus_unregister(&iio_bus_type
);
316 static void __exit
iio_exit(void)
319 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
320 bus_unregister(&iio_bus_type
);
321 debugfs_remove(iio_debugfs_dentry
);
324 #if defined(CONFIG_DEBUG_FS)
325 static ssize_t
iio_debugfs_read_reg(struct file
*file
, char __user
*userbuf
,
326 size_t count
, loff_t
*ppos
)
328 struct iio_dev
*indio_dev
= file
->private_data
;
329 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
334 return simple_read_from_buffer(userbuf
, count
, ppos
,
335 iio_dev_opaque
->read_buf
,
336 iio_dev_opaque
->read_buf_len
);
338 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
,
339 iio_dev_opaque
->cached_reg_addr
,
342 dev_err(indio_dev
->dev
.parent
, "%s: read failed\n", __func__
);
346 iio_dev_opaque
->read_buf_len
= snprintf(iio_dev_opaque
->read_buf
,
347 sizeof(iio_dev_opaque
->read_buf
),
350 return simple_read_from_buffer(userbuf
, count
, ppos
,
351 iio_dev_opaque
->read_buf
,
352 iio_dev_opaque
->read_buf_len
);
355 static ssize_t
iio_debugfs_write_reg(struct file
*file
,
356 const char __user
*userbuf
, size_t count
, loff_t
*ppos
)
358 struct iio_dev
*indio_dev
= file
->private_data
;
359 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
364 count
= min_t(size_t, count
, (sizeof(buf
)-1));
365 if (copy_from_user(buf
, userbuf
, count
))
370 ret
= sscanf(buf
, "%i %i", ®
, &val
);
374 iio_dev_opaque
->cached_reg_addr
= reg
;
377 iio_dev_opaque
->cached_reg_addr
= reg
;
378 ret
= indio_dev
->info
->debugfs_reg_access(indio_dev
, reg
,
381 dev_err(indio_dev
->dev
.parent
, "%s: write failed\n",
393 static const struct file_operations iio_debugfs_reg_fops
= {
395 .read
= iio_debugfs_read_reg
,
396 .write
= iio_debugfs_write_reg
,
399 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
401 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
402 debugfs_remove_recursive(iio_dev_opaque
->debugfs_dentry
);
405 static void iio_device_register_debugfs(struct iio_dev
*indio_dev
)
407 struct iio_dev_opaque
*iio_dev_opaque
;
409 if (indio_dev
->info
->debugfs_reg_access
== NULL
)
412 if (!iio_debugfs_dentry
)
415 iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
417 iio_dev_opaque
->debugfs_dentry
=
418 debugfs_create_dir(dev_name(&indio_dev
->dev
),
421 debugfs_create_file("direct_reg_access", 0644,
422 iio_dev_opaque
->debugfs_dentry
, indio_dev
,
423 &iio_debugfs_reg_fops
);
426 static void iio_device_register_debugfs(struct iio_dev
*indio_dev
)
430 static void iio_device_unregister_debugfs(struct iio_dev
*indio_dev
)
433 #endif /* CONFIG_DEBUG_FS */
435 static ssize_t
iio_read_channel_ext_info(struct device
*dev
,
436 struct device_attribute
*attr
,
439 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
440 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
441 const struct iio_chan_spec_ext_info
*ext_info
;
443 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
445 return ext_info
->read(indio_dev
, ext_info
->private, this_attr
->c
, buf
);
448 static ssize_t
iio_write_channel_ext_info(struct device
*dev
,
449 struct device_attribute
*attr
,
453 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
454 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
455 const struct iio_chan_spec_ext_info
*ext_info
;
457 ext_info
= &this_attr
->c
->ext_info
[this_attr
->address
];
459 return ext_info
->write(indio_dev
, ext_info
->private,
460 this_attr
->c
, buf
, len
);
463 ssize_t
iio_enum_available_read(struct iio_dev
*indio_dev
,
464 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
466 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
473 for (i
= 0; i
< e
->num_items
; ++i
)
474 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%s ", e
->items
[i
]);
476 /* replace last space with a newline */
481 EXPORT_SYMBOL_GPL(iio_enum_available_read
);
483 ssize_t
iio_enum_read(struct iio_dev
*indio_dev
,
484 uintptr_t priv
, const struct iio_chan_spec
*chan
, char *buf
)
486 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
492 i
= e
->get(indio_dev
, chan
);
495 else if (i
>= e
->num_items
)
498 return snprintf(buf
, PAGE_SIZE
, "%s\n", e
->items
[i
]);
500 EXPORT_SYMBOL_GPL(iio_enum_read
);
502 ssize_t
iio_enum_write(struct iio_dev
*indio_dev
,
503 uintptr_t priv
, const struct iio_chan_spec
*chan
, const char *buf
,
506 const struct iio_enum
*e
= (const struct iio_enum
*)priv
;
512 ret
= __sysfs_match_string(e
->items
, e
->num_items
, buf
);
516 ret
= e
->set(indio_dev
, chan
, ret
);
517 return ret
? ret
: len
;
519 EXPORT_SYMBOL_GPL(iio_enum_write
);
521 static const struct iio_mount_matrix iio_mount_idmatrix
= {
529 static int iio_setup_mount_idmatrix(const struct device
*dev
,
530 struct iio_mount_matrix
*matrix
)
532 *matrix
= iio_mount_idmatrix
;
533 dev_info(dev
, "mounting matrix not found: using identity...\n");
537 ssize_t
iio_show_mount_matrix(struct iio_dev
*indio_dev
, uintptr_t priv
,
538 const struct iio_chan_spec
*chan
, char *buf
)
540 const struct iio_mount_matrix
*mtx
= ((iio_get_mount_matrix_t
*)
541 priv
)(indio_dev
, chan
);
547 mtx
= &iio_mount_idmatrix
;
549 return snprintf(buf
, PAGE_SIZE
, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
550 mtx
->rotation
[0], mtx
->rotation
[1], mtx
->rotation
[2],
551 mtx
->rotation
[3], mtx
->rotation
[4], mtx
->rotation
[5],
552 mtx
->rotation
[6], mtx
->rotation
[7], mtx
->rotation
[8]);
554 EXPORT_SYMBOL_GPL(iio_show_mount_matrix
);
557 * iio_read_mount_matrix() - retrieve iio device mounting matrix from
558 * device "mount-matrix" property
559 * @dev: device the mounting matrix property is assigned to
560 * @propname: device specific mounting matrix property name
561 * @matrix: where to store retrieved matrix
563 * If device is assigned no mounting matrix property, a default 3x3 identity
564 * matrix will be filled in.
566 * Return: 0 if success, or a negative error code on failure.
568 int iio_read_mount_matrix(struct device
*dev
, const char *propname
,
569 struct iio_mount_matrix
*matrix
)
571 size_t len
= ARRAY_SIZE(iio_mount_idmatrix
.rotation
);
574 err
= device_property_read_string_array(dev
, propname
,
575 matrix
->rotation
, len
);
580 /* Invalid number of matrix entries. */
584 /* Invalid matrix declaration format. */
587 /* Matrix was not declared at all: fallback to identity. */
588 return iio_setup_mount_idmatrix(dev
, matrix
);
590 EXPORT_SYMBOL(iio_read_mount_matrix
);
592 static ssize_t
__iio_format_value(char *buf
, size_t len
, unsigned int type
,
593 int size
, const int *vals
)
595 unsigned long long tmp
;
597 bool scale_db
= false;
601 return scnprintf(buf
, len
, "%d", vals
[0]);
602 case IIO_VAL_INT_PLUS_MICRO_DB
:
605 case IIO_VAL_INT_PLUS_MICRO
:
607 return scnprintf(buf
, len
, "-%d.%06u%s", abs(vals
[0]),
608 -vals
[1], scale_db
? " dB" : "");
610 return scnprintf(buf
, len
, "%d.%06u%s", vals
[0], vals
[1],
611 scale_db
? " dB" : "");
612 case IIO_VAL_INT_PLUS_NANO
:
614 return scnprintf(buf
, len
, "-%d.%09u", abs(vals
[0]),
617 return scnprintf(buf
, len
, "%d.%09u", vals
[0], vals
[1]);
618 case IIO_VAL_FRACTIONAL
:
619 tmp
= div_s64((s64
)vals
[0] * 1000000000LL, vals
[1]);
621 tmp0
= (int)div_s64_rem(tmp
, 1000000000, &tmp1
);
622 return scnprintf(buf
, len
, "%d.%09u", tmp0
, abs(tmp1
));
623 case IIO_VAL_FRACTIONAL_LOG2
:
624 tmp
= shift_right((s64
)vals
[0] * 1000000000LL, vals
[1]);
625 tmp0
= (int)div_s64_rem(tmp
, 1000000000LL, &tmp1
);
626 return scnprintf(buf
, len
, "%d.%09u", tmp0
, abs(tmp1
));
627 case IIO_VAL_INT_MULTIPLE
:
632 for (i
= 0; i
< size
; ++i
) {
633 l
+= scnprintf(&buf
[l
], len
- l
, "%d ", vals
[i
]);
640 return scnprintf(buf
, len
, "%c", (char)vals
[0]);
647 * iio_format_value() - Formats a IIO value into its string representation
648 * @buf: The buffer to which the formatted value gets written
649 * which is assumed to be big enough (i.e. PAGE_SIZE).
650 * @type: One of the IIO_VAL_* constants. This decides how the val
651 * and val2 parameters are formatted.
652 * @size: Number of IIO value entries contained in vals
653 * @vals: Pointer to the values, exact meaning depends on the
656 * Return: 0 by default, a negative number on failure or the
657 * total number of characters written for a type that belongs
658 * to the IIO_VAL_* constant.
660 ssize_t
iio_format_value(char *buf
, unsigned int type
, int size
, int *vals
)
664 len
= __iio_format_value(buf
, PAGE_SIZE
, type
, size
, vals
);
665 if (len
>= PAGE_SIZE
- 1)
668 return len
+ sprintf(buf
+ len
, "\n");
670 EXPORT_SYMBOL_GPL(iio_format_value
);
672 static ssize_t
iio_read_channel_info(struct device
*dev
,
673 struct device_attribute
*attr
,
676 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
677 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
678 int vals
[INDIO_MAX_RAW_ELEMENTS
];
682 if (indio_dev
->info
->read_raw_multi
)
683 ret
= indio_dev
->info
->read_raw_multi(indio_dev
, this_attr
->c
,
684 INDIO_MAX_RAW_ELEMENTS
,
688 ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
689 &vals
[0], &vals
[1], this_attr
->address
);
694 return iio_format_value(buf
, ret
, val_len
, vals
);
697 static ssize_t
iio_format_avail_list(char *buf
, const int *vals
,
698 int type
, int length
)
705 for (i
= 0; i
< length
; i
++) {
706 len
+= __iio_format_value(buf
+ len
, PAGE_SIZE
- len
,
708 if (len
>= PAGE_SIZE
)
711 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
714 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
716 if (len
>= PAGE_SIZE
)
721 for (i
= 0; i
< length
/ 2; i
++) {
722 len
+= __iio_format_value(buf
+ len
, PAGE_SIZE
- len
,
723 type
, 2, &vals
[i
* 2]);
724 if (len
>= PAGE_SIZE
)
726 if (i
< length
/ 2 - 1)
727 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
730 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
732 if (len
>= PAGE_SIZE
)
740 static ssize_t
iio_format_avail_range(char *buf
, const int *vals
, int type
)
745 len
= snprintf(buf
, PAGE_SIZE
, "[");
748 for (i
= 0; i
< 3; i
++) {
749 len
+= __iio_format_value(buf
+ len
, PAGE_SIZE
- len
,
751 if (len
>= PAGE_SIZE
)
754 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
757 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
759 if (len
>= PAGE_SIZE
)
764 for (i
= 0; i
< 3; i
++) {
765 len
+= __iio_format_value(buf
+ len
, PAGE_SIZE
- len
,
766 type
, 2, &vals
[i
* 2]);
767 if (len
>= PAGE_SIZE
)
770 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
773 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
775 if (len
>= PAGE_SIZE
)
783 static ssize_t
iio_read_channel_info_avail(struct device
*dev
,
784 struct device_attribute
*attr
,
787 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
788 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
794 ret
= indio_dev
->info
->read_avail(indio_dev
, this_attr
->c
,
795 &vals
, &type
, &length
,
802 return iio_format_avail_list(buf
, vals
, type
, length
);
803 case IIO_AVAIL_RANGE
:
804 return iio_format_avail_range(buf
, vals
, type
);
811 * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
812 * @str: The string to parse
813 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
814 * @integer: The integer part of the number
815 * @fract: The fractional part of the number
816 * @scale_db: True if this should parse as dB
818 * Returns 0 on success, or a negative error code if the string could not be
821 static int __iio_str_to_fixpoint(const char *str
, int fract_mult
,
822 int *integer
, int *fract
, bool scale_db
)
825 bool integer_part
= true, negative
= false;
827 if (fract_mult
== 0) {
830 return kstrtoint(str
, 0, integer
);
836 } else if (str
[0] == '+') {
841 if ('0' <= *str
&& *str
<= '9') {
843 i
= i
* 10 + *str
- '0';
845 f
+= fract_mult
* (*str
- '0');
848 } else if (*str
== '\n') {
849 if (*(str
+ 1) == '\0')
853 } else if (!strncmp(str
, " dB", sizeof(" dB") - 1) && scale_db
) {
854 /* Ignore the dB suffix */
855 str
+= sizeof(" dB") - 1;
857 } else if (!strncmp(str
, "dB", sizeof("dB") - 1) && scale_db
) {
858 /* Ignore the dB suffix */
859 str
+= sizeof("dB") - 1;
861 } else if (*str
== '.' && integer_part
) {
862 integer_part
= false;
883 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
884 * @str: The string to parse
885 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
886 * @integer: The integer part of the number
887 * @fract: The fractional part of the number
889 * Returns 0 on success, or a negative error code if the string could not be
892 int iio_str_to_fixpoint(const char *str
, int fract_mult
,
893 int *integer
, int *fract
)
895 return __iio_str_to_fixpoint(str
, fract_mult
, integer
, fract
, false);
897 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint
);
899 static ssize_t
iio_write_channel_info(struct device
*dev
,
900 struct device_attribute
*attr
,
904 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
905 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
906 int ret
, fract_mult
= 100000;
907 int integer
, fract
= 0;
908 bool is_char
= false;
909 bool scale_db
= false;
911 /* Assumes decimal - precision based on number of digits */
912 if (!indio_dev
->info
->write_raw
)
915 if (indio_dev
->info
->write_raw_get_fmt
)
916 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
917 this_attr
->c
, this_attr
->address
)) {
921 case IIO_VAL_INT_PLUS_MICRO_DB
:
924 case IIO_VAL_INT_PLUS_MICRO
:
927 case IIO_VAL_INT_PLUS_NANO
:
928 fract_mult
= 100000000;
940 if (sscanf(buf
, "%c", &ch
) != 1)
944 ret
= __iio_str_to_fixpoint(buf
, fract_mult
, &integer
, &fract
,
950 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
951 integer
, fract
, this_attr
->address
);
959 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
961 struct iio_chan_spec
const *chan
,
962 ssize_t (*readfunc
)(struct device
*dev
,
963 struct device_attribute
*attr
,
965 ssize_t (*writefunc
)(struct device
*dev
,
966 struct device_attribute
*attr
,
969 enum iio_shared_by shared_by
)
974 sysfs_attr_init(&dev_attr
->attr
);
976 /* Build up postfix of <extend_name>_<modifier>_postfix */
977 if (chan
->modified
&& (shared_by
== IIO_SEPARATE
)) {
978 if (chan
->extend_name
)
979 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
980 iio_modifier_names
[chan
985 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s",
986 iio_modifier_names
[chan
990 if (chan
->extend_name
== NULL
|| shared_by
!= IIO_SEPARATE
)
991 full_postfix
= kstrdup(postfix
, GFP_KERNEL
);
993 full_postfix
= kasprintf(GFP_KERNEL
,
998 if (full_postfix
== NULL
)
1001 if (chan
->differential
) { /* Differential can not have modifier */
1002 switch (shared_by
) {
1003 case IIO_SHARED_BY_ALL
:
1004 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
1006 case IIO_SHARED_BY_DIR
:
1007 name
= kasprintf(GFP_KERNEL
, "%s_%s",
1008 iio_direction
[chan
->output
],
1011 case IIO_SHARED_BY_TYPE
:
1012 name
= kasprintf(GFP_KERNEL
, "%s_%s-%s_%s",
1013 iio_direction
[chan
->output
],
1014 iio_chan_type_name_spec
[chan
->type
],
1015 iio_chan_type_name_spec
[chan
->type
],
1019 if (!chan
->indexed
) {
1020 WARN(1, "Differential channels must be indexed\n");
1022 goto error_free_full_postfix
;
1024 name
= kasprintf(GFP_KERNEL
,
1026 iio_direction
[chan
->output
],
1027 iio_chan_type_name_spec
[chan
->type
],
1029 iio_chan_type_name_spec
[chan
->type
],
1034 } else { /* Single ended */
1035 switch (shared_by
) {
1036 case IIO_SHARED_BY_ALL
:
1037 name
= kasprintf(GFP_KERNEL
, "%s", full_postfix
);
1039 case IIO_SHARED_BY_DIR
:
1040 name
= kasprintf(GFP_KERNEL
, "%s_%s",
1041 iio_direction
[chan
->output
],
1044 case IIO_SHARED_BY_TYPE
:
1045 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
1046 iio_direction
[chan
->output
],
1047 iio_chan_type_name_spec
[chan
->type
],
1053 name
= kasprintf(GFP_KERNEL
, "%s_%s%d_%s",
1054 iio_direction
[chan
->output
],
1055 iio_chan_type_name_spec
[chan
->type
],
1059 name
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
1060 iio_direction
[chan
->output
],
1061 iio_chan_type_name_spec
[chan
->type
],
1068 goto error_free_full_postfix
;
1070 dev_attr
->attr
.name
= name
;
1073 dev_attr
->attr
.mode
|= S_IRUGO
;
1074 dev_attr
->show
= readfunc
;
1078 dev_attr
->attr
.mode
|= S_IWUSR
;
1079 dev_attr
->store
= writefunc
;
1082 error_free_full_postfix
:
1083 kfree(full_postfix
);
1088 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
1090 kfree(dev_attr
->attr
.name
);
1093 int __iio_add_chan_devattr(const char *postfix
,
1094 struct iio_chan_spec
const *chan
,
1095 ssize_t (*readfunc
)(struct device
*dev
,
1096 struct device_attribute
*attr
,
1098 ssize_t (*writefunc
)(struct device
*dev
,
1099 struct device_attribute
*attr
,
1103 enum iio_shared_by shared_by
,
1105 struct list_head
*attr_list
)
1108 struct iio_dev_attr
*iio_attr
, *t
;
1110 iio_attr
= kzalloc(sizeof(*iio_attr
), GFP_KERNEL
);
1111 if (iio_attr
== NULL
)
1113 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
1115 readfunc
, writefunc
, shared_by
);
1117 goto error_iio_dev_attr_free
;
1119 iio_attr
->address
= mask
;
1120 list_for_each_entry(t
, attr_list
, l
)
1121 if (strcmp(t
->dev_attr
.attr
.name
,
1122 iio_attr
->dev_attr
.attr
.name
) == 0) {
1123 if (shared_by
== IIO_SEPARATE
)
1124 dev_err(dev
, "tried to double register : %s\n",
1125 t
->dev_attr
.attr
.name
);
1127 goto error_device_attr_deinit
;
1129 list_add(&iio_attr
->l
, attr_list
);
1133 error_device_attr_deinit
:
1134 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
1135 error_iio_dev_attr_free
:
1140 static int iio_device_add_info_mask_type(struct iio_dev
*indio_dev
,
1141 struct iio_chan_spec
const *chan
,
1142 enum iio_shared_by shared_by
,
1143 const long *infomask
)
1145 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1146 int i
, ret
, attrcount
= 0;
1148 for_each_set_bit(i
, infomask
, sizeof(*infomask
)*8) {
1149 if (i
>= ARRAY_SIZE(iio_chan_info_postfix
))
1151 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
],
1153 &iio_read_channel_info
,
1154 &iio_write_channel_info
,
1158 &iio_dev_opaque
->channel_attr_list
);
1159 if ((ret
== -EBUSY
) && (shared_by
!= IIO_SEPARATE
))
1169 static int iio_device_add_info_mask_type_avail(struct iio_dev
*indio_dev
,
1170 struct iio_chan_spec
const *chan
,
1171 enum iio_shared_by shared_by
,
1172 const long *infomask
)
1174 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1175 int i
, ret
, attrcount
= 0;
1176 char *avail_postfix
;
1178 for_each_set_bit(i
, infomask
, sizeof(*infomask
) * 8) {
1179 if (i
>= ARRAY_SIZE(iio_chan_info_postfix
))
1181 avail_postfix
= kasprintf(GFP_KERNEL
,
1183 iio_chan_info_postfix
[i
]);
1187 ret
= __iio_add_chan_devattr(avail_postfix
,
1189 &iio_read_channel_info_avail
,
1194 &iio_dev_opaque
->channel_attr_list
);
1195 kfree(avail_postfix
);
1196 if ((ret
== -EBUSY
) && (shared_by
!= IIO_SEPARATE
))
1206 static int iio_device_add_channel_sysfs(struct iio_dev
*indio_dev
,
1207 struct iio_chan_spec
const *chan
)
1209 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1210 int ret
, attrcount
= 0;
1211 const struct iio_chan_spec_ext_info
*ext_info
;
1213 if (chan
->channel
< 0)
1215 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
1217 &chan
->info_mask_separate
);
1222 ret
= iio_device_add_info_mask_type_avail(indio_dev
, chan
,
1225 info_mask_separate_available
);
1230 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
1232 &chan
->info_mask_shared_by_type
);
1237 ret
= iio_device_add_info_mask_type_avail(indio_dev
, chan
,
1240 info_mask_shared_by_type_available
);
1245 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
1247 &chan
->info_mask_shared_by_dir
);
1252 ret
= iio_device_add_info_mask_type_avail(indio_dev
, chan
,
1254 &chan
->info_mask_shared_by_dir_available
);
1259 ret
= iio_device_add_info_mask_type(indio_dev
, chan
,
1261 &chan
->info_mask_shared_by_all
);
1266 ret
= iio_device_add_info_mask_type_avail(indio_dev
, chan
,
1268 &chan
->info_mask_shared_by_all_available
);
1273 if (chan
->ext_info
) {
1275 for (ext_info
= chan
->ext_info
; ext_info
->name
; ext_info
++) {
1276 ret
= __iio_add_chan_devattr(ext_info
->name
,
1279 &iio_read_channel_ext_info
: NULL
,
1281 &iio_write_channel_ext_info
: NULL
,
1285 &iio_dev_opaque
->channel_attr_list
);
1287 if (ret
== -EBUSY
&& ext_info
->shared
)
1301 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1302 * @attr_list: List of IIO device attributes
1304 * This function frees the memory allocated for each of the IIO device
1305 * attributes in the list.
1307 void iio_free_chan_devattr_list(struct list_head
*attr_list
)
1309 struct iio_dev_attr
*p
, *n
;
1311 list_for_each_entry_safe(p
, n
, attr_list
, l
) {
1312 kfree(p
->dev_attr
.attr
.name
);
1318 static ssize_t
iio_show_dev_name(struct device
*dev
,
1319 struct device_attribute
*attr
,
1322 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1323 return snprintf(buf
, PAGE_SIZE
, "%s\n", indio_dev
->name
);
1326 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
1328 static ssize_t
iio_show_dev_label(struct device
*dev
,
1329 struct device_attribute
*attr
,
1332 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1333 return snprintf(buf
, PAGE_SIZE
, "%s\n", indio_dev
->label
);
1336 static DEVICE_ATTR(label
, S_IRUGO
, iio_show_dev_label
, NULL
);
1338 static ssize_t
iio_show_timestamp_clock(struct device
*dev
,
1339 struct device_attribute
*attr
,
1342 const struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1343 const clockid_t clk
= iio_device_get_clock(indio_dev
);
1348 case CLOCK_REALTIME
:
1349 name
= "realtime\n";
1350 sz
= sizeof("realtime\n");
1352 case CLOCK_MONOTONIC
:
1353 name
= "monotonic\n";
1354 sz
= sizeof("monotonic\n");
1356 case CLOCK_MONOTONIC_RAW
:
1357 name
= "monotonic_raw\n";
1358 sz
= sizeof("monotonic_raw\n");
1360 case CLOCK_REALTIME_COARSE
:
1361 name
= "realtime_coarse\n";
1362 sz
= sizeof("realtime_coarse\n");
1364 case CLOCK_MONOTONIC_COARSE
:
1365 name
= "monotonic_coarse\n";
1366 sz
= sizeof("monotonic_coarse\n");
1368 case CLOCK_BOOTTIME
:
1369 name
= "boottime\n";
1370 sz
= sizeof("boottime\n");
1374 sz
= sizeof("tai\n");
1380 memcpy(buf
, name
, sz
);
1384 static ssize_t
iio_store_timestamp_clock(struct device
*dev
,
1385 struct device_attribute
*attr
,
1386 const char *buf
, size_t len
)
1391 if (sysfs_streq(buf
, "realtime"))
1392 clk
= CLOCK_REALTIME
;
1393 else if (sysfs_streq(buf
, "monotonic"))
1394 clk
= CLOCK_MONOTONIC
;
1395 else if (sysfs_streq(buf
, "monotonic_raw"))
1396 clk
= CLOCK_MONOTONIC_RAW
;
1397 else if (sysfs_streq(buf
, "realtime_coarse"))
1398 clk
= CLOCK_REALTIME_COARSE
;
1399 else if (sysfs_streq(buf
, "monotonic_coarse"))
1400 clk
= CLOCK_MONOTONIC_COARSE
;
1401 else if (sysfs_streq(buf
, "boottime"))
1402 clk
= CLOCK_BOOTTIME
;
1403 else if (sysfs_streq(buf
, "tai"))
1408 ret
= iio_device_set_clock(dev_to_iio_dev(dev
), clk
);
1415 static DEVICE_ATTR(current_timestamp_clock
, S_IRUGO
| S_IWUSR
,
1416 iio_show_timestamp_clock
, iio_store_timestamp_clock
);
1418 static int iio_device_register_sysfs(struct iio_dev
*indio_dev
)
1420 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1421 int i
, ret
= 0, attrcount
, attrn
, attrcount_orig
= 0;
1422 struct iio_dev_attr
*p
;
1423 struct attribute
**attr
, *clk
= NULL
;
1425 /* First count elements in any existing group */
1426 if (indio_dev
->info
->attrs
) {
1427 attr
= indio_dev
->info
->attrs
->attrs
;
1428 while (*attr
++ != NULL
)
1431 attrcount
= attrcount_orig
;
1433 * New channel registration method - relies on the fact a group does
1434 * not need to be initialized if its name is NULL.
1436 if (indio_dev
->channels
)
1437 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1438 const struct iio_chan_spec
*chan
=
1439 &indio_dev
->channels
[i
];
1441 if (chan
->type
== IIO_TIMESTAMP
)
1442 clk
= &dev_attr_current_timestamp_clock
.attr
;
1444 ret
= iio_device_add_channel_sysfs(indio_dev
, chan
);
1446 goto error_clear_attrs
;
1450 if (iio_dev_opaque
->event_interface
)
1451 clk
= &dev_attr_current_timestamp_clock
.attr
;
1453 if (indio_dev
->name
)
1455 if (indio_dev
->label
)
1460 iio_dev_opaque
->chan_attr_group
.attrs
=
1461 kcalloc(attrcount
+ 1,
1462 sizeof(iio_dev_opaque
->chan_attr_group
.attrs
[0]),
1464 if (iio_dev_opaque
->chan_attr_group
.attrs
== NULL
) {
1466 goto error_clear_attrs
;
1468 /* Copy across original attributes */
1469 if (indio_dev
->info
->attrs
)
1470 memcpy(iio_dev_opaque
->chan_attr_group
.attrs
,
1471 indio_dev
->info
->attrs
->attrs
,
1472 sizeof(iio_dev_opaque
->chan_attr_group
.attrs
[0])
1474 attrn
= attrcount_orig
;
1475 /* Add all elements from the list. */
1476 list_for_each_entry(p
, &iio_dev_opaque
->channel_attr_list
, l
)
1477 iio_dev_opaque
->chan_attr_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
1478 if (indio_dev
->name
)
1479 iio_dev_opaque
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_name
.attr
;
1480 if (indio_dev
->label
)
1481 iio_dev_opaque
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_label
.attr
;
1483 iio_dev_opaque
->chan_attr_group
.attrs
[attrn
++] = clk
;
1485 indio_dev
->groups
[indio_dev
->groupcounter
++] =
1486 &iio_dev_opaque
->chan_attr_group
;
1491 iio_free_chan_devattr_list(&iio_dev_opaque
->channel_attr_list
);
1496 static void iio_device_unregister_sysfs(struct iio_dev
*indio_dev
)
1498 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1500 iio_free_chan_devattr_list(&iio_dev_opaque
->channel_attr_list
);
1501 kfree(iio_dev_opaque
->chan_attr_group
.attrs
);
1502 iio_dev_opaque
->chan_attr_group
.attrs
= NULL
;
1505 static void iio_dev_release(struct device
*device
)
1507 struct iio_dev
*indio_dev
= dev_to_iio_dev(device
);
1508 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1510 if (indio_dev
->modes
& INDIO_ALL_TRIGGERED_MODES
)
1511 iio_device_unregister_trigger_consumer(indio_dev
);
1512 iio_device_unregister_eventset(indio_dev
);
1513 iio_device_unregister_sysfs(indio_dev
);
1515 iio_buffer_put(indio_dev
->buffer
);
1517 ida_simple_remove(&iio_ida
, indio_dev
->id
);
1518 kfree(iio_dev_opaque
);
1521 struct device_type iio_device_type
= {
1522 .name
= "iio_device",
1523 .release
= iio_dev_release
,
1527 * iio_device_alloc() - allocate an iio_dev from a driver
1528 * @parent: Parent device.
1529 * @sizeof_priv: Space to allocate for private structure.
1531 struct iio_dev
*iio_device_alloc(struct device
*parent
, int sizeof_priv
)
1533 struct iio_dev_opaque
*iio_dev_opaque
;
1534 struct iio_dev
*dev
;
1537 alloc_size
= sizeof(struct iio_dev_opaque
);
1539 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
1540 alloc_size
+= sizeof_priv
;
1543 iio_dev_opaque
= kzalloc(alloc_size
, GFP_KERNEL
);
1544 if (!iio_dev_opaque
)
1547 dev
= &iio_dev_opaque
->indio_dev
;
1548 dev
->priv
= (char *)iio_dev_opaque
+
1549 ALIGN(sizeof(struct iio_dev_opaque
), IIO_ALIGN
);
1551 dev
->dev
.parent
= parent
;
1552 dev
->dev
.groups
= dev
->groups
;
1553 dev
->dev
.type
= &iio_device_type
;
1554 dev
->dev
.bus
= &iio_bus_type
;
1555 device_initialize(&dev
->dev
);
1556 dev_set_drvdata(&dev
->dev
, (void *)dev
);
1557 mutex_init(&dev
->mlock
);
1558 mutex_init(&dev
->info_exist_lock
);
1559 INIT_LIST_HEAD(&iio_dev_opaque
->channel_attr_list
);
1561 dev
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
1563 /* cannot use a dev_err as the name isn't available */
1564 pr_err("failed to get device id\n");
1565 kfree(iio_dev_opaque
);
1568 dev_set_name(&dev
->dev
, "iio:device%d", dev
->id
);
1569 INIT_LIST_HEAD(&iio_dev_opaque
->buffer_list
);
1573 EXPORT_SYMBOL(iio_device_alloc
);
1576 * iio_device_free() - free an iio_dev from a driver
1577 * @dev: the iio_dev associated with the device
1579 void iio_device_free(struct iio_dev
*dev
)
1582 put_device(&dev
->dev
);
1584 EXPORT_SYMBOL(iio_device_free
);
1586 static void devm_iio_device_release(struct device
*dev
, void *res
)
1588 iio_device_free(*(struct iio_dev
**)res
);
1592 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1593 * @parent: Device to allocate iio_dev for, and parent for this IIO device
1594 * @sizeof_priv: Space to allocate for private structure.
1596 * Managed iio_device_alloc. iio_dev allocated with this function is
1597 * automatically freed on driver detach.
1600 * Pointer to allocated iio_dev on success, NULL on failure.
1602 struct iio_dev
*devm_iio_device_alloc(struct device
*parent
, int sizeof_priv
)
1604 struct iio_dev
**ptr
, *iio_dev
;
1606 ptr
= devres_alloc(devm_iio_device_release
, sizeof(*ptr
),
1611 iio_dev
= iio_device_alloc(parent
, sizeof_priv
);
1614 devres_add(parent
, ptr
);
1621 EXPORT_SYMBOL_GPL(devm_iio_device_alloc
);
1624 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1625 * @inode: Inode structure for identifying the device in the file system
1626 * @filp: File structure for iio device used to keep and later access
1629 * Return: 0 on success or -EBUSY if the device is already opened
1631 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1633 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1634 struct iio_dev
, chrdev
);
1636 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
))
1639 iio_device_get(indio_dev
);
1641 filp
->private_data
= indio_dev
;
1647 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1648 * @inode: Inode structure pointer for the char device
1649 * @filp: File structure pointer for the char device
1651 * Return: 0 for successful release
1653 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1655 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1656 struct iio_dev
, chrdev
);
1657 clear_bit(IIO_BUSY_BIT_POS
, &indio_dev
->flags
);
1658 iio_device_put(indio_dev
);
1663 /* Somewhat of a cross file organization violation - ioctls here are actually
1665 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1667 struct iio_dev
*indio_dev
= filp
->private_data
;
1668 int __user
*ip
= (int __user
*)arg
;
1671 if (!indio_dev
->info
)
1674 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1675 fd
= iio_event_getfd(indio_dev
);
1678 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1685 static const struct file_operations iio_buffer_fileops
= {
1686 .read
= iio_buffer_read_outer_addr
,
1687 .release
= iio_chrdev_release
,
1688 .open
= iio_chrdev_open
,
1689 .poll
= iio_buffer_poll_addr
,
1690 .owner
= THIS_MODULE
,
1691 .llseek
= noop_llseek
,
1692 .unlocked_ioctl
= iio_ioctl
,
1693 .compat_ioctl
= compat_ptr_ioctl
,
1696 static int iio_check_unique_scan_index(struct iio_dev
*indio_dev
)
1699 const struct iio_chan_spec
*channels
= indio_dev
->channels
;
1701 if (!(indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
))
1704 for (i
= 0; i
< indio_dev
->num_channels
- 1; i
++) {
1705 if (channels
[i
].scan_index
< 0)
1707 for (j
= i
+ 1; j
< indio_dev
->num_channels
; j
++)
1708 if (channels
[i
].scan_index
== channels
[j
].scan_index
) {
1709 dev_err(&indio_dev
->dev
,
1710 "Duplicate scan index %d\n",
1711 channels
[i
].scan_index
);
1719 static const struct iio_buffer_setup_ops noop_ring_setup_ops
;
1721 int __iio_device_register(struct iio_dev
*indio_dev
, struct module
*this_mod
)
1725 if (!indio_dev
->info
)
1728 indio_dev
->driver_module
= this_mod
;
1729 /* If the calling driver did not initialize of_node, do it here */
1730 if (!indio_dev
->dev
.of_node
&& indio_dev
->dev
.parent
)
1731 indio_dev
->dev
.of_node
= indio_dev
->dev
.parent
->of_node
;
1733 indio_dev
->label
= of_get_property(indio_dev
->dev
.of_node
, "label",
1736 ret
= iio_check_unique_scan_index(indio_dev
);
1740 /* configure elements for the chrdev */
1741 indio_dev
->dev
.devt
= MKDEV(MAJOR(iio_devt
), indio_dev
->id
);
1743 iio_device_register_debugfs(indio_dev
);
1745 ret
= iio_buffer_alloc_sysfs_and_mask(indio_dev
);
1747 dev_err(indio_dev
->dev
.parent
,
1748 "Failed to create buffer sysfs interfaces\n");
1749 goto error_unreg_debugfs
;
1752 ret
= iio_device_register_sysfs(indio_dev
);
1754 dev_err(indio_dev
->dev
.parent
,
1755 "Failed to register sysfs interfaces\n");
1756 goto error_buffer_free_sysfs
;
1758 ret
= iio_device_register_eventset(indio_dev
);
1760 dev_err(indio_dev
->dev
.parent
,
1761 "Failed to register event set\n");
1762 goto error_free_sysfs
;
1764 if (indio_dev
->modes
& INDIO_ALL_TRIGGERED_MODES
)
1765 iio_device_register_trigger_consumer(indio_dev
);
1767 if ((indio_dev
->modes
& INDIO_ALL_BUFFER_MODES
) &&
1768 indio_dev
->setup_ops
== NULL
)
1769 indio_dev
->setup_ops
= &noop_ring_setup_ops
;
1771 cdev_init(&indio_dev
->chrdev
, &iio_buffer_fileops
);
1773 indio_dev
->chrdev
.owner
= this_mod
;
1775 ret
= cdev_device_add(&indio_dev
->chrdev
, &indio_dev
->dev
);
1777 goto error_unreg_eventset
;
1781 error_unreg_eventset
:
1782 iio_device_unregister_eventset(indio_dev
);
1784 iio_device_unregister_sysfs(indio_dev
);
1785 error_buffer_free_sysfs
:
1786 iio_buffer_free_sysfs_and_mask(indio_dev
);
1787 error_unreg_debugfs
:
1788 iio_device_unregister_debugfs(indio_dev
);
1791 EXPORT_SYMBOL(__iio_device_register
);
1794 * iio_device_unregister() - unregister a device from the IIO subsystem
1795 * @indio_dev: Device structure representing the device.
1797 void iio_device_unregister(struct iio_dev
*indio_dev
)
1799 cdev_device_del(&indio_dev
->chrdev
, &indio_dev
->dev
);
1801 mutex_lock(&indio_dev
->info_exist_lock
);
1803 iio_device_unregister_debugfs(indio_dev
);
1805 iio_disable_all_buffers(indio_dev
);
1807 indio_dev
->info
= NULL
;
1809 iio_device_wakeup_eventset(indio_dev
);
1810 iio_buffer_wakeup_poll(indio_dev
);
1812 mutex_unlock(&indio_dev
->info_exist_lock
);
1814 iio_buffer_free_sysfs_and_mask(indio_dev
);
1816 EXPORT_SYMBOL(iio_device_unregister
);
1818 static void devm_iio_device_unreg(struct device
*dev
, void *res
)
1820 iio_device_unregister(*(struct iio_dev
**)res
);
1823 int __devm_iio_device_register(struct device
*dev
, struct iio_dev
*indio_dev
,
1824 struct module
*this_mod
)
1826 struct iio_dev
**ptr
;
1829 ptr
= devres_alloc(devm_iio_device_unreg
, sizeof(*ptr
), GFP_KERNEL
);
1834 ret
= __iio_device_register(indio_dev
, this_mod
);
1836 devres_add(dev
, ptr
);
1842 EXPORT_SYMBOL_GPL(__devm_iio_device_register
);
1845 * iio_device_claim_direct_mode - Keep device in direct mode
1846 * @indio_dev: the iio_dev associated with the device
1848 * If the device is in direct mode it is guaranteed to stay
1849 * that way until iio_device_release_direct_mode() is called.
1851 * Use with iio_device_release_direct_mode()
1853 * Returns: 0 on success, -EBUSY on failure
1855 int iio_device_claim_direct_mode(struct iio_dev
*indio_dev
)
1857 mutex_lock(&indio_dev
->mlock
);
1859 if (iio_buffer_enabled(indio_dev
)) {
1860 mutex_unlock(&indio_dev
->mlock
);
1865 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode
);
1868 * iio_device_release_direct_mode - releases claim on direct mode
1869 * @indio_dev: the iio_dev associated with the device
1871 * Release the claim. Device is no longer guaranteed to stay
1874 * Use with iio_device_claim_direct_mode()
1876 void iio_device_release_direct_mode(struct iio_dev
*indio_dev
)
1878 mutex_unlock(&indio_dev
->mlock
);
1880 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode
);
1882 subsys_initcall(iio_init
);
1883 module_exit(iio_exit
);
1885 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1886 MODULE_DESCRIPTION("Industrial I/O core");
1887 MODULE_LICENSE("GPL");