summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/rmi4/rmi_f21.c
blob: c8144c3beefaf41d188366360a3c8179a333b265 (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
/*
 * Copyright (c) 2012-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 rmi_fn_21_data
#define FNUM 21

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

#define FUNCTION_NUMBER 0x21

union f21_2df_query {
	struct {
		/* Query 0 */
		u8 max_force_sensor_count:3;
		u8 f21_2df_query0_b3__6:4;
		u8 has_high_resolution:1;
	} __attribute__((__packed__));
	struct {
		u8 regs[1];
		u16 address;
	} __attribute__((__packed__));
};

union f21_2df_control_0__3 {
	struct {
		/* Control 0 */
		u8 reporting_mode:3;
		u8 no_rezero:1;
		u8 ctl0_reserved:4;

		/* Control 1 */
		u8 force_click_threshold:8;

		/* Control 2 */
		u8 int_en_force_0:1;
		u8 int_en_force_1:1;
		u8 int_en_force_2:1;
		u8 int_en_force_3:1;
		u8 int_en_force_4:1;
		u8 int_en_force_5:1;
		u8 int_en_force_6:1;
		u8 int_en_click:1;

		/* Control 3 */
		u8 force_interrupt_threshold:8;
	} __attribute__((__packed__));
	struct {
		u8 regs[4];
		u16 address;
	} __attribute__((__packed__));
};

struct f21_2df_control_4n {
	/*Control 4.* */
	u8 one_newton_linear_calibration:7;
	u8 use_cfg_cal:1;
} __attribute__((__packed__));

struct f21_2df_control_4 {
		struct f21_2df_control_4n *regs;
		u16 address;
		u8 length;
} __attribute__((__packed__));

struct f21_2df_control_5n {
	/* Control 5 */
	u8 one_newton_nonlinear_calibration;
} __attribute__((__packed__));

struct f21_2df_control_5 {
		struct f21_2df_control_5n *regs;
		u16 address;
		u8 length;
};

struct f21_2df_control_6n {
	/*Control 6.* */
	u8 x_location;
} __attribute__((__packed__));

struct f21_2df_control_6 {
		struct f21_2df_control_6n *regs;
		u16 address;
		u8 length;
};

struct f21_2df_control_7n {
	/*Control 7.* */
	u8 y_location;
} __attribute__((__packed__));

struct f21_2df_control_7 {
		struct f21_2df_control_7n *regs;
		u16 address;
		u8 length;
};

struct f21_2df_control_8n {
	/*Control 8.* */
	u8 transmitter_force_sensor;
} __attribute__((__packed__));

struct f21_2df_control_8 {
		struct f21_2df_control_8n *regs;
		u16 address;
		u8 length;
};

struct f21_2df_control_9n {
	/*Control 9.* */
	u8 receiver_force_sensor;
} __attribute__((__packed__));

struct f21_2df_control_9 {
		struct f21_2df_control_9n *regs;
		u16 address;
		u8 length;
};


#define RMI_F21_NUM_CTRL_REGS 8
struct f21_2df_control {
	/* Control 0-3 */
	union f21_2df_control_0__3 *reg_0__3;

	/* Control 4 */
	struct f21_2df_control_4 *reg_4;

	/* Control 5 */
	struct f21_2df_control_5 *reg_5;

	/* Control 6 */
	struct f21_2df_control_6 *reg_6;

	/* Control 7 */
	struct f21_2df_control_7 *reg_7;

	/* Control 8 */
	struct f21_2df_control_8 *reg_8;

	/* Control 9 */
	struct f21_2df_control_9 *reg_9;
};

union f21_2df_data_2 {
	struct {
		/* Data 2 */
		u8 force_click:1;
		u8 f21_2df_control0_b2__7:7;
	} __attribute__((__packed__));
	struct {
		u8 regs[1];
		u16 address;
	} __attribute__((__packed__));
};

struct f21_2df_data {
	/* Data 0 */
	struct {
		u8 *force_hi_lo;
		u16 address;
	} reg_0__1;

	/* Data 2 */
	union f21_2df_data_2 *reg_2;
};

#define F21_REZERO_CMD 0x01

