max8925_onkey.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * MAX8925 ONKEY driver
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  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. dev_err(&pdev->dev, "No IRQ resource!\n");
  65. return -EINVAL;
  66. }
  67. irq[1] = platform_get_irq(pdev, 1);
  68. if (irq[1] < 0) {
  69. dev_err(&pdev->dev, "No IRQ resource!\n");
  70. return -EINVAL;
  71. }
  72. info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_onkey_info),
  73. GFP_KERNEL);
  74. if (!info)
  75. return -ENOMEM;
  76. input = devm_input_allocate_device(&pdev->dev);
  77. if (!input)
  78. return -ENOMEM;
  79. info->idev = input;
  80. info->i2c = chip->i2c;
  81. info->dev = &pdev->dev;
  82. info->irq[0] = irq[0];
  83. info->irq[1] = irq[1];
  84. input->name = "max8925_on";
  85. input->phys = "max8925_on/input0";
  86. input->id.bustype = BUS_I2C;
  87. input->dev.parent = &pdev->dev;
  88. input_set_capability(input, EV_KEY, KEY_POWER);
  89. error = devm_request_threaded_irq(&pdev->dev, irq[0], NULL,
  90. max8925_onkey_handler, IRQF_ONESHOT,
  91. "onkey-down", info);
  92. if (error < 0) {
  93. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  94. irq[0], error);
  95. return error;
  96. }
  97. error = devm_request_threaded_irq(&pdev->dev, irq[1], NULL,
  98. max8925_onkey_handler, IRQF_ONESHOT,
  99. "onkey-up", info);
  100. if (error < 0) {
  101. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  102. irq[1], error);
  103. return error;
  104. }
  105. error = input_register_device(info->idev);
  106. if (error) {
  107. dev_err(chip->dev, "Can't register input device: %d\n", error);
  108. return error;
  109. }
  110. platform_set_drvdata(pdev, info);
  111. device_init_wakeup(&pdev->dev, 1);
  112. return 0;
  113. }
  114. static int __maybe_unused max8925_onkey_suspend(struct device *dev)
  115. {
  116. struct platform_device *pdev = to_platform_device(dev);
  117. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  118. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  119. if (device_may_wakeup(dev)) {
  120. chip->wakeup_flag |= 1 << info->irq[0];
  121. chip->wakeup_flag |= 1 << info->irq[1];
  122. }
  123. return 0;
  124. }
  125. static int __maybe_unused max8925_onkey_resume(struct device *dev)
  126. {
  127. struct platform_device *pdev = to_platform_device(dev);
  128. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  129. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  130. if (device_may_wakeup(dev)) {
  131. chip->wakeup_flag &= ~(1 << info->irq[0]);
  132. chip->wakeup_flag &= ~(1 << info->irq[1]);
  133. }
  134. return 0;
  135. }
  136. static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume);
  137. static struct platform_driver max8925_onkey_driver = {
  138. .driver = {
  139. .name = "max8925-onkey",
  140. .pm = &max8925_onkey_pm_ops,
  141. },
  142. .probe = max8925_onkey_probe,
  143. };
  144. module_platform_driver(max8925_onkey_driver);
  145. MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
  146. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  147. MODULE_LICENSE("GPL");