1 // SPDX-License-Identifier: GPL-2.0
3 * Input driver for joysticks connected over ADC.
6 #include <linux/ctype.h>
7 #include <linux/input.h>
8 #include <linux/iio/iio.h>
9 #include <linux/iio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
14 #include <asm/unaligned.h>
16 struct adc_joystick_axis {
24 struct input_dev *input;
25 struct iio_cb_buffer *buffer;
26 struct adc_joystick_axis *axes;
27 struct iio_channel *chans;
32 static void adc_joystick_poll(struct input_dev *input)
34 struct adc_joystick *joy = input_get_drvdata(input);
37 for (i = 0; i < joy->num_chans; i++) {
38 ret = iio_read_channel_raw(&joy->chans[i], &val);
41 input_report_abs(input, joy->axes[i].code, val);
46 static int adc_joystick_handle(const void *data, void *private)
48 struct adc_joystick *joy = private;
49 enum iio_endian endianness;
50 int bytes, msb, val, idx, i;
54 bytes = joy->chans[0].channel->scan_type.storagebits >> 3;
56 for (i = 0; i < joy->num_chans; ++i) {
57 idx = joy->chans[i].channel->scan_index;
58 endianness = joy->chans[i].channel->scan_type.endianness;
59 msb = joy->chans[i].channel->scan_type.realbits - 1;
60 sign = tolower(joy->chans[i].channel->scan_type.sign) == 's';
64 val = ((const u8 *)data)[idx];
67 data_u16 = (const u16 *)data + idx;
70 * Data is aligned to the sample size by IIO core.
71 * Call `get_unaligned_xe16` to hide type casting.
73 if (endianness == IIO_BE)
74 val = get_unaligned_be16(data_u16);
75 else if (endianness == IIO_LE)
76 val = get_unaligned_le16(data_u16);
84 val >>= joy->chans[i].channel->scan_type.shift;
86 val = sign_extend32(val, msb);
88 val &= GENMASK(msb, 0);
89 input_report_abs(joy->input, joy->axes[i].code, val);
92 input_sync(joy->input);
97 static int adc_joystick_open(struct input_dev *dev)
99 struct adc_joystick *joy = input_get_drvdata(dev);
100 struct device *devp = &dev->dev;
103 ret = iio_channel_start_all_cb(joy->buffer);
105 dev_err(devp, "Unable to start callback buffer: %d\n", ret);
110 static void adc_joystick_close(struct input_dev *dev)
112 struct adc_joystick *joy = input_get_drvdata(dev);
114 iio_channel_stop_all_cb(joy->buffer);
117 static void adc_joystick_cleanup(void *data)
119 iio_channel_release_all_cb(data);
122 static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy)
124 struct adc_joystick_axis *axes;
125 struct fwnode_handle *child;
126 int num_axes, error, i;
128 num_axes = device_get_child_node_count(dev);
130 dev_err(dev, "Unable to find child nodes\n");
134 if (num_axes != joy->num_chans) {
135 dev_err(dev, "Got %d child nodes for %d channels\n",
136 num_axes, joy->num_chans);
140 axes = devm_kmalloc_array(dev, num_axes, sizeof(*axes), GFP_KERNEL);
144 device_for_each_child_node(dev, child) {
145 error = fwnode_property_read_u32(child, "reg", &i);
147 dev_err(dev, "reg invalid or missing\n");
153 dev_err(dev, "No matching axis for reg %d\n", i);
157 error = fwnode_property_read_u32(child, "linux,code",
160 dev_err(dev, "linux,code invalid or missing\n");
164 error = fwnode_property_read_u32_array(child, "abs-range",
167 dev_err(dev, "abs-range invalid or missing\n");
171 fwnode_property_read_u32(child, "abs-fuzz", &axes[i].fuzz);
172 fwnode_property_read_u32(child, "abs-flat", &axes[i].flat);
174 input_set_abs_params(joy->input, axes[i].code,
175 axes[i].range[0], axes[i].range[1],
176 axes[i].fuzz, axes[i].flat);
177 input_set_capability(joy->input, EV_ABS, axes[i].code);
185 fwnode_handle_put(child);
189 static int adc_joystick_probe(struct platform_device *pdev)
191 struct device *dev = &pdev->dev;
192 struct adc_joystick *joy;
193 struct input_dev *input;
197 unsigned int poll_interval;
199 joy = devm_kzalloc(dev, sizeof(*joy), GFP_KERNEL);
203 joy->chans = devm_iio_channel_get_all(dev);
204 if (IS_ERR(joy->chans)) {
205 error = PTR_ERR(joy->chans);
206 if (error != -EPROBE_DEFER)
207 dev_err(dev, "Unable to get IIO channels");
211 error = device_property_read_u32(dev, "poll-interval", &poll_interval);
213 /* -EINVAL means the property is absent. */
214 if (error != -EINVAL)
216 } else if (poll_interval == 0) {
217 dev_err(dev, "Unable to get poll-interval\n");
224 * Count how many channels we got. NULL terminated.
225 * Do not check the storage size if using polling.
227 for (i = 0; joy->chans[i].indio_dev; i++) {
230 bits = joy->chans[i].channel->scan_type.storagebits;
231 if (!bits || bits > 16) {
232 dev_err(dev, "Unsupported channel storage size\n");
235 if (bits != joy->chans[0].channel->scan_type.storagebits) {
236 dev_err(dev, "Channels must have equal storage size\n");
242 input = devm_input_allocate_device(dev);
244 dev_err(dev, "Unable to allocate input device\n");
249 input->name = pdev->name;
250 input->id.bustype = BUS_HOST;
252 error = adc_joystick_set_axes(dev, joy);
257 input_setup_polling(input, adc_joystick_poll);
258 input_set_poll_interval(input, poll_interval);
260 input->open = adc_joystick_open;
261 input->close = adc_joystick_close;
263 joy->buffer = iio_channel_get_all_cb(dev, adc_joystick_handle,
265 if (IS_ERR(joy->buffer)) {
266 dev_err(dev, "Unable to allocate callback buffer\n");
267 return PTR_ERR(joy->buffer);
270 error = devm_add_action_or_reset(dev, adc_joystick_cleanup,
273 dev_err(dev, "Unable to add action\n");
278 input_set_drvdata(input, joy);
280 error = input_register_device(input);
282 dev_err(dev, "Unable to register input device\n");
289 static const struct of_device_id adc_joystick_of_match[] = {
290 { .compatible = "adc-joystick", },
293 MODULE_DEVICE_TABLE(of, adc_joystick_of_match);
295 static struct platform_driver adc_joystick_driver = {
297 .name = "adc-joystick",
298 .of_match_table = adc_joystick_of_match,
300 .probe = adc_joystick_probe,
302 module_platform_driver(adc_joystick_driver);
304 MODULE_DESCRIPTION("Input driver for joysticks connected over ADC");
306 MODULE_LICENSE("GPL");