summaryrefslogtreecommitdiff
path: root/drivers/clk/clk-sc.c
blob: 49b5ceb83dacc82a169a2c8f6f8d0b621d2e99d3 (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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
/*
 * Copyright (C) 2013 Spreadtrum Communications Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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/clk-provider.h>
#include <linux/clkdev.h>
#include <linux/clk-provider.h>
#include <linux/clk-private.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <soc/sprd/hardware.h>
#include <soc/sprd/sci_glb_regs.h>
#include <soc/sprd/arch_lock.h>

#ifdef CONFIG_OF

#define clk_debug(format, arg...) pr_debug("clk: " "@@@%s: " format, __func__, ## arg)
#define clk_info(format, arg...) pr_info("clk: " "@@@%s: " format, __func__, ## arg)

struct cfg_reg {
	void __iomem *reg;
	u32 msk;
};

struct clk_sprd {
	struct clk_hw hw;
	struct cfg_reg enb;
	u8 flags;
	union {
		unsigned long fixed_rate;
		u32 c_mul;
		struct cfg_reg mul, sel;
		struct clk_hw *mux_hw;
	} m;
	union {
		u32 c_div;
		struct cfg_reg div, pre;
		struct clk_hw *div_hw;
	} d;
};

#define to_clk_sprd(_hw) container_of(_hw, struct clk_sprd, hw)

#define in_range(b, first, len)	((b) >= (first) && (b) <= (first) + (len) - 1)
#define to_range(b, first, base) ( (b) - (first) + (base) )

static inline unsigned long cfg_reg_p2v(const u32 regp)
{
	return SPRD_DEV_P2V(regp);
/*	u32 regv = 0;

	if (0) {
	} else if (in_range(regp, SPRD_AHB_PHYS, SPRD_AHB_SIZE)) {
		regv = to_range(regp, SPRD_AHB_PHYS, SPRD_AHB_BASE);
	} else if (in_range(regp, SPRD_PMU_PHYS, SPRD_PMU_SIZE)) {
		regv = to_range(regp, SPRD_PMU_PHYS, SPRD_PMU_BASE);
	} else if (in_range(regp, SPRD_AONAPB_PHYS, SPRD_AONAPB_SIZE)) {
		regv = to_range(regp, SPRD_AONAPB_PHYS, SPRD_AONAPB_BASE);
	} else if (in_range(regp, SPRD_AONCKG_PHYS, SPRD_AONCKG_SIZE)) {
		regv = to_range(regp, SPRD_AONCKG_PHYS, SPRD_AONCKG_BASE);
	} else if (in_range(regp, SPRD_GPUCKG_PHYS, SPRD_GPUCKG_SIZE)) {
		regv = to_range(regp, SPRD_GPUCKG_PHYS, SPRD_GPUCKG_BASE);
	} else if (in_range(regp, SPRD_GPUAPB_PHYS, SPRD_GPUAPB_SIZE)) {
		regv = to_range(regp, SPRD_GPUAPB_PHYS, SPRD_GPUAPB_BASE);
	} else if (in_range(regp, SPRD_MMCKG_PHYS, SPRD_MMCKG_SIZE)) {
		regv = to_range(regp, SPRD_MMCKG_PHYS, SPRD_MMCKG_BASE);
	} else if (in_range(regp, SPRD_MMAHB_PHYS, SPRD_MMAHB_SIZE)) {
		regv = to_range(regp, SPRD_MMAHB_PHYS, SPRD_MMAHB_BASE);
	} else if (in_range(regp, SPRD_APBREG_PHYS, SPRD_APBREG_SIZE)) {
		regv = to_range(regp, SPRD_APBREG_PHYS, SPRD_APBREG_BASE);
	} else if (in_range(regp, SPRD_APBCKG_PHYS, SPRD_APBCKG_SIZE)) {
		regv = to_range(regp, SPRD_APBCKG_PHYS, SPRD_APBCKG_BASE);
	} else {
		WARN(1, "regp %08x\n", regp);
	}

	return regv;*/
}

static inline void of_read_reg(struct cfg_reg *cfg, const __be32 * cell)
{
	if (!cell)
		return;
	cfg->reg = (void *)cfg_reg_p2v(be32_to_cpu(*(cell++)));
	cfg->msk = be32_to_cpu(*(cell++));
}

static inline void __glbreg_setclr(struct clk_hw *hw, void *reg, u32 msk,
				   int is_set)
{
	unsigned long flags;
	if (!reg)
		return;

	clk_debug("%s %s %p[%x]\n", (hw) ? __clk_get_name(hw->clk) : NULL,
		  (is_set) ? "SET" : "CLR", reg, (u32) msk);

	__arch_default_lock(HWLOCK_GLB, &flags);

	if (is_set)
		__raw_writel(msk, (void *)((unsigned long) (reg) + 0x1000));
	else
	{
		__raw_writel(msk, (void *)((unsigned long) (reg) + 0x2000));
	}
	__arch_default_unlock(HWLOCK_GLB, &flags);
}

#define __glbreg_set(hw, reg, msk)	__glbreg_setclr(hw, reg, msk, 1)
#define __glbreg_clr(hw, reg, msk)	__glbreg_setclr(hw, reg, msk, 0)

static int sprd_clk_prepare(struct clk_hw *hw)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	int set = ! !(c->flags & CLK_GATE_SET_TO_DISABLE);

	__glbreg_setclr(hw, c->d.pre.reg, (u32) c->d.pre.msk, set ^ 1);
	return 0;
}

static void sprd_clk_unprepare(struct clk_hw *hw)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	int set = ! !(c->flags & CLK_GATE_SET_TO_DISABLE);
	__glbreg_setclr(hw, c->d.pre.reg, (u32) c->d.pre.msk, set ^ 0);
}

static int sprd_clk_is_prepared(struct clk_hw *hw)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	int ret, set = ! !(c->flags & CLK_GATE_SET_TO_DISABLE);

	if (!c->d.pre.reg)
		return 0;

	/* if a set bit prepare this gate, flip it before masking */
	ret = ! !(__raw_readl(c->d.pre.reg) & BIT(c->d.pre.msk));
	return set ^ ret;
}

#define __SPRD_MM_TIMEOUT            (3 * 1000)

/* FIXME: sharkls no chip macro */
#if defined(CONFIG_MACH_SP9830I) || defined(CONFIG_ARCH_SCX30G2) || defined(CONFIG_ARCH_SCX35LT8)
static int sprd_clk_coda7_is_ready(void)
{
	u32 power_state1, power_state2, power_state3;
	unsigned long timeout = jiffies + msecs_to_jiffies(__SPRD_MM_TIMEOUT);

	do {
		cpu_relax();
		power_state1 =
			__raw_readl((void *)REG_CODEC_AHB_CODA7_STAT) & BIT_CODA7_RUN;
		power_state2 =
			__raw_readl((void *)REG_CODEC_AHB_CODA7_STAT) & BIT_CODA7_RUN;
		power_state3 =
			__raw_readl((void *)REG_CODEC_AHB_CODA7_STAT) & BIT_CODA7_RUN;
		BUG_ON(time_after(jiffies, timeout));
	} while (power_state1 != power_state2 || power_state2 != power_state3);

	if (!power_state1)
		return 1;

	return 0;
}
#else
static int sprd_clk_coda7_is_ready(void)
{
	return 1;
}
#endif

