twl4030-pwrbutton.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/mfd/twl.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. err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &value, STS_HW_CONDITIONS);
  38. if (!err) {
  39. pm_wakeup_event(pwr->dev.parent, 0);
  40. input_report_key(pwr, KEY_POWER, value & PWR_PWRON_IRQ);
  41. input_sync(pwr);
  42. } else {
  43. dev_err(pwr->dev.parent, "twl4030: i2c error %d while reading"
  44. " TWL4030 PM_MASTER STS_HW_CONDITIONS register\n", err);
  45. }
  46. return IRQ_HANDLED;
  47. }
  48. static int twl4030_pwrbutton_probe(struct platform_device *pdev)
  49. {
  50. struct input_dev *pwr;
  51. int irq = platform_get_irq(pdev, 0);
  52. int err;
  53. pwr = devm_input_allocate_device(&pdev->dev);
  54. if (!pwr) {
  55. dev_err(&pdev->dev, "Can't allocate power button\n");
  56. return -ENOMEM;
  57. }
  58. input_set_capability(pwr, EV_KEY, KEY_POWER);
  59. pwr->name = "twl4030_pwrbutton";
  60. pwr->phys = "twl4030_pwrbutton/input0";
  61. pwr->dev.parent = &pdev->dev;
  62. err = devm_request_threaded_irq(&pdev->dev, irq, NULL, powerbutton_irq,
  63. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
  64. IRQF_ONESHOT,
  65. "twl4030_pwrbutton", pwr);
  66. if (err < 0) {
  67. dev_err(&pdev->dev, "Can't get IRQ for pwrbutton: %d\n", err);
  68. return err;
  69. }
  70. err = input_register_device(pwr);
  71. if (err) {
  72. dev_err(&pdev->dev, "Can't register power button: %d\n", err);
  73. return err;
  74. }
  75. device_init_wakeup(&pdev->dev, true);
  76. return 0;
  77. }
  78. #ifdef CONFIG_OF
  79. static const struct of_device_id twl4030_pwrbutton_dt_match_table[] = {
  80. { .compatible = "ti,twl4030-pwrbutton" },
  81. {},
  82. };
  83. MODULE_DEVICE_TABLE(of, twl4030_pwrbutton_dt_match_table);
  84. #endif
  85. static struct platform_driver twl4030_pwrbutton_driver = {
  86. .probe = twl4030_pwrbutton_probe,
  87. .driver = {
  88. .name = "twl4030_pwrbutton",
  89. .of_match_table = of_match_ptr(twl4030_pwrbutton_dt_match_table),
  90. },
  91. };
  92. module_platform_driver(twl4030_pwrbutton_driver);
  93. MODULE_ALIAS("platform:twl4030_pwrbutton");
  94. MODULE_DESCRIPTION("Triton2 Power Button");
  95. MODULE_LICENSE("GPL");
  96. MODULE_AUTHOR("Peter De Schrijver <peter.de-schrijver@nokia.com>");
  97. MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");