0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/err.h>
0009 #include <linux/iio/consumer.h>
0010 #include <linux/iio/types.h>
0011 #include <linux/input.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/property.h>
0017 #include <linux/slab.h>
0018
0019 struct adc_keys_button {
0020 u32 voltage;
0021 u32 keycode;
0022 };
0023
0024 struct adc_keys_state {
0025 struct iio_channel *channel;
0026 u32 num_keys;
0027 u32 last_key;
0028 u32 keyup_voltage;
0029 const struct adc_keys_button *map;
0030 };
0031
0032 static void adc_keys_poll(struct input_dev *input)
0033 {
0034 struct adc_keys_state *st = input_get_drvdata(input);
0035 int i, value, ret;
0036 u32 diff, closest = 0xffffffff;
0037 int keycode = 0;
0038
0039 ret = iio_read_channel_processed(st->channel, &value);
0040 if (unlikely(ret < 0)) {
0041
0042 value = st->keyup_voltage;
0043 } else {
0044 for (i = 0; i < st->num_keys; i++) {
0045 diff = abs(st->map[i].voltage - value);
0046 if (diff < closest) {
0047 closest = diff;
0048 keycode = st->map[i].keycode;
0049 }
0050 }
0051 }
0052
0053 if (abs(st->keyup_voltage - value) < closest)
0054 keycode = 0;
0055
0056 if (st->last_key && st->last_key != keycode)
0057 input_report_key(input, st->last_key, 0);
0058
0059 if (keycode)
0060 input_report_key(input, keycode, 1);
0061
0062 input_sync(input);
0063 st->last_key = keycode;
0064 }
0065
0066 static int adc_keys_load_keymap(struct device *dev, struct adc_keys_state *st)
0067 {
0068 struct adc_keys_button *map;
0069 struct fwnode_handle *child;
0070 int i;
0071
0072 st->num_keys = device_get_child_node_count(dev);
0073 if (st->num_keys == 0) {
0074 dev_err(dev, "keymap is missing\n");
0075 return -EINVAL;
0076 }
0077
0078 map = devm_kmalloc_array(dev, st->num_keys, sizeof(*map), GFP_KERNEL);
0079 if (!map)
0080 return -ENOMEM;
0081
0082 i = 0;
0083 device_for_each_child_node(dev, child) {
0084 if (fwnode_property_read_u32(child, "press-threshold-microvolt",
0085 &map[i].voltage)) {
0086 dev_err(dev, "Key with invalid or missing voltage\n");
0087 fwnode_handle_put(child);
0088 return -EINVAL;
0089 }
0090 map[i].voltage /= 1000;
0091
0092 if (fwnode_property_read_u32(child, "linux,code",
0093 &map[i].keycode)) {
0094 dev_err(dev, "Key with invalid or missing linux,code\n");
0095 fwnode_handle_put(child);
0096 return -EINVAL;
0097 }
0098
0099 i++;
0100 }
0101
0102 st->map = map;
0103 return 0;
0104 }
0105
0106 static int adc_keys_probe(struct platform_device *pdev)
0107 {
0108 struct device *dev = &pdev->dev;
0109 struct adc_keys_state *st;
0110 struct input_dev *input;
0111 enum iio_chan_type type;
0112 int i, value;
0113 int error;
0114
0115 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
0116 if (!st)
0117 return -ENOMEM;
0118
0119 st->channel = devm_iio_channel_get(dev, "buttons");
0120 if (IS_ERR(st->channel))
0121 return PTR_ERR(st->channel);
0122
0123 if (!st->channel->indio_dev)
0124 return -ENXIO;
0125
0126 error = iio_get_channel_type(st->channel, &type);
0127 if (error < 0)
0128 return error;
0129
0130 if (type != IIO_VOLTAGE) {
0131 dev_err(dev, "Incompatible channel type %d\n", type);
0132 return -EINVAL;
0133 }
0134
0135 if (device_property_read_u32(dev, "keyup-threshold-microvolt",
0136 &st->keyup_voltage)) {
0137 dev_err(dev, "Invalid or missing keyup voltage\n");
0138 return -EINVAL;
0139 }
0140 st->keyup_voltage /= 1000;
0141
0142 error = adc_keys_load_keymap(dev, st);
0143 if (error)
0144 return error;
0145
0146 input = devm_input_allocate_device(dev);
0147 if (!input) {
0148 dev_err(dev, "failed to allocate input device\n");
0149 return -ENOMEM;
0150 }
0151
0152 input_set_drvdata(input, st);
0153
0154 input->name = pdev->name;
0155 input->phys = "adc-keys/input0";
0156
0157 input->id.bustype = BUS_HOST;
0158 input->id.vendor = 0x0001;
0159 input->id.product = 0x0001;
0160 input->id.version = 0x0100;
0161
0162 __set_bit(EV_KEY, input->evbit);
0163 for (i = 0; i < st->num_keys; i++)
0164 __set_bit(st->map[i].keycode, input->keybit);
0165
0166 if (device_property_read_bool(dev, "autorepeat"))
0167 __set_bit(EV_REP, input->evbit);
0168
0169
0170 error = input_setup_polling(input, adc_keys_poll);
0171 if (error) {
0172 dev_err(dev, "Unable to set up polling: %d\n", error);
0173 return error;
0174 }
0175
0176 if (!device_property_read_u32(dev, "poll-interval", &value))
0177 input_set_poll_interval(input, value);
0178
0179 error = input_register_device(input);
0180 if (error) {
0181 dev_err(dev, "Unable to register input device: %d\n", error);
0182 return error;
0183 }
0184
0185 return 0;
0186 }
0187
0188 #ifdef CONFIG_OF
0189 static const struct of_device_id adc_keys_of_match[] = {
0190 { .compatible = "adc-keys", },
0191 { }
0192 };
0193 MODULE_DEVICE_TABLE(of, adc_keys_of_match);
0194 #endif
0195
0196 static struct platform_driver adc_keys_driver = {
0197 .driver = {
0198 .name = "adc_keys",
0199 .of_match_table = of_match_ptr(adc_keys_of_match),
0200 },
0201 .probe = adc_keys_probe,
0202 };
0203 module_platform_driver(adc_keys_driver);
0204
0205 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
0206 MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
0207 MODULE_LICENSE("GPL v2");