max8925_onkey.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #define SW_INPUT (1 << 7) /* 0/1 -- up/down */
  29. #define HARDRESET_EN (1 << 7)
  30. #define PWREN_EN (1 << 7)
  31. struct max8925_onkey_info {
  32. struct input_dev *idev;
  33. struct i2c_client *i2c;
  34. struct device *dev;
  35. unsigned int irq[2];
  36. };
  37. /*
  38. * MAX8925 gives us an interrupt when ONKEY is pressed or released.
  39. * max8925_set_bits() operates I2C bus and may sleep. So implement
  40. * it in thread IRQ handler.
  41. */
  42. static irqreturn_t max8925_onkey_handler(int irq, void *data)
  43. {
  44. struct max8925_onkey_info *info = data;
  45. int state;
  46. state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS);
  47. input_report_key(info->idev, KEY_POWER, state & SW_INPUT);
  48. input_sync(info->idev);
  49. dev_dbg(info->dev, "onkey state:%d\n", state);
  50. /* Enable hardreset to halt if system isn't shutdown on time */
  51. max8925_set_bits(info->i2c, MAX8925_SYSENSEL,
  52. HARDRESET_EN, HARDRESET_EN);
  53. return IRQ_HANDLED;
  54. }
  55. static int __devinit max8925_onkey_probe(struct platform_device *pdev)
  56. {
  57. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  58. struct max8925_onkey_info *info;
  59. struct input_dev *input;
  60. int irq[2], error;
  61. irq[0] = platform_get_irq(pdev, 0);
  62. if (irq[0] < 0) {
  63. dev_err(&pdev->dev, "No IRQ resource!\n");
  64. return -EINVAL;
  65. }
  66. irq[1] = platform_get_irq(pdev, 1);
  67. if (irq[1] < 0) {
  68. dev_err(&pdev->dev, "No IRQ resource!\n");
  69. return -EINVAL;
  70. }
  71. info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL);
  72. input = input_allocate_device();
  73. if (!info || !input) {
  74. error = -ENOMEM;
  75. goto err_free_mem;
  76. }
  77. info->idev = input;
  78. info->i2c = chip->i2c;
  79. info->dev = &pdev->dev;
  80. info->irq[0] = irq[0];
  81. info->irq[1] = irq[1];
  82. input->name = "max8925_on";
  83. input->phys = "max8925_on/input0";
  84. input->id.bustype = BUS_I2C;
  85. input->dev.parent = &pdev->dev;
  86. input_set_capability(input, EV_KEY, KEY_POWER);
  87. irq[0] += chip->irq_base;
  88. irq[1] += chip->irq_base;
  89. error = request_threaded_irq(irq[0], NULL, max8925_onkey_handler,
  90. IRQF_ONESHOT, "onkey-down", info);
  91. if (error < 0) {
  92. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  93. irq[0], error);
  94. goto err_free_mem;
  95. }
  96. error = request_threaded_irq(irq[1], NULL, max8925_onkey_handler,
  97. IRQF_ONESHOT, "onkey-up", info);
  98. if (error < 0) {
  99. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  100. irq[1], error);
  101. goto err_free_irq0;
  102. }
  103. error = input_register_device(info->idev);
  104. if (error) {
  105. dev_err(chip->dev, "Can't register input device: %d\n", error);
  106. goto err_free_irq1;
  107. }
  108. platform_set_drvdata(pdev, info);
  109. device_init_wakeup(&pdev->dev, 1);
  110. return 0;
  111. err_free_irq1:
  112. free_irq(irq[1], info);
  113. err_free_irq0:
  114. free_irq(irq[0], info);
  115. err_free_mem:
  116. input_free_device(input);
  117. kfree(info);
  118. return error;
  119. }
  120. static int __devexit max8925_onkey_remove(struct platform_device *pdev)
  121. {
  122. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  123. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  124. free_irq(info->irq[0] + chip->irq_base, info);
  125. free_irq(info->irq[1] + chip->irq_base, info);
  126. input_unregister_device(info->idev);
  127. kfree(info);
  128. platform_set_drvdata(pdev, NULL);
  129. return 0;
  130. }
  131. #ifdef CONFIG_PM_SLEEP
  132. static int max8925_onkey_suspend(struct device *dev)
  133. {
  134. struct platform_device *pdev = to_platform_device(dev);
  135. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  136. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  137. if (device_may_wakeup(dev)) {
  138. chip->wakeup_flag |= 1 << info->irq[0];
  139. chip->wakeup_flag |= 1 << info->irq[1];
  140. }
  141. return 0;
  142. }
  143. static int max8925_onkey_resume(struct device *dev)
  144. {
  145. struct platform_device *pdev = to_platform_device(dev);
  146. struct max8925_onkey_info *info = platform_get_drvdata(pdev);
  147. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  148. if (device_may_wakeup(dev)) {
  149. chip->wakeup_flag &= ~(1 << info->irq[0]);
  150. chip->wakeup_flag &= ~(1 << info->irq[1]);
  151. }
  152. return 0;
  153. }
  154. #endif
  155. static SIMPLE_DEV_PM_OPS(max8925_onkey_pm_ops, max8925_onkey_suspend, max8925_onkey_resume);
  156. static struct platform_driver max8925_onkey_driver = {
  157. .driver = {
  158. .name = "max8925-onkey",
  159. .owner = THIS_MODULE,
  160. .pm = &max8925_onkey_pm_ops,
  161. },
  162. .probe = max8925_onkey_probe,
  163. .remove = __devexit_p(max8925_onkey_remove),
  164. };
  165. module_platform_driver(max8925_onkey_driver);
  166. MODULE_DESCRIPTION("Maxim MAX8925 ONKEY driver");
  167. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  168. MODULE_LICENSE("GPL");