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