1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Unit tests for IIO light sensor gain-time-scale helpers
4 * Copyright (c) 2023 Matti Vaittinen <mazziesaccount@gmail.com>
7 #include <kunit/device.h>
8 #include <kunit/test.h>
9 #include <linux/device.h>
10 #include <linux/iio/iio-gts-helper.h>
11 #include <linux/iio/types.h>
14 * Please, read the "rant" from the top of the lib/test_linear_ranges.c if
15 * you see a line of helper code which is not being tested.
17 * Then, please look at the line which is not being tested. Is this line
18 * somehow unusually complex? If answer is "no", then chances are that the
19 * "development inertia" caused by adding a test exceeds the benefits.
21 * If yes, then adding a test is probably a good idea but please stop for a
22 * moment and consider the effort of changing all the tests when code gets
23 * refactored. Eventually it neeeds to be.
26 #define TEST_TSEL_50 1
27 #define TEST_TSEL_X_MIN TEST_TSEL_50
28 #define TEST_TSEL_100 0
29 #define TEST_TSEL_200 2
30 #define TEST_TSEL_400 4
31 #define TEST_TSEL_X_MAX TEST_TSEL_400
33 #define TEST_GSEL_1 0x00
34 #define TEST_GSEL_X_MIN TEST_GSEL_1
35 #define TEST_GSEL_4 0x08
36 #define TEST_GSEL_16 0x0a
37 #define TEST_GSEL_32 0x0b
38 #define TEST_GSEL_64 0x0c
39 #define TEST_GSEL_256 0x18
40 #define TEST_GSEL_512 0x19
41 #define TEST_GSEL_1024 0x1a
42 #define TEST_GSEL_2048 0x1b
43 #define TEST_GSEL_4096 0x1c
44 #define TEST_GSEL_X_MAX TEST_GSEL_4096
46 #define TEST_SCALE_1X 64
47 #define TEST_SCALE_MIN_X TEST_SCALE_1X
48 #define TEST_SCALE_2X 32
49 #define TEST_SCALE_4X 16
50 #define TEST_SCALE_8X 8
51 #define TEST_SCALE_16X 4
52 #define TEST_SCALE_32X 2
53 #define TEST_SCALE_64X 1
55 #define TEST_SCALE_NANO_128X 500000000
56 #define TEST_SCALE_NANO_256X 250000000
57 #define TEST_SCALE_NANO_512X 125000000
58 #define TEST_SCALE_NANO_1024X 62500000
59 #define TEST_SCALE_NANO_2048X 31250000
60 #define TEST_SCALE_NANO_4096X 15625000
61 #define TEST_SCALE_NANO_4096X2 7812500
62 #define TEST_SCALE_NANO_4096X4 3906250
63 #define TEST_SCALE_NANO_4096X8 1953125
65 #define TEST_SCALE_NANO_MAX_X TEST_SCALE_NANO_4096X8
68 * Can't have this allocated from stack because the kunit clean-up will
69 * happen only after the test function has already gone
71 static struct iio_gts gts;
73 static const struct iio_gain_sel_pair gts_test_gains[] = {
74 GAIN_SCALE_GAIN(1, TEST_GSEL_1),
75 GAIN_SCALE_GAIN(4, TEST_GSEL_4),
76 GAIN_SCALE_GAIN(16, TEST_GSEL_16),
77 GAIN_SCALE_GAIN(32, TEST_GSEL_32),
78 GAIN_SCALE_GAIN(64, TEST_GSEL_64),
79 GAIN_SCALE_GAIN(256, TEST_GSEL_256),
80 GAIN_SCALE_GAIN(512, TEST_GSEL_512),
81 GAIN_SCALE_GAIN(1024, TEST_GSEL_1024),
82 GAIN_SCALE_GAIN(2048, TEST_GSEL_2048),
83 GAIN_SCALE_GAIN(4096, TEST_GSEL_4096),
84 #define HWGAIN_MAX 4096
87 static const struct iio_itime_sel_mul gts_test_itimes[] = {
88 GAIN_SCALE_ITIME_US(400 * 1000, TEST_TSEL_400, 8),
89 GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4),
90 GAIN_SCALE_ITIME_US(100 * 1000, TEST_TSEL_100, 2),
91 GAIN_SCALE_ITIME_US(50 * 1000, TEST_TSEL_50, 1),
92 #define TIMEGAIN_MAX 8
94 #define TOTAL_GAIN_MAX (HWGAIN_MAX * TIMEGAIN_MAX)
95 #define IIO_GTS_TEST_DEV "iio-gts-test-dev"
97 static struct device *__test_init_iio_gain_scale(struct kunit *test,
98 struct iio_gts *gts, const struct iio_gain_sel_pair *g_table,
99 int num_g, const struct iio_itime_sel_mul *i_table, int num_i)
104 dev = kunit_device_register(test, IIO_GTS_TEST_DEV);
106 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
107 if (IS_ERR_OR_NULL(dev))
110 ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, g_table, num_g,
111 i_table, num_i, gts);
112 KUNIT_EXPECT_EQ(test, 0, ret);
119 #define test_init_iio_gain_scale(test, gts) \
120 __test_init_iio_gain_scale(test, gts, gts_test_gains, \
121 ARRAY_SIZE(gts_test_gains), gts_test_itimes, \
122 ARRAY_SIZE(gts_test_itimes))
124 static void test_init_iio_gts_invalid(struct kunit *test)
128 const struct iio_itime_sel_mul itimes_neg[] = {
129 GAIN_SCALE_ITIME_US(-10, TEST_TSEL_400, 8),
130 GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4),
132 const struct iio_gain_sel_pair gains_neg[] = {
133 GAIN_SCALE_GAIN(1, TEST_GSEL_1),
134 GAIN_SCALE_GAIN(2, TEST_GSEL_4),
135 GAIN_SCALE_GAIN(-2, TEST_GSEL_16),
137 /* 55555 * 38656 = 2147534080 => overflows 32bit int */
138 const struct iio_itime_sel_mul itimes_overflow[] = {
139 GAIN_SCALE_ITIME_US(400 * 1000, TEST_TSEL_400, 55555),
140 GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4),
142 const struct iio_gain_sel_pair gains_overflow[] = {
143 GAIN_SCALE_GAIN(1, TEST_GSEL_1),
144 GAIN_SCALE_GAIN(2, TEST_GSEL_4),
145 GAIN_SCALE_GAIN(38656, TEST_GSEL_16),
148 dev = kunit_device_register(test, IIO_GTS_TEST_DEV);
149 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
153 /* Ok gains, negative time */
154 ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, gts_test_gains,
155 ARRAY_SIZE(gts_test_gains), itimes_neg,
156 ARRAY_SIZE(itimes_neg), >s);
157 KUNIT_EXPECT_EQ(test, -EINVAL, ret);
159 /* Ok times, negative gain */
160 ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, gains_neg,
161 ARRAY_SIZE(gains_neg), gts_test_itimes,
162 ARRAY_SIZE(gts_test_itimes), >s);
163 KUNIT_EXPECT_EQ(test, -EINVAL, ret);
165 /* gain * time overflow int */
166 ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, gains_overflow,
167 ARRAY_SIZE(gains_overflow), itimes_overflow,
168 ARRAY_SIZE(itimes_overflow), >s);
169 KUNIT_EXPECT_EQ(test, -EOVERFLOW, ret);
172 static void test_iio_gts_find_gain_for_scale_using_time(struct kunit *test)
177 dev = test_init_iio_gain_scale(test, >s);
181 ret = iio_gts_find_gain_sel_for_scale_using_time(>s, TEST_TSEL_100,
182 TEST_SCALE_8X, 0, &gain_sel);
184 * Meas time 100 => gain by time 2x
185 * TEST_SCALE_8X matches total gain 8x
186 * => required HWGAIN 4x
188 KUNIT_EXPECT_EQ(test, 0, ret);
189 KUNIT_EXPECT_EQ(test, TEST_GSEL_4, gain_sel);
191 ret = iio_gts_find_gain_sel_for_scale_using_time(>s, TEST_TSEL_200, 0,
192 TEST_SCALE_NANO_256X, &gain_sel);
194 * Meas time 200 => gain by time 4x
195 * TEST_SCALE_256X matches total gain 256x
196 * => required HWGAIN 256/4 => 64x
198 KUNIT_EXPECT_EQ(test, 0, ret);
199 KUNIT_EXPECT_EQ(test, TEST_GSEL_64, gain_sel);
201 /* Min time, Min gain */
202 ret = iio_gts_find_gain_sel_for_scale_using_time(>s, TEST_TSEL_X_MIN,
203 TEST_SCALE_MIN_X, 0, &gain_sel);
204 KUNIT_EXPECT_EQ(test, 0, ret);
205 KUNIT_EXPECT_EQ(test, TEST_GSEL_1, gain_sel);
207 /* Max time, Max gain */
208 ret = iio_gts_find_gain_sel_for_scale_using_time(>s, TEST_TSEL_X_MAX,
209 0, TEST_SCALE_NANO_MAX_X, &gain_sel);
210 KUNIT_EXPECT_EQ(test, 0, ret);
211 KUNIT_EXPECT_EQ(test, TEST_GSEL_4096, gain_sel);
213 ret = iio_gts_find_gain_sel_for_scale_using_time(>s, TEST_TSEL_100, 0,
214 TEST_SCALE_NANO_256X, &gain_sel);
216 * Meas time 100 => gain by time 2x
217 * TEST_SCALE_256X matches total gain 256x
218 * => required HWGAIN 256/2 => 128x (not in gain-table - unsupported)
220 KUNIT_EXPECT_NE(test, 0, ret);
222 ret = iio_gts_find_gain_sel_for_scale_using_time(>s, TEST_TSEL_200, 0,
223 TEST_SCALE_NANO_MAX_X, &gain_sel);
224 /* We can't reach the max gain with integration time smaller than MAX */
225 KUNIT_EXPECT_NE(test, 0, ret);
227 ret = iio_gts_find_gain_sel_for_scale_using_time(>s, TEST_TSEL_50, 0,
228 TEST_SCALE_NANO_MAX_X, &gain_sel);
229 /* We can't reach the max gain with integration time smaller than MAX */
230 KUNIT_EXPECT_NE(test, 0, ret);
233 static void test_iio_gts_find_new_gain_sel_by_old_gain_time(struct kunit *test)
236 int ret, old_gain, new_gain, old_time_sel, new_time_sel;
238 dev = test_init_iio_gain_scale(test, >s);
243 old_time_sel = TEST_TSEL_200;
244 new_time_sel = TEST_TSEL_400;
246 ret = iio_gts_find_new_gain_sel_by_old_gain_time(>s, old_gain,
247 old_time_sel, new_time_sel, &new_gain);
248 KUNIT_EXPECT_EQ(test, 0, ret);
250 * Doubling the integration time doubles the total gain - so old
251 * (hw)gain must be divided by two to compensate. => 32 / 2 => 16
253 KUNIT_EXPECT_EQ(test, 16, new_gain);
256 old_time_sel = TEST_TSEL_50;
257 new_time_sel = TEST_TSEL_200;
258 ret = iio_gts_find_new_gain_sel_by_old_gain_time(>s, old_gain,
259 old_time_sel, new_time_sel, &new_gain);
260 KUNIT_EXPECT_EQ(test, 0, ret);
262 * gain by time 1x => 4x - (hw)gain 4x => 1x
264 KUNIT_EXPECT_EQ(test, 1, new_gain);
267 old_time_sel = TEST_TSEL_400;
268 new_time_sel = TEST_TSEL_50;
269 ret = iio_gts_find_new_gain_sel_by_old_gain_time(>s, old_gain,
270 old_time_sel, new_time_sel, &new_gain);
271 KUNIT_EXPECT_EQ(test, 0, ret);
273 * gain by time 8x => 1x - (hw)gain 512x => 4096x)
275 KUNIT_EXPECT_EQ(test, 4096, new_gain);
277 /* Unsupported gain 2x */
279 old_time_sel = TEST_TSEL_200;
280 new_time_sel = TEST_TSEL_400;
281 ret = iio_gts_find_new_gain_sel_by_old_gain_time(>s, old_gain,
282 old_time_sel, new_time_sel, &new_gain);
283 KUNIT_EXPECT_NE(test, 0, ret);
287 old_time_sel = TEST_TSEL_50;
288 new_time_sel = TEST_TSEL_400;
289 ret = iio_gts_find_new_gain_sel_by_old_gain_time(>s, old_gain,
290 old_time_sel, new_time_sel, &new_gain);
291 KUNIT_EXPECT_NE(test, 0, ret);
295 old_time_sel = TEST_TSEL_400;
296 new_time_sel = TEST_TSEL_50;
297 ret = iio_gts_find_new_gain_sel_by_old_gain_time(>s, old_gain,
298 old_time_sel, new_time_sel, &new_gain);
299 KUNIT_EXPECT_NE(test, 0, ret);
303 static void test_iio_find_closest_gain_low(struct kunit *test)
309 const struct iio_gain_sel_pair gts_test_gains_gain_low[] = {
310 GAIN_SCALE_GAIN(4, TEST_GSEL_4),
311 GAIN_SCALE_GAIN(16, TEST_GSEL_16),
312 GAIN_SCALE_GAIN(32, TEST_GSEL_32),
315 dev = test_init_iio_gain_scale(test, >s);
319 ret = iio_find_closest_gain_low(>s, 2, &in_range);
320 KUNIT_EXPECT_EQ(test, 1, ret);
321 KUNIT_EXPECT_EQ(test, true, in_range);
323 ret = iio_find_closest_gain_low(>s, 1, &in_range);
324 KUNIT_EXPECT_EQ(test, 1, ret);
325 KUNIT_EXPECT_EQ(test, true, in_range);
327 ret = iio_find_closest_gain_low(>s, 4095, &in_range);
328 KUNIT_EXPECT_EQ(test, 2048, ret);
329 KUNIT_EXPECT_EQ(test, true, in_range);
331 ret = iio_find_closest_gain_low(>s, 4097, &in_range);
332 KUNIT_EXPECT_EQ(test, 4096, ret);
333 KUNIT_EXPECT_EQ(test, false, in_range);
335 kunit_device_unregister(test, dev);
337 dev = __test_init_iio_gain_scale(test, >s, gts_test_gains_gain_low,
338 ARRAY_SIZE(gts_test_gains_gain_low),
339 gts_test_itimes, ARRAY_SIZE(gts_test_itimes));
343 ret = iio_find_closest_gain_low(>s, 3, &in_range);
344 KUNIT_EXPECT_EQ(test, -EINVAL, ret);
345 KUNIT_EXPECT_EQ(test, false, in_range);
348 static void test_iio_gts_total_gain_to_scale(struct kunit *test)
351 int ret, scale_int, scale_nano;
353 dev = test_init_iio_gain_scale(test, >s);
357 ret = iio_gts_total_gain_to_scale(>s, 1, &scale_int, &scale_nano);
358 KUNIT_EXPECT_EQ(test, 0, ret);
359 KUNIT_EXPECT_EQ(test, TEST_SCALE_1X, scale_int);
360 KUNIT_EXPECT_EQ(test, 0, scale_nano);
362 ret = iio_gts_total_gain_to_scale(>s, 1, &scale_int, &scale_nano);
363 KUNIT_EXPECT_EQ(test, 0, ret);
364 KUNIT_EXPECT_EQ(test, TEST_SCALE_1X, scale_int);
365 KUNIT_EXPECT_EQ(test, 0, scale_nano);
367 ret = iio_gts_total_gain_to_scale(>s, 4096 * 8, &scale_int,
369 KUNIT_EXPECT_EQ(test, 0, ret);
370 KUNIT_EXPECT_EQ(test, 0, scale_int);
371 KUNIT_EXPECT_EQ(test, TEST_SCALE_NANO_4096X8, scale_nano);
374 static void test_iio_gts_chk_times(struct kunit *test, const int *vals)
376 static const int expected[] = {0, 50000, 0, 100000, 0, 200000, 0, 400000};
379 for (i = 0; i < ARRAY_SIZE(expected); i++)
380 KUNIT_EXPECT_EQ(test, expected[i], vals[i]);
383 static void test_iio_gts_chk_scales_all(struct kunit *test, struct iio_gts *gts,
384 const int *vals, int len)
386 static const int gains[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
387 1024, 2048, 4096, 4096 * 2, 4096 * 4,
389 int expected[ARRAY_SIZE(gains) * 2];
391 int exp_len = ARRAY_SIZE(gains) * 2;
393 KUNIT_EXPECT_EQ(test, exp_len, len);
397 for (i = 0; i < ARRAY_SIZE(gains); i++) {
398 ret = iio_gts_total_gain_to_scale(gts, gains[i],
400 &expected[2 * i + 1]);
401 KUNIT_EXPECT_EQ(test, 0, ret);
406 for (i = 0; i < ARRAY_SIZE(expected); i++)
407 KUNIT_EXPECT_EQ(test, expected[i], vals[i]);
410 static void test_iio_gts_chk_scales_t200(struct kunit *test, struct iio_gts *gts,
411 const int *vals, int len)
413 /* The gain caused by time 200 is 4x */
414 static const int gains[] = {
426 int expected[ARRAY_SIZE(gains) * 2];
429 KUNIT_EXPECT_EQ(test, 2 * ARRAY_SIZE(gains), len);
430 if (len < 2 * ARRAY_SIZE(gains))
433 for (i = 0; i < ARRAY_SIZE(gains); i++) {
434 ret = iio_gts_total_gain_to_scale(gts, gains[i],
436 &expected[2 * i + 1]);
437 KUNIT_EXPECT_EQ(test, 0, ret);
442 for (i = 0; i < ARRAY_SIZE(expected); i++)
443 KUNIT_EXPECT_EQ(test, expected[i], vals[i]);
446 static void test_iio_gts_avail_test(struct kunit *test)
453 dev = test_init_iio_gain_scale(test, >s);
457 /* test table building for times and iio_gts_avail_times() */
458 ret = iio_gts_avail_times(>s, &vals, &type, &len);
459 KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret);
463 KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_MICRO, type);
464 KUNIT_EXPECT_EQ(test, 8, len);
468 test_iio_gts_chk_times(test, vals);
470 /* Test table building for all scales and iio_gts_all_avail_scales() */
471 ret = iio_gts_all_avail_scales(>s, &vals, &type, &len);
472 KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret);
476 KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_NANO, type);
478 test_iio_gts_chk_scales_all(test, >s, vals, len);
481 * Test table building for scales/time and
482 * iio_gts_avail_scales_for_time()
484 ret = iio_gts_avail_scales_for_time(>s, 200000, &vals, &type, &len);
485 KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret);
489 KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_NANO, type);
490 test_iio_gts_chk_scales_t200(test, >s, vals, len);
493 static struct kunit_case iio_gts_test_cases[] = {
494 KUNIT_CASE(test_init_iio_gts_invalid),
495 KUNIT_CASE(test_iio_gts_find_gain_for_scale_using_time),
496 KUNIT_CASE(test_iio_gts_find_new_gain_sel_by_old_gain_time),
497 KUNIT_CASE(test_iio_find_closest_gain_low),
498 KUNIT_CASE(test_iio_gts_total_gain_to_scale),
499 KUNIT_CASE(test_iio_gts_avail_test),
503 static struct kunit_suite iio_gts_test_suite = {
504 .name = "iio-gain-time-scale",
505 .test_cases = iio_gts_test_cases,
508 kunit_test_suite(iio_gts_test_suite);
510 MODULE_LICENSE("GPL");
511 MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
512 MODULE_DESCRIPTION("Test IIO light sensor gain-time-scale helpers");
513 MODULE_IMPORT_NS(IIO_GTS_HELPER);