Merge tag 'pci-v5.11-fixes-1' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[platform/kernel/linux-starfive.git] / drivers / input / rmi4 / rmi_f11.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2011-2015 Synaptics Incorporated
4  * Copyright (c) 2011 Unixphere
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/input.h>
11 #include <linux/input/mt.h>
12 #include <linux/rmi.h>
13 #include <linux/slab.h>
14 #include <linux/of.h>
15 #include "rmi_driver.h"
16 #include "rmi_2d_sensor.h"
17
18 #define F11_MAX_NUM_OF_FINGERS          10
19 #define F11_MAX_NUM_OF_TOUCH_SHAPES     16
20
21 #define FINGER_STATE_MASK       0x03
22
23 #define F11_CTRL_SENSOR_MAX_X_POS_OFFSET        6
24 #define F11_CTRL_SENSOR_MAX_Y_POS_OFFSET        8
25
26 #define DEFAULT_XY_MAX 9999
27 #define DEFAULT_MAX_ABS_MT_PRESSURE 255
28 #define DEFAULT_MAX_ABS_MT_TOUCH 15
29 #define DEFAULT_MAX_ABS_MT_ORIENTATION 1
30 #define DEFAULT_MIN_ABS_MT_TRACKING_ID 1
31 #define DEFAULT_MAX_ABS_MT_TRACKING_ID 10
32
33 /*
34  * A note about RMI4 F11 register structure.
35  *
36  * The properties for a given sensor are described by its query registers.  The
37  * number of query registers and the layout of their contents are described by
38  * the F11 device queries as well as the sensor query information.
39  *
40  * Similarly, each sensor has control registers that govern its behavior.  The
41  * size and layout of the control registers for a given sensor can be determined
42  * by parsing that sensors query registers.
43  *
44  * And in a likewise fashion, each sensor has data registers where it reports
45  * its touch data and other interesting stuff.  The size and layout of a
46  * sensors data registers must be determined by parsing its query registers.
47  *
48  * The short story is that we need to read and parse a lot of query
49  * registers in order to determine the attributes of a sensor. Then
50  * we need to use that data to compute the size of the control and data
51  * registers for sensor.
52  *
53  * The end result is that we have a number of structs that aren't used to
54  * directly generate the input events, but their size, location and contents
55  * are critical to determining where the data we are interested in lives.
56  *
57  * At this time, the driver does not yet comprehend all possible F11
58  * configuration options, but it should be sufficient to cover 99% of RMI4 F11
59  * devices currently in the field.
60  */
61
62 /* maximum ABS_MT_POSITION displacement (in mm) */
63 #define DMAX 10
64
65 /*
66  * Writing this to the F11 command register will cause the sensor to
67  * calibrate to the current capacitive state.
68  */
69 #define RMI_F11_REZERO  0x01
70
71 #define RMI_F11_HAS_QUERY9              (1 << 3)
72 #define RMI_F11_HAS_QUERY11             (1 << 4)
73 #define RMI_F11_HAS_QUERY12             (1 << 5)
74 #define RMI_F11_HAS_QUERY27             (1 << 6)
75 #define RMI_F11_HAS_QUERY28             (1 << 7)
76
77 /** Defs for Query 1 */
78
79 #define RMI_F11_NR_FINGERS_MASK 0x07
80 #define RMI_F11_HAS_REL                 (1 << 3)
81 #define RMI_F11_HAS_ABS                 (1 << 4)
82 #define RMI_F11_HAS_GESTURES            (1 << 5)
83 #define RMI_F11_HAS_SENSITIVITY_ADJ     (1 << 6)
84 #define RMI_F11_CONFIGURABLE            (1 << 7)
85
86 /** Defs for Query 2, 3, and 4. */
87 #define RMI_F11_NR_ELECTRODES_MASK      0x7F
88
89 /** Defs for Query 5 */
90
91 #define RMI_F11_ABS_DATA_SIZE_MASK      0x03
92 #define RMI_F11_HAS_ANCHORED_FINGER     (1 << 2)
93 #define RMI_F11_HAS_ADJ_HYST            (1 << 3)
94 #define RMI_F11_HAS_DRIBBLE             (1 << 4)
95 #define RMI_F11_HAS_BENDING_CORRECTION  (1 << 5)
96 #define RMI_F11_HAS_LARGE_OBJECT_SUPPRESSION    (1 << 6)
97 #define RMI_F11_HAS_JITTER_FILTER       (1 << 7)
98
99 /** Defs for Query 7 */
100 #define RMI_F11_HAS_SINGLE_TAP                  (1 << 0)
101 #define RMI_F11_HAS_TAP_AND_HOLD                (1 << 1)
102 #define RMI_F11_HAS_DOUBLE_TAP                  (1 << 2)
103 #define RMI_F11_HAS_EARLY_TAP                   (1 << 3)
104 #define RMI_F11_HAS_FLICK                       (1 << 4)
105 #define RMI_F11_HAS_PRESS                       (1 << 5)
106 #define RMI_F11_HAS_PINCH                       (1 << 6)
107 #define RMI_F11_HAS_CHIRAL                      (1 << 7)
108
109 /** Defs for Query 8 */
110 #define RMI_F11_HAS_PALM_DET                    (1 << 0)
111 #define RMI_F11_HAS_ROTATE                      (1 << 1)
112 #define RMI_F11_HAS_TOUCH_SHAPES                (1 << 2)
113 #define RMI_F11_HAS_SCROLL_ZONES                (1 << 3)
114 #define RMI_F11_HAS_INDIVIDUAL_SCROLL_ZONES     (1 << 4)
115 #define RMI_F11_HAS_MF_SCROLL                   (1 << 5)
116 #define RMI_F11_HAS_MF_EDGE_MOTION              (1 << 6)
117 #define RMI_F11_HAS_MF_SCROLL_INERTIA           (1 << 7)
118
119 /** Defs for Query 9. */
120 #define RMI_F11_HAS_PEN                         (1 << 0)
121 #define RMI_F11_HAS_PROXIMITY                   (1 << 1)
122 #define RMI_F11_HAS_PALM_DET_SENSITIVITY        (1 << 2)
123 #define RMI_F11_HAS_SUPPRESS_ON_PALM_DETECT     (1 << 3)
124 #define RMI_F11_HAS_TWO_PEN_THRESHOLDS          (1 << 4)
125 #define RMI_F11_HAS_CONTACT_GEOMETRY            (1 << 5)
126 #define RMI_F11_HAS_PEN_HOVER_DISCRIMINATION    (1 << 6)
127 #define RMI_F11_HAS_PEN_FILTERS                 (1 << 7)
128
129 /** Defs for Query 10. */
130 #define RMI_F11_NR_TOUCH_SHAPES_MASK            0x1F
131
132 /** Defs for Query 11 */
133
134 #define RMI_F11_HAS_Z_TUNING                    (1 << 0)
135 #define RMI_F11_HAS_ALGORITHM_SELECTION         (1 << 1)
136 #define RMI_F11_HAS_W_TUNING                    (1 << 2)
137 #define RMI_F11_HAS_PITCH_INFO                  (1 << 3)
138 #define RMI_F11_HAS_FINGER_SIZE                 (1 << 4)
139 #define RMI_F11_HAS_SEGMENTATION_AGGRESSIVENESS (1 << 5)
140 #define RMI_F11_HAS_XY_CLIP                     (1 << 6)
141 #define RMI_F11_HAS_DRUMMING_FILTER             (1 << 7)
142
143 /** Defs for Query 12. */
144
145 #define RMI_F11_HAS_GAPLESS_FINGER              (1 << 0)
146 #define RMI_F11_HAS_GAPLESS_FINGER_TUNING       (1 << 1)
147 #define RMI_F11_HAS_8BIT_W                      (1 << 2)
148 #define RMI_F11_HAS_ADJUSTABLE_MAPPING          (1 << 3)
149 #define RMI_F11_HAS_INFO2                       (1 << 4)
150 #define RMI_F11_HAS_PHYSICAL_PROPS              (1 << 5)
151 #define RMI_F11_HAS_FINGER_LIMIT                (1 << 6)
152 #define RMI_F11_HAS_LINEAR_COEFF                (1 << 7)
153
154 /** Defs for Query 13. */
155
156 #define RMI_F11_JITTER_WINDOW_MASK              0x1F
157 #define RMI_F11_JITTER_FILTER_MASK              0x60
158 #define RMI_F11_JITTER_FILTER_SHIFT             5
159
160 /** Defs for Query 14. */
161 #define RMI_F11_LIGHT_CONTROL_MASK              0x03
162 #define RMI_F11_IS_CLEAR                        (1 << 2)
163 #define RMI_F11_CLICKPAD_PROPS_MASK             0x18
164 #define RMI_F11_CLICKPAD_PROPS_SHIFT            3
165 #define RMI_F11_MOUSE_BUTTONS_MASK              0x60
166 #define RMI_F11_MOUSE_BUTTONS_SHIFT             5
167 #define RMI_F11_HAS_ADVANCED_GESTURES           (1 << 7)
168
169 #define RMI_F11_QUERY_SIZE                      4
170 #define RMI_F11_QUERY_GESTURE_SIZE              2
171
172 #define F11_LIGHT_CTL_NONE 0x00
173 #define F11_LUXPAD         0x01
174 #define F11_DUAL_MODE      0x02
175
176 #define F11_NOT_CLICKPAD     0x00
177 #define F11_HINGED_CLICKPAD  0x01
178 #define F11_UNIFORM_CLICKPAD 0x02
179
180 /**
181  * struct f11_2d_sensor_queries - describes sensor capabilities
182  *
183  * Query registers 1 through 4 are always present.
184  *
185  * @nr_fingers: describes the maximum number of fingers the 2-D sensor
186  *      supports.
187  * @has_rel: the sensor supports relative motion reporting.
188  * @has_abs: the sensor supports absolute poition reporting.
189  * @has_gestures: the sensor supports gesture reporting.
190  * @has_sensitivity_adjust: the sensor supports a global sensitivity
191  *      adjustment.
192  * @configurable: the sensor supports various configuration options.
193  * @nr_x_electrodes:  the maximum number of electrodes the 2-D sensor
194  *      supports on the X axis.
195  * @nr_y_electrodes:  the maximum number of electrodes the 2-D sensor
196  *      supports on the Y axis.
197  * @max_electrodes: the total number of X and Y electrodes that may be
198  *      configured.
199  *
200  * Query 5 is present if the has_abs bit is set.
201  *
202  * @abs_data_size: describes the format of data reported by the absolute
203  *      data source.  Only one format (the kind used here) is supported at this
204  *      time.
205  * @has_anchored_finger: then the sensor supports the high-precision second
206  *      finger tracking provided by the manual tracking and motion sensitivity
207  *      options.
208  * @has_adj_hyst: the difference between the finger release threshold and
209  *      the touch threshold.
210  * @has_dribble: the sensor supports the generation of dribble interrupts,
211  *      which may be enabled or disabled with the dribble control bit.
212  * @has_bending_correction: Bending related data registers 28 and 36, and
213  *      control register 52..57 are present.
214  * @has_large_object_suppression: control register 58 and data register 28
215  *      exist.
216  * @has_jitter_filter: query 13 and control 73..76 exist.
217  *
218  * Query 6 is present if the has_rel it is set.
219  *
220  * @f11_2d_query6: this register is reserved.
221  *
222  * Gesture information queries 7 and 8 are present if has_gestures bit is set.
223  *
224  * @has_single_tap: a basic single-tap gesture is supported.
225  * @has_tap_n_hold: tap-and-hold gesture is supported.
226  * @has_double_tap: double-tap gesture is supported.
227  * @has_early_tap: early tap is supported and reported as soon as the finger
228  *      lifts for any tap event that could be interpreted as either a single
229  *      tap or as the first tap of a double-tap or tap-and-hold gesture.
230  * @has_flick: flick detection is supported.
231  * @has_press: press gesture reporting is supported.
232  * @has_pinch: pinch gesture detection is supported.
233  * @has_chiral: chiral (circular) scrolling  gesture detection is supported.
234  * @has_palm_det: the 2-D sensor notifies the host whenever a large conductive
235  *      object such as a palm or a cheek touches the 2-D sensor.
236  * @has_rotate: rotation gesture detection is supported.
237  * @has_touch_shapes: TouchShapes are supported.  A TouchShape is a fixed
238  *      rectangular area on the sensor that behaves like a capacitive button.
239  * @has_scroll_zones: scrolling areas near the sensor edges are supported.
240  * @has_individual_scroll_zones: if 1, then 4 scroll zones are supported;
241  *      if 0, then only two are supported.
242  * @has_mf_scroll: the multifinger_scrolling bit will be set when
243  *      more than one finger is involved in a scrolling action.
244  * @has_mf_edge_motion: indicates whether multi-finger edge motion gesture
245  *      is supported.
246  * @has_mf_scroll_inertia: indicates whether multi-finger scroll inertia
247  *      feature is supported.
248  *
249  * Convenience for checking bytes in the gesture info registers.  This is done
250  * often enough that we put it here to declutter the conditionals
251  *
252  * @query7_nonzero: true if none of the query 7 bits are set
253  * @query8_nonzero: true if none of the query 8 bits are set
254  *
255  * Query 9 is present if the has_query9 is set.
256  *
257  * @has_pen: detection of a stylus is supported and registers F11_2D_Ctrl20
258  *      and F11_2D_Ctrl21 exist.
259  * @has_proximity: detection of fingers near the sensor is supported and
260  *      registers F11_2D_Ctrl22 through F11_2D_Ctrl26 exist.
261  * @has_palm_det_sensitivity:  the sensor supports the palm detect sensitivity
262  *      feature and register F11_2D_Ctrl27 exists.
263  * @has_suppress_on_palm_detect: the device supports the large object detect
264  *      suppression feature and register F11_2D_Ctrl27 exists.
265  * @has_two_pen_thresholds: if has_pen is also set, then F11_2D_Ctrl35 exists.
266  * @has_contact_geometry: the sensor supports the use of contact geometry to
267  *      map absolute X and Y target positions and registers F11_2D_Data18
268  *      through F11_2D_Data27 exist.
269  * @has_pen_hover_discrimination: if has_pen is also set, then registers
270  *      F11_2D_Data29 through F11_2D_Data31, F11_2D_Ctrl68.*, F11_2D_Ctrl69
271  *      and F11_2D_Ctrl72 exist.
272  * @has_pen_filters: if has_pen is also set, then registers F11_2D_Ctrl70 and
273  *      F11_2D_Ctrl71 exist.
274  *
275  * Touch shape info (query 10) is present if has_touch_shapes is set.
276  *
277  * @nr_touch_shapes: the total number of touch shapes supported.
278  *
279  * Query 11 is present if the has_query11 bit is set in query 0.
280  *
281  * @has_z_tuning: if set, the sensor supports Z tuning and registers
282  *      F11_2D_Ctrl29 through F11_2D_Ctrl33 exist.
283  * @has_algorithm_selection: controls choice of noise suppression algorithm
284  * @has_w_tuning: the sensor supports Wx and Wy scaling and registers
285  *      F11_2D_Ctrl36 through F11_2D_Ctrl39 exist.
286  * @has_pitch_info: the X and Y pitches of the sensor electrodes can be
287  *      configured and registers F11_2D_Ctrl40 and F11_2D_Ctrl41 exist.
288  * @has_finger_size: the default finger width settings for the sensor
289  *      can be configured and registers F11_2D_Ctrl42 through F11_2D_Ctrl44
290  *      exist.
291  * @has_segmentation_aggressiveness: the sensor’s ability to distinguish
292  *      multiple objects close together can be configured and register
293  *      F11_2D_Ctrl45 exists.
294  * @has_XY_clip: the inactive outside borders of the sensor can be
295  *      configured and registers F11_2D_Ctrl46 through F11_2D_Ctrl49 exist.
296  * @has_drumming_filter: the sensor can be configured to distinguish
297  *      between a fast flick and a quick drumming movement and registers
298  *      F11_2D_Ctrl50 and F11_2D_Ctrl51 exist.
299  *
300  * Query 12 is present if hasQuery12 bit is set.
301  *
302  * @has_gapless_finger: control registers relating to gapless finger are
303  *      present.
304  * @has_gapless_finger_tuning: additional control and data registers relating
305  *      to gapless finger are present.
306  * @has_8bit_w: larger W value reporting is supported.
307  * @has_adjustable_mapping: TBD
308  * @has_info2: the general info query14 is present
309  * @has_physical_props: additional queries describing the physical properties
310  *      of the sensor are present.
311  * @has_finger_limit: indicates that F11 Ctrl 80 exists.
312  * @has_linear_coeff_2: indicates that F11 Ctrl 81 exists.
313  *
314  * Query 13 is present if Query 5's has_jitter_filter bit is set.
315  *
316  * @jitter_window_size: used by Design Studio 4.
317  * @jitter_filter_type: used by Design Studio 4.
318  *
319  * Query 14 is present if query 12's has_general_info2 flag is set.
320  *
321  * @light_control: Indicates what light/led control features are present,
322  *      if any.
323  * @is_clear: if set, this is a clear sensor (indicating direct pointing
324  *      application), otherwise it's opaque (indicating indirect pointing).
325  * @clickpad_props: specifies if this is a clickpad, and if so what sort of
326  *      mechanism it uses
327  * @mouse_buttons: specifies the number of mouse buttons present (if any).
328  * @has_advanced_gestures: advanced driver gestures are supported.
329  *
330  * @x_sensor_size_mm: size of the sensor in millimeters on the X axis.
331  * @y_sensor_size_mm: size of the sensor in millimeters on the Y axis.
332  */
333 struct f11_2d_sensor_queries {
334         /* query1 */
335         u8 nr_fingers;
336         bool has_rel;
337         bool has_abs;
338         bool has_gestures;
339         bool has_sensitivity_adjust;
340         bool configurable;
341
342         /* query2 */
343         u8 nr_x_electrodes;
344
345         /* query3 */
346         u8 nr_y_electrodes;
347
348         /* query4 */
349         u8 max_electrodes;
350
351         /* query5 */
352         u8 abs_data_size;
353         bool has_anchored_finger;
354         bool has_adj_hyst;
355         bool has_dribble;
356         bool has_bending_correction;
357         bool has_large_object_suppression;
358         bool has_jitter_filter;
359
360         u8 f11_2d_query6;
361
362         /* query 7 */
363         bool has_single_tap;
364         bool has_tap_n_hold;
365         bool has_double_tap;
366         bool has_early_tap;
367         bool has_flick;
368         bool has_press;
369         bool has_pinch;
370         bool has_chiral;
371
372         bool query7_nonzero;
373
374         /* query 8 */
375         bool has_palm_det;
376         bool has_rotate;
377         bool has_touch_shapes;
378         bool has_scroll_zones;
379         bool has_individual_scroll_zones;
380         bool has_mf_scroll;
381         bool has_mf_edge_motion;
382         bool has_mf_scroll_inertia;
383
384         bool query8_nonzero;
385
386         /* Query 9 */
387         bool has_pen;
388         bool has_proximity;
389         bool has_palm_det_sensitivity;
390         bool has_suppress_on_palm_detect;
391         bool has_two_pen_thresholds;
392         bool has_contact_geometry;
393         bool has_pen_hover_discrimination;
394         bool has_pen_filters;
395
396         /* Query 10 */
397         u8 nr_touch_shapes;
398
399         /* Query 11. */
400         bool has_z_tuning;
401         bool has_algorithm_selection;
402         bool has_w_tuning;
403         bool has_pitch_info;
404         bool has_finger_size;
405         bool has_segmentation_aggressiveness;
406         bool has_XY_clip;
407         bool has_drumming_filter;
408
409         /* Query 12 */
410         bool has_gapless_finger;
411         bool has_gapless_finger_tuning;
412         bool has_8bit_w;
413         bool has_adjustable_mapping;
414         bool has_info2;
415         bool has_physical_props;
416         bool has_finger_limit;
417         bool has_linear_coeff_2;
418
419         /* Query 13 */
420         u8 jitter_window_size;
421         u8 jitter_filter_type;
422
423         /* Query 14 */
424         u8 light_control;
425         bool is_clear;
426         u8 clickpad_props;
427         u8 mouse_buttons;
428         bool has_advanced_gestures;
429
430         /* Query 15 - 18 */
431         u16 x_sensor_size_mm;
432         u16 y_sensor_size_mm;
433 };
434
435 /* Defs for Ctrl0. */
436 #define RMI_F11_REPORT_MODE_MASK        0x07
437 #define RMI_F11_REPORT_MODE_CONTINUOUS  (0 << 0)
438 #define RMI_F11_REPORT_MODE_REDUCED     (1 << 0)
439 #define RMI_F11_REPORT_MODE_FS_CHANGE   (2 << 0)
440 #define RMI_F11_REPORT_MODE_FP_CHANGE   (3 << 0)
441 #define RMI_F11_ABS_POS_FILT            (1 << 3)
442 #define RMI_F11_REL_POS_FILT            (1 << 4)
443 #define RMI_F11_REL_BALLISTICS          (1 << 5)
444 #define RMI_F11_DRIBBLE                 (1 << 6)
445 #define RMI_F11_REPORT_BEYOND_CLIP      (1 << 7)
446
447 /* Defs for Ctrl1. */
448 #define RMI_F11_PALM_DETECT_THRESH_MASK 0x0F
449 #define RMI_F11_MOTION_SENSITIVITY_MASK 0x30
450 #define RMI_F11_MANUAL_TRACKING         (1 << 6)
451 #define RMI_F11_MANUAL_TRACKED_FINGER   (1 << 7)
452
453 #define RMI_F11_DELTA_X_THRESHOLD       2
454 #define RMI_F11_DELTA_Y_THRESHOLD       3
455
456 #define RMI_F11_CTRL_REG_COUNT          12
457
458 struct f11_2d_ctrl {
459         u8              ctrl0_11[RMI_F11_CTRL_REG_COUNT];
460         u16             ctrl0_11_address;
461 };
462
463 #define RMI_F11_ABS_BYTES 5
464 #define RMI_F11_REL_BYTES 2
465
466 /* Defs for Data 8 */
467
468 #define RMI_F11_SINGLE_TAP              (1 << 0)
469 #define RMI_F11_TAP_AND_HOLD            (1 << 1)
470 #define RMI_F11_DOUBLE_TAP              (1 << 2)
471 #define RMI_F11_EARLY_TAP               (1 << 3)
472 #define RMI_F11_FLICK                   (1 << 4)
473 #define RMI_F11_PRESS                   (1 << 5)
474 #define RMI_F11_PINCH                   (1 << 6)
475
476 /* Defs for Data 9 */
477
478 #define RMI_F11_PALM_DETECT                     (1 << 0)
479 #define RMI_F11_ROTATE                          (1 << 1)
480 #define RMI_F11_SHAPE                           (1 << 2)
481 #define RMI_F11_SCROLLZONE                      (1 << 3)
482 #define RMI_F11_GESTURE_FINGER_COUNT_MASK       0x70
483
484 /** Handy pointers into our data buffer.
485  *
486  * @f_state - start of finger state registers.
487  * @abs_pos - start of absolute position registers (if present).
488  * @rel_pos - start of relative data registers (if present).
489  * @gest_1  - gesture flags (if present).
490  * @gest_2  - gesture flags & finger count (if present).
491  * @pinch   - pinch motion register (if present).
492  * @flick   - flick distance X & Y, flick time (if present).
493  * @rotate  - rotate motion and finger separation.
494  * @multi_scroll - chiral deltas for X and Y (if present).
495  * @scroll_zones - scroll deltas for 4 regions (if present).
496  */
497 struct f11_2d_data {
498         u8      *f_state;
499         u8      *abs_pos;
500         s8      *rel_pos;
501         u8      *gest_1;
502         u8      *gest_2;
503         s8      *pinch;
504         u8      *flick;
505         u8      *rotate;
506         u8      *shapes;
507         s8      *multi_scroll;
508         s8      *scroll_zones;
509 };
510
511 /** Data pertaining to F11 in general.  For per-sensor data, see struct
512  * f11_2d_sensor.
513  *
514  * @dev_query - F11 device specific query registers.
515  * @dev_controls - F11 device specific control registers.
516  * @dev_controls_mutex - lock for the control registers.
517  * @rezero_wait_ms - if nonzero, upon resume we will wait this many
518  * milliseconds before rezeroing the sensor(s).  This is useful in systems with
519  * poor electrical behavior on resume, where the initial calibration of the
520  * sensor(s) coming out of sleep state may be bogus.
521  * @sensors - per sensor data structures.
522  */
523 struct f11_data {
524         bool has_query9;
525         bool has_query11;
526         bool has_query12;
527         bool has_query27;
528         bool has_query28;
529         bool has_acm;
530         struct f11_2d_ctrl dev_controls;
531         struct mutex dev_controls_mutex;
532         u16 rezero_wait_ms;
533         struct rmi_2d_sensor sensor;
534         struct f11_2d_sensor_queries sens_query;
535         struct f11_2d_data data;
536         struct rmi_2d_sensor_platform_data sensor_pdata;
537         unsigned long *abs_mask;
538         unsigned long *rel_mask;
539 };
540
541 enum f11_finger_state {
542         F11_NO_FINGER   = 0x00,
543         F11_PRESENT     = 0x01,
544         F11_INACCURATE  = 0x02,
545         F11_RESERVED    = 0x03
546 };
547
548 static void rmi_f11_rel_pos_report(struct f11_data *f11, u8 n_finger)
549 {
550         struct rmi_2d_sensor *sensor = &f11->sensor;
551         struct f11_2d_data *data = &f11->data;
552         s8 x, y;
553
554         x = data->rel_pos[n_finger * RMI_F11_REL_BYTES];
555         y = data->rel_pos[n_finger * RMI_F11_REL_BYTES + 1];
556
557         rmi_2d_sensor_rel_report(sensor, x, y);
558 }
559
560 static void rmi_f11_abs_pos_process(struct f11_data *f11,
561                                    struct rmi_2d_sensor *sensor,
562                                    struct rmi_2d_sensor_abs_object *obj,
563                                    enum f11_finger_state finger_state,
564                                    u8 n_finger)
565 {
566         struct f11_2d_data *data = &f11->data;
567         u8 *pos_data = &data->abs_pos[n_finger * RMI_F11_ABS_BYTES];
568         int tool_type = MT_TOOL_FINGER;
569
570         switch (finger_state) {
571         case F11_PRESENT:
572                 obj->type = RMI_2D_OBJECT_FINGER;
573                 break;
574         default:
575                 obj->type = RMI_2D_OBJECT_NONE;
576         }
577
578         obj->mt_tool = tool_type;
579         obj->x = (pos_data[0] << 4) | (pos_data[2] & 0x0F);
580         obj->y = (pos_data[1] << 4) | (pos_data[2] >> 4);
581         obj->z = pos_data[4];
582         obj->wx = pos_data[3] & 0x0f;
583         obj->wy = pos_data[3] >> 4;
584
585         rmi_2d_sensor_abs_process(sensor, obj, n_finger);
586 }
587
588 static inline u8 rmi_f11_parse_finger_state(const u8 *f_state, u8 n_finger)
589 {
590         return (f_state[n_finger / 4] >> (2 * (n_finger % 4))) &
591                                                         FINGER_STATE_MASK;
592 }
593
594 static void rmi_f11_finger_handler(struct f11_data *f11,
595                                    struct rmi_2d_sensor *sensor, int size)
596 {
597         const u8 *f_state = f11->data.f_state;
598         u8 finger_state;
599         u8 i;
600         int abs_fingers;
601         int rel_fingers;
602         int abs_size = sensor->nbr_fingers * RMI_F11_ABS_BYTES;
603
604         if (sensor->report_abs) {
605                 if (abs_size > size)
606                         abs_fingers = size / RMI_F11_ABS_BYTES;
607                 else
608                         abs_fingers = sensor->nbr_fingers;
609
610                 for (i = 0; i < abs_fingers; i++) {
611                         /* Possible of having 4 fingers per f_state register */
612                         finger_state = rmi_f11_parse_finger_state(f_state, i);
613                         if (finger_state == F11_RESERVED) {
614                                 pr_err("Invalid finger state[%d]: 0x%02x", i,
615                                         finger_state);
616                                 continue;
617                         }
618
619                         rmi_f11_abs_pos_process(f11, sensor, &sensor->objs[i],
620                                                         finger_state, i);
621                 }
622
623                 /*
624                  * the absolute part is made in 2 parts to allow the kernel
625                  * tracking to take place.
626                  */
627                 if (sensor->kernel_tracking)
628                         input_mt_assign_slots(sensor->input,
629                                               sensor->tracking_slots,
630                                               sensor->tracking_pos,
631                                               sensor->nbr_fingers,
632                                               sensor->dmax);
633
634                 for (i = 0; i < abs_fingers; i++) {
635                         finger_state = rmi_f11_parse_finger_state(f_state, i);
636                         if (finger_state == F11_RESERVED)
637                                 /* no need to send twice the error */
638                                 continue;
639
640                         rmi_2d_sensor_abs_report(sensor, &sensor->objs[i], i);
641                 }
642
643                 input_mt_sync_frame(sensor->input);
644         } else if (sensor->report_rel) {
645                 if ((abs_size + sensor->nbr_fingers * RMI_F11_REL_BYTES) > size)
646                         rel_fingers = (size - abs_size) / RMI_F11_REL_BYTES;
647                 else
648                         rel_fingers = sensor->nbr_fingers;
649
650                 for (i = 0; i < rel_fingers; i++)
651                         rmi_f11_rel_pos_report(f11, i);
652         }
653
654 }
655
656 static int f11_2d_construct_data(struct f11_data *f11)
657 {
658         struct rmi_2d_sensor *sensor = &f11->sensor;
659         struct f11_2d_sensor_queries *query = &f11->sens_query;
660         struct f11_2d_data *data = &f11->data;
661         int i;
662
663         sensor->nbr_fingers = (query->nr_fingers == 5 ? 10 :
664                                 query->nr_fingers + 1);
665
666         sensor->pkt_size = DIV_ROUND_UP(sensor->nbr_fingers, 4);
667
668         if (query->has_abs) {
669                 sensor->pkt_size += (sensor->nbr_fingers * 5);
670                 sensor->attn_size = sensor->pkt_size;
671         }
672
673         if (query->has_rel)
674                 sensor->pkt_size +=  (sensor->nbr_fingers * 2);
675
676         /* Check if F11_2D_Query7 is non-zero */
677         if (query->query7_nonzero)
678                 sensor->pkt_size += sizeof(u8);
679
680         /* Check if F11_2D_Query7 or F11_2D_Query8 is non-zero */
681         if (query->query7_nonzero || query->query8_nonzero)
682                 sensor->pkt_size += sizeof(u8);
683
684         if (query->has_pinch || query->has_flick || query->has_rotate) {
685                 sensor->pkt_size += 3;
686                 if (!query->has_flick)
687                         sensor->pkt_size--;
688                 if (!query->has_rotate)
689                         sensor->pkt_size--;
690         }
691
692         if (query->has_touch_shapes)
693                 sensor->pkt_size +=
694                         DIV_ROUND_UP(query->nr_touch_shapes + 1, 8);
695
696         sensor->data_pkt = devm_kzalloc(&sensor->fn->dev, sensor->pkt_size,
697                                         GFP_KERNEL);
698         if (!sensor->data_pkt)
699                 return -ENOMEM;
700
701         data->f_state = sensor->data_pkt;
702         i = DIV_ROUND_UP(sensor->nbr_fingers, 4);
703
704         if (query->has_abs) {
705                 data->abs_pos = &sensor->data_pkt[i];
706                 i += (sensor->nbr_fingers * RMI_F11_ABS_BYTES);
707         }
708
709         if (query->has_rel) {
710                 data->rel_pos = &sensor->data_pkt[i];
711                 i += (sensor->nbr_fingers * RMI_F11_REL_BYTES);
712         }
713
714         if (query->query7_nonzero) {
715                 data->gest_1 = &sensor->data_pkt[i];
716                 i++;
717         }
718
719         if (query->query7_nonzero || query->query8_nonzero) {
720                 data->gest_2 = &sensor->data_pkt[i];
721                 i++;
722         }
723
724         if (query->has_pinch) {
725                 data->pinch = &sensor->data_pkt[i];
726                 i++;
727         }
728
729         if (query->has_flick) {
730                 if (query->has_pinch) {
731                         data->flick = data->pinch;
732                         i += 2;
733                 } else {
734                         data->flick = &sensor->data_pkt[i];
735                         i += 3;
736                 }
737         }
738
739         if (query->has_rotate) {
740                 if (query->has_flick) {
741                         data->rotate = data->flick + 1;
742                 } else {
743                         data->rotate = &sensor->data_pkt[i];
744                         i += 2;
745                 }
746         }
747
748         if (query->has_touch_shapes)
749                 data->shapes = &sensor->data_pkt[i];
750
751         return 0;
752 }
753
754 static int f11_read_control_regs(struct rmi_function *fn,
755                                 struct f11_2d_ctrl *ctrl, u16 ctrl_base_addr) {
756         struct rmi_device *rmi_dev = fn->rmi_dev;
757         int error = 0;
758
759         ctrl->ctrl0_11_address = ctrl_base_addr;
760         error = rmi_read_block(rmi_dev, ctrl_base_addr, ctrl->ctrl0_11,
761                                 RMI_F11_CTRL_REG_COUNT);
762         if (error < 0) {
763                 dev_err(&fn->dev, "Failed to read ctrl0, code: %d.\n", error);
764                 return error;
765         }
766
767         return 0;
768 }
769
770 static int f11_write_control_regs(struct rmi_function *fn,
771                                         struct f11_2d_sensor_queries *query,
772                                         struct f11_2d_ctrl *ctrl,
773                                         u16 ctrl_base_addr)
774 {
775         struct rmi_device *rmi_dev = fn->rmi_dev;
776         int error;
777
778         error = rmi_write_block(rmi_dev, ctrl_base_addr, ctrl->ctrl0_11,
779                                 RMI_F11_CTRL_REG_COUNT);
780         if (error < 0)
781                 return error;
782
783         return 0;
784 }
785
786 static int rmi_f11_get_query_parameters(struct rmi_device *rmi_dev,
787                         struct f11_data *f11,
788                         struct f11_2d_sensor_queries *sensor_query,
789                         u16 query_base_addr)
790 {
791         int query_size;
792         int rc;
793         u8 query_buf[RMI_F11_QUERY_SIZE];
794         bool has_query36 = false;
795
796         rc = rmi_read_block(rmi_dev, query_base_addr, query_buf,
797                                 RMI_F11_QUERY_SIZE);
798         if (rc < 0)
799                 return rc;
800
801         sensor_query->nr_fingers = query_buf[0] & RMI_F11_NR_FINGERS_MASK;
802         sensor_query->has_rel = !!(query_buf[0] & RMI_F11_HAS_REL);
803         sensor_query->has_abs = !!(query_buf[0] & RMI_F11_HAS_ABS);
804         sensor_query->has_gestures = !!(query_buf[0] & RMI_F11_HAS_GESTURES);
805         sensor_query->has_sensitivity_adjust =
806                 !!(query_buf[0] & RMI_F11_HAS_SENSITIVITY_ADJ);
807         sensor_query->configurable = !!(query_buf[0] & RMI_F11_CONFIGURABLE);
808
809         sensor_query->nr_x_electrodes =
810                                 query_buf[1] & RMI_F11_NR_ELECTRODES_MASK;
811         sensor_query->nr_y_electrodes =
812                                 query_buf[2] & RMI_F11_NR_ELECTRODES_MASK;
813         sensor_query->max_electrodes =
814                                 query_buf[3] & RMI_F11_NR_ELECTRODES_MASK;
815
816         query_size = RMI_F11_QUERY_SIZE;
817
818         if (sensor_query->has_abs) {
819                 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
820                 if (rc < 0)
821                         return rc;
822
823                 sensor_query->abs_data_size =
824                         query_buf[0] & RMI_F11_ABS_DATA_SIZE_MASK;
825                 sensor_query->has_anchored_finger =
826                         !!(query_buf[0] & RMI_F11_HAS_ANCHORED_FINGER);
827                 sensor_query->has_adj_hyst =
828                         !!(query_buf[0] & RMI_F11_HAS_ADJ_HYST);
829                 sensor_query->has_dribble =
830                         !!(query_buf[0] & RMI_F11_HAS_DRIBBLE);
831                 sensor_query->has_bending_correction =
832                         !!(query_buf[0] & RMI_F11_HAS_BENDING_CORRECTION);
833                 sensor_query->has_large_object_suppression =
834                         !!(query_buf[0] & RMI_F11_HAS_LARGE_OBJECT_SUPPRESSION);
835                 sensor_query->has_jitter_filter =
836                         !!(query_buf[0] & RMI_F11_HAS_JITTER_FILTER);
837                 query_size++;
838         }
839
840         if (sensor_query->has_rel) {
841                 rc = rmi_read(rmi_dev, query_base_addr + query_size,
842                                         &sensor_query->f11_2d_query6);
843                 if (rc < 0)
844                         return rc;
845                 query_size++;
846         }
847
848         if (sensor_query->has_gestures) {
849                 rc = rmi_read_block(rmi_dev, query_base_addr + query_size,
850                                         query_buf, RMI_F11_QUERY_GESTURE_SIZE);
851                 if (rc < 0)
852                         return rc;
853
854                 sensor_query->has_single_tap =
855                         !!(query_buf[0] & RMI_F11_HAS_SINGLE_TAP);
856                 sensor_query->has_tap_n_hold =
857                         !!(query_buf[0] & RMI_F11_HAS_TAP_AND_HOLD);
858                 sensor_query->has_double_tap =
859                         !!(query_buf[0] & RMI_F11_HAS_DOUBLE_TAP);
860                 sensor_query->has_early_tap =
861                         !!(query_buf[0] & RMI_F11_HAS_EARLY_TAP);
862                 sensor_query->has_flick =
863                         !!(query_buf[0] & RMI_F11_HAS_FLICK);
864                 sensor_query->has_press =
865                         !!(query_buf[0] & RMI_F11_HAS_PRESS);
866                 sensor_query->has_pinch =
867                         !!(query_buf[0] & RMI_F11_HAS_PINCH);
868                 sensor_query->has_chiral =
869                         !!(query_buf[0] & RMI_F11_HAS_CHIRAL);
870
871                 /* query 8 */
872                 sensor_query->has_palm_det =
873                         !!(query_buf[1] & RMI_F11_HAS_PALM_DET);
874                 sensor_query->has_rotate =
875                         !!(query_buf[1] & RMI_F11_HAS_ROTATE);
876                 sensor_query->has_touch_shapes =
877                         !!(query_buf[1] & RMI_F11_HAS_TOUCH_SHAPES);
878                 sensor_query->has_scroll_zones =
879                         !!(query_buf[1] & RMI_F11_HAS_SCROLL_ZONES);
880                 sensor_query->has_individual_scroll_zones =
881                         !!(query_buf[1] & RMI_F11_HAS_INDIVIDUAL_SCROLL_ZONES);
882                 sensor_query->has_mf_scroll =
883                         !!(query_buf[1] & RMI_F11_HAS_MF_SCROLL);
884                 sensor_query->has_mf_edge_motion =
885                         !!(query_buf[1] & RMI_F11_HAS_MF_EDGE_MOTION);
886                 sensor_query->has_mf_scroll_inertia =
887                         !!(query_buf[1] & RMI_F11_HAS_MF_SCROLL_INERTIA);
888
889                 sensor_query->query7_nonzero = !!(query_buf[0]);
890                 sensor_query->query8_nonzero = !!(query_buf[1]);
891
892                 query_size += 2;
893         }
894
895         if (f11->has_query9) {
896                 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
897                 if (rc < 0)
898                         return rc;
899
900                 sensor_query->has_pen =
901                         !!(query_buf[0] & RMI_F11_HAS_PEN);
902                 sensor_query->has_proximity =
903                         !!(query_buf[0] & RMI_F11_HAS_PROXIMITY);
904                 sensor_query->has_palm_det_sensitivity =
905                         !!(query_buf[0] & RMI_F11_HAS_PALM_DET_SENSITIVITY);
906                 sensor_query->has_suppress_on_palm_detect =
907                         !!(query_buf[0] & RMI_F11_HAS_SUPPRESS_ON_PALM_DETECT);
908                 sensor_query->has_two_pen_thresholds =
909                         !!(query_buf[0] & RMI_F11_HAS_TWO_PEN_THRESHOLDS);
910                 sensor_query->has_contact_geometry =
911                         !!(query_buf[0] & RMI_F11_HAS_CONTACT_GEOMETRY);
912                 sensor_query->has_pen_hover_discrimination =
913                         !!(query_buf[0] & RMI_F11_HAS_PEN_HOVER_DISCRIMINATION);
914                 sensor_query->has_pen_filters =
915                         !!(query_buf[0] & RMI_F11_HAS_PEN_FILTERS);
916
917                 query_size++;
918         }
919
920         if (sensor_query->has_touch_shapes) {
921                 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
922                 if (rc < 0)
923                         return rc;
924
925                 sensor_query->nr_touch_shapes = query_buf[0] &
926                                 RMI_F11_NR_TOUCH_SHAPES_MASK;
927
928                 query_size++;
929         }
930
931         if (f11->has_query11) {
932                 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
933                 if (rc < 0)
934                         return rc;
935
936                 sensor_query->has_z_tuning =
937                         !!(query_buf[0] & RMI_F11_HAS_Z_TUNING);
938                 sensor_query->has_algorithm_selection =
939                         !!(query_buf[0] & RMI_F11_HAS_ALGORITHM_SELECTION);
940                 sensor_query->has_w_tuning =
941                         !!(query_buf[0] & RMI_F11_HAS_W_TUNING);
942                 sensor_query->has_pitch_info =
943                         !!(query_buf[0] & RMI_F11_HAS_PITCH_INFO);
944                 sensor_query->has_finger_size =
945                         !!(query_buf[0] & RMI_F11_HAS_FINGER_SIZE);
946                 sensor_query->has_segmentation_aggressiveness =
947                         !!(query_buf[0] &
948                                 RMI_F11_HAS_SEGMENTATION_AGGRESSIVENESS);
949                 sensor_query->has_XY_clip =
950                         !!(query_buf[0] & RMI_F11_HAS_XY_CLIP);
951                 sensor_query->has_drumming_filter =
952                         !!(query_buf[0] & RMI_F11_HAS_DRUMMING_FILTER);
953
954                 query_size++;
955         }
956
957         if (f11->has_query12) {
958                 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
959                 if (rc < 0)
960                         return rc;
961
962                 sensor_query->has_gapless_finger =
963                         !!(query_buf[0] & RMI_F11_HAS_GAPLESS_FINGER);
964                 sensor_query->has_gapless_finger_tuning =
965                         !!(query_buf[0] & RMI_F11_HAS_GAPLESS_FINGER_TUNING);
966                 sensor_query->has_8bit_w =
967                         !!(query_buf[0] & RMI_F11_HAS_8BIT_W);
968                 sensor_query->has_adjustable_mapping =
969                         !!(query_buf[0] & RMI_F11_HAS_ADJUSTABLE_MAPPING);
970                 sensor_query->has_info2 =
971                         !!(query_buf[0] & RMI_F11_HAS_INFO2);
972                 sensor_query->has_physical_props =
973                         !!(query_buf[0] & RMI_F11_HAS_PHYSICAL_PROPS);
974                 sensor_query->has_finger_limit =
975                         !!(query_buf[0] & RMI_F11_HAS_FINGER_LIMIT);
976                 sensor_query->has_linear_coeff_2 =
977                         !!(query_buf[0] & RMI_F11_HAS_LINEAR_COEFF);
978
979                 query_size++;
980         }
981
982         if (sensor_query->has_jitter_filter) {
983                 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
984                 if (rc < 0)
985                         return rc;
986
987                 sensor_query->jitter_window_size = query_buf[0] &
988                         RMI_F11_JITTER_WINDOW_MASK;
989                 sensor_query->jitter_filter_type = (query_buf[0] &
990                         RMI_F11_JITTER_FILTER_MASK) >>
991                         RMI_F11_JITTER_FILTER_SHIFT;
992
993                 query_size++;
994         }
995
996         if (sensor_query->has_info2) {
997                 rc = rmi_read(rmi_dev, query_base_addr + query_size, query_buf);
998                 if (rc < 0)
999                         return rc;
1000
1001                 sensor_query->light_control =
1002                         query_buf[0] & RMI_F11_LIGHT_CONTROL_MASK;
1003                 sensor_query->is_clear =
1004                         !!(query_buf[0] & RMI_F11_IS_CLEAR);
1005                 sensor_query->clickpad_props =
1006                         (query_buf[0] & RMI_F11_CLICKPAD_PROPS_MASK) >>
1007                         RMI_F11_CLICKPAD_PROPS_SHIFT;
1008                 sensor_query->mouse_buttons =
1009                         (query_buf[0] & RMI_F11_MOUSE_BUTTONS_MASK) >>
1010                         RMI_F11_MOUSE_BUTTONS_SHIFT;
1011                 sensor_query->has_advanced_gestures =
1012                         !!(query_buf[0] & RMI_F11_HAS_ADVANCED_GESTURES);
1013
1014                 query_size++;
1015         }
1016
1017         if (sensor_query->has_physical_props) {
1018                 rc = rmi_read_block(rmi_dev, query_base_addr
1019                         + query_size, query_buf, 4);
1020                 if (rc < 0)
1021                         return rc;
1022
1023                 sensor_query->x_sensor_size_mm =
1024                         (query_buf[0] | (query_buf[1] << 8)) / 10;
1025                 sensor_query->y_sensor_size_mm =
1026                         (query_buf[2] | (query_buf[3] << 8)) / 10;
1027
1028                 /*
1029                  * query 15 - 18 contain the size of the sensor
1030                  * and query 19 - 26 contain bezel dimensions
1031                  */
1032                 query_size += 12;
1033         }
1034
1035         if (f11->has_query27)
1036                 ++query_size;
1037
1038         if (f11->has_query28) {
1039                 rc = rmi_read(rmi_dev, query_base_addr + query_size,
1040                                 query_buf);
1041                 if (rc < 0)
1042                         return rc;
1043
1044                 has_query36 = !!(query_buf[0] & BIT(6));
1045         }
1046
1047         if (has_query36) {
1048                 query_size += 2;
1049                 rc = rmi_read(rmi_dev, query_base_addr + query_size,
1050                                 query_buf);
1051                 if (rc < 0)
1052                         return rc;
1053
1054                 if (!!(query_buf[0] & BIT(5)))
1055                         f11->has_acm = true;
1056         }
1057
1058         return query_size;
1059 }
1060
1061 static int rmi_f11_initialize(struct rmi_function *fn)
1062 {
1063         struct rmi_device *rmi_dev = fn->rmi_dev;
1064         struct f11_data *f11;
1065         struct f11_2d_ctrl *ctrl;
1066         u8 query_offset;
1067         u16 query_base_addr;
1068         u16 control_base_addr;
1069         u16 max_x_pos, max_y_pos;
1070         int rc;
1071         const struct rmi_device_platform_data *pdata =
1072                                 rmi_get_platform_data(rmi_dev);
1073         struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
1074         struct rmi_2d_sensor *sensor;
1075         u8 buf;
1076         int mask_size;
1077
1078         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Initializing F11 values.\n");
1079
1080         mask_size = BITS_TO_LONGS(drvdata->irq_count) * sizeof(unsigned long);
1081
1082         /*
1083         ** init instance data, fill in values and create any sysfs files
1084         */
1085         f11 = devm_kzalloc(&fn->dev, sizeof(struct f11_data) + mask_size * 2,
1086                         GFP_KERNEL);
1087         if (!f11)
1088                 return -ENOMEM;
1089
1090         if (fn->dev.of_node) {
1091                 rc = rmi_2d_sensor_of_probe(&fn->dev, &f11->sensor_pdata);
1092                 if (rc)
1093                         return rc;
1094         } else {
1095                 f11->sensor_pdata = pdata->sensor_pdata;
1096         }
1097
1098         f11->rezero_wait_ms = f11->sensor_pdata.rezero_wait;
1099
1100         f11->abs_mask = (unsigned long *)((char *)f11
1101                         + sizeof(struct f11_data));
1102         f11->rel_mask = (unsigned long *)((char *)f11
1103                         + sizeof(struct f11_data) + mask_size);
1104
1105         set_bit(fn->irq_pos, f11->abs_mask);
1106         set_bit(fn->irq_pos + 1, f11->rel_mask);
1107
1108         query_base_addr = fn->fd.query_base_addr;
1109         control_base_addr = fn->fd.control_base_addr;
1110
1111         rc = rmi_read(rmi_dev, query_base_addr, &buf);
1112         if (rc < 0)
1113                 return rc;
1114
1115         f11->has_query9 = !!(buf & RMI_F11_HAS_QUERY9);
1116         f11->has_query11 = !!(buf & RMI_F11_HAS_QUERY11);
1117         f11->has_query12 = !!(buf & RMI_F11_HAS_QUERY12);
1118         f11->has_query27 = !!(buf & RMI_F11_HAS_QUERY27);
1119         f11->has_query28 = !!(buf & RMI_F11_HAS_QUERY28);
1120
1121         query_offset = (query_base_addr + 1);
1122         sensor = &f11->sensor;
1123         sensor->fn = fn;
1124
1125         rc = rmi_f11_get_query_parameters(rmi_dev, f11,
1126                         &f11->sens_query, query_offset);
1127         if (rc < 0)
1128                 return rc;
1129         query_offset += rc;
1130
1131         rc = f11_read_control_regs(fn, &f11->dev_controls,
1132                         control_base_addr);
1133         if (rc < 0) {
1134                 dev_err(&fn->dev,
1135                         "Failed to read F11 control params.\n");
1136                 return rc;
1137         }
1138
1139         if (f11->sens_query.has_info2) {
1140                 if (f11->sens_query.is_clear)
1141                         f11->sensor.sensor_type = rmi_sensor_touchscreen;
1142                 else
1143                         f11->sensor.sensor_type = rmi_sensor_touchpad;
1144         }
1145
1146         sensor->report_abs = f11->sens_query.has_abs;
1147
1148         sensor->axis_align =
1149                 f11->sensor_pdata.axis_align;
1150
1151         sensor->topbuttonpad = f11->sensor_pdata.topbuttonpad;
1152         sensor->kernel_tracking = f11->sensor_pdata.kernel_tracking;
1153         sensor->dmax = f11->sensor_pdata.dmax;
1154         sensor->dribble = f11->sensor_pdata.dribble;
1155         sensor->palm_detect = f11->sensor_pdata.palm_detect;
1156
1157         if (f11->sens_query.has_physical_props) {
1158                 sensor->x_mm = f11->sens_query.x_sensor_size_mm;
1159                 sensor->y_mm = f11->sens_query.y_sensor_size_mm;
1160         } else {
1161                 sensor->x_mm = f11->sensor_pdata.x_mm;
1162                 sensor->y_mm = f11->sensor_pdata.y_mm;
1163         }
1164
1165         if (sensor->sensor_type == rmi_sensor_default)
1166                 sensor->sensor_type =
1167                         f11->sensor_pdata.sensor_type;
1168
1169         sensor->report_abs = sensor->report_abs
1170                 && !(f11->sensor_pdata.disable_report_mask
1171                         & RMI_F11_DISABLE_ABS_REPORT);
1172
1173         if (!sensor->report_abs)
1174                 /*
1175                  * If device doesn't have abs or if it has been disables
1176                  * fallback to reporting rel data.
1177                  */
1178                 sensor->report_rel = f11->sens_query.has_rel;
1179
1180         rc = rmi_read_block(rmi_dev,
1181                 control_base_addr + F11_CTRL_SENSOR_MAX_X_POS_OFFSET,
1182                 (u8 *)&max_x_pos, sizeof(max_x_pos));
1183         if (rc < 0)
1184                 return rc;
1185
1186         rc = rmi_read_block(rmi_dev,
1187                 control_base_addr + F11_CTRL_SENSOR_MAX_Y_POS_OFFSET,
1188                 (u8 *)&max_y_pos, sizeof(max_y_pos));
1189         if (rc < 0)
1190                 return rc;
1191
1192         sensor->max_x = max_x_pos;
1193         sensor->max_y = max_y_pos;
1194
1195         rc = f11_2d_construct_data(f11);
1196         if (rc < 0)
1197                 return rc;
1198
1199         if (f11->has_acm)
1200                 f11->sensor.attn_size += f11->sensor.nbr_fingers * 2;
1201
1202         /* allocate the in-kernel tracking buffers */
1203         sensor->tracking_pos = devm_kcalloc(&fn->dev,
1204                         sensor->nbr_fingers, sizeof(struct input_mt_pos),
1205                         GFP_KERNEL);
1206         sensor->tracking_slots = devm_kcalloc(&fn->dev,
1207                         sensor->nbr_fingers, sizeof(int), GFP_KERNEL);
1208         sensor->objs = devm_kcalloc(&fn->dev,
1209                         sensor->nbr_fingers,
1210                         sizeof(struct rmi_2d_sensor_abs_object),
1211                         GFP_KERNEL);
1212         if (!sensor->tracking_pos || !sensor->tracking_slots || !sensor->objs)
1213                 return -ENOMEM;
1214
1215         ctrl = &f11->dev_controls;
1216         if (sensor->axis_align.delta_x_threshold)
1217                 ctrl->ctrl0_11[RMI_F11_DELTA_X_THRESHOLD] =
1218                         sensor->axis_align.delta_x_threshold;
1219
1220         if (sensor->axis_align.delta_y_threshold)
1221                 ctrl->ctrl0_11[RMI_F11_DELTA_Y_THRESHOLD] =
1222                         sensor->axis_align.delta_y_threshold;
1223
1224         /*
1225          * If distance threshold values are set, switch to reduced reporting
1226          * mode so they actually get used by the controller.
1227          */
1228         if (sensor->axis_align.delta_x_threshold ||
1229             sensor->axis_align.delta_y_threshold) {
1230                 ctrl->ctrl0_11[0] &= ~RMI_F11_REPORT_MODE_MASK;
1231                 ctrl->ctrl0_11[0] |= RMI_F11_REPORT_MODE_REDUCED;
1232         }
1233
1234         if (f11->sens_query.has_dribble) {
1235                 switch (sensor->dribble) {
1236                 case RMI_REG_STATE_OFF:
1237                         ctrl->ctrl0_11[0] &= ~BIT(6);
1238                         break;
1239                 case RMI_REG_STATE_ON:
1240                         ctrl->ctrl0_11[0] |= BIT(6);
1241                         break;
1242                 case RMI_REG_STATE_DEFAULT:
1243                 default:
1244                         break;
1245                 }
1246         }
1247
1248         if (f11->sens_query.has_palm_det) {
1249                 switch (sensor->palm_detect) {
1250                 case RMI_REG_STATE_OFF:
1251                         ctrl->ctrl0_11[11] &= ~BIT(0);
1252                         break;
1253                 case RMI_REG_STATE_ON:
1254                         ctrl->ctrl0_11[11] |= BIT(0);
1255                         break;
1256                 case RMI_REG_STATE_DEFAULT:
1257                 default:
1258                         break;
1259                 }
1260         }
1261
1262         rc = f11_write_control_regs(fn, &f11->sens_query,
1263                            &f11->dev_controls, fn->fd.control_base_addr);
1264         if (rc)
1265                 dev_warn(&fn->dev, "Failed to write control registers\n");
1266
1267         mutex_init(&f11->dev_controls_mutex);
1268
1269         dev_set_drvdata(&fn->dev, f11);
1270
1271         return 0;
1272 }
1273
1274 static int rmi_f11_config(struct rmi_function *fn)
1275 {
1276         struct f11_data *f11 = dev_get_drvdata(&fn->dev);
1277         struct rmi_driver *drv = fn->rmi_dev->driver;
1278         struct rmi_2d_sensor *sensor = &f11->sensor;
1279         int rc;
1280
1281         if (!sensor->report_abs)
1282                 drv->clear_irq_bits(fn->rmi_dev, f11->abs_mask);
1283         else
1284                 drv->set_irq_bits(fn->rmi_dev, f11->abs_mask);
1285
1286         if (!sensor->report_rel)
1287                 drv->clear_irq_bits(fn->rmi_dev, f11->rel_mask);
1288         else
1289                 drv->set_irq_bits(fn->rmi_dev, f11->rel_mask);
1290
1291         rc = f11_write_control_regs(fn, &f11->sens_query,
1292                            &f11->dev_controls, fn->fd.query_base_addr);
1293         if (rc < 0)
1294                 return rc;
1295
1296         return 0;
1297 }
1298
1299 static irqreturn_t rmi_f11_attention(int irq, void *ctx)
1300 {
1301         struct rmi_function *fn = ctx;
1302         struct rmi_device *rmi_dev = fn->rmi_dev;
1303         struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
1304         struct f11_data *f11 = dev_get_drvdata(&fn->dev);
1305         u16 data_base_addr = fn->fd.data_base_addr;
1306         int error;
1307         int valid_bytes = f11->sensor.pkt_size;
1308
1309         if (drvdata->attn_data.data) {
1310                 /*
1311                  * The valid data in the attention report is less then
1312                  * expected. Only process the complete fingers.
1313                  */
1314                 if (f11->sensor.attn_size > drvdata->attn_data.size)
1315                         valid_bytes = drvdata->attn_data.size;
1316                 else
1317                         valid_bytes = f11->sensor.attn_size;
1318                 memcpy(f11->sensor.data_pkt, drvdata->attn_data.data,
1319                         valid_bytes);
1320                 drvdata->attn_data.data += valid_bytes;
1321                 drvdata->attn_data.size -= valid_bytes;
1322         } else {
1323                 error = rmi_read_block(rmi_dev,
1324                                 data_base_addr, f11->sensor.data_pkt,
1325                                 f11->sensor.pkt_size);
1326                 if (error < 0)
1327                         return IRQ_RETVAL(error);
1328         }
1329
1330         rmi_f11_finger_handler(f11, &f11->sensor, valid_bytes);
1331
1332         return IRQ_HANDLED;
1333 }
1334
1335 static int rmi_f11_resume(struct rmi_function *fn)
1336 {
1337         struct f11_data *f11 = dev_get_drvdata(&fn->dev);
1338         int error;
1339
1340         rmi_dbg(RMI_DEBUG_FN, &fn->dev, "Resuming...\n");
1341         if (!f11->rezero_wait_ms)
1342                 return 0;
1343
1344         mdelay(f11->rezero_wait_ms);
1345
1346         error = rmi_write(fn->rmi_dev, fn->fd.command_base_addr,
1347                                 RMI_F11_REZERO);
1348         if (error) {
1349                 dev_err(&fn->dev,
1350                         "%s: failed to issue rezero command, error = %d.",
1351                         __func__, error);
1352                 return error;
1353         }
1354
1355         return 0;
1356 }
1357
1358 static int rmi_f11_probe(struct rmi_function *fn)
1359 {
1360         int error;
1361         struct f11_data *f11;
1362
1363         error = rmi_f11_initialize(fn);
1364         if (error)
1365                 return error;
1366
1367         f11 = dev_get_drvdata(&fn->dev);
1368         error = rmi_2d_sensor_configure_input(fn, &f11->sensor);
1369         if (error)
1370                 return error;
1371
1372         return 0;
1373 }
1374
1375 struct rmi_function_handler rmi_f11_handler = {
1376         .driver = {
1377                 .name   = "rmi4_f11",
1378         },
1379         .func           = 0x11,
1380         .probe          = rmi_f11_probe,
1381         .config         = rmi_f11_config,
1382         .attention      = rmi_f11_attention,
1383         .resume         = rmi_f11_resume,
1384 };