History

iio: afe: rescale: expose scale processing function

In preparation for the addition of kunit tests, expose the logic responsible for combining channel scales.

Liam Beguin committed 4 years ago

Commit bc437f71 parent b59c0412 files · +60 −37Browse files

Unified · Split

2 files
  1. drivers/iio/afe/iio-rescale.c +28 −37
  2. include/linux/iio/afe/rescale.h +32 −0
drivers/iio/afe/iio-rescale.c+28−37
@@ -15,32 +15,43 @@ · 14 unmodified lines
1515 #include <linux/platform_device.h>
1616 #include <linux/property.h>
1717
18+#include <linux/iio/afe/rescale.h>
1819 #include <linux/iio/consumer.h>
1920 #include <linux/iio/iio.h>
2021
21−struct rescale;
22−
23−struct rescale_cfg {
24− enum iio_chan_type type;
25− int (*props)(struct device *dev, struct rescale *rescale);
26−};
22+int rescale_process_scale(struct rescale *rescale, int scale_type,
23+ int *val, int *val2)
24+{
25+ s64 tmp;
2726
28−struct rescale {
29− const struct rescale_cfg *cfg;
30− struct iio_channel *source;
31− struct iio_chan_spec chan;
32− struct iio_chan_spec_ext_info *ext_info;
33− bool chan_processed;
34− s32 numerator;
35− s32 denominator;
36−};
27+ switch (scale_type) {
28+ case IIO_VAL_FRACTIONAL:
29+ *val *= rescale->numerator;
30+ *val2 *= rescale->denominator;
31+ return scale_type;
32+ case IIO_VAL_INT:
33+ *val *= rescale->numerator;
34+ if (rescale->denominator == 1)
35+ return scale_type;
36+ *val2 = rescale->denominator;
37+ return IIO_VAL_FRACTIONAL;
38+ case IIO_VAL_FRACTIONAL_LOG2:
39+ tmp = (s64)*val * 1000000000LL;
40+ tmp = div_s64(tmp, rescale->denominator);
41+ tmp *= rescale->numerator;
42+ tmp = div_s64(tmp, 1000000000LL);
43+ *val = tmp;
44+ return scale_type;
45+ default:
46+ return -EOPNOTSUPP;
47+ }
48+}
3749
3850 static int rescale_read_raw(struct iio_dev *indio_dev,
3951 struct iio_chan_spec const *chan,
4052 int *val, int *val2, long mask)
4153 {
4254 struct rescale *rescale = iio_priv(indio_dev);
43− s64 tmp;
4455 int ret;
4556
4657 switch (mask) {
@@ -66,27 +77,7 @@ static int rescale_read_raw(struct iio_dev *indio_dev, · 19 unmodified lines
6677 } else {
6778 ret = iio_read_channel_scale(rescale->source, val, val2);
6879 }
69− switch (ret) {
70− case IIO_VAL_FRACTIONAL:
71− *val *= rescale->numerator;
72− *val2 *= rescale->denominator;
73− return ret;
74− case IIO_VAL_INT:
75− *val *= rescale->numerator;
76− if (rescale->denominator == 1)
77− return ret;
78− *val2 = rescale->denominator;
79− return IIO_VAL_FRACTIONAL;
80− case IIO_VAL_FRACTIONAL_LOG2:
81− tmp = (s64)*val * 1000000000LL;
82− tmp = div_s64(tmp, rescale->denominator);
83− tmp *= rescale->numerator;
84− tmp = div_s64(tmp, 1000000000LL);
85− *val = tmp;
86− return ret;
87− default:
88− return -EOPNOTSUPP;
89− }
80+ return rescale_process_scale(rescale, ret, val, val2);
9081 default:
9182 return -EINVAL;
9283 }
include/linux/iio/afe/rescale.h+32−0
@@ -0,0 +1,32 @@
1+/* SPDX-License-Identifier: GPL-2.0-only */
2+/*
3+ * Copyright (C) 2018 Axentia Technologies AB
4+ */
5+
6+#ifndef __IIO_RESCALE_H__
7+#define __IIO_RESCALE_H__
8+
9+#include <linux/types.h>
10+#include <linux/iio/iio.h>
11+
12+struct device;
13+struct rescale;
14+
15+struct rescale_cfg {
16+ enum iio_chan_type type;
17+ int (*props)(struct device *dev, struct rescale *rescale);
18+};
19+
20+struct rescale {
21+ const struct rescale_cfg *cfg;
22+ struct iio_channel *source;
23+ struct iio_chan_spec chan;
24+ struct iio_chan_spec_ext_info *ext_info;
25+ bool chan_processed;
26+ s32 numerator;
27+ s32 denominator;
28+};
29+
30+int rescale_process_scale(struct rescale *rescale, int scale_type,
31+ int *val, int *val2);
32+#endif /* __IIO_RESCALE_H__ */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t