static int sprd_clk_enable(struct clk_hw *hw)
{
	unsigned long flags = 0;
	struct clk_sprd *c = to_clk_sprd(hw);

	if (!strcmp(__clk_get_name(hw->clk), "clk_coda7_axi") ||
			!strcmp(__clk_get_name(hw->clk), "clk_coda7_cc") ||
			!strcmp(__clk_get_name(hw->clk), "clk_coda7_apb")) {
		__arch_default_lock(HWLOCK_GLB, &flags);
		if (!strcmp(__clk_get_name(hw->clk), "clk_coda7_apb")) {
			__raw_writel(__raw_readl(c->enb.reg) & (~((u32)c->enb.msk)), c->enb.reg);
		} else {
			__raw_writel(__raw_readl(c->enb.reg) | (u32)c->enb.msk, c->enb.reg);
		}
		__arch_default_unlock(HWLOCK_GLB, &flags);
		BUG_ON(!sprd_clk_coda7_is_ready());
	} else {
		__glbreg_set(hw, c->enb.reg, (u32) c->enb.msk);
	}

	return 0;
}

static void sprd_clk_disable(struct clk_hw *hw)
{
	unsigned long flags = 0;
	struct clk_sprd *c = to_clk_sprd(hw);

	if (!strcmp(__clk_get_name(hw->clk), "clk_coda7_axi") ||
			!strcmp(__clk_get_name(hw->clk), "clk_coda7_cc") ||
			!strcmp(__clk_get_name(hw->clk), "clk_coda7_apb")) {
		__arch_default_lock(HWLOCK_GLB, &flags);
		if (!strcmp(__clk_get_name(hw->clk), "clk_coda7_apb")) {
			__raw_writel(__raw_readl(c->enb.reg) | (u32)c->enb.msk, c->enb.reg);
		} else {
			__raw_writel(__raw_readl(c->enb.reg) & (~((u32)c->enb.msk)), c->enb.reg);
		}
		__arch_default_unlock(HWLOCK_GLB, &flags);
	} else {
		__glbreg_clr(hw, c->enb.reg, (u32) c->enb.msk);
	}
}

static int sprd_clk_is_enable(struct clk_hw *hw)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	int ret = ! !(__raw_readl(c->enb.reg) & BIT(c->enb.msk));
	return ret;
}

static unsigned long sprd_clk_fixed_pll_recalc_rate(struct clk_hw *hw,
						    unsigned long parent_rate)
{
	return to_clk_sprd(hw)->m.fixed_rate;
}

#ifdef CONFIG_ARCH_SCX30G
/* bits definitions for register REG PLL CFG1 */
#define BITS_PLL_KINT(_X_)                               ( (_X_) << 12 & (BIT(12)|BIT(13)|BIT(14)|BIT(15)|BIT(16)|BIT(17)|BIT(18)|BIT(19)|BIT(20)|BIT(21)|BIT(22)|BIT(23)|BIT(24)|BIT(25)|BIT(26)|BIT(27)|BIT(28)|BIT(29)|BIT(30)|BIT(31)) )
#define BIT_PLL_DIV_S                                    ( BIT(10) )
#define BITS_PLL_RSV(_X_)                                ( (_X_) << 8 & (BIT(8)|BIT(9)) )
#define BIT_PLL_MOD_EN                                   ( BIT(7) )
#define BIT_PLL_SDM_EN                                   ( BIT(6) )
#define BITS_PLL_NINT(_X_)                               ( (_X_) & (BIT(0)|BIT(1)|BIT(2)|BIT(3)|BIT(4)|BIT(5)) )

#define SHFT_PLL_KINT                                     ( 12 )
#define SHFT_PLL_NINT                                     ( 0 )

#elif defined(CONFIG_ARCH_SCX35L)
/* bits definitions for register REG_AON_APB_PLL_CFG1 */
#define BITS_PLL_RES(_X_)                                ( (_X_) << 28 & (BIT(28)|BIT(29)) )
#define BIT_PLL_LOCK_DONE                                ( BIT(27) )
#define BIT_PLL_DIV_S                                    ( BIT(26) )
#define BIT_PLL_MOD_EN                                   ( BIT(25) )
#define BIT_PLL_SDM_EN                                   ( BIT(24) )
#define BITS_PLL_LPF(_X_)                                ( (_X_) << 20 & (BIT(20)|BIT(21)|BIT(22)) )
#define BITS_PLL_REFIN(_X_)                              ( (_X_) << 18 & (BIT(18)|BIT(19)) )
#define BITS_PLL_IBIAS(_X_)                              ( (_X_) << 16 & (BIT(16)|BIT(17)) )
#define BITS_PLL_N(_X_)                                  ( (_X_) & (BIT(0)|BIT(1)|BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7)|BIT(8)|BIT(9)|BIT(10)) )

/* bits definitions for register REG_AON_APB_PLL_CFG2 */
#define BITS_PLL_NINT(_X_)                               ( (_X_) << 24 & (BIT(24)|BIT(25)|BIT(26)|BIT(27)|BIT(28)|BIT(29)) )
#define BITS_PLL_KINT(_X_)                               ( (_X_) & (BIT(0)|BIT(1)|BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7)|BIT(8)|BIT(9)|BIT(10)|BIT(11)|BIT(12)|BIT(13)|BIT(14)|BIT(15)|BIT(16)|BIT(17)|BIT(18)|BIT(19)) )

#define SHFT_PLL_KINT                                     ( 0 )
#define SHFT_PLL_NINT                                     ( 24 )

#else
#define BITS_MPLL_REFIN(_X_)                              ( (_X_) << 24 & (BIT(24)|BIT(25)) )

#endif

/*
 How To look PLL Setting
 reg name	 val	 bit
 MPLL_CFG	 Fin	 [25:24]
 MPLL_CFG	 N	 [5:0]
 MPLL_CFG1	 div_s	 [10]
 MPLL_CFG1	 sdm_en  [6]
 MPLL_CFG1	 Nint	 [5:0]
 MPLL_CFG1	 Kint	 [31:12]
 div_s = 1	 sdm_en = 1  Fout = Fin * ( Nint + Kint/1048576)
 div_s = 1	 sdm_en = 0  Fout = Fin * Nint
 div_s = 0	 sdm_en = x  Fout = Fin * N
 */

static inline unsigned int __pll_get_refin_rate(void *reg)
{
	const unsigned long refin[4] = { 2000000, 4000000, 13000000, 26000000 };
	u32 i, msk = BITS_MPLL_REFIN(-1);
	i = (__raw_readl(reg) & msk) >> __ffs(msk);
	return refin[i];
}

