summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/rmi4/rmi_f34.c
blob: f2987ea4eb4e0c843d4a606020e69de869b29bdb (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
/*
 * Copyright (c) 2011, 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.
 */

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

#define BLK_SZ_OFF	3
#define IMG_BLK_CNT_OFF	5
#define CFG_BLK_CNT_OFF	7

#define BLK_NUM_OFF 2
#define FUNCTION_NUMBER 0x34

/* data specific to fn $34 that needs to be kept around */
struct rmi_fn_34_data {
	u8 status;
	u8 cmd;
	u16 bootloaderid;
	u16 blocksize;
	u16 imageblockcount;
	u16 configblockcount;
	u16 blocknum;
	bool inflashprogmode;
	struct mutex attn_mutex;
};

static int f34_read_status(struct rmi_function_dev *fn_dev)
{
	struct rmi_fn_34_data *instance_data = fn_dev->data;
	u16 data_base_addr = fn_dev->fd.data_base_addr;
	u8 status;
	int retval;

	if (instance_data->status == ECONNRESET)
		return instance_data->status;

	/* Read the Fn $34 status from F34_Flash_Data3 to see the previous
	 * commands status. F34_Flash_Data3 will be the address after the
	 * 2 block number registers plus blocksize Data registers.
	 *  inform user space - through a sysfs param. */
	retval = rmi_read(fn_dev->rmi_dev,
			  data_base_addr + instance_data->blocksize +
			  BLK_NUM_OFF, &status);

	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read status from 0x%x\n",
		       data_base_addr + instance_data->blocksize + BLK_NUM_OFF);
		status = 0xff;	/* failure */
	}

	/* set a sysfs value that the user mode can read - only
	 * upper 4 bits are the status. successful is $80, anything
	 * else is failure */
	instance_data->status = status & 0xf0;

	/* put mode into Flash Prog Mode when we successfully do
	 * an Enable Flash Prog cmd. */
	if ((instance_data->status == F34_STATUS_IDLE) &&
		(instance_data->cmd == F34_ENABLE_FLASH_PROG))
		instance_data->inflashprogmode = true;

	return retval;
}

static ssize_t rmi_fn_34_bootloaderid_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;

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

	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->bootloaderid);
}

static ssize_t rmi_fn_34_bootloaderid_store(struct device *dev,
					struct device_attribute *attr,
					const char *buf,
					size_t count)
{
	int error;
	unsigned long val;
	u8 data[2];
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;
	u16 data_base_addr;

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

	/* need to convert the string data to an actual value */
	error = strict_strtoul(buf, 10, &val);

	if (error)
		return error;

	instance_data->bootloaderid = val;

	/* Write the Bootloader ID key data back to the first two Block
	 * Data registers (F34_Flash_Data2.0 and F34_Flash_Data2.1). */
	hstoba(data, (u16)val);
	data_base_addr = fn_dev->fd.data_base_addr;

	error = rmi_write_block(fn_dev->rmi_dev,
				data_base_addr + BLK_NUM_OFF,
				data,
				ARRAY_SIZE(data));

	if (error < 0) {
		dev_err(dev, "%s : Could not write bootloader id to 0x%x\n",
		       __func__, data_base_addr + BLK_NUM_OFF);
		return error;
	}

	return count;
}

static ssize_t rmi_fn_34_blocksize_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;

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

	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->blocksize);
}

static ssize_t rmi_fn_34_imageblockcount_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;

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

	return snprintf(buf, PAGE_SIZE, "%u\n",
			instance_data->imageblockcount);
}

static ssize_t rmi_fn_34_configblockcount_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;

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

	return snprintf(buf, PAGE_SIZE, "%u\n",
			instance_data->configblockcount);
}

static ssize_t rmi_fn_34_status_show(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;
	int retval;

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

	mutex_lock(&instance_data->attn_mutex);
	retval = f34_read_status(fn_dev);
	mutex_unlock(&instance_data->attn_mutex);

	if (retval < 0)
		return retval;

	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->status);
}


static ssize_t rmi_fn_34_status_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;

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

	instance_data->status = 0;

	return 0;
}


static ssize_t rmi_fn_34_cmd_show(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;

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

	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->cmd);
}

