pwm-beeper.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * PWM beeper driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/input.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pwm.h>
  21. #include <linux/slab.h>
  22. #include <linux/workqueue.h>
  23. struct pwm_beeper {
  24. struct input_dev *input;
  25. struct pwm_device *pwm;
  26. struct work_struct work;
  27. unsigned long period;
  28. };
  29. #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
  30. static void __pwm_beeper_set(struct pwm_beeper *beeper)
  31. {
  32. unsigned long period = beeper->period;
  33. if (period) {
  34. pwm_config(beeper->pwm, period / 2, period);
  35. pwm_enable(beeper->pwm);
  36. } else
  37. pwm_disable(beeper->pwm);
  38. }
  39. static void pwm_beeper_work(struct work_struct *work)
  40. {
  41. struct pwm_beeper *beeper =
  42. container_of(work, struct pwm_beeper, work);
  43. __pwm_beeper_set(beeper);
  44. }
  45. static int pwm_beeper_event(struct input_dev *input,
  46. unsigned int type, unsigned int code, int value)
  47. {
  48. struct pwm_beeper *beeper = input_get_drvdata(input);
  49. if (type != EV_SND || value < 0)
  50. return -EINVAL;
  51. switch (code) {
  52. case SND_BELL:
  53. value = value ? 1000 : 0;
  54. break;
  55. case SND_TONE:
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. if (value == 0)
  61. beeper->period = 0;
  62. else
  63. beeper->period = HZ_TO_NANOSECONDS(value);
  64. schedule_work(&beeper->work);
  65. return 0;
  66. }
  67. static void pwm_beeper_stop(struct pwm_beeper *beeper)
  68. {
  69. cancel_work_sync(&beeper->work);
  70. if (beeper->period)
  71. pwm_disable(beeper->pwm);
  72. }
  73. static void pwm_beeper_close(struct input_dev *input)
  74. {
  75. struct pwm_beeper *beeper = input_get_drvdata(input);
  76. pwm_beeper_stop(beeper);
  77. }
  78. static int pwm_beeper_probe(struct platform_device *pdev)
  79. {
  80. struct device *dev = &pdev->dev;
  81. struct pwm_beeper *beeper;
  82. int error;
  83. beeper = devm_kzalloc(dev, sizeof(*beeper), GFP_KERNEL);
  84. if (!beeper)
  85. return -ENOMEM;
  86. beeper->pwm = devm_pwm_get(dev, NULL);
  87. if (IS_ERR(beeper->pwm)) {
  88. error = PTR_ERR(beeper->pwm);
  89. dev_err(dev, "Failed to request PWM device: %d\n", error);
  90. return error;
  91. }
  92. /*
  93. * FIXME: pwm_apply_args() should be removed when switching to
  94. * the atomic PWM API.
  95. */
  96. pwm_apply_args(beeper->pwm);
  97. INIT_WORK(&beeper->work, pwm_beeper_work);
  98. beeper->input = devm_input_allocate_device(dev);
  99. if (!beeper->input) {
  100. dev_err(dev, "Failed to allocate input device\n");
  101. return -ENOMEM;
  102. }
  103. beeper->input->name = "pwm-beeper";
  104. beeper->input->phys = "pwm/input0";
  105. beeper->input->id.bustype = BUS_HOST;
  106. beeper->input->id.vendor = 0x001f;
  107. beeper->input->id.product = 0x0001;
  108. beeper->input->id.version = 0x0100;
  109. input_set_capability(beeper->input, EV_SND, SND_TONE);
  110. input_set_capability(beeper->input, EV_SND, SND_BELL);
  111. beeper->input->event = pwm_beeper_event;
  112. beeper->input->close = pwm_beeper_close;
  113. input_set_drvdata(beeper->input, beeper);
  114. error = input_register_device(beeper->input);
  115. if (error) {
  116. dev_err(dev, "Failed to register input device: %d\n", error);
  117. return error;
  118. }
  119. platform_set_drvdata(pdev, beeper);
  120. return 0;
  121. }
  122. static int __maybe_unused pwm_beeper_suspend(struct device *dev)
  123. {
  124. struct pwm_beeper *beeper = dev_get_drvdata(dev);
  125. pwm_beeper_stop(beeper);
  126. return 0;
  127. }
  128. static int __maybe_unused pwm_beeper_resume(struct device *dev)
  129. {
  130. struct pwm_beeper *beeper = dev_get_drvdata(dev);
  131. if (beeper->period)
  132. __pwm_beeper_set(beeper);
  133. return 0;
  134. }
  135. static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
  136. pwm_beeper_suspend, pwm_beeper_resume);
  137. #ifdef CONFIG_OF
  138. static const struct of_device_id pwm_beeper_match[] = {
  139. { .compatible = "pwm-beeper", },
  140. { },
  141. };
  142. MODULE_DEVICE_TABLE(of, pwm_beeper_match);
  143. #endif
  144. static struct platform_driver pwm_beeper_driver = {
  145. .probe = pwm_beeper_probe,
  146. .driver = {
  147. .name = "pwm-beeper",
  148. .pm = &pwm_beeper_pm_ops,
  149. .of_match_table = of_match_ptr(pwm_beeper_match),
  150. },
  151. };
  152. module_platform_driver(pwm_beeper_driver);
  153. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  154. MODULE_DESCRIPTION("PWM beeper driver");
  155. MODULE_LICENSE("GPL");
  156. MODULE_ALIAS("platform:pwm-beeper");