MINI2440: Auto probe for SDRAM size
[u-boot-openmoko/mini2440.git] / board / gen860t / beeper.c
blob46fe66ba619df08bbe98c05308e410e6c2a27f86
1 /*
2 * (C) Copyright 2002
3 * Keith Outwater, keith_outwater@mvis.com
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
24 #include <common.h>
25 #include <mpc8xx.h>
26 #include <asm/8xx_immap.h>
27 #include <linux/ctype.h>
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.
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.
45 void
46 init_beeper(void)
48 volatile immap_t *immap = (immap_t *)CFG_IMMR;
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;
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.
64 void
65 set_beeper_frequency(uint frequency)
67 #define FREQ_LIMIT 2500
69 volatile immap_t *immap = (immap_t *)CFG_IMMR;
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).
75 if (frequency > FREQ_LIMIT) frequency = FREQ_LIMIT;
76 frequency = 1000000/frequency;
77 immap->im_cpmtimer.cpmt_trr1 = (ushort)frequency;
82 * Turn the beeper on
84 void
85 beeper_on(void)
87 volatile immap_t *immap = (immap_t *)CFG_IMMR;
89 immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_STP1;
94 * Turn the beeper off
96 void
97 beeper_off(void)
99 volatile immap_t *immap = (immap_t *)CFG_IMMR;
101 immap->im_cpmtimer.cpmt_tgcr |= TGCR_STP1;
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.
113 void
114 set_beeper_volume(int steps)
116 volatile immap_t *immap = (immap_t *)CFG_IMMR;
117 int i;
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);
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);
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
147 * Return 1 on success, 0 on failure
150 do_beeper(char *sequence)
152 #define DELIMITER ';'
154 int args[4];
155 int i;
156 int val;
157 char *p = sequence;
158 char *tp;
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.
165 if (*p == '\0' || !isdigit(*p)) {
166 printf("%s:%d: null or invalid string (%s)\n",
167 __FILE__, __LINE__, p);
168 return 0;
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;
181 else {
182 args[i] = val;
185 i++;
186 if (*tp == DELIMITER)
187 p = tp;
188 else
189 p = ++tp;
191 p++;
194 * Well, we got something that has a chance of being correct
196 #if 0
197 for (i = 0; i < 4; i++) {
198 printf("%s:%d:arg %d = %d\n", __FILE__, __LINE__, i, args[i]);
200 printf("\n");
201 #endif
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]);
210 return 1;
213 /* vim: set ts=4 sw=4 tw=78: */