#!/bin/sh

# EC reg. values
FAN_CTL_VAL=0x81        # Pol. active high, 48MHz base clock, 0% min. duty, PWM1 enabled
FAN_CTL_MAIN_VAL=0x77   # FAN tacho enabled, automatic mode independent, SmartGuardian mode
FAN_CTL_PWM1_VAL=0x81   # Automatic, TMPIN2
FAN_CTL_PWM2_VAL=0x80   # Automatic, TMPIN1
FAN_CTL_PWM3_VAL=0x82   # Automatic, TMPIN3
FAN_CTL1_OFF_LIMIT=28   # FAN off limit
FAN_CTL2_OFF_LIMIT=28
FAN_CTL3_OFF_LIMIT=28
FAN_CTL1_START_LIMIT=33 # FAN start limit
FAN_CTL2_START_LIMIT=33
FAN_CTL3_START_LIMIT=33
FAN_CTL1_FULL_LIMIT=40  # FAN full limit
FAN_CTL2_FULL_LIMIT=40
FAN_CTL3_FULL_LIMIT=40
FAN_CTL1_PWM_START=0x46 # Start PWM value
FAN_CTL2_PWM_START=0x46
FAN_CTL3_PWM_START=0x46
FAN_CTL1_SG_CTRL=0x9d   # Temperature smoothing, 1s fan spin-up time, 16 PWM values/degree
FAN_CTL2_SG_CTRL=0x1d   # 1s fan spin-up time, 16 PWM values/degree
FAN_CTL3_SG_CTRL=0xa5   # Temperature smoothing, spin-up feedback, 16 PWM values/degree

# PnP config
ADDR=0x2e
DATA=0x2f
# EC reg. addresses
FAN_CTL_MAIN=0x13
FAN_CTL=0x14
FAN_CTL_PWM1=0x15
FAN_CTL_PWM2=0x16
FAN_CTL_PWM3=0x17
FAN_CTL1_OFF=0x60
FAN_CTL2_OFF=0x68
FAN_CTL3_OFF=0x70
FAN_CTL1_START=0x61
FAN_CTL2_START=0x69
FAN_CTL3_START=0x71
FAN_CTL1_FULL=0x62
FAN_CTL2_FULL=0x6A
FAN_CTL3_FULL=0x72
FAN_CTL1_PWM=0x63
FAN_CTL2_PWM=0x6B
FAN_CTL3_PWM=0x73
FAN_CTL1_SG=0x64
FAN_CTL2_SG=0x6C
FAN_CTL3_SG=0x74



# Get chip ID and EC base address
ec_init()
{
  # Enter PnP config mode
  outb $ADDR 0x87
  outb $ADDR 0x01
  outb $ADDR 0x55
  outb $ADDR 0x55

  # Get chip ID
  outb $ADDR 0x20
  CHIPID=$(($(inb $DATA) * 0x100))
  outb $ADDR 0x21
  CHIPID=$(($(inb $DATA) + $CHIPID))
  printf "Chip ID: %02x\n" $CHIPID

  # Check chip ID
  if [ $CHIPID -ne 34565 ]
  then
    echo "No 8705 found..."
    exit 1
  fi

  # EC base address
  outb $ADDR 0x07
  outb $DATA 0x04
  outb $ADDR 0x60
  ECBASE=$(($(inb $DATA) * 0x100))
  outb $ADDR 0x61
  ECBASE=$(($(inb $DATA) + $ECBASE))
  printf "EC base address: 0x%04x\n" $ECBASE
  ECADDR=$(($ECBASE + 5))
  ECDATA=$(($ECBASE + 6))
  
  # Exit configuration mode
  outb $ADDR 0x02
  outb $DATA 0x02
  #inb $DATA
}

# EC write
ec_write ()
{
  outb $ECADDR $1
  outb $ECDATA $2
}

# Dump regs
ec_dump ()
{
  for i in $(seq 0x13 0x74)
  do
    outb $ECADDR $i
    printf "0x%02x: 0x%02x\n" $i $(inb $ECDATA)
  done
}

#########################
# Main
#########################
ec_init

#ec_dump
#exit 0

