summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/rmi4/rmi_f19.c
blob: 1fdc9b3373f51c80e7b778be222f8b3181a35a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/*
 * Copyright (c) 2012 Synaptics Incorporated
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#define FUNCTION_DATA f19_data
#define FNUM 19

#include <linux/kernel.h>
#include <linux/rmi.h>
#include <linux/input.h>
#include <linux/slab.h>
#include "rmi_driver.h"

#define QUERY_BASE_INDEX 1
#define MAX_LEN 256
#define MAX_BUFFER_LEN 80

#define SENSOR_MAP_MIN			0
#define SENSOR_MAP_MAX			127
#define SENSITIVITY_ADJ_MIN		0
#define SENSITIVITY_ADJ_MAX		31
#define HYSTERESIS_THRESHOLD_MIN	0
#define HYSTERESIS_THRESHOLD_MAX	15
#define FUNCTION_NUMBER			0x19

union f19_0d_query {
		struct {
			u8 configurable:1;
			u8 has_sensitivity_adjust:1;
			u8 has_hysteresis_threshold:1;
		u8 reserved_1:5;

		u8 button_count:5;
		u8 reserved_2:3;
	} __attribute__((__packed__));
	struct {
		u8 regs[2];
		u16 address;
	} __attribute__((__packed__));
};

union f19_0d_control_0 {
		struct {
			u8 button_usage:2;
			u8 filter_mode:2;
	} __attribute__((__packed__));
	struct {
		u8 regs[1];
		u16 address;
	} __attribute__((__packed__));
};
/* rewrite control regs */
struct f19_0d_control_1n {
	u8 int_enabled_button;
} __attribute__((__packed__));

struct f19_0d_control_1 {
	struct f19_0d_control_1n *regs;
	u16 address;
	u8 length;
} __attribute__((__packed__));

struct f19_0d_control_2n {
	u8 single_button;
} __attribute__((__packed__));

struct f19_0d_control_2 {
	struct f19_0d_control_2n *regs;
	u16 address;
	u8 length;
} __attribute__((__packed__));

struct f19_0d_control_3n {
	u8 sensor_map_button:7;
} __attribute__((__packed__));

struct f19_0d_control_3 {
	struct f19_0d_control_3n *regs;
	u16 address;
	u8 length;
} __attribute__((__packed__));

struct f19_0d_control_4n {
	u8 sensitivity_button:7;
} __attribute__((__packed__));

struct f19_0d_control_4 {
	struct f19_0d_control_4n *regs;
	u16 address;
	u8 length;
} __attribute__((__packed__));

union f19_0d_control_5 {
	struct {
		u8 sensitivity_adj:5;
	};
	struct {
		u8 regs[1];
		u16 address;
	} __attribute__((__packed__));
};

union f19_0d_control_6 {
	struct {
		u8 hysteresis_threshold:4;
	};
	struct {
		u8 regs[1];
		u16 address;
	} __attribute__((__packed__));
};

struct f19_0d_control {
	union f19_0d_control_0 *reg_0;
	struct f19_0d_control_1 *reg_1;
	struct f19_0d_control_2 *reg_2;
	struct f19_0d_control_3 *reg_3;
	struct f19_0d_control_4 *reg_4;
	union f19_0d_control_5 *reg_5;
	union f19_0d_control_6 *reg_6;
};

/* data specific to fn $19 that needs to be kept around */
struct f19_data {
	struct f19_0d_control control;
	union f19_0d_query query;
	u8 button_rezero;
	u8 button_count;
	u8 button_bitmask_size;
	u8 *button_data_buffer;
	u16 *button_map;
	char input_phys[MAX_LEN];
	struct input_dev *input;
	struct mutex control_mutex;
	struct mutex data_mutex;
};

/* Query sysfs files */
show_union_struct_prototype(configurable)
show_union_struct_prototype(has_sensitivity_adjust)
show_union_struct_prototype(has_hysteresis_threshold)
show_union_struct_prototype(button_count)

static struct attribute *attrs[] = {
	attrify(configurable),
	attrify(has_sensitivity_adjust),
	attrify(has_hysteresis_threshold),
	attrify(button_count),
	NULL
};

