summaryrefstree
path: root/kernel/drivers/cpufreq/exynos-mp-cpufreq.c
blob: f7c20f5a1a63bb8fd1c2eaf9b4803a09b5507313 (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
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * EXYNOS - MP CPU frequency scaling support for EXYNOS Big.Little series
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 * kind, whether express or implied; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/regulator/consumer.h>
#include <linux/cpufreq.h>
#include <linux/suspend.h>
#include <linux/module.h>
#include <linux/reboot.h>
#include <linux/delay.h>
#include <linux/cpu.h>
#include <linux/pm_qos.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/cpumask.h>
#include <linux/exynos-ss.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/apm-exynos.h>

#include <asm/smp_plat.h>
#include <asm/cputype.h>

#if defined(CONFIG_SEC_PM) && defined(CONFIG_MUIC_NOTIFIER)
#include <linux/muic/muic.h>
#include <linux/muic/muic_notifier.h>
#endif

#include <soc/samsung/cpufreq.h>
#include <soc/samsung/exynos-powermode.h>
#include <soc/samsung/asv-exynos.h>
#include <soc/samsung/tmu.h>
#include <soc/samsung/ect_parser.h>
#include <soc/samsung/exynos-pmu.h>

#define POWER_COEFF_15P		68 /* percore param */
#define POWER_COEFF_7P		10 /* percore  param */

#define VOLT_RANGE_STEP		25000
#define CLUSTER_ID(cl)		(cl ? ID_CL1 : ID_CL0)

#define LIMIT_FREQ_DIVIDER	4

#ifdef CONFIG_SMP
struct lpj_info {
	unsigned long   ref;
	unsigned int    freq;
};

static struct lpj_info global_lpj_ref;
#endif

/* For switcher */
static unsigned int freq_min[CL_END] __read_mostly;	/* Minimum (Big/Little) clock frequency */
static unsigned int freq_max[CL_END] __read_mostly;	/* Maximum (Big/Little) clock frequency */

static struct exynos_dvfs_info *exynos_info[CL_END];
static unsigned int volt_offset;
static struct cpufreq_freqs *freqs[CL_END];

static DEFINE_MUTEX(cpufreq_lock);
static DEFINE_MUTEX(cpufreq_scale_lock);

bool exynos_cpufreq_init_done;
static bool suspend_prepared = false;
#ifdef CONFIG_PM
#ifdef CONFIG_SCHED_HMP
static bool hmp_boosted = false;
#endif
static bool cluster1_hotplugged = false;
#endif

#ifdef CONFIG_SW_SELF_DISCHARGING
static int self_discharging;
#endif

/* Include CPU mask of each cluster */
cluster_type exynos_boot_cluster;
static cluster_type boot_cluster;
static struct cpumask cluster_cpus[CL_END];

DEFINE_PER_CPU(cluster_type, cpu_cur_cluster);

static struct pm_qos_constraints core_max_qos_const[CL_END];

static struct pm_qos_request boot_min_qos[CL_END];
static struct pm_qos_request boot_max_qos[CL_END];
static struct pm_qos_request core_min_qos[CL_END];
static struct pm_qos_request core_max_qos[CL_END];
static struct pm_qos_request core_min_qos_real[CL_END];
static struct pm_qos_request core_max_qos_real[CL_END];
static struct pm_qos_request exynos_mif_qos[CL_END];
static struct pm_qos_request ipa_max_qos[CL_END];
static struct pm_qos_request reboot_max_qos[CL_END];
#if defined(CONFIG_SEC_PM) && defined(CONFIG_MUIC_NOTIFIER)
static struct pm_qos_request jig_boot_max_qos[CL_END];
#endif

static int qos_max_class[CL_END] = {PM_QOS_CLUSTER0_FREQ_MAX, PM_QOS_CLUSTER1_FREQ_MAX};
static int qos_min_class[CL_END] = {PM_QOS_CLUSTER0_FREQ_MIN, PM_QOS_CLUSTER1_FREQ_MIN};
//static int qos_max_default_value[CL_END] = {PM_QOS_CLUSTER0_FREQ_MAX_DEFAULT_VALUE, PM_QOS_CLUSTER1_FREQ_MAX_DEFAULT_VALUE};
static int qos_min_default_value[CL_END] = {PM_QOS_CLUSTER0_FREQ_MIN_DEFAULT_VALUE, PM_QOS_CLUSTER1_FREQ_MIN_DEFAULT_VALUE};

/* For limit number of online cpus through cpuhotplug */
struct pm_qos_request cpufreq_cpu_hotplug_max_request;

/*
 * CPUFREQ init notifier
 */
static BLOCKING_NOTIFIER_HEAD(exynos_cpufreq_init_notifier_list);

int exynos_cpufreq_init_register_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&exynos_cpufreq_init_notifier_list, nb);
}
EXPORT_SYMBOL(exynos_cpufreq_init_register_notifier);

int exynos_cpufreq_init_unregister_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&exynos_cpufreq_init_notifier_list, nb);
}
EXPORT_SYMBOL(exynos_cpufreq_init_unregister_notifier);

static int exynos_cpufreq_init_notify_call_chain(unsigned long val)
{
	int ret = blocking_notifier_call_chain(&exynos_cpufreq_init_notifier_list, val, NULL);

	return notifier_to_errno(ret);
}

static BLOCKING_NOTIFIER_HEAD(exynos_cpufreq_smpl_warn_notifier_list);

static int exynos_cpufreq_smpl_warn_register_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&exynos_cpufreq_smpl_warn_notifier_list, nb);
}

static int exynos_cpufreq_smpl_warn_unregister_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&exynos_cpufreq_smpl_warn_notifier_list, nb);
}

int exynos_cpufreq_smpl_warn_notify_call_chain(void)
{
	int ret = blocking_notifier_call_chain(&exynos_cpufreq_smpl_warn_notifier_list, 0, NULL);
	return notifier_to_errno(ret);
}
EXPORT_SYMBOL(exynos_cpufreq_smpl_warn_notify_call_chain);

static int exynos_cpufreq_smpl_warn_notifier_call(
					struct notifier_block *notifer,
					unsigned long event, void *v)
{
	if (!exynos_info[CL_ONE]->en_smpl)
		return NOTIFY_OK;

	if (exynos_info[CL_ONE]->clear_smpl)
		exynos_info[CL_ONE]->clear_smpl();

	pr_info("%s : SMPL_WARN is cleared\n",__func__);

	return NOTIFY_OK;
}

static struct notifier_block exynos_cpufreq_smpl_warn_notifier = {
	.notifier_call = exynos_cpufreq_smpl_warn_notifier_call,
};


#if defined(CONFIG_EXYNOS_BIG_FREQ_BOOST)
static void exynos_cpufreq_print_max_freq(void)
{
	int i, idx;

	pr_info("Boost Frequency\n");
	for (i = 1; i <= NR_CLUST1_CPUS; i++) {
		idx = exynos_info[CL_ONE]->max_support_idx_table[i];
		pr_info("Number of onlince core: %d -> max frequency: %d\n",
			i, exynos_info[CL_ONE]->freq_table[idx].frequency);
	}
}

/* input num of online core, return possible max freq */
static unsigned int exynos_cpufreq_get_possible_max_freq(int num_of_online)
{
	int idx;

	if (num_of_online > NR_CLUST1_CPUS) {
		pr_err("Input value is invalied! return possible max frequency\n");
		idx = exynos_info[CL_ONE]->max_support_idx_table[NR_CLUST1_CPUS];
	} else {
		idx = exynos_info[CL_ONE]->max_support_idx_table[num_of_online];
	}

	return  exynos_info[CL_ONE]->freq_table[idx].frequency;
}

static void exynos_cpufreq_verify_possible_freq(int *new_index)
{
	unsigned int cpu;
	unsigned int off_cnt = 0;

	for (cpu = nr_cpu_ids - 1; cpu >= NR_CLUST0_CPUS; cpu--)
		if (!cpu_online(cpu) && !exynos_cpu.power_state(cpu))
			off_cnt++;

	/* Check whether new freq is possible freq */
	if (*new_index >= exynos_info[CL_ONE]->max_support_idx_table[NR_CLUST1_CPUS - off_cnt])
		return;

	pr_err("Can't use boost freq (freq %d -> %d, big off_cnt %d\n",
			freqs[CL_ONE]->old, freqs[CL_ONE]->new, off_cnt);
	BUG();

	/* change index and new_freq to possible value */
	*new_index = exynos_info[CL_ONE]->max_support_idx_table[NR_CLUST1_CPUS - off_cnt];
	freqs[CL_ONE]->new = exynos_info[CL_ONE]->freq_table[*new_index].frequency;
}
#endif

static unsigned int get_limit_voltage(unsigned int voltage)
{
	BUG_ON(!voltage);
	if (voltage > LIMIT_COLD_VOLTAGE)
		return voltage;

	if (voltage + volt_offset > LIMIT_COLD_VOLTAGE)
		return LIMIT_COLD_VOLTAGE;

	if (ENABLE_MIN_COLD && volt_offset
		&& (voltage + volt_offset) < MIN_COLD_VOLTAGE)
		return MIN_COLD_VOLTAGE;

	return voltage + volt_offset;
}

static void init_cpumask_cluster_set(cluster_type cluster)
{
	unsigned int i;
	cluster_type set_cluster;

	for_each_cpu(i, cpu_possible_mask) {
		set_cluster = cluster;

		if ((cluster == CL_ZERO) && (i >= NR_CLUST0_CPUS)) {
			set_cluster = CL_ONE;
		} else if ((cluster == CL_ONE) && (i >= NR_CLUST1_CPUS)) {
			set_cluster = CL_ZERO;
		}

		cpumask_set_cpu(i, &cluster_cpus[set_cluster]);
		per_cpu(cpu_cur_cluster, i) = set_cluster;
	}
}

/*
 * get_cur_cluster - return current cluster
 *
 * You may reference this fuction directly, but it cannot be
 * standard of judging current cluster. If you make a decision
 * of operation by this function, it occurs system hang.
 */
static cluster_type get_cur_cluster(unsigned int cpu)
{
	return per_cpu(cpu_cur_cluster, cpu);
}

static unsigned int clk_get_freq(cluster_type cl)
{
	if (exynos_info[cl]->get_freq)
		return exynos_info[cl]->get_freq();

	else if (exynos_info[cl]->cpu_clk)
		return clk_get_rate(exynos_info[cl]->cpu_clk);

	pr_err("%s: clk_get_freq failed\n", cl? "CL_ONE" : "CL_ZERO");

	return 0;
}

static void set_boot_freq(cluster_type cluster)
{

	if (exynos_info[cluster]) {
		exynos_info[cluster]->boot_freq
				= clk_get_freq(cluster);
		pr_info("Cluster[%d] BootFreq: %dKHz\n", cluster,
						exynos_info[cluster]->boot_freq);
	}

	return;
}

static void set_resume_freq(cluster_type cluster)
{

	if (exynos_info[cluster])
		exynos_info[cluster]->resume_freq
				= clk_get_freq(cluster);
}

static unsigned int get_freq_volt(int cluster, unsigned int target_freq, int *idx)
{
	int index;
	int i;

	struct cpufreq_frequency_table *table = exynos_info[cluster]->freq_table;

	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
		unsigned int freq = table[i].frequency;
		if (freq == CPUFREQ_ENTRY_INVALID)
			continue;

		if (target_freq == freq) {
			index = i;
			break;
		}
	}

	if (table[i].frequency == CPUFREQ_TABLE_END) {
		if (exynos_info[cluster]->boot_freq_idx != -1 &&
			target_freq == exynos_info[cluster]->boot_freq)
			index = exynos_info[cluster]->boot_freq_idx;
		else
			return -EINVAL;
	}

	if (idx)
		*idx = index;

	return exynos_info[cluster]->volt_table[index];
}

static unsigned int get_boot_freq(unsigned int cluster)
{
	if (exynos_info[cluster] == NULL)
		return 0;

	return exynos_info[cluster]->boot_freq;
}

static unsigned int get_boot_volt(int cluster)
{
	unsigned int boot_freq = get_boot_freq(cluster);

	return get_freq_volt(cluster, boot_freq, NULL);
}

static unsigned int get_resume_freq(unsigned int cluster)
{
	if (exynos_info[cluster] == NULL) {
		BUG_ON(1);
		return 0;
	}
	return exynos_info[cluster]->resume_freq;
}

int exynos_verify_speed(struct cpufreq_policy *policy)
{
	unsigned int cur = get_cur_cluster(policy->cpu);

	return cpufreq_frequency_table_verify(policy,
				exynos_info[cur]->freq_table);
}

