summaryrefslogtreecommitdiff
path: root/drivers/mxc/pmic/mc13783/pmic_convity.c
blob: 9d89f493a8f295f47b7e25fab95c8ea7b00d5f84 (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
/*
 * Copyright 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file mc13783/pmic_convity.c
 * @brief Implementation of the PMIC Connectivity driver APIs.
 *
 * The PMIC connectivity device driver and this API were developed to support
 * the external connectivity capabilities of several power management ICs that
 * are available from Freescale Semiconductor, Inc.
 *
 * The following operating modes, in terms of external connectivity, are
 * supported:
 *
 * @verbatim
       Operating Mode     mc13783
       ---------------    -------
       USB (incl. OTG)     Yes
       RS-232              Yes
       CEA-936             Yes

   @endverbatim
 *
 * @ingroup PMIC_CONNECTIVITY
 */

#include <linux/interrupt.h>	/* For tasklet interface.                  */
#include <linux/platform_device.h>	/* For kernel module interface.            */
#include <linux/spinlock.h>	/* For spinlock interface.                 */
#include <linux/pmic_adc.h>	/* For PMIC ADC driver interface.          */
#include <linux/pmic_status.h>
#include <mach/pmic_convity.h>	/* For PMIC Connectivity driver interface. */

/*
 * mc13783 Connectivity API
 */
/* EXPORTED FUNCTIONS */
EXPORT_SYMBOL(pmic_convity_open);
EXPORT_SYMBOL(pmic_convity_close);
EXPORT_SYMBOL(pmic_convity_set_mode);
EXPORT_SYMBOL(pmic_convity_get_mode);
EXPORT_SYMBOL(pmic_convity_reset);
EXPORT_SYMBOL(pmic_convity_set_callback);
EXPORT_SYMBOL(pmic_convity_clear_callback);
EXPORT_SYMBOL(pmic_convity_get_callback);
EXPORT_SYMBOL(pmic_convity_usb_set_speed);
EXPORT_SYMBOL(pmic_convity_usb_get_speed);
EXPORT_SYMBOL(pmic_convity_usb_set_power_source);
EXPORT_SYMBOL(pmic_convity_usb_get_power_source);
EXPORT_SYMBOL(pmic_convity_usb_set_xcvr);
EXPORT_SYMBOL(pmic_convity_usb_get_xcvr);
EXPORT_SYMBOL(pmic_convity_usb_otg_set_dlp_duration);
EXPORT_SYMBOL(pmic_convity_usb_otg_get_dlp_duration);
EXPORT_SYMBOL(pmic_convity_usb_otg_set_config);
EXPORT_SYMBOL(pmic_convity_usb_otg_clear_config);
EXPORT_SYMBOL(pmic_convity_usb_otg_get_config);
EXPORT_SYMBOL(pmic_convity_set_output);
EXPORT_SYMBOL(pmic_convity_rs232_set_config);
EXPORT_SYMBOL(pmic_convity_rs232_get_config);
EXPORT_SYMBOL(pmic_convity_cea936_exit_signal);

/*! @def SET_BITS
 * Set a register field to a given value.
 */

#define SET_BITS(reg, field, value)    (((value) << reg.field.offset) & \
		reg.field.mask)

/*! @def GET_BITS
 * Get the current value of a given register field.
 */
#define GET_BITS(reg, value)    (((value) & reg.mask) >> \
		reg.offset)

/*!
 * @brief Define the possible states for a device handle.
 *
 * This enumeration is used to track the current state of each device handle.
 */
typedef enum {
	HANDLE_FREE,		/*!< Handle is available for use. */
	HANDLE_IN_USE		/*!< Handle is currently in use.  */
} HANDLE_STATE;

/*
 * This structure is used to define a specific hardware register field.
 *
 * All hardware register fields are defined using an offset to the LSB
 * and a mask. The offset is used to right shift a register value before
 * applying the mask to actually obtain the value of the field.
 */
typedef struct {
	const unsigned char offset;	/* Offset of LSB of register field.           */
	const unsigned int mask;	/* Mask value used to isolate register field. */
} REGFIELD;

/*!
 * @brief This structure is used to identify the fields in the USBCNTRL_REG_0 hardware register.
 *
 * This structure lists all of the fields within the USBCNTRL_REG_0 hardware
 * register.
 */
typedef struct {
	REGFIELD FSENB;		/*!< USB Full Speed Enable                            */
	REGFIELD USB_SUSPEND;	/*!< USB Suspend Mode Enable                          */
	REGFIELD USB_PU;	/*!< USB Pullup Enable                                */
	REGFIELD UDP_PD;	/*!< USB Data Plus Pulldown Enable                    */
	REGFIELD UDM_PD;	/*!< USB 150K UDP Pullup Enable                       */
	REGFIELD DP150K_PU;	/*!< USB Pullup/Pulldown Override Enable              */
	REGFIELD VBUSPDENB;	/*!< USB VBUS Pulldown NMOS Switch Enable             */
	REGFIELD CURRENT_LIMIT;	/*!< USB Regulator Current Limit Setting-3 bits       */
	REGFIELD DLP_SRP;	/*!< USB Data Line Pulsing Timer Enable               */
	REGFIELD SE0_CONN;	/*!< USB Pullup Connect When SE0 Detected             */
	REGFIELD USBXCVREN;	/*!< USB Transceiver Enabled When INTERFACE_MODE[2:0]=000 and RESETB=high */
	REGFIELD PULLOVR;	/*!< 1K5 Pullup and UDP/UDM Pulldown Disable When UTXENB=Low             */
	REGFIELD INTERFACE_MODE;	/*!< Connectivity Interface Mode Select-3 Bits        */
	REGFIELD DATSE0;	/*!< USB Single or Differential Mode Select           */
	REGFIELD BIDIR;		/*!< USB Unidirectional/Bidirectional Transmission    */
	REGFIELD USBCNTRL;	/*!< USB Mode of Operation controlled By USBEN/SPI Pin */
	REGFIELD IDPD;		/*!< USB UID Pulldown Enable                          */
	REGFIELD IDPULSE;	/*!< USB Pulse to Gnd on UID Line Generated           */
	REGFIELD IDPUCNTRL;	/*!< USB UID Pin pulled high By 5ua Curr Source       */
	REGFIELD DMPULSE;	/*!< USB Positive pulse on the UDM Line Generated     */
} USBCNTRL_REG_0;

/*!
 * @brief This variable is used to access the USBCNTRL_REG_0 hardware register.
 *
 * This variable defines how to access all of the fields within the
 * USBCNTRL_REG_0  hardware register. The initial values consist of the offset
 * and mask values needed to access each of the register fields.
 */
static const USBCNTRL_REG_0 regUSB0 = {
	{0, 0x000001},		/*!< FSENB        */
	{1, 0x000002},		/*!< USB_SUSPEND  */
	{2, 0x000004},		/*!< USB_PU       */
	{3, 0x000008},		/*!< UDP_PD       */
	{4, 0x000010},		/*!< UDM_PD       */
	{5, 0x000020},		/*!< DP150K_PU    */
	{6, 0x000040},		/*!< VBUSPDENB    */
	{7, 0x000380},		/*!< CURRENT_LIMIT */
	{10, 0x000400},		/*!< DLP_SRP      */
	{11, 0x000800},		/*!< SE0_CONN     */
	{12, 0x001000},		/*!< USBXCVREN    */
	{13, 0x002000},		/*!< PULLOVR      */
	{14, 0x01c000},		/*!< INTERFACE_MODE */
	{17, 0x020000},		/*!< DATSE0       */
	{18, 0x040000},		/*!< BIDIR        */
	{19, 0x080000},		/*!< USBCNTRL     */
	{20, 0x100000},		/*!< IDPD         */
	{21, 0x200000},		/*!< IDPULSE      */
	{22, 0x400000},		/*!< IDPUCNTRL    */
	{23, 0x800000}		/*!< DMPULSE      */

};

/*!
 * @brief This structure is used to identify the fields in the USBCNTRL_REG_1 hardware register.
 *
 * This structure lists all of the fields within the USBCNTRL_REG_1 hardware
 * register.
 */
typedef struct {
	REGFIELD VUSBIN;	/*!< Controls The Input Source For VUSB             */
	REGFIELD VUSB;		/*!< VUSB Output Voltage Select-High=3.3V Low=2.775V */
	REGFIELD VUSBEN;	/*!< VUSB Output Enable-                            */
	REGFIELD VBUSEN;	/*!< VBUS Output Enable-                            */
	REGFIELD RSPOL;		/*!< Low=RS232 TX on UDM, RX on UDP
				   High= RS232 TX on UDP, RX on UDM               */
	REGFIELD RSTRI;		/*!< TX Forced To Tristate in RS232 Mode Only       */
	REGFIELD ID100kPU;	/*!< 100k UID Pullup Enabled                        */
} USBCNTRL_REG_1;

/*!
 * @brief This variable is used to access the USBCNTRL_REG_1 hardware register.
 *
 * This variable defines how to access all of the fields within the
 * USBCNTRL_REG_1  hardware register. The initial values consist of the offset
 * and mask values needed to access each of the register fields.
 */
static const USBCNTRL_REG_1 regUSB1 = {
	{0, 0x000003},		/*!< VUSBIN-2 Bits  */
	{2, 0x000004},		/*!< VUSB           */
	{3, 0x000008},		/*!< VUSBEN         */
	/*{4, 0x000010} *//*!< Reserved       */
	{5, 0x000020},		/*!< VBUSEN         */
	{6, 0x000040},		/*!< RSPOL          */
	{7, 0x000080},		/*!< RSTRI          */
	{8, 0x000100}		/*!< ID100kPU       */
	/*!< 9-23 Unused    */
};

/*! Define a mask to access the entire hardware register. */
static const unsigned int REG_FULLMASK = 0xffffff;

/*! Define the mc13783 USBCNTRL_REG_0 register power on reset state. */
static const unsigned int RESET_USBCNTRL_REG_0 = 0x080060;

/*! Define the mc13783 USBCNTRL_REG_1 register power on reset state. */
static const unsigned int RESET_USBCNTRL_REG_1 = 0x000006;

static pmic_event_callback_t eventNotify;

/*!
 * @brief This structure is used to maintain the current device driver state.
 *
 * This structure maintains the current state of the connectivity driver. This
 * includes both the PMIC hardware state as well as the device handle and
 * callback states.
 */

