1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic DT helper functions for touchscreen devices
5 * Copyright (c) 2014 Sebastian Reichel <sre@kernel.org>
8 #include <linux/property.h>
9 #include <linux/input.h>
10 #include <linux/input/mt.h>
11 #include <linux/input/touchscreen.h>
12 #include <linux/module.h>
14 static bool touchscreen_get_prop_u32(struct device
*dev
,
16 unsigned int default_value
,
22 error
= device_property_read_u32(dev
, property
, &val
);
24 *value
= default_value
;
32 static void touchscreen_set_params(struct input_dev
*dev
,
34 int min
, int max
, int fuzz
)
36 struct input_absinfo
*absinfo
;
38 if (!test_bit(axis
, dev
->absbit
)) {
40 "DT specifies parameters but the axis %lu is not set up\n",
45 absinfo
= &dev
->absinfo
[axis
];
46 absinfo
->minimum
= min
;
47 absinfo
->maximum
= max
;
52 * touchscreen_parse_properties - parse common touchscreen DT properties
53 * @input: input device that should be parsed
54 * @multitouch: specifies whether parsed properties should be applied to
55 * single-touch or multi-touch axes
56 * @prop: pointer to a struct touchscreen_properties into which to store
57 * axis swap and invert info for use with touchscreen_report_x_y();
60 * This function parses common DT properties for touchscreens and setups the
61 * input device accordingly. The function keeps previously set up default
62 * values if no value is specified via DT.
64 void touchscreen_parse_properties(struct input_dev
*input
, bool multitouch
,
65 struct touchscreen_properties
*prop
)
67 struct device
*dev
= input
->dev
.parent
;
68 struct input_absinfo
*absinfo
;
70 unsigned int minimum
, maximum
, fuzz
;
73 input_alloc_absinfo(input
);
77 axis
= multitouch
? ABS_MT_POSITION_X
: ABS_X
;
78 data_present
= touchscreen_get_prop_u32(dev
, "touchscreen-min-x",
79 input_abs_get_min(input
, axis
),
81 touchscreen_get_prop_u32(dev
, "touchscreen-size-x",
82 input_abs_get_max(input
,
85 touchscreen_get_prop_u32(dev
, "touchscreen-fuzz-x",
86 input_abs_get_fuzz(input
, axis
),
89 touchscreen_set_params(input
, axis
, minimum
, maximum
- 1, fuzz
);
91 axis
= multitouch
? ABS_MT_POSITION_Y
: ABS_Y
;
92 data_present
= touchscreen_get_prop_u32(dev
, "touchscreen-min-y",
93 input_abs_get_min(input
, axis
),
95 touchscreen_get_prop_u32(dev
, "touchscreen-size-y",
96 input_abs_get_max(input
,
99 touchscreen_get_prop_u32(dev
, "touchscreen-fuzz-y",
100 input_abs_get_fuzz(input
, axis
),
103 touchscreen_set_params(input
, axis
, minimum
, maximum
- 1, fuzz
);
105 axis
= multitouch
? ABS_MT_PRESSURE
: ABS_PRESSURE
;
106 data_present
= touchscreen_get_prop_u32(dev
,
107 "touchscreen-max-pressure",
108 input_abs_get_max(input
, axis
),
110 touchscreen_get_prop_u32(dev
,
111 "touchscreen-fuzz-pressure",
112 input_abs_get_fuzz(input
, axis
),
115 touchscreen_set_params(input
, axis
, 0, maximum
, fuzz
);
120 axis
= multitouch
? ABS_MT_POSITION_X
: ABS_X
;
122 prop
->max_x
= input_abs_get_max(input
, axis
);
123 prop
->max_y
= input_abs_get_max(input
, axis
+ 1);
126 device_property_read_bool(dev
, "touchscreen-inverted-x");
127 if (prop
->invert_x
) {
128 absinfo
= &input
->absinfo
[axis
];
129 absinfo
->maximum
-= absinfo
->minimum
;
130 absinfo
->minimum
= 0;
134 device_property_read_bool(dev
, "touchscreen-inverted-y");
135 if (prop
->invert_y
) {
136 absinfo
= &input
->absinfo
[axis
+ 1];
137 absinfo
->maximum
-= absinfo
->minimum
;
138 absinfo
->minimum
= 0;
142 device_property_read_bool(dev
, "touchscreen-swapped-x-y");
144 swap(input
->absinfo
[axis
], input
->absinfo
[axis
+ 1]);
146 EXPORT_SYMBOL(touchscreen_parse_properties
);
149 touchscreen_apply_prop_to_x_y(const struct touchscreen_properties
*prop
,
150 unsigned int *x
, unsigned int *y
)
153 *x
= prop
->max_x
- *x
;
156 *y
= prop
->max_y
- *y
;
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
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.
173 void touchscreen_set_mt_pos(struct input_mt_pos
*pos
,
174 const struct touchscreen_properties
*prop
,
175 unsigned int x
, unsigned int y
)
177 touchscreen_apply_prop_to_x_y(prop
, &x
, &y
);
181 EXPORT_SYMBOL(touchscreen_set_mt_pos
);
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
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.
195 void touchscreen_report_pos(struct input_dev
*input
,
196 const struct touchscreen_properties
*prop
,
197 unsigned int x
, unsigned int y
,
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
);
204 EXPORT_SYMBOL(touchscreen_report_pos
);
206 MODULE_LICENSE("GPL v2");
207 MODULE_DESCRIPTION("Device-tree helpers functions for touchscreen devices");