of_touchscreen.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/of.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_node *np,
  16. const char *property,
  17. unsigned int default_value,
  18. unsigned int *value)
  19. {
  20. u32 val;
  21. int error;
  22. error = of_property_read_u32(np, 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. /*
  37. * Emit a warning only if the axis is not a multitouch
  38. * axis, which might not be set by the driver.
  39. */
  40. if (!input_is_mt_axis(axis))
  41. dev_warn(&dev->dev,
  42. "DT specifies parameters but the axis is not set up\n");
  43. return;
  44. }
  45. absinfo = &dev->absinfo[axis];
  46. absinfo->maximum = max;
  47. absinfo->fuzz = fuzz;
  48. }
  49. /**
  50. * touchscreen_parse_of_params - parse common touchscreen DT properties
  51. * @dev: device that should be parsed
  52. *
  53. * This function parses common DT properties for touchscreens and setups the
  54. * input device accordingly. The function keeps previously setuped default
  55. * values if no value is specified via DT.
  56. */
  57. void touchscreen_parse_of_params(struct input_dev *dev, bool multitouch)
  58. {
  59. struct device_node *np = dev->dev.parent->of_node;
  60. unsigned int axis;
  61. unsigned int maximum, fuzz;
  62. bool data_present;
  63. input_alloc_absinfo(dev);
  64. if (!dev->absinfo)
  65. return;
  66. axis = multitouch ? ABS_MT_POSITION_X : ABS_X;
  67. data_present = touchscreen_get_prop_u32(np, "touchscreen-size-x",
  68. input_abs_get_max(dev, axis),
  69. &maximum) |
  70. touchscreen_get_prop_u32(np, "touchscreen-fuzz-x",
  71. input_abs_get_fuzz(dev, axis),
  72. &fuzz);
  73. if (data_present)
  74. touchscreen_set_params(dev, axis, maximum, fuzz);
  75. axis = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
  76. data_present = touchscreen_get_prop_u32(np, "touchscreen-size-y",
  77. input_abs_get_max(dev, axis),
  78. &maximum) |
  79. touchscreen_get_prop_u32(np, "touchscreen-fuzz-y",
  80. input_abs_get_fuzz(dev, axis),
  81. &fuzz);
  82. if (data_present)
  83. touchscreen_set_params(dev, axis, maximum, fuzz);
  84. axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
  85. data_present = touchscreen_get_prop_u32(np, "touchscreen-max-pressure",
  86. input_abs_get_max(dev, axis),
  87. &maximum) |
  88. touchscreen_get_prop_u32(np, "touchscreen-fuzz-pressure",
  89. input_abs_get_fuzz(dev, axis),
  90. &fuzz);
  91. if (data_present)
  92. touchscreen_set_params(dev, axis, maximum, fuzz);
  93. }
  94. EXPORT_SYMBOL(touchscreen_parse_of_params);