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