tps65218-pwrbutton.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Texas Instruments' TPS65217 and TPS65218 Power Button Input Driver
  4. *
  5. * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  6. * Author: Felipe Balbi <[email protected]>
  7. * Author: Marcin Niestroj <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/input.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mfd/tps65217.h>
  14. #include <linux/mfd/tps65218.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. struct tps6521x_data {
  21. unsigned int reg_status;
  22. unsigned int pb_mask;
  23. const char *name;
  24. };
  25. static const struct tps6521x_data tps65217_data = {
  26. .reg_status = TPS65217_REG_STATUS,
  27. .pb_mask = TPS65217_STATUS_PB,
  28. .name = "tps65217_pwrbutton",
  29. };
  30. static const struct tps6521x_data tps65218_data = {
  31. .reg_status = TPS65218_REG_STATUS,
  32. .pb_mask = TPS65218_STATUS_PB_STATE,
  33. .name = "tps65218_pwrbutton",
  34. };
  35. struct tps6521x_pwrbutton {
  36. struct device *dev;
  37. struct regmap *regmap;
  38. struct input_dev *idev;
  39. const struct tps6521x_data *data;
  40. char phys[32];
  41. };
  42. static const struct of_device_id of_tps6521x_pb_match[] = {
  43. { .compatible = "ti,tps65217-pwrbutton", .data = &tps65217_data },
  44. { .compatible = "ti,tps65218-pwrbutton", .data = &tps65218_data },
  45. { },
  46. };
  47. MODULE_DEVICE_TABLE(of, of_tps6521x_pb_match);
  48. static irqreturn_t tps6521x_pb_irq(int irq, void *_pwr)
  49. {
  50. struct tps6521x_pwrbutton *pwr = _pwr;
  51. const struct tps6521x_data *tps_data = pwr->data;
  52. unsigned int reg;
  53. int error;
  54. error = regmap_read(pwr->regmap, tps_data->reg_status, &reg);
  55. if (error) {
  56. dev_err(pwr->dev, "can't read register: %d\n", error);
  57. goto out;
  58. }
  59. if (reg & tps_data->pb_mask) {
  60. input_report_key(pwr->idev, KEY_POWER, 1);
  61. pm_wakeup_event(pwr->dev, 0);
  62. } else {
  63. input_report_key(pwr->idev, KEY_POWER, 0);
  64. }
  65. input_sync(pwr->idev);
  66. out:
  67. return IRQ_HANDLED;
  68. }
  69. static int tps6521x_pb_probe(struct platform_device *pdev)
  70. {
  71. struct device *dev = &pdev->dev;
  72. struct tps6521x_pwrbutton *pwr;
  73. struct input_dev *idev;
  74. const struct of_device_id *match;
  75. int error;
  76. int irq;
  77. match = of_match_node(of_tps6521x_pb_match, dev->of_node);
  78. if (!match)
  79. return -ENXIO;
  80. pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
  81. if (!pwr)
  82. return -ENOMEM;
  83. pwr->data = match->data;
  84. idev = devm_input_allocate_device(dev);
  85. if (!idev)
  86. return -ENOMEM;
  87. idev->name = pwr->data->name;
  88. snprintf(pwr->phys, sizeof(pwr->phys), "%s/input0",
  89. pwr->data->name);
  90. idev->phys = pwr->phys;
  91. idev->dev.parent = dev;
  92. idev->id.bustype = BUS_I2C;
  93. input_set_capability(idev, EV_KEY, KEY_POWER);
  94. pwr->regmap = dev_get_regmap(dev->parent, NULL);
  95. pwr->dev = dev;
  96. pwr->idev = idev;
  97. device_init_wakeup(dev, true);
  98. irq = platform_get_irq(pdev, 0);
  99. if (irq < 0)
  100. return -EINVAL;
  101. error = devm_request_threaded_irq(dev, irq, NULL, tps6521x_pb_irq,
  102. IRQF_TRIGGER_RISING |
  103. IRQF_TRIGGER_FALLING |
  104. IRQF_ONESHOT,
  105. pwr->data->name, pwr);
  106. if (error) {
  107. dev_err(dev, "failed to request IRQ #%d: %d\n", irq, error);
  108. return error;
  109. }
  110. error= input_register_device(idev);
  111. if (error) {
  112. dev_err(dev, "Can't register power button: %d\n", error);
  113. return error;
  114. }
  115. return 0;
  116. }
  117. static const struct platform_device_id tps6521x_pwrbtn_id_table[] = {
  118. { "tps65218-pwrbutton", },
  119. { "tps65217-pwrbutton", },
  120. { /* sentinel */ }
  121. };
  122. MODULE_DEVICE_TABLE(platform, tps6521x_pwrbtn_id_table);
  123. static struct platform_driver tps6521x_pb_driver = {
  124. .probe = tps6521x_pb_probe,
  125. .driver = {
  126. .name = "tps6521x_pwrbutton",
  127. .of_match_table = of_tps6521x_pb_match,
  128. },
  129. .id_table = tps6521x_pwrbtn_id_table,
  130. };
  131. module_platform_driver(tps6521x_pb_driver);
  132. MODULE_DESCRIPTION("TPS6521X Power Button");
  133. MODULE_LICENSE("GPL v2");
  134. MODULE_AUTHOR("Felipe Balbi <[email protected]>");