ts: rk29_i2c_goodix: goodix_init_panel retry write cfg info when error
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / rmi4 / rmi_f11.c
1 /*
2  * Copyright (c) 2011 Synaptics Incorporated
3  * Copyright (c) 2011 Unixphere
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/input.h>
23 #include <linux/input/mt.h>
24 #include <linux/rmi.h>
25 #include "rmi_driver.h"
26 #define RESUME_REZERO (1 && defined(CONFIG_PM))
27 #if RESUME_REZERO
28 #include <linux/delay.h>
29 #define DEFAULT_REZERO_WAIT_MS  40
30 #endif
31
32 #ifndef MT_TOOL_MAX
33 #define MT_TOOL_MAX MT_TOOL_PEN
34 #endif
35
36 #define F11_MAX_NUM_OF_SENSORS          8
37 #define F11_MAX_NUM_OF_FINGERS          10
38 #define F11_MAX_NUM_OF_TOUCH_SHAPES     16
39
40 #define F11_REL_POS_MIN         -128
41 #define F11_REL_POS_MAX         127
42
43 #define FINGER_STATE_MASK       0x03
44 #define GET_FINGER_STATE(f_states, i) \
45         ((f_states[i / 4] >> (2 * (i % 4))) & FINGER_STATE_MASK)
46
47 #define F11_CTRL_SENSOR_MAX_X_POS_OFFSET        6
48 #define F11_CTRL_SENSOR_MAX_Y_POS_OFFSET        8
49
50 #define F11_CEIL(x, y) (((x) + ((y)-1)) / (y))
51 #define INBOX(x, y, box) (x >= box.x && x < (box.x + box.width) \
52                         && y >= box.y && y < (box.y + box.height))
53
54 #define DEFAULT_XY_MAX 9999
55 #define DEFAULT_MAX_ABS_MT_PRESSURE 255
56 #define DEFAULT_MAX_ABS_MT_TOUCH 15
57 #define DEFAULT_MAX_ABS_MT_ORIENTATION 1
58 #define DEFAULT_MIN_ABS_MT_TRACKING_ID 1
59 #define DEFAULT_MAX_ABS_MT_TRACKING_ID 10
60 #define MAX_NAME_LENGTH 256
61
62 #define SYNAPTICS_MAX_POINTS  (10)
63 static ssize_t f11_flip_show(struct device *dev,
64                                    struct device_attribute *attr, char *buf);
65
66 static ssize_t f11_flip_store(struct device *dev,
67                                     struct device_attribute *attr,
68                                     const char *buf, size_t count);
69
70 static ssize_t f11_clip_show(struct device *dev,
71                                    struct device_attribute *attr, char *buf);
72
73 static ssize_t f11_clip_store(struct device *dev,
74                                     struct device_attribute *attr,
75                                     const char *buf, size_t count);
76
77 static ssize_t f11_offset_show(struct device *dev,
78                                      struct device_attribute *attr, char *buf);
79
80 static ssize_t f11_offset_store(struct device *dev,
81                                       struct device_attribute *attr,
82                                       const char *buf, size_t count);
83
84 static ssize_t f11_swap_show(struct device *dev,
85                                    struct device_attribute *attr, char *buf);
86
87 static ssize_t f11_swap_store(struct device *dev,
88                                     struct device_attribute *attr,
89                                     const char *buf, size_t count);
90
91 static ssize_t f11_relreport_show(struct device *dev,
92                                         struct device_attribute *attr,
93                                         char *buf);
94
95 static ssize_t f11_relreport_store(struct device *dev,
96                                          struct device_attribute *attr,
97                                          const char *buf, size_t count);
98
99 static ssize_t f11_maxPos_show(struct device *dev,
100                                      struct device_attribute *attr, char *buf);
101
102 static ssize_t f11_rezero_store(struct device *dev,
103                                          struct device_attribute *attr,
104                                          const char *buf, size_t count);
105
106 #if RESUME_REZERO
107 static ssize_t f11_rezeroOnResume_show(struct device *dev,
108                                         struct device_attribute *attr,
109                                         char *buf);
110
111 static ssize_t f11_rezeroOnResume_store(struct device *dev,
112                                          struct device_attribute *attr,
113                                          const char *buf, size_t count);
114 static ssize_t f11_rezeroWait_show(struct device *dev,
115                                         struct device_attribute *attr,
116                                         char *buf);
117
118 static ssize_t f11_rezeroWait_store(struct device *dev,
119                                          struct device_attribute *attr,
120                                          const char *buf, size_t count);
121 #endif
122
123
124 static void rmi_f11_free_memory(struct rmi_function_container *fc);
125
126 static int rmi_f11_initialize(struct rmi_function_container *fc);
127
128 static int rmi_f11_create_sysfs(struct rmi_function_container *fc);
129
130 static int rmi_f11_config(struct rmi_function_container *fc);
131
132 static int rmi_f11_reset(struct rmi_function_container *fc);
133
134 static int rmi_f11_register_devices(struct rmi_function_container *fc);
135
136 static void rmi_f11_free_devices(struct rmi_function_container *fc);
137
138
139 static struct device_attribute attrs[] = {
140         __ATTR(flip, RMI_RW_ATTR, f11_flip_show, f11_flip_store),
141         __ATTR(clip, RMI_RW_ATTR, f11_clip_show, f11_clip_store),
142         __ATTR(offset, RMI_RW_ATTR, f11_offset_show, f11_offset_store),
143         __ATTR(swap, RMI_RW_ATTR, f11_swap_show, f11_swap_store),
144         __ATTR(relreport, RMI_RW_ATTR, f11_relreport_show, f11_relreport_store),
145         __ATTR(maxPos, RMI_RO_ATTR, f11_maxPos_show, rmi_store_error),
146 #if RESUME_REZERO
147         __ATTR(rezeroOnResume, RMI_RW_ATTR, f11_rezeroOnResume_show,
148                 f11_rezeroOnResume_store),
149         __ATTR(rezeroWait, RMI_RW_ATTR, f11_rezeroWait_show,
150                 f11_rezeroWait_store),
151 #endif
152         __ATTR(rezero, RMI_WO_ATTR, rmi_show_error, f11_rezero_store)
153 };
154
155
156 union f11_2d_commands {
157         struct {
158                 u8 rezero:1;
159         };
160         u8 reg;
161 };
162
163 struct f11_2d_device_query {
164         union {
165                 struct {
166                         u8 nbr_of_sensors:3;
167                         u8 has_query9:1;
168                         u8 has_query11:1;
169                 };
170                 u8 f11_2d_query0;
171         };
172
173         union {
174                 struct {
175                         u8 has_z_tuning:1;
176                         u8 has_pos_interpolation_tuning:1;
177                         u8 has_w_tuning:1;
178                         u8 has_pitch_info:1;
179                         u8 has_default_finger_width:1;
180                         u8 has_segmentation_aggressiveness:1;
181                         u8 has_tx_rw_clip:1;
182                         u8 has_drumming_correction:1;
183                 };
184                 u8 f11_2d_query11;
185         };
186 };
187
188 union f11_2d_query9 {
189         struct {
190                 u8 has_pen:1;
191                 u8 has_proximity:1;
192                 u8 has_palm_det_sensitivity:1;
193                 u8 has_suppress_on_palm_detect:1;
194                 u8 has_two_pen_thresholds:1;
195                 u8 has_contact_geometry:1;
196         };
197         u8 reg;
198 };
199
200 struct f11_2d_sensor_query {
201         union {
202                 struct {
203                         /* query1 */
204                         u8 number_of_fingers:3;
205                         u8 has_rel:1;
206                         u8 has_abs:1;
207                         u8 has_gestures:1;
208                         u8 has_sensitivity_adjust:1;
209                         u8 configurable:1;
210                         /* query2 */
211                         u8 num_of_x_electrodes:7;
212                         /* query3 */
213                         u8 num_of_y_electrodes:7;
214                         /* query4 */
215                         u8 max_electrodes:7;
216                 };
217                 u8 f11_2d_query1__4[4];
218         };
219
220         union {
221                 struct {
222                         u8 abs_data_size:3;
223                         u8 has_anchored_finger:1;
224                         u8 has_adj_hyst:1;
225                         u8 has_dribble:1;
226                 };
227                 u8 f11_2d_query5;
228         };
229
230         u8 f11_2d_query6;
231
232         union {
233                 struct {
234                         u8 has_single_tap:1;
235                         u8 has_tap_n_hold:1;
236                         u8 has_double_tap:1;
237                         u8 has_early_tap:1;
238                         u8 has_flick:1;
239                         u8 has_press:1;
240                         u8 has_pinch:1;
241                         u8 padding:1;
242
243                         u8 has_palm_det:1;
244                         u8 has_rotate:1;
245                         u8 has_touch_shapes:1;
246                         u8 has_scroll_zones:1;
247                         u8 has_individual_scroll_zones:1;
248                         u8 has_multi_finger_scroll:1;
249                 };
250                 u8 f11_2d_query7__8[2];
251         };
252
253         union f11_2d_query9 query9;
254
255         union {
256                 struct {
257                         u8 nbr_touch_shapes:5;
258                 };
259                 u8 f11_2d_query10;
260         };
261 };
262
263 union f11_2d_ctrl10 {
264         struct {
265                 u8 single_tap_int_enable:1;
266                 u8 tap_n_hold_int_enable:1;
267                 u8 double_tap_int_enable:1;
268                 u8 early_tap_int_enable:1;
269                 u8 flick_int_enable:1;
270                 u8 press_int_enable:1;
271                 u8 pinch_int_enable:1;
272         };
273         u8 reg;
274 };
275
276 union f11_2d_ctrl11 {
277         struct {
278                 u8 palm_detect_int_enable:1;
279                 u8 rotate_int_enable:1;
280                 u8 touch_shape_int_enable:1;
281                 u8 scroll_zone_int_enable:1;
282                 u8 multi_finger_scroll_int_enable:1;
283         };
284         u8 reg;
285 };
286
287 union f11_2d_ctrl12 {
288         struct {
289                 u8 sensor_map:7;
290                 u8 xy_sel:1;
291         };
292         u8 reg;
293 };
294
295 union f11_2d_ctrl14 {
296         struct {
297                 u8 sens_adjustment:5;
298                 u8 hyst_adjustment:3;
299         };
300         u8 reg;
301 };
302
303 union f11_2d_ctrl15 {
304         struct {
305                 u8 max_tap_time:8;
306         };
307         u8 reg;
308 };
309
310 union f11_2d_ctrl16 {
311         struct {
312                 u8 min_press_time:8;
313         };
314         u8 reg;
315 };
316
317 union f11_2d_ctrl17 {
318         struct {
319                 u8 max_tap_distance:8;
320         };
321         u8 reg;
322 };
323
324 union f11_2d_ctrl18_19 {
325         struct {
326                 u8 min_flick_distance:8;
327                 u8 min_flick_speed:8;
328         };
329         u8 reg[2];
330 };
331
332 union f11_2d_ctrl20_21 {
333         struct {
334                 u8 pen_detect_enable:1;
335                 u8 pen_jitter_filter_enable:1;
336                 u8 ctrl20_reserved:6;
337                 u8 pen_z_threshold:8;
338         };
339         u8 reg[2];
340 };
341
342 struct  f11_2d_ctrl {
343
344         union {
345                 struct {
346                         /* F11_2D_Ctrl0 */
347                         u8 reporting_mode:3;
348                         u8 abs_pos_filt:1;
349                         u8 rel_pos_filt:1;
350                         u8 rel_ballistics:1;
351                         u8 dribble:1;
352                         u8 report_beyond_clip:1;
353                         /* F11_2D_Ctrl1 */
354                         u8 palm_detect_thres:4;
355                         u8 motion_sensitivity:2;
356                         u8 man_track_en:1;
357                         u8 man_tracked_finger:1;
358                         /* F11_2D_Ctrl2 and 3 */
359                         u8 delta_x_threshold:8;
360                         u8 delta_y_threshold:8;
361                         /* F11_2D_Ctrl4 and 5 */
362                         u8 velocity:8;
363                         u8 acceleration:8;
364                         /* F11_2D_Ctrl6 thru 9 */
365                         u16 sensor_max_x_pos:12;
366                         u8 ctrl7_reserved:4;
367                         u16 sensor_max_y_pos:12;
368                         u8 ctrl9_reserved:4;
369                 };
370                 u8 ctrl0_9[10];
371         };
372
373         union f11_2d_ctrl10             *ctrl10;
374         union f11_2d_ctrl11             *ctrl11;
375         union f11_2d_ctrl12             *ctrl12;
376         u8                              ctrl12_size;
377         union f11_2d_ctrl14             *ctrl14;
378         union f11_2d_ctrl15             *ctrl15;
379         union f11_2d_ctrl16             *ctrl16;
380         union f11_2d_ctrl17             *ctrl17;
381         union f11_2d_ctrl18_19          *ctrl18_19;
382         union f11_2d_ctrl20_21          *ctrl20_21;
383 };
384
385 struct f11_2d_data_1_5 {
386         u8 x_msb;
387         u8 y_msb;
388         u8 x_lsb:4;
389         u8 y_lsb:4;
390         u8 w_y:4;
391         u8 w_x:4;
392         u8 z;
393 };
394
395 struct f11_2d_data_6_7 {
396         s8 delta_x;
397         s8 delta_y;
398 };
399
400 struct f11_2d_data_8 {
401         u8 single_tap:1;
402         u8 tap_and_hold:1;
403         u8 double_tap:1;
404         u8 early_tap:1;
405         u8 flick:1;
406         u8 press:1;
407         u8 pinch:1;
408 };
409
410 struct f11_2d_data_9 {
411         u8 palm_detect:1;
412         u8 rotate:1;
413         u8 shape:1;
414         u8 scrollzone:1;
415         u8 finger_count:3;
416 };
417
418 struct f11_2d_data_10 {
419         u8 pinch_motion;
420 };
421
422 struct f11_2d_data_10_12 {
423         u8 x_flick_dist;
424         u8 y_flick_dist;
425         u8 flick_time;
426 };
427
428 struct f11_2d_data_11_12 {
429         u8 motion;
430         u8 finger_separation;
431 };
432
433 struct f11_2d_data_13 {
434         u8 shape_n;
435 };
436
437 struct f11_2d_data_14_15 {
438         u8 horizontal;
439         u8 vertical;
440 };
441
442 struct f11_2d_data_14_17 {
443         u8 x_low;
444         u8 y_right;
445         u8 x_upper;
446         u8 y_left;
447 };
448
449 struct f11_2d_data {
450         u8                              *f_state;
451         const struct f11_2d_data_1_5    *abs_pos;
452         const struct f11_2d_data_6_7    *rel_pos;
453         const struct f11_2d_data_8      *gest_1;
454         const struct f11_2d_data_9      *gest_2;
455         const struct f11_2d_data_10     *pinch;
456         const struct f11_2d_data_10_12  *flick;
457         const struct f11_2d_data_11_12  *rotate;
458         const struct f11_2d_data_13     *shapes;
459         const struct f11_2d_data_14_15  *multi_scroll;
460         const struct f11_2d_data_14_17  *scroll_zones;
461 };
462
463 struct f11_2d_sensor {
464         struct rmi_f11_2d_axis_alignment axis_align;
465         struct f11_2d_sensor_query sens_query;
466         struct f11_2d_data data;
467         u16 max_x;
468         u16 max_y;
469         u8 nbr_fingers;
470         u8 finger_tracker[F11_MAX_NUM_OF_FINGERS];
471         u8 *data_pkt;
472         int pkt_size;
473         u8 sensor_index;
474         struct rmi_f11_virtualbutton_map virtualbutton_map;
475         char input_name[MAX_NAME_LENGTH];
476         char input_phys[MAX_NAME_LENGTH];
477         struct input_dev *input;
478         struct input_dev *mouse_input;
479 };
480
481 struct f11_data {
482         struct f11_2d_device_query dev_query;
483         struct f11_2d_ctrl dev_controls;
484 #if     RESUME_REZERO
485         u16 rezero_wait_ms;
486         bool rezero_on_resume;
487 #endif
488         struct f11_2d_sensor sensors[F11_MAX_NUM_OF_SENSORS];
489 };
490
491 enum finger_state_values {
492         F11_NO_FINGER   = 0x00,
493         F11_PRESENT     = 0x01,
494         F11_INACCURATE  = 0x02,
495         F11_RESERVED    = 0x03
496 };
497
498 /** F11_INACCURATE state is overloaded to indicate pen present. */
499 #define F11_PEN F11_INACCURATE
500
501 static int get_tool_type(struct f11_2d_sensor *sensor, u8 finger_state) {
502         if (sensor->sens_query.query9.has_pen && finger_state == F11_PEN)
503                 return MT_TOOL_PEN;
504         return MT_TOOL_FINGER;
505 }
506
507 static void rmi_f11_rel_pos_report(struct f11_2d_sensor *sensor, u8 n_finger)
508 {
509         struct f11_2d_data *data = &sensor->data;
510         struct rmi_f11_2d_axis_alignment *axis_align = &sensor->axis_align;
511         s8 x, y;
512         s8 temp;
513
514         x = data->rel_pos[n_finger].delta_x;
515         y = data->rel_pos[n_finger].delta_y;
516
517         x = min(F11_REL_POS_MAX, max(F11_REL_POS_MIN, (int)x));
518         y = min(F11_REL_POS_MAX, max(F11_REL_POS_MIN, (int)y));
519
520         if (axis_align->swap_axes) {
521                 temp = x;
522                 x = y;
523                 y = temp;
524         }
525         if (axis_align->flip_x)
526                 x = min(F11_REL_POS_MAX, -x);
527         if (axis_align->flip_y)
528                 y = min(F11_REL_POS_MAX, -y);
529
530         if (x || y) {
531                 input_report_rel(sensor->input, REL_X, x);
532                 input_report_rel(sensor->input, REL_Y, y);
533                 input_report_rel(sensor->mouse_input, REL_X, x);
534                 input_report_rel(sensor->mouse_input, REL_Y, y);
535         }
536         input_sync(sensor->mouse_input);
537 }
538
539 static void rmi_f11_abs_pos_report(struct f11_2d_sensor *sensor,
540                                         u8 finger_state, u8 n_finger)
541 {
542         struct f11_2d_data *data = &sensor->data;
543         struct rmi_f11_2d_axis_alignment *axis_align = &sensor->axis_align;
544         int prev_state = sensor->finger_tracker[n_finger];
545         int x, y, z;
546         int w_x, w_y, w_max, w_min, orient;
547         int temp;
548
549         if (prev_state && !finger_state) {
550                 /* this is a release */
551                 x = y = z = w_max = w_min = orient = 0;
552                 input_mt_slot(sensor->input, n_finger);
553                 input_mt_report_slot_state(sensor->input, MT_TOOL_FINGER, false);
554                 input_report_abs(sensor->input, ABS_MT_TRACKING_ID, -1);
555         } else if (!prev_state && !finger_state) {
556                 /* nothing to report */
557                 return;
558         } else {
559                 x = ((data->abs_pos[n_finger].x_msb << 4) |
560                         data->abs_pos[n_finger].x_lsb);
561                 y = ((data->abs_pos[n_finger].y_msb << 4) |
562                         data->abs_pos[n_finger].y_lsb);
563                 z = data->abs_pos[n_finger].z;
564                 w_x = data->abs_pos[n_finger].w_x;
565                 w_y = data->abs_pos[n_finger].w_y;
566                 w_max = max(w_x, w_y);
567                 w_min = min(w_x, w_y);
568                 if (axis_align->swap_axes) {
569                         temp = x;
570                         x = y;
571                         y = temp;
572                         temp = w_x;
573                         w_x = w_y;
574                         w_y = temp;
575                 }
576
577                 orient = w_x > w_y ? 1 : 0;
578
579                 if (axis_align->flip_x)
580                         x = max(sensor->max_x - x, 0);
581
582                 if (axis_align->flip_y)
583                         y = max(sensor->max_y - y, 0);
584
585                 /*
586                 ** here checking if X offset or y offset are specified is
587                 **  redundant.  We just add the offsets or, clip the values
588                 **
589                 ** note: offsets need to be done before clipping occurs,
590                 ** or we could get funny values that are outside
591                 ** clipping boundaries.
592                 */
593                 x += axis_align->offset_X;
594                 y += axis_align->offset_Y;
595                 x =  max(axis_align->clip_X_low, x);
596                 y =  max(axis_align->clip_Y_low, y);
597                 if (axis_align->clip_X_high)
598                         x = min(axis_align->clip_X_high, x);
599                 if (axis_align->clip_Y_high)
600                         y =  min(axis_align->clip_Y_high, y);
601
602                 pr_debug("%s: f_state[%d]:%d - x:%d y:%d z:%d w_max:%d w_min:%d\n",
603                         __func__, n_finger, finger_state, x, y, z, w_max, w_min);
604 #ifndef CONFIG_RMI4_F11_PEN
605                 /* Some UIs ignore W of zero, so we fudge it to 1 for pens. */
606                 if (sensor->sens_query.query9.has_pen &&
607                                 get_tool_type(sensor, finger_state) == MT_TOOL_PEN) {
608                         w_max = max(1, w_max);
609                         w_min = max(1, w_min);
610                 }
611 #endif
612                 //printk("+finger:%d, state:%d x:%d, y:%d\n",n_finger, finger_state, x , y);
613                 input_mt_slot(sensor->input, n_finger);
614                 input_mt_report_slot_state(sensor->input, MT_TOOL_FINGER, true);
615                 input_report_abs(sensor->input, ABS_MT_TRACKING_ID, n_finger);
616                 input_report_abs(sensor->input, ABS_MT_TOUCH_MAJOR, w_max);
617                 input_report_abs(sensor->input, ABS_MT_PRESSURE, z);
618                 input_report_abs(sensor->input, ABS_MT_TOUCH_MINOR, w_min);
619                 input_report_abs(sensor->input, ABS_MT_ORIENTATION, orient);
620                 input_report_abs(sensor->input, ABS_MT_POSITION_X, x);
621                 input_report_abs(sensor->input, ABS_MT_POSITION_Y, y);
622
623 #ifdef  CONFIG_RMI4_F11_PEN
624                 if (sensor->sens_query.query9.has_pen) {
625                         input_report_abs(sensor->input, ABS_MT_TOOL_TYPE,
626                                          get_tool_type(sensor, finger_state));
627                 }
628 #endif
629
630         }
631         sensor->finger_tracker[n_finger] = finger_state;
632 }
633
634 #ifdef CONFIG_RMI4_VIRTUAL_BUTTON
635 static int rmi_f11_virtual_button_handler(struct f11_2d_sensor *sensor)
636 {
637         int i;
638         int x;
639         int y;
640         struct rmi_f11_virtualbutton_map *virtualbutton_map;
641         if (sensor->sens_query.has_gestures &&
642                 sensor->data.gest_1->single_tap) {
643                 virtualbutton_map = &sensor->virtualbutton_map;
644                 x = ((sensor->data.abs_pos[0].x_msb << 4) |
645                         sensor->data.abs_pos[0].x_lsb);
646                 y = ((sensor->data.abs_pos[0].y_msb << 4) |
647                         sensor->data.abs_pos[0].y_lsb);
648                 for (i = 0; i < virtualbutton_map->buttons; i++) {
649                         if (INBOX(x, y, virtualbutton_map->map[i])) {
650                                 input_report_key(sensor->input,
651                                         virtualbutton_map->map[i].code, 1);
652                                 input_report_key(sensor->input,
653                                         virtualbutton_map->map[i].code, 0);
654                                 input_sync(sensor->input);
655                                 return 0;
656                         }
657                 }
658         }
659         return 0;
660 }
661 #else
662 #define rmi_f11_virtual_button_handler(sensor)
663 #endif
664
665 static void rmi_f11_finger_handler(struct f11_2d_sensor *sensor)
666 {
667         const u8 *f_state = sensor->data.f_state;
668         u8 finger_state;
669         u8 finger_pressed_count;
670         u8 i;
671         for (i = 0, finger_pressed_count = 0; i < sensor->nbr_fingers; i++) {
672                 /* Possible of having 4 fingers per f_statet register */
673                 finger_state = GET_FINGER_STATE(f_state, i);
674
675                 if (finger_state == F11_RESERVED) {
676                         pr_err("%s: Invalid finger state[%d]:0x%02x.", __func__,
677                                         i, finger_state);
678                         continue;
679                 } else if (finger_state == F11_PRESENT) {
680                         finger_pressed_count++;
681                 } else if (finger_state == F11_INACCURATE) {
682                         continue;
683                 }
684
685                 if (sensor->data.abs_pos)
686                         rmi_f11_abs_pos_report(sensor, finger_state, i);
687
688                 if (sensor->data.rel_pos)
689                         rmi_f11_rel_pos_report(sensor, i);
690         }
691         input_report_key(sensor->input, BTN_TOUCH, finger_pressed_count);
692         input_sync(sensor->input);
693 }
694
695 static int f11_2d_construct_data(struct f11_2d_sensor *sensor)
696 {
697         struct f11_2d_sensor_query *query = &sensor->sens_query;
698         struct f11_2d_data *data = &sensor->data;
699         int i;
700
701         sensor->nbr_fingers = (query->number_of_fingers == 5 ? 10 :
702                                 query->number_of_fingers + 1);
703
704         sensor->pkt_size = F11_CEIL(sensor->nbr_fingers, 4);
705
706         if (query->has_abs)
707                 sensor->pkt_size += (sensor->nbr_fingers * 5);
708
709         if (query->has_rel)
710                 sensor->pkt_size +=  (sensor->nbr_fingers * 2);
711
712         /* Check if F11_2D_Query7 is non-zero */
713         if (query->f11_2d_query7__8[0])
714                 sensor->pkt_size += sizeof(u8);
715
716         /* Check if F11_2D_Query7 or F11_2D_Query8 is non-zero */
717         if (query->f11_2d_query7__8[0] || query->f11_2d_query7__8[1])
718                 sensor->pkt_size += sizeof(u8);
719
720         if (query->has_pinch || query->has_flick || query->has_rotate) {
721                 sensor->pkt_size += 3;
722                 if (!query->has_flick)
723                         sensor->pkt_size--;
724                 if (!query->has_rotate)
725                         sensor->pkt_size--;
726         }
727
728         if (query->has_touch_shapes)
729                 sensor->pkt_size += F11_CEIL(query->nbr_touch_shapes + 1, 8);
730
731         sensor->data_pkt = kzalloc(sensor->pkt_size, GFP_KERNEL);
732         if (!sensor->data_pkt)
733                 return -ENOMEM;
734
735         data->f_state = sensor->data_pkt;
736         i = F11_CEIL(sensor->nbr_fingers, 4);
737
738         if (query->has_abs) {
739                 data->abs_pos = (struct f11_2d_data_1_5 *)
740                                 &sensor->data_pkt[i];
741                 i += (sensor->nbr_fingers * 5);
742         }
743
744         if (query->has_rel) {
745                 data->rel_pos = (struct f11_2d_data_6_7 *)
746                                 &sensor->data_pkt[i];
747                 i += (sensor->nbr_fingers * 2);
748         }
749
750         if (query->f11_2d_query7__8[0]) {
751                 data->gest_1 = (struct f11_2d_data_8 *)&sensor->data_pkt[i];
752                 i++;
753         }
754
755         if (query->f11_2d_query7__8[0] || query->f11_2d_query7__8[1]) {
756                 data->gest_2 = (struct f11_2d_data_9 *)&sensor->data_pkt[i];
757                 i++;
758         }
759
760         if (query->has_pinch) {
761                 data->pinch = (struct f11_2d_data_10 *)&sensor->data_pkt[i];
762                 i++;
763         }
764
765         if (query->has_flick) {
766                 if (query->has_pinch) {
767                         data->flick = (struct f11_2d_data_10_12 *)data->pinch;
768                         i += 2;
769                 } else {
770                         data->flick = (struct f11_2d_data_10_12 *)
771                                         &sensor->data_pkt[i];
772                         i += 3;
773                 }
774         }
775
776         if (query->has_rotate) {
777                 if (query->has_flick) {
778                         data->rotate = (struct f11_2d_data_11_12 *)
779                                         (data->flick + 1);
780                 } else {
781                         data->rotate = (struct f11_2d_data_11_12 *)
782                                         &sensor->data_pkt[i];
783                         i += 2;
784                 }
785         }
786
787         if (query->has_touch_shapes)
788                 data->shapes = (struct f11_2d_data_13 *)&sensor->data_pkt[i];
789
790         return 0;
791 }
792
793 static void f11_free_control_regs(struct f11_2d_ctrl *ctrl)
794 {
795         kfree(ctrl->ctrl10);
796         kfree(ctrl->ctrl11);
797         kfree(ctrl->ctrl14);
798         kfree(ctrl->ctrl15);
799         kfree(ctrl->ctrl16);
800         kfree(ctrl->ctrl17);
801         kfree(ctrl->ctrl18_19);
802         kfree(ctrl->ctrl20_21);
803         ctrl->ctrl10 = NULL;
804         ctrl->ctrl11 = NULL;
805         ctrl->ctrl14 = NULL;
806         ctrl->ctrl15 = NULL;
807         ctrl->ctrl16 = NULL;
808         ctrl->ctrl17 = NULL;
809         ctrl->ctrl18_19 = NULL;
810         ctrl->ctrl20_21 = NULL;
811 }
812
813 static int f11_read_control_regs(struct rmi_device *rmi_dev,
814                                            struct f11_2d_ctrl *ctrl,
815                                            int ctrl_base_addr) {
816         int read_address = ctrl_base_addr;
817         int error = 0;
818
819         error = rmi_read_block(rmi_dev, read_address, ctrl->ctrl0_9,
820                 sizeof(ctrl->ctrl0_9));
821         if (error < 0) {
822                 dev_err(&rmi_dev->dev,
823                         "Failed to read F11 ctrl0, code: %d.\n", error);
824                 return error;
825         }
826         read_address = read_address + sizeof(ctrl->ctrl0_9);
827
828         if (ctrl->ctrl10) {
829                 error = rmi_read_block(rmi_dev, read_address,
830                         &ctrl->ctrl10->reg, sizeof(union f11_2d_ctrl10));
831                 if (error < 0) {
832                         dev_err(&rmi_dev->dev,
833                                 "Failed to read F11 ctrl10, code: %d.\n",
834                                 error);
835                         return error;
836                 }
837                 read_address = read_address + sizeof(union f11_2d_ctrl10);
838         }
839
840         if (ctrl->ctrl11) {
841                 error = rmi_read_block(rmi_dev, read_address,
842                         &ctrl->ctrl11->reg, sizeof(union f11_2d_ctrl11));
843                 if (error < 0) {
844                         dev_err(&rmi_dev->dev,
845                                 "Failed to read F11 ctrl11, code: %d.\n",
846                                 error);
847                         return error;
848                 }
849                 read_address = read_address + sizeof(union f11_2d_ctrl11);
850         }
851
852         if (ctrl->ctrl14) {
853                 error = rmi_read_block(rmi_dev, read_address,
854                         &ctrl->ctrl14->reg, sizeof(union f11_2d_ctrl14));
855                 if (error < 0) {
856                         dev_err(&rmi_dev->dev,
857                                 "Failed to read F11 ctrl14, code: %d.\n",
858                                 error);
859                         return error;
860                 }
861                 read_address = read_address + sizeof(union f11_2d_ctrl14);
862         }
863
864         if (ctrl->ctrl15) {
865                 error = rmi_read_block(rmi_dev, read_address,
866                         &ctrl->ctrl15->reg, sizeof(union f11_2d_ctrl15));
867                 if (error < 0) {
868                         dev_err(&rmi_dev->dev,
869                                 "Failed to read F11 ctrl15, code: %d.\n",
870                                 error);
871                         return error;
872                 }
873                 read_address = read_address + sizeof(union f11_2d_ctrl15);
874         }
875
876         if (ctrl->ctrl16) {
877                 error = rmi_read_block(rmi_dev, read_address,
878                         &ctrl->ctrl16->reg, sizeof(union f11_2d_ctrl16));
879                 if (error < 0) {
880                         dev_err(&rmi_dev->dev,
881                                 "Failed to read F11 ctrl16, code: %d.\n",
882                                 error);
883                         return error;
884                 }
885                 read_address = read_address + sizeof(union f11_2d_ctrl16);
886         }
887
888         if (ctrl->ctrl17) {
889                 error = rmi_read_block(rmi_dev, read_address,
890                         &ctrl->ctrl17->reg, sizeof(union f11_2d_ctrl17));
891                 if (error < 0) {
892                         dev_err(&rmi_dev->dev,
893                                 "Failed to read F11 ctrl17, code: %d.\n",
894                                 error);
895                         return error;
896                 }
897                 read_address = read_address + sizeof(union f11_2d_ctrl17);
898         }
899
900         if (ctrl->ctrl18_19) {
901                 error = rmi_read_block(rmi_dev, read_address,
902                         ctrl->ctrl18_19->reg, sizeof(union f11_2d_ctrl18_19));
903                 if (error < 0) {
904                         dev_err(&rmi_dev->dev,
905                                 "Failed to read F11 ctrl18_19, code: %d.\n",
906                                 error);
907                         return error;
908                 }
909                 read_address = read_address + sizeof(union f11_2d_ctrl18_19);
910         }
911
912         if (ctrl->ctrl20_21) {
913                 error = rmi_read_block(rmi_dev, read_address,
914                         ctrl->ctrl20_21->reg, sizeof(union f11_2d_ctrl20_21));
915                 if (error < 0) {
916                         dev_err(&rmi_dev->dev,
917                                 "Failed to read F11 ctrl20_21, code: %d.\n",
918                                 error);
919                         return error;
920                 }
921                 read_address = read_address + sizeof(union f11_2d_ctrl20_21);
922         }
923
924         return 0;
925 }
926
927 static int f11_allocate_control_regs(struct rmi_device *rmi_dev,
928                                 struct f11_2d_device_query *device_query,
929                                 struct f11_2d_sensor_query *sensor_query,
930                                 struct f11_2d_ctrl *ctrl,
931                                 int ctrl_base_addr) {
932         int error = 0;
933
934         if (sensor_query->f11_2d_query7__8[0]) {
935                 ctrl->ctrl10 = kzalloc(sizeof(union f11_2d_ctrl10),
936                                        GFP_KERNEL);
937                 if (!ctrl->ctrl10) {
938                         error = -ENOMEM;
939                         goto error_exit;
940                 }
941         }
942
943         if (sensor_query->f11_2d_query7__8[1]) {
944                 ctrl->ctrl11 = kzalloc(sizeof(union f11_2d_ctrl11),
945                                        GFP_KERNEL);
946                 if (!ctrl->ctrl11) {
947                         error = -ENOMEM;
948                         goto error_exit;
949                 }
950         }
951
952         if (device_query->has_query9 && sensor_query->query9.has_pen) {
953                 ctrl->ctrl20_21 = kzalloc(sizeof(union f11_2d_ctrl20_21),
954                                           GFP_KERNEL);
955                 if (!ctrl->ctrl20_21) {
956                         error = -ENOMEM;
957                         goto error_exit;
958                 }
959         }
960
961         return f11_read_control_regs(rmi_dev, ctrl, ctrl_base_addr);
962
963 error_exit:
964         f11_free_control_regs(ctrl);
965         return error;
966 }
967
968 static int f11_write_control_regs(struct rmi_device *rmi_dev,
969                                         struct f11_2d_sensor_query *query,
970                                         struct f11_2d_ctrl *ctrl,
971                                         int ctrl_base_addr)
972 {
973         int write_address = ctrl_base_addr;
974         int error;
975
976         error = rmi_write_block(rmi_dev, write_address,
977                                 ctrl->ctrl0_9, sizeof(ctrl->ctrl0_9));
978         if (error < 0)
979                 return error;
980         write_address += sizeof(ctrl->ctrl0_9);
981
982         if (ctrl->ctrl10) {
983                 error = rmi_write_block(rmi_dev, write_address,
984                                         &ctrl->ctrl10->reg, 1);
985                 if (error < 0)
986                         return error;
987                 write_address++;
988         }
989
990         if (ctrl->ctrl11) {
991                 error = rmi_write_block(rmi_dev, write_address,
992                                         &ctrl->ctrl11->reg, 1);
993                 if (error < 0)
994                         return error;
995                 write_address++;
996         }
997
998         if (ctrl->ctrl12 && ctrl->ctrl12_size && query->configurable) {
999                 if (ctrl->ctrl12_size > query->max_electrodes) {
1000                         dev_err(&rmi_dev->dev,
1001                                 "%s: invalid cfg size:%d, should be < %d.\n",
1002                                 __func__, ctrl->ctrl12_size,
1003                                 query->max_electrodes);
1004                         return -EINVAL;
1005                 }
1006                 error = rmi_write_block(rmi_dev, write_address,
1007                                                 &ctrl->ctrl12->reg,
1008                                                 ctrl->ctrl12_size);
1009                 if (error < 0)
1010                         return error;
1011                 write_address += ctrl->ctrl12_size;
1012         }
1013
1014         if (ctrl->ctrl14) {
1015                 error = rmi_write_block(rmi_dev, write_address,
1016                                 &ctrl->ctrl14->reg, 1);
1017                 if (error < 0)
1018                         return error;
1019                 write_address++;
1020         }
1021
1022         if (ctrl->ctrl15) {
1023                 error = rmi_write_block(rmi_dev, write_address,
1024                                 &ctrl->ctrl15->reg, 1);
1025                 if (error < 0)
1026                         return error;
1027                 write_address++;
1028         }
1029
1030         if (ctrl->ctrl16) {
1031                 error = rmi_write_block(rmi_dev, write_address,
1032                                 &ctrl->ctrl16->reg, 1);
1033                 if (error < 0)
1034                         return error;
1035                 write_address++;
1036         }
1037
1038         if (ctrl->ctrl17) {
1039                 error = rmi_write_block(rmi_dev, write_address,
1040                                 &ctrl->ctrl17->reg, 1);
1041                 if (error < 0)
1042                         return error;
1043                 write_address++;
1044         }
1045
1046         if (ctrl->ctrl18_19) {
1047                 error = rmi_write_block(rmi_dev, write_address,
1048                         ctrl->ctrl18_19->reg, sizeof(union f11_2d_ctrl18_19));
1049                 if (error < 0)
1050                         return error;
1051                 write_address += sizeof(union f11_2d_ctrl18_19);
1052         }
1053
1054         if (ctrl->ctrl20_21) {
1055                 error = rmi_write_block(rmi_dev, write_address,
1056                                         ctrl->ctrl20_21->reg,
1057                                         sizeof(union f11_2d_ctrl20_21));
1058                 if (error < 0)
1059                         return error;
1060                 write_address += sizeof(union f11_2d_ctrl20_21);
1061         }
1062
1063         return 0;
1064 }
1065
1066 static int rmi_f11_get_query_parameters(struct rmi_device *rmi_dev,
1067                         struct f11_2d_sensor_query *query, u8 query_base_addr)
1068 {
1069         int query_size;
1070         int rc;
1071
1072         rc = rmi_read_block(rmi_dev, query_base_addr, query->f11_2d_query1__4,
1073                                         sizeof(query->f11_2d_query1__4));
1074         if (rc < 0)
1075                 return rc;
1076         query_size = rc;
1077
1078         if (query->has_abs) {
1079                 rc = rmi_read(rmi_dev, query_base_addr + query_size,
1080                                         &query->f11_2d_query5);
1081                 if (rc < 0)
1082                         return rc;
1083                 query_size++;
1084         }
1085
1086         if (query->has_rel) {
1087                 rc = rmi_read(rmi_dev, query_base_addr + query_size,
1088                                         &query->f11_2d_query6);
1089                 if (rc < 0)
1090                         return rc;
1091                 query_size++;
1092         }
1093
1094         if (query->has_gestures) {
1095                 rc = rmi_read_block(rmi_dev, query_base_addr + query_size,
1096                                         query->f11_2d_query7__8,
1097                                         sizeof(query->f11_2d_query7__8));
1098                 if (rc < 0)
1099                         return rc;
1100                 query_size += sizeof(query->f11_2d_query7__8);
1101         }
1102
1103         if (query->has_touch_shapes) {
1104                 rc = rmi_read(rmi_dev, query_base_addr + query_size,
1105                                         &query->f11_2d_query10);
1106                 if (rc < 0)
1107                         return rc;
1108                 query_size++;
1109         }
1110
1111         return query_size;
1112 }
1113
1114 /* This operation is done in a number of places, so we have a handy routine
1115  * for it.
1116  */
1117 static void f11_set_abs_params(struct rmi_function_container *fc, int index)
1118 {
1119         struct f11_data *instance_data =  fc->data;
1120         struct f11_2d_sensor *sensor = &instance_data->sensors[index];
1121         struct input_dev *input = sensor->input;
1122         int device_x_max =
1123                 instance_data->dev_controls.sensor_max_x_pos;
1124         int device_y_max =
1125                 instance_data->dev_controls.sensor_max_y_pos;
1126         int x_min, x_max, y_min, y_max;
1127
1128         if (sensor->axis_align.swap_axes) {
1129                 int temp = device_x_max;
1130                 device_x_max = device_y_max;
1131                 device_y_max = temp;
1132         }
1133
1134         /* Use the max X and max Y read from the device, or the clip values,
1135          * whichever is stricter.
1136          */
1137         x_min = sensor->axis_align.clip_X_low;
1138         if (sensor->axis_align.clip_X_high)
1139                 x_max = min((int) device_x_max,
1140                         sensor->axis_align.clip_X_high);
1141         else
1142                 x_max = device_x_max;
1143
1144         y_min = sensor->axis_align.clip_Y_low;
1145         if (sensor->axis_align.clip_Y_high)
1146                 y_max = min((int) device_y_max,
1147                         sensor->axis_align.clip_Y_high);
1148         else
1149                 y_max = device_y_max;
1150
1151         dev_dbg(&fc->dev, "Set ranges X=[%d..%d] Y=[%d..%d].",
1152                         x_min, x_max, y_min, y_max);
1153
1154         __set_bit(INPUT_PROP_DIRECT, input->propbit);
1155         __set_bit(EV_ABS, input->evbit);
1156         input_mt_init_slots(input, SYNAPTICS_MAX_POINTS);
1157         input_set_abs_params(input, ABS_MT_PRESSURE, 0, DEFAULT_MAX_ABS_MT_PRESSURE, 0, 0);
1158         input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, DEFAULT_MAX_ABS_MT_TOUCH, 0, 0);
1159         input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, DEFAULT_MAX_ABS_MT_TOUCH, 0, 0);
1160         input_set_abs_params(input, ABS_MT_ORIENTATION, 0, DEFAULT_MAX_ABS_MT_ORIENTATION, 0, 0);
1161         input_set_abs_params(input, ABS_MT_TRACKING_ID, 
1162                         DEFAULT_MIN_ABS_MT_TRACKING_ID,
1163                         DEFAULT_MAX_ABS_MT_TRACKING_ID, 0, 0);
1164         /* TODO get max_x_pos (and y) from control registers. */
1165         input_set_abs_params(input, ABS_MT_POSITION_X, x_min, x_max, 0, 0);
1166         input_set_abs_params(input, ABS_MT_POSITION_Y, y_min, y_max, 0, 0);
1167 #ifdef  CONFIG_RMI4_F11_PEN
1168         if (sensor->sens_query.query9.has_pen)
1169                 input_set_abs_params(input, ABS_MT_TOOL_TYPE,
1170                                      0, MT_TOOL_MAX, 0, 0);
1171 #endif
1172 }
1173
1174 static int rmi_f11_init(struct rmi_function_container *fc)
1175 {
1176         int rc;
1177         char buf[10];
1178         rc = rmi_f11_initialize(fc);
1179         if (rc < 0)
1180                 goto err_free_data;
1181
1182         rc = rmi_f11_register_devices(fc);
1183         if (rc < 0)
1184                 goto err_free_data;
1185
1186         rc = rmi_f11_create_sysfs(fc);
1187         if (rc < 0)
1188                 goto err_free_data;
1189
1190         //hhb@rock-chips.com
1191         buf[0] = 0x09;
1192         //buf[1] = 0x1b;
1193         rmi_write_block(fc->rmi_dev, fc->fd.control_base_addr, buf, 1);
1194         return 0;
1195
1196 err_free_data:
1197         rmi_f11_free_memory(fc);
1198
1199         return rc;
1200 }
1201
1202 static void rmi_f11_free_memory(struct rmi_function_container *fc)
1203 {
1204         struct f11_data *f11 = fc->data;
1205         int i;
1206
1207         if (f11) {
1208                 f11_free_control_regs(&f11->dev_controls);
1209                 for (i = 0; i < f11->dev_query.nbr_of_sensors + 1; i++)
1210                         kfree(f11->sensors[i].virtualbutton_map.map);
1211                 kfree(f11);
1212                 fc->data = NULL;
1213         }
1214 }
1215
1216
1217 static int rmi_f11_initialize(struct rmi_function_container *fc)
1218 {
1219         struct rmi_device *rmi_dev = fc->rmi_dev;
1220         struct f11_data *f11;
1221         u8 query_offset;
1222         u8 query_base_addr;
1223         u8 control_base_addr;
1224         u16 max_x_pos, max_y_pos, temp;
1225         int rc;
1226         int i;
1227         struct rmi_device_platform_data *pdata = to_rmi_platform_data(rmi_dev);
1228
1229         dev_dbg(&fc->dev, "Initializing F11 values for %s.\n",
1230                  pdata->sensor_name);
1231
1232         /*
1233         ** init instance data, fill in values and create any sysfs files
1234         */
1235         f11 = kzalloc(sizeof(struct f11_data), GFP_KERNEL);
1236         if (!f11)
1237                 return -ENOMEM;
1238
1239         fc->data = f11;
1240 #if     RESUME_REZERO
1241         f11->rezero_on_resume = true;
1242         f11->rezero_wait_ms = DEFAULT_REZERO_WAIT_MS;
1243 #endif
1244
1245         query_base_addr = fc->fd.query_base_addr;
1246         control_base_addr = fc->fd.control_base_addr;
1247
1248         rc = rmi_read(rmi_dev, query_base_addr, &f11->dev_query.f11_2d_query0);
1249         if (rc < 0)
1250                 return rc;
1251
1252         query_offset = (query_base_addr + 1);
1253         /* Increase with one since number of sensors is zero based */
1254         for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
1255                 f11->sensors[i].sensor_index = i;
1256
1257                 rc = rmi_f11_get_query_parameters(rmi_dev,
1258                                         &f11->sensors[i].sens_query,
1259                                         query_offset);
1260                 if (rc < 0)
1261                         return rc;
1262                 query_offset += rc;
1263
1264                 if (f11->dev_query.has_query9) {
1265                         rc = rmi_read(rmi_dev, query_offset,
1266                                       &f11->sensors[i].sens_query.query9.reg);
1267                         if (rc < 0) {
1268                                 dev_err(&fc->dev, "Failed to read query 9.\n");
1269                                 return rc;
1270                         }
1271                         query_offset += rc;
1272                 }
1273
1274                 rc = f11_allocate_control_regs(rmi_dev,
1275                                 &f11->dev_query, &f11->sensors[i].sens_query,
1276                                 &f11->dev_controls, control_base_addr);
1277                 if (rc < 0) {
1278                         dev_err(&fc->dev,
1279                                 "Failed to initialize F11 control params.\n");
1280                         return rc;
1281                 }
1282
1283                 f11->sensors[i].axis_align = pdata->axis_align;
1284
1285                 rc = rmi_read_block(rmi_dev,
1286                         control_base_addr + F11_CTRL_SENSOR_MAX_X_POS_OFFSET,
1287                         (u8 *)&max_x_pos, sizeof(max_x_pos));
1288                 if (rc < 0)
1289                         return rc;
1290
1291                 rc = rmi_read_block(rmi_dev,
1292                         control_base_addr + F11_CTRL_SENSOR_MAX_Y_POS_OFFSET,
1293                         (u8 *)&max_y_pos, sizeof(max_y_pos));
1294                 if (rc < 0)
1295                         return rc;
1296
1297                 if (pdata->axis_align.swap_axes) {
1298                         temp = max_x_pos;
1299                         max_x_pos = max_y_pos;
1300                         max_y_pos = temp;
1301                 }
1302                 f11->sensors[i].max_x = max_x_pos;
1303                 f11->sensors[i].max_y = max_y_pos;
1304
1305                 rc = f11_2d_construct_data(&f11->sensors[i]);
1306                 if (rc < 0)
1307                         return rc;
1308         }
1309
1310         return 0;
1311 }
1312
1313 static int rmi_f11_register_devices(struct rmi_function_container *fc)
1314 {
1315         struct rmi_device *rmi_dev = fc->rmi_dev;
1316         struct f11_data *f11 = fc->data;
1317         struct input_dev *input_dev;
1318         struct input_dev *input_dev_mouse;
1319         int sensors_itertd = 0;
1320         int i;
1321         int rc;
1322 #ifdef CONFIG_RMI4_VIRTUAL_BUTTON
1323         struct rmi_f11_virtualbutton_map *vm_sensor;
1324         struct rmi_f11_virtualbutton_map *vm_pdata;
1325         struct rmi_device_platform_data *pdata = to_rmi_platform_data(rmi_dev);
1326 #endif
1327
1328         for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
1329                 sensors_itertd = i;
1330                 input_dev = input_allocate_device();
1331                 if (!input_dev) {
1332                         rc = -ENOMEM;
1333                         goto error_unregister;
1334                 }
1335
1336                 f11->sensors[i].input = input_dev;
1337                 /* TODO how to modify the dev name and
1338                 * phys name for input device */
1339                 sprintf(f11->sensors[i].input_name, "%sfn%02x",
1340                         dev_name(&rmi_dev->dev), fc->fd.function_number);
1341                 input_dev->name = f11->sensors[i].input_name;
1342                 sprintf(f11->sensors[i].input_phys, "%s/input0",
1343                         input_dev->name);
1344                 input_dev->phys = f11->sensors[i].input_phys;
1345                 input_dev->dev.parent = &rmi_dev->dev;
1346                 input_set_drvdata(input_dev, f11);
1347
1348                 set_bit(EV_SYN, input_dev->evbit);
1349                 set_bit(EV_KEY, input_dev->evbit);
1350                 set_bit(EV_ABS, input_dev->evbit);
1351
1352                 f11_set_abs_params(fc, i);
1353
1354                 dev_dbg(&fc->dev, "%s: Sensor %d hasRel %d.\n",
1355                         __func__, i, f11->sensors[i].sens_query.has_rel);
1356                 if (f11->sensors[i].sens_query.has_rel) {
1357                         set_bit(EV_REL, input_dev->evbit);
1358                         set_bit(REL_X, input_dev->relbit);
1359                         set_bit(REL_Y, input_dev->relbit);
1360                 }
1361                 rc = input_register_device(input_dev);
1362                 if (rc < 0) {
1363                         input_free_device(input_dev);
1364                         f11->sensors[i].input = NULL;
1365                         goto error_unregister;
1366                 }
1367
1368                 /* how to register the virtualbutton device */
1369 #ifdef CONFIG_RMI4_VIRTUAL_BUTTON
1370                 if (f11->sensors[i].sens_query.has_gestures) {
1371                         int j;
1372
1373                         vm_sensor = &f11->sensors[i].virtualbutton_map;
1374                         vm_pdata = pdata->virtualbutton_map;
1375                         if (!vm_pdata) {
1376                                 dev_err(&fc->dev, "Failed to get the pdata virtualbutton map.\n");
1377                                 goto error_unregister;
1378                         }
1379                         vm_sensor->buttons = vm_pdata->buttons;
1380                         vm_sensor->map = kcalloc(vm_pdata->buttons,
1381                                         sizeof(struct virtualbutton_map),
1382                                         GFP_KERNEL);
1383                         if (!vm_sensor->map) {
1384                                 dev_err(&fc->dev, "Failed to allocate the virtualbutton map.\n");
1385                                 rc = -ENOMEM;
1386                                 goto error_unregister;
1387                         }
1388                         /* set bits for each button... */
1389                         for (j = 0; j < vm_pdata->buttons; j++) {
1390                                 memcpy(&vm_sensor->map[j], &vm_pdata->map[j],
1391                                         sizeof(struct virtualbutton_map));
1392                                 set_bit(vm_sensor->map[j].code,
1393                                         f11->sensors[i].input->keybit);
1394                         }
1395                 }
1396
1397 #endif
1398
1399                 if (f11->sensors[i].sens_query.has_rel) {
1400                         /*create input device for mouse events  */
1401                         input_dev_mouse = input_allocate_device();
1402                         if (!input_dev_mouse) {
1403                                 rc = -ENOMEM;
1404                                 goto error_unregister;
1405                         }
1406
1407                         f11->sensors[i].mouse_input = input_dev_mouse;
1408                         input_dev_mouse->name = "rmi_mouse";
1409                         input_dev_mouse->phys = "rmi_f11/input0";
1410
1411                         input_dev_mouse->id.vendor  = 0x18d1;
1412                         input_dev_mouse->id.product = 0x0210;
1413                         input_dev_mouse->id.version = 0x0100;
1414
1415                         set_bit(EV_REL, input_dev_mouse->evbit);
1416                         set_bit(REL_X, input_dev_mouse->relbit);
1417                         set_bit(REL_Y, input_dev_mouse->relbit);
1418
1419                         set_bit(BTN_MOUSE, input_dev_mouse->evbit);
1420                         /* Register device's buttons and keys */
1421                         set_bit(EV_KEY, input_dev_mouse->evbit);
1422                         set_bit(BTN_LEFT, input_dev_mouse->keybit);
1423                         set_bit(BTN_MIDDLE, input_dev_mouse->keybit);
1424                         set_bit(BTN_RIGHT, input_dev_mouse->keybit);
1425
1426                         rc = input_register_device(input_dev_mouse);
1427                         if (rc < 0) {
1428                                 input_free_device(input_dev_mouse);
1429                                 f11->sensors[i].mouse_input = NULL;
1430                                 goto error_unregister;
1431                         }
1432
1433                         set_bit(BTN_RIGHT, input_dev_mouse->keybit);
1434                 }
1435
1436         }
1437
1438         return 0;
1439
1440 error_unregister:
1441         for (; sensors_itertd > 0; sensors_itertd--) {
1442                 if (f11->sensors[sensors_itertd].input) {
1443                         if (f11->sensors[sensors_itertd].mouse_input) {
1444                                 input_unregister_device(
1445                                    f11->sensors[sensors_itertd].mouse_input);
1446                                 f11->sensors[sensors_itertd].mouse_input = NULL;
1447                         }
1448                         input_unregister_device(f11->sensors[i].input);
1449                         f11->sensors[i].input = NULL;
1450                 }
1451                 kfree(f11->sensors[i].virtualbutton_map.map);
1452         }
1453
1454         return rc;
1455 }
1456
1457 static void rmi_f11_free_devices(struct rmi_function_container *fc)
1458 {
1459         struct f11_data *f11 = fc->data;
1460         int i;
1461
1462         for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
1463                 if (f11->sensors[i].input)
1464                         input_unregister_device(f11->sensors[i].input);
1465                 if (f11->sensors[i].sens_query.has_rel &&
1466                                 f11->sensors[i].mouse_input)
1467                         input_unregister_device(f11->sensors[i].mouse_input);
1468         }
1469 }
1470
1471 static int rmi_f11_create_sysfs(struct rmi_function_container *fc)
1472 {
1473         int attr_count = 0;
1474         int rc;
1475
1476         dev_dbg(&fc->dev, "Creating sysfs files.\n");
1477         /* Set up sysfs device attributes. */
1478         for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
1479                 if (sysfs_create_file
1480                     (&fc->dev.kobj, &attrs[attr_count].attr) < 0) {
1481                         dev_err(&fc->dev,
1482                                 "Failed to create sysfs file for %s.",
1483                                 attrs[attr_count].attr.name);
1484                         rc = -ENODEV;
1485                         goto err_remove_sysfs;
1486                 }
1487         }
1488
1489         return 0;
1490
1491 err_remove_sysfs:
1492         for (attr_count--; attr_count >= 0; attr_count--)
1493                 sysfs_remove_file(&fc->dev.kobj,
1494                                                   &attrs[attr_count].attr);
1495         return rc;
1496 }
1497
1498 static int rmi_f11_config(struct rmi_function_container *fc)
1499 {
1500         struct f11_data *f11 = fc->data;
1501         int i;
1502         int rc;
1503
1504         for (i = 0; i < (f11->dev_query.nbr_of_sensors + 1); i++) {
1505                 rc = f11_write_control_regs(fc->rmi_dev,
1506                                    &f11->sensors[i].sens_query,
1507                                    &f11->dev_controls,
1508                                    fc->fd.query_base_addr);
1509                 if (rc < 0)
1510                         return rc;
1511         }
1512         return 0;
1513 }
1514
1515 static int rmi_f11_reset(struct rmi_function_container *fc)
1516 {
1517         /* we do nothing here */
1518         return 0;
1519 }
1520
1521 int rmi_f11_attention(struct rmi_function_container *fc, u8 *irq_bits)
1522 {
1523         struct rmi_device *rmi_dev = fc->rmi_dev;
1524         struct f11_data *f11 = fc->data;
1525         u8 data_base_addr = fc->fd.data_base_addr;
1526         int data_base_addr_offset = 0;
1527         int error;
1528         int i;
1529
1530         for (i = 0; i < f11->dev_query.nbr_of_sensors + 1; i++) {
1531                 error = rmi_read_block(rmi_dev,
1532                                 data_base_addr + data_base_addr_offset,
1533                                 f11->sensors[i].data_pkt,
1534                                 f11->sensors[i].pkt_size);
1535                 if (error < 0)
1536                         return error;
1537
1538                 rmi_f11_finger_handler(&f11->sensors[i]);
1539                 rmi_f11_virtual_button_handler(&f11->sensors[i]);
1540                 data_base_addr_offset += f11->sensors[i].pkt_size;
1541         }
1542
1543         return 0;
1544 }
1545
1546 #if RESUME_REZERO
1547 static int rmi_f11_resume(struct rmi_function_container *fc)
1548 {
1549         struct rmi_device *rmi_dev = fc->rmi_dev;
1550         struct f11_data *data = fc->data;
1551         /* Command register always reads as 0, so we can just use a local. */
1552         union f11_2d_commands commands = {};
1553         int retval = 0;
1554
1555         dev_dbg(&fc->dev, "Resuming...\n");
1556         if (!data->rezero_on_resume)
1557                 return 0;
1558
1559         if (data->rezero_wait_ms)
1560                 mdelay(data->rezero_wait_ms);
1561
1562         commands.rezero = 1;
1563         retval = rmi_write_block(rmi_dev, fc->fd.command_base_addr,
1564                         &commands.reg, sizeof(commands.reg));
1565         if (retval < 0) {
1566                 dev_err(&rmi_dev->dev, "%s: failed to issue rezero command, error = %d.",
1567                         __func__, retval);
1568                 return retval;
1569         }
1570
1571         return retval;
1572 }
1573 #endif /* RESUME_REZERO */
1574
1575 static void rmi_f11_remove(struct rmi_function_container *fc)
1576 {
1577         int attr_count = 0;
1578
1579         for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
1580                 sysfs_remove_file(&fc->dev.kobj,
1581                                   &attrs[attr_count].attr);
1582         }
1583
1584         rmi_f11_free_devices(fc);
1585
1586         rmi_f11_free_memory(fc);
1587
1588 }
1589
1590 static struct rmi_function_handler function_handler = {
1591         .func = 0x11,
1592         .init = rmi_f11_init,
1593         .config = rmi_f11_config,
1594         .reset = rmi_f11_reset,
1595         .attention = rmi_f11_attention,
1596         .remove = rmi_f11_remove
1597 #if     RESUME_REZERO
1598 #ifdef CONFIG_HAS_EARLYSUSPEND
1599         ,
1600         .late_resume = rmi_f11_resume
1601 #else
1602         .resume = rmi_f11_resume
1603 #endif  /* CONFIG_HAS_EARLYSUSPEND */
1604 #endif
1605 };
1606
1607 static int __init rmi_f11_module_init(void)
1608 {
1609         int error;
1610
1611         error = rmi_register_function_driver(&function_handler);
1612         if (error < 0) {
1613                 pr_err("%s: register failed!\n", __func__);
1614                 return error;
1615         }
1616
1617         return 0;
1618 }
1619
1620 static void __exit rmi_f11_module_exit(void)
1621 {
1622         rmi_unregister_function_driver(&function_handler);
1623 }
1624
1625 static ssize_t f11_maxPos_show(struct device *dev,
1626                                      struct device_attribute *attr,
1627                                      char *buf)
1628 {
1629         struct rmi_function_container *fc;
1630         struct f11_data *data;
1631
1632         fc = to_rmi_function_container(dev);
1633         data = fc->data;
1634
1635         return snprintf(buf, PAGE_SIZE, "%u %u\n",
1636                         data->sensors[0].max_x, data->sensors[0].max_y);
1637 }
1638
1639 static ssize_t f11_flip_show(struct device *dev,
1640                                    struct device_attribute *attr,
1641                                    char *buf)
1642 {
1643         struct rmi_function_container *fc;
1644         struct f11_data *data;
1645
1646         fc = to_rmi_function_container(dev);
1647         data = fc->data;
1648
1649         return snprintf(buf, PAGE_SIZE, "%u %u\n",
1650                         data->sensors[0].axis_align.flip_x,
1651                         data->sensors[0].axis_align.flip_y);
1652 }
1653
1654 static ssize_t f11_flip_store(struct device *dev,
1655                                     struct device_attribute *attr,
1656                                     const char *buf,
1657                                     size_t count)
1658 {
1659         struct rmi_function_container *fc;
1660         struct f11_data *instance_data;
1661         unsigned int new_X, new_Y;
1662
1663         fc = to_rmi_function_container(dev);
1664         instance_data = fc->data;
1665
1666
1667         if (sscanf(buf, "%u %u", &new_X, &new_Y) != 2)
1668                 return -EINVAL;
1669         if (new_X < 0 || new_X > 1 || new_Y < 0 || new_Y > 1)
1670                 return -EINVAL;
1671         instance_data->sensors[0].axis_align.flip_x = new_X;
1672         instance_data->sensors[0].axis_align.flip_y = new_Y;
1673
1674         return count;
1675 }
1676
1677 static ssize_t f11_swap_show(struct device *dev,
1678                                    struct device_attribute *attr, char *buf)
1679 {
1680         struct rmi_function_container *fc;
1681         struct f11_data *instance_data;
1682
1683         fc = to_rmi_function_container(dev);
1684         instance_data = fc->data;
1685
1686         return snprintf(buf, PAGE_SIZE, "%u\n",
1687                         instance_data->sensors[0].axis_align.swap_axes);
1688 }
1689
1690 static ssize_t f11_swap_store(struct device *dev,
1691                                     struct device_attribute *attr,
1692                                     const char *buf, size_t count)
1693 {
1694         struct rmi_function_container *fc;
1695         struct f11_data *instance_data;
1696         unsigned int newSwap;
1697
1698         fc = to_rmi_function_container(dev);
1699         instance_data = fc->data;
1700
1701
1702         if (sscanf(buf, "%u", &newSwap) != 1)
1703                 return -EINVAL;
1704         if (newSwap < 0 || newSwap > 1)
1705                 return -EINVAL;
1706         instance_data->sensors[0].axis_align.swap_axes = newSwap;
1707
1708         f11_set_abs_params(fc, 0);
1709
1710         return count;
1711 }
1712
1713 static ssize_t f11_relreport_show(struct device *dev,
1714                                         struct device_attribute *attr,
1715                                         char *buf)
1716 {
1717         struct rmi_function_container *fc;
1718         struct f11_data *instance_data;
1719
1720         fc = to_rmi_function_container(dev);
1721         instance_data = fc->data;
1722
1723         return snprintf(buf, PAGE_SIZE, "%u\n",
1724                         instance_data->
1725                         sensors[0].axis_align.rel_report_enabled);
1726 }
1727
1728 static ssize_t f11_relreport_store(struct device *dev,
1729                                          struct device_attribute *attr,
1730                                          const char *buf,
1731                                          size_t count)
1732 {
1733         struct rmi_function_container *fc;
1734         struct f11_data *instance_data;
1735         unsigned int new_value;
1736
1737         fc = to_rmi_function_container(dev);
1738         instance_data = fc->data;
1739
1740
1741         if (sscanf(buf, "%u", &new_value) != 1)
1742                 return -EINVAL;
1743         if (new_value < 0 || new_value > 1)
1744                 return -EINVAL;
1745         instance_data->sensors[0].axis_align.rel_report_enabled = new_value;
1746
1747         return count;
1748 }
1749
1750 static ssize_t f11_offset_show(struct device *dev,
1751                                      struct device_attribute *attr,
1752                                      char *buf)
1753 {
1754         struct rmi_function_container *fc;
1755         struct f11_data *instance_data;
1756
1757         fc = to_rmi_function_container(dev);
1758         instance_data = fc->data;
1759
1760         return snprintf(buf, PAGE_SIZE, "%d %d\n",
1761                         instance_data->sensors[0].axis_align.offset_X,
1762                         instance_data->sensors[0].axis_align.offset_Y);
1763 }
1764
1765 static ssize_t f11_offset_store(struct device *dev,
1766                                       struct device_attribute *attr,
1767                                       const char *buf,
1768                                       size_t count)
1769 {
1770         struct rmi_function_container *fc;
1771         struct f11_data *instance_data;
1772         int new_X, new_Y;
1773
1774         fc = to_rmi_function_container(dev);
1775         instance_data = fc->data;
1776
1777
1778         if (sscanf(buf, "%d %d", &new_X, &new_Y) != 2)
1779                 return -EINVAL;
1780         instance_data->sensors[0].axis_align.offset_X = new_X;
1781         instance_data->sensors[0].axis_align.offset_Y = new_Y;
1782
1783         return count;
1784 }
1785
1786 static ssize_t f11_clip_show(struct device *dev,
1787                                    struct device_attribute *attr,
1788                                    char *buf)
1789 {
1790
1791         struct rmi_function_container *fc;
1792         struct f11_data *instance_data;
1793
1794         fc = to_rmi_function_container(dev);
1795         instance_data = fc->data;
1796
1797         return snprintf(buf, PAGE_SIZE, "%u %u %u %u\n",
1798                         instance_data->sensors[0].axis_align.clip_X_low,
1799                         instance_data->sensors[0].axis_align.clip_X_high,
1800                         instance_data->sensors[0].axis_align.clip_Y_low,
1801                         instance_data->sensors[0].axis_align.clip_Y_high);
1802 }
1803
1804 static ssize_t f11_clip_store(struct device *dev,
1805                                     struct device_attribute *attr,
1806                                     const char *buf,
1807                                     size_t count)
1808 {
1809         struct rmi_function_container *fc;
1810         struct f11_data *instance_data;
1811         unsigned int new_X_low, new_X_high, new_Y_low, new_Y_high;
1812
1813         fc = to_rmi_function_container(dev);
1814         instance_data = fc->data;
1815
1816         if (sscanf(buf, "%u %u %u %u",
1817                    &new_X_low, &new_X_high, &new_Y_low, &new_Y_high) != 4)
1818                 return -EINVAL;
1819         if (new_X_low < 0 || new_X_low >= new_X_high || new_Y_low < 0
1820             || new_Y_low >= new_Y_high)
1821                 return -EINVAL;
1822         instance_data->sensors[0].axis_align.clip_X_low = new_X_low;
1823         instance_data->sensors[0].axis_align.clip_X_high = new_X_high;
1824         instance_data->sensors[0].axis_align.clip_Y_low = new_Y_low;
1825         instance_data->sensors[0].axis_align.clip_Y_high = new_Y_high;
1826
1827         /*
1828         ** for now, we assume this is sensor index 0
1829         */
1830         f11_set_abs_params(fc, 0);
1831
1832         return count;
1833 }
1834
1835 static ssize_t f11_rezero_store(struct device *dev,
1836                                      struct device_attribute *attr,
1837                                      const char *buf, size_t count)
1838 {
1839         struct rmi_function_container *fc = NULL;
1840         unsigned int rezero;
1841         int retval = 0;
1842         /* Command register always reads as 0, so we can just use a local. */
1843         union f11_2d_commands commands = {};
1844
1845         fc = to_rmi_function_container(dev);
1846
1847         if (sscanf(buf, "%u", &rezero) != 1)
1848                 return -EINVAL;
1849         if (rezero < 0 || rezero > 1)
1850                 return -EINVAL;
1851
1852         /* Per spec, 0 has no effect, so we skip it entirely. */
1853         if (rezero) {
1854                 commands.rezero = 1;
1855                 retval = rmi_write_block(fc->rmi_dev, fc->fd.command_base_addr,
1856                                 &commands.reg, sizeof(commands.reg));
1857                 if (retval < 0) {
1858                         dev_err(dev, "%s: failed to issue rezero command, error = %d.",
1859                                 __func__, retval);
1860                         return retval;
1861                 }
1862         }
1863
1864         return count;
1865 }
1866
1867 #if RESUME_REZERO
1868 static ssize_t f11_rezeroOnResume_store(struct device *dev,
1869                                      struct device_attribute *attr,
1870                                      const char *buf, size_t count)
1871 {
1872         struct rmi_function_container *fc = NULL;
1873         unsigned int newValue;
1874         struct f11_data *instance_data;
1875
1876         fc = to_rmi_function_container(dev);
1877         instance_data = fc->data;
1878
1879         if (sscanf(buf, "%u", &newValue) != 1)
1880                 return -EINVAL;
1881         if (newValue < 0 || newValue > 1) {
1882                 dev_err(dev, "rezeroOnResume must be either 1 or 0.\n");
1883                 return -EINVAL;
1884         }
1885
1886         instance_data->rezero_on_resume = (newValue != 0);
1887
1888         return count;
1889 }
1890
1891 static ssize_t f11_rezeroOnResume_show(struct device *dev,
1892                                         struct device_attribute *attr,
1893                                         char *buf)
1894 {
1895         struct rmi_function_container *fc;
1896         struct f11_data *instance_data;
1897
1898         fc = to_rmi_function_container(dev);
1899         instance_data = fc->data;
1900
1901         return snprintf(buf, PAGE_SIZE, "%u\n",
1902                         instance_data->rezero_on_resume);
1903 }
1904
1905 static ssize_t f11_rezeroWait_store(struct device *dev,
1906                                      struct device_attribute *attr,
1907                                      const char *buf, size_t count)
1908 {
1909         struct rmi_function_container *fc = NULL;
1910         unsigned int newValue;
1911         struct f11_data *instance_data;
1912
1913         fc = to_rmi_function_container(dev);
1914         instance_data = fc->data;
1915
1916         if (sscanf(buf, "%u", &newValue) != 1)
1917                 return -EINVAL;
1918         if (newValue < 0) {
1919                 dev_err(dev, "rezeroWait must be 0 or greater.\n");
1920                 return -EINVAL;
1921         }
1922
1923         instance_data->rezero_wait_ms = (newValue != 0);
1924
1925         return count;
1926 }
1927
1928 static ssize_t f11_rezeroWait_show(struct device *dev,
1929                                         struct device_attribute *attr,
1930                                         char *buf)
1931 {
1932         struct rmi_function_container *fc;
1933         struct f11_data *instance_data;
1934
1935         fc = to_rmi_function_container(dev);
1936         instance_data = fc->data;
1937
1938         return snprintf(buf, PAGE_SIZE, "%u\n",
1939                         instance_data->rezero_wait_ms);
1940 }
1941 #endif
1942
1943 module_init(rmi_f11_module_init);
1944 module_exit(rmi_f11_module_exit);
1945
1946 MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com");
1947 MODULE_DESCRIPTION("RMI F11 module");
1948 MODULE_LICENSE("GPL");
1949 MODULE_VERSION(RMI_DRIVER_VERSION);
1950