]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/gen860t/beeper.c
mx6sxsabresd: Add Ethernet support
[karo-tx-uboot.git] / board / gen860t / beeper.c
1 /*
2  * (C) Copyright 2002
3  * Keith Outwater, keith_outwater@mvis.com
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <mpc8xx.h>
10 #include <asm/8xx_immap.h>
11 #include <linux/ctype.h>
12
13 /*
14  * Basic beeper support for the GEN860T board.  The GEN860T includes
15  * an audio sounder driven by a Phillips TDA8551 amplifier.  The
16  * TDA8551 features a digital volume control which uses a "trinary"
17  * input (high/high-Z/low) to set volume.  The 860's SPKROUT pin
18  * drives the amplifier input.
19  */
20
21 /*
22  * Initialize beeper-related hardware. Initialize timer 1 for use with
23  * the beeper. Use 66 MHz internal clock with prescale of 33 to get
24  * 1 uS period per count.
25  * FIXME: we should really compute the prescale based on the reported
26  * core clock frequency.
27  */
28 void init_beeper (void)
29 {
30         volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
31
32         immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_RST1 | TGCR_STP1;
33         immap->im_cpmtimer.cpmt_tmr1 = ((33 << TMR_PS_SHIFT) & TMR_PS_MSK)
34                 | TMR_OM | TMR_FRR | TMR_ICLK_IN_GEN;
35         immap->im_cpmtimer.cpmt_tcn1 = 0;
36         immap->im_cpmtimer.cpmt_ter1 = 0xffff;
37         immap->im_cpmtimer.cpmt_tgcr |= TGCR_RST1;
38 }
39
40 /*
41  * Set beeper frequency.  Max allowed frequency is 2.5 KHz.  This limit
42  * is mostly arbitrary, but the beeper isn't really much good beyond this
43  * frequency.
44  */
45 void set_beeper_frequency (uint frequency)
46 {
47 #define FREQ_LIMIT      2500
48
49         volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
50
51         /*
52          * Compute timer ticks given desired frequency.  The timer is set up
53          * to count 0.5 uS per tick and it takes two ticks per cycle (Hz).
54          */
55         if (frequency > FREQ_LIMIT)
56                 frequency = FREQ_LIMIT;
57         frequency = 1000000 / frequency;
58         immap->im_cpmtimer.cpmt_trr1 = (ushort) frequency;
59 }
60
61 /*
62  * Turn the beeper on
63  */
64 void beeper_on (void)
65 {
66         volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
67
68         immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_STP1;
69 }
70
71 /*
72  * Turn the beeper off
73  */
74 void beeper_off (void)
75 {
76         volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
77
78         immap->im_cpmtimer.cpmt_tgcr |= TGCR_STP1;
79 }
80
81 /*
82  * Increase or decrease the beeper volume.  Volume can be set
83  * from off to full in 64 steps.  To increase volume, the output
84  * pin is actively driven high, then returned to tristate.
85  * To decrease volume, output a low on the port pin (no need to
86  * change pin mode to tristate) then output a high to go back to
87  * tristate.
88  */
89 void set_beeper_volume (int steps)
90 {
91         volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
92         int i;
93
94         if (steps >= 0) {
95                 for (i = 0; i < (steps >= 64 ? 64 : steps); i++) {
96                         immap->im_cpm.cp_pbodr &= ~(0x80000000 >> 19);
97                         udelay (1);
98                         immap->im_cpm.cp_pbodr |= (0x80000000 >> 19);
99                         udelay (1);
100                 }
101         } else {
102                 for (i = 0; i > (steps <= -64 ? -64 : steps); i--) {
103                         immap->im_cpm.cp_pbdat &= ~(0x80000000 >> 19);
104                         udelay (1);
105                         immap->im_cpm.cp_pbdat |= (0x80000000 >> 19);
106                         udelay (1);
107                 }
108         }
109 }
110
111 /*
112  * Check the environment to see if the beeper needs beeping.
113  * Controlled by a sequence of the form:
114  * freq/delta volume/on time/off time;... where:
115  * freq                 = frequency in Hz (0 - 2500)
116  * delta volume = volume steps up or down (-64 <= vol <= 64)
117  * on time              = time in mS
118  * off time             = time in mS
119  *
120  * Return 1 on success, 0 on failure
121  */
122 int do_beeper (char *sequence)
123 {
124 #define DELIMITER       ';'
125
126         int args[4];
127         int i;
128         int val;
129         char *p = sequence;
130         char *tp;
131
132         /*
133          * Parse the control sequence.  This is a really simple parser
134          * without any real error checking.  You can probably blow it
135          * up really easily.
136          */
137         if (*p == '\0' || !isdigit (*p)) {
138                 printf ("%s:%d: null or invalid string (%s)\n",
139                         __FILE__, __LINE__, p);
140                 return 0;
141         }
142
143         i = 0;
144         while (*p != '\0') {
145                 while (*p != DELIMITER) {
146                         if (i > 3)
147                                 i = 0;
148                         val = (int) simple_strtol (p, &tp, 0);
149                         if (tp == p) {
150                                 printf ("%s:%d: no digits or bad format\n",
151                                         __FILE__, __LINE__);
152                                 return 0;
153                         } else {
154                                 args[i] = val;
155                         }
156
157                         i++;
158                         if (*tp == DELIMITER)
159                                 p = tp;
160                         else
161                                 p = ++tp;
162                 }
163                 p++;
164
165                 /*
166                  * Well, we got something that has a chance of being correct
167                  */
168 #if 0
169                 for (i = 0; i < 4; i++) {
170                         printf ("%s:%d:arg %d = %d\n", __FILE__, __LINE__, i,
171                                 args[i]);
172                 }
173                 printf ("\n");
174 #endif
175                 set_beeper_frequency (args[0]);
176                 set_beeper_volume (args[1]);
177                 beeper_on ();
178                 udelay (1000 * args[2]);
179                 beeper_off ();
180                 udelay (1000 * args[3]);
181         }
182         return 1;
183 }