ixp4xx-beeper.c 4.0 KB

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