aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/atu/atu_clk_maintainer.c
blob: 297dff8a5a5dda4f9c4022f36d7434f3db775cf0 (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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
 * Atu Clock Maintainer
 *
 * Copyright (C) 2015 Imagination Technologies Ltd.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 */

#include <linux/atu_clk.h>
#include <linux/clk.h>
#include <linux/clocksource.h>
#include <linux/export.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/timecounter.h>
#include <linux/timer.h>
#include <linux/uaccess.h>

#include <uapi/atu_ioctl.h>

#include "atu_clk_maintainer.h"
#include "atu_clk_ntp.h"

static int atu_rate_changed;
#define ATU_UPDATE_TIMER_INTRVL	1
#define ATU_MODE_ON_THE_FLY	0x80000000
#define EVENT_TIMER_RATE_TOLERANCE	100000

/* Structure holding internal clk managing values. */
struct atu_clk_maintainer {
	struct	timespec atu_time;
	struct	timecounter atu_timecntr;
	int	shift;
	u32	mult;
	cycle_t clk_cycles_per_ntp_cycle;
	cycle_t	shifted_ns_per_ntp_cycle;
	s64	shifted_remain_ns_per_ntp_cycle;
	/* NTP_SCALE_SHIFT bit shfted error */
	s64	tm_error;
	/*
	 * Error shift with respect to current clock's
	 * shift i.e (NTP_SCALE_SHIFT - clock shift)
	 */
	int	tm_error_shift;
	/* Char device which holds IOCTL handler */
	struct	miscdevice miscdev;
	/*
	 * The timer structure used to update the elapsed ticks count
	 * at regular interval
	 */
	struct	timer_list atu_timer;
	unsigned long	atu_timer_data;
	struct atu_clk_ntp atu_ntp;
	struct clk *clk_atu;
	spinlock_t atu_clk_lock;
	int event_timer_rate;
	struct notifier_block atu_clk_notifier;
};

static void atu_time_update(void);
static int atu_adjtimex(struct timex *txc);

static struct atu_clk_maintainer *patu_clk_mtner;

static u64 do_div_round_closest(u64 numerator, u32 denominator)
{
	u64 result = 0;

	result = denominator >> 1;
	result += numerator;
	do_div(result, denominator);

	return result;
}

/*
 * This function updates ATU wall time and
 * reschedules for next ATU wall time update.
 */
static void atu_timer_timeout(unsigned long dat)
{
	atu_time_update();
	add_timer(&patu_clk_mtner->atu_timer);
}

/* This function initialises atu wall time update scheduler */
static void atu_timer_init(void)
{
	init_timer(&patu_clk_mtner->atu_timer);
	patu_clk_mtner->atu_timer.expires  = jiffies + ATU_UPDATE_TIMER_INTRVL;
	patu_clk_mtner->atu_timer.data =
		(unsigned long)&patu_clk_mtner->atu_timer_data;
	patu_clk_mtner->atu_timer.function = atu_timer_timeout;

	add_timer(&patu_clk_mtner->atu_timer);
	pr_debug("ATU Wall Time scheduler started\n");
}

static void atu_timer_exit(void)
{
	del_timer_sync(&patu_clk_mtner->atu_timer);
	pr_debug("ATU Wall Time scheduler removed\n");
}

static int atu_gettimestamp(struct atu_event *event)
{
	struct timespec timeofday;
	unsigned long flags;

	if (event->counter < ATU_MAX_COUNTERS) {
		spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
		if (!patu_clk_mtner->atu_timecntr.cc) {
			spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock,
					       flags);
			return -EFAULT;
		}
		event->timestamp_counter =
			patu_clk_mtner->atu_timecntr.cc->
			read(patu_clk_mtner->atu_timecntr.cc);

		event->timestamp = 0x0;
		event->timekeeping_shift = patu_clk_mtner->shift;
		event->timekeeping_mult = patu_clk_mtner->mult;
		atu_getnstimeofday(&timeofday);
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);

		event->timeofday_sec = timeofday.tv_sec;
		event->timeofday_ns = timeofday.tv_nsec;

		return 0;
	} else {
		return -ERANGE;
	}
}

