twl4030-pwrbutton.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * twl4030-pwrbutton.c - TWL4030 Power Button Input Driver
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Written by Peter De Schrijver <peter.de-schrijver@nokia.com>
  7. * Several fixes by Felipe Balbi <felipe.balbi@nokia.com>
  8. *
  9. * This file is subject to the terms and conditions of the GNU General
  10. * Public License. See the file "COPYING" in the main directory of this
  11. * archive for more details.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/input.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/i2c/twl4030.h>
  30. #define PWR_PWRON_IRQ (1 << 0)
  31. #define STS_HW_CONDITIONS 0xf
  32. static irqreturn_t powerbutton_irq(int irq, void *_pwr)
  33. {
  34. struct input_dev *pwr = _pwr;
  35. int err;
  36. u8 value;
  37. #ifdef CONFIG_LOCKDEP
  38. /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
  39. * we don't want and can't tolerate since this is a threaded
  40. * IRQ and can sleep due to the i2c reads it has to issue.
  41. * Although it might be friendlier not to borrow this thread
  42. * context...
  43. */
  44. local_irq_enable();
  45. #endif
  46. err = twl4030_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &value,
  47. STS_HW_CONDITIONS);
  48. if (!err) {
  49. input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ);
  50. input_sync(pwr);
  51. } else {
  52. dev_err(pwr->dev.parent, "twl4030: i2c error %d while reading"
  53. " TWL4030 PM_MASTER STS_HW_CONDITIONS register\n", err);
  54. }
  55. return IRQ_HANDLED;
  56. }
  57. static int __devinit twl4030_pwrbutton_probe(struct platform_device *pdev)
  58. {
  59. struct input_dev *pwr;
  60. int irq = platform_get_irq(pdev, 0);
  61. int err;
  62. pwr = input_allocate_device();
  63. if (!pwr) {
  64. dev_dbg(&pdev->dev, "Can't allocate power button\n");
  65. return -ENOMEM;
  66. }
  67. pwr->evbit[0] = BIT_MASK(EV_KEY);
  68. pwr->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
  69. pwr->name = "twl4030_pwrbutton";
  70. pwr->phys = "twl4030_pwrbutton/input0";
  71. pwr->dev.parent = &pdev->dev;
  72. err = request_irq(irq, powerbutton_irq,
  73. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  74. "twl4030_pwrbutton", pwr);
  75. if (err < 0) {
  76. dev_dbg(&pdev->dev, "Can't get IRQ for pwrbutton: %d\n", err);
  77. goto free_input_dev;
  78. }
  79. err = input_register_device(pwr);
  80. if (err) {
  81. dev_dbg(&pdev->dev, "Can't register power button: %d\n", err);
  82. goto free_irq;
  83. }
  84. platform_set_drvdata(pdev, pwr);
  85. return 0;
  86. free_irq:
  87. free_irq(irq, NULL);
  88. free_input_dev:
  89. input_free_device(pwr);
  90. return err;
  91. }
  92. static int __devexit twl4030_pwrbutton_remove(struct platform_device *pdev)
  93. {
  94. struct input_dev *pwr = platform_get_drvdata(pdev);
  95. int irq = platform_get_irq(pdev, 0);
  96. free_irq(irq, pwr);
  97. input_unregister_device(pwr);
  98. return 0;
  99. }
  100. struct platform_driver twl4030_pwrbutton_driver = {
  101. .probe = twl4030_pwrbutton_probe,
  102. .remove = __devexit_p(twl4030_pwrbutton_remove),
  103. .driver = {
  104. .name = "twl4030_pwrbutton",
  105. .owner = THIS_MODULE,
  106. },
  107. };
  108. static int __init twl4030_pwrbutton_init(void)
  109. {
  110. return platform_driver_register(&twl4030_pwrbutton_driver);
  111. }
  112. module_init(twl4030_pwrbutton_init);
  113. static void __exit twl4030_pwrbutton_exit(void)
  114. {
  115. platform_driver_unregister(&twl4030_pwrbutton_driver);
  116. }
  117. module_exit(twl4030_pwrbutton_exit);
  118. MODULE_ALIAS("platform:twl4030_pwrbutton");
  119. MODULE_DESCRIPTION("Triton2 Power Button");
  120. MODULE_LICENSE("GPL");
  121. MODULE_AUTHOR("Peter De Schrijver <peter.de-schrijver@nokia.com>");
  122. MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");