pm8941-pwrkey.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  3. * Copyright (c) 2014, Sony Mobile Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/errno.h>
  16. #include <linux/input.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/log2.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/reboot.h>
  24. #include <linux/regmap.h>
  25. #define PON_REV2 0x01
  26. #define PON_RT_STS 0x10
  27. #define PON_KPDPWR_N_SET BIT(0)
  28. #define PON_PS_HOLD_RST_CTL 0x5a
  29. #define PON_PS_HOLD_RST_CTL2 0x5b
  30. #define PON_PS_HOLD_ENABLE BIT(7)
  31. #define PON_PS_HOLD_TYPE_MASK 0x0f
  32. #define PON_PS_HOLD_TYPE_SHUTDOWN 4
  33. #define PON_PS_HOLD_TYPE_HARD_RESET 7
  34. #define PON_PULL_CTL 0x70
  35. #define PON_KPDPWR_PULL_UP BIT(1)
  36. #define PON_DBC_CTL 0x71
  37. #define PON_DBC_DELAY_MASK 0x7
  38. struct pm8941_pwrkey {
  39. struct device *dev;
  40. int irq;
  41. u32 baseaddr;
  42. struct regmap *regmap;
  43. struct input_dev *input;
  44. unsigned int revision;
  45. struct notifier_block reboot_notifier;
  46. };
  47. static int pm8941_reboot_notify(struct notifier_block *nb,
  48. unsigned long code, void *unused)
  49. {
  50. struct pm8941_pwrkey *pwrkey = container_of(nb, struct pm8941_pwrkey,
  51. reboot_notifier);
  52. unsigned int enable_reg;
  53. unsigned int reset_type;
  54. int error;
  55. /* PMICs with revision 0 have the enable bit in same register as ctrl */
  56. if (pwrkey->revision == 0)
  57. enable_reg = PON_PS_HOLD_RST_CTL;
  58. else
  59. enable_reg = PON_PS_HOLD_RST_CTL2;
  60. error = regmap_update_bits(pwrkey->regmap,
  61. pwrkey->baseaddr + enable_reg,
  62. PON_PS_HOLD_ENABLE,
  63. 0);
  64. if (error)
  65. dev_err(pwrkey->dev,
  66. "unable to clear ps hold reset enable: %d\n",
  67. error);
  68. /*
  69. * Updates of PON_PS_HOLD_ENABLE requires 3 sleep cycles between
  70. * writes.
  71. */
  72. usleep_range(100, 1000);
  73. switch (code) {
  74. case SYS_HALT:
  75. case SYS_POWER_OFF:
  76. reset_type = PON_PS_HOLD_TYPE_SHUTDOWN;
  77. break;
  78. case SYS_RESTART:
  79. default:
  80. reset_type = PON_PS_HOLD_TYPE_HARD_RESET;
  81. break;
  82. }
  83. error = regmap_update_bits(pwrkey->regmap,
  84. pwrkey->baseaddr + PON_PS_HOLD_RST_CTL,
  85. PON_PS_HOLD_TYPE_MASK,
  86. reset_type);
  87. if (error)
  88. dev_err(pwrkey->dev, "unable to set ps hold reset type: %d\n",
  89. error);
  90. error = regmap_update_bits(pwrkey->regmap,
  91. pwrkey->baseaddr + enable_reg,
  92. PON_PS_HOLD_ENABLE,
  93. PON_PS_HOLD_ENABLE);
  94. if (error)
  95. dev_err(pwrkey->dev, "unable to re-set enable: %d\n", error);
  96. return NOTIFY_DONE;
  97. }
  98. static irqreturn_t pm8941_pwrkey_irq(int irq, void *_data)
  99. {
  100. struct pm8941_pwrkey *pwrkey = _data;
  101. unsigned int sts;
  102. int error;
  103. error = regmap_read(pwrkey->regmap,
  104. pwrkey->baseaddr + PON_RT_STS, &sts);
  105. if (error)
  106. return IRQ_HANDLED;
  107. input_report_key(pwrkey->input, KEY_POWER, !!(sts & PON_KPDPWR_N_SET));
  108. input_sync(pwrkey->input);
  109. return IRQ_HANDLED;
  110. }
  111. static int __maybe_unused pm8941_pwrkey_suspend(struct device *dev)
  112. {
  113. struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
  114. if (device_may_wakeup(dev))
  115. enable_irq_wake(pwrkey->irq);
  116. return 0;
  117. }
  118. static int __maybe_unused pm8941_pwrkey_resume(struct device *dev)
  119. {
  120. struct pm8941_pwrkey *pwrkey = dev_get_drvdata(dev);
  121. if (device_may_wakeup(dev))
  122. disable_irq_wake(pwrkey->irq);
  123. return 0;
  124. }
  125. static SIMPLE_DEV_PM_OPS(pm8941_pwr_key_pm_ops,
  126. pm8941_pwrkey_suspend, pm8941_pwrkey_resume);
  127. static int pm8941_pwrkey_probe(struct platform_device *pdev)
  128. {
  129. struct pm8941_pwrkey *pwrkey;
  130. bool pull_up;
  131. u32 req_delay;
  132. int error;
  133. if (of_property_read_u32(pdev->dev.of_node, "debounce", &req_delay))
  134. req_delay = 15625;
  135. if (req_delay > 2000000 || req_delay == 0) {
  136. dev_err(&pdev->dev, "invalid debounce time: %u\n", req_delay);
  137. return -EINVAL;
  138. }
  139. pull_up = of_property_read_bool(pdev->dev.of_node, "bias-pull-up");
  140. pwrkey = devm_kzalloc(&pdev->dev, sizeof(*pwrkey), GFP_KERNEL);
  141. if (!pwrkey)
  142. return -ENOMEM;
  143. pwrkey->dev = &pdev->dev;
  144. pwrkey->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  145. if (!pwrkey->regmap) {
  146. dev_err(&pdev->dev, "failed to locate regmap\n");
  147. return -ENODEV;
  148. }
  149. pwrkey->irq = platform_get_irq(pdev, 0);
  150. if (pwrkey->irq < 0) {
  151. dev_err(&pdev->dev, "failed to get irq\n");
  152. return pwrkey->irq;
  153. }
  154. error = of_property_read_u32(pdev->dev.of_node, "reg",
  155. &pwrkey->baseaddr);
  156. if (error)
  157. return error;
  158. error = regmap_read(pwrkey->regmap, pwrkey->baseaddr + PON_REV2,
  159. &pwrkey->revision);
  160. if (error) {
  161. dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
  162. return error;
  163. }
  164. pwrkey->input = devm_input_allocate_device(&pdev->dev);
  165. if (!pwrkey->input) {
  166. dev_dbg(&pdev->dev, "unable to allocate input device\n");
  167. return -ENOMEM;
  168. }
  169. input_set_capability(pwrkey->input, EV_KEY, KEY_POWER);
  170. pwrkey->input->name = "pm8941_pwrkey";
  171. pwrkey->input->phys = "pm8941_pwrkey/input0";
  172. req_delay = (req_delay << 6) / USEC_PER_SEC;
  173. req_delay = ilog2(req_delay);
  174. error = regmap_update_bits(pwrkey->regmap,
  175. pwrkey->baseaddr + PON_DBC_CTL,
  176. PON_DBC_DELAY_MASK,
  177. req_delay);
  178. if (error) {
  179. dev_err(&pdev->dev, "failed to set debounce: %d\n", error);
  180. return error;
  181. }
  182. error = regmap_update_bits(pwrkey->regmap,
  183. pwrkey->baseaddr + PON_PULL_CTL,
  184. PON_KPDPWR_PULL_UP,
  185. pull_up ? PON_KPDPWR_PULL_UP : 0);
  186. if (error) {
  187. dev_err(&pdev->dev, "failed to set pull: %d\n", error);
  188. return error;
  189. }
  190. error = devm_request_threaded_irq(&pdev->dev, pwrkey->irq,
  191. NULL, pm8941_pwrkey_irq,
  192. IRQF_ONESHOT,
  193. "pm8941_pwrkey", pwrkey);
  194. if (error) {
  195. dev_err(&pdev->dev, "failed requesting IRQ: %d\n", error);
  196. return error;
  197. }
  198. error = input_register_device(pwrkey->input);
  199. if (error) {
  200. dev_err(&pdev->dev, "failed to register input device: %d\n",
  201. error);
  202. return error;
  203. }
  204. pwrkey->reboot_notifier.notifier_call = pm8941_reboot_notify,
  205. error = register_reboot_notifier(&pwrkey->reboot_notifier);
  206. if (error) {
  207. dev_err(&pdev->dev, "failed to register reboot notifier: %d\n",
  208. error);
  209. return error;
  210. }
  211. platform_set_drvdata(pdev, pwrkey);
  212. device_init_wakeup(&pdev->dev, 1);
  213. return 0;
  214. }
  215. static int pm8941_pwrkey_remove(struct platform_device *pdev)
  216. {
  217. struct pm8941_pwrkey *pwrkey = platform_get_drvdata(pdev);
  218. device_init_wakeup(&pdev->dev, 0);
  219. unregister_reboot_notifier(&pwrkey->reboot_notifier);
  220. return 0;
  221. }
  222. static const struct of_device_id pm8941_pwr_key_id_table[] = {
  223. { .compatible = "qcom,pm8941-pwrkey" },
  224. { }
  225. };
  226. MODULE_DEVICE_TABLE(of, pm8941_pwr_key_id_table);
  227. static struct platform_driver pm8941_pwrkey_driver = {
  228. .probe = pm8941_pwrkey_probe,
  229. .remove = pm8941_pwrkey_remove,
  230. .driver = {
  231. .name = "pm8941-pwrkey",
  232. .pm = &pm8941_pwr_key_pm_ops,
  233. .of_match_table = of_match_ptr(pm8941_pwr_key_id_table),
  234. },
  235. };
  236. module_platform_driver(pm8941_pwrkey_driver);
  237. MODULE_DESCRIPTION("PM8941 Power Key driver");
  238. MODULE_LICENSE("GPL v2");