static struct attribute_group attrs_query = GROUP(attrs);
/* Control sysfs files */
show_store_union_struct_prototype(button_usage)
show_store_union_struct_prototype(filter_mode)
show_store_union_struct_prototype(int_enabled_button)
show_store_union_struct_prototype(single_button)
show_store_union_struct_prototype(sensitivity_button)
show_store_union_struct_prototype(sensitivity_adj)
show_store_union_struct_prototype(hysteresis_threshold)


static struct attribute *attrsCtrl[] = {
	attrify(button_usage),
	attrify(filter_mode),
	attrify(int_enabled_button),
	attrify(single_button),
	attrify(sensitivity_button),
	NULL
};
static struct attribute_group attrs_control = GROUP(attrsCtrl);

static ssize_t rmi_f19_sensor_map_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct f19_data *data;
	int reg_length;
	int result, size = 0;
	char *temp;
	int i;

	fn_dev = to_rmi_function_dev(dev);
	data = fn_dev->data;

	/* Read current regtype values */
	reg_length = data->control.reg_3->length;
	result = rmi_read_block(fn_dev->rmi_dev, data->control.reg_3->address,
			 data->control.reg_3->regs, reg_length * sizeof(u8));
	if (result < 0) {
		dev_dbg(dev, "%s : Could not read regtype at 0x%x\n"
					"Data may be outdated.", __func__,
					data->control.reg_3->address);
	}
	temp = buf;
	for (i = 0; i < reg_length; i++) {
		result = snprintf(temp, PAGE_SIZE - size, "%u ",
				data->control.reg_3->regs[i].sensor_map_button);
		if (result < 0) {
			dev_err(dev, "%s : Could not write output.", __func__);
			return result;
			}
		size += result;
		temp += result;
		}
	result = snprintf(temp, PAGE_SIZE - size, "\n");
	if (result < 0) {
		dev_err(dev, "%s : Could not write output.", __func__);
		return result;
	}
	return size + result;
}

static ssize_t rmi_f19_sensor_map_store(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf, size_t count)
{
	struct rmi_function_dev *fn_dev;
	struct f19_data *data;
	int reg_length;
	int result;
	const char *temp;
	int i;
	unsigned int newval;

	fn_dev = to_rmi_function_dev(dev);
	data = fn_dev->data;

	/* Read current regtype values */
	reg_length = data->control.reg_3->length;
	result = rmi_read_block(fn_dev->rmi_dev, data->control.reg_3->address,
			 data->control.reg_3->regs, reg_length * sizeof(u8));

	if (result < 0) {
		dev_dbg(dev, "%s: Could not read regtype at %#06x. "
					"Data may be outdated.", __func__,
					data->control.reg_3->address);
	}

	/* parse input */
	temp = buf;
	for (i = 0; i < reg_length; i++) {
		if (sscanf(temp, "%u", &newval) == 1) {
			data->control.reg_3->regs[i].sensor_map_button = newval;
		} else {
			/* If we don't read a value for each position, abort,
			 * restore previous values locally by rereading */
			result = rmi_read_block(fn_dev->rmi_dev,
					data->control.reg_3->address,
					data->control.reg_3->regs,
					reg_length * sizeof(u8));
			if (result < 0) {
				dev_dbg(dev, "%s: Couldn't read regtype at "
					"%#06x. Local data may be inaccurate",
					__func__,
					data->control.reg_3->address);
			}
			return -EINVAL;
		}
		/* move to next number */
		while (*temp != 0) {
			temp++;
			if (isspace(*(temp - 1)) && !isspace(*temp))
				break;
	}
		}
	result = rmi_write_block(fn_dev->rmi_dev, data->control.reg_3->address,
			 data->control.reg_3->regs,
			reg_length * sizeof(u8));
	if (result < 0) {
		dev_dbg(dev, "%s: Could not write new values to %#06x\n",
				__func__, data->control.reg_3->address);
		return result;
	}
	return count;
}
static struct device_attribute sensor_map_attr = __ATTR(sensor_map_button,
			RMI_RW_ATTR, rmi_f19_sensor_map_show,
			rmi_f19_sensor_map_store);

static struct device_attribute sensor_map_ro_attr =  __ATTR(sensor_map_button,
			RMI_RO_ATTR, rmi_f19_sensor_map_show,
			 NULL);

