pcspkr.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PC Speaker beeper driver for Linux
  4. *
  5. * Copyright (c) 2002 Vojtech Pavlik
  6. * Copyright (c) 1992 Orest Zborowski
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/i8253.h>
  11. #include <linux/input.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/timex.h>
  14. #include <linux/io.h>
  15. MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
  16. MODULE_DESCRIPTION("PC Speaker beeper driver");
  17. MODULE_LICENSE("GPL");
  18. MODULE_ALIAS("platform:pcspkr");
  19. static int pcspkr_event(struct input_dev *dev, unsigned int type,
  20. unsigned int code, int value)
  21. {
  22. unsigned int count = 0;
  23. unsigned long flags;
  24. if (type != EV_SND)
  25. return -EINVAL;
  26. switch (code) {
  27. case SND_BELL:
  28. if (value)
  29. value = 1000;
  30. break;
  31. case SND_TONE:
  32. break;
  33. default:
  34. return -EINVAL;
  35. }
  36. if (value > 20 && value < 32767)
  37. count = PIT_TICK_RATE / value;
  38. raw_spin_lock_irqsave(&i8253_lock, flags);
  39. if (count) {
  40. /* set command for counter 2, 2 byte write */
  41. outb_p(0xB6, 0x43);
  42. /* select desired HZ */
  43. outb_p(count & 0xff, 0x42);
  44. outb((count >> 8) & 0xff, 0x42);
  45. /* enable counter 2 */
  46. outb_p(inb_p(0x61) | 3, 0x61);
  47. } else {
  48. /* disable counter 2 */
  49. outb(inb_p(0x61) & 0xFC, 0x61);
  50. }
  51. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  52. return 0;
  53. }
  54. static int pcspkr_probe(struct platform_device *dev)
  55. {
  56. struct input_dev *pcspkr_dev;
  57. int err;
  58. pcspkr_dev = input_allocate_device();
  59. if (!pcspkr_dev)
  60. return -ENOMEM;
  61. pcspkr_dev->name = "PC Speaker";
  62. pcspkr_dev->phys = "isa0061/input0";
  63. pcspkr_dev->id.bustype = BUS_ISA;
  64. pcspkr_dev->id.vendor = 0x001f;
  65. pcspkr_dev->id.product = 0x0001;
  66. pcspkr_dev->id.version = 0x0100;
  67. pcspkr_dev->dev.parent = &dev->dev;
  68. pcspkr_dev->evbit[0] = BIT_MASK(EV_SND);
  69. pcspkr_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  70. pcspkr_dev->event = pcspkr_event;
  71. err = input_register_device(pcspkr_dev);
  72. if (err) {
  73. input_free_device(pcspkr_dev);
  74. return err;
  75. }
  76. platform_set_drvdata(dev, pcspkr_dev);
  77. return 0;
  78. }
  79. static int pcspkr_remove(struct platform_device *dev)
  80. {
  81. struct input_dev *pcspkr_dev = platform_get_drvdata(dev);
  82. input_unregister_device(pcspkr_dev);
  83. /* turn off the speaker */
  84. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  85. return 0;
  86. }
  87. static int pcspkr_suspend(struct device *dev)
  88. {
  89. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  90. return 0;
  91. }
  92. static void pcspkr_shutdown(struct platform_device *dev)
  93. {
  94. /* turn off the speaker */
  95. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  96. }
  97. static const struct dev_pm_ops pcspkr_pm_ops = {
  98. .suspend = pcspkr_suspend,
  99. };
  100. static struct platform_driver pcspkr_platform_driver = {
  101. .driver = {
  102. .name = "pcspkr",
  103. .pm = &pcspkr_pm_ops,
  104. },
  105. .probe = pcspkr_probe,
  106. .remove = pcspkr_remove,
  107. .shutdown = pcspkr_shutdown,
  108. };
  109. module_platform_driver(pcspkr_platform_driver);