88pm80x_onkey.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Marvell 88PM80x ONKEY driver
  3. *
  4. * Copyright (C) 2012 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. * Qiao Zhou <zhouqiao@marvell.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file "COPYING" in the main directory of this
  10. * archive for more details.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/input.h>
  24. #include <linux/mfd/88pm80x.h>
  25. #include <linux/regmap.h>
  26. #include <linux/slab.h>
  27. #define PM800_LONG_ONKEY_EN (1 << 0)
  28. #define PM800_LONG_KEY_DELAY (8) /* 1 .. 16 seconds */
  29. #define PM800_LONKEY_PRESS_TIME ((PM800_LONG_KEY_DELAY-1) << 4)
  30. #define PM800_LONKEY_PRESS_TIME_MASK (0xF0)
  31. #define PM800_SW_PDOWN (1 << 5)
  32. struct pm80x_onkey_info {
  33. struct input_dev *idev;
  34. struct pm80x_chip *pm80x;
  35. struct regmap *map;
  36. int irq;
  37. };
  38. /* 88PM80x gives us an interrupt when ONKEY is held */
  39. static irqreturn_t pm80x_onkey_handler(int irq, void *data)
  40. {
  41. struct pm80x_onkey_info *info = data;
  42. int ret = 0;
  43. unsigned int val;
  44. ret = regmap_read(info->map, PM800_STATUS_1, &val);
  45. if (ret < 0) {
  46. dev_err(info->idev->dev.parent, "failed to read status: %d\n", ret);
  47. return IRQ_NONE;
  48. }
  49. val &= PM800_ONKEY_STS1;
  50. input_report_key(info->idev, KEY_POWER, val);
  51. input_sync(info->idev);
  52. return IRQ_HANDLED;
  53. }
  54. static SIMPLE_DEV_PM_OPS(pm80x_onkey_pm_ops, pm80x_dev_suspend,
  55. pm80x_dev_resume);
  56. static int pm80x_onkey_probe(struct platform_device *pdev)
  57. {
  58. struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  59. struct pm80x_onkey_info *info;
  60. int err;
  61. info = kzalloc(sizeof(struct pm80x_onkey_info), GFP_KERNEL);
  62. if (!info)
  63. return -ENOMEM;
  64. info->pm80x = chip;
  65. info->irq = platform_get_irq(pdev, 0);
  66. if (info->irq < 0) {
  67. dev_err(&pdev->dev, "No IRQ resource!\n");
  68. err = -EINVAL;
  69. goto out;
  70. }
  71. info->map = info->pm80x->regmap;
  72. if (!info->map) {
  73. dev_err(&pdev->dev, "no regmap!\n");
  74. err = -EINVAL;
  75. goto out;
  76. }
  77. info->idev = input_allocate_device();
  78. if (!info->idev) {
  79. dev_err(&pdev->dev, "Failed to allocate input dev\n");
  80. err = -ENOMEM;
  81. goto out;
  82. }
  83. info->idev->name = "88pm80x_on";
  84. info->idev->phys = "88pm80x_on/input0";
  85. info->idev->id.bustype = BUS_I2C;
  86. info->idev->dev.parent = &pdev->dev;
  87. info->idev->evbit[0] = BIT_MASK(EV_KEY);
  88. __set_bit(KEY_POWER, info->idev->keybit);
  89. err = pm80x_request_irq(info->pm80x, info->irq, pm80x_onkey_handler,
  90. IRQF_ONESHOT, "onkey", info);
  91. if (err < 0) {
  92. dev_err(&pdev->dev, "Failed to request IRQ: #%d: %d\n",
  93. info->irq, err);
  94. goto out_reg;
  95. }
  96. err = input_register_device(info->idev);
  97. if (err) {
  98. dev_err(&pdev->dev, "Can't register input device: %d\n", err);
  99. goto out_irq;
  100. }
  101. platform_set_drvdata(pdev, info);
  102. /* Enable long onkey detection */
  103. regmap_update_bits(info->map, PM800_RTC_MISC4, PM800_LONG_ONKEY_EN,
  104. PM800_LONG_ONKEY_EN);
  105. /* Set 8-second interval */
  106. regmap_update_bits(info->map, PM800_RTC_MISC3,
  107. PM800_LONKEY_PRESS_TIME_MASK,
  108. PM800_LONKEY_PRESS_TIME);
  109. device_init_wakeup(&pdev->dev, 1);
  110. return 0;
  111. out_irq:
  112. pm80x_free_irq(info->pm80x, info->irq, info);
  113. out_reg:
  114. input_free_device(info->idev);
  115. out:
  116. kfree(info);
  117. return err;
  118. }
  119. static int pm80x_onkey_remove(struct platform_device *pdev)
  120. {
  121. struct pm80x_onkey_info *info = platform_get_drvdata(pdev);
  122. device_init_wakeup(&pdev->dev, 0);
  123. pm80x_free_irq(info->pm80x, info->irq, info);
  124. input_unregister_device(info->idev);
  125. kfree(info);
  126. return 0;
  127. }
  128. static struct platform_driver pm80x_onkey_driver = {
  129. .driver = {
  130. .name = "88pm80x-onkey",
  131. .pm = &pm80x_onkey_pm_ops,
  132. },
  133. .probe = pm80x_onkey_probe,
  134. .remove = pm80x_onkey_remove,
  135. };
  136. module_platform_driver(pm80x_onkey_driver);
  137. MODULE_LICENSE("GPL");
  138. MODULE_DESCRIPTION("Marvell 88PM80x ONKEY driver");
  139. MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  140. MODULE_ALIAS("platform:88pm80x-onkey");