snvs_pwrkey.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 0xF8
  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_disable_clk(void *data)
  90. {
  91. clk_disable_unprepare(data);
  92. }
  93. static void imx_snvs_pwrkey_act(void *pdata)
  94. {
  95. struct pwrkey_drv_data *pd = pdata;
  96. del_timer_sync(&pd->check_timer);
  97. }
  98. static int imx_snvs_pwrkey_probe(struct platform_device *pdev)
  99. {
  100. struct pwrkey_drv_data *pdata;
  101. struct input_dev *input;
  102. struct device_node *np;
  103. struct clk *clk;
  104. int error;
  105. u32 vid;
  106. /* Get SNVS register Page */
  107. np = pdev->dev.of_node;
  108. if (!np)
  109. return -ENODEV;
  110. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  111. if (!pdata)
  112. return -ENOMEM;
  113. pdata->snvs = syscon_regmap_lookup_by_phandle(np, "regmap");
  114. if (IS_ERR(pdata->snvs)) {
  115. dev_err(&pdev->dev, "Can't get snvs syscon\n");
  116. return PTR_ERR(pdata->snvs);
  117. }
  118. if (of_property_read_u32(np, "linux,keycode", &pdata->keycode)) {
  119. pdata->keycode = KEY_POWER;
  120. dev_warn(&pdev->dev, "KEY_POWER without setting in dts\n");
  121. }
  122. clk = devm_clk_get_optional(&pdev->dev, NULL);
  123. if (IS_ERR(clk)) {
  124. dev_err(&pdev->dev, "Failed to get snvs clock (%pe)\n", clk);
  125. return PTR_ERR(clk);
  126. }
  127. error = clk_prepare_enable(clk);
  128. if (error) {
  129. dev_err(&pdev->dev, "Failed to enable snvs clock (%pe)\n",
  130. ERR_PTR(error));
  131. return error;
  132. }
  133. error = devm_add_action_or_reset(&pdev->dev,
  134. imx_snvs_pwrkey_disable_clk, clk);
  135. if (error) {
  136. dev_err(&pdev->dev,
  137. "Failed to register clock cleanup handler (%pe)\n",
  138. ERR_PTR(error));
  139. return error;
  140. }
  141. pdata->wakeup = of_property_read_bool(np, "wakeup-source");
  142. pdata->irq = platform_get_irq(pdev, 0);
  143. if (pdata->irq < 0)
  144. return -EINVAL;
  145. regmap_read(pdata->snvs, SNVS_HPVIDR1_REG, &vid);
  146. pdata->minor_rev = vid & 0xff;
  147. regmap_update_bits(pdata->snvs, SNVS_LPCR_REG, SNVS_LPCR_DEP_EN, SNVS_LPCR_DEP_EN);
  148. /* clear the unexpected interrupt before driver ready */
  149. regmap_write(pdata->snvs, SNVS_LPSR_REG, SNVS_LPSR_SPO);
  150. timer_setup(&pdata->check_timer, imx_imx_snvs_check_for_events, 0);
  151. input = devm_input_allocate_device(&pdev->dev);
  152. if (!input) {
  153. dev_err(&pdev->dev, "failed to allocate the input device\n");
  154. return -ENOMEM;
  155. }
  156. input->name = pdev->name;
  157. input->phys = "snvs-pwrkey/input0";
  158. input->id.bustype = BUS_HOST;
  159. input_set_capability(input, EV_KEY, pdata->keycode);
  160. /* input customer action to cancel release timer */
  161. error = devm_add_action(&pdev->dev, imx_snvs_pwrkey_act, pdata);
  162. if (error) {
  163. dev_err(&pdev->dev, "failed to register remove action\n");
  164. return error;
  165. }
  166. pdata->input = input;
  167. platform_set_drvdata(pdev, pdata);
  168. error = devm_request_irq(&pdev->dev, pdata->irq,
  169. imx_snvs_pwrkey_interrupt,
  170. 0, pdev->name, pdev);
  171. if (error) {
  172. dev_err(&pdev->dev, "interrupt not available.\n");
  173. return error;
  174. }
  175. error = input_register_device(input);
  176. if (error < 0) {
  177. dev_err(&pdev->dev, "failed to register input device\n");
  178. return error;
  179. }
  180. device_init_wakeup(&pdev->dev, pdata->wakeup);
  181. error = dev_pm_set_wake_irq(&pdev->dev, pdata->irq);
  182. if (error)
  183. dev_err(&pdev->dev, "irq wake enable failed.\n");
  184. return 0;
  185. }
  186. static const struct of_device_id imx_snvs_pwrkey_ids[] = {
  187. { .compatible = "fsl,sec-v4.0-pwrkey" },
  188. { /* sentinel */ }
  189. };
  190. MODULE_DEVICE_TABLE(of, imx_snvs_pwrkey_ids);
  191. static struct platform_driver imx_snvs_pwrkey_driver = {
  192. .driver = {
  193. .name = "snvs_pwrkey",
  194. .of_match_table = imx_snvs_pwrkey_ids,
  195. },
  196. .probe = imx_snvs_pwrkey_probe,
  197. };
  198. module_platform_driver(imx_snvs_pwrkey_driver);
  199. MODULE_AUTHOR("Freescale Semiconductor");
  200. MODULE_DESCRIPTION("i.MX snvs power key Driver");
  201. MODULE_LICENSE("GPL");