adc-keys.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. int i;
  60. st->num_keys = device_get_child_node_count(dev);
  61. if (st->num_keys == 0) {
  62. dev_err(dev, "keymap is missing\n");
  63. return -EINVAL;
  64. }
  65. map = devm_kmalloc_array(dev, st->num_keys, sizeof(*map), GFP_KERNEL);
  66. if (!map)
  67. return -ENOMEM;
  68. i = 0;
  69. device_for_each_child_node_scoped(dev, child) {
  70. if (fwnode_property_read_u32(child, "press-threshold-microvolt",
  71. &map[i].voltage)) {
  72. dev_err(dev, "Key with invalid or missing voltage\n");
  73. return -EINVAL;
  74. }
  75. map[i].voltage /= 1000;
  76. if (fwnode_property_read_u32(child, "linux,code",
  77. &map[i].keycode)) {
  78. dev_err(dev, "Key with invalid or missing linux,code\n");
  79. return -EINVAL;
  80. }
  81. i++;
  82. }
  83. st->map = map;
  84. return 0;
  85. }
  86. static int adc_keys_probe(struct platform_device *pdev)
  87. {
  88. struct device *dev = &pdev->dev;
  89. struct adc_keys_state *st;
  90. struct input_dev *input;
  91. enum iio_chan_type type;
  92. int i, value;
  93. int error;
  94. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  95. if (!st)
  96. return -ENOMEM;
  97. st->channel = devm_iio_channel_get(dev, "buttons");
  98. if (IS_ERR(st->channel))
  99. return PTR_ERR(st->channel);
  100. if (!st->channel->indio_dev)
  101. return -ENXIO;
  102. error = iio_get_channel_type(st->channel, &type);
  103. if (error < 0)
  104. return error;
  105. if (type != IIO_VOLTAGE) {
  106. dev_err(dev, "Incompatible channel type %d\n", type);
  107. return -EINVAL;
  108. }
  109. if (device_property_read_u32(dev, "keyup-threshold-microvolt",
  110. &st->keyup_voltage)) {
  111. dev_err(dev, "Invalid or missing keyup voltage\n");
  112. return -EINVAL;
  113. }
  114. st->keyup_voltage /= 1000;
  115. error = adc_keys_load_keymap(dev, st);
  116. if (error)
  117. return error;
  118. input = devm_input_allocate_device(dev);
  119. if (!input) {
  120. dev_err(dev, "failed to allocate input device\n");
  121. return -ENOMEM;
  122. }
  123. input_set_drvdata(input, st);
  124. input->name = pdev->name;
  125. input->phys = "adc-keys/input0";
  126. input->id.bustype = BUS_HOST;
  127. input->id.vendor = 0x0001;
  128. input->id.product = 0x0001;
  129. input->id.version = 0x0100;
  130. __set_bit(EV_KEY, input->evbit);
  131. for (i = 0; i < st->num_keys; i++)
  132. __set_bit(st->map[i].keycode, input->keybit);
  133. if (device_property_read_bool(dev, "autorepeat"))
  134. __set_bit(EV_REP, input->evbit);
  135. error = input_setup_polling(input, adc_keys_poll);
  136. if (error) {
  137. dev_err(dev, "Unable to set up polling: %d\n", error);
  138. return error;
  139. }
  140. if (!device_property_read_u32(dev, "poll-interval", &value))
  141. input_set_poll_interval(input, value);
  142. error = input_register_device(input);
  143. if (error) {
  144. dev_err(dev, "Unable to register input device: %d\n", error);
  145. return error;
  146. }
  147. return 0;
  148. }
  149. #ifdef CONFIG_OF
  150. static const struct of_device_id adc_keys_of_match[] = {
  151. { .compatible = "adc-keys", },
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(of, adc_keys_of_match);
  155. #endif
  156. static struct platform_driver adc_keys_driver = {
  157. .driver = {
  158. .name = "adc_keys",
  159. .of_match_table = of_match_ptr(adc_keys_of_match),
  160. },
  161. .probe = adc_keys_probe,
  162. };
  163. module_platform_driver(adc_keys_driver);
  164. MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
  165. MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
  166. MODULE_LICENSE("GPL v2");