static ssize_t rmi_fn_34_cmd_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf,
				size_t count)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;
	unsigned long val;
	u16 data_base_addr;
	int error;

	fn_dev = to_rmi_function_dev(dev);
	instance_data = fn_dev->data;
	data_base_addr = fn_dev->fd.data_base_addr;

	/* need to convert the string data to an actual value */
	error = strict_strtoul(buf, 10, &val);
	if (error)
		return error;

	/* make sure we are in Flash Prog mode for all cmds except the
	 * Enable Flash Programming cmd - otherwise we are in error */
	if ((val != F34_ENABLE_FLASH_PROG) && !instance_data->inflashprogmode) {
		dev_err(dev, "Cannot send command to sensor - not in flash programming mode.\n");
		return -EINVAL;
	}

	instance_data->cmd = val;

	/* Validate command value and (if necessary) write it to the command
	 * register.
	 */
	switch (instance_data->cmd) {
	case F34_ENABLE_FLASH_PROG:
	case F34_ERASE_ALL:
	case F34_ERASE_CONFIG:
	case F34_WRITE_FW_BLOCK:
	case F34_READ_CONFIG_BLOCK:
	case F34_WRITE_CONFIG_BLOCK:
		/* Reset the status to indicate we are in progress on a cmd. */
		/* The status will change when the ATTN interrupt happens
		   and the status of the cmd that was issued is read from
		   the F34_Flash_Data3 register - result should be 0x80 for
		   success - any other value indicates an error */

		/* Issue the command to the device. */
		error = rmi_write(fn_dev->rmi_dev,
				data_base_addr + instance_data->blocksize +
				BLK_NUM_OFF, instance_data->cmd);

		if (error < 0) {
			dev_err(dev, "Could not write command %#04x to %#06x\n",
				instance_data->cmd,
				data_base_addr + instance_data->blocksize +
				BLK_NUM_OFF);
			return error;
		}

		if (instance_data->cmd == F34_ENABLE_FLASH_PROG)
			instance_data->inflashprogmode = true;

		/* set status to indicate we are in progress */
		instance_data->status = F34_STATUS_IN_PROGRESS;
		break;
	default:
		dev_dbg(dev, "RMI4 F34 - unknown command 0x%02lx.\n", val);
		count = -EINVAL;
		break;
	}

	return count;
}

static ssize_t rmi_fn_34_blocknum_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;

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

	return snprintf(buf, PAGE_SIZE, "%u\n", instance_data->blocknum);
}

static ssize_t rmi_fn_34_blocknum_store(struct device *dev,
					struct device_attribute *attr,
					const char *buf,
					size_t count)
{
	int error;
	unsigned long val;
	u8 data[2];
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;
	u16 data_base_addr;

	fn_dev = to_rmi_function_dev(dev);
	instance_data = fn_dev->data;
	data_base_addr = fn_dev->fd.data_base_addr;

	/* need to convert the string data to an actual value */
	error = strict_strtoul(buf, 10, &val);

	if (error)
		return error;

	instance_data->blocknum = val;

	/* Write the Block Number data back to the first two Block
	 * Data registers (F34_Flash_Data_0 and F34_Flash_Data_1). */
	hstoba(data, (u16)val);

	error = rmi_write_block(fn_dev->rmi_dev, data_base_addr,
				data, ARRAY_SIZE(data));

	if (error < 0) {
		dev_err(dev, "Could not write block number %u to %#06x.\n",
		       instance_data->blocknum, data_base_addr);
		return error;
	}

	return count;
}

