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
|
/*
* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define pr_fmt(fmt) "%s: " fmt, __func__
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/platform_device.h>
#include <linux/wakelock.h>
#include <linux/workqueue.h>
#include <linux/regulator/driver.h>
#include <mach/rpm.h>
#include <mach/rpm-regulator.h>
#include <mach/rpm-regulator-smd.h>
#include <mach/socinfo.h>
#include "rpm_resources.h"
#include "rpm-regulator-private.h"
/* Debug Definitions */
enum {
MSM_RPM_VREG_DEBUG_REQUEST = BIT(0),
MSM_RPM_VREG_DEBUG_VOTE = BIT(1),
MSM_RPM_VREG_DEBUG_DUPLICATE = BIT(2),
MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG = BIT(3),
};
static int msm_rpm_vreg_debug_mask;
module_param_named(
debug_mask, msm_rpm_vreg_debug_mask, int, S_IRUSR | S_IWUSR
);
/* Used for access via the rpm_regulator_* API. */
struct rpm_regulator {
int vreg_id;
enum rpm_vreg_voter voter;
int sleep_also;
int min_uV;
int max_uV;
};
struct vreg_config *(*get_config[])(void) = {
[RPM_VREG_VERSION_8660] = get_config_8660,
[RPM_VREG_VERSION_8960] = get_config_8960,
[RPM_VREG_VERSION_9615] = get_config_9615,
[RPM_VREG_VERSION_8930] = get_config_8930,
[RPM_VREG_VERSION_8930_PM8917] = get_config_8930_pm8917,
[RPM_VREG_VERSION_8960_PM8917] = get_config_8960_pm8917,
};
static struct rpm_regulator_consumer_mapping *consumer_map;
static int consumer_map_len;
#define SET_PART(_vreg, _part, _val) \
_vreg->req[_vreg->part->_part.word].value \
= (_vreg->req[_vreg->part->_part.word].value \
& ~_vreg->part->_part.mask) \
| (((_val) << _vreg->part->_part.shift) \
& _vreg->part->_part.mask)
#define GET_PART(_vreg, _part) \
((_vreg->req[_vreg->part->_part.word].value & _vreg->part->_part.mask) \
>> _vreg->part->_part.shift)
#define GET_PART_PREV_ACT(_vreg, _part) \
((_vreg->prev_active_req[_vreg->part->_part.word].value \
& _vreg->part->_part.mask) \
>> _vreg->part->_part.shift)
#define USES_PART(_vreg, _part) (_vreg->part->_part.mask)
#define vreg_err(vreg, fmt, ...) \
pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
#define RPM_VREG_PIN_CTRL_EN0 0x01
#define RPM_VREG_PIN_CTRL_EN1 0x02
#define RPM_VREG_PIN_CTRL_EN2 0x04
#define RPM_VREG_PIN_CTRL_EN3 0x08
#define RPM_VREG_PIN_CTRL_ALL 0x0F
static const char *label_freq[] = {
[RPM_VREG_FREQ_NONE] = " N/A",
[RPM_VREG_FREQ_19p20] = "19.2",
[RPM_VREG_FREQ_9p60] = "9.60",
[RPM_VREG_FREQ_6p40] = "6.40",
[RPM_VREG_FREQ_4p80] = "4.80",
[RPM_VREG_FREQ_3p84] = "3.84",
[RPM_VREG_FREQ_3p20] = "3.20",
[RPM_VREG_FREQ_2p74] = "2.74",
[RPM_VREG_FREQ_2p40] = "2.40",
[RPM_VREG_FREQ_2p13] = "2.13",
[RPM_VREG_FREQ_1p92] = "1.92",
[RPM_VREG_FREQ_1p75] = "1.75",
[RPM_VREG_FREQ_1p60] = "1.60",
[RPM_VREG_FREQ_1p48] = "1.48",
[RPM_VREG_FREQ_1p37] = "1.37",
[RPM_VREG_FREQ_1p28] = "1.28",
[RPM_VREG_FREQ_1p20] = "1.20",
};
static const char *label_corner[] = {
[RPM_VREG_CORNER_NONE] = "NONE",
[RPM_VREG_CORNER_LOW] = "LOW",
[RPM_VREG_CORNER_NOMINAL] = "NOM",
[RPM_VREG_CORNER_HIGH] = "HIGH",
};
/*
* This is used when voting for LPM or HPM by subtracting or adding to the
* hpm_min_load of a regulator. It has units of uA.
*/
#define LOAD_THRESHOLD_STEP 1000
/* rpm_version keeps track of the version for the currently running driver. */
enum rpm_vreg_version rpm_version = -1;
/* config holds all configuration data of the currently running driver. */
static struct vreg_config *config;
/* These regulator ID values are specified in the board file. */
static int vreg_id_vdd_mem, vreg_id_vdd_dig;
static inline int vreg_id_is_vdd_mem_or_dig(int id)
{
return id == vreg_id_vdd_mem || id == vreg_id_vdd_dig;
}
#define DEBUG_PRINT_BUFFER_SIZE 512
static void rpm_regulator_req(struct vreg *vreg, int set)
{
int uV, mV, fm, pm, pc, pf, pd, freq, state, i;
const char *pf_label = "", *fm_label = "", *pc_total = "";
const char *pc_en[4] = {"", "", "", ""};
const char *pm_label = "", *freq_label = "", *corner_label = "";
char buf[DEBUG_PRINT_BUFFER_SIZE];
size_t buflen = DEBUG_PRINT_BUFFER_SIZE;
int pos = 0;
/* Suppress VDD_MEM and VDD_DIG printing. */
if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
&& vreg_id_is_vdd_mem_or_dig(vreg->id))
return;
uV = GET_PART(vreg, uV);
mV = GET_PART(vreg, mV);
if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
uV = -uV;
mV = -mV;
}
fm = GET_PART(vreg, fm);
pm = GET_PART(vreg, pm);
pc = GET_PART(vreg, pc);
pf = GET_PART(vreg, pf);
pd = GET_PART(vreg, pd);
freq = GET_PART(vreg, freq);
state = GET_PART(vreg, enable_state);
if (pf >= 0 && pf < config->label_pin_func_len)
pf_label = config->label_pin_func[pf];
if (fm >= 0 && fm < config->label_force_mode_len)
fm_label = config->label_force_mode[fm];
if (pm >= 0 && pm < config->label_power_mode_len)
pm_label = config->label_power_mode[pm];
if (freq >= 0 && freq < ARRAY_SIZE(label_freq))
freq_label = label_freq[freq];
for (i = 0; i < config->label_pin_ctrl_len; i++)
if (pc & (1 << i))
pc_en[i] = config->label_pin_ctrl[i];
if (pc == RPM_VREG_PIN_CTRL_NONE)
pc_total = " none";
pos += scnprintf(buf + pos, buflen - pos, "%s%s: ",
KERN_INFO, __func__);
pos += scnprintf(buf + pos, buflen - pos, "%s %-9s: s=%c",
(set == MSM_RPM_CTX_SET_0 ? "sending " : "buffered"),
vreg->rdesc.name,
(set == MSM_RPM_CTX_SET_0 ? 'A' : 'S'));
if (USES_PART(vreg, uV) && vreg->type != RPM_REGULATOR_TYPE_CORNER)
pos += scnprintf(buf + pos, buflen - pos, ", v=%7d uV", uV);
if (USES_PART(vreg, mV))
pos += scnprintf(buf + pos, buflen - pos, ", v=%4d mV", mV);
if (USES_PART(vreg, enable_state))
pos += scnprintf(buf + pos, buflen - pos, ", state=%s (%d)",
(state == 1 ? "on" : "off"), state);
if (USES_PART(vreg, ip))
pos += scnprintf(buf + pos, buflen - pos,
", ip=%4d mA", GET_PART(vreg, ip));
if (USES_PART(vreg, fm))
pos += scnprintf(buf + pos, buflen - pos,
", fm=%s (%d)", fm_label, fm);
if (USES_PART(vreg, pc))
pos += scnprintf(buf + pos, buflen - pos,
", pc=%s%s%s%s%s (%X)", pc_en[0], pc_en[1],
pc_en[2], pc_en[3], pc_total, pc);
if (USES_PART(vreg, pf))
pos += scnprintf(buf + pos, buflen - pos,
", pf=%s (%d)", pf_label, pf);
if (USES_PART(vreg, pd))
pos += scnprintf(buf + pos, buflen - pos,
", pd=%s (%d)", (pd == 1 ? "Y" : "N"), pd);
if (USES_PART(vreg, ia))
pos += scnprintf(buf + pos, buflen - pos,
", ia=%4d mA", GET_PART(vreg, ia));
if (USES_PART(vreg, freq)) {
if (vreg->type == RPM_REGULATOR_TYPE_NCP)
pos += scnprintf(buf + pos, buflen - pos,
", freq=%2d", freq);
else
pos += scnprintf(buf + pos, buflen - pos,
", freq=%s MHz (%2d)", freq_label, freq);
}
if (USES_PART(vreg, pm))
pos += scnprintf(buf + pos, buflen - pos,
", pm=%s (%d)", pm_label, pm);
if (USES_PART(vreg, freq_clk_src))
pos += scnprintf(buf + pos, buflen - pos,
", clk_src=%d", GET_PART(vreg, freq_clk_src));
if (USES_PART(vreg, comp_mode))
pos += scnprintf(buf + pos, buflen - pos,
", comp=%d", GET_PART(vreg, comp_mode));
if (USES_PART(vreg, hpm))
pos += scnprintf(buf + pos, buflen - pos,
", hpm=%d", GET_PART(vreg, hpm));
if (USES_PART(vreg, uV) && vreg->type == RPM_REGULATOR_TYPE_CORNER) {
if (uV >= 0 && uV < (ARRAY_SIZE(label_corner) - 1))
corner_label = label_corner[uV+1];
pos += scnprintf(buf + pos, buflen - pos, ", corner=%s (%d)",
corner_label, uV);
}
pos += scnprintf(buf + pos, buflen - pos, "; req[0]={%d, 0x%08X}",
vreg->req[0].id, vreg->req[0].value);
if (vreg->part->request_len > 1)
pos += scnprintf(buf + pos, buflen - pos,
", req[1]={%d, 0x%08X}", vreg->req[1].id,
vreg->req[1].value);
pos += scnprintf(buf + pos, buflen - pos, "\n");
printk(buf);
}
static void rpm_regulator_vote(struct vreg *vreg, enum rpm_vreg_voter voter,
int set, int voter_uV, int aggregate_uV)
{
/* Suppress VDD_MEM and VDD_DIG printing. */
if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
&& vreg_id_is_vdd_mem_or_dig(vreg->id))
return;
pr_info("vote received %-9s: voter=%d, set=%c, v_voter=%7d uV, "
"v_aggregate=%7d uV\n", vreg->rdesc.name, voter,
(set == 0 ? 'A' : 'S'), voter_uV, aggregate_uV);
}
static void rpm_regulator_duplicate(struct vreg *vreg, int set, int cnt)
{
/* Suppress VDD_MEM and VDD_DIG printing. */
if ((msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_IGNORE_VDD_MEM_DIG)
&& vreg_id_is_vdd_mem_or_dig(vreg->id))
return;
if (cnt == 2)
pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}, "
"req[1]={%d, 0x%08X}\n", vreg->rdesc.name,
(set == 0 ? 'A' : 'S'),
vreg->req[0].id, vreg->req[0].value,
vreg->req[1].id, vreg->req[1].value);
else if (cnt == 1)
pr_info("ignored request %-9s: set=%c; req[0]={%d, 0x%08X}\n",
vreg->rdesc.name, (set == 0 ? 'A' : 'S'),
vreg->req[0].id, vreg->req[0].value);
}
static bool requires_tcxo_workaround;
static struct clk *tcxo_handle;
static struct wake_lock tcxo_wake_lock;
static DEFINE_MUTEX(tcxo_mutex);
static bool tcxo_is_enabled;
/*
* TCXO must be kept on for at least the duration of its warmup (4 ms);
* otherwise, it will stay on when hardware disabling is attempted.
*/
#define TCXO_WARMUP_TIME_MS 4
static void tcxo_get_handle(void)
{
if (!tcxo_handle) {
tcxo_handle = clk_get_sys("rpm-regulator", "vref_buff");
if (IS_ERR(tcxo_handle))
tcxo_handle = NULL;
}
}
/*
* Perform best effort enable of CXO. Since the MSM clock drivers depend upon
* the rpm-regulator driver, any rpm-regulator devices that are configured with
* always_on == 1 will not be able to enable CXO during probe. This does not
* cause a problem though since CXO will be enabled by the boot loaders before
* Apps boots up.
*/
static bool tcxo_enable(void)
{
int rc;
if (tcxo_handle && !tcxo_is_enabled) {
rc = clk_prepare_enable(tcxo_handle);
if (!rc) {
tcxo_is_enabled = true;
wake_lock(&tcxo_wake_lock);
return true;
}
}
return false;
}
static void tcxo_delayed_disable_work(struct work_struct *work)
{
mutex_lock(&tcxo_mutex);
clk_disable_unprepare(tcxo_handle);
tcxo_is_enabled = false;
wake_unlock(&tcxo_wake_lock);
mutex_unlock(&tcxo_mutex);
}
static DECLARE_DELAYED_WORK(tcxo_disable_work, tcxo_delayed_disable_work);
static void tcxo_delayed_disable(void)
{
/*
* The delay in jiffies has 1 added to it to ensure that at least
* one jiffy takes place before the work is enqueued. Without this,
* the work would be scheduled to run in the very next jiffy which could
* result in too little delay and TCXO being stuck on.
*/
if (tcxo_handle)
schedule_delayed_work(&tcxo_disable_work,
msecs_to_jiffies(TCXO_WARMUP_TIME_MS) + 1);
}
/* Mutex lock needed for sleep-selectable regulators. */
static DEFINE_MUTEX(rpm_sleep_sel_lock);
static int voltage_from_req(struct vreg *vreg)
{
int uV = 0;
if (vreg->part->uV.mask)
uV = GET_PART(vreg, uV);
else if (vreg->part->mV.mask)
uV = MILLI_TO_MICRO(GET_PART(vreg, mV));
else if (vreg->part->enable_state.mask)
uV = GET_PART(vreg, enable_state);
return uV;
}
static void voltage_to_req(int uV, struct vreg *vreg)
{
if (vreg->part->uV.mask)
SET_PART(vreg, uV, uV);
else if (vreg->part->mV.mask)
SET_PART(vreg, mV, MICRO_TO_MILLI(uV));
else if (vreg->part->enable_state.mask)
SET_PART(vreg, enable_state, uV);
}
static int vreg_send_request(struct vreg *vreg, enum rpm_vreg_voter voter,
int set, unsigned mask0, unsigned val0,
unsigned mask1, unsigned val1, unsigned cnt,
int update_voltage)
{
struct msm_rpm_iv_pair *prev_req;
int rc = 0, max_uV_vote = 0;
bool tcxo_enabled = false;
bool voltage_increased = false;
unsigned prev0, prev1;
int *min_uV_vote;
int i;
if (set == MSM_RPM_CTX_SET_0) {
min_uV_vote = vreg->active_min_uV_vote;
prev_req = vreg->prev_active_req;
} else {
min_uV_vote = vreg->sleep_min_uV_vote;
prev_req = vreg->prev_sleep_req;
}
prev0 = vreg->req[0].value;
vreg->req[0].value &= ~mask0;
vreg->req[0].value |= val0 & mask0;
prev1 = vreg->req[1].value;
vreg->req[1].value &= ~mask1;
vreg->req[1].value |= val1 & mask1;
/* Set the force mode field based on which set is being requested. */
if (set == MSM_RPM_CTX_SET_0)
SET_PART(vreg, fm, vreg->pdata.force_mode);
else
SET_PART(vreg, fm, vreg->pdata.sleep_set_force_mode);
if (update_voltage)
min_uV_vote[voter] = voltage_from_req(vreg);
/* Find the highest voltage voted for and use it. */
for (i = 0; i < RPM_VREG_VOTER_COUNT; i++)
max_uV_vote = max(max_uV_vote, min_uV_vote[i]);
voltage_to_req(max_uV_vote, vreg);
if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_VOTE)
rpm_regulator_vote(vreg, voter, set, min_uV_vote[voter],
max_uV_vote);
/* Ignore duplicate requests */
if (vreg->req[0].value != prev_req[0].value ||
vreg->req[1].value != prev_req[1].value) {
/* Enable CXO clock if necessary for TCXO workaround. */
if (requires_tcxo_workaround && vreg->requires_cxo
&& (set == MSM_RPM_CTX_SET_0)
&& (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
mutex_lock(&tcxo_mutex);
if (!tcxo_handle)
tcxo_get_handle();
voltage_increased = true;
tcxo_enabled = tcxo_enable();
}
rc = msm_rpmrs_set(set, vreg->req, cnt);
if (rc) {
vreg->req[0].value = prev0;
vreg->req[1].value = prev1;
vreg_err(vreg, "msm_rpmrs_set failed - "
"set=%s, id=%d, rc=%d\n",
(set == MSM_RPM_CTX_SET_0 ? "active" : "sleep"),
vreg->req[0].id, rc);
} else {
/* Only save if nonzero and active set. */
if (max_uV_vote && (set == MSM_RPM_CTX_SET_0))
vreg->save_uV = max_uV_vote;
if (msm_rpm_vreg_debug_mask
& MSM_RPM_VREG_DEBUG_REQUEST)
rpm_regulator_req(vreg, set);
prev_req[0].value = vreg->req[0].value;
prev_req[1].value = vreg->req[1].value;
}
/*
* Schedule CXO clock to be disabled after TCXO warmup time if
* TCXO workaround is applicable for this regulator.
*/
if (voltage_increased) {
if (tcxo_enabled)
tcxo_delayed_disable();
mutex_unlock(&tcxo_mutex);
}
} else if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE) {
rpm_regulator_duplicate(vreg, set, cnt);
}
return rc;
}
static int vreg_set_sleep_sel(struct vreg *vreg, enum rpm_vreg_voter voter,
int sleep, unsigned mask0, unsigned val0,
unsigned mask1, unsigned val1, unsigned cnt,
int update_voltage)
{
unsigned int s_mask[2] = {mask0, mask1}, s_val[2] = {val0, val1};
int rc;
if (voter < 0 || voter >= RPM_VREG_VOTER_COUNT)
return -EINVAL;
mutex_lock(&rpm_sleep_sel_lock);
/*
* Send sleep set request first so that subsequent set_mode, etc calls
* use the voltage from the active set.
*/
if (sleep)
rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
mask0, val0, mask1, val1, cnt, update_voltage);
else {
/*
* Vote for 0 V in the sleep set when active set-only is
* specified. This ensures that a disable vote will be issued
* at some point for the sleep set of the regulator.
*/
if (vreg->part->uV.mask) {
s_val[vreg->part->uV.word] = 0 << vreg->part->uV.shift;
s_mask[vreg->part->uV.word] = vreg->part->uV.mask;
} else if (vreg->part->mV.mask) {
s_val[vreg->part->mV.word] = 0 << vreg->part->mV.shift;
s_mask[vreg->part->mV.word] = vreg->part->mV.mask;
} else if (vreg->part->enable_state.mask) {
s_val[vreg->part->enable_state.word]
= 0 << vreg->part->enable_state.shift;
s_mask[vreg->part->enable_state.word]
= vreg->part->enable_state.mask;
}
rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_SLEEP,
s_mask[0], s_val[0], s_mask[1], s_val[1],
cnt, update_voltage);
}
rc = vreg_send_request(vreg, voter, MSM_RPM_CTX_SET_0, mask0, val0,
mask1, val1, cnt, update_voltage);
mutex_unlock(&rpm_sleep_sel_lock);
return rc;
}
/**
* rpm_vreg_set_voltage - vote for a min_uV value of specified regualtor
* @vreg: ID for regulator
* @voter: ID for the voter
* @min_uV: minimum acceptable voltage (in uV) that is voted for
* @max_uV: maximum acceptable voltage (in uV) that is voted for
* @sleep_also: 0 for active set only, non-0 for active set and sleep set
*
* Returns 0 on success or errno.
*
* This function is used to vote for the voltage of a regulator without
* using the regulator framework. It is needed for consumers which wish to only
* vote for active set regulator voltage.
*
* If sleep_also == 0, then a sleep-set value of 0V will be voted for.
*
* This function may only be called for regulators which have the sleep flag
* specified in their private data.
*
* Consumers can vote to disable a regulator with this function by passing
* min_uV = 0 and max_uV = 0.
*
* Voltage switch type regulators may be controlled via rpm_vreg_set_voltage
* as well. For this type of regulator, max_uV > 0 is treated as an enable
* request and max_uV == 0 is treated as a disable request.
*/
int rpm_vreg_set_voltage(int vreg_id, enum rpm_vreg_voter voter, int min_uV,
int max_uV, int sleep_also)
{
unsigned int mask[2] = {0}, val[2] = {0};
struct vreg_range *range;
struct vreg *vreg;
int uV = min_uV;
int lim_min_uV, lim_max_uV, i, rc;
if (!config) {
pr_err("rpm-regulator driver has not probed yet.\n");
return -ENODEV;
}
if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
pr_err("invalid regulator id=%d\n", vreg_id);
return -EINVAL;
}
vreg = &config->vregs[vreg_id];
if (!vreg->pdata.sleep_selectable) {
vreg_err(vreg, "regulator is not marked sleep selectable\n");
return -EINVAL;
}
/* Allow min_uV == max_uV == 0 to represent a disable request. */
if ((min_uV != 0 || max_uV != 0)
&& (vreg->part->uV.mask || vreg->part->mV.mask)) {
/*
* Check if request voltage is outside of allowed range. The
* regulator core has already checked that constraint range
* is inside of the physically allowed range.
*/
lim_min_uV = vreg->pdata.init_data.constraints.min_uV;
lim_max_uV = vreg->pdata.init_data.constraints.max_uV;
if (uV < lim_min_uV && max_uV >= lim_min_uV)
uV = lim_min_uV;
if (uV < lim_min_uV || uV > lim_max_uV) {
vreg_err(vreg, "request v=[%d, %d] is outside allowed "
"v=[%d, %d]\n", min_uV, max_uV, lim_min_uV,
lim_max_uV);
return -EINVAL;
}
range = &vreg->set_points->range[0];
/* Find the range which uV is inside of. */
for (i = vreg->set_points->count - 1; i > 0; i--) {
if (uV > vreg->set_points->range[i - 1].max_uV) {
range = &vreg->set_points->range[i];
break;
}
}
/*
* Force uV to be an allowed set point and apply a ceiling
* function to non-set point values.
*/
uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
uV = uV * range->step_uV + range->min_uV;
if (uV > max_uV) {
vreg_err(vreg,
"request v=[%d, %d] cannot be met by any set point; "
"next set point: %d\n",
min_uV, max_uV, uV);
return -EINVAL;
}
}
if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
/*
* Translate from enum values which work as inputs in the
* rpm_vreg_set_voltage function to the actual corner values
* sent to the RPM.
*/
if (uV > 0)
uV -= RPM_VREG_CORNER_NONE;
}
if (vreg->part->uV.mask) {
val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
mask[vreg->part->uV.word] = vreg->part->uV.mask;
} else if (vreg->part->mV.mask) {
val[vreg->part->mV.word]
= MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
mask[vreg->part->mV.word] = vreg->part->mV.mask;
} else if (vreg->part->enable_state.mask) {
/*
* Translate max_uV > 0 into an enable request for regulator
* types which to not support voltage setting, e.g. voltage
* switches.
*/
val[vreg->part->enable_state.word]
= (max_uV > 0 ? 1 : 0) << vreg->part->enable_state.shift;
mask[vreg->part->enable_state.word]
= vreg->part->enable_state.mask;
}
rc = vreg_set_sleep_sel(vreg, voter, sleep_also, mask[0], val[0],
mask[1], val[1], vreg->part->request_len, 1);
if (rc)
vreg_err(vreg, "vreg_set_sleep_sel failed, rc=%d\n", rc);
return rc;
}
EXPORT_SYMBOL_GPL(rpm_vreg_set_voltage);
/**
* rpm_vreg_set_frequency - sets the frequency of a switching regulator
* @vreg: ID for regulator
* @freq: enum corresponding to desired frequency
*
* Returns 0 on success or errno.
*/
int rpm_vreg_set_frequency(int vreg_id, enum rpm_vreg_freq freq)
{
unsigned int mask[2] = {0}, val[2] = {0};
struct vreg *vreg;
int rc;
if (!config) {
pr_err("rpm-regulator driver has not probed yet.\n");
return -ENODEV;
}
if (vreg_id < config->vreg_id_min || vreg_id > config->vreg_id_max) {
pr_err("invalid regulator id=%d\n", vreg_id);
return -EINVAL;
}
vreg = &config->vregs[vreg_id];
if (freq < 0 || freq > RPM_VREG_FREQ_1p20) {
vreg_err(vreg, "invalid frequency=%d\n", freq);
return -EINVAL;
}
if (!vreg->pdata.sleep_selectable) {
vreg_err(vreg, "regulator is not marked sleep selectable\n");
return -EINVAL;
}
if (!vreg->part->freq.mask) {
vreg_err(vreg, "frequency not supported\n");
return -EINVAL;
}
val[vreg->part->freq.word] = freq << vreg->part->freq.shift;
mask[vreg->part->freq.word] = vreg->part->freq.mask;
rc = vreg_set_sleep_sel(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1, mask[0],
val[0], mask[1], val[1], vreg->part->request_len, 0);
if (rc)
vreg_err(vreg, "vreg_set_sleep_sel failed, rc=%d\n", rc);
return rc;
}
EXPORT_SYMBOL_GPL(rpm_vreg_set_frequency);
#define MAX_NAME_LEN 64
/**
* rpm_regulator_get() - lookup and obtain a handle to an RPM regulator
* @dev: device for regulator consumer
* @supply: supply name
*
* Returns a struct rpm_regulator corresponding to the regulator producer,
* or ERR_PTR() containing errno.
*
* This function may only be called from nonatomic context. The mapping between
* <dev, supply> tuples and rpm_regulators struct pointers is specified via
* rpm-regulator platform data.
*/
struct rpm_regulator *rpm_regulator_get(struct device *dev, const char *supply)
{
struct rpm_regulator_consumer_mapping *mapping = NULL;
const char *devname = NULL;
struct rpm_regulator *regulator;
int i;
if (!config) {
pr_err("rpm-regulator driver has not probed yet.\n");
return ERR_PTR(-ENODEV);
}
if (consumer_map == NULL || consumer_map_len == 0) {
pr_err("No private consumer mapping has been specified.\n");
return ERR_PTR(-ENODEV);
}
if (supply == NULL) {
pr_err("supply name must be specified\n");
return ERR_PTR(-EINVAL);
}
if (dev)
devname = dev_name(dev);
for (i = 0; i < consumer_map_len; i++) {
/* If the mapping has a device set up it must match */
if (consumer_map[i].dev_name &&
(!devname || strncmp(consumer_map[i].dev_name, devname,
MAX_NAME_LEN)))
continue;
if (strncmp(consumer_map[i].supply, supply, MAX_NAME_LEN)
== 0) {
mapping = &consumer_map[i];
break;
}
}
if (mapping == NULL) {
pr_err("could not find mapping for dev=%s, supply=%s\n",
(devname ? devname : "(null)"), supply);
return ERR_PTR(-ENODEV);
}
regulator = kzalloc(sizeof(struct rpm_regulator), GFP_KERNEL);
if (regulator == NULL) {
pr_err("could not allocate memory for regulator\n");
return ERR_PTR(-ENOMEM);
}
regulator->vreg_id = mapping->vreg_id;
regulator->voter = mapping->voter;
regulator->sleep_also = mapping->sleep_also;
return regulator;
}
EXPORT_SYMBOL_GPL(rpm_regulator_get);
static int rpm_regulator_check_input(struct rpm_regulator *regulator)
{
int rc = 0;
if (regulator == NULL) {
rc = -EINVAL;
pr_err("invalid (null) rpm_regulator pointer\n");
} else if (IS_ERR(regulator)) {
rc = PTR_ERR(regulator);
pr_err("invalid rpm_regulator pointer, rc=%d\n", rc);
}
return rc;
}
/**
* rpm_regulator_put() - free the RPM regulator handle
* @regulator: RPM regulator handle
*
* Parameter reaggregation does not take place when rpm_regulator_put is called.
* Therefore, regulator enable state and voltage must be configured
* appropriately before calling rpm_regulator_put.
*
* This function may be called from either atomic or nonatomic context.
*/
void rpm_regulator_put(struct rpm_regulator *regulator)
{
kfree(regulator);
}
EXPORT_SYMBOL_GPL(rpm_regulator_put);
/**
* rpm_regulator_enable() - enable regulator output
* @regulator: RPM regulator handle
*
* Returns 0 on success or errno on failure.
*
* This function may be called from either atomic or nonatomic context. This
* function may only be called for regulators which have the sleep_selectable
* flag set in their configuration data.
*
* rpm_regulator_set_voltage must be called before rpm_regulator_enable because
* enabling is defined by the RPM interface to be requesting the desired
* non-zero regulator output voltage.
*/
int rpm_regulator_enable(struct rpm_regulator *regulator)
{
int rc = rpm_regulator_check_input(regulator);
struct vreg *vreg;
if (rc)
return rc;
if (regulator->vreg_id < config->vreg_id_min
|| regulator->vreg_id > config->vreg_id_max) {
pr_err("invalid regulator id=%d\n", regulator->vreg_id);
return -EINVAL;
}
vreg = &config->vregs[regulator->vreg_id];
/*
* Handle voltage switches which can be enabled without
* rpm_regulator_set_voltage ever being called.
*/
if (regulator->min_uV == 0 && regulator->max_uV == 0
&& vreg->part->uV.mask == 0 && vreg->part->mV.mask == 0) {
regulator->min_uV = 1;
regulator->max_uV = 1;
}
if (regulator->min_uV == 0 && regulator->max_uV == 0) {
pr_err("Voltage must be set with rpm_regulator_set_voltage "
"before calling rpm_regulator_enable; vreg_id=%d, "
"voter=%d\n", regulator->vreg_id, regulator->voter);
return -EINVAL;
}
rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter,
regulator->min_uV, regulator->max_uV, regulator->sleep_also);
if (rc)
pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
return rc;
}
EXPORT_SYMBOL_GPL(rpm_regulator_enable);
/**
* rpm_regulator_disable() - disable regulator output
* @regulator: RPM regulator handle
*
* Returns 0 on success or errno on failure.
*
* The enable state of the regulator is determined by aggregating the requests
* of all consumers. Therefore, it is possible that the regulator will remain
* enabled even after rpm_regulator_disable is called.
*
* This function may be called from either atomic or nonatomic context. This
* function may only be called for regulators which have the sleep_selectable
* flag set in their configuration data.
*/
int rpm_regulator_disable(struct rpm_regulator *regulator)
{
int rc = rpm_regulator_check_input(regulator);
if (rc)
return rc;
rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, 0, 0,
regulator->sleep_also);
if (rc)
pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
return rc;
}
EXPORT_SYMBOL_GPL(rpm_regulator_disable);
/**
* rpm_regulator_set_voltage() - set regulator output voltage
* @regulator: RPM regulator handle
* @min_uV: minimum required voltage in uV
* @max_uV: maximum acceptable voltage in uV
*
* Sets a voltage regulator to the desired output voltage. This can be set
* while the regulator is disabled or enabled. If the regulator is disabled,
* then rpm_regulator_set_voltage will both enable the regulator and set it to
* output at the requested voltage.
*
* The min_uV to max_uV voltage range requested must intersect with the
* voltage constraint range configured for the regulator.
*
* Returns 0 on success or errno on failure.
*
* The final voltage value that is sent to the RPM is aggregated based upon the
* values requested by all consumers of the regulator. This corresponds to the
* maximum min_uV value.
*
* This function may be called from either atomic or nonatomic context. This
* function may only be called for regulators which have the sleep_selectable
* flag set in their configuration data.
*/
int rpm_regulator_set_voltage(struct rpm_regulator *regulator, int min_uV,
int max_uV)
{
int rc = rpm_regulator_check_input(regulator);
if (rc)
return rc;
rc = rpm_vreg_set_voltage(regulator->vreg_id, regulator->voter, min_uV,
max_uV, regulator->sleep_also);
if (rc) {
pr_err("rpm_vreg_set_voltage failed, rc=%d\n", rc);
} else {
regulator->min_uV = min_uV;
regulator->max_uV = max_uV;
}
return rc;
}
EXPORT_SYMBOL_GPL(rpm_regulator_set_voltage);
static inline int vreg_hpm_min_uA(struct vreg *vreg)
{
return vreg->hpm_min_load;
}
static inline int vreg_lpm_max_uA(struct vreg *vreg)
{
return vreg->hpm_min_load - LOAD_THRESHOLD_STEP;
}
static inline unsigned saturate_peak_load(struct vreg *vreg, unsigned load_uA)
{
unsigned load_max
= MILLI_TO_MICRO(vreg->part->ip.mask >> vreg->part->ip.shift);
return (load_uA > load_max ? load_max : load_uA);
}
static inline unsigned saturate_avg_load(struct vreg *vreg, unsigned load_uA)
{
unsigned load_max
= MILLI_TO_MICRO(vreg->part->ia.mask >> vreg->part->ia.shift);
return (load_uA > load_max ? load_max : load_uA);
}
/* Change vreg->req, but do not send it to the RPM. */
static int vreg_store(struct vreg *vreg, unsigned mask0, unsigned val0,
unsigned mask1, unsigned val1)
{
if (vreg->pdata.sleep_selectable)
mutex_lock(&rpm_sleep_sel_lock);
vreg->req[0].value &= ~mask0;
vreg->req[0].value |= val0 & mask0;
vreg->req[1].value &= ~mask1;
vreg->req[1].value |= val1 & mask1;
if (vreg->pdata.sleep_selectable)
mutex_unlock(&rpm_sleep_sel_lock);
return 0;
}
static int vreg_set(struct vreg *vreg, unsigned mask0, unsigned val0,
unsigned mask1, unsigned val1, unsigned cnt)
{
unsigned prev0 = 0, prev1 = 0;
bool tcxo_enabled = false;
bool voltage_increased = false;
int rc;
/*
* Bypass the normal route for regulators that can be called to change
* just the active set values.
*/
if (vreg->pdata.sleep_selectable)
return vreg_set_sleep_sel(vreg, RPM_VREG_VOTER_REG_FRAMEWORK, 1,
mask0, val0, mask1, val1, cnt, 1);
prev0 = vreg->req[0].value;
vreg->req[0].value &= ~mask0;
vreg->req[0].value |= val0 & mask0;
prev1 = vreg->req[1].value;
vreg->req[1].value &= ~mask1;
vreg->req[1].value |= val1 & mask1;
/* Ignore duplicate requests */
if (vreg->req[0].value == vreg->prev_active_req[0].value &&
vreg->req[1].value == vreg->prev_active_req[1].value) {
if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_DUPLICATE)
rpm_regulator_duplicate(vreg, MSM_RPM_CTX_SET_0, cnt);
return 0;
}
/* Enable CXO clock if necessary for TCXO workaround. */
if (requires_tcxo_workaround && vreg->requires_cxo
&& (GET_PART(vreg, uV) > GET_PART_PREV_ACT(vreg, uV))) {
mutex_lock(&tcxo_mutex);
if (!tcxo_handle)
tcxo_get_handle();
voltage_increased = true;
tcxo_enabled = tcxo_enable();
}
rc = msm_rpm_set(MSM_RPM_CTX_SET_0, vreg->req, cnt);
if (rc) {
vreg->req[0].value = prev0;
vreg->req[1].value = prev1;
vreg_err(vreg, "msm_rpm_set failed, set=active, id=%d, rc=%d\n",
vreg->req[0].id, rc);
} else {
if (msm_rpm_vreg_debug_mask & MSM_RPM_VREG_DEBUG_REQUEST)
rpm_regulator_req(vreg, MSM_RPM_CTX_SET_0);
vreg->prev_active_req[0].value = vreg->req[0].value;
vreg->prev_active_req[1].value = vreg->req[1].value;
}
/*
* Schedule CXO clock to be disabled after TCXO warmup time if TCXO
* workaround is applicable for this regulator.
*/
if (voltage_increased) {
if (tcxo_enabled)
tcxo_delayed_disable();
mutex_unlock(&tcxo_mutex);
}
return rc;
}
static int vreg_is_enabled(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
int enabled;
mutex_lock(&vreg->pc_lock);
enabled = vreg->is_enabled;
mutex_unlock(&vreg->pc_lock);
return enabled;
}
static void set_enable(struct vreg *vreg, unsigned int *mask, unsigned int *val)
{
switch (vreg->type) {
case RPM_REGULATOR_TYPE_LDO:
case RPM_REGULATOR_TYPE_SMPS:
case RPM_REGULATOR_TYPE_CORNER:
/* Enable by setting a voltage. */
if (vreg->part->uV.mask) {
val[vreg->part->uV.word]
|= vreg->save_uV << vreg->part->uV.shift;
mask[vreg->part->uV.word] |= vreg->part->uV.mask;
} else {
val[vreg->part->mV.word]
|= MICRO_TO_MILLI(vreg->save_uV)
<< vreg->part->mV.shift;
mask[vreg->part->mV.word] |= vreg->part->mV.mask;
}
break;
case RPM_REGULATOR_TYPE_VS:
case RPM_REGULATOR_TYPE_NCP:
/* Enable by setting enable_state. */
val[vreg->part->enable_state.word]
|= RPM_VREG_STATE_ON << vreg->part->enable_state.shift;
mask[vreg->part->enable_state.word]
|= vreg->part->enable_state.mask;
}
}
static int rpm_vreg_enable(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
unsigned int mask[2] = {0}, val[2] = {0};
int rc = 0;
set_enable(vreg, mask, val);
mutex_lock(&vreg->pc_lock);
rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
vreg->part->request_len);
if (!rc)
vreg->is_enabled = true;
mutex_unlock(&vreg->pc_lock);
if (rc)
vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
return rc;
}
static void set_disable(struct vreg *vreg, unsigned int *mask,
unsigned int *val)
{
switch (vreg->type) {
case RPM_REGULATOR_TYPE_LDO:
case RPM_REGULATOR_TYPE_SMPS:
case RPM_REGULATOR_TYPE_CORNER:
/* Disable by setting a voltage of 0 uV. */
if (vreg->part->uV.mask) {
val[vreg->part->uV.word] |= 0 << vreg->part->uV.shift;
mask[vreg->part->uV.word] |= vreg->part->uV.mask;
} else {
val[vreg->part->mV.word] |= 0 << vreg->part->mV.shift;
mask[vreg->part->mV.word] |= vreg->part->mV.mask;
}
break;
case RPM_REGULATOR_TYPE_VS:
case RPM_REGULATOR_TYPE_NCP:
/* Disable by setting enable_state. */
val[vreg->part->enable_state.word]
|= RPM_VREG_STATE_OFF << vreg->part->enable_state.shift;
mask[vreg->part->enable_state.word]
|= vreg->part->enable_state.mask;
}
}
static int rpm_vreg_disable(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
unsigned int mask[2] = {0}, val[2] = {0};
int rc = 0;
set_disable(vreg, mask, val);
mutex_lock(&vreg->pc_lock);
/* Only disable if pin control is not in use. */
if (!vreg->is_enabled_pc)
rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
vreg->part->request_len);
if (!rc)
vreg->is_enabled = false;
mutex_unlock(&vreg->pc_lock);
if (rc)
vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
return rc;
}
static int vreg_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
unsigned *selector)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
struct vreg_range *range = &vreg->set_points->range[0];
unsigned int mask[2] = {0}, val[2] = {0};
int rc = 0, uV = min_uV;
int lim_min_uV, lim_max_uV, i;
/* Check if request voltage is outside of physically settable range. */
lim_min_uV = vreg->set_points->range[0].min_uV;
lim_max_uV =
vreg->set_points->range[vreg->set_points->count - 1].max_uV;
if (uV < lim_min_uV && max_uV >= lim_min_uV)
uV = lim_min_uV;
if (uV < lim_min_uV || uV > lim_max_uV) {
vreg_err(vreg,
"request v=[%d, %d] is outside possible v=[%d, %d]\n",
min_uV, max_uV, lim_min_uV, lim_max_uV);
return -EINVAL;
}
/* Find the range which uV is inside of. */
for (i = vreg->set_points->count - 1; i > 0; i--) {
if (uV > vreg->set_points->range[i - 1].max_uV) {
range = &vreg->set_points->range[i];
break;
}
}
/*
* Force uV to be an allowed set point and apply a ceiling function
* to non-set point values.
*/
uV = (uV - range->min_uV + range->step_uV - 1) / range->step_uV;
uV = uV * range->step_uV + range->min_uV;
if (uV > max_uV) {
vreg_err(vreg,
"request v=[%d, %d] cannot be met by any set point; "
"next set point: %d\n",
min_uV, max_uV, uV);
return -EINVAL;
}
if (vreg->type == RPM_REGULATOR_TYPE_CORNER) {
/*
* Translate from enum values which work as inputs in the
* regulator_set_voltage function to the actual corner values
* sent to the RPM.
*/
uV -= RPM_VREG_CORNER_NONE;
}
if (vreg->part->uV.mask) {
val[vreg->part->uV.word] = uV << vreg->part->uV.shift;
mask[vreg->part->uV.word] = vreg->part->uV.mask;
} else {
val[vreg->part->mV.word]
= MICRO_TO_MILLI(uV) << vreg->part->mV.shift;
mask[vreg->part->mV.word] = vreg->part->mV.mask;
}
mutex_lock(&vreg->pc_lock);
/*
* Only send a request for a new voltage if the regulator is currently
* enabled. This will ensure that LDO and SMPS regulators are not
* inadvertently turned on because voltage > 0 is equivalent to
* enabling. For NCP, this just removes unnecessary RPM requests.
*/
if (vreg->is_enabled) {
rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
vreg->part->request_len);
if (rc)
vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
} else if (vreg->type == RPM_REGULATOR_TYPE_NCP) {
/* Regulator is disabled; store but don't send new request. */
rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
}
if (!rc && (!vreg->pdata.sleep_selectable || !vreg->is_enabled))
vreg->save_uV = uV;
mutex_unlock(&vreg->pc_lock);
return rc;
}
static int vreg_get_voltage(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
return vreg->save_uV;
}
static int vreg_list_voltage(struct regulator_dev *rdev, unsigned selector)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
int uV = 0;
int i;
if (!vreg->set_points) {
vreg_err(vreg, "no voltages available\n");
return -EINVAL;
}
if (selector >= vreg->set_points->n_voltages)
return 0;
for (i = 0; i < vreg->set_points->count; i++) {
if (selector < vreg->set_points->range[i].n_voltages) {
uV = selector * vreg->set_points->range[i].step_uV
+ vreg->set_points->range[i].min_uV;
break;
} else {
selector -= vreg->set_points->range[i].n_voltages;
}
}
return uV;
}
static int vreg_set_mode(struct regulator_dev *rdev, unsigned int mode)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
unsigned int mask[2] = {0}, val[2] = {0};
int rc = 0;
int peak_uA;
mutex_lock(&vreg->pc_lock);
peak_uA = MILLI_TO_MICRO((vreg->req[vreg->part->ip.word].value
& vreg->part->ip.mask) >> vreg->part->ip.shift);
if (mode == config->mode_hpm) {
/* Make sure that request currents are in HPM range. */
if (peak_uA < vreg_hpm_min_uA(vreg)) {
val[vreg->part->ip.word]
= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
<< vreg->part->ip.shift;
mask[vreg->part->ip.word] = vreg->part->ip.mask;
if (config->ia_follows_ip) {
val[vreg->part->ia.word]
|= MICRO_TO_MILLI(vreg_hpm_min_uA(vreg))
<< vreg->part->ia.shift;
mask[vreg->part->ia.word]
|= vreg->part->ia.mask;
}
}
} else if (mode == config->mode_lpm) {
/* Make sure that request currents are in LPM range. */
if (peak_uA > vreg_lpm_max_uA(vreg)) {
val[vreg->part->ip.word]
= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
<< vreg->part->ip.shift;
mask[vreg->part->ip.word] = vreg->part->ip.mask;
if (config->ia_follows_ip) {
val[vreg->part->ia.word]
|= MICRO_TO_MILLI(vreg_lpm_max_uA(vreg))
<< vreg->part->ia.shift;
mask[vreg->part->ia.word]
|= vreg->part->ia.mask;
}
}
} else {
vreg_err(vreg, "invalid mode: %u\n", mode);
mutex_unlock(&vreg->pc_lock);
return -EINVAL;
}
if (vreg->is_enabled) {
rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
vreg->part->request_len);
} else {
/* Regulator is disabled; store but don't send new request. */
rc = vreg_store(vreg, mask[0], val[0], mask[1], val[1]);
}
if (rc)
vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
else
vreg->mode = mode;
mutex_unlock(&vreg->pc_lock);
return rc;
}
static unsigned int vreg_get_mode(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
return vreg->mode;
}
static unsigned int vreg_get_optimum_mode(struct regulator_dev *rdev,
int input_uV, int output_uV, int load_uA)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
unsigned int mode;
load_uA += vreg->pdata.system_uA;
mutex_lock(&vreg->pc_lock);
SET_PART(vreg, ip, MICRO_TO_MILLI(saturate_peak_load(vreg, load_uA)));
if (config->ia_follows_ip)
SET_PART(vreg, ia,
MICRO_TO_MILLI(saturate_avg_load(vreg, load_uA)));
mutex_unlock(&vreg->pc_lock);
if (load_uA >= vreg->hpm_min_load)
mode = config->mode_hpm;
else
mode = config->mode_lpm;
return mode;
}
static unsigned int vreg_legacy_get_optimum_mode(struct regulator_dev *rdev,
int input_uV, int output_uV, int load_uA)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
if (MICRO_TO_MILLI(load_uA) <= 0) {
/*
* vreg_legacy_get_optimum_mode is being called before consumers
* have specified their load currents via
* regulator_set_optimum_mode. Return whatever the existing mode
* is.
*/
return vreg->mode;
}
return vreg_get_optimum_mode(rdev, input_uV, output_uV, load_uA);
}
/*
* Returns the logical pin control enable state because the pin control options
* present in the hardware out of restart could be different from those desired
* by the consumer.
*/
static int vreg_pin_control_is_enabled(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
return vreg->is_enabled_pc;
}
static int vreg_pin_control_enable(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
unsigned int mask[2] = {0}, val[2] = {0};
int rc;
mutex_lock(&vreg->pc_lock);
val[vreg->part->pc.word]
|= vreg->pdata.pin_ctrl << vreg->part->pc.shift;
mask[vreg->part->pc.word] |= vreg->part->pc.mask;
val[vreg->part->pf.word] |= vreg->pdata.pin_fn << vreg->part->pf.shift;
mask[vreg->part->pf.word] |= vreg->part->pf.mask;
if (!vreg->is_enabled)
set_enable(vreg, mask, val);
rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
vreg->part->request_len);
if (!rc)
vreg->is_enabled_pc = true;
mutex_unlock(&vreg->pc_lock);
if (rc)
vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
return rc;
}
static int vreg_pin_control_disable(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
unsigned int mask[2] = {0}, val[2] = {0};
int pin_fn, rc;
mutex_lock(&vreg->pc_lock);
val[vreg->part->pc.word]
|= RPM_VREG_PIN_CTRL_NONE << vreg->part->pc.shift;
mask[vreg->part->pc.word] |= vreg->part->pc.mask;
pin_fn = config->pin_func_none;
if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
pin_fn = config->pin_func_sleep_b;
val[vreg->part->pf.word] |= pin_fn << vreg->part->pf.shift;
mask[vreg->part->pf.word] |= vreg->part->pf.mask;
if (!vreg->is_enabled)
set_disable(vreg, mask, val);
rc = vreg_set(vreg, mask[0], val[0], mask[1], val[1],
vreg->part->request_len);
if (!rc)
vreg->is_enabled_pc = false;
mutex_unlock(&vreg->pc_lock);
if (rc)
vreg_err(vreg, "vreg_set failed, rc=%d\n", rc);
return rc;
}
static int vreg_enable_time(struct regulator_dev *rdev)
{
struct vreg *vreg = rdev_get_drvdata(rdev);
return vreg->pdata.enable_time;
}
/* Real regulator operations. */
static struct regulator_ops ldo_ops = {
.enable = rpm_vreg_enable,
.disable = rpm_vreg_disable,
.is_enabled = vreg_is_enabled,
.set_voltage = vreg_set_voltage,
.get_voltage = vreg_get_voltage,
.list_voltage = vreg_list_voltage,
.set_mode = vreg_set_mode,
.get_mode = vreg_get_mode,
.get_optimum_mode = vreg_get_optimum_mode,
.enable_time = vreg_enable_time,
};
static struct regulator_ops smps_ops = {
.enable = rpm_vreg_enable,
.disable = rpm_vreg_disable,
.is_enabled = vreg_is_enabled,
.set_voltage = vreg_set_voltage,
.get_voltage = vreg_get_voltage,
.list_voltage = vreg_list_voltage,
.set_mode = vreg_set_mode,
.get_mode = vreg_get_mode,
.get_optimum_mode = vreg_get_optimum_mode,
.enable_time = vreg_enable_time,
};
static struct regulator_ops switch_ops = {
.enable = rpm_vreg_enable,
.disable = rpm_vreg_disable,
.is_enabled = vreg_is_enabled,
.enable_time = vreg_enable_time,
};
static struct regulator_ops ncp_ops = {
.enable = rpm_vreg_enable,
.disable = rpm_vreg_disable,
.is_enabled = vreg_is_enabled,
.set_voltage = vreg_set_voltage,
.get_voltage = vreg_get_voltage,
.list_voltage = vreg_list_voltage,
.enable_time = vreg_enable_time,
};
static struct regulator_ops corner_ops = {
.enable = rpm_vreg_enable,
.disable = rpm_vreg_disable,
.is_enabled = vreg_is_enabled,
.set_voltage = vreg_set_voltage,
.get_voltage = vreg_get_voltage,
.list_voltage = vreg_list_voltage,
.enable_time = vreg_enable_time,
};
/* Pin control regulator operations. */
static struct regulator_ops pin_control_ops = {
.enable = vreg_pin_control_enable,
.disable = vreg_pin_control_disable,
.is_enabled = vreg_pin_control_is_enabled,
};
struct regulator_ops *vreg_ops[] = {
[RPM_REGULATOR_TYPE_LDO] = &ldo_ops,
[RPM_REGULATOR_TYPE_SMPS] = &smps_ops,
[RPM_REGULATOR_TYPE_VS] = &switch_ops,
[RPM_REGULATOR_TYPE_NCP] = &ncp_ops,
[RPM_REGULATOR_TYPE_CORNER] = &corner_ops,
};
static struct vreg *rpm_vreg_get_vreg(int id)
{
struct vreg *vreg;
if (id < config->vreg_id_min || id > config->vreg_id_max)
return NULL;
if (!config->is_real_id(id))
id = config->pc_id_to_real_id(id);
vreg = &config->vregs[id];
return vreg;
}
static int __devinit
rpm_vreg_init_regulator(const struct rpm_regulator_init_data *pdata,
struct device *dev)
{
struct regulator_desc *rdesc = NULL;
struct regulator_dev *rdev;
struct vreg *vreg;
unsigned pin_ctrl;
int pin_fn;
int rc = 0;
if (!pdata) {
pr_err("platform data missing\n");
return -EINVAL;
}
vreg = rpm_vreg_get_vreg(pdata->id);
if (!vreg) {
pr_err("invalid regulator id: %d\n", pdata->id);
return -ENODEV;
}
if (config->is_real_id(pdata->id))
rdesc = &vreg->rdesc;
else
rdesc = &vreg->rdesc_pc;
if (vreg->type < 0 || vreg->type > RPM_REGULATOR_TYPE_MAX) {
pr_err("%s: invalid regulator type: %d\n",
vreg->rdesc.name, vreg->type);
return -EINVAL;
}
mutex_lock(&vreg->pc_lock);
if (vreg->set_points)
rdesc->n_voltages = vreg->set_points->n_voltages;
else
rdesc->n_voltages = 0;
rdesc->id = pdata->id;
rdesc->owner = THIS_MODULE;
rdesc->type = REGULATOR_VOLTAGE;
if (config->is_real_id(pdata->id)) {
/*
* Real regulator; do not modify pin control and pin function
* values.
*/
rdesc->ops = vreg_ops[vreg->type];
pin_ctrl = vreg->pdata.pin_ctrl;
pin_fn = vreg->pdata.pin_fn;
memcpy(&(vreg->pdata), pdata,
sizeof(struct rpm_regulator_init_data));
vreg->pdata.pin_ctrl = pin_ctrl;
vreg->pdata.pin_fn = pin_fn;
vreg->save_uV = vreg->pdata.default_uV;
if (vreg->pdata.peak_uA >= vreg->hpm_min_load)
vreg->mode = config->mode_hpm;
else
vreg->mode = config->mode_lpm;
/* Initialize the RPM request. */
SET_PART(vreg, ip,
MICRO_TO_MILLI(saturate_peak_load(vreg, vreg->pdata.peak_uA)));
SET_PART(vreg, fm, vreg->pdata.force_mode);
SET_PART(vreg, pm, vreg->pdata.power_mode);
SET_PART(vreg, pd, vreg->pdata.pull_down_enable);
SET_PART(vreg, ia,
MICRO_TO_MILLI(saturate_avg_load(vreg, vreg->pdata.avg_uA)));
SET_PART(vreg, freq, vreg->pdata.freq);
SET_PART(vreg, freq_clk_src, 0);
SET_PART(vreg, comp_mode, 0);
SET_PART(vreg, hpm, 0);
if (!vreg->is_enabled_pc) {
SET_PART(vreg, pf, config->pin_func_none);
SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
}
} else {
if ((pdata->pin_ctrl & RPM_VREG_PIN_CTRL_ALL)
== RPM_VREG_PIN_CTRL_NONE
&& pdata->pin_fn != config->pin_func_sleep_b) {
pr_err("%s: no pin control input specified\n",
vreg->rdesc.name);
mutex_unlock(&vreg->pc_lock);
return -EINVAL;
}
rdesc->ops = &pin_control_ops;
vreg->pdata.pin_ctrl = pdata->pin_ctrl;
vreg->pdata.pin_fn = pdata->pin_fn;
/* Initialize the RPM request. */
pin_fn = config->pin_func_none;
/* Allow pf=sleep_b to be specified by platform data. */
if (vreg->pdata.pin_fn == config->pin_func_sleep_b)
pin_fn = config->pin_func_sleep_b;
SET_PART(vreg, pf, pin_fn);
SET_PART(vreg, pc, RPM_VREG_PIN_CTRL_NONE);
}
mutex_unlock(&vreg->pc_lock);
if (rc)
goto bail;
rdev = regulator_register(rdesc, dev, &(pdata->init_data), vreg, NULL);
if (IS_ERR(rdev)) {
rc = PTR_ERR(rdev);
pr_err("regulator_register failed: %s, rc=%d\n",
vreg->rdesc.name, rc);
return rc;
} else {
if (config->is_real_id(pdata->id))
vreg->rdev = rdev;
else
vreg->rdev_pc = rdev;
}
bail:
if (rc)
pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
return rc;
}
static void rpm_vreg_set_point_init(void)
{
struct vreg_set_points **set_points;
int i, j, temp;
set_points = config->set_points;
/* Calculate the number of set points available for each regulator. */
for (i = 0; i < config->set_points_len; i++) {
temp = 0;
for (j = 0; j < set_points[i]->count; j++) {
set_points[i]->range[j].n_voltages
= (set_points[i]->range[j].max_uV
- set_points[i]->range[j].min_uV)
/ set_points[i]->range[j].step_uV + 1;
temp += set_points[i]->range[j].n_voltages;
}
set_points[i]->n_voltages = temp;
}
}
static int __devinit rpm_vreg_probe(struct platform_device *pdev)
{
struct rpm_regulator_platform_data *platform_data;
static struct rpm_regulator_consumer_mapping *prev_consumer_map;
static int prev_consumer_map_len;
int rc = 0;
int i, id;
platform_data = pdev->dev.platform_data;
if (!platform_data) {
pr_err("rpm-regulator requires platform data\n");
return -EINVAL;
}
if (rpm_version >= 0 && rpm_version <= RPM_VREG_VERSION_MAX
&& platform_data->version != rpm_version) {
pr_err("rpm version %d does not match previous version %d\n",
platform_data->version, rpm_version);
return -EINVAL;
}
if (platform_data->version < 0
|| platform_data->version > RPM_VREG_VERSION_MAX) {
pr_err("rpm version %d is invalid\n", platform_data->version);
return -EINVAL;
}
if (rpm_version < 0 || rpm_version > RPM_VREG_VERSION_MAX) {
rpm_version = platform_data->version;
config = get_config[platform_data->version]();
vreg_id_vdd_mem = platform_data->vreg_id_vdd_mem;
vreg_id_vdd_dig = platform_data->vreg_id_vdd_dig;
if (!config) {
pr_err("rpm version %d is not available\n",
platform_data->version);
return -ENODEV;
}
if (config->use_legacy_optimum_mode)
for (i = 0; i < ARRAY_SIZE(vreg_ops); i++)
vreg_ops[i]->get_optimum_mode
= vreg_legacy_get_optimum_mode;
rpm_vreg_set_point_init();
/* First time probed; initialize pin control mutexes. */
for (i = 0; i < config->vregs_len; i++)
mutex_init(&config->vregs[i].pc_lock);
}
/* Copy the list of private API consumers. */
if (platform_data->consumer_map_len > 0) {
if (consumer_map_len == 0) {
consumer_map_len = platform_data->consumer_map_len;
consumer_map = kmemdup(platform_data->consumer_map,
sizeof(struct rpm_regulator_consumer_mapping)
* consumer_map_len, GFP_KERNEL);
if (consumer_map == NULL) {
pr_err("memory allocation failed\n");
consumer_map_len = 0;
return -ENOMEM;
}
} else {
/* Concatenate new map with the existing one. */
prev_consumer_map = consumer_map;
prev_consumer_map_len = consumer_map_len;
consumer_map_len += platform_data->consumer_map_len;
consumer_map = kmalloc(
sizeof(struct rpm_regulator_consumer_mapping)
* consumer_map_len, GFP_KERNEL);
if (consumer_map == NULL) {
pr_err("memory allocation failed\n");
consumer_map_len = 0;
return -ENOMEM;
}
memcpy(consumer_map, prev_consumer_map,
sizeof(struct rpm_regulator_consumer_mapping)
* prev_consumer_map_len);
memcpy(&consumer_map[prev_consumer_map_len],
platform_data->consumer_map,
sizeof(struct rpm_regulator_consumer_mapping)
* platform_data->consumer_map_len);
}
}
if (platform_data->requires_tcxo_workaround
&& !requires_tcxo_workaround) {
requires_tcxo_workaround = true;
wake_lock_init(&tcxo_wake_lock, WAKE_LOCK_SUSPEND,
"rpm_regulator_tcxo");
}
/* Initialize all of the regulators listed in the platform data. */
for (i = 0; i < platform_data->num_regulators; i++) {
rc = rpm_vreg_init_regulator(&platform_data->init_data[i],
&pdev->dev);
if (rc) {
pr_err("rpm_vreg_init_regulator failed, rc=%d\n", rc);
goto remove_regulators;
}
}
platform_set_drvdata(pdev, platform_data);
return rc;
remove_regulators:
/* Unregister all regulators added before the erroring one. */
for (; i >= 0; i--) {
id = platform_data->init_data[i].id;
if (config->is_real_id(id)) {
regulator_unregister(config->vregs[id].rdev);
config->vregs[id].rdev = NULL;
} else {
regulator_unregister(config->vregs[
config->pc_id_to_real_id(id)].rdev_pc);
config->vregs[id].rdev_pc = NULL;
}
}
return rc;
}
static int __devexit rpm_vreg_remove(struct platform_device *pdev)
{
struct rpm_regulator_platform_data *platform_data;
int i, id;
platform_data = platform_get_drvdata(pdev);
platform_set_drvdata(pdev, NULL);
if (platform_data) {
for (i = 0; i < platform_data->num_regulators; i++) {
id = platform_data->init_data[i].id;
if (config->is_real_id(id)) {
regulator_unregister(config->vregs[id].rdev);
config->vregs[id].rdev = NULL;
} else {
regulator_unregister(config->vregs[
config->pc_id_to_real_id(id)].rdev_pc);
config->vregs[id].rdev_pc = NULL;
}
}
}
return 0;
}
static struct platform_driver rpm_vreg_driver = {
.probe = rpm_vreg_probe,
.remove = __devexit_p(rpm_vreg_remove),
.driver = {
.name = RPM_REGULATOR_DEV_NAME,
.owner = THIS_MODULE,
},
};
static int __init rpm_vreg_init(void)
{
return platform_driver_register(&rpm_vreg_driver);
}
static void __exit rpm_vreg_exit(void)
{
int i;
platform_driver_unregister(&rpm_vreg_driver);
kfree(consumer_map);
for (i = 0; i < config->vregs_len; i++)
mutex_destroy(&config->vregs[i].pc_lock);
if (tcxo_handle)
clk_put(tcxo_handle);
}
postcore_initcall(rpm_vreg_init);
module_exit(rpm_vreg_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("MSM RPM regulator driver");
MODULE_VERSION("1.0");
MODULE_ALIAS("platform:" RPM_REGULATOR_DEV_NAME);
|