summaryrefslogtreecommitdiff
path: root/drivers/iio/test/iio-test-gts.c
blob: cf7ab773ea0b15d68ffe5da0646acba9a52c3c04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// SPDX-License-Identifier: GPL-2.0-only
/* Unit tests for IIO light sensor gain-time-scale helpers
 *
 * Copyright (c) 2023 Matti Vaittinen <mazziesaccount@gmail.com>
 */

#include <kunit/device.h>
#include <kunit/test.h>
#include <linux/device.h>
#include <linux/iio/iio-gts-helper.h>
#include <linux/iio/types.h>

/*
 * Please, read the "rant" from the top of the lib/test_linear_ranges.c if
 * you see a line of helper code which is not being tested.
 *
 * Then, please look at the line which is not being tested. Is this line
 * somehow unusually complex? If answer is "no", then chances are that the
 * "development inertia" caused by adding a test exceeds the benefits.
 *
 * If yes, then adding a test is probably a good idea but please stop for a
 * moment and consider the effort of changing all the tests when code gets
 * refactored. Eventually it neeeds to be.
 */

#define TEST_TSEL_50		1
#define TEST_TSEL_X_MIN		TEST_TSEL_50
#define TEST_TSEL_100		0
#define TEST_TSEL_200		2
#define TEST_TSEL_400		4
#define TEST_TSEL_X_MAX		TEST_TSEL_400

#define TEST_GSEL_1		0x00
#define TEST_GSEL_X_MIN		TEST_GSEL_1
#define TEST_GSEL_4		0x08
#define TEST_GSEL_16		0x0a
#define TEST_GSEL_32		0x0b
#define TEST_GSEL_64		0x0c
#define TEST_GSEL_256		0x18
#define TEST_GSEL_512		0x19
#define TEST_GSEL_1024		0x1a
#define TEST_GSEL_2048		0x1b
#define TEST_GSEL_4096		0x1c
#define TEST_GSEL_X_MAX		TEST_GSEL_4096

#define TEST_SCALE_1X		64
#define TEST_SCALE_MIN_X	TEST_SCALE_1X
#define TEST_SCALE_2X		32
#define TEST_SCALE_4X		16
#define TEST_SCALE_8X		8
#define TEST_SCALE_16X		4
#define TEST_SCALE_32X		2
#define TEST_SCALE_64X		1

#define TEST_SCALE_NANO_128X	500000000
#define TEST_SCALE_NANO_256X	250000000
#define TEST_SCALE_NANO_512X	125000000
#define TEST_SCALE_NANO_1024X	62500000
#define TEST_SCALE_NANO_2048X	31250000
#define TEST_SCALE_NANO_4096X	15625000
#define TEST_SCALE_NANO_4096X2	7812500
#define TEST_SCALE_NANO_4096X4	3906250
#define TEST_SCALE_NANO_4096X8	1953125

#define TEST_SCALE_NANO_MAX_X TEST_SCALE_NANO_4096X8

/*
 * Can't have this allocated from stack because the kunit clean-up will
 * happen only after the test function has already gone
 */
static struct iio_gts gts;

static const struct iio_gain_sel_pair gts_test_gains[] = {
	GAIN_SCALE_GAIN(1, TEST_GSEL_1),
	GAIN_SCALE_GAIN(4, TEST_GSEL_4),
	GAIN_SCALE_GAIN(16, TEST_GSEL_16),
	GAIN_SCALE_GAIN(32, TEST_GSEL_32),
	GAIN_SCALE_GAIN(64, TEST_GSEL_64),
	GAIN_SCALE_GAIN(256, TEST_GSEL_256),
	GAIN_SCALE_GAIN(512, TEST_GSEL_512),
	GAIN_SCALE_GAIN(1024, TEST_GSEL_1024),
	GAIN_SCALE_GAIN(2048, TEST_GSEL_2048),
	GAIN_SCALE_GAIN(4096, TEST_GSEL_4096),
#define HWGAIN_MAX 4096
};

static const struct iio_itime_sel_mul gts_test_itimes[] = {
	GAIN_SCALE_ITIME_US(400 * 1000, TEST_TSEL_400, 8),
	GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4),
	GAIN_SCALE_ITIME_US(100 * 1000, TEST_TSEL_100, 2),
	GAIN_SCALE_ITIME_US(50 * 1000, TEST_TSEL_50, 1),
