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_get(struct device * dev,const char * channel_name)365 struct iio_channel *devm_iio_channel_get(struct device *dev,
366 const char *channel_name)
367 {
368 struct iio_channel **ptr, *channel;
369
370 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
371 if (!ptr)
372 return ERR_PTR(-ENOMEM);
373
374 channel = iio_channel_get(dev, channel_name);
375 if (IS_ERR(channel)) {
376 devres_free(ptr);
377 return channel;
378 }
379
380 *ptr = channel;
381 devres_add(dev, ptr);
382
383 return channel;
384 }
385 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
386
iio_channel_get_all(struct device * dev)387 struct iio_channel *iio_channel_get_all(struct device *dev)
388 {
389 const char *name;
390 struct iio_channel *chans;
391 struct iio_map_internal *c = NULL;
392 int nummaps = 0;
393 int mapind = 0;
394 int i, ret;
395
396 if (dev == NULL)
397 return ERR_PTR(-EINVAL);
398
399 chans = of_iio_channel_get_all(dev);
400 if (chans)
401 return chans;
402
403 name = dev_name(dev);
404
405 mutex_lock(&iio_map_list_lock);
406 /* first count the matching maps */
407 list_for_each_entry(c, &iio_map_list, l)
408 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
409 continue;
410 else
411 nummaps++;
412
413 if (nummaps == 0) {
414 ret = -ENODEV;
415 goto error_ret;
416 }
417
418 /* NULL terminated array to save passing size */
419 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
420 if (chans == NULL) {
421 ret = -ENOMEM;
422 goto error_ret;
423 }
424
425 /* for each map fill in the chans element */
426 list_for_each_entry(c, &iio_map_list, l) {
427 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
428 continue;
429 chans[mapind].indio_dev = c->indio_dev;
430 chans[mapind].data = c->map->consumer_data;
431 chans[mapind].channel =
432 iio_chan_spec_from_name(chans[mapind].indio_dev,
433 c->map->adc_channel_label);
434 if (chans[mapind].channel == NULL) {
435 ret = -EINVAL;
436 goto error_free_chans;
437 }
438 iio_device_get(chans[mapind].indio_dev);
439 mapind++;
440 }
441 if (mapind == 0) {
442 ret = -ENODEV;
443 goto error_free_chans;
444 }
445 mutex_unlock(&iio_map_list_lock);
446
447 return chans;
448
449 error_free_chans:
450 for (i = 0; i < nummaps; i++)
451 iio_device_put(chans[i].indio_dev);
452 kfree(chans);
453 error_ret:
454 mutex_unlock(&iio_map_list_lock);
455
456 return ERR_PTR(ret);
457 }
458 EXPORT_SYMBOL_GPL(iio_channel_get_all);
459
iio_channel_release_all(struct iio_channel * channels)460 void iio_channel_release_all(struct iio_channel *channels)
461 {
462 struct iio_channel *chan = &channels[0];
463
464 while (chan->indio_dev) {
465 iio_device_put(chan->indio_dev);
466 chan++;
467 }
468 kfree(channels);
469 }
470 EXPORT_SYMBOL_GPL(iio_channel_release_all);
471
devm_iio_channel_free_all(struct device * dev,void * res)472 static void devm_iio_channel_free_all(struct device *dev, void *res)
473 {
474 struct iio_channel *channels = *(struct iio_channel **)res;
475
476 iio_channel_release_all(channels);
477 }
478
devm_iio_channel_get_all(struct device * dev)479 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
480 {
481 struct iio_channel **ptr, *channels;
482
483 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
484 if (!ptr)
485 return ERR_PTR(-ENOMEM);
486
487 channels = iio_channel_get_all(dev);
488 if (IS_ERR(channels)) {
489 devres_free(ptr);
490 return channels;
491 }
492
493 *ptr = channels;
494 devres_add(dev, ptr);
495
496 return channels;
497 }
498 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
499
iio_channel_read(struct iio_channel * chan,int * val,int * val2,enum iio_chan_info_enum info)500 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
501 enum iio_chan_info_enum info)
502 {
503 const struct iio_info *iio_info = chan->indio_dev->info;
504 int unused;
505 int vals[INDIO_MAX_RAW_ELEMENTS];
506 int ret;
507 int val_len = 2;
508
509 if (val2 == NULL)
510 val2 = &unused;
511
512 if (!iio_channel_has_info(chan->channel, info))
513 return -EINVAL;
514
515 if (iio_info->read_raw_multi) {
516 ret = iio_info->read_raw_multi(chan->indio_dev,
517 chan->channel,
518 INDIO_MAX_RAW_ELEMENTS,
519 vals, &val_len, info);
520 *val = vals[0];
521 *val2 = vals[1];
522 } else if (iio_info->read_raw) {
523 ret = iio_info->read_raw(chan->indio_dev,
524 chan->channel, val, val2, info);
525 } else {
526 return -EINVAL;
527 }
528
529 return ret;
530 }
531
iio_read_channel_raw(struct iio_channel * chan,int * val)532 int iio_read_channel_raw(struct iio_channel *chan, int *val)
533 {
534 int ret;
535
536 mutex_lock(&chan->indio_dev->info_exist_lock);
537 if (chan->indio_dev->info == NULL) {
538 ret = -ENODEV;
539 goto err_unlock;
540 }
541
542 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
543 err_unlock:
544 mutex_unlock(&chan->indio_dev->info_exist_lock);
545
546 return ret;
547 }
548 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
549
iio_read_channel_average_raw(struct iio_channel * chan,int * val)550 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
551 {
552 int ret;
553
554 mutex_lock(&chan->indio_dev->info_exist_lock);
555 if (chan->indio_dev->info == NULL) {
556 ret = -ENODEV;
557 goto err_unlock;
558 }
559
560 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
561 err_unlock:
562 mutex_unlock(&chan->indio_dev->info_exist_lock);
563
564 return ret;
565 }
566 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
567
iio_convert_raw_to_processed_unlocked(struct iio_channel * chan,int raw,int * processed,unsigned int scale)568 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
569 int raw, int *processed, unsigned int scale)
570 {
571 int scale_type, scale_val, scale_val2;
572 int offset_type, offset_val, offset_val2;
573 s64 raw64 = raw;
574
575 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
576 IIO_CHAN_INFO_OFFSET);
577 if (offset_type >= 0) {
578 switch (offset_type) {
579 case IIO_VAL_INT:
580 break;
581 case IIO_VAL_INT_PLUS_MICRO:
582 case IIO_VAL_INT_PLUS_NANO:
583 /*
584 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
585 * implicitely truncate the offset to it's integer form.
586 */
587 break;
588 case IIO_VAL_FRACTIONAL:
589 offset_val /= offset_val2;
590 break;
591 case IIO_VAL_FRACTIONAL_LOG2:
592 offset_val >>= offset_val2;
593 break;
594 default:
595 return -EINVAL;
596 }
597
598 raw64 += offset_val;
599 }
600
601 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
602 IIO_CHAN_INFO_SCALE);
603 if (scale_type < 0) {
604 /*
605 * If no channel scaling is available apply consumer scale to
606 * raw value and return.
607 */
608 *processed = raw * scale;
609 return 0;
610 }
611
612 switch (scale_type) {
613 case IIO_VAL_INT:
614 *processed = raw64 * scale_val * scale;
615 break;
616 case IIO_VAL_INT_PLUS_MICRO:
617 if (scale_val2 < 0)
618 *processed = -raw64 * scale_val;
619 else
620 *processed = raw64 * scale_val;
621 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
622 1000000LL);
623 break;
624 case IIO_VAL_INT_PLUS_NANO:
625 if (scale_val2 < 0)
626 *processed = -raw64 * scale_val;
627 else
628 *processed = raw64 * scale_val;
629 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
630 1000000000LL);
631 break;
632 case IIO_VAL_FRACTIONAL:
633 *processed = div_s64(raw64 * (s64)scale_val * scale,
634 scale_val2);
635 break;
636 case IIO_VAL_FRACTIONAL_LOG2:
637 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
638 break;
639 default:
640 return -EINVAL;
641 }
642
643 return 0;
644 }
645
iio_convert_raw_to_processed(struct iio_channel * chan,int raw,int * processed,unsigned int scale)646 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
647 int *processed, unsigned int scale)
648 {
649 int ret;
650
651 mutex_lock(&chan->indio_dev->info_exist_lock);
652 if (chan->indio_dev->info == NULL) {
653 ret = -ENODEV;
654 goto err_unlock;
655 }
656
657 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
658 scale);
659 err_unlock:
660 mutex_unlock(&chan->indio_dev->info_exist_lock);
661
662 return ret;
663 }
664 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
665
iio_read_channel_attribute(struct iio_channel * chan,int * val,int * val2,enum iio_chan_info_enum attribute)666 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
667 enum iio_chan_info_enum attribute)
668 {
669 int ret;
670
671 mutex_lock(&chan->indio_dev->info_exist_lock);
672 if (chan->indio_dev->info == NULL) {
673 ret = -ENODEV;
674 goto err_unlock;
675 }
676
677 ret = iio_channel_read(chan, val, val2, attribute);
678 err_unlock:
679 mutex_unlock(&chan->indio_dev->info_exist_lock);
680
681 return ret;
682 }
683 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
684
iio_read_channel_offset(struct iio_channel * chan,int * val,int * val2)685 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
686 {
687 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
688 }
689 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
690
iio_read_channel_processed(struct iio_channel * chan,int * val)691 int iio_read_channel_processed(struct iio_channel *chan, int *val)
692 {
693 int ret;
694
695 mutex_lock(&chan->indio_dev->info_exist_lock);
696 if (chan->indio_dev->info == NULL) {
697 ret = -ENODEV;
698 goto err_unlock;
699 }
700
701 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
702 ret = iio_channel_read(chan, val, NULL,
703 IIO_CHAN_INFO_PROCESSED);
704 } else {
705 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
706 if (ret < 0)
707 goto err_unlock;
708 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
709 }
710
711 err_unlock:
712 mutex_unlock(&chan->indio_dev->info_exist_lock);
713
714 return ret;
715 }
716 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
717
iio_read_channel_scale(struct iio_channel * chan,int * val,int * val2)718 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
719 {
720 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
721 }
722 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
723
iio_channel_read_avail(struct iio_channel * chan,const int ** vals,int * type,int * length,enum iio_chan_info_enum info)724 static int iio_channel_read_avail(struct iio_channel *chan,
725 const int **vals, int *type, int *length,
726 enum iio_chan_info_enum info)
727 {
728 const struct iio_info *iio_info = chan->indio_dev->info;
729
730 if (!iio_channel_has_available(chan->channel, info))
731 return -EINVAL;
732
733 if (iio_info->read_avail)
734 return iio_info->read_avail(chan->indio_dev, chan->channel,
735 vals, type, length, info);
736 return -EINVAL;
737 }
738
iio_read_avail_channel_attribute(struct iio_channel * chan,const int ** vals,int * type,int * length,enum iio_chan_info_enum attribute)739 int iio_read_avail_channel_attribute(struct iio_channel *chan,
740 const int **vals, int *type, int *length,
741 enum iio_chan_info_enum attribute)
742 {
743 int ret;
744
745 mutex_lock(&chan->indio_dev->info_exist_lock);
746 if (!chan->indio_dev->info) {
747 ret = -ENODEV;
748 goto err_unlock;
749 }
750
751 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
752 err_unlock:
753 mutex_unlock(&chan->indio_dev->info_exist_lock);
754
755 return ret;
756 }
757 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
758
iio_read_avail_channel_raw(struct iio_channel * chan,const int ** vals,int * length)759 int iio_read_avail_channel_raw(struct iio_channel *chan,
760 const int **vals, int *length)
761 {
762 int ret;
763 int type;
764
765 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
766 IIO_CHAN_INFO_RAW);
767
768 if (ret >= 0 && type != IIO_VAL_INT)
769 /* raw values are assumed to be IIO_VAL_INT */
770 ret = -EINVAL;
771
772 return ret;
773 }
774 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
775
iio_channel_read_max(struct iio_channel * chan,int * val,int * val2,int * type,enum iio_chan_info_enum info)776 static int iio_channel_read_max(struct iio_channel *chan,
777 int *val, int *val2, int *type,
778 enum iio_chan_info_enum info)
779 {
780 int unused;
781 const int *vals;
782 int length;
783 int ret;
784
785 if (!val2)
786 val2 = &unused;
787
788 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
789 switch (ret) {
790 case IIO_AVAIL_RANGE:
791 switch (*type) {
792 case IIO_VAL_INT:
793 *val = vals[2];
794 break;
795 default:
796 *val = vals[4];
797 *val2 = vals[5];
798 }
799 return 0;
800
801 case IIO_AVAIL_LIST:
802 if (length <= 0)
803 return -EINVAL;
804 switch (*type) {
805 case IIO_VAL_INT:
806 *val = vals[--length];
807 while (length) {
808 if (vals[--length] > *val)
809 *val = vals[length];
810 }
811 break;
812 default:
813 /* FIXME: learn about max for other iio values */
814 return -EINVAL;
815 }
816 return 0;
817
818 default:
819 return ret;
820 }
821 }
822
iio_read_max_channel_raw(struct iio_channel * chan,int * val)823 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
824 {
825 int ret;
826 int type;
827
828 mutex_lock(&chan->indio_dev->info_exist_lock);
829 if (!chan->indio_dev->info) {
830 ret = -ENODEV;
831 goto err_unlock;
832 }
833
834 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
835 err_unlock:
836 mutex_unlock(&chan->indio_dev->info_exist_lock);
837
838 return ret;
839 }
840 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
841
iio_get_channel_type(struct iio_channel * chan,enum iio_chan_type * type)842 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
843 {
844 int ret = 0;
845 /* Need to verify underlying driver has not gone away */
846
847 mutex_lock(&chan->indio_dev->info_exist_lock);
848 if (chan->indio_dev->info == NULL) {
849 ret = -ENODEV;
850 goto err_unlock;
851 }
852
853 *type = chan->channel->type;
854 err_unlock:
855 mutex_unlock(&chan->indio_dev->info_exist_lock);
856
857 return ret;
858 }
859 EXPORT_SYMBOL_GPL(iio_get_channel_type);
860
iio_channel_write(struct iio_channel * chan,int val,int val2,enum iio_chan_info_enum info)861 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
862 enum iio_chan_info_enum info)
863 {
864 const struct iio_info *iio_info = chan->indio_dev->info;
865
866 if (iio_info->write_raw)
867 return iio_info->write_raw(chan->indio_dev,
868 chan->channel, val, val2, info);
869 return -EINVAL;
870 }
871
iio_write_channel_attribute(struct iio_channel * chan,int val,int val2,enum iio_chan_info_enum attribute)872 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
873 enum iio_chan_info_enum attribute)
874 {
875 int ret;
876
877 mutex_lock(&chan->indio_dev->info_exist_lock);
878 if (chan->indio_dev->info == NULL) {
879 ret = -ENODEV;
880 goto err_unlock;
881 }
882
883 ret = iio_channel_write(chan, val, val2, attribute);
884 err_unlock:
885 mutex_unlock(&chan->indio_dev->info_exist_lock);
886
887 return ret;
888 }
889 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
890
iio_write_channel_raw(struct iio_channel * chan,int val)891 int iio_write_channel_raw(struct iio_channel *chan, int val)
892 {
893 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
894 }
895 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
896
iio_get_channel_ext_info_count(struct iio_channel * chan)897 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
898 {
899 const struct iio_chan_spec_ext_info *ext_info;
900 unsigned int i = 0;
901
902 if (!chan->channel->ext_info)
903 return i;
904
905 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
906 ++i;
907
908 return i;
909 }
910 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
911
iio_lookup_ext_info(const struct iio_channel * chan,const char * attr)912 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
913 const struct iio_channel *chan,
914 const char *attr)
915 {
916 const struct iio_chan_spec_ext_info *ext_info;
917
918 if (!chan->channel->ext_info)
919 return NULL;
920
921 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
922 if (!strcmp(attr, ext_info->name))
923 return ext_info;
924 }
925
926 return NULL;
927 }
928
iio_read_channel_ext_info(struct iio_channel * chan,const char * attr,char * buf)929 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
930 const char *attr, char *buf)
931 {
932 const struct iio_chan_spec_ext_info *ext_info;
933
934 ext_info = iio_lookup_ext_info(chan, attr);
935 if (!ext_info)
936 return -EINVAL;
937
938 return ext_info->read(chan->indio_dev, ext_info->private,
939 chan->channel, buf);
940 }
941 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
942
iio_write_channel_ext_info(struct iio_channel * chan,const char * attr,const char * buf,size_t len)943 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
944 const char *buf, size_t len)
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->write(chan->indio_dev, ext_info->private,
953 chan->channel, buf, len);
954 }
955 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
956