static long
ioctl_img_atu(struct file *file, unsigned int cmd, unsigned long arg)
{
	void __user *argp = (void __user *)arg;
	struct atu_event event;
	int ret;
	struct timex u_txc;
	struct timeval u_tv;
	struct timespec	u_ts;
	unsigned long flags;

	if (!argp)
		return -EINVAL;

	switch (cmd) {
	default:
		return -EINVAL;

	case ATUIO_GETEVTS:
		if (copy_from_user(&event, argp, sizeof(event)))
			return -EFAULT;

		ret = atu_gettimestamp(&event);
		if (ret)
			return ret;

		if (copy_to_user(argp, &event, sizeof(event)))
			return -EFAULT;
		break;

	case ATUIO_ADJTIMEX:
		if (copy_from_user(&u_txc, argp, sizeof(u_txc)))
			return -EFAULT;

		if (u_txc.modes == 0)
			return -EINVAL;

		ret = atu_adjtimex(&u_txc);

		if (copy_to_user(argp, &u_txc, sizeof(u_txc)))
			return -EFAULT;

		if (ret)
			return ret;
		break;

	case ATUIO_SETTIMEOFDAY:
		if (copy_from_user(&u_tv, argp, sizeof(u_tv)))
			return -EFAULT;
		u_ts.tv_sec = u_tv.tv_sec;
		u_ts.tv_nsec = u_tv.tv_usec * NSEC_PER_USEC;
		ret = atu_settimeofday(&u_ts);
		if (ret)
			return ret;
		break;

	case ATUIO_GETTIMESPEC:
		spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
		if (!patu_clk_mtner->atu_timecntr.cc) {
			spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock,
					       flags);
			return -EFAULT;
		}
		atu_getnstimeofday(&u_ts);
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);

		if (copy_to_user(argp, &u_ts, sizeof(u_ts)))
			return -EFAULT;
		break;
	}

	return 0;
}

static const struct file_operations atu_dev_fops = {
	.unlocked_ioctl		= ioctl_img_atu,
};

static int atu_chardev_init(void)
{
	int error;

	patu_clk_mtner->miscdev.minor = MISC_DYNAMIC_MINOR;
	patu_clk_mtner->miscdev.fops = &atu_dev_fops;
	patu_clk_mtner->miscdev.name = "img-atu";
	error = misc_register(&patu_clk_mtner->miscdev);
	if (error) {
		pr_err("Unable to register atu device\n");
		goto err_misc_reg;
	}
	pr_debug("ATU Clock dev added\n");

	return 0;

err_misc_reg:
	return error;
}

static int atu_chardev_remove(void)
{
	misc_deregister(&patu_clk_mtner->miscdev);
	pr_debug("ATU Clock dev removed\n");

	return 0;
}

static void
atu_clk_mtner_setup_internals(struct cyclecounter *patu_cyclecntr,
			      struct clk *clk_atu)
{
	cycle_t clk_cycle;
	u64 tmp, ntp_ns_per_cycle;

	patu_clk_mtner->atu_timecntr.cc = patu_cyclecntr;
	patu_clk_mtner->clk_atu = clk_atu;

	/* Clear the error */
	patu_clk_mtner->tm_error = 0;
	patu_clk_mtner->tm_error_shift = NTP_SCALE_SHIFT -
					patu_clk_mtner->atu_timecntr.cc->shift;

	tmp = NTP_INTERVAL_LENGTH;
	tmp <<= patu_clk_mtner->atu_timecntr.cc->shift;
	ntp_ns_per_cycle = tmp;

	tmp = do_div_round_closest(tmp, patu_clk_mtner->atu_timecntr.cc->mult);
	if (tmp == 0)
		tmp = 1;

	clk_cycle = (cycle_t)tmp;
	patu_clk_mtner->clk_cycles_per_ntp_cycle = clk_cycle;

	patu_clk_mtner->shifted_ns_per_ntp_cycle =
		(u64)clk_cycle * patu_clk_mtner->atu_timecntr.cc->mult;
	patu_clk_mtner->shifted_remain_ns_per_ntp_cycle =
		ntp_ns_per_cycle - patu_clk_mtner->shifted_ns_per_ntp_cycle;

	patu_clk_mtner->shift = patu_clk_mtner->atu_timecntr.cc->shift;

	patu_clk_mtner->mult = patu_clk_mtner->atu_timecntr.cc->mult;

	/* Update cycle_last with current read value */
	patu_clk_mtner->atu_timecntr.cycle_last = patu_clk_mtner->
			atu_timecntr.cc->read(patu_clk_mtner->atu_timecntr.cc);
}

