####################################################################
# Script : /etc/rc.d/rc.platform/aarch64/rk3399
# Purpose: Perform boot time configuration activities for Hardware
#          Models using the rk3399 SoC.  This includes:
#          * RockPro64,
#          * Pinebook Pro
# Caller : This script is executed from /etc/rc.d/rc.platform
#          (which is loaded from /etc/rc.d/rc.M prior to rc.local) 
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 20-Jan-2022
####################################################################
# Note: This configuration file is managed by the Slackware packages
#       and is NOT user serviceable.  Any changes made here will be
#       removed when the 'a/sysvinit-scripts' package is upgraded.
#
#       To adjust the settings used for the actions applied here,
#       edit /etc/rc.d/rc.platform.conf and the settings will become
#       operative at the next boot.
####################################################################

# Pinebook Pro settings:
case "$HWM" in
   "Pine64 Pinebook Pro"*)
       paneldev="/sys/class/backlight/edp-backlight/brightness"
       # Maximum brightness of the LCD panel for the Pinebook Pro:
       # This may have been overridden by /etc/rc.d/rc.platform.conf
       LCD_PANEL_BRIGHTNESS=${LCD_PANEL_BRIGHTNESS:-4000}

       # Enable maximum brightness of the Pinebook Pro's LCD panel.
       # Without this, it's hard to see the on-screen text.
       echo -n "LCD panel: awaiting control interface.."
       cnt=0
       while [ ! -f ${paneldev} -a $cnt -le 5 ] > /dev/null 2>&1; do
             sleep 1 ; echo -n "." ; ((cnt++))
       done
       if [ -f ${paneldev} ]; then
          echo " done"
          # Maximum brightness on the Pinebook Pro's LCD panel:
          echo -n "LCD panel: Setting brightness level.."
          # The minimum this author can realistically see is 1000:
          for (( i=1000 ; i<=${LCD_PANEL_BRIGHTNESS}; i++ )); do
             echo $i > ${paneldev}
          done
          echo " done"
        else
           # we timed out finding the interface, not setting the brightness:
           echo " time out"
       fi
   ;;
esac

# RockPro64 settings:
case "$HWM" in
   "Pine64 RockPro64"*)
       # If you have no fan, uncomment the variable 'CPU_FAN_OFF' in
       # the config file: /etc/rc.d/rc.platform.conf
       [ -z "${CPU_FAN_OFF}" ] && {
          fandev="/sys/devices/platform/pwm-fan/hwmon/hwmon*/pwm1"
          echo -n "CPU fan: awaiting control interface.."
          cnt=0
          while [ ! -f ${fandev} -a $cnt -le 5 ] > /dev/null 2>&1; do
                sleep 1 ; echo -n "." ; ((cnt++))
          done
          if [ -f ${fandev} ]; then
             echo " done"
             # One of my RockPro fans seems to get 'stuck' with just one run
             # where as another needs an initial high RPM spin.
             # We start at max speed and tune down until we reach the lowest speed
             # at which the fan spins:
             echo 255 > ${fandev}
             sleep 2
             echo 255 > ${fandev}
             echo -n "CPU fan: setting speed.."
             for fanspeed in $( seq 255 -10 65 ) ; do
                 sleep 0.20
                 echo $fanspeed > ${fandev}
             done
             # Force manual mode to prevent fan spinning down:
             ( echo disabled > /sys/devices/virtual/thermal/thermal_zone0/mode ) 2>/dev/null
             # If the user specified a speed, we'll now set it:
             [ ! -z "$CPU_FAN_MAX_SPEED" ] && {
                # Sometimes the fan interface doesn't cause the fan speed
                # to change, so let's hammer it home a few times:
                for (( i=0 ; i<=10; i++ )); do
                   echo $CPU_FAN_MAX_SPEED > ${fandev}
                done ;}
             echo " done"
           else
              # we timed out finding the interface, not setting the speed:
              echo " time out"
          fi ;}
    ;;
esac