/* data specific to fn $21 that needs to be kept around */
struct rmi_fn_21_data {
	union f21_2df_query query;
	struct f21_2df_control control;
	struct f21_2df_data data;

	struct mutex control_mutex;
	struct mutex data_mutex;
};

/* Sysfs files */

/* Query sysfs files */


show_union_struct_prototype(max_force_sensor_count)
show_union_struct_prototype(has_high_resolution)

static struct attribute *attrs[] = {
	attrify(max_force_sensor_count),
	attrify(has_high_resolution),
	NULL
};
static struct attribute_group attrs_query = GROUP(attrs);
/* Control sysfs files */

show_store_union_struct_prototype(reporting_mode)
show_store_union_struct_prototype(no_rezero)
show_store_union_struct_prototype(force_click_threshold)
show_store_union_struct_prototype(int_en_force_0)
show_store_union_struct_prototype(int_en_force_1)
show_store_union_struct_prototype(int_en_force_2)
show_store_union_struct_prototype(int_en_force_3)
show_store_union_struct_prototype(int_en_force_4)
show_store_union_struct_prototype(int_en_force_5)
show_store_union_struct_prototype(int_en_force_6)
show_store_union_struct_prototype(int_en_click)
show_store_union_struct_prototype(force_interrupt_threshold)

show_store_union_struct_prototype(use_cfg_cal)
show_store_union_struct_prototype(one_newton_linear_calibration)
show_store_union_struct_prototype(one_newton_nonlinear_calibration)
show_store_union_struct_prototype(x_location)
show_store_union_struct_prototype(y_location)
show_store_union_struct_prototype(transmitter_force_sensor)
show_store_union_struct_prototype(receiver_force_sensor)


static struct attribute *attrs2[] = {
	attrify(reporting_mode),
	attrify(no_rezero),
	attrify(force_click_threshold),
	attrify(int_en_click),
	attrify(force_interrupt_threshold),
	attrify(use_cfg_cal),
	attrify(one_newton_linear_calibration),
	attrify(one_newton_nonlinear_calibration),
	attrify(x_location),
	attrify(y_location),
	attrify(transmitter_force_sensor),
	attrify(receiver_force_sensor),
	NULL
};
static struct attribute_group attrs_control = GROUP(attrs2);

/* Data sysfs files */
show_union_struct_prototype(force)
show_union_struct_prototype(force_click)

static struct attribute *attrs3[] = {
	attrify(force),
	attrify(force_click),
	NULL
};
static struct attribute_group attrs_data = GROUP(attrs3);

/* Command sysfs files */

static ssize_t rmi_fn_21_rezero_store(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count);
DEVICE_ATTR(rezero, RMI_WO_ATTR,
		NULL,
		rmi_fn_21_rezero_store);

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

static int rmi_f21_alloc_memory(struct rmi_function_dev *fn_dev)
{
	struct rmi_fn_21_data *f21;

	f21 = devm_kzalloc(&fn_dev->dev, sizeof(struct rmi_fn_21_data),
			   GFP_KERNEL);
	if (!f21) {
		dev_err(&fn_dev->dev, "Failed to allocate rmi_fn_21_data.\n");
		return -ENOMEM;
	}
	fn_dev->data = f21;

	return 0;
}

static void rmi_f21_free_memory(struct rmi_function_dev *fn_dev)
{
	struct rmi_fn_21_data *f21 = fn_dev->data;
	u8 int_num = f21->query.max_force_sensor_count;
	sysfs_remove_group(&fn_dev->dev.kobj, &attrs_query);
	sysfs_remove_group(&fn_dev->dev.kobj, &attrs_control);
	switch (int_num) {
	case 7:
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(int_en_force_6));
	case 6:
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(int_en_force_5));
	case 5:
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(int_en_force_4));
	case 4:
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(int_en_force_3));
	case 3:
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(int_en_force_2));
	case 2:
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(int_en_force_1));
	case 1:
		sysfs_remove_file(&fn_dev->dev.kobj, attrify(int_en_force_0));
	default:
		break;
	}
	sysfs_remove_group(&fn_dev->dev.kobj, &attrs_data);
	sysfs_remove_group(&fn_dev->dev.kobj, &attrs_command);

}