# FAN_CTL_MAIN
printf "Setting FAN_CTL_MAIN to 0x%02x\n" $FAN_CTL_MAIN_VAL
ec_write $FAN_CTL_MAIN $FAN_CTL_MAIN_VAL
# FAN_CTL
printf "Setting FAN_CTL to 0x%02x\n" $FAN_CTL_VAL
ec_write $FAN_CTL $FAN_CTL_VAL

# FAN_CTL_PWM1-3
printf "Setting FAN_CTL_PWM1 to 0x%02x\n" $FAN_CTL_PWM1_VAL
ec_write $FAN_CTL_PWM1 $FAN_CTL_PWM1_VAL
printf "Setting FAN_CTL_PWM2 to 0x%02x\n" $FAN_CTL_PWM2_VAL
ec_write $FAN_CTL_PWM2 $FAN_CTL_PWM2_VAL
printf "Setting FAN_CTL_PWM3 to 0x%02x\n" $FAN_CTL_PWM3_VAL
ec_write $FAN_CTL_PWM3 $FAN_CTL_PWM3_VAL

# FAN off limits
printf "Setting FAN_CTL1_OFF to %d\n" $FAN_CTL1_OFF_LIMIT
ec_write $FAN_CTL1_OFF $FAN_CTL1_OFF_LIMIT
printf "Setting FAN_CTL2_OFF to %d\n" $FAN_CTL2_OFF_LIMIT
ec_write $FAN_CTL2_OFF $FAN_CTL2_OFF_LIMIT
printf "Setting FAN_CTL3_OFF to %d\n" $FAN_CTL3_OFF_LIMIT
ec_write $FAN_CTL3_OFF $FAN_CTL3_OFF_LIMIT
# FAN full limits
printf "Setting FAN_CTL1_FULL to %d\n" $FAN_CTL1_FULL_LIMIT
ec_write $FAN_CTL1_FULL $FAN_CTL1_FULL_LIMIT
printf "Setting FAN_CTL2_FULL to %d\n" $FAN_CTL2_FULL_LIMIT
ec_write $FAN_CTL2_FULL $FAN_CTL2_FULL_LIMIT
printf "Setting FAN_CTL3_FULL to %d\n" $FAN_CTL3_FULL_LIMIT
ec_write $FAN_CTL3_FULL $FAN_CTL3_FULL_LIMIT
# FAN start limits
printf "Setting FAN_CTL1_START to %d\n" $FAN_CTL1_START_LIMIT
ec_write $FAN_CTL1_START $FAN_CTL1_START_LIMIT
printf "Setting FAN_CTL2_START to %d\n" $FAN_CTL2_START_LIMIT
ec_write $FAN_CTL2_START $FAN_CTL2_START_LIMIT
printf "Setting FAN_CTL3_START to %d\n" $FAN_CTL3_START_LIMIT
ec_write $FAN_CTL3_START $FAN_CTL3_START_LIMIT
# PWM start values
printf "Setting FAN_CTL1_PWM to %d\n" $FAN_CTL1_PWM_START
ec_write $FAN_CTL1_PWM $FAN_CTL1_PWM_START
printf "Setting FAN_CTL2_PWM to %d\n" $FAN_CTL2_PWM_START
ec_write $FAN_CTL2_PWM $FAN_CTL2_PWM_START
printf "Setting FAN_CTL3_PWM to %d\n" $FAN_CTL3_PWM_START
ec_write $FAN_CTL3_PWM $FAN_CTL3_PWM_START

# Smartguardian control
printf "Setting FAN_CTL1_SG to 0x%02x\n" $FAN_CTL1_SG_CTRL
ec_write $FAN_CTL1_SG $FAN_CTL1_SG_CTRL
printf "Setting FAN_CTL2_SG to 0x%02x\n" $FAN_CTL2_SG_CTRL
ec_write $FAN_CTL2_SG $FAN_CTL2_SG_CTRL
printf "Setting FAN_CTL3_SG to 0x%02x\n" $FAN_CTL3_SG_CTRL
ec_write $FAN_CTL3_SG $FAN_CTL3_SG_CTRL

#ec_dump
