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

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