#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2014 Alex Deryskyba (alex@codesnake.com)
#
#  OpenELEC 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, either version 2 of the License, or
#  (at your option) any later version.
#
#  OpenELEC 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 OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

hdmimode=720p

# Parse command line arguments
for arg in $(cat /proc/cmdline); do
  case $arg in
    hdmimode=*)
      hdmimode="${arg#*=}"
      ;;
    scaling_governor=*)
      scaling_governor="${arg#*=}"
      ;;
    scaling_min_freq=*)
      scaling_min_freq="${arg#*=}"
      ;;
    scaling_max_freq=*)
      scaling_max_freq="${arg#*=}"
      ;;
  esac
done

echo "$hdmimode" > /sys/class/display/mode

# Enable framebuffer device
echo 0 > /sys/class/graphics/fb0/blank

# Disable framebuffer scaling
echo 0 > /sys/class/graphics/fb0/free_scale

# set initial video state
echo 1 > /sys/class/video/disable_video

# Set framebuffer geometry to match the resolution
case "$hdmimode" in
  720*)
    fbset -fb /dev/fb0 -g 1280 720 1920 2160 32
    ;;
  1080*)
    fbset -fb /dev/fb0 -g 1920 1080 1920 2160 32
    ;;
esac

# Include deinterlacer into default VFM map
echo rm default > /sys/class/vfm/map
echo add default decoder ppmgr deinterlace amvideo > /sys/class/vfm/map

# Boot with performance governor, then switch to the governor specified in the kernel command line
# Assume SMP uses shared cpufreq policy for all CPUs
cpufreq="/sys/devices/system/cpu/cpu0/cpufreq"
if [ -n "$scaling_governor" ]; then
  echo "$scaling_governor" > "$cpufreq/scaling_governor"
fi
if [ -n "$scaling_min_freq" ]; then
  echo "$scaling_min_freq" > "$cpufreq/scaling_min_freq"
fi
if [ -n "$scaling_max_freq" ]; then
  echo "$scaling_max_freq" > "$cpufreq/scaling_max_freq"
  echo "$scaling_max_freq" > "$cpufreq/scaling_dflt_freq"
fi