static ssize_t rmi_fn_34_rescanPDT_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;
	struct rmi_device *rmi_dev;
	struct rmi_driver_data *driver_data;
	struct pdt_entry pdt_entry;
	bool fn01found = false;
	bool fn34found = false;
	unsigned int rescan;
	int irq_count = 0;
	int retval = 0;
	int i;

	/* Rescan of the PDT is needed since issuing the Flash Enable cmd
	 * the device registers for Fn$01 and Fn$34 moving around because
	 * of the change from Bootloader mode to Flash Programming mode
	 * may change to a different PDT with only Fn$01 and Fn$34 that
	 * could have addresses for query, control, data, command registers
	 * that differ from the PDT scan done at device initialization. */

	fn_dev = to_rmi_function_dev(dev);
	instance_data = fn_dev->data;
	rmi_dev = fn_dev->rmi_dev;
	driver_data = dev_get_drvdata(&rmi_dev->dev);

	/* Make sure we are only in Flash Programming mode  - DON'T
	 * ALLOW THIS IN UI MODE. */
	if (instance_data->cmd != F34_ENABLE_FLASH_PROG) {
		dev_err(dev, "Not in flash programming mode, cannot rescan PDT.");
		return -EINVAL;
	}

	/* The only good value to write to this is 1, we allow 0, but with
	 * no effect (this is consistent with the way the command bit works. */
	if (sscanf(buf, "%u", &rescan) != 1)
		return -EINVAL;
	if (rescan < 0 || rescan > 1)
		return -EINVAL;

	/* 0 has no effect, so we skip it entirely. */
	if (rescan) {
		/* rescan the PDT - filling in Fn01 and Fn34 addresses -
		 * this is only temporary - the device will need to be reset
		 * to return the PDT to the normal values. */

		/* mini-parse the PDT - we only have to get Fn$01 and Fn$34 and
		   since we are Flash Programming mode we only have page 0. */
		for (i = PDT_START_SCAN_LOCATION; i >= PDT_END_SCAN_LOCATION;
			i -= sizeof(pdt_entry)) {
			retval = rmi_read_block(rmi_dev, i, (u8 *)&pdt_entry,
					       sizeof(pdt_entry));
			if (retval != sizeof(pdt_entry)) {
				dev_err(dev, "Failed to read PDT entry at %#06x, error = %d.",
						retval, i);
				return retval;
			}

			if ((pdt_entry.function_number == 0x00) ||
				(pdt_entry.function_number == 0xff))
				break;

			dev_dbg(dev, "%s: Found F%.2X\n",
				__func__, pdt_entry.function_number);

			/* f01 found - just fill in the new addresses in
			 * the existing fn_dev. */
			if (pdt_entry.function_number == 0x01) {
				struct rmi_function_dev *f01_dev =
					driver_data->f01_dev;
				fn01found = true;
				f01_dev->fd.query_base_addr =
					pdt_entry.query_base_addr;
				f01_dev->fd.command_base_addr =
				  pdt_entry.command_base_addr;
				f01_dev->fd.control_base_addr =
				  pdt_entry.control_base_addr;
				f01_dev->fd.data_base_addr =
				  pdt_entry.data_base_addr;
				f01_dev->fd.function_number =
				  pdt_entry.function_number;
				f01_dev->fd.interrupt_source_count =
				  pdt_entry.interrupt_source_count;
				f01_dev->num_of_irqs =
				  pdt_entry.interrupt_source_count;
				f01_dev->irq_pos = irq_count;

				irq_count += f01_dev->num_of_irqs;

				if (fn34found)
					break;
			}

			/* f34 found - just fill in the new addresses in
			 * the existing fn_dev. */
			if (pdt_entry.function_number == 0x34) {
				fn34found = true;
				fn_dev->fd.query_base_addr =
				  pdt_entry.query_base_addr;
				fn_dev->fd.command_base_addr =
				  pdt_entry.command_base_addr;
				fn_dev->fd.control_base_addr =
				  pdt_entry.control_base_addr;
				fn_dev->fd.data_base_addr =
				  pdt_entry.data_base_addr;
				fn_dev->fd.function_number =
				  pdt_entry.function_number;
				fn_dev->fd.interrupt_source_count =
				  pdt_entry.interrupt_source_count;
				fn_dev->num_of_irqs =
				  pdt_entry.interrupt_source_count;
				fn_dev->irq_pos = irq_count;

				irq_count += fn_dev->num_of_irqs;

				if (fn01found)
					break;
			}

		}

		if (!fn01found || !fn34found) {
			dev_err(dev, "F01 or F34 not found in PDT rescan.\n");
			return -EINVAL;
		}
	}

	return count;
}

static struct device_attribute attrs[] = {
	__ATTR(status, RMI_RW_ATTR,
	       rmi_fn_34_status_show, rmi_fn_34_status_store),

	/* Also, sysfs will need to have a file set up to distinguish
	 * between commands - like Config write/read, Image write/verify. */
	__ATTR(cmd, RMI_RW_ATTR,
	       rmi_fn_34_cmd_show, rmi_fn_34_cmd_store),
	__ATTR(bootloaderid, RMI_RW_ATTR,
	       rmi_fn_34_bootloaderid_show, rmi_fn_34_bootloaderid_store),
	__ATTR(blocksize, RMI_RO_ATTR,
	       rmi_fn_34_blocksize_show, NULL),
	__ATTR(imageblockcount, RMI_RO_ATTR,
	       rmi_fn_34_imageblockcount_show, NULL),
	__ATTR(configblockcount, RMI_RO_ATTR,
	       rmi_fn_34_configblockcount_show, NULL),
	__ATTR(blocknum, RMI_RW_ATTR,
	       rmi_fn_34_blocknum_show, rmi_fn_34_blocknum_store),
	__ATTR(rescanPDT, RMI_WO_ATTR,
	       NULL, rmi_fn_34_rescanPDT_store)
};