struct cpufreq_frequency_table *cpufreq_get_info_table(unsigned int cpu)
{
	unsigned int cur = get_cur_cluster(cpu);
	struct cpufreq_frequency_table *freq_table
					= exynos_info[cur]->freq_table;

	return freq_table;
}

unsigned int exynos_getspeed_cluster(cluster_type cluster)
{
	return clk_get_freq(cluster);
}

unsigned int exynos_getspeed(unsigned int cpu)
{
	unsigned int cur = get_cur_cluster(cpu);

	if (check_cluster_idle_state(cpu))
		return freqs[cur]->old;
	else
		return exynos_getspeed_cluster(cur);
}

static unsigned int exynos_get_safe_volt(unsigned int old_index,
					unsigned int new_index,
					unsigned int cur)
{
	unsigned int safe_arm_volt = 0;
	struct cpufreq_frequency_table *freq_table
					= exynos_info[cur]->freq_table;
	unsigned int *volt_table = exynos_info[cur]->volt_table;

	/*
	 * ARM clock source will be changed APLL to MPLL temporary
	 * To support this level, need to control regulator for
	 * reguired voltage level
	 */
	if (exynos_info[cur]->need_apll_change != NULL) {
		if (exynos_info[cur]->need_apll_change(old_index, new_index) &&
			(freq_table[new_index].frequency < exynos_info[cur]->mpll_freq_khz) &&
			(freq_table[old_index].frequency < exynos_info[cur]->mpll_freq_khz)) {
				safe_arm_volt = volt_table[exynos_info[cur]->pll_safe_idx];
		}
	}

	return safe_arm_volt;
}

/* Determine valid target frequency using freq_table */
int exynos5_frequency_table_target(struct cpufreq_policy *policy,
				   struct cpufreq_frequency_table *table,
				   unsigned int target_freq,
				   unsigned int relation,
				   unsigned int *index)
{
	unsigned int i;

	if (!cpu_online(policy->cpu))
		return -EINVAL;

	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
		unsigned int freq = table[i].frequency;
		if (freq == CPUFREQ_ENTRY_INVALID)
			continue;

		if (target_freq == freq) {
			*index = i;
			break;
		}
	}

	if (table[i].frequency == CPUFREQ_TABLE_END) {
		unsigned int cur = get_cur_cluster(policy->cpu);
		if (exynos_info[cur]->boot_freq_idx != -1 &&
			target_freq == exynos_info[cur]->boot_freq)
			*index = exynos_info[cur]->boot_freq_idx;
		else
			return -EINVAL;
	}

	return 0;
}

static int exynos_set_voltage(unsigned int cur_index,
								unsigned int new_index,
								unsigned int volt,
								unsigned int cluster)
{
	struct regulator *regulator = exynos_info[cluster]->regulator;
	struct cpufreq_frequency_table *freq_table = exynos_info[cluster]->freq_table;
	bool set_abb_first_than_volt = false;
	int ret = 0;

	if (exynos_info[cluster]->abb_table)
		set_abb_first_than_volt = is_set_abb_first(CLUSTER_ID(cluster),
										freq_table[cur_index].frequency,
										freq_table[new_index].frequency);

	if (!set_abb_first_than_volt) {
		ret = regulator_set_voltage(regulator, volt, volt + VOLT_RANGE_STEP);
		if (ret)
			goto out;

		exynos_info[cluster]->cur_volt = volt;
	}

	if (exynos_info[cluster]->abb_table)
		set_match_abb(CLUSTER_ID(cluster), exynos_info[cluster]->abb_table[new_index]);

	if (set_abb_first_than_volt) {
		ret = regulator_set_voltage(regulator, volt, volt + VOLT_RANGE_STEP);
		if (ret)
			goto out;

		exynos_info[cluster]->cur_volt = volt;
	}

out:
	return ret;
}

static int exynos_cpufreq_scale(unsigned int target_freq, unsigned int cpu)
{
	unsigned int cur = get_cur_cluster(cpu);
	struct cpufreq_frequency_table *freq_table = exynos_info[cur]->freq_table;
	unsigned int *volt_table = exynos_info[cur]->volt_table;
	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
	unsigned int new_index, old_index;
	unsigned int volt, safe_volt = 0;
	int ret = 0;
	unsigned int current_freq = freqs[cur]->old;

	if (!policy)
		return -EINVAL;

	freqs[cur]->cpu = cpu;
	freqs[cur]->new = target_freq;

	if (exynos5_frequency_table_target(policy, freq_table,
				freqs[cur]->old, CPUFREQ_RELATION_L, &old_index)) {
		ret = -EINVAL;
		goto put_policy;
	}

	if (exynos5_frequency_table_target(policy, freq_table,
				freqs[cur]->new, CPUFREQ_RELATION_L, &new_index)) {
		ret = -EINVAL;
		goto put_policy;
	}

#if defined(CONFIG_EXYNOS_BIG_FREQ_BOOST)
	/*
	 * check whether new freq is possilbe
	 * if not change new freq to possible max freq
	 */
	if (cur == CL_ONE &&
		new_index < exynos_info[cur]->max_support_idx_table[NR_CLUST1_CPUS])
		exynos_cpufreq_verify_possible_freq(&new_index);
#endif

	if (old_index == new_index)
		goto put_policy;

	/*
	 * ARM clock source will be changed APLL to MPLL temporary
	 * To support this level, need to control regulator for
	 * required voltage level
	 */
	safe_volt = exynos_get_safe_volt(old_index, new_index, cur);
	if (safe_volt)
		safe_volt = get_limit_voltage(safe_volt);

	volt = get_limit_voltage(volt_table[new_index]);

	/* Update policy current frequency */
	cpufreq_freq_transition_begin(policy, freqs[cur]);

	if (old_index > new_index)
		if (exynos_info[cur]->set_int_skew)
			exynos_info[cur]->set_int_skew(new_index);

#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
	if (!volt_offset)
		exynos_cl_dvfs_stop(CLUSTER_ID(cur), new_index);
#endif

	if (old_index > new_index) {
		if (pm_qos_request_active(&exynos_mif_qos[cur]))
			pm_qos_update_request(&exynos_mif_qos[cur],
					exynos_info[cur]->bus_table[new_index]);
	}

	/* When the new frequency is higher than current frequency */
	if ((old_index > new_index) && !safe_volt) {
		/* Firstly, voltage up to increase frequency */
		ret = exynos_set_voltage(old_index, new_index, volt, cur);
		if (ret)
			goto fail_dvfs;

		if (exynos_info[cur]->set_ema)
			exynos_info[cur]->set_ema(volt);
	}

	if (safe_volt) {
		ret = exynos_set_voltage(old_index, new_index, safe_volt, cur);
		if (ret)
			goto fail_dvfs;

		if (exynos_info[cur]->set_ema)
			exynos_info[cur]->set_ema(safe_volt);
	}

	exynos_info[cur]->set_freq(old_index, new_index);

#ifdef CONFIG_SMP
	if (!global_lpj_ref.freq) {
		global_lpj_ref.ref = loops_per_jiffy;
		global_lpj_ref.freq = freqs[cur]->old;
	}

#ifndef CONFIG_ARM64
	loops_per_jiffy = cpufreq_scale(global_lpj_ref.ref,
			global_lpj_ref.freq, freqs[cur]->new);
#endif
#endif

	cpufreq_freq_transition_end(policy, freqs[cur], 0);

	/* When the new frequency is lower than current frequency */
	if ((old_index < new_index) || ((old_index > new_index) && safe_volt)) {
		/* down the voltage after frequency change */
		if (exynos_info[cur]->set_ema)
			 exynos_info[cur]->set_ema(volt);

		ret = exynos_set_voltage(old_index, new_index, volt, cur);
		if (ret) {
			/* save current frequency as frequency is changed*/
			freqs[cur]->old = target_freq;
			goto put_policy;
		}
	}

	if (old_index < new_index) {
		if (pm_qos_request_active(&exynos_mif_qos[cur]))
			pm_qos_update_request(&exynos_mif_qos[cur],
					exynos_info[cur]->bus_table[new_index]);
	}

	if (old_index < new_index)
		if (exynos_info[cur]->set_int_skew)
			exynos_info[cur]->set_int_skew(new_index);

#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
	if (!volt_offset)
		exynos_cl_dvfs_start(CLUSTER_ID(cur));
#endif

	cpufreq_cpu_put(policy);

	return 0;

fail_dvfs:
	cpufreq_freq_transition_end(policy, freqs[cur], ret);

	/* Recover old freq when voltage set failed */
	freqs[cur]->old = current_freq;

put_policy:
	cpufreq_cpu_put(policy);

	return ret;
}

void exynos_set_max_freq(int max_freq, unsigned int cpu)
{
	cluster_type cluster = get_cur_cluster(cpu);

	if (pm_qos_request_active(&ipa_max_qos[cluster]))
		pm_qos_update_request(&ipa_max_qos[cluster], max_freq);
	else
		pm_qos_add_request(&ipa_max_qos[cluster], qos_max_class[cluster], max_freq);
}

struct cpu_power_info {
	unsigned int load[4];
	unsigned int freq;
	cluster_type cluster;
	unsigned int temp;
};