typedef struct {
	PMIC_CONVITY_HANDLE handle;	/*!< Device handle.   */
	HANDLE_STATE handle_state;	/*!< Device handle
					   state.           */
	PMIC_CONVITY_MODE mode;	/*!< Device mode.     */
	PMIC_CONVITY_CALLBACK callback;	/*!< Event callback function pointer. */
	PMIC_CONVITY_EVENTS eventMask;	/*!< Event mask.      */
	PMIC_CONVITY_USB_SPEED usbSpeed;	/*!< USB connection
						   speed.           */
	PMIC_CONVITY_USB_MODE usbMode;	/*!< USB connection
					   mode.            */
	PMIC_CONVITY_USB_POWER_IN usbPowerIn;	/*!< USB transceiver
						   power source.    */
	PMIC_CONVITY_USB_POWER_OUT usbPowerOut;	/*!< USB transceiver
						   power output
						   level.           */
	PMIC_CONVITY_USB_TRANSCEIVER_MODE usbXcvrMode;	/*!< USB transceiver
							   mode.            */
	unsigned int usbDlpDuration;	/*!< USB Data Line
					   Pulsing duration. */
	PMIC_CONVITY_USB_OTG_CONFIG usbOtgCfg;	/*!< USB OTG
						   configuration
						   options.         */
	PMIC_CONVITY_RS232_INTERNAL rs232CfgInternal;	/*!< RS-232 internal
							   connections.     */
	PMIC_CONVITY_RS232_EXTERNAL rs232CfgExternal;	/*!< RS-232 external
							   connections.     */
} pmic_convity_state_struct;

/*!
 * @brief This structure is used to maintain the current device driver state.
 *
 * This structure maintains the current state of the driver in USB mode. This
 * includes both the PMIC hardware state as well as the device handle and
 * callback states.
 */

typedef struct {
	PMIC_CONVITY_HANDLE handle;	/*!< Device handle.   */
	HANDLE_STATE handle_state;	/*!< Device handle
					   state.           */
	PMIC_CONVITY_MODE mode;	/*!< Device mode.     */
	PMIC_CONVITY_CALLBACK callback;	/*!< Event callback function pointer. */
	PMIC_CONVITY_EVENTS eventMask;	/*!< Event mask.      */
	PMIC_CONVITY_USB_SPEED usbSpeed;	/*!< USB connection
						   speed.           */
	PMIC_CONVITY_USB_MODE usbMode;	/*!< USB connection
					   mode.            */
	PMIC_CONVITY_USB_POWER_IN usbPowerIn;	/*!< USB transceiver
						   power source.    */
	PMIC_CONVITY_USB_POWER_OUT usbPowerOut;	/*!< USB transceiver
						   power output
						   level.           */
	PMIC_CONVITY_USB_TRANSCEIVER_MODE usbXcvrMode;	/*!< USB transceiver
							   mode.            */
	unsigned int usbDlpDuration;	/*!< USB Data Line
					   Pulsing duration. */
	PMIC_CONVITY_USB_OTG_CONFIG usbOtgCfg;	/*!< USB OTG
						   configuration
						   options.         */
} pmic_convity_usb_state;

/*!
 * @brief This structure is used to maintain the current device driver state.
 *
 * This structure maintains the current state of the driver in RS_232 mode. This
 * includes both the PMIC hardware state as well as the device handle and
 * callback states.
 */

typedef struct {
	PMIC_CONVITY_HANDLE handle;	/*!< Device handle.   */
	HANDLE_STATE handle_state;	/*!< Device handle
					   state.           */
	PMIC_CONVITY_MODE mode;	/*!< Device mode.     */
	PMIC_CONVITY_CALLBACK callback;	/*!< Event callback function pointer. */
	PMIC_CONVITY_EVENTS eventMask;	/*!< Event mask.      */
	PMIC_CONVITY_RS232_INTERNAL rs232CfgInternal;	/*!< RS-232 internal
							   connections.     */
	PMIC_CONVITY_RS232_EXTERNAL rs232CfgExternal;	/*!< RS-232 external
							   connections.     */
} pmic_convity_rs232_state;

/*!
 * @brief This structure is used to maintain the current device driver state.
 *
 * This structure maintains the current state of the driver in cea-936 mode. This
 * includes both the PMIC hardware state as well as the device handle and
 * callback states.
 */

typedef struct {
	PMIC_CONVITY_HANDLE handle;	/*!< Device handle.   */
	HANDLE_STATE handle_state;	/*!< Device handle
					   state.           */
	PMIC_CONVITY_MODE mode;	/*!< Device mode.     */
	PMIC_CONVITY_CALLBACK callback;	/*!< Event callback function pointer. */
	PMIC_CONVITY_EVENTS eventMask;	/*!< Event mask.      */

} pmic_convity_cea936_state;

/*!
 * @brief Identifies the hardware interrupt source.
 *
 * This enumeration identifies which of the possible hardware interrupt
 * sources actually caused the current interrupt handler to be called.
 */
typedef enum {
	CORE_EVENT_4V4 = 1,	/*!< Detected USB 4.4 V event.              */
	CORE_EVENT_2V0 = 2,	/*!< Detected USB 2.0 V event.              */
	CORE_EVENT_0V8 = 4,	/*!< Detected USB 0.8 V event.              */
	CORE_EVENT_ABDET = 8	/*!< Detected USB mini A-B connector event. */
} PMIC_CORE_EVENT;

/*!
 * @brief This structure defines the reset/power on state for the Connectivity driver.
 */
static const pmic_convity_state_struct reset = {
	0,
	HANDLE_FREE,
	USB,
	NULL,
	0,
	USB_FULL_SPEED,
	USB_PERIPHERAL,
	USB_POWER_INTERNAL,
	USB_POWER_3V3,
	USB_TRANSCEIVER_OFF,
	0,
	USB_PULL_OVERRIDE | USB_VBUS_CURRENT_LIMIT_HIGH,
	RS232_TX_USE0VM_RX_UDATVP,
	RS232_TX_UDM_RX_UDP
};

/*!
 * @brief This structure maintains the current state of the Connectivity driver.
 *
 * The initial values must be identical to the reset state defined by the
 * #reset variable.
 */
static pmic_convity_usb_state usb = {
	0,
	HANDLE_FREE,
	USB,
	NULL,
	0,
	USB_FULL_SPEED,
	USB_PERIPHERAL,
	USB_POWER_INTERNAL,
	USB_POWER_3V3,
	USB_TRANSCEIVER_OFF,
	0,
	USB_PULL_OVERRIDE | USB_VBUS_CURRENT_LIMIT_HIGH,
};

/*!
 * @brief This structure maintains the current state of the Connectivity driver.
 *
 * The initial values must be identical to the reset state defined by the
 * #reset variable.
 */
static pmic_convity_rs232_state rs_232 = {
	0,
	HANDLE_FREE,
	RS232_1,
	NULL,
	0,
	RS232_TX_USE0VM_RX_UDATVP,
	RS232_TX_UDM_RX_UDP
};

/*!
 * @brief This structure maintains the current state of the Connectivity driver.
 *
 * The initial values must be identical to the reset state defined by the
 * #reset variable.
 */
static pmic_convity_cea936_state cea_936 = {
	0,
	HANDLE_FREE,
	CEA936_MONO,
	NULL,
	0,
};

/*!
 * @brief This spinlock is used to provide mutual exclusion.
 *
 * Create a spinlock that can be used to provide mutually exclusive
 * read/write access to the globally accessible "convity" data structure
 * that was defined above. Mutually exclusive access is required to
 * ensure that the convity data structure is consistent at all times
 * when possibly accessed by multiple threads of execution (for example,
 * while simultaneously handling a user request and an interrupt event).
 *
 * We need to use a spinlock sometimes because we need to provide mutual
 * exclusion while handling a hardware interrupt.
 */
static DEFINE_SPINLOCK(lock);

/*!
 * @brief This mutex is used to provide mutual exclusion.
 *
 * Create a mutex that can be used to provide mutually exclusive
 * read/write access to the globally accessible data structures
 * that were defined above. Mutually exclusive access is required to
 * ensure that the Connectivity data structures are consistent at all
 * times when possibly accessed by multiple threads of execution.
 *
 * Note that we use a mutex instead of the spinlock whenever disabling
 * interrupts while in the critical section is not required. This helps
 * to minimize kernel interrupt handling latency.
 */
static DECLARE_MUTEX(mutex);

/* Prototype for the connectivity driver tasklet function. */
static void pmic_convity_tasklet(struct work_struct *work);

/*!
 * @brief Tasklet handler for the connectivity driver.
 *
 * Declare a tasklet that will do most of the processing for all of the
 * connectivity-related interrupt events (USB4.4VI, USB2.0VI, USB0.8VI,
 * and AB_DETI). Note that we cannot do all of the required processing
 * within the interrupt handler itself because we may need to call the
 * ADC driver to measure voltages as well as calling any user-registered
 * callback functions.
 */
DECLARE_WORK(convityTasklet, pmic_convity_tasklet);

/*!
 * @brief Global variable to track currently active interrupt events.
 *
 * This global variable is used to keep track of all of the currently
 * active interrupt events for the connectivity driver. Note that access
 * to this variable may occur while within an interrupt context and,
 * therefore, must be guarded by using a spinlock.
 */
static PMIC_CORE_EVENT eventID;

/* Prototypes for all static connectivity driver functions. */
static PMIC_STATUS pmic_convity_set_mode_internal(const PMIC_CONVITY_MODE mode);
static PMIC_STATUS pmic_convity_deregister_all(void);
static void pmic_convity_event_handler(void *param);

/**************************************************************************
 * General setup and configuration functions.
 **************************************************************************
 */

/*!
 * @name General Setup and Configuration Connectivity APIs
 * Functions for setting up and configuring the connectivity hardware.
 */
/*@{*/

/*!
 * Attempt to open and gain exclusive access to the PMIC connectivity
 * hardware. An initial operating mode must also be specified.
 *
 * If the open request is successful, then a numeric handle is returned
 * and this handle must be used in all subsequent function calls. The
 * same handle must also be used in the pmic_convity_close() call when use
 * of the PMIC connectivity hardware is no longer required.
 *
 * @param       handle          device handle from open() call
 * @param       mode            initial connectivity operating mode
 *
 * @return      PMIC_SUCCESS    if the open request was successful
 */
