touchscreen.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic helper functions for touchscreens and other two-dimensional
  4. * pointing devices
  5. *
  6. * Copyright (c) 2014 Sebastian Reichel <sre@kernel.org>
  7. */
  8. #include <linux/export.h>
  9. #include <linux/property.h>
  10. #include <linux/input.h>
  11. #include <linux/input/mt.h>
  12. #include <linux/input/touchscreen.h>
  13. #include <linux/module.h>
  14. static bool touchscreen_get_prop_u32(struct device *dev,
  15. const char *property,
  16. unsigned int default_value,
  17. unsigned int *value)
  18. {
  19. u32 val;
  20. int error;
  21. error = device_property_read_u32(dev, property, &val);
  22. if (error) {
  23. *value = default_value;
  24. return false;
  25. }
  26. *value = val;
  27. return true;
  28. }
  29. static void touchscreen_set_params(struct input_dev *dev,
  30. unsigned long axis,
  31. int min, int max, int fuzz)
  32. {
  33. struct input_absinfo *absinfo;
  34. if (!test_bit(axis, dev->absbit)) {
  35. dev_warn(&dev->dev,
  36. "Parameters are specified but the axis %lu is not set up\n",
  37. axis);
  38. return;
  39. }
  40. absinfo = &dev->absinfo[axis];
  41. absinfo->minimum = min;
  42. absinfo->maximum = max;
  43. absinfo->fuzz = fuzz;
  44. }
  45. /**
  46. * touchscreen_parse_properties - parse common touchscreen properties
  47. * @input: input device that should be parsed
  48. * @multitouch: specifies whether parsed properties should be applied to
  49. * single-touch or multi-touch axes
  50. * @prop: pointer to a struct touchscreen_properties into which to store
  51. * axis swap and invert info for use with touchscreen_report_x_y();
  52. * or %NULL
  53. *
  54. * This function parses common properties for touchscreens and sets up the
  55. * input device accordingly. The function keeps previously set up default
  56. * values if no value is specified.
  57. */
  58. void touchscreen_parse_properties(struct input_dev *input, bool multitouch,
  59. struct touchscreen_properties *prop)
  60. {
  61. struct device *dev = input->dev.parent;
  62. struct input_absinfo *absinfo;
  63. unsigned int axis, axis_x, axis_y;
  64. unsigned int minimum, maximum, fuzz;
  65. bool data_present;
  66. input_alloc_absinfo(input);
  67. if (!input->absinfo)
  68. return;
  69. axis_x = multitouch ? ABS_MT_POSITION_X : ABS_X;
  70. axis_y = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
  71. data_present = touchscreen_get_prop_u32(dev, "touchscreen-min-x",
  72. input_abs_get_min(input, axis_x),
  73. &minimum);
  74. data_present |= touchscreen_get_prop_u32(dev, "touchscreen-size-x",
  75. input_abs_get_max(input,
  76. axis_x) + 1,
  77. &maximum);
  78. data_present |= touchscreen_get_prop_u32(dev, "touchscreen-fuzz-x",
  79. input_abs_get_fuzz(input, axis_x),
  80. &fuzz);
  81. if (data_present)
  82. touchscreen_set_params(input, axis_x, minimum, maximum - 1, fuzz);
  83. data_present = touchscreen_get_prop_u32(dev, "touchscreen-min-y",
  84. input_abs_get_min(input, axis_y),
  85. &minimum);
  86. data_present |= touchscreen_get_prop_u32(dev, "touchscreen-size-y",
  87. input_abs_get_max(input,
  88. axis_y) + 1,
  89. &maximum);
  90. data_present |= touchscreen_get_prop_u32(dev, "touchscreen-fuzz-y",
  91. input_abs_get_fuzz(input, axis_y),
  92. &fuzz);
  93. if (data_present)
  94. touchscreen_set_params(input, axis_y, minimum, maximum - 1, fuzz);
  95. axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
  96. data_present = touchscreen_get_prop_u32(dev,
  97. "touchscreen-max-pressure",
  98. input_abs_get_max(input, axis),
  99. &maximum);
  100. data_present |= touchscreen_get_prop_u32(dev,
  101. "touchscreen-fuzz-pressure",
  102. input_abs_get_fuzz(input, axis),
  103. &fuzz);
  104. if (data_present)
  105. touchscreen_set_params(input, axis, 0, maximum, fuzz);
  106. if (!prop)
  107. return;
  108. prop->max_x = input_abs_get_max(input, axis_x);
  109. prop->max_y = input_abs_get_max(input, axis_y);
  110. prop->invert_x =
  111. device_property_read_bool(dev, "touchscreen-inverted-x");
  112. if (prop->invert_x) {
  113. absinfo = &input->absinfo[axis_x];
  114. absinfo->maximum -= absinfo->minimum;
  115. absinfo->minimum = 0;
  116. }
  117. prop->invert_y =
  118. device_property_read_bool(dev, "touchscreen-inverted-y");
  119. if (prop->invert_y) {
  120. absinfo = &input->absinfo[axis_y];
  121. absinfo->maximum -= absinfo->minimum;
  122. absinfo->minimum = 0;
  123. }
  124. prop->swap_x_y =
  125. device_property_read_bool(dev, "touchscreen-swapped-x-y");
  126. if (prop->swap_x_y)
  127. swap(input->absinfo[axis_x], input->absinfo[axis_y]);
  128. }
  129. EXPORT_SYMBOL(touchscreen_parse_properties);
  130. static void
  131. touchscreen_apply_prop_to_x_y(const struct touchscreen_properties *prop,
  132. unsigned int *x, unsigned int *y)
  133. {
  134. if (prop->invert_x)
  135. *x = prop->max_x - *x;
  136. if (prop->invert_y)
  137. *y = prop->max_y - *y;
  138. if (prop->swap_x_y)
  139. swap(*x, *y);
  140. }
  141. /**
  142. * touchscreen_set_mt_pos - Set input_mt_pos coordinates
  143. * @pos: input_mt_pos to set coordinates of
  144. * @prop: pointer to a struct touchscreen_properties
  145. * @x: X coordinate to store in pos
  146. * @y: Y coordinate to store in pos
  147. *
  148. * Adjust the passed in x and y values applying any axis inversion and
  149. * swapping requested in the passed in touchscreen_properties and store
  150. * the result in a struct input_mt_pos.
  151. */
  152. void touchscreen_set_mt_pos(struct input_mt_pos *pos,
  153. const struct touchscreen_properties *prop,
  154. unsigned int x, unsigned int y)
  155. {
  156. touchscreen_apply_prop_to_x_y(prop, &x, &y);
  157. pos->x = x;
  158. pos->y = y;
  159. }
  160. EXPORT_SYMBOL(touchscreen_set_mt_pos);
  161. /**
  162. * touchscreen_report_pos - Report touchscreen coordinates
  163. * @input: input_device to report coordinates for
  164. * @prop: pointer to a struct touchscreen_properties
  165. * @x: X coordinate to report
  166. * @y: Y coordinate to report
  167. * @multitouch: Report coordinates on single-touch or multi-touch axes
  168. *
  169. * Adjust the passed in x and y values applying any axis inversion and
  170. * swapping requested in the passed in touchscreen_properties and then
  171. * report the resulting coordinates on the input_dev's x and y axis.
  172. */
  173. void touchscreen_report_pos(struct input_dev *input,
  174. const struct touchscreen_properties *prop,
  175. unsigned int x, unsigned int y,
  176. bool multitouch)
  177. {
  178. touchscreen_apply_prop_to_x_y(prop, &x, &y);
  179. input_report_abs(input, multitouch ? ABS_MT_POSITION_X : ABS_X, x);
  180. input_report_abs(input, multitouch ? ABS_MT_POSITION_Y : ABS_Y, y);
  181. }
  182. EXPORT_SYMBOL(touchscreen_report_pos);
  183. MODULE_LICENSE("GPL v2");
  184. MODULE_DESCRIPTION("Helper functions for touchscreens and other devices");