wm831x-on.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * wm831x-on.c - WM831X ON pin driver
  3. *
  4. * Copyright (C) 2009 Wolfson Microelectronics plc
  5. *
  6. * This file is subject to the terms and conditions of the GNU General
  7. * Public License. See the file "COPYING" in the main directory of this
  8. * archive for more details.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/input.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/mfd/wm831x/core.h>
  29. struct wm831x_on {
  30. struct input_dev *dev;
  31. struct delayed_work work;
  32. struct wm831x *wm831x;
  33. };
  34. /*
  35. * The chip gives us an interrupt when the ON pin is asserted but we
  36. * then need to poll to see when the pin is deasserted.
  37. */
  38. static void wm831x_poll_on(struct work_struct *work)
  39. {
  40. struct wm831x_on *wm831x_on = container_of(work, struct wm831x_on,
  41. work.work);
  42. struct wm831x *wm831x = wm831x_on->wm831x;
  43. int poll, ret;
  44. ret = wm831x_reg_read(wm831x, WM831X_ON_PIN_CONTROL);
  45. if (ret >= 0) {
  46. poll = !(ret & WM831X_ON_PIN_STS);
  47. input_report_key(wm831x_on->dev, KEY_POWER, poll);
  48. input_sync(wm831x_on->dev);
  49. } else {
  50. dev_err(wm831x->dev, "Failed to read ON status: %d\n", ret);
  51. poll = 1;
  52. }
  53. if (poll)
  54. schedule_delayed_work(&wm831x_on->work, 100);
  55. }
  56. static irqreturn_t wm831x_on_irq(int irq, void *data)
  57. {
  58. struct wm831x_on *wm831x_on = data;
  59. schedule_delayed_work(&wm831x_on->work, 0);
  60. return IRQ_HANDLED;
  61. }
  62. static int __devinit wm831x_on_probe(struct platform_device *pdev)
  63. {
  64. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  65. struct wm831x_on *wm831x_on;
  66. int irq = platform_get_irq(pdev, 0);
  67. int ret;
  68. wm831x_on = kzalloc(sizeof(struct wm831x_on), GFP_KERNEL);
  69. if (!wm831x_on) {
  70. dev_err(&pdev->dev, "Can't allocate data\n");
  71. return -ENOMEM;
  72. }
  73. wm831x_on->wm831x = wm831x;
  74. INIT_DELAYED_WORK(&wm831x_on->work, wm831x_poll_on);
  75. wm831x_on->dev = input_allocate_device();
  76. if (!wm831x_on->dev) {
  77. dev_err(&pdev->dev, "Can't allocate input dev\n");
  78. ret = -ENOMEM;
  79. goto err;
  80. }
  81. wm831x_on->dev->evbit[0] = BIT_MASK(EV_KEY);
  82. wm831x_on->dev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
  83. wm831x_on->dev->name = "wm831x_on";
  84. wm831x_on->dev->phys = "wm831x_on/input0";
  85. wm831x_on->dev->dev.parent = &pdev->dev;
  86. ret = request_threaded_irq(irq, NULL, wm831x_on_irq,
  87. IRQF_TRIGGER_RISING, "wm831x_on",
  88. wm831x_on);
  89. if (ret < 0) {
  90. dev_err(&pdev->dev, "Unable to request IRQ: %d\n", ret);
  91. goto err_input_dev;
  92. }
  93. ret = input_register_device(wm831x_on->dev);
  94. if (ret) {
  95. dev_dbg(&pdev->dev, "Can't register input device: %d\n", ret);
  96. goto err_irq;
  97. }
  98. platform_set_drvdata(pdev, wm831x_on);
  99. return 0;
  100. err_irq:
  101. free_irq(irq, wm831x_on);
  102. err_input_dev:
  103. input_free_device(wm831x_on->dev);
  104. err:
  105. kfree(wm831x_on);
  106. return ret;
  107. }
  108. static int __devexit wm831x_on_remove(struct platform_device *pdev)
  109. {
  110. struct wm831x_on *wm831x_on = platform_get_drvdata(pdev);
  111. int irq = platform_get_irq(pdev, 0);
  112. free_irq(irq, wm831x_on);
  113. cancel_delayed_work_sync(&wm831x_on->work);
  114. input_unregister_device(wm831x_on->dev);
  115. kfree(wm831x_on);
  116. return 0;
  117. }
  118. static struct platform_driver wm831x_on_driver = {
  119. .probe = wm831x_on_probe,
  120. .remove = __devexit_p(wm831x_on_remove),
  121. .driver = {
  122. .name = "wm831x-on",
  123. .owner = THIS_MODULE,
  124. },
  125. };
  126. module_platform_driver(wm831x_on_driver);
  127. MODULE_ALIAS("platform:wm831x-on");
  128. MODULE_DESCRIPTION("WM831x ON pin");
  129. MODULE_LICENSE("GPL");
  130. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");