static ssize_t rmi_fn_34_data_read(struct file *data_file,
				struct kobject *kobj,
				struct bin_attribute *attributes,
				char *buf,
				loff_t pos,
				size_t count)
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;
	u16 data_base_addr;
	int error;

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

	data_base_addr = fn_dev->fd.data_base_addr;

	if (count != instance_data->blocksize) {
		dev_err(dev, "Incorrect F34 block size %d. Expected size %d.\n",
				count, instance_data->blocksize);
		return -EINVAL;
	}

	/* Read the data from flash into buf.  The app layer will be blocked
	 * at reading from the sysfs file.  When we return the count (or
	 * error if we fail) the app will resume. */
	error = rmi_read_block(fn_dev->rmi_dev, data_base_addr + BLK_NUM_OFF,
			(u8 *)buf, count);

	if (error < 0) {
		dev_err(dev, "Could not read data from %#06x\n",
				data_base_addr + BLK_NUM_OFF);
		return error;
	}

	return count;
}

static ssize_t rmi_fn_34_data_write(struct file *data_file,
				struct kobject *kobj,
				struct bin_attribute *attributes,
				char *buf,
				loff_t pos,
				size_t count)
{
	struct device *dev = container_of(kobj, struct device, kobj);
	struct rmi_function_dev *fn_dev;
	struct rmi_fn_34_data *instance_data;
	u16 data_base_addr;
	int error;

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

	data_base_addr = fn_dev->fd.data_base_addr;

	/* Write the data from buf to flash. The app layer will be
	 * blocked at writing to the sysfs file.  When we return the
	 * count (or error if we fail) the app will resume. */

	if (count != instance_data->blocksize) {
		dev_err(dev, "Incorrect F34 block size %d. Expected size %d.\n",
			count, instance_data->blocksize);
		return -EINVAL;
	}

	/* Write the data block - only if the count is non-zero  */
	if (count) {
		error = rmi_write_block(fn_dev->rmi_dev,
				data_base_addr + BLK_NUM_OFF,
				(u8 *)buf, count);

		if (error < 0) {
			dev_err(dev, "%s : Could not write block data to 0x%x\n",
				__func__, data_base_addr + BLK_NUM_OFF);
			return error;
		}
	}

	return count;
}


struct bin_attribute dev_attr_data = {
	.attr = {
		 .name = "data",
		 .mode = 0666},
	.size = 0,
	.read = rmi_fn_34_data_read,
	.write = rmi_fn_34_data_write,
};

static int rmi_f34_alloc_memory(struct rmi_function_dev *fn_dev)
{
	struct rmi_fn_34_data *f34;

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

	return 0;
}

static int rmi_f34_initialize(struct rmi_function_dev *fn_dev)
{
	struct rmi_device *rmi_dev = fn_dev->rmi_dev;
	struct rmi_device_platform_data *pdata;
	int retval = 0;
	struct rmi_fn_34_data *f34 = fn_dev->data;
	u16 query_base_addr;
	u16 control_base_addr;
	u8 buf[2];

	pdata = to_rmi_platform_data(rmi_dev);

	mutex_init(&f34->attn_mutex);

	/* get the Bootloader ID and Block Size. */
	query_base_addr = fn_dev->fd.query_base_addr;
	control_base_addr = fn_dev->fd.control_base_addr;

	retval = rmi_read_block(fn_dev->rmi_dev, query_base_addr, buf,
			ARRAY_SIZE(buf));

	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read bootloaderid from 0x%04x.\n",
			query_base_addr);
		return retval;
	}

	f34->bootloaderid = batohs(buf);

	retval = rmi_read_block(fn_dev->rmi_dev, query_base_addr + BLK_SZ_OFF,
				buf, ARRAY_SIZE(buf));

	if (retval < 0) {
		dev_err(&fn_dev->dev, "Could not read block size from 0x%04x, error=%d.\n",
			query_base_addr + BLK_SZ_OFF, retval);
		return retval;
	}
	f34->blocksize = batohs(buf);

	/* Get firmware image block count and store it in the instance data */
	retval = rmi_read_block(fn_dev->rmi_dev,
				query_base_addr + IMG_BLK_CNT_OFF,
				buf, ARRAY_SIZE(buf));

	if (retval < 0) {
		dev_err(&fn_dev->dev, "Couldn't read image block count from 0x%x, error=%d.\n",
			query_base_addr + IMG_BLK_CNT_OFF, retval);
		return retval;
	}
	f34->imageblockcount = batohs(buf);

	/* Get config block count and store it in the instance data */
	retval = rmi_read_block(fn_dev->rmi_dev, query_base_addr + 7, buf,
			ARRAY_SIZE(buf));

	if (retval < 0) {
		dev_err(&fn_dev->dev, "Couldn't read config block count from 0x%x, error=%d.\n",
			query_base_addr + CFG_BLK_CNT_OFF, retval);
		return retval;
	}
	f34->configblockcount = batohs(buf);

	return 0;
}

static int rmi_f34_create_sysfs(struct rmi_function_dev *fn_dev)
{
	int attr_count = 0;
	int rc;

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

	/* We need a sysfs file for the image/config block to write or read.
	 * Set up sysfs bin file for binary data block. Since the image is
	 * already in our format there is no need to convert the data for
	 * endianess. */
	rc = sysfs_create_bin_file(&fn_dev->dev.kobj,
				&dev_attr_data);
	if (rc < 0) {
		dev_err(&fn_dev->dev, "Failed to create sysfs file for F34 data, error=%d.\n",
			rc);
		return -ENODEV;
	}

	/* Set up sysfs device attributes. */
	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
		if (sysfs_create_file
		    (&fn_dev->dev.kobj, &attrs[attr_count].attr) < 0) {
			dev_err(&fn_dev->dev, "Failed to create sysfs file for %s.",
			     attrs[attr_count].attr.name);
			rc = -ENODEV;
			goto err_remove_sysfs;
		}
	}

	return 0;

err_remove_sysfs:
	sysfs_remove_bin_file(&fn_dev->dev.kobj, &dev_attr_data);

	for (attr_count--; attr_count >= 0; attr_count--)
		sysfs_remove_file(&fn_dev->dev.kobj,
				  &attrs[attr_count].attr);
	return rc;
}

static int rmi_f34_probe(struct rmi_function_dev *fn_dev)
{
	int retval;

	dev_info(&fn_dev->dev, "Intializing f34 values.");

	/* init instance data, fill in values and create any sysfs files */
	retval = rmi_f34_alloc_memory(fn_dev);
	if (retval < 0)
		return retval;

	retval = rmi_f34_initialize(fn_dev);
	if (retval < 0)
		return retval;

	retval = rmi_f34_create_sysfs(fn_dev);
	if (retval < 0)
		return retval;

	return 0;
}

static int rmi_f34_config(struct rmi_function_dev *fn_dev)
{
	/* for this function we should do nothing here */
	return 0;
}


static int rmi_f34_reset(struct rmi_function_dev *fn_dev)
{
	struct  rmi_fn_34_data  *instance_data = fn_dev->data;

	instance_data->status = ECONNRESET;

	return 0;
}

static int rmi_f34_remove(struct rmi_function_dev *fn_dev)
{
	int attr_count;

	sysfs_remove_bin_file(&fn_dev->dev.kobj, &dev_attr_data);

	for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++)
		sysfs_remove_file(&fn_dev->dev.kobj, &attrs[attr_count].attr);

	return 0;
}

int rmi_f34_attention(struct rmi_function_dev *fn_dev,
					unsigned long *irq_bits)
{
	int retval;
	struct rmi_fn_34_data *data = fn_dev->data;

	mutex_lock(&data->attn_mutex);
	retval = f34_read_status(fn_dev);
	mutex_unlock(&data->attn_mutex);
	return retval;
}

static struct rmi_function_driver function_driver = {
	.driver = {
		.name = "rmi_f34",
	},
	.func = FUNCTION_NUMBER,
	.probe = rmi_f34_probe,
	.remove = rmi_f34_remove,
	.config = rmi_f34_config,
	.reset = rmi_f34_reset,
	.attention = rmi_f34_attention,
};

module_rmi_function_driver(function_driver);

MODULE_AUTHOR("William Manson <wmanson@synaptics.com");
MODULE_DESCRIPTION("RMI F34 module");
MODULE_LICENSE("GPL");
MODULE_VERSION(RMI_DRIVER_VERSION);