static s64 atu_tm_get_ns(void)
{
	cycle_t ticks_now, ticks_delta;

	/* Read present clock ticks */
	ticks_now = patu_clk_mtner->atu_timecntr.cc->
				read(patu_clk_mtner->atu_timecntr.cc);

	/* Calculate the ticks delta since the last atu_update_clk_time: */
	ticks_delta = (ticks_now - patu_clk_mtner->atu_timecntr.cycle_last) &
			patu_clk_mtner->atu_timecntr.cc->mask;

	/* Convert to nanoseconds */
	return clocksource_cyc2ns(ticks_delta, patu_clk_mtner->mult,
				  patu_clk_mtner->shift);
}

/* It will update the latest clock's tick to the clk time */
static void atu_time_refresh(void)
{
	cycle_t cycle_now, cycle_delta;
	s64 nsec;

	cycle_now = patu_clk_mtner->atu_timecntr.cc->
				read(patu_clk_mtner->atu_timecntr.cc);

	cycle_delta = (cycle_now - patu_clk_mtner->atu_timecntr.cycle_last) &
			patu_clk_mtner->atu_timecntr.cc->mask;
	patu_clk_mtner->atu_timecntr.cycle_last = cycle_now;

	nsec = clocksource_cyc2ns(cycle_delta, patu_clk_mtner->mult,
				  patu_clk_mtner->shift);

	timespec_add_ns(&patu_clk_mtner->atu_time, nsec);
}

void atu_getnstimeofday(struct timespec *ts)
{
	s64 nsecs;

	if (!ts)
		return;
	*ts = patu_clk_mtner->atu_time;
	nsecs = atu_tm_get_ns();
	timespec_add_ns(ts, nsecs);
}

u64 atu_get_current_time(void)
{
	u64 nsecs;
	struct timespec ts;
	unsigned long flags;

	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
	if (!patu_clk_mtner->atu_timecntr.cc) {
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
		return -EFAULT;
	}
	ts = patu_clk_mtner->atu_time;
	nsecs = atu_tm_get_ns();
	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);

	nsecs += ((u64)ts.tv_sec * NSEC_PER_SEC) + ((u64)ts.tv_nsec);

	return nsecs;
}
EXPORT_SYMBOL(atu_get_current_time);

int atu_settimeofday(const struct timespec *ts)
{
	unsigned long flags;

	if (ts->tv_nsec >= NSEC_PER_SEC)
		return -EINVAL;

	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
	if (!patu_clk_mtner->atu_timecntr.cc) {
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
		return -EFAULT;
	}

	/*
	 * Refresh the time so that it will update
	 * clock's last cycle to present value
	 */
	atu_time_refresh();

	patu_clk_mtner->atu_time = *ts;

	patu_clk_mtner->tm_error = 0;
	atu_ntp_reset(&patu_clk_mtner->atu_ntp);

	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);

	return 0;
}

int atu_tm_add_offset(struct timespec *ts)
{
	if (ts->tv_nsec >= NSEC_PER_SEC)
		return -EINVAL;

	atu_time_refresh();

	patu_clk_mtner->atu_time = timespec_add(patu_clk_mtner->atu_time, *ts);

	patu_clk_mtner->tm_error = 0;

	atu_ntp_reset(&patu_clk_mtner->atu_ntp);

	return 0;
}

unsigned long atu_get_seconds(void)
{
	return patu_clk_mtner->atu_time.tv_sec;
}

static void frc_ticks_to_atu_units(u32 frccnt, u64 *patu)
{
	*patu = ((__u64)frccnt * patu_clk_mtner->mult) >> patu_clk_mtner->shift;
}

static void atu_units_to_frc_ticks(u64 atu, u64 *pfrccnt)
{
	*pfrccnt = do_div_round_closest(atu <<
			patu_clk_mtner->shift, patu_clk_mtner->mult);
}

static void atu_get_cur_atu_frc_pair(u64 *patu, u32 *pfrc)
{
	u64 nsecs;
	struct timespec ts;
	cycle_t ticks_now, ticks_delta;

	ts = patu_clk_mtner->atu_time;

	/* Read present clock ticks */
	ticks_now = patu_clk_mtner->atu_timecntr.cc->
				read(patu_clk_mtner->atu_timecntr.cc);
	ticks_now = ticks_now & patu_clk_mtner->atu_timecntr.cc->mask;

	/* Calculate the ticks delta since the last atu_update_clk_time: */
	ticks_delta = (ticks_now - patu_clk_mtner->atu_timecntr.cycle_last) &
			patu_clk_mtner->atu_timecntr.cc->mask;

	/* Convert to nanoseconds */
	nsecs = clocksource_cyc2ns(ticks_delta, patu_clk_mtner->mult,
				   patu_clk_mtner->shift);

	nsecs += ((u64)ts.tv_sec * NSEC_PER_SEC) + ((u64)ts.tv_nsec);

	*patu = nsecs;
	*pfrc = ticks_now;
}

