]>
Commit | Line | Data |
---|---|---|
c609719b WD |
1 | /* |
2 | * (C) Copyright 2002 | |
3 | * Keith Outwater, [email protected] | |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
c609719b WD |
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 | ||
c609719b WD |
21 | /* |
22 | * Initialize beeper-related hardware. Initialize timer 1 for use with | |
8ed44d91 | 23 | * the beeper. Use 66 MHz internal clock with prescale of 33 to get |
c609719b WD |
24 | * 1 uS period per count. |
25 | * FIXME: we should really compute the prescale based on the reported | |
26 | * core clock frequency. | |
27 | */ | |
53677ef1 | 28 | void init_beeper (void) |
c609719b | 29 | { |
6d0f6bcf | 30 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
c609719b WD |
31 | |
32 | immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_RST1 | TGCR_STP1; | |
33 | immap->im_cpmtimer.cpmt_tmr1 = ((33 << TMR_PS_SHIFT) & TMR_PS_MSK) | |
53677ef1 | 34 | | TMR_OM | TMR_FRR | TMR_ICLK_IN_GEN; |
c609719b WD |
35 | immap->im_cpmtimer.cpmt_tcn1 = 0; |
36 | immap->im_cpmtimer.cpmt_ter1 = 0xffff; | |
37 | immap->im_cpmtimer.cpmt_tgcr |= TGCR_RST1; | |
38 | } | |
39 | ||
c609719b WD |
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 | */ | |
53677ef1 | 45 | void set_beeper_frequency (uint frequency) |
c609719b WD |
46 | { |
47 | #define FREQ_LIMIT 2500 | |
48 | ||
6d0f6bcf | 49 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
c609719b WD |
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 | */ | |
53677ef1 WD |
55 | if (frequency > FREQ_LIMIT) |
56 | frequency = FREQ_LIMIT; | |
57 | frequency = 1000000 / frequency; | |
58 | immap->im_cpmtimer.cpmt_trr1 = (ushort) frequency; | |
c609719b WD |
59 | } |
60 | ||
c609719b WD |
61 | /* |
62 | * Turn the beeper on | |
63 | */ | |
53677ef1 | 64 | void beeper_on (void) |
c609719b | 65 | { |
6d0f6bcf | 66 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
c609719b WD |
67 | |
68 | immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_STP1; | |
69 | } | |
70 | ||
c609719b WD |
71 | /* |
72 | * Turn the beeper off | |
73 | */ | |
53677ef1 | 74 | void beeper_off (void) |
c609719b | 75 | { |
6d0f6bcf | 76 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
c609719b WD |
77 | |
78 | immap->im_cpmtimer.cpmt_tgcr |= TGCR_STP1; | |
79 | } | |
80 | ||
c609719b WD |
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 | */ | |
53677ef1 | 89 | void set_beeper_volume (int steps) |
c609719b | 90 | { |
6d0f6bcf | 91 | volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR; |
c609719b WD |
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); | |
53677ef1 | 97 | udelay (1); |
c609719b | 98 | immap->im_cpm.cp_pbodr |= (0x80000000 >> 19); |
53677ef1 | 99 | udelay (1); |
c609719b | 100 | } |
53677ef1 | 101 | } else { |
c609719b WD |
102 | for (i = 0; i > (steps <= -64 ? -64 : steps); i--) { |
103 | immap->im_cpm.cp_pbdat &= ~(0x80000000 >> 19); | |
53677ef1 | 104 | udelay (1); |
c609719b | 105 | immap->im_cpm.cp_pbdat |= (0x80000000 >> 19); |
53677ef1 | 106 | udelay (1); |
c609719b WD |
107 | } |
108 | } | |
109 | } | |
110 | ||
c609719b WD |
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: | |
53677ef1 | 115 | * freq = frequency in Hz (0 - 2500) |
c609719b WD |
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 | */ | |
53677ef1 | 122 | int do_beeper (char *sequence) |
c609719b WD |
123 | { |
124 | #define DELIMITER ';' | |
125 | ||
53677ef1 WD |
126 | int args[4]; |
127 | int i; | |
128 | int val; | |
129 | char *p = sequence; | |
130 | char *tp; | |
c609719b WD |
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 | */ | |
53677ef1 WD |
137 | if (*p == '\0' || !isdigit (*p)) { |
138 | printf ("%s:%d: null or invalid string (%s)\n", | |
139 | __FILE__, __LINE__, p); | |
c609719b WD |
140 | return 0; |
141 | } | |
142 | ||
143 | i = 0; | |
144 | while (*p != '\0') { | |
145 | while (*p != DELIMITER) { | |
53677ef1 WD |
146 | if (i > 3) |
147 | i = 0; | |
148 | val = (int) simple_strtol (p, &tp, 0); | |
c609719b | 149 | if (tp == p) { |
53677ef1 WD |
150 | printf ("%s:%d: no digits or bad format\n", |
151 | __FILE__, __LINE__); | |
c609719b | 152 | return 0; |
53677ef1 | 153 | } else { |
c609719b WD |
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++) { | |
53677ef1 WD |
170 | printf ("%s:%d:arg %d = %d\n", __FILE__, __LINE__, i, |
171 | args[i]); | |
c609719b | 172 | } |
53677ef1 | 173 | printf ("\n"); |
c609719b | 174 | #endif |
53677ef1 WD |
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]); | |
c609719b WD |
181 | } |
182 | return 1; | |
183 | } |