xref: /linux/drivers/iio/industrialio-core.c (revision 682ca76bc60ab86824cca1b34bc865bd0094ac7e)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a980e046SJonathan Cameron /* The industrial I/O core
3a980e046SJonathan Cameron  *
4a980e046SJonathan Cameron  * Copyright (c) 2008 Jonathan Cameron
5a980e046SJonathan Cameron  *
6a980e046SJonathan Cameron  * Based on elements of hwmon and input subsystems.
7a980e046SJonathan Cameron  */
8a980e046SJonathan Cameron 
93176dd5dSSachin Kamat #define pr_fmt(fmt) "iio-core: " fmt
103176dd5dSSachin Kamat 
11a980e046SJonathan Cameron #include <linux/kernel.h>
12a980e046SJonathan Cameron #include <linux/module.h>
13a980e046SJonathan Cameron #include <linux/idr.h>
14a980e046SJonathan Cameron #include <linux/kdev_t.h>
15a980e046SJonathan Cameron #include <linux/err.h>
16a980e046SJonathan Cameron #include <linux/device.h>
17a980e046SJonathan Cameron #include <linux/fs.h>
18a980e046SJonathan Cameron #include <linux/poll.h>
19fb158971SAndy Shevchenko #include <linux/property.h>
20a980e046SJonathan Cameron #include <linux/sched.h>
21a980e046SJonathan Cameron #include <linux/wait.h>
22a980e046SJonathan Cameron #include <linux/cdev.h>
23a980e046SJonathan Cameron #include <linux/slab.h>
24a980e046SJonathan Cameron #include <linux/anon_inodes.h>
25a980e046SJonathan Cameron #include <linux/debugfs.h>
2608a33805SAlison Schofield #include <linux/mutex.h>
27a980e046SJonathan Cameron #include <linux/iio/iio.h>
286d4ebd56SAlexandru Ardelean #include <linux/iio/iio-opaque.h>
29a980e046SJonathan Cameron #include "iio_core.h"
30a980e046SJonathan Cameron #include "iio_core_trigger.h"
31a980e046SJonathan Cameron #include <linux/iio/sysfs.h>
32a980e046SJonathan Cameron #include <linux/iio/events.h>
339e69c935SLars-Peter Clausen #include <linux/iio/buffer.h>
3433dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h>
35a980e046SJonathan Cameron 
36a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */
37a980e046SJonathan Cameron static DEFINE_IDA(iio_ida);
38a980e046SJonathan Cameron 
39a980e046SJonathan Cameron static dev_t iio_devt;
40a980e046SJonathan Cameron 
41a980e046SJonathan Cameron #define IIO_DEV_MAX 256
42a980e046SJonathan Cameron struct bus_type iio_bus_type = {
43a980e046SJonathan Cameron 	.name = "iio",
44a980e046SJonathan Cameron };
45a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type);
46a980e046SJonathan Cameron 
47a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry;
48a980e046SJonathan Cameron 
49a980e046SJonathan Cameron static const char * const iio_direction[] = {
50a980e046SJonathan Cameron 	[0] = "in",
51a980e046SJonathan Cameron 	[1] = "out",
52a980e046SJonathan Cameron };
53a980e046SJonathan Cameron 
54a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = {
55a980e046SJonathan Cameron 	[IIO_VOLTAGE] = "voltage",
56a980e046SJonathan Cameron 	[IIO_CURRENT] = "current",
57a980e046SJonathan Cameron 	[IIO_POWER] = "power",
58a980e046SJonathan Cameron 	[IIO_ACCEL] = "accel",
59a980e046SJonathan Cameron 	[IIO_ANGL_VEL] = "anglvel",
60a980e046SJonathan Cameron 	[IIO_MAGN] = "magn",
61a980e046SJonathan Cameron 	[IIO_LIGHT] = "illuminance",
62a980e046SJonathan Cameron 	[IIO_INTENSITY] = "intensity",
63a980e046SJonathan Cameron 	[IIO_PROXIMITY] = "proximity",
64a980e046SJonathan Cameron 	[IIO_TEMP] = "temp",
65a980e046SJonathan Cameron 	[IIO_INCLI] = "incli",
66a980e046SJonathan Cameron 	[IIO_ROT] = "rot",
67a980e046SJonathan Cameron 	[IIO_ANGL] = "angl",
68a980e046SJonathan Cameron 	[IIO_TIMESTAMP] = "timestamp",
69a980e046SJonathan Cameron 	[IIO_CAPACITANCE] = "capacitance",
70a6b12855SMichael Hennerich 	[IIO_ALTVOLTAGE] = "altvoltage",
7121cd1fabSJon Brenner 	[IIO_CCT] = "cct",
72c4f0c693SLars-Peter Clausen 	[IIO_PRESSURE] = "pressure",
73ac216aa2SHarald Geyer 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
7455aebeb9SDaniel Baluta 	[IIO_ACTIVITY] = "activity",
75a88bfe78SIrina Tirdea 	[IIO_STEPS] = "steps",
7672c66644SIrina Tirdea 	[IIO_ENERGY] = "energy",
77cc3c9eecSIrina Tirdea 	[IIO_DISTANCE] = "distance",
785a1a9329SIrina Tirdea 	[IIO_VELOCITY] = "velocity",
798ff6b3bcSMatt Ranostay 	[IIO_CONCENTRATION] = "concentration",
80d38d5469SMatt Ranostay 	[IIO_RESISTANCE] = "resistance",
81ecb3a7ccSMatt Ranostay 	[IIO_PH] = "ph",
82d409404cSPeter Meerwald-Stadler 	[IIO_UVINDEX] = "uvindex",
834b9d2090SMatt Ranostay 	[IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
841a8f324aSWilliam Breathitt Gray 	[IIO_COUNT] = "count",
851a8f324aSWilliam Breathitt Gray 	[IIO_INDEX] = "index",
86571299d0SSong Hongyan 	[IIO_GRAVITY]  = "gravity",
873055a6cfSEugen Hristev 	[IIO_POSITIONRELATIVE]  = "positionrelative",
88c73314e6SMathieu Othacehe 	[IIO_PHASE] = "phase",
8917abc9ecSTomasz Duszynski 	[IIO_MASSCONCENTRATION] = "massconcentration",
90a980e046SJonathan Cameron };
91a980e046SJonathan Cameron 
92a980e046SJonathan Cameron static const char * const iio_modifier_names[] = {
93a980e046SJonathan Cameron 	[IIO_MOD_X] = "x",
94a980e046SJonathan Cameron 	[IIO_MOD_Y] = "y",
95a980e046SJonathan Cameron 	[IIO_MOD_Z] = "z",
964b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y] = "x&y",
974b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Z] = "x&z",
984b8d8015SPeter Meerwald 	[IIO_MOD_Y_AND_Z] = "y&z",
994b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
1004b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y] = "x|y",
1014b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Z] = "x|z",
1024b8d8015SPeter Meerwald 	[IIO_MOD_Y_OR_Z] = "y|z",
1034b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
1048f5879b2SJonathan Cameron 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
105cf82cb81SJonathan Cameron 	[IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
106a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_BOTH] = "both",
107a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_IR] = "ir",
10821cd1fabSJon Brenner 	[IIO_MOD_LIGHT_CLEAR] = "clear",
10921cd1fabSJon Brenner 	[IIO_MOD_LIGHT_RED] = "red",
11021cd1fabSJon Brenner 	[IIO_MOD_LIGHT_GREEN] = "green",
11121cd1fabSJon Brenner 	[IIO_MOD_LIGHT_BLUE] = "blue",
1122c5ff1f9SPeter Meerwald-Stadler 	[IIO_MOD_LIGHT_UV] = "uv",
113c0e4e0fdSMaxime Roussin-Bélanger 	[IIO_MOD_LIGHT_DUV] = "duv",
1145082f405SSrinivas Pandruvada 	[IIO_MOD_QUATERNION] = "quaternion",
115638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_AMBIENT] = "ambient",
116638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_OBJECT] = "object",
11711b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
11811b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE] = "from_north_true",
11911b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
12011b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
12155aebeb9SDaniel Baluta 	[IIO_MOD_RUNNING] = "running",
12255aebeb9SDaniel Baluta 	[IIO_MOD_JOGGING] = "jogging",
12355aebeb9SDaniel Baluta 	[IIO_MOD_WALKING] = "walking",
12455aebeb9SDaniel Baluta 	[IIO_MOD_STILL] = "still",
1255a1a9329SIrina Tirdea 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
1261ce87f21SLars-Peter Clausen 	[IIO_MOD_I] = "i",
1271ce87f21SLars-Peter Clausen 	[IIO_MOD_Q] = "q",
1288ff6b3bcSMatt Ranostay 	[IIO_MOD_CO2] = "co2",
1298ff6b3bcSMatt Ranostay 	[IIO_MOD_VOC] = "voc",
13017abc9ecSTomasz Duszynski 	[IIO_MOD_PM1] = "pm1",
13117abc9ecSTomasz Duszynski 	[IIO_MOD_PM2P5] = "pm2p5",
13217abc9ecSTomasz Duszynski 	[IIO_MOD_PM4] = "pm4",
13317abc9ecSTomasz Duszynski 	[IIO_MOD_PM10] = "pm10",
13425f02d32SMatt Ranostay 	[IIO_MOD_ETHANOL] = "ethanol",
13525f02d32SMatt Ranostay 	[IIO_MOD_H2] = "h2",
1364ffa22fdSMatt Ranostay 	[IIO_MOD_O2] = "o2",
137a980e046SJonathan Cameron };
138a980e046SJonathan Cameron 
139a980e046SJonathan Cameron /* relies on pairs of these shared then separate */
140a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = {
141a980e046SJonathan Cameron 	[IIO_CHAN_INFO_RAW] = "raw",
142a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PROCESSED] = "input",
143a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SCALE] = "scale",
144a980e046SJonathan Cameron 	[IIO_CHAN_INFO_OFFSET] = "offset",
145a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
146a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
147a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK] = "peak_raw",
148a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
149a980e046SJonathan Cameron 	[IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
150a980e046SJonathan Cameron 	[IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
151a980e046SJonathan Cameron 	[IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
152a980e046SJonathan Cameron 	= "filter_low_pass_3db_frequency",
1533f7f642bSMartin Fuzzey 	[IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
1543f7f642bSMartin Fuzzey 	= "filter_high_pass_3db_frequency",
155a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
156a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_FREQUENCY] = "frequency",
157a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_PHASE] = "phase",
158b65d6212SMichael Hennerich 	[IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
1597c9ab035Ssrinivas pandruvada 	[IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
1601c71a286SYe Xiang 	[IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative",
161899d90bdSPeter Meerwald 	[IIO_CHAN_INFO_INT_TIME] = "integration_time",
162a88bfe78SIrina Tirdea 	[IIO_CHAN_INFO_ENABLE] = "en",
163bcdf28fbSIrina Tirdea 	[IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
164d37f6836SIrina Tirdea 	[IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
1652f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
1662f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
167c8a85854SVianney le Clément de Saint-Marcq 	[IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
168faaa4495SIrina Tirdea 	[IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
169ddfb97d8SAndrea Merello 	[IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
170dde8ceecSCrt Mori 	[IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
1715c64990bSJonathan Cameron 	[IIO_CHAN_INFO_ZEROPOINT] = "zeropoint",
172a980e046SJonathan Cameron };
17315ea2878SJonathan Cameron /**
17415ea2878SJonathan Cameron  * iio_device_id() - query the unique ID for the device
17515ea2878SJonathan Cameron  * @indio_dev:		Device structure whose ID is being queried
17615ea2878SJonathan Cameron  *
17715ea2878SJonathan Cameron  * The IIO device ID is a unique index used for example for the naming
17815ea2878SJonathan Cameron  * of the character device /dev/iio\:device[ID]
17915ea2878SJonathan Cameron  */
18015ea2878SJonathan Cameron int iio_device_id(struct iio_dev *indio_dev)
18115ea2878SJonathan Cameron {
18215ea2878SJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
18315ea2878SJonathan Cameron 
18415ea2878SJonathan Cameron 	return iio_dev_opaque->id;
18515ea2878SJonathan Cameron }
18615ea2878SJonathan Cameron EXPORT_SYMBOL_GPL(iio_device_id);
187a980e046SJonathan Cameron 
188d9a0e73cSLars-Peter Clausen /**
1892f53b4adSMiquel Raynal  * iio_buffer_enabled() - helper function to test if the buffer is enabled
1902f53b4adSMiquel Raynal  * @indio_dev:		IIO device structure for device
1912f53b4adSMiquel Raynal  */
1922f53b4adSMiquel Raynal bool iio_buffer_enabled(struct iio_dev *indio_dev)
1932f53b4adSMiquel Raynal {
19451570c9dSMiquel Raynal 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
19551570c9dSMiquel Raynal 
19651570c9dSMiquel Raynal 	return iio_dev_opaque->currentmode
1972f53b4adSMiquel Raynal 		& (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE |
1982f53b4adSMiquel Raynal 		   INDIO_BUFFER_SOFTWARE);
1992f53b4adSMiquel Raynal }
2002f53b4adSMiquel Raynal EXPORT_SYMBOL_GPL(iio_buffer_enabled);
2012f53b4adSMiquel Raynal 
2022f53b4adSMiquel Raynal /**
203d9a0e73cSLars-Peter Clausen  * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps
204d9a0e73cSLars-Peter Clausen  * @array: array of strings
205d9a0e73cSLars-Peter Clausen  * @n: number of strings in the array
206d9a0e73cSLars-Peter Clausen  * @str: string to match with
207d9a0e73cSLars-Peter Clausen  *
208d9a0e73cSLars-Peter Clausen  * Returns index of @str in the @array or -EINVAL, similar to match_string().
209d9a0e73cSLars-Peter Clausen  * Uses sysfs_streq instead of strcmp for matching.
210d9a0e73cSLars-Peter Clausen  *
211d9a0e73cSLars-Peter Clausen  * This routine will look for a string in an array of strings.
212d9a0e73cSLars-Peter Clausen  * The search will continue until the element is found or the n-th element
213d9a0e73cSLars-Peter Clausen  * is reached, regardless of any NULL elements in the array.
214d9a0e73cSLars-Peter Clausen  */
215d9a0e73cSLars-Peter Clausen static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n,
216d9a0e73cSLars-Peter Clausen 					    const char *str)
217d9a0e73cSLars-Peter Clausen {
218d9a0e73cSLars-Peter Clausen 	const char *item;
219d9a0e73cSLars-Peter Clausen 	int index;
220d9a0e73cSLars-Peter Clausen 
221d9a0e73cSLars-Peter Clausen 	for (index = 0; index < n; index++) {
222d9a0e73cSLars-Peter Clausen 		item = array[index];
223d9a0e73cSLars-Peter Clausen 		if (!item)
224d9a0e73cSLars-Peter Clausen 			continue;
225d9a0e73cSLars-Peter Clausen 		if (sysfs_streq(item, str))
226d9a0e73cSLars-Peter Clausen 			return index;
227d9a0e73cSLars-Peter Clausen 	}
228d9a0e73cSLars-Peter Clausen 
229d9a0e73cSLars-Peter Clausen 	return -EINVAL;
230d9a0e73cSLars-Peter Clausen }
231d9a0e73cSLars-Peter Clausen 
23296fb1b67SAlexandru Ardelean #if defined(CONFIG_DEBUG_FS)
233c9561fd2SJonathan Cameron /*
23496fb1b67SAlexandru Ardelean  * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
23596fb1b67SAlexandru Ardelean  * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
23696fb1b67SAlexandru Ardelean  */
23796fb1b67SAlexandru Ardelean struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
23896fb1b67SAlexandru Ardelean {
23996fb1b67SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
2409d9ec8d0SJoe Simmons-Talbott 
24196fb1b67SAlexandru Ardelean 	return iio_dev_opaque->debugfs_dentry;
24296fb1b67SAlexandru Ardelean }
24396fb1b67SAlexandru Ardelean EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
24496fb1b67SAlexandru Ardelean #endif
24596fb1b67SAlexandru Ardelean 
246a7e57dceSSachin Kamat /**
247a7e57dceSSachin Kamat  * iio_find_channel_from_si() - get channel from its scan index
248a7e57dceSSachin Kamat  * @indio_dev:		device
249a7e57dceSSachin Kamat  * @si:			scan index to match
250a7e57dceSSachin Kamat  */
251a980e046SJonathan Cameron const struct iio_chan_spec
252a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
253a980e046SJonathan Cameron {
254a980e046SJonathan Cameron 	int i;
255a980e046SJonathan Cameron 
256a980e046SJonathan Cameron 	for (i = 0; i < indio_dev->num_channels; i++)
257a980e046SJonathan Cameron 		if (indio_dev->channels[i].scan_index == si)
258a980e046SJonathan Cameron 			return &indio_dev->channels[i];
259a980e046SJonathan Cameron 	return NULL;
260a980e046SJonathan Cameron }
261a980e046SJonathan Cameron 
262a980e046SJonathan Cameron /* This turns up an awful lot */
263a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev,
264a980e046SJonathan Cameron 			    struct device_attribute *attr,
265a980e046SJonathan Cameron 			    char *buf)
266a980e046SJonathan Cameron {
26783ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string);
268a980e046SJonathan Cameron }
269a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr);
270a980e046SJonathan Cameron 
27169f0793eSGwendal Grignou /**
27269f0793eSGwendal Grignou  * iio_device_set_clock() - Set current timestamping clock for the device
27369f0793eSGwendal Grignou  * @indio_dev: IIO device structure containing the device
27469f0793eSGwendal Grignou  * @clock_id: timestamping clock posix identifier to set.
27569f0793eSGwendal Grignou  */
27669f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
277bc2b7dabSGregor Boirie {
278bc2b7dabSGregor Boirie 	int ret;
279fa83c3baSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
280fa83c3baSAlexandru Ardelean 	const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
281bc2b7dabSGregor Boirie 
282bc2b7dabSGregor Boirie 	ret = mutex_lock_interruptible(&indio_dev->mlock);
283bc2b7dabSGregor Boirie 	if (ret)
284bc2b7dabSGregor Boirie 		return ret;
285bc2b7dabSGregor Boirie 	if ((ev_int && iio_event_enabled(ev_int)) ||
286bc2b7dabSGregor Boirie 	    iio_buffer_enabled(indio_dev)) {
287bc2b7dabSGregor Boirie 		mutex_unlock(&indio_dev->mlock);
288bc2b7dabSGregor Boirie 		return -EBUSY;
289bc2b7dabSGregor Boirie 	}
29062a486c4SJonathan Cameron 	iio_dev_opaque->clock_id = clock_id;
291bc2b7dabSGregor Boirie 	mutex_unlock(&indio_dev->mlock);
292bc2b7dabSGregor Boirie 
293bc2b7dabSGregor Boirie 	return 0;
294bc2b7dabSGregor Boirie }
29569f0793eSGwendal Grignou EXPORT_SYMBOL(iio_device_set_clock);
296bc2b7dabSGregor Boirie 
297bc2b7dabSGregor Boirie /**
29862a486c4SJonathan Cameron  * iio_device_get_clock() - Retrieve current timestamping clock for the device
29962a486c4SJonathan Cameron  * @indio_dev: IIO device structure containing the device
30062a486c4SJonathan Cameron  */
30162a486c4SJonathan Cameron clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
30262a486c4SJonathan Cameron {
30362a486c4SJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
30462a486c4SJonathan Cameron 
30562a486c4SJonathan Cameron 	return iio_dev_opaque->clock_id;
30662a486c4SJonathan Cameron }
30762a486c4SJonathan Cameron EXPORT_SYMBOL(iio_device_get_clock);
30862a486c4SJonathan Cameron 
30962a486c4SJonathan Cameron /**
310bc2b7dabSGregor Boirie  * iio_get_time_ns() - utility function to get a time stamp for events etc
311bc2b7dabSGregor Boirie  * @indio_dev: device
312bc2b7dabSGregor Boirie  */
313bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev)
314bc2b7dabSGregor Boirie {
31545e7f5d2SArnd Bergmann 	struct timespec64 tp;
316bc2b7dabSGregor Boirie 
317bc2b7dabSGregor Boirie 	switch (iio_device_get_clock(indio_dev)) {
318bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
31945e7f5d2SArnd Bergmann 		return ktime_get_real_ns();
320bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
32145e7f5d2SArnd Bergmann 		return ktime_get_ns();
322bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
32345e7f5d2SArnd Bergmann 		return ktime_get_raw_ns();
324bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
32545e7f5d2SArnd Bergmann 		return ktime_to_ns(ktime_get_coarse_real());
326bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
32745e7f5d2SArnd Bergmann 		ktime_get_coarse_ts64(&tp);
32845e7f5d2SArnd Bergmann 		return timespec64_to_ns(&tp);
329bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
3309285ec4cSJason A. Donenfeld 		return ktime_get_boottime_ns();
331bc2b7dabSGregor Boirie 	case CLOCK_TAI:
3329285ec4cSJason A. Donenfeld 		return ktime_get_clocktai_ns();
333bc2b7dabSGregor Boirie 	default:
334bc2b7dabSGregor Boirie 		BUG();
335bc2b7dabSGregor Boirie 	}
336bc2b7dabSGregor Boirie }
337bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns);
338bc2b7dabSGregor Boirie 
339a980e046SJonathan Cameron static int __init iio_init(void)
340a980e046SJonathan Cameron {
341a980e046SJonathan Cameron 	int ret;
342a980e046SJonathan Cameron 
343a980e046SJonathan Cameron 	/* Register sysfs bus */
344a980e046SJonathan Cameron 	ret  = bus_register(&iio_bus_type);
345a980e046SJonathan Cameron 	if (ret < 0) {
3463176dd5dSSachin Kamat 		pr_err("could not register bus type\n");
347a980e046SJonathan Cameron 		goto error_nothing;
348a980e046SJonathan Cameron 	}
349a980e046SJonathan Cameron 
350a980e046SJonathan Cameron 	ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
351a980e046SJonathan Cameron 	if (ret < 0) {
3523176dd5dSSachin Kamat 		pr_err("failed to allocate char dev region\n");
353a980e046SJonathan Cameron 		goto error_unregister_bus_type;
354a980e046SJonathan Cameron 	}
355a980e046SJonathan Cameron 
356a980e046SJonathan Cameron 	iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
357a980e046SJonathan Cameron 
358a980e046SJonathan Cameron 	return 0;
359a980e046SJonathan Cameron 
360a980e046SJonathan Cameron error_unregister_bus_type:
361a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
362a980e046SJonathan Cameron error_nothing:
363a980e046SJonathan Cameron 	return ret;
364a980e046SJonathan Cameron }
365a980e046SJonathan Cameron 
366a980e046SJonathan Cameron static void __exit iio_exit(void)
367a980e046SJonathan Cameron {
368a980e046SJonathan Cameron 	if (iio_devt)
369a980e046SJonathan Cameron 		unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
370a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
371a980e046SJonathan Cameron 	debugfs_remove(iio_debugfs_dentry);
372a980e046SJonathan Cameron }
373a980e046SJonathan Cameron 
374a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS)
375a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
376a980e046SJonathan Cameron 			      size_t count, loff_t *ppos)
377a980e046SJonathan Cameron {
378a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
37996fb1b67SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
380abeba59fSJoe Simmons-Talbott 	unsigned int val = 0;
381a980e046SJonathan Cameron 	int ret;
382a980e046SJonathan Cameron 
3832ddc982aSAlexandru Tachici 	if (*ppos > 0)
3842ddc982aSAlexandru Tachici 		return simple_read_from_buffer(userbuf, count, ppos,
38596fb1b67SAlexandru Ardelean 					       iio_dev_opaque->read_buf,
38696fb1b67SAlexandru Ardelean 					       iio_dev_opaque->read_buf_len);
3872ddc982aSAlexandru Tachici 
388a980e046SJonathan Cameron 	ret = indio_dev->info->debugfs_reg_access(indio_dev,
38996fb1b67SAlexandru Ardelean 						  iio_dev_opaque->cached_reg_addr,
390a980e046SJonathan Cameron 						  0, &val);
3913d62c78aSMatt Fornero 	if (ret) {
392a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
3933d62c78aSMatt Fornero 		return ret;
3943d62c78aSMatt Fornero 	}
395a980e046SJonathan Cameron 
39696fb1b67SAlexandru Ardelean 	iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
39796fb1b67SAlexandru Ardelean 					      sizeof(iio_dev_opaque->read_buf),
3982ddc982aSAlexandru Tachici 					      "0x%X\n", val);
399a980e046SJonathan Cameron 
4002ddc982aSAlexandru Tachici 	return simple_read_from_buffer(userbuf, count, ppos,
40196fb1b67SAlexandru Ardelean 				       iio_dev_opaque->read_buf,
40296fb1b67SAlexandru Ardelean 				       iio_dev_opaque->read_buf_len);
403a980e046SJonathan Cameron }
404a980e046SJonathan Cameron 
405a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file,
406a980e046SJonathan Cameron 		     const char __user *userbuf, size_t count, loff_t *ppos)
407a980e046SJonathan Cameron {
408a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
40996fb1b67SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
410abeba59fSJoe Simmons-Talbott 	unsigned int reg, val;
411a980e046SJonathan Cameron 	char buf[80];
412a980e046SJonathan Cameron 	int ret;
413a980e046SJonathan Cameron 
414a980e046SJonathan Cameron 	count = min_t(size_t, count, (sizeof(buf)-1));
415a980e046SJonathan Cameron 	if (copy_from_user(buf, userbuf, count))
416a980e046SJonathan Cameron 		return -EFAULT;
417a980e046SJonathan Cameron 
418a980e046SJonathan Cameron 	buf[count] = 0;
419a980e046SJonathan Cameron 
420a980e046SJonathan Cameron 	ret = sscanf(buf, "%i %i", &reg, &val);
421a980e046SJonathan Cameron 
422a980e046SJonathan Cameron 	switch (ret) {
423a980e046SJonathan Cameron 	case 1:
42496fb1b67SAlexandru Ardelean 		iio_dev_opaque->cached_reg_addr = reg;
425a980e046SJonathan Cameron 		break;
426a980e046SJonathan Cameron 	case 2:
42796fb1b67SAlexandru Ardelean 		iio_dev_opaque->cached_reg_addr = reg;
428a980e046SJonathan Cameron 		ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
429a980e046SJonathan Cameron 							  val, NULL);
430a980e046SJonathan Cameron 		if (ret) {
431a980e046SJonathan Cameron 			dev_err(indio_dev->dev.parent, "%s: write failed\n",
432a980e046SJonathan Cameron 				__func__);
433a980e046SJonathan Cameron 			return ret;
434a980e046SJonathan Cameron 		}
435a980e046SJonathan Cameron 		break;
436a980e046SJonathan Cameron 	default:
437a980e046SJonathan Cameron 		return -EINVAL;
438a980e046SJonathan Cameron 	}
439a980e046SJonathan Cameron 
440a980e046SJonathan Cameron 	return count;
441a980e046SJonathan Cameron }
442a980e046SJonathan Cameron 
443a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = {
4445a28c873SAxel Lin 	.open = simple_open,
445a980e046SJonathan Cameron 	.read = iio_debugfs_read_reg,
446a980e046SJonathan Cameron 	.write = iio_debugfs_write_reg,
447a980e046SJonathan Cameron };
448a980e046SJonathan Cameron 
449a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
450a980e046SJonathan Cameron {
45196fb1b67SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
4529d9ec8d0SJoe Simmons-Talbott 
45396fb1b67SAlexandru Ardelean 	debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
454a980e046SJonathan Cameron }
455a980e046SJonathan Cameron 
4568915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev)
457a980e046SJonathan Cameron {
45896fb1b67SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque;
45996fb1b67SAlexandru Ardelean 
460a980e046SJonathan Cameron 	if (indio_dev->info->debugfs_reg_access == NULL)
4618915aacaSGreg Kroah-Hartman 		return;
462a980e046SJonathan Cameron 
463abd5a2fbSAxel Lin 	if (!iio_debugfs_dentry)
4648915aacaSGreg Kroah-Hartman 		return;
465a980e046SJonathan Cameron 
46696fb1b67SAlexandru Ardelean 	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
46796fb1b67SAlexandru Ardelean 
46896fb1b67SAlexandru Ardelean 	iio_dev_opaque->debugfs_dentry =
469a980e046SJonathan Cameron 		debugfs_create_dir(dev_name(&indio_dev->dev),
470a980e046SJonathan Cameron 				   iio_debugfs_dentry);
471a980e046SJonathan Cameron 
4728915aacaSGreg Kroah-Hartman 	debugfs_create_file("direct_reg_access", 0644,
47396fb1b67SAlexandru Ardelean 			    iio_dev_opaque->debugfs_dentry, indio_dev,
4748915aacaSGreg Kroah-Hartman 			    &iio_debugfs_reg_fops);
475a980e046SJonathan Cameron }
476a980e046SJonathan Cameron #else
4778915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev)
478a980e046SJonathan Cameron {
479a980e046SJonathan Cameron }
480a980e046SJonathan Cameron 
481a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
482a980e046SJonathan Cameron {
483a980e046SJonathan Cameron }
484a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */
485a980e046SJonathan Cameron 
486a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev,
487a980e046SJonathan Cameron 				     struct device_attribute *attr,
488a980e046SJonathan Cameron 				     char *buf)
489a980e046SJonathan Cameron {
490e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
491a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
492a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
493a980e046SJonathan Cameron 
494a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
495a980e046SJonathan Cameron 
496fc6d1139SMichael Hennerich 	return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
497a980e046SJonathan Cameron }
498a980e046SJonathan Cameron 
499a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev,
500a980e046SJonathan Cameron 				     struct device_attribute *attr,
501a980e046SJonathan Cameron 				     const char *buf,
502a980e046SJonathan Cameron 					 size_t len)
503a980e046SJonathan Cameron {
504e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
505a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
506a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
507a980e046SJonathan Cameron 
508a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
509a980e046SJonathan Cameron 
510fc6d1139SMichael Hennerich 	return ext_info->write(indio_dev, ext_info->private,
511fc6d1139SMichael Hennerich 			       this_attr->c, buf, len);
512a980e046SJonathan Cameron }
513a980e046SJonathan Cameron 
5145212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
5155212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
5165212cc8aSLars-Peter Clausen {
5175212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
5185212cc8aSLars-Peter Clausen 	unsigned int i;
5195212cc8aSLars-Peter Clausen 	size_t len = 0;
5205212cc8aSLars-Peter Clausen 
5215212cc8aSLars-Peter Clausen 	if (!e->num_items)
5225212cc8aSLars-Peter Clausen 		return 0;
5235212cc8aSLars-Peter Clausen 
524d9a0e73cSLars-Peter Clausen 	for (i = 0; i < e->num_items; ++i) {
525d9a0e73cSLars-Peter Clausen 		if (!e->items[i])
526d9a0e73cSLars-Peter Clausen 			continue;
5270207483bSLars-Peter Clausen 		len += sysfs_emit_at(buf, len, "%s ", e->items[i]);
528d9a0e73cSLars-Peter Clausen 	}
5295212cc8aSLars-Peter Clausen 
5305212cc8aSLars-Peter Clausen 	/* replace last space with a newline */
5315212cc8aSLars-Peter Clausen 	buf[len - 1] = '\n';
5325212cc8aSLars-Peter Clausen 
5335212cc8aSLars-Peter Clausen 	return len;
5345212cc8aSLars-Peter Clausen }
5355212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read);
5365212cc8aSLars-Peter Clausen 
5375212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev,
5385212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
5395212cc8aSLars-Peter Clausen {
5405212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
5415212cc8aSLars-Peter Clausen 	int i;
5425212cc8aSLars-Peter Clausen 
5435212cc8aSLars-Peter Clausen 	if (!e->get)
5445212cc8aSLars-Peter Clausen 		return -EINVAL;
5455212cc8aSLars-Peter Clausen 
5465212cc8aSLars-Peter Clausen 	i = e->get(indio_dev, chan);
5475212cc8aSLars-Peter Clausen 	if (i < 0)
5485212cc8aSLars-Peter Clausen 		return i;
549d9a0e73cSLars-Peter Clausen 	else if (i >= e->num_items || !e->items[i])
5505212cc8aSLars-Peter Clausen 		return -EINVAL;
5515212cc8aSLars-Peter Clausen 
55283ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%s\n", e->items[i]);
5535212cc8aSLars-Peter Clausen }
5545212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read);
5555212cc8aSLars-Peter Clausen 
5565212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev,
5575212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
5585212cc8aSLars-Peter Clausen 	size_t len)
5595212cc8aSLars-Peter Clausen {
5605212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
5615212cc8aSLars-Peter Clausen 	int ret;
5625212cc8aSLars-Peter Clausen 
5635212cc8aSLars-Peter Clausen 	if (!e->set)
5645212cc8aSLars-Peter Clausen 		return -EINVAL;
5655212cc8aSLars-Peter Clausen 
566d9a0e73cSLars-Peter Clausen 	ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf);
56702e9a0ffSAndy Shevchenko 	if (ret < 0)
56802e9a0ffSAndy Shevchenko 		return ret;
5695212cc8aSLars-Peter Clausen 
57002e9a0ffSAndy Shevchenko 	ret = e->set(indio_dev, chan, ret);
5715212cc8aSLars-Peter Clausen 	return ret ? ret : len;
5725212cc8aSLars-Peter Clausen }
5735212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write);
5745212cc8aSLars-Peter Clausen 
575dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = {
576dfc57732SGregor Boirie 	.rotation = {
577dfc57732SGregor Boirie 		"1", "0", "0",
578dfc57732SGregor Boirie 		"0", "1", "0",
579dfc57732SGregor Boirie 		"0", "0", "1"
580dfc57732SGregor Boirie 	}
581dfc57732SGregor Boirie };
582dfc57732SGregor Boirie 
583dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev,
584dfc57732SGregor Boirie 				    struct iio_mount_matrix *matrix)
585dfc57732SGregor Boirie {
586dfc57732SGregor Boirie 	*matrix = iio_mount_idmatrix;
587dfc57732SGregor Boirie 	dev_info(dev, "mounting matrix not found: using identity...\n");
588dfc57732SGregor Boirie 	return 0;
589dfc57732SGregor Boirie }
590dfc57732SGregor Boirie 
591dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
592dfc57732SGregor Boirie 			      const struct iio_chan_spec *chan, char *buf)
593dfc57732SGregor Boirie {
594dfc57732SGregor Boirie 	const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
595dfc57732SGregor Boirie 					      priv)(indio_dev, chan);
596dfc57732SGregor Boirie 
597dfc57732SGregor Boirie 	if (IS_ERR(mtx))
598dfc57732SGregor Boirie 		return PTR_ERR(mtx);
599dfc57732SGregor Boirie 
600dfc57732SGregor Boirie 	if (!mtx)
601dfc57732SGregor Boirie 		mtx = &iio_mount_idmatrix;
602dfc57732SGregor Boirie 
60383ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
604dfc57732SGregor Boirie 			  mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
605dfc57732SGregor Boirie 			  mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
606dfc57732SGregor Boirie 			  mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
607dfc57732SGregor Boirie }
608dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
609dfc57732SGregor Boirie 
610dfc57732SGregor Boirie /**
611fb158971SAndy Shevchenko  * iio_read_mount_matrix() - retrieve iio device mounting matrix from
612fb158971SAndy Shevchenko  *                           device "mount-matrix" property
613dfc57732SGregor Boirie  * @dev:	device the mounting matrix property is assigned to
614dfc57732SGregor Boirie  * @matrix:	where to store retrieved matrix
615dfc57732SGregor Boirie  *
616dfc57732SGregor Boirie  * If device is assigned no mounting matrix property, a default 3x3 identity
617dfc57732SGregor Boirie  * matrix will be filled in.
618dfc57732SGregor Boirie  *
619dfc57732SGregor Boirie  * Return: 0 if success, or a negative error code on failure.
620dfc57732SGregor Boirie  */
621b892770aSAndy Shevchenko int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix)
622dfc57732SGregor Boirie {
623fb158971SAndy Shevchenko 	size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
624fb158971SAndy Shevchenko 	int err;
625dfc57732SGregor Boirie 
626b892770aSAndy Shevchenko 	err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len);
627fb158971SAndy Shevchenko 	if (err == len)
628dfc57732SGregor Boirie 		return 0;
629dfc57732SGregor Boirie 
630dfc57732SGregor Boirie 	if (err >= 0)
631dfc57732SGregor Boirie 		/* Invalid number of matrix entries. */
632dfc57732SGregor Boirie 		return -EINVAL;
633dfc57732SGregor Boirie 
634dfc57732SGregor Boirie 	if (err != -EINVAL)
635dfc57732SGregor Boirie 		/* Invalid matrix declaration format. */
636dfc57732SGregor Boirie 		return err;
637dfc57732SGregor Boirie 
638dfc57732SGregor Boirie 	/* Matrix was not declared at all: fallback to identity. */
639dfc57732SGregor Boirie 	return iio_setup_mount_idmatrix(dev, matrix);
640dfc57732SGregor Boirie }
641fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix);
642dfc57732SGregor Boirie 
6436b92ba0aSLars-Peter Clausen static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
64451239600SJonathan Cameron 				  int size, const int *vals)
64551239600SJonathan Cameron {
64651239600SJonathan Cameron 	int tmp0, tmp1;
6470e7a3978SAnand Ashok Dumbre 	s64 tmp2;
64851239600SJonathan Cameron 	bool scale_db = false;
64951239600SJonathan Cameron 
65051239600SJonathan Cameron 	switch (type) {
65151239600SJonathan Cameron 	case IIO_VAL_INT:
6526b92ba0aSLars-Peter Clausen 		return sysfs_emit_at(buf, offset, "%d", vals[0]);
65351239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO_DB:
65451239600SJonathan Cameron 		scale_db = true;
655df561f66SGustavo A. R. Silva 		fallthrough;
65651239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO:
65751239600SJonathan Cameron 		if (vals[1] < 0)
6586b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "-%d.%06u%s",
6596b92ba0aSLars-Peter Clausen 					     abs(vals[0]), -vals[1],
66051239600SJonathan Cameron 					     scale_db ? " dB" : "");
6616b92ba0aSLars-Peter Clausen 		else
6626b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0],
6636b92ba0aSLars-Peter Clausen 					     vals[1], scale_db ? " dB" : "");
66451239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_NANO:
66551239600SJonathan Cameron 		if (vals[1] < 0)
6666b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "-%d.%09u",
6676b92ba0aSLars-Peter Clausen 					     abs(vals[0]), -vals[1]);
66851239600SJonathan Cameron 		else
6696b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "%d.%09u", vals[0],
6706b92ba0aSLars-Peter Clausen 					     vals[1]);
67151239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL:
6720e7a3978SAnand Ashok Dumbre 		tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
67351239600SJonathan Cameron 		tmp1 = vals[1];
6740e7a3978SAnand Ashok Dumbre 		tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
6750e7a3978SAnand Ashok Dumbre 		if ((tmp2 < 0) && (tmp0 == 0))
6766b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
6770e7a3978SAnand Ashok Dumbre 		else
6786b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
6796b92ba0aSLars-Peter Clausen 					     abs(tmp1));
68051239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL_LOG2:
6812646a95dSLars-Peter Clausen 		tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
6822646a95dSLars-Peter Clausen 		tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
68338a52cdeSLars-Peter Clausen 		if (tmp0 == 0 && tmp2 < 0)
6846b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
68538a52cdeSLars-Peter Clausen 		else
6866b92ba0aSLars-Peter Clausen 			return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
6876b92ba0aSLars-Peter Clausen 					     abs(tmp1));
68851239600SJonathan Cameron 	case IIO_VAL_INT_MULTIPLE:
68951239600SJonathan Cameron 	{
69051239600SJonathan Cameron 		int i;
69151239600SJonathan Cameron 		int l = 0;
69251239600SJonathan Cameron 
6936b92ba0aSLars-Peter Clausen 		for (i = 0; i < size; ++i)
6946b92ba0aSLars-Peter Clausen 			l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]);
69551239600SJonathan Cameron 		return l;
69651239600SJonathan Cameron 	}
6978cb34036SAndrea Merello 	case IIO_VAL_CHAR:
6986b92ba0aSLars-Peter Clausen 		return sysfs_emit_at(buf, offset, "%c", (char)vals[0]);
6996bb835f3SAndriy Tryshnivskyy 	case IIO_VAL_INT_64:
7006bb835f3SAndriy Tryshnivskyy 		tmp2 = (s64)((((u64)vals[1]) << 32) | (u32)vals[0]);
7016bb835f3SAndriy Tryshnivskyy 		return sysfs_emit_at(buf, offset, "%lld", tmp2);
70251239600SJonathan Cameron 	default:
70351239600SJonathan Cameron 		return 0;
70451239600SJonathan Cameron 	}
70551239600SJonathan Cameron }
70651239600SJonathan Cameron 
7073661f3f5SLars-Peter Clausen /**
7083661f3f5SLars-Peter Clausen  * iio_format_value() - Formats a IIO value into its string representation
7092498dcf6SCristina Opriceana  * @buf:	The buffer to which the formatted value gets written
71051239600SJonathan Cameron  *		which is assumed to be big enough (i.e. PAGE_SIZE).
711c175cb7cSRandy Dunlap  * @type:	One of the IIO_VAL_* constants. This decides how the val
7122498dcf6SCristina Opriceana  *		and val2 parameters are formatted.
7132498dcf6SCristina Opriceana  * @size:	Number of IIO value entries contained in vals
7142498dcf6SCristina Opriceana  * @vals:	Pointer to the values, exact meaning depends on the
7152498dcf6SCristina Opriceana  *		type parameter.
7162498dcf6SCristina Opriceana  *
7172498dcf6SCristina Opriceana  * Return: 0 by default, a negative number on failure or the
7182498dcf6SCristina Opriceana  *	   total number of characters written for a type that belongs
719c175cb7cSRandy Dunlap  *	   to the IIO_VAL_* constant.
7203661f3f5SLars-Peter Clausen  */
7219fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
722a980e046SJonathan Cameron {
72351239600SJonathan Cameron 	ssize_t len;
724a980e046SJonathan Cameron 
7256b92ba0aSLars-Peter Clausen 	len = __iio_format_value(buf, 0, type, size, vals);
72651239600SJonathan Cameron 	if (len >= PAGE_SIZE - 1)
72751239600SJonathan Cameron 		return -EFBIG;
7289fbfb4b3SSrinivas Pandruvada 
7296b92ba0aSLars-Peter Clausen 	return len + sysfs_emit_at(buf, len, "\n");
73067eedba3SMichael Hennerich }
7317d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value);
732a980e046SJonathan Cameron 
7331d4ef9b3SCristian Pop static ssize_t iio_read_channel_label(struct device *dev,
7341d4ef9b3SCristian Pop 				      struct device_attribute *attr,
7351d4ef9b3SCristian Pop 				      char *buf)
7361d4ef9b3SCristian Pop {
7371d4ef9b3SCristian Pop 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
7381d4ef9b3SCristian Pop 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
7391d4ef9b3SCristian Pop 
74013efdc3dSPaul Cercueil 	if (indio_dev->info->read_label)
7411d4ef9b3SCristian Pop 		return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
74213efdc3dSPaul Cercueil 
74313efdc3dSPaul Cercueil 	if (this_attr->c->extend_name)
744c3154defSLars-Peter Clausen 		return sysfs_emit(buf, "%s\n", this_attr->c->extend_name);
74513efdc3dSPaul Cercueil 
74613efdc3dSPaul Cercueil 	return -EINVAL;
7471d4ef9b3SCristian Pop }
7481d4ef9b3SCristian Pop 
7493661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev,
7503661f3f5SLars-Peter Clausen 				     struct device_attribute *attr,
7513661f3f5SLars-Peter Clausen 				     char *buf)
7523661f3f5SLars-Peter Clausen {
7533661f3f5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
7543661f3f5SLars-Peter Clausen 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
7559fbfb4b3SSrinivas Pandruvada 	int vals[INDIO_MAX_RAW_ELEMENTS];
7569fbfb4b3SSrinivas Pandruvada 	int ret;
7579fbfb4b3SSrinivas Pandruvada 	int val_len = 2;
7589fbfb4b3SSrinivas Pandruvada 
7599fbfb4b3SSrinivas Pandruvada 	if (indio_dev->info->read_raw_multi)
7609fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
7619fbfb4b3SSrinivas Pandruvada 							INDIO_MAX_RAW_ELEMENTS,
7629fbfb4b3SSrinivas Pandruvada 							vals, &val_len,
7639fbfb4b3SSrinivas Pandruvada 							this_attr->address);
7649fbfb4b3SSrinivas Pandruvada 	else
7659fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
7669fbfb4b3SSrinivas Pandruvada 				    &vals[0], &vals[1], this_attr->address);
7673661f3f5SLars-Peter Clausen 
7683661f3f5SLars-Peter Clausen 	if (ret < 0)
7693661f3f5SLars-Peter Clausen 		return ret;
7703661f3f5SLars-Peter Clausen 
7719fbfb4b3SSrinivas Pandruvada 	return iio_format_value(buf, ret, val_len, vals);
7723661f3f5SLars-Peter Clausen }
7733661f3f5SLars-Peter Clausen 
774eda20ba1SLars-Peter Clausen static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
775eda20ba1SLars-Peter Clausen 			       const char *prefix, const char *suffix)
77651239600SJonathan Cameron {
777eda20ba1SLars-Peter Clausen 	ssize_t len;
778e08b60d3SLars-Peter Clausen 	int stride;
77951239600SJonathan Cameron 	int i;
780eda20ba1SLars-Peter Clausen 
78151239600SJonathan Cameron 	switch (type) {
78251239600SJonathan Cameron 	case IIO_VAL_INT:
783e08b60d3SLars-Peter Clausen 		stride = 1;
78451239600SJonathan Cameron 		break;
78551239600SJonathan Cameron 	default:
786e08b60d3SLars-Peter Clausen 		stride = 2;
787e08b60d3SLars-Peter Clausen 		break;
788e08b60d3SLars-Peter Clausen 	}
789e08b60d3SLars-Peter Clausen 
7906b92ba0aSLars-Peter Clausen 	len = sysfs_emit(buf, prefix);
791e08b60d3SLars-Peter Clausen 
792e08b60d3SLars-Peter Clausen 	for (i = 0; i <= length - stride; i += stride) {
793e08b60d3SLars-Peter Clausen 		if (i != 0) {
7946b92ba0aSLars-Peter Clausen 			len += sysfs_emit_at(buf, len, " ");
79551239600SJonathan Cameron 			if (len >= PAGE_SIZE)
79651239600SJonathan Cameron 				return -EFBIG;
79751239600SJonathan Cameron 		}
798e08b60d3SLars-Peter Clausen 
7996b92ba0aSLars-Peter Clausen 		len += __iio_format_value(buf, len, type, stride, &vals[i]);
800e08b60d3SLars-Peter Clausen 		if (len >= PAGE_SIZE)
801e08b60d3SLars-Peter Clausen 			return -EFBIG;
80251239600SJonathan Cameron 	}
80351239600SJonathan Cameron 
8046b92ba0aSLars-Peter Clausen 	len += sysfs_emit_at(buf, len, "%s\n", suffix);
805e08b60d3SLars-Peter Clausen 
80651239600SJonathan Cameron 	return len;
80751239600SJonathan Cameron }
80851239600SJonathan Cameron 
809eda20ba1SLars-Peter Clausen static ssize_t iio_format_avail_list(char *buf, const int *vals,
810eda20ba1SLars-Peter Clausen 				     int type, int length)
811eda20ba1SLars-Peter Clausen {
812eda20ba1SLars-Peter Clausen 
813eda20ba1SLars-Peter Clausen 	return iio_format_list(buf, vals, type, length, "", "");
814eda20ba1SLars-Peter Clausen }
815eda20ba1SLars-Peter Clausen 
81651239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
81751239600SJonathan Cameron {
8185e1f9185SFawzi Khaber 	int length;
8195e1f9185SFawzi Khaber 
8205e1f9185SFawzi Khaber 	/*
8215e1f9185SFawzi Khaber 	 * length refers to the array size , not the number of elements.
8225e1f9185SFawzi Khaber 	 * The purpose is to print the range [min , step ,max] so length should
8235e1f9185SFawzi Khaber 	 * be 3 in case of int, and 6 for other types.
8245e1f9185SFawzi Khaber 	 */
8255e1f9185SFawzi Khaber 	switch (type) {
8265e1f9185SFawzi Khaber 	case IIO_VAL_INT:
8275e1f9185SFawzi Khaber 		length = 3;
8285e1f9185SFawzi Khaber 		break;
8295e1f9185SFawzi Khaber 	default:
8305e1f9185SFawzi Khaber 		length = 6;
8315e1f9185SFawzi Khaber 		break;
8325e1f9185SFawzi Khaber 	}
8335e1f9185SFawzi Khaber 
8345e1f9185SFawzi Khaber 	return iio_format_list(buf, vals, type, length, "[", "]");
83551239600SJonathan Cameron }
83651239600SJonathan Cameron 
83751239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev,
83851239600SJonathan Cameron 					   struct device_attribute *attr,
83951239600SJonathan Cameron 					   char *buf)
84051239600SJonathan Cameron {
84151239600SJonathan Cameron 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
84251239600SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
84351239600SJonathan Cameron 	const int *vals;
84451239600SJonathan Cameron 	int ret;
84551239600SJonathan Cameron 	int length;
84651239600SJonathan Cameron 	int type;
84751239600SJonathan Cameron 
84851239600SJonathan Cameron 	ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
84951239600SJonathan Cameron 					  &vals, &type, &length,
85051239600SJonathan Cameron 					  this_attr->address);
85151239600SJonathan Cameron 
85251239600SJonathan Cameron 	if (ret < 0)
85351239600SJonathan Cameron 		return ret;
85451239600SJonathan Cameron 	switch (ret) {
85551239600SJonathan Cameron 	case IIO_AVAIL_LIST:
85651239600SJonathan Cameron 		return iio_format_avail_list(buf, vals, type, length);
85751239600SJonathan Cameron 	case IIO_AVAIL_RANGE:
85851239600SJonathan Cameron 		return iio_format_avail_range(buf, vals, type);
85951239600SJonathan Cameron 	default:
86051239600SJonathan Cameron 		return -EINVAL;
86151239600SJonathan Cameron 	}
86251239600SJonathan Cameron }
86351239600SJonathan Cameron 
8646807d721SLars-Peter Clausen /**
865b8528224SBeniamin Bia  * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
8666807d721SLars-Peter Clausen  * @str: The string to parse
8676807d721SLars-Peter Clausen  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
8686807d721SLars-Peter Clausen  * @integer: The integer part of the number
8696807d721SLars-Peter Clausen  * @fract: The fractional part of the number
870b8528224SBeniamin Bia  * @scale_db: True if this should parse as dB
8716807d721SLars-Peter Clausen  *
8726807d721SLars-Peter Clausen  * Returns 0 on success, or a negative error code if the string could not be
8736807d721SLars-Peter Clausen  * parsed.
8746807d721SLars-Peter Clausen  */
875b8528224SBeniamin Bia static int __iio_str_to_fixpoint(const char *str, int fract_mult,
876b8528224SBeniamin Bia 				 int *integer, int *fract, bool scale_db)
8776807d721SLars-Peter Clausen {
8786807d721SLars-Peter Clausen 	int i = 0, f = 0;
8796807d721SLars-Peter Clausen 	bool integer_part = true, negative = false;
8806807d721SLars-Peter Clausen 
881f47dff32SSean Nyekjaer 	if (fract_mult == 0) {
882f47dff32SSean Nyekjaer 		*fract = 0;
883f47dff32SSean Nyekjaer 
884f47dff32SSean Nyekjaer 		return kstrtoint(str, 0, integer);
885f47dff32SSean Nyekjaer 	}
886f47dff32SSean Nyekjaer 
8876807d721SLars-Peter Clausen 	if (str[0] == '-') {
8886807d721SLars-Peter Clausen 		negative = true;
8896807d721SLars-Peter Clausen 		str++;
8906807d721SLars-Peter Clausen 	} else if (str[0] == '+') {
8916807d721SLars-Peter Clausen 		str++;
8926807d721SLars-Peter Clausen 	}
8936807d721SLars-Peter Clausen 
8946807d721SLars-Peter Clausen 	while (*str) {
8956807d721SLars-Peter Clausen 		if ('0' <= *str && *str <= '9') {
8966807d721SLars-Peter Clausen 			if (integer_part) {
8976807d721SLars-Peter Clausen 				i = i * 10 + *str - '0';
8986807d721SLars-Peter Clausen 			} else {
8996807d721SLars-Peter Clausen 				f += fract_mult * (*str - '0');
9006807d721SLars-Peter Clausen 				fract_mult /= 10;
9016807d721SLars-Peter Clausen 			}
9026807d721SLars-Peter Clausen 		} else if (*str == '\n') {
9036807d721SLars-Peter Clausen 			if (*(str + 1) == '\0')
9046807d721SLars-Peter Clausen 				break;
9056807d721SLars-Peter Clausen 			return -EINVAL;
906b8528224SBeniamin Bia 		} else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
907b8528224SBeniamin Bia 			/* Ignore the dB suffix */
908b8528224SBeniamin Bia 			str += sizeof(" dB") - 1;
909b8528224SBeniamin Bia 			continue;
910b8528224SBeniamin Bia 		} else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
911b8528224SBeniamin Bia 			/* Ignore the dB suffix */
912b8528224SBeniamin Bia 			str += sizeof("dB") - 1;
913b8528224SBeniamin Bia 			continue;
9146807d721SLars-Peter Clausen 		} else if (*str == '.' && integer_part) {
9156807d721SLars-Peter Clausen 			integer_part = false;
9166807d721SLars-Peter Clausen 		} else {
9176807d721SLars-Peter Clausen 			return -EINVAL;
9186807d721SLars-Peter Clausen 		}
9196807d721SLars-Peter Clausen 		str++;
9206807d721SLars-Peter Clausen 	}
9216807d721SLars-Peter Clausen 
9226807d721SLars-Peter Clausen 	if (negative) {
9236807d721SLars-Peter Clausen 		if (i)
9246807d721SLars-Peter Clausen 			i = -i;
9256807d721SLars-Peter Clausen 		else
9266807d721SLars-Peter Clausen 			f = -f;
9276807d721SLars-Peter Clausen 	}
9286807d721SLars-Peter Clausen 
9296807d721SLars-Peter Clausen 	*integer = i;
9306807d721SLars-Peter Clausen 	*fract = f;
9316807d721SLars-Peter Clausen 
9326807d721SLars-Peter Clausen 	return 0;
9336807d721SLars-Peter Clausen }
934b8528224SBeniamin Bia 
935b8528224SBeniamin Bia /**
936b8528224SBeniamin Bia  * iio_str_to_fixpoint() - Parse a fixed-point number from a string
937b8528224SBeniamin Bia  * @str: The string to parse
938b8528224SBeniamin Bia  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
939b8528224SBeniamin Bia  * @integer: The integer part of the number
940b8528224SBeniamin Bia  * @fract: The fractional part of the number
941b8528224SBeniamin Bia  *
942b8528224SBeniamin Bia  * Returns 0 on success, or a negative error code if the string could not be
943b8528224SBeniamin Bia  * parsed.
944b8528224SBeniamin Bia  */
945b8528224SBeniamin Bia int iio_str_to_fixpoint(const char *str, int fract_mult,
946b8528224SBeniamin Bia 			int *integer, int *fract)
947b8528224SBeniamin Bia {
948b8528224SBeniamin Bia 	return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
949b8528224SBeniamin Bia }
9506807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
9516807d721SLars-Peter Clausen 
952a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev,
953a980e046SJonathan Cameron 				      struct device_attribute *attr,
954a980e046SJonathan Cameron 				      const char *buf,
955a980e046SJonathan Cameron 				      size_t len)
956a980e046SJonathan Cameron {
957e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
958a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
9596807d721SLars-Peter Clausen 	int ret, fract_mult = 100000;
9608cb34036SAndrea Merello 	int integer, fract = 0;
9618cb34036SAndrea Merello 	bool is_char = false;
962b8528224SBeniamin Bia 	bool scale_db = false;
963a980e046SJonathan Cameron 
964a980e046SJonathan Cameron 	/* Assumes decimal - precision based on number of digits */
965a980e046SJonathan Cameron 	if (!indio_dev->info->write_raw)
966a980e046SJonathan Cameron 		return -EINVAL;
967a980e046SJonathan Cameron 
968a980e046SJonathan Cameron 	if (indio_dev->info->write_raw_get_fmt)
969a980e046SJonathan Cameron 		switch (indio_dev->info->write_raw_get_fmt(indio_dev,
970a980e046SJonathan Cameron 			this_attr->c, this_attr->address)) {
971f47dff32SSean Nyekjaer 		case IIO_VAL_INT:
972f47dff32SSean Nyekjaer 			fract_mult = 0;
973f47dff32SSean Nyekjaer 			break;
974b8528224SBeniamin Bia 		case IIO_VAL_INT_PLUS_MICRO_DB:
975b8528224SBeniamin Bia 			scale_db = true;
976df561f66SGustavo A. R. Silva 			fallthrough;
977a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_MICRO:
978a980e046SJonathan Cameron 			fract_mult = 100000;
979a980e046SJonathan Cameron 			break;
980a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_NANO:
981a980e046SJonathan Cameron 			fract_mult = 100000000;
982a980e046SJonathan Cameron 			break;
9838cb34036SAndrea Merello 		case IIO_VAL_CHAR:
9848cb34036SAndrea Merello 			is_char = true;
9858cb34036SAndrea Merello 			break;
986a980e046SJonathan Cameron 		default:
987a980e046SJonathan Cameron 			return -EINVAL;
988a980e046SJonathan Cameron 		}
989a980e046SJonathan Cameron 
9908cb34036SAndrea Merello 	if (is_char) {
9918cb34036SAndrea Merello 		char ch;
9928cb34036SAndrea Merello 
9938cb34036SAndrea Merello 		if (sscanf(buf, "%c", &ch) != 1)
9948cb34036SAndrea Merello 			return -EINVAL;
9958cb34036SAndrea Merello 		integer = ch;
9968cb34036SAndrea Merello 	} else {
997b8528224SBeniamin Bia 		ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
998b8528224SBeniamin Bia 					    scale_db);
999b8528224SBeniamin Bia 		if (ret)
1000b8528224SBeniamin Bia 			return ret;
10015403f280SMircea Caprioru 	}
1002a980e046SJonathan Cameron 
1003a980e046SJonathan Cameron 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
1004a980e046SJonathan Cameron 					 integer, fract, this_attr->address);
1005a980e046SJonathan Cameron 	if (ret)
1006a980e046SJonathan Cameron 		return ret;
1007a980e046SJonathan Cameron 
1008a980e046SJonathan Cameron 	return len;
1009a980e046SJonathan Cameron }
1010a980e046SJonathan Cameron 
1011a980e046SJonathan Cameron static
1012a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr,
1013a980e046SJonathan Cameron 			   const char *postfix,
1014a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
1015a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
1016a980e046SJonathan Cameron 					       struct device_attribute *attr,
1017a980e046SJonathan Cameron 					       char *buf),
1018a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
1019a980e046SJonathan Cameron 						struct device_attribute *attr,
1020a980e046SJonathan Cameron 						const char *buf,
1021a980e046SJonathan Cameron 						size_t len),
10223704432fSJonathan Cameron 			   enum iio_shared_by shared_by)
1023a980e046SJonathan Cameron {
10243704432fSJonathan Cameron 	int ret = 0;
10257bbcf7e1SLars-Peter Clausen 	char *name = NULL;
10263704432fSJonathan Cameron 	char *full_postfix;
10279d9ec8d0SJoe Simmons-Talbott 
1028a980e046SJonathan Cameron 	sysfs_attr_init(&dev_attr->attr);
1029a980e046SJonathan Cameron 
1030a980e046SJonathan Cameron 	/* Build up postfix of <extend_name>_<modifier>_postfix */
10313704432fSJonathan Cameron 	if (chan->modified && (shared_by == IIO_SEPARATE)) {
1032a980e046SJonathan Cameron 		if (chan->extend_name)
1033a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
1034a980e046SJonathan Cameron 						 iio_modifier_names[chan
1035a980e046SJonathan Cameron 								    ->channel2],
1036a980e046SJonathan Cameron 						 chan->extend_name,
1037a980e046SJonathan Cameron 						 postfix);
1038a980e046SJonathan Cameron 		else
1039a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
1040a980e046SJonathan Cameron 						 iio_modifier_names[chan
1041a980e046SJonathan Cameron 								    ->channel2],
1042a980e046SJonathan Cameron 						 postfix);
1043a980e046SJonathan Cameron 	} else {
104477bfa8baSLars-Peter Clausen 		if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
1045a980e046SJonathan Cameron 			full_postfix = kstrdup(postfix, GFP_KERNEL);
1046a980e046SJonathan Cameron 		else
1047a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL,
1048a980e046SJonathan Cameron 						 "%s_%s",
1049a980e046SJonathan Cameron 						 chan->extend_name,
1050a980e046SJonathan Cameron 						 postfix);
1051a980e046SJonathan Cameron 	}
10523704432fSJonathan Cameron 	if (full_postfix == NULL)
10533704432fSJonathan Cameron 		return -ENOMEM;
1054a980e046SJonathan Cameron 
1055a980e046SJonathan Cameron 	if (chan->differential) { /* Differential can not have modifier */
10563704432fSJonathan Cameron 		switch (shared_by) {
1057c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
10587bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1059c006ec83SJonathan Cameron 			break;
1060c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
10617bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
1062c006ec83SJonathan Cameron 						iio_direction[chan->output],
1063c006ec83SJonathan Cameron 						full_postfix);
1064c006ec83SJonathan Cameron 			break;
10653704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
10667bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
1067a980e046SJonathan Cameron 					    iio_direction[chan->output],
1068a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
1069a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
1070a980e046SJonathan Cameron 					    full_postfix);
10713704432fSJonathan Cameron 			break;
10723704432fSJonathan Cameron 		case IIO_SEPARATE:
10733704432fSJonathan Cameron 			if (!chan->indexed) {
1074231bfe53SDan Carpenter 				WARN(1, "Differential channels must be indexed\n");
10753704432fSJonathan Cameron 				ret = -EINVAL;
10763704432fSJonathan Cameron 				goto error_free_full_postfix;
10773704432fSJonathan Cameron 			}
10787bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL,
10793704432fSJonathan Cameron 					    "%s_%s%d-%s%d_%s",
1080a980e046SJonathan Cameron 					    iio_direction[chan->output],
1081a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
1082a980e046SJonathan Cameron 					    chan->channel,
1083a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
1084a980e046SJonathan Cameron 					    chan->channel2,
1085a980e046SJonathan Cameron 					    full_postfix);
10863704432fSJonathan Cameron 			break;
1087a980e046SJonathan Cameron 		}
1088a980e046SJonathan Cameron 	} else { /* Single ended */
10893704432fSJonathan Cameron 		switch (shared_by) {
1090c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
10917bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1092c006ec83SJonathan Cameron 			break;
1093c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
10947bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
1095c006ec83SJonathan Cameron 						iio_direction[chan->output],
1096c006ec83SJonathan Cameron 						full_postfix);
1097c006ec83SJonathan Cameron 			break;
10983704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
10997bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1100a980e046SJonathan Cameron 					    iio_direction[chan->output],
1101a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
1102a980e046SJonathan Cameron 					    full_postfix);
11033704432fSJonathan Cameron 			break;
11043704432fSJonathan Cameron 
11053704432fSJonathan Cameron 		case IIO_SEPARATE:
11063704432fSJonathan Cameron 			if (chan->indexed)
11077bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
1108a980e046SJonathan Cameron 						    iio_direction[chan->output],
1109a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
1110a980e046SJonathan Cameron 						    chan->channel,
1111a980e046SJonathan Cameron 						    full_postfix);
1112a980e046SJonathan Cameron 			else
11137bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1114a980e046SJonathan Cameron 						    iio_direction[chan->output],
1115a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
1116a980e046SJonathan Cameron 						    full_postfix);
11173704432fSJonathan Cameron 			break;
11183704432fSJonathan Cameron 		}
1119a980e046SJonathan Cameron 	}
11207bbcf7e1SLars-Peter Clausen 	if (name == NULL) {
1121a980e046SJonathan Cameron 		ret = -ENOMEM;
1122a980e046SJonathan Cameron 		goto error_free_full_postfix;
1123a980e046SJonathan Cameron 	}
11247bbcf7e1SLars-Peter Clausen 	dev_attr->attr.name = name;
1125a980e046SJonathan Cameron 
1126a980e046SJonathan Cameron 	if (readfunc) {
11279cf0b618SJoe Simmons-Talbott 		dev_attr->attr.mode |= 0444;
1128a980e046SJonathan Cameron 		dev_attr->show = readfunc;
1129a980e046SJonathan Cameron 	}
1130a980e046SJonathan Cameron 
1131a980e046SJonathan Cameron 	if (writefunc) {
11329cf0b618SJoe Simmons-Talbott 		dev_attr->attr.mode |= 0200;
1133a980e046SJonathan Cameron 		dev_attr->store = writefunc;
1134a980e046SJonathan Cameron 	}
11357bbcf7e1SLars-Peter Clausen 
1136a980e046SJonathan Cameron error_free_full_postfix:
1137a980e046SJonathan Cameron 	kfree(full_postfix);
11383704432fSJonathan Cameron 
1139a980e046SJonathan Cameron 	return ret;
1140a980e046SJonathan Cameron }
1141a980e046SJonathan Cameron 
1142a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1143a980e046SJonathan Cameron {
1144a980e046SJonathan Cameron 	kfree(dev_attr->attr.name);
1145a980e046SJonathan Cameron }
1146a980e046SJonathan Cameron 
1147a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix,
1148a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
1149a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
1150a980e046SJonathan Cameron 					       struct device_attribute *attr,
1151a980e046SJonathan Cameron 					       char *buf),
1152a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
1153a980e046SJonathan Cameron 						struct device_attribute *attr,
1154a980e046SJonathan Cameron 						const char *buf,
1155a980e046SJonathan Cameron 						size_t len),
1156a980e046SJonathan Cameron 			   u64 mask,
11573704432fSJonathan Cameron 			   enum iio_shared_by shared_by,
1158a980e046SJonathan Cameron 			   struct device *dev,
11593e3d11b2SAlexandru Ardelean 			   struct iio_buffer *buffer,
1160a980e046SJonathan Cameron 			   struct list_head *attr_list)
1161a980e046SJonathan Cameron {
1162a980e046SJonathan Cameron 	int ret;
1163a980e046SJonathan Cameron 	struct iio_dev_attr *iio_attr, *t;
1164a980e046SJonathan Cameron 
1165670c1103SSachin Kamat 	iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
116692825ff9SHartmut Knaack 	if (iio_attr == NULL)
116792825ff9SHartmut Knaack 		return -ENOMEM;
1168a980e046SJonathan Cameron 	ret = __iio_device_attr_init(&iio_attr->dev_attr,
1169a980e046SJonathan Cameron 				     postfix, chan,
11703704432fSJonathan Cameron 				     readfunc, writefunc, shared_by);
1171a980e046SJonathan Cameron 	if (ret)
1172a980e046SJonathan Cameron 		goto error_iio_dev_attr_free;
1173a980e046SJonathan Cameron 	iio_attr->c = chan;
1174a980e046SJonathan Cameron 	iio_attr->address = mask;
11753e3d11b2SAlexandru Ardelean 	iio_attr->buffer = buffer;
1176a980e046SJonathan Cameron 	list_for_each_entry(t, attr_list, l)
1177a980e046SJonathan Cameron 		if (strcmp(t->dev_attr.attr.name,
1178a980e046SJonathan Cameron 			   iio_attr->dev_attr.attr.name) == 0) {
11793704432fSJonathan Cameron 			if (shared_by == IIO_SEPARATE)
1180a980e046SJonathan Cameron 				dev_err(dev, "tried to double register : %s\n",
1181a980e046SJonathan Cameron 					t->dev_attr.attr.name);
1182a980e046SJonathan Cameron 			ret = -EBUSY;
1183a980e046SJonathan Cameron 			goto error_device_attr_deinit;
1184a980e046SJonathan Cameron 		}
1185a980e046SJonathan Cameron 	list_add(&iio_attr->l, attr_list);
1186a980e046SJonathan Cameron 
1187a980e046SJonathan Cameron 	return 0;
1188a980e046SJonathan Cameron 
1189a980e046SJonathan Cameron error_device_attr_deinit:
1190a980e046SJonathan Cameron 	__iio_device_attr_deinit(&iio_attr->dev_attr);
1191a980e046SJonathan Cameron error_iio_dev_attr_free:
1192a980e046SJonathan Cameron 	kfree(iio_attr);
1193a980e046SJonathan Cameron 	return ret;
1194a980e046SJonathan Cameron }
1195a980e046SJonathan Cameron 
11961d4ef9b3SCristian Pop static int iio_device_add_channel_label(struct iio_dev *indio_dev,
11971d4ef9b3SCristian Pop 					 struct iio_chan_spec const *chan)
11981d4ef9b3SCristian Pop {
11991d4ef9b3SCristian Pop 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
12001d4ef9b3SCristian Pop 	int ret;
12011d4ef9b3SCristian Pop 
120213efdc3dSPaul Cercueil 	if (!indio_dev->info->read_label && !chan->extend_name)
12031d4ef9b3SCristian Pop 		return 0;
12041d4ef9b3SCristian Pop 
12051d4ef9b3SCristian Pop 	ret = __iio_add_chan_devattr("label",
12061d4ef9b3SCristian Pop 				     chan,
12071d4ef9b3SCristian Pop 				     &iio_read_channel_label,
12081d4ef9b3SCristian Pop 				     NULL,
12091d4ef9b3SCristian Pop 				     0,
12101d4ef9b3SCristian Pop 				     IIO_SEPARATE,
12111d4ef9b3SCristian Pop 				     &indio_dev->dev,
12123e3d11b2SAlexandru Ardelean 				     NULL,
12131d4ef9b3SCristian Pop 				     &iio_dev_opaque->channel_attr_list);
12141d4ef9b3SCristian Pop 	if (ret < 0)
12151d4ef9b3SCristian Pop 		return ret;
12161d4ef9b3SCristian Pop 
12171d4ef9b3SCristian Pop 	return 1;
12181d4ef9b3SCristian Pop }
12191d4ef9b3SCristian Pop 
12203704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
12213704432fSJonathan Cameron 					 struct iio_chan_spec const *chan,
12223704432fSJonathan Cameron 					 enum iio_shared_by shared_by,
12233704432fSJonathan Cameron 					 const long *infomask)
12243704432fSJonathan Cameron {
1225207c2d27SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
12263704432fSJonathan Cameron 	int i, ret, attrcount = 0;
12273704432fSJonathan Cameron 
12281016d567SOrson Zhai 	for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1229ef4b4856SJonathan Cameron 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1230ef4b4856SJonathan Cameron 			return -EINVAL;
12313704432fSJonathan Cameron 		ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
12323704432fSJonathan Cameron 					     chan,
12333704432fSJonathan Cameron 					     &iio_read_channel_info,
12343704432fSJonathan Cameron 					     &iio_write_channel_info,
12353704432fSJonathan Cameron 					     i,
12363704432fSJonathan Cameron 					     shared_by,
12373704432fSJonathan Cameron 					     &indio_dev->dev,
12383e3d11b2SAlexandru Ardelean 					     NULL,
1239207c2d27SAlexandru Ardelean 					     &iio_dev_opaque->channel_attr_list);
12403704432fSJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
12413704432fSJonathan Cameron 			continue;
12423704432fSJonathan Cameron 		else if (ret < 0)
12433704432fSJonathan Cameron 			return ret;
12443704432fSJonathan Cameron 		attrcount++;
12453704432fSJonathan Cameron 	}
12463704432fSJonathan Cameron 
12473704432fSJonathan Cameron 	return attrcount;
12483704432fSJonathan Cameron }
12493704432fSJonathan Cameron 
125051239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
125151239600SJonathan Cameron 					       struct iio_chan_spec const *chan,
125251239600SJonathan Cameron 					       enum iio_shared_by shared_by,
125351239600SJonathan Cameron 					       const long *infomask)
125451239600SJonathan Cameron {
1255207c2d27SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
125651239600SJonathan Cameron 	int i, ret, attrcount = 0;
125751239600SJonathan Cameron 	char *avail_postfix;
125851239600SJonathan Cameron 
12591016d567SOrson Zhai 	for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1260936d3e53SYoung Xiao 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1261936d3e53SYoung Xiao 			return -EINVAL;
126251239600SJonathan Cameron 		avail_postfix = kasprintf(GFP_KERNEL,
126351239600SJonathan Cameron 					  "%s_available",
126451239600SJonathan Cameron 					  iio_chan_info_postfix[i]);
126551239600SJonathan Cameron 		if (!avail_postfix)
126651239600SJonathan Cameron 			return -ENOMEM;
126751239600SJonathan Cameron 
126851239600SJonathan Cameron 		ret = __iio_add_chan_devattr(avail_postfix,
126951239600SJonathan Cameron 					     chan,
127051239600SJonathan Cameron 					     &iio_read_channel_info_avail,
127151239600SJonathan Cameron 					     NULL,
127251239600SJonathan Cameron 					     i,
127351239600SJonathan Cameron 					     shared_by,
127451239600SJonathan Cameron 					     &indio_dev->dev,
12753e3d11b2SAlexandru Ardelean 					     NULL,
1276207c2d27SAlexandru Ardelean 					     &iio_dev_opaque->channel_attr_list);
127751239600SJonathan Cameron 		kfree(avail_postfix);
127851239600SJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
127951239600SJonathan Cameron 			continue;
128051239600SJonathan Cameron 		else if (ret < 0)
128151239600SJonathan Cameron 			return ret;
128251239600SJonathan Cameron 		attrcount++;
128351239600SJonathan Cameron 	}
128451239600SJonathan Cameron 
128551239600SJonathan Cameron 	return attrcount;
128651239600SJonathan Cameron }
128751239600SJonathan Cameron 
1288a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1289a980e046SJonathan Cameron 					struct iio_chan_spec const *chan)
1290a980e046SJonathan Cameron {
1291207c2d27SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1292a980e046SJonathan Cameron 	int ret, attrcount = 0;
1293a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
1294a980e046SJonathan Cameron 
1295a980e046SJonathan Cameron 	if (chan->channel < 0)
1296a980e046SJonathan Cameron 		return 0;
12973704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
12983704432fSJonathan Cameron 					    IIO_SEPARATE,
12993704432fSJonathan Cameron 					    &chan->info_mask_separate);
13008655cc49SJonathan Cameron 	if (ret < 0)
13013704432fSJonathan Cameron 		return ret;
13023704432fSJonathan Cameron 	attrcount += ret;
13033704432fSJonathan Cameron 
130451239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
130551239600SJonathan Cameron 						  IIO_SEPARATE,
1306*682ca76bSJoe Simmons-Talbott 						  &chan->info_mask_separate_available);
130751239600SJonathan Cameron 	if (ret < 0)
130851239600SJonathan Cameron 		return ret;
130951239600SJonathan Cameron 	attrcount += ret;
131051239600SJonathan Cameron 
13113704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
13123704432fSJonathan Cameron 					    IIO_SHARED_BY_TYPE,
13133704432fSJonathan Cameron 					    &chan->info_mask_shared_by_type);
13143704432fSJonathan Cameron 	if (ret < 0)
13153704432fSJonathan Cameron 		return ret;
13163704432fSJonathan Cameron 	attrcount += ret;
1317a980e046SJonathan Cameron 
131851239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
131951239600SJonathan Cameron 						  IIO_SHARED_BY_TYPE,
1320*682ca76bSJoe Simmons-Talbott 						  &chan->info_mask_shared_by_type_available);
132151239600SJonathan Cameron 	if (ret < 0)
132251239600SJonathan Cameron 		return ret;
132351239600SJonathan Cameron 	attrcount += ret;
132451239600SJonathan Cameron 
1325c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1326c006ec83SJonathan Cameron 					    IIO_SHARED_BY_DIR,
1327c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_dir);
1328c006ec83SJonathan Cameron 	if (ret < 0)
1329c006ec83SJonathan Cameron 		return ret;
1330c006ec83SJonathan Cameron 	attrcount += ret;
1331c006ec83SJonathan Cameron 
133251239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
133351239600SJonathan Cameron 						  IIO_SHARED_BY_DIR,
133451239600SJonathan Cameron 						  &chan->info_mask_shared_by_dir_available);
133551239600SJonathan Cameron 	if (ret < 0)
133651239600SJonathan Cameron 		return ret;
133751239600SJonathan Cameron 	attrcount += ret;
133851239600SJonathan Cameron 
1339c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1340c006ec83SJonathan Cameron 					    IIO_SHARED_BY_ALL,
1341c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_all);
1342c006ec83SJonathan Cameron 	if (ret < 0)
1343c006ec83SJonathan Cameron 		return ret;
1344c006ec83SJonathan Cameron 	attrcount += ret;
1345c006ec83SJonathan Cameron 
134651239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
134751239600SJonathan Cameron 						  IIO_SHARED_BY_ALL,
134851239600SJonathan Cameron 						  &chan->info_mask_shared_by_all_available);
134951239600SJonathan Cameron 	if (ret < 0)
135051239600SJonathan Cameron 		return ret;
135151239600SJonathan Cameron 	attrcount += ret;
135251239600SJonathan Cameron 
13531d4ef9b3SCristian Pop 	ret = iio_device_add_channel_label(indio_dev, chan);
13541d4ef9b3SCristian Pop 	if (ret < 0)
13551d4ef9b3SCristian Pop 		return ret;
13561d4ef9b3SCristian Pop 	attrcount += ret;
13571d4ef9b3SCristian Pop 
1358a980e046SJonathan Cameron 	if (chan->ext_info) {
1359a980e046SJonathan Cameron 		unsigned int i = 0;
13609d9ec8d0SJoe Simmons-Talbott 
1361a980e046SJonathan Cameron 		for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1362a980e046SJonathan Cameron 			ret = __iio_add_chan_devattr(ext_info->name,
1363a980e046SJonathan Cameron 					chan,
1364a980e046SJonathan Cameron 					ext_info->read ?
1365a980e046SJonathan Cameron 					    &iio_read_channel_ext_info : NULL,
1366a980e046SJonathan Cameron 					ext_info->write ?
1367a980e046SJonathan Cameron 					    &iio_write_channel_ext_info : NULL,
1368a980e046SJonathan Cameron 					i,
1369a980e046SJonathan Cameron 					ext_info->shared,
1370a980e046SJonathan Cameron 					&indio_dev->dev,
13713e3d11b2SAlexandru Ardelean 					NULL,
1372207c2d27SAlexandru Ardelean 					&iio_dev_opaque->channel_attr_list);
1373a980e046SJonathan Cameron 			i++;
1374a980e046SJonathan Cameron 			if (ret == -EBUSY && ext_info->shared)
1375a980e046SJonathan Cameron 				continue;
1376a980e046SJonathan Cameron 
1377a980e046SJonathan Cameron 			if (ret)
13783704432fSJonathan Cameron 				return ret;
1379a980e046SJonathan Cameron 
1380a980e046SJonathan Cameron 			attrcount++;
1381a980e046SJonathan Cameron 		}
1382a980e046SJonathan Cameron 	}
1383a980e046SJonathan Cameron 
13843704432fSJonathan Cameron 	return attrcount;
1385a980e046SJonathan Cameron }
1386a980e046SJonathan Cameron 
138784088ebdSLars-Peter Clausen /**
138884088ebdSLars-Peter Clausen  * iio_free_chan_devattr_list() - Free a list of IIO device attributes
138984088ebdSLars-Peter Clausen  * @attr_list: List of IIO device attributes
139084088ebdSLars-Peter Clausen  *
139184088ebdSLars-Peter Clausen  * This function frees the memory allocated for each of the IIO device
1392c1b03ab5SMartin Fuzzey  * attributes in the list.
139384088ebdSLars-Peter Clausen  */
139484088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list)
1395a980e046SJonathan Cameron {
139684088ebdSLars-Peter Clausen 	struct iio_dev_attr *p, *n;
139784088ebdSLars-Peter Clausen 
139884088ebdSLars-Peter Clausen 	list_for_each_entry_safe(p, n, attr_list, l) {
139915097c7aSAlexandru Ardelean 		kfree_const(p->dev_attr.attr.name);
1400c1b03ab5SMartin Fuzzey 		list_del(&p->l);
1401a980e046SJonathan Cameron 		kfree(p);
1402a980e046SJonathan Cameron 	}
140384088ebdSLars-Peter Clausen }
1404a980e046SJonathan Cameron 
14059cf0b618SJoe Simmons-Talbott static ssize_t name_show(struct device *dev, struct device_attribute *attr,
1406a980e046SJonathan Cameron 			 char *buf)
1407a980e046SJonathan Cameron {
1408e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
14099d9ec8d0SJoe Simmons-Talbott 
141083ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%s\n", indio_dev->name);
1411a980e046SJonathan Cameron }
1412a980e046SJonathan Cameron 
14139cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RO(name);
1414a980e046SJonathan Cameron 
14159cf0b618SJoe Simmons-Talbott static ssize_t label_show(struct device *dev, struct device_attribute *attr,
14162c3d0c9fSPhil Reid 			  char *buf)
14172c3d0c9fSPhil Reid {
14182c3d0c9fSPhil Reid 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
14199d9ec8d0SJoe Simmons-Talbott 
142083ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%s\n", indio_dev->label);
14212c3d0c9fSPhil Reid }
14222c3d0c9fSPhil Reid 
14239cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RO(label);
14242c3d0c9fSPhil Reid 
14259cf0b618SJoe Simmons-Talbott static ssize_t current_timestamp_clock_show(struct device *dev,
1426bc2b7dabSGregor Boirie 					    struct device_attribute *attr,
1427bc2b7dabSGregor Boirie 					    char *buf)
1428bc2b7dabSGregor Boirie {
1429bc2b7dabSGregor Boirie 	const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1430bc2b7dabSGregor Boirie 	const clockid_t clk = iio_device_get_clock(indio_dev);
1431bc2b7dabSGregor Boirie 	const char *name;
1432bc2b7dabSGregor Boirie 	ssize_t sz;
1433bc2b7dabSGregor Boirie 
1434bc2b7dabSGregor Boirie 	switch (clk) {
1435bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
1436bc2b7dabSGregor Boirie 		name = "realtime\n";
1437bc2b7dabSGregor Boirie 		sz = sizeof("realtime\n");
1438bc2b7dabSGregor Boirie 		break;
1439bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
1440bc2b7dabSGregor Boirie 		name = "monotonic\n";
1441bc2b7dabSGregor Boirie 		sz = sizeof("monotonic\n");
1442bc2b7dabSGregor Boirie 		break;
1443bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
1444bc2b7dabSGregor Boirie 		name = "monotonic_raw\n";
1445bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_raw\n");
1446bc2b7dabSGregor Boirie 		break;
1447bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
1448bc2b7dabSGregor Boirie 		name = "realtime_coarse\n";
1449bc2b7dabSGregor Boirie 		sz = sizeof("realtime_coarse\n");
1450bc2b7dabSGregor Boirie 		break;
1451bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
1452bc2b7dabSGregor Boirie 		name = "monotonic_coarse\n";
1453bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_coarse\n");
1454bc2b7dabSGregor Boirie 		break;
1455bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
1456bc2b7dabSGregor Boirie 		name = "boottime\n";
1457bc2b7dabSGregor Boirie 		sz = sizeof("boottime\n");
1458bc2b7dabSGregor Boirie 		break;
1459bc2b7dabSGregor Boirie 	case CLOCK_TAI:
1460bc2b7dabSGregor Boirie 		name = "tai\n";
1461bc2b7dabSGregor Boirie 		sz = sizeof("tai\n");
1462bc2b7dabSGregor Boirie 		break;
1463bc2b7dabSGregor Boirie 	default:
1464bc2b7dabSGregor Boirie 		BUG();
1465bc2b7dabSGregor Boirie 	}
1466bc2b7dabSGregor Boirie 
1467bc2b7dabSGregor Boirie 	memcpy(buf, name, sz);
1468bc2b7dabSGregor Boirie 	return sz;
1469bc2b7dabSGregor Boirie }
1470bc2b7dabSGregor Boirie 
14719cf0b618SJoe Simmons-Talbott static ssize_t current_timestamp_clock_store(struct device *dev,
1472bc2b7dabSGregor Boirie 					     struct device_attribute *attr,
1473bc2b7dabSGregor Boirie 					     const char *buf, size_t len)
1474bc2b7dabSGregor Boirie {
1475bc2b7dabSGregor Boirie 	clockid_t clk;
1476bc2b7dabSGregor Boirie 	int ret;
1477bc2b7dabSGregor Boirie 
1478bc2b7dabSGregor Boirie 	if (sysfs_streq(buf, "realtime"))
1479bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME;
1480bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic"))
1481bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC;
1482bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_raw"))
1483bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_RAW;
1484bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "realtime_coarse"))
1485bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME_COARSE;
1486bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_coarse"))
1487bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_COARSE;
1488bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "boottime"))
1489bc2b7dabSGregor Boirie 		clk = CLOCK_BOOTTIME;
1490bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "tai"))
1491bc2b7dabSGregor Boirie 		clk = CLOCK_TAI;
1492bc2b7dabSGregor Boirie 	else
1493bc2b7dabSGregor Boirie 		return -EINVAL;
1494bc2b7dabSGregor Boirie 
1495bc2b7dabSGregor Boirie 	ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1496bc2b7dabSGregor Boirie 	if (ret)
1497bc2b7dabSGregor Boirie 		return ret;
1498bc2b7dabSGregor Boirie 
1499bc2b7dabSGregor Boirie 	return len;
1500bc2b7dabSGregor Boirie }
1501bc2b7dabSGregor Boirie 
150232f17172SAlexandru Ardelean int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
150332f17172SAlexandru Ardelean 				    const struct attribute_group *group)
150432f17172SAlexandru Ardelean {
150532f17172SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
150632f17172SAlexandru Ardelean 	const struct attribute_group **new, **old = iio_dev_opaque->groups;
150732f17172SAlexandru Ardelean 	unsigned int cnt = iio_dev_opaque->groupcounter;
150832f17172SAlexandru Ardelean 
150932f17172SAlexandru Ardelean 	new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
151032f17172SAlexandru Ardelean 	if (!new)
151132f17172SAlexandru Ardelean 		return -ENOMEM;
151232f17172SAlexandru Ardelean 
151332f17172SAlexandru Ardelean 	new[iio_dev_opaque->groupcounter++] = group;
151432f17172SAlexandru Ardelean 	new[iio_dev_opaque->groupcounter] = NULL;
151532f17172SAlexandru Ardelean 
151632f17172SAlexandru Ardelean 	iio_dev_opaque->groups = new;
151732f17172SAlexandru Ardelean 
151832f17172SAlexandru Ardelean 	return 0;
151932f17172SAlexandru Ardelean }
152032f17172SAlexandru Ardelean 
15219cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RW(current_timestamp_clock);
1522bc2b7dabSGregor Boirie 
1523a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1524a980e046SJonathan Cameron {
1525207c2d27SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1526a980e046SJonathan Cameron 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
152784088ebdSLars-Peter Clausen 	struct iio_dev_attr *p;
1528bc2b7dabSGregor Boirie 	struct attribute **attr, *clk = NULL;
1529a980e046SJonathan Cameron 
1530a980e046SJonathan Cameron 	/* First count elements in any existing group */
1531a980e046SJonathan Cameron 	if (indio_dev->info->attrs) {
1532a980e046SJonathan Cameron 		attr = indio_dev->info->attrs->attrs;
1533a980e046SJonathan Cameron 		while (*attr++ != NULL)
1534a980e046SJonathan Cameron 			attrcount_orig++;
1535a980e046SJonathan Cameron 	}
1536a980e046SJonathan Cameron 	attrcount = attrcount_orig;
1537a980e046SJonathan Cameron 	/*
1538a980e046SJonathan Cameron 	 * New channel registration method - relies on the fact a group does
1539d25b3808SPeter Meerwald 	 * not need to be initialized if its name is NULL.
1540a980e046SJonathan Cameron 	 */
1541a980e046SJonathan Cameron 	if (indio_dev->channels)
1542a980e046SJonathan Cameron 		for (i = 0; i < indio_dev->num_channels; i++) {
1543bc2b7dabSGregor Boirie 			const struct iio_chan_spec *chan =
1544bc2b7dabSGregor Boirie 				&indio_dev->channels[i];
1545bc2b7dabSGregor Boirie 
1546bc2b7dabSGregor Boirie 			if (chan->type == IIO_TIMESTAMP)
1547bc2b7dabSGregor Boirie 				clk = &dev_attr_current_timestamp_clock.attr;
1548bc2b7dabSGregor Boirie 
1549bc2b7dabSGregor Boirie 			ret = iio_device_add_channel_sysfs(indio_dev, chan);
1550a980e046SJonathan Cameron 			if (ret < 0)
1551a980e046SJonathan Cameron 				goto error_clear_attrs;
1552a980e046SJonathan Cameron 			attrcount += ret;
1553a980e046SJonathan Cameron 		}
1554a980e046SJonathan Cameron 
1555fa83c3baSAlexandru Ardelean 	if (iio_dev_opaque->event_interface)
1556bc2b7dabSGregor Boirie 		clk = &dev_attr_current_timestamp_clock.attr;
1557bc2b7dabSGregor Boirie 
1558a980e046SJonathan Cameron 	if (indio_dev->name)
1559a980e046SJonathan Cameron 		attrcount++;
15602c3d0c9fSPhil Reid 	if (indio_dev->label)
15612c3d0c9fSPhil Reid 		attrcount++;
1562bc2b7dabSGregor Boirie 	if (clk)
1563bc2b7dabSGregor Boirie 		attrcount++;
1564a980e046SJonathan Cameron 
1565207c2d27SAlexandru Ardelean 	iio_dev_opaque->chan_attr_group.attrs =
1566207c2d27SAlexandru Ardelean 		kcalloc(attrcount + 1,
1567207c2d27SAlexandru Ardelean 			sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1568a980e046SJonathan Cameron 			GFP_KERNEL);
1569207c2d27SAlexandru Ardelean 	if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
1570a980e046SJonathan Cameron 		ret = -ENOMEM;
1571a980e046SJonathan Cameron 		goto error_clear_attrs;
1572a980e046SJonathan Cameron 	}
1573a980e046SJonathan Cameron 	/* Copy across original attributes */
157418b4c9cdSHans de Goede 	if (indio_dev->info->attrs) {
1575207c2d27SAlexandru Ardelean 		memcpy(iio_dev_opaque->chan_attr_group.attrs,
1576a980e046SJonathan Cameron 		       indio_dev->info->attrs->attrs,
1577207c2d27SAlexandru Ardelean 		       sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
1578a980e046SJonathan Cameron 		       *attrcount_orig);
157918b4c9cdSHans de Goede 		iio_dev_opaque->chan_attr_group.is_visible =
158018b4c9cdSHans de Goede 			indio_dev->info->attrs->is_visible;
158118b4c9cdSHans de Goede 	}
1582a980e046SJonathan Cameron 	attrn = attrcount_orig;
1583a980e046SJonathan Cameron 	/* Add all elements from the list. */
1584207c2d27SAlexandru Ardelean 	list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1585207c2d27SAlexandru Ardelean 		iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1586a980e046SJonathan Cameron 	if (indio_dev->name)
1587207c2d27SAlexandru Ardelean 		iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
15882c3d0c9fSPhil Reid 	if (indio_dev->label)
1589207c2d27SAlexandru Ardelean 		iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1590bc2b7dabSGregor Boirie 	if (clk)
1591207c2d27SAlexandru Ardelean 		iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
1592a980e046SJonathan Cameron 
159332f17172SAlexandru Ardelean 	ret = iio_device_register_sysfs_group(indio_dev,
159432f17172SAlexandru Ardelean 					      &iio_dev_opaque->chan_attr_group);
159532f17172SAlexandru Ardelean 	if (ret)
159632f17172SAlexandru Ardelean 		goto error_clear_attrs;
1597a980e046SJonathan Cameron 
1598a980e046SJonathan Cameron 	return 0;
1599a980e046SJonathan Cameron 
1600a980e046SJonathan Cameron error_clear_attrs:
1601207c2d27SAlexandru Ardelean 	iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1602a980e046SJonathan Cameron 
1603a980e046SJonathan Cameron 	return ret;
1604a980e046SJonathan Cameron }
1605a980e046SJonathan Cameron 
1606a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1607a980e046SJonathan Cameron {
1608207c2d27SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1609a980e046SJonathan Cameron 
1610207c2d27SAlexandru Ardelean 	iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1611207c2d27SAlexandru Ardelean 	kfree(iio_dev_opaque->chan_attr_group.attrs);
1612207c2d27SAlexandru Ardelean 	iio_dev_opaque->chan_attr_group.attrs = NULL;
161332f17172SAlexandru Ardelean 	kfree(iio_dev_opaque->groups);
161419833c40SYang Yingliang 	iio_dev_opaque->groups = NULL;
1615a980e046SJonathan Cameron }
1616a980e046SJonathan Cameron 
1617a980e046SJonathan Cameron static void iio_dev_release(struct device *device)
1618a980e046SJonathan Cameron {
1619e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(device);
16206d4ebd56SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
16216d4ebd56SAlexandru Ardelean 
1622d89e119aSBenjamin Gaignard 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1623a980e046SJonathan Cameron 		iio_device_unregister_trigger_consumer(indio_dev);
1624a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1625a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
1626e407fd65SLars-Peter Clausen 
1627218bc53dSAlexandru Ardelean 	iio_device_detach_buffers(indio_dev);
16289e69c935SLars-Peter Clausen 
1629319dbcd8Skeliu 	ida_free(&iio_ida, iio_dev_opaque->id);
16306d4ebd56SAlexandru Ardelean 	kfree(iio_dev_opaque);
1631a980e046SJonathan Cameron }
1632a980e046SJonathan Cameron 
16333c33b7b8SLars-Peter Clausen const struct device_type iio_device_type = {
1634a980e046SJonathan Cameron 	.name = "iio_device",
1635a980e046SJonathan Cameron 	.release = iio_dev_release,
1636a980e046SJonathan Cameron };
1637a980e046SJonathan Cameron 
1638a7e57dceSSachin Kamat /**
1639a7e57dceSSachin Kamat  * iio_device_alloc() - allocate an iio_dev from a driver
1640c9561fd2SJonathan Cameron  * @parent:		Parent device.
1641a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1642a7e57dceSSachin Kamat  **/
164378289b4aSAlexandru Ardelean struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
1644a980e046SJonathan Cameron {
16456d4ebd56SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque;
16460d596bb2SAlexandru Ardelean 	struct iio_dev *indio_dev;
1647a980e046SJonathan Cameron 	size_t alloc_size;
1648a980e046SJonathan Cameron 
16496d4ebd56SAlexandru Ardelean 	alloc_size = sizeof(struct iio_dev_opaque);
1650a980e046SJonathan Cameron 	if (sizeof_priv) {
165112c4efe3SJonathan Cameron 		alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
1652a980e046SJonathan Cameron 		alloc_size += sizeof_priv;
1653a980e046SJonathan Cameron 	}
1654a980e046SJonathan Cameron 
16556d4ebd56SAlexandru Ardelean 	iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
16566d4ebd56SAlexandru Ardelean 	if (!iio_dev_opaque)
16570d4b2184SJonathan Cameron 		return NULL;
1658a980e046SJonathan Cameron 
16590d596bb2SAlexandru Ardelean 	indio_dev = &iio_dev_opaque->indio_dev;
16600d596bb2SAlexandru Ardelean 	indio_dev->priv = (char *)iio_dev_opaque +
166112c4efe3SJonathan Cameron 		ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN);
16626d4ebd56SAlexandru Ardelean 
16630d596bb2SAlexandru Ardelean 	indio_dev->dev.parent = parent;
16640d596bb2SAlexandru Ardelean 	indio_dev->dev.type = &iio_device_type;
16650d596bb2SAlexandru Ardelean 	indio_dev->dev.bus = &iio_bus_type;
16660d596bb2SAlexandru Ardelean 	device_initialize(&indio_dev->dev);
16670d596bb2SAlexandru Ardelean 	mutex_init(&indio_dev->mlock);
1668b804e2b7SJonathan Cameron 	mutex_init(&iio_dev_opaque->info_exist_lock);
1669207c2d27SAlexandru Ardelean 	INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
1670a980e046SJonathan Cameron 
1671319dbcd8Skeliu 	iio_dev_opaque->id = ida_alloc(&iio_ida, GFP_KERNEL);
167215ea2878SJonathan Cameron 	if (iio_dev_opaque->id < 0) {
1673a980e046SJonathan Cameron 		/* cannot use a dev_err as the name isn't available */
16743176dd5dSSachin Kamat 		pr_err("failed to get device id\n");
16756d4ebd56SAlexandru Ardelean 		kfree(iio_dev_opaque);
1676a980e046SJonathan Cameron 		return NULL;
1677a980e046SJonathan Cameron 	}
1678fe6f45f6SYang Yingliang 
1679fe6f45f6SYang Yingliang 	if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) {
1680319dbcd8Skeliu 		ida_free(&iio_ida, iio_dev_opaque->id);
1681fe6f45f6SYang Yingliang 		kfree(iio_dev_opaque);
1682fe6f45f6SYang Yingliang 		return NULL;
1683fe6f45f6SYang Yingliang 	}
1684fe6f45f6SYang Yingliang 
16856a8c6b26SAlexandru Ardelean 	INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
16868dedcc3eSAlexandru Ardelean 	INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
1687a980e046SJonathan Cameron 
16880d596bb2SAlexandru Ardelean 	return indio_dev;
1689a980e046SJonathan Cameron }
16907cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc);
1691a980e046SJonathan Cameron 
1692a7e57dceSSachin Kamat /**
1693a7e57dceSSachin Kamat  * iio_device_free() - free an iio_dev from a driver
1694a7e57dceSSachin Kamat  * @dev:		the iio_dev associated with the device
1695a7e57dceSSachin Kamat  **/
16967cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev)
1697a980e046SJonathan Cameron {
1698e407fd65SLars-Peter Clausen 	if (dev)
1699e407fd65SLars-Peter Clausen 		put_device(&dev->dev);
1700a980e046SJonathan Cameron }
17017cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free);
1702a980e046SJonathan Cameron 
1703cf5724e9SYicong Yang static void devm_iio_device_release(void *iio_dev)
17049dabaf5eSGrygorii Strashko {
1705cf5724e9SYicong Yang 	iio_device_free(iio_dev);
17069dabaf5eSGrygorii Strashko }
17079dabaf5eSGrygorii Strashko 
1708a7e57dceSSachin Kamat /**
1709a7e57dceSSachin Kamat  * devm_iio_device_alloc - Resource-managed iio_device_alloc()
171078289b4aSAlexandru Ardelean  * @parent:		Device to allocate iio_dev for, and parent for this IIO device
1711a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1712a7e57dceSSachin Kamat  *
1713a7e57dceSSachin Kamat  * Managed iio_device_alloc. iio_dev allocated with this function is
1714a7e57dceSSachin Kamat  * automatically freed on driver detach.
1715a7e57dceSSachin Kamat  *
1716a7e57dceSSachin Kamat  * RETURNS:
1717a7e57dceSSachin Kamat  * Pointer to allocated iio_dev on success, NULL on failure.
1718a7e57dceSSachin Kamat  */
171978289b4aSAlexandru Ardelean struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
17209dabaf5eSGrygorii Strashko {
1721cf5724e9SYicong Yang 	struct iio_dev *iio_dev;
1722cf5724e9SYicong Yang 	int ret;
17239dabaf5eSGrygorii Strashko 
172478289b4aSAlexandru Ardelean 	iio_dev = iio_device_alloc(parent, sizeof_priv);
1725cf5724e9SYicong Yang 	if (!iio_dev)
1726cf5724e9SYicong Yang 		return NULL;
1727cf5724e9SYicong Yang 
1728cf5724e9SYicong Yang 	ret = devm_add_action_or_reset(parent, devm_iio_device_release,
1729cf5724e9SYicong Yang 				       iio_dev);
1730cf5724e9SYicong Yang 	if (ret)
173143fa7394SDan Carpenter 		return NULL;
17329dabaf5eSGrygorii Strashko 
17339dabaf5eSGrygorii Strashko 	return iio_dev;
17349dabaf5eSGrygorii Strashko }
17359dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
17369dabaf5eSGrygorii Strashko 
1737a7e57dceSSachin Kamat /**
1738a980e046SJonathan Cameron  * iio_chrdev_open() - chrdev file open for buffer access and ioctls
17392498dcf6SCristina Opriceana  * @inode:	Inode structure for identifying the device in the file system
17402498dcf6SCristina Opriceana  * @filp:	File structure for iio device used to keep and later access
17412498dcf6SCristina Opriceana  *		private data
17422498dcf6SCristina Opriceana  *
17432498dcf6SCristina Opriceana  * Return: 0 on success or -EBUSY if the device is already opened
1744a980e046SJonathan Cameron  **/
1745a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp)
1746a980e046SJonathan Cameron {
1747396f7234SJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque =
1748396f7234SJonathan Cameron 		container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1749396f7234SJonathan Cameron 	struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
1750be24dcb1SAlexandru Ardelean 	struct iio_dev_buffer_pair *ib;
1751a980e046SJonathan Cameron 
17528b1c82cbSJonathan Cameron 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags))
1753a980e046SJonathan Cameron 		return -EBUSY;
1754a980e046SJonathan Cameron 
1755cadc2125SLars-Peter Clausen 	iio_device_get(indio_dev);
1756cadc2125SLars-Peter Clausen 
1757be24dcb1SAlexandru Ardelean 	ib = kmalloc(sizeof(*ib), GFP_KERNEL);
1758be24dcb1SAlexandru Ardelean 	if (!ib) {
1759be24dcb1SAlexandru Ardelean 		iio_device_put(indio_dev);
17608b1c82cbSJonathan Cameron 		clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1761be24dcb1SAlexandru Ardelean 		return -ENOMEM;
1762be24dcb1SAlexandru Ardelean 	}
1763be24dcb1SAlexandru Ardelean 
1764be24dcb1SAlexandru Ardelean 	ib->indio_dev = indio_dev;
1765be24dcb1SAlexandru Ardelean 	ib->buffer = indio_dev->buffer;
1766be24dcb1SAlexandru Ardelean 
1767be24dcb1SAlexandru Ardelean 	filp->private_data = ib;
1768a980e046SJonathan Cameron 
1769a980e046SJonathan Cameron 	return 0;
1770a980e046SJonathan Cameron }
1771a980e046SJonathan Cameron 
1772a980e046SJonathan Cameron /**
1773a980e046SJonathan Cameron  * iio_chrdev_release() - chrdev file close buffer access and ioctls
17742498dcf6SCristina Opriceana  * @inode:	Inode structure pointer for the char device
17752498dcf6SCristina Opriceana  * @filp:	File structure pointer for the char device
17762498dcf6SCristina Opriceana  *
17772498dcf6SCristina Opriceana  * Return: 0 for successful release
17782498dcf6SCristina Opriceana  */
1779a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp)
1780a980e046SJonathan Cameron {
1781be24dcb1SAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filp->private_data;
1782396f7234SJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque =
1783396f7234SJonathan Cameron 		container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1784396f7234SJonathan Cameron 	struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
17859d9ec8d0SJoe Simmons-Talbott 
1786be24dcb1SAlexandru Ardelean 	kfree(ib);
17878b1c82cbSJonathan Cameron 	clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
1788cadc2125SLars-Peter Clausen 	iio_device_put(indio_dev);
1789cadc2125SLars-Peter Clausen 
1790a980e046SJonathan Cameron 	return 0;
1791a980e046SJonathan Cameron }
1792a980e046SJonathan Cameron 
17938dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
17948dedcc3eSAlexandru Ardelean 				       struct iio_ioctl_handler *h)
17958dedcc3eSAlexandru Ardelean {
17968dedcc3eSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
17978dedcc3eSAlexandru Ardelean 
17988dedcc3eSAlexandru Ardelean 	list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers);
17998dedcc3eSAlexandru Ardelean }
18008dedcc3eSAlexandru Ardelean 
18018dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h)
18028dedcc3eSAlexandru Ardelean {
18038dedcc3eSAlexandru Ardelean 	list_del(&h->entry);
18048dedcc3eSAlexandru Ardelean }
18058dedcc3eSAlexandru Ardelean 
1806a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1807a980e046SJonathan Cameron {
1808be24dcb1SAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filp->private_data;
1809be24dcb1SAlexandru Ardelean 	struct iio_dev *indio_dev = ib->indio_dev;
18108dedcc3eSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
18118dedcc3eSAlexandru Ardelean 	struct iio_ioctl_handler *h;
18128dedcc3eSAlexandru Ardelean 	int ret = -ENODEV;
1813a980e046SJonathan Cameron 
1814b804e2b7SJonathan Cameron 	mutex_lock(&iio_dev_opaque->info_exist_lock);
18158dedcc3eSAlexandru Ardelean 
18168dedcc3eSAlexandru Ardelean 	/**
18178dedcc3eSAlexandru Ardelean 	 * The NULL check here is required to prevent crashing when a device
18188dedcc3eSAlexandru Ardelean 	 * is being removed while userspace would still have open file handles
18198dedcc3eSAlexandru Ardelean 	 * to try to access this device.
18208dedcc3eSAlexandru Ardelean 	 */
1821f18e7a06SLars-Peter Clausen 	if (!indio_dev->info)
18228dedcc3eSAlexandru Ardelean 		goto out_unlock;
1823f18e7a06SLars-Peter Clausen 
18248dedcc3eSAlexandru Ardelean 	list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
18258dedcc3eSAlexandru Ardelean 		ret = h->ioctl(indio_dev, filp, cmd, arg);
18268dedcc3eSAlexandru Ardelean 		if (ret != IIO_IOCTL_UNHANDLED)
18278dedcc3eSAlexandru Ardelean 			break;
1828a980e046SJonathan Cameron 	}
18298dedcc3eSAlexandru Ardelean 
183074d826daSAlexandru Ardelean 	if (ret == IIO_IOCTL_UNHANDLED)
1831af0670b0SAlexandru Ardelean 		ret = -ENODEV;
183274d826daSAlexandru Ardelean 
18338dedcc3eSAlexandru Ardelean out_unlock:
1834b804e2b7SJonathan Cameron 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
18358dedcc3eSAlexandru Ardelean 
18368dedcc3eSAlexandru Ardelean 	return ret;
1837a980e046SJonathan Cameron }
1838a980e046SJonathan Cameron 
1839a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = {
1840a980e046SJonathan Cameron 	.owner = THIS_MODULE,
1841a980e046SJonathan Cameron 	.llseek = noop_llseek,
1842ee8caea0SAlexandru Ardelean 	.read = iio_buffer_read_outer_addr,
18439eeee3b0SMihail Chindris 	.write = iio_buffer_write_outer_addr,
1844ee8caea0SAlexandru Ardelean 	.poll = iio_buffer_poll_addr,
1845a980e046SJonathan Cameron 	.unlocked_ioctl = iio_ioctl,
18461832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
1847ee8caea0SAlexandru Ardelean 	.open = iio_chrdev_open,
1848ee8caea0SAlexandru Ardelean 	.release = iio_chrdev_release,
1849a980e046SJonathan Cameron };
1850a980e046SJonathan Cameron 
18518ebaa3ffSAlexandru Ardelean static const struct file_operations iio_event_fileops = {
18528ebaa3ffSAlexandru Ardelean 	.owner = THIS_MODULE,
18538ebaa3ffSAlexandru Ardelean 	.llseek = noop_llseek,
18548ebaa3ffSAlexandru Ardelean 	.unlocked_ioctl = iio_ioctl,
18558ebaa3ffSAlexandru Ardelean 	.compat_ioctl = compat_ptr_ioctl,
18568ebaa3ffSAlexandru Ardelean 	.open = iio_chrdev_open,
18578ebaa3ffSAlexandru Ardelean 	.release = iio_chrdev_release,
18588ebaa3ffSAlexandru Ardelean };
18598ebaa3ffSAlexandru Ardelean 
18608f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
18618f5d8727SVlad Dogaru {
18628f5d8727SVlad Dogaru 	int i, j;
18638f5d8727SVlad Dogaru 	const struct iio_chan_spec *channels = indio_dev->channels;
18648f5d8727SVlad Dogaru 
18658f5d8727SVlad Dogaru 	if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
18668f5d8727SVlad Dogaru 		return 0;
18678f5d8727SVlad Dogaru 
18688f5d8727SVlad Dogaru 	for (i = 0; i < indio_dev->num_channels - 1; i++) {
18698f5d8727SVlad Dogaru 		if (channels[i].scan_index < 0)
18708f5d8727SVlad Dogaru 			continue;
18718f5d8727SVlad Dogaru 		for (j = i + 1; j < indio_dev->num_channels; j++)
18728f5d8727SVlad Dogaru 			if (channels[i].scan_index == channels[j].scan_index) {
18738f5d8727SVlad Dogaru 				dev_err(&indio_dev->dev,
18748f5d8727SVlad Dogaru 					"Duplicate scan index %d\n",
18758f5d8727SVlad Dogaru 					channels[i].scan_index);
18768f5d8727SVlad Dogaru 				return -EINVAL;
18778f5d8727SVlad Dogaru 			}
18788f5d8727SVlad Dogaru 	}
18798f5d8727SVlad Dogaru 
18808f5d8727SVlad Dogaru 	return 0;
18818f5d8727SVlad Dogaru }
18828f5d8727SVlad Dogaru 
18830e0761f8SPaul Cercueil static int iio_check_extended_name(const struct iio_dev *indio_dev)
18840e0761f8SPaul Cercueil {
18850e0761f8SPaul Cercueil 	unsigned int i;
18860e0761f8SPaul Cercueil 
18870e0761f8SPaul Cercueil 	if (!indio_dev->info->read_label)
18880e0761f8SPaul Cercueil 		return 0;
18890e0761f8SPaul Cercueil 
18900e0761f8SPaul Cercueil 	for (i = 0; i < indio_dev->num_channels; i++) {
18910e0761f8SPaul Cercueil 		if (indio_dev->channels[i].extend_name) {
18920e0761f8SPaul Cercueil 			dev_err(&indio_dev->dev,
18930e0761f8SPaul Cercueil 				"Cannot use labels and extend_name at the same time\n");
18940e0761f8SPaul Cercueil 			return -EINVAL;
18950e0761f8SPaul Cercueil 		}
18960e0761f8SPaul Cercueil 	}
18970e0761f8SPaul Cercueil 
18980e0761f8SPaul Cercueil 	return 0;
18990e0761f8SPaul Cercueil }
19000e0761f8SPaul Cercueil 
1901a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1902a980e046SJonathan Cameron 
190363b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1904a980e046SJonathan Cameron {
19058ebaa3ffSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
190681366556SAndy Shevchenko 	struct fwnode_handle *fwnode;
1907a980e046SJonathan Cameron 	int ret;
1908a980e046SJonathan Cameron 
1909a17cb784SAlexandru Ardelean 	if (!indio_dev->info)
1910a17cb784SAlexandru Ardelean 		return -EINVAL;
1911a17cb784SAlexandru Ardelean 
19126eaf9f6aSJonathan Cameron 	iio_dev_opaque->driver_module = this_mod;
191317d82b47SGuenter Roeck 
191481366556SAndy Shevchenko 	/* If the calling driver did not initialize firmware node, do it here */
191581366556SAndy Shevchenko 	if (dev_fwnode(&indio_dev->dev))
191681366556SAndy Shevchenko 		fwnode = dev_fwnode(&indio_dev->dev);
191781366556SAndy Shevchenko 	else
191881366556SAndy Shevchenko 		fwnode = dev_fwnode(indio_dev->dev.parent);
191981366556SAndy Shevchenko 	device_set_node(&indio_dev->dev, fwnode);
192081366556SAndy Shevchenko 
192181366556SAndy Shevchenko 	fwnode_property_read_string(fwnode, "label", &indio_dev->label);
19222c3d0c9fSPhil Reid 
19238f5d8727SVlad Dogaru 	ret = iio_check_unique_scan_index(indio_dev);
19248f5d8727SVlad Dogaru 	if (ret < 0)
19258f5d8727SVlad Dogaru 		return ret;
19268f5d8727SVlad Dogaru 
19270e0761f8SPaul Cercueil 	ret = iio_check_extended_name(indio_dev);
19280e0761f8SPaul Cercueil 	if (ret < 0)
19290e0761f8SPaul Cercueil 		return ret;
19300e0761f8SPaul Cercueil 
19318915aacaSGreg Kroah-Hartman 	iio_device_register_debugfs(indio_dev);
19323e1b6c95SLars-Peter Clausen 
1933ee708e6bSAlexandru Ardelean 	ret = iio_buffers_alloc_sysfs_and_mask(indio_dev);
19343e1b6c95SLars-Peter Clausen 	if (ret) {
19353e1b6c95SLars-Peter Clausen 		dev_err(indio_dev->dev.parent,
19363e1b6c95SLars-Peter Clausen 			"Failed to create buffer sysfs interfaces\n");
19373e1b6c95SLars-Peter Clausen 		goto error_unreg_debugfs;
19383e1b6c95SLars-Peter Clausen 	}
19393e1b6c95SLars-Peter Clausen 
1940a980e046SJonathan Cameron 	ret = iio_device_register_sysfs(indio_dev);
1941a980e046SJonathan Cameron 	if (ret) {
1942a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1943a980e046SJonathan Cameron 			"Failed to register sysfs interfaces\n");
19443e1b6c95SLars-Peter Clausen 		goto error_buffer_free_sysfs;
1945a980e046SJonathan Cameron 	}
1946a980e046SJonathan Cameron 	ret = iio_device_register_eventset(indio_dev);
1947a980e046SJonathan Cameron 	if (ret) {
1948a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1949a980e046SJonathan Cameron 			"Failed to register event set\n");
1950a980e046SJonathan Cameron 		goto error_free_sysfs;
1951a980e046SJonathan Cameron 	}
1952d89e119aSBenjamin Gaignard 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1953a980e046SJonathan Cameron 		iio_device_register_trigger_consumer(indio_dev);
1954a980e046SJonathan Cameron 
1955a980e046SJonathan Cameron 	if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1956a980e046SJonathan Cameron 		indio_dev->setup_ops == NULL)
1957a980e046SJonathan Cameron 		indio_dev->setup_ops = &noop_ring_setup_ops;
1958a980e046SJonathan Cameron 
1959ee708e6bSAlexandru Ardelean 	if (iio_dev_opaque->attached_buffers_cnt)
1960396f7234SJonathan Cameron 		cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops);
19618ebaa3ffSAlexandru Ardelean 	else if (iio_dev_opaque->event_interface)
1962396f7234SJonathan Cameron 		cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops);
196363b19547SJonathan Cameron 
1964ee708e6bSAlexandru Ardelean 	if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) {
196515ea2878SJonathan Cameron 		indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id);
1966396f7234SJonathan Cameron 		iio_dev_opaque->chrdev.owner = this_mod;
19678ebaa3ffSAlexandru Ardelean 	}
196838923911SLogan Gunthorpe 
196932f17172SAlexandru Ardelean 	/* assign device groups now; they should be all registered now */
197032f17172SAlexandru Ardelean 	indio_dev->dev.groups = iio_dev_opaque->groups;
197132f17172SAlexandru Ardelean 
1972396f7234SJonathan Cameron 	ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev);
1973a980e046SJonathan Cameron 	if (ret < 0)
19740d5b7daeSLars-Peter Clausen 		goto error_unreg_eventset;
1975a980e046SJonathan Cameron 
19760d5b7daeSLars-Peter Clausen 	return 0;
197738923911SLogan Gunthorpe 
1978a980e046SJonathan Cameron error_unreg_eventset:
1979a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1980a980e046SJonathan Cameron error_free_sysfs:
1981a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
19823e1b6c95SLars-Peter Clausen error_buffer_free_sysfs:
1983ee708e6bSAlexandru Ardelean 	iio_buffers_free_sysfs_and_mask(indio_dev);
1984a980e046SJonathan Cameron error_unreg_debugfs:
1985a980e046SJonathan Cameron 	iio_device_unregister_debugfs(indio_dev);
1986a980e046SJonathan Cameron 	return ret;
1987a980e046SJonathan Cameron }
198863b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register);
1989a980e046SJonathan Cameron 
1990a7e57dceSSachin Kamat /**
1991a7e57dceSSachin Kamat  * iio_device_unregister() - unregister a device from the IIO subsystem
1992a7e57dceSSachin Kamat  * @indio_dev:		Device structure representing the device.
1993a7e57dceSSachin Kamat  **/
1994a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev)
1995a980e046SJonathan Cameron {
1996396f7234SJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1997396f7234SJonathan Cameron 
1998396f7234SJonathan Cameron 	cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev);
1999a87c82e4SLars-Peter Clausen 
2000b804e2b7SJonathan Cameron 	mutex_lock(&iio_dev_opaque->info_exist_lock);
20017f75591fSFabrice Gasnier 
2002bc4c9612SLars-Peter Clausen 	iio_device_unregister_debugfs(indio_dev);
20030d5b7daeSLars-Peter Clausen 
2004a87c82e4SLars-Peter Clausen 	iio_disable_all_buffers(indio_dev);
2005a87c82e4SLars-Peter Clausen 
2006a980e046SJonathan Cameron 	indio_dev->info = NULL;
2007d2f0a48fSLars-Peter Clausen 
2008d2f0a48fSLars-Peter Clausen 	iio_device_wakeup_eventset(indio_dev);
2009d2f0a48fSLars-Peter Clausen 	iio_buffer_wakeup_poll(indio_dev);
2010d2f0a48fSLars-Peter Clausen 
2011b804e2b7SJonathan Cameron 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
20123e1b6c95SLars-Peter Clausen 
2013ee708e6bSAlexandru Ardelean 	iio_buffers_free_sysfs_and_mask(indio_dev);
2014a980e046SJonathan Cameron }
2015a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister);
20168caa07c0SSachin Kamat 
2017cf5724e9SYicong Yang static void devm_iio_device_unreg(void *indio_dev)
20188caa07c0SSachin Kamat {
2019cf5724e9SYicong Yang 	iio_device_unregister(indio_dev);
20208caa07c0SSachin Kamat }
20218caa07c0SSachin Kamat 
202263b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
202363b19547SJonathan Cameron 			       struct module *this_mod)
20248caa07c0SSachin Kamat {
20258caa07c0SSachin Kamat 	int ret;
20268caa07c0SSachin Kamat 
202763b19547SJonathan Cameron 	ret = __iio_device_register(indio_dev, this_mod);
2028cf5724e9SYicong Yang 	if (ret)
20298caa07c0SSachin Kamat 		return ret;
2030cf5724e9SYicong Yang 
2031cf5724e9SYicong Yang 	return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev);
20328caa07c0SSachin Kamat }
203363b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register);
20348caa07c0SSachin Kamat 
20358caa07c0SSachin Kamat /**
203608a33805SAlison Schofield  * iio_device_claim_direct_mode - Keep device in direct mode
203708a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
203808a33805SAlison Schofield  *
203908a33805SAlison Schofield  * If the device is in direct mode it is guaranteed to stay
204008a33805SAlison Schofield  * that way until iio_device_release_direct_mode() is called.
204108a33805SAlison Schofield  *
204208a33805SAlison Schofield  * Use with iio_device_release_direct_mode()
204308a33805SAlison Schofield  *
204408a33805SAlison Schofield  * Returns: 0 on success, -EBUSY on failure
204508a33805SAlison Schofield  */
204608a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
204708a33805SAlison Schofield {
204808a33805SAlison Schofield 	mutex_lock(&indio_dev->mlock);
204908a33805SAlison Schofield 
205008a33805SAlison Schofield 	if (iio_buffer_enabled(indio_dev)) {
205108a33805SAlison Schofield 		mutex_unlock(&indio_dev->mlock);
205208a33805SAlison Schofield 		return -EBUSY;
205308a33805SAlison Schofield 	}
205408a33805SAlison Schofield 	return 0;
205508a33805SAlison Schofield }
205608a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
205708a33805SAlison Schofield 
205808a33805SAlison Schofield /**
205908a33805SAlison Schofield  * iio_device_release_direct_mode - releases claim on direct mode
206008a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
206108a33805SAlison Schofield  *
206208a33805SAlison Schofield  * Release the claim. Device is no longer guaranteed to stay
206308a33805SAlison Schofield  * in direct mode.
206408a33805SAlison Schofield  *
206508a33805SAlison Schofield  * Use with iio_device_claim_direct_mode()
206608a33805SAlison Schofield  */
206708a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev)
206808a33805SAlison Schofield {
206908a33805SAlison Schofield 	mutex_unlock(&indio_dev->mlock);
207008a33805SAlison Schofield }
207108a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
207208a33805SAlison Schofield 
20738c576f87SMiquel Raynal /**
20748c576f87SMiquel Raynal  * iio_device_get_current_mode() - helper function providing read-only access to
207551570c9dSMiquel Raynal  *				   the opaque @currentmode variable
20768c576f87SMiquel Raynal  * @indio_dev:			   IIO device structure for device
20778c576f87SMiquel Raynal  */
20788c576f87SMiquel Raynal int iio_device_get_current_mode(struct iio_dev *indio_dev)
20798c576f87SMiquel Raynal {
208051570c9dSMiquel Raynal 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
208151570c9dSMiquel Raynal 
208251570c9dSMiquel Raynal 	return iio_dev_opaque->currentmode;
20838c576f87SMiquel Raynal }
20848c576f87SMiquel Raynal EXPORT_SYMBOL_GPL(iio_device_get_current_mode);
20858c576f87SMiquel Raynal 
2086a980e046SJonathan Cameron subsys_initcall(iio_init);
2087a980e046SJonathan Cameron module_exit(iio_exit);
2088a980e046SJonathan Cameron 
2089c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
2090a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core");
2091a980e046SJonathan Cameron MODULE_LICENSE("GPL");
2092