max8925_onkey.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * max8925_onkey.c - 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. #define HARDRESET_EN (1 << 7)
  29. #define PWREN_EN (1 << 7)
  30. struct max8925_onkey_info {
  31. struct input_dev *idev;
  32. struct i2c_client *i2c;
  33. int irq;
  34. };
  35. /*
  36. * MAX8925 gives us an interrupt when ONKEY is held for 3 seconds.
  37. * max8925_set_bits() operates I2C bus and may sleep. So implement
  38. * it in thread IRQ handler.
  39. */
  40. static irqreturn_t max8925_onkey_handler(int irq, void *data)
  41. {
  42. struct max8925_onkey_info *info = data;
  43. input_report_key(info->idev, KEY_POWER, 1);
  44. input_sync(info->idev);
  45. /* Enable hardreset to halt if system isn't shutdown on time */
  46. max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
  47. HARDRESET_EN, HARDRESET_EN);
  48. return IRQ_HANDLED;
  49. }
  50. static int __devinit max8925_onkey_probe(struct platform_device *pdev)
  51. {
  52. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  53. struct max8925_onkey_info *info;
  54. int error;
  55. info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL);
  56. if (!info)
  57. return -ENOMEM;
  58. info->i2c = chip->i2c;
  59. info->irq = chip->irq_base + MAX8925_IRQ_GPM_SW_3SEC;
  60. info->idev = input_allocate_device();
  61. if (!info->idev) {
  62. dev_err(chip->dev, "Failed to allocate input dev\n");
  63. error = -ENOMEM;
  64. goto out_input;
  65. }
  66. info->idev->name = "max8925_on";
  67. info->idev->phys = "max8925_on/input0";
  68. info->idev->id.bustype = BUS_I2C;
  69. info->idev->dev.parent = &pdev->dev;
  70. info->idev->evbit[0] = BIT_MASK(EV_KEY);
  71. info->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
  72. error = request_threaded_irq(info->irq, NULL, max8925_onkey_handler,
  73. IRQF_ONESHOT, "onkey", info);
  74. if (error < 0) {
  75. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  76. info->irq, error);
  77. goto out_irq;
  78. }
  79. error = input_register_device(info->idev);
  80. if (error) {
  81. dev_err(chip->dev, "Can't register input device: %d\n", error);
  82. goto out;
  83. }
  84. platform_set_drvdata(pdev, info);
  85. return 0;
  86. out:
  87. free_irq(info->irq, info);
  88. out_irq:
  89. input_free_device(info->idev);
  90. out_input:
  91. kfree(info);
  92. return error;
  93. }
  94. static int __devexit max8925_onkey_remove(struct platform_device *pdev)
  95. {
  96. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  97. free_irq(info->irq, info);
  98. input_unregister_device(info->idev);
  99. kfree(info);
  100. platform_set_drvdata(pdev, NULL);
  101. return 0;
  102. }
  103. static struct platform_driver max8925_onkey_driver = {
  104. .driver = {
  105. .name = "max8925-onkey",
  106. .owner = THIS_MODULE,
  107. },
  108. .probe = max8925_onkey_probe,
  109. .remove = __devexit_p(max8925_onkey_remove),
  110. };
  111. static int __init max8925_onkey_init(void)
  112. {
  113. return platform_driver_register(&max8925_onkey_driver);
  114. }
  115. module_init(max8925_onkey_init);
  116. static void __exit max8925_onkey_exit(void)
  117. {
  118. platform_driver_unregister(&max8925_onkey_driver);
  119. }
  120. module_exit(max8925_onkey_exit);
  121. MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
  122. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  123. MODULE_LICENSE("GPL");