pcspkr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * PC Speaker beeper driver for Linux
  3. *
  4. * Copyright (c) 2002 Vojtech Pavlik
  5. * Copyright (c) 1992 Orest Zborowski
  6. *
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/input.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/timex.h>
  19. #include <asm/io.h>
  20. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  21. MODULE_DESCRIPTION("PC Speaker beeper driver");
  22. MODULE_LICENSE("GPL");
  23. MODULE_ALIAS("platform:pcspkr");
  24. #if defined(CONFIG_MIPS) || defined(CONFIG_X86)
  25. /* Use the global PIT lock ! */
  26. #include <asm/i8253.h>
  27. #else
  28. #include <asm/8253pit.h>
  29. static DEFINE_RAW_SPINLOCK(i8253_lock);
  30. #endif
  31. static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  32. {
  33. unsigned int count = 0;
  34. unsigned long flags;
  35. if (type != EV_SND)
  36. return -1;
  37. switch (code) {
  38. case SND_BELL: if (value) value = 1000;
  39. case SND_TONE: break;
  40. default: return -1;
  41. }
  42. if (value > 20 && value < 32767)
  43. count = PIT_TICK_RATE / value;
  44. raw_spin_lock_irqsave(&i8253_lock, flags);
  45. if (count) {
  46. /* set command for counter 2, 2 byte write */
  47. outb_p(0xB6, 0x43);
  48. /* select desired HZ */
  49. outb_p(count & 0xff, 0x42);
  50. outb((count >> 8) & 0xff, 0x42);
  51. /* enable counter 2 */
  52. outb_p(inb_p(0x61) | 3, 0x61);
  53. } else {
  54. /* disable counter 2 */
  55. outb(inb_p(0x61) & 0xFC, 0x61);
  56. }
  57. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  58. return 0;
  59. }
  60. static int __devinit pcspkr_probe(struct platform_device *dev)
  61. {
  62. struct input_dev *pcspkr_dev;
  63. int err;
  64. pcspkr_dev = input_allocate_device();
  65. if (!pcspkr_dev)
  66. return -ENOMEM;
  67. pcspkr_dev->name = "PC Speaker";
  68. pcspkr_dev->phys = "isa0061/input0";
  69. pcspkr_dev->id.bustype = BUS_ISA;
  70. pcspkr_dev->id.vendor = 0x001f;
  71. pcspkr_dev->id.product = 0x0001;
  72. pcspkr_dev->id.version = 0x0100;
  73. pcspkr_dev->dev.parent = &dev->dev;
  74. pcspkr_dev->evbit[0] = BIT_MASK(EV_SND);
  75. pcspkr_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  76. pcspkr_dev->event = pcspkr_event;
  77. err = input_register_device(pcspkr_dev);
  78. if (err) {
  79. input_free_device(pcspkr_dev);
  80. return err;
  81. }
  82. platform_set_drvdata(dev, pcspkr_dev);
  83. return 0;
  84. }
  85. static int __devexit pcspkr_remove(struct platform_device *dev)
  86. {
  87. struct input_dev *pcspkr_dev = platform_get_drvdata(dev);
  88. input_unregister_device(pcspkr_dev);
  89. platform_set_drvdata(dev, NULL);
  90. /* turn off the speaker */
  91. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  92. return 0;
  93. }
  94. static int pcspkr_suspend(struct device *dev)
  95. {
  96. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  97. return 0;
  98. }
  99. static void pcspkr_shutdown(struct platform_device *dev)
  100. {
  101. /* turn off the speaker */
  102. pcspkr_event(NULL, EV_SND, SND_BELL, 0);
  103. }
  104. static const struct dev_pm_ops pcspkr_pm_ops = {
  105. .suspend = pcspkr_suspend,
  106. };
  107. static struct platform_driver pcspkr_platform_driver = {
  108. .driver = {
  109. .name = "pcspkr",
  110. .owner = THIS_MODULE,
  111. .pm = &pcspkr_pm_ops,
  112. },
  113. .probe = pcspkr_probe,
  114. .remove = __devexit_p(pcspkr_remove),
  115. .shutdown = pcspkr_shutdown,
  116. };
  117. static int __init pcspkr_init(void)
  118. {
  119. return platform_driver_register(&pcspkr_platform_driver);
  120. }
  121. static void __exit pcspkr_exit(void)
  122. {
  123. platform_driver_unregister(&pcspkr_platform_driver);
  124. }
  125. module_init(pcspkr_init);
  126. module_exit(pcspkr_exit);