1
2
3
4
5
6
7
8
9
10
11
12#include <linux/property.h>
13#include <linux/input.h>
14#include <linux/input/mt.h>
15#include <linux/input/touchscreen.h>
16#include <linux/module.h>
17
18static bool touchscreen_get_prop_u32(struct device *dev,
19 const char *property,
20 unsigned int default_value,
21 unsigned int *value)
22{
23 u32 val;
24 int error;
25
26 error = device_property_read_u32(dev, property, &val);
27 if (error) {
28 *value = default_value;
29 return false;
30 }
31
32 *value = val;
33 return true;
34}
35
36static void touchscreen_set_params(struct input_dev *dev,
37 unsigned long axis,
38 int max, int fuzz)
39{
40 struct input_absinfo *absinfo;
41
42 if (!test_bit(axis, dev->absbit)) {
43 dev_warn(&dev->dev,
44 "DT specifies parameters but the axis %lu is not set up\n",
45 axis);
46 return;
47 }
48
49 absinfo = &dev->absinfo[axis];
50 absinfo->maximum = max;
51 absinfo->fuzz = fuzz;
52}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67void touchscreen_parse_properties(struct input_dev *input, bool multitouch,
68 struct touchscreen_properties *prop)
69{
70 struct device *dev = input->dev.parent;
71 unsigned int axis;
72 unsigned int maximum, fuzz;
73 bool data_present;
74
75 input_alloc_absinfo(input);
76 if (!input->absinfo)
77 return;
78
79 axis = multitouch ? ABS_MT_POSITION_X : ABS_X;
80 data_present = touchscreen_get_prop_u32(dev, "touchscreen-size-x",
81 input_abs_get_max(input,
82 axis) + 1,
83 &maximum) |
84 touchscreen_get_prop_u32(dev, "touchscreen-fuzz-x",
85 input_abs_get_fuzz(input, axis),
86 &fuzz);
87 if (data_present)
88 touchscreen_set_params(input, axis, maximum - 1, fuzz);
89
90 axis = multitouch ? ABS_MT_POSITION_Y : ABS_Y;
91 data_present = touchscreen_get_prop_u32(dev, "touchscreen-size-y",
92 input_abs_get_max(input,
93 axis) + 1,
94 &maximum) |
95 touchscreen_get_prop_u32(dev, "touchscreen-fuzz-y",
96 input_abs_get_fuzz(input, axis),
97 &fuzz);
98 if (data_present)
99 touchscreen_set_params(input, axis, maximum - 1, fuzz);
100
101 axis = multitouch ? ABS_MT_PRESSURE : ABS_PRESSURE;
102 data_present = touchscreen_get_prop_u32(dev,
103 "touchscreen-max-pressure",
104 input_abs_get_max(input, axis),
105 &maximum) |
106 touchscreen_get_prop_u32(dev,
107 "touchscreen-fuzz-pressure",
108 input_abs_get_fuzz(input, axis),
109 &fuzz);
110 if (data_present)
111 touchscreen_set_params(input, axis, maximum, fuzz);
112
113 if (!prop)
114 return;
115
116 axis = multitouch ? ABS_MT_POSITION_X : ABS_X;
117
118 prop->max_x = input_abs_get_max(input, axis);
119 prop->max_y = input_abs_get_max(input, axis + 1);
120 prop->invert_x =
121 device_property_read_bool(dev, "touchscreen-inverted-x");
122 prop->invert_y =
123 device_property_read_bool(dev, "touchscreen-inverted-y");
124 prop->swap_x_y =
125 device_property_read_bool(dev, "touchscreen-swapped-x-y");
126
127 if (prop->swap_x_y)
128 swap(input->absinfo[axis], input->absinfo[axis + 1]);
129}
130EXPORT_SYMBOL(touchscreen_parse_properties);
131
132static void
133touchscreen_apply_prop_to_x_y(const struct touchscreen_properties *prop,
134 unsigned int *x, unsigned int *y)
135{
136 if (prop->invert_x)
137 *x = prop->max_x - *x;
138
139 if (prop->invert_y)
140 *y = prop->max_y - *y;
141
142 if (prop->swap_x_y)
143 swap(*x, *y);
144}
145
146
147
148
149
150
151
152
153
154
155
156
157void touchscreen_set_mt_pos(struct input_mt_pos *pos,
158 const struct touchscreen_properties *prop,
159 unsigned int x, unsigned int y)
160{
161 touchscreen_apply_prop_to_x_y(prop, &x, &y);
162 pos->x = x;
163 pos->y = y;
164}
165EXPORT_SYMBOL(touchscreen_set_mt_pos);
166
167
168
169
170
171
172
173
174
175
176
177
178
179void touchscreen_report_pos(struct input_dev *input,
180 const struct touchscreen_properties *prop,
181 unsigned int x, unsigned int y,
182 bool multitouch)
183{
184 touchscreen_apply_prop_to_x_y(prop, &x, &y);
185 input_report_abs(input, multitouch ? ABS_MT_POSITION_X : ABS_X, x);
186 input_report_abs(input, multitouch ? ABS_MT_POSITION_Y : ABS_Y, y);
187}
188EXPORT_SYMBOL(touchscreen_report_pos);
189
190MODULE_LICENSE("GPL v2");
191MODULE_DESCRIPTION("Device-tree helpers functions for touchscreen devices");
192