#define TIMEGAIN_MAX 8
};
#define TOTAL_GAIN_MAX	(HWGAIN_MAX * TIMEGAIN_MAX)
#define IIO_GTS_TEST_DEV "iio-gts-test-dev"

static struct device *__test_init_iio_gain_scale(struct kunit *test,
		struct iio_gts *gts, const struct iio_gain_sel_pair *g_table,
		int num_g, const struct iio_itime_sel_mul *i_table, int num_i)
{
	struct device *dev;
	int ret;

	dev = kunit_device_register(test, IIO_GTS_TEST_DEV);

	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
	if (IS_ERR_OR_NULL(dev))
		return NULL;

	ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, g_table, num_g,
				    i_table, num_i, gts);
	KUNIT_EXPECT_EQ(test, 0, ret);
	if (ret)
		return NULL;

	return dev;
}

#define test_init_iio_gain_scale(test, gts)	\
	__test_init_iio_gain_scale(test, gts, gts_test_gains, \
				   ARRAY_SIZE(gts_test_gains), gts_test_itimes, \
				   ARRAY_SIZE(gts_test_itimes))

static void test_init_iio_gts_invalid(struct kunit *test)
{
	struct device *dev;
	int ret;
	const struct iio_itime_sel_mul itimes_neg[] = {
		GAIN_SCALE_ITIME_US(-10, TEST_TSEL_400, 8),
		GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4),
	};
	const struct iio_gain_sel_pair gains_neg[] = {
		GAIN_SCALE_GAIN(1, TEST_GSEL_1),
		GAIN_SCALE_GAIN(2, TEST_GSEL_4),
		GAIN_SCALE_GAIN(-2, TEST_GSEL_16),
	};
	/* 55555 * 38656 = 2147534080 => overflows 32bit int */
	const struct iio_itime_sel_mul itimes_overflow[] = {
		GAIN_SCALE_ITIME_US(400 * 1000, TEST_TSEL_400, 55555),
		GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4),
	};
	const struct iio_gain_sel_pair gains_overflow[] = {
		GAIN_SCALE_GAIN(1, TEST_GSEL_1),
		GAIN_SCALE_GAIN(2, TEST_GSEL_4),
		GAIN_SCALE_GAIN(38656, TEST_GSEL_16),
	};

	dev = kunit_device_register(test, IIO_GTS_TEST_DEV);
	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
	if (!dev)
		return;

	/* Ok gains, negative time */
	ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, gts_test_gains,
				    ARRAY_SIZE(gts_test_gains), itimes_neg,
				    ARRAY_SIZE(itimes_neg), &gts);
	KUNIT_EXPECT_EQ(test, -EINVAL, ret);

	/* Ok times, negative gain */
	ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, gains_neg,
				    ARRAY_SIZE(gains_neg), gts_test_itimes,
				    ARRAY_SIZE(gts_test_itimes), &gts);
	KUNIT_EXPECT_EQ(test, -EINVAL, ret);

	/* gain * time overflow int */
	ret = devm_iio_init_iio_gts(dev, TEST_SCALE_1X, 0, gains_overflow,
				    ARRAY_SIZE(gains_overflow), itimes_overflow,
				    ARRAY_SIZE(itimes_overflow), &gts);
	KUNIT_EXPECT_EQ(test, -EOVERFLOW, ret);
}

