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