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
;
62 unsigned long *abs_mask
;
63 unsigned long *rel_mask
;
66 static int rmi_f12_read_sensor_tuning(struct f12_data
*f12
)
68 const struct rmi_register_desc_item
*item
;
69 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
70 struct rmi_function
*fn
= sensor
->fn
;
71 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
81 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 8);
84 "F12 does not have the sensor tuning control register\n");
88 offset
= rmi_register_desc_calc_reg_offset(&f12
->control_reg_desc
, 8);
90 if (item
->reg_size
> sizeof(buf
)) {
92 "F12 control8 should be no bigger than %zd bytes, not: %ld\n",
93 sizeof(buf
), item
->reg_size
);
97 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
+ offset
, buf
,
103 if (rmi_register_desc_has_subpacket(item
, 0)) {
104 sensor
->max_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
105 sensor
->max_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
109 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: max_x: %d max_y: %d\n", __func__
,
110 sensor
->max_x
, sensor
->max_y
);
112 if (rmi_register_desc_has_subpacket(item
, 1)) {
113 pitch_x
= (buf
[offset
+ 1] << 8) | buf
[offset
];
114 pitch_y
= (buf
[offset
+ 3] << 8) | buf
[offset
+ 2];
118 if (rmi_register_desc_has_subpacket(item
, 2)) {
119 /* Units 1/128 sensor pitch */
120 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
,
121 "%s: Inactive Border xlo:%d xhi:%d ylo:%d yhi:%d\n",
123 buf
[offset
], buf
[offset
+ 1],
124 buf
[offset
+ 2], buf
[offset
+ 3]);
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
, int size
)
152 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
153 int objects
= f12
->data1
->num_subpackets
;
155 if ((f12
->data1
->num_subpackets
* F12_DATA1_BYTES_PER_OBJ
) > size
)
156 objects
= size
/ F12_DATA1_BYTES_PER_OBJ
;
158 for (i
= 0; i
< objects
; i
++) {
159 struct rmi_2d_sensor_abs_object
*obj
= &sensor
->objs
[i
];
161 obj
->type
= RMI_2D_OBJECT_NONE
;
162 obj
->mt_tool
= MT_TOOL_FINGER
;
165 case RMI_F12_OBJECT_FINGER
:
166 obj
->type
= RMI_2D_OBJECT_FINGER
;
168 case RMI_F12_OBJECT_STYLUS
:
169 obj
->type
= RMI_2D_OBJECT_STYLUS
;
170 obj
->mt_tool
= MT_TOOL_PEN
;
172 case RMI_F12_OBJECT_PALM
:
173 obj
->type
= RMI_2D_OBJECT_PALM
;
174 obj
->mt_tool
= MT_TOOL_PALM
;
176 case RMI_F12_OBJECT_UNCLASSIFIED
:
177 obj
->type
= RMI_2D_OBJECT_UNCLASSIFIED
;
181 obj
->x
= (data1
[2] << 8) | data1
[1];
182 obj
->y
= (data1
[4] << 8) | data1
[3];
187 rmi_2d_sensor_abs_process(sensor
, obj
, i
);
189 data1
+= F12_DATA1_BYTES_PER_OBJ
;
192 if (sensor
->kernel_tracking
)
193 input_mt_assign_slots(sensor
->input
,
194 sensor
->tracking_slots
,
195 sensor
->tracking_pos
,
199 for (i
= 0; i
< objects
; i
++)
200 rmi_2d_sensor_abs_report(sensor
, &sensor
->objs
[i
], i
);
203 static irqreturn_t
rmi_f12_attention(int irq
, void *ctx
)
206 struct rmi_function
*fn
= ctx
;
207 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
208 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
209 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
210 struct rmi_2d_sensor
*sensor
= &f12
->sensor
;
211 int valid_bytes
= sensor
->pkt_size
;
213 if (drvdata
->attn_data
.data
) {
214 if (sensor
->attn_size
> drvdata
->attn_data
.size
)
215 valid_bytes
= drvdata
->attn_data
.size
;
217 valid_bytes
= sensor
->attn_size
;
218 memcpy(sensor
->data_pkt
, drvdata
->attn_data
.data
,
220 drvdata
->attn_data
.data
+= valid_bytes
;
221 drvdata
->attn_data
.size
-= valid_bytes
;
223 retval
= rmi_read_block(rmi_dev
, f12
->data_addr
,
224 sensor
->data_pkt
, sensor
->pkt_size
);
226 dev_err(&fn
->dev
, "Failed to read object data. Code: %d.\n",
228 return IRQ_RETVAL(retval
);
233 rmi_f12_process_objects(f12
,
234 &sensor
->data_pkt
[f12
->data1_offset
], valid_bytes
);
236 input_mt_sync_frame(sensor
->input
);
241 static int rmi_f12_write_control_regs(struct rmi_function
*fn
)
244 const struct rmi_register_desc_item
*item
;
245 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
246 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
249 u16 control_offset
= 0;
250 u8 subpacket_offset
= 0;
253 && (f12
->sensor
.dribble
!= RMI_REG_STATE_DEFAULT
)) {
254 item
= rmi_get_register_desc_item(&f12
->control_reg_desc
, 20);
256 control_offset
= rmi_register_desc_calc_reg_offset(
257 &f12
->control_reg_desc
, 20);
260 * The byte containing the EnableDribble bit will be
261 * in either byte 0 or byte 2 of control 20. Depending
262 * on the existence of subpacket 0. If control 20 is
263 * larger then 3 bytes, just read the first 3.
265 control_size
= min(item
->reg_size
, 3UL);
267 ret
= rmi_read_block(rmi_dev
, fn
->fd
.control_base_addr
268 + control_offset
, buf
, control_size
);
272 if (rmi_register_desc_has_subpacket(item
, 0))
273 subpacket_offset
+= 1;
275 switch (f12
->sensor
.dribble
) {
276 case RMI_REG_STATE_OFF
:
277 buf
[subpacket_offset
] &= ~BIT(2);
279 case RMI_REG_STATE_ON
:
280 buf
[subpacket_offset
] |= BIT(2);
282 case RMI_REG_STATE_DEFAULT
:
287 ret
= rmi_write_block(rmi_dev
,
288 fn
->fd
.control_base_addr
+ control_offset
,
299 static int rmi_f12_config(struct rmi_function
*fn
)
301 struct rmi_driver
*drv
= fn
->rmi_dev
->driver
;
302 struct f12_data
*f12
= dev_get_drvdata(&fn
->dev
);
303 struct rmi_2d_sensor
*sensor
;
306 sensor
= &f12
->sensor
;
308 if (!sensor
->report_abs
)
309 drv
->clear_irq_bits(fn
->rmi_dev
, f12
->abs_mask
);
311 drv
->set_irq_bits(fn
->rmi_dev
, f12
->abs_mask
);
313 drv
->clear_irq_bits(fn
->rmi_dev
, f12
->rel_mask
);
315 ret
= rmi_f12_write_control_regs(fn
);
318 "Failed to write F12 control registers: %d\n", ret
);
323 static int rmi_f12_probe(struct rmi_function
*fn
)
325 struct f12_data
*f12
;
327 struct rmi_device
*rmi_dev
= fn
->rmi_dev
;
329 u16 query_addr
= fn
->fd
.query_base_addr
;
330 const struct rmi_register_desc_item
*item
;
331 struct rmi_2d_sensor
*sensor
;
332 struct rmi_device_platform_data
*pdata
= rmi_get_platform_data(rmi_dev
);
333 struct rmi_driver_data
*drvdata
= dev_get_drvdata(&rmi_dev
->dev
);
337 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s\n", __func__
);
339 mask_size
= BITS_TO_LONGS(drvdata
->irq_count
) * sizeof(unsigned long);
341 ret
= rmi_read(fn
->rmi_dev
, query_addr
, &buf
);
343 dev_err(&fn
->dev
, "Failed to read general info register: %d\n",
349 if (!(buf
& BIT(0))) {
351 "Behavior of F12 without register descriptors is undefined.\n");
355 f12
= devm_kzalloc(&fn
->dev
, sizeof(struct f12_data
) + mask_size
* 2,
360 f12
->abs_mask
= (unsigned long *)((char *)f12
361 + sizeof(struct f12_data
));
362 f12
->rel_mask
= (unsigned long *)((char *)f12
363 + sizeof(struct f12_data
) + mask_size
);
365 set_bit(fn
->irq_pos
, f12
->abs_mask
);
366 set_bit(fn
->irq_pos
+ 1, f12
->rel_mask
);
368 f12
->has_dribble
= !!(buf
& BIT(3));
370 if (fn
->dev
.of_node
) {
371 ret
= rmi_2d_sensor_of_probe(&fn
->dev
, &f12
->sensor_pdata
);
375 f12
->sensor_pdata
= pdata
->sensor_pdata
;
378 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
379 &f12
->query_reg_desc
);
382 "Failed to read the Query Register Descriptor: %d\n",
388 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
389 &f12
->control_reg_desc
);
392 "Failed to read the Control Register Descriptor: %d\n",
398 ret
= rmi_read_register_desc(rmi_dev
, query_addr
,
399 &f12
->data_reg_desc
);
402 "Failed to read the Data Register Descriptor: %d\n",
408 sensor
= &f12
->sensor
;
410 f12
->data_addr
= fn
->fd
.data_base_addr
;
411 sensor
->pkt_size
= rmi_register_desc_calc_size(&f12
->data_reg_desc
);
414 f12
->sensor_pdata
.axis_align
;
416 sensor
->x_mm
= f12
->sensor_pdata
.x_mm
;
417 sensor
->y_mm
= f12
->sensor_pdata
.y_mm
;
418 sensor
->dribble
= f12
->sensor_pdata
.dribble
;
420 if (sensor
->sensor_type
== rmi_sensor_default
)
421 sensor
->sensor_type
=
422 f12
->sensor_pdata
.sensor_type
;
424 rmi_dbg(RMI_DEBUG_FN
, &fn
->dev
, "%s: data packet size: %d\n", __func__
,
426 sensor
->data_pkt
= devm_kzalloc(&fn
->dev
, sensor
->pkt_size
, GFP_KERNEL
);
427 if (!sensor
->data_pkt
)
430 dev_set_drvdata(&fn
->dev
, f12
);
432 ret
= rmi_f12_read_sensor_tuning(f12
);
437 * Figure out what data is contained in the data registers. HID devices
438 * may have registers defined, but their data is not reported in the
439 * HID attention report. Registers which are not reported in the HID
440 * attention report check to see if the device is receiving data from
441 * HID attention reports.
443 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 0);
444 if (item
&& !drvdata
->attn_data
.data
)
445 data_offset
+= item
->reg_size
;
447 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 1);
450 f12
->data1_offset
= data_offset
;
451 data_offset
+= item
->reg_size
;
452 sensor
->nbr_fingers
= item
->num_subpackets
;
453 sensor
->report_abs
= 1;
454 sensor
->attn_size
+= item
->reg_size
;
457 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 2);
458 if (item
&& !drvdata
->attn_data
.data
)
459 data_offset
+= item
->reg_size
;
461 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 3);
462 if (item
&& !drvdata
->attn_data
.data
)
463 data_offset
+= item
->reg_size
;
465 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 4);
466 if (item
&& !drvdata
->attn_data
.data
)
467 data_offset
+= item
->reg_size
;
469 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 5);
472 f12
->data5_offset
= data_offset
;
473 data_offset
+= item
->reg_size
;
474 sensor
->attn_size
+= item
->reg_size
;
477 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 6);
478 if (item
&& !drvdata
->attn_data
.data
) {
480 f12
->data6_offset
= data_offset
;
481 data_offset
+= item
->reg_size
;
484 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 7);
485 if (item
&& !drvdata
->attn_data
.data
)
486 data_offset
+= item
->reg_size
;
488 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 8);
489 if (item
&& !drvdata
->attn_data
.data
)
490 data_offset
+= item
->reg_size
;
492 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 9);
493 if (item
&& !drvdata
->attn_data
.data
) {
495 f12
->data9_offset
= data_offset
;
496 data_offset
+= item
->reg_size
;
497 if (!sensor
->report_abs
)
498 sensor
->report_rel
= 1;
501 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 10);
502 if (item
&& !drvdata
->attn_data
.data
)
503 data_offset
+= item
->reg_size
;
505 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 11);
506 if (item
&& !drvdata
->attn_data
.data
)
507 data_offset
+= item
->reg_size
;
509 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 12);
510 if (item
&& !drvdata
->attn_data
.data
)
511 data_offset
+= item
->reg_size
;
513 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 13);
514 if (item
&& !drvdata
->attn_data
.data
)
515 data_offset
+= item
->reg_size
;
517 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 14);
518 if (item
&& !drvdata
->attn_data
.data
)
519 data_offset
+= item
->reg_size
;
521 item
= rmi_get_register_desc_item(&f12
->data_reg_desc
, 15);
522 if (item
&& !drvdata
->attn_data
.data
) {
524 f12
->data15_offset
= data_offset
;
525 data_offset
+= item
->reg_size
;
528 /* allocate the in-kernel tracking buffers */
529 sensor
->tracking_pos
= devm_kcalloc(&fn
->dev
,
530 sensor
->nbr_fingers
, sizeof(struct input_mt_pos
),
532 sensor
->tracking_slots
= devm_kcalloc(&fn
->dev
,
533 sensor
->nbr_fingers
, sizeof(int), GFP_KERNEL
);
534 sensor
->objs
= devm_kcalloc(&fn
->dev
,
536 sizeof(struct rmi_2d_sensor_abs_object
),
538 if (!sensor
->tracking_pos
|| !sensor
->tracking_slots
|| !sensor
->objs
)
541 ret
= rmi_2d_sensor_configure_input(fn
, sensor
);
548 struct rmi_function_handler rmi_f12_handler
= {
553 .probe
= rmi_f12_probe
,
554 .config
= rmi_f12_config
,
555 .attention
= rmi_f12_attention
,