1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2016 Synaptics Incorporated
5 #include <linux/input.h>
6 #include <linux/input/mt.h>
8 #include "rmi_driver.h"
9 #include "rmi_2d_sensor.h"
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,
26 #define F12_DATA1_BYTES_PER_OBJ 8
29 struct rmi_2d_sensor sensor
;
30 struct rmi_2d_sensor_platform_data sensor_pdata
;
35 struct rmi_register_descriptor query_reg_desc
;
36 struct rmi_register_descriptor control_reg_desc
;
37 struct rmi_register_descriptor data_reg_desc
;
39 /* F12 Data1 describes sensed objects */
40 const struct rmi_register_desc_item
*data1
;
43 /* F12 Data5 describes finger ACM */
44 const struct rmi_register_desc_item
*data5
;
47 /* F12 Data5 describes Pen */
48 const struct rmi_register_desc_item
*data6
;
52 /* F12 Data9 reports relative data */
53 const struct rmi_register_desc_item
*data9
;
56 const struct rmi_register_desc_item
*data15
;
59 unsigned long *abs_mask
;
60 unsigned long *rel_mask
;
63 static int rmi_f12_read_sensor_tuning(struct f12_data
*f12
)
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
;
77 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 8);
80 "F12 does not have the sensor tuning control register\n");
84 offset
= rmi_register_desc_calc_reg_offset(&f12
->control_reg_desc
, 8);
86 if (item
->reg_size
> sizeof(buf
)) {
88 "F12 control8 should be no bigger than %zd bytes, not: %ld\n",
89 sizeof(buf
), item
->reg_size
);
93 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
+ offset
, buf
,
99 if (rmi_register_desc_has_subpacket(item
, 0)) {
100 sensor
->max_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
101 sensor
->max_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
105 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: max_x: %d max_y: %d\n", __func__
,
106 sensor
->max_x
, sensor
->max_y
);
108 if (rmi_register_desc_has_subpacket(item
, 1)) {
109 pitch_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
110 pitch_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
114 if (rmi_register_desc_has_subpacket(item
, 2)) {
115 /* Units 1/128 sensor pitch */
116 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
117 "%s: Inactive Border xlo:%d xhi:%d ylo:%d yhi:%d\n",
119 buf
[offset
], buf
[offset
+ 1],
120 buf
[offset
+ 2], buf
[offset
+ 3]);
125 if (rmi_register_desc_has_subpacket(item
, 3)) {
126 rx_receivers
= buf
[offset
];
127 tx_receivers
= buf
[offset
+ 1];
131 /* Skip over sensor flags */
132 if (rmi_register_desc_has_subpacket(item
, 4))
135 sensor
->x_mm
= (pitch_x
* rx_receivers
) >> 12;
136 sensor
->y_mm
= (pitch_y
* tx_receivers
) >> 12;
138 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: x_mm: %d y_mm: %d\n", __func__
,
139 sensor
->x_mm
, sensor
->y_mm
);
144 static void rmi_f12_process_objects(struct f12_data
*f12
, u8
*data1
, int size
)
147 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
148 int objects
= f12
->data1
->num_subpackets
;
150 if ((f12
->data1
->num_subpackets
* F12_DATA1_BYTES_PER_OBJ
) > size
)
151 objects
= size
/ F12_DATA1_BYTES_PER_OBJ
;
153 for (i
= 0; i
< objects
; i
++) {
154 struct rmi_2d_sensor_abs_object
*obj
= &sensor
->objs
[i
];
156 obj
->type
= RMI_2D_OBJECT_NONE
;
157 obj
->mt_tool
= MT_TOOL_FINGER
;
160 case RMI_F12_OBJECT_FINGER
:
161 obj
->type
= RMI_2D_OBJECT_FINGER
;
163 case RMI_F12_OBJECT_STYLUS
:
164 obj
->type
= RMI_2D_OBJECT_STYLUS
;
165 obj
->mt_tool
= MT_TOOL_PEN
;
167 case RMI_F12_OBJECT_PALM
:
168 obj
->type
= RMI_2D_OBJECT_PALM
;
169 obj
->mt_tool
= MT_TOOL_PALM
;
171 case RMI_F12_OBJECT_UNCLASSIFIED
:
172 obj
->type
= RMI_2D_OBJECT_UNCLASSIFIED
;
176 obj
->x
= (data1
[2] << 8) | data1
[1];
177 obj
->y
= (data1
[4] << 8) | data1
[3];
182 rmi_2d_sensor_abs_process(sensor
, obj
, i
);
184 data1
+= F12_DATA1_BYTES_PER_OBJ
;
187 if (sensor
->kernel_tracking
)
188 input_mt_assign_slots(sensor
->input
,
189 sensor
->tracking_slots
,
190 sensor
->tracking_pos
,
194 for (i
= 0; i
< objects
; i
++)
195 rmi_2d_sensor_abs_report(sensor
, &sensor
->objs
[i
], i
);
198 static irqreturn_t
rmi_f12_attention(int irq
, void *ctx
)
201 struct rmi_function
*fn
= ctx
;
202 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
203 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
204 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
205 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
206 int valid_bytes
= sensor
->pkt_size
;
208 if (drvdata
->attn_data
.data
) {
209 if (sensor
->attn_size
> drvdata
->attn_data
.size
)
210 valid_bytes
= drvdata
->attn_data
.size
;
212 valid_bytes
= sensor
->attn_size
;
213 memcpy(sensor
->data_pkt
, drvdata
->attn_data
.data
,
215 drvdata
->attn_data
.data
+= valid_bytes
;
216 drvdata
->attn_data
.size
-= valid_bytes
;
218 retval
= rmi_read_block(rmi_dev
, f12
->data_addr
,
219 sensor
->data_pkt
, sensor
->pkt_size
);
221 dev_err(&fn
->dev
, "Failed to read object data. Code: %d.\n",
223 return IRQ_RETVAL(retval
);
228 rmi_f12_process_objects(f12
,
229 &sensor
->data_pkt
[f12
->data1_offset
], valid_bytes
);
231 input_mt_sync_frame(sensor
->input
);
236 static int rmi_f12_write_control_regs(struct rmi_function
*fn
)
239 const struct rmi_register_desc_item
*item
;
240 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
241 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
244 u16 control_offset
= 0;
245 u8 subpacket_offset
= 0;
248 && (f12
->sensor
.dribble
!= RMI_REG_STATE_DEFAULT
)) {
249 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 20);
251 control_offset
= rmi_register_desc_calc_reg_offset(
252 &f12
->control_reg_desc
, 20);
255 * The byte containing the EnableDribble bit will be
256 * in either byte 0 or byte 2 of control 20. Depending
257 * on the existence of subpacket 0. If control 20 is
258 * larger then 3 bytes, just read the first 3.
260 control_size
= min(item
->reg_size
, 3UL);
262 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
263 + control_offset
, buf
, control_size
);
267 if (rmi_register_desc_has_subpacket(item
, 0))
268 subpacket_offset
+= 1;
270 switch (f12
->sensor
.dribble
) {
271 case RMI_REG_STATE_OFF
:
272 buf
[subpacket_offset
] &= ~BIT(2);
274 case RMI_REG_STATE_ON
:
275 buf
[subpacket_offset
] |= BIT(2);
277 case RMI_REG_STATE_DEFAULT
:
282 ret
= rmi_write_block(rmi_dev
,
283 fn
->fd
.control_base_addr
+ control_offset
,
294 static int rmi_f12_config(struct rmi_function
*fn
)
296 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
297 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
298 struct rmi_2d_sensor
*sensor
;
301 sensor
= &f12
->sensor
;
303 if (!sensor
->report_abs
)
304 drv
->clear_irq_bits(fn
->rmi_dev
, f12
->abs_mask
);
306 drv
->set_irq_bits(fn
->rmi_dev
, f12
->abs_mask
);
308 drv
->clear_irq_bits(fn
->rmi_dev
, f12
->rel_mask
);
310 ret
= rmi_f12_write_control_regs(fn
);
313 "Failed to write F12 control registers: %d\n", ret
);
318 static int rmi_f12_probe(struct rmi_function
*fn
)
320 struct f12_data
*f12
;
322 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
324 u16 query_addr
= fn
->fd
.query_base_addr
;
325 const struct rmi_register_desc_item
*item
;
326 struct rmi_2d_sensor
*sensor
;
327 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
328 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
332 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s\n", __func__
);
334 mask_size
= BITS_TO_LONGS(drvdata
->irq_count
) * sizeof(unsigned long);
336 ret
= rmi_read(fn
->rmi_dev
, query_addr
, &buf
);
338 dev_err(&fn
->dev
, "Failed to read general info register: %d\n",
344 if (!(buf
& BIT(0))) {
346 "Behavior of F12 without register descriptors is undefined.\n");
350 f12
= devm_kzalloc(&fn
->dev
, sizeof(struct f12_data
) + mask_size
* 2,
355 f12
->abs_mask
= (unsigned long *)((char *)f12
356 + sizeof(struct f12_data
));
357 f12
->rel_mask
= (unsigned long *)((char *)f12
358 + sizeof(struct f12_data
) + mask_size
);
360 set_bit(fn
->irq_pos
, f12
->abs_mask
);
361 set_bit(fn
->irq_pos
+ 1, f12
->rel_mask
);
363 f12
->has_dribble
= !!(buf
& BIT(3));
365 if (fn
->dev
.of_node
) {
366 ret
= rmi_2d_sensor_of_probe(&fn
->dev
, &f12
->sensor_pdata
);
370 f12
->sensor_pdata
= pdata
->sensor_pdata
;
373 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
374 &f12
->query_reg_desc
);
377 "Failed to read the Query Register Descriptor: %d\n",
383 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
384 &f12
->control_reg_desc
);
387 "Failed to read the Control Register Descriptor: %d\n",
393 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
394 &f12
->data_reg_desc
);
397 "Failed to read the Data Register Descriptor: %d\n",
403 sensor
= &f12
->sensor
;
405 f12
->data_addr
= fn
->fd
.data_base_addr
;
406 sensor
->pkt_size
= rmi_register_desc_calc_size(&f12
->data_reg_desc
);
409 f12
->sensor_pdata
.axis_align
;
411 sensor
->x_mm
= f12
->sensor_pdata
.x_mm
;
412 sensor
->y_mm
= f12
->sensor_pdata
.y_mm
;
413 sensor
->dribble
= f12
->sensor_pdata
.dribble
;
415 if (sensor
->sensor_type
== rmi_sensor_default
)
416 sensor
->sensor_type
=
417 f12
->sensor_pdata
.sensor_type
;
419 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: data packet size: %d\n", __func__
,
421 sensor
->data_pkt
= devm_kzalloc(&fn
->dev
, sensor
->pkt_size
, GFP_KERNEL
);
422 if (!sensor
->data_pkt
)
425 dev_set_drvdata(&fn
->dev
, f12
);
427 ret
= rmi_f12_read_sensor_tuning(f12
);
432 * Figure out what data is contained in the data registers. HID devices
433 * may have registers defined, but their data is not reported in the
434 * HID attention report. Registers which are not reported in the HID
435 * attention report check to see if the device is receiving data from
436 * HID attention reports.
438 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 0);
439 if (item
&& !drvdata
->attn_data
.data
)
440 data_offset
+= item
->reg_size
;
442 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 1);
445 f12
->data1_offset
= data_offset
;
446 data_offset
+= item
->reg_size
;
447 sensor
->nbr_fingers
= item
->num_subpackets
;
448 sensor
->report_abs
= 1;
449 sensor
->attn_size
+= item
->reg_size
;
452 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 2);
453 if (item
&& !drvdata
->attn_data
.data
)
454 data_offset
+= item
->reg_size
;
456 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 3);
457 if (item
&& !drvdata
->attn_data
.data
)
458 data_offset
+= item
->reg_size
;
460 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 4);
461 if (item
&& !drvdata
->attn_data
.data
)
462 data_offset
+= item
->reg_size
;
464 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 5);
467 f12
->data5_offset
= data_offset
;
468 data_offset
+= item
->reg_size
;
469 sensor
->attn_size
+= item
->reg_size
;
472 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 6);
473 if (item
&& !drvdata
->attn_data
.data
) {
475 f12
->data6_offset
= data_offset
;
476 data_offset
+= item
->reg_size
;
479 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 7);
480 if (item
&& !drvdata
->attn_data
.data
)
481 data_offset
+= item
->reg_size
;
483 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 8);
484 if (item
&& !drvdata
->attn_data
.data
)
485 data_offset
+= item
->reg_size
;
487 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 9);
488 if (item
&& !drvdata
->attn_data
.data
) {
490 f12
->data9_offset
= data_offset
;
491 data_offset
+= item
->reg_size
;
492 if (!sensor
->report_abs
)
493 sensor
->report_rel
= 1;
496 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 10);
497 if (item
&& !drvdata
->attn_data
.data
)
498 data_offset
+= item
->reg_size
;
500 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 11);
501 if (item
&& !drvdata
->attn_data
.data
)
502 data_offset
+= item
->reg_size
;
504 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 12);
505 if (item
&& !drvdata
->attn_data
.data
)
506 data_offset
+= item
->reg_size
;
508 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 13);
509 if (item
&& !drvdata
->attn_data
.data
)
510 data_offset
+= item
->reg_size
;
512 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 14);
513 if (item
&& !drvdata
->attn_data
.data
)
514 data_offset
+= item
->reg_size
;
516 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 15);
517 if (item
&& !drvdata
->attn_data
.data
) {
519 f12
->data15_offset
= data_offset
;
520 data_offset
+= item
->reg_size
;
523 /* allocate the in-kernel tracking buffers */
524 sensor
->tracking_pos
= devm_kcalloc(&fn
->dev
,
525 sensor
->nbr_fingers
, sizeof(struct input_mt_pos
),
527 sensor
->tracking_slots
= devm_kcalloc(&fn
->dev
,
528 sensor
->nbr_fingers
, sizeof(int), GFP_KERNEL
);
529 sensor
->objs
= devm_kcalloc(&fn
->dev
,
531 sizeof(struct rmi_2d_sensor_abs_object
),
533 if (!sensor
->tracking_pos
|| !sensor
->tracking_slots
|| !sensor
->objs
)
536 ret
= rmi_2d_sensor_configure_input(fn
, sensor
);
543 struct rmi_function_handler rmi_f12_handler
= {
548 .probe
= rmi_f12_probe
,
549 .config
= rmi_f12_config
,
550 .attention
= rmi_f12_attention
,