gpio-mockup-sysfs.sh 1.7 KB

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