PMIC_STATUS pmic_convity_open(PMIC_CONVITY_HANDLE * const handle,
			      const PMIC_CONVITY_MODE mode)
{
	PMIC_STATUS rc = PMIC_ERROR;

	if (handle == (PMIC_CONVITY_HANDLE *) NULL) {
		/* Do not dereference a NULL pointer. */
		return PMIC_ERROR;
	}

	/* We only need to acquire a mutex here because the interrupt handler
	 * never modifies the device handle or device handle state. Therefore,
	 * we don't need to worry about conflicts with the interrupt handler
	 * or the need to execute in an interrupt context.
	 *
	 * But we do need a critical section here to avoid problems in case
	 * multiple calls to pmic_convity_open() are made since we can only
	 * allow one of them to succeed.
	 */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	/* Check the current device handle state and acquire the handle if
	 * it is available.
	 */
	if ((usb.handle_state != HANDLE_FREE)
	    && (rs_232.handle_state != HANDLE_FREE)
	    && (cea_936.handle_state != HANDLE_FREE)) {

		/* Cannot open the PMIC connectivity hardware at this time or an invalid
		 * mode was requested.
		 */
		*handle = reset.handle;
	} else {

		if (mode == USB) {
			usb.handle = (PMIC_CONVITY_HANDLE) (&usb);
			usb.handle_state = HANDLE_IN_USE;
		} else if ((mode == RS232_1) || (mode == RS232_2)) {
			rs_232.handle = (PMIC_CONVITY_HANDLE) (&rs_232);
			rs_232.handle_state = HANDLE_IN_USE;
		} else if ((mode == CEA936_STEREO) || (mode == CEA936_MONO)
			   || (mode == CEA936_TEST_LEFT)
			   || (mode == CEA936_TEST_RIGHT)) {
			cea_936.handle = (PMIC_CONVITY_HANDLE) (&cea_936);
			cea_936.handle_state = HANDLE_IN_USE;

		}
		/* Let's begin by acquiring the connectivity device handle. */
		/* Then we can try to set the desired operating mode. */
		rc = pmic_convity_set_mode_internal(mode);

		if (rc == PMIC_SUCCESS) {
			/* Successfully set the desired operating mode, now return the
			 * handle to the caller.
			 */
			if (mode == USB) {
				*handle = usb.handle;
			} else if ((mode == RS232_1) || (mode == RS232_2)) {
				*handle = rs_232.handle;
			} else if ((mode == CEA936_STEREO)
				   || (mode == CEA936_MONO)
				   || (mode == CEA936_TEST_LEFT)
				   || (mode == CEA936_TEST_RIGHT)) {
				*handle = cea_936.handle;
			}
		} else {
			/* Failed to set the desired mode, return the handle to an unused
			 * state.
			 */
			if (mode == USB) {
				usb.handle = reset.handle;
				usb.handle_state = reset.handle_state;
			} else if ((mode == RS232_1) || (mode == RS232_2)) {
				rs_232.handle = reset.handle;
				rs_232.handle_state = reset.handle_state;
			} else if ((mode == CEA936_STEREO)
				   || (mode == CEA936_MONO)
				   || (mode == CEA936_TEST_LEFT)
				   || (mode == CEA936_TEST_RIGHT)) {
				cea_936.handle = reset.handle;
				cea_936.handle_state = reset.handle_state;
			}

			*handle = reset.handle;
		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Terminate further access to the PMIC connectivity hardware. Also allows
 * another process to call pmic_convity_open() to gain access.
 *
 * @param       handle          device handle from open() call
 *
 * @return      PMIC_SUCCESS    if the close request was successful
 */
PMIC_STATUS pmic_convity_close(const PMIC_CONVITY_HANDLE handle)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Begin a critical section here to avoid the possibility of race
	 * conditions if multiple threads happen to call this function and
	 * pmic_convity_open() at the same time.
	 */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	/* Confirm that the device handle matches the one assigned in the
	 * pmic_convity_open() call and then close the connection.
	 */
	if (((handle == usb.handle) &&
	     (usb.handle_state == HANDLE_IN_USE)) || ((handle == rs_232.handle)
						      && (rs_232.handle_state ==
							  HANDLE_IN_USE))
	    || ((handle == cea_936.handle)
		&& (cea_936.handle_state == HANDLE_IN_USE))) {
		rc = PMIC_SUCCESS;

		/* Deregister for all existing callbacks if necessary and make sure
		 * that the event handling settings are consistent following the
		 * close operation.
		 */
		if ((usb.callback != reset.callback)
		    || (rs_232.callback != reset.callback)
		    || (cea_936.callback != reset.callback)) {
			/* Deregister the existing callback function and all registered
			 * events before we completely close the handle.
			 */
			rc = pmic_convity_deregister_all();
			if (rc == PMIC_SUCCESS) {

			} else if (usb.eventMask != reset.eventMask) {
				/* Having a non-zero eventMask without a callback function being
				 * defined should never occur but let's just make sure here that
				 * we keep things consistent.
				 */
				usb.eventMask = reset.eventMask;
				/* Mark the connectivity device handle as being closed. */
				usb.handle = reset.handle;
				usb.handle_state = reset.handle_state;

			} else if (rs_232.eventMask != reset.eventMask) {

				rs_232.eventMask = reset.eventMask;
				/* Mark the connectivity device handle as being closed. */
				rs_232.handle = reset.handle;
				rs_232.handle_state = reset.handle_state;

			} else if (cea_936.eventMask != reset.eventMask) {
				cea_936.eventMask = reset.eventMask;
				/* Mark the connectivity device handle as being closed. */
				cea_936.handle = reset.handle;
				cea_936.handle_state = reset.handle_state;

			}

		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Change the current operating mode of the PMIC connectivity hardware.
 * The available connectivity operating modes is hardware dependent and
 * consists of one or more of the following: USB (including USB On-the-Go),
 * RS-232, and CEA-936. Requesting an operating mode that is not supported
 * by the PMIC hardware will return PMIC_NOT_SUPPORTED.
 *
 * @param       handle          device handle from
					open() call
 * @param       mode            desired operating mode
 *
 * @return      PMIC_SUCCESS    if the requested mode was successfully set
 */
PMIC_STATUS pmic_convity_set_mode(const PMIC_CONVITY_HANDLE handle,
				  const PMIC_CONVITY_MODE mode)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if (((handle == usb.handle) &&
	     (usb.handle_state == HANDLE_IN_USE)) || ((handle == rs_232.handle)
						      && (rs_232.handle_state ==
							  HANDLE_IN_USE))
	    || ((handle == cea_936.handle)
		&& (cea_936.handle_state == HANDLE_IN_USE))) {
		rc = pmic_convity_set_mode_internal(mode);
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Get the current operating mode for the PMIC connectivity hardware.
 *
 * @param       handle          device handle from open() call
 * @param       mode            the current PMIC connectivity operating mode
 *
 * @return      PMIC_SUCCESS    if the requested mode was successfully set
 */
PMIC_STATUS pmic_convity_get_mode(const PMIC_CONVITY_HANDLE handle,
				  PMIC_CONVITY_MODE * const mode)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((((handle == usb.handle) &&
	      (usb.handle_state == HANDLE_IN_USE)) || ((handle == rs_232.handle)
						       && (rs_232.
							   handle_state ==
							   HANDLE_IN_USE))
	     || ((handle == cea_936.handle)
		 && (cea_936.handle_state == HANDLE_IN_USE)))
	    && (mode != (PMIC_CONVITY_MODE *) NULL)) {

		*mode = usb.mode;

		rc = PMIC_SUCCESS;
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Restore all registers to the initial power-on/reset state.
 *
 * @param       handle          device handle from open() call
 *
 * @return      PMIC_SUCCESS    if the reset was successful
 */
PMIC_STATUS pmic_convity_reset(const PMIC_CONVITY_HANDLE handle)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;
	if (((handle == usb.handle) &&
	     (usb.handle_state == HANDLE_IN_USE)) || ((handle == rs_232.handle)
						      && (rs_232.handle_state ==
							  HANDLE_IN_USE))
	    || ((handle == cea_936.handle)
		&& (cea_936.handle_state == HANDLE_IN_USE))) {

		/* Reset the PMIC Connectivity register to it's power on state. */
		rc = pmic_write_reg(REG_USB, RESET_USBCNTRL_REG_0,
				    REG_FULLMASK);

		rc = pmic_write_reg(REG_CHARGE_USB_SPARE,
				    RESET_USBCNTRL_REG_1, REG_FULLMASK);

		if (rc == PMIC_SUCCESS) {
			/* Also reset the device driver state data structure. */

		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Register a callback function that will be used to signal PMIC connectivity
 * events. For example, the USB subsystem should register a callback function
 * in order to be notified of device connect/disconnect events. Note, however,
 * that non-USB events may also be signalled depending upon the PMIC hardware
 * capabilities. Therefore, the callback function must be able to properly
 * handle all of the possible events if support for non-USB peripherals is
 * also to be included.
 *
 * @param       handle          device handle from open() call
 * @param       func            a pointer to the callback function
 * @param       eventMask       a mask selecting events to be notified
 *
 * @return      PMIC_SUCCESS    if the callback was successful registered
 */
PMIC_STATUS pmic_convity_set_callback(const PMIC_CONVITY_HANDLE handle,
				      const PMIC_CONVITY_CALLBACK func,
				      const PMIC_CONVITY_EVENTS eventMask)
{
	unsigned long flags;
	PMIC_STATUS rc = PMIC_ERROR;

	/* We need to start a critical section here to ensure a consistent state
	 * in case simultaneous calls to pmic_convity_set_callback() are made. In
	 * that case, we must serialize the calls to ensure that the "callback"
	 * and "eventMask" state variables are always consistent.
	 *
	 * Note that we don't actually need to acquire the spinlock until later
	 * when we are finally ready to update the "callback" and "eventMask"
	 * state variables which are shared with the interrupt handler.
	 */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) && (usb.handle_state == HANDLE_IN_USE)) {

		/* Return an error if either the callback function or event mask
		 * is not properly defined.
		 *
		 * It is also considered an error if a callback function has already
		 * been defined. If you wish to register for a new set of events,
		 * then you must first call pmic_convity_clear_callback() to
		 * deregister the existing callback function and list of events
		 * before trying to register a new callback function.
		 */
		if ((func == NULL) || (eventMask == 0)
		    || (usb.callback != NULL)) {
			rc = PMIC_ERROR;

			/* Register for PMIC events from the core protocol driver. */
		} else {

			if ((eventMask & USB_DETECT_4V4_RISE) ||
			    (eventMask & USB_DETECT_4V4_FALL)) {
				/* We need to register for the 4.4V interrupt.
				 EVENT_USBI or EVENT_USB_44VI */
				eventNotify.func = pmic_convity_event_handler;
				eventNotify.param = (void *)(CORE_EVENT_4V4);
				rc = pmic_event_subscribe(EVENT_USBI,
							  eventNotify);

				if (rc != PMIC_SUCCESS) {
					return rc;
				}
			}

			if ((eventMask & USB_DETECT_2V0_RISE) ||
			    (eventMask & USB_DETECT_2V0_FALL)) {
				/* We need to register for the 2.0V interrupt.
				 EVENT_USB_20VI or EVENT_USBI */
				eventNotify.func = pmic_convity_event_handler;
				eventNotify.param = (void *)(CORE_EVENT_2V0);
				rc = pmic_event_subscribe(EVENT_USBI,
							  eventNotify);

				if (rc != PMIC_SUCCESS) {
					goto Cleanup_4V4;
				}
			}

			if ((eventMask & USB_DETECT_0V8_RISE) ||
			    (eventMask & USB_DETECT_0V8_FALL)) {
				/* We need to register for the 0.8V interrupt.
				 EVENT_USB_08VI or EVENT_USBI */
				eventNotify.func = pmic_convity_event_handler;
				eventNotify.param = (void *)(CORE_EVENT_0V8);
				rc = pmic_event_subscribe(EVENT_USBI,
							  eventNotify);

				if (rc != PMIC_SUCCESS) {
					goto Cleanup_2V0;
				}
			}

			if ((eventMask & USB_DETECT_MINI_A) ||
			    (eventMask & USB_DETECT_MINI_B)
			    || (eventMask & USB_DETECT_NON_USB_ACCESSORY)
			    || (eventMask & USB_DETECT_FACTORY_MODE)) {
				/* We need to register for the AB_DET interrupt.
				 EVENT_AB_DETI or EVENT_IDI */
				eventNotify.func = pmic_convity_event_handler;
				eventNotify.param = (void *)(CORE_EVENT_ABDET);
				rc = pmic_event_subscribe(EVENT_IDI,
							  eventNotify);

				if (rc != PMIC_SUCCESS) {
					goto Cleanup_0V8;
				}
			}

			/* Use a critical section to maintain a consistent state. */
			spin_lock_irqsave(&lock, flags);

			/* Successfully registered for all events. */
			usb.callback = func;
			usb.eventMask = eventMask;
			spin_unlock_irqrestore(&lock, flags);

			goto End;

			/* This section unregisters any already registered events if we should
			 * encounter an error partway through the registration process. Note
			 * that we don't check the return status here since it is already set
			 * to PMIC_ERROR before we get here.
			 */
		      Cleanup_0V8:

			if ((eventMask & USB_DETECT_0V8_RISE) ||
			    (eventMask & USB_DETECT_0V8_FALL)) {
				/* EVENT_USB_08VI or EVENT_USBI */
				eventNotify.func = pmic_convity_event_handler;
				eventNotify.param = (void *)(CORE_EVENT_0V8);
				pmic_event_unsubscribe(EVENT_USBI, eventNotify);
				goto End;
			}

		      Cleanup_2V0:

			if ((eventMask & USB_DETECT_2V0_RISE) ||
			    (eventMask & USB_DETECT_2V0_FALL)) {
				/* EVENT_USB_20VI or EVENT_USBI */
				eventNotify.func = pmic_convity_event_handler;
				eventNotify.param = (void *)(CORE_EVENT_2V0);
				pmic_event_unsubscribe(EVENT_USBI, eventNotify);
				goto End;
			}

		      Cleanup_4V4:

			if ((eventMask & USB_DETECT_4V4_RISE) ||
			    (eventMask & USB_DETECT_4V4_FALL)) {
				/* EVENT_USB_44VI or EVENT_USBI */
				eventNotify.func = pmic_convity_event_handler;
				eventNotify.param = (void *)(CORE_EVENT_4V4);
				pmic_event_unsubscribe(EVENT_USBI, eventNotify);
			}
		}
		/* Exit the critical section. */

	}
      End:up(&mutex);
	return rc;

}

/*!
 * Clears the current callback function. If this function returns successfully
 * then all future Connectivity events will only be handled by the default
 * handler within the Connectivity driver.
 *
 * @param       handle          device handle from open() call
 *
 * @return      PMIC_SUCCESS    if the callback was successful cleared
 */
PMIC_STATUS pmic_convity_clear_callback(const PMIC_CONVITY_HANDLE handle)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;
	if (((handle == usb.handle) &&
	     (usb.handle_state == HANDLE_IN_USE)) || ((handle == rs_232.handle)
						      && (rs_232.handle_state ==
							  HANDLE_IN_USE))
	    || ((handle == cea_936.handle)
		&& (cea_936.handle_state == HANDLE_IN_USE))) {

		rc = pmic_convity_deregister_all();
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Get the current callback function and event mask.
 *
 * @param       handle          device handle from open() call
 * @param       func            the current callback function
 * @param       eventMask       the current event selection mask
 *
 * @return      PMIC_SUCCESS    if the callback information was successful
 *                              retrieved
 */
PMIC_STATUS pmic_convity_get_callback(const PMIC_CONVITY_HANDLE handle,
				      PMIC_CONVITY_CALLBACK * const func,
				      PMIC_CONVITY_EVENTS * const eventMask)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;
	if ((((handle == usb.handle) &&
	      (usb.handle_state == HANDLE_IN_USE)) || ((handle == rs_232.handle)
						       && (rs_232.
							   handle_state ==
							   HANDLE_IN_USE))
	     || ((handle == cea_936.handle)
		 && (cea_936.handle_state == HANDLE_IN_USE)))
	    && (func != (PMIC_CONVITY_CALLBACK *) NULL)
	    && (eventMask != (PMIC_CONVITY_EVENTS *) NULL)) {
		*func = usb.callback;
		*eventMask = usb.eventMask;

		rc = PMIC_SUCCESS;
	}

	/* Exit the critical section. */

	up(&mutex);

	return rc;
}

/*@*/

/**************************************************************************
 * USB-specific configuration and setup functions.
 **************************************************************************
 */

/*!
 * @name USB and USB-OTG Connectivity APIs
 * Functions for controlling USB and USB-OTG connectivity.
 */
/*@{*/

/*!
 * Set the USB transceiver speed.
 *
 * @param       handle          device handle from open() call
 * @param       speed           the desired USB transceiver speed
 *
 * @return      PMIC_SUCCESS    if the transceiver speed was successfully set
 */
PMIC_STATUS pmic_convity_usb_set_speed(const PMIC_CONVITY_HANDLE handle,
				       const PMIC_CONVITY_USB_SPEED speed)
{
	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value = 0;
	unsigned int reg_mask = SET_BITS(regUSB0, FSENB, 1);

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if (handle == (rs_232.handle || cea_936.handle)) {
		return PMIC_PARAMETER_ERROR;
	} else {
		if ((handle == usb.handle) &&
		    (usb.handle_state == HANDLE_IN_USE)) {
			/* Validate the function parameters and if they are valid, then
			 * configure the pull-up and pull-down resistors as required for
			 * the desired operating mode.
			 */
			if ((speed == USB_HIGH_SPEED)) {
				/*
				 * The USB transceiver also does not support the high speed mode
				 * (which is also optional under the USB OTG specification).
				 */
				rc = PMIC_NOT_SUPPORTED;
			} else if ((speed != USB_LOW_SPEED)
				   && (speed != USB_FULL_SPEED)) {
				/* Final validity check on the speed parameter. */
				rc = PMIC_ERROR;;
			} else {
				/* First configure the D+ and D- pull-up/pull-down resistors as
				 * per the USB OTG specification.
				 */
				if (speed == USB_FULL_SPEED) {
					/* Activate pull-up on D+ and pull-down on D-. */
					reg_value =
					    SET_BITS(regUSB0, UDM_PD, 1);
				} else if (speed == USB_LOW_SPEED) {
					/* Activate pull-up on D+ and pull-down on D-. */
					reg_value = SET_BITS(regUSB0, FSENB, 1);
				}

				/* Now set the desired USB transceiver speed. Note that
				 * USB_FULL_SPEED simply requires FSENB=0 (which it
				 * already is).
				 */

				rc = pmic_write_reg(REG_USB, reg_value,
						    reg_mask);

				if (rc == PMIC_SUCCESS) {
					usb.usbSpeed = speed;
				}
			}
		}
	}
	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Get the USB transceiver speed.
 *
 * @param       handle          device handle from open() call
 * @param       speed           the current USB transceiver speed
 * @param       mode            the current USB transceiver mode
 *
 * @return      PMIC_SUCCESS    if the transceiver speed was successfully
 *                              obtained
 */
PMIC_STATUS pmic_convity_usb_get_speed(const PMIC_CONVITY_HANDLE handle,
				       PMIC_CONVITY_USB_SPEED * const speed,
				       PMIC_CONVITY_USB_MODE * const mode)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) &&
	    (usb.handle_state == HANDLE_IN_USE) &&
	    (speed != (PMIC_CONVITY_USB_SPEED *) NULL) &&
	    (mode != (PMIC_CONVITY_USB_MODE *) NULL)) {
		*speed = usb.usbSpeed;
		*mode = usb.usbMode;

		rc = PMIC_SUCCESS;
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * This function enables/disables VUSB and VBUS output.
 * This API configures the VUSBEN and VBUSEN bits of USB register
 *
 * @param       handle          device handle from open() call
 * @param        out_type true, for VBUS
 *                        false, for VUSB
 * @param        out 	if true, output is enabled
 *                      if false, output is disabled
 *
 * @return       This function returns PMIC_SUCCESS if successful.
 */
PMIC_STATUS pmic_convity_set_output(const PMIC_CONVITY_HANDLE handle,
				    bool out_type, bool out)
{

	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value = 0;

	unsigned int reg_mask = 0;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) && (usb.handle_state == HANDLE_IN_USE)) {

		if ((out_type == 0) && (out == 1)) {

			reg_value = SET_BITS(regUSB1, VUSBEN, 1);
			reg_mask = SET_BITS(regUSB1, VUSBEN, 1);

			rc = pmic_write_reg(REG_CHARGE_USB_SPARE,
					    reg_value, reg_mask);
		} else if (out_type == 0 && out == 0) {
			reg_mask = SET_BITS(regUSB1, VBUSEN, 1);

			rc = pmic_write_reg(REG_CHARGE_USB_SPARE,
					    reg_value, reg_mask);
		} else if (out_type == 1 && out == 1) {

			reg_value = SET_BITS(regUSB1, VBUSEN, 1);
			reg_mask = SET_BITS(regUSB1, VBUSEN, 1);

			rc = pmic_write_reg(REG_CHARGE_USB_SPARE,
					    reg_value, reg_mask);
		}

		else if (out_type == 1 && out == 0) {

			reg_mask = SET_BITS(regUSB1, VBUSEN, 1);
			rc = pmic_write_reg(REG_CHARGE_USB_SPARE,
					    reg_value, reg_mask);
		}

		/*else {

		   rc = pmic_write_reg(REG_CHARGE_USB_SPARE,
		   reg_value, reg_mask);
		   } */
	}

	up(&mutex);

	return rc;
}

/*!
 * Set the USB transceiver's power supply configuration.
 *
 * @param       handle          device handle from open() call
 * @param       pwrin           USB transceiver regulator input power source
 * @param       pwrout          USB transceiver regulator output power level
 *
 * @return      PMIC_SUCCESS    if the USB transceiver's power supply
 *                              configuration was successfully set
 */
PMIC_STATUS pmic_convity_usb_set_power_source(const PMIC_CONVITY_HANDLE handle,
					      const PMIC_CONVITY_USB_POWER_IN
					      pwrin,
					      const PMIC_CONVITY_USB_POWER_OUT
					      pwrout)
{
	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value = 0;
	unsigned int reg_mask = 0;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) && (usb.handle_state == HANDLE_IN_USE)) {

		if (pwrin == USB_POWER_INTERNAL_BOOST) {
			reg_value |= SET_BITS(regUSB1, VUSBIN, 0);
			reg_mask = SET_BITS(regUSB1, VUSBIN, 1);
		} else if (pwrin == USB_POWER_VBUS) {
			reg_value |= SET_BITS(regUSB1, VUSBIN, 1);
			reg_mask = SET_BITS(regUSB1, VUSBIN, 1);
		}

		else if (pwrin == USB_POWER_INTERNAL) {
			reg_value |= SET_BITS(regUSB1, VUSBIN, 2);
			reg_mask = SET_BITS(regUSB1, VUSBIN, 1);
		}

		if (pwrout == USB_POWER_3V3) {
			reg_value |= SET_BITS(regUSB1, VUSB, 1);
			reg_mask |= SET_BITS(regUSB1, VUSB, 1);
		}

		else if (pwrout == USB_POWER_2V775) {
			reg_value |= SET_BITS(regUSB1, VUSB, 0);
			reg_mask |= SET_BITS(regUSB1, VUSB, 1);
		}
		rc = pmic_write_reg(REG_CHARGE_USB_SPARE, reg_value, reg_mask);

		if (rc == PMIC_SUCCESS) {
			usb.usbPowerIn = pwrin;
			usb.usbPowerOut = pwrout;
		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Get the USB transceiver's current power supply configuration.
 *
 * @param       handle          device handle from open() call
 * @param       pwrin           USB transceiver regulator input power source
 * @param       pwrout          USB transceiver regulator output power level
 *
 * @return      PMIC_SUCCESS    if the USB transceiver's power supply
 *                              configuration was successfully retrieved
 */
PMIC_STATUS pmic_convity_usb_get_power_source(const PMIC_CONVITY_HANDLE handle,
					      PMIC_CONVITY_USB_POWER_IN *
					      const pwrin,
					      PMIC_CONVITY_USB_POWER_OUT *
					      const pwrout)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) &&
	    (usb.handle_state == HANDLE_IN_USE) &&
	    (pwrin != (PMIC_CONVITY_USB_POWER_IN *) NULL) &&
	    (pwrout != (PMIC_CONVITY_USB_POWER_OUT *) NULL)) {
		*pwrin = usb.usbPowerIn;
		*pwrout = usb.usbPowerOut;

		rc = PMIC_SUCCESS;
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Set the USB transceiver's operating mode.
 *
 * @param       handle          device handle from open() call
 * @param       mode            desired operating mode
 *
 * @return      PMIC_SUCCESS    if the USB transceiver's operating mode
 *                              was successfully configured
 */
PMIC_STATUS pmic_convity_usb_set_xcvr(const PMIC_CONVITY_HANDLE handle,
				      const PMIC_CONVITY_USB_TRANSCEIVER_MODE
				      mode)
{
	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value = 0;
	unsigned int reg_mask = 0;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) && (usb.handle_state == HANDLE_IN_USE)) {

		if (mode == USB_TRANSCEIVER_OFF) {
			reg_value = SET_BITS(regUSB0, USBXCVREN, 0);
			reg_mask |= SET_BITS(regUSB0, USB_SUSPEND, 1);

			rc = pmic_write_reg(REG_USB, reg_value, reg_mask);

		}

		if (mode == USB_SINGLE_ENDED_UNIDIR) {
			reg_value |=
			    SET_BITS(regUSB0, DATSE0, 1) | SET_BITS(regUSB0,
								    BIDIR, 0);
			reg_mask |=
			    SET_BITS(regUSB0, USB_SUSPEND,
				     1) | SET_BITS(regUSB0, DATSE0,
						   1) | SET_BITS(regUSB0, BIDIR,
								 1);
		} else if (mode == USB_SINGLE_ENDED_BIDIR) {
			reg_value |=
			    SET_BITS(regUSB0, DATSE0, 1) | SET_BITS(regUSB0,
								    BIDIR, 1);
			reg_mask |=
			    SET_BITS(regUSB0, USB_SUSPEND,
				     1) | SET_BITS(regUSB0, DATSE0,
						   1) | SET_BITS(regUSB0, BIDIR,
								 1);
		} else if (mode == USB_DIFFERENTIAL_UNIDIR) {
			reg_value |=
			    SET_BITS(regUSB0, DATSE0, 0) | SET_BITS(regUSB0,
								    BIDIR, 0);
			reg_mask |=
			    SET_BITS(regUSB0, USB_SUSPEND,
				     1) | SET_BITS(regUSB0, DATSE0,
						   1) | SET_BITS(regUSB0, BIDIR,
								 1);
		} else if (mode == USB_DIFFERENTIAL_BIDIR) {
			reg_value |=
			    SET_BITS(regUSB0, DATSE0, 0) | SET_BITS(regUSB0,
								    BIDIR, 1);
			reg_mask |=
			    SET_BITS(regUSB0, USB_SUSPEND,
				     1) | SET_BITS(regUSB0, DATSE0,
						   1) | SET_BITS(regUSB0, BIDIR,
								 1);
		}

		if (mode == USB_SUSPEND_ON) {
			reg_value |= SET_BITS(regUSB0, USB_SUSPEND, 1);
			reg_mask |= SET_BITS(regUSB0, USB_SUSPEND, 1);
		} else if (mode == USB_SUSPEND_OFF) {
			reg_value |= SET_BITS(regUSB0, USB_SUSPEND, 0);
			reg_mask |= SET_BITS(regUSB0, USB_SUSPEND, 1);
		}

		if (mode == USB_OTG_SRP_DLP_START) {
			reg_value |= SET_BITS(regUSB0, USB_PU, 0);
			reg_mask |= SET_BITS(regUSB0, USB_SUSPEND, 1);
		} else if (mode == USB_OTG_SRP_DLP_STOP) {
			reg_value &= SET_BITS(regUSB0, USB_PU, 1);
			reg_mask |= SET_BITS(regUSB0, USB_PU, 1);
		}

		rc = pmic_write_reg(REG_USB, reg_value, reg_mask);

		if (rc == PMIC_SUCCESS) {
			usb.usbXcvrMode = mode;
		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Get the USB transceiver's current operating mode.
 *
 * @param       handle          device handle from open() call
 * @param       mode            current operating mode
 *
 * @return      PMIC_SUCCESS    if the USB transceiver's operating mode
 *                              was successfully retrieved
 */
PMIC_STATUS pmic_convity_usb_get_xcvr(const PMIC_CONVITY_HANDLE handle,
				      PMIC_CONVITY_USB_TRANSCEIVER_MODE *
				      const mode)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) &&
	    (usb.handle_state == HANDLE_IN_USE) &&
	    (mode != (PMIC_CONVITY_USB_TRANSCEIVER_MODE *) NULL)) {
		*mode = usb.usbXcvrMode;

		rc = PMIC_SUCCESS;
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Set the Data Line Pulse duration (in milliseconds) for the USB OTG
 * Session Request Protocol.
 *
 * For mc13783, this feature is not supported.So return PMIC_NOT_SUPPORTED
 *
 * @param       handle          device handle from open() call
 * @param       duration        the data line pulse duration (ms)
 *
 * @return      PMIC_SUCCESS    if the pulse duration was successfully set
 */
PMIC_STATUS pmic_convity_usb_otg_set_dlp_duration(const PMIC_CONVITY_HANDLE
						  handle,
						  const unsigned int duration)
{
	PMIC_STATUS rc = PMIC_NOT_SUPPORTED;

	/* The setting of the dlp duration is not supported by the mc13783 PMIC hardware. */

	/* No critical section is required. */

	if ((handle != usb.handle)
	    || (usb.handle_state != HANDLE_IN_USE)) {
		/* Must return error indication for invalid handle parameter to be
		 * consistent with other APIs.
		 */
		rc = PMIC_ERROR;
	}

	return rc;
}

/*!
 * Get the current Data Line Pulse duration (in milliseconds) for the USB
 * OTG Session Request Protocol.
 *
 * @param       handle          device handle from open() call
 * @param       duration        the data line pulse duration (ms)
 *
 * @return      PMIC_SUCCESS    if the pulse duration was successfully obtained
 */
PMIC_STATUS pmic_convity_usb_otg_get_dlp_duration(const PMIC_CONVITY_HANDLE
						  handle,
						  unsigned int *const duration)
{
	PMIC_STATUS rc = PMIC_NOT_SUPPORTED;

	/* The setting of dlp duration is not supported by the mc13783 PMIC hardware. */

	/* No critical section is required. */

	if ((handle != usb.handle)
	    || (usb.handle_state != HANDLE_IN_USE)) {
		/* Must return error indication for invalid handle parameter to be
		 * consistent with other APIs.
		 */
		rc = PMIC_ERROR;
	}

	return rc;
}

/*!
 * Set the USB On-The-Go (OTG) configuration.
 *
 * @param       handle          device handle from open() call
 * @param       cfg             desired USB OTG configuration
 *
 * @return      PMIC_SUCCESS    if the OTG configuration was successfully set
 */
PMIC_STATUS pmic_convity_usb_otg_set_config(const PMIC_CONVITY_HANDLE handle,
					    const PMIC_CONVITY_USB_OTG_CONFIG
					    cfg)
{
	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value = 0;
	unsigned int reg_mask = 0;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) && (usb.handle_state == HANDLE_IN_USE)) {
		if (cfg & USB_OTG_SE0CONN) {
			reg_value = SET_BITS(regUSB0, SE0_CONN, 1);
			reg_mask = SET_BITS(regUSB0, SE0_CONN, 1);
		}
		if (cfg & USBXCVREN) {
			reg_value |= SET_BITS(regUSB0, USBXCVREN, 1);
			reg_mask |= SET_BITS(regUSB0, USBXCVREN, 1);
		}

		if (cfg & USB_OTG_DLP_SRP) {
			reg_value |= SET_BITS(regUSB0, DLP_SRP, 1);
			reg_mask |= SET_BITS(regUSB0, DLP_SRP, 1);
		}

		if (cfg & USB_PULL_OVERRIDE) {
			reg_value |= SET_BITS(regUSB0, PULLOVR, 1);
			reg_mask |= SET_BITS(regUSB0, PULLOVR, 1);
		}

		if (cfg & USB_PU) {
			reg_value |= SET_BITS(regUSB0, USB_PU, 1);
			reg_mask |= SET_BITS(regUSB0, USB_PU, 1);
		}

		if (cfg & USB_UDM_PD) {
			reg_value |= SET_BITS(regUSB0, UDM_PD, 1);
			reg_mask |= SET_BITS(regUSB0, UDM_PD, 1);
		}

		if (cfg & USB_UDP_PD) {
			reg_value |= SET_BITS(regUSB0, UDP_PD, 1);
			reg_mask |= SET_BITS(regUSB0, UDP_PD, 1);
		}

		if (cfg & USB_DP150K_PU) {
			reg_value |= SET_BITS(regUSB0, DP150K_PU, 1);
			reg_mask |= SET_BITS(regUSB0, DP150K_PU, 1);
		}

		if (cfg & USB_USBCNTRL) {
			reg_value |= SET_BITS(regUSB0, USBCNTRL, 1);
			reg_mask |= SET_BITS(regUSB0, USBCNTRL, 1);
		}

		if (cfg & USB_VBUS_CURRENT_LIMIT_HIGH) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 0);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_10MS) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 1);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_20MS) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 2);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_30MS) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 3);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_40MS) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 4);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_50MS) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 5);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_60MS) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 6);
		}
		if (cfg & USB_VBUS_CURRENT_LIMIT_LOW) {
			reg_value |= SET_BITS(regUSB0, CURRENT_LIMIT, 7);
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 7);
		}

		if (cfg & USB_VBUS_PULLDOWN) {
			reg_value |= SET_BITS(regUSB0, VBUSPDENB, 1);
			reg_mask |= SET_BITS(regUSB0, VBUSPDENB, 1);
		}

		rc = pmic_write_reg(REG_USB, reg_value, reg_mask);

		if (rc == PMIC_SUCCESS) {
			if ((cfg & USB_VBUS_CURRENT_LIMIT_HIGH) ||
			    (cfg & USB_VBUS_CURRENT_LIMIT_LOW) ||
			    (cfg & USB_VBUS_CURRENT_LIMIT_LOW_10MS) ||
			    (cfg & USB_VBUS_CURRENT_LIMIT_LOW_20MS) ||
			    (cfg & USB_VBUS_CURRENT_LIMIT_LOW_30MS) ||
			    (cfg & USB_VBUS_CURRENT_LIMIT_LOW_40MS) ||
			    (cfg & USB_VBUS_CURRENT_LIMIT_LOW_50MS) ||
			    (cfg & USB_VBUS_CURRENT_LIMIT_LOW_60MS)) {
				/* Make sure that the VBUS current limit state is
				 * correctly set to either USB_VBUS_CURRENT_LIMIT_HIGH
				 * or USB_VBUS_CURRENT_LIMIT_LOW but never both at the
				 * same time.
				 *
				 * We guarantee this by first clearing both of the
				 * status bits and then resetting the correct one.
				 */
				usb.usbOtgCfg &=
				    ~(USB_VBUS_CURRENT_LIMIT_HIGH |
				      USB_VBUS_CURRENT_LIMIT_LOW |
				      USB_VBUS_CURRENT_LIMIT_LOW_10MS |
				      USB_VBUS_CURRENT_LIMIT_LOW_20MS |
				      USB_VBUS_CURRENT_LIMIT_LOW_30MS |
				      USB_VBUS_CURRENT_LIMIT_LOW_40MS |
				      USB_VBUS_CURRENT_LIMIT_LOW_50MS |
				      USB_VBUS_CURRENT_LIMIT_LOW_60MS);
			}

			usb.usbOtgCfg |= cfg;
		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Clears the USB On-The-Go (OTG) configuration. Multiple configuration settings
 * may be OR'd together in a single call. However, selecting conflicting
 * settings (e.g., multiple VBUS current limits) will result in undefined
 * behavior.
 *
 * @param   handle          Device handle from open() call.
 * @param   cfg             USB OTG configuration settings to be cleared.
 *
 * @retval      PMIC_SUCCESS         If the OTG configuration was successfully
 *                                   cleared.
 * @retval      PMIC_PARAMETER_ERROR If the handle is invalid.
 * @retval      PMIC_NOT_SUPPORTED   If the desired USB OTG configuration is
 *                                   not supported by the PMIC hardware.
 */
PMIC_STATUS pmic_convity_usb_otg_clear_config(const PMIC_CONVITY_HANDLE handle,
					      const PMIC_CONVITY_USB_OTG_CONFIG
					      cfg)
{
	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value = 0;
	unsigned int reg_mask = 0;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) && (usb.handle_state == HANDLE_IN_USE)) {
		/* if ((cfg & USB_VBUS_CURRENT_LIMIT_LOW_10MS) ||
		   (cfg & USB_VBUS_CURRENT_LIMIT_LOW_20MS) ||
		   (cfg & USB_VBUS_CURRENT_LIMIT_LOW_30MS) ||
		   (cfg & USB_VBUS_CURRENT_LIMIT_LOW_40MS) ||
		   (cfg & USB_VBUS_CURRENT_LIMIT_LOW_50MS) ||
		   (cfg & USB_VBUS_CURRENT_LIMIT_LOW_60MS))
		   {
		   rc = PMIC_NOT_SUPPORTED;
		   } */

		if (cfg & USB_OTG_SE0CONN) {
			reg_mask = SET_BITS(regUSB0, SE0_CONN, 1);
		}

		if (cfg & USB_OTG_DLP_SRP) {
			reg_mask |= SET_BITS(regUSB0, DLP_SRP, 1);
		}

		if (cfg & USB_DP150K_PU) {
			reg_mask |= SET_BITS(regUSB0, DP150K_PU, 1);
		}

		if (cfg & USB_PULL_OVERRIDE) {
			reg_mask |= SET_BITS(regUSB0, PULLOVR, 1);
		}

		if (cfg & USB_PU) {

			reg_mask |= SET_BITS(regUSB0, USB_PU, 1);
		}

		if (cfg & USB_UDM_PD) {

			reg_mask |= SET_BITS(regUSB0, UDM_PD, 1);
		}

		if (cfg & USB_UDP_PD) {

			reg_mask |= SET_BITS(regUSB0, UDP_PD, 1);
		}

		if (cfg & USB_USBCNTRL) {
			reg_mask |= SET_BITS(regUSB0, USBCNTRL, 1);
		}

		if (cfg & USB_VBUS_CURRENT_LIMIT_HIGH) {
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 0);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_10MS) {
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 1);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_20MS) {
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 2);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_30MS) {
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 3);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_40MS) {
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 4);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_50MS) {
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 5);
		} else if (cfg & USB_VBUS_CURRENT_LIMIT_LOW_60MS) {
			reg_mask |= SET_BITS(regUSB0, CURRENT_LIMIT, 6);
		}

		if (cfg & USB_VBUS_PULLDOWN) {
			reg_mask |= SET_BITS(regUSB0, VBUSPDENB, 1);
		}

		rc = pmic_write_reg(REG_USB, reg_value, reg_mask);

		if (rc == PMIC_SUCCESS) {
			usb.usbOtgCfg &= ~cfg;
		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Get the current USB On-The-Go (OTG) configuration.
 *
 * @param       handle          device handle from open() call
 * @param       cfg             the current USB OTG configuration
 *
 * @return      PMIC_SUCCESS    if the OTG configuration was successfully
 *                              retrieved
 */
PMIC_STATUS pmic_convity_usb_otg_get_config(const PMIC_CONVITY_HANDLE handle,
					    PMIC_CONVITY_USB_OTG_CONFIG *
					    const cfg)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == usb.handle) &&
	    (usb.handle_state == HANDLE_IN_USE) &&
	    (cfg != (PMIC_CONVITY_USB_OTG_CONFIG *) NULL)) {
		*cfg = usb.usbOtgCfg;

		rc = PMIC_SUCCESS;
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*@}*/

