pwm-beeper.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. unsigned long pwm_id = (unsigned long)dev_get_platdata(&pdev->dev);
  81. struct pwm_beeper *beeper;
  82. int error;
  83. beeper = kzalloc(sizeof(*beeper), GFP_KERNEL);
  84. if (!beeper)
  85. return -ENOMEM;
  86. beeper->pwm = pwm_get(&pdev->dev, NULL);
  87. if (IS_ERR(beeper->pwm)) {
  88. dev_dbg(&pdev->dev, "unable to request PWM, trying legacy API\n");
  89. beeper->pwm = pwm_request(pwm_id, "pwm beeper");
  90. }
  91. if (IS_ERR(beeper->pwm)) {
  92. error = PTR_ERR(beeper->pwm);
  93. dev_err(&pdev->dev, "Failed to request pwm device: %d\n", error);
  94. goto err_free;
  95. }
  96. INIT_WORK(&beeper->work, pwm_beeper_work);
  97. beeper->input = input_allocate_device();
  98. if (!beeper->input) {
  99. dev_err(&pdev->dev, "Failed to allocate input device\n");
  100. error = -ENOMEM;
  101. goto err_pwm_free;
  102. }
  103. beeper->input->dev.parent = &pdev->dev;
  104. beeper->input->name = "pwm-beeper";
  105. beeper->input->phys = "pwm/input0";
  106. beeper->input->id.bustype = BUS_HOST;
  107. beeper->input->id.vendor = 0x001f;
  108. beeper->input->id.product = 0x0001;
  109. beeper->input->id.version = 0x0100;
  110. beeper->input->evbit[0] = BIT(EV_SND);
  111. beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL);
  112. beeper->input->event = pwm_beeper_event;
  113. beeper->input->close = pwm_beeper_close;
  114. input_set_drvdata(beeper->input, beeper);
  115. error = input_register_device(beeper->input);
  116. if (error) {
  117. dev_err(&pdev->dev, "Failed to register input device: %d\n", error);
  118. goto err_input_free;
  119. }
  120. platform_set_drvdata(pdev, beeper);
  121. return 0;
  122. err_input_free:
  123. input_free_device(beeper->input);
  124. err_pwm_free:
  125. pwm_free(beeper->pwm);
  126. err_free:
  127. kfree(beeper);
  128. return error;
  129. }
  130. static int pwm_beeper_remove(struct platform_device *pdev)
  131. {
  132. struct pwm_beeper *beeper = platform_get_drvdata(pdev);
  133. input_unregister_device(beeper->input);
  134. pwm_free(beeper->pwm);
  135. kfree(beeper);
  136. return 0;
  137. }
  138. static int __maybe_unused pwm_beeper_suspend(struct device *dev)
  139. {
  140. struct pwm_beeper *beeper = dev_get_drvdata(dev);
  141. pwm_beeper_stop(beeper);
  142. return 0;
  143. }
  144. static int __maybe_unused pwm_beeper_resume(struct device *dev)
  145. {
  146. struct pwm_beeper *beeper = dev_get_drvdata(dev);
  147. if (beeper->period)
  148. __pwm_beeper_set(beeper);
  149. return 0;
  150. }
  151. static SIMPLE_DEV_PM_OPS(pwm_beeper_pm_ops,
  152. pwm_beeper_suspend, pwm_beeper_resume);
  153. #ifdef CONFIG_OF
  154. static const struct of_device_id pwm_beeper_match[] = {
  155. { .compatible = "pwm-beeper", },
  156. { },
  157. };
  158. MODULE_DEVICE_TABLE(of, pwm_beeper_match);
  159. #endif
  160. static struct platform_driver pwm_beeper_driver = {
  161. .probe = pwm_beeper_probe,
  162. .remove = pwm_beeper_remove,
  163. .driver = {
  164. .name = "pwm-beeper",
  165. .pm = &pwm_beeper_pm_ops,
  166. .of_match_table = of_match_ptr(pwm_beeper_match),
  167. },
  168. };
  169. module_platform_driver(pwm_beeper_driver);
  170. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  171. MODULE_DESCRIPTION("PWM beeper driver");
  172. MODULE_LICENSE("GPL");
  173. MODULE_ALIAS("platform:pwm-beeper");