int frc_to_atu(u32 frc, u64 *patu, s32 dir)
{
	u32 diff = 0, cur_frc;
	u64 cur_atu;
	unsigned long flags;

	if (!patu)
		return -EINVAL;

	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
	if (!patu_clk_mtner->atu_timecntr.cc) {
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
		return -EFAULT;
	}

	/* Get current atu and frc count */
	atu_get_cur_atu_frc_pair(&cur_atu, &cur_frc);

	if (cur_frc >= frc)
		diff = cur_frc - frc;
	else
		diff = patu_clk_mtner->atu_timecntr.cc->mask +
					1 + cur_frc - frc;
	frc_ticks_to_atu_units(diff, patu);
	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);

	if (dir == ATU_PAST) {
		*patu = cur_atu - *patu;
		if (*patu < 0)
			return -ERANGE;
	} else {
		*patu = cur_atu + *patu;
	}

	return 0;
}
EXPORT_SYMBOL(frc_to_atu);

int atu_to_frc(u64 atu, u32 *pfrc, u64 min_nsec)
{
	u64 cur_atu, atu_diff, frc_cnt;
	u32 cur_frc;
	unsigned long flags;

	if (!pfrc)
		return -EINVAL;

	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
	if (!patu_clk_mtner->atu_timecntr.cc) {
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
		return -EFAULT;
	}

	/* Get current atu and frc count */
	atu_get_cur_atu_frc_pair(&cur_atu, &cur_frc);

	atu_diff = atu - cur_atu;

	/* Check for past time and min time diff */
	if (atu < cur_atu || atu_diff < min_nsec) {
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
		return -ERANGE;
	}

	atu_units_to_frc_ticks(atu_diff, &frc_cnt);
	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);

	/* Check for obtained value is not more than counter rollover value */
	if (frc_cnt > patu_clk_mtner->atu_timecntr.cc->mask)
		return -ERANGE;

	/* Get cycle count val at the given atu time */
	*pfrc = (frc_cnt + cur_frc) &
			patu_clk_mtner->atu_timecntr.cc->mask;

	/* Check with latest time for min time diff */
	if ((atu - min_nsec) < atu_get_current_time())
		return -ERANGE;

	return 0;
}
EXPORT_SYMBOL(atu_to_frc);

void
atu_clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
{
	u64 tmp;
	u32 sft, sftacc = 32;

	/*
	 * Calculate the shift factor which is limiting the conversion
	 * range:
	 */
	tmp = ((u64)maxsec * from) >> 32;
	while (tmp) {
		tmp >>= 1;
		sftacc--;
	}

	/*
	 * Find the conversion shift/mult pair which has the best
	 * accuracy and fits the maxsec conversion range:
	 */
	for (sft = 32; sft > 0; sft--) {
		tmp = (u64)to << sft;
		tmp += from / 2;
		do_div(tmp, from);
		if ((tmp >> sftacc) == 0)
			break;
	}
	*mult = tmp;
	*shift = sft;
}

