Erik Staats | e7d3e47 | 2017-11-06 16:47:20 -0800 | [diff] [blame] | 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. |
| 25 | if [ "${#}" -eq 0 ]; then |
| 26 | use_defaults=1 |
| 27 | else |
| 28 | use_defaults=0 |
| 29 | fi |
| 30 | |
| 31 | # Get the program name. |
| 32 | prog_name=$(basename ${0}) |
| 33 | |
| 34 | # Read the power chip name. |
| 35 | chip_name=${1} |
| 36 | |
| 37 | # Read the power gpio number. |
| 38 | gpio_num=${2} |
| 39 | |
| 40 | # Get the hardware platform and platform version. |
| 41 | hw_platform=`cat /sys/devices/soc0/hw_platform` |
| 42 | platform_version=`cat /sys/devices/soc0/platform_version` |
| 43 | |
| 44 | # If using default values, check if platform needs Edge Sense to be initialized. |
| 45 | if [ ${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 |
| 52 | fi |
| 53 | |
| 54 | # Set default values if using them. |
| 55 | if [ ${use_defaults} -ne 0 ]; then |
| 56 | chip_name=pm8998 |
| 57 | gpio_num=2 |
| 58 | fi |
| 59 | |
| 60 | # Validate chip name and gpio number. |
| 61 | if [ -z ${chip_name} ]; then |
| 62 | log -t "${prog_name}" "Chip name not specified." |
| 63 | exit 1 |
| 64 | fi |
| 65 | if [ -z ${gpio_num} ]; then |
| 66 | log -t "${prog_name}" "GPIO number not specified." |
| 67 | exit 1 |
| 68 | fi |
| 69 | |
| 70 | # Find the GPIO pin control device node for the power chip. |
| 71 | pinctrl=`find /sys/devices -name "*${chip_name}*pinctrl*"` |
| 72 | if [ -z ${pinctrl} ]; then |
| 73 | log -t "${prog_name}" "Power chip \"${chip_name}\" not found." |
| 74 | exit 1 |
| 75 | fi |
| 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. |
| 79 | found=0 |
| 80 | gpio_name=gpio${gpio_num} |
| 81 | gpio_index_off=0 |
| 82 | while 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)) |
| 91 | done < ${pinctrl}/of_node/interrupt-names |
| 92 | if [ ${found} -eq 0 ]; then |
| 93 | log -t "${prog_name}" "GPIO ${gpio_num} on chip \"${chip_name}\" not found." |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | # Find the chip GPIO base index. |
| 98 | base_file=`find ${pinctrl} -name base` |
| 99 | gpio_index_base=`cat ${base_file}` |
| 100 | |
| 101 | # Get the GPIO index. |
| 102 | gpio_index=$((${gpio_index_base} + ${gpio_index_off})) |
| 103 | |
| 104 | # Export the GPIO. |
| 105 | echo ${gpio_index} > /sys/class/gpio/export |
| 106 | |
| 107 | # Set the GPIO direction to out. |
| 108 | echo out > /sys/class/gpio/gpio${gpio_index}/direction |
| 109 | |
| 110 | # Set the GPIO high. |
| 111 | echo 1 > /sys/class/gpio/gpio${gpio_index}/value |
| 112 | |
| 113 | |