static int rmi_f21_initialize(struct rmi_function_dev *fn_dev)
{
	struct rmi_fn_21_data *f21 = fn_dev->data;
	int retval = 0;
	u16 next_loc;

	/* Read F21 Query Data */
	f21->query.address = fn_dev->fd.query_base_addr;
	retval = rmi_read_block(fn_dev->rmi_dev, f21->query.address,
		(u8 *)&f21->query, sizeof(f21->query.regs));
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read query registers from %#06x.\n",
				f21->query.address);
		return retval;
	}

	/* Initialize Control Data */
	next_loc = fn_dev->fd.control_base_addr;

	f21->control.reg_0__3 = devm_kzalloc(&fn_dev->dev,
			sizeof(union f21_2df_control_0__3), GFP_KERNEL);
	if (!f21->control.reg_0__3) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_0__3->address = next_loc;
	next_loc += sizeof(f21->control.reg_0__3->regs);
	retval = rmi_read_block(fn_dev->rmi_dev, f21->control.reg_0__3->address,
			f21->control.reg_0__3->regs,
			sizeof(f21->control.reg_0__3->regs));
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read control registers 0-3 from %#06x.\n",
			f21->control.reg_0__3->address);
		return retval;
	}

	f21->control.reg_4 = devm_kzalloc(&fn_dev->dev,
				sizeof(struct f21_2df_control_4), GFP_KERNEL);
	if (!f21->control.reg_4) {
		dev_err(&fn_dev->dev, "Failed to allocate control register.");
		return -ENOMEM;
	}
	f21->control.reg_4->length = f21->query.max_force_sensor_count;
	f21->control.reg_4->regs = devm_kzalloc(&fn_dev->dev,
		sizeof(struct f21_2df_control_4n)*f21->control.reg_4->length,
		GFP_KERNEL);
	if (!f21->control.reg_4->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_4->address = next_loc;
	next_loc += f21->control.reg_4->length;
	retval = rmi_read_block(fn_dev->rmi_dev, f21->control.reg_4->address,
		(u8 *) f21->control.reg_4->regs, f21->control.reg_4->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read Control register 4 from %#06x.\n",
			f21->control.reg_4->address);
		return retval;
	}


	f21->control.reg_5 = devm_kzalloc(&fn_dev->dev,
				sizeof(struct f21_2df_control_5), GFP_KERNEL);
	if (!f21->control.reg_5) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_5->length = f21->query.max_force_sensor_count;
	f21->control.reg_5->regs = devm_kzalloc(&fn_dev->dev,
		sizeof(struct f21_2df_control_5n)*f21->control.reg_5->length,
		GFP_KERNEL);
	if (!f21->control.reg_5->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_5->address = next_loc;
	next_loc += f21->control.reg_5->length;
	retval = rmi_read_block(fn_dev->rmi_dev, f21->control.reg_5->address,
				(u8 *) f21->control.reg_5->regs,
				f21->control.reg_5->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read Control register 5 from %#06x.\n",
				f21->control.reg_5->address);
		return retval;
	}

	f21->control.reg_6 = devm_kzalloc(&fn_dev->dev,
			sizeof(struct f21_2df_control_6), GFP_KERNEL);
	if (!f21->control.reg_6) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_6->length = f21->query.max_force_sensor_count;
	f21->control.reg_6->regs = devm_kzalloc(&fn_dev->dev,
		sizeof(struct f21_2df_control_6n)*f21->control.reg_6->length,
		GFP_KERNEL);
	if (!f21->control.reg_6->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_6->address = next_loc;
	next_loc += f21->control.reg_6->length;
	retval = rmi_read_block(fn_dev->rmi_dev, f21->control.reg_6->address,
			(u8 *) f21->control.reg_6->regs,
				sizeof(f21->control.reg_6->regs));
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read control register 6 from %#06x.\n",
					f21->control.reg_6->address);
		return retval;
	}

	f21->control.reg_7 = devm_kzalloc(&fn_dev->dev,
				sizeof(struct f21_2df_control_7), GFP_KERNEL);
	if (!f21->control.reg_7) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_7->length = f21->query.max_force_sensor_count;
	f21->control.reg_7->regs = devm_kzalloc(&fn_dev->dev,
		sizeof(struct f21_2df_control_7n)*f21->control.reg_7->length,
		GFP_KERNEL);
	if (!f21->control.reg_7->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_7->address = next_loc;
	next_loc += f21->control.reg_7->length;
	retval = rmi_read_block(fn_dev->rmi_dev, f21->control.reg_7->address,
		(u8 *) f21->control.reg_7->regs, f21->control.reg_7->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read control register 7 from %#06x.\n",
				f21->control.reg_7->address);
		return retval;
	}

	f21->control.reg_8 = devm_kzalloc(&fn_dev->dev,
				sizeof(struct f21_2df_control_8), GFP_KERNEL);
	if (!f21->control.reg_8) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_8->length = f21->query.max_force_sensor_count;
	f21->control.reg_8->regs = devm_kzalloc(&fn_dev->dev,
		sizeof(struct f21_2df_control_8n) * f21->control.reg_8->length,
		GFP_KERNEL);
	if (!f21->control.reg_8->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_8->address = next_loc;
	next_loc += f21->control.reg_8->length;
	retval = rmi_read_block(fn_dev->rmi_dev, f21->control.reg_8->address,
		(u8 *) f21->control.reg_8->regs, f21->control.reg_8->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read control register 8 from %#06x.\n",
					f21->control.reg_8->address);
		return retval;
	}

	f21->control.reg_9 = devm_kzalloc(&fn_dev->dev,
			sizeof(struct f21_2df_control_9), GFP_KERNEL);
	if (!f21->control.reg_9) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_9->length = f21->query.max_force_sensor_count;
	f21->control.reg_9->regs = devm_kzalloc(&fn_dev->dev,
			sizeof(struct f21_2df_control_9n)
			*f21->control.reg_9->length, GFP_KERNEL);
	if (!f21->control.reg_9->regs) {
		dev_err(&fn_dev->dev, "Failed to allocate control registers.");
		return -ENOMEM;
	}
	f21->control.reg_9->address = next_loc;
	retval = rmi_read_block(fn_dev->rmi_dev, f21->control.reg_9->address,
		(u8 *) f21->control.reg_9->regs, f21->control.reg_9->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read control register 9 from %#06x.\n",
					f21->control.reg_9->address);
		return retval;
	}


	/* initialize data registers */
	next_loc = fn_dev->fd.data_base_addr;

	f21->data.reg_0__1.force_hi_lo = devm_kzalloc(&fn_dev->dev,
			2 * f21->query.max_force_sensor_count * sizeof(u8),
			GFP_KERNEL);
	if (!f21->data.reg_0__1.force_hi_lo) {
		dev_err(&fn_dev->dev, "Failed to allocate data registers.");
		return -ENOMEM;
	}
	f21->data.reg_0__1.address = next_loc;
	next_loc += 2 * f21->query.max_force_sensor_count;

	f21->data.reg_2 = devm_kzalloc(&fn_dev->dev,
				sizeof(union f21_2df_data_2), GFP_KERNEL);
	if (!f21->data.reg_2) {
		dev_err(&fn_dev->dev, "Failed to allocate data registers.");
		return -ENOMEM;
	}
	f21->control.reg_0__3->address = next_loc;

	mutex_init(&f21->control_mutex);
	return 0;
}