static void atu_clk_rate_change_on_the_fly(unsigned long int rate)
{
	unsigned long flags;
	u32 mult, shift, mask;
	cycle_t clk_cycle;
	u64 ntp_ns_per_cycle, ntp_ns_per_cycle_div;

	mask = patu_clk_mtner->atu_timecntr.cc->mask;

	clocks_calc_mult_shift(&mult, &shift, rate,
			       NSEC_PER_SEC, DIV_ROUND_UP(mask, rate));

	ntp_ns_per_cycle = (u64)NTP_INTERVAL_LENGTH << shift;
	ntp_ns_per_cycle_div = do_div_round_closest(ntp_ns_per_cycle, mult);

	clk_cycle = ntp_ns_per_cycle_div ? (cycle_t)ntp_ns_per_cycle_div :
					  (cycle_t)1;

	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);

	patu_clk_mtner->shift = shift;
	patu_clk_mtner->mult = mult;
	patu_clk_mtner->event_timer_rate = rate;

	atu_rate_changed = 1;

	/* Clear the error */
	patu_clk_mtner->tm_error = 0;
	patu_clk_mtner->tm_error_shift = NTP_SCALE_SHIFT - shift;

	patu_clk_mtner->clk_cycles_per_ntp_cycle = clk_cycle;

	patu_clk_mtner->shifted_ns_per_ntp_cycle =
		(u64)clk_cycle * mult;
	patu_clk_mtner->shifted_remain_ns_per_ntp_cycle =
		ntp_ns_per_cycle - patu_clk_mtner->shifted_ns_per_ntp_cycle;

	/* Update cycle_last with current read value */
	patu_clk_mtner->atu_timecntr.cycle_last = patu_clk_mtner->
			atu_timecntr.cc->read(patu_clk_mtner->atu_timecntr.cc);

	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
}

static int atu_adjtimex(struct timex *txc)
{
	int ret = 0;
	unsigned long flags;

	/* Fractional PLL */
	if (patu_clk_mtner->clk_atu) {

		spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
		/*
		 * Mode (utilizing the txc->modes) to check
		 * the rate change on the fly
		 */

		if (txc->modes == ATU_MODE_ON_THE_FLY) {
			if (atu_rate_changed) {
				atu_rate_changed = 0;
				ret = 1;
			}
			goto unlock_and_return;
		}
		if (atu_rate_changed) {
			ret = 0;
			goto unlock_and_return;
		}

		/* Time error correction - ADJ_SETOFFSET */
		if (txc->modes & ADJ_SETOFFSET) {
			ret = atu_set_time_offset(txc);
			goto unlock_and_return;
		}
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);

		/* Rate error correction - ADJ_FREQUENCY */
		if (txc->modes & ADJ_FREQUENCY) {
			unsigned long int rate;
			long int freq;
			int dir;

			freq = txc->freq;

			if (freq < 0) {
				dir = -1;
				freq = -freq;
			} else  {
				dir = 1;
			}

			rate = patu_clk_mtner->event_timer_rate +
					do_div_round_closest(((u64)freq) *
					patu_clk_mtner->event_timer_rate,
					NSEC_PER_SEC) * dir;

			/* Setting new rate */
			clk_set_rate(patu_clk_mtner->clk_atu, rate);
			txc->freq = rate;
		}

	} else { /* Fixed PLL */
		spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
		if (patu_clk_mtner->atu_timecntr.cc)
			ret = __atu_adjtimex(txc, &patu_clk_mtner->atu_ntp);
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
	}

	return ret;

unlock_and_return:
	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
	return ret;
}

static int atu_tm_big_adj_clk(s64 error, s64 *clk_cycle, s64 *ticks)
{
	s64 ntp_error, clk_interval;
	u32 look_ahead, shift;
	s32 mult = 1;
	s32 tm_error;

	/*
	 * Correction of about 1msec within about 1 sec
	 * or 2^20 nsec in 2^SHIFT_HZ ticks.
	 */
	tm_error = patu_clk_mtner->tm_error >>
				(NTP_SCALE_SHIFT + 22 - 2 * SHIFT_HZ);

	tm_error = abs(tm_error);

	for (look_ahead = 0; tm_error > 0; look_ahead++)
		tm_error >>= 2;

	ntp_error = get_ntp_shifted_nsecs_per_cycle(&patu_clk_mtner->atu_ntp) >>
				(patu_clk_mtner->tm_error_shift + 1);
	ntp_error -= patu_clk_mtner->shifted_ns_per_ntp_cycle >> 1;

	error = ((error - ntp_error) >> look_ahead) + ntp_error;

	clk_interval = *clk_cycle;
	if (error < 0) {
		error = -error;
		*clk_cycle = -*clk_cycle;
		*ticks = -*ticks;
		mult = -1;
	}
	for (shift = 0;
		error > clk_interval; shift++)
		error >>= 1;

	*clk_cycle <<= shift;
	*ticks <<= shift;
	return mult << shift;
}

