xref: /openbmc/linux/drivers/iio/inkern.c (revision 51ad5b54)
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/slab.h>
9 #include <linux/mutex.h>
10 #include <linux/of.h>
11 
12 #include <linux/iio/iio.h>
13 #include "iio_core.h"
14 #include <linux/iio/machine.h>
15 #include <linux/iio/driver.h>
16 #include <linux/iio/consumer.h>
17 
18 struct iio_map_internal {
19 	struct iio_dev *indio_dev;
20 	struct iio_map *map;
21 	struct list_head l;
22 };
23 
24 static LIST_HEAD(iio_map_list);
25 static DEFINE_MUTEX(iio_map_list_lock);
26 
27 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
28 {
29 	int i = 0, ret = 0;
30 	struct iio_map_internal *mapi;
31 
32 	if (maps == NULL)
33 		return 0;
34 
35 	mutex_lock(&iio_map_list_lock);
36 	while (maps[i].consumer_dev_name != NULL) {
37 		mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
38 		if (mapi == NULL) {
39 			ret = -ENOMEM;
40 			goto error_ret;
41 		}
42 		mapi->map = &maps[i];
43 		mapi->indio_dev = indio_dev;
44 		list_add_tail(&mapi->l, &iio_map_list);
45 		i++;
46 	}
47 error_ret:
48 	mutex_unlock(&iio_map_list_lock);
49 
50 	return ret;
51 }
52 EXPORT_SYMBOL_GPL(iio_map_array_register);
53 
54 
55 /*
56  * Remove all map entries associated with the given iio device
57  */
58 int iio_map_array_unregister(struct iio_dev *indio_dev)
59 {
60 	int ret = -ENODEV;
61 	struct iio_map_internal *mapi, *next;
62 
63 	mutex_lock(&iio_map_list_lock);
64 	list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
65 		if (indio_dev == mapi->indio_dev) {
66 			list_del(&mapi->l);
67 			kfree(mapi);
68 			ret = 0;
69 		}
70 	}
71 	mutex_unlock(&iio_map_list_lock);
72 	return ret;
73 }
74 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
75 
76 static const struct iio_chan_spec
77 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
78 {
79 	int i;
80 	const struct iio_chan_spec *chan = NULL;
81 
82 	for (i = 0; i < indio_dev->num_channels; i++)
83 		if (indio_dev->channels[i].datasheet_name &&
84 		    strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
85 			chan = &indio_dev->channels[i];
86 			break;
87 		}
88 	return chan;
89 }
90 
91 #ifdef CONFIG_OF
92 
93 static int iio_dev_node_match(struct device *dev, const void *data)
94 {
95 	return dev->of_node == data && dev->type == &iio_device_type;
96 }
97 
98 /**
99  * __of_iio_simple_xlate - translate iiospec to the IIO channel index
100  * @indio_dev:	pointer to the iio_dev structure
101  * @iiospec:	IIO specifier as found in the device tree
102  *
103  * This is simple translation function, suitable for the most 1:1 mapped
104  * channels in IIO chips. This function performs only one sanity check:
105  * whether IIO index is less than num_channels (that is specified in the
106  * iio_dev).
107  */
108 static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
109 				const struct of_phandle_args *iiospec)
110 {
111 	if (!iiospec->args_count)
112 		return 0;
113 
114 	if (iiospec->args[0] >= indio_dev->num_channels) {
115 		dev_err(&indio_dev->dev, "invalid channel index %u\n",
116 			iiospec->args[0]);
117 		return -EINVAL;
118 	}
119 
120 	return iiospec->args[0];
121 }
122 
123 static int __of_iio_channel_get(struct iio_channel *channel,
124 				struct device_node *np, int index)
125 {
126 	struct device *idev;
127 	struct iio_dev *indio_dev;
128 	int err;
129 	struct of_phandle_args iiospec;
130 
131 	err = of_parse_phandle_with_args(np, "io-channels",
132 					 "#io-channel-cells",
133 					 index, &iiospec);
134 	if (err)
135 		return err;
136 
137 	idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
138 			       iio_dev_node_match);
139 	of_node_put(iiospec.np);
140 	if (idev == NULL)
141 		return -EPROBE_DEFER;
142 
143 	indio_dev = dev_to_iio_dev(idev);
144 	channel->indio_dev = indio_dev;
145 	if (indio_dev->info->of_xlate)
146 		index = indio_dev->info->of_xlate(indio_dev, &iiospec);
147 	else
148 		index = __of_iio_simple_xlate(indio_dev, &iiospec);
149 	if (index < 0)
150 		goto err_put;
151 	channel->channel = &indio_dev->channels[index];
152 
153 	return 0;
154 
155 err_put:
156 	iio_device_put(indio_dev);
157 	return index;
158 }
159 
160 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
161 {
162 	struct iio_channel *channel;
163 	int err;
164 
165 	if (index < 0)
166 		return ERR_PTR(-EINVAL);
167 
168 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
169 	if (channel == NULL)
170 		return ERR_PTR(-ENOMEM);
171 
172 	err = __of_iio_channel_get(channel, np, index);
173 	if (err)
174 		goto err_free_channel;
175 
176 	return channel;
177 
178 err_free_channel:
179 	kfree(channel);
180 	return ERR_PTR(err);
181 }
182 
183 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
184 						      const char *name)
185 {
186 	struct iio_channel *chan = NULL;
187 
188 	/* Walk up the tree of devices looking for a matching iio channel */
189 	while (np) {
190 		int index = 0;
191 
192 		/*
193 		 * For named iio channels, first look up the name in the
194 		 * "io-channel-names" property.  If it cannot be found, the
195 		 * index will be an error code, and of_iio_channel_get()
196 		 * will fail.
197 		 */
198 		if (name)
199 			index = of_property_match_string(np, "io-channel-names",
200 							 name);
201 		chan = of_iio_channel_get(np, index);
202 		if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
203 			break;
204 		else if (name && index >= 0) {
205 			pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
206 				np, name ? name : "", index);
207 			return NULL;
208 		}
209 
210 		/*
211 		 * No matching IIO channel found on this node.
212 		 * If the parent node has a "io-channel-ranges" property,
213 		 * then we can try one of its channels.
214 		 */
215 		np = np->parent;
216 		if (np && !of_get_property(np, "io-channel-ranges", NULL))
217 			return NULL;
218 	}
219 
220 	return chan;
221 }
222 
223 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
224 {
225 	struct iio_channel *chans;
226 	int i, mapind, nummaps = 0;
227 	int ret;
228 
229 	do {
230 		ret = of_parse_phandle_with_args(dev->of_node,
231 						 "io-channels",
232 						 "#io-channel-cells",
233 						 nummaps, NULL);
234 		if (ret < 0)
235 			break;
236 	} while (++nummaps);
237 
238 	if (nummaps == 0)	/* no error, return NULL to search map table */
239 		return NULL;
240 
241 	/* NULL terminated array to save passing size */
242 	chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
243 	if (chans == NULL)
244 		return ERR_PTR(-ENOMEM);
245 
246 	/* Search for OF matches */
247 	for (mapind = 0; mapind < nummaps; mapind++) {
248 		ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
249 					   mapind);
250 		if (ret)
251 			goto error_free_chans;
252 	}
253 	return chans;
254 
255 error_free_chans:
256 	for (i = 0; i < mapind; i++)
257 		iio_device_put(chans[i].indio_dev);
258 	kfree(chans);
259 	return ERR_PTR(ret);
260 }
261 
262 #else /* CONFIG_OF */
263 
264 static inline struct iio_channel *
265 of_iio_channel_get_by_name(struct device_node *np, const char *name)
266 {
267 	return NULL;
268 }
269 
270 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
271 {
272 	return NULL;
273 }
274 
275 #endif /* CONFIG_OF */
276 
277 static struct iio_channel *iio_channel_get_sys(const char *name,
278 					       const char *channel_name)
279 {
280 	struct iio_map_internal *c_i = NULL, *c = NULL;
281 	struct iio_channel *channel;
282 	int err;
283 
284 	if (name == NULL && channel_name == NULL)
285 		return ERR_PTR(-ENODEV);
286 
287 	/* first find matching entry the channel map */
288 	mutex_lock(&iio_map_list_lock);
289 	list_for_each_entry(c_i, &iio_map_list, l) {
290 		if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
291 		    (channel_name &&
292 		     strcmp(channel_name, c_i->map->consumer_channel) != 0))
293 			continue;
294 		c = c_i;
295 		iio_device_get(c->indio_dev);
296 		break;
297 	}
298 	mutex_unlock(&iio_map_list_lock);
299 	if (c == NULL)
300 		return ERR_PTR(-ENODEV);
301 
302 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
303 	if (channel == NULL) {
304 		err = -ENOMEM;
305 		goto error_no_mem;
306 	}
307 
308 	channel->indio_dev = c->indio_dev;
309 
310 	if (c->map->adc_channel_label) {
311 		channel->channel =
312 			iio_chan_spec_from_name(channel->indio_dev,
313 						c->map->adc_channel_label);
314 
315 		if (channel->channel == NULL) {
316 			err = -EINVAL;
317 			goto error_no_chan;
318 		}
319 	}
320 
321 	return channel;
322 
323 error_no_chan:
324 	kfree(channel);
325 error_no_mem:
326 	iio_device_put(c->indio_dev);
327 	return ERR_PTR(err);
328 }
329 
330 struct iio_channel *iio_channel_get(struct device *dev,
331 				    const char *channel_name)
332 {
333 	const char *name = dev ? dev_name(dev) : NULL;
334 	struct iio_channel *channel;
335 
336 	if (dev) {
337 		channel = of_iio_channel_get_by_name(dev->of_node,
338 						     channel_name);
339 		if (channel != NULL)
340 			return channel;
341 	}
342 
343 	return iio_channel_get_sys(name, channel_name);
344 }
345 EXPORT_SYMBOL_GPL(iio_channel_get);
346 
347 void iio_channel_release(struct iio_channel *channel)
348 {
349 	if (!channel)
350 		return;
351 	iio_device_put(channel->indio_dev);
352 	kfree(channel);
353 }
354 EXPORT_SYMBOL_GPL(iio_channel_release);
355 
356 static void devm_iio_channel_free(struct device *dev, void *res)
357 {
358 	struct iio_channel *channel = *(struct iio_channel **)res;
359 
360 	iio_channel_release(channel);
361 }
362 
363 struct iio_channel *devm_iio_channel_get(struct device *dev,
364 					 const char *channel_name)
365 {
366 	struct iio_channel **ptr, *channel;
367 
368 	ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
369 	if (!ptr)
370 		return ERR_PTR(-ENOMEM);
371 
372 	channel = iio_channel_get(dev, channel_name);
373 	if (IS_ERR(channel)) {
374 		devres_free(ptr);
375 		return channel;
376 	}
377 
378 	*ptr = channel;
379 	devres_add(dev, ptr);
380 
381 	return channel;
382 }
383 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
384 
385 struct iio_channel *iio_channel_get_all(struct device *dev)
386 {
387 	const char *name;
388 	struct iio_channel *chans;
389 	struct iio_map_internal *c = NULL;
390 	int nummaps = 0;
391 	int mapind = 0;
392 	int i, ret;
393 
394 	if (dev == NULL)
395 		return ERR_PTR(-EINVAL);
396 
397 	chans = of_iio_channel_get_all(dev);
398 	if (chans)
399 		return chans;
400 
401 	name = dev_name(dev);
402 
403 	mutex_lock(&iio_map_list_lock);
404 	/* first count the matching maps */
405 	list_for_each_entry(c, &iio_map_list, l)
406 		if (name && strcmp(name, c->map->consumer_dev_name) != 0)
407 			continue;
408 		else
409 			nummaps++;
410 
411 	if (nummaps == 0) {
412 		ret = -ENODEV;
413 		goto error_ret;
414 	}
415 
416 	/* NULL terminated array to save passing size */
417 	chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
418 	if (chans == NULL) {
419 		ret = -ENOMEM;
420 		goto error_ret;
421 	}
422 
423 	/* for each map fill in the chans element */
424 	list_for_each_entry(c, &iio_map_list, l) {
425 		if (name && strcmp(name, c->map->consumer_dev_name) != 0)
426 			continue;
427 		chans[mapind].indio_dev = c->indio_dev;
428 		chans[mapind].data = c->map->consumer_data;
429 		chans[mapind].channel =
430 			iio_chan_spec_from_name(chans[mapind].indio_dev,
431 						c->map->adc_channel_label);
432 		if (chans[mapind].channel == NULL) {
433 			ret = -EINVAL;
434 			goto error_free_chans;
435 		}
436 		iio_device_get(chans[mapind].indio_dev);
437 		mapind++;
438 	}
439 	if (mapind == 0) {
440 		ret = -ENODEV;
441 		goto error_free_chans;
442 	}
443 	mutex_unlock(&iio_map_list_lock);
444 
445 	return chans;
446 
447 error_free_chans:
448 	for (i = 0; i < nummaps; i++)
449 		iio_device_put(chans[i].indio_dev);
450 	kfree(chans);
451 error_ret:
452 	mutex_unlock(&iio_map_list_lock);
453 
454 	return ERR_PTR(ret);
455 }
456 EXPORT_SYMBOL_GPL(iio_channel_get_all);
457 
458 void iio_channel_release_all(struct iio_channel *channels)
459 {
460 	struct iio_channel *chan = &channels[0];
461 
462 	while (chan->indio_dev) {
463 		iio_device_put(chan->indio_dev);
464 		chan++;
465 	}
466 	kfree(channels);
467 }
468 EXPORT_SYMBOL_GPL(iio_channel_release_all);
469 
470 static void devm_iio_channel_free_all(struct device *dev, void *res)
471 {
472 	struct iio_channel *channels = *(struct iio_channel **)res;
473 
474 	iio_channel_release_all(channels);
475 }
476 
477 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
478 {
479 	struct iio_channel **ptr, *channels;
480 
481 	ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
482 	if (!ptr)
483 		return ERR_PTR(-ENOMEM);
484 
485 	channels = iio_channel_get_all(dev);
486 	if (IS_ERR(channels)) {
487 		devres_free(ptr);
488 		return channels;
489 	}
490 
491 	*ptr = channels;
492 	devres_add(dev, ptr);
493 
494 	return channels;
495 }
496 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
497 
498 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
499 	enum iio_chan_info_enum info)
500 {
501 	int unused;
502 	int vals[INDIO_MAX_RAW_ELEMENTS];
503 	int ret;
504 	int val_len = 2;
505 
506 	if (val2 == NULL)
507 		val2 = &unused;
508 
509 	if (!iio_channel_has_info(chan->channel, info))
510 		return -EINVAL;
511 
512 	if (chan->indio_dev->info->read_raw_multi) {
513 		ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
514 					chan->channel, INDIO_MAX_RAW_ELEMENTS,
515 					vals, &val_len, info);
516 		*val = vals[0];
517 		*val2 = vals[1];
518 	} else
519 		ret = chan->indio_dev->info->read_raw(chan->indio_dev,
520 					chan->channel, val, val2, info);
521 
522 	return ret;
523 }
524 
525 int iio_read_channel_raw(struct iio_channel *chan, int *val)
526 {
527 	int ret;
528 
529 	mutex_lock(&chan->indio_dev->info_exist_lock);
530 	if (chan->indio_dev->info == NULL) {
531 		ret = -ENODEV;
532 		goto err_unlock;
533 	}
534 
535 	ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
536 err_unlock:
537 	mutex_unlock(&chan->indio_dev->info_exist_lock);
538 
539 	return ret;
540 }
541 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
542 
543 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
544 {
545 	int ret;
546 
547 	mutex_lock(&chan->indio_dev->info_exist_lock);
548 	if (chan->indio_dev->info == NULL) {
549 		ret = -ENODEV;
550 		goto err_unlock;
551 	}
552 
553 	ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
554 err_unlock:
555 	mutex_unlock(&chan->indio_dev->info_exist_lock);
556 
557 	return ret;
558 }
559 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
560 
561 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
562 	int raw, int *processed, unsigned int scale)
563 {
564 	int scale_type, scale_val, scale_val2, offset;
565 	s64 raw64 = raw;
566 	int ret;
567 
568 	ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
569 	if (ret >= 0)
570 		raw64 += offset;
571 
572 	scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
573 					IIO_CHAN_INFO_SCALE);
574 	if (scale_type < 0) {
575 		/*
576 		 * Just pass raw values as processed if no scaling is
577 		 * available.
578 		 */
579 		*processed = raw;
580 		return 0;
581 	}
582 
583 	switch (scale_type) {
584 	case IIO_VAL_INT:
585 		*processed = raw64 * scale_val;
586 		break;
587 	case IIO_VAL_INT_PLUS_MICRO:
588 		if (scale_val2 < 0)
589 			*processed = -raw64 * scale_val;
590 		else
591 			*processed = raw64 * scale_val;
592 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
593 				      1000000LL);
594 		break;
595 	case IIO_VAL_INT_PLUS_NANO:
596 		if (scale_val2 < 0)
597 			*processed = -raw64 * scale_val;
598 		else
599 			*processed = raw64 * scale_val;
600 		*processed += div_s64(raw64 * (s64)scale_val2 * scale,
601 				      1000000000LL);
602 		break;
603 	case IIO_VAL_FRACTIONAL:
604 		*processed = div_s64(raw64 * (s64)scale_val * scale,
605 				     scale_val2);
606 		break;
607 	case IIO_VAL_FRACTIONAL_LOG2:
608 		*processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
609 		break;
610 	default:
611 		return -EINVAL;
612 	}
613 
614 	return 0;
615 }
616 
617 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
618 	int *processed, unsigned int scale)
619 {
620 	int ret;
621 
622 	mutex_lock(&chan->indio_dev->info_exist_lock);
623 	if (chan->indio_dev->info == NULL) {
624 		ret = -ENODEV;
625 		goto err_unlock;
626 	}
627 
628 	ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
629 							scale);
630 err_unlock:
631 	mutex_unlock(&chan->indio_dev->info_exist_lock);
632 
633 	return ret;
634 }
635 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
636 
637 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
638 			       enum iio_chan_info_enum attribute)
639 {
640 	int ret;
641 
642 	mutex_lock(&chan->indio_dev->info_exist_lock);
643 	if (chan->indio_dev->info == NULL) {
644 		ret = -ENODEV;
645 		goto err_unlock;
646 	}
647 
648 	ret = iio_channel_read(chan, val, val2, attribute);
649 err_unlock:
650 	mutex_unlock(&chan->indio_dev->info_exist_lock);
651 
652 	return ret;
653 }
654 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
655 
656 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
657 {
658 	return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
659 }
660 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
661 
662 int iio_read_channel_processed(struct iio_channel *chan, int *val)
663 {
664 	int ret;
665 
666 	mutex_lock(&chan->indio_dev->info_exist_lock);
667 	if (chan->indio_dev->info == NULL) {
668 		ret = -ENODEV;
669 		goto err_unlock;
670 	}
671 
672 	if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
673 		ret = iio_channel_read(chan, val, NULL,
674 				       IIO_CHAN_INFO_PROCESSED);
675 	} else {
676 		ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
677 		if (ret < 0)
678 			goto err_unlock;
679 		ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
680 	}
681 
682 err_unlock:
683 	mutex_unlock(&chan->indio_dev->info_exist_lock);
684 
685 	return ret;
686 }
687 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
688 
689 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
690 {
691 	return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
692 }
693 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
694 
695 static int iio_channel_read_avail(struct iio_channel *chan,
696 				  const int **vals, int *type, int *length,
697 				  enum iio_chan_info_enum info)
698 {
699 	if (!iio_channel_has_available(chan->channel, info))
700 		return -EINVAL;
701 
702 	return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
703 						 vals, type, length, info);
704 }
705 
706 int iio_read_avail_channel_attribute(struct iio_channel *chan,
707 				     const int **vals, int *type, int *length,
708 				     enum iio_chan_info_enum attribute)
709 {
710 	int ret;
711 
712 	mutex_lock(&chan->indio_dev->info_exist_lock);
713 	if (!chan->indio_dev->info) {
714 		ret = -ENODEV;
715 		goto err_unlock;
716 	}
717 
718 	ret = iio_channel_read_avail(chan, vals, type, length, attribute);
719 err_unlock:
720 	mutex_unlock(&chan->indio_dev->info_exist_lock);
721 
722 	return ret;
723 }
724 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
725 
726 int iio_read_avail_channel_raw(struct iio_channel *chan,
727 			       const int **vals, int *length)
728 {
729 	int ret;
730 	int type;
731 
732 	ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
733 					 IIO_CHAN_INFO_RAW);
734 
735 	if (ret >= 0 && type != IIO_VAL_INT)
736 		/* raw values are assumed to be IIO_VAL_INT */
737 		ret = -EINVAL;
738 
739 	return ret;
740 }
741 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
742 
743 static int iio_channel_read_max(struct iio_channel *chan,
744 				int *val, int *val2, int *type,
745 				enum iio_chan_info_enum info)
746 {
747 	int unused;
748 	const int *vals;
749 	int length;
750 	int ret;
751 
752 	if (!val2)
753 		val2 = &unused;
754 
755 	ret = iio_channel_read_avail(chan, &vals, type, &length, info);
756 	switch (ret) {
757 	case IIO_AVAIL_RANGE:
758 		switch (*type) {
759 		case IIO_VAL_INT:
760 			*val = vals[2];
761 			break;
762 		default:
763 			*val = vals[4];
764 			*val2 = vals[5];
765 		}
766 		return 0;
767 
768 	case IIO_AVAIL_LIST:
769 		if (length <= 0)
770 			return -EINVAL;
771 		switch (*type) {
772 		case IIO_VAL_INT:
773 			*val = vals[--length];
774 			while (length) {
775 				if (vals[--length] > *val)
776 					*val = vals[length];
777 			}
778 			break;
779 		default:
780 			/* FIXME: learn about max for other iio values */
781 			return -EINVAL;
782 		}
783 		return 0;
784 
785 	default:
786 		return ret;
787 	}
788 }
789 
790 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
791 {
792 	int ret;
793 	int type;
794 
795 	mutex_lock(&chan->indio_dev->info_exist_lock);
796 	if (!chan->indio_dev->info) {
797 		ret = -ENODEV;
798 		goto err_unlock;
799 	}
800 
801 	ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
802 err_unlock:
803 	mutex_unlock(&chan->indio_dev->info_exist_lock);
804 
805 	return ret;
806 }
807 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
808 
809 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
810 {
811 	int ret = 0;
812 	/* Need to verify underlying driver has not gone away */
813 
814 	mutex_lock(&chan->indio_dev->info_exist_lock);
815 	if (chan->indio_dev->info == NULL) {
816 		ret = -ENODEV;
817 		goto err_unlock;
818 	}
819 
820 	*type = chan->channel->type;
821 err_unlock:
822 	mutex_unlock(&chan->indio_dev->info_exist_lock);
823 
824 	return ret;
825 }
826 EXPORT_SYMBOL_GPL(iio_get_channel_type);
827 
828 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
829 			     enum iio_chan_info_enum info)
830 {
831 	return chan->indio_dev->info->write_raw(chan->indio_dev,
832 						chan->channel, val, val2, info);
833 }
834 
835 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
836 				enum iio_chan_info_enum attribute)
837 {
838 	int ret;
839 
840 	mutex_lock(&chan->indio_dev->info_exist_lock);
841 	if (chan->indio_dev->info == NULL) {
842 		ret = -ENODEV;
843 		goto err_unlock;
844 	}
845 
846 	ret = iio_channel_write(chan, val, val2, attribute);
847 err_unlock:
848 	mutex_unlock(&chan->indio_dev->info_exist_lock);
849 
850 	return ret;
851 }
852 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
853 
854 int iio_write_channel_raw(struct iio_channel *chan, int val)
855 {
856 	return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
857 }
858 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
859 
860 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
861 {
862 	const struct iio_chan_spec_ext_info *ext_info;
863 	unsigned int i = 0;
864 
865 	if (!chan->channel->ext_info)
866 		return i;
867 
868 	for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
869 		++i;
870 
871 	return i;
872 }
873 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
874 
875 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
876 						const struct iio_channel *chan,
877 						const char *attr)
878 {
879 	const struct iio_chan_spec_ext_info *ext_info;
880 
881 	if (!chan->channel->ext_info)
882 		return NULL;
883 
884 	for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
885 		if (!strcmp(attr, ext_info->name))
886 			return ext_info;
887 	}
888 
889 	return NULL;
890 }
891 
892 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
893 				  const char *attr, char *buf)
894 {
895 	const struct iio_chan_spec_ext_info *ext_info;
896 
897 	ext_info = iio_lookup_ext_info(chan, attr);
898 	if (!ext_info)
899 		return -EINVAL;
900 
901 	return ext_info->read(chan->indio_dev, ext_info->private,
902 			      chan->channel, buf);
903 }
904 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
905 
906 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
907 				   const char *buf, size_t len)
908 {
909 	const struct iio_chan_spec_ext_info *ext_info;
910 
911 	ext_info = iio_lookup_ext_info(chan, attr);
912 	if (!ext_info)
913 		return -EINVAL;
914 
915 	return ext_info->write(chan->indio_dev, ext_info->private,
916 			       chan->channel, buf, len);
917 }
918 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
919