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