static int rmi_f21_create_sysfs(struct rmi_function_dev *fn_dev)
{
	struct rmi_fn_21_data *f21 = fn_dev->data;
	u8 int_num = f21->query.max_force_sensor_count;
	int rc;

	if (int_num > 7)
		int_num = 7;
	dev_dbg(&fn_dev->dev, "Creating sysfs files.");

	/* 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;
	}
	switch (int_num) {
	case 7:
		rc = sysfs_create_file(&fn_dev->dev.kobj,
				       attrify(int_en_force_6));
		break;
	case 6:
		rc = sysfs_create_file(&fn_dev->dev.kobj,
				       attrify(int_en_force_5));
		break;
	case 5:
		rc = sysfs_create_file(&fn_dev->dev.kobj,
				       attrify(int_en_force_4));
		break;
	case 4:
		rc = sysfs_create_file(&fn_dev->dev.kobj,
				       attrify(int_en_force_3));
		break;
	case 3:
		rc = sysfs_create_file(&fn_dev->dev.kobj,
				       attrify(int_en_force_2));
		break;
	case 2:
		rc = sysfs_create_file(&fn_dev->dev.kobj,
				       attrify(int_en_force_1));
		break;
	case 1:
		rc = sysfs_create_file(&fn_dev->dev.kobj,
				       attrify(int_en_force_0));
		break;
	default:
		rc = 0;
		break;
	}
	if (rc < 0) {
		dev_err(&fn_dev->dev, "Failed to create control sysfs files.\n");
		return rc;
	}
	if (sysfs_create_group(&fn_dev->dev.kobj, &attrs_data) < 0) {
		dev_err(&fn_dev->dev, "Failed to create data sysfs files.\n");
		return -ENODEV;
	}
	if (sysfs_create_group(&fn_dev->dev.kobj, &attrs_command) < 0) {
		dev_err(&fn_dev->dev, "Failed to create command sysfs files.\n");
		return -ENODEV;
	}
	return 0;
}

static int rmi_f21_config(struct rmi_function_dev *fn_dev)
{
	struct rmi_fn_21_data *data = fn_dev->data;
	int retval;
	/* repeated register functions */

	/* Write Control Register values back to device */
	retval = rmi_write_block(fn_dev->rmi_dev,
				data->control.reg_0__3->address,
				data->control.reg_0__3,
				sizeof(data->control.reg_0__3->regs));
	if (retval < 0) {
		dev_err(&fn_dev->dev, "%s : Could not write reg_0_3 to 0x%x\n",
			__func__, data->control.reg_0__3->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, data->control.reg_4->address,
			data->control.reg_4->regs, data->control.reg_4->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "%s : Could not write reg_4 to 0x%x\n",
			__func__, data->control.reg_4->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, data->control.reg_5->address,
			data->control.reg_5->regs, data->control.reg_5->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "%s : Could not write reg_5 to 0x%x\n",
			__func__, data->control.reg_5->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, data->control.reg_6->address,
			(u8 *) data->control.reg_6->regs,
			data->control.reg_6->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "%s : Could not write reg_6 to 0x%x\n",
			__func__, data->control.reg_6->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, data->control.reg_7->address,
			(u8 *) data->control.reg_7->regs,
			data->control.reg_7->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "%s : Could not write reg_7 to 0x%x\n",
			__func__, data->control.reg_7->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, data->control.reg_8->address,
			(u8 *) data->control.reg_8->regs,
			data->control.reg_8->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "%s : Could not write reg_8 to 0x%x\n",
			__func__, data->control.reg_8->address);
		return retval;
	}

	retval = rmi_write_block(fn_dev->rmi_dev, data->control.reg_9->address,
			(u8 *) data->control.reg_9->regs,
			data->control.reg_9->length);
	if (retval < 0) {
		dev_err(&fn_dev->dev, "%s : Could not write reg_9 to 0x%x\n",
			__func__, data->control.reg_9->address);
		return retval;
	}

	return 0;
}