static void test_iio_gts_find_gain_for_scale_using_time(struct kunit *test)
{
	struct device *dev;
	int ret, gain_sel;

	dev = test_init_iio_gain_scale(test, &gts);
	if (!dev)
		return;

	ret = iio_gts_find_gain_sel_for_scale_using_time(&gts, TEST_TSEL_100,
						TEST_SCALE_8X, 0, &gain_sel);
	/*
	 * Meas time 100 => gain by time 2x
	 * TEST_SCALE_8X matches total gain 8x
	 * => required HWGAIN 4x
	 */
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, TEST_GSEL_4, gain_sel);

	ret = iio_gts_find_gain_sel_for_scale_using_time(&gts, TEST_TSEL_200, 0,
						TEST_SCALE_NANO_256X, &gain_sel);
	/*
	 * Meas time 200 => gain by time 4x
	 * TEST_SCALE_256X matches total gain 256x
	 * => required HWGAIN 256/4 => 64x
	 */
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, TEST_GSEL_64, gain_sel);

	/* Min time, Min gain */
	ret = iio_gts_find_gain_sel_for_scale_using_time(&gts, TEST_TSEL_X_MIN,
						TEST_SCALE_MIN_X, 0, &gain_sel);
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, TEST_GSEL_1, gain_sel);

	/* Max time, Max gain */
	ret = iio_gts_find_gain_sel_for_scale_using_time(&gts, TEST_TSEL_X_MAX,
					0, TEST_SCALE_NANO_MAX_X, &gain_sel);
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, TEST_GSEL_4096, gain_sel);

	ret = iio_gts_find_gain_sel_for_scale_using_time(&gts, TEST_TSEL_100, 0,
						TEST_SCALE_NANO_256X, &gain_sel);
	/*
	 * Meas time 100 => gain by time 2x
	 * TEST_SCALE_256X matches total gain 256x
	 * => required HWGAIN 256/2 => 128x (not in gain-table - unsupported)
	 */
	KUNIT_EXPECT_NE(test, 0, ret);

	ret = iio_gts_find_gain_sel_for_scale_using_time(&gts, TEST_TSEL_200, 0,
						TEST_SCALE_NANO_MAX_X, &gain_sel);
	/* We can't reach the max gain with integration time smaller than MAX */
	KUNIT_EXPECT_NE(test, 0, ret);

	ret = iio_gts_find_gain_sel_for_scale_using_time(&gts, TEST_TSEL_50, 0,
						TEST_SCALE_NANO_MAX_X, &gain_sel);
	/* We can't reach the max gain with integration time smaller than MAX */
	KUNIT_EXPECT_NE(test, 0, ret);
}

static void test_iio_gts_find_new_gain_sel_by_old_gain_time(struct kunit *test)
{
	struct device *dev;
	int ret, old_gain, new_gain, old_time_sel, new_time_sel;

	dev = test_init_iio_gain_scale(test, &gts);
	if (!dev)
		return;

	old_gain = 32;
	old_time_sel = TEST_TSEL_200;
	new_time_sel = TEST_TSEL_400;

	ret = iio_gts_find_new_gain_sel_by_old_gain_time(&gts, old_gain,
					old_time_sel, new_time_sel, &new_gain);
	KUNIT_EXPECT_EQ(test, 0, ret);
	/*
	 * Doubling the integration time doubles the total gain - so old
	 * (hw)gain must be divided by two to compensate. => 32 / 2 => 16
	 */
	KUNIT_EXPECT_EQ(test, 16, new_gain);

	old_gain = 4;
	old_time_sel = TEST_TSEL_50;
	new_time_sel = TEST_TSEL_200;
	ret = iio_gts_find_new_gain_sel_by_old_gain_time(&gts, old_gain,
					old_time_sel, new_time_sel, &new_gain);
	KUNIT_EXPECT_EQ(test, 0, ret);
	/*
	 * gain by time 1x => 4x - (hw)gain 4x => 1x
	 */
	KUNIT_EXPECT_EQ(test, 1, new_gain);

	old_gain = 512;
	old_time_sel = TEST_TSEL_400;
	new_time_sel = TEST_TSEL_50;
	ret = iio_gts_find_new_gain_sel_by_old_gain_time(&gts, old_gain,
					old_time_sel, new_time_sel, &new_gain);
	KUNIT_EXPECT_EQ(test, 0, ret);
	/*
	 * gain by time 8x => 1x - (hw)gain 512x => 4096x)
	 */
	KUNIT_EXPECT_EQ(test, 4096, new_gain);

	/* Unsupported gain 2x */
	old_gain = 4;
	old_time_sel = TEST_TSEL_200;
	new_time_sel = TEST_TSEL_400;
	ret = iio_gts_find_new_gain_sel_by_old_gain_time(&gts, old_gain,
					old_time_sel, new_time_sel, &new_gain);
	KUNIT_EXPECT_NE(test, 0, ret);

	/* Too small gain */
	old_gain = 4;
	old_time_sel = TEST_TSEL_50;
	new_time_sel = TEST_TSEL_400;
	ret = iio_gts_find_new_gain_sel_by_old_gain_time(&gts, old_gain,
					old_time_sel, new_time_sel, &new_gain);
	KUNIT_EXPECT_NE(test, 0, ret);

	/* Too big gain */
	old_gain = 1024;
	old_time_sel = TEST_TSEL_400;
	new_time_sel = TEST_TSEL_50;
	ret = iio_gts_find_new_gain_sel_by_old_gain_time(&gts, old_gain,
					old_time_sel, new_time_sel, &new_gain);
	KUNIT_EXPECT_NE(test, 0, ret);

}

