Back to home page

OSCL-LXR

 
 

    


0001 
0002 # SPDX-License-Identifier: GPL-2.0
0003 
0004 # Overrides functions in gpio-mockup.sh to test using the GPIO SYSFS uAPI
0005 
0006 SYSFS=`grep -w sysfs /proc/mounts | cut -f2 -d' '`
0007 [ -d "$SYSFS" ] || skip "sysfs is not mounted"
0008 
0009 GPIO_SYSFS="${SYSFS}/class/gpio"
0010 [ -d "$GPIO_SYSFS" ] || skip "CONFIG_GPIO_SYSFS is not selected"
0011 
0012 PLATFORM_SYSFS=$SYSFS/devices/platform
0013 
0014 sysfs_nr=
0015 sysfs_ldir=
0016 
0017 # determine the sysfs GPIO number given the $chip and $offset
0018 # e.g. gpiochip1:32
0019 find_sysfs_nr()
0020 {
0021         # e.g. /sys/devices/platform/gpio-mockup.1/gpiochip1
0022         local platform=$(find $PLATFORM_SYSFS -mindepth 2 -maxdepth 2 -type d -name $chip)
0023         [ "$platform" ] || fail "can't find platform of $chip"
0024         # e.g. /sys/devices/platform/gpio-mockup.1/gpio/gpiochip508/base
0025         local base=$(find ${platform%/*}/gpio/ -mindepth 2 -maxdepth 2 -type f -name base)
0026         [ "$base" ] || fail "can't find base of $chip"
0027         sysfs_nr=$(($(< "$base") + $offset))
0028         sysfs_ldir="$GPIO_SYSFS/gpio$sysfs_nr"
0029 }
0030 
0031 acquire_line()
0032 {
0033         [ "$sysfs_nr" ] && return
0034         find_sysfs_nr
0035         echo "$sysfs_nr" > "$GPIO_SYSFS/export"
0036 }
0037 
0038 # The helpers being overridden...
0039 get_line()
0040 {
0041         [ -e "$sysfs_ldir/value" ] && echo $(< "$sysfs_ldir/value")
0042 }
0043 
0044 set_line()
0045 {
0046         acquire_line
0047 
0048         for option in $*; do
0049                 case $option in
0050                 active-high)
0051                         echo 0 > "$sysfs_ldir/active_low"
0052                         ;;
0053                 active-low)
0054                         echo 1 > "$sysfs_ldir/active_low"
0055                         ;;
0056                 input)
0057                         echo "in" > "$sysfs_ldir/direction"
0058                         ;;
0059                 0)
0060                         echo "out" > "$sysfs_ldir/direction"
0061                         echo 0 > "$sysfs_ldir/value"
0062                         ;;
0063                 1)
0064                         echo "out" > "$sysfs_ldir/direction"
0065                         echo 1 > "$sysfs_ldir/value"
0066                         ;;
0067                 esac
0068         done
0069 }
0070 
0071 release_line()
0072 {
0073         [ "$sysfs_nr" ] || return 0
0074         echo "$sysfs_nr" > "$GPIO_SYSFS/unexport"
0075         sysfs_nr=
0076         sysfs_ldir=
0077 }