ixp4xx-beeper.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <linux/interrupt.h>
  22. #include <asm/hardware.h>
  23. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  24. MODULE_DESCRIPTION("ixp4xx beeper driver");
  25. MODULE_LICENSE("GPL");
  26. static DEFINE_SPINLOCK(beep_lock);
  27. static void ixp4xx_spkr_control(unsigned int pin, unsigned int count)
  28. {
  29. unsigned long flags;
  30. spin_lock_irqsave(&beep_lock, flags);
  31. if (count) {
  32. gpio_line_config(pin, IXP4XX_GPIO_OUT);
  33. gpio_line_set(pin, IXP4XX_GPIO_LOW);
  34. *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
  35. } else {
  36. gpio_line_config(pin, IXP4XX_GPIO_IN);
  37. gpio_line_set(pin, IXP4XX_GPIO_HIGH);
  38. *IXP4XX_OSRT2 = 0;
  39. }
  40. spin_unlock_irqrestore(&beep_lock, flags);
  41. }
  42. static int ixp4xx_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  43. {
  44. unsigned int pin = (unsigned int) dev->private;
  45. unsigned int count = 0;
  46. if (type != EV_SND)
  47. return -1;
  48. switch (code) {
  49. case SND_BELL:
  50. if (value)
  51. value = 1000;
  52. case SND_TONE:
  53. break;
  54. default:
  55. return -1;
  56. }
  57. if (value > 20 && value < 32767)
  58. #ifndef FREQ
  59. count = (ixp4xx_get_board_tick_rate() / (value * 4)) - 1;
  60. #else
  61. count = (FREQ / (value * 4)) - 1;
  62. #endif
  63. ixp4xx_spkr_control(pin, count);
  64. return 0;
  65. }
  66. static irqreturn_t ixp4xx_spkr_interrupt(int irq, void *dev_id)
  67. {
  68. /* clear interrupt */
  69. *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND;
  70. /* flip the beeper output */
  71. *IXP4XX_GPIO_GPOUTR ^= (1 << (unsigned int) dev_id);
  72. return IRQ_HANDLED;
  73. }
  74. static int __devinit ixp4xx_spkr_probe(struct platform_device *dev)
  75. {
  76. struct input_dev *input_dev;
  77. int err;
  78. input_dev = input_allocate_device();
  79. if (!input_dev)
  80. return -ENOMEM;
  81. input_dev->private = (void *) dev->id;
  82. input_dev->name = "ixp4xx beeper",
  83. input_dev->phys = "ixp4xx/gpio";
  84. input_dev->id.bustype = BUS_HOST;
  85. input_dev->id.vendor = 0x001f;
  86. input_dev->id.product = 0x0001;
  87. input_dev->id.version = 0x0100;
  88. input_dev->cdev.dev = &dev->dev;
  89. input_dev->evbit[0] = BIT(EV_SND);
  90. input_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
  91. input_dev->event = ixp4xx_spkr_event;
  92. err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt,
  93. IRQF_DISABLED | IRQF_TIMER, "ixp4xx-beeper", (void *) dev->id);
  94. if (err)
  95. goto err_free_device;
  96. err = input_register_device(input_dev);
  97. if (err)
  98. goto err_free_irq;
  99. platform_set_drvdata(dev, input_dev);
  100. return 0;
  101. err_free_irq:
  102. free_irq(IRQ_IXP4XX_TIMER2, dev);
  103. err_free_device:
  104. input_free_device(input_dev);
  105. return err;
  106. }
  107. static int __devexit ixp4xx_spkr_remove(struct platform_device *dev)
  108. {
  109. struct input_dev *input_dev = platform_get_drvdata(dev);
  110. unsigned int pin = (unsigned int) input_dev->private;
  111. input_unregister_device(input_dev);
  112. platform_set_drvdata(dev, NULL);
  113. /* turn the speaker off */
  114. disable_irq(IRQ_IXP4XX_TIMER2);
  115. ixp4xx_spkr_control(pin, 0);
  116. free_irq(IRQ_IXP4XX_TIMER2, dev);
  117. return 0;
  118. }
  119. static void ixp4xx_spkr_shutdown(struct platform_device *dev)
  120. {
  121. struct input_dev *input_dev = platform_get_drvdata(dev);
  122. unsigned int pin = (unsigned int) input_dev->private;
  123. /* turn off the speaker */
  124. disable_irq(IRQ_IXP4XX_TIMER2);
  125. ixp4xx_spkr_control(pin, 0);
  126. }
  127. static struct platform_driver ixp4xx_spkr_platform_driver = {
  128. .driver = {
  129. .name = "ixp4xx-beeper",
  130. .owner = THIS_MODULE,
  131. },
  132. .probe = ixp4xx_spkr_probe,
  133. .remove = __devexit_p(ixp4xx_spkr_remove),
  134. .shutdown = ixp4xx_spkr_shutdown,
  135. };
  136. static int __init ixp4xx_spkr_init(void)
  137. {
  138. return platform_driver_register(&ixp4xx_spkr_platform_driver);
  139. }
  140. static void __exit ixp4xx_spkr_exit(void)
  141. {
  142. platform_driver_unregister(&ixp4xx_spkr_platform_driver);
  143. }
  144. module_init(ixp4xx_spkr_init);
  145. module_exit(ixp4xx_spkr_exit);