adc-keys.c 4.6 KB

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