88pm860x_onkey.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * 88pm860x_onkey.c - Marvell 88PM860x ONKEY driver
  3. *
  4. * Copyright (C) 2009-2010 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/88pm860x.h>
  27. #include <linux/slab.h>
  28. #define PM8607_WAKEUP 0x0b
  29. #define LONG_ONKEY_EN (1 << 1)
  30. #define ONKEY_STATUS (1 << 0)
  31. struct pm860x_onkey_info {
  32. struct input_dev *idev;
  33. struct pm860x_chip *chip;
  34. struct i2c_client *i2c;
  35. struct device *dev;
  36. int irq;
  37. };
  38. /* 88PM860x gives us an interrupt when ONKEY is held */
  39. static irqreturn_t pm860x_onkey_handler(int irq, void *data)
  40. {
  41. struct pm860x_onkey_info *info = data;
  42. int ret;
  43. ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
  44. ret &= ONKEY_STATUS;
  45. input_report_key(info->idev, KEY_POWER, ret);
  46. input_sync(info->idev);
  47. /* Enable 8-second long onkey detection */
  48. pm860x_set_bits(info->i2c, PM8607_WAKEUP, 3, LONG_ONKEY_EN);
  49. return IRQ_HANDLED;
  50. }
  51. static int __devinit pm860x_onkey_probe(struct platform_device *pdev)
  52. {
  53. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  54. struct pm860x_onkey_info *info;
  55. int irq, ret;
  56. irq = platform_get_irq(pdev, 0);
  57. if (irq < 0) {
  58. dev_err(&pdev->dev, "No IRQ resource!\n");
  59. return -EINVAL;
  60. }
  61. info = kzalloc(sizeof(struct pm860x_onkey_info), GFP_KERNEL);
  62. if (!info)
  63. return -ENOMEM;
  64. info->chip = chip;
  65. info->i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  66. info->dev = &pdev->dev;
  67. info->irq = irq;
  68. info->idev = input_allocate_device();
  69. if (!info->idev) {
  70. dev_err(chip->dev, "Failed to allocate input dev\n");
  71. ret = -ENOMEM;
  72. goto out;
  73. }
  74. info->idev->name = "88pm860x_on";
  75. info->idev->phys = "88pm860x_on/input0";
  76. info->idev->id.bustype = BUS_I2C;
  77. info->idev->dev.parent = &pdev->dev;
  78. info->idev->evbit[0] = BIT_MASK(EV_KEY);
  79. info->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
  80. ret = input_register_device(info->idev);
  81. if (ret) {
  82. dev_err(chip->dev, "Can't register input device: %d\n", ret);
  83. goto out_reg;
  84. }
  85. ret = request_threaded_irq(info->irq, NULL, pm860x_onkey_handler,
  86. IRQF_ONESHOT, "onkey", info);
  87. if (ret < 0) {
  88. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  89. info->irq, ret);
  90. goto out_irq;
  91. }
  92. platform_set_drvdata(pdev, info);
  93. device_init_wakeup(&pdev->dev, 1);
  94. return 0;
  95. out_irq:
  96. input_unregister_device(info->idev);
  97. kfree(info);
  98. return ret;
  99. out_reg:
  100. input_free_device(info->idev);
  101. out:
  102. kfree(info);
  103. return ret;
  104. }
  105. static int __devexit pm860x_onkey_remove(struct platform_device *pdev)
  106. {
  107. struct pm860x_onkey_info *info = platform_get_drvdata(pdev);
  108. free_irq(info->irq, info);
  109. input_unregister_device(info->idev);
  110. kfree(info);
  111. return 0;
  112. }
  113. #ifdef CONFIG_PM_SLEEP
  114. static int pm860x_onkey_suspend(struct device *dev)
  115. {
  116. struct platform_device *pdev = to_platform_device(dev);
  117. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  118. if (device_may_wakeup(dev))
  119. chip->wakeup_flag |= 1 << PM8607_IRQ_ONKEY;
  120. return 0;
  121. }
  122. static int pm860x_onkey_resume(struct device *dev)
  123. {
  124. struct platform_device *pdev = to_platform_device(dev);
  125. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  126. if (device_may_wakeup(dev))
  127. chip->wakeup_flag &= ~(1 << PM8607_IRQ_ONKEY);
  128. return 0;
  129. }
  130. #endif
  131. static SIMPLE_DEV_PM_OPS(pm860x_onkey_pm_ops, pm860x_onkey_suspend, pm860x_onkey_resume);
  132. static struct platform_driver pm860x_onkey_driver = {
  133. .driver = {
  134. .name = "88pm860x-onkey",
  135. .owner = THIS_MODULE,
  136. .pm = &pm860x_onkey_pm_ops,
  137. },
  138. .probe = pm860x_onkey_probe,
  139. .remove = __devexit_p(pm860x_onkey_remove),
  140. };
  141. module_platform_driver(pm860x_onkey_driver);
  142. MODULE_DESCRIPTION("Marvell 88PM860x ONKEY driver");
  143. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  144. MODULE_LICENSE("GPL");