rmi_2d_sensor.c 8.7 KB

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