rmi_2d_sensor.c 8.6 KB

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