static void test_iio_find_closest_gain_low(struct kunit *test)
{
	struct device *dev;
	bool in_range;
	int ret;

	const struct iio_gain_sel_pair gts_test_gains_gain_low[] = {
		GAIN_SCALE_GAIN(4, TEST_GSEL_4),
		GAIN_SCALE_GAIN(16, TEST_GSEL_16),
		GAIN_SCALE_GAIN(32, TEST_GSEL_32),
	};

	dev = test_init_iio_gain_scale(test, &gts);
	if (!dev)
		return;

	ret = iio_find_closest_gain_low(&gts, 2, &in_range);
	KUNIT_EXPECT_EQ(test, 1, ret);
	KUNIT_EXPECT_EQ(test, true, in_range);

	ret = iio_find_closest_gain_low(&gts, 1, &in_range);
	KUNIT_EXPECT_EQ(test, 1, ret);
	KUNIT_EXPECT_EQ(test, true, in_range);

	ret = iio_find_closest_gain_low(&gts, 4095, &in_range);
	KUNIT_EXPECT_EQ(test, 2048, ret);
	KUNIT_EXPECT_EQ(test, true, in_range);

	ret = iio_find_closest_gain_low(&gts, 4097, &in_range);
	KUNIT_EXPECT_EQ(test, 4096, ret);
	KUNIT_EXPECT_EQ(test, false, in_range);

	kunit_device_unregister(test, dev);

	dev = __test_init_iio_gain_scale(test, &gts, gts_test_gains_gain_low,
				ARRAY_SIZE(gts_test_gains_gain_low),
				gts_test_itimes, ARRAY_SIZE(gts_test_itimes));
	if (!dev)
		return;

	ret = iio_find_closest_gain_low(&gts, 3, &in_range);
	KUNIT_EXPECT_EQ(test, -EINVAL, ret);
	KUNIT_EXPECT_EQ(test, false, in_range);
}

static void test_iio_gts_total_gain_to_scale(struct kunit *test)
{
	struct device *dev;
	int ret, scale_int, scale_nano;

	dev = test_init_iio_gain_scale(test, &gts);
	if (!dev)
		return;

	ret = iio_gts_total_gain_to_scale(&gts, 1, &scale_int, &scale_nano);
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, TEST_SCALE_1X, scale_int);
	KUNIT_EXPECT_EQ(test, 0, scale_nano);

	ret = iio_gts_total_gain_to_scale(&gts, 1, &scale_int, &scale_nano);
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, TEST_SCALE_1X, scale_int);
	KUNIT_EXPECT_EQ(test, 0, scale_nano);

	ret = iio_gts_total_gain_to_scale(&gts, 4096 * 8, &scale_int,
					  &scale_nano);
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, 0, scale_int);
	KUNIT_EXPECT_EQ(test, TEST_SCALE_NANO_4096X8, scale_nano);
}

static void test_iio_gts_chk_times(struct kunit *test, const int *vals)
{
	static const int expected[] = {0, 50000, 0, 100000, 0, 200000, 0, 400000};
	int i;

	for (i = 0; i < ARRAY_SIZE(expected); i++)
		KUNIT_EXPECT_EQ(test, expected[i], vals[i]);
}

