intel_mid_powerbtn.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Power button driver for Medfield.
  3. *
  4. * Copyright (C) 2010 Intel Corp
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/slab.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/input.h>
  25. #include <linux/mfd/intel_msic.h>
  26. #include <linux/pm_wakeirq.h>
  27. #define DRIVER_NAME "msic_power_btn"
  28. #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
  29. /*
  30. * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
  31. * power button interrupt
  32. */
  33. #define MSIC_PWRBTNM (1 << 0)
  34. static irqreturn_t mfld_pb_isr(int irq, void *dev_id)
  35. {
  36. struct input_dev *input = dev_id;
  37. int ret;
  38. u8 pbstat;
  39. ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat);
  40. dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
  41. if (ret < 0) {
  42. dev_err(input->dev.parent, "Read error %d while reading"
  43. " MSIC_PB_STATUS\n", ret);
  44. } else {
  45. input_event(input, EV_KEY, KEY_POWER,
  46. !(pbstat & MSIC_PB_LEVEL));
  47. input_sync(input);
  48. }
  49. return IRQ_HANDLED;
  50. }
  51. static int mfld_pb_probe(struct platform_device *pdev)
  52. {
  53. struct input_dev *input;
  54. int irq = platform_get_irq(pdev, 0);
  55. int error;
  56. if (irq < 0)
  57. return -EINVAL;
  58. input = input_allocate_device();
  59. if (!input)
  60. return -ENOMEM;
  61. input->name = pdev->name;
  62. input->phys = "power-button/input0";
  63. input->id.bustype = BUS_HOST;
  64. input->dev.parent = &pdev->dev;
  65. input_set_capability(input, EV_KEY, KEY_POWER);
  66. error = request_threaded_irq(irq, NULL, mfld_pb_isr, IRQF_ONESHOT,
  67. DRIVER_NAME, input);
  68. if (error) {
  69. dev_err(&pdev->dev, "Unable to request irq %d for mfld power"
  70. "button\n", irq);
  71. goto err_free_input;
  72. }
  73. device_init_wakeup(&pdev->dev, true);
  74. dev_pm_set_wake_irq(&pdev->dev, irq);
  75. error = input_register_device(input);
  76. if (error) {
  77. dev_err(&pdev->dev, "Unable to register input dev, error "
  78. "%d\n", error);
  79. goto err_free_irq;
  80. }
  81. platform_set_drvdata(pdev, input);
  82. /*
  83. * SCU firmware might send power button interrupts to IA core before
  84. * kernel boots and doesn't get EOI from IA core. The first bit of
  85. * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
  86. * power interrupt to Android kernel. Unmask the bit when probing
  87. * power button in kernel.
  88. * There is a very narrow race between irq handler and power button
  89. * initialization. The race happens rarely. So we needn't worry
  90. * about it.
  91. */
  92. error = intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM);
  93. if (error) {
  94. dev_err(&pdev->dev, "Unable to clear power button interrupt, "
  95. "error: %d\n", error);
  96. goto err_free_irq;
  97. }
  98. return 0;
  99. err_free_irq:
  100. free_irq(irq, input);
  101. err_free_input:
  102. input_free_device(input);
  103. return error;
  104. }
  105. static int mfld_pb_remove(struct platform_device *pdev)
  106. {
  107. struct input_dev *input = platform_get_drvdata(pdev);
  108. int irq = platform_get_irq(pdev, 0);
  109. dev_pm_clear_wake_irq(&pdev->dev);
  110. device_init_wakeup(&pdev->dev, false);
  111. free_irq(irq, input);
  112. input_unregister_device(input);
  113. return 0;
  114. }
  115. static struct platform_driver mfld_pb_driver = {
  116. .driver = {
  117. .name = DRIVER_NAME,
  118. },
  119. .probe = mfld_pb_probe,
  120. .remove = mfld_pb_remove,
  121. };
  122. module_platform_driver(mfld_pb_driver);
  123. MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
  124. MODULE_DESCRIPTION("Intel Medfield Power Button Driver");
  125. MODULE_LICENSE("GPL v2");
  126. MODULE_ALIAS("platform:" DRIVER_NAME);