static ssize_t f19_rezero_store(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf,
					 size_t count)
{
	unsigned int new_value;
	int len;
	struct f19_data *f19;
	struct rmi_function_dev *fn_dev = to_rmi_function_dev(dev);

	f19 = fn_dev->data;
	len = sscanf(buf, "%u", &new_value);
	if (new_value != 0 && new_value != 1) {
		dev_err(dev, "%s: Error - rezero is not a valid value 0x%x.\n",
			__func__, new_value);
		return -EINVAL;
		}
	f19->button_rezero = new_value & 1;
	len = rmi_write(fn_dev->rmi_dev, fn_dev->fd.command_base_addr,
		f19->button_rezero);

	if (len < 0) {
		dev_err(dev, "%s : Could not write rezero to 0x%x\n",
				__func__, fn_dev->fd.command_base_addr);
		return -EINVAL;
	}
	return count;
}

static DEVICE_ATTR(rezero, RMI_WO_ATTR,
		NULL,
		f19_rezero_store);

static struct attribute *attrsCommand[] = {
	attrify(rezero),
	NULL
};
static struct attribute_group attrs_command = GROUP(attrsCommand);

int rmi_f19_read_control_parameters(struct rmi_device *rmi_dev,
	struct f19_data *f19)
{
	int retval = 0;
	union f19_0d_query *query = &f19->query;
	struct f19_0d_control *control = &f19->control;


	retval = rmi_read_block(rmi_dev, control->reg_0->address,
			control->reg_0->regs, sizeof(control->reg_0->regs));
	if (retval < 0) {
		dev_err(&rmi_dev->dev, "Could not read control reg0 to %#06x.\n",
				control->reg_0->address);
		return retval;
	}

	retval = rmi_read_block(rmi_dev, control->reg_1->address,
			control->reg_1->regs, f19->button_bitmask_size);
	if (retval < 0) {
		dev_err(&rmi_dev->dev, "Could not read control reg1 to %#06x.\n",
			 control->reg_1->address);
		return retval;
	}

	retval = rmi_read_block(rmi_dev, control->reg_2->address,
			control->reg_2->regs, f19->button_bitmask_size);
	if (retval < 0) {
		dev_err(&rmi_dev->dev, "Could not read control reg2 to %#06x.\n",
			 control->reg_2->address);
		return retval;
	}

	retval = rmi_read_block(rmi_dev, control->reg_3->address,
			control->reg_3->regs, f19->button_count);
	if (retval < 0) {
		dev_err(&rmi_dev->dev, "Could not read control reg3 to %#06x.\n",
			 control->reg_3->address);
		return retval;
	}

	retval = rmi_read_block(rmi_dev, control->reg_4->address,
			control->reg_4->regs, f19->button_count);
	if (retval < 0) {
		dev_err(&rmi_dev->dev, "Could not read control reg3 to %#06x.\n",
			 control->reg_4->address);
		return retval;
	}

	if (query->has_sensitivity_adjust) {
		retval = rmi_read_block(rmi_dev, control->reg_5->address,
				control->reg_5->regs,
				sizeof(control->reg_5->regs));
		if (retval < 0) {
			dev_err(&rmi_dev->dev, "Could not read control reg5 to %#06x.\n",
				control->reg_5->address);
			return retval;
		}
	}
	if (query->has_hysteresis_threshold) {
		retval = rmi_read_block(rmi_dev, control->reg_6->address,
				control->reg_6->regs,
				sizeof(control->reg_6->regs));
		if (retval < 0) {
			dev_err(&rmi_dev->dev, "Could not read control reg6 to %#06x.\n",
				control->reg_6->address);
			return retval;
		}
	}
	return 0;
}

