ixp4xx-beeper.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Generic IXP4xx beeper driver
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. *
  6. * based on nslu2-io.c
  7. * Copyright (C) 2004 Karen Spearel
  8. *
  9. * Author: Alessandro Zummo <a.zummo@towertech.it>
  10. * Maintainers: http://www.nslu2-linux.org/
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/input.h>
  19. #include <linux/delay.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/hardware.h>
  22. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  23. MODULE_DESCRIPTION("ixp4xx beeper driver");
  24. MODULE_LICENSE("GPL");
  25. static DEFINE_SPINLOCK(beep_lock);
  26. static void ixp4xx_spkr_control(unsigned int pin, unsigned int count)
  27. {
  28. unsigned long flags;
  29. spin_lock_irqsave(&beep_lock, flags);
  30. if (count) {
  31. gpio_line_config(pin, IXP4XX_GPIO_OUT);
  32. gpio_line_set(pin, IXP4XX_GPIO_LOW);
  33. *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
  34. } else {
  35. gpio_line_config(pin, IXP4XX_GPIO_IN);
  36. gpio_line_set(pin, IXP4XX_GPIO_HIGH);
  37. *IXP4XX_OSRT2 = 0;
  38. }
  39. spin_unlock_irqrestore(&beep_lock, flags);
  40. }
  41. static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  42. {
  43. unsigned int pin = (unsigned int) dev->private;
  44. unsigned int count = 0;
  45. if (type != EV_SND)
  46. return -1;
  47. switch (code) {
  48. case SND_BELL:
  49. if (value)
  50. value = 1000;
  51. case SND_TONE:
  52. break;
  53. default:
  54. return -1;
  55. }
  56. if (value > 20 && value < 32767)
  57. #ifndef FREQ
  58. count = (ixp4xx_get_board_tick_rate() / (value * 4)) - 1;
  59. #else
  60. count = (FREQ / (value * 4)) - 1;
  61. #endif
  62. ixp4xx_spkr_control(pin, count);
  63. return 0;
  64. }
  65. static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  66. {
  67. /* clear interrupt */
  68. *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND;
  69. /* flip the beeper output */
  70. *IXP4XX_GPIO_GPOUTR ^= (1 << (unsigned int) dev_id);
  71. return IRQ_HANDLED;
  72. }
  73. static int __devinit ixp4xx_spkr_probe(struct platform_device *dev)
  74. {
  75. struct input_dev *input_dev;
  76. int err;
  77. input_dev = input_allocate_device();
  78. if (!input_dev)
  79. return -ENOMEM;
  80. input_dev->private = (void *) dev->id;
  81. input_dev->name = "ixp4xx beeper",
  82. input_dev->phys = "ixp4xx/gpio";
  83. input_dev->id.bustype = BUS_HOST;
  84. input_dev->id.vendor = 0x001f;
  85. input_dev->id.product = 0x0001;
  86. input_dev->id.version = 0x0100;
  87. input_dev->cdev.dev = &dev->dev;
  88. input_dev->evbit[0] = BIT(EV_SND);
  89. input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
  90. input_dev->event = ixp4xx_spkr_event;
  91. err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt,
  92. SA_INTERRUPT | SA_TIMER, "ixp4xx-beeper", (void *) dev->id);
  93. if (err)
  94. goto err_free_device;
  95. err = input_register_device(input_dev);
  96. if (err)
  97. goto err_free_irq;
  98. platform_set_drvdata(dev, input_dev);
  99. return 0;
  100. err_free_irq:
  101. free_irq(IRQ_IXP4XX_TIMER2, dev);
  102. err_free_device:
  103. input_free_device(input_dev);
  104. return err;
  105. }
  106. static int __devexit ixp4xx_spkr_remove(struct platform_device *dev)
  107. {
  108. struct input_dev *input_dev = platform_get_drvdata(dev);
  109. unsigned int pin = (unsigned int) input_dev->private;
  110. input_unregister_device(input_dev);
  111. platform_set_drvdata(dev, NULL);
  112. /* turn the speaker off */
  113. disable_irq(IRQ_IXP4XX_TIMER2);
  114. ixp4xx_spkr_control(pin, 0);
  115. free_irq(IRQ_IXP4XX_TIMER2, dev);
  116. return 0;
  117. }
  118. static void ixp4xx_spkr_shutdown(struct platform_device *dev)
  119. {
  120. struct input_dev *input_dev = platform_get_drvdata(dev);
  121. unsigned int pin = (unsigned int) input_dev->private;
  122. /* turn off the speaker */
  123. disable_irq(IRQ_IXP4XX_TIMER2);
  124. ixp4xx_spkr_control(pin, 0);
  125. }
  126. static struct platform_driver ixp4xx_spkr_platform_driver = {
  127. .driver = {
  128. .name = "ixp4xx-beeper",
  129. .owner = THIS_MODULE,
  130. },
  131. .probe = ixp4xx_spkr_probe,
  132. .remove = __devexit_p(ixp4xx_spkr_remove),
  133. .shutdown = ixp4xx_spkr_shutdown,
  134. };
  135. static int __init ixp4xx_spkr_init(void)
  136. {
  137. return platform_driver_register(&ixp4xx_spkr_platform_driver);
  138. }
  139. static void __exit ixp4xx_spkr_exit(void)
  140. {
  141. platform_driver_unregister(&ixp4xx_spkr_platform_driver);
  142. }
  143. module_init(ixp4xx_spkr_init);
  144. module_exit(ixp4xx_spkr_exit);