hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/drivers/iio/inkern.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* The industrial I/O core in kernel channel mapping
23 *
34 * Copyright (c) 2011 Jonathan Cameron
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms of the GNU General Public License version 2 as published by
7
- * the Free Software Foundation.
85 */
96 #include <linux/err.h>
107 #include <linux/export.h>
....@@ -93,7 +90,7 @@
9390
9491 #ifdef CONFIG_OF
9592
96
-static int iio_dev_node_match(struct device *dev, void *data)
93
+static int iio_dev_node_match(struct device *dev, const void *data)
9794 {
9895 return dev->of_node == data && dev->type == &iio_device_type;
9996 }
....@@ -139,9 +136,10 @@
139136
140137 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
141138 iio_dev_node_match);
142
- of_node_put(iiospec.np);
143
- if (idev == NULL)
139
+ if (idev == NULL) {
140
+ of_node_put(iiospec.np);
144141 return -EPROBE_DEFER;
142
+ }
145143
146144 indio_dev = dev_to_iio_dev(idev);
147145 channel->indio_dev = indio_dev;
....@@ -149,6 +147,7 @@
149147 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
150148 else
151149 index = __of_iio_simple_xlate(indio_dev, &iiospec);
150
+ of_node_put(iiospec.np);
152151 if (index < 0)
153152 goto err_put;
154153 channel->channel = &indio_dev->channels[index];
....@@ -363,18 +362,6 @@
363362 iio_channel_release(channel);
364363 }
365364
366
-static int devm_iio_channel_match(struct device *dev, void *res, void *data)
367
-{
368
- struct iio_channel **r = res;
369
-
370
- if (!r || !*r) {
371
- WARN_ON(!r || !*r);
372
- return 0;
373
- }
374
-
375
- return *r == data;
376
-}
377
-
378365 struct iio_channel *devm_iio_channel_get(struct device *dev,
379366 const char *channel_name)
380367 {
....@@ -396,13 +383,6 @@
396383 return channel;
397384 }
398385 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
399
-
400
-void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
401
-{
402
- WARN_ON(devres_release(dev, devm_iio_channel_free,
403
- devm_iio_channel_match, channel));
404
-}
405
-EXPORT_SYMBOL_GPL(devm_iio_channel_release);
406386
407387 struct iio_channel *iio_channel_get_all(struct device *dev)
408388 {
....@@ -517,14 +497,6 @@
517497 }
518498 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
519499
520
-void devm_iio_channel_release_all(struct device *dev,
521
- struct iio_channel *channels)
522
-{
523
- WARN_ON(devres_release(dev, devm_iio_channel_free_all,
524
- devm_iio_channel_match, channels));
525
-}
526
-EXPORT_SYMBOL_GPL(devm_iio_channel_release_all);
527
-
528500 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
529501 enum iio_chan_info_enum info)
530502 {
....@@ -591,28 +563,50 @@
591563 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
592564 int raw, int *processed, unsigned int scale)
593565 {
594
- int scale_type, scale_val, scale_val2, offset;
566
+ int scale_type, scale_val, scale_val2;
567
+ int offset_type, offset_val, offset_val2;
595568 s64 raw64 = raw;
596
- int ret;
597569
598
- ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
599
- if (ret >= 0)
600
- raw64 += offset;
570
+ offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
571
+ IIO_CHAN_INFO_OFFSET);
572
+ if (offset_type >= 0) {
573
+ switch (offset_type) {
574
+ case IIO_VAL_INT:
575
+ break;
576
+ case IIO_VAL_INT_PLUS_MICRO:
577
+ case IIO_VAL_INT_PLUS_NANO:
578
+ /*
579
+ * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
580
+ * implicitely truncate the offset to it's integer form.
581
+ */
582
+ break;
583
+ case IIO_VAL_FRACTIONAL:
584
+ offset_val /= offset_val2;
585
+ break;
586
+ case IIO_VAL_FRACTIONAL_LOG2:
587
+ offset_val >>= offset_val2;
588
+ break;
589
+ default:
590
+ return -EINVAL;
591
+ }
592
+
593
+ raw64 += offset_val;
594
+ }
601595
602596 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
603597 IIO_CHAN_INFO_SCALE);
604598 if (scale_type < 0) {
605599 /*
606
- * Just pass raw values as processed if no scaling is
607
- * available.
600
+ * If no channel scaling is available apply consumer scale to
601
+ * raw value and return.
608602 */
609
- *processed = raw;
603
+ *processed = raw * scale;
610604 return 0;
611605 }
612606
613607 switch (scale_type) {
614608 case IIO_VAL_INT:
615
- *processed = raw64 * scale_val;
609
+ *processed = raw64 * scale_val * scale;
616610 break;
617611 case IIO_VAL_INT_PLUS_MICRO:
618612 if (scale_val2 < 0)
....@@ -733,11 +727,11 @@
733727 vals, type, length, info);
734728 }
735729
736
-int iio_read_avail_channel_raw(struct iio_channel *chan,
737
- const int **vals, int *length)
730
+int iio_read_avail_channel_attribute(struct iio_channel *chan,
731
+ const int **vals, int *type, int *length,
732
+ enum iio_chan_info_enum attribute)
738733 {
739734 int ret;
740
- int type;
741735
742736 mutex_lock(&chan->indio_dev->info_exist_lock);
743737 if (!chan->indio_dev->info) {
....@@ -745,11 +739,23 @@
745739 goto err_unlock;
746740 }
747741
748
- ret = iio_channel_read_avail(chan,
749
- vals, &type, length, IIO_CHAN_INFO_RAW);
742
+ ret = iio_channel_read_avail(chan, vals, type, length, attribute);
750743 err_unlock:
751744 mutex_unlock(&chan->indio_dev->info_exist_lock);
752745
746
+ return ret;
747
+}
748
+EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
749
+
750
+int iio_read_avail_channel_raw(struct iio_channel *chan,
751
+ const int **vals, int *length)
752
+{
753
+ int ret;
754
+ int type;
755
+
756
+ ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
757
+ IIO_CHAN_INFO_RAW);
758
+
753759 if (ret >= 0 && type != IIO_VAL_INT)
754760 /* raw values are assumed to be IIO_VAL_INT */
755761 ret = -EINVAL;