snvs_pwrkey.c 6.2 KB

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