2 * Input driver for resistor ladder connected on ADC
4 * Copyright (c) 2016 Alexandre Belloni
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.
11 #include <linux/err.h>
12 #include <linux/iio/consumer.h>
13 #include <linux/iio/types.h>
14 #include <linux/input.h>
15 #include <linux/input-polldev.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
23 struct adc_keys_button
{
28 struct adc_keys_state
{
29 struct iio_channel
*channel
;
33 const struct adc_keys_button
*map
;
36 static void adc_keys_poll(struct input_polled_dev
*dev
)
38 struct adc_keys_state
*st
= dev
->private;
40 u32 diff
, closest
= 0xffffffff;
43 ret
= iio_read_channel_processed(st
->channel
, &value
);
44 if (unlikely(ret
< 0)) {
45 /* Forcibly release key if any was pressed */
46 value
= st
->keyup_voltage
;
48 for (i
= 0; i
< st
->num_keys
; i
++) {
49 diff
= abs(st
->map
[i
].voltage
- value
);
52 keycode
= st
->map
[i
].keycode
;
57 if (abs(st
->keyup_voltage
- value
) < closest
)
60 if (st
->last_key
&& st
->last_key
!= keycode
)
61 input_report_key(dev
->input
, st
->last_key
, 0);
64 input_report_key(dev
->input
, keycode
, 1);
66 input_sync(dev
->input
);
67 st
->last_key
= keycode
;
70 static int adc_keys_load_keymap(struct device
*dev
, struct adc_keys_state
*st
)
72 struct adc_keys_button
*map
;
73 struct fwnode_handle
*child
;
76 st
->num_keys
= device_get_child_node_count(dev
);
77 if (st
->num_keys
== 0) {
78 dev_err(dev
, "keymap is missing\n");
82 map
= devm_kmalloc_array(dev
, st
->num_keys
, sizeof(*map
), GFP_KERNEL
);
87 device_for_each_child_node(dev
, child
) {
88 if (fwnode_property_read_u32(child
, "press-threshold-microvolt",
90 dev_err(dev
, "Key with invalid or missing voltage\n");
91 fwnode_handle_put(child
);
94 map
[i
].voltage
/= 1000;
96 if (fwnode_property_read_u32(child
, "linux,code",
98 dev_err(dev
, "Key with invalid or missing linux,code\n");
99 fwnode_handle_put(child
);
110 static int adc_keys_probe(struct platform_device
*pdev
)
112 struct device
*dev
= &pdev
->dev
;
113 struct adc_keys_state
*st
;
114 struct input_polled_dev
*poll_dev
;
115 struct input_dev
*input
;
116 enum iio_chan_type type
;
120 st
= devm_kzalloc(dev
, sizeof(*st
), GFP_KERNEL
);
124 st
->channel
= devm_iio_channel_get(dev
, "buttons");
125 if (IS_ERR(st
->channel
))
126 return PTR_ERR(st
->channel
);
128 if (!st
->channel
->indio_dev
)
131 error
= iio_get_channel_type(st
->channel
, &type
);
135 if (type
!= IIO_VOLTAGE
) {
136 dev_err(dev
, "Incompatible channel type %d\n", type
);
140 if (device_property_read_u32(dev
, "keyup-threshold-microvolt",
141 &st
->keyup_voltage
)) {
142 dev_err(dev
, "Invalid or missing keyup voltage\n");
145 st
->keyup_voltage
/= 1000;
147 error
= adc_keys_load_keymap(dev
, st
);
151 poll_dev
= devm_input_allocate_polled_device(dev
);
153 dev_err(dev
, "failed to allocate input device\n");
157 if (!device_property_read_u32(dev
, "poll-interval", &value
))
158 poll_dev
->poll_interval
= value
;
160 poll_dev
->poll
= adc_keys_poll
;
161 poll_dev
->private = st
;
163 input
= poll_dev
->input
;
165 input
->name
= pdev
->name
;
166 input
->phys
= "adc-keys/input0";
168 input
->id
.bustype
= BUS_HOST
;
169 input
->id
.vendor
= 0x0001;
170 input
->id
.product
= 0x0001;
171 input
->id
.version
= 0x0100;
173 __set_bit(EV_KEY
, input
->evbit
);
174 for (i
= 0; i
< st
->num_keys
; i
++)
175 __set_bit(st
->map
[i
].keycode
, input
->keybit
);
177 if (device_property_read_bool(dev
, "autorepeat"))
178 __set_bit(EV_REP
, input
->evbit
);
180 error
= input_register_polled_device(poll_dev
);
182 dev_err(dev
, "Unable to register input device: %d\n", error
);
190 static const struct of_device_id adc_keys_of_match
[] = {
191 { .compatible
= "adc-keys", },
194 MODULE_DEVICE_TABLE(of
, adc_keys_of_match
);
197 static struct platform_driver __refdata adc_keys_driver
= {
200 .of_match_table
= of_match_ptr(adc_keys_of_match
),
202 .probe
= adc_keys_probe
,
204 module_platform_driver(adc_keys_driver
);
206 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
207 MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
208 MODULE_LICENSE("GPL v2");