~ubuntu-branches/ubuntu/saucy/lm-sensors-3/saucy

« back to all changes in this revision

Viewing changes to prog/pwm/vt1211_pwm

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno
  • Date: 2009-10-14 00:20:17 UTC
  • mfrom: (1.1.2 upstream) (0.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091014002017-36cfephnx9saq42i
Tags: 1:3.1.1-4
* Bumped Standards-version to 3.8.3 (no changes).
* Drop libsensors4 postinst script and the dependency on makedev | 
  udev. This part has been moved to i2c-tools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# vt1211_pwm - Manually control the PWM outputs of the VIA vt1211 Super-I/O
 
4
#
 
5
# Copyright (C) 2008 Juerg Haefliger (juergh at gmail.com>
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
#
 
21
 
 
22
function usage
 
23
{
 
24
    echo "Usage: vt1211_pwm -i <pwm> | -r <pwm> | -s <pwm>,<val>"
 
25
    echo
 
26
    echo "DESCRIPTION"
 
27
    echo "   This tool provides manual control of the PWM outputs of the VIA "
 
28
    echo "   vt1211 Super-I/O."
 
29
    echo
 
30
    echo "OPTIONS"
 
31
    echo "   -d               Display the current PWM settings of the vt1211."
 
32
    echo "   -i <pwm>         Initialize the PWM output for manual control."
 
33
    echo "   -r <pwm>         Restore the PWM output to the original settings."
 
34
    echo "   -s <pwm>,<val>   Set the PWM output duty-cycle. "
 
35
    echo
 
36
    echo "WITH"
 
37
    echo "   <pwm>   PWM output (1-2)."
 
38
    echo "   <val>   PWM duty-cycle (0-255)."
 
39
    echo
 
40
    exit 2
 
41
}
 
42
 
 
43
function set_attr
 
44
{
 
45
    local tmp
 
46
    read tmp < $1
 
47
    echo "echo $tmp > $1"
 
48
    echo $2 > $1
 
49
}
 
50
 
 
51
# check if we have the right permissions to run
 
52
if [ `whoami` != "root" ] ; then
 
53
    echo "You must be root to run this script."
 
54
    exit 1
 
55
fi
 
56
 
 
57
# parse the command line options
 
58
action=
 
59
nr=1
 
60
val=0
 
61
while getopts di:r:s: opt ; do
 
62
    [ -z "$action" ] || usage
 
63
    case $opt in
 
64
        d)  action="display"
 
65
            ;;
 
66
        i)  action="init"
 
67
            nr=$OPTARG
 
68
            ;;
 
69
        r)  action="restore"
 
70
            nr=$OPTARG
 
71
            ;;
 
72
        s)  action="set"
 
73
            nr=${OPTARG%,*}
 
74
            val=${OPTARG#*,}
 
75
            ;;
 
76
        ?)  usage
 
77
            ;;
 
78
    esac
 
79
done
 
80
 
 
81
[ -z "$action" ] && usage
 
82
[ $nr -lt 1 -o $nr -gt 2 ] && usage
 
83
[ $val -lt 0 -o $val -gt 255 ] && usage
 
84
 
 
85
# check hwmon device
 
86
dev=`ls -d /sys/devices/platform/vt1211.*`
 
87
if [ -z "$dev" ] ; then
 
88
    echo "Platform device vt1211 not found. Did you load the vt1211 module?"
 
89
    exit 1
 
90
fi
 
91
 
 
92
# init file
 
93
initfile="/root/.vt1211_pwm${nr}"
 
94
 
 
95
# ACTION: display
 
96
if [ $action = "display" ] ; then
 
97
    for nr in 1 2 ; do
 
98
        read mode < ${dev}/pwm${nr}_enable
 
99
        if [ $mode -eq 0 ] ; then
 
100
            mode_txt="(disabled)"
 
101
        elif [ $mode -eq 2 ] ; then
 
102
            mode_txt="(auto)    "
 
103
        else
 
104
            mode_txt="(unknown) "
 
105
        fi
 
106
        read val < ${dev}/pwm${nr}
 
107
        dc=$((val * 100 / 255))
 
108
        echo "pwm${nr}: mode = ${mode} ${mode_txt}, val = ${val} (${dc}%)"
 
109
    done
 
110
fi
 
111
 
 
112
# ACTION: init
 
113
if [ $action = "init" ] ; then
 
114
    # check for an init file
 
115
    if [ -e  $initfile ] ; then
 
116
        echo " $initfile exists. Did you already initialize this PWM?"
 
117
        exit 1
 
118
    fi
 
119
 
 
120
    (
 
121
        # set the pwm-to-temp mapping
 
122
        # use the internal temp as a reference
 
123
        set_attr ${dev}/pwm${nr}_auto_channels_temp 2
 
124
 
 
125
        # enable the pwm auto controller
 
126
        set_attr ${dev}/pwm${nr}_enable 2
 
127
 
 
128
        # set the PWM duty-cycle
 
129
        set_attr ${dev}/pwm${nr}_auto_point2_pwm 0
 
130
        set_attr ${dev}/pwm${nr}_auto_point3_pwm 255
 
131
 
 
132
        # set the temp boundaries to extrem values
 
133
        set_attr ${dev}/pwm1_auto_point1_temp 0
 
134
        set_attr ${dev}/pwm1_auto_point2_temp 0
 
135
        set_attr ${dev}/pwm1_auto_point3_temp 1000000
 
136
        set_attr ${dev}/pwm1_auto_point4_temp 1000000
 
137
 
 
138
    ) > $initfile
 
139
 
 
140
    exit 0
 
141
fi
 
142
 
 
143
# ACTION: restore
 
144
if [ $action = "restore" ] ; then
 
145
    # check for an init file
 
146
    if [ ! -e  $initfile ] ; then
 
147
        echo " $initfile not found. Nothing to restore."
 
148
        exit 1
 
149
    fi
 
150
 
 
151
    sh $initfile
 
152
    rm $initfile
 
153
 
 
154
    exit 0
 
155
fi
 
156
 
 
157
# ACTION: set
 
158
if [ $action = "set" ] ; then
 
159
    # check for an init file
 
160
    if [ ! -e $initfile ] ; then
 
161
        echo "$initfile not found. Did you initialize this PWM?"
 
162
        exit 1
 
163
    fi
 
164
 
 
165
    # set the PWM duty-cycle
 
166
    echo $val > ${dev}/pwm${nr}_auto_point3_pwm
 
167
 
 
168
    # fool the vt1211 into believing there is a temp change
 
169
    # otherwise the new PWM duty-cycle doesn't take effect
 
170
    echo 0 > ${dev}/pwm1_auto_point3_temp
 
171
    echo 1000000 > ${dev}/pwm1_auto_point3_temp
 
172
 
 
173
    # read the new PWM duty-cycle back and print it
 
174
    read val < ${dev}/pwm${nr}
 
175
    dc=$((val * 100 / 255))
 
176
    echo "pwm${nr} is now at ${val} (${dc}%)."
 
177
 
 
178
    exit 0
 
179
fi