of_touchscreen.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. *
  51. * This function parses common DT properties for touchscreens and setups the
  52. * input device accordingly. The function keeps previously set up default
  53. * values if no value is specified via DT.
  54. */
  55. void touchscreen_parse_properties(struct input_dev *input, bool multitouch)
  56. {
  57. struct device *dev = input->dev.parent;
  58. unsigned int axis;
  59. unsigned int maximum, fuzz;
  60. bool data_present;
  61. input_alloc_absinfo(input);
  62. if (!input->absinfo)
  63. return;
  64. axis = multitouch ? ABS_MT_POSITION_X : ABS_X;
  65. data_present = touchscreen_get_prop_u32(dev, "touchscreen-size-x",
  66. input_abs_get_max(input,
  67. axis) + 1,
  68. &maximum) |
  69. touchscreen_get_prop_u32(dev, "touchscreen-fuzz-x",
  70. input_abs_get_fuzz(input, axis),
  71. &fuzz);
  72. if (data_present)
  73. touchscreen_set_params(input, axis, maximum - 1, fuzz);
  74. axis = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
  75. data_present = touchscreen_get_prop_u32(dev, "touchscreen-size-y",
  76. input_abs_get_max(input,
  77. axis) + 1,
  78. &maximum) |
  79. touchscreen_get_prop_u32(dev, "touchscreen-fuzz-y",
  80. input_abs_get_fuzz(input, axis),
  81. &fuzz);
  82. if (data_present)
  83. touchscreen_set_params(input, axis, maximum - 1, fuzz);
  84. axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
  85. data_present = touchscreen_get_prop_u32(dev,
  86. "touchscreen-max-pressure",
  87. input_abs_get_max(input, axis),
  88. &maximum) |
  89. touchscreen_get_prop_u32(dev,
  90. "touchscreen-fuzz-pressure",
  91. input_abs_get_fuzz(input, axis),
  92. &fuzz);
  93. if (data_present)
  94. touchscreen_set_params(input, axis, maximum, fuzz);
  95. }
  96. EXPORT_SYMBOL(touchscreen_parse_properties);