static int rmi_f19_alloc_memory(struct rmi_function_dev *fn_dev)
{
	struct f19_data *f19;
	int rc;
	u16 ctrl_base_addr;

	/* allow memory for fn19 data */
	f19 = devm_kzalloc(&fn_dev->dev, sizeof(struct f19_data), GFP_KERNEL);
	if (!f19) {
		dev_err(&fn_dev->dev, "Failed to allocate function data.\n");
		return -ENOMEM;
	}
	fn_dev->data = f19;
	rc = rmi_read_block(fn_dev->rmi_dev, fn_dev->fd.query_base_addr,
				&f19->query,
				sizeof(f19->query.regs));
	if (rc < 0) {
		dev_err(&fn_dev->dev, "Failed to read query register.\n");
		return rc;
	}
	f19->query.address = fn_dev->fd.query_base_addr;

	f19->button_bitmask_size = sizeof(u8)*(f19->query.button_count + 7)/8;
	f19->button_data_buffer = devm_kzalloc(&fn_dev->dev,
				f19->button_bitmask_size, GFP_KERNEL);
	if (!f19->button_data_buffer) {
		dev_err(&fn_dev->dev, "Failed to allocate button data buffer.\n");
		return -ENOMEM;
	}

	f19->button_map = devm_kzalloc(&fn_dev->dev, f19->query.button_count,
				GFP_KERNEL);
	if (!f19->button_map) {
		dev_err(&fn_dev->dev, "Failed to allocate button map.\n");
		return -ENOMEM;
	}

	/* allocate memory for control reg */
	/* reg 0 */
	ctrl_base_addr = fn_dev->fd.control_base_addr;
	f19->control.reg_0 = devm_kzalloc(&fn_dev->dev,
				sizeof(f19->control.reg_0->regs), GFP_KERNEL);
	if (!f19->control.reg_0) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_0 control registers.");
		return -ENOMEM;
	}
	f19->control.reg_0->address = ctrl_base_addr;
	ctrl_base_addr += sizeof(f19->control.reg_0->regs);

	/* reg 1 */
	f19->control.reg_1 = devm_kzalloc(&fn_dev->dev,
				sizeof(struct f19_0d_control_1), GFP_KERNEL);
	if (!f19->control.reg_1) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_1 control registers.");
		return -ENOMEM;
	}
	f19->control.reg_1->regs = devm_kzalloc(&fn_dev->dev,
					f19->button_bitmask_size, GFP_KERNEL);
	if (!f19->control.reg_1->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_1->regs control registers.");
		return -ENOMEM;
	}
	f19->control.reg_1->address = ctrl_base_addr;
	f19->control.reg_1->length = f19->button_bitmask_size;
	ctrl_base_addr += f19->button_bitmask_size;

	/* reg 2 */
	f19->control.reg_2 = devm_kzalloc(&fn_dev->dev,
			sizeof(struct f19_0d_control_2), GFP_KERNEL);
	if (!f19->control.reg_2) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_2 control registers.");
		return -ENOMEM;
	}
	f19->control.reg_2->regs = devm_kzalloc(&fn_dev->dev,
					f19->button_bitmask_size, GFP_KERNEL);
	if (!f19->control.reg_2->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_2->regs control registers.");
		return -ENOMEM;
	}
	f19->control.reg_2->address = ctrl_base_addr;
	f19->control.reg_2->length = f19->button_bitmask_size;
	ctrl_base_addr += f19->button_bitmask_size;

	/* reg 3 */
	f19->control.reg_3 = devm_kzalloc(&fn_dev->dev,
			sizeof(struct f19_0d_control_3), GFP_KERNEL);
	if (!f19->control.reg_3) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_3 control registers.");
		return -ENOMEM;
	}
	f19->control.reg_3->regs = devm_kzalloc(&fn_dev->dev,
				f19->query.button_count, GFP_KERNEL);
	if (!f19->control.reg_3->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_3->regs control registers.");
		return -ENOMEM;
	}
	f19->control.reg_3->address = ctrl_base_addr;
	f19->control.reg_3->length = f19->query.button_count;
	ctrl_base_addr += f19->query.button_count;

	/* reg 4 */
	f19->control.reg_4 = devm_kzalloc(&fn_dev->dev,
				sizeof(struct f19_0d_control_4), GFP_KERNEL);
	if (!f19->control.reg_4) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_3 control registers.");
		return -ENOMEM;
	}
	f19->control.reg_4->regs = devm_kzalloc(&fn_dev->dev,
				f19->query.button_count, GFP_KERNEL);
	if (!f19->control.reg_4->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate reg_3->regs control registers.");
		return -ENOMEM;
	}
	f19->control.reg_4->address = ctrl_base_addr;
	f19->control.reg_4->length = f19->query.button_count;
	ctrl_base_addr += f19->query.button_count;

	/* reg 5 */
	if (f19->query.has_sensitivity_adjust) {
		f19->control.reg_5 = devm_kzalloc(&fn_dev->dev,
			sizeof(f19->control.reg_5->regs), GFP_KERNEL);
		if (!f19->control.reg_5) {
			dev_err(&fn_dev->dev, "Failed to allocate reg_5 control registers.");
			return -ENOMEM;
		}
		f19->control.reg_5->address = ctrl_base_addr;
		ctrl_base_addr += sizeof(f19->control.reg_5->regs);
	}
	/* reg 6 */
	if (f19->query.has_hysteresis_threshold) {
		f19->control.reg_6 =
			devm_kzalloc(&fn_dev->dev,
				sizeof(f19->control.reg_6->regs), GFP_KERNEL);
		if (!f19->control.reg_6) {
			dev_err(&fn_dev->dev, "Failed to allocate reg_6 control registers.");
			return -ENOMEM;
		}
		f19->control.reg_6->address = ctrl_base_addr;
	}
	return 0;
}

