adc-joystick.c 7.0 KB

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