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