xref: /linux/drivers/iio/industrialio-core.c (revision 8915aacac4dd5326a07f6b9c01f266289c57c71e)
1a980e046SJonathan Cameron /* The industrial I/O core
2a980e046SJonathan Cameron  *
3a980e046SJonathan Cameron  * Copyright (c) 2008 Jonathan Cameron
4a980e046SJonathan Cameron  *
5a980e046SJonathan Cameron  * This program is free software; you can redistribute it and/or modify it
6a980e046SJonathan Cameron  * under the terms of the GNU General Public License version 2 as published by
7a980e046SJonathan Cameron  * the Free Software Foundation.
8a980e046SJonathan Cameron  *
9a980e046SJonathan Cameron  * Based on elements of hwmon and input subsystems.
10a980e046SJonathan Cameron  */
11a980e046SJonathan Cameron 
123176dd5dSSachin Kamat #define pr_fmt(fmt) "iio-core: " fmt
133176dd5dSSachin Kamat 
14a980e046SJonathan Cameron #include <linux/kernel.h>
15a980e046SJonathan Cameron #include <linux/module.h>
16a980e046SJonathan Cameron #include <linux/idr.h>
17a980e046SJonathan Cameron #include <linux/kdev_t.h>
18a980e046SJonathan Cameron #include <linux/err.h>
19a980e046SJonathan Cameron #include <linux/device.h>
20a980e046SJonathan Cameron #include <linux/fs.h>
21a980e046SJonathan Cameron #include <linux/poll.h>
22fb158971SAndy Shevchenko #include <linux/property.h>
23a980e046SJonathan Cameron #include <linux/sched.h>
24a980e046SJonathan Cameron #include <linux/wait.h>
25a980e046SJonathan Cameron #include <linux/cdev.h>
26a980e046SJonathan Cameron #include <linux/slab.h>
27a980e046SJonathan Cameron #include <linux/anon_inodes.h>
28a980e046SJonathan Cameron #include <linux/debugfs.h>
2908a33805SAlison Schofield #include <linux/mutex.h>
30a980e046SJonathan Cameron #include <linux/iio/iio.h>
31a980e046SJonathan Cameron #include "iio_core.h"
32a980e046SJonathan Cameron #include "iio_core_trigger.h"
33a980e046SJonathan Cameron #include <linux/iio/sysfs.h>
34a980e046SJonathan Cameron #include <linux/iio/events.h>
359e69c935SLars-Peter Clausen #include <linux/iio/buffer.h>
3633dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h>
37a980e046SJonathan Cameron 
38a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */
39a980e046SJonathan Cameron static DEFINE_IDA(iio_ida);
40a980e046SJonathan Cameron 
41a980e046SJonathan Cameron static dev_t iio_devt;
42a980e046SJonathan Cameron 
43a980e046SJonathan Cameron #define IIO_DEV_MAX 256
44a980e046SJonathan Cameron struct bus_type iio_bus_type = {
45a980e046SJonathan Cameron 	.name = "iio",
46a980e046SJonathan Cameron };
47a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type);
48a980e046SJonathan Cameron 
49a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry;
50a980e046SJonathan Cameron 
51a980e046SJonathan Cameron static const char * const iio_direction[] = {
52a980e046SJonathan Cameron 	[0] = "in",
53a980e046SJonathan Cameron 	[1] = "out",
54a980e046SJonathan Cameron };
55a980e046SJonathan Cameron 
56a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = {
57a980e046SJonathan Cameron 	[IIO_VOLTAGE] = "voltage",
58a980e046SJonathan Cameron 	[IIO_CURRENT] = "current",
59a980e046SJonathan Cameron 	[IIO_POWER] = "power",
60a980e046SJonathan Cameron 	[IIO_ACCEL] = "accel",
61a980e046SJonathan Cameron 	[IIO_ANGL_VEL] = "anglvel",
62a980e046SJonathan Cameron 	[IIO_MAGN] = "magn",
63a980e046SJonathan Cameron 	[IIO_LIGHT] = "illuminance",
64a980e046SJonathan Cameron 	[IIO_INTENSITY] = "intensity",
65a980e046SJonathan Cameron 	[IIO_PROXIMITY] = "proximity",
66a980e046SJonathan Cameron 	[IIO_TEMP] = "temp",
67a980e046SJonathan Cameron 	[IIO_INCLI] = "incli",
68a980e046SJonathan Cameron 	[IIO_ROT] = "rot",
69a980e046SJonathan Cameron 	[IIO_ANGL] = "angl",
70a980e046SJonathan Cameron 	[IIO_TIMESTAMP] = "timestamp",
71a980e046SJonathan Cameron 	[IIO_CAPACITANCE] = "capacitance",
72a6b12855SMichael Hennerich 	[IIO_ALTVOLTAGE] = "altvoltage",
7321cd1fabSJon Brenner 	[IIO_CCT] = "cct",
74c4f0c693SLars-Peter Clausen 	[IIO_PRESSURE] = "pressure",
75ac216aa2SHarald Geyer 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
7655aebeb9SDaniel Baluta 	[IIO_ACTIVITY] = "activity",
77a88bfe78SIrina Tirdea 	[IIO_STEPS] = "steps",
7872c66644SIrina Tirdea 	[IIO_ENERGY] = "energy",
79cc3c9eecSIrina Tirdea 	[IIO_DISTANCE] = "distance",
805a1a9329SIrina Tirdea 	[IIO_VELOCITY] = "velocity",
818ff6b3bcSMatt Ranostay 	[IIO_CONCENTRATION] = "concentration",
82d38d5469SMatt Ranostay 	[IIO_RESISTANCE] = "resistance",
83ecb3a7ccSMatt Ranostay 	[IIO_PH] = "ph",
84d409404cSPeter Meerwald-Stadler 	[IIO_UVINDEX] = "uvindex",
854b9d2090SMatt Ranostay 	[IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
861a8f324aSWilliam Breathitt Gray 	[IIO_COUNT] = "count",
871a8f324aSWilliam Breathitt Gray 	[IIO_INDEX] = "index",
88571299d0SSong Hongyan 	[IIO_GRAVITY]  = "gravity",
893055a6cfSEugen Hristev 	[IIO_POSITIONRELATIVE]  = "positionrelative",
90c73314e6SMathieu Othacehe 	[IIO_PHASE] = "phase",
9117abc9ecSTomasz Duszynski 	[IIO_MASSCONCENTRATION] = "massconcentration",
92a980e046SJonathan Cameron };
93a980e046SJonathan Cameron 
94a980e046SJonathan Cameron static const char * const iio_modifier_names[] = {
95a980e046SJonathan Cameron 	[IIO_MOD_X] = "x",
96a980e046SJonathan Cameron 	[IIO_MOD_Y] = "y",
97a980e046SJonathan Cameron 	[IIO_MOD_Z] = "z",
984b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y] = "x&y",
994b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Z] = "x&z",
1004b8d8015SPeter Meerwald 	[IIO_MOD_Y_AND_Z] = "y&z",
1014b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
1024b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y] = "x|y",
1034b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Z] = "x|z",
1044b8d8015SPeter Meerwald 	[IIO_MOD_Y_OR_Z] = "y|z",
1054b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
1068f5879b2SJonathan Cameron 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
107cf82cb81SJonathan Cameron 	[IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
108a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_BOTH] = "both",
109a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_IR] = "ir",
11021cd1fabSJon Brenner 	[IIO_MOD_LIGHT_CLEAR] = "clear",
11121cd1fabSJon Brenner 	[IIO_MOD_LIGHT_RED] = "red",
11221cd1fabSJon Brenner 	[IIO_MOD_LIGHT_GREEN] = "green",
11321cd1fabSJon Brenner 	[IIO_MOD_LIGHT_BLUE] = "blue",
1142c5ff1f9SPeter Meerwald-Stadler 	[IIO_MOD_LIGHT_UV] = "uv",
115c0e4e0fdSMaxime Roussin-Bélanger 	[IIO_MOD_LIGHT_DUV] = "duv",
1165082f405SSrinivas Pandruvada 	[IIO_MOD_QUATERNION] = "quaternion",
117638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_AMBIENT] = "ambient",
118638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_OBJECT] = "object",
11911b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
12011b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE] = "from_north_true",
12111b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
12211b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
12355aebeb9SDaniel Baluta 	[IIO_MOD_RUNNING] = "running",
12455aebeb9SDaniel Baluta 	[IIO_MOD_JOGGING] = "jogging",
12555aebeb9SDaniel Baluta 	[IIO_MOD_WALKING] = "walking",
12655aebeb9SDaniel Baluta 	[IIO_MOD_STILL] = "still",
1275a1a9329SIrina Tirdea 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
1281ce87f21SLars-Peter Clausen 	[IIO_MOD_I] = "i",
1291ce87f21SLars-Peter Clausen 	[IIO_MOD_Q] = "q",
1308ff6b3bcSMatt Ranostay 	[IIO_MOD_CO2] = "co2",
1318ff6b3bcSMatt Ranostay 	[IIO_MOD_VOC] = "voc",
13217abc9ecSTomasz Duszynski 	[IIO_MOD_PM1] = "pm1",
13317abc9ecSTomasz Duszynski 	[IIO_MOD_PM2P5] = "pm2p5",
13417abc9ecSTomasz Duszynski 	[IIO_MOD_PM4] = "pm4",
13517abc9ecSTomasz Duszynski 	[IIO_MOD_PM10] = "pm10",
136a980e046SJonathan Cameron };
137a980e046SJonathan Cameron 
138a980e046SJonathan Cameron /* relies on pairs of these shared then separate */
139a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = {
140a980e046SJonathan Cameron 	[IIO_CHAN_INFO_RAW] = "raw",
141a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PROCESSED] = "input",
142a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SCALE] = "scale",
143a980e046SJonathan Cameron 	[IIO_CHAN_INFO_OFFSET] = "offset",
144a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
145a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
146a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK] = "peak_raw",
147a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
148a980e046SJonathan Cameron 	[IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
149a980e046SJonathan Cameron 	[IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
150a980e046SJonathan Cameron 	[IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
151a980e046SJonathan Cameron 	= "filter_low_pass_3db_frequency",
1523f7f642bSMartin Fuzzey 	[IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
1533f7f642bSMartin Fuzzey 	= "filter_high_pass_3db_frequency",
154a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
155a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_FREQUENCY] = "frequency",
156a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_PHASE] = "phase",
157b65d6212SMichael Hennerich 	[IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
1587c9ab035Ssrinivas pandruvada 	[IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
159899d90bdSPeter Meerwald 	[IIO_CHAN_INFO_INT_TIME] = "integration_time",
160a88bfe78SIrina Tirdea 	[IIO_CHAN_INFO_ENABLE] = "en",
161bcdf28fbSIrina Tirdea 	[IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
162d37f6836SIrina Tirdea 	[IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
1632f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
1642f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
165c8a85854SVianney le Clément de Saint-Marcq 	[IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
166faaa4495SIrina Tirdea 	[IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
167a980e046SJonathan Cameron };
168a980e046SJonathan Cameron 
169a7e57dceSSachin Kamat /**
170a7e57dceSSachin Kamat  * iio_find_channel_from_si() - get channel from its scan index
171a7e57dceSSachin Kamat  * @indio_dev:		device
172a7e57dceSSachin Kamat  * @si:			scan index to match
173a7e57dceSSachin Kamat  */
174a980e046SJonathan Cameron const struct iio_chan_spec
175a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
176a980e046SJonathan Cameron {
177a980e046SJonathan Cameron 	int i;
178a980e046SJonathan Cameron 
179a980e046SJonathan Cameron 	for (i = 0; i < indio_dev->num_channels; i++)
180a980e046SJonathan Cameron 		if (indio_dev->channels[i].scan_index == si)
181a980e046SJonathan Cameron 			return &indio_dev->channels[i];
182a980e046SJonathan Cameron 	return NULL;
183a980e046SJonathan Cameron }
184a980e046SJonathan Cameron 
185a980e046SJonathan Cameron /* This turns up an awful lot */
186a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev,
187a980e046SJonathan Cameron 			    struct device_attribute *attr,
188a980e046SJonathan Cameron 			    char *buf)
189a980e046SJonathan Cameron {
190a980e046SJonathan Cameron 	return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
191a980e046SJonathan Cameron }
192a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr);
193a980e046SJonathan Cameron 
194bc2b7dabSGregor Boirie static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
195bc2b7dabSGregor Boirie {
196bc2b7dabSGregor Boirie 	int ret;
197bc2b7dabSGregor Boirie 	const struct iio_event_interface *ev_int = indio_dev->event_interface;
198bc2b7dabSGregor Boirie 
199bc2b7dabSGregor Boirie 	ret = mutex_lock_interruptible(&indio_dev->mlock);
200bc2b7dabSGregor Boirie 	if (ret)
201bc2b7dabSGregor Boirie 		return ret;
202bc2b7dabSGregor Boirie 	if ((ev_int && iio_event_enabled(ev_int)) ||
203bc2b7dabSGregor Boirie 	    iio_buffer_enabled(indio_dev)) {
204bc2b7dabSGregor Boirie 		mutex_unlock(&indio_dev->mlock);
205bc2b7dabSGregor Boirie 		return -EBUSY;
206bc2b7dabSGregor Boirie 	}
207bc2b7dabSGregor Boirie 	indio_dev->clock_id = clock_id;
208bc2b7dabSGregor Boirie 	mutex_unlock(&indio_dev->mlock);
209bc2b7dabSGregor Boirie 
210bc2b7dabSGregor Boirie 	return 0;
211bc2b7dabSGregor Boirie }
212bc2b7dabSGregor Boirie 
213bc2b7dabSGregor Boirie /**
214bc2b7dabSGregor Boirie  * iio_get_time_ns() - utility function to get a time stamp for events etc
215bc2b7dabSGregor Boirie  * @indio_dev: device
216bc2b7dabSGregor Boirie  */
217bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev)
218bc2b7dabSGregor Boirie {
21945e7f5d2SArnd Bergmann 	struct timespec64 tp;
220bc2b7dabSGregor Boirie 
221bc2b7dabSGregor Boirie 	switch (iio_device_get_clock(indio_dev)) {
222bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
22345e7f5d2SArnd Bergmann 		return ktime_get_real_ns();
224bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
22545e7f5d2SArnd Bergmann 		return ktime_get_ns();
226bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
22745e7f5d2SArnd Bergmann 		return ktime_get_raw_ns();
228bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
22945e7f5d2SArnd Bergmann 		return ktime_to_ns(ktime_get_coarse_real());
230bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
23145e7f5d2SArnd Bergmann 		ktime_get_coarse_ts64(&tp);
23245e7f5d2SArnd Bergmann 		return timespec64_to_ns(&tp);
233bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
23445e7f5d2SArnd Bergmann 		return ktime_get_boot_ns();
235bc2b7dabSGregor Boirie 	case CLOCK_TAI:
23645e7f5d2SArnd Bergmann 		return ktime_get_tai_ns();
237bc2b7dabSGregor Boirie 	default:
238bc2b7dabSGregor Boirie 		BUG();
239bc2b7dabSGregor Boirie 	}
240bc2b7dabSGregor Boirie }
241bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns);
242bc2b7dabSGregor Boirie 
243bc2b7dabSGregor Boirie /**
244bc2b7dabSGregor Boirie  * iio_get_time_res() - utility function to get time stamp clock resolution in
245bc2b7dabSGregor Boirie  *                      nano seconds.
246bc2b7dabSGregor Boirie  * @indio_dev: device
247bc2b7dabSGregor Boirie  */
248bc2b7dabSGregor Boirie unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
249bc2b7dabSGregor Boirie {
250bc2b7dabSGregor Boirie 	switch (iio_device_get_clock(indio_dev)) {
251bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
252bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
253bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
254bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
255bc2b7dabSGregor Boirie 	case CLOCK_TAI:
256bc2b7dabSGregor Boirie 		return hrtimer_resolution;
257bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
258bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
259bc2b7dabSGregor Boirie 		return LOW_RES_NSEC;
260bc2b7dabSGregor Boirie 	default:
261bc2b7dabSGregor Boirie 		BUG();
262bc2b7dabSGregor Boirie 	}
263bc2b7dabSGregor Boirie }
264bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_res);
265bc2b7dabSGregor Boirie 
266a980e046SJonathan Cameron static int __init iio_init(void)
267a980e046SJonathan Cameron {
268a980e046SJonathan Cameron 	int ret;
269a980e046SJonathan Cameron 
270a980e046SJonathan Cameron 	/* Register sysfs bus */
271a980e046SJonathan Cameron 	ret  = bus_register(&iio_bus_type);
272a980e046SJonathan Cameron 	if (ret < 0) {
2733176dd5dSSachin Kamat 		pr_err("could not register bus type\n");
274a980e046SJonathan Cameron 		goto error_nothing;
275a980e046SJonathan Cameron 	}
276a980e046SJonathan Cameron 
277a980e046SJonathan Cameron 	ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
278a980e046SJonathan Cameron 	if (ret < 0) {
2793176dd5dSSachin Kamat 		pr_err("failed to allocate char dev region\n");
280a980e046SJonathan Cameron 		goto error_unregister_bus_type;
281a980e046SJonathan Cameron 	}
282a980e046SJonathan Cameron 
283a980e046SJonathan Cameron 	iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
284a980e046SJonathan Cameron 
285a980e046SJonathan Cameron 	return 0;
286a980e046SJonathan Cameron 
287a980e046SJonathan Cameron error_unregister_bus_type:
288a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
289a980e046SJonathan Cameron error_nothing:
290a980e046SJonathan Cameron 	return ret;
291a980e046SJonathan Cameron }
292a980e046SJonathan Cameron 
293a980e046SJonathan Cameron static void __exit iio_exit(void)
294a980e046SJonathan Cameron {
295a980e046SJonathan Cameron 	if (iio_devt)
296a980e046SJonathan Cameron 		unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
297a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
298a980e046SJonathan Cameron 	debugfs_remove(iio_debugfs_dentry);
299a980e046SJonathan Cameron }
300a980e046SJonathan Cameron 
301a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS)
302a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
303a980e046SJonathan Cameron 			      size_t count, loff_t *ppos)
304a980e046SJonathan Cameron {
305a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
306a980e046SJonathan Cameron 	char buf[20];
307a980e046SJonathan Cameron 	unsigned val = 0;
308a980e046SJonathan Cameron 	ssize_t len;
309a980e046SJonathan Cameron 	int ret;
310a980e046SJonathan Cameron 
311a980e046SJonathan Cameron 	ret = indio_dev->info->debugfs_reg_access(indio_dev,
312a980e046SJonathan Cameron 						  indio_dev->cached_reg_addr,
313a980e046SJonathan Cameron 						  0, &val);
3143d62c78aSMatt Fornero 	if (ret) {
315a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
3163d62c78aSMatt Fornero 		return ret;
3173d62c78aSMatt Fornero 	}
318a980e046SJonathan Cameron 
319a980e046SJonathan Cameron 	len = snprintf(buf, sizeof(buf), "0x%X\n", val);
320a980e046SJonathan Cameron 
321a980e046SJonathan Cameron 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
322a980e046SJonathan Cameron }
323a980e046SJonathan Cameron 
324a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file,
325a980e046SJonathan Cameron 		     const char __user *userbuf, size_t count, loff_t *ppos)
326a980e046SJonathan Cameron {
327a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
328a980e046SJonathan Cameron 	unsigned reg, val;
329a980e046SJonathan Cameron 	char buf[80];
330a980e046SJonathan Cameron 	int ret;
331a980e046SJonathan Cameron 
332a980e046SJonathan Cameron 	count = min_t(size_t, count, (sizeof(buf)-1));
333a980e046SJonathan Cameron 	if (copy_from_user(buf, userbuf, count))
334a980e046SJonathan Cameron 		return -EFAULT;
335a980e046SJonathan Cameron 
336a980e046SJonathan Cameron 	buf[count] = 0;
337a980e046SJonathan Cameron 
338a980e046SJonathan Cameron 	ret = sscanf(buf, "%i %i", &reg, &val);
339a980e046SJonathan Cameron 
340a980e046SJonathan Cameron 	switch (ret) {
341a980e046SJonathan Cameron 	case 1:
342a980e046SJonathan Cameron 		indio_dev->cached_reg_addr = reg;
343a980e046SJonathan Cameron 		break;
344a980e046SJonathan Cameron 	case 2:
345a980e046SJonathan Cameron 		indio_dev->cached_reg_addr = reg;
346a980e046SJonathan Cameron 		ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
347a980e046SJonathan Cameron 							  val, NULL);
348a980e046SJonathan Cameron 		if (ret) {
349a980e046SJonathan Cameron 			dev_err(indio_dev->dev.parent, "%s: write failed\n",
350a980e046SJonathan Cameron 				__func__);
351a980e046SJonathan Cameron 			return ret;
352a980e046SJonathan Cameron 		}
353a980e046SJonathan Cameron 		break;
354a980e046SJonathan Cameron 	default:
355a980e046SJonathan Cameron 		return -EINVAL;
356a980e046SJonathan Cameron 	}
357a980e046SJonathan Cameron 
358a980e046SJonathan Cameron 	return count;
359a980e046SJonathan Cameron }
360a980e046SJonathan Cameron 
361a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = {
3625a28c873SAxel Lin 	.open = simple_open,
363a980e046SJonathan Cameron 	.read = iio_debugfs_read_reg,
364a980e046SJonathan Cameron 	.write = iio_debugfs_write_reg,
365a980e046SJonathan Cameron };
366a980e046SJonathan Cameron 
367a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
368a980e046SJonathan Cameron {
369a980e046SJonathan Cameron 	debugfs_remove_recursive(indio_dev->debugfs_dentry);
370a980e046SJonathan Cameron }
371a980e046SJonathan Cameron 
372*8915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev)
373a980e046SJonathan Cameron {
374a980e046SJonathan Cameron 	if (indio_dev->info->debugfs_reg_access == NULL)
375*8915aacaSGreg Kroah-Hartman 		return;
376a980e046SJonathan Cameron 
377abd5a2fbSAxel Lin 	if (!iio_debugfs_dentry)
378*8915aacaSGreg Kroah-Hartman 		return;
379a980e046SJonathan Cameron 
380a980e046SJonathan Cameron 	indio_dev->debugfs_dentry =
381a980e046SJonathan Cameron 		debugfs_create_dir(dev_name(&indio_dev->dev),
382a980e046SJonathan Cameron 				   iio_debugfs_dentry);
383a980e046SJonathan Cameron 
384*8915aacaSGreg Kroah-Hartman 	debugfs_create_file("direct_reg_access", 0644,
385*8915aacaSGreg Kroah-Hartman 			    indio_dev->debugfs_dentry, indio_dev,
386*8915aacaSGreg Kroah-Hartman 			    &iio_debugfs_reg_fops);
387a980e046SJonathan Cameron }
388a980e046SJonathan Cameron #else
389*8915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev)
390a980e046SJonathan Cameron {
391a980e046SJonathan Cameron }
392a980e046SJonathan Cameron 
393a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
394a980e046SJonathan Cameron {
395a980e046SJonathan Cameron }
396a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */
397a980e046SJonathan Cameron 
398a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev,
399a980e046SJonathan Cameron 				     struct device_attribute *attr,
400a980e046SJonathan Cameron 				     char *buf)
401a980e046SJonathan Cameron {
402e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
403a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
404a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
405a980e046SJonathan Cameron 
406a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
407a980e046SJonathan Cameron 
408fc6d1139SMichael Hennerich 	return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
409a980e046SJonathan Cameron }
410a980e046SJonathan Cameron 
411a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev,
412a980e046SJonathan Cameron 				     struct device_attribute *attr,
413a980e046SJonathan Cameron 				     const char *buf,
414a980e046SJonathan Cameron 					 size_t len)
415a980e046SJonathan Cameron {
416e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
417a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
418a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
419a980e046SJonathan Cameron 
420a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
421a980e046SJonathan Cameron 
422fc6d1139SMichael Hennerich 	return ext_info->write(indio_dev, ext_info->private,
423fc6d1139SMichael Hennerich 			       this_attr->c, buf, len);
424a980e046SJonathan Cameron }
425a980e046SJonathan Cameron 
4265212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
4275212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
4285212cc8aSLars-Peter Clausen {
4295212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4305212cc8aSLars-Peter Clausen 	unsigned int i;
4315212cc8aSLars-Peter Clausen 	size_t len = 0;
4325212cc8aSLars-Peter Clausen 
4335212cc8aSLars-Peter Clausen 	if (!e->num_items)
4345212cc8aSLars-Peter Clausen 		return 0;
4355212cc8aSLars-Peter Clausen 
4365212cc8aSLars-Peter Clausen 	for (i = 0; i < e->num_items; ++i)
43774dcd439SLars-Peter Clausen 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
4385212cc8aSLars-Peter Clausen 
4395212cc8aSLars-Peter Clausen 	/* replace last space with a newline */
4405212cc8aSLars-Peter Clausen 	buf[len - 1] = '\n';
4415212cc8aSLars-Peter Clausen 
4425212cc8aSLars-Peter Clausen 	return len;
4435212cc8aSLars-Peter Clausen }
4445212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read);
4455212cc8aSLars-Peter Clausen 
4465212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev,
4475212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
4485212cc8aSLars-Peter Clausen {
4495212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4505212cc8aSLars-Peter Clausen 	int i;
4515212cc8aSLars-Peter Clausen 
4525212cc8aSLars-Peter Clausen 	if (!e->get)
4535212cc8aSLars-Peter Clausen 		return -EINVAL;
4545212cc8aSLars-Peter Clausen 
4555212cc8aSLars-Peter Clausen 	i = e->get(indio_dev, chan);
4565212cc8aSLars-Peter Clausen 	if (i < 0)
4575212cc8aSLars-Peter Clausen 		return i;
4585212cc8aSLars-Peter Clausen 	else if (i >= e->num_items)
4595212cc8aSLars-Peter Clausen 		return -EINVAL;
4605212cc8aSLars-Peter Clausen 
461598db581SKees Cook 	return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
4625212cc8aSLars-Peter Clausen }
4635212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read);
4645212cc8aSLars-Peter Clausen 
4655212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev,
4665212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
4675212cc8aSLars-Peter Clausen 	size_t len)
4685212cc8aSLars-Peter Clausen {
4695212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4705212cc8aSLars-Peter Clausen 	int ret;
4715212cc8aSLars-Peter Clausen 
4725212cc8aSLars-Peter Clausen 	if (!e->set)
4735212cc8aSLars-Peter Clausen 		return -EINVAL;
4745212cc8aSLars-Peter Clausen 
47502e9a0ffSAndy Shevchenko 	ret = __sysfs_match_string(e->items, e->num_items, buf);
47602e9a0ffSAndy Shevchenko 	if (ret < 0)
47702e9a0ffSAndy Shevchenko 		return ret;
4785212cc8aSLars-Peter Clausen 
47902e9a0ffSAndy Shevchenko 	ret = e->set(indio_dev, chan, ret);
4805212cc8aSLars-Peter Clausen 	return ret ? ret : len;
4815212cc8aSLars-Peter Clausen }
4825212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write);
4835212cc8aSLars-Peter Clausen 
484dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = {
485dfc57732SGregor Boirie 	.rotation = {
486dfc57732SGregor Boirie 		"1", "0", "0",
487dfc57732SGregor Boirie 		"0", "1", "0",
488dfc57732SGregor Boirie 		"0", "0", "1"
489dfc57732SGregor Boirie 	}
490dfc57732SGregor Boirie };
491dfc57732SGregor Boirie 
492dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev,
493dfc57732SGregor Boirie 				    struct iio_mount_matrix *matrix)
494dfc57732SGregor Boirie {
495dfc57732SGregor Boirie 	*matrix = iio_mount_idmatrix;
496dfc57732SGregor Boirie 	dev_info(dev, "mounting matrix not found: using identity...\n");
497dfc57732SGregor Boirie 	return 0;
498dfc57732SGregor Boirie }
499dfc57732SGregor Boirie 
500dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
501dfc57732SGregor Boirie 			      const struct iio_chan_spec *chan, char *buf)
502dfc57732SGregor Boirie {
503dfc57732SGregor Boirie 	const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
504dfc57732SGregor Boirie 					      priv)(indio_dev, chan);
505dfc57732SGregor Boirie 
506dfc57732SGregor Boirie 	if (IS_ERR(mtx))
507dfc57732SGregor Boirie 		return PTR_ERR(mtx);
508dfc57732SGregor Boirie 
509dfc57732SGregor Boirie 	if (!mtx)
510dfc57732SGregor Boirie 		mtx = &iio_mount_idmatrix;
511dfc57732SGregor Boirie 
512dfc57732SGregor Boirie 	return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
513dfc57732SGregor Boirie 			mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
514dfc57732SGregor Boirie 			mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
515dfc57732SGregor Boirie 			mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
516dfc57732SGregor Boirie }
517dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
518dfc57732SGregor Boirie 
519dfc57732SGregor Boirie /**
520fb158971SAndy Shevchenko  * iio_read_mount_matrix() - retrieve iio device mounting matrix from
521fb158971SAndy Shevchenko  *                           device "mount-matrix" property
522dfc57732SGregor Boirie  * @dev:	device the mounting matrix property is assigned to
523dfc57732SGregor Boirie  * @propname:	device specific mounting matrix property name
524dfc57732SGregor Boirie  * @matrix:	where to store retrieved matrix
525dfc57732SGregor Boirie  *
526dfc57732SGregor Boirie  * If device is assigned no mounting matrix property, a default 3x3 identity
527dfc57732SGregor Boirie  * matrix will be filled in.
528dfc57732SGregor Boirie  *
529dfc57732SGregor Boirie  * Return: 0 if success, or a negative error code on failure.
530dfc57732SGregor Boirie  */
531fb158971SAndy Shevchenko int iio_read_mount_matrix(struct device *dev, const char *propname,
532dfc57732SGregor Boirie 			  struct iio_mount_matrix *matrix)
533dfc57732SGregor Boirie {
534fb158971SAndy Shevchenko 	size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
535fb158971SAndy Shevchenko 	int err;
536dfc57732SGregor Boirie 
537fb158971SAndy Shevchenko 	err = device_property_read_string_array(dev, propname,
538fb158971SAndy Shevchenko 						matrix->rotation, len);
539fb158971SAndy Shevchenko 	if (err == len)
540dfc57732SGregor Boirie 		return 0;
541dfc57732SGregor Boirie 
542dfc57732SGregor Boirie 	if (err >= 0)
543dfc57732SGregor Boirie 		/* Invalid number of matrix entries. */
544dfc57732SGregor Boirie 		return -EINVAL;
545dfc57732SGregor Boirie 
546dfc57732SGregor Boirie 	if (err != -EINVAL)
547dfc57732SGregor Boirie 		/* Invalid matrix declaration format. */
548dfc57732SGregor Boirie 		return err;
549dfc57732SGregor Boirie 
550dfc57732SGregor Boirie 	/* Matrix was not declared at all: fallback to identity. */
551dfc57732SGregor Boirie 	return iio_setup_mount_idmatrix(dev, matrix);
552dfc57732SGregor Boirie }
553fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix);
554dfc57732SGregor Boirie 
55551239600SJonathan Cameron static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
55651239600SJonathan Cameron 				  int size, const int *vals)
55751239600SJonathan Cameron {
55851239600SJonathan Cameron 	unsigned long long tmp;
55951239600SJonathan Cameron 	int tmp0, tmp1;
56051239600SJonathan Cameron 	bool scale_db = false;
56151239600SJonathan Cameron 
56251239600SJonathan Cameron 	switch (type) {
56351239600SJonathan Cameron 	case IIO_VAL_INT:
56451239600SJonathan Cameron 		return snprintf(buf, len, "%d", vals[0]);
56551239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO_DB:
56651239600SJonathan Cameron 		scale_db = true;
5679d793c1aSGustavo A. R. Silva 		/* fall through */
56851239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO:
56951239600SJonathan Cameron 		if (vals[1] < 0)
57051239600SJonathan Cameron 			return snprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
57151239600SJonathan Cameron 					-vals[1], scale_db ? " dB" : "");
57251239600SJonathan Cameron 		else
57351239600SJonathan Cameron 			return snprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
57451239600SJonathan Cameron 					scale_db ? " dB" : "");
57551239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_NANO:
57651239600SJonathan Cameron 		if (vals[1] < 0)
57751239600SJonathan Cameron 			return snprintf(buf, len, "-%d.%09u", abs(vals[0]),
57851239600SJonathan Cameron 					-vals[1]);
57951239600SJonathan Cameron 		else
58051239600SJonathan Cameron 			return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
58151239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL:
58251239600SJonathan Cameron 		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
58351239600SJonathan Cameron 		tmp1 = vals[1];
58451239600SJonathan Cameron 		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
58551239600SJonathan Cameron 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
58651239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL_LOG2:
5877fd6592dSNikolaus Schulz 		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
5887fd6592dSNikolaus Schulz 		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
5897fd6592dSNikolaus Schulz 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
59051239600SJonathan Cameron 	case IIO_VAL_INT_MULTIPLE:
59151239600SJonathan Cameron 	{
59251239600SJonathan Cameron 		int i;
59351239600SJonathan Cameron 		int l = 0;
59451239600SJonathan Cameron 
59551239600SJonathan Cameron 		for (i = 0; i < size; ++i) {
59651239600SJonathan Cameron 			l += snprintf(&buf[l], len - l, "%d ", vals[i]);
59751239600SJonathan Cameron 			if (l >= len)
59851239600SJonathan Cameron 				break;
59951239600SJonathan Cameron 		}
60051239600SJonathan Cameron 		return l;
60151239600SJonathan Cameron 	}
60251239600SJonathan Cameron 	default:
60351239600SJonathan Cameron 		return 0;
60451239600SJonathan Cameron 	}
60551239600SJonathan Cameron }
60651239600SJonathan Cameron 
6073661f3f5SLars-Peter Clausen /**
6083661f3f5SLars-Peter Clausen  * iio_format_value() - Formats a IIO value into its string representation
6092498dcf6SCristina Opriceana  * @buf:	The buffer to which the formatted value gets written
61051239600SJonathan Cameron  *		which is assumed to be big enough (i.e. PAGE_SIZE).
611c175cb7cSRandy Dunlap  * @type:	One of the IIO_VAL_* constants. This decides how the val
6122498dcf6SCristina Opriceana  *		and val2 parameters are formatted.
6132498dcf6SCristina Opriceana  * @size:	Number of IIO value entries contained in vals
6142498dcf6SCristina Opriceana  * @vals:	Pointer to the values, exact meaning depends on the
6152498dcf6SCristina Opriceana  *		type parameter.
6162498dcf6SCristina Opriceana  *
6172498dcf6SCristina Opriceana  * Return: 0 by default, a negative number on failure or the
6182498dcf6SCristina Opriceana  *	   total number of characters written for a type that belongs
619c175cb7cSRandy Dunlap  *	   to the IIO_VAL_* constant.
6203661f3f5SLars-Peter Clausen  */
6219fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
622a980e046SJonathan Cameron {
62351239600SJonathan Cameron 	ssize_t len;
624a980e046SJonathan Cameron 
62551239600SJonathan Cameron 	len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
62651239600SJonathan Cameron 	if (len >= PAGE_SIZE - 1)
62751239600SJonathan Cameron 		return -EFBIG;
6289fbfb4b3SSrinivas Pandruvada 
62951239600SJonathan Cameron 	return len + sprintf(buf + len, "\n");
63067eedba3SMichael Hennerich }
6317d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value);
632a980e046SJonathan Cameron 
6333661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev,
6343661f3f5SLars-Peter Clausen 				     struct device_attribute *attr,
6353661f3f5SLars-Peter Clausen 				     char *buf)
6363661f3f5SLars-Peter Clausen {
6373661f3f5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
6383661f3f5SLars-Peter Clausen 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
6399fbfb4b3SSrinivas Pandruvada 	int vals[INDIO_MAX_RAW_ELEMENTS];
6409fbfb4b3SSrinivas Pandruvada 	int ret;
6419fbfb4b3SSrinivas Pandruvada 	int val_len = 2;
6429fbfb4b3SSrinivas Pandruvada 
6439fbfb4b3SSrinivas Pandruvada 	if (indio_dev->info->read_raw_multi)
6449fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
6459fbfb4b3SSrinivas Pandruvada 							INDIO_MAX_RAW_ELEMENTS,
6469fbfb4b3SSrinivas Pandruvada 							vals, &val_len,
6479fbfb4b3SSrinivas Pandruvada 							this_attr->address);
6489fbfb4b3SSrinivas Pandruvada 	else
6499fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
6509fbfb4b3SSrinivas Pandruvada 				    &vals[0], &vals[1], this_attr->address);
6513661f3f5SLars-Peter Clausen 
6523661f3f5SLars-Peter Clausen 	if (ret < 0)
6533661f3f5SLars-Peter Clausen 		return ret;
6543661f3f5SLars-Peter Clausen 
6559fbfb4b3SSrinivas Pandruvada 	return iio_format_value(buf, ret, val_len, vals);
6563661f3f5SLars-Peter Clausen }
6573661f3f5SLars-Peter Clausen 
65851239600SJonathan Cameron static ssize_t iio_format_avail_list(char *buf, const int *vals,
65951239600SJonathan Cameron 				     int type, int length)
66051239600SJonathan Cameron {
66151239600SJonathan Cameron 	int i;
66251239600SJonathan Cameron 	ssize_t len = 0;
66351239600SJonathan Cameron 
66451239600SJonathan Cameron 	switch (type) {
66551239600SJonathan Cameron 	case IIO_VAL_INT:
66651239600SJonathan Cameron 		for (i = 0; i < length; i++) {
66751239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
66851239600SJonathan Cameron 						  type, 1, &vals[i]);
66951239600SJonathan Cameron 			if (len >= PAGE_SIZE)
67051239600SJonathan Cameron 				return -EFBIG;
67151239600SJonathan Cameron 			if (i < length - 1)
67251239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
67351239600SJonathan Cameron 						" ");
67451239600SJonathan Cameron 			else
67551239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
67651239600SJonathan Cameron 						"\n");
67751239600SJonathan Cameron 			if (len >= PAGE_SIZE)
67851239600SJonathan Cameron 				return -EFBIG;
67951239600SJonathan Cameron 		}
68051239600SJonathan Cameron 		break;
68151239600SJonathan Cameron 	default:
68251239600SJonathan Cameron 		for (i = 0; i < length / 2; i++) {
68351239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
68451239600SJonathan Cameron 						  type, 2, &vals[i * 2]);
68551239600SJonathan Cameron 			if (len >= PAGE_SIZE)
68651239600SJonathan Cameron 				return -EFBIG;
68751239600SJonathan Cameron 			if (i < length / 2 - 1)
68851239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
68951239600SJonathan Cameron 						" ");
69051239600SJonathan Cameron 			else
69151239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
69251239600SJonathan Cameron 						"\n");
69351239600SJonathan Cameron 			if (len >= PAGE_SIZE)
69451239600SJonathan Cameron 				return -EFBIG;
69551239600SJonathan Cameron 		}
69651239600SJonathan Cameron 	}
69751239600SJonathan Cameron 
69851239600SJonathan Cameron 	return len;
69951239600SJonathan Cameron }
70051239600SJonathan Cameron 
70151239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
70251239600SJonathan Cameron {
70351239600SJonathan Cameron 	int i;
70451239600SJonathan Cameron 	ssize_t len;
70551239600SJonathan Cameron 
70651239600SJonathan Cameron 	len = snprintf(buf, PAGE_SIZE, "[");
70751239600SJonathan Cameron 	switch (type) {
70851239600SJonathan Cameron 	case IIO_VAL_INT:
70951239600SJonathan Cameron 		for (i = 0; i < 3; i++) {
71051239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
71151239600SJonathan Cameron 						  type, 1, &vals[i]);
71251239600SJonathan Cameron 			if (len >= PAGE_SIZE)
71351239600SJonathan Cameron 				return -EFBIG;
71451239600SJonathan Cameron 			if (i < 2)
71551239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
71651239600SJonathan Cameron 						" ");
71751239600SJonathan Cameron 			else
71851239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
71951239600SJonathan Cameron 						"]\n");
72051239600SJonathan Cameron 			if (len >= PAGE_SIZE)
72151239600SJonathan Cameron 				return -EFBIG;
72251239600SJonathan Cameron 		}
72351239600SJonathan Cameron 		break;
72451239600SJonathan Cameron 	default:
72551239600SJonathan Cameron 		for (i = 0; i < 3; i++) {
72651239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
72751239600SJonathan Cameron 						  type, 2, &vals[i * 2]);
72851239600SJonathan Cameron 			if (len >= PAGE_SIZE)
72951239600SJonathan Cameron 				return -EFBIG;
73051239600SJonathan Cameron 			if (i < 2)
73151239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
73251239600SJonathan Cameron 						" ");
73351239600SJonathan Cameron 			else
73451239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
73551239600SJonathan Cameron 						"]\n");
73651239600SJonathan Cameron 			if (len >= PAGE_SIZE)
73751239600SJonathan Cameron 				return -EFBIG;
73851239600SJonathan Cameron 		}
73951239600SJonathan Cameron 	}
74051239600SJonathan Cameron 
74151239600SJonathan Cameron 	return len;
74251239600SJonathan Cameron }
74351239600SJonathan Cameron 
74451239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev,
74551239600SJonathan Cameron 					   struct device_attribute *attr,
74651239600SJonathan Cameron 					   char *buf)
74751239600SJonathan Cameron {
74851239600SJonathan Cameron 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
74951239600SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
75051239600SJonathan Cameron 	const int *vals;
75151239600SJonathan Cameron 	int ret;
75251239600SJonathan Cameron 	int length;
75351239600SJonathan Cameron 	int type;
75451239600SJonathan Cameron 
75551239600SJonathan Cameron 	ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
75651239600SJonathan Cameron 					  &vals, &type, &length,
75751239600SJonathan Cameron 					  this_attr->address);
75851239600SJonathan Cameron 
75951239600SJonathan Cameron 	if (ret < 0)
76051239600SJonathan Cameron 		return ret;
76151239600SJonathan Cameron 	switch (ret) {
76251239600SJonathan Cameron 	case IIO_AVAIL_LIST:
76351239600SJonathan Cameron 		return iio_format_avail_list(buf, vals, type, length);
76451239600SJonathan Cameron 	case IIO_AVAIL_RANGE:
76551239600SJonathan Cameron 		return iio_format_avail_range(buf, vals, type);
76651239600SJonathan Cameron 	default:
76751239600SJonathan Cameron 		return -EINVAL;
76851239600SJonathan Cameron 	}
76951239600SJonathan Cameron }
77051239600SJonathan Cameron 
7716807d721SLars-Peter Clausen /**
7726807d721SLars-Peter Clausen  * iio_str_to_fixpoint() - Parse a fixed-point number from a string
7736807d721SLars-Peter Clausen  * @str: The string to parse
7746807d721SLars-Peter Clausen  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
7756807d721SLars-Peter Clausen  * @integer: The integer part of the number
7766807d721SLars-Peter Clausen  * @fract: The fractional part of the number
7776807d721SLars-Peter Clausen  *
7786807d721SLars-Peter Clausen  * Returns 0 on success, or a negative error code if the string could not be
7796807d721SLars-Peter Clausen  * parsed.
7806807d721SLars-Peter Clausen  */
7816807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult,
7826807d721SLars-Peter Clausen 	int *integer, int *fract)
7836807d721SLars-Peter Clausen {
7846807d721SLars-Peter Clausen 	int i = 0, f = 0;
7856807d721SLars-Peter Clausen 	bool integer_part = true, negative = false;
7866807d721SLars-Peter Clausen 
787f47dff32SSean Nyekjaer 	if (fract_mult == 0) {
788f47dff32SSean Nyekjaer 		*fract = 0;
789f47dff32SSean Nyekjaer 
790f47dff32SSean Nyekjaer 		return kstrtoint(str, 0, integer);
791f47dff32SSean Nyekjaer 	}
792f47dff32SSean Nyekjaer 
7936807d721SLars-Peter Clausen 	if (str[0] == '-') {
7946807d721SLars-Peter Clausen 		negative = true;
7956807d721SLars-Peter Clausen 		str++;
7966807d721SLars-Peter Clausen 	} else if (str[0] == '+') {
7976807d721SLars-Peter Clausen 		str++;
7986807d721SLars-Peter Clausen 	}
7996807d721SLars-Peter Clausen 
8006807d721SLars-Peter Clausen 	while (*str) {
8016807d721SLars-Peter Clausen 		if ('0' <= *str && *str <= '9') {
8026807d721SLars-Peter Clausen 			if (integer_part) {
8036807d721SLars-Peter Clausen 				i = i * 10 + *str - '0';
8046807d721SLars-Peter Clausen 			} else {
8056807d721SLars-Peter Clausen 				f += fract_mult * (*str - '0');
8066807d721SLars-Peter Clausen 				fract_mult /= 10;
8076807d721SLars-Peter Clausen 			}
8086807d721SLars-Peter Clausen 		} else if (*str == '\n') {
8096807d721SLars-Peter Clausen 			if (*(str + 1) == '\0')
8106807d721SLars-Peter Clausen 				break;
8116807d721SLars-Peter Clausen 			else
8126807d721SLars-Peter Clausen 				return -EINVAL;
8136807d721SLars-Peter Clausen 		} else if (*str == '.' && integer_part) {
8146807d721SLars-Peter Clausen 			integer_part = false;
8156807d721SLars-Peter Clausen 		} else {
8166807d721SLars-Peter Clausen 			return -EINVAL;
8176807d721SLars-Peter Clausen 		}
8186807d721SLars-Peter Clausen 		str++;
8196807d721SLars-Peter Clausen 	}
8206807d721SLars-Peter Clausen 
8216807d721SLars-Peter Clausen 	if (negative) {
8226807d721SLars-Peter Clausen 		if (i)
8236807d721SLars-Peter Clausen 			i = -i;
8246807d721SLars-Peter Clausen 		else
8256807d721SLars-Peter Clausen 			f = -f;
8266807d721SLars-Peter Clausen 	}
8276807d721SLars-Peter Clausen 
8286807d721SLars-Peter Clausen 	*integer = i;
8296807d721SLars-Peter Clausen 	*fract = f;
8306807d721SLars-Peter Clausen 
8316807d721SLars-Peter Clausen 	return 0;
8326807d721SLars-Peter Clausen }
8336807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
8346807d721SLars-Peter Clausen 
835a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev,
836a980e046SJonathan Cameron 				      struct device_attribute *attr,
837a980e046SJonathan Cameron 				      const char *buf,
838a980e046SJonathan Cameron 				      size_t len)
839a980e046SJonathan Cameron {
840e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
841a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
8426807d721SLars-Peter Clausen 	int ret, fract_mult = 100000;
8436807d721SLars-Peter Clausen 	int integer, fract;
844a980e046SJonathan Cameron 
845a980e046SJonathan Cameron 	/* Assumes decimal - precision based on number of digits */
846a980e046SJonathan Cameron 	if (!indio_dev->info->write_raw)
847a980e046SJonathan Cameron 		return -EINVAL;
848a980e046SJonathan Cameron 
849a980e046SJonathan Cameron 	if (indio_dev->info->write_raw_get_fmt)
850a980e046SJonathan Cameron 		switch (indio_dev->info->write_raw_get_fmt(indio_dev,
851a980e046SJonathan Cameron 			this_attr->c, this_attr->address)) {
852f47dff32SSean Nyekjaer 		case IIO_VAL_INT:
853f47dff32SSean Nyekjaer 			fract_mult = 0;
854f47dff32SSean Nyekjaer 			break;
855a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_MICRO:
856a980e046SJonathan Cameron 			fract_mult = 100000;
857a980e046SJonathan Cameron 			break;
858a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_NANO:
859a980e046SJonathan Cameron 			fract_mult = 100000000;
860a980e046SJonathan Cameron 			break;
861a980e046SJonathan Cameron 		default:
862a980e046SJonathan Cameron 			return -EINVAL;
863a980e046SJonathan Cameron 		}
864a980e046SJonathan Cameron 
8656807d721SLars-Peter Clausen 	ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
8666807d721SLars-Peter Clausen 	if (ret)
8676807d721SLars-Peter Clausen 		return ret;
868a980e046SJonathan Cameron 
869a980e046SJonathan Cameron 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
870a980e046SJonathan Cameron 					 integer, fract, this_attr->address);
871a980e046SJonathan Cameron 	if (ret)
872a980e046SJonathan Cameron 		return ret;
873a980e046SJonathan Cameron 
874a980e046SJonathan Cameron 	return len;
875a980e046SJonathan Cameron }
876a980e046SJonathan Cameron 
877a980e046SJonathan Cameron static
878a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr,
879a980e046SJonathan Cameron 			   const char *postfix,
880a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
881a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
882a980e046SJonathan Cameron 					       struct device_attribute *attr,
883a980e046SJonathan Cameron 					       char *buf),
884a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
885a980e046SJonathan Cameron 						struct device_attribute *attr,
886a980e046SJonathan Cameron 						const char *buf,
887a980e046SJonathan Cameron 						size_t len),
8883704432fSJonathan Cameron 			   enum iio_shared_by shared_by)
889a980e046SJonathan Cameron {
8903704432fSJonathan Cameron 	int ret = 0;
8917bbcf7e1SLars-Peter Clausen 	char *name = NULL;
8923704432fSJonathan Cameron 	char *full_postfix;
893a980e046SJonathan Cameron 	sysfs_attr_init(&dev_attr->attr);
894a980e046SJonathan Cameron 
895a980e046SJonathan Cameron 	/* Build up postfix of <extend_name>_<modifier>_postfix */
8963704432fSJonathan Cameron 	if (chan->modified && (shared_by == IIO_SEPARATE)) {
897a980e046SJonathan Cameron 		if (chan->extend_name)
898a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
899a980e046SJonathan Cameron 						 iio_modifier_names[chan
900a980e046SJonathan Cameron 								    ->channel2],
901a980e046SJonathan Cameron 						 chan->extend_name,
902a980e046SJonathan Cameron 						 postfix);
903a980e046SJonathan Cameron 		else
904a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
905a980e046SJonathan Cameron 						 iio_modifier_names[chan
906a980e046SJonathan Cameron 								    ->channel2],
907a980e046SJonathan Cameron 						 postfix);
908a980e046SJonathan Cameron 	} else {
90977bfa8baSLars-Peter Clausen 		if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
910a980e046SJonathan Cameron 			full_postfix = kstrdup(postfix, GFP_KERNEL);
911a980e046SJonathan Cameron 		else
912a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL,
913a980e046SJonathan Cameron 						 "%s_%s",
914a980e046SJonathan Cameron 						 chan->extend_name,
915a980e046SJonathan Cameron 						 postfix);
916a980e046SJonathan Cameron 	}
9173704432fSJonathan Cameron 	if (full_postfix == NULL)
9183704432fSJonathan Cameron 		return -ENOMEM;
919a980e046SJonathan Cameron 
920a980e046SJonathan Cameron 	if (chan->differential) { /* Differential can not have modifier */
9213704432fSJonathan Cameron 		switch (shared_by) {
922c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
9237bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
924c006ec83SJonathan Cameron 			break;
925c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
9267bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
927c006ec83SJonathan Cameron 						iio_direction[chan->output],
928c006ec83SJonathan Cameron 						full_postfix);
929c006ec83SJonathan Cameron 			break;
9303704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
9317bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
932a980e046SJonathan Cameron 					    iio_direction[chan->output],
933a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
934a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
935a980e046SJonathan Cameron 					    full_postfix);
9363704432fSJonathan Cameron 			break;
9373704432fSJonathan Cameron 		case IIO_SEPARATE:
9383704432fSJonathan Cameron 			if (!chan->indexed) {
939231bfe53SDan Carpenter 				WARN(1, "Differential channels must be indexed\n");
9403704432fSJonathan Cameron 				ret = -EINVAL;
9413704432fSJonathan Cameron 				goto error_free_full_postfix;
9423704432fSJonathan Cameron 			}
9437bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL,
9443704432fSJonathan Cameron 					    "%s_%s%d-%s%d_%s",
945a980e046SJonathan Cameron 					    iio_direction[chan->output],
946a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
947a980e046SJonathan Cameron 					    chan->channel,
948a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
949a980e046SJonathan Cameron 					    chan->channel2,
950a980e046SJonathan Cameron 					    full_postfix);
9513704432fSJonathan Cameron 			break;
952a980e046SJonathan Cameron 		}
953a980e046SJonathan Cameron 	} else { /* Single ended */
9543704432fSJonathan Cameron 		switch (shared_by) {
955c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
9567bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
957c006ec83SJonathan Cameron 			break;
958c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
9597bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
960c006ec83SJonathan Cameron 						iio_direction[chan->output],
961c006ec83SJonathan Cameron 						full_postfix);
962c006ec83SJonathan Cameron 			break;
9633704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
9647bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s_%s",
965a980e046SJonathan Cameron 					    iio_direction[chan->output],
966a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
967a980e046SJonathan Cameron 					    full_postfix);
9683704432fSJonathan Cameron 			break;
9693704432fSJonathan Cameron 
9703704432fSJonathan Cameron 		case IIO_SEPARATE:
9713704432fSJonathan Cameron 			if (chan->indexed)
9727bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
973a980e046SJonathan Cameron 						    iio_direction[chan->output],
974a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
975a980e046SJonathan Cameron 						    chan->channel,
976a980e046SJonathan Cameron 						    full_postfix);
977a980e046SJonathan Cameron 			else
9787bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s_%s",
979a980e046SJonathan Cameron 						    iio_direction[chan->output],
980a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
981a980e046SJonathan Cameron 						    full_postfix);
9823704432fSJonathan Cameron 			break;
9833704432fSJonathan Cameron 		}
984a980e046SJonathan Cameron 	}
9857bbcf7e1SLars-Peter Clausen 	if (name == NULL) {
986a980e046SJonathan Cameron 		ret = -ENOMEM;
987a980e046SJonathan Cameron 		goto error_free_full_postfix;
988a980e046SJonathan Cameron 	}
9897bbcf7e1SLars-Peter Clausen 	dev_attr->attr.name = name;
990a980e046SJonathan Cameron 
991a980e046SJonathan Cameron 	if (readfunc) {
992a980e046SJonathan Cameron 		dev_attr->attr.mode |= S_IRUGO;
993a980e046SJonathan Cameron 		dev_attr->show = readfunc;
994a980e046SJonathan Cameron 	}
995a980e046SJonathan Cameron 
996a980e046SJonathan Cameron 	if (writefunc) {
997a980e046SJonathan Cameron 		dev_attr->attr.mode |= S_IWUSR;
998a980e046SJonathan Cameron 		dev_attr->store = writefunc;
999a980e046SJonathan Cameron 	}
10007bbcf7e1SLars-Peter Clausen 
1001a980e046SJonathan Cameron error_free_full_postfix:
1002a980e046SJonathan Cameron 	kfree(full_postfix);
10033704432fSJonathan Cameron 
1004a980e046SJonathan Cameron 	return ret;
1005a980e046SJonathan Cameron }
1006a980e046SJonathan Cameron 
1007a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1008a980e046SJonathan Cameron {
1009a980e046SJonathan Cameron 	kfree(dev_attr->attr.name);
1010a980e046SJonathan Cameron }
1011a980e046SJonathan Cameron 
1012a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix,
1013a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
1014a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
1015a980e046SJonathan Cameron 					       struct device_attribute *attr,
1016a980e046SJonathan Cameron 					       char *buf),
1017a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
1018a980e046SJonathan Cameron 						struct device_attribute *attr,
1019a980e046SJonathan Cameron 						const char *buf,
1020a980e046SJonathan Cameron 						size_t len),
1021a980e046SJonathan Cameron 			   u64 mask,
10223704432fSJonathan Cameron 			   enum iio_shared_by shared_by,
1023a980e046SJonathan Cameron 			   struct device *dev,
1024a980e046SJonathan Cameron 			   struct list_head *attr_list)
1025a980e046SJonathan Cameron {
1026a980e046SJonathan Cameron 	int ret;
1027a980e046SJonathan Cameron 	struct iio_dev_attr *iio_attr, *t;
1028a980e046SJonathan Cameron 
1029670c1103SSachin Kamat 	iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
103092825ff9SHartmut Knaack 	if (iio_attr == NULL)
103192825ff9SHartmut Knaack 		return -ENOMEM;
1032a980e046SJonathan Cameron 	ret = __iio_device_attr_init(&iio_attr->dev_attr,
1033a980e046SJonathan Cameron 				     postfix, chan,
10343704432fSJonathan Cameron 				     readfunc, writefunc, shared_by);
1035a980e046SJonathan Cameron 	if (ret)
1036a980e046SJonathan Cameron 		goto error_iio_dev_attr_free;
1037a980e046SJonathan Cameron 	iio_attr->c = chan;
1038a980e046SJonathan Cameron 	iio_attr->address = mask;
1039a980e046SJonathan Cameron 	list_for_each_entry(t, attr_list, l)
1040a980e046SJonathan Cameron 		if (strcmp(t->dev_attr.attr.name,
1041a980e046SJonathan Cameron 			   iio_attr->dev_attr.attr.name) == 0) {
10423704432fSJonathan Cameron 			if (shared_by == IIO_SEPARATE)
1043a980e046SJonathan Cameron 				dev_err(dev, "tried to double register : %s\n",
1044a980e046SJonathan Cameron 					t->dev_attr.attr.name);
1045a980e046SJonathan Cameron 			ret = -EBUSY;
1046a980e046SJonathan Cameron 			goto error_device_attr_deinit;
1047a980e046SJonathan Cameron 		}
1048a980e046SJonathan Cameron 	list_add(&iio_attr->l, attr_list);
1049a980e046SJonathan Cameron 
1050a980e046SJonathan Cameron 	return 0;
1051a980e046SJonathan Cameron 
1052a980e046SJonathan Cameron error_device_attr_deinit:
1053a980e046SJonathan Cameron 	__iio_device_attr_deinit(&iio_attr->dev_attr);
1054a980e046SJonathan Cameron error_iio_dev_attr_free:
1055a980e046SJonathan Cameron 	kfree(iio_attr);
1056a980e046SJonathan Cameron 	return ret;
1057a980e046SJonathan Cameron }
1058a980e046SJonathan Cameron 
10593704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
10603704432fSJonathan Cameron 					 struct iio_chan_spec const *chan,
10613704432fSJonathan Cameron 					 enum iio_shared_by shared_by,
10623704432fSJonathan Cameron 					 const long *infomask)
10633704432fSJonathan Cameron {
10643704432fSJonathan Cameron 	int i, ret, attrcount = 0;
10653704432fSJonathan Cameron 
10661016d567SOrson Zhai 	for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1067ef4b4856SJonathan Cameron 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1068ef4b4856SJonathan Cameron 			return -EINVAL;
10693704432fSJonathan Cameron 		ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
10703704432fSJonathan Cameron 					     chan,
10713704432fSJonathan Cameron 					     &iio_read_channel_info,
10723704432fSJonathan Cameron 					     &iio_write_channel_info,
10733704432fSJonathan Cameron 					     i,
10743704432fSJonathan Cameron 					     shared_by,
10753704432fSJonathan Cameron 					     &indio_dev->dev,
10763704432fSJonathan Cameron 					     &indio_dev->channel_attr_list);
10773704432fSJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
10783704432fSJonathan Cameron 			continue;
10793704432fSJonathan Cameron 		else if (ret < 0)
10803704432fSJonathan Cameron 			return ret;
10813704432fSJonathan Cameron 		attrcount++;
10823704432fSJonathan Cameron 	}
10833704432fSJonathan Cameron 
10843704432fSJonathan Cameron 	return attrcount;
10853704432fSJonathan Cameron }
10863704432fSJonathan Cameron 
108751239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
108851239600SJonathan Cameron 					       struct iio_chan_spec const *chan,
108951239600SJonathan Cameron 					       enum iio_shared_by shared_by,
109051239600SJonathan Cameron 					       const long *infomask)
109151239600SJonathan Cameron {
109251239600SJonathan Cameron 	int i, ret, attrcount = 0;
109351239600SJonathan Cameron 	char *avail_postfix;
109451239600SJonathan Cameron 
10951016d567SOrson Zhai 	for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1096936d3e53SYoung Xiao 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1097936d3e53SYoung Xiao 			return -EINVAL;
109851239600SJonathan Cameron 		avail_postfix = kasprintf(GFP_KERNEL,
109951239600SJonathan Cameron 					  "%s_available",
110051239600SJonathan Cameron 					  iio_chan_info_postfix[i]);
110151239600SJonathan Cameron 		if (!avail_postfix)
110251239600SJonathan Cameron 			return -ENOMEM;
110351239600SJonathan Cameron 
110451239600SJonathan Cameron 		ret = __iio_add_chan_devattr(avail_postfix,
110551239600SJonathan Cameron 					     chan,
110651239600SJonathan Cameron 					     &iio_read_channel_info_avail,
110751239600SJonathan Cameron 					     NULL,
110851239600SJonathan Cameron 					     i,
110951239600SJonathan Cameron 					     shared_by,
111051239600SJonathan Cameron 					     &indio_dev->dev,
111151239600SJonathan Cameron 					     &indio_dev->channel_attr_list);
111251239600SJonathan Cameron 		kfree(avail_postfix);
111351239600SJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
111451239600SJonathan Cameron 			continue;
111551239600SJonathan Cameron 		else if (ret < 0)
111651239600SJonathan Cameron 			return ret;
111751239600SJonathan Cameron 		attrcount++;
111851239600SJonathan Cameron 	}
111951239600SJonathan Cameron 
112051239600SJonathan Cameron 	return attrcount;
112151239600SJonathan Cameron }
112251239600SJonathan Cameron 
1123a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1124a980e046SJonathan Cameron 					struct iio_chan_spec const *chan)
1125a980e046SJonathan Cameron {
1126a980e046SJonathan Cameron 	int ret, attrcount = 0;
1127a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
1128a980e046SJonathan Cameron 
1129a980e046SJonathan Cameron 	if (chan->channel < 0)
1130a980e046SJonathan Cameron 		return 0;
11313704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
11323704432fSJonathan Cameron 					    IIO_SEPARATE,
11333704432fSJonathan Cameron 					    &chan->info_mask_separate);
11348655cc49SJonathan Cameron 	if (ret < 0)
11353704432fSJonathan Cameron 		return ret;
11363704432fSJonathan Cameron 	attrcount += ret;
11373704432fSJonathan Cameron 
113851239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
113951239600SJonathan Cameron 						  IIO_SEPARATE,
114051239600SJonathan Cameron 						  &chan->
114151239600SJonathan Cameron 						  info_mask_separate_available);
114251239600SJonathan Cameron 	if (ret < 0)
114351239600SJonathan Cameron 		return ret;
114451239600SJonathan Cameron 	attrcount += ret;
114551239600SJonathan Cameron 
11463704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
11473704432fSJonathan Cameron 					    IIO_SHARED_BY_TYPE,
11483704432fSJonathan Cameron 					    &chan->info_mask_shared_by_type);
11493704432fSJonathan Cameron 	if (ret < 0)
11503704432fSJonathan Cameron 		return ret;
11513704432fSJonathan Cameron 	attrcount += ret;
1152a980e046SJonathan Cameron 
115351239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
115451239600SJonathan Cameron 						  IIO_SHARED_BY_TYPE,
115551239600SJonathan Cameron 						  &chan->
115651239600SJonathan Cameron 						  info_mask_shared_by_type_available);
115751239600SJonathan Cameron 	if (ret < 0)
115851239600SJonathan Cameron 		return ret;
115951239600SJonathan Cameron 	attrcount += ret;
116051239600SJonathan Cameron 
1161c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1162c006ec83SJonathan Cameron 					    IIO_SHARED_BY_DIR,
1163c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_dir);
1164c006ec83SJonathan Cameron 	if (ret < 0)
1165c006ec83SJonathan Cameron 		return ret;
1166c006ec83SJonathan Cameron 	attrcount += ret;
1167c006ec83SJonathan Cameron 
116851239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
116951239600SJonathan Cameron 						  IIO_SHARED_BY_DIR,
117051239600SJonathan Cameron 						  &chan->info_mask_shared_by_dir_available);
117151239600SJonathan Cameron 	if (ret < 0)
117251239600SJonathan Cameron 		return ret;
117351239600SJonathan Cameron 	attrcount += ret;
117451239600SJonathan Cameron 
1175c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1176c006ec83SJonathan Cameron 					    IIO_SHARED_BY_ALL,
1177c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_all);
1178c006ec83SJonathan Cameron 	if (ret < 0)
1179c006ec83SJonathan Cameron 		return ret;
1180c006ec83SJonathan Cameron 	attrcount += ret;
1181c006ec83SJonathan Cameron 
118251239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
118351239600SJonathan Cameron 						  IIO_SHARED_BY_ALL,
118451239600SJonathan Cameron 						  &chan->info_mask_shared_by_all_available);
118551239600SJonathan Cameron 	if (ret < 0)
118651239600SJonathan Cameron 		return ret;
118751239600SJonathan Cameron 	attrcount += ret;
118851239600SJonathan Cameron 
1189a980e046SJonathan Cameron 	if (chan->ext_info) {
1190a980e046SJonathan Cameron 		unsigned int i = 0;
1191a980e046SJonathan Cameron 		for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1192a980e046SJonathan Cameron 			ret = __iio_add_chan_devattr(ext_info->name,
1193a980e046SJonathan Cameron 					chan,
1194a980e046SJonathan Cameron 					ext_info->read ?
1195a980e046SJonathan Cameron 					    &iio_read_channel_ext_info : NULL,
1196a980e046SJonathan Cameron 					ext_info->write ?
1197a980e046SJonathan Cameron 					    &iio_write_channel_ext_info : NULL,
1198a980e046SJonathan Cameron 					i,
1199a980e046SJonathan Cameron 					ext_info->shared,
1200a980e046SJonathan Cameron 					&indio_dev->dev,
1201a980e046SJonathan Cameron 					&indio_dev->channel_attr_list);
1202a980e046SJonathan Cameron 			i++;
1203a980e046SJonathan Cameron 			if (ret == -EBUSY && ext_info->shared)
1204a980e046SJonathan Cameron 				continue;
1205a980e046SJonathan Cameron 
1206a980e046SJonathan Cameron 			if (ret)
12073704432fSJonathan Cameron 				return ret;
1208a980e046SJonathan Cameron 
1209a980e046SJonathan Cameron 			attrcount++;
1210a980e046SJonathan Cameron 		}
1211a980e046SJonathan Cameron 	}
1212a980e046SJonathan Cameron 
12133704432fSJonathan Cameron 	return attrcount;
1214a980e046SJonathan Cameron }
1215a980e046SJonathan Cameron 
121684088ebdSLars-Peter Clausen /**
121784088ebdSLars-Peter Clausen  * iio_free_chan_devattr_list() - Free a list of IIO device attributes
121884088ebdSLars-Peter Clausen  * @attr_list: List of IIO device attributes
121984088ebdSLars-Peter Clausen  *
122084088ebdSLars-Peter Clausen  * This function frees the memory allocated for each of the IIO device
1221c1b03ab5SMartin Fuzzey  * attributes in the list.
122284088ebdSLars-Peter Clausen  */
122384088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list)
1224a980e046SJonathan Cameron {
122584088ebdSLars-Peter Clausen 	struct iio_dev_attr *p, *n;
122684088ebdSLars-Peter Clausen 
122784088ebdSLars-Peter Clausen 	list_for_each_entry_safe(p, n, attr_list, l) {
1228a980e046SJonathan Cameron 		kfree(p->dev_attr.attr.name);
1229c1b03ab5SMartin Fuzzey 		list_del(&p->l);
1230a980e046SJonathan Cameron 		kfree(p);
1231a980e046SJonathan Cameron 	}
123284088ebdSLars-Peter Clausen }
1233a980e046SJonathan Cameron 
1234a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev,
1235a980e046SJonathan Cameron 				 struct device_attribute *attr,
1236a980e046SJonathan Cameron 				 char *buf)
1237a980e046SJonathan Cameron {
1238e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1239598db581SKees Cook 	return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
1240a980e046SJonathan Cameron }
1241a980e046SJonathan Cameron 
1242a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1243a980e046SJonathan Cameron 
1244bc2b7dabSGregor Boirie static ssize_t iio_show_timestamp_clock(struct device *dev,
1245bc2b7dabSGregor Boirie 					struct device_attribute *attr,
1246bc2b7dabSGregor Boirie 					char *buf)
1247bc2b7dabSGregor Boirie {
1248bc2b7dabSGregor Boirie 	const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1249bc2b7dabSGregor Boirie 	const clockid_t clk = iio_device_get_clock(indio_dev);
1250bc2b7dabSGregor Boirie 	const char *name;
1251bc2b7dabSGregor Boirie 	ssize_t sz;
1252bc2b7dabSGregor Boirie 
1253bc2b7dabSGregor Boirie 	switch (clk) {
1254bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
1255bc2b7dabSGregor Boirie 		name = "realtime\n";
1256bc2b7dabSGregor Boirie 		sz = sizeof("realtime\n");
1257bc2b7dabSGregor Boirie 		break;
1258bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
1259bc2b7dabSGregor Boirie 		name = "monotonic\n";
1260bc2b7dabSGregor Boirie 		sz = sizeof("monotonic\n");
1261bc2b7dabSGregor Boirie 		break;
1262bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
1263bc2b7dabSGregor Boirie 		name = "monotonic_raw\n";
1264bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_raw\n");
1265bc2b7dabSGregor Boirie 		break;
1266bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
1267bc2b7dabSGregor Boirie 		name = "realtime_coarse\n";
1268bc2b7dabSGregor Boirie 		sz = sizeof("realtime_coarse\n");
1269bc2b7dabSGregor Boirie 		break;
1270bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
1271bc2b7dabSGregor Boirie 		name = "monotonic_coarse\n";
1272bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_coarse\n");
1273bc2b7dabSGregor Boirie 		break;
1274bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
1275bc2b7dabSGregor Boirie 		name = "boottime\n";
1276bc2b7dabSGregor Boirie 		sz = sizeof("boottime\n");
1277bc2b7dabSGregor Boirie 		break;
1278bc2b7dabSGregor Boirie 	case CLOCK_TAI:
1279bc2b7dabSGregor Boirie 		name = "tai\n";
1280bc2b7dabSGregor Boirie 		sz = sizeof("tai\n");
1281bc2b7dabSGregor Boirie 		break;
1282bc2b7dabSGregor Boirie 	default:
1283bc2b7dabSGregor Boirie 		BUG();
1284bc2b7dabSGregor Boirie 	}
1285bc2b7dabSGregor Boirie 
1286bc2b7dabSGregor Boirie 	memcpy(buf, name, sz);
1287bc2b7dabSGregor Boirie 	return sz;
1288bc2b7dabSGregor Boirie }
1289bc2b7dabSGregor Boirie 
1290bc2b7dabSGregor Boirie static ssize_t iio_store_timestamp_clock(struct device *dev,
1291bc2b7dabSGregor Boirie 					 struct device_attribute *attr,
1292bc2b7dabSGregor Boirie 					 const char *buf, size_t len)
1293bc2b7dabSGregor Boirie {
1294bc2b7dabSGregor Boirie 	clockid_t clk;
1295bc2b7dabSGregor Boirie 	int ret;
1296bc2b7dabSGregor Boirie 
1297bc2b7dabSGregor Boirie 	if (sysfs_streq(buf, "realtime"))
1298bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME;
1299bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic"))
1300bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC;
1301bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_raw"))
1302bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_RAW;
1303bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "realtime_coarse"))
1304bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME_COARSE;
1305bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_coarse"))
1306bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_COARSE;
1307bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "boottime"))
1308bc2b7dabSGregor Boirie 		clk = CLOCK_BOOTTIME;
1309bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "tai"))
1310bc2b7dabSGregor Boirie 		clk = CLOCK_TAI;
1311bc2b7dabSGregor Boirie 	else
1312bc2b7dabSGregor Boirie 		return -EINVAL;
1313bc2b7dabSGregor Boirie 
1314bc2b7dabSGregor Boirie 	ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1315bc2b7dabSGregor Boirie 	if (ret)
1316bc2b7dabSGregor Boirie 		return ret;
1317bc2b7dabSGregor Boirie 
1318bc2b7dabSGregor Boirie 	return len;
1319bc2b7dabSGregor Boirie }
1320bc2b7dabSGregor Boirie 
1321bc2b7dabSGregor Boirie static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1322bc2b7dabSGregor Boirie 		   iio_show_timestamp_clock, iio_store_timestamp_clock);
1323bc2b7dabSGregor Boirie 
1324a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1325a980e046SJonathan Cameron {
1326a980e046SJonathan Cameron 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
132784088ebdSLars-Peter Clausen 	struct iio_dev_attr *p;
1328bc2b7dabSGregor Boirie 	struct attribute **attr, *clk = NULL;
1329a980e046SJonathan Cameron 
1330a980e046SJonathan Cameron 	/* First count elements in any existing group */
1331a980e046SJonathan Cameron 	if (indio_dev->info->attrs) {
1332a980e046SJonathan Cameron 		attr = indio_dev->info->attrs->attrs;
1333a980e046SJonathan Cameron 		while (*attr++ != NULL)
1334a980e046SJonathan Cameron 			attrcount_orig++;
1335a980e046SJonathan Cameron 	}
1336a980e046SJonathan Cameron 	attrcount = attrcount_orig;
1337a980e046SJonathan Cameron 	/*
1338a980e046SJonathan Cameron 	 * New channel registration method - relies on the fact a group does
1339d25b3808SPeter Meerwald 	 * not need to be initialized if its name is NULL.
1340a980e046SJonathan Cameron 	 */
1341a980e046SJonathan Cameron 	if (indio_dev->channels)
1342a980e046SJonathan Cameron 		for (i = 0; i < indio_dev->num_channels; i++) {
1343bc2b7dabSGregor Boirie 			const struct iio_chan_spec *chan =
1344bc2b7dabSGregor Boirie 				&indio_dev->channels[i];
1345bc2b7dabSGregor Boirie 
1346bc2b7dabSGregor Boirie 			if (chan->type == IIO_TIMESTAMP)
1347bc2b7dabSGregor Boirie 				clk = &dev_attr_current_timestamp_clock.attr;
1348bc2b7dabSGregor Boirie 
1349bc2b7dabSGregor Boirie 			ret = iio_device_add_channel_sysfs(indio_dev, chan);
1350a980e046SJonathan Cameron 			if (ret < 0)
1351a980e046SJonathan Cameron 				goto error_clear_attrs;
1352a980e046SJonathan Cameron 			attrcount += ret;
1353a980e046SJonathan Cameron 		}
1354a980e046SJonathan Cameron 
1355bc2b7dabSGregor Boirie 	if (indio_dev->event_interface)
1356bc2b7dabSGregor Boirie 		clk = &dev_attr_current_timestamp_clock.attr;
1357bc2b7dabSGregor Boirie 
1358a980e046SJonathan Cameron 	if (indio_dev->name)
1359a980e046SJonathan Cameron 		attrcount++;
1360bc2b7dabSGregor Boirie 	if (clk)
1361bc2b7dabSGregor Boirie 		attrcount++;
1362a980e046SJonathan Cameron 
1363a980e046SJonathan Cameron 	indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
1364a980e046SJonathan Cameron 						   sizeof(indio_dev->chan_attr_group.attrs[0]),
1365a980e046SJonathan Cameron 						   GFP_KERNEL);
1366a980e046SJonathan Cameron 	if (indio_dev->chan_attr_group.attrs == NULL) {
1367a980e046SJonathan Cameron 		ret = -ENOMEM;
1368a980e046SJonathan Cameron 		goto error_clear_attrs;
1369a980e046SJonathan Cameron 	}
1370a980e046SJonathan Cameron 	/* Copy across original attributes */
1371a980e046SJonathan Cameron 	if (indio_dev->info->attrs)
1372a980e046SJonathan Cameron 		memcpy(indio_dev->chan_attr_group.attrs,
1373a980e046SJonathan Cameron 		       indio_dev->info->attrs->attrs,
1374a980e046SJonathan Cameron 		       sizeof(indio_dev->chan_attr_group.attrs[0])
1375a980e046SJonathan Cameron 		       *attrcount_orig);
1376a980e046SJonathan Cameron 	attrn = attrcount_orig;
1377a980e046SJonathan Cameron 	/* Add all elements from the list. */
1378a980e046SJonathan Cameron 	list_for_each_entry(p, &indio_dev->channel_attr_list, l)
1379a980e046SJonathan Cameron 		indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1380a980e046SJonathan Cameron 	if (indio_dev->name)
1381a980e046SJonathan Cameron 		indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1382bc2b7dabSGregor Boirie 	if (clk)
1383bc2b7dabSGregor Boirie 		indio_dev->chan_attr_group.attrs[attrn++] = clk;
1384a980e046SJonathan Cameron 
1385a980e046SJonathan Cameron 	indio_dev->groups[indio_dev->groupcounter++] =
1386a980e046SJonathan Cameron 		&indio_dev->chan_attr_group;
1387a980e046SJonathan Cameron 
1388a980e046SJonathan Cameron 	return 0;
1389a980e046SJonathan Cameron 
1390a980e046SJonathan Cameron error_clear_attrs:
139184088ebdSLars-Peter Clausen 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1392a980e046SJonathan Cameron 
1393a980e046SJonathan Cameron 	return ret;
1394a980e046SJonathan Cameron }
1395a980e046SJonathan Cameron 
1396a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1397a980e046SJonathan Cameron {
1398a980e046SJonathan Cameron 
139984088ebdSLars-Peter Clausen 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1400a980e046SJonathan Cameron 	kfree(indio_dev->chan_attr_group.attrs);
1401c1b03ab5SMartin Fuzzey 	indio_dev->chan_attr_group.attrs = NULL;
1402a980e046SJonathan Cameron }
1403a980e046SJonathan Cameron 
1404a980e046SJonathan Cameron static void iio_dev_release(struct device *device)
1405a980e046SJonathan Cameron {
1406e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(device);
1407d89e119aSBenjamin Gaignard 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1408a980e046SJonathan Cameron 		iio_device_unregister_trigger_consumer(indio_dev);
1409a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1410a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
1411e407fd65SLars-Peter Clausen 
14129e69c935SLars-Peter Clausen 	iio_buffer_put(indio_dev->buffer);
14139e69c935SLars-Peter Clausen 
1414e407fd65SLars-Peter Clausen 	ida_simple_remove(&iio_ida, indio_dev->id);
1415e407fd65SLars-Peter Clausen 	kfree(indio_dev);
1416a980e046SJonathan Cameron }
1417a980e046SJonathan Cameron 
141817d82b47SGuenter Roeck struct device_type iio_device_type = {
1419a980e046SJonathan Cameron 	.name = "iio_device",
1420a980e046SJonathan Cameron 	.release = iio_dev_release,
1421a980e046SJonathan Cameron };
1422a980e046SJonathan Cameron 
1423a7e57dceSSachin Kamat /**
1424a7e57dceSSachin Kamat  * iio_device_alloc() - allocate an iio_dev from a driver
1425a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1426a7e57dceSSachin Kamat  **/
14277cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv)
1428a980e046SJonathan Cameron {
1429a980e046SJonathan Cameron 	struct iio_dev *dev;
1430a980e046SJonathan Cameron 	size_t alloc_size;
1431a980e046SJonathan Cameron 
1432a980e046SJonathan Cameron 	alloc_size = sizeof(struct iio_dev);
1433a980e046SJonathan Cameron 	if (sizeof_priv) {
1434a980e046SJonathan Cameron 		alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1435a980e046SJonathan Cameron 		alloc_size += sizeof_priv;
1436a980e046SJonathan Cameron 	}
1437a980e046SJonathan Cameron 	/* ensure 32-byte alignment of whole construct ? */
1438a980e046SJonathan Cameron 	alloc_size += IIO_ALIGN - 1;
1439a980e046SJonathan Cameron 
1440a980e046SJonathan Cameron 	dev = kzalloc(alloc_size, GFP_KERNEL);
1441a980e046SJonathan Cameron 
1442a980e046SJonathan Cameron 	if (dev) {
1443a980e046SJonathan Cameron 		dev->dev.groups = dev->groups;
144417d82b47SGuenter Roeck 		dev->dev.type = &iio_device_type;
1445a980e046SJonathan Cameron 		dev->dev.bus = &iio_bus_type;
1446a980e046SJonathan Cameron 		device_initialize(&dev->dev);
1447a980e046SJonathan Cameron 		dev_set_drvdata(&dev->dev, (void *)dev);
1448a980e046SJonathan Cameron 		mutex_init(&dev->mlock);
1449a980e046SJonathan Cameron 		mutex_init(&dev->info_exist_lock);
1450e407fd65SLars-Peter Clausen 		INIT_LIST_HEAD(&dev->channel_attr_list);
1451a980e046SJonathan Cameron 
1452a980e046SJonathan Cameron 		dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1453a980e046SJonathan Cameron 		if (dev->id < 0) {
1454a980e046SJonathan Cameron 			/* cannot use a dev_err as the name isn't available */
14553176dd5dSSachin Kamat 			pr_err("failed to get device id\n");
1456a980e046SJonathan Cameron 			kfree(dev);
1457a980e046SJonathan Cameron 			return NULL;
1458a980e046SJonathan Cameron 		}
1459a980e046SJonathan Cameron 		dev_set_name(&dev->dev, "iio:device%d", dev->id);
146084b36ce5SJonathan Cameron 		INIT_LIST_HEAD(&dev->buffer_list);
1461a980e046SJonathan Cameron 	}
1462a980e046SJonathan Cameron 
1463a980e046SJonathan Cameron 	return dev;
1464a980e046SJonathan Cameron }
14657cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc);
1466a980e046SJonathan Cameron 
1467a7e57dceSSachin Kamat /**
1468a7e57dceSSachin Kamat  * iio_device_free() - free an iio_dev from a driver
1469a7e57dceSSachin Kamat  * @dev:		the iio_dev associated with the device
1470a7e57dceSSachin Kamat  **/
14717cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev)
1472a980e046SJonathan Cameron {
1473e407fd65SLars-Peter Clausen 	if (dev)
1474e407fd65SLars-Peter Clausen 		put_device(&dev->dev);
1475a980e046SJonathan Cameron }
14767cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free);
1477a980e046SJonathan Cameron 
14789dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res)
14799dabaf5eSGrygorii Strashko {
14809dabaf5eSGrygorii Strashko 	iio_device_free(*(struct iio_dev **)res);
14819dabaf5eSGrygorii Strashko }
14829dabaf5eSGrygorii Strashko 
148370e48348SGregor Boirie int devm_iio_device_match(struct device *dev, void *res, void *data)
14849dabaf5eSGrygorii Strashko {
14859dabaf5eSGrygorii Strashko 	struct iio_dev **r = res;
14869dabaf5eSGrygorii Strashko 	if (!r || !*r) {
14879dabaf5eSGrygorii Strashko 		WARN_ON(!r || !*r);
14889dabaf5eSGrygorii Strashko 		return 0;
14899dabaf5eSGrygorii Strashko 	}
14909dabaf5eSGrygorii Strashko 	return *r == data;
14919dabaf5eSGrygorii Strashko }
149270e48348SGregor Boirie EXPORT_SYMBOL_GPL(devm_iio_device_match);
14939dabaf5eSGrygorii Strashko 
1494a7e57dceSSachin Kamat /**
1495a7e57dceSSachin Kamat  * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1496a7e57dceSSachin Kamat  * @dev:		Device to allocate iio_dev for
1497a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1498a7e57dceSSachin Kamat  *
1499a7e57dceSSachin Kamat  * Managed iio_device_alloc. iio_dev allocated with this function is
1500a7e57dceSSachin Kamat  * automatically freed on driver detach.
1501a7e57dceSSachin Kamat  *
1502a7e57dceSSachin Kamat  * If an iio_dev allocated with this function needs to be freed separately,
1503a7e57dceSSachin Kamat  * devm_iio_device_free() must be used.
1504a7e57dceSSachin Kamat  *
1505a7e57dceSSachin Kamat  * RETURNS:
1506a7e57dceSSachin Kamat  * Pointer to allocated iio_dev on success, NULL on failure.
1507a7e57dceSSachin Kamat  */
15089dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
15099dabaf5eSGrygorii Strashko {
15109dabaf5eSGrygorii Strashko 	struct iio_dev **ptr, *iio_dev;
15119dabaf5eSGrygorii Strashko 
15129dabaf5eSGrygorii Strashko 	ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
15139dabaf5eSGrygorii Strashko 			   GFP_KERNEL);
15149dabaf5eSGrygorii Strashko 	if (!ptr)
15159dabaf5eSGrygorii Strashko 		return NULL;
15169dabaf5eSGrygorii Strashko 
15179dabaf5eSGrygorii Strashko 	iio_dev = iio_device_alloc(sizeof_priv);
15189dabaf5eSGrygorii Strashko 	if (iio_dev) {
15199dabaf5eSGrygorii Strashko 		*ptr = iio_dev;
15209dabaf5eSGrygorii Strashko 		devres_add(dev, ptr);
15219dabaf5eSGrygorii Strashko 	} else {
15229dabaf5eSGrygorii Strashko 		devres_free(ptr);
15239dabaf5eSGrygorii Strashko 	}
15249dabaf5eSGrygorii Strashko 
15259dabaf5eSGrygorii Strashko 	return iio_dev;
15269dabaf5eSGrygorii Strashko }
15279dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
15289dabaf5eSGrygorii Strashko 
1529a7e57dceSSachin Kamat /**
1530a7e57dceSSachin Kamat  * devm_iio_device_free - Resource-managed iio_device_free()
1531a7e57dceSSachin Kamat  * @dev:		Device this iio_dev belongs to
1532a7e57dceSSachin Kamat  * @iio_dev:		the iio_dev associated with the device
1533a7e57dceSSachin Kamat  *
1534a7e57dceSSachin Kamat  * Free iio_dev allocated with devm_iio_device_alloc().
1535a7e57dceSSachin Kamat  */
15369dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
15379dabaf5eSGrygorii Strashko {
15389dabaf5eSGrygorii Strashko 	int rc;
15399dabaf5eSGrygorii Strashko 
15409dabaf5eSGrygorii Strashko 	rc = devres_release(dev, devm_iio_device_release,
15419dabaf5eSGrygorii Strashko 			    devm_iio_device_match, iio_dev);
15429dabaf5eSGrygorii Strashko 	WARN_ON(rc);
15439dabaf5eSGrygorii Strashko }
15449dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free);
15459dabaf5eSGrygorii Strashko 
1546a980e046SJonathan Cameron /**
1547a980e046SJonathan Cameron  * iio_chrdev_open() - chrdev file open for buffer access and ioctls
15482498dcf6SCristina Opriceana  * @inode:	Inode structure for identifying the device in the file system
15492498dcf6SCristina Opriceana  * @filp:	File structure for iio device used to keep and later access
15502498dcf6SCristina Opriceana  *		private data
15512498dcf6SCristina Opriceana  *
15522498dcf6SCristina Opriceana  * Return: 0 on success or -EBUSY if the device is already opened
1553a980e046SJonathan Cameron  **/
1554a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp)
1555a980e046SJonathan Cameron {
1556a980e046SJonathan Cameron 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1557a980e046SJonathan Cameron 						struct iio_dev, chrdev);
1558a980e046SJonathan Cameron 
1559a980e046SJonathan Cameron 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1560a980e046SJonathan Cameron 		return -EBUSY;
1561a980e046SJonathan Cameron 
1562cadc2125SLars-Peter Clausen 	iio_device_get(indio_dev);
1563cadc2125SLars-Peter Clausen 
1564a980e046SJonathan Cameron 	filp->private_data = indio_dev;
1565a980e046SJonathan Cameron 
1566a980e046SJonathan Cameron 	return 0;
1567a980e046SJonathan Cameron }
1568a980e046SJonathan Cameron 
1569a980e046SJonathan Cameron /**
1570a980e046SJonathan Cameron  * iio_chrdev_release() - chrdev file close buffer access and ioctls
15712498dcf6SCristina Opriceana  * @inode:	Inode structure pointer for the char device
15722498dcf6SCristina Opriceana  * @filp:	File structure pointer for the char device
15732498dcf6SCristina Opriceana  *
15742498dcf6SCristina Opriceana  * Return: 0 for successful release
15752498dcf6SCristina Opriceana  */
1576a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp)
1577a980e046SJonathan Cameron {
1578a980e046SJonathan Cameron 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1579a980e046SJonathan Cameron 						struct iio_dev, chrdev);
1580a980e046SJonathan Cameron 	clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1581cadc2125SLars-Peter Clausen 	iio_device_put(indio_dev);
1582cadc2125SLars-Peter Clausen 
1583a980e046SJonathan Cameron 	return 0;
1584a980e046SJonathan Cameron }
1585a980e046SJonathan Cameron 
1586a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually
1587a980e046SJonathan Cameron  * event related */
1588a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1589a980e046SJonathan Cameron {
1590a980e046SJonathan Cameron 	struct iio_dev *indio_dev = filp->private_data;
1591a980e046SJonathan Cameron 	int __user *ip = (int __user *)arg;
1592a980e046SJonathan Cameron 	int fd;
1593a980e046SJonathan Cameron 
1594f18e7a06SLars-Peter Clausen 	if (!indio_dev->info)
1595f18e7a06SLars-Peter Clausen 		return -ENODEV;
1596f18e7a06SLars-Peter Clausen 
1597a980e046SJonathan Cameron 	if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1598a980e046SJonathan Cameron 		fd = iio_event_getfd(indio_dev);
15993f9059b7SLinus Walleij 		if (fd < 0)
16003f9059b7SLinus Walleij 			return fd;
1601a980e046SJonathan Cameron 		if (copy_to_user(ip, &fd, sizeof(fd)))
1602a980e046SJonathan Cameron 			return -EFAULT;
1603a980e046SJonathan Cameron 		return 0;
1604a980e046SJonathan Cameron 	}
1605a980e046SJonathan Cameron 	return -EINVAL;
1606a980e046SJonathan Cameron }
1607a980e046SJonathan Cameron 
1608a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = {
1609a980e046SJonathan Cameron 	.read = iio_buffer_read_first_n_outer_addr,
1610a980e046SJonathan Cameron 	.release = iio_chrdev_release,
1611a980e046SJonathan Cameron 	.open = iio_chrdev_open,
1612a980e046SJonathan Cameron 	.poll = iio_buffer_poll_addr,
1613a980e046SJonathan Cameron 	.owner = THIS_MODULE,
1614a980e046SJonathan Cameron 	.llseek = noop_llseek,
1615a980e046SJonathan Cameron 	.unlocked_ioctl = iio_ioctl,
1616a980e046SJonathan Cameron 	.compat_ioctl = iio_ioctl,
1617a980e046SJonathan Cameron };
1618a980e046SJonathan Cameron 
16198f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
16208f5d8727SVlad Dogaru {
16218f5d8727SVlad Dogaru 	int i, j;
16228f5d8727SVlad Dogaru 	const struct iio_chan_spec *channels = indio_dev->channels;
16238f5d8727SVlad Dogaru 
16248f5d8727SVlad Dogaru 	if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
16258f5d8727SVlad Dogaru 		return 0;
16268f5d8727SVlad Dogaru 
16278f5d8727SVlad Dogaru 	for (i = 0; i < indio_dev->num_channels - 1; i++) {
16288f5d8727SVlad Dogaru 		if (channels[i].scan_index < 0)
16298f5d8727SVlad Dogaru 			continue;
16308f5d8727SVlad Dogaru 		for (j = i + 1; j < indio_dev->num_channels; j++)
16318f5d8727SVlad Dogaru 			if (channels[i].scan_index == channels[j].scan_index) {
16328f5d8727SVlad Dogaru 				dev_err(&indio_dev->dev,
16338f5d8727SVlad Dogaru 					"Duplicate scan index %d\n",
16348f5d8727SVlad Dogaru 					channels[i].scan_index);
16358f5d8727SVlad Dogaru 				return -EINVAL;
16368f5d8727SVlad Dogaru 			}
16378f5d8727SVlad Dogaru 	}
16388f5d8727SVlad Dogaru 
16398f5d8727SVlad Dogaru 	return 0;
16408f5d8727SVlad Dogaru }
16418f5d8727SVlad Dogaru 
1642a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1643a980e046SJonathan Cameron 
164463b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1645a980e046SJonathan Cameron {
1646a980e046SJonathan Cameron 	int ret;
1647a980e046SJonathan Cameron 
164863b19547SJonathan Cameron 	indio_dev->driver_module = this_mod;
164917d82b47SGuenter Roeck 	/* If the calling driver did not initialize of_node, do it here */
165017d82b47SGuenter Roeck 	if (!indio_dev->dev.of_node && indio_dev->dev.parent)
165117d82b47SGuenter Roeck 		indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
165217d82b47SGuenter Roeck 
16538f5d8727SVlad Dogaru 	ret = iio_check_unique_scan_index(indio_dev);
16548f5d8727SVlad Dogaru 	if (ret < 0)
16558f5d8727SVlad Dogaru 		return ret;
16568f5d8727SVlad Dogaru 
1657d45ebf1fSAndi Shyti 	if (!indio_dev->info)
1658d45ebf1fSAndi Shyti 		return -EINVAL;
1659d45ebf1fSAndi Shyti 
1660a980e046SJonathan Cameron 	/* configure elements for the chrdev */
1661a980e046SJonathan Cameron 	indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1662a980e046SJonathan Cameron 
1663*8915aacaSGreg Kroah-Hartman 	iio_device_register_debugfs(indio_dev);
16643e1b6c95SLars-Peter Clausen 
16653e1b6c95SLars-Peter Clausen 	ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
16663e1b6c95SLars-Peter Clausen 	if (ret) {
16673e1b6c95SLars-Peter Clausen 		dev_err(indio_dev->dev.parent,
16683e1b6c95SLars-Peter Clausen 			"Failed to create buffer sysfs interfaces\n");
16693e1b6c95SLars-Peter Clausen 		goto error_unreg_debugfs;
16703e1b6c95SLars-Peter Clausen 	}
16713e1b6c95SLars-Peter Clausen 
1672a980e046SJonathan Cameron 	ret = iio_device_register_sysfs(indio_dev);
1673a980e046SJonathan Cameron 	if (ret) {
1674a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1675a980e046SJonathan Cameron 			"Failed to register sysfs interfaces\n");
16763e1b6c95SLars-Peter Clausen 		goto error_buffer_free_sysfs;
1677a980e046SJonathan Cameron 	}
1678a980e046SJonathan Cameron 	ret = iio_device_register_eventset(indio_dev);
1679a980e046SJonathan Cameron 	if (ret) {
1680a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1681a980e046SJonathan Cameron 			"Failed to register event set\n");
1682a980e046SJonathan Cameron 		goto error_free_sysfs;
1683a980e046SJonathan Cameron 	}
1684d89e119aSBenjamin Gaignard 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1685a980e046SJonathan Cameron 		iio_device_register_trigger_consumer(indio_dev);
1686a980e046SJonathan Cameron 
1687a980e046SJonathan Cameron 	if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1688a980e046SJonathan Cameron 		indio_dev->setup_ops == NULL)
1689a980e046SJonathan Cameron 		indio_dev->setup_ops = &noop_ring_setup_ops;
1690a980e046SJonathan Cameron 
1691a980e046SJonathan Cameron 	cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
169263b19547SJonathan Cameron 
169363b19547SJonathan Cameron 	indio_dev->chrdev.owner = this_mod;
169438923911SLogan Gunthorpe 
169538923911SLogan Gunthorpe 	ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev);
1696a980e046SJonathan Cameron 	if (ret < 0)
16970d5b7daeSLars-Peter Clausen 		goto error_unreg_eventset;
1698a980e046SJonathan Cameron 
16990d5b7daeSLars-Peter Clausen 	return 0;
170038923911SLogan Gunthorpe 
1701a980e046SJonathan Cameron error_unreg_eventset:
1702a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1703a980e046SJonathan Cameron error_free_sysfs:
1704a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
17053e1b6c95SLars-Peter Clausen error_buffer_free_sysfs:
17063e1b6c95SLars-Peter Clausen 	iio_buffer_free_sysfs_and_mask(indio_dev);
1707a980e046SJonathan Cameron error_unreg_debugfs:
1708a980e046SJonathan Cameron 	iio_device_unregister_debugfs(indio_dev);
1709a980e046SJonathan Cameron 	return ret;
1710a980e046SJonathan Cameron }
171163b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register);
1712a980e046SJonathan Cameron 
1713a7e57dceSSachin Kamat /**
1714a7e57dceSSachin Kamat  * iio_device_unregister() - unregister a device from the IIO subsystem
1715a7e57dceSSachin Kamat  * @indio_dev:		Device structure representing the device.
1716a7e57dceSSachin Kamat  **/
1717a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev)
1718a980e046SJonathan Cameron {
171938923911SLogan Gunthorpe 	cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
1720a87c82e4SLars-Peter Clausen 
17217f75591fSFabrice Gasnier 	mutex_lock(&indio_dev->info_exist_lock);
17227f75591fSFabrice Gasnier 
1723bc4c9612SLars-Peter Clausen 	iio_device_unregister_debugfs(indio_dev);
17240d5b7daeSLars-Peter Clausen 
1725a87c82e4SLars-Peter Clausen 	iio_disable_all_buffers(indio_dev);
1726a87c82e4SLars-Peter Clausen 
1727a980e046SJonathan Cameron 	indio_dev->info = NULL;
1728d2f0a48fSLars-Peter Clausen 
1729d2f0a48fSLars-Peter Clausen 	iio_device_wakeup_eventset(indio_dev);
1730d2f0a48fSLars-Peter Clausen 	iio_buffer_wakeup_poll(indio_dev);
1731d2f0a48fSLars-Peter Clausen 
1732a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->info_exist_lock);
17333e1b6c95SLars-Peter Clausen 
17343e1b6c95SLars-Peter Clausen 	iio_buffer_free_sysfs_and_mask(indio_dev);
1735a980e046SJonathan Cameron }
1736a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister);
17378caa07c0SSachin Kamat 
17388caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res)
17398caa07c0SSachin Kamat {
17408caa07c0SSachin Kamat 	iio_device_unregister(*(struct iio_dev **)res);
17418caa07c0SSachin Kamat }
17428caa07c0SSachin Kamat 
174363b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
174463b19547SJonathan Cameron 			       struct module *this_mod)
17458caa07c0SSachin Kamat {
17468caa07c0SSachin Kamat 	struct iio_dev **ptr;
17478caa07c0SSachin Kamat 	int ret;
17488caa07c0SSachin Kamat 
17498caa07c0SSachin Kamat 	ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
17508caa07c0SSachin Kamat 	if (!ptr)
17518caa07c0SSachin Kamat 		return -ENOMEM;
17528caa07c0SSachin Kamat 
17538caa07c0SSachin Kamat 	*ptr = indio_dev;
175463b19547SJonathan Cameron 	ret = __iio_device_register(indio_dev, this_mod);
17558caa07c0SSachin Kamat 	if (!ret)
17568caa07c0SSachin Kamat 		devres_add(dev, ptr);
17578caa07c0SSachin Kamat 	else
17588caa07c0SSachin Kamat 		devres_free(ptr);
17598caa07c0SSachin Kamat 
17608caa07c0SSachin Kamat 	return ret;
17618caa07c0SSachin Kamat }
176263b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register);
17638caa07c0SSachin Kamat 
17648caa07c0SSachin Kamat /**
17658caa07c0SSachin Kamat  * devm_iio_device_unregister - Resource-managed iio_device_unregister()
17668caa07c0SSachin Kamat  * @dev:	Device this iio_dev belongs to
17678caa07c0SSachin Kamat  * @indio_dev:	the iio_dev associated with the device
17688caa07c0SSachin Kamat  *
17698caa07c0SSachin Kamat  * Unregister iio_dev registered with devm_iio_device_register().
17708caa07c0SSachin Kamat  */
17718caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
17728caa07c0SSachin Kamat {
17738caa07c0SSachin Kamat 	int rc;
17748caa07c0SSachin Kamat 
17758caa07c0SSachin Kamat 	rc = devres_release(dev, devm_iio_device_unreg,
17768caa07c0SSachin Kamat 			    devm_iio_device_match, indio_dev);
17778caa07c0SSachin Kamat 	WARN_ON(rc);
17788caa07c0SSachin Kamat }
17798caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
17808caa07c0SSachin Kamat 
178108a33805SAlison Schofield /**
178208a33805SAlison Schofield  * iio_device_claim_direct_mode - Keep device in direct mode
178308a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
178408a33805SAlison Schofield  *
178508a33805SAlison Schofield  * If the device is in direct mode it is guaranteed to stay
178608a33805SAlison Schofield  * that way until iio_device_release_direct_mode() is called.
178708a33805SAlison Schofield  *
178808a33805SAlison Schofield  * Use with iio_device_release_direct_mode()
178908a33805SAlison Schofield  *
179008a33805SAlison Schofield  * Returns: 0 on success, -EBUSY on failure
179108a33805SAlison Schofield  */
179208a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
179308a33805SAlison Schofield {
179408a33805SAlison Schofield 	mutex_lock(&indio_dev->mlock);
179508a33805SAlison Schofield 
179608a33805SAlison Schofield 	if (iio_buffer_enabled(indio_dev)) {
179708a33805SAlison Schofield 		mutex_unlock(&indio_dev->mlock);
179808a33805SAlison Schofield 		return -EBUSY;
179908a33805SAlison Schofield 	}
180008a33805SAlison Schofield 	return 0;
180108a33805SAlison Schofield }
180208a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
180308a33805SAlison Schofield 
180408a33805SAlison Schofield /**
180508a33805SAlison Schofield  * iio_device_release_direct_mode - releases claim on direct mode
180608a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
180708a33805SAlison Schofield  *
180808a33805SAlison Schofield  * Release the claim. Device is no longer guaranteed to stay
180908a33805SAlison Schofield  * in direct mode.
181008a33805SAlison Schofield  *
181108a33805SAlison Schofield  * Use with iio_device_claim_direct_mode()
181208a33805SAlison Schofield  */
181308a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev)
181408a33805SAlison Schofield {
181508a33805SAlison Schofield 	mutex_unlock(&indio_dev->mlock);
181608a33805SAlison Schofield }
181708a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
181808a33805SAlison Schofield 
1819a980e046SJonathan Cameron subsys_initcall(iio_init);
1820a980e046SJonathan Cameron module_exit(iio_exit);
1821a980e046SJonathan Cameron 
1822c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1823a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core");
1824a980e046SJonathan Cameron MODULE_LICENSE("GPL");
1825