####################################################################
# 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.
####################################################################

# Note: both of these may cause a system hang if the interface
# doesn't appear. However, I don't think this will happen.
# If this becomes a problem I'll wrap it in a retry loop.

# Pinebook Pro settings:
case "$HWM" in
   "Pine64 Pinebook Pro"*)

       # 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.."
       ( while [ ! -f /sys/class/backlight/edp-backlight/brightness ]; do
               sleep 1
               echo -n "."
         done ) > /dev/null 2>&1
       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 > /sys/class/backlight/edp-backlight/brightness
       done
       echo " done"
   ;;
esac

# RockPro64 settings:
case "$HWM" in
   "Pine64 RockPro64"*)
       echo -n "CPU fan: awaiting control interface.."
       ( while [ ! -f /sys/devices/platform/pwm-fan/hwmon/hwmon*/pwm1 ]; do
               sleep 1
               echo -n "."
         done ) > /dev/null 2>&1
       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 > /sys/devices/platform/pwm-fan/hwmon/hwmon*/pwm1
       sleep 2
       echo 255 > /sys/devices/platform/pwm-fan/hwmon/hwmon*/pwm1
       echo -n "CPU fan: setting speed.."
       for fanspeed in $( seq 255 -10 65 ) ; do
           sleep 0.20
           echo $fanspeed > /sys/devices/platform/pwm-fan/hwmon/hwmon*/pwm1
       done
       # 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 > /sys/devices/platform/pwm-fan/hwmon/hwmon*/pwm1
          done ;}
       echo " done"
    ;;
esac
