1e28b5c8dSJeff LaBundy // SPDX-License-Identifier: GPL-2.0-only
2e28b5c8dSJeff LaBundy /*
351e01fc0SJeff LaBundy * Generic helper functions for touchscreens and other two-dimensional
451e01fc0SJeff LaBundy * pointing devices
5e28b5c8dSJeff LaBundy *
6e28b5c8dSJeff LaBundy * Copyright (c) 2014 Sebastian Reichel <[email protected]>
7e28b5c8dSJeff LaBundy */
8e28b5c8dSJeff LaBundy
9e28b5c8dSJeff LaBundy #include <linux/property.h>
10e28b5c8dSJeff LaBundy #include <linux/input.h>
11e28b5c8dSJeff LaBundy #include <linux/input/mt.h>
12e28b5c8dSJeff LaBundy #include <linux/input/touchscreen.h>
13e28b5c8dSJeff LaBundy #include <linux/module.h>
14e28b5c8dSJeff LaBundy
touchscreen_get_prop_u32(struct device * dev,const char * property,unsigned int default_value,unsigned int * value)15e28b5c8dSJeff LaBundy static bool touchscreen_get_prop_u32(struct device *dev,
16e28b5c8dSJeff LaBundy const char *property,
17e28b5c8dSJeff LaBundy unsigned int default_value,
18e28b5c8dSJeff LaBundy unsigned int *value)
19e28b5c8dSJeff LaBundy {
20e28b5c8dSJeff LaBundy u32 val;
21e28b5c8dSJeff LaBundy int error;
22e28b5c8dSJeff LaBundy
23e28b5c8dSJeff LaBundy error = device_property_read_u32(dev, property, &val);
24e28b5c8dSJeff LaBundy if (error) {
25e28b5c8dSJeff LaBundy *value = default_value;
26e28b5c8dSJeff LaBundy return false;
27e28b5c8dSJeff LaBundy }
28e28b5c8dSJeff LaBundy
29e28b5c8dSJeff LaBundy *value = val;
30e28b5c8dSJeff LaBundy return true;
31e28b5c8dSJeff LaBundy }
32e28b5c8dSJeff LaBundy
touchscreen_set_params(struct input_dev * dev,unsigned long axis,int min,int max,int fuzz)33e28b5c8dSJeff LaBundy static void touchscreen_set_params(struct input_dev *dev,
34e28b5c8dSJeff LaBundy unsigned long axis,
35e28b5c8dSJeff LaBundy int min, int max, int fuzz)
36e28b5c8dSJeff LaBundy {
37e28b5c8dSJeff LaBundy struct input_absinfo *absinfo;
38e28b5c8dSJeff LaBundy
39e28b5c8dSJeff LaBundy if (!test_bit(axis, dev->absbit)) {
40e28b5c8dSJeff LaBundy dev_warn(&dev->dev,
4151e01fc0SJeff LaBundy "Parameters are specified but the axis %lu is not set up\n",
42e28b5c8dSJeff LaBundy axis);
43e28b5c8dSJeff LaBundy return;
44e28b5c8dSJeff LaBundy }
45e28b5c8dSJeff LaBundy
46e28b5c8dSJeff LaBundy absinfo = &dev->absinfo[axis];
47e28b5c8dSJeff LaBundy absinfo->minimum = min;
48e28b5c8dSJeff LaBundy absinfo->maximum = max;
49e28b5c8dSJeff LaBundy absinfo->fuzz = fuzz;
50e28b5c8dSJeff LaBundy }
51e28b5c8dSJeff LaBundy
52e28b5c8dSJeff LaBundy /**
5351e01fc0SJeff LaBundy * touchscreen_parse_properties - parse common touchscreen properties
54e28b5c8dSJeff LaBundy * @input: input device that should be parsed
55e28b5c8dSJeff LaBundy * @multitouch: specifies whether parsed properties should be applied to
56e28b5c8dSJeff LaBundy * single-touch or multi-touch axes
57e28b5c8dSJeff LaBundy * @prop: pointer to a struct touchscreen_properties into which to store
58e28b5c8dSJeff LaBundy * axis swap and invert info for use with touchscreen_report_x_y();
59e28b5c8dSJeff LaBundy * or %NULL
60e28b5c8dSJeff LaBundy *
6151e01fc0SJeff LaBundy * This function parses common properties for touchscreens and sets up the
62e28b5c8dSJeff LaBundy * input device accordingly. The function keeps previously set up default
6351e01fc0SJeff LaBundy * values if no value is specified.
64e28b5c8dSJeff LaBundy */
touchscreen_parse_properties(struct input_dev * input,bool multitouch,struct touchscreen_properties * prop)65e28b5c8dSJeff LaBundy void touchscreen_parse_properties(struct input_dev *input, bool multitouch,
66e28b5c8dSJeff LaBundy struct touchscreen_properties *prop)
67e28b5c8dSJeff LaBundy {
68e28b5c8dSJeff LaBundy struct device *dev = input->dev.parent;
69e28b5c8dSJeff LaBundy struct input_absinfo *absinfo;
70e28b5c8dSJeff LaBundy unsigned int axis, axis_x, axis_y;
71e28b5c8dSJeff LaBundy unsigned int minimum, maximum, fuzz;
72e28b5c8dSJeff LaBundy bool data_present;
73e28b5c8dSJeff LaBundy
74e28b5c8dSJeff LaBundy input_alloc_absinfo(input);
75e28b5c8dSJeff LaBundy if (!input->absinfo)
76e28b5c8dSJeff LaBundy return;
77e28b5c8dSJeff LaBundy
78e28b5c8dSJeff LaBundy axis_x = multitouch ? ABS_MT_POSITION_X : ABS_X;
79e28b5c8dSJeff LaBundy axis_y = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
80e28b5c8dSJeff LaBundy
81e28b5c8dSJeff LaBundy data_present = touchscreen_get_prop_u32(dev, "touchscreen-min-x",
82e28b5c8dSJeff LaBundy input_abs_get_min(input, axis_x),
83*a02dcde5SNathan Chancellor &minimum);
84*a02dcde5SNathan Chancellor data_present |= touchscreen_get_prop_u32(dev, "touchscreen-size-x",
85e28b5c8dSJeff LaBundy input_abs_get_max(input,
86e28b5c8dSJeff LaBundy axis_x) + 1,
87*a02dcde5SNathan Chancellor &maximum);
88*a02dcde5SNathan Chancellor data_present |= touchscreen_get_prop_u32(dev, "touchscreen-fuzz-x",
89e28b5c8dSJeff LaBundy input_abs_get_fuzz(input, axis_x),
90e28b5c8dSJeff LaBundy &fuzz);
91e28b5c8dSJeff LaBundy if (data_present)
92e28b5c8dSJeff LaBundy touchscreen_set_params(input, axis_x, minimum, maximum - 1, fuzz);
93e28b5c8dSJeff LaBundy
94e28b5c8dSJeff LaBundy data_present = touchscreen_get_prop_u32(dev, "touchscreen-min-y",
95e28b5c8dSJeff LaBundy input_abs_get_min(input, axis_y),
96*a02dcde5SNathan Chancellor &minimum);
97*a02dcde5SNathan Chancellor data_present |= touchscreen_get_prop_u32(dev, "touchscreen-size-y",
98e28b5c8dSJeff LaBundy input_abs_get_max(input,
99e28b5c8dSJeff LaBundy axis_y) + 1,
100*a02dcde5SNathan Chancellor &maximum);
101*a02dcde5SNathan Chancellor data_present |= touchscreen_get_prop_u32(dev, "touchscreen-fuzz-y",
102e28b5c8dSJeff LaBundy input_abs_get_fuzz(input, axis_y),
103e28b5c8dSJeff LaBundy &fuzz);
104e28b5c8dSJeff LaBundy if (data_present)
105e28b5c8dSJeff LaBundy touchscreen_set_params(input, axis_y, minimum, maximum - 1, fuzz);
106e28b5c8dSJeff LaBundy
107e28b5c8dSJeff LaBundy axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
108e28b5c8dSJeff LaBundy data_present = touchscreen_get_prop_u32(dev,
109e28b5c8dSJeff LaBundy "touchscreen-max-pressure",
110e28b5c8dSJeff LaBundy input_abs_get_max(input, axis),
111*a02dcde5SNathan Chancellor &maximum);
112*a02dcde5SNathan Chancellor data_present |= touchscreen_get_prop_u32(dev,
113e28b5c8dSJeff LaBundy "touchscreen-fuzz-pressure",
114e28b5c8dSJeff LaBundy input_abs_get_fuzz(input, axis),
115e28b5c8dSJeff LaBundy &fuzz);
116e28b5c8dSJeff LaBundy if (data_present)
117e28b5c8dSJeff LaBundy touchscreen_set_params(input, axis, 0, maximum, fuzz);
118e28b5c8dSJeff LaBundy
119e28b5c8dSJeff LaBundy if (!prop)
120e28b5c8dSJeff LaBundy return;
121e28b5c8dSJeff LaBundy
122e28b5c8dSJeff LaBundy prop->max_x = input_abs_get_max(input, axis_x);
123e28b5c8dSJeff LaBundy prop->max_y = input_abs_get_max(input, axis_y);
124e28b5c8dSJeff LaBundy
125e28b5c8dSJeff LaBundy prop->invert_x =
126e28b5c8dSJeff LaBundy device_property_read_bool(dev, "touchscreen-inverted-x");
127e28b5c8dSJeff LaBundy if (prop->invert_x) {
128e28b5c8dSJeff LaBundy absinfo = &input->absinfo[axis_x];
129e28b5c8dSJeff LaBundy absinfo->maximum -= absinfo->minimum;
130e28b5c8dSJeff LaBundy absinfo->minimum = 0;
131e28b5c8dSJeff LaBundy }
132e28b5c8dSJeff LaBundy
133e28b5c8dSJeff LaBundy prop->invert_y =
134e28b5c8dSJeff LaBundy device_property_read_bool(dev, "touchscreen-inverted-y");
135e28b5c8dSJeff LaBundy if (prop->invert_y) {
136e28b5c8dSJeff LaBundy absinfo = &input->absinfo[axis_y];
137e28b5c8dSJeff LaBundy absinfo->maximum -= absinfo->minimum;
138e28b5c8dSJeff LaBundy absinfo->minimum = 0;
139e28b5c8dSJeff LaBundy }
140e28b5c8dSJeff LaBundy
141e28b5c8dSJeff LaBundy prop->swap_x_y =
142e28b5c8dSJeff LaBundy device_property_read_bool(dev, "touchscreen-swapped-x-y");
143e28b5c8dSJeff LaBundy if (prop->swap_x_y)
144e28b5c8dSJeff LaBundy swap(input->absinfo[axis_x], input->absinfo[axis_y]);
145e28b5c8dSJeff LaBundy }
146e28b5c8dSJeff LaBundy EXPORT_SYMBOL(touchscreen_parse_properties);
147e28b5c8dSJeff LaBundy
148e28b5c8dSJeff LaBundy static void
touchscreen_apply_prop_to_x_y(const struct touchscreen_properties * prop,unsigned int * x,unsigned int * y)149e28b5c8dSJeff LaBundy touchscreen_apply_prop_to_x_y(const struct touchscreen_properties *prop,
150e28b5c8dSJeff LaBundy unsigned int *x, unsigned int *y)
151e28b5c8dSJeff LaBundy {
152e28b5c8dSJeff LaBundy if (prop->invert_x)
153e28b5c8dSJeff LaBundy *x = prop->max_x - *x;
154e28b5c8dSJeff LaBundy
155e28b5c8dSJeff LaBundy if (prop->invert_y)
156e28b5c8dSJeff LaBundy *y = prop->max_y - *y;
157e28b5c8dSJeff LaBundy
158e28b5c8dSJeff LaBundy if (prop->swap_x_y)
159e28b5c8dSJeff LaBundy swap(*x, *y);
160e28b5c8dSJeff LaBundy }
161e28b5c8dSJeff LaBundy
162e28b5c8dSJeff LaBundy /**
163e28b5c8dSJeff LaBundy * touchscreen_set_mt_pos - Set input_mt_pos coordinates
164e28b5c8dSJeff LaBundy * @pos: input_mt_pos to set coordinates of
165e28b5c8dSJeff LaBundy * @prop: pointer to a struct touchscreen_properties
166e28b5c8dSJeff LaBundy * @x: X coordinate to store in pos
167e28b5c8dSJeff LaBundy * @y: Y coordinate to store in pos
168e28b5c8dSJeff LaBundy *
169e28b5c8dSJeff LaBundy * Adjust the passed in x and y values applying any axis inversion and
170e28b5c8dSJeff LaBundy * swapping requested in the passed in touchscreen_properties and store
171e28b5c8dSJeff LaBundy * the result in a struct input_mt_pos.
172e28b5c8dSJeff LaBundy */
touchscreen_set_mt_pos(struct input_mt_pos * pos,const struct touchscreen_properties * prop,unsigned int x,unsigned int y)173e28b5c8dSJeff LaBundy void touchscreen_set_mt_pos(struct input_mt_pos *pos,
174e28b5c8dSJeff LaBundy const struct touchscreen_properties *prop,
175e28b5c8dSJeff LaBundy unsigned int x, unsigned int y)
176e28b5c8dSJeff LaBundy {
177e28b5c8dSJeff LaBundy touchscreen_apply_prop_to_x_y(prop, &x, &y);
178e28b5c8dSJeff LaBundy pos->x = x;
179e28b5c8dSJeff LaBundy pos->y = y;
180e28b5c8dSJeff LaBundy }
181e28b5c8dSJeff LaBundy EXPORT_SYMBOL(touchscreen_set_mt_pos);
182e28b5c8dSJeff LaBundy
183e28b5c8dSJeff LaBundy /**
184e28b5c8dSJeff LaBundy * touchscreen_report_pos - Report touchscreen coordinates
185e28b5c8dSJeff LaBundy * @input: input_device to report coordinates for
186e28b5c8dSJeff LaBundy * @prop: pointer to a struct touchscreen_properties
187e28b5c8dSJeff LaBundy * @x: X coordinate to report
188e28b5c8dSJeff LaBundy * @y: Y coordinate to report
189e28b5c8dSJeff LaBundy * @multitouch: Report coordinates on single-touch or multi-touch axes
190e28b5c8dSJeff LaBundy *
191e28b5c8dSJeff LaBundy * Adjust the passed in x and y values applying any axis inversion and
192e28b5c8dSJeff LaBundy * swapping requested in the passed in touchscreen_properties and then
193e28b5c8dSJeff LaBundy * report the resulting coordinates on the input_dev's x and y axis.
194e28b5c8dSJeff LaBundy */
touchscreen_report_pos(struct input_dev * input,const struct touchscreen_properties * prop,unsigned int x,unsigned int y,bool multitouch)195e28b5c8dSJeff LaBundy void touchscreen_report_pos(struct input_dev *input,
196e28b5c8dSJeff LaBundy const struct touchscreen_properties *prop,
197e28b5c8dSJeff LaBundy unsigned int x, unsigned int y,
198e28b5c8dSJeff LaBundy bool multitouch)
199e28b5c8dSJeff LaBundy {
200e28b5c8dSJeff LaBundy touchscreen_apply_prop_to_x_y(prop, &x, &y);
201e28b5c8dSJeff LaBundy input_report_abs(input, multitouch ? ABS_MT_POSITION_X : ABS_X, x);
202e28b5c8dSJeff LaBundy input_report_abs(input, multitouch ? ABS_MT_POSITION_Y : ABS_Y, y);
203e28b5c8dSJeff LaBundy }
204e28b5c8dSJeff LaBundy EXPORT_SYMBOL(touchscreen_report_pos);
205e28b5c8dSJeff LaBundy
206e28b5c8dSJeff LaBundy MODULE_LICENSE("GPL v2");
20751e01fc0SJeff LaBundy MODULE_DESCRIPTION("Helper functions for touchscreens and other devices");
208