xref: /openbmc/linux/drivers/iio/inkern.c (revision a2977c0c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core in kernel channel mapping
3  *
4  * Copyright (c) 2011 Jonathan Cameron
5  */
6 #include <linux/err.h>
7 #include <linux/export.h>
8 #include <linux/minmax.h>
9 #include <linux/mutex.h>
10 #include <linux/property.h>
11 #include <linux/slab.h>
12 
13 #include <linux/iio/iio.h>
14 #include <linux/iio/iio-opaque.h>
15 #include "iio_core.h"
16 #include <linux/iio/machine.h>
17 #include <linux/iio/driver.h>
18 #include <linux/iio/consumer.h>
19 
20 struct iio_map_internal {
21 	struct iio_dev *indio_dev;
22 	struct iio_map *map;
23 	struct list_head l;
24 };
25 
26 static LIST_HEAD(iio_map_list);
27 static DEFINE_MUTEX(iio_map_list_lock);
28 
29 static int iio_map_array_unregister_locked(struct iio_dev *indio_dev)
30 {
31 	int ret = -ENODEV;
32 	struct iio_map_internal *mapi, *next;
33 
34 	list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
35 		if (indio_dev == mapi->indio_dev) {
36 			list_del(&mapi->l);
37 			kfree(mapi);
38 			ret = 0;
39 		}
40 	}
41 	return ret;
42 }
43 
44 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
45 {
46 	int i = 0, ret = 0;
47 	struct iio_map_internal *mapi;
48 
49 	if (!maps)
50 		return 0;
51 
52 	mutex_lock(&iio_map_list_lock);
53 	while (maps[i].consumer_dev_name) {
54 		mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
55 		if (!mapi) {
56 			ret = -ENOMEM;
57 			goto error_ret;
58 		}
59 		mapi->map = &maps[i];
60 		mapi->indio_dev = indio_dev;
61 		list_add_tail(&mapi->l, &iio_map_list);
62 		i++;
63 	}
64 error_ret:
65 	if (ret)
66 		iio_map_array_unregister_locked(indio_dev);
67 	mutex_unlock(&iio_map_list_lock);
68 
69 	return ret;
70 }
71 EXPORT_SYMBOL_GPL(iio_map_array_register);
72 
73 /*
74  * Remove all map entries associated with the given iio device
75  */
76 int iio_map_array_unregister(struct iio_dev *indio_dev)
77 {
78 	int ret;
79 
80 	mutex_lock(&iio_map_list_lock);
81 	ret = iio_map_array_unregister_locked(indio_dev);
82 	mutex_unlock(&iio_map_list_lock);
83 
84 	return ret;
85 }
86 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
87 
88 static void iio_map_array_unregister_cb(void *indio_dev)
89 {
90 	iio_map_array_unregister(indio_dev);
91 }
92 
93 int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps)
94 {
95 	int ret;
96 
97 	ret = iio_map_array_register(indio_dev, maps);
98 	if (ret)
99 		return ret;
100 
101 	return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev);
102 }
103 EXPORT_SYMBOL_GPL(devm_iio_map_array_register);
104 
105 static const struct iio_chan_spec
106 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
107 {
108 	int i;
109 	const struct iio_chan_spec *chan = NULL;
110 
111 	for (i = 0; i < indio_dev->num_channels; i++)
112 		if (indio_dev->channels[i].datasheet_name &&
113 		    strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
114 			chan = &indio_dev->channels[i];
115 			break;
116 		}
117 	return chan;
118 }
119 
120 /**
121  * __fwnode_iio_simple_xlate - translate iiospec to the IIO channel index
122  * @indio_dev:	pointer to the iio_dev structure
123  * @iiospec:	IIO specifier as found in the device tree
124  *
125  * This is simple translation function, suitable for the most 1:1 mapped
126  * channels in IIO chips. This function performs only one sanity check:
127  * whether IIO index is less than num_channels (that is specified in the
128  * iio_dev).
129  */
130 static int __fwnode_iio_simple_xlate(struct iio_dev *indio_dev,
131 				     const struct fwnode_reference_args *iiospec)
132 {
133 	if (!iiospec->nargs)
134 		return 0;
135 
136 	if (iiospec->args[0] >= indio_dev->num_channels) {
137 		dev_err(&indio_dev->dev, "invalid channel index %llu\n",
138 			iiospec->args[0]);
139 		return -EINVAL;
140 	}
141 
142 	return iiospec->args[0];
143 }
144 
145 static int __fwnode_iio_channel_get(struct iio_channel *channel,
146 				    struct fwnode_handle *fwnode, int index)
147 {
148 	struct fwnode_reference_args iiospec;
149 	struct device *idev;
150 	struct iio_dev *indio_dev;
151 	int err;
152 
153 	err = fwnode_property_get_reference_args(fwnode, "io-channels",
154 						 "#io-channel-cells", 0,
155 						 index, &iiospec);
156 	if (err)
157 		return err;
158 
159 	idev = bus_find_device_by_fwnode(&iio_bus_type, iiospec.fwnode);
160 	if (!idev) {
161 		fwnode_handle_put(iiospec.fwnode);
162 		return -EPROBE_DEFER;
163 	}
164 
165 	indio_dev = dev_to_iio_dev(idev);
166 	channel->indio_dev = indio_dev;
167 	if (indio_dev->info->fwnode_xlate)
168 		index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
169 	else
170 		index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
171 	fwnode_handle_put(iiospec.fwnode);
172 	if (index < 0)
173 		goto err_put;
174 	channel->channel = &indio_dev->channels[index];
175 
176 	return 0;
177 
178 err_put:
179 	iio_device_put(indio_dev);
180 	return index;
181 }
182 
183 static struct iio_channel *fwnode_iio_channel_get(struct fwnode_handle *fwnode,
184 						  int index)
185 {
186 	struct iio_channel *channel;
187 	int err;
188 
189 	if (index < 0)
190 		return ERR_PTR(-EINVAL);
191 
192 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
193 	if (!channel)
194 		return ERR_PTR(-ENOMEM);
195 
196 	err = __fwnode_iio_channel_get(channel, fwnode, index);
197 	if (err)
198 		goto err_free_channel;
199 
200 	return channel;
201 
202 err_free_channel:
203 	kfree(channel);
204 	return ERR_PTR(err);
205 }
206 
207 static struct iio_channel *
208 __fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode, const char *name)
209 {
210 	struct iio_channel *chan;
211 	int index = 0;
212 
213 	/*
214 	 * For named iio channels, first look up the name in the
215 	 * "io-channel-names" property.  If it cannot be found, the
216 	 * index will be an error code, and fwnode_iio_channel_get()
217 	 * will fail.
218 	 */
219 	if (name)
220 		index = fwnode_property_match_string(fwnode, "io-channel-names",
221 						     name);
222 
223 	chan = fwnode_iio_channel_get(fwnode, index);
224 	if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
225 		return chan;
226 	if (name) {
227 		if (index >= 0) {
228 			pr_err("ERROR: could not get IIO channel %pfw:%s(%i)\n",
229 			       fwnode, name, index);
230 			/*
231 			 * In this case, we found 'name' in 'io-channel-names'
232 			 * but somehow we still fail so that we should not proceed
233 			 * with any other lookup. Hence, explicitly return -EINVAL
234 			 * (maybe not the better error code) so that the caller
235 			 * won't do a system lookup.
236 			 */
237 			return ERR_PTR(-EINVAL);
238 		}
239 		/*
240 		 * If index < 0, then fwnode_property_get_reference_args() fails
241 		 * with -EINVAL or -ENOENT (ACPI case) which is expected. We
242 		 * should not proceed if we get any other error.
243 		 */
244 		if (PTR_ERR(chan) != -EINVAL && PTR_ERR(chan) != -ENOENT)
245 			return chan;
246 	} else if (PTR_ERR(chan) != -ENOENT) {
247 		/*
248 		 * if !name, then we should only proceed the lookup if
249 		 * fwnode_property_get_reference_args() returns -ENOENT.
250 		 */
251 		return chan;
252 	}
253 
254 	/* so we continue the lookup */
255 	return ERR_PTR(-ENODEV);
256 }
257 
258 struct iio_channel *fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode,
259 						   const char *name)
260 {
261 	struct fwnode_handle *parent;
262 	struct iio_channel *chan;
263 
264 	/* Walk up the tree of devices looking for a matching iio channel */
265 	chan = __fwnode_iio_channel_get_by_name(fwnode, name);
266 	if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV)
267 		return chan;
268 
269 	/*
270 	 * No matching IIO channel found on this node.
271 	 * If the parent node has a "io-channel-ranges" property,
272 	 * then we can try one of its channels.
273 	 */
274 	fwnode_for_each_parent_node(fwnode, parent) {
275 		if (!fwnode_property_present(parent, "io-channel-ranges")) {
276 			fwnode_handle_put(parent);
277 			return ERR_PTR(-ENODEV);
278 		}
279 
280 		chan = __fwnode_iio_channel_get_by_name(fwnode, name);
281 		if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV) {
282 			fwnode_handle_put(parent);
283  			return chan;
284 		}
285 	}
286 
287 	return ERR_PTR(-ENODEV);
288 }
289 EXPORT_SYMBOL_GPL(fwnode_iio_channel_get_by_name);
290 
291 static struct iio_channel *fwnode_iio_channel_get_all(struct device *dev)
292 {
293 	struct fwnode_handle *fwnode = dev_fwnode(dev);
294 	struct iio_channel *chans;
295 	int i, mapind, nummaps = 0;
296 	int ret;
297 
298 	do {
299 		ret = fwnode_property_get_reference_args(fwnode, "io-channels",
300 							 "#io-channel-cells", 0,
301 							 nummaps, NULL);
302 		if (ret < 0)
303 			break;
304 	} while (++nummaps);
305 
306 	if (nummaps == 0)
307 		return ERR_PTR(-ENODEV);
308 
309 	/* NULL terminated array to save passing size */
310 	chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
311 	if (!chans)
312 		return ERR_PTR(-ENOMEM);
313 
314 	/* Search for FW matches */
315 	for (mapind = 0; mapind < nummaps; mapind++) {
316 		ret = __fwnode_iio_channel_get(&chans[mapind], fwnode, mapind);
317 		if (ret)
318 			goto error_free_chans;
319 	}
320 	return chans;
321 
322 error_free_chans:
323 	for (i = 0; i < mapind; i++)
324 		iio_device_put(chans[i].indio_dev);
325 	kfree(chans);
326 	return ERR_PTR(ret);
327 }
328 
329 static struct iio_channel *iio_channel_get_sys(const char *name,
330 					       const char *channel_name)
331 {
332 	struct iio_map_internal *c_i = NULL, *c = NULL;
333 	struct iio_channel *channel;
334 	int err;
335 
336 	if (!(name || channel_name))
337 		return ERR_PTR(-ENODEV);
338 
339 	/* first find matching entry the channel map */
340 	mutex_lock(&iio_map_list_lock);
341 	list_for_each_entry(c_i, &iio_map_list, l) {
342 		if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
343 		    (channel_name &&
344 		     strcmp(channel_name, c_i->map->consumer_channel) != 0))
345 			continue;
346 		c = c_i;
347 		iio_device_get(c->indio_dev);
348 		break;
349 	}
350 	mutex_unlock(&iio_map_list_lock);
351 	if (!c)
352 		return ERR_PTR(-ENODEV);
353 
354 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
355 	if (!channel) {
356 		err = -ENOMEM;
357 		goto error_no_mem;
358 	}
359 
360 	channel->indio_dev = c->indio_dev;
361 
362 	if (c->map->adc_channel_label) {
363 		channel->channel =
364 			iio_chan_spec_from_name(channel->indio_dev,
365 						c->map->adc_channel_label);
366 
367 		if (!channel->channel) {
368 			err = -EINVAL;
369 			goto error_no_chan;
370 		}
371 	}
372 
373 	return channel;
374 
375 error_no_chan:
376 	kfree(channel);
377 error_no_mem:
378 	iio_device_put(c->indio_dev);
379 	return ERR_PTR(err);
380 }
381 
382 struct iio_channel *iio_channel_get(struct device *dev,
383 				    const char *channel_name)
384 {
385 	const char *name = dev ? dev_name(dev) : NULL;
386 	struct iio_channel *channel;
387 
388 	if (dev) {
389 		channel = fwnode_iio_channel_get_by_name(dev_fwnode(dev),
390 							 channel_name);
391 		if (!IS_ERR(channel) || PTR_ERR(channel) != -ENODEV)
392 			return channel;
393 	}
394 
395 	return iio_channel_get_sys(name, channel_name);
396 }
397 EXPORT_SYMBOL_GPL(iio_channel_get);
398 
399 void iio_channel_release(struct iio_channel *channel)
400 {
401 	if (!channel)
402 		return;
403 	iio_device_put(channel->indio_dev);
404 	kfree(channel);
405 }
406 EXPORT_SYMBOL_GPL(iio_channel_release);
407 
408 static void devm_iio_channel_free(void *iio_channel)
409 {
410 	iio_channel_release(iio_channel);
411 }
412 
413 struct iio_channel *devm_iio_channel_get(struct device *dev,
414 					 const char *channel_name)
415 {
416 	struct iio_channel *channel;
417 	int ret;
418 
419 	channel = iio_channel_get(dev, channel_name);
420 	if (IS_ERR(channel))
421 		return channel;
422 
423 	ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
424 	if (ret)
425 		return ERR_PTR(ret);
426 
427 	return channel;
428 }
429 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
430 
431 struct iio_channel *devm_fwnode_iio_channel_get_by_name(struct device *dev,
432 							struct fwnode_handle *fwnode,
433 							const char *channel_name)
434 {
435 	struct iio_channel *channel;
436 	int ret;
437 
438 	channel = fwnode_iio_channel_get_by_name(fwnode, channel_name);
439 	if (IS_ERR(channel))
440 		return channel;
441 
442 	ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
443 	if (ret)
444 		return ERR_PTR(ret);
445 
446 	return channel;
447 }
448 EXPORT_SYMBOL_GPL(devm_fwnode_iio_channel_get_by_name);
449 
450 struct iio_channel *iio_channel_get_all(struct device *dev)
451 {
452 	const char *name;
453 	struct iio_channel *chans;
454 	struct iio_map_internal *c = NULL;
455 	int nummaps = 0;
456 	int mapind = 0;
457 	int i, ret;
458 
459 	if (!dev)
460 		return ERR_PTR(-EINVAL);
461 
462 	chans = fwnode_iio_channel_get_all(dev);
463 	/*
464 	 * We only want to carry on if the error is -ENODEV.  Anything else
465 	 * should be reported up the stack.
466 	 */
467 	if (!IS_ERR(chans) || PTR_ERR(chans) != -ENODEV)
468 		return chans;
469 
470 	name = dev_name(dev);
471 
472 	mutex_lock(&iio_map_list_lock);
473 	/* first count the matching maps */
474 	list_for_each_entry(c, &iio_map_list, l)
475 		if (name && strcmp(name, c->map->consumer_dev_name) != 0)
476 			continue;
477 		else
478 			nummaps++;
479 
480 	if (nummaps == 0) {
481 		ret = -ENODEV;
482 		goto error_ret;
483 	}
484 
485 	/* NULL terminated array to save passing size */
486 	chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
487 	if (!chans) {
488 		ret = -ENOMEM;
489 		goto error_ret;
490 	}
491 
492 	/* for each map fill in the chans element */
493 	list_for_each_entry(c, &iio_map_list, l) {
494 		if (name && strcmp(name, c->map->consumer_dev_name) != 0)
495 			continue;
496 		chans[mapind].indio_dev = c->indio_dev;
497 		chans[mapind].data = c->map->consumer_data;
498 		chans[mapind].channel =
499 			iio_chan_spec_from_name(chans[mapind].indio_dev,
500 						c->map->adc_channel_label);
501 		if (!chans[mapind].channel) {
502 			ret = -EINVAL;
503 			goto error_free_chans;
504 		}
505 		iio_device_get(chans[mapind].indio_dev);
506 		mapind++;
507 	}
508 	if (mapind == 0) {
509 		ret = -ENODEV;
510 		goto error_free_chans;
511 	}
512 	mutex_unlock(&iio_map_list_lock);
513 
514 	return chans;
515 
516 error_free_chans:
517 	for (i = 0; i < nummaps; i++)
518 		iio_device_put(chans[i].indio_dev);
519 	kfree(chans);
520 error_ret:
521 	mutex_unlock(&iio_map_list_lock);
522 
523 	return ERR_PTR(ret);
524 }
525 EXPORT_SYMBOL_GPL(iio_channel_get_all);
526 
527 void iio_channel_release_all(struct iio_channel *channels)
528 {
529 	struct iio_channel *chan = &channels[0];
530 
531 	while (chan->indio_dev) {
532 		iio_device_put(chan->indio_dev);
533 		chan++;
534 	}
535 	kfree(channels);
536 }
537 EXPORT_SYMBOL_GPL(iio_channel_release_all);
538 
539 static void devm_iio_channel_free_all(void *iio_channels)
540 {
541 	iio_channel_release_all(iio_channels);
542 }
543 
544 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
545 {
546 	struct iio_channel *channels;
547 	int ret;
548 
549 	channels = iio_channel_get_all(dev);
550 	if (IS_ERR(channels))
551 		return channels;
552 
553 	ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
554 				       channels);
555 	if (ret)
556 		return ERR_PTR(ret);
557 
558 	return channels;
559 }
560 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
561 
562 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
563 			    enum iio_chan_info_enum info)
564 {
565 	const struct iio_info *iio_info = chan->indio_dev->info;
566 	int unused;
567 	int vals[INDIO_MAX_RAW_ELEMENTS];
568 	int ret;
569 	int val_len = 2;
570 
571 	if (!val2)
572 		val2 = &unused;
573 
574 	if (!iio_channel_has_info(chan->channel, info))
575 		return -EINVAL;
576 
577 	if (iio_info->read_raw_multi) {
578 		ret = iio_info->read_raw_multi(chan->indio_dev,
579 					       chan->channel,
580 					       INDIO_MAX_RAW_ELEMENTS,
581 					       vals, &val_len, info);
582 		*val = vals[0];
583 		*val2 = vals[1];
584 	} else if (iio_info->read_raw) {
585 		ret = iio_info->read_raw(chan->indio_dev,
586 					 chan->channel, val, val2, info);
587 	} else {
588 		return -EINVAL;
589 	}
590 
591 	return ret;
592 }
593 
594 int iio_read_channel_raw(struct iio_channel *chan, int *val)
595 {
596 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
597 	int ret;
598 
599 	mutex_lock(&iio_dev_opaque->info_exist_lock);
600 	if (!chan->indio_dev->info) {
601 		ret = -ENODEV;
602 		goto err_unlock;
603 	}
604 
605 	ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
606 err_unlock:
607 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
608 
609 	return ret;
610 }
611 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
612 
613 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
614 {
615 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
616 	int ret;
617 
618 	mutex_lock(&iio_dev_opaque->info_exist_lock);
619 	if (!chan->indio_dev->info) {
620 		ret = -ENODEV;
621 		goto err_unlock;
622 	}
623 
624 	ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
625 err_unlock:
626 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
627 
628 	return ret;
629 }
630 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
631 
632 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
633 						 int raw, int *processed,
634 						 unsigned int scale)
635 {
636 	int scale_type, scale_val, scale_val2;
637 	int offset_type, offset_val, offset_val2;
638 	s64 raw64 = raw;
639 
640 	offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
641 				       IIO_CHAN_INFO_OFFSET);
642 	if (offset_type >= 0) {
643 		switch (offset_type) {
644 		case IIO_VAL_INT:
645 			break;
646 		case IIO_VAL_INT_PLUS_MICRO:
647 		case IIO_VAL_INT_PLUS_NANO:
648 			/*
649 			 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
650 			 * implicitely truncate the offset to it's integer form.
651 			 */
652 			break;
653 		case IIO_VAL_FRACTIONAL:
654 			offset_val /= offset_val2;
655 			break;
656 		case IIO_VAL_FRACTIONAL_LOG2:
657 			offset_val >>= offset_val2;
658 			break;
659 		default:
660 			return -EINVAL;
661 		}
662 
663 		raw64 += offset_val;
664 	}
665 
666 	scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
667 				      IIO_CHAN_INFO_SCALE);
668 	if (scale_type < 0) {
669 		/*
670 		 * If no channel scaling is available apply consumer scale to
671 		 * raw value and return.
672 		 */
673 		*processed = raw * scale;
674 		return 0;
675 	}
676 
677 	switch (scale_type) {
678 	case IIO_VAL_INT:
679 		*processed = raw64 * scale_val * scale;
680 		break;
681 	case IIO_VAL_INT_PLUS_MICRO:
682 		if (scale_val2 < 0)
683 			*processed = -raw64 * scale_val * scale;
684 		else
685 			*processed = raw64 * scale_val * scale;
686 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
687 				      1000000LL);
688 		break;
689 	case IIO_VAL_INT_PLUS_NANO:
690 		if (scale_val2 < 0)
691 			*processed = -raw64 * scale_val * scale;
692 		else
693 			*processed = raw64 * scale_val * scale;
694 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
695 				      1000000000LL);
696 		break;
697 	case IIO_VAL_FRACTIONAL:
698 		*processed = div_s64(raw64 * (s64)scale_val * scale,
699 				     scale_val2);
700 		break;
701 	case IIO_VAL_FRACTIONAL_LOG2:
702 		*processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
703 		break;
704 	default:
705 		return -EINVAL;
706 	}
707 
708 	return 0;
709 }
710 
711 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
712 				 int *processed, unsigned int scale)
713 {
714 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
715 	int ret;
716 
717 	mutex_lock(&iio_dev_opaque->info_exist_lock);
718 	if (!chan->indio_dev->info) {
719 		ret = -ENODEV;
720 		goto err_unlock;
721 	}
722 
723 	ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
724 						    scale);
725 err_unlock:
726 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
727 
728 	return ret;
729 }
730 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
731 
732 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
733 			       enum iio_chan_info_enum attribute)
734 {
735 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
736 	int ret;
737 
738 	mutex_lock(&iio_dev_opaque->info_exist_lock);
739 	if (!chan->indio_dev->info) {
740 		ret = -ENODEV;
741 		goto err_unlock;
742 	}
743 
744 	ret = iio_channel_read(chan, val, val2, attribute);
745 err_unlock:
746 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
747 
748 	return ret;
749 }
750 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
751 
752 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
753 {
754 	return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
755 }
756 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
757 
758 int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
759 				     unsigned int scale)
760 {
761 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
762 	int ret;
763 
764 	mutex_lock(&iio_dev_opaque->info_exist_lock);
765 	if (!chan->indio_dev->info) {
766 		ret = -ENODEV;
767 		goto err_unlock;
768 	}
769 
770 	if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
771 		ret = iio_channel_read(chan, val, NULL,
772 				       IIO_CHAN_INFO_PROCESSED);
773 		if (ret < 0)
774 			goto err_unlock;
775 		*val *= scale;
776 	} else {
777 		ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
778 		if (ret < 0)
779 			goto err_unlock;
780 		ret = iio_convert_raw_to_processed_unlocked(chan, *val, val,
781 							    scale);
782 	}
783 
784 err_unlock:
785 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
786 
787 	return ret;
788 }
789 EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
790 
791 int iio_read_channel_processed(struct iio_channel *chan, int *val)
792 {
793 	/* This is just a special case with scale factor 1 */
794 	return iio_read_channel_processed_scale(chan, val, 1);
795 }
796 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
797 
798 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
799 {
800 	return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
801 }
802 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
803 
804 static int iio_channel_read_avail(struct iio_channel *chan,
805 				  const int **vals, int *type, int *length,
806 				  enum iio_chan_info_enum info)
807 {
808 	const struct iio_info *iio_info = chan->indio_dev->info;
809 
810 	if (!iio_channel_has_available(chan->channel, info))
811 		return -EINVAL;
812 
813 	if (iio_info->read_avail)
814 		return iio_info->read_avail(chan->indio_dev, chan->channel,
815 					    vals, type, length, info);
816 	return -EINVAL;
817 }
818 
819 int iio_read_avail_channel_attribute(struct iio_channel *chan,
820 				     const int **vals, int *type, int *length,
821 				     enum iio_chan_info_enum attribute)
822 {
823 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
824 	int ret;
825 
826 	mutex_lock(&iio_dev_opaque->info_exist_lock);
827 	if (!chan->indio_dev->info) {
828 		ret = -ENODEV;
829 		goto err_unlock;
830 	}
831 
832 	ret = iio_channel_read_avail(chan, vals, type, length, attribute);
833 err_unlock:
834 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
835 
836 	return ret;
837 }
838 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
839 
840 int iio_read_avail_channel_raw(struct iio_channel *chan,
841 			       const int **vals, int *length)
842 {
843 	int ret;
844 	int type;
845 
846 	ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
847 					       IIO_CHAN_INFO_RAW);
848 
849 	if (ret >= 0 && type != IIO_VAL_INT)
850 		/* raw values are assumed to be IIO_VAL_INT */
851 		ret = -EINVAL;
852 
853 	return ret;
854 }
855 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
856 
857 static int iio_channel_read_max(struct iio_channel *chan,
858 				int *val, int *val2, int *type,
859 				enum iio_chan_info_enum info)
860 {
861 	const int *vals;
862 	int length;
863 	int ret;
864 
865 	ret = iio_channel_read_avail(chan, &vals, type, &length, info);
866 	if (ret < 0)
867 		return ret;
868 
869 	switch (ret) {
870 	case IIO_AVAIL_RANGE:
871 		switch (*type) {
872 		case IIO_VAL_INT:
873 			*val = vals[2];
874 			break;
875 		default:
876 			*val = vals[4];
877 			if (val2)
878 				*val2 = vals[5];
879 		}
880 		return 0;
881 
882 	case IIO_AVAIL_LIST:
883 		if (length <= 0)
884 			return -EINVAL;
885 		switch (*type) {
886 		case IIO_VAL_INT:
887 			*val = max_array(vals, length);
888 			break;
889 		default:
890 			/* TODO: learn about max for other iio values */
891 			return -EINVAL;
892 		}
893 		return 0;
894 
895 	default:
896 		return -EINVAL;
897 	}
898 }
899 
900 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
901 {
902 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
903 	int ret;
904 	int type;
905 
906 	mutex_lock(&iio_dev_opaque->info_exist_lock);
907 	if (!chan->indio_dev->info) {
908 		ret = -ENODEV;
909 		goto err_unlock;
910 	}
911 
912 	ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
913 err_unlock:
914 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
915 
916 	return ret;
917 }
918 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
919 
920 static int iio_channel_read_min(struct iio_channel *chan,
921 				int *val, int *val2, int *type,
922 				enum iio_chan_info_enum info)
923 {
924 	const int *vals;
925 	int length;
926 	int ret;
927 
928 	ret = iio_channel_read_avail(chan, &vals, type, &length, info);
929 	if (ret < 0)
930 		return ret;
931 
932 	switch (ret) {
933 	case IIO_AVAIL_RANGE:
934 		switch (*type) {
935 		case IIO_VAL_INT:
936 			*val = vals[0];
937 			break;
938 		default:
939 			*val = vals[0];
940 			if (val2)
941 				*val2 = vals[1];
942 		}
943 		return 0;
944 
945 	case IIO_AVAIL_LIST:
946 		if (length <= 0)
947 			return -EINVAL;
948 		switch (*type) {
949 		case IIO_VAL_INT:
950 			*val = min_array(vals, length);
951 			break;
952 		default:
953 			/* TODO: learn about min for other iio values */
954 			return -EINVAL;
955 		}
956 		return 0;
957 
958 	default:
959 		return -EINVAL;
960 	}
961 }
962 
963 int iio_read_min_channel_raw(struct iio_channel *chan, int *val)
964 {
965 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
966 	int ret;
967 	int type;
968 
969 	mutex_lock(&iio_dev_opaque->info_exist_lock);
970 	if (!chan->indio_dev->info) {
971 		ret = -ENODEV;
972 		goto err_unlock;
973 	}
974 
975 	ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
976 err_unlock:
977 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
978 
979 	return ret;
980 }
981 EXPORT_SYMBOL_GPL(iio_read_min_channel_raw);
982 
983 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
984 {
985 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
986 	int ret = 0;
987 	/* Need to verify underlying driver has not gone away */
988 
989 	mutex_lock(&iio_dev_opaque->info_exist_lock);
990 	if (!chan->indio_dev->info) {
991 		ret = -ENODEV;
992 		goto err_unlock;
993 	}
994 
995 	*type = chan->channel->type;
996 err_unlock:
997 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
998 
999 	return ret;
1000 }
1001 EXPORT_SYMBOL_GPL(iio_get_channel_type);
1002 
1003 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
1004 			     enum iio_chan_info_enum info)
1005 {
1006 	const struct iio_info *iio_info = chan->indio_dev->info;
1007 
1008 	if (iio_info->write_raw)
1009 		return iio_info->write_raw(chan->indio_dev,
1010 					   chan->channel, val, val2, info);
1011 	return -EINVAL;
1012 }
1013 
1014 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
1015 				enum iio_chan_info_enum attribute)
1016 {
1017 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
1018 	int ret;
1019 
1020 	mutex_lock(&iio_dev_opaque->info_exist_lock);
1021 	if (!chan->indio_dev->info) {
1022 		ret = -ENODEV;
1023 		goto err_unlock;
1024 	}
1025 
1026 	ret = iio_channel_write(chan, val, val2, attribute);
1027 err_unlock:
1028 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
1029 
1030 	return ret;
1031 }
1032 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
1033 
1034 int iio_write_channel_raw(struct iio_channel *chan, int val)
1035 {
1036 	return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
1037 }
1038 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
1039 
1040 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
1041 {
1042 	const struct iio_chan_spec_ext_info *ext_info;
1043 	unsigned int i = 0;
1044 
1045 	if (!chan->channel->ext_info)
1046 		return i;
1047 
1048 	for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
1049 		++i;
1050 
1051 	return i;
1052 }
1053 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
1054 
1055 static const struct iio_chan_spec_ext_info *
1056 iio_lookup_ext_info(const struct iio_channel *chan, const char *attr)
1057 {
1058 	const struct iio_chan_spec_ext_info *ext_info;
1059 
1060 	if (!chan->channel->ext_info)
1061 		return NULL;
1062 
1063 	for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
1064 		if (!strcmp(attr, ext_info->name))
1065 			return ext_info;
1066 	}
1067 
1068 	return NULL;
1069 }
1070 
1071 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
1072 				  const char *attr, char *buf)
1073 {
1074 	const struct iio_chan_spec_ext_info *ext_info;
1075 
1076 	ext_info = iio_lookup_ext_info(chan, attr);
1077 	if (!ext_info)
1078 		return -EINVAL;
1079 
1080 	return ext_info->read(chan->indio_dev, ext_info->private,
1081 			      chan->channel, buf);
1082 }
1083 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
1084 
1085 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
1086 				   const char *buf, size_t len)
1087 {
1088 	const struct iio_chan_spec_ext_info *ext_info;
1089 
1090 	ext_info = iio_lookup_ext_info(chan, attr);
1091 	if (!ext_info)
1092 		return -EINVAL;
1093 
1094 	return ext_info->write(chan->indio_dev, ext_info->private,
1095 			       chan->channel, buf, len);
1096 }
1097 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
1098