blob: 8c3d9ce0296eeea226066c55f2bdfb3312d561ae [file] [log] [blame]
Erik Staatse7d3e472017-11-06 16:47:20 -08001#!/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# If using default values, check if platform needs Edge Sense to be initialized.
45if [ ${use_defaults} -ne 0 ]; then
46 if [ "${hw_platform}" != "OEM" ] || \
47 [ "${platform_version}" == "65538" ]; then
48 log -t "${prog_name}" "Platform \"${hw_platform}\" version" \
49 "${platform_version} does not need Edge Sense to be initialized."
50 exit
51 fi
52fi
53
54# Set default values if using them.
55if [ ${use_defaults} -ne 0 ]; then
56 chip_name=pm8998
57 gpio_num=2
58fi
59
60# Validate chip name and gpio number.
61if [ -z ${chip_name} ]; then
62 log -t "${prog_name}" "Chip name not specified."
63 exit 1
64fi
65if [ -z ${gpio_num} ]; then
66 log -t "${prog_name}" "GPIO number not specified."
67 exit 1
68fi
69
70# Find the GPIO pin control device node for the power chip.
71pinctrl=`find /sys/devices -name "*${chip_name}*pinctrl*"`
72if [ -z ${pinctrl} ]; then
73 log -t "${prog_name}" "Power chip \"${chip_name}\" not found."
74 exit 1
75fi
76
77# Find the GPIO index within the chip GPIO interrupt name list. This will be
78# the GPIO index offset from the chip GPIO index base.
79found=0
80gpio_name=gpio${gpio_num}
81gpio_index_off=0
82while IFS= read -d '' name; do
83 # Check for a match.
84 if [ "${name%${gpio_name}}" != "${name}" ]; then
85 found=1
86 break
87 fi
88
89 # Check next GPIO index.
90 gpio_index_off=$((${gpio_index_off} + 1))
91done < ${pinctrl}/of_node/interrupt-names
92if [ ${found} -eq 0 ]; then
93 log -t "${prog_name}" "GPIO ${gpio_num} on chip \"${chip_name}\" not found."
94 exit 1
95fi
96
97# Find the chip GPIO base index.
98base_file=`find ${pinctrl} -name base`
99gpio_index_base=`cat ${base_file}`
100
101# Get the GPIO index.
102gpio_index=$((${gpio_index_base} + ${gpio_index_off}))
103
104# Export the GPIO.
105echo ${gpio_index} > /sys/class/gpio/export
106
107# Set the GPIO direction to out.
108echo out > /sys/class/gpio/gpio${gpio_index}/direction
109
110# Set the GPIO high.
111echo 1 > /sys/class/gpio/gpio${gpio_index}/value
112
113