xref: /linux/drivers/iio/industrialio-core.c (revision 69f0793eb60dacd153388974bbaaa1d3184d171d)
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>
28a980e046SJonathan Cameron #include "iio_core.h"
29a980e046SJonathan Cameron #include "iio_core_trigger.h"
30a980e046SJonathan Cameron #include <linux/iio/sysfs.h>
31a980e046SJonathan Cameron #include <linux/iio/events.h>
329e69c935SLars-Peter Clausen #include <linux/iio/buffer.h>
3333dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h>
34a980e046SJonathan Cameron 
35a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */
36a980e046SJonathan Cameron static DEFINE_IDA(iio_ida);
37a980e046SJonathan Cameron 
38a980e046SJonathan Cameron static dev_t iio_devt;
39a980e046SJonathan Cameron 
40a980e046SJonathan Cameron #define IIO_DEV_MAX 256
41a980e046SJonathan Cameron struct bus_type iio_bus_type = {
42a980e046SJonathan Cameron 	.name = "iio",
43a980e046SJonathan Cameron };
44a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type);
45a980e046SJonathan Cameron 
46a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry;
47a980e046SJonathan Cameron 
48a980e046SJonathan Cameron static const char * const iio_direction[] = {
49a980e046SJonathan Cameron 	[0] = "in",
50a980e046SJonathan Cameron 	[1] = "out",
51a980e046SJonathan Cameron };
52a980e046SJonathan Cameron 
53a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = {
54a980e046SJonathan Cameron 	[IIO_VOLTAGE] = "voltage",
55a980e046SJonathan Cameron 	[IIO_CURRENT] = "current",
56a980e046SJonathan Cameron 	[IIO_POWER] = "power",
57a980e046SJonathan Cameron 	[IIO_ACCEL] = "accel",
58a980e046SJonathan Cameron 	[IIO_ANGL_VEL] = "anglvel",
59a980e046SJonathan Cameron 	[IIO_MAGN] = "magn",
60a980e046SJonathan Cameron 	[IIO_LIGHT] = "illuminance",
61a980e046SJonathan Cameron 	[IIO_INTENSITY] = "intensity",
62a980e046SJonathan Cameron 	[IIO_PROXIMITY] = "proximity",
63a980e046SJonathan Cameron 	[IIO_TEMP] = "temp",
64a980e046SJonathan Cameron 	[IIO_INCLI] = "incli",
65a980e046SJonathan Cameron 	[IIO_ROT] = "rot",
66a980e046SJonathan Cameron 	[IIO_ANGL] = "angl",
67a980e046SJonathan Cameron 	[IIO_TIMESTAMP] = "timestamp",
68a980e046SJonathan Cameron 	[IIO_CAPACITANCE] = "capacitance",
69a6b12855SMichael Hennerich 	[IIO_ALTVOLTAGE] = "altvoltage",
7021cd1fabSJon Brenner 	[IIO_CCT] = "cct",
71c4f0c693SLars-Peter Clausen 	[IIO_PRESSURE] = "pressure",
72ac216aa2SHarald Geyer 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
7355aebeb9SDaniel Baluta 	[IIO_ACTIVITY] = "activity",
74a88bfe78SIrina Tirdea 	[IIO_STEPS] = "steps",
7572c66644SIrina Tirdea 	[IIO_ENERGY] = "energy",
76cc3c9eecSIrina Tirdea 	[IIO_DISTANCE] = "distance",
775a1a9329SIrina Tirdea 	[IIO_VELOCITY] = "velocity",
788ff6b3bcSMatt Ranostay 	[IIO_CONCENTRATION] = "concentration",
79d38d5469SMatt Ranostay 	[IIO_RESISTANCE] = "resistance",
80ecb3a7ccSMatt Ranostay 	[IIO_PH] = "ph",
81d409404cSPeter Meerwald-Stadler 	[IIO_UVINDEX] = "uvindex",
824b9d2090SMatt Ranostay 	[IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
831a8f324aSWilliam Breathitt Gray 	[IIO_COUNT] = "count",
841a8f324aSWilliam Breathitt Gray 	[IIO_INDEX] = "index",
85571299d0SSong Hongyan 	[IIO_GRAVITY]  = "gravity",
863055a6cfSEugen Hristev 	[IIO_POSITIONRELATIVE]  = "positionrelative",
87c73314e6SMathieu Othacehe 	[IIO_PHASE] = "phase",
8817abc9ecSTomasz Duszynski 	[IIO_MASSCONCENTRATION] = "massconcentration",
89a980e046SJonathan Cameron };
90a980e046SJonathan Cameron 
91a980e046SJonathan Cameron static const char * const iio_modifier_names[] = {
92a980e046SJonathan Cameron 	[IIO_MOD_X] = "x",
93a980e046SJonathan Cameron 	[IIO_MOD_Y] = "y",
94a980e046SJonathan Cameron 	[IIO_MOD_Z] = "z",
954b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y] = "x&y",
964b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Z] = "x&z",
974b8d8015SPeter Meerwald 	[IIO_MOD_Y_AND_Z] = "y&z",
984b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
994b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y] = "x|y",
1004b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Z] = "x|z",
1014b8d8015SPeter Meerwald 	[IIO_MOD_Y_OR_Z] = "y|z",
1024b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
1038f5879b2SJonathan Cameron 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
104cf82cb81SJonathan Cameron 	[IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
105a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_BOTH] = "both",
106a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_IR] = "ir",
10721cd1fabSJon Brenner 	[IIO_MOD_LIGHT_CLEAR] = "clear",
10821cd1fabSJon Brenner 	[IIO_MOD_LIGHT_RED] = "red",
10921cd1fabSJon Brenner 	[IIO_MOD_LIGHT_GREEN] = "green",
11021cd1fabSJon Brenner 	[IIO_MOD_LIGHT_BLUE] = "blue",
1112c5ff1f9SPeter Meerwald-Stadler 	[IIO_MOD_LIGHT_UV] = "uv",
112c0e4e0fdSMaxime Roussin-Bélanger 	[IIO_MOD_LIGHT_DUV] = "duv",
1135082f405SSrinivas Pandruvada 	[IIO_MOD_QUATERNION] = "quaternion",
114638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_AMBIENT] = "ambient",
115638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_OBJECT] = "object",
11611b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
11711b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE] = "from_north_true",
11811b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
11911b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
12055aebeb9SDaniel Baluta 	[IIO_MOD_RUNNING] = "running",
12155aebeb9SDaniel Baluta 	[IIO_MOD_JOGGING] = "jogging",
12255aebeb9SDaniel Baluta 	[IIO_MOD_WALKING] = "walking",
12355aebeb9SDaniel Baluta 	[IIO_MOD_STILL] = "still",
1245a1a9329SIrina Tirdea 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
1251ce87f21SLars-Peter Clausen 	[IIO_MOD_I] = "i",
1261ce87f21SLars-Peter Clausen 	[IIO_MOD_Q] = "q",
1278ff6b3bcSMatt Ranostay 	[IIO_MOD_CO2] = "co2",
1288ff6b3bcSMatt Ranostay 	[IIO_MOD_VOC] = "voc",
12917abc9ecSTomasz Duszynski 	[IIO_MOD_PM1] = "pm1",
13017abc9ecSTomasz Duszynski 	[IIO_MOD_PM2P5] = "pm2p5",
13117abc9ecSTomasz Duszynski 	[IIO_MOD_PM4] = "pm4",
13217abc9ecSTomasz Duszynski 	[IIO_MOD_PM10] = "pm10",
133a980e046SJonathan Cameron };
134a980e046SJonathan Cameron 
135a980e046SJonathan Cameron /* relies on pairs of these shared then separate */
136a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = {
137a980e046SJonathan Cameron 	[IIO_CHAN_INFO_RAW] = "raw",
138a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PROCESSED] = "input",
139a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SCALE] = "scale",
140a980e046SJonathan Cameron 	[IIO_CHAN_INFO_OFFSET] = "offset",
141a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
142a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
143a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK] = "peak_raw",
144a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
145a980e046SJonathan Cameron 	[IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
146a980e046SJonathan Cameron 	[IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
147a980e046SJonathan Cameron 	[IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
148a980e046SJonathan Cameron 	= "filter_low_pass_3db_frequency",
1493f7f642bSMartin Fuzzey 	[IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
1503f7f642bSMartin Fuzzey 	= "filter_high_pass_3db_frequency",
151a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
152a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_FREQUENCY] = "frequency",
153a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_PHASE] = "phase",
154b65d6212SMichael Hennerich 	[IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
1557c9ab035Ssrinivas pandruvada 	[IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
156899d90bdSPeter Meerwald 	[IIO_CHAN_INFO_INT_TIME] = "integration_time",
157a88bfe78SIrina Tirdea 	[IIO_CHAN_INFO_ENABLE] = "en",
158bcdf28fbSIrina Tirdea 	[IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
159d37f6836SIrina Tirdea 	[IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
1602f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
1612f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
162c8a85854SVianney le Clément de Saint-Marcq 	[IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
163faaa4495SIrina Tirdea 	[IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
164ddfb97d8SAndrea Merello 	[IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
165a980e046SJonathan Cameron };
166a980e046SJonathan Cameron 
167a7e57dceSSachin Kamat /**
168a7e57dceSSachin Kamat  * iio_find_channel_from_si() - get channel from its scan index
169a7e57dceSSachin Kamat  * @indio_dev:		device
170a7e57dceSSachin Kamat  * @si:			scan index to match
171a7e57dceSSachin Kamat  */
172a980e046SJonathan Cameron const struct iio_chan_spec
173a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
174a980e046SJonathan Cameron {
175a980e046SJonathan Cameron 	int i;
176a980e046SJonathan Cameron 
177a980e046SJonathan Cameron 	for (i = 0; i < indio_dev->num_channels; i++)
178a980e046SJonathan Cameron 		if (indio_dev->channels[i].scan_index == si)
179a980e046SJonathan Cameron 			return &indio_dev->channels[i];
180a980e046SJonathan Cameron 	return NULL;
181a980e046SJonathan Cameron }
182a980e046SJonathan Cameron 
183a980e046SJonathan Cameron /* This turns up an awful lot */
184a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev,
185a980e046SJonathan Cameron 			    struct device_attribute *attr,
186a980e046SJonathan Cameron 			    char *buf)
187a980e046SJonathan Cameron {
188a980e046SJonathan Cameron 	return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
189a980e046SJonathan Cameron }
190a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr);
191a980e046SJonathan Cameron 
192*69f0793eSGwendal Grignou /**
193*69f0793eSGwendal Grignou  * iio_device_set_clock() - Set current timestamping clock for the device
194*69f0793eSGwendal Grignou  * @indio_dev: IIO device structure containing the device
195*69f0793eSGwendal Grignou  * @clock_id: timestamping clock posix identifier to set.
196*69f0793eSGwendal Grignou  */
197*69f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
198bc2b7dabSGregor Boirie {
199bc2b7dabSGregor Boirie 	int ret;
200bc2b7dabSGregor Boirie 	const struct iio_event_interface *ev_int = indio_dev->event_interface;
201bc2b7dabSGregor Boirie 
202bc2b7dabSGregor Boirie 	ret = mutex_lock_interruptible(&indio_dev->mlock);
203bc2b7dabSGregor Boirie 	if (ret)
204bc2b7dabSGregor Boirie 		return ret;
205bc2b7dabSGregor Boirie 	if ((ev_int && iio_event_enabled(ev_int)) ||
206bc2b7dabSGregor Boirie 	    iio_buffer_enabled(indio_dev)) {
207bc2b7dabSGregor Boirie 		mutex_unlock(&indio_dev->mlock);
208bc2b7dabSGregor Boirie 		return -EBUSY;
209bc2b7dabSGregor Boirie 	}
210bc2b7dabSGregor Boirie 	indio_dev->clock_id = clock_id;
211bc2b7dabSGregor Boirie 	mutex_unlock(&indio_dev->mlock);
212bc2b7dabSGregor Boirie 
213bc2b7dabSGregor Boirie 	return 0;
214bc2b7dabSGregor Boirie }
215*69f0793eSGwendal Grignou EXPORT_SYMBOL(iio_device_set_clock);
216bc2b7dabSGregor Boirie 
217bc2b7dabSGregor Boirie /**
218bc2b7dabSGregor Boirie  * iio_get_time_ns() - utility function to get a time stamp for events etc
219bc2b7dabSGregor Boirie  * @indio_dev: device
220bc2b7dabSGregor Boirie  */
221bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev)
222bc2b7dabSGregor Boirie {
22345e7f5d2SArnd Bergmann 	struct timespec64 tp;
224bc2b7dabSGregor Boirie 
225bc2b7dabSGregor Boirie 	switch (iio_device_get_clock(indio_dev)) {
226bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
22745e7f5d2SArnd Bergmann 		return ktime_get_real_ns();
228bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
22945e7f5d2SArnd Bergmann 		return ktime_get_ns();
230bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
23145e7f5d2SArnd Bergmann 		return ktime_get_raw_ns();
232bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
23345e7f5d2SArnd Bergmann 		return ktime_to_ns(ktime_get_coarse_real());
234bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
23545e7f5d2SArnd Bergmann 		ktime_get_coarse_ts64(&tp);
23645e7f5d2SArnd Bergmann 		return timespec64_to_ns(&tp);
237bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
2389285ec4cSJason A. Donenfeld 		return ktime_get_boottime_ns();
239bc2b7dabSGregor Boirie 	case CLOCK_TAI:
2409285ec4cSJason A. Donenfeld 		return ktime_get_clocktai_ns();
241bc2b7dabSGregor Boirie 	default:
242bc2b7dabSGregor Boirie 		BUG();
243bc2b7dabSGregor Boirie 	}
244bc2b7dabSGregor Boirie }
245bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns);
246bc2b7dabSGregor Boirie 
247bc2b7dabSGregor Boirie /**
248bc2b7dabSGregor Boirie  * iio_get_time_res() - utility function to get time stamp clock resolution in
249bc2b7dabSGregor Boirie  *                      nano seconds.
250bc2b7dabSGregor Boirie  * @indio_dev: device
251bc2b7dabSGregor Boirie  */
252bc2b7dabSGregor Boirie unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
253bc2b7dabSGregor Boirie {
254bc2b7dabSGregor Boirie 	switch (iio_device_get_clock(indio_dev)) {
255bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
256bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
257bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
258bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
259bc2b7dabSGregor Boirie 	case CLOCK_TAI:
260bc2b7dabSGregor Boirie 		return hrtimer_resolution;
261bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
262bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
263bc2b7dabSGregor Boirie 		return LOW_RES_NSEC;
264bc2b7dabSGregor Boirie 	default:
265bc2b7dabSGregor Boirie 		BUG();
266bc2b7dabSGregor Boirie 	}
267bc2b7dabSGregor Boirie }
268bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_res);
269bc2b7dabSGregor Boirie 
270a980e046SJonathan Cameron static int __init iio_init(void)
271a980e046SJonathan Cameron {
272a980e046SJonathan Cameron 	int ret;
273a980e046SJonathan Cameron 
274a980e046SJonathan Cameron 	/* Register sysfs bus */
275a980e046SJonathan Cameron 	ret  = bus_register(&iio_bus_type);
276a980e046SJonathan Cameron 	if (ret < 0) {
2773176dd5dSSachin Kamat 		pr_err("could not register bus type\n");
278a980e046SJonathan Cameron 		goto error_nothing;
279a980e046SJonathan Cameron 	}
280a980e046SJonathan Cameron 
281a980e046SJonathan Cameron 	ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
282a980e046SJonathan Cameron 	if (ret < 0) {
2833176dd5dSSachin Kamat 		pr_err("failed to allocate char dev region\n");
284a980e046SJonathan Cameron 		goto error_unregister_bus_type;
285a980e046SJonathan Cameron 	}
286a980e046SJonathan Cameron 
287a980e046SJonathan Cameron 	iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
288a980e046SJonathan Cameron 
289a980e046SJonathan Cameron 	return 0;
290a980e046SJonathan Cameron 
291a980e046SJonathan Cameron error_unregister_bus_type:
292a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
293a980e046SJonathan Cameron error_nothing:
294a980e046SJonathan Cameron 	return ret;
295a980e046SJonathan Cameron }
296a980e046SJonathan Cameron 
297a980e046SJonathan Cameron static void __exit iio_exit(void)
298a980e046SJonathan Cameron {
299a980e046SJonathan Cameron 	if (iio_devt)
300a980e046SJonathan Cameron 		unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
301a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
302a980e046SJonathan Cameron 	debugfs_remove(iio_debugfs_dentry);
303a980e046SJonathan Cameron }
304a980e046SJonathan Cameron 
305a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS)
306a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
307a980e046SJonathan Cameron 			      size_t count, loff_t *ppos)
308a980e046SJonathan Cameron {
309a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
310a980e046SJonathan Cameron 	char buf[20];
311a980e046SJonathan Cameron 	unsigned val = 0;
312a980e046SJonathan Cameron 	ssize_t len;
313a980e046SJonathan Cameron 	int ret;
314a980e046SJonathan Cameron 
315a980e046SJonathan Cameron 	ret = indio_dev->info->debugfs_reg_access(indio_dev,
316a980e046SJonathan Cameron 						  indio_dev->cached_reg_addr,
317a980e046SJonathan Cameron 						  0, &val);
3183d62c78aSMatt Fornero 	if (ret) {
319a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
3203d62c78aSMatt Fornero 		return ret;
3213d62c78aSMatt Fornero 	}
322a980e046SJonathan Cameron 
323a980e046SJonathan Cameron 	len = snprintf(buf, sizeof(buf), "0x%X\n", val);
324a980e046SJonathan Cameron 
325a980e046SJonathan Cameron 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
326a980e046SJonathan Cameron }
327a980e046SJonathan Cameron 
328a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file,
329a980e046SJonathan Cameron 		     const char __user *userbuf, size_t count, loff_t *ppos)
330a980e046SJonathan Cameron {
331a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
332a980e046SJonathan Cameron 	unsigned reg, val;
333a980e046SJonathan Cameron 	char buf[80];
334a980e046SJonathan Cameron 	int ret;
335a980e046SJonathan Cameron 
336a980e046SJonathan Cameron 	count = min_t(size_t, count, (sizeof(buf)-1));
337a980e046SJonathan Cameron 	if (copy_from_user(buf, userbuf, count))
338a980e046SJonathan Cameron 		return -EFAULT;
339a980e046SJonathan Cameron 
340a980e046SJonathan Cameron 	buf[count] = 0;
341a980e046SJonathan Cameron 
342a980e046SJonathan Cameron 	ret = sscanf(buf, "%i %i", &reg, &val);
343a980e046SJonathan Cameron 
344a980e046SJonathan Cameron 	switch (ret) {
345a980e046SJonathan Cameron 	case 1:
346a980e046SJonathan Cameron 		indio_dev->cached_reg_addr = reg;
347a980e046SJonathan Cameron 		break;
348a980e046SJonathan Cameron 	case 2:
349a980e046SJonathan Cameron 		indio_dev->cached_reg_addr = reg;
350a980e046SJonathan Cameron 		ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
351a980e046SJonathan Cameron 							  val, NULL);
352a980e046SJonathan Cameron 		if (ret) {
353a980e046SJonathan Cameron 			dev_err(indio_dev->dev.parent, "%s: write failed\n",
354a980e046SJonathan Cameron 				__func__);
355a980e046SJonathan Cameron 			return ret;
356a980e046SJonathan Cameron 		}
357a980e046SJonathan Cameron 		break;
358a980e046SJonathan Cameron 	default:
359a980e046SJonathan Cameron 		return -EINVAL;
360a980e046SJonathan Cameron 	}
361a980e046SJonathan Cameron 
362a980e046SJonathan Cameron 	return count;
363a980e046SJonathan Cameron }
364a980e046SJonathan Cameron 
365a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = {
3665a28c873SAxel Lin 	.open = simple_open,
367a980e046SJonathan Cameron 	.read = iio_debugfs_read_reg,
368a980e046SJonathan Cameron 	.write = iio_debugfs_write_reg,
369a980e046SJonathan Cameron };
370a980e046SJonathan Cameron 
371a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
372a980e046SJonathan Cameron {
373a980e046SJonathan Cameron 	debugfs_remove_recursive(indio_dev->debugfs_dentry);
374a980e046SJonathan Cameron }
375a980e046SJonathan Cameron 
3768915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev)
377a980e046SJonathan Cameron {
378a980e046SJonathan Cameron 	if (indio_dev->info->debugfs_reg_access == NULL)
3798915aacaSGreg Kroah-Hartman 		return;
380a980e046SJonathan Cameron 
381abd5a2fbSAxel Lin 	if (!iio_debugfs_dentry)
3828915aacaSGreg Kroah-Hartman 		return;
383a980e046SJonathan Cameron 
384a980e046SJonathan Cameron 	indio_dev->debugfs_dentry =
385a980e046SJonathan Cameron 		debugfs_create_dir(dev_name(&indio_dev->dev),
386a980e046SJonathan Cameron 				   iio_debugfs_dentry);
387a980e046SJonathan Cameron 
3888915aacaSGreg Kroah-Hartman 	debugfs_create_file("direct_reg_access", 0644,
3898915aacaSGreg Kroah-Hartman 			    indio_dev->debugfs_dentry, indio_dev,
3908915aacaSGreg Kroah-Hartman 			    &iio_debugfs_reg_fops);
391a980e046SJonathan Cameron }
392a980e046SJonathan Cameron #else
3938915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev)
394a980e046SJonathan Cameron {
395a980e046SJonathan Cameron }
396a980e046SJonathan Cameron 
397a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
398a980e046SJonathan Cameron {
399a980e046SJonathan Cameron }
400a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */
401a980e046SJonathan Cameron 
402a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev,
403a980e046SJonathan Cameron 				     struct device_attribute *attr,
404a980e046SJonathan Cameron 				     char *buf)
405a980e046SJonathan Cameron {
406e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
407a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
408a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
409a980e046SJonathan Cameron 
410a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
411a980e046SJonathan Cameron 
412fc6d1139SMichael Hennerich 	return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
413a980e046SJonathan Cameron }
414a980e046SJonathan Cameron 
415a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev,
416a980e046SJonathan Cameron 				     struct device_attribute *attr,
417a980e046SJonathan Cameron 				     const char *buf,
418a980e046SJonathan Cameron 					 size_t len)
419a980e046SJonathan Cameron {
420e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
421a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
422a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
423a980e046SJonathan Cameron 
424a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
425a980e046SJonathan Cameron 
426fc6d1139SMichael Hennerich 	return ext_info->write(indio_dev, ext_info->private,
427fc6d1139SMichael Hennerich 			       this_attr->c, buf, len);
428a980e046SJonathan Cameron }
429a980e046SJonathan Cameron 
4305212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
4315212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
4325212cc8aSLars-Peter Clausen {
4335212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4345212cc8aSLars-Peter Clausen 	unsigned int i;
4355212cc8aSLars-Peter Clausen 	size_t len = 0;
4365212cc8aSLars-Peter Clausen 
4375212cc8aSLars-Peter Clausen 	if (!e->num_items)
4385212cc8aSLars-Peter Clausen 		return 0;
4395212cc8aSLars-Peter Clausen 
4405212cc8aSLars-Peter Clausen 	for (i = 0; i < e->num_items; ++i)
44174dcd439SLars-Peter Clausen 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
4425212cc8aSLars-Peter Clausen 
4435212cc8aSLars-Peter Clausen 	/* replace last space with a newline */
4445212cc8aSLars-Peter Clausen 	buf[len - 1] = '\n';
4455212cc8aSLars-Peter Clausen 
4465212cc8aSLars-Peter Clausen 	return len;
4475212cc8aSLars-Peter Clausen }
4485212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read);
4495212cc8aSLars-Peter Clausen 
4505212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev,
4515212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
4525212cc8aSLars-Peter Clausen {
4535212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4545212cc8aSLars-Peter Clausen 	int i;
4555212cc8aSLars-Peter Clausen 
4565212cc8aSLars-Peter Clausen 	if (!e->get)
4575212cc8aSLars-Peter Clausen 		return -EINVAL;
4585212cc8aSLars-Peter Clausen 
4595212cc8aSLars-Peter Clausen 	i = e->get(indio_dev, chan);
4605212cc8aSLars-Peter Clausen 	if (i < 0)
4615212cc8aSLars-Peter Clausen 		return i;
4625212cc8aSLars-Peter Clausen 	else if (i >= e->num_items)
4635212cc8aSLars-Peter Clausen 		return -EINVAL;
4645212cc8aSLars-Peter Clausen 
465598db581SKees Cook 	return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
4665212cc8aSLars-Peter Clausen }
4675212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read);
4685212cc8aSLars-Peter Clausen 
4695212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev,
4705212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
4715212cc8aSLars-Peter Clausen 	size_t len)
4725212cc8aSLars-Peter Clausen {
4735212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4745212cc8aSLars-Peter Clausen 	int ret;
4755212cc8aSLars-Peter Clausen 
4765212cc8aSLars-Peter Clausen 	if (!e->set)
4775212cc8aSLars-Peter Clausen 		return -EINVAL;
4785212cc8aSLars-Peter Clausen 
47902e9a0ffSAndy Shevchenko 	ret = __sysfs_match_string(e->items, e->num_items, buf);
48002e9a0ffSAndy Shevchenko 	if (ret < 0)
48102e9a0ffSAndy Shevchenko 		return ret;
4825212cc8aSLars-Peter Clausen 
48302e9a0ffSAndy Shevchenko 	ret = e->set(indio_dev, chan, ret);
4845212cc8aSLars-Peter Clausen 	return ret ? ret : len;
4855212cc8aSLars-Peter Clausen }
4865212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write);
4875212cc8aSLars-Peter Clausen 
488dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = {
489dfc57732SGregor Boirie 	.rotation = {
490dfc57732SGregor Boirie 		"1", "0", "0",
491dfc57732SGregor Boirie 		"0", "1", "0",
492dfc57732SGregor Boirie 		"0", "0", "1"
493dfc57732SGregor Boirie 	}
494dfc57732SGregor Boirie };
495dfc57732SGregor Boirie 
496dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev,
497dfc57732SGregor Boirie 				    struct iio_mount_matrix *matrix)
498dfc57732SGregor Boirie {
499dfc57732SGregor Boirie 	*matrix = iio_mount_idmatrix;
500dfc57732SGregor Boirie 	dev_info(dev, "mounting matrix not found: using identity...\n");
501dfc57732SGregor Boirie 	return 0;
502dfc57732SGregor Boirie }
503dfc57732SGregor Boirie 
504dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
505dfc57732SGregor Boirie 			      const struct iio_chan_spec *chan, char *buf)
506dfc57732SGregor Boirie {
507dfc57732SGregor Boirie 	const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
508dfc57732SGregor Boirie 					      priv)(indio_dev, chan);
509dfc57732SGregor Boirie 
510dfc57732SGregor Boirie 	if (IS_ERR(mtx))
511dfc57732SGregor Boirie 		return PTR_ERR(mtx);
512dfc57732SGregor Boirie 
513dfc57732SGregor Boirie 	if (!mtx)
514dfc57732SGregor Boirie 		mtx = &iio_mount_idmatrix;
515dfc57732SGregor Boirie 
516dfc57732SGregor Boirie 	return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
517dfc57732SGregor Boirie 			mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
518dfc57732SGregor Boirie 			mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
519dfc57732SGregor Boirie 			mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
520dfc57732SGregor Boirie }
521dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
522dfc57732SGregor Boirie 
523dfc57732SGregor Boirie /**
524fb158971SAndy Shevchenko  * iio_read_mount_matrix() - retrieve iio device mounting matrix from
525fb158971SAndy Shevchenko  *                           device "mount-matrix" property
526dfc57732SGregor Boirie  * @dev:	device the mounting matrix property is assigned to
527dfc57732SGregor Boirie  * @propname:	device specific mounting matrix property name
528dfc57732SGregor Boirie  * @matrix:	where to store retrieved matrix
529dfc57732SGregor Boirie  *
530dfc57732SGregor Boirie  * If device is assigned no mounting matrix property, a default 3x3 identity
531dfc57732SGregor Boirie  * matrix will be filled in.
532dfc57732SGregor Boirie  *
533dfc57732SGregor Boirie  * Return: 0 if success, or a negative error code on failure.
534dfc57732SGregor Boirie  */
535fb158971SAndy Shevchenko int iio_read_mount_matrix(struct device *dev, const char *propname,
536dfc57732SGregor Boirie 			  struct iio_mount_matrix *matrix)
537dfc57732SGregor Boirie {
538fb158971SAndy Shevchenko 	size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
539fb158971SAndy Shevchenko 	int err;
540dfc57732SGregor Boirie 
541fb158971SAndy Shevchenko 	err = device_property_read_string_array(dev, propname,
542fb158971SAndy Shevchenko 						matrix->rotation, len);
543fb158971SAndy Shevchenko 	if (err == len)
544dfc57732SGregor Boirie 		return 0;
545dfc57732SGregor Boirie 
546dfc57732SGregor Boirie 	if (err >= 0)
547dfc57732SGregor Boirie 		/* Invalid number of matrix entries. */
548dfc57732SGregor Boirie 		return -EINVAL;
549dfc57732SGregor Boirie 
550dfc57732SGregor Boirie 	if (err != -EINVAL)
551dfc57732SGregor Boirie 		/* Invalid matrix declaration format. */
552dfc57732SGregor Boirie 		return err;
553dfc57732SGregor Boirie 
554dfc57732SGregor Boirie 	/* Matrix was not declared at all: fallback to identity. */
555dfc57732SGregor Boirie 	return iio_setup_mount_idmatrix(dev, matrix);
556dfc57732SGregor Boirie }
557fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix);
558dfc57732SGregor Boirie 
55951239600SJonathan Cameron static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
56051239600SJonathan Cameron 				  int size, const int *vals)
56151239600SJonathan Cameron {
56251239600SJonathan Cameron 	unsigned long long tmp;
56351239600SJonathan Cameron 	int tmp0, tmp1;
56451239600SJonathan Cameron 	bool scale_db = false;
56551239600SJonathan Cameron 
56651239600SJonathan Cameron 	switch (type) {
56751239600SJonathan Cameron 	case IIO_VAL_INT:
56851239600SJonathan Cameron 		return snprintf(buf, len, "%d", vals[0]);
56951239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO_DB:
57051239600SJonathan Cameron 		scale_db = true;
5719d793c1aSGustavo A. R. Silva 		/* fall through */
57251239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO:
57351239600SJonathan Cameron 		if (vals[1] < 0)
57451239600SJonathan Cameron 			return snprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
57551239600SJonathan Cameron 					-vals[1], scale_db ? " dB" : "");
57651239600SJonathan Cameron 		else
57751239600SJonathan Cameron 			return snprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
57851239600SJonathan Cameron 					scale_db ? " dB" : "");
57951239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_NANO:
58051239600SJonathan Cameron 		if (vals[1] < 0)
58151239600SJonathan Cameron 			return snprintf(buf, len, "-%d.%09u", abs(vals[0]),
58251239600SJonathan Cameron 					-vals[1]);
58351239600SJonathan Cameron 		else
58451239600SJonathan Cameron 			return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
58551239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL:
58651239600SJonathan Cameron 		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
58751239600SJonathan Cameron 		tmp1 = vals[1];
58851239600SJonathan Cameron 		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
58951239600SJonathan Cameron 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
59051239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL_LOG2:
5917fd6592dSNikolaus Schulz 		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
5927fd6592dSNikolaus Schulz 		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
5937fd6592dSNikolaus Schulz 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
59451239600SJonathan Cameron 	case IIO_VAL_INT_MULTIPLE:
59551239600SJonathan Cameron 	{
59651239600SJonathan Cameron 		int i;
59751239600SJonathan Cameron 		int l = 0;
59851239600SJonathan Cameron 
59951239600SJonathan Cameron 		for (i = 0; i < size; ++i) {
60051239600SJonathan Cameron 			l += snprintf(&buf[l], len - l, "%d ", vals[i]);
60151239600SJonathan Cameron 			if (l >= len)
60251239600SJonathan Cameron 				break;
60351239600SJonathan Cameron 		}
60451239600SJonathan Cameron 		return l;
60551239600SJonathan Cameron 	}
6068cb34036SAndrea Merello 	case IIO_VAL_CHAR:
6078cb34036SAndrea Merello 		return snprintf(buf, len, "%c", (char)vals[0]);
60851239600SJonathan Cameron 	default:
60951239600SJonathan Cameron 		return 0;
61051239600SJonathan Cameron 	}
61151239600SJonathan Cameron }
61251239600SJonathan Cameron 
6133661f3f5SLars-Peter Clausen /**
6143661f3f5SLars-Peter Clausen  * iio_format_value() - Formats a IIO value into its string representation
6152498dcf6SCristina Opriceana  * @buf:	The buffer to which the formatted value gets written
61651239600SJonathan Cameron  *		which is assumed to be big enough (i.e. PAGE_SIZE).
617c175cb7cSRandy Dunlap  * @type:	One of the IIO_VAL_* constants. This decides how the val
6182498dcf6SCristina Opriceana  *		and val2 parameters are formatted.
6192498dcf6SCristina Opriceana  * @size:	Number of IIO value entries contained in vals
6202498dcf6SCristina Opriceana  * @vals:	Pointer to the values, exact meaning depends on the
6212498dcf6SCristina Opriceana  *		type parameter.
6222498dcf6SCristina Opriceana  *
6232498dcf6SCristina Opriceana  * Return: 0 by default, a negative number on failure or the
6242498dcf6SCristina Opriceana  *	   total number of characters written for a type that belongs
625c175cb7cSRandy Dunlap  *	   to the IIO_VAL_* constant.
6263661f3f5SLars-Peter Clausen  */
6279fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
628a980e046SJonathan Cameron {
62951239600SJonathan Cameron 	ssize_t len;
630a980e046SJonathan Cameron 
63151239600SJonathan Cameron 	len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
63251239600SJonathan Cameron 	if (len >= PAGE_SIZE - 1)
63351239600SJonathan Cameron 		return -EFBIG;
6349fbfb4b3SSrinivas Pandruvada 
63551239600SJonathan Cameron 	return len + sprintf(buf + len, "\n");
63667eedba3SMichael Hennerich }
6377d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value);
638a980e046SJonathan Cameron 
6393661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev,
6403661f3f5SLars-Peter Clausen 				     struct device_attribute *attr,
6413661f3f5SLars-Peter Clausen 				     char *buf)
6423661f3f5SLars-Peter Clausen {
6433661f3f5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
6443661f3f5SLars-Peter Clausen 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
6459fbfb4b3SSrinivas Pandruvada 	int vals[INDIO_MAX_RAW_ELEMENTS];
6469fbfb4b3SSrinivas Pandruvada 	int ret;
6479fbfb4b3SSrinivas Pandruvada 	int val_len = 2;
6489fbfb4b3SSrinivas Pandruvada 
6499fbfb4b3SSrinivas Pandruvada 	if (indio_dev->info->read_raw_multi)
6509fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
6519fbfb4b3SSrinivas Pandruvada 							INDIO_MAX_RAW_ELEMENTS,
6529fbfb4b3SSrinivas Pandruvada 							vals, &val_len,
6539fbfb4b3SSrinivas Pandruvada 							this_attr->address);
6549fbfb4b3SSrinivas Pandruvada 	else
6559fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
6569fbfb4b3SSrinivas Pandruvada 				    &vals[0], &vals[1], this_attr->address);
6573661f3f5SLars-Peter Clausen 
6583661f3f5SLars-Peter Clausen 	if (ret < 0)
6593661f3f5SLars-Peter Clausen 		return ret;
6603661f3f5SLars-Peter Clausen 
6619fbfb4b3SSrinivas Pandruvada 	return iio_format_value(buf, ret, val_len, vals);
6623661f3f5SLars-Peter Clausen }
6633661f3f5SLars-Peter Clausen 
66451239600SJonathan Cameron static ssize_t iio_format_avail_list(char *buf, const int *vals,
66551239600SJonathan Cameron 				     int type, int length)
66651239600SJonathan Cameron {
66751239600SJonathan Cameron 	int i;
66851239600SJonathan Cameron 	ssize_t len = 0;
66951239600SJonathan Cameron 
67051239600SJonathan Cameron 	switch (type) {
67151239600SJonathan Cameron 	case IIO_VAL_INT:
67251239600SJonathan Cameron 		for (i = 0; i < length; i++) {
67351239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
67451239600SJonathan Cameron 						  type, 1, &vals[i]);
67551239600SJonathan Cameron 			if (len >= PAGE_SIZE)
67651239600SJonathan Cameron 				return -EFBIG;
67751239600SJonathan Cameron 			if (i < length - 1)
67851239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
67951239600SJonathan Cameron 						" ");
68051239600SJonathan Cameron 			else
68151239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
68251239600SJonathan Cameron 						"\n");
68351239600SJonathan Cameron 			if (len >= PAGE_SIZE)
68451239600SJonathan Cameron 				return -EFBIG;
68551239600SJonathan Cameron 		}
68651239600SJonathan Cameron 		break;
68751239600SJonathan Cameron 	default:
68851239600SJonathan Cameron 		for (i = 0; i < length / 2; i++) {
68951239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
69051239600SJonathan Cameron 						  type, 2, &vals[i * 2]);
69151239600SJonathan Cameron 			if (len >= PAGE_SIZE)
69251239600SJonathan Cameron 				return -EFBIG;
69351239600SJonathan Cameron 			if (i < length / 2 - 1)
69451239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
69551239600SJonathan Cameron 						" ");
69651239600SJonathan Cameron 			else
69751239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
69851239600SJonathan Cameron 						"\n");
69951239600SJonathan Cameron 			if (len >= PAGE_SIZE)
70051239600SJonathan Cameron 				return -EFBIG;
70151239600SJonathan Cameron 		}
70251239600SJonathan Cameron 	}
70351239600SJonathan Cameron 
70451239600SJonathan Cameron 	return len;
70551239600SJonathan Cameron }
70651239600SJonathan Cameron 
70751239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
70851239600SJonathan Cameron {
70951239600SJonathan Cameron 	int i;
71051239600SJonathan Cameron 	ssize_t len;
71151239600SJonathan Cameron 
71251239600SJonathan Cameron 	len = snprintf(buf, PAGE_SIZE, "[");
71351239600SJonathan Cameron 	switch (type) {
71451239600SJonathan Cameron 	case IIO_VAL_INT:
71551239600SJonathan Cameron 		for (i = 0; i < 3; i++) {
71651239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
71751239600SJonathan Cameron 						  type, 1, &vals[i]);
71851239600SJonathan Cameron 			if (len >= PAGE_SIZE)
71951239600SJonathan Cameron 				return -EFBIG;
72051239600SJonathan Cameron 			if (i < 2)
72151239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
72251239600SJonathan Cameron 						" ");
72351239600SJonathan Cameron 			else
72451239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
72551239600SJonathan Cameron 						"]\n");
72651239600SJonathan Cameron 			if (len >= PAGE_SIZE)
72751239600SJonathan Cameron 				return -EFBIG;
72851239600SJonathan Cameron 		}
72951239600SJonathan Cameron 		break;
73051239600SJonathan Cameron 	default:
73151239600SJonathan Cameron 		for (i = 0; i < 3; i++) {
73251239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
73351239600SJonathan Cameron 						  type, 2, &vals[i * 2]);
73451239600SJonathan Cameron 			if (len >= PAGE_SIZE)
73551239600SJonathan Cameron 				return -EFBIG;
73651239600SJonathan Cameron 			if (i < 2)
73751239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
73851239600SJonathan Cameron 						" ");
73951239600SJonathan Cameron 			else
74051239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
74151239600SJonathan Cameron 						"]\n");
74251239600SJonathan Cameron 			if (len >= PAGE_SIZE)
74351239600SJonathan Cameron 				return -EFBIG;
74451239600SJonathan Cameron 		}
74551239600SJonathan Cameron 	}
74651239600SJonathan Cameron 
74751239600SJonathan Cameron 	return len;
74851239600SJonathan Cameron }
74951239600SJonathan Cameron 
75051239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev,
75151239600SJonathan Cameron 					   struct device_attribute *attr,
75251239600SJonathan Cameron 					   char *buf)
75351239600SJonathan Cameron {
75451239600SJonathan Cameron 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
75551239600SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
75651239600SJonathan Cameron 	const int *vals;
75751239600SJonathan Cameron 	int ret;
75851239600SJonathan Cameron 	int length;
75951239600SJonathan Cameron 	int type;
76051239600SJonathan Cameron 
76151239600SJonathan Cameron 	ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
76251239600SJonathan Cameron 					  &vals, &type, &length,
76351239600SJonathan Cameron 					  this_attr->address);
76451239600SJonathan Cameron 
76551239600SJonathan Cameron 	if (ret < 0)
76651239600SJonathan Cameron 		return ret;
76751239600SJonathan Cameron 	switch (ret) {
76851239600SJonathan Cameron 	case IIO_AVAIL_LIST:
76951239600SJonathan Cameron 		return iio_format_avail_list(buf, vals, type, length);
77051239600SJonathan Cameron 	case IIO_AVAIL_RANGE:
77151239600SJonathan Cameron 		return iio_format_avail_range(buf, vals, type);
77251239600SJonathan Cameron 	default:
77351239600SJonathan Cameron 		return -EINVAL;
77451239600SJonathan Cameron 	}
77551239600SJonathan Cameron }
77651239600SJonathan Cameron 
7776807d721SLars-Peter Clausen /**
7786807d721SLars-Peter Clausen  * iio_str_to_fixpoint() - Parse a fixed-point number from a string
7796807d721SLars-Peter Clausen  * @str: The string to parse
7806807d721SLars-Peter Clausen  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
7816807d721SLars-Peter Clausen  * @integer: The integer part of the number
7826807d721SLars-Peter Clausen  * @fract: The fractional part of the number
7836807d721SLars-Peter Clausen  *
7846807d721SLars-Peter Clausen  * Returns 0 on success, or a negative error code if the string could not be
7856807d721SLars-Peter Clausen  * parsed.
7866807d721SLars-Peter Clausen  */
7876807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult,
7886807d721SLars-Peter Clausen 	int *integer, int *fract)
7896807d721SLars-Peter Clausen {
7906807d721SLars-Peter Clausen 	int i = 0, f = 0;
7916807d721SLars-Peter Clausen 	bool integer_part = true, negative = false;
7926807d721SLars-Peter Clausen 
793f47dff32SSean Nyekjaer 	if (fract_mult == 0) {
794f47dff32SSean Nyekjaer 		*fract = 0;
795f47dff32SSean Nyekjaer 
796f47dff32SSean Nyekjaer 		return kstrtoint(str, 0, integer);
797f47dff32SSean Nyekjaer 	}
798f47dff32SSean Nyekjaer 
7996807d721SLars-Peter Clausen 	if (str[0] == '-') {
8006807d721SLars-Peter Clausen 		negative = true;
8016807d721SLars-Peter Clausen 		str++;
8026807d721SLars-Peter Clausen 	} else if (str[0] == '+') {
8036807d721SLars-Peter Clausen 		str++;
8046807d721SLars-Peter Clausen 	}
8056807d721SLars-Peter Clausen 
8066807d721SLars-Peter Clausen 	while (*str) {
8076807d721SLars-Peter Clausen 		if ('0' <= *str && *str <= '9') {
8086807d721SLars-Peter Clausen 			if (integer_part) {
8096807d721SLars-Peter Clausen 				i = i * 10 + *str - '0';
8106807d721SLars-Peter Clausen 			} else {
8116807d721SLars-Peter Clausen 				f += fract_mult * (*str - '0');
8126807d721SLars-Peter Clausen 				fract_mult /= 10;
8136807d721SLars-Peter Clausen 			}
8146807d721SLars-Peter Clausen 		} else if (*str == '\n') {
8156807d721SLars-Peter Clausen 			if (*(str + 1) == '\0')
8166807d721SLars-Peter Clausen 				break;
8176807d721SLars-Peter Clausen 			else
8186807d721SLars-Peter Clausen 				return -EINVAL;
8196807d721SLars-Peter Clausen 		} else if (*str == '.' && integer_part) {
8206807d721SLars-Peter Clausen 			integer_part = false;
8216807d721SLars-Peter Clausen 		} else {
8226807d721SLars-Peter Clausen 			return -EINVAL;
8236807d721SLars-Peter Clausen 		}
8246807d721SLars-Peter Clausen 		str++;
8256807d721SLars-Peter Clausen 	}
8266807d721SLars-Peter Clausen 
8276807d721SLars-Peter Clausen 	if (negative) {
8286807d721SLars-Peter Clausen 		if (i)
8296807d721SLars-Peter Clausen 			i = -i;
8306807d721SLars-Peter Clausen 		else
8316807d721SLars-Peter Clausen 			f = -f;
8326807d721SLars-Peter Clausen 	}
8336807d721SLars-Peter Clausen 
8346807d721SLars-Peter Clausen 	*integer = i;
8356807d721SLars-Peter Clausen 	*fract = f;
8366807d721SLars-Peter Clausen 
8376807d721SLars-Peter Clausen 	return 0;
8386807d721SLars-Peter Clausen }
8396807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
8406807d721SLars-Peter Clausen 
841a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev,
842a980e046SJonathan Cameron 				      struct device_attribute *attr,
843a980e046SJonathan Cameron 				      const char *buf,
844a980e046SJonathan Cameron 				      size_t len)
845a980e046SJonathan Cameron {
846e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
847a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
8486807d721SLars-Peter Clausen 	int ret, fract_mult = 100000;
8498cb34036SAndrea Merello 	int integer, fract = 0;
8508cb34036SAndrea Merello 	bool is_char = false;
851a980e046SJonathan Cameron 
852a980e046SJonathan Cameron 	/* Assumes decimal - precision based on number of digits */
853a980e046SJonathan Cameron 	if (!indio_dev->info->write_raw)
854a980e046SJonathan Cameron 		return -EINVAL;
855a980e046SJonathan Cameron 
856a980e046SJonathan Cameron 	if (indio_dev->info->write_raw_get_fmt)
857a980e046SJonathan Cameron 		switch (indio_dev->info->write_raw_get_fmt(indio_dev,
858a980e046SJonathan Cameron 			this_attr->c, this_attr->address)) {
859f47dff32SSean Nyekjaer 		case IIO_VAL_INT:
860f47dff32SSean Nyekjaer 			fract_mult = 0;
861f47dff32SSean Nyekjaer 			break;
862a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_MICRO:
863a980e046SJonathan Cameron 			fract_mult = 100000;
864a980e046SJonathan Cameron 			break;
865a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_NANO:
866a980e046SJonathan Cameron 			fract_mult = 100000000;
867a980e046SJonathan Cameron 			break;
8688cb34036SAndrea Merello 		case IIO_VAL_CHAR:
8698cb34036SAndrea Merello 			is_char = true;
8708cb34036SAndrea Merello 			break;
871a980e046SJonathan Cameron 		default:
872a980e046SJonathan Cameron 			return -EINVAL;
873a980e046SJonathan Cameron 		}
874a980e046SJonathan Cameron 
8758cb34036SAndrea Merello 	if (is_char) {
8768cb34036SAndrea Merello 		char ch;
8778cb34036SAndrea Merello 
8788cb34036SAndrea Merello 		if (sscanf(buf, "%c", &ch) != 1)
8798cb34036SAndrea Merello 			return -EINVAL;
8808cb34036SAndrea Merello 		integer = ch;
8818cb34036SAndrea Merello 	} else {
8826807d721SLars-Peter Clausen 		ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
8836807d721SLars-Peter Clausen 		if (ret)
8846807d721SLars-Peter Clausen 			return ret;
8858cb34036SAndrea Merello 	}
886a980e046SJonathan Cameron 
887a980e046SJonathan Cameron 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
888a980e046SJonathan Cameron 					 integer, fract, this_attr->address);
889a980e046SJonathan Cameron 	if (ret)
890a980e046SJonathan Cameron 		return ret;
891a980e046SJonathan Cameron 
892a980e046SJonathan Cameron 	return len;
893a980e046SJonathan Cameron }
894a980e046SJonathan Cameron 
895a980e046SJonathan Cameron static
896a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr,
897a980e046SJonathan Cameron 			   const char *postfix,
898a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
899a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
900a980e046SJonathan Cameron 					       struct device_attribute *attr,
901a980e046SJonathan Cameron 					       char *buf),
902a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
903a980e046SJonathan Cameron 						struct device_attribute *attr,
904a980e046SJonathan Cameron 						const char *buf,
905a980e046SJonathan Cameron 						size_t len),
9063704432fSJonathan Cameron 			   enum iio_shared_by shared_by)
907a980e046SJonathan Cameron {
9083704432fSJonathan Cameron 	int ret = 0;
9097bbcf7e1SLars-Peter Clausen 	char *name = NULL;
9103704432fSJonathan Cameron 	char *full_postfix;
911a980e046SJonathan Cameron 	sysfs_attr_init(&dev_attr->attr);
912a980e046SJonathan Cameron 
913a980e046SJonathan Cameron 	/* Build up postfix of <extend_name>_<modifier>_postfix */
9143704432fSJonathan Cameron 	if (chan->modified && (shared_by == IIO_SEPARATE)) {
915a980e046SJonathan Cameron 		if (chan->extend_name)
916a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
917a980e046SJonathan Cameron 						 iio_modifier_names[chan
918a980e046SJonathan Cameron 								    ->channel2],
919a980e046SJonathan Cameron 						 chan->extend_name,
920a980e046SJonathan Cameron 						 postfix);
921a980e046SJonathan Cameron 		else
922a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
923a980e046SJonathan Cameron 						 iio_modifier_names[chan
924a980e046SJonathan Cameron 								    ->channel2],
925a980e046SJonathan Cameron 						 postfix);
926a980e046SJonathan Cameron 	} else {
92777bfa8baSLars-Peter Clausen 		if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
928a980e046SJonathan Cameron 			full_postfix = kstrdup(postfix, GFP_KERNEL);
929a980e046SJonathan Cameron 		else
930a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL,
931a980e046SJonathan Cameron 						 "%s_%s",
932a980e046SJonathan Cameron 						 chan->extend_name,
933a980e046SJonathan Cameron 						 postfix);
934a980e046SJonathan Cameron 	}
9353704432fSJonathan Cameron 	if (full_postfix == NULL)
9363704432fSJonathan Cameron 		return -ENOMEM;
937a980e046SJonathan Cameron 
938a980e046SJonathan Cameron 	if (chan->differential) { /* Differential can not have modifier */
9393704432fSJonathan Cameron 		switch (shared_by) {
940c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
9417bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
942c006ec83SJonathan Cameron 			break;
943c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
9447bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
945c006ec83SJonathan Cameron 						iio_direction[chan->output],
946c006ec83SJonathan Cameron 						full_postfix);
947c006ec83SJonathan Cameron 			break;
9483704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
9497bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
950a980e046SJonathan Cameron 					    iio_direction[chan->output],
951a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
952a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
953a980e046SJonathan Cameron 					    full_postfix);
9543704432fSJonathan Cameron 			break;
9553704432fSJonathan Cameron 		case IIO_SEPARATE:
9563704432fSJonathan Cameron 			if (!chan->indexed) {
957231bfe53SDan Carpenter 				WARN(1, "Differential channels must be indexed\n");
9583704432fSJonathan Cameron 				ret = -EINVAL;
9593704432fSJonathan Cameron 				goto error_free_full_postfix;
9603704432fSJonathan Cameron 			}
9617bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL,
9623704432fSJonathan Cameron 					    "%s_%s%d-%s%d_%s",
963a980e046SJonathan Cameron 					    iio_direction[chan->output],
964a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
965a980e046SJonathan Cameron 					    chan->channel,
966a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
967a980e046SJonathan Cameron 					    chan->channel2,
968a980e046SJonathan Cameron 					    full_postfix);
9693704432fSJonathan Cameron 			break;
970a980e046SJonathan Cameron 		}
971a980e046SJonathan Cameron 	} else { /* Single ended */
9723704432fSJonathan Cameron 		switch (shared_by) {
973c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
9747bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
975c006ec83SJonathan Cameron 			break;
976c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
9777bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
978c006ec83SJonathan Cameron 						iio_direction[chan->output],
979c006ec83SJonathan Cameron 						full_postfix);
980c006ec83SJonathan Cameron 			break;
9813704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
9827bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s_%s",
983a980e046SJonathan Cameron 					    iio_direction[chan->output],
984a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
985a980e046SJonathan Cameron 					    full_postfix);
9863704432fSJonathan Cameron 			break;
9873704432fSJonathan Cameron 
9883704432fSJonathan Cameron 		case IIO_SEPARATE:
9893704432fSJonathan Cameron 			if (chan->indexed)
9907bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
991a980e046SJonathan Cameron 						    iio_direction[chan->output],
992a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
993a980e046SJonathan Cameron 						    chan->channel,
994a980e046SJonathan Cameron 						    full_postfix);
995a980e046SJonathan Cameron 			else
9967bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s_%s",
997a980e046SJonathan Cameron 						    iio_direction[chan->output],
998a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
999a980e046SJonathan Cameron 						    full_postfix);
10003704432fSJonathan Cameron 			break;
10013704432fSJonathan Cameron 		}
1002a980e046SJonathan Cameron 	}
10037bbcf7e1SLars-Peter Clausen 	if (name == NULL) {
1004a980e046SJonathan Cameron 		ret = -ENOMEM;
1005a980e046SJonathan Cameron 		goto error_free_full_postfix;
1006a980e046SJonathan Cameron 	}
10077bbcf7e1SLars-Peter Clausen 	dev_attr->attr.name = name;
1008a980e046SJonathan Cameron 
1009a980e046SJonathan Cameron 	if (readfunc) {
1010a980e046SJonathan Cameron 		dev_attr->attr.mode |= S_IRUGO;
1011a980e046SJonathan Cameron 		dev_attr->show = readfunc;
1012a980e046SJonathan Cameron 	}
1013a980e046SJonathan Cameron 
1014a980e046SJonathan Cameron 	if (writefunc) {
1015a980e046SJonathan Cameron 		dev_attr->attr.mode |= S_IWUSR;
1016a980e046SJonathan Cameron 		dev_attr->store = writefunc;
1017a980e046SJonathan Cameron 	}
10187bbcf7e1SLars-Peter Clausen 
1019a980e046SJonathan Cameron error_free_full_postfix:
1020a980e046SJonathan Cameron 	kfree(full_postfix);
10213704432fSJonathan Cameron 
1022a980e046SJonathan Cameron 	return ret;
1023a980e046SJonathan Cameron }
1024a980e046SJonathan Cameron 
1025a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1026a980e046SJonathan Cameron {
1027a980e046SJonathan Cameron 	kfree(dev_attr->attr.name);
1028a980e046SJonathan Cameron }
1029a980e046SJonathan Cameron 
1030a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix,
1031a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
1032a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
1033a980e046SJonathan Cameron 					       struct device_attribute *attr,
1034a980e046SJonathan Cameron 					       char *buf),
1035a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
1036a980e046SJonathan Cameron 						struct device_attribute *attr,
1037a980e046SJonathan Cameron 						const char *buf,
1038a980e046SJonathan Cameron 						size_t len),
1039a980e046SJonathan Cameron 			   u64 mask,
10403704432fSJonathan Cameron 			   enum iio_shared_by shared_by,
1041a980e046SJonathan Cameron 			   struct device *dev,
1042a980e046SJonathan Cameron 			   struct list_head *attr_list)
1043a980e046SJonathan Cameron {
1044a980e046SJonathan Cameron 	int ret;
1045a980e046SJonathan Cameron 	struct iio_dev_attr *iio_attr, *t;
1046a980e046SJonathan Cameron 
1047670c1103SSachin Kamat 	iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
104892825ff9SHartmut Knaack 	if (iio_attr == NULL)
104992825ff9SHartmut Knaack 		return -ENOMEM;
1050a980e046SJonathan Cameron 	ret = __iio_device_attr_init(&iio_attr->dev_attr,
1051a980e046SJonathan Cameron 				     postfix, chan,
10523704432fSJonathan Cameron 				     readfunc, writefunc, shared_by);
1053a980e046SJonathan Cameron 	if (ret)
1054a980e046SJonathan Cameron 		goto error_iio_dev_attr_free;
1055a980e046SJonathan Cameron 	iio_attr->c = chan;
1056a980e046SJonathan Cameron 	iio_attr->address = mask;
1057a980e046SJonathan Cameron 	list_for_each_entry(t, attr_list, l)
1058a980e046SJonathan Cameron 		if (strcmp(t->dev_attr.attr.name,
1059a980e046SJonathan Cameron 			   iio_attr->dev_attr.attr.name) == 0) {
10603704432fSJonathan Cameron 			if (shared_by == IIO_SEPARATE)
1061a980e046SJonathan Cameron 				dev_err(dev, "tried to double register : %s\n",
1062a980e046SJonathan Cameron 					t->dev_attr.attr.name);
1063a980e046SJonathan Cameron 			ret = -EBUSY;
1064a980e046SJonathan Cameron 			goto error_device_attr_deinit;
1065a980e046SJonathan Cameron 		}
1066a980e046SJonathan Cameron 	list_add(&iio_attr->l, attr_list);
1067a980e046SJonathan Cameron 
1068a980e046SJonathan Cameron 	return 0;
1069a980e046SJonathan Cameron 
1070a980e046SJonathan Cameron error_device_attr_deinit:
1071a980e046SJonathan Cameron 	__iio_device_attr_deinit(&iio_attr->dev_attr);
1072a980e046SJonathan Cameron error_iio_dev_attr_free:
1073a980e046SJonathan Cameron 	kfree(iio_attr);
1074a980e046SJonathan Cameron 	return ret;
1075a980e046SJonathan Cameron }
1076a980e046SJonathan Cameron 
10773704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
10783704432fSJonathan Cameron 					 struct iio_chan_spec const *chan,
10793704432fSJonathan Cameron 					 enum iio_shared_by shared_by,
10803704432fSJonathan Cameron 					 const long *infomask)
10813704432fSJonathan Cameron {
10823704432fSJonathan Cameron 	int i, ret, attrcount = 0;
10833704432fSJonathan Cameron 
10841016d567SOrson Zhai 	for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1085ef4b4856SJonathan Cameron 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1086ef4b4856SJonathan Cameron 			return -EINVAL;
10873704432fSJonathan Cameron 		ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
10883704432fSJonathan Cameron 					     chan,
10893704432fSJonathan Cameron 					     &iio_read_channel_info,
10903704432fSJonathan Cameron 					     &iio_write_channel_info,
10913704432fSJonathan Cameron 					     i,
10923704432fSJonathan Cameron 					     shared_by,
10933704432fSJonathan Cameron 					     &indio_dev->dev,
10943704432fSJonathan Cameron 					     &indio_dev->channel_attr_list);
10953704432fSJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
10963704432fSJonathan Cameron 			continue;
10973704432fSJonathan Cameron 		else if (ret < 0)
10983704432fSJonathan Cameron 			return ret;
10993704432fSJonathan Cameron 		attrcount++;
11003704432fSJonathan Cameron 	}
11013704432fSJonathan Cameron 
11023704432fSJonathan Cameron 	return attrcount;
11033704432fSJonathan Cameron }
11043704432fSJonathan Cameron 
110551239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
110651239600SJonathan Cameron 					       struct iio_chan_spec const *chan,
110751239600SJonathan Cameron 					       enum iio_shared_by shared_by,
110851239600SJonathan Cameron 					       const long *infomask)
110951239600SJonathan Cameron {
111051239600SJonathan Cameron 	int i, ret, attrcount = 0;
111151239600SJonathan Cameron 	char *avail_postfix;
111251239600SJonathan Cameron 
11131016d567SOrson Zhai 	for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1114936d3e53SYoung Xiao 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1115936d3e53SYoung Xiao 			return -EINVAL;
111651239600SJonathan Cameron 		avail_postfix = kasprintf(GFP_KERNEL,
111751239600SJonathan Cameron 					  "%s_available",
111851239600SJonathan Cameron 					  iio_chan_info_postfix[i]);
111951239600SJonathan Cameron 		if (!avail_postfix)
112051239600SJonathan Cameron 			return -ENOMEM;
112151239600SJonathan Cameron 
112251239600SJonathan Cameron 		ret = __iio_add_chan_devattr(avail_postfix,
112351239600SJonathan Cameron 					     chan,
112451239600SJonathan Cameron 					     &iio_read_channel_info_avail,
112551239600SJonathan Cameron 					     NULL,
112651239600SJonathan Cameron 					     i,
112751239600SJonathan Cameron 					     shared_by,
112851239600SJonathan Cameron 					     &indio_dev->dev,
112951239600SJonathan Cameron 					     &indio_dev->channel_attr_list);
113051239600SJonathan Cameron 		kfree(avail_postfix);
113151239600SJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
113251239600SJonathan Cameron 			continue;
113351239600SJonathan Cameron 		else if (ret < 0)
113451239600SJonathan Cameron 			return ret;
113551239600SJonathan Cameron 		attrcount++;
113651239600SJonathan Cameron 	}
113751239600SJonathan Cameron 
113851239600SJonathan Cameron 	return attrcount;
113951239600SJonathan Cameron }
114051239600SJonathan Cameron 
1141a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1142a980e046SJonathan Cameron 					struct iio_chan_spec const *chan)
1143a980e046SJonathan Cameron {
1144a980e046SJonathan Cameron 	int ret, attrcount = 0;
1145a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
1146a980e046SJonathan Cameron 
1147a980e046SJonathan Cameron 	if (chan->channel < 0)
1148a980e046SJonathan Cameron 		return 0;
11493704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
11503704432fSJonathan Cameron 					    IIO_SEPARATE,
11513704432fSJonathan Cameron 					    &chan->info_mask_separate);
11528655cc49SJonathan Cameron 	if (ret < 0)
11533704432fSJonathan Cameron 		return ret;
11543704432fSJonathan Cameron 	attrcount += ret;
11553704432fSJonathan Cameron 
115651239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
115751239600SJonathan Cameron 						  IIO_SEPARATE,
115851239600SJonathan Cameron 						  &chan->
115951239600SJonathan Cameron 						  info_mask_separate_available);
116051239600SJonathan Cameron 	if (ret < 0)
116151239600SJonathan Cameron 		return ret;
116251239600SJonathan Cameron 	attrcount += ret;
116351239600SJonathan Cameron 
11643704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
11653704432fSJonathan Cameron 					    IIO_SHARED_BY_TYPE,
11663704432fSJonathan Cameron 					    &chan->info_mask_shared_by_type);
11673704432fSJonathan Cameron 	if (ret < 0)
11683704432fSJonathan Cameron 		return ret;
11693704432fSJonathan Cameron 	attrcount += ret;
1170a980e046SJonathan Cameron 
117151239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
117251239600SJonathan Cameron 						  IIO_SHARED_BY_TYPE,
117351239600SJonathan Cameron 						  &chan->
117451239600SJonathan Cameron 						  info_mask_shared_by_type_available);
117551239600SJonathan Cameron 	if (ret < 0)
117651239600SJonathan Cameron 		return ret;
117751239600SJonathan Cameron 	attrcount += ret;
117851239600SJonathan Cameron 
1179c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1180c006ec83SJonathan Cameron 					    IIO_SHARED_BY_DIR,
1181c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_dir);
1182c006ec83SJonathan Cameron 	if (ret < 0)
1183c006ec83SJonathan Cameron 		return ret;
1184c006ec83SJonathan Cameron 	attrcount += ret;
1185c006ec83SJonathan Cameron 
118651239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
118751239600SJonathan Cameron 						  IIO_SHARED_BY_DIR,
118851239600SJonathan Cameron 						  &chan->info_mask_shared_by_dir_available);
118951239600SJonathan Cameron 	if (ret < 0)
119051239600SJonathan Cameron 		return ret;
119151239600SJonathan Cameron 	attrcount += ret;
119251239600SJonathan Cameron 
1193c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1194c006ec83SJonathan Cameron 					    IIO_SHARED_BY_ALL,
1195c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_all);
1196c006ec83SJonathan Cameron 	if (ret < 0)
1197c006ec83SJonathan Cameron 		return ret;
1198c006ec83SJonathan Cameron 	attrcount += ret;
1199c006ec83SJonathan Cameron 
120051239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
120151239600SJonathan Cameron 						  IIO_SHARED_BY_ALL,
120251239600SJonathan Cameron 						  &chan->info_mask_shared_by_all_available);
120351239600SJonathan Cameron 	if (ret < 0)
120451239600SJonathan Cameron 		return ret;
120551239600SJonathan Cameron 	attrcount += ret;
120651239600SJonathan Cameron 
1207a980e046SJonathan Cameron 	if (chan->ext_info) {
1208a980e046SJonathan Cameron 		unsigned int i = 0;
1209a980e046SJonathan Cameron 		for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1210a980e046SJonathan Cameron 			ret = __iio_add_chan_devattr(ext_info->name,
1211a980e046SJonathan Cameron 					chan,
1212a980e046SJonathan Cameron 					ext_info->read ?
1213a980e046SJonathan Cameron 					    &iio_read_channel_ext_info : NULL,
1214a980e046SJonathan Cameron 					ext_info->write ?
1215a980e046SJonathan Cameron 					    &iio_write_channel_ext_info : NULL,
1216a980e046SJonathan Cameron 					i,
1217a980e046SJonathan Cameron 					ext_info->shared,
1218a980e046SJonathan Cameron 					&indio_dev->dev,
1219a980e046SJonathan Cameron 					&indio_dev->channel_attr_list);
1220a980e046SJonathan Cameron 			i++;
1221a980e046SJonathan Cameron 			if (ret == -EBUSY && ext_info->shared)
1222a980e046SJonathan Cameron 				continue;
1223a980e046SJonathan Cameron 
1224a980e046SJonathan Cameron 			if (ret)
12253704432fSJonathan Cameron 				return ret;
1226a980e046SJonathan Cameron 
1227a980e046SJonathan Cameron 			attrcount++;
1228a980e046SJonathan Cameron 		}
1229a980e046SJonathan Cameron 	}
1230a980e046SJonathan Cameron 
12313704432fSJonathan Cameron 	return attrcount;
1232a980e046SJonathan Cameron }
1233a980e046SJonathan Cameron 
123484088ebdSLars-Peter Clausen /**
123584088ebdSLars-Peter Clausen  * iio_free_chan_devattr_list() - Free a list of IIO device attributes
123684088ebdSLars-Peter Clausen  * @attr_list: List of IIO device attributes
123784088ebdSLars-Peter Clausen  *
123884088ebdSLars-Peter Clausen  * This function frees the memory allocated for each of the IIO device
1239c1b03ab5SMartin Fuzzey  * attributes in the list.
124084088ebdSLars-Peter Clausen  */
124184088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list)
1242a980e046SJonathan Cameron {
124384088ebdSLars-Peter Clausen 	struct iio_dev_attr *p, *n;
124484088ebdSLars-Peter Clausen 
124584088ebdSLars-Peter Clausen 	list_for_each_entry_safe(p, n, attr_list, l) {
1246a980e046SJonathan Cameron 		kfree(p->dev_attr.attr.name);
1247c1b03ab5SMartin Fuzzey 		list_del(&p->l);
1248a980e046SJonathan Cameron 		kfree(p);
1249a980e046SJonathan Cameron 	}
125084088ebdSLars-Peter Clausen }
1251a980e046SJonathan Cameron 
1252a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev,
1253a980e046SJonathan Cameron 				 struct device_attribute *attr,
1254a980e046SJonathan Cameron 				 char *buf)
1255a980e046SJonathan Cameron {
1256e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1257598db581SKees Cook 	return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
1258a980e046SJonathan Cameron }
1259a980e046SJonathan Cameron 
1260a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1261a980e046SJonathan Cameron 
12622c3d0c9fSPhil Reid static ssize_t iio_show_dev_label(struct device *dev,
12632c3d0c9fSPhil Reid 				 struct device_attribute *attr,
12642c3d0c9fSPhil Reid 				 char *buf)
12652c3d0c9fSPhil Reid {
12662c3d0c9fSPhil Reid 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
12672c3d0c9fSPhil Reid 	return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->label);
12682c3d0c9fSPhil Reid }
12692c3d0c9fSPhil Reid 
12702c3d0c9fSPhil Reid static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL);
12712c3d0c9fSPhil Reid 
1272bc2b7dabSGregor Boirie static ssize_t iio_show_timestamp_clock(struct device *dev,
1273bc2b7dabSGregor Boirie 					struct device_attribute *attr,
1274bc2b7dabSGregor Boirie 					char *buf)
1275bc2b7dabSGregor Boirie {
1276bc2b7dabSGregor Boirie 	const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1277bc2b7dabSGregor Boirie 	const clockid_t clk = iio_device_get_clock(indio_dev);
1278bc2b7dabSGregor Boirie 	const char *name;
1279bc2b7dabSGregor Boirie 	ssize_t sz;
1280bc2b7dabSGregor Boirie 
1281bc2b7dabSGregor Boirie 	switch (clk) {
1282bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
1283bc2b7dabSGregor Boirie 		name = "realtime\n";
1284bc2b7dabSGregor Boirie 		sz = sizeof("realtime\n");
1285bc2b7dabSGregor Boirie 		break;
1286bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
1287bc2b7dabSGregor Boirie 		name = "monotonic\n";
1288bc2b7dabSGregor Boirie 		sz = sizeof("monotonic\n");
1289bc2b7dabSGregor Boirie 		break;
1290bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
1291bc2b7dabSGregor Boirie 		name = "monotonic_raw\n";
1292bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_raw\n");
1293bc2b7dabSGregor Boirie 		break;
1294bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
1295bc2b7dabSGregor Boirie 		name = "realtime_coarse\n";
1296bc2b7dabSGregor Boirie 		sz = sizeof("realtime_coarse\n");
1297bc2b7dabSGregor Boirie 		break;
1298bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
1299bc2b7dabSGregor Boirie 		name = "monotonic_coarse\n";
1300bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_coarse\n");
1301bc2b7dabSGregor Boirie 		break;
1302bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
1303bc2b7dabSGregor Boirie 		name = "boottime\n";
1304bc2b7dabSGregor Boirie 		sz = sizeof("boottime\n");
1305bc2b7dabSGregor Boirie 		break;
1306bc2b7dabSGregor Boirie 	case CLOCK_TAI:
1307bc2b7dabSGregor Boirie 		name = "tai\n";
1308bc2b7dabSGregor Boirie 		sz = sizeof("tai\n");
1309bc2b7dabSGregor Boirie 		break;
1310bc2b7dabSGregor Boirie 	default:
1311bc2b7dabSGregor Boirie 		BUG();
1312bc2b7dabSGregor Boirie 	}
1313bc2b7dabSGregor Boirie 
1314bc2b7dabSGregor Boirie 	memcpy(buf, name, sz);
1315bc2b7dabSGregor Boirie 	return sz;
1316bc2b7dabSGregor Boirie }
1317bc2b7dabSGregor Boirie 
1318bc2b7dabSGregor Boirie static ssize_t iio_store_timestamp_clock(struct device *dev,
1319bc2b7dabSGregor Boirie 					 struct device_attribute *attr,
1320bc2b7dabSGregor Boirie 					 const char *buf, size_t len)
1321bc2b7dabSGregor Boirie {
1322bc2b7dabSGregor Boirie 	clockid_t clk;
1323bc2b7dabSGregor Boirie 	int ret;
1324bc2b7dabSGregor Boirie 
1325bc2b7dabSGregor Boirie 	if (sysfs_streq(buf, "realtime"))
1326bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME;
1327bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic"))
1328bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC;
1329bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_raw"))
1330bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_RAW;
1331bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "realtime_coarse"))
1332bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME_COARSE;
1333bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_coarse"))
1334bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_COARSE;
1335bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "boottime"))
1336bc2b7dabSGregor Boirie 		clk = CLOCK_BOOTTIME;
1337bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "tai"))
1338bc2b7dabSGregor Boirie 		clk = CLOCK_TAI;
1339bc2b7dabSGregor Boirie 	else
1340bc2b7dabSGregor Boirie 		return -EINVAL;
1341bc2b7dabSGregor Boirie 
1342bc2b7dabSGregor Boirie 	ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1343bc2b7dabSGregor Boirie 	if (ret)
1344bc2b7dabSGregor Boirie 		return ret;
1345bc2b7dabSGregor Boirie 
1346bc2b7dabSGregor Boirie 	return len;
1347bc2b7dabSGregor Boirie }
1348bc2b7dabSGregor Boirie 
1349bc2b7dabSGregor Boirie static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1350bc2b7dabSGregor Boirie 		   iio_show_timestamp_clock, iio_store_timestamp_clock);
1351bc2b7dabSGregor Boirie 
1352a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1353a980e046SJonathan Cameron {
1354a980e046SJonathan Cameron 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
135584088ebdSLars-Peter Clausen 	struct iio_dev_attr *p;
1356bc2b7dabSGregor Boirie 	struct attribute **attr, *clk = NULL;
1357a980e046SJonathan Cameron 
1358a980e046SJonathan Cameron 	/* First count elements in any existing group */
1359a980e046SJonathan Cameron 	if (indio_dev->info->attrs) {
1360a980e046SJonathan Cameron 		attr = indio_dev->info->attrs->attrs;
1361a980e046SJonathan Cameron 		while (*attr++ != NULL)
1362a980e046SJonathan Cameron 			attrcount_orig++;
1363a980e046SJonathan Cameron 	}
1364a980e046SJonathan Cameron 	attrcount = attrcount_orig;
1365a980e046SJonathan Cameron 	/*
1366a980e046SJonathan Cameron 	 * New channel registration method - relies on the fact a group does
1367d25b3808SPeter Meerwald 	 * not need to be initialized if its name is NULL.
1368a980e046SJonathan Cameron 	 */
1369a980e046SJonathan Cameron 	if (indio_dev->channels)
1370a980e046SJonathan Cameron 		for (i = 0; i < indio_dev->num_channels; i++) {
1371bc2b7dabSGregor Boirie 			const struct iio_chan_spec *chan =
1372bc2b7dabSGregor Boirie 				&indio_dev->channels[i];
1373bc2b7dabSGregor Boirie 
1374bc2b7dabSGregor Boirie 			if (chan->type == IIO_TIMESTAMP)
1375bc2b7dabSGregor Boirie 				clk = &dev_attr_current_timestamp_clock.attr;
1376bc2b7dabSGregor Boirie 
1377bc2b7dabSGregor Boirie 			ret = iio_device_add_channel_sysfs(indio_dev, chan);
1378a980e046SJonathan Cameron 			if (ret < 0)
1379a980e046SJonathan Cameron 				goto error_clear_attrs;
1380a980e046SJonathan Cameron 			attrcount += ret;
1381a980e046SJonathan Cameron 		}
1382a980e046SJonathan Cameron 
1383bc2b7dabSGregor Boirie 	if (indio_dev->event_interface)
1384bc2b7dabSGregor Boirie 		clk = &dev_attr_current_timestamp_clock.attr;
1385bc2b7dabSGregor Boirie 
1386a980e046SJonathan Cameron 	if (indio_dev->name)
1387a980e046SJonathan Cameron 		attrcount++;
13882c3d0c9fSPhil Reid 	if (indio_dev->label)
13892c3d0c9fSPhil Reid 		attrcount++;
1390bc2b7dabSGregor Boirie 	if (clk)
1391bc2b7dabSGregor Boirie 		attrcount++;
1392a980e046SJonathan Cameron 
1393a980e046SJonathan Cameron 	indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
1394a980e046SJonathan Cameron 						   sizeof(indio_dev->chan_attr_group.attrs[0]),
1395a980e046SJonathan Cameron 						   GFP_KERNEL);
1396a980e046SJonathan Cameron 	if (indio_dev->chan_attr_group.attrs == NULL) {
1397a980e046SJonathan Cameron 		ret = -ENOMEM;
1398a980e046SJonathan Cameron 		goto error_clear_attrs;
1399a980e046SJonathan Cameron 	}
1400a980e046SJonathan Cameron 	/* Copy across original attributes */
1401a980e046SJonathan Cameron 	if (indio_dev->info->attrs)
1402a980e046SJonathan Cameron 		memcpy(indio_dev->chan_attr_group.attrs,
1403a980e046SJonathan Cameron 		       indio_dev->info->attrs->attrs,
1404a980e046SJonathan Cameron 		       sizeof(indio_dev->chan_attr_group.attrs[0])
1405a980e046SJonathan Cameron 		       *attrcount_orig);
1406a980e046SJonathan Cameron 	attrn = attrcount_orig;
1407a980e046SJonathan Cameron 	/* Add all elements from the list. */
1408a980e046SJonathan Cameron 	list_for_each_entry(p, &indio_dev->channel_attr_list, l)
1409a980e046SJonathan Cameron 		indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1410a980e046SJonathan Cameron 	if (indio_dev->name)
1411a980e046SJonathan Cameron 		indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
14122c3d0c9fSPhil Reid 	if (indio_dev->label)
14132c3d0c9fSPhil Reid 		indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1414bc2b7dabSGregor Boirie 	if (clk)
1415bc2b7dabSGregor Boirie 		indio_dev->chan_attr_group.attrs[attrn++] = clk;
1416a980e046SJonathan Cameron 
1417a980e046SJonathan Cameron 	indio_dev->groups[indio_dev->groupcounter++] =
1418a980e046SJonathan Cameron 		&indio_dev->chan_attr_group;
1419a980e046SJonathan Cameron 
1420a980e046SJonathan Cameron 	return 0;
1421a980e046SJonathan Cameron 
1422a980e046SJonathan Cameron error_clear_attrs:
142384088ebdSLars-Peter Clausen 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1424a980e046SJonathan Cameron 
1425a980e046SJonathan Cameron 	return ret;
1426a980e046SJonathan Cameron }
1427a980e046SJonathan Cameron 
1428a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1429a980e046SJonathan Cameron {
1430a980e046SJonathan Cameron 
143184088ebdSLars-Peter Clausen 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1432a980e046SJonathan Cameron 	kfree(indio_dev->chan_attr_group.attrs);
1433c1b03ab5SMartin Fuzzey 	indio_dev->chan_attr_group.attrs = NULL;
1434a980e046SJonathan Cameron }
1435a980e046SJonathan Cameron 
1436a980e046SJonathan Cameron static void iio_dev_release(struct device *device)
1437a980e046SJonathan Cameron {
1438e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(device);
1439d89e119aSBenjamin Gaignard 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1440a980e046SJonathan Cameron 		iio_device_unregister_trigger_consumer(indio_dev);
1441a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1442a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
1443e407fd65SLars-Peter Clausen 
14449e69c935SLars-Peter Clausen 	iio_buffer_put(indio_dev->buffer);
14459e69c935SLars-Peter Clausen 
1446e407fd65SLars-Peter Clausen 	ida_simple_remove(&iio_ida, indio_dev->id);
1447e407fd65SLars-Peter Clausen 	kfree(indio_dev);
1448a980e046SJonathan Cameron }
1449a980e046SJonathan Cameron 
145017d82b47SGuenter Roeck struct device_type iio_device_type = {
1451a980e046SJonathan Cameron 	.name = "iio_device",
1452a980e046SJonathan Cameron 	.release = iio_dev_release,
1453a980e046SJonathan Cameron };
1454a980e046SJonathan Cameron 
1455a7e57dceSSachin Kamat /**
1456a7e57dceSSachin Kamat  * iio_device_alloc() - allocate an iio_dev from a driver
1457a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1458a7e57dceSSachin Kamat  **/
14597cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv)
1460a980e046SJonathan Cameron {
1461a980e046SJonathan Cameron 	struct iio_dev *dev;
1462a980e046SJonathan Cameron 	size_t alloc_size;
1463a980e046SJonathan Cameron 
1464a980e046SJonathan Cameron 	alloc_size = sizeof(struct iio_dev);
1465a980e046SJonathan Cameron 	if (sizeof_priv) {
1466a980e046SJonathan Cameron 		alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1467a980e046SJonathan Cameron 		alloc_size += sizeof_priv;
1468a980e046SJonathan Cameron 	}
1469a980e046SJonathan Cameron 	/* ensure 32-byte alignment of whole construct ? */
1470a980e046SJonathan Cameron 	alloc_size += IIO_ALIGN - 1;
1471a980e046SJonathan Cameron 
1472a980e046SJonathan Cameron 	dev = kzalloc(alloc_size, GFP_KERNEL);
1473a980e046SJonathan Cameron 
1474a980e046SJonathan Cameron 	if (dev) {
1475a980e046SJonathan Cameron 		dev->dev.groups = dev->groups;
147617d82b47SGuenter Roeck 		dev->dev.type = &iio_device_type;
1477a980e046SJonathan Cameron 		dev->dev.bus = &iio_bus_type;
1478a980e046SJonathan Cameron 		device_initialize(&dev->dev);
1479a980e046SJonathan Cameron 		dev_set_drvdata(&dev->dev, (void *)dev);
1480a980e046SJonathan Cameron 		mutex_init(&dev->mlock);
1481a980e046SJonathan Cameron 		mutex_init(&dev->info_exist_lock);
1482e407fd65SLars-Peter Clausen 		INIT_LIST_HEAD(&dev->channel_attr_list);
1483a980e046SJonathan Cameron 
1484a980e046SJonathan Cameron 		dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1485a980e046SJonathan Cameron 		if (dev->id < 0) {
1486a980e046SJonathan Cameron 			/* cannot use a dev_err as the name isn't available */
14873176dd5dSSachin Kamat 			pr_err("failed to get device id\n");
1488a980e046SJonathan Cameron 			kfree(dev);
1489a980e046SJonathan Cameron 			return NULL;
1490a980e046SJonathan Cameron 		}
1491a980e046SJonathan Cameron 		dev_set_name(&dev->dev, "iio:device%d", dev->id);
149284b36ce5SJonathan Cameron 		INIT_LIST_HEAD(&dev->buffer_list);
1493a980e046SJonathan Cameron 	}
1494a980e046SJonathan Cameron 
1495a980e046SJonathan Cameron 	return dev;
1496a980e046SJonathan Cameron }
14977cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc);
1498a980e046SJonathan Cameron 
1499a7e57dceSSachin Kamat /**
1500a7e57dceSSachin Kamat  * iio_device_free() - free an iio_dev from a driver
1501a7e57dceSSachin Kamat  * @dev:		the iio_dev associated with the device
1502a7e57dceSSachin Kamat  **/
15037cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev)
1504a980e046SJonathan Cameron {
1505e407fd65SLars-Peter Clausen 	if (dev)
1506e407fd65SLars-Peter Clausen 		put_device(&dev->dev);
1507a980e046SJonathan Cameron }
15087cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free);
1509a980e046SJonathan Cameron 
15109dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res)
15119dabaf5eSGrygorii Strashko {
15129dabaf5eSGrygorii Strashko 	iio_device_free(*(struct iio_dev **)res);
15139dabaf5eSGrygorii Strashko }
15149dabaf5eSGrygorii Strashko 
151570e48348SGregor Boirie int devm_iio_device_match(struct device *dev, void *res, void *data)
15169dabaf5eSGrygorii Strashko {
15179dabaf5eSGrygorii Strashko 	struct iio_dev **r = res;
15189dabaf5eSGrygorii Strashko 	if (!r || !*r) {
15199dabaf5eSGrygorii Strashko 		WARN_ON(!r || !*r);
15209dabaf5eSGrygorii Strashko 		return 0;
15219dabaf5eSGrygorii Strashko 	}
15229dabaf5eSGrygorii Strashko 	return *r == data;
15239dabaf5eSGrygorii Strashko }
152470e48348SGregor Boirie EXPORT_SYMBOL_GPL(devm_iio_device_match);
15259dabaf5eSGrygorii Strashko 
1526a7e57dceSSachin Kamat /**
1527a7e57dceSSachin Kamat  * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1528a7e57dceSSachin Kamat  * @dev:		Device to allocate iio_dev for
1529a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1530a7e57dceSSachin Kamat  *
1531a7e57dceSSachin Kamat  * Managed iio_device_alloc. iio_dev allocated with this function is
1532a7e57dceSSachin Kamat  * automatically freed on driver detach.
1533a7e57dceSSachin Kamat  *
1534a7e57dceSSachin Kamat  * If an iio_dev allocated with this function needs to be freed separately,
1535a7e57dceSSachin Kamat  * devm_iio_device_free() must be used.
1536a7e57dceSSachin Kamat  *
1537a7e57dceSSachin Kamat  * RETURNS:
1538a7e57dceSSachin Kamat  * Pointer to allocated iio_dev on success, NULL on failure.
1539a7e57dceSSachin Kamat  */
15409dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
15419dabaf5eSGrygorii Strashko {
15429dabaf5eSGrygorii Strashko 	struct iio_dev **ptr, *iio_dev;
15439dabaf5eSGrygorii Strashko 
15449dabaf5eSGrygorii Strashko 	ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
15459dabaf5eSGrygorii Strashko 			   GFP_KERNEL);
15469dabaf5eSGrygorii Strashko 	if (!ptr)
15479dabaf5eSGrygorii Strashko 		return NULL;
15489dabaf5eSGrygorii Strashko 
15499dabaf5eSGrygorii Strashko 	iio_dev = iio_device_alloc(sizeof_priv);
15509dabaf5eSGrygorii Strashko 	if (iio_dev) {
15519dabaf5eSGrygorii Strashko 		*ptr = iio_dev;
15529dabaf5eSGrygorii Strashko 		devres_add(dev, ptr);
15539dabaf5eSGrygorii Strashko 	} else {
15549dabaf5eSGrygorii Strashko 		devres_free(ptr);
15559dabaf5eSGrygorii Strashko 	}
15569dabaf5eSGrygorii Strashko 
15579dabaf5eSGrygorii Strashko 	return iio_dev;
15589dabaf5eSGrygorii Strashko }
15599dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
15609dabaf5eSGrygorii Strashko 
1561a7e57dceSSachin Kamat /**
1562a7e57dceSSachin Kamat  * devm_iio_device_free - Resource-managed iio_device_free()
1563a7e57dceSSachin Kamat  * @dev:		Device this iio_dev belongs to
1564a7e57dceSSachin Kamat  * @iio_dev:		the iio_dev associated with the device
1565a7e57dceSSachin Kamat  *
1566a7e57dceSSachin Kamat  * Free iio_dev allocated with devm_iio_device_alloc().
1567a7e57dceSSachin Kamat  */
15689dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
15699dabaf5eSGrygorii Strashko {
15709dabaf5eSGrygorii Strashko 	int rc;
15719dabaf5eSGrygorii Strashko 
15729dabaf5eSGrygorii Strashko 	rc = devres_release(dev, devm_iio_device_release,
15739dabaf5eSGrygorii Strashko 			    devm_iio_device_match, iio_dev);
15749dabaf5eSGrygorii Strashko 	WARN_ON(rc);
15759dabaf5eSGrygorii Strashko }
15769dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free);
15779dabaf5eSGrygorii Strashko 
1578a980e046SJonathan Cameron /**
1579a980e046SJonathan Cameron  * iio_chrdev_open() - chrdev file open for buffer access and ioctls
15802498dcf6SCristina Opriceana  * @inode:	Inode structure for identifying the device in the file system
15812498dcf6SCristina Opriceana  * @filp:	File structure for iio device used to keep and later access
15822498dcf6SCristina Opriceana  *		private data
15832498dcf6SCristina Opriceana  *
15842498dcf6SCristina Opriceana  * Return: 0 on success or -EBUSY if the device is already opened
1585a980e046SJonathan Cameron  **/
1586a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp)
1587a980e046SJonathan Cameron {
1588a980e046SJonathan Cameron 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1589a980e046SJonathan Cameron 						struct iio_dev, chrdev);
1590a980e046SJonathan Cameron 
1591a980e046SJonathan Cameron 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1592a980e046SJonathan Cameron 		return -EBUSY;
1593a980e046SJonathan Cameron 
1594cadc2125SLars-Peter Clausen 	iio_device_get(indio_dev);
1595cadc2125SLars-Peter Clausen 
1596a980e046SJonathan Cameron 	filp->private_data = indio_dev;
1597a980e046SJonathan Cameron 
1598a980e046SJonathan Cameron 	return 0;
1599a980e046SJonathan Cameron }
1600a980e046SJonathan Cameron 
1601a980e046SJonathan Cameron /**
1602a980e046SJonathan Cameron  * iio_chrdev_release() - chrdev file close buffer access and ioctls
16032498dcf6SCristina Opriceana  * @inode:	Inode structure pointer for the char device
16042498dcf6SCristina Opriceana  * @filp:	File structure pointer for the char device
16052498dcf6SCristina Opriceana  *
16062498dcf6SCristina Opriceana  * Return: 0 for successful release
16072498dcf6SCristina Opriceana  */
1608a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp)
1609a980e046SJonathan Cameron {
1610a980e046SJonathan Cameron 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1611a980e046SJonathan Cameron 						struct iio_dev, chrdev);
1612a980e046SJonathan Cameron 	clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1613cadc2125SLars-Peter Clausen 	iio_device_put(indio_dev);
1614cadc2125SLars-Peter Clausen 
1615a980e046SJonathan Cameron 	return 0;
1616a980e046SJonathan Cameron }
1617a980e046SJonathan Cameron 
1618a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually
1619a980e046SJonathan Cameron  * event related */
1620a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1621a980e046SJonathan Cameron {
1622a980e046SJonathan Cameron 	struct iio_dev *indio_dev = filp->private_data;
1623a980e046SJonathan Cameron 	int __user *ip = (int __user *)arg;
1624a980e046SJonathan Cameron 	int fd;
1625a980e046SJonathan Cameron 
1626f18e7a06SLars-Peter Clausen 	if (!indio_dev->info)
1627f18e7a06SLars-Peter Clausen 		return -ENODEV;
1628f18e7a06SLars-Peter Clausen 
1629a980e046SJonathan Cameron 	if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1630a980e046SJonathan Cameron 		fd = iio_event_getfd(indio_dev);
16313f9059b7SLinus Walleij 		if (fd < 0)
16323f9059b7SLinus Walleij 			return fd;
1633a980e046SJonathan Cameron 		if (copy_to_user(ip, &fd, sizeof(fd)))
1634a980e046SJonathan Cameron 			return -EFAULT;
1635a980e046SJonathan Cameron 		return 0;
1636a980e046SJonathan Cameron 	}
1637a980e046SJonathan Cameron 	return -EINVAL;
1638a980e046SJonathan Cameron }
1639a980e046SJonathan Cameron 
1640a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = {
1641f6d4033dSLars-Peter Clausen 	.read = iio_buffer_read_outer_addr,
1642a980e046SJonathan Cameron 	.release = iio_chrdev_release,
1643a980e046SJonathan Cameron 	.open = iio_chrdev_open,
1644a980e046SJonathan Cameron 	.poll = iio_buffer_poll_addr,
1645a980e046SJonathan Cameron 	.owner = THIS_MODULE,
1646a980e046SJonathan Cameron 	.llseek = noop_llseek,
1647a980e046SJonathan Cameron 	.unlocked_ioctl = iio_ioctl,
16481832f2d8SArnd Bergmann 	.compat_ioctl = compat_ptr_ioctl,
1649a980e046SJonathan Cameron };
1650a980e046SJonathan Cameron 
16518f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
16528f5d8727SVlad Dogaru {
16538f5d8727SVlad Dogaru 	int i, j;
16548f5d8727SVlad Dogaru 	const struct iio_chan_spec *channels = indio_dev->channels;
16558f5d8727SVlad Dogaru 
16568f5d8727SVlad Dogaru 	if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
16578f5d8727SVlad Dogaru 		return 0;
16588f5d8727SVlad Dogaru 
16598f5d8727SVlad Dogaru 	for (i = 0; i < indio_dev->num_channels - 1; i++) {
16608f5d8727SVlad Dogaru 		if (channels[i].scan_index < 0)
16618f5d8727SVlad Dogaru 			continue;
16628f5d8727SVlad Dogaru 		for (j = i + 1; j < indio_dev->num_channels; j++)
16638f5d8727SVlad Dogaru 			if (channels[i].scan_index == channels[j].scan_index) {
16648f5d8727SVlad Dogaru 				dev_err(&indio_dev->dev,
16658f5d8727SVlad Dogaru 					"Duplicate scan index %d\n",
16668f5d8727SVlad Dogaru 					channels[i].scan_index);
16678f5d8727SVlad Dogaru 				return -EINVAL;
16688f5d8727SVlad Dogaru 			}
16698f5d8727SVlad Dogaru 	}
16708f5d8727SVlad Dogaru 
16718f5d8727SVlad Dogaru 	return 0;
16728f5d8727SVlad Dogaru }
16738f5d8727SVlad Dogaru 
1674a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1675a980e046SJonathan Cameron 
167663b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1677a980e046SJonathan Cameron {
1678a980e046SJonathan Cameron 	int ret;
1679a980e046SJonathan Cameron 
168063b19547SJonathan Cameron 	indio_dev->driver_module = this_mod;
168117d82b47SGuenter Roeck 	/* If the calling driver did not initialize of_node, do it here */
168217d82b47SGuenter Roeck 	if (!indio_dev->dev.of_node && indio_dev->dev.parent)
168317d82b47SGuenter Roeck 		indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
168417d82b47SGuenter Roeck 
16852c3d0c9fSPhil Reid 	indio_dev->label = of_get_property(indio_dev->dev.of_node, "label",
16862c3d0c9fSPhil Reid 					   NULL);
16872c3d0c9fSPhil Reid 
16888f5d8727SVlad Dogaru 	ret = iio_check_unique_scan_index(indio_dev);
16898f5d8727SVlad Dogaru 	if (ret < 0)
16908f5d8727SVlad Dogaru 		return ret;
16918f5d8727SVlad Dogaru 
1692d45ebf1fSAndi Shyti 	if (!indio_dev->info)
1693d45ebf1fSAndi Shyti 		return -EINVAL;
1694d45ebf1fSAndi Shyti 
1695a980e046SJonathan Cameron 	/* configure elements for the chrdev */
1696a980e046SJonathan Cameron 	indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1697a980e046SJonathan Cameron 
16988915aacaSGreg Kroah-Hartman 	iio_device_register_debugfs(indio_dev);
16993e1b6c95SLars-Peter Clausen 
17003e1b6c95SLars-Peter Clausen 	ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
17013e1b6c95SLars-Peter Clausen 	if (ret) {
17023e1b6c95SLars-Peter Clausen 		dev_err(indio_dev->dev.parent,
17033e1b6c95SLars-Peter Clausen 			"Failed to create buffer sysfs interfaces\n");
17043e1b6c95SLars-Peter Clausen 		goto error_unreg_debugfs;
17053e1b6c95SLars-Peter Clausen 	}
17063e1b6c95SLars-Peter Clausen 
1707a980e046SJonathan Cameron 	ret = iio_device_register_sysfs(indio_dev);
1708a980e046SJonathan Cameron 	if (ret) {
1709a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1710a980e046SJonathan Cameron 			"Failed to register sysfs interfaces\n");
17113e1b6c95SLars-Peter Clausen 		goto error_buffer_free_sysfs;
1712a980e046SJonathan Cameron 	}
1713a980e046SJonathan Cameron 	ret = iio_device_register_eventset(indio_dev);
1714a980e046SJonathan Cameron 	if (ret) {
1715a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1716a980e046SJonathan Cameron 			"Failed to register event set\n");
1717a980e046SJonathan Cameron 		goto error_free_sysfs;
1718a980e046SJonathan Cameron 	}
1719d89e119aSBenjamin Gaignard 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1720a980e046SJonathan Cameron 		iio_device_register_trigger_consumer(indio_dev);
1721a980e046SJonathan Cameron 
1722a980e046SJonathan Cameron 	if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1723a980e046SJonathan Cameron 		indio_dev->setup_ops == NULL)
1724a980e046SJonathan Cameron 		indio_dev->setup_ops = &noop_ring_setup_ops;
1725a980e046SJonathan Cameron 
1726a980e046SJonathan Cameron 	cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
172763b19547SJonathan Cameron 
172863b19547SJonathan Cameron 	indio_dev->chrdev.owner = this_mod;
172938923911SLogan Gunthorpe 
173038923911SLogan Gunthorpe 	ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev);
1731a980e046SJonathan Cameron 	if (ret < 0)
17320d5b7daeSLars-Peter Clausen 		goto error_unreg_eventset;
1733a980e046SJonathan Cameron 
17340d5b7daeSLars-Peter Clausen 	return 0;
173538923911SLogan Gunthorpe 
1736a980e046SJonathan Cameron error_unreg_eventset:
1737a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1738a980e046SJonathan Cameron error_free_sysfs:
1739a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
17403e1b6c95SLars-Peter Clausen error_buffer_free_sysfs:
17413e1b6c95SLars-Peter Clausen 	iio_buffer_free_sysfs_and_mask(indio_dev);
1742a980e046SJonathan Cameron error_unreg_debugfs:
1743a980e046SJonathan Cameron 	iio_device_unregister_debugfs(indio_dev);
1744a980e046SJonathan Cameron 	return ret;
1745a980e046SJonathan Cameron }
174663b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register);
1747a980e046SJonathan Cameron 
1748a7e57dceSSachin Kamat /**
1749a7e57dceSSachin Kamat  * iio_device_unregister() - unregister a device from the IIO subsystem
1750a7e57dceSSachin Kamat  * @indio_dev:		Device structure representing the device.
1751a7e57dceSSachin Kamat  **/
1752a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev)
1753a980e046SJonathan Cameron {
175438923911SLogan Gunthorpe 	cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
1755a87c82e4SLars-Peter Clausen 
17567f75591fSFabrice Gasnier 	mutex_lock(&indio_dev->info_exist_lock);
17577f75591fSFabrice Gasnier 
1758bc4c9612SLars-Peter Clausen 	iio_device_unregister_debugfs(indio_dev);
17590d5b7daeSLars-Peter Clausen 
1760a87c82e4SLars-Peter Clausen 	iio_disable_all_buffers(indio_dev);
1761a87c82e4SLars-Peter Clausen 
1762a980e046SJonathan Cameron 	indio_dev->info = NULL;
1763d2f0a48fSLars-Peter Clausen 
1764d2f0a48fSLars-Peter Clausen 	iio_device_wakeup_eventset(indio_dev);
1765d2f0a48fSLars-Peter Clausen 	iio_buffer_wakeup_poll(indio_dev);
1766d2f0a48fSLars-Peter Clausen 
1767a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->info_exist_lock);
17683e1b6c95SLars-Peter Clausen 
17693e1b6c95SLars-Peter Clausen 	iio_buffer_free_sysfs_and_mask(indio_dev);
1770a980e046SJonathan Cameron }
1771a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister);
17728caa07c0SSachin Kamat 
17738caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res)
17748caa07c0SSachin Kamat {
17758caa07c0SSachin Kamat 	iio_device_unregister(*(struct iio_dev **)res);
17768caa07c0SSachin Kamat }
17778caa07c0SSachin Kamat 
177863b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
177963b19547SJonathan Cameron 			       struct module *this_mod)
17808caa07c0SSachin Kamat {
17818caa07c0SSachin Kamat 	struct iio_dev **ptr;
17828caa07c0SSachin Kamat 	int ret;
17838caa07c0SSachin Kamat 
17848caa07c0SSachin Kamat 	ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
17858caa07c0SSachin Kamat 	if (!ptr)
17868caa07c0SSachin Kamat 		return -ENOMEM;
17878caa07c0SSachin Kamat 
17888caa07c0SSachin Kamat 	*ptr = indio_dev;
178963b19547SJonathan Cameron 	ret = __iio_device_register(indio_dev, this_mod);
17908caa07c0SSachin Kamat 	if (!ret)
17918caa07c0SSachin Kamat 		devres_add(dev, ptr);
17928caa07c0SSachin Kamat 	else
17938caa07c0SSachin Kamat 		devres_free(ptr);
17948caa07c0SSachin Kamat 
17958caa07c0SSachin Kamat 	return ret;
17968caa07c0SSachin Kamat }
179763b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register);
17988caa07c0SSachin Kamat 
17998caa07c0SSachin Kamat /**
18008caa07c0SSachin Kamat  * devm_iio_device_unregister - Resource-managed iio_device_unregister()
18018caa07c0SSachin Kamat  * @dev:	Device this iio_dev belongs to
18028caa07c0SSachin Kamat  * @indio_dev:	the iio_dev associated with the device
18038caa07c0SSachin Kamat  *
18048caa07c0SSachin Kamat  * Unregister iio_dev registered with devm_iio_device_register().
18058caa07c0SSachin Kamat  */
18068caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
18078caa07c0SSachin Kamat {
18088caa07c0SSachin Kamat 	int rc;
18098caa07c0SSachin Kamat 
18108caa07c0SSachin Kamat 	rc = devres_release(dev, devm_iio_device_unreg,
18118caa07c0SSachin Kamat 			    devm_iio_device_match, indio_dev);
18128caa07c0SSachin Kamat 	WARN_ON(rc);
18138caa07c0SSachin Kamat }
18148caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
18158caa07c0SSachin Kamat 
181608a33805SAlison Schofield /**
181708a33805SAlison Schofield  * iio_device_claim_direct_mode - Keep device in direct mode
181808a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
181908a33805SAlison Schofield  *
182008a33805SAlison Schofield  * If the device is in direct mode it is guaranteed to stay
182108a33805SAlison Schofield  * that way until iio_device_release_direct_mode() is called.
182208a33805SAlison Schofield  *
182308a33805SAlison Schofield  * Use with iio_device_release_direct_mode()
182408a33805SAlison Schofield  *
182508a33805SAlison Schofield  * Returns: 0 on success, -EBUSY on failure
182608a33805SAlison Schofield  */
182708a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
182808a33805SAlison Schofield {
182908a33805SAlison Schofield 	mutex_lock(&indio_dev->mlock);
183008a33805SAlison Schofield 
183108a33805SAlison Schofield 	if (iio_buffer_enabled(indio_dev)) {
183208a33805SAlison Schofield 		mutex_unlock(&indio_dev->mlock);
183308a33805SAlison Schofield 		return -EBUSY;
183408a33805SAlison Schofield 	}
183508a33805SAlison Schofield 	return 0;
183608a33805SAlison Schofield }
183708a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
183808a33805SAlison Schofield 
183908a33805SAlison Schofield /**
184008a33805SAlison Schofield  * iio_device_release_direct_mode - releases claim on direct mode
184108a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
184208a33805SAlison Schofield  *
184308a33805SAlison Schofield  * Release the claim. Device is no longer guaranteed to stay
184408a33805SAlison Schofield  * in direct mode.
184508a33805SAlison Schofield  *
184608a33805SAlison Schofield  * Use with iio_device_claim_direct_mode()
184708a33805SAlison Schofield  */
184808a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev)
184908a33805SAlison Schofield {
185008a33805SAlison Schofield 	mutex_unlock(&indio_dev->mlock);
185108a33805SAlison Schofield }
185208a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
185308a33805SAlison Schofield 
1854a980e046SJonathan Cameron subsys_initcall(iio_init);
1855a980e046SJonathan Cameron module_exit(iio_exit);
1856a980e046SJonathan Cameron 
1857c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1858a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core");
1859a980e046SJonathan Cameron MODULE_LICENSE("GPL");
1860