unsigned int get_power_value(struct cpu_power_info *power_info)
{
	/* ps : power_static (mW)
         * pd : power_dynamic (mW)
	 */
	u64 temp;
	u64 pd_tot;
	unsigned int ps, total_power;
	unsigned int volt, maxfreq;
	int i, coeff;
	struct cpufreq_frequency_table *freq_table;
	unsigned int *volt_table;
	unsigned int freq = power_info->freq / 1000;
	/* converting frequency unit as MHz */
	cluster_type cluster = power_info->cluster;

	if (cluster == CL_ONE || cluster == CL_ZERO) {
		coeff = (cluster ? POWER_COEFF_15P : POWER_COEFF_7P);
		freq_table = exynos_info[cluster]->freq_table;
		volt_table = exynos_info[cluster]->volt_table;
		maxfreq = exynos_info[cluster]->freq_table[exynos_info[cluster]->max_support_idx].frequency;
	} else {
		BUG_ON(1);
	}

	if (power_info->freq < freq_min[power_info->cluster]) {
		pr_warn("%s: freq: %d for cluster: %d is less than min. freq: %d\n",
			__func__, power_info->freq, power_info->cluster,
			freq_min[power_info->cluster]);
		power_info->freq = freq_min[power_info->cluster];
	}

	if (power_info->freq > maxfreq) {
		pr_warn("%s: freq: %d for cluster: %d is greater than max. freq: %d\n",
			__func__, power_info->freq, power_info->cluster,
			maxfreq);
		power_info->freq = maxfreq;
	}

	for (i = 0; (freq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
		if (power_info->freq == freq_table[i].frequency)
			break;
	}

	volt = volt_table[i] / 10000;

	/* TODO: pde_p is not linear to load, need to update equation */
	pd_tot = 0;
	temp = ((u64)(coeff * freq * volt * volt)) / 1000000;
	/* adding an extra 0 for division in order to compensate for the frequency unit change */
	for (i = 0; i < 4; i++) {
		if (power_info->load[i] > 100) {
			pr_err("%s: Load should be a percent value\n", __func__);
			BUG_ON(1);
		}
		pd_tot += temp * (power_info->load[i]+1);
	}
	total_power = (unsigned int)pd_tot / (unsigned int)100;

	/* pse = alpha ~ volt ~ temp */
	/* TODO: Update voltage, temperature variant PSE */
	ps = 0;

	total_power += ps;

	return total_power;
}

int get_real_max_freq(cluster_type cluster)
{
	return freq_max[cluster];
}

static void exynos_qos_nop(void *info)
{
}

static int exynos_enable_cpd(int cpu)
{
	release_cpd();

	return 1;
}

static int exynos_disable_cpd(int cpu)
{
	block_cpd();

	if (check_cluster_idle_state(cpu))
		smp_call_function_single(cpu, exynos_qos_nop, NULL, 0);

	return 1;
}

unsigned int g_clamp_cpufreqs[CL_END];

static unsigned int exynos_verify_pm_qos_limit(struct cpufreq_policy *policy,
		unsigned int target_freq, cluster_type cur)
{
#if defined(CONFIG_CPU_FREQ_GOV_USERSPACE) || defined(CONFIG_CPU_FREQ_GOV_PERFORMANCE)
	if ((strcmp(policy->governor->name, "userspace") == 0)
			|| strcmp(policy->governor->name, "performance") == 0)
		return target_freq;
#endif
	target_freq = max((unsigned int)pm_qos_request(qos_min_class[cur]), target_freq);
	target_freq = min((unsigned int)pm_qos_request(qos_max_class[cur]), target_freq);
	target_freq = min(g_clamp_cpufreqs[cur], target_freq); /* add IPA clamp */

	return target_freq;
}

/* Set clock frequency */
static int exynos_target(struct cpufreq_policy *policy,
			  unsigned int target_freq,
			  unsigned int relation)
{
	cluster_type cur = get_cur_cluster(policy->cpu);
	struct cpufreq_frequency_table *freq_table = exynos_info[cur]->freq_table;
	unsigned int index;
	int ret = 0;
#ifdef CONFIG_CPU_THERMAL_IPA_DEBUG
	trace_printk("IPA:%s:%d Called by %x, with target_freq %d", __PRETTY_FUNCTION__, __LINE__,
			(unsigned int) __builtin_return_address (0), target_freq);
#endif

	mutex_lock(&cpufreq_lock);

	if (exynos_info[cur]->blocked)
		goto out;

	if (target_freq == 0)
		target_freq = policy->min;

	if (exynos_info[cur]->check_smpl && exynos_info[cur]->check_smpl())
		goto out;

	/* if PLL bypass, frequency scale is skip */
	if (exynos_getspeed(policy->cpu) <= 24000)
		goto out;

	/* verify old frequency */
	if (freqs[cur]->old != exynos_getspeed(policy->cpu)) {
		printk("oops, sombody change clock  old clk:%d, cur clk:%d \n", freqs[cur]->old, exynos_getspeed(policy->cpu));
		BUG_ON(freqs[cur]->old != exynos_getspeed(policy->cpu));
	}

	/* verify pm_qos_lock */
	target_freq = exynos_verify_pm_qos_limit(policy, target_freq, cur);

#ifdef CONFIG_CPU_THERMAL_IPA_DEBUG
	trace_printk("IPA:%s:%d will apply %d ", __PRETTY_FUNCTION__, __LINE__, target_freq);
#endif

	if (cpufreq_frequency_table_target(policy, freq_table,
				target_freq, relation, &index)) {
		ret = -EINVAL;
		goto out;
	}

	target_freq = freq_table[index].frequency;
	if (target_freq > policy->max)
		target_freq = policy->max;

	/* disable cluster power down during scale */
	if (cur == CL_ONE)
		exynos_disable_cpd(policy->cpu);

	pr_debug("%s[%d]: new_freq[%d], index[%d]\n",
				__func__, cur, target_freq, index);

	exynos_ss_freq(cur, freqs[cur]->old, target_freq, ESS_FLAG_IN);
	/* frequency and volt scaling */
	ret = exynos_cpufreq_scale(target_freq, policy->cpu);
	exynos_ss_freq(cur, freqs[cur]->old, target_freq, ESS_FLAG_OUT);

	/* enable cluster power down  */
	if (cur == CL_ONE)
		exynos_enable_cpd(policy->cpu);

	if (ret < 0)
		goto out;

	/* save current frequency */
	freqs[cur]->old = target_freq;

out:

	mutex_unlock(&cpufreq_lock);

	return ret;
}

void ipa_set_clamp(int cpu, unsigned int clamp_freq, unsigned int gov_target)
{
	unsigned int freq = 0;
	unsigned int new_freq = 0;
	unsigned int prev_clamp_freq = 0;
	unsigned int cur = get_cur_cluster(cpu);
	struct cpufreq_policy *policy;

	prev_clamp_freq = g_clamp_cpufreqs[cur];
	g_clamp_cpufreqs[cur] = clamp_freq;

	if (cur == CL_ONE)
		gov_target = max((unsigned int)pm_qos_request(PM_QOS_CLUSTER1_FREQ_MIN), gov_target);
	else
		gov_target = max((unsigned int)pm_qos_request(PM_QOS_CLUSTER0_FREQ_MIN), gov_target);

	new_freq = min(clamp_freq, gov_target);
	freq = exynos_getspeed(cpu);

	if ((freq <= clamp_freq) && (prev_clamp_freq >= clamp_freq))
		return;

	policy = cpufreq_cpu_get(cpu);

	if (!policy)
		return;

	if (!policy->user_policy.governor) {
		cpufreq_cpu_put(policy);
		return;
	}
#if defined(CONFIG_CPU_FREQ_GOV_USERSPACE) || defined(CONFIG_CPU_FREQ_GOV_PERFORMANCE)
	if ((strcmp(policy->governor->name, "userspace") == 0)
			|| strcmp(policy->governor->name, "performance") == 0) {
		cpufreq_cpu_put(policy);
		return;
	}
#endif
#ifdef CONFIG_CPU_THERMAL_IPA_DEBUG
	trace_printk("IPA: %s:%d: set clamps for cpu %d to %d (curr was %d)",
		     __PRETTY_FUNCTION__, __LINE__, cpu, clamp_freq, freq);
#endif

	__cpufreq_driver_target(policy, new_freq, CPUFREQ_RELATION_H);
	cpufreq_cpu_put(policy);
}

#ifdef CONFIG_PM
static int exynos_cpufreq_suspend(struct cpufreq_policy *policy)
{
	return 0;
}

static int exynos_cpufreq_resume(struct cpufreq_policy *policy)
{
	int cl, cur;

	for (cl = 0; cl < CL_END; cl++)
		freqs[cl]->old = exynos_getspeed_cluster(cl);

	/* Update policy->cur value to resume frequency */
	cur = get_cur_cluster(policy->cpu);
	policy->cur = exynos_getspeed_cluster(cur);

	return 0;
}
#endif

static int __cpuinit exynos_cpufreq_cpu_up_notifier(struct notifier_block *notifier,
					unsigned long action, void *hcpu)
{
	unsigned int cpu = (unsigned long)hcpu;
	struct device *dev;
	struct cpumask mask;
	int cluster;

	dev = get_cpu_device(cpu);
	if (dev) {
		switch (action) {
		case CPU_ONLINE:
			cluster = get_cur_cluster(cpu);
			if (cluster == CL_ONE) {
				cpumask_and(&mask, cpu_coregroup_mask(cpu), cpu_online_mask);
				if (cpumask_weight(&mask) == 1)
					pm_qos_update_request(&boot_max_qos[cluster], freq_max[cluster]);
			}
			break;
		}
	}

	return NOTIFY_OK;
}

static int __cpuinit exynos_cpufreq_cpu_down_notifier(struct notifier_block *notifier,
					unsigned long action, void *hcpu)
{
	unsigned int cpu = (unsigned long)hcpu;
	struct device *dev;
	struct cpumask mask;
	int cluster;

	if (suspend_prepared)
		return NOTIFY_OK;

	dev = get_cpu_device(cpu);
	if (dev) {
		switch (action) {
		case CPU_DOWN_PREPARE:
			cluster = get_cur_cluster(cpu);
			if (cluster == CL_ONE) {
				cpumask_and(&mask, cpu_coregroup_mask(cpu), cpu_online_mask);
				if (cpumask_weight(&mask) == 1)
					pm_qos_update_request(&boot_max_qos[cluster], freq_min[cluster]);
			}
			break;
		case CPU_DOWN_PREPARE_FROZEN:
		case CPU_DOWN_LATE_PREPARE:
			mutex_lock(&cpufreq_lock);
			exynos_info[CL_ZERO]->blocked = true;
			exynos_info[CL_ONE]->blocked = true;
			mutex_unlock(&cpufreq_lock);
			break;
		case CPU_DOWN_FAILED:
		case CPU_DOWN_FAILED_FROZEN:
		case CPU_DEAD:
			mutex_lock(&cpufreq_lock);
			exynos_info[CL_ZERO]->blocked = false;
			exynos_info[CL_ONE]->blocked = false;
			mutex_unlock(&cpufreq_lock);
			break;
		}
	}
	return NOTIFY_OK;
}

static struct notifier_block __refdata exynos_cpufreq_cpu_up_nb = {
	.notifier_call = exynos_cpufreq_cpu_up_notifier,
	.priority = INT_MIN,
};

/*
 * This notifier should be perform before
 * cpufreq_cpu_notifier performs.
 */
static struct notifier_block __refdata exynos_cpufreq_cpu_down_nb = {
	.notifier_call = exynos_cpufreq_cpu_down_notifier,
	.priority = INT_MIN + 2,
};

/*
 * exynos_cpufreq_pm_notifier - block CPUFREQ's activities in suspend-resume
 *			context
 * @notifier
 * @pm_event
 * @v
 *
 * While cpufreq_disable == true, target() ignores every frequency but
 * boot_freq. The boot_freq value is the initial frequency,
 * which is set by the bootloader. In order to eliminate possible
 * inconsistency in clock values, we save and restore frequencies during
 * suspend and resume and block CPUFREQ activities. Note that the standard
 * suspend/resume cannot be used as they are too deep (syscore_ops) for
 * regulator actions.
 */
static int exynos_cpufreq_pm_notifier(struct notifier_block *notifier,
				       unsigned long pm_event, void *v)
{
	struct cpufreq_frequency_table *freq_table = NULL;
	unsigned int bootfreq;
	unsigned int abb_freq = 0;
	bool set_abb_first_than_volt = false;
	int volt, i, cl;
	int cl_index;

	switch (pm_event) {
	case PM_SUSPEND_PREPARE:
		/* Fix min/max freq to bootfreq for boot cluster */
		pm_qos_update_request(&boot_min_qos[CL_ZERO],
					exynos_info[CL_ZERO]->boot_freq);
		pm_qos_update_request(&boot_max_qos[CL_ZERO],
					exynos_info[CL_ZERO]->boot_freq);

		pm_qos_update_request(&boot_max_qos[CL_ONE], freq_min[CL_ONE]);

		mutex_lock(&cpufreq_lock);
		exynos_info[CL_ZERO]->blocked = true;
		exynos_info[CL_ONE]->blocked = true;

		for (cl = 0; cl < CL_END; cl++) {
			bootfreq = get_resume_freq(cl);

			volt = max(get_boot_volt(cl), get_freq_volt(cl, freqs[cl]->old, &cl_index));
			if ( volt <= 0) {
				printk("oops, strange voltage %s -> boot volt:%d, get_freq_volt:%d, freq:%d \n",
					(cl ? "CL_ONE" : "CL_ZERO"), get_boot_volt(cl),
					get_freq_volt(cl, freqs[cl]->old, &cl_index), freqs[cl]->old);
				BUG_ON(volt <= 0);
			}
			volt = get_limit_voltage(volt);

			if (exynos_info[cl]->abb_table)
				set_abb_first_than_volt = is_set_abb_first(CLUSTER_ID(cl), freqs[cl]->old, bootfreq);
#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
			exynos_cl_dvfs_stop(CLUSTER_ID(cl), cl_index);
#endif
			if (!set_abb_first_than_volt)
				if (regulator_set_voltage(exynos_info[cl]->regulator, volt, volt + VOLT_RANGE_STEP))
					goto err;

			exynos_info[cl]->cur_volt = volt;

			if (exynos_info[cl]->abb_table) {
				abb_freq = max(bootfreq, freqs[cl]->old);
				freq_table = exynos_info[cl]->freq_table;
				for (i = 0; (freq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
					if (freq_table[i].frequency == CPUFREQ_ENTRY_INVALID)
						continue;
					if (freq_table[i].frequency == abb_freq) {
						set_match_abb(CLUSTER_ID(cl), exynos_info[cl]->abb_table[i]);
						break;
					}
				}
			}

			if (set_abb_first_than_volt)
				if (regulator_set_voltage(exynos_info[cl]->regulator, volt, volt + VOLT_RANGE_STEP))
					goto err;

			exynos_info[cl]->cur_volt = volt;

			if (exynos_info[cl]->set_ema)
				exynos_info[cl]->set_ema(volt);
#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
			exynos_cl_dvfs_start(CLUSTER_ID(cl));
#endif
			set_abb_first_than_volt = false;
		}

		suspend_prepared = true;
		mutex_unlock(&cpufreq_lock);

		pr_debug("PM_SUSPEND_PREPARE for CPUFREQ\n");

		break;
	case PM_POST_SUSPEND:
		pr_debug("PM_POST_SUSPEND for CPUFREQ\n");
		for (cl = 0; cl < CL_END; cl++) {
			set_resume_freq(cl);

			mutex_lock(&cpufreq_lock);
			exynos_info[cl]->blocked = false;
			mutex_unlock(&cpufreq_lock);
		}

		/* Recovery min/max frequency normally for boot cluster */
		pm_qos_update_request(&boot_min_qos[CL_ZERO], freq_min[CL_ZERO]);
		pm_qos_update_request(&boot_max_qos[CL_ZERO], freq_max[CL_ZERO]);

		pm_qos_update_request(&boot_max_qos[CL_ONE], INT_MAX);

		suspend_prepared = false;

		break;
	}
	return NOTIFY_OK;
err:
	pr_err("%s: failed to set voltage\n", __func__);

	return NOTIFY_BAD;
}

static struct notifier_block exynos_cpufreq_nb = {
	.notifier_call = exynos_cpufreq_pm_notifier,
};

#ifdef CONFIG_EXYNOS_THERMAL
static int exynos_cpufreq_tmu_notifier(struct notifier_block *notifier,
				       unsigned long event, void *v)
{
	int volt, cl;
	int *on = v;
	int ret = NOTIFY_OK;
#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
	int cl_index = 0;
#endif
	int cold_offset = 0;

	if (event != TMU_COLD)
		return NOTIFY_OK;

	mutex_lock(&cpufreq_lock);
	if (*on) {
		if (volt_offset)
			goto out;
		else
			volt_offset = COLD_VOLT_OFFSET;

		for (cl = 0; cl < CL_END; cl++) {
#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
			get_freq_volt(cl, freqs[cl]->old, &cl_index);
#endif
			volt = exynos_info[cl]->cur_volt;
#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
			exynos_cl_dvfs_stop(CLUSTER_ID(cl), cl_index);
#endif
			volt = get_limit_voltage(volt);
			ret = regulator_set_voltage(exynos_info[cl]->regulator, volt, volt + VOLT_RANGE_STEP);
			if (ret) {
				ret = NOTIFY_BAD;
				goto out;
			}

			exynos_info[cl]->cur_volt = volt;

			if (exynos_info[cl]->set_ema)
				exynos_info[cl]->set_ema(volt);
		}
	} else {
		if (!volt_offset)
			goto out;
		else {
			cold_offset = volt_offset;
			volt_offset = 0;
		}
		for (cl = 0; cl < CL_END; cl++) {
			volt = get_limit_voltage(exynos_info[cl]->cur_volt - cold_offset);
#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
			exynos_cl_dvfs_stop(CLUSTER_ID(cl), cl_index);
#endif
			if (exynos_info[cl]->set_ema)
				exynos_info[cl]->set_ema(volt);

			ret = regulator_set_voltage(exynos_info[cl]->regulator, volt, volt + VOLT_RANGE_STEP);
			if (ret) {
				ret = NOTIFY_BAD;
				goto out;
			}

			exynos_info[cl]->cur_volt = volt;

#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
			exynos_cl_dvfs_start(CLUSTER_ID(cl));
#endif
		}
	}

out:
	mutex_unlock(&cpufreq_lock);

	return ret;
}

static struct notifier_block exynos_tmu_nb = {
	.notifier_call = exynos_cpufreq_tmu_notifier,
};
#endif

static int exynos_cpufreq_cpu_init(struct cpufreq_policy *policy)
{
	unsigned int cur = get_cur_cluster(policy->cpu);

	pr_debug("%s: cpu[%d]\n", __func__, policy->cpu);

	policy->cur = policy->min = policy->max = exynos_getspeed(policy->cpu);

	cpufreq_table_validate_and_show(policy, exynos_info[cur]->freq_table);

	/* set the transition latency value */
	policy->cpuinfo.transition_latency = 100000;

	if (cpumask_test_cpu(policy->cpu, &cluster_cpus[CL_ONE])) {
		cpumask_copy(policy->cpus, &cluster_cpus[CL_ONE]);
		cpumask_copy(policy->related_cpus, &cluster_cpus[CL_ONE]);
	} else {
		cpumask_copy(policy->cpus, &cluster_cpus[CL_ZERO]);
		cpumask_copy(policy->related_cpus, &cluster_cpus[CL_ZERO]);
	}

	return cpufreq_frequency_table_cpuinfo(policy, exynos_info[cur]->freq_table);
}

static struct cpufreq_driver exynos_driver = {
	.flags		= CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY,
	.verify		= exynos_verify_speed,
	.target		= exynos_target,
	.get		= exynos_getspeed,
	.init		= exynos_cpufreq_cpu_init,
	.name		= "exynos_cpufreq",
#ifdef CONFIG_PM
	.suspend	= exynos_cpufreq_suspend,
	.resume		= exynos_cpufreq_resume,
#endif
};

/************************** sysfs interface ************************/
#ifdef CONFIG_PM
static ssize_t show_cpufreq_smpl_warn_ctrl(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	ssize_t	nsize = 0;
	int cluster;

	for (cluster = 0; cluster < CL_END; cluster++)
		nsize += snprintf(&buf[nsize], 20, "cl[%d]:%d ", cluster, exynos_info[cluster]->en_smpl);

	nsize += snprintf(&buf[nsize], 2, "\n");

	return nsize;
}

static ssize_t store_cpufreq_smpl_warn_ctrl(struct kobject *kobj, struct attribute *attr,
					const char *buf, size_t count)
{
	int input, cluster;

	if (!sscanf(buf, "%1d %1d", &cluster, &input))
		return -EINVAL;

	if (!cluster) {
		pr_err("Do not support smpl_warn for little cluster.\n");
		return -EBUSY;
	}

	if (exynos_info[cluster]->check_smpl()) {
		pr_err("Already happened smpl_warn.\n");
		return -EBUSY;
	}

	input ? exynos_info[cluster]->init_smpl() : exynos_info[cluster]->deinit_smpl();
	exynos_info[cluster]->en_smpl = !!input;

	return count;
}

static ssize_t show_cpufreq_table(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	int i;
	ssize_t count = 0;
	size_t tbl_sz = 0, pr_len;
	struct cpufreq_frequency_table *freq_table_cluster1 = exynos_info[CL_ONE]->freq_table;
	struct cpufreq_frequency_table *freq_table_cluster0 = exynos_info[CL_ZERO]->freq_table;

	for (i = 0; freq_table_cluster1[i].frequency != CPUFREQ_TABLE_END; i++)
		tbl_sz++;
	for (i = 0; freq_table_cluster0[i].frequency != CPUFREQ_TABLE_END; i++)
		tbl_sz++;

	if (tbl_sz == 0)
		return -EINVAL;

	pr_len = (size_t)((PAGE_SIZE - 2) / tbl_sz);

	for (i = 0; freq_table_cluster1[i].frequency != CPUFREQ_TABLE_END; i++) {
		if (freq_table_cluster1[i].frequency != CPUFREQ_ENTRY_INVALID)
			count += snprintf(&buf[count], pr_len, "%d ",
						freq_table_cluster1[i].frequency);
	}

	for (i = 0; freq_table_cluster0[i].frequency != CPUFREQ_TABLE_END; i++) {
		if (freq_table_cluster0[i].frequency != CPUFREQ_ENTRY_INVALID)
			count += snprintf(&buf[count], pr_len, "%d ",
					freq_table_cluster0[i].frequency / LIMIT_FREQ_DIVIDER);
	}

	count += snprintf(&buf[count - 1], 2, "\n");

	return count - 1;
}

static ssize_t show_cpufreq_min_limit(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	ssize_t	nsize = 0;
	unsigned int cluster1_qos_min = pm_qos_request(qos_min_class[CL_ONE]);
	unsigned int cluster0_qos_min = pm_qos_request(qos_min_class[CL_ZERO]);

#ifdef CONFIG_SCHED_HMP
	if (cluster1_qos_min > 0 && get_hmp_boost())
#else
	if (cluster1_qos_min > 0)
#endif
	{
		if (cluster1_qos_min > freq_max[CL_ONE])
			cluster1_qos_min = freq_max[CL_ONE];
		else if (cluster1_qos_min < freq_min[CL_ONE])
			cluster1_qos_min = freq_min[CL_ONE];
		nsize = snprintf(buf, 10, "%u\n", cluster1_qos_min);
	} else if (cluster0_qos_min > 0) {
		if (cluster0_qos_min > freq_max[CL_ZERO])
			cluster0_qos_min = freq_max[CL_ZERO];
		if (cluster0_qos_min < freq_min[CL_ZERO])
			cluster0_qos_min = freq_min[CL_ZERO];
		nsize = snprintf(buf, 10, "%u\n", cluster0_qos_min / LIMIT_FREQ_DIVIDER);
	} else if (cluster0_qos_min == 0) {
		cluster0_qos_min = freq_min[CL_ZERO];
		nsize = snprintf(buf, 10, "%u\n", cluster0_qos_min / LIMIT_FREQ_DIVIDER);
	}

	return nsize;
}

static ssize_t store_cpufreq_min_limit(struct kobject *kobj, struct attribute *attr,
					const char *buf, size_t count)
{
	int cluster1_input, cluster0_input;

	if (!sscanf(buf, "%8d", &cluster1_input))
		return -EINVAL;

	if (cluster1_input >= (int)freq_min[CL_ONE]) {
#ifdef CONFIG_SCHED_HMP
		if (!hmp_boosted) {
			if (set_hmp_boost(1) < 0)
				pr_err("%s: failed HMP boost enable\n",
							__func__);
			else
				hmp_boosted = true;
		}
#endif
		cluster1_input = min(cluster1_input, (int)freq_max[CL_ONE]);
		if (exynos_info[CL_ZERO]->boost_freq)
			cluster0_input = exynos_info[CL_ZERO]->boost_freq;
		else
			cluster0_input = core_max_qos_const[CL_ZERO].default_value;
	} else if (cluster1_input < (int)freq_min[CL_ONE]) {
#ifdef CONFIG_SCHED_HMP
		if (hmp_boosted) {
			if (set_hmp_boost(0) < 0)
				pr_err("%s: failed HMP boost disable\n",
							__func__);
			else
				hmp_boosted = false;
		}
#endif
		if (cluster1_input < 0) {
			cluster1_input = qos_min_default_value[CL_ONE];
			cluster0_input = qos_min_default_value[CL_ZERO];
		} else {
			cluster0_input = cluster1_input * LIMIT_FREQ_DIVIDER;
			if (cluster0_input > 0)
				cluster0_input = min(cluster0_input, (int)freq_max[CL_ZERO]);
			cluster1_input = qos_min_default_value[CL_ONE];
		}
	}

	if (pm_qos_request_active(&core_min_qos[CL_ONE]))
		pm_qos_update_request(&core_min_qos[CL_ONE], cluster1_input);
	if (pm_qos_request_active(&core_min_qos[CL_ZERO]))
		pm_qos_update_request(&core_min_qos[CL_ZERO], cluster0_input);

	return count;
}

static ssize_t show_cpufreq_max_limit(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	ssize_t	nsize = 0;
	unsigned int cluster1_qos_max = pm_qos_request(qos_max_class[CL_ONE]);
	unsigned int cluster0_qos_max = pm_qos_request(qos_max_class[CL_ZERO]);

	if (cluster1_qos_max > 0) {
		if (cluster1_qos_max < freq_min[CL_ONE])
			cluster1_qos_max = freq_min[CL_ONE];
		else if (cluster1_qos_max > freq_max[CL_ONE])
			cluster1_qos_max = freq_max[CL_ONE];
		nsize = snprintf(buf, 10, "%u\n", cluster1_qos_max);
	} else if (cluster0_qos_max > 0) {
		if (cluster0_qos_max < freq_min[CL_ZERO])
			cluster0_qos_max = freq_min[CL_ZERO];
		if (cluster0_qos_max > freq_max[CL_ZERO])
			cluster0_qos_max = freq_max[CL_ZERO];
		nsize = snprintf(buf, 10, "%u\n", cluster0_qos_max / LIMIT_FREQ_DIVIDER);
	} else if (cluster0_qos_max == 0) {
		cluster0_qos_max = freq_min[CL_ZERO];
		nsize = snprintf(buf, 10, "%u\n", cluster0_qos_max / LIMIT_FREQ_DIVIDER);
	}

	return nsize;
}

static void enable_nonboot_cluster_cpus(void)
{
	pm_qos_update_request(&cpufreq_cpu_hotplug_max_request, NR_CPUS);
}

static void disable_nonboot_cluster_cpus(void)
{
	pm_qos_update_request(&cpufreq_cpu_hotplug_max_request, NR_CLUST1_CPUS);
}

static ssize_t store_cpufreq_max_limit(struct kobject *kobj, struct attribute *attr,
					const char *buf, size_t count)
{
	int cluster1_input, cluster0_input;

	if (!sscanf(buf, "%8d", &cluster1_input))
		return -EINVAL;

	if (cluster1_input >= (int)freq_min[CL_ONE]) {
		if (cluster1_hotplugged) {
			enable_nonboot_cluster_cpus();
			cluster1_hotplugged = false;
		}

		cluster1_input = max(cluster1_input, (int)freq_min[CL_ONE]);
		cluster0_input = core_max_qos_const[CL_ZERO].default_value;
	} else if (cluster1_input < (int)freq_min[CL_ONE]) {
		if (cluster1_input < 0) {
			if (cluster1_hotplugged) {
				enable_nonboot_cluster_cpus();
				cluster1_hotplugged = false;
			}

			cluster1_input = core_max_qos_const[CL_ONE].default_value;
			cluster0_input = core_max_qos_const[CL_ZERO].default_value;
		} else {
			cluster0_input = cluster1_input * LIMIT_FREQ_DIVIDER;
			if (cluster0_input > 0)
				cluster0_input = max(cluster0_input, (int)freq_min[CL_ZERO]);
			cluster1_input = qos_min_default_value[CL_ONE];

			if (!cluster1_hotplugged) {
				disable_nonboot_cluster_cpus();
				cluster1_hotplugged = true;
			}
		}
	}

	if (pm_qos_request_active(&core_max_qos[CL_ONE]))
		pm_qos_update_request(&core_max_qos[CL_ONE], cluster1_input);
	if (pm_qos_request_active(&core_max_qos[CL_ZERO]))
		pm_qos_update_request(&core_max_qos[CL_ZERO], cluster0_input);

	return count;
}
#endif

#ifdef CONFIG_SW_SELF_DISCHARGING
static ssize_t show_cpufreq_self_discharging(struct kobject *kobj,
			     struct attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", self_discharging);
}

static ssize_t store_cpufreq_self_discharging(struct kobject *kobj, struct attribute *attr,
			      const char *buf, size_t count)
{
	int input;

	if (!sscanf(buf, "%d", &input))
		return -EINVAL;

	if (input > 0) {
		self_discharging = input;
		cpu_idle_poll_ctrl(true);
	}
	else {
		self_discharging = 0;
		cpu_idle_poll_ctrl(false);
	}

	return count;
}
#endif

inline ssize_t show_core_freq_table(char *buf, cluster_type cluster)
{
	int i, count = 0;
	size_t tbl_sz = 0, pr_len;
	struct cpufreq_frequency_table *freq_table = exynos_info[cluster]->freq_table;

	for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++)
		tbl_sz++;

	if (tbl_sz == 0)
		return -EINVAL;

	pr_len = (size_t)((PAGE_SIZE - 2) / tbl_sz);

	for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
		if (freq_table[i].frequency != CPUFREQ_ENTRY_INVALID)
			count += snprintf(&buf[count], pr_len, "%d ",
					freq_table[i].frequency);
	}

	count += snprintf(&buf[count], 2, "\n");
	return count;
}