static void rmi_f19_free_memory(struct rmi_function_dev *fn_dev)
{
	union f19_0d_query *query;
	struct f19_data *f19 = fn_dev->data;

	query = &f19->query;
	sysfs_remove_group(&fn_dev->dev.kobj, &attrs_query);
	sysfs_remove_group(&fn_dev->dev.kobj, &attrs_control);
	if (query->has_sensitivity_adjust)
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(sensitivity_adj));

	if (query->has_hysteresis_threshold)
		sysfs_remove_file(&fn_dev->dev.kobj,
				attrify(hysteresis_threshold));
	sysfs_remove_group(&fn_dev->dev.kobj, &attrs_command);
	if (query->configurable)
		sysfs_remove_file(&fn_dev->dev.kobj, &sensor_map_attr.attr);
	else
		sysfs_remove_file(&fn_dev->dev.kobj, &sensor_map_ro_attr.attr);

}

static int rmi_f19_initialize(struct rmi_function_dev *fn_dev)
{
	int i;
	int rc;
	struct rmi_device *rmi_dev = fn_dev->rmi_dev;
	struct rmi_device_platform_data *pdata;
	struct f19_data *f19 = fn_dev->data;

	/* initial all default values for f19 data here */
	rc = rmi_read(rmi_dev, fn_dev->fd.command_base_addr,
		&f19->button_rezero);
	if (rc < 0) {
		dev_err(&fn_dev->dev, "Failed to read command register.\n");
		return rc;
	}
	f19->button_rezero = f19->button_rezero & 1;

	pdata = to_rmi_platform_data(rmi_dev);
	if (pdata) {
		if (!pdata->f19_button_map)
			dev_warn(&fn_dev->dev, "F19 button_map is NULL");
		else if (!pdata->f19_button_map->map)
			dev_warn(&fn_dev->dev,
				 "Platformdata button map is missing!\n");
		else {
			if (pdata->f19_button_map->nbuttons !=
						f19->query.button_count)
				dev_warn(&fn_dev->dev, "Platformdata button map size (%d) != number of buttons on device (%d) - ignored.\n",
					pdata->f19_button_map->nbuttons,
					f19->query.button_count);
			f19->button_count = min(pdata->f19_button_map->nbuttons,
					 (u8) f19->query.button_count);
			for (i = 0; i < f19->button_count; i++)
				f19->button_map[i] =
					pdata->f19_button_map->map[i];
		}
	}
	rc = rmi_f19_read_control_parameters(rmi_dev, f19);
	if (rc < 0) {
		dev_err(&fn_dev->dev,
			"Failed to initialize F19 control params.\n");
		return rc;
	}

	mutex_init(&f19->control_mutex);
	mutex_init(&f19->data_mutex);
	return 0;
}

