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
> 14) {
90 dev_err(&fn
->dev
, "F12 control8 should be 14 bytes, not: %ld\n",
95 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
+ offset
, buf
,
101 if (rmi_register_desc_has_subpacket(item
, 0)) {
102 sensor
->max_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
103 sensor
->max_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
107 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: max_x: %d max_y: %d\n", __func__
,
108 sensor
->max_x
, sensor
->max_y
);
110 if (rmi_register_desc_has_subpacket(item
, 1)) {
111 pitch_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
112 pitch_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
116 if (rmi_register_desc_has_subpacket(item
, 2)) {
117 sensor
->axis_align
.clip_x_low
= buf
[offset
];
118 sensor
->axis_align
.clip_x_high
= sensor
->max_x
120 sensor
->axis_align
.clip_y_low
= buf
[offset
+ 2];
121 sensor
->axis_align
.clip_y_high
= sensor
->max_y
126 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: x low: %d x high: %d y low: %d y high: %d\n",
127 __func__
, clip_x_low
, clip_x_high
, clip_y_low
, clip_y_high
);
129 if (rmi_register_desc_has_subpacket(item
, 3)) {
130 rx_receivers
= buf
[offset
];
131 tx_receivers
= buf
[offset
+ 1];
135 if (rmi_register_desc_has_subpacket(item
, 4)) {
136 sensor_flags
= buf
[offset
];
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
);
149 static void rmi_f12_process_objects(struct f12_data
*f12
, u8
*data1
)
152 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
154 for (i
= 0; i
< f12
->data1
->num_subpackets
; i
++) {
155 struct rmi_2d_sensor_abs_object
*obj
= &sensor
->objs
[i
];
157 obj
->type
= RMI_2D_OBJECT_NONE
;
158 obj
->mt_tool
= MT_TOOL_FINGER
;
161 case RMI_F12_OBJECT_FINGER
:
162 obj
->type
= RMI_2D_OBJECT_FINGER
;
164 case RMI_F12_OBJECT_STYLUS
:
165 obj
->type
= RMI_2D_OBJECT_STYLUS
;
166 obj
->mt_tool
= MT_TOOL_PEN
;
168 case RMI_F12_OBJECT_PALM
:
169 obj
->type
= RMI_2D_OBJECT_PALM
;
170 obj
->mt_tool
= MT_TOOL_PALM
;
172 case RMI_F12_OBJECT_UNCLASSIFIED
:
173 obj
->type
= RMI_2D_OBJECT_UNCLASSIFIED
;
177 obj
->x
= (data1
[2] << 8) | data1
[1];
178 obj
->y
= (data1
[4] << 8) | data1
[3];
183 rmi_2d_sensor_abs_process(sensor
, obj
, i
);
188 if (sensor
->kernel_tracking
)
189 input_mt_assign_slots(sensor
->input
,
190 sensor
->tracking_slots
,
191 sensor
->tracking_pos
,
195 for (i
= 0; i
< sensor
->nbr_fingers
; i
++)
196 rmi_2d_sensor_abs_report(sensor
, &sensor
->objs
[i
], i
);
199 static int rmi_f12_attention(struct rmi_function
*fn
,
200 unsigned long *irq_nr_regs
)
203 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
204 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
205 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
207 if (rmi_dev
->xport
->attn_data
) {
208 memcpy(sensor
->data_pkt
, rmi_dev
->xport
->attn_data
,
210 rmi_dev
->xport
->attn_data
+= sensor
->attn_size
;
211 rmi_dev
->xport
->attn_size
-= sensor
->attn_size
;
213 retval
= rmi_read_block(rmi_dev
, f12
->data_addr
,
214 sensor
->data_pkt
, sensor
->pkt_size
);
216 dev_err(&fn
->dev
, "Failed to read object data. Code: %d.\n",
223 rmi_f12_process_objects(f12
,
224 &sensor
->data_pkt
[f12
->data1_offset
]);
226 input_mt_sync_frame(sensor
->input
);
231 static int rmi_f12_config(struct rmi_function
*fn
)
233 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
235 drv
->set_irq_bits(fn
->rmi_dev
, fn
->irq_mask
);
240 static int rmi_f12_probe(struct rmi_function
*fn
)
242 struct f12_data
*f12
;
244 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
246 u16 query_addr
= fn
->fd
.query_base_addr
;
247 const struct rmi_register_desc_item
*item
;
248 struct rmi_2d_sensor
*sensor
;
249 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
250 struct rmi_transport_dev
*xport
= rmi_dev
->xport
;
253 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s\n", __func__
);
255 ret
= rmi_read(fn
->rmi_dev
, query_addr
, &buf
);
257 dev_err(&fn
->dev
, "Failed to read general info register: %d\n",
265 "Behavior of F12 without register descriptors is undefined.\n");
269 f12
= devm_kzalloc(&fn
->dev
, sizeof(struct f12_data
), GFP_KERNEL
);
273 if (fn
->dev
.of_node
) {
274 ret
= rmi_2d_sensor_of_probe(&fn
->dev
, &f12
->sensor_pdata
);
277 } else if (pdata
->sensor_pdata
) {
278 f12
->sensor_pdata
= *pdata
->sensor_pdata
;
281 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
282 &f12
->query_reg_desc
);
285 "Failed to read the Query Register Descriptor: %d\n",
291 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
292 &f12
->control_reg_desc
);
295 "Failed to read the Control Register Descriptor: %d\n",
301 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
302 &f12
->data_reg_desc
);
305 "Failed to read the Data Register Descriptor: %d\n",
311 sensor
= &f12
->sensor
;
313 f12
->data_addr
= fn
->fd
.data_base_addr
;
314 sensor
->pkt_size
= rmi_register_desc_calc_size(&f12
->data_reg_desc
);
317 f12
->sensor_pdata
.axis_align
;
319 sensor
->x_mm
= f12
->sensor_pdata
.x_mm
;
320 sensor
->y_mm
= f12
->sensor_pdata
.y_mm
;
322 if (sensor
->sensor_type
== rmi_sensor_default
)
323 sensor
->sensor_type
=
324 f12
->sensor_pdata
.sensor_type
;
326 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: data packet size: %d\n", __func__
,
328 sensor
->data_pkt
= devm_kzalloc(&fn
->dev
, sensor
->pkt_size
, GFP_KERNEL
);
329 if (!sensor
->data_pkt
)
332 dev_set_drvdata(&fn
->dev
, f12
);
334 ret
= rmi_f12_read_sensor_tuning(f12
);
339 * Figure out what data is contained in the data registers. HID devices
340 * may have registers defined, but their data is not reported in the
341 * HID attention report. Registers which are not reported in the HID
342 * attention report check to see if the device is receiving data from
343 * HID attention reports.
345 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 0);
346 if (item
&& !xport
->attn_data
)
347 data_offset
+= item
->reg_size
;
349 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 1);
352 f12
->data1_offset
= data_offset
;
353 data_offset
+= item
->reg_size
;
354 sensor
->nbr_fingers
= item
->num_subpackets
;
355 sensor
->report_abs
= 1;
356 sensor
->attn_size
+= item
->reg_size
;
359 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 2);
360 if (item
&& !xport
->attn_data
)
361 data_offset
+= item
->reg_size
;
363 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 3);
364 if (item
&& !xport
->attn_data
)
365 data_offset
+= item
->reg_size
;
367 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 4);
368 if (item
&& !xport
->attn_data
)
369 data_offset
+= item
->reg_size
;
371 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 5);
374 f12
->data5_offset
= data_offset
;
375 data_offset
+= item
->reg_size
;
376 sensor
->attn_size
+= item
->reg_size
;
379 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 6);
380 if (item
&& !xport
->attn_data
) {
382 f12
->data6_offset
= data_offset
;
383 data_offset
+= item
->reg_size
;
386 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 7);
387 if (item
&& !xport
->attn_data
)
388 data_offset
+= item
->reg_size
;
390 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 8);
391 if (item
&& !xport
->attn_data
)
392 data_offset
+= item
->reg_size
;
394 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 9);
395 if (item
&& !xport
->attn_data
) {
397 f12
->data9_offset
= data_offset
;
398 data_offset
+= item
->reg_size
;
399 if (!sensor
->report_abs
)
400 sensor
->report_rel
= 1;
403 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 10);
404 if (item
&& !xport
->attn_data
)
405 data_offset
+= item
->reg_size
;
407 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 11);
408 if (item
&& !xport
->attn_data
)
409 data_offset
+= item
->reg_size
;
411 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 12);
412 if (item
&& !xport
->attn_data
)
413 data_offset
+= item
->reg_size
;
415 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 13);
416 if (item
&& !xport
->attn_data
)
417 data_offset
+= item
->reg_size
;
419 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 14);
420 if (item
&& !xport
->attn_data
)
421 data_offset
+= item
->reg_size
;
423 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 15);
424 if (item
&& !xport
->attn_data
) {
426 f12
->data15_offset
= data_offset
;
427 data_offset
+= item
->reg_size
;
430 /* allocate the in-kernel tracking buffers */
431 sensor
->tracking_pos
= devm_kzalloc(&fn
->dev
,
432 sizeof(struct input_mt_pos
) * sensor
->nbr_fingers
,
434 sensor
->tracking_slots
= devm_kzalloc(&fn
->dev
,
435 sizeof(int) * sensor
->nbr_fingers
, GFP_KERNEL
);
436 sensor
->objs
= devm_kzalloc(&fn
->dev
,
437 sizeof(struct rmi_2d_sensor_abs_object
)
438 * sensor
->nbr_fingers
, GFP_KERNEL
);
439 if (!sensor
->tracking_pos
|| !sensor
->tracking_slots
|| !sensor
->objs
)
442 ret
= rmi_2d_sensor_configure_input(fn
, sensor
);
449 struct rmi_function_handler rmi_f12_handler
= {
454 .probe
= rmi_f12_probe
,
455 .config
= rmi_f12_config
,
456 .attention
= rmi_f12_attention
,