palmas-pwrbutton.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Texas Instruments' Palmas Power Button Input Driver
  3. *
  4. * Copyright (C) 2012-2014 Texas Instruments Incorporated - http://www.ti.com/
  5. * Girish S Ghongdemath
  6. * Nishanth Menon
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/input.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mfd/palmas.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #define PALMAS_LPK_TIME_MASK 0x0c
  27. #define PALMAS_PWRON_DEBOUNCE_MASK 0x03
  28. #define PALMAS_PWR_KEY_Q_TIME_MS 20
  29. /**
  30. * struct palmas_pwron - Palmas power on data
  31. * @palmas: pointer to palmas device
  32. * @input_dev: pointer to input device
  33. * @input_work: work for detecting release of key
  34. * @irq: irq that we are hooked on to
  35. */
  36. struct palmas_pwron {
  37. struct palmas *palmas;
  38. struct input_dev *input_dev;
  39. struct delayed_work input_work;
  40. int irq;
  41. };
  42. /**
  43. * struct palmas_pwron_config - configuration of palmas power on
  44. * @long_press_time_val: value for long press h/w shutdown event
  45. * @pwron_debounce_val: value for debounce of power button
  46. */
  47. struct palmas_pwron_config {
  48. u8 long_press_time_val;
  49. u8 pwron_debounce_val;
  50. };
  51. /**
  52. * palmas_power_button_work() - Detects the button release event
  53. * @work: work item to detect button release
  54. */
  55. static void palmas_power_button_work(struct work_struct *work)
  56. {
  57. struct palmas_pwron *pwron = container_of(work,
  58. struct palmas_pwron,
  59. input_work.work);
  60. struct input_dev *input_dev = pwron->input_dev;
  61. unsigned int reg;
  62. int error;
  63. error = palmas_read(pwron->palmas, PALMAS_INTERRUPT_BASE,
  64. PALMAS_INT1_LINE_STATE, &reg);
  65. if (error) {
  66. dev_err(input_dev->dev.parent,
  67. "Cannot read palmas PWRON status: %d\n", error);
  68. } else if (reg & BIT(1)) {
  69. /* The button is released, report event. */
  70. input_report_key(input_dev, KEY_POWER, 0);
  71. input_sync(input_dev);
  72. } else {
  73. /* The button is still depressed, keep checking. */
  74. schedule_delayed_work(&pwron->input_work,
  75. msecs_to_jiffies(PALMAS_PWR_KEY_Q_TIME_MS));
  76. }
  77. }
  78. /**
  79. * pwron_irq() - button press isr
  80. * @irq: irq
  81. * @palmas_pwron: pwron struct
  82. *
  83. * Return: IRQ_HANDLED
  84. */
  85. static irqreturn_t pwron_irq(int irq, void *palmas_pwron)
  86. {
  87. struct palmas_pwron *pwron = palmas_pwron;
  88. struct input_dev *input_dev = pwron->input_dev;
  89. input_report_key(input_dev, KEY_POWER, 1);
  90. pm_wakeup_event(input_dev->dev.parent, 0);
  91. input_sync(input_dev);
  92. mod_delayed_work(system_wq, &pwron->input_work,
  93. msecs_to_jiffies(PALMAS_PWR_KEY_Q_TIME_MS));
  94. return IRQ_HANDLED;
  95. }
  96. /**
  97. * palmas_pwron_params_ofinit() - device tree parameter parser
  98. * @dev: palmas button device
  99. * @config: configuration params that this fills up
  100. */
  101. static void palmas_pwron_params_ofinit(struct device *dev,
  102. struct palmas_pwron_config *config)
  103. {
  104. struct device_node *np;
  105. u32 val;
  106. int i, error;
  107. u8 lpk_times[] = { 6, 8, 10, 12 };
  108. int pwr_on_deb_ms[] = { 15, 100, 500, 1000 };
  109. memset(config, 0, sizeof(*config));
  110. /* Default config parameters */
  111. config->long_press_time_val = ARRAY_SIZE(lpk_times) - 1;
  112. np = dev->of_node;
  113. if (!np)
  114. return;
  115. error = of_property_read_u32(np, "ti,palmas-long-press-seconds", &val);
  116. if (!error) {
  117. for (i = 0; i < ARRAY_SIZE(lpk_times); i++) {
  118. if (val <= lpk_times[i]) {
  119. config->long_press_time_val = i;
  120. break;
  121. }
  122. }
  123. }
  124. error = of_property_read_u32(np,
  125. "ti,palmas-pwron-debounce-milli-seconds",
  126. &val);
  127. if (!error) {
  128. for (i = 0; i < ARRAY_SIZE(pwr_on_deb_ms); i++) {
  129. if (val <= pwr_on_deb_ms[i]) {
  130. config->pwron_debounce_val = i;
  131. break;
  132. }
  133. }
  134. }
  135. dev_info(dev, "h/w controlled shutdown duration=%d seconds\n",
  136. lpk_times[config->long_press_time_val]);
  137. }
  138. /**
  139. * palmas_pwron_probe() - probe
  140. * @pdev: platform device for the button
  141. *
  142. * Return: 0 for successful probe else appropriate error
  143. */
  144. static int palmas_pwron_probe(struct platform_device *pdev)
  145. {
  146. struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
  147. struct device *dev = &pdev->dev;
  148. struct input_dev *input_dev;
  149. struct palmas_pwron *pwron;
  150. struct palmas_pwron_config config;
  151. int val;
  152. int error;
  153. palmas_pwron_params_ofinit(dev, &config);
  154. pwron = kzalloc(sizeof(*pwron), GFP_KERNEL);
  155. if (!pwron)
  156. return -ENOMEM;
  157. input_dev = input_allocate_device();
  158. if (!input_dev) {
  159. dev_err(dev, "Can't allocate power button\n");
  160. error = -ENOMEM;
  161. goto err_free_mem;
  162. }
  163. input_dev->name = "palmas_pwron";
  164. input_dev->phys = "palmas_pwron/input0";
  165. input_dev->dev.parent = dev;
  166. input_set_capability(input_dev, EV_KEY, KEY_POWER);
  167. /*
  168. * Setup default hardware shutdown option (long key press)
  169. * and debounce.
  170. */
  171. val = config.long_press_time_val << __ffs(PALMAS_LPK_TIME_MASK);
  172. val |= config.pwron_debounce_val << __ffs(PALMAS_PWRON_DEBOUNCE_MASK);
  173. error = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
  174. PALMAS_LONG_PRESS_KEY,
  175. PALMAS_LPK_TIME_MASK |
  176. PALMAS_PWRON_DEBOUNCE_MASK,
  177. val);
  178. if (error) {
  179. dev_err(dev, "LONG_PRESS_KEY_UPDATE failed: %d\n", error);
  180. goto err_free_input;
  181. }
  182. pwron->palmas = palmas;
  183. pwron->input_dev = input_dev;
  184. INIT_DELAYED_WORK(&pwron->input_work, palmas_power_button_work);
  185. pwron->irq = platform_get_irq(pdev, 0);
  186. error = request_threaded_irq(pwron->irq, NULL, pwron_irq,
  187. IRQF_TRIGGER_HIGH |
  188. IRQF_TRIGGER_LOW |
  189. IRQF_ONESHOT,
  190. dev_name(dev), pwron);
  191. if (error) {
  192. dev_err(dev, "Can't get IRQ for pwron: %d\n", error);
  193. goto err_free_input;
  194. }
  195. error = input_register_device(input_dev);
  196. if (error) {
  197. dev_err(dev, "Can't register power button: %d\n", error);
  198. goto err_free_irq;
  199. }
  200. platform_set_drvdata(pdev, pwron);
  201. device_init_wakeup(dev, true);
  202. return 0;
  203. err_free_irq:
  204. cancel_delayed_work_sync(&pwron->input_work);
  205. free_irq(pwron->irq, pwron);
  206. err_free_input:
  207. input_free_device(input_dev);
  208. err_free_mem:
  209. kfree(pwron);
  210. return error;
  211. }
  212. /**
  213. * palmas_pwron_remove() - Cleanup on removal
  214. * @pdev: platform device for the button
  215. *
  216. * Return: 0
  217. */
  218. static int palmas_pwron_remove(struct platform_device *pdev)
  219. {
  220. struct palmas_pwron *pwron = platform_get_drvdata(pdev);
  221. free_irq(pwron->irq, pwron);
  222. cancel_delayed_work_sync(&pwron->input_work);
  223. input_unregister_device(pwron->input_dev);
  224. kfree(pwron);
  225. return 0;
  226. }
  227. /**
  228. * palmas_pwron_suspend() - suspend handler
  229. * @dev: power button device
  230. *
  231. * Cancel all pending work items for the power button, setup irq for wakeup
  232. *
  233. * Return: 0
  234. */
  235. static int __maybe_unused palmas_pwron_suspend(struct device *dev)
  236. {
  237. struct platform_device *pdev = to_platform_device(dev);
  238. struct palmas_pwron *pwron = platform_get_drvdata(pdev);
  239. cancel_delayed_work_sync(&pwron->input_work);
  240. if (device_may_wakeup(dev))
  241. enable_irq_wake(pwron->irq);
  242. return 0;
  243. }
  244. /**
  245. * palmas_pwron_resume() - resume handler
  246. * @dev: power button device
  247. *
  248. * Just disable the wakeup capability of irq here.
  249. *
  250. * Return: 0
  251. */
  252. static int __maybe_unused palmas_pwron_resume(struct device *dev)
  253. {
  254. struct platform_device *pdev = to_platform_device(dev);
  255. struct palmas_pwron *pwron = platform_get_drvdata(pdev);
  256. if (device_may_wakeup(dev))
  257. disable_irq_wake(pwron->irq);
  258. return 0;
  259. }
  260. static SIMPLE_DEV_PM_OPS(palmas_pwron_pm,
  261. palmas_pwron_suspend, palmas_pwron_resume);
  262. #ifdef CONFIG_OF
  263. static const struct of_device_id of_palmas_pwr_match[] = {
  264. { .compatible = "ti,palmas-pwrbutton" },
  265. { },
  266. };
  267. MODULE_DEVICE_TABLE(of, of_palmas_pwr_match);
  268. #endif
  269. static struct platform_driver palmas_pwron_driver = {
  270. .probe = palmas_pwron_probe,
  271. .remove = palmas_pwron_remove,
  272. .driver = {
  273. .name = "palmas_pwrbutton",
  274. .of_match_table = of_match_ptr(of_palmas_pwr_match),
  275. .pm = &palmas_pwron_pm,
  276. },
  277. };
  278. module_platform_driver(palmas_pwron_driver);
  279. MODULE_ALIAS("platform:palmas-pwrbutton");
  280. MODULE_DESCRIPTION("Palmas Power Button");
  281. MODULE_LICENSE("GPL v2");
  282. MODULE_AUTHOR("Texas Instruments Inc.");