snvs_pwrkey.c 5.6 KB

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