1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core
3  *
4  * Copyright (c) 2008 Jonathan Cameron
5  *
6  * Based on elements of hwmon and input subsystems.
7  */
8 
9 #define pr_fmt(fmt) "iio-core: " fmt
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/idr.h>
14 #include <linux/kdev_t.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
17 #include <linux/fs.h>
18 #include <linux/poll.h>
19 #include <linux/property.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
24 #include <linux/anon_inodes.h>
25 #include <linux/debugfs.h>
26 #include <linux/mutex.h>
27 #include <linux/iio/iio.h>
28 #include "iio_core.h"
29 #include "iio_core_trigger.h"
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/events.h>
32 #include <linux/iio/buffer.h>
33 #include <linux/iio/buffer_impl.h>
34 
35 /* IDA to assign each registered device a unique id */
36 static DEFINE_IDA(iio_ida);
37 
38 static dev_t iio_devt;
39 
40 #define IIO_DEV_MAX 256
41 struct bus_type iio_bus_type = {
42 	.name = "iio",
43 };
44 EXPORT_SYMBOL(iio_bus_type);
45 
46 static struct dentry *iio_debugfs_dentry;
47 
48 static const char * const iio_direction[] = {
49 	[0] = "in",
50 	[1] = "out",
51 };
52 
53 static const char * const iio_chan_type_name_spec[] = {
54 	[IIO_VOLTAGE] = "voltage",
55 	[IIO_CURRENT] = "current",
56 	[IIO_POWER] = "power",
57 	[IIO_ACCEL] = "accel",
58 	[IIO_ANGL_VEL] = "anglvel",
59 	[IIO_MAGN] = "magn",
60 	[IIO_LIGHT] = "illuminance",
61 	[IIO_INTENSITY] = "intensity",
62 	[IIO_PROXIMITY] = "proximity",
63 	[IIO_TEMP] = "temp",
64 	[IIO_INCLI] = "incli",
65 	[IIO_ROT] = "rot",
66 	[IIO_ANGL] = "angl",
67 	[IIO_TIMESTAMP] = "timestamp",
68 	[IIO_CAPACITANCE] = "capacitance",
69 	[IIO_ALTVOLTAGE] = "altvoltage",
70 	[IIO_CCT] = "cct",
71 	[IIO_PRESSURE] = "pressure",
72 	[IIO_HUMIDITYRELATIVE] = "humidityrelative",
73 	[IIO_ACTIVITY] = "activity",
74 	[IIO_STEPS] = "steps",
75 	[IIO_ENERGY] = "energy",
76 	[IIO_DISTANCE] = "distance",
77 	[IIO_VELOCITY] = "velocity",
78 	[IIO_CONCENTRATION] = "concentration",
79 	[IIO_RESISTANCE] = "resistance",
80 	[IIO_PH] = "ph",
81 	[IIO_UVINDEX] = "uvindex",
82 	[IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
83 	[IIO_COUNT] = "count",
84 	[IIO_INDEX] = "index",
85 	[IIO_GRAVITY]  = "gravity",
86 	[IIO_POSITIONRELATIVE]  = "positionrelative",
87 	[IIO_PHASE] = "phase",
88 	[IIO_MASSCONCENTRATION] = "massconcentration",
89 };
90 
91 static const char * const iio_modifier_names[] = {
92 	[IIO_MOD_X] = "x",
93 	[IIO_MOD_Y] = "y",
94 	[IIO_MOD_Z] = "z",
95 	[IIO_MOD_X_AND_Y] = "x&y",
96 	[IIO_MOD_X_AND_Z] = "x&z",
97 	[IIO_MOD_Y_AND_Z] = "y&z",
98 	[IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
99 	[IIO_MOD_X_OR_Y] = "x|y",
100 	[IIO_MOD_X_OR_Z] = "x|z",
101 	[IIO_MOD_Y_OR_Z] = "y|z",
102 	[IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
103 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
104 	[IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
105 	[IIO_MOD_LIGHT_BOTH] = "both",
106 	[IIO_MOD_LIGHT_IR] = "ir",
107 	[IIO_MOD_LIGHT_CLEAR] = "clear",
108 	[IIO_MOD_LIGHT_RED] = "red",
109 	[IIO_MOD_LIGHT_GREEN] = "green",
110 	[IIO_MOD_LIGHT_BLUE] = "blue",
111 	[IIO_MOD_LIGHT_UV] = "uv",
112 	[IIO_MOD_LIGHT_DUV] = "duv",
113 	[IIO_MOD_QUATERNION] = "quaternion",
114 	[IIO_MOD_TEMP_AMBIENT] = "ambient",
115 	[IIO_MOD_TEMP_OBJECT] = "object",
116 	[IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
117 	[IIO_MOD_NORTH_TRUE] = "from_north_true",
118 	[IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
119 	[IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
120 	[IIO_MOD_RUNNING] = "running",
121 	[IIO_MOD_JOGGING] = "jogging",
122 	[IIO_MOD_WALKING] = "walking",
123 	[IIO_MOD_STILL] = "still",
124 	[IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
125 	[IIO_MOD_I] = "i",
126 	[IIO_MOD_Q] = "q",
127 	[IIO_MOD_CO2] = "co2",
128 	[IIO_MOD_VOC] = "voc",
129 	[IIO_MOD_PM1] = "pm1",
130 	[IIO_MOD_PM2P5] = "pm2p5",
131 	[IIO_MOD_PM4] = "pm4",
132 	[IIO_MOD_PM10] = "pm10",
133 };
134 
135 /* relies on pairs of these shared then separate */
136 static const char * const iio_chan_info_postfix[] = {
137 	[IIO_CHAN_INFO_RAW] = "raw",
138 	[IIO_CHAN_INFO_PROCESSED] = "input",
139 	[IIO_CHAN_INFO_SCALE] = "scale",
140 	[IIO_CHAN_INFO_OFFSET] = "offset",
141 	[IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
142 	[IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
143 	[IIO_CHAN_INFO_PEAK] = "peak_raw",
144 	[IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
145 	[IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
146 	[IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
147 	[IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
148 	= "filter_low_pass_3db_frequency",
149 	[IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
150 	= "filter_high_pass_3db_frequency",
151 	[IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
152 	[IIO_CHAN_INFO_FREQUENCY] = "frequency",
153 	[IIO_CHAN_INFO_PHASE] = "phase",
154 	[IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
155 	[IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
156 	[IIO_CHAN_INFO_INT_TIME] = "integration_time",
157 	[IIO_CHAN_INFO_ENABLE] = "en",
158 	[IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
159 	[IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
160 	[IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
161 	[IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
162 	[IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
163 	[IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
164 	[IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
165 };
166 
167 /**
168  * iio_find_channel_from_si() - get channel from its scan index
169  * @indio_dev:		device
170  * @si:			scan index to match
171  */
172 const struct iio_chan_spec
173 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
174 {
175 	int i;
176 
177 	for (i = 0; i < indio_dev->num_channels; i++)
178 		if (indio_dev->channels[i].scan_index == si)
179 			return &indio_dev->channels[i];
180 	return NULL;
181 }
182 
183 /* This turns up an awful lot */
184 ssize_t iio_read_const_attr(struct device *dev,
185 			    struct device_attribute *attr,
186 			    char *buf)
187 {
188 	return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
189 }
190 EXPORT_SYMBOL(iio_read_const_attr);
191 
192 /**
193  * iio_device_set_clock() - Set current timestamping clock for the device
194  * @indio_dev: IIO device structure containing the device
195  * @clock_id: timestamping clock posix identifier to set.
196  */
197 int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
198 {
199 	int ret;
200 	const struct iio_event_interface *ev_int = indio_dev->event_interface;
201 
202 	ret = mutex_lock_interruptible(&indio_dev->mlock);
203 	if (ret)
204 		return ret;
205 	if ((ev_int && iio_event_enabled(ev_int)) ||
206 	    iio_buffer_enabled(indio_dev)) {
207 		mutex_unlock(&indio_dev->mlock);
208 		return -EBUSY;
209 	}
210 	indio_dev->clock_id = clock_id;
211 	mutex_unlock(&indio_dev->mlock);
212 
213 	return 0;
214 }
215 EXPORT_SYMBOL(iio_device_set_clock);
216 
217 /**
218  * iio_get_time_ns() - utility function to get a time stamp for events etc
219  * @indio_dev: device
220  */
221 s64 iio_get_time_ns(const struct iio_dev *indio_dev)
222 {
223 	struct timespec64 tp;
224 
225 	switch (iio_device_get_clock(indio_dev)) {
226 	case CLOCK_REALTIME:
227 		return ktime_get_real_ns();
228 	case CLOCK_MONOTONIC:
229 		return ktime_get_ns();
230 	case CLOCK_MONOTONIC_RAW:
231 		return ktime_get_raw_ns();
232 	case CLOCK_REALTIME_COARSE:
233 		return ktime_to_ns(ktime_get_coarse_real());
234 	case CLOCK_MONOTONIC_COARSE:
235 		ktime_get_coarse_ts64(&tp);
236 		return timespec64_to_ns(&tp);
237 	case CLOCK_BOOTTIME:
238 		return ktime_get_boottime_ns();
239 	case CLOCK_TAI:
240 		return ktime_get_clocktai_ns();
241 	default:
242 		BUG();
243 	}
244 }
245 EXPORT_SYMBOL(iio_get_time_ns);
246 
247 /**
248  * iio_get_time_res() - utility function to get time stamp clock resolution in
249  *                      nano seconds.
250  * @indio_dev: device
251  */
252 unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
253 {
254 	switch (iio_device_get_clock(indio_dev)) {
255 	case CLOCK_REALTIME:
256 	case CLOCK_MONOTONIC:
257 	case CLOCK_MONOTONIC_RAW:
258 	case CLOCK_BOOTTIME:
259 	case CLOCK_TAI:
260 		return hrtimer_resolution;
261 	case CLOCK_REALTIME_COARSE:
262 	case CLOCK_MONOTONIC_COARSE:
263 		return LOW_RES_NSEC;
264 	default:
265 		BUG();
266 	}
267 }
268 EXPORT_SYMBOL(iio_get_time_res);
269 
270 static int __init iio_init(void)
271 {
272 	int ret;
273 
274 	/* Register sysfs bus */
275 	ret  = bus_register(&iio_bus_type);
276 	if (ret < 0) {
277 		pr_err("could not register bus type\n");
278 		goto error_nothing;
279 	}
280 
281 	ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
282 	if (ret < 0) {
283 		pr_err("failed to allocate char dev region\n");
284 		goto error_unregister_bus_type;
285 	}
286 
287 	iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
288 
289 	return 0;
290 
291 error_unregister_bus_type:
292 	bus_unregister(&iio_bus_type);
293 error_nothing:
294 	return ret;
295 }
296 
297 static void __exit iio_exit(void)
298 {
299 	if (iio_devt)
300 		unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
301 	bus_unregister(&iio_bus_type);
302 	debugfs_remove(iio_debugfs_dentry);
303 }
304 
305 #if defined(CONFIG_DEBUG_FS)
306 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
307 			      size_t count, loff_t *ppos)
308 {
309 	struct iio_dev *indio_dev = file->private_data;
310 	unsigned val = 0;
311 	int ret;
312 
313 	if (*ppos > 0)
314 		return simple_read_from_buffer(userbuf, count, ppos,
315 					       indio_dev->read_buf,
316 					       indio_dev->read_buf_len);
317 
318 	ret = indio_dev->info->debugfs_reg_access(indio_dev,
319 						  indio_dev->cached_reg_addr,
320 						  0, &val);
321 	if (ret) {
322 		dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
323 		return ret;
324 	}
325 
326 	indio_dev->read_buf_len = snprintf(indio_dev->read_buf,
327 					   sizeof(indio_dev->read_buf),
328 					   "0x%X\n", val);
329 
330 	return simple_read_from_buffer(userbuf, count, ppos,
331 				       indio_dev->read_buf,
332 				       indio_dev->read_buf_len);
333 }
334 
335 static ssize_t iio_debugfs_write_reg(struct file *file,
336 		     const char __user *userbuf, size_t count, loff_t *ppos)
337 {
338 	struct iio_dev *indio_dev = file->private_data;
339 	unsigned reg, val;
340 	char buf[80];
341 	int ret;
342 
343 	count = min_t(size_t, count, (sizeof(buf)-1));
344 	if (copy_from_user(buf, userbuf, count))
345 		return -EFAULT;
346 
347 	buf[count] = 0;
348 
349 	ret = sscanf(buf, "%i %i", &reg, &val);
350 
351 	switch (ret) {
352 	case 1:
353 		indio_dev->cached_reg_addr = reg;
354 		break;
355 	case 2:
356 		indio_dev->cached_reg_addr = reg;
357 		ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
358 							  val, NULL);
359 		if (ret) {
360 			dev_err(indio_dev->dev.parent, "%s: write failed\n",
361 				__func__);
362 			return ret;
363 		}
364 		break;
365 	default:
366 		return -EINVAL;
367 	}
368 
369 	return count;
370 }
371 
372 static const struct file_operations iio_debugfs_reg_fops = {
373 	.open = simple_open,
374 	.read = iio_debugfs_read_reg,
375 	.write = iio_debugfs_write_reg,
376 };
377 
378 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
379 {
380 	debugfs_remove_recursive(indio_dev->debugfs_dentry);
381 }
382 
383 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
384 {
385 	if (indio_dev->info->debugfs_reg_access == NULL)
386 		return;
387 
388 	if (!iio_debugfs_dentry)
389 		return;
390 
391 	indio_dev->debugfs_dentry =
392 		debugfs_create_dir(dev_name(&indio_dev->dev),
393 				   iio_debugfs_dentry);
394 
395 	debugfs_create_file("direct_reg_access", 0644,
396 			    indio_dev->debugfs_dentry, indio_dev,
397 			    &iio_debugfs_reg_fops);
398 }
399 #else
400 static void iio_device_register_debugfs(struct iio_dev *indio_dev)
401 {
402 }
403 
404 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
405 {
406 }
407 #endif /* CONFIG_DEBUG_FS */
408 
409 static ssize_t iio_read_channel_ext_info(struct device *dev,
410 				     struct device_attribute *attr,
411 				     char *buf)
412 {
413 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
414 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
415 	const struct iio_chan_spec_ext_info *ext_info;
416 
417 	ext_info = &this_attr->c->ext_info[this_attr->address];
418 
419 	return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
420 }
421 
422 static ssize_t iio_write_channel_ext_info(struct device *dev,
423 				     struct device_attribute *attr,
424 				     const char *buf,
425 					 size_t len)
426 {
427 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
428 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
429 	const struct iio_chan_spec_ext_info *ext_info;
430 
431 	ext_info = &this_attr->c->ext_info[this_attr->address];
432 
433 	return ext_info->write(indio_dev, ext_info->private,
434 			       this_attr->c, buf, len);
435 }
436 
437 ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
438 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
439 {
440 	const struct iio_enum *e = (const struct iio_enum *)priv;
441 	unsigned int i;
442 	size_t len = 0;
443 
444 	if (!e->num_items)
445 		return 0;
446 
447 	for (i = 0; i < e->num_items; ++i)
448 		len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
449 
450 	/* replace last space with a newline */
451 	buf[len - 1] = '\n';
452 
453 	return len;
454 }
455 EXPORT_SYMBOL_GPL(iio_enum_available_read);
456 
457 ssize_t iio_enum_read(struct iio_dev *indio_dev,
458 	uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
459 {
460 	const struct iio_enum *e = (const struct iio_enum *)priv;
461 	int i;
462 
463 	if (!e->get)
464 		return -EINVAL;
465 
466 	i = e->get(indio_dev, chan);
467 	if (i < 0)
468 		return i;
469 	else if (i >= e->num_items)
470 		return -EINVAL;
471 
472 	return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
473 }
474 EXPORT_SYMBOL_GPL(iio_enum_read);
475 
476 ssize_t iio_enum_write(struct iio_dev *indio_dev,
477 	uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
478 	size_t len)
479 {
480 	const struct iio_enum *e = (const struct iio_enum *)priv;
481 	int ret;
482 
483 	if (!e->set)
484 		return -EINVAL;
485 
486 	ret = __sysfs_match_string(e->items, e->num_items, buf);
487 	if (ret < 0)
488 		return ret;
489 
490 	ret = e->set(indio_dev, chan, ret);
491 	return ret ? ret : len;
492 }
493 EXPORT_SYMBOL_GPL(iio_enum_write);
494 
495 static const struct iio_mount_matrix iio_mount_idmatrix = {
496 	.rotation = {
497 		"1", "0", "0",
498 		"0", "1", "0",
499 		"0", "0", "1"
500 	}
501 };
502 
503 static int iio_setup_mount_idmatrix(const struct device *dev,
504 				    struct iio_mount_matrix *matrix)
505 {
506 	*matrix = iio_mount_idmatrix;
507 	dev_info(dev, "mounting matrix not found: using identity...\n");
508 	return 0;
509 }
510 
511 ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
512 			      const struct iio_chan_spec *chan, char *buf)
513 {
514 	const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
515 					      priv)(indio_dev, chan);
516 
517 	if (IS_ERR(mtx))
518 		return PTR_ERR(mtx);
519 
520 	if (!mtx)
521 		mtx = &iio_mount_idmatrix;
522 
523 	return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
524 			mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
525 			mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
526 			mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
527 }
528 EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
529 
530 /**
531  * iio_read_mount_matrix() - retrieve iio device mounting matrix from
532  *                           device "mount-matrix" property
533  * @dev:	device the mounting matrix property is assigned to
534  * @propname:	device specific mounting matrix property name
535  * @matrix:	where to store retrieved matrix
536  *
537  * If device is assigned no mounting matrix property, a default 3x3 identity
538  * matrix will be filled in.
539  *
540  * Return: 0 if success, or a negative error code on failure.
541  */
542 int iio_read_mount_matrix(struct device *dev, const char *propname,
543 			  struct iio_mount_matrix *matrix)
544 {
545 	size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
546 	int err;
547 
548 	err = device_property_read_string_array(dev, propname,
549 						matrix->rotation, len);
550 	if (err == len)
551 		return 0;
552 
553 	if (err >= 0)
554 		/* Invalid number of matrix entries. */
555 		return -EINVAL;
556 
557 	if (err != -EINVAL)
558 		/* Invalid matrix declaration format. */
559 		return err;
560 
561 	/* Matrix was not declared at all: fallback to identity. */
562 	return iio_setup_mount_idmatrix(dev, matrix);
563 }
564 EXPORT_SYMBOL(iio_read_mount_matrix);
565 
566 static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
567 				  int size, const int *vals)
568 {
569 	unsigned long long tmp;
570 	int tmp0, tmp1;
571 	bool scale_db = false;
572 
573 	switch (type) {
574 	case IIO_VAL_INT:
575 		return snprintf(buf, len, "%d", vals[0]);
576 	case IIO_VAL_INT_PLUS_MICRO_DB:
577 		scale_db = true;
578 		/* fall through */
579 	case IIO_VAL_INT_PLUS_MICRO:
580 		if (vals[1] < 0)
581 			return snprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
582 					-vals[1], scale_db ? " dB" : "");
583 		else
584 			return snprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
585 					scale_db ? " dB" : "");
586 	case IIO_VAL_INT_PLUS_NANO:
587 		if (vals[1] < 0)
588 			return snprintf(buf, len, "-%d.%09u", abs(vals[0]),
589 					-vals[1]);
590 		else
591 			return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
592 	case IIO_VAL_FRACTIONAL:
593 		tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
594 		tmp1 = vals[1];
595 		tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
596 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
597 	case IIO_VAL_FRACTIONAL_LOG2:
598 		tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
599 		tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
600 		return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
601 	case IIO_VAL_INT_MULTIPLE:
602 	{
603 		int i;
604 		int l = 0;
605 
606 		for (i = 0; i < size; ++i) {
607 			l += snprintf(&buf[l], len - l, "%d ", vals[i]);
608 			if (l >= len)
609 				break;
610 		}
611 		return l;
612 	}
613 	case IIO_VAL_CHAR:
614 		return snprintf(buf, len, "%c", (char)vals[0]);
615 	default:
616 		return 0;
617 	}
618 }
619 
620 /**
621  * iio_format_value() - Formats a IIO value into its string representation
622  * @buf:	The buffer to which the formatted value gets written
623  *		which is assumed to be big enough (i.e. PAGE_SIZE).
624  * @type:	One of the IIO_VAL_* constants. This decides how the val
625  *		and val2 parameters are formatted.
626  * @size:	Number of IIO value entries contained in vals
627  * @vals:	Pointer to the values, exact meaning depends on the
628  *		type parameter.
629  *
630  * Return: 0 by default, a negative number on failure or the
631  *	   total number of characters written for a type that belongs
632  *	   to the IIO_VAL_* constant.
633  */
634 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
635 {
636 	ssize_t len;
637 
638 	len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
639 	if (len >= PAGE_SIZE - 1)
640 		return -EFBIG;
641 
642 	return len + sprintf(buf + len, "\n");
643 }
644 EXPORT_SYMBOL_GPL(iio_format_value);
645 
646 static ssize_t iio_read_channel_info(struct device *dev,
647 				     struct device_attribute *attr,
648 				     char *buf)
649 {
650 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
651 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
652 	int vals[INDIO_MAX_RAW_ELEMENTS];
653 	int ret;
654 	int val_len = 2;
655 
656 	if (indio_dev->info->read_raw_multi)
657 		ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
658 							INDIO_MAX_RAW_ELEMENTS,
659 							vals, &val_len,
660 							this_attr->address);
661 	else
662 		ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
663 				    &vals[0], &vals[1], this_attr->address);
664 
665 	if (ret < 0)
666 		return ret;
667 
668 	return iio_format_value(buf, ret, val_len, vals);
669 }
670 
671 static ssize_t iio_format_avail_list(char *buf, const int *vals,
672 				     int type, int length)
673 {
674 	int i;
675 	ssize_t len = 0;
676 
677 	switch (type) {
678 	case IIO_VAL_INT:
679 		for (i = 0; i < length; i++) {
680 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
681 						  type, 1, &vals[i]);
682 			if (len >= PAGE_SIZE)
683 				return -EFBIG;
684 			if (i < length - 1)
685 				len += snprintf(buf + len, PAGE_SIZE - len,
686 						" ");
687 			else
688 				len += snprintf(buf + len, PAGE_SIZE - len,
689 						"\n");
690 			if (len >= PAGE_SIZE)
691 				return -EFBIG;
692 		}
693 		break;
694 	default:
695 		for (i = 0; i < length / 2; i++) {
696 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
697 						  type, 2, &vals[i * 2]);
698 			if (len >= PAGE_SIZE)
699 				return -EFBIG;
700 			if (i < length / 2 - 1)
701 				len += snprintf(buf + len, PAGE_SIZE - len,
702 						" ");
703 			else
704 				len += snprintf(buf + len, PAGE_SIZE - len,
705 						"\n");
706 			if (len >= PAGE_SIZE)
707 				return -EFBIG;
708 		}
709 	}
710 
711 	return len;
712 }
713 
714 static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
715 {
716 	int i;
717 	ssize_t len;
718 
719 	len = snprintf(buf, PAGE_SIZE, "[");
720 	switch (type) {
721 	case IIO_VAL_INT:
722 		for (i = 0; i < 3; i++) {
723 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
724 						  type, 1, &vals[i]);
725 			if (len >= PAGE_SIZE)
726 				return -EFBIG;
727 			if (i < 2)
728 				len += snprintf(buf + len, PAGE_SIZE - len,
729 						" ");
730 			else
731 				len += snprintf(buf + len, PAGE_SIZE - len,
732 						"]\n");
733 			if (len >= PAGE_SIZE)
734 				return -EFBIG;
735 		}
736 		break;
737 	default:
738 		for (i = 0; i < 3; i++) {
739 			len += __iio_format_value(buf + len, PAGE_SIZE - len,
740 						  type, 2, &vals[i * 2]);
741 			if (len >= PAGE_SIZE)
742 				return -EFBIG;
743 			if (i < 2)
744 				len += snprintf(buf + len, PAGE_SIZE - len,
745 						" ");
746 			else
747 				len += snprintf(buf + len, PAGE_SIZE - len,
748 						"]\n");
749 			if (len >= PAGE_SIZE)
750 				return -EFBIG;
751 		}
752 	}
753 
754 	return len;
755 }
756 
757 static ssize_t iio_read_channel_info_avail(struct device *dev,
758 					   struct device_attribute *attr,
759 					   char *buf)
760 {
761 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
762 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
763 	const int *vals;
764 	int ret;
765 	int length;
766 	int type;
767 
768 	ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
769 					  &vals, &type, &length,
770 					  this_attr->address);
771 
772 	if (ret < 0)
773 		return ret;
774 	switch (ret) {
775 	case IIO_AVAIL_LIST:
776 		return iio_format_avail_list(buf, vals, type, length);
777 	case IIO_AVAIL_RANGE:
778 		return iio_format_avail_range(buf, vals, type);
779 	default:
780 		return -EINVAL;
781 	}
782 }
783 
784 /**
785  * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
786  * @str: The string to parse
787  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
788  * @integer: The integer part of the number
789  * @fract: The fractional part of the number
790  * @scale_db: True if this should parse as dB
791  *
792  * Returns 0 on success, or a negative error code if the string could not be
793  * parsed.
794  */
795 static int __iio_str_to_fixpoint(const char *str, int fract_mult,
796 				 int *integer, int *fract, bool scale_db)
797 {
798 	int i = 0, f = 0;
799 	bool integer_part = true, negative = false;
800 
801 	if (fract_mult == 0) {
802 		*fract = 0;
803 
804 		return kstrtoint(str, 0, integer);
805 	}
806 
807 	if (str[0] == '-') {
808 		negative = true;
809 		str++;
810 	} else if (str[0] == '+') {
811 		str++;
812 	}
813 
814 	while (*str) {
815 		if ('0' <= *str && *str <= '9') {
816 			if (integer_part) {
817 				i = i * 10 + *str - '0';
818 			} else {
819 				f += fract_mult * (*str - '0');
820 				fract_mult /= 10;
821 			}
822 		} else if (*str == '\n') {
823 			if (*(str + 1) == '\0')
824 				break;
825 			else
826 				return -EINVAL;
827 		} else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
828 			/* Ignore the dB suffix */
829 			str += sizeof(" dB") - 1;
830 			continue;
831 		} else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
832 			/* Ignore the dB suffix */
833 			str += sizeof("dB") - 1;
834 			continue;
835 		} else if (*str == '.' && integer_part) {
836 			integer_part = false;
837 		} else {
838 			return -EINVAL;
839 		}
840 		str++;
841 	}
842 
843 	if (negative) {
844 		if (i)
845 			i = -i;
846 		else
847 			f = -f;
848 	}
849 
850 	*integer = i;
851 	*fract = f;
852 
853 	return 0;
854 }
855 
856 /**
857  * iio_str_to_fixpoint() - Parse a fixed-point number from a string
858  * @str: The string to parse
859  * @fract_mult: Multiplier for the first decimal place, should be a power of 10
860  * @integer: The integer part of the number
861  * @fract: The fractional part of the number
862  *
863  * Returns 0 on success, or a negative error code if the string could not be
864  * parsed.
865  */
866 int iio_str_to_fixpoint(const char *str, int fract_mult,
867 			int *integer, int *fract)
868 {
869 	return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
870 }
871 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
872 
873 static ssize_t iio_write_channel_info(struct device *dev,
874 				      struct device_attribute *attr,
875 				      const char *buf,
876 				      size_t len)
877 {
878 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
879 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
880 	int ret, fract_mult = 100000;
881 	int integer, fract = 0;
882 	bool is_char = false;
883 	bool scale_db = false;
884 
885 	/* Assumes decimal - precision based on number of digits */
886 	if (!indio_dev->info->write_raw)
887 		return -EINVAL;
888 
889 	if (indio_dev->info->write_raw_get_fmt)
890 		switch (indio_dev->info->write_raw_get_fmt(indio_dev,
891 			this_attr->c, this_attr->address)) {
892 		case IIO_VAL_INT:
893 			fract_mult = 0;
894 			break;
895 		case IIO_VAL_INT_PLUS_MICRO_DB:
896 			scale_db = true;
897 			/* fall through */
898 		case IIO_VAL_INT_PLUS_MICRO:
899 			fract_mult = 100000;
900 			break;
901 		case IIO_VAL_INT_PLUS_NANO:
902 			fract_mult = 100000000;
903 			break;
904 		case IIO_VAL_CHAR:
905 			is_char = true;
906 			break;
907 		default:
908 			return -EINVAL;
909 		}
910 
911 	if (is_char) {
912 		char ch;
913 
914 		if (sscanf(buf, "%c", &ch) != 1)
915 			return -EINVAL;
916 		integer = ch;
917 	} else {
918 		ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
919 		if (ret)
920 			return ret;
921 	}
922 	ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
923 				    scale_db);
924 	if (ret)
925 		return ret;
926 
927 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
928 					 integer, fract, this_attr->address);
929 	if (ret)
930 		return ret;
931 
932 	return len;
933 }
934 
935 static
936 int __iio_device_attr_init(struct device_attribute *dev_attr,
937 			   const char *postfix,
938 			   struct iio_chan_spec const *chan,
939 			   ssize_t (*readfunc)(struct device *dev,
940 					       struct device_attribute *attr,
941 					       char *buf),
942 			   ssize_t (*writefunc)(struct device *dev,
943 						struct device_attribute *attr,
944 						const char *buf,
945 						size_t len),
946 			   enum iio_shared_by shared_by)
947 {
948 	int ret = 0;
949 	char *name = NULL;
950 	char *full_postfix;
951 	sysfs_attr_init(&dev_attr->attr);
952 
953 	/* Build up postfix of <extend_name>_<modifier>_postfix */
954 	if (chan->modified && (shared_by == IIO_SEPARATE)) {
955 		if (chan->extend_name)
956 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
957 						 iio_modifier_names[chan
958 								    ->channel2],
959 						 chan->extend_name,
960 						 postfix);
961 		else
962 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
963 						 iio_modifier_names[chan
964 								    ->channel2],
965 						 postfix);
966 	} else {
967 		if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
968 			full_postfix = kstrdup(postfix, GFP_KERNEL);
969 		else
970 			full_postfix = kasprintf(GFP_KERNEL,
971 						 "%s_%s",
972 						 chan->extend_name,
973 						 postfix);
974 	}
975 	if (full_postfix == NULL)
976 		return -ENOMEM;
977 
978 	if (chan->differential) { /* Differential can not have modifier */
979 		switch (shared_by) {
980 		case IIO_SHARED_BY_ALL:
981 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
982 			break;
983 		case IIO_SHARED_BY_DIR:
984 			name = kasprintf(GFP_KERNEL, "%s_%s",
985 						iio_direction[chan->output],
986 						full_postfix);
987 			break;
988 		case IIO_SHARED_BY_TYPE:
989 			name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
990 					    iio_direction[chan->output],
991 					    iio_chan_type_name_spec[chan->type],
992 					    iio_chan_type_name_spec[chan->type],
993 					    full_postfix);
994 			break;
995 		case IIO_SEPARATE:
996 			if (!chan->indexed) {
997 				WARN(1, "Differential channels must be indexed\n");
998 				ret = -EINVAL;
999 				goto error_free_full_postfix;
1000 			}
1001 			name = kasprintf(GFP_KERNEL,
1002 					    "%s_%s%d-%s%d_%s",
1003 					    iio_direction[chan->output],
1004 					    iio_chan_type_name_spec[chan->type],
1005 					    chan->channel,
1006 					    iio_chan_type_name_spec[chan->type],
1007 					    chan->channel2,
1008 					    full_postfix);
1009 			break;
1010 		}
1011 	} else { /* Single ended */
1012 		switch (shared_by) {
1013 		case IIO_SHARED_BY_ALL:
1014 			name = kasprintf(GFP_KERNEL, "%s", full_postfix);
1015 			break;
1016 		case IIO_SHARED_BY_DIR:
1017 			name = kasprintf(GFP_KERNEL, "%s_%s",
1018 						iio_direction[chan->output],
1019 						full_postfix);
1020 			break;
1021 		case IIO_SHARED_BY_TYPE:
1022 			name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1023 					    iio_direction[chan->output],
1024 					    iio_chan_type_name_spec[chan->type],
1025 					    full_postfix);
1026 			break;
1027 
1028 		case IIO_SEPARATE:
1029 			if (chan->indexed)
1030 				name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
1031 						    iio_direction[chan->output],
1032 						    iio_chan_type_name_spec[chan->type],
1033 						    chan->channel,
1034 						    full_postfix);
1035 			else
1036 				name = kasprintf(GFP_KERNEL, "%s_%s_%s",
1037 						    iio_direction[chan->output],
1038 						    iio_chan_type_name_spec[chan->type],
1039 						    full_postfix);
1040 			break;
1041 		}
1042 	}
1043 	if (name == NULL) {
1044 		ret = -ENOMEM;
1045 		goto error_free_full_postfix;
1046 	}
1047 	dev_attr->attr.name = name;
1048 
1049 	if (readfunc) {
1050 		dev_attr->attr.mode |= S_IRUGO;
1051 		dev_attr->show = readfunc;
1052 	}
1053 
1054 	if (writefunc) {
1055 		dev_attr->attr.mode |= S_IWUSR;
1056 		dev_attr->store = writefunc;
1057 	}
1058 
1059 error_free_full_postfix:
1060 	kfree(full_postfix);
1061 
1062 	return ret;
1063 }
1064 
1065 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1066 {
1067 	kfree(dev_attr->attr.name);
1068 }
1069 
1070 int __iio_add_chan_devattr(const char *postfix,
1071 			   struct iio_chan_spec const *chan,
1072 			   ssize_t (*readfunc)(struct device *dev,
1073 					       struct device_attribute *attr,
1074 					       char *buf),
1075 			   ssize_t (*writefunc)(struct device *dev,
1076 						struct device_attribute *attr,
1077 						const char *buf,
1078 						size_t len),
1079 			   u64 mask,
1080 			   enum iio_shared_by shared_by,
1081 			   struct device *dev,
1082 			   struct list_head *attr_list)
1083 {
1084 	int ret;
1085 	struct iio_dev_attr *iio_attr, *t;
1086 
1087 	iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
1088 	if (iio_attr == NULL)
1089 		return -ENOMEM;
1090 	ret = __iio_device_attr_init(&iio_attr->dev_attr,
1091 				     postfix, chan,
1092 				     readfunc, writefunc, shared_by);
1093 	if (ret)
1094 		goto error_iio_dev_attr_free;
1095 	iio_attr->c = chan;
1096 	iio_attr->address = mask;
1097 	list_for_each_entry(t, attr_list, l)
1098 		if (strcmp(t->dev_attr.attr.name,
1099 			   iio_attr->dev_attr.attr.name) == 0) {
1100 			if (shared_by == IIO_SEPARATE)
1101 				dev_err(dev, "tried to double register : %s\n",
1102 					t->dev_attr.attr.name);
1103 			ret = -EBUSY;
1104 			goto error_device_attr_deinit;
1105 		}
1106 	list_add(&iio_attr->l, attr_list);
1107 
1108 	return 0;
1109 
1110 error_device_attr_deinit:
1111 	__iio_device_attr_deinit(&iio_attr->dev_attr);
1112 error_iio_dev_attr_free:
1113 	kfree(iio_attr);
1114 	return ret;
1115 }
1116 
1117 static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1118 					 struct iio_chan_spec const *chan,
1119 					 enum iio_shared_by shared_by,
1120 					 const long *infomask)
1121 {
1122 	int i, ret, attrcount = 0;
1123 
1124 	for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
1125 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1126 			return -EINVAL;
1127 		ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1128 					     chan,
1129 					     &iio_read_channel_info,
1130 					     &iio_write_channel_info,
1131 					     i,
1132 					     shared_by,
1133 					     &indio_dev->dev,
1134 					     &indio_dev->channel_attr_list);
1135 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1136 			continue;
1137 		else if (ret < 0)
1138 			return ret;
1139 		attrcount++;
1140 	}
1141 
1142 	return attrcount;
1143 }
1144 
1145 static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1146 					       struct iio_chan_spec const *chan,
1147 					       enum iio_shared_by shared_by,
1148 					       const long *infomask)
1149 {
1150 	int i, ret, attrcount = 0;
1151 	char *avail_postfix;
1152 
1153 	for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1154 		if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1155 			return -EINVAL;
1156 		avail_postfix = kasprintf(GFP_KERNEL,
1157 					  "%s_available",
1158 					  iio_chan_info_postfix[i]);
1159 		if (!avail_postfix)
1160 			return -ENOMEM;
1161 
1162 		ret = __iio_add_chan_devattr(avail_postfix,
1163 					     chan,
1164 					     &iio_read_channel_info_avail,
1165 					     NULL,
1166 					     i,
1167 					     shared_by,
1168 					     &indio_dev->dev,
1169 					     &indio_dev->channel_attr_list);
1170 		kfree(avail_postfix);
1171 		if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1172 			continue;
1173 		else if (ret < 0)
1174 			return ret;
1175 		attrcount++;
1176 	}
1177 
1178 	return attrcount;
1179 }
1180 
1181 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1182 					struct iio_chan_spec const *chan)
1183 {
1184 	int ret, attrcount = 0;
1185 	const struct iio_chan_spec_ext_info *ext_info;
1186 
1187 	if (chan->channel < 0)
1188 		return 0;
1189 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1190 					    IIO_SEPARATE,
1191 					    &chan->info_mask_separate);
1192 	if (ret < 0)
1193 		return ret;
1194 	attrcount += ret;
1195 
1196 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1197 						  IIO_SEPARATE,
1198 						  &chan->
1199 						  info_mask_separate_available);
1200 	if (ret < 0)
1201 		return ret;
1202 	attrcount += ret;
1203 
1204 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1205 					    IIO_SHARED_BY_TYPE,
1206 					    &chan->info_mask_shared_by_type);
1207 	if (ret < 0)
1208 		return ret;
1209 	attrcount += ret;
1210 
1211 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1212 						  IIO_SHARED_BY_TYPE,
1213 						  &chan->
1214 						  info_mask_shared_by_type_available);
1215 	if (ret < 0)
1216 		return ret;
1217 	attrcount += ret;
1218 
1219 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1220 					    IIO_SHARED_BY_DIR,
1221 					    &chan->info_mask_shared_by_dir);
1222 	if (ret < 0)
1223 		return ret;
1224 	attrcount += ret;
1225 
1226 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1227 						  IIO_SHARED_BY_DIR,
1228 						  &chan->info_mask_shared_by_dir_available);
1229 	if (ret < 0)
1230 		return ret;
1231 	attrcount += ret;
1232 
1233 	ret = iio_device_add_info_mask_type(indio_dev, chan,
1234 					    IIO_SHARED_BY_ALL,
1235 					    &chan->info_mask_shared_by_all);
1236 	if (ret < 0)
1237 		return ret;
1238 	attrcount += ret;
1239 
1240 	ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1241 						  IIO_SHARED_BY_ALL,
1242 						  &chan->info_mask_shared_by_all_available);
1243 	if (ret < 0)
1244 		return ret;
1245 	attrcount += ret;
1246 
1247 	if (chan->ext_info) {
1248 		unsigned int i = 0;
1249 		for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1250 			ret = __iio_add_chan_devattr(ext_info->name,
1251 					chan,
1252 					ext_info->read ?
1253 					    &iio_read_channel_ext_info : NULL,
1254 					ext_info->write ?
1255 					    &iio_write_channel_ext_info : NULL,
1256 					i,
1257 					ext_info->shared,
1258 					&indio_dev->dev,
1259 					&indio_dev->channel_attr_list);
1260 			i++;
1261 			if (ret == -EBUSY && ext_info->shared)
1262 				continue;
1263 
1264 			if (ret)
1265 				return ret;
1266 
1267 			attrcount++;
1268 		}
1269 	}
1270 
1271 	return attrcount;
1272 }
1273 
1274 /**
1275  * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1276  * @attr_list: List of IIO device attributes
1277  *
1278  * This function frees the memory allocated for each of the IIO device
1279  * attributes in the list.
1280  */
1281 void iio_free_chan_devattr_list(struct list_head *attr_list)
1282 {
1283 	struct iio_dev_attr *p, *n;
1284 
1285 	list_for_each_entry_safe(p, n, attr_list, l) {
1286 		kfree(p->dev_attr.attr.name);
1287 		list_del(&p->l);
1288 		kfree(p);
1289 	}
1290 }
1291 
1292 static ssize_t iio_show_dev_name(struct device *dev,
1293 				 struct device_attribute *attr,
1294 				 char *buf)
1295 {
1296 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1297 	return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
1298 }
1299 
1300 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1301 
1302 static ssize_t iio_show_dev_label(struct device *dev,
1303 				 struct device_attribute *attr,
1304 				 char *buf)
1305 {
1306 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1307 	return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->label);
1308 }
1309 
1310 static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL);
1311 
1312 static ssize_t iio_show_timestamp_clock(struct device *dev,
1313 					struct device_attribute *attr,
1314 					char *buf)
1315 {
1316 	const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1317 	const clockid_t clk = iio_device_get_clock(indio_dev);
1318 	const char *name;
1319 	ssize_t sz;
1320 
1321 	switch (clk) {
1322 	case CLOCK_REALTIME:
1323 		name = "realtime\n";
1324 		sz = sizeof("realtime\n");
1325 		break;
1326 	case CLOCK_MONOTONIC:
1327 		name = "monotonic\n";
1328 		sz = sizeof("monotonic\n");
1329 		break;
1330 	case CLOCK_MONOTONIC_RAW:
1331 		name = "monotonic_raw\n";
1332 		sz = sizeof("monotonic_raw\n");
1333 		break;
1334 	case CLOCK_REALTIME_COARSE:
1335 		name = "realtime_coarse\n";
1336 		sz = sizeof("realtime_coarse\n");
1337 		break;
1338 	case CLOCK_MONOTONIC_COARSE:
1339 		name = "monotonic_coarse\n";
1340 		sz = sizeof("monotonic_coarse\n");
1341 		break;
1342 	case CLOCK_BOOTTIME:
1343 		name = "boottime\n";
1344 		sz = sizeof("boottime\n");
1345 		break;
1346 	case CLOCK_TAI:
1347 		name = "tai\n";
1348 		sz = sizeof("tai\n");
1349 		break;
1350 	default:
1351 		BUG();
1352 	}
1353 
1354 	memcpy(buf, name, sz);
1355 	return sz;
1356 }
1357 
1358 static ssize_t iio_store_timestamp_clock(struct device *dev,
1359 					 struct device_attribute *attr,
1360 					 const char *buf, size_t len)
1361 {
1362 	clockid_t clk;
1363 	int ret;
1364 
1365 	if (sysfs_streq(buf, "realtime"))
1366 		clk = CLOCK_REALTIME;
1367 	else if (sysfs_streq(buf, "monotonic"))
1368 		clk = CLOCK_MONOTONIC;
1369 	else if (sysfs_streq(buf, "monotonic_raw"))
1370 		clk = CLOCK_MONOTONIC_RAW;
1371 	else if (sysfs_streq(buf, "realtime_coarse"))
1372 		clk = CLOCK_REALTIME_COARSE;
1373 	else if (sysfs_streq(buf, "monotonic_coarse"))
1374 		clk = CLOCK_MONOTONIC_COARSE;
1375 	else if (sysfs_streq(buf, "boottime"))
1376 		clk = CLOCK_BOOTTIME;
1377 	else if (sysfs_streq(buf, "tai"))
1378 		clk = CLOCK_TAI;
1379 	else
1380 		return -EINVAL;
1381 
1382 	ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1383 	if (ret)
1384 		return ret;
1385 
1386 	return len;
1387 }
1388 
1389 static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1390 		   iio_show_timestamp_clock, iio_store_timestamp_clock);
1391 
1392 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1393 {
1394 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1395 	struct iio_dev_attr *p;
1396 	struct attribute **attr, *clk = NULL;
1397 
1398 	/* First count elements in any existing group */
1399 	if (indio_dev->info->attrs) {
1400 		attr = indio_dev->info->attrs->attrs;
1401 		while (*attr++ != NULL)
1402 			attrcount_orig++;
1403 	}
1404 	attrcount = attrcount_orig;
1405 	/*
1406 	 * New channel registration method - relies on the fact a group does
1407 	 * not need to be initialized if its name is NULL.
1408 	 */
1409 	if (indio_dev->channels)
1410 		for (i = 0; i < indio_dev->num_channels; i++) {
1411 			const struct iio_chan_spec *chan =
1412 				&indio_dev->channels[i];
1413 
1414 			if (chan->type == IIO_TIMESTAMP)
1415 				clk = &dev_attr_current_timestamp_clock.attr;
1416 
1417 			ret = iio_device_add_channel_sysfs(indio_dev, chan);
1418 			if (ret < 0)
1419 				goto error_clear_attrs;
1420 			attrcount += ret;
1421 		}
1422 
1423 	if (indio_dev->event_interface)
1424 		clk = &dev_attr_current_timestamp_clock.attr;
1425 
1426 	if (indio_dev->name)
1427 		attrcount++;
1428 	if (indio_dev->label)
1429 		attrcount++;
1430 	if (clk)
1431 		attrcount++;
1432 
1433 	indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
1434 						   sizeof(indio_dev->chan_attr_group.attrs[0]),
1435 						   GFP_KERNEL);
1436 	if (indio_dev->chan_attr_group.attrs == NULL) {
1437 		ret = -ENOMEM;
1438 		goto error_clear_attrs;
1439 	}
1440 	/* Copy across original attributes */
1441 	if (indio_dev->info->attrs)
1442 		memcpy(indio_dev->chan_attr_group.attrs,
1443 		       indio_dev->info->attrs->attrs,
1444 		       sizeof(indio_dev->chan_attr_group.attrs[0])
1445 		       *attrcount_orig);
1446 	attrn = attrcount_orig;
1447 	/* Add all elements from the list. */
1448 	list_for_each_entry(p, &indio_dev->channel_attr_list, l)
1449 		indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1450 	if (indio_dev->name)
1451 		indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1452 	if (indio_dev->label)
1453 		indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
1454 	if (clk)
1455 		indio_dev->chan_attr_group.attrs[attrn++] = clk;
1456 
1457 	indio_dev->groups[indio_dev->groupcounter++] =
1458 		&indio_dev->chan_attr_group;
1459 
1460 	return 0;
1461 
1462 error_clear_attrs:
1463 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1464 
1465 	return ret;
1466 }
1467 
1468 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
1469 {
1470 
1471 	iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1472 	kfree(indio_dev->chan_attr_group.attrs);
1473 	indio_dev->chan_attr_group.attrs = NULL;
1474 }
1475 
1476 static void iio_dev_release(struct device *device)
1477 {
1478 	struct iio_dev *indio_dev = dev_to_iio_dev(device);
1479 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1480 		iio_device_unregister_trigger_consumer(indio_dev);
1481 	iio_device_unregister_eventset(indio_dev);
1482 	iio_device_unregister_sysfs(indio_dev);
1483 
1484 	iio_buffer_put(indio_dev->buffer);
1485 
1486 	ida_simple_remove(&iio_ida, indio_dev->id);
1487 	kfree(indio_dev);
1488 }
1489 
1490 struct device_type iio_device_type = {
1491 	.name = "iio_device",
1492 	.release = iio_dev_release,
1493 };
1494 
1495 /**
1496  * iio_device_alloc() - allocate an iio_dev from a driver
1497  * @sizeof_priv:	Space to allocate for private structure.
1498  **/
1499 struct iio_dev *iio_device_alloc(int sizeof_priv)
1500 {
1501 	struct iio_dev *dev;
1502 	size_t alloc_size;
1503 
1504 	alloc_size = sizeof(struct iio_dev);
1505 	if (sizeof_priv) {
1506 		alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1507 		alloc_size += sizeof_priv;
1508 	}
1509 	/* ensure 32-byte alignment of whole construct ? */
1510 	alloc_size += IIO_ALIGN - 1;
1511 
1512 	dev = kzalloc(alloc_size, GFP_KERNEL);
1513 
1514 	if (dev) {
1515 		dev->dev.groups = dev->groups;
1516 		dev->dev.type = &iio_device_type;
1517 		dev->dev.bus = &iio_bus_type;
1518 		device_initialize(&dev->dev);
1519 		dev_set_drvdata(&dev->dev, (void *)dev);
1520 		mutex_init(&dev->mlock);
1521 		mutex_init(&dev->info_exist_lock);
1522 		INIT_LIST_HEAD(&dev->channel_attr_list);
1523 
1524 		dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1525 		if (dev->id < 0) {
1526 			/* cannot use a dev_err as the name isn't available */
1527 			pr_err("failed to get device id\n");
1528 			kfree(dev);
1529 			return NULL;
1530 		}
1531 		dev_set_name(&dev->dev, "iio:device%d", dev->id);
1532 		INIT_LIST_HEAD(&dev->buffer_list);
1533 	}
1534 
1535 	return dev;
1536 }
1537 EXPORT_SYMBOL(iio_device_alloc);
1538 
1539 /**
1540  * iio_device_free() - free an iio_dev from a driver
1541  * @dev:		the iio_dev associated with the device
1542  **/
1543 void iio_device_free(struct iio_dev *dev)
1544 {
1545 	if (dev)
1546 		put_device(&dev->dev);
1547 }
1548 EXPORT_SYMBOL(iio_device_free);
1549 
1550 static void devm_iio_device_release(struct device *dev, void *res)
1551 {
1552 	iio_device_free(*(struct iio_dev **)res);
1553 }
1554 
1555 int devm_iio_device_match(struct device *dev, void *res, void *data)
1556 {
1557 	struct iio_dev **r = res;
1558 	if (!r || !*r) {
1559 		WARN_ON(!r || !*r);
1560 		return 0;
1561 	}
1562 	return *r == data;
1563 }
1564 EXPORT_SYMBOL_GPL(devm_iio_device_match);
1565 
1566 /**
1567  * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1568  * @dev:		Device to allocate iio_dev for
1569  * @sizeof_priv:	Space to allocate for private structure.
1570  *
1571  * Managed iio_device_alloc. iio_dev allocated with this function is
1572  * automatically freed on driver detach.
1573  *
1574  * If an iio_dev allocated with this function needs to be freed separately,
1575  * devm_iio_device_free() must be used.
1576  *
1577  * RETURNS:
1578  * Pointer to allocated iio_dev on success, NULL on failure.
1579  */
1580 struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1581 {
1582 	struct iio_dev **ptr, *iio_dev;
1583 
1584 	ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1585 			   GFP_KERNEL);
1586 	if (!ptr)
1587 		return NULL;
1588 
1589 	iio_dev = iio_device_alloc(sizeof_priv);
1590 	if (iio_dev) {
1591 		*ptr = iio_dev;
1592 		devres_add(dev, ptr);
1593 	} else {
1594 		devres_free(ptr);
1595 	}
1596 
1597 	return iio_dev;
1598 }
1599 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1600 
1601 /**
1602  * devm_iio_device_free - Resource-managed iio_device_free()
1603  * @dev:		Device this iio_dev belongs to
1604  * @iio_dev:		the iio_dev associated with the device
1605  *
1606  * Free iio_dev allocated with devm_iio_device_alloc().
1607  */
1608 void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1609 {
1610 	int rc;
1611 
1612 	rc = devres_release(dev, devm_iio_device_release,
1613 			    devm_iio_device_match, iio_dev);
1614 	WARN_ON(rc);
1615 }
1616 EXPORT_SYMBOL_GPL(devm_iio_device_free);
1617 
1618 /**
1619  * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1620  * @inode:	Inode structure for identifying the device in the file system
1621  * @filp:	File structure for iio device used to keep and later access
1622  *		private data
1623  *
1624  * Return: 0 on success or -EBUSY if the device is already opened
1625  **/
1626 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1627 {
1628 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1629 						struct iio_dev, chrdev);
1630 
1631 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1632 		return -EBUSY;
1633 
1634 	iio_device_get(indio_dev);
1635 
1636 	filp->private_data = indio_dev;
1637 
1638 	return 0;
1639 }
1640 
1641 /**
1642  * iio_chrdev_release() - chrdev file close buffer access and ioctls
1643  * @inode:	Inode structure pointer for the char device
1644  * @filp:	File structure pointer for the char device
1645  *
1646  * Return: 0 for successful release
1647  */
1648 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1649 {
1650 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
1651 						struct iio_dev, chrdev);
1652 	clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1653 	iio_device_put(indio_dev);
1654 
1655 	return 0;
1656 }
1657 
1658 /* Somewhat of a cross file organization violation - ioctls here are actually
1659  * event related */
1660 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1661 {
1662 	struct iio_dev *indio_dev = filp->private_data;
1663 	int __user *ip = (int __user *)arg;
1664 	int fd;
1665 
1666 	if (!indio_dev->info)
1667 		return -ENODEV;
1668 
1669 	if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1670 		fd = iio_event_getfd(indio_dev);
1671 		if (fd < 0)
1672 			return fd;
1673 		if (copy_to_user(ip, &fd, sizeof(fd)))
1674 			return -EFAULT;
1675 		return 0;
1676 	}
1677 	return -EINVAL;
1678 }
1679 
1680 static const struct file_operations iio_buffer_fileops = {
1681 	.read = iio_buffer_read_outer_addr,
1682 	.release = iio_chrdev_release,
1683 	.open = iio_chrdev_open,
1684 	.poll = iio_buffer_poll_addr,
1685 	.owner = THIS_MODULE,
1686 	.llseek = noop_llseek,
1687 	.unlocked_ioctl = iio_ioctl,
1688 	.compat_ioctl = compat_ptr_ioctl,
1689 };
1690 
1691 static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1692 {
1693 	int i, j;
1694 	const struct iio_chan_spec *channels = indio_dev->channels;
1695 
1696 	if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1697 		return 0;
1698 
1699 	for (i = 0; i < indio_dev->num_channels - 1; i++) {
1700 		if (channels[i].scan_index < 0)
1701 			continue;
1702 		for (j = i + 1; j < indio_dev->num_channels; j++)
1703 			if (channels[i].scan_index == channels[j].scan_index) {
1704 				dev_err(&indio_dev->dev,
1705 					"Duplicate scan index %d\n",
1706 					channels[i].scan_index);
1707 				return -EINVAL;
1708 			}
1709 	}
1710 
1711 	return 0;
1712 }
1713 
1714 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1715 
1716 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
1717 {
1718 	int ret;
1719 
1720 	indio_dev->driver_module = this_mod;
1721 	/* If the calling driver did not initialize of_node, do it here */
1722 	if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1723 		indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1724 
1725 	indio_dev->label = of_get_property(indio_dev->dev.of_node, "label",
1726 					   NULL);
1727 
1728 	ret = iio_check_unique_scan_index(indio_dev);
1729 	if (ret < 0)
1730 		return ret;
1731 
1732 	if (!indio_dev->info)
1733 		return -EINVAL;
1734 
1735 	/* configure elements for the chrdev */
1736 	indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1737 
1738 	iio_device_register_debugfs(indio_dev);
1739 
1740 	ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1741 	if (ret) {
1742 		dev_err(indio_dev->dev.parent,
1743 			"Failed to create buffer sysfs interfaces\n");
1744 		goto error_unreg_debugfs;
1745 	}
1746 
1747 	ret = iio_device_register_sysfs(indio_dev);
1748 	if (ret) {
1749 		dev_err(indio_dev->dev.parent,
1750 			"Failed to register sysfs interfaces\n");
1751 		goto error_buffer_free_sysfs;
1752 	}
1753 	ret = iio_device_register_eventset(indio_dev);
1754 	if (ret) {
1755 		dev_err(indio_dev->dev.parent,
1756 			"Failed to register event set\n");
1757 		goto error_free_sysfs;
1758 	}
1759 	if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
1760 		iio_device_register_trigger_consumer(indio_dev);
1761 
1762 	if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1763 		indio_dev->setup_ops == NULL)
1764 		indio_dev->setup_ops = &noop_ring_setup_ops;
1765 
1766 	cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1767 
1768 	indio_dev->chrdev.owner = this_mod;
1769 
1770 	ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev);
1771 	if (ret < 0)
1772 		goto error_unreg_eventset;
1773 
1774 	return 0;
1775 
1776 error_unreg_eventset:
1777 	iio_device_unregister_eventset(indio_dev);
1778 error_free_sysfs:
1779 	iio_device_unregister_sysfs(indio_dev);
1780 error_buffer_free_sysfs:
1781 	iio_buffer_free_sysfs_and_mask(indio_dev);
1782 error_unreg_debugfs:
1783 	iio_device_unregister_debugfs(indio_dev);
1784 	return ret;
1785 }
1786 EXPORT_SYMBOL(__iio_device_register);
1787 
1788 /**
1789  * iio_device_unregister() - unregister a device from the IIO subsystem
1790  * @indio_dev:		Device structure representing the device.
1791  **/
1792 void iio_device_unregister(struct iio_dev *indio_dev)
1793 {
1794 	cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
1795 
1796 	mutex_lock(&indio_dev->info_exist_lock);
1797 
1798 	iio_device_unregister_debugfs(indio_dev);
1799 
1800 	iio_disable_all_buffers(indio_dev);
1801 
1802 	indio_dev->info = NULL;
1803 
1804 	iio_device_wakeup_eventset(indio_dev);
1805 	iio_buffer_wakeup_poll(indio_dev);
1806 
1807 	mutex_unlock(&indio_dev->info_exist_lock);
1808 
1809 	iio_buffer_free_sysfs_and_mask(indio_dev);
1810 }
1811 EXPORT_SYMBOL(iio_device_unregister);
1812 
1813 static void devm_iio_device_unreg(struct device *dev, void *res)
1814 {
1815 	iio_device_unregister(*(struct iio_dev **)res);
1816 }
1817 
1818 int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
1819 			       struct module *this_mod)
1820 {
1821 	struct iio_dev **ptr;
1822 	int ret;
1823 
1824 	ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1825 	if (!ptr)
1826 		return -ENOMEM;
1827 
1828 	*ptr = indio_dev;
1829 	ret = __iio_device_register(indio_dev, this_mod);
1830 	if (!ret)
1831 		devres_add(dev, ptr);
1832 	else
1833 		devres_free(ptr);
1834 
1835 	return ret;
1836 }
1837 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
1838 
1839 /**
1840  * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1841  * @dev:	Device this iio_dev belongs to
1842  * @indio_dev:	the iio_dev associated with the device
1843  *
1844  * Unregister iio_dev registered with devm_iio_device_register().
1845  */
1846 void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1847 {
1848 	int rc;
1849 
1850 	rc = devres_release(dev, devm_iio_device_unreg,
1851 			    devm_iio_device_match, indio_dev);
1852 	WARN_ON(rc);
1853 }
1854 EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1855 
1856 /**
1857  * iio_device_claim_direct_mode - Keep device in direct mode
1858  * @indio_dev:	the iio_dev associated with the device
1859  *
1860  * If the device is in direct mode it is guaranteed to stay
1861  * that way until iio_device_release_direct_mode() is called.
1862  *
1863  * Use with iio_device_release_direct_mode()
1864  *
1865  * Returns: 0 on success, -EBUSY on failure
1866  */
1867 int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
1868 {
1869 	mutex_lock(&indio_dev->mlock);
1870 
1871 	if (iio_buffer_enabled(indio_dev)) {
1872 		mutex_unlock(&indio_dev->mlock);
1873 		return -EBUSY;
1874 	}
1875 	return 0;
1876 }
1877 EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
1878 
1879 /**
1880  * iio_device_release_direct_mode - releases claim on direct mode
1881  * @indio_dev:	the iio_dev associated with the device
1882  *
1883  * Release the claim. Device is no longer guaranteed to stay
1884  * in direct mode.
1885  *
1886  * Use with iio_device_claim_direct_mode()
1887  */
1888 void iio_device_release_direct_mode(struct iio_dev *indio_dev)
1889 {
1890 	mutex_unlock(&indio_dev->mlock);
1891 }
1892 EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
1893 
1894 subsys_initcall(iio_init);
1895 module_exit(iio_exit);
1896 
1897 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
1898 MODULE_DESCRIPTION("Industrial I/O core");
1899 MODULE_LICENSE("GPL");
1900