#!/bin/sh
#
# Copyright 2014-present Facebook. All Rights Reserved.
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

### BEGIN INIT INFO
# Provides:          sensor-setup
# Required-Start:    power-on
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Power on micro-server
### END INIT INFO

# Eventually, this will be used to configure the various (mostly
# i2c-based) sensors, once we have a kernel version that supports
# doing this more dynamically.
#
# For now, we're using it to install the lm75 and pmbus module so that it
# can detect the fourth temperature sensor, which is located
# on the uServer, which doesn't get power until power-on executes.
#
# Similarly, the pmbus sensor seems to have an easier time of
# detecting the NCP4200 buck converters after poweron.  This has not
# been carefully explored.

modprobe lm75
modprobe pmbus

# Enable the ADC inputs;  adc0 - adc15 are connected to various voltage sensors
# adc12 - adc15 are for SLOT_TYPE

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# setup ADC channels

ADC_PATH="/sys/devices/platform/ast_adc.0"
# For MINILAKETB tyep V:
# channel 0:  r1:  5.36K; r2:  2.0K; v2: 0mv
# channel 1:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 2:  r1:  2.87K; r2:  2.0K; v2: 0mv
# channel 3:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 4:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 5:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 6:  r1:  15.8K; r2:  2.0K; v2: 0mv
# channel 7:  r1:  2.87K; r2:  2.0K; v2: 0mv
# channel 8:  r1:     1K; r2:  0.0K; v2: 0mv
# channel 9:  r1:     1K; r2:  0.0K; v2: 0mv
# channel 10: r1:  1.69K; r2:  2.0K; v2: 0mv
# channel 11: r1:    665; r2:  2.0K; v2: 0mv
# channel 12: r1:     1K; r2:    0K; v2: 0mv
# channel 13: r1:     1K; r2:    0K; v2: 0mv
# channel 14: r1:     1K; r2:    0K; v2: 0mv
# channel 15: r1:     1K; r2:    0K; v2: 0mv

config_adc() {
    channel=$1
    r1=$2
    r2=$3
    v2=$4
    echo $r1 > ${ADC_PATH}/adc${channel}_r1
    echo $r2 > ${ADC_PATH}/adc${channel}_r2
    echo $v2 > ${ADC_PATH}/adc${channel}_v2
    echo 1 > ${ADC_PATH}/adc${channel}_en
}

config_adc 0  5360  2000 0
config_adc 1 15800  2000 0
config_adc 2  2870  2000 0
config_adc 3 15800  2000 0
config_adc 4 15800  2000 0
config_adc 5 15800  2000 0
config_adc 6 15800  2000 0
config_adc 7  2870  2000 0
config_adc 8  	 0  1000 0
config_adc 9  	 0  1000 0
config_adc 10 1690  2000 0
config_adc 11  665  2000 0
config_adc 12    0  1000 0
config_adc 13    0  1000 0
config_adc 14    0  1000 0
config_adc 15    0  1000 0
