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