snvs_pwrkey.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Driver for the IMX SNVS ON/OFF Power Key
  4. // Copyright (C) 2015 Freescale Semiconductor, Inc. All Rights Reserved.
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/init.h>
  8. #include <linux/input.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/regmap.h>
  19. #define SNVS_LPSR_REG 0x4C /* LP Status Register */
  20. #define SNVS_LPCR_REG 0x38 /* LP Control Register */
  21. #define SNVS_HPSR_REG 0x14
  22. #define SNVS_HPSR_BTN BIT(6)
  23. #define SNVS_LPSR_SPO BIT(18)
  24. #define SNVS_LPCR_DEP_EN BIT(5)
  25. #define DEBOUNCE_TIME 30
  26. #define REPEAT_INTERVAL 60
  27. struct pwrkey_drv_data {
  28. struct regmap *snvs;
  29. int irq;
  30. int keycode;
  31. int keystate; /* 1:pressed */
  32. int wakeup;
  33. struct timer_list check_timer;
  34. struct input_dev *input;
  35. };
  36. static void imx_imx_snvs_check_for_events(struct timer_list *t)
  37. {
  38. struct pwrkey_drv_data *pdata = from_timer(pdata, t, check_timer);
  39. struct input_dev *input = pdata->input;
  40. u32 state;
  41. regmap_read(pdata->snvs, SNVS_HPSR_REG, &state);
  42. state = state & SNVS_HPSR_BTN ? 1 : 0;
  43. /* only report new event if status changed */
  44. if (state ^ pdata->keystate) {
  45. pdata->keystate = state;
  46. input_event(input, EV_KEY, pdata->keycode, state);
  47. input_sync(input);
  48. pm_relax(pdata->input->dev.parent);
  49. }
  50. /* repeat check if pressed long */
  51. if (state) {
  52. mod_timer(&pdata->check_timer,
  53. jiffies + msecs_to_jiffies(REPEAT_INTERVAL));
  54. }
  55. }
  56. static irqreturn_t imx_snvs_pwrkey_interrupt(int irq, void *dev_id)
  57. {
  58. struct platform_device *pdev = dev_id;
  59. struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
  60. u32 lp_status;
  61. pm_wakeup_event(pdata->input->dev.parent, 0);
  62. regmap_read(pdata->snvs, SNVS_LPSR_REG, &lp_status);
  63. if (lp_status & SNVS_LPSR_SPO)
  64. mod_timer(&pdata->check_timer, jiffies + msecs_to_jiffies(DEBOUNCE_TIME));
  65. /* clear SPO status */
  66. regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
  67. return IRQ_HANDLED;
  68. }
  69. static void imx_snvs_pwrkey_act(void *pdata)
  70. {
  71. struct pwrkey_drv_data *pd = pdata;
  72. del_timer_sync(&pd->check_timer);
  73. }
  74. static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
  75. {
  76. struct pwrkey_drv_data *pdata = NULL;
  77. struct input_dev *input = NULL;
  78. struct device_node *np;
  79. int error;
  80. /* Get SNVS register Page */
  81. np = pdev->dev.of_node;
  82. if (!np)
  83. return -ENODEV;
  84. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  85. if (!pdata)
  86. return -ENOMEM;
  87. pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
  88. if (IS_ERR(pdata->snvs)) {
  89. dev_err(&pdev->dev, "Can't get snvs syscon\n");
  90. return PTR_ERR(pdata->snvs);
  91. }
  92. if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
  93. pdata->keycode = KEY_POWER;
  94. dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
  95. }
  96. pdata->wakeup = of_property_read_bool(np, "wakeup-source");
  97. pdata->irq = platform_get_irq(pdev, 0);
  98. if (pdata->irq < 0) {
  99. dev_err(&pdev->dev, "no irq defined in platform data\n");
  100. return -EINVAL;
  101. }
  102. regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
  103. /* clear the unexpected interrupt before driver ready */
  104. regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
  105. timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
  106. input = devm_input_allocate_device(&pdev->dev);
  107. if (!input) {
  108. dev_err(&pdev->dev, "failed to allocate the input device\n");
  109. return -ENOMEM;
  110. }
  111. input->name = pdev->name;
  112. input->phys = "snvs-pwrkey/input0";
  113. input->id.bustype = BUS_HOST;
  114. input_set_capability(input, EV_KEY, pdata->keycode);
  115. /* input customer action to cancel release timer */
  116. error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
  117. if (error) {
  118. dev_err(&pdev->dev, "failed to register remove action\n");
  119. return error;
  120. }
  121. error = devm_request_irq(&pdev->dev, pdata->irq,
  122. imx_snvs_pwrkey_interrupt,
  123. 0, pdev->name, pdev);
  124. if (error) {
  125. dev_err(&pdev->dev, "interrupt not available.\n");
  126. return error;
  127. }
  128. error = input_register_device(input);
  129. if (error < 0) {
  130. dev_err(&pdev->dev, "failed to register input device\n");
  131. return error;
  132. }
  133. pdata->input = input;
  134. platform_set_drvdata(pdev, pdata);
  135. device_init_wakeup(&pdev->dev, pdata->wakeup);
  136. return 0;
  137. }
  138. static int __maybe_unused imx_snvs_pwrkey_suspend(struct device *dev)
  139. {
  140. struct platform_device *pdev = to_platform_device(dev);
  141. struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
  142. if (device_may_wakeup(&pdev->dev))
  143. enable_irq_wake(pdata->irq);
  144. return 0;
  145. }
  146. static int __maybe_unused imx_snvs_pwrkey_resume(struct device *dev)
  147. {
  148. struct platform_device *pdev = to_platform_device(dev);
  149. struct pwrkey_drv_data *pdata = platform_get_drvdata(pdev);
  150. if (device_may_wakeup(&pdev->dev))
  151. disable_irq_wake(pdata->irq);
  152. return 0;
  153. }
  154. static const struct of_device_id imx_snvs_pwrkey_ids[] = {
  155. { .compatible = "fsl,sec-v4.0-pwrkey" },
  156. { /* sentinel */ }
  157. };
  158. MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
  159. static SIMPLE_DEV_PM_OPS(imx_snvs_pwrkey_pm_ops, imx_snvs_pwrkey_suspend,
  160. imx_snvs_pwrkey_resume);
  161. static struct platform_driver imx_snvs_pwrkey_driver = {
  162. .driver = {
  163. .name = "snvs_pwrkey",
  164. .pm = &imx_snvs_pwrkey_pm_ops,
  165. .of_match_table = imx_snvs_pwrkey_ids,
  166. },
  167. .probe = imx_snvs_pwrkey_probe,
  168. };
  169. module_platform_driver(imx_snvs_pwrkey_driver);
  170. MODULE_AUTHOR("Freescale Semiconductor");
  171. MODULE_DESCRIPTION("i.MX snvs power key Driver");
  172. MODULE_LICENSE("GPL");