of_touchscreen.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Generic DT helper functions for touchscreen devices
  3. *
  4. * Copyright (c) 2014 Sebastian Reichel <sre@kernel.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/property.h>
  12. #include <linux/input.h>
  13. #include <linux/input/mt.h>
  14. #include <linux/input/touchscreen.h>
  15. static bool touchscreen_get_prop_u32(struct device *dev,
  16. const char *property,
  17. unsigned int default_value,
  18. unsigned int *value)
  19. {
  20. u32 val;
  21. int error;
  22. error = device_property_read_u32(dev, property, &val);
  23. if (error) {
  24. *value = default_value;
  25. return false;
  26. }
  27. *value = val;
  28. return true;
  29. }
  30. static void touchscreen_set_params(struct input_dev *dev,
  31. unsigned long axis,
  32. int max, int fuzz)
  33. {
  34. struct input_absinfo *absinfo;
  35. if (!test_bit(axis, dev->absbit)) {
  36. dev_warn(&dev->dev,
  37. "DT specifies parameters but the axis %lu is not set up\n",
  38. axis);
  39. return;
  40. }
  41. absinfo = &dev->absinfo[axis];
  42. absinfo->maximum = max;
  43. absinfo->fuzz = fuzz;
  44. }
  45. /**
  46. * touchscreen_parse_properties - parse common touchscreen DT 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 DT properties for touchscreens and setups the
  55. * input device accordingly. The function keeps previously set up default
  56. * values if no value is specified via DT.
  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. unsigned int axis;
  63. unsigned int maximum, fuzz;
  64. bool data_present;
  65. input_alloc_absinfo(input);
  66. if (!input->absinfo)
  67. return;
  68. axis = multitouch ? ABS_MT_POSITION_X : ABS_X;
  69. data_present = touchscreen_get_prop_u32(dev, "touchscreen-size-x",
  70. input_abs_get_max(input,
  71. axis) + 1,
  72. &maximum) |
  73. touchscreen_get_prop_u32(dev, "touchscreen-fuzz-x",
  74. input_abs_get_fuzz(input, axis),
  75. &fuzz);
  76. if (data_present)
  77. touchscreen_set_params(input, axis, maximum - 1, fuzz);
  78. axis = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
  79. data_present = touchscreen_get_prop_u32(dev, "touchscreen-size-y",
  80. input_abs_get_max(input,
  81. axis) + 1,
  82. &maximum) |
  83. touchscreen_get_prop_u32(dev, "touchscreen-fuzz-y",
  84. input_abs_get_fuzz(input, axis),
  85. &fuzz);
  86. if (data_present)
  87. touchscreen_set_params(input, axis, maximum - 1, fuzz);
  88. axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
  89. data_present = touchscreen_get_prop_u32(dev,
  90. "touchscreen-max-pressure",
  91. input_abs_get_max(input, axis),
  92. &maximum) |
  93. touchscreen_get_prop_u32(dev,
  94. "touchscreen-fuzz-pressure",
  95. input_abs_get_fuzz(input, axis),
  96. &fuzz);
  97. if (data_present)
  98. touchscreen_set_params(input, axis, maximum, fuzz);
  99. if (!prop)
  100. return;
  101. axis = multitouch ? ABS_MT_POSITION_X : ABS_X;
  102. prop->max_x = input_abs_get_max(input, axis);
  103. prop->max_y = input_abs_get_max(input, axis + 1);
  104. prop->invert_x =
  105. device_property_read_bool(dev, "touchscreen-inverted-x");
  106. prop->invert_y =
  107. device_property_read_bool(dev, "touchscreen-inverted-y");
  108. prop->swap_x_y =
  109. device_property_read_bool(dev, "touchscreen-swapped-x-y");
  110. if (prop->swap_x_y)
  111. swap(input->absinfo[axis], input->absinfo[axis + 1]);
  112. }
  113. EXPORT_SYMBOL(touchscreen_parse_properties);
  114. static void
  115. touchscreen_apply_prop_to_x_y(const struct touchscreen_properties *prop,
  116. unsigned int *x, unsigned int *y)
  117. {
  118. if (prop->invert_x)
  119. *x = prop->max_x - *x;
  120. if (prop->invert_y)
  121. *y = prop->max_y - *y;
  122. if (prop->swap_x_y)
  123. swap(*x, *y);
  124. }
  125. /**
  126. * touchscreen_set_mt_pos - Set input_mt_pos coordinates
  127. * @pos: input_mt_pos to set coordinates of
  128. * @prop: pointer to a struct touchscreen_properties
  129. * @x: X coordinate to store in pos
  130. * @y: Y coordinate to store in pos
  131. *
  132. * Adjust the passed in x and y values applying any axis inversion and
  133. * swapping requested in the passed in touchscreen_properties and store
  134. * the result in a struct input_mt_pos.
  135. */
  136. void touchscreen_set_mt_pos(struct input_mt_pos *pos,
  137. const struct touchscreen_properties *prop,
  138. unsigned int x, unsigned int y)
  139. {
  140. touchscreen_apply_prop_to_x_y(prop, &x, &y);
  141. pos->x = x;
  142. pos->y = y;
  143. }
  144. EXPORT_SYMBOL(touchscreen_set_mt_pos);
  145. /**
  146. * touchscreen_report_pos - Report touchscreen coordinates
  147. * @input: input_device to report coordinates for
  148. * @prop: pointer to a struct touchscreen_properties
  149. * @x: X coordinate to report
  150. * @y: Y coordinate to report
  151. * @multitouch: Report coordinates on single-touch or multi-touch axes
  152. *
  153. * Adjust the passed in x and y values applying any axis inversion and
  154. * swapping requested in the passed in touchscreen_properties and then
  155. * report the resulting coordinates on the input_dev's x and y axis.
  156. */
  157. void touchscreen_report_pos(struct input_dev *input,
  158. const struct touchscreen_properties *prop,
  159. unsigned int x, unsigned int y,
  160. bool multitouch)
  161. {
  162. touchscreen_apply_prop_to_x_y(prop, &x, &y);
  163. input_report_abs(input, multitouch ? ABS_MT_POSITION_X : ABS_X, x);
  164. input_report_abs(input, multitouch ? ABS_MT_POSITION_Y : ABS_Y, y);
  165. }
  166. EXPORT_SYMBOL(touchscreen_report_pos);