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.
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
32 struct rmi_2d_sensor sensor
;
33 struct rmi_2d_sensor_platform_data sensor_pdata
;
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
;
46 /* F12 Data5 describes finger ACM */
47 const struct rmi_register_desc_item
*data5
;
50 /* F12 Data5 describes Pen */
51 const struct rmi_register_desc_item
*data6
;
55 /* F12 Data9 reports relative data */
56 const struct rmi_register_desc_item
*data9
;
59 const struct rmi_register_desc_item
*data15
;
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
;
78 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 8);
81 "F12 does not have the sensor tuning control register\n");
85 offset
= rmi_register_desc_calc_reg_offset(&f12
->control_reg_desc
, 8);
87 if (item
->reg_size
> sizeof(buf
)) {
89 "F12 control8 should be no bigger than %zd bytes, not: %ld\n",
90 sizeof(buf
), item
->reg_size
);
94 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
+ offset
, buf
,
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];
106 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: max_x: %d max_y: %d\n", __func__
,
107 sensor
->max_x
, sensor
->max_y
);
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];
115 if (rmi_register_desc_has_subpacket(item
, 2)) {
116 /* Units 1/128 sensor pitch */
117 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
118 "%s: Inactive Border xlo:%d xhi:%d ylo:%d yhi:%d\n",
120 buf
[offset
], buf
[offset
+ 1],
121 buf
[offset
+ 2], buf
[offset
+ 3]);
126 if (rmi_register_desc_has_subpacket(item
, 3)) {
127 rx_receivers
= buf
[offset
];
128 tx_receivers
= buf
[offset
+ 1];
132 if (rmi_register_desc_has_subpacket(item
, 4)) {
133 sensor_flags
= buf
[offset
];
137 sensor
->x_mm
= (pitch_x
* rx_receivers
) >> 12;
138 sensor
->y_mm
= (pitch_y
* tx_receivers
) >> 12;
140 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: x_mm: %d y_mm: %d\n", __func__
,
141 sensor
->x_mm
, sensor
->y_mm
);
146 static void rmi_f12_process_objects(struct f12_data
*f12
, u8
*data1
, int size
)
149 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
150 int objects
= f12
->data1
->num_subpackets
;
152 if ((f12
->data1
->num_subpackets
* F12_DATA1_BYTES_PER_OBJ
) > size
)
153 objects
= size
/ F12_DATA1_BYTES_PER_OBJ
;
155 for (i
= 0; i
< objects
; i
++) {
156 struct rmi_2d_sensor_abs_object
*obj
= &sensor
->objs
[i
];
158 obj
->type
= RMI_2D_OBJECT_NONE
;
159 obj
->mt_tool
= MT_TOOL_FINGER
;
162 case RMI_F12_OBJECT_FINGER
:
163 obj
->type
= RMI_2D_OBJECT_FINGER
;
165 case RMI_F12_OBJECT_STYLUS
:
166 obj
->type
= RMI_2D_OBJECT_STYLUS
;
167 obj
->mt_tool
= MT_TOOL_PEN
;
169 case RMI_F12_OBJECT_PALM
:
170 obj
->type
= RMI_2D_OBJECT_PALM
;
171 obj
->mt_tool
= MT_TOOL_PALM
;
173 case RMI_F12_OBJECT_UNCLASSIFIED
:
174 obj
->type
= RMI_2D_OBJECT_UNCLASSIFIED
;
178 obj
->x
= (data1
[2] << 8) | data1
[1];
179 obj
->y
= (data1
[4] << 8) | data1
[3];
184 rmi_2d_sensor_abs_process(sensor
, obj
, i
);
186 data1
+= F12_DATA1_BYTES_PER_OBJ
;
189 if (sensor
->kernel_tracking
)
190 input_mt_assign_slots(sensor
->input
,
191 sensor
->tracking_slots
,
192 sensor
->tracking_pos
,
196 for (i
= 0; i
< objects
; i
++)
197 rmi_2d_sensor_abs_report(sensor
, &sensor
->objs
[i
], i
);
200 static irqreturn_t
rmi_f12_attention(int irq
, void *ctx
)
203 struct rmi_function
*fn
= ctx
;
204 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
205 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
206 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
207 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
208 int valid_bytes
= sensor
->pkt_size
;
210 if (drvdata
->attn_data
.data
) {
211 if (sensor
->attn_size
> drvdata
->attn_data
.size
)
212 valid_bytes
= drvdata
->attn_data
.size
;
214 valid_bytes
= sensor
->attn_size
;
215 memcpy(sensor
->data_pkt
, drvdata
->attn_data
.data
,
217 drvdata
->attn_data
.data
+= sensor
->attn_size
;
218 drvdata
->attn_data
.size
-= sensor
->attn_size
;
220 retval
= rmi_read_block(rmi_dev
, f12
->data_addr
,
221 sensor
->data_pkt
, sensor
->pkt_size
);
223 dev_err(&fn
->dev
, "Failed to read object data. Code: %d.\n",
225 return IRQ_RETVAL(retval
);
230 rmi_f12_process_objects(f12
,
231 &sensor
->data_pkt
[f12
->data1_offset
], valid_bytes
);
233 input_mt_sync_frame(sensor
->input
);
238 static int rmi_f12_write_control_regs(struct rmi_function
*fn
)
241 const struct rmi_register_desc_item
*item
;
242 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
243 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
246 u16 control_offset
= 0;
247 u8 subpacket_offset
= 0;
250 && (f12
->sensor
.dribble
!= RMI_REG_STATE_DEFAULT
)) {
251 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 20);
253 control_offset
= rmi_register_desc_calc_reg_offset(
254 &f12
->control_reg_desc
, 20);
257 * The byte containing the EnableDribble bit will be
258 * in either byte 0 or byte 2 of control 20. Depending
259 * on the existence of subpacket 0. If control 20 is
260 * larger then 3 bytes, just read the first 3.
262 control_size
= min(item
->reg_size
, 3UL);
264 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
265 + control_offset
, buf
, control_size
);
269 if (rmi_register_desc_has_subpacket(item
, 0))
270 subpacket_offset
+= 1;
272 switch (f12
->sensor
.dribble
) {
273 case RMI_REG_STATE_OFF
:
274 buf
[subpacket_offset
] &= ~BIT(2);
276 case RMI_REG_STATE_ON
:
277 buf
[subpacket_offset
] |= BIT(2);
279 case RMI_REG_STATE_DEFAULT
:
284 ret
= rmi_write_block(rmi_dev
,
285 fn
->fd
.control_base_addr
+ control_offset
,
296 static int rmi_f12_config(struct rmi_function
*fn
)
298 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
301 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
303 ret
= rmi_f12_write_control_regs(fn
);
306 "Failed to write F12 control registers: %d\n", ret
);
311 static int rmi_f12_probe(struct rmi_function
*fn
)
313 struct f12_data
*f12
;
315 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
317 u16 query_addr
= fn
->fd
.query_base_addr
;
318 const struct rmi_register_desc_item
*item
;
319 struct rmi_2d_sensor
*sensor
;
320 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
321 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
324 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s\n", __func__
);
326 ret
= rmi_read(fn
->rmi_dev
, query_addr
, &buf
);
328 dev_err(&fn
->dev
, "Failed to read general info register: %d\n",
334 if (!(buf
& BIT(0))) {
336 "Behavior of F12 without register descriptors is undefined.\n");
340 f12
= devm_kzalloc(&fn
->dev
, sizeof(struct f12_data
), GFP_KERNEL
);
344 f12
->has_dribble
= !!(buf
& BIT(3));
346 if (fn
->dev
.of_node
) {
347 ret
= rmi_2d_sensor_of_probe(&fn
->dev
, &f12
->sensor_pdata
);
351 f12
->sensor_pdata
= pdata
->sensor_pdata
;
354 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
355 &f12
->query_reg_desc
);
358 "Failed to read the Query Register Descriptor: %d\n",
364 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
365 &f12
->control_reg_desc
);
368 "Failed to read the Control Register Descriptor: %d\n",
374 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
375 &f12
->data_reg_desc
);
378 "Failed to read the Data Register Descriptor: %d\n",
384 sensor
= &f12
->sensor
;
386 f12
->data_addr
= fn
->fd
.data_base_addr
;
387 sensor
->pkt_size
= rmi_register_desc_calc_size(&f12
->data_reg_desc
);
390 f12
->sensor_pdata
.axis_align
;
392 sensor
->x_mm
= f12
->sensor_pdata
.x_mm
;
393 sensor
->y_mm
= f12
->sensor_pdata
.y_mm
;
394 sensor
->dribble
= f12
->sensor_pdata
.dribble
;
396 if (sensor
->sensor_type
== rmi_sensor_default
)
397 sensor
->sensor_type
=
398 f12
->sensor_pdata
.sensor_type
;
400 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: data packet size: %d\n", __func__
,
402 sensor
->data_pkt
= devm_kzalloc(&fn
->dev
, sensor
->pkt_size
, GFP_KERNEL
);
403 if (!sensor
->data_pkt
)
406 dev_set_drvdata(&fn
->dev
, f12
);
408 ret
= rmi_f12_read_sensor_tuning(f12
);
413 * Figure out what data is contained in the data registers. HID devices
414 * may have registers defined, but their data is not reported in the
415 * HID attention report. Registers which are not reported in the HID
416 * attention report check to see if the device is receiving data from
417 * HID attention reports.
419 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 0);
420 if (item
&& !drvdata
->attn_data
.data
)
421 data_offset
+= item
->reg_size
;
423 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 1);
426 f12
->data1_offset
= data_offset
;
427 data_offset
+= item
->reg_size
;
428 sensor
->nbr_fingers
= item
->num_subpackets
;
429 sensor
->report_abs
= 1;
430 sensor
->attn_size
+= item
->reg_size
;
433 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 2);
434 if (item
&& !drvdata
->attn_data
.data
)
435 data_offset
+= item
->reg_size
;
437 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 3);
438 if (item
&& !drvdata
->attn_data
.data
)
439 data_offset
+= item
->reg_size
;
441 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 4);
442 if (item
&& !drvdata
->attn_data
.data
)
443 data_offset
+= item
->reg_size
;
445 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 5);
448 f12
->data5_offset
= data_offset
;
449 data_offset
+= item
->reg_size
;
450 sensor
->attn_size
+= item
->reg_size
;
453 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 6);
454 if (item
&& !drvdata
->attn_data
.data
) {
456 f12
->data6_offset
= data_offset
;
457 data_offset
+= item
->reg_size
;
460 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 7);
461 if (item
&& !drvdata
->attn_data
.data
)
462 data_offset
+= item
->reg_size
;
464 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 8);
465 if (item
&& !drvdata
->attn_data
.data
)
466 data_offset
+= item
->reg_size
;
468 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 9);
469 if (item
&& !drvdata
->attn_data
.data
) {
471 f12
->data9_offset
= data_offset
;
472 data_offset
+= item
->reg_size
;
473 if (!sensor
->report_abs
)
474 sensor
->report_rel
= 1;
477 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 10);
478 if (item
&& !drvdata
->attn_data
.data
)
479 data_offset
+= item
->reg_size
;
481 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 11);
482 if (item
&& !drvdata
->attn_data
.data
)
483 data_offset
+= item
->reg_size
;
485 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 12);
486 if (item
&& !drvdata
->attn_data
.data
)
487 data_offset
+= item
->reg_size
;
489 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 13);
490 if (item
&& !drvdata
->attn_data
.data
)
491 data_offset
+= item
->reg_size
;
493 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 14);
494 if (item
&& !drvdata
->attn_data
.data
)
495 data_offset
+= item
->reg_size
;
497 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 15);
498 if (item
&& !drvdata
->attn_data
.data
) {
500 f12
->data15_offset
= data_offset
;
501 data_offset
+= item
->reg_size
;
504 /* allocate the in-kernel tracking buffers */
505 sensor
->tracking_pos
= devm_kcalloc(&fn
->dev
,
506 sensor
->nbr_fingers
, sizeof(struct input_mt_pos
),
508 sensor
->tracking_slots
= devm_kcalloc(&fn
->dev
,
509 sensor
->nbr_fingers
, sizeof(int), GFP_KERNEL
);
510 sensor
->objs
= devm_kcalloc(&fn
->dev
,
512 sizeof(struct rmi_2d_sensor_abs_object
),
514 if (!sensor
->tracking_pos
|| !sensor
->tracking_slots
|| !sensor
->objs
)
517 ret
= rmi_2d_sensor_configure_input(fn
, sensor
);
524 struct rmi_function_handler rmi_f12_handler
= {
529 .probe
= rmi_f12_probe
,
530 .config
= rmi_f12_config
,
531 .attention
= rmi_f12_attention
,