pcsp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * PC-Speaker driver for Linux
  3. *
  4. * Copyright (C) 1997-2001 David Woodhouse
  5. * Copyright (C) 2001-2008 Stas Sergeev
  6. */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <sound/core.h>
  11. #include <sound/initval.h>
  12. #include <sound/pcm.h>
  13. #include <linux/input.h>
  14. #include <linux/delay.h>
  15. #include <linux/bitops.h>
  16. #include "pcsp_input.h"
  17. #include "pcsp.h"
  18. MODULE_AUTHOR("Stas Sergeev <stsp@users.sourceforge.net>");
  19. MODULE_DESCRIPTION("PC-Speaker driver");
  20. MODULE_LICENSE("GPL");
  21. MODULE_SUPPORTED_DEVICE("{{PC-Speaker, pcsp}}");
  22. MODULE_ALIAS("platform:pcspkr");
  23. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  24. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  25. static bool enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
  26. static bool nopcm; /* Disable PCM capability of the driver */
  27. module_param(index, int, 0444);
  28. MODULE_PARM_DESC(index, "Index value for pcsp soundcard.");
  29. module_param(id, charp, 0444);
  30. MODULE_PARM_DESC(id, "ID string for pcsp soundcard.");
  31. module_param(enable, bool, 0444);
  32. MODULE_PARM_DESC(enable, "Enable PC-Speaker sound.");
  33. module_param(nopcm, bool, 0444);
  34. MODULE_PARM_DESC(nopcm, "Disable PC-Speaker PCM sound. Only beeps remain.");
  35. struct snd_pcsp pcsp_chip;
  36. static int snd_pcsp_create(struct snd_card *card)
  37. {
  38. static struct snd_device_ops ops = { };
  39. unsigned int resolution = hrtimer_resolution;
  40. int err, div, min_div, order;
  41. if (!nopcm) {
  42. if (resolution > PCSP_MAX_PERIOD_NS) {
  43. printk(KERN_ERR "PCSP: Timer resolution is not sufficient "
  44. "(%unS)\n", resolution);
  45. printk(KERN_ERR "PCSP: Make sure you have HPET and ACPI "
  46. "enabled.\n");
  47. printk(KERN_ERR "PCSP: Turned into nopcm mode.\n");
  48. nopcm = 1;
  49. }
  50. }
  51. if (loops_per_jiffy >= PCSP_MIN_LPJ && resolution <= PCSP_MIN_PERIOD_NS)
  52. min_div = MIN_DIV;
  53. else
  54. min_div = MAX_DIV;
  55. #if PCSP_DEBUG
  56. printk(KERN_DEBUG "PCSP: lpj=%li, min_div=%i, res=%u\n",
  57. loops_per_jiffy, min_div, resolution);
  58. #endif
  59. div = MAX_DIV / min_div;
  60. order = fls(div) - 1;
  61. pcsp_chip.max_treble = min(order, PCSP_MAX_TREBLE);
  62. pcsp_chip.treble = min(pcsp_chip.max_treble, PCSP_DEFAULT_TREBLE);
  63. pcsp_chip.playback_ptr = 0;
  64. pcsp_chip.period_ptr = 0;
  65. atomic_set(&pcsp_chip.timer_active, 0);
  66. pcsp_chip.enable = 1;
  67. pcsp_chip.pcspkr = 1;
  68. spin_lock_init(&pcsp_chip.substream_lock);
  69. pcsp_chip.card = card;
  70. pcsp_chip.port = 0x61;
  71. pcsp_chip.irq = -1;
  72. pcsp_chip.dma = -1;
  73. /* Register device */
  74. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, &pcsp_chip, &ops);
  75. if (err < 0)
  76. return err;
  77. return 0;
  78. }
  79. static int snd_card_pcsp_probe(int devnum, struct device *dev)
  80. {
  81. struct snd_card *card;
  82. int err;
  83. if (devnum != 0)
  84. return -EINVAL;
  85. hrtimer_init(&pcsp_chip.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  86. pcsp_chip.timer.function = pcsp_do_timer;
  87. err = snd_card_new(dev, index, id, THIS_MODULE, 0, &card);
  88. if (err < 0)
  89. return err;
  90. err = snd_pcsp_create(card);
  91. if (err < 0) {
  92. snd_card_free(card);
  93. return err;
  94. }
  95. if (!nopcm) {
  96. err = snd_pcsp_new_pcm(&pcsp_chip);
  97. if (err < 0) {
  98. snd_card_free(card);
  99. return err;
  100. }
  101. }
  102. err = snd_pcsp_new_mixer(&pcsp_chip, nopcm);
  103. if (err < 0) {
  104. snd_card_free(card);
  105. return err;
  106. }
  107. strcpy(card->driver, "PC-Speaker");
  108. strcpy(card->shortname, "pcsp");
  109. sprintf(card->longname, "Internal PC-Speaker at port 0x%x",
  110. pcsp_chip.port);
  111. err = snd_card_register(card);
  112. if (err < 0) {
  113. snd_card_free(card);
  114. return err;
  115. }
  116. return 0;
  117. }
  118. static int alsa_card_pcsp_init(struct device *dev)
  119. {
  120. int err;
  121. err = snd_card_pcsp_probe(0, dev);
  122. if (err) {
  123. printk(KERN_ERR "PC-Speaker initialization failed.\n");
  124. return err;
  125. }
  126. #ifdef CONFIG_DEBUG_PAGEALLOC
  127. /* Well, CONFIG_DEBUG_PAGEALLOC makes the sound horrible. Lets alert */
  128. printk(KERN_WARNING "PCSP: CONFIG_DEBUG_PAGEALLOC is enabled, "
  129. "which may make the sound noisy.\n");
  130. #endif
  131. return 0;
  132. }
  133. static void alsa_card_pcsp_exit(struct snd_pcsp *chip)
  134. {
  135. snd_card_free(chip->card);
  136. }
  137. static int pcsp_probe(struct platform_device *dev)
  138. {
  139. int err;
  140. err = pcspkr_input_init(&pcsp_chip.input_dev, &dev->dev);
  141. if (err < 0)
  142. return err;
  143. err = alsa_card_pcsp_init(&dev->dev);
  144. if (err < 0) {
  145. pcspkr_input_remove(pcsp_chip.input_dev);
  146. return err;
  147. }
  148. platform_set_drvdata(dev, &pcsp_chip);
  149. return 0;
  150. }
  151. static int pcsp_remove(struct platform_device *dev)
  152. {
  153. struct snd_pcsp *chip = platform_get_drvdata(dev);
  154. pcspkr_input_remove(chip->input_dev);
  155. alsa_card_pcsp_exit(chip);
  156. return 0;
  157. }
  158. static void pcsp_stop_beep(struct snd_pcsp *chip)
  159. {
  160. pcsp_sync_stop(chip);
  161. pcspkr_stop_sound();
  162. }
  163. #ifdef CONFIG_PM_SLEEP
  164. static int pcsp_suspend(struct device *dev)
  165. {
  166. struct snd_pcsp *chip = dev_get_drvdata(dev);
  167. pcsp_stop_beep(chip);
  168. snd_pcm_suspend_all(chip->pcm);
  169. return 0;
  170. }
  171. static SIMPLE_DEV_PM_OPS(pcsp_pm, pcsp_suspend, NULL);
  172. #define PCSP_PM_OPS &pcsp_pm
  173. #else
  174. #define PCSP_PM_OPS NULL
  175. #endif /* CONFIG_PM_SLEEP */
  176. static void pcsp_shutdown(struct platform_device *dev)
  177. {
  178. struct snd_pcsp *chip = platform_get_drvdata(dev);
  179. pcsp_stop_beep(chip);
  180. }
  181. static struct platform_driver pcsp_platform_driver = {
  182. .driver = {
  183. .name = "pcspkr",
  184. .pm = PCSP_PM_OPS,
  185. },
  186. .probe = pcsp_probe,
  187. .remove = pcsp_remove,
  188. .shutdown = pcsp_shutdown,
  189. };
  190. static int __init pcsp_init(void)
  191. {
  192. if (!enable)
  193. return -ENODEV;
  194. return platform_driver_register(&pcsp_platform_driver);
  195. }
  196. static void __exit pcsp_exit(void)
  197. {
  198. platform_driver_unregister(&pcsp_platform_driver);
  199. }
  200. module_init(pcsp_init);
  201. module_exit(pcsp_exit);