/**************************************************************************
 * RS-232-specific configuration and setup functions.
 **************************************************************************
 */

/*!
 * @name RS-232 Serial Connectivity APIs
 * Functions for controlling RS-232 serial connectivity.
 */
/*@{*/

/*!
 * Set the connectivity interface to the selected RS-232 operating
 * configuration. Note that the RS-232 operating mode will be automatically
 * overridden if the USB_EN is asserted at any time (e.g., when a USB device
 * is attached). However, we will also automatically return to the RS-232
 * mode once the USB device is detached.
 *
 * @param       handle          device handle from open() call
 * @param       cfgInternal     RS-232 transceiver internal connections
 * @param       cfgExternal     RS-232 transceiver external connections
 *
 * @return      PMIC_SUCCESS    if the requested mode was set
 */
PMIC_STATUS pmic_convity_rs232_set_config(const PMIC_CONVITY_HANDLE handle,
					  const PMIC_CONVITY_RS232_INTERNAL
					  cfgInternal,
					  const PMIC_CONVITY_RS232_EXTERNAL
					  cfgExternal)
{
	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value0 = 0, reg_value1 = 0;
	unsigned int reg_mask = SET_BITS(regUSB1, RSPOL, 1);

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == rs_232.handle) && (rs_232.handle_state == HANDLE_IN_USE)) {
		rc = PMIC_SUCCESS;

		/* Validate the calling parameters. */
		/*if ((cfgInternal !=  RS232_TX_USE0VM_RX_UDATVP) &&
		   (cfgInternal != RS232_TX_RX_INTERNAL_DEFAULT) && (cfgInternal !=  RS232_TX_UDATVP_RX_URXVM))
		   {

		   rc = PMIC_NOT_SUPPORTED;
		   } */
		if (cfgInternal == RS232_TX_USE0VM_RX_UDATVP) {

			reg_value0 = SET_BITS(regUSB0, INTERFACE_MODE, 1);

		} else if (cfgInternal == RS232_TX_RX_INTERNAL_DEFAULT) {

			reg_value0 = SET_BITS(regUSB0, INTERFACE_MODE, 1);
			reg_mask |= SET_BITS(regUSB1, RSPOL, 1);

		} else if (cfgInternal == RS232_TX_UDATVP_RX_URXVM) {

			reg_value0 = SET_BITS(regUSB0, INTERFACE_MODE, 2);
			reg_value1 |= SET_BITS(regUSB1, RSPOL, 1);

		} else if ((cfgExternal == RS232_TX_UDM_RX_UDP) ||
			   (cfgExternal == RS232_TX_RX_EXTERNAL_DEFAULT)) {
			/* Configure for TX on D+ and RX on D-. */
			reg_value0 |= SET_BITS(regUSB0, INTERFACE_MODE, 1);
			reg_value1 |= SET_BITS(regUSB1, RSPOL, 0);
		} else if (cfgExternal != RS232_TX_UDM_RX_UDP) {
			/* Any other RS-232 configuration is an error. */
			rc = PMIC_ERROR;
		}

		if (rc == PMIC_SUCCESS) {
			/* Configure for TX on D- and RX on D+. */
			rc = pmic_write_reg(REG_USB, reg_value0, reg_mask);

			rc = pmic_write_reg(REG_CHARGE_USB_SPARE,
					    reg_value1, reg_mask);

			if (rc == PMIC_SUCCESS) {
				rs_232.rs232CfgInternal = cfgInternal;
				rs_232.rs232CfgExternal = cfgExternal;
			}
		}
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*!
 * Get the connectivity interface's current RS-232 operating configuration.
 *
 * @param       handle          device handle from open() call
 * @param       cfgInternal     RS-232 transceiver internal connections
 * @param       cfgExternal     RS-232 transceiver external connections
 *
 * @return      PMIC_SUCCESS    if the requested mode was retrieved
 */
PMIC_STATUS pmic_convity_rs232_get_config(const PMIC_CONVITY_HANDLE handle,
					  PMIC_CONVITY_RS232_INTERNAL *
					  const cfgInternal,
					  PMIC_CONVITY_RS232_EXTERNAL *
					  const cfgExternal)
{
	PMIC_STATUS rc = PMIC_ERROR;

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle == rs_232.handle) &&
	    (rs_232.handle_state == HANDLE_IN_USE) &&
	    (cfgInternal != (PMIC_CONVITY_RS232_INTERNAL *) NULL) &&
	    (cfgExternal != (PMIC_CONVITY_RS232_EXTERNAL *) NULL)) {
		*cfgInternal = rs_232.rs232CfgInternal;
		*cfgExternal = rs_232.rs232CfgExternal;

		rc = PMIC_SUCCESS;
	}

	/* Exit the critical section. */
	up(&mutex);

	return rc;
}

