211-input-add-axp20x-power-enable-key-support.patch 9.1 KB

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