max8925_onkey.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * MAX8925 ONKEY driver
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <[email protected]>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file "COPYING" in the main directory of this
  9. * archive for more details.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/i2c.h>
  24. #include <linux/input.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/mfd/max8925.h>
  27. #include <linux/slab.h>
  28. #include <linux/device.h>
  29. #define SW_INPUT (1 << 7) /* 0/1 -- up/down */
  30. #define HARDRESET_EN (1 << 7)
  31. #define PWREN_EN (1 << 7)
  32. struct max8925_onkey_info {
  33. struct input_dev *idev;
  34. struct i2c_client *i2c;
  35. struct device *dev;
  36. unsigned int irq[2];
  37. };
  38. /*
  39. * MAX8925 gives us an interrupt when ONKEY is pressed or released.
  40. * max8925_set_bits() operates I2C bus and may sleep. So implement
  41. * it in thread IRQ handler.
  42. */
  43. static irqreturn_t max8925_onkey_handler(int irq, void *data)
  44. {
  45. struct max8925_onkey_info *info = data;
  46. int state;
  47. state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
  48. input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
  49. input_sync(info->idev);
  50. dev_dbg(info->dev, "onkey state:%d\n", state);
  51. /* Enable hardreset to halt if system isn't shutdown on time */
  52. max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
  53. HARDRESET_EN, HARDRESET_EN);
  54. return IRQ_HANDLED;
  55. }
  56. static int max8925_onkey_probe(struct platform_device *pdev)
  57. {
  58. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  59. struct max8925_onkey_info *info;
  60. struct input_dev *input;
  61. int irq[2], error;
  62. irq[0] = platform_get_irq(pdev, 0);
  63. if (irq[0] < 0)
  64. return -EINVAL;
  65. irq[1] = platform_get_irq(pdev, 1);
  66. if (irq[1] < 0)
  67. return -EINVAL;
  68. info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_onkey_info),
  69. GFP_KERNEL);
  70. if (!info)
  71. return -ENOMEM;
  72. input = devm_input_allocate_device(&pdev->dev);
  73. if (!input)
  74. return -ENOMEM;
  75. info->idev = input;
  76. info->i2c = chip->i2c;
  77. info->dev = &pdev->dev;
  78. info->irq[0] = irq[0];
  79. info->irq[1] = irq[1];
  80. input->name = "max8925_on";
  81. input->phys = "max8925_on/input0";
  82. input->id.bustype = BUS_I2C;
  83. input->dev.parent = &pdev->dev;
  84. input_set_capability(input, EV_KEY, KEY_POWER);
  85. error = devm_request_threaded_irq(&pdev->dev, irq[0], NULL,
  86. max8925_onkey_handler, IRQF_ONESHOT,
  87. "onkey-down", info);
  88. if (error < 0) {
  89. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  90. irq[0], error);
  91. return error;
  92. }
  93. error = devm_request_threaded_irq(&pdev->dev, irq[1], NULL,
  94. max8925_onkey_handler, IRQF_ONESHOT,
  95. "onkey-up", info);
  96. if (error < 0) {
  97. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  98. irq[1], error);
  99. return error;
  100. }
  101. error = input_register_device(info->idev);
  102. if (error) {
  103. dev_err(chip->dev, "Can't register input device: %d\n", error);
  104. return error;
  105. }
  106. platform_set_drvdata(pdev, info);
  107. device_init_wakeup(&pdev->dev, 1);
  108. return 0;
  109. }
  110. static int __maybe_unused max8925_onkey_suspend(struct device *dev)
  111. {
  112. struct platform_device *pdev = to_platform_device(dev);
  113. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  114. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  115. if (device_may_wakeup(dev)) {
  116. chip->wakeup_flag |= 1 << info->irq[0];
  117. chip->wakeup_flag |= 1 << info->irq[1];
  118. }
  119. return 0;
  120. }
  121. static int __maybe_unused max8925_onkey_resume(struct device *dev)
  122. {
  123. struct platform_device *pdev = to_platform_device(dev);
  124. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  125. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  126. if (device_may_wakeup(dev)) {
  127. chip->wakeup_flag &= ~(1 << info->irq[0]);
  128. chip->wakeup_flag &= ~(1 << info->irq[1]);
  129. }
  130. return 0;
  131. }
  132. static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume);
  133. static struct platform_driver max8925_onkey_driver = {
  134. .driver = {
  135. .name = "max8925-onkey",
  136. .pm = &max8925_onkey_pm_ops,
  137. },
  138. .probe = max8925_onkey_probe,
  139. };
  140. module_platform_driver(max8925_onkey_driver);
  141. MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
  142. MODULE_AUTHOR("Haojian Zhuang <[email protected]>");
  143. MODULE_LICENSE("GPL");