adc-joystick.c 7.6 KB

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