summaryrefslogtreecommitdiff
path: root/lwis_device.c
blob: 309584d4196f6174b4e712e05d74f5ae234bc867 (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
/*
 * Google LWIS Base Device Driver
 *
 * Copyright (c) 2018 Google, LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#define pr_fmt(fmt) KBUILD_MODNAME "-dev: " fmt

#include "lwis_device.h"

#include <linux/delay.h>
#include <linux/device.h>
#include <linux/hashtable.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pinctrl/consumer.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#include "lwis_buffer.h"
#include "lwis_clock.h"
#include "lwis_commands.h"
#include "lwis_debug.h"
#include "lwis_device.h"
#include "lwis_device_dpm.h"
#include "lwis_device_slc.h"
#include "lwis_device_test.h"
#include "lwis_dt.h"
#include "lwis_event.h"
#include "lwis_gpio.h"
#include "lwis_init.h"
#include "lwis_ioctl.h"
#include "lwis_periodic_io.h"
#include "lwis_pinctrl.h"
#include "lwis_platform.h"
#include "lwis_transaction.h"
#include "lwis_util.h"
#include "lwis_version.h"
#include "lwis_trace.h"
#include "lwis_i2c_bus_manager.h"

#ifdef CONFIG_OF
#include "lwis_dt.h"
#endif

#define LWIS_CLASS_NAME "lwis"
#define LWIS_DEVICE_NAME "lwis"
#define LWIS_MAX_DEVICES (1U << MINORBITS)
#define MCLK_ON_STRING "mclk_on"
#define MCLK_OFF_STRING "mclk_off"

/* Define this to help debug power sequence */
#undef LWIS_PWR_SEQ_DEBUG

/* Global declaration for core lwis structure */
static struct lwis_core core;

static int lwis_open(struct inode *node, struct file *fp);
static int lwis_release(struct inode *node, struct file *fp);
static long lwis_ioctl(struct file *fp, unsigned int type, unsigned long param);
static unsigned int lwis_poll(struct file *fp, poll_table *wait);
static ssize_t lwis_read(struct file *fp, char __user *user_buf, size_t count, loff_t *pos);

static struct file_operations lwis_fops = {
	.owner = THIS_MODULE,
	.open = lwis_open,
	.release = lwis_release,
	.unlocked_ioctl = lwis_ioctl,
	.poll = lwis_poll,
	.read = lwis_read,
};

/*
 * transaction_work_func:
 * Function to be called by transaction worker thread to direct it the correct client
 * to process their queues
 */
static void transaction_work_func(struct kthread_work *work)
{
	struct lwis_client *client = container_of(work, struct lwis_client, transaction_work);
	lwis_process_worker_queue(client);
}

static void i2c_process_work_func(struct kthread_work *work)
{
	/* i2c_work stores the context of the lwis_client submitting the transfer request */
	struct lwis_client *client = container_of(work, struct lwis_client, i2c_work);
	lwis_i2c_bus_manager_process_worker_queue(client);
}

/*
 *  lwis_open: Opening an instance of a LWIS device
 */
static int lwis_open(struct inode *node, struct file *fp)
{
	struct lwis_device *lwis_dev;
	struct lwis_client *lwis_client;
	unsigned long flags;

	/* Making sure the minor number associated with fp exists */
	mutex_lock(&core.lock);
	lwis_dev = idr_find(core.idr, iminor(node));
	mutex_unlock(&core.lock);
	if (!lwis_dev) {
		pr_err("No device %d found\n", iminor(node));
		return -ENODEV;
	}
	dev_info(lwis_dev->dev, "Opening instance %d\n", iminor(node));

	lwis_client = kzalloc(sizeof(struct lwis_client), GFP_KERNEL);
	if (!lwis_client) {
		dev_err(lwis_dev->dev, "Failed to allocate lwis client\n");
		return -ENOMEM;
	}

	lwis_client->lwis_dev = lwis_dev;
	/* Initialize locks */
	mutex_init(&lwis_client->lock);
	spin_lock_init(&lwis_client->periodic_io_lock);
	spin_lock_init(&lwis_client->event_lock);
	spin_lock_init(&lwis_client->flush_lock);
	spin_lock_init(&lwis_client->buffer_lock);

	/* Empty hash table for client event states */
	hash_init(lwis_client->event_states);

	/* The event queue itself is a linked list */
	INIT_LIST_HEAD(&lwis_client->event_queue);
	INIT_LIST_HEAD(&lwis_client->error_event_queue);

	/* Initialize the wait queue for the event queue */
	init_waitqueue_head(&lwis_client->event_wait_queue);

	/* Empty hash table for client allocated buffers */
	hash_init(lwis_client->allocated_buffers);

	/* Empty hash table for client enrolled buffers */
	hash_init(lwis_client->enrolled_buffers);

	/* Initialize the allocator */
	lwis_allocator_init(lwis_dev);

	kthread_init_work(&lwis_client->transaction_work, transaction_work_func);
	kthread_init_work(&lwis_client->i2c_work, i2c_process_work_func);

	/* Start transaction processor task */
	lwis_transaction_init(lwis_client);

	/* Start periodic io processor task */
	lwis_periodic_io_init(lwis_client);

	memset(&lwis_client->debug_info, 0, sizeof(lwis_client->debug_info));

	spin_lock_irqsave(&lwis_dev->lock, flags);
	list_add(&lwis_client->node, &lwis_dev->clients);
	spin_unlock_irqrestore(&lwis_dev->lock, flags);

	/* Storing the client handle in fp private_data for easy access */
	fp->private_data = lwis_client;

	spin_lock_irqsave(&lwis_client->flush_lock, flags);
	lwis_client->flush_state = NOT_FLUSHING;
	spin_unlock_irqrestore(&lwis_client->flush_lock, flags);

	if (lwis_i2c_bus_manager_connect_client(lwis_client)) {
		dev_err(lwis_dev->dev, "Failed to connect lwis client to I2C bus manager\n");
		return -EINVAL;
	}

	lwis_client->is_enabled = false;
	return 0;
}

/* Cleans client contents to a reset state. This will hold the LWIS client mutex. */
static int lwis_cleanup_client(struct lwis_client *lwis_client)
{
	/* Let's lock the mutex while we're cleaning up to avoid other parts
	 * of the code from acquiring dangling pointers to the client or any
	 * of the client event states that are about to be freed
	 */
	mutex_lock(&lwis_client->lock);

	/* Clear event states for this client */
	lwis_client_event_states_clear(lwis_client);

	/* Clear the event queues */
	lwis_client_event_queue_clear(lwis_client);
	lwis_client_error_event_queue_clear(lwis_client);

	/* Clean up all periodic io state for the client */
	lwis_periodic_io_client_cleanup(lwis_client);

	/* Cancel all pending transactions for the client and destory workqueue*/
	lwis_transaction_clear(lwis_client);

	/* Run cleanup transactions. */
	lwis_transaction_client_cleanup(lwis_client);

	/* Disenroll and clear the table of allocated and enrolled buffers */
	lwis_client_allocated_buffers_clear(lwis_client);
	lwis_client_enrolled_buffers_clear(lwis_client);

	mutex_unlock(&lwis_client->lock);

	return 0;
}

/* Calling this requires holding lwis_dev->lock. */
static inline bool check_client_exists(const struct lwis_device *lwis_dev,
				       const struct lwis_client *lwis_client)
{
	struct lwis_client *p, *n;
	list_for_each_entry_safe (p, n, &lwis_dev->clients, node) {
		if (lwis_client == p) {
			return true;
		}
	}
	return false;
}

/* Release client and deletes its entry from the device's client list,
 * this assumes that LWIS device still exists and will hold LWIS device
 * and LWIS client locks. */
static int lwis_release_client(struct lwis_client *lwis_client)
{
	struct lwis_device *lwis_dev = lwis_client->lwis_dev;
	int rc = 0;
	unsigned long flags;

	rc = lwis_cleanup_client(lwis_client);
	if (rc) {
		return rc;
	}

	/* Take this lwis_client off the list of active clients */
	spin_lock_irqsave(&lwis_dev->lock, flags);
	if (check_client_exists(lwis_dev, lwis_client)) {
		list_del(&lwis_client->node);
	} else {
		dev_err(lwis_dev->dev, "Trying to release a client tied to this device, "
				       "but the entry was not found on the clients list.");
	}
	spin_unlock_irqrestore(&lwis_dev->lock, flags);

	lwis_i2c_bus_manager_disconnect_client(lwis_client);

	kfree(lwis_client);

	return 0;
}

/*
 *  lwis_release: Closing an instance of a LWIS device
 */
static int lwis_release(struct inode *node, struct file *fp)
{
	struct lwis_client *lwis_client = fp->private_data;
	struct lwis_device *lwis_dev = lwis_client->lwis_dev;
	int rc = 0;
	int __maybe_unused i;
	bool is_client_enabled = lwis_client->is_enabled;

	dev_info(lwis_dev->dev, "Closing instance %d\n", iminor(node));

	rc = lwis_release_client(lwis_client);

	/* Release the allocator and its cache */
	lwis_allocator_release(lwis_dev);

	mutex_lock(&lwis_dev->client_lock);
	/* Release power if client closed without power down called */
	if (is_client_enabled && lwis_dev->enabled > 0) {
		lwis_dev->enabled--;
		if (lwis_dev->enabled == 0) {
			lwis_debug_crash_info_dump(lwis_dev);
			dev_info(lwis_dev->dev, "No more client, power down\n");
			rc = lwis_dev_power_down_locked(lwis_dev);
			lwis_dev->is_suspended = false;
		}
	}

	if (lwis_dev->enabled == 0) {
		for (i = 0; i < lwis_dev->bts_block_num; i++) {
			if (lwis_dev->bts_indexes[i] != BTS_UNSUPPORTED) {
				lwis_platform_update_bts(lwis_dev, i, /*bw_peak=*/0,
							 /*bw_read=*/0, /*bw_write=*/0,
							 /*bw_rt=*/0);
			}
		}
		/* remove voted qos */
		lwis_platform_remove_qos(lwis_dev);
		/* Release device event states if no more client is using */
		lwis_device_event_states_clear_locked(lwis_dev);
	}
	mutex_unlock(&lwis_dev->client_lock);

	/* Call device type specific close routines. */
	if (lwis_dev->enabled == 0) {
		if (lwis_dev->vops.close != NULL) {
			lwis_dev->vops.close(lwis_dev);
		}
	}

	return rc;
}

/*
 *  lwis_ioctl: I/O control function on a LWIS device
 *
 *  List of IOCTL types are defined in lwis_commands.h
 */
static long lwis_ioctl(struct file *fp, unsigned int type, unsigned long param)
{
	struct lwis_client *lwis_client;
	struct lwis_device *lwis_dev;

	lwis_client = fp->private_data;
	if (!lwis_client) {
		pr_err("Cannot find client instance\n");
		return -ENODEV;
	}

	lwis_dev = lwis_client->lwis_dev;
	if (!lwis_dev) {
		pr_err("Cannot find device instance\n");
		return -ENODEV;
	}

	return lwis_ioctl_handler(lwis_client, type, param);
}
/*
 *  lwis_poll: Event queue status function of LWIS
 *
 */
static unsigned int lwis_poll(struct file *fp, poll_table *wait)
{
	unsigned int mask = 0;
	struct lwis_client *lwis_client;

	lwis_client = fp->private_data;
	if (!lwis_client) {
		pr_err("Cannot find client instance\n");
		return POLLERR;
	}

	/* Add our wait queue to the poll table */
	poll_wait(fp, &lwis_client->event_wait_queue, wait);

	/* Check if we have anything in the event lists */
	if (lwis_client_error_event_peek_front(lwis_client, NULL) == 0) {
		mask |= POLLERR;
	} else if (lwis_client_event_peek_front(lwis_client, NULL) == 0) {
		mask |= POLLIN;
	}

	return mask;
}

static ssize_t lwis_read(struct file *fp, char __user *user_buf, size_t count, loff_t *pos)
{
	int ret = 0;
	/* Buffer to store information */
	const size_t buffer_size = 8192;
	char *buffer = kzalloc(buffer_size, GFP_KERNEL);
	if (!buffer) {
		pr_err("Failed to allocate read buffer\n");
		return -ENOMEM;
	}

	lwis_get_feature_flags(buffer, buffer_size);

	ret = simple_read_from_buffer(user_buf, count, pos, buffer, strlen(buffer));

	kfree(buffer);

	return ret;
}

static int lwis_base_setup(struct lwis_device *lwis_dev)
{
	int ret = 0;

#ifdef CONFIG_OF
	/* Parse device tree for device configurations */
	ret = lwis_base_parse_dt(lwis_dev);
	if (ret) {
		pr_err("Failed to parse device tree\n");
	}
#else
	/* Non-device-tree init: Save for future implementation */
	ret = -ENOSYS;
#endif

	return ret;
}

/*
 *  lwis_assign_top_to_other: Assign top device to the devices probed before.
 */
static void lwis_assign_top_to_other(struct lwis_device *top_dev)
{
	struct lwis_device *lwis_dev;

	mutex_lock(&core.lock);
	list_for_each_entry (lwis_dev, &core.lwis_dev_list, dev_list) {
		lwis_dev->top_dev = top_dev;
	}
	mutex_unlock(&core.lock);
}

static bool need_to_power_up(struct lwis_device *lwis_dev)
{
	int i;

	if (lwis_dev->power_seq_handler == NULL) {
		return true;
	}

	mutex_lock(&core.lock);
	for (i = 0; i < MAX_UNIFIED_POWER_DEVICE; i++) {
		if (core.unified_dev_pwr_map[i].dev_node_seq == NULL) {
			break;
		}
		if (core.unified_dev_pwr_map[i].dev_node_seq == lwis_dev->power_seq_handler) {
			if (core.unified_dev_pwr_map[i].count == 0) {
				break;
			}
			mutex_unlock(&core.lock);
#ifdef LWIS_PWR_SEQ_DEBUG
			dev_info(lwis_dev->dev, "%s: Already power up\n", __func__);
#endif
			return false;
		}
	}
	mutex_unlock(&core.lock);
#ifdef LWIS_PWR_SEQ_DEBUG
	dev_info(lwis_dev->dev, "%s: Need power up\n", __func__);
#endif
	return true;
}

static bool need_to_power_down(struct lwis_device *lwis_dev)
{
	int i;

	if (lwis_dev->power_seq_handler == NULL) {
		return true;
	}

	mutex_lock(&core.lock);
	for (i = 0; i < MAX_UNIFIED_POWER_DEVICE; i++) {
		if (core.unified_dev_pwr_map[i].dev_node_seq == NULL) {
			break;
		}
		if (core.unified_dev_pwr_map[i].dev_node_seq == lwis_dev->power_seq_handler) {
			if (core.unified_dev_pwr_map[i].count == 1) {
				break;
			}
			mutex_unlock(&core.lock);
#ifdef LWIS_PWR_SEQ_DEBUG
			dev_info(lwis_dev->dev, "%s: No need power down\n", __func__);
#endif
			return false;
		}
	}
	mutex_unlock(&core.lock);
#ifdef LWIS_PWR_SEQ_DEBUG
	dev_info(lwis_dev->dev, "%s: Ready to power down\n", __func__);
#endif
	return true;
}

static int increase_unified_power_count(struct lwis_device *lwis_dev)
{
	int i;

	if (lwis_dev->power_seq_handler == NULL) {
		return 0;
	}

	mutex_lock(&core.lock);
	for (i = 0; i < MAX_UNIFIED_POWER_DEVICE; i++) {
		if (core.unified_dev_pwr_map[i].dev_node_seq == NULL) {
			break;
		}
		if (core.unified_dev_pwr_map[i].dev_node_seq == lwis_dev->power_seq_handler) {
			core.unified_dev_pwr_map[i].count++;
			if (core.unified_dev_pwr_map[i].count == 1) {
				core.unified_dev_pwr_map[i].hold_dev = lwis_dev;
			}
			mutex_unlock(&core.lock);
#ifdef LWIS_PWR_SEQ_DEBUG
			dev_info(lwis_dev->dev, "%s: power counter = %d\n", __func__,
				 core.unified_dev_pwr_map[i].count);
#endif
			return 0;
		}
	}
	if (i >= MAX_UNIFIED_POWER_DEVICE) {
		dev_err(lwis_dev->dev, "Unified power sequence map overflow\n");
		mutex_unlock(&core.lock);
		return -EOVERFLOW;
	}

	core.unified_dev_pwr_map[i].dev_node_seq = lwis_dev->power_seq_handler;
	core.unified_dev_pwr_map[i].hold_dev = lwis_dev;
	core.unified_dev_pwr_map[i].count++;
	mutex_unlock(&core.lock);

#ifdef LWIS_PWR_SEQ_DEBUG
	dev_info(lwis_dev->dev, "%s: power counter = %d\n", __func__,
		 core.unified_dev_pwr_map[i].count);
#endif
	return 0;
}

static int decrease_unified_power_count(struct lwis_device *lwis_dev)
{
	int i;

	if (lwis_dev->power_seq_handler == NULL) {
		return 0;
	}

	mutex_lock(&core.lock);
	for (i = 0; i < MAX_UNIFIED_POWER_DEVICE; i++) {
		if (core.unified_dev_pwr_map[i].dev_node_seq == NULL) {
			break;
		}
		if (core.unified_dev_pwr_map[i].dev_node_seq == lwis_dev->power_seq_handler) {
			if (core.unified_dev_pwr_map[i].count > 0) {
				core.unified_dev_pwr_map[i].count--;
				if (core.unified_dev_pwr_map[i].count == 0) {
					core.unified_dev_pwr_map[i].hold_dev = NULL;
				}
			}
			mutex_unlock(&core.lock);
#ifdef LWIS_PWR_SEQ_DEBUG
			dev_info(lwis_dev->dev, "%s: power counter = %d\n", __func__,
				 core.unified_dev_pwr_map[i].count);
#endif
			return 0;
		}
	}
	mutex_unlock(&core.lock);
	dev_err(lwis_dev->dev, "Unified power sequence not found\n");
	return -ENODEV;
}

static struct lwis_device *get_power_down_dev(struct lwis_device *lwis_dev)
{
	int i;

	if (lwis_dev->power_seq_handler == NULL) {
		return lwis_dev;
	}

	mutex_lock(&core.lock);
	for (i = 0; i < MAX_UNIFIED_POWER_DEVICE; i++) {
		if (core.unified_dev_pwr_map[i].dev_node_seq == NULL) {
			break;
		}
		if (core.unified_dev_pwr_map[i].dev_node_seq == lwis_dev->power_seq_handler) {
			mutex_unlock(&core.lock);
#ifdef LWIS_PWR_SEQ_DEBUG
			dev_info(lwis_dev->dev, "%s: power dev = %s\n", __func__,
				 core.unified_dev_pwr_map[i].hold_dev->name);
#endif
			return core.unified_dev_pwr_map[i].hold_dev;
		}
	}
	if (i >= MAX_UNIFIED_POWER_DEVICE) {
		dev_err(lwis_dev->dev, "Unified power sequence not found\n");
		mutex_unlock(&core.lock);
		return lwis_dev;
	}
	mutex_unlock(&core.lock);

	return lwis_dev;
}

int lwis_dev_process_power_sequence(struct lwis_device *lwis_dev,
				    struct lwis_device_power_sequence_list *list, bool set_active,
				    bool skip_error)
{
	int ret = 0;
	int last_error = 0;
	int i;

	if (lwis_dev == NULL) {
		pr_err("lwis_dev is NULL\n");
		return -ENODEV;
	}

	if (list == NULL || list->count == 0) {
		dev_err(lwis_dev->dev, "No power_up_sequence defined\n");
		return -EINVAL;
	}
	LWIS_ATRACE_FUNC_BEGIN(lwis_dev, "lwis_dev_process_power_sequence");
	for (i = 0; i < list->count; ++i) {
#ifdef LWIS_PWR_SEQ_DEBUG
		dev_info(lwis_dev->dev, "%s: %d - type:%s name:%s delay_us:%d", __func__, i,
			 list->seq_info[i].type, list->seq_info[i].name,
			 list->seq_info[i].delay_us);
#endif
		if (strcmp(list->seq_info[i].type, "regulator") == 0) {
			if (lwis_dev->regulators == NULL) {
				dev_err(lwis_dev->dev, "No regulators defined\n");
				ret = -EINVAL;
				if (!skip_error) {
					return ret;
				} else {
					last_error = ret;
					continue;
				}
			}
			if (set_active) {
				ret = lwis_regulator_enable_by_name(lwis_dev->regulators,
								    list->seq_info[i].name);
			} else {
				ret = lwis_regulator_disable_by_name(lwis_dev->regulators,
								     list->seq_info[i].name);
			}
			if (ret) {
				dev_err(lwis_dev->dev, "Error set regulators (%d)\n", ret);
				if (!skip_error) {
					LWIS_ATRACE_FUNC_END(lwis_dev,
							     "lwis_dev_process_power_sequence");
					return ret;
				}
				last_error = ret;
			}
		} else if (strcmp(list->seq_info[i].type, "gpio") == 0) {
			struct lwis_gpios_info *gpios_info = NULL;
			int set_value = 0;
			bool set_state = true;

			gpios_info = lwis_gpios_get_info_by_name(lwis_dev->gpios_list,
								 list->seq_info[i].name);
			if (IS_ERR_OR_NULL(gpios_info)) {
				dev_err(lwis_dev->dev, "Get %s gpios info failed\n",
					list->seq_info[i].name);
				ret = PTR_ERR(gpios_info);
				if (!skip_error) {
					LWIS_ATRACE_FUNC_END(lwis_dev,
							     "lwis_dev_process_power_sequence");
					return ret;
				} else {
					last_error = ret;
					continue;
				}
			}

			if (set_active) {
				struct gpio_descs *gpios = NULL;
				gpios = lwis_gpio_list_get(&lwis_dev->plat_dev->dev,
							   list->seq_info[i].name);
				if (IS_ERR_OR_NULL(gpios)) {
					gpios_info->gpios = NULL;
					ret = PTR_ERR(gpios);
					if (ret == -EBUSY && gpios_info->is_shared) {
						dev_warn(
							lwis_dev->dev,
							"Shared gpios requested by another device\n");
						ret = 0;
						continue;
					} else {
						dev_err(lwis_dev->dev,
							"Failed to obtain gpio list (%d)\n", ret);
						if (!skip_error) {
							LWIS_ATRACE_FUNC_END(
								lwis_dev,
								"lwis_dev_process_power_sequence");
							return ret;
						} else {
							last_error = ret;
							continue;
						}
					}
				}
				gpios_info->gpios = gpios;
				set_value = 1;
			} else {
				if (gpios_info->gpios == NULL) {
					if (gpios_info->is_shared) {
						continue;
					}
					dev_err(lwis_dev->dev, "No %s gpios defined\n",
						list->seq_info[i].name);
					ret = -ENODEV;
					if (!skip_error) {
						LWIS_ATRACE_FUNC_END(
							lwis_dev,
							"lwis_dev_process_power_sequence");
						return ret;
					} else {
						last_error = ret;
						continue;
					}
				}
				set_value = 0;
			}

			if (gpios_info->is_shared && !set_active) {
				struct lwis_device *lwis_dev_it;
				struct lwis_gpios_info *gpios_info_it;

				/* Look up if gpio it's already acquired */
				mutex_lock(&core.lock);
				list_for_each_entry (lwis_dev_it, &core.lwis_dev_list, dev_list) {
					if ((lwis_dev->id != lwis_dev_it->id) &&
					    lwis_dev_it->enabled && lwis_dev_it->gpios_list) {
						gpios_info_it = lwis_gpios_get_info_by_name(
							lwis_dev_it->gpios_list,
							list->seq_info[i].name);
						if (IS_ERR_OR_NULL(gpios_info_it)) {
							continue;
						}
						if (gpios_info_it->id == gpios_info->id &&
						    gpios_info_it->gpios == NULL) {
							dev_info(lwis_dev->dev,
								 "Handover shared GPIO to %s\n",
								 lwis_dev_it->name);
							gpios_info_it->gpios = gpios_info->gpios;
							gpios_info_it->hold_dev =
								gpios_info->hold_dev;
							set_state = false;
							gpios_info->gpios = NULL;
						}
						break;
					}
				}
				mutex_unlock(&core.lock);
			}
			if (!set_state) {
				continue;
			}

			if (gpios_info->is_pulse) {
				ret = lwis_gpio_list_set_output_value(gpios_info->gpios,
								      1 - set_value);
				if (ret) {
					dev_err(lwis_dev->dev, "Error set GPIO pins (%d)\n", ret);
					if (!skip_error) {
						LWIS_ATRACE_FUNC_END(
							lwis_dev,
							"lwis_dev_process_power_sequence");
						return ret;
					}
					last_error = ret;
				}
				usleep_range(1000, 1500);
			}
			ret = lwis_gpio_list_set_output_value(gpios_info->gpios, set_value);
			if (ret) {
				dev_err(lwis_dev->dev, "Error set GPIO pins (%d)\n", ret);
				if (!skip_error) {
					LWIS_ATRACE_FUNC_END(lwis_dev,
							     "lwis_dev_process_power_sequence");
					return ret;
				}
				last_error = ret;
			}

			if (!set_active) {
				/* Release "ownership" of the GPIO pins */
				lwis_gpio_list_put(gpios_info->gpios, gpios_info->hold_dev);
				gpios_info->gpios = NULL;
			}
		} else if (strcmp(list->seq_info[i].type, "pinctrl") == 0) {
			bool set_state = true;

			if (set_active) {
				lwis_dev->mclk_ctrl = devm_pinctrl_get(&lwis_dev->plat_dev->dev);
				if (IS_ERR_OR_NULL(lwis_dev->mclk_ctrl)) {
					dev_err(lwis_dev->dev, "Failed to get mclk\n");
					ret = PTR_ERR(lwis_dev->mclk_ctrl);
					lwis_dev->mclk_ctrl = NULL;
					if (!skip_error) {
						LWIS_ATRACE_FUNC_END(
							lwis_dev,
							"lwis_dev_process_power_sequence");
						return ret;
					} else {
						last_error = ret;
						continue;
					}
				}
			} else {
				if (lwis_dev->mclk_ctrl == NULL) {
					dev_err(lwis_dev->dev, "No pinctrl defined\n");
					ret = -ENODEV;
					if (!skip_error) {
						LWIS_ATRACE_FUNC_END(
							lwis_dev,
							"lwis_dev_process_power_sequence");
						return ret;
					} else {
						last_error = ret;
						continue;
					}
				}
			}

			if (lwis_dev->shared_pinctrl) {
				struct lwis_device *lwis_dev_it;
				/* Look up if pinctrl it's already acquired */
				mutex_lock(&core.lock);
				list_for_each_entry (lwis_dev_it, &core.lwis_dev_list, dev_list) {
					if ((lwis_dev->id != lwis_dev_it->id) &&
					    (lwis_dev_it->shared_pinctrl ==
					     lwis_dev->shared_pinctrl) &&
					    lwis_dev_it->enabled) {
						dev_info(lwis_dev->dev, "mclk already acquired\n");
						if (set_active) {
							devm_pinctrl_put(lwis_dev->mclk_ctrl);
						} else {
							/*
							 * Move mclk owner to the device who
							 * still using it
							 */
							lwis_dev_it->mclk_ctrl =
								lwis_dev->mclk_ctrl;
						}
						set_state = false;
						lwis_dev->mclk_ctrl = NULL;
						break;
					}
				}
				mutex_unlock(&core.lock);
			}

			if (set_state) {
				/* Set MCLK state */
				ret = lwis_pinctrl_set_state(lwis_dev->mclk_ctrl,
							     list->seq_info[i].name);
				if (ret) {
					dev_err(lwis_dev->dev, "Error setting %s state (%d)\n",
						list->seq_info[i].name, ret);
					if (set_active) {
						devm_pinctrl_put(lwis_dev->mclk_ctrl);
						lwis_dev->mclk_ctrl = NULL;
					}
					if (!skip_error) {
						LWIS_ATRACE_FUNC_END(
							lwis_dev,
							"lwis_dev_process_power_sequence");
						return ret;
					} else {
						last_error = ret;
						continue;
					}
				}
				if (!set_active) {
					devm_pinctrl_put(lwis_dev->mclk_ctrl);
					lwis_dev->mclk_ctrl = NULL;
				}
			}
		}
		usleep_range(list->seq_info[i].delay_us, list->seq_info[i].delay_us);
	}

	if (last_error) {
		LWIS_ATRACE_FUNC_END(lwis_dev, "lwis_dev_process_power_sequence");
		return last_error;
	}
	LWIS_ATRACE_FUNC_END(lwis_dev, "lwis_dev_process_power_sequence");
	return ret;
}

static int lwis_dev_power_up_by_default(struct lwis_device *lwis_dev)
{
	int ret;

	if (lwis_dev == NULL) {
		pr_err("lwis_dev is NULL\n");
		return -ENODEV;
	}

	if (lwis_dev->regulators) {
		/* Enable all the regulators related to this sensor */
		ret = lwis_regulator_enable_all(lwis_dev->regulators);
		if (ret) {
			dev_err(lwis_dev->dev, "Error enabling regulators (%d)\n", ret);
			return ret;
		}
	}

	if (lwis_dev->enable_gpios_present) {
		struct gpio_descs *gpios;

		gpios = lwis_gpio_list_get(&lwis_dev->plat_dev->dev, "enable");
		if (IS_ERR_OR_NULL(gpios)) {
			dev_err(lwis_dev->dev, "Failed to obtain enable gpio list (%ld)\n",
				PTR_ERR(gpios));
			return PTR_ERR(gpios);
		}

		/*
		 * Setting enable_gpios to non-NULL to indicate that this lwis
		 * device is holding onto the GPIO pins.
		 */
		lwis_dev->enable_gpios = gpios;

		/* Set enable pins to 1 (i.e. asserted) */
		ret = lwis_gpio_list_set_output_value(gpios, 1);
		if (ret) {
			dev_err(lwis_dev->dev, "Error enabling GPIO pins (%d)\n", ret);
			return ret;
		}

		if (lwis_dev->enable_gpios_settle_time > 0) {
			usleep_range(lwis_dev->enable_gpios_settle_time,
				     lwis_dev->enable_gpios_settle_time);
		}
	}

	if (lwis_dev->shared_enable_gpios_present) {
		struct gpio_descs *gpios;

		gpios = lwis_gpio_list_get(&lwis_dev->plat_dev->dev, "shared-enable");
		if (IS_ERR_OR_NULL(gpios)) {
			if (PTR_ERR(gpios) == -EBUSY) {
				dev_warn(lwis_dev->dev,
					 "Shared gpios requested by another device\n");
			} else {
				dev_err(lwis_dev->dev, "Failed to obtain shared gpio list (%ld)\n",
					PTR_ERR(gpios));
				return PTR_ERR(gpios);
			}
		} else {
			lwis_dev->shared_enable_gpios = gpios;
			/* Set enable pins to 1 (i.e. asserted) */
			ret = lwis_gpio_list_set_output_value(gpios, 1);
			if (ret) {
				dev_err(lwis_dev->dev, "Error enabling GPIO pins (%d)\n", ret);
				return ret;
			}
		}
	}

	if (lwis_dev->mclk_present) {
		bool activate_mclk = true;

		lwis_dev->mclk_ctrl = devm_pinctrl_get(&lwis_dev->plat_dev->dev);
		if (IS_ERR_OR_NULL(lwis_dev->mclk_ctrl)) {
			dev_err(lwis_dev->dev, "Failed to get mclk\n");
			ret = PTR_ERR(lwis_dev->mclk_ctrl);
			lwis_dev->mclk_ctrl = NULL;
			return ret;
		}

		if (lwis_dev->shared_pinctrl > 0) {
			struct lwis_device *lwis_dev_it;
			/* Look up if pinctrl it's already enabled */
			mutex_lock(&core.lock);
			list_for_each_entry (lwis_dev_it, &core.lwis_dev_list, dev_list) {
				if ((lwis_dev->id != lwis_dev_it->id) &&
				    (lwis_dev_it->shared_pinctrl == lwis_dev->shared_pinctrl) &&
				    lwis_dev_it->enabled) {
					activate_mclk = false;
					devm_pinctrl_put(lwis_dev->mclk_ctrl);
					lwis_dev->mclk_ctrl = NULL;
					dev_info(lwis_dev->dev, "mclk already acquired\n");
					break;
				}
			}
			mutex_unlock(&core.lock);
		}

		if (activate_mclk) {
			/* Set MCLK state to on */
			ret = lwis_pinctrl_set_state(lwis_dev->mclk_ctrl, MCLK_ON_STRING);
			if (ret) {
				dev_err(lwis_dev->dev, "Error setting %s state (%d)\n",
					MCLK_ON_STRING, ret);
				devm_pinctrl_put(lwis_dev->mclk_ctrl);
				lwis_dev->mclk_ctrl = NULL;
				return ret;
			}
		}
	}

	if (lwis_dev->reset_gpios_present) {
		struct gpio_descs *gpios;

		gpios = lwis_gpio_list_get(&lwis_dev->plat_dev->dev, "reset");
		if (IS_ERR_OR_NULL(gpios)) {
			dev_err(lwis_dev->dev, "Failed to obtain reset gpio list (%ld)\n",
				PTR_ERR(gpios));
			return PTR_ERR(gpios);
		}
		/*
		 * Setting reset_gpios to non-NULL to indicate that this lwis
		 * device is holding onto the GPIO pins.
		 */
		lwis_dev->reset_gpios = gpios;

		/* Set reset pin to 0 (i.e. deasserted) */
		ret = lwis_gpio_list_set_output_value(gpios, 0);
		if (ret) {
			dev_err(lwis_dev->dev, "Failed to set reset GPIOs to ACTIVE (%d)\n", ret);
			return ret;
		}

		usleep_range(1000, 1500);

		/* Set reset pin to 1 (i.e. asserted) */
		ret = lwis_gpio_list_set_output_value(gpios, 1);
		if (ret) {
			dev_err(lwis_dev->dev, "Failed to set reset GPIOs to DE-ACTIVE (%d)\n",
				ret);
			return ret;
		}
	}

	return 0;
}

/*
 * Power up a LWIS device, should be called when lwis_dev->enabled is 0
 * lwis_dev->client_lock should be held before this function.
 */
int lwis_dev_power_up_locked(struct lwis_device *lwis_dev)
{
	int ret;
	struct lwis_i2c_device *i2c_dev = NULL;

	if (lwis_dev->type == DEVICE_TYPE_I2C) {
		i2c_dev = container_of(lwis_dev, struct lwis_i2c_device, base_dev);
		if (i2c_dev->group_i2c_lock == NULL) {
			dev_err(lwis_dev->dev, "group_i2c_lock is NULL. Abort power up.\n");
			return -EINVAL;
		}
	}

	/* Let's do the platform-specific enable call */
	ret = lwis_platform_device_enable(lwis_dev);
	if (ret) {
		dev_err(lwis_dev->dev, "Platform-specific device enable fail: %d\n", ret);
		goto error_power_up;
	}

	if (lwis_dev->clocks) {
		/* Enable clocks */
		ret = lwis_clock_enable_all(lwis_dev->clocks);
		if (ret) {
			dev_err(lwis_dev->dev, "Error enabling clocks (%d)\n", ret);
			goto error_power_up;
		}
	}

	if (lwis_dev->type == DEVICE_TYPE_I2C) {
		mutex_lock(i2c_dev->group_i2c_lock);
	}
	if (lwis_dev->power_up_sequence) {
		if (need_to_power_up(lwis_dev)) {
			ret = lwis_dev_process_power_sequence(lwis_dev, lwis_dev->power_up_sequence,
							      /*set_active=*/true,
							      /*skip_error=*/false);
			if (ret) {
				dev_err(lwis_dev->dev,
					"Error lwis_dev_process_power_sequence (%d)\n", ret);
				if (lwis_dev->type == DEVICE_TYPE_I2C) {
					mutex_unlock(i2c_dev->group_i2c_lock);
				}
				goto error_power_up;
			}
		}
		increase_unified_power_count(lwis_dev);
	} else {
		ret = lwis_dev_power_up_by_default(lwis_dev);
		if (ret) {
			dev_err(lwis_dev->dev, "Error lwis_dev_power_up_by_default (%d)\n", ret);
			if (lwis_dev->type == DEVICE_TYPE_I2C) {
				mutex_unlock(i2c_dev->group_i2c_lock);
			}
			goto error_power_up;
		}
	}
	if (lwis_dev->type == DEVICE_TYPE_I2C) {
		mutex_unlock(i2c_dev->group_i2c_lock);
	}

	if (lwis_dev->phys) {
		/* Power on the PHY */
		ret = lwis_phy_set_power_all(lwis_dev->phys,
					     /* power_on = */ true);
		if (ret) {
			dev_err(lwis_dev->dev, "Error powering on PHY\n");
			goto error_power_up;
		}
	}

	if (lwis_dev->vops.device_enable) {
		ret = lwis_dev->vops.device_enable(lwis_dev);
		if (ret) {
			dev_err(lwis_dev->dev, "Error executing device enable function\n");
			goto error_power_up;
		}
	}

	/* Sleeping to make sure all pins are ready to go */
	usleep_range(2000, 2000);
	return 0;

	/* Error handling */
error_power_up:
	lwis_dev_power_down_locked(lwis_dev);
	return ret;
}

static int lwis_dev_power_down_by_default(struct lwis_device *lwis_dev)
{
	int last_error = 0;
	int ret;

	if (lwis_dev == NULL) {
		pr_err("lwis_dev is NULL\n");
		return -ENODEV;
	}

	if (lwis_dev->mclk_ctrl) {
		bool deactivate_mclk = true;

		if (lwis_dev->shared_pinctrl) {
			struct lwis_device *lwis_dev_it;
			/* Look up if pinctrl still used by other device */
			mutex_lock(&core.lock);
			list_for_each_entry (lwis_dev_it, &core.lwis_dev_list, dev_list) {
				if ((lwis_dev->id != lwis_dev_it->id) &&
				    (lwis_dev_it->shared_pinctrl == lwis_dev->shared_pinctrl) &&
				    lwis_dev_it->enabled) {
					/*
					 * Move mclk owner to the device who
					 * still using it
					 */
					lwis_dev_it->mclk_ctrl = lwis_dev->mclk_ctrl;
					lwis_dev->mclk_ctrl = NULL;
					deactivate_mclk = false;
					break;
				}
			}
			mutex_unlock(&core.lock);
		}

		if (deactivate_mclk) {
			/* Set MCLK state to off */
			ret = lwis_pinctrl_set_state(lwis_dev->mclk_ctrl, MCLK_OFF_STRING);
			if (ret) {
				dev_err(lwis_dev->dev, "Error setting %s state (%d)\n",
					MCLK_OFF_STRING, ret);
				last_error = ret;
			}
			devm_pinctrl_put(lwis_dev->mclk_ctrl);
			lwis_dev->mclk_ctrl = NULL;
		}
	}

	if (lwis_dev->reset_gpios_present && lwis_dev->reset_gpios) {
		/* Set reset pins to low */
		ret = lwis_gpio_list_set_output_value(lwis_dev->reset_gpios, 0);
		if (ret) {
			dev_err(lwis_dev->dev, "Error setting reset GPIOs to ACTIVE (%d)\n", ret);
			last_error = ret;
		}

		/* Release ownership of the GPIO pins */
		lwis_gpio_list_put(lwis_dev->reset_gpios, &lwis_dev->plat_dev->dev);
		lwis_dev->reset_gpios = NULL;
	}

	if (lwis_dev->shared_enable_gpios_present && lwis_dev->shared_enable_gpios) {
		/* Set enable pins to 0 (i.e. deasserted) */
		ret = lwis_gpio_list_set_output_value(lwis_dev->shared_enable_gpios, 0);
		if (ret) {
			dev_err(lwis_dev->dev, "Error disabling GPIO pins (%d)\n", ret);
			last_error = ret;
		}

		/* Release "ownership" of the GPIO pins */
		lwis_gpio_list_put(lwis_dev->shared_enable_gpios, &lwis_dev->plat_dev->dev);
		lwis_dev->shared_enable_gpios = NULL;
	}

	if (lwis_dev->enable_gpios_present && lwis_dev->enable_gpios) {
		/* Set enable pins to 0 (i.e. deasserted) */
		ret = lwis_gpio_list_set_output_value(lwis_dev->enable_gpios, 0);
		if (ret) {
			dev_err(lwis_dev->dev, "Error disabling GPIO pins (%d)\n", ret);
			last_error = ret;
		}

		/* Release "ownership" of the GPIO pins */
		lwis_gpio_list_put(lwis_dev->enable_gpios, &lwis_dev->plat_dev->dev);
		lwis_dev->enable_gpios = NULL;
	}

	if (lwis_dev->regulators) {
		/* Disable all the regulators */
		ret = lwis_regulator_disable_all(lwis_dev->regulators);
		if (ret) {
			dev_err(lwis_dev->dev, "Error disabling regulators (%d)\n", ret);
			last_error = ret;
		}
	}

	if (last_error) {
		return last_error;
	}

	return 0;
}

/*
 * Power down a LWIS device, should be called when lwis_dev->enabled become 0
 * lwis_dev->client_lock should be held before this function.
 */
int lwis_dev_power_down_locked(struct lwis_device *lwis_dev)
{
	int ret;
	int last_error = 0;
	struct lwis_i2c_device *i2c_dev = NULL;

	if (lwis_dev->type == DEVICE_TYPE_I2C) {
		i2c_dev = container_of(lwis_dev, struct lwis_i2c_device, base_dev);
		if (i2c_dev->group_i2c_lock == NULL) {
			dev_err(lwis_dev->dev, "group_i2c_lock is NULL. Abort power up.\n");
			return -EINVAL;
		}
	}

	if (lwis_dev->vops.device_disable) {
		ret = lwis_dev->vops.device_disable(lwis_dev);
		if (ret) {
			dev_err(lwis_dev->dev, "Error executing device disable function\n");
			last_error = ret;
		}
	}

	if (lwis_dev->phys) {
		/* Power on the PHY */
		ret = lwis_phy_set_power_all(lwis_dev->phys,
					     /* power_on = */ false);
		if (ret) {
			dev_err(lwis_dev->dev, "Error powering off PHY\n");
			last_error = ret;
		}
	}

	if (lwis_dev->type == DEVICE_TYPE_I2C) {
		mutex_lock(i2c_dev->group_i2c_lock);
	}
	if (lwis_dev->power_down_sequence) {
		if (need_to_power_down(lwis_dev)) {
			struct lwis_device *power_dev = get_power_down_dev(lwis_dev);
			ret = lwis_dev_process_power_sequence(power_dev,
							      power_dev->power_down_sequence,
							      /*set_active=*/false,
							      /*skip_error=*/true);
			if (ret) {
				dev_err(lwis_dev->dev,
					"Error lwis_dev_process_power_sequence (%d)\n", ret);
				last_error = ret;
			}
		}
		decrease_unified_power_count(lwis_dev);
	} else {
		ret = lwis_dev_power_down_by_default(lwis_dev);
		if (ret) {
			dev_err(lwis_dev->dev, "Error lwis_dev_power_down_by_default (%d)\n", ret);
			last_error = ret;
		}
	}
	if (lwis_dev->type == DEVICE_TYPE_I2C) {
		mutex_unlock(i2c_dev->group_i2c_lock);
	}

	if (lwis_dev->clocks) {
		/* Disable all clocks */
		lwis_clock_disable_all(lwis_dev->clocks);
	}

	/* Let's do the platform-specific disable call */
	ret = lwis_platform_device_disable(lwis_dev);
	if (ret) {
		dev_err(lwis_dev->dev, "Platform-specific device disable fail: %d\n", ret);
		last_error = ret;
	}

	if (last_error) {
		return last_error;
	}

	return ret;
}

/*
 *  lwis_dev_power_seq_list_alloc:
 *  Allocate an instance of the lwis_device_power_sequence_info
 *  and initialize the data structures according to the number of
 *  lwis_device_power_sequence_info specified.
 */
struct lwis_device_power_sequence_list *lwis_dev_power_seq_list_alloc(int count)
{
	struct lwis_device_power_sequence_list *list;

	/* No need to allocate if count is invalid */
	if (count <= 0) {
		return ERR_PTR(-EINVAL);
	}

	list = kmalloc(sizeof(struct lwis_device_power_sequence_list), GFP_KERNEL);
	if (!list) {
		pr_err("Failed to allocate power sequence list\n");
		return ERR_PTR(-ENOMEM);
	}

	list->seq_info =
		kmalloc(count * sizeof(struct lwis_device_power_sequence_info), GFP_KERNEL);
	if (!list->seq_info) {
		pr_err("Failed to allocate lwis_device_power_sequence_info "
		       "instances\n");
		kfree(list);
		return ERR_PTR(-ENOMEM);
	}

	list->count = count;

	return list;
}

/*
 *  lwis_dev_power_seq_list_free: Deallocate the
 *  lwis_device_power_sequence_info structure.
 */
void lwis_dev_power_seq_list_free(struct lwis_device_power_sequence_list *list)
{
	if (!list) {
		return;
	}

	if (list->seq_info) {
		kfree(list->seq_info);
	}

	kfree(list);
}

/*
 *  lwis_dev_power_seq_list_print:
 *  Print lwis_device_power_sequence_list content
 */
void lwis_dev_power_seq_list_print(struct lwis_device_power_sequence_list *list)
{
	int i;

	for (i = 0; i < list->count; ++i) {
		pr_info("type:%s  name:%s  delay_us:%d\n", list->seq_info[i].type,
			list->seq_info[i].name, list->seq_info[i].delay_us);
	}
}

static struct lwis_device *find_top_dev(void)
{
	struct lwis_device *lwis_dev;
	mutex_lock(&core.lock);
	list_for_each_entry (lwis_dev, &core.lwis_dev_list, dev_list) {
		if (lwis_dev->type == DEVICE_TYPE_TOP) {
			mutex_unlock(&core.lock);
			return lwis_dev;
		}
	}
	mutex_unlock(&core.lock);
	return NULL;
}

static void event_heartbeat_timer(struct timer_list *t)
{
	struct lwis_device *lwis_dev = from_timer(lwis_dev, t, heartbeat_timer);
	int64_t event_id = LWIS_EVENT_ID_HEARTBEAT | (int64_t)lwis_dev->id
							     << LWIS_EVENT_ID_EVENT_CODE_LEN;

	lwis_device_event_emit(lwis_dev, event_id, NULL, 0);

	mod_timer(t, jiffies + msecs_to_jiffies(LWIS_HEARTBEAT_EVENT_INTERVAL_MS));
}

/*
 *  lwis_find_dev_by_id: Find LWIS device by id.
 */
struct lwis_device *lwis_find_dev_by_id(int dev_id)
{
	struct lwis_device *lwis_dev;

	mutex_lock(&core.lock);
	list_for_each_entry (lwis_dev, &core.lwis_dev_list, dev_list) {
		if (lwis_dev->id == dev_id) {
			mutex_unlock(&core.lock);
			return lwis_dev;
		}
	}
	mutex_unlock(&core.lock);
	return NULL;
}

/*
 *  lwis_i2c_dev_is_in_use: Check i2c device is in use.
 */
bool lwis_i2c_dev_is_in_use(struct lwis_device *lwis_dev)
{
	struct lwis_device *lwis_dev_it;
	struct lwis_i2c_device *i2c_dev;

	if (lwis_dev->type != DEVICE_TYPE_I2C) {
		dev_err(lwis_dev->dev, "It's not i2c device\n");
		return false;
	}

	i2c_dev = container_of(lwis_dev, struct lwis_i2c_device, base_dev);
	mutex_lock(&core.lock);
	list_for_each_entry (lwis_dev_it, &core.lwis_dev_list, dev_list) {
		if (lwis_dev_it->type == DEVICE_TYPE_I2C) {
			struct lwis_i2c_device *i2c_dev_it =
				container_of(lwis_dev_it, struct lwis_i2c_device, base_dev);
			/* Look up if i2c bus are still in use by other device*/
			if ((i2c_dev_it->state_pinctrl == i2c_dev->state_pinctrl) &&
			    (i2c_dev_it != i2c_dev) && lwis_dev_it->enabled) {
				mutex_unlock(&core.lock);
				return true;
			}
		}
	}
	mutex_unlock(&core.lock);
	return false;
}

void lwis_device_info_dump(const char *name, void (*func)(struct lwis_device *))
{
	struct lwis_device *lwis_dev_it;

	pr_info("LWIS Device Info Dump: %s\n\n", name);

	mutex_lock(&core.lock);
	list_for_each_entry (lwis_dev_it, &core.lwis_dev_list, dev_list) {
		func(lwis_dev_it);
	}
	mutex_unlock(&core.lock);
}

void lwis_save_register_io_info(struct lwis_device *lwis_dev, struct lwis_io_entry *io_entry,
				size_t access_size)
{
	lwis_dev->debug_info.io_entry_hist[lwis_dev->debug_info.cur_io_entry_hist_idx].io_entry =
		*io_entry;
	lwis_dev->debug_info.io_entry_hist[lwis_dev->debug_info.cur_io_entry_hist_idx].access_size =
		access_size;
	lwis_dev->debug_info.io_entry_hist[lwis_dev->debug_info.cur_io_entry_hist_idx]
		.start_timestamp = ktime_to_ns(lwis_get_time());
	lwis_dev->debug_info.cur_io_entry_hist_idx++;
	if (lwis_dev->debug_info.cur_io_entry_hist_idx >= IO_ENTRY_DEBUG_HISTORY_SIZE) {
		lwis_dev->debug_info.cur_io_entry_hist_idx = 0;
	}
}

/*
 *  lwis_base_probe: Create a device instance for each of the LWIS device.
 */
int lwis_base_probe(struct lwis_device *lwis_dev, struct platform_device *plat_dev)
{
	int ret = 0;

	/* Allocate a minor number to this device */
	mutex_lock(&core.lock);
	ret = idr_alloc(core.idr, lwis_dev, 0, LWIS_MAX_DEVICES, GFP_KERNEL);
	mutex_unlock(&core.lock);
	if (ret >= 0) {
		lwis_dev->id = ret;
	} else {
		dev_err(&plat_dev->dev, "Unable to allocate minor ID (%d)\n", ret);
		return ret;
	}

	/* Initialize enabled state */
	lwis_dev->enabled = 0;
	lwis_dev->is_suspended = false;
	lwis_dev->clock_family = CLOCK_FAMILY_INVALID;

	/* Initialize client mutex */
	mutex_init(&lwis_dev->client_lock);

	/* Initialize an empty list of clients */
	INIT_LIST_HEAD(&lwis_dev->clients);

	/* Initialize event state hash table */
	hash_init(lwis_dev->event_states);

	/* Initialize the spinlock */
	spin_lock_init(&lwis_dev->lock);

	if (lwis_dev->type == DEVICE_TYPE_TOP) {
		lwis_dev->top_dev = lwis_dev;
		/* Assign top device to the devices probed before */
		lwis_assign_top_to_other(lwis_dev);
	} else {
		lwis_dev->top_dev = find_top_dev();
		if (lwis_dev->top_dev == NULL)
			pr_warn("Top device not probed yet");
	}

	/* Add this instance to the device list */
	mutex_lock(&core.lock);
	list_add(&lwis_dev->dev_list, &core.lwis_dev_list);
	mutex_unlock(&core.lock);

	lwis_dev->plat_dev = plat_dev;
	ret = lwis_base_setup(lwis_dev);
	if (ret) {
		pr_err("Error initializing LWIS device\n");
		goto error_init;
	}

	/* Upon success initialization, create device for this instance */
	lwis_dev->dev = device_create(core.dev_class, NULL, MKDEV(core.device_major, lwis_dev->id),
				      lwis_dev, LWIS_DEVICE_NAME "-%s", lwis_dev->name);
	if (IS_ERR_OR_NULL(lwis_dev->dev)) {
		pr_err("Failed to create device\n");
		ret = PTR_ERR(lwis_dev->dev);
		goto error_init;
	}

	platform_set_drvdata(plat_dev, lwis_dev);

	/* Call platform-specific probe function */
	lwis_platform_probe(lwis_dev);

	lwis_device_debugfs_setup(lwis_dev, core.dbg_root);

	timer_setup(&lwis_dev->heartbeat_timer, event_heartbeat_timer, 0);

	dev_info(lwis_dev->dev, "Base Probe: Success\n");

	return ret;

	/* Error conditions */
error_init:
	lwis_base_unprobe(lwis_dev);
	mutex_lock(&core.lock);
	idr_remove(core.idr, lwis_dev->id);
	mutex_unlock(&core.lock);
	return ret;
}

/*
 *  lwis_base_unprobe: Cleanup a device instance
 */
void lwis_base_unprobe(struct lwis_device *unprobe_lwis_dev)
{
	struct lwis_device *lwis_dev, *temp;

	mutex_lock(&core.lock);
	list_for_each_entry_safe (lwis_dev, temp, &core.lwis_dev_list, dev_list) {
		if (lwis_dev == unprobe_lwis_dev) {
			pr_info("Destroy device %s id %d", lwis_dev->name, lwis_dev->id);
			lwis_device_debugfs_cleanup(lwis_dev);
			/* Release device clock list */
			if (lwis_dev->clocks) {
				lwis_clock_list_free(lwis_dev->clocks);
				lwis_dev->clocks = NULL;
			}
			/* Release device interrupt list */
			if (lwis_dev->irqs) {
				lwis_interrupt_list_free(lwis_dev->irqs);
				lwis_dev->irqs = NULL;
			}
			/* Release device regulator list */
			if (lwis_dev->regulators) {
				lwis_regulator_put_all(lwis_dev->regulators);
				lwis_regulator_list_free(lwis_dev->regulators);
				lwis_dev->regulators = NULL;
			}
			/* Release device phy list */
			if (lwis_dev->phys) {
				lwis_phy_list_free(lwis_dev->phys);
				lwis_dev->phys = NULL;
			}
			/* Release device power sequence list */
			if (lwis_dev->power_up_sequence) {
				lwis_dev_power_seq_list_free(lwis_dev->power_up_sequence);
				lwis_dev->power_up_sequence = NULL;
			}
			if (lwis_dev->power_down_sequence) {
				lwis_dev_power_seq_list_free(lwis_dev->power_down_sequence);
				lwis_dev->power_down_sequence = NULL;
			}
			/* Release device gpio list */
			if (lwis_dev->gpios_list) {
				lwis_gpios_list_free(lwis_dev->gpios_list);
				lwis_dev->gpios_list = NULL;
			}
			/* Release device gpio info irq list */
			if (lwis_dev->irq_gpios_info.irq_list) {
				lwis_interrupt_list_free(lwis_dev->irq_gpios_info.irq_list);
				lwis_dev->irq_gpios_info.irq_list = NULL;
			}
			if (lwis_dev->irq_gpios_info.gpios) {
				lwis_gpio_list_put(lwis_dev->irq_gpios_info.gpios,
						   &lwis_dev->plat_dev->dev);
				lwis_dev->irq_gpios_info.gpios = NULL;
			}

			/* Disconnect from the bus manager */
			lwis_i2c_bus_manager_disconnect(lwis_dev);

			/* Destroy device */
			if (!IS_ERR_OR_NULL(lwis_dev->dev)) {
				device_destroy(core.dev_class,
					       MKDEV(core.device_major, lwis_dev->id));
			}
			list_del(&lwis_dev->dev_list);

			if (timer_pending(&lwis_dev->heartbeat_timer))
				del_timer(&lwis_dev->heartbeat_timer);
		}
	}
	mutex_unlock(&core.lock);
}

/*
 *  lwis_register_base_device: Create device class and device major number to
 *  the class of LWIS devices.
 *
 *  This is called once when the core LWIS driver is initialized.
 */
static int __init lwis_register_base_device(void)
{
	int ret = 0;

	/* Allocate ID management instance for device minor numbers */
	core.idr = kzalloc(sizeof(struct idr), GFP_KERNEL);
	if (!core.idr) {
		pr_err("Cannot allocate idr instance\n");
		return -ENOMEM;
	}

	mutex_lock(&core.lock);

	idr_init(core.idr);

	/* Acquire device major number and allocate the range to minor numbers
		 to the device */
	ret = alloc_chrdev_region(&core.lwis_devt, 0, LWIS_MAX_DEVICES, LWIS_DEVICE_NAME);
	if (ret) {
		pr_err("Error in allocating chrdev region\n");
		goto error_chrdev_alloc;
	}

	core.device_major = MAJOR(core.lwis_devt);

	/* Create a device class*/
	core.dev_class = class_create(THIS_MODULE, LWIS_CLASS_NAME);
	if (IS_ERR_OR_NULL(core.dev_class)) {
		pr_err("Failed to create device class\n");
		ret = PTR_ERR(core.dev_class);
		goto error_class_create;
	}

	/* Allocate a character device */
	core.chr_dev = cdev_alloc();
	if (!core.chr_dev) {
		pr_err("Failed to allocate cdev\n");
		ret = -ENOMEM;
		goto error_cdev_alloc;
	}

	core.chr_dev->ops = &lwis_fops;

	ret = cdev_add(core.chr_dev, core.lwis_devt, LWIS_MAX_DEVICES);
	if (ret) {
		pr_err("Failed to add cdev\n");
		goto error_cdev_alloc;
	}

	INIT_LIST_HEAD(&core.lwis_dev_list);

#ifdef CONFIG_DEBUG_FS
	/* Create DebugFS directory for LWIS, if avaiable */
	core.dbg_root = debugfs_create_dir("lwis", NULL);
	if (IS_ERR_OR_NULL(core.dbg_root)) {
		/* No need to return error as this is just informational that
		   DebugFS is not present */
		pr_info("Failed to create DebugFS dir - DebugFS not present?");
		core.dbg_root = NULL;
	}
#endif

	mutex_unlock(&core.lock);

	return ret;

	/* Error conditions */
error_cdev_alloc:
	class_destroy(core.dev_class);
	core.dev_class = NULL;
error_class_create:
	unregister_chrdev_region(core.lwis_devt, LWIS_MAX_DEVICES);
error_chrdev_alloc:
	mutex_unlock(&core.lock);
	kfree(core.idr);
	core.idr = NULL;

	return ret;
}

static void lwis_unregister_base_device(void)
{
	mutex_lock(&core.lock);

#ifdef CONFIG_DEBUGFS
	debugfs_remove(core.dbg_root);
	core.dbg_root = NULL;
#endif

	cdev_del(core.chr_dev);
	core.chr_dev = NULL;

	class_destroy(core.dev_class);
	core.dev_class = NULL;

	unregister_chrdev_region(core.lwis_devt, LWIS_MAX_DEVICES);

	kfree(core.idr);
	core.idr = NULL;

	mutex_unlock(&core.lock);
}

/*
 *  lwis_base_device_init: Called during subsys_initcall routines.
 */
static int __init lwis_base_device_init(void)
{
	int ret = 0;

	pr_info("LWIS device initialization\n");

	/* Initialize the core struct */
	memset(&core, 0, sizeof(struct lwis_core));
	mutex_init(&core.lock);

	ret = lwis_register_base_device();
	if (ret) {
		pr_err("Failed to register LWIS base (%d)\n", ret);
		return ret;
	}

	ret = lwis_top_device_init();
	if (ret) {
		pr_err("Failed to lwis_top_device_init (%d)\n", ret);
		goto top_failure;
	}

	ret = lwis_ioreg_device_init();
	if (ret) {
		pr_err("Failed to lwis_ioreg_device_init (%d)\n", ret);
		goto ioreg_failure;
	}

	ret = lwis_i2c_device_init();
	if (ret) {
		pr_err("Failed to lwis_i2c_device_init (%d)\n", ret);
		goto i2c_failure;
	}

	ret = lwis_slc_device_init();
	if (ret) {
		pr_err("Failed to lwis_slc_device_init (%d)\n", ret);
		goto slc_failure;
	}

	ret = lwis_dpm_device_init();
	if (ret) {
		pr_err("Failed to lwis_dpm_device_init (%d)\n", ret);
		goto dpm_failure;
	}

	ret = lwis_test_device_init();
	if (ret) {
		pr_err("Failed to lwis_test_device_init (%d)\n", ret);
		goto test_failure;
	}

	return 0;

test_failure:
	lwis_test_device_deinit();
dpm_failure:
	lwis_slc_device_deinit();
slc_failure:
	lwis_i2c_device_deinit();
i2c_failure:
	lwis_ioreg_device_deinit();
ioreg_failure:
	lwis_top_device_deinit();
top_failure:
	lwis_unregister_base_device();
	return ret;
}

/*
 *  lwis_base_device_deinit: Called when driver is unloaded.
 */
static void __exit lwis_driver_exit(void)
{
	struct lwis_device *lwis_dev, *temp;
	struct lwis_client *client, *client_temp;

	pr_info("%s Clean up LWIS devices.\n", __func__);
	list_for_each_entry_safe (lwis_dev, temp, &core.lwis_dev_list, dev_list) {
		pr_info("Destroy device %s id %d", lwis_dev->name, lwis_dev->id);
		lwis_device_debugfs_cleanup(lwis_dev);
		/* Disable lwis device events */
		lwis_device_event_enable(lwis_dev, LWIS_EVENT_ID_HEARTBEAT, false);
		if (lwis_dev->type == DEVICE_TYPE_I2C) {
			struct lwis_i2c_device *i2c_dev;
			i2c_dev = container_of(lwis_dev, struct lwis_i2c_device, base_dev);
			i2c_unregister_device(i2c_dev->client);
		}
		/* Relase each client registered with dev */
		list_for_each_entry_safe (client, client_temp, &lwis_dev->clients, node) {
			if (lwis_release_client(client))
				pr_info("Failed to release client.");
		}
		pm_runtime_disable(&lwis_dev->plat_dev->dev);
		/* Release device clock list */
		if (lwis_dev->clocks) {
			lwis_clock_list_free(lwis_dev->clocks);
		}
		/* Release device interrupt list */
		if (lwis_dev->irqs) {
			lwis_interrupt_list_free(lwis_dev->irqs);
		}
		/* Release device regulator list */
		if (lwis_dev->regulators) {
			lwis_regulator_put_all(lwis_dev->regulators);
			lwis_regulator_list_free(lwis_dev->regulators);
		}
		/* Release device phy list */
		if (lwis_dev->phys) {
			lwis_phy_list_free(lwis_dev->phys);
		}
		/* Release device power sequence list */
		if (lwis_dev->power_up_sequence) {
			lwis_dev_power_seq_list_free(lwis_dev->power_up_sequence);
		}
		if (lwis_dev->power_down_sequence) {
			lwis_dev_power_seq_list_free(lwis_dev->power_down_sequence);
		}
		/* Release device gpio list */
		if (lwis_dev->gpios_list) {
			lwis_gpios_list_free(lwis_dev->gpios_list);
		}
		/* Release device gpio info irq list */
		if (lwis_dev->irq_gpios_info.irq_list) {
			lwis_interrupt_list_free(lwis_dev->irq_gpios_info.irq_list);
		}
		if (lwis_dev->irq_gpios_info.gpios) {
			lwis_gpio_list_put(lwis_dev->irq_gpios_info.gpios,
					   &lwis_dev->plat_dev->dev);
		}
		if (lwis_dev->reset_gpios) {
			lwis_gpio_list_put(lwis_dev->reset_gpios, &lwis_dev->plat_dev->dev);
		}
		if (lwis_dev->enable_gpios) {
			lwis_gpio_list_put(lwis_dev->enable_gpios, &lwis_dev->plat_dev->dev);
		}
		if (lwis_dev->shared_enable_gpios) {
			lwis_gpio_list_put(lwis_dev->shared_enable_gpios, &lwis_dev->plat_dev->dev);
		}
		/* Release event subscription components */
		if (lwis_dev->type == DEVICE_TYPE_TOP) {
			lwis_dev->top_dev->subscribe_ops.release(lwis_dev);
		}

		/* Destroy device */
		device_destroy(core.dev_class, MKDEV(core.device_major, lwis_dev->id));
		list_del(&lwis_dev->dev_list);
		kfree(lwis_dev);
	}

	/* Deinit device classes */
	lwis_test_device_deinit();
	lwis_dpm_device_deinit();
	lwis_slc_device_deinit();
	lwis_i2c_device_deinit();
	lwis_ioreg_device_deinit();
	lwis_top_device_deinit();

	/* Unregister base lwis device */
	lwis_unregister_base_device();
}

void lwis_process_worker_queue(struct lwis_client *client)
{
	lwis_process_transactions_in_queue(client);
	lwis_process_periodic_io_in_queue(client);
}

subsys_initcall(lwis_base_device_init);
module_exit(lwis_driver_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Google-ACMA");
MODULE_DESCRIPTION("LWIS Base Device Driver");
MODULE_IMPORT_NS(DMA_BUF);