rmi_2d_sensor.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2016 Synaptics Incorporated
  4. * Copyright (c) 2011 Unixphere
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/device.h>
  8. #include <linux/of.h>
  9. #include <linux/input.h>
  10. #include <linux/input/mt.h>
  11. #include <linux/rmi.h>
  12. #include "rmi_driver.h"
  13. #include "rmi_2d_sensor.h"
  14. #define RMI_2D_REL_POS_MIN -128
  15. #define RMI_2D_REL_POS_MAX 127
  16. /* maximum ABS_MT_POSITION displacement (in mm) */
  17. #define DMAX 10
  18. void rmi_2d_sensor_abs_process(struct rmi_2d_sensor *sensor,
  19. struct rmi_2d_sensor_abs_object *obj,
  20. int slot)
  21. {
  22. struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;
  23. /* we keep the previous values if the finger is released */
  24. if (obj->type == RMI_2D_OBJECT_NONE)
  25. return;
  26. if (axis_align->flip_x)
  27. obj->x = sensor->max_x - obj->x;
  28. if (axis_align->flip_y)
  29. obj->y = sensor->max_y - obj->y;
  30. if (axis_align->swap_axes)
  31. swap(obj->x, obj->y);
  32. /*
  33. * Here checking if X offset or y offset are specified is
  34. * redundant. We just add the offsets or clip the values.
  35. *
  36. * Note: offsets need to be applied before clipping occurs,
  37. * or we could get funny values that are outside of
  38. * clipping boundaries.
  39. */
  40. obj->x += axis_align->offset_x;
  41. obj->y += axis_align->offset_y;
  42. obj->x = max(axis_align->clip_x_low, obj->x);
  43. obj->y = max(axis_align->clip_y_low, obj->y);
  44. if (axis_align->clip_x_high)
  45. obj->x = min(sensor->max_x, obj->x);
  46. if (axis_align->clip_y_high)
  47. obj->y = min(sensor->max_y, obj->y);
  48. sensor->tracking_pos[slot].x = obj->x;
  49. sensor->tracking_pos[slot].y = obj->y;
  50. }
  51. EXPORT_SYMBOL_GPL(rmi_2d_sensor_abs_process);
  52. void rmi_2d_sensor_abs_report(struct rmi_2d_sensor *sensor,
  53. struct rmi_2d_sensor_abs_object *obj,
  54. int slot)
  55. {
  56. struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;
  57. struct input_dev *input = sensor->input;
  58. int wide, major, minor;
  59. if (sensor->kernel_tracking)
  60. input_mt_slot(input, sensor->tracking_slots[slot]);
  61. else
  62. input_mt_slot(input, slot);
  63. input_mt_report_slot_state(input, obj->mt_tool,
  64. obj->type != RMI_2D_OBJECT_NONE);
  65. if (obj->type != RMI_2D_OBJECT_NONE) {
  66. obj->x = sensor->tracking_pos[slot].x;
  67. obj->y = sensor->tracking_pos[slot].y;
  68. if (axis_align->swap_axes)
  69. swap(obj->wx, obj->wy);
  70. wide = (obj->wx > obj->wy);
  71. major = max(obj->wx, obj->wy);
  72. minor = min(obj->wx, obj->wy);
  73. if (obj->type == RMI_2D_OBJECT_STYLUS) {
  74. major = max(1, major);
  75. minor = max(1, minor);
  76. }
  77. input_event(sensor->input, EV_ABS, ABS_MT_POSITION_X, obj->x);
  78. input_event(sensor->input, EV_ABS, ABS_MT_POSITION_Y, obj->y);
  79. input_event(sensor->input, EV_ABS, ABS_MT_ORIENTATION, wide);
  80. input_event(sensor->input, EV_ABS, ABS_MT_PRESSURE, obj->z);
  81. input_event(sensor->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
  82. input_event(sensor->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);
  83. rmi_dbg(RMI_DEBUG_2D_SENSOR, &sensor->input->dev,
  84. "%s: obj[%d]: type: 0x%02x X: %d Y: %d Z: %d WX: %d WY: %d\n",
  85. __func__, slot, obj->type, obj->x, obj->y, obj->z,
  86. obj->wx, obj->wy);
  87. }
  88. }
  89. EXPORT_SYMBOL_GPL(rmi_2d_sensor_abs_report);
  90. void rmi_2d_sensor_rel_report(struct rmi_2d_sensor *sensor, int x, int y)
  91. {
  92. struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;
  93. x = min(RMI_2D_REL_POS_MAX, max(RMI_2D_REL_POS_MIN, (int)x));
  94. y = min(RMI_2D_REL_POS_MAX, max(RMI_2D_REL_POS_MIN, (int)y));
  95. if (axis_align->flip_x)
  96. x = min(RMI_2D_REL_POS_MAX, -x);
  97. if (axis_align->flip_y)
  98. y = min(RMI_2D_REL_POS_MAX, -y);
  99. if (axis_align->swap_axes)
  100. swap(x, y);
  101. if (x || y) {
  102. input_report_rel(sensor->input, REL_X, x);
  103. input_report_rel(sensor->input, REL_Y, y);
  104. }
  105. }
  106. EXPORT_SYMBOL_GPL(rmi_2d_sensor_rel_report);
  107. static void rmi_2d_sensor_set_input_params(struct rmi_2d_sensor *sensor)
  108. {
  109. struct input_dev *input = sensor->input;
  110. int res_x;
  111. int res_y;
  112. int max_x, max_y;
  113. int input_flags = 0;
  114. if (sensor->report_abs) {
  115. sensor->min_x = sensor->axis_align.clip_x_low;
  116. if (sensor->axis_align.clip_x_high)
  117. sensor->max_x = min(sensor->max_x,
  118. sensor->axis_align.clip_x_high);
  119. sensor->min_y = sensor->axis_align.clip_y_low;
  120. if (sensor->axis_align.clip_y_high)
  121. sensor->max_y = min(sensor->max_y,
  122. sensor->axis_align.clip_y_high);
  123. set_bit(EV_ABS, input->evbit);
  124. max_x = sensor->max_x;
  125. max_y = sensor->max_y;
  126. if (sensor->axis_align.swap_axes)
  127. swap(max_x, max_y);
  128. input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
  129. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);
  130. if (sensor->x_mm && sensor->y_mm) {
  131. res_x = (sensor->max_x - sensor->min_x) / sensor->x_mm;
  132. res_y = (sensor->max_y - sensor->min_y) / sensor->y_mm;
  133. if (sensor->axis_align.swap_axes)
  134. swap(res_x, res_y);
  135. input_abs_set_res(input, ABS_X, res_x);
  136. input_abs_set_res(input, ABS_Y, res_y);
  137. input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
  138. input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);
  139. if (!sensor->dmax)
  140. sensor->dmax = DMAX * res_x;
  141. }
  142. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
  143. input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
  144. input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
  145. input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  146. input_set_abs_params(input, ABS_MT_TOOL_TYPE,
  147. 0, MT_TOOL_MAX, 0, 0);
  148. if (sensor->sensor_type == rmi_sensor_touchpad)
  149. input_flags = INPUT_MT_POINTER;
  150. else
  151. input_flags = INPUT_MT_DIRECT;
  152. if (sensor->kernel_tracking)
  153. input_flags |= INPUT_MT_TRACK;
  154. input_mt_init_slots(input, sensor->nbr_fingers, input_flags);
  155. }
  156. if (sensor->report_rel) {
  157. set_bit(EV_REL, input->evbit);
  158. set_bit(REL_X, input->relbit);
  159. set_bit(REL_Y, input->relbit);
  160. }
  161. if (sensor->topbuttonpad)
  162. set_bit(INPUT_PROP_TOPBUTTONPAD, input->propbit);
  163. }
  164. int rmi_2d_sensor_configure_input(struct rmi_function *fn,
  165. struct rmi_2d_sensor *sensor)
  166. {
  167. struct rmi_device *rmi_dev = fn->rmi_dev;
  168. struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
  169. if (!drv_data->input)
  170. return -ENODEV;
  171. sensor->input = drv_data->input;
  172. rmi_2d_sensor_set_input_params(sensor);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(rmi_2d_sensor_configure_input);
  176. #ifdef CONFIG_OF
  177. int rmi_2d_sensor_of_probe(struct device *dev,
  178. struct rmi_2d_sensor_platform_data *pdata)
  179. {
  180. int retval;
  181. u32 val;
  182. pdata->axis_align.swap_axes = of_property_read_bool(dev->of_node,
  183. "touchscreen-swapped-x-y");
  184. pdata->axis_align.flip_x = of_property_read_bool(dev->of_node,
  185. "touchscreen-inverted-x");
  186. pdata->axis_align.flip_y = of_property_read_bool(dev->of_node,
  187. "touchscreen-inverted-y");
  188. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-low", 1);
  189. if (retval)
  190. return retval;
  191. pdata->axis_align.clip_x_low = val;
  192. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-low", 1);
  193. if (retval)
  194. return retval;
  195. pdata->axis_align.clip_y_low = val;
  196. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-high", 1);
  197. if (retval)
  198. return retval;
  199. pdata->axis_align.clip_x_high = val;
  200. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-high", 1);
  201. if (retval)
  202. return retval;
  203. pdata->axis_align.clip_y_high = val;
  204. retval = rmi_of_property_read_u32(dev, &val, "syna,offset-x", 1);
  205. if (retval)
  206. return retval;
  207. pdata->axis_align.offset_x = val;
  208. retval = rmi_of_property_read_u32(dev, &val, "syna,offset-y", 1);
  209. if (retval)
  210. return retval;
  211. pdata->axis_align.offset_y = val;
  212. retval = rmi_of_property_read_u32(dev, &val, "syna,delta-x-threshold",
  213. 1);
  214. if (retval)
  215. return retval;
  216. pdata->axis_align.delta_x_threshold = val;
  217. retval = rmi_of_property_read_u32(dev, &val, "syna,delta-y-threshold",
  218. 1);
  219. if (retval)
  220. return retval;
  221. pdata->axis_align.delta_y_threshold = val;
  222. retval = rmi_of_property_read_u32(dev, (u32 *)&pdata->sensor_type,
  223. "syna,sensor-type", 1);
  224. if (retval)
  225. return retval;
  226. retval = rmi_of_property_read_u32(dev, &val, "touchscreen-x-mm", 1);
  227. if (retval)
  228. return retval;
  229. pdata->x_mm = val;
  230. retval = rmi_of_property_read_u32(dev, &val, "touchscreen-y-mm", 1);
  231. if (retval)
  232. return retval;
  233. pdata->y_mm = val;
  234. retval = rmi_of_property_read_u32(dev, &val,
  235. "syna,disable-report-mask", 1);
  236. if (retval)
  237. return retval;
  238. pdata->disable_report_mask = val;
  239. retval = rmi_of_property_read_u32(dev, &val, "syna,rezero-wait-ms",
  240. 1);
  241. if (retval)
  242. return retval;
  243. pdata->rezero_wait = val;
  244. return 0;
  245. }
  246. #else
  247. inline int rmi_2d_sensor_of_probe(struct device *dev,
  248. struct rmi_2d_sensor_platform_data *pdata)
  249. {
  250. return -ENODEV;
  251. }
  252. #endif
  253. EXPORT_SYMBOL_GPL(rmi_2d_sensor_of_probe);