1#!/vendor/bin/sh
2#
3# edge_sense_init.sh [<power chip name>] [<power gpio number>]
4#
5# Initialize Edge Sense.  If needed, power it up using the power controller chip
6# specified by [<power chip name>] and power GPIO specified by
7# [<power gpio number>].
8#
9#   [<power chip name>]     Name of chip (e.g., "pm8998") controlling Edge Sense
10#                           power.
11#   [<power gpio number>]   GPIO number controlling Edge Sens power (e.g., 2).
12#
13# [<power chip name>] and [<power gpio number>] default to values appropriate
14# for the type and version of device.
15#
16# TODO: b/67205273
17#       The Edge Sense should only be powered up when it's in use.
18#       Ideally, this would be done in the Edge Sense SLPI driver, but it
19#       doesn't have direct access to the PM8998 GPIOs.
20#       As an alternative, the Elmyra Edge Sense sensor HAL driver could power
21#       up the Edge Sense or act as a GPIO proxy for the SLPI driver.
22#
23
24# Check for default values.
25if [ "${#}" -eq 0 ]; then
26  use_defaults=1
27else
28  use_defaults=0
29fi
30
31# Get the program name.
32prog_name=$(basename ${0})
33
34# Read the power chip name.
35chip_name=${1}
36
37# Read the power gpio number.
38gpio_num=${2}
39
40# Get the hardware platform and platform version.
41hw_platform=`cat /sys/devices/soc0/hw_platform`
42platform_version=`cat /sys/devices/soc0/platform_version`
43
44# Set default values if using them.
45if [ ${use_defaults} -ne 0 ]; then
46  chip_name=pm660@
47  gpio_num=3
48fi
49
50# Validate chip name and gpio number.
51if [ -z ${chip_name} ]; then
52  log -t "${prog_name}" "Chip name not specified."
53  exit 1
54fi
55if [ -z ${gpio_num} ]; then
56  log -t "${prog_name}" "GPIO number not specified."
57  exit 1
58fi
59
60# Find the GPIO pin control device node for the power chip.
61pinctrl=`find /sys/devices -name "*${chip_name}*pinctrl*"`
62if [ -z ${pinctrl} ]; then
63  log -t "${prog_name}" "Power chip \"${chip_name}\" not found."
64  exit 1
65fi
66
67# Find the GPIO index within the chip GPIO interrupt name list.  This will be
68# the GPIO index offset from the chip GPIO index base.
69found=0
70gpio_name=gpio${gpio_num}
71gpio_index_off=0
72while IFS= read -d '' name; do
73  # Check for a match.
74  if [ "${name%${gpio_name}}" != "${name}" ]; then
75    found=1
76    break
77  fi
78
79  # Check next GPIO index.
80  gpio_index_off=$((${gpio_index_off} + 1))
81done < ${pinctrl}/of_node/interrupt-names
82if [ ${found} -eq 0 ]; then
83  log -t "${prog_name}" "GPIO ${gpio_num} on chip \"${chip_name}\" not found."
84  exit 1
85fi
86
87# Find the chip GPIO base index.
88base_file=`find ${pinctrl} -name base`
89gpio_index_base=`cat ${base_file}`
90
91# Get the GPIO index.
92gpio_index=$((${gpio_index_base} + ${gpio_index_off}))
93
94# Export the GPIO.
95echo ${gpio_index} > /sys/class/gpio/export
96
97# Set the GPIO direction to out.
98echo out > /sys/class/gpio/gpio${gpio_index}/direction
99
100# Set the GPIO high.
101echo 1 > /sys/class/gpio/gpio${gpio_index}/value
102
103
104