static void atu_tm_adj_clk(s64 ticks)
{
	s64 error, clk_cycle;
	int mult;

	clk_cycle = patu_clk_mtner->clk_cycles_per_ntp_cycle;

	error = patu_clk_mtner->tm_error >>
			(patu_clk_mtner->tm_error_shift - 1);

	if (error > clk_cycle) {
		error >>= 3;

		if (likely(error <= clk_cycle))
				mult = 1;
		else
				mult = atu_tm_big_adj_clk(error, &clk_cycle,
							  &ticks);
	} else if (error < -clk_cycle) {
		error >>= 3;

		if (likely(error >= -clk_cycle)) {
				mult = -1;
				clk_cycle = -clk_cycle;
				ticks = -ticks;
		} else {
				mult = atu_tm_big_adj_clk(error, &clk_cycle,
							  &ticks);
		}
	} else {
		return;
	}

		patu_clk_mtner->mult += mult;
		patu_clk_mtner->shifted_ns_per_ntp_cycle += clk_cycle;
		patu_clk_mtner->atu_time.tv_nsec -=
				ticks >> patu_clk_mtner->shift;
		patu_clk_mtner->tm_error -= (clk_cycle - ticks) <<
						patu_clk_mtner->tm_error_shift;
}

static cycle_t calculate_remainder_ticks(cycle_t ticks)
{
	u64 no_cycles = ticks;
	u64 clk_cycle;
	s64 tick_error = 0;

	clk_cycle = patu_clk_mtner->clk_cycles_per_ntp_cycle;

	/* Get the no clk cycles per ntp cycle in the given ticks */
	do_div(no_cycles, clk_cycle);

	/* Subtract the no. of rounded cycles from ticks */
	ticks -= (no_cycles * patu_clk_mtner->clk_cycles_per_ntp_cycle);

	/* Add the no. of clk cycles to the cycle_last */
	patu_clk_mtner->atu_timecntr.cycle_last +=
		(no_cycles * patu_clk_mtner->clk_cycles_per_ntp_cycle);

	/* Add the no.of clk cycles to nano seconds */
	patu_clk_mtner->atu_time.tv_nsec += (no_cycles * patu_clk_mtner->
		shifted_ns_per_ntp_cycle) >> patu_clk_mtner->shift;

	/*
	 * If there is an overflow of nano seconds will update tv_sec and
	 * try to update/sync ntp's params
	 */
	while (patu_clk_mtner->atu_time.tv_nsec >= NSEC_PER_SEC) {
		patu_clk_mtner->atu_time.tv_nsec -= NSEC_PER_SEC;
		patu_clk_mtner->atu_time.tv_sec++;
		atu_ntp_param_update_per_second(&patu_clk_mtner->atu_ntp);
	}

	tick_error += get_ntp_shifted_nsecs_per_cycle(&patu_clk_mtner->atu_ntp);
	tick_error -=
		(patu_clk_mtner->shifted_ns_per_ntp_cycle +
		patu_clk_mtner->shifted_remain_ns_per_ntp_cycle) <<
				(patu_clk_mtner->tm_error_shift);

	/* Add the tick error of no_cycles to ntp error */
	patu_clk_mtner->tm_error += (no_cycles * tick_error);

	/* Return the left over ticks count */
	return ticks;
}

static void atu_update_clk_time(void)
{
	cycle_t ticks;
	cycle_t curticks;

	curticks = patu_clk_mtner->atu_timecntr.cc->
			read(patu_clk_mtner->atu_timecntr.cc);

	ticks = (curticks - patu_clk_mtner->atu_timecntr.cycle_last) &
					patu_clk_mtner->atu_timecntr.cc->mask;

	ticks = calculate_remainder_ticks(ticks);

	/* Correct the clock */
	atu_tm_adj_clk(ticks);

	/* If tv_nsec is -ve we will adjust that as error */
	if (unlikely((s64)patu_clk_mtner->atu_time.tv_nsec < 0)) {
		s64 neg = -(s64)patu_clk_mtner->atu_time.tv_nsec;

		patu_clk_mtner->atu_time.tv_nsec = 0;
		patu_clk_mtner->tm_error +=
			neg << patu_clk_mtner->tm_error_shift;
	}

	while ((patu_clk_mtner->atu_time.tv_nsec >= NSEC_PER_SEC)) {
		patu_clk_mtner->atu_time.tv_nsec -= NSEC_PER_SEC;
		patu_clk_mtner->atu_time.tv_sec++;
		atu_ntp_param_update_per_second(&patu_clk_mtner->atu_ntp);
	}
}

