summaryrefslogtreecommitdiffstats
path: root/drivers/input/rmi4/rmi_2d_sensor.c
blob: b7fe6eb35a4ecd629f367bf49306a9fdcbd2c98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2011-2016 Synaptics Incorporated
 * Copyright (c) 2011 Unixphere
 */

#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/rmi.h>
#include "rmi_driver.h"
#include "rmi_2d_sensor.h"

#define RMI_2D_REL_POS_MIN		-128
#define RMI_2D_REL_POS_MAX		127

/* maximum ABS_MT_POSITION displacement (in mm) */
#define DMAX 10

void rmi_2d_sensor_abs_process(struct rmi_2d_sensor *sensor,
				struct rmi_2d_sensor_abs_object *obj,
				int slot)
{
	struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;

	/* we keep the previous values if the finger is released */
	if (obj->type == RMI_2D_OBJECT_NONE)
		return;

	if (axis_align->flip_x)
		obj->x = sensor->max_x - obj->x;

	if (axis_align->flip_y)
		obj->y = sensor->max_y - obj->y;

	if (axis_align->swap_axes)
		swap(obj->x, obj->y);

	/*
	 * Here checking if X offset or y offset are specified is
	 * redundant. We just add the offsets or clip the values.
	 *
	 * Note: offsets need to be applied before clipping occurs,
	 * or we could get funny values that are outside of
	 * clipping boundaries.
	 */
	obj->x += axis_align->offset_x;
	obj->y += axis_align->offset_y;

	obj->x =  max(axis_align->clip_x_low, obj->x);
	obj->y =  max(axis_align->clip_y_low, obj->y);

	if (axis_align->clip_x_high)
		obj->x = min(sensor->max_x, obj->x);

	if (axis_align->clip_y_high)
		obj->y =  min(sensor->max_y, obj->y);

	sensor->tracking_pos[slot].x = obj->x;
	sensor->tracking_pos[slot].y = obj->y;
}
EXPORT_SYMBOL_GPL(rmi_2d_sensor_abs_process);

void rmi_2d_sensor_abs_report(struct rmi_2d_sensor *sensor,
				struct rmi_2d_sensor_abs_object *obj,
				int slot)
{
	struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;
	struct input_dev *input = sensor->input;
	int wide, major, minor;

	if (sensor->kernel_tracking)
		input_mt_slot(input, sensor->tracking_slots[slot]);
	else
		input_mt_slot(input, slot);

	input_mt_report_slot_state(input, obj->mt_tool,
				   obj->type != RMI_2D_OBJECT_NONE);

	if (obj->type != RMI_2D_OBJECT_NONE) {
		obj->x = sensor->tracking_pos[slot].x;
		obj->y = sensor->tracking_pos[slot].y;

		if (axis_align->swap_axes)
			swap(obj->wx, obj->wy);

		wide = (obj->wx > obj->wy);
		major = max(obj->wx, obj->wy);
		minor = min(obj->wx, obj->wy);

		if (obj->type == RMI_2D_OBJECT_STYLUS) {
			major = max(1, major);
			minor = max(1, minor);
		}

		input_event(sensor->input, EV_ABS, ABS_MT_POSITION_X, obj->x);
		input_event(sensor->input, EV_ABS, ABS_MT_POSITION_Y, obj->y);
		input_event(sensor->input, EV_ABS, ABS_MT_ORIENTATION, wide);
		input_event(sensor->input, EV_ABS, ABS_MT_PRESSURE, obj->z);
		input_event(sensor->input, EV_ABS, ABS_MT_TOUCH_MAJOR, major);
		input_event(sensor->input, EV_ABS, ABS_MT_TOUCH_MINOR, minor);

		rmi_dbg(RMI_DEBUG_2D_SENSOR, &sensor->input->dev,
			"%s: obj[%d]: type: 0x%02x X: %d Y: %d Z: %d WX: %d WY: %d\n",
			__func__, slot, obj->type, obj->x, obj->y, obj->z,
			obj->wx, obj->wy);
	}
}
EXPORT_SYMBOL_GPL(rmi_2d_sensor_abs_report);

void rmi_2d_sensor_rel_report(struct rmi_2d_sensor *sensor, int x, int y)
{
	struct rmi_2d_axis_alignment *axis_align = &sensor->axis_align;

	x = min(RMI_2D_REL_POS_MAX, max(RMI_2D_REL_POS_MIN, (int)x));
	y = min(RMI_2D_REL_POS_MAX, max(RMI_2D_REL_POS_MIN, (int)y));

	if (axis_align->flip_x)
		x = min(RMI_2D_REL_POS_MAX, -x);

	if (axis_align->flip_y)
		y = min(RMI_2D_REL_POS_MAX, -y);

	if (axis_align->swap_axes)
		swap(x, y);

	if (x || y) {
		input_report_rel(sensor->input, REL_X, x);
		input_report_rel(sensor->input, REL_Y, y);
	}
}
EXPORT_SYMBOL_GPL(rmi_2d_sensor_rel_report);

