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