rmi_2d_sensor.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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->swap_axes)
  30. swap(obj->x, obj->y);
  31. if (axis_align->flip_x)
  32. obj->x = sensor->max_x - obj->x;
  33. if (axis_align->flip_y)
  34. obj->y = sensor->max_y - 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->swap_axes)
  99. swap(x, y);
  100. if (axis_align->flip_x)
  101. x = min(RMI_2D_REL_POS_MAX, -x);
  102. if (axis_align->flip_y)
  103. y = min(RMI_2D_REL_POS_MAX, -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 input_flags = 0;
  116. if (sensor->report_abs) {
  117. if (sensor->axis_align.swap_axes)
  118. swap(sensor->max_x, sensor->max_y);
  119. sensor->min_x = sensor->axis_align.clip_x_low;
  120. if (sensor->axis_align.clip_x_high)
  121. sensor->max_x = min(sensor->max_x,
  122. sensor->axis_align.clip_x_high);
  123. sensor->min_y = sensor->axis_align.clip_y_low;
  124. if (sensor->axis_align.clip_y_high)
  125. sensor->max_y = min(sensor->max_y,
  126. sensor->axis_align.clip_y_high);
  127. set_bit(EV_ABS, input->evbit);
  128. input_set_abs_params(input, ABS_MT_POSITION_X, 0, sensor->max_x,
  129. 0, 0);
  130. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, sensor->max_y,
  131. 0, 0);
  132. if (sensor->x_mm && sensor->y_mm) {
  133. res_x = (sensor->max_x - sensor->min_x) / sensor->x_mm;
  134. res_y = (sensor->max_y - sensor->min_y) / sensor->y_mm;
  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. if (sensor->sensor_type == rmi_sensor_touchpad)
  147. input_flags = INPUT_MT_POINTER;
  148. else
  149. input_flags = INPUT_MT_DIRECT;
  150. if (sensor->kernel_tracking)
  151. input_flags |= INPUT_MT_TRACK;
  152. input_mt_init_slots(input, sensor->nbr_fingers, input_flags);
  153. }
  154. if (sensor->report_rel) {
  155. set_bit(EV_REL, input->evbit);
  156. set_bit(REL_X, input->relbit);
  157. set_bit(REL_Y, input->relbit);
  158. }
  159. if (sensor->topbuttonpad)
  160. set_bit(INPUT_PROP_TOPBUTTONPAD, input->propbit);
  161. }
  162. EXPORT_SYMBOL_GPL(rmi_2d_sensor_set_input_params);
  163. int rmi_2d_sensor_configure_input(struct rmi_function *fn,
  164. struct rmi_2d_sensor *sensor)
  165. {
  166. struct rmi_device *rmi_dev = fn->rmi_dev;
  167. struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);
  168. if (!drv_data->input)
  169. return -ENODEV;
  170. sensor->input = drv_data->input;
  171. rmi_2d_sensor_set_input_params(sensor);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL_GPL(rmi_2d_sensor_configure_input);
  175. #ifdef CONFIG_OF
  176. int rmi_2d_sensor_of_probe(struct device *dev,
  177. struct rmi_2d_sensor_platform_data *pdata)
  178. {
  179. int retval;
  180. u32 val;
  181. pdata->axis_align.swap_axes = of_property_read_bool(dev->of_node,
  182. "touchscreen-swapped-x-y");
  183. pdata->axis_align.flip_x = of_property_read_bool(dev->of_node,
  184. "touchscreen-inverted-x");
  185. pdata->axis_align.flip_y = of_property_read_bool(dev->of_node,
  186. "touchscreen-inverted-y");
  187. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-low", 1);
  188. if (retval)
  189. return retval;
  190. pdata->axis_align.clip_x_low = val;
  191. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-low", 1);
  192. if (retval)
  193. return retval;
  194. pdata->axis_align.clip_y_low = val;
  195. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-high", 1);
  196. if (retval)
  197. return retval;
  198. pdata->axis_align.clip_x_high = val;
  199. retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-high", 1);
  200. if (retval)
  201. return retval;
  202. pdata->axis_align.clip_y_high = val;
  203. retval = rmi_of_property_read_u32(dev, &val, "syna,offset-x", 1);
  204. if (retval)
  205. return retval;
  206. pdata->axis_align.offset_x = val;
  207. retval = rmi_of_property_read_u32(dev, &val, "syna,offset-y", 1);
  208. if (retval)
  209. return retval;
  210. pdata->axis_align.offset_y = val;
  211. retval = rmi_of_property_read_u32(dev, &val, "syna,delta-x-threshold",
  212. 1);
  213. if (retval)
  214. return retval;
  215. pdata->axis_align.delta_x_threshold = val;
  216. retval = rmi_of_property_read_u32(dev, &val, "syna,delta-y-threshold",
  217. 1);
  218. if (retval)
  219. return retval;
  220. pdata->axis_align.delta_y_threshold = val;
  221. retval = rmi_of_property_read_u32(dev, (u32 *)&pdata->sensor_type,
  222. "syna,sensor-type", 1);
  223. if (retval)
  224. return retval;
  225. retval = rmi_of_property_read_u32(dev, &val, "touchscreen-x-mm", 1);
  226. if (retval)
  227. return retval;
  228. pdata->x_mm = val;
  229. retval = rmi_of_property_read_u32(dev, &val, "touchscreen-y-mm", 1);
  230. if (retval)
  231. return retval;
  232. pdata->y_mm = val;
  233. retval = rmi_of_property_read_u32(dev, &val,
  234. "syna,disable-report-mask", 1);
  235. if (retval)
  236. return retval;
  237. pdata->disable_report_mask = val;
  238. retval = rmi_of_property_read_u32(dev, &val, "syna,rezero-wait-ms",
  239. 1);
  240. if (retval)
  241. return retval;
  242. pdata->rezero_wait = val;
  243. return 0;
  244. }
  245. #else
  246. inline int rmi_2d_sensor_of_probe(struct device *dev,
  247. struct rmi_2d_sensor_platform_data *pdata)
  248. {
  249. return -ENODEV;
  250. }
  251. #endif
  252. EXPORT_SYMBOL_GPL(rmi_2d_sensor_of_probe);