static unsigned long sprd_clk_adjustable_pll_recalc_rate(struct clk_hw *hw,
							 unsigned long
							 parent_rate)
{
	struct clk_sprd *pll = to_clk_sprd(hw);
	unsigned int rate;

#ifdef CONFIG_ARCH_SCX30G
	unsigned int k = 0, mn, cfg1;
	cfg1 = __raw_readl(pll->m.mul.reg);
	mn = (cfg1 & BITS_PLL_NINT(~0)) >> SHFT_PLL_NINT;

	/* FIXME: Kint only valid while sdm_en = 1 */
	if ((cfg1 & BIT_PLL_SDM_EN))
		k = (cfg1 & BITS_PLL_KINT(~0)) >> SHFT_PLL_KINT;

	rate =
	    26 * (mn) * 1000000 + DIV_ROUND_CLOSEST(26 * k * 100,
						    1048576) * 10000;
	clk_debug("rate %u, k %u, mn %u\n", rate, k, mn);
#elif defined(CONFIG_ARCH_SCX35L)
	unsigned int k = 0, mn, cfg1, cfg2;
	cfg1 = __raw_readl(pll->m.mul.reg);
	cfg2 = __raw_readl((u32 *)pll->m.mul.reg + 1);
	mn = (cfg2 & BITS_PLL_NINT(~0)) >> SHFT_PLL_NINT;

	/* FIXME: Kint only valid while sdm_en = 1 */
	if ((cfg1 & BIT_PLL_SDM_EN))
		k = (cfg2 & BITS_PLL_KINT(~0)) >> SHFT_PLL_KINT;

	rate =
	    26 * (mn) * 1000000 + DIV_ROUND_CLOSEST(26 * k * 100,
						    1048576) * 10000;
	clk_debug("rate %u, k %u, mn %u\n", rate, k, mn);
#else
	unsigned int refin, mn;
	refin = __pll_get_refin_rate(pll->m.mul.reg);
	mn = (__raw_readl(pll->m.mul.reg)
	      & pll->m.mul.msk) >> __ffs(pll->m.mul.msk);

	rate = refin * mn;
	clk_debug("rate %u, refin %u, mn %u\n", rate, refin, mn);
#endif
	return (unsigned long)rate;
}

static long sprd_clk_adjustable_pll_round_rate(struct clk_hw *hw,
					       unsigned long rate,
					       unsigned long *prate)
{
	//struct clk_sprd *pll = to_clk_sprd(hw);
	clk_debug("rate %lu, %lu\n", rate, *prate);
	return rate;
}

static void __pllreg_write(void *reg, u32 val, u32 msk)
{
	__raw_writel((__raw_readl(reg) & ~msk) | val, reg);
}

static int __pll_enable_time(struct clk_hw *hw, unsigned long old_rate)
{
	/* FIXME: for mpll, each step (100MHz) takes 50us */
	u32 rate = sprd_clk_adjustable_pll_recalc_rate(hw, 0) / 1000000;
	int dly = abs(rate - old_rate) * 50 / 100;
	WARN_ON(dly > 1000);
	udelay(dly);
	return 0;
}

static int sprd_clk_adjustable_pll_set_rate(struct clk_hw *hw,
					    unsigned long rate,
					    unsigned long parent_rate)
{
	struct clk_sprd *pll = to_clk_sprd(hw);
	u32 old_rate = sprd_clk_adjustable_pll_recalc_rate(hw, 0) / 1000000;
#ifdef CONFIG_ARCH_SCX30G
	u32 k, mn, cfg1;

	mn = (rate / 1000000) / 26;
	k = DIV_ROUND_CLOSEST(((rate / 10000) - 26 * mn * 100) * 1048576,
			      26 * 100);

	cfg1 = BITS_PLL_NINT(mn);
	if (k)
		cfg1 |= BITS_PLL_KINT(k) | BIT_PLL_SDM_EN;

	clk_debug("%s rate %u, k %u, mn %u\n", __clk_get_name(hw->clk),
		  (u32) rate, k, mn);
	__pllreg_write(pll->m.mul.reg, cfg1,
		       BITS_PLL_KINT(~0) | BITS_PLL_NINT(~0) | BIT_PLL_SDM_EN);
#elif defined(CONFIG_ARCH_SCX35L)
	u32 k, mn, cfg2;
	u32 cfg1 = 0;

	mn = (rate / 1000000) / 26;
	k = DIV_ROUND_CLOSEST(((rate / 10000) - 26 * mn * 100) * 1048576,
			      26 * 100);

	cfg2 = BITS_PLL_NINT(mn);
	if (k) {
		cfg2 |= BITS_PLL_KINT(k);
		cfg1 |= BIT_PLL_SDM_EN;
	}

	clk_debug("%s rate %u, k %u, mn %u\n", __clk_get_name(hw->clk),
		  (u32) rate, k, mn);

	__pllreg_write((u32 *)pll->m.mul.reg + 1, cfg2, BITS_PLL_KINT(~0) | BITS_PLL_NINT(~0));
	/* FIXME: pll clock set should not two-step */
	__pllreg_write(pll->m.mul.reg, cfg1, BIT_PLL_SDM_EN);
#else
	u32 refin, mn;
	refin = __pll_get_refin_rate(pll->m.mul.reg);
	mn = rate / refin;
	clk_debug("rate %u, refin %u, mn %u\n", (u32) rate, refin, mn);
	if (mn <= pll->m.mul.msk >> __ffs(pll->m.mul.msk)) {
		__pllreg_write(pll->m.mul.reg, mn << __ffs(pll->m.mul.msk),
			       pll->m.mul.msk);
	}
#endif
	__pll_enable_time(hw, old_rate);
	return 0;
}

/* FIXME:
 * Inherit from clk-mux.c
 */
static u8 sprd_clk_mux_get_parent(struct clk_hw *hw)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	if (!c->m.mux_hw->clk)
		c->m.mux_hw->clk = c->hw.clk;
	clk_debug("%s\n", __clk_get_name(hw->clk));
	return clk_mux_ops.get_parent(c->m.mux_hw);
}

static int sprd_clk_mux_set_parent(struct clk_hw *hw, u8 index)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	if (!c->m.mux_hw->clk)
		c->m.mux_hw->clk = c->hw.clk;
	clk_debug("%s %d\n", __clk_get_name(hw->clk), (u32) index);
	return clk_mux_ops.set_parent(c->m.mux_hw, index);
}

/* FIXME:
 * Inherit from clk-divider.c
 */

static unsigned long sprd_clk_divider_recalc_rate(struct clk_hw *hw,
						  unsigned long parent_rate)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	if (!c->d.div_hw->clk)
		c->d.div_hw->clk = c->hw.clk;
	clk_debug("%s %lu\n", __clk_get_name(hw->clk), parent_rate);
#ifdef CONFIG_CPLL_1024M
	if (hw->clk->parent != NULL && hw->clk->parent->parent != NULL)
		if (!strcmp(__clk_get_name(hw->clk->parent->parent), "clk_cpll"))
			if(!strcmp(__clk_get_name(hw->clk),"clk_dcam") || !strcmp(__clk_get_name(hw->clk), "clk_gpu") || !strcmp(__clk_get_name(hw->clk), "clk_isp"))
				return hw->clk->rate;
#endif
	return clk_divider_ops.recalc_rate(c->d.div_hw, parent_rate);
}

static long sprd_clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
					unsigned long *prate)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	if (!c->d.div_hw->clk)
		c->d.div_hw->clk = c->hw.clk;
	clk_debug("%s rate %lu %lu\n", __clk_get_name(hw->clk), rate,
		  (prate) ? *prate : 0);

	/* FIXME: see clk_divider_bestdiv()
	 * bestdiv = DIV_ROUND_UP(parent_rate, rate);
	 * so round rate would be lower than rate
	 */
	return rate;
	//return clk_divider_ops.round_rate(c->d.div_hw, rate, prate);
}

static int sprd_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
				     unsigned long parent_rate)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	if (!c->d.div_hw->clk)
		c->d.div_hw->clk = c->hw.clk;
	clk_debug("%s %lu %lu\n", __clk_get_name(hw->clk), rate, parent_rate);
	return clk_divider_ops.set_rate(c->d.div_hw, rate, parent_rate);
}

const struct clk_ops sprd_clk_fixed_pll_ops = {
	.prepare = sprd_clk_prepare,
	.unprepare = sprd_clk_unprepare,
	.is_prepared = sprd_clk_is_prepared,
	.recalc_rate = sprd_clk_fixed_pll_recalc_rate,
};

