adc-joystick.c 6.1 KB

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