inline ssize_t show_core_freq(char *buf,
				cluster_type cluster,
				bool qos_flag) /* qos_flag : false is Freq_min, true is Freq_max */
{
	int qos_class = (qos_flag ? qos_max_class[cluster] : qos_min_class[cluster]);
	unsigned int qos_value = pm_qos_request(qos_class);
	unsigned int *freq_info = (qos_flag ? freq_max : freq_min);

	if (qos_value == 0)
		qos_value = freq_info[cluster];

	return snprintf(buf, PAGE_SIZE, "%u\n", qos_value);
}

inline ssize_t store_core_freq(const char *buf, size_t count,
				cluster_type cluster,
				bool qos_flag) /* qos_flag : false is Freq_min, true is Freq_max */
{
	struct pm_qos_request *core_qos_real = (qos_flag ? core_max_qos_real : core_min_qos_real);
	/* Need qos_flag's opposite freq_info for comparison */
	unsigned int *freq_info = (qos_flag ? freq_min : freq_max);
	int input;

	if (!sscanf(buf, "%8d", &input))
		return -EINVAL;

	if (input > 0) {
		if (qos_flag)
			input = max(input, (int)freq_info[cluster]);
		else
			input = min(input, (int)freq_info[cluster]);
	}

	if (pm_qos_request_active(&core_qos_real[cluster]))
		pm_qos_update_request(&core_qos_real[cluster], input);

	return count;
}

