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
27 #define RMI_F12_QUERY_RESOLUTION 29
30 struct rmi_2d_sensor sensor
;
31 struct rmi_2d_sensor_platform_data sensor_pdata
;
36 struct rmi_register_descriptor query_reg_desc
;
37 struct rmi_register_descriptor control_reg_desc
;
38 struct rmi_register_descriptor data_reg_desc
;
40 /* F12 Data1 describes sensed objects */
41 const struct rmi_register_desc_item
*data1
;
44 /* F12 Data5 describes finger ACM */
45 const struct rmi_register_desc_item
*data5
;
48 /* F12 Data5 describes Pen */
49 const struct rmi_register_desc_item
*data6
;
53 /* F12 Data9 reports relative data */
54 const struct rmi_register_desc_item
*data9
;
57 const struct rmi_register_desc_item
*data15
;
60 unsigned long *abs_mask
;
61 unsigned long *rel_mask
;
64 static int rmi_f12_read_sensor_tuning(struct f12_data
*f12
)
66 const struct rmi_register_desc_item
*item
;
67 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
68 struct rmi_function
*fn
= sensor
->fn
;
69 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
77 u16 query_dpm_addr
= 0;
78 int dpm_resolution
= 0;
80 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 8);
83 "F12 does not have the sensor tuning control register\n");
87 offset
= rmi_register_desc_calc_reg_offset(&f12
->control_reg_desc
, 8);
89 if (item
->reg_size
> sizeof(buf
)) {
91 "F12 control8 should be no bigger than %zd bytes, not: %ld\n",
92 sizeof(buf
), item
->reg_size
);
96 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
+ offset
, buf
,
102 if (rmi_register_desc_has_subpacket(item
, 0)) {
103 sensor
->max_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
104 sensor
->max_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
108 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: max_x: %d max_y: %d\n", __func__
,
109 sensor
->max_x
, sensor
->max_y
);
111 if (rmi_register_desc_has_subpacket(item
, 1)) {
112 pitch_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
113 pitch_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
117 if (rmi_register_desc_has_subpacket(item
, 2)) {
118 /* Units 1/128 sensor pitch */
119 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
120 "%s: Inactive Border xlo:%d xhi:%d ylo:%d yhi:%d\n",
122 buf
[offset
], buf
[offset
+ 1],
123 buf
[offset
+ 2], buf
[offset
+ 3]);
129 * Use the Query DPM feature when the resolution query register
132 if (rmi_get_register_desc_item(&f12
->query_reg_desc
,
133 RMI_F12_QUERY_RESOLUTION
)) {
134 offset
= rmi_register_desc_calc_reg_offset(&f12
->query_reg_desc
,
135 RMI_F12_QUERY_RESOLUTION
);
136 query_dpm_addr
= fn
->fd
.query_base_addr
+ offset
;
137 ret
= rmi_read(fn
->rmi_dev
, query_dpm_addr
, buf
);
139 dev_err(&fn
->dev
, "Failed to read DPM value: %d\n", ret
);
142 dpm_resolution
= buf
[0];
144 sensor
->x_mm
= sensor
->max_x
/ dpm_resolution
;
145 sensor
->y_mm
= sensor
->max_y
/ dpm_resolution
;
147 if (rmi_register_desc_has_subpacket(item
, 3)) {
148 rx_receivers
= buf
[offset
];
149 tx_receivers
= buf
[offset
+ 1];
153 /* Skip over sensor flags */
154 if (rmi_register_desc_has_subpacket(item
, 4))
157 sensor
->x_mm
= (pitch_x
* rx_receivers
) >> 12;
158 sensor
->y_mm
= (pitch_y
* tx_receivers
) >> 12;
161 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: x_mm: %d y_mm: %d\n", __func__
,
162 sensor
->x_mm
, sensor
->y_mm
);
167 static void rmi_f12_process_objects(struct f12_data
*f12
, u8
*data1
, int size
)
170 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
171 int objects
= f12
->data1
->num_subpackets
;
173 if ((f12
->data1
->num_subpackets
* F12_DATA1_BYTES_PER_OBJ
) > size
)
174 objects
= size
/ F12_DATA1_BYTES_PER_OBJ
;
176 for (i
= 0; i
< objects
; i
++) {
177 struct rmi_2d_sensor_abs_object
*obj
= &sensor
->objs
[i
];
179 obj
->type
= RMI_2D_OBJECT_NONE
;
180 obj
->mt_tool
= MT_TOOL_FINGER
;
183 case RMI_F12_OBJECT_FINGER
:
184 obj
->type
= RMI_2D_OBJECT_FINGER
;
186 case RMI_F12_OBJECT_STYLUS
:
187 obj
->type
= RMI_2D_OBJECT_STYLUS
;
188 obj
->mt_tool
= MT_TOOL_PEN
;
190 case RMI_F12_OBJECT_PALM
:
191 obj
->type
= RMI_2D_OBJECT_PALM
;
192 obj
->mt_tool
= MT_TOOL_PALM
;
194 case RMI_F12_OBJECT_UNCLASSIFIED
:
195 obj
->type
= RMI_2D_OBJECT_UNCLASSIFIED
;
199 obj
->x
= (data1
[2] << 8) | data1
[1];
200 obj
->y
= (data1
[4] << 8) | data1
[3];
205 rmi_2d_sensor_abs_process(sensor
, obj
, i
);
207 data1
+= F12_DATA1_BYTES_PER_OBJ
;
210 if (sensor
->kernel_tracking
)
211 input_mt_assign_slots(sensor
->input
,
212 sensor
->tracking_slots
,
213 sensor
->tracking_pos
,
217 for (i
= 0; i
< objects
; i
++)
218 rmi_2d_sensor_abs_report(sensor
, &sensor
->objs
[i
], i
);
221 static irqreturn_t
rmi_f12_attention(int irq
, void *ctx
)
224 struct rmi_function
*fn
= ctx
;
225 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
226 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
227 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
228 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
229 int valid_bytes
= sensor
->pkt_size
;
231 if (drvdata
->attn_data
.data
) {
232 if (sensor
->attn_size
> drvdata
->attn_data
.size
)
233 valid_bytes
= drvdata
->attn_data
.size
;
235 valid_bytes
= sensor
->attn_size
;
236 memcpy(sensor
->data_pkt
, drvdata
->attn_data
.data
,
238 drvdata
->attn_data
.data
+= valid_bytes
;
239 drvdata
->attn_data
.size
-= valid_bytes
;
241 retval
= rmi_read_block(rmi_dev
, f12
->data_addr
,
242 sensor
->data_pkt
, sensor
->pkt_size
);
244 dev_err(&fn
->dev
, "Failed to read object data. Code: %d.\n",
246 return IRQ_RETVAL(retval
);
251 rmi_f12_process_objects(f12
,
252 &sensor
->data_pkt
[f12
->data1_offset
], valid_bytes
);
254 input_mt_sync_frame(sensor
->input
);
259 static int rmi_f12_write_control_regs(struct rmi_function
*fn
)
262 const struct rmi_register_desc_item
*item
;
263 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
264 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
267 u16 control_offset
= 0;
268 u8 subpacket_offset
= 0;
271 && (f12
->sensor
.dribble
!= RMI_REG_STATE_DEFAULT
)) {
272 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 20);
274 control_offset
= rmi_register_desc_calc_reg_offset(
275 &f12
->control_reg_desc
, 20);
278 * The byte containing the EnableDribble bit will be
279 * in either byte 0 or byte 2 of control 20. Depending
280 * on the existence of subpacket 0. If control 20 is
281 * larger then 3 bytes, just read the first 3.
283 control_size
= min(item
->reg_size
, 3UL);
285 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
286 + control_offset
, buf
, control_size
);
290 if (rmi_register_desc_has_subpacket(item
, 0))
291 subpacket_offset
+= 1;
293 switch (f12
->sensor
.dribble
) {
294 case RMI_REG_STATE_OFF
:
295 buf
[subpacket_offset
] &= ~BIT(2);
297 case RMI_REG_STATE_ON
:
298 buf
[subpacket_offset
] |= BIT(2);
300 case RMI_REG_STATE_DEFAULT
:
305 ret
= rmi_write_block(rmi_dev
,
306 fn
->fd
.control_base_addr
+ control_offset
,
317 static int rmi_f12_config(struct rmi_function
*fn
)
319 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
320 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
321 struct rmi_2d_sensor
*sensor
;
324 sensor
= &f12
->sensor
;
326 if (!sensor
->report_abs
)
327 drv
->clear_irq_bits(fn
->rmi_dev
, f12
->abs_mask
);
329 drv
->set_irq_bits(fn
->rmi_dev
, f12
->abs_mask
);
331 drv
->clear_irq_bits(fn
->rmi_dev
, f12
->rel_mask
);
333 ret
= rmi_f12_write_control_regs(fn
);
336 "Failed to write F12 control registers: %d\n", ret
);
341 static int rmi_f12_probe(struct rmi_function
*fn
)
343 struct f12_data
*f12
;
345 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
347 u16 query_addr
= fn
->fd
.query_base_addr
;
348 const struct rmi_register_desc_item
*item
;
349 struct rmi_2d_sensor
*sensor
;
350 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
351 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
355 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s\n", __func__
);
357 mask_size
= BITS_TO_LONGS(drvdata
->irq_count
) * sizeof(unsigned long);
359 ret
= rmi_read(fn
->rmi_dev
, query_addr
, &buf
);
361 dev_err(&fn
->dev
, "Failed to read general info register: %d\n",
367 if (!(buf
& BIT(0))) {
369 "Behavior of F12 without register descriptors is undefined.\n");
373 f12
= devm_kzalloc(&fn
->dev
, sizeof(struct f12_data
) + mask_size
* 2,
378 f12
->abs_mask
= (unsigned long *)((char *)f12
379 + sizeof(struct f12_data
));
380 f12
->rel_mask
= (unsigned long *)((char *)f12
381 + sizeof(struct f12_data
) + mask_size
);
383 set_bit(fn
->irq_pos
, f12
->abs_mask
);
384 set_bit(fn
->irq_pos
+ 1, f12
->rel_mask
);
386 f12
->has_dribble
= !!(buf
& BIT(3));
388 if (fn
->dev
.of_node
) {
389 ret
= rmi_2d_sensor_of_probe(&fn
->dev
, &f12
->sensor_pdata
);
393 f12
->sensor_pdata
= pdata
->sensor_pdata
;
396 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
397 &f12
->query_reg_desc
);
400 "Failed to read the Query Register Descriptor: %d\n",
406 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
407 &f12
->control_reg_desc
);
410 "Failed to read the Control Register Descriptor: %d\n",
416 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
417 &f12
->data_reg_desc
);
420 "Failed to read the Data Register Descriptor: %d\n",
426 sensor
= &f12
->sensor
;
428 f12
->data_addr
= fn
->fd
.data_base_addr
;
429 sensor
->pkt_size
= rmi_register_desc_calc_size(&f12
->data_reg_desc
);
432 f12
->sensor_pdata
.axis_align
;
434 sensor
->x_mm
= f12
->sensor_pdata
.x_mm
;
435 sensor
->y_mm
= f12
->sensor_pdata
.y_mm
;
436 sensor
->dribble
= f12
->sensor_pdata
.dribble
;
438 if (sensor
->sensor_type
== rmi_sensor_default
)
439 sensor
->sensor_type
=
440 f12
->sensor_pdata
.sensor_type
;
442 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: data packet size: %d\n", __func__
,
444 sensor
->data_pkt
= devm_kzalloc(&fn
->dev
, sensor
->pkt_size
, GFP_KERNEL
);
445 if (!sensor
->data_pkt
)
448 dev_set_drvdata(&fn
->dev
, f12
);
450 ret
= rmi_f12_read_sensor_tuning(f12
);
455 * Figure out what data is contained in the data registers. HID devices
456 * may have registers defined, but their data is not reported in the
457 * HID attention report. Registers which are not reported in the HID
458 * attention report check to see if the device is receiving data from
459 * HID attention reports.
461 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 0);
462 if (item
&& !drvdata
->attn_data
.data
)
463 data_offset
+= item
->reg_size
;
465 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 1);
468 f12
->data1_offset
= data_offset
;
469 data_offset
+= item
->reg_size
;
470 sensor
->nbr_fingers
= item
->num_subpackets
;
471 sensor
->report_abs
= 1;
472 sensor
->attn_size
+= item
->reg_size
;
475 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 2);
476 if (item
&& !drvdata
->attn_data
.data
)
477 data_offset
+= item
->reg_size
;
479 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 3);
480 if (item
&& !drvdata
->attn_data
.data
)
481 data_offset
+= item
->reg_size
;
483 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 4);
484 if (item
&& !drvdata
->attn_data
.data
)
485 data_offset
+= item
->reg_size
;
487 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 5);
490 f12
->data5_offset
= data_offset
;
491 data_offset
+= item
->reg_size
;
492 sensor
->attn_size
+= item
->reg_size
;
495 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 6);
496 if (item
&& !drvdata
->attn_data
.data
) {
498 f12
->data6_offset
= data_offset
;
499 data_offset
+= item
->reg_size
;
502 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 7);
503 if (item
&& !drvdata
->attn_data
.data
)
504 data_offset
+= item
->reg_size
;
506 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 8);
507 if (item
&& !drvdata
->attn_data
.data
)
508 data_offset
+= item
->reg_size
;
510 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 9);
511 if (item
&& !drvdata
->attn_data
.data
) {
513 f12
->data9_offset
= data_offset
;
514 data_offset
+= item
->reg_size
;
515 if (!sensor
->report_abs
)
516 sensor
->report_rel
= 1;
519 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 10);
520 if (item
&& !drvdata
->attn_data
.data
)
521 data_offset
+= item
->reg_size
;
523 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 11);
524 if (item
&& !drvdata
->attn_data
.data
)
525 data_offset
+= item
->reg_size
;
527 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 12);
528 if (item
&& !drvdata
->attn_data
.data
)
529 data_offset
+= item
->reg_size
;
531 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 13);
532 if (item
&& !drvdata
->attn_data
.data
)
533 data_offset
+= item
->reg_size
;
535 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 14);
536 if (item
&& !drvdata
->attn_data
.data
)
537 data_offset
+= item
->reg_size
;
539 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 15);
540 if (item
&& !drvdata
->attn_data
.data
) {
542 f12
->data15_offset
= data_offset
;
543 data_offset
+= item
->reg_size
;
546 /* allocate the in-kernel tracking buffers */
547 sensor
->tracking_pos
= devm_kcalloc(&fn
->dev
,
548 sensor
->nbr_fingers
, sizeof(struct input_mt_pos
),
550 sensor
->tracking_slots
= devm_kcalloc(&fn
->dev
,
551 sensor
->nbr_fingers
, sizeof(int), GFP_KERNEL
);
552 sensor
->objs
= devm_kcalloc(&fn
->dev
,
554 sizeof(struct rmi_2d_sensor_abs_object
),
556 if (!sensor
->tracking_pos
|| !sensor
->tracking_slots
|| !sensor
->objs
)
559 ret
= rmi_2d_sensor_configure_input(fn
, sensor
);
566 struct rmi_function_handler rmi_f12_handler
= {
571 .probe
= rmi_f12_probe
,
572 .config
= rmi_f12_config
,
573 .attention
= rmi_f12_attention
,