xref: /linux/drivers/iio/industrialio-core.c (revision 51239600074bc9979b0a0e83b72c726d7dcc3132)
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>
22a980e046SJonathan Cameron #include <linux/sched.h>
23a980e046SJonathan Cameron #include <linux/wait.h>
24a980e046SJonathan Cameron #include <linux/cdev.h>
25a980e046SJonathan Cameron #include <linux/slab.h>
26a980e046SJonathan Cameron #include <linux/anon_inodes.h>
27a980e046SJonathan Cameron #include <linux/debugfs.h>
2808a33805SAlison Schofield #include <linux/mutex.h>
29a980e046SJonathan Cameron #include <linux/iio/iio.h>
30a980e046SJonathan Cameron #include "iio_core.h"
31a980e046SJonathan Cameron #include "iio_core_trigger.h"
32a980e046SJonathan Cameron #include <linux/iio/sysfs.h>
33a980e046SJonathan Cameron #include <linux/iio/events.h>
349e69c935SLars-Peter Clausen #include <linux/iio/buffer.h>
35a980e046SJonathan Cameron 
36a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */
37a980e046SJonathan Cameron static DEFINE_IDA(iio_ida);
38a980e046SJonathan Cameron 
39a980e046SJonathan Cameron static dev_t iio_devt;
40a980e046SJonathan Cameron 
41a980e046SJonathan Cameron #define IIO_DEV_MAX 256
42a980e046SJonathan Cameron struct bus_type iio_bus_type = {
43a980e046SJonathan Cameron 	.name = "iio",
44a980e046SJonathan Cameron };
45a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type);
46a980e046SJonathan Cameron 
47a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry;
48a980e046SJonathan Cameron 
49a980e046SJonathan Cameron static const char * const iio_direction[] = {
50a980e046SJonathan Cameron 	[0] = "in",
51a980e046SJonathan Cameron 	[1] = "out",
52a980e046SJonathan Cameron };
53a980e046SJonathan Cameron 
54a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = {
55a980e046SJonathan Cameron 	[IIO_VOLTAGE] = "voltage",
56a980e046SJonathan Cameron 	[IIO_CURRENT] = "current",
57a980e046SJonathan Cameron 	[IIO_POWER] = "power",
58a980e046SJonathan Cameron 	[IIO_ACCEL] = "accel",
59a980e046SJonathan Cameron 	[IIO_ANGL_VEL] = "anglvel",
60a980e046SJonathan Cameron 	[IIO_MAGN] = "magn",
61a980e046SJonathan Cameron 	[IIO_LIGHT] = "illuminance",
62a980e046SJonathan Cameron 	[IIO_INTENSITY] = "intensity",
63a980e046SJonathan Cameron 	[IIO_PROXIMITY] = "proximity",
64a980e046SJonathan Cameron 	[IIO_TEMP] = "temp",
65a980e046SJonathan Cameron 	[IIO_INCLI] = "incli",
66a980e046SJonathan Cameron 	[IIO_ROT] = "rot",
67a980e046SJonathan Cameron 	[IIO_ANGL] = "angl",
68a980e046SJonathan Cameron 	[IIO_TIMESTAMP] = "timestamp",
69a980e046SJonathan Cameron 	[IIO_CAPACITANCE] = "capacitance",
70a6b12855SMichael Hennerich 	[IIO_ALTVOLTAGE] = "altvoltage",
7121cd1fabSJon Brenner 	[IIO_CCT] = "cct",
72c4f0c693SLars-Peter Clausen 	[IIO_PRESSURE] = "pressure",
73ac216aa2SHarald Geyer 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
7455aebeb9SDaniel Baluta 	[IIO_ACTIVITY] = "activity",
75a88bfe78SIrina Tirdea 	[IIO_STEPS] = "steps",
7672c66644SIrina Tirdea 	[IIO_ENERGY] = "energy",
77cc3c9eecSIrina Tirdea 	[IIO_DISTANCE] = "distance",
785a1a9329SIrina Tirdea 	[IIO_VELOCITY] = "velocity",
798ff6b3bcSMatt Ranostay 	[IIO_CONCENTRATION] = "concentration",
80d38d5469SMatt Ranostay 	[IIO_RESISTANCE] = "resistance",
81ecb3a7ccSMatt Ranostay 	[IIO_PH] = "ph",
82d409404cSPeter Meerwald-Stadler 	[IIO_UVINDEX] = "uvindex",
834b9d2090SMatt Ranostay 	[IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
841a8f324aSWilliam Breathitt Gray 	[IIO_COUNT] = "count",
851a8f324aSWilliam Breathitt Gray 	[IIO_INDEX] = "index",
86a980e046SJonathan Cameron };
87a980e046SJonathan Cameron 
88a980e046SJonathan Cameron static const char * const iio_modifier_names[] = {
89a980e046SJonathan Cameron 	[IIO_MOD_X] = "x",
90a980e046SJonathan Cameron 	[IIO_MOD_Y] = "y",
91a980e046SJonathan Cameron 	[IIO_MOD_Z] = "z",
924b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y] = "x&y",
934b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Z] = "x&z",
944b8d8015SPeter Meerwald 	[IIO_MOD_Y_AND_Z] = "y&z",
954b8d8015SPeter Meerwald 	[IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
964b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y] = "x|y",
974b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Z] = "x|z",
984b8d8015SPeter Meerwald 	[IIO_MOD_Y_OR_Z] = "y|z",
994b8d8015SPeter Meerwald 	[IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
1008f5879b2SJonathan Cameron 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
101cf82cb81SJonathan Cameron 	[IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
102a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_BOTH] = "both",
103a980e046SJonathan Cameron 	[IIO_MOD_LIGHT_IR] = "ir",
10421cd1fabSJon Brenner 	[IIO_MOD_LIGHT_CLEAR] = "clear",
10521cd1fabSJon Brenner 	[IIO_MOD_LIGHT_RED] = "red",
10621cd1fabSJon Brenner 	[IIO_MOD_LIGHT_GREEN] = "green",
10721cd1fabSJon Brenner 	[IIO_MOD_LIGHT_BLUE] = "blue",
1082c5ff1f9SPeter Meerwald-Stadler 	[IIO_MOD_LIGHT_UV] = "uv",
1095082f405SSrinivas Pandruvada 	[IIO_MOD_QUATERNION] = "quaternion",
110638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_AMBIENT] = "ambient",
111638b43b3SPeter Meerwald 	[IIO_MOD_TEMP_OBJECT] = "object",
11211b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
11311b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE] = "from_north_true",
11411b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
11511b8ddabSReyad Attiyat 	[IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
11655aebeb9SDaniel Baluta 	[IIO_MOD_RUNNING] = "running",
11755aebeb9SDaniel Baluta 	[IIO_MOD_JOGGING] = "jogging",
11855aebeb9SDaniel Baluta 	[IIO_MOD_WALKING] = "walking",
11955aebeb9SDaniel Baluta 	[IIO_MOD_STILL] = "still",
1205a1a9329SIrina Tirdea 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
1211ce87f21SLars-Peter Clausen 	[IIO_MOD_I] = "i",
1221ce87f21SLars-Peter Clausen 	[IIO_MOD_Q] = "q",
1238ff6b3bcSMatt Ranostay 	[IIO_MOD_CO2] = "co2",
1248ff6b3bcSMatt Ranostay 	[IIO_MOD_VOC] = "voc",
125a980e046SJonathan Cameron };
126a980e046SJonathan Cameron 
127a980e046SJonathan Cameron /* relies on pairs of these shared then separate */
128a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = {
129a980e046SJonathan Cameron 	[IIO_CHAN_INFO_RAW] = "raw",
130a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PROCESSED] = "input",
131a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SCALE] = "scale",
132a980e046SJonathan Cameron 	[IIO_CHAN_INFO_OFFSET] = "offset",
133a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
134a980e046SJonathan Cameron 	[IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
135a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK] = "peak_raw",
136a980e046SJonathan Cameron 	[IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
137a980e046SJonathan Cameron 	[IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
138a980e046SJonathan Cameron 	[IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
139a980e046SJonathan Cameron 	[IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
140a980e046SJonathan Cameron 	= "filter_low_pass_3db_frequency",
1413f7f642bSMartin Fuzzey 	[IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
1423f7f642bSMartin Fuzzey 	= "filter_high_pass_3db_frequency",
143a980e046SJonathan Cameron 	[IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
144a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_FREQUENCY] = "frequency",
145a6b12855SMichael Hennerich 	[IIO_CHAN_INFO_PHASE] = "phase",
146b65d6212SMichael Hennerich 	[IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
1477c9ab035Ssrinivas pandruvada 	[IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
148899d90bdSPeter Meerwald 	[IIO_CHAN_INFO_INT_TIME] = "integration_time",
149a88bfe78SIrina Tirdea 	[IIO_CHAN_INFO_ENABLE] = "en",
150bcdf28fbSIrina Tirdea 	[IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
151d37f6836SIrina Tirdea 	[IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
1522f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
1532f0ecb7cSIrina Tirdea 	[IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
154c8a85854SVianney le Clément de Saint-Marcq 	[IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
155faaa4495SIrina Tirdea 	[IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
156a980e046SJonathan Cameron };
157a980e046SJonathan Cameron 
158a7e57dceSSachin Kamat /**
159a7e57dceSSachin Kamat  * iio_find_channel_from_si() - get channel from its scan index
160a7e57dceSSachin Kamat  * @indio_dev:		device
161a7e57dceSSachin Kamat  * @si:			scan index to match
162a7e57dceSSachin Kamat  */
163a980e046SJonathan Cameron const struct iio_chan_spec
164a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
165a980e046SJonathan Cameron {
166a980e046SJonathan Cameron 	int i;
167a980e046SJonathan Cameron 
168a980e046SJonathan Cameron 	for (i = 0; i < indio_dev->num_channels; i++)
169a980e046SJonathan Cameron 		if (indio_dev->channels[i].scan_index == si)
170a980e046SJonathan Cameron 			return &indio_dev->channels[i];
171a980e046SJonathan Cameron 	return NULL;
172a980e046SJonathan Cameron }
173a980e046SJonathan Cameron 
174a980e046SJonathan Cameron /* This turns up an awful lot */
175a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev,
176a980e046SJonathan Cameron 			    struct device_attribute *attr,
177a980e046SJonathan Cameron 			    char *buf)
178a980e046SJonathan Cameron {
179a980e046SJonathan Cameron 	return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
180a980e046SJonathan Cameron }
181a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr);
182a980e046SJonathan Cameron 
183bc2b7dabSGregor Boirie static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
184bc2b7dabSGregor Boirie {
185bc2b7dabSGregor Boirie 	int ret;
186bc2b7dabSGregor Boirie 	const struct iio_event_interface *ev_int = indio_dev->event_interface;
187bc2b7dabSGregor Boirie 
188bc2b7dabSGregor Boirie 	ret = mutex_lock_interruptible(&indio_dev->mlock);
189bc2b7dabSGregor Boirie 	if (ret)
190bc2b7dabSGregor Boirie 		return ret;
191bc2b7dabSGregor Boirie 	if ((ev_int && iio_event_enabled(ev_int)) ||
192bc2b7dabSGregor Boirie 	    iio_buffer_enabled(indio_dev)) {
193bc2b7dabSGregor Boirie 		mutex_unlock(&indio_dev->mlock);
194bc2b7dabSGregor Boirie 		return -EBUSY;
195bc2b7dabSGregor Boirie 	}
196bc2b7dabSGregor Boirie 	indio_dev->clock_id = clock_id;
197bc2b7dabSGregor Boirie 	mutex_unlock(&indio_dev->mlock);
198bc2b7dabSGregor Boirie 
199bc2b7dabSGregor Boirie 	return 0;
200bc2b7dabSGregor Boirie }
201bc2b7dabSGregor Boirie 
202bc2b7dabSGregor Boirie /**
203bc2b7dabSGregor Boirie  * iio_get_time_ns() - utility function to get a time stamp for events etc
204bc2b7dabSGregor Boirie  * @indio_dev: device
205bc2b7dabSGregor Boirie  */
206bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev)
207bc2b7dabSGregor Boirie {
208bc2b7dabSGregor Boirie 	struct timespec tp;
209bc2b7dabSGregor Boirie 
210bc2b7dabSGregor Boirie 	switch (iio_device_get_clock(indio_dev)) {
211bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
212bc2b7dabSGregor Boirie 		ktime_get_real_ts(&tp);
213bc2b7dabSGregor Boirie 		break;
214bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
215bc2b7dabSGregor Boirie 		ktime_get_ts(&tp);
216bc2b7dabSGregor Boirie 		break;
217bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
218bc2b7dabSGregor Boirie 		getrawmonotonic(&tp);
219bc2b7dabSGregor Boirie 		break;
220bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
221bc2b7dabSGregor Boirie 		tp = current_kernel_time();
222bc2b7dabSGregor Boirie 		break;
223bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
224bc2b7dabSGregor Boirie 		tp = get_monotonic_coarse();
225bc2b7dabSGregor Boirie 		break;
226bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
227bc2b7dabSGregor Boirie 		get_monotonic_boottime(&tp);
228bc2b7dabSGregor Boirie 		break;
229bc2b7dabSGregor Boirie 	case CLOCK_TAI:
230bc2b7dabSGregor Boirie 		timekeeping_clocktai(&tp);
231bc2b7dabSGregor Boirie 		break;
232bc2b7dabSGregor Boirie 	default:
233bc2b7dabSGregor Boirie 		BUG();
234bc2b7dabSGregor Boirie 	}
235bc2b7dabSGregor Boirie 
236bc2b7dabSGregor Boirie 	return timespec_to_ns(&tp);
237bc2b7dabSGregor Boirie }
238bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns);
239bc2b7dabSGregor Boirie 
240bc2b7dabSGregor Boirie /**
241bc2b7dabSGregor Boirie  * iio_get_time_res() - utility function to get time stamp clock resolution in
242bc2b7dabSGregor Boirie  *                      nano seconds.
243bc2b7dabSGregor Boirie  * @indio_dev: device
244bc2b7dabSGregor Boirie  */
245bc2b7dabSGregor Boirie unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
246bc2b7dabSGregor Boirie {
247bc2b7dabSGregor Boirie 	switch (iio_device_get_clock(indio_dev)) {
248bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
249bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
250bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
251bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
252bc2b7dabSGregor Boirie 	case CLOCK_TAI:
253bc2b7dabSGregor Boirie 		return hrtimer_resolution;
254bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
255bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
256bc2b7dabSGregor Boirie 		return LOW_RES_NSEC;
257bc2b7dabSGregor Boirie 	default:
258bc2b7dabSGregor Boirie 		BUG();
259bc2b7dabSGregor Boirie 	}
260bc2b7dabSGregor Boirie }
261bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_res);
262bc2b7dabSGregor Boirie 
263a980e046SJonathan Cameron static int __init iio_init(void)
264a980e046SJonathan Cameron {
265a980e046SJonathan Cameron 	int ret;
266a980e046SJonathan Cameron 
267a980e046SJonathan Cameron 	/* Register sysfs bus */
268a980e046SJonathan Cameron 	ret  = bus_register(&iio_bus_type);
269a980e046SJonathan Cameron 	if (ret < 0) {
2703176dd5dSSachin Kamat 		pr_err("could not register bus type\n");
271a980e046SJonathan Cameron 		goto error_nothing;
272a980e046SJonathan Cameron 	}
273a980e046SJonathan Cameron 
274a980e046SJonathan Cameron 	ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
275a980e046SJonathan Cameron 	if (ret < 0) {
2763176dd5dSSachin Kamat 		pr_err("failed to allocate char dev region\n");
277a980e046SJonathan Cameron 		goto error_unregister_bus_type;
278a980e046SJonathan Cameron 	}
279a980e046SJonathan Cameron 
280a980e046SJonathan Cameron 	iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
281a980e046SJonathan Cameron 
282a980e046SJonathan Cameron 	return 0;
283a980e046SJonathan Cameron 
284a980e046SJonathan Cameron error_unregister_bus_type:
285a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
286a980e046SJonathan Cameron error_nothing:
287a980e046SJonathan Cameron 	return ret;
288a980e046SJonathan Cameron }
289a980e046SJonathan Cameron 
290a980e046SJonathan Cameron static void __exit iio_exit(void)
291a980e046SJonathan Cameron {
292a980e046SJonathan Cameron 	if (iio_devt)
293a980e046SJonathan Cameron 		unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
294a980e046SJonathan Cameron 	bus_unregister(&iio_bus_type);
295a980e046SJonathan Cameron 	debugfs_remove(iio_debugfs_dentry);
296a980e046SJonathan Cameron }
297a980e046SJonathan Cameron 
298a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS)
299a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
300a980e046SJonathan Cameron 			      size_t count, loff_t *ppos)
301a980e046SJonathan Cameron {
302a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
303a980e046SJonathan Cameron 	char buf[20];
304a980e046SJonathan Cameron 	unsigned val = 0;
305a980e046SJonathan Cameron 	ssize_t len;
306a980e046SJonathan Cameron 	int ret;
307a980e046SJonathan Cameron 
308a980e046SJonathan Cameron 	ret = indio_dev->info->debugfs_reg_access(indio_dev,
309a980e046SJonathan Cameron 						  indio_dev->cached_reg_addr,
310a980e046SJonathan Cameron 						  0, &val);
311a980e046SJonathan Cameron 	if (ret)
312a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
313a980e046SJonathan Cameron 
314a980e046SJonathan Cameron 	len = snprintf(buf, sizeof(buf), "0x%X\n", val);
315a980e046SJonathan Cameron 
316a980e046SJonathan Cameron 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
317a980e046SJonathan Cameron }
318a980e046SJonathan Cameron 
319a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file,
320a980e046SJonathan Cameron 		     const char __user *userbuf, size_t count, loff_t *ppos)
321a980e046SJonathan Cameron {
322a980e046SJonathan Cameron 	struct iio_dev *indio_dev = file->private_data;
323a980e046SJonathan Cameron 	unsigned reg, val;
324a980e046SJonathan Cameron 	char buf[80];
325a980e046SJonathan Cameron 	int ret;
326a980e046SJonathan Cameron 
327a980e046SJonathan Cameron 	count = min_t(size_t, count, (sizeof(buf)-1));
328a980e046SJonathan Cameron 	if (copy_from_user(buf, userbuf, count))
329a980e046SJonathan Cameron 		return -EFAULT;
330a980e046SJonathan Cameron 
331a980e046SJonathan Cameron 	buf[count] = 0;
332a980e046SJonathan Cameron 
333a980e046SJonathan Cameron 	ret = sscanf(buf, "%i %i", &reg, &val);
334a980e046SJonathan Cameron 
335a980e046SJonathan Cameron 	switch (ret) {
336a980e046SJonathan Cameron 	case 1:
337a980e046SJonathan Cameron 		indio_dev->cached_reg_addr = reg;
338a980e046SJonathan Cameron 		break;
339a980e046SJonathan Cameron 	case 2:
340a980e046SJonathan Cameron 		indio_dev->cached_reg_addr = reg;
341a980e046SJonathan Cameron 		ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
342a980e046SJonathan Cameron 							  val, NULL);
343a980e046SJonathan Cameron 		if (ret) {
344a980e046SJonathan Cameron 			dev_err(indio_dev->dev.parent, "%s: write failed\n",
345a980e046SJonathan Cameron 				__func__);
346a980e046SJonathan Cameron 			return ret;
347a980e046SJonathan Cameron 		}
348a980e046SJonathan Cameron 		break;
349a980e046SJonathan Cameron 	default:
350a980e046SJonathan Cameron 		return -EINVAL;
351a980e046SJonathan Cameron 	}
352a980e046SJonathan Cameron 
353a980e046SJonathan Cameron 	return count;
354a980e046SJonathan Cameron }
355a980e046SJonathan Cameron 
356a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = {
3575a28c873SAxel Lin 	.open = simple_open,
358a980e046SJonathan Cameron 	.read = iio_debugfs_read_reg,
359a980e046SJonathan Cameron 	.write = iio_debugfs_write_reg,
360a980e046SJonathan Cameron };
361a980e046SJonathan Cameron 
362a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
363a980e046SJonathan Cameron {
364a980e046SJonathan Cameron 	debugfs_remove_recursive(indio_dev->debugfs_dentry);
365a980e046SJonathan Cameron }
366a980e046SJonathan Cameron 
367a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev)
368a980e046SJonathan Cameron {
369a980e046SJonathan Cameron 	struct dentry *d;
370a980e046SJonathan Cameron 
371a980e046SJonathan Cameron 	if (indio_dev->info->debugfs_reg_access == NULL)
372a980e046SJonathan Cameron 		return 0;
373a980e046SJonathan Cameron 
374abd5a2fbSAxel Lin 	if (!iio_debugfs_dentry)
375a980e046SJonathan Cameron 		return 0;
376a980e046SJonathan Cameron 
377a980e046SJonathan Cameron 	indio_dev->debugfs_dentry =
378a980e046SJonathan Cameron 		debugfs_create_dir(dev_name(&indio_dev->dev),
379a980e046SJonathan Cameron 				   iio_debugfs_dentry);
380a980e046SJonathan Cameron 	if (indio_dev->debugfs_dentry == NULL) {
381a980e046SJonathan Cameron 		dev_warn(indio_dev->dev.parent,
382a980e046SJonathan Cameron 			 "Failed to create debugfs directory\n");
383a980e046SJonathan Cameron 		return -EFAULT;
384a980e046SJonathan Cameron 	}
385a980e046SJonathan Cameron 
386a980e046SJonathan Cameron 	d = debugfs_create_file("direct_reg_access", 0644,
387a980e046SJonathan Cameron 				indio_dev->debugfs_dentry,
388a980e046SJonathan Cameron 				indio_dev, &iio_debugfs_reg_fops);
389a980e046SJonathan Cameron 	if (!d) {
390a980e046SJonathan Cameron 		iio_device_unregister_debugfs(indio_dev);
391a980e046SJonathan Cameron 		return -ENOMEM;
392a980e046SJonathan Cameron 	}
393a980e046SJonathan Cameron 
394a980e046SJonathan Cameron 	return 0;
395a980e046SJonathan Cameron }
396a980e046SJonathan Cameron #else
397a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev)
398a980e046SJonathan Cameron {
399a980e046SJonathan Cameron 	return 0;
400a980e046SJonathan Cameron }
401a980e046SJonathan Cameron 
402a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
403a980e046SJonathan Cameron {
404a980e046SJonathan Cameron }
405a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */
406a980e046SJonathan Cameron 
407a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev,
408a980e046SJonathan Cameron 				     struct device_attribute *attr,
409a980e046SJonathan Cameron 				     char *buf)
410a980e046SJonathan Cameron {
411e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
412a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
413a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
414a980e046SJonathan Cameron 
415a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
416a980e046SJonathan Cameron 
417fc6d1139SMichael Hennerich 	return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
418a980e046SJonathan Cameron }
419a980e046SJonathan Cameron 
420a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev,
421a980e046SJonathan Cameron 				     struct device_attribute *attr,
422a980e046SJonathan Cameron 				     const char *buf,
423a980e046SJonathan Cameron 					 size_t len)
424a980e046SJonathan Cameron {
425e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
426a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
427a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
428a980e046SJonathan Cameron 
429a980e046SJonathan Cameron 	ext_info = &this_attr->c->ext_info[this_attr->address];
430a980e046SJonathan Cameron 
431fc6d1139SMichael Hennerich 	return ext_info->write(indio_dev, ext_info->private,
432fc6d1139SMichael Hennerich 			       this_attr->c, buf, len);
433a980e046SJonathan Cameron }
434a980e046SJonathan Cameron 
4355212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
4365212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
4375212cc8aSLars-Peter Clausen {
4385212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4395212cc8aSLars-Peter Clausen 	unsigned int i;
4405212cc8aSLars-Peter Clausen 	size_t len = 0;
4415212cc8aSLars-Peter Clausen 
4425212cc8aSLars-Peter Clausen 	if (!e->num_items)
4435212cc8aSLars-Peter Clausen 		return 0;
4445212cc8aSLars-Peter Clausen 
4455212cc8aSLars-Peter Clausen 	for (i = 0; i < e->num_items; ++i)
44674dcd439SLars-Peter Clausen 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
4475212cc8aSLars-Peter Clausen 
4485212cc8aSLars-Peter Clausen 	/* replace last space with a newline */
4495212cc8aSLars-Peter Clausen 	buf[len - 1] = '\n';
4505212cc8aSLars-Peter Clausen 
4515212cc8aSLars-Peter Clausen 	return len;
4525212cc8aSLars-Peter Clausen }
4535212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read);
4545212cc8aSLars-Peter Clausen 
4555212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev,
4565212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
4575212cc8aSLars-Peter Clausen {
4585212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4595212cc8aSLars-Peter Clausen 	int i;
4605212cc8aSLars-Peter Clausen 
4615212cc8aSLars-Peter Clausen 	if (!e->get)
4625212cc8aSLars-Peter Clausen 		return -EINVAL;
4635212cc8aSLars-Peter Clausen 
4645212cc8aSLars-Peter Clausen 	i = e->get(indio_dev, chan);
4655212cc8aSLars-Peter Clausen 	if (i < 0)
4665212cc8aSLars-Peter Clausen 		return i;
4675212cc8aSLars-Peter Clausen 	else if (i >= e->num_items)
4685212cc8aSLars-Peter Clausen 		return -EINVAL;
4695212cc8aSLars-Peter Clausen 
470598db581SKees Cook 	return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
4715212cc8aSLars-Peter Clausen }
4725212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read);
4735212cc8aSLars-Peter Clausen 
4745212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev,
4755212cc8aSLars-Peter Clausen 	uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
4765212cc8aSLars-Peter Clausen 	size_t len)
4775212cc8aSLars-Peter Clausen {
4785212cc8aSLars-Peter Clausen 	const struct iio_enum *e = (const struct iio_enum *)priv;
4795212cc8aSLars-Peter Clausen 	unsigned int i;
4805212cc8aSLars-Peter Clausen 	int ret;
4815212cc8aSLars-Peter Clausen 
4825212cc8aSLars-Peter Clausen 	if (!e->set)
4835212cc8aSLars-Peter Clausen 		return -EINVAL;
4845212cc8aSLars-Peter Clausen 
4855212cc8aSLars-Peter Clausen 	for (i = 0; i < e->num_items; i++) {
4865212cc8aSLars-Peter Clausen 		if (sysfs_streq(buf, e->items[i]))
4875212cc8aSLars-Peter Clausen 			break;
4885212cc8aSLars-Peter Clausen 	}
4895212cc8aSLars-Peter Clausen 
4905212cc8aSLars-Peter Clausen 	if (i == e->num_items)
4915212cc8aSLars-Peter Clausen 		return -EINVAL;
4925212cc8aSLars-Peter Clausen 
4935212cc8aSLars-Peter Clausen 	ret = e->set(indio_dev, chan, i);
4945212cc8aSLars-Peter Clausen 	return ret ? ret : len;
4955212cc8aSLars-Peter Clausen }
4965212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write);
4975212cc8aSLars-Peter Clausen 
498dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = {
499dfc57732SGregor Boirie 	.rotation = {
500dfc57732SGregor Boirie 		"1", "0", "0",
501dfc57732SGregor Boirie 		"0", "1", "0",
502dfc57732SGregor Boirie 		"0", "0", "1"
503dfc57732SGregor Boirie 	}
504dfc57732SGregor Boirie };
505dfc57732SGregor Boirie 
506dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev,
507dfc57732SGregor Boirie 				    struct iio_mount_matrix *matrix)
508dfc57732SGregor Boirie {
509dfc57732SGregor Boirie 	*matrix = iio_mount_idmatrix;
510dfc57732SGregor Boirie 	dev_info(dev, "mounting matrix not found: using identity...\n");
511dfc57732SGregor Boirie 	return 0;
512dfc57732SGregor Boirie }
513dfc57732SGregor Boirie 
514dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
515dfc57732SGregor Boirie 			      const struct iio_chan_spec *chan, char *buf)
516dfc57732SGregor Boirie {
517dfc57732SGregor Boirie 	const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
518dfc57732SGregor Boirie 					      priv)(indio_dev, chan);
519dfc57732SGregor Boirie 
520dfc57732SGregor Boirie 	if (IS_ERR(mtx))
521dfc57732SGregor Boirie 		return PTR_ERR(mtx);
522dfc57732SGregor Boirie 
523dfc57732SGregor Boirie 	if (!mtx)
524dfc57732SGregor Boirie 		mtx = &iio_mount_idmatrix;
525dfc57732SGregor Boirie 
526dfc57732SGregor Boirie 	return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
527dfc57732SGregor Boirie 			mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
528dfc57732SGregor Boirie 			mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
529dfc57732SGregor Boirie 			mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
530dfc57732SGregor Boirie }
531dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
532dfc57732SGregor Boirie 
533dfc57732SGregor Boirie /**
534dfc57732SGregor Boirie  * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from
535dfc57732SGregor Boirie  *                              device-tree "mount-matrix" property
536dfc57732SGregor Boirie  * @dev:	device the mounting matrix property is assigned to
537dfc57732SGregor Boirie  * @propname:	device specific mounting matrix property name
538dfc57732SGregor Boirie  * @matrix:	where to store retrieved matrix
539dfc57732SGregor Boirie  *
540dfc57732SGregor Boirie  * If device is assigned no mounting matrix property, a default 3x3 identity
541dfc57732SGregor Boirie  * matrix will be filled in.
542dfc57732SGregor Boirie  *
543dfc57732SGregor Boirie  * Return: 0 if success, or a negative error code on failure.
544dfc57732SGregor Boirie  */
545dfc57732SGregor Boirie #ifdef CONFIG_OF
546dfc57732SGregor Boirie int of_iio_read_mount_matrix(const struct device *dev,
547dfc57732SGregor Boirie 			     const char *propname,
548dfc57732SGregor Boirie 			     struct iio_mount_matrix *matrix)
549dfc57732SGregor Boirie {
550dfc57732SGregor Boirie 	if (dev->of_node) {
551dfc57732SGregor Boirie 		int err = of_property_read_string_array(dev->of_node,
552dfc57732SGregor Boirie 				propname, matrix->rotation,
553dfc57732SGregor Boirie 				ARRAY_SIZE(iio_mount_idmatrix.rotation));
554dfc57732SGregor Boirie 
555dfc57732SGregor Boirie 		if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation))
556dfc57732SGregor Boirie 			return 0;
557dfc57732SGregor Boirie 
558dfc57732SGregor Boirie 		if (err >= 0)
559dfc57732SGregor Boirie 			/* Invalid number of matrix entries. */
560dfc57732SGregor Boirie 			return -EINVAL;
561dfc57732SGregor Boirie 
562dfc57732SGregor Boirie 		if (err != -EINVAL)
563dfc57732SGregor Boirie 			/* Invalid matrix declaration format. */
564dfc57732SGregor Boirie 			return err;
565dfc57732SGregor Boirie 	}
566dfc57732SGregor Boirie 
567dfc57732SGregor Boirie 	/* Matrix was not declared at all: fallback to identity. */
568dfc57732SGregor Boirie 	return iio_setup_mount_idmatrix(dev, matrix);
569dfc57732SGregor Boirie }
570dfc57732SGregor Boirie #else
571dfc57732SGregor Boirie int of_iio_read_mount_matrix(const struct device *dev,
572dfc57732SGregor Boirie 			     const char *propname,
573dfc57732SGregor Boirie 			     struct iio_mount_matrix *matrix)
574dfc57732SGregor Boirie {
575dfc57732SGregor Boirie 	return iio_setup_mount_idmatrix(dev, matrix);
576dfc57732SGregor Boirie }
577dfc57732SGregor Boirie #endif
578dfc57732SGregor Boirie EXPORT_SYMBOL(of_iio_read_mount_matrix);
579dfc57732SGregor Boirie 
580*51239600SJonathan Cameron static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
581*51239600SJonathan Cameron 				  int size, const int *vals)
582*51239600SJonathan Cameron {
583*51239600SJonathan Cameron 	unsigned long long tmp;
584*51239600SJonathan Cameron 	int tmp0, tmp1;
585*51239600SJonathan Cameron 	bool scale_db = false;
586*51239600SJonathan Cameron 
587*51239600SJonathan Cameron 	switch (type) {
588*51239600SJonathan Cameron 	case IIO_VAL_INT:
589*51239600SJonathan Cameron 		return snprintf(buf, len, "%d", vals[0]);
590*51239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO_DB:
591*51239600SJonathan Cameron 		scale_db = true;
592*51239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_MICRO:
593*51239600SJonathan Cameron 		if (vals[1] < 0)
594*51239600SJonathan Cameron 			return snprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
595*51239600SJonathan Cameron 					-vals[1], scale_db ? " dB" : "");
596*51239600SJonathan Cameron 		else
597*51239600SJonathan Cameron 			return snprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
598*51239600SJonathan Cameron 					scale_db ? " dB" : "");
599*51239600SJonathan Cameron 	case IIO_VAL_INT_PLUS_NANO:
600*51239600SJonathan Cameron 		if (vals[1] < 0)
601*51239600SJonathan Cameron 			return snprintf(buf, len, "-%d.%09u", abs(vals[0]),
602*51239600SJonathan Cameron 					-vals[1]);
603*51239600SJonathan Cameron 		else
604*51239600SJonathan Cameron 			return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
605*51239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL:
606*51239600SJonathan Cameron 		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
607*51239600SJonathan Cameron 		tmp1 = vals[1];
608*51239600SJonathan Cameron 		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
609*51239600SJonathan Cameron 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
610*51239600SJonathan Cameron 	case IIO_VAL_FRACTIONAL_LOG2:
611*51239600SJonathan Cameron 		tmp = (s64)vals[0] * 1000000000LL >> vals[1];
612*51239600SJonathan Cameron 		tmp1 = do_div(tmp, 1000000000LL);
613*51239600SJonathan Cameron 		tmp0 = tmp;
614*51239600SJonathan Cameron 		return snprintf(buf, len, "%d.%09u", tmp0, tmp1);
615*51239600SJonathan Cameron 	case IIO_VAL_INT_MULTIPLE:
616*51239600SJonathan Cameron 	{
617*51239600SJonathan Cameron 		int i;
618*51239600SJonathan Cameron 		int l = 0;
619*51239600SJonathan Cameron 
620*51239600SJonathan Cameron 		for (i = 0; i < size; ++i) {
621*51239600SJonathan Cameron 			l += snprintf(&buf[l], len - l, "%d ", vals[i]);
622*51239600SJonathan Cameron 			if (l >= len)
623*51239600SJonathan Cameron 				break;
624*51239600SJonathan Cameron 		}
625*51239600SJonathan Cameron 		return l;
626*51239600SJonathan Cameron 	}
627*51239600SJonathan Cameron 	default:
628*51239600SJonathan Cameron 		return 0;
629*51239600SJonathan Cameron 	}
630*51239600SJonathan Cameron }
631*51239600SJonathan Cameron 
6323661f3f5SLars-Peter Clausen /**
6333661f3f5SLars-Peter Clausen  * iio_format_value() - Formats a IIO value into its string representation
6342498dcf6SCristina Opriceana  * @buf:	The buffer to which the formatted value gets written
635*51239600SJonathan Cameron  *		which is assumed to be big enough (i.e. PAGE_SIZE).
6362498dcf6SCristina Opriceana  * @type:	One of the IIO_VAL_... constants. This decides how the val
6372498dcf6SCristina Opriceana  *		and val2 parameters are formatted.
6382498dcf6SCristina Opriceana  * @size:	Number of IIO value entries contained in vals
6392498dcf6SCristina Opriceana  * @vals:	Pointer to the values, exact meaning depends on the
6402498dcf6SCristina Opriceana  *		type parameter.
6412498dcf6SCristina Opriceana  *
6422498dcf6SCristina Opriceana  * Return: 0 by default, a negative number on failure or the
6432498dcf6SCristina Opriceana  *	   total number of characters written for a type that belongs
6442498dcf6SCristina Opriceana  *	   to the IIO_VAL_... constant.
6453661f3f5SLars-Peter Clausen  */
6469fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
647a980e046SJonathan Cameron {
648*51239600SJonathan Cameron 	ssize_t len;
649a980e046SJonathan Cameron 
650*51239600SJonathan Cameron 	len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
651*51239600SJonathan Cameron 	if (len >= PAGE_SIZE - 1)
652*51239600SJonathan Cameron 		return -EFBIG;
6539fbfb4b3SSrinivas Pandruvada 
654*51239600SJonathan Cameron 	return len + sprintf(buf + len, "\n");
65567eedba3SMichael Hennerich }
6567d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value);
657a980e046SJonathan Cameron 
6583661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev,
6593661f3f5SLars-Peter Clausen 				     struct device_attribute *attr,
6603661f3f5SLars-Peter Clausen 				     char *buf)
6613661f3f5SLars-Peter Clausen {
6623661f3f5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
6633661f3f5SLars-Peter Clausen 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
6649fbfb4b3SSrinivas Pandruvada 	int vals[INDIO_MAX_RAW_ELEMENTS];
6659fbfb4b3SSrinivas Pandruvada 	int ret;
6669fbfb4b3SSrinivas Pandruvada 	int val_len = 2;
6679fbfb4b3SSrinivas Pandruvada 
6689fbfb4b3SSrinivas Pandruvada 	if (indio_dev->info->read_raw_multi)
6699fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
6709fbfb4b3SSrinivas Pandruvada 							INDIO_MAX_RAW_ELEMENTS,
6719fbfb4b3SSrinivas Pandruvada 							vals, &val_len,
6729fbfb4b3SSrinivas Pandruvada 							this_attr->address);
6739fbfb4b3SSrinivas Pandruvada 	else
6749fbfb4b3SSrinivas Pandruvada 		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
6759fbfb4b3SSrinivas Pandruvada 				    &vals[0], &vals[1], this_attr->address);
6763661f3f5SLars-Peter Clausen 
6773661f3f5SLars-Peter Clausen 	if (ret < 0)
6783661f3f5SLars-Peter Clausen 		return ret;
6793661f3f5SLars-Peter Clausen 
6809fbfb4b3SSrinivas Pandruvada 	return iio_format_value(buf, ret, val_len, vals);
6813661f3f5SLars-Peter Clausen }
6823661f3f5SLars-Peter Clausen 
683*51239600SJonathan Cameron static ssize_t iio_format_avail_list(char *buf, const int *vals,
684*51239600SJonathan Cameron 				     int type, int length)
685*51239600SJonathan Cameron {
686*51239600SJonathan Cameron 	int i;
687*51239600SJonathan Cameron 	ssize_t len = 0;
688*51239600SJonathan Cameron 
689*51239600SJonathan Cameron 	switch (type) {
690*51239600SJonathan Cameron 	case IIO_VAL_INT:
691*51239600SJonathan Cameron 		for (i = 0; i < length; i++) {
692*51239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
693*51239600SJonathan Cameron 						  type, 1, &vals[i]);
694*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
695*51239600SJonathan Cameron 				return -EFBIG;
696*51239600SJonathan Cameron 			if (i < length - 1)
697*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
698*51239600SJonathan Cameron 						" ");
699*51239600SJonathan Cameron 			else
700*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
701*51239600SJonathan Cameron 						"\n");
702*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
703*51239600SJonathan Cameron 				return -EFBIG;
704*51239600SJonathan Cameron 		}
705*51239600SJonathan Cameron 		break;
706*51239600SJonathan Cameron 	default:
707*51239600SJonathan Cameron 		for (i = 0; i < length / 2; i++) {
708*51239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
709*51239600SJonathan Cameron 						  type, 2, &vals[i * 2]);
710*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
711*51239600SJonathan Cameron 				return -EFBIG;
712*51239600SJonathan Cameron 			if (i < length / 2 - 1)
713*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
714*51239600SJonathan Cameron 						" ");
715*51239600SJonathan Cameron 			else
716*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
717*51239600SJonathan Cameron 						"\n");
718*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
719*51239600SJonathan Cameron 				return -EFBIG;
720*51239600SJonathan Cameron 		}
721*51239600SJonathan Cameron 	}
722*51239600SJonathan Cameron 
723*51239600SJonathan Cameron 	return len;
724*51239600SJonathan Cameron }
725*51239600SJonathan Cameron 
726*51239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
727*51239600SJonathan Cameron {
728*51239600SJonathan Cameron 	int i;
729*51239600SJonathan Cameron 	ssize_t len;
730*51239600SJonathan Cameron 
731*51239600SJonathan Cameron 	len = snprintf(buf, PAGE_SIZE, "[");
732*51239600SJonathan Cameron 	switch (type) {
733*51239600SJonathan Cameron 	case IIO_VAL_INT:
734*51239600SJonathan Cameron 		for (i = 0; i < 3; i++) {
735*51239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
736*51239600SJonathan Cameron 						  type, 1, &vals[i]);
737*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
738*51239600SJonathan Cameron 				return -EFBIG;
739*51239600SJonathan Cameron 			if (i < 2)
740*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
741*51239600SJonathan Cameron 						" ");
742*51239600SJonathan Cameron 			else
743*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
744*51239600SJonathan Cameron 						"]\n");
745*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
746*51239600SJonathan Cameron 				return -EFBIG;
747*51239600SJonathan Cameron 		}
748*51239600SJonathan Cameron 		break;
749*51239600SJonathan Cameron 	default:
750*51239600SJonathan Cameron 		for (i = 0; i < 3; i++) {
751*51239600SJonathan Cameron 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
752*51239600SJonathan Cameron 						  type, 2, &vals[i * 2]);
753*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
754*51239600SJonathan Cameron 				return -EFBIG;
755*51239600SJonathan Cameron 			if (i < 2)
756*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
757*51239600SJonathan Cameron 						" ");
758*51239600SJonathan Cameron 			else
759*51239600SJonathan Cameron 				len += snprintf(buf + len, PAGE_SIZE - len,
760*51239600SJonathan Cameron 						"]\n");
761*51239600SJonathan Cameron 			if (len >= PAGE_SIZE)
762*51239600SJonathan Cameron 				return -EFBIG;
763*51239600SJonathan Cameron 		}
764*51239600SJonathan Cameron 	}
765*51239600SJonathan Cameron 
766*51239600SJonathan Cameron 	return len;
767*51239600SJonathan Cameron }
768*51239600SJonathan Cameron 
769*51239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev,
770*51239600SJonathan Cameron 					   struct device_attribute *attr,
771*51239600SJonathan Cameron 					   char *buf)
772*51239600SJonathan Cameron {
773*51239600SJonathan Cameron 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
774*51239600SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
775*51239600SJonathan Cameron 	const int *vals;
776*51239600SJonathan Cameron 	int ret;
777*51239600SJonathan Cameron 	int length;
778*51239600SJonathan Cameron 	int type;
779*51239600SJonathan Cameron 
780*51239600SJonathan Cameron 	ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
781*51239600SJonathan Cameron 					  &vals, &type, &length,
782*51239600SJonathan Cameron 					  this_attr->address);
783*51239600SJonathan Cameron 
784*51239600SJonathan Cameron 	if (ret < 0)
785*51239600SJonathan Cameron 		return ret;
786*51239600SJonathan Cameron 	switch (ret) {
787*51239600SJonathan Cameron 	case IIO_AVAIL_LIST:
788*51239600SJonathan Cameron 		return iio_format_avail_list(buf, vals, type, length);
789*51239600SJonathan Cameron 	case IIO_AVAIL_RANGE:
790*51239600SJonathan Cameron 		return iio_format_avail_range(buf, vals, type);
791*51239600SJonathan Cameron 	default:
792*51239600SJonathan Cameron 		return -EINVAL;
793*51239600SJonathan Cameron 	}
794*51239600SJonathan Cameron }
795*51239600SJonathan Cameron 
7966807d721SLars-Peter Clausen /**
7976807d721SLars-Peter Clausen  * iio_str_to_fixpoint() - Parse a fixed-point number from a string
7986807d721SLars-Peter Clausen  * @str: The string to parse
7996807d721SLars-Peter Clausen  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
8006807d721SLars-Peter Clausen  * @integer: The integer part of the number
8016807d721SLars-Peter Clausen  * @fract: The fractional part of the number
8026807d721SLars-Peter Clausen  *
8036807d721SLars-Peter Clausen  * Returns 0 on success, or a negative error code if the string could not be
8046807d721SLars-Peter Clausen  * parsed.
8056807d721SLars-Peter Clausen  */
8066807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult,
8076807d721SLars-Peter Clausen 	int *integer, int *fract)
8086807d721SLars-Peter Clausen {
8096807d721SLars-Peter Clausen 	int i = 0, f = 0;
8106807d721SLars-Peter Clausen 	bool integer_part = true, negative = false;
8116807d721SLars-Peter Clausen 
812f47dff32SSean Nyekjaer 	if (fract_mult == 0) {
813f47dff32SSean Nyekjaer 		*fract = 0;
814f47dff32SSean Nyekjaer 
815f47dff32SSean Nyekjaer 		return kstrtoint(str, 0, integer);
816f47dff32SSean Nyekjaer 	}
817f47dff32SSean Nyekjaer 
8186807d721SLars-Peter Clausen 	if (str[0] == '-') {
8196807d721SLars-Peter Clausen 		negative = true;
8206807d721SLars-Peter Clausen 		str++;
8216807d721SLars-Peter Clausen 	} else if (str[0] == '+') {
8226807d721SLars-Peter Clausen 		str++;
8236807d721SLars-Peter Clausen 	}
8246807d721SLars-Peter Clausen 
8256807d721SLars-Peter Clausen 	while (*str) {
8266807d721SLars-Peter Clausen 		if ('0' <= *str && *str <= '9') {
8276807d721SLars-Peter Clausen 			if (integer_part) {
8286807d721SLars-Peter Clausen 				i = i * 10 + *str - '0';
8296807d721SLars-Peter Clausen 			} else {
8306807d721SLars-Peter Clausen 				f += fract_mult * (*str - '0');
8316807d721SLars-Peter Clausen 				fract_mult /= 10;
8326807d721SLars-Peter Clausen 			}
8336807d721SLars-Peter Clausen 		} else if (*str == '\n') {
8346807d721SLars-Peter Clausen 			if (*(str + 1) == '\0')
8356807d721SLars-Peter Clausen 				break;
8366807d721SLars-Peter Clausen 			else
8376807d721SLars-Peter Clausen 				return -EINVAL;
8386807d721SLars-Peter Clausen 		} else if (*str == '.' && integer_part) {
8396807d721SLars-Peter Clausen 			integer_part = false;
8406807d721SLars-Peter Clausen 		} else {
8416807d721SLars-Peter Clausen 			return -EINVAL;
8426807d721SLars-Peter Clausen 		}
8436807d721SLars-Peter Clausen 		str++;
8446807d721SLars-Peter Clausen 	}
8456807d721SLars-Peter Clausen 
8466807d721SLars-Peter Clausen 	if (negative) {
8476807d721SLars-Peter Clausen 		if (i)
8486807d721SLars-Peter Clausen 			i = -i;
8496807d721SLars-Peter Clausen 		else
8506807d721SLars-Peter Clausen 			f = -f;
8516807d721SLars-Peter Clausen 	}
8526807d721SLars-Peter Clausen 
8536807d721SLars-Peter Clausen 	*integer = i;
8546807d721SLars-Peter Clausen 	*fract = f;
8556807d721SLars-Peter Clausen 
8566807d721SLars-Peter Clausen 	return 0;
8576807d721SLars-Peter Clausen }
8586807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
8596807d721SLars-Peter Clausen 
860a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev,
861a980e046SJonathan Cameron 				      struct device_attribute *attr,
862a980e046SJonathan Cameron 				      const char *buf,
863a980e046SJonathan Cameron 				      size_t len)
864a980e046SJonathan Cameron {
865e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
866a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
8676807d721SLars-Peter Clausen 	int ret, fract_mult = 100000;
8686807d721SLars-Peter Clausen 	int integer, fract;
869a980e046SJonathan Cameron 
870a980e046SJonathan Cameron 	/* Assumes decimal - precision based on number of digits */
871a980e046SJonathan Cameron 	if (!indio_dev->info->write_raw)
872a980e046SJonathan Cameron 		return -EINVAL;
873a980e046SJonathan Cameron 
874a980e046SJonathan Cameron 	if (indio_dev->info->write_raw_get_fmt)
875a980e046SJonathan Cameron 		switch (indio_dev->info->write_raw_get_fmt(indio_dev,
876a980e046SJonathan Cameron 			this_attr->c, this_attr->address)) {
877f47dff32SSean Nyekjaer 		case IIO_VAL_INT:
878f47dff32SSean Nyekjaer 			fract_mult = 0;
879f47dff32SSean Nyekjaer 			break;
880a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_MICRO:
881a980e046SJonathan Cameron 			fract_mult = 100000;
882a980e046SJonathan Cameron 			break;
883a980e046SJonathan Cameron 		case IIO_VAL_INT_PLUS_NANO:
884a980e046SJonathan Cameron 			fract_mult = 100000000;
885a980e046SJonathan Cameron 			break;
886a980e046SJonathan Cameron 		default:
887a980e046SJonathan Cameron 			return -EINVAL;
888a980e046SJonathan Cameron 		}
889a980e046SJonathan Cameron 
8906807d721SLars-Peter Clausen 	ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
8916807d721SLars-Peter Clausen 	if (ret)
8926807d721SLars-Peter Clausen 		return ret;
893a980e046SJonathan Cameron 
894a980e046SJonathan Cameron 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
895a980e046SJonathan Cameron 					 integer, fract, this_attr->address);
896a980e046SJonathan Cameron 	if (ret)
897a980e046SJonathan Cameron 		return ret;
898a980e046SJonathan Cameron 
899a980e046SJonathan Cameron 	return len;
900a980e046SJonathan Cameron }
901a980e046SJonathan Cameron 
902a980e046SJonathan Cameron static
903a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr,
904a980e046SJonathan Cameron 			   const char *postfix,
905a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
906a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
907a980e046SJonathan Cameron 					       struct device_attribute *attr,
908a980e046SJonathan Cameron 					       char *buf),
909a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
910a980e046SJonathan Cameron 						struct device_attribute *attr,
911a980e046SJonathan Cameron 						const char *buf,
912a980e046SJonathan Cameron 						size_t len),
9133704432fSJonathan Cameron 			   enum iio_shared_by shared_by)
914a980e046SJonathan Cameron {
9153704432fSJonathan Cameron 	int ret = 0;
9167bbcf7e1SLars-Peter Clausen 	char *name = NULL;
9173704432fSJonathan Cameron 	char *full_postfix;
918a980e046SJonathan Cameron 	sysfs_attr_init(&dev_attr->attr);
919a980e046SJonathan Cameron 
920a980e046SJonathan Cameron 	/* Build up postfix of <extend_name>_<modifier>_postfix */
9213704432fSJonathan Cameron 	if (chan->modified && (shared_by == IIO_SEPARATE)) {
922a980e046SJonathan Cameron 		if (chan->extend_name)
923a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
924a980e046SJonathan Cameron 						 iio_modifier_names[chan
925a980e046SJonathan Cameron 								    ->channel2],
926a980e046SJonathan Cameron 						 chan->extend_name,
927a980e046SJonathan Cameron 						 postfix);
928a980e046SJonathan Cameron 		else
929a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
930a980e046SJonathan Cameron 						 iio_modifier_names[chan
931a980e046SJonathan Cameron 								    ->channel2],
932a980e046SJonathan Cameron 						 postfix);
933a980e046SJonathan Cameron 	} else {
93477bfa8baSLars-Peter Clausen 		if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
935a980e046SJonathan Cameron 			full_postfix = kstrdup(postfix, GFP_KERNEL);
936a980e046SJonathan Cameron 		else
937a980e046SJonathan Cameron 			full_postfix = kasprintf(GFP_KERNEL,
938a980e046SJonathan Cameron 						 "%s_%s",
939a980e046SJonathan Cameron 						 chan->extend_name,
940a980e046SJonathan Cameron 						 postfix);
941a980e046SJonathan Cameron 	}
9423704432fSJonathan Cameron 	if (full_postfix == NULL)
9433704432fSJonathan Cameron 		return -ENOMEM;
944a980e046SJonathan Cameron 
945a980e046SJonathan Cameron 	if (chan->differential) { /* Differential can not have modifier */
9463704432fSJonathan Cameron 		switch (shared_by) {
947c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
9487bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
949c006ec83SJonathan Cameron 			break;
950c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
9517bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
952c006ec83SJonathan Cameron 						iio_direction[chan->output],
953c006ec83SJonathan Cameron 						full_postfix);
954c006ec83SJonathan Cameron 			break;
9553704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
9567bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
957a980e046SJonathan Cameron 					    iio_direction[chan->output],
958a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
959a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
960a980e046SJonathan Cameron 					    full_postfix);
9613704432fSJonathan Cameron 			break;
9623704432fSJonathan Cameron 		case IIO_SEPARATE:
9633704432fSJonathan Cameron 			if (!chan->indexed) {
964231bfe53SDan Carpenter 				WARN(1, "Differential channels must be indexed\n");
9653704432fSJonathan Cameron 				ret = -EINVAL;
9663704432fSJonathan Cameron 				goto error_free_full_postfix;
9673704432fSJonathan Cameron 			}
9687bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL,
9693704432fSJonathan Cameron 					    "%s_%s%d-%s%d_%s",
970a980e046SJonathan Cameron 					    iio_direction[chan->output],
971a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
972a980e046SJonathan Cameron 					    chan->channel,
973a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
974a980e046SJonathan Cameron 					    chan->channel2,
975a980e046SJonathan Cameron 					    full_postfix);
9763704432fSJonathan Cameron 			break;
977a980e046SJonathan Cameron 		}
978a980e046SJonathan Cameron 	} else { /* Single ended */
9793704432fSJonathan Cameron 		switch (shared_by) {
980c006ec83SJonathan Cameron 		case IIO_SHARED_BY_ALL:
9817bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
982c006ec83SJonathan Cameron 			break;
983c006ec83SJonathan Cameron 		case IIO_SHARED_BY_DIR:
9847bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s",
985c006ec83SJonathan Cameron 						iio_direction[chan->output],
986c006ec83SJonathan Cameron 						full_postfix);
987c006ec83SJonathan Cameron 			break;
9883704432fSJonathan Cameron 		case IIO_SHARED_BY_TYPE:
9897bbcf7e1SLars-Peter Clausen 			name = kasprintf(GFP_KERNEL, "%s_%s_%s",
990a980e046SJonathan Cameron 					    iio_direction[chan->output],
991a980e046SJonathan Cameron 					    iio_chan_type_name_spec[chan->type],
992a980e046SJonathan Cameron 					    full_postfix);
9933704432fSJonathan Cameron 			break;
9943704432fSJonathan Cameron 
9953704432fSJonathan Cameron 		case IIO_SEPARATE:
9963704432fSJonathan Cameron 			if (chan->indexed)
9977bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
998a980e046SJonathan Cameron 						    iio_direction[chan->output],
999a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
1000a980e046SJonathan Cameron 						    chan->channel,
1001a980e046SJonathan Cameron 						    full_postfix);
1002a980e046SJonathan Cameron 			else
10037bbcf7e1SLars-Peter Clausen 				name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1004a980e046SJonathan Cameron 						    iio_direction[chan->output],
1005a980e046SJonathan Cameron 						    iio_chan_type_name_spec[chan->type],
1006a980e046SJonathan Cameron 						    full_postfix);
10073704432fSJonathan Cameron 			break;
10083704432fSJonathan Cameron 		}
1009a980e046SJonathan Cameron 	}
10107bbcf7e1SLars-Peter Clausen 	if (name == NULL) {
1011a980e046SJonathan Cameron 		ret = -ENOMEM;
1012a980e046SJonathan Cameron 		goto error_free_full_postfix;
1013a980e046SJonathan Cameron 	}
10147bbcf7e1SLars-Peter Clausen 	dev_attr->attr.name = name;
1015a980e046SJonathan Cameron 
1016a980e046SJonathan Cameron 	if (readfunc) {
1017a980e046SJonathan Cameron 		dev_attr->attr.mode |= S_IRUGO;
1018a980e046SJonathan Cameron 		dev_attr->show = readfunc;
1019a980e046SJonathan Cameron 	}
1020a980e046SJonathan Cameron 
1021a980e046SJonathan Cameron 	if (writefunc) {
1022a980e046SJonathan Cameron 		dev_attr->attr.mode |= S_IWUSR;
1023a980e046SJonathan Cameron 		dev_attr->store = writefunc;
1024a980e046SJonathan Cameron 	}
10257bbcf7e1SLars-Peter Clausen 
1026a980e046SJonathan Cameron error_free_full_postfix:
1027a980e046SJonathan Cameron 	kfree(full_postfix);
10283704432fSJonathan Cameron 
1029a980e046SJonathan Cameron 	return ret;
1030a980e046SJonathan Cameron }
1031a980e046SJonathan Cameron 
1032a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1033a980e046SJonathan Cameron {
1034a980e046SJonathan Cameron 	kfree(dev_attr->attr.name);
1035a980e046SJonathan Cameron }
1036a980e046SJonathan Cameron 
1037a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix,
1038a980e046SJonathan Cameron 			   struct iio_chan_spec const *chan,
1039a980e046SJonathan Cameron 			   ssize_t (*readfunc)(struct device *dev,
1040a980e046SJonathan Cameron 					       struct device_attribute *attr,
1041a980e046SJonathan Cameron 					       char *buf),
1042a980e046SJonathan Cameron 			   ssize_t (*writefunc)(struct device *dev,
1043a980e046SJonathan Cameron 						struct device_attribute *attr,
1044a980e046SJonathan Cameron 						const char *buf,
1045a980e046SJonathan Cameron 						size_t len),
1046a980e046SJonathan Cameron 			   u64 mask,
10473704432fSJonathan Cameron 			   enum iio_shared_by shared_by,
1048a980e046SJonathan Cameron 			   struct device *dev,
1049a980e046SJonathan Cameron 			   struct list_head *attr_list)
1050a980e046SJonathan Cameron {
1051a980e046SJonathan Cameron 	int ret;
1052a980e046SJonathan Cameron 	struct iio_dev_attr *iio_attr, *t;
1053a980e046SJonathan Cameron 
1054670c1103SSachin Kamat 	iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
105592825ff9SHartmut Knaack 	if (iio_attr == NULL)
105692825ff9SHartmut Knaack 		return -ENOMEM;
1057a980e046SJonathan Cameron 	ret = __iio_device_attr_init(&iio_attr->dev_attr,
1058a980e046SJonathan Cameron 				     postfix, chan,
10593704432fSJonathan Cameron 				     readfunc, writefunc, shared_by);
1060a980e046SJonathan Cameron 	if (ret)
1061a980e046SJonathan Cameron 		goto error_iio_dev_attr_free;
1062a980e046SJonathan Cameron 	iio_attr->c = chan;
1063a980e046SJonathan Cameron 	iio_attr->address = mask;
1064a980e046SJonathan Cameron 	list_for_each_entry(t, attr_list, l)
1065a980e046SJonathan Cameron 		if (strcmp(t->dev_attr.attr.name,
1066a980e046SJonathan Cameron 			   iio_attr->dev_attr.attr.name) == 0) {
10673704432fSJonathan Cameron 			if (shared_by == IIO_SEPARATE)
1068a980e046SJonathan Cameron 				dev_err(dev, "tried to double register : %s\n",
1069a980e046SJonathan Cameron 					t->dev_attr.attr.name);
1070a980e046SJonathan Cameron 			ret = -EBUSY;
1071a980e046SJonathan Cameron 			goto error_device_attr_deinit;
1072a980e046SJonathan Cameron 		}
1073a980e046SJonathan Cameron 	list_add(&iio_attr->l, attr_list);
1074a980e046SJonathan Cameron 
1075a980e046SJonathan Cameron 	return 0;
1076a980e046SJonathan Cameron 
1077a980e046SJonathan Cameron error_device_attr_deinit:
1078a980e046SJonathan Cameron 	__iio_device_attr_deinit(&iio_attr->dev_attr);
1079a980e046SJonathan Cameron error_iio_dev_attr_free:
1080a980e046SJonathan Cameron 	kfree(iio_attr);
1081a980e046SJonathan Cameron 	return ret;
1082a980e046SJonathan Cameron }
1083a980e046SJonathan Cameron 
10843704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
10853704432fSJonathan Cameron 					 struct iio_chan_spec const *chan,
10863704432fSJonathan Cameron 					 enum iio_shared_by shared_by,
10873704432fSJonathan Cameron 					 const long *infomask)
10883704432fSJonathan Cameron {
10893704432fSJonathan Cameron 	int i, ret, attrcount = 0;
10903704432fSJonathan Cameron 
10913704432fSJonathan Cameron 	for_each_set_bit(i, infomask, sizeof(infomask)*8) {
1092ef4b4856SJonathan Cameron 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1093ef4b4856SJonathan Cameron 			return -EINVAL;
10943704432fSJonathan Cameron 		ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
10953704432fSJonathan Cameron 					     chan,
10963704432fSJonathan Cameron 					     &iio_read_channel_info,
10973704432fSJonathan Cameron 					     &iio_write_channel_info,
10983704432fSJonathan Cameron 					     i,
10993704432fSJonathan Cameron 					     shared_by,
11003704432fSJonathan Cameron 					     &indio_dev->dev,
11013704432fSJonathan Cameron 					     &indio_dev->channel_attr_list);
11023704432fSJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
11033704432fSJonathan Cameron 			continue;
11043704432fSJonathan Cameron 		else if (ret < 0)
11053704432fSJonathan Cameron 			return ret;
11063704432fSJonathan Cameron 		attrcount++;
11073704432fSJonathan Cameron 	}
11083704432fSJonathan Cameron 
11093704432fSJonathan Cameron 	return attrcount;
11103704432fSJonathan Cameron }
11113704432fSJonathan Cameron 
1112*51239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1113*51239600SJonathan Cameron 					       struct iio_chan_spec const *chan,
1114*51239600SJonathan Cameron 					       enum iio_shared_by shared_by,
1115*51239600SJonathan Cameron 					       const long *infomask)
1116*51239600SJonathan Cameron {
1117*51239600SJonathan Cameron 	int i, ret, attrcount = 0;
1118*51239600SJonathan Cameron 	char *avail_postfix;
1119*51239600SJonathan Cameron 
1120*51239600SJonathan Cameron 	for_each_set_bit(i, infomask, sizeof(infomask) * 8) {
1121*51239600SJonathan Cameron 		avail_postfix = kasprintf(GFP_KERNEL,
1122*51239600SJonathan Cameron 					  "%s_available",
1123*51239600SJonathan Cameron 					  iio_chan_info_postfix[i]);
1124*51239600SJonathan Cameron 		if (!avail_postfix)
1125*51239600SJonathan Cameron 			return -ENOMEM;
1126*51239600SJonathan Cameron 
1127*51239600SJonathan Cameron 		ret = __iio_add_chan_devattr(avail_postfix,
1128*51239600SJonathan Cameron 					     chan,
1129*51239600SJonathan Cameron 					     &iio_read_channel_info_avail,
1130*51239600SJonathan Cameron 					     NULL,
1131*51239600SJonathan Cameron 					     i,
1132*51239600SJonathan Cameron 					     shared_by,
1133*51239600SJonathan Cameron 					     &indio_dev->dev,
1134*51239600SJonathan Cameron 					     &indio_dev->channel_attr_list);
1135*51239600SJonathan Cameron 		kfree(avail_postfix);
1136*51239600SJonathan Cameron 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1137*51239600SJonathan Cameron 			continue;
1138*51239600SJonathan Cameron 		else if (ret < 0)
1139*51239600SJonathan Cameron 			return ret;
1140*51239600SJonathan Cameron 		attrcount++;
1141*51239600SJonathan Cameron 	}
1142*51239600SJonathan Cameron 
1143*51239600SJonathan Cameron 	return attrcount;
1144*51239600SJonathan Cameron }
1145*51239600SJonathan Cameron 
1146a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1147a980e046SJonathan Cameron 					struct iio_chan_spec const *chan)
1148a980e046SJonathan Cameron {
1149a980e046SJonathan Cameron 	int ret, attrcount = 0;
1150a980e046SJonathan Cameron 	const struct iio_chan_spec_ext_info *ext_info;
1151a980e046SJonathan Cameron 
1152a980e046SJonathan Cameron 	if (chan->channel < 0)
1153a980e046SJonathan Cameron 		return 0;
11543704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
11553704432fSJonathan Cameron 					    IIO_SEPARATE,
11563704432fSJonathan Cameron 					    &chan->info_mask_separate);
11578655cc49SJonathan Cameron 	if (ret < 0)
11583704432fSJonathan Cameron 		return ret;
11593704432fSJonathan Cameron 	attrcount += ret;
11603704432fSJonathan Cameron 
1161*51239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1162*51239600SJonathan Cameron 						  IIO_SEPARATE,
1163*51239600SJonathan Cameron 						  &chan->
1164*51239600SJonathan Cameron 						  info_mask_separate_available);
1165*51239600SJonathan Cameron 	if (ret < 0)
1166*51239600SJonathan Cameron 		return ret;
1167*51239600SJonathan Cameron 	attrcount += ret;
1168*51239600SJonathan Cameron 
11693704432fSJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
11703704432fSJonathan Cameron 					    IIO_SHARED_BY_TYPE,
11713704432fSJonathan Cameron 					    &chan->info_mask_shared_by_type);
11723704432fSJonathan Cameron 	if (ret < 0)
11733704432fSJonathan Cameron 		return ret;
11743704432fSJonathan Cameron 	attrcount += ret;
1175a980e046SJonathan Cameron 
1176*51239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1177*51239600SJonathan Cameron 						  IIO_SHARED_BY_TYPE,
1178*51239600SJonathan Cameron 						  &chan->
1179*51239600SJonathan Cameron 						  info_mask_shared_by_type_available);
1180*51239600SJonathan Cameron 	if (ret < 0)
1181*51239600SJonathan Cameron 		return ret;
1182*51239600SJonathan Cameron 	attrcount += ret;
1183*51239600SJonathan Cameron 
1184c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1185c006ec83SJonathan Cameron 					    IIO_SHARED_BY_DIR,
1186c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_dir);
1187c006ec83SJonathan Cameron 	if (ret < 0)
1188c006ec83SJonathan Cameron 		return ret;
1189c006ec83SJonathan Cameron 	attrcount += ret;
1190c006ec83SJonathan Cameron 
1191*51239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1192*51239600SJonathan Cameron 						  IIO_SHARED_BY_DIR,
1193*51239600SJonathan Cameron 						  &chan->info_mask_shared_by_dir_available);
1194*51239600SJonathan Cameron 	if (ret < 0)
1195*51239600SJonathan Cameron 		return ret;
1196*51239600SJonathan Cameron 	attrcount += ret;
1197*51239600SJonathan Cameron 
1198c006ec83SJonathan Cameron 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1199c006ec83SJonathan Cameron 					    IIO_SHARED_BY_ALL,
1200c006ec83SJonathan Cameron 					    &chan->info_mask_shared_by_all);
1201c006ec83SJonathan Cameron 	if (ret < 0)
1202c006ec83SJonathan Cameron 		return ret;
1203c006ec83SJonathan Cameron 	attrcount += ret;
1204c006ec83SJonathan Cameron 
1205*51239600SJonathan Cameron 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1206*51239600SJonathan Cameron 						  IIO_SHARED_BY_ALL,
1207*51239600SJonathan Cameron 						  &chan->info_mask_shared_by_all_available);
1208*51239600SJonathan Cameron 	if (ret < 0)
1209*51239600SJonathan Cameron 		return ret;
1210*51239600SJonathan Cameron 	attrcount += ret;
1211*51239600SJonathan Cameron 
1212a980e046SJonathan Cameron 	if (chan->ext_info) {
1213a980e046SJonathan Cameron 		unsigned int i = 0;
1214a980e046SJonathan Cameron 		for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1215a980e046SJonathan Cameron 			ret = __iio_add_chan_devattr(ext_info->name,
1216a980e046SJonathan Cameron 					chan,
1217a980e046SJonathan Cameron 					ext_info->read ?
1218a980e046SJonathan Cameron 					    &iio_read_channel_ext_info : NULL,
1219a980e046SJonathan Cameron 					ext_info->write ?
1220a980e046SJonathan Cameron 					    &iio_write_channel_ext_info : NULL,
1221a980e046SJonathan Cameron 					i,
1222a980e046SJonathan Cameron 					ext_info->shared,
1223a980e046SJonathan Cameron 					&indio_dev->dev,
1224a980e046SJonathan Cameron 					&indio_dev->channel_attr_list);
1225a980e046SJonathan Cameron 			i++;
1226a980e046SJonathan Cameron 			if (ret == -EBUSY && ext_info->shared)
1227a980e046SJonathan Cameron 				continue;
1228a980e046SJonathan Cameron 
1229a980e046SJonathan Cameron 			if (ret)
12303704432fSJonathan Cameron 				return ret;
1231a980e046SJonathan Cameron 
1232a980e046SJonathan Cameron 			attrcount++;
1233a980e046SJonathan Cameron 		}
1234a980e046SJonathan Cameron 	}
1235a980e046SJonathan Cameron 
12363704432fSJonathan Cameron 	return attrcount;
1237a980e046SJonathan Cameron }
1238a980e046SJonathan Cameron 
123984088ebdSLars-Peter Clausen /**
124084088ebdSLars-Peter Clausen  * iio_free_chan_devattr_list() - Free a list of IIO device attributes
124184088ebdSLars-Peter Clausen  * @attr_list: List of IIO device attributes
124284088ebdSLars-Peter Clausen  *
124384088ebdSLars-Peter Clausen  * This function frees the memory allocated for each of the IIO device
1244c1b03ab5SMartin Fuzzey  * attributes in the list.
124584088ebdSLars-Peter Clausen  */
124684088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list)
1247a980e046SJonathan Cameron {
124884088ebdSLars-Peter Clausen 	struct iio_dev_attr *p, *n;
124984088ebdSLars-Peter Clausen 
125084088ebdSLars-Peter Clausen 	list_for_each_entry_safe(p, n, attr_list, l) {
1251a980e046SJonathan Cameron 		kfree(p->dev_attr.attr.name);
1252c1b03ab5SMartin Fuzzey 		list_del(&p->l);
1253a980e046SJonathan Cameron 		kfree(p);
1254a980e046SJonathan Cameron 	}
125584088ebdSLars-Peter Clausen }
1256a980e046SJonathan Cameron 
1257a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev,
1258a980e046SJonathan Cameron 				 struct device_attribute *attr,
1259a980e046SJonathan Cameron 				 char *buf)
1260a980e046SJonathan Cameron {
1261e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1262598db581SKees Cook 	return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
1263a980e046SJonathan Cameron }
1264a980e046SJonathan Cameron 
1265a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1266a980e046SJonathan Cameron 
1267bc2b7dabSGregor Boirie static ssize_t iio_show_timestamp_clock(struct device *dev,
1268bc2b7dabSGregor Boirie 					struct device_attribute *attr,
1269bc2b7dabSGregor Boirie 					char *buf)
1270bc2b7dabSGregor Boirie {
1271bc2b7dabSGregor Boirie 	const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1272bc2b7dabSGregor Boirie 	const clockid_t clk = iio_device_get_clock(indio_dev);
1273bc2b7dabSGregor Boirie 	const char *name;
1274bc2b7dabSGregor Boirie 	ssize_t sz;
1275bc2b7dabSGregor Boirie 
1276bc2b7dabSGregor Boirie 	switch (clk) {
1277bc2b7dabSGregor Boirie 	case CLOCK_REALTIME:
1278bc2b7dabSGregor Boirie 		name = "realtime\n";
1279bc2b7dabSGregor Boirie 		sz = sizeof("realtime\n");
1280bc2b7dabSGregor Boirie 		break;
1281bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC:
1282bc2b7dabSGregor Boirie 		name = "monotonic\n";
1283bc2b7dabSGregor Boirie 		sz = sizeof("monotonic\n");
1284bc2b7dabSGregor Boirie 		break;
1285bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_RAW:
1286bc2b7dabSGregor Boirie 		name = "monotonic_raw\n";
1287bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_raw\n");
1288bc2b7dabSGregor Boirie 		break;
1289bc2b7dabSGregor Boirie 	case CLOCK_REALTIME_COARSE:
1290bc2b7dabSGregor Boirie 		name = "realtime_coarse\n";
1291bc2b7dabSGregor Boirie 		sz = sizeof("realtime_coarse\n");
1292bc2b7dabSGregor Boirie 		break;
1293bc2b7dabSGregor Boirie 	case CLOCK_MONOTONIC_COARSE:
1294bc2b7dabSGregor Boirie 		name = "monotonic_coarse\n";
1295bc2b7dabSGregor Boirie 		sz = sizeof("monotonic_coarse\n");
1296bc2b7dabSGregor Boirie 		break;
1297bc2b7dabSGregor Boirie 	case CLOCK_BOOTTIME:
1298bc2b7dabSGregor Boirie 		name = "boottime\n";
1299bc2b7dabSGregor Boirie 		sz = sizeof("boottime\n");
1300bc2b7dabSGregor Boirie 		break;
1301bc2b7dabSGregor Boirie 	case CLOCK_TAI:
1302bc2b7dabSGregor Boirie 		name = "tai\n";
1303bc2b7dabSGregor Boirie 		sz = sizeof("tai\n");
1304bc2b7dabSGregor Boirie 		break;
1305bc2b7dabSGregor Boirie 	default:
1306bc2b7dabSGregor Boirie 		BUG();
1307bc2b7dabSGregor Boirie 	}
1308bc2b7dabSGregor Boirie 
1309bc2b7dabSGregor Boirie 	memcpy(buf, name, sz);
1310bc2b7dabSGregor Boirie 	return sz;
1311bc2b7dabSGregor Boirie }
1312bc2b7dabSGregor Boirie 
1313bc2b7dabSGregor Boirie static ssize_t iio_store_timestamp_clock(struct device *dev,
1314bc2b7dabSGregor Boirie 					 struct device_attribute *attr,
1315bc2b7dabSGregor Boirie 					 const char *buf, size_t len)
1316bc2b7dabSGregor Boirie {
1317bc2b7dabSGregor Boirie 	clockid_t clk;
1318bc2b7dabSGregor Boirie 	int ret;
1319bc2b7dabSGregor Boirie 
1320bc2b7dabSGregor Boirie 	if (sysfs_streq(buf, "realtime"))
1321bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME;
1322bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic"))
1323bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC;
1324bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_raw"))
1325bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_RAW;
1326bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "realtime_coarse"))
1327bc2b7dabSGregor Boirie 		clk = CLOCK_REALTIME_COARSE;
1328bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "monotonic_coarse"))
1329bc2b7dabSGregor Boirie 		clk = CLOCK_MONOTONIC_COARSE;
1330bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "boottime"))
1331bc2b7dabSGregor Boirie 		clk = CLOCK_BOOTTIME;
1332bc2b7dabSGregor Boirie 	else if (sysfs_streq(buf, "tai"))
1333bc2b7dabSGregor Boirie 		clk = CLOCK_TAI;
1334bc2b7dabSGregor Boirie 	else
1335bc2b7dabSGregor Boirie 		return -EINVAL;
1336bc2b7dabSGregor Boirie 
1337bc2b7dabSGregor Boirie 	ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1338bc2b7dabSGregor Boirie 	if (ret)
1339bc2b7dabSGregor Boirie 		return ret;
1340bc2b7dabSGregor Boirie 
1341bc2b7dabSGregor Boirie 	return len;
1342bc2b7dabSGregor Boirie }
1343bc2b7dabSGregor Boirie 
1344bc2b7dabSGregor Boirie static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1345bc2b7dabSGregor Boirie 		   iio_show_timestamp_clock, iio_store_timestamp_clock);
1346bc2b7dabSGregor Boirie 
1347a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1348a980e046SJonathan Cameron {
1349a980e046SJonathan Cameron 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
135084088ebdSLars-Peter Clausen 	struct iio_dev_attr *p;
1351bc2b7dabSGregor Boirie 	struct attribute **attr, *clk = NULL;
1352a980e046SJonathan Cameron 
1353a980e046SJonathan Cameron 	/* First count elements in any existing group */
1354a980e046SJonathan Cameron 	if (indio_dev->info->attrs) {
1355a980e046SJonathan Cameron 		attr = indio_dev->info->attrs->attrs;
1356a980e046SJonathan Cameron 		while (*attr++ != NULL)
1357a980e046SJonathan Cameron 			attrcount_orig++;
1358a980e046SJonathan Cameron 	}
1359a980e046SJonathan Cameron 	attrcount = attrcount_orig;
1360a980e046SJonathan Cameron 	/*
1361a980e046SJonathan Cameron 	 * New channel registration method - relies on the fact a group does
1362d25b3808SPeter Meerwald 	 * not need to be initialized if its name is NULL.
1363a980e046SJonathan Cameron 	 */
1364a980e046SJonathan Cameron 	if (indio_dev->channels)
1365a980e046SJonathan Cameron 		for (i = 0; i < indio_dev->num_channels; i++) {
1366bc2b7dabSGregor Boirie 			const struct iio_chan_spec *chan =
1367bc2b7dabSGregor Boirie 				&indio_dev->channels[i];
1368bc2b7dabSGregor Boirie 
1369bc2b7dabSGregor Boirie 			if (chan->type == IIO_TIMESTAMP)
1370bc2b7dabSGregor Boirie 				clk = &dev_attr_current_timestamp_clock.attr;
1371bc2b7dabSGregor Boirie 
1372bc2b7dabSGregor Boirie 			ret = iio_device_add_channel_sysfs(indio_dev, chan);
1373a980e046SJonathan Cameron 			if (ret < 0)
1374a980e046SJonathan Cameron 				goto error_clear_attrs;
1375a980e046SJonathan Cameron 			attrcount += ret;
1376a980e046SJonathan Cameron 		}
1377a980e046SJonathan Cameron 
1378bc2b7dabSGregor Boirie 	if (indio_dev->event_interface)
1379bc2b7dabSGregor Boirie 		clk = &dev_attr_current_timestamp_clock.attr;
1380bc2b7dabSGregor Boirie 
1381a980e046SJonathan Cameron 	if (indio_dev->name)
1382a980e046SJonathan Cameron 		attrcount++;
1383bc2b7dabSGregor Boirie 	if (clk)
1384bc2b7dabSGregor Boirie 		attrcount++;
1385a980e046SJonathan Cameron 
1386a980e046SJonathan Cameron 	indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
1387a980e046SJonathan Cameron 						   sizeof(indio_dev->chan_attr_group.attrs[0]),
1388a980e046SJonathan Cameron 						   GFP_KERNEL);
1389a980e046SJonathan Cameron 	if (indio_dev->chan_attr_group.attrs == NULL) {
1390a980e046SJonathan Cameron 		ret = -ENOMEM;
1391a980e046SJonathan Cameron 		goto error_clear_attrs;
1392a980e046SJonathan Cameron 	}
1393a980e046SJonathan Cameron 	/* Copy across original attributes */
1394a980e046SJonathan Cameron 	if (indio_dev->info->attrs)
1395a980e046SJonathan Cameron 		memcpy(indio_dev->chan_attr_group.attrs,
1396a980e046SJonathan Cameron 		       indio_dev->info->attrs->attrs,
1397a980e046SJonathan Cameron 		       sizeof(indio_dev->chan_attr_group.attrs[0])
1398a980e046SJonathan Cameron 		       *attrcount_orig);
1399a980e046SJonathan Cameron 	attrn = attrcount_orig;
1400a980e046SJonathan Cameron 	/* Add all elements from the list. */
1401a980e046SJonathan Cameron 	list_for_each_entry(p, &indio_dev->channel_attr_list, l)
1402a980e046SJonathan Cameron 		indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1403a980e046SJonathan Cameron 	if (indio_dev->name)
1404a980e046SJonathan Cameron 		indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1405bc2b7dabSGregor Boirie 	if (clk)
1406bc2b7dabSGregor Boirie 		indio_dev->chan_attr_group.attrs[attrn++] = clk;
1407a980e046SJonathan Cameron 
1408a980e046SJonathan Cameron 	indio_dev->groups[indio_dev->groupcounter++] =
1409a980e046SJonathan Cameron 		&indio_dev->chan_attr_group;
1410a980e046SJonathan Cameron 
1411a980e046SJonathan Cameron 	return 0;
1412a980e046SJonathan Cameron 
1413a980e046SJonathan Cameron error_clear_attrs:
141484088ebdSLars-Peter Clausen 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1415a980e046SJonathan Cameron 
1416a980e046SJonathan Cameron 	return ret;
1417a980e046SJonathan Cameron }
1418a980e046SJonathan Cameron 
1419a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1420a980e046SJonathan Cameron {
1421a980e046SJonathan Cameron 
142284088ebdSLars-Peter Clausen 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1423a980e046SJonathan Cameron 	kfree(indio_dev->chan_attr_group.attrs);
1424c1b03ab5SMartin Fuzzey 	indio_dev->chan_attr_group.attrs = NULL;
1425a980e046SJonathan Cameron }
1426a980e046SJonathan Cameron 
1427a980e046SJonathan Cameron static void iio_dev_release(struct device *device)
1428a980e046SJonathan Cameron {
1429e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(device);
1430735ad074SVladimir Barinov 	if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
1431a980e046SJonathan Cameron 		iio_device_unregister_trigger_consumer(indio_dev);
1432a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1433a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
1434e407fd65SLars-Peter Clausen 
14359e69c935SLars-Peter Clausen 	iio_buffer_put(indio_dev->buffer);
14369e69c935SLars-Peter Clausen 
1437e407fd65SLars-Peter Clausen 	ida_simple_remove(&iio_ida, indio_dev->id);
1438e407fd65SLars-Peter Clausen 	kfree(indio_dev);
1439a980e046SJonathan Cameron }
1440a980e046SJonathan Cameron 
144117d82b47SGuenter Roeck struct device_type iio_device_type = {
1442a980e046SJonathan Cameron 	.name = "iio_device",
1443a980e046SJonathan Cameron 	.release = iio_dev_release,
1444a980e046SJonathan Cameron };
1445a980e046SJonathan Cameron 
1446a7e57dceSSachin Kamat /**
1447a7e57dceSSachin Kamat  * iio_device_alloc() - allocate an iio_dev from a driver
1448a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1449a7e57dceSSachin Kamat  **/
14507cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv)
1451a980e046SJonathan Cameron {
1452a980e046SJonathan Cameron 	struct iio_dev *dev;
1453a980e046SJonathan Cameron 	size_t alloc_size;
1454a980e046SJonathan Cameron 
1455a980e046SJonathan Cameron 	alloc_size = sizeof(struct iio_dev);
1456a980e046SJonathan Cameron 	if (sizeof_priv) {
1457a980e046SJonathan Cameron 		alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1458a980e046SJonathan Cameron 		alloc_size += sizeof_priv;
1459a980e046SJonathan Cameron 	}
1460a980e046SJonathan Cameron 	/* ensure 32-byte alignment of whole construct ? */
1461a980e046SJonathan Cameron 	alloc_size += IIO_ALIGN - 1;
1462a980e046SJonathan Cameron 
1463a980e046SJonathan Cameron 	dev = kzalloc(alloc_size, GFP_KERNEL);
1464a980e046SJonathan Cameron 
1465a980e046SJonathan Cameron 	if (dev) {
1466a980e046SJonathan Cameron 		dev->dev.groups = dev->groups;
146717d82b47SGuenter Roeck 		dev->dev.type = &iio_device_type;
1468a980e046SJonathan Cameron 		dev->dev.bus = &iio_bus_type;
1469a980e046SJonathan Cameron 		device_initialize(&dev->dev);
1470a980e046SJonathan Cameron 		dev_set_drvdata(&dev->dev, (void *)dev);
1471a980e046SJonathan Cameron 		mutex_init(&dev->mlock);
1472a980e046SJonathan Cameron 		mutex_init(&dev->info_exist_lock);
1473e407fd65SLars-Peter Clausen 		INIT_LIST_HEAD(&dev->channel_attr_list);
1474a980e046SJonathan Cameron 
1475a980e046SJonathan Cameron 		dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1476a980e046SJonathan Cameron 		if (dev->id < 0) {
1477a980e046SJonathan Cameron 			/* cannot use a dev_err as the name isn't available */
14783176dd5dSSachin Kamat 			pr_err("failed to get device id\n");
1479a980e046SJonathan Cameron 			kfree(dev);
1480a980e046SJonathan Cameron 			return NULL;
1481a980e046SJonathan Cameron 		}
1482a980e046SJonathan Cameron 		dev_set_name(&dev->dev, "iio:device%d", dev->id);
148384b36ce5SJonathan Cameron 		INIT_LIST_HEAD(&dev->buffer_list);
1484a980e046SJonathan Cameron 	}
1485a980e046SJonathan Cameron 
1486a980e046SJonathan Cameron 	return dev;
1487a980e046SJonathan Cameron }
14887cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc);
1489a980e046SJonathan Cameron 
1490a7e57dceSSachin Kamat /**
1491a7e57dceSSachin Kamat  * iio_device_free() - free an iio_dev from a driver
1492a7e57dceSSachin Kamat  * @dev:		the iio_dev associated with the device
1493a7e57dceSSachin Kamat  **/
14947cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev)
1495a980e046SJonathan Cameron {
1496e407fd65SLars-Peter Clausen 	if (dev)
1497e407fd65SLars-Peter Clausen 		put_device(&dev->dev);
1498a980e046SJonathan Cameron }
14997cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free);
1500a980e046SJonathan Cameron 
15019dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res)
15029dabaf5eSGrygorii Strashko {
15039dabaf5eSGrygorii Strashko 	iio_device_free(*(struct iio_dev **)res);
15049dabaf5eSGrygorii Strashko }
15059dabaf5eSGrygorii Strashko 
150670e48348SGregor Boirie int devm_iio_device_match(struct device *dev, void *res, void *data)
15079dabaf5eSGrygorii Strashko {
15089dabaf5eSGrygorii Strashko 	struct iio_dev **r = res;
15099dabaf5eSGrygorii Strashko 	if (!r || !*r) {
15109dabaf5eSGrygorii Strashko 		WARN_ON(!r || !*r);
15119dabaf5eSGrygorii Strashko 		return 0;
15129dabaf5eSGrygorii Strashko 	}
15139dabaf5eSGrygorii Strashko 	return *r == data;
15149dabaf5eSGrygorii Strashko }
151570e48348SGregor Boirie EXPORT_SYMBOL_GPL(devm_iio_device_match);
15169dabaf5eSGrygorii Strashko 
1517a7e57dceSSachin Kamat /**
1518a7e57dceSSachin Kamat  * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1519a7e57dceSSachin Kamat  * @dev:		Device to allocate iio_dev for
1520a7e57dceSSachin Kamat  * @sizeof_priv:	Space to allocate for private structure.
1521a7e57dceSSachin Kamat  *
1522a7e57dceSSachin Kamat  * Managed iio_device_alloc. iio_dev allocated with this function is
1523a7e57dceSSachin Kamat  * automatically freed on driver detach.
1524a7e57dceSSachin Kamat  *
1525a7e57dceSSachin Kamat  * If an iio_dev allocated with this function needs to be freed separately,
1526a7e57dceSSachin Kamat  * devm_iio_device_free() must be used.
1527a7e57dceSSachin Kamat  *
1528a7e57dceSSachin Kamat  * RETURNS:
1529a7e57dceSSachin Kamat  * Pointer to allocated iio_dev on success, NULL on failure.
1530a7e57dceSSachin Kamat  */
15319dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
15329dabaf5eSGrygorii Strashko {
15339dabaf5eSGrygorii Strashko 	struct iio_dev **ptr, *iio_dev;
15349dabaf5eSGrygorii Strashko 
15359dabaf5eSGrygorii Strashko 	ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
15369dabaf5eSGrygorii Strashko 			   GFP_KERNEL);
15379dabaf5eSGrygorii Strashko 	if (!ptr)
15389dabaf5eSGrygorii Strashko 		return NULL;
15399dabaf5eSGrygorii Strashko 
15409dabaf5eSGrygorii Strashko 	iio_dev = iio_device_alloc(sizeof_priv);
15419dabaf5eSGrygorii Strashko 	if (iio_dev) {
15429dabaf5eSGrygorii Strashko 		*ptr = iio_dev;
15439dabaf5eSGrygorii Strashko 		devres_add(dev, ptr);
15449dabaf5eSGrygorii Strashko 	} else {
15459dabaf5eSGrygorii Strashko 		devres_free(ptr);
15469dabaf5eSGrygorii Strashko 	}
15479dabaf5eSGrygorii Strashko 
15489dabaf5eSGrygorii Strashko 	return iio_dev;
15499dabaf5eSGrygorii Strashko }
15509dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
15519dabaf5eSGrygorii Strashko 
1552a7e57dceSSachin Kamat /**
1553a7e57dceSSachin Kamat  * devm_iio_device_free - Resource-managed iio_device_free()
1554a7e57dceSSachin Kamat  * @dev:		Device this iio_dev belongs to
1555a7e57dceSSachin Kamat  * @iio_dev:		the iio_dev associated with the device
1556a7e57dceSSachin Kamat  *
1557a7e57dceSSachin Kamat  * Free iio_dev allocated with devm_iio_device_alloc().
1558a7e57dceSSachin Kamat  */
15599dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
15609dabaf5eSGrygorii Strashko {
15619dabaf5eSGrygorii Strashko 	int rc;
15629dabaf5eSGrygorii Strashko 
15639dabaf5eSGrygorii Strashko 	rc = devres_release(dev, devm_iio_device_release,
15649dabaf5eSGrygorii Strashko 			    devm_iio_device_match, iio_dev);
15659dabaf5eSGrygorii Strashko 	WARN_ON(rc);
15669dabaf5eSGrygorii Strashko }
15679dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free);
15689dabaf5eSGrygorii Strashko 
1569a980e046SJonathan Cameron /**
1570a980e046SJonathan Cameron  * iio_chrdev_open() - chrdev file open for buffer access and ioctls
15712498dcf6SCristina Opriceana  * @inode:	Inode structure for identifying the device in the file system
15722498dcf6SCristina Opriceana  * @filp:	File structure for iio device used to keep and later access
15732498dcf6SCristina Opriceana  *		private data
15742498dcf6SCristina Opriceana  *
15752498dcf6SCristina Opriceana  * Return: 0 on success or -EBUSY if the device is already opened
1576a980e046SJonathan Cameron  **/
1577a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp)
1578a980e046SJonathan Cameron {
1579a980e046SJonathan Cameron 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1580a980e046SJonathan Cameron 						struct iio_dev, chrdev);
1581a980e046SJonathan Cameron 
1582a980e046SJonathan Cameron 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1583a980e046SJonathan Cameron 		return -EBUSY;
1584a980e046SJonathan Cameron 
1585cadc2125SLars-Peter Clausen 	iio_device_get(indio_dev);
1586cadc2125SLars-Peter Clausen 
1587a980e046SJonathan Cameron 	filp->private_data = indio_dev;
1588a980e046SJonathan Cameron 
1589a980e046SJonathan Cameron 	return 0;
1590a980e046SJonathan Cameron }
1591a980e046SJonathan Cameron 
1592a980e046SJonathan Cameron /**
1593a980e046SJonathan Cameron  * iio_chrdev_release() - chrdev file close buffer access and ioctls
15942498dcf6SCristina Opriceana  * @inode:	Inode structure pointer for the char device
15952498dcf6SCristina Opriceana  * @filp:	File structure pointer for the char device
15962498dcf6SCristina Opriceana  *
15972498dcf6SCristina Opriceana  * Return: 0 for successful release
15982498dcf6SCristina Opriceana  */
1599a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp)
1600a980e046SJonathan Cameron {
1601a980e046SJonathan Cameron 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1602a980e046SJonathan Cameron 						struct iio_dev, chrdev);
1603a980e046SJonathan Cameron 	clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1604cadc2125SLars-Peter Clausen 	iio_device_put(indio_dev);
1605cadc2125SLars-Peter Clausen 
1606a980e046SJonathan Cameron 	return 0;
1607a980e046SJonathan Cameron }
1608a980e046SJonathan Cameron 
1609a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually
1610a980e046SJonathan Cameron  * event related */
1611a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1612a980e046SJonathan Cameron {
1613a980e046SJonathan Cameron 	struct iio_dev *indio_dev = filp->private_data;
1614a980e046SJonathan Cameron 	int __user *ip = (int __user *)arg;
1615a980e046SJonathan Cameron 	int fd;
1616a980e046SJonathan Cameron 
1617f18e7a06SLars-Peter Clausen 	if (!indio_dev->info)
1618f18e7a06SLars-Peter Clausen 		return -ENODEV;
1619f18e7a06SLars-Peter Clausen 
1620a980e046SJonathan Cameron 	if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1621a980e046SJonathan Cameron 		fd = iio_event_getfd(indio_dev);
16223f9059b7SLinus Walleij 		if (fd < 0)
16233f9059b7SLinus Walleij 			return fd;
1624a980e046SJonathan Cameron 		if (copy_to_user(ip, &fd, sizeof(fd)))
1625a980e046SJonathan Cameron 			return -EFAULT;
1626a980e046SJonathan Cameron 		return 0;
1627a980e046SJonathan Cameron 	}
1628a980e046SJonathan Cameron 	return -EINVAL;
1629a980e046SJonathan Cameron }
1630a980e046SJonathan Cameron 
1631a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = {
1632a980e046SJonathan Cameron 	.read = iio_buffer_read_first_n_outer_addr,
1633a980e046SJonathan Cameron 	.release = iio_chrdev_release,
1634a980e046SJonathan Cameron 	.open = iio_chrdev_open,
1635a980e046SJonathan Cameron 	.poll = iio_buffer_poll_addr,
1636a980e046SJonathan Cameron 	.owner = THIS_MODULE,
1637a980e046SJonathan Cameron 	.llseek = noop_llseek,
1638a980e046SJonathan Cameron 	.unlocked_ioctl = iio_ioctl,
1639a980e046SJonathan Cameron 	.compat_ioctl = iio_ioctl,
1640a980e046SJonathan Cameron };
1641a980e046SJonathan Cameron 
16428f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
16438f5d8727SVlad Dogaru {
16448f5d8727SVlad Dogaru 	int i, j;
16458f5d8727SVlad Dogaru 	const struct iio_chan_spec *channels = indio_dev->channels;
16468f5d8727SVlad Dogaru 
16478f5d8727SVlad Dogaru 	if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
16488f5d8727SVlad Dogaru 		return 0;
16498f5d8727SVlad Dogaru 
16508f5d8727SVlad Dogaru 	for (i = 0; i < indio_dev->num_channels - 1; i++) {
16518f5d8727SVlad Dogaru 		if (channels[i].scan_index < 0)
16528f5d8727SVlad Dogaru 			continue;
16538f5d8727SVlad Dogaru 		for (j = i + 1; j < indio_dev->num_channels; j++)
16548f5d8727SVlad Dogaru 			if (channels[i].scan_index == channels[j].scan_index) {
16558f5d8727SVlad Dogaru 				dev_err(&indio_dev->dev,
16568f5d8727SVlad Dogaru 					"Duplicate scan index %d\n",
16578f5d8727SVlad Dogaru 					channels[i].scan_index);
16588f5d8727SVlad Dogaru 				return -EINVAL;
16598f5d8727SVlad Dogaru 			}
16608f5d8727SVlad Dogaru 	}
16618f5d8727SVlad Dogaru 
16628f5d8727SVlad Dogaru 	return 0;
16638f5d8727SVlad Dogaru }
16648f5d8727SVlad Dogaru 
1665a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1666a980e046SJonathan Cameron 
1667a7e57dceSSachin Kamat /**
1668a7e57dceSSachin Kamat  * iio_device_register() - register a device with the IIO subsystem
1669a7e57dceSSachin Kamat  * @indio_dev:		Device structure filled by the device driver
1670a7e57dceSSachin Kamat  **/
1671a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev)
1672a980e046SJonathan Cameron {
1673a980e046SJonathan Cameron 	int ret;
1674a980e046SJonathan Cameron 
167517d82b47SGuenter Roeck 	/* If the calling driver did not initialize of_node, do it here */
167617d82b47SGuenter Roeck 	if (!indio_dev->dev.of_node && indio_dev->dev.parent)
167717d82b47SGuenter Roeck 		indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
167817d82b47SGuenter Roeck 
16798f5d8727SVlad Dogaru 	ret = iio_check_unique_scan_index(indio_dev);
16808f5d8727SVlad Dogaru 	if (ret < 0)
16818f5d8727SVlad Dogaru 		return ret;
16828f5d8727SVlad Dogaru 
1683a980e046SJonathan Cameron 	/* configure elements for the chrdev */
1684a980e046SJonathan Cameron 	indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1685a980e046SJonathan Cameron 
1686a980e046SJonathan Cameron 	ret = iio_device_register_debugfs(indio_dev);
1687a980e046SJonathan Cameron 	if (ret) {
1688a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1689a980e046SJonathan Cameron 			"Failed to register debugfs interfaces\n");
169092825ff9SHartmut Knaack 		return ret;
1691a980e046SJonathan Cameron 	}
16923e1b6c95SLars-Peter Clausen 
16933e1b6c95SLars-Peter Clausen 	ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
16943e1b6c95SLars-Peter Clausen 	if (ret) {
16953e1b6c95SLars-Peter Clausen 		dev_err(indio_dev->dev.parent,
16963e1b6c95SLars-Peter Clausen 			"Failed to create buffer sysfs interfaces\n");
16973e1b6c95SLars-Peter Clausen 		goto error_unreg_debugfs;
16983e1b6c95SLars-Peter Clausen 	}
16993e1b6c95SLars-Peter Clausen 
1700a980e046SJonathan Cameron 	ret = iio_device_register_sysfs(indio_dev);
1701a980e046SJonathan Cameron 	if (ret) {
1702a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1703a980e046SJonathan Cameron 			"Failed to register sysfs interfaces\n");
17043e1b6c95SLars-Peter Clausen 		goto error_buffer_free_sysfs;
1705a980e046SJonathan Cameron 	}
1706a980e046SJonathan Cameron 	ret = iio_device_register_eventset(indio_dev);
1707a980e046SJonathan Cameron 	if (ret) {
1708a980e046SJonathan Cameron 		dev_err(indio_dev->dev.parent,
1709a980e046SJonathan Cameron 			"Failed to register event set\n");
1710a980e046SJonathan Cameron 		goto error_free_sysfs;
1711a980e046SJonathan Cameron 	}
1712735ad074SVladimir Barinov 	if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
1713a980e046SJonathan Cameron 		iio_device_register_trigger_consumer(indio_dev);
1714a980e046SJonathan Cameron 
1715a980e046SJonathan Cameron 	if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1716a980e046SJonathan Cameron 		indio_dev->setup_ops == NULL)
1717a980e046SJonathan Cameron 		indio_dev->setup_ops = &noop_ring_setup_ops;
1718a980e046SJonathan Cameron 
1719a980e046SJonathan Cameron 	cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1720a980e046SJonathan Cameron 	indio_dev->chrdev.owner = indio_dev->info->driver_module;
17210d5b7daeSLars-Peter Clausen 	indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
1722a980e046SJonathan Cameron 	ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
1723a980e046SJonathan Cameron 	if (ret < 0)
17240d5b7daeSLars-Peter Clausen 		goto error_unreg_eventset;
1725a980e046SJonathan Cameron 
17260d5b7daeSLars-Peter Clausen 	ret = device_add(&indio_dev->dev);
17270d5b7daeSLars-Peter Clausen 	if (ret < 0)
17280d5b7daeSLars-Peter Clausen 		goto error_cdev_del;
17290d5b7daeSLars-Peter Clausen 
17300d5b7daeSLars-Peter Clausen 	return 0;
17310d5b7daeSLars-Peter Clausen error_cdev_del:
17320d5b7daeSLars-Peter Clausen 	cdev_del(&indio_dev->chrdev);
1733a980e046SJonathan Cameron error_unreg_eventset:
1734a980e046SJonathan Cameron 	iio_device_unregister_eventset(indio_dev);
1735a980e046SJonathan Cameron error_free_sysfs:
1736a980e046SJonathan Cameron 	iio_device_unregister_sysfs(indio_dev);
17373e1b6c95SLars-Peter Clausen error_buffer_free_sysfs:
17383e1b6c95SLars-Peter Clausen 	iio_buffer_free_sysfs_and_mask(indio_dev);
1739a980e046SJonathan Cameron error_unreg_debugfs:
1740a980e046SJonathan Cameron 	iio_device_unregister_debugfs(indio_dev);
1741a980e046SJonathan Cameron 	return ret;
1742a980e046SJonathan Cameron }
1743a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register);
1744a980e046SJonathan Cameron 
1745a7e57dceSSachin Kamat /**
1746a7e57dceSSachin Kamat  * iio_device_unregister() - unregister a device from the IIO subsystem
1747a7e57dceSSachin Kamat  * @indio_dev:		Device structure representing the device.
1748a7e57dceSSachin Kamat  **/
1749a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev)
1750a980e046SJonathan Cameron {
1751a980e046SJonathan Cameron 	mutex_lock(&indio_dev->info_exist_lock);
1752a87c82e4SLars-Peter Clausen 
1753a87c82e4SLars-Peter Clausen 	device_del(&indio_dev->dev);
1754a87c82e4SLars-Peter Clausen 
17550d5b7daeSLars-Peter Clausen 	if (indio_dev->chrdev.dev)
17560d5b7daeSLars-Peter Clausen 		cdev_del(&indio_dev->chrdev);
1757bc4c9612SLars-Peter Clausen 	iio_device_unregister_debugfs(indio_dev);
17580d5b7daeSLars-Peter Clausen 
1759a87c82e4SLars-Peter Clausen 	iio_disable_all_buffers(indio_dev);
1760a87c82e4SLars-Peter Clausen 
1761a980e046SJonathan Cameron 	indio_dev->info = NULL;
1762d2f0a48fSLars-Peter Clausen 
1763d2f0a48fSLars-Peter Clausen 	iio_device_wakeup_eventset(indio_dev);
1764d2f0a48fSLars-Peter Clausen 	iio_buffer_wakeup_poll(indio_dev);
1765d2f0a48fSLars-Peter Clausen 
1766a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->info_exist_lock);
17673e1b6c95SLars-Peter Clausen 
17683e1b6c95SLars-Peter Clausen 	iio_buffer_free_sysfs_and_mask(indio_dev);
1769a980e046SJonathan Cameron }
1770a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister);
17718caa07c0SSachin Kamat 
17728caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res)
17738caa07c0SSachin Kamat {
17748caa07c0SSachin Kamat 	iio_device_unregister(*(struct iio_dev **)res);
17758caa07c0SSachin Kamat }
17768caa07c0SSachin Kamat 
17778caa07c0SSachin Kamat /**
17788caa07c0SSachin Kamat  * devm_iio_device_register - Resource-managed iio_device_register()
17798caa07c0SSachin Kamat  * @dev:	Device to allocate iio_dev for
17808caa07c0SSachin Kamat  * @indio_dev:	Device structure filled by the device driver
17818caa07c0SSachin Kamat  *
17828caa07c0SSachin Kamat  * Managed iio_device_register.  The IIO device registered with this
17838caa07c0SSachin Kamat  * function is automatically unregistered on driver detach. This function
17848caa07c0SSachin Kamat  * calls iio_device_register() internally. Refer to that function for more
17858caa07c0SSachin Kamat  * information.
17868caa07c0SSachin Kamat  *
17878caa07c0SSachin Kamat  * If an iio_dev registered with this function needs to be unregistered
17888caa07c0SSachin Kamat  * separately, devm_iio_device_unregister() must be used.
17898caa07c0SSachin Kamat  *
17908caa07c0SSachin Kamat  * RETURNS:
17918caa07c0SSachin Kamat  * 0 on success, negative error number on failure.
17928caa07c0SSachin Kamat  */
17938caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
17948caa07c0SSachin Kamat {
17958caa07c0SSachin Kamat 	struct iio_dev **ptr;
17968caa07c0SSachin Kamat 	int ret;
17978caa07c0SSachin Kamat 
17988caa07c0SSachin Kamat 	ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
17998caa07c0SSachin Kamat 	if (!ptr)
18008caa07c0SSachin Kamat 		return -ENOMEM;
18018caa07c0SSachin Kamat 
18028caa07c0SSachin Kamat 	*ptr = indio_dev;
18038caa07c0SSachin Kamat 	ret = iio_device_register(indio_dev);
18048caa07c0SSachin Kamat 	if (!ret)
18058caa07c0SSachin Kamat 		devres_add(dev, ptr);
18068caa07c0SSachin Kamat 	else
18078caa07c0SSachin Kamat 		devres_free(ptr);
18088caa07c0SSachin Kamat 
18098caa07c0SSachin Kamat 	return ret;
18108caa07c0SSachin Kamat }
18118caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register);
18128caa07c0SSachin Kamat 
18138caa07c0SSachin Kamat /**
18148caa07c0SSachin Kamat  * devm_iio_device_unregister - Resource-managed iio_device_unregister()
18158caa07c0SSachin Kamat  * @dev:	Device this iio_dev belongs to
18168caa07c0SSachin Kamat  * @indio_dev:	the iio_dev associated with the device
18178caa07c0SSachin Kamat  *
18188caa07c0SSachin Kamat  * Unregister iio_dev registered with devm_iio_device_register().
18198caa07c0SSachin Kamat  */
18208caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
18218caa07c0SSachin Kamat {
18228caa07c0SSachin Kamat 	int rc;
18238caa07c0SSachin Kamat 
18248caa07c0SSachin Kamat 	rc = devres_release(dev, devm_iio_device_unreg,
18258caa07c0SSachin Kamat 			    devm_iio_device_match, indio_dev);
18268caa07c0SSachin Kamat 	WARN_ON(rc);
18278caa07c0SSachin Kamat }
18288caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
18298caa07c0SSachin Kamat 
183008a33805SAlison Schofield /**
183108a33805SAlison Schofield  * iio_device_claim_direct_mode - Keep device in direct mode
183208a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
183308a33805SAlison Schofield  *
183408a33805SAlison Schofield  * If the device is in direct mode it is guaranteed to stay
183508a33805SAlison Schofield  * that way until iio_device_release_direct_mode() is called.
183608a33805SAlison Schofield  *
183708a33805SAlison Schofield  * Use with iio_device_release_direct_mode()
183808a33805SAlison Schofield  *
183908a33805SAlison Schofield  * Returns: 0 on success, -EBUSY on failure
184008a33805SAlison Schofield  */
184108a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
184208a33805SAlison Schofield {
184308a33805SAlison Schofield 	mutex_lock(&indio_dev->mlock);
184408a33805SAlison Schofield 
184508a33805SAlison Schofield 	if (iio_buffer_enabled(indio_dev)) {
184608a33805SAlison Schofield 		mutex_unlock(&indio_dev->mlock);
184708a33805SAlison Schofield 		return -EBUSY;
184808a33805SAlison Schofield 	}
184908a33805SAlison Schofield 	return 0;
185008a33805SAlison Schofield }
185108a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
185208a33805SAlison Schofield 
185308a33805SAlison Schofield /**
185408a33805SAlison Schofield  * iio_device_release_direct_mode - releases claim on direct mode
185508a33805SAlison Schofield  * @indio_dev:	the iio_dev associated with the device
185608a33805SAlison Schofield  *
185708a33805SAlison Schofield  * Release the claim. Device is no longer guaranteed to stay
185808a33805SAlison Schofield  * in direct mode.
185908a33805SAlison Schofield  *
186008a33805SAlison Schofield  * Use with iio_device_claim_direct_mode()
186108a33805SAlison Schofield  */
186208a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev)
186308a33805SAlison Schofield {
186408a33805SAlison Schofield 	mutex_unlock(&indio_dev->mlock);
186508a33805SAlison Schofield }
186608a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
186708a33805SAlison Schofield 
1868a980e046SJonathan Cameron subsys_initcall(iio_init);
1869a980e046SJonathan Cameron module_exit(iio_exit);
1870a980e046SJonathan Cameron 
1871c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1872a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core");
1873a980e046SJonathan Cameron MODULE_LICENSE("GPL");
1874