xref: /linux/tools/testing/selftests/power_supply/test_power_supply_properties.sh (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# Copyright (c) 2022, 2024 Collabora Ltd
5#
6# This test validates the power supply uAPI: namely, the files in sysfs and
7# lines in uevent that expose the power supply properties.
8#
9# By default all power supplies available are tested. Optionally the name of a
10# power supply can be passed as a parameter to test only that one instead.
11DIR="$(dirname "$(readlink -f "$0")")"
12
13. "${DIR}"/../kselftest/ktap_helpers.sh
14
15. "${DIR}"/helpers.sh
16
17count_tests() {
18	SUPPLIES=$1
19
20	# This needs to be updated every time a new test is added.
21	NUM_TESTS=33
22
23	total_tests=0
24
25	for i in $SUPPLIES; do
26		total_tests=$((total_tests + NUM_TESTS))
27	done
28
29	echo "$total_tests"
30}
31
32ktap_print_header
33
34SYSFS_SUPPLIES=/sys/class/power_supply/
35
36if [ $# -eq 0 ]; then
37	supplies=$(ls "$SYSFS_SUPPLIES")
38else
39	supplies=$1
40fi
41
42ktap_set_plan "$(count_tests "$supplies")"
43
44for DEVNAME in $supplies; do
45	ktap_print_msg Testing device "$DEVNAME"
46
47	if [ ! -d "$SYSFS_SUPPLIES"/"$DEVNAME" ]; then
48		ktap_test_fail "$DEVNAME".exists
49		ktap_exit_fail_msg Device does not exist
50	fi
51
52	ktap_test_pass "$DEVNAME".exists
53
54	test_uevent_prop NAME "$DEVNAME"
55
56	test_sysfs_prop type
57	SUPPLY_TYPE=$(cat "$SYSFS_SUPPLIES"/"$DEVNAME"/type)
58	# This fails on kernels < 5.8 (needs 2ad3d74e3c69f)
59	test_uevent_prop TYPE "$SUPPLY_TYPE"
60
61	test_sysfs_prop_optional usb_type
62
63	test_sysfs_prop_optional_range online 0 2
64	test_sysfs_prop_optional_range present 0 1
65
66	test_sysfs_prop_optional_list status "Unknown","Charging","Discharging","Not charging","Full"
67
68	# Capacity is reported as percentage, thus any value less than 0 and
69	# greater than 100 are not allowed.
70	test_sysfs_prop_optional_range capacity 0 100 "%"
71
72	test_sysfs_prop_optional_list capacity_level "Unknown","Critical","Low","Normal","High","Full"
73
74	test_sysfs_prop_optional model_name
75	test_sysfs_prop_optional manufacturer
76	test_sysfs_prop_optional serial_number
77	test_sysfs_prop_optional_list technology "Unknown","NiMH","Li-ion","Li-poly","LiFe","NiCd","LiMn"
78
79	test_sysfs_prop_optional cycle_count
80
81	test_sysfs_prop_optional_list scope "Unknown","System","Device"
82
83	test_sysfs_prop_optional input_current_limit "uA"
84	test_sysfs_prop_optional input_voltage_limit "uV"
85
86	# Technically the power-supply class does not limit reported values.
87	# E.g. one could expose an RTC backup-battery, which goes below 1.5V or
88	# an electric vehicle battery with over 300V. But most devices do not
89	# have a step-up capable regulator behind the battery and operate with
90	# voltages considered safe to touch, so we limit the allowed range to
91	# 1.8V-60V to catch drivers reporting incorrectly scaled values. E.g. a
92	# common mistake is reporting data in mV instead of µV.
93	test_sysfs_prop_optional_range voltage_now 1800000 60000000 "uV"
94	test_sysfs_prop_optional_range voltage_min 1800000 60000000 "uV"
95	test_sysfs_prop_optional_range voltage_max 1800000 60000000 "uV"
96	test_sysfs_prop_optional_range voltage_min_design 1800000 60000000 "uV"
97	test_sysfs_prop_optional_range voltage_max_design 1800000 60000000 "uV"
98
99	# current based systems
100	test_sysfs_prop_optional current_now "uA"
101	test_sysfs_prop_optional current_max "uA"
102	test_sysfs_prop_optional charge_now "uAh"
103	test_sysfs_prop_optional charge_full "uAh"
104	test_sysfs_prop_optional charge_full_design "uAh"
105
106	# power based systems
107	test_sysfs_prop_optional power_now "uW"
108	test_sysfs_prop_optional energy_now "uWh"
109	test_sysfs_prop_optional energy_full "uWh"
110	test_sysfs_prop_optional energy_full_design "uWh"
111	test_sysfs_prop_optional energy_full_design "uWh"
112done
113
114ktap_finished
115