static int rmi_f19_register_device(struct rmi_function_dev *fn_dev)
{
	int i;
	int rc;
	struct rmi_device *rmi_dev = fn_dev->rmi_dev;
	struct f19_data *f19 = fn_dev->data;
	struct rmi_driver *driver = fn_dev->rmi_dev->driver;
	struct input_dev *input_dev = input_allocate_device();

	if (!input_dev) {
		dev_err(&fn_dev->dev, "Failed to allocate input device.\n");
		return -ENOMEM;
	}

	f19->input = input_dev;

	if (driver->set_input_params) {
		rc = driver->set_input_params(rmi_dev, input_dev);
		if (rc < 0) {
			dev_err(&fn_dev->dev, "%s: Error in setting input device.\n",
			__func__);
			goto error_free_device;
		}
	}
	sprintf(f19->input_phys, "%s/input0", dev_name(&fn_dev->dev));
	input_dev->phys = f19->input_phys;
	input_dev->dev.parent = &rmi_dev->dev;
	input_set_drvdata(input_dev, f19);

	/* Set up any input events. */
	set_bit(EV_SYN, input_dev->evbit);
	set_bit(EV_KEY, input_dev->evbit);

	/* manage button map using input subsystem */
	input_dev->keycode = f19->button_map;
	input_dev->keycodesize = sizeof(f19->button_map);
	input_dev->keycodemax = f19->button_count;

	/* set bits for each button... */
	for (i = 0; i < f19->button_count; i++)
		set_bit(f19->button_map[i], input_dev->keybit);
	rc = input_register_device(input_dev);
	if (rc < 0) {
		dev_err(&fn_dev->dev, "Failed to register input device.\n");
		goto error_free_device;
	}

	return 0;

error_free_device:
	input_free_device(input_dev);

	return rc;
}

static int rmi_f19_create_sysfs(struct rmi_function_dev *fn_dev)
{
	struct f19_data *f19 = fn_dev->data;
	union f19_0d_query *query = &f19->query;

	dev_dbg(&fn_dev->dev, "Creating sysfs files.\n");

	/* Set up sysfs device attributes. */
	if (sysfs_create_group(&fn_dev->dev.kobj, &attrs_query) < 0) {
		dev_err(&fn_dev->dev, "Failed to create query sysfs files.");
		return -ENODEV;
	}
	if (sysfs_create_group(&fn_dev->dev.kobj, &attrs_control) < 0) {
		dev_err(&fn_dev->dev, "Failed to create control sysfs files.");
		return -ENODEV;
	}
	if (query->has_sensitivity_adjust) {
		if (sysfs_create_file(&fn_dev->dev.kobj,
				attrify(sensitivity_adj)) < 0) {
			dev_err(&fn_dev->dev,
				"Failed to create control sysfs files.");
			return -ENODEV;
		}
	}
	if (query->has_hysteresis_threshold) {
		if (sysfs_create_file(&fn_dev->dev.kobj,
				attrify(hysteresis_threshold)) < 0) {
			dev_err(&fn_dev->dev,
				"Failed to create control sysfs files.");
			return -ENODEV;
		}
	}
	if (sysfs_create_group(&fn_dev->dev.kobj, &attrs_command) < 0) {
		dev_err(&fn_dev->dev, "Failed to create command sysfs files.");
		return -ENODEV;
	}
	if (query->configurable) {
		if (sysfs_create_file(&fn_dev->dev.kobj,
					&sensor_map_attr.attr) < 0) {
			dev_err(&fn_dev->dev,
				"Failed to create control sysfs files.");
			return -ENODEV;
	}
	} else {
		if (sysfs_create_file(&fn_dev->dev.kobj,
					&sensor_map_ro_attr.attr) < 0) {
			dev_err(&fn_dev->dev,
				"Failed to create control sysfs files.");
			return -ENODEV;
		}
	}
	return 0;
}

static int rmi_f19_config(struct rmi_function_dev *fn_dev)
{
	struct f19_data *f19 = fn_dev->data;
	int retval = 0;
	union f19_0d_query *query = &f19->query;
	struct f19_0d_control *control = &f19->control;

	retval = rmi_write_block(fn_dev->rmi_dev, control->reg_0->address,
			control->reg_0->regs,
			sizeof(u8));
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not write control reg0 to %#06x.\n",
				control->reg_0->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, control->reg_1->address,
			control->reg_1->regs, f19->button_bitmask_size);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not write control reg1 to %#06x.\n",
			 control->reg_1->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, control->reg_2->address,
			control->reg_2->regs, f19->button_bitmask_size);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not write control reg2 to %#06x.\n",
			 control->reg_2->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, control->reg_3->address,
			control->reg_3->regs, query->button_count);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not write control reg3 to %#06x.\n",
			 control->reg_3->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, control->reg_4->address,
			control->reg_4->regs, query->button_count);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not write control reg3 to %#06x.\n",
			 control->reg_4->address);
		return retval;
		}

	if (query->has_sensitivity_adjust) {
		retval = rmi_write_block(fn_dev->rmi_dev,
				control->reg_5->address, control->reg_5->regs,
				sizeof(control->reg_5->regs));
		if (retval < 0) {
			dev_err(&fn_dev->dev,
				"Could not write control reg5 to %#06x.\n",
				control->reg_5->address);
			return retval;
		}
	}
	if (query->has_hysteresis_threshold) {
		retval = rmi_write_block(fn_dev->rmi_dev,
					control->reg_6->address,
					control->reg_6->regs,
					sizeof(control->reg_6->regs));
	if (retval < 0) {
			dev_err(&fn_dev->dev,
				"Could not write control reg6 to %#06x.\n",
				control->reg_6->address);
			return retval;
	}
	}
	return 0;
}

