sun4i-lradc-keys.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Allwinner sun4i low res adc attached tablet keys driver
  3. *
  4. * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. /*
  17. * Allwinnner sunxi SoCs have a lradc which is specifically designed to have
  18. * various (tablet) keys (ie home, back, search, etc). attached to it using
  19. * a resistor network. This driver is for the keys on such boards.
  20. *
  21. * There are 2 channels, currently this driver only supports channel 0 since
  22. * there are no boards known to use channel 1.
  23. */
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/input.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/io.h>
  29. #include <linux/module.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/regulator/consumer.h>
  33. #include <linux/slab.h>
  34. #define LRADC_CTRL 0x00
  35. #define LRADC_INTC 0x04
  36. #define LRADC_INTS 0x08
  37. #define LRADC_DATA0 0x0c
  38. #define LRADC_DATA1 0x10
  39. /* LRADC_CTRL bits */
  40. #define FIRST_CONVERT_DLY(x) ((x) << 24) /* 8 bits */
  41. #define CHAN_SELECT(x) ((x) << 22) /* 2 bits */
  42. #define CONTINUE_TIME_SEL(x) ((x) << 16) /* 4 bits */
  43. #define KEY_MODE_SEL(x) ((x) << 12) /* 2 bits */
  44. #define LEVELA_B_CNT(x) ((x) << 8) /* 4 bits */
  45. #define HOLD_EN(x) ((x) << 6)
  46. #define LEVELB_VOL(x) ((x) << 4) /* 2 bits */
  47. #define SAMPLE_RATE(x) ((x) << 2) /* 2 bits */
  48. #define ENABLE(x) ((x) << 0)
  49. /* LRADC_INTC and LRADC_INTS bits */
  50. #define CHAN1_KEYUP_IRQ BIT(12)
  51. #define CHAN1_ALRDY_HOLD_IRQ BIT(11)
  52. #define CHAN1_HOLD_IRQ BIT(10)
  53. #define CHAN1_KEYDOWN_IRQ BIT(9)
  54. #define CHAN1_DATA_IRQ BIT(8)
  55. #define CHAN0_KEYUP_IRQ BIT(4)
  56. #define CHAN0_ALRDY_HOLD_IRQ BIT(3)
  57. #define CHAN0_HOLD_IRQ BIT(2)
  58. #define CHAN0_KEYDOWN_IRQ BIT(1)
  59. #define CHAN0_DATA_IRQ BIT(0)
  60. struct sun4i_lradc_keymap {
  61. u32 voltage;
  62. u32 keycode;
  63. };
  64. struct sun4i_lradc_data {
  65. struct device *dev;
  66. struct input_dev *input;
  67. void __iomem *base;
  68. struct regulator *vref_supply;
  69. struct sun4i_lradc_keymap *chan0_map;
  70. u32 chan0_map_count;
  71. u32 chan0_keycode;
  72. u32 vref;
  73. };
  74. static irqreturn_t sun4i_lradc_irq(int irq, void *dev_id)
  75. {
  76. struct sun4i_lradc_data *lradc = dev_id;
  77. u32 i, ints, val, voltage, diff, keycode = 0, closest = 0xffffffff;
  78. ints = readl(lradc->base + LRADC_INTS);
  79. /*
  80. * lradc supports only one keypress at a time, release does not give
  81. * any info as to which key was released, so we cache the keycode.
  82. */
  83. if (ints & CHAN0_KEYUP_IRQ) {
  84. input_report_key(lradc->input, lradc->chan0_keycode, 0);
  85. lradc->chan0_keycode = 0;
  86. }
  87. if ((ints & CHAN0_KEYDOWN_IRQ) && lradc->chan0_keycode == 0) {
  88. val = readl(lradc->base + LRADC_DATA0) & 0x3f;
  89. voltage = val * lradc->vref / 63;
  90. for (i = 0; i < lradc->chan0_map_count; i++) {
  91. diff = abs(lradc->chan0_map[i].voltage - voltage);
  92. if (diff < closest) {
  93. closest = diff;
  94. keycode = lradc->chan0_map[i].keycode;
  95. }
  96. }
  97. lradc->chan0_keycode = keycode;
  98. input_report_key(lradc->input, lradc->chan0_keycode, 1);
  99. }
  100. input_sync(lradc->input);
  101. writel(ints, lradc->base + LRADC_INTS);
  102. return IRQ_HANDLED;
  103. }
  104. static int sun4i_lradc_open(struct input_dev *dev)
  105. {
  106. struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  107. int error;
  108. error = regulator_enable(lradc->vref_supply);
  109. if (error)
  110. return error;
  111. /* lradc Vref internally is divided by 2/3 */
  112. lradc->vref = regulator_get_voltage(lradc->vref_supply) * 2 / 3;
  113. /*
  114. * Set sample time to 4 ms / 250 Hz. Wait 2 * 4 ms for key to
  115. * stabilize on press, wait (1 + 1) * 4 ms for key release
  116. */
  117. writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  118. SAMPLE_RATE(0) | ENABLE(1), lradc->base + LRADC_CTRL);
  119. writel(CHAN0_KEYUP_IRQ | CHAN0_KEYDOWN_IRQ, lradc->base + LRADC_INTC);
  120. return 0;
  121. }
  122. static void sun4i_lradc_close(struct input_dev *dev)
  123. {
  124. struct sun4i_lradc_data *lradc = input_get_drvdata(dev);
  125. /* Disable lradc, leave other settings unchanged */
  126. writel(FIRST_CONVERT_DLY(2) | LEVELA_B_CNT(1) | HOLD_EN(1) |
  127. SAMPLE_RATE(2), lradc->base + LRADC_CTRL);
  128. writel(0, lradc->base + LRADC_INTC);
  129. regulator_disable(lradc->vref_supply);
  130. }
  131. static int sun4i_lradc_load_dt_keymap(struct device *dev,
  132. struct sun4i_lradc_data *lradc)
  133. {
  134. struct device_node *np, *pp;
  135. int i;
  136. int error;
  137. np = dev->of_node;
  138. if (!np)
  139. return -EINVAL;
  140. lradc->chan0_map_count = of_get_child_count(np);
  141. if (lradc->chan0_map_count == 0) {
  142. dev_err(dev, "keymap is missing in device tree\n");
  143. return -EINVAL;
  144. }
  145. lradc->chan0_map = devm_kmalloc_array(dev, lradc->chan0_map_count,
  146. sizeof(struct sun4i_lradc_keymap),
  147. GFP_KERNEL);
  148. if (!lradc->chan0_map)
  149. return -ENOMEM;
  150. i = 0;
  151. for_each_child_of_node(np, pp) {
  152. struct sun4i_lradc_keymap *map = &lradc->chan0_map[i];
  153. u32 channel;
  154. error = of_property_read_u32(pp, "channel", &channel);
  155. if (error || channel != 0) {
  156. dev_err(dev, "%s: Inval channel prop\n", pp->name);
  157. return -EINVAL;
  158. }
  159. error = of_property_read_u32(pp, "voltage", &map->voltage);
  160. if (error) {
  161. dev_err(dev, "%s: Inval voltage prop\n", pp->name);
  162. return -EINVAL;
  163. }
  164. error = of_property_read_u32(pp, "linux,code", &map->keycode);
  165. if (error) {
  166. dev_err(dev, "%s: Inval linux,code prop\n", pp->name);
  167. return -EINVAL;
  168. }
  169. i++;
  170. }
  171. return 0;
  172. }
  173. static int sun4i_lradc_probe(struct platform_device *pdev)
  174. {
  175. struct sun4i_lradc_data *lradc;
  176. struct device *dev = &pdev->dev;
  177. int i;
  178. int error;
  179. lradc = devm_kzalloc(dev, sizeof(struct sun4i_lradc_data), GFP_KERNEL);
  180. if (!lradc)
  181. return -ENOMEM;
  182. error = sun4i_lradc_load_dt_keymap(dev, lradc);
  183. if (error)
  184. return error;
  185. lradc->vref_supply = devm_regulator_get(dev, "vref");
  186. if (IS_ERR(lradc->vref_supply))
  187. return PTR_ERR(lradc->vref_supply);
  188. lradc->dev = dev;
  189. lradc->input = devm_input_allocate_device(dev);
  190. if (!lradc->input)
  191. return -ENOMEM;
  192. lradc->input->name = pdev->name;
  193. lradc->input->phys = "sun4i_lradc/input0";
  194. lradc->input->open = sun4i_lradc_open;
  195. lradc->input->close = sun4i_lradc_close;
  196. lradc->input->id.bustype = BUS_HOST;
  197. lradc->input->id.vendor = 0x0001;
  198. lradc->input->id.product = 0x0001;
  199. lradc->input->id.version = 0x0100;
  200. __set_bit(EV_KEY, lradc->input->evbit);
  201. for (i = 0; i < lradc->chan0_map_count; i++)
  202. __set_bit(lradc->chan0_map[i].keycode, lradc->input->keybit);
  203. input_set_drvdata(lradc->input, lradc);
  204. lradc->base = devm_ioremap_resource(dev,
  205. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  206. if (IS_ERR(lradc->base))
  207. return PTR_ERR(lradc->base);
  208. error = devm_request_irq(dev, platform_get_irq(pdev, 0),
  209. sun4i_lradc_irq, 0,
  210. "sun4i-a10-lradc-keys", lradc);
  211. if (error)
  212. return error;
  213. error = input_register_device(lradc->input);
  214. if (error)
  215. return error;
  216. platform_set_drvdata(pdev, lradc);
  217. return 0;
  218. }
  219. static const struct of_device_id sun4i_lradc_of_match[] = {
  220. { .compatible = "allwinner,sun4i-a10-lradc-keys", },
  221. { /* sentinel */ }
  222. };
  223. MODULE_DEVICE_TABLE(of, sun4i_lradc_of_match);
  224. static struct platform_driver sun4i_lradc_driver = {
  225. .driver = {
  226. .name = "sun4i-a10-lradc-keys",
  227. .of_match_table = of_match_ptr(sun4i_lradc_of_match),
  228. },
  229. .probe = sun4i_lradc_probe,
  230. };
  231. module_platform_driver(sun4i_lradc_driver);
  232. MODULE_DESCRIPTION("Allwinner sun4i low res adc attached tablet keys driver");
  233. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  234. MODULE_LICENSE("GPL");