static ssize_t show_cluster1_freq_table(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	return show_core_freq_table(buf, CL_ONE);
}

static ssize_t show_cluster1_min_freq(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	return show_core_freq(buf, CL_ONE, false);
}

static ssize_t show_cluster1_max_freq(struct kobject *kobj,
				struct attribute *attr, char *buf)
{
	return show_core_freq(buf, CL_ONE, true);
}

static ssize_t store_cluster1_min_freq(struct kobject *kobj, struct attribute *attr,
					const char *buf, size_t count)
{
	return store_core_freq(buf, count, CL_ONE, false);
}

static ssize_t store_cluster1_max_freq(struct kobject *kobj, struct attribute *attr,
					const char *buf, size_t count)
{
	return store_core_freq(buf, count, CL_ONE, true);
}

static ssize_t show_cluster0_freq_table(struct kobject *kobj,
			     struct attribute *attr, char *buf)
{
	return show_core_freq_table(buf, CL_ZERO);
}

static ssize_t show_cluster0_min_freq(struct kobject *kobj,
			     struct attribute *attr, char *buf)
{
	return show_core_freq(buf, CL_ZERO, false);
}

static ssize_t show_cluster0_max_freq(struct kobject *kobj,
			     struct attribute *attr, char *buf)
{
	return show_core_freq(buf, CL_ZERO, true);
}

static ssize_t store_cluster0_min_freq(struct kobject *kobj, struct attribute *attr,
					const char *buf, size_t count)
{
	return store_core_freq(buf, count, CL_ZERO, false);
}

static ssize_t store_cluster0_max_freq(struct kobject *kobj, struct attribute *attr,
					const char *buf, size_t count)
{
	return store_core_freq(buf, count, CL_ZERO, true);
}

define_one_global_ro(cluster1_freq_table);
define_one_global_rw(cluster1_min_freq);
define_one_global_rw(cluster1_max_freq);
define_one_global_ro(cluster0_freq_table);
define_one_global_rw(cluster0_min_freq);
define_one_global_rw(cluster0_max_freq);

static struct attribute *mp_attributes[] = {
	&cluster1_freq_table.attr,
	&cluster1_min_freq.attr,
	&cluster1_max_freq.attr,
	&cluster0_freq_table.attr,
	&cluster0_min_freq.attr,
	&cluster0_max_freq.attr,
	NULL
};

static struct attribute_group mp_attr_group = {
	.attrs = mp_attributes,
	.name = "mp-cpufreq",
};

#ifdef CONFIG_PM
static struct global_attr smpl_warn_ctrl =
		__ATTR(smpl_warn_ctrl, S_IRUGO | S_IWUSR,
			show_cpufreq_smpl_warn_ctrl, store_cpufreq_smpl_warn_ctrl);
static struct global_attr cpufreq_table =
		__ATTR(cpufreq_table, S_IRUGO, show_cpufreq_table, NULL);
static struct global_attr cpufreq_min_limit =
		__ATTR(cpufreq_min_limit, S_IRUGO | S_IWUSR,
			show_cpufreq_min_limit, store_cpufreq_min_limit);
static struct global_attr cpufreq_max_limit =
		__ATTR(cpufreq_max_limit, S_IRUGO | S_IWUSR,
			show_cpufreq_max_limit, store_cpufreq_max_limit);
#endif

#ifdef CONFIG_SW_SELF_DISCHARGING
static struct global_attr cpufreq_self_discharging =
		__ATTR(cpufreq_self_discharging, S_IRUGO | S_IWUSR,
			show_cpufreq_self_discharging, store_cpufreq_self_discharging);
#endif

/************************** sysfs end ************************/

