#!/bin/bash
### BEGIN INIT INFO
# Provides:          rtc_cal
# Required-Start:
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Set RTC calibration values
# Description:       Set RTC calibration registers based on cal data for Abacrom AB1805 RTC
#                    setting in /etc/default/rtccal.conf (see also rcS(5)).
### END INIT INFO
#
# WARNING:      this script is specific to the Abacrom AB18xx series of RTC chips
#
# rtc_cal script to perform rtc calibration

# Get system wide init options
[ -f /etc/default/rcS ] && . /etc/default/rcS
# and system wide init funcs
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
# Set default values
CALFILE=/etc/abrtc.cal
I2CBUS=0
I2CADDR=0x69
# override with /etc/default values
CONFFILE=/etc/default/rtc_cal.conf
[ -f ${CONFFILE} ] && . ${CONFFILE}

rv=0
osc_status_reg=0x1D
cal_xt_reg=0x14

die()
{
	echo $*
	exit 1
}

compute_from_ppm()
{
        local ppm=${1}
        # adj value is ppm in adjustment units (~2ppm)
        local adj=$((($ppm*1000000)/1907530))
        local abs_adj=$adj
        # Set xtcal value based on magnitude of adj value
        [ 0 -gt $adj ] && abs_adj=$((abs_adj * -1))
        local xtcal=$(((${abs_adj}-1)/64))
        [ ${xtcal} -gt 3 ] && xtcal=3
        # If adj is large then enable 4 ppm cal
        local cmdx=0
        [ -256 -gt $adj ] && cmdx=1
        local offsetx=$((($adj + 192)/2))
        [ 0 == $cmdx ] && offsetx=$(($adj + ($xtcal * 64)))
        x_temp=$(printf "0x%02X" $offsetx)
        XTCAL=$xtcal
        CMDX=$cmdx
        OFFSETX=$(($x_temp & 0x7F))

}

if [ $# -gt 1 ]
then
CALFILE=$2
fi

[ -x /usr/sbin/i2cget ] || die i2cget not installed
[ -x /usr/sbin/i2cset ] || die i2cset not installed

case "$1" in
	start)
		if [ -e ${CALFILE} ]
		then
		  echo "loading RTC calibration values from ${CALFILE} ..."
		  XTCAL=$(awk -F = '/^XTCAL/ {print $2}' ${CALFILE})
		  CMDX=$(awk -F = '/^CMDX/ {print $2}' ${CALFILE})
		  OFFSETX=$(awk -F = '/^OFFSETX/ {print $2}' ${CALFILE})
		  PPM=$(awk -F = '/^PPM/ {print $2}' ${CALFILE})
		else
		  exit -1
		fi

		if [ -n "$PPM" ]
		then
			compute_from_ppm ${PPM}
		fi
		[ -n "$XTCAL" ] || die XTCAL not set
		[ -n "$CMDX" ] || die CMDX not set
		[ -n "$OFFSETX" ] || die OFFSETX not set
		# Cal_XT Register = COOOOOOO
		# C = CMDX
		# O = OFFSETX
		cal_xt=$((($CMDX << 7) + $OFFSETX))
		/usr/sbin/i2cset -f -y ${I2CBUS} ${I2CADDR} ${cal_xt_reg} ${cal_xt} 
		osc_stat=$(/usr/sbin/i2cget -f -y ${I2CBUS} ${I2CADDR} ${osc_status_reg})
		osc_stat=$((($osc_stat & 0x3f) | ($XTCAL << 6)))
		/usr/sbin/i2cset -f -y ${I2CBUS} ${I2CADDR} ${osc_status_reg} ${osc_stat} 
	  	rv=0
	;;

	stop|restart|reload|force-reload)
	;;

	show)
		[ -f ${CALFILE} ] && cat ${CALFILE} || echo No calibration file \"${CALFILE}\"
		echo -n "CAL_XT REG   = " ; /usr/sbin/i2cget -f -y ${I2CBUS} ${I2CADDR} ${cal_xt_reg}
		echo -n "OSC STAT REG = " ; /usr/sbin/i2cget -f -y ${I2CBUS} ${I2CADDR} ${osc_status_reg}
	;;
	*)
		die 1 Usage $0 start\|show
	;;
esac

exit $rv
