pmic8xxx-pwrkey.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/input.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/log2.h>
  21. #include <linux/of.h>
  22. #define PON_CNTL_1 0x1C
  23. #define PON_CNTL_PULL_UP BIT(7)
  24. #define PON_CNTL_TRIG_DELAY_MASK (0x7)
  25. /**
  26. * struct pmic8xxx_pwrkey - pmic8xxx pwrkey information
  27. * @key_press_irq: key press irq number
  28. */
  29. struct pmic8xxx_pwrkey {
  30. int key_press_irq;
  31. };
  32. static irqreturn_t pwrkey_press_irq(int irq, void *_pwr)
  33. {
  34. struct input_dev *pwr = _pwr;
  35. input_report_key(pwr, KEY_POWER, 1);
  36. input_sync(pwr);
  37. return IRQ_HANDLED;
  38. }
  39. static irqreturn_t pwrkey_release_irq(int irq, void *_pwr)
  40. {
  41. struct input_dev *pwr = _pwr;
  42. input_report_key(pwr, KEY_POWER, 0);
  43. input_sync(pwr);
  44. return IRQ_HANDLED;
  45. }
  46. #ifdef CONFIG_PM_SLEEP
  47. static int pmic8xxx_pwrkey_suspend(struct device *dev)
  48. {
  49. struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
  50. if (device_may_wakeup(dev))
  51. enable_irq_wake(pwrkey->key_press_irq);
  52. return 0;
  53. }
  54. static int pmic8xxx_pwrkey_resume(struct device *dev)
  55. {
  56. struct pmic8xxx_pwrkey *pwrkey = dev_get_drvdata(dev);
  57. if (device_may_wakeup(dev))
  58. disable_irq_wake(pwrkey->key_press_irq);
  59. return 0;
  60. }
  61. #endif
  62. static SIMPLE_DEV_PM_OPS(pm8xxx_pwr_key_pm_ops,
  63. pmic8xxx_pwrkey_suspend, pmic8xxx_pwrkey_resume);
  64. static int pmic8xxx_pwrkey_probe(struct platform_device *pdev)
  65. {
  66. struct input_dev *pwr;
  67. int key_release_irq = platform_get_irq(pdev, 0);
  68. int key_press_irq = platform_get_irq(pdev, 1);
  69. int err;
  70. unsigned int delay;
  71. unsigned int pon_cntl;
  72. struct regmap *regmap;
  73. struct pmic8xxx_pwrkey *pwrkey;
  74. u32 kpd_delay;
  75. bool pull_up;
  76. if (of_property_read_u32(pdev->dev.of_node, "debounce", &kpd_delay))
  77. kpd_delay = 15625;
  78. if (kpd_delay > 62500 || kpd_delay == 0) {
  79. dev_err(&pdev->dev, "invalid power key trigger delay\n");
  80. return -EINVAL;
  81. }
  82. pull_up = of_property_read_bool(pdev->dev.of_node, "pull-up");
  83. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  84. if (!regmap) {
  85. dev_err(&pdev->dev, "failed to locate regmap for the device\n");
  86. return -ENODEV;
  87. }
  88. pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
  89. if (!pwrkey)
  90. return -ENOMEM;
  91. pwrkey->key_press_irq = key_press_irq;
  92. pwr = devm_input_allocate_device(&pdev->dev);
  93. if (!pwr) {
  94. dev_dbg(&pdev->dev, "Can't allocate power button\n");
  95. return -ENOMEM;
  96. }
  97. input_set_capability(pwr, EV_KEY, KEY_POWER);
  98. pwr->name = "pmic8xxx_pwrkey";
  99. pwr->phys = "pmic8xxx_pwrkey/input0";
  100. delay = (kpd_delay << 10) / USEC_PER_SEC;
  101. delay = 1 + ilog2(delay);
  102. err = regmap_read(regmap, PON_CNTL_1, &pon_cntl);
  103. if (err < 0) {
  104. dev_err(&pdev->dev, "failed reading PON_CNTL_1 err=%d\n", err);
  105. return err;
  106. }
  107. pon_cntl &= ~PON_CNTL_TRIG_DELAY_MASK;
  108. pon_cntl |= (delay & PON_CNTL_TRIG_DELAY_MASK);
  109. if (pull_up)
  110. pon_cntl |= PON_CNTL_PULL_UP;
  111. else
  112. pon_cntl &= ~PON_CNTL_PULL_UP;
  113. err = regmap_write(regmap, PON_CNTL_1, pon_cntl);
  114. if (err < 0) {
  115. dev_err(&pdev->dev, "failed writing PON_CNTL_1 err=%d\n", err);
  116. return err;
  117. }
  118. err = devm_request_irq(&pdev->dev, key_press_irq, pwrkey_press_irq,
  119. IRQF_TRIGGER_RISING,
  120. "pmic8xxx_pwrkey_press", pwr);
  121. if (err) {
  122. dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
  123. key_press_irq, err);
  124. return err;
  125. }
  126. err = devm_request_irq(&pdev->dev, key_release_irq, pwrkey_release_irq,
  127. IRQF_TRIGGER_RISING,
  128. "pmic8xxx_pwrkey_release", pwr);
  129. if (err) {
  130. dev_err(&pdev->dev, "Can't get %d IRQ for pwrkey: %d\n",
  131. key_release_irq, err);
  132. return err;
  133. }
  134. err = input_register_device(pwr);
  135. if (err) {
  136. dev_err(&pdev->dev, "Can't register power key: %d\n", err);
  137. return err;
  138. }
  139. platform_set_drvdata(pdev, pwrkey);
  140. device_init_wakeup(&pdev->dev, 1);
  141. return 0;
  142. }
  143. static int pmic8xxx_pwrkey_remove(struct platform_device *pdev)
  144. {
  145. device_init_wakeup(&pdev->dev, 0);
  146. return 0;
  147. }
  148. static const struct of_device_id pm8xxx_pwr_key_id_table[] = {
  149. { .compatible = "qcom,pm8058-pwrkey" },
  150. { .compatible = "qcom,pm8921-pwrkey" },
  151. { }
  152. };
  153. MODULE_DEVICE_TABLE(of, pm8xxx_pwr_key_id_table);
  154. static struct platform_driver pmic8xxx_pwrkey_driver = {
  155. .probe = pmic8xxx_pwrkey_probe,
  156. .remove = pmic8xxx_pwrkey_remove,
  157. .driver = {
  158. .name = "pm8xxx-pwrkey",
  159. .owner = THIS_MODULE,
  160. .pm = &pm8xxx_pwr_key_pm_ops,
  161. .of_match_table = pm8xxx_pwr_key_id_table,
  162. },
  163. };
  164. module_platform_driver(pmic8xxx_pwrkey_driver);
  165. MODULE_ALIAS("platform:pmic8xxx_pwrkey");
  166. MODULE_DESCRIPTION("PMIC8XXX Power Key driver");
  167. MODULE_LICENSE("GPL v2");
  168. MODULE_AUTHOR("Trilok Soni <tsoni@codeaurora.org>");