Linux 4.19.133
[linux/fpc-iii.git] / drivers / input / rmi4 / rmi_f12.c
blob9066f2b70ff0e49c9be1ae9568c6120f099046b0
1 /*
2 * Copyright (c) 2012-2016 Synaptics Incorporated
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"
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,
29 #define F12_DATA1_BYTES_PER_OBJ 8
31 struct f12_data {
32 struct rmi_2d_sensor sensor;
33 struct rmi_2d_sensor_platform_data sensor_pdata;
34 bool has_dribble;
36 u16 data_addr;
38 struct rmi_register_descriptor query_reg_desc;
39 struct rmi_register_descriptor control_reg_desc;
40 struct rmi_register_descriptor data_reg_desc;
42 /* F12 Data1 describes sensed objects */
43 const struct rmi_register_desc_item *data1;
44 u16 data1_offset;
46 /* F12 Data5 describes finger ACM */
47 const struct rmi_register_desc_item *data5;
48 u16 data5_offset;
50 /* F12 Data5 describes Pen */
51 const struct rmi_register_desc_item *data6;
52 u16 data6_offset;
55 /* F12 Data9 reports relative data */
56 const struct rmi_register_desc_item *data9;
57 u16 data9_offset;
59 const struct rmi_register_desc_item *data15;
60 u16 data15_offset;
62 unsigned long *abs_mask;
63 unsigned long *rel_mask;
66 static int rmi_f12_read_sensor_tuning(struct f12_data *f12)
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;
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;
88 offset = rmi_register_desc_calc_reg_offset(&f12->control_reg_desc, 8);
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;
97 ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr + offset, buf,
98 item->reg_size);
99 if (ret)
100 return ret;
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;
109 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: max_x: %d max_y: %d\n", __func__,
110 sensor->max_x, sensor->max_y);
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;
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]);
126 offset += 4;
129 if (rmi_register_desc_has_subpacket(item, 3)) {
130 rx_receivers = buf[offset];
131 tx_receivers = buf[offset + 1];
132 offset += 2;
135 if (rmi_register_desc_has_subpacket(item, 4)) {
136 sensor_flags = buf[offset];
137 offset += 1;
140 sensor->x_mm = (pitch_x * rx_receivers) >> 12;
141 sensor->y_mm = (pitch_y * tx_receivers) >> 12;
143 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s: x_mm: %d y_mm: %d\n", __func__,
144 sensor->x_mm, sensor->y_mm);
146 return 0;
149 static void rmi_f12_process_objects(struct f12_data *f12, u8 *data1, int size)
151 int i;
152 struct rmi_2d_sensor *sensor = &f12->sensor;
153 int objects = f12->data1->num_subpackets;
155 if ((f12->data1->num_subpackets * F12_DATA1_BYTES_PER_OBJ) > size)
156 objects = size / F12_DATA1_BYTES_PER_OBJ;
158 for (i = 0; i < objects; i++) {
159 struct rmi_2d_sensor_abs_object *obj = &sensor->objs[i];
161 obj->type = RMI_2D_OBJECT_NONE;
162 obj->mt_tool = MT_TOOL_FINGER;
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;
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];
187 rmi_2d_sensor_abs_process(sensor, obj, i);
189 data1 += F12_DATA1_BYTES_PER_OBJ;
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);
199 for (i = 0; i < objects; i++)
200 rmi_2d_sensor_abs_report(sensor, &sensor->objs[i], i);
203 static irqreturn_t rmi_f12_attention(int irq, void *ctx)
205 int retval;
206 struct rmi_function *fn = ctx;
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;
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 IRQ_RETVAL(retval);
232 if (f12->data1)
233 rmi_f12_process_objects(f12,
234 &sensor->data_pkt[f12->data1_offset], valid_bytes);
236 input_mt_sync_frame(sensor->input);
238 return IRQ_HANDLED;
241 static int rmi_f12_write_control_regs(struct rmi_function *fn)
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;
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);
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.
265 control_size = min(item->reg_size, 3UL);
267 ret = rmi_read_block(rmi_dev, fn->fd.control_base_addr
268 + control_offset, buf, control_size);
269 if (ret)
270 return ret;
272 if (rmi_register_desc_has_subpacket(item, 0))
273 subpacket_offset += 1;
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;
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;
295 return 0;
299 static int rmi_f12_config(struct rmi_function *fn)
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;
306 sensor = &f12->sensor;
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);
313 drv->clear_irq_bits(fn->rmi_dev, f12->rel_mask);
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);
320 return 0;
323 static int rmi_f12_probe(struct rmi_function *fn)
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;
337 rmi_dbg(RMI_DEBUG_FN, &fn->dev, "%s\n", __func__);
339 mask_size = BITS_TO_LONGS(drvdata->irq_count) * sizeof(unsigned long);
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;
347 ++query_addr;
349 if (!(buf & BIT(0))) {
350 dev_err(&fn->dev,
351 "Behavior of F12 without register descriptors is undefined.\n");
352 return -ENODEV;
355 f12 = devm_kzalloc(&fn->dev, sizeof(struct f12_data) + mask_size * 2,
356 GFP_KERNEL);
357 if (!f12)
358 return -ENOMEM;
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);
365 set_bit(fn->irq_pos, f12->abs_mask);
366 set_bit(fn->irq_pos + 1, f12->rel_mask);
368 f12->has_dribble = !!(buf & BIT(3));
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;
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;
386 query_addr += 3;
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;
396 query_addr += 3;
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;
406 query_addr += 3;
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);
413 sensor->axis_align =
414 f12->sensor_pdata.axis_align;
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;
420 if (sensor->sensor_type == rmi_sensor_default)
421 sensor->sensor_type =
422 f12->sensor_pdata.sensor_type;
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;
430 dev_set_drvdata(&fn->dev, f12);
432 ret = rmi_f12_read_sensor_tuning(f12);
433 if (ret)
434 return ret;
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.
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
528 /* allocate the in-kernel tracking buffers */
529 sensor->tracking_pos = devm_kcalloc(&fn->dev,
530 sensor->nbr_fingers, sizeof(struct input_mt_pos),
531 GFP_KERNEL);
532 sensor->tracking_slots = devm_kcalloc(&fn->dev,
533 sensor->nbr_fingers, sizeof(int), GFP_KERNEL);
534 sensor->objs = devm_kcalloc(&fn->dev,
535 sensor->nbr_fingers,
536 sizeof(struct rmi_2d_sensor_abs_object),
537 GFP_KERNEL);
538 if (!sensor->tracking_pos || !sensor->tracking_slots || !sensor->objs)
539 return -ENOMEM;
541 ret = rmi_2d_sensor_configure_input(fn, sensor);
542 if (ret)
543 return ret;
545 return 0;
548 struct rmi_function_handler rmi_f12_handler = {
549 .driver = {
550 .name = "rmi4_f12",
552 .func = 0x12,
553 .probe = rmi_f12_probe,
554 .config = rmi_f12_config,
555 .attention = rmi_f12_attention,