const struct clk_ops sprd_clk_adjustable_pll_ops = {
	.prepare = sprd_clk_prepare,
	.unprepare = sprd_clk_unprepare,
	.round_rate = sprd_clk_adjustable_pll_round_rate,
	.set_rate = sprd_clk_adjustable_pll_set_rate,
	.recalc_rate = sprd_clk_adjustable_pll_recalc_rate,
};

const struct clk_ops sprd_clk_gate_ops = {
	.prepare = sprd_clk_prepare,
	.unprepare = sprd_clk_unprepare,
	.enable = sprd_clk_enable,
	.disable = sprd_clk_disable,
	.is_enabled = sprd_clk_is_enable,
};

const struct clk_ops sprd_clk_mux_ops = {
	.enable = sprd_clk_enable,
	.disable = sprd_clk_disable,
	.get_parent = sprd_clk_mux_get_parent,
	.set_parent = sprd_clk_mux_set_parent,
#if defined(CONFIG_ARCH_SCX35LT8)
	.prepare = sprd_clk_prepare,
	.unprepare = sprd_clk_unprepare,
#endif
};

const struct clk_ops sprd_clk_divider_ops = {
	.enable = sprd_clk_enable,
	.disable = sprd_clk_disable,
	.recalc_rate = sprd_clk_divider_recalc_rate,
	.round_rate = sprd_clk_divider_round_rate,
	.set_rate = sprd_clk_divider_set_rate,
};

const struct clk_ops sprd_clk_composite_ops = {
	.enable = sprd_clk_enable,
	.disable = sprd_clk_disable,
	.get_parent = sprd_clk_mux_get_parent,
	.set_parent = sprd_clk_mux_set_parent,
	.recalc_rate = sprd_clk_divider_recalc_rate,
	.round_rate = sprd_clk_divider_round_rate,
	.set_rate = sprd_clk_divider_set_rate,
};

static inline void __mmreg_setclr(struct clk_hw *hw, void *reg, u32 msk,
				  int is_set)
{
	if (!reg)
		return;

	clk_debug("%s %s %p[%x]\n", __clk_get_name(hw->clk),
		  (is_set) ? "SET" : "CLR", reg, (u32) msk);

	if (is_set)
		__raw_writel(__raw_readl((void *)reg) | msk, (void *)reg);
	else
		__raw_writel(__raw_readl((void *)reg) & ~msk, (void *)reg);
}

#define __mmreg_set(hw, reg, msk)	__mmreg_setclr(hw, reg, msk, 1)
#define __mmreg_clr(hw, reg, msk)	__mmreg_setclr(hw, reg, msk, 0)
static int sprd_mm_domain_state(struct clk_hw *hw)
{
	/* FIXME: rtc domain */
	u32 power_state1, power_state2, power_state3;
	unsigned long timeout = jiffies + msecs_to_jiffies(__SPRD_MM_TIMEOUT);

	do {
		cpu_relax();
		power_state1 =
		    __raw_readl((void *)REG_PMU_APB_PWR_STATUS0_DBG) &
		    BITS_PD_MM_TOP_STATE(-1);
		power_state2 =
		    __raw_readl((void *)REG_PMU_APB_PWR_STATUS0_DBG) &
		    BITS_PD_MM_TOP_STATE(-1);
		power_state3 =
		    __raw_readl((void *)REG_PMU_APB_PWR_STATUS0_DBG) &
		    BITS_PD_MM_TOP_STATE(-1);
		BUG_ON(time_after(jiffies, timeout));
	} while (power_state1 != power_state2 || power_state2 != power_state3);

	return (power_state1);
}

static int sprd_mm_domain_is_ready(struct clk_hw *hw)
{
#ifdef CONFIG_ARCH_SCX35
	return sprd_mm_domain_state(hw) == BITS_PD_MM_TOP_STATE(0);
#else
	return 1;
#endif
}

static int sprd_mm_domain_is_shutdown(struct clk_hw *hw)
{
#ifdef CONFIG_ARCH_SCX35
	return sprd_mm_domain_state(hw) == BITS_PD_MM_TOP_STATE(7);
#else
	return 0;
#endif
}

/* FIXME: mm domain soft-retention,sharkls support clk_vpp */
#if defined(CONFIG_MACH_SP9830I) || defined(CONFIG_ARCH_SCX30G2) || defined(CONFIG_ARCH_SCX35LT8)
static u32 saved_mm_ckg[11];
#else
static u32 saved_mm_ckg[10];
#endif
static void sprd_mm_domain_save(struct clk_hw *hw)
{
#ifdef CONFIG_ARCH_SCX35
	u32 *ckg = (u32 *) REG_MM_CLK_MM_AHB_CFG;
	int i;
	BUG_ON(!(sprd_mm_domain_is_ready(hw)));
	BUG_ON(!(__raw_readl((void *)REG_AON_APB_APB_EB0) & BIT_MM_EB));
	for (i = 0; i < ARRAY_SIZE(saved_mm_ckg); i++, ckg++) {
		saved_mm_ckg[i] = __raw_readl(ckg);
	}
	clk_debug("ahb %08x sensor %08x vsp %08x\n", saved_mm_ckg[0],
		  saved_mm_ckg[1], saved_mm_ckg[4]);
#endif
}

static void sprd_mm_domain_restore(struct clk_hw *hw)
{
#ifdef CONFIG_ARCH_SCX35
	u32 *ckg = (u32 *) REG_MM_CLK_MM_AHB_CFG;
	int i;
	clk_debug("ahb %08x sensor %08x vsp %08x\n", saved_mm_ckg[0],
		  saved_mm_ckg[1], saved_mm_ckg[4]);
	BUG_ON(!(sprd_mm_domain_is_ready(hw)));
	BUG_ON(!(__raw_readl((void *)REG_AON_APB_APB_EB0) & BIT_MM_EB));
	for (i = 0; i < ARRAY_SIZE(saved_mm_ckg); i++, ckg++) {
		__raw_writel(saved_mm_ckg[i], ckg);
	}
#endif
}

static atomic_t domain_cnt;
#define __atomic_inc_and_test(v)	(atomic_add_return(1, v) == 1)

static int __sprd_clk_mm_domain_prepare(struct clk_hw *hw)
{
	clk_debug("counter %d\n", domain_cnt.counter);
	if (__atomic_inc_and_test(&domain_cnt)) {
		unsigned long timeout =
		    jiffies + msecs_to_jiffies(__SPRD_MM_TIMEOUT);

		__glbreg_clr(hw, (void *)REG_PMU_APB_PD_MM_TOP_CFG,
			     BIT_PD_MM_TOP_FORCE_SHUTDOWN);
		/* FIXME: wait a moment for mm domain stable
		 */
		while (!sprd_mm_domain_is_ready(hw)
		       && !time_after(jiffies, timeout)) {
			udelay(50);
			cpu_relax();
		}
		WARN(!sprd_mm_domain_is_ready(hw),
		     "MM TOP CFG 0x%08x, STATE 0x%08x\n",
		     __raw_readl((void *)REG_PMU_APB_PD_MM_TOP_CFG),
		     __raw_readl((void *)REG_PMU_APB_PWR_STATUS0_DBG));
		__glbreg_set(hw, (void *)REG_AON_APB_APB_EB0, BIT_MM_EB);
		__glbreg_set(hw, (void *)REG_MM_AHB_AHB_EB, BIT_MM_CKG_EB);
		__mmreg_set(hw, (void *)REG_MM_AHB_GEN_CKG_CFG,
			    BIT_MM_MTX_AXI_CKG_EN | BIT_MM_AXI_CKG_EN);
		__mmreg_set(hw, (void *)REG_MM_CLK_MM_AHB_CFG, 0x3);	/* set mm ahb 153.6MHz */
		sprd_mm_domain_restore(hw);
	}
	return 0;
}

