xref: /linux/tools/testing/selftests/gpio/gpio-mockup-sysfs.sh (revision 8bc395a6a2e24bfae934e43c7f968b23f9b9b55f)
122f6592bSBamvor Jian Zhang
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
3*8bc395a6SKent Gibson
4*8bc395a6SKent Gibson# Overrides functions in gpio-mockup.sh to test using the GPIO SYSFS uAPI
5*8bc395a6SKent Gibson
6*8bc395a6SKent GibsonSYSFS=`grep -w sysfs /proc/mounts | cut -f2 -d' '`
7*8bc395a6SKent Gibson[ -d "$SYSFS" ] || skip "sysfs is not mounted"
8*8bc395a6SKent Gibson
9*8bc395a6SKent GibsonGPIO_SYSFS="${SYSFS}/class/gpio"
10*8bc395a6SKent Gibson[ -d "$GPIO_SYSFS" ] || skip "CONFIG_GPIO_SYSFS is not selected"
11*8bc395a6SKent Gibson
12*8bc395a6SKent GibsonPLATFORM_SYSFS=$SYSFS/devices/platform
13*8bc395a6SKent Gibson
14*8bc395a6SKent Gibsonsysfs_nr=
15*8bc395a6SKent Gibsonsysfs_ldir=
16*8bc395a6SKent Gibson
17*8bc395a6SKent Gibson# determine the sysfs GPIO number given the $chip and $offset
18*8bc395a6SKent Gibson# e.g. gpiochip1:32
19*8bc395a6SKent Gibsonfind_sysfs_nr()
2022f6592bSBamvor Jian Zhang{
21*8bc395a6SKent Gibson	# e.g. /sys/devices/platform/gpio-mockup.1/gpiochip1
22*8bc395a6SKent Gibson	local platform=$(find $PLATFORM_SYSFS -mindepth 2 -maxdepth 2 -type d -name $chip)
23*8bc395a6SKent Gibson	[ "$platform" ] || fail "can't find platform of $chip"
24*8bc395a6SKent Gibson	# e.g. /sys/devices/platform/gpio-mockup.1/gpio/gpiochip508/base
25*8bc395a6SKent Gibson	local base=$(find ${platform%/*}/gpio/ -mindepth 2 -maxdepth 2 -type f -name base)
26*8bc395a6SKent Gibson	[ "$base" ] || fail "can't find base of $chip"
27*8bc395a6SKent Gibson	sysfs_nr=$(($(< "$base") + $offset))
28*8bc395a6SKent Gibson	sysfs_ldir="$GPIO_SYSFS/gpio$sysfs_nr"
2922f6592bSBamvor Jian Zhang}
3022f6592bSBamvor Jian Zhang
31*8bc395a6SKent Gibsonacquire_line()
3222f6592bSBamvor Jian Zhang{
33*8bc395a6SKent Gibson	[ "$sysfs_nr" ] && return
34*8bc395a6SKent Gibson	find_sysfs_nr
35*8bc395a6SKent Gibson	echo "$sysfs_nr" > "$GPIO_SYSFS/export"
3622f6592bSBamvor Jian Zhang}
3722f6592bSBamvor Jian Zhang
38*8bc395a6SKent Gibson# The helpers being overridden...
39*8bc395a6SKent Gibsonget_line()
4022f6592bSBamvor Jian Zhang{
41*8bc395a6SKent Gibson	[ -e "$sysfs_ldir/value" ] && echo $(< "$sysfs_ldir/value")
4222f6592bSBamvor Jian Zhang}
4322f6592bSBamvor Jian Zhang
44*8bc395a6SKent Gibsonset_line()
4522f6592bSBamvor Jian Zhang{
46*8bc395a6SKent Gibson	acquire_line
4722f6592bSBamvor Jian Zhang
48*8bc395a6SKent Gibson	for option in $*; do
49*8bc395a6SKent Gibson		case $option in
50*8bc395a6SKent Gibson		active-high)
51*8bc395a6SKent Gibson			echo 0 > "$sysfs_ldir/active_low"
52*8bc395a6SKent Gibson			;;
53*8bc395a6SKent Gibson		active-low)
54*8bc395a6SKent Gibson			echo 1 > "$sysfs_ldir/active_low"
55*8bc395a6SKent Gibson			;;
56*8bc395a6SKent Gibson		input)
57*8bc395a6SKent Gibson			echo "in" > "$sysfs_ldir/direction"
58*8bc395a6SKent Gibson			;;
59*8bc395a6SKent Gibson		0)
60*8bc395a6SKent Gibson			echo "out" > "$sysfs_ldir/direction"
61*8bc395a6SKent Gibson			echo 0 > "$sysfs_ldir/value"
62*8bc395a6SKent Gibson			;;
63*8bc395a6SKent Gibson		1)
64*8bc395a6SKent Gibson			echo "out" > "$sysfs_ldir/direction"
65*8bc395a6SKent Gibson			echo 1 > "$sysfs_ldir/value"
66*8bc395a6SKent Gibson			;;
67*8bc395a6SKent Gibson		esac
6822f6592bSBamvor Jian Zhang	done
6922f6592bSBamvor Jian Zhang}
7022f6592bSBamvor Jian Zhang
71*8bc395a6SKent Gibsonrelease_line()
72*8bc395a6SKent Gibson{
73*8bc395a6SKent Gibson	[ "$sysfs_nr" ] || return 0
74*8bc395a6SKent Gibson	echo "$sysfs_nr" > "$GPIO_SYSFS/unexport"
75*8bc395a6SKent Gibson	sysfs_nr=
76*8bc395a6SKent Gibson	sysfs_ldir=
77*8bc395a6SKent Gibson}
78