130-input-add-axp20x-pek.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. From 63d559304a15ffead2fa1014b93dbcabf516c257 Mon Sep 17 00:00:00 2001
  2. From: Carlo Caione <carlo@caione.org>
  3. Date: Mon, 19 May 2014 21:47:45 +0200
  4. Subject: [PATCH] input: misc: Add driver for AXP20x Power Enable Key
  5. This patch add support for the Power Enable Key found on MFD AXP202 and
  6. AXP209. Besides the basic support for the button, the driver adds two
  7. entries in sysfs to configure the time delay for power on/off.
  8. Signed-off-by: Carlo Caione <carlo@caione.org>
  9. Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
  10. ---
  11. drivers/input/misc/Kconfig | 11 ++
  12. drivers/input/misc/Makefile | 1 +
  13. drivers/input/misc/axp20x-pek.c | 281 ++++++++++++++++++++++++++++++++++++++++
  14. 3 files changed, 293 insertions(+)
  15. create mode 100644 drivers/input/misc/axp20x-pek.c
  16. --- a/drivers/input/misc/Kconfig
  17. +++ b/drivers/input/misc/Kconfig
  18. @@ -404,6 +404,17 @@ config INPUT_RETU_PWRBUTTON
  19. To compile this driver as a module, choose M here. The module will
  20. be called retu-pwrbutton.
  21. +config INPUT_AXP20X_PEK
  22. + tristate "X-Powers AXP20X power button driver"
  23. + depends on MFD_AXP20X
  24. + help
  25. + Say Y here if you want to enable power key reporting via the
  26. + AXP20X PMIC.
  27. +
  28. + To compile this driver as a module, choose M here. The module will
  29. + be called axp20x-pek.
  30. +
  31. +
  32. config INPUT_TWL4030_PWRBUTTON
  33. tristate "TWL4030 Power button Driver"
  34. depends on TWL4030_CORE
  35. --- a/drivers/input/misc/Makefile
  36. +++ b/drivers/input/misc/Makefile
  37. @@ -54,6 +54,7 @@ obj-$(CONFIG_INPUT_POWERMATE) += powerm
  38. obj-$(CONFIG_INPUT_PWM_BEEPER) += pwm-beeper.o
  39. obj-$(CONFIG_INPUT_RB532_BUTTON) += rb532_button.o
  40. obj-$(CONFIG_INPUT_RETU_PWRBUTTON) += retu-pwrbutton.o
  41. +obj-$(CONFIG_INPUT_AXP20X_PEK) += axp20x-pek.o
  42. obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER) += rotary_encoder.o
  43. obj-$(CONFIG_INPUT_SGI_BTNS) += sgi_btns.o
  44. obj-$(CONFIG_INPUT_SIRFSOC_ONKEY) += sirfsoc-onkey.o
  45. --- /dev/null
  46. +++ b/drivers/input/misc/axp20x-pek.c
  47. @@ -0,0 +1,281 @@
  48. +/*
  49. + * axp20x power button driver.
  50. + *
  51. + * Copyright (C) 2013 Carlo Caione <carlo@caione.org>
  52. + *
  53. + * This file is subject to the terms and conditions of the GNU General
  54. + * Public License. See the file "COPYING" in the main directory of this
  55. + * archive for more details.
  56. + *
  57. + * This program is distributed in the hope that it will be useful,
  58. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  59. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  60. + * GNU General Public License for more details.
  61. + */
  62. +
  63. +#include <linux/errno.h>
  64. +#include <linux/irq.h>
  65. +#include <linux/init.h>
  66. +#include <linux/input.h>
  67. +#include <linux/interrupt.h>
  68. +#include <linux/kernel.h>
  69. +#include <linux/mfd/axp20x.h>
  70. +#include <linux/module.h>
  71. +#include <linux/platform_device.h>
  72. +#include <linux/regmap.h>
  73. +#include <linux/slab.h>
  74. +
  75. +#define AXP20X_PEK_STARTUP_MASK (0xc0)
  76. +#define AXP20X_PEK_SHUTDOWN_MASK (0x03)
  77. +
  78. +struct axp20x_pek {
  79. + struct axp20x_dev *axp20x;
  80. + struct input_dev *input;
  81. + int irq_dbr;
  82. + int irq_dbf;
  83. +};
  84. +
  85. +struct axp20x_time {
  86. + unsigned int time;
  87. + unsigned int idx;
  88. +};
  89. +
  90. +static const struct axp20x_time startup_time[] = {
  91. + { .time = 128, .idx = 0 },
  92. + { .time = 1000, .idx = 2 },
  93. + { .time = 3000, .idx = 1 },
  94. + { .time = 2000, .idx = 3 },
  95. +};
  96. +
  97. +static const struct axp20x_time shutdown_time[] = {
  98. + { .time = 4000, .idx = 0 },
  99. + { .time = 6000, .idx = 1 },
  100. + { .time = 8000, .idx = 2 },
  101. + { .time = 10000, .idx = 3 },
  102. +};
  103. +
  104. +struct axp20x_pek_ext_attr {
  105. + const struct axp20x_time *p_time;
  106. + unsigned int mask;
  107. +};
  108. +
  109. +static struct axp20x_pek_ext_attr axp20x_pek_startup_ext_attr = {
  110. + .p_time = startup_time,
  111. + .mask = AXP20X_PEK_STARTUP_MASK,
  112. +};
  113. +
  114. +static struct axp20x_pek_ext_attr axp20x_pek_shutdown_ext_attr = {
  115. + .p_time = shutdown_time,
  116. + .mask = AXP20X_PEK_SHUTDOWN_MASK,
  117. +};
  118. +
  119. +static struct axp20x_pek_ext_attr *get_axp_ext_attr(struct device_attribute *attr)
  120. +{
  121. + return container_of(attr, struct dev_ext_attribute, attr)->var;
  122. +}
  123. +
  124. +static ssize_t axp20x_show_ext_attr(struct device *dev, struct device_attribute *attr,
  125. + char *buf)
  126. +{
  127. + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  128. + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
  129. + unsigned int val;
  130. + int ret, i;
  131. +
  132. + ret = regmap_read(axp20x_pek->axp20x->regmap, AXP20X_PEK_KEY, &val);
  133. + if (ret != 0)
  134. + return ret;
  135. +
  136. + val &= axp20x_ea->mask;
  137. + val >>= ffs(axp20x_ea->mask) - 1;
  138. +
  139. + for (i = 0; i < 4; i++)
  140. + if (val == axp20x_ea->p_time[i].idx)
  141. + val = axp20x_ea->p_time[i].time;
  142. +
  143. + return sprintf(buf, "%u\n", val);
  144. +}
  145. +
  146. +static ssize_t axp20x_store_ext_attr(struct device *dev, struct device_attribute *attr,
  147. + const char *buf, size_t count)
  148. +{
  149. + struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev);
  150. + struct axp20x_pek_ext_attr *axp20x_ea = get_axp_ext_attr(attr);
  151. + char val_str[20];
  152. + size_t len;
  153. + int ret, i;
  154. + unsigned int val, idx = 0;
  155. + unsigned int best_err = UINT_MAX;
  156. +
  157. + val_str[sizeof(val_str) - 1] = '\0';
  158. + strncpy(val_str, buf, sizeof(val_str) - 1);
  159. + len = strlen(val_str);
  160. +
  161. + if (len && val_str[len - 1] == '\n')
  162. + val_str[len - 1] = '\0';
  163. +
  164. + ret = kstrtouint(val_str, 10, &val);
  165. + if (ret)
  166. + return ret;
  167. +
  168. + for (i = 3; i >= 0; i--) {
  169. + unsigned int err;
  170. +
  171. + err = abs(axp20x_ea->p_time[i].time - val);
  172. + if (err < best_err) {
  173. + best_err = err;
  174. + idx = axp20x_ea->p_time[i].idx;
  175. + }
  176. +
  177. + if (!err)
  178. + break;
  179. + }
  180. +
  181. + idx <<= ffs(axp20x_ea->mask) - 1;
  182. + ret = regmap_update_bits(axp20x_pek->axp20x->regmap,
  183. + AXP20X_PEK_KEY,
  184. + axp20x_ea->mask, idx);
  185. + if (ret != 0)
  186. + return -EINVAL;
  187. + return count;
  188. +}
  189. +
  190. +static struct dev_ext_attribute axp20x_dev_attr_startup = {
  191. + .attr = __ATTR(startup, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
  192. + .var = &axp20x_pek_startup_ext_attr
  193. +};
  194. +
  195. +static struct dev_ext_attribute axp20x_dev_attr_shutdown = {
  196. + .attr = __ATTR(shutdown, 0644, axp20x_show_ext_attr, axp20x_store_ext_attr),
  197. + .var = &axp20x_pek_shutdown_ext_attr
  198. +};
  199. +
  200. +static irqreturn_t axp20x_pek_irq(int irq, void *pwr)
  201. +{
  202. + struct input_dev *idev = pwr;
  203. + struct axp20x_pek *axp20x_pek = input_get_drvdata(idev);
  204. +
  205. + if (irq == axp20x_pek->irq_dbr)
  206. + input_report_key(idev, KEY_POWER, true);
  207. + else if (irq == axp20x_pek->irq_dbf)
  208. + input_report_key(idev, KEY_POWER, false);
  209. +
  210. + input_sync(idev);
  211. +
  212. + return IRQ_HANDLED;
  213. +}
  214. +
  215. +static int axp20x_pek_probe(struct platform_device *pdev)
  216. +{
  217. + struct axp20x_pek *axp20x_pek;
  218. + struct axp20x_dev *axp20x;
  219. + struct input_dev *idev;
  220. + int error;
  221. +
  222. + axp20x_pek = devm_kzalloc(&pdev->dev, sizeof(struct axp20x_pek),
  223. + GFP_KERNEL);
  224. + if (!axp20x_pek)
  225. + return -ENOMEM;
  226. +
  227. + axp20x_pek->axp20x = dev_get_drvdata(pdev->dev.parent);
  228. + axp20x = axp20x_pek->axp20x;
  229. +
  230. + axp20x_pek->irq_dbr = platform_get_irq_byname(pdev, "PEK_DBR");
  231. + if (axp20x_pek->irq_dbr < 0) {
  232. + dev_err(&pdev->dev, "No IRQ for PEK_DBR, error=%d\n",
  233. + axp20x_pek->irq_dbr);
  234. + return axp20x_pek->irq_dbr;
  235. + }
  236. + axp20x_pek->irq_dbr = regmap_irq_get_virq(axp20x->regmap_irqc,
  237. + axp20x_pek->irq_dbr);
  238. +
  239. + axp20x_pek->irq_dbf = platform_get_irq_byname(pdev, "PEK_DBF");
  240. + if (axp20x_pek->irq_dbf < 0) {
  241. + dev_err(&pdev->dev, "No IRQ for PEK_DBF, error=%d\n",
  242. + axp20x_pek->irq_dbf);
  243. + return axp20x_pek->irq_dbf;
  244. + }
  245. + axp20x_pek->irq_dbf = regmap_irq_get_virq(axp20x->regmap_irqc,
  246. + axp20x_pek->irq_dbf);
  247. +
  248. + axp20x_pek->input = devm_input_allocate_device(&pdev->dev);
  249. + if (!axp20x_pek->input)
  250. + return -ENOMEM;
  251. +
  252. + idev = axp20x_pek->input;
  253. +
  254. + idev->name = "axp20x-pek";
  255. + idev->phys = "m1kbd/input2";
  256. + idev->dev.parent = &pdev->dev;
  257. +
  258. + input_set_capability(idev, EV_KEY, KEY_POWER);
  259. +
  260. + input_set_drvdata(idev, axp20x_pek);
  261. +
  262. + error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbr,
  263. + axp20x_pek_irq, 0,
  264. + "axp20x-pek-dbr", idev);
  265. + if (error < 0) {
  266. + dev_err(axp20x->dev, "Failed to request dbr IRQ#%d: %d\n",
  267. + axp20x_pek->irq_dbr, error);
  268. +
  269. + return error;
  270. + }
  271. +
  272. + error = devm_request_any_context_irq(&pdev->dev, axp20x_pek->irq_dbf,
  273. + axp20x_pek_irq, 0,
  274. + "axp20x-pek-dbf", idev);
  275. + if (error < 0) {
  276. + dev_err(axp20x->dev, "Failed to request dbf IRQ#%d: %d\n",
  277. + axp20x_pek->irq_dbf, error);
  278. + return error;
  279. + }
  280. +
  281. + error = device_create_file(&pdev->dev, &axp20x_dev_attr_startup.attr);
  282. + if (error)
  283. + return error;
  284. +
  285. + error = device_create_file(&pdev->dev, &axp20x_dev_attr_shutdown.attr);
  286. + if (error)
  287. + goto clear_startup_attr;
  288. +
  289. + error = input_register_device(idev);
  290. + if (error) {
  291. + dev_err(axp20x->dev, "Can't register input device: %d\n", error);
  292. + goto clear_attr;
  293. + }
  294. +
  295. + platform_set_drvdata(pdev, axp20x_pek);
  296. +
  297. + return 0;
  298. +
  299. +clear_attr:
  300. + device_remove_file(&pdev->dev, &axp20x_dev_attr_shutdown.attr);
  301. +
  302. +clear_startup_attr:
  303. + device_remove_file(&pdev->dev, &axp20x_dev_attr_startup.attr);
  304. +
  305. + return error;
  306. +}
  307. +
  308. +int axp20x_pek_remove(struct platform_device *pdev)
  309. +{
  310. + device_remove_file(&pdev->dev, &axp20x_dev_attr_shutdown.attr);
  311. + device_remove_file(&pdev->dev, &axp20x_dev_attr_startup.attr);
  312. +
  313. + return 0;
  314. +}
  315. +
  316. +static struct platform_driver axp20x_pek_driver = {
  317. + .probe = axp20x_pek_probe,
  318. + .remove = axp20x_pek_remove,
  319. + .driver = {
  320. + .name = "axp20x-pek",
  321. + .owner = THIS_MODULE,
  322. + },
  323. +};
  324. +module_platform_driver(axp20x_pek_driver);
  325. +
  326. +MODULE_DESCRIPTION("axp20x Power Button");
  327. +MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
  328. +MODULE_LICENSE("GPL");