#!/bin/sh
#
#    kms_powersave: this script switchs between GPU power mode
#
#    Copyright (C) 2011 Mathieu Bérard 
#
#    Authors: Mathieu Bérard  <mathieu@mberard.eu>
#
#    This program 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 3 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.  If not, see <http://www.gnu.org/licenses/>.

# Only support radeon kms driver pm modes for now.
# The sysfs interface is explained here, I highly recommend reading it
# before tweaking this script: 
# http://lists.freedesktop.org/archives/dri-devel/2010-May/000492.html

#Full power, default profile mode
#Laptops may benefit from using FULL_PROFILE="auto"
FULL_METHOD="profile"
FULL_PROFILE="default"

#Alternativly dynpm mode
#FULL_METHOD="dynpm"
#FULL_PROFILE="default"

#Powersave mode
LOW_METHOD="profile"
LOW_PROFILE="low"

DRM_PATH="/sys/class/drm/card0/device"
DRM_DRIVER="${DRM_PATH}/driver"
METHOD_SYSFILE="${DRM_PATH}/power_method"
PROFILE_SYSFILE="${DRM_PATH}/power_profile"

[ -e $DRM_DRIVER ] && [ -w $METHOD_SYSFILE ] && [ -w $METHOD_SYSFILE ] || exit 0

#At this point we should be using the radeon driver, but let's be extra carefull.
DRM_DRIVER=`readlink -f ${DRM_DRIVER}`
DRM_DRV_NAME=`basename ${DRM_DRIVER}`
[ "$DRM_DRV_NAME" = "radeon" ] || exit 0

set_kms_power() {
	echo $1 > $PROFILE_SYSFILE
	echo $2 > $METHOD_SYSFILE
}

help() {
	echo "Radeon KMS power mode to save power."
}

case $1 in
	true)
		set_kms_power $LOW_PROFILE $LOW_METHOD ;;
	false)
		set_kms_power $FULL_PROFILE $FULL_METHOD ;;
	help)
		help;;
	*)
		exit $NA ;;
esac
