tps65218-pwrbutton.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Texas Instruments' TPS65218 Power Button Input Driver
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  5. * Author: Felipe Balbi <balbi@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/input.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mfd/tps65218.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. struct tps65218_pwrbutton {
  26. struct device *dev;
  27. struct tps65218 *tps;
  28. struct input_dev *idev;
  29. };
  30. static irqreturn_t tps65218_pwr_irq(int irq, void *_pwr)
  31. {
  32. struct tps65218_pwrbutton *pwr = _pwr;
  33. unsigned int reg;
  34. int error;
  35. error = tps65218_reg_read(pwr->tps, TPS65218_REG_STATUS, &reg);
  36. if (error) {
  37. dev_err(pwr->dev, "can't read register: %d\n", error);
  38. goto out;
  39. }
  40. if (reg & TPS65218_STATUS_PB_STATE) {
  41. input_report_key(pwr->idev, KEY_POWER, 1);
  42. pm_wakeup_event(pwr->dev, 0);
  43. } else {
  44. input_report_key(pwr->idev, KEY_POWER, 0);
  45. }
  46. input_sync(pwr->idev);
  47. out:
  48. return IRQ_HANDLED;
  49. }
  50. static int tps65218_pwron_probe(struct platform_device *pdev)
  51. {
  52. struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
  53. struct device *dev = &pdev->dev;
  54. struct tps65218_pwrbutton *pwr;
  55. struct input_dev *idev;
  56. int error;
  57. int irq;
  58. pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
  59. if (!pwr)
  60. return -ENOMEM;
  61. idev = devm_input_allocate_device(dev);
  62. if (!idev)
  63. return -ENOMEM;
  64. idev->name = "tps65218_pwrbutton";
  65. idev->phys = "tps65218_pwrbutton/input0";
  66. idev->dev.parent = dev;
  67. idev->id.bustype = BUS_I2C;
  68. input_set_capability(idev, EV_KEY, KEY_POWER);
  69. pwr->tps = tps;
  70. pwr->dev = dev;
  71. pwr->idev = idev;
  72. platform_set_drvdata(pdev, pwr);
  73. device_init_wakeup(dev, true);
  74. irq = platform_get_irq(pdev, 0);
  75. error = devm_request_threaded_irq(dev, irq, NULL, tps65218_pwr_irq,
  76. IRQF_TRIGGER_RISING |
  77. IRQF_TRIGGER_FALLING |
  78. IRQF_ONESHOT,
  79. "tps65218-pwrbutton", pwr);
  80. if (error) {
  81. dev_err(dev, "failed to request IRQ #%d: %d\n",
  82. irq, error);
  83. return error;
  84. }
  85. error= input_register_device(idev);
  86. if (error) {
  87. dev_err(dev, "Can't register power button: %d\n", error);
  88. return error;
  89. }
  90. return 0;
  91. }
  92. static const struct of_device_id of_tps65218_pwr_match[] = {
  93. { .compatible = "ti,tps65218-pwrbutton" },
  94. { },
  95. };
  96. MODULE_DEVICE_TABLE(of, of_tps65218_pwr_match);
  97. static struct platform_driver tps65218_pwron_driver = {
  98. .probe = tps65218_pwron_probe,
  99. .driver = {
  100. .name = "tps65218_pwrbutton",
  101. .of_match_table = of_tps65218_pwr_match,
  102. },
  103. };
  104. module_platform_driver(tps65218_pwron_driver);
  105. MODULE_DESCRIPTION("TPS65218 Power Button");
  106. MODULE_LICENSE("GPL v2");
  107. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");