Path: blob/master/tools/testing/selftests/gpio/gpio-mockup-sysfs.sh
26285 views
1# SPDX-License-Identifier: GPL-2.023# Overrides functions in gpio-mockup.sh to test using the GPIO SYSFS uAPI45SYSFS=`grep -w sysfs /proc/mounts | cut -f2 -d' '`6[ -d "$SYSFS" ] || skip "sysfs is not mounted"78GPIO_SYSFS="${SYSFS}/class/gpio"9[ -d "$GPIO_SYSFS" ] || skip "CONFIG_GPIO_SYSFS is not selected"1011PLATFORM_SYSFS=$SYSFS/devices/platform1213sysfs_nr=14sysfs_ldir=1516# determine the sysfs GPIO number given the $chip and $offset17# e.g. gpiochip1:3218find_sysfs_nr()19{20# e.g. /sys/devices/platform/gpio-mockup.1/gpiochip121local platform=$(find $PLATFORM_SYSFS -mindepth 2 -maxdepth 2 -type d -name $chip)22[ "$platform" ] || fail "can't find platform of $chip"23# e.g. /sys/devices/platform/gpio-mockup.1/gpio/gpiochip508/base24local base=$(find ${platform%/*}/gpio/ -mindepth 2 -maxdepth 2 -type f -name base)25[ "$base" ] || fail "can't find base of $chip"26sysfs_nr=$(($(< "$base") + $offset))27sysfs_ldir="$GPIO_SYSFS/gpio$sysfs_nr"28}2930acquire_line()31{32[ "$sysfs_nr" ] && return33find_sysfs_nr34echo "$sysfs_nr" > "$GPIO_SYSFS/export"35}3637# The helpers being overridden...38get_line()39{40[ -e "$sysfs_ldir/value" ] && echo $(< "$sysfs_ldir/value")41}4243set_line()44{45acquire_line4647for option in $*; do48case $option in49active-high)50echo 0 > "$sysfs_ldir/active_low"51;;52active-low)53echo 1 > "$sysfs_ldir/active_low"54;;55input)56echo "in" > "$sysfs_ldir/direction"57;;580)59echo "out" > "$sysfs_ldir/direction"60echo 0 > "$sysfs_ldir/value"61;;621)63echo "out" > "$sysfs_ldir/direction"64echo 1 > "$sysfs_ldir/value"65;;66esac67done68}6970release_line()71{72[ "$sysfs_nr" ] || return 073echo "$sysfs_nr" > "$GPIO_SYSFS/unexport"74sysfs_nr=75sysfs_ldir=76}777879