intel_mid_powerbtn.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <asm/intel_scu_ipc.h>
  26. #define DRIVER_NAME "msic_power_btn"
  27. #define MSIC_IRQ_STAT 0x02
  28. #define MSIC_IRQ_PB (1 << 0)
  29. #define MSIC_PB_CONFIG 0x3e
  30. #define MSIC_PB_STATUS 0x3f
  31. #define MSIC_PB_LEVEL (1 << 3) /* 1 - release, 0 - press */
  32. struct mfld_pb_priv {
  33. struct input_dev *input;
  34. unsigned int irq;
  35. };
  36. static irqreturn_t mfld_pb_isr(int irq, void *dev_id)
  37. {
  38. struct mfld_pb_priv *priv = dev_id;
  39. int ret;
  40. u8 pbstat;
  41. ret = intel_scu_ipc_ioread8(MSIC_PB_STATUS, &pbstat);
  42. if (ret < 0)
  43. return IRQ_HANDLED;
  44. input_event(priv->input, EV_KEY, KEY_POWER, !(pbstat & MSIC_PB_LEVEL));
  45. input_sync(priv->input);
  46. return IRQ_HANDLED;
  47. }
  48. static int __devinit mfld_pb_probe(struct platform_device *pdev)
  49. {
  50. struct mfld_pb_priv *priv;
  51. struct input_dev *input;
  52. int irq;
  53. int error;
  54. irq = platform_get_irq(pdev, 0);
  55. if (irq < 0)
  56. return -EINVAL;
  57. priv = kzalloc(sizeof(struct mfld_pb_priv), GFP_KERNEL);
  58. input = input_allocate_device();
  59. if (!priv || !input) {
  60. error = -ENOMEM;
  61. goto err_free_mem;
  62. }
  63. priv->input = input;
  64. priv->irq = irq;
  65. input->name = pdev->name;
  66. input->phys = "power-button/input0";
  67. input->id.bustype = BUS_HOST;
  68. input->dev.parent = &pdev->dev;
  69. input_set_capability(input, EV_KEY, KEY_POWER);
  70. error = request_threaded_irq(priv->irq, NULL, mfld_pb_isr,
  71. 0, DRIVER_NAME, priv);
  72. if (error) {
  73. dev_err(&pdev->dev,
  74. "unable to request irq %d for mfld power button\n",
  75. irq);
  76. goto err_free_mem;
  77. }
  78. error = input_register_device(input);
  79. if (error) {
  80. dev_err(&pdev->dev,
  81. "unable to register input dev, error %d\n", error);
  82. goto err_free_irq;
  83. }
  84. platform_set_drvdata(pdev, priv);
  85. return 0;
  86. err_free_irq:
  87. free_irq(priv->irq, priv);
  88. err_free_mem:
  89. input_free_device(input);
  90. kfree(priv);
  91. return error;
  92. }
  93. static int __devexit mfld_pb_remove(struct platform_device *pdev)
  94. {
  95. struct mfld_pb_priv *priv = platform_get_drvdata(pdev);
  96. free_irq(priv->irq, priv);
  97. input_unregister_device(priv->input);
  98. kfree(priv);
  99. platform_set_drvdata(pdev, NULL);
  100. return 0;
  101. }
  102. static struct platform_driver mfld_pb_driver = {
  103. .driver = {
  104. .name = DRIVER_NAME,
  105. .owner = THIS_MODULE,
  106. },
  107. .probe = mfld_pb_probe,
  108. .remove = __devexit_p(mfld_pb_remove),
  109. };
  110. static int __init mfld_pb_init(void)
  111. {
  112. return platform_driver_register(&mfld_pb_driver);
  113. }
  114. module_init(mfld_pb_init);
  115. static void __exit mfld_pb_exit(void)
  116. {
  117. platform_driver_unregister(&mfld_pb_driver);
  118. }
  119. module_exit(mfld_pb_exit);
  120. MODULE_AUTHOR("Hong Liu <hong.liu@intel.com>");
  121. MODULE_DESCRIPTION("Intel Medfield Power Button Driver");
  122. MODULE_LICENSE("GPL v2");
  123. MODULE_ALIAS("platform:" DRIVER_NAME);