summaryrefslogtreecommitdiffstats
path: root/common/recipes-utils/openbmc-utils/files/openbmc-utils.sh
blob: 885a6eb63ce28cf9db59f84fa717319edad689fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA

DEVMEM=/sbin/devmem
SHADOW_GPIO=/tmp/gpionames

devmem_set_bit() {
    local addr
    local val
    addr=$1
    val=$($DEVMEM $addr)
    val=$((val | (0x1 << $2)))
    $DEVMEM $addr 32 $val
}

devmem_clear_bit() {
    local addr
    local val
    addr=$1
    val=$($DEVMEM $addr)
    val=$((val & ~(0x1 << $2)))
    $DEVMEM $addr 32 $val
}

GPIODIR="/sys/class/gpio"
GPIOEXPORT="$GPIODIR/export"

gpio_dir() {
    echo "$GPIODIR/gpio$1"
}

gpio_name2value() {
    local first remaining base val
    remaining=$1
    base="${SHADOW_GPIO}/${remaining}"
    if [ -L "${base}" ]; then
        val=$(readlink -f ${base} 2>/dev/null)
        if [ -n "${val}" ]; then
            val=${val##*gpio}
            if [ -n "${val}" ]; then
                echo "$val"
                return
            fi
        fi
    fi
    val=0
    while [ -n "$remaining" ]; do
        first=${remaining:0:1}
        case "$first" in
            [[:lower:]])
                base=$(printf "%d" "'$first'")
                base=$((base - 96))
                val=$((val * 26 + base))
                ;;
            [[:upper:]])
                base=$(printf "%d" "'$first'")
                base=$((base - 64))
                val=$((val * 26 + base))
                ;;
            *)
                if [ $val -gt 0 ]; then
                    val=$((val-1))
                fi
                val=$((val * 8 + $remaining))
                break
                ;;
        esac
        remaining=${remaining:1}
    done
    echo "$val"
}

gpio_export() {
    local gpio
    gpio=$(gpio_name2value $1)
    dir=$(gpio_dir $gpio)
    if [ ! -d ${dir} ]; then
        echo $gpio > $GPIOEXPORT
    fi
    if [ $# -gt 1 ]; then
        if [ ! -d  $SHADOW_GPIO ]; then
            mkdir -p $SHADOW_GPIO
        fi
        ln -s $dir $SHADOW_GPIO/$2
    fi
}

gpio_set() {
    local gpio
    local val
    gpio=$(gpio_name2value $1)
    val=$2
    dir=$(gpio_dir $gpio)
    if [ ! -d ${dir} ]; then
        echo $gpio > $GPIOEXPORT
    fi
    echo $val > ${dir}/value
    echo out > ${dir}/direction
}

gpio_get() {
    local gpio
    local val
    gpio=$(gpio_name2value $1)
    dir=$(gpio_dir $gpio)
    if [ ! -d ${dir} ]; then
        echo $gpio > $GPIOEXPORT
    fi
    echo in > ${dir}/direction
    cat ${dir}/value
}

if [ -f "/usr/local/bin/soc-utils.sh" ]; then
    source "/usr/local/bin/soc-utils.sh"
fi

if [ -f "/usr/local/bin/board-utils.sh" ]; then
    source "/usr/local/bin/board-utils.sh"
fi
OpenPOWER on IntegriCloud