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 <linux/unaligned.h>
16 struct adc_joystick_axis {
22 struct input_dev *input;
23 struct iio_cb_buffer *buffer;
24 struct iio_channel *chans;
25 unsigned int num_chans;
26 struct adc_joystick_axis axes[] __counted_by(num_chans);
29 static int adc_joystick_invert(struct input_dev *dev,
30 unsigned int axis, int val)
32 int min = input_abs_get_min(dev, axis);
33 int max = input_abs_get_max(dev, axis);
35 return (max + min) - val;
38 static void adc_joystick_poll(struct input_dev *input)
40 struct adc_joystick *joy = input_get_drvdata(input);
43 for (i = 0; i < joy->num_chans; i++) {
44 ret = iio_read_channel_raw(&joy->chans[i], &val);
47 if (joy->axes[i].inverted)
48 val = adc_joystick_invert(input, i, val);
49 input_report_abs(input, joy->axes[i].code, val);
54 static int adc_joystick_handle(const void *data, void *private)
56 struct adc_joystick *joy = private;
57 enum iio_endian endianness;
58 int bytes, msb, val, idx, i;
62 bytes = joy->chans[0].channel->scan_type.storagebits >> 3;
64 for (i = 0; i < joy->num_chans; ++i) {
65 idx = joy->chans[i].channel->scan_index;
66 endianness = joy->chans[i].channel->scan_type.endianness;
67 msb = joy->chans[i].channel->scan_type.realbits - 1;
68 sign = tolower(joy->chans[i].channel->scan_type.sign) == 's';
72 val = ((const u8 *)data)[idx];
75 data_u16 = (const u16 *)data + idx;
78 * Data is aligned to the sample size by IIO core.
79 * Call `get_unaligned_xe16` to hide type casting.
81 if (endianness == IIO_BE)
82 val = get_unaligned_be16(data_u16);
83 else if (endianness == IIO_LE)
84 val = get_unaligned_le16(data_u16);
92 val >>= joy->chans[i].channel->scan_type.shift;
94 val = sign_extend32(val, msb);
96 val &= GENMASK(msb, 0);
97 if (joy->axes[i].inverted)
98 val = adc_joystick_invert(joy->input, i, val);
99 input_report_abs(joy->input, joy->axes[i].code, val);
102 input_sync(joy->input);
107 static int adc_joystick_open(struct input_dev *dev)
109 struct adc_joystick *joy = input_get_drvdata(dev);
110 struct device *devp = &dev->dev;
113 ret = iio_channel_start_all_cb(joy->buffer);
115 dev_err(devp, "Unable to start callback buffer: %d\n", ret);
120 static void adc_joystick_close(struct input_dev *dev)
122 struct adc_joystick *joy = input_get_drvdata(dev);
124 iio_channel_stop_all_cb(joy->buffer);
127 static void adc_joystick_cleanup(void *data)
129 iio_channel_release_all_cb(data);
132 static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy)
134 struct adc_joystick_axis *axes = joy->axes;
135 s32 range[2], fuzz, flat;
136 unsigned int num_axes;
139 num_axes = device_get_child_node_count(dev);
141 dev_err(dev, "Unable to find child nodes\n");
145 if (num_axes != joy->num_chans) {
146 dev_err(dev, "Got %d child nodes for %d channels\n",
147 num_axes, joy->num_chans);
151 device_for_each_child_node_scoped(dev, child) {
152 error = fwnode_property_read_u32(child, "reg", &i);
154 dev_err(dev, "reg invalid or missing\n");
159 dev_err(dev, "No matching axis for reg %d\n", i);
163 error = fwnode_property_read_u32(child, "linux,code",
166 dev_err(dev, "linux,code invalid or missing\n");
170 error = fwnode_property_read_u32_array(child, "abs-range",
173 dev_err(dev, "abs-range invalid or missing\n");
177 if (range[0] > range[1]) {
178 dev_dbg(dev, "abs-axis %d inverted\n", i);
179 axes[i].inverted = true;
180 swap(range[0], range[1]);
183 if (fwnode_property_read_u32(child, "abs-fuzz", &fuzz))
186 if (fwnode_property_read_u32(child, "abs-flat", &flat))
189 input_set_abs_params(joy->input, axes[i].code,
190 range[0], range[1], fuzz, flat);
197 static int adc_joystick_count_channels(struct device *dev,
198 const struct iio_channel *chans,
200 unsigned int *num_chans)
206 * Count how many channels we got. NULL terminated.
207 * Do not check the storage size if using polling.
209 for (i = 0; chans[i].indio_dev; i++) {
212 bits = chans[i].channel->scan_type.storagebits;
213 if (!bits || bits > 16) {
214 dev_err(dev, "Unsupported channel storage size\n");
217 if (bits != chans[0].channel->scan_type.storagebits) {
218 dev_err(dev, "Channels must have equal storage size\n");
227 static int adc_joystick_probe(struct platform_device *pdev)
229 struct device *dev = &pdev->dev;
230 struct iio_channel *chans;
231 struct adc_joystick *joy;
232 struct input_dev *input;
233 unsigned int poll_interval = 0;
234 unsigned int num_chans;
237 chans = devm_iio_channel_get_all(dev);
238 error = PTR_ERR_OR_ZERO(chans);
240 if (error != -EPROBE_DEFER)
241 dev_err(dev, "Unable to get IIO channels");
245 error = device_property_read_u32(dev, "poll-interval", &poll_interval);
247 /* -EINVAL means the property is absent. */
248 if (error != -EINVAL)
250 } else if (poll_interval == 0) {
251 dev_err(dev, "Unable to get poll-interval\n");
255 error = adc_joystick_count_channels(dev, chans, poll_interval != 0,
260 joy = devm_kzalloc(dev, struct_size(joy, axes, num_chans), GFP_KERNEL);
265 joy->num_chans = num_chans;
267 input = devm_input_allocate_device(dev);
269 dev_err(dev, "Unable to allocate input device\n");
274 input->name = pdev->name;
275 input->id.bustype = BUS_HOST;
277 error = adc_joystick_set_axes(dev, joy);
281 if (poll_interval != 0) {
282 input_setup_polling(input, adc_joystick_poll);
283 input_set_poll_interval(input, poll_interval);
285 input->open = adc_joystick_open;
286 input->close = adc_joystick_close;
288 joy->buffer = iio_channel_get_all_cb(dev, adc_joystick_handle,
290 if (IS_ERR(joy->buffer)) {
291 dev_err(dev, "Unable to allocate callback buffer\n");
292 return PTR_ERR(joy->buffer);
295 error = devm_add_action_or_reset(dev, adc_joystick_cleanup,
298 dev_err(dev, "Unable to add action\n");
303 input_set_drvdata(input, joy);
305 error = input_register_device(input);
307 dev_err(dev, "Unable to register input device\n");
314 static const struct of_device_id adc_joystick_of_match[] = {
315 { .compatible = "adc-joystick", },
318 MODULE_DEVICE_TABLE(of, adc_joystick_of_match);
320 static struct platform_driver adc_joystick_driver = {
322 .name = "adc-joystick",
323 .of_match_table = adc_joystick_of_match,
325 .probe = adc_joystick_probe,
327 module_platform_driver(adc_joystick_driver);
329 MODULE_DESCRIPTION("Input driver for joysticks connected over ADC");
331 MODULE_LICENSE("GPL");