static int exynos_cpufreq_reboot_notifier_call(struct notifier_block *this,
				   unsigned long code, void *_cmd)
{
	struct cpufreq_frequency_table *freq_table;
	unsigned int bootfreq;
	unsigned int abb_freq = 0;
	bool set_abb_first_than_volt = false;
	int volt, i, cl, cl_index;

	for (cl = 0; cl < CL_END; cl++) {
		if (exynos_info[cl]->reboot_limit_freq)
			pm_qos_add_request(&reboot_max_qos[cl], qos_max_class[cl],
						exynos_info[cl]->reboot_limit_freq);

		mutex_lock(&cpufreq_lock);
		exynos_info[cl]->blocked = true;
		mutex_unlock(&cpufreq_lock);

		bootfreq = get_boot_freq(cl);

		volt = max(get_boot_volt(cl),
				get_freq_volt(cl, freqs[cl]->old, &cl_index));
		volt = get_limit_voltage(volt);

		if (exynos_info[cl]->abb_table)
			set_abb_first_than_volt = is_set_abb_first(CLUSTER_ID(cl), freqs[cl]->old, max(bootfreq, freqs[cl]->old));

#ifdef CONFIG_EXYNOS_CL_DVFS_CPU
		exynos_cl_dvfs_stop(CLUSTER_ID(cl), cl_index);
#endif
		if (!set_abb_first_than_volt)
			if (regulator_set_voltage(exynos_info[cl]->regulator, volt, volt + VOLT_RANGE_STEP))
				goto err;

		exynos_info[cl]->cur_volt = volt;

		if (exynos_info[cl]->abb_table) {
			abb_freq = max(bootfreq, freqs[cl]->old);
			freq_table = exynos_info[cl]->freq_table;
			for (i = 0; (freq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
				if (freq_table[i].frequency == CPUFREQ_ENTRY_INVALID)
					continue;
				if (freq_table[i].frequency == abb_freq) {
					set_match_abb(CLUSTER_ID(cl), exynos_info[cl]->abb_table[i]);
					break;
				}
			}
		}

		if (set_abb_first_than_volt)
			if (regulator_set_voltage(exynos_info[cl]->regulator, volt, volt + VOLT_RANGE_STEP))
				goto err;

		exynos_info[cl]->cur_volt = volt;

		if (exynos_info[cl]->set_ema)
			exynos_info[cl]->set_ema(volt);

		set_abb_first_than_volt = false;
	}

	return NOTIFY_DONE;
err:
	pr_err("%s: failed to set voltage\n", __func__);

	return NOTIFY_BAD;
}

static struct notifier_block exynos_cpufreq_reboot_notifier = {
	.notifier_call = exynos_cpufreq_reboot_notifier_call,
};

static int exynos_cluster1_min_qos_handler(struct notifier_block *b, unsigned long val, void *v)
{
	int ret;
	unsigned long freq;
	struct cpufreq_policy *policy;
	int cpu = boot_cluster ? 0 : NR_CLUST0_CPUS;

	freq = exynos_getspeed(cpu);
	if (freq >= val)
		goto good;

	policy = cpufreq_cpu_get(cpu);

	if (!policy)
		goto bad;

	if (!policy->user_policy.governor) {
		cpufreq_cpu_put(policy);
		goto bad;
	}

#if defined(CONFIG_CPU_FREQ_GOV_USERSPACE) || defined(CONFIG_CPU_FREQ_GOV_PERFORMANCE)
	if ((strcmp(policy->user_policy.governor->name, "userspace") == 0)
			|| strcmp(policy->user_policy.governor->name, "performance") == 0) {
		cpufreq_cpu_put(policy);
		goto good;
	}
#endif

	ret = __cpufreq_driver_target(policy, val, CPUFREQ_RELATION_H);
	cpufreq_cpu_put(policy);

	if (ret < 0)
		goto bad;

good:
	return NOTIFY_OK;
bad:
	return NOTIFY_BAD;
}

static struct notifier_block exynos_cluster1_min_qos_notifier = {
	.notifier_call = exynos_cluster1_min_qos_handler,
	.priority = INT_MAX,
};

static int exynos_cluster1_max_qos_handler(struct notifier_block *b, unsigned long val, void *v)
{
	int ret;
	unsigned long freq;
	struct cpufreq_policy *policy;
	int cpu = boot_cluster ? 0 : NR_CLUST0_CPUS;

	freq = exynos_getspeed(cpu);
	if (freq <= val)
		goto good;

	policy = cpufreq_cpu_get(cpu);

	if (!policy)
		goto bad;

	if (!policy->user_policy.governor) {
		cpufreq_cpu_put(policy);
		goto bad;
	}

#if defined(CONFIG_CPU_FREQ_GOV_USERSPACE) || defined(CONFIG_CPU_FREQ_GOV_PERFORMANCE)
	if ((strcmp(policy->user_policy.governor->name, "userspace") == 0)
			|| strcmp(policy->user_policy.governor->name, "performance") == 0) {
		cpufreq_cpu_put(policy);
		goto good;
	}
#endif
	ret = __cpufreq_driver_target(policy, val, CPUFREQ_RELATION_H);
	cpufreq_cpu_put(policy);

	if (ret < 0)
		goto bad;

good:
	return NOTIFY_OK;
bad:
	return NOTIFY_BAD;
}

static struct notifier_block exynos_cluster1_max_qos_notifier = {
	.notifier_call = exynos_cluster1_max_qos_handler,
	.priority = INT_MAX,
};

static int exynos_cluster0_min_qos_handler(struct notifier_block *b, unsigned long val, void *v)
{
	int ret;
	unsigned long freq;
	struct cpufreq_policy *policy;
	int cpu = boot_cluster ? NR_CLUST1_CPUS : 0;
	unsigned int threshold_freq;

#if defined(CONFIG_CPU_FREQ_GOV_INTERACTIVE)
	threshold_freq = cpufreq_interactive_get_hispeed_freq(0);
	if (!threshold_freq)
		threshold_freq = 1000000;	/* 1.0GHz */
#else
	threshold_freq = 1000000;	/* 1.0GHz */
#endif

	freq = exynos_getspeed(cpu);
	if (freq >= val)
		goto good;

	policy = cpufreq_cpu_get(cpu);

	if (!policy)
		goto bad;

	if (!policy->user_policy.governor) {
		cpufreq_cpu_put(policy);
		goto bad;
	}

#if defined(CONFIG_CPU_FREQ_GOV_USERSPACE) || defined(CONFIG_CPU_FREQ_GOV_PERFORMANCE)
	if ((strcmp(policy->user_policy.governor->name, "userspace") == 0)
			|| strcmp(policy->user_policy.governor->name, "performance") == 0) {
		cpufreq_cpu_put(policy);
		goto good;
	}
#endif

	ret = __cpufreq_driver_target(policy, val, CPUFREQ_RELATION_H);
	cpufreq_cpu_put(policy);

	if (ret < 0)
		goto bad;

good:
	return NOTIFY_OK;
bad:
	return NOTIFY_BAD;
}

static struct notifier_block exynos_cluster0_min_qos_notifier = {
	.notifier_call = exynos_cluster0_min_qos_handler,
	.priority = INT_MAX,
};

static int exynos_cluster0_max_qos_handler(struct notifier_block *b, unsigned long val, void *v)
{
	int ret;
	unsigned long freq;
	struct cpufreq_policy *policy;
	int cpu = boot_cluster ? NR_CLUST1_CPUS : 0;

	freq = exynos_getspeed(cpu);
	if (freq <= val)
		goto good;

	policy = cpufreq_cpu_get(cpu);

	if (!policy)
		goto bad;

	if (!policy->user_policy.governor) {
		cpufreq_cpu_put(policy);
		goto bad;
	}

#if defined(CONFIG_CPU_FREQ_GOV_USERSPACE) || defined(CONFIG_CPU_FREQ_GOV_PERFORMANCE)
	if ((strcmp(policy->user_policy.governor->name, "userspace") == 0)
			|| strcmp(policy->user_policy.governor->name, "performance") == 0) {
		cpufreq_cpu_put(policy);
		goto good;
	}
#endif

	ret = __cpufreq_driver_target(policy, val, CPUFREQ_RELATION_H);
	cpufreq_cpu_put(policy);

	if (ret < 0)
		goto bad;

good:
	return NOTIFY_OK;
bad:
	return NOTIFY_BAD;
}

static struct notifier_block exynos_cluster0_max_qos_notifier = {
	.notifier_call = exynos_cluster0_max_qos_handler,
	.priority = INT_MAX,
};

#if defined(CONFIG_EXYNOS_BIG_FREQ_BOOST)
extern struct cpumask hmp_fast_cpu_mask;
static int big_cpu_cnt;

static int cpufreq_policy_notifier(struct notifier_block *nb,
				    unsigned long event, void *data)
{
	struct cpufreq_policy *policy = data;
	unsigned long max_freq;

	if (event != CPUFREQ_ADJUST || !policy->cpu)
		return 0;

	max_freq = exynos_cpufreq_get_possible_max_freq(big_cpu_cnt);

	if (policy->max != max_freq)
		cpufreq_verify_within_limits(policy, 0, max_freq);

	return 0;
}

/* Notifier for cpufreq policy change */
static struct notifier_block exynos_cpufreq_policy_nb = {
	.notifier_call = cpufreq_policy_notifier,
};

static int exynos_cpufreq_cpus_notifier(struct notifier_block *nb,
				    unsigned long event, void *data)
{
	struct cpumask mask;
	struct cpumask policy_update_mask;
	unsigned long timeout;

	switch (event) {
	case CPUS_DOWN_COMPLETE:
	case CPUS_UP_PREPARE:
		cpumask_copy(&mask, data);
		cpumask_and(&mask, &mask, &hmp_fast_cpu_mask);
		big_cpu_cnt = cpumask_weight(&mask);

		/*
		 * if policy_update_mask is 0, it means first hotplug in or last hotplug out
		 * of fast_cpu domain
		 */
		cpumask_clear(&policy_update_mask);
		cpumask_and(&policy_update_mask, &hmp_fast_cpu_mask, cpu_online_mask);
		if (!cpumask_weight(&policy_update_mask))
			return NOTIFY_OK;

		/* if cpufreq_update_policy is failed after 50ms, cancel cpu_up */
		timeout = jiffies + msecs_to_jiffies(50);
		while(cpufreq_update_policy(cpumask_first(&policy_update_mask))
							&& event == CPUS_UP_PREPARE) {
			if (time_after(jiffies, timeout))
				return NOTIFY_BAD;
		}
	}

	if (event == CPUS_UP_PREPARE) {
		mutex_lock(&cpufreq_lock);

		if (unlikely(freqs[CL_ONE]->old > exynos_cpufreq_get_possible_max_freq(big_cpu_cnt))) {
			pr_err("frequency(%d) is higher than possible max frequency of big cpu cnt: %d\n",
			freqs[CL_ONE]->old, big_cpu_cnt);
			mutex_unlock(&cpufreq_lock);

			return NOTIFY_BAD;
		}
		mutex_unlock(&cpufreq_lock);
	}

	return NOTIFY_OK;
}

static struct notifier_block exynos_cpufreq_cpus_nb = {
	.notifier_call = exynos_cpufreq_cpus_notifier,
	.priority = INT_MIN,
};
#endif

static int exynos_cpufreq_init(void)
{
	int i, ret = -EINVAL;
	cluster_type cluster;
	struct cpufreq_frequency_table *freq_table;
	struct cpufreq_policy *policy;

	boot_cluster = 0;

	g_clamp_cpufreqs[CL_ONE] = UINT_MAX;
	g_clamp_cpufreqs[CL_ZERO] = UINT_MAX;

	/* Get to boot_cluster_num - 0 for CL_ZERO; 1 for CL_ONE */
	boot_cluster = !MPIDR_AFFINITY_LEVEL(cpu_logical_map(0), 1);
	pr_debug("%s: boot_cluster is %s\n", __func__,
					boot_cluster == CL_ZERO ? "CL_ZERO" : "CL_ONE");
	exynos_boot_cluster = boot_cluster;

	init_cpumask_cluster_set(boot_cluster);

	for (cluster = 0; cluster < CL_END; cluster++) {
		exynos_info[cluster]->regulator = regulator_get(NULL, (cluster ? "vdd_mngs" : "vdd_apollo"));
		if (IS_ERR(exynos_info[cluster]->regulator)) {
			exynos_info[cluster]->regulator = regulator_get(NULL, (cluster ? "vdd_eagle" : "vdd_kfc"));
			if (IS_ERR(exynos_info[cluster]->regulator)) {
				pr_err("%s:failed to get resource vdd_%score\n", __func__,
												(cluster ? "cluster1" : "cluster0"));
				goto err_init;
			}
		}

		if (exynos_info[cluster]->set_freq == NULL) {
			pr_err("%s: No set_freq function (ERR)\n", __func__);
			goto err_init;
		}

		freq_max[cluster] = exynos_info[cluster]->
			freq_table[exynos_info[cluster]->max_support_idx].frequency;

		freq_min[cluster] = exynos_info[cluster]->
			freq_table[exynos_info[cluster]->min_support_idx].frequency;

		set_boot_freq(cluster);
		set_resume_freq(cluster);

		exynos_info[cluster]->cur_volt = regulator_get_voltage(exynos_info[cluster]->regulator);

		/* set initial old frequency */
		freqs[cluster]->old = exynos_getspeed_cluster(cluster);

		/*
		 * boot freq index is needed if minimum supported frequency
		 * greater than boot freq
		 */
			freq_table = exynos_info[cluster]->freq_table;
			exynos_info[cluster]->boot_freq_idx = -1;
			pr_debug("%s Core Max-Min frequency[%d - %d]KHz\n",
				cluster ? "CL_ONE" : "CL_ZERO", freq_max[cluster], freq_min[cluster]);

			for (i = L0; (freq_table[i].frequency != CPUFREQ_TABLE_END); i++) {
				if (exynos_info[cluster]->boot_freq == freq_table[i].frequency) {
					exynos_info[cluster]->boot_freq_idx = i;
					pr_debug("boot frequency[%d] index %d\n",
							exynos_info[cluster]->boot_freq, i);
				}

				if (freq_table[i].frequency > freq_max[cluster] ||
					freq_table[i].frequency < freq_min[cluster])
					freq_table[i].frequency = CPUFREQ_ENTRY_INVALID;
			}

		/* setup default qos constraints */
		core_max_qos_const[cluster].target_value = freq_max[cluster];
		core_max_qos_const[cluster].default_value = freq_max[cluster];
		pm_qos_update_constraints(qos_max_class[cluster], &core_max_qos_const[cluster]);

		pm_qos_add_notifier(qos_min_class[cluster],
							(cluster ? &exynos_cluster1_min_qos_notifier :
							&exynos_cluster0_min_qos_notifier));
		pm_qos_add_notifier(qos_max_class[cluster],
							(cluster ? &exynos_cluster1_max_qos_notifier :
							&exynos_cluster0_max_qos_notifier));

	}

	register_hotcpu_notifier(&exynos_cpufreq_cpu_up_nb);
	register_hotcpu_notifier(&exynos_cpufreq_cpu_down_nb);
	register_pm_notifier(&exynos_cpufreq_nb);
	register_reboot_notifier(&exynos_cpufreq_reboot_notifier);
#ifdef CONFIG_EXYNOS_THERMAL
	exynos_tmu_add_notifier(&exynos_tmu_nb);
#endif

#if defined(CONFIG_EXYNOS_BIG_FREQ_BOOST)
	cpufreq_register_notifier(&exynos_cpufreq_policy_nb, CPUFREQ_POLICY_NOTIFIER);
	register_cpus_notifier(&exynos_cpufreq_cpus_nb);
	exynos_cpufreq_print_max_freq();
#endif

	/* block frequency scale before acquire boot lock */
#if !defined(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE) && !defined(CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE)
	mutex_lock(&cpufreq_lock);
	exynos_info[CL_ZERO]->blocked = true;
	exynos_info[CL_ONE]->blocked = true;
	mutex_unlock(&cpufreq_lock);
#endif
	if (cpufreq_register_driver(&exynos_driver)) {
		pr_err("%s: failed to register cpufreq driver\n", __func__);
		goto err_cpufreq;
	}

	for (cluster = 0; cluster < CL_END; cluster++) {
		pm_qos_add_request(&core_min_qos[cluster], qos_min_class[cluster],
						qos_min_default_value[cluster]);
		pm_qos_add_request(&core_max_qos[cluster], qos_max_class[cluster],
						core_max_qos_const[cluster].default_value);
		pm_qos_add_request(&core_min_qos_real[cluster], qos_min_class[cluster],
						qos_min_default_value[cluster]);
		pm_qos_add_request(&core_max_qos_real[cluster], qos_max_class[cluster],
						core_max_qos_const[cluster].default_value);

		if (exynos_info[cluster]->boot_cpu_min_qos) {
			pm_qos_add_request(&boot_min_qos[cluster], qos_min_class[cluster],
						qos_min_default_value[cluster]);
			if (!exynos_info[cluster]->boot_cpu_min_qos_timeout)
				exynos_info[cluster]->boot_cpu_min_qos_timeout = 40 * USEC_PER_SEC;

			pm_qos_update_request_timeout(&boot_min_qos[cluster],
						exynos_info[cluster]->boot_cpu_min_qos,
						exynos_info[cluster]->boot_cpu_min_qos_timeout);
		}

		if (exynos_info[cluster]->boot_cpu_max_qos) {
			pm_qos_add_request(&boot_max_qos[cluster], qos_max_class[cluster],
						core_max_qos_const[cluster].default_value);
			if (!exynos_info[cluster]->boot_cpu_max_qos_timeout)
				exynos_info[cluster]->boot_cpu_max_qos_timeout = 40 * USEC_PER_SEC;

			pm_qos_update_request_timeout(&boot_max_qos[cluster],
						exynos_info[cluster]->boot_cpu_max_qos,
						exynos_info[cluster]->boot_cpu_max_qos_timeout);
		}

		if (exynos_info[cluster]->bus_table)
			pm_qos_add_request(&exynos_mif_qos[cluster], PM_QOS_BUS_THROUGHPUT, 0);
	}

	/* unblock frequency scale */
#if !defined(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE) && !defined(CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE)
	mutex_lock(&cpufreq_lock);
	exynos_info[CL_ZERO]->blocked = false;
	exynos_info[CL_ONE]->blocked = false;
	mutex_unlock(&cpufreq_lock);
#endif

	/*
	 * forced call cpufreq target function.
	 * If target function is called by interactive governor when blocked cpufreq scale,
	 * interactive governor's target_freq is updated to new_freq. But, frequency is
	 * not changed because blocking cpufreq scale. And if governor request same frequency
	 * after unblocked scale, speedchange_task is not wakeup because new_freq and target_freq
	 * is same.
	 */

	policy = cpufreq_cpu_get(NR_CLUST0_CPUS);
	if (!policy)
		goto err_policy;

	if (!policy->user_policy.governor) {
		cpufreq_cpu_put(policy);
		goto err_policy;
	}

	smp_call_function_single(NR_CLUST0_CPUS, exynos_qos_nop, NULL, 0);
	cpufreq_cpu_put(policy);

	ret = cpufreq_sysfs_create_group(&mp_attr_group);
	if (ret) {
		pr_err("%s: failed to create mp-cpufreq sysfs interface\n", __func__);
		goto err_mp_attr;
	}

#ifdef CONFIG_PM
	ret = sysfs_create_file(power_kobj, &cpufreq_table.attr);
	if (ret) {
		pr_err("%s: failed to create cpufreq_table sysfs interface\n", __func__);
		goto err_cpu_table;
	}

	ret = sysfs_create_file(power_kobj, &cpufreq_min_limit.attr);
	if (ret) {
		pr_err("%s: failed to create cpufreq_min_limit sysfs interface\n", __func__);
		goto err_cpufreq_min_limit;
	}

	ret = sysfs_create_file(power_kobj, &cpufreq_max_limit.attr);
	if (ret) {
		pr_err("%s: failed to create cpufreq_max_limit sysfs interface\n", __func__);
		goto err_cpufreq_max_limit;
	}
#endif

#ifdef CONFIG_SW_SELF_DISCHARGING
	ret = sysfs_create_file(power_kobj, &cpufreq_self_discharging.attr);
	if (ret) {
		pr_err("%s: failed to create cpufreq_self_discharging sysfs interface\n", __func__);
		goto err_cpufreq_self_discharging;
	}
#endif

	exynos_cpufreq_init_done = true;
	exynos_cpufreq_init_notify_call_chain(CPUFREQ_INIT_COMPLETE);

	pr_info("%s: MP-CPUFreq Initialization Complete\n", __func__);
	return 0;

#ifdef CONFIG_SW_SELF_DISCHARGING
err_cpufreq_self_discharging:
#ifdef CONFIG_PM
	sysfs_remove_file(power_kobj, &cpufreq_max_limit.attr);
#endif
#endif
#ifdef CONFIG_PM
err_cpufreq_max_limit:
	sysfs_remove_file(power_kobj, &cpufreq_min_limit.attr);
err_cpufreq_min_limit:
	sysfs_remove_file(power_kobj, &cpufreq_table.attr);
err_cpu_table:
	cpufreq_sysfs_remove_group(&mp_attr_group);
#endif
err_policy:
err_mp_attr:
	for (cluster = 0; cluster < CL_END; cluster++) {
		if (exynos_info[cluster]) {
			if (exynos_info[cluster]->bus_table &&
				pm_qos_request_active(&exynos_mif_qos[cluster]))
				pm_qos_remove_request(&exynos_mif_qos[cluster]);

			if (pm_qos_request_active(&boot_max_qos[cluster]))
				pm_qos_remove_request(&boot_max_qos[cluster]);
			if (pm_qos_request_active(&boot_min_qos[cluster]))
				pm_qos_remove_request(&boot_min_qos[cluster]);

			if (pm_qos_request_active(&core_min_qos[cluster]))
				pm_qos_remove_request(&core_min_qos[cluster]);
			if (pm_qos_request_active(&core_max_qos[cluster]))
				pm_qos_remove_request(&core_max_qos[cluster]);
			if (pm_qos_request_active(&core_min_qos_real[cluster]))
				pm_qos_remove_request(&core_min_qos_real[cluster]);
			if (pm_qos_request_active(&core_max_qos_real[cluster]))
				pm_qos_remove_request(&core_max_qos_real[cluster]);
		}
	}
	cpufreq_unregister_driver(&exynos_driver);
err_cpufreq:
	unregister_reboot_notifier(&exynos_cpufreq_reboot_notifier);
	unregister_pm_notifier(&exynos_cpufreq_nb);
	unregister_hotcpu_notifier(&exynos_cpufreq_cpu_up_nb);
	unregister_hotcpu_notifier(&exynos_cpufreq_cpu_down_nb);
#if defined(CONFIG_EXYNOS_BIG_FREQ_BOOST)
	unregister_cpus_notifier(&exynos_cpufreq_cpus_nb);
#endif
err_init:
	for (cluster = 0; cluster < CL_END; cluster++) {
		/* remove all pm_qos requests */
		if (exynos_info[cluster]) {
			/* Remove pm_qos notifiers */
			pm_qos_remove_notifier(qos_min_class[cluster],
								(cluster ? &exynos_cluster1_min_qos_notifier :
								&exynos_cluster0_min_qos_notifier));
			pm_qos_remove_notifier(qos_max_class[cluster],
								(cluster ? &exynos_cluster1_max_qos_notifier :
								&exynos_cluster0_max_qos_notifier));

			/* Release regulater handles */
			if (exynos_info[cluster]->regulator)
				regulator_put(exynos_info[cluster]->regulator);
		}
	}
	pr_err("%s: failed initialization\n", __func__);

	return ret;
}

static const struct of_device_id exynos_mp_cpufreq_match[] = {
	{
		.compatible = "samsung,exynos-mp-cpufreq",
	},
	{},
};
MODULE_DEVICE_TABLE(of, exynos_mp_cpufreq);

static struct platform_device_id exynos_mp_cpufreq_driver_ids[] = {
	{
		.name		= "exynos-mp-cpufreq",
	},
	{ },
};
MODULE_DEVICE_TABLE(platform, exynos_mp_cpufreq_driver_ids);

#if defined(CONFIG_ECT)
static void exynos_mp_cpufreq_parse_minlock(char *cluster_name, unsigned int frequency, unsigned int *mif_freq)
{
	int i;
	void *cpumif_block;
	struct ect_minlock_domain *domain;

	cpumif_block = ect_get_block(BLOCK_MINLOCK);
	if (cpumif_block == NULL)
		return;

	domain = ect_minlock_get_domain(cpumif_block, cluster_name);
	if (domain == NULL)
		return;

	for (i = 0; i < domain->num_of_level; ++i) {
		if (domain->level[i].main_frequencies == frequency) {
			*mif_freq = domain->level[i].sub_frequencies;
			return;
		}
	}

	*mif_freq = 0;
	return;
}

static int exynos_mp_cpufreq_parse_frequency(char *cluster_name, struct exynos_dvfs_info *ptr)
{
	int i;
	void *dvfs_block;
	struct ect_dvfs_domain *domain;

	dvfs_block = ect_get_block(BLOCK_DVFS);
	if (dvfs_block == NULL)
		return -ENODEV;

	domain = ect_dvfs_get_domain(dvfs_block, cluster_name);
	if (domain == NULL)
		return -ENODEV;

	ptr->freq_table = kzalloc(sizeof(struct cpufreq_frequency_table)
				* (domain->num_of_level + 1), GFP_KERNEL);
	if (ptr->freq_table == NULL) {
		pr_err("%s: failed to allocate memory for freq_table\n", __func__);
		return -ENOMEM;
	}

	ptr->volt_table = kzalloc(sizeof(unsigned int)
				* domain->num_of_level, GFP_KERNEL);
	if (ptr->volt_table == NULL) {
		pr_err("%s: failed to allocate for volt_table\n", __func__);
		return -ENOMEM;
	}

	ptr->bus_table = kzalloc(sizeof(unsigned int)
				* domain->num_of_level, GFP_KERNEL);
	if (ptr->bus_table == NULL) {
		pr_err("%s: failed to allocate for bus_table\n", __func__);
		return -ENOMEM;
	}

	ptr->max_idx_num = domain->num_of_level;
	for (i = 0; i < domain->num_of_level; ++i) {
		ptr->freq_table[i].driver_data = i;
		ptr->freq_table[i].frequency = domain->list_level[i].level;
		ptr->volt_table[i] = 0;
		exynos_mp_cpufreq_parse_minlock(cluster_name, ptr->freq_table[i].frequency, &ptr->bus_table[i]);
	}

	return 0;
}
#endif

static int exynos_mp_cpufreq_parse_dt(struct device_node *np, cluster_type cl)
{
	struct exynos_dvfs_info *ptr = exynos_info[cl];
	struct cpufreq_dvfs_table *table_ptr;
	unsigned int table_size, i;
	const char *cluster_domain_name;
	char *cluster_name;
	int ret;
	int not_using_ect = true;

	if (!np) {
		pr_info("%s: cpufreq_dt is not existed. \n", __func__);
		return -ENODEV;
	}

	if (of_property_read_u32(np,(cl ? "cl1_idx_num" : "cl0_idx_num"),
				&ptr->max_idx_num))
		return -ENODEV;

	if (of_property_read_u32(np, (cl ? "cl1_max_support_idx" : "cl0_max_support_idx"),
				&ptr->max_support_idx))
		return -ENODEV;

	if (of_property_read_u32(np, (cl ? "cl1_min_support_idx" : "cl0_min_support_idx"),
				&ptr->min_support_idx))
		return -ENODEV;

	if (of_property_read_u32(np, (cl ? "cl1_boot_max_qos" : "cl0_boot_max_qos"),
				&ptr->boot_cpu_max_qos))
		return -ENODEV;

	if (of_property_read_u32(np, (cl ? "cl1_boot_min_qos" : "cl0_boot_min_qos"),
				&ptr->boot_cpu_min_qos))
		return -ENODEV;

	if (of_property_read_u32(np, (cl ? "cl1_en_ema" : "cl0_en_ema"),
				&ptr->en_ema))
		return -ENODEV;

	if (cl == CL_ZERO) {
		if (of_property_read_u32(np, "cl0_boost_freq", &ptr->boost_freq))
			return -ENODEV;
	}

	if (cl == CL_ONE) {
		if (of_property_read_u32(np, "cl1_en_smpl", &ptr->en_smpl))
			return -ENODEV;
		if (of_property_read_u32(np, "cl1_reboot_limit_freq", &ptr->reboot_limit_freq))
			return -ENODEV;
#if defined(CONFIG_SEC_PM) && defined(CONFIG_MUIC_NOTIFIER)
		if (of_property_read_u32(np, "cl1_jig_boot_max_qos", &ptr->jig_boot_cpu_max_qos))
			return -ENODEV;
#endif

#if defined(CONFIG_EXYNOS_BIG_FREQ_BOOST)
		ptr->max_support_idx_table = kzalloc(sizeof(unsigned int)
				* (NR_CLUST1_CPUS + 1), GFP_KERNEL);
		ret = of_property_read_u32_array(np, "cl1_max_support_idx_table",
				(unsigned int *)ptr->max_support_idx_table, NR_CLUST1_CPUS + 1);
		if (ret < 0)
			return -ENODEV;

		/* change max_support_idx */
		ptr->max_support_idx = ptr->max_support_idx_table[1];
#endif
	}

	cluster_name = (cl ? "cl1_dvfs_table" :  "cl0_dvfs_table");

	if (of_property_read_string(np, (cl ? "cl1_dvfs_domain_name" : "cl0_dvfs_domain_name"),
				&cluster_domain_name))
		return -ENODEV;

#if defined(CONFIG_ECT)
	not_using_ect = exynos_mp_cpufreq_parse_frequency((char *)cluster_domain_name, ptr);
#endif
	if (not_using_ect) {
		/* memory alloc for table */
		ptr->freq_table = kzalloc(sizeof(struct cpufreq_frequency_table)
				* (ptr->max_idx_num + 1), GFP_KERNEL);
		if (ptr->freq_table == NULL) {
			pr_err("%s: failed to allocate memory for freq_table\n", __func__);
			return -ENOMEM;
		}

		ptr->volt_table = kzalloc(sizeof(unsigned int)
					* ptr->max_idx_num, GFP_KERNEL);
		if (ptr->volt_table == NULL) {
			pr_err("%s: failed to allocate for volt_table\n", __func__);
			return -ENOMEM;
		}

		ptr->bus_table = kzalloc(sizeof(unsigned int)
					* ptr->max_idx_num, GFP_KERNEL);
		if (ptr->bus_table == NULL) {
			pr_err("%s: failed to allocate for bus_table\n", __func__);
			return -ENOMEM;
		}

		/* temparary table for dt parse*/
		table_ptr = kzalloc(sizeof(struct cpufreq_dvfs_table)
				* ptr->max_idx_num, GFP_KERNEL);
		if (table_ptr == NULL) {
			pr_err("%s: failed to allocate for cpufreq_dvfs_table\n", __func__);
			kfree(table_ptr);
			return -ENOMEM;
		}
		table_size = sizeof(struct cpufreq_dvfs_table) / sizeof(unsigned int);

		ret = of_property_read_u32_array(np, cluster_name,
				(unsigned int *)table_ptr, table_size * (ptr->max_idx_num));
		if (ret < 0) {
			kfree(table_ptr);
			return -ENODEV;
		}

		/* freq/volt/bus table is initailized by DT */
		for (i = 0; i < ptr->max_idx_num; i++) {
			ptr->freq_table[i].driver_data = table_ptr[i].index;
			ptr->freq_table[i].frequency = table_ptr[i].frequency;
			ptr->volt_table[i] = table_ptr[i].voltage;
			ptr->bus_table[i] = table_ptr[i].bus_qos_lock;
		}

		kfree(table_ptr);
	}

	ptr->freq_table[ptr->max_idx_num].driver_data = ptr->max_idx_num;
	ptr->freq_table[ptr->max_idx_num].frequency = CPUFREQ_TABLE_END;

	return 0;
}


static int exynos_mp_cpufreq_probe(struct platform_device *pdev)
{
	int ret;
	cluster_type cluster;

	pm_qos_add_request(&cpufreq_cpu_hotplug_max_request, PM_QOS_CPU_ONLINE_MAX,
						PM_QOS_CPU_ONLINE_MAX_DEFAULT_VALUE);

	for (cluster = 0; cluster < CL_END; cluster++) {
		exynos_info[cluster] = kzalloc(sizeof(struct exynos_dvfs_info), GFP_KERNEL);
		if (!exynos_info[cluster]) {
			return -ENOMEM;
			goto err_init;
		}

		freqs[cluster] = kzalloc(sizeof(struct cpufreq_freqs), GFP_KERNEL);
		if (!freqs[cluster]) {
			ret = -ENOMEM;
			goto err_init;
		}
		/* parse dt */
		if (of_device_is_compatible(pdev->dev.of_node, "samsung,exynos-mp-cpufreq")) {
			ret = exynos_mp_cpufreq_parse_dt(pdev->dev.of_node, cluster);
			if (ret < 0) {
				pr_err("%s: parse_dt failed\n", __func__);
				goto err_init;
			}
		}
	}

	ret = exynos_cpufreq_cluster1_init(exynos_info[CL_ONE]);
	if (ret < 0)
		goto err_init;

	ret = exynos_cpufreq_cluster0_init(exynos_info[CL_ZERO]);
	if (ret < 0)
		goto err_init;

	ret = exynos_cpufreq_init();
	if (ret < 0)
		goto err_init;

	if (exynos_info[CL_ONE]->en_smpl && exynos_info[CL_ONE]->init_smpl) {
		exynos_cpufreq_smpl_warn_register_notifier(&exynos_cpufreq_smpl_warn_notifier);
		if (exynos_info[CL_ONE]->init_smpl() < 0) {
			pr_err("CL_ONE failed SMPL_WARN init\n");
			exynos_info[CL_ONE]->en_smpl = 0;
			exynos_info[CL_ONE]->check_smpl = NULL;
			exynos_cpufreq_smpl_warn_unregister_notifier(&exynos_cpufreq_smpl_warn_notifier);
		} else {
			ret = sysfs_create_file(power_kobj, &smpl_warn_ctrl.attr);
			if (ret < 0)
				pr_err("%s: failed to create smpl_warn_ctrl sysfs interface\n", __func__);
		}
	}

	return ret;

err_init:
	for (cluster = 0; cluster < CL_END; cluster++) {
		if (exynos_info[cluster]->bus_table)
			kfree(exynos_info[cluster]->bus_table);

		if (exynos_info[cluster]->volt_table)
			kfree(exynos_info[cluster]->volt_table);

		if (exynos_info[cluster]->freq_table)
			kfree(exynos_info[cluster]->freq_table);

		if (exynos_info[cluster])
			kfree(exynos_info[cluster]);

		if (freqs[cluster])
			kfree(freqs[cluster]);
	}

	pr_err("%s: failed initialization\n", __func__);

	return ret;
}

static int exynos_mp_cpufreq_remove(struct platform_device *pdev)
{
	cluster_type cluster;

	for (cluster = 0; cluster < CL_END; cluster++) {
		if (exynos_info[cluster]->bus_table &&
				pm_qos_request_active(&exynos_mif_qos[cluster]))
			pm_qos_remove_request(&exynos_mif_qos[cluster]);

		if (pm_qos_request_active(&boot_max_qos[cluster]))
			pm_qos_remove_request(&boot_max_qos[cluster]);
		if (pm_qos_request_active(&boot_min_qos[cluster]))
			pm_qos_remove_request(&boot_min_qos[cluster]);
		if (pm_qos_request_active(&core_min_qos[cluster]))
			pm_qos_remove_request(&core_min_qos[cluster]);
		if (pm_qos_request_active(&core_max_qos[cluster]))
			pm_qos_remove_request(&core_max_qos[cluster]);
		if (pm_qos_request_active(&core_min_qos_real[cluster]))
			pm_qos_remove_request(&core_min_qos_real[cluster]);
		if (pm_qos_request_active(&core_max_qos_real[cluster]))
			pm_qos_remove_request(&core_max_qos_real[cluster]);

		/* Remove pm_qos notifiers */
		pm_qos_remove_notifier(qos_min_class[cluster],
				(cluster ? &exynos_cluster1_min_qos_notifier :
				 &exynos_cluster0_min_qos_notifier));
		pm_qos_remove_notifier(qos_max_class[cluster],
				(cluster ? &exynos_cluster1_max_qos_notifier :
				 &exynos_cluster0_max_qos_notifier));

		/* Release regulater handles */
		if (exynos_info[cluster]->regulator)
			regulator_put(exynos_info[cluster]->regulator);

		/* free table momory */
		if (exynos_info[cluster]->bus_table)
			kfree(exynos_info[cluster]->bus_table);

		if (exynos_info[cluster]->volt_table)
			kfree(exynos_info[cluster]->volt_table);

		if (exynos_info[cluster]->freq_table)
			kfree(exynos_info[cluster]->freq_table);

		if (exynos_info[cluster])
			kfree(exynos_info[cluster]);

		if (freqs[cluster])
			kfree(freqs[cluster]);
	}
	unregister_reboot_notifier(&exynos_cpufreq_reboot_notifier);
	unregister_pm_notifier(&exynos_cpufreq_nb);
	unregister_hotcpu_notifier(&exynos_cpufreq_cpu_up_nb);
	unregister_hotcpu_notifier(&exynos_cpufreq_cpu_down_nb);

#if defined(CONFIG_EXYNOS_BIG_FREQ_BOOST)
	unregister_cpus_notifier(&exynos_cpufreq_cpus_nb);
#endif

#ifdef CONFIG_PM
	sysfs_remove_file(power_kobj, &cpufreq_min_limit.attr);
	sysfs_remove_file(power_kobj, &cpufreq_table.attr);
	cpufreq_sysfs_remove_group(&mp_attr_group);
#endif

	cpufreq_unregister_driver(&exynos_driver);
	return 0;
}

static struct platform_driver exynos_mp_cpufreq_driver = {
	.probe	= exynos_mp_cpufreq_probe,
	.remove	= exynos_mp_cpufreq_remove,
	.id_table = exynos_mp_cpufreq_driver_ids,
	.driver	= {
		.name	= "exynos-mp-cpufreq",
		.owner	= THIS_MODULE,
		.of_match_table = of_match_ptr(exynos_mp_cpufreq_match),
	},
};

static struct platform_device exynos_mp_cpufreq_device = {
	.name	= "exynos-cpufreq",
	.id	= -1,
};

static int __init exynos_mp_cpufreq_init(void)
{
	int ret = 0;

	ret = platform_device_register(&exynos_mp_cpufreq_device);
	if (ret)
		return ret;

	return platform_driver_register(&exynos_mp_cpufreq_driver);
}
#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
device_initcall(exynos_mp_cpufreq_init);
#else
late_initcall(exynos_mp_cpufreq_init);
#endif

#if defined(CONFIG_SEC_PM) && defined(CONFIG_MUIC_NOTIFIER)

/* In case of CCIC, check jig detection by boot param */
#if defined(CONFIG_MUIC_SUPPORT_CCIC) && defined(CONFIG_SEC_FACTORY)
static bool jig_is_attached = false;
static __init int get_jig_status(char *arg)
{
	jig_is_attached = true;	
	return 0;
}

early_param("jig", get_jig_status);
#else
static struct notifier_block cpufreq_muic_nb;
static bool jig_is_attached;

static int exynos_cpufreq_muic_notifier(struct notifier_block *nb,
				unsigned long action, void *data)
{
	muic_attached_dev_t attached_dev = *(muic_attached_dev_t *)data;

	switch (attached_dev) {
	case ATTACHED_DEV_JIG_UART_OFF_MUIC:
	case ATTACHED_DEV_JIG_UART_OFF_VB_MUIC:
	case ATTACHED_DEV_JIG_UART_OFF_VB_OTG_MUIC:
	case ATTACHED_DEV_JIG_UART_OFF_VB_FG_MUIC:
		if (action == MUIC_NOTIFY_CMD_DETACH)
			jig_is_attached = false;
		else if (action == MUIC_NOTIFY_CMD_ATTACH)
			jig_is_attached = true;
		else
			pr_err("%s - ACTION Error!\n", __func__);
		break;
	default:
		break;
	}

	return NOTIFY_DONE;
}
#endif
static int __init exynos_cpufreq_late_init(void)
{
	cluster_type cluster;
	unsigned long timeout = 500 * USEC_PER_SEC;

#ifdef CONFIG_SEC_FACTORY
	timeout = 100 * USEC_PER_SEC;
#endif

#if defined(CONFIG_MUIC_SUPPORT_CCIC) && defined(CONFIG_SEC_FACTORY)
	if (!jig_is_attached)
		return 0;
#else
	muic_notifier_register(&cpufreq_muic_nb,
			exynos_cpufreq_muic_notifier, MUIC_NOTIFY_DEV_CPUFREQ);

	if (!jig_is_attached)
		return 0;
#endif
	pr_info("%s: JIG is attached: boot cpu qos %lu sec\n", __func__,
			timeout / USEC_PER_SEC);

	for (cluster = 0; cluster < CL_END; cluster++) {
		if (!exynos_info[cluster]->jig_boot_cpu_max_qos)
			exynos_info[cluster]->jig_boot_cpu_max_qos =
				exynos_info[cluster]->boot_cpu_max_qos;

		if (exynos_info[cluster]->jig_boot_cpu_max_qos)
			pm_qos_add_request(&jig_boot_max_qos[cluster],
					qos_max_class[cluster],
					core_max_qos_const[cluster].default_value);
			pm_qos_update_request_timeout(&jig_boot_max_qos[cluster],
					exynos_info[cluster]->jig_boot_cpu_max_qos,
					timeout);
	}
	return 0;
}

late_initcall(exynos_cpufreq_late_init);
#endif /* CONFIG_SEC_PM && CONFIG_MUIC_NOTIFIER && CONFIG_MUIC_SUPPORT_CCIC is not defined */

static void __exit exynos_mp_cpufreq_exit(void)
{
	platform_driver_unregister(&exynos_mp_cpufreq_driver);
	platform_device_unregister(&exynos_mp_cpufreq_device);
}
module_exit(exynos_mp_cpufreq_exit);