pcsp.c 4.9 KB

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