78 lines · bash
1 2# SPDX-License-Identifier: GPL-2.03 4# Overrides functions in gpio-mockup.sh to test using the GPIO SYSFS uAPI5 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/platform13 14sysfs_nr=15sysfs_ldir=16 17# determine the sysfs GPIO number given the $chip and $offset18# e.g. gpiochip1:3219find_sysfs_nr()20{21 # e.g. /sys/devices/platform/gpio-mockup.1/gpiochip122 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/base25 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" ] && return34 find_sysfs_nr35 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_line47 48 for option in $*; do49 case $option in50 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 esac68 done69}70 71release_line()72{73 [ "$sysfs_nr" ] || return 074 echo "$sysfs_nr" > "$GPIO_SYSFS/unexport"75 sysfs_nr=76 sysfs_ldir=77}78