/*@}*/

/**************************************************************************
 * CEA-936-specific configuration and setup functions.
 **************************************************************************
 */

/*!
 * @name CEA-936 Connectivity APIs
 * Functions for controlling CEA-936 connectivity.
 */
/*@{*/

/*!
 * Signal the attached device to exit the current CEA-936 operating mode.
 * Returns an error if the current operating mode is not CEA-936.
 *
 * @param       handle          device handle from open() call
 * @param       signal          type of exit signal to be sent
 *
 * @return      PMIC_SUCCESS    if exit signal was sent
 */
PMIC_STATUS pmic_convity_cea936_exit_signal(const PMIC_CONVITY_HANDLE handle,
					    const
					    PMIC_CONVITY_CEA936_EXIT_SIGNAL
					    signal)
{
	PMIC_STATUS rc = PMIC_ERROR;
	unsigned int reg_value = 0;
	unsigned int reg_mask =
	    SET_BITS(regUSB0, IDPD, 1) | SET_BITS(regUSB0, IDPULSE, 1);

	/* Use a critical section to maintain a consistent state. */
	if (down_interruptible(&mutex))
		return PMIC_SYSTEM_ERROR_EINTR;

	if ((handle != cea_936.handle)
	    || (cea_936.handle_state != HANDLE_IN_USE)) {
		/* Must return error indication for invalid handle parameter to be
		 * consistent with other APIs.
		 */
		rc = PMIC_ERROR;
	} else if (signal == CEA936_UID_PULLDOWN_6MS) {
		reg_value =
		    SET_BITS(regUSB0, IDPULSE, 0) | SET_BITS(regUSB0, IDPD, 0);
	} else if (signal == CEA936_UID_PULLDOWN_6MS) {
		reg_value = SET_BITS(regUSB0, IDPULSE, 1);
	} else if (signal == CEA936_UID_PULLDOWN) {
		reg_value = SET_BITS(regUSB0, IDPD, 1);
	} else if (signal == CEA936_UDMPULSE) {
		reg_value = SET_BITS(regUSB0, DMPULSE, 1);
	}

	rc = pmic_write_reg(REG_USB, reg_value, reg_mask);

	up(&mutex);

	return rc;
}

