adc-keys.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Input driver for resistor ladder connected on ADC
  3. *
  4. * Copyright (c) 2016 Alexandre Belloni
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #include <linux/err.h>
  11. #include <linux/iio/consumer.h>
  12. #include <linux/iio/types.h>
  13. #include <linux/input.h>
  14. #include <linux/input-polldev.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/property.h>
  20. #include <linux/slab.h>
  21. struct adc_keys_button {
  22. u32 voltage;
  23. u32 keycode;
  24. };
  25. struct adc_keys_state {
  26. struct iio_channel *channel;
  27. u32 num_keys;
  28. u32 last_key;
  29. u32 keyup_voltage;
  30. const struct adc_keys_button *map;
  31. };
  32. static void adc_keys_poll(struct input_polled_dev *dev)
  33. {
  34. struct adc_keys_state *st = dev->private;
  35. int i, value, ret;
  36. u32 diff, closest = 0xffffffff;
  37. int keycode = 0;
  38. ret = iio_read_channel_processed(st->channel, &value);
  39. if (unlikely(ret < 0)) {
  40. /* Forcibly release key if any was pressed */
  41. value = st->keyup_voltage;
  42. } else {
  43. for (i = 0; i < st->num_keys; i++) {
  44. diff = abs(st->map[i].voltage - value);
  45. if (diff < closest) {
  46. closest = diff;
  47. keycode = st->map[i].keycode;
  48. }
  49. }
  50. }
  51. if (abs(st->keyup_voltage - value) < closest)
  52. keycode = 0;
  53. if (st->last_key && st->last_key != keycode)
  54. input_report_key(dev->input, st->last_key, 0);
  55. if (keycode)
  56. input_report_key(dev->input, keycode, 1);
  57. input_sync(dev->input);
  58. st->last_key = keycode;
  59. }
  60. static int adc_keys_load_keymap(struct device *dev, struct adc_keys_state *st)
  61. {
  62. struct adc_keys_button *map;
  63. struct fwnode_handle *child;
  64. int i;
  65. st->num_keys = device_get_child_node_count(dev);
  66. if (st->num_keys == 0) {
  67. dev_err(dev, "keymap is missing\n");
  68. return -EINVAL;
  69. }
  70. map = devm_kmalloc_array(dev, st->num_keys, sizeof(*map), GFP_KERNEL);
  71. if (!map)
  72. return -ENOMEM;
  73. i = 0;
  74. device_for_each_child_node(dev, child) {
  75. if (fwnode_property_read_u32(child, "press-threshold-microvolt",
  76. &map[i].voltage)) {
  77. dev_err(dev, "Key with invalid or missing voltage\n");
  78. fwnode_handle_put(child);
  79. return -EINVAL;
  80. }
  81. map[i].voltage /= 1000;
  82. if (fwnode_property_read_u32(child, "linux,code",
  83. &map[i].keycode)) {
  84. dev_err(dev, "Key with invalid or missing linux,code\n");
  85. fwnode_handle_put(child);
  86. return -EINVAL;
  87. }
  88. i++;
  89. }
  90. st->map = map;
  91. return 0;
  92. }
  93. static int adc_keys_probe(struct platform_device *pdev)
  94. {
  95. struct device *dev = &pdev->dev;
  96. struct adc_keys_state *st;
  97. struct input_polled_dev *poll_dev;
  98. struct input_dev *input;
  99. enum iio_chan_type type;
  100. int i, value;
  101. int error;
  102. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  103. if (!st)
  104. return -ENOMEM;
  105. st->channel = devm_iio_channel_get(dev, "buttons");
  106. if (IS_ERR(st->channel))
  107. return PTR_ERR(st->channel);
  108. if (!st->channel->indio_dev)
  109. return -ENXIO;
  110. error = iio_get_channel_type(st->channel, &type);
  111. if (error < 0)
  112. return error;
  113. if (type != IIO_VOLTAGE) {
  114. dev_err(dev, "Incompatible channel type %d\n", type);
  115. return -EINVAL;
  116. }
  117. if (device_property_read_u32(dev, "keyup-threshold-microvolt",
  118. &st->keyup_voltage)) {
  119. dev_err(dev, "Invalid or missing keyup voltage\n");
  120. return -EINVAL;
  121. }
  122. st->keyup_voltage /= 1000;
  123. error = adc_keys_load_keymap(dev, st);
  124. if (error)
  125. return error;
  126. poll_dev = devm_input_allocate_polled_device(dev);
  127. if (!poll_dev) {
  128. dev_err(dev, "failed to allocate input device\n");
  129. return -ENOMEM;
  130. }
  131. if (!device_property_read_u32(dev, "poll-interval", &value))
  132. poll_dev->poll_interval = value;
  133. poll_dev->poll = adc_keys_poll;
  134. poll_dev->private = st;
  135. input = poll_dev->input;
  136. input->name = pdev->name;
  137. input->phys = "adc-keys/input0";
  138. input->id.bustype = BUS_HOST;
  139. input->id.vendor = 0x0001;
  140. input->id.product = 0x0001;
  141. input->id.version = 0x0100;
  142. __set_bit(EV_KEY, input->evbit);
  143. for (i = 0; i < st->num_keys; i++)
  144. __set_bit(st->map[i].keycode, input->keybit);
  145. if (device_property_read_bool(dev, "autorepeat"))
  146. __set_bit(EV_REP, input->evbit);
  147. error = input_register_polled_device(poll_dev);
  148. if (error) {
  149. dev_err(dev, "Unable to register input device: %d\n", error);
  150. return error;
  151. }
  152. return 0;
  153. }
  154. #ifdef CONFIG_OF
  155. static const struct of_device_id adc_keys_of_match[] = {
  156. { .compatible = "adc-keys", },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(of, adc_keys_of_match);
  160. #endif
  161. static struct platform_driver __refdata adc_keys_driver = {
  162. .driver = {
  163. .name = "adc_keys",
  164. .of_match_table = of_match_ptr(adc_keys_of_match),
  165. },
  166. .probe = adc_keys_probe,
  167. };
  168. module_platform_driver(adc_keys_driver);
  169. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  170. MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
  171. MODULE_LICENSE("GPL v2");