hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
....@@ -1,30 +1,22 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * HID Sensors Driver
34 * Copyright (c) 2012, Intel Corporation.
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms and conditions of the GNU General Public License,
7
- * version 2, as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope it will be useful, but WITHOUT
10
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
- * more details.
13
- *
14
- * You should have received a copy of the GNU General Public License along with
15
- * this program; if not, write to the Free Software Foundation, Inc.,
16
- * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17
- *
185 */
196 #include <linux/device.h>
207 #include <linux/platform_device.h>
218 #include <linux/module.h>
229 #include <linux/interrupt.h>
2310 #include <linux/irq.h>
11
+#include <linux/kernel.h>
2412 #include <linux/slab.h>
13
+#include <linux/time.h>
14
+
2515 #include <linux/hid-sensor-hub.h>
2616 #include <linux/iio/iio.h>
2717 #include <linux/iio/sysfs.h>
18
+
19
+#define HZ_PER_MHZ 1000000L
2820
2921 static struct {
3022 u32 usage_id;
....@@ -81,16 +73,6 @@
8173 {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
8274 };
8375
84
-static int pow_10(unsigned power)
85
-{
86
- int i;
87
- int ret = 1;
88
- for (i = 0; i < power; ++i)
89
- ret = ret * 10;
90
-
91
- return ret;
92
-}
93
-
9476 static void simple_div(int dividend, int divisor, int *whole,
9577 int *micro_frac)
9678 {
....@@ -109,14 +91,16 @@
10991 rem *= 10;
11092 exp++;
11193 }
112
- *micro_frac = (rem / divisor) * pow_10(6-exp);
94
+ *micro_frac = (rem / divisor) * int_pow(10, 6 - exp);
11395 }
11496 }
11597
11698 static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
11799 {
118
- *val1 = no/pow_10(exp);
119
- *val2 = no%pow_10(exp) * pow_10(6-exp);
100
+ int divisor = int_pow(10, exp);
101
+
102
+ *val1 = no / divisor;
103
+ *val2 = no % divisor * int_pow(10, 6 - exp);
120104 }
121105
122106 /*
....@@ -138,7 +122,7 @@
138122 }
139123 exp = hid_sensor_convert_exponent(exp);
140124 if (exp >= 0) {
141
- *val1 = sign * value * pow_10(exp);
125
+ *val1 = sign * value * int_pow(10, exp);
142126 *val2 = 0;
143127 } else {
144128 split_micro_fraction(value, -exp, val1, val2);
....@@ -151,6 +135,7 @@
151135
152136 static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
153137 {
138
+ int divisor;
154139 u32 value;
155140 int sign = 1;
156141
....@@ -158,10 +143,13 @@
158143 sign = -1;
159144 exp = hid_sensor_convert_exponent(exp);
160145 if (exp < 0) {
161
- value = abs(val1) * pow_10(-exp);
162
- value += abs(val2) / pow_10(6+exp);
163
- } else
164
- value = abs(val1) / pow_10(exp);
146
+ divisor = int_pow(10, 6 + exp);
147
+ value = abs(val1) * int_pow(10, -exp);
148
+ value += abs(val2) / divisor;
149
+ } else {
150
+ divisor = int_pow(10, exp);
151
+ value = abs(val1) / divisor;
152
+ }
165153 if (sign < 0)
166154 value = ((1LL << (size * 8)) - value);
167155
....@@ -224,12 +212,12 @@
224212 if (val1 < 0 || val2 < 0)
225213 return -EINVAL;
226214
227
- value = val1 * pow_10(6) + val2;
215
+ value = val1 * HZ_PER_MHZ + val2;
228216 if (value) {
229217 if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
230
- value = pow_10(9)/value;
218
+ value = NSEC_PER_SEC / value;
231219 else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
232
- value = pow_10(6)/value;
220
+ value = USEC_PER_SEC / value;
233221 else
234222 value = 0;
235223 }
....@@ -318,40 +306,44 @@
318306 static void adjust_exponent_nano(int *val0, int *val1, int scale0,
319307 int scale1, int exp)
320308 {
309
+ int divisor;
321310 int i;
322311 int x;
323312 int res;
324313 int rem;
325314
326315 if (exp > 0) {
327
- *val0 = scale0 * pow_10(exp);
316
+ *val0 = scale0 * int_pow(10, exp);
328317 res = 0;
329318 if (exp > 9) {
330319 *val1 = 0;
331320 return;
332321 }
333322 for (i = 0; i < exp; ++i) {
334
- x = scale1 / pow_10(8 - i);
335
- res += (pow_10(exp - 1 - i) * x);
336
- scale1 = scale1 % pow_10(8 - i);
323
+ divisor = int_pow(10, 8 - i);
324
+ x = scale1 / divisor;
325
+ res += int_pow(10, exp - 1 - i) * x;
326
+ scale1 = scale1 % divisor;
337327 }
338328 *val0 += res;
339
- *val1 = scale1 * pow_10(exp);
329
+ *val1 = scale1 * int_pow(10, exp);
340330 } else if (exp < 0) {
341331 exp = abs(exp);
342332 if (exp > 9) {
343333 *val0 = *val1 = 0;
344334 return;
345335 }
346
- *val0 = scale0 / pow_10(exp);
347
- rem = scale0 % pow_10(exp);
336
+ divisor = int_pow(10, exp);
337
+ *val0 = scale0 / divisor;
338
+ rem = scale0 % divisor;
348339 res = 0;
349340 for (i = 0; i < (9 - exp); ++i) {
350
- x = scale1 / pow_10(8 - i);
351
- res += (pow_10(8 - exp - i) * x);
352
- scale1 = scale1 % pow_10(8 - i);
341
+ divisor = int_pow(10, 8 - i);
342
+ x = scale1 / divisor;
343
+ res += int_pow(10, 8 - exp - i) * x;
344
+ scale1 = scale1 % divisor;
353345 }
354
- *val1 = rem * pow_10(9 - exp) + res;
346
+ *val1 = rem * int_pow(10, 9 - exp) + res;
355347 } else {
356348 *val0 = scale0;
357349 *val1 = scale1;