static int rmi_f21_probe(struct rmi_function_dev *fn_dev)
{
	int retval = 0;

	retval = rmi_f21_alloc_memory(fn_dev);
	if (retval < 0)
		goto error_exit;

	retval = rmi_f21_initialize(fn_dev);
	if (retval < 0)
		goto error_exit;

	retval = rmi_f21_create_sysfs(fn_dev);
	if (retval < 0)
		goto error_exit;

	return retval;

error_exit:
	rmi_f21_free_memory(fn_dev);

	return retval;
}

static int rmi_f21_remove(struct rmi_function_dev *fn_dev)
{
	rmi_f21_free_memory(fn_dev);
	return 0;
}

/* sysfs functions */
/* Query */
simple_show_union_struct_unsigned(query, max_force_sensor_count)
simple_show_union_struct_unsigned(query, has_high_resolution)

/* Control */
show_store_union_struct_unsigned(control, reg_0__3, reporting_mode)
show_store_union_struct_unsigned(control, reg_0__3, no_rezero)

show_store_union_struct_unsigned(control, reg_0__3, force_click_threshold)

show_store_union_struct_unsigned(control, reg_0__3, int_en_force_0)
show_store_union_struct_unsigned(control, reg_0__3, int_en_force_1)
show_store_union_struct_unsigned(control, reg_0__3, int_en_force_2)
show_store_union_struct_unsigned(control, reg_0__3, int_en_force_3)
show_store_union_struct_unsigned(control, reg_0__3, int_en_force_4)
show_store_union_struct_unsigned(control, reg_0__3, int_en_force_5)
show_store_union_struct_unsigned(control, reg_0__3, int_en_force_6)
show_store_union_struct_unsigned(control, reg_0__3, int_en_click)
show_store_union_struct_unsigned(control, reg_0__3, force_interrupt_threshold)