static void __sprd_clk_mm_domain_unprepare(struct clk_hw *hw)
{
	clk_debug("counter %d\n", domain_cnt.counter);
	if (atomic_dec_and_test(&domain_cnt)) {	/* last */
		unsigned long timeout =
		    jiffies + msecs_to_jiffies(__SPRD_MM_TIMEOUT);

		if (!(__raw_readl((void *)REG_AON_APB_APB_EB0) & BIT_MM_EB))
			__glbreg_set(hw, (void *)REG_AON_APB_APB_EB0, BIT_MM_EB);
		if (!(__raw_readl((void *)REG_MM_AHB_AHB_EB) & BIT_MM_CKG_EB))
			__glbreg_set(hw, (void *)REG_MM_AHB_AHB_EB, BIT_MM_CKG_EB);
		/* FIXME: save all mm ckg regs before disable mm */
		sprd_mm_domain_save(hw);
		__glbreg_clr(hw, (void *)REG_AON_APB_APB_EB0, BIT_MM_EB);

		__glbreg_set(hw, (void *)REG_PMU_APB_PD_MM_TOP_CFG,
			     BIT_PD_MM_TOP_FORCE_SHUTDOWN);
		/* FIXME: wait a moment for mm domain stable
		 */
		while (!sprd_mm_domain_is_shutdown(hw)
		       && !time_after(jiffies, timeout)) {
			udelay(50);
			cpu_relax();
		}
		WARN(!sprd_mm_domain_is_shutdown(hw),
		     "MM TOP CFG 0x%08x, STATE 0x%08x\n",
		     __raw_readl((void *)REG_PMU_APB_PD_MM_TOP_CFG),
		     __raw_readl((void *)REG_PMU_APB_PWR_STATUS0_DBG));
	}
}

static int __sprd_clk_mm_prepare_enable(struct clk_hw *hw)
{
#ifdef CONFIG_ARCH_SCX35
	__sprd_clk_mm_domain_prepare(hw);

	if (!(__raw_readl((void *)REG_AON_APB_APB_EB0) & BIT_MM_EB)) {
		__glbreg_set(hw, (void *)REG_AON_APB_APB_EB0, BIT_MM_EB);
		if (!(__raw_readl((void *)REG_MM_AHB_AHB_EB) & BIT_MM_CKG_EB)) {
			__glbreg_set(hw, (void *)REG_MM_AHB_AHB_EB,
				     BIT_MM_CKG_EB);
			__mmreg_set(hw, (void *)REG_MM_AHB_GEN_CKG_CFG,
				    BIT_MM_MTX_AXI_CKG_EN | BIT_MM_AXI_CKG_EN);
			__mmreg_set(hw, (void *)REG_MM_CLK_MM_AHB_CFG, 0x3);	/* set mm ahb 153.6MHz */
		}
	}
#endif
	return 0;
}

static void __sprd_clk_mm_disable_unprepare(struct clk_hw *hw)
{
	__sprd_clk_mm_domain_unprepare(hw);

	if ((__raw_readl((void *)REG_AON_APB_APB_EB0) & BIT_MM_EB)) {
		__glbreg_clr(hw, (void *)REG_AON_APB_APB_EB0, BIT_MM_EB);
	}
}

static int __sprd_clk_mm_enable(struct clk_hw *hw)
{
	return 0;
}

static void __sprd_clk_mm_disable(struct clk_hw *hw)
{
}

static int __internal_clk_mm_prepare(struct clk_hw *hw)
{
#ifdef CONFIG_ARCH_SCX35
	if (!sprd_mm_domain_is_ready(hw)) {
		unsigned long timeout =
		    jiffies + msecs_to_jiffies(__SPRD_MM_TIMEOUT);

		__glbreg_clr(hw, (void *)REG_PMU_APB_PD_MM_TOP_CFG,
			     BIT_PD_MM_TOP_FORCE_SHUTDOWN);
		/* FIXME: wait a moment for mm domain stable
		 */
		while (!sprd_mm_domain_is_ready(hw)
		       && !time_after(jiffies, timeout)) {
			udelay(50);
			cpu_relax();
		}
		WARN(!sprd_mm_domain_is_ready(hw),
		     "MM TOP CFG 0x%08x, STATE 0x%08x\n",
		     __raw_readl((void *)REG_PMU_APB_PD_MM_TOP_CFG),
		     __raw_readl((void *)REG_PMU_APB_PWR_STATUS0_DBG));
		__glbreg_set(hw, (void *)REG_AON_APB_APB_EB0, BIT_MM_EB);
		__glbreg_set(hw, (void *)REG_MM_AHB_AHB_EB, BIT_MM_CKG_EB);
		__mmreg_set(hw, (void *)REG_MM_AHB_GEN_CKG_CFG,
			    BIT_MM_MTX_AXI_CKG_EN | BIT_MM_AXI_CKG_EN);
		__mmreg_set(hw, (void *)REG_MM_CLK_MM_AHB_CFG, 0x3);	/* set mm ahb 153.6MHz */
	}

	if (!(__raw_readl((void *)REG_AON_APB_APB_EB0) & BIT_MM_EB)) {
		__glbreg_set(hw, (void *)REG_AON_APB_APB_EB0, BIT_MM_EB);
		if (!(__raw_readl((void *)REG_MM_AHB_AHB_EB) & BIT_MM_CKG_EB)) {
			__glbreg_set(hw, (void *)REG_MM_AHB_AHB_EB,
				     BIT_MM_CKG_EB);
			__mmreg_set(hw, (void *)REG_MM_AHB_GEN_CKG_CFG,
				    BIT_MM_MTX_AXI_CKG_EN | BIT_MM_AXI_CKG_EN);
			__mmreg_set(hw, (void *)REG_MM_CLK_MM_AHB_CFG, 0x3);	/* set mm ahb 153.6MHz */
		}
	}
#endif
	return 0;
}

static int sprd_mm_clk_prepare(struct clk_hw *hw)
{
	__internal_clk_mm_prepare(hw);
	return sprd_clk_prepare(hw);
}

static int sprd_mm_clk_enable(struct clk_hw *hw)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	if (sprd_mm_domain_is_ready(hw)) {
		__mmreg_set(hw, c->enb.reg, (u32) c->enb.msk);
	}
	return 0;
}

static void sprd_mm_clk_disable(struct clk_hw *hw)
{
	struct clk_sprd *c = to_clk_sprd(hw);
	if (sprd_mm_domain_is_ready(hw)) {
		__mmreg_clr(hw, c->enb.reg, (u32) c->enb.msk);
	}
}

static int sprd_mm_clk_is_enable(struct clk_hw *hw)
{
	if (sprd_mm_domain_is_ready(hw)) {
		return sprd_clk_is_enable(hw);
	}
	return 0;
}

static u8 sprd_mm_clk_mux_get_parent(struct clk_hw *hw)
{
	if (sprd_mm_domain_is_ready(hw)) {
		return sprd_clk_mux_get_parent(hw);
	}
	return 0;
}

