xref: /linux/tools/testing/selftests/gpio/gpio-mockup-sysfs.sh (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1
2# SPDX-License-Identifier: GPL-2.0
3
4# Overrides functions in gpio-mockup.sh to test using the GPIO SYSFS uAPI
5
6SYSFS=`grep -w sysfs /proc/mounts | cut -f2 -d' '`
7[ -d "$SYSFS" ] || skip "sysfs is not mounted"
8
9GPIO_SYSFS="${SYSFS}/class/gpio"
10[ -d "$GPIO_SYSFS" ] || skip "CONFIG_GPIO_SYSFS is not selected"
11
12PLATFORM_SYSFS=$SYSFS/devices/platform
13
14sysfs_nr=
15sysfs_ldir=
16
17# determine the sysfs GPIO number given the $chip and $offset
18# e.g. gpiochip1:32
19find_sysfs_nr()
20{
21	# e.g. /sys/devices/platform/gpio-mockup.1/gpiochip1
22	local platform=$(find $PLATFORM_SYSFS -mindepth 2 -maxdepth 2 -type d -name $chip)
23	[ "$platform" ] || fail "can't find platform of $chip"
24	# e.g. /sys/devices/platform/gpio-mockup.1/gpio/gpiochip508/base
25	local base=$(find ${platform%/*}/gpio/ -mindepth 2 -maxdepth 2 -type f -name base)
26	[ "$base" ] || fail "can't find base of $chip"
27	sysfs_nr=$(($(< "$base") + $offset))
28	sysfs_ldir="$GPIO_SYSFS/gpio$sysfs_nr"
29}
30
31acquire_line()
32{
33	[ "$sysfs_nr" ] && return
34	find_sysfs_nr
35	echo "$sysfs_nr" > "$GPIO_SYSFS/export"
36}
37
38# The helpers being overridden...
39get_line()
40{
41	[ -e "$sysfs_ldir/value" ] && echo $(< "$sysfs_ldir/value")
42}
43
44set_line()
45{
46	acquire_line
47
48	for option in $*; do
49		case $option in
50		active-high)
51			echo 0 > "$sysfs_ldir/active_low"
52			;;
53		active-low)
54			echo 1 > "$sysfs_ldir/active_low"
55			;;
56		input)
57			echo "in" > "$sysfs_ldir/direction"
58			;;
59		0)
60			echo "out" > "$sysfs_ldir/direction"
61			echo 0 > "$sysfs_ldir/value"
62			;;
63		1)
64			echo "out" > "$sysfs_ldir/direction"
65			echo 1 > "$sysfs_ldir/value"
66			;;
67		esac
68	done
69}
70
71release_line()
72{
73	[ "$sysfs_nr" ] || return 0
74	echo "$sysfs_nr" > "$GPIO_SYSFS/unexport"
75	sysfs_nr=
76	sysfs_ldir=
77}
78