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