of_touchscreen.c 5.5 KB

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