meta-bletchley: motor-ctrl: enable motor by pwm and gpio driver

1.enable bmc pwm output by sysfs
2.control motor vref by libgpiod-tools
3.Correct typo and formating

Change-Id: Ie0a57a12e328913f007cb2014d4205ded99c91b3
Signed-off-by: Allen.Wang <Allen_Wang@quantatw.com>
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
index 3f8f2ca..9607453 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
@@ -20,27 +20,6 @@
     return 0
 }
 
-#Get i2c bus number for sledN
-function get_bus_num() {
-    SLED_NUM=$1
-    local bus=0
-
-    if [[ "$SLED_NUM" = [0-5] ]]; then
-        bus="$SLED_NUM"
-    fi
-    echo "$bus"
-}
-
-#Enable sledN Motor VRef
-function open_vref() {
-    i2cset -f -y "${1}" 0x67 0x06 0x95
-}
-
-#Disable sledN Motor VRef
-function close_vref() {
-    i2cset -f -y "${1}" 0x67 0x06 0x55
-}
-
 #######################################
 # Setting step motor control pins to start/stop motor
 # Arguments:
@@ -48,14 +27,17 @@
 #   2. Value of STBY RESET PIN
 #   3. Value of ENABLE PIN
 #   4. Value of DIRECTION PIN
+#   5. Value of Motor Driver VREF PIN
 #######################################
 function set_motor() {
     STBY_PIN="SLED${1}_MD_STBY_RESET"
     EN_PIN="SLED${1}_MD_IOEXP_EN_FAULT"
     DIR_PIN="SLED${1}_MD_DIR"
+    VREF_PIN="SLED${1}_MD_REF_PWM"
     set_gpio "$STBY_PIN" "$2"
     set_gpio "$EN_PIN"   "$3"
     set_gpio "$DIR_PIN"  "$4"
+    set_gpio "$VREF_PIN" "$5"
 }
 
 function show_usage(){
@@ -73,7 +55,6 @@
 if [[ "$1" =~ ^(slot[0-5]{1})$ ]] || [[ "$1" =~ ^(sled[0-5]{1})$ ]]; then
     SLED=$1
     SLED_NUM=${SLED:4}
-    I2C_NUM=$(get_bus_num "$SLED_NUM")
     ACTION=$2
 else
      echo "invalid sled name: $1"
@@ -82,16 +63,13 @@
 
 if [[ "$ACTION" == "s" ]]; then
     echo "stop motor"
-    set_motor "$SLED_NUM" 1 0 0
-    close_vref "$I2C_NUM"
+    set_motor "$SLED_NUM" 1 0 0 1
 elif [[ "$ACTION" == "f" ]];then
     echo "start motor, direction:forward"
-    set_motor  "$SLED_NUM" 1 1 0
-    open_vref "$I2C_NUM"
+    set_motor "$SLED_NUM" 1 1 0 0
 elif [[ "$ACTION" == "r" ]];then
     echo "start motor, direction:reverse"
-    set_motor "$SLED_NUM" 1 1 1
-    open_vref "$I2C_NUM"
+    set_motor "$SLED_NUM" 1 1 1 0
 else
     echo "Error: Unknown action!"
     exit 1
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
index cecb996..60de76b 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
@@ -7,79 +7,42 @@
 
 export PATH=$PATH:/usr/libexec
 
-PWM_BASE_ADDR="0x1e61"
-DEV_FILE="/dev/mem"
+PWM_CLASS_PATH="/sys/class/pwm/pwmchip0"
+#Sleld 0~5 using bmc pwm8~13 as motor driver stick
+PWM_NUM_OFFSET=8
+PWM_PERIOD=2500000 #400HZ
+PWM_DUTY=250000    #PWM_PERIOD X 10%
 CALIBRATE_TIMEOUT=120
 
-#Get pwm register address for sledN
-function get_pwm_offset() {
-  local offset=0
-  case $1 in
-    0)
-      offset="0080"
-    ;;
-    1)
-      offset="0090"
-    ;;
-    2)
-      offset="00a0"
-    ;;
-    3)
-      offset="00b0"
-    ;;
-    4)
-      offset="00c0"
-    ;;
-    5)
-      offset="00d0"
-    ;;
-  esac
-
-  echo ${PWM_BASE_ADDR}${offset}
-}
-
-#Get pwm duty register address for sledN
-function get_duty_offset() {
-  local offset=0
-  case $1 in
-    0)
-      offset="0084"
-    ;;
-    1)
-      offset="0094"
-    ;;
-    2)
-      offset="00a4"
-    ;;
-    3)
-      offset="00b4"
-    ;;
-    4)
-      offset="00c4"
-    ;;
-    5)
-      offset="00d4"
-    ;;
-  esac
-
-  echo ${PWM_BASE_ADDR}${offset}
-}
-
 #Enable pwm for sledN
 function open_pwm() {
     local SLED_NUM="$1"
     echo "Open pwm of sled$SLED_NUM"
-    #enable BMC PWM
-    if [ ! -c "$DEV_FILE" ]; then
-       mknod /dev/mem c 1 1
+    PWM_NUM=$(( SLED_NUM + PWM_NUM_OFFSET ))
+    PWM_PATH="${PWM_CLASS_PATH}/pwm${PWM_NUM}"
+    if [ ! -d "$PWM_PATH" ];then
+        echo "$PWM_NUM" > "${PWM_CLASS_PATH}/export"
     fi
+    if [ -d "$PWM_PATH" ];then
+        echo "set pwm period to $PWM_PERIOD ns"
+        if ! echo "$PWM_PERIOD" > "${PWM_PATH}/period"; then
+            echo "Error: set pwm period fail"
+            return 1
+        fi
 
-    PWM_OFFSET=$(get_pwm_offset "$SLED_NUM")
-    DUTY_OFFSET=$(get_duty_offset "$SLED_NUM")
-    echo "setting ${PWM_OFFSET} to 0x000113F3"
-    echo "setting ${DUTY_OFFSET} to 0xFF006400"
-    devmem "$PWM_OFFSET"  32 0x000113F3
-    devmem "$DUTY_OFFSET" 32 0xFF006400
+        if ! echo 1 > "${PWM_PATH}/enable"; then
+            echo "Error: set pwm enable fail"
+            return 1
+        fi
+
+        if ! echo "$PWM_DUTY" > "${PWM_PATH}/duty_cycle"; then
+            echo "Error: set pwm duty_cycle fail"
+            return 1
+        fi
+    else
+        echo "Error: ${PWM_PATH} not exist, export pwm${PWM_NUM} fail"
+        return 1
+    fi
 }
 
 function set_gpio()
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
index 111ce8b..eebe50c 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
@@ -28,7 +28,7 @@
 
 function trigger_power_button() {
     local sled_num=$1
-    local  delay_time=$2
+    local delay_time=$2
 
     #SLED{N}_MS_DETECT1  (initial position)
     GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1"
@@ -45,7 +45,7 @@
         echo "Press power key for Sled${1} ${delay_time} seconds..."
         sleep "$delay_time"
     else
-        echo "Power key switch not trigger, back motor to initail position"
+        echo "Power key switch not trigger, back motor to initial position"
     fi
 
     motor-ctrl "sled${sled_num}" r >/dev/null
@@ -54,7 +54,7 @@
     if [ "$(get_gpio  "$GPIO_DETECT_PIN1")" -eq 0 ];then
         echo "Motor reverse to initial position successful"
     else
-        echo "Initail position switch not trigger, force stop motor"
+        echo "Initial position switch not trigger, force stop motor"
     fi
 }