intel_mid_powerbtn.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #define DRIVER_NAME "msic_power_btn"
  27. #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
  28. /*
  29. * MSIC document ti_datasheet defines the 1st bit reg 0x21 is used to mask
  30. * power button interrupt
  31. */
  32. #define MSIC_PWRBTNM (1 << 0)
  33. static irqreturn_t mfld_pb_isr(int irq, void *dev_id)
  34. {
  35. struct input_dev *input = dev_id;
  36. int ret;
  37. u8 pbstat;
  38. ret = intel_msic_reg_read(INTEL_MSIC_PBSTATUS, &pbstat);
  39. dev_dbg(input->dev.parent, "PB_INT status= %d\n", pbstat);
  40. if (ret < 0) {
  41. dev_err(input->dev.parent, "Read error %d while reading"
  42. " MSIC_PB_STATUS\n", ret);
  43. } else {
  44. input_event(input, EV_KEY, KEY_POWER,
  45. !(pbstat & MSIC_PB_LEVEL));
  46. input_sync(input);
  47. }
  48. return IRQ_HANDLED;
  49. }
  50. static int __devinit mfld_pb_probe(struct platform_device *pdev)
  51. {
  52. struct input_dev *input;
  53. int irq = platform_get_irq(pdev, 0);
  54. int error;
  55. if (irq < 0)
  56. return -EINVAL;
  57. input = input_allocate_device();
  58. if (!input) {
  59. dev_err(&pdev->dev, "Input device allocation error\n");
  60. return -ENOMEM;
  61. }
  62. input->name = pdev->name;
  63. input->phys = "power-button/input0";
  64. input->id.bustype = BUS_HOST;
  65. input->dev.parent = &pdev->dev;
  66. input_set_capability(input, EV_KEY, KEY_POWER);
  67. error = request_threaded_irq(irq, NULL, mfld_pb_isr, IRQF_NO_SUSPEND,
  68. DRIVER_NAME, input);
  69. if (error) {
  70. dev_err(&pdev->dev, "Unable to request irq %d for mfld power"
  71. "button\n", irq);
  72. goto err_free_input;
  73. }
  74. error = input_register_device(input);
  75. if (error) {
  76. dev_err(&pdev->dev, "Unable to register input dev, error "
  77. "%d\n", error);
  78. goto err_free_irq;
  79. }
  80. platform_set_drvdata(pdev, input);
  81. /*
  82. * SCU firmware might send power button interrupts to IA core before
  83. * kernel boots and doesn't get EOI from IA core. The first bit of
  84. * MSIC reg 0x21 is kept masked, and SCU firmware doesn't send new
  85. * power interrupt to Android kernel. Unmask the bit when probing
  86. * power button in kernel.
  87. * There is a very narrow race between irq handler and power button
  88. * initialization. The race happens rarely. So we needn't worry
  89. * about it.
  90. */
  91. error = intel_msic_reg_update(INTEL_MSIC_IRQLVL1MSK, 0, MSIC_PWRBTNM);
  92. if (error) {
  93. dev_err(&pdev->dev, "Unable to clear power button interrupt, "
  94. "error: %d\n", error);
  95. goto err_free_irq;
  96. }
  97. return 0;
  98. err_free_irq:
  99. free_irq(irq, input);
  100. err_free_input:
  101. input_free_device(input);
  102. return error;
  103. }
  104. static int __devexit mfld_pb_remove(struct platform_device *pdev)
  105. {
  106. struct input_dev *input = platform_get_drvdata(pdev);
  107. int irq = platform_get_irq(pdev, 0);
  108. free_irq(irq, input);
  109. input_unregister_device(input);
  110. platform_set_drvdata(pdev, NULL);
  111. return 0;
  112. }
  113. static struct platform_driver mfld_pb_driver = {
  114. .driver = {
  115. .name = DRIVER_NAME,
  116. .owner = THIS_MODULE,
  117. },
  118. .probe = mfld_pb_probe,
  119. .remove = __devexit_p(mfld_pb_remove),
  120. };
  121. module_platform_driver(mfld_pb_driver);
  122. MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
  123. MODULE_DESCRIPTION("Intel Medfield Power Button Driver");
  124. MODULE_LICENSE("GPL v2");
  125. MODULE_ALIAS("platform:" DRIVER_NAME);