static int sprd_mm_clk_mux_set_parent(struct clk_hw *hw, u8 index)
{
	if (sprd_mm_domain_is_ready(hw)) {
		return sprd_clk_mux_set_parent(hw, index);
	}
	return 0;
}

static unsigned long sprd_mm_clk_divider_recalc_rate(struct clk_hw *hw,
						     unsigned long parent_rate)
{
	if (sprd_mm_domain_is_ready(hw)) {
		return sprd_clk_divider_recalc_rate(hw, parent_rate);
	}
	return parent_rate;
}

static int sprd_mm_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
					unsigned long parent_rate)
{
	if (sprd_mm_domain_is_ready(hw)) {
		return sprd_clk_divider_set_rate(hw, rate, parent_rate);
	}
	return 0;
}

const struct clk_ops sprd_clk_mm_gate_ops = {
	.prepare = __sprd_clk_mm_prepare_enable,
	.unprepare = __sprd_clk_mm_disable_unprepare,
	.enable = __sprd_clk_mm_enable,
	.disable = __sprd_clk_mm_disable,
	.is_enabled = sprd_clk_is_enable,
};

const struct clk_ops sprd_clk_mm_domain_gate_ops = {
	.prepare = __sprd_clk_mm_domain_prepare,
	.unprepare = __sprd_clk_mm_domain_unprepare,
};

const struct clk_ops sprd_mm_clk_gate_ops = {
	.prepare = sprd_mm_clk_prepare,
	.unprepare = sprd_clk_unprepare,
	.enable = sprd_mm_clk_enable,
	.disable = sprd_mm_clk_disable,
	.is_enabled = sprd_mm_clk_is_enable,
};

const struct clk_ops sprd_mm_clk_mux_ops = {
	.prepare = sprd_mm_clk_prepare,
	.unprepare = sprd_clk_unprepare,
	.enable = sprd_mm_clk_enable,
	.disable = sprd_mm_clk_disable,
	.get_parent = sprd_mm_clk_mux_get_parent,
	.set_parent = sprd_mm_clk_mux_set_parent,
};

const struct clk_ops sprd_mm_clk_composite_ops = {
	.enable = sprd_mm_clk_enable,
	.disable = sprd_mm_clk_disable,
	.get_parent = sprd_mm_clk_mux_get_parent,
	.set_parent = sprd_mm_clk_mux_set_parent,
	.recalc_rate = sprd_mm_clk_divider_recalc_rate,
	.round_rate = sprd_clk_divider_round_rate,
	.set_rate = sprd_mm_clk_divider_set_rate,
};

static void __init file_clk_data(struct clk *clk, const char *clk_name);
static void __init sprd_clk_register(struct device *dev,
				     struct device_node *node,
				     struct clk_sprd *c,
				     struct clk_init_data *init);

/**
 * of_sprd_fixed_clk_setup() - Setup function for simple sprd fixed rate clock
 */
static void __init of_sprd_fixed_clk_setup(struct device_node *node)
{
	struct clk *clk = NULL;
	const char *clk_name = node->name;
	u32 rate;

	if (of_property_read_u32(node, "clock-frequency", &rate))
		return;

	of_property_read_string(node, "clock-output-names", &clk_name);

	clk = clk_register_fixed_rate(NULL, clk_name, NULL, CLK_IS_ROOT, rate);
	if (!IS_ERR(clk)) {
		of_clk_add_provider(node, of_clk_src_simple_get, clk);
		clk_register_clkdev(clk, clk_name, 0);
		file_clk_data(clk, clk_name);
	}
	clk_debug("[%p]%s fixed-rate %d\n", clk, clk_name, rate);
}

/**
 * of_sprd_fixed_factor_clk_setup() - Setup function for simple sprd fixed factor clock
 */
void __init of_sprd_fixed_factor_clk_setup(struct device_node *node)
{
	struct clk *clk = NULL;
	const char *clk_name = node->name;
	const char *parent_name;
	u32 div, mult;

	if (of_property_read_u32(node, "clock-div", &div)) {
		pr_err
		    ("%s Fixed factor clock <%s> must have a clock-div property\n",
		     __func__, node->name);
		return;
	}

	if (of_property_read_u32(node, "clock-mult", &mult)) {
		pr_err
		    ("%s Fixed factor clock <%s> must have a clokc-mult property\n",
		     __func__, node->name);
		return;
	}

	of_property_read_string(node, "clock-output-names", &clk_name);
	parent_name = of_clk_get_parent_name(node, 0);

	clk = clk_register_fixed_factor(NULL, clk_name, parent_name, 0,
					mult, div);
	if (!IS_ERR(clk)) {
		of_clk_add_provider(node, of_clk_src_simple_get, clk);
		clk_register_clkdev(clk, clk_name, 0);
		file_clk_data(clk, clk_name);
	}
	clk_debug("[%p]%s parent %s mult %d div %d\n", clk, clk_name,
		  parent_name, mult, div);

	clk_debug("%s RATE %lu\n", __clk_get_name(clk), clk_get_rate(clk));
}

/**
 * of_sprd_fixed_pll_clk_setup() - Setup function for simple sprd fixed-pll clock
 */
static void __init of_sprd_fixed_pll_clk_setup(struct device_node *node)
{
	struct clk_sprd *c;
	struct clk *clk = NULL;
	const char *clk_name = node->name;
	struct clk_init_data init = {
		.name = clk_name,
		.ops = &sprd_clk_fixed_pll_ops,
		.flags = CLK_IS_ROOT,
		.num_parents = 0,
	};
	u32 rate;
	const __be32 *prereg;

	if (of_property_read_u32(node, "clock-frequency", &rate))
		return;

	prereg = of_get_address(node, 0, NULL, NULL);
	if (!prereg) {
		pr_err
		    ("%s Fixed pll clock <%s> must have a prepare reg property\n",
		     __func__, node->name);
		return;
	}

	of_property_read_string(node, "clock-output-names", &clk_name);

	/* allocate fixed-pll clock */
	c = kzalloc(sizeof(struct clk_sprd), GFP_KERNEL);
	if (!c) {
		pr_err("%s: could not allocate sprd fixed-pll clk\n", __func__);
		return;
	}

	/* struct clk_fixed_rate assignments */
	c->m.fixed_rate = rate;

	/* set fixed pll regs */
	of_read_reg(&c->d.pre, prereg);

	sprd_clk_register(NULL, node, c, &init);

	clk_debug("[%p]%s fixed-pll-rate %d, prepare %p[%x]\n", clk, clk_name,
		  rate, c->d.pre.reg, c->d.pre.msk);

	clk_debug("%s RATE %lu\n", __clk_get_name(c->hw.clk),
		  clk_get_rate(c->hw.clk));
}

/**
 * of_sprd_adjustable_pll_clk_setup() - Setup function for simple sprd adjustable-pll clock
 */