static void atu_time_update(void)
{
	unsigned long flags;

	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
	if (!patu_clk_mtner->atu_timecntr.cc) {
		spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
		return;
	}
	atu_update_clk_time();
	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
}

static int atu_clk_notifier_cb(struct notifier_block *nb,
			       unsigned long event, void *data)
{
	struct clk_notifier_data *ndata = data;
	long int diff;

	switch (event) {
	case PRE_RATE_CHANGE:
		diff = abs((int)patu_clk_mtner->event_timer_rate
				 - (int)ndata->new_rate);

		/*
		 *  Assumption is that card driver rate change
		 *  will be more than 100,000 Hz
		 */
		if (diff > EVENT_TIMER_RATE_TOLERANCE) {
			atu_clk_rate_change_on_the_fly(ndata->new_rate);
			pr_debug("ATU rate change %lu\n", ndata->new_rate);
		}
		return NOTIFY_OK;
	case POST_RATE_CHANGE:
	case ABORT_RATE_CHANGE:
		return NOTIFY_OK;
	default:
		return NOTIFY_DONE;
	}
}

int atu_cyclecounter_register(struct cyclecounter *pcc, struct clk *clk_atu)
{
	unsigned long flags;

	if (!pcc) {
		pr_err("%s got NULL pointer\n", __func__);
		return -EINVAL;
	}

	if (clk_atu)
		pr_info("Register Fractional PLL to ATU Clock\n");
	else
		pr_info("Register Fixed PLL to ATU Clock\n");

	if (clk_atu) {
		patu_clk_mtner->event_timer_rate = clk_get_rate(clk_atu);
		patu_clk_mtner->atu_clk_notifier.notifier_call =
				atu_clk_notifier_cb;
		clk_notifier_register(clk_atu,
				      &patu_clk_mtner->atu_clk_notifier);
	}

	atu_chardev_init();
	atu_ntp_init(&patu_clk_mtner->atu_ntp);

	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
	atu_clk_mtner_setup_internals(pcc, clk_atu);
	atu_ntp_reset(&patu_clk_mtner->atu_ntp);
	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
	atu_timer_init();
	pr_info("ATU Clock Registered for cycle counter:0x%p\n", pcc);

	return 0;
}
EXPORT_SYMBOL(atu_cyclecounter_register);

int atu_cyclecounter_unregister(struct cyclecounter *pcc)
{
	unsigned long flags;

	if (!pcc) {
		pr_err("%s got NULL pointer\n", __func__);
		return -EINVAL;
	}
	if (pcc != patu_clk_mtner->atu_timecntr.cc) {
		pr_err("Invalid input data to %s\n", __func__);
		return -EINVAL;
	}
	if (patu_clk_mtner->clk_atu)
		clk_notifier_unregister(patu_clk_mtner->clk_atu,
					&patu_clk_mtner->atu_clk_notifier);

	atu_timer_exit();
	atu_chardev_remove();
	spin_lock_irqsave(&patu_clk_mtner->atu_clk_lock, flags);
	patu_clk_mtner->atu_timecntr.cc = NULL;
	patu_clk_mtner->clk_atu = NULL;
	spin_unlock_irqrestore(&patu_clk_mtner->atu_clk_lock, flags);
	pr_info("ATU Clock Un-Registered for cycle counter:0x%p\n", pcc);

	return 0;
}
EXPORT_SYMBOL(atu_cyclecounter_unregister);

static int __init atu_tm_init(void)
{
	patu_clk_mtner = kzalloc(sizeof(*patu_clk_mtner), GFP_KERNEL);
	if (!patu_clk_mtner)
		return -ENOMEM;

	/* Initialise the spin lock */
	spin_lock_init(&patu_clk_mtner->atu_clk_lock);

	/* Initialise ATU wall time */
	patu_clk_mtner->atu_time.tv_sec = 0;
	patu_clk_mtner->atu_time.tv_nsec = 0;

	pr_info("ATU Clock Module Loaded\n");

	return 0;
}
module_init(atu_tm_init);

static void __exit atu_tm_exit(void)
{
	if (patu_clk_mtner->atu_timecntr.cc)
		atu_timer_exit();
	atu_chardev_remove();
	kfree(patu_clk_mtner);
	patu_clk_mtner = NULL;
}
module_exit(atu_tm_exit);

MODULE_DESCRIPTION("ATU Clock Maintainer");
MODULE_AUTHOR("Krishna.Badam@imgtec.com");
MODULE_LICENSE("GPL v2");