Import upstream u-boot 1.1.4
[u-boot.git] / board / gen860t / beeper.c
1 /*
2  * (C) Copyright 2002
3  * Keith Outwater, keith_outwater@mvis.com
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <mpc8xx.h>
26 #include <asm/8xx_immap.h>
27 #include <linux/ctype.h>
28
29 /*
30  * Basic beeper support for the GEN860T board.  The GEN860T includes
31  * an audio sounder driven by a Phillips TDA8551 amplifier.  The
32  * TDA8551 features a digital volume control which uses a "trinary"
33  * input (high/high-Z/low) to set volume.  The 860's SPKROUT pin
34  * drives the amplifier input.
35  */
36
37
38 /*
39  * Initialize beeper-related hardware. Initialize timer 1 for use with
40  * the beeper. Use 66 Mhz internal clock with prescale of 33 to get
41  * 1 uS period per count.
42  * FIXME: we should really compute the prescale based on the reported
43  * core clock frequency.
44  */
45 void
46 init_beeper(void)
47 {
48     volatile immap_t *immap  = (immap_t *)CFG_IMMR;
49
50         immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_RST1 | TGCR_STP1;
51         immap->im_cpmtimer.cpmt_tmr1 = ((33 << TMR_PS_SHIFT) & TMR_PS_MSK)
52                                                                  | TMR_OM | TMR_FRR | TMR_ICLK_IN_GEN;
53         immap->im_cpmtimer.cpmt_tcn1 = 0;
54         immap->im_cpmtimer.cpmt_ter1 = 0xffff;
55         immap->im_cpmtimer.cpmt_tgcr |= TGCR_RST1;
56 }
57
58
59 /*
60  * Set beeper frequency.  Max allowed frequency is 2.5 KHz.  This limit
61  * is mostly arbitrary, but the beeper isn't really much good beyond this
62  * frequency.
63  */
64 void
65 set_beeper_frequency(uint frequency)
66 {
67 #define FREQ_LIMIT      2500
68
69     volatile immap_t *immap  = (immap_t *)CFG_IMMR;
70
71         /*
72          * Compute timer ticks given desired frequency.  The timer is set up
73          * to count 0.5 uS per tick and it takes two ticks per cycle (Hz).
74          */
75         if (frequency > FREQ_LIMIT) frequency = FREQ_LIMIT;
76         frequency = 1000000/frequency;
77         immap->im_cpmtimer.cpmt_trr1 = (ushort)frequency;
78 }
79
80
81 /*
82  * Turn the beeper on
83  */
84 void
85 beeper_on(void)
86 {
87     volatile immap_t *immap  = (immap_t *)CFG_IMMR;
88
89         immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_STP1;
90 }
91
92
93 /*
94  * Turn the beeper off
95  */
96 void
97 beeper_off(void)
98 {
99     volatile immap_t *immap  = (immap_t *)CFG_IMMR;
100
101         immap->im_cpmtimer.cpmt_tgcr |= TGCR_STP1;
102 }
103
104
105 /*
106  * Increase or decrease the beeper volume.  Volume can be set
107  * from off to full in 64 steps.  To increase volume, the output
108  * pin is actively driven high, then returned to tristate.
109  * To decrease volume, output a low on the port pin (no need to
110  * change pin mode to tristate) then output a high to go back to
111  * tristate.
112  */
113 void
114 set_beeper_volume(int steps)
115 {
116     volatile immap_t *immap  = (immap_t *)CFG_IMMR;
117         int i;
118
119         if (steps >= 0) {
120                 for (i = 0; i < (steps >= 64 ? 64 : steps); i++) {
121                         immap->im_cpm.cp_pbodr &= ~(0x80000000 >> 19);
122                         udelay(1);
123                         immap->im_cpm.cp_pbodr |= (0x80000000 >> 19);
124                         udelay(1);
125                 }
126         }
127         else {
128                 for (i = 0; i > (steps <= -64 ? -64 : steps); i--) {
129                         immap->im_cpm.cp_pbdat &= ~(0x80000000 >> 19);
130                         udelay(1);
131                         immap->im_cpm.cp_pbdat |= (0x80000000 >> 19);
132                         udelay(1);
133                 }
134         }
135 }
136
137
138 /*
139  * Check the environment to see if the beeper needs beeping.
140  * Controlled by a sequence of the form:
141  * freq/delta volume/on time/off time;... where:
142  * freq                 = frequency in Hz (0 - 2500)
143  * delta volume = volume steps up or down (-64 <= vol <= 64)
144  * on time              = time in mS
145  * off time             = time in mS
146  *
147  * Return 1 on success, 0 on failure
148  */
149 int
150 do_beeper(char *sequence)
151 {
152 #define DELIMITER       ';'
153
154 int args[4];
155 int i;
156 int val;
157 char *p = sequence;
158 char *tp;
159
160         /*
161          * Parse the control sequence.  This is a really simple parser
162          * without any real error checking.  You can probably blow it
163          * up really easily.
164          */
165         if (*p == '\0' || !isdigit(*p)) {
166                 printf("%s:%d: null or invalid string (%s)\n",
167                                 __FILE__, __LINE__, p);
168                 return 0;
169         }
170
171         i = 0;
172         while (*p != '\0') {
173                 while (*p != DELIMITER) {
174                         if (i > 3) i = 0;
175                         val = (int) simple_strtol(p, &tp, 0);
176                         if (tp == p) {
177                                 printf("%s:%d: no digits or bad format\n",
178                                                                 __FILE__,__LINE__);
179                                 return 0;
180                         }
181                         else {
182                                 args[i] = val;
183                         }
184
185                         i++;
186                         if (*tp == DELIMITER)
187                                 p = tp;
188                         else
189                                 p = ++tp;
190                 }
191                 p++;
192
193                 /*
194                  * Well, we got something that has a chance of being correct
195                  */
196 #if 0
197                 for (i = 0; i < 4; i++) {
198                         printf("%s:%d:arg %d = %d\n", __FILE__, __LINE__, i, args[i]);
199                 }
200                 printf("\n");
201 #endif
202
203                 set_beeper_frequency(args[0]);
204                 set_beeper_volume(args[1]);
205                 beeper_on();
206                 udelay(1000 * args[2]);
207                 beeper_off();
208                 udelay(1000 * args[3]);
209         }
210         return 1;
211 }
212
213 /* vim: set ts=4 sw=4 tw=78: */