static void __init of_sprd_adjustable_pll_clk_setup(struct device_node *node)
{
	struct clk_sprd *c;
	struct clk *clk = NULL;
	const char *clk_name = node->name;
	struct clk_init_data init = {
		.name = clk_name,
		.ops = &sprd_clk_adjustable_pll_ops,
		.flags = CLK_IS_ROOT,
		.num_parents = 0,
	};
	const __be32 *mulreg, *prereg;

	mulreg = of_get_address(node, 0, NULL, NULL);
	if (!mulreg) {
		pr_err
		    ("%s adjustable clock <%s> must have a mul reg property\n",
		     __func__, node->name);
		return;
	}

	prereg = of_get_address(node, 1, NULL, NULL);
	if (!prereg) {
		pr_err
		    ("%s adjustable pll clock <%s> must have a prepare reg property\n",
		     __func__, node->name);
		return;
	}

	of_property_read_string(node, "clock-output-names", &clk_name);

	/* allocate adjustable-pll clock */
	c = kzalloc(sizeof(struct clk_sprd), GFP_KERNEL);
	if (!c) {
		pr_err("%s: could not allocate adjustable-pll clk\n", __func__);
		return;
	}

	/* struct clk_adjustable_pll assignments */
	of_read_reg(&c->m.mul, mulreg);
	of_read_reg(&c->d.pre, prereg);

	/* Flags:
	 * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
	 *  enable the clock.  Setting this flag does the opposite: setting the bit
	 *  disable the clock and clearing it enables the clock
	 */
	if ((unsigned long) c->d.pre.reg & 1) {
		*(u32 *) & c->d.pre.reg &= ~3;
		c->flags |= CLK_GATE_SET_TO_DISABLE;
	}
	sprd_clk_register(NULL, node, c, &init);

	if (prereg)
		clk_debug("[%p]%s mul %p[%x], prepare %p[%x]\n", clk, clk_name,
			  c->m.mul.reg, c->m.mul.msk, c->d.pre.reg,
			  c->d.pre.msk);
	else
		clk_debug("[%p]%s mul %p[%x]\n", clk, clk_name,
			  c->m.mul.reg, c->m.mul.msk);

	clk_debug("%s RATE %lu\n", __clk_get_name(c->hw.clk),
		  clk_get_rate(c->hw.clk));
}

/**
 * of_sprd_gate_clk_setup() - Setup function for simple gate rate clock
 */
static void __init of_sprd_gate_clk_setup(struct device_node *node)
{
	struct clk_sprd *c;
	struct clk *clk = NULL;
	const char *clk_name = node->name;
	const char *parent_name;
	struct clk_init_data init = {
		.name = clk_name,
		.ops = &sprd_clk_gate_ops,
		.flags = CLK_IGNORE_UNUSED,
	};
	const __be32 *enbreg, *prereg;

	enbreg = of_get_address(node, 0, NULL, NULL);
	if (!enbreg) {
		pr_err
		    ("%s gate clock <%s> must have a reg-enb property\n",
		     __func__, node->name);
		return;
	}

	prereg = of_get_address(node, 1, NULL, NULL);
	if (!prereg) {
		//prepare reg is optional
	}

	of_property_read_string(node, "clock-output-names", &clk_name);
	parent_name = of_clk_get_parent_name(node, 0);

	/* allocate the gate */
	c = kzalloc(sizeof(struct clk_sprd), GFP_KERNEL);
	if (!c) {
		pr_err("%s: could not allocate gated clk\n", __func__);
		return;
	}

	if (of_get_property(node, "mm-domain", NULL)) {
		init.ops = &sprd_mm_clk_gate_ops;
	}
#ifdef CONFIG_ARCH_SCX35
	if (0 == strcmp(clk_name, "clk_mm")) {
		init.ops = &sprd_clk_mm_gate_ops;
		__internal_clk_mm_prepare(&c->hw);
		sprd_mm_domain_save(&c->hw);
	}
	else if (0 == strcmp(clk_name, "clk_mm_axi")) {
		init.ops = &sprd_clk_mm_domain_gate_ops;
		parent_name = NULL;	/* FIXME: dummy clock for mm domain prepare */
	}
#endif

	init.parent_names = (parent_name ? &parent_name : NULL);
	init.num_parents = (parent_name ? 1 : 0);

	/* struct clk_gate assignments */
	of_read_reg(&c->enb, enbreg);
	of_read_reg(&c->d.pre, prereg);
	/* Flags:
	 * CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
	 *  enable the clock.  Setting this flag does the opposite: setting the bit
	 *  disable the clock and clearing it enables the clock
	 */
	if ((unsigned long) c->d.pre.reg & 1) {
		*(u32 *) & c->d.pre.reg &= ~3;
		c->flags |= CLK_GATE_SET_TO_DISABLE;
	}

	sprd_clk_register(NULL, node, c, &init);

	if (prereg)
		clk_debug("[%p]%s enable %p[%x] prepare %p[%x]\n", clk,
			  clk_name, c->enb.reg, c->enb.msk, c->d.pre.reg,
			  c->d.pre.msk);
	else
		clk_debug("[%p]%s enable %p[%x]\n", clk, clk_name,
			  c->enb.reg, c->enb.msk);
}

/**
 * of_sprd_composite_clk_setup() - Setup function for simple composite clock
 */
static struct clk_sprd *__init __of_sprd_composite_clk_setup(struct device_node
							     *node, int has_mux,
							     int has_div)
{
	struct clk_sprd *c;
	const char *clk_name = node->name;
	const char *parent_name;
	struct clk_init_data init = {
		.name = clk_name,
		.flags = CLK_IGNORE_UNUSED,
	};
	const __be32 *selreg = NULL, *divreg = NULL, *enbreg, *prereg = NULL;
	int idx = 0;

	if (has_mux)
		selreg = of_get_address(node, idx++, NULL, NULL);

	if (has_div)
		divreg = of_get_address(node, idx++, NULL, NULL);

	enbreg = of_get_address(node, idx++, NULL, NULL);
	if (enbreg) {
		prereg = of_get_address(node, idx++, NULL, NULL);
	}

	of_property_read_string(node, "clock-output-names", &clk_name);

	/* allocate the clock */
	c = kzalloc(sizeof(struct clk_sprd), GFP_KERNEL);
	if (!c) {
		pr_err("%s: could not allocate sprd clk\n", __func__);
		return NULL;
	}

	/* struct clk_sprd assignments */
	of_read_reg(&c->enb, enbreg);

	if (selreg) {
		int i, num_parents;
		struct clk_mux *mux;

		of_read_reg(&c->m.sel, selreg);
		of_read_reg(&c->d.pre, prereg);

		/* Flags:
		* CLK_GATE_SET_TO_DISABLE - by default this clock sets the bit at bit_idx to
		*  enable the clock.  Setting this flag does the opposite: setting the bit
		*  disable the clock and clearing it enables the clock
		*/
	#if defined(CONFIG_ARCH_SCX35LT8)
		if ((unsigned long) c->d.pre.reg & 1) {
			*(u32 *) & c->d.pre.reg &= ~3;
			c->flags |= CLK_GATE_SET_TO_DISABLE;
		}
	#endif
		init.ops = &sprd_clk_mux_ops;
		if (of_get_property(node, "mm-domain", NULL)) {
			init.ops = &sprd_mm_clk_mux_ops;
		}

		/* FIXME: Retrieve the phandle list property */
		of_get_property(node, "clocks", &num_parents);
		init.num_parents = (u8) num_parents / 4;
		mux =
		    kzalloc(sizeof(struct clk_mux) +
			    sizeof(const char *) * init.num_parents,
			    GFP_KERNEL);
		init.parent_names = (const char **)&mux[1];
		clk_debug("parents : ");
		for (i = 0; i < init.num_parents; i++) {
			init.parent_names[i] = of_clk_get_parent_name(node, i);
			pr_debug("[%d]%s ", i, init.parent_names[i]);
		}
		pr_debug("\n");

		/* struct clk_mux assignments */
		mux->reg = c->m.sel.reg;
		mux->shift = __ffs(c->m.sel.msk);
		mux->mask = c->m.sel.msk >> mux->shift;
		mux->flags = 0;
		mux->lock = 0;
		mux->table = 0;
		c->m.mux_hw = &mux->hw;	/* FIXME: should not use m.sel.reg at now */
		clk_debug("mux %u, %x\n", (u32) mux->shift, mux->mask);
	}

	if (divreg) {
		struct clk_divider *div;

		of_read_reg(&c->d.div, divreg);

		div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
		init.ops = &sprd_clk_divider_ops;
		if (!init.num_parents) {
			parent_name = of_clk_get_parent_name(node, 0);;
			init.parent_names = (parent_name ? &parent_name : NULL);
			init.num_parents = (parent_name ? 1 : 0);
			clk_debug("parent %s\n", parent_name);
		}

		/* struct clk_divider assignments */
		div->reg = c->d.div.reg;
		div->shift = __ffs(c->d.div.msk);
		div->width = __ffs(~(c->d.div.msk >> div->shift));
		div->flags = 0;
		div->lock = 0;
		div->table = 0;
		c->d.div_hw = &div->hw;	/* FIXME: should not use d.div.reg at now */
		clk_debug("div %u, %u\n", (u32) div->shift, (u32) div->width);
	}

	if (divreg && selreg) {
		init.ops = &sprd_clk_composite_ops;
		if (of_get_property(node, "mm-domain", NULL)) {
			init.ops = &sprd_mm_clk_composite_ops;
		}
	}

	sprd_clk_register(NULL, node, c, &init);
	return c;

}

