110-input-add-sun4i-lradc.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. From 2c1fab89e83245a520871e807e233e66cbdb7c57 Mon Sep 17 00:00:00 2001
  2. From: Hans de Goede <hdegoede@redhat.com>
  3. Date: Wed, 1 Jan 2014 19:44:49 +0100
  4. Subject: [PATCH] input: Add new sun4i-lradc-keys driver
  5. Allwinnner sunxi SoCs have a low resolution adc (called lradc) which is
  6. specifically designed to have various (tablet) keys (ie home, back, search,
  7. etc). attached to it using a resistor network. This adds a driver for this.
  8. There are 2 channels, currently this driver only supports chan0 since there
  9. are no boards known to use chan1.
  10. This has been tested on an olimex a10s-olinuxino-micro, a13-olinuxino, and
  11. a20-olinuxino-micro.
  12. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
  13. --
  14. Changes in v2:
  15. -Change devicetree bindings to use a per key subnode, like gpio-keys does
  16. ---
  17. .../devicetree/bindings/input/sun4i-lradc-keys.txt | 62 +++++
  18. MAINTAINERS | 7 +
  19. drivers/input/keyboard/Kconfig | 10 +
  20. drivers/input/keyboard/Makefile | 1 +
  21. drivers/input/keyboard/sun4i-lradc-keys.c | 258 +++++++++++++++++++++
  22. 5 files changed, 338 insertions(+)
  23. create mode 100644 Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
  24. create mode 100644 drivers/input/keyboard/sun4i-lradc-keys.c
  25. --- /dev/null
  26. +++ b/Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
  27. @@ -0,0 +1,62 @@
  28. +Allwinner sun4i low res adc attached tablet keys
  29. +------------------------------------------------
  30. +
  31. +Required properties:
  32. + - compatible: "allwinner,sun4i-a10-lradc-keys"
  33. + - reg: mmio address range of the chip
  34. + - interrupts: interrupt to which the chip is connected
  35. + - vref-supply: powersupply for the lradc reference voltage
  36. +
  37. +Each key is represented as a sub-node of "allwinner,sun4i-a10-lradc-keys":
  38. +
  39. +Required subnode-properties:
  40. + - label: Descriptive name of the key.
  41. + - linux,code: Keycode to emit.
  42. + - channel: Channel this key is attached to, mut be 0 or 1.
  43. + - voltage: Voltage in µV at lradc input when this key is pressed.
  44. +
  45. +Example:
  46. +
  47. +#include <dt-bindings/input/input.h>
  48. +
  49. + lradc: lradc@01c22800 {
  50. + compatible = "allwinner,sun4i-a10-lradc-keys";
  51. + reg = <0x01c22800 0x100>;
  52. + interrupts = <31>;
  53. + vref-supply = <&reg_vcc3v0>;
  54. +
  55. + button@191 {
  56. + label = "Volume Up";
  57. + linux,code = <KEY_VOLUMEUP>;
  58. + channel = <0>;
  59. + voltage = <191274>;
  60. + };
  61. +
  62. + button@392 {
  63. + label = "Volume Down";
  64. + linux,code = <KEY_VOLUMEDOWN>;
  65. + channel = <0>;
  66. + voltage = <392644>;
  67. + };
  68. +
  69. + button@601 {
  70. + label = "Menu";
  71. + linux,code = <KEY_MENU>;
  72. + channel = <0>;
  73. + voltage = <601151>;
  74. + };
  75. +
  76. + button@795 {
  77. + label = "Enter";
  78. + linux,code = <KEY_ENTER>;
  79. + channel = <0>;
  80. + voltage = <795090>;
  81. + };
  82. +
  83. + button@987 {
  84. + label = "Home";
  85. + linux,code = <KEY_HOMEPAGE>;
  86. + channel = <0>;
  87. + voltage = <987387>;
  88. + };
  89. + };
  90. --- a/MAINTAINERS
  91. +++ b/MAINTAINERS
  92. @@ -9002,6 +9002,13 @@ F: arch/m68k/sun3*/
  93. F: arch/m68k/include/asm/sun3*
  94. F: drivers/net/ethernet/i825xx/sun3*
  95. +SUN4I LOW RES ADC ATTACHED TABLET KEYS DRIVER
  96. +M: Hans de Goede <hdegoede@redhat.com>
  97. +L: linux-input@vger.kernel.org
  98. +S: Maintained
  99. +F: Documentation/devicetree/bindings/input/sun4i-lradc-keys.txt
  100. +F: drivers/input/keyboard/sun4i-lradc-keys.c
  101. +
  102. SUNDANCE NETWORK DRIVER
  103. M: Denis Kirjanov <kda@linux-powerpc.org>
  104. L: netdev@vger.kernel.org
  105. --- a/drivers/input/keyboard/Kconfig
  106. +++ b/drivers/input/keyboard/Kconfig
  107. @@ -567,6 +567,16 @@ config KEYBOARD_STMPE
  108. To compile this driver as a module, choose M here: the module will be
  109. called stmpe-keypad.
  110. +config KEYBOARD_SUN4I_LRADC
  111. + tristate "Allwinner sun4i low res adc attached tablet keys support"
  112. + depends on ARCH_SUNXI
  113. + help
  114. + This selects support for the Allwinner low res adc attached tablet
  115. + keys found on Allwinner sunxi SoCs.
  116. +
  117. + To compile this driver as a module, choose M here: the
  118. + module will be called sun4i-lradc-keys.
  119. +
  120. config KEYBOARD_DAVINCI
  121. tristate "TI DaVinci Key Scan"
  122. depends on ARCH_DAVINCI_DM365
  123. --- a/drivers/input/keyboard/Makefile
  124. +++ b/drivers/input/keyboard/Makefile
  125. @@ -53,6 +53,7 @@ obj-$(CONFIG_KEYBOARD_SPEAR) += spear-k
  126. obj-$(CONFIG_KEYBOARD_STMPE) += stmpe-keypad.o
  127. obj-$(CONFIG_KEYBOARD_STOWAWAY) += stowaway.o
  128. obj-$(CONFIG_KEYBOARD_ST_KEYSCAN) += st-keyscan.o
  129. +obj-$(CONFIG_KEYBOARD_SUN4I_LRADC) += sun4i-lradc-keys.o
  130. obj-$(CONFIG_KEYBOARD_SUNKBD) += sunkbd.o
  131. obj-$(CONFIG_KEYBOARD_TC3589X) += tc3589x-keypad.o
  132. obj-$(CONFIG_KEYBOARD_TEGRA) += tegra-kbc.o
  133. --- /dev/null
  134. +++ b/drivers/input/keyboard/sun4i-lradc-keys.c
  135. @@ -0,0 +1,258 @@
  136. +/*
  137. + * Allwinner sun4i low res adc attached tablet keys driver
  138. + *
  139. + * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
  140. + *
  141. + * This program is free software; you can redistribute it and/or modify
  142. + * it under the terms of the GNU General Public License as published by
  143. + * the Free Software Foundation; either version 2 of the License, or
  144. + * (at your option) any later version.
  145. + *
  146. + * This program is distributed in the hope that it will be useful,
  147. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  148. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  149. + * GNU General Public License for more details.
  150. + */
  151. +
  152. +/*
  153. + * Allwinnner sunxi SoCs have a lradc which is specifically designed to have
  154. + * various (tablet) keys (ie home, back, search, etc). attached to it using
  155. + * a resistor network. This driver is for the keys on such boards.
  156. + *
  157. + * There are 2 channels, currently this driver only supports channel 0 since
  158. + * there are no boards known to use channel 1.
  159. + */
  160. +
  161. +#include <linux/err.h>
  162. +#include <linux/init.h>
  163. +#include <linux/input.h>
  164. +#include <linux/interrupt.h>
  165. +#include <linux/io.h>
  166. +#include <linux/module.h>
  167. +#include <linux/of_platform.h>
  168. +#include <linux/platform_device.h>
  169. +#include <linux/regulator/consumer.h>
  170. +#include <linux/slab.h>
  171. +
  172. +#define LRADC_CTRL 0x00
  173. +#define LRADC_INTC 0x04
  174. +#define LRADC_INTS 0x08
  175. +#define LRADC_DATA0 0x0c
  176. +#define LRADC_DATA1 0x10
  177. +
  178. +/* LRADC_CTRL bits */
  179. +#define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */
  180. +#define CHAN_SELECT(x) ((x) << 22) /* 2 bits */
  181. +#define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */
  182. +#define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */
  183. +#define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */
  184. +#define HOLD_EN(x) ((x) << 6)
  185. +#define LEVELB_VOL(x) ((x) << 4) /* 2 bits */
  186. +#define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */
  187. +#define ENABLE(x) ((x) << 0)
  188. +
  189. +/* LRADC_INTC and LRADC_INTS bits */
  190. +#define CHAN1_KEYUP_IRQ BIT(12)
  191. +#define CHAN1_ALRDY_HOLD_IRQ BIT(11)
  192. +#define CHAN1_HOLD_IRQ BIT(10)
  193. +#define CHAN1_KEYDOWN_IRQ BIT(9)
  194. +#define CHAN1_DATA_IRQ BIT(8)
  195. +#define CHAN0_KEYUP_IRQ BIT(4)
  196. +#define CHAN0_ALRDY_HOLD_IRQ BIT(3)
  197. +#define CHAN0_HOLD_IRQ BIT(2)
  198. +#define CHAN0_KEYDOWN_IRQ BIT(1)
  199. +#define CHAN0_DATA_IRQ BIT(0)
  200. +
  201. +struct sun4i_lradc_keymap {
  202. + u32 voltage;
  203. + u32 keycode;
  204. +};
  205. +
  206. +struct sun4i_lradc_data {
  207. + struct device *dev;
  208. + struct input_dev *input;
  209. + void __iomem *base;
  210. + struct regulator *vref_supply;
  211. + struct sun4i_lradc_keymap *chan0_map;
  212. + u32 chan0_map_count;
  213. + u32 chan0_keycode;
  214. + u32 vref;
  215. +};
  216. +
  217. +static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
  218. +{
  219. + struct sun4i_lradc_data *lradc = dev_id;
  220. + u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff;
  221. +
  222. + ints = readl(lradc->base + LRADC_INTS);
  223. +
  224. + /*
  225. + * lradc supports only one keypress at a time, release does not give
  226. + * any info as to which key was released, so we cache the keycode.
  227. + */
  228. + if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
  229. + val = readl(lradc->base + LRADC_DATA0) & 0x3f;
  230. + voltage = val * lradc->vref / 63;
  231. +
  232. + for (i = 0; i < lradc->chan0_map_count; i++) {
  233. + diff = abs(lradc->chan0_map[i].voltage - voltage);
  234. + if (diff < closest) {
  235. + closest = diff;
  236. + keycode = lradc->chan0_map[i].keycode;
  237. + }
  238. + }
  239. +
  240. + lradc->chan0_keycode = keycode;
  241. + input_report_key(lradc->input, lradc->chan0_keycode, 1);
  242. + }
  243. +
  244. + if (ints & CHAN0_KEYUP_IRQ) {
  245. + input_report_key(lradc->input, lradc->chan0_keycode, 0);
  246. + lradc->chan0_keycode = 0;
  247. + }
  248. +
  249. + input_sync(lradc->input);
  250. +
  251. + writel(ints, lradc->base + LRADC_INTS);
  252. +
  253. + return IRQ_HANDLED;
  254. +}
  255. +
  256. +static int sun4i_lradc_open(struct input_dev *dev)
  257. +{
  258. + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  259. + int ret;
  260. +
  261. + ret = regulator_enable(lradc->vref_supply);
  262. + if (ret)
  263. + return ret;
  264. +
  265. + /* lradc Vref internally is divided by 2/3 */
  266. + lradc->vref = regulator_get_voltage(lradc->vref_supply) * 2 / 3;
  267. +
  268. + /*
  269. + * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to
  270. + * stabilize on press, wait (1 + 1) * 4 ms for key release
  271. + */
  272. + writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  273. + SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL);
  274. +
  275. + writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
  276. +
  277. + return 0;
  278. +}
  279. +
  280. +static void sun4i_lradc_close(struct input_dev *dev)
  281. +{
  282. + struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  283. +
  284. + /* Disable lradc, leave other settings unchanged */
  285. + writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  286. + SAMPLE_RATE(2), lradc->base + LRADC_CTRL);
  287. + writel(0, lradc->base + LRADC_INTC);
  288. +
  289. + regulator_disable(lradc->vref_supply);
  290. +}
  291. +
  292. +static int sun4i_lradc_probe(struct platform_device *pdev)
  293. +{
  294. + struct sun4i_lradc_data *lradc;
  295. + struct device *dev = &pdev->dev;
  296. + struct device_node *pp, *np = dev->of_node;
  297. + u32 channel;
  298. + int i, ret;
  299. +
  300. + lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
  301. + if (!lradc)
  302. + return -ENOMEM;
  303. +
  304. + lradc->chan0_map_count = of_get_child_count(np);
  305. + lradc->chan0_map = devm_kmalloc(dev, lradc->chan0_map_count *
  306. + sizeof(struct sun4i_lradc_keymap), GFP_KERNEL);
  307. + if (!lradc->chan0_map)
  308. + return -ENOMEM;
  309. +
  310. + i = 0;
  311. + for_each_child_of_node(np, pp) {
  312. + struct sun4i_lradc_keymap *map = &lradc->chan0_map[i];
  313. +
  314. + ret = of_property_read_u32(pp, "channel", &channel);
  315. + if (ret || channel != 0) {
  316. + dev_err(dev, "%s: Inval channel prop\n", pp->name);
  317. + return -EINVAL;
  318. + }
  319. +
  320. + ret = of_property_read_u32(pp, "voltage", &map->voltage);
  321. + if (ret) {
  322. + dev_err(dev, "%s: Inval voltage prop\n", pp->name);
  323. + return -EINVAL;
  324. + }
  325. +
  326. + ret = of_property_read_u32(pp, "linux,code", &map->keycode);
  327. + if (ret) {
  328. + dev_err(dev, "%s: Inval linux,code prop\n", pp->name);
  329. + return -EINVAL;
  330. + }
  331. +
  332. + i++;
  333. + }
  334. +
  335. + lradc->vref_supply = devm_regulator_get(dev, "vref");
  336. + if (IS_ERR(lradc->vref_supply))
  337. + return PTR_ERR(lradc->vref_supply);
  338. +
  339. + lradc->dev = dev;
  340. + lradc->input = devm_input_allocate_device(dev);
  341. + if (!lradc->input)
  342. + return -ENOMEM;
  343. +
  344. + lradc->input->name = pdev->name;
  345. + lradc->input->phys = "sun4i_lradc/input0";
  346. + lradc->input->open = sun4i_lradc_open;
  347. + lradc->input->close = sun4i_lradc_close;
  348. + lradc->input->id.bustype = BUS_HOST;
  349. + lradc->input->id.vendor = 0x0001;
  350. + lradc->input->id.product = 0x0001;
  351. + lradc->input->id.version = 0x0100;
  352. + lradc->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY);
  353. + for (i = 0; i < lradc->chan0_map_count; i++)
  354. + set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit);
  355. + input_set_drvdata(lradc->input, lradc);
  356. +
  357. + lradc->base = devm_ioremap_resource(dev,
  358. + platform_get_resource(pdev, IORESOURCE_MEM, 0));
  359. + if (IS_ERR(lradc->base))
  360. + return PTR_ERR(lradc->base);
  361. +
  362. + ret = devm_request_irq(dev, platform_get_irq(pdev, 0), sun4i_lradc_irq,
  363. + 0, "sun4i-a10-lradc-keys", lradc);
  364. + if (ret)
  365. + return ret;
  366. +
  367. + ret = input_register_device(lradc->input);
  368. + if (ret)
  369. + return ret;
  370. +
  371. + platform_set_drvdata(pdev, lradc);
  372. + return 0;
  373. +}
  374. +
  375. +static const struct of_device_id sun4i_lradc_of_match[] = {
  376. + { .compatible = "allwinner,sun4i-a10-lradc-keys", },
  377. + { /* sentinel */ }
  378. +};
  379. +MODULE_DEVICE_TABLE(of, sun4i_lradc_of_match);
  380. +
  381. +static struct platform_driver sun4i_lradc_driver = {
  382. + .driver = {
  383. + .name = "sun4i-a10-lradc-keys",
  384. + .of_match_table = of_match_ptr(sun4i_lradc_of_match),
  385. + },
  386. + .probe = sun4i_lradc_probe,
  387. +};
  388. +
  389. +module_platform_driver(sun4i_lradc_driver);
  390. +
  391. +MODULE_DESCRIPTION("Allwinner sun4i low res adc attached tablet keys driver");
  392. +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  393. +MODULE_LICENSE("GPL");