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,
30 struct rmi_function
*fn
;
31 struct rmi_2d_sensor sensor
;
32 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
;
61 static int rmi_f12_read_sensor_tuning(struct f12_data
*f12
)
63 const struct rmi_register_desc_item
*item
;
64 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
65 struct rmi_function
*fn
= sensor
->fn
;
66 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
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 sensor
->axis_align
.clip_x_low
= buf
[offset
];
119 sensor
->axis_align
.clip_x_high
= sensor
->max_x
121 sensor
->axis_align
.clip_y_low
= buf
[offset
+ 2];
122 sensor
->axis_align
.clip_y_high
= sensor
->max_y
127 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: x low: %d x high: %d y low: %d y high: %d\n",
128 __func__
, clip_x_low
, clip_x_high
, clip_y_low
, clip_y_high
);
130 if (rmi_register_desc_has_subpacket(item
, 3)) {
131 rx_receivers
= buf
[offset
];
132 tx_receivers
= buf
[offset
+ 1];
136 if (rmi_register_desc_has_subpacket(item
, 4)) {
137 sensor_flags
= buf
[offset
];
141 sensor
->x_mm
= (pitch_x
* rx_receivers
) >> 12;
142 sensor
->y_mm
= (pitch_y
* tx_receivers
) >> 12;
144 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: x_mm: %d y_mm: %d\n", __func__
,
145 sensor
->x_mm
, sensor
->y_mm
);
150 static void rmi_f12_process_objects(struct f12_data
*f12
, u8
*data1
)
153 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
155 for (i
= 0; i
< f12
->data1
->num_subpackets
; 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
);
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
< sensor
->nbr_fingers
; i
++)
197 rmi_2d_sensor_abs_report(sensor
, &sensor
->objs
[i
], i
);
200 static int rmi_f12_attention(struct rmi_function
*fn
,
201 unsigned long *irq_nr_regs
)
204 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
205 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
206 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
208 if (rmi_dev
->xport
->attn_data
) {
209 memcpy(sensor
->data_pkt
, rmi_dev
->xport
->attn_data
,
211 rmi_dev
->xport
->attn_data
+= sensor
->attn_size
;
212 rmi_dev
->xport
->attn_size
-= sensor
->attn_size
;
214 retval
= rmi_read_block(rmi_dev
, f12
->data_addr
,
215 sensor
->data_pkt
, sensor
->pkt_size
);
217 dev_err(&fn
->dev
, "Failed to read object data. Code: %d.\n",
224 rmi_f12_process_objects(f12
,
225 &sensor
->data_pkt
[f12
->data1_offset
]);
227 input_mt_sync_frame(sensor
->input
);
232 static int rmi_f12_config(struct rmi_function
*fn
)
234 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
236 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
241 static int rmi_f12_probe(struct rmi_function
*fn
)
243 struct f12_data
*f12
;
245 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
247 u16 query_addr
= fn
->fd
.query_base_addr
;
248 const struct rmi_register_desc_item
*item
;
249 struct rmi_2d_sensor
*sensor
;
250 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
251 struct rmi_transport_dev
*xport
= rmi_dev
->xport
;
254 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s\n", __func__
);
256 ret
= rmi_read(fn
->rmi_dev
, query_addr
, &buf
);
258 dev_err(&fn
->dev
, "Failed to read general info register: %d\n",
266 "Behavior of F12 without register descriptors is undefined.\n");
270 f12
= devm_kzalloc(&fn
->dev
, sizeof(struct f12_data
), GFP_KERNEL
);
274 if (fn
->dev
.of_node
) {
275 ret
= rmi_2d_sensor_of_probe(&fn
->dev
, &f12
->sensor_pdata
);
278 } else if (pdata
->sensor_pdata
) {
279 f12
->sensor_pdata
= *pdata
->sensor_pdata
;
282 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
283 &f12
->query_reg_desc
);
286 "Failed to read the Query Register Descriptor: %d\n",
292 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
293 &f12
->control_reg_desc
);
296 "Failed to read the Control Register Descriptor: %d\n",
302 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
303 &f12
->data_reg_desc
);
306 "Failed to read the Data Register Descriptor: %d\n",
312 sensor
= &f12
->sensor
;
314 f12
->data_addr
= fn
->fd
.data_base_addr
;
315 sensor
->pkt_size
= rmi_register_desc_calc_size(&f12
->data_reg_desc
);
318 f12
->sensor_pdata
.axis_align
;
320 sensor
->x_mm
= f12
->sensor_pdata
.x_mm
;
321 sensor
->y_mm
= f12
->sensor_pdata
.y_mm
;
323 if (sensor
->sensor_type
== rmi_sensor_default
)
324 sensor
->sensor_type
=
325 f12
->sensor_pdata
.sensor_type
;
327 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: data packet size: %d\n", __func__
,
329 sensor
->data_pkt
= devm_kzalloc(&fn
->dev
, sensor
->pkt_size
, GFP_KERNEL
);
330 if (!sensor
->data_pkt
)
333 dev_set_drvdata(&fn
->dev
, f12
);
335 ret
= rmi_f12_read_sensor_tuning(f12
);
340 * Figure out what data is contained in the data registers. HID devices
341 * may have registers defined, but their data is not reported in the
342 * HID attention report. Registers which are not reported in the HID
343 * attention report check to see if the device is receiving data from
344 * HID attention reports.
346 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 0);
347 if (item
&& !xport
->attn_data
)
348 data_offset
+= item
->reg_size
;
350 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 1);
353 f12
->data1_offset
= data_offset
;
354 data_offset
+= item
->reg_size
;
355 sensor
->nbr_fingers
= item
->num_subpackets
;
356 sensor
->report_abs
= 1;
357 sensor
->attn_size
+= item
->reg_size
;
360 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 2);
361 if (item
&& !xport
->attn_data
)
362 data_offset
+= item
->reg_size
;
364 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 3);
365 if (item
&& !xport
->attn_data
)
366 data_offset
+= item
->reg_size
;
368 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 4);
369 if (item
&& !xport
->attn_data
)
370 data_offset
+= item
->reg_size
;
372 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 5);
375 f12
->data5_offset
= data_offset
;
376 data_offset
+= item
->reg_size
;
377 sensor
->attn_size
+= item
->reg_size
;
380 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 6);
381 if (item
&& !xport
->attn_data
) {
383 f12
->data6_offset
= data_offset
;
384 data_offset
+= item
->reg_size
;
387 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 7);
388 if (item
&& !xport
->attn_data
)
389 data_offset
+= item
->reg_size
;
391 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 8);
392 if (item
&& !xport
->attn_data
)
393 data_offset
+= item
->reg_size
;
395 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 9);
396 if (item
&& !xport
->attn_data
) {
398 f12
->data9_offset
= data_offset
;
399 data_offset
+= item
->reg_size
;
400 if (!sensor
->report_abs
)
401 sensor
->report_rel
= 1;
404 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 10);
405 if (item
&& !xport
->attn_data
)
406 data_offset
+= item
->reg_size
;
408 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 11);
409 if (item
&& !xport
->attn_data
)
410 data_offset
+= item
->reg_size
;
412 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 12);
413 if (item
&& !xport
->attn_data
)
414 data_offset
+= item
->reg_size
;
416 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 13);
417 if (item
&& !xport
->attn_data
)
418 data_offset
+= item
->reg_size
;
420 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 14);
421 if (item
&& !xport
->attn_data
)
422 data_offset
+= item
->reg_size
;
424 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 15);
425 if (item
&& !xport
->attn_data
) {
427 f12
->data15_offset
= data_offset
;
428 data_offset
+= item
->reg_size
;
431 /* allocate the in-kernel tracking buffers */
432 sensor
->tracking_pos
= devm_kzalloc(&fn
->dev
,
433 sizeof(struct input_mt_pos
) * sensor
->nbr_fingers
,
435 sensor
->tracking_slots
= devm_kzalloc(&fn
->dev
,
436 sizeof(int) * sensor
->nbr_fingers
, GFP_KERNEL
);
437 sensor
->objs
= devm_kzalloc(&fn
->dev
,
438 sizeof(struct rmi_2d_sensor_abs_object
)
439 * sensor
->nbr_fingers
, GFP_KERNEL
);
440 if (!sensor
->tracking_pos
|| !sensor
->tracking_slots
|| !sensor
->objs
)
443 ret
= rmi_2d_sensor_configure_input(fn
, sensor
);
450 struct rmi_function_handler rmi_f12_handler
= {
455 .probe
= rmi_f12_probe
,
456 .config
= rmi_f12_config
,
457 .attention
= rmi_f12_attention
,