#define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)

static void __init of_sprd_muxed_clk_setup(struct device_node *node)
{
	struct clk_sprd *c;
	c = __of_sprd_composite_clk_setup(node, 1, 0);
	if (!c)
		return;

	{
		struct clk_mux *mux = to_clk_mux(c->m.mux_hw);
		if (!mux)
			return;
		clk_debug("[%p]%s select %p[%x] enable %p[%x] prepare %p[%x]\n",
			  c->hw.clk, __clk_get_name(c->hw.clk), mux->reg,
			  c->m.sel.msk, c->enb.reg, c->enb.msk, c->d.pre.reg,
			  c->d.pre.msk);
		clk_debug("%s RATE %lu\n", __clk_get_name(c->hw.clk),
			  clk_get_rate(c->hw.clk));
	}
}

#define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)

static void __init of_sprd_divider_clk_setup(struct device_node *node)
{
	struct clk_sprd *c;
	c = __of_sprd_composite_clk_setup(node, 0, 1);
	if (!c)
		return;
	{
		struct clk_divider *div = to_clk_divider(c->d.div_hw);
		if (!div)
			return;
		clk_debug("[%p]%s divider %p[%x] enable %p[%x]\n", c->hw.clk,
			  __clk_get_name(c->hw.clk), div->reg,
			  c->d.div.msk, c->enb.reg, c->enb.msk);
		clk_debug("%s RATE %lu\n", __clk_get_name(c->hw.clk),
			  clk_get_rate(c->hw.clk));
	}
}

static void __init of_sprd_composite_clk_setup(struct device_node *node)
{
	struct clk_sprd *c;
	c = __of_sprd_composite_clk_setup(node, 1, 1);
	if (!c)
		return;
	{
		struct clk_mux *mux = to_clk_mux(c->m.mux_hw);
		struct clk_divider *div = to_clk_divider(c->d.div_hw);
		if (!mux || !div)
			return;
		clk_debug("[%p]%s select %p[%x] divider %p[%x] enable %p[%x]\n",
			  c->hw.clk, __clk_get_name(c->hw.clk), mux->reg,
			  c->m.sel.msk, div->reg, c->d.div.msk, c->enb.reg,
			  c->enb.msk);
		clk_debug("%s RATE %lu\n", __clk_get_name(c->hw.clk),
			  clk_get_rate(c->hw.clk));
	}
}

/* register the clock */
static struct clk_onecell_data clk_data;
static void __init init_clk_data(struct device_node *node)
{
	struct clk **clks;
	int num = of_get_child_count(node);
	clks = kzalloc(sizeof(struct clk *) * num, GFP_KERNEL);
	if (!clks)
		return;

	clk_data.clks = clks;
	clk_data.clk_num = num;
}

static void __init file_clk_data(struct clk *clk, const char *clk_name)
{
	static int clk_idx = 0;
	/* FIXME: Add oncell clock provider, be careful not to mistake the clock index */
	if (clk_data.clks) {
		clk = clk_get_sys(NULL, clk_name);
		if (!IS_ERR(clk)) {
			clk_info("[%d]%s\n", clk_idx, clk_name);
			clk_data.clks[clk_idx++] = clk;
		}
	}
}

static void __init sprd_clk_register(struct device *dev,
				     struct device_node *node,
				     struct clk_sprd *c,
				     struct clk_init_data *init)
{
	struct clk *clk;
	const char *clk_name = init->name;

	c->hw.init = init;
	clk = clk_register(dev, &c->hw);
	if (!IS_ERR(clk)) {
		of_clk_add_provider(node, of_clk_src_simple_get, clk);
		clk_register_clkdev(clk, clk_name, 0);
		file_clk_data(clk, clk_name);
	}
}

static void __init sprd_clocks_init(struct device_node *node)
{
	init_clk_data(node);
	of_clk_add_provider(node, of_clk_src_onecell_get, &clk_data);
}

/**
 * clk_force_disable - force disable clock output
 * @clk: clock source
 *
 * Forcibly disable the clock output.
 * NOTE: this *will* disable the clock output even if other consumer
 * devices have it enabled. This should be used for situations when device
 * suspend or damage will likely occur if the devices is not disabled.
 */
void clk_force_disable(struct clk *clk)
{
	if (IS_ERR_OR_NULL(clk))
		return;

	clk_debug("clk %p, usage %d\n", clk, __clk_get_enable_count(clk));
	while (__clk_get_enable_count(clk) > 0) {
		clk_disable(clk);
	}
}

EXPORT_SYMBOL_GPL(clk_force_disable);

CLK_OF_DECLARE(scx15_clock, "sprd,scx15-clocks", sprd_clocks_init);
CLK_OF_DECLARE(scx35_clock, "sprd,scx35-clocks", sprd_clocks_init);
CLK_OF_DECLARE(scx30g_clock, "sprd,scx30g-clocks", sprd_clocks_init);
CLK_OF_DECLARE(scx35l_clock, "sprd,scx35l-clocks", sprd_clocks_init);
CLK_OF_DECLARE(fixed_clock, "sprd,fixed-clock", of_sprd_fixed_clk_setup);
CLK_OF_DECLARE(fixed_factor_clock, "sprd,fixed-factor-clock",
	       of_sprd_fixed_factor_clk_setup);
CLK_OF_DECLARE(fixed_pll_clock, "sprd,fixed-pll-clock",
	       of_sprd_fixed_pll_clk_setup);
CLK_OF_DECLARE(adjustable_pll_clock, "sprd,adjustable-pll-clock",
	       of_sprd_adjustable_pll_clk_setup);
CLK_OF_DECLARE(gate_clock, "sprd,gate-clock", of_sprd_gate_clk_setup);
CLK_OF_DECLARE(muxed_clock, "sprd,muxed-clock", of_sprd_muxed_clk_setup);
CLK_OF_DECLARE(divider_clock, "sprd,divider-clock", of_sprd_divider_clk_setup);
CLK_OF_DECLARE(composite_clock, "sprd,composite-dev-clock",
	       of_sprd_composite_clk_setup);
#endif