blob: 4620e20d0190f7bb0e5ab197cca0765dcd8ad821 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Sebastian Reichelb98abe52014-05-28 23:51:53 -07002/*
Jeff LaBundy51e01fc2021-03-22 16:43:00 -07003 * Generic helper functions for touchscreens and other two-dimensional
4 * pointing devices
Sebastian Reichelb98abe52014-05-28 23:51:53 -07005 *
6 * Copyright (c) 2014 Sebastian Reichel <sre@kernel.org>
Sebastian Reichelb98abe52014-05-28 23:51:53 -07007 */
8
Dmitry Torokhov4200e832015-07-06 15:18:24 -07009#include <linux/property.h>
Sebastian Reichelb98abe52014-05-28 23:51:53 -070010#include <linux/input.h>
Maxime Ripard0a363a32015-03-21 20:17:57 -070011#include <linux/input/mt.h>
Sebastian Reichelb98abe52014-05-28 23:51:53 -070012#include <linux/input/touchscreen.h>
Arnd Bergmann43173a02018-01-10 11:29:23 -080013#include <linux/module.h>
Sebastian Reichelb98abe52014-05-28 23:51:53 -070014
Dmitry Torokhov4200e832015-07-06 15:18:24 -070015static bool touchscreen_get_prop_u32(struct device *dev,
Dmitry Torokhov7c494372015-06-01 10:35:16 -070016 const char *property,
17 unsigned int default_value,
18 unsigned int *value)
Maxime Ripard3eea8b52015-03-21 20:17:48 -070019{
Dmitry Torokhov7c494372015-06-01 10:35:16 -070020 u32 val;
21 int error;
Maxime Ripard3eea8b52015-03-21 20:17:48 -070022
Dmitry Torokhov4200e832015-07-06 15:18:24 -070023 error = device_property_read_u32(dev, property, &val);
Dmitry Torokhov7c494372015-06-01 10:35:16 -070024 if (error) {
25 *value = default_value;
26 return false;
27 }
Maxime Ripard3eea8b52015-03-21 20:17:48 -070028
Dmitry Torokhov7c494372015-06-01 10:35:16 -070029 *value = val;
30 return true;
Maxime Ripard3eea8b52015-03-21 20:17:48 -070031}
32
33static void touchscreen_set_params(struct input_dev *dev,
34 unsigned long axis,
Hans de Goeded9265e82018-10-11 17:04:13 -070035 int min, int max, int fuzz)
Maxime Ripard3eea8b52015-03-21 20:17:48 -070036{
37 struct input_absinfo *absinfo;
38
39 if (!test_bit(axis, dev->absbit)) {
Dmitry Torokhovf61fd212015-07-06 14:45:25 -070040 dev_warn(&dev->dev,
Jeff LaBundy51e01fc2021-03-22 16:43:00 -070041 "Parameters are specified but the axis %lu is not set up\n",
Dmitry Torokhovf61fd212015-07-06 14:45:25 -070042 axis);
Maxime Ripard3eea8b52015-03-21 20:17:48 -070043 return;
44 }
45
46 absinfo = &dev->absinfo[axis];
Hans de Goeded9265e82018-10-11 17:04:13 -070047 absinfo->minimum = min;
Maxime Ripard3eea8b52015-03-21 20:17:48 -070048 absinfo->maximum = max;
49 absinfo->fuzz = fuzz;
50}
51
Sebastian Reichelb98abe52014-05-28 23:51:53 -070052/**
Jeff LaBundy51e01fc2021-03-22 16:43:00 -070053 * touchscreen_parse_properties - parse common touchscreen properties
Dmitry Torokhov4200e832015-07-06 15:18:24 -070054 * @input: input device that should be parsed
55 * @multitouch: specifies whether parsed properties should be applied to
56 * single-touch or multi-touch axes
Hans de Goedeed7c9872016-07-15 14:05:29 -070057 * @prop: pointer to a struct touchscreen_properties into which to store
58 * axis swap and invert info for use with touchscreen_report_x_y();
59 * or %NULL
Sebastian Reichelb98abe52014-05-28 23:51:53 -070060 *
Jeff LaBundy51e01fc2021-03-22 16:43:00 -070061 * This function parses common properties for touchscreens and sets up the
Dmitry Torokhov4200e832015-07-06 15:18:24 -070062 * input device accordingly. The function keeps previously set up default
Jeff LaBundy51e01fc2021-03-22 16:43:00 -070063 * values if no value is specified.
Sebastian Reichelb98abe52014-05-28 23:51:53 -070064 */
Hans de Goedeed7c9872016-07-15 14:05:29 -070065void touchscreen_parse_properties(struct input_dev *input, bool multitouch,
66 struct touchscreen_properties *prop)
Sebastian Reichelb98abe52014-05-28 23:51:53 -070067{
Dmitry Torokhov4200e832015-07-06 15:18:24 -070068 struct device *dev = input->dev.parent;
Hans de Goeded9265e82018-10-11 17:04:13 -070069 struct input_absinfo *absinfo;
Andy Shevchenkocc5117d2020-03-06 17:11:46 -080070 unsigned int axis, axis_x, axis_y;
Hans de Goeded9265e82018-10-11 17:04:13 -070071 unsigned int minimum, maximum, fuzz;
Dmitry Torokhov7c494372015-06-01 10:35:16 -070072 bool data_present;
Sebastian Reichelb98abe52014-05-28 23:51:53 -070073
Dmitry Torokhov4200e832015-07-06 15:18:24 -070074 input_alloc_absinfo(input);
75 if (!input->absinfo)
Sebastian Reichelb98abe52014-05-28 23:51:53 -070076 return;
77
Andy Shevchenkocc5117d2020-03-06 17:11:46 -080078 axis_x = multitouch ? ABS_MT_POSITION_X : ABS_X;
79 axis_y = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
80
Hans de Goeded9265e82018-10-11 17:04:13 -070081 data_present = touchscreen_get_prop_u32(dev, "touchscreen-min-x",
Andy Shevchenkocc5117d2020-03-06 17:11:46 -080082 input_abs_get_min(input, axis_x),
Nathan Chancellora02dcde2021-10-15 13:13:06 -070083 &minimum);
84 data_present |= touchscreen_get_prop_u32(dev, "touchscreen-size-x",
85 input_abs_get_max(input,
86 axis_x) + 1,
87 &maximum);
88 data_present |= touchscreen_get_prop_u32(dev, "touchscreen-fuzz-x",
89 input_abs_get_fuzz(input, axis_x),
90 &fuzz);
Dmitry Torokhov7c494372015-06-01 10:35:16 -070091 if (data_present)
Andy Shevchenkocc5117d2020-03-06 17:11:46 -080092 touchscreen_set_params(input, axis_x, minimum, maximum - 1, fuzz);
Sebastian Reichelb98abe52014-05-28 23:51:53 -070093
Hans de Goeded9265e82018-10-11 17:04:13 -070094 data_present = touchscreen_get_prop_u32(dev, "touchscreen-min-y",
Andy Shevchenkocc5117d2020-03-06 17:11:46 -080095 input_abs_get_min(input, axis_y),
Nathan Chancellora02dcde2021-10-15 13:13:06 -070096 &minimum);
97 data_present |= touchscreen_get_prop_u32(dev, "touchscreen-size-y",
98 input_abs_get_max(input,
99 axis_y) + 1,
100 &maximum);
101 data_present |= touchscreen_get_prop_u32(dev, "touchscreen-fuzz-y",
102 input_abs_get_fuzz(input, axis_y),
103 &fuzz);
Dmitry Torokhov7c494372015-06-01 10:35:16 -0700104 if (data_present)
Andy Shevchenkocc5117d2020-03-06 17:11:46 -0800105 touchscreen_set_params(input, axis_y, minimum, maximum - 1, fuzz);
Sebastian Reichelb98abe52014-05-28 23:51:53 -0700106
Dmitry Torokhov7c494372015-06-01 10:35:16 -0700107 axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
Dmitry Torokhov4200e832015-07-06 15:18:24 -0700108 data_present = touchscreen_get_prop_u32(dev,
109 "touchscreen-max-pressure",
110 input_abs_get_max(input, axis),
Nathan Chancellora02dcde2021-10-15 13:13:06 -0700111 &maximum);
112 data_present |= touchscreen_get_prop_u32(dev,
113 "touchscreen-fuzz-pressure",
114 input_abs_get_fuzz(input, axis),
115 &fuzz);
Dmitry Torokhov7c494372015-06-01 10:35:16 -0700116 if (data_present)
Hans de Goeded9265e82018-10-11 17:04:13 -0700117 touchscreen_set_params(input, axis, 0, maximum, fuzz);
Hans de Goedeed7c9872016-07-15 14:05:29 -0700118
119 if (!prop)
120 return;
121
Andy Shevchenkocc5117d2020-03-06 17:11:46 -0800122 prop->max_x = input_abs_get_max(input, axis_x);
123 prop->max_y = input_abs_get_max(input, axis_y);
Hans de Goeded9265e82018-10-11 17:04:13 -0700124
Hans de Goedeed7c9872016-07-15 14:05:29 -0700125 prop->invert_x =
126 device_property_read_bool(dev, "touchscreen-inverted-x");
Hans de Goeded9265e82018-10-11 17:04:13 -0700127 if (prop->invert_x) {
Andy Shevchenkocc5117d2020-03-06 17:11:46 -0800128 absinfo = &input->absinfo[axis_x];
Hans de Goeded9265e82018-10-11 17:04:13 -0700129 absinfo->maximum -= absinfo->minimum;
130 absinfo->minimum = 0;
131 }
132
Hans de Goedeed7c9872016-07-15 14:05:29 -0700133 prop->invert_y =
134 device_property_read_bool(dev, "touchscreen-inverted-y");
Hans de Goeded9265e82018-10-11 17:04:13 -0700135 if (prop->invert_y) {
Andy Shevchenkocc5117d2020-03-06 17:11:46 -0800136 absinfo = &input->absinfo[axis_y];
Hans de Goeded9265e82018-10-11 17:04:13 -0700137 absinfo->maximum -= absinfo->minimum;
138 absinfo->minimum = 0;
139 }
140
Hans de Goedeed7c9872016-07-15 14:05:29 -0700141 prop->swap_x_y =
142 device_property_read_bool(dev, "touchscreen-swapped-x-y");
Hans de Goedeed7c9872016-07-15 14:05:29 -0700143 if (prop->swap_x_y)
Andy Shevchenkocc5117d2020-03-06 17:11:46 -0800144 swap(input->absinfo[axis_x], input->absinfo[axis_y]);
Sebastian Reichelb98abe52014-05-28 23:51:53 -0700145}
Dmitry Torokhov4200e832015-07-06 15:18:24 -0700146EXPORT_SYMBOL(touchscreen_parse_properties);
Hans de Goedeed7c9872016-07-15 14:05:29 -0700147
148static void
149touchscreen_apply_prop_to_x_y(const struct touchscreen_properties *prop,
150 unsigned int *x, unsigned int *y)
151{
152 if (prop->invert_x)
153 *x = prop->max_x - *x;
154
155 if (prop->invert_y)
156 *y = prop->max_y - *y;
157
158 if (prop->swap_x_y)
159 swap(*x, *y);
160}
161
162/**
163 * touchscreen_set_mt_pos - Set input_mt_pos coordinates
164 * @pos: input_mt_pos to set coordinates of
165 * @prop: pointer to a struct touchscreen_properties
166 * @x: X coordinate to store in pos
167 * @y: Y coordinate to store in pos
168 *
169 * Adjust the passed in x and y values applying any axis inversion and
170 * swapping requested in the passed in touchscreen_properties and store
171 * the result in a struct input_mt_pos.
172 */
173void touchscreen_set_mt_pos(struct input_mt_pos *pos,
174 const struct touchscreen_properties *prop,
175 unsigned int x, unsigned int y)
176{
177 touchscreen_apply_prop_to_x_y(prop, &x, &y);
178 pos->x = x;
179 pos->y = y;
180}
181EXPORT_SYMBOL(touchscreen_set_mt_pos);
182
183/**
184 * touchscreen_report_pos - Report touchscreen coordinates
185 * @input: input_device to report coordinates for
186 * @prop: pointer to a struct touchscreen_properties
187 * @x: X coordinate to report
188 * @y: Y coordinate to report
189 * @multitouch: Report coordinates on single-touch or multi-touch axes
190 *
191 * Adjust the passed in x and y values applying any axis inversion and
192 * swapping requested in the passed in touchscreen_properties and then
193 * report the resulting coordinates on the input_dev's x and y axis.
194 */
195void touchscreen_report_pos(struct input_dev *input,
196 const struct touchscreen_properties *prop,
197 unsigned int x, unsigned int y,
198 bool multitouch)
199{
200 touchscreen_apply_prop_to_x_y(prop, &x, &y);
201 input_report_abs(input, multitouch ? ABS_MT_POSITION_X : ABS_X, x);
202 input_report_abs(input, multitouch ? ABS_MT_POSITION_Y : ABS_Y, y);
203}
204EXPORT_SYMBOL(touchscreen_report_pos);
Arnd Bergmann43173a02018-01-10 11:29:23 -0800205
206MODULE_LICENSE("GPL v2");
Jeff LaBundy51e01fc2021-03-22 16:43:00 -0700207MODULE_DESCRIPTION("Helper functions for touchscreens and other devices");