/*@}*/

/**************************************************************************
 * Static functions.
 **************************************************************************
 */

/*!
 * @name Connectivity Driver Internal Support Functions
 * These non-exported internal functions are used to support the functionality
 * of the exported connectivity APIs.
 */
/*@{*/

/*!
 * This internal helper function sets the desired operating mode (either USB
 * OTG or RS-232). It must be called with the mutex already acquired.
 *
 * @param       mode               the desired operating mode (USB or RS232)
 *
 * @return      PMIC_SUCCESS       if the desired operating mode was set
 * @return      PMIC_NOT_SUPPORTED if the desired operating mode is invalid
 */
static PMIC_STATUS pmic_convity_set_mode_internal(const PMIC_CONVITY_MODE mode)
{
	unsigned int reg_value0 = 0, reg_value1 = 0;
	unsigned int reg_mask0 = 0, reg_mask1 = 0;

	PMIC_STATUS rc = PMIC_SUCCESS;

	switch (mode) {
	case USB:
		/* For the USB mode, we start by tri-stating the USB bus (by
		 * setting VBUSEN = 0) until a device is connected (i.e.,
		 * until we receive a 4.4V rising edge event). All pull-up
		 * and pull-down resistors are also disabled until a USB
		 * device is actually connected and we have determined which
		 * device is the host and the desired USB bus speed.
		 *
		 * Also tri-state the RS-232 buffers (by setting RSTRI = 1).
		 * This prevents the hardware from automatically returning to
		 * the RS-232 mode when the USB device is detached.
		 */

		reg_value0 = SET_BITS(regUSB0, INTERFACE_MODE, 0);
		reg_mask0 = SET_BITS(regUSB0, INTERFACE_MODE, 7);

		/*reg_value1 = SET_BITS(regUSB1, RSTRI, 1); */

		rc = pmic_write_reg(REG_USB, reg_value0, reg_mask0);
		/*      if (rc == PMIC_SUCCESS) {
		   CHECK_ERROR(pmic_write_reg
		   (REG_CHARGE_USB_SPARE,
		   reg_value1, reg_mask1));
		   } */

		break;

	case RS232_1:
		/* For the RS-232 mode, we tri-state the USB bus (by setting
		 * VBUSEN = 0) and enable the RS-232 transceiver (by setting
		 * RS232ENB = 0).
		 *
		 * Note that even in the RS-232 mode, if a USB device is
		 * plugged in, we will receive a 4.4V rising edge event which
		 * will automatically disable the RS-232 transceiver and
		 * tri-state the RS-232 buffers. This allows us to temporarily
		 * switch over to USB mode while the USB device is attached.
		 * The RS-232 transceiver and buffers will be automatically
		 * re-enabled when the USB device is detached.
		 */

		/* Explicitly disconnect all of the USB pull-down resistors
		 * and the VUSB power regulator here just to be safe.
		 *
		 * But we do connect the internal pull-up resistor on USB_D+
		 * to avoid having an extra load on the USB_D+ line when in
		 * RS-232 mode.
		 */

		reg_value0 |= SET_BITS(regUSB0, INTERFACE_MODE, 1) |
		    SET_BITS(regUSB0, VBUSPDENB, 1) |
		    SET_BITS(regUSB0, USB_PU, 1);
		reg_mask0 =
		    SET_BITS(regUSB0, INTERFACE_MODE, 7) | SET_BITS(regUSB0,
								    VBUSPDENB,
								    1) |
		    SET_BITS(regUSB0, USB_PU, 1);

		reg_value1 = SET_BITS(regUSB1, RSPOL, 0);

		rc = pmic_write_reg(REG_USB, reg_value0, reg_mask0);

		if (rc == PMIC_SUCCESS) {
			CHECK_ERROR(pmic_write_reg
				    (REG_CHARGE_USB_SPARE,
				     reg_value1, reg_mask1));
		}
		break;

	case RS232_2:
		reg_value0 |= SET_BITS(regUSB0, INTERFACE_MODE, 2) |
		    SET_BITS(regUSB0, VBUSPDENB, 1) |
		    SET_BITS(regUSB0, USB_PU, 1);
		reg_mask0 =
		    SET_BITS(regUSB0, INTERFACE_MODE, 7) | SET_BITS(regUSB0,
								    VBUSPDENB,
								    1) |
		    SET_BITS(regUSB0, USB_PU, 1);

		reg_value1 = SET_BITS(regUSB1, RSPOL, 1);

		rc = pmic_write_reg(REG_USB, reg_value0, reg_mask0);

		if (rc == PMIC_SUCCESS) {
			CHECK_ERROR(pmic_write_reg
				    (REG_CHARGE_USB_SPARE,
				     reg_value1, reg_mask1));
		}
		break;

	case CEA936_MONO:
		reg_value0 |= SET_BITS(regUSB0, INTERFACE_MODE, 4);

		rc = pmic_write_reg(REG_USB, reg_value0, reg_mask0);
		break;

	case CEA936_STEREO:
		reg_value0 |= SET_BITS(regUSB0, INTERFACE_MODE, 5);
		reg_mask0 = SET_BITS(regUSB0, INTERFACE_MODE, 7);

		rc = pmic_write_reg(REG_USB, reg_value0, reg_mask0);
		break;

	case CEA936_TEST_RIGHT:
		reg_value0 |= SET_BITS(regUSB0, INTERFACE_MODE, 6);
		reg_mask0 = SET_BITS(regUSB0, INTERFACE_MODE, 7);

		rc = pmic_write_reg(REG_USB, reg_value0, reg_mask0);
		break;

	case CEA936_TEST_LEFT:
		reg_value0 |= SET_BITS(regUSB0, INTERFACE_MODE, 7);
		reg_mask0 = SET_BITS(regUSB0, INTERFACE_MODE, 7);

		rc = pmic_write_reg(REG_USB, reg_value0, reg_mask0);
		break;

	default:
		rc = PMIC_NOT_SUPPORTED;
	}

	if (rc == PMIC_SUCCESS) {
		if (mode == USB) {
			usb.mode = mode;
		} else if ((mode == RS232_1) || (mode == RS232_1)) {
			rs_232.mode = mode;
		} else if ((mode == CEA936_MONO) || (mode == CEA936_STEREO) ||
			   (mode == CEA936_TEST_RIGHT)
			   || (mode == CEA936_TEST_LEFT)) {
			cea_936.mode = mode;
		}
	}

	return rc;
}

