88pm860x_onkey.c 4.0 KB

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