static void test_iio_gts_chk_scales_all(struct kunit *test, struct iio_gts *gts,
					const int *vals, int len)
{
	static const int gains[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
				    1024, 2048, 4096, 4096 * 2, 4096 * 4,
				    4096 * 8};
	int expected[ARRAY_SIZE(gains) * 2];
	int i, ret;
	int exp_len = ARRAY_SIZE(gains) * 2;

	KUNIT_EXPECT_EQ(test, exp_len, len);
	if (len != exp_len)
		return;

	for (i = 0; i < ARRAY_SIZE(gains); i++) {
		ret = iio_gts_total_gain_to_scale(gts, gains[i],
						  &expected[2 * i],
						  &expected[2 * i + 1]);
		KUNIT_EXPECT_EQ(test, 0, ret);
		if (ret)
			return;
	}

	for (i = 0; i < ARRAY_SIZE(expected); i++)
		KUNIT_EXPECT_EQ(test, expected[i], vals[i]);
}

static void test_iio_gts_chk_scales_t200(struct kunit *test, struct iio_gts *gts,
					 const int *vals, int len)
{
	/* The gain caused by time 200 is 4x */
	static const int gains[] = {
		1 * 4,
		4 * 4,
		16 * 4,
		32 * 4,
		64 * 4,
		256 * 4,
		512 * 4,
		1024 * 4,
		2048 * 4,
		4096 * 4
	};
	int expected[ARRAY_SIZE(gains) * 2];
	int i, ret;

	KUNIT_EXPECT_EQ(test, 2 * ARRAY_SIZE(gains), len);
	if (len < 2 * ARRAY_SIZE(gains))
		return;

	for (i = 0; i < ARRAY_SIZE(gains); i++) {
		ret = iio_gts_total_gain_to_scale(gts, gains[i],
						  &expected[2 * i],
						  &expected[2 * i + 1]);
		KUNIT_EXPECT_EQ(test, 0, ret);
		if (ret)
			return;
	}

	for (i = 0; i < ARRAY_SIZE(expected); i++)
		KUNIT_EXPECT_EQ(test, expected[i], vals[i]);
}

static void test_iio_gts_avail_test(struct kunit *test)
{
	struct device *dev;
	int ret;
	int type, len;
	const int *vals;

	dev = test_init_iio_gain_scale(test, &gts);
	if (!dev)
		return;

	/* test table building for times and iio_gts_avail_times() */
	ret = iio_gts_avail_times(&gts, &vals, &type, &len);
	KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret);
	if (ret)
		return;

	KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_MICRO, type);
	KUNIT_EXPECT_EQ(test, 8, len);
	if (len < 8)
		return;

	test_iio_gts_chk_times(test, vals);

	/* Test table building for all scales and iio_gts_all_avail_scales() */
	ret = iio_gts_all_avail_scales(&gts, &vals, &type, &len);
	KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret);
	if (ret)
		return;

	KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_NANO, type);

	test_iio_gts_chk_scales_all(test, &gts, vals, len);

	/*
	 * Test table building for scales/time and
	 * iio_gts_avail_scales_for_time()
	 */
	ret = iio_gts_avail_scales_for_time(&gts, 200000, &vals, &type, &len);
	KUNIT_EXPECT_EQ(test, IIO_AVAIL_LIST, ret);
	if (ret)
		return;

	KUNIT_EXPECT_EQ(test, IIO_VAL_INT_PLUS_NANO, type);
	test_iio_gts_chk_scales_t200(test, &gts, vals, len);
}

static struct kunit_case iio_gts_test_cases[] = {
	KUNIT_CASE(test_init_iio_gts_invalid),
	KUNIT_CASE(test_iio_gts_find_gain_for_scale_using_time),
	KUNIT_CASE(test_iio_gts_find_new_gain_sel_by_old_gain_time),
	KUNIT_CASE(test_iio_find_closest_gain_low),
	KUNIT_CASE(test_iio_gts_total_gain_to_scale),
	KUNIT_CASE(test_iio_gts_avail_test),
	{}
};

static struct kunit_suite iio_gts_test_suite = {
	.name = "iio-gain-time-scale",
	.test_cases = iio_gts_test_cases,
};

kunit_test_suite(iio_gts_test_suite);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
MODULE_DESCRIPTION("Test IIO light sensor gain-time-scale helpers");
MODULE_IMPORT_NS(IIO_GTS_HELPER);