Input: dlink-dir685-touchkeys - fix a typo in driver name
[linux/fpc-iii.git] / drivers / input / touchscreen / of_touchscreen.c
blob9642f103b7268cb79da418d9b65de8d8bfbb8542
1 /*
2 * Generic DT helper functions for touchscreen devices
4 * Copyright (c) 2014 Sebastian Reichel <sre@kernel.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
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>
18 static bool touchscreen_get_prop_u32(struct device *dev,
19 const char *property,
20 unsigned int default_value,
21 unsigned int *value)
23 u32 val;
24 int error;
26 error = device_property_read_u32(dev, property, &val);
27 if (error) {
28 *value = default_value;
29 return false;
32 *value = val;
33 return true;
36 static void touchscreen_set_params(struct input_dev *dev,
37 unsigned long axis,
38 int max, int fuzz)
40 struct input_absinfo *absinfo;
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;
49 absinfo = &dev->absinfo[axis];
50 absinfo->maximum = max;
51 absinfo->fuzz = fuzz;
54 /**
55 * touchscreen_parse_properties - parse common touchscreen DT properties
56 * @input: input device that should be parsed
57 * @multitouch: specifies whether parsed properties should be applied to
58 * single-touch or multi-touch axes
59 * @prop: pointer to a struct touchscreen_properties into which to store
60 * axis swap and invert info for use with touchscreen_report_x_y();
61 * or %NULL
63 * This function parses common DT properties for touchscreens and setups the
64 * input device accordingly. The function keeps previously set up default
65 * values if no value is specified via DT.
67 void touchscreen_parse_properties(struct input_dev *input, bool multitouch,
68 struct touchscreen_properties *prop)
70 struct device *dev = input->dev.parent;
71 unsigned int axis;
72 unsigned int maximum, fuzz;
73 bool data_present;
75 input_alloc_absinfo(input);
76 if (!input->absinfo)
77 return;
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);
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);
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);
113 if (!prop)
114 return;
116 axis = multitouch ? ABS_MT_POSITION_X : ABS_X;
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");
127 if (prop->swap_x_y)
128 swap(input->absinfo[axis], input->absinfo[axis + 1]);
130 EXPORT_SYMBOL(touchscreen_parse_properties);
132 static void
133 touchscreen_apply_prop_to_x_y(const struct touchscreen_properties *prop,
134 unsigned int *x, unsigned int *y)
136 if (prop->invert_x)
137 *x = prop->max_x - *x;
139 if (prop->invert_y)
140 *y = prop->max_y - *y;
142 if (prop->swap_x_y)
143 swap(*x, *y);
147 * touchscreen_set_mt_pos - Set input_mt_pos coordinates
148 * @pos: input_mt_pos to set coordinates of
149 * @prop: pointer to a struct touchscreen_properties
150 * @x: X coordinate to store in pos
151 * @y: Y coordinate to store in pos
153 * Adjust the passed in x and y values applying any axis inversion and
154 * swapping requested in the passed in touchscreen_properties and store
155 * the result in a struct input_mt_pos.
157 void touchscreen_set_mt_pos(struct input_mt_pos *pos,
158 const struct touchscreen_properties *prop,
159 unsigned int x, unsigned int y)
161 touchscreen_apply_prop_to_x_y(prop, &x, &y);
162 pos->x = x;
163 pos->y = y;
165 EXPORT_SYMBOL(touchscreen_set_mt_pos);
168 * touchscreen_report_pos - Report touchscreen coordinates
169 * @input: input_device to report coordinates for
170 * @prop: pointer to a struct touchscreen_properties
171 * @x: X coordinate to report
172 * @y: Y coordinate to report
173 * @multitouch: Report coordinates on single-touch or multi-touch axes
175 * Adjust the passed in x and y values applying any axis inversion and
176 * swapping requested in the passed in touchscreen_properties and then
177 * report the resulting coordinates on the input_dev's x and y axis.
179 void touchscreen_report_pos(struct input_dev *input,
180 const struct touchscreen_properties *prop,
181 unsigned int x, unsigned int y,
182 bool multitouch)
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);
188 EXPORT_SYMBOL(touchscreen_report_pos);
190 MODULE_LICENSE("GPL v2");
191 MODULE_DESCRIPTION("Device-tree helpers functions for touchscreen devices");