static void rmi_2d_sensor_set_input_params(struct rmi_2d_sensor *sensor)
{
	struct input_dev *input = sensor->input;
	int res_x;
	int res_y;
	int max_x, max_y;
	int input_flags = 0;

	if (sensor->report_abs) {
		sensor->min_x = sensor->axis_align.clip_x_low;
		if (sensor->axis_align.clip_x_high)
			sensor->max_x = min(sensor->max_x,
				sensor->axis_align.clip_x_high);

		sensor->min_y = sensor->axis_align.clip_y_low;
		if (sensor->axis_align.clip_y_high)
			sensor->max_y = min(sensor->max_y,
				sensor->axis_align.clip_y_high);

		set_bit(EV_ABS, input->evbit);

		max_x = sensor->max_x;
		max_y = sensor->max_y;
		if (sensor->axis_align.swap_axes)
			swap(max_x, max_y);
		input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_x, 0, 0);
		input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_y, 0, 0);

		if (sensor->x_mm && sensor->y_mm) {
			res_x = (sensor->max_x - sensor->min_x) / sensor->x_mm;
			res_y = (sensor->max_y - sensor->min_y) / sensor->y_mm;
			if (sensor->axis_align.swap_axes)
				swap(res_x, res_y);

			input_abs_set_res(input, ABS_X, res_x);
			input_abs_set_res(input, ABS_Y, res_y);

			input_abs_set_res(input, ABS_MT_POSITION_X, res_x);
			input_abs_set_res(input, ABS_MT_POSITION_Y, res_y);

			if (!sensor->dmax)
				sensor->dmax = DMAX * res_x;
		}

		input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xff, 0, 0);
		input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 0x0f, 0, 0);
		input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 0x0f, 0, 0);
		input_set_abs_params(input, ABS_MT_ORIENTATION, 0, 1, 0, 0);
		input_set_abs_params(input, ABS_MT_TOOL_TYPE,
				     0, MT_TOOL_MAX, 0, 0);

		if (sensor->sensor_type == rmi_sensor_touchpad)
			input_flags = INPUT_MT_POINTER;
		else
			input_flags = INPUT_MT_DIRECT;

		if (sensor->kernel_tracking)
			input_flags |= INPUT_MT_TRACK;

		input_mt_init_slots(input, sensor->nbr_fingers, input_flags);
	}

	if (sensor->report_rel) {
		set_bit(EV_REL, input->evbit);
		set_bit(REL_X, input->relbit);
		set_bit(REL_Y, input->relbit);
	}

	if (sensor->topbuttonpad)
		set_bit(INPUT_PROP_TOPBUTTONPAD, input->propbit);
}

int rmi_2d_sensor_configure_input(struct rmi_function *fn,
					struct rmi_2d_sensor *sensor)
{
	struct rmi_device *rmi_dev = fn->rmi_dev;
	struct rmi_driver_data *drv_data = dev_get_drvdata(&rmi_dev->dev);

	if (!drv_data->input)
		return -ENODEV;

	sensor->input = drv_data->input;
	rmi_2d_sensor_set_input_params(sensor);

	return 0;
}
EXPORT_SYMBOL_GPL(rmi_2d_sensor_configure_input);

#ifdef CONFIG_OF
int rmi_2d_sensor_of_probe(struct device *dev,
			struct rmi_2d_sensor_platform_data *pdata)
{
	int retval;
	u32 val;

	pdata->axis_align.swap_axes = of_property_read_bool(dev->of_node,
						"touchscreen-swapped-x-y");

	pdata->axis_align.flip_x = of_property_read_bool(dev->of_node,
						"touchscreen-inverted-x");

	pdata->axis_align.flip_y = of_property_read_bool(dev->of_node,
						"touchscreen-inverted-y");

	retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-low", 1);
	if (retval)
		return retval;

	pdata->axis_align.clip_x_low = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-low",	1);
	if (retval)
		return retval;

	pdata->axis_align.clip_y_low = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,clip-x-high", 1);
	if (retval)
		return retval;

	pdata->axis_align.clip_x_high = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,clip-y-high", 1);
	if (retval)
		return retval;

	pdata->axis_align.clip_y_high = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,offset-x", 1);
	if (retval)
		return retval;

	pdata->axis_align.offset_x = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,offset-y", 1);
	if (retval)
		return retval;

	pdata->axis_align.offset_y = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,delta-x-threshold",
						1);
	if (retval)
		return retval;

	pdata->axis_align.delta_x_threshold = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,delta-y-threshold",
						1);
	if (retval)
		return retval;

	pdata->axis_align.delta_y_threshold = val;

	retval = rmi_of_property_read_u32(dev, (u32 *)&pdata->sensor_type,
			"syna,sensor-type", 1);
	if (retval)
		return retval;

	retval = rmi_of_property_read_u32(dev, &val, "touchscreen-x-mm", 1);
	if (retval)
		return retval;

	pdata->x_mm = val;

	retval = rmi_of_property_read_u32(dev, &val, "touchscreen-y-mm", 1);
	if (retval)
		return retval;

	pdata->y_mm = val;

	retval = rmi_of_property_read_u32(dev, &val,
				"syna,disable-report-mask", 1);
	if (retval)
		return retval;

	pdata->disable_report_mask = val;

	retval = rmi_of_property_read_u32(dev, &val, "syna,rezero-wait-ms",
						1);
	if (retval)
		return retval;

	pdata->rezero_wait = val;

	return 0;
}
#else
inline int rmi_2d_sensor_of_probe(struct device *dev,
			struct rmi_2d_sensor_platform_data *pdata)
{
	return -ENODEV;
}
#endif
EXPORT_SYMBOL_GPL(rmi_2d_sensor_of_probe);