Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Input driver for joysticks connected over ADC.
4 * Copyright (c) 2019-2020 Artur Rojek <contact@artur-rojek.eu>
5 */
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>
13
14#include <asm/unaligned.h>
15
16struct adc_joystick_axis {
17 u32 code;
18 s32 range[2];
19 s32 fuzz;
20 s32 flat;
21};
22
23struct adc_joystick {
24 struct input_dev *input;
25 struct iio_cb_buffer *buffer;
26 struct adc_joystick_axis *axes;
27 struct iio_channel *chans;
28 int num_chans;
29 bool polled;
30};
31
32static void adc_joystick_poll(struct input_dev *input)
33{
34 struct adc_joystick *joy = input_get_drvdata(input);
35 int i, val, ret;
36
37 for (i = 0; i < joy->num_chans; i++) {
38 ret = iio_read_channel_raw(&joy->chans[i], &val);
39 if (ret < 0)
40 return;
41 input_report_abs(input, joy->axes[i].code, val);
42 }
43 input_sync(input);
44}
45
46static int adc_joystick_handle(const void *data, void *private)
47{
48 struct adc_joystick *joy = private;
49 enum iio_endian endianness;
50 int bytes, msb, val, idx, i;
51 const u16 *data_u16;
52 bool sign;
53
54 bytes = joy->chans[0].channel->scan_type.storagebits >> 3;
55
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';
61
62 switch (bytes) {
63 case 1:
64 val = ((const u8 *)data)[idx];
65 break;
66 case 2:
67 data_u16 = (const u16 *)data + idx;
68
69 /*
70 * Data is aligned to the sample size by IIO core.
71 * Call `get_unaligned_xe16` to hide type casting.
72 */
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);
77 else /* IIO_CPU */
78 val = *data_u16;
79 break;
80 default:
81 return -EINVAL;
82 }
83
84 val >>= joy->chans[i].channel->scan_type.shift;
85 if (sign)
86 val = sign_extend32(val, msb);
87 else
88 val &= GENMASK(msb, 0);
89 input_report_abs(joy->input, joy->axes[i].code, val);
90 }
91
92 input_sync(joy->input);
93
94 return 0;
95}
96
97static int adc_joystick_open(struct input_dev *dev)
98{
99 struct adc_joystick *joy = input_get_drvdata(dev);
100 struct device *devp = &dev->dev;
101 int ret;
102
103 ret = iio_channel_start_all_cb(joy->buffer);
104 if (ret)
105 dev_err(devp, "Unable to start callback buffer: %d\n", ret);
106
107 return ret;
108}
109
110static void adc_joystick_close(struct input_dev *dev)
111{
112 struct adc_joystick *joy = input_get_drvdata(dev);
113
114 iio_channel_stop_all_cb(joy->buffer);
115}
116
117static void adc_joystick_cleanup(void *data)
118{
119 iio_channel_release_all_cb(data);
120}
121
122static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy)
123{
124 struct adc_joystick_axis *axes;
125 struct fwnode_handle *child;
126 int num_axes, error, i;
127
128 num_axes = device_get_child_node_count(dev);
129 if (!num_axes) {
130 dev_err(dev, "Unable to find child nodes\n");
131 return -EINVAL;
132 }
133
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);
137 return -EINVAL;
138 }
139
140 axes = devm_kmalloc_array(dev, num_axes, sizeof(*axes), GFP_KERNEL);
141 if (!axes)
142 return -ENOMEM;
143
144 device_for_each_child_node(dev, child) {
145 error = fwnode_property_read_u32(child, "reg", &i);
146 if (error) {
147 dev_err(dev, "reg invalid or missing\n");
148 goto err_fwnode_put;
149 }
150
151 if (i >= num_axes) {
152 error = -EINVAL;
153 dev_err(dev, "No matching axis for reg %d\n", i);
154 goto err_fwnode_put;
155 }
156
157 error = fwnode_property_read_u32(child, "linux,code",
158 &axes[i].code);
159 if (error) {
160 dev_err(dev, "linux,code invalid or missing\n");
161 goto err_fwnode_put;
162 }
163
164 error = fwnode_property_read_u32_array(child, "abs-range",
165 axes[i].range, 2);
166 if (error) {
167 dev_err(dev, "abs-range invalid or missing\n");
168 goto err_fwnode_put;
169 }
170
171 fwnode_property_read_u32(child, "abs-fuzz", &axes[i].fuzz);
172 fwnode_property_read_u32(child, "abs-flat", &axes[i].flat);
173
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);
178 }
179
180 joy->axes = axes;
181
182 return 0;
183
184err_fwnode_put:
185 fwnode_handle_put(child);
186 return error;
187}
188
189static int adc_joystick_probe(struct platform_device *pdev)
190{
191 struct device *dev = &pdev->dev;
192 struct adc_joystick *joy;
193 struct input_dev *input;
194 int error;
195 int bits;
196 int i;
197 unsigned int poll_interval;
198
199 joy = devm_kzalloc(dev, sizeof(*joy), GFP_KERNEL);
200 if (!joy)
201 return -ENOMEM;
202
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");
208 return error;
209 }
210
211 error = device_property_read_u32(dev, "poll-interval", &poll_interval);
212 if (error) {
213 /* -EINVAL means the property is absent. */
214 if (error != -EINVAL)
215 return error;
216 } else if (poll_interval == 0) {
217 dev_err(dev, "Unable to get poll-interval\n");
218 return -EINVAL;
219 } else {
220 joy->polled = true;
221 }
222
223 /*
224 * Count how many channels we got. NULL terminated.
225 * Do not check the storage size if using polling.
226 */
227 for (i = 0; joy->chans[i].indio_dev; i++) {
228 if (joy->polled)
229 continue;
230 bits = joy->chans[i].channel->scan_type.storagebits;
231 if (!bits || bits > 16) {
232 dev_err(dev, "Unsupported channel storage size\n");
233 return -EINVAL;
234 }
235 if (bits != joy->chans[0].channel->scan_type.storagebits) {
236 dev_err(dev, "Channels must have equal storage size\n");
237 return -EINVAL;
238 }
239 }
240 joy->num_chans = i;
241
242 input = devm_input_allocate_device(dev);
243 if (!input) {
244 dev_err(dev, "Unable to allocate input device\n");
245 return -ENOMEM;
246 }
247
248 joy->input = input;
249 input->name = pdev->name;
250 input->id.bustype = BUS_HOST;
251
252 error = adc_joystick_set_axes(dev, joy);
253 if (error)
254 return error;
255
256 if (joy->polled) {
257 input_setup_polling(input, adc_joystick_poll);
258 input_set_poll_interval(input, poll_interval);
259 } else {
260 input->open = adc_joystick_open;
261 input->close = adc_joystick_close;
262
263 joy->buffer = iio_channel_get_all_cb(dev, adc_joystick_handle,
264 joy);
265 if (IS_ERR(joy->buffer)) {
266 dev_err(dev, "Unable to allocate callback buffer\n");
267 return PTR_ERR(joy->buffer);
268 }
269
270 error = devm_add_action_or_reset(dev, adc_joystick_cleanup,
271 joy->buffer);
272 if (error) {
273 dev_err(dev, "Unable to add action\n");
274 return error;
275 }
276 }
277
278 input_set_drvdata(input, joy);
279
280 error = input_register_device(input);
281 if (error) {
282 dev_err(dev, "Unable to register input device\n");
283 return error;
284 }
285
286 return 0;
287}
288
289static const struct of_device_id adc_joystick_of_match[] = {
290 { .compatible = "adc-joystick", },
291 { }
292};
293MODULE_DEVICE_TABLE(of, adc_joystick_of_match);
294
295static struct platform_driver adc_joystick_driver = {
296 .driver = {
297 .name = "adc-joystick",
298 .of_match_table = adc_joystick_of_match,
299 },
300 .probe = adc_joystick_probe,
301};
302module_platform_driver(adc_joystick_driver);
303
304MODULE_DESCRIPTION("Input driver for joysticks connected over ADC");
305MODULE_AUTHOR("Artur Rojek <contact@artur-rojek.eu>");
306MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Input driver for joysticks connected over ADC.
4 * Copyright (c) 2019-2020 Artur Rojek <contact@artur-rojek.eu>
5 */
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>
13
14#include <asm/unaligned.h>
15
16struct adc_joystick_axis {
17 u32 code;
18 s32 range[2];
19 s32 fuzz;
20 s32 flat;
21};
22
23struct adc_joystick {
24 struct input_dev *input;
25 struct iio_cb_buffer *buffer;
26 struct adc_joystick_axis *axes;
27 struct iio_channel *chans;
28 int num_chans;
29};
30
31static int adc_joystick_handle(const void *data, void *private)
32{
33 struct adc_joystick *joy = private;
34 enum iio_endian endianness;
35 int bytes, msb, val, idx, i;
36 const u16 *data_u16;
37 bool sign;
38
39 bytes = joy->chans[0].channel->scan_type.storagebits >> 3;
40
41 for (i = 0; i < joy->num_chans; ++i) {
42 idx = joy->chans[i].channel->scan_index;
43 endianness = joy->chans[i].channel->scan_type.endianness;
44 msb = joy->chans[i].channel->scan_type.realbits - 1;
45 sign = tolower(joy->chans[i].channel->scan_type.sign) == 's';
46
47 switch (bytes) {
48 case 1:
49 val = ((const u8 *)data)[idx];
50 break;
51 case 2:
52 data_u16 = (const u16 *)data + idx;
53
54 /*
55 * Data is aligned to the sample size by IIO core.
56 * Call `get_unaligned_xe16` to hide type casting.
57 */
58 if (endianness == IIO_BE)
59 val = get_unaligned_be16(data_u16);
60 else if (endianness == IIO_LE)
61 val = get_unaligned_le16(data_u16);
62 else /* IIO_CPU */
63 val = *data_u16;
64 break;
65 default:
66 return -EINVAL;
67 }
68
69 val >>= joy->chans[i].channel->scan_type.shift;
70 if (sign)
71 val = sign_extend32(val, msb);
72 else
73 val &= GENMASK(msb, 0);
74 input_report_abs(joy->input, joy->axes[i].code, val);
75 }
76
77 input_sync(joy->input);
78
79 return 0;
80}
81
82static int adc_joystick_open(struct input_dev *dev)
83{
84 struct adc_joystick *joy = input_get_drvdata(dev);
85 struct device *devp = &dev->dev;
86 int ret;
87
88 ret = iio_channel_start_all_cb(joy->buffer);
89 if (ret)
90 dev_err(devp, "Unable to start callback buffer: %d\n", ret);
91
92 return ret;
93}
94
95static void adc_joystick_close(struct input_dev *dev)
96{
97 struct adc_joystick *joy = input_get_drvdata(dev);
98
99 iio_channel_stop_all_cb(joy->buffer);
100}
101
102static void adc_joystick_cleanup(void *data)
103{
104 iio_channel_release_all_cb(data);
105}
106
107static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy)
108{
109 struct adc_joystick_axis *axes;
110 struct fwnode_handle *child;
111 int num_axes, error, i;
112
113 num_axes = device_get_child_node_count(dev);
114 if (!num_axes) {
115 dev_err(dev, "Unable to find child nodes\n");
116 return -EINVAL;
117 }
118
119 if (num_axes != joy->num_chans) {
120 dev_err(dev, "Got %d child nodes for %d channels\n",
121 num_axes, joy->num_chans);
122 return -EINVAL;
123 }
124
125 axes = devm_kmalloc_array(dev, num_axes, sizeof(*axes), GFP_KERNEL);
126 if (!axes)
127 return -ENOMEM;
128
129 device_for_each_child_node(dev, child) {
130 error = fwnode_property_read_u32(child, "reg", &i);
131 if (error) {
132 dev_err(dev, "reg invalid or missing\n");
133 goto err_fwnode_put;
134 }
135
136 if (i >= num_axes) {
137 error = -EINVAL;
138 dev_err(dev, "No matching axis for reg %d\n", i);
139 goto err_fwnode_put;
140 }
141
142 error = fwnode_property_read_u32(child, "linux,code",
143 &axes[i].code);
144 if (error) {
145 dev_err(dev, "linux,code invalid or missing\n");
146 goto err_fwnode_put;
147 }
148
149 error = fwnode_property_read_u32_array(child, "abs-range",
150 axes[i].range, 2);
151 if (error) {
152 dev_err(dev, "abs-range invalid or missing\n");
153 goto err_fwnode_put;
154 }
155
156 fwnode_property_read_u32(child, "abs-fuzz", &axes[i].fuzz);
157 fwnode_property_read_u32(child, "abs-flat", &axes[i].flat);
158
159 input_set_abs_params(joy->input, axes[i].code,
160 axes[i].range[0], axes[i].range[1],
161 axes[i].fuzz, axes[i].flat);
162 input_set_capability(joy->input, EV_ABS, axes[i].code);
163 }
164
165 joy->axes = axes;
166
167 return 0;
168
169err_fwnode_put:
170 fwnode_handle_put(child);
171 return error;
172}
173
174static int adc_joystick_probe(struct platform_device *pdev)
175{
176 struct device *dev = &pdev->dev;
177 struct adc_joystick *joy;
178 struct input_dev *input;
179 int error;
180 int bits;
181 int i;
182
183 joy = devm_kzalloc(dev, sizeof(*joy), GFP_KERNEL);
184 if (!joy)
185 return -ENOMEM;
186
187 joy->chans = devm_iio_channel_get_all(dev);
188 if (IS_ERR(joy->chans)) {
189 error = PTR_ERR(joy->chans);
190 if (error != -EPROBE_DEFER)
191 dev_err(dev, "Unable to get IIO channels");
192 return error;
193 }
194
195 /* Count how many channels we got. NULL terminated. */
196 for (i = 0; joy->chans[i].indio_dev; i++) {
197 bits = joy->chans[i].channel->scan_type.storagebits;
198 if (!bits || bits > 16) {
199 dev_err(dev, "Unsupported channel storage size\n");
200 return -EINVAL;
201 }
202 if (bits != joy->chans[0].channel->scan_type.storagebits) {
203 dev_err(dev, "Channels must have equal storage size\n");
204 return -EINVAL;
205 }
206 }
207 joy->num_chans = i;
208
209 input = devm_input_allocate_device(dev);
210 if (!input) {
211 dev_err(dev, "Unable to allocate input device\n");
212 return -ENOMEM;
213 }
214
215 joy->input = input;
216 input->name = pdev->name;
217 input->id.bustype = BUS_HOST;
218 input->open = adc_joystick_open;
219 input->close = adc_joystick_close;
220
221 error = adc_joystick_set_axes(dev, joy);
222 if (error)
223 return error;
224
225 input_set_drvdata(input, joy);
226 error = input_register_device(input);
227 if (error) {
228 dev_err(dev, "Unable to register input device\n");
229 return error;
230 }
231
232 joy->buffer = iio_channel_get_all_cb(dev, adc_joystick_handle, joy);
233 if (IS_ERR(joy->buffer)) {
234 dev_err(dev, "Unable to allocate callback buffer\n");
235 return PTR_ERR(joy->buffer);
236 }
237
238 error = devm_add_action_or_reset(dev, adc_joystick_cleanup, joy->buffer);
239 if (error) {
240 dev_err(dev, "Unable to add action\n");
241 return error;
242 }
243
244 return 0;
245}
246
247static const struct of_device_id adc_joystick_of_match[] = {
248 { .compatible = "adc-joystick", },
249 { }
250};
251MODULE_DEVICE_TABLE(of, adc_joystick_of_match);
252
253static struct platform_driver adc_joystick_driver = {
254 .driver = {
255 .name = "adc-joystick",
256 .of_match_table = adc_joystick_of_match,
257 },
258 .probe = adc_joystick_probe,
259};
260module_platform_driver(adc_joystick_driver);
261
262MODULE_DESCRIPTION("Input driver for joysticks connected over ADC");
263MODULE_AUTHOR("Artur Rojek <contact@artur-rojek.eu>");
264MODULE_LICENSE("GPL");