beeper.c 5.0 KB

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