/*!
 * This internal helper function deregisters all of the currently registered
 * callback events. It must be called with the mutual exclusion spinlock
 * already acquired.
 *
 * We've defined the event and callback deregistration code here as a separate
 * function because it can be called by either the pmic_convity_close() or the
 * pmic_convity_clear_callback() APIs. We also wanted to avoid any possible
 * issues with having the same thread calling spin_lock_irq() twice.
 *
 * Note that the mutex must have already been acquired. We will also acquire
 * the spinlock here to avoid any possible race conditions with the interrupt
 * handler.
 *
 * @return      PMIC_SUCCESS    if all of the callback events were cleared
 */
static PMIC_STATUS pmic_convity_deregister_all(void)
{
	unsigned long flags;
	PMIC_STATUS rc = PMIC_SUCCESS;

	/* Deregister each of the PMIC events that we had previously
	 * registered for by using pmic_event_subscribe().
	 */

	if ((usb.eventMask & USB_DETECT_MINI_A) ||
	    (usb.eventMask & USB_DETECT_MINI_B) ||
	    (usb.eventMask & USB_DETECT_NON_USB_ACCESSORY) ||
	    (usb.eventMask & USB_DETECT_FACTORY_MODE)) {
		/* EVENT_AB_DETI or EVENT_IDI */
		eventNotify.func = pmic_convity_event_handler;
		eventNotify.param = (void *)(CORE_EVENT_ABDET);

		if (pmic_event_unsubscribe(EVENT_IDI, eventNotify) ==
		    PMIC_SUCCESS) {
			/* Also acquire the spinlock here to avoid any possible race
			 * conditions with the interrupt handler.
			 */

			spin_lock_irqsave(&lock, flags);

			usb.eventMask &= ~(USB_DETECT_MINI_A |
					   USB_DETECT_MINI_B |
					   USB_DETECT_NON_USB_ACCESSORY |
					   USB_DETECT_FACTORY_MODE);

			spin_unlock_irqrestore(&lock, flags);
		} else {
			pr_debug
			    ("%s: pmic_event_unsubscribe() for EVENT_AB_DETI failed\n",
			     __FILE__);
			rc = PMIC_ERROR;
		}
	}

	else if ((usb.eventMask & USB_DETECT_0V8_RISE) ||
		 (usb.eventMask & USB_DETECT_0V8_FALL)) {
		/* EVENT_USB_08VI or EVENT_USBI */
		eventNotify.func = pmic_convity_event_handler;
		eventNotify.param = (void *)(CORE_EVENT_0V8);
		if (pmic_event_unsubscribe(EVENT_USBI, eventNotify) ==
		    PMIC_SUCCESS) {
			/* Also acquire the spinlock here to avoid any possible race
			 * conditions with the interrupt handler.
			 */
			spin_lock_irqsave(&lock, flags);

			usb.eventMask &= ~(USB_DETECT_0V8_RISE |
					   USB_DETECT_0V8_FALL);

			spin_unlock_irqrestore(&lock, flags);
		} else {
			pr_debug
			    ("%s: pmic_event_unsubscribe() for EVENT_USB_08VI failed\n",
			     __FILE__);
			rc = PMIC_ERROR;
		}

	}

	else if ((usb.eventMask & USB_DETECT_2V0_RISE) ||
		 (usb.eventMask & USB_DETECT_2V0_FALL)) {
		/* EVENT_USB_20VI or EVENT_USBI */
		eventNotify.func = pmic_convity_event_handler;
		eventNotify.param = (void *)(CORE_EVENT_2V0);
		if (pmic_event_unsubscribe(EVENT_USBI, eventNotify) ==
		    PMIC_SUCCESS) {
			/* Also acquire the spinlock here to avoid any possible race
			 * conditions with the interrupt handler.
			 */
			spin_lock_irqsave(&lock, flags);

			usb.eventMask &= ~(USB_DETECT_2V0_RISE |
					   USB_DETECT_2V0_FALL);

			spin_unlock_irqrestore(&lock, flags);
		} else {
			pr_debug
			    ("%s: pmic_event_unsubscribe() for EVENT_USB_20VI failed\n",
			     __FILE__);
			rc = PMIC_ERROR;
		}
	}

	else if ((usb.eventMask & USB_DETECT_4V4_RISE) ||
		 (usb.eventMask & USB_DETECT_4V4_FALL)) {

		/* EVENT_USB_44VI or EVENT_USBI */
		eventNotify.func = pmic_convity_event_handler;
		eventNotify.param = (void *)(CORE_EVENT_4V4);

		if (pmic_event_unsubscribe(EVENT_USBI, eventNotify) ==
		    PMIC_SUCCESS) {

			/* Also acquire the spinlock here to avoid any possible race
			 * conditions with the interrupt handler.
			 */
			spin_lock_irqsave(&lock, flags);

			usb.eventMask &= ~(USB_DETECT_4V4_RISE |
					   USB_DETECT_4V4_FALL);

			spin_unlock_irqrestore(&lock, flags);
		} else {
			pr_debug
			    ("%s: pmic_event_unsubscribe() for EVENT_USB_44VI failed\n",
			     __FILE__);
			rc = PMIC_ERROR;
		}
	}

	if (rc == PMIC_SUCCESS) {
		/* Also acquire the spinlock here to avoid any possible race
		 * conditions with the interrupt handler.
		 */
		spin_lock_irqsave(&lock, flags);

		/* Restore the initial reset values for the callback function
		 * and event mask parameters. This should be NULL and zero,
		 * respectively.
		 *
		 * Note that we wait until the end here to fully reset everything
		 * just in case some of the pmic_event_unsubscribe() calls above
		 * failed for some reason (which normally shouldn't happen).
		 */
		usb.callback = reset.callback;
		usb.eventMask = reset.eventMask;

		spin_unlock_irqrestore(&lock, flags);
	}
	return rc;
}

