xref: /openbmc/linux/drivers/input/rmi4/rmi_f12.c (revision 4b4f3acc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2016 Synaptics Incorporated
4  */
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
7 #include <linux/rmi.h>
8 #include "rmi_driver.h"
9 #include "rmi_2d_sensor.h"
10 
11 enum rmi_f12_object_type {
12 	RMI_F12_OBJECT_NONE			= 0x00,
13 	RMI_F12_OBJECT_FINGER			= 0x01,
14 	RMI_F12_OBJECT_STYLUS			= 0x02,
15 	RMI_F12_OBJECT_PALM			= 0x03,
16 	RMI_F12_OBJECT_UNCLASSIFIED		= 0x04,
17 	RMI_F12_OBJECT_GLOVED_FINGER		= 0x06,
18 	RMI_F12_OBJECT_NARROW_OBJECT		= 0x07,
19 	RMI_F12_OBJECT_HAND_EDGE		= 0x08,
20 	RMI_F12_OBJECT_COVER			= 0x0A,
21 	RMI_F12_OBJECT_STYLUS_2			= 0x0B,
22 	RMI_F12_OBJECT_ERASER			= 0x0C,
23 	RMI_F12_OBJECT_SMALL_OBJECT		= 0x0D,
24 };
25 
26 #define F12_DATA1_BYTES_PER_OBJ			8
27 
28 struct f12_data {
29 	struct rmi_2d_sensor sensor;
30 	struct rmi_2d_sensor_platform_data sensor_pdata;
31 	bool has_dribble;
32 
33 	u16 data_addr;
34 
35 	struct rmi_register_descriptor query_reg_desc;
36 	struct rmi_register_descriptor control_reg_desc;
37 	struct rmi_register_descriptor data_reg_desc;
38 
39 	/* F12 Data1 describes sensed objects */
40 	const struct rmi_register_desc_item *data1;
41 	u16 data1_offset;
42 
43 	/* F12 Data5 describes finger ACM */
44 	const struct rmi_register_desc_item *data5;
45 	u16 data5_offset;
46 
47 	/* F12 Data5 describes Pen */
48 	const struct rmi_register_desc_item *data6;
49 	u16 data6_offset;
50 
51 
52 	/* F12 Data9 reports relative data */
53 	const struct rmi_register_desc_item *data9;
54 	u16 data9_offset;
55 
56 	const struct rmi_register_desc_item *data15;
57 	u16 data15_offset;
58 };
59 
60 static int rmi_f12_read_sensor_tuning(struct f12_data *f12)
61 {
62 	const struct rmi_register_desc_item *item;
63 	struct rmi_2d_sensor *sensor = &f12->sensor;
64 	struct rmi_function *fn = sensor->fn;
65 	struct rmi_device *rmi_dev = fn->rmi_dev;
66 	int ret;
67 	int offset;
68 	u8 buf[15];
69 	int pitch_x = 0;
70 	int pitch_y = 0;
71 	int rx_receivers = 0;
72 	int tx_receivers = 0;
73 	int sensor_flags = 0;
74 
75 	item = rmi_get_register_desc_item(&f12->control_reg_desc, 8);
76 	if (!item) {
77 		dev_err(&fn->dev,
78 			"F12 does not have the sensor tuning control register\n");
79 		return -ENODEV;
80 	}
81 
82 	offset = rmi_register_desc_calc_reg_offset(&f12->control_reg_desc, 8);
83 
84 	if (item->reg_size > sizeof(buf)) {
85 		dev_err(&fn->dev,
86 			"F12 control8 should be no bigger than %zd bytes, not: %ld\n",
87 			sizeof(buf), item->reg_size);
88 		return -ENODEV;
89 	}
90 
91 	ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr + offset, buf,
92 				item->reg_size);
93 	if (ret)
94 		return ret;
95 
96 	offset = 0;
97 	if (rmi_register_desc_has_subpacket(item, 0)) {
98 		sensor->max_x = (buf[offset + 1] << 8) | buf[offset];
99 		sensor->max_y = (buf[offset + 3] << 8) | buf[offset + 2];
100 		offset += 4;
101 	}
102 
103 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: max_x: %d max_y: %d\n", __func__,
104 		sensor->max_x, sensor->max_y);
105 
106 	if (rmi_register_desc_has_subpacket(item, 1)) {
107 		pitch_x = (buf[offset + 1] << 8) | buf[offset];
108 		pitch_y	= (buf[offset + 3] << 8) | buf[offset + 2];
109 		offset += 4;
110 	}
111 
112 	if (rmi_register_desc_has_subpacket(item, 2)) {
113 		/* Units 1/128 sensor pitch */
114 		rmi_dbg(RMI_DEBUG_FN, &fn->dev,
115 			"%s: Inactive Border xlo:%d xhi:%d ylo:%d yhi:%d\n",
116 			__func__,
117 			buf[offset], buf[offset + 1],
118 			buf[offset + 2], buf[offset + 3]);
119 
120 		offset += 4;
121 	}
122 
123 	if (rmi_register_desc_has_subpacket(item, 3)) {
124 		rx_receivers = buf[offset];
125 		tx_receivers = buf[offset + 1];
126 		offset += 2;
127 	}
128 
129 	if (rmi_register_desc_has_subpacket(item, 4)) {
130 		sensor_flags = buf[offset];
131 		offset += 1;
132 	}
133 
134 	sensor->x_mm = (pitch_x * rx_receivers) >> 12;
135 	sensor->y_mm = (pitch_y * tx_receivers) >> 12;
136 
137 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: x_mm: %d y_mm: %d\n", __func__,
138 		sensor->x_mm, sensor->y_mm);
139 
140 	return 0;
141 }
142 
143 static void rmi_f12_process_objects(struct f12_data *f12, u8 *data1, int size)
144 {
145 	int i;
146 	struct rmi_2d_sensor *sensor = &f12->sensor;
147 	int objects = f12->data1->num_subpackets;
148 
149 	if ((f12->data1->num_subpackets * F12_DATA1_BYTES_PER_OBJ) > size)
150 		objects = size / F12_DATA1_BYTES_PER_OBJ;
151 
152 	for (i = 0; i < objects; i++) {
153 		struct rmi_2d_sensor_abs_object *obj = &sensor->objs[i];
154 
155 		obj->type = RMI_2D_OBJECT_NONE;
156 		obj->mt_tool = MT_TOOL_FINGER;
157 
158 		switch (data1[0]) {
159 		case RMI_F12_OBJECT_FINGER:
160 			obj->type = RMI_2D_OBJECT_FINGER;
161 			break;
162 		case RMI_F12_OBJECT_STYLUS:
163 			obj->type = RMI_2D_OBJECT_STYLUS;
164 			obj->mt_tool = MT_TOOL_PEN;
165 			break;
166 		case RMI_F12_OBJECT_PALM:
167 			obj->type = RMI_2D_OBJECT_PALM;
168 			obj->mt_tool = MT_TOOL_PALM;
169 			break;
170 		case RMI_F12_OBJECT_UNCLASSIFIED:
171 			obj->type = RMI_2D_OBJECT_UNCLASSIFIED;
172 			break;
173 		}
174 
175 		obj->x = (data1[2] << 8) | data1[1];
176 		obj->y = (data1[4] << 8) | data1[3];
177 		obj->z = data1[5];
178 		obj->wx = data1[6];
179 		obj->wy = data1[7];
180 
181 		rmi_2d_sensor_abs_process(sensor, obj, i);
182 
183 		data1 += F12_DATA1_BYTES_PER_OBJ;
184 	}
185 
186 	if (sensor->kernel_tracking)
187 		input_mt_assign_slots(sensor->input,
188 				      sensor->tracking_slots,
189 				      sensor->tracking_pos,
190 				      sensor->nbr_fingers,
191 				      sensor->dmax);
192 
193 	for (i = 0; i < objects; i++)
194 		rmi_2d_sensor_abs_report(sensor, &sensor->objs[i], i);
195 }
196 
197 static irqreturn_t rmi_f12_attention(int irq, void *ctx)
198 {
199 	int retval;
200 	struct rmi_function *fn = ctx;
201 	struct rmi_device *rmi_dev = fn->rmi_dev;
202 	struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
203 	struct f12_data *f12 = dev_get_drvdata(&fn->dev);
204 	struct rmi_2d_sensor *sensor = &f12->sensor;
205 	int valid_bytes = sensor->pkt_size;
206 
207 	if (drvdata->attn_data.data) {
208 		if (sensor->attn_size > drvdata->attn_data.size)
209 			valid_bytes = drvdata->attn_data.size;
210 		else
211 			valid_bytes = sensor->attn_size;
212 		memcpy(sensor->data_pkt, drvdata->attn_data.data,
213 			valid_bytes);
214 		drvdata->attn_data.data += sensor->attn_size;
215 		drvdata->attn_data.size -= sensor->attn_size;
216 	} else {
217 		retval = rmi_read_block(rmi_dev, f12->data_addr,
218 					sensor->data_pkt, sensor->pkt_size);
219 		if (retval < 0) {
220 			dev_err(&fn->dev, "Failed to read object data. Code: %d.\n",
221 				retval);
222 			return IRQ_RETVAL(retval);
223 		}
224 	}
225 
226 	if (f12->data1)
227 		rmi_f12_process_objects(f12,
228 			&sensor->data_pkt[f12->data1_offset], valid_bytes);
229 
230 	input_mt_sync_frame(sensor->input);
231 
232 	return IRQ_HANDLED;
233 }
234 
235 static int rmi_f12_write_control_regs(struct rmi_function *fn)
236 {
237 	int ret;
238 	const struct rmi_register_desc_item *item;
239 	struct rmi_device *rmi_dev = fn->rmi_dev;
240 	struct f12_data *f12 = dev_get_drvdata(&fn->dev);
241 	int control_size;
242 	char buf[3];
243 	u16 control_offset = 0;
244 	u8 subpacket_offset = 0;
245 
246 	if (f12->has_dribble
247 	    && (f12->sensor.dribble != RMI_REG_STATE_DEFAULT)) {
248 		item = rmi_get_register_desc_item(&f12->control_reg_desc, 20);
249 		if (item) {
250 			control_offset = rmi_register_desc_calc_reg_offset(
251 						&f12->control_reg_desc, 20);
252 
253 			/*
254 			 * The byte containing the EnableDribble bit will be
255 			 * in either byte 0 or byte 2 of control 20. Depending
256 			 * on the existence of subpacket 0. If control 20 is
257 			 * larger then 3 bytes, just read the first 3.
258 			 */
259 			control_size = min(item->reg_size, 3UL);
260 
261 			ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr
262 					+ control_offset, buf, control_size);
263 			if (ret)
264 				return ret;
265 
266 			if (rmi_register_desc_has_subpacket(item, 0))
267 				subpacket_offset += 1;
268 
269 			switch (f12->sensor.dribble) {
270 			case RMI_REG_STATE_OFF:
271 				buf[subpacket_offset] &= ~BIT(2);
272 				break;
273 			case RMI_REG_STATE_ON:
274 				buf[subpacket_offset] |= BIT(2);
275 				break;
276 			case RMI_REG_STATE_DEFAULT:
277 			default:
278 				break;
279 			}
280 
281 			ret = rmi_write_block(rmi_dev,
282 				fn->fd.control_base_addr + control_offset,
283 				buf, control_size);
284 			if (ret)
285 				return ret;
286 		}
287 	}
288 
289 	return 0;
290 
291 }
292 
293 static int rmi_f12_config(struct rmi_function *fn)
294 {
295 	struct rmi_driver *drv = fn->rmi_dev->driver;
296 	int ret;
297 
298 	drv->set_irq_bits(fn->rmi_dev, fn->irq_mask);
299 
300 	ret = rmi_f12_write_control_regs(fn);
301 	if (ret)
302 		dev_warn(&fn->dev,
303 			"Failed to write F12 control registers: %d\n", ret);
304 
305 	return 0;
306 }
307 
308 static int rmi_f12_probe(struct rmi_function *fn)
309 {
310 	struct f12_data *f12;
311 	int ret;
312 	struct rmi_device *rmi_dev = fn->rmi_dev;
313 	char buf;
314 	u16 query_addr = fn->fd.query_base_addr;
315 	const struct rmi_register_desc_item *item;
316 	struct rmi_2d_sensor *sensor;
317 	struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
318 	struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
319 	u16 data_offset = 0;
320 
321 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s\n", __func__);
322 
323 	ret = rmi_read(fn->rmi_dev, query_addr, &buf);
324 	if (ret < 0) {
325 		dev_err(&fn->dev, "Failed to read general info register: %d\n",
326 			ret);
327 		return -ENODEV;
328 	}
329 	++query_addr;
330 
331 	if (!(buf & BIT(0))) {
332 		dev_err(&fn->dev,
333 			"Behavior of F12 without register descriptors is undefined.\n");
334 		return -ENODEV;
335 	}
336 
337 	f12 = devm_kzalloc(&fn->dev, sizeof(struct f12_data), GFP_KERNEL);
338 	if (!f12)
339 		return -ENOMEM;
340 
341 	f12->has_dribble = !!(buf & BIT(3));
342 
343 	if (fn->dev.of_node) {
344 		ret = rmi_2d_sensor_of_probe(&fn->dev, &f12->sensor_pdata);
345 		if (ret)
346 			return ret;
347 	} else {
348 		f12->sensor_pdata = pdata->sensor_pdata;
349 	}
350 
351 	ret = rmi_read_register_desc(rmi_dev, query_addr,
352 					&f12->query_reg_desc);
353 	if (ret) {
354 		dev_err(&fn->dev,
355 			"Failed to read the Query Register Descriptor: %d\n",
356 			ret);
357 		return ret;
358 	}
359 	query_addr += 3;
360 
361 	ret = rmi_read_register_desc(rmi_dev, query_addr,
362 						&f12->control_reg_desc);
363 	if (ret) {
364 		dev_err(&fn->dev,
365 			"Failed to read the Control Register Descriptor: %d\n",
366 			ret);
367 		return ret;
368 	}
369 	query_addr += 3;
370 
371 	ret = rmi_read_register_desc(rmi_dev, query_addr,
372 						&f12->data_reg_desc);
373 	if (ret) {
374 		dev_err(&fn->dev,
375 			"Failed to read the Data Register Descriptor: %d\n",
376 			ret);
377 		return ret;
378 	}
379 	query_addr += 3;
380 
381 	sensor = &f12->sensor;
382 	sensor->fn = fn;
383 	f12->data_addr = fn->fd.data_base_addr;
384 	sensor->pkt_size = rmi_register_desc_calc_size(&f12->data_reg_desc);
385 
386 	sensor->axis_align =
387 		f12->sensor_pdata.axis_align;
388 
389 	sensor->x_mm = f12->sensor_pdata.x_mm;
390 	sensor->y_mm = f12->sensor_pdata.y_mm;
391 	sensor->dribble = f12->sensor_pdata.dribble;
392 
393 	if (sensor->sensor_type == rmi_sensor_default)
394 		sensor->sensor_type =
395 			f12->sensor_pdata.sensor_type;
396 
397 	rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: data packet size: %d\n", __func__,
398 		sensor->pkt_size);
399 	sensor->data_pkt = devm_kzalloc(&fn->dev, sensor->pkt_size, GFP_KERNEL);
400 	if (!sensor->data_pkt)
401 		return -ENOMEM;
402 
403 	dev_set_drvdata(&fn->dev, f12);
404 
405 	ret = rmi_f12_read_sensor_tuning(f12);
406 	if (ret)
407 		return ret;
408 
409 	/*
410 	 * Figure out what data is contained in the data registers. HID devices
411 	 * may have registers defined, but their data is not reported in the
412 	 * HID attention report. Registers which are not reported in the HID
413 	 * attention report check to see if the device is receiving data from
414 	 * HID attention reports.
415 	 */
416 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 0);
417 	if (item && !drvdata->attn_data.data)
418 		data_offset += item->reg_size;
419 
420 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 1);
421 	if (item) {
422 		f12->data1 = item;
423 		f12->data1_offset = data_offset;
424 		data_offset += item->reg_size;
425 		sensor->nbr_fingers = item->num_subpackets;
426 		sensor->report_abs = 1;
427 		sensor->attn_size += item->reg_size;
428 	}
429 
430 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 2);
431 	if (item && !drvdata->attn_data.data)
432 		data_offset += item->reg_size;
433 
434 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 3);
435 	if (item && !drvdata->attn_data.data)
436 		data_offset += item->reg_size;
437 
438 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 4);
439 	if (item && !drvdata->attn_data.data)
440 		data_offset += item->reg_size;
441 
442 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 5);
443 	if (item) {
444 		f12->data5 = item;
445 		f12->data5_offset = data_offset;
446 		data_offset += item->reg_size;
447 		sensor->attn_size += item->reg_size;
448 	}
449 
450 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 6);
451 	if (item && !drvdata->attn_data.data) {
452 		f12->data6 = item;
453 		f12->data6_offset = data_offset;
454 		data_offset += item->reg_size;
455 	}
456 
457 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 7);
458 	if (item && !drvdata->attn_data.data)
459 		data_offset += item->reg_size;
460 
461 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 8);
462 	if (item && !drvdata->attn_data.data)
463 		data_offset += item->reg_size;
464 
465 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 9);
466 	if (item && !drvdata->attn_data.data) {
467 		f12->data9 = item;
468 		f12->data9_offset = data_offset;
469 		data_offset += item->reg_size;
470 		if (!sensor->report_abs)
471 			sensor->report_rel = 1;
472 	}
473 
474 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 10);
475 	if (item && !drvdata->attn_data.data)
476 		data_offset += item->reg_size;
477 
478 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 11);
479 	if (item && !drvdata->attn_data.data)
480 		data_offset += item->reg_size;
481 
482 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 12);
483 	if (item && !drvdata->attn_data.data)
484 		data_offset += item->reg_size;
485 
486 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 13);
487 	if (item && !drvdata->attn_data.data)
488 		data_offset += item->reg_size;
489 
490 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 14);
491 	if (item && !drvdata->attn_data.data)
492 		data_offset += item->reg_size;
493 
494 	item = rmi_get_register_desc_item(&f12->data_reg_desc, 15);
495 	if (item && !drvdata->attn_data.data) {
496 		f12->data15 = item;
497 		f12->data15_offset = data_offset;
498 		data_offset += item->reg_size;
499 	}
500 
501 	/* allocate the in-kernel tracking buffers */
502 	sensor->tracking_pos = devm_kcalloc(&fn->dev,
503 			sensor->nbr_fingers, sizeof(struct input_mt_pos),
504 			GFP_KERNEL);
505 	sensor->tracking_slots = devm_kcalloc(&fn->dev,
506 			sensor->nbr_fingers, sizeof(int), GFP_KERNEL);
507 	sensor->objs = devm_kcalloc(&fn->dev,
508 			sensor->nbr_fingers,
509 			sizeof(struct rmi_2d_sensor_abs_object),
510 			GFP_KERNEL);
511 	if (!sensor->tracking_pos || !sensor->tracking_slots || !sensor->objs)
512 		return -ENOMEM;
513 
514 	ret = rmi_2d_sensor_configure_input(fn, sensor);
515 	if (ret)
516 		return ret;
517 
518 	return 0;
519 }
520 
521 struct rmi_function_handler rmi_f12_handler = {
522 	.driver = {
523 		.name = "rmi4_f12",
524 	},
525 	.func = 0x12,
526 	.probe = rmi_f12_probe,
527 	.config = rmi_f12_config,
528 	.attention = rmi_f12_attention,
529 };
530