sparcspkr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Driver for PC-speaker like devices found on various Sparc systems.
  3. *
  4. * Copyright (c) 2002 Vojtech Pavlik
  5. * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  6. */
  7. #include <linux/config.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/input.h>
  12. #include <linux/platform_device.h>
  13. #include <asm/io.h>
  14. #include <asm/ebus.h>
  15. #ifdef CONFIG_SPARC64
  16. #include <asm/isa.h>
  17. #endif
  18. MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
  19. MODULE_DESCRIPTION("Sparc Speaker beeper driver");
  20. MODULE_LICENSE("GPL");
  21. const char *beep_name;
  22. static unsigned long beep_iobase;
  23. static int (*beep_event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  24. static DEFINE_SPINLOCK(beep_lock);
  25. static int ebus_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  26. {
  27. unsigned int count = 0;
  28. unsigned long flags;
  29. if (type != EV_SND)
  30. return -1;
  31. switch (code) {
  32. case SND_BELL: if (value) value = 1000;
  33. case SND_TONE: break;
  34. default: return -1;
  35. }
  36. if (value > 20 && value < 32767)
  37. count = 1193182 / value;
  38. spin_lock_irqsave(&beep_lock, flags);
  39. /* EBUS speaker only has on/off state, the frequency does not
  40. * appear to be programmable.
  41. */
  42. if (beep_iobase & 0x2UL)
  43. outb(!!count, beep_iobase);
  44. else
  45. outl(!!count, beep_iobase);
  46. spin_unlock_irqrestore(&beep_lock, flags);
  47. return 0;
  48. }
  49. #ifdef CONFIG_SPARC64
  50. static int isa_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  51. {
  52. unsigned int count = 0;
  53. unsigned long flags;
  54. if (type != EV_SND)
  55. return -1;
  56. switch (code) {
  57. case SND_BELL: if (value) value = 1000;
  58. case SND_TONE: break;
  59. default: return -1;
  60. }
  61. if (value > 20 && value < 32767)
  62. count = 1193182 / value;
  63. spin_lock_irqsave(&beep_lock, flags);
  64. if (count) {
  65. /* enable counter 2 */
  66. outb(inb(beep_iobase + 0x61) | 3, beep_iobase + 0x61);
  67. /* set command for counter 2, 2 byte write */
  68. outb(0xB6, beep_iobase + 0x43);
  69. /* select desired HZ */
  70. outb(count & 0xff, beep_iobase + 0x42);
  71. outb((count >> 8) & 0xff, beep_iobase + 0x42);
  72. } else {
  73. /* disable counter 2 */
  74. outb(inb_p(beep_iobase + 0x61) & 0xFC, beep_iobase + 0x61);
  75. }
  76. spin_unlock_irqrestore(&beep_lock, flags);
  77. return 0;
  78. }
  79. #endif
  80. static int __devinit sparcspkr_probe(struct platform_device *dev)
  81. {
  82. struct input_dev *input_dev;
  83. int error;
  84. input_dev = input_allocate_device();
  85. if (!input_dev)
  86. return -ENOMEM;
  87. input_dev->name = beep_name;
  88. input_dev->phys = "sparc/input0";
  89. input_dev->id.bustype = BUS_ISA;
  90. input_dev->id.vendor = 0x001f;
  91. input_dev->id.product = 0x0001;
  92. input_dev->id.version = 0x0100;
  93. input_dev->cdev.dev = &dev->dev;
  94. input_dev->evbit[0] = BIT(EV_SND);
  95. input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
  96. input_dev->event = beep_event;
  97. error = input_register_device(input_dev);
  98. if (error) {
  99. input_free_device(input_dev);
  100. return error;
  101. }
  102. platform_set_drvdata(dev, input_dev);
  103. return 0;
  104. }
  105. static int __devexit sparcspkr_remove(struct platform_device *dev)
  106. {
  107. struct input_dev *input_dev = platform_get_drvdata(dev);
  108. input_unregister_device(input_dev);
  109. platform_set_drvdata(dev, NULL);
  110. /* turn off the speaker */
  111. beep_event(NULL, EV_SND, SND_BELL, 0);
  112. return 0;
  113. }
  114. static void sparcspkr_shutdown(struct platform_device *dev)
  115. {
  116. /* turn off the speaker */
  117. beep_event(NULL, EV_SND, SND_BELL, 0);
  118. }
  119. static struct platform_driver sparcspkr_platform_driver = {
  120. .driver = {
  121. .name = "sparcspkr",
  122. .owner = THIS_MODULE,
  123. },
  124. .probe = sparcspkr_probe,
  125. .remove = __devexit_p(sparcspkr_remove),
  126. .shutdown = sparcspkr_shutdown,
  127. };
  128. static struct platform_device *sparcspkr_platform_device;
  129. static int __init sparcspkr_drv_init(void)
  130. {
  131. int error;
  132. error = platform_driver_register(&sparcspkr_platform_driver);
  133. if (error)
  134. return error;
  135. sparcspkr_platform_device = platform_device_alloc("sparcspkr", -1);
  136. if (!sparcspkr_platform_device) {
  137. error = -ENOMEM;
  138. goto err_unregister_driver;
  139. }
  140. error = platform_device_add(sparcspkr_platform_device);
  141. if (error)
  142. goto err_free_device;
  143. return 0;
  144. err_free_device:
  145. platform_device_put(sparcspkr_platform_device);
  146. err_unregister_driver:
  147. platform_driver_unregister(&sparcspkr_platform_driver);
  148. return error;
  149. }
  150. static int __init sparcspkr_init(void)
  151. {
  152. struct linux_ebus *ebus;
  153. struct linux_ebus_device *edev;
  154. #ifdef CONFIG_SPARC64
  155. struct sparc_isa_bridge *isa_br;
  156. struct sparc_isa_device *isa_dev;
  157. #endif
  158. for_each_ebus(ebus) {
  159. for_each_ebusdev(edev, ebus) {
  160. if (!strcmp(edev->prom_name, "beep")) {
  161. beep_name = "Sparc EBUS Speaker";
  162. beep_event = ebus_spkr_event;
  163. beep_iobase = edev->resource[0].start;
  164. return sparcspkr_drv_init();
  165. }
  166. }
  167. }
  168. #ifdef CONFIG_SPARC64
  169. for_each_isa(isa_br) {
  170. for_each_isadev(isa_dev, isa_br) {
  171. /* A hack, the beep device's base lives in
  172. * the DMA isa node.
  173. */
  174. if (!strcmp(isa_dev->prom_name, "dma")) {
  175. beep_name = "Sparc ISA Speaker";
  176. beep_event = isa_spkr_event,
  177. beep_iobase = isa_dev->resource.start;
  178. return sparcspkr_drv_init();
  179. }
  180. }
  181. }
  182. #endif
  183. return -ENODEV;
  184. }
  185. static void __exit sparcspkr_exit(void)
  186. {
  187. platform_device_unregister(sparcspkr_platform_device);
  188. platform_driver_unregister(&sparcspkr_platform_driver);
  189. }
  190. module_init(sparcspkr_init);
  191. module_exit(sparcspkr_exit);