vibrator: Add Diag Utility

Test: Manually
Bug: 140078239
Change-Id: I3bf91d298b493bea97547bd76ed32f8a6a1465cc
Signed-off-by: Harpreet \"Eli\" Sangha <eliptus@google.com>
diff --git a/vibrator/cs40l25/diag/Android.bp b/vibrator/cs40l25/diag/Android.bp
new file mode 100644
index 0000000..acd8632
--- /dev/null
+++ b/vibrator/cs40l25/diag/Android.bp
@@ -0,0 +1,20 @@
+//
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+sh_binary {
+    name: "diag-vibrator",
+    src: "diag-vibrator.sh",
+    vendor: true,
+}
diff --git a/vibrator/cs40l25/diag/diag-vibrator.sh b/vibrator/cs40l25/diag/diag-vibrator.sh
new file mode 100644
index 0000000..30dca9d
--- /dev/null
+++ b/vibrator/cs40l25/diag/diag-vibrator.sh
@@ -0,0 +1,64 @@
+#!/vendor/bin/sh
+
+set -e
+
+do_id()
+{
+    cat /sys/class/leds/vibrator/device/modalias | sed 's/^i2c://g'
+}
+
+do_ping()
+{
+    test "$(do_id)" == "cs40l25a"
+}
+
+do_enable()
+{
+    /system/bin/idlcli vibrator on "${@}"
+}
+
+do_disable()
+{
+    /system/bin/idlcli vibrator off
+}
+
+do_state()
+{
+    local state="$(cat /sys/class/leds/vibrator/device/vibe_state)"
+    if [[ "${state}" == "0" ]]
+    then
+        echo "stopped"
+    else
+        echo "running"
+    fi
+}
+
+do_dump()
+{
+    local loc="$(basename /sys/class/leds/vibrator/device/driver/*-0043)"
+    cat /sys/kernel/debug/regmap/${loc}/registers
+}
+
+do_help()
+{
+    local name="$(basename "${0}")"
+    echo "Usage:"
+    echo "  ${name} id          - Prints controller ID"
+    echo "  ${name} ping        - Verifies probe succedded"
+    echo "  ${name} enable <ms> - Enables vibrator for <ms> milliseconds"
+    echo "  ${name} disable     - Disables vibrator."
+    echo "  ${name} state       - Returns 'stopped' or 'running' state."
+    echo "  ${name} dump        - Dumps memory mapped registers."
+}
+
+if [[ "${#}" -gt "0" ]]
+then
+    cmd="do_${1}" && shift
+fi
+
+if ! typeset -f "${cmd}" >/dev/null
+then
+    cmd="do_help"
+fi
+
+exec "${cmd}" "${@}"