static int rmi_f19_probe(struct rmi_function_dev *fn_dev)
{
	int rc = rmi_f19_alloc_memory(fn_dev);
	if (rc < 0)
		goto err_free_data;

	rc = rmi_f19_initialize(fn_dev);
	if (rc < 0)
		goto err_free_data;

	rc = rmi_f19_register_device(fn_dev);
	if (rc < 0)
		goto err_free_data;

	rc = rmi_f19_create_sysfs(fn_dev);
	if (rc < 0)
		goto err_free_data;

	return 0;

err_free_data:
	rmi_f19_free_memory(fn_dev);
	return rc;
}

static int rmi_f19_reset(struct rmi_function_dev *fn_dev)
{
	/* we do nnothing here */
	return 0;
}


static int rmi_f19_remove(struct rmi_function_dev *fn_dev)
{
	struct f19_data *f19 = fn_dev->data;

	input_unregister_device(f19->input);
	rmi_f19_free_memory(fn_dev);

	return 0;
}

static int rmi_f19_attention(struct rmi_function_dev *fn_dev,
						unsigned long *irq_bits)
{
	int error;
	int button;
	struct rmi_device *rmi_dev = fn_dev->rmi_dev;
	struct f19_data *f19 = fn_dev->data;
	u16 data_base_addr = fn_dev->fd.data_base_addr;

	/* Read the button data. */
	error = rmi_read_block(rmi_dev, data_base_addr, f19->button_data_buffer,
			f19->button_bitmask_size);
	if (error < 0) {
		dev_err(&fn_dev->dev, "%s: Failed to read button data registers.\n",
			__func__);
		return error;
	}

	/* Generate events for buttons. */
	for (button = 0; button < f19->button_count; button++) {
		int button_reg;
		int button_shift;
		bool button_status;

		/* determine which data byte the button status is in */
		button_reg = button / 8;
		/* bit shift to get button's status */
		button_shift = button % 8;
		button_status =
		    ((f19->button_data_buffer[button_reg] >> button_shift)
			& 0x01) != 0;
		/* Generate an event here. */
		input_report_key(f19->input, f19->button_map[button],
				 button_status);
	}

	input_sync(f19->input); /* sync after groups of events */
	return 0;
}

static struct rmi_function_driver function_driver = {
	.driver = {
		.name = "rmi_f19",
	},
	.func = FUNCTION_NUMBER,
	.probe = rmi_f19_probe,
	.remove = rmi_f19_remove,
	.config = rmi_f19_config,
	.reset = rmi_f19_reset,
	.attention = rmi_f19_attention,
};


/* sysfs functions */
/* Query */
simple_show_union_struct_unsigned(query, configurable)
simple_show_union_struct_unsigned(query, has_sensitivity_adjust)
simple_show_union_struct_unsigned(query, has_hysteresis_threshold)
simple_show_union_struct_unsigned(query, button_count)

/* Control */
show_store_union_struct_unsigned(control, reg_0, button_usage)
show_store_union_struct_unsigned(control, reg_0, filter_mode)
show_store_union_struct_unsigned(control, reg_5, sensitivity_adj)
show_store_union_struct_unsigned(control, reg_6, hysteresis_threshold)

/* repeated register functions */
show_store_repeated_union_struct_unsigned(control, reg_1, int_enabled_button)
show_store_repeated_union_struct_unsigned(control, reg_2, single_button)
show_store_repeated_union_struct_unsigned(control, reg_4, sensitivity_button)


module_rmi_function_driver(function_driver);

MODULE_AUTHOR("Vivian Ly <vly@synaptics.com>");
MODULE_DESCRIPTION("RMI F19 module");
MODULE_LICENSE("GPL");
MODULE_VERSION(RMI_DRIVER_VERSION);