ixp4xx-beeper.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <mach/hardware.h>
  23. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  24. MODULE_DESCRIPTION("ixp4xx beeper driver");
  25. MODULE_LICENSE("GPL");
  26. MODULE_ALIAS("platform:ixp4xx-beeper");
  27. static DEFINE_SPINLOCK(beep_lock);
  28. static void ixp4xx_spkr_control(unsigned int pin, unsigned int count)
  29. {
  30. unsigned long flags;
  31. spin_lock_irqsave(&beep_lock, flags);
  32. if (count) {
  33. gpio_line_config(pin, IXP4XX_GPIO_OUT);
  34. gpio_line_set(pin, IXP4XX_GPIO_LOW);
  35. *IXP4XX_OSRT2 = (count & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
  36. } else {
  37. gpio_line_config(pin, IXP4XX_GPIO_IN);
  38. gpio_line_set(pin, IXP4XX_GPIO_HIGH);
  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. /* clear interrupt */
  66. *IXP4XX_OSST = IXP4XX_OSST_TIMER_2_PEND;
  67. /* flip the beeper output */
  68. *IXP4XX_GPIO_GPOUTR ^= (1 << (unsigned int) dev_id);
  69. return IRQ_HANDLED;
  70. }
  71. static int __devinit ixp4xx_spkr_probe(struct platform_device *dev)
  72. {
  73. struct input_dev *input_dev;
  74. int err;
  75. input_dev = input_allocate_device();
  76. if (!input_dev)
  77. return -ENOMEM;
  78. input_set_drvdata(input_dev, (void *) dev->id);
  79. input_dev->name = "ixp4xx beeper",
  80. input_dev->phys = "ixp4xx/gpio";
  81. input_dev->id.bustype = BUS_HOST;
  82. input_dev->id.vendor = 0x001f;
  83. input_dev->id.product = 0x0001;
  84. input_dev->id.version = 0x0100;
  85. input_dev->dev.parent = &dev->dev;
  86. input_dev->evbit[0] = BIT_MASK(EV_SND);
  87. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  88. input_dev->event = ixp4xx_spkr_event;
  89. err = request_irq(IRQ_IXP4XX_TIMER2, &ixp4xx_spkr_interrupt,
  90. IRQF_NO_SUSPEND, "ixp4xx-beeper",
  91. (void *) dev->id);
  92. if (err)
  93. goto err_free_device;
  94. err = input_register_device(input_dev);
  95. if (err)
  96. goto err_free_irq;
  97. platform_set_drvdata(dev, input_dev);
  98. return 0;
  99. err_free_irq:
  100. free_irq(IRQ_IXP4XX_TIMER2, dev);
  101. err_free_device:
  102. input_free_device(input_dev);
  103. return err;
  104. }
  105. static int __devexit ixp4xx_spkr_remove(struct platform_device *dev)
  106. {
  107. struct input_dev *input_dev = platform_get_drvdata(dev);
  108. unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
  109. input_unregister_device(input_dev);
  110. platform_set_drvdata(dev, NULL);
  111. /* turn the speaker off */
  112. disable_irq(IRQ_IXP4XX_TIMER2);
  113. ixp4xx_spkr_control(pin, 0);
  114. free_irq(IRQ_IXP4XX_TIMER2, dev);
  115. return 0;
  116. }
  117. static void ixp4xx_spkr_shutdown(struct platform_device *dev)
  118. {
  119. struct input_dev *input_dev = platform_get_drvdata(dev);
  120. unsigned int pin = (unsigned int) input_get_drvdata(input_dev);
  121. /* turn off the speaker */
  122. disable_irq(IRQ_IXP4XX_TIMER2);
  123. ixp4xx_spkr_control(pin, 0);
  124. }
  125. static struct platform_driver ixp4xx_spkr_platform_driver = {
  126. .driver = {
  127. .name = "ixp4xx-beeper",
  128. .owner = THIS_MODULE,
  129. },
  130. .probe = ixp4xx_spkr_probe,
  131. .remove = __devexit_p(ixp4xx_spkr_remove),
  132. .shutdown = ixp4xx_spkr_shutdown,
  133. };
  134. module_platform_driver(ixp4xx_spkr_platform_driver);