/*!
 * This is the default event handler for all connectivity-related events
 * and hardware interrupts.
 *
 * @param       param           event ID
 */
static void pmic_convity_event_handler(void *param)
{
	unsigned long flags;

	/* Update the global list of active interrupt events. */
	spin_lock_irqsave(&lock, flags);
	eventID |= (PMIC_CORE_EVENT) (param);
	spin_unlock_irqrestore(&lock, flags);

	/* Schedule the tasklet to be run as soon as it is convenient to do so. */
	schedule_work(&convityTasklet);
}

/*!
 * @brief This is the connectivity driver tasklet that handles interrupt events.
 *
 * This function is scheduled by the connectivity driver interrupt handler
 * pmic_convity_event_handler() to complete the processing of all of the
 * connectivity-related interrupt events.
 *
 * Since this tasklet runs with interrupts enabled, we can safely call
 * the ADC driver, if necessary, to properly detect the type of USB connection
 * that is being made and to call any user-registered callback functions.
 *
 * @param   arg                The parameter that was provided above in
 *                                  the DECLARE_TASKLET() macro (unused).
 */
static void pmic_convity_tasklet(struct work_struct *work)
{

	PMIC_CONVITY_EVENTS activeEvents = 0;
	unsigned long flags = 0;

	/* Check the interrupt sense bits to determine exactly what
	 * event just occurred.
	 */
	if (eventID & CORE_EVENT_4V4) {
		spin_lock_irqsave(&lock, flags);
		eventID &= ~CORE_EVENT_4V4;
		spin_unlock_irqrestore(&lock, flags);

		activeEvents |= pmic_check_sensor(SENSE_USB4V4S) ?
		    USB_DETECT_4V4_RISE : USB_DETECT_4V4_FALL;

		if (activeEvents & ~usb.eventMask) {
			/* The default handler for 4.4 V rising/falling edge detection
			 * is to simply ignore the event.
			 */
			;
		}
	}
	if (eventID & CORE_EVENT_2V0) {
		spin_lock_irqsave(&lock, flags);
		eventID &= ~CORE_EVENT_2V0;
		spin_unlock_irqrestore(&lock, flags);

		activeEvents |= pmic_check_sensor(SENSE_USB2V0S) ?
		    USB_DETECT_2V0_RISE : USB_DETECT_2V0_FALL;
		if (activeEvents & ~usb.eventMask) {
			/* The default handler for 2.0 V rising/falling edge detection
			 * is to simply ignore the event.
			 */
			;
		}
	}
	if (eventID & CORE_EVENT_0V8) {
		spin_lock_irqsave(&lock, flags);
		eventID &= ~CORE_EVENT_0V8;
		spin_unlock_irqrestore(&lock, flags);

		activeEvents |= pmic_check_sensor(SENSE_USB0V8S) ?
		    USB_DETECT_0V8_RISE : USB_DETECT_0V8_FALL;

		if (activeEvents & ~usb.eventMask) {
			/* The default handler for 0.8 V rising/falling edge detection
			 * is to simply ignore the event.
			 */
			;
		}
	}
	if (eventID & CORE_EVENT_ABDET) {
		spin_lock_irqsave(&lock, flags);
		eventID &= ~CORE_EVENT_ABDET;
		spin_unlock_irqrestore(&lock, flags);

		activeEvents |= pmic_check_sensor(SENSE_ID_GNDS) ?
		    USB_DETECT_MINI_A : 0;

		activeEvents |= pmic_check_sensor(SENSE_ID_FLOATS) ?
		    USB_DETECT_MINI_B : 0;
	}

	/* Begin a critical section here so that we don't register/deregister
	 * for events or open/close the connectivity driver while the existing
	 * event handler (if it is currently defined) is in the middle of handling
	 * the current event.
	 */
	spin_lock_irqsave(&lock, flags);

	/* Finally, call the user-defined callback function if required. */
	if ((usb.handle_state == HANDLE_IN_USE) &&
	    (usb.callback != NULL) && (activeEvents & usb.eventMask)) {
		(*usb.callback) (activeEvents);
	}

	spin_unlock_irqrestore(&lock, flags);
}

/*@}*/

/**************************************************************************
 * Module initialization and termination functions.
 *
 * Note that if this code is compiled into the kernel, then the
 * module_init() function will be called within the device_initcall()
 * group.
 **************************************************************************
 */

/*!
 * @name Connectivity Driver Loading/Unloading Functions
 * These non-exported internal functions are used to support the connectivity
 * device driver initialization and de-initialization operations.
 */
/*@{*/

/*!
 * @brief This is the connectivity device driver initialization function.
 *
 * This function is called by the kernel when this device driver is first
 * loaded.
 */
static int __init mc13783_pmic_convity_init(void)
{
	printk(KERN_INFO "PMIC Connectivity driver loading..\n");

	return 0;
}

/*!
 * @brief This is the Connectivity device driver de-initialization function.
 *
 * This function is called by the kernel when this device driver is about
 * to be unloaded.
 */
static void __exit mc13783_pmic_convity_exit(void)
{
	printk(KERN_INFO "PMIC Connectivity driver unloading\n");

	/* Close the device handle if it is still open. This will also
	 * deregister any callbacks that may still be active.
	 */
	if (usb.handle_state == HANDLE_IN_USE) {
		pmic_convity_close(usb.handle);
	} else if (usb.handle_state == HANDLE_IN_USE) {
		pmic_convity_close(rs_232.handle);
	} else if (usb.handle_state == HANDLE_IN_USE) {
		pmic_convity_close(cea_936.handle);
	}

	/* Reset the PMIC Connectivity register to it's power on state.
	 * We should do this when unloading the module so that we don't
	 * leave the hardware in a state which could cause problems when
	 * no device driver is loaded.
	 */
	pmic_write_reg(REG_USB, RESET_USBCNTRL_REG_0, REG_FULLMASK);
	pmic_write_reg(REG_CHARGE_USB_SPARE, RESET_USBCNTRL_REG_1,
		       REG_FULLMASK);
	/* Note that there is no need to reset the "convity" device driver
	 * state structure to the reset state since we are in the final
	 * stage of unloading the device driver. The device driver state
	 * structure will be automatically and properly reinitialized if
	 * this device driver is reloaded.
	 */
}

/*@}*/

/*
 * Module entry points and description information.
 */

module_init(mc13783_pmic_convity_init);
module_exit(mc13783_pmic_convity_exit);

MODULE_DESCRIPTION("mc13783 Connectivity device driver");
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_LICENSE("GPL");