/* repeated register functions */
show_store_repeated_union_struct_unsigned(control, reg_4, use_cfg_cal)
show_store_repeated_union_struct_unsigned(control, reg_4,
					  one_newton_linear_calibration)
show_store_repeated_union_struct_unsigned(control, reg_5,
					  one_newton_nonlinear_calibration)
show_store_repeated_union_struct_unsigned(control, reg_6, x_location)
show_store_repeated_union_struct_unsigned(control, reg_7, y_location)
show_store_repeated_union_struct_unsigned(control, reg_8,
					  transmitter_force_sensor)
show_store_repeated_union_struct_unsigned(control, reg_9,
					  receiver_force_sensor)

/* Data */
static ssize_t rmi_fn_21_force_show(struct device *dev,
					struct device_attribute *attr,
					char *buf) {
	struct rmi_function_dev *fn_dev;
	struct FUNCTION_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->query.max_force_sensor_count;
	result = rmi_read_block(fn_dev->rmi_dev, data->data.reg_0__1.address,
			data->data.reg_0__1.force_hi_lo,
			2 * reg_length * sizeof(u8));

	if (result < 0) {
		dev_err(dev, "Could not read regtype at %#06x. Data may be outdated.",
					data->data.reg_0__1.address);
	}
	temp = buf;
	for (i = 0; i < reg_length; i++) {
		result = snprintf(temp, PAGE_SIZE - size, "%d ",
			data->data.reg_0__1.force_hi_lo[i] * (2 << 3)
			+ data->data.reg_0__1.force_hi_lo[i + reg_length]);
		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;
}

show_union_struct_unsigned(data, reg_2, force_click)

/* Command */

static ssize_t rmi_fn_21_rezero_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count) {
	unsigned long val;
	int error, result;
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_21_data *f21;
	struct rmi_driver *driver;
	u8 command;

	fn_dev = to_rmi_function_dev(dev);
	f21 = fn_dev->data;
	driver = fn_dev->rmi_dev->driver;

	/* need to convert the string data to an actual value */
	error = strict_strtoul(buf, 10, &val);
	if (error)
		return error;
	/* Do nothing if not set to 1. This prevents accidental commands. */
	if (val != 1)
		return count;

	command = F21_REZERO_CMD;

	/* Write the command to the command register */
	result = rmi_write_block(fn_dev->rmi_dev, fn_dev->fd.command_base_addr,
						&command, 1);
	if (result < 0) {
		dev_err(dev, "%s : Could not write command to 0x%x\n",
				__func__, fn_dev->fd.command_base_addr);
		return result;
	}
	return count;
}

static struct rmi_function_driver function_driver = {
	.driver = {
		.name = "rmi_f21",
	},
	.func = FUNCTION_NUMBER,
	.remove = rmi_f21_remove,
	.probe = rmi_f21_probe,
	.config = rmi_f21_config,
	.reset = NULL,
	.attention = NULL,
};

module_rmi_function_driver(function_driver);

MODULE_AUTHOR("Daniel Rosenberg <daniel.rosenberg@synaptics.com>");
MODULE_DESCRIPTION("RMI F21 module");
MODULE_LICENSE("GPL");
MODULE_VERSION(RMI_DRIVER_VERSION);