aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-msm/pp2s.c
blob: 569704dbd2009aa7791acdc69938326d45505dad (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
916
917
918
919
920
921
922
923
924
925
926
927
/* Copyright (c) 2014, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/of.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <asm/io.h>
#include <asm/div64.h>

#define PP2S_MODULE_NAME "PP2S"
#define WC_MODULE_NAME   "wallclock"

/*
 * Logging macros...
 */
#define LOG_DRVR_DEBUG(args...) pr_debug(args)
#define LOG_DRVR_ERR(args...)   pr_err(args)

/*
 * The following defines how many sysfs nodes the driver is creating.
 */
#define NUM_PP2S_SYSFS_REG_NODES 1

#define NUM_WC_SYSFS_REG_NODES 8

#define ATTR_PTR_SIZE sizeof(struct attribute *)

/*
 * The following deal with register locations in virtual memory.
 *
 * More specifically, we're mapping in base addresses and a length
 * (that come from the device tree) during the probe and then the
 * registers are just offsets from their respective base...
 */
#define BASEADDR_PLUS(base, off)                        \
	((void *) ((u32) (base) + (u32) (off)))

#define WC_SECS_ADDR       BASEADDR_PLUS(time_bank,   0x00000014)
#define WC_NSECS_ADDR      BASEADDR_PLUS(time_bank,   0x00000018)
#define WC_CONTROL_ADDR    BASEADDR_PLUS(cntrl_bank1, 0x00000020)
#define WC_PULSECNT_ADDR   BASEADDR_PLUS(cntrl_bank1, 0x00000024)
#define WC_CLOCKCNT_ADDR   BASEADDR_PLUS(cntrl_bank1, 0x0000002C)
#define WC_SNAPSHOT_ADDR   BASEADDR_PLUS(cntrl_bank1, 0x00000028)
#define WC_SFN_ADDR        BASEADDR_PLUS(cntrl_bank2, 0x00000000)

/*
 * Number of bits in the nanosecond register to be used for the
 * storage of leap seconds...
 */
#define BITS_FOR_LEAP 6

/*
 * The rate the free running clock runs at...
 */
#define CLK_RATE 122880000

/*
 * The difference (in seconds) between the unix and gps epochs...
 */
#define UNIX_EPOCH_TO_GPS_EPOCH_GAP 315964800

/*
 * These macros assist with creating sysfs attributes...
 */
#define MAKE_RO_ATTR(_name, _ptr, _index)                             \
	(_ptr)->kobj_attr_##_name.attr.name = __stringify(_name);         \
	(_ptr)->kobj_attr_##_name.attr.mode = S_IRUSR;                    \
	(_ptr)->kobj_attr_##_name.show = read_##_name;                    \
	(_ptr)->kobj_attr_##_name.store = NULL;                           \
	sysfs_attr_init(&(_ptr)->kobj_attr_##_name.attr);                 \
	(_ptr)->attr_grp.attrs[_index] = &(_ptr)->kobj_attr_##_name.attr;

#define MAKE_RW_ATTR(_name, _ptr, _index)                             \
	(_ptr)->kobj_attr_##_name.attr.name = __stringify(_name);         \
	(_ptr)->kobj_attr_##_name.attr.mode = S_IRUSR|S_IWUSR;            \
	(_ptr)->kobj_attr_##_name.show = read_##_name;                    \
	(_ptr)->kobj_attr_##_name.store = write_##_name;                  \
	sysfs_attr_init(&(_ptr)->kobj_attr_##_name.attr);                 \
	(_ptr)->attr_grp.attrs[_index] = &(_ptr)->kobj_attr_##_name.attr;

/*
 * The following will break any pending poll/select(s) on a sysfs
 * node...
 */
#define RELEASE_SELECT_ON_ATTR(_ptr, _attr)                     \
	sysfs_notify((_ptr)->kobj, NULL, __stringify(_attr))

/*
 * The following is for the creation of pp2s sysfs entries.
 */
struct pp2s_sysfs_t {
	struct kobject        *kobj;
	struct attribute_group attr_grp;

	struct kobj_attribute  kobj_attr_pp2s_cnt;
	unsigned long long     pp2s_cnt;
};

/*
 * The following is for the creation of wallclock sysfs entries.
 */
struct wc_sysfs_t {
	struct kobject        *kobj;
	struct attribute_group attr_grp;
	struct kobj_attribute  kobj_attr_cntrl_reg;
	u32                    cntrl_reg;
	struct kobj_attribute  kobj_attr_secs_reg;
	u32                    secs_reg;
	struct kobj_attribute  kobj_attr_nsecs_reg;
	u32                    nsecs_reg;
	struct kobj_attribute  kobj_attr_pulsecnt_reg;
	u32                    pulsecnt_reg;
	struct kobj_attribute  kobj_attr_clockcnt_reg;
	u32                    clockcnt_reg;
	struct kobj_attribute  kobj_attr_snapshot_reg;
	u32                    snapshot_reg;
	struct kobj_attribute  kobj_attr_reset_time;
	u32                    reset_time;
	struct kobj_attribute  kobj_attr_sfn_reg;
	u32                    sfn_reg;
};

/*
 * Some rudimentary internal state...
 */
static int                      pp2s_irq;
static struct pp2s_sysfs_t      pp2s_sysfs;
static struct workqueue_struct *workqueue;
static struct work_struct       bottom_work;
static struct wc_sysfs_t        wc_sysfs;
static int                      time_is_set = -1;

/*
 * The following will be set to point at the two regions where the
 * wall clock registers in question live.
 *
 * More specifically:
 *
 *   *time_bank : Will be used to point at the region which holds two
 *                time registers (ie. the base time or "epoch" when
 *                the time was set), and
 *
 *   *cntrl_bank1 : A set of registers used to make current wall clock
 *                  time calculations relative to the base time...
 *
 *   *cntrl_bank2 : Similar to cntrl_bank1
 */
static void *time_bank;
static void *cntrl_bank1;
static void *cntrl_bank2;

/*
 * The following function takes GPS time from the FTM and converts it
 * to unix time.
 *
 * OF NOTE:
 *
 *   Upon hard sync, TFCS seeds the FTM time registers used by this
 *   function with GPS time.
 *
 *   GPS time is stored in the registers in the following way:
 *
 *   1) Two registers, referred to as the base time registers, are
 *      seeded with the time that the first PP2S arrived.  These
 *      registers never change value once set.
 *
 *   2) Two other register, "pulse count" and "snap shot," hold
 *      dynamic values that, when combined with the base time, yield
 *      the actual GPS time.
 *
 *   The "pulse count" count register holds the number of PP2S pulses
 *   that have transpired prior to the register being read.  The "snap
 *   shot" register is a latch register whose value is set by simply
 *   doing a read of the "pulse count" register.  The value being
 *   latched is that of the "clock register" which is a free running
 *   clock that's running at 122.88 MHz.  Its value goes back to 0 and
 *   free runs again on every PP2S pusle.
 *
 * Given the above, we initally calculate GPS time which then gets
 * converted to unix time thusly:
 */
static void gps_to_unix_time(
	struct timespec *ts_ptr)
{
	if (ts_ptr) {
		u32 pulses = readl_relaxed(WC_PULSECNT_ADDR);
		u32 clkCnt = readl_relaxed(WC_SNAPSHOT_ADDR);
		u32 secs   = readl_relaxed(WC_SECS_ADDR);
		u32 nsecs  = readl_relaxed(WC_NSECS_ADDR);
		u32 leaps;
		u32 rmndr, unused;
		u64 q;

		/*
		 * NOTE:
		 *
		 *   There is some bit manipulation of the nano second value
		 *   below.  This is because the nsecs register is doing duel
		 *   duty.  It not only stores nano seconds, but also, the first
		 *   BITS_FOR_LEAP low order bits are used to store leap
		 *   seconds.
		 *   The remaining high order bits hold a scaled (by a factor of
		 *   16) version of nano seconds.
		 */
		leaps = nsecs & ((1 << BITS_FOR_LEAP) - 1);

		nsecs >>= BITS_FOR_LEAP;
		nsecs <<= 4;

		/*
		 * Convert PP2S pulses to seconds and add that in.
		 */
		secs += (pulses * 2);

		/*
		 * Now convert the free running clock into seconds and
		 * nanoseconds
		 * and add that in as well...
		 */
		secs += (u32) div_u64_rem((u64) clkCnt, CLK_RATE, &rmndr);

		q = div_u64_rem((u64) NSEC_PER_SEC, CLK_RATE, &unused);

		nsecs += (u32) ((u64) rmndr * q);

		/*
		 * And finally, the actual conversion from GPS to unix time...
		 */
		ts_ptr->tv_sec  = secs + (UNIX_EPOCH_TO_GPS_EPOCH_GAP - leaps);
		ts_ptr->tv_nsec = nsecs;

		if (ts_ptr->tv_sec < 0 || ts_ptr->tv_nsec < 0) {
			ts_ptr->tv_sec = 0;
			ts_ptr->tv_nsec = 0;
		}
	}
}

/*
 * Given a register address and a storage location address, this
 * routine will read a value from the register address and store it at
 * the location address.
 */
static ssize_t register_read(
	const char *caller_ptr,
	void       *reg_addr,
	char       *valfromreg_ptr)
{
	u32 val = readl_relaxed(reg_addr);

	val = scnprintf(valfromreg_ptr, PAGE_SIZE, "%u", val);

	LOG_DRVR_DEBUG(
		"%s -> read %s (from addr %p) with %u bytes\n",
		caller_ptr, valfromreg_ptr, reg_addr, val);

	return val;
}

/*
 * Given a register address and an address of a value, this routine
 * will take the value and copy it to the register address...
 */
static ssize_t register_write(
	const char *caller_ptr,
	void       *reg_addr,
	const char *valtoreg_ptr)
{
	u32 val = 0;

	sscanf(valtoreg_ptr, "%u", &val);

	writel_relaxed(val, reg_addr);

	LOG_DRVR_DEBUG(
		"%s -> wrote %u (to addr %p) with %u bytes\n",
		caller_ptr, val, reg_addr, sizeof(val));

	return strlen(valtoreg_ptr);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the cntrl_reg sysfs node...
 */
static ssize_t read_cntrl_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_read(__func__, WC_CONTROL_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the secs_reg sysfs node...
 */
static ssize_t read_secs_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_read(__func__, WC_SECS_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a
 * write on the secs_reg sysfs node...
 */
static ssize_t write_secs_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	const char            *buf,
	size_t                 count)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_write(__func__, WC_SECS_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the nsecs_reg sysfs node...
 */
static ssize_t read_nsecs_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_read(__func__, WC_NSECS_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a
 * write on the nsecs_reg sysfs node...
 */
static ssize_t write_nsecs_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	const char            *buf,
	size_t                 count)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_write(__func__, WC_NSECS_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the pulsecnt_reg sysfs node...
 */
static ssize_t read_pulsecnt_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_read(__func__, WC_PULSECNT_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the clockcnt_reg sysfs node...
 */
static ssize_t read_clockcnt_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_read(__func__, WC_CLOCKCNT_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the snapshot_reg sysfs node...
 */
static ssize_t read_snapshot_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_read(__func__, WC_SNAPSHOT_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the reset_time sysfs node...
 */
static ssize_t read_reset_time(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return scnprintf(buf,
			 PAGE_SIZE,
			 "linux time is %s",
			 (time_is_set > 0) ? "set" : "not set");
}

/*
 * The sysfs routine which is called when an application issues a
 * write on the reset_time sysfs node...
 */
static ssize_t write_reset_time(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	const char            *buf,
	size_t                 count)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	time_is_set = -1;

	return 1;
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the sfn_reg sysfs node...
 */
static ssize_t read_sfn_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_read(__func__, WC_SFN_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a
 * write on the sfn_reg sysfs node...
 */
static ssize_t write_sfn_reg(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	const char            *buf,
	size_t                 count)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return register_write(__func__, WC_SFN_ADDR, buf);
}

/*
 * The sysfs routine which is called when an application issues a read
 * on the pp2s_cnt sysfs node...
 */
static ssize_t read_pp2s_cnt(
	struct kobject        *kobj,
	struct kobj_attribute *attr,
	char                  *buf)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	return read_pulsecnt_reg(kobj, attr, buf);
}

/*
 * The pp2s interrupt handler top half schedules the following to be
 * run upon reception of the pp2s interrupt, so that any polls or
 * selects on the pp2s_cnt sysfs node can be released...
 */
static void pp2s_intr_bottom(
	struct work_struct *work)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	/*
	 * On the first PP2S interrupt, GPS time will be set in the FTM
	 * and we'll have the first pass through this function.  At that
	 * point, it is required to take GPS time, convert it to unix
	 * time, and to set it in the kernel, hence...
	 */
	if (time_is_set <= 0) {
		struct timespec ts;
		gps_to_unix_time(&ts);
		time_is_set = !do_settimeofday(&ts);
		if (!time_is_set)
			LOG_DRVR_ERR("do_settimeofday() failed\n");
	}

	RELEASE_SELECT_ON_ATTR(&pp2s_sysfs, pp2s_cnt);
}

/*
 * The following is called upon receipt of the pp2s interrupt.  It
 * simply schedules pp2s_intr_bottom to be called...
 */
static irqreturn_t pp2s_intr_top(
	int   irq,
	void *arbDatPtr)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	queue_work(workqueue, &bottom_work);

	pp2s_sysfs.pp2s_cnt++;

	return IRQ_HANDLED;
}

/*
 * The following will probe the device tree file for this driver's IRQ
 * number.  Once found, it will be enabled after a top half interrupt
 * handler is associated with the IRQ.
 *
 * This routine will also create the pp2s_cnt sysfs node...
 */
static int pp2s_probe(
	struct platform_device *pdev)
{
	struct resource *r;
	int              ret = 0;

	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "pp2s_irq");

	if (r == 0) {
		LOG_DRVR_ERR(
			"%s: platform_get_resource_byname() failed\n",
			__func__);
		ret = -ENODEV;
		goto perr0;
	}

	pp2s_irq = r->start;

	LOG_DRVR_DEBUG(
		"%s: retrieved irq (%d) from device tree\n",
		__func__, pp2s_irq);

	ret = request_irq(
		pp2s_irq,
		pp2s_intr_top,
		IRQF_TRIGGER_RISING,
		PP2S_MODULE_NAME,
		0);

	if (ret != 0) {
		LOG_DRVR_ERR(
			"%s: request_irq() failed\n",
			__func__);
		goto perr0;
	}

	/*
	 * Create the sysfs obj...
	 */
	pp2s_sysfs.kobj = kobject_create_and_add("pp2s", kernel_kobj);

	if (pp2s_sysfs.kobj == 0) {
		ret = -ENOMEM;
		goto perr1;
	}

	/*
	 * Allocate space for sysfs attrs...
	 */
	pp2s_sysfs.attr_grp.attrs =
		kzalloc(ATTR_PTR_SIZE * NUM_PP2S_SYSFS_REG_NODES, GFP_KERNEL);

	if (pp2s_sysfs.attr_grp.attrs == 0) {
		ret = -ENOMEM;
		goto perr2;
	}

	/*
	 * Add sysfs attr...
	 */
	MAKE_RO_ATTR(pp2s_cnt, &pp2s_sysfs, 0);

	/*
	 * Create sysfs group...
	 */
	ret = sysfs_create_group(pp2s_sysfs.kobj, &pp2s_sysfs.attr_grp);

	if (ret != 0)
		goto perr3;

	return ret;

perr3:
	kzfree(pp2s_sysfs.attr_grp.attrs);
	pp2s_sysfs.attr_grp.attrs = 0;
perr2:
	kobject_put(pp2s_sysfs.kobj);
	pp2s_sysfs.kobj = 0;
perr1:
	disable_irq(pp2s_irq);
	free_irq(pp2s_irq, 0);
perr0:
	return ret;
}

/*
 * This routine will read the device tree file and initialize a few
 * important wallclock data structures...
 */
static int wc_probe(
	struct platform_device *pdev)
{
	struct resource *r;
	int              ret = 0;

	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	/*
	 * Find the address of the time bank and map it in...
	 */
	r = platform_get_resource_byname(
		pdev, IORESOURCE_MEM, "wallclock_time_bank");

	if (r == 0) {
		LOG_DRVR_ERR(
			"%s: platform_get_resource_byname(time_bank) failed\n",
			__func__);
		ret = -ENODEV;
		goto werr0;
	}

	LOG_DRVR_DEBUG(
		"%s: time_bank start(%d) size(%d)\n",
		__func__, r->start, resource_size(r));

	time_bank = ioremap_nocache(r->start, resource_size(r));

	if (time_bank == 0) {
		LOG_DRVR_ERR(
			"%s: ioremap(time_bank) failed\n",
			__func__);
		ret = -ENOMEM;
		goto werr0;
	}

	/*
	 * Find the address of the 1st control bank and map it in...
	 */
	r = platform_get_resource_byname(
		pdev, IORESOURCE_MEM, "wallclock_cntrl_bank1");

	if (r == 0) {
		LOG_DRVR_ERR(
			"%s: platform_get_resource_byname(cntrl_bank1) failed\n",
			__func__);
		ret = -ENODEV;
		goto werr1;
	}

	LOG_DRVR_DEBUG(
		"%s: cntrl_bank1 start(%d) size(%d)\n",
		__func__, r->start, resource_size(r));

	cntrl_bank1 = ioremap_nocache(r->start, resource_size(r));

	if (cntrl_bank1 == 0) {
		LOG_DRVR_ERR(
			"%s: ioremap(cntrl_bank1) failed\n",
			__func__);
		ret = -ENOMEM;
		goto werr1;
	}

	/*
	 * Find the address of the 2nd control bank and map it in...
	 */
	r = platform_get_resource_byname(
		pdev, IORESOURCE_MEM, "wallclock_cntrl_bank2");

	if (r == 0) {
		LOG_DRVR_ERR(
			"%s: platform_get_resource_byname(cntrl_bank2) failed\n",
			__func__);
		ret = -ENODEV;
		goto werr2;
	}

	LOG_DRVR_DEBUG(
		"%s: cntrl_bank2 start(%d) size(%d)\n",
		__func__, r->start, resource_size(r));

	cntrl_bank2 = ioremap_nocache(r->start, resource_size(r));

	if (cntrl_bank2 == 0) {
		LOG_DRVR_ERR(
			"%s: ioremap(cntrl_bank2) failed\n",
			__func__);
		ret = -ENOMEM;
		goto werr2;
	}

	/*
	 * Create the sysfs obj...
	 */
	wc_sysfs.kobj = kobject_create_and_add("wall_clock", kernel_kobj);

	if (wc_sysfs.kobj == 0) {
		ret = -ENOMEM;
		goto werr3;
	}

	/*
	 * Allocate space for sysfs attrs...
	 */
	wc_sysfs.attr_grp.attrs =
		kzalloc(ATTR_PTR_SIZE * NUM_WC_SYSFS_REG_NODES, GFP_KERNEL);

	if (wc_sysfs.attr_grp.attrs == 0) {
		ret = -ENOMEM;
		goto werr4;
	}

	/*
	 * Add sysfs attrs...
	 */
	MAKE_RO_ATTR(clockcnt_reg, &wc_sysfs, 0);
	MAKE_RO_ATTR(cntrl_reg,    &wc_sysfs, 1);
	MAKE_RO_ATTR(pulsecnt_reg, &wc_sysfs, 2);
	MAKE_RO_ATTR(snapshot_reg, &wc_sysfs, 3);

	MAKE_RW_ATTR(secs_reg,     &wc_sysfs, 4);
	MAKE_RW_ATTR(nsecs_reg,    &wc_sysfs, 5);
	MAKE_RW_ATTR(reset_time,   &wc_sysfs, 6);
	MAKE_RW_ATTR(sfn_reg,      &wc_sysfs, 7);

	/*
	 * Create sysfs group...
	 */
	ret = sysfs_create_group(wc_sysfs.kobj, &wc_sysfs.attr_grp);

	if (ret != 0)
		goto werr5;

	return ret;

werr5:
	kzfree(wc_sysfs.attr_grp.attrs);
	wc_sysfs.attr_grp.attrs = 0;
werr4:
	kobject_put(wc_sysfs.kobj);
	wc_sysfs.kobj = 0;
werr3:
	iounmap(cntrl_bank2);
	cntrl_bank2 = 0;
werr2:
	iounmap(cntrl_bank1);
	cntrl_bank1 = 0;
werr1:
	iounmap(time_bank);
	time_bank = 0;
werr0:
	return ret;
}

/*
 * Free up pp2s resources upon driver removal.
 */
static int pp2s_remove(
	struct platform_device *pdev)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	kzfree(pp2s_sysfs.attr_grp.attrs);
	pp2s_sysfs.attr_grp.attrs = 0;

	kobject_put(pp2s_sysfs.kobj);
	pp2s_sysfs.kobj = 0;

	disable_irq(pp2s_irq);
	free_irq(pp2s_irq, 0);

	return 0;
}

/*
 * Free up wallclock resources upon driver removal.
 */
static int wc_remove(
	struct platform_device *pdev)
{
	LOG_DRVR_DEBUG("Entering %s\n", __func__);

	kzfree(wc_sysfs.attr_grp.attrs);
	wc_sysfs.attr_grp.attrs = 0;

	kobject_put(wc_sysfs.kobj);
	wc_sysfs.kobj = 0;

	iounmap(time_bank);
	time_bank = 0;

	iounmap(cntrl_bank1);
	cntrl_bank1 = 0;

	iounmap(cntrl_bank2);
	cntrl_bank2 = 0;

	return 0;
}

/*
 * All of the following are standard platform driver initialization
 * calls...
 */
static struct of_device_id pp2s_match_table[] = {
	{ .compatible = "qcom,pp2s", },
	{}
};

static struct platform_driver pp2s_driver = {
	.probe  = pp2s_probe,
	.remove = pp2s_remove,
	.driver = {
		.name           = PP2S_MODULE_NAME,
		.owner          = THIS_MODULE,
		.of_match_table = pp2s_match_table,
	},
};

static struct of_device_id wc_match_table[] = {
	{ .compatible = "qcom,wallclock", },
	{}
};

static struct platform_driver wc_driver = {
	.probe  = wc_probe,
	.remove = wc_remove,
	.driver = {
		.name           = WC_MODULE_NAME,
		.owner          = THIS_MODULE,
		.of_match_table = wc_match_table,
	},
};

/*
 * Called when device driver module loaded...
 */
static int __init pp2s_init(void)
{
	int ret;

	LOG_DRVR_DEBUG(
		"%s: PP2S interrupt notifier initialization\n",
		__func__);

	memset(&pp2s_sysfs, 0, sizeof(pp2s_sysfs));

	workqueue = create_singlethread_workqueue("pp2s_wq");

	if (workqueue == 0) {
		LOG_DRVR_ERR(
			"%s: create_singlethread_workqueue() failed\n",
			__func__);
		return -ENOMEM;
	}

	INIT_WORK(&bottom_work, pp2s_intr_bottom);

	ret = platform_driver_register(&pp2s_driver);

	if (ret) {
		LOG_DRVR_ERR(
		  "%s: Registration of %s failed\n",
		  __func__,
		  PP2S_MODULE_NAME);
		return ret;
	}

	ret = platform_driver_register(&wc_driver);

	if (ret) {
		LOG_DRVR_ERR(
		  "%s: Registration of %s failed\n",
		  __func__,
		  WC_MODULE_NAME);
		return ret;
	}

	return 0;
}

/*
 * Called when device driver module unloaded...
 */
static void __exit pp2s_exit(void)
{
	LOG_DRVR_DEBUG(
		"%s: PP2S interrupt notifier de-initialization\n",
		__func__);

	platform_driver_unregister(&pp2s_driver);
	platform_driver_unregister(&wc_driver);
}

MODULE_DESCRIPTION("FSM PP2S Interrupt Notification Driver");

module_init(pp2s_init);
module_exit(pp2s_exit);