beeper.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  42. init_beeper(void)
  43. {
  44. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  45. immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_RST1 | TGCR_STP1;
  46. immap->im_cpmtimer.cpmt_tmr1 = ((33 << TMR_PS_SHIFT) & TMR_PS_MSK)
  47. | TMR_OM | TMR_FRR | TMR_ICLK_IN_GEN;
  48. immap->im_cpmtimer.cpmt_tcn1 = 0;
  49. immap->im_cpmtimer.cpmt_ter1 = 0xffff;
  50. immap->im_cpmtimer.cpmt_tgcr |= TGCR_RST1;
  51. }
  52. /*
  53. * Set beeper frequency. Max allowed frequency is 2.5 KHz. This limit
  54. * is mostly arbitrary, but the beeper isn't really much good beyond this
  55. * frequency.
  56. */
  57. void
  58. set_beeper_frequency(uint frequency)
  59. {
  60. #define FREQ_LIMIT 2500
  61. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  62. /*
  63. * Compute timer ticks given desired frequency. The timer is set up
  64. * to count 0.5 uS per tick and it takes two ticks per cycle (Hz).
  65. */
  66. if (frequency > FREQ_LIMIT) frequency = FREQ_LIMIT;
  67. frequency = 1000000/frequency;
  68. immap->im_cpmtimer.cpmt_trr1 = (ushort)frequency;
  69. }
  70. /*
  71. * Turn the beeper on
  72. */
  73. void
  74. beeper_on(void)
  75. {
  76. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  77. immap->im_cpmtimer.cpmt_tgcr &= ~TGCR_STP1;
  78. }
  79. /*
  80. * Turn the beeper off
  81. */
  82. void
  83. beeper_off(void)
  84. {
  85. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  86. immap->im_cpmtimer.cpmt_tgcr |= TGCR_STP1;
  87. }
  88. /*
  89. * Increase or decrease the beeper volume. Volume can be set
  90. * from off to full in 64 steps. To increase volume, the output
  91. * pin is actively driven high, then returned to tristate.
  92. * To decrease volume, output a low on the port pin (no need to
  93. * change pin mode to tristate) then output a high to go back to
  94. * tristate.
  95. */
  96. void
  97. set_beeper_volume(int steps)
  98. {
  99. volatile immap_t *immap = (immap_t *)CFG_IMMR;
  100. int i;
  101. if (steps >= 0) {
  102. for (i = 0; i < (steps >= 64 ? 64 : steps); i++) {
  103. immap->im_cpm.cp_pbodr &= ~(0x80000000 >> 19);
  104. udelay(1);
  105. immap->im_cpm.cp_pbodr |= (0x80000000 >> 19);
  106. udelay(1);
  107. }
  108. }
  109. else {
  110. for (i = 0; i > (steps <= -64 ? -64 : steps); i--) {
  111. immap->im_cpm.cp_pbdat &= ~(0x80000000 >> 19);
  112. udelay(1);
  113. immap->im_cpm.cp_pbdat |= (0x80000000 >> 19);
  114. udelay(1);
  115. }
  116. }
  117. }
  118. /*
  119. * Check the environment to see if the beeper needs beeping.
  120. * Controlled by a sequence of the form:
  121. * freq/delta volume/on time/off time;... where:
  122. * freq = frequency in Hz (0 - 2500)
  123. * delta volume = volume steps up or down (-64 <= vol <= 64)
  124. * on time = time in mS
  125. * off time = time in mS
  126. *
  127. * Return 1 on success, 0 on failure
  128. */
  129. int
  130. do_beeper(char *sequence)
  131. {
  132. #define DELIMITER ';'
  133. int args[4];
  134. int i;
  135. int val;
  136. char *p = sequence;
  137. char *tp;
  138. /*
  139. * Parse the control sequence. This is a really simple parser
  140. * without any real error checking. You can probably blow it
  141. * up really easily.
  142. */
  143. if (*p == '\0' || !isdigit(*p)) {
  144. printf("%s:%d: null or invalid string (%s)\n",
  145. __FILE__, __LINE__, p);
  146. return 0;
  147. }
  148. i = 0;
  149. while (*p != '\0') {
  150. while (*p != DELIMITER) {
  151. if (i > 3) i = 0;
  152. val = (int) simple_strtol(p, &tp, 0);
  153. if (tp == p) {
  154. printf("%s:%d: no digits or bad format\n",
  155. __FILE__,__LINE__);
  156. return 0;
  157. }
  158. else {
  159. args[i] = val;
  160. }
  161. i++;
  162. if (*tp == DELIMITER)
  163. p = tp;
  164. else
  165. p = ++tp;
  166. }
  167. p++;
  168. /*
  169. * Well, we got something that has a chance of being correct
  170. */
  171. #if 0
  172. for (i = 0; i < 4; i++) {
  173. printf("%s:%d:arg %d = %d\n", __FILE__, __LINE__, i, args[i]);
  174. }
  175. printf("\n");
  176. #endif
  177. set_beeper_frequency(args[0]);
  178. set_beeper_volume(args[1]);
  179. beeper_on();
  180. udelay(1000 * args[2]);
  181. beeper_off();
  182. udelay(1000 * args[3]);
  183. }
  184. return 1;
  185. }
  186. /* vim: set ts=4 sw=4 tw=78: */