tps65218-pwrbutton.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Texas Instruments' TPS65217 and 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. * Author: Marcin Niestroj <m.niestroj@grinn-global.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/input.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mfd/tps65217.h>
  22. #include <linux/mfd/tps65218.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regmap.h>
  27. #include <linux/slab.h>
  28. struct tps6521x_data {
  29. unsigned int reg_status;
  30. unsigned int pb_mask;
  31. const char *name;
  32. };
  33. static const struct tps6521x_data tps65217_data = {
  34. .reg_status = TPS65217_REG_STATUS,
  35. .pb_mask = TPS65217_STATUS_PB,
  36. .name = "tps65217_pwrbutton",
  37. };
  38. static const struct tps6521x_data tps65218_data = {
  39. .reg_status = TPS65218_REG_STATUS,
  40. .pb_mask = TPS65218_STATUS_PB_STATE,
  41. .name = "tps65218_pwrbutton",
  42. };
  43. struct tps6521x_pwrbutton {
  44. struct device *dev;
  45. struct regmap *regmap;
  46. struct input_dev *idev;
  47. const struct tps6521x_data *data;
  48. char phys[32];
  49. };
  50. static const struct of_device_id of_tps6521x_pb_match[] = {
  51. { .compatible = "ti,tps65217-pwrbutton", .data = &tps65217_data },
  52. { .compatible = "ti,tps65218-pwrbutton", .data = &tps65218_data },
  53. { },
  54. };
  55. MODULE_DEVICE_TABLE(of, of_tps6521x_pb_match);
  56. static irqreturn_t tps6521x_pb_irq(int irq, void *_pwr)
  57. {
  58. struct tps6521x_pwrbutton *pwr = _pwr;
  59. const struct tps6521x_data *tps_data = pwr->data;
  60. unsigned int reg;
  61. int error;
  62. error = regmap_read(pwr->regmap, tps_data->reg_status, &reg);
  63. if (error) {
  64. dev_err(pwr->dev, "can't read register: %d\n", error);
  65. goto out;
  66. }
  67. if (reg & tps_data->pb_mask) {
  68. input_report_key(pwr->idev, KEY_POWER, 1);
  69. pm_wakeup_event(pwr->dev, 0);
  70. } else {
  71. input_report_key(pwr->idev, KEY_POWER, 0);
  72. }
  73. input_sync(pwr->idev);
  74. out:
  75. return IRQ_HANDLED;
  76. }
  77. static int tps6521x_pb_probe(struct platform_device *pdev)
  78. {
  79. struct device *dev = &pdev->dev;
  80. struct tps6521x_pwrbutton *pwr;
  81. struct input_dev *idev;
  82. const struct of_device_id *match;
  83. int error;
  84. int irq;
  85. match = of_match_node(of_tps6521x_pb_match, dev->of_node);
  86. if (!match)
  87. return -ENXIO;
  88. pwr = devm_kzalloc(dev, sizeof(*pwr), GFP_KERNEL);
  89. if (!pwr)
  90. return -ENOMEM;
  91. pwr->data = match->data;
  92. idev = devm_input_allocate_device(dev);
  93. if (!idev)
  94. return -ENOMEM;
  95. idev->name = pwr->data->name;
  96. snprintf(pwr->phys, sizeof(pwr->phys), "%s/input0",
  97. pwr->data->name);
  98. idev->phys = pwr->phys;
  99. idev->dev.parent = dev;
  100. idev->id.bustype = BUS_I2C;
  101. input_set_capability(idev, EV_KEY, KEY_POWER);
  102. pwr->regmap = dev_get_regmap(dev->parent, NULL);
  103. pwr->dev = dev;
  104. pwr->idev = idev;
  105. device_init_wakeup(dev, true);
  106. irq = platform_get_irq(pdev, 0);
  107. if (irq < 0) {
  108. dev_err(dev, "No IRQ resource!\n");
  109. return -EINVAL;
  110. }
  111. error = devm_request_threaded_irq(dev, irq, NULL, tps6521x_pb_irq,
  112. IRQF_TRIGGER_RISING |
  113. IRQF_TRIGGER_FALLING |
  114. IRQF_ONESHOT,
  115. pwr->data->name, pwr);
  116. if (error) {
  117. dev_err(dev, "failed to request IRQ #%d: %d\n", irq, error);
  118. return error;
  119. }
  120. error= input_register_device(idev);
  121. if (error) {
  122. dev_err(dev, "Can't register power button: %d\n", error);
  123. return error;
  124. }
  125. return 0;
  126. }
  127. static const struct platform_device_id tps6521x_pwrbtn_id_table[] = {
  128. { "tps65218-pwrbutton", },
  129. { "tps65217-pwrbutton", },
  130. { /* sentinel */ }
  131. };
  132. MODULE_DEVICE_TABLE(platform, tps6521x_pwrbtn_id_table);
  133. static struct platform_driver tps6521x_pb_driver = {
  134. .probe = tps6521x_pb_probe,
  135. .driver = {
  136. .name = "tps6521x_pwrbutton",
  137. .of_match_table = of_tps6521x_pb_match,
  138. },
  139. .id_table = tps6521x_pwrbtn_id_table,
  140. };
  141. module_platform_driver(tps6521x_pb_driver);
  142. MODULE_DESCRIPTION("TPS6521X Power Button");
  143. MODULE_LICENSE("GPL v2");
  144. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");