wm831x-on.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/input.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/mfd/wm831x/core.h>
  28. struct wm831x_on {
  29. struct input_dev *dev;
  30. struct delayed_work work;
  31. struct wm831x *wm831x;
  32. };
  33. /*
  34. * The chip gives us an interrupt when the ON pin is asserted but we
  35. * then need to poll to see when the pin is deasserted.
  36. */
  37. static void wm831x_poll_on(struct work_struct *work)
  38. {
  39. struct wm831x_on *wm831x_on = container_of(work, struct wm831x_on,
  40. work.work);
  41. struct wm831x *wm831x = wm831x_on->wm831x;
  42. int poll, ret;
  43. ret = wm831x_reg_read(wm831x, WM831X_ON_PIN_CONTROL);
  44. if (ret >= 0) {
  45. poll = !(ret & WM831X_ON_PIN_STS);
  46. input_report_key(wm831x_on->dev, KEY_POWER, poll);
  47. input_sync(wm831x_on->dev);
  48. } else {
  49. dev_err(wm831x->dev, "Failed to read ON status: %d\n", ret);
  50. poll = 1;
  51. }
  52. if (poll)
  53. schedule_delayed_work(&wm831x_on->work, 100);
  54. }
  55. static irqreturn_t wm831x_on_irq(int irq, void *data)
  56. {
  57. struct wm831x_on *wm831x_on = data;
  58. schedule_delayed_work(&wm831x_on->work, 0);
  59. return IRQ_HANDLED;
  60. }
  61. static int __devinit wm831x_on_probe(struct platform_device *pdev)
  62. {
  63. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  64. struct wm831x_on *wm831x_on;
  65. int irq = platform_get_irq(pdev, 0);
  66. int ret;
  67. wm831x_on = kzalloc(sizeof(struct wm831x_on), GFP_KERNEL);
  68. if (!wm831x_on) {
  69. dev_err(&pdev->dev, "Can't allocate data\n");
  70. return -ENOMEM;
  71. }
  72. wm831x_on->wm831x = wm831x;
  73. INIT_DELAYED_WORK(&wm831x_on->work, wm831x_poll_on);
  74. wm831x_on->dev = input_allocate_device();
  75. if (!wm831x_on->dev) {
  76. dev_err(&pdev->dev, "Can't allocate input dev\n");
  77. ret = -ENOMEM;
  78. goto err;
  79. }
  80. wm831x_on->dev->evbit[0] = BIT_MASK(EV_KEY);
  81. wm831x_on->dev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
  82. wm831x_on->dev->name = "wm831x_on";
  83. wm831x_on->dev->phys = "wm831x_on/input0";
  84. wm831x_on->dev->dev.parent = &pdev->dev;
  85. ret = wm831x_request_irq(wm831x, irq, wm831x_on_irq,
  86. IRQF_TRIGGER_RISING, "wm831x_on", wm831x_on);
  87. if (ret < 0) {
  88. dev_err(&pdev->dev, "Unable to request IRQ: %d\n", ret);
  89. goto err_input_dev;
  90. }
  91. ret = input_register_device(wm831x_on->dev);
  92. if (ret) {
  93. dev_dbg(&pdev->dev, "Can't register input device: %d\n", ret);
  94. goto err_irq;
  95. }
  96. platform_set_drvdata(pdev, wm831x_on);
  97. return 0;
  98. err_irq:
  99. wm831x_free_irq(wm831x, irq, NULL);
  100. err_input_dev:
  101. input_free_device(wm831x_on->dev);
  102. err:
  103. kfree(wm831x_on);
  104. return ret;
  105. }
  106. static int __devexit wm831x_on_remove(struct platform_device *pdev)
  107. {
  108. struct wm831x_on *wm831x_on = platform_get_drvdata(pdev);
  109. int irq = platform_get_irq(pdev, 0);
  110. wm831x_free_irq(wm831x_on->wm831x, irq, wm831x_on);
  111. cancel_delayed_work_sync(&wm831x_on->work);
  112. input_unregister_device(wm831x_on->dev);
  113. kfree(wm831x_on);
  114. return 0;
  115. }
  116. static struct platform_driver wm831x_on_driver = {
  117. .probe = wm831x_on_probe,
  118. .remove = __devexit_p(wm831x_on_remove),
  119. .driver = {
  120. .name = "wm831x-on",
  121. .owner = THIS_MODULE,
  122. },
  123. };
  124. static int __init wm831x_on_init(void)
  125. {
  126. return platform_driver_register(&wm831x_on_driver);
  127. }
  128. module_init(wm831x_on_init);
  129. static void __exit wm831x_on_exit(void)
  130. {
  131. platform_driver_unregister(&wm831x_on_driver);
  132. }
  133. module_exit(wm831x_on_exit);
  134. MODULE_ALIAS("platform:wm831x-on");
  135. MODULE_DESCRIPTION